mshtml: Implement IHTMLDOMNode previousSibling.
[wine] / dlls / gdiplus / graphics.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
28
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
34
35 #include "winreg.h"
36 #include "shlwapi.h"
37
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
44
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
48
49 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
51 {
52     REAL radAngle, hypotenuse;
53
54     radAngle = deg2rad(angle);
55     hypotenuse = 50.0; /* arbitrary */
56
57     *x = x_0 + cos(radAngle) * hypotenuse;
58     *y = y_0 + sin(radAngle) * hypotenuse;
59 }
60
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
63 {
64     BYTE ret;
65
66     switch(type & PathPointTypePathTypeMask){
67         case PathPointTypeBezier:
68             ret = PT_BEZIERTO;
69             break;
70         case PathPointTypeLine:
71             ret = PT_LINETO;
72             break;
73         case PathPointTypeStart:
74             ret = PT_MOVETO;
75             break;
76         default:
77             ERR("Bad point type\n");
78             return 0;
79     }
80
81     if(type & PathPointTypeCloseSubpath)
82         ret |= PT_CLOSEFIGURE;
83
84     return ret;
85 }
86
87 static REAL graphics_res(GpGraphics *graphics)
88 {
89     if (graphics->image) return graphics->image->xres;
90     else return (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
91 }
92
93 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
94 {
95     HPEN gdipen;
96     REAL width;
97     INT save_state, i, numdashes;
98     GpPointF pt[2];
99     DWORD dash_array[MAX_DASHLEN];
100
101     save_state = SaveDC(graphics->hdc);
102
103     EndPath(graphics->hdc);
104
105     if(pen->unit == UnitPixel){
106         width = pen->width;
107     }
108     else{
109         /* Get an estimate for the amount the pen width is affected by the world
110          * transform. (This is similar to what some of the wine drivers do.) */
111         pt[0].X = 0.0;
112         pt[0].Y = 0.0;
113         pt[1].X = 1.0;
114         pt[1].Y = 1.0;
115         GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
116         width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
117                      (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
118
119         width *= pen->width * convert_unit(graphics_res(graphics),
120                               pen->unit == UnitWorld ? graphics->unit : pen->unit);
121     }
122
123     if(pen->dash == DashStyleCustom){
124         numdashes = min(pen->numdashes, MAX_DASHLEN);
125
126         TRACE("dashes are: ");
127         for(i = 0; i < numdashes; i++){
128             dash_array[i] = roundr(width * pen->dashes[i]);
129             TRACE("%d, ", dash_array[i]);
130         }
131         TRACE("\n and the pen style is %x\n", pen->style);
132
133         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
134                               numdashes, dash_array);
135     }
136     else
137         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
138
139     SelectObject(graphics->hdc, gdipen);
140
141     return save_state;
142 }
143
144 static void restore_dc(GpGraphics *graphics, INT state)
145 {
146     DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
147     RestoreDC(graphics->hdc, state);
148 }
149
150 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
151         GpCoordinateSpace src_space, GpMatrix **matrix);
152
153 /* This helper applies all the changes that the points listed in ptf need in
154  * order to be drawn on the device context.  In the end, this should include at
155  * least:
156  *  -scaling by page unit
157  *  -applying world transformation
158  *  -converting from float to int
159  * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
160  * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
161  * gdi to draw, and these functions would irreparably mess with line widths.
162  */
163 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
164     GpPointF *ptf, INT count)
165 {
166     REAL unitscale;
167     GpMatrix *matrix;
168     int i;
169
170     unitscale = convert_unit(graphics_res(graphics), graphics->unit);
171
172     /* apply page scale */
173     if(graphics->unit != UnitDisplay)
174         unitscale *= graphics->scale;
175
176     GdipCloneMatrix(graphics->worldtrans, &matrix);
177     GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
178     GdipTransformMatrixPoints(matrix, ptf, count);
179     GdipDeleteMatrix(matrix);
180
181     for(i = 0; i < count; i++){
182         pti[i].x = roundr(ptf[i].X);
183         pti[i].y = roundr(ptf[i].Y);
184     }
185 }
186
187 /* Draw non-premultiplied ARGB data to the given graphics object */
188 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
189     const BYTE *src, INT src_width, INT src_height, INT src_stride)
190 {
191     if (graphics->image && graphics->image->type == ImageTypeBitmap)
192     {
193         GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
194         INT x, y;
195
196         for (x=0; x<src_width; x++)
197         {
198             for (y=0; y<src_height; y++)
199             {
200                 ARGB dst_color, src_color;
201                 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
202                 src_color = ((ARGB*)(src + src_stride * y))[x];
203                 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
204             }
205         }
206
207         return Ok;
208     }
209     else
210     {
211         HDC hdc;
212         HBITMAP hbitmap, old_hbm=NULL;
213         BITMAPINFOHEADER bih;
214         BYTE *temp_bits;
215         BLENDFUNCTION bf;
216
217         hdc = CreateCompatibleDC(0);
218
219         bih.biSize = sizeof(BITMAPINFOHEADER);
220         bih.biWidth = src_width;
221         bih.biHeight = -src_height;
222         bih.biPlanes = 1;
223         bih.biBitCount = 32;
224         bih.biCompression = BI_RGB;
225         bih.biSizeImage = 0;
226         bih.biXPelsPerMeter = 0;
227         bih.biYPelsPerMeter = 0;
228         bih.biClrUsed = 0;
229         bih.biClrImportant = 0;
230
231         hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
232             (void**)&temp_bits, NULL, 0);
233
234         convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
235             4 * src_width, src, src_stride);
236
237         old_hbm = SelectObject(hdc, hbitmap);
238
239         bf.BlendOp = AC_SRC_OVER;
240         bf.BlendFlags = 0;
241         bf.SourceConstantAlpha = 255;
242         bf.AlphaFormat = AC_SRC_ALPHA;
243
244         GdiAlphaBlend(graphics->hdc, dst_x, dst_y, src_width, src_height,
245             hdc, 0, 0, src_width, src_height, bf);
246
247         SelectObject(hdc, old_hbm);
248         DeleteDC(hdc);
249         DeleteObject(hbitmap);
250
251         return Ok;
252     }
253 }
254
255 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
256 {
257     ARGB result=0;
258     ARGB i;
259     INT a1, a2, a3;
260
261     a1 = (start >> 24) & 0xff;
262     a2 = (end >> 24) & 0xff;
263
264     a3 = (int)(a1*(1.0f - position)+a2*(position));
265
266     result |= a3 << 24;
267
268     for (i=0xff; i<=0xff0000; i = i << 8)
269         result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
270     return result;
271 }
272
273 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
274 {
275     REAL blendfac;
276
277     /* clamp to between 0.0 and 1.0, using the wrap mode */
278     if (brush->wrap == WrapModeTile)
279     {
280         position = fmodf(position, 1.0f);
281         if (position < 0.0f) position += 1.0f;
282     }
283     else /* WrapModeFlip* */
284     {
285         position = fmodf(position, 2.0f);
286         if (position < 0.0f) position += 2.0f;
287         if (position > 1.0f) position = 2.0f - position;
288     }
289
290     if (brush->blendcount == 1)
291         blendfac = position;
292     else
293     {
294         int i=1;
295         REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
296         REAL range;
297
298         /* locate the blend positions surrounding this position */
299         while (position > brush->blendpos[i])
300             i++;
301
302         /* interpolate between the blend positions */
303         left_blendpos = brush->blendpos[i-1];
304         left_blendfac = brush->blendfac[i-1];
305         right_blendpos = brush->blendpos[i];
306         right_blendfac = brush->blendfac[i];
307         range = right_blendpos - left_blendpos;
308         blendfac = (left_blendfac * (right_blendpos - position) +
309                     right_blendfac * (position - left_blendpos)) / range;
310     }
311
312     if (brush->pblendcount == 0)
313         return blend_colors(brush->startcolor, brush->endcolor, blendfac);
314     else
315     {
316         int i=1;
317         ARGB left_blendcolor, right_blendcolor;
318         REAL left_blendpos, right_blendpos;
319
320         /* locate the blend colors surrounding this position */
321         while (blendfac > brush->pblendpos[i])
322             i++;
323
324         /* interpolate between the blend colors */
325         left_blendpos = brush->pblendpos[i-1];
326         left_blendcolor = brush->pblendcolor[i-1];
327         right_blendpos = brush->pblendpos[i];
328         right_blendcolor = brush->pblendcolor[i];
329         blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
330         return blend_colors(left_blendcolor, right_blendcolor, blendfac);
331     }
332 }
333
334 static void apply_image_attributes(const GpImageAttributes *attributes, LPBYTE data,
335     UINT width, UINT height, INT stride, ColorAdjustType type)
336 {
337     UINT x, y, i;
338
339     if (attributes->colorkeys[type].enabled ||
340         attributes->colorkeys[ColorAdjustTypeDefault].enabled)
341     {
342         const struct color_key *key;
343         BYTE min_blue, min_green, min_red;
344         BYTE max_blue, max_green, max_red;
345
346         if (attributes->colorkeys[type].enabled)
347             key = &attributes->colorkeys[type];
348         else
349             key = &attributes->colorkeys[ColorAdjustTypeDefault];
350
351         min_blue = key->low&0xff;
352         min_green = (key->low>>8)&0xff;
353         min_red = (key->low>>16)&0xff;
354
355         max_blue = key->high&0xff;
356         max_green = (key->high>>8)&0xff;
357         max_red = (key->high>>16)&0xff;
358
359         for (x=0; x<width; x++)
360             for (y=0; y<height; y++)
361             {
362                 ARGB *src_color;
363                 BYTE blue, green, red;
364                 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
365                 blue = *src_color&0xff;
366                 green = (*src_color>>8)&0xff;
367                 red = (*src_color>>16)&0xff;
368                 if (blue >= min_blue && green >= min_green && red >= min_red &&
369                     blue <= max_blue && green <= max_green && red <= max_red)
370                     *src_color = 0x00000000;
371             }
372     }
373
374     if (attributes->colorremaptables[type].enabled ||
375         attributes->colorremaptables[ColorAdjustTypeDefault].enabled)
376     {
377         const struct color_remap_table *table;
378
379         if (attributes->colorremaptables[type].enabled)
380             table = &attributes->colorremaptables[type];
381         else
382             table = &attributes->colorremaptables[ColorAdjustTypeDefault];
383
384         for (x=0; x<width; x++)
385             for (y=0; y<height; y++)
386             {
387                 ARGB *src_color;
388                 src_color = (ARGB*)(data + stride * y + sizeof(ARGB) * x);
389                 for (i=0; i<table->mapsize; i++)
390                 {
391                     if (*src_color == table->colormap[i].oldColor.Argb)
392                     {
393                         *src_color = table->colormap[i].newColor.Argb;
394                         break;
395                     }
396                 }
397             }
398     }
399
400     if (attributes->colormatrices[type].enabled ||
401         attributes->colormatrices[ColorAdjustTypeDefault].enabled)
402     {
403         static int fixme;
404         if (!fixme++)
405             FIXME("Color transforms not implemented\n");
406     }
407
408     if (attributes->gamma_enabled[type] ||
409         attributes->gamma_enabled[ColorAdjustTypeDefault])
410     {
411         static int fixme;
412         if (!fixme++)
413             FIXME("Gamma adjustment not implemented\n");
414     }
415 }
416
417 /* Given a bitmap and its source rectangle, find the smallest rectangle in the
418  * bitmap that contains all the pixels we may need to draw it. */
419 static void get_bitmap_sample_size(InterpolationMode interpolation, WrapMode wrap,
420     GpBitmap* bitmap, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
421     GpRect *rect)
422 {
423     INT left, top, right, bottom;
424
425     switch (interpolation)
426     {
427     case InterpolationModeHighQualityBilinear:
428     case InterpolationModeHighQualityBicubic:
429     /* FIXME: Include a greater range for the prefilter? */
430     case InterpolationModeBicubic:
431     case InterpolationModeBilinear:
432         left = (INT)(floorf(srcx));
433         top = (INT)(floorf(srcy));
434         right = (INT)(ceilf(srcx+srcwidth));
435         bottom = (INT)(ceilf(srcy+srcheight));
436         break;
437     case InterpolationModeNearestNeighbor:
438     default:
439         left = roundr(srcx);
440         top = roundr(srcy);
441         right = roundr(srcx+srcwidth);
442         bottom = roundr(srcy+srcheight);
443         break;
444     }
445
446     if (wrap == WrapModeClamp)
447     {
448         if (left < 0)
449             left = 0;
450         if (top < 0)
451             top = 0;
452         if (right >= bitmap->width)
453             right = bitmap->width-1;
454         if (bottom >= bitmap->height)
455             bottom = bitmap->height-1;
456     }
457     else
458     {
459         /* In some cases we can make the rectangle smaller here, but the logic
460          * is hard to get right, and tiling suggests we're likely to use the
461          * entire source image. */
462         if (left < 0 || right >= bitmap->width)
463         {
464             left = 0;
465             right = bitmap->width-1;
466         }
467
468         if (top < 0 || bottom >= bitmap->height)
469         {
470             top = 0;
471             bottom = bitmap->height-1;
472         }
473     }
474
475     rect->X = left;
476     rect->Y = top;
477     rect->Width = right - left + 1;
478     rect->Height = bottom - top + 1;
479 }
480
481 static ARGB sample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
482     UINT height, INT x, INT y, GDIPCONST GpImageAttributes *attributes)
483 {
484     if (attributes->wrap == WrapModeClamp)
485     {
486         if (x < 0 || y < 0 || x >= width || y >= height)
487             return attributes->outside_color;
488     }
489     else
490     {
491         /* Tiling. Make sure co-ordinates are positive as it simplifies the math. */
492         if (x < 0)
493             x = width*2 + x % (width * 2);
494         if (y < 0)
495             y = height*2 + y % (height * 2);
496
497         if ((attributes->wrap & 1) == 1)
498         {
499             /* Flip X */
500             if ((x / width) % 2 == 0)
501                 x = x % width;
502             else
503                 x = width - 1 - x % width;
504         }
505         else
506             x = x % width;
507
508         if ((attributes->wrap & 2) == 2)
509         {
510             /* Flip Y */
511             if ((y / height) % 2 == 0)
512                 y = y % height;
513             else
514                 y = height - 1 - y % height;
515         }
516         else
517             y = y % height;
518     }
519
520     if (x < src_rect->X || y < src_rect->Y || x >= src_rect->X + src_rect->Width || y >= src_rect->Y + src_rect->Height)
521     {
522         ERR("out of range pixel requested\n");
523         return 0xffcd0084;
524     }
525
526     return ((DWORD*)(bits))[(x - src_rect->X) + (y - src_rect->Y) * src_rect->Width];
527 }
528
529 static ARGB resample_bitmap_pixel(GDIPCONST GpRect *src_rect, LPBYTE bits, UINT width,
530     UINT height, GpPointF *point, GDIPCONST GpImageAttributes *attributes,
531     InterpolationMode interpolation)
532 {
533     static int fixme;
534
535     switch (interpolation)
536     {
537     default:
538         if (!fixme++)
539             FIXME("Unimplemented interpolation %i\n", interpolation);
540         /* fall-through */
541     case InterpolationModeBilinear:
542     {
543         REAL leftxf, topyf;
544         INT leftx, rightx, topy, bottomy;
545         ARGB topleft, topright, bottomleft, bottomright;
546         ARGB top, bottom;
547         float x_offset;
548
549         leftxf = floorf(point->X);
550         leftx = (INT)leftxf;
551         rightx = (INT)ceilf(point->X);
552         topyf = floorf(point->Y);
553         topy = (INT)topyf;
554         bottomy = (INT)ceilf(point->Y);
555
556         if (leftx == rightx && topy == bottomy)
557             return sample_bitmap_pixel(src_rect, bits, width, height,
558                 leftx, topy, attributes);
559
560         topleft = sample_bitmap_pixel(src_rect, bits, width, height,
561             leftx, topy, attributes);
562         topright = sample_bitmap_pixel(src_rect, bits, width, height,
563             rightx, topy, attributes);
564         bottomleft = sample_bitmap_pixel(src_rect, bits, width, height,
565             leftx, bottomy, attributes);
566         bottomright = sample_bitmap_pixel(src_rect, bits, width, height,
567             rightx, bottomy, attributes);
568
569         x_offset = point->X - leftxf;
570         top = blend_colors(topleft, topright, x_offset);
571         bottom = blend_colors(bottomleft, bottomright, x_offset);
572
573         return blend_colors(top, bottom, point->Y - topyf);
574     }
575     case InterpolationModeNearestNeighbor:
576         return sample_bitmap_pixel(src_rect, bits, width, height,
577             roundr(point->X), roundr(point->Y), attributes);
578     }
579 }
580
581 static INT brush_can_fill_path(GpBrush *brush)
582 {
583     switch (brush->bt)
584     {
585     case BrushTypeSolidColor:
586     case BrushTypeHatchFill:
587         return 1;
588     case BrushTypeLinearGradient:
589     case BrushTypeTextureFill:
590     /* Gdi32 isn't much help with these, so we should use brush_fill_pixels instead. */
591     default:
592         return 0;
593     }
594 }
595
596 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
597 {
598     switch (brush->bt)
599     {
600     case BrushTypeSolidColor:
601     {
602         GpSolidFill *fill = (GpSolidFill*)brush;
603         if (fill->bmp)
604         {
605             RECT rc;
606             /* partially transparent fill */
607
608             SelectClipPath(graphics->hdc, RGN_AND);
609             if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
610             {
611                 HDC hdc = CreateCompatibleDC(NULL);
612                 HBITMAP oldbmp;
613                 BLENDFUNCTION bf;
614
615                 if (!hdc) break;
616
617                 oldbmp = SelectObject(hdc, fill->bmp);
618
619                 bf.BlendOp = AC_SRC_OVER;
620                 bf.BlendFlags = 0;
621                 bf.SourceConstantAlpha = 255;
622                 bf.AlphaFormat = AC_SRC_ALPHA;
623
624                 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
625
626                 SelectObject(hdc, oldbmp);
627                 DeleteDC(hdc);
628             }
629
630             break;
631         }
632         /* else fall through */
633     }
634     default:
635         SelectObject(graphics->hdc, brush->gdibrush);
636         FillPath(graphics->hdc);
637         break;
638     }
639 }
640
641 static INT brush_can_fill_pixels(GpBrush *brush)
642 {
643     switch (brush->bt)
644     {
645     case BrushTypeSolidColor:
646     case BrushTypeHatchFill:
647     case BrushTypeLinearGradient:
648     case BrushTypeTextureFill:
649         return 1;
650     default:
651         return 0;
652     }
653 }
654
655 static GpStatus brush_fill_pixels(GpGraphics *graphics, GpBrush *brush,
656     DWORD *argb_pixels, GpRect *fill_area, UINT cdwStride)
657 {
658     switch (brush->bt)
659     {
660     case BrushTypeSolidColor:
661     {
662         int x, y;
663         GpSolidFill *fill = (GpSolidFill*)brush;
664         for (x=0; x<fill_area->Width; x++)
665             for (y=0; y<fill_area->Height; y++)
666                 argb_pixels[x + y*cdwStride] = fill->color;
667         return Ok;
668     }
669     case BrushTypeHatchFill:
670     {
671         int x, y;
672         GpHatch *fill = (GpHatch*)brush;
673         const char *hatch_data;
674
675         if (get_hatch_data(fill->hatchstyle, &hatch_data) != Ok)
676             return NotImplemented;
677
678         for (x=0; x<fill_area->Width; x++)
679             for (y=0; y<fill_area->Height; y++)
680             {
681                 int hx, hy;
682
683                 /* FIXME: Account for the rendering origin */
684                 hx = (x + fill_area->X) % 8;
685                 hy = (y + fill_area->Y) % 8;
686
687                 if ((hatch_data[7-hy] & (0x80 >> hx)) != 0)
688                     argb_pixels[x + y*cdwStride] = fill->forecol;
689                 else
690                     argb_pixels[x + y*cdwStride] = fill->backcol;
691             }
692
693         return Ok;
694     }
695     case BrushTypeLinearGradient:
696     {
697         GpLineGradient *fill = (GpLineGradient*)brush;
698         GpPointF draw_points[3], line_points[3];
699         GpStatus stat;
700         static const GpRectF box_1 = { 0.0, 0.0, 1.0, 1.0 };
701         GpMatrix *world_to_gradient; /* FIXME: Store this in the brush? */
702         int x, y;
703
704         draw_points[0].X = fill_area->X;
705         draw_points[0].Y = fill_area->Y;
706         draw_points[1].X = fill_area->X+1;
707         draw_points[1].Y = fill_area->Y;
708         draw_points[2].X = fill_area->X;
709         draw_points[2].Y = fill_area->Y+1;
710
711         /* Transform the points to a co-ordinate space where X is the point's
712          * position in the gradient, 0.0 being the start point and 1.0 the
713          * end point. */
714         stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
715             CoordinateSpaceDevice, draw_points, 3);
716
717         if (stat == Ok)
718         {
719             line_points[0] = fill->startpoint;
720             line_points[1] = fill->endpoint;
721             line_points[2].X = fill->startpoint.X + (fill->startpoint.Y - fill->endpoint.Y);
722             line_points[2].Y = fill->startpoint.Y + (fill->endpoint.X - fill->startpoint.X);
723
724             stat = GdipCreateMatrix3(&box_1, line_points, &world_to_gradient);
725         }
726
727         if (stat == Ok)
728         {
729             stat = GdipInvertMatrix(world_to_gradient);
730
731             if (stat == Ok)
732                 stat = GdipTransformMatrixPoints(world_to_gradient, draw_points, 3);
733
734             GdipDeleteMatrix(world_to_gradient);
735         }
736
737         if (stat == Ok)
738         {
739             REAL x_delta = draw_points[1].X - draw_points[0].X;
740             REAL y_delta = draw_points[2].X - draw_points[0].X;
741
742             for (y=0; y<fill_area->Height; y++)
743             {
744                 for (x=0; x<fill_area->Width; x++)
745                 {
746                     REAL pos = draw_points[0].X + x * x_delta + y * y_delta;
747
748                     argb_pixels[x + y*cdwStride] = blend_line_gradient(fill, pos);
749                 }
750             }
751         }
752
753         return stat;
754     }
755     case BrushTypeTextureFill:
756     {
757         GpTexture *fill = (GpTexture*)brush;
758         GpPointF draw_points[3];
759         GpStatus stat;
760         GpMatrix *world_to_texture;
761         int x, y;
762         GpBitmap *bitmap;
763         int src_stride;
764         GpRect src_area;
765
766         if (fill->image->type != ImageTypeBitmap)
767         {
768             FIXME("metafile texture brushes not implemented\n");
769             return NotImplemented;
770         }
771
772         bitmap = (GpBitmap*)fill->image;
773         src_stride = sizeof(ARGB) * bitmap->width;
774
775         src_area.X = src_area.Y = 0;
776         src_area.Width = bitmap->width;
777         src_area.Height = bitmap->height;
778
779         draw_points[0].X = fill_area->X;
780         draw_points[0].Y = fill_area->Y;
781         draw_points[1].X = fill_area->X+1;
782         draw_points[1].Y = fill_area->Y;
783         draw_points[2].X = fill_area->X;
784         draw_points[2].Y = fill_area->Y+1;
785
786         /* Transform the points to the co-ordinate space of the bitmap. */
787         stat = GdipTransformPoints(graphics, CoordinateSpaceWorld,
788             CoordinateSpaceDevice, draw_points, 3);
789
790         if (stat == Ok)
791         {
792             stat = GdipCloneMatrix(fill->transform, &world_to_texture);
793         }
794
795         if (stat == Ok)
796         {
797             stat = GdipInvertMatrix(world_to_texture);
798
799             if (stat == Ok)
800                 stat = GdipTransformMatrixPoints(world_to_texture, draw_points, 3);
801
802             GdipDeleteMatrix(world_to_texture);
803         }
804
805         if (stat == Ok && !fill->bitmap_bits)
806         {
807             BitmapData lockeddata;
808
809             fill->bitmap_bits = GdipAlloc(sizeof(ARGB) * bitmap->width * bitmap->height);
810             if (!fill->bitmap_bits)
811                 stat = OutOfMemory;
812
813             if (stat == Ok)
814             {
815                 lockeddata.Width = bitmap->width;
816                 lockeddata.Height = bitmap->height;
817                 lockeddata.Stride = src_stride;
818                 lockeddata.PixelFormat = PixelFormat32bppARGB;
819                 lockeddata.Scan0 = fill->bitmap_bits;
820
821                 stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
822                     PixelFormat32bppARGB, &lockeddata);
823             }
824
825             if (stat == Ok)
826                 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
827
828             if (stat == Ok)
829                 apply_image_attributes(fill->imageattributes, fill->bitmap_bits,
830                     bitmap->width, bitmap->height,
831                     src_stride, ColorAdjustTypeBitmap);
832
833             if (stat != Ok)
834             {
835                 GdipFree(fill->bitmap_bits);
836                 fill->bitmap_bits = NULL;
837             }
838         }
839
840         if (stat == Ok)
841         {
842             REAL x_dx = draw_points[1].X - draw_points[0].X;
843             REAL x_dy = draw_points[1].Y - draw_points[0].Y;
844             REAL y_dx = draw_points[2].X - draw_points[0].X;
845             REAL y_dy = draw_points[2].Y - draw_points[0].Y;
846
847             for (y=0; y<fill_area->Height; y++)
848             {
849                 for (x=0; x<fill_area->Width; x++)
850                 {
851                     GpPointF point;
852                     point.X = draw_points[0].X + x * x_dx + y * y_dx;
853                     point.Y = draw_points[0].Y + y * x_dy + y * y_dy;
854
855                     argb_pixels[x + y*cdwStride] = resample_bitmap_pixel(
856                         &src_area, fill->bitmap_bits, bitmap->width, bitmap->height,
857                         &point, fill->imageattributes, graphics->interpolation);
858                 }
859             }
860         }
861
862         return stat;
863     }
864     default:
865         return NotImplemented;
866     }
867 }
868
869 /* GdipDrawPie/GdipFillPie helper function */
870 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
871     REAL height, REAL startAngle, REAL sweepAngle)
872 {
873     GpPointF ptf[4];
874     POINT pti[4];
875
876     ptf[0].X = x;
877     ptf[0].Y = y;
878     ptf[1].X = x + width;
879     ptf[1].Y = y + height;
880
881     deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
882     deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
883
884     transform_and_round_points(graphics, pti, ptf, 4);
885
886     Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
887         pti[2].y, pti[3].x, pti[3].y);
888 }
889
890 /* Draws the linecap the specified color and size on the hdc.  The linecap is in
891  * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
892  * should not be called on an hdc that has a path you care about. */
893 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
894     const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
895 {
896     HGDIOBJ oldbrush = NULL, oldpen = NULL;
897     GpMatrix *matrix = NULL;
898     HBRUSH brush = NULL;
899     HPEN pen = NULL;
900     PointF ptf[4], *custptf = NULL;
901     POINT pt[4], *custpt = NULL;
902     BYTE *tp = NULL;
903     REAL theta, dsmall, dbig, dx, dy = 0.0;
904     INT i, count;
905     LOGBRUSH lb;
906     BOOL customstroke;
907
908     if((x1 == x2) && (y1 == y2))
909         return;
910
911     theta = gdiplus_atan2(y2 - y1, x2 - x1);
912
913     customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
914     if(!customstroke){
915         brush = CreateSolidBrush(color);
916         lb.lbStyle = BS_SOLID;
917         lb.lbColor = color;
918         lb.lbHatch = 0;
919         pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
920                            PS_JOIN_MITER, 1, &lb, 0,
921                            NULL);
922         oldbrush = SelectObject(graphics->hdc, brush);
923         oldpen = SelectObject(graphics->hdc, pen);
924     }
925
926     switch(cap){
927         case LineCapFlat:
928             break;
929         case LineCapSquare:
930         case LineCapSquareAnchor:
931         case LineCapDiamondAnchor:
932             size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
933             if(cap == LineCapDiamondAnchor){
934                 dsmall = cos(theta + M_PI_2) * size;
935                 dbig = sin(theta + M_PI_2) * size;
936             }
937             else{
938                 dsmall = cos(theta + M_PI_4) * size;
939                 dbig = sin(theta + M_PI_4) * size;
940             }
941
942             ptf[0].X = x2 - dsmall;
943             ptf[1].X = x2 + dbig;
944
945             ptf[0].Y = y2 - dbig;
946             ptf[3].Y = y2 + dsmall;
947
948             ptf[1].Y = y2 - dsmall;
949             ptf[2].Y = y2 + dbig;
950
951             ptf[3].X = x2 - dbig;
952             ptf[2].X = x2 + dsmall;
953
954             transform_and_round_points(graphics, pt, ptf, 4);
955             Polygon(graphics->hdc, pt, 4);
956
957             break;
958         case LineCapArrowAnchor:
959             size = size * 4.0 / sqrt(3.0);
960
961             dx = cos(M_PI / 6.0 + theta) * size;
962             dy = sin(M_PI / 6.0 + theta) * size;
963
964             ptf[0].X = x2 - dx;
965             ptf[0].Y = y2 - dy;
966
967             dx = cos(- M_PI / 6.0 + theta) * size;
968             dy = sin(- M_PI / 6.0 + theta) * size;
969
970             ptf[1].X = x2 - dx;
971             ptf[1].Y = y2 - dy;
972
973             ptf[2].X = x2;
974             ptf[2].Y = y2;
975
976             transform_and_round_points(graphics, pt, ptf, 3);
977             Polygon(graphics->hdc, pt, 3);
978
979             break;
980         case LineCapRoundAnchor:
981             dx = dy = ANCHOR_WIDTH * size / 2.0;
982
983             ptf[0].X = x2 - dx;
984             ptf[0].Y = y2 - dy;
985             ptf[1].X = x2 + dx;
986             ptf[1].Y = y2 + dy;
987
988             transform_and_round_points(graphics, pt, ptf, 2);
989             Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
990
991             break;
992         case LineCapTriangle:
993             size = size / 2.0;
994             dx = cos(M_PI_2 + theta) * size;
995             dy = sin(M_PI_2 + theta) * size;
996
997             ptf[0].X = x2 - dx;
998             ptf[0].Y = y2 - dy;
999             ptf[1].X = x2 + dx;
1000             ptf[1].Y = y2 + dy;
1001
1002             dx = cos(theta) * size;
1003             dy = sin(theta) * size;
1004
1005             ptf[2].X = x2 + dx;
1006             ptf[2].Y = y2 + dy;
1007
1008             transform_and_round_points(graphics, pt, ptf, 3);
1009             Polygon(graphics->hdc, pt, 3);
1010
1011             break;
1012         case LineCapRound:
1013             dx = dy = size / 2.0;
1014
1015             ptf[0].X = x2 - dx;
1016             ptf[0].Y = y2 - dy;
1017             ptf[1].X = x2 + dx;
1018             ptf[1].Y = y2 + dy;
1019
1020             dx = -cos(M_PI_2 + theta) * size;
1021             dy = -sin(M_PI_2 + theta) * size;
1022
1023             ptf[2].X = x2 - dx;
1024             ptf[2].Y = y2 - dy;
1025             ptf[3].X = x2 + dx;
1026             ptf[3].Y = y2 + dy;
1027
1028             transform_and_round_points(graphics, pt, ptf, 4);
1029             Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
1030                 pt[2].y, pt[3].x, pt[3].y);
1031
1032             break;
1033         case LineCapCustom:
1034             if(!custom)
1035                 break;
1036
1037             count = custom->pathdata.Count;
1038             custptf = GdipAlloc(count * sizeof(PointF));
1039             custpt = GdipAlloc(count * sizeof(POINT));
1040             tp = GdipAlloc(count);
1041
1042             if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
1043                 goto custend;
1044
1045             memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
1046
1047             GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
1048             GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
1049                              MatrixOrderAppend);
1050             GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
1051             GdipTransformMatrixPoints(matrix, custptf, count);
1052
1053             transform_and_round_points(graphics, custpt, custptf, count);
1054
1055             for(i = 0; i < count; i++)
1056                 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
1057
1058             if(custom->fill){
1059                 BeginPath(graphics->hdc);
1060                 PolyDraw(graphics->hdc, custpt, tp, count);
1061                 EndPath(graphics->hdc);
1062                 StrokeAndFillPath(graphics->hdc);
1063             }
1064             else
1065                 PolyDraw(graphics->hdc, custpt, tp, count);
1066
1067 custend:
1068             GdipFree(custptf);
1069             GdipFree(custpt);
1070             GdipFree(tp);
1071             GdipDeleteMatrix(matrix);
1072             break;
1073         default:
1074             break;
1075     }
1076
1077     if(!customstroke){
1078         SelectObject(graphics->hdc, oldbrush);
1079         SelectObject(graphics->hdc, oldpen);
1080         DeleteObject(brush);
1081         DeleteObject(pen);
1082     }
1083 }
1084
1085 /* Shortens the line by the given percent by changing x2, y2.
1086  * If percent is > 1.0 then the line will change direction.
1087  * If percent is negative it can lengthen the line. */
1088 static void shorten_line_percent(REAL x1, REAL  y1, REAL *x2, REAL *y2, REAL percent)
1089 {
1090     REAL dist, theta, dx, dy;
1091
1092     if((y1 == *y2) && (x1 == *x2))
1093         return;
1094
1095     dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
1096     theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
1097     dx = cos(theta) * dist;
1098     dy = sin(theta) * dist;
1099
1100     *x2 = *x2 + dx;
1101     *y2 = *y2 + dy;
1102 }
1103
1104 /* Shortens the line by the given amount by changing x2, y2.
1105  * If the amount is greater than the distance, the line will become length 0.
1106  * If the amount is negative, it can lengthen the line. */
1107 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
1108 {
1109     REAL dx, dy, percent;
1110
1111     dx = *x2 - x1;
1112     dy = *y2 - y1;
1113     if(dx == 0 && dy == 0)
1114         return;
1115
1116     percent = amt / sqrt(dx * dx + dy * dy);
1117     if(percent >= 1.0){
1118         *x2 = x1;
1119         *y2 = y1;
1120         return;
1121     }
1122
1123     shorten_line_percent(x1, y1, x2, y2, percent);
1124 }
1125
1126 /* Draws lines between the given points, and if caps is true then draws an endcap
1127  * at the end of the last line. */
1128 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
1129     GDIPCONST GpPointF * pt, INT count, BOOL caps)
1130 {
1131     POINT *pti = NULL;
1132     GpPointF *ptcopy = NULL;
1133     GpStatus status = GenericError;
1134
1135     if(!count)
1136         return Ok;
1137
1138     pti = GdipAlloc(count * sizeof(POINT));
1139     ptcopy = GdipAlloc(count * sizeof(GpPointF));
1140
1141     if(!pti || !ptcopy){
1142         status = OutOfMemory;
1143         goto end;
1144     }
1145
1146     memcpy(ptcopy, pt, count * sizeof(GpPointF));
1147
1148     if(caps){
1149         if(pen->endcap == LineCapArrowAnchor)
1150             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1151                              &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
1152         else if((pen->endcap == LineCapCustom) && pen->customend)
1153             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
1154                              &ptcopy[count-1].X, &ptcopy[count-1].Y,
1155                              pen->customend->inset * pen->width);
1156
1157         if(pen->startcap == LineCapArrowAnchor)
1158             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1159                              &ptcopy[0].X, &ptcopy[0].Y, pen->width);
1160         else if((pen->startcap == LineCapCustom) && pen->customstart)
1161             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
1162                              &ptcopy[0].X, &ptcopy[0].Y,
1163                              pen->customstart->inset * pen->width);
1164
1165         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1166                  pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
1167         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1168                          pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
1169     }
1170
1171     transform_and_round_points(graphics, pti, ptcopy, count);
1172
1173     if(Polyline(graphics->hdc, pti, count))
1174         status = Ok;
1175
1176 end:
1177     GdipFree(pti);
1178     GdipFree(ptcopy);
1179
1180     return status;
1181 }
1182
1183 /* Conducts a linear search to find the bezier points that will back off
1184  * the endpoint of the curve by a distance of amt. Linear search works
1185  * better than binary in this case because there are multiple solutions,
1186  * and binary searches often find a bad one. I don't think this is what
1187  * Windows does but short of rendering the bezier without GDI's help it's
1188  * the best we can do. If rev then work from the start of the passed points
1189  * instead of the end. */
1190 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
1191 {
1192     GpPointF origpt[4];
1193     REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
1194     INT i, first = 0, second = 1, third = 2, fourth = 3;
1195
1196     if(rev){
1197         first = 3;
1198         second = 2;
1199         third = 1;
1200         fourth = 0;
1201     }
1202
1203     origx = pt[fourth].X;
1204     origy = pt[fourth].Y;
1205     memcpy(origpt, pt, sizeof(GpPointF) * 4);
1206
1207     for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
1208         /* reset bezier points to original values */
1209         memcpy(pt, origpt, sizeof(GpPointF) * 4);
1210         /* Perform magic on bezier points. Order is important here.*/
1211         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1212         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1213         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1214         shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
1215         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
1216         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
1217
1218         dx = pt[fourth].X - origx;
1219         dy = pt[fourth].Y - origy;
1220
1221         diff = sqrt(dx * dx + dy * dy);
1222         percent += 0.0005 * amt;
1223     }
1224 }
1225
1226 /* Draws bezier curves between given points, and if caps is true then draws an
1227  * endcap at the end of the last line. */
1228 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
1229     GDIPCONST GpPointF * pt, INT count, BOOL caps)
1230 {
1231     POINT *pti;
1232     GpPointF *ptcopy;
1233     GpStatus status = GenericError;
1234
1235     if(!count)
1236         return Ok;
1237
1238     pti = GdipAlloc(count * sizeof(POINT));
1239     ptcopy = GdipAlloc(count * sizeof(GpPointF));
1240
1241     if(!pti || !ptcopy){
1242         status = OutOfMemory;
1243         goto end;
1244     }
1245
1246     memcpy(ptcopy, pt, count * sizeof(GpPointF));
1247
1248     if(caps){
1249         if(pen->endcap == LineCapArrowAnchor)
1250             shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
1251         else if((pen->endcap == LineCapCustom) && pen->customend)
1252             shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
1253                                FALSE);
1254
1255         if(pen->startcap == LineCapArrowAnchor)
1256             shorten_bezier_amt(ptcopy, pen->width, TRUE);
1257         else if((pen->startcap == LineCapCustom) && pen->customstart)
1258             shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
1259
1260         /* the direction of the line cap is parallel to the direction at the
1261          * end of the bezier (which, if it has been shortened, is not the same
1262          * as the direction from pt[count-2] to pt[count-1]) */
1263         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1264             pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1265             pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1266             pt[count - 1].X, pt[count - 1].Y);
1267
1268         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1269             pt[0].X - (ptcopy[0].X - ptcopy[1].X),
1270             pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
1271     }
1272
1273     transform_and_round_points(graphics, pti, ptcopy, count);
1274
1275     PolyBezier(graphics->hdc, pti, count);
1276
1277     status = Ok;
1278
1279 end:
1280     GdipFree(pti);
1281     GdipFree(ptcopy);
1282
1283     return status;
1284 }
1285
1286 /* Draws a combination of bezier curves and lines between points. */
1287 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
1288     GDIPCONST BYTE * types, INT count, BOOL caps)
1289 {
1290     POINT *pti = GdipAlloc(count * sizeof(POINT));
1291     BYTE *tp = GdipAlloc(count);
1292     GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
1293     INT i, j;
1294     GpStatus status = GenericError;
1295
1296     if(!count){
1297         status = Ok;
1298         goto end;
1299     }
1300     if(!pti || !tp || !ptcopy){
1301         status = OutOfMemory;
1302         goto end;
1303     }
1304
1305     for(i = 1; i < count; i++){
1306         if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
1307             if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
1308                 || !(types[i + 1] & PathPointTypeBezier)){
1309                 ERR("Bad bezier points\n");
1310                 goto end;
1311             }
1312             i += 2;
1313         }
1314     }
1315
1316     memcpy(ptcopy, pt, count * sizeof(GpPointF));
1317
1318     /* If we are drawing caps, go through the points and adjust them accordingly,
1319      * and draw the caps. */
1320     if(caps){
1321         switch(types[count - 1] & PathPointTypePathTypeMask){
1322             case PathPointTypeBezier:
1323                 if(pen->endcap == LineCapArrowAnchor)
1324                     shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
1325                 else if((pen->endcap == LineCapCustom) && pen->customend)
1326                     shorten_bezier_amt(&ptcopy[count - 4],
1327                                        pen->width * pen->customend->inset, FALSE);
1328
1329                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1330                     pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
1331                     pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
1332                     pt[count - 1].X, pt[count - 1].Y);
1333
1334                 break;
1335             case PathPointTypeLine:
1336                 if(pen->endcap == LineCapArrowAnchor)
1337                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1338                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1339                                      pen->width);
1340                 else if((pen->endcap == LineCapCustom) && pen->customend)
1341                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
1342                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
1343                                      pen->customend->inset * pen->width);
1344
1345                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
1346                          pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
1347                          pt[count - 1].Y);
1348
1349                 break;
1350             default:
1351                 ERR("Bad path last point\n");
1352                 goto end;
1353         }
1354
1355         /* Find start of points */
1356         for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
1357             == PathPointTypeStart); j++);
1358
1359         switch(types[j] & PathPointTypePathTypeMask){
1360             case PathPointTypeBezier:
1361                 if(pen->startcap == LineCapArrowAnchor)
1362                     shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
1363                 else if((pen->startcap == LineCapCustom) && pen->customstart)
1364                     shorten_bezier_amt(&ptcopy[j - 1],
1365                                        pen->width * pen->customstart->inset, TRUE);
1366
1367                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1368                     pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
1369                     pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
1370                     pt[j - 1].X, pt[j - 1].Y);
1371
1372                 break;
1373             case PathPointTypeLine:
1374                 if(pen->startcap == LineCapArrowAnchor)
1375                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1376                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1377                                      pen->width);
1378                 else if((pen->startcap == LineCapCustom) && pen->customstart)
1379                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
1380                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
1381                                      pen->customstart->inset * pen->width);
1382
1383                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1384                          pt[j].X, pt[j].Y, pt[j - 1].X,
1385                          pt[j - 1].Y);
1386
1387                 break;
1388             default:
1389                 ERR("Bad path points\n");
1390                 goto end;
1391         }
1392     }
1393
1394     transform_and_round_points(graphics, pti, ptcopy, count);
1395
1396     for(i = 0; i < count; i++){
1397         tp[i] = convert_path_point_type(types[i]);
1398     }
1399
1400     PolyDraw(graphics->hdc, pti, tp, count);
1401
1402     status = Ok;
1403
1404 end:
1405     GdipFree(pti);
1406     GdipFree(ptcopy);
1407     GdipFree(tp);
1408
1409     return status;
1410 }
1411
1412 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1413 {
1414     GpStatus result;
1415
1416     BeginPath(graphics->hdc);
1417     result = draw_poly(graphics, NULL, path->pathdata.Points,
1418                        path->pathdata.Types, path->pathdata.Count, FALSE);
1419     EndPath(graphics->hdc);
1420     return result;
1421 }
1422
1423 typedef struct _GraphicsContainerItem {
1424     struct list entry;
1425     GraphicsContainer contid;
1426
1427     SmoothingMode smoothing;
1428     CompositingQuality compqual;
1429     InterpolationMode interpolation;
1430     CompositingMode compmode;
1431     TextRenderingHint texthint;
1432     REAL scale;
1433     GpUnit unit;
1434     PixelOffsetMode pixeloffset;
1435     UINT textcontrast;
1436     GpMatrix* worldtrans;
1437     GpRegion* clip;
1438 } GraphicsContainerItem;
1439
1440 static GpStatus init_container(GraphicsContainerItem** container,
1441         GDIPCONST GpGraphics* graphics){
1442     GpStatus sts;
1443
1444     *container = GdipAlloc(sizeof(GraphicsContainerItem));
1445     if(!(*container))
1446         return OutOfMemory;
1447
1448     (*container)->contid = graphics->contid + 1;
1449
1450     (*container)->smoothing = graphics->smoothing;
1451     (*container)->compqual = graphics->compqual;
1452     (*container)->interpolation = graphics->interpolation;
1453     (*container)->compmode = graphics->compmode;
1454     (*container)->texthint = graphics->texthint;
1455     (*container)->scale = graphics->scale;
1456     (*container)->unit = graphics->unit;
1457     (*container)->textcontrast = graphics->textcontrast;
1458     (*container)->pixeloffset = graphics->pixeloffset;
1459
1460     sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1461     if(sts != Ok){
1462         GdipFree(*container);
1463         *container = NULL;
1464         return sts;
1465     }
1466
1467     sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1468     if(sts != Ok){
1469         GdipDeleteMatrix((*container)->worldtrans);
1470         GdipFree(*container);
1471         *container = NULL;
1472         return sts;
1473     }
1474
1475     return Ok;
1476 }
1477
1478 static void delete_container(GraphicsContainerItem* container){
1479     GdipDeleteMatrix(container->worldtrans);
1480     GdipDeleteRegion(container->clip);
1481     GdipFree(container);
1482 }
1483
1484 static GpStatus restore_container(GpGraphics* graphics,
1485         GDIPCONST GraphicsContainerItem* container){
1486     GpStatus sts;
1487     GpMatrix *newTrans;
1488     GpRegion *newClip;
1489
1490     sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1491     if(sts != Ok)
1492         return sts;
1493
1494     sts = GdipCloneRegion(container->clip, &newClip);
1495     if(sts != Ok){
1496         GdipDeleteMatrix(newTrans);
1497         return sts;
1498     }
1499
1500     GdipDeleteMatrix(graphics->worldtrans);
1501     graphics->worldtrans = newTrans;
1502
1503     GdipDeleteRegion(graphics->clip);
1504     graphics->clip = newClip;
1505
1506     graphics->contid = container->contid - 1;
1507
1508     graphics->smoothing = container->smoothing;
1509     graphics->compqual = container->compqual;
1510     graphics->interpolation = container->interpolation;
1511     graphics->compmode = container->compmode;
1512     graphics->texthint = container->texthint;
1513     graphics->scale = container->scale;
1514     graphics->unit = container->unit;
1515     graphics->textcontrast = container->textcontrast;
1516     graphics->pixeloffset = container->pixeloffset;
1517
1518     return Ok;
1519 }
1520
1521 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1522 {
1523     RECT wnd_rect;
1524     GpStatus stat=Ok;
1525     GpUnit unit;
1526
1527     if(graphics->hwnd) {
1528         if(!GetClientRect(graphics->hwnd, &wnd_rect))
1529             return GenericError;
1530
1531         rect->X = wnd_rect.left;
1532         rect->Y = wnd_rect.top;
1533         rect->Width = wnd_rect.right - wnd_rect.left;
1534         rect->Height = wnd_rect.bottom - wnd_rect.top;
1535     }else if (graphics->image){
1536         stat = GdipGetImageBounds(graphics->image, rect, &unit);
1537         if (stat == Ok && unit != UnitPixel)
1538             FIXME("need to convert from unit %i\n", unit);
1539     }else{
1540         rect->X = 0;
1541         rect->Y = 0;
1542         rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1543         rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1544     }
1545
1546     return stat;
1547 }
1548
1549 /* on success, rgn will contain the region of the graphics object which
1550  * is visible after clipping has been applied */
1551 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
1552 {
1553     GpStatus stat;
1554     GpRectF rectf;
1555     GpRegion* tmp;
1556
1557     if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1558         return stat;
1559
1560     if((stat = GdipCreateRegion(&tmp)) != Ok)
1561         return stat;
1562
1563     if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1564         goto end;
1565
1566     if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1567         goto end;
1568
1569     stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1570
1571 end:
1572     GdipDeleteRegion(tmp);
1573     return stat;
1574 }
1575
1576 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1577 {
1578     TRACE("(%p, %p)\n", hdc, graphics);
1579
1580     return GdipCreateFromHDC2(hdc, NULL, graphics);
1581 }
1582
1583 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1584 {
1585     GpStatus retval;
1586
1587     TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1588
1589     if(hDevice != NULL) {
1590         FIXME("Don't know how to handle parameter hDevice\n");
1591         return NotImplemented;
1592     }
1593
1594     if(hdc == NULL)
1595         return OutOfMemory;
1596
1597     if(graphics == NULL)
1598         return InvalidParameter;
1599
1600     *graphics = GdipAlloc(sizeof(GpGraphics));
1601     if(!*graphics)  return OutOfMemory;
1602
1603     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1604         GdipFree(*graphics);
1605         return retval;
1606     }
1607
1608     if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1609         GdipFree((*graphics)->worldtrans);
1610         GdipFree(*graphics);
1611         return retval;
1612     }
1613
1614     (*graphics)->hdc = hdc;
1615     (*graphics)->hwnd = WindowFromDC(hdc);
1616     (*graphics)->owndc = FALSE;
1617     (*graphics)->smoothing = SmoothingModeDefault;
1618     (*graphics)->compqual = CompositingQualityDefault;
1619     (*graphics)->interpolation = InterpolationModeBilinear;
1620     (*graphics)->pixeloffset = PixelOffsetModeDefault;
1621     (*graphics)->compmode = CompositingModeSourceOver;
1622     (*graphics)->unit = UnitDisplay;
1623     (*graphics)->scale = 1.0;
1624     (*graphics)->busy = FALSE;
1625     (*graphics)->textcontrast = 4;
1626     list_init(&(*graphics)->containers);
1627     (*graphics)->contid = 0;
1628
1629     TRACE("<-- %p\n", *graphics);
1630
1631     return Ok;
1632 }
1633
1634 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
1635 {
1636     GpStatus retval;
1637
1638     *graphics = GdipAlloc(sizeof(GpGraphics));
1639     if(!*graphics)  return OutOfMemory;
1640
1641     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1642         GdipFree(*graphics);
1643         return retval;
1644     }
1645
1646     if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1647         GdipFree((*graphics)->worldtrans);
1648         GdipFree(*graphics);
1649         return retval;
1650     }
1651
1652     (*graphics)->hdc = NULL;
1653     (*graphics)->hwnd = NULL;
1654     (*graphics)->owndc = FALSE;
1655     (*graphics)->image = image;
1656     (*graphics)->smoothing = SmoothingModeDefault;
1657     (*graphics)->compqual = CompositingQualityDefault;
1658     (*graphics)->interpolation = InterpolationModeBilinear;
1659     (*graphics)->pixeloffset = PixelOffsetModeDefault;
1660     (*graphics)->compmode = CompositingModeSourceOver;
1661     (*graphics)->unit = UnitDisplay;
1662     (*graphics)->scale = 1.0;
1663     (*graphics)->busy = FALSE;
1664     (*graphics)->textcontrast = 4;
1665     list_init(&(*graphics)->containers);
1666     (*graphics)->contid = 0;
1667
1668     TRACE("<-- %p\n", *graphics);
1669
1670     return Ok;
1671 }
1672
1673 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1674 {
1675     GpStatus ret;
1676     HDC hdc;
1677
1678     TRACE("(%p, %p)\n", hwnd, graphics);
1679
1680     hdc = GetDC(hwnd);
1681
1682     if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1683     {
1684         ReleaseDC(hwnd, hdc);
1685         return ret;
1686     }
1687
1688     (*graphics)->hwnd = hwnd;
1689     (*graphics)->owndc = TRUE;
1690
1691     return Ok;
1692 }
1693
1694 /* FIXME: no icm handling */
1695 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1696 {
1697     TRACE("(%p, %p)\n", hwnd, graphics);
1698
1699     return GdipCreateFromHWND(hwnd, graphics);
1700 }
1701
1702 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1703     GpMetafile **metafile)
1704 {
1705     static int calls;
1706
1707     TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
1708
1709     if(!hemf || !metafile)
1710         return InvalidParameter;
1711
1712     if(!(calls++))
1713         FIXME("not implemented\n");
1714
1715     return NotImplemented;
1716 }
1717
1718 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1719     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1720 {
1721     IStream *stream = NULL;
1722     UINT read;
1723     BYTE* copy;
1724     HENHMETAFILE hemf;
1725     GpStatus retval = Ok;
1726
1727     TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1728
1729     if(!hwmf || !metafile || !placeable)
1730         return InvalidParameter;
1731
1732     *metafile = NULL;
1733     read = GetMetaFileBitsEx(hwmf, 0, NULL);
1734     if(!read)
1735         return GenericError;
1736     copy = GdipAlloc(read);
1737     GetMetaFileBitsEx(hwmf, read, copy);
1738
1739     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1740     GdipFree(copy);
1741
1742     read = GetEnhMetaFileBits(hemf, 0, NULL);
1743     copy = GdipAlloc(read);
1744     GetEnhMetaFileBits(hemf, read, copy);
1745     DeleteEnhMetaFile(hemf);
1746
1747     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1748         ERR("could not make stream\n");
1749         GdipFree(copy);
1750         retval = GenericError;
1751         goto err;
1752     }
1753
1754     *metafile = GdipAlloc(sizeof(GpMetafile));
1755     if(!*metafile){
1756         retval = OutOfMemory;
1757         goto err;
1758     }
1759
1760     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1761         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1762     {
1763         retval = GenericError;
1764         goto err;
1765     }
1766
1767
1768     (*metafile)->image.type = ImageTypeMetafile;
1769     memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
1770     (*metafile)->image.palette_flags = 0;
1771     (*metafile)->image.palette_count = 0;
1772     (*metafile)->image.palette_size = 0;
1773     (*metafile)->image.palette_entries = NULL;
1774     (*metafile)->image.xres = (REAL)placeable->Inch;
1775     (*metafile)->image.yres = (REAL)placeable->Inch;
1776     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1777     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Top) / ((REAL) placeable->Inch);
1778     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1779                     - placeable->BoundingBox.Left));
1780     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1781                    - placeable->BoundingBox.Top));
1782     (*metafile)->unit = UnitPixel;
1783
1784     if(delete)
1785         DeleteMetaFile(hwmf);
1786
1787     TRACE("<-- %p\n", *metafile);
1788
1789 err:
1790     if (retval != Ok)
1791         GdipFree(*metafile);
1792     IStream_Release(stream);
1793     return retval;
1794 }
1795
1796 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1797     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1798 {
1799     HMETAFILE hmf = GetMetaFileW(file);
1800
1801     TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1802
1803     if(!hmf) return InvalidParameter;
1804
1805     return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1806 }
1807
1808 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1809     GpMetafile **metafile)
1810 {
1811     FIXME("(%p, %p): stub\n", file, metafile);
1812     return NotImplemented;
1813 }
1814
1815 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1816     GpMetafile **metafile)
1817 {
1818     FIXME("(%p, %p): stub\n", stream, metafile);
1819     return NotImplemented;
1820 }
1821
1822 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1823     UINT access, IStream **stream)
1824 {
1825     DWORD dwMode;
1826     HRESULT ret;
1827
1828     TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1829
1830     if(!stream || !filename)
1831         return InvalidParameter;
1832
1833     if(access & GENERIC_WRITE)
1834         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1835     else if(access & GENERIC_READ)
1836         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1837     else
1838         return InvalidParameter;
1839
1840     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1841
1842     return hresult_to_status(ret);
1843 }
1844
1845 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1846 {
1847     GraphicsContainerItem *cont, *next;
1848     TRACE("(%p)\n", graphics);
1849
1850     if(!graphics) return InvalidParameter;
1851     if(graphics->busy) return ObjectBusy;
1852
1853     if(graphics->owndc)
1854         ReleaseDC(graphics->hwnd, graphics->hdc);
1855
1856     LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1857         list_remove(&cont->entry);
1858         delete_container(cont);
1859     }
1860
1861     GdipDeleteRegion(graphics->clip);
1862     GdipDeleteMatrix(graphics->worldtrans);
1863     GdipFree(graphics);
1864
1865     return Ok;
1866 }
1867
1868 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1869     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1870 {
1871     INT save_state, num_pts;
1872     GpPointF points[MAX_ARC_PTS];
1873     GpStatus retval;
1874
1875     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1876           width, height, startAngle, sweepAngle);
1877
1878     if(!graphics || !pen || width <= 0 || height <= 0)
1879         return InvalidParameter;
1880
1881     if(graphics->busy)
1882         return ObjectBusy;
1883
1884     if (!graphics->hdc)
1885     {
1886         FIXME("graphics object has no HDC\n");
1887         return Ok;
1888     }
1889
1890     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1891
1892     save_state = prepare_dc(graphics, pen);
1893
1894     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1895
1896     restore_dc(graphics, save_state);
1897
1898     return retval;
1899 }
1900
1901 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1902     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1903 {
1904     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1905           width, height, startAngle, sweepAngle);
1906
1907     return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1908 }
1909
1910 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1911     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1912 {
1913     INT save_state;
1914     GpPointF pt[4];
1915     GpStatus retval;
1916
1917     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1918           x2, y2, x3, y3, x4, y4);
1919
1920     if(!graphics || !pen)
1921         return InvalidParameter;
1922
1923     if(graphics->busy)
1924         return ObjectBusy;
1925
1926     if (!graphics->hdc)
1927     {
1928         FIXME("graphics object has no HDC\n");
1929         return Ok;
1930     }
1931
1932     pt[0].X = x1;
1933     pt[0].Y = y1;
1934     pt[1].X = x2;
1935     pt[1].Y = y2;
1936     pt[2].X = x3;
1937     pt[2].Y = y3;
1938     pt[3].X = x4;
1939     pt[3].Y = y4;
1940
1941     save_state = prepare_dc(graphics, pen);
1942
1943     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1944
1945     restore_dc(graphics, save_state);
1946
1947     return retval;
1948 }
1949
1950 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1951     INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1952 {
1953     INT save_state;
1954     GpPointF pt[4];
1955     GpStatus retval;
1956
1957     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1958           x2, y2, x3, y3, x4, y4);
1959
1960     if(!graphics || !pen)
1961         return InvalidParameter;
1962
1963     if(graphics->busy)
1964         return ObjectBusy;
1965
1966     if (!graphics->hdc)
1967     {
1968         FIXME("graphics object has no HDC\n");
1969         return Ok;
1970     }
1971
1972     pt[0].X = x1;
1973     pt[0].Y = y1;
1974     pt[1].X = x2;
1975     pt[1].Y = y2;
1976     pt[2].X = x3;
1977     pt[2].Y = y3;
1978     pt[3].X = x4;
1979     pt[3].Y = y4;
1980
1981     save_state = prepare_dc(graphics, pen);
1982
1983     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1984
1985     restore_dc(graphics, save_state);
1986
1987     return retval;
1988 }
1989
1990 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1991     GDIPCONST GpPointF *points, INT count)
1992 {
1993     INT i;
1994     GpStatus ret;
1995
1996     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1997
1998     if(!graphics || !pen || !points || (count <= 0))
1999         return InvalidParameter;
2000
2001     if(graphics->busy)
2002         return ObjectBusy;
2003
2004     for(i = 0; i < floor(count / 4); i++){
2005         ret = GdipDrawBezier(graphics, pen,
2006                              points[4*i].X, points[4*i].Y,
2007                              points[4*i + 1].X, points[4*i + 1].Y,
2008                              points[4*i + 2].X, points[4*i + 2].Y,
2009                              points[4*i + 3].X, points[4*i + 3].Y);
2010         if(ret != Ok)
2011             return ret;
2012     }
2013
2014     return Ok;
2015 }
2016
2017 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
2018     GDIPCONST GpPoint *points, INT count)
2019 {
2020     GpPointF *pts;
2021     GpStatus ret;
2022     INT i;
2023
2024     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2025
2026     if(!graphics || !pen || !points || (count <= 0))
2027         return InvalidParameter;
2028
2029     if(graphics->busy)
2030         return ObjectBusy;
2031
2032     pts = GdipAlloc(sizeof(GpPointF) * count);
2033     if(!pts)
2034         return OutOfMemory;
2035
2036     for(i = 0; i < count; i++){
2037         pts[i].X = (REAL)points[i].X;
2038         pts[i].Y = (REAL)points[i].Y;
2039     }
2040
2041     ret = GdipDrawBeziers(graphics,pen,pts,count);
2042
2043     GdipFree(pts);
2044
2045     return ret;
2046 }
2047
2048 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
2049     GDIPCONST GpPointF *points, INT count)
2050 {
2051     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2052
2053     return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
2054 }
2055
2056 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
2057     GDIPCONST GpPoint *points, INT count)
2058 {
2059     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2060
2061     return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
2062 }
2063
2064 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
2065     GDIPCONST GpPointF *points, INT count, REAL tension)
2066 {
2067     GpPath *path;
2068     GpStatus stat;
2069
2070     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2071
2072     if(!graphics || !pen || !points || count <= 0)
2073         return InvalidParameter;
2074
2075     if(graphics->busy)
2076         return ObjectBusy;
2077
2078     if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
2079         return stat;
2080
2081     stat = GdipAddPathClosedCurve2(path, points, count, tension);
2082     if(stat != Ok){
2083         GdipDeletePath(path);
2084         return stat;
2085     }
2086
2087     stat = GdipDrawPath(graphics, pen, path);
2088
2089     GdipDeletePath(path);
2090
2091     return stat;
2092 }
2093
2094 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
2095     GDIPCONST GpPoint *points, INT count, REAL tension)
2096 {
2097     GpPointF *ptf;
2098     GpStatus stat;
2099     INT i;
2100
2101     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2102
2103     if(!points || count <= 0)
2104         return InvalidParameter;
2105
2106     ptf = GdipAlloc(sizeof(GpPointF)*count);
2107     if(!ptf)
2108         return OutOfMemory;
2109
2110     for(i = 0; i < count; i++){
2111         ptf[i].X = (REAL)points[i].X;
2112         ptf[i].Y = (REAL)points[i].Y;
2113     }
2114
2115     stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
2116
2117     GdipFree(ptf);
2118
2119     return stat;
2120 }
2121
2122 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
2123     GDIPCONST GpPointF *points, INT count)
2124 {
2125     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2126
2127     return GdipDrawCurve2(graphics,pen,points,count,1.0);
2128 }
2129
2130 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
2131     GDIPCONST GpPoint *points, INT count)
2132 {
2133     GpPointF *pointsF;
2134     GpStatus ret;
2135     INT i;
2136
2137     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2138
2139     if(!points)
2140         return InvalidParameter;
2141
2142     pointsF = GdipAlloc(sizeof(GpPointF)*count);
2143     if(!pointsF)
2144         return OutOfMemory;
2145
2146     for(i = 0; i < count; i++){
2147         pointsF[i].X = (REAL)points[i].X;
2148         pointsF[i].Y = (REAL)points[i].Y;
2149     }
2150
2151     ret = GdipDrawCurve(graphics,pen,pointsF,count);
2152     GdipFree(pointsF);
2153
2154     return ret;
2155 }
2156
2157 /* Approximates cardinal spline with Bezier curves. */
2158 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
2159     GDIPCONST GpPointF *points, INT count, REAL tension)
2160 {
2161     /* PolyBezier expects count*3-2 points. */
2162     INT i, len_pt = count*3-2, save_state;
2163     GpPointF *pt;
2164     REAL x1, x2, y1, y2;
2165     GpStatus retval;
2166
2167     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2168
2169     if(!graphics || !pen)
2170         return InvalidParameter;
2171
2172     if(graphics->busy)
2173         return ObjectBusy;
2174
2175     if(count < 2)
2176         return InvalidParameter;
2177
2178     if (!graphics->hdc)
2179     {
2180         FIXME("graphics object has no HDC\n");
2181         return Ok;
2182     }
2183
2184     pt = GdipAlloc(len_pt * sizeof(GpPointF));
2185     if(!pt)
2186         return OutOfMemory;
2187
2188     tension = tension * TENSION_CONST;
2189
2190     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
2191         tension, &x1, &y1);
2192
2193     pt[0].X = points[0].X;
2194     pt[0].Y = points[0].Y;
2195     pt[1].X = x1;
2196     pt[1].Y = y1;
2197
2198     for(i = 0; i < count-2; i++){
2199         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
2200
2201         pt[3*i+2].X = x1;
2202         pt[3*i+2].Y = y1;
2203         pt[3*i+3].X = points[i+1].X;
2204         pt[3*i+3].Y = points[i+1].Y;
2205         pt[3*i+4].X = x2;
2206         pt[3*i+4].Y = y2;
2207     }
2208
2209     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
2210         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
2211
2212     pt[len_pt-2].X = x1;
2213     pt[len_pt-2].Y = y1;
2214     pt[len_pt-1].X = points[count-1].X;
2215     pt[len_pt-1].Y = points[count-1].Y;
2216
2217     save_state = prepare_dc(graphics, pen);
2218
2219     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
2220
2221     GdipFree(pt);
2222     restore_dc(graphics, save_state);
2223
2224     return retval;
2225 }
2226
2227 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
2228     GDIPCONST GpPoint *points, INT count, REAL tension)
2229 {
2230     GpPointF *pointsF;
2231     GpStatus ret;
2232     INT i;
2233
2234     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
2235
2236     if(!points)
2237         return InvalidParameter;
2238
2239     pointsF = GdipAlloc(sizeof(GpPointF)*count);
2240     if(!pointsF)
2241         return OutOfMemory;
2242
2243     for(i = 0; i < count; i++){
2244         pointsF[i].X = (REAL)points[i].X;
2245         pointsF[i].Y = (REAL)points[i].Y;
2246     }
2247
2248     ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
2249     GdipFree(pointsF);
2250
2251     return ret;
2252 }
2253
2254 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
2255     GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
2256     REAL tension)
2257 {
2258     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2259
2260     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2261         return InvalidParameter;
2262     }
2263
2264     return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
2265 }
2266
2267 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
2268     GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
2269     REAL tension)
2270 {
2271     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
2272
2273     if(count < 0){
2274         return OutOfMemory;
2275     }
2276
2277     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
2278         return InvalidParameter;
2279     }
2280
2281     return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
2282 }
2283
2284 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
2285     REAL y, REAL width, REAL height)
2286 {
2287     INT save_state;
2288     GpPointF ptf[2];
2289     POINT pti[2];
2290
2291     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2292
2293     if(!graphics || !pen)
2294         return InvalidParameter;
2295
2296     if(graphics->busy)
2297         return ObjectBusy;
2298
2299     if (!graphics->hdc)
2300     {
2301         FIXME("graphics object has no HDC\n");
2302         return Ok;
2303     }
2304
2305     ptf[0].X = x;
2306     ptf[0].Y = y;
2307     ptf[1].X = x + width;
2308     ptf[1].Y = y + height;
2309
2310     save_state = prepare_dc(graphics, pen);
2311     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2312
2313     transform_and_round_points(graphics, pti, ptf, 2);
2314
2315     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2316
2317     restore_dc(graphics, save_state);
2318
2319     return Ok;
2320 }
2321
2322 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
2323     INT y, INT width, INT height)
2324 {
2325     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2326
2327     return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2328 }
2329
2330
2331 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
2332 {
2333     UINT width, height;
2334     GpPointF points[3];
2335
2336     TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
2337
2338     if(!graphics || !image)
2339         return InvalidParameter;
2340
2341     GdipGetImageWidth(image, &width);
2342     GdipGetImageHeight(image, &height);
2343
2344     /* FIXME: we should use the graphics and image dpi, somehow */
2345
2346     points[0].X = points[2].X = x;
2347     points[0].Y = points[1].Y = y;
2348     points[1].X = x + width;
2349     points[2].Y = y + height;
2350
2351     return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
2352         UnitPixel, NULL, NULL, NULL);
2353 }
2354
2355 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
2356     INT y)
2357 {
2358     TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
2359
2360     return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
2361 }
2362
2363 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
2364     REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
2365     GpUnit srcUnit)
2366 {
2367     GpPointF points[3];
2368     TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2369
2370     points[0].X = points[2].X = x;
2371     points[0].Y = points[1].Y = y;
2372
2373     /* FIXME: convert image coordinates to Graphics coordinates? */
2374     points[1].X = x + srcwidth;
2375     points[2].Y = y + srcheight;
2376
2377     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2378         srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
2379 }
2380
2381 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2382     INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2383     GpUnit srcUnit)
2384 {
2385     return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2386 }
2387
2388 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2389     GDIPCONST GpPointF *dstpoints, INT count)
2390 {
2391     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2392     return NotImplemented;
2393 }
2394
2395 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2396     GDIPCONST GpPoint *dstpoints, INT count)
2397 {
2398     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2399     return NotImplemented;
2400 }
2401
2402 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2403      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2404      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2405      DrawImageAbort callback, VOID * callbackData)
2406 {
2407     GpPointF ptf[4];
2408     POINT pti[4];
2409     REAL dx, dy;
2410     GpStatus stat;
2411
2412     TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2413           count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2414           callbackData);
2415
2416     if (count > 3)
2417         return NotImplemented;
2418
2419     if(!graphics || !image || !points || count != 3)
2420          return InvalidParameter;
2421
2422     TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2423         debugstr_pointf(&points[2]));
2424
2425     memcpy(ptf, points, 3 * sizeof(GpPointF));
2426     ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2427     ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2428     if (!srcwidth || !srcheight || ptf[3].X == ptf[0].X || ptf[3].Y == ptf[0].Y)
2429         return Ok;
2430     transform_and_round_points(graphics, pti, ptf, 4);
2431
2432     if (image->picture)
2433     {
2434         if (!graphics->hdc)
2435         {
2436             FIXME("graphics object has no HDC\n");
2437         }
2438
2439         /* FIXME: partially implemented (only works for rectangular parallelograms) */
2440         if(srcUnit == UnitInch)
2441             dx = dy = (REAL) INCH_HIMETRIC;
2442         else if(srcUnit == UnitPixel){
2443             dx = ((REAL) INCH_HIMETRIC) /
2444                  ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
2445             dy = ((REAL) INCH_HIMETRIC) /
2446                  ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
2447         }
2448         else
2449             return NotImplemented;
2450
2451         if(IPicture_Render(image->picture, graphics->hdc,
2452             pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2453             srcx * dx, srcy * dy,
2454             srcwidth * dx, srcheight * dy,
2455             NULL) != S_OK){
2456             if(callback)
2457                 callback(callbackData);
2458             return GenericError;
2459         }
2460     }
2461     else if (image->type == ImageTypeBitmap)
2462     {
2463         GpBitmap* bitmap = (GpBitmap*)image;
2464         int use_software=0;
2465
2466         if (srcUnit == UnitInch)
2467             dx = dy = 96.0; /* FIXME: use the image resolution */
2468         else if (srcUnit == UnitPixel)
2469             dx = dy = 1.0;
2470         else
2471             return NotImplemented;
2472
2473         srcx = srcx * dx;
2474         srcy = srcy * dy;
2475         srcwidth = srcwidth * dx;
2476         srcheight = srcheight * dy;
2477
2478         if (imageAttributes ||
2479             (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2480             !((GpBitmap*)image)->hbitmap ||
2481             ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X ||
2482             ptf[1].X - ptf[0].X != srcwidth || ptf[2].Y - ptf[0].Y != srcheight ||
2483             srcx < 0 || srcy < 0 ||
2484             srcx + srcwidth > bitmap->width || srcy + srcheight > bitmap->height)
2485             use_software = 1;
2486
2487         if (use_software)
2488         {
2489             RECT dst_area;
2490             GpRect src_area;
2491             int i, x, y, src_stride, dst_stride;
2492             GpMatrix *dst_to_src;
2493             REAL m11, m12, m21, m22, mdx, mdy;
2494             LPBYTE src_data, dst_data;
2495             BitmapData lockeddata;
2496             InterpolationMode interpolation = graphics->interpolation;
2497             GpPointF dst_to_src_points[3] = {{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}};
2498             REAL x_dx, x_dy, y_dx, y_dy;
2499             static const GpImageAttributes defaultImageAttributes = {WrapModeClamp, 0, FALSE};
2500
2501             if (!imageAttributes)
2502                 imageAttributes = &defaultImageAttributes;
2503
2504             dst_area.left = dst_area.right = pti[0].x;
2505             dst_area.top = dst_area.bottom = pti[0].y;
2506             for (i=1; i<4; i++)
2507             {
2508                 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2509                 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2510                 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2511                 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2512             }
2513
2514             m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2515             m21 = (ptf[2].X - ptf[0].X) / srcheight;
2516             mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2517             m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2518             m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2519             mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2520
2521             stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
2522             if (stat != Ok) return stat;
2523
2524             stat = GdipInvertMatrix(dst_to_src);
2525             if (stat != Ok)
2526             {
2527                 GdipDeleteMatrix(dst_to_src);
2528                 return stat;
2529             }
2530
2531             dst_data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2532             if (!dst_data)
2533             {
2534                 GdipDeleteMatrix(dst_to_src);
2535                 return OutOfMemory;
2536             }
2537
2538             dst_stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
2539
2540             get_bitmap_sample_size(interpolation, imageAttributes->wrap,
2541                 bitmap, srcx, srcy, srcwidth, srcheight, &src_area);
2542
2543             src_data = GdipAlloc(sizeof(ARGB) * src_area.Width * src_area.Height);
2544             if (!src_data)
2545             {
2546                 GdipFree(dst_data);
2547                 GdipDeleteMatrix(dst_to_src);
2548                 return OutOfMemory;
2549             }
2550             src_stride = sizeof(ARGB) * src_area.Width;
2551
2552             /* Read the bits we need from the source bitmap into an ARGB buffer. */
2553             lockeddata.Width = src_area.Width;
2554             lockeddata.Height = src_area.Height;
2555             lockeddata.Stride = src_stride;
2556             lockeddata.PixelFormat = PixelFormat32bppARGB;
2557             lockeddata.Scan0 = src_data;
2558
2559             stat = GdipBitmapLockBits(bitmap, &src_area, ImageLockModeRead|ImageLockModeUserInputBuf,
2560                 PixelFormat32bppARGB, &lockeddata);
2561
2562             if (stat == Ok)
2563                 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
2564
2565             if (stat != Ok)
2566             {
2567                 if (src_data != dst_data)
2568                     GdipFree(src_data);
2569                 GdipFree(dst_data);
2570                 GdipDeleteMatrix(dst_to_src);
2571                 return OutOfMemory;
2572             }
2573
2574             apply_image_attributes(imageAttributes, src_data,
2575                 src_area.Width, src_area.Height,
2576                 src_stride, ColorAdjustTypeBitmap);
2577
2578             /* Transform the bits as needed to the destination. */
2579             GdipTransformMatrixPoints(dst_to_src, dst_to_src_points, 3);
2580
2581             x_dx = dst_to_src_points[1].X - dst_to_src_points[0].X;
2582             x_dy = dst_to_src_points[1].Y - dst_to_src_points[0].Y;
2583             y_dx = dst_to_src_points[2].X - dst_to_src_points[0].X;
2584             y_dy = dst_to_src_points[2].Y - dst_to_src_points[0].Y;
2585
2586             for (x=dst_area.left; x<dst_area.right; x++)
2587             {
2588                 for (y=dst_area.top; y<dst_area.bottom; y++)
2589                 {
2590                     GpPointF src_pointf;
2591                     ARGB *dst_color;
2592
2593                     src_pointf.X = dst_to_src_points[0].X + x * x_dx + y * y_dx;
2594                     src_pointf.Y = dst_to_src_points[0].Y + x * x_dy + y * y_dy;
2595
2596                     dst_color = (ARGB*)(dst_data + dst_stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2597
2598                     if (src_pointf.X >= srcx && src_pointf.X < srcx + srcwidth && src_pointf.Y >= srcy && src_pointf.Y < srcy+srcheight)
2599                         *dst_color = resample_bitmap_pixel(&src_area, src_data, bitmap->width, bitmap->height, &src_pointf, imageAttributes, interpolation);
2600                     else
2601                         *dst_color = 0;
2602                 }
2603             }
2604
2605             GdipDeleteMatrix(dst_to_src);
2606
2607             GdipFree(src_data);
2608
2609             stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
2610                 dst_data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, dst_stride);
2611
2612             GdipFree(dst_data);
2613
2614             return stat;
2615         }
2616         else
2617         {
2618             HDC hdc;
2619             int temp_hdc=0, temp_bitmap=0;
2620             HBITMAP hbitmap, old_hbm=NULL;
2621
2622             if (!(bitmap->format == PixelFormat16bppRGB555 ||
2623                   bitmap->format == PixelFormat24bppRGB ||
2624                   bitmap->format == PixelFormat32bppRGB ||
2625                   bitmap->format == PixelFormat32bppPARGB))
2626             {
2627                 BITMAPINFOHEADER bih;
2628                 BYTE *temp_bits;
2629                 PixelFormat dst_format;
2630
2631                 /* we can't draw a bitmap of this format directly */
2632                 hdc = CreateCompatibleDC(0);
2633                 temp_hdc = 1;
2634                 temp_bitmap = 1;
2635
2636                 bih.biSize = sizeof(BITMAPINFOHEADER);
2637                 bih.biWidth = bitmap->width;
2638                 bih.biHeight = -bitmap->height;
2639                 bih.biPlanes = 1;
2640                 bih.biBitCount = 32;
2641                 bih.biCompression = BI_RGB;
2642                 bih.biSizeImage = 0;
2643                 bih.biXPelsPerMeter = 0;
2644                 bih.biYPelsPerMeter = 0;
2645                 bih.biClrUsed = 0;
2646                 bih.biClrImportant = 0;
2647
2648                 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
2649                     (void**)&temp_bits, NULL, 0);
2650
2651                 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2652                     dst_format = PixelFormat32bppPARGB;
2653                 else
2654                     dst_format = PixelFormat32bppRGB;
2655
2656                 convert_pixels(bitmap->width, bitmap->height,
2657                     bitmap->width*4, temp_bits, dst_format,
2658                     bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
2659             }
2660             else
2661             {
2662                 hbitmap = bitmap->hbitmap;
2663                 hdc = bitmap->hdc;
2664                 temp_hdc = (hdc == 0);
2665             }
2666
2667             if (temp_hdc)
2668             {
2669                 if (!hdc) hdc = CreateCompatibleDC(0);
2670                 old_hbm = SelectObject(hdc, hbitmap);
2671             }
2672
2673             if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2674             {
2675                 BLENDFUNCTION bf;
2676
2677                 bf.BlendOp = AC_SRC_OVER;
2678                 bf.BlendFlags = 0;
2679                 bf.SourceConstantAlpha = 255;
2680                 bf.AlphaFormat = AC_SRC_ALPHA;
2681
2682                 GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2683                     hdc, srcx, srcy, srcwidth, srcheight, bf);
2684             }
2685             else
2686             {
2687                 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2688                     hdc, srcx, srcy, srcwidth, srcheight, SRCCOPY);
2689             }
2690
2691             if (temp_hdc)
2692             {
2693                 SelectObject(hdc, old_hbm);
2694                 DeleteDC(hdc);
2695             }
2696
2697             if (temp_bitmap)
2698                 DeleteObject(hbitmap);
2699         }
2700     }
2701     else
2702     {
2703         ERR("GpImage with no IPicture or HBITMAP?!\n");
2704         return NotImplemented;
2705     }
2706
2707     return Ok;
2708 }
2709
2710 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
2711      GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
2712      INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2713      DrawImageAbort callback, VOID * callbackData)
2714 {
2715     GpPointF pointsF[3];
2716     INT i;
2717
2718     TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
2719           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2720           callbackData);
2721
2722     if(!points || count!=3)
2723         return InvalidParameter;
2724
2725     for(i = 0; i < count; i++){
2726         pointsF[i].X = (REAL)points[i].X;
2727         pointsF[i].Y = (REAL)points[i].Y;
2728     }
2729
2730     return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
2731                                    (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
2732                                    callback, callbackData);
2733 }
2734
2735 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
2736     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
2737     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
2738     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
2739     VOID * callbackData)
2740 {
2741     GpPointF points[3];
2742
2743     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2744           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2745           srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2746
2747     points[0].X = dstx;
2748     points[0].Y = dsty;
2749     points[1].X = dstx + dstwidth;
2750     points[1].Y = dsty;
2751     points[2].X = dstx;
2752     points[2].Y = dsty + dstheight;
2753
2754     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2755                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2756 }
2757
2758 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
2759         INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
2760         INT srcwidth, INT srcheight, GpUnit srcUnit,
2761         GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
2762         VOID * callbackData)
2763 {
2764     GpPointF points[3];
2765
2766     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2767           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2768           srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2769
2770     points[0].X = dstx;
2771     points[0].Y = dsty;
2772     points[1].X = dstx + dstwidth;
2773     points[1].Y = dsty;
2774     points[2].X = dstx;
2775     points[2].Y = dsty + dstheight;
2776
2777     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2778                srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2779 }
2780
2781 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
2782     REAL x, REAL y, REAL width, REAL height)
2783 {
2784     RectF bounds;
2785     GpUnit unit;
2786     GpStatus ret;
2787
2788     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
2789
2790     if(!graphics || !image)
2791         return InvalidParameter;
2792
2793     ret = GdipGetImageBounds(image, &bounds, &unit);
2794     if(ret != Ok)
2795         return ret;
2796
2797     return GdipDrawImageRectRect(graphics, image, x, y, width, height,
2798                                  bounds.X, bounds.Y, bounds.Width, bounds.Height,
2799                                  unit, NULL, NULL, NULL);
2800 }
2801
2802 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
2803     INT x, INT y, INT width, INT height)
2804 {
2805     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
2806
2807     return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
2808 }
2809
2810 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
2811     REAL y1, REAL x2, REAL y2)
2812 {
2813     INT save_state;
2814     GpPointF pt[2];
2815     GpStatus retval;
2816
2817     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2818
2819     if(!pen || !graphics)
2820         return InvalidParameter;
2821
2822     if(graphics->busy)
2823         return ObjectBusy;
2824
2825     if (!graphics->hdc)
2826     {
2827         FIXME("graphics object has no HDC\n");
2828         return Ok;
2829     }
2830
2831     pt[0].X = x1;
2832     pt[0].Y = y1;
2833     pt[1].X = x2;
2834     pt[1].Y = y2;
2835
2836     save_state = prepare_dc(graphics, pen);
2837
2838     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2839
2840     restore_dc(graphics, save_state);
2841
2842     return retval;
2843 }
2844
2845 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2846     INT y1, INT x2, INT y2)
2847 {
2848     INT save_state;
2849     GpPointF pt[2];
2850     GpStatus retval;
2851
2852     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2853
2854     if(!pen || !graphics)
2855         return InvalidParameter;
2856
2857     if(graphics->busy)
2858         return ObjectBusy;
2859
2860     if (!graphics->hdc)
2861     {
2862         FIXME("graphics object has no HDC\n");
2863         return Ok;
2864     }
2865
2866     pt[0].X = (REAL)x1;
2867     pt[0].Y = (REAL)y1;
2868     pt[1].X = (REAL)x2;
2869     pt[1].Y = (REAL)y2;
2870
2871     save_state = prepare_dc(graphics, pen);
2872
2873     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2874
2875     restore_dc(graphics, save_state);
2876
2877     return retval;
2878 }
2879
2880 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2881     GpPointF *points, INT count)
2882 {
2883     INT save_state;
2884     GpStatus retval;
2885
2886     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2887
2888     if(!pen || !graphics || (count < 2))
2889         return InvalidParameter;
2890
2891     if(graphics->busy)
2892         return ObjectBusy;
2893
2894     if (!graphics->hdc)
2895     {
2896         FIXME("graphics object has no HDC\n");
2897         return Ok;
2898     }
2899
2900     save_state = prepare_dc(graphics, pen);
2901
2902     retval = draw_polyline(graphics, pen, points, count, TRUE);
2903
2904     restore_dc(graphics, save_state);
2905
2906     return retval;
2907 }
2908
2909 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2910     GpPoint *points, INT count)
2911 {
2912     INT save_state;
2913     GpStatus retval;
2914     GpPointF *ptf = NULL;
2915     int i;
2916
2917     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2918
2919     if(!pen || !graphics || (count < 2))
2920         return InvalidParameter;
2921
2922     if(graphics->busy)
2923         return ObjectBusy;
2924
2925     if (!graphics->hdc)
2926     {
2927         FIXME("graphics object has no HDC\n");
2928         return Ok;
2929     }
2930
2931     ptf = GdipAlloc(count * sizeof(GpPointF));
2932     if(!ptf) return OutOfMemory;
2933
2934     for(i = 0; i < count; i ++){
2935         ptf[i].X = (REAL) points[i].X;
2936         ptf[i].Y = (REAL) points[i].Y;
2937     }
2938
2939     save_state = prepare_dc(graphics, pen);
2940
2941     retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2942
2943     restore_dc(graphics, save_state);
2944
2945     GdipFree(ptf);
2946     return retval;
2947 }
2948
2949 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2950 {
2951     INT save_state;
2952     GpStatus retval;
2953
2954     TRACE("(%p, %p, %p)\n", graphics, pen, path);
2955
2956     if(!pen || !graphics)
2957         return InvalidParameter;
2958
2959     if(graphics->busy)
2960         return ObjectBusy;
2961
2962     if (!graphics->hdc)
2963     {
2964         FIXME("graphics object has no HDC\n");
2965         return Ok;
2966     }
2967
2968     save_state = prepare_dc(graphics, pen);
2969
2970     retval = draw_poly(graphics, pen, path->pathdata.Points,
2971                        path->pathdata.Types, path->pathdata.Count, TRUE);
2972
2973     restore_dc(graphics, save_state);
2974
2975     return retval;
2976 }
2977
2978 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2979     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2980 {
2981     INT save_state;
2982
2983     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2984             width, height, startAngle, sweepAngle);
2985
2986     if(!graphics || !pen)
2987         return InvalidParameter;
2988
2989     if(graphics->busy)
2990         return ObjectBusy;
2991
2992     if (!graphics->hdc)
2993     {
2994         FIXME("graphics object has no HDC\n");
2995         return Ok;
2996     }
2997
2998     save_state = prepare_dc(graphics, pen);
2999     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3000
3001     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3002
3003     restore_dc(graphics, save_state);
3004
3005     return Ok;
3006 }
3007
3008 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
3009     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3010 {
3011     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
3012             width, height, startAngle, sweepAngle);
3013
3014     return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3015 }
3016
3017 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
3018     REAL y, REAL width, REAL height)
3019 {
3020     INT save_state;
3021     GpPointF ptf[4];
3022     POINT pti[4];
3023
3024     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
3025
3026     if(!pen || !graphics)
3027         return InvalidParameter;
3028
3029     if(graphics->busy)
3030         return ObjectBusy;
3031
3032     if (!graphics->hdc)
3033     {
3034         FIXME("graphics object has no HDC\n");
3035         return Ok;
3036     }
3037
3038     ptf[0].X = x;
3039     ptf[0].Y = y;
3040     ptf[1].X = x + width;
3041     ptf[1].Y = y;
3042     ptf[2].X = x + width;
3043     ptf[2].Y = y + height;
3044     ptf[3].X = x;
3045     ptf[3].Y = y + height;
3046
3047     save_state = prepare_dc(graphics, pen);
3048     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3049
3050     transform_and_round_points(graphics, pti, ptf, 4);
3051     Polygon(graphics->hdc, pti, 4);
3052
3053     restore_dc(graphics, save_state);
3054
3055     return Ok;
3056 }
3057
3058 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
3059     INT y, INT width, INT height)
3060 {
3061     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
3062
3063     return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3064 }
3065
3066 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
3067     GDIPCONST GpRectF* rects, INT count)
3068 {
3069     GpPointF *ptf;
3070     POINT *pti;
3071     INT save_state, i;
3072
3073     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3074
3075     if(!graphics || !pen || !rects || count < 1)
3076         return InvalidParameter;
3077
3078     if(graphics->busy)
3079         return ObjectBusy;
3080
3081     if (!graphics->hdc)
3082     {
3083         FIXME("graphics object has no HDC\n");
3084         return Ok;
3085     }
3086
3087     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
3088     pti = GdipAlloc(4 * count * sizeof(POINT));
3089
3090     if(!ptf || !pti){
3091         GdipFree(ptf);
3092         GdipFree(pti);
3093         return OutOfMemory;
3094     }
3095
3096     for(i = 0; i < count; i++){
3097         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
3098         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
3099         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
3100         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
3101     }
3102
3103     save_state = prepare_dc(graphics, pen);
3104     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3105
3106     transform_and_round_points(graphics, pti, ptf, 4 * count);
3107
3108     for(i = 0; i < count; i++)
3109         Polygon(graphics->hdc, &pti[4 * i], 4);
3110
3111     restore_dc(graphics, save_state);
3112
3113     GdipFree(ptf);
3114     GdipFree(pti);
3115
3116     return Ok;
3117 }
3118
3119 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
3120     GDIPCONST GpRect* rects, INT count)
3121 {
3122     GpRectF *rectsF;
3123     GpStatus ret;
3124     INT i;
3125
3126     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
3127
3128     if(!rects || count<=0)
3129         return InvalidParameter;
3130
3131     rectsF = GdipAlloc(sizeof(GpRectF) * count);
3132     if(!rectsF)
3133         return OutOfMemory;
3134
3135     for(i = 0;i < count;i++){
3136         rectsF[i].X      = (REAL)rects[i].X;
3137         rectsF[i].Y      = (REAL)rects[i].Y;
3138         rectsF[i].Width  = (REAL)rects[i].Width;
3139         rectsF[i].Height = (REAL)rects[i].Height;
3140     }
3141
3142     ret = GdipDrawRectangles(graphics, pen, rectsF, count);
3143     GdipFree(rectsF);
3144
3145     return ret;
3146 }
3147
3148 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
3149     GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
3150 {
3151     GpPath *path;
3152     GpStatus stat;
3153
3154     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3155             count, tension, fill);
3156
3157     if(!graphics || !brush || !points)
3158         return InvalidParameter;
3159
3160     if(graphics->busy)
3161         return ObjectBusy;
3162
3163     if(count == 1)    /* Do nothing */
3164         return Ok;
3165
3166     stat = GdipCreatePath(fill, &path);
3167     if(stat != Ok)
3168         return stat;
3169
3170     stat = GdipAddPathClosedCurve2(path, points, count, tension);
3171     if(stat != Ok){
3172         GdipDeletePath(path);
3173         return stat;
3174     }
3175
3176     stat = GdipFillPath(graphics, brush, path);
3177     if(stat != Ok){
3178         GdipDeletePath(path);
3179         return stat;
3180     }
3181
3182     GdipDeletePath(path);
3183
3184     return Ok;
3185 }
3186
3187 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
3188     GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
3189 {
3190     GpPointF *ptf;
3191     GpStatus stat;
3192     INT i;
3193
3194     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
3195             count, tension, fill);
3196
3197     if(!points || count == 0)
3198         return InvalidParameter;
3199
3200     if(count == 1)    /* Do nothing */
3201         return Ok;
3202
3203     ptf = GdipAlloc(sizeof(GpPointF)*count);
3204     if(!ptf)
3205         return OutOfMemory;
3206
3207     for(i = 0;i < count;i++){
3208         ptf[i].X = (REAL)points[i].X;
3209         ptf[i].Y = (REAL)points[i].Y;
3210     }
3211
3212     stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
3213
3214     GdipFree(ptf);
3215
3216     return stat;
3217 }
3218
3219 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
3220     GDIPCONST GpPointF *points, INT count)
3221 {
3222     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3223     return GdipFillClosedCurve2(graphics, brush, points, count,
3224                0.5f, FillModeAlternate);
3225 }
3226
3227 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
3228     GDIPCONST GpPoint *points, INT count)
3229 {
3230     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3231     return GdipFillClosedCurve2I(graphics, brush, points, count,
3232                0.5f, FillModeAlternate);
3233 }
3234
3235 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
3236     REAL y, REAL width, REAL height)
3237 {
3238     GpStatus stat;
3239     GpPath *path;
3240
3241     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3242
3243     if(!graphics || !brush)
3244         return InvalidParameter;
3245
3246     if(graphics->busy)
3247         return ObjectBusy;
3248
3249     stat = GdipCreatePath(FillModeAlternate, &path);
3250
3251     if (stat == Ok)
3252     {
3253         stat = GdipAddPathEllipse(path, x, y, width, height);
3254
3255         if (stat == Ok)
3256             stat = GdipFillPath(graphics, brush, path);
3257
3258         GdipDeletePath(path);
3259     }
3260
3261     return stat;
3262 }
3263
3264 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
3265     INT y, INT width, INT height)
3266 {
3267     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3268
3269     return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
3270 }
3271
3272 static GpStatus GDI32_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3273 {
3274     INT save_state;
3275     GpStatus retval;
3276
3277     if(!graphics->hdc || !brush_can_fill_path(brush))
3278         return NotImplemented;
3279
3280     save_state = SaveDC(graphics->hdc);
3281     EndPath(graphics->hdc);
3282     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
3283                                                                     : WINDING));
3284
3285     BeginPath(graphics->hdc);
3286     retval = draw_poly(graphics, NULL, path->pathdata.Points,
3287                        path->pathdata.Types, path->pathdata.Count, FALSE);
3288
3289     if(retval != Ok)
3290         goto end;
3291
3292     EndPath(graphics->hdc);
3293     brush_fill_path(graphics, brush);
3294
3295     retval = Ok;
3296
3297 end:
3298     RestoreDC(graphics->hdc, save_state);
3299
3300     return retval;
3301 }
3302
3303 static GpStatus SOFTWARE_GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3304 {
3305     GpStatus stat;
3306     GpRegion *rgn;
3307
3308     if (!brush_can_fill_pixels(brush))
3309         return NotImplemented;
3310
3311     /* FIXME: This could probably be done more efficiently without regions. */
3312
3313     stat = GdipCreateRegionPath(path, &rgn);
3314
3315     if (stat == Ok)
3316     {
3317         stat = GdipFillRegion(graphics, brush, rgn);
3318
3319         GdipDeleteRegion(rgn);
3320     }
3321
3322     return stat;
3323 }
3324
3325 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
3326 {
3327     GpStatus stat = NotImplemented;
3328
3329     TRACE("(%p, %p, %p)\n", graphics, brush, path);
3330
3331     if(!brush || !graphics || !path)
3332         return InvalidParameter;
3333
3334     if(graphics->busy)
3335         return ObjectBusy;
3336
3337     if (!graphics->image)
3338         stat = GDI32_GdipFillPath(graphics, brush, path);
3339
3340     if (stat == NotImplemented)
3341         stat = SOFTWARE_GdipFillPath(graphics, brush, path);
3342
3343     if (stat == NotImplemented)
3344     {
3345         FIXME("Not implemented for brushtype %i\n", brush->bt);
3346         stat = Ok;
3347     }
3348
3349     return stat;
3350 }
3351
3352 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
3353     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
3354 {
3355     GpStatus stat;
3356     GpPath *path;
3357
3358     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
3359             graphics, brush, x, y, width, height, startAngle, sweepAngle);
3360
3361     if(!graphics || !brush)
3362         return InvalidParameter;
3363
3364     if(graphics->busy)
3365         return ObjectBusy;
3366
3367     stat = GdipCreatePath(FillModeAlternate, &path);
3368
3369     if (stat == Ok)
3370     {
3371         stat = GdipAddPathPie(path, x, y, width, height, startAngle, sweepAngle);
3372
3373         if (stat == Ok)
3374             stat = GdipFillPath(graphics, brush, path);
3375
3376         GdipDeletePath(path);
3377     }
3378
3379     return stat;
3380 }
3381
3382 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3383     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3384 {
3385     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3386             graphics, brush, x, y, width, height, startAngle, sweepAngle);
3387
3388     return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3389 }
3390
3391 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3392     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3393 {
3394     GpStatus stat;
3395     GpPath *path;
3396
3397     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3398
3399     if(!graphics || !brush || !points || !count)
3400         return InvalidParameter;
3401
3402     if(graphics->busy)
3403         return ObjectBusy;
3404
3405     stat = GdipCreatePath(fillMode, &path);
3406
3407     if (stat == Ok)
3408     {
3409         stat = GdipAddPathPolygon(path, points, count);
3410
3411         if (stat == Ok)
3412             stat = GdipFillPath(graphics, brush, path);
3413
3414         GdipDeletePath(path);
3415     }
3416
3417     return stat;
3418 }
3419
3420 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3421     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3422 {
3423     GpStatus stat;
3424     GpPath *path;
3425
3426     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3427
3428     if(!graphics || !brush || !points || !count)
3429         return InvalidParameter;
3430
3431     if(graphics->busy)
3432         return ObjectBusy;
3433
3434     stat = GdipCreatePath(fillMode, &path);
3435
3436     if (stat == Ok)
3437     {
3438         stat = GdipAddPathPolygonI(path, points, count);
3439
3440         if (stat == Ok)
3441             stat = GdipFillPath(graphics, brush, path);
3442
3443         GdipDeletePath(path);
3444     }
3445
3446     return stat;
3447 }
3448
3449 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3450     GDIPCONST GpPointF *points, INT count)
3451 {
3452     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3453
3454     return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3455 }
3456
3457 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3458     GDIPCONST GpPoint *points, INT count)
3459 {
3460     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3461
3462     return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3463 }
3464
3465 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3466     REAL x, REAL y, REAL width, REAL height)
3467 {
3468     GpStatus stat;
3469     GpPath *path;
3470
3471     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3472
3473     if(!graphics || !brush)
3474         return InvalidParameter;
3475
3476     if(graphics->busy)
3477         return ObjectBusy;
3478
3479     stat = GdipCreatePath(FillModeAlternate, &path);
3480
3481     if (stat == Ok)
3482     {
3483         stat = GdipAddPathRectangle(path, x, y, width, height);
3484
3485         if (stat == Ok)
3486             stat = GdipFillPath(graphics, brush, path);
3487
3488         GdipDeletePath(path);
3489     }
3490
3491     return stat;
3492 }
3493
3494 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3495     INT x, INT y, INT width, INT height)
3496 {
3497     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3498
3499     return GdipFillRectangle(graphics, brush, x, y, width, height);
3500 }
3501
3502 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3503     INT count)
3504 {
3505     GpStatus ret;
3506     INT i;
3507
3508     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3509
3510     if(!rects)
3511         return InvalidParameter;
3512
3513     for(i = 0; i < count; i++){
3514         ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
3515         if(ret != Ok)   return ret;
3516     }
3517
3518     return Ok;
3519 }
3520
3521 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3522     INT count)
3523 {
3524     GpRectF *rectsF;
3525     GpStatus ret;
3526     INT i;
3527
3528     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3529
3530     if(!rects || count <= 0)
3531         return InvalidParameter;
3532
3533     rectsF = GdipAlloc(sizeof(GpRectF)*count);
3534     if(!rectsF)
3535         return OutOfMemory;
3536
3537     for(i = 0; i < count; i++){
3538         rectsF[i].X      = (REAL)rects[i].X;
3539         rectsF[i].Y      = (REAL)rects[i].Y;
3540         rectsF[i].X      = (REAL)rects[i].Width;
3541         rectsF[i].Height = (REAL)rects[i].Height;
3542     }
3543
3544     ret = GdipFillRectangles(graphics,brush,rectsF,count);
3545     GdipFree(rectsF);
3546
3547     return ret;
3548 }
3549
3550 static GpStatus GDI32_GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3551     GpRegion* region)
3552 {
3553     INT save_state;
3554     GpStatus status;
3555     HRGN hrgn;
3556     RECT rc;
3557
3558     if(!graphics->hdc || !brush_can_fill_path(brush))
3559         return NotImplemented;
3560
3561     status = GdipGetRegionHRgn(region, graphics, &hrgn);
3562     if(status != Ok)
3563         return status;
3564
3565     save_state = SaveDC(graphics->hdc);
3566     EndPath(graphics->hdc);
3567
3568     ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3569
3570     if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3571     {
3572         BeginPath(graphics->hdc);
3573         Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3574         EndPath(graphics->hdc);
3575
3576         brush_fill_path(graphics, brush);
3577     }
3578
3579     RestoreDC(graphics->hdc, save_state);
3580
3581     DeleteObject(hrgn);
3582
3583     return Ok;
3584 }
3585
3586 static GpStatus SOFTWARE_GdipFillRegion(GpGraphics *graphics, GpBrush *brush,
3587     GpRegion* region)
3588 {
3589     GpStatus stat;
3590     GpRegion *temp_region;
3591     GpMatrix *world_to_device, *identity;
3592     GpRectF graphics_bounds;
3593     UINT scans_count, i;
3594     INT dummy;
3595     GpRect *scans;
3596     DWORD *pixel_data;
3597
3598     if (!brush_can_fill_pixels(brush))
3599         return NotImplemented;
3600
3601     stat = get_graphics_bounds(graphics, &graphics_bounds);
3602
3603     if (stat == Ok)
3604         stat = GdipCloneRegion(region, &temp_region);
3605
3606     if (stat == Ok)
3607     {
3608         stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
3609             CoordinateSpaceWorld, &world_to_device);
3610
3611         if (stat == Ok)
3612         {
3613             stat = GdipTransformRegion(temp_region, world_to_device);
3614
3615             GdipDeleteMatrix(world_to_device);
3616         }
3617
3618         if (stat == Ok)
3619             stat = GdipCombineRegionRect(temp_region, &graphics_bounds, CombineModeIntersect);
3620
3621         if (stat == Ok)
3622             stat = GdipCreateMatrix(&identity);
3623
3624         if (stat == Ok)
3625         {
3626             stat = GdipGetRegionScansCount(temp_region, &scans_count, identity);
3627
3628             if (stat == Ok && scans_count != 0)
3629             {
3630                 scans = GdipAlloc(sizeof(*scans) * scans_count);
3631                 if (!scans)
3632                     stat = OutOfMemory;
3633
3634                 if (stat == Ok)
3635                 {
3636                     stat = GdipGetRegionScansI(temp_region, scans, &dummy, identity);
3637
3638                     if (stat != Ok)
3639                         GdipFree(scans);
3640                 }
3641             }
3642
3643             GdipDeleteMatrix(identity);
3644         }
3645
3646         GdipDeleteRegion(temp_region);
3647     }
3648
3649     if (stat == Ok && scans_count == 0)
3650         return Ok;
3651
3652     if (stat == Ok)
3653     {
3654         if (!graphics->image)
3655         {
3656             /* If we have to go through gdi32, use as few alpha blends as possible. */
3657             INT min_x, min_y, max_x, max_y;
3658             UINT data_width, data_height;
3659
3660             min_x = scans[0].X;
3661             min_y = scans[0].Y;
3662             max_x = scans[0].X+scans[0].Width;
3663             max_y = scans[0].Y+scans[0].Height;
3664
3665             for (i=1; i<scans_count; i++)
3666             {
3667                 min_x = min(min_x, scans[i].X);
3668                 min_y = min(min_y, scans[i].Y);
3669                 max_x = max(max_x, scans[i].X+scans[i].Width);
3670                 max_y = max(max_y, scans[i].Y+scans[i].Height);
3671             }
3672
3673             data_width = max_x - min_x;
3674             data_height = max_y - min_y;
3675
3676             pixel_data = GdipAlloc(sizeof(*pixel_data) * data_width * data_height);
3677             if (!pixel_data)
3678                 stat = OutOfMemory;
3679
3680             if (stat == Ok)
3681             {
3682                 for (i=0; i<scans_count; i++)
3683                 {
3684                     stat = brush_fill_pixels(graphics, brush,
3685                         pixel_data + (scans[i].X - min_x) + (scans[i].Y - min_y) * data_width,
3686                         &scans[i], data_width);
3687
3688                     if (stat != Ok)
3689                         break;
3690                 }
3691
3692                 if (stat == Ok)
3693                 {
3694                     stat = alpha_blend_pixels(graphics, min_x, min_y,
3695                         (BYTE*)pixel_data, data_width, data_height,
3696                         data_width * 4);
3697                 }
3698
3699                 GdipFree(pixel_data);
3700             }
3701         }
3702         else
3703         {
3704             UINT max_size=0;
3705
3706             for (i=0; i<scans_count; i++)
3707             {
3708                 UINT size = scans[i].Width * scans[i].Height;
3709
3710                 if (size > max_size)
3711                     max_size = size;
3712             }
3713
3714             pixel_data = GdipAlloc(sizeof(*pixel_data) * max_size);
3715             if (!pixel_data)
3716                 stat = OutOfMemory;
3717
3718             if (stat == Ok)
3719             {
3720                 for (i=0; i<scans_count; i++)
3721                 {
3722                     stat = brush_fill_pixels(graphics, brush, pixel_data, &scans[i],
3723                         scans[i].Width);
3724
3725                     if (stat == Ok)
3726                     {
3727                         stat = alpha_blend_pixels(graphics, scans[i].X, scans[i].Y,
3728                             (BYTE*)pixel_data, scans[i].Width, scans[i].Height,
3729                             scans[i].Width * 4);
3730                     }
3731
3732                     if (stat != Ok)
3733                         break;
3734                 }
3735
3736                 GdipFree(pixel_data);
3737             }
3738         }
3739
3740         GdipFree(scans);
3741     }
3742
3743     return stat;
3744 }
3745
3746 /*****************************************************************************
3747  * GdipFillRegion [GDIPLUS.@]
3748  */
3749 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3750         GpRegion* region)
3751 {
3752     GpStatus stat = NotImplemented;
3753
3754     TRACE("(%p, %p, %p)\n", graphics, brush, region);
3755
3756     if (!(graphics && brush && region))
3757         return InvalidParameter;
3758
3759     if(graphics->busy)
3760         return ObjectBusy;
3761
3762     if (!graphics->image)
3763         stat = GDI32_GdipFillRegion(graphics, brush, region);
3764
3765     if (stat == NotImplemented)
3766         stat = SOFTWARE_GdipFillRegion(graphics, brush, region);
3767
3768     if (stat == NotImplemented)
3769     {
3770         FIXME("not implemented for brushtype %i\n", brush->bt);
3771         stat = Ok;
3772     }
3773
3774     return stat;
3775 }
3776
3777 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3778 {
3779     TRACE("(%p,%u)\n", graphics, intention);
3780
3781     if(!graphics)
3782         return InvalidParameter;
3783
3784     if(graphics->busy)
3785         return ObjectBusy;
3786
3787     /* We have no internal operation queue, so there's no need to clear it. */
3788
3789     if (graphics->hdc)
3790         GdiFlush();
3791
3792     return Ok;
3793 }
3794
3795 /*****************************************************************************
3796  * GdipGetClipBounds [GDIPLUS.@]
3797  */
3798 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3799 {
3800     TRACE("(%p, %p)\n", graphics, rect);
3801
3802     if(!graphics)
3803         return InvalidParameter;
3804
3805     if(graphics->busy)
3806         return ObjectBusy;
3807
3808     return GdipGetRegionBounds(graphics->clip, graphics, rect);
3809 }
3810
3811 /*****************************************************************************
3812  * GdipGetClipBoundsI [GDIPLUS.@]
3813  */
3814 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
3815 {
3816     TRACE("(%p, %p)\n", graphics, rect);
3817
3818     if(!graphics)
3819         return InvalidParameter;
3820
3821     if(graphics->busy)
3822         return ObjectBusy;
3823
3824     return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
3825 }
3826
3827 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
3828 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
3829     CompositingMode *mode)
3830 {
3831     TRACE("(%p, %p)\n", graphics, mode);
3832
3833     if(!graphics || !mode)
3834         return InvalidParameter;
3835
3836     if(graphics->busy)
3837         return ObjectBusy;
3838
3839     *mode = graphics->compmode;
3840
3841     return Ok;
3842 }
3843
3844 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3845 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3846     CompositingQuality *quality)
3847 {
3848     TRACE("(%p, %p)\n", graphics, quality);
3849
3850     if(!graphics || !quality)
3851         return InvalidParameter;
3852
3853     if(graphics->busy)
3854         return ObjectBusy;
3855
3856     *quality = graphics->compqual;
3857
3858     return Ok;
3859 }
3860
3861 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3862 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3863     InterpolationMode *mode)
3864 {
3865     TRACE("(%p, %p)\n", graphics, mode);
3866
3867     if(!graphics || !mode)
3868         return InvalidParameter;
3869
3870     if(graphics->busy)
3871         return ObjectBusy;
3872
3873     *mode = graphics->interpolation;
3874
3875     return Ok;
3876 }
3877
3878 /* FIXME: Need to handle color depths less than 24bpp */
3879 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3880 {
3881     FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
3882
3883     if(!graphics || !argb)
3884         return InvalidParameter;
3885
3886     if(graphics->busy)
3887         return ObjectBusy;
3888
3889     return Ok;
3890 }
3891
3892 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3893 {
3894     TRACE("(%p, %p)\n", graphics, scale);
3895
3896     if(!graphics || !scale)
3897         return InvalidParameter;
3898
3899     if(graphics->busy)
3900         return ObjectBusy;
3901
3902     *scale = graphics->scale;
3903
3904     return Ok;
3905 }
3906
3907 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3908 {
3909     TRACE("(%p, %p)\n", graphics, unit);
3910
3911     if(!graphics || !unit)
3912         return InvalidParameter;
3913
3914     if(graphics->busy)
3915         return ObjectBusy;
3916
3917     *unit = graphics->unit;
3918
3919     return Ok;
3920 }
3921
3922 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3923 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3924     *mode)
3925 {
3926     TRACE("(%p, %p)\n", graphics, mode);
3927
3928     if(!graphics || !mode)
3929         return InvalidParameter;
3930
3931     if(graphics->busy)
3932         return ObjectBusy;
3933
3934     *mode = graphics->pixeloffset;
3935
3936     return Ok;
3937 }
3938
3939 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3940 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3941 {
3942     TRACE("(%p, %p)\n", graphics, mode);
3943
3944     if(!graphics || !mode)
3945         return InvalidParameter;
3946
3947     if(graphics->busy)
3948         return ObjectBusy;
3949
3950     *mode = graphics->smoothing;
3951
3952     return Ok;
3953 }
3954
3955 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3956 {
3957     TRACE("(%p, %p)\n", graphics, contrast);
3958
3959     if(!graphics || !contrast)
3960         return InvalidParameter;
3961
3962     *contrast = graphics->textcontrast;
3963
3964     return Ok;
3965 }
3966
3967 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3968 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3969     TextRenderingHint *hint)
3970 {
3971     TRACE("(%p, %p)\n", graphics, hint);
3972
3973     if(!graphics || !hint)
3974         return InvalidParameter;
3975
3976     if(graphics->busy)
3977         return ObjectBusy;
3978
3979     *hint = graphics->texthint;
3980
3981     return Ok;
3982 }
3983
3984 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3985 {
3986     GpRegion *clip_rgn;
3987     GpStatus stat;
3988
3989     TRACE("(%p, %p)\n", graphics, rect);
3990
3991     if(!graphics || !rect)
3992         return InvalidParameter;
3993
3994     if(graphics->busy)
3995         return ObjectBusy;
3996
3997     /* intersect window and graphics clipping regions */
3998     if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3999         return stat;
4000
4001     if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
4002         goto cleanup;
4003
4004     /* get bounds of the region */
4005     stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
4006
4007 cleanup:
4008     GdipDeleteRegion(clip_rgn);
4009
4010     return stat;
4011 }
4012
4013 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4014 {
4015     GpRectF rectf;
4016     GpStatus stat;
4017
4018     TRACE("(%p, %p)\n", graphics, rect);
4019
4020     if(!graphics || !rect)
4021         return InvalidParameter;
4022
4023     if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
4024     {
4025         rect->X = roundr(rectf.X);
4026         rect->Y = roundr(rectf.Y);
4027         rect->Width  = roundr(rectf.Width);
4028         rect->Height = roundr(rectf.Height);
4029     }
4030
4031     return stat;
4032 }
4033
4034 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4035 {
4036     TRACE("(%p, %p)\n", graphics, matrix);
4037
4038     if(!graphics || !matrix)
4039         return InvalidParameter;
4040
4041     if(graphics->busy)
4042         return ObjectBusy;
4043
4044     *matrix = *graphics->worldtrans;
4045     return Ok;
4046 }
4047
4048 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
4049 {
4050     GpSolidFill *brush;
4051     GpStatus stat;
4052     GpRectF wnd_rect;
4053
4054     TRACE("(%p, %x)\n", graphics, color);
4055
4056     if(!graphics)
4057         return InvalidParameter;
4058
4059     if(graphics->busy)
4060         return ObjectBusy;
4061
4062     if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
4063         return stat;
4064
4065     if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
4066         GdipDeleteBrush((GpBrush*)brush);
4067         return stat;
4068     }
4069
4070     GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
4071                                                  wnd_rect.Width, wnd_rect.Height);
4072
4073     GdipDeleteBrush((GpBrush*)brush);
4074
4075     return Ok;
4076 }
4077
4078 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
4079 {
4080     TRACE("(%p, %p)\n", graphics, res);
4081
4082     if(!graphics || !res)
4083         return InvalidParameter;
4084
4085     return GdipIsEmptyRegion(graphics->clip, graphics, res);
4086 }
4087
4088 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
4089 {
4090     GpStatus stat;
4091     GpRegion* rgn;
4092     GpPointF pt;
4093
4094     TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
4095
4096     if(!graphics || !result)
4097         return InvalidParameter;
4098
4099     if(graphics->busy)
4100         return ObjectBusy;
4101
4102     pt.X = x;
4103     pt.Y = y;
4104     if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4105                    CoordinateSpaceWorld, &pt, 1)) != Ok)
4106         return stat;
4107
4108     if((stat = GdipCreateRegion(&rgn)) != Ok)
4109         return stat;
4110
4111     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4112         goto cleanup;
4113
4114     stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
4115
4116 cleanup:
4117     GdipDeleteRegion(rgn);
4118     return stat;
4119 }
4120
4121 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
4122 {
4123     return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
4124 }
4125
4126 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
4127 {
4128     GpStatus stat;
4129     GpRegion* rgn;
4130     GpPointF pts[2];
4131
4132     TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
4133
4134     if(!graphics || !result)
4135         return InvalidParameter;
4136
4137     if(graphics->busy)
4138         return ObjectBusy;
4139
4140     pts[0].X = x;
4141     pts[0].Y = y;
4142     pts[1].X = x + width;
4143     pts[1].Y = y + height;
4144
4145     if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
4146                     CoordinateSpaceWorld, pts, 2)) != Ok)
4147         return stat;
4148
4149     pts[1].X -= pts[0].X;
4150     pts[1].Y -= pts[0].Y;
4151
4152     if((stat = GdipCreateRegion(&rgn)) != Ok)
4153         return stat;
4154
4155     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
4156         goto cleanup;
4157
4158     stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
4159
4160 cleanup:
4161     GdipDeleteRegion(rgn);
4162     return stat;
4163 }
4164
4165 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
4166 {
4167     return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
4168 }
4169
4170 GpStatus gdip_format_string(HDC hdc,
4171     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4172     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4173     gdip_format_string_callback callback, void *user_data)
4174 {
4175     WCHAR* stringdup;
4176     int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
4177         nheight, lineend, lineno = 0;
4178     RectF bounds;
4179     StringAlignment halign;
4180     GpStatus stat = Ok;
4181     SIZE size;
4182
4183     if(length == -1) length = lstrlenW(string);
4184
4185     stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
4186     if(!stringdup) return OutOfMemory;
4187
4188     nwidth = roundr(rect->Width);
4189     nheight = roundr(rect->Height);
4190
4191     if (rect->Width >= INT_MAX || rect->Width < 0.5) nwidth = INT_MAX;
4192     if (rect->Height >= INT_MAX || rect->Width < 0.5) nheight = INT_MAX;
4193
4194     for(i = 0, j = 0; i < length; i++){
4195         /* FIXME: This makes the indexes passed to callback inaccurate. */
4196         if(!isprintW(string[i]) && (string[i] != '\n'))
4197             continue;
4198
4199         stringdup[j] = string[i];
4200         j++;
4201     }
4202
4203     length = j;
4204
4205     if (format) halign = format->align;
4206     else halign = StringAlignmentNear;
4207
4208     while(sum < length){
4209         GetTextExtentExPointW(hdc, stringdup + sum, length - sum,
4210                               nwidth, &fit, NULL, &size);
4211         fitcpy = fit;
4212
4213         if(fit == 0)
4214             break;
4215
4216         for(lret = 0; lret < fit; lret++)
4217             if(*(stringdup + sum + lret) == '\n')
4218                 break;
4219
4220         /* Line break code (may look strange, but it imitates windows). */
4221         if(lret < fit)
4222             lineend = fit = lret;    /* this is not an off-by-one error */
4223         else if(fit < (length - sum)){
4224             if(*(stringdup + sum + fit) == ' ')
4225                 while(*(stringdup + sum + fit) == ' ')
4226                     fit++;
4227             else
4228                 while(*(stringdup + sum + fit - 1) != ' '){
4229                     fit--;
4230
4231                     if(*(stringdup + sum + fit) == '\t')
4232                         break;
4233
4234                     if(fit == 0){
4235                         fit = fitcpy;
4236                         break;
4237                     }
4238                 }
4239             lineend = fit;
4240             while(*(stringdup + sum + lineend - 1) == ' ' ||
4241                   *(stringdup + sum + lineend - 1) == '\t')
4242                 lineend--;
4243         }
4244         else
4245             lineend = fit;
4246
4247         GetTextExtentExPointW(hdc, stringdup + sum, lineend,
4248                               nwidth, &j, NULL, &size);
4249
4250         bounds.Width = size.cx;
4251
4252         if(height + size.cy > nheight)
4253             bounds.Height = nheight - (height + size.cy);
4254         else
4255             bounds.Height = size.cy;
4256
4257         bounds.Y = rect->Y + height;
4258
4259         switch (halign)
4260         {
4261         case StringAlignmentNear:
4262         default:
4263             bounds.X = rect->X;
4264             break;
4265         case StringAlignmentCenter:
4266             bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
4267             break;
4268         case StringAlignmentFar:
4269             bounds.X = rect->X + rect->Width - bounds.Width;
4270             break;
4271         }
4272
4273         stat = callback(hdc, stringdup, sum, lineend,
4274             font, rect, format, lineno, &bounds, user_data);
4275
4276         if (stat != Ok)
4277             break;
4278
4279         sum += fit + (lret < fitcpy ? 1 : 0);
4280         height += size.cy;
4281         lineno++;
4282
4283         if(height > nheight)
4284             break;
4285
4286         /* Stop if this was a linewrap (but not if it was a linebreak). */
4287         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
4288             break;
4289     }
4290
4291     GdipFree(stringdup);
4292
4293     return stat;
4294 }
4295
4296 struct measure_ranges_args {
4297     GpRegion **regions;
4298 };
4299
4300 static GpStatus measure_ranges_callback(HDC hdc,
4301     GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4302     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4303     INT lineno, const RectF *bounds, void *user_data)
4304 {
4305     int i;
4306     GpStatus stat = Ok;
4307     struct measure_ranges_args *args = user_data;
4308
4309     for (i=0; i<format->range_count; i++)
4310     {
4311         INT range_start = max(index, format->character_ranges[i].First);
4312         INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
4313         if (range_start < range_end)
4314         {
4315             GpRectF range_rect;
4316             SIZE range_size;
4317
4318             range_rect.Y = bounds->Y;
4319             range_rect.Height = bounds->Height;
4320
4321             GetTextExtentExPointW(hdc, string + index, range_start - index,
4322                                   INT_MAX, NULL, NULL, &range_size);
4323             range_rect.X = bounds->X + range_size.cx;
4324
4325             GetTextExtentExPointW(hdc, string + index, range_end - index,
4326                                   INT_MAX, NULL, NULL, &range_size);
4327             range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
4328
4329             stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
4330             if (stat != Ok)
4331                 break;
4332         }
4333     }
4334
4335     return stat;
4336 }
4337
4338 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
4339         GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
4340         GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
4341         INT regionCount, GpRegion** regions)
4342 {
4343     GpStatus stat;
4344     int i;
4345     HFONT oldfont;
4346     struct measure_ranges_args args;
4347     HDC hdc, temp_hdc=NULL;
4348
4349     TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
4350             length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
4351
4352     if (!(graphics && string && font && layoutRect && stringFormat && regions))
4353         return InvalidParameter;
4354
4355     if (regionCount < stringFormat->range_count)
4356         return InvalidParameter;
4357
4358     if(!graphics->hdc)
4359     {
4360         hdc = temp_hdc = CreateCompatibleDC(0);
4361         if (!temp_hdc) return OutOfMemory;
4362     }
4363     else
4364         hdc = graphics->hdc;
4365
4366     if (stringFormat->attr)
4367         TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
4368
4369     oldfont = SelectObject(hdc, CreateFontIndirectW(&font->lfw));
4370
4371     for (i=0; i<stringFormat->range_count; i++)
4372     {
4373         stat = GdipSetEmpty(regions[i]);
4374         if (stat != Ok)
4375             return stat;
4376     }
4377
4378     args.regions = regions;
4379
4380     stat = gdip_format_string(hdc, string, length, font, layoutRect, stringFormat,
4381         measure_ranges_callback, &args);
4382
4383     DeleteObject(SelectObject(hdc, oldfont));
4384
4385     if (temp_hdc)
4386         DeleteDC(temp_hdc);
4387
4388     return stat;
4389 }
4390
4391 struct measure_string_args {
4392     RectF *bounds;
4393     INT *codepointsfitted;
4394     INT *linesfilled;
4395 };
4396
4397 static GpStatus measure_string_callback(HDC hdc,
4398     GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4399     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4400     INT lineno, const RectF *bounds, void *user_data)
4401 {
4402     struct measure_string_args *args = user_data;
4403
4404     if (bounds->Width > args->bounds->Width)
4405         args->bounds->Width = bounds->Width;
4406
4407     if (bounds->Height + bounds->Y > args->bounds->Height + args->bounds->Y)
4408         args->bounds->Height = bounds->Height + bounds->Y - args->bounds->Y;
4409
4410     if (args->codepointsfitted)
4411         *args->codepointsfitted = index + length;
4412
4413     if (args->linesfilled)
4414         (*args->linesfilled)++;
4415
4416     return Ok;
4417 }
4418
4419 /* Find the smallest rectangle that bounds the text when it is printed in rect
4420  * according to the format options listed in format. If rect has 0 width and
4421  * height, then just find the smallest rectangle that bounds the text when it's
4422  * printed at location (rect->X, rect-Y). */
4423 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
4424     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
4425     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
4426     INT *codepointsfitted, INT *linesfilled)
4427 {
4428     HFONT oldfont;
4429     struct measure_string_args args;
4430     HDC temp_hdc=NULL;
4431
4432     TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
4433         debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
4434         bounds, codepointsfitted, linesfilled);
4435
4436     if(!graphics || !string || !font || !rect || !bounds)
4437         return InvalidParameter;
4438
4439     if(!graphics->hdc)
4440     {
4441         temp_hdc = CreateCompatibleDC(0);
4442         if (!temp_hdc) return OutOfMemory;
4443     }
4444
4445     if(linesfilled) *linesfilled = 0;
4446     if(codepointsfitted) *codepointsfitted = 0;
4447
4448     if(format)
4449         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4450
4451     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
4452
4453     bounds->X = rect->X;
4454     bounds->Y = rect->Y;
4455     bounds->Width = 0.0;
4456     bounds->Height = 0.0;
4457
4458     args.bounds = bounds;
4459     args.codepointsfitted = codepointsfitted;
4460     args.linesfilled = linesfilled;
4461
4462     gdip_format_string(graphics->hdc ? graphics->hdc : temp_hdc, string, length, font, rect, format,
4463         measure_string_callback, &args);
4464
4465     DeleteObject(SelectObject(graphics->hdc, oldfont));
4466
4467     if (temp_hdc)
4468         DeleteDC(temp_hdc);
4469
4470     return Ok;
4471 }
4472
4473 struct draw_string_args {
4474     POINT drawbase;
4475     UINT drawflags;
4476     REAL ang_cos, ang_sin;
4477 };
4478
4479 static GpStatus draw_string_callback(HDC hdc,
4480     GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4481     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4482     INT lineno, const RectF *bounds, void *user_data)
4483 {
4484     struct draw_string_args *args = user_data;
4485     RECT drawcoord;
4486
4487     drawcoord.left = drawcoord.right = args->drawbase.x + roundr(args->ang_sin * bounds->Y);
4488     drawcoord.top = drawcoord.bottom = args->drawbase.y + roundr(args->ang_cos * bounds->Y);
4489
4490     DrawTextW(hdc, string + index, length, &drawcoord, args->drawflags);
4491
4492     return Ok;
4493 }
4494
4495 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4496     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4497     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4498 {
4499     HRGN rgn = NULL;
4500     HFONT gdifont;
4501     LOGFONTW lfw;
4502     TEXTMETRICW textmet;
4503     GpPointF pt[3], rectcpy[4];
4504     POINT corners[4];
4505     REAL angle, rel_width, rel_height;
4506     INT offsety = 0, save_state;
4507     struct draw_string_args args;
4508     RectF scaled_rect;
4509
4510     TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4511         length, font, debugstr_rectf(rect), format, brush);
4512
4513     if(!graphics || !string || !font || !brush || !rect)
4514         return InvalidParameter;
4515
4516     if((brush->bt != BrushTypeSolidColor)){
4517         FIXME("not implemented for given parameters\n");
4518         return NotImplemented;
4519     }
4520
4521     if(!graphics->hdc)
4522     {
4523         FIXME("graphics object has no HDC\n");
4524         return Ok;
4525     }
4526
4527     if(format){
4528         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4529
4530         /* Should be no need to explicitly test for StringAlignmentNear as
4531          * that is default behavior if no alignment is passed. */
4532         if(format->vertalign != StringAlignmentNear){
4533             RectF bounds;
4534             GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
4535
4536             if(format->vertalign == StringAlignmentCenter)
4537                 offsety = (rect->Height - bounds.Height) / 2;
4538             else if(format->vertalign == StringAlignmentFar)
4539                 offsety = (rect->Height - bounds.Height);
4540         }
4541     }
4542
4543     save_state = SaveDC(graphics->hdc);
4544     SetBkMode(graphics->hdc, TRANSPARENT);
4545     SetTextColor(graphics->hdc, brush->lb.lbColor);
4546
4547     pt[0].X = 0.0;
4548     pt[0].Y = 0.0;
4549     pt[1].X = 1.0;
4550     pt[1].Y = 0.0;
4551     pt[2].X = 0.0;
4552     pt[2].Y = 1.0;
4553     GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4554     angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
4555     args.ang_cos = cos(angle);
4556     args.ang_sin = sin(angle);
4557     rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4558                      (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4559     rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4560                       (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4561
4562     rectcpy[3].X = rectcpy[0].X = rect->X;
4563     rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
4564     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4565     rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
4566     transform_and_round_points(graphics, corners, rectcpy, 4);
4567
4568     scaled_rect.X = 0.0;
4569     scaled_rect.Y = 0.0;
4570     scaled_rect.Width = rel_width * rect->Width;
4571     scaled_rect.Height = rel_height * rect->Height;
4572
4573     if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
4574     {
4575         /* FIXME: If only the width or only the height is 0, we should probably still clip */
4576         rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
4577         SelectClipRgn(graphics->hdc, rgn);
4578     }
4579
4580     /* Use gdi to find the font, then perform transformations on it (height,
4581      * width, angle). */
4582     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
4583     GetTextMetricsW(graphics->hdc, &textmet);
4584     lfw = font->lfw;
4585
4586     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
4587     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
4588
4589     lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
4590
4591     gdifont = CreateFontIndirectW(&lfw);
4592     DeleteObject(SelectObject(graphics->hdc, gdifont));
4593
4594     if (!format || format->align == StringAlignmentNear)
4595     {
4596         args.drawbase.x = corners[0].x;
4597         args.drawbase.y = corners[0].y;
4598         args.drawflags = DT_NOCLIP | DT_EXPANDTABS;
4599     }
4600     else if (format->align == StringAlignmentCenter)
4601     {
4602         args.drawbase.x = (corners[0].x + corners[1].x)/2;
4603         args.drawbase.y = (corners[0].y + corners[1].y)/2;
4604         args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
4605     }
4606     else /* (format->align == StringAlignmentFar) */
4607     {
4608         args.drawbase.x = corners[1].x;
4609         args.drawbase.y = corners[1].y;
4610         args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
4611     }
4612
4613     gdip_format_string(graphics->hdc, string, length, font, &scaled_rect, format,
4614         draw_string_callback, &args);
4615
4616     DeleteObject(rgn);
4617     DeleteObject(gdifont);
4618
4619     RestoreDC(graphics->hdc, save_state);
4620
4621     return Ok;
4622 }
4623
4624 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
4625 {
4626     TRACE("(%p)\n", graphics);
4627
4628     if(!graphics)
4629         return InvalidParameter;
4630
4631     if(graphics->busy)
4632         return ObjectBusy;
4633
4634     return GdipSetInfinite(graphics->clip);
4635 }
4636
4637 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
4638 {
4639     TRACE("(%p)\n", graphics);
4640
4641     if(!graphics)
4642         return InvalidParameter;
4643
4644     if(graphics->busy)
4645         return ObjectBusy;
4646
4647     graphics->worldtrans->matrix[0] = 1.0;
4648     graphics->worldtrans->matrix[1] = 0.0;
4649     graphics->worldtrans->matrix[2] = 0.0;
4650     graphics->worldtrans->matrix[3] = 1.0;
4651     graphics->worldtrans->matrix[4] = 0.0;
4652     graphics->worldtrans->matrix[5] = 0.0;
4653
4654     return Ok;
4655 }
4656
4657 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
4658 {
4659     return GdipEndContainer(graphics, state);
4660 }
4661
4662 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
4663     GpMatrixOrder order)
4664 {
4665     TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
4666
4667     if(!graphics)
4668         return InvalidParameter;
4669
4670     if(graphics->busy)
4671         return ObjectBusy;
4672
4673     return GdipRotateMatrix(graphics->worldtrans, angle, order);
4674 }
4675
4676 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
4677 {
4678     return GdipBeginContainer2(graphics, state);
4679 }
4680
4681 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
4682         GraphicsContainer *state)
4683 {
4684     GraphicsContainerItem *container;
4685     GpStatus sts;
4686
4687     TRACE("(%p, %p)\n", graphics, state);
4688
4689     if(!graphics || !state)
4690         return InvalidParameter;
4691
4692     sts = init_container(&container, graphics);
4693     if(sts != Ok)
4694         return sts;
4695
4696     list_add_head(&graphics->containers, &container->entry);
4697     *state = graphics->contid = container->contid;
4698
4699     return Ok;
4700 }
4701
4702 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
4703 {
4704     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4705     return NotImplemented;
4706 }
4707
4708 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
4709 {
4710     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4711     return NotImplemented;
4712 }
4713
4714 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
4715 {
4716     FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
4717     return NotImplemented;
4718 }
4719
4720 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
4721 {
4722     GpStatus sts;
4723     GraphicsContainerItem *container, *container2;
4724
4725     TRACE("(%p, %x)\n", graphics, state);
4726
4727     if(!graphics)
4728         return InvalidParameter;
4729
4730     LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
4731         if(container->contid == state)
4732             break;
4733     }
4734
4735     /* did not find a matching container */
4736     if(&container->entry == &graphics->containers)
4737         return Ok;
4738
4739     sts = restore_container(graphics, container);
4740     if(sts != Ok)
4741         return sts;
4742
4743     /* remove all of the containers on top of the found container */
4744     LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
4745         if(container->contid == state)
4746             break;
4747         list_remove(&container->entry);
4748         delete_container(container);
4749     }
4750
4751     list_remove(&container->entry);
4752     delete_container(container);
4753
4754     return Ok;
4755 }
4756
4757 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
4758     REAL sy, GpMatrixOrder order)
4759 {
4760     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
4761
4762     if(!graphics)
4763         return InvalidParameter;
4764
4765     if(graphics->busy)
4766         return ObjectBusy;
4767
4768     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
4769 }
4770
4771 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
4772     CombineMode mode)
4773 {
4774     TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
4775
4776     if(!graphics || !srcgraphics)
4777         return InvalidParameter;
4778
4779     return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
4780 }
4781
4782 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
4783     CompositingMode mode)
4784 {
4785     TRACE("(%p, %d)\n", graphics, mode);
4786
4787     if(!graphics)
4788         return InvalidParameter;
4789
4790     if(graphics->busy)
4791         return ObjectBusy;
4792
4793     graphics->compmode = mode;
4794
4795     return Ok;
4796 }
4797
4798 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
4799     CompositingQuality quality)
4800 {
4801     TRACE("(%p, %d)\n", graphics, quality);
4802
4803     if(!graphics)
4804         return InvalidParameter;
4805
4806     if(graphics->busy)
4807         return ObjectBusy;
4808
4809     graphics->compqual = quality;
4810
4811     return Ok;
4812 }
4813
4814 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
4815     InterpolationMode mode)
4816 {
4817     TRACE("(%p, %d)\n", graphics, mode);
4818
4819     if(!graphics || mode == InterpolationModeInvalid || mode > InterpolationModeHighQualityBicubic)
4820         return InvalidParameter;
4821
4822     if(graphics->busy)
4823         return ObjectBusy;
4824
4825     if (mode == InterpolationModeDefault || mode == InterpolationModeLowQuality)
4826         mode = InterpolationModeBilinear;
4827
4828     if (mode == InterpolationModeHighQuality)
4829         mode = InterpolationModeHighQualityBicubic;
4830
4831     graphics->interpolation = mode;
4832
4833     return Ok;
4834 }
4835
4836 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
4837 {
4838     TRACE("(%p, %.2f)\n", graphics, scale);
4839
4840     if(!graphics || (scale <= 0.0))
4841         return InvalidParameter;
4842
4843     if(graphics->busy)
4844         return ObjectBusy;
4845
4846     graphics->scale = scale;
4847
4848     return Ok;
4849 }
4850
4851 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
4852 {
4853     TRACE("(%p, %d)\n", graphics, unit);
4854
4855     if(!graphics)
4856         return InvalidParameter;
4857
4858     if(graphics->busy)
4859         return ObjectBusy;
4860
4861     if(unit == UnitWorld)
4862         return InvalidParameter;
4863
4864     graphics->unit = unit;
4865
4866     return Ok;
4867 }
4868
4869 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4870     mode)
4871 {
4872     TRACE("(%p, %d)\n", graphics, mode);
4873
4874     if(!graphics)
4875         return InvalidParameter;
4876
4877     if(graphics->busy)
4878         return ObjectBusy;
4879
4880     graphics->pixeloffset = mode;
4881
4882     return Ok;
4883 }
4884
4885 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
4886 {
4887     static int calls;
4888
4889     TRACE("(%p,%i,%i)\n", graphics, x, y);
4890
4891     if (!(calls++))
4892         FIXME("not implemented\n");
4893
4894     return NotImplemented;
4895 }
4896
4897 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
4898 {
4899     static int calls;
4900
4901     TRACE("(%p,%p,%p)\n", graphics, x, y);
4902
4903     if (!(calls++))
4904         FIXME("not implemented\n");
4905
4906     *x = *y = 0;
4907
4908     return NotImplemented;
4909 }
4910
4911 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
4912 {
4913     TRACE("(%p, %d)\n", graphics, mode);
4914
4915     if(!graphics)
4916         return InvalidParameter;
4917
4918     if(graphics->busy)
4919         return ObjectBusy;
4920
4921     graphics->smoothing = mode;
4922
4923     return Ok;
4924 }
4925
4926 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
4927 {
4928     TRACE("(%p, %d)\n", graphics, contrast);
4929
4930     if(!graphics)
4931         return InvalidParameter;
4932
4933     graphics->textcontrast = contrast;
4934
4935     return Ok;
4936 }
4937
4938 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
4939     TextRenderingHint hint)
4940 {
4941     TRACE("(%p, %d)\n", graphics, hint);
4942
4943     if(!graphics)
4944         return InvalidParameter;
4945
4946     if(graphics->busy)
4947         return ObjectBusy;
4948
4949     graphics->texthint = hint;
4950
4951     return Ok;
4952 }
4953
4954 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4955 {
4956     TRACE("(%p, %p)\n", graphics, matrix);
4957
4958     if(!graphics || !matrix)
4959         return InvalidParameter;
4960
4961     if(graphics->busy)
4962         return ObjectBusy;
4963
4964     GdipDeleteMatrix(graphics->worldtrans);
4965     return GdipCloneMatrix(matrix, &graphics->worldtrans);
4966 }
4967
4968 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
4969     REAL dy, GpMatrixOrder order)
4970 {
4971     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
4972
4973     if(!graphics)
4974         return InvalidParameter;
4975
4976     if(graphics->busy)
4977         return ObjectBusy;
4978
4979     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
4980 }
4981
4982 /*****************************************************************************
4983  * GdipSetClipHrgn [GDIPLUS.@]
4984  */
4985 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
4986 {
4987     GpRegion *region;
4988     GpStatus status;
4989
4990     TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
4991
4992     if(!graphics)
4993         return InvalidParameter;
4994
4995     status = GdipCreateRegionHrgn(hrgn, &region);
4996     if(status != Ok)
4997         return status;
4998
4999     status = GdipSetClipRegion(graphics, region, mode);
5000
5001     GdipDeleteRegion(region);
5002     return status;
5003 }
5004
5005 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
5006 {
5007     TRACE("(%p, %p, %d)\n", graphics, path, mode);
5008
5009     if(!graphics)
5010         return InvalidParameter;
5011
5012     if(graphics->busy)
5013         return ObjectBusy;
5014
5015     return GdipCombineRegionPath(graphics->clip, path, mode);
5016 }
5017
5018 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
5019                                     REAL width, REAL height,
5020                                     CombineMode mode)
5021 {
5022     GpRectF rect;
5023
5024     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
5025
5026     if(!graphics)
5027         return InvalidParameter;
5028
5029     if(graphics->busy)
5030         return ObjectBusy;
5031
5032     rect.X = x;
5033     rect.Y = y;
5034     rect.Width  = width;
5035     rect.Height = height;
5036
5037     return GdipCombineRegionRect(graphics->clip, &rect, mode);
5038 }
5039
5040 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
5041                                      INT width, INT height,
5042                                      CombineMode mode)
5043 {
5044     TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
5045
5046     if(!graphics)
5047         return InvalidParameter;
5048
5049     if(graphics->busy)
5050         return ObjectBusy;
5051
5052     return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
5053 }
5054
5055 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
5056                                       CombineMode mode)
5057 {
5058     TRACE("(%p, %p, %d)\n", graphics, region, mode);
5059
5060     if(!graphics || !region)
5061         return InvalidParameter;
5062
5063     if(graphics->busy)
5064         return ObjectBusy;
5065
5066     return GdipCombineRegionRegion(graphics->clip, region, mode);
5067 }
5068
5069 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
5070     UINT limitDpi)
5071 {
5072     static int calls;
5073
5074     TRACE("(%p,%u)\n", metafile, limitDpi);
5075
5076     if(!(calls++))
5077         FIXME("not implemented\n");
5078
5079     return NotImplemented;
5080 }
5081
5082 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
5083     INT count)
5084 {
5085     INT save_state;
5086     POINT *pti;
5087
5088     TRACE("(%p, %p, %d)\n", graphics, points, count);
5089
5090     if(!graphics || !pen || count<=0)
5091         return InvalidParameter;
5092
5093     if(graphics->busy)
5094         return ObjectBusy;
5095
5096     if (!graphics->hdc)
5097     {
5098         FIXME("graphics object has no HDC\n");
5099         return Ok;
5100     }
5101
5102     pti = GdipAlloc(sizeof(POINT) * count);
5103
5104     save_state = prepare_dc(graphics, pen);
5105     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
5106
5107     transform_and_round_points(graphics, pti, (GpPointF*)points, count);
5108     Polygon(graphics->hdc, pti, count);
5109
5110     restore_dc(graphics, save_state);
5111     GdipFree(pti);
5112
5113     return Ok;
5114 }
5115
5116 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
5117     INT count)
5118 {
5119     GpStatus ret;
5120     GpPointF *ptf;
5121     INT i;
5122
5123     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
5124
5125     if(count<=0)    return InvalidParameter;
5126     ptf = GdipAlloc(sizeof(GpPointF) * count);
5127
5128     for(i = 0;i < count; i++){
5129         ptf[i].X = (REAL)points[i].X;
5130         ptf[i].Y = (REAL)points[i].Y;
5131     }
5132
5133     ret = GdipDrawPolygon(graphics,pen,ptf,count);
5134     GdipFree(ptf);
5135
5136     return ret;
5137 }
5138
5139 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
5140 {
5141     TRACE("(%p, %p)\n", graphics, dpi);
5142
5143     if(!graphics || !dpi)
5144         return InvalidParameter;
5145
5146     if(graphics->busy)
5147         return ObjectBusy;
5148
5149     if (graphics->image)
5150         *dpi = graphics->image->xres;
5151     else
5152         *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
5153
5154     return Ok;
5155 }
5156
5157 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
5158 {
5159     TRACE("(%p, %p)\n", graphics, dpi);
5160
5161     if(!graphics || !dpi)
5162         return InvalidParameter;
5163
5164     if(graphics->busy)
5165         return ObjectBusy;
5166
5167     if (graphics->image)
5168         *dpi = graphics->image->yres;
5169     else
5170         *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
5171
5172     return Ok;
5173 }
5174
5175 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
5176     GpMatrixOrder order)
5177 {
5178     GpMatrix m;
5179     GpStatus ret;
5180
5181     TRACE("(%p, %p, %d)\n", graphics, matrix, order);
5182
5183     if(!graphics || !matrix)
5184         return InvalidParameter;
5185
5186     if(graphics->busy)
5187         return ObjectBusy;
5188
5189     m = *(graphics->worldtrans);
5190
5191     ret = GdipMultiplyMatrix(&m, matrix, order);
5192     if(ret == Ok)
5193         *(graphics->worldtrans) = m;
5194
5195     return ret;
5196 }
5197
5198 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
5199 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
5200
5201 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
5202 {
5203     TRACE("(%p, %p)\n", graphics, hdc);
5204
5205     if(!graphics || !hdc)
5206         return InvalidParameter;
5207
5208     if(graphics->busy)
5209         return ObjectBusy;
5210
5211     if (!graphics->hdc ||
5212         (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
5213     {
5214         /* Create a fake HDC and fill it with a constant color. */
5215         HDC temp_hdc;
5216         HBITMAP hbitmap;
5217         GpStatus stat;
5218         GpRectF bounds;
5219         BITMAPINFOHEADER bmih;
5220         int i;
5221
5222         stat = get_graphics_bounds(graphics, &bounds);
5223         if (stat != Ok)
5224             return stat;
5225
5226         graphics->temp_hbitmap_width = bounds.Width;
5227         graphics->temp_hbitmap_height = bounds.Height;
5228
5229         bmih.biSize = sizeof(bmih);
5230         bmih.biWidth = graphics->temp_hbitmap_width;
5231         bmih.biHeight = -graphics->temp_hbitmap_height;
5232         bmih.biPlanes = 1;
5233         bmih.biBitCount = 32;
5234         bmih.biCompression = BI_RGB;
5235         bmih.biSizeImage = 0;
5236         bmih.biXPelsPerMeter = 0;
5237         bmih.biYPelsPerMeter = 0;
5238         bmih.biClrUsed = 0;
5239         bmih.biClrImportant = 0;
5240
5241         hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
5242             (void**)&graphics->temp_bits, NULL, 0);
5243         if (!hbitmap)
5244             return GenericError;
5245
5246         temp_hdc = CreateCompatibleDC(0);
5247         if (!temp_hdc)
5248         {
5249             DeleteObject(hbitmap);
5250             return GenericError;
5251         }
5252
5253         for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5254             ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
5255
5256         SelectObject(temp_hdc, hbitmap);
5257
5258         graphics->temp_hbitmap = hbitmap;
5259         *hdc = graphics->temp_hdc = temp_hdc;
5260     }
5261     else
5262     {
5263         *hdc = graphics->hdc;
5264     }
5265
5266     graphics->busy = TRUE;
5267
5268     return Ok;
5269 }
5270
5271 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
5272 {
5273     TRACE("(%p, %p)\n", graphics, hdc);
5274
5275     if(!graphics || !hdc)
5276         return InvalidParameter;
5277
5278     if((graphics->hdc != hdc && graphics->temp_hdc != hdc) || !(graphics->busy))
5279         return InvalidParameter;
5280
5281     if (graphics->temp_hdc == hdc)
5282     {
5283         DWORD* pos;
5284         int i;
5285
5286         /* Find the pixels that have changed, and mark them as opaque. */
5287         pos = (DWORD*)graphics->temp_bits;
5288         for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
5289         {
5290             if (*pos != DC_BACKGROUND_KEY)
5291             {
5292                 *pos |= 0xff000000;
5293             }
5294             pos++;
5295         }
5296
5297         /* Write the changed pixels to the real target. */
5298         alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
5299             graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
5300             graphics->temp_hbitmap_width * 4);
5301
5302         /* Clean up. */
5303         DeleteDC(graphics->temp_hdc);
5304         DeleteObject(graphics->temp_hbitmap);
5305         graphics->temp_hdc = NULL;
5306         graphics->temp_hbitmap = NULL;
5307     }
5308
5309     graphics->busy = FALSE;
5310
5311     return Ok;
5312 }
5313
5314 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
5315 {
5316     GpRegion *clip;
5317     GpStatus status;
5318
5319     TRACE("(%p, %p)\n", graphics, region);
5320
5321     if(!graphics || !region)
5322         return InvalidParameter;
5323
5324     if(graphics->busy)
5325         return ObjectBusy;
5326
5327     if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
5328         return status;
5329
5330     /* free everything except root node and header */
5331     delete_element(&region->node);
5332     memcpy(region, clip, sizeof(GpRegion));
5333     GdipFree(clip);
5334
5335     return Ok;
5336 }
5337
5338 static GpStatus get_graphics_transform(GpGraphics *graphics, GpCoordinateSpace dst_space,
5339         GpCoordinateSpace src_space, GpMatrix **matrix)
5340 {
5341     GpStatus stat = GdipCreateMatrix(matrix);
5342     REAL unitscale;
5343
5344     if (dst_space != src_space && stat == Ok)
5345     {
5346         unitscale = convert_unit(graphics_res(graphics), graphics->unit);
5347
5348         if(graphics->unit != UnitDisplay)
5349             unitscale *= graphics->scale;
5350
5351         /* transform from src_space to CoordinateSpacePage */
5352         switch (src_space)
5353         {
5354         case CoordinateSpaceWorld:
5355             GdipMultiplyMatrix(*matrix, graphics->worldtrans, MatrixOrderAppend);
5356             break;
5357         case CoordinateSpacePage:
5358             break;
5359         case CoordinateSpaceDevice:
5360             GdipScaleMatrix(*matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
5361             break;
5362         }
5363
5364         /* transform from CoordinateSpacePage to dst_space */
5365         switch (dst_space)
5366         {
5367         case CoordinateSpaceWorld:
5368             {
5369                 GpMatrix *inverted_transform;
5370                 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
5371                 if (stat == Ok)
5372                 {
5373                     stat = GdipInvertMatrix(inverted_transform);
5374                     if (stat == Ok)
5375                         GdipMultiplyMatrix(*matrix, inverted_transform, MatrixOrderAppend);
5376                     GdipDeleteMatrix(inverted_transform);
5377                 }
5378                 break;
5379             }
5380         case CoordinateSpacePage:
5381             break;
5382         case CoordinateSpaceDevice:
5383             GdipScaleMatrix(*matrix, unitscale, unitscale, MatrixOrderAppend);
5384             break;
5385         }
5386     }
5387     return stat;
5388 }
5389
5390 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
5391                                         GpCoordinateSpace src_space, GpPointF *points, INT count)
5392 {
5393     GpMatrix *matrix;
5394     GpStatus stat;
5395
5396     if(!graphics || !points || count <= 0)
5397         return InvalidParameter;
5398
5399     if(graphics->busy)
5400         return ObjectBusy;
5401
5402     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5403
5404     if (src_space == dst_space) return Ok;
5405
5406     stat = get_graphics_transform(graphics, dst_space, src_space, &matrix);
5407
5408     if (stat == Ok)
5409     {
5410         stat = GdipTransformMatrixPoints(matrix, points, count);
5411
5412         GdipDeleteMatrix(matrix);
5413     }
5414
5415     return stat;
5416 }
5417
5418 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
5419                                          GpCoordinateSpace src_space, GpPoint *points, INT count)
5420 {
5421     GpPointF *pointsF;
5422     GpStatus ret;
5423     INT i;
5424
5425     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
5426
5427     if(count <= 0)
5428         return InvalidParameter;
5429
5430     pointsF = GdipAlloc(sizeof(GpPointF) * count);
5431     if(!pointsF)
5432         return OutOfMemory;
5433
5434     for(i = 0; i < count; i++){
5435         pointsF[i].X = (REAL)points[i].X;
5436         pointsF[i].Y = (REAL)points[i].Y;
5437     }
5438
5439     ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
5440
5441     if(ret == Ok)
5442         for(i = 0; i < count; i++){
5443             points[i].X = roundr(pointsF[i].X);
5444             points[i].Y = roundr(pointsF[i].Y);
5445         }
5446     GdipFree(pointsF);
5447
5448     return ret;
5449 }
5450
5451 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5452 {
5453     static int calls;
5454
5455     TRACE("\n");
5456
5457     if (!calls++)
5458       FIXME("stub\n");
5459
5460     return NULL;
5461 }
5462
5463 /*****************************************************************************
5464  * GdipTranslateClip [GDIPLUS.@]
5465  */
5466 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5467 {
5468     TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5469
5470     if(!graphics)
5471         return InvalidParameter;
5472
5473     if(graphics->busy)
5474         return ObjectBusy;
5475
5476     return GdipTranslateRegion(graphics->clip, dx, dy);
5477 }
5478
5479 /*****************************************************************************
5480  * GdipTranslateClipI [GDIPLUS.@]
5481  */
5482 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5483 {
5484     TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5485
5486     if(!graphics)
5487         return InvalidParameter;
5488
5489     if(graphics->busy)
5490         return ObjectBusy;
5491
5492     return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5493 }
5494
5495
5496 /*****************************************************************************
5497  * GdipMeasureDriverString [GDIPLUS.@]
5498  */
5499 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5500                                             GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5501                                             INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5502 {
5503     FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5504     return NotImplemented;
5505 }
5506
5507 /*****************************************************************************
5508  * GdipDrawDriverString [GDIPLUS.@]
5509  */
5510 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5511                                          GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
5512                                          GDIPCONST PointF *positions, INT flags,
5513                                          GDIPCONST GpMatrix *matrix )
5514 {
5515     FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
5516     return NotImplemented;
5517 }
5518
5519 GpStatus WINGDIPAPI GdipRecordMetafile(HDC hdc, EmfType type, GDIPCONST GpRectF *frameRect,
5520                                        MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5521 {
5522     FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5523     return NotImplemented;
5524 }
5525
5526 /*****************************************************************************
5527  * GdipRecordMetafileI [GDIPLUS.@]
5528  */
5529 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5530                                         MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5531 {
5532     FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5533     return NotImplemented;
5534 }
5535
5536 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5537                                         MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5538 {
5539     FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
5540     return NotImplemented;
5541 }
5542
5543 /*****************************************************************************
5544  * GdipIsVisibleClipEmpty [GDIPLUS.@]
5545  */
5546 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
5547 {
5548     GpStatus stat;
5549     GpRegion* rgn;
5550
5551     TRACE("(%p, %p)\n", graphics, res);
5552
5553     if((stat = GdipCreateRegion(&rgn)) != Ok)
5554         return stat;
5555
5556     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5557         goto cleanup;
5558
5559     stat = GdipIsEmptyRegion(rgn, graphics, res);
5560
5561 cleanup:
5562     GdipDeleteRegion(rgn);
5563     return stat;
5564 }
5565
5566 GpStatus WINGDIPAPI GdipGetHemfFromMetafile(GpMetafile *metafile, HENHMETAFILE *hEmf)
5567 {
5568     FIXME("(%p,%p): stub\n", metafile, hEmf);
5569
5570     if (!metafile || !hEmf)
5571         return InvalidParameter;
5572
5573     *hEmf = NULL;
5574
5575     return NotImplemented;
5576 }