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