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