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