gdiplus/tests: Added GdipAddPathLineI test.
[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
42 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
43
44 /* looks-right constants */
45 #define TENSION_CONST (0.3)
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 /* GdipDrawPie/GdipFillPie helper function */
177 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
178     REAL height, REAL startAngle, REAL sweepAngle)
179 {
180     GpPointF ptf[4];
181     POINT pti[4];
182
183     ptf[0].X = x;
184     ptf[0].Y = y;
185     ptf[1].X = x + width;
186     ptf[1].Y = y + height;
187
188     deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
189     deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
190
191     transform_and_round_points(graphics, pti, ptf, 4);
192
193     Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
194         pti[2].y, pti[3].x, pti[3].y);
195 }
196
197 /* GdipDrawCurve helper function.
198  * Calculates Bezier points from cardinal spline points. */
199 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
200     REAL *y1, REAL *x2, REAL *y2)
201 {
202     REAL xdiff, ydiff;
203
204     /* calculate tangent */
205     xdiff = pts[2].X - pts[0].X;
206     ydiff = pts[2].Y - pts[0].Y;
207
208     /* apply tangent to get control points */
209     *x1 = pts[1].X - tension * xdiff;
210     *y1 = pts[1].Y - tension * ydiff;
211     *x2 = pts[1].X + tension * xdiff;
212     *y2 = pts[1].Y + tension * ydiff;
213 }
214
215 /* GdipDrawCurve helper function.
216  * Calculates Bezier points from cardinal spline endpoints. */
217 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
218     REAL tension, REAL *x, REAL *y)
219 {
220     /* tangent at endpoints is the line from the endpoint to the adjacent point */
221     *x = roundr(tension * (xadj - xend) + xend);
222     *y = roundr(tension * (yadj - yend) + yend);
223 }
224
225 /* Draws the linecap the specified color and size on the hdc.  The linecap is in
226  * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
227  * should not be called on an hdc that has a path you care about. */
228 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
229     const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
230 {
231     HGDIOBJ oldbrush = NULL, oldpen = NULL;
232     GpMatrix *matrix = NULL;
233     HBRUSH brush = NULL;
234     HPEN pen = NULL;
235     PointF ptf[4], *custptf = NULL;
236     POINT pt[4], *custpt = NULL;
237     BYTE *tp = NULL;
238     REAL theta, dsmall, dbig, dx, dy = 0.0;
239     INT i, count;
240     LOGBRUSH lb;
241     BOOL customstroke;
242
243     if((x1 == x2) && (y1 == y2))
244         return;
245
246     theta = gdiplus_atan2(y2 - y1, x2 - x1);
247
248     customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
249     if(!customstroke){
250         brush = CreateSolidBrush(color);
251         lb.lbStyle = BS_SOLID;
252         lb.lbColor = color;
253         lb.lbHatch = 0;
254         pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
255                            PS_JOIN_MITER, 1, &lb, 0,
256                            NULL);
257         oldbrush = SelectObject(graphics->hdc, brush);
258         oldpen = SelectObject(graphics->hdc, pen);
259     }
260
261     switch(cap){
262         case LineCapFlat:
263             break;
264         case LineCapSquare:
265         case LineCapSquareAnchor:
266         case LineCapDiamondAnchor:
267             size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
268             if(cap == LineCapDiamondAnchor){
269                 dsmall = cos(theta + M_PI_2) * size;
270                 dbig = sin(theta + M_PI_2) * size;
271             }
272             else{
273                 dsmall = cos(theta + M_PI_4) * size;
274                 dbig = sin(theta + M_PI_4) * size;
275             }
276
277             ptf[0].X = x2 - dsmall;
278             ptf[1].X = x2 + dbig;
279
280             ptf[0].Y = y2 - dbig;
281             ptf[3].Y = y2 + dsmall;
282
283             ptf[1].Y = y2 - dsmall;
284             ptf[2].Y = y2 + dbig;
285
286             ptf[3].X = x2 - dbig;
287             ptf[2].X = x2 + dsmall;
288
289             transform_and_round_points(graphics, pt, ptf, 4);
290             Polygon(graphics->hdc, pt, 4);
291
292             break;
293         case LineCapArrowAnchor:
294             size = size * 4.0 / sqrt(3.0);
295
296             dx = cos(M_PI / 6.0 + theta) * size;
297             dy = sin(M_PI / 6.0 + theta) * size;
298
299             ptf[0].X = x2 - dx;
300             ptf[0].Y = y2 - dy;
301
302             dx = cos(- M_PI / 6.0 + theta) * size;
303             dy = sin(- M_PI / 6.0 + theta) * size;
304
305             ptf[1].X = x2 - dx;
306             ptf[1].Y = y2 - dy;
307
308             ptf[2].X = x2;
309             ptf[2].Y = y2;
310
311             transform_and_round_points(graphics, pt, ptf, 3);
312             Polygon(graphics->hdc, pt, 3);
313
314             break;
315         case LineCapRoundAnchor:
316             dx = dy = ANCHOR_WIDTH * size / 2.0;
317
318             ptf[0].X = x2 - dx;
319             ptf[0].Y = y2 - dy;
320             ptf[1].X = x2 + dx;
321             ptf[1].Y = y2 + dy;
322
323             transform_and_round_points(graphics, pt, ptf, 2);
324             Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
325
326             break;
327         case LineCapTriangle:
328             size = size / 2.0;
329             dx = cos(M_PI_2 + theta) * size;
330             dy = sin(M_PI_2 + theta) * size;
331
332             ptf[0].X = x2 - dx;
333             ptf[0].Y = y2 - dy;
334             ptf[1].X = x2 + dx;
335             ptf[1].Y = y2 + dy;
336
337             dx = cos(theta) * size;
338             dy = sin(theta) * size;
339
340             ptf[2].X = x2 + dx;
341             ptf[2].Y = y2 + dy;
342
343             transform_and_round_points(graphics, pt, ptf, 3);
344             Polygon(graphics->hdc, pt, 3);
345
346             break;
347         case LineCapRound:
348             dx = dy = size / 2.0;
349
350             ptf[0].X = x2 - dx;
351             ptf[0].Y = y2 - dy;
352             ptf[1].X = x2 + dx;
353             ptf[1].Y = y2 + dy;
354
355             dx = -cos(M_PI_2 + theta) * size;
356             dy = -sin(M_PI_2 + theta) * size;
357
358             ptf[2].X = x2 - dx;
359             ptf[2].Y = y2 - dy;
360             ptf[3].X = x2 + dx;
361             ptf[3].Y = y2 + dy;
362
363             transform_and_round_points(graphics, pt, ptf, 4);
364             Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
365                 pt[2].y, pt[3].x, pt[3].y);
366
367             break;
368         case LineCapCustom:
369             if(!custom)
370                 break;
371
372             count = custom->pathdata.Count;
373             custptf = GdipAlloc(count * sizeof(PointF));
374             custpt = GdipAlloc(count * sizeof(POINT));
375             tp = GdipAlloc(count);
376
377             if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
378                 goto custend;
379
380             memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
381
382             GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
383             GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
384                              MatrixOrderAppend);
385             GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
386             GdipTransformMatrixPoints(matrix, custptf, count);
387
388             transform_and_round_points(graphics, custpt, custptf, count);
389
390             for(i = 0; i < count; i++)
391                 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
392
393             if(custom->fill){
394                 BeginPath(graphics->hdc);
395                 PolyDraw(graphics->hdc, custpt, tp, count);
396                 EndPath(graphics->hdc);
397                 StrokeAndFillPath(graphics->hdc);
398             }
399             else
400                 PolyDraw(graphics->hdc, custpt, tp, count);
401
402 custend:
403             GdipFree(custptf);
404             GdipFree(custpt);
405             GdipFree(tp);
406             GdipDeleteMatrix(matrix);
407             break;
408         default:
409             break;
410     }
411
412     if(!customstroke){
413         SelectObject(graphics->hdc, oldbrush);
414         SelectObject(graphics->hdc, oldpen);
415         DeleteObject(brush);
416         DeleteObject(pen);
417     }
418 }
419
420 /* Shortens the line by the given percent by changing x2, y2.
421  * If percent is > 1.0 then the line will change direction.
422  * If percent is negative it can lengthen the line. */
423 static void shorten_line_percent(REAL x1, REAL  y1, REAL *x2, REAL *y2, REAL percent)
424 {
425     REAL dist, theta, dx, dy;
426
427     if((y1 == *y2) && (x1 == *x2))
428         return;
429
430     dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
431     theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
432     dx = cos(theta) * dist;
433     dy = sin(theta) * dist;
434
435     *x2 = *x2 + dx;
436     *y2 = *y2 + dy;
437 }
438
439 /* Shortens the line by the given amount by changing x2, y2.
440  * If the amount is greater than the distance, the line will become length 0.
441  * If the amount is negative, it can lengthen the line. */
442 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
443 {
444     REAL dx, dy, percent;
445
446     dx = *x2 - x1;
447     dy = *y2 - y1;
448     if(dx == 0 && dy == 0)
449         return;
450
451     percent = amt / sqrt(dx * dx + dy * dy);
452     if(percent >= 1.0){
453         *x2 = x1;
454         *y2 = y1;
455         return;
456     }
457
458     shorten_line_percent(x1, y1, x2, y2, percent);
459 }
460
461 /* Draws lines between the given points, and if caps is true then draws an endcap
462  * at the end of the last line. */
463 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
464     GDIPCONST GpPointF * pt, INT count, BOOL caps)
465 {
466     POINT *pti = NULL;
467     GpPointF *ptcopy = NULL;
468     GpStatus status = GenericError;
469
470     if(!count)
471         return Ok;
472
473     pti = GdipAlloc(count * sizeof(POINT));
474     ptcopy = GdipAlloc(count * sizeof(GpPointF));
475
476     if(!pti || !ptcopy){
477         status = OutOfMemory;
478         goto end;
479     }
480
481     memcpy(ptcopy, pt, count * sizeof(GpPointF));
482
483     if(caps){
484         if(pen->endcap == LineCapArrowAnchor)
485             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
486                              &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
487         else if((pen->endcap == LineCapCustom) && pen->customend)
488             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
489                              &ptcopy[count-1].X, &ptcopy[count-1].Y,
490                              pen->customend->inset * pen->width);
491
492         if(pen->startcap == LineCapArrowAnchor)
493             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
494                              &ptcopy[0].X, &ptcopy[0].Y, pen->width);
495         else if((pen->startcap == LineCapCustom) && pen->customstart)
496             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
497                              &ptcopy[0].X, &ptcopy[0].Y,
498                              pen->customstart->inset * pen->width);
499
500         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
501                  pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
502         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
503                          pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);\
504     }
505
506     transform_and_round_points(graphics, pti, ptcopy, count);
507
508     Polyline(graphics->hdc, pti, count);
509
510 end:
511     GdipFree(pti);
512     GdipFree(ptcopy);
513
514     return status;
515 }
516
517 /* Conducts a linear search to find the bezier points that will back off
518  * the endpoint of the curve by a distance of amt. Linear search works
519  * better than binary in this case because there are multiple solutions,
520  * and binary searches often find a bad one. I don't think this is what
521  * Windows does but short of rendering the bezier without GDI's help it's
522  * the best we can do. If rev then work from the start of the passed points
523  * instead of the end. */
524 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
525 {
526     GpPointF origpt[4];
527     REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
528     INT i, first = 0, second = 1, third = 2, fourth = 3;
529
530     if(rev){
531         first = 3;
532         second = 2;
533         third = 1;
534         fourth = 0;
535     }
536
537     origx = pt[fourth].X;
538     origy = pt[fourth].Y;
539     memcpy(origpt, pt, sizeof(GpPointF) * 4);
540
541     for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
542         /* reset bezier points to original values */
543         memcpy(pt, origpt, sizeof(GpPointF) * 4);
544         /* Perform magic on bezier points. Order is important here.*/
545         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
546         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
547         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
548         shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
549         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
550         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
551
552         dx = pt[fourth].X - origx;
553         dy = pt[fourth].Y - origy;
554
555         diff = sqrt(dx * dx + dy * dy);
556         percent += 0.0005 * amt;
557     }
558 }
559
560 /* Draws bezier curves between given points, and if caps is true then draws an
561  * endcap at the end of the last line. */
562 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
563     GDIPCONST GpPointF * pt, INT count, BOOL caps)
564 {
565     POINT *pti;
566     GpPointF *ptcopy;
567     GpStatus status = GenericError;
568
569     if(!count)
570         return Ok;
571
572     pti = GdipAlloc(count * sizeof(POINT));
573     ptcopy = GdipAlloc(count * sizeof(GpPointF));
574
575     if(!pti || !ptcopy){
576         status = OutOfMemory;
577         goto end;
578     }
579
580     memcpy(ptcopy, pt, count * sizeof(GpPointF));
581
582     if(caps){
583         if(pen->endcap == LineCapArrowAnchor)
584             shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
585         else if((pen->endcap == LineCapCustom) && pen->customend)
586             shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
587                                FALSE);
588
589         if(pen->startcap == LineCapArrowAnchor)
590             shorten_bezier_amt(ptcopy, pen->width, TRUE);
591         else if((pen->startcap == LineCapCustom) && pen->customstart)
592             shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
593
594         /* the direction of the line cap is parallel to the direction at the
595          * end of the bezier (which, if it has been shortened, is not the same
596          * as the direction from pt[count-2] to pt[count-1]) */
597         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
598             pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
599             pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
600             pt[count - 1].X, pt[count - 1].Y);
601
602         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
603             pt[0].X - (ptcopy[0].X - ptcopy[1].X),
604             pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
605     }
606
607     transform_and_round_points(graphics, pti, ptcopy, count);
608
609     PolyBezier(graphics->hdc, pti, count);
610
611     status = Ok;
612
613 end:
614     GdipFree(pti);
615     GdipFree(ptcopy);
616
617     return status;
618 }
619
620 /* Draws a combination of bezier curves and lines between points. */
621 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
622     GDIPCONST BYTE * types, INT count, BOOL caps)
623 {
624     POINT *pti = GdipAlloc(count * sizeof(POINT));
625     BYTE *tp = GdipAlloc(count);
626     GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
627     INT i, j;
628     GpStatus status = GenericError;
629
630     if(!count){
631         status = Ok;
632         goto end;
633     }
634     if(!pti || !tp || !ptcopy){
635         status = OutOfMemory;
636         goto end;
637     }
638
639     for(i = 1; i < count; i++){
640         if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
641             if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
642                 || !(types[i + 1] & PathPointTypeBezier)){
643                 ERR("Bad bezier points\n");
644                 goto end;
645             }
646             i += 2;
647         }
648     }
649
650     memcpy(ptcopy, pt, count * sizeof(GpPointF));
651
652     /* If we are drawing caps, go through the points and adjust them accordingly,
653      * and draw the caps. */
654     if(caps){
655         switch(types[count - 1] & PathPointTypePathTypeMask){
656             case PathPointTypeBezier:
657                 if(pen->endcap == LineCapArrowAnchor)
658                     shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
659                 else if((pen->endcap == LineCapCustom) && pen->customend)
660                     shorten_bezier_amt(&ptcopy[count - 4],
661                                        pen->width * pen->customend->inset, FALSE);
662
663                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
664                     pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
665                     pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
666                     pt[count - 1].X, pt[count - 1].Y);
667
668                 break;
669             case PathPointTypeLine:
670                 if(pen->endcap == LineCapArrowAnchor)
671                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
672                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
673                                      pen->width);
674                 else if((pen->endcap == LineCapCustom) && pen->customend)
675                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
676                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
677                                      pen->customend->inset * pen->width);
678
679                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
680                          pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
681                          pt[count - 1].Y);
682
683                 break;
684             default:
685                 ERR("Bad path last point\n");
686                 goto end;
687         }
688
689         /* Find start of points */
690         for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
691             == PathPointTypeStart); j++);
692
693         switch(types[j] & PathPointTypePathTypeMask){
694             case PathPointTypeBezier:
695                 if(pen->startcap == LineCapArrowAnchor)
696                     shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
697                 else if((pen->startcap == LineCapCustom) && pen->customstart)
698                     shorten_bezier_amt(&ptcopy[j - 1],
699                                        pen->width * pen->customstart->inset, TRUE);
700
701                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
702                     pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
703                     pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
704                     pt[j - 1].X, pt[j - 1].Y);
705
706                 break;
707             case PathPointTypeLine:
708                 if(pen->startcap == LineCapArrowAnchor)
709                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
710                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
711                                      pen->width);
712                 else if((pen->startcap == LineCapCustom) && pen->customstart)
713                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
714                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
715                                      pen->customstart->inset * pen->width);
716
717                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
718                          pt[j].X, pt[j].Y, pt[j - 1].X,
719                          pt[j - 1].Y);
720
721                 break;
722             default:
723                 ERR("Bad path points\n");
724                 goto end;
725         }
726     }
727
728     transform_and_round_points(graphics, pti, ptcopy, count);
729
730     for(i = 0; i < count; i++){
731         tp[i] = convert_path_point_type(types[i]);
732     }
733
734     PolyDraw(graphics->hdc, pti, tp, count);
735
736     status = Ok;
737
738 end:
739     GdipFree(pti);
740     GdipFree(ptcopy);
741     GdipFree(tp);
742
743     return status;
744 }
745
746 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
747 {
748     GpStatus retval;
749
750     if(hdc == NULL)
751         return OutOfMemory;
752
753     if(graphics == NULL)
754         return InvalidParameter;
755
756     *graphics = GdipAlloc(sizeof(GpGraphics));
757     if(!*graphics)  return OutOfMemory;
758
759     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
760         GdipFree(*graphics);
761         return retval;
762     }
763
764     (*graphics)->hdc = hdc;
765     (*graphics)->hwnd = NULL;
766     (*graphics)->smoothing = SmoothingModeDefault;
767     (*graphics)->compqual = CompositingQualityDefault;
768     (*graphics)->interpolation = InterpolationModeDefault;
769     (*graphics)->pixeloffset = PixelOffsetModeDefault;
770     (*graphics)->compmode = CompositingModeSourceOver;
771     (*graphics)->unit = UnitDisplay;
772     (*graphics)->scale = 1.0;
773
774     return Ok;
775 }
776
777 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
778 {
779     GpStatus ret;
780
781     if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
782         return ret;
783
784     (*graphics)->hwnd = hwnd;
785
786     return Ok;
787 }
788
789 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
790     GpMetafile **metafile)
791 {
792     static int calls;
793
794     if(!hemf || !metafile)
795         return InvalidParameter;
796
797     if(!(calls++))
798         FIXME("not implemented\n");
799
800     return NotImplemented;
801 }
802
803 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
804     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
805 {
806     IStream *stream = NULL;
807     UINT read;
808     BYTE* copy;
809     HENHMETAFILE hemf;
810     GpStatus retval = GenericError;
811
812     if(!hwmf || !metafile || !placeable)
813         return InvalidParameter;
814
815     *metafile = NULL;
816     read = GetMetaFileBitsEx(hwmf, 0, NULL);
817     if(!read)
818         return GenericError;
819     copy = GdipAlloc(read);
820     GetMetaFileBitsEx(hwmf, read, copy);
821
822     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
823     GdipFree(copy);
824
825     read = GetEnhMetaFileBits(hemf, 0, NULL);
826     copy = GdipAlloc(read);
827     GetEnhMetaFileBits(hemf, read, copy);
828     DeleteEnhMetaFile(hemf);
829
830     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
831         ERR("could not make stream\n");
832         GdipFree(copy);
833         goto err;
834     }
835
836     *metafile = GdipAlloc(sizeof(GpMetafile));
837     if(!*metafile){
838         retval = OutOfMemory;
839         goto err;
840     }
841
842     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
843         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
844         goto err;
845
846
847     (*metafile)->image.type = ImageTypeMetafile;
848     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
849     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
850     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
851                     - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
852     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
853                    - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
854     (*metafile)->unit = UnitInch;
855
856     if(delete)
857         DeleteMetaFile(hwmf);
858
859     return Ok;
860
861 err:
862     GdipFree(*metafile);
863     IStream_Release(stream);
864     return retval;
865 }
866
867 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
868     UINT access, IStream **stream)
869 {
870     DWORD dwMode;
871     HRESULT ret;
872
873     if(!stream || !filename)
874         return InvalidParameter;
875
876     if(access & GENERIC_WRITE)
877         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
878     else if(access & GENERIC_READ)
879         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
880     else
881         return InvalidParameter;
882
883     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
884
885     return hresult_to_status(ret);
886 }
887
888 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
889 {
890     if(!graphics) return InvalidParameter;
891     if(graphics->hwnd)
892         ReleaseDC(graphics->hwnd, graphics->hdc);
893
894     GdipDeleteMatrix(graphics->worldtrans);
895     HeapFree(GetProcessHeap(), 0, graphics);
896
897     return Ok;
898 }
899
900 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
901     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
902 {
903     INT save_state, num_pts;
904     GpPointF points[MAX_ARC_PTS];
905     GpStatus retval;
906
907     if(!graphics || !pen)
908         return InvalidParameter;
909
910     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
911
912     save_state = prepare_dc(graphics, pen);
913
914     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
915
916     restore_dc(graphics, save_state);
917
918     return retval;
919 }
920
921 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
922     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
923 {
924     INT save_state;
925     GpPointF pt[4];
926     GpStatus retval;
927
928     if(!graphics || !pen)
929         return InvalidParameter;
930
931     pt[0].X = x1;
932     pt[0].Y = y1;
933     pt[1].X = x2;
934     pt[1].Y = y2;
935     pt[2].X = x3;
936     pt[2].Y = y3;
937     pt[3].X = x4;
938     pt[3].Y = y4;
939
940     save_state = prepare_dc(graphics, pen);
941
942     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
943
944     restore_dc(graphics, save_state);
945
946     return retval;
947 }
948
949 /* Approximates cardinal spline with Bezier curves. */
950 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
951     GDIPCONST GpPointF *points, INT count, REAL tension)
952 {
953     /* PolyBezier expects count*3-2 points. */
954     INT i, len_pt = count*3-2, save_state;
955     GpPointF *pt;
956     REAL x1, x2, y1, y2;
957     GpStatus retval;
958
959     if(!graphics || !pen)
960         return InvalidParameter;
961
962     pt = GdipAlloc(len_pt * sizeof(GpPointF));
963     tension = tension * TENSION_CONST;
964
965     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
966         tension, &x1, &y1);
967
968     pt[0].X = points[0].X;
969     pt[0].Y = points[0].Y;
970     pt[1].X = x1;
971     pt[1].Y = y1;
972
973     for(i = 0; i < count-2; i++){
974         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
975
976         pt[3*i+2].X = x1;
977         pt[3*i+2].Y = y1;
978         pt[3*i+3].X = points[i+1].X;
979         pt[3*i+3].Y = points[i+1].Y;
980         pt[3*i+4].X = x2;
981         pt[3*i+4].Y = y2;
982     }
983
984     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
985         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
986
987     pt[len_pt-2].X = x1;
988     pt[len_pt-2].Y = y1;
989     pt[len_pt-1].X = points[count-1].X;
990     pt[len_pt-1].Y = points[count-1].Y;
991
992     save_state = prepare_dc(graphics, pen);
993
994     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
995
996     GdipFree(pt);
997     restore_dc(graphics, save_state);
998
999     return retval;
1000 }
1001
1002 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1003     INT y)
1004 {
1005     UINT width, height, srcw, srch;
1006
1007     if(!graphics || !image)
1008         return InvalidParameter;
1009
1010     GdipGetImageWidth(image, &width);
1011     GdipGetImageHeight(image, &height);
1012
1013     srcw = width * (((REAL) INCH_HIMETRIC) /
1014             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1015     srch = height * (((REAL) INCH_HIMETRIC) /
1016             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1017
1018     if(image->type != ImageTypeMetafile){
1019         y += height;
1020         height *= -1;
1021     }
1022
1023     IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1024                     0, 0, srcw, srch, NULL);
1025
1026     return Ok;
1027 }
1028
1029 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1030 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1031      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1032      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1033      DrawImageAbort callback, VOID * callbackData)
1034 {
1035     GpPointF ptf[3];
1036     POINT pti[3];
1037     REAL dx, dy;
1038
1039     TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1040           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1041           callbackData);
1042
1043     if(!graphics || !image || !points || !imageAttributes || count != 3)
1044          return InvalidParameter;
1045
1046     if(srcUnit == UnitInch)
1047         dx = dy = (REAL) INCH_HIMETRIC;
1048     else if(srcUnit == UnitPixel){
1049         dx = ((REAL) INCH_HIMETRIC) /
1050              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1051         dy = ((REAL) INCH_HIMETRIC) /
1052              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1053     }
1054     else
1055         return NotImplemented;
1056
1057     memcpy(ptf, points, 3 * sizeof(GpPointF));
1058     transform_and_round_points(graphics, pti, ptf, 3);
1059
1060     /* IPicture renders bitmaps with the y-axis reversed
1061      * FIXME: flipping for unknown image type might not be correct. */
1062     if(image->type != ImageTypeMetafile){
1063         INT temp;
1064         temp = pti[0].y;
1065         pti[0].y = pti[2].y;
1066         pti[2].y = temp;
1067     }
1068
1069     if(IPicture_Render(image->picture, graphics->hdc,
1070         pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1071         srcx * dx, srcy * dy,
1072         srcwidth * dx, srcheight * dy,
1073         NULL) != S_OK){
1074         if(callback)
1075             callback(callbackData);
1076         return GenericError;
1077     }
1078
1079     return Ok;
1080 }
1081
1082 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1083     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1084     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1085     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1086     VOID * callbackData)
1087 {
1088     GpPointF points[3];
1089
1090     points[0].X = dstx;
1091     points[0].Y = dsty;
1092     points[1].X = dstx + dstwidth;
1093     points[1].Y = dsty;
1094     points[2].X = dstx;
1095     points[2].Y = dsty + dstheight;
1096
1097     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1098                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1099 }
1100
1101 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1102     REAL y1, REAL x2, REAL y2)
1103 {
1104     INT save_state;
1105     GpPointF pt[2];
1106     GpStatus retval;
1107
1108     if(!pen || !graphics)
1109         return InvalidParameter;
1110
1111     pt[0].X = x1;
1112     pt[0].Y = y1;
1113     pt[1].X = x2;
1114     pt[1].Y = y2;
1115
1116     save_state = prepare_dc(graphics, pen);
1117
1118     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1119
1120     restore_dc(graphics, save_state);
1121
1122     return retval;
1123 }
1124
1125 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1126     INT y1, INT x2, INT y2)
1127 {
1128     INT save_state;
1129     GpPointF pt[2];
1130     GpStatus retval;
1131
1132     if(!pen || !graphics)
1133         return InvalidParameter;
1134
1135     pt[0].X = (REAL)x1;
1136     pt[0].Y = (REAL)y1;
1137     pt[1].X = (REAL)x2;
1138     pt[1].Y = (REAL)y2;
1139
1140     save_state = prepare_dc(graphics, pen);
1141
1142     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1143
1144     restore_dc(graphics, save_state);
1145
1146     return retval;
1147 }
1148
1149 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1150     GpPointF *points, INT count)
1151 {
1152     INT save_state;
1153     GpStatus retval;
1154
1155     if(!pen || !graphics || (count < 2))
1156         return InvalidParameter;
1157
1158     save_state = prepare_dc(graphics, pen);
1159
1160     retval = draw_polyline(graphics, pen, points, count, TRUE);
1161
1162     restore_dc(graphics, save_state);
1163
1164     return retval;
1165 }
1166
1167 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1168 {
1169     INT save_state;
1170     GpStatus retval;
1171
1172     if(!pen || !graphics)
1173         return InvalidParameter;
1174
1175     save_state = prepare_dc(graphics, pen);
1176
1177     retval = draw_poly(graphics, pen, path->pathdata.Points,
1178                        path->pathdata.Types, path->pathdata.Count, TRUE);
1179
1180     restore_dc(graphics, save_state);
1181
1182     return retval;
1183 }
1184
1185 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1186     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1187 {
1188     INT save_state;
1189
1190     if(!graphics || !pen)
1191         return InvalidParameter;
1192
1193     save_state = prepare_dc(graphics, pen);
1194     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1195
1196     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1197
1198     restore_dc(graphics, save_state);
1199
1200     return Ok;
1201 }
1202
1203 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1204     INT y, INT width, INT height)
1205 {
1206     INT save_state;
1207     GpPointF ptf[4];
1208     POINT pti[4];
1209
1210     if(!pen || !graphics)
1211         return InvalidParameter;
1212
1213     ptf[0].X = x;
1214     ptf[0].Y = y;
1215     ptf[1].X = x + width;
1216     ptf[1].Y = y;
1217     ptf[2].X = x + width;
1218     ptf[2].Y = y + height;
1219     ptf[3].X = x;
1220     ptf[3].Y = y + height;
1221
1222     save_state = prepare_dc(graphics, pen);
1223     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1224
1225     transform_and_round_points(graphics, pti, ptf, 4);
1226     Polygon(graphics->hdc, pti, 4);
1227
1228     restore_dc(graphics, save_state);
1229
1230     return Ok;
1231 }
1232
1233 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1234     GpRectF* rects, INT count)
1235 {
1236     GpPointF *ptf;
1237     POINT *pti;
1238     INT save_state, i;
1239
1240     if(!graphics || !pen || !rects || count < 1)
1241         return InvalidParameter;
1242
1243     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1244     pti = GdipAlloc(4 * count * sizeof(POINT));
1245
1246     if(!ptf || !pti){
1247         GdipFree(ptf);
1248         GdipFree(pti);
1249         return OutOfMemory;
1250     }
1251
1252     for(i = 0; i < count; i++){
1253         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1254         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1255         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1256         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1257     }
1258
1259     save_state = prepare_dc(graphics, pen);
1260     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1261
1262     transform_and_round_points(graphics, pti, ptf, 4 * count);
1263
1264     for(i = 0; i < count; i++)
1265         Polygon(graphics->hdc, &pti[4 * i], 4);
1266
1267     restore_dc(graphics, save_state);
1268
1269     GdipFree(ptf);
1270     GdipFree(pti);
1271
1272     return Ok;
1273 }
1274
1275 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1276     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1277     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1278 {
1279     HRGN rgn = NULL;
1280     HFONT gdifont;
1281     LOGFONTW lfw;
1282     TEXTMETRICW textmet;
1283     GpPointF pt[2], rectcpy[4];
1284     POINT corners[4];
1285     WCHAR* stringdup;
1286     REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1287     INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
1288         nheight;
1289     SIZE size;
1290     RECT drawcoord;
1291
1292     if(!graphics || !string || !font || !brush || !rect)
1293         return InvalidParameter;
1294
1295     if((brush->bt != BrushTypeSolidColor)){
1296         FIXME("not implemented for given parameters\n");
1297         return NotImplemented;
1298     }
1299
1300     if(format)
1301         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1302
1303     if(length == -1) length = lstrlenW(string);
1304
1305     stringdup = GdipAlloc(length * sizeof(WCHAR));
1306     if(!stringdup) return OutOfMemory;
1307
1308     save_state = SaveDC(graphics->hdc);
1309     SetBkMode(graphics->hdc, TRANSPARENT);
1310     SetTextColor(graphics->hdc, brush->lb.lbColor);
1311
1312     rectcpy[3].X = rectcpy[0].X = rect->X;
1313     rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1314     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1315     rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1316     transform_and_round_points(graphics, corners, rectcpy, 4);
1317
1318     if(roundr(rect->Width) == 0 && roundr(rect->Height) == 0){
1319         rel_width = rel_height = 1.0;
1320         nwidth = nheight = INT_MAX;
1321     }
1322     else{
1323         rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1324                          (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1325                          / rect->Width;
1326         rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1327                           (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1328                           / rect->Height;
1329
1330         nwidth = roundr(rel_width * rect->Width);
1331         nheight = roundr(rel_height * rect->Height);
1332         rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1333         SelectClipRgn(graphics->hdc, rgn);
1334     }
1335
1336     /* Use gdi to find the font, then perform transformations on it (height,
1337      * width, angle). */
1338     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1339     GetTextMetricsW(graphics->hdc, &textmet);
1340     memcpy(&lfw, &font->lfw, sizeof(LOGFONTW));
1341
1342     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1343     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1344
1345     pt[0].X = 0.0;
1346     pt[0].Y = 0.0;
1347     pt[1].X = 1.0;
1348     pt[1].Y = 0.0;
1349     GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1350     angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1351     ang_cos = cos(angle);
1352     ang_sin = sin(angle);
1353     lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1354
1355     gdifont = CreateFontIndirectW(&lfw);
1356     DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1357
1358     for(i = 0, j = 0; i < length; i++){
1359         if(!isprintW(string[i]) && (string[i] != '\n'))
1360             continue;
1361
1362         stringdup[j] = string[i];
1363         j++;
1364     }
1365
1366     stringdup[j] = 0;
1367     length = j;
1368
1369     while(sum < length){
1370         drawcoord.left = corners[0].x + roundr(ang_sin * (REAL) height);
1371         drawcoord.top = corners[0].y + roundr(ang_cos * (REAL) height);
1372
1373         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1374                               nwidth, &fit, NULL, &size);
1375         fitcpy = fit;
1376
1377         if(fit == 0){
1378             DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, DT_NOCLIP |
1379                       DT_EXPANDTABS);
1380             break;
1381         }
1382
1383         for(lret = 0; lret < fit; lret++)
1384             if(*(stringdup + sum + lret) == '\n')
1385                 break;
1386
1387         /* Line break code (may look strange, but it imitates windows). */
1388         if(lret < fit)
1389             fit = lret;    /* this is not an off-by-one error */
1390         else if(fit < (length - sum)){
1391             if(*(stringdup + sum + fit) == ' ')
1392                 while(*(stringdup + sum + fit) == ' ')
1393                     fit++;
1394             else
1395                 while(*(stringdup + sum + fit - 1) != ' '){
1396                     fit--;
1397
1398                     if(*(stringdup + sum + fit) == '\t')
1399                         break;
1400
1401                     if(fit == 0){
1402                         fit = fitcpy;
1403                         break;
1404                     }
1405                 }
1406         }
1407         DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
1408                   &drawcoord, DT_NOCLIP | DT_EXPANDTABS);
1409
1410         sum += fit + (lret < fitcpy ? 1 : 0);
1411         height += size.cy;
1412
1413         if(height > nheight)
1414             break;
1415
1416         /* Stop if this was a linewrap (but not if it was a linebreak). */
1417         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1418             break;
1419     }
1420
1421     DeleteObject(rgn);
1422     DeleteObject(gdifont);
1423
1424     RestoreDC(graphics->hdc, save_state);
1425
1426     return Ok;
1427 }
1428
1429 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1430 {
1431     INT save_state;
1432     GpStatus retval;
1433
1434     if(!brush || !graphics || !path)
1435         return InvalidParameter;
1436
1437     save_state = SaveDC(graphics->hdc);
1438     EndPath(graphics->hdc);
1439     SelectObject(graphics->hdc, brush->gdibrush);
1440     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1441                                                                     : WINDING));
1442
1443     BeginPath(graphics->hdc);
1444     retval = draw_poly(graphics, NULL, path->pathdata.Points,
1445                        path->pathdata.Types, path->pathdata.Count, FALSE);
1446
1447     if(retval != Ok)
1448         goto end;
1449
1450     EndPath(graphics->hdc);
1451     FillPath(graphics->hdc);
1452
1453     retval = Ok;
1454
1455 end:
1456     RestoreDC(graphics->hdc, save_state);
1457
1458     return retval;
1459 }
1460
1461 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1462     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1463 {
1464     INT save_state;
1465
1466     if(!graphics || !brush)
1467         return InvalidParameter;
1468
1469     save_state = SaveDC(graphics->hdc);
1470     EndPath(graphics->hdc);
1471     SelectObject(graphics->hdc, brush->gdibrush);
1472     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1473
1474     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1475
1476     RestoreDC(graphics->hdc, save_state);
1477
1478     return Ok;
1479 }
1480
1481 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1482     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1483 {
1484     INT save_state;
1485     GpPointF *ptf = NULL;
1486     POINT *pti = NULL;
1487     GpStatus retval = Ok;
1488
1489     if(!graphics || !brush || !points || !count)
1490         return InvalidParameter;
1491
1492     ptf = GdipAlloc(count * sizeof(GpPointF));
1493     pti = GdipAlloc(count * sizeof(POINT));
1494     if(!ptf || !pti){
1495         retval = OutOfMemory;
1496         goto end;
1497     }
1498
1499     memcpy(ptf, points, count * sizeof(GpPointF));
1500
1501     save_state = SaveDC(graphics->hdc);
1502     EndPath(graphics->hdc);
1503     SelectObject(graphics->hdc, brush->gdibrush);
1504     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1505     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1506                                                                   : WINDING));
1507
1508     transform_and_round_points(graphics, pti, ptf, count);
1509     Polygon(graphics->hdc, pti, count);
1510
1511     RestoreDC(graphics->hdc, save_state);
1512
1513 end:
1514     GdipFree(ptf);
1515     GdipFree(pti);
1516
1517     return retval;
1518 }
1519
1520 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1521     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1522 {
1523     INT save_state, i;
1524     GpPointF *ptf = NULL;
1525     POINT *pti = NULL;
1526     GpStatus retval = Ok;
1527
1528     if(!graphics || !brush || !points || !count)
1529         return InvalidParameter;
1530
1531     ptf = GdipAlloc(count * sizeof(GpPointF));
1532     pti = GdipAlloc(count * sizeof(POINT));
1533     if(!ptf || !pti){
1534         retval = OutOfMemory;
1535         goto end;
1536     }
1537
1538     for(i = 0; i < count; i ++){
1539         ptf[i].X = (REAL) points[i].X;
1540         ptf[i].Y = (REAL) points[i].Y;
1541     }
1542
1543     save_state = SaveDC(graphics->hdc);
1544     EndPath(graphics->hdc);
1545     SelectObject(graphics->hdc, brush->gdibrush);
1546     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1547     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1548                                                                   : WINDING));
1549
1550     transform_and_round_points(graphics, pti, ptf, count);
1551     Polygon(graphics->hdc, pti, count);
1552
1553     RestoreDC(graphics->hdc, save_state);
1554
1555 end:
1556     GdipFree(ptf);
1557     GdipFree(pti);
1558
1559     return retval;
1560 }
1561
1562 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1563     REAL x, REAL y, REAL width, REAL height)
1564 {
1565     INT save_state;
1566     GpPointF ptf[4];
1567     POINT pti[4];
1568
1569     if(!graphics || !brush)
1570         return InvalidParameter;
1571
1572     ptf[0].X = x;
1573     ptf[0].Y = y;
1574     ptf[1].X = x + width;
1575     ptf[1].Y = y;
1576     ptf[2].X = x + width;
1577     ptf[2].Y = y + height;
1578     ptf[3].X = x;
1579     ptf[3].Y = y + height;
1580
1581     save_state = SaveDC(graphics->hdc);
1582     EndPath(graphics->hdc);
1583     SelectObject(graphics->hdc, brush->gdibrush);
1584     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1585
1586     transform_and_round_points(graphics, pti, ptf, 4);
1587
1588     Polygon(graphics->hdc, pti, 4);
1589
1590     RestoreDC(graphics->hdc, save_state);
1591
1592     return Ok;
1593 }
1594
1595 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1596     INT x, INT y, INT width, INT height)
1597 {
1598     INT save_state;
1599     GpPointF ptf[4];
1600     POINT pti[4];
1601
1602     if(!graphics || !brush)
1603         return InvalidParameter;
1604
1605     ptf[0].X = x;
1606     ptf[0].Y = y;
1607     ptf[1].X = x + width;
1608     ptf[1].Y = y;
1609     ptf[2].X = x + width;
1610     ptf[2].Y = y + height;
1611     ptf[3].X = x;
1612     ptf[3].Y = y + height;
1613
1614     save_state = SaveDC(graphics->hdc);
1615     EndPath(graphics->hdc);
1616     SelectObject(graphics->hdc, brush->gdibrush);
1617     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1618
1619     transform_and_round_points(graphics, pti, ptf, 4);
1620
1621     Polygon(graphics->hdc, pti, 4);
1622
1623     RestoreDC(graphics->hdc, save_state);
1624
1625     return Ok;
1626 }
1627
1628 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
1629 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
1630     CompositingMode *mode)
1631 {
1632     if(!graphics || !mode)
1633         return InvalidParameter;
1634
1635     *mode = graphics->compmode;
1636
1637     return Ok;
1638 }
1639
1640 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1641 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1642     CompositingQuality *quality)
1643 {
1644     if(!graphics || !quality)
1645         return InvalidParameter;
1646
1647     *quality = graphics->compqual;
1648
1649     return Ok;
1650 }
1651
1652 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1653 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1654     InterpolationMode *mode)
1655 {
1656     if(!graphics || !mode)
1657         return InvalidParameter;
1658
1659     *mode = graphics->interpolation;
1660
1661     return Ok;
1662 }
1663
1664 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1665 {
1666     if(!graphics || !scale)
1667         return InvalidParameter;
1668
1669     *scale = graphics->scale;
1670
1671     return Ok;
1672 }
1673
1674 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1675 {
1676     if(!graphics || !unit)
1677         return InvalidParameter;
1678
1679     *unit = graphics->unit;
1680
1681     return Ok;
1682 }
1683
1684 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1685 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1686     *mode)
1687 {
1688     if(!graphics || !mode)
1689         return InvalidParameter;
1690
1691     *mode = graphics->pixeloffset;
1692
1693     return Ok;
1694 }
1695
1696 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1697 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1698 {
1699     if(!graphics || !mode)
1700         return InvalidParameter;
1701
1702     *mode = graphics->smoothing;
1703
1704     return Ok;
1705 }
1706
1707 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
1708 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
1709     TextRenderingHint *hint)
1710 {
1711     if(!graphics || !hint)
1712         return InvalidParameter;
1713
1714     *hint = graphics->texthint;
1715
1716     return Ok;
1717 }
1718
1719 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1720 {
1721     if(!graphics || !matrix)
1722         return InvalidParameter;
1723
1724     memcpy(matrix, graphics->worldtrans, sizeof(GpMatrix));
1725     return Ok;
1726 }
1727
1728 /* Find the smallest rectangle that bounds the text when it is printed in rect
1729  * according to the format options listed in format. If rect has 0 width and
1730  * height, then just find the smallest rectangle that bounds the text when it's
1731  * printed at location (rect->X, rect-Y). */
1732 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
1733     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
1734     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
1735     INT *codepointsfitted, INT *linesfilled)
1736 {
1737     HFONT oldfont;
1738     WCHAR* stringdup;
1739     INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
1740         nheight;
1741     SIZE size;
1742
1743     if(!graphics || !string || !font || !rect)
1744         return InvalidParameter;
1745
1746     if(codepointsfitted || linesfilled){
1747         FIXME("not implemented for given parameters\n");
1748         return NotImplemented;
1749     }
1750
1751     if(format)
1752         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1753
1754     if(length == -1) length = lstrlenW(string);
1755
1756     stringdup = GdipAlloc(length * sizeof(WCHAR));
1757     if(!stringdup) return OutOfMemory;
1758
1759     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1760     nwidth = roundr(rect->Width);
1761     nheight = roundr(rect->Height);
1762
1763     if((nwidth == 0) && (nheight == 0))
1764         nwidth = nheight = INT_MAX;
1765
1766     for(i = 0, j = 0; i < length; i++){
1767         if(!isprintW(string[i]) && (string[i] != '\n'))
1768             continue;
1769
1770         stringdup[j] = string[i];
1771         j++;
1772     }
1773
1774     stringdup[j] = 0;
1775     length = j;
1776
1777     while(sum < length){
1778         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1779                               nwidth, &fit, NULL, &size);
1780         fitcpy = fit;
1781
1782         if(fit == 0)
1783             break;
1784
1785         for(lret = 0; lret < fit; lret++)
1786             if(*(stringdup + sum + lret) == '\n')
1787                 break;
1788
1789         /* Line break code (may look strange, but it imitates windows). */
1790         if(lret < fit)
1791             fit = lret;    /* this is not an off-by-one error */
1792         else if(fit < (length - sum)){
1793             if(*(stringdup + sum + fit) == ' ')
1794                 while(*(stringdup + sum + fit) == ' ')
1795                     fit++;
1796             else
1797                 while(*(stringdup + sum + fit - 1) != ' '){
1798                     fit--;
1799
1800                     if(*(stringdup + sum + fit) == '\t')
1801                         break;
1802
1803                     if(fit == 0){
1804                         fit = fitcpy;
1805                         break;
1806                     }
1807                 }
1808         }
1809
1810         GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
1811                               nwidth, &j, NULL, &size);
1812
1813         sum += fit + (lret < fitcpy ? 1 : 0);
1814         height += size.cy;
1815         max_width = max(max_width, size.cx);
1816
1817         if(height > nheight)
1818             break;
1819
1820         /* Stop if this was a linewrap (but not if it was a linebreak). */
1821         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1822             break;
1823     }
1824
1825     bounds->X = rect->X;
1826     bounds->Y = rect->Y;
1827     bounds->Width = (REAL)max_width;
1828     bounds->Height = (REAL) min(height, nheight);
1829
1830     DeleteObject(SelectObject(graphics->hdc, oldfont));
1831
1832     return Ok;
1833 }
1834
1835 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1836 {
1837     static int calls;
1838
1839     if(!graphics)
1840         return InvalidParameter;
1841
1842     if(!(calls++))
1843         FIXME("graphics state not implemented\n");
1844
1845     return NotImplemented;
1846 }
1847
1848 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
1849     GpMatrixOrder order)
1850 {
1851     if(!graphics)
1852         return InvalidParameter;
1853
1854     return GdipRotateMatrix(graphics->worldtrans, angle, order);
1855 }
1856
1857 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1858 {
1859     static int calls;
1860
1861     if(!graphics || !state)
1862         return InvalidParameter;
1863
1864     if(!(calls++))
1865         FIXME("graphics state not implemented\n");
1866
1867     return NotImplemented;
1868 }
1869
1870 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
1871     REAL sy, GpMatrixOrder order)
1872 {
1873     if(!graphics)
1874         return InvalidParameter;
1875
1876     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
1877 }
1878
1879 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
1880     CompositingMode mode)
1881 {
1882     if(!graphics)
1883         return InvalidParameter;
1884
1885     graphics->compmode = mode;
1886
1887     return Ok;
1888 }
1889
1890 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1891     CompositingQuality quality)
1892 {
1893     if(!graphics)
1894         return InvalidParameter;
1895
1896     graphics->compqual = quality;
1897
1898     return Ok;
1899 }
1900
1901 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1902     InterpolationMode mode)
1903 {
1904     if(!graphics)
1905         return InvalidParameter;
1906
1907     graphics->interpolation = mode;
1908
1909     return Ok;
1910 }
1911
1912 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
1913 {
1914     if(!graphics || (scale <= 0.0))
1915         return InvalidParameter;
1916
1917     graphics->scale = scale;
1918
1919     return Ok;
1920 }
1921
1922 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
1923 {
1924     if(!graphics || (unit == UnitWorld))
1925         return InvalidParameter;
1926
1927     graphics->unit = unit;
1928
1929     return Ok;
1930 }
1931
1932 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1933     mode)
1934 {
1935     if(!graphics)
1936         return InvalidParameter;
1937
1938     graphics->pixeloffset = mode;
1939
1940     return Ok;
1941 }
1942
1943 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1944 {
1945     if(!graphics)
1946         return InvalidParameter;
1947
1948     graphics->smoothing = mode;
1949
1950     return Ok;
1951 }
1952
1953 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
1954     TextRenderingHint hint)
1955 {
1956     if(!graphics)
1957         return InvalidParameter;
1958
1959     graphics->texthint = hint;
1960
1961     return Ok;
1962 }
1963
1964 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1965 {
1966     if(!graphics || !matrix)
1967         return InvalidParameter;
1968
1969     GdipDeleteMatrix(graphics->worldtrans);
1970     return GdipCloneMatrix(matrix, &graphics->worldtrans);
1971 }
1972
1973 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
1974     REAL dy, GpMatrixOrder order)
1975 {
1976     if(!graphics)
1977         return InvalidParameter;
1978
1979     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
1980 }