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