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