gdiplus: GdipDeleteGraphics affected by busy state too. Test added.
[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 WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
719 {
720     return GdipCreateFromHDC2(hdc, NULL, graphics);
721 }
722
723 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
724 {
725     GpStatus retval;
726
727     if(hDevice != NULL) {
728         FIXME("Don't know how to hadle parameter hDevice\n");
729         return NotImplemented;
730     }
731
732     if(hdc == NULL)
733         return OutOfMemory;
734
735     if(graphics == NULL)
736         return InvalidParameter;
737
738     *graphics = GdipAlloc(sizeof(GpGraphics));
739     if(!*graphics)  return OutOfMemory;
740
741     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
742         GdipFree(*graphics);
743         return retval;
744     }
745
746     (*graphics)->hdc = hdc;
747     (*graphics)->hwnd = NULL;
748     (*graphics)->smoothing = SmoothingModeDefault;
749     (*graphics)->compqual = CompositingQualityDefault;
750     (*graphics)->interpolation = InterpolationModeDefault;
751     (*graphics)->pixeloffset = PixelOffsetModeDefault;
752     (*graphics)->compmode = CompositingModeSourceOver;
753     (*graphics)->unit = UnitDisplay;
754     (*graphics)->scale = 1.0;
755     (*graphics)->busy = FALSE;
756
757     return Ok;
758 }
759
760 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
761 {
762     GpStatus ret;
763
764     if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
765         return ret;
766
767     (*graphics)->hwnd = hwnd;
768
769     return Ok;
770 }
771
772 /* FIXME: no icm handling */
773 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
774 {
775     return GdipCreateFromHWND(hwnd, graphics);
776 }
777
778 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
779     GpMetafile **metafile)
780 {
781     static int calls;
782
783     if(!hemf || !metafile)
784         return InvalidParameter;
785
786     if(!(calls++))
787         FIXME("not implemented\n");
788
789     return NotImplemented;
790 }
791
792 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
793     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
794 {
795     IStream *stream = NULL;
796     UINT read;
797     BYTE* copy;
798     HENHMETAFILE hemf;
799     GpStatus retval = GenericError;
800
801     if(!hwmf || !metafile || !placeable)
802         return InvalidParameter;
803
804     *metafile = NULL;
805     read = GetMetaFileBitsEx(hwmf, 0, NULL);
806     if(!read)
807         return GenericError;
808     copy = GdipAlloc(read);
809     GetMetaFileBitsEx(hwmf, read, copy);
810
811     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
812     GdipFree(copy);
813
814     read = GetEnhMetaFileBits(hemf, 0, NULL);
815     copy = GdipAlloc(read);
816     GetEnhMetaFileBits(hemf, read, copy);
817     DeleteEnhMetaFile(hemf);
818
819     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
820         ERR("could not make stream\n");
821         GdipFree(copy);
822         goto err;
823     }
824
825     *metafile = GdipAlloc(sizeof(GpMetafile));
826     if(!*metafile){
827         retval = OutOfMemory;
828         goto err;
829     }
830
831     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
832         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
833         goto err;
834
835
836     (*metafile)->image.type = ImageTypeMetafile;
837     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
838     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
839     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
840                     - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
841     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
842                    - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
843     (*metafile)->unit = UnitInch;
844
845     if(delete)
846         DeleteMetaFile(hwmf);
847
848     return Ok;
849
850 err:
851     GdipFree(*metafile);
852     IStream_Release(stream);
853     return retval;
854 }
855
856 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
857     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
858 {
859     HMETAFILE hmf = GetMetaFileW(file);
860
861     TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
862
863     if(!hmf) return InvalidParameter;
864
865     return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
866 }
867
868 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
869     UINT access, IStream **stream)
870 {
871     DWORD dwMode;
872     HRESULT ret;
873
874     if(!stream || !filename)
875         return InvalidParameter;
876
877     if(access & GENERIC_WRITE)
878         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
879     else if(access & GENERIC_READ)
880         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
881     else
882         return InvalidParameter;
883
884     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
885
886     return hresult_to_status(ret);
887 }
888
889 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
890 {
891     if(!graphics) return InvalidParameter;
892     if(graphics->busy) return ObjectBusy;
893
894     if(graphics->hwnd)
895         ReleaseDC(graphics->hwnd, graphics->hdc);
896
897     GdipDeleteMatrix(graphics->worldtrans);
898     GdipFree(graphics);
899
900     return Ok;
901 }
902
903 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
904     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
905 {
906     INT save_state, num_pts;
907     GpPointF points[MAX_ARC_PTS];
908     GpStatus retval;
909
910     if(!graphics || !pen || width <= 0 || height <= 0)
911         return InvalidParameter;
912
913     if(graphics->busy)
914         return ObjectBusy;
915
916     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
917
918     save_state = prepare_dc(graphics, pen);
919
920     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
921
922     restore_dc(graphics, save_state);
923
924     return retval;
925 }
926
927 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
928     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
929 {
930     return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
931 }
932
933 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
934     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
935 {
936     INT save_state;
937     GpPointF pt[4];
938     GpStatus retval;
939
940     if(!graphics || !pen)
941         return InvalidParameter;
942
943     if(graphics->busy)
944         return ObjectBusy;
945
946     pt[0].X = x1;
947     pt[0].Y = y1;
948     pt[1].X = x2;
949     pt[1].Y = y2;
950     pt[2].X = x3;
951     pt[2].Y = y3;
952     pt[3].X = x4;
953     pt[3].Y = y4;
954
955     save_state = prepare_dc(graphics, pen);
956
957     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
958
959     restore_dc(graphics, save_state);
960
961     return retval;
962 }
963
964 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
965     INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
966 {
967     INT save_state;
968     GpPointF pt[4];
969     GpStatus retval;
970
971     if(!graphics || !pen)
972         return InvalidParameter;
973
974     if(graphics->busy)
975         return ObjectBusy;
976
977     pt[0].X = x1;
978     pt[0].Y = y1;
979     pt[1].X = x2;
980     pt[1].Y = y2;
981     pt[2].X = x3;
982     pt[2].Y = y3;
983     pt[3].X = x4;
984     pt[3].Y = y4;
985
986     save_state = prepare_dc(graphics, pen);
987
988     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
989
990     restore_dc(graphics, save_state);
991
992     return retval;
993 }
994
995 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
996     GDIPCONST GpPointF *points, INT count)
997 {
998     INT i;
999     GpStatus ret;
1000
1001     if(!graphics || !pen || !points || (count <= 0))
1002         return InvalidParameter;
1003
1004     if(graphics->busy)
1005         return ObjectBusy;
1006
1007     for(i = 0; i < floor(count / 4); i++){
1008         ret = GdipDrawBezier(graphics, pen,
1009                              points[4*i].X, points[4*i].Y,
1010                              points[4*i + 1].X, points[4*i + 1].Y,
1011                              points[4*i + 2].X, points[4*i + 2].Y,
1012                              points[4*i + 3].X, points[4*i + 3].Y);
1013         if(ret != Ok)
1014             return ret;
1015     }
1016
1017     return Ok;
1018 }
1019
1020 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1021     GDIPCONST GpPoint *points, INT count)
1022 {
1023     GpPointF *pts;
1024     GpStatus ret;
1025     INT i;
1026
1027     if(!graphics || !pen || !points || (count <= 0))
1028         return InvalidParameter;
1029
1030     if(graphics->busy)
1031         return ObjectBusy;
1032
1033     pts = GdipAlloc(sizeof(GpPointF) * count);
1034     if(!pts)
1035         return OutOfMemory;
1036
1037     for(i = 0; i < count; i++){
1038         pts[i].X = (REAL)points[i].X;
1039         pts[i].Y = (REAL)points[i].Y;
1040     }
1041
1042     ret = GdipDrawBeziers(graphics,pen,pts,count);
1043
1044     GdipFree(pts);
1045
1046     return ret;
1047 }
1048
1049 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1050     GDIPCONST GpPointF *points, INT count)
1051 {
1052     return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1053 }
1054
1055 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1056     GDIPCONST GpPoint *points, INT count)
1057 {
1058     return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1059 }
1060
1061 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1062     GDIPCONST GpPointF *points, INT count, REAL tension)
1063 {
1064     GpPointF *ptf;
1065     GpStatus stat;
1066
1067     if(!graphics || !pen || !points || count <= 0)
1068         return InvalidParameter;
1069
1070     if(graphics->busy)
1071         return ObjectBusy;
1072
1073     /* make a full points copy.. */
1074     ptf = GdipAlloc(sizeof(GpPointF)*(count+1));
1075     if(!ptf)
1076         return OutOfMemory;
1077     memcpy(ptf, points, sizeof(GpPointF)*count);
1078
1079     /* ..and add a first point as a last one */
1080     ptf[count] = ptf[0];
1081
1082     stat = GdipDrawCurve2(graphics, pen, ptf, count + 1, tension);
1083
1084     GdipFree(ptf);
1085
1086     return stat;
1087 }
1088
1089 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1090     GDIPCONST GpPoint *points, INT count, REAL tension)
1091 {
1092     GpPointF *ptf;
1093     GpStatus stat;
1094     INT i;
1095
1096     if(!points || count <= 0)
1097         return InvalidParameter;
1098
1099     ptf = GdipAlloc(sizeof(GpPointF)*count);
1100     if(!ptf)
1101         return OutOfMemory;
1102
1103     for(i = 0; i < count; i++){
1104         ptf[i].X = (REAL)points[i].X;
1105         ptf[i].Y = (REAL)points[i].Y;
1106     }
1107
1108     stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1109
1110     GdipFree(ptf);
1111
1112     return stat;
1113 }
1114
1115 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1116     GDIPCONST GpPointF *points, INT count)
1117 {
1118     return GdipDrawCurve2(graphics,pen,points,count,1.0);
1119 }
1120
1121 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1122     GDIPCONST GpPoint *points, INT count)
1123 {
1124     GpPointF *pointsF;
1125     GpStatus ret;
1126     INT i;
1127
1128     if(!points || count <= 0)
1129         return InvalidParameter;
1130
1131     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1132     if(!pointsF)
1133         return OutOfMemory;
1134
1135     for(i = 0; i < count; i++){
1136         pointsF[i].X = (REAL)points[i].X;
1137         pointsF[i].Y = (REAL)points[i].Y;
1138     }
1139
1140     ret = GdipDrawCurve(graphics,pen,pointsF,count);
1141     GdipFree(pointsF);
1142
1143     return ret;
1144 }
1145
1146 /* Approximates cardinal spline with Bezier curves. */
1147 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1148     GDIPCONST GpPointF *points, INT count, REAL tension)
1149 {
1150     /* PolyBezier expects count*3-2 points. */
1151     INT i, len_pt = count*3-2, save_state;
1152     GpPointF *pt;
1153     REAL x1, x2, y1, y2;
1154     GpStatus retval;
1155
1156     if(!graphics || !pen)
1157         return InvalidParameter;
1158
1159     if(graphics->busy)
1160         return ObjectBusy;
1161
1162     pt = GdipAlloc(len_pt * sizeof(GpPointF));
1163     tension = tension * TENSION_CONST;
1164
1165     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1166         tension, &x1, &y1);
1167
1168     pt[0].X = points[0].X;
1169     pt[0].Y = points[0].Y;
1170     pt[1].X = x1;
1171     pt[1].Y = y1;
1172
1173     for(i = 0; i < count-2; i++){
1174         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1175
1176         pt[3*i+2].X = x1;
1177         pt[3*i+2].Y = y1;
1178         pt[3*i+3].X = points[i+1].X;
1179         pt[3*i+3].Y = points[i+1].Y;
1180         pt[3*i+4].X = x2;
1181         pt[3*i+4].Y = y2;
1182     }
1183
1184     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1185         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1186
1187     pt[len_pt-2].X = x1;
1188     pt[len_pt-2].Y = y1;
1189     pt[len_pt-1].X = points[count-1].X;
1190     pt[len_pt-1].Y = points[count-1].Y;
1191
1192     save_state = prepare_dc(graphics, pen);
1193
1194     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1195
1196     GdipFree(pt);
1197     restore_dc(graphics, save_state);
1198
1199     return retval;
1200 }
1201
1202 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1203     GDIPCONST GpPoint *points, INT count, REAL tension)
1204 {
1205     GpPointF *pointsF;
1206     GpStatus ret;
1207     INT i;
1208
1209     if(!points || count <= 0)
1210         return InvalidParameter;
1211
1212     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1213     if(!pointsF)
1214         return OutOfMemory;
1215
1216     for(i = 0; i < count; i++){
1217         pointsF[i].X = (REAL)points[i].X;
1218         pointsF[i].Y = (REAL)points[i].Y;
1219     }
1220
1221     ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1222     GdipFree(pointsF);
1223
1224     return ret;
1225 }
1226
1227 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1228     REAL y, REAL width, REAL height)
1229 {
1230     INT save_state;
1231     GpPointF ptf[2];
1232     POINT pti[2];
1233
1234     if(!graphics || !pen)
1235         return InvalidParameter;
1236
1237     if(graphics->busy)
1238         return ObjectBusy;
1239
1240     ptf[0].X = x;
1241     ptf[0].Y = y;
1242     ptf[1].X = x + width;
1243     ptf[1].Y = y + height;
1244
1245     save_state = prepare_dc(graphics, pen);
1246     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1247
1248     transform_and_round_points(graphics, pti, ptf, 2);
1249
1250     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1251
1252     restore_dc(graphics, save_state);
1253
1254     return Ok;
1255 }
1256
1257 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1258     INT y, INT width, INT height)
1259 {
1260     return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1261 }
1262
1263
1264 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1265 {
1266     /* IPicture::Render uses LONG coords */
1267     return GdipDrawImageI(graphics,image,roundr(x),roundr(y));
1268 }
1269
1270 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1271     INT y)
1272 {
1273     UINT width, height, srcw, srch;
1274
1275     if(!graphics || !image)
1276         return InvalidParameter;
1277
1278     GdipGetImageWidth(image, &width);
1279     GdipGetImageHeight(image, &height);
1280
1281     srcw = width * (((REAL) INCH_HIMETRIC) /
1282             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1283     srch = height * (((REAL) INCH_HIMETRIC) /
1284             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1285
1286     if(image->type != ImageTypeMetafile){
1287         y += height;
1288         height *= -1;
1289     }
1290
1291     IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1292                     0, 0, srcw, srch, NULL);
1293
1294     return Ok;
1295 }
1296
1297 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1298 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1299      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1300      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1301      DrawImageAbort callback, VOID * callbackData)
1302 {
1303     GpPointF ptf[3];
1304     POINT pti[3];
1305     REAL dx, dy;
1306
1307     TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1308           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1309           callbackData);
1310
1311     if(!graphics || !image || !points || count != 3)
1312          return InvalidParameter;
1313
1314     if(srcUnit == UnitInch)
1315         dx = dy = (REAL) INCH_HIMETRIC;
1316     else if(srcUnit == UnitPixel){
1317         dx = ((REAL) INCH_HIMETRIC) /
1318              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1319         dy = ((REAL) INCH_HIMETRIC) /
1320              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1321     }
1322     else
1323         return NotImplemented;
1324
1325     memcpy(ptf, points, 3 * sizeof(GpPointF));
1326     transform_and_round_points(graphics, pti, ptf, 3);
1327
1328     /* IPicture renders bitmaps with the y-axis reversed
1329      * FIXME: flipping for unknown image type might not be correct. */
1330     if(image->type != ImageTypeMetafile){
1331         INT temp;
1332         temp = pti[0].y;
1333         pti[0].y = pti[2].y;
1334         pti[2].y = temp;
1335     }
1336
1337     if(IPicture_Render(image->picture, graphics->hdc,
1338         pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1339         srcx * dx, srcy * dy,
1340         srcwidth * dx, srcheight * dy,
1341         NULL) != S_OK){
1342         if(callback)
1343             callback(callbackData);
1344         return GenericError;
1345     }
1346
1347     return Ok;
1348 }
1349
1350 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1351      GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1352      INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1353      DrawImageAbort callback, VOID * callbackData)
1354 {
1355     GpPointF pointsF[3];
1356     INT i;
1357
1358     if(!points || count!=3)
1359         return InvalidParameter;
1360
1361     for(i = 0; i < count; i++){
1362         pointsF[i].X = (REAL)points[i].X;
1363         pointsF[i].Y = (REAL)points[i].Y;
1364     }
1365
1366     return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1367                                    (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1368                                    callback, callbackData);
1369 }
1370
1371 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1372     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1373     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1374     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1375     VOID * callbackData)
1376 {
1377     GpPointF points[3];
1378
1379     points[0].X = dstx;
1380     points[0].Y = dsty;
1381     points[1].X = dstx + dstwidth;
1382     points[1].Y = dsty;
1383     points[2].X = dstx;
1384     points[2].Y = dsty + dstheight;
1385
1386     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1387                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1388 }
1389
1390 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1391         INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1392         INT srcwidth, INT srcheight, GpUnit srcUnit,
1393         GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1394         VOID * callbackData)
1395 {
1396    GpPointF points[3];
1397
1398     points[0].X = dstx;
1399     points[0].Y = dsty;
1400     points[1].X = dstx + dstwidth;
1401     points[1].Y = dsty;
1402     points[2].X = dstx;
1403     points[2].Y = dsty + dstheight;
1404
1405     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1406                srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1407 }
1408
1409 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1410     REAL x, REAL y, REAL width, REAL height)
1411 {
1412     RectF bounds;
1413     GpUnit unit;
1414     GpStatus ret;
1415
1416     if(!graphics || !image)
1417         return InvalidParameter;
1418
1419     ret = GdipGetImageBounds(image, &bounds, &unit);
1420     if(ret != Ok)
1421         return ret;
1422
1423     return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1424                                  bounds.X, bounds.Y, bounds.Width, bounds.Height,
1425                                  unit, NULL, NULL, NULL);
1426 }
1427
1428 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1429     INT x, INT y, INT width, INT height)
1430 {
1431     return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1432 }
1433
1434 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1435     REAL y1, REAL x2, REAL y2)
1436 {
1437     INT save_state;
1438     GpPointF pt[2];
1439     GpStatus retval;
1440
1441     if(!pen || !graphics)
1442         return InvalidParameter;
1443
1444     if(graphics->busy)
1445         return ObjectBusy;
1446
1447     pt[0].X = x1;
1448     pt[0].Y = y1;
1449     pt[1].X = x2;
1450     pt[1].Y = y2;
1451
1452     save_state = prepare_dc(graphics, pen);
1453
1454     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1455
1456     restore_dc(graphics, save_state);
1457
1458     return retval;
1459 }
1460
1461 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1462     INT y1, INT x2, INT y2)
1463 {
1464     INT save_state;
1465     GpPointF pt[2];
1466     GpStatus retval;
1467
1468     if(!pen || !graphics)
1469         return InvalidParameter;
1470
1471     if(graphics->busy)
1472         return ObjectBusy;
1473
1474     pt[0].X = (REAL)x1;
1475     pt[0].Y = (REAL)y1;
1476     pt[1].X = (REAL)x2;
1477     pt[1].Y = (REAL)y2;
1478
1479     save_state = prepare_dc(graphics, pen);
1480
1481     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1482
1483     restore_dc(graphics, save_state);
1484
1485     return retval;
1486 }
1487
1488 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1489     GpPointF *points, INT count)
1490 {
1491     INT save_state;
1492     GpStatus retval;
1493
1494     if(!pen || !graphics || (count < 2))
1495         return InvalidParameter;
1496
1497     if(graphics->busy)
1498         return ObjectBusy;
1499
1500     save_state = prepare_dc(graphics, pen);
1501
1502     retval = draw_polyline(graphics, pen, points, count, TRUE);
1503
1504     restore_dc(graphics, save_state);
1505
1506     return retval;
1507 }
1508
1509 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
1510     GpPoint *points, INT count)
1511 {
1512     INT save_state;
1513     GpStatus retval;
1514     GpPointF *ptf = NULL;
1515     int i;
1516
1517     if(!pen || !graphics || (count < 2))
1518         return InvalidParameter;
1519
1520     if(graphics->busy)
1521         return ObjectBusy;
1522
1523     ptf = GdipAlloc(count * sizeof(GpPointF));
1524     if(!ptf) return OutOfMemory;
1525
1526     for(i = 0; i < count; i ++){
1527         ptf[i].X = (REAL) points[i].X;
1528         ptf[i].Y = (REAL) points[i].Y;
1529     }
1530
1531     save_state = prepare_dc(graphics, pen);
1532
1533     retval = draw_polyline(graphics, pen, ptf, count, TRUE);
1534
1535     restore_dc(graphics, save_state);
1536
1537     GdipFree(ptf);
1538     return retval;
1539 }
1540
1541 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1542 {
1543     INT save_state;
1544     GpStatus retval;
1545
1546     if(!pen || !graphics)
1547         return InvalidParameter;
1548
1549     if(graphics->busy)
1550         return ObjectBusy;
1551
1552     save_state = prepare_dc(graphics, pen);
1553
1554     retval = draw_poly(graphics, pen, path->pathdata.Points,
1555                        path->pathdata.Types, path->pathdata.Count, TRUE);
1556
1557     restore_dc(graphics, save_state);
1558
1559     return retval;
1560 }
1561
1562 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1563     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1564 {
1565     INT save_state;
1566
1567     if(!graphics || !pen)
1568         return InvalidParameter;
1569
1570     if(graphics->busy)
1571         return ObjectBusy;
1572
1573     save_state = prepare_dc(graphics, pen);
1574     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1575
1576     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1577
1578     restore_dc(graphics, save_state);
1579
1580     return Ok;
1581 }
1582
1583 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
1584     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1585 {
1586     return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1587 }
1588
1589 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
1590     REAL y, REAL width, REAL height)
1591 {
1592     INT save_state;
1593     GpPointF ptf[4];
1594     POINT pti[4];
1595
1596     if(!pen || !graphics)
1597         return InvalidParameter;
1598
1599     if(graphics->busy)
1600         return ObjectBusy;
1601
1602     ptf[0].X = x;
1603     ptf[0].Y = y;
1604     ptf[1].X = x + width;
1605     ptf[1].Y = y;
1606     ptf[2].X = x + width;
1607     ptf[2].Y = y + height;
1608     ptf[3].X = x;
1609     ptf[3].Y = y + height;
1610
1611     save_state = prepare_dc(graphics, pen);
1612     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1613
1614     transform_and_round_points(graphics, pti, ptf, 4);
1615     Polygon(graphics->hdc, pti, 4);
1616
1617     restore_dc(graphics, save_state);
1618
1619     return Ok;
1620 }
1621
1622 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1623     INT y, INT width, INT height)
1624 {
1625     return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1626 }
1627
1628 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1629     GDIPCONST GpRectF* rects, INT count)
1630 {
1631     GpPointF *ptf;
1632     POINT *pti;
1633     INT save_state, i;
1634
1635     if(!graphics || !pen || !rects || count < 1)
1636         return InvalidParameter;
1637
1638     if(graphics->busy)
1639         return ObjectBusy;
1640
1641     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1642     pti = GdipAlloc(4 * count * sizeof(POINT));
1643
1644     if(!ptf || !pti){
1645         GdipFree(ptf);
1646         GdipFree(pti);
1647         return OutOfMemory;
1648     }
1649
1650     for(i = 0; i < count; i++){
1651         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1652         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1653         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1654         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1655     }
1656
1657     save_state = prepare_dc(graphics, pen);
1658     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1659
1660     transform_and_round_points(graphics, pti, ptf, 4 * count);
1661
1662     for(i = 0; i < count; i++)
1663         Polygon(graphics->hdc, &pti[4 * i], 4);
1664
1665     restore_dc(graphics, save_state);
1666
1667     GdipFree(ptf);
1668     GdipFree(pti);
1669
1670     return Ok;
1671 }
1672
1673 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
1674     GDIPCONST GpRect* rects, INT count)
1675 {
1676     GpRectF *rectsF;
1677     GpStatus ret;
1678     INT i;
1679
1680     if(!rects || count<=0)
1681         return InvalidParameter;
1682
1683     rectsF = GdipAlloc(sizeof(GpRectF) * count);
1684     if(!rectsF)
1685         return OutOfMemory;
1686
1687     for(i = 0;i < count;i++){
1688         rectsF[i].X      = (REAL)rects[i].X;
1689         rectsF[i].Y      = (REAL)rects[i].Y;
1690         rectsF[i].Width  = (REAL)rects[i].Width;
1691         rectsF[i].Height = (REAL)rects[i].Height;
1692     }
1693
1694     ret = GdipDrawRectangles(graphics, pen, rectsF, count);
1695     GdipFree(rectsF);
1696
1697     return ret;
1698 }
1699
1700 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1701     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1702     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1703 {
1704     HRGN rgn = NULL;
1705     HFONT gdifont;
1706     LOGFONTW lfw;
1707     TEXTMETRICW textmet;
1708     GpPointF pt[2], rectcpy[4];
1709     POINT corners[4];
1710     WCHAR* stringdup;
1711     REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1712     INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
1713         nheight;
1714     SIZE size;
1715     RECT drawcoord;
1716
1717     if(!graphics || !string || !font || !brush || !rect)
1718         return InvalidParameter;
1719
1720     if((brush->bt != BrushTypeSolidColor)){
1721         FIXME("not implemented for given parameters\n");
1722         return NotImplemented;
1723     }
1724
1725     if(format)
1726         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1727
1728     if(length == -1) length = lstrlenW(string);
1729
1730     stringdup = GdipAlloc(length * sizeof(WCHAR));
1731     if(!stringdup) return OutOfMemory;
1732
1733     save_state = SaveDC(graphics->hdc);
1734     SetBkMode(graphics->hdc, TRANSPARENT);
1735     SetTextColor(graphics->hdc, brush->lb.lbColor);
1736
1737     rectcpy[3].X = rectcpy[0].X = rect->X;
1738     rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1739     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1740     rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1741     transform_and_round_points(graphics, corners, rectcpy, 4);
1742
1743     if(roundr(rect->Width) == 0 && roundr(rect->Height) == 0){
1744         rel_width = rel_height = 1.0;
1745         nwidth = nheight = INT_MAX;
1746     }
1747     else{
1748         rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1749                          (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1750                          / rect->Width;
1751         rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1752                           (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1753                           / rect->Height;
1754
1755         nwidth = roundr(rel_width * rect->Width);
1756         nheight = roundr(rel_height * rect->Height);
1757         rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1758         SelectClipRgn(graphics->hdc, rgn);
1759     }
1760
1761     /* Use gdi to find the font, then perform transformations on it (height,
1762      * width, angle). */
1763     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1764     GetTextMetricsW(graphics->hdc, &textmet);
1765     lfw = font->lfw;
1766
1767     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1768     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1769
1770     pt[0].X = 0.0;
1771     pt[0].Y = 0.0;
1772     pt[1].X = 1.0;
1773     pt[1].Y = 0.0;
1774     GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1775     angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1776     ang_cos = cos(angle);
1777     ang_sin = sin(angle);
1778     lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1779
1780     gdifont = CreateFontIndirectW(&lfw);
1781     DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1782
1783     for(i = 0, j = 0; i < length; i++){
1784         if(!isprintW(string[i]) && (string[i] != '\n'))
1785             continue;
1786
1787         stringdup[j] = string[i];
1788         j++;
1789     }
1790
1791     stringdup[j] = 0;
1792     length = j;
1793
1794     while(sum < length){
1795         drawcoord.left = corners[0].x + roundr(ang_sin * (REAL) height);
1796         drawcoord.top = corners[0].y + roundr(ang_cos * (REAL) height);
1797
1798         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1799                               nwidth, &fit, NULL, &size);
1800         fitcpy = fit;
1801
1802         if(fit == 0){
1803             DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, DT_NOCLIP |
1804                       DT_EXPANDTABS);
1805             break;
1806         }
1807
1808         for(lret = 0; lret < fit; lret++)
1809             if(*(stringdup + sum + lret) == '\n')
1810                 break;
1811
1812         /* Line break code (may look strange, but it imitates windows). */
1813         if(lret < fit)
1814             fit = lret;    /* this is not an off-by-one error */
1815         else if(fit < (length - sum)){
1816             if(*(stringdup + sum + fit) == ' ')
1817                 while(*(stringdup + sum + fit) == ' ')
1818                     fit++;
1819             else
1820                 while(*(stringdup + sum + fit - 1) != ' '){
1821                     fit--;
1822
1823                     if(*(stringdup + sum + fit) == '\t')
1824                         break;
1825
1826                     if(fit == 0){
1827                         fit = fitcpy;
1828                         break;
1829                     }
1830                 }
1831         }
1832         DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
1833                   &drawcoord, DT_NOCLIP | DT_EXPANDTABS);
1834
1835         sum += fit + (lret < fitcpy ? 1 : 0);
1836         height += size.cy;
1837
1838         if(height > nheight)
1839             break;
1840
1841         /* Stop if this was a linewrap (but not if it was a linebreak). */
1842         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1843             break;
1844     }
1845
1846     GdipFree(stringdup);
1847     DeleteObject(rgn);
1848     DeleteObject(gdifont);
1849
1850     RestoreDC(graphics->hdc, save_state);
1851
1852     return Ok;
1853 }
1854
1855 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
1856     GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
1857 {
1858     GpPath *path;
1859     GpStatus stat;
1860
1861     if(!graphics || !brush || !points)
1862         return InvalidParameter;
1863
1864     if(graphics->busy)
1865         return ObjectBusy;
1866
1867     stat = GdipCreatePath(fill, &path);
1868     if(stat != Ok)
1869         return stat;
1870
1871     stat = GdipAddPathClosedCurve2(path, points, count, tension);
1872     if(stat != Ok){
1873         GdipDeletePath(path);
1874         return stat;
1875     }
1876
1877     stat = GdipFillPath(graphics, brush, path);
1878     if(stat != Ok){
1879         GdipDeletePath(path);
1880         return stat;
1881     }
1882
1883     GdipDeletePath(path);
1884
1885     return Ok;
1886 }
1887
1888 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
1889     GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
1890 {
1891     GpPointF *ptf;
1892     GpStatus stat;
1893     INT i;
1894
1895     if(!points || count <= 0)
1896         return InvalidParameter;
1897
1898     ptf = GdipAlloc(sizeof(GpPointF)*count);
1899     if(!ptf)
1900         return OutOfMemory;
1901
1902     for(i = 0;i < count;i++){
1903         ptf[i].X = (REAL)points[i].X;
1904         ptf[i].Y = (REAL)points[i].Y;
1905     }
1906
1907     stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
1908
1909     GdipFree(ptf);
1910
1911     return stat;
1912 }
1913
1914 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
1915     REAL y, REAL width, REAL height)
1916 {
1917     INT save_state;
1918     GpPointF ptf[2];
1919     POINT pti[2];
1920
1921     if(!graphics || !brush)
1922         return InvalidParameter;
1923
1924     if(graphics->busy)
1925         return ObjectBusy;
1926
1927     ptf[0].X = x;
1928     ptf[0].Y = y;
1929     ptf[1].X = x + width;
1930     ptf[1].Y = y + height;
1931
1932     save_state = SaveDC(graphics->hdc);
1933     EndPath(graphics->hdc);
1934     SelectObject(graphics->hdc, brush->gdibrush);
1935     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1936
1937     transform_and_round_points(graphics, pti, ptf, 2);
1938
1939     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1940
1941     RestoreDC(graphics->hdc, save_state);
1942
1943     return Ok;
1944 }
1945
1946 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
1947     INT y, INT width, INT height)
1948 {
1949     return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1950 }
1951
1952 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1953 {
1954     INT save_state;
1955     GpStatus retval;
1956
1957     if(!brush || !graphics || !path)
1958         return InvalidParameter;
1959
1960     if(graphics->busy)
1961         return ObjectBusy;
1962
1963     save_state = SaveDC(graphics->hdc);
1964     EndPath(graphics->hdc);
1965     SelectObject(graphics->hdc, brush->gdibrush);
1966     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1967                                                                     : WINDING));
1968
1969     BeginPath(graphics->hdc);
1970     retval = draw_poly(graphics, NULL, path->pathdata.Points,
1971                        path->pathdata.Types, path->pathdata.Count, FALSE);
1972
1973     if(retval != Ok)
1974         goto end;
1975
1976     EndPath(graphics->hdc);
1977     FillPath(graphics->hdc);
1978
1979     retval = Ok;
1980
1981 end:
1982     RestoreDC(graphics->hdc, save_state);
1983
1984     return retval;
1985 }
1986
1987 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1988     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1989 {
1990     INT save_state;
1991
1992     if(!graphics || !brush)
1993         return InvalidParameter;
1994
1995     if(graphics->busy)
1996         return ObjectBusy;
1997
1998     save_state = SaveDC(graphics->hdc);
1999     EndPath(graphics->hdc);
2000     SelectObject(graphics->hdc, brush->gdibrush);
2001     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2002
2003     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2004
2005     RestoreDC(graphics->hdc, save_state);
2006
2007     return Ok;
2008 }
2009
2010 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
2011     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2012 {
2013     return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2014 }
2015
2016 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
2017     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
2018 {
2019     INT save_state;
2020     GpPointF *ptf = NULL;
2021     POINT *pti = NULL;
2022     GpStatus retval = Ok;
2023
2024     if(!graphics || !brush || !points || !count)
2025         return InvalidParameter;
2026
2027     if(graphics->busy)
2028         return ObjectBusy;
2029
2030     ptf = GdipAlloc(count * sizeof(GpPointF));
2031     pti = GdipAlloc(count * sizeof(POINT));
2032     if(!ptf || !pti){
2033         retval = OutOfMemory;
2034         goto end;
2035     }
2036
2037     memcpy(ptf, points, count * sizeof(GpPointF));
2038
2039     save_state = SaveDC(graphics->hdc);
2040     EndPath(graphics->hdc);
2041     SelectObject(graphics->hdc, brush->gdibrush);
2042     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2043     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2044                                                                   : WINDING));
2045
2046     transform_and_round_points(graphics, pti, ptf, count);
2047     Polygon(graphics->hdc, pti, count);
2048
2049     RestoreDC(graphics->hdc, save_state);
2050
2051 end:
2052     GdipFree(ptf);
2053     GdipFree(pti);
2054
2055     return retval;
2056 }
2057
2058 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
2059     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
2060 {
2061     INT save_state, i;
2062     GpPointF *ptf = NULL;
2063     POINT *pti = NULL;
2064     GpStatus retval = Ok;
2065
2066     if(!graphics || !brush || !points || !count)
2067         return InvalidParameter;
2068
2069     if(graphics->busy)
2070         return ObjectBusy;
2071
2072     ptf = GdipAlloc(count * sizeof(GpPointF));
2073     pti = GdipAlloc(count * sizeof(POINT));
2074     if(!ptf || !pti){
2075         retval = OutOfMemory;
2076         goto end;
2077     }
2078
2079     for(i = 0; i < count; i ++){
2080         ptf[i].X = (REAL) points[i].X;
2081         ptf[i].Y = (REAL) points[i].Y;
2082     }
2083
2084     save_state = SaveDC(graphics->hdc);
2085     EndPath(graphics->hdc);
2086     SelectObject(graphics->hdc, brush->gdibrush);
2087     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2088     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
2089                                                                   : WINDING));
2090
2091     transform_and_round_points(graphics, pti, ptf, count);
2092     Polygon(graphics->hdc, pti, count);
2093
2094     RestoreDC(graphics->hdc, save_state);
2095
2096 end:
2097     GdipFree(ptf);
2098     GdipFree(pti);
2099
2100     return retval;
2101 }
2102
2103 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
2104     GDIPCONST GpPointF *points, INT count)
2105 {
2106     return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
2107 }
2108
2109 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
2110     GDIPCONST GpPoint *points, INT count)
2111 {
2112     return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
2113 }
2114
2115 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
2116     REAL x, REAL y, REAL width, REAL height)
2117 {
2118     INT save_state;
2119     GpPointF ptf[4];
2120     POINT pti[4];
2121
2122     if(!graphics || !brush)
2123         return InvalidParameter;
2124
2125     if(graphics->busy)
2126         return ObjectBusy;
2127
2128     ptf[0].X = x;
2129     ptf[0].Y = y;
2130     ptf[1].X = x + width;
2131     ptf[1].Y = y;
2132     ptf[2].X = x + width;
2133     ptf[2].Y = y + height;
2134     ptf[3].X = x;
2135     ptf[3].Y = y + height;
2136
2137     save_state = SaveDC(graphics->hdc);
2138     EndPath(graphics->hdc);
2139     SelectObject(graphics->hdc, brush->gdibrush);
2140     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2141
2142     transform_and_round_points(graphics, pti, ptf, 4);
2143
2144     Polygon(graphics->hdc, pti, 4);
2145
2146     RestoreDC(graphics->hdc, save_state);
2147
2148     return Ok;
2149 }
2150
2151 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
2152     INT x, INT y, INT width, INT height)
2153 {
2154     INT save_state;
2155     GpPointF ptf[4];
2156     POINT pti[4];
2157
2158     if(!graphics || !brush)
2159         return InvalidParameter;
2160
2161     if(graphics->busy)
2162         return ObjectBusy;
2163
2164     ptf[0].X = x;
2165     ptf[0].Y = y;
2166     ptf[1].X = x + width;
2167     ptf[1].Y = y;
2168     ptf[2].X = x + width;
2169     ptf[2].Y = y + height;
2170     ptf[3].X = x;
2171     ptf[3].Y = y + height;
2172
2173     save_state = SaveDC(graphics->hdc);
2174     EndPath(graphics->hdc);
2175     SelectObject(graphics->hdc, brush->gdibrush);
2176     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
2177
2178     transform_and_round_points(graphics, pti, ptf, 4);
2179
2180     Polygon(graphics->hdc, pti, 4);
2181
2182     RestoreDC(graphics->hdc, save_state);
2183
2184     return Ok;
2185 }
2186
2187 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
2188     INT count)
2189 {
2190     GpStatus ret;
2191     INT i;
2192
2193     if(!rects)
2194         return InvalidParameter;
2195
2196     for(i = 0; i < count; i++){
2197         ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2198         if(ret != Ok)   return ret;
2199     }
2200
2201     return Ok;
2202 }
2203
2204 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
2205     INT count)
2206 {
2207     GpRectF *rectsF;
2208     GpStatus ret;
2209     INT i;
2210
2211     if(!rects || count <= 0)
2212         return InvalidParameter;
2213
2214     rectsF = GdipAlloc(sizeof(GpRectF)*count);
2215     if(!rectsF)
2216         return OutOfMemory;
2217
2218     for(i = 0; i < count; i++){
2219         rectsF[i].X      = (REAL)rects[i].X;
2220         rectsF[i].Y      = (REAL)rects[i].Y;
2221         rectsF[i].X      = (REAL)rects[i].Width;
2222         rectsF[i].Height = (REAL)rects[i].Height;
2223     }
2224
2225     ret = GdipFillRectangles(graphics,brush,rectsF,count);
2226     GdipFree(rectsF);
2227
2228     return ret;
2229 }
2230
2231 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
2232         GpRegion* region)
2233 {
2234     if (!(graphics && brush && region))
2235         return InvalidParameter;
2236
2237     if(graphics->busy)
2238         return ObjectBusy;
2239
2240     FIXME("(%p, %p, %p): stub\n", graphics, brush, region);
2241
2242     return NotImplemented;
2243 }
2244
2245 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
2246 {
2247     static int calls;
2248
2249     if(!graphics)
2250         return InvalidParameter;
2251
2252     if(graphics->busy)
2253         return ObjectBusy;
2254
2255     if(!(calls++))
2256         FIXME("not implemented\n");
2257
2258     return NotImplemented;
2259 }
2260
2261 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
2262 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
2263     CompositingMode *mode)
2264 {
2265     if(!graphics || !mode)
2266         return InvalidParameter;
2267
2268     if(graphics->busy)
2269         return ObjectBusy;
2270
2271     *mode = graphics->compmode;
2272
2273     return Ok;
2274 }
2275
2276 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
2277 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
2278     CompositingQuality *quality)
2279 {
2280     if(!graphics || !quality)
2281         return InvalidParameter;
2282
2283     if(graphics->busy)
2284         return ObjectBusy;
2285
2286     *quality = graphics->compqual;
2287
2288     return Ok;
2289 }
2290
2291 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
2292 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
2293     InterpolationMode *mode)
2294 {
2295     if(!graphics || !mode)
2296         return InvalidParameter;
2297
2298     if(graphics->busy)
2299         return ObjectBusy;
2300
2301     *mode = graphics->interpolation;
2302
2303     return Ok;
2304 }
2305
2306 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
2307 {
2308     if(!graphics || !scale)
2309         return InvalidParameter;
2310
2311     if(graphics->busy)
2312         return ObjectBusy;
2313
2314     *scale = graphics->scale;
2315
2316     return Ok;
2317 }
2318
2319 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
2320 {
2321     if(!graphics || !unit)
2322         return InvalidParameter;
2323
2324     if(graphics->busy)
2325         return ObjectBusy;
2326
2327     *unit = graphics->unit;
2328
2329     return Ok;
2330 }
2331
2332 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
2333 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2334     *mode)
2335 {
2336     if(!graphics || !mode)
2337         return InvalidParameter;
2338
2339     if(graphics->busy)
2340         return ObjectBusy;
2341
2342     *mode = graphics->pixeloffset;
2343
2344     return Ok;
2345 }
2346
2347 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
2348 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
2349 {
2350     if(!graphics || !mode)
2351         return InvalidParameter;
2352
2353     if(graphics->busy)
2354         return ObjectBusy;
2355
2356     *mode = graphics->smoothing;
2357
2358     return Ok;
2359 }
2360
2361 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
2362 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
2363     TextRenderingHint *hint)
2364 {
2365     if(!graphics || !hint)
2366         return InvalidParameter;
2367
2368     if(graphics->busy)
2369         return ObjectBusy;
2370
2371     *hint = graphics->texthint;
2372
2373     return Ok;
2374 }
2375
2376 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2377 {
2378     if(!graphics || !matrix)
2379         return InvalidParameter;
2380
2381     if(graphics->busy)
2382         return ObjectBusy;
2383
2384     *matrix = *graphics->worldtrans;
2385     return Ok;
2386 }
2387
2388 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
2389         GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
2390         GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
2391         INT regionCount, GpRegion** regions)
2392 {
2393     if (!(graphics && string && font && layoutRect && stringFormat && regions))
2394         return InvalidParameter;
2395
2396     FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
2397             length, font, layoutRect, stringFormat, regionCount, regions);
2398
2399     return NotImplemented;
2400 }
2401
2402 /* Find the smallest rectangle that bounds the text when it is printed in rect
2403  * according to the format options listed in format. If rect has 0 width and
2404  * height, then just find the smallest rectangle that bounds the text when it's
2405  * printed at location (rect->X, rect-Y). */
2406 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
2407     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
2408     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
2409     INT *codepointsfitted, INT *linesfilled)
2410 {
2411     HFONT oldfont;
2412     WCHAR* stringdup;
2413     INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
2414         nheight;
2415     SIZE size;
2416
2417     if(!graphics || !string || !font || !rect)
2418         return InvalidParameter;
2419
2420     if(codepointsfitted || linesfilled){
2421         FIXME("not implemented for given parameters\n");
2422         return NotImplemented;
2423     }
2424
2425     if(format)
2426         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2427
2428     if(length == -1) length = lstrlenW(string);
2429
2430     stringdup = GdipAlloc(length * sizeof(WCHAR));
2431     if(!stringdup) return OutOfMemory;
2432
2433     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2434     nwidth = roundr(rect->Width);
2435     nheight = roundr(rect->Height);
2436
2437     if((nwidth == 0) && (nheight == 0))
2438         nwidth = nheight = INT_MAX;
2439
2440     for(i = 0, j = 0; i < length; i++){
2441         if(!isprintW(string[i]) && (string[i] != '\n'))
2442             continue;
2443
2444         stringdup[j] = string[i];
2445         j++;
2446     }
2447
2448     stringdup[j] = 0;
2449     length = j;
2450
2451     while(sum < length){
2452         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2453                               nwidth, &fit, NULL, &size);
2454         fitcpy = fit;
2455
2456         if(fit == 0)
2457             break;
2458
2459         for(lret = 0; lret < fit; lret++)
2460             if(*(stringdup + sum + lret) == '\n')
2461                 break;
2462
2463         /* Line break code (may look strange, but it imitates windows). */
2464         if(lret < fit)
2465             fit = lret;    /* this is not an off-by-one error */
2466         else if(fit < (length - sum)){
2467             if(*(stringdup + sum + fit) == ' ')
2468                 while(*(stringdup + sum + fit) == ' ')
2469                     fit++;
2470             else
2471                 while(*(stringdup + sum + fit - 1) != ' '){
2472                     fit--;
2473
2474                     if(*(stringdup + sum + fit) == '\t')
2475                         break;
2476
2477                     if(fit == 0){
2478                         fit = fitcpy;
2479                         break;
2480                     }
2481                 }
2482         }
2483
2484         GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
2485                               nwidth, &j, NULL, &size);
2486
2487         sum += fit + (lret < fitcpy ? 1 : 0);
2488         height += size.cy;
2489         max_width = max(max_width, size.cx);
2490
2491         if(height > nheight)
2492             break;
2493
2494         /* Stop if this was a linewrap (but not if it was a linebreak). */
2495         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2496             break;
2497     }
2498
2499     bounds->X = rect->X;
2500     bounds->Y = rect->Y;
2501     bounds->Width = (REAL)max_width;
2502     bounds->Height = (REAL) min(height, nheight);
2503
2504     GdipFree(stringdup);
2505     DeleteObject(SelectObject(graphics->hdc, oldfont));
2506
2507     return Ok;
2508 }
2509
2510 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
2511 {
2512     if(!graphics)
2513         return InvalidParameter;
2514
2515     if(graphics->busy)
2516         return ObjectBusy;
2517
2518     graphics->worldtrans->matrix[0] = 1.0;
2519     graphics->worldtrans->matrix[1] = 0.0;
2520     graphics->worldtrans->matrix[2] = 0.0;
2521     graphics->worldtrans->matrix[3] = 1.0;
2522     graphics->worldtrans->matrix[4] = 0.0;
2523     graphics->worldtrans->matrix[5] = 0.0;
2524
2525     return Ok;
2526 }
2527
2528 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
2529 {
2530     static int calls;
2531
2532     if(!graphics)
2533         return InvalidParameter;
2534
2535     if(!(calls++))
2536         FIXME("graphics state not implemented\n");
2537
2538     return NotImplemented;
2539 }
2540
2541 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
2542     GpMatrixOrder order)
2543 {
2544     if(!graphics)
2545         return InvalidParameter;
2546
2547     if(graphics->busy)
2548         return ObjectBusy;
2549
2550     return GdipRotateMatrix(graphics->worldtrans, angle, order);
2551 }
2552
2553 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
2554 {
2555     static int calls;
2556
2557     if(!graphics || !state)
2558         return InvalidParameter;
2559
2560     if(!(calls++))
2561         FIXME("graphics state not implemented\n");
2562
2563     return NotImplemented;
2564 }
2565
2566 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
2567     REAL sy, GpMatrixOrder order)
2568 {
2569     if(!graphics)
2570         return InvalidParameter;
2571
2572     if(graphics->busy)
2573         return ObjectBusy;
2574
2575     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
2576 }
2577
2578 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
2579     CompositingMode mode)
2580 {
2581     if(!graphics)
2582         return InvalidParameter;
2583
2584     if(graphics->busy)
2585         return ObjectBusy;
2586
2587     graphics->compmode = mode;
2588
2589     return Ok;
2590 }
2591
2592 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
2593     CompositingQuality quality)
2594 {
2595     if(!graphics)
2596         return InvalidParameter;
2597
2598     if(graphics->busy)
2599         return ObjectBusy;
2600
2601     graphics->compqual = quality;
2602
2603     return Ok;
2604 }
2605
2606 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
2607     InterpolationMode mode)
2608 {
2609     if(!graphics)
2610         return InvalidParameter;
2611
2612     if(graphics->busy)
2613         return ObjectBusy;
2614
2615     graphics->interpolation = mode;
2616
2617     return Ok;
2618 }
2619
2620 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
2621 {
2622     if(!graphics || (scale <= 0.0))
2623         return InvalidParameter;
2624
2625     if(graphics->busy)
2626         return ObjectBusy;
2627
2628     graphics->scale = scale;
2629
2630     return Ok;
2631 }
2632
2633 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
2634 {
2635     if(!graphics)
2636         return InvalidParameter;
2637
2638     if(graphics->busy)
2639         return ObjectBusy;
2640
2641     if(unit == UnitWorld)
2642         return InvalidParameter;
2643
2644     graphics->unit = unit;
2645
2646     return Ok;
2647 }
2648
2649 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2650     mode)
2651 {
2652     if(!graphics)
2653         return InvalidParameter;
2654
2655     if(graphics->busy)
2656         return ObjectBusy;
2657
2658     graphics->pixeloffset = mode;
2659
2660     return Ok;
2661 }
2662
2663 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
2664 {
2665     if(!graphics)
2666         return InvalidParameter;
2667
2668     if(graphics->busy)
2669         return ObjectBusy;
2670
2671     graphics->smoothing = mode;
2672
2673     return Ok;
2674 }
2675
2676 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
2677     TextRenderingHint hint)
2678 {
2679     if(!graphics)
2680         return InvalidParameter;
2681
2682     if(graphics->busy)
2683         return ObjectBusy;
2684
2685     graphics->texthint = hint;
2686
2687     return Ok;
2688 }
2689
2690 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2691 {
2692     if(!graphics || !matrix)
2693         return InvalidParameter;
2694
2695     if(graphics->busy)
2696         return ObjectBusy;
2697
2698     GdipDeleteMatrix(graphics->worldtrans);
2699     return GdipCloneMatrix(matrix, &graphics->worldtrans);
2700 }
2701
2702 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
2703     REAL dy, GpMatrixOrder order)
2704 {
2705     if(!graphics)
2706         return InvalidParameter;
2707
2708     if(graphics->busy)
2709         return ObjectBusy;
2710
2711     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
2712 }
2713
2714 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
2715                                      INT width, INT height,
2716                                      CombineMode combineMode)
2717 {
2718     static int calls;
2719
2720     if(!graphics)
2721         return InvalidParameter;
2722
2723     if(graphics->busy)
2724         return ObjectBusy;
2725
2726     if(!(calls++))
2727         FIXME("not implemented\n");
2728
2729     return NotImplemented;
2730 }
2731
2732 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
2733                                      CombineMode combineMode)
2734 {
2735     static int calls;
2736
2737     if(!(calls++))
2738         FIXME("not implemented\n");
2739
2740     return NotImplemented;
2741 }
2742
2743 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
2744     UINT limitDpi)
2745 {
2746     static int calls;
2747
2748     if(!(calls++))
2749         FIXME("not implemented\n");
2750
2751     return NotImplemented;
2752 }
2753
2754 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
2755     INT count)
2756 {
2757     INT save_state;
2758     POINT *pti;
2759
2760     if(!graphics || !pen || count<=0)
2761         return InvalidParameter;
2762
2763     if(graphics->busy)
2764         return ObjectBusy;
2765
2766     pti = GdipAlloc(sizeof(POINT) * count);
2767
2768     save_state = prepare_dc(graphics, pen);
2769     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2770
2771     transform_and_round_points(graphics, pti, (GpPointF*)points, count);
2772     Polygon(graphics->hdc, pti, count);
2773
2774     restore_dc(graphics, save_state);
2775     GdipFree(pti);
2776
2777     return Ok;
2778 }
2779
2780 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
2781     INT count)
2782 {
2783     GpStatus ret;
2784     GpPointF *ptf;
2785     INT i;
2786
2787     if(count<=0)    return InvalidParameter;
2788     ptf = GdipAlloc(sizeof(GpPointF) * count);
2789
2790     for(i = 0;i < count; i++){
2791         ptf[i].X = (REAL)points[i].X;
2792         ptf[i].Y = (REAL)points[i].Y;
2793     }
2794
2795     ret = GdipDrawPolygon(graphics,pen,ptf,count);
2796     GdipFree(ptf);
2797
2798     return ret;
2799 }
2800
2801 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
2802 {
2803     if(!graphics || !dpi)
2804         return InvalidParameter;
2805
2806     if(graphics->busy)
2807         return ObjectBusy;
2808
2809     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
2810
2811     return Ok;
2812 }
2813
2814 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
2815 {
2816     if(!graphics || !dpi)
2817         return InvalidParameter;
2818
2819     if(graphics->busy)
2820         return ObjectBusy;
2821
2822     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
2823
2824     return Ok;
2825 }
2826
2827 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
2828     GpMatrixOrder order)
2829 {
2830     GpMatrix m;
2831     GpStatus ret;
2832
2833     if(!graphics || !matrix)
2834         return InvalidParameter;
2835
2836     if(graphics->busy)
2837         return ObjectBusy;
2838
2839     m = *(graphics->worldtrans);
2840
2841     ret = GdipMultiplyMatrix(&m, (GpMatrix*)matrix, order);
2842     if(ret == Ok)
2843         *(graphics->worldtrans) = m;
2844
2845     return ret;
2846 }
2847
2848 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
2849 {
2850     if(!graphics || !hdc)
2851         return InvalidParameter;
2852
2853     if(graphics->busy)
2854         return ObjectBusy;
2855
2856     *hdc = graphics->hdc;
2857     graphics->busy = TRUE;
2858
2859     return Ok;
2860 }
2861
2862 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
2863 {
2864     if(!graphics)
2865         return InvalidParameter;
2866
2867     if(graphics->hdc != hdc || !(graphics->busy))
2868         return InvalidParameter;
2869
2870     graphics->busy = FALSE;
2871
2872     return Ok;
2873 }
2874
2875 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
2876 {
2877     if(!graphics || !region)
2878         return InvalidParameter;
2879
2880     if(graphics->busy)
2881         return ObjectBusy;
2882
2883     FIXME("(%p, %p): stub\n", graphics, region);
2884     return NotImplemented;
2885 }
2886
2887 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
2888                                         GpCoordinateSpace src_space, GpPointF *points, INT count)
2889 {
2890     FIXME("(%p, %d, %d, %p, %d): stub\n", graphics, dst_space, src_space, points, count);
2891
2892     return NotImplemented;
2893 }
2894
2895 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
2896                                          GpCoordinateSpace src_space, GpPoint *points, INT count)
2897 {
2898     FIXME("(%p, %d, %d, %p, %d): stub\n", graphics, dst_space, src_space, points, count);
2899
2900     return NotImplemented;
2901 }