gdiplus: Added GdipDrawImageRectRect.
[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
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "wingdi.h"
26
27 #define COBJMACROS
28 #include "objbase.h"
29 #include "ocidl.h"
30 #include "olectl.h"
31 #include "ole2.h"
32
33 #include "winreg.h"
34 #include "shlwapi.h"
35
36 #include "gdiplus.h"
37 #include "gdiplus_private.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
41
42 /* looks-right constants */
43 #define TENSION_CONST (0.3)
44 #define ANCHOR_WIDTH (2.0)
45 #define MAX_ITERS (50)
46
47 /* Converts angle (in degrees) to x/y coordinates */
48 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
49 {
50     REAL radAngle, hypotenuse;
51
52     radAngle = deg2rad(angle);
53     hypotenuse = 50.0; /* arbitrary */
54
55     *x = x_0 + cos(radAngle) * hypotenuse;
56     *y = y_0 + sin(radAngle) * hypotenuse;
57 }
58
59 /* Converts from gdiplus path point type to gdi path point type. */
60 static BYTE convert_path_point_type(BYTE type)
61 {
62     BYTE ret;
63
64     switch(type & PathPointTypePathTypeMask){
65         case PathPointTypeBezier:
66             ret = PT_BEZIERTO;
67             break;
68         case PathPointTypeLine:
69             ret = PT_LINETO;
70             break;
71         case PathPointTypeStart:
72             ret = PT_MOVETO;
73             break;
74         default:
75             ERR("Bad point type\n");
76             return 0;
77     }
78
79     if(type & PathPointTypeCloseSubpath)
80         ret |= PT_CLOSEFIGURE;
81
82     return ret;
83 }
84
85 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
86 {
87     HPEN gdipen;
88     REAL width;
89     INT save_state = SaveDC(graphics->hdc), i, numdashes;
90     GpPointF pt[2];
91     DWORD dash_array[MAX_DASHLEN];
92
93     EndPath(graphics->hdc);
94
95     /* Get an estimate for the amount the pen width is affected by the world
96      * transform. (This is similar to what some of the wine drivers do.) */
97     pt[0].X = 0.0;
98     pt[0].Y = 0.0;
99     pt[1].X = 1.0;
100     pt[1].Y = 1.0;
101     GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
102     width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
103                  (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
104
105     width *= pen->width * convert_unit(graphics->hdc,
106                           pen->unit == UnitWorld ? graphics->unit : pen->unit);
107
108     if(pen->dash == DashStyleCustom){
109         numdashes = min(pen->numdashes, MAX_DASHLEN);
110
111         TRACE("dashes are: ");
112         for(i = 0; i < numdashes; i++){
113             dash_array[i] = roundr(width * pen->dashes[i]);
114             TRACE("%d, ", dash_array[i]);
115         }
116         TRACE("\n and the pen style is %x\n", pen->style);
117
118         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
119                               numdashes, dash_array);
120     }
121     else
122         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
123
124     SelectObject(graphics->hdc, gdipen);
125
126     return save_state;
127 }
128
129 static void restore_dc(GpGraphics *graphics, INT state)
130 {
131     DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
132     RestoreDC(graphics->hdc, state);
133 }
134
135 /* This helper applies all the changes that the points listed in ptf need in
136  * order to be drawn on the device context.  In the end, this should include at
137  * least:
138  *  -scaling by page unit
139  *  -applying world transformation
140  *  -converting from float to int
141  * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
142  * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
143  * gdi to draw, and these functions would irreparably mess with line widths.
144  */
145 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
146     GpPointF *ptf, INT count)
147 {
148     REAL unitscale;
149     GpMatrix *matrix;
150     int i;
151
152     unitscale = convert_unit(graphics->hdc, graphics->unit);
153
154     /* apply page scale */
155     if(graphics->unit != UnitDisplay)
156         unitscale *= graphics->scale;
157
158     GdipCloneMatrix(graphics->worldtrans, &matrix);
159     GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
160     GdipTransformMatrixPoints(matrix, ptf, count);
161     GdipDeleteMatrix(matrix);
162
163     for(i = 0; i < count; i++){
164         pti[i].x = roundr(ptf[i].X);
165         pti[i].y = roundr(ptf[i].Y);
166     }
167 }
168
169 /* GdipDrawPie/GdipFillPie helper function */
170 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
171     REAL height, REAL startAngle, REAL sweepAngle)
172 {
173     GpPointF ptf[4];
174     POINT pti[4];
175
176     ptf[0].X = x;
177     ptf[0].Y = y;
178     ptf[1].X = x + width;
179     ptf[1].Y = y + height;
180
181     deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
182     deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
183
184     transform_and_round_points(graphics, pti, ptf, 4);
185
186     Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
187         pti[2].y, pti[3].x, pti[3].y);
188 }
189
190 /* GdipDrawCurve helper function.
191  * Calculates Bezier points from cardinal spline points. */
192 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
193     REAL *y1, REAL *x2, REAL *y2)
194 {
195     REAL xdiff, ydiff;
196
197     /* calculate tangent */
198     xdiff = pts[2].X - pts[0].X;
199     ydiff = pts[2].Y - pts[0].Y;
200
201     /* apply tangent to get control points */
202     *x1 = pts[1].X - tension * xdiff;
203     *y1 = pts[1].Y - tension * ydiff;
204     *x2 = pts[1].X + tension * xdiff;
205     *y2 = pts[1].Y + tension * ydiff;
206 }
207
208 /* GdipDrawCurve helper function.
209  * Calculates Bezier points from cardinal spline endpoints. */
210 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
211     REAL tension, REAL *x, REAL *y)
212 {
213     /* tangent at endpoints is the line from the endpoint to the adjacent point */
214     *x = roundr(tension * (xadj - xend) + xend);
215     *y = roundr(tension * (yadj - yend) + yend);
216 }
217
218 /* Draws the linecap the specified color and size on the hdc.  The linecap is in
219  * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
220  * should not be called on an hdc that has a path you care about. */
221 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
222     const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
223 {
224     HGDIOBJ oldbrush = NULL, oldpen = NULL;
225     GpMatrix *matrix = NULL;
226     HBRUSH brush = NULL;
227     HPEN pen = NULL;
228     PointF ptf[4], *custptf = NULL;
229     POINT pt[4], *custpt = NULL;
230     BYTE *tp = NULL;
231     REAL theta, dsmall, dbig, dx, dy = 0.0;
232     INT i, count;
233     LOGBRUSH lb;
234     BOOL customstroke;
235
236     if((x1 == x2) && (y1 == y2))
237         return;
238
239     theta = gdiplus_atan2(y2 - y1, x2 - x1);
240
241     customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
242     if(!customstroke){
243         brush = CreateSolidBrush(color);
244         lb.lbStyle = BS_SOLID;
245         lb.lbColor = color;
246         lb.lbHatch = 0;
247         pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
248                            PS_JOIN_MITER, 1, &lb, 0,
249                            NULL);
250         oldbrush = SelectObject(graphics->hdc, brush);
251         oldpen = SelectObject(graphics->hdc, pen);
252     }
253
254     switch(cap){
255         case LineCapFlat:
256             break;
257         case LineCapSquare:
258         case LineCapSquareAnchor:
259         case LineCapDiamondAnchor:
260             size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
261             if(cap == LineCapDiamondAnchor){
262                 dsmall = cos(theta + M_PI_2) * size;
263                 dbig = sin(theta + M_PI_2) * size;
264             }
265             else{
266                 dsmall = cos(theta + M_PI_4) * size;
267                 dbig = sin(theta + M_PI_4) * size;
268             }
269
270             ptf[0].X = x2 - dsmall;
271             ptf[1].X = x2 + dbig;
272
273             ptf[0].Y = y2 - dbig;
274             ptf[3].Y = y2 + dsmall;
275
276             ptf[1].Y = y2 - dsmall;
277             ptf[2].Y = y2 + dbig;
278
279             ptf[3].X = x2 - dbig;
280             ptf[2].X = x2 + dsmall;
281
282             transform_and_round_points(graphics, pt, ptf, 4);
283             Polygon(graphics->hdc, pt, 4);
284
285             break;
286         case LineCapArrowAnchor:
287             size = size * 4.0 / sqrt(3.0);
288
289             dx = cos(M_PI / 6.0 + theta) * size;
290             dy = sin(M_PI / 6.0 + theta) * size;
291
292             ptf[0].X = x2 - dx;
293             ptf[0].Y = y2 - dy;
294
295             dx = cos(- M_PI / 6.0 + theta) * size;
296             dy = sin(- M_PI / 6.0 + theta) * size;
297
298             ptf[1].X = x2 - dx;
299             ptf[1].Y = y2 - dy;
300
301             ptf[2].X = x2;
302             ptf[2].Y = y2;
303
304             transform_and_round_points(graphics, pt, ptf, 3);
305             Polygon(graphics->hdc, pt, 3);
306
307             break;
308         case LineCapRoundAnchor:
309             dx = dy = ANCHOR_WIDTH * size / 2.0;
310
311             ptf[0].X = x2 - dx;
312             ptf[0].Y = y2 - dy;
313             ptf[1].X = x2 + dx;
314             ptf[1].Y = y2 + dy;
315
316             transform_and_round_points(graphics, pt, ptf, 2);
317             Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
318
319             break;
320         case LineCapTriangle:
321             size = size / 2.0;
322             dx = cos(M_PI_2 + theta) * size;
323             dy = sin(M_PI_2 + theta) * size;
324
325             ptf[0].X = x2 - dx;
326             ptf[0].Y = y2 - dy;
327             ptf[1].X = x2 + dx;
328             ptf[1].Y = y2 + dy;
329
330             dx = cos(theta) * size;
331             dy = sin(theta) * size;
332
333             ptf[2].X = x2 + dx;
334             ptf[2].Y = y2 + dy;
335
336             transform_and_round_points(graphics, pt, ptf, 3);
337             Polygon(graphics->hdc, pt, 3);
338
339             break;
340         case LineCapRound:
341             dx = dy = size / 2.0;
342
343             ptf[0].X = x2 - dx;
344             ptf[0].Y = y2 - dy;
345             ptf[1].X = x2 + dx;
346             ptf[1].Y = y2 + dy;
347
348             dx = -cos(M_PI_2 + theta) * size;
349             dy = -sin(M_PI_2 + theta) * size;
350
351             ptf[2].X = x2 - dx;
352             ptf[2].Y = y2 - dy;
353             ptf[3].X = x2 + dx;
354             ptf[3].Y = y2 + dy;
355
356             transform_and_round_points(graphics, pt, ptf, 4);
357             Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
358                 pt[2].y, pt[3].x, pt[3].y);
359
360             break;
361         case LineCapCustom:
362             if(!custom)
363                 break;
364
365             count = custom->pathdata.Count;
366             custptf = GdipAlloc(count * sizeof(PointF));
367             custpt = GdipAlloc(count * sizeof(POINT));
368             tp = GdipAlloc(count);
369
370             if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
371                 goto custend;
372
373             memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
374
375             GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
376             GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
377                              MatrixOrderAppend);
378             GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
379             GdipTransformMatrixPoints(matrix, custptf, count);
380
381             transform_and_round_points(graphics, custpt, custptf, count);
382
383             for(i = 0; i < count; i++)
384                 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
385
386             if(custom->fill){
387                 BeginPath(graphics->hdc);
388                 PolyDraw(graphics->hdc, custpt, tp, count);
389                 EndPath(graphics->hdc);
390                 StrokeAndFillPath(graphics->hdc);
391             }
392             else
393                 PolyDraw(graphics->hdc, custpt, tp, count);
394
395 custend:
396             GdipFree(custptf);
397             GdipFree(custpt);
398             GdipFree(tp);
399             GdipDeleteMatrix(matrix);
400             break;
401         default:
402             break;
403     }
404
405     if(!customstroke){
406         SelectObject(graphics->hdc, oldbrush);
407         SelectObject(graphics->hdc, oldpen);
408         DeleteObject(brush);
409         DeleteObject(pen);
410     }
411 }
412
413 /* Shortens the line by the given percent by changing x2, y2.
414  * If percent is > 1.0 then the line will change direction.
415  * If percent is negative it can lengthen the line. */
416 static void shorten_line_percent(REAL x1, REAL  y1, REAL *x2, REAL *y2, REAL percent)
417 {
418     REAL dist, theta, dx, dy;
419
420     if((y1 == *y2) && (x1 == *x2))
421         return;
422
423     dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
424     theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
425     dx = cos(theta) * dist;
426     dy = sin(theta) * dist;
427
428     *x2 = *x2 + dx;
429     *y2 = *y2 + dy;
430 }
431
432 /* Shortens the line by the given amount by changing x2, y2.
433  * If the amount is greater than the distance, the line will become length 0.
434  * If the amount is negative, it can lengthen the line. */
435 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
436 {
437     REAL dx, dy, percent;
438
439     dx = *x2 - x1;
440     dy = *y2 - y1;
441     if(dx == 0 && dy == 0)
442         return;
443
444     percent = amt / sqrt(dx * dx + dy * dy);
445     if(percent >= 1.0){
446         *x2 = x1;
447         *y2 = y1;
448         return;
449     }
450
451     shorten_line_percent(x1, y1, x2, y2, percent);
452 }
453
454 /* Draws lines between the given points, and if caps is true then draws an endcap
455  * at the end of the last line. */
456 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
457     GDIPCONST GpPointF * pt, INT count, BOOL caps)
458 {
459     POINT *pti = NULL;
460     GpPointF *ptcopy = NULL;
461     GpStatus status = GenericError;
462
463     if(!count)
464         return Ok;
465
466     pti = GdipAlloc(count * sizeof(POINT));
467     ptcopy = GdipAlloc(count * sizeof(GpPointF));
468
469     if(!pti || !ptcopy){
470         status = OutOfMemory;
471         goto end;
472     }
473
474     memcpy(ptcopy, pt, count * sizeof(GpPointF));
475
476     if(caps){
477         if(pen->endcap == LineCapArrowAnchor)
478             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
479                              &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
480         else if((pen->endcap == LineCapCustom) && pen->customend)
481             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
482                              &ptcopy[count-1].X, &ptcopy[count-1].Y,
483                              pen->customend->inset * pen->width);
484
485         if(pen->startcap == LineCapArrowAnchor)
486             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
487                              &ptcopy[0].X, &ptcopy[0].Y, pen->width);
488         else if((pen->startcap == LineCapCustom) && pen->customstart)
489             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
490                              &ptcopy[0].X, &ptcopy[0].Y,
491                              pen->customstart->inset * pen->width);
492
493         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
494                  pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
495         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
496                          pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);\
497     }
498
499     transform_and_round_points(graphics, pti, ptcopy, count);
500
501     Polyline(graphics->hdc, pti, count);
502
503 end:
504     GdipFree(pti);
505     GdipFree(ptcopy);
506
507     return status;
508 }
509
510 /* Conducts a linear search to find the bezier points that will back off
511  * the endpoint of the curve by a distance of amt. Linear search works
512  * better than binary in this case because there are multiple solutions,
513  * and binary searches often find a bad one. I don't think this is what
514  * Windows does but short of rendering the bezier without GDI's help it's
515  * the best we can do. If rev then work from the start of the passed points
516  * instead of the end. */
517 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
518 {
519     GpPointF origpt[4];
520     REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
521     INT i, first = 0, second = 1, third = 2, fourth = 3;
522
523     if(rev){
524         first = 3;
525         second = 2;
526         third = 1;
527         fourth = 0;
528     }
529
530     origx = pt[fourth].X;
531     origy = pt[fourth].Y;
532     memcpy(origpt, pt, sizeof(GpPointF) * 4);
533
534     for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
535         /* reset bezier points to original values */
536         memcpy(pt, origpt, sizeof(GpPointF) * 4);
537         /* Perform magic on bezier points. Order is important here.*/
538         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
539         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
540         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
541         shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
542         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
543         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
544
545         dx = pt[fourth].X - origx;
546         dy = pt[fourth].Y - origy;
547
548         diff = sqrt(dx * dx + dy * dy);
549         percent += 0.0005 * amt;
550     }
551 }
552
553 /* Draws bezier curves between given points, and if caps is true then draws an
554  * endcap at the end of the last line. */
555 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
556     GDIPCONST GpPointF * pt, INT count, BOOL caps)
557 {
558     POINT *pti;
559     GpPointF *ptcopy;
560     GpStatus status = GenericError;
561
562     if(!count)
563         return Ok;
564
565     pti = GdipAlloc(count * sizeof(POINT));
566     ptcopy = GdipAlloc(count * sizeof(GpPointF));
567
568     if(!pti || !ptcopy){
569         status = OutOfMemory;
570         goto end;
571     }
572
573     memcpy(ptcopy, pt, count * sizeof(GpPointF));
574
575     if(caps){
576         if(pen->endcap == LineCapArrowAnchor)
577             shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
578         else if((pen->endcap == LineCapCustom) && pen->customend)
579             shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
580                                FALSE);
581
582         if(pen->startcap == LineCapArrowAnchor)
583             shorten_bezier_amt(ptcopy, pen->width, TRUE);
584         else if((pen->startcap == LineCapCustom) && pen->customstart)
585             shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
586
587         /* the direction of the line cap is parallel to the direction at the
588          * end of the bezier (which, if it has been shortened, is not the same
589          * as the direction from pt[count-2] to pt[count-1]) */
590         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
591             pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
592             pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
593             pt[count - 1].X, pt[count - 1].Y);
594
595         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
596             pt[0].X - (ptcopy[0].X - ptcopy[1].X),
597             pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
598     }
599
600     transform_and_round_points(graphics, pti, ptcopy, count);
601
602     PolyBezier(graphics->hdc, pti, count);
603
604     status = Ok;
605
606 end:
607     GdipFree(pti);
608     GdipFree(ptcopy);
609
610     return status;
611 }
612
613 /* Draws a combination of bezier curves and lines between points. */
614 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
615     GDIPCONST BYTE * types, INT count, BOOL caps)
616 {
617     POINT *pti = GdipAlloc(count * sizeof(POINT));
618     BYTE *tp = GdipAlloc(count);
619     GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
620     INT i, j;
621     GpStatus status = GenericError;
622
623     if(!count){
624         status = Ok;
625         goto end;
626     }
627     if(!pti || !tp || !ptcopy){
628         status = OutOfMemory;
629         goto end;
630     }
631
632     for(i = 1; i < count; i++){
633         if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
634             if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
635                 || !(types[i + 1] & PathPointTypeBezier)){
636                 ERR("Bad bezier points\n");
637                 goto end;
638             }
639             i += 2;
640         }
641     }
642
643     memcpy(ptcopy, pt, count * sizeof(GpPointF));
644
645     /* If we are drawing caps, go through the points and adjust them accordingly,
646      * and draw the caps. */
647     if(caps){
648         switch(types[count - 1] & PathPointTypePathTypeMask){
649             case PathPointTypeBezier:
650                 if(pen->endcap == LineCapArrowAnchor)
651                     shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
652                 else if((pen->endcap == LineCapCustom) && pen->customend)
653                     shorten_bezier_amt(&ptcopy[count - 4],
654                                        pen->width * pen->customend->inset, FALSE);
655
656                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
657                     pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
658                     pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
659                     pt[count - 1].X, pt[count - 1].Y);
660
661                 break;
662             case PathPointTypeLine:
663                 if(pen->endcap == LineCapArrowAnchor)
664                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
665                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
666                                      pen->width);
667                 else if((pen->endcap == LineCapCustom) && pen->customend)
668                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
669                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
670                                      pen->customend->inset * pen->width);
671
672                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
673                          pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
674                          pt[count - 1].Y);
675
676                 break;
677             default:
678                 ERR("Bad path last point\n");
679                 goto end;
680         }
681
682         /* Find start of points */
683         for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
684             == PathPointTypeStart); j++);
685
686         switch(types[j] & PathPointTypePathTypeMask){
687             case PathPointTypeBezier:
688                 if(pen->startcap == LineCapArrowAnchor)
689                     shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
690                 else if((pen->startcap == LineCapCustom) && pen->customstart)
691                     shorten_bezier_amt(&ptcopy[j - 1],
692                                        pen->width * pen->customstart->inset, TRUE);
693
694                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
695                     pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
696                     pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
697                     pt[j - 1].X, pt[j - 1].Y);
698
699                 break;
700             case PathPointTypeLine:
701                 if(pen->startcap == LineCapArrowAnchor)
702                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
703                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
704                                      pen->width);
705                 else if((pen->startcap == LineCapCustom) && pen->customstart)
706                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
707                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
708                                      pen->customstart->inset * pen->width);
709
710                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
711                          pt[j].X, pt[j].Y, pt[j - 1].X,
712                          pt[j - 1].Y);
713
714                 break;
715             default:
716                 ERR("Bad path points\n");
717                 goto end;
718         }
719     }
720
721     transform_and_round_points(graphics, pti, ptcopy, count);
722
723     for(i = 0; i < count; i++){
724         tp[i] = convert_path_point_type(types[i]);
725     }
726
727     PolyDraw(graphics->hdc, pti, tp, count);
728
729     status = Ok;
730
731 end:
732     GdipFree(pti);
733     GdipFree(ptcopy);
734     GdipFree(tp);
735
736     return status;
737 }
738
739 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
740 {
741     GpStatus retval;
742
743     if(hdc == NULL)
744         return OutOfMemory;
745
746     if(graphics == NULL)
747         return InvalidParameter;
748
749     *graphics = GdipAlloc(sizeof(GpGraphics));
750     if(!*graphics)  return OutOfMemory;
751
752     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
753         GdipFree(*graphics);
754         return retval;
755     }
756
757     (*graphics)->hdc = hdc;
758     (*graphics)->hwnd = NULL;
759     (*graphics)->smoothing = SmoothingModeDefault;
760     (*graphics)->compqual = CompositingQualityDefault;
761     (*graphics)->interpolation = InterpolationModeDefault;
762     (*graphics)->pixeloffset = PixelOffsetModeDefault;
763     (*graphics)->unit = UnitDisplay;
764     (*graphics)->scale = 1.0;
765
766     return Ok;
767 }
768
769 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
770 {
771     GpStatus ret;
772
773     if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
774         return ret;
775
776     (*graphics)->hwnd = hwnd;
777
778     return Ok;
779 }
780
781 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
782     GpMetafile **metafile)
783 {
784     static int calls;
785
786     if(!hemf || !metafile)
787         return InvalidParameter;
788
789     if(!(calls++))
790         FIXME("not implemented\n");
791
792     return NotImplemented;
793 }
794
795 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
796     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
797 {
798     IStream *stream = NULL;
799     UINT read;
800     BYTE* copy;
801     HENHMETAFILE hemf;
802     GpStatus retval = GenericError;
803
804     if(!hwmf || !metafile || !placeable)
805         return InvalidParameter;
806
807     *metafile = NULL;
808     read = GetMetaFileBitsEx(hwmf, 0, NULL);
809     if(!read)
810         return GenericError;
811     copy = GdipAlloc(read);
812     GetMetaFileBitsEx(hwmf, read, copy);
813
814     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
815     GdipFree(copy);
816
817     read = GetEnhMetaFileBits(hemf, 0, NULL);
818     copy = GdipAlloc(read);
819     GetEnhMetaFileBits(hemf, read, copy);
820     DeleteEnhMetaFile(hemf);
821
822     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
823         ERR("could not make stream\n");
824         GdipFree(copy);
825         goto err;
826     }
827
828     *metafile = GdipAlloc(sizeof(GpMetafile));
829     if(!*metafile){
830         retval = OutOfMemory;
831         goto err;
832     }
833
834     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
835         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
836         goto err;
837
838
839     (*metafile)->image.type = ImageTypeMetafile;
840     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
841     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
842     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
843                     - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
844     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
845                    - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
846     (*metafile)->unit = UnitInch;
847
848     if(delete)
849         DeleteMetaFile(hwmf);
850
851     return Ok;
852
853 err:
854     GdipFree(*metafile);
855     IStream_Release(stream);
856     return retval;
857 }
858
859 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
860     UINT access, IStream **stream)
861 {
862     DWORD dwMode;
863     HRESULT ret;
864
865     if(!stream || !filename)
866         return InvalidParameter;
867
868     if(access & GENERIC_WRITE)
869         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
870     else if(access & GENERIC_READ)
871         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
872     else
873         return InvalidParameter;
874
875     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
876
877     return hresult_to_status(ret);
878 }
879
880 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
881 {
882     if(!graphics) return InvalidParameter;
883     if(graphics->hwnd)
884         ReleaseDC(graphics->hwnd, graphics->hdc);
885
886     GdipDeleteMatrix(graphics->worldtrans);
887     HeapFree(GetProcessHeap(), 0, graphics);
888
889     return Ok;
890 }
891
892 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
893     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
894 {
895     INT save_state, num_pts;
896     GpPointF points[MAX_ARC_PTS];
897     GpStatus retval;
898
899     if(!graphics || !pen)
900         return InvalidParameter;
901
902     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
903
904     save_state = prepare_dc(graphics, pen);
905
906     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
907
908     restore_dc(graphics, save_state);
909
910     return retval;
911 }
912
913 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
914     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
915 {
916     INT save_state;
917     GpPointF pt[4];
918     GpStatus retval;
919
920     if(!graphics || !pen)
921         return InvalidParameter;
922
923     pt[0].X = x1;
924     pt[0].Y = y1;
925     pt[1].X = x2;
926     pt[1].Y = y2;
927     pt[2].X = x3;
928     pt[2].Y = y3;
929     pt[3].X = x4;
930     pt[3].Y = y4;
931
932     save_state = prepare_dc(graphics, pen);
933
934     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
935
936     restore_dc(graphics, save_state);
937
938     return retval;
939 }
940
941 /* Approximates cardinal spline with Bezier curves. */
942 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
943     GDIPCONST GpPointF *points, INT count, REAL tension)
944 {
945     /* PolyBezier expects count*3-2 points. */
946     INT i, len_pt = count*3-2, save_state;
947     GpPointF *pt;
948     REAL x1, x2, y1, y2;
949     GpStatus retval;
950
951     if(!graphics || !pen)
952         return InvalidParameter;
953
954     pt = GdipAlloc(len_pt * sizeof(GpPointF));
955     tension = tension * TENSION_CONST;
956
957     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
958         tension, &x1, &y1);
959
960     pt[0].X = points[0].X;
961     pt[0].Y = points[0].Y;
962     pt[1].X = x1;
963     pt[1].Y = y1;
964
965     for(i = 0; i < count-2; i++){
966         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
967
968         pt[3*i+2].X = x1;
969         pt[3*i+2].Y = y1;
970         pt[3*i+3].X = points[i+1].X;
971         pt[3*i+3].Y = points[i+1].Y;
972         pt[3*i+4].X = x2;
973         pt[3*i+4].Y = y2;
974     }
975
976     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
977         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
978
979     pt[len_pt-2].X = x1;
980     pt[len_pt-2].Y = y1;
981     pt[len_pt-1].X = points[count-1].X;
982     pt[len_pt-1].Y = points[count-1].Y;
983
984     save_state = prepare_dc(graphics, pen);
985
986     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
987
988     GdipFree(pt);
989     restore_dc(graphics, save_state);
990
991     return retval;
992 }
993
994 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
995     INT y)
996 {
997     UINT width, height, srcw, srch;
998
999     if(!graphics || !image)
1000         return InvalidParameter;
1001
1002     GdipGetImageWidth(image, &width);
1003     GdipGetImageHeight(image, &height);
1004
1005     srcw = width * (((REAL) INCH_HIMETRIC) /
1006             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1007     srch = height * (((REAL) INCH_HIMETRIC) /
1008             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1009
1010     if(image->type != ImageTypeMetafile){
1011         y += height;
1012         height *= -1;
1013     }
1014
1015     IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1016                     0, 0, srcw, srch, NULL);
1017
1018     return Ok;
1019 }
1020
1021 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1022 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1023      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1024      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1025      DrawImageAbort callback, VOID * callbackData)
1026 {
1027     GpPointF ptf[3];
1028     POINT pti[3];
1029     REAL dx, dy;
1030
1031     TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1032           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1033           callbackData);
1034
1035     if(!graphics || !image || !points || !imageAttributes || count != 3)
1036          return InvalidParameter;
1037
1038     if(srcUnit == UnitInch)
1039         dx = dy = (REAL) INCH_HIMETRIC;
1040     else if(srcUnit == UnitPixel){
1041         dx = ((REAL) INCH_HIMETRIC) /
1042              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1043         dy = ((REAL) INCH_HIMETRIC) /
1044              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1045     }
1046     else
1047         return NotImplemented;
1048
1049     memcpy(ptf, points, 3 * sizeof(GpPointF));
1050     transform_and_round_points(graphics, pti, ptf, 3);
1051
1052     /* IPicture renders bitmaps with the y-axis reversed
1053      * FIXME: flipping for unknown image type might not be correct. */
1054     if(image->type != ImageTypeMetafile){
1055         INT temp;
1056         temp = pti[0].y;
1057         pti[0].y = pti[2].y;
1058         pti[2].y = temp;
1059     }
1060
1061     if(IPicture_Render(image->picture, graphics->hdc,
1062         pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1063         srcx * dx, srcy * dy,
1064         srcwidth * dx, srcheight * dy,
1065         NULL) != S_OK){
1066         if(callback)
1067             callback(callbackData);
1068         return GenericError;
1069     }
1070
1071     return Ok;
1072 }
1073
1074 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1075     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1076     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1077     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1078     VOID * callbackData)
1079 {
1080     GpPointF points[3];
1081
1082     points[0].X = dstx;
1083     points[0].Y = dsty;
1084     points[1].X = dstx + dstwidth;
1085     points[1].Y = dsty;
1086     points[2].X = dstx;
1087     points[2].Y = dsty + dstheight;
1088
1089     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1090                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1091 }
1092
1093 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1094     REAL y1, REAL x2, REAL y2)
1095 {
1096     INT save_state;
1097     GpPointF pt[2];
1098     GpStatus retval;
1099
1100     if(!pen || !graphics)
1101         return InvalidParameter;
1102
1103     pt[0].X = x1;
1104     pt[0].Y = y1;
1105     pt[1].X = x2;
1106     pt[1].Y = y2;
1107
1108     save_state = prepare_dc(graphics, pen);
1109
1110     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1111
1112     restore_dc(graphics, save_state);
1113
1114     return retval;
1115 }
1116
1117 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1118     INT y1, INT x2, INT y2)
1119 {
1120     INT save_state;
1121     GpPointF pt[2];
1122     GpStatus retval;
1123
1124     if(!pen || !graphics)
1125         return InvalidParameter;
1126
1127     pt[0].X = (REAL)x1;
1128     pt[0].Y = (REAL)y1;
1129     pt[1].X = (REAL)x2;
1130     pt[1].Y = (REAL)y2;
1131
1132     save_state = prepare_dc(graphics, pen);
1133
1134     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1135
1136     restore_dc(graphics, save_state);
1137
1138     return retval;
1139 }
1140
1141 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1142     GpPointF *points, INT count)
1143 {
1144     INT save_state;
1145     GpStatus retval;
1146
1147     if(!pen || !graphics || (count < 2))
1148         return InvalidParameter;
1149
1150     save_state = prepare_dc(graphics, pen);
1151
1152     retval = draw_polyline(graphics, pen, points, count, TRUE);
1153
1154     restore_dc(graphics, save_state);
1155
1156     return retval;
1157 }
1158
1159 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1160 {
1161     INT save_state;
1162     GpStatus retval;
1163
1164     if(!pen || !graphics)
1165         return InvalidParameter;
1166
1167     save_state = prepare_dc(graphics, pen);
1168
1169     retval = draw_poly(graphics, pen, path->pathdata.Points,
1170                        path->pathdata.Types, path->pathdata.Count, TRUE);
1171
1172     restore_dc(graphics, save_state);
1173
1174     return retval;
1175 }
1176
1177 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1178     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1179 {
1180     INT save_state;
1181
1182     if(!graphics || !pen)
1183         return InvalidParameter;
1184
1185     save_state = prepare_dc(graphics, pen);
1186     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1187
1188     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1189
1190     restore_dc(graphics, save_state);
1191
1192     return Ok;
1193 }
1194
1195 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1196     INT y, INT width, INT height)
1197 {
1198     INT save_state;
1199     GpPointF ptf[4];
1200     POINT pti[4];
1201
1202     if(!pen || !graphics)
1203         return InvalidParameter;
1204
1205     ptf[0].X = x;
1206     ptf[0].Y = y;
1207     ptf[1].X = x + width;
1208     ptf[1].Y = y;
1209     ptf[2].X = x + width;
1210     ptf[2].Y = y + height;
1211     ptf[3].X = x;
1212     ptf[3].Y = y + height;
1213
1214     save_state = prepare_dc(graphics, pen);
1215     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1216
1217     transform_and_round_points(graphics, pti, ptf, 4);
1218     Polygon(graphics->hdc, pti, 4);
1219
1220     restore_dc(graphics, save_state);
1221
1222     return Ok;
1223 }
1224
1225 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1226 {
1227     INT save_state;
1228     GpStatus retval;
1229
1230     if(!brush || !graphics || !path)
1231         return InvalidParameter;
1232
1233     save_state = SaveDC(graphics->hdc);
1234     EndPath(graphics->hdc);
1235     SelectObject(graphics->hdc, brush->gdibrush);
1236     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1237                                                                     : WINDING));
1238
1239     BeginPath(graphics->hdc);
1240     retval = draw_poly(graphics, NULL, path->pathdata.Points,
1241                        path->pathdata.Types, path->pathdata.Count, FALSE);
1242
1243     if(retval != Ok)
1244         goto end;
1245
1246     EndPath(graphics->hdc);
1247     FillPath(graphics->hdc);
1248
1249     retval = Ok;
1250
1251 end:
1252     RestoreDC(graphics->hdc, save_state);
1253
1254     return retval;
1255 }
1256
1257 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1258     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1259 {
1260     INT save_state;
1261
1262     if(!graphics || !brush)
1263         return InvalidParameter;
1264
1265     save_state = SaveDC(graphics->hdc);
1266     EndPath(graphics->hdc);
1267     SelectObject(graphics->hdc, brush->gdibrush);
1268     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1269
1270     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1271
1272     RestoreDC(graphics->hdc, save_state);
1273
1274     return Ok;
1275 }
1276
1277 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1278     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1279 {
1280     INT save_state;
1281     GpPointF *ptf = NULL;
1282     POINT *pti = NULL;
1283     GpStatus retval = Ok;
1284
1285     if(!graphics || !brush || !points || !count)
1286         return InvalidParameter;
1287
1288     ptf = GdipAlloc(count * sizeof(GpPointF));
1289     pti = GdipAlloc(count * sizeof(POINT));
1290     if(!ptf || !pti){
1291         retval = OutOfMemory;
1292         goto end;
1293     }
1294
1295     memcpy(ptf, points, count * sizeof(GpPointF));
1296
1297     save_state = SaveDC(graphics->hdc);
1298     EndPath(graphics->hdc);
1299     SelectObject(graphics->hdc, brush->gdibrush);
1300     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1301     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1302                                                                   : WINDING));
1303
1304     transform_and_round_points(graphics, pti, ptf, count);
1305     Polygon(graphics->hdc, pti, count);
1306
1307     RestoreDC(graphics->hdc, save_state);
1308
1309 end:
1310     GdipFree(ptf);
1311     GdipFree(pti);
1312
1313     return retval;
1314 }
1315
1316 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1317     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1318 {
1319     INT save_state, i;
1320     GpPointF *ptf = NULL;
1321     POINT *pti = NULL;
1322     GpStatus retval = Ok;
1323
1324     if(!graphics || !brush || !points || !count)
1325         return InvalidParameter;
1326
1327     ptf = GdipAlloc(count * sizeof(GpPointF));
1328     pti = GdipAlloc(count * sizeof(POINT));
1329     if(!ptf || !pti){
1330         retval = OutOfMemory;
1331         goto end;
1332     }
1333
1334     for(i = 0; i < count; i ++){
1335         ptf[i].X = (REAL) points[i].X;
1336         ptf[i].Y = (REAL) points[i].Y;
1337     }
1338
1339     save_state = SaveDC(graphics->hdc);
1340     EndPath(graphics->hdc);
1341     SelectObject(graphics->hdc, brush->gdibrush);
1342     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1343     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1344                                                                   : WINDING));
1345
1346     transform_and_round_points(graphics, pti, ptf, count);
1347     Polygon(graphics->hdc, pti, count);
1348
1349     RestoreDC(graphics->hdc, save_state);
1350
1351 end:
1352     GdipFree(ptf);
1353     GdipFree(pti);
1354
1355     return retval;
1356 }
1357
1358 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1359     REAL x, REAL y, REAL width, REAL height)
1360 {
1361     INT save_state;
1362     GpPointF ptf[4];
1363     POINT pti[4];
1364
1365     if(!graphics || !brush)
1366         return InvalidParameter;
1367
1368     ptf[0].X = x;
1369     ptf[0].Y = y;
1370     ptf[1].X = x + width;
1371     ptf[1].Y = y;
1372     ptf[2].X = x + width;
1373     ptf[2].Y = y + height;
1374     ptf[3].X = x;
1375     ptf[3].Y = y + height;
1376
1377     save_state = SaveDC(graphics->hdc);
1378     EndPath(graphics->hdc);
1379     SelectObject(graphics->hdc, brush->gdibrush);
1380     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1381
1382     transform_and_round_points(graphics, pti, ptf, 4);
1383
1384     Polygon(graphics->hdc, pti, 4);
1385
1386     RestoreDC(graphics->hdc, save_state);
1387
1388     return Ok;
1389 }
1390
1391 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1392     INT x, INT y, INT width, INT height)
1393 {
1394     INT save_state;
1395     GpPointF ptf[4];
1396     POINT pti[4];
1397
1398     if(!graphics || !brush)
1399         return InvalidParameter;
1400
1401     ptf[0].X = x;
1402     ptf[0].Y = y;
1403     ptf[1].X = x + width;
1404     ptf[1].Y = y;
1405     ptf[2].X = x + width;
1406     ptf[2].Y = y + height;
1407     ptf[3].X = x;
1408     ptf[3].Y = y + height;
1409
1410     save_state = SaveDC(graphics->hdc);
1411     EndPath(graphics->hdc);
1412     SelectObject(graphics->hdc, brush->gdibrush);
1413     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1414
1415     transform_and_round_points(graphics, pti, ptf, 4);
1416
1417     Polygon(graphics->hdc, pti, 4);
1418
1419     RestoreDC(graphics->hdc, save_state);
1420
1421     return Ok;
1422 }
1423
1424 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1425 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1426     CompositingQuality *quality)
1427 {
1428     if(!graphics || !quality)
1429         return InvalidParameter;
1430
1431     *quality = graphics->compqual;
1432
1433     return Ok;
1434 }
1435
1436 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1437 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1438     InterpolationMode *mode)
1439 {
1440     if(!graphics || !mode)
1441         return InvalidParameter;
1442
1443     *mode = graphics->interpolation;
1444
1445     return Ok;
1446 }
1447
1448 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1449 {
1450     if(!graphics || !scale)
1451         return InvalidParameter;
1452
1453     *scale = graphics->scale;
1454
1455     return Ok;
1456 }
1457
1458 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1459 {
1460     if(!graphics || !unit)
1461         return InvalidParameter;
1462
1463     *unit = graphics->unit;
1464
1465     return Ok;
1466 }
1467
1468 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1469 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1470     *mode)
1471 {
1472     if(!graphics || !mode)
1473         return InvalidParameter;
1474
1475     *mode = graphics->pixeloffset;
1476
1477     return Ok;
1478 }
1479
1480 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1481 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1482 {
1483     if(!graphics || !mode)
1484         return InvalidParameter;
1485
1486     *mode = graphics->smoothing;
1487
1488     return Ok;
1489 }
1490
1491 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1492 {
1493     if(!graphics || !matrix)
1494         return InvalidParameter;
1495
1496     memcpy(matrix, graphics->worldtrans, sizeof(GpMatrix));
1497     return Ok;
1498 }
1499
1500 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1501 {
1502     static int calls;
1503
1504     if(!graphics)
1505         return InvalidParameter;
1506
1507     if(!(calls++))
1508         FIXME("graphics state not implemented\n");
1509
1510     return NotImplemented;
1511 }
1512
1513 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1514 {
1515     static int calls;
1516
1517     if(!graphics || !state)
1518         return InvalidParameter;
1519
1520     if(!(calls++))
1521         FIXME("graphics state not implemented\n");
1522
1523     return NotImplemented;
1524 }
1525
1526 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1527     CompositingQuality quality)
1528 {
1529     if(!graphics)
1530         return InvalidParameter;
1531
1532     graphics->compqual = quality;
1533
1534     return Ok;
1535 }
1536
1537 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1538     InterpolationMode mode)
1539 {
1540     if(!graphics)
1541         return InvalidParameter;
1542
1543     graphics->interpolation = mode;
1544
1545     return Ok;
1546 }
1547
1548 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
1549 {
1550     if(!graphics || (scale <= 0.0))
1551         return InvalidParameter;
1552
1553     graphics->scale = scale;
1554
1555     return Ok;
1556 }
1557
1558 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
1559 {
1560     if(!graphics || (unit == UnitWorld))
1561         return InvalidParameter;
1562
1563     graphics->unit = unit;
1564
1565     return Ok;
1566 }
1567
1568 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1569     mode)
1570 {
1571     if(!graphics)
1572         return InvalidParameter;
1573
1574     graphics->pixeloffset = mode;
1575
1576     return Ok;
1577 }
1578
1579 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1580 {
1581     if(!graphics)
1582         return InvalidParameter;
1583
1584     graphics->smoothing = mode;
1585
1586     return Ok;
1587 }
1588
1589 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1590 {
1591     if(!graphics || !matrix)
1592         return InvalidParameter;
1593
1594     GdipDeleteMatrix(graphics->worldtrans);
1595     return GdipCloneMatrix(matrix, &graphics->worldtrans);
1596 }