comctl32/dpa: Some DPA_Merge tests.
[wine] / dlls / gdiplus / graphics.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
28
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
34
35 #include "winreg.h"
36 #include "shlwapi.h"
37
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
44
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
48
49 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
51 {
52     REAL radAngle, hypotenuse;
53
54     radAngle = deg2rad(angle);
55     hypotenuse = 50.0; /* arbitrary */
56
57     *x = x_0 + cos(radAngle) * hypotenuse;
58     *y = y_0 + sin(radAngle) * hypotenuse;
59 }
60
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
63 {
64     BYTE ret;
65
66     switch(type & PathPointTypePathTypeMask){
67         case PathPointTypeBezier:
68             ret = PT_BEZIERTO;
69             break;
70         case PathPointTypeLine:
71             ret = PT_LINETO;
72             break;
73         case PathPointTypeStart:
74             ret = PT_MOVETO;
75             break;
76         default:
77             ERR("Bad point type\n");
78             return 0;
79     }
80
81     if(type & PathPointTypeCloseSubpath)
82         ret |= PT_CLOSEFIGURE;
83
84     return ret;
85 }
86
87 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
88 {
89     HPEN gdipen;
90     REAL width;
91     INT save_state = SaveDC(graphics->hdc), i, numdashes;
92     GpPointF pt[2];
93     DWORD dash_array[MAX_DASHLEN];
94
95     EndPath(graphics->hdc);
96
97     if(pen->unit == UnitPixel){
98         width = pen->width;
99     }
100     else{
101         /* Get an estimate for the amount the pen width is affected by the world
102          * transform. (This is similar to what some of the wine drivers do.) */
103         pt[0].X = 0.0;
104         pt[0].Y = 0.0;
105         pt[1].X = 1.0;
106         pt[1].Y = 1.0;
107         GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
108         width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
109                      (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
110
111         width *= pen->width * convert_unit(graphics->hdc,
112                               pen->unit == UnitWorld ? graphics->unit : pen->unit);
113     }
114
115     if(pen->dash == DashStyleCustom){
116         numdashes = min(pen->numdashes, MAX_DASHLEN);
117
118         TRACE("dashes are: ");
119         for(i = 0; i < numdashes; i++){
120             dash_array[i] = roundr(width * pen->dashes[i]);
121             TRACE("%d, ", dash_array[i]);
122         }
123         TRACE("\n and the pen style is %x\n", pen->style);
124
125         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
126                               numdashes, dash_array);
127     }
128     else
129         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
130
131     SelectObject(graphics->hdc, gdipen);
132
133     return save_state;
134 }
135
136 static void restore_dc(GpGraphics *graphics, INT state)
137 {
138     DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
139     RestoreDC(graphics->hdc, state);
140 }
141
142 /* This helper applies all the changes that the points listed in ptf need in
143  * order to be drawn on the device context.  In the end, this should include at
144  * least:
145  *  -scaling by page unit
146  *  -applying world transformation
147  *  -converting from float to int
148  * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
149  * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
150  * gdi to draw, and these functions would irreparably mess with line widths.
151  */
152 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
153     GpPointF *ptf, INT count)
154 {
155     REAL unitscale;
156     GpMatrix *matrix;
157     int i;
158
159     unitscale = convert_unit(graphics->hdc, graphics->unit);
160
161     /* apply page scale */
162     if(graphics->unit != UnitDisplay)
163         unitscale *= graphics->scale;
164
165     GdipCloneMatrix(graphics->worldtrans, &matrix);
166     GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
167     GdipTransformMatrixPoints(matrix, ptf, count);
168     GdipDeleteMatrix(matrix);
169
170     for(i = 0; i < count; i++){
171         pti[i].x = roundr(ptf[i].X);
172         pti[i].y = roundr(ptf[i].Y);
173     }
174 }
175
176 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
177 {
178     ARGB result=0;
179     ARGB i;
180     for (i=0xff; i<=0xff0000; i = i << 8)
181         result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
182     return result;
183 }
184
185 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
186 {
187     REAL blendfac;
188
189     /* clamp to between 0.0 and 1.0, using the wrap mode */
190     if (brush->wrap == WrapModeTile)
191     {
192         position = fmodf(position, 1.0f);
193         if (position < 0.0f) position += 1.0f;
194     }
195     else /* WrapModeFlip* */
196     {
197         position = fmodf(position, 2.0f);
198         if (position < 0.0f) position += 2.0f;
199         if (position > 1.0f) position = 2.0f - position;
200     }
201
202     if (brush->blendcount == 1)
203         blendfac = position;
204     else
205     {
206         int i=1;
207         REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
208         REAL range;
209
210         /* locate the blend positions surrounding this position */
211         while (position > brush->blendpos[i])
212             i++;
213
214         /* interpolate between the blend positions */
215         left_blendpos = brush->blendpos[i-1];
216         left_blendfac = brush->blendfac[i-1];
217         right_blendpos = brush->blendpos[i];
218         right_blendfac = brush->blendfac[i];
219         range = right_blendpos - left_blendpos;
220         blendfac = (left_blendfac * (right_blendpos - position) +
221                     right_blendfac * (position - left_blendpos)) / range;
222     }
223     return blend_colors(brush->startcolor, brush->endcolor, blendfac);
224 }
225
226 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
227 {
228     switch (brush->bt)
229     {
230     case BrushTypeLinearGradient:
231     {
232         GpLineGradient *line = (GpLineGradient*)brush;
233         RECT rc;
234
235         SelectClipPath(graphics->hdc, RGN_AND);
236         if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
237         {
238             GpPointF endpointsf[2];
239             POINT endpointsi[2];
240             POINT poly[4];
241
242             SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
243
244             endpointsf[0] = line->startpoint;
245             endpointsf[1] = line->endpoint;
246             transform_and_round_points(graphics, endpointsi, endpointsf, 2);
247
248             if (abs(endpointsi[0].x-endpointsi[1].x) > abs(endpointsi[0].y-endpointsi[1].y))
249             {
250                 /* vertical-ish gradient */
251                 int startx, endx; /* x co-ordinates of endpoints shifted to intersect the top of the visible rectangle */
252                 int startbottomx; /* x co-ordinate of start point shifted to intersect the bottom of the visible rectangle */
253                 int width;
254                 COLORREF col;
255                 HBRUSH hbrush, hprevbrush;
256                 int leftx, rightx; /* x co-ordinates where the leftmost and rightmost gradient lines hit the top of the visible rectangle */
257                 int x;
258                 int tilt; /* horizontal distance covered by a gradient line */
259
260                 startx = roundr((rc.top - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
261                 endx = roundr((rc.top - endpointsf[1].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[1].X);
262                 width = endx - startx;
263                 startbottomx = roundr((rc.bottom - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
264                 tilt = startx - startbottomx;
265
266                 if (startx >= startbottomx)
267                 {
268                     leftx = rc.left;
269                     rightx = rc.right + tilt;
270                 }
271                 else
272                 {
273                     leftx = rc.left + tilt;
274                     rightx = rc.right;
275                 }
276
277                 poly[0].y = rc.bottom;
278                 poly[1].y = rc.top;
279                 poly[2].y = rc.top;
280                 poly[3].y = rc.bottom;
281
282                 for (x=leftx; x<=rightx; x++)
283                 {
284                     ARGB argb = blend_line_gradient(line, (x-startx)/(REAL)width);
285                     col = ARGB2COLORREF(argb);
286                     hbrush = CreateSolidBrush(col);
287                     hprevbrush = SelectObject(graphics->hdc, hbrush);
288                     poly[0].x = x - tilt - 1;
289                     poly[1].x = x - 1;
290                     poly[2].x = x;
291                     poly[3].x = x - tilt;
292                     Polygon(graphics->hdc, poly, 4);
293                     SelectObject(graphics->hdc, hprevbrush);
294                     DeleteObject(hbrush);
295                 }
296             }
297             else if (endpointsi[0].y != endpointsi[1].y)
298             {
299                 /* horizontal-ish gradient */
300                 int starty, endy; /* y co-ordinates of endpoints shifted to intersect the left of the visible rectangle */
301                 int startrighty; /* y co-ordinate of start point shifted to intersect the right of the visible rectangle */
302                 int height;
303                 COLORREF col;
304                 HBRUSH hbrush, hprevbrush;
305                 int topy, bottomy; /* y co-ordinates where the topmost and bottommost gradient lines hit the left of the visible rectangle */
306                 int y;
307                 int tilt; /* vertical distance covered by a gradient line */
308
309                 starty = roundr((rc.left - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
310                 endy = roundr((rc.left - endpointsf[1].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[1].Y);
311                 height = endy - starty;
312                 startrighty = roundr((rc.right - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
313                 tilt = starty - startrighty;
314
315                 if (starty >= startrighty)
316                 {
317                     topy = rc.top;
318                     bottomy = rc.bottom + tilt;
319                 }
320                 else
321                 {
322                     topy = rc.top + tilt;
323                     bottomy = rc.bottom;
324                 }
325
326                 poly[0].x = rc.right;
327                 poly[1].x = rc.left;
328                 poly[2].x = rc.left;
329                 poly[3].x = rc.right;
330
331                 for (y=topy; y<=bottomy; y++)
332                 {
333                     ARGB argb = blend_line_gradient(line, (y-starty)/(REAL)height);
334                     col = ARGB2COLORREF(argb);
335                     hbrush = CreateSolidBrush(col);
336                     hprevbrush = SelectObject(graphics->hdc, hbrush);
337                     poly[0].y = y - tilt - 1;
338                     poly[1].y = y - 1;
339                     poly[2].y = y;
340                     poly[3].y = y - tilt;
341                     Polygon(graphics->hdc, poly, 4);
342                     SelectObject(graphics->hdc, hprevbrush);
343                     DeleteObject(hbrush);
344                 }
345             }
346             /* else startpoint == endpoint */
347         }
348         break;
349     }
350     case BrushTypeSolidColor:
351     {
352         GpSolidFill *fill = (GpSolidFill*)brush;
353         if (fill->bmp)
354         {
355             RECT rc;
356             /* partially transparent fill */
357
358             SelectClipPath(graphics->hdc, RGN_AND);
359             if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
360             {
361                 HDC hdc = CreateCompatibleDC(NULL);
362                 HBITMAP oldbmp;
363                 BLENDFUNCTION bf;
364
365                 if (!hdc) break;
366
367                 oldbmp = SelectObject(hdc, fill->bmp);
368
369                 bf.BlendOp = AC_SRC_OVER;
370                 bf.BlendFlags = 0;
371                 bf.SourceConstantAlpha = 255;
372                 bf.AlphaFormat = AC_SRC_ALPHA;
373
374                 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
375
376                 SelectObject(hdc, oldbmp);
377                 DeleteDC(hdc);
378             }
379
380             break;
381         }
382         /* else fall through */
383     }
384     default:
385         SelectObject(graphics->hdc, brush->gdibrush);
386         FillPath(graphics->hdc);
387         break;
388     }
389 }
390
391 /* GdipDrawPie/GdipFillPie helper function */
392 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
393     REAL height, REAL startAngle, REAL sweepAngle)
394 {
395     GpPointF ptf[4];
396     POINT pti[4];
397
398     ptf[0].X = x;
399     ptf[0].Y = y;
400     ptf[1].X = x + width;
401     ptf[1].Y = y + height;
402
403     deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
404     deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
405
406     transform_and_round_points(graphics, pti, ptf, 4);
407
408     Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
409         pti[2].y, pti[3].x, pti[3].y);
410 }
411
412 /* Draws the linecap the specified color and size on the hdc.  The linecap is in
413  * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
414  * should not be called on an hdc that has a path you care about. */
415 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
416     const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
417 {
418     HGDIOBJ oldbrush = NULL, oldpen = NULL;
419     GpMatrix *matrix = NULL;
420     HBRUSH brush = NULL;
421     HPEN pen = NULL;
422     PointF ptf[4], *custptf = NULL;
423     POINT pt[4], *custpt = NULL;
424     BYTE *tp = NULL;
425     REAL theta, dsmall, dbig, dx, dy = 0.0;
426     INT i, count;
427     LOGBRUSH lb;
428     BOOL customstroke;
429
430     if((x1 == x2) && (y1 == y2))
431         return;
432
433     theta = gdiplus_atan2(y2 - y1, x2 - x1);
434
435     customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
436     if(!customstroke){
437         brush = CreateSolidBrush(color);
438         lb.lbStyle = BS_SOLID;
439         lb.lbColor = color;
440         lb.lbHatch = 0;
441         pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
442                            PS_JOIN_MITER, 1, &lb, 0,
443                            NULL);
444         oldbrush = SelectObject(graphics->hdc, brush);
445         oldpen = SelectObject(graphics->hdc, pen);
446     }
447
448     switch(cap){
449         case LineCapFlat:
450             break;
451         case LineCapSquare:
452         case LineCapSquareAnchor:
453         case LineCapDiamondAnchor:
454             size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
455             if(cap == LineCapDiamondAnchor){
456                 dsmall = cos(theta + M_PI_2) * size;
457                 dbig = sin(theta + M_PI_2) * size;
458             }
459             else{
460                 dsmall = cos(theta + M_PI_4) * size;
461                 dbig = sin(theta + M_PI_4) * size;
462             }
463
464             ptf[0].X = x2 - dsmall;
465             ptf[1].X = x2 + dbig;
466
467             ptf[0].Y = y2 - dbig;
468             ptf[3].Y = y2 + dsmall;
469
470             ptf[1].Y = y2 - dsmall;
471             ptf[2].Y = y2 + dbig;
472
473             ptf[3].X = x2 - dbig;
474             ptf[2].X = x2 + dsmall;
475
476             transform_and_round_points(graphics, pt, ptf, 4);
477             Polygon(graphics->hdc, pt, 4);
478
479             break;
480         case LineCapArrowAnchor:
481             size = size * 4.0 / sqrt(3.0);
482
483             dx = cos(M_PI / 6.0 + theta) * size;
484             dy = sin(M_PI / 6.0 + theta) * size;
485
486             ptf[0].X = x2 - dx;
487             ptf[0].Y = y2 - dy;
488
489             dx = cos(- M_PI / 6.0 + theta) * size;
490             dy = sin(- M_PI / 6.0 + theta) * size;
491
492             ptf[1].X = x2 - dx;
493             ptf[1].Y = y2 - dy;
494
495             ptf[2].X = x2;
496             ptf[2].Y = y2;
497
498             transform_and_round_points(graphics, pt, ptf, 3);
499             Polygon(graphics->hdc, pt, 3);
500
501             break;
502         case LineCapRoundAnchor:
503             dx = dy = ANCHOR_WIDTH * size / 2.0;
504
505             ptf[0].X = x2 - dx;
506             ptf[0].Y = y2 - dy;
507             ptf[1].X = x2 + dx;
508             ptf[1].Y = y2 + dy;
509
510             transform_and_round_points(graphics, pt, ptf, 2);
511             Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
512
513             break;
514         case LineCapTriangle:
515             size = size / 2.0;
516             dx = cos(M_PI_2 + theta) * size;
517             dy = sin(M_PI_2 + theta) * size;
518
519             ptf[0].X = x2 - dx;
520             ptf[0].Y = y2 - dy;
521             ptf[1].X = x2 + dx;
522             ptf[1].Y = y2 + dy;
523
524             dx = cos(theta) * size;
525             dy = sin(theta) * size;
526
527             ptf[2].X = x2 + dx;
528             ptf[2].Y = y2 + dy;
529
530             transform_and_round_points(graphics, pt, ptf, 3);
531             Polygon(graphics->hdc, pt, 3);
532
533             break;
534         case LineCapRound:
535             dx = dy = size / 2.0;
536
537             ptf[0].X = x2 - dx;
538             ptf[0].Y = y2 - dy;
539             ptf[1].X = x2 + dx;
540             ptf[1].Y = y2 + dy;
541
542             dx = -cos(M_PI_2 + theta) * size;
543             dy = -sin(M_PI_2 + theta) * size;
544
545             ptf[2].X = x2 - dx;
546             ptf[2].Y = y2 - dy;
547             ptf[3].X = x2 + dx;
548             ptf[3].Y = y2 + dy;
549
550             transform_and_round_points(graphics, pt, ptf, 4);
551             Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
552                 pt[2].y, pt[3].x, pt[3].y);
553
554             break;
555         case LineCapCustom:
556             if(!custom)
557                 break;
558
559             count = custom->pathdata.Count;
560             custptf = GdipAlloc(count * sizeof(PointF));
561             custpt = GdipAlloc(count * sizeof(POINT));
562             tp = GdipAlloc(count);
563
564             if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
565                 goto custend;
566
567             memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
568
569             GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
570             GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
571                              MatrixOrderAppend);
572             GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
573             GdipTransformMatrixPoints(matrix, custptf, count);
574
575             transform_and_round_points(graphics, custpt, custptf, count);
576
577             for(i = 0; i < count; i++)
578                 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
579
580             if(custom->fill){
581                 BeginPath(graphics->hdc);
582                 PolyDraw(graphics->hdc, custpt, tp, count);
583                 EndPath(graphics->hdc);
584                 StrokeAndFillPath(graphics->hdc);
585             }
586             else
587                 PolyDraw(graphics->hdc, custpt, tp, count);
588
589 custend:
590             GdipFree(custptf);
591             GdipFree(custpt);
592             GdipFree(tp);
593             GdipDeleteMatrix(matrix);
594             break;
595         default:
596             break;
597     }
598
599     if(!customstroke){
600         SelectObject(graphics->hdc, oldbrush);
601         SelectObject(graphics->hdc, oldpen);
602         DeleteObject(brush);
603         DeleteObject(pen);
604     }
605 }
606
607 /* Shortens the line by the given percent by changing x2, y2.
608  * If percent is > 1.0 then the line will change direction.
609  * If percent is negative it can lengthen the line. */
610 static void shorten_line_percent(REAL x1, REAL  y1, REAL *x2, REAL *y2, REAL percent)
611 {
612     REAL dist, theta, dx, dy;
613
614     if((y1 == *y2) && (x1 == *x2))
615         return;
616
617     dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
618     theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
619     dx = cos(theta) * dist;
620     dy = sin(theta) * dist;
621
622     *x2 = *x2 + dx;
623     *y2 = *y2 + dy;
624 }
625
626 /* Shortens the line by the given amount by changing x2, y2.
627  * If the amount is greater than the distance, the line will become length 0.
628  * If the amount is negative, it can lengthen the line. */
629 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
630 {
631     REAL dx, dy, percent;
632
633     dx = *x2 - x1;
634     dy = *y2 - y1;
635     if(dx == 0 && dy == 0)
636         return;
637
638     percent = amt / sqrt(dx * dx + dy * dy);
639     if(percent >= 1.0){
640         *x2 = x1;
641         *y2 = y1;
642         return;
643     }
644
645     shorten_line_percent(x1, y1, x2, y2, percent);
646 }
647
648 /* Draws lines between the given points, and if caps is true then draws an endcap
649  * at the end of the last line. */
650 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
651     GDIPCONST GpPointF * pt, INT count, BOOL caps)
652 {
653     POINT *pti = NULL;
654     GpPointF *ptcopy = NULL;
655     GpStatus status = GenericError;
656
657     if(!count)
658         return Ok;
659
660     pti = GdipAlloc(count * sizeof(POINT));
661     ptcopy = GdipAlloc(count * sizeof(GpPointF));
662
663     if(!pti || !ptcopy){
664         status = OutOfMemory;
665         goto end;
666     }
667
668     memcpy(ptcopy, pt, count * sizeof(GpPointF));
669
670     if(caps){
671         if(pen->endcap == LineCapArrowAnchor)
672             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
673                              &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
674         else if((pen->endcap == LineCapCustom) && pen->customend)
675             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
676                              &ptcopy[count-1].X, &ptcopy[count-1].Y,
677                              pen->customend->inset * pen->width);
678
679         if(pen->startcap == LineCapArrowAnchor)
680             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
681                              &ptcopy[0].X, &ptcopy[0].Y, pen->width);
682         else if((pen->startcap == LineCapCustom) && pen->customstart)
683             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
684                              &ptcopy[0].X, &ptcopy[0].Y,
685                              pen->customstart->inset * pen->width);
686
687         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
688                  pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
689         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
690                          pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
691     }
692
693     transform_and_round_points(graphics, pti, ptcopy, count);
694
695     if(Polyline(graphics->hdc, pti, count))
696         status = Ok;
697
698 end:
699     GdipFree(pti);
700     GdipFree(ptcopy);
701
702     return status;
703 }
704
705 /* Conducts a linear search to find the bezier points that will back off
706  * the endpoint of the curve by a distance of amt. Linear search works
707  * better than binary in this case because there are multiple solutions,
708  * and binary searches often find a bad one. I don't think this is what
709  * Windows does but short of rendering the bezier without GDI's help it's
710  * the best we can do. If rev then work from the start of the passed points
711  * instead of the end. */
712 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
713 {
714     GpPointF origpt[4];
715     REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
716     INT i, first = 0, second = 1, third = 2, fourth = 3;
717
718     if(rev){
719         first = 3;
720         second = 2;
721         third = 1;
722         fourth = 0;
723     }
724
725     origx = pt[fourth].X;
726     origy = pt[fourth].Y;
727     memcpy(origpt, pt, sizeof(GpPointF) * 4);
728
729     for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
730         /* reset bezier points to original values */
731         memcpy(pt, origpt, sizeof(GpPointF) * 4);
732         /* Perform magic on bezier points. Order is important here.*/
733         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
734         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
735         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
736         shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
737         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
738         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
739
740         dx = pt[fourth].X - origx;
741         dy = pt[fourth].Y - origy;
742
743         diff = sqrt(dx * dx + dy * dy);
744         percent += 0.0005 * amt;
745     }
746 }
747
748 /* Draws bezier curves between given points, and if caps is true then draws an
749  * endcap at the end of the last line. */
750 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
751     GDIPCONST GpPointF * pt, INT count, BOOL caps)
752 {
753     POINT *pti;
754     GpPointF *ptcopy;
755     GpStatus status = GenericError;
756
757     if(!count)
758         return Ok;
759
760     pti = GdipAlloc(count * sizeof(POINT));
761     ptcopy = GdipAlloc(count * sizeof(GpPointF));
762
763     if(!pti || !ptcopy){
764         status = OutOfMemory;
765         goto end;
766     }
767
768     memcpy(ptcopy, pt, count * sizeof(GpPointF));
769
770     if(caps){
771         if(pen->endcap == LineCapArrowAnchor)
772             shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
773         else if((pen->endcap == LineCapCustom) && pen->customend)
774             shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
775                                FALSE);
776
777         if(pen->startcap == LineCapArrowAnchor)
778             shorten_bezier_amt(ptcopy, pen->width, TRUE);
779         else if((pen->startcap == LineCapCustom) && pen->customstart)
780             shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
781
782         /* the direction of the line cap is parallel to the direction at the
783          * end of the bezier (which, if it has been shortened, is not the same
784          * as the direction from pt[count-2] to pt[count-1]) */
785         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
786             pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
787             pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
788             pt[count - 1].X, pt[count - 1].Y);
789
790         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
791             pt[0].X - (ptcopy[0].X - ptcopy[1].X),
792             pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
793     }
794
795     transform_and_round_points(graphics, pti, ptcopy, count);
796
797     PolyBezier(graphics->hdc, pti, count);
798
799     status = Ok;
800
801 end:
802     GdipFree(pti);
803     GdipFree(ptcopy);
804
805     return status;
806 }
807
808 /* Draws a combination of bezier curves and lines between points. */
809 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
810     GDIPCONST BYTE * types, INT count, BOOL caps)
811 {
812     POINT *pti = GdipAlloc(count * sizeof(POINT));
813     BYTE *tp = GdipAlloc(count);
814     GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
815     INT i, j;
816     GpStatus status = GenericError;
817
818     if(!count){
819         status = Ok;
820         goto end;
821     }
822     if(!pti || !tp || !ptcopy){
823         status = OutOfMemory;
824         goto end;
825     }
826
827     for(i = 1; i < count; i++){
828         if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
829             if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
830                 || !(types[i + 1] & PathPointTypeBezier)){
831                 ERR("Bad bezier points\n");
832                 goto end;
833             }
834             i += 2;
835         }
836     }
837
838     memcpy(ptcopy, pt, count * sizeof(GpPointF));
839
840     /* If we are drawing caps, go through the points and adjust them accordingly,
841      * and draw the caps. */
842     if(caps){
843         switch(types[count - 1] & PathPointTypePathTypeMask){
844             case PathPointTypeBezier:
845                 if(pen->endcap == LineCapArrowAnchor)
846                     shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
847                 else if((pen->endcap == LineCapCustom) && pen->customend)
848                     shorten_bezier_amt(&ptcopy[count - 4],
849                                        pen->width * pen->customend->inset, FALSE);
850
851                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
852                     pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
853                     pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
854                     pt[count - 1].X, pt[count - 1].Y);
855
856                 break;
857             case PathPointTypeLine:
858                 if(pen->endcap == LineCapArrowAnchor)
859                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
860                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
861                                      pen->width);
862                 else if((pen->endcap == LineCapCustom) && pen->customend)
863                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
864                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
865                                      pen->customend->inset * pen->width);
866
867                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
868                          pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
869                          pt[count - 1].Y);
870
871                 break;
872             default:
873                 ERR("Bad path last point\n");
874                 goto end;
875         }
876
877         /* Find start of points */
878         for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
879             == PathPointTypeStart); j++);
880
881         switch(types[j] & PathPointTypePathTypeMask){
882             case PathPointTypeBezier:
883                 if(pen->startcap == LineCapArrowAnchor)
884                     shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
885                 else if((pen->startcap == LineCapCustom) && pen->customstart)
886                     shorten_bezier_amt(&ptcopy[j - 1],
887                                        pen->width * pen->customstart->inset, TRUE);
888
889                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
890                     pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
891                     pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
892                     pt[j - 1].X, pt[j - 1].Y);
893
894                 break;
895             case PathPointTypeLine:
896                 if(pen->startcap == LineCapArrowAnchor)
897                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
898                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
899                                      pen->width);
900                 else if((pen->startcap == LineCapCustom) && pen->customstart)
901                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
902                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
903                                      pen->customstart->inset * pen->width);
904
905                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
906                          pt[j].X, pt[j].Y, pt[j - 1].X,
907                          pt[j - 1].Y);
908
909                 break;
910             default:
911                 ERR("Bad path points\n");
912                 goto end;
913         }
914     }
915
916     transform_and_round_points(graphics, pti, ptcopy, count);
917
918     for(i = 0; i < count; i++){
919         tp[i] = convert_path_point_type(types[i]);
920     }
921
922     PolyDraw(graphics->hdc, pti, tp, count);
923
924     status = Ok;
925
926 end:
927     GdipFree(pti);
928     GdipFree(ptcopy);
929     GdipFree(tp);
930
931     return status;
932 }
933
934 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
935 {
936     GpStatus result;
937
938     BeginPath(graphics->hdc);
939     result = draw_poly(graphics, NULL, path->pathdata.Points,
940                        path->pathdata.Types, path->pathdata.Count, FALSE);
941     EndPath(graphics->hdc);
942     return result;
943 }
944
945 typedef struct _GraphicsContainerItem {
946     struct list entry;
947     GraphicsContainer contid;
948
949     SmoothingMode smoothing;
950     CompositingQuality compqual;
951     InterpolationMode interpolation;
952     CompositingMode compmode;
953     TextRenderingHint texthint;
954     REAL scale;
955     GpUnit unit;
956     PixelOffsetMode pixeloffset;
957     UINT textcontrast;
958     GpMatrix* worldtrans;
959     GpRegion* clip;
960 } GraphicsContainerItem;
961
962 static GpStatus init_container(GraphicsContainerItem** container,
963         GDIPCONST GpGraphics* graphics){
964     GpStatus sts;
965
966     *container = GdipAlloc(sizeof(GraphicsContainerItem));
967     if(!(*container))
968         return OutOfMemory;
969
970     (*container)->contid = graphics->contid + 1;
971
972     (*container)->smoothing = graphics->smoothing;
973     (*container)->compqual = graphics->compqual;
974     (*container)->interpolation = graphics->interpolation;
975     (*container)->compmode = graphics->compmode;
976     (*container)->texthint = graphics->texthint;
977     (*container)->scale = graphics->scale;
978     (*container)->unit = graphics->unit;
979     (*container)->textcontrast = graphics->textcontrast;
980     (*container)->pixeloffset = graphics->pixeloffset;
981
982     sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
983     if(sts != Ok){
984         GdipFree(*container);
985         *container = NULL;
986         return sts;
987     }
988
989     sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
990     if(sts != Ok){
991         GdipDeleteMatrix((*container)->worldtrans);
992         GdipFree(*container);
993         *container = NULL;
994         return sts;
995     }
996
997     return Ok;
998 }
999
1000 static void delete_container(GraphicsContainerItem* container){
1001     GdipDeleteMatrix(container->worldtrans);
1002     GdipDeleteRegion(container->clip);
1003     GdipFree(container);
1004 }
1005
1006 static GpStatus restore_container(GpGraphics* graphics,
1007         GDIPCONST GraphicsContainerItem* container){
1008     GpStatus sts;
1009     GpMatrix *newTrans;
1010     GpRegion *newClip;
1011
1012     sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1013     if(sts != Ok)
1014         return sts;
1015
1016     sts = GdipCloneRegion(container->clip, &newClip);
1017     if(sts != Ok){
1018         GdipDeleteMatrix(newTrans);
1019         return sts;
1020     }
1021
1022     GdipDeleteMatrix(graphics->worldtrans);
1023     graphics->worldtrans = newTrans;
1024
1025     GdipDeleteRegion(graphics->clip);
1026     graphics->clip = newClip;
1027
1028     graphics->contid = container->contid - 1;
1029
1030     graphics->smoothing = container->smoothing;
1031     graphics->compqual = container->compqual;
1032     graphics->interpolation = container->interpolation;
1033     graphics->compmode = container->compmode;
1034     graphics->texthint = container->texthint;
1035     graphics->scale = container->scale;
1036     graphics->unit = container->unit;
1037     graphics->textcontrast = container->textcontrast;
1038     graphics->pixeloffset = container->pixeloffset;
1039
1040     return Ok;
1041 }
1042
1043 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1044 {
1045     RECT wnd_rect;
1046
1047     if(graphics->hwnd) {
1048         if(!GetClientRect(graphics->hwnd, &wnd_rect))
1049             return GenericError;
1050
1051         rect->X = wnd_rect.left;
1052         rect->Y = wnd_rect.top;
1053         rect->Width = wnd_rect.right - wnd_rect.left;
1054         rect->Height = wnd_rect.bottom - wnd_rect.top;
1055     }else{
1056         rect->X = 0;
1057         rect->Y = 0;
1058         rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1059         rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1060     }
1061
1062     return Ok;
1063 }
1064
1065 /* on success, rgn will contain the region of the graphics object which
1066  * is visible after clipping has been applied */
1067 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
1068 {
1069     GpStatus stat;
1070     GpRectF rectf;
1071     GpRegion* tmp;
1072
1073     if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1074         return stat;
1075
1076     if((stat = GdipCreateRegion(&tmp)) != Ok)
1077         return stat;
1078
1079     if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1080         goto end;
1081
1082     if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1083         goto end;
1084
1085     stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1086
1087 end:
1088     GdipDeleteRegion(tmp);
1089     return stat;
1090 }
1091
1092 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1093 {
1094     TRACE("(%p, %p)\n", hdc, graphics);
1095
1096     return GdipCreateFromHDC2(hdc, NULL, graphics);
1097 }
1098
1099 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1100 {
1101     GpStatus retval;
1102
1103     TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1104
1105     if(hDevice != NULL) {
1106         FIXME("Don't know how to handle parameter hDevice\n");
1107         return NotImplemented;
1108     }
1109
1110     if(hdc == NULL)
1111         return OutOfMemory;
1112
1113     if(graphics == NULL)
1114         return InvalidParameter;
1115
1116     *graphics = GdipAlloc(sizeof(GpGraphics));
1117     if(!*graphics)  return OutOfMemory;
1118
1119     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1120         GdipFree(*graphics);
1121         return retval;
1122     }
1123
1124     if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1125         GdipFree((*graphics)->worldtrans);
1126         GdipFree(*graphics);
1127         return retval;
1128     }
1129
1130     (*graphics)->hdc = hdc;
1131     (*graphics)->hwnd = WindowFromDC(hdc);
1132     (*graphics)->owndc = FALSE;
1133     (*graphics)->smoothing = SmoothingModeDefault;
1134     (*graphics)->compqual = CompositingQualityDefault;
1135     (*graphics)->interpolation = InterpolationModeDefault;
1136     (*graphics)->pixeloffset = PixelOffsetModeDefault;
1137     (*graphics)->compmode = CompositingModeSourceOver;
1138     (*graphics)->unit = UnitDisplay;
1139     (*graphics)->scale = 1.0;
1140     (*graphics)->busy = FALSE;
1141     (*graphics)->textcontrast = 4;
1142     list_init(&(*graphics)->containers);
1143     (*graphics)->contid = 0;
1144
1145     return Ok;
1146 }
1147
1148 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1149 {
1150     GpStatus ret;
1151     HDC hdc;
1152
1153     TRACE("(%p, %p)\n", hwnd, graphics);
1154
1155     hdc = GetDC(hwnd);
1156
1157     if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1158     {
1159         ReleaseDC(hwnd, hdc);
1160         return ret;
1161     }
1162
1163     (*graphics)->hwnd = hwnd;
1164     (*graphics)->owndc = TRUE;
1165
1166     return Ok;
1167 }
1168
1169 /* FIXME: no icm handling */
1170 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1171 {
1172     TRACE("(%p, %p)\n", hwnd, graphics);
1173
1174     return GdipCreateFromHWND(hwnd, graphics);
1175 }
1176
1177 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1178     GpMetafile **metafile)
1179 {
1180     static int calls;
1181
1182     if(!hemf || !metafile)
1183         return InvalidParameter;
1184
1185     if(!(calls++))
1186         FIXME("not implemented\n");
1187
1188     return NotImplemented;
1189 }
1190
1191 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1192     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1193 {
1194     IStream *stream = NULL;
1195     UINT read;
1196     BYTE* copy;
1197     HENHMETAFILE hemf;
1198     GpStatus retval = GenericError;
1199
1200     TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1201
1202     if(!hwmf || !metafile || !placeable)
1203         return InvalidParameter;
1204
1205     *metafile = NULL;
1206     read = GetMetaFileBitsEx(hwmf, 0, NULL);
1207     if(!read)
1208         return GenericError;
1209     copy = GdipAlloc(read);
1210     GetMetaFileBitsEx(hwmf, read, copy);
1211
1212     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1213     GdipFree(copy);
1214
1215     read = GetEnhMetaFileBits(hemf, 0, NULL);
1216     copy = GdipAlloc(read);
1217     GetEnhMetaFileBits(hemf, read, copy);
1218     DeleteEnhMetaFile(hemf);
1219
1220     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1221         ERR("could not make stream\n");
1222         GdipFree(copy);
1223         goto err;
1224     }
1225
1226     *metafile = GdipAlloc(sizeof(GpMetafile));
1227     if(!*metafile){
1228         retval = OutOfMemory;
1229         goto err;
1230     }
1231
1232     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1233         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1234         goto err;
1235
1236
1237     (*metafile)->image.type = ImageTypeMetafile;
1238     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1239     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
1240     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1241                     - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
1242     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1243                    - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
1244     (*metafile)->unit = UnitInch;
1245
1246     if(delete)
1247         DeleteMetaFile(hwmf);
1248
1249     return Ok;
1250
1251 err:
1252     GdipFree(*metafile);
1253     IStream_Release(stream);
1254     return retval;
1255 }
1256
1257 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1258     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1259 {
1260     HMETAFILE hmf = GetMetaFileW(file);
1261
1262     TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1263
1264     if(!hmf) return InvalidParameter;
1265
1266     return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1267 }
1268
1269 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1270     GpMetafile **metafile)
1271 {
1272     FIXME("(%p, %p): stub\n", file, metafile);
1273     return NotImplemented;
1274 }
1275
1276 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1277     GpMetafile **metafile)
1278 {
1279     FIXME("(%p, %p): stub\n", stream, metafile);
1280     return NotImplemented;
1281 }
1282
1283 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1284     UINT access, IStream **stream)
1285 {
1286     DWORD dwMode;
1287     HRESULT ret;
1288
1289     TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1290
1291     if(!stream || !filename)
1292         return InvalidParameter;
1293
1294     if(access & GENERIC_WRITE)
1295         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1296     else if(access & GENERIC_READ)
1297         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1298     else
1299         return InvalidParameter;
1300
1301     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1302
1303     return hresult_to_status(ret);
1304 }
1305
1306 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1307 {
1308     GraphicsContainerItem *cont, *next;
1309     TRACE("(%p)\n", graphics);
1310
1311     if(!graphics) return InvalidParameter;
1312     if(graphics->busy) return ObjectBusy;
1313
1314     if(graphics->owndc)
1315         ReleaseDC(graphics->hwnd, graphics->hdc);
1316
1317     LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1318         list_remove(&cont->entry);
1319         delete_container(cont);
1320     }
1321
1322     GdipDeleteRegion(graphics->clip);
1323     GdipDeleteMatrix(graphics->worldtrans);
1324     GdipFree(graphics);
1325
1326     return Ok;
1327 }
1328
1329 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1330     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1331 {
1332     INT save_state, num_pts;
1333     GpPointF points[MAX_ARC_PTS];
1334     GpStatus retval;
1335
1336     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1337           width, height, startAngle, sweepAngle);
1338
1339     if(!graphics || !pen || width <= 0 || height <= 0)
1340         return InvalidParameter;
1341
1342     if(graphics->busy)
1343         return ObjectBusy;
1344
1345     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1346
1347     save_state = prepare_dc(graphics, pen);
1348
1349     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1350
1351     restore_dc(graphics, save_state);
1352
1353     return retval;
1354 }
1355
1356 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1357     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1358 {
1359     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1360           width, height, startAngle, sweepAngle);
1361
1362     return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1363 }
1364
1365 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1366     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1367 {
1368     INT save_state;
1369     GpPointF pt[4];
1370     GpStatus retval;
1371
1372     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1373           x2, y2, x3, y3, x4, y4);
1374
1375     if(!graphics || !pen)
1376         return InvalidParameter;
1377
1378     if(graphics->busy)
1379         return ObjectBusy;
1380
1381     pt[0].X = x1;
1382     pt[0].Y = y1;
1383     pt[1].X = x2;
1384     pt[1].Y = y2;
1385     pt[2].X = x3;
1386     pt[2].Y = y3;
1387     pt[3].X = x4;
1388     pt[3].Y = y4;
1389
1390     save_state = prepare_dc(graphics, pen);
1391
1392     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1393
1394     restore_dc(graphics, save_state);
1395
1396     return retval;
1397 }
1398
1399 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1400     INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1401 {
1402     INT save_state;
1403     GpPointF pt[4];
1404     GpStatus retval;
1405
1406     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1407           x2, y2, x3, y3, x4, y4);
1408
1409     if(!graphics || !pen)
1410         return InvalidParameter;
1411
1412     if(graphics->busy)
1413         return ObjectBusy;
1414
1415     pt[0].X = x1;
1416     pt[0].Y = y1;
1417     pt[1].X = x2;
1418     pt[1].Y = y2;
1419     pt[2].X = x3;
1420     pt[2].Y = y3;
1421     pt[3].X = x4;
1422     pt[3].Y = y4;
1423
1424     save_state = prepare_dc(graphics, pen);
1425
1426     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1427
1428     restore_dc(graphics, save_state);
1429
1430     return retval;
1431 }
1432
1433 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1434     GDIPCONST GpPointF *points, INT count)
1435 {
1436     INT i;
1437     GpStatus ret;
1438
1439     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1440
1441     if(!graphics || !pen || !points || (count <= 0))
1442         return InvalidParameter;
1443
1444     if(graphics->busy)
1445         return ObjectBusy;
1446
1447     for(i = 0; i < floor(count / 4); i++){
1448         ret = GdipDrawBezier(graphics, pen,
1449                              points[4*i].X, points[4*i].Y,
1450                              points[4*i + 1].X, points[4*i + 1].Y,
1451                              points[4*i + 2].X, points[4*i + 2].Y,
1452                              points[4*i + 3].X, points[4*i + 3].Y);
1453         if(ret != Ok)
1454             return ret;
1455     }
1456
1457     return Ok;
1458 }
1459
1460 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1461     GDIPCONST GpPoint *points, INT count)
1462 {
1463     GpPointF *pts;
1464     GpStatus ret;
1465     INT i;
1466
1467     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1468
1469     if(!graphics || !pen || !points || (count <= 0))
1470         return InvalidParameter;
1471
1472     if(graphics->busy)
1473         return ObjectBusy;
1474
1475     pts = GdipAlloc(sizeof(GpPointF) * count);
1476     if(!pts)
1477         return OutOfMemory;
1478
1479     for(i = 0; i < count; i++){
1480         pts[i].X = (REAL)points[i].X;
1481         pts[i].Y = (REAL)points[i].Y;
1482     }
1483
1484     ret = GdipDrawBeziers(graphics,pen,pts,count);
1485
1486     GdipFree(pts);
1487
1488     return ret;
1489 }
1490
1491 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1492     GDIPCONST GpPointF *points, INT count)
1493 {
1494     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1495
1496     return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1497 }
1498
1499 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1500     GDIPCONST GpPoint *points, INT count)
1501 {
1502     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1503
1504     return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1505 }
1506
1507 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1508     GDIPCONST GpPointF *points, INT count, REAL tension)
1509 {
1510     GpPath *path;
1511     GpStatus stat;
1512
1513     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1514
1515     if(!graphics || !pen || !points || count <= 0)
1516         return InvalidParameter;
1517
1518     if(graphics->busy)
1519         return ObjectBusy;
1520
1521     if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1522         return stat;
1523
1524     stat = GdipAddPathClosedCurve2(path, points, count, tension);
1525     if(stat != Ok){
1526         GdipDeletePath(path);
1527         return stat;
1528     }
1529
1530     stat = GdipDrawPath(graphics, pen, path);
1531
1532     GdipDeletePath(path);
1533
1534     return stat;
1535 }
1536
1537 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1538     GDIPCONST GpPoint *points, INT count, REAL tension)
1539 {
1540     GpPointF *ptf;
1541     GpStatus stat;
1542     INT i;
1543
1544     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1545
1546     if(!points || count <= 0)
1547         return InvalidParameter;
1548
1549     ptf = GdipAlloc(sizeof(GpPointF)*count);
1550     if(!ptf)
1551         return OutOfMemory;
1552
1553     for(i = 0; i < count; i++){
1554         ptf[i].X = (REAL)points[i].X;
1555         ptf[i].Y = (REAL)points[i].Y;
1556     }
1557
1558     stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1559
1560     GdipFree(ptf);
1561
1562     return stat;
1563 }
1564
1565 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1566     GDIPCONST GpPointF *points, INT count)
1567 {
1568     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1569
1570     return GdipDrawCurve2(graphics,pen,points,count,1.0);
1571 }
1572
1573 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1574     GDIPCONST GpPoint *points, INT count)
1575 {
1576     GpPointF *pointsF;
1577     GpStatus ret;
1578     INT i;
1579
1580     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1581
1582     if(!points)
1583         return InvalidParameter;
1584
1585     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1586     if(!pointsF)
1587         return OutOfMemory;
1588
1589     for(i = 0; i < count; i++){
1590         pointsF[i].X = (REAL)points[i].X;
1591         pointsF[i].Y = (REAL)points[i].Y;
1592     }
1593
1594     ret = GdipDrawCurve(graphics,pen,pointsF,count);
1595     GdipFree(pointsF);
1596
1597     return ret;
1598 }
1599
1600 /* Approximates cardinal spline with Bezier curves. */
1601 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1602     GDIPCONST GpPointF *points, INT count, REAL tension)
1603 {
1604     /* PolyBezier expects count*3-2 points. */
1605     INT i, len_pt = count*3-2, save_state;
1606     GpPointF *pt;
1607     REAL x1, x2, y1, y2;
1608     GpStatus retval;
1609
1610     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1611
1612     if(!graphics || !pen)
1613         return InvalidParameter;
1614
1615     if(graphics->busy)
1616         return ObjectBusy;
1617
1618     if(count < 2)
1619         return InvalidParameter;
1620
1621     pt = GdipAlloc(len_pt * sizeof(GpPointF));
1622     if(!pt)
1623         return OutOfMemory;
1624
1625     tension = tension * TENSION_CONST;
1626
1627     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1628         tension, &x1, &y1);
1629
1630     pt[0].X = points[0].X;
1631     pt[0].Y = points[0].Y;
1632     pt[1].X = x1;
1633     pt[1].Y = y1;
1634
1635     for(i = 0; i < count-2; i++){
1636         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1637
1638         pt[3*i+2].X = x1;
1639         pt[3*i+2].Y = y1;
1640         pt[3*i+3].X = points[i+1].X;
1641         pt[3*i+3].Y = points[i+1].Y;
1642         pt[3*i+4].X = x2;
1643         pt[3*i+4].Y = y2;
1644     }
1645
1646     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1647         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1648
1649     pt[len_pt-2].X = x1;
1650     pt[len_pt-2].Y = y1;
1651     pt[len_pt-1].X = points[count-1].X;
1652     pt[len_pt-1].Y = points[count-1].Y;
1653
1654     save_state = prepare_dc(graphics, pen);
1655
1656     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1657
1658     GdipFree(pt);
1659     restore_dc(graphics, save_state);
1660
1661     return retval;
1662 }
1663
1664 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1665     GDIPCONST GpPoint *points, INT count, REAL tension)
1666 {
1667     GpPointF *pointsF;
1668     GpStatus ret;
1669     INT i;
1670
1671     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1672
1673     if(!points)
1674         return InvalidParameter;
1675
1676     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1677     if(!pointsF)
1678         return OutOfMemory;
1679
1680     for(i = 0; i < count; i++){
1681         pointsF[i].X = (REAL)points[i].X;
1682         pointsF[i].Y = (REAL)points[i].Y;
1683     }
1684
1685     ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1686     GdipFree(pointsF);
1687
1688     return ret;
1689 }
1690
1691 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
1692     GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
1693     REAL tension)
1694 {
1695     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1696
1697     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1698         return InvalidParameter;
1699     }
1700
1701     return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
1702 }
1703
1704 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
1705     GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
1706     REAL tension)
1707 {
1708     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1709
1710     if(count < 0){
1711         return OutOfMemory;
1712     }
1713
1714     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1715         return InvalidParameter;
1716     }
1717
1718     return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
1719 }
1720
1721 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1722     REAL y, REAL width, REAL height)
1723 {
1724     INT save_state;
1725     GpPointF ptf[2];
1726     POINT pti[2];
1727
1728     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1729
1730     if(!graphics || !pen)
1731         return InvalidParameter;
1732
1733     if(graphics->busy)
1734         return ObjectBusy;
1735
1736     ptf[0].X = x;
1737     ptf[0].Y = y;
1738     ptf[1].X = x + width;
1739     ptf[1].Y = y + height;
1740
1741     save_state = prepare_dc(graphics, pen);
1742     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1743
1744     transform_and_round_points(graphics, pti, ptf, 2);
1745
1746     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1747
1748     restore_dc(graphics, save_state);
1749
1750     return Ok;
1751 }
1752
1753 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1754     INT y, INT width, INT height)
1755 {
1756     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1757
1758     return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1759 }
1760
1761
1762 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1763 {
1764     UINT width, height;
1765     GpPointF points[3];
1766
1767     TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1768
1769     if(!graphics || !image)
1770         return InvalidParameter;
1771
1772     GdipGetImageWidth(image, &width);
1773     GdipGetImageHeight(image, &height);
1774
1775     /* FIXME: we should use the graphics and image dpi, somehow */
1776
1777     points[0].X = points[2].X = x;
1778     points[0].Y = points[1].Y = y;
1779     points[1].X = x + width;
1780     points[2].Y = y + height;
1781
1782     return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
1783         UnitPixel, NULL, NULL, NULL);
1784 }
1785
1786 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1787     INT y)
1788 {
1789     TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1790
1791     return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
1792 }
1793
1794 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
1795     REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
1796     GpUnit srcUnit)
1797 {
1798     GpPointF points[3];
1799     TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1800
1801     points[0].X = points[2].X = x;
1802     points[0].Y = points[1].Y = y;
1803
1804     /* FIXME: convert image coordinates to Graphics coordinates? */
1805     points[1].X = x + srcwidth;
1806     points[2].Y = y + srcheight;
1807
1808     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1809         srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
1810 }
1811
1812 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
1813     INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
1814     GpUnit srcUnit)
1815 {
1816     return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1817 }
1818
1819 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
1820     GDIPCONST GpPointF *dstpoints, INT count)
1821 {
1822     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1823     return NotImplemented;
1824 }
1825
1826 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
1827     GDIPCONST GpPoint *dstpoints, INT count)
1828 {
1829     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1830     return NotImplemented;
1831 }
1832
1833 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1834 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1835      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1836      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1837      DrawImageAbort callback, VOID * callbackData)
1838 {
1839     GpPointF ptf[3];
1840     POINT pti[3];
1841     REAL dx, dy;
1842
1843     TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
1844           count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1845           callbackData);
1846
1847     if(!graphics || !image || !points || count != 3)
1848          return InvalidParameter;
1849
1850     memcpy(ptf, points, 3 * sizeof(GpPointF));
1851     transform_and_round_points(graphics, pti, ptf, 3);
1852
1853     if (image->picture)
1854     {
1855         if(srcUnit == UnitInch)
1856             dx = dy = (REAL) INCH_HIMETRIC;
1857         else if(srcUnit == UnitPixel){
1858             dx = ((REAL) INCH_HIMETRIC) /
1859                  ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1860             dy = ((REAL) INCH_HIMETRIC) /
1861                  ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1862         }
1863         else
1864             return NotImplemented;
1865
1866         if(IPicture_Render(image->picture, graphics->hdc,
1867             pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1868             srcx * dx, srcy * dy,
1869             srcwidth * dx, srcheight * dy,
1870             NULL) != S_OK){
1871             if(callback)
1872                 callback(callbackData);
1873             return GenericError;
1874         }
1875     }
1876     else if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
1877     {
1878         HDC hdc;
1879         GpBitmap* bitmap = (GpBitmap*)image;
1880         int temp_hdc=0, temp_bitmap=0;
1881         HBITMAP hbitmap, old_hbm=NULL;
1882
1883         if (srcUnit == UnitInch)
1884             dx = dy = 96.0; /* FIXME: use the image resolution */
1885         else if (srcUnit == UnitPixel)
1886             dx = dy = 1.0;
1887         else
1888             return NotImplemented;
1889
1890         if (bitmap->format == PixelFormat32bppARGB)
1891         {
1892             BITMAPINFOHEADER bih;
1893             BYTE *temp_bits;
1894
1895             /* we need a bitmap with premultiplied alpha */
1896             hdc = CreateCompatibleDC(0);
1897             temp_hdc = 1;
1898             temp_bitmap = 1;
1899
1900             bih.biSize = sizeof(BITMAPINFOHEADER);
1901             bih.biWidth = bitmap->width;
1902             bih.biHeight = -bitmap->height;
1903             bih.biPlanes = 1;
1904             bih.biBitCount = 32;
1905             bih.biCompression = BI_RGB;
1906             bih.biSizeImage = 0;
1907             bih.biXPelsPerMeter = 0;
1908             bih.biYPelsPerMeter = 0;
1909             bih.biClrUsed = 0;
1910             bih.biClrImportant = 0;
1911
1912             hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
1913                 (void**)&temp_bits, NULL, 0);
1914
1915             convert_32bppARGB_to_32bppPARGB(bitmap->width, bitmap->height,
1916                 temp_bits, bitmap->width*4, bitmap->bits, bitmap->stride);
1917         }
1918         else
1919         {
1920             hbitmap = bitmap->hbitmap;
1921             hdc = bitmap->hdc;
1922             temp_hdc = (hdc == 0);
1923         }
1924
1925         if (temp_hdc)
1926         {
1927             if (!hdc) hdc = CreateCompatibleDC(0);
1928             old_hbm = SelectObject(hdc, hbitmap);
1929         }
1930
1931         if (bitmap->format == PixelFormat32bppARGB || bitmap->format == PixelFormat32bppPARGB)
1932         {
1933             BLENDFUNCTION bf;
1934
1935             bf.BlendOp = AC_SRC_OVER;
1936             bf.BlendFlags = 0;
1937             bf.SourceConstantAlpha = 255;
1938             bf.AlphaFormat = AC_SRC_ALPHA;
1939
1940             GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
1941                 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, bf);
1942         }
1943         else
1944         {
1945             StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
1946                 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, SRCCOPY);
1947         }
1948
1949         if (temp_hdc)
1950         {
1951             SelectObject(hdc, old_hbm);
1952             DeleteDC(hdc);
1953         }
1954
1955         if (temp_bitmap)
1956             DeleteObject(hbitmap);
1957     }
1958     else
1959     {
1960         ERR("GpImage with no IPicture or HBITMAP?!\n");
1961         return NotImplemented;
1962     }
1963
1964     return Ok;
1965 }
1966
1967 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1968      GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1969      INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1970      DrawImageAbort callback, VOID * callbackData)
1971 {
1972     GpPointF pointsF[3];
1973     INT i;
1974
1975     TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
1976           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1977           callbackData);
1978
1979     if(!points || count!=3)
1980         return InvalidParameter;
1981
1982     for(i = 0; i < count; i++){
1983         pointsF[i].X = (REAL)points[i].X;
1984         pointsF[i].Y = (REAL)points[i].Y;
1985     }
1986
1987     return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1988                                    (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1989                                    callback, callbackData);
1990 }
1991
1992 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1993     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1994     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1995     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1996     VOID * callbackData)
1997 {
1998     GpPointF points[3];
1999
2000     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2001           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2002           srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2003
2004     points[0].X = dstx;
2005     points[0].Y = dsty;
2006     points[1].X = dstx + dstwidth;
2007     points[1].Y = dsty;
2008     points[2].X = dstx;
2009     points[2].Y = dsty + dstheight;
2010
2011     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2012                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2013 }
2014
2015 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
2016         INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
2017         INT srcwidth, INT srcheight, GpUnit srcUnit,
2018         GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
2019         VOID * callbackData)
2020 {
2021     GpPointF points[3];
2022
2023     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2024           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2025           srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2026
2027     points[0].X = dstx;
2028     points[0].Y = dsty;
2029     points[1].X = dstx + dstwidth;
2030     points[1].Y = dsty;
2031     points[2].X = dstx;
2032     points[2].Y = dsty + dstheight;
2033
2034     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2035                srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2036 }
2037
2038 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
2039     REAL x, REAL y, REAL width, REAL height)
2040 {
2041     RectF bounds;
2042     GpUnit unit;
2043     GpStatus ret;
2044
2045     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
2046
2047     if(!graphics || !image)
2048         return InvalidParameter;
2049
2050     ret = GdipGetImageBounds(image, &bounds, &unit);
2051     if(ret != Ok)
2052         return ret;
2053
2054     return GdipDrawImageRectRect(graphics, image, x, y, width, height,
2055                                  bounds.X, bounds.Y, bounds.Width, bounds.Height,
2056                                  unit, NULL, NULL, NULL);
2057 }
2058
2059 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
2060     INT x, INT y, INT width, INT height)
2061 {
2062     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
2063
2064     return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
2065 }
2066
2067 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
2068     REAL y1, REAL x2, REAL y2)
2069 {
2070     INT save_state;
2071     GpPointF pt[2];
2072     GpStatus retval;
2073
2074     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2075
2076     if(!pen || !graphics)
2077         return InvalidParameter;
2078
2079     if(graphics->busy)
2080         return ObjectBusy;
2081
2082     pt[0].X = x1;
2083     pt[0].Y = y1;
2084     pt[1].X = x2;
2085     pt[1].Y = y2;
2086
2087     save_state = prepare_dc(graphics, pen);
2088
2089     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2090
2091     restore_dc(graphics, save_state);
2092
2093     return retval;
2094 }
2095
2096 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2097     INT y1, INT x2, INT y2)
2098 {
2099     INT save_state;
2100     GpPointF pt[2];
2101     GpStatus retval;
2102
2103     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2104
2105     if(!pen || !graphics)
2106         return InvalidParameter;
2107
2108     if(graphics->busy)
2109         return ObjectBusy;
2110
2111     pt[0].X = (REAL)x1;
2112     pt[0].Y = (REAL)y1;
2113     pt[1].X = (REAL)x2;
2114     pt[1].Y = (REAL)y2;
2115
2116     save_state = prepare_dc(graphics, pen);
2117
2118     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2119
2120     restore_dc(graphics, save_state);
2121
2122     return retval;
2123 }
2124
2125 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2126     GpPointF *points, INT count)
2127 {
2128     INT save_state;
2129     GpStatus retval;
2130
2131     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2132
2133     if(!pen || !graphics || (count < 2))
2134         return InvalidParameter;
2135
2136     if(graphics->busy)
2137         return ObjectBusy;
2138
2139     save_state = prepare_dc(graphics, pen);
2140
2141     retval = draw_polyline(graphics, pen, points, count, TRUE);
2142
2143     restore_dc(graphics, save_state);
2144
2145     return retval;
2146 }
2147
2148 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2149     GpPoint *points, INT count)
2150 {
2151     INT save_state;
2152     GpStatus retval;
2153     GpPointF *ptf = NULL;
2154     int i;
2155
2156     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2157
2158     if(!pen || !graphics || (count < 2))
2159         return InvalidParameter;
2160
2161     if(graphics->busy)
2162         return ObjectBusy;
2163
2164     ptf = GdipAlloc(count * sizeof(GpPointF));
2165     if(!ptf) return OutOfMemory;
2166
2167     for(i = 0; i < count; i ++){
2168         ptf[i].X = (REAL) points[i].X;
2169         ptf[i].Y = (REAL) points[i].Y;
2170     }
2171
2172     save_state = prepare_dc(graphics, pen);
2173
2174     retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2175
2176     restore_dc(graphics, save_state);
2177
2178     GdipFree(ptf);
2179     return retval;
2180 }
2181
2182 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2183 {
2184     INT save_state;
2185     GpStatus retval;
2186
2187     TRACE("(%p, %p, %p)\n", graphics, pen, path);
2188
2189     if(!pen || !graphics)
2190         return InvalidParameter;
2191
2192     if(graphics->busy)
2193         return ObjectBusy;
2194
2195     save_state = prepare_dc(graphics, pen);
2196
2197     retval = draw_poly(graphics, pen, path->pathdata.Points,
2198                        path->pathdata.Types, path->pathdata.Count, TRUE);
2199
2200     restore_dc(graphics, save_state);
2201
2202     return retval;
2203 }
2204
2205 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2206     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2207 {
2208     INT save_state;
2209
2210     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2211             width, height, startAngle, sweepAngle);
2212
2213     if(!graphics || !pen)
2214         return InvalidParameter;
2215
2216     if(graphics->busy)
2217         return ObjectBusy;
2218
2219     save_state = prepare_dc(graphics, pen);
2220     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2221
2222     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2223
2224     restore_dc(graphics, save_state);
2225
2226     return Ok;
2227 }
2228
2229 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
2230     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2231 {
2232     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2233             width, height, startAngle, sweepAngle);
2234
2235     return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2236 }
2237
2238 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
2239     REAL y, REAL width, REAL height)
2240 {
2241     INT save_state;
2242     GpPointF ptf[4];
2243     POINT pti[4];
2244
2245     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2246
2247     if(!pen || !graphics)
2248         return InvalidParameter;
2249
2250     if(graphics->busy)
2251         return ObjectBusy;
2252
2253     ptf[0].X = x;
2254     ptf[0].Y = y;
2255     ptf[1].X = x + width;
2256     ptf[1].Y = y;
2257     ptf[2].X = x + width;
2258     ptf[2].Y = y + height;
2259     ptf[3].X = x;
2260     ptf[3].Y = y + height;
2261
2262     save_state = prepare_dc(graphics, pen);
2263     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2264
2265     transform_and_round_points(graphics, pti, ptf, 4);
2266     Polygon(graphics->hdc, pti, 4);
2267
2268     restore_dc(graphics, save_state);
2269
2270     return Ok;
2271 }
2272
2273 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
2274     INT y, INT width, INT height)
2275 {
2276     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2277
2278     return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2279 }
2280
2281 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
2282     GDIPCONST GpRectF* rects, INT count)
2283 {
2284     GpPointF *ptf;
2285     POINT *pti;
2286     INT save_state, i;
2287
2288     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2289
2290     if(!graphics || !pen || !rects || count < 1)
2291         return InvalidParameter;
2292
2293     if(graphics->busy)
2294         return ObjectBusy;
2295
2296     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
2297     pti = GdipAlloc(4 * count * sizeof(POINT));
2298
2299     if(!ptf || !pti){
2300         GdipFree(ptf);
2301         GdipFree(pti);
2302         return OutOfMemory;
2303     }
2304
2305     for(i = 0; i < count; i++){
2306         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
2307         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
2308         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
2309         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
2310     }
2311
2312     save_state = prepare_dc(graphics, pen);
2313     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2314
2315     transform_and_round_points(graphics, pti, ptf, 4 * count);
2316
2317     for(i = 0; i < count; i++)
2318         Polygon(graphics->hdc, &pti[4 * i], 4);
2319
2320     restore_dc(graphics, save_state);
2321
2322     GdipFree(ptf);
2323     GdipFree(pti);
2324
2325     return Ok;
2326 }
2327
2328 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
2329     GDIPCONST GpRect* rects, INT count)
2330 {
2331     GpRectF *rectsF;
2332     GpStatus ret;
2333     INT i;
2334
2335     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2336
2337     if(!rects || count<=0)
2338         return InvalidParameter;
2339
2340     rectsF = GdipAlloc(sizeof(GpRectF) * count);
2341     if(!rectsF)
2342         return OutOfMemory;
2343
2344     for(i = 0;i < count;i++){
2345         rectsF[i].X      = (REAL)rects[i].X;
2346         rectsF[i].Y      = (REAL)rects[i].Y;
2347         rectsF[i].Width  = (REAL)rects[i].Width;
2348         rectsF[i].Height = (REAL)rects[i].Height;
2349     }
2350
2351     ret = GdipDrawRectangles(graphics, pen, rectsF, count);
2352     GdipFree(rectsF);
2353
2354     return ret;
2355 }
2356
2357 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
2358     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
2359     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
2360 {
2361     HRGN rgn = NULL;
2362     HFONT gdifont;
2363     LOGFONTW lfw;
2364     TEXTMETRICW textmet;
2365     GpPointF pt[3], rectcpy[4];
2366     POINT corners[4];
2367     WCHAR* stringdup;
2368     REAL angle, ang_cos, ang_sin, rel_width, rel_height;
2369     INT sum = 0, height = 0, offsety = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
2370         nheight, lineend;
2371     SIZE size;
2372     POINT drawbase;
2373     UINT drawflags;
2374     RECT drawcoord;
2375
2376     TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
2377         length, font, debugstr_rectf(rect), format, brush);
2378
2379     if(!graphics || !string || !font || !brush || !rect)
2380         return InvalidParameter;
2381
2382     if((brush->bt != BrushTypeSolidColor)){
2383         FIXME("not implemented for given parameters\n");
2384         return NotImplemented;
2385     }
2386
2387     if(format){
2388         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2389
2390         /* Should be no need to explicitly test for StringAlignmentNear as
2391          * that is default behavior if no alignment is passed. */
2392         if(format->vertalign != StringAlignmentNear){
2393             RectF bounds;
2394             GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
2395
2396             if(format->vertalign == StringAlignmentCenter)
2397                 offsety = (rect->Height - bounds.Height) / 2;
2398             else if(format->vertalign == StringAlignmentFar)
2399                 offsety = (rect->Height - bounds.Height);
2400         }
2401     }
2402
2403     if(length == -1) length = lstrlenW(string);
2404
2405     stringdup = GdipAlloc(length * sizeof(WCHAR));
2406     if(!stringdup) return OutOfMemory;
2407
2408     save_state = SaveDC(graphics->hdc);
2409     SetBkMode(graphics->hdc, TRANSPARENT);
2410     SetTextColor(graphics->hdc, brush->lb.lbColor);
2411
2412     pt[0].X = 0.0;
2413     pt[0].Y = 0.0;
2414     pt[1].X = 1.0;
2415     pt[1].Y = 0.0;
2416     pt[2].X = 0.0;
2417     pt[2].Y = 1.0;
2418     GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2419     angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2420     ang_cos = cos(angle);
2421     ang_sin = sin(angle);
2422     rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2423                      (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2424     rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2425                       (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2426
2427     rectcpy[3].X = rectcpy[0].X = rect->X;
2428     rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
2429     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
2430     rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
2431     transform_and_round_points(graphics, corners, rectcpy, 4);
2432
2433     if (roundr(rect->Width) == 0)
2434         nwidth = INT_MAX;
2435     else
2436         nwidth = roundr(rel_width * rect->Width);
2437
2438     if (roundr(rect->Height) == 0)
2439         nheight = INT_MAX;
2440     else
2441         nheight = roundr(rel_height * rect->Height);
2442
2443     if (roundr(rect->Width) != 0 && roundr(rect->Height) != 0)
2444     {
2445         /* FIXME: If only the width or only the height is 0, we should probably still clip */
2446         rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
2447         SelectClipRgn(graphics->hdc, rgn);
2448     }
2449
2450     /* Use gdi to find the font, then perform transformations on it (height,
2451      * width, angle). */
2452     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2453     GetTextMetricsW(graphics->hdc, &textmet);
2454     lfw = font->lfw;
2455
2456     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
2457     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
2458
2459     lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
2460
2461     gdifont = CreateFontIndirectW(&lfw);
2462     DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
2463
2464     for(i = 0, j = 0; i < length; i++){
2465         if(!isprintW(string[i]) && (string[i] != '\n'))
2466             continue;
2467
2468         stringdup[j] = string[i];
2469         j++;
2470     }
2471
2472     length = j;
2473
2474     if (!format || format->align == StringAlignmentNear)
2475     {
2476         drawbase.x = corners[0].x;
2477         drawbase.y = corners[0].y;
2478         drawflags = DT_NOCLIP | DT_EXPANDTABS;
2479     }
2480     else if (format->align == StringAlignmentCenter)
2481     {
2482         drawbase.x = (corners[0].x + corners[1].x)/2;
2483         drawbase.y = (corners[0].y + corners[1].y)/2;
2484         drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
2485     }
2486     else /* (format->align == StringAlignmentFar) */
2487     {
2488         drawbase.x = corners[1].x;
2489         drawbase.y = corners[1].y;
2490         drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
2491     }
2492
2493     while(sum < length){
2494         drawcoord.left = drawcoord.right = drawbase.x + roundr(ang_sin * (REAL) height);
2495         drawcoord.top = drawcoord.bottom = drawbase.y + roundr(ang_cos * (REAL) height);
2496
2497         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2498                               nwidth, &fit, NULL, &size);
2499         fitcpy = fit;
2500
2501         if(fit == 0){
2502             DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, drawflags);
2503             break;
2504         }
2505
2506         for(lret = 0; lret < fit; lret++)
2507             if(*(stringdup + sum + lret) == '\n')
2508                 break;
2509
2510         /* Line break code (may look strange, but it imitates windows). */
2511         if(lret < fit)
2512             lineend = fit = lret;    /* this is not an off-by-one error */
2513         else if(fit < (length - sum)){
2514             if(*(stringdup + sum + fit) == ' ')
2515                 while(*(stringdup + sum + fit) == ' ')
2516                     fit++;
2517             else
2518                 while(*(stringdup + sum + fit - 1) != ' '){
2519                     fit--;
2520
2521                     if(*(stringdup + sum + fit) == '\t')
2522                         break;
2523
2524                     if(fit == 0){
2525                         fit = fitcpy;
2526                         break;
2527                     }
2528                 }
2529             lineend = fit;
2530             while(*(stringdup + sum + lineend - 1) == ' ' ||
2531                   *(stringdup + sum + lineend - 1) == '\t')
2532                 lineend--;
2533         }
2534         else
2535             lineend = fit;
2536         DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, lineend),
2537                   &drawcoord, drawflags);
2538
2539         sum += fit + (lret < fitcpy ? 1 : 0);
2540         height += size.cy;
2541
2542         if(height > nheight)
2543             break;
2544
2545         /* Stop if this was a linewrap (but not if it was a linebreak). */
2546         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2547             break;
2548     }
2549
2550     GdipFree(stringdup);
2551     DeleteObject(rgn);
2552     DeleteObject(gdifont);
2553
2554     RestoreDC(graphics->hdc, save_state);
2555
2556     return Ok;
2557 }
2558
2559 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
2560     GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
2561 {
2562     GpPath *path;
2563     GpStatus stat;
2564
2565     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2566             count, tension, fill);
2567
2568     if(!graphics || !brush || !points)
2569         return InvalidParameter;
2570
2571     if(graphics->busy)
2572         return ObjectBusy;
2573
2574     stat = GdipCreatePath(fill, &path);
2575     if(stat != Ok)
2576         return stat;
2577
2578     stat = GdipAddPathClosedCurve2(path, points, count, tension);
2579     if(stat != Ok){
2580         GdipDeletePath(path);
2581         return stat;
2582     }
2583
2584     stat = GdipFillPath(graphics, brush, path);
2585     if(stat != Ok){
2586         GdipDeletePath(path);
2587         return stat;
2588     }
2589
2590     GdipDeletePath(path);
2591
2592     return Ok;
2593 }
2594
2595 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
2596     GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
2597 {
2598     GpPointF *ptf;
2599     GpStatus stat;
2600     INT i;
2601
2602     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2603             count, tension, fill);
2604
2605     if(!points || count <= 0)
2606         return InvalidParameter;
2607
2608     ptf = GdipAlloc(sizeof(GpPointF)*count);
2609     if(!ptf)
2610         return OutOfMemory;
2611
2612     for(i = 0;i < count;i++){
2613         ptf[i].X = (REAL)points[i].X;
2614         ptf[i].Y = (REAL)points[i].Y;
2615     }
2616
2617     stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2618
2619     GdipFree(ptf);
2620
2621     return stat;
2622 }
2623
2624 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2625     REAL y, REAL width, REAL height)
2626 {
2627     INT save_state;
2628     GpPointF ptf[2];
2629     POINT pti[2];
2630
2631     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2632
2633     if(!graphics || !brush)
2634         return InvalidParameter;
2635
2636     if(graphics->busy)
2637         return ObjectBusy;
2638
2639     ptf[0].X = x;
2640     ptf[0].Y = y;
2641     ptf[1].X = x + width;
2642     ptf[1].Y = y + height;
2643
2644     save_state = SaveDC(graphics->hdc);
2645     EndPath(graphics->hdc);
2646
2647     transform_and_round_points(graphics, pti, ptf, 2);
2648
2649     BeginPath(graphics->hdc);
2650     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2651     EndPath(graphics->hdc);
2652
2653     brush_fill_path(graphics, brush);
2654
2655     RestoreDC(graphics->hdc, save_state);
2656
2657     return Ok;
2658 }
2659
2660 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2661     INT y, INT width, INT height)
2662 {
2663     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2664
2665     return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2666 }
2667
2668 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2669 {
2670     INT save_state;
2671     GpStatus retval;
2672
2673     TRACE("(%p, %p, %p)\n", graphics, brush, path);
2674
2675     if(!brush || !graphics || !path)
2676         return InvalidParameter;
2677
2678     if(graphics->busy)
2679         return ObjectBusy;
2680
2681     save_state = SaveDC(graphics->hdc);
2682     EndPath(graphics->hdc);
2683     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2684                                                                     : WINDING));
2685
2686     BeginPath(graphics->hdc);
2687     retval = draw_poly(graphics, NULL, path->pathdata.Points,
2688                        path->pathdata.Types, path->pathdata.Count, FALSE);
2689
2690     if(retval != Ok)
2691         goto end;
2692
2693     EndPath(graphics->hdc);
2694     brush_fill_path(graphics, brush);
2695
2696     retval = Ok;
2697
2698 end:
2699     RestoreDC(graphics->hdc, save_state);
2700
2701     return retval;
2702 }
2703
2704 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2705     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2706 {
2707     INT save_state;
2708
2709     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2710             graphics, brush, x, y, width, height, startAngle, sweepAngle);
2711
2712     if(!graphics || !brush)
2713         return InvalidParameter;
2714
2715     if(graphics->busy)
2716         return ObjectBusy;
2717
2718     save_state = SaveDC(graphics->hdc);
2719     EndPath(graphics->hdc);
2720
2721     BeginPath(graphics->hdc);
2722     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2723     EndPath(graphics->hdc);
2724
2725     brush_fill_path(graphics, brush);
2726
2727     RestoreDC(graphics->hdc, save_state);
2728
2729     return Ok;
2730 }
2731
2732 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2733     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2734 {
2735     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
2736             graphics, brush, x, y, width, height, startAngle, sweepAngle);
2737
2738     return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2739 }
2740
2741 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2742     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2743 {
2744     INT save_state;
2745     GpPointF *ptf = NULL;
2746     POINT *pti = NULL;
2747     GpStatus retval = Ok;
2748
2749     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2750
2751     if(!graphics || !brush || !points || !count)
2752         return InvalidParameter;
2753
2754     if(graphics->busy)
2755         return ObjectBusy;
2756
2757     ptf = GdipAlloc(count * sizeof(GpPointF));
2758     pti = GdipAlloc(count * sizeof(POINT));
2759     if(!ptf || !pti){
2760         retval = OutOfMemory;
2761         goto end;
2762     }
2763
2764     memcpy(ptf, points, count * sizeof(GpPointF));
2765
2766     save_state = SaveDC(graphics->hdc);
2767     EndPath(graphics->hdc);
2768     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2769                                                                   : WINDING));
2770
2771     transform_and_round_points(graphics, pti, ptf, count);
2772
2773     BeginPath(graphics->hdc);
2774     Polygon(graphics->hdc, pti, count);
2775     EndPath(graphics->hdc);
2776
2777     brush_fill_path(graphics, brush);
2778
2779     RestoreDC(graphics->hdc, save_state);
2780
2781 end:
2782     GdipFree(ptf);
2783     GdipFree(pti);
2784
2785     return retval;
2786 }
2787
2788 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2789     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2790 {
2791     INT save_state, i;
2792     GpPointF *ptf = NULL;
2793     POINT *pti = NULL;
2794     GpStatus retval = Ok;
2795
2796     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2797
2798     if(!graphics || !brush || !points || !count)
2799         return InvalidParameter;
2800
2801     if(graphics->busy)
2802         return ObjectBusy;
2803
2804     ptf = GdipAlloc(count * sizeof(GpPointF));
2805     pti = GdipAlloc(count * sizeof(POINT));
2806     if(!ptf || !pti){
2807         retval = OutOfMemory;
2808         goto end;
2809     }
2810
2811     for(i = 0; i < count; i ++){
2812         ptf[i].X = (REAL) points[i].X;
2813         ptf[i].Y = (REAL) points[i].Y;
2814     }
2815
2816     save_state = SaveDC(graphics->hdc);
2817     EndPath(graphics->hdc);
2818     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2819                                                                   : WINDING));
2820
2821     transform_and_round_points(graphics, pti, ptf, count);
2822
2823     BeginPath(graphics->hdc);
2824     Polygon(graphics->hdc, pti, count);
2825     EndPath(graphics->hdc);
2826
2827     brush_fill_path(graphics, brush);
2828
2829     RestoreDC(graphics->hdc, save_state);
2830
2831 end:
2832     GdipFree(ptf);
2833     GdipFree(pti);
2834
2835     return retval;
2836 }
2837
2838 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2839     GDIPCONST GpPointF *points, INT count)
2840 {
2841     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2842
2843     return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2844 }
2845
2846 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2847     GDIPCONST GpPoint *points, INT count)
2848 {
2849     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2850
2851     return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2852 }
2853
2854 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2855     REAL x, REAL y, REAL width, REAL height)
2856 {
2857     INT save_state;
2858     GpPointF ptf[4];
2859     POINT pti[4];
2860
2861     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2862
2863     if(!graphics || !brush)
2864         return InvalidParameter;
2865
2866     if(graphics->busy)
2867         return ObjectBusy;
2868
2869     ptf[0].X = x;
2870     ptf[0].Y = y;
2871     ptf[1].X = x + width;
2872     ptf[1].Y = y;
2873     ptf[2].X = x + width;
2874     ptf[2].Y = y + height;
2875     ptf[3].X = x;
2876     ptf[3].Y = y + height;
2877
2878     save_state = SaveDC(graphics->hdc);
2879     EndPath(graphics->hdc);
2880
2881     transform_and_round_points(graphics, pti, ptf, 4);
2882
2883     BeginPath(graphics->hdc);
2884     Polygon(graphics->hdc, pti, 4);
2885     EndPath(graphics->hdc);
2886
2887     brush_fill_path(graphics, brush);
2888
2889     RestoreDC(graphics->hdc, save_state);
2890
2891     return Ok;
2892 }
2893
2894 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2895     INT x, INT y, INT width, INT height)
2896 {
2897     INT save_state;
2898     GpPointF ptf[4];
2899     POINT pti[4];
2900
2901     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2902
2903     if(!graphics || !brush)
2904         return InvalidParameter;
2905
2906     if(graphics->busy)
2907         return ObjectBusy;
2908
2909     ptf[0].X = x;
2910     ptf[0].Y = y;
2911     ptf[1].X = x + width;
2912     ptf[1].Y = y;
2913     ptf[2].X = x + width;
2914     ptf[2].Y = y + height;
2915     ptf[3].X = x;
2916     ptf[3].Y = y + height;
2917
2918     save_state = SaveDC(graphics->hdc);
2919     EndPath(graphics->hdc);
2920
2921     transform_and_round_points(graphics, pti, ptf, 4);
2922
2923     BeginPath(graphics->hdc);
2924     Polygon(graphics->hdc, pti, 4);
2925     EndPath(graphics->hdc);
2926
2927     brush_fill_path(graphics, brush);
2928
2929     RestoreDC(graphics->hdc, save_state);
2930
2931     return Ok;
2932 }
2933
2934 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2935     INT count)
2936 {
2937     GpStatus ret;
2938     INT i;
2939
2940     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2941
2942     if(!rects)
2943         return InvalidParameter;
2944
2945     for(i = 0; i < count; i++){
2946         ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2947         if(ret != Ok)   return ret;
2948     }
2949
2950     return Ok;
2951 }
2952
2953 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2954     INT count)
2955 {
2956     GpRectF *rectsF;
2957     GpStatus ret;
2958     INT i;
2959
2960     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2961
2962     if(!rects || count <= 0)
2963         return InvalidParameter;
2964
2965     rectsF = GdipAlloc(sizeof(GpRectF)*count);
2966     if(!rectsF)
2967         return OutOfMemory;
2968
2969     for(i = 0; i < count; i++){
2970         rectsF[i].X      = (REAL)rects[i].X;
2971         rectsF[i].Y      = (REAL)rects[i].Y;
2972         rectsF[i].X      = (REAL)rects[i].Width;
2973         rectsF[i].Height = (REAL)rects[i].Height;
2974     }
2975
2976     ret = GdipFillRectangles(graphics,brush,rectsF,count);
2977     GdipFree(rectsF);
2978
2979     return ret;
2980 }
2981
2982 /*****************************************************************************
2983  * GdipFillRegion [GDIPLUS.@]
2984  */
2985 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
2986         GpRegion* region)
2987 {
2988     INT save_state;
2989     GpStatus status;
2990     HRGN hrgn;
2991     RECT rc;
2992
2993     TRACE("(%p, %p, %p)\n", graphics, brush, region);
2994
2995     if (!(graphics && brush && region))
2996         return InvalidParameter;
2997
2998     if(graphics->busy)
2999         return ObjectBusy;
3000
3001     status = GdipGetRegionHRgn(region, graphics, &hrgn);
3002     if(status != Ok)
3003         return status;
3004
3005     save_state = SaveDC(graphics->hdc);
3006     EndPath(graphics->hdc);
3007
3008     ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3009
3010     if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3011     {
3012         BeginPath(graphics->hdc);
3013         Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3014         EndPath(graphics->hdc);
3015
3016         brush_fill_path(graphics, brush);
3017     }
3018
3019     RestoreDC(graphics->hdc, save_state);
3020
3021     DeleteObject(hrgn);
3022
3023     return Ok;
3024 }
3025
3026 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3027 {
3028     static int calls;
3029
3030     if(!graphics)
3031         return InvalidParameter;
3032
3033     if(graphics->busy)
3034         return ObjectBusy;
3035
3036     if(!(calls++))
3037         FIXME("not implemented\n");
3038
3039     return NotImplemented;
3040 }
3041
3042 /*****************************************************************************
3043  * GdipGetClipBounds [GDIPLUS.@]
3044  */
3045 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3046 {
3047     TRACE("(%p, %p)\n", graphics, rect);
3048
3049     if(!graphics)
3050         return InvalidParameter;
3051
3052     if(graphics->busy)
3053         return ObjectBusy;
3054
3055     return GdipGetRegionBounds(graphics->clip, graphics, rect);
3056 }
3057
3058 /*****************************************************************************
3059  * GdipGetClipBoundsI [GDIPLUS.@]
3060  */
3061 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
3062 {
3063     TRACE("(%p, %p)\n", graphics, rect);
3064
3065     if(!graphics)
3066         return InvalidParameter;
3067
3068     if(graphics->busy)
3069         return ObjectBusy;
3070
3071     return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
3072 }
3073
3074 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
3075 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
3076     CompositingMode *mode)
3077 {
3078     TRACE("(%p, %p)\n", graphics, mode);
3079
3080     if(!graphics || !mode)
3081         return InvalidParameter;
3082
3083     if(graphics->busy)
3084         return ObjectBusy;
3085
3086     *mode = graphics->compmode;
3087
3088     return Ok;
3089 }
3090
3091 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3092 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3093     CompositingQuality *quality)
3094 {
3095     TRACE("(%p, %p)\n", graphics, quality);
3096
3097     if(!graphics || !quality)
3098         return InvalidParameter;
3099
3100     if(graphics->busy)
3101         return ObjectBusy;
3102
3103     *quality = graphics->compqual;
3104
3105     return Ok;
3106 }
3107
3108 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3109 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3110     InterpolationMode *mode)
3111 {
3112     TRACE("(%p, %p)\n", graphics, mode);
3113
3114     if(!graphics || !mode)
3115         return InvalidParameter;
3116
3117     if(graphics->busy)
3118         return ObjectBusy;
3119
3120     *mode = graphics->interpolation;
3121
3122     return Ok;
3123 }
3124
3125 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3126 {
3127     if(!graphics || !argb)
3128         return InvalidParameter;
3129
3130     if(graphics->busy)
3131         return ObjectBusy;
3132
3133     FIXME("(%p, %p): stub\n", graphics, argb);
3134
3135     return NotImplemented;
3136 }
3137
3138 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3139 {
3140     TRACE("(%p, %p)\n", graphics, scale);
3141
3142     if(!graphics || !scale)
3143         return InvalidParameter;
3144
3145     if(graphics->busy)
3146         return ObjectBusy;
3147
3148     *scale = graphics->scale;
3149
3150     return Ok;
3151 }
3152
3153 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3154 {
3155     TRACE("(%p, %p)\n", graphics, unit);
3156
3157     if(!graphics || !unit)
3158         return InvalidParameter;
3159
3160     if(graphics->busy)
3161         return ObjectBusy;
3162
3163     *unit = graphics->unit;
3164
3165     return Ok;
3166 }
3167
3168 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3169 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3170     *mode)
3171 {
3172     TRACE("(%p, %p)\n", graphics, mode);
3173
3174     if(!graphics || !mode)
3175         return InvalidParameter;
3176
3177     if(graphics->busy)
3178         return ObjectBusy;
3179
3180     *mode = graphics->pixeloffset;
3181
3182     return Ok;
3183 }
3184
3185 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3186 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3187 {
3188     TRACE("(%p, %p)\n", graphics, mode);
3189
3190     if(!graphics || !mode)
3191         return InvalidParameter;
3192
3193     if(graphics->busy)
3194         return ObjectBusy;
3195
3196     *mode = graphics->smoothing;
3197
3198     return Ok;
3199 }
3200
3201 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3202 {
3203     TRACE("(%p, %p)\n", graphics, contrast);
3204
3205     if(!graphics || !contrast)
3206         return InvalidParameter;
3207
3208     *contrast = graphics->textcontrast;
3209
3210     return Ok;
3211 }
3212
3213 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3214 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3215     TextRenderingHint *hint)
3216 {
3217     TRACE("(%p, %p)\n", graphics, hint);
3218
3219     if(!graphics || !hint)
3220         return InvalidParameter;
3221
3222     if(graphics->busy)
3223         return ObjectBusy;
3224
3225     *hint = graphics->texthint;
3226
3227     return Ok;
3228 }
3229
3230 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3231 {
3232     GpRegion *clip_rgn;
3233     GpStatus stat;
3234
3235     TRACE("(%p, %p)\n", graphics, rect);
3236
3237     if(!graphics || !rect)
3238         return InvalidParameter;
3239
3240     if(graphics->busy)
3241         return ObjectBusy;
3242
3243     /* intersect window and graphics clipping regions */
3244     if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3245         return stat;
3246
3247     if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
3248         goto cleanup;
3249
3250     /* get bounds of the region */
3251     stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
3252
3253 cleanup:
3254     GdipDeleteRegion(clip_rgn);
3255
3256     return stat;
3257 }
3258
3259 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
3260 {
3261     GpRectF rectf;
3262     GpStatus stat;
3263
3264     TRACE("(%p, %p)\n", graphics, rect);
3265
3266     if(!graphics || !rect)
3267         return InvalidParameter;
3268
3269     if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
3270     {
3271         rect->X = roundr(rectf.X);
3272         rect->Y = roundr(rectf.Y);
3273         rect->Width  = roundr(rectf.Width);
3274         rect->Height = roundr(rectf.Height);
3275     }
3276
3277     return stat;
3278 }
3279
3280 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3281 {
3282     TRACE("(%p, %p)\n", graphics, matrix);
3283
3284     if(!graphics || !matrix)
3285         return InvalidParameter;
3286
3287     if(graphics->busy)
3288         return ObjectBusy;
3289
3290     *matrix = *graphics->worldtrans;
3291     return Ok;
3292 }
3293
3294 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
3295 {
3296     GpSolidFill *brush;
3297     GpStatus stat;
3298     GpRectF wnd_rect;
3299
3300     TRACE("(%p, %x)\n", graphics, color);
3301
3302     if(!graphics)
3303         return InvalidParameter;
3304
3305     if(graphics->busy)
3306         return ObjectBusy;
3307
3308     if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
3309         return stat;
3310
3311     if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
3312         GdipDeleteBrush((GpBrush*)brush);
3313         return stat;
3314     }
3315
3316     GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
3317                                                  wnd_rect.Width, wnd_rect.Height);
3318
3319     GdipDeleteBrush((GpBrush*)brush);
3320
3321     return Ok;
3322 }
3323
3324 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
3325 {
3326     TRACE("(%p, %p)\n", graphics, res);
3327
3328     if(!graphics || !res)
3329         return InvalidParameter;
3330
3331     return GdipIsEmptyRegion(graphics->clip, graphics, res);
3332 }
3333
3334 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
3335 {
3336     GpStatus stat;
3337     GpRegion* rgn;
3338     GpPointF pt;
3339
3340     TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
3341
3342     if(!graphics || !result)
3343         return InvalidParameter;
3344
3345     if(graphics->busy)
3346         return ObjectBusy;
3347
3348     pt.X = x;
3349     pt.Y = y;
3350     if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3351                    CoordinateSpaceWorld, &pt, 1)) != Ok)
3352         return stat;
3353
3354     if((stat = GdipCreateRegion(&rgn)) != Ok)
3355         return stat;
3356
3357     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3358         goto cleanup;
3359
3360     stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
3361
3362 cleanup:
3363     GdipDeleteRegion(rgn);
3364     return stat;
3365 }
3366
3367 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
3368 {
3369     return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
3370 }
3371
3372 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
3373 {
3374     GpStatus stat;
3375     GpRegion* rgn;
3376     GpPointF pts[2];
3377
3378     TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
3379
3380     if(!graphics || !result)
3381         return InvalidParameter;
3382
3383     if(graphics->busy)
3384         return ObjectBusy;
3385
3386     pts[0].X = x;
3387     pts[0].Y = y;
3388     pts[1].X = x + width;
3389     pts[1].Y = y + height;
3390
3391     if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3392                     CoordinateSpaceWorld, pts, 2)) != Ok)
3393         return stat;
3394
3395     pts[1].X -= pts[0].X;
3396     pts[1].Y -= pts[0].Y;
3397
3398     if((stat = GdipCreateRegion(&rgn)) != Ok)
3399         return stat;
3400
3401     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3402         goto cleanup;
3403
3404     stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
3405
3406 cleanup:
3407     GdipDeleteRegion(rgn);
3408     return stat;
3409 }
3410
3411 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
3412 {
3413     return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
3414 }
3415
3416 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
3417         GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
3418         GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
3419         INT regionCount, GpRegion** regions)
3420 {
3421     if (!(graphics && string && font && layoutRect && stringFormat && regions))
3422         return InvalidParameter;
3423
3424     FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
3425             length, font, layoutRect, stringFormat, regionCount, regions);
3426
3427     return NotImplemented;
3428 }
3429
3430 /* Find the smallest rectangle that bounds the text when it is printed in rect
3431  * according to the format options listed in format. If rect has 0 width and
3432  * height, then just find the smallest rectangle that bounds the text when it's
3433  * printed at location (rect->X, rect-Y). */
3434 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
3435     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3436     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
3437     INT *codepointsfitted, INT *linesfilled)
3438 {
3439     HFONT oldfont;
3440     WCHAR* stringdup;
3441     INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
3442         nheight, lineend;
3443     SIZE size;
3444
3445     TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
3446         debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
3447         bounds, codepointsfitted, linesfilled);
3448
3449     if(!graphics || !string || !font || !rect)
3450         return InvalidParameter;
3451
3452     if(linesfilled) *linesfilled = 0;
3453     if(codepointsfitted) *codepointsfitted = 0;
3454
3455     if(format)
3456         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
3457
3458     if(length == -1) length = lstrlenW(string);
3459
3460     stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
3461     if(!stringdup) return OutOfMemory;
3462
3463     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3464     nwidth = roundr(rect->Width);
3465     nheight = roundr(rect->Height);
3466
3467     if((nwidth == 0) && (nheight == 0))
3468         nwidth = nheight = INT_MAX;
3469
3470     for(i = 0, j = 0; i < length; i++){
3471         if(!isprintW(string[i]) && (string[i] != '\n'))
3472             continue;
3473
3474         stringdup[j] = string[i];
3475         j++;
3476     }
3477
3478     stringdup[j] = 0;
3479     length = j;
3480
3481     while(sum < length){
3482         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
3483                               nwidth, &fit, NULL, &size);
3484         fitcpy = fit;
3485
3486         if(fit == 0)
3487             break;
3488
3489         for(lret = 0; lret < fit; lret++)
3490             if(*(stringdup + sum + lret) == '\n')
3491                 break;
3492
3493         /* Line break code (may look strange, but it imitates windows). */
3494         if(lret < fit)
3495             lineend = fit = lret;    /* this is not an off-by-one error */
3496         else if(fit < (length - sum)){
3497             if(*(stringdup + sum + fit) == ' ')
3498                 while(*(stringdup + sum + fit) == ' ')
3499                     fit++;
3500             else
3501                 while(*(stringdup + sum + fit - 1) != ' '){
3502                     fit--;
3503
3504                     if(*(stringdup + sum + fit) == '\t')
3505                         break;
3506
3507                     if(fit == 0){
3508                         fit = fitcpy;
3509                         break;
3510                     }
3511                 }
3512             lineend = fit;
3513             while(*(stringdup + sum + lineend - 1) == ' ' ||
3514                   *(stringdup + sum + lineend - 1) == '\t')
3515                 lineend--;
3516         }
3517         else
3518             lineend = fit;
3519
3520         GetTextExtentExPointW(graphics->hdc, stringdup + sum, lineend,
3521                               nwidth, &j, NULL, &size);
3522
3523         sum += fit + (lret < fitcpy ? 1 : 0);
3524         if(codepointsfitted) *codepointsfitted = sum;
3525
3526         height += size.cy;
3527         if(linesfilled) *linesfilled += size.cy;
3528         max_width = max(max_width, size.cx);
3529
3530         if(height > nheight)
3531             break;
3532
3533         /* Stop if this was a linewrap (but not if it was a linebreak). */
3534         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
3535             break;
3536     }
3537
3538     bounds->X = rect->X;
3539     bounds->Y = rect->Y;
3540     bounds->Width = (REAL)max_width;
3541     bounds->Height = (REAL) min(height, nheight);
3542
3543     GdipFree(stringdup);
3544     DeleteObject(SelectObject(graphics->hdc, oldfont));
3545
3546     return Ok;
3547 }
3548
3549 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
3550 {
3551     TRACE("(%p)\n", graphics);
3552
3553     if(!graphics)
3554         return InvalidParameter;
3555
3556     if(graphics->busy)
3557         return ObjectBusy;
3558
3559     return GdipSetInfinite(graphics->clip);
3560 }
3561
3562 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
3563 {
3564     TRACE("(%p)\n", graphics);
3565
3566     if(!graphics)
3567         return InvalidParameter;
3568
3569     if(graphics->busy)
3570         return ObjectBusy;
3571
3572     graphics->worldtrans->matrix[0] = 1.0;
3573     graphics->worldtrans->matrix[1] = 0.0;
3574     graphics->worldtrans->matrix[2] = 0.0;
3575     graphics->worldtrans->matrix[3] = 1.0;
3576     graphics->worldtrans->matrix[4] = 0.0;
3577     graphics->worldtrans->matrix[5] = 0.0;
3578
3579     return Ok;
3580 }
3581
3582 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
3583 {
3584     return GdipEndContainer(graphics, state);
3585 }
3586
3587 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
3588     GpMatrixOrder order)
3589 {
3590     TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
3591
3592     if(!graphics)
3593         return InvalidParameter;
3594
3595     if(graphics->busy)
3596         return ObjectBusy;
3597
3598     return GdipRotateMatrix(graphics->worldtrans, angle, order);
3599 }
3600
3601 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
3602 {
3603     return GdipBeginContainer2(graphics, state);
3604 }
3605
3606 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
3607         GraphicsContainer *state)
3608 {
3609     GraphicsContainerItem *container;
3610     GpStatus sts;
3611
3612     TRACE("(%p, %p)\n", graphics, state);
3613
3614     if(!graphics || !state)
3615         return InvalidParameter;
3616
3617     sts = init_container(&container, graphics);
3618     if(sts != Ok)
3619         return sts;
3620
3621     list_add_head(&graphics->containers, &container->entry);
3622     *state = graphics->contid = container->contid;
3623
3624     return Ok;
3625 }
3626
3627 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
3628 {
3629     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3630     return NotImplemented;
3631 }
3632
3633 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
3634 {
3635     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3636     return NotImplemented;
3637 }
3638
3639 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
3640 {
3641     FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
3642     return NotImplemented;
3643 }
3644
3645 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
3646 {
3647     GpStatus sts;
3648     GraphicsContainerItem *container, *container2;
3649
3650     TRACE("(%p, %x)\n", graphics, state);
3651
3652     if(!graphics)
3653         return InvalidParameter;
3654
3655     LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
3656         if(container->contid == state)
3657             break;
3658     }
3659
3660     /* did not find a matching container */
3661     if(&container->entry == &graphics->containers)
3662         return Ok;
3663
3664     sts = restore_container(graphics, container);
3665     if(sts != Ok)
3666         return sts;
3667
3668     /* remove all of the containers on top of the found container */
3669     LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
3670         if(container->contid == state)
3671             break;
3672         list_remove(&container->entry);
3673         delete_container(container);
3674     }
3675
3676     list_remove(&container->entry);
3677     delete_container(container);
3678
3679     return Ok;
3680 }
3681
3682 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
3683     REAL sy, GpMatrixOrder order)
3684 {
3685     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
3686
3687     if(!graphics)
3688         return InvalidParameter;
3689
3690     if(graphics->busy)
3691         return ObjectBusy;
3692
3693     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
3694 }
3695
3696 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
3697     CombineMode mode)
3698 {
3699     TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
3700
3701     if(!graphics || !srcgraphics)
3702         return InvalidParameter;
3703
3704     return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
3705 }
3706
3707 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
3708     CompositingMode mode)
3709 {
3710     TRACE("(%p, %d)\n", graphics, mode);
3711
3712     if(!graphics)
3713         return InvalidParameter;
3714
3715     if(graphics->busy)
3716         return ObjectBusy;
3717
3718     graphics->compmode = mode;
3719
3720     return Ok;
3721 }
3722
3723 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
3724     CompositingQuality quality)
3725 {
3726     TRACE("(%p, %d)\n", graphics, quality);
3727
3728     if(!graphics)
3729         return InvalidParameter;
3730
3731     if(graphics->busy)
3732         return ObjectBusy;
3733
3734     graphics->compqual = quality;
3735
3736     return Ok;
3737 }
3738
3739 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
3740     InterpolationMode mode)
3741 {
3742     TRACE("(%p, %d)\n", graphics, mode);
3743
3744     if(!graphics)
3745         return InvalidParameter;
3746
3747     if(graphics->busy)
3748         return ObjectBusy;
3749
3750     graphics->interpolation = mode;
3751
3752     return Ok;
3753 }
3754
3755 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
3756 {
3757     TRACE("(%p, %.2f)\n", graphics, scale);
3758
3759     if(!graphics || (scale <= 0.0))
3760         return InvalidParameter;
3761
3762     if(graphics->busy)
3763         return ObjectBusy;
3764
3765     graphics->scale = scale;
3766
3767     return Ok;
3768 }
3769
3770 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
3771 {
3772     TRACE("(%p, %d)\n", graphics, unit);
3773
3774     if(!graphics)
3775         return InvalidParameter;
3776
3777     if(graphics->busy)
3778         return ObjectBusy;
3779
3780     if(unit == UnitWorld)
3781         return InvalidParameter;
3782
3783     graphics->unit = unit;
3784
3785     return Ok;
3786 }
3787
3788 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3789     mode)
3790 {
3791     TRACE("(%p, %d)\n", graphics, mode);
3792
3793     if(!graphics)
3794         return InvalidParameter;
3795
3796     if(graphics->busy)
3797         return ObjectBusy;
3798
3799     graphics->pixeloffset = mode;
3800
3801     return Ok;
3802 }
3803
3804 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
3805 {
3806     static int calls;
3807
3808     TRACE("(%p,%i,%i)\n", graphics, x, y);
3809
3810     if (!(calls++))
3811         FIXME("not implemented\n");
3812
3813     return NotImplemented;
3814 }
3815
3816 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
3817 {
3818     TRACE("(%p, %d)\n", graphics, mode);
3819
3820     if(!graphics)
3821         return InvalidParameter;
3822
3823     if(graphics->busy)
3824         return ObjectBusy;
3825
3826     graphics->smoothing = mode;
3827
3828     return Ok;
3829 }
3830
3831 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
3832 {
3833     TRACE("(%p, %d)\n", graphics, contrast);
3834
3835     if(!graphics)
3836         return InvalidParameter;
3837
3838     graphics->textcontrast = contrast;
3839
3840     return Ok;
3841 }
3842
3843 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
3844     TextRenderingHint hint)
3845 {
3846     TRACE("(%p, %d)\n", graphics, hint);
3847
3848     if(!graphics)
3849         return InvalidParameter;
3850
3851     if(graphics->busy)
3852         return ObjectBusy;
3853
3854     graphics->texthint = hint;
3855
3856     return Ok;
3857 }
3858
3859 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3860 {
3861     TRACE("(%p, %p)\n", graphics, matrix);
3862
3863     if(!graphics || !matrix)
3864         return InvalidParameter;
3865
3866     if(graphics->busy)
3867         return ObjectBusy;
3868
3869     GdipDeleteMatrix(graphics->worldtrans);
3870     return GdipCloneMatrix(matrix, &graphics->worldtrans);
3871 }
3872
3873 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
3874     REAL dy, GpMatrixOrder order)
3875 {
3876     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
3877
3878     if(!graphics)
3879         return InvalidParameter;
3880
3881     if(graphics->busy)
3882         return ObjectBusy;
3883
3884     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
3885 }
3886
3887 /*****************************************************************************
3888  * GdipSetClipHrgn [GDIPLUS.@]
3889  */
3890 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
3891 {
3892     GpRegion *region;
3893     GpStatus status;
3894
3895     TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
3896
3897     if(!graphics)
3898         return InvalidParameter;
3899
3900     status = GdipCreateRegionHrgn(hrgn, &region);
3901     if(status != Ok)
3902         return status;
3903
3904     status = GdipSetClipRegion(graphics, region, mode);
3905
3906     GdipDeleteRegion(region);
3907     return status;
3908 }
3909
3910 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
3911 {
3912     TRACE("(%p, %p, %d)\n", graphics, path, mode);
3913
3914     if(!graphics)
3915         return InvalidParameter;
3916
3917     if(graphics->busy)
3918         return ObjectBusy;
3919
3920     return GdipCombineRegionPath(graphics->clip, path, mode);
3921 }
3922
3923 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
3924                                     REAL width, REAL height,
3925                                     CombineMode mode)
3926 {
3927     GpRectF rect;
3928
3929     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
3930
3931     if(!graphics)
3932         return InvalidParameter;
3933
3934     if(graphics->busy)
3935         return ObjectBusy;
3936
3937     rect.X = x;
3938     rect.Y = y;
3939     rect.Width  = width;
3940     rect.Height = height;
3941
3942     return GdipCombineRegionRect(graphics->clip, &rect, mode);
3943 }
3944
3945 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
3946                                      INT width, INT height,
3947                                      CombineMode mode)
3948 {
3949     TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
3950
3951     if(!graphics)
3952         return InvalidParameter;
3953
3954     if(graphics->busy)
3955         return ObjectBusy;
3956
3957     return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
3958 }
3959
3960 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
3961                                       CombineMode mode)
3962 {
3963     TRACE("(%p, %p, %d)\n", graphics, region, mode);
3964
3965     if(!graphics || !region)
3966         return InvalidParameter;
3967
3968     if(graphics->busy)
3969         return ObjectBusy;
3970
3971     return GdipCombineRegionRegion(graphics->clip, region, mode);
3972 }
3973
3974 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
3975     UINT limitDpi)
3976 {
3977     static int calls;
3978
3979     if(!(calls++))
3980         FIXME("not implemented\n");
3981
3982     return NotImplemented;
3983 }
3984
3985 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
3986     INT count)
3987 {
3988     INT save_state;
3989     POINT *pti;
3990
3991     TRACE("(%p, %p, %d)\n", graphics, points, count);
3992
3993     if(!graphics || !pen || count<=0)
3994         return InvalidParameter;
3995
3996     if(graphics->busy)
3997         return ObjectBusy;
3998
3999     pti = GdipAlloc(sizeof(POINT) * count);
4000
4001     save_state = prepare_dc(graphics, pen);
4002     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
4003
4004     transform_and_round_points(graphics, pti, (GpPointF*)points, count);
4005     Polygon(graphics->hdc, pti, count);
4006
4007     restore_dc(graphics, save_state);
4008     GdipFree(pti);
4009
4010     return Ok;
4011 }
4012
4013 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
4014     INT count)
4015 {
4016     GpStatus ret;
4017     GpPointF *ptf;
4018     INT i;
4019
4020     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
4021
4022     if(count<=0)    return InvalidParameter;
4023     ptf = GdipAlloc(sizeof(GpPointF) * count);
4024
4025     for(i = 0;i < count; i++){
4026         ptf[i].X = (REAL)points[i].X;
4027         ptf[i].Y = (REAL)points[i].Y;
4028     }
4029
4030     ret = GdipDrawPolygon(graphics,pen,ptf,count);
4031     GdipFree(ptf);
4032
4033     return ret;
4034 }
4035
4036 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
4037 {
4038     TRACE("(%p, %p)\n", graphics, dpi);
4039
4040     if(!graphics || !dpi)
4041         return InvalidParameter;
4042
4043     if(graphics->busy)
4044         return ObjectBusy;
4045
4046     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
4047
4048     return Ok;
4049 }
4050
4051 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
4052 {
4053     TRACE("(%p, %p)\n", graphics, dpi);
4054
4055     if(!graphics || !dpi)
4056         return InvalidParameter;
4057
4058     if(graphics->busy)
4059         return ObjectBusy;
4060
4061     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
4062
4063     return Ok;
4064 }
4065
4066 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
4067     GpMatrixOrder order)
4068 {
4069     GpMatrix m;
4070     GpStatus ret;
4071
4072     TRACE("(%p, %p, %d)\n", graphics, matrix, order);
4073
4074     if(!graphics || !matrix)
4075         return InvalidParameter;
4076
4077     if(graphics->busy)
4078         return ObjectBusy;
4079
4080     m = *(graphics->worldtrans);
4081
4082     ret = GdipMultiplyMatrix(&m, matrix, order);
4083     if(ret == Ok)
4084         *(graphics->worldtrans) = m;
4085
4086     return ret;
4087 }
4088
4089 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
4090 {
4091     TRACE("(%p, %p)\n", graphics, hdc);
4092
4093     if(!graphics || !hdc)
4094         return InvalidParameter;
4095
4096     if(graphics->busy)
4097         return ObjectBusy;
4098
4099     *hdc = graphics->hdc;
4100     graphics->busy = TRUE;
4101
4102     return Ok;
4103 }
4104
4105 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
4106 {
4107     TRACE("(%p, %p)\n", graphics, hdc);
4108
4109     if(!graphics)
4110         return InvalidParameter;
4111
4112     if(graphics->hdc != hdc || !(graphics->busy))
4113         return InvalidParameter;
4114
4115     graphics->busy = FALSE;
4116
4117     return Ok;
4118 }
4119
4120 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
4121 {
4122     GpRegion *clip;
4123     GpStatus status;
4124
4125     TRACE("(%p, %p)\n", graphics, region);
4126
4127     if(!graphics || !region)
4128         return InvalidParameter;
4129
4130     if(graphics->busy)
4131         return ObjectBusy;
4132
4133     if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
4134         return status;
4135
4136     /* free everything except root node and header */
4137     delete_element(&region->node);
4138     memcpy(region, clip, sizeof(GpRegion));
4139
4140     return Ok;
4141 }
4142
4143 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
4144                                         GpCoordinateSpace src_space, GpPointF *points, INT count)
4145 {
4146     GpMatrix *matrix;
4147     GpStatus stat;
4148     REAL unitscale;
4149
4150     if(!graphics || !points || count <= 0)
4151         return InvalidParameter;
4152
4153     if(graphics->busy)
4154         return ObjectBusy;
4155
4156     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4157
4158     if (src_space == dst_space) return Ok;
4159
4160     stat = GdipCreateMatrix(&matrix);
4161     if (stat == Ok)
4162     {
4163         unitscale = convert_unit(graphics->hdc, graphics->unit);
4164
4165         if(graphics->unit != UnitDisplay)
4166             unitscale *= graphics->scale;
4167
4168         /* transform from src_space to CoordinateSpacePage */
4169         switch (src_space)
4170         {
4171         case CoordinateSpaceWorld:
4172             GdipMultiplyMatrix(matrix, graphics->worldtrans, MatrixOrderAppend);
4173             break;
4174         case CoordinateSpacePage:
4175             break;
4176         case CoordinateSpaceDevice:
4177             GdipScaleMatrix(matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
4178             break;
4179         }
4180
4181         /* transform from CoordinateSpacePage to dst_space */
4182         switch (dst_space)
4183         {
4184         case CoordinateSpaceWorld:
4185             {
4186                 GpMatrix *inverted_transform;
4187                 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
4188                 if (stat == Ok)
4189                 {
4190                     stat = GdipInvertMatrix(inverted_transform);
4191                     if (stat == Ok)
4192                         GdipMultiplyMatrix(matrix, inverted_transform, MatrixOrderAppend);
4193                     GdipDeleteMatrix(inverted_transform);
4194                 }
4195                 break;
4196             }
4197         case CoordinateSpacePage:
4198             break;
4199         case CoordinateSpaceDevice:
4200             GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
4201             break;
4202         }
4203
4204         if (stat == Ok)
4205             stat = GdipTransformMatrixPoints(matrix, points, count);
4206
4207         GdipDeleteMatrix(matrix);
4208     }
4209
4210     return stat;
4211 }
4212
4213 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
4214                                          GpCoordinateSpace src_space, GpPoint *points, INT count)
4215 {
4216     GpPointF *pointsF;
4217     GpStatus ret;
4218     INT i;
4219
4220     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4221
4222     if(count <= 0)
4223         return InvalidParameter;
4224
4225     pointsF = GdipAlloc(sizeof(GpPointF) * count);
4226     if(!pointsF)
4227         return OutOfMemory;
4228
4229     for(i = 0; i < count; i++){
4230         pointsF[i].X = (REAL)points[i].X;
4231         pointsF[i].Y = (REAL)points[i].Y;
4232     }
4233
4234     ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
4235
4236     if(ret == Ok)
4237         for(i = 0; i < count; i++){
4238             points[i].X = roundr(pointsF[i].X);
4239             points[i].Y = roundr(pointsF[i].Y);
4240         }
4241     GdipFree(pointsF);
4242
4243     return ret;
4244 }
4245
4246 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
4247 {
4248     FIXME("\n");
4249
4250     return NULL;
4251 }
4252
4253 /*****************************************************************************
4254  * GdipTranslateClip [GDIPLUS.@]
4255  */
4256 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
4257 {
4258     TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
4259
4260     if(!graphics)
4261         return InvalidParameter;
4262
4263     if(graphics->busy)
4264         return ObjectBusy;
4265
4266     return GdipTranslateRegion(graphics->clip, dx, dy);
4267 }
4268
4269 /*****************************************************************************
4270  * GdipTranslateClipI [GDIPLUS.@]
4271  */
4272 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
4273 {
4274     TRACE("(%p, %d, %d)\n", graphics, dx, dy);
4275
4276     if(!graphics)
4277         return InvalidParameter;
4278
4279     if(graphics->busy)
4280         return ObjectBusy;
4281
4282     return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
4283 }
4284
4285
4286 /*****************************************************************************
4287  * GdipMeasureDriverString [GDIPLUS.@]
4288  */
4289 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4290                                             GDIPCONST GpFont *font, GDIPCONST PointF *positions,
4291                                             INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
4292 {
4293     FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
4294     return NotImplemented;
4295 }
4296
4297 /*****************************************************************************
4298  * GdipDrawDriverString [GDIPLUS.@]
4299  */
4300 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4301                                          GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
4302                                          GDIPCONST PointF *positions, INT flags,
4303                                          GDIPCONST GpMatrix *matrix )
4304 {
4305     FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
4306     return NotImplemented;
4307 }
4308
4309 /*****************************************************************************
4310  * GdipRecordMetafileI [GDIPLUS.@]
4311  */
4312 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
4313                                         MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
4314 {
4315     FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
4316     return NotImplemented;
4317 }