mshtml: Added IHTMLWindow2::get_screen implementation.
[wine] / dlls / gdiplus / graphics.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
28
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
34
35 #include "winreg.h"
36 #include "shlwapi.h"
37
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
44
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
48
49 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
51 {
52     REAL radAngle, hypotenuse;
53
54     radAngle = deg2rad(angle);
55     hypotenuse = 50.0; /* arbitrary */
56
57     *x = x_0 + cos(radAngle) * hypotenuse;
58     *y = y_0 + sin(radAngle) * hypotenuse;
59 }
60
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
63 {
64     BYTE ret;
65
66     switch(type & PathPointTypePathTypeMask){
67         case PathPointTypeBezier:
68             ret = PT_BEZIERTO;
69             break;
70         case PathPointTypeLine:
71             ret = PT_LINETO;
72             break;
73         case PathPointTypeStart:
74             ret = PT_MOVETO;
75             break;
76         default:
77             ERR("Bad point type\n");
78             return 0;
79     }
80
81     if(type & PathPointTypeCloseSubpath)
82         ret |= PT_CLOSEFIGURE;
83
84     return ret;
85 }
86
87 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
88 {
89     HPEN gdipen;
90     REAL width;
91     INT save_state = SaveDC(graphics->hdc), i, numdashes;
92     GpPointF pt[2];
93     DWORD dash_array[MAX_DASHLEN];
94
95     EndPath(graphics->hdc);
96
97     if(pen->unit == UnitPixel){
98         width = pen->width;
99     }
100     else{
101         /* Get an estimate for the amount the pen width is affected by the world
102          * transform. (This is similar to what some of the wine drivers do.) */
103         pt[0].X = 0.0;
104         pt[0].Y = 0.0;
105         pt[1].X = 1.0;
106         pt[1].Y = 1.0;
107         GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
108         width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
109                      (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
110
111         width *= pen->width * convert_unit(graphics->hdc,
112                               pen->unit == UnitWorld ? graphics->unit : pen->unit);
113     }
114
115     if(pen->dash == DashStyleCustom){
116         numdashes = min(pen->numdashes, MAX_DASHLEN);
117
118         TRACE("dashes are: ");
119         for(i = 0; i < numdashes; i++){
120             dash_array[i] = roundr(width * pen->dashes[i]);
121             TRACE("%d, ", dash_array[i]);
122         }
123         TRACE("\n and the pen style is %x\n", pen->style);
124
125         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
126                               numdashes, dash_array);
127     }
128     else
129         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
130
131     SelectObject(graphics->hdc, gdipen);
132
133     return save_state;
134 }
135
136 static void restore_dc(GpGraphics *graphics, INT state)
137 {
138     DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
139     RestoreDC(graphics->hdc, state);
140 }
141
142 /* This helper applies all the changes that the points listed in ptf need in
143  * order to be drawn on the device context.  In the end, this should include at
144  * least:
145  *  -scaling by page unit
146  *  -applying world transformation
147  *  -converting from float to int
148  * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
149  * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
150  * gdi to draw, and these functions would irreparably mess with line widths.
151  */
152 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
153     GpPointF *ptf, INT count)
154 {
155     REAL unitscale;
156     GpMatrix *matrix;
157     int i;
158
159     unitscale = convert_unit(graphics->hdc, graphics->unit);
160
161     /* apply page scale */
162     if(graphics->unit != UnitDisplay)
163         unitscale *= graphics->scale;
164
165     GdipCloneMatrix(graphics->worldtrans, &matrix);
166     GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
167     GdipTransformMatrixPoints(matrix, ptf, count);
168     GdipDeleteMatrix(matrix);
169
170     for(i = 0; i < count; i++){
171         pti[i].x = roundr(ptf[i].X);
172         pti[i].y = roundr(ptf[i].Y);
173     }
174 }
175
176 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
177 {
178     ARGB result=0;
179     ARGB i;
180     for (i=0xff; i<=0xff0000; i = i << 8)
181         result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
182     return result;
183 }
184
185 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
186 {
187     REAL blendfac;
188
189     /* clamp to between 0.0 and 1.0, using the wrap mode */
190     if (brush->wrap == WrapModeTile)
191     {
192         position = fmodf(position, 1.0f);
193         if (position < 0.0f) position += 1.0f;
194     }
195     else /* WrapModeFlip* */
196     {
197         position = fmodf(position, 2.0f);
198         if (position < 0.0f) position += 2.0f;
199         if (position > 1.0f) position = 2.0f - position;
200     }
201
202     if (brush->blendcount == 1)
203         blendfac = position;
204     else
205     {
206         int i=1;
207         REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
208         REAL range;
209
210         /* locate the blend positions surrounding this position */
211         while (position > brush->blendpos[i])
212             i++;
213
214         /* interpolate between the blend positions */
215         left_blendpos = brush->blendpos[i-1];
216         left_blendfac = brush->blendfac[i-1];
217         right_blendpos = brush->blendpos[i];
218         right_blendfac = brush->blendfac[i];
219         range = right_blendpos - left_blendpos;
220         blendfac = (left_blendfac * (right_blendpos - position) +
221                     right_blendfac * (position - left_blendpos)) / range;
222     }
223
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     return Ok;
1166 }
1167
1168 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1169 {
1170     GpStatus ret;
1171     HDC hdc;
1172
1173     TRACE("(%p, %p)\n", hwnd, graphics);
1174
1175     hdc = GetDC(hwnd);
1176
1177     if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1178     {
1179         ReleaseDC(hwnd, hdc);
1180         return ret;
1181     }
1182
1183     (*graphics)->hwnd = hwnd;
1184     (*graphics)->owndc = TRUE;
1185
1186     return Ok;
1187 }
1188
1189 /* FIXME: no icm handling */
1190 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1191 {
1192     TRACE("(%p, %p)\n", hwnd, graphics);
1193
1194     return GdipCreateFromHWND(hwnd, graphics);
1195 }
1196
1197 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1198     GpMetafile **metafile)
1199 {
1200     static int calls;
1201
1202     if(!hemf || !metafile)
1203         return InvalidParameter;
1204
1205     if(!(calls++))
1206         FIXME("not implemented\n");
1207
1208     return NotImplemented;
1209 }
1210
1211 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1212     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1213 {
1214     IStream *stream = NULL;
1215     UINT read;
1216     BYTE* copy;
1217     HENHMETAFILE hemf;
1218     GpStatus retval = GenericError;
1219
1220     TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1221
1222     if(!hwmf || !metafile || !placeable)
1223         return InvalidParameter;
1224
1225     *metafile = NULL;
1226     read = GetMetaFileBitsEx(hwmf, 0, NULL);
1227     if(!read)
1228         return GenericError;
1229     copy = GdipAlloc(read);
1230     GetMetaFileBitsEx(hwmf, read, copy);
1231
1232     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1233     GdipFree(copy);
1234
1235     read = GetEnhMetaFileBits(hemf, 0, NULL);
1236     copy = GdipAlloc(read);
1237     GetEnhMetaFileBits(hemf, read, copy);
1238     DeleteEnhMetaFile(hemf);
1239
1240     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1241         ERR("could not make stream\n");
1242         GdipFree(copy);
1243         goto err;
1244     }
1245
1246     *metafile = GdipAlloc(sizeof(GpMetafile));
1247     if(!*metafile){
1248         retval = OutOfMemory;
1249         goto err;
1250     }
1251
1252     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1253         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1254         goto err;
1255
1256
1257     (*metafile)->image.type = ImageTypeMetafile;
1258     memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
1259     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1260     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
1261     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1262                     - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
1263     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1264                    - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
1265     (*metafile)->unit = UnitInch;
1266
1267     if(delete)
1268         DeleteMetaFile(hwmf);
1269
1270     return Ok;
1271
1272 err:
1273     GdipFree(*metafile);
1274     IStream_Release(stream);
1275     return retval;
1276 }
1277
1278 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1279     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1280 {
1281     HMETAFILE hmf = GetMetaFileW(file);
1282
1283     TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1284
1285     if(!hmf) return InvalidParameter;
1286
1287     return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1288 }
1289
1290 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1291     GpMetafile **metafile)
1292 {
1293     FIXME("(%p, %p): stub\n", file, metafile);
1294     return NotImplemented;
1295 }
1296
1297 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1298     GpMetafile **metafile)
1299 {
1300     FIXME("(%p, %p): stub\n", stream, metafile);
1301     return NotImplemented;
1302 }
1303
1304 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1305     UINT access, IStream **stream)
1306 {
1307     DWORD dwMode;
1308     HRESULT ret;
1309
1310     TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1311
1312     if(!stream || !filename)
1313         return InvalidParameter;
1314
1315     if(access & GENERIC_WRITE)
1316         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1317     else if(access & GENERIC_READ)
1318         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1319     else
1320         return InvalidParameter;
1321
1322     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1323
1324     return hresult_to_status(ret);
1325 }
1326
1327 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1328 {
1329     GraphicsContainerItem *cont, *next;
1330     TRACE("(%p)\n", graphics);
1331
1332     if(!graphics) return InvalidParameter;
1333     if(graphics->busy) return ObjectBusy;
1334
1335     if(graphics->owndc)
1336         ReleaseDC(graphics->hwnd, graphics->hdc);
1337
1338     LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1339         list_remove(&cont->entry);
1340         delete_container(cont);
1341     }
1342
1343     GdipDeleteRegion(graphics->clip);
1344     GdipDeleteMatrix(graphics->worldtrans);
1345     GdipFree(graphics);
1346
1347     return Ok;
1348 }
1349
1350 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1351     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1352 {
1353     INT save_state, num_pts;
1354     GpPointF points[MAX_ARC_PTS];
1355     GpStatus retval;
1356
1357     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1358           width, height, startAngle, sweepAngle);
1359
1360     if(!graphics || !pen || width <= 0 || height <= 0)
1361         return InvalidParameter;
1362
1363     if(graphics->busy)
1364         return ObjectBusy;
1365
1366     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1367
1368     save_state = prepare_dc(graphics, pen);
1369
1370     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1371
1372     restore_dc(graphics, save_state);
1373
1374     return retval;
1375 }
1376
1377 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1378     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1379 {
1380     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1381           width, height, startAngle, sweepAngle);
1382
1383     return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1384 }
1385
1386 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1387     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1388 {
1389     INT save_state;
1390     GpPointF pt[4];
1391     GpStatus retval;
1392
1393     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1394           x2, y2, x3, y3, x4, y4);
1395
1396     if(!graphics || !pen)
1397         return InvalidParameter;
1398
1399     if(graphics->busy)
1400         return ObjectBusy;
1401
1402     pt[0].X = x1;
1403     pt[0].Y = y1;
1404     pt[1].X = x2;
1405     pt[1].Y = y2;
1406     pt[2].X = x3;
1407     pt[2].Y = y3;
1408     pt[3].X = x4;
1409     pt[3].Y = y4;
1410
1411     save_state = prepare_dc(graphics, pen);
1412
1413     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1414
1415     restore_dc(graphics, save_state);
1416
1417     return retval;
1418 }
1419
1420 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1421     INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1422 {
1423     INT save_state;
1424     GpPointF pt[4];
1425     GpStatus retval;
1426
1427     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1428           x2, y2, x3, y3, x4, y4);
1429
1430     if(!graphics || !pen)
1431         return InvalidParameter;
1432
1433     if(graphics->busy)
1434         return ObjectBusy;
1435
1436     pt[0].X = x1;
1437     pt[0].Y = y1;
1438     pt[1].X = x2;
1439     pt[1].Y = y2;
1440     pt[2].X = x3;
1441     pt[2].Y = y3;
1442     pt[3].X = x4;
1443     pt[3].Y = y4;
1444
1445     save_state = prepare_dc(graphics, pen);
1446
1447     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1448
1449     restore_dc(graphics, save_state);
1450
1451     return retval;
1452 }
1453
1454 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1455     GDIPCONST GpPointF *points, INT count)
1456 {
1457     INT i;
1458     GpStatus ret;
1459
1460     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1461
1462     if(!graphics || !pen || !points || (count <= 0))
1463         return InvalidParameter;
1464
1465     if(graphics->busy)
1466         return ObjectBusy;
1467
1468     for(i = 0; i < floor(count / 4); i++){
1469         ret = GdipDrawBezier(graphics, pen,
1470                              points[4*i].X, points[4*i].Y,
1471                              points[4*i + 1].X, points[4*i + 1].Y,
1472                              points[4*i + 2].X, points[4*i + 2].Y,
1473                              points[4*i + 3].X, points[4*i + 3].Y);
1474         if(ret != Ok)
1475             return ret;
1476     }
1477
1478     return Ok;
1479 }
1480
1481 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1482     GDIPCONST GpPoint *points, INT count)
1483 {
1484     GpPointF *pts;
1485     GpStatus ret;
1486     INT i;
1487
1488     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1489
1490     if(!graphics || !pen || !points || (count <= 0))
1491         return InvalidParameter;
1492
1493     if(graphics->busy)
1494         return ObjectBusy;
1495
1496     pts = GdipAlloc(sizeof(GpPointF) * count);
1497     if(!pts)
1498         return OutOfMemory;
1499
1500     for(i = 0; i < count; i++){
1501         pts[i].X = (REAL)points[i].X;
1502         pts[i].Y = (REAL)points[i].Y;
1503     }
1504
1505     ret = GdipDrawBeziers(graphics,pen,pts,count);
1506
1507     GdipFree(pts);
1508
1509     return ret;
1510 }
1511
1512 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1513     GDIPCONST GpPointF *points, INT count)
1514 {
1515     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1516
1517     return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1518 }
1519
1520 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1521     GDIPCONST GpPoint *points, INT count)
1522 {
1523     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1524
1525     return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1526 }
1527
1528 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1529     GDIPCONST GpPointF *points, INT count, REAL tension)
1530 {
1531     GpPath *path;
1532     GpStatus stat;
1533
1534     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1535
1536     if(!graphics || !pen || !points || count <= 0)
1537         return InvalidParameter;
1538
1539     if(graphics->busy)
1540         return ObjectBusy;
1541
1542     if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1543         return stat;
1544
1545     stat = GdipAddPathClosedCurve2(path, points, count, tension);
1546     if(stat != Ok){
1547         GdipDeletePath(path);
1548         return stat;
1549     }
1550
1551     stat = GdipDrawPath(graphics, pen, path);
1552
1553     GdipDeletePath(path);
1554
1555     return stat;
1556 }
1557
1558 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1559     GDIPCONST GpPoint *points, INT count, REAL tension)
1560 {
1561     GpPointF *ptf;
1562     GpStatus stat;
1563     INT i;
1564
1565     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1566
1567     if(!points || count <= 0)
1568         return InvalidParameter;
1569
1570     ptf = GdipAlloc(sizeof(GpPointF)*count);
1571     if(!ptf)
1572         return OutOfMemory;
1573
1574     for(i = 0; i < count; i++){
1575         ptf[i].X = (REAL)points[i].X;
1576         ptf[i].Y = (REAL)points[i].Y;
1577     }
1578
1579     stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1580
1581     GdipFree(ptf);
1582
1583     return stat;
1584 }
1585
1586 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1587     GDIPCONST GpPointF *points, INT count)
1588 {
1589     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1590
1591     return GdipDrawCurve2(graphics,pen,points,count,1.0);
1592 }
1593
1594 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1595     GDIPCONST GpPoint *points, INT count)
1596 {
1597     GpPointF *pointsF;
1598     GpStatus ret;
1599     INT i;
1600
1601     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1602
1603     if(!points)
1604         return InvalidParameter;
1605
1606     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1607     if(!pointsF)
1608         return OutOfMemory;
1609
1610     for(i = 0; i < count; i++){
1611         pointsF[i].X = (REAL)points[i].X;
1612         pointsF[i].Y = (REAL)points[i].Y;
1613     }
1614
1615     ret = GdipDrawCurve(graphics,pen,pointsF,count);
1616     GdipFree(pointsF);
1617
1618     return ret;
1619 }
1620
1621 /* Approximates cardinal spline with Bezier curves. */
1622 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1623     GDIPCONST GpPointF *points, INT count, REAL tension)
1624 {
1625     /* PolyBezier expects count*3-2 points. */
1626     INT i, len_pt = count*3-2, save_state;
1627     GpPointF *pt;
1628     REAL x1, x2, y1, y2;
1629     GpStatus retval;
1630
1631     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1632
1633     if(!graphics || !pen)
1634         return InvalidParameter;
1635
1636     if(graphics->busy)
1637         return ObjectBusy;
1638
1639     if(count < 2)
1640         return InvalidParameter;
1641
1642     pt = GdipAlloc(len_pt * sizeof(GpPointF));
1643     if(!pt)
1644         return OutOfMemory;
1645
1646     tension = tension * TENSION_CONST;
1647
1648     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1649         tension, &x1, &y1);
1650
1651     pt[0].X = points[0].X;
1652     pt[0].Y = points[0].Y;
1653     pt[1].X = x1;
1654     pt[1].Y = y1;
1655
1656     for(i = 0; i < count-2; i++){
1657         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1658
1659         pt[3*i+2].X = x1;
1660         pt[3*i+2].Y = y1;
1661         pt[3*i+3].X = points[i+1].X;
1662         pt[3*i+3].Y = points[i+1].Y;
1663         pt[3*i+4].X = x2;
1664         pt[3*i+4].Y = y2;
1665     }
1666
1667     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1668         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1669
1670     pt[len_pt-2].X = x1;
1671     pt[len_pt-2].Y = y1;
1672     pt[len_pt-1].X = points[count-1].X;
1673     pt[len_pt-1].Y = points[count-1].Y;
1674
1675     save_state = prepare_dc(graphics, pen);
1676
1677     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1678
1679     GdipFree(pt);
1680     restore_dc(graphics, save_state);
1681
1682     return retval;
1683 }
1684
1685 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1686     GDIPCONST GpPoint *points, INT count, REAL tension)
1687 {
1688     GpPointF *pointsF;
1689     GpStatus ret;
1690     INT i;
1691
1692     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1693
1694     if(!points)
1695         return InvalidParameter;
1696
1697     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1698     if(!pointsF)
1699         return OutOfMemory;
1700
1701     for(i = 0; i < count; i++){
1702         pointsF[i].X = (REAL)points[i].X;
1703         pointsF[i].Y = (REAL)points[i].Y;
1704     }
1705
1706     ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1707     GdipFree(pointsF);
1708
1709     return ret;
1710 }
1711
1712 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
1713     GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
1714     REAL tension)
1715 {
1716     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1717
1718     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1719         return InvalidParameter;
1720     }
1721
1722     return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
1723 }
1724
1725 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
1726     GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
1727     REAL tension)
1728 {
1729     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1730
1731     if(count < 0){
1732         return OutOfMemory;
1733     }
1734
1735     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1736         return InvalidParameter;
1737     }
1738
1739     return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
1740 }
1741
1742 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1743     REAL y, REAL width, REAL height)
1744 {
1745     INT save_state;
1746     GpPointF ptf[2];
1747     POINT pti[2];
1748
1749     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1750
1751     if(!graphics || !pen)
1752         return InvalidParameter;
1753
1754     if(graphics->busy)
1755         return ObjectBusy;
1756
1757     ptf[0].X = x;
1758     ptf[0].Y = y;
1759     ptf[1].X = x + width;
1760     ptf[1].Y = y + height;
1761
1762     save_state = prepare_dc(graphics, pen);
1763     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1764
1765     transform_and_round_points(graphics, pti, ptf, 2);
1766
1767     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1768
1769     restore_dc(graphics, save_state);
1770
1771     return Ok;
1772 }
1773
1774 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1775     INT y, INT width, INT height)
1776 {
1777     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1778
1779     return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1780 }
1781
1782
1783 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1784 {
1785     UINT width, height;
1786     GpPointF points[3];
1787
1788     TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1789
1790     if(!graphics || !image)
1791         return InvalidParameter;
1792
1793     GdipGetImageWidth(image, &width);
1794     GdipGetImageHeight(image, &height);
1795
1796     /* FIXME: we should use the graphics and image dpi, somehow */
1797
1798     points[0].X = points[2].X = x;
1799     points[0].Y = points[1].Y = y;
1800     points[1].X = x + width;
1801     points[2].Y = y + height;
1802
1803     return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
1804         UnitPixel, NULL, NULL, NULL);
1805 }
1806
1807 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1808     INT y)
1809 {
1810     TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1811
1812     return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
1813 }
1814
1815 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
1816     REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
1817     GpUnit srcUnit)
1818 {
1819     GpPointF points[3];
1820     TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1821
1822     points[0].X = points[2].X = x;
1823     points[0].Y = points[1].Y = y;
1824
1825     /* FIXME: convert image coordinates to Graphics coordinates? */
1826     points[1].X = x + srcwidth;
1827     points[2].Y = y + srcheight;
1828
1829     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1830         srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
1831 }
1832
1833 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
1834     INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
1835     GpUnit srcUnit)
1836 {
1837     return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1838 }
1839
1840 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
1841     GDIPCONST GpPointF *dstpoints, INT count)
1842 {
1843     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1844     return NotImplemented;
1845 }
1846
1847 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
1848     GDIPCONST GpPoint *dstpoints, INT count)
1849 {
1850     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
1851     return NotImplemented;
1852 }
1853
1854 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1855 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1856      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1857      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1858      DrawImageAbort callback, VOID * callbackData)
1859 {
1860     GpPointF ptf[3];
1861     POINT pti[3];
1862     REAL dx, dy;
1863
1864     TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
1865           count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1866           callbackData);
1867
1868     if(!graphics || !image || !points || count != 3)
1869          return InvalidParameter;
1870
1871     memcpy(ptf, points, 3 * sizeof(GpPointF));
1872     transform_and_round_points(graphics, pti, ptf, 3);
1873
1874     if (image->picture)
1875     {
1876         if(srcUnit == UnitInch)
1877             dx = dy = (REAL) INCH_HIMETRIC;
1878         else if(srcUnit == UnitPixel){
1879             dx = ((REAL) INCH_HIMETRIC) /
1880                  ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1881             dy = ((REAL) INCH_HIMETRIC) /
1882                  ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1883         }
1884         else
1885             return NotImplemented;
1886
1887         if(IPicture_Render(image->picture, graphics->hdc,
1888             pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1889             srcx * dx, srcy * dy,
1890             srcwidth * dx, srcheight * dy,
1891             NULL) != S_OK){
1892             if(callback)
1893                 callback(callbackData);
1894             return GenericError;
1895         }
1896     }
1897     else if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
1898     {
1899         HDC hdc;
1900         GpBitmap* bitmap = (GpBitmap*)image;
1901         int temp_hdc=0, temp_bitmap=0;
1902         HBITMAP hbitmap, old_hbm=NULL;
1903
1904         if (srcUnit == UnitInch)
1905             dx = dy = 96.0; /* FIXME: use the image resolution */
1906         else if (srcUnit == UnitPixel)
1907             dx = dy = 1.0;
1908         else
1909             return NotImplemented;
1910
1911         if (bitmap->format == PixelFormat32bppARGB)
1912         {
1913             BITMAPINFOHEADER bih;
1914             BYTE *temp_bits;
1915
1916             /* we need a bitmap with premultiplied alpha */
1917             hdc = CreateCompatibleDC(0);
1918             temp_hdc = 1;
1919             temp_bitmap = 1;
1920
1921             bih.biSize = sizeof(BITMAPINFOHEADER);
1922             bih.biWidth = bitmap->width;
1923             bih.biHeight = -bitmap->height;
1924             bih.biPlanes = 1;
1925             bih.biBitCount = 32;
1926             bih.biCompression = BI_RGB;
1927             bih.biSizeImage = 0;
1928             bih.biXPelsPerMeter = 0;
1929             bih.biYPelsPerMeter = 0;
1930             bih.biClrUsed = 0;
1931             bih.biClrImportant = 0;
1932
1933             hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
1934                 (void**)&temp_bits, NULL, 0);
1935
1936             convert_32bppARGB_to_32bppPARGB(bitmap->width, bitmap->height,
1937                 temp_bits, bitmap->width*4, bitmap->bits, bitmap->stride);
1938         }
1939         else
1940         {
1941             hbitmap = bitmap->hbitmap;
1942             hdc = bitmap->hdc;
1943             temp_hdc = (hdc == 0);
1944         }
1945
1946         if (temp_hdc)
1947         {
1948             if (!hdc) hdc = CreateCompatibleDC(0);
1949             old_hbm = SelectObject(hdc, hbitmap);
1950         }
1951
1952         if (bitmap->format == PixelFormat32bppARGB || bitmap->format == PixelFormat32bppPARGB)
1953         {
1954             BLENDFUNCTION bf;
1955
1956             bf.BlendOp = AC_SRC_OVER;
1957             bf.BlendFlags = 0;
1958             bf.SourceConstantAlpha = 255;
1959             bf.AlphaFormat = AC_SRC_ALPHA;
1960
1961             GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
1962                 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, bf);
1963         }
1964         else
1965         {
1966             StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
1967                 hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, SRCCOPY);
1968         }
1969
1970         if (temp_hdc)
1971         {
1972             SelectObject(hdc, old_hbm);
1973             DeleteDC(hdc);
1974         }
1975
1976         if (temp_bitmap)
1977             DeleteObject(hbitmap);
1978     }
1979     else
1980     {
1981         ERR("GpImage with no IPicture or HBITMAP?!\n");
1982         return NotImplemented;
1983     }
1984
1985     return Ok;
1986 }
1987
1988 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1989      GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1990      INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1991      DrawImageAbort callback, VOID * callbackData)
1992 {
1993     GpPointF pointsF[3];
1994     INT i;
1995
1996     TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
1997           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1998           callbackData);
1999
2000     if(!points || count!=3)
2001         return InvalidParameter;
2002
2003     for(i = 0; i < count; i++){
2004         pointsF[i].X = (REAL)points[i].X;
2005         pointsF[i].Y = (REAL)points[i].Y;
2006     }
2007
2008     return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
2009                                    (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
2010                                    callback, callbackData);
2011 }
2012
2013 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
2014     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
2015     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
2016     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
2017     VOID * callbackData)
2018 {
2019     GpPointF points[3];
2020
2021     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2022           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2023           srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2024
2025     points[0].X = dstx;
2026     points[0].Y = dsty;
2027     points[1].X = dstx + dstwidth;
2028     points[1].Y = dsty;
2029     points[2].X = dstx;
2030     points[2].Y = dsty + dstheight;
2031
2032     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2033                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2034 }
2035
2036 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
2037         INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
2038         INT srcwidth, INT srcheight, GpUnit srcUnit,
2039         GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
2040         VOID * callbackData)
2041 {
2042     GpPointF points[3];
2043
2044     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2045           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2046           srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2047
2048     points[0].X = dstx;
2049     points[0].Y = dsty;
2050     points[1].X = dstx + dstwidth;
2051     points[1].Y = dsty;
2052     points[2].X = dstx;
2053     points[2].Y = dsty + dstheight;
2054
2055     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2056                srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2057 }
2058
2059 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
2060     REAL x, REAL y, REAL width, REAL height)
2061 {
2062     RectF bounds;
2063     GpUnit unit;
2064     GpStatus ret;
2065
2066     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
2067
2068     if(!graphics || !image)
2069         return InvalidParameter;
2070
2071     ret = GdipGetImageBounds(image, &bounds, &unit);
2072     if(ret != Ok)
2073         return ret;
2074
2075     return GdipDrawImageRectRect(graphics, image, x, y, width, height,
2076                                  bounds.X, bounds.Y, bounds.Width, bounds.Height,
2077                                  unit, NULL, NULL, NULL);
2078 }
2079
2080 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
2081     INT x, INT y, INT width, INT height)
2082 {
2083     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
2084
2085     return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
2086 }
2087
2088 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
2089     REAL y1, REAL x2, REAL y2)
2090 {
2091     INT save_state;
2092     GpPointF pt[2];
2093     GpStatus retval;
2094
2095     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2096
2097     if(!pen || !graphics)
2098         return InvalidParameter;
2099
2100     if(graphics->busy)
2101         return ObjectBusy;
2102
2103     pt[0].X = x1;
2104     pt[0].Y = y1;
2105     pt[1].X = x2;
2106     pt[1].Y = y2;
2107
2108     save_state = prepare_dc(graphics, pen);
2109
2110     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2111
2112     restore_dc(graphics, save_state);
2113
2114     return retval;
2115 }
2116
2117 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2118     INT y1, INT x2, INT y2)
2119 {
2120     INT save_state;
2121     GpPointF pt[2];
2122     GpStatus retval;
2123
2124     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2125
2126     if(!pen || !graphics)
2127         return InvalidParameter;
2128
2129     if(graphics->busy)
2130         return ObjectBusy;
2131
2132     pt[0].X = (REAL)x1;
2133     pt[0].Y = (REAL)y1;
2134     pt[1].X = (REAL)x2;
2135     pt[1].Y = (REAL)y2;
2136
2137     save_state = prepare_dc(graphics, pen);
2138
2139     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2140
2141     restore_dc(graphics, save_state);
2142
2143     return retval;
2144 }
2145
2146 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2147     GpPointF *points, INT count)
2148 {
2149     INT save_state;
2150     GpStatus retval;
2151
2152     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2153
2154     if(!pen || !graphics || (count < 2))
2155         return InvalidParameter;
2156
2157     if(graphics->busy)
2158         return ObjectBusy;
2159
2160     save_state = prepare_dc(graphics, pen);
2161
2162     retval = draw_polyline(graphics, pen, points, count, TRUE);
2163
2164     restore_dc(graphics, save_state);
2165
2166     return retval;
2167 }
2168
2169 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2170     GpPoint *points, INT count)
2171 {
2172     INT save_state;
2173     GpStatus retval;
2174     GpPointF *ptf = NULL;
2175     int i;
2176
2177     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2178
2179     if(!pen || !graphics || (count < 2))
2180         return InvalidParameter;
2181
2182     if(graphics->busy)
2183         return ObjectBusy;
2184
2185     ptf = GdipAlloc(count * sizeof(GpPointF));
2186     if(!ptf) return OutOfMemory;
2187
2188     for(i = 0; i < count; i ++){
2189         ptf[i].X = (REAL) points[i].X;
2190         ptf[i].Y = (REAL) points[i].Y;
2191     }
2192
2193     save_state = prepare_dc(graphics, pen);
2194
2195     retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2196
2197     restore_dc(graphics, save_state);
2198
2199     GdipFree(ptf);
2200     return retval;
2201 }
2202
2203 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2204 {
2205     INT save_state;
2206     GpStatus retval;
2207
2208     TRACE("(%p, %p, %p)\n", graphics, pen, path);
2209
2210     if(!pen || !graphics)
2211         return InvalidParameter;
2212
2213     if(graphics->busy)
2214         return ObjectBusy;
2215
2216     save_state = prepare_dc(graphics, pen);
2217
2218     retval = draw_poly(graphics, pen, path->pathdata.Points,
2219                        path->pathdata.Types, path->pathdata.Count, TRUE);
2220
2221     restore_dc(graphics, save_state);
2222
2223     return retval;
2224 }
2225
2226 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2227     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2228 {
2229     INT save_state;
2230
2231     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2232             width, height, startAngle, sweepAngle);
2233
2234     if(!graphics || !pen)
2235         return InvalidParameter;
2236
2237     if(graphics->busy)
2238         return ObjectBusy;
2239
2240     save_state = prepare_dc(graphics, pen);
2241     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2242
2243     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2244
2245     restore_dc(graphics, save_state);
2246
2247     return Ok;
2248 }
2249
2250 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
2251     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2252 {
2253     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2254             width, height, startAngle, sweepAngle);
2255
2256     return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2257 }
2258
2259 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
2260     REAL y, REAL width, REAL height)
2261 {
2262     INT save_state;
2263     GpPointF ptf[4];
2264     POINT pti[4];
2265
2266     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2267
2268     if(!pen || !graphics)
2269         return InvalidParameter;
2270
2271     if(graphics->busy)
2272         return ObjectBusy;
2273
2274     ptf[0].X = x;
2275     ptf[0].Y = y;
2276     ptf[1].X = x + width;
2277     ptf[1].Y = y;
2278     ptf[2].X = x + width;
2279     ptf[2].Y = y + height;
2280     ptf[3].X = x;
2281     ptf[3].Y = y + height;
2282
2283     save_state = prepare_dc(graphics, pen);
2284     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2285
2286     transform_and_round_points(graphics, pti, ptf, 4);
2287     Polygon(graphics->hdc, pti, 4);
2288
2289     restore_dc(graphics, save_state);
2290
2291     return Ok;
2292 }
2293
2294 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
2295     INT y, INT width, INT height)
2296 {
2297     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2298
2299     return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2300 }
2301
2302 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
2303     GDIPCONST GpRectF* rects, INT count)
2304 {
2305     GpPointF *ptf;
2306     POINT *pti;
2307     INT save_state, i;
2308
2309     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2310
2311     if(!graphics || !pen || !rects || count < 1)
2312         return InvalidParameter;
2313
2314     if(graphics->busy)
2315         return ObjectBusy;
2316
2317     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
2318     pti = GdipAlloc(4 * count * sizeof(POINT));
2319
2320     if(!ptf || !pti){
2321         GdipFree(ptf);
2322         GdipFree(pti);
2323         return OutOfMemory;
2324     }
2325
2326     for(i = 0; i < count; i++){
2327         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
2328         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
2329         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
2330         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
2331     }
2332
2333     save_state = prepare_dc(graphics, pen);
2334     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2335
2336     transform_and_round_points(graphics, pti, ptf, 4 * count);
2337
2338     for(i = 0; i < count; i++)
2339         Polygon(graphics->hdc, &pti[4 * i], 4);
2340
2341     restore_dc(graphics, save_state);
2342
2343     GdipFree(ptf);
2344     GdipFree(pti);
2345
2346     return Ok;
2347 }
2348
2349 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
2350     GDIPCONST GpRect* rects, INT count)
2351 {
2352     GpRectF *rectsF;
2353     GpStatus ret;
2354     INT i;
2355
2356     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2357
2358     if(!rects || count<=0)
2359         return InvalidParameter;
2360
2361     rectsF = GdipAlloc(sizeof(GpRectF) * count);
2362     if(!rectsF)
2363         return OutOfMemory;
2364
2365     for(i = 0;i < count;i++){
2366         rectsF[i].X      = (REAL)rects[i].X;
2367         rectsF[i].Y      = (REAL)rects[i].Y;
2368         rectsF[i].Width  = (REAL)rects[i].Width;
2369         rectsF[i].Height = (REAL)rects[i].Height;
2370     }
2371
2372     ret = GdipDrawRectangles(graphics, pen, rectsF, count);
2373     GdipFree(rectsF);
2374
2375     return ret;
2376 }
2377
2378 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
2379     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
2380     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
2381 {
2382     HRGN rgn = NULL;
2383     HFONT gdifont;
2384     LOGFONTW lfw;
2385     TEXTMETRICW textmet;
2386     GpPointF pt[3], rectcpy[4];
2387     POINT corners[4];
2388     WCHAR* stringdup;
2389     REAL angle, ang_cos, ang_sin, rel_width, rel_height;
2390     INT sum = 0, height = 0, offsety = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
2391         nheight, lineend;
2392     SIZE size;
2393     POINT drawbase;
2394     UINT drawflags;
2395     RECT drawcoord;
2396
2397     TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
2398         length, font, debugstr_rectf(rect), format, brush);
2399
2400     if(!graphics || !string || !font || !brush || !rect)
2401         return InvalidParameter;
2402
2403     if((brush->bt != BrushTypeSolidColor)){
2404         FIXME("not implemented for given parameters\n");
2405         return NotImplemented;
2406     }
2407
2408     if(format){
2409         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2410
2411         /* Should be no need to explicitly test for StringAlignmentNear as
2412          * that is default behavior if no alignment is passed. */
2413         if(format->vertalign != StringAlignmentNear){
2414             RectF bounds;
2415             GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
2416
2417             if(format->vertalign == StringAlignmentCenter)
2418                 offsety = (rect->Height - bounds.Height) / 2;
2419             else if(format->vertalign == StringAlignmentFar)
2420                 offsety = (rect->Height - bounds.Height);
2421         }
2422     }
2423
2424     if(length == -1) length = lstrlenW(string);
2425
2426     stringdup = GdipAlloc(length * sizeof(WCHAR));
2427     if(!stringdup) return OutOfMemory;
2428
2429     save_state = SaveDC(graphics->hdc);
2430     SetBkMode(graphics->hdc, TRANSPARENT);
2431     SetTextColor(graphics->hdc, brush->lb.lbColor);
2432
2433     pt[0].X = 0.0;
2434     pt[0].Y = 0.0;
2435     pt[1].X = 1.0;
2436     pt[1].Y = 0.0;
2437     pt[2].X = 0.0;
2438     pt[2].Y = 1.0;
2439     GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
2440     angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
2441     ang_cos = cos(angle);
2442     ang_sin = sin(angle);
2443     rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
2444                      (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
2445     rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
2446                       (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
2447
2448     rectcpy[3].X = rectcpy[0].X = rect->X;
2449     rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
2450     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
2451     rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
2452     transform_and_round_points(graphics, corners, rectcpy, 4);
2453
2454     if (roundr(rect->Width) == 0)
2455         nwidth = INT_MAX;
2456     else
2457         nwidth = roundr(rel_width * rect->Width);
2458
2459     if (roundr(rect->Height) == 0)
2460         nheight = INT_MAX;
2461     else
2462         nheight = roundr(rel_height * rect->Height);
2463
2464     if (roundr(rect->Width) != 0 && roundr(rect->Height) != 0)
2465     {
2466         /* FIXME: If only the width or only the height is 0, we should probably still clip */
2467         rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
2468         SelectClipRgn(graphics->hdc, rgn);
2469     }
2470
2471     /* Use gdi to find the font, then perform transformations on it (height,
2472      * width, angle). */
2473     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2474     GetTextMetricsW(graphics->hdc, &textmet);
2475     lfw = font->lfw;
2476
2477     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
2478     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
2479
2480     lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
2481
2482     gdifont = CreateFontIndirectW(&lfw);
2483     DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
2484
2485     for(i = 0, j = 0; i < length; i++){
2486         if(!isprintW(string[i]) && (string[i] != '\n'))
2487             continue;
2488
2489         stringdup[j] = string[i];
2490         j++;
2491     }
2492
2493     length = j;
2494
2495     if (!format || format->align == StringAlignmentNear)
2496     {
2497         drawbase.x = corners[0].x;
2498         drawbase.y = corners[0].y;
2499         drawflags = DT_NOCLIP | DT_EXPANDTABS;
2500     }
2501     else if (format->align == StringAlignmentCenter)
2502     {
2503         drawbase.x = (corners[0].x + corners[1].x)/2;
2504         drawbase.y = (corners[0].y + corners[1].y)/2;
2505         drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
2506     }
2507     else /* (format->align == StringAlignmentFar) */
2508     {
2509         drawbase.x = corners[1].x;
2510         drawbase.y = corners[1].y;
2511         drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
2512     }
2513
2514     while(sum < length){
2515         drawcoord.left = drawcoord.right = drawbase.x + roundr(ang_sin * (REAL) height);
2516         drawcoord.top = drawcoord.bottom = drawbase.y + roundr(ang_cos * (REAL) height);
2517
2518         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2519                               nwidth, &fit, NULL, &size);
2520         fitcpy = fit;
2521
2522         if(fit == 0){
2523             DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, drawflags);
2524             break;
2525         }
2526
2527         for(lret = 0; lret < fit; lret++)
2528             if(*(stringdup + sum + lret) == '\n')
2529                 break;
2530
2531         /* Line break code (may look strange, but it imitates windows). */
2532         if(lret < fit)
2533             lineend = fit = lret;    /* this is not an off-by-one error */
2534         else if(fit < (length - sum)){
2535             if(*(stringdup + sum + fit) == ' ')
2536                 while(*(stringdup + sum + fit) == ' ')
2537                     fit++;
2538             else
2539                 while(*(stringdup + sum + fit - 1) != ' '){
2540                     fit--;
2541
2542                     if(*(stringdup + sum + fit) == '\t')
2543                         break;
2544
2545                     if(fit == 0){
2546                         fit = fitcpy;
2547                         break;
2548                     }
2549                 }
2550             lineend = fit;
2551             while(*(stringdup + sum + lineend - 1) == ' ' ||
2552                   *(stringdup + sum + lineend - 1) == '\t')
2553                 lineend--;
2554         }
2555         else
2556             lineend = fit;
2557         DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, lineend),
2558                   &drawcoord, drawflags);
2559
2560         sum += fit + (lret < fitcpy ? 1 : 0);
2561         height += size.cy;
2562
2563         if(height > nheight)
2564             break;
2565
2566         /* Stop if this was a linewrap (but not if it was a linebreak). */
2567         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2568             break;
2569     }
2570
2571     GdipFree(stringdup);
2572     DeleteObject(rgn);
2573     DeleteObject(gdifont);
2574
2575     RestoreDC(graphics->hdc, save_state);
2576
2577     return Ok;
2578 }
2579
2580 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
2581     GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
2582 {
2583     GpPath *path;
2584     GpStatus stat;
2585
2586     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2587             count, tension, fill);
2588
2589     if(!graphics || !brush || !points)
2590         return InvalidParameter;
2591
2592     if(graphics->busy)
2593         return ObjectBusy;
2594
2595     stat = GdipCreatePath(fill, &path);
2596     if(stat != Ok)
2597         return stat;
2598
2599     stat = GdipAddPathClosedCurve2(path, points, count, tension);
2600     if(stat != Ok){
2601         GdipDeletePath(path);
2602         return stat;
2603     }
2604
2605     stat = GdipFillPath(graphics, brush, path);
2606     if(stat != Ok){
2607         GdipDeletePath(path);
2608         return stat;
2609     }
2610
2611     GdipDeletePath(path);
2612
2613     return Ok;
2614 }
2615
2616 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
2617     GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
2618 {
2619     GpPointF *ptf;
2620     GpStatus stat;
2621     INT i;
2622
2623     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2624             count, tension, fill);
2625
2626     if(!points || count <= 0)
2627         return InvalidParameter;
2628
2629     ptf = GdipAlloc(sizeof(GpPointF)*count);
2630     if(!ptf)
2631         return OutOfMemory;
2632
2633     for(i = 0;i < count;i++){
2634         ptf[i].X = (REAL)points[i].X;
2635         ptf[i].Y = (REAL)points[i].Y;
2636     }
2637
2638     stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2639
2640     GdipFree(ptf);
2641
2642     return stat;
2643 }
2644
2645 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2646     REAL y, REAL width, REAL height)
2647 {
2648     INT save_state;
2649     GpPointF ptf[2];
2650     POINT pti[2];
2651
2652     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2653
2654     if(!graphics || !brush)
2655         return InvalidParameter;
2656
2657     if(graphics->busy)
2658         return ObjectBusy;
2659
2660     ptf[0].X = x;
2661     ptf[0].Y = y;
2662     ptf[1].X = x + width;
2663     ptf[1].Y = y + height;
2664
2665     save_state = SaveDC(graphics->hdc);
2666     EndPath(graphics->hdc);
2667
2668     transform_and_round_points(graphics, pti, ptf, 2);
2669
2670     BeginPath(graphics->hdc);
2671     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2672     EndPath(graphics->hdc);
2673
2674     brush_fill_path(graphics, brush);
2675
2676     RestoreDC(graphics->hdc, save_state);
2677
2678     return Ok;
2679 }
2680
2681 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2682     INT y, INT width, INT height)
2683 {
2684     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2685
2686     return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2687 }
2688
2689 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2690 {
2691     INT save_state;
2692     GpStatus retval;
2693
2694     TRACE("(%p, %p, %p)\n", graphics, brush, path);
2695
2696     if(!brush || !graphics || !path)
2697         return InvalidParameter;
2698
2699     if(graphics->busy)
2700         return ObjectBusy;
2701
2702     save_state = SaveDC(graphics->hdc);
2703     EndPath(graphics->hdc);
2704     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2705                                                                     : WINDING));
2706
2707     BeginPath(graphics->hdc);
2708     retval = draw_poly(graphics, NULL, path->pathdata.Points,
2709                        path->pathdata.Types, path->pathdata.Count, FALSE);
2710
2711     if(retval != Ok)
2712         goto end;
2713
2714     EndPath(graphics->hdc);
2715     brush_fill_path(graphics, brush);
2716
2717     retval = Ok;
2718
2719 end:
2720     RestoreDC(graphics->hdc, save_state);
2721
2722     return retval;
2723 }
2724
2725 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2726     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2727 {
2728     INT save_state;
2729
2730     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2731             graphics, brush, x, y, width, height, startAngle, sweepAngle);
2732
2733     if(!graphics || !brush)
2734         return InvalidParameter;
2735
2736     if(graphics->busy)
2737         return ObjectBusy;
2738
2739     save_state = SaveDC(graphics->hdc);
2740     EndPath(graphics->hdc);
2741
2742     BeginPath(graphics->hdc);
2743     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2744     EndPath(graphics->hdc);
2745
2746     brush_fill_path(graphics, brush);
2747
2748     RestoreDC(graphics->hdc, save_state);
2749
2750     return Ok;
2751 }
2752
2753 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2754     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2755 {
2756     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
2757             graphics, brush, x, y, width, height, startAngle, sweepAngle);
2758
2759     return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2760 }
2761
2762 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2763     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2764 {
2765     INT save_state;
2766     GpPointF *ptf = NULL;
2767     POINT *pti = NULL;
2768     GpStatus retval = Ok;
2769
2770     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2771
2772     if(!graphics || !brush || !points || !count)
2773         return InvalidParameter;
2774
2775     if(graphics->busy)
2776         return ObjectBusy;
2777
2778     ptf = GdipAlloc(count * sizeof(GpPointF));
2779     pti = GdipAlloc(count * sizeof(POINT));
2780     if(!ptf || !pti){
2781         retval = OutOfMemory;
2782         goto end;
2783     }
2784
2785     memcpy(ptf, points, count * sizeof(GpPointF));
2786
2787     save_state = SaveDC(graphics->hdc);
2788     EndPath(graphics->hdc);
2789     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2790                                                                   : WINDING));
2791
2792     transform_and_round_points(graphics, pti, ptf, count);
2793
2794     BeginPath(graphics->hdc);
2795     Polygon(graphics->hdc, pti, count);
2796     EndPath(graphics->hdc);
2797
2798     brush_fill_path(graphics, brush);
2799
2800     RestoreDC(graphics->hdc, save_state);
2801
2802 end:
2803     GdipFree(ptf);
2804     GdipFree(pti);
2805
2806     return retval;
2807 }
2808
2809 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2810     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2811 {
2812     INT save_state, i;
2813     GpPointF *ptf = NULL;
2814     POINT *pti = NULL;
2815     GpStatus retval = Ok;
2816
2817     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2818
2819     if(!graphics || !brush || !points || !count)
2820         return InvalidParameter;
2821
2822     if(graphics->busy)
2823         return ObjectBusy;
2824
2825     ptf = GdipAlloc(count * sizeof(GpPointF));
2826     pti = GdipAlloc(count * sizeof(POINT));
2827     if(!ptf || !pti){
2828         retval = OutOfMemory;
2829         goto end;
2830     }
2831
2832     for(i = 0; i < count; i ++){
2833         ptf[i].X = (REAL) points[i].X;
2834         ptf[i].Y = (REAL) points[i].Y;
2835     }
2836
2837     save_state = SaveDC(graphics->hdc);
2838     EndPath(graphics->hdc);
2839     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2840                                                                   : WINDING));
2841
2842     transform_and_round_points(graphics, pti, ptf, count);
2843
2844     BeginPath(graphics->hdc);
2845     Polygon(graphics->hdc, pti, count);
2846     EndPath(graphics->hdc);
2847
2848     brush_fill_path(graphics, brush);
2849
2850     RestoreDC(graphics->hdc, save_state);
2851
2852 end:
2853     GdipFree(ptf);
2854     GdipFree(pti);
2855
2856     return retval;
2857 }
2858
2859 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2860     GDIPCONST GpPointF *points, INT count)
2861 {
2862     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2863
2864     return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2865 }
2866
2867 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2868     GDIPCONST GpPoint *points, INT count)
2869 {
2870     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2871
2872     return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2873 }
2874
2875 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2876     REAL x, REAL y, REAL width, REAL height)
2877 {
2878     INT save_state;
2879     GpPointF ptf[4];
2880     POINT pti[4];
2881
2882     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2883
2884     if(!graphics || !brush)
2885         return InvalidParameter;
2886
2887     if(graphics->busy)
2888         return ObjectBusy;
2889
2890     ptf[0].X = x;
2891     ptf[0].Y = y;
2892     ptf[1].X = x + width;
2893     ptf[1].Y = y;
2894     ptf[2].X = x + width;
2895     ptf[2].Y = y + height;
2896     ptf[3].X = x;
2897     ptf[3].Y = y + height;
2898
2899     save_state = SaveDC(graphics->hdc);
2900     EndPath(graphics->hdc);
2901
2902     transform_and_round_points(graphics, pti, ptf, 4);
2903
2904     BeginPath(graphics->hdc);
2905     Polygon(graphics->hdc, pti, 4);
2906     EndPath(graphics->hdc);
2907
2908     brush_fill_path(graphics, brush);
2909
2910     RestoreDC(graphics->hdc, save_state);
2911
2912     return Ok;
2913 }
2914
2915 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2916     INT x, INT y, INT width, INT height)
2917 {
2918     INT save_state;
2919     GpPointF ptf[4];
2920     POINT pti[4];
2921
2922     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2923
2924     if(!graphics || !brush)
2925         return InvalidParameter;
2926
2927     if(graphics->busy)
2928         return ObjectBusy;
2929
2930     ptf[0].X = x;
2931     ptf[0].Y = y;
2932     ptf[1].X = x + width;
2933     ptf[1].Y = y;
2934     ptf[2].X = x + width;
2935     ptf[2].Y = y + height;
2936     ptf[3].X = x;
2937     ptf[3].Y = y + height;
2938
2939     save_state = SaveDC(graphics->hdc);
2940     EndPath(graphics->hdc);
2941
2942     transform_and_round_points(graphics, pti, ptf, 4);
2943
2944     BeginPath(graphics->hdc);
2945     Polygon(graphics->hdc, pti, 4);
2946     EndPath(graphics->hdc);
2947
2948     brush_fill_path(graphics, brush);
2949
2950     RestoreDC(graphics->hdc, save_state);
2951
2952     return Ok;
2953 }
2954
2955 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2956     INT count)
2957 {
2958     GpStatus ret;
2959     INT i;
2960
2961     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2962
2963     if(!rects)
2964         return InvalidParameter;
2965
2966     for(i = 0; i < count; i++){
2967         ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2968         if(ret != Ok)   return ret;
2969     }
2970
2971     return Ok;
2972 }
2973
2974 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2975     INT count)
2976 {
2977     GpRectF *rectsF;
2978     GpStatus ret;
2979     INT i;
2980
2981     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2982
2983     if(!rects || count <= 0)
2984         return InvalidParameter;
2985
2986     rectsF = GdipAlloc(sizeof(GpRectF)*count);
2987     if(!rectsF)
2988         return OutOfMemory;
2989
2990     for(i = 0; i < count; i++){
2991         rectsF[i].X      = (REAL)rects[i].X;
2992         rectsF[i].Y      = (REAL)rects[i].Y;
2993         rectsF[i].X      = (REAL)rects[i].Width;
2994         rectsF[i].Height = (REAL)rects[i].Height;
2995     }
2996
2997     ret = GdipFillRectangles(graphics,brush,rectsF,count);
2998     GdipFree(rectsF);
2999
3000     return ret;
3001 }
3002
3003 /*****************************************************************************
3004  * GdipFillRegion [GDIPLUS.@]
3005  */
3006 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3007         GpRegion* region)
3008 {
3009     INT save_state;
3010     GpStatus status;
3011     HRGN hrgn;
3012     RECT rc;
3013
3014     TRACE("(%p, %p, %p)\n", graphics, brush, region);
3015
3016     if (!(graphics && brush && region))
3017         return InvalidParameter;
3018
3019     if(graphics->busy)
3020         return ObjectBusy;
3021
3022     status = GdipGetRegionHRgn(region, graphics, &hrgn);
3023     if(status != Ok)
3024         return status;
3025
3026     save_state = SaveDC(graphics->hdc);
3027     EndPath(graphics->hdc);
3028
3029     ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3030
3031     if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3032     {
3033         BeginPath(graphics->hdc);
3034         Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3035         EndPath(graphics->hdc);
3036
3037         brush_fill_path(graphics, brush);
3038     }
3039
3040     RestoreDC(graphics->hdc, save_state);
3041
3042     DeleteObject(hrgn);
3043
3044     return Ok;
3045 }
3046
3047 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3048 {
3049     static int calls;
3050
3051     if(!graphics)
3052         return InvalidParameter;
3053
3054     if(graphics->busy)
3055         return ObjectBusy;
3056
3057     if(!(calls++))
3058         FIXME("not implemented\n");
3059
3060     return NotImplemented;
3061 }
3062
3063 /*****************************************************************************
3064  * GdipGetClipBounds [GDIPLUS.@]
3065  */
3066 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3067 {
3068     TRACE("(%p, %p)\n", graphics, rect);
3069
3070     if(!graphics)
3071         return InvalidParameter;
3072
3073     if(graphics->busy)
3074         return ObjectBusy;
3075
3076     return GdipGetRegionBounds(graphics->clip, graphics, rect);
3077 }
3078
3079 /*****************************************************************************
3080  * GdipGetClipBoundsI [GDIPLUS.@]
3081  */
3082 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
3083 {
3084     TRACE("(%p, %p)\n", graphics, rect);
3085
3086     if(!graphics)
3087         return InvalidParameter;
3088
3089     if(graphics->busy)
3090         return ObjectBusy;
3091
3092     return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
3093 }
3094
3095 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
3096 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
3097     CompositingMode *mode)
3098 {
3099     TRACE("(%p, %p)\n", graphics, mode);
3100
3101     if(!graphics || !mode)
3102         return InvalidParameter;
3103
3104     if(graphics->busy)
3105         return ObjectBusy;
3106
3107     *mode = graphics->compmode;
3108
3109     return Ok;
3110 }
3111
3112 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3113 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3114     CompositingQuality *quality)
3115 {
3116     TRACE("(%p, %p)\n", graphics, quality);
3117
3118     if(!graphics || !quality)
3119         return InvalidParameter;
3120
3121     if(graphics->busy)
3122         return ObjectBusy;
3123
3124     *quality = graphics->compqual;
3125
3126     return Ok;
3127 }
3128
3129 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3130 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3131     InterpolationMode *mode)
3132 {
3133     TRACE("(%p, %p)\n", graphics, mode);
3134
3135     if(!graphics || !mode)
3136         return InvalidParameter;
3137
3138     if(graphics->busy)
3139         return ObjectBusy;
3140
3141     *mode = graphics->interpolation;
3142
3143     return Ok;
3144 }
3145
3146 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3147 {
3148     if(!graphics || !argb)
3149         return InvalidParameter;
3150
3151     if(graphics->busy)
3152         return ObjectBusy;
3153
3154     FIXME("(%p, %p): stub\n", graphics, argb);
3155
3156     return NotImplemented;
3157 }
3158
3159 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3160 {
3161     TRACE("(%p, %p)\n", graphics, scale);
3162
3163     if(!graphics || !scale)
3164         return InvalidParameter;
3165
3166     if(graphics->busy)
3167         return ObjectBusy;
3168
3169     *scale = graphics->scale;
3170
3171     return Ok;
3172 }
3173
3174 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3175 {
3176     TRACE("(%p, %p)\n", graphics, unit);
3177
3178     if(!graphics || !unit)
3179         return InvalidParameter;
3180
3181     if(graphics->busy)
3182         return ObjectBusy;
3183
3184     *unit = graphics->unit;
3185
3186     return Ok;
3187 }
3188
3189 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3190 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3191     *mode)
3192 {
3193     TRACE("(%p, %p)\n", graphics, mode);
3194
3195     if(!graphics || !mode)
3196         return InvalidParameter;
3197
3198     if(graphics->busy)
3199         return ObjectBusy;
3200
3201     *mode = graphics->pixeloffset;
3202
3203     return Ok;
3204 }
3205
3206 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3207 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3208 {
3209     TRACE("(%p, %p)\n", graphics, mode);
3210
3211     if(!graphics || !mode)
3212         return InvalidParameter;
3213
3214     if(graphics->busy)
3215         return ObjectBusy;
3216
3217     *mode = graphics->smoothing;
3218
3219     return Ok;
3220 }
3221
3222 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3223 {
3224     TRACE("(%p, %p)\n", graphics, contrast);
3225
3226     if(!graphics || !contrast)
3227         return InvalidParameter;
3228
3229     *contrast = graphics->textcontrast;
3230
3231     return Ok;
3232 }
3233
3234 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3235 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3236     TextRenderingHint *hint)
3237 {
3238     TRACE("(%p, %p)\n", graphics, hint);
3239
3240     if(!graphics || !hint)
3241         return InvalidParameter;
3242
3243     if(graphics->busy)
3244         return ObjectBusy;
3245
3246     *hint = graphics->texthint;
3247
3248     return Ok;
3249 }
3250
3251 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3252 {
3253     GpRegion *clip_rgn;
3254     GpStatus stat;
3255
3256     TRACE("(%p, %p)\n", graphics, rect);
3257
3258     if(!graphics || !rect)
3259         return InvalidParameter;
3260
3261     if(graphics->busy)
3262         return ObjectBusy;
3263
3264     /* intersect window and graphics clipping regions */
3265     if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3266         return stat;
3267
3268     if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
3269         goto cleanup;
3270
3271     /* get bounds of the region */
3272     stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
3273
3274 cleanup:
3275     GdipDeleteRegion(clip_rgn);
3276
3277     return stat;
3278 }
3279
3280 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
3281 {
3282     GpRectF rectf;
3283     GpStatus stat;
3284
3285     TRACE("(%p, %p)\n", graphics, rect);
3286
3287     if(!graphics || !rect)
3288         return InvalidParameter;
3289
3290     if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
3291     {
3292         rect->X = roundr(rectf.X);
3293         rect->Y = roundr(rectf.Y);
3294         rect->Width  = roundr(rectf.Width);
3295         rect->Height = roundr(rectf.Height);
3296     }
3297
3298     return stat;
3299 }
3300
3301 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3302 {
3303     TRACE("(%p, %p)\n", graphics, matrix);
3304
3305     if(!graphics || !matrix)
3306         return InvalidParameter;
3307
3308     if(graphics->busy)
3309         return ObjectBusy;
3310
3311     *matrix = *graphics->worldtrans;
3312     return Ok;
3313 }
3314
3315 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
3316 {
3317     GpSolidFill *brush;
3318     GpStatus stat;
3319     GpRectF wnd_rect;
3320
3321     TRACE("(%p, %x)\n", graphics, color);
3322
3323     if(!graphics)
3324         return InvalidParameter;
3325
3326     if(graphics->busy)
3327         return ObjectBusy;
3328
3329     if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
3330         return stat;
3331
3332     if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
3333         GdipDeleteBrush((GpBrush*)brush);
3334         return stat;
3335     }
3336
3337     GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
3338                                                  wnd_rect.Width, wnd_rect.Height);
3339
3340     GdipDeleteBrush((GpBrush*)brush);
3341
3342     return Ok;
3343 }
3344
3345 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
3346 {
3347     TRACE("(%p, %p)\n", graphics, res);
3348
3349     if(!graphics || !res)
3350         return InvalidParameter;
3351
3352     return GdipIsEmptyRegion(graphics->clip, graphics, res);
3353 }
3354
3355 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
3356 {
3357     GpStatus stat;
3358     GpRegion* rgn;
3359     GpPointF pt;
3360
3361     TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
3362
3363     if(!graphics || !result)
3364         return InvalidParameter;
3365
3366     if(graphics->busy)
3367         return ObjectBusy;
3368
3369     pt.X = x;
3370     pt.Y = y;
3371     if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3372                    CoordinateSpaceWorld, &pt, 1)) != Ok)
3373         return stat;
3374
3375     if((stat = GdipCreateRegion(&rgn)) != Ok)
3376         return stat;
3377
3378     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3379         goto cleanup;
3380
3381     stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
3382
3383 cleanup:
3384     GdipDeleteRegion(rgn);
3385     return stat;
3386 }
3387
3388 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
3389 {
3390     return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
3391 }
3392
3393 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
3394 {
3395     GpStatus stat;
3396     GpRegion* rgn;
3397     GpPointF pts[2];
3398
3399     TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
3400
3401     if(!graphics || !result)
3402         return InvalidParameter;
3403
3404     if(graphics->busy)
3405         return ObjectBusy;
3406
3407     pts[0].X = x;
3408     pts[0].Y = y;
3409     pts[1].X = x + width;
3410     pts[1].Y = y + height;
3411
3412     if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3413                     CoordinateSpaceWorld, pts, 2)) != Ok)
3414         return stat;
3415
3416     pts[1].X -= pts[0].X;
3417     pts[1].Y -= pts[0].Y;
3418
3419     if((stat = GdipCreateRegion(&rgn)) != Ok)
3420         return stat;
3421
3422     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3423         goto cleanup;
3424
3425     stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
3426
3427 cleanup:
3428     GdipDeleteRegion(rgn);
3429     return stat;
3430 }
3431
3432 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
3433 {
3434     return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
3435 }
3436
3437 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
3438         GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
3439         GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
3440         INT regionCount, GpRegion** regions)
3441 {
3442     if (!(graphics && string && font && layoutRect && stringFormat && regions))
3443         return InvalidParameter;
3444
3445     FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
3446             length, font, layoutRect, stringFormat, regionCount, regions);
3447
3448     return NotImplemented;
3449 }
3450
3451 /* Find the smallest rectangle that bounds the text when it is printed in rect
3452  * according to the format options listed in format. If rect has 0 width and
3453  * height, then just find the smallest rectangle that bounds the text when it's
3454  * printed at location (rect->X, rect-Y). */
3455 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
3456     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3457     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
3458     INT *codepointsfitted, INT *linesfilled)
3459 {
3460     HFONT oldfont;
3461     WCHAR* stringdup;
3462     INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
3463         nheight, lineend;
3464     SIZE size;
3465
3466     TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
3467         debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
3468         bounds, codepointsfitted, linesfilled);
3469
3470     if(!graphics || !string || !font || !rect)
3471         return InvalidParameter;
3472
3473     if(linesfilled) *linesfilled = 0;
3474     if(codepointsfitted) *codepointsfitted = 0;
3475
3476     if(format)
3477         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
3478
3479     if(length == -1) length = lstrlenW(string);
3480
3481     stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
3482     if(!stringdup) return OutOfMemory;
3483
3484     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3485     nwidth = roundr(rect->Width);
3486     nheight = roundr(rect->Height);
3487
3488     if((nwidth == 0) && (nheight == 0))
3489         nwidth = nheight = INT_MAX;
3490
3491     for(i = 0, j = 0; i < length; i++){
3492         if(!isprintW(string[i]) && (string[i] != '\n'))
3493             continue;
3494
3495         stringdup[j] = string[i];
3496         j++;
3497     }
3498
3499     stringdup[j] = 0;
3500     length = j;
3501
3502     while(sum < length){
3503         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
3504                               nwidth, &fit, NULL, &size);
3505         fitcpy = fit;
3506
3507         if(fit == 0)
3508             break;
3509
3510         for(lret = 0; lret < fit; lret++)
3511             if(*(stringdup + sum + lret) == '\n')
3512                 break;
3513
3514         /* Line break code (may look strange, but it imitates windows). */
3515         if(lret < fit)
3516             lineend = fit = lret;    /* this is not an off-by-one error */
3517         else if(fit < (length - sum)){
3518             if(*(stringdup + sum + fit) == ' ')
3519                 while(*(stringdup + sum + fit) == ' ')
3520                     fit++;
3521             else
3522                 while(*(stringdup + sum + fit - 1) != ' '){
3523                     fit--;
3524
3525                     if(*(stringdup + sum + fit) == '\t')
3526                         break;
3527
3528                     if(fit == 0){
3529                         fit = fitcpy;
3530                         break;
3531                     }
3532                 }
3533             lineend = fit;
3534             while(*(stringdup + sum + lineend - 1) == ' ' ||
3535                   *(stringdup + sum + lineend - 1) == '\t')
3536                 lineend--;
3537         }
3538         else
3539             lineend = fit;
3540
3541         GetTextExtentExPointW(graphics->hdc, stringdup + sum, lineend,
3542                               nwidth, &j, NULL, &size);
3543
3544         sum += fit + (lret < fitcpy ? 1 : 0);
3545         if(codepointsfitted) *codepointsfitted = sum;
3546
3547         height += size.cy;
3548         if(linesfilled) *linesfilled += size.cy;
3549         max_width = max(max_width, size.cx);
3550
3551         if(height > nheight)
3552             break;
3553
3554         /* Stop if this was a linewrap (but not if it was a linebreak). */
3555         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
3556             break;
3557     }
3558
3559     bounds->X = rect->X;
3560     bounds->Y = rect->Y;
3561     bounds->Width = (REAL)max_width;
3562     bounds->Height = (REAL) min(height, nheight);
3563
3564     GdipFree(stringdup);
3565     DeleteObject(SelectObject(graphics->hdc, oldfont));
3566
3567     return Ok;
3568 }
3569
3570 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
3571 {
3572     TRACE("(%p)\n", graphics);
3573
3574     if(!graphics)
3575         return InvalidParameter;
3576
3577     if(graphics->busy)
3578         return ObjectBusy;
3579
3580     return GdipSetInfinite(graphics->clip);
3581 }
3582
3583 GpStatus WINGDIPAPI GdipResetWorldTransform(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     graphics->worldtrans->matrix[0] = 1.0;
3594     graphics->worldtrans->matrix[1] = 0.0;
3595     graphics->worldtrans->matrix[2] = 0.0;
3596     graphics->worldtrans->matrix[3] = 1.0;
3597     graphics->worldtrans->matrix[4] = 0.0;
3598     graphics->worldtrans->matrix[5] = 0.0;
3599
3600     return Ok;
3601 }
3602
3603 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
3604 {
3605     return GdipEndContainer(graphics, state);
3606 }
3607
3608 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
3609     GpMatrixOrder order)
3610 {
3611     TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
3612
3613     if(!graphics)
3614         return InvalidParameter;
3615
3616     if(graphics->busy)
3617         return ObjectBusy;
3618
3619     return GdipRotateMatrix(graphics->worldtrans, angle, order);
3620 }
3621
3622 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
3623 {
3624     return GdipBeginContainer2(graphics, state);
3625 }
3626
3627 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
3628         GraphicsContainer *state)
3629 {
3630     GraphicsContainerItem *container;
3631     GpStatus sts;
3632
3633     TRACE("(%p, %p)\n", graphics, state);
3634
3635     if(!graphics || !state)
3636         return InvalidParameter;
3637
3638     sts = init_container(&container, graphics);
3639     if(sts != Ok)
3640         return sts;
3641
3642     list_add_head(&graphics->containers, &container->entry);
3643     *state = graphics->contid = container->contid;
3644
3645     return Ok;
3646 }
3647
3648 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
3649 {
3650     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3651     return NotImplemented;
3652 }
3653
3654 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
3655 {
3656     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
3657     return NotImplemented;
3658 }
3659
3660 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
3661 {
3662     FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
3663     return NotImplemented;
3664 }
3665
3666 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
3667 {
3668     GpStatus sts;
3669     GraphicsContainerItem *container, *container2;
3670
3671     TRACE("(%p, %x)\n", graphics, state);
3672
3673     if(!graphics)
3674         return InvalidParameter;
3675
3676     LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
3677         if(container->contid == state)
3678             break;
3679     }
3680
3681     /* did not find a matching container */
3682     if(&container->entry == &graphics->containers)
3683         return Ok;
3684
3685     sts = restore_container(graphics, container);
3686     if(sts != Ok)
3687         return sts;
3688
3689     /* remove all of the containers on top of the found container */
3690     LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
3691         if(container->contid == state)
3692             break;
3693         list_remove(&container->entry);
3694         delete_container(container);
3695     }
3696
3697     list_remove(&container->entry);
3698     delete_container(container);
3699
3700     return Ok;
3701 }
3702
3703 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
3704     REAL sy, GpMatrixOrder order)
3705 {
3706     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
3707
3708     if(!graphics)
3709         return InvalidParameter;
3710
3711     if(graphics->busy)
3712         return ObjectBusy;
3713
3714     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
3715 }
3716
3717 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
3718     CombineMode mode)
3719 {
3720     TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
3721
3722     if(!graphics || !srcgraphics)
3723         return InvalidParameter;
3724
3725     return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
3726 }
3727
3728 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
3729     CompositingMode mode)
3730 {
3731     TRACE("(%p, %d)\n", graphics, mode);
3732
3733     if(!graphics)
3734         return InvalidParameter;
3735
3736     if(graphics->busy)
3737         return ObjectBusy;
3738
3739     graphics->compmode = mode;
3740
3741     return Ok;
3742 }
3743
3744 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
3745     CompositingQuality quality)
3746 {
3747     TRACE("(%p, %d)\n", graphics, quality);
3748
3749     if(!graphics)
3750         return InvalidParameter;
3751
3752     if(graphics->busy)
3753         return ObjectBusy;
3754
3755     graphics->compqual = quality;
3756
3757     return Ok;
3758 }
3759
3760 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
3761     InterpolationMode mode)
3762 {
3763     TRACE("(%p, %d)\n", graphics, mode);
3764
3765     if(!graphics)
3766         return InvalidParameter;
3767
3768     if(graphics->busy)
3769         return ObjectBusy;
3770
3771     graphics->interpolation = mode;
3772
3773     return Ok;
3774 }
3775
3776 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
3777 {
3778     TRACE("(%p, %.2f)\n", graphics, scale);
3779
3780     if(!graphics || (scale <= 0.0))
3781         return InvalidParameter;
3782
3783     if(graphics->busy)
3784         return ObjectBusy;
3785
3786     graphics->scale = scale;
3787
3788     return Ok;
3789 }
3790
3791 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
3792 {
3793     TRACE("(%p, %d)\n", graphics, unit);
3794
3795     if(!graphics)
3796         return InvalidParameter;
3797
3798     if(graphics->busy)
3799         return ObjectBusy;
3800
3801     if(unit == UnitWorld)
3802         return InvalidParameter;
3803
3804     graphics->unit = unit;
3805
3806     return Ok;
3807 }
3808
3809 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3810     mode)
3811 {
3812     TRACE("(%p, %d)\n", graphics, mode);
3813
3814     if(!graphics)
3815         return InvalidParameter;
3816
3817     if(graphics->busy)
3818         return ObjectBusy;
3819
3820     graphics->pixeloffset = mode;
3821
3822     return Ok;
3823 }
3824
3825 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
3826 {
3827     static int calls;
3828
3829     TRACE("(%p,%i,%i)\n", graphics, x, y);
3830
3831     if (!(calls++))
3832         FIXME("not implemented\n");
3833
3834     return NotImplemented;
3835 }
3836
3837 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
3838 {
3839     TRACE("(%p, %d)\n", graphics, mode);
3840
3841     if(!graphics)
3842         return InvalidParameter;
3843
3844     if(graphics->busy)
3845         return ObjectBusy;
3846
3847     graphics->smoothing = mode;
3848
3849     return Ok;
3850 }
3851
3852 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
3853 {
3854     TRACE("(%p, %d)\n", graphics, contrast);
3855
3856     if(!graphics)
3857         return InvalidParameter;
3858
3859     graphics->textcontrast = contrast;
3860
3861     return Ok;
3862 }
3863
3864 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
3865     TextRenderingHint hint)
3866 {
3867     TRACE("(%p, %d)\n", graphics, hint);
3868
3869     if(!graphics)
3870         return InvalidParameter;
3871
3872     if(graphics->busy)
3873         return ObjectBusy;
3874
3875     graphics->texthint = hint;
3876
3877     return Ok;
3878 }
3879
3880 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3881 {
3882     TRACE("(%p, %p)\n", graphics, matrix);
3883
3884     if(!graphics || !matrix)
3885         return InvalidParameter;
3886
3887     if(graphics->busy)
3888         return ObjectBusy;
3889
3890     GdipDeleteMatrix(graphics->worldtrans);
3891     return GdipCloneMatrix(matrix, &graphics->worldtrans);
3892 }
3893
3894 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
3895     REAL dy, GpMatrixOrder order)
3896 {
3897     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
3898
3899     if(!graphics)
3900         return InvalidParameter;
3901
3902     if(graphics->busy)
3903         return ObjectBusy;
3904
3905     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
3906 }
3907
3908 /*****************************************************************************
3909  * GdipSetClipHrgn [GDIPLUS.@]
3910  */
3911 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
3912 {
3913     GpRegion *region;
3914     GpStatus status;
3915
3916     TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
3917
3918     if(!graphics)
3919         return InvalidParameter;
3920
3921     status = GdipCreateRegionHrgn(hrgn, &region);
3922     if(status != Ok)
3923         return status;
3924
3925     status = GdipSetClipRegion(graphics, region, mode);
3926
3927     GdipDeleteRegion(region);
3928     return status;
3929 }
3930
3931 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
3932 {
3933     TRACE("(%p, %p, %d)\n", graphics, path, mode);
3934
3935     if(!graphics)
3936         return InvalidParameter;
3937
3938     if(graphics->busy)
3939         return ObjectBusy;
3940
3941     return GdipCombineRegionPath(graphics->clip, path, mode);
3942 }
3943
3944 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
3945                                     REAL width, REAL height,
3946                                     CombineMode mode)
3947 {
3948     GpRectF rect;
3949
3950     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
3951
3952     if(!graphics)
3953         return InvalidParameter;
3954
3955     if(graphics->busy)
3956         return ObjectBusy;
3957
3958     rect.X = x;
3959     rect.Y = y;
3960     rect.Width  = width;
3961     rect.Height = height;
3962
3963     return GdipCombineRegionRect(graphics->clip, &rect, mode);
3964 }
3965
3966 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
3967                                      INT width, INT height,
3968                                      CombineMode mode)
3969 {
3970     TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
3971
3972     if(!graphics)
3973         return InvalidParameter;
3974
3975     if(graphics->busy)
3976         return ObjectBusy;
3977
3978     return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
3979 }
3980
3981 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
3982                                       CombineMode mode)
3983 {
3984     TRACE("(%p, %p, %d)\n", graphics, region, mode);
3985
3986     if(!graphics || !region)
3987         return InvalidParameter;
3988
3989     if(graphics->busy)
3990         return ObjectBusy;
3991
3992     return GdipCombineRegionRegion(graphics->clip, region, mode);
3993 }
3994
3995 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
3996     UINT limitDpi)
3997 {
3998     static int calls;
3999
4000     if(!(calls++))
4001         FIXME("not implemented\n");
4002
4003     return NotImplemented;
4004 }
4005
4006 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
4007     INT count)
4008 {
4009     INT save_state;
4010     POINT *pti;
4011
4012     TRACE("(%p, %p, %d)\n", graphics, points, count);
4013
4014     if(!graphics || !pen || count<=0)
4015         return InvalidParameter;
4016
4017     if(graphics->busy)
4018         return ObjectBusy;
4019
4020     pti = GdipAlloc(sizeof(POINT) * count);
4021
4022     save_state = prepare_dc(graphics, pen);
4023     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
4024
4025     transform_and_round_points(graphics, pti, (GpPointF*)points, count);
4026     Polygon(graphics->hdc, pti, count);
4027
4028     restore_dc(graphics, save_state);
4029     GdipFree(pti);
4030
4031     return Ok;
4032 }
4033
4034 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
4035     INT count)
4036 {
4037     GpStatus ret;
4038     GpPointF *ptf;
4039     INT i;
4040
4041     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
4042
4043     if(count<=0)    return InvalidParameter;
4044     ptf = GdipAlloc(sizeof(GpPointF) * count);
4045
4046     for(i = 0;i < count; i++){
4047         ptf[i].X = (REAL)points[i].X;
4048         ptf[i].Y = (REAL)points[i].Y;
4049     }
4050
4051     ret = GdipDrawPolygon(graphics,pen,ptf,count);
4052     GdipFree(ptf);
4053
4054     return ret;
4055 }
4056
4057 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
4058 {
4059     TRACE("(%p, %p)\n", graphics, dpi);
4060
4061     if(!graphics || !dpi)
4062         return InvalidParameter;
4063
4064     if(graphics->busy)
4065         return ObjectBusy;
4066
4067     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
4068
4069     return Ok;
4070 }
4071
4072 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
4073 {
4074     TRACE("(%p, %p)\n", graphics, dpi);
4075
4076     if(!graphics || !dpi)
4077         return InvalidParameter;
4078
4079     if(graphics->busy)
4080         return ObjectBusy;
4081
4082     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
4083
4084     return Ok;
4085 }
4086
4087 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
4088     GpMatrixOrder order)
4089 {
4090     GpMatrix m;
4091     GpStatus ret;
4092
4093     TRACE("(%p, %p, %d)\n", graphics, matrix, order);
4094
4095     if(!graphics || !matrix)
4096         return InvalidParameter;
4097
4098     if(graphics->busy)
4099         return ObjectBusy;
4100
4101     m = *(graphics->worldtrans);
4102
4103     ret = GdipMultiplyMatrix(&m, matrix, order);
4104     if(ret == Ok)
4105         *(graphics->worldtrans) = m;
4106
4107     return ret;
4108 }
4109
4110 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
4111 {
4112     TRACE("(%p, %p)\n", graphics, hdc);
4113
4114     if(!graphics || !hdc)
4115         return InvalidParameter;
4116
4117     if(graphics->busy)
4118         return ObjectBusy;
4119
4120     *hdc = graphics->hdc;
4121     graphics->busy = TRUE;
4122
4123     return Ok;
4124 }
4125
4126 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
4127 {
4128     TRACE("(%p, %p)\n", graphics, hdc);
4129
4130     if(!graphics)
4131         return InvalidParameter;
4132
4133     if(graphics->hdc != hdc || !(graphics->busy))
4134         return InvalidParameter;
4135
4136     graphics->busy = FALSE;
4137
4138     return Ok;
4139 }
4140
4141 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
4142 {
4143     GpRegion *clip;
4144     GpStatus status;
4145
4146     TRACE("(%p, %p)\n", graphics, region);
4147
4148     if(!graphics || !region)
4149         return InvalidParameter;
4150
4151     if(graphics->busy)
4152         return ObjectBusy;
4153
4154     if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
4155         return status;
4156
4157     /* free everything except root node and header */
4158     delete_element(&region->node);
4159     memcpy(region, clip, sizeof(GpRegion));
4160
4161     return Ok;
4162 }
4163
4164 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
4165                                         GpCoordinateSpace src_space, GpPointF *points, INT count)
4166 {
4167     GpMatrix *matrix;
4168     GpStatus stat;
4169     REAL unitscale;
4170
4171     if(!graphics || !points || count <= 0)
4172         return InvalidParameter;
4173
4174     if(graphics->busy)
4175         return ObjectBusy;
4176
4177     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4178
4179     if (src_space == dst_space) return Ok;
4180
4181     stat = GdipCreateMatrix(&matrix);
4182     if (stat == Ok)
4183     {
4184         unitscale = convert_unit(graphics->hdc, graphics->unit);
4185
4186         if(graphics->unit != UnitDisplay)
4187             unitscale *= graphics->scale;
4188
4189         /* transform from src_space to CoordinateSpacePage */
4190         switch (src_space)
4191         {
4192         case CoordinateSpaceWorld:
4193             GdipMultiplyMatrix(matrix, graphics->worldtrans, MatrixOrderAppend);
4194             break;
4195         case CoordinateSpacePage:
4196             break;
4197         case CoordinateSpaceDevice:
4198             GdipScaleMatrix(matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
4199             break;
4200         }
4201
4202         /* transform from CoordinateSpacePage to dst_space */
4203         switch (dst_space)
4204         {
4205         case CoordinateSpaceWorld:
4206             {
4207                 GpMatrix *inverted_transform;
4208                 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
4209                 if (stat == Ok)
4210                 {
4211                     stat = GdipInvertMatrix(inverted_transform);
4212                     if (stat == Ok)
4213                         GdipMultiplyMatrix(matrix, inverted_transform, MatrixOrderAppend);
4214                     GdipDeleteMatrix(inverted_transform);
4215                 }
4216                 break;
4217             }
4218         case CoordinateSpacePage:
4219             break;
4220         case CoordinateSpaceDevice:
4221             GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
4222             break;
4223         }
4224
4225         if (stat == Ok)
4226             stat = GdipTransformMatrixPoints(matrix, points, count);
4227
4228         GdipDeleteMatrix(matrix);
4229     }
4230
4231     return stat;
4232 }
4233
4234 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
4235                                          GpCoordinateSpace src_space, GpPoint *points, INT count)
4236 {
4237     GpPointF *pointsF;
4238     GpStatus ret;
4239     INT i;
4240
4241     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4242
4243     if(count <= 0)
4244         return InvalidParameter;
4245
4246     pointsF = GdipAlloc(sizeof(GpPointF) * count);
4247     if(!pointsF)
4248         return OutOfMemory;
4249
4250     for(i = 0; i < count; i++){
4251         pointsF[i].X = (REAL)points[i].X;
4252         pointsF[i].Y = (REAL)points[i].Y;
4253     }
4254
4255     ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
4256
4257     if(ret == Ok)
4258         for(i = 0; i < count; i++){
4259             points[i].X = roundr(pointsF[i].X);
4260             points[i].Y = roundr(pointsF[i].Y);
4261         }
4262     GdipFree(pointsF);
4263
4264     return ret;
4265 }
4266
4267 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
4268 {
4269     FIXME("\n");
4270
4271     return NULL;
4272 }
4273
4274 /*****************************************************************************
4275  * GdipTranslateClip [GDIPLUS.@]
4276  */
4277 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
4278 {
4279     TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
4280
4281     if(!graphics)
4282         return InvalidParameter;
4283
4284     if(graphics->busy)
4285         return ObjectBusy;
4286
4287     return GdipTranslateRegion(graphics->clip, dx, dy);
4288 }
4289
4290 /*****************************************************************************
4291  * GdipTranslateClipI [GDIPLUS.@]
4292  */
4293 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
4294 {
4295     TRACE("(%p, %d, %d)\n", graphics, dx, dy);
4296
4297     if(!graphics)
4298         return InvalidParameter;
4299
4300     if(graphics->busy)
4301         return ObjectBusy;
4302
4303     return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
4304 }
4305
4306
4307 /*****************************************************************************
4308  * GdipMeasureDriverString [GDIPLUS.@]
4309  */
4310 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4311                                             GDIPCONST GpFont *font, GDIPCONST PointF *positions,
4312                                             INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
4313 {
4314     FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
4315     return NotImplemented;
4316 }
4317
4318 /*****************************************************************************
4319  * GdipDrawDriverString [GDIPLUS.@]
4320  */
4321 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
4322                                          GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
4323                                          GDIPCONST PointF *positions, INT flags,
4324                                          GDIPCONST GpMatrix *matrix )
4325 {
4326     FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
4327     return NotImplemented;
4328 }
4329
4330 /*****************************************************************************
4331  * GdipRecordMetafileI [GDIPLUS.@]
4332  */
4333 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
4334                                         MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
4335 {
4336     FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
4337     return NotImplemented;
4338 }