jscript: Added String_fromCharCode implementation.
[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 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1044 {
1045     TRACE("(%p, %p)\n", hdc, graphics);
1046
1047     return GdipCreateFromHDC2(hdc, NULL, graphics);
1048 }
1049
1050 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1051 {
1052     GpStatus retval;
1053
1054     TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1055
1056     if(hDevice != NULL) {
1057         FIXME("Don't know how to handle parameter hDevice\n");
1058         return NotImplemented;
1059     }
1060
1061     if(hdc == NULL)
1062         return OutOfMemory;
1063
1064     if(graphics == NULL)
1065         return InvalidParameter;
1066
1067     *graphics = GdipAlloc(sizeof(GpGraphics));
1068     if(!*graphics)  return OutOfMemory;
1069
1070     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1071         GdipFree(*graphics);
1072         return retval;
1073     }
1074
1075     if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1076         GdipFree((*graphics)->worldtrans);
1077         GdipFree(*graphics);
1078         return retval;
1079     }
1080
1081     (*graphics)->hdc = hdc;
1082     (*graphics)->hwnd = WindowFromDC(hdc);
1083     (*graphics)->owndc = FALSE;
1084     (*graphics)->smoothing = SmoothingModeDefault;
1085     (*graphics)->compqual = CompositingQualityDefault;
1086     (*graphics)->interpolation = InterpolationModeDefault;
1087     (*graphics)->pixeloffset = PixelOffsetModeDefault;
1088     (*graphics)->compmode = CompositingModeSourceOver;
1089     (*graphics)->unit = UnitDisplay;
1090     (*graphics)->scale = 1.0;
1091     (*graphics)->busy = FALSE;
1092     (*graphics)->textcontrast = 4;
1093     list_init(&(*graphics)->containers);
1094     (*graphics)->contid = 0;
1095
1096     return Ok;
1097 }
1098
1099 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1100 {
1101     GpStatus ret;
1102     HDC hdc;
1103
1104     TRACE("(%p, %p)\n", hwnd, graphics);
1105
1106     hdc = GetDC(hwnd);
1107
1108     if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1109     {
1110         ReleaseDC(hwnd, hdc);
1111         return ret;
1112     }
1113
1114     (*graphics)->hwnd = hwnd;
1115     (*graphics)->owndc = TRUE;
1116
1117     return Ok;
1118 }
1119
1120 /* FIXME: no icm handling */
1121 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1122 {
1123     TRACE("(%p, %p)\n", hwnd, graphics);
1124
1125     return GdipCreateFromHWND(hwnd, graphics);
1126 }
1127
1128 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1129     GpMetafile **metafile)
1130 {
1131     static int calls;
1132
1133     if(!hemf || !metafile)
1134         return InvalidParameter;
1135
1136     if(!(calls++))
1137         FIXME("not implemented\n");
1138
1139     return NotImplemented;
1140 }
1141
1142 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1143     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1144 {
1145     IStream *stream = NULL;
1146     UINT read;
1147     BYTE* copy;
1148     HENHMETAFILE hemf;
1149     GpStatus retval = GenericError;
1150
1151     TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1152
1153     if(!hwmf || !metafile || !placeable)
1154         return InvalidParameter;
1155
1156     *metafile = NULL;
1157     read = GetMetaFileBitsEx(hwmf, 0, NULL);
1158     if(!read)
1159         return GenericError;
1160     copy = GdipAlloc(read);
1161     GetMetaFileBitsEx(hwmf, read, copy);
1162
1163     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1164     GdipFree(copy);
1165
1166     read = GetEnhMetaFileBits(hemf, 0, NULL);
1167     copy = GdipAlloc(read);
1168     GetEnhMetaFileBits(hemf, read, copy);
1169     DeleteEnhMetaFile(hemf);
1170
1171     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1172         ERR("could not make stream\n");
1173         GdipFree(copy);
1174         goto err;
1175     }
1176
1177     *metafile = GdipAlloc(sizeof(GpMetafile));
1178     if(!*metafile){
1179         retval = OutOfMemory;
1180         goto err;
1181     }
1182
1183     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1184         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1185         goto err;
1186
1187
1188     (*metafile)->image.type = ImageTypeMetafile;
1189     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1190     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
1191     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1192                     - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
1193     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1194                    - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
1195     (*metafile)->unit = UnitInch;
1196
1197     if(delete)
1198         DeleteMetaFile(hwmf);
1199
1200     return Ok;
1201
1202 err:
1203     GdipFree(*metafile);
1204     IStream_Release(stream);
1205     return retval;
1206 }
1207
1208 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1209     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1210 {
1211     HMETAFILE hmf = GetMetaFileW(file);
1212
1213     TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1214
1215     if(!hmf) return InvalidParameter;
1216
1217     return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1218 }
1219
1220 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1221     GpMetafile **metafile)
1222 {
1223     FIXME("(%p, %p): stub\n", file, metafile);
1224     return NotImplemented;
1225 }
1226
1227 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1228     GpMetafile **metafile)
1229 {
1230     FIXME("(%p, %p): stub\n", stream, metafile);
1231     return NotImplemented;
1232 }
1233
1234 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1235     UINT access, IStream **stream)
1236 {
1237     DWORD dwMode;
1238     HRESULT ret;
1239
1240     TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1241
1242     if(!stream || !filename)
1243         return InvalidParameter;
1244
1245     if(access & GENERIC_WRITE)
1246         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1247     else if(access & GENERIC_READ)
1248         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1249     else
1250         return InvalidParameter;
1251
1252     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1253
1254     return hresult_to_status(ret);
1255 }
1256
1257 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1258 {
1259     GraphicsContainerItem *cont, *next;
1260     TRACE("(%p)\n", graphics);
1261
1262     if(!graphics) return InvalidParameter;
1263     if(graphics->busy) return ObjectBusy;
1264
1265     if(graphics->owndc)
1266         ReleaseDC(graphics->hwnd, graphics->hdc);
1267
1268     LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1269         list_remove(&cont->entry);
1270         delete_container(cont);
1271     }
1272
1273     GdipDeleteRegion(graphics->clip);
1274     GdipDeleteMatrix(graphics->worldtrans);
1275     GdipFree(graphics);
1276
1277     return Ok;
1278 }
1279
1280 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1281     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1282 {
1283     INT save_state, num_pts;
1284     GpPointF points[MAX_ARC_PTS];
1285     GpStatus retval;
1286
1287     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1288           width, height, startAngle, sweepAngle);
1289
1290     if(!graphics || !pen || width <= 0 || height <= 0)
1291         return InvalidParameter;
1292
1293     if(graphics->busy)
1294         return ObjectBusy;
1295
1296     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1297
1298     save_state = prepare_dc(graphics, pen);
1299
1300     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1301
1302     restore_dc(graphics, save_state);
1303
1304     return retval;
1305 }
1306
1307 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1308     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1309 {
1310     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1311           width, height, startAngle, sweepAngle);
1312
1313     return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1314 }
1315
1316 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1317     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1318 {
1319     INT save_state;
1320     GpPointF pt[4];
1321     GpStatus retval;
1322
1323     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1324           x2, y2, x3, y3, x4, y4);
1325
1326     if(!graphics || !pen)
1327         return InvalidParameter;
1328
1329     if(graphics->busy)
1330         return ObjectBusy;
1331
1332     pt[0].X = x1;
1333     pt[0].Y = y1;
1334     pt[1].X = x2;
1335     pt[1].Y = y2;
1336     pt[2].X = x3;
1337     pt[2].Y = y3;
1338     pt[3].X = x4;
1339     pt[3].Y = y4;
1340
1341     save_state = prepare_dc(graphics, pen);
1342
1343     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1344
1345     restore_dc(graphics, save_state);
1346
1347     return retval;
1348 }
1349
1350 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1351     INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1352 {
1353     INT save_state;
1354     GpPointF pt[4];
1355     GpStatus retval;
1356
1357     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1358           x2, y2, x3, y3, x4, y4);
1359
1360     if(!graphics || !pen)
1361         return InvalidParameter;
1362
1363     if(graphics->busy)
1364         return ObjectBusy;
1365
1366     pt[0].X = x1;
1367     pt[0].Y = y1;
1368     pt[1].X = x2;
1369     pt[1].Y = y2;
1370     pt[2].X = x3;
1371     pt[2].Y = y3;
1372     pt[3].X = x4;
1373     pt[3].Y = y4;
1374
1375     save_state = prepare_dc(graphics, pen);
1376
1377     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1378
1379     restore_dc(graphics, save_state);
1380
1381     return retval;
1382 }
1383
1384 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1385     GDIPCONST GpPointF *points, INT count)
1386 {
1387     INT i;
1388     GpStatus ret;
1389
1390     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1391
1392     if(!graphics || !pen || !points || (count <= 0))
1393         return InvalidParameter;
1394
1395     if(graphics->busy)
1396         return ObjectBusy;
1397
1398     for(i = 0; i < floor(count / 4); i++){
1399         ret = GdipDrawBezier(graphics, pen,
1400                              points[4*i].X, points[4*i].Y,
1401                              points[4*i + 1].X, points[4*i + 1].Y,
1402                              points[4*i + 2].X, points[4*i + 2].Y,
1403                              points[4*i + 3].X, points[4*i + 3].Y);
1404         if(ret != Ok)
1405             return ret;
1406     }
1407
1408     return Ok;
1409 }
1410
1411 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1412     GDIPCONST GpPoint *points, INT count)
1413 {
1414     GpPointF *pts;
1415     GpStatus ret;
1416     INT i;
1417
1418     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1419
1420     if(!graphics || !pen || !points || (count <= 0))
1421         return InvalidParameter;
1422
1423     if(graphics->busy)
1424         return ObjectBusy;
1425
1426     pts = GdipAlloc(sizeof(GpPointF) * count);
1427     if(!pts)
1428         return OutOfMemory;
1429
1430     for(i = 0; i < count; i++){
1431         pts[i].X = (REAL)points[i].X;
1432         pts[i].Y = (REAL)points[i].Y;
1433     }
1434
1435     ret = GdipDrawBeziers(graphics,pen,pts,count);
1436
1437     GdipFree(pts);
1438
1439     return ret;
1440 }
1441
1442 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1443     GDIPCONST GpPointF *points, INT count)
1444 {
1445     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1446
1447     return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1448 }
1449
1450 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1451     GDIPCONST GpPoint *points, INT count)
1452 {
1453     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1454
1455     return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1456 }
1457
1458 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1459     GDIPCONST GpPointF *points, INT count, REAL tension)
1460 {
1461     GpPath *path;
1462     GpStatus stat;
1463
1464     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1465
1466     if(!graphics || !pen || !points || count <= 0)
1467         return InvalidParameter;
1468
1469     if(graphics->busy)
1470         return ObjectBusy;
1471
1472     if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1473         return stat;
1474
1475     stat = GdipAddPathClosedCurve2(path, points, count, tension);
1476     if(stat != Ok){
1477         GdipDeletePath(path);
1478         return stat;
1479     }
1480
1481     stat = GdipDrawPath(graphics, pen, path);
1482
1483     GdipDeletePath(path);
1484
1485     return stat;
1486 }
1487
1488 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1489     GDIPCONST GpPoint *points, INT count, REAL tension)
1490 {
1491     GpPointF *ptf;
1492     GpStatus stat;
1493     INT i;
1494
1495     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1496
1497     if(!points || count <= 0)
1498         return InvalidParameter;
1499
1500     ptf = GdipAlloc(sizeof(GpPointF)*count);
1501     if(!ptf)
1502         return OutOfMemory;
1503
1504     for(i = 0; i < count; i++){
1505         ptf[i].X = (REAL)points[i].X;
1506         ptf[i].Y = (REAL)points[i].Y;
1507     }
1508
1509     stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1510
1511     GdipFree(ptf);
1512
1513     return stat;
1514 }
1515
1516 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1517     GDIPCONST GpPointF *points, INT count)
1518 {
1519     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1520
1521     return GdipDrawCurve2(graphics,pen,points,count,1.0);
1522 }
1523
1524 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1525     GDIPCONST GpPoint *points, INT count)
1526 {
1527     GpPointF *pointsF;
1528     GpStatus ret;
1529     INT i;
1530
1531     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1532
1533     if(!points)
1534         return InvalidParameter;
1535
1536     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1537     if(!pointsF)
1538         return OutOfMemory;
1539
1540     for(i = 0; i < count; i++){
1541         pointsF[i].X = (REAL)points[i].X;
1542         pointsF[i].Y = (REAL)points[i].Y;
1543     }
1544
1545     ret = GdipDrawCurve(graphics,pen,pointsF,count);
1546     GdipFree(pointsF);
1547
1548     return ret;
1549 }
1550
1551 /* Approximates cardinal spline with Bezier curves. */
1552 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1553     GDIPCONST GpPointF *points, INT count, REAL tension)
1554 {
1555     /* PolyBezier expects count*3-2 points. */
1556     INT i, len_pt = count*3-2, save_state;
1557     GpPointF *pt;
1558     REAL x1, x2, y1, y2;
1559     GpStatus retval;
1560
1561     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1562
1563     if(!graphics || !pen)
1564         return InvalidParameter;
1565
1566     if(graphics->busy)
1567         return ObjectBusy;
1568
1569     if(count < 2)
1570         return InvalidParameter;
1571
1572     pt = GdipAlloc(len_pt * sizeof(GpPointF));
1573     if(!pt)
1574         return OutOfMemory;
1575
1576     tension = tension * TENSION_CONST;
1577
1578     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1579         tension, &x1, &y1);
1580
1581     pt[0].X = points[0].X;
1582     pt[0].Y = points[0].Y;
1583     pt[1].X = x1;
1584     pt[1].Y = y1;
1585
1586     for(i = 0; i < count-2; i++){
1587         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1588
1589         pt[3*i+2].X = x1;
1590         pt[3*i+2].Y = y1;
1591         pt[3*i+3].X = points[i+1].X;
1592         pt[3*i+3].Y = points[i+1].Y;
1593         pt[3*i+4].X = x2;
1594         pt[3*i+4].Y = y2;
1595     }
1596
1597     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1598         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1599
1600     pt[len_pt-2].X = x1;
1601     pt[len_pt-2].Y = y1;
1602     pt[len_pt-1].X = points[count-1].X;
1603     pt[len_pt-1].Y = points[count-1].Y;
1604
1605     save_state = prepare_dc(graphics, pen);
1606
1607     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1608
1609     GdipFree(pt);
1610     restore_dc(graphics, save_state);
1611
1612     return retval;
1613 }
1614
1615 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1616     GDIPCONST GpPoint *points, INT count, REAL tension)
1617 {
1618     GpPointF *pointsF;
1619     GpStatus ret;
1620     INT i;
1621
1622     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1623
1624     if(!points)
1625         return InvalidParameter;
1626
1627     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1628     if(!pointsF)
1629         return OutOfMemory;
1630
1631     for(i = 0; i < count; i++){
1632         pointsF[i].X = (REAL)points[i].X;
1633         pointsF[i].Y = (REAL)points[i].Y;
1634     }
1635
1636     ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1637     GdipFree(pointsF);
1638
1639     return ret;
1640 }
1641
1642 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
1643     GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
1644     REAL tension)
1645 {
1646     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1647
1648     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1649         return InvalidParameter;
1650     }
1651
1652     return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
1653 }
1654
1655 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
1656     GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
1657     REAL tension)
1658 {
1659     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1660
1661     if(count < 0){
1662         return OutOfMemory;
1663     }
1664
1665     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1666         return InvalidParameter;
1667     }
1668
1669     return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
1670 }
1671
1672 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1673     REAL y, REAL width, REAL height)
1674 {
1675     INT save_state;
1676     GpPointF ptf[2];
1677     POINT pti[2];
1678
1679     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1680
1681     if(!graphics || !pen)
1682         return InvalidParameter;
1683
1684     if(graphics->busy)
1685         return ObjectBusy;
1686
1687     ptf[0].X = x;
1688     ptf[0].Y = y;
1689     ptf[1].X = x + width;
1690     ptf[1].Y = y + height;
1691
1692     save_state = prepare_dc(graphics, pen);
1693     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1694
1695     transform_and_round_points(graphics, pti, ptf, 2);
1696
1697     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1698
1699     restore_dc(graphics, save_state);
1700
1701     return Ok;
1702 }
1703
1704 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1705     INT y, INT width, INT height)
1706 {
1707     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1708
1709     return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1710 }
1711
1712
1713 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1714 {
1715     TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1716
1717     /* IPicture::Render uses LONG coords */
1718     return GdipDrawImageI(graphics,image,roundr(x),roundr(y));
1719 }
1720
1721 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1722     INT y)
1723 {
1724     UINT width, height, srcw, srch;
1725
1726     TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1727
1728     if(!graphics || !image)
1729         return InvalidParameter;
1730
1731     GdipGetImageWidth(image, &width);
1732     GdipGetImageHeight(image, &height);
1733
1734     srcw = width * (((REAL) INCH_HIMETRIC) /
1735             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1736     srch = height * (((REAL) INCH_HIMETRIC) /
1737             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1738
1739     if(image->type != ImageTypeMetafile){
1740         y += height;
1741         height *= -1;
1742     }
1743
1744     IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1745                     0, 0, srcw, srch, NULL);
1746
1747     return Ok;
1748 }
1749
1750 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
1751     REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
1752     GpUnit srcUnit)
1753 {
1754     FIXME("(%p, %p, %f, %f, %f, %f, %f, %f, %d): stub\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1755     return NotImplemented;
1756 }
1757
1758 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
1759     INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
1760     GpUnit srcUnit)
1761 {
1762     FIXME("(%p, %p, %d, %d, %d, %d, %d, %d, %d): stub\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1763     return NotImplemented;
1764 }
1765
1766 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
1767     GDIPCONST GpPointF *dstpoints, INT count)
1768 {
1769     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1770     return NotImplemented;
1771 }
1772
1773 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
1774     GDIPCONST GpPoint *dstpoints, INT count)
1775 {
1776     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1777     return NotImplemented;
1778 }
1779
1780 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1781 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1782      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1783      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1784      DrawImageAbort callback, VOID * callbackData)
1785 {
1786     GpPointF ptf[3];
1787     POINT pti[3];
1788     REAL dx, dy;
1789
1790     TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
1791           count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1792           callbackData);
1793
1794     if(!graphics || !image || !points || count != 3)
1795          return InvalidParameter;
1796
1797     if(srcUnit == UnitInch)
1798         dx = dy = (REAL) INCH_HIMETRIC;
1799     else if(srcUnit == UnitPixel){
1800         dx = ((REAL) INCH_HIMETRIC) /
1801              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1802         dy = ((REAL) INCH_HIMETRIC) /
1803              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1804     }
1805     else
1806         return NotImplemented;
1807
1808     memcpy(ptf, points, 3 * sizeof(GpPointF));
1809     transform_and_round_points(graphics, pti, ptf, 3);
1810
1811     /* IPicture renders bitmaps with the y-axis reversed
1812      * FIXME: flipping for unknown image type might not be correct. */
1813     if(image->type != ImageTypeMetafile){
1814         INT temp;
1815         temp = pti[0].y;
1816         pti[0].y = pti[2].y;
1817         pti[2].y = temp;
1818     }
1819
1820     if(IPicture_Render(image->picture, graphics->hdc,
1821         pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1822         srcx * dx, srcy * dy,
1823         srcwidth * dx, srcheight * dy,
1824         NULL) != S_OK){
1825         if(callback)
1826             callback(callbackData);
1827         return GenericError;
1828     }
1829
1830     return Ok;
1831 }
1832
1833 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1834      GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1835      INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1836      DrawImageAbort callback, VOID * callbackData)
1837 {
1838     GpPointF pointsF[3];
1839     INT i;
1840
1841     TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
1842           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1843           callbackData);
1844
1845     if(!points || count!=3)
1846         return InvalidParameter;
1847
1848     for(i = 0; i < count; i++){
1849         pointsF[i].X = (REAL)points[i].X;
1850         pointsF[i].Y = (REAL)points[i].Y;
1851     }
1852
1853     return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1854                                    (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1855                                    callback, callbackData);
1856 }
1857
1858 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1859     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1860     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1861     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1862     VOID * callbackData)
1863 {
1864     GpPointF points[3];
1865
1866     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
1867           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1868           srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1869
1870     points[0].X = dstx;
1871     points[0].Y = dsty;
1872     points[1].X = dstx + dstwidth;
1873     points[1].Y = dsty;
1874     points[2].X = dstx;
1875     points[2].Y = dsty + dstheight;
1876
1877     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1878                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1879 }
1880
1881 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1882         INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1883         INT srcwidth, INT srcheight, GpUnit srcUnit,
1884         GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1885         VOID * callbackData)
1886 {
1887     GpPointF points[3];
1888
1889     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
1890           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1891           srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1892
1893     points[0].X = dstx;
1894     points[0].Y = dsty;
1895     points[1].X = dstx + dstwidth;
1896     points[1].Y = dsty;
1897     points[2].X = dstx;
1898     points[2].Y = dsty + dstheight;
1899
1900     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1901                srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1902 }
1903
1904 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1905     REAL x, REAL y, REAL width, REAL height)
1906 {
1907     RectF bounds;
1908     GpUnit unit;
1909     GpStatus ret;
1910
1911     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
1912
1913     if(!graphics || !image)
1914         return InvalidParameter;
1915
1916     ret = GdipGetImageBounds(image, &bounds, &unit);
1917     if(ret != Ok)
1918         return ret;
1919
1920     return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1921                                  bounds.X, bounds.Y, bounds.Width, bounds.Height,
1922                                  unit, NULL, NULL, NULL);
1923 }
1924
1925 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1926     INT x, INT y, INT width, INT height)
1927 {
1928     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
1929
1930     return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1931 }
1932
1933 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1934     REAL y1, REAL x2, REAL y2)
1935 {
1936     INT save_state;
1937     GpPointF pt[2];
1938     GpStatus retval;
1939
1940     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
1941
1942     if(!pen || !graphics)
1943         return InvalidParameter;
1944
1945     if(graphics->busy)
1946         return ObjectBusy;
1947
1948     pt[0].X = x1;
1949     pt[0].Y = y1;
1950     pt[1].X = x2;
1951     pt[1].Y = y2;
1952
1953     save_state = prepare_dc(graphics, pen);
1954
1955     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1956
1957     restore_dc(graphics, save_state);
1958
1959     return retval;
1960 }
1961
1962 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1963     INT y1, INT x2, INT y2)
1964 {
1965     INT save_state;
1966     GpPointF pt[2];
1967     GpStatus retval;
1968
1969     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
1970
1971     if(!pen || !graphics)
1972         return InvalidParameter;
1973
1974     if(graphics->busy)
1975         return ObjectBusy;
1976
1977     pt[0].X = (REAL)x1;
1978     pt[0].Y = (REAL)y1;
1979     pt[1].X = (REAL)x2;
1980     pt[1].Y = (REAL)y2;
1981
1982     save_state = prepare_dc(graphics, pen);
1983
1984     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1985
1986     restore_dc(graphics, save_state);
1987
1988     return retval;
1989 }
1990
1991 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1992     GpPointF *points, INT count)
1993 {
1994     INT save_state;
1995     GpStatus retval;
1996
1997     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1998
1999     if(!pen || !graphics || (count < 2))
2000         return InvalidParameter;
2001
2002     if(graphics->busy)
2003         return ObjectBusy;
2004
2005     save_state = prepare_dc(graphics, pen);
2006
2007     retval = draw_polyline(graphics, pen, points, count, TRUE);
2008
2009     restore_dc(graphics, save_state);
2010
2011     return retval;
2012 }
2013
2014 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2015     GpPoint *points, INT count)
2016 {
2017     INT save_state;
2018     GpStatus retval;
2019     GpPointF *ptf = NULL;
2020     int i;
2021
2022     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2023
2024     if(!pen || !graphics || (count < 2))
2025         return InvalidParameter;
2026
2027     if(graphics->busy)
2028         return ObjectBusy;
2029
2030     ptf = GdipAlloc(count * sizeof(GpPointF));
2031     if(!ptf) return OutOfMemory;
2032
2033     for(i = 0; i < count; i ++){
2034         ptf[i].X = (REAL) points[i].X;
2035         ptf[i].Y = (REAL) points[i].Y;
2036     }
2037
2038     save_state = prepare_dc(graphics, pen);
2039
2040     retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2041
2042     restore_dc(graphics, save_state);
2043
2044     GdipFree(ptf);
2045     return retval;
2046 }
2047
2048 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2049 {
2050     INT save_state;
2051     GpStatus retval;
2052
2053     TRACE("(%p, %p, %p)\n", graphics, pen, path);
2054
2055     if(!pen || !graphics)
2056         return InvalidParameter;
2057
2058     if(graphics->busy)
2059         return ObjectBusy;
2060
2061     save_state = prepare_dc(graphics, pen);
2062
2063     retval = draw_poly(graphics, pen, path->pathdata.Points,
2064                        path->pathdata.Types, path->pathdata.Count, TRUE);
2065
2066     restore_dc(graphics, save_state);
2067
2068     return retval;
2069 }
2070
2071 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2072     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2073 {
2074     INT save_state;
2075
2076     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2077             width, height, startAngle, sweepAngle);
2078
2079     if(!graphics || !pen)
2080         return InvalidParameter;
2081
2082     if(graphics->busy)
2083         return ObjectBusy;
2084
2085     save_state = prepare_dc(graphics, pen);
2086     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2087
2088     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2089
2090     restore_dc(graphics, save_state);
2091
2092     return Ok;
2093 }
2094
2095 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
2096     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2097 {
2098     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2099             width, height, startAngle, sweepAngle);
2100
2101     return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2102 }
2103
2104 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
2105     REAL y, REAL width, REAL height)
2106 {
2107     INT save_state;
2108     GpPointF ptf[4];
2109     POINT pti[4];
2110
2111     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2112
2113     if(!pen || !graphics)
2114         return InvalidParameter;
2115
2116     if(graphics->busy)
2117         return ObjectBusy;
2118
2119     ptf[0].X = x;
2120     ptf[0].Y = y;
2121     ptf[1].X = x + width;
2122     ptf[1].Y = y;
2123     ptf[2].X = x + width;
2124     ptf[2].Y = y + height;
2125     ptf[3].X = x;
2126     ptf[3].Y = y + height;
2127
2128     save_state = prepare_dc(graphics, pen);
2129     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2130
2131     transform_and_round_points(graphics, pti, ptf, 4);
2132     Polygon(graphics->hdc, pti, 4);
2133
2134     restore_dc(graphics, save_state);
2135
2136     return Ok;
2137 }
2138
2139 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
2140     INT y, INT width, INT height)
2141 {
2142     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2143
2144     return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2145 }
2146
2147 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
2148     GDIPCONST GpRectF* rects, INT count)
2149 {
2150     GpPointF *ptf;
2151     POINT *pti;
2152     INT save_state, i;
2153
2154     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2155
2156     if(!graphics || !pen || !rects || count < 1)
2157         return InvalidParameter;
2158
2159     if(graphics->busy)
2160         return ObjectBusy;
2161
2162     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
2163     pti = GdipAlloc(4 * count * sizeof(POINT));
2164
2165     if(!ptf || !pti){
2166         GdipFree(ptf);
2167         GdipFree(pti);
2168         return OutOfMemory;
2169     }
2170
2171     for(i = 0; i < count; i++){
2172         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
2173         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
2174         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
2175         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
2176     }
2177
2178     save_state = prepare_dc(graphics, pen);
2179     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2180
2181     transform_and_round_points(graphics, pti, ptf, 4 * count);
2182
2183     for(i = 0; i < count; i++)
2184         Polygon(graphics->hdc, &pti[4 * i], 4);
2185
2186     restore_dc(graphics, save_state);
2187
2188     GdipFree(ptf);
2189     GdipFree(pti);
2190
2191     return Ok;
2192 }
2193
2194 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
2195     GDIPCONST GpRect* rects, INT count)
2196 {
2197     GpRectF *rectsF;
2198     GpStatus ret;
2199     INT i;
2200
2201     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2202
2203     if(!rects || count<=0)
2204         return InvalidParameter;
2205
2206     rectsF = GdipAlloc(sizeof(GpRectF) * count);
2207     if(!rectsF)
2208         return OutOfMemory;
2209
2210     for(i = 0;i < count;i++){
2211         rectsF[i].X      = (REAL)rects[i].X;
2212         rectsF[i].Y      = (REAL)rects[i].Y;
2213         rectsF[i].Width  = (REAL)rects[i].Width;
2214         rectsF[i].Height = (REAL)rects[i].Height;
2215     }
2216
2217     ret = GdipDrawRectangles(graphics, pen, rectsF, count);
2218     GdipFree(rectsF);
2219
2220     return ret;
2221 }
2222
2223 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
2224     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
2225     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
2226 {
2227     HRGN rgn = NULL;
2228     HFONT gdifont;
2229     LOGFONTW lfw;
2230     TEXTMETRICW textmet;
2231     GpPointF pt[2], rectcpy[4];
2232     POINT corners[4];
2233     WCHAR* stringdup;
2234     REAL angle, ang_cos, ang_sin, rel_width, rel_height;
2235     INT sum = 0, height = 0, offsety = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
2236         nheight;
2237     SIZE size;
2238     POINT drawbase;
2239     UINT drawflags;
2240     RECT drawcoord;
2241
2242     TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
2243         length, font, debugstr_rectf(rect), format, brush);
2244
2245     if(!graphics || !string || !font || !brush || !rect)
2246         return InvalidParameter;
2247
2248     if((brush->bt != BrushTypeSolidColor)){
2249         FIXME("not implemented for given parameters\n");
2250         return NotImplemented;
2251     }
2252
2253     if(format){
2254         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2255
2256         /* Should be no need to explicitly test for StringAlignmentNear as
2257          * that is default behavior if no alignment is passed. */
2258         if(format->vertalign != StringAlignmentNear){
2259             RectF bounds;
2260             GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
2261
2262             if(format->vertalign == StringAlignmentCenter)
2263                 offsety = (rect->Height - bounds.Height) / 2;
2264             else if(format->vertalign == StringAlignmentFar)
2265                 offsety = (rect->Height - bounds.Height);
2266         }
2267     }
2268
2269     if(length == -1) length = lstrlenW(string);
2270
2271     stringdup = GdipAlloc(length * sizeof(WCHAR));
2272     if(!stringdup) return OutOfMemory;
2273
2274     save_state = SaveDC(graphics->hdc);
2275     SetBkMode(graphics->hdc, TRANSPARENT);
2276     SetTextColor(graphics->hdc, brush->lb.lbColor);
2277
2278     rectcpy[3].X = rectcpy[0].X = rect->X;
2279     rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
2280     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
2281     rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
2282     transform_and_round_points(graphics, corners, rectcpy, 4);
2283
2284     if (roundr(rect->Width) == 0)
2285     {
2286         rel_width = 1.0;
2287         nwidth = INT_MAX;
2288     }
2289     else
2290     {
2291         rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
2292                          (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
2293                          / rect->Width;
2294         nwidth = roundr(rel_width * rect->Width);
2295     }
2296
2297     if (roundr(rect->Height) == 0)
2298     {
2299         rel_height = 1.0;
2300         nheight = INT_MAX;
2301     }
2302     else
2303     {
2304         rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
2305                           (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
2306                           / rect->Height;
2307         nheight = roundr(rel_height * rect->Height);
2308     }
2309
2310     if (roundr(rect->Width) != 0 && roundr(rect->Height) != 0)
2311     {
2312         /* FIXME: If only the width or only the height is 0, we should probably still clip */
2313         rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
2314         SelectClipRgn(graphics->hdc, rgn);
2315     }
2316
2317     /* Use gdi to find the font, then perform transformations on it (height,
2318      * width, angle). */
2319     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2320     GetTextMetricsW(graphics->hdc, &textmet);
2321     lfw = font->lfw;
2322
2323     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
2324     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
2325
2326     pt[0].X = 0.0;
2327     pt[0].Y = 0.0;
2328     pt[1].X = 1.0;
2329     pt[1].Y = 0.0;
2330     GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
2331     angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2332     ang_cos = cos(angle);
2333     ang_sin = sin(angle);
2334     lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
2335
2336     gdifont = CreateFontIndirectW(&lfw);
2337     DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
2338
2339     for(i = 0, j = 0; i < length; i++){
2340         if(!isprintW(string[i]) && (string[i] != '\n'))
2341             continue;
2342
2343         stringdup[j] = string[i];
2344         j++;
2345     }
2346
2347     length = j;
2348
2349     if (!format || format->align == StringAlignmentNear)
2350     {
2351         drawbase.x = corners[0].x;
2352         drawbase.y = corners[0].y;
2353         drawflags = DT_NOCLIP | DT_EXPANDTABS;
2354     }
2355     else if (format->align == StringAlignmentCenter)
2356     {
2357         drawbase.x = (corners[0].x + corners[1].x)/2;
2358         drawbase.y = (corners[0].y + corners[1].y)/2;
2359         drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
2360     }
2361     else /* (format->align == StringAlignmentFar) */
2362     {
2363         drawbase.x = corners[1].x;
2364         drawbase.y = corners[1].y;
2365         drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
2366     }
2367
2368     while(sum < length){
2369         drawcoord.left = drawcoord.right = drawbase.x + roundr(ang_sin * (REAL) height);
2370         drawcoord.top = drawcoord.bottom = drawbase.y + roundr(ang_cos * (REAL) height);
2371
2372         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2373                               nwidth, &fit, NULL, &size);
2374         fitcpy = fit;
2375
2376         if(fit == 0){
2377             DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, drawflags);
2378             break;
2379         }
2380
2381         for(lret = 0; lret < fit; lret++)
2382             if(*(stringdup + sum + lret) == '\n')
2383                 break;
2384
2385         /* Line break code (may look strange, but it imitates windows). */
2386         if(lret < fit)
2387             fit = lret;    /* this is not an off-by-one error */
2388         else if(fit < (length - sum)){
2389             if(*(stringdup + sum + fit) == ' ')
2390                 while(*(stringdup + sum + fit) == ' ')
2391                     fit++;
2392             else
2393                 while(*(stringdup + sum + fit - 1) != ' '){
2394                     fit--;
2395
2396                     if(*(stringdup + sum + fit) == '\t')
2397                         break;
2398
2399                     if(fit == 0){
2400                         fit = fitcpy;
2401                         break;
2402                     }
2403                 }
2404         }
2405         DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
2406                   &drawcoord, drawflags);
2407
2408         sum += fit + (lret < fitcpy ? 1 : 0);
2409         height += size.cy;
2410
2411         if(height > nheight)
2412             break;
2413
2414         /* Stop if this was a linewrap (but not if it was a linebreak). */
2415         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2416             break;
2417     }
2418
2419     GdipFree(stringdup);
2420     DeleteObject(rgn);
2421     DeleteObject(gdifont);
2422
2423     RestoreDC(graphics->hdc, save_state);
2424
2425     return Ok;
2426 }
2427
2428 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
2429     GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
2430 {
2431     GpPath *path;
2432     GpStatus stat;
2433
2434     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2435             count, tension, fill);
2436
2437     if(!graphics || !brush || !points)
2438         return InvalidParameter;
2439
2440     if(graphics->busy)
2441         return ObjectBusy;
2442
2443     stat = GdipCreatePath(fill, &path);
2444     if(stat != Ok)
2445         return stat;
2446
2447     stat = GdipAddPathClosedCurve2(path, points, count, tension);
2448     if(stat != Ok){
2449         GdipDeletePath(path);
2450         return stat;
2451     }
2452
2453     stat = GdipFillPath(graphics, brush, path);
2454     if(stat != Ok){
2455         GdipDeletePath(path);
2456         return stat;
2457     }
2458
2459     GdipDeletePath(path);
2460
2461     return Ok;
2462 }
2463
2464 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
2465     GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
2466 {
2467     GpPointF *ptf;
2468     GpStatus stat;
2469     INT i;
2470
2471     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2472             count, tension, fill);
2473
2474     if(!points || count <= 0)
2475         return InvalidParameter;
2476
2477     ptf = GdipAlloc(sizeof(GpPointF)*count);
2478     if(!ptf)
2479         return OutOfMemory;
2480
2481     for(i = 0;i < count;i++){
2482         ptf[i].X = (REAL)points[i].X;
2483         ptf[i].Y = (REAL)points[i].Y;
2484     }
2485
2486     stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2487
2488     GdipFree(ptf);
2489
2490     return stat;
2491 }
2492
2493 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2494     REAL y, REAL width, REAL height)
2495 {
2496     INT save_state;
2497     GpPointF ptf[2];
2498     POINT pti[2];
2499
2500     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2501
2502     if(!graphics || !brush)
2503         return InvalidParameter;
2504
2505     if(graphics->busy)
2506         return ObjectBusy;
2507
2508     ptf[0].X = x;
2509     ptf[0].Y = y;
2510     ptf[1].X = x + width;
2511     ptf[1].Y = y + height;
2512
2513     save_state = SaveDC(graphics->hdc);
2514     EndPath(graphics->hdc);
2515
2516     transform_and_round_points(graphics, pti, ptf, 2);
2517
2518     BeginPath(graphics->hdc);
2519     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2520     EndPath(graphics->hdc);
2521
2522     brush_fill_path(graphics, brush);
2523
2524     RestoreDC(graphics->hdc, save_state);
2525
2526     return Ok;
2527 }
2528
2529 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2530     INT y, INT width, INT height)
2531 {
2532     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2533
2534     return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2535 }
2536
2537 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2538 {
2539     INT save_state;
2540     GpStatus retval;
2541
2542     TRACE("(%p, %p, %p)\n", graphics, brush, path);
2543
2544     if(!brush || !graphics || !path)
2545         return InvalidParameter;
2546
2547     if(graphics->busy)
2548         return ObjectBusy;
2549
2550     save_state = SaveDC(graphics->hdc);
2551     EndPath(graphics->hdc);
2552     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2553                                                                     : WINDING));
2554
2555     BeginPath(graphics->hdc);
2556     retval = draw_poly(graphics, NULL, path->pathdata.Points,
2557                        path->pathdata.Types, path->pathdata.Count, FALSE);
2558
2559     if(retval != Ok)
2560         goto end;
2561
2562     EndPath(graphics->hdc);
2563     brush_fill_path(graphics, brush);
2564
2565     retval = Ok;
2566
2567 end:
2568     RestoreDC(graphics->hdc, save_state);
2569
2570     return retval;
2571 }
2572
2573 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2574     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2575 {
2576     INT save_state;
2577
2578     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2579             graphics, brush, x, y, width, height, startAngle, sweepAngle);
2580
2581     if(!graphics || !brush)
2582         return InvalidParameter;
2583
2584     if(graphics->busy)
2585         return ObjectBusy;
2586
2587     save_state = SaveDC(graphics->hdc);
2588     EndPath(graphics->hdc);
2589
2590     BeginPath(graphics->hdc);
2591     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2592     EndPath(graphics->hdc);
2593
2594     brush_fill_path(graphics, brush);
2595
2596     RestoreDC(graphics->hdc, save_state);
2597
2598     return Ok;
2599 }
2600
2601 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2602     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2603 {
2604     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
2605             graphics, brush, x, y, width, height, startAngle, sweepAngle);
2606
2607     return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2608 }
2609
2610 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2611     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2612 {
2613     INT save_state;
2614     GpPointF *ptf = NULL;
2615     POINT *pti = NULL;
2616     GpStatus retval = Ok;
2617
2618     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2619
2620     if(!graphics || !brush || !points || !count)
2621         return InvalidParameter;
2622
2623     if(graphics->busy)
2624         return ObjectBusy;
2625
2626     ptf = GdipAlloc(count * sizeof(GpPointF));
2627     pti = GdipAlloc(count * sizeof(POINT));
2628     if(!ptf || !pti){
2629         retval = OutOfMemory;
2630         goto end;
2631     }
2632
2633     memcpy(ptf, points, count * sizeof(GpPointF));
2634
2635     save_state = SaveDC(graphics->hdc);
2636     EndPath(graphics->hdc);
2637     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2638                                                                   : WINDING));
2639
2640     transform_and_round_points(graphics, pti, ptf, count);
2641
2642     BeginPath(graphics->hdc);
2643     Polygon(graphics->hdc, pti, count);
2644     EndPath(graphics->hdc);
2645
2646     brush_fill_path(graphics, brush);
2647
2648     RestoreDC(graphics->hdc, save_state);
2649
2650 end:
2651     GdipFree(ptf);
2652     GdipFree(pti);
2653
2654     return retval;
2655 }
2656
2657 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2658     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2659 {
2660     INT save_state, i;
2661     GpPointF *ptf = NULL;
2662     POINT *pti = NULL;
2663     GpStatus retval = Ok;
2664
2665     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2666
2667     if(!graphics || !brush || !points || !count)
2668         return InvalidParameter;
2669
2670     if(graphics->busy)
2671         return ObjectBusy;
2672
2673     ptf = GdipAlloc(count * sizeof(GpPointF));
2674     pti = GdipAlloc(count * sizeof(POINT));
2675     if(!ptf || !pti){
2676         retval = OutOfMemory;
2677         goto end;
2678     }
2679
2680     for(i = 0; i < count; i ++){
2681         ptf[i].X = (REAL) points[i].X;
2682         ptf[i].Y = (REAL) points[i].Y;
2683     }
2684
2685     save_state = SaveDC(graphics->hdc);
2686     EndPath(graphics->hdc);
2687     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2688                                                                   : WINDING));
2689
2690     transform_and_round_points(graphics, pti, ptf, count);
2691
2692     BeginPath(graphics->hdc);
2693     Polygon(graphics->hdc, pti, count);
2694     EndPath(graphics->hdc);
2695
2696     brush_fill_path(graphics, brush);
2697
2698     RestoreDC(graphics->hdc, save_state);
2699
2700 end:
2701     GdipFree(ptf);
2702     GdipFree(pti);
2703
2704     return retval;
2705 }
2706
2707 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2708     GDIPCONST GpPointF *points, INT count)
2709 {
2710     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2711
2712     return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2713 }
2714
2715 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2716     GDIPCONST GpPoint *points, INT count)
2717 {
2718     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2719
2720     return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2721 }
2722
2723 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2724     REAL x, REAL y, REAL width, REAL height)
2725 {
2726     INT save_state;
2727     GpPointF ptf[4];
2728     POINT pti[4];
2729
2730     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2731
2732     if(!graphics || !brush)
2733         return InvalidParameter;
2734
2735     if(graphics->busy)
2736         return ObjectBusy;
2737
2738     ptf[0].X = x;
2739     ptf[0].Y = y;
2740     ptf[1].X = x + width;
2741     ptf[1].Y = y;
2742     ptf[2].X = x + width;
2743     ptf[2].Y = y + height;
2744     ptf[3].X = x;
2745     ptf[3].Y = y + height;
2746
2747     save_state = SaveDC(graphics->hdc);
2748     EndPath(graphics->hdc);
2749
2750     transform_and_round_points(graphics, pti, ptf, 4);
2751
2752     BeginPath(graphics->hdc);
2753     Polygon(graphics->hdc, pti, 4);
2754     EndPath(graphics->hdc);
2755
2756     brush_fill_path(graphics, brush);
2757
2758     RestoreDC(graphics->hdc, save_state);
2759
2760     return Ok;
2761 }
2762
2763 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2764     INT x, INT y, INT width, INT height)
2765 {
2766     INT save_state;
2767     GpPointF ptf[4];
2768     POINT pti[4];
2769
2770     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2771
2772     if(!graphics || !brush)
2773         return InvalidParameter;
2774
2775     if(graphics->busy)
2776         return ObjectBusy;
2777
2778     ptf[0].X = x;
2779     ptf[0].Y = y;
2780     ptf[1].X = x + width;
2781     ptf[1].Y = y;
2782     ptf[2].X = x + width;
2783     ptf[2].Y = y + height;
2784     ptf[3].X = x;
2785     ptf[3].Y = y + height;
2786
2787     save_state = SaveDC(graphics->hdc);
2788     EndPath(graphics->hdc);
2789
2790     transform_and_round_points(graphics, pti, ptf, 4);
2791
2792     BeginPath(graphics->hdc);
2793     Polygon(graphics->hdc, pti, 4);
2794     EndPath(graphics->hdc);
2795
2796     brush_fill_path(graphics, brush);
2797
2798     RestoreDC(graphics->hdc, save_state);
2799
2800     return Ok;
2801 }
2802
2803 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2804     INT count)
2805 {
2806     GpStatus ret;
2807     INT i;
2808
2809     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2810
2811     if(!rects)
2812         return InvalidParameter;
2813
2814     for(i = 0; i < count; i++){
2815         ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2816         if(ret != Ok)   return ret;
2817     }
2818
2819     return Ok;
2820 }
2821
2822 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2823     INT count)
2824 {
2825     GpRectF *rectsF;
2826     GpStatus ret;
2827     INT i;
2828
2829     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2830
2831     if(!rects || count <= 0)
2832         return InvalidParameter;
2833
2834     rectsF = GdipAlloc(sizeof(GpRectF)*count);
2835     if(!rectsF)
2836         return OutOfMemory;
2837
2838     for(i = 0; i < count; i++){
2839         rectsF[i].X      = (REAL)rects[i].X;
2840         rectsF[i].Y      = (REAL)rects[i].Y;
2841         rectsF[i].X      = (REAL)rects[i].Width;
2842         rectsF[i].Height = (REAL)rects[i].Height;
2843     }
2844
2845     ret = GdipFillRectangles(graphics,brush,rectsF,count);
2846     GdipFree(rectsF);
2847
2848     return ret;
2849 }
2850
2851 /*****************************************************************************
2852  * GdipFillRegion [GDIPLUS.@]
2853  */
2854 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
2855         GpRegion* region)
2856 {
2857     INT save_state;
2858     GpStatus status;
2859     HRGN hrgn;
2860     RECT rc;
2861
2862     TRACE("(%p, %p, %p)\n", graphics, brush, region);
2863
2864     if (!(graphics && brush && region))
2865         return InvalidParameter;
2866
2867     if(graphics->busy)
2868         return ObjectBusy;
2869
2870     status = GdipGetRegionHRgn(region, graphics, &hrgn);
2871     if(status != Ok)
2872         return status;
2873
2874     save_state = SaveDC(graphics->hdc);
2875     EndPath(graphics->hdc);
2876
2877     ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
2878
2879     if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
2880     {
2881         BeginPath(graphics->hdc);
2882         Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
2883         EndPath(graphics->hdc);
2884
2885         brush_fill_path(graphics, brush);
2886     }
2887
2888     RestoreDC(graphics->hdc, save_state);
2889
2890     DeleteObject(hrgn);
2891
2892     return Ok;
2893 }
2894
2895 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
2896 {
2897     static int calls;
2898
2899     if(!graphics)
2900         return InvalidParameter;
2901
2902     if(graphics->busy)
2903         return ObjectBusy;
2904
2905     if(!(calls++))
2906         FIXME("not implemented\n");
2907
2908     return NotImplemented;
2909 }
2910
2911 /*****************************************************************************
2912  * GdipGetClipBounds [GDIPLUS.@]
2913  */
2914 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
2915 {
2916     TRACE("(%p, %p)\n", graphics, rect);
2917
2918     if(!graphics)
2919         return InvalidParameter;
2920
2921     if(graphics->busy)
2922         return ObjectBusy;
2923
2924     return GdipGetRegionBounds(graphics->clip, graphics, rect);
2925 }
2926
2927 /*****************************************************************************
2928  * GdipGetClipBoundsI [GDIPLUS.@]
2929  */
2930 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
2931 {
2932     TRACE("(%p, %p)\n", graphics, rect);
2933
2934     if(!graphics)
2935         return InvalidParameter;
2936
2937     if(graphics->busy)
2938         return ObjectBusy;
2939
2940     return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
2941 }
2942
2943 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
2944 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
2945     CompositingMode *mode)
2946 {
2947     TRACE("(%p, %p)\n", graphics, mode);
2948
2949     if(!graphics || !mode)
2950         return InvalidParameter;
2951
2952     if(graphics->busy)
2953         return ObjectBusy;
2954
2955     *mode = graphics->compmode;
2956
2957     return Ok;
2958 }
2959
2960 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
2961 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
2962     CompositingQuality *quality)
2963 {
2964     TRACE("(%p, %p)\n", graphics, quality);
2965
2966     if(!graphics || !quality)
2967         return InvalidParameter;
2968
2969     if(graphics->busy)
2970         return ObjectBusy;
2971
2972     *quality = graphics->compqual;
2973
2974     return Ok;
2975 }
2976
2977 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
2978 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
2979     InterpolationMode *mode)
2980 {
2981     TRACE("(%p, %p)\n", graphics, mode);
2982
2983     if(!graphics || !mode)
2984         return InvalidParameter;
2985
2986     if(graphics->busy)
2987         return ObjectBusy;
2988
2989     *mode = graphics->interpolation;
2990
2991     return Ok;
2992 }
2993
2994 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
2995 {
2996     if(!graphics || !argb)
2997         return InvalidParameter;
2998
2999     if(graphics->busy)
3000         return ObjectBusy;
3001
3002     FIXME("(%p, %p): stub\n", graphics, argb);
3003
3004     return NotImplemented;
3005 }
3006
3007 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3008 {
3009     TRACE("(%p, %p)\n", graphics, scale);
3010
3011     if(!graphics || !scale)
3012         return InvalidParameter;
3013
3014     if(graphics->busy)
3015         return ObjectBusy;
3016
3017     *scale = graphics->scale;
3018
3019     return Ok;
3020 }
3021
3022 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3023 {
3024     TRACE("(%p, %p)\n", graphics, unit);
3025
3026     if(!graphics || !unit)
3027         return InvalidParameter;
3028
3029     if(graphics->busy)
3030         return ObjectBusy;
3031
3032     *unit = graphics->unit;
3033
3034     return Ok;
3035 }
3036
3037 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3038 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3039     *mode)
3040 {
3041     TRACE("(%p, %p)\n", graphics, mode);
3042
3043     if(!graphics || !mode)
3044         return InvalidParameter;
3045
3046     if(graphics->busy)
3047         return ObjectBusy;
3048
3049     *mode = graphics->pixeloffset;
3050
3051     return Ok;
3052 }
3053
3054 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3055 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3056 {
3057     TRACE("(%p, %p)\n", graphics, mode);
3058
3059     if(!graphics || !mode)
3060         return InvalidParameter;
3061
3062     if(graphics->busy)
3063         return ObjectBusy;
3064
3065     *mode = graphics->smoothing;
3066
3067     return Ok;
3068 }
3069
3070 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3071 {
3072     TRACE("(%p, %p)\n", graphics, contrast);
3073
3074     if(!graphics || !contrast)
3075         return InvalidParameter;
3076
3077     *contrast = graphics->textcontrast;
3078
3079     return Ok;
3080 }
3081
3082 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3083 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3084     TextRenderingHint *hint)
3085 {
3086     TRACE("(%p, %p)\n", graphics, hint);
3087
3088     if(!graphics || !hint)
3089         return InvalidParameter;
3090
3091     if(graphics->busy)
3092         return ObjectBusy;
3093
3094     *hint = graphics->texthint;
3095
3096     return Ok;
3097 }
3098
3099 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3100 {
3101     TRACE("(%p, %p)\n", graphics, matrix);
3102
3103     if(!graphics || !matrix)
3104         return InvalidParameter;
3105
3106     if(graphics->busy)
3107         return ObjectBusy;
3108
3109     *matrix = *graphics->worldtrans;
3110     return Ok;
3111 }
3112
3113 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
3114 {
3115     GpSolidFill *brush;
3116     GpStatus stat;
3117     RECT rect;
3118
3119     TRACE("(%p, %x)\n", graphics, color);
3120
3121     if(!graphics)
3122         return InvalidParameter;
3123
3124     if(graphics->busy)
3125         return ObjectBusy;
3126
3127     if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
3128         return stat;
3129
3130     if(graphics->hwnd){
3131         if(!GetWindowRect(graphics->hwnd, &rect)){
3132             GdipDeleteBrush((GpBrush*)brush);
3133             return GenericError;
3134         }
3135
3136         GdipFillRectangle(graphics, (GpBrush*)brush, 0.0, 0.0, (REAL)(rect.right  - rect.left),
3137                                                                (REAL)(rect.bottom - rect.top));
3138     }
3139     else
3140         GdipFillRectangle(graphics, (GpBrush*)brush, 0.0, 0.0, (REAL)GetDeviceCaps(graphics->hdc, HORZRES),
3141                                                                (REAL)GetDeviceCaps(graphics->hdc, VERTRES));
3142
3143     GdipDeleteBrush((GpBrush*)brush);
3144
3145     return Ok;
3146 }
3147
3148 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
3149 {
3150     TRACE("(%p, %p)\n", graphics, res);
3151
3152     if(!graphics || !res)
3153         return InvalidParameter;
3154
3155     return GdipIsEmptyRegion(graphics->clip, graphics, res);
3156 }
3157
3158 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
3159 {
3160     FIXME("(%p, %.2f, %.2f, %p) stub\n", graphics, x, y, result);
3161
3162     if(!graphics || !result)
3163         return InvalidParameter;
3164
3165     if(graphics->busy)
3166         return ObjectBusy;
3167
3168     return NotImplemented;
3169 }
3170
3171 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
3172 {
3173     FIXME("(%p, %d, %d, %p) stub\n", graphics, x, y, result);
3174
3175     if(!graphics || !result)
3176         return InvalidParameter;
3177
3178     if(graphics->busy)
3179         return ObjectBusy;
3180
3181     return NotImplemented;
3182 }
3183
3184 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
3185         GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
3186         GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
3187         INT regionCount, GpRegion** regions)
3188 {
3189     if (!(graphics && string && font && layoutRect && stringFormat && regions))
3190         return InvalidParameter;
3191
3192     FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
3193             length, font, layoutRect, stringFormat, regionCount, regions);
3194
3195     return NotImplemented;
3196 }
3197
3198 /* Find the smallest rectangle that bounds the text when it is printed in rect
3199  * according to the format options listed in format. If rect has 0 width and
3200  * height, then just find the smallest rectangle that bounds the text when it's
3201  * printed at location (rect->X, rect-Y). */
3202 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
3203     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3204     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
3205     INT *codepointsfitted, INT *linesfilled)
3206 {
3207     HFONT oldfont;
3208     WCHAR* stringdup;
3209     INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
3210         nheight;
3211     SIZE size;
3212
3213     TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
3214         debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
3215         bounds, codepointsfitted, linesfilled);
3216
3217     if(!graphics || !string || !font || !rect)
3218         return InvalidParameter;
3219
3220     if(linesfilled) *linesfilled = 0;
3221     if(codepointsfitted) *codepointsfitted = 0;
3222
3223     if(format)
3224         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
3225
3226     if(length == -1) length = lstrlenW(string);
3227
3228     stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
3229     if(!stringdup) return OutOfMemory;
3230
3231     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3232     nwidth = roundr(rect->Width);
3233     nheight = roundr(rect->Height);
3234
3235     if((nwidth == 0) && (nheight == 0))
3236         nwidth = nheight = INT_MAX;
3237
3238     for(i = 0, j = 0; i < length; i++){
3239         if(!isprintW(string[i]) && (string[i] != '\n'))
3240             continue;
3241
3242         stringdup[j] = string[i];
3243         j++;
3244     }
3245
3246     stringdup[j] = 0;
3247     length = j;
3248
3249     while(sum < length){
3250         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
3251                               nwidth, &fit, NULL, &size);
3252         fitcpy = fit;
3253
3254         if(fit == 0)
3255             break;
3256
3257         for(lret = 0; lret < fit; lret++)
3258             if(*(stringdup + sum + lret) == '\n')
3259                 break;
3260
3261         /* Line break code (may look strange, but it imitates windows). */
3262         if(lret < fit)
3263             fit = lret;    /* this is not an off-by-one error */
3264         else if(fit < (length - sum)){
3265             if(*(stringdup + sum + fit) == ' ')
3266                 while(*(stringdup + sum + fit) == ' ')
3267                     fit++;
3268             else
3269                 while(*(stringdup + sum + fit - 1) != ' '){
3270                     fit--;
3271
3272                     if(*(stringdup + sum + fit) == '\t')
3273                         break;
3274
3275                     if(fit == 0){
3276                         fit = fitcpy;
3277                         break;
3278                     }
3279                 }
3280         }
3281
3282         GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
3283                               nwidth, &j, NULL, &size);
3284
3285         sum += fit + (lret < fitcpy ? 1 : 0);
3286         if(codepointsfitted) *codepointsfitted = sum;
3287
3288         height += size.cy;
3289         if(linesfilled) *linesfilled += size.cy;
3290         max_width = max(max_width, size.cx);
3291
3292         if(height > nheight)
3293             break;
3294
3295         /* Stop if this was a linewrap (but not if it was a linebreak). */
3296         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
3297             break;
3298     }
3299
3300     bounds->X = rect->X;
3301     bounds->Y = rect->Y;
3302     bounds->Width = (REAL)max_width;
3303     bounds->Height = (REAL) min(height, nheight);
3304
3305     GdipFree(stringdup);
3306     DeleteObject(SelectObject(graphics->hdc, oldfont));
3307
3308     return Ok;
3309 }
3310
3311 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
3312 {
3313     TRACE("(%p)\n", graphics);
3314
3315     if(!graphics)
3316         return InvalidParameter;
3317
3318     if(graphics->busy)
3319         return ObjectBusy;
3320
3321     return GdipSetInfinite(graphics->clip);
3322 }
3323
3324 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
3325 {
3326     TRACE("(%p)\n", graphics);
3327
3328     if(!graphics)
3329         return InvalidParameter;
3330
3331     if(graphics->busy)
3332         return ObjectBusy;
3333
3334     graphics->worldtrans->matrix[0] = 1.0;
3335     graphics->worldtrans->matrix[1] = 0.0;
3336     graphics->worldtrans->matrix[2] = 0.0;
3337     graphics->worldtrans->matrix[3] = 1.0;
3338     graphics->worldtrans->matrix[4] = 0.0;
3339     graphics->worldtrans->matrix[5] = 0.0;
3340
3341     return Ok;
3342 }
3343
3344 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
3345 {
3346     return GdipEndContainer(graphics, state);
3347 }
3348
3349 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
3350     GpMatrixOrder order)
3351 {
3352     TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
3353
3354     if(!graphics)
3355         return InvalidParameter;
3356
3357     if(graphics->busy)
3358         return ObjectBusy;
3359
3360     return GdipRotateMatrix(graphics->worldtrans, angle, order);
3361 }
3362
3363 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
3364 {
3365     return GdipBeginContainer2(graphics, state);
3366 }
3367
3368 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
3369         GraphicsContainer *state)
3370 {
3371     GraphicsContainerItem *container;
3372     GpStatus sts;
3373
3374     TRACE("(%p, %p)\n", graphics, state);
3375
3376     if(!graphics || !state)
3377         return InvalidParameter;
3378
3379     sts = init_container(&container, graphics);
3380     if(sts != Ok)
3381         return sts;
3382
3383     list_add_head(&graphics->containers, &container->entry);
3384     *state = graphics->contid = container->contid;
3385
3386     return Ok;
3387 }
3388
3389 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
3390 {
3391     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3392     return NotImplemented;
3393 }
3394
3395 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
3396 {
3397     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3398     return NotImplemented;
3399 }
3400
3401 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
3402 {
3403     FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
3404     return NotImplemented;
3405 }
3406
3407 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
3408 {
3409     GpStatus sts;
3410     GraphicsContainerItem *container, *container2;
3411
3412     TRACE("(%p, %x)\n", graphics, state);
3413
3414     if(!graphics)
3415         return InvalidParameter;
3416
3417     LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
3418         if(container->contid == state)
3419             break;
3420     }
3421
3422     /* did not find a matching container */
3423     if(&container->entry == &graphics->containers)
3424         return Ok;
3425
3426     sts = restore_container(graphics, container);
3427     if(sts != Ok)
3428         return sts;
3429
3430     /* remove all of the containers on top of the found container */
3431     LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
3432         if(container->contid == state)
3433             break;
3434         list_remove(&container->entry);
3435         delete_container(container);
3436     }
3437
3438     list_remove(&container->entry);
3439     delete_container(container);
3440
3441     return Ok;
3442 }
3443
3444 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
3445     REAL sy, GpMatrixOrder order)
3446 {
3447     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
3448
3449     if(!graphics)
3450         return InvalidParameter;
3451
3452     if(graphics->busy)
3453         return ObjectBusy;
3454
3455     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
3456 }
3457
3458 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
3459     CombineMode mode)
3460 {
3461     TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
3462
3463     if(!graphics || !srcgraphics)
3464         return InvalidParameter;
3465
3466     return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
3467 }
3468
3469 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
3470     CompositingMode mode)
3471 {
3472     TRACE("(%p, %d)\n", graphics, mode);
3473
3474     if(!graphics)
3475         return InvalidParameter;
3476
3477     if(graphics->busy)
3478         return ObjectBusy;
3479
3480     graphics->compmode = mode;
3481
3482     return Ok;
3483 }
3484
3485 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
3486     CompositingQuality quality)
3487 {
3488     TRACE("(%p, %d)\n", graphics, quality);
3489
3490     if(!graphics)
3491         return InvalidParameter;
3492
3493     if(graphics->busy)
3494         return ObjectBusy;
3495
3496     graphics->compqual = quality;
3497
3498     return Ok;
3499 }
3500
3501 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
3502     InterpolationMode mode)
3503 {
3504     TRACE("(%p, %d)\n", graphics, mode);
3505
3506     if(!graphics)
3507         return InvalidParameter;
3508
3509     if(graphics->busy)
3510         return ObjectBusy;
3511
3512     graphics->interpolation = mode;
3513
3514     return Ok;
3515 }
3516
3517 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
3518 {
3519     TRACE("(%p, %.2f)\n", graphics, scale);
3520
3521     if(!graphics || (scale <= 0.0))
3522         return InvalidParameter;
3523
3524     if(graphics->busy)
3525         return ObjectBusy;
3526
3527     graphics->scale = scale;
3528
3529     return Ok;
3530 }
3531
3532 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
3533 {
3534     TRACE("(%p, %d)\n", graphics, unit);
3535
3536     if(!graphics)
3537         return InvalidParameter;
3538
3539     if(graphics->busy)
3540         return ObjectBusy;
3541
3542     if(unit == UnitWorld)
3543         return InvalidParameter;
3544
3545     graphics->unit = unit;
3546
3547     return Ok;
3548 }
3549
3550 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3551     mode)
3552 {
3553     TRACE("(%p, %d)\n", graphics, mode);
3554
3555     if(!graphics)
3556         return InvalidParameter;
3557
3558     if(graphics->busy)
3559         return ObjectBusy;
3560
3561     graphics->pixeloffset = mode;
3562
3563     return Ok;
3564 }
3565
3566 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
3567 {
3568     static int calls;
3569
3570     TRACE("(%p,%i,%i)\n", graphics, x, y);
3571
3572     if (!(calls++))
3573         FIXME("not implemented\n");
3574
3575     return NotImplemented;
3576 }
3577
3578 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
3579 {
3580     TRACE("(%p, %d)\n", graphics, mode);
3581
3582     if(!graphics)
3583         return InvalidParameter;
3584
3585     if(graphics->busy)
3586         return ObjectBusy;
3587
3588     graphics->smoothing = mode;
3589
3590     return Ok;
3591 }
3592
3593 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
3594 {
3595     TRACE("(%p, %d)\n", graphics, contrast);
3596
3597     if(!graphics)
3598         return InvalidParameter;
3599
3600     graphics->textcontrast = contrast;
3601
3602     return Ok;
3603 }
3604
3605 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
3606     TextRenderingHint hint)
3607 {
3608     TRACE("(%p, %d)\n", graphics, hint);
3609
3610     if(!graphics)
3611         return InvalidParameter;
3612
3613     if(graphics->busy)
3614         return ObjectBusy;
3615
3616     graphics->texthint = hint;
3617
3618     return Ok;
3619 }
3620
3621 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3622 {
3623     TRACE("(%p, %p)\n", graphics, matrix);
3624
3625     if(!graphics || !matrix)
3626         return InvalidParameter;
3627
3628     if(graphics->busy)
3629         return ObjectBusy;
3630
3631     GdipDeleteMatrix(graphics->worldtrans);
3632     return GdipCloneMatrix(matrix, &graphics->worldtrans);
3633 }
3634
3635 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
3636     REAL dy, GpMatrixOrder order)
3637 {
3638     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
3639
3640     if(!graphics)
3641         return InvalidParameter;
3642
3643     if(graphics->busy)
3644         return ObjectBusy;
3645
3646     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
3647 }
3648
3649 /*****************************************************************************
3650  * GdipSetClipHrgn [GDIPLUS.@]
3651  */
3652 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
3653 {
3654     GpRegion *region;
3655     GpStatus status;
3656
3657     TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
3658
3659     if(!graphics)
3660         return InvalidParameter;
3661
3662     status = GdipCreateRegionHrgn(hrgn, &region);
3663     if(status != Ok)
3664         return status;
3665
3666     status = GdipSetClipRegion(graphics, region, mode);
3667
3668     GdipDeleteRegion(region);
3669     return status;
3670 }
3671
3672 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
3673 {
3674     TRACE("(%p, %p, %d)\n", graphics, path, mode);
3675
3676     if(!graphics)
3677         return InvalidParameter;
3678
3679     if(graphics->busy)
3680         return ObjectBusy;
3681
3682     return GdipCombineRegionPath(graphics->clip, path, mode);
3683 }
3684
3685 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
3686                                     REAL width, REAL height,
3687                                     CombineMode mode)
3688 {
3689     GpRectF rect;
3690
3691     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
3692
3693     if(!graphics)
3694         return InvalidParameter;
3695
3696     if(graphics->busy)
3697         return ObjectBusy;
3698
3699     rect.X = x;
3700     rect.Y = y;
3701     rect.Width  = width;
3702     rect.Height = height;
3703
3704     return GdipCombineRegionRect(graphics->clip, &rect, mode);
3705 }
3706
3707 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
3708                                      INT width, INT height,
3709                                      CombineMode mode)
3710 {
3711     TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
3712
3713     if(!graphics)
3714         return InvalidParameter;
3715
3716     if(graphics->busy)
3717         return ObjectBusy;
3718
3719     return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
3720 }
3721
3722 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
3723                                       CombineMode mode)
3724 {
3725     TRACE("(%p, %p, %d)\n", graphics, region, mode);
3726
3727     if(!graphics || !region)
3728         return InvalidParameter;
3729
3730     if(graphics->busy)
3731         return ObjectBusy;
3732
3733     return GdipCombineRegionRegion(graphics->clip, region, mode);
3734 }
3735
3736 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
3737     UINT limitDpi)
3738 {
3739     static int calls;
3740
3741     if(!(calls++))
3742         FIXME("not implemented\n");
3743
3744     return NotImplemented;
3745 }
3746
3747 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
3748     INT count)
3749 {
3750     INT save_state;
3751     POINT *pti;
3752
3753     TRACE("(%p, %p, %d)\n", graphics, points, count);
3754
3755     if(!graphics || !pen || count<=0)
3756         return InvalidParameter;
3757
3758     if(graphics->busy)
3759         return ObjectBusy;
3760
3761     pti = GdipAlloc(sizeof(POINT) * count);
3762
3763     save_state = prepare_dc(graphics, pen);
3764     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3765
3766     transform_and_round_points(graphics, pti, (GpPointF*)points, count);
3767     Polygon(graphics->hdc, pti, count);
3768
3769     restore_dc(graphics, save_state);
3770     GdipFree(pti);
3771
3772     return Ok;
3773 }
3774
3775 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
3776     INT count)
3777 {
3778     GpStatus ret;
3779     GpPointF *ptf;
3780     INT i;
3781
3782     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3783
3784     if(count<=0)    return InvalidParameter;
3785     ptf = GdipAlloc(sizeof(GpPointF) * count);
3786
3787     for(i = 0;i < count; i++){
3788         ptf[i].X = (REAL)points[i].X;
3789         ptf[i].Y = (REAL)points[i].Y;
3790     }
3791
3792     ret = GdipDrawPolygon(graphics,pen,ptf,count);
3793     GdipFree(ptf);
3794
3795     return ret;
3796 }
3797
3798 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
3799 {
3800     TRACE("(%p, %p)\n", graphics, dpi);
3801
3802     if(!graphics || !dpi)
3803         return InvalidParameter;
3804
3805     if(graphics->busy)
3806         return ObjectBusy;
3807
3808     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
3809
3810     return Ok;
3811 }
3812
3813 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
3814 {
3815     TRACE("(%p, %p)\n", graphics, dpi);
3816
3817     if(!graphics || !dpi)
3818         return InvalidParameter;
3819
3820     if(graphics->busy)
3821         return ObjectBusy;
3822
3823     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
3824
3825     return Ok;
3826 }
3827
3828 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
3829     GpMatrixOrder order)
3830 {
3831     GpMatrix m;
3832     GpStatus ret;
3833
3834     TRACE("(%p, %p, %d)\n", graphics, matrix, order);
3835
3836     if(!graphics || !matrix)
3837         return InvalidParameter;
3838
3839     if(graphics->busy)
3840         return ObjectBusy;
3841
3842     m = *(graphics->worldtrans);
3843
3844     ret = GdipMultiplyMatrix(&m, matrix, order);
3845     if(ret == Ok)
3846         *(graphics->worldtrans) = m;
3847
3848     return ret;
3849 }
3850
3851 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
3852 {
3853     TRACE("(%p, %p)\n", graphics, hdc);
3854
3855     if(!graphics || !hdc)
3856         return InvalidParameter;
3857
3858     if(graphics->busy)
3859         return ObjectBusy;
3860
3861     *hdc = graphics->hdc;
3862     graphics->busy = TRUE;
3863
3864     return Ok;
3865 }
3866
3867 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
3868 {
3869     TRACE("(%p, %p)\n", graphics, hdc);
3870
3871     if(!graphics)
3872         return InvalidParameter;
3873
3874     if(graphics->hdc != hdc || !(graphics->busy))
3875         return InvalidParameter;
3876
3877     graphics->busy = FALSE;
3878
3879     return Ok;
3880 }
3881
3882 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
3883 {
3884     GpRegion *clip;
3885     GpStatus status;
3886
3887     TRACE("(%p, %p)\n", graphics, region);
3888
3889     if(!graphics || !region)
3890         return InvalidParameter;
3891
3892     if(graphics->busy)
3893         return ObjectBusy;
3894
3895     if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
3896         return status;
3897
3898     /* free everything except root node and header */
3899     delete_element(&region->node);
3900     memcpy(region, clip, sizeof(GpRegion));
3901
3902     return Ok;
3903 }
3904
3905 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
3906                                         GpCoordinateSpace src_space, GpPointF *points, INT count)
3907 {
3908     GpMatrix *matrix;
3909     GpStatus stat;
3910     REAL unitscale;
3911
3912     if(!graphics || !points || count <= 0)
3913         return InvalidParameter;
3914
3915     if(graphics->busy)
3916         return ObjectBusy;
3917
3918     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
3919
3920     if (src_space == dst_space) return Ok;
3921
3922     stat = GdipCreateMatrix(&matrix);
3923     if (stat == Ok)
3924     {
3925         unitscale = convert_unit(graphics->hdc, graphics->unit);
3926
3927         if(graphics->unit != UnitDisplay)
3928             unitscale *= graphics->scale;
3929
3930         /* transform from src_space to CoordinateSpacePage */
3931         switch (src_space)
3932         {
3933         case CoordinateSpaceWorld:
3934             GdipMultiplyMatrix(matrix, graphics->worldtrans, MatrixOrderAppend);
3935             break;
3936         case CoordinateSpacePage:
3937             break;
3938         case CoordinateSpaceDevice:
3939             GdipScaleMatrix(matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
3940             break;
3941         }
3942
3943         /* transform from CoordinateSpacePage to dst_space */
3944         switch (dst_space)
3945         {
3946         case CoordinateSpaceWorld:
3947             {
3948                 GpMatrix *inverted_transform;
3949                 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
3950                 if (stat == Ok)
3951                 {
3952                     stat = GdipInvertMatrix(inverted_transform);
3953                     if (stat == Ok)
3954                         GdipMultiplyMatrix(matrix, inverted_transform, MatrixOrderAppend);
3955                     GdipDeleteMatrix(inverted_transform);
3956                 }
3957                 break;
3958             }
3959         case CoordinateSpacePage:
3960             break;
3961         case CoordinateSpaceDevice:
3962             GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
3963             break;
3964         }
3965
3966         if (stat == Ok)
3967             stat = GdipTransformMatrixPoints(matrix, points, count);
3968
3969         GdipDeleteMatrix(matrix);
3970     }
3971
3972     return stat;
3973 }
3974
3975 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
3976                                          GpCoordinateSpace src_space, GpPoint *points, INT count)
3977 {
3978     GpPointF *pointsF;
3979     GpStatus ret;
3980     INT i;
3981
3982     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
3983
3984     if(count <= 0)
3985         return InvalidParameter;
3986
3987     pointsF = GdipAlloc(sizeof(GpPointF) * count);
3988     if(!pointsF)
3989         return OutOfMemory;
3990
3991     for(i = 0; i < count; i++){
3992         pointsF[i].X = (REAL)points[i].X;
3993         pointsF[i].Y = (REAL)points[i].Y;
3994     }
3995
3996     ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
3997
3998     if(ret == Ok)
3999         for(i = 0; i < count; i++){
4000             points[i].X = roundr(pointsF[i].X);
4001             points[i].Y = roundr(pointsF[i].Y);
4002         }
4003     GdipFree(pointsF);
4004
4005     return ret;
4006 }
4007
4008 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
4009 {
4010     FIXME("\n");
4011
4012     return NULL;
4013 }
4014
4015 /*****************************************************************************
4016  * GdipTranslateClip [GDIPLUS.@]
4017  */
4018 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
4019 {
4020     TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
4021
4022     if(!graphics)
4023         return InvalidParameter;
4024
4025     if(graphics->busy)
4026         return ObjectBusy;
4027
4028     return GdipTranslateRegion(graphics->clip, dx, dy);
4029 }
4030
4031 /*****************************************************************************
4032  * GdipTranslateClipI [GDIPLUS.@]
4033  */
4034 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
4035 {
4036     TRACE("(%p, %d, %d)\n", graphics, dx, dy);
4037
4038     if(!graphics)
4039         return InvalidParameter;
4040
4041     if(graphics->busy)
4042         return ObjectBusy;
4043
4044     return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
4045 }
4046
4047
4048 /*****************************************************************************
4049  * GdipMeasureDriverString [GDIPLUS.@]
4050  */
4051 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4052                                             GDIPCONST GpFont *font, GDIPCONST PointF *positions,
4053                                             INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
4054 {
4055     FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
4056     return NotImplemented;
4057 }
4058
4059 /*****************************************************************************
4060  * GdipGetVisibleClipBoundsI [GDIPLUS.@]
4061  */
4062 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
4063 {
4064     FIXME("(%p %p): stub\n", graphics, rect);
4065     return NotImplemented;
4066 }
4067
4068 /*****************************************************************************
4069  * GdipDrawDriverString [GDIPLUS.@]
4070  */
4071 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4072                                          GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
4073                                          GDIPCONST PointF *positions, INT flags,
4074                                          GDIPCONST GpMatrix *matrix )
4075 {
4076     FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
4077     return NotImplemented;
4078 }
4079
4080 /*****************************************************************************
4081  * GdipIsVisibleRegionPointI [GDIPLUS.@]
4082  */
4083 GpStatus WINGDIPAPI GdipIsVisibleRegionPointI(GpRegion *region, INT x, INT y, GpGraphics *graphics, BOOL *result)
4084 {
4085     FIXME("(%p %d %d %p %p): stub\n", region, x, y, graphics, result);
4086     return NotImplemented;
4087 }