winhttp, wininet: Load i2d_X509 from libcrypto.so.
[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 ANCHOR_WIDTH (2.0)
46 #define MAX_ITERS (50)
47
48 /* Converts angle (in degrees) to x/y coordinates */
49 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
50 {
51     REAL radAngle, hypotenuse;
52
53     radAngle = deg2rad(angle);
54     hypotenuse = 50.0; /* arbitrary */
55
56     *x = x_0 + cos(radAngle) * hypotenuse;
57     *y = y_0 + sin(radAngle) * hypotenuse;
58 }
59
60 /* Converts from gdiplus path point type to gdi path point type. */
61 static BYTE convert_path_point_type(BYTE type)
62 {
63     BYTE ret;
64
65     switch(type & PathPointTypePathTypeMask){
66         case PathPointTypeBezier:
67             ret = PT_BEZIERTO;
68             break;
69         case PathPointTypeLine:
70             ret = PT_LINETO;
71             break;
72         case PathPointTypeStart:
73             ret = PT_MOVETO;
74             break;
75         default:
76             ERR("Bad point type\n");
77             return 0;
78     }
79
80     if(type & PathPointTypeCloseSubpath)
81         ret |= PT_CLOSEFIGURE;
82
83     return ret;
84 }
85
86 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
87 {
88     HPEN gdipen;
89     REAL width;
90     INT save_state = SaveDC(graphics->hdc), i, numdashes;
91     GpPointF pt[2];
92     DWORD dash_array[MAX_DASHLEN];
93
94     EndPath(graphics->hdc);
95
96     if(pen->unit == UnitPixel){
97         width = pen->width;
98     }
99     else{
100         /* Get an estimate for the amount the pen width is affected by the world
101          * transform. (This is similar to what some of the wine drivers do.) */
102         pt[0].X = 0.0;
103         pt[0].Y = 0.0;
104         pt[1].X = 1.0;
105         pt[1].Y = 1.0;
106         GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
107         width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
108                      (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
109
110         width *= pen->width * convert_unit(graphics->hdc,
111                               pen->unit == UnitWorld ? graphics->unit : pen->unit);
112     }
113
114     if(pen->dash == DashStyleCustom){
115         numdashes = min(pen->numdashes, MAX_DASHLEN);
116
117         TRACE("dashes are: ");
118         for(i = 0; i < numdashes; i++){
119             dash_array[i] = roundr(width * pen->dashes[i]);
120             TRACE("%d, ", dash_array[i]);
121         }
122         TRACE("\n and the pen style is %x\n", pen->style);
123
124         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
125                               numdashes, dash_array);
126     }
127     else
128         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
129
130     SelectObject(graphics->hdc, gdipen);
131
132     return save_state;
133 }
134
135 static void restore_dc(GpGraphics *graphics, INT state)
136 {
137     DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
138     RestoreDC(graphics->hdc, state);
139 }
140
141 /* This helper applies all the changes that the points listed in ptf need in
142  * order to be drawn on the device context.  In the end, this should include at
143  * least:
144  *  -scaling by page unit
145  *  -applying world transformation
146  *  -converting from float to int
147  * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
148  * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
149  * gdi to draw, and these functions would irreparably mess with line widths.
150  */
151 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
152     GpPointF *ptf, INT count)
153 {
154     REAL unitscale;
155     GpMatrix *matrix;
156     int i;
157
158     unitscale = convert_unit(graphics->hdc, graphics->unit);
159
160     /* apply page scale */
161     if(graphics->unit != UnitDisplay)
162         unitscale *= graphics->scale;
163
164     GdipCloneMatrix(graphics->worldtrans, &matrix);
165     GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
166     GdipTransformMatrixPoints(matrix, ptf, count);
167     GdipDeleteMatrix(matrix);
168
169     for(i = 0; i < count; i++){
170         pti[i].x = roundr(ptf[i].X);
171         pti[i].y = roundr(ptf[i].Y);
172     }
173 }
174
175 /* GdipDrawPie/GdipFillPie helper function */
176 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
177     REAL height, REAL startAngle, REAL sweepAngle)
178 {
179     GpPointF ptf[4];
180     POINT pti[4];
181
182     ptf[0].X = x;
183     ptf[0].Y = y;
184     ptf[1].X = x + width;
185     ptf[1].Y = y + height;
186
187     deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
188     deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
189
190     transform_and_round_points(graphics, pti, ptf, 4);
191
192     Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
193         pti[2].y, pti[3].x, pti[3].y);
194 }
195
196 /* Draws the linecap the specified color and size on the hdc.  The linecap is in
197  * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
198  * should not be called on an hdc that has a path you care about. */
199 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
200     const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
201 {
202     HGDIOBJ oldbrush = NULL, oldpen = NULL;
203     GpMatrix *matrix = NULL;
204     HBRUSH brush = NULL;
205     HPEN pen = NULL;
206     PointF ptf[4], *custptf = NULL;
207     POINT pt[4], *custpt = NULL;
208     BYTE *tp = NULL;
209     REAL theta, dsmall, dbig, dx, dy = 0.0;
210     INT i, count;
211     LOGBRUSH lb;
212     BOOL customstroke;
213
214     if((x1 == x2) && (y1 == y2))
215         return;
216
217     theta = gdiplus_atan2(y2 - y1, x2 - x1);
218
219     customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
220     if(!customstroke){
221         brush = CreateSolidBrush(color);
222         lb.lbStyle = BS_SOLID;
223         lb.lbColor = color;
224         lb.lbHatch = 0;
225         pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
226                            PS_JOIN_MITER, 1, &lb, 0,
227                            NULL);
228         oldbrush = SelectObject(graphics->hdc, brush);
229         oldpen = SelectObject(graphics->hdc, pen);
230     }
231
232     switch(cap){
233         case LineCapFlat:
234             break;
235         case LineCapSquare:
236         case LineCapSquareAnchor:
237         case LineCapDiamondAnchor:
238             size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
239             if(cap == LineCapDiamondAnchor){
240                 dsmall = cos(theta + M_PI_2) * size;
241                 dbig = sin(theta + M_PI_2) * size;
242             }
243             else{
244                 dsmall = cos(theta + M_PI_4) * size;
245                 dbig = sin(theta + M_PI_4) * size;
246             }
247
248             ptf[0].X = x2 - dsmall;
249             ptf[1].X = x2 + dbig;
250
251             ptf[0].Y = y2 - dbig;
252             ptf[3].Y = y2 + dsmall;
253
254             ptf[1].Y = y2 - dsmall;
255             ptf[2].Y = y2 + dbig;
256
257             ptf[3].X = x2 - dbig;
258             ptf[2].X = x2 + dsmall;
259
260             transform_and_round_points(graphics, pt, ptf, 4);
261             Polygon(graphics->hdc, pt, 4);
262
263             break;
264         case LineCapArrowAnchor:
265             size = size * 4.0 / sqrt(3.0);
266
267             dx = cos(M_PI / 6.0 + theta) * size;
268             dy = sin(M_PI / 6.0 + theta) * size;
269
270             ptf[0].X = x2 - dx;
271             ptf[0].Y = y2 - dy;
272
273             dx = cos(- M_PI / 6.0 + theta) * size;
274             dy = sin(- M_PI / 6.0 + theta) * size;
275
276             ptf[1].X = x2 - dx;
277             ptf[1].Y = y2 - dy;
278
279             ptf[2].X = x2;
280             ptf[2].Y = y2;
281
282             transform_and_round_points(graphics, pt, ptf, 3);
283             Polygon(graphics->hdc, pt, 3);
284
285             break;
286         case LineCapRoundAnchor:
287             dx = dy = ANCHOR_WIDTH * size / 2.0;
288
289             ptf[0].X = x2 - dx;
290             ptf[0].Y = y2 - dy;
291             ptf[1].X = x2 + dx;
292             ptf[1].Y = y2 + dy;
293
294             transform_and_round_points(graphics, pt, ptf, 2);
295             Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
296
297             break;
298         case LineCapTriangle:
299             size = size / 2.0;
300             dx = cos(M_PI_2 + theta) * size;
301             dy = sin(M_PI_2 + theta) * size;
302
303             ptf[0].X = x2 - dx;
304             ptf[0].Y = y2 - dy;
305             ptf[1].X = x2 + dx;
306             ptf[1].Y = y2 + dy;
307
308             dx = cos(theta) * size;
309             dy = sin(theta) * size;
310
311             ptf[2].X = x2 + dx;
312             ptf[2].Y = y2 + dy;
313
314             transform_and_round_points(graphics, pt, ptf, 3);
315             Polygon(graphics->hdc, pt, 3);
316
317             break;
318         case LineCapRound:
319             dx = dy = size / 2.0;
320
321             ptf[0].X = x2 - dx;
322             ptf[0].Y = y2 - dy;
323             ptf[1].X = x2 + dx;
324             ptf[1].Y = y2 + dy;
325
326             dx = -cos(M_PI_2 + theta) * size;
327             dy = -sin(M_PI_2 + theta) * size;
328
329             ptf[2].X = x2 - dx;
330             ptf[2].Y = y2 - dy;
331             ptf[3].X = x2 + dx;
332             ptf[3].Y = y2 + dy;
333
334             transform_and_round_points(graphics, pt, ptf, 4);
335             Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
336                 pt[2].y, pt[3].x, pt[3].y);
337
338             break;
339         case LineCapCustom:
340             if(!custom)
341                 break;
342
343             count = custom->pathdata.Count;
344             custptf = GdipAlloc(count * sizeof(PointF));
345             custpt = GdipAlloc(count * sizeof(POINT));
346             tp = GdipAlloc(count);
347
348             if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
349                 goto custend;
350
351             memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
352
353             GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
354             GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
355                              MatrixOrderAppend);
356             GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
357             GdipTransformMatrixPoints(matrix, custptf, count);
358
359             transform_and_round_points(graphics, custpt, custptf, count);
360
361             for(i = 0; i < count; i++)
362                 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
363
364             if(custom->fill){
365                 BeginPath(graphics->hdc);
366                 PolyDraw(graphics->hdc, custpt, tp, count);
367                 EndPath(graphics->hdc);
368                 StrokeAndFillPath(graphics->hdc);
369             }
370             else
371                 PolyDraw(graphics->hdc, custpt, tp, count);
372
373 custend:
374             GdipFree(custptf);
375             GdipFree(custpt);
376             GdipFree(tp);
377             GdipDeleteMatrix(matrix);
378             break;
379         default:
380             break;
381     }
382
383     if(!customstroke){
384         SelectObject(graphics->hdc, oldbrush);
385         SelectObject(graphics->hdc, oldpen);
386         DeleteObject(brush);
387         DeleteObject(pen);
388     }
389 }
390
391 /* Shortens the line by the given percent by changing x2, y2.
392  * If percent is > 1.0 then the line will change direction.
393  * If percent is negative it can lengthen the line. */
394 static void shorten_line_percent(REAL x1, REAL  y1, REAL *x2, REAL *y2, REAL percent)
395 {
396     REAL dist, theta, dx, dy;
397
398     if((y1 == *y2) && (x1 == *x2))
399         return;
400
401     dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
402     theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
403     dx = cos(theta) * dist;
404     dy = sin(theta) * dist;
405
406     *x2 = *x2 + dx;
407     *y2 = *y2 + dy;
408 }
409
410 /* Shortens the line by the given amount by changing x2, y2.
411  * If the amount is greater than the distance, the line will become length 0.
412  * If the amount is negative, it can lengthen the line. */
413 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
414 {
415     REAL dx, dy, percent;
416
417     dx = *x2 - x1;
418     dy = *y2 - y1;
419     if(dx == 0 && dy == 0)
420         return;
421
422     percent = amt / sqrt(dx * dx + dy * dy);
423     if(percent >= 1.0){
424         *x2 = x1;
425         *y2 = y1;
426         return;
427     }
428
429     shorten_line_percent(x1, y1, x2, y2, percent);
430 }
431
432 /* Draws lines between the given points, and if caps is true then draws an endcap
433  * at the end of the last line. */
434 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
435     GDIPCONST GpPointF * pt, INT count, BOOL caps)
436 {
437     POINT *pti = NULL;
438     GpPointF *ptcopy = NULL;
439     GpStatus status = GenericError;
440
441     if(!count)
442         return Ok;
443
444     pti = GdipAlloc(count * sizeof(POINT));
445     ptcopy = GdipAlloc(count * sizeof(GpPointF));
446
447     if(!pti || !ptcopy){
448         status = OutOfMemory;
449         goto end;
450     }
451
452     memcpy(ptcopy, pt, count * sizeof(GpPointF));
453
454     if(caps){
455         if(pen->endcap == LineCapArrowAnchor)
456             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
457                              &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
458         else if((pen->endcap == LineCapCustom) && pen->customend)
459             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
460                              &ptcopy[count-1].X, &ptcopy[count-1].Y,
461                              pen->customend->inset * pen->width);
462
463         if(pen->startcap == LineCapArrowAnchor)
464             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
465                              &ptcopy[0].X, &ptcopy[0].Y, pen->width);
466         else if((pen->startcap == LineCapCustom) && pen->customstart)
467             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
468                              &ptcopy[0].X, &ptcopy[0].Y,
469                              pen->customstart->inset * pen->width);
470
471         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
472                  pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
473         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
474                          pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
475     }
476
477     transform_and_round_points(graphics, pti, ptcopy, count);
478
479     if(Polyline(graphics->hdc, pti, count))
480         status = Ok;
481
482 end:
483     GdipFree(pti);
484     GdipFree(ptcopy);
485
486     return status;
487 }
488
489 /* Conducts a linear search to find the bezier points that will back off
490  * the endpoint of the curve by a distance of amt. Linear search works
491  * better than binary in this case because there are multiple solutions,
492  * and binary searches often find a bad one. I don't think this is what
493  * Windows does but short of rendering the bezier without GDI's help it's
494  * the best we can do. If rev then work from the start of the passed points
495  * instead of the end. */
496 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
497 {
498     GpPointF origpt[4];
499     REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
500     INT i, first = 0, second = 1, third = 2, fourth = 3;
501
502     if(rev){
503         first = 3;
504         second = 2;
505         third = 1;
506         fourth = 0;
507     }
508
509     origx = pt[fourth].X;
510     origy = pt[fourth].Y;
511     memcpy(origpt, pt, sizeof(GpPointF) * 4);
512
513     for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
514         /* reset bezier points to original values */
515         memcpy(pt, origpt, sizeof(GpPointF) * 4);
516         /* Perform magic on bezier points. Order is important here.*/
517         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
518         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
519         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
520         shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
521         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
522         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
523
524         dx = pt[fourth].X - origx;
525         dy = pt[fourth].Y - origy;
526
527         diff = sqrt(dx * dx + dy * dy);
528         percent += 0.0005 * amt;
529     }
530 }
531
532 /* Draws bezier curves between given points, and if caps is true then draws an
533  * endcap at the end of the last line. */
534 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
535     GDIPCONST GpPointF * pt, INT count, BOOL caps)
536 {
537     POINT *pti;
538     GpPointF *ptcopy;
539     GpStatus status = GenericError;
540
541     if(!count)
542         return Ok;
543
544     pti = GdipAlloc(count * sizeof(POINT));
545     ptcopy = GdipAlloc(count * sizeof(GpPointF));
546
547     if(!pti || !ptcopy){
548         status = OutOfMemory;
549         goto end;
550     }
551
552     memcpy(ptcopy, pt, count * sizeof(GpPointF));
553
554     if(caps){
555         if(pen->endcap == LineCapArrowAnchor)
556             shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
557         else if((pen->endcap == LineCapCustom) && pen->customend)
558             shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
559                                FALSE);
560
561         if(pen->startcap == LineCapArrowAnchor)
562             shorten_bezier_amt(ptcopy, pen->width, TRUE);
563         else if((pen->startcap == LineCapCustom) && pen->customstart)
564             shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
565
566         /* the direction of the line cap is parallel to the direction at the
567          * end of the bezier (which, if it has been shortened, is not the same
568          * as the direction from pt[count-2] to pt[count-1]) */
569         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
570             pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
571             pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
572             pt[count - 1].X, pt[count - 1].Y);
573
574         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
575             pt[0].X - (ptcopy[0].X - ptcopy[1].X),
576             pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
577     }
578
579     transform_and_round_points(graphics, pti, ptcopy, count);
580
581     PolyBezier(graphics->hdc, pti, count);
582
583     status = Ok;
584
585 end:
586     GdipFree(pti);
587     GdipFree(ptcopy);
588
589     return status;
590 }
591
592 /* Draws a combination of bezier curves and lines between points. */
593 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
594     GDIPCONST BYTE * types, INT count, BOOL caps)
595 {
596     POINT *pti = GdipAlloc(count * sizeof(POINT));
597     BYTE *tp = GdipAlloc(count);
598     GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
599     INT i, j;
600     GpStatus status = GenericError;
601
602     if(!count){
603         status = Ok;
604         goto end;
605     }
606     if(!pti || !tp || !ptcopy){
607         status = OutOfMemory;
608         goto end;
609     }
610
611     for(i = 1; i < count; i++){
612         if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
613             if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
614                 || !(types[i + 1] & PathPointTypeBezier)){
615                 ERR("Bad bezier points\n");
616                 goto end;
617             }
618             i += 2;
619         }
620     }
621
622     memcpy(ptcopy, pt, count * sizeof(GpPointF));
623
624     /* If we are drawing caps, go through the points and adjust them accordingly,
625      * and draw the caps. */
626     if(caps){
627         switch(types[count - 1] & PathPointTypePathTypeMask){
628             case PathPointTypeBezier:
629                 if(pen->endcap == LineCapArrowAnchor)
630                     shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
631                 else if((pen->endcap == LineCapCustom) && pen->customend)
632                     shorten_bezier_amt(&ptcopy[count - 4],
633                                        pen->width * pen->customend->inset, FALSE);
634
635                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
636                     pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
637                     pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
638                     pt[count - 1].X, pt[count - 1].Y);
639
640                 break;
641             case PathPointTypeLine:
642                 if(pen->endcap == LineCapArrowAnchor)
643                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
644                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
645                                      pen->width);
646                 else if((pen->endcap == LineCapCustom) && pen->customend)
647                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
648                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
649                                      pen->customend->inset * pen->width);
650
651                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
652                          pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
653                          pt[count - 1].Y);
654
655                 break;
656             default:
657                 ERR("Bad path last point\n");
658                 goto end;
659         }
660
661         /* Find start of points */
662         for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
663             == PathPointTypeStart); j++);
664
665         switch(types[j] & PathPointTypePathTypeMask){
666             case PathPointTypeBezier:
667                 if(pen->startcap == LineCapArrowAnchor)
668                     shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
669                 else if((pen->startcap == LineCapCustom) && pen->customstart)
670                     shorten_bezier_amt(&ptcopy[j - 1],
671                                        pen->width * pen->customstart->inset, TRUE);
672
673                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
674                     pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
675                     pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
676                     pt[j - 1].X, pt[j - 1].Y);
677
678                 break;
679             case PathPointTypeLine:
680                 if(pen->startcap == LineCapArrowAnchor)
681                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
682                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
683                                      pen->width);
684                 else if((pen->startcap == LineCapCustom) && pen->customstart)
685                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
686                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
687                                      pen->customstart->inset * pen->width);
688
689                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
690                          pt[j].X, pt[j].Y, pt[j - 1].X,
691                          pt[j - 1].Y);
692
693                 break;
694             default:
695                 ERR("Bad path points\n");
696                 goto end;
697         }
698     }
699
700     transform_and_round_points(graphics, pti, ptcopy, count);
701
702     for(i = 0; i < count; i++){
703         tp[i] = convert_path_point_type(types[i]);
704     }
705
706     PolyDraw(graphics->hdc, pti, tp, count);
707
708     status = Ok;
709
710 end:
711     GdipFree(pti);
712     GdipFree(ptcopy);
713     GdipFree(tp);
714
715     return status;
716 }
717
718 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
719 {
720     GpStatus result;
721
722     BeginPath(graphics->hdc);
723     result = draw_poly(graphics, NULL, path->pathdata.Points,
724                        path->pathdata.Types, path->pathdata.Count, FALSE);
725     EndPath(graphics->hdc);
726     return result;
727 }
728
729 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
730 {
731     TRACE("(%p, %p)\n", hdc, graphics);
732
733     return GdipCreateFromHDC2(hdc, NULL, graphics);
734 }
735
736 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
737 {
738     GpStatus retval;
739
740     TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
741
742     if(hDevice != NULL) {
743         FIXME("Don't know how to hadle parameter hDevice\n");
744         return NotImplemented;
745     }
746
747     if(hdc == NULL)
748         return OutOfMemory;
749
750     if(graphics == NULL)
751         return InvalidParameter;
752
753     *graphics = GdipAlloc(sizeof(GpGraphics));
754     if(!*graphics)  return OutOfMemory;
755
756     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
757         GdipFree(*graphics);
758         return retval;
759     }
760
761     if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
762         GdipFree((*graphics)->worldtrans);
763         GdipFree(*graphics);
764         return retval;
765     }
766
767     (*graphics)->hdc = hdc;
768     (*graphics)->hwnd = WindowFromDC(hdc);
769     (*graphics)->smoothing = SmoothingModeDefault;
770     (*graphics)->compqual = CompositingQualityDefault;
771     (*graphics)->interpolation = InterpolationModeDefault;
772     (*graphics)->pixeloffset = PixelOffsetModeDefault;
773     (*graphics)->compmode = CompositingModeSourceOver;
774     (*graphics)->unit = UnitDisplay;
775     (*graphics)->scale = 1.0;
776     (*graphics)->busy = FALSE;
777     (*graphics)->textcontrast = 4;
778
779     return Ok;
780 }
781
782 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
783 {
784     GpStatus ret;
785
786     TRACE("(%p, %p)\n", hwnd, graphics);
787
788     if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
789         return ret;
790
791     (*graphics)->hwnd = hwnd;
792
793     return Ok;
794 }
795
796 /* FIXME: no icm handling */
797 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
798 {
799     TRACE("(%p, %p)\n", hwnd, graphics);
800
801     return GdipCreateFromHWND(hwnd, graphics);
802 }
803
804 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
805     GpMetafile **metafile)
806 {
807     static int calls;
808
809     if(!hemf || !metafile)
810         return InvalidParameter;
811
812     if(!(calls++))
813         FIXME("not implemented\n");
814
815     return NotImplemented;
816 }
817
818 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
819     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
820 {
821     IStream *stream = NULL;
822     UINT read;
823     BYTE* copy;
824     HENHMETAFILE hemf;
825     GpStatus retval = GenericError;
826
827     TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
828
829     if(!hwmf || !metafile || !placeable)
830         return InvalidParameter;
831
832     *metafile = NULL;
833     read = GetMetaFileBitsEx(hwmf, 0, NULL);
834     if(!read)
835         return GenericError;
836     copy = GdipAlloc(read);
837     GetMetaFileBitsEx(hwmf, read, copy);
838
839     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
840     GdipFree(copy);
841
842     read = GetEnhMetaFileBits(hemf, 0, NULL);
843     copy = GdipAlloc(read);
844     GetEnhMetaFileBits(hemf, read, copy);
845     DeleteEnhMetaFile(hemf);
846
847     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
848         ERR("could not make stream\n");
849         GdipFree(copy);
850         goto err;
851     }
852
853     *metafile = GdipAlloc(sizeof(GpMetafile));
854     if(!*metafile){
855         retval = OutOfMemory;
856         goto err;
857     }
858
859     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
860         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
861         goto err;
862
863
864     (*metafile)->image.type = ImageTypeMetafile;
865     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
866     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
867     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
868                     - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
869     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
870                    - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
871     (*metafile)->unit = UnitInch;
872
873     if(delete)
874         DeleteMetaFile(hwmf);
875
876     return Ok;
877
878 err:
879     GdipFree(*metafile);
880     IStream_Release(stream);
881     return retval;
882 }
883
884 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
885     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
886 {
887     HMETAFILE hmf = GetMetaFileW(file);
888
889     TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
890
891     if(!hmf) return InvalidParameter;
892
893     return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
894 }
895
896 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
897     UINT access, IStream **stream)
898 {
899     DWORD dwMode;
900     HRESULT ret;
901
902     TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
903
904     if(!stream || !filename)
905         return InvalidParameter;
906
907     if(access & GENERIC_WRITE)
908         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
909     else if(access & GENERIC_READ)
910         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
911     else
912         return InvalidParameter;
913
914     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
915
916     return hresult_to_status(ret);
917 }
918
919 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
920 {
921     TRACE("(%p)\n", graphics);
922
923     if(!graphics) return InvalidParameter;
924     if(graphics->busy) return ObjectBusy;
925
926     if(graphics->hwnd)
927         ReleaseDC(graphics->hwnd, graphics->hdc);
928
929     GdipDeleteRegion(graphics->clip);
930     GdipDeleteMatrix(graphics->worldtrans);
931     GdipFree(graphics);
932
933     return Ok;
934 }
935
936 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
937     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
938 {
939     INT save_state, num_pts;
940     GpPointF points[MAX_ARC_PTS];
941     GpStatus retval;
942
943     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
944           width, height, startAngle, sweepAngle);
945
946     if(!graphics || !pen || width <= 0 || height <= 0)
947         return InvalidParameter;
948
949     if(graphics->busy)
950         return ObjectBusy;
951
952     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
953
954     save_state = prepare_dc(graphics, pen);
955
956     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
957
958     restore_dc(graphics, save_state);
959
960     return retval;
961 }
962
963 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
964     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
965 {
966     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
967           width, height, startAngle, sweepAngle);
968
969     return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
970 }
971
972 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
973     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
974 {
975     INT save_state;
976     GpPointF pt[4];
977     GpStatus retval;
978
979     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
980           x2, y2, x3, y3, x4, y4);
981
982     if(!graphics || !pen)
983         return InvalidParameter;
984
985     if(graphics->busy)
986         return ObjectBusy;
987
988     pt[0].X = x1;
989     pt[0].Y = y1;
990     pt[1].X = x2;
991     pt[1].Y = y2;
992     pt[2].X = x3;
993     pt[2].Y = y3;
994     pt[3].X = x4;
995     pt[3].Y = y4;
996
997     save_state = prepare_dc(graphics, pen);
998
999     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1000
1001     restore_dc(graphics, save_state);
1002
1003     return retval;
1004 }
1005
1006 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1007     INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1008 {
1009     INT save_state;
1010     GpPointF pt[4];
1011     GpStatus retval;
1012
1013     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1014           x2, y2, x3, y3, x4, y4);
1015
1016     if(!graphics || !pen)
1017         return InvalidParameter;
1018
1019     if(graphics->busy)
1020         return ObjectBusy;
1021
1022     pt[0].X = x1;
1023     pt[0].Y = y1;
1024     pt[1].X = x2;
1025     pt[1].Y = y2;
1026     pt[2].X = x3;
1027     pt[2].Y = y3;
1028     pt[3].X = x4;
1029     pt[3].Y = y4;
1030
1031     save_state = prepare_dc(graphics, pen);
1032
1033     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1034
1035     restore_dc(graphics, save_state);
1036
1037     return retval;
1038 }
1039
1040 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1041     GDIPCONST GpPointF *points, INT count)
1042 {
1043     INT i;
1044     GpStatus ret;
1045
1046     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1047
1048     if(!graphics || !pen || !points || (count <= 0))
1049         return InvalidParameter;
1050
1051     if(graphics->busy)
1052         return ObjectBusy;
1053
1054     for(i = 0; i < floor(count / 4); i++){
1055         ret = GdipDrawBezier(graphics, pen,
1056                              points[4*i].X, points[4*i].Y,
1057                              points[4*i + 1].X, points[4*i + 1].Y,
1058                              points[4*i + 2].X, points[4*i + 2].Y,
1059                              points[4*i + 3].X, points[4*i + 3].Y);
1060         if(ret != Ok)
1061             return ret;
1062     }
1063
1064     return Ok;
1065 }
1066
1067 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1068     GDIPCONST GpPoint *points, INT count)
1069 {
1070     GpPointF *pts;
1071     GpStatus ret;
1072     INT i;
1073
1074     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1075
1076     if(!graphics || !pen || !points || (count <= 0))
1077         return InvalidParameter;
1078
1079     if(graphics->busy)
1080         return ObjectBusy;
1081
1082     pts = GdipAlloc(sizeof(GpPointF) * count);
1083     if(!pts)
1084         return OutOfMemory;
1085
1086     for(i = 0; i < count; i++){
1087         pts[i].X = (REAL)points[i].X;
1088         pts[i].Y = (REAL)points[i].Y;
1089     }
1090
1091     ret = GdipDrawBeziers(graphics,pen,pts,count);
1092
1093     GdipFree(pts);
1094
1095     return ret;
1096 }
1097
1098 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1099     GDIPCONST GpPointF *points, INT count)
1100 {
1101     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1102
1103     return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1104 }
1105
1106 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1107     GDIPCONST GpPoint *points, INT count)
1108 {
1109     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1110
1111     return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1112 }
1113
1114 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1115     GDIPCONST GpPointF *points, INT count, REAL tension)
1116 {
1117     GpPath *path;
1118     GpStatus stat;
1119
1120     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1121
1122     if(!graphics || !pen || !points || count <= 0)
1123         return InvalidParameter;
1124
1125     if(graphics->busy)
1126         return ObjectBusy;
1127
1128     if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1129         return stat;
1130
1131     stat = GdipAddPathClosedCurve2(path, points, count, tension);
1132     if(stat != Ok){
1133         GdipDeletePath(path);
1134         return stat;
1135     }
1136
1137     stat = GdipDrawPath(graphics, pen, path);
1138
1139     GdipDeletePath(path);
1140
1141     return stat;
1142 }
1143
1144 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1145     GDIPCONST GpPoint *points, INT count, REAL tension)
1146 {
1147     GpPointF *ptf;
1148     GpStatus stat;
1149     INT i;
1150
1151     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1152
1153     if(!points || count <= 0)
1154         return InvalidParameter;
1155
1156     ptf = GdipAlloc(sizeof(GpPointF)*count);
1157     if(!ptf)
1158         return OutOfMemory;
1159
1160     for(i = 0; i < count; i++){
1161         ptf[i].X = (REAL)points[i].X;
1162         ptf[i].Y = (REAL)points[i].Y;
1163     }
1164
1165     stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1166
1167     GdipFree(ptf);
1168
1169     return stat;
1170 }
1171
1172 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1173     GDIPCONST GpPointF *points, INT count)
1174 {
1175     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1176
1177     return GdipDrawCurve2(graphics,pen,points,count,1.0);
1178 }
1179
1180 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1181     GDIPCONST GpPoint *points, INT count)
1182 {
1183     GpPointF *pointsF;
1184     GpStatus ret;
1185     INT i;
1186
1187     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1188
1189     if(!points || count <= 0)
1190         return InvalidParameter;
1191
1192     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1193     if(!pointsF)
1194         return OutOfMemory;
1195
1196     for(i = 0; i < count; i++){
1197         pointsF[i].X = (REAL)points[i].X;
1198         pointsF[i].Y = (REAL)points[i].Y;
1199     }
1200
1201     ret = GdipDrawCurve(graphics,pen,pointsF,count);
1202     GdipFree(pointsF);
1203
1204     return ret;
1205 }
1206
1207 /* Approximates cardinal spline with Bezier curves. */
1208 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1209     GDIPCONST GpPointF *points, INT count, REAL tension)
1210 {
1211     /* PolyBezier expects count*3-2 points. */
1212     INT i, len_pt = count*3-2, save_state;
1213     GpPointF *pt;
1214     REAL x1, x2, y1, y2;
1215     GpStatus retval;
1216
1217     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1218
1219     if(!graphics || !pen)
1220         return InvalidParameter;
1221
1222     if(graphics->busy)
1223         return ObjectBusy;
1224
1225     pt = GdipAlloc(len_pt * sizeof(GpPointF));
1226     tension = tension * TENSION_CONST;
1227
1228     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1229         tension, &x1, &y1);
1230
1231     pt[0].X = points[0].X;
1232     pt[0].Y = points[0].Y;
1233     pt[1].X = x1;
1234     pt[1].Y = y1;
1235
1236     for(i = 0; i < count-2; i++){
1237         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1238
1239         pt[3*i+2].X = x1;
1240         pt[3*i+2].Y = y1;
1241         pt[3*i+3].X = points[i+1].X;
1242         pt[3*i+3].Y = points[i+1].Y;
1243         pt[3*i+4].X = x2;
1244         pt[3*i+4].Y = y2;
1245     }
1246
1247     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1248         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1249
1250     pt[len_pt-2].X = x1;
1251     pt[len_pt-2].Y = y1;
1252     pt[len_pt-1].X = points[count-1].X;
1253     pt[len_pt-1].Y = points[count-1].Y;
1254
1255     save_state = prepare_dc(graphics, pen);
1256
1257     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1258
1259     GdipFree(pt);
1260     restore_dc(graphics, save_state);
1261
1262     return retval;
1263 }
1264
1265 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1266     GDIPCONST GpPoint *points, INT count, REAL tension)
1267 {
1268     GpPointF *pointsF;
1269     GpStatus ret;
1270     INT i;
1271
1272     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1273
1274     if(!points || count <= 0)
1275         return InvalidParameter;
1276
1277     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1278     if(!pointsF)
1279         return OutOfMemory;
1280
1281     for(i = 0; i < count; i++){
1282         pointsF[i].X = (REAL)points[i].X;
1283         pointsF[i].Y = (REAL)points[i].Y;
1284     }
1285
1286     ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1287     GdipFree(pointsF);
1288
1289     return ret;
1290 }
1291
1292 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1293     REAL y, REAL width, REAL height)
1294 {
1295     INT save_state;
1296     GpPointF ptf[2];
1297     POINT pti[2];
1298
1299     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1300
1301     if(!graphics || !pen)
1302         return InvalidParameter;
1303
1304     if(graphics->busy)
1305         return ObjectBusy;
1306
1307     ptf[0].X = x;
1308     ptf[0].Y = y;
1309     ptf[1].X = x + width;
1310     ptf[1].Y = y + height;
1311
1312     save_state = prepare_dc(graphics, pen);
1313     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1314
1315     transform_and_round_points(graphics, pti, ptf, 2);
1316
1317     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1318
1319     restore_dc(graphics, save_state);
1320
1321     return Ok;
1322 }
1323
1324 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1325     INT y, INT width, INT height)
1326 {
1327     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1328
1329     return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1330 }
1331
1332
1333 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1334 {
1335     TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1336
1337     /* IPicture::Render uses LONG coords */
1338     return GdipDrawImageI(graphics,image,roundr(x),roundr(y));
1339 }
1340
1341 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1342     INT y)
1343 {
1344     UINT width, height, srcw, srch;
1345
1346     TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1347
1348     if(!graphics || !image)
1349         return InvalidParameter;
1350
1351     GdipGetImageWidth(image, &width);
1352     GdipGetImageHeight(image, &height);
1353
1354     srcw = width * (((REAL) INCH_HIMETRIC) /
1355             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1356     srch = height * (((REAL) INCH_HIMETRIC) /
1357             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1358
1359     if(image->type != ImageTypeMetafile){
1360         y += height;
1361         height *= -1;
1362     }
1363
1364     IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1365                     0, 0, srcw, srch, NULL);
1366
1367     return Ok;
1368 }
1369
1370 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1371 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1372      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1373      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1374      DrawImageAbort callback, VOID * callbackData)
1375 {
1376     GpPointF ptf[3];
1377     POINT pti[3];
1378     REAL dx, dy;
1379
1380     TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
1381           count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1382           callbackData);
1383
1384     if(!graphics || !image || !points || count != 3)
1385          return InvalidParameter;
1386
1387     if(srcUnit == UnitInch)
1388         dx = dy = (REAL) INCH_HIMETRIC;
1389     else if(srcUnit == UnitPixel){
1390         dx = ((REAL) INCH_HIMETRIC) /
1391              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1392         dy = ((REAL) INCH_HIMETRIC) /
1393              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1394     }
1395     else
1396         return NotImplemented;
1397
1398     memcpy(ptf, points, 3 * sizeof(GpPointF));
1399     transform_and_round_points(graphics, pti, ptf, 3);
1400
1401     /* IPicture renders bitmaps with the y-axis reversed
1402      * FIXME: flipping for unknown image type might not be correct. */
1403     if(image->type != ImageTypeMetafile){
1404         INT temp;
1405         temp = pti[0].y;
1406         pti[0].y = pti[2].y;
1407         pti[2].y = temp;
1408     }
1409
1410     if(IPicture_Render(image->picture, graphics->hdc,
1411         pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1412         srcx * dx, srcy * dy,
1413         srcwidth * dx, srcheight * dy,
1414         NULL) != S_OK){
1415         if(callback)
1416             callback(callbackData);
1417         return GenericError;
1418     }
1419
1420     return Ok;
1421 }
1422
1423 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1424      GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1425      INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1426      DrawImageAbort callback, VOID * callbackData)
1427 {
1428     GpPointF pointsF[3];
1429     INT i;
1430
1431     TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
1432           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1433           callbackData);
1434
1435     if(!points || count!=3)
1436         return InvalidParameter;
1437
1438     for(i = 0; i < count; i++){
1439         pointsF[i].X = (REAL)points[i].X;
1440         pointsF[i].Y = (REAL)points[i].Y;
1441     }
1442
1443     return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1444                                    (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1445                                    callback, callbackData);
1446 }
1447
1448 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1449     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1450     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1451     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1452     VOID * callbackData)
1453 {
1454     GpPointF points[3];
1455
1456     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
1457           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1458           srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1459
1460     points[0].X = dstx;
1461     points[0].Y = dsty;
1462     points[1].X = dstx + dstwidth;
1463     points[1].Y = dsty;
1464     points[2].X = dstx;
1465     points[2].Y = dsty + dstheight;
1466
1467     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1468                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1469 }
1470
1471 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1472         INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1473         INT srcwidth, INT srcheight, GpUnit srcUnit,
1474         GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1475         VOID * callbackData)
1476 {
1477     GpPointF points[3];
1478
1479     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
1480           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
1481           srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1482
1483     points[0].X = dstx;
1484     points[0].Y = dsty;
1485     points[1].X = dstx + dstwidth;
1486     points[1].Y = dsty;
1487     points[2].X = dstx;
1488     points[2].Y = dsty + dstheight;
1489
1490     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1491                srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1492 }
1493
1494 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1495     REAL x, REAL y, REAL width, REAL height)
1496 {
1497     RectF bounds;
1498     GpUnit unit;
1499     GpStatus ret;
1500
1501     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
1502
1503     if(!graphics || !image)
1504         return InvalidParameter;
1505
1506     ret = GdipGetImageBounds(image, &bounds, &unit);
1507     if(ret != Ok)
1508         return ret;
1509
1510     return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1511                                  bounds.X, bounds.Y, bounds.Width, bounds.Height,
1512                                  unit, NULL, NULL, NULL);
1513 }
1514
1515 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1516     INT x, INT y, INT width, INT height)
1517 {
1518     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
1519
1520     return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1521 }
1522
1523 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1524     REAL y1, REAL x2, REAL y2)
1525 {
1526     INT save_state;
1527     GpPointF pt[2];
1528     GpStatus retval;
1529
1530     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
1531
1532     if(!pen || !graphics)
1533         return InvalidParameter;
1534
1535     if(graphics->busy)
1536         return ObjectBusy;
1537
1538     pt[0].X = x1;
1539     pt[0].Y = y1;
1540     pt[1].X = x2;
1541     pt[1].Y = y2;
1542
1543     save_state = prepare_dc(graphics, pen);
1544
1545     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1546
1547     restore_dc(graphics, save_state);
1548
1549     return retval;
1550 }
1551
1552 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1553     INT y1, INT x2, INT y2)
1554 {
1555     INT save_state;
1556     GpPointF pt[2];
1557     GpStatus retval;
1558
1559     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
1560
1561     if(!pen || !graphics)
1562         return InvalidParameter;
1563
1564     if(graphics->busy)
1565         return ObjectBusy;
1566
1567     pt[0].X = (REAL)x1;
1568     pt[0].Y = (REAL)y1;
1569     pt[1].X = (REAL)x2;
1570     pt[1].Y = (REAL)y2;
1571
1572     save_state = prepare_dc(graphics, pen);
1573
1574     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1575
1576     restore_dc(graphics, save_state);
1577
1578     return retval;
1579 }
1580
1581 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1582     GpPointF *points, INT count)
1583 {
1584     INT save_state;
1585     GpStatus retval;
1586
1587     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1588
1589     if(!pen || !graphics || (count < 2))
1590         return InvalidParameter;
1591
1592     if(graphics->busy)
1593         return ObjectBusy;
1594
1595     save_state = prepare_dc(graphics, pen);
1596
1597     retval = draw_polyline(graphics, pen, points, count, TRUE);
1598
1599     restore_dc(graphics, save_state);
1600
1601     return retval;
1602 }
1603
1604 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
1605     GpPoint *points, INT count)
1606 {
1607     INT save_state;
1608     GpStatus retval;
1609     GpPointF *ptf = NULL;
1610     int i;
1611
1612     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1613
1614     if(!pen || !graphics || (count < 2))
1615         return InvalidParameter;
1616
1617     if(graphics->busy)
1618         return ObjectBusy;
1619
1620     ptf = GdipAlloc(count * sizeof(GpPointF));
1621     if(!ptf) return OutOfMemory;
1622
1623     for(i = 0; i < count; i ++){
1624         ptf[i].X = (REAL) points[i].X;
1625         ptf[i].Y = (REAL) points[i].Y;
1626     }
1627
1628     save_state = prepare_dc(graphics, pen);
1629
1630     retval = draw_polyline(graphics, pen, ptf, count, TRUE);
1631
1632     restore_dc(graphics, save_state);
1633
1634     GdipFree(ptf);
1635     return retval;
1636 }
1637
1638 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1639 {
1640     INT save_state;
1641     GpStatus retval;
1642
1643     TRACE("(%p, %p, %p)\n", graphics, pen, path);
1644
1645     if(!pen || !graphics)
1646         return InvalidParameter;
1647
1648     if(graphics->busy)
1649         return ObjectBusy;
1650
1651     save_state = prepare_dc(graphics, pen);
1652
1653     retval = draw_poly(graphics, pen, path->pathdata.Points,
1654                        path->pathdata.Types, path->pathdata.Count, TRUE);
1655
1656     restore_dc(graphics, save_state);
1657
1658     return retval;
1659 }
1660
1661 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1662     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1663 {
1664     INT save_state;
1665
1666     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1667             width, height, startAngle, sweepAngle);
1668
1669     if(!graphics || !pen)
1670         return InvalidParameter;
1671
1672     if(graphics->busy)
1673         return ObjectBusy;
1674
1675     save_state = prepare_dc(graphics, pen);
1676     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1677
1678     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1679
1680     restore_dc(graphics, save_state);
1681
1682     return Ok;
1683 }
1684
1685 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
1686     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1687 {
1688     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1689             width, height, startAngle, sweepAngle);
1690
1691     return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1692 }
1693
1694 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
1695     REAL y, REAL width, REAL height)
1696 {
1697     INT save_state;
1698     GpPointF ptf[4];
1699     POINT pti[4];
1700
1701     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1702
1703     if(!pen || !graphics)
1704         return InvalidParameter;
1705
1706     if(graphics->busy)
1707         return ObjectBusy;
1708
1709     ptf[0].X = x;
1710     ptf[0].Y = y;
1711     ptf[1].X = x + width;
1712     ptf[1].Y = y;
1713     ptf[2].X = x + width;
1714     ptf[2].Y = y + height;
1715     ptf[3].X = x;
1716     ptf[3].Y = y + height;
1717
1718     save_state = prepare_dc(graphics, pen);
1719     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1720
1721     transform_and_round_points(graphics, pti, ptf, 4);
1722     Polygon(graphics->hdc, pti, 4);
1723
1724     restore_dc(graphics, save_state);
1725
1726     return Ok;
1727 }
1728
1729 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1730     INT y, INT width, INT height)
1731 {
1732     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1733
1734     return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1735 }
1736
1737 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1738     GDIPCONST GpRectF* rects, INT count)
1739 {
1740     GpPointF *ptf;
1741     POINT *pti;
1742     INT save_state, i;
1743
1744     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
1745
1746     if(!graphics || !pen || !rects || count < 1)
1747         return InvalidParameter;
1748
1749     if(graphics->busy)
1750         return ObjectBusy;
1751
1752     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1753     pti = GdipAlloc(4 * count * sizeof(POINT));
1754
1755     if(!ptf || !pti){
1756         GdipFree(ptf);
1757         GdipFree(pti);
1758         return OutOfMemory;
1759     }
1760
1761     for(i = 0; i < count; i++){
1762         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1763         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1764         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1765         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1766     }
1767
1768     save_state = prepare_dc(graphics, pen);
1769     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1770
1771     transform_and_round_points(graphics, pti, ptf, 4 * count);
1772
1773     for(i = 0; i < count; i++)
1774         Polygon(graphics->hdc, &pti[4 * i], 4);
1775
1776     restore_dc(graphics, save_state);
1777
1778     GdipFree(ptf);
1779     GdipFree(pti);
1780
1781     return Ok;
1782 }
1783
1784 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
1785     GDIPCONST GpRect* rects, INT count)
1786 {
1787     GpRectF *rectsF;
1788     GpStatus ret;
1789     INT i;
1790
1791     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
1792
1793     if(!rects || count<=0)
1794         return InvalidParameter;
1795
1796     rectsF = GdipAlloc(sizeof(GpRectF) * count);
1797     if(!rectsF)
1798         return OutOfMemory;
1799
1800     for(i = 0;i < count;i++){
1801         rectsF[i].X      = (REAL)rects[i].X;
1802         rectsF[i].Y      = (REAL)rects[i].Y;
1803         rectsF[i].Width  = (REAL)rects[i].Width;
1804         rectsF[i].Height = (REAL)rects[i].Height;
1805     }
1806
1807     ret = GdipDrawRectangles(graphics, pen, rectsF, count);
1808     GdipFree(rectsF);
1809
1810     return ret;
1811 }
1812
1813 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1814     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1815     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1816 {
1817     HRGN rgn = NULL;
1818     HFONT gdifont;
1819     LOGFONTW lfw;
1820     TEXTMETRICW textmet;
1821     GpPointF pt[2], rectcpy[4];
1822     POINT corners[4];
1823     WCHAR* stringdup;
1824     REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1825     INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
1826         nheight;
1827     SIZE size;
1828     RECT drawcoord;
1829
1830     if(!graphics || !string || !font || !brush || !rect)
1831         return InvalidParameter;
1832
1833     if((brush->bt != BrushTypeSolidColor)){
1834         FIXME("not implemented for given parameters\n");
1835         return NotImplemented;
1836     }
1837
1838     if(format)
1839         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1840
1841     if(length == -1) length = lstrlenW(string);
1842
1843     stringdup = GdipAlloc(length * sizeof(WCHAR));
1844     if(!stringdup) return OutOfMemory;
1845
1846     save_state = SaveDC(graphics->hdc);
1847     SetBkMode(graphics->hdc, TRANSPARENT);
1848     SetTextColor(graphics->hdc, brush->lb.lbColor);
1849
1850     rectcpy[3].X = rectcpy[0].X = rect->X;
1851     rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1852     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1853     rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1854     transform_and_round_points(graphics, corners, rectcpy, 4);
1855
1856     if(roundr(rect->Width) == 0 && roundr(rect->Height) == 0){
1857         rel_width = rel_height = 1.0;
1858         nwidth = nheight = INT_MAX;
1859     }
1860     else{
1861         rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1862                          (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1863                          / rect->Width;
1864         rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1865                           (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1866                           / rect->Height;
1867
1868         nwidth = roundr(rel_width * rect->Width);
1869         nheight = roundr(rel_height * rect->Height);
1870         rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1871         SelectClipRgn(graphics->hdc, rgn);
1872     }
1873
1874     /* Use gdi to find the font, then perform transformations on it (height,
1875      * width, angle). */
1876     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1877     GetTextMetricsW(graphics->hdc, &textmet);
1878     lfw = font->lfw;
1879
1880     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1881     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1882
1883     pt[0].X = 0.0;
1884     pt[0].Y = 0.0;
1885     pt[1].X = 1.0;
1886     pt[1].Y = 0.0;
1887     GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1888     angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1889     ang_cos = cos(angle);
1890     ang_sin = sin(angle);
1891     lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1892
1893     gdifont = CreateFontIndirectW(&lfw);
1894     DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1895
1896     for(i = 0, j = 0; i < length; i++){
1897         if(!isprintW(string[i]) && (string[i] != '\n'))
1898             continue;
1899
1900         stringdup[j] = string[i];
1901         j++;
1902     }
1903
1904     length = j;
1905
1906     while(sum < length){
1907         drawcoord.left = corners[0].x + roundr(ang_sin * (REAL) height);
1908         drawcoord.top = corners[0].y + roundr(ang_cos * (REAL) height);
1909
1910         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1911                               nwidth, &fit, NULL, &size);
1912         fitcpy = fit;
1913
1914         if(fit == 0){
1915             DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, DT_NOCLIP |
1916                       DT_EXPANDTABS);
1917             break;
1918         }
1919
1920         for(lret = 0; lret < fit; lret++)
1921             if(*(stringdup + sum + lret) == '\n')
1922                 break;
1923
1924         /* Line break code (may look strange, but it imitates windows). */
1925         if(lret < fit)
1926             fit = lret;    /* this is not an off-by-one error */
1927         else if(fit < (length - sum)){
1928             if(*(stringdup + sum + fit) == ' ')
1929                 while(*(stringdup + sum + fit) == ' ')
1930                     fit++;
1931             else
1932                 while(*(stringdup + sum + fit - 1) != ' '){
1933                     fit--;
1934
1935                     if(*(stringdup + sum + fit) == '\t')
1936                         break;
1937
1938                     if(fit == 0){
1939                         fit = fitcpy;
1940                         break;
1941                     }
1942                 }
1943         }
1944         DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
1945                   &drawcoord, DT_NOCLIP | DT_EXPANDTABS);
1946
1947         sum += fit + (lret < fitcpy ? 1 : 0);
1948         height += size.cy;
1949
1950         if(height > nheight)
1951             break;
1952
1953         /* Stop if this was a linewrap (but not if it was a linebreak). */
1954         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1955             break;
1956     }
1957
1958     GdipFree(stringdup);
1959     DeleteObject(rgn);
1960     DeleteObject(gdifont);
1961
1962     RestoreDC(graphics->hdc, save_state);
1963
1964     return Ok;
1965 }
1966
1967 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
1968     GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
1969 {
1970     GpPath *path;
1971     GpStatus stat;
1972
1973     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
1974             count, tension, fill);
1975
1976     if(!graphics || !brush || !points)
1977         return InvalidParameter;
1978
1979     if(graphics->busy)
1980         return ObjectBusy;
1981
1982     stat = GdipCreatePath(fill, &path);
1983     if(stat != Ok)
1984         return stat;
1985
1986     stat = GdipAddPathClosedCurve2(path, points, count, tension);
1987     if(stat != Ok){
1988         GdipDeletePath(path);
1989         return stat;
1990     }
1991
1992     stat = GdipFillPath(graphics, brush, path);
1993     if(stat != Ok){
1994         GdipDeletePath(path);
1995         return stat;
1996     }
1997
1998     GdipDeletePath(path);
1999
2000     return Ok;
2001 }
2002
2003 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
2004     GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
2005 {
2006     GpPointF *ptf;
2007     GpStatus stat;
2008     INT i;
2009
2010     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2011             count, tension, fill);
2012
2013     if(!points || count <= 0)
2014         return InvalidParameter;
2015
2016     ptf = GdipAlloc(sizeof(GpPointF)*count);
2017     if(!ptf)
2018         return OutOfMemory;
2019
2020     for(i = 0;i < count;i++){
2021         ptf[i].X = (REAL)points[i].X;
2022         ptf[i].Y = (REAL)points[i].Y;
2023     }
2024
2025     stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2026
2027     GdipFree(ptf);
2028
2029     return stat;
2030 }
2031
2032 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2033     REAL y, REAL width, REAL height)
2034 {
2035     INT save_state;
2036     GpPointF ptf[2];
2037     POINT pti[2];
2038
2039     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2040
2041     if(!graphics || !brush)
2042         return InvalidParameter;
2043
2044     if(graphics->busy)
2045         return ObjectBusy;
2046
2047     ptf[0].X = x;
2048     ptf[0].Y = y;
2049     ptf[1].X = x + width;
2050     ptf[1].Y = y + height;
2051
2052     save_state = SaveDC(graphics->hdc);
2053     EndPath(graphics->hdc);
2054     SelectObject(graphics->hdc, brush->gdibrush);
2055     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2056
2057     transform_and_round_points(graphics, pti, ptf, 2);
2058
2059     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2060
2061     RestoreDC(graphics->hdc, save_state);
2062
2063     return Ok;
2064 }
2065
2066 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2067     INT y, INT width, INT height)
2068 {
2069     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2070
2071     return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2072 }
2073
2074 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2075 {
2076     INT save_state;
2077     GpStatus retval;
2078
2079     TRACE("(%p, %p, %p)\n", graphics, brush, path);
2080
2081     if(!brush || !graphics || !path)
2082         return InvalidParameter;
2083
2084     if(graphics->busy)
2085         return ObjectBusy;
2086
2087     save_state = SaveDC(graphics->hdc);
2088     EndPath(graphics->hdc);
2089     SelectObject(graphics->hdc, brush->gdibrush);
2090     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2091                                                                     : WINDING));
2092
2093     BeginPath(graphics->hdc);
2094     retval = draw_poly(graphics, NULL, path->pathdata.Points,
2095                        path->pathdata.Types, path->pathdata.Count, FALSE);
2096
2097     if(retval != Ok)
2098         goto end;
2099
2100     EndPath(graphics->hdc);
2101     FillPath(graphics->hdc);
2102
2103     retval = Ok;
2104
2105 end:
2106     RestoreDC(graphics->hdc, save_state);
2107
2108     return retval;
2109 }
2110
2111 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2112     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2113 {
2114     INT save_state;
2115
2116     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2117             graphics, brush, x, y, width, height, startAngle, sweepAngle);
2118
2119     if(!graphics || !brush)
2120         return InvalidParameter;
2121
2122     if(graphics->busy)
2123         return ObjectBusy;
2124
2125     save_state = SaveDC(graphics->hdc);
2126     EndPath(graphics->hdc);
2127     SelectObject(graphics->hdc, brush->gdibrush);
2128     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2129
2130     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2131
2132     RestoreDC(graphics->hdc, save_state);
2133
2134     return Ok;
2135 }
2136
2137 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2138     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2139 {
2140     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
2141             graphics, brush, x, y, width, height, startAngle, sweepAngle);
2142
2143     return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2144 }
2145
2146 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2147     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2148 {
2149     INT save_state;
2150     GpPointF *ptf = NULL;
2151     POINT *pti = NULL;
2152     GpStatus retval = Ok;
2153
2154     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2155
2156     if(!graphics || !brush || !points || !count)
2157         return InvalidParameter;
2158
2159     if(graphics->busy)
2160         return ObjectBusy;
2161
2162     ptf = GdipAlloc(count * sizeof(GpPointF));
2163     pti = GdipAlloc(count * sizeof(POINT));
2164     if(!ptf || !pti){
2165         retval = OutOfMemory;
2166         goto end;
2167     }
2168
2169     memcpy(ptf, points, count * sizeof(GpPointF));
2170
2171     save_state = SaveDC(graphics->hdc);
2172     EndPath(graphics->hdc);
2173     SelectObject(graphics->hdc, brush->gdibrush);
2174     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2175     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2176                                                                   : WINDING));
2177
2178     transform_and_round_points(graphics, pti, ptf, count);
2179     Polygon(graphics->hdc, pti, count);
2180
2181     RestoreDC(graphics->hdc, save_state);
2182
2183 end:
2184     GdipFree(ptf);
2185     GdipFree(pti);
2186
2187     return retval;
2188 }
2189
2190 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2191     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2192 {
2193     INT save_state, i;
2194     GpPointF *ptf = NULL;
2195     POINT *pti = NULL;
2196     GpStatus retval = Ok;
2197
2198     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
2199
2200     if(!graphics || !brush || !points || !count)
2201         return InvalidParameter;
2202
2203     if(graphics->busy)
2204         return ObjectBusy;
2205
2206     ptf = GdipAlloc(count * sizeof(GpPointF));
2207     pti = GdipAlloc(count * sizeof(POINT));
2208     if(!ptf || !pti){
2209         retval = OutOfMemory;
2210         goto end;
2211     }
2212
2213     for(i = 0; i < count; i ++){
2214         ptf[i].X = (REAL) points[i].X;
2215         ptf[i].Y = (REAL) points[i].Y;
2216     }
2217
2218     save_state = SaveDC(graphics->hdc);
2219     EndPath(graphics->hdc);
2220     SelectObject(graphics->hdc, brush->gdibrush);
2221     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2222     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2223                                                                   : WINDING));
2224
2225     transform_and_round_points(graphics, pti, ptf, count);
2226     Polygon(graphics->hdc, pti, count);
2227
2228     RestoreDC(graphics->hdc, save_state);
2229
2230 end:
2231     GdipFree(ptf);
2232     GdipFree(pti);
2233
2234     return retval;
2235 }
2236
2237 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2238     GDIPCONST GpPointF *points, INT count)
2239 {
2240     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2241
2242     return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2243 }
2244
2245 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2246     GDIPCONST GpPoint *points, INT count)
2247 {
2248     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2249
2250     return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2251 }
2252
2253 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2254     REAL x, REAL y, REAL width, REAL height)
2255 {
2256     INT save_state;
2257     GpPointF ptf[4];
2258     POINT pti[4];
2259
2260     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2261
2262     if(!graphics || !brush)
2263         return InvalidParameter;
2264
2265     if(graphics->busy)
2266         return ObjectBusy;
2267
2268     ptf[0].X = x;
2269     ptf[0].Y = y;
2270     ptf[1].X = x + width;
2271     ptf[1].Y = y;
2272     ptf[2].X = x + width;
2273     ptf[2].Y = y + height;
2274     ptf[3].X = x;
2275     ptf[3].Y = y + height;
2276
2277     save_state = SaveDC(graphics->hdc);
2278     EndPath(graphics->hdc);
2279     SelectObject(graphics->hdc, brush->gdibrush);
2280     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2281
2282     transform_and_round_points(graphics, pti, ptf, 4);
2283
2284     Polygon(graphics->hdc, pti, 4);
2285
2286     RestoreDC(graphics->hdc, save_state);
2287
2288     return Ok;
2289 }
2290
2291 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2292     INT x, INT y, INT width, INT height)
2293 {
2294     INT save_state;
2295     GpPointF ptf[4];
2296     POINT pti[4];
2297
2298     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2299
2300     if(!graphics || !brush)
2301         return InvalidParameter;
2302
2303     if(graphics->busy)
2304         return ObjectBusy;
2305
2306     ptf[0].X = x;
2307     ptf[0].Y = y;
2308     ptf[1].X = x + width;
2309     ptf[1].Y = y;
2310     ptf[2].X = x + width;
2311     ptf[2].Y = y + height;
2312     ptf[3].X = x;
2313     ptf[3].Y = y + height;
2314
2315     save_state = SaveDC(graphics->hdc);
2316     EndPath(graphics->hdc);
2317     SelectObject(graphics->hdc, brush->gdibrush);
2318     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2319
2320     transform_and_round_points(graphics, pti, ptf, 4);
2321
2322     Polygon(graphics->hdc, pti, 4);
2323
2324     RestoreDC(graphics->hdc, save_state);
2325
2326     return Ok;
2327 }
2328
2329 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2330     INT count)
2331 {
2332     GpStatus ret;
2333     INT i;
2334
2335     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2336
2337     if(!rects)
2338         return InvalidParameter;
2339
2340     for(i = 0; i < count; i++){
2341         ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2342         if(ret != Ok)   return ret;
2343     }
2344
2345     return Ok;
2346 }
2347
2348 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2349     INT count)
2350 {
2351     GpRectF *rectsF;
2352     GpStatus ret;
2353     INT i;
2354
2355     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
2356
2357     if(!rects || count <= 0)
2358         return InvalidParameter;
2359
2360     rectsF = GdipAlloc(sizeof(GpRectF)*count);
2361     if(!rectsF)
2362         return OutOfMemory;
2363
2364     for(i = 0; i < count; i++){
2365         rectsF[i].X      = (REAL)rects[i].X;
2366         rectsF[i].Y      = (REAL)rects[i].Y;
2367         rectsF[i].X      = (REAL)rects[i].Width;
2368         rectsF[i].Height = (REAL)rects[i].Height;
2369     }
2370
2371     ret = GdipFillRectangles(graphics,brush,rectsF,count);
2372     GdipFree(rectsF);
2373
2374     return ret;
2375 }
2376
2377 /*****************************************************************************
2378  * GdipFillRegion [GDIPLUS.@]
2379  */
2380 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
2381         GpRegion* region)
2382 {
2383     INT save_state;
2384     GpStatus status;
2385     HRGN hrgn;
2386
2387     TRACE("(%p, %p, %p)\n", graphics, brush, region);
2388
2389     if (!(graphics && brush && region))
2390         return InvalidParameter;
2391
2392     if(graphics->busy)
2393         return ObjectBusy;
2394
2395     status = GdipGetRegionHRgn(region, graphics, &hrgn);
2396     if(status != Ok)
2397         return status;
2398
2399     save_state = SaveDC(graphics->hdc);
2400     EndPath(graphics->hdc);
2401     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2402
2403     FillRgn(graphics->hdc, hrgn, brush->gdibrush);
2404
2405     RestoreDC(graphics->hdc, save_state);
2406
2407     DeleteObject(hrgn);
2408
2409     return Ok;
2410 }
2411
2412 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
2413 {
2414     static int calls;
2415
2416     if(!graphics)
2417         return InvalidParameter;
2418
2419     if(graphics->busy)
2420         return ObjectBusy;
2421
2422     if(!(calls++))
2423         FIXME("not implemented\n");
2424
2425     return NotImplemented;
2426 }
2427
2428 /*****************************************************************************
2429  * GdipGetClipBounds [GDIPLUS.@]
2430  */
2431 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
2432 {
2433     TRACE("(%p, %p)\n", graphics, rect);
2434
2435     if(!graphics)
2436         return InvalidParameter;
2437
2438     if(graphics->busy)
2439         return ObjectBusy;
2440
2441     return GdipGetRegionBounds(graphics->clip, graphics, rect);
2442 }
2443
2444 /*****************************************************************************
2445  * GdipGetClipBoundsI [GDIPLUS.@]
2446  */
2447 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
2448 {
2449     TRACE("(%p, %p)\n", graphics, rect);
2450
2451     if(!graphics)
2452         return InvalidParameter;
2453
2454     if(graphics->busy)
2455         return ObjectBusy;
2456
2457     return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
2458 }
2459
2460 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
2461 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
2462     CompositingMode *mode)
2463 {
2464     TRACE("(%p, %p)\n", graphics, mode);
2465
2466     if(!graphics || !mode)
2467         return InvalidParameter;
2468
2469     if(graphics->busy)
2470         return ObjectBusy;
2471
2472     *mode = graphics->compmode;
2473
2474     return Ok;
2475 }
2476
2477 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
2478 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
2479     CompositingQuality *quality)
2480 {
2481     TRACE("(%p, %p)\n", graphics, quality);
2482
2483     if(!graphics || !quality)
2484         return InvalidParameter;
2485
2486     if(graphics->busy)
2487         return ObjectBusy;
2488
2489     *quality = graphics->compqual;
2490
2491     return Ok;
2492 }
2493
2494 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
2495 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
2496     InterpolationMode *mode)
2497 {
2498     TRACE("(%p, %p)\n", graphics, mode);
2499
2500     if(!graphics || !mode)
2501         return InvalidParameter;
2502
2503     if(graphics->busy)
2504         return ObjectBusy;
2505
2506     *mode = graphics->interpolation;
2507
2508     return Ok;
2509 }
2510
2511 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
2512 {
2513     if(!graphics || !argb)
2514         return InvalidParameter;
2515
2516     if(graphics->busy)
2517         return ObjectBusy;
2518
2519     FIXME("(%p, %p): stub\n", graphics, argb);
2520
2521     return NotImplemented;
2522 }
2523
2524 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
2525 {
2526     TRACE("(%p, %p)\n", graphics, scale);
2527
2528     if(!graphics || !scale)
2529         return InvalidParameter;
2530
2531     if(graphics->busy)
2532         return ObjectBusy;
2533
2534     *scale = graphics->scale;
2535
2536     return Ok;
2537 }
2538
2539 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
2540 {
2541     TRACE("(%p, %p)\n", graphics, unit);
2542
2543     if(!graphics || !unit)
2544         return InvalidParameter;
2545
2546     if(graphics->busy)
2547         return ObjectBusy;
2548
2549     *unit = graphics->unit;
2550
2551     return Ok;
2552 }
2553
2554 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
2555 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2556     *mode)
2557 {
2558     TRACE("(%p, %p)\n", graphics, mode);
2559
2560     if(!graphics || !mode)
2561         return InvalidParameter;
2562
2563     if(graphics->busy)
2564         return ObjectBusy;
2565
2566     *mode = graphics->pixeloffset;
2567
2568     return Ok;
2569 }
2570
2571 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
2572 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
2573 {
2574     TRACE("(%p, %p)\n", graphics, mode);
2575
2576     if(!graphics || !mode)
2577         return InvalidParameter;
2578
2579     if(graphics->busy)
2580         return ObjectBusy;
2581
2582     *mode = graphics->smoothing;
2583
2584     return Ok;
2585 }
2586
2587 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
2588 {
2589     TRACE("(%p, %p)\n", graphics, contrast);
2590
2591     if(!graphics || !contrast)
2592         return InvalidParameter;
2593
2594     *contrast = graphics->textcontrast;
2595
2596     return Ok;
2597 }
2598
2599 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
2600 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
2601     TextRenderingHint *hint)
2602 {
2603     TRACE("(%p, %p)\n", graphics, hint);
2604
2605     if(!graphics || !hint)
2606         return InvalidParameter;
2607
2608     if(graphics->busy)
2609         return ObjectBusy;
2610
2611     *hint = graphics->texthint;
2612
2613     return Ok;
2614 }
2615
2616 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2617 {
2618     TRACE("(%p, %p)\n", graphics, matrix);
2619
2620     if(!graphics || !matrix)
2621         return InvalidParameter;
2622
2623     if(graphics->busy)
2624         return ObjectBusy;
2625
2626     *matrix = *graphics->worldtrans;
2627     return Ok;
2628 }
2629
2630 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
2631 {
2632     GpSolidFill *brush;
2633     GpStatus stat;
2634     RECT rect;
2635
2636     TRACE("(%p, %x)\n", graphics, color);
2637
2638     if(!graphics)
2639         return InvalidParameter;
2640
2641     if(graphics->busy)
2642         return ObjectBusy;
2643
2644     if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
2645         return stat;
2646
2647     if(graphics->hwnd){
2648         if(!GetWindowRect(graphics->hwnd, &rect)){
2649             GdipDeleteBrush((GpBrush*)brush);
2650             return GenericError;
2651         }
2652
2653         GdipFillRectangle(graphics, (GpBrush*)brush, 0.0, 0.0, (REAL)(rect.right  - rect.left),
2654                                                                (REAL)(rect.bottom - rect.top));
2655     }
2656     else
2657         GdipFillRectangle(graphics, (GpBrush*)brush, 0.0, 0.0, (REAL)GetDeviceCaps(graphics->hdc, HORZRES),
2658                                                                (REAL)GetDeviceCaps(graphics->hdc, VERTRES));
2659
2660     GdipDeleteBrush((GpBrush*)brush);
2661
2662     return Ok;
2663 }
2664
2665 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
2666 {
2667     TRACE("(%p, %p)\n", graphics, res);
2668
2669     if(!graphics || !res)
2670         return InvalidParameter;
2671
2672     return GdipIsEmptyRegion(graphics->clip, graphics, res);
2673 }
2674
2675 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
2676 {
2677     FIXME("(%p, %.2f, %.2f, %p) stub\n", graphics, x, y, result);
2678
2679     if(!graphics || !result)
2680         return InvalidParameter;
2681
2682     if(graphics->busy)
2683         return ObjectBusy;
2684
2685     return NotImplemented;
2686 }
2687
2688 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
2689 {
2690     FIXME("(%p, %d, %d, %p) stub\n", graphics, x, y, result);
2691
2692     if(!graphics || !result)
2693         return InvalidParameter;
2694
2695     if(graphics->busy)
2696         return ObjectBusy;
2697
2698     return NotImplemented;
2699 }
2700
2701 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
2702         GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
2703         GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
2704         INT regionCount, GpRegion** regions)
2705 {
2706     if (!(graphics && string && font && layoutRect && stringFormat && regions))
2707         return InvalidParameter;
2708
2709     FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
2710             length, font, layoutRect, stringFormat, regionCount, regions);
2711
2712     return NotImplemented;
2713 }
2714
2715 /* Find the smallest rectangle that bounds the text when it is printed in rect
2716  * according to the format options listed in format. If rect has 0 width and
2717  * height, then just find the smallest rectangle that bounds the text when it's
2718  * printed at location (rect->X, rect-Y). */
2719 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
2720     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
2721     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
2722     INT *codepointsfitted, INT *linesfilled)
2723 {
2724     HFONT oldfont;
2725     WCHAR* stringdup;
2726     INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
2727         nheight;
2728     SIZE size;
2729
2730     if(!graphics || !string || !font || !rect)
2731         return InvalidParameter;
2732
2733     if(linesfilled) *linesfilled = 0;
2734     if(codepointsfitted) *codepointsfitted = 0;
2735
2736     if(format)
2737         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2738
2739     if(length == -1) length = lstrlenW(string);
2740
2741     stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
2742     if(!stringdup) return OutOfMemory;
2743
2744     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2745     nwidth = roundr(rect->Width);
2746     nheight = roundr(rect->Height);
2747
2748     if((nwidth == 0) && (nheight == 0))
2749         nwidth = nheight = INT_MAX;
2750
2751     for(i = 0, j = 0; i < length; i++){
2752         if(!isprintW(string[i]) && (string[i] != '\n'))
2753             continue;
2754
2755         stringdup[j] = string[i];
2756         j++;
2757     }
2758
2759     stringdup[j] = 0;
2760     length = j;
2761
2762     while(sum < length){
2763         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2764                               nwidth, &fit, NULL, &size);
2765         fitcpy = fit;
2766
2767         if(fit == 0)
2768             break;
2769
2770         for(lret = 0; lret < fit; lret++)
2771             if(*(stringdup + sum + lret) == '\n')
2772                 break;
2773
2774         /* Line break code (may look strange, but it imitates windows). */
2775         if(lret < fit)
2776             fit = lret;    /* this is not an off-by-one error */
2777         else if(fit < (length - sum)){
2778             if(*(stringdup + sum + fit) == ' ')
2779                 while(*(stringdup + sum + fit) == ' ')
2780                     fit++;
2781             else
2782                 while(*(stringdup + sum + fit - 1) != ' '){
2783                     fit--;
2784
2785                     if(*(stringdup + sum + fit) == '\t')
2786                         break;
2787
2788                     if(fit == 0){
2789                         fit = fitcpy;
2790                         break;
2791                     }
2792                 }
2793         }
2794
2795         GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
2796                               nwidth, &j, NULL, &size);
2797
2798         sum += fit + (lret < fitcpy ? 1 : 0);
2799         if(codepointsfitted) *codepointsfitted = sum;
2800
2801         height += size.cy;
2802         if(linesfilled) *linesfilled += size.cy;
2803         max_width = max(max_width, size.cx);
2804
2805         if(height > nheight)
2806             break;
2807
2808         /* Stop if this was a linewrap (but not if it was a linebreak). */
2809         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2810             break;
2811     }
2812
2813     bounds->X = rect->X;
2814     bounds->Y = rect->Y;
2815     bounds->Width = (REAL)max_width;
2816     bounds->Height = (REAL) min(height, nheight);
2817
2818     GdipFree(stringdup);
2819     DeleteObject(SelectObject(graphics->hdc, oldfont));
2820
2821     return Ok;
2822 }
2823
2824 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
2825 {
2826     TRACE("(%p)\n", graphics);
2827
2828     if(!graphics)
2829         return InvalidParameter;
2830
2831     if(graphics->busy)
2832         return ObjectBusy;
2833
2834     return GdipSetInfinite(graphics->clip);
2835 }
2836
2837 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
2838 {
2839     TRACE("(%p)\n", graphics);
2840
2841     if(!graphics)
2842         return InvalidParameter;
2843
2844     if(graphics->busy)
2845         return ObjectBusy;
2846
2847     graphics->worldtrans->matrix[0] = 1.0;
2848     graphics->worldtrans->matrix[1] = 0.0;
2849     graphics->worldtrans->matrix[2] = 0.0;
2850     graphics->worldtrans->matrix[3] = 1.0;
2851     graphics->worldtrans->matrix[4] = 0.0;
2852     graphics->worldtrans->matrix[5] = 0.0;
2853
2854     return Ok;
2855 }
2856
2857 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
2858 {
2859     static int calls;
2860
2861     if(!graphics)
2862         return InvalidParameter;
2863
2864     if(!(calls++))
2865         FIXME("graphics state not implemented\n");
2866
2867     return Ok;
2868 }
2869
2870 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
2871     GpMatrixOrder order)
2872 {
2873     TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
2874
2875     if(!graphics)
2876         return InvalidParameter;
2877
2878     if(graphics->busy)
2879         return ObjectBusy;
2880
2881     return GdipRotateMatrix(graphics->worldtrans, angle, order);
2882 }
2883
2884 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
2885 {
2886     static int calls;
2887
2888     if(!graphics || !state)
2889         return InvalidParameter;
2890
2891     if(!(calls++))
2892         FIXME("graphics state not implemented\n");
2893
2894     *state = 0xdeadbeef;
2895     return Ok;
2896 }
2897
2898 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics, GraphicsContainer *state)
2899 {
2900     FIXME("(%p, %p)\n", graphics, state);
2901
2902     if(!graphics || !state)
2903         return InvalidParameter;
2904
2905     *state = 0xdeadbeef;
2906     return Ok;
2907 }
2908
2909 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsState state)
2910 {
2911     FIXME("(%p, 0x%x)\n", graphics, state);
2912
2913     if(!graphics || !state)
2914         return InvalidParameter;
2915
2916     return Ok;
2917 }
2918
2919 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
2920     REAL sy, GpMatrixOrder order)
2921 {
2922     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
2923
2924     if(!graphics)
2925         return InvalidParameter;
2926
2927     if(graphics->busy)
2928         return ObjectBusy;
2929
2930     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
2931 }
2932
2933 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
2934     CombineMode mode)
2935 {
2936     TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
2937
2938     if(!graphics || !srcgraphics)
2939         return InvalidParameter;
2940
2941     return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
2942 }
2943
2944 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
2945     CompositingMode mode)
2946 {
2947     TRACE("(%p, %d)\n", graphics, mode);
2948
2949     if(!graphics)
2950         return InvalidParameter;
2951
2952     if(graphics->busy)
2953         return ObjectBusy;
2954
2955     graphics->compmode = mode;
2956
2957     return Ok;
2958 }
2959
2960 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
2961     CompositingQuality quality)
2962 {
2963     TRACE("(%p, %d)\n", graphics, quality);
2964
2965     if(!graphics)
2966         return InvalidParameter;
2967
2968     if(graphics->busy)
2969         return ObjectBusy;
2970
2971     graphics->compqual = quality;
2972
2973     return Ok;
2974 }
2975
2976 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
2977     InterpolationMode mode)
2978 {
2979     TRACE("(%p, %d)\n", graphics, mode);
2980
2981     if(!graphics)
2982         return InvalidParameter;
2983
2984     if(graphics->busy)
2985         return ObjectBusy;
2986
2987     graphics->interpolation = mode;
2988
2989     return Ok;
2990 }
2991
2992 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
2993 {
2994     TRACE("(%p, %.2f)\n", graphics, scale);
2995
2996     if(!graphics || (scale <= 0.0))
2997         return InvalidParameter;
2998
2999     if(graphics->busy)
3000         return ObjectBusy;
3001
3002     graphics->scale = scale;
3003
3004     return Ok;
3005 }
3006
3007 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
3008 {
3009     TRACE("(%p, %d)\n", graphics, unit);
3010
3011     if(!graphics)
3012         return InvalidParameter;
3013
3014     if(graphics->busy)
3015         return ObjectBusy;
3016
3017     if(unit == UnitWorld)
3018         return InvalidParameter;
3019
3020     graphics->unit = unit;
3021
3022     return Ok;
3023 }
3024
3025 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3026     mode)
3027 {
3028     TRACE("(%p, %d)\n", graphics, mode);
3029
3030     if(!graphics)
3031         return InvalidParameter;
3032
3033     if(graphics->busy)
3034         return ObjectBusy;
3035
3036     graphics->pixeloffset = mode;
3037
3038     return Ok;
3039 }
3040
3041 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
3042 {
3043     TRACE("(%p, %d)\n", graphics, mode);
3044
3045     if(!graphics)
3046         return InvalidParameter;
3047
3048     if(graphics->busy)
3049         return ObjectBusy;
3050
3051     graphics->smoothing = mode;
3052
3053     return Ok;
3054 }
3055
3056 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
3057 {
3058     TRACE("(%p, %d)\n", graphics, contrast);
3059
3060     if(!graphics)
3061         return InvalidParameter;
3062
3063     graphics->textcontrast = contrast;
3064
3065     return Ok;
3066 }
3067
3068 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
3069     TextRenderingHint hint)
3070 {
3071     TRACE("(%p, %d)\n", graphics, hint);
3072
3073     if(!graphics)
3074         return InvalidParameter;
3075
3076     if(graphics->busy)
3077         return ObjectBusy;
3078
3079     graphics->texthint = hint;
3080
3081     return Ok;
3082 }
3083
3084 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3085 {
3086     TRACE("(%p, %p)\n", graphics, matrix);
3087
3088     if(!graphics || !matrix)
3089         return InvalidParameter;
3090
3091     if(graphics->busy)
3092         return ObjectBusy;
3093
3094     GdipDeleteMatrix(graphics->worldtrans);
3095     return GdipCloneMatrix(matrix, &graphics->worldtrans);
3096 }
3097
3098 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
3099     REAL dy, GpMatrixOrder order)
3100 {
3101     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
3102
3103     if(!graphics)
3104         return InvalidParameter;
3105
3106     if(graphics->busy)
3107         return ObjectBusy;
3108
3109     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
3110 }
3111
3112 /*****************************************************************************
3113  * GdipSetClipHrgn [GDIPLUS.@]
3114  */
3115 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
3116 {
3117     GpRegion *region;
3118     GpStatus status;
3119
3120     TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
3121
3122     if(!graphics)
3123         return InvalidParameter;
3124
3125     status = GdipCreateRegionHrgn(hrgn, &region);
3126     if(status != Ok)
3127         return status;
3128
3129     status = GdipSetClipRegion(graphics, region, mode);
3130
3131     GdipDeleteRegion(region);
3132     return status;
3133 }
3134
3135 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
3136 {
3137     TRACE("(%p, %p, %d)\n", graphics, path, mode);
3138
3139     if(!graphics)
3140         return InvalidParameter;
3141
3142     if(graphics->busy)
3143         return ObjectBusy;
3144
3145     return GdipCombineRegionPath(graphics->clip, path, mode);
3146 }
3147
3148 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
3149                                     REAL width, REAL height,
3150                                     CombineMode mode)
3151 {
3152     GpRectF rect;
3153
3154     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
3155
3156     if(!graphics)
3157         return InvalidParameter;
3158
3159     if(graphics->busy)
3160         return ObjectBusy;
3161
3162     rect.X = x;
3163     rect.Y = y;
3164     rect.Width  = width;
3165     rect.Height = height;
3166
3167     return GdipCombineRegionRect(graphics->clip, &rect, mode);
3168 }
3169
3170 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
3171                                      INT width, INT height,
3172                                      CombineMode mode)
3173 {
3174     TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
3175
3176     if(!graphics)
3177         return InvalidParameter;
3178
3179     if(graphics->busy)
3180         return ObjectBusy;
3181
3182     return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
3183 }
3184
3185 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
3186                                       CombineMode mode)
3187 {
3188     TRACE("(%p, %p, %d)\n", graphics, region, mode);
3189
3190     if(!graphics || !region)
3191         return InvalidParameter;
3192
3193     if(graphics->busy)
3194         return ObjectBusy;
3195
3196     return GdipCombineRegionRegion(graphics->clip, region, mode);
3197 }
3198
3199 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
3200     UINT limitDpi)
3201 {
3202     static int calls;
3203
3204     if(!(calls++))
3205         FIXME("not implemented\n");
3206
3207     return NotImplemented;
3208 }
3209
3210 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
3211     INT count)
3212 {
3213     INT save_state;
3214     POINT *pti;
3215
3216     TRACE("(%p, %p, %d)\n", graphics, points, count);
3217
3218     if(!graphics || !pen || count<=0)
3219         return InvalidParameter;
3220
3221     if(graphics->busy)
3222         return ObjectBusy;
3223
3224     pti = GdipAlloc(sizeof(POINT) * count);
3225
3226     save_state = prepare_dc(graphics, pen);
3227     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
3228
3229     transform_and_round_points(graphics, pti, (GpPointF*)points, count);
3230     Polygon(graphics->hdc, pti, count);
3231
3232     restore_dc(graphics, save_state);
3233     GdipFree(pti);
3234
3235     return Ok;
3236 }
3237
3238 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
3239     INT count)
3240 {
3241     GpStatus ret;
3242     GpPointF *ptf;
3243     INT i;
3244
3245     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
3246
3247     if(count<=0)    return InvalidParameter;
3248     ptf = GdipAlloc(sizeof(GpPointF) * count);
3249
3250     for(i = 0;i < count; i++){
3251         ptf[i].X = (REAL)points[i].X;
3252         ptf[i].Y = (REAL)points[i].Y;
3253     }
3254
3255     ret = GdipDrawPolygon(graphics,pen,ptf,count);
3256     GdipFree(ptf);
3257
3258     return ret;
3259 }
3260
3261 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
3262 {
3263     TRACE("(%p, %p)\n", graphics, dpi);
3264
3265     if(!graphics || !dpi)
3266         return InvalidParameter;
3267
3268     if(graphics->busy)
3269         return ObjectBusy;
3270
3271     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
3272
3273     return Ok;
3274 }
3275
3276 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
3277 {
3278     TRACE("(%p, %p)\n", graphics, dpi);
3279
3280     if(!graphics || !dpi)
3281         return InvalidParameter;
3282
3283     if(graphics->busy)
3284         return ObjectBusy;
3285
3286     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
3287
3288     return Ok;
3289 }
3290
3291 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
3292     GpMatrixOrder order)
3293 {
3294     GpMatrix m;
3295     GpStatus ret;
3296
3297     TRACE("(%p, %p, %d)\n", graphics, matrix, order);
3298
3299     if(!graphics || !matrix)
3300         return InvalidParameter;
3301
3302     if(graphics->busy)
3303         return ObjectBusy;
3304
3305     m = *(graphics->worldtrans);
3306
3307     ret = GdipMultiplyMatrix(&m, matrix, order);
3308     if(ret == Ok)
3309         *(graphics->worldtrans) = m;
3310
3311     return ret;
3312 }
3313
3314 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
3315 {
3316     TRACE("(%p, %p)\n", graphics, hdc);
3317
3318     if(!graphics || !hdc)
3319         return InvalidParameter;
3320
3321     if(graphics->busy)
3322         return ObjectBusy;
3323
3324     *hdc = graphics->hdc;
3325     graphics->busy = TRUE;
3326
3327     return Ok;
3328 }
3329
3330 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
3331 {
3332     TRACE("(%p, %p)\n", graphics, hdc);
3333
3334     if(!graphics)
3335         return InvalidParameter;
3336
3337     if(graphics->hdc != hdc || !(graphics->busy))
3338         return InvalidParameter;
3339
3340     graphics->busy = FALSE;
3341
3342     return Ok;
3343 }
3344
3345 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
3346 {
3347     GpRegion *clip;
3348     GpStatus status;
3349
3350     TRACE("(%p, %p)\n", graphics, region);
3351
3352     if(!graphics || !region)
3353         return InvalidParameter;
3354
3355     if(graphics->busy)
3356         return ObjectBusy;
3357
3358     if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
3359         return status;
3360
3361     /* free everything except root node and header */
3362     delete_element(&region->node);
3363     memcpy(region, clip, sizeof(GpRegion));
3364
3365     return Ok;
3366 }
3367
3368 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
3369                                         GpCoordinateSpace src_space, GpPointF *points, INT count)
3370 {
3371     if(!graphics || !points || count <= 0)
3372         return InvalidParameter;
3373
3374     if(graphics->busy)
3375         return ObjectBusy;
3376
3377     FIXME("(%p, %d, %d, %p, %d): stub\n", graphics, dst_space, src_space, points, count);
3378
3379     return NotImplemented;
3380 }
3381
3382 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
3383                                          GpCoordinateSpace src_space, GpPoint *points, INT count)
3384 {
3385     FIXME("(%p, %d, %d, %p, %d): stub\n", graphics, dst_space, src_space, points, count);
3386
3387     return NotImplemented;
3388 }
3389
3390 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
3391 {
3392     FIXME("\n");
3393
3394     return NULL;
3395 }
3396
3397 /*****************************************************************************
3398  * GdipTranslateClip [GDIPLUS.@]
3399  */
3400 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
3401 {
3402     TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
3403
3404     if(!graphics)
3405         return InvalidParameter;
3406
3407     if(graphics->busy)
3408         return ObjectBusy;
3409
3410     return GdipTranslateRegion(graphics->clip, dx, dy);
3411 }
3412
3413 /*****************************************************************************
3414  * GdipTranslateClipI [GDIPLUS.@]
3415  */
3416 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
3417 {
3418     TRACE("(%p, %d, %d)\n", graphics, dx, dy);
3419
3420     if(!graphics)
3421         return InvalidParameter;
3422
3423     if(graphics->busy)
3424         return ObjectBusy;
3425
3426     return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
3427 }