~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/gdiplus/graphics.c

Version: ~ [ wine-1.5.4 ] ~ [ wine-1.5.3 ] ~ [ wine-1.5.2 ] ~ [ wine-1.5.1 ] ~ [ wine-1.5.0 ] ~ [ wine-1.4 ] ~ [ wine-1.4-rc6 ] ~ [ wine-1.4-rc5 ] ~ [ wine-1.4-rc4 ] ~ [ wine-1.4-rc3 ] ~ [ wine-1.4-rc2 ] ~ [ wine-1.4-rc1 ] ~ [ wine-1.3.37 ] ~ [ wine-1.3.36 ] ~ [ wine-1.3.35 ] ~ [ wine-1.3.34 ] ~ [ wine-1.3.33 ] ~ [ wine-1.3.32 ] ~ [ wine-1.3.31 ] ~ [ wine-1.3.30 ] ~ [ wine-1.3.29 ] ~ [ wine-1.3.28 ] ~ [ wine-1.3.27 ] ~ [ wine-1.3.26 ] ~ [ wine-1.3.25 ] ~ [ wine-1.3.24 ] ~ [ wine-1.3.23 ] ~ [ wine-1.3.22 ] ~ [ wine-1.3.21 ] ~ [ wine-1.3.20 ] ~ [ wine-1.3.19 ] ~ [ wine-1.3.18 ] ~ [ wine-1.2.3 ] ~ [ wine-1.3.17 ] ~ [ wine-1.3.16 ] ~ [ wine-1.3.15 ] ~ [ wine-1.3.14 ] ~ [ wine-1.3.13 ] ~ [ wine-1.3.12 ] ~ [ wine-1.3.11 ] ~ [ wine-1.3.10 ] ~ [ wine-1.3.9 ] ~ [ wine-1.2.2 ] ~ [ wine-1.3.8 ] ~ [ wine-1.3.7 ] ~ [ wine-1.3.6 ] ~ [ wine-1.3.5 ] ~ [ wine-1.2.1 ] ~ [ wine-1.3.4 ] ~ [ wine-1.3.3 ] ~ [ wine-1.3.2 ] ~ [ wine-1.3.1 ] ~ [ wine-1.3.0 ] ~ [ wine-1.2 ] ~ [ wine-1.2-rc7 ] ~ [ wine-1.2-rc6 ] ~ [ wine-1.2-rc5 ] ~ [ wine-1.2-rc4 ] ~ [ wine-1.2-rc3 ] ~ [ wine-1.2-rc2 ] ~ [ wine-1.2-rc1 ] ~ [ wine-1.1.44 ] ~ [ wine-1.1.43 ] ~ [ wine-1.1.42 ] ~ [ wine-1.1.41 ] ~ [ wine-1.1.40 ] ~ [ wine-1.1.39 ] ~ [ wine-1.1.38 ] ~ [ wine-1.1.37 ] ~ [ wine-1.1.36 ] ~ [ wine-1.1.35 ] ~ [ wine-1.1.34 ] ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.