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