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