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