gdiplus: Stubs for GdipGetDC and GdipReleaseDC.
[wine] / dlls / gdiplus / graphics.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20 #include <math.h>
21 #include <limits.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
28
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
34
35 #include "winreg.h"
36 #include "shlwapi.h"
37
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
43
44 /* looks-right constants */
45 #define TENSION_CONST (0.3)
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
48
49 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
51 {
52     REAL radAngle, hypotenuse;
53
54     radAngle = deg2rad(angle);
55     hypotenuse = 50.0; /* arbitrary */
56
57     *x = x_0 + cos(radAngle) * hypotenuse;
58     *y = y_0 + sin(radAngle) * hypotenuse;
59 }
60
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
63 {
64     BYTE ret;
65
66     switch(type & PathPointTypePathTypeMask){
67         case PathPointTypeBezier:
68             ret = PT_BEZIERTO;
69             break;
70         case PathPointTypeLine:
71             ret = PT_LINETO;
72             break;
73         case PathPointTypeStart:
74             ret = PT_MOVETO;
75             break;
76         default:
77             ERR("Bad point type\n");
78             return 0;
79     }
80
81     if(type & PathPointTypeCloseSubpath)
82         ret |= PT_CLOSEFIGURE;
83
84     return ret;
85 }
86
87 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
88 {
89     HPEN gdipen;
90     REAL width;
91     INT save_state = SaveDC(graphics->hdc), i, numdashes;
92     GpPointF pt[2];
93     DWORD dash_array[MAX_DASHLEN];
94
95     EndPath(graphics->hdc);
96
97     if(pen->unit == UnitPixel){
98         width = pen->width;
99     }
100     else{
101         /* Get an estimate for the amount the pen width is affected by the world
102          * transform. (This is similar to what some of the wine drivers do.) */
103         pt[0].X = 0.0;
104         pt[0].Y = 0.0;
105         pt[1].X = 1.0;
106         pt[1].Y = 1.0;
107         GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
108         width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
109                      (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
110
111         width *= pen->width * convert_unit(graphics->hdc,
112                               pen->unit == UnitWorld ? graphics->unit : pen->unit);
113     }
114
115     if(pen->dash == DashStyleCustom){
116         numdashes = min(pen->numdashes, MAX_DASHLEN);
117
118         TRACE("dashes are: ");
119         for(i = 0; i < numdashes; i++){
120             dash_array[i] = roundr(width * pen->dashes[i]);
121             TRACE("%d, ", dash_array[i]);
122         }
123         TRACE("\n and the pen style is %x\n", pen->style);
124
125         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
126                               numdashes, dash_array);
127     }
128     else
129         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
130
131     SelectObject(graphics->hdc, gdipen);
132
133     return save_state;
134 }
135
136 static void restore_dc(GpGraphics *graphics, INT state)
137 {
138     DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
139     RestoreDC(graphics->hdc, state);
140 }
141
142 /* This helper applies all the changes that the points listed in ptf need in
143  * order to be drawn on the device context.  In the end, this should include at
144  * least:
145  *  -scaling by page unit
146  *  -applying world transformation
147  *  -converting from float to int
148  * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
149  * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
150  * gdi to draw, and these functions would irreparably mess with line widths.
151  */
152 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
153     GpPointF *ptf, INT count)
154 {
155     REAL unitscale;
156     GpMatrix *matrix;
157     int i;
158
159     unitscale = convert_unit(graphics->hdc, graphics->unit);
160
161     /* apply page scale */
162     if(graphics->unit != UnitDisplay)
163         unitscale *= graphics->scale;
164
165     GdipCloneMatrix(graphics->worldtrans, &matrix);
166     GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
167     GdipTransformMatrixPoints(matrix, ptf, count);
168     GdipDeleteMatrix(matrix);
169
170     for(i = 0; i < count; i++){
171         pti[i].x = roundr(ptf[i].X);
172         pti[i].y = roundr(ptf[i].Y);
173     }
174 }
175
176 /* GdipDrawPie/GdipFillPie helper function */
177 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
178     REAL height, REAL startAngle, REAL sweepAngle)
179 {
180     GpPointF ptf[4];
181     POINT pti[4];
182
183     ptf[0].X = x;
184     ptf[0].Y = y;
185     ptf[1].X = x + width;
186     ptf[1].Y = y + height;
187
188     deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
189     deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
190
191     transform_and_round_points(graphics, pti, ptf, 4);
192
193     Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
194         pti[2].y, pti[3].x, pti[3].y);
195 }
196
197 /* GdipDrawCurve helper function.
198  * Calculates Bezier points from cardinal spline points. */
199 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
200     REAL *y1, REAL *x2, REAL *y2)
201 {
202     REAL xdiff, ydiff;
203
204     /* calculate tangent */
205     xdiff = pts[2].X - pts[0].X;
206     ydiff = pts[2].Y - pts[0].Y;
207
208     /* apply tangent to get control points */
209     *x1 = pts[1].X - tension * xdiff;
210     *y1 = pts[1].Y - tension * ydiff;
211     *x2 = pts[1].X + tension * xdiff;
212     *y2 = pts[1].Y + tension * ydiff;
213 }
214
215 /* GdipDrawCurve helper function.
216  * Calculates Bezier points from cardinal spline endpoints. */
217 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
218     REAL tension, REAL *x, REAL *y)
219 {
220     /* tangent at endpoints is the line from the endpoint to the adjacent point */
221     *x = roundr(tension * (xadj - xend) + xend);
222     *y = roundr(tension * (yadj - yend) + yend);
223 }
224
225 /* Draws the linecap the specified color and size on the hdc.  The linecap is in
226  * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
227  * should not be called on an hdc that has a path you care about. */
228 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
229     const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
230 {
231     HGDIOBJ oldbrush = NULL, oldpen = NULL;
232     GpMatrix *matrix = NULL;
233     HBRUSH brush = NULL;
234     HPEN pen = NULL;
235     PointF ptf[4], *custptf = NULL;
236     POINT pt[4], *custpt = NULL;
237     BYTE *tp = NULL;
238     REAL theta, dsmall, dbig, dx, dy = 0.0;
239     INT i, count;
240     LOGBRUSH lb;
241     BOOL customstroke;
242
243     if((x1 == x2) && (y1 == y2))
244         return;
245
246     theta = gdiplus_atan2(y2 - y1, x2 - x1);
247
248     customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
249     if(!customstroke){
250         brush = CreateSolidBrush(color);
251         lb.lbStyle = BS_SOLID;
252         lb.lbColor = color;
253         lb.lbHatch = 0;
254         pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
255                            PS_JOIN_MITER, 1, &lb, 0,
256                            NULL);
257         oldbrush = SelectObject(graphics->hdc, brush);
258         oldpen = SelectObject(graphics->hdc, pen);
259     }
260
261     switch(cap){
262         case LineCapFlat:
263             break;
264         case LineCapSquare:
265         case LineCapSquareAnchor:
266         case LineCapDiamondAnchor:
267             size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
268             if(cap == LineCapDiamondAnchor){
269                 dsmall = cos(theta + M_PI_2) * size;
270                 dbig = sin(theta + M_PI_2) * size;
271             }
272             else{
273                 dsmall = cos(theta + M_PI_4) * size;
274                 dbig = sin(theta + M_PI_4) * size;
275             }
276
277             ptf[0].X = x2 - dsmall;
278             ptf[1].X = x2 + dbig;
279
280             ptf[0].Y = y2 - dbig;
281             ptf[3].Y = y2 + dsmall;
282
283             ptf[1].Y = y2 - dsmall;
284             ptf[2].Y = y2 + dbig;
285
286             ptf[3].X = x2 - dbig;
287             ptf[2].X = x2 + dsmall;
288
289             transform_and_round_points(graphics, pt, ptf, 4);
290             Polygon(graphics->hdc, pt, 4);
291
292             break;
293         case LineCapArrowAnchor:
294             size = size * 4.0 / sqrt(3.0);
295
296             dx = cos(M_PI / 6.0 + theta) * size;
297             dy = sin(M_PI / 6.0 + theta) * size;
298
299             ptf[0].X = x2 - dx;
300             ptf[0].Y = y2 - dy;
301
302             dx = cos(- M_PI / 6.0 + theta) * size;
303             dy = sin(- M_PI / 6.0 + theta) * size;
304
305             ptf[1].X = x2 - dx;
306             ptf[1].Y = y2 - dy;
307
308             ptf[2].X = x2;
309             ptf[2].Y = y2;
310
311             transform_and_round_points(graphics, pt, ptf, 3);
312             Polygon(graphics->hdc, pt, 3);
313
314             break;
315         case LineCapRoundAnchor:
316             dx = dy = ANCHOR_WIDTH * size / 2.0;
317
318             ptf[0].X = x2 - dx;
319             ptf[0].Y = y2 - dy;
320             ptf[1].X = x2 + dx;
321             ptf[1].Y = y2 + dy;
322
323             transform_and_round_points(graphics, pt, ptf, 2);
324             Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
325
326             break;
327         case LineCapTriangle:
328             size = size / 2.0;
329             dx = cos(M_PI_2 + theta) * size;
330             dy = sin(M_PI_2 + theta) * size;
331
332             ptf[0].X = x2 - dx;
333             ptf[0].Y = y2 - dy;
334             ptf[1].X = x2 + dx;
335             ptf[1].Y = y2 + dy;
336
337             dx = cos(theta) * size;
338             dy = sin(theta) * size;
339
340             ptf[2].X = x2 + dx;
341             ptf[2].Y = y2 + dy;
342
343             transform_and_round_points(graphics, pt, ptf, 3);
344             Polygon(graphics->hdc, pt, 3);
345
346             break;
347         case LineCapRound:
348             dx = dy = size / 2.0;
349
350             ptf[0].X = x2 - dx;
351             ptf[0].Y = y2 - dy;
352             ptf[1].X = x2 + dx;
353             ptf[1].Y = y2 + dy;
354
355             dx = -cos(M_PI_2 + theta) * size;
356             dy = -sin(M_PI_2 + theta) * size;
357
358             ptf[2].X = x2 - dx;
359             ptf[2].Y = y2 - dy;
360             ptf[3].X = x2 + dx;
361             ptf[3].Y = y2 + dy;
362
363             transform_and_round_points(graphics, pt, ptf, 4);
364             Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
365                 pt[2].y, pt[3].x, pt[3].y);
366
367             break;
368         case LineCapCustom:
369             if(!custom)
370                 break;
371
372             count = custom->pathdata.Count;
373             custptf = GdipAlloc(count * sizeof(PointF));
374             custpt = GdipAlloc(count * sizeof(POINT));
375             tp = GdipAlloc(count);
376
377             if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
378                 goto custend;
379
380             memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
381
382             GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
383             GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
384                              MatrixOrderAppend);
385             GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
386             GdipTransformMatrixPoints(matrix, custptf, count);
387
388             transform_and_round_points(graphics, custpt, custptf, count);
389
390             for(i = 0; i < count; i++)
391                 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
392
393             if(custom->fill){
394                 BeginPath(graphics->hdc);
395                 PolyDraw(graphics->hdc, custpt, tp, count);
396                 EndPath(graphics->hdc);
397                 StrokeAndFillPath(graphics->hdc);
398             }
399             else
400                 PolyDraw(graphics->hdc, custpt, tp, count);
401
402 custend:
403             GdipFree(custptf);
404             GdipFree(custpt);
405             GdipFree(tp);
406             GdipDeleteMatrix(matrix);
407             break;
408         default:
409             break;
410     }
411
412     if(!customstroke){
413         SelectObject(graphics->hdc, oldbrush);
414         SelectObject(graphics->hdc, oldpen);
415         DeleteObject(brush);
416         DeleteObject(pen);
417     }
418 }
419
420 /* Shortens the line by the given percent by changing x2, y2.
421  * If percent is > 1.0 then the line will change direction.
422  * If percent is negative it can lengthen the line. */
423 static void shorten_line_percent(REAL x1, REAL  y1, REAL *x2, REAL *y2, REAL percent)
424 {
425     REAL dist, theta, dx, dy;
426
427     if((y1 == *y2) && (x1 == *x2))
428         return;
429
430     dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
431     theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
432     dx = cos(theta) * dist;
433     dy = sin(theta) * dist;
434
435     *x2 = *x2 + dx;
436     *y2 = *y2 + dy;
437 }
438
439 /* Shortens the line by the given amount by changing x2, y2.
440  * If the amount is greater than the distance, the line will become length 0.
441  * If the amount is negative, it can lengthen the line. */
442 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
443 {
444     REAL dx, dy, percent;
445
446     dx = *x2 - x1;
447     dy = *y2 - y1;
448     if(dx == 0 && dy == 0)
449         return;
450
451     percent = amt / sqrt(dx * dx + dy * dy);
452     if(percent >= 1.0){
453         *x2 = x1;
454         *y2 = y1;
455         return;
456     }
457
458     shorten_line_percent(x1, y1, x2, y2, percent);
459 }
460
461 /* Draws lines between the given points, and if caps is true then draws an endcap
462  * at the end of the last line. */
463 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
464     GDIPCONST GpPointF * pt, INT count, BOOL caps)
465 {
466     POINT *pti = NULL;
467     GpPointF *ptcopy = NULL;
468     GpStatus status = GenericError;
469
470     if(!count)
471         return Ok;
472
473     pti = GdipAlloc(count * sizeof(POINT));
474     ptcopy = GdipAlloc(count * sizeof(GpPointF));
475
476     if(!pti || !ptcopy){
477         status = OutOfMemory;
478         goto end;
479     }
480
481     memcpy(ptcopy, pt, count * sizeof(GpPointF));
482
483     if(caps){
484         if(pen->endcap == LineCapArrowAnchor)
485             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
486                              &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
487         else if((pen->endcap == LineCapCustom) && pen->customend)
488             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
489                              &ptcopy[count-1].X, &ptcopy[count-1].Y,
490                              pen->customend->inset * pen->width);
491
492         if(pen->startcap == LineCapArrowAnchor)
493             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
494                              &ptcopy[0].X, &ptcopy[0].Y, pen->width);
495         else if((pen->startcap == LineCapCustom) && pen->customstart)
496             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
497                              &ptcopy[0].X, &ptcopy[0].Y,
498                              pen->customstart->inset * pen->width);
499
500         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
501                  pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
502         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
503                          pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
504     }
505
506     transform_and_round_points(graphics, pti, ptcopy, count);
507
508     if(Polyline(graphics->hdc, pti, count))
509         status = Ok;
510
511 end:
512     GdipFree(pti);
513     GdipFree(ptcopy);
514
515     return status;
516 }
517
518 /* Conducts a linear search to find the bezier points that will back off
519  * the endpoint of the curve by a distance of amt. Linear search works
520  * better than binary in this case because there are multiple solutions,
521  * and binary searches often find a bad one. I don't think this is what
522  * Windows does but short of rendering the bezier without GDI's help it's
523  * the best we can do. If rev then work from the start of the passed points
524  * instead of the end. */
525 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
526 {
527     GpPointF origpt[4];
528     REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
529     INT i, first = 0, second = 1, third = 2, fourth = 3;
530
531     if(rev){
532         first = 3;
533         second = 2;
534         third = 1;
535         fourth = 0;
536     }
537
538     origx = pt[fourth].X;
539     origy = pt[fourth].Y;
540     memcpy(origpt, pt, sizeof(GpPointF) * 4);
541
542     for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
543         /* reset bezier points to original values */
544         memcpy(pt, origpt, sizeof(GpPointF) * 4);
545         /* Perform magic on bezier points. Order is important here.*/
546         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
547         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
548         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
549         shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
550         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
551         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
552
553         dx = pt[fourth].X - origx;
554         dy = pt[fourth].Y - origy;
555
556         diff = sqrt(dx * dx + dy * dy);
557         percent += 0.0005 * amt;
558     }
559 }
560
561 /* Draws bezier curves between given points, and if caps is true then draws an
562  * endcap at the end of the last line. */
563 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
564     GDIPCONST GpPointF * pt, INT count, BOOL caps)
565 {
566     POINT *pti;
567     GpPointF *ptcopy;
568     GpStatus status = GenericError;
569
570     if(!count)
571         return Ok;
572
573     pti = GdipAlloc(count * sizeof(POINT));
574     ptcopy = GdipAlloc(count * sizeof(GpPointF));
575
576     if(!pti || !ptcopy){
577         status = OutOfMemory;
578         goto end;
579     }
580
581     memcpy(ptcopy, pt, count * sizeof(GpPointF));
582
583     if(caps){
584         if(pen->endcap == LineCapArrowAnchor)
585             shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
586         else if((pen->endcap == LineCapCustom) && pen->customend)
587             shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
588                                FALSE);
589
590         if(pen->startcap == LineCapArrowAnchor)
591             shorten_bezier_amt(ptcopy, pen->width, TRUE);
592         else if((pen->startcap == LineCapCustom) && pen->customstart)
593             shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
594
595         /* the direction of the line cap is parallel to the direction at the
596          * end of the bezier (which, if it has been shortened, is not the same
597          * as the direction from pt[count-2] to pt[count-1]) */
598         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
599             pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
600             pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
601             pt[count - 1].X, pt[count - 1].Y);
602
603         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
604             pt[0].X - (ptcopy[0].X - ptcopy[1].X),
605             pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
606     }
607
608     transform_and_round_points(graphics, pti, ptcopy, count);
609
610     PolyBezier(graphics->hdc, pti, count);
611
612     status = Ok;
613
614 end:
615     GdipFree(pti);
616     GdipFree(ptcopy);
617
618     return status;
619 }
620
621 /* Draws a combination of bezier curves and lines between points. */
622 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
623     GDIPCONST BYTE * types, INT count, BOOL caps)
624 {
625     POINT *pti = GdipAlloc(count * sizeof(POINT));
626     BYTE *tp = GdipAlloc(count);
627     GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
628     INT i, j;
629     GpStatus status = GenericError;
630
631     if(!count){
632         status = Ok;
633         goto end;
634     }
635     if(!pti || !tp || !ptcopy){
636         status = OutOfMemory;
637         goto end;
638     }
639
640     for(i = 1; i < count; i++){
641         if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
642             if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
643                 || !(types[i + 1] & PathPointTypeBezier)){
644                 ERR("Bad bezier points\n");
645                 goto end;
646             }
647             i += 2;
648         }
649     }
650
651     memcpy(ptcopy, pt, count * sizeof(GpPointF));
652
653     /* If we are drawing caps, go through the points and adjust them accordingly,
654      * and draw the caps. */
655     if(caps){
656         switch(types[count - 1] & PathPointTypePathTypeMask){
657             case PathPointTypeBezier:
658                 if(pen->endcap == LineCapArrowAnchor)
659                     shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
660                 else if((pen->endcap == LineCapCustom) && pen->customend)
661                     shorten_bezier_amt(&ptcopy[count - 4],
662                                        pen->width * pen->customend->inset, FALSE);
663
664                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
665                     pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
666                     pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
667                     pt[count - 1].X, pt[count - 1].Y);
668
669                 break;
670             case PathPointTypeLine:
671                 if(pen->endcap == LineCapArrowAnchor)
672                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
673                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
674                                      pen->width);
675                 else if((pen->endcap == LineCapCustom) && pen->customend)
676                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
677                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
678                                      pen->customend->inset * pen->width);
679
680                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
681                          pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
682                          pt[count - 1].Y);
683
684                 break;
685             default:
686                 ERR("Bad path last point\n");
687                 goto end;
688         }
689
690         /* Find start of points */
691         for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
692             == PathPointTypeStart); j++);
693
694         switch(types[j] & PathPointTypePathTypeMask){
695             case PathPointTypeBezier:
696                 if(pen->startcap == LineCapArrowAnchor)
697                     shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
698                 else if((pen->startcap == LineCapCustom) && pen->customstart)
699                     shorten_bezier_amt(&ptcopy[j - 1],
700                                        pen->width * pen->customstart->inset, TRUE);
701
702                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
703                     pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
704                     pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
705                     pt[j - 1].X, pt[j - 1].Y);
706
707                 break;
708             case PathPointTypeLine:
709                 if(pen->startcap == LineCapArrowAnchor)
710                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
711                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
712                                      pen->width);
713                 else if((pen->startcap == LineCapCustom) && pen->customstart)
714                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
715                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
716                                      pen->customstart->inset * pen->width);
717
718                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
719                          pt[j].X, pt[j].Y, pt[j - 1].X,
720                          pt[j - 1].Y);
721
722                 break;
723             default:
724                 ERR("Bad path points\n");
725                 goto end;
726         }
727     }
728
729     transform_and_round_points(graphics, pti, ptcopy, count);
730
731     for(i = 0; i < count; i++){
732         tp[i] = convert_path_point_type(types[i]);
733     }
734
735     PolyDraw(graphics->hdc, pti, tp, count);
736
737     status = Ok;
738
739 end:
740     GdipFree(pti);
741     GdipFree(ptcopy);
742     GdipFree(tp);
743
744     return status;
745 }
746
747 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
748 {
749     return GdipCreateFromHDC2(hdc, NULL, graphics);
750 }
751
752 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
753 {
754     GpStatus retval;
755
756     if(hDevice != NULL) {
757         FIXME("Don't know how to hadle parameter hDevice\n");
758         return NotImplemented;
759     }
760
761     if(hdc == NULL)
762         return OutOfMemory;
763
764     if(graphics == NULL)
765         return InvalidParameter;
766
767     *graphics = GdipAlloc(sizeof(GpGraphics));
768     if(!*graphics)  return OutOfMemory;
769
770     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
771         GdipFree(*graphics);
772         return retval;
773     }
774
775     (*graphics)->hdc = hdc;
776     (*graphics)->hwnd = NULL;
777     (*graphics)->smoothing = SmoothingModeDefault;
778     (*graphics)->compqual = CompositingQualityDefault;
779     (*graphics)->interpolation = InterpolationModeDefault;
780     (*graphics)->pixeloffset = PixelOffsetModeDefault;
781     (*graphics)->compmode = CompositingModeSourceOver;
782     (*graphics)->unit = UnitDisplay;
783     (*graphics)->scale = 1.0;
784
785     return Ok;
786 }
787
788 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
789 {
790     GpStatus ret;
791
792     if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
793         return ret;
794
795     (*graphics)->hwnd = hwnd;
796
797     return Ok;
798 }
799
800 /* FIXME: no icm handling */
801 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
802 {
803     return GdipCreateFromHWND(hwnd, graphics);
804 }
805
806 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
807     GpMetafile **metafile)
808 {
809     static int calls;
810
811     if(!hemf || !metafile)
812         return InvalidParameter;
813
814     if(!(calls++))
815         FIXME("not implemented\n");
816
817     return NotImplemented;
818 }
819
820 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
821     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
822 {
823     IStream *stream = NULL;
824     UINT read;
825     BYTE* copy;
826     HENHMETAFILE hemf;
827     GpStatus retval = GenericError;
828
829     if(!hwmf || !metafile || !placeable)
830         return InvalidParameter;
831
832     *metafile = NULL;
833     read = GetMetaFileBitsEx(hwmf, 0, NULL);
834     if(!read)
835         return GenericError;
836     copy = GdipAlloc(read);
837     GetMetaFileBitsEx(hwmf, read, copy);
838
839     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
840     GdipFree(copy);
841
842     read = GetEnhMetaFileBits(hemf, 0, NULL);
843     copy = GdipAlloc(read);
844     GetEnhMetaFileBits(hemf, read, copy);
845     DeleteEnhMetaFile(hemf);
846
847     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
848         ERR("could not make stream\n");
849         GdipFree(copy);
850         goto err;
851     }
852
853     *metafile = GdipAlloc(sizeof(GpMetafile));
854     if(!*metafile){
855         retval = OutOfMemory;
856         goto err;
857     }
858
859     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
860         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
861         goto err;
862
863
864     (*metafile)->image.type = ImageTypeMetafile;
865     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
866     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
867     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
868                     - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
869     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
870                    - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
871     (*metafile)->unit = UnitInch;
872
873     if(delete)
874         DeleteMetaFile(hwmf);
875
876     return Ok;
877
878 err:
879     GdipFree(*metafile);
880     IStream_Release(stream);
881     return retval;
882 }
883
884 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
885     UINT access, IStream **stream)
886 {
887     DWORD dwMode;
888     HRESULT ret;
889
890     if(!stream || !filename)
891         return InvalidParameter;
892
893     if(access & GENERIC_WRITE)
894         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
895     else if(access & GENERIC_READ)
896         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
897     else
898         return InvalidParameter;
899
900     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
901
902     return hresult_to_status(ret);
903 }
904
905 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
906 {
907     if(!graphics) return InvalidParameter;
908     if(graphics->hwnd)
909         ReleaseDC(graphics->hwnd, graphics->hdc);
910
911     GdipDeleteMatrix(graphics->worldtrans);
912     HeapFree(GetProcessHeap(), 0, graphics);
913
914     return Ok;
915 }
916
917 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
918     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
919 {
920     INT save_state, num_pts;
921     GpPointF points[MAX_ARC_PTS];
922     GpStatus retval;
923
924     if(!graphics || !pen || width <= 0 || height <= 0)
925         return InvalidParameter;
926
927     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
928
929     save_state = prepare_dc(graphics, pen);
930
931     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
932
933     restore_dc(graphics, save_state);
934
935     return retval;
936 }
937
938 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
939     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
940 {
941     return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
942 }
943
944 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
945     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
946 {
947     INT save_state;
948     GpPointF pt[4];
949     GpStatus retval;
950
951     if(!graphics || !pen)
952         return InvalidParameter;
953
954     pt[0].X = x1;
955     pt[0].Y = y1;
956     pt[1].X = x2;
957     pt[1].Y = y2;
958     pt[2].X = x3;
959     pt[2].Y = y3;
960     pt[3].X = x4;
961     pt[3].Y = y4;
962
963     save_state = prepare_dc(graphics, pen);
964
965     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
966
967     restore_dc(graphics, save_state);
968
969     return retval;
970 }
971
972 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
973     INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
974 {
975     INT save_state;
976     GpPointF pt[4];
977     GpStatus retval;
978
979     if(!graphics || !pen)
980         return InvalidParameter;
981
982     pt[0].X = x1;
983     pt[0].Y = y1;
984     pt[1].X = x2;
985     pt[1].Y = y2;
986     pt[2].X = x3;
987     pt[2].Y = y3;
988     pt[3].X = x4;
989     pt[3].Y = y4;
990
991     save_state = prepare_dc(graphics, pen);
992
993     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
994
995     restore_dc(graphics, save_state);
996
997     return retval;
998 }
999
1000 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1001     GDIPCONST GpPointF *points, INT count)
1002 {
1003     return GdipDrawCurve2(graphics,pen,points,count,1.0);
1004 }
1005
1006 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1007     GDIPCONST GpPoint *points, INT count)
1008 {
1009     GpPointF *pointsF;
1010     GpStatus ret;
1011     INT i;
1012
1013     if(!points || count <= 0)
1014         return InvalidParameter;
1015
1016     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1017     if(!pointsF)
1018         return OutOfMemory;
1019
1020     for(i = 0; i < count; i++){
1021         pointsF[i].X = (REAL)points[i].X;
1022         pointsF[i].Y = (REAL)points[i].Y;
1023     }
1024
1025     ret = GdipDrawCurve(graphics,pen,pointsF,count);
1026     GdipFree(pointsF);
1027
1028     return ret;
1029 }
1030
1031 /* Approximates cardinal spline with Bezier curves. */
1032 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1033     GDIPCONST GpPointF *points, INT count, REAL tension)
1034 {
1035     /* PolyBezier expects count*3-2 points. */
1036     INT i, len_pt = count*3-2, save_state;
1037     GpPointF *pt;
1038     REAL x1, x2, y1, y2;
1039     GpStatus retval;
1040
1041     if(!graphics || !pen)
1042         return InvalidParameter;
1043
1044     pt = GdipAlloc(len_pt * sizeof(GpPointF));
1045     tension = tension * TENSION_CONST;
1046
1047     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1048         tension, &x1, &y1);
1049
1050     pt[0].X = points[0].X;
1051     pt[0].Y = points[0].Y;
1052     pt[1].X = x1;
1053     pt[1].Y = y1;
1054
1055     for(i = 0; i < count-2; i++){
1056         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1057
1058         pt[3*i+2].X = x1;
1059         pt[3*i+2].Y = y1;
1060         pt[3*i+3].X = points[i+1].X;
1061         pt[3*i+3].Y = points[i+1].Y;
1062         pt[3*i+4].X = x2;
1063         pt[3*i+4].Y = y2;
1064     }
1065
1066     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1067         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1068
1069     pt[len_pt-2].X = x1;
1070     pt[len_pt-2].Y = y1;
1071     pt[len_pt-1].X = points[count-1].X;
1072     pt[len_pt-1].Y = points[count-1].Y;
1073
1074     save_state = prepare_dc(graphics, pen);
1075
1076     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1077
1078     GdipFree(pt);
1079     restore_dc(graphics, save_state);
1080
1081     return retval;
1082 }
1083
1084 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1085     GDIPCONST GpPoint *points, INT count, REAL tension)
1086 {
1087     GpPointF *pointsF;
1088     GpStatus ret;
1089     INT i;
1090
1091     if(!points || count <= 0)
1092         return InvalidParameter;
1093
1094     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1095     if(!pointsF)
1096         return OutOfMemory;
1097
1098     for(i = 0; i < count; i++){
1099         pointsF[i].X = (REAL)points[i].X;
1100         pointsF[i].Y = (REAL)points[i].Y;
1101     }
1102
1103     ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1104     GdipFree(pointsF);
1105
1106     return ret;
1107 }
1108
1109 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1110 {
1111     /* IPicture::Render uses LONG coords */
1112     return GdipDrawImageI(graphics,image,roundr(x),roundr(y));
1113 }
1114
1115 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1116     INT y)
1117 {
1118     UINT width, height, srcw, srch;
1119
1120     if(!graphics || !image)
1121         return InvalidParameter;
1122
1123     GdipGetImageWidth(image, &width);
1124     GdipGetImageHeight(image, &height);
1125
1126     srcw = width * (((REAL) INCH_HIMETRIC) /
1127             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1128     srch = height * (((REAL) INCH_HIMETRIC) /
1129             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1130
1131     if(image->type != ImageTypeMetafile){
1132         y += height;
1133         height *= -1;
1134     }
1135
1136     IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1137                     0, 0, srcw, srch, NULL);
1138
1139     return Ok;
1140 }
1141
1142 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1143 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1144      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1145      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1146      DrawImageAbort callback, VOID * callbackData)
1147 {
1148     GpPointF ptf[3];
1149     POINT pti[3];
1150     REAL dx, dy;
1151
1152     TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1153           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1154           callbackData);
1155
1156     if(!graphics || !image || !points || count != 3)
1157          return InvalidParameter;
1158
1159     if(srcUnit == UnitInch)
1160         dx = dy = (REAL) INCH_HIMETRIC;
1161     else if(srcUnit == UnitPixel){
1162         dx = ((REAL) INCH_HIMETRIC) /
1163              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1164         dy = ((REAL) INCH_HIMETRIC) /
1165              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1166     }
1167     else
1168         return NotImplemented;
1169
1170     memcpy(ptf, points, 3 * sizeof(GpPointF));
1171     transform_and_round_points(graphics, pti, ptf, 3);
1172
1173     /* IPicture renders bitmaps with the y-axis reversed
1174      * FIXME: flipping for unknown image type might not be correct. */
1175     if(image->type != ImageTypeMetafile){
1176         INT temp;
1177         temp = pti[0].y;
1178         pti[0].y = pti[2].y;
1179         pti[2].y = temp;
1180     }
1181
1182     if(IPicture_Render(image->picture, graphics->hdc,
1183         pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1184         srcx * dx, srcy * dy,
1185         srcwidth * dx, srcheight * dy,
1186         NULL) != S_OK){
1187         if(callback)
1188             callback(callbackData);
1189         return GenericError;
1190     }
1191
1192     return Ok;
1193 }
1194
1195 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1196      GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1197      INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1198      DrawImageAbort callback, VOID * callbackData)
1199 {
1200     GpPointF pointsF[3];
1201     INT i;
1202
1203     if(!points || count!=3)
1204         return InvalidParameter;
1205
1206     for(i = 0; i < count; i++){
1207         pointsF[i].X = (REAL)points[i].X;
1208         pointsF[i].Y = (REAL)points[i].Y;
1209     }
1210
1211     return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1212                                    (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1213                                    callback, callbackData);
1214 }
1215
1216 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1217     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1218     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1219     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1220     VOID * callbackData)
1221 {
1222     GpPointF points[3];
1223
1224     points[0].X = dstx;
1225     points[0].Y = dsty;
1226     points[1].X = dstx + dstwidth;
1227     points[1].Y = dsty;
1228     points[2].X = dstx;
1229     points[2].Y = dsty + dstheight;
1230
1231     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1232                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1233 }
1234
1235 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1236         INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1237         INT srcwidth, INT srcheight, GpUnit srcUnit,
1238         GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1239         VOID * callbackData)
1240 {
1241    GpPointF points[3];
1242
1243     points[0].X = dstx;
1244     points[0].Y = dsty;
1245     points[1].X = dstx + dstwidth;
1246     points[1].Y = dsty;
1247     points[2].X = dstx;
1248     points[2].Y = dsty + dstheight;
1249
1250     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1251                srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1252 }
1253
1254 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1255     REAL x, REAL y, REAL width, REAL height)
1256 {
1257     RectF bounds;
1258     GpUnit unit;
1259     GpStatus ret;
1260
1261     if(!graphics || !image)
1262         return InvalidParameter;
1263
1264     ret = GdipGetImageBounds(image, &bounds, &unit);
1265     if(ret != Ok)
1266         return ret;
1267
1268     return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1269                                  bounds.X, bounds.Y, bounds.Width, bounds.Height,
1270                                  unit, NULL, NULL, NULL);
1271 }
1272
1273 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1274     INT x, INT y, INT width, INT height)
1275 {
1276     return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1277 }
1278
1279 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1280     REAL y1, REAL x2, REAL y2)
1281 {
1282     INT save_state;
1283     GpPointF pt[2];
1284     GpStatus retval;
1285
1286     if(!pen || !graphics)
1287         return InvalidParameter;
1288
1289     pt[0].X = x1;
1290     pt[0].Y = y1;
1291     pt[1].X = x2;
1292     pt[1].Y = y2;
1293
1294     save_state = prepare_dc(graphics, pen);
1295
1296     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1297
1298     restore_dc(graphics, save_state);
1299
1300     return retval;
1301 }
1302
1303 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1304     INT y1, INT x2, INT y2)
1305 {
1306     INT save_state;
1307     GpPointF pt[2];
1308     GpStatus retval;
1309
1310     if(!pen || !graphics)
1311         return InvalidParameter;
1312
1313     pt[0].X = (REAL)x1;
1314     pt[0].Y = (REAL)y1;
1315     pt[1].X = (REAL)x2;
1316     pt[1].Y = (REAL)y2;
1317
1318     save_state = prepare_dc(graphics, pen);
1319
1320     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1321
1322     restore_dc(graphics, save_state);
1323
1324     return retval;
1325 }
1326
1327 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1328     GpPointF *points, INT count)
1329 {
1330     INT save_state;
1331     GpStatus retval;
1332
1333     if(!pen || !graphics || (count < 2))
1334         return InvalidParameter;
1335
1336     save_state = prepare_dc(graphics, pen);
1337
1338     retval = draw_polyline(graphics, pen, points, count, TRUE);
1339
1340     restore_dc(graphics, save_state);
1341
1342     return retval;
1343 }
1344
1345 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
1346     GpPoint *points, INT count)
1347 {
1348     INT save_state;
1349     GpStatus retval;
1350     GpPointF *ptf = NULL;
1351     int i;
1352
1353     if(!pen || !graphics || (count < 2))
1354         return InvalidParameter;
1355
1356     ptf = GdipAlloc(count * sizeof(GpPointF));
1357     if(!ptf) return OutOfMemory;
1358
1359     for(i = 0; i < count; i ++){
1360         ptf[i].X = (REAL) points[i].X;
1361         ptf[i].Y = (REAL) points[i].Y;
1362     }
1363
1364     save_state = prepare_dc(graphics, pen);
1365
1366     retval = draw_polyline(graphics, pen, ptf, count, TRUE);
1367
1368     restore_dc(graphics, save_state);
1369
1370     GdipFree(ptf);
1371     return retval;
1372 }
1373
1374 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1375 {
1376     INT save_state;
1377     GpStatus retval;
1378
1379     if(!pen || !graphics)
1380         return InvalidParameter;
1381
1382     save_state = prepare_dc(graphics, pen);
1383
1384     retval = draw_poly(graphics, pen, path->pathdata.Points,
1385                        path->pathdata.Types, path->pathdata.Count, TRUE);
1386
1387     restore_dc(graphics, save_state);
1388
1389     return retval;
1390 }
1391
1392 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1393     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1394 {
1395     INT save_state;
1396
1397     if(!graphics || !pen)
1398         return InvalidParameter;
1399
1400     save_state = prepare_dc(graphics, pen);
1401     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1402
1403     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1404
1405     restore_dc(graphics, save_state);
1406
1407     return Ok;
1408 }
1409
1410 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
1411     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1412 {
1413     return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1414 }
1415
1416 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
1417     REAL y, REAL width, REAL height)
1418 {
1419     INT save_state;
1420     GpPointF ptf[4];
1421     POINT pti[4];
1422
1423     if(!pen || !graphics)
1424         return InvalidParameter;
1425
1426     ptf[0].X = x;
1427     ptf[0].Y = y;
1428     ptf[1].X = x + width;
1429     ptf[1].Y = y;
1430     ptf[2].X = x + width;
1431     ptf[2].Y = y + height;
1432     ptf[3].X = x;
1433     ptf[3].Y = y + height;
1434
1435     save_state = prepare_dc(graphics, pen);
1436     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1437
1438     transform_and_round_points(graphics, pti, ptf, 4);
1439     Polygon(graphics->hdc, pti, 4);
1440
1441     restore_dc(graphics, save_state);
1442
1443     return Ok;
1444 }
1445
1446 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1447     INT y, INT width, INT height)
1448 {
1449     return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1450 }
1451
1452 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1453     GDIPCONST GpRectF* rects, INT count)
1454 {
1455     GpPointF *ptf;
1456     POINT *pti;
1457     INT save_state, i;
1458
1459     if(!graphics || !pen || !rects || count < 1)
1460         return InvalidParameter;
1461
1462     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1463     pti = GdipAlloc(4 * count * sizeof(POINT));
1464
1465     if(!ptf || !pti){
1466         GdipFree(ptf);
1467         GdipFree(pti);
1468         return OutOfMemory;
1469     }
1470
1471     for(i = 0; i < count; i++){
1472         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1473         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1474         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1475         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1476     }
1477
1478     save_state = prepare_dc(graphics, pen);
1479     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1480
1481     transform_and_round_points(graphics, pti, ptf, 4 * count);
1482
1483     for(i = 0; i < count; i++)
1484         Polygon(graphics->hdc, &pti[4 * i], 4);
1485
1486     restore_dc(graphics, save_state);
1487
1488     GdipFree(ptf);
1489     GdipFree(pti);
1490
1491     return Ok;
1492 }
1493
1494 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
1495     GDIPCONST GpRect* rects, INT count)
1496 {
1497     GpRectF *rectsF;
1498     GpStatus ret;
1499     INT i;
1500
1501     if(!rects || count<=0)
1502         return InvalidParameter;
1503
1504     rectsF = GdipAlloc(sizeof(GpRectF) * count);
1505     if(!rectsF)
1506         return OutOfMemory;
1507
1508     for(i = 0;i < count;i++){
1509         rectsF[i].X      = (REAL)rects[i].X;
1510         rectsF[i].Y      = (REAL)rects[i].Y;
1511         rectsF[i].Width  = (REAL)rects[i].Width;
1512         rectsF[i].Height = (REAL)rects[i].Height;
1513     }
1514
1515     ret = GdipDrawRectangles(graphics, pen, rectsF, count);
1516     GdipFree(rectsF);
1517
1518     return ret;
1519 }
1520
1521 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1522     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1523     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1524 {
1525     HRGN rgn = NULL;
1526     HFONT gdifont;
1527     LOGFONTW lfw;
1528     TEXTMETRICW textmet;
1529     GpPointF pt[2], rectcpy[4];
1530     POINT corners[4];
1531     WCHAR* stringdup;
1532     REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1533     INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
1534         nheight;
1535     SIZE size;
1536     RECT drawcoord;
1537
1538     if(!graphics || !string || !font || !brush || !rect)
1539         return InvalidParameter;
1540
1541     if((brush->bt != BrushTypeSolidColor)){
1542         FIXME("not implemented for given parameters\n");
1543         return NotImplemented;
1544     }
1545
1546     if(format)
1547         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1548
1549     if(length == -1) length = lstrlenW(string);
1550
1551     stringdup = GdipAlloc(length * sizeof(WCHAR));
1552     if(!stringdup) return OutOfMemory;
1553
1554     save_state = SaveDC(graphics->hdc);
1555     SetBkMode(graphics->hdc, TRANSPARENT);
1556     SetTextColor(graphics->hdc, brush->lb.lbColor);
1557
1558     rectcpy[3].X = rectcpy[0].X = rect->X;
1559     rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1560     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1561     rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1562     transform_and_round_points(graphics, corners, rectcpy, 4);
1563
1564     if(roundr(rect->Width) == 0 && roundr(rect->Height) == 0){
1565         rel_width = rel_height = 1.0;
1566         nwidth = nheight = INT_MAX;
1567     }
1568     else{
1569         rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1570                          (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1571                          / rect->Width;
1572         rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1573                           (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1574                           / rect->Height;
1575
1576         nwidth = roundr(rel_width * rect->Width);
1577         nheight = roundr(rel_height * rect->Height);
1578         rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1579         SelectClipRgn(graphics->hdc, rgn);
1580     }
1581
1582     /* Use gdi to find the font, then perform transformations on it (height,
1583      * width, angle). */
1584     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1585     GetTextMetricsW(graphics->hdc, &textmet);
1586     lfw = font->lfw;
1587
1588     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1589     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1590
1591     pt[0].X = 0.0;
1592     pt[0].Y = 0.0;
1593     pt[1].X = 1.0;
1594     pt[1].Y = 0.0;
1595     GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1596     angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1597     ang_cos = cos(angle);
1598     ang_sin = sin(angle);
1599     lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1600
1601     gdifont = CreateFontIndirectW(&lfw);
1602     DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1603
1604     for(i = 0, j = 0; i < length; i++){
1605         if(!isprintW(string[i]) && (string[i] != '\n'))
1606             continue;
1607
1608         stringdup[j] = string[i];
1609         j++;
1610     }
1611
1612     stringdup[j] = 0;
1613     length = j;
1614
1615     while(sum < length){
1616         drawcoord.left = corners[0].x + roundr(ang_sin * (REAL) height);
1617         drawcoord.top = corners[0].y + roundr(ang_cos * (REAL) height);
1618
1619         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1620                               nwidth, &fit, NULL, &size);
1621         fitcpy = fit;
1622
1623         if(fit == 0){
1624             DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, DT_NOCLIP |
1625                       DT_EXPANDTABS);
1626             break;
1627         }
1628
1629         for(lret = 0; lret < fit; lret++)
1630             if(*(stringdup + sum + lret) == '\n')
1631                 break;
1632
1633         /* Line break code (may look strange, but it imitates windows). */
1634         if(lret < fit)
1635             fit = lret;    /* this is not an off-by-one error */
1636         else if(fit < (length - sum)){
1637             if(*(stringdup + sum + fit) == ' ')
1638                 while(*(stringdup + sum + fit) == ' ')
1639                     fit++;
1640             else
1641                 while(*(stringdup + sum + fit - 1) != ' '){
1642                     fit--;
1643
1644                     if(*(stringdup + sum + fit) == '\t')
1645                         break;
1646
1647                     if(fit == 0){
1648                         fit = fitcpy;
1649                         break;
1650                     }
1651                 }
1652         }
1653         DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
1654                   &drawcoord, DT_NOCLIP | DT_EXPANDTABS);
1655
1656         sum += fit + (lret < fitcpy ? 1 : 0);
1657         height += size.cy;
1658
1659         if(height > nheight)
1660             break;
1661
1662         /* Stop if this was a linewrap (but not if it was a linebreak). */
1663         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1664             break;
1665     }
1666
1667     GdipFree(stringdup);
1668     DeleteObject(rgn);
1669     DeleteObject(gdifont);
1670
1671     RestoreDC(graphics->hdc, save_state);
1672
1673     return Ok;
1674 }
1675
1676 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
1677     REAL y, REAL width, REAL height)
1678 {
1679     INT save_state;
1680     GpPointF ptf[2];
1681     POINT pti[2];
1682
1683     if(!graphics || !brush)
1684         return InvalidParameter;
1685
1686     ptf[0].X = x;
1687     ptf[0].Y = y;
1688     ptf[1].X = x + width;
1689     ptf[1].Y = y + height;
1690
1691     save_state = SaveDC(graphics->hdc);
1692     EndPath(graphics->hdc);
1693     SelectObject(graphics->hdc, brush->gdibrush);
1694     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1695
1696     transform_and_round_points(graphics, pti, ptf, 2);
1697
1698     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1699
1700     RestoreDC(graphics->hdc, save_state);
1701
1702     return Ok;
1703 }
1704
1705 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
1706     INT y, INT width, INT height)
1707 {
1708     return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1709 }
1710
1711 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1712 {
1713     INT save_state;
1714     GpStatus retval;
1715
1716     if(!brush || !graphics || !path)
1717         return InvalidParameter;
1718
1719     save_state = SaveDC(graphics->hdc);
1720     EndPath(graphics->hdc);
1721     SelectObject(graphics->hdc, brush->gdibrush);
1722     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1723                                                                     : WINDING));
1724
1725     BeginPath(graphics->hdc);
1726     retval = draw_poly(graphics, NULL, path->pathdata.Points,
1727                        path->pathdata.Types, path->pathdata.Count, FALSE);
1728
1729     if(retval != Ok)
1730         goto end;
1731
1732     EndPath(graphics->hdc);
1733     FillPath(graphics->hdc);
1734
1735     retval = Ok;
1736
1737 end:
1738     RestoreDC(graphics->hdc, save_state);
1739
1740     return retval;
1741 }
1742
1743 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1744     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1745 {
1746     INT save_state;
1747
1748     if(!graphics || !brush)
1749         return InvalidParameter;
1750
1751     save_state = SaveDC(graphics->hdc);
1752     EndPath(graphics->hdc);
1753     SelectObject(graphics->hdc, brush->gdibrush);
1754     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1755
1756     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1757
1758     RestoreDC(graphics->hdc, save_state);
1759
1760     return Ok;
1761 }
1762
1763 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
1764     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1765 {
1766     return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1767 }
1768
1769 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1770     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1771 {
1772     INT save_state;
1773     GpPointF *ptf = NULL;
1774     POINT *pti = NULL;
1775     GpStatus retval = Ok;
1776
1777     if(!graphics || !brush || !points || !count)
1778         return InvalidParameter;
1779
1780     ptf = GdipAlloc(count * sizeof(GpPointF));
1781     pti = GdipAlloc(count * sizeof(POINT));
1782     if(!ptf || !pti){
1783         retval = OutOfMemory;
1784         goto end;
1785     }
1786
1787     memcpy(ptf, points, count * sizeof(GpPointF));
1788
1789     save_state = SaveDC(graphics->hdc);
1790     EndPath(graphics->hdc);
1791     SelectObject(graphics->hdc, brush->gdibrush);
1792     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1793     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1794                                                                   : WINDING));
1795
1796     transform_and_round_points(graphics, pti, ptf, count);
1797     Polygon(graphics->hdc, pti, count);
1798
1799     RestoreDC(graphics->hdc, save_state);
1800
1801 end:
1802     GdipFree(ptf);
1803     GdipFree(pti);
1804
1805     return retval;
1806 }
1807
1808 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1809     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1810 {
1811     INT save_state, i;
1812     GpPointF *ptf = NULL;
1813     POINT *pti = NULL;
1814     GpStatus retval = Ok;
1815
1816     if(!graphics || !brush || !points || !count)
1817         return InvalidParameter;
1818
1819     ptf = GdipAlloc(count * sizeof(GpPointF));
1820     pti = GdipAlloc(count * sizeof(POINT));
1821     if(!ptf || !pti){
1822         retval = OutOfMemory;
1823         goto end;
1824     }
1825
1826     for(i = 0; i < count; i ++){
1827         ptf[i].X = (REAL) points[i].X;
1828         ptf[i].Y = (REAL) points[i].Y;
1829     }
1830
1831     save_state = SaveDC(graphics->hdc);
1832     EndPath(graphics->hdc);
1833     SelectObject(graphics->hdc, brush->gdibrush);
1834     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1835     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1836                                                                   : WINDING));
1837
1838     transform_and_round_points(graphics, pti, ptf, count);
1839     Polygon(graphics->hdc, pti, count);
1840
1841     RestoreDC(graphics->hdc, save_state);
1842
1843 end:
1844     GdipFree(ptf);
1845     GdipFree(pti);
1846
1847     return retval;
1848 }
1849
1850 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1851     REAL x, REAL y, REAL width, REAL height)
1852 {
1853     INT save_state;
1854     GpPointF ptf[4];
1855     POINT pti[4];
1856
1857     if(!graphics || !brush)
1858         return InvalidParameter;
1859
1860     ptf[0].X = x;
1861     ptf[0].Y = y;
1862     ptf[1].X = x + width;
1863     ptf[1].Y = y;
1864     ptf[2].X = x + width;
1865     ptf[2].Y = y + height;
1866     ptf[3].X = x;
1867     ptf[3].Y = y + height;
1868
1869     save_state = SaveDC(graphics->hdc);
1870     EndPath(graphics->hdc);
1871     SelectObject(graphics->hdc, brush->gdibrush);
1872     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1873
1874     transform_and_round_points(graphics, pti, ptf, 4);
1875
1876     Polygon(graphics->hdc, pti, 4);
1877
1878     RestoreDC(graphics->hdc, save_state);
1879
1880     return Ok;
1881 }
1882
1883 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1884     INT x, INT y, INT width, INT height)
1885 {
1886     INT save_state;
1887     GpPointF ptf[4];
1888     POINT pti[4];
1889
1890     if(!graphics || !brush)
1891         return InvalidParameter;
1892
1893     ptf[0].X = x;
1894     ptf[0].Y = y;
1895     ptf[1].X = x + width;
1896     ptf[1].Y = y;
1897     ptf[2].X = x + width;
1898     ptf[2].Y = y + height;
1899     ptf[3].X = x;
1900     ptf[3].Y = y + height;
1901
1902     save_state = SaveDC(graphics->hdc);
1903     EndPath(graphics->hdc);
1904     SelectObject(graphics->hdc, brush->gdibrush);
1905     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1906
1907     transform_and_round_points(graphics, pti, ptf, 4);
1908
1909     Polygon(graphics->hdc, pti, 4);
1910
1911     RestoreDC(graphics->hdc, save_state);
1912
1913     return Ok;
1914 }
1915
1916 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
1917     INT count)
1918 {
1919     GpStatus ret;
1920     INT i;
1921
1922     if(!rects)
1923         return InvalidParameter;
1924
1925     for(i = 0; i < count; i++){
1926         ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
1927         if(ret != Ok)   return ret;
1928     }
1929
1930     return Ok;
1931 }
1932
1933 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
1934     INT count)
1935 {
1936     GpRectF *rectsF;
1937     GpStatus ret;
1938     INT i;
1939
1940     if(!rects || count <= 0)
1941         return InvalidParameter;
1942
1943     rectsF = GdipAlloc(sizeof(GpRectF)*count);
1944     if(!rectsF)
1945         return OutOfMemory;
1946
1947     for(i = 0; i < count; i++){
1948         rectsF[i].X      = (REAL)rects[i].X;
1949         rectsF[i].Y      = (REAL)rects[i].Y;
1950         rectsF[i].X      = (REAL)rects[i].Width;
1951         rectsF[i].Height = (REAL)rects[i].Height;
1952     }
1953
1954     ret = GdipFillRectangles(graphics,brush,rectsF,count);
1955     GdipFree(rectsF);
1956
1957     return ret;
1958 }
1959
1960 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
1961 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
1962     CompositingMode *mode)
1963 {
1964     if(!graphics || !mode)
1965         return InvalidParameter;
1966
1967     *mode = graphics->compmode;
1968
1969     return Ok;
1970 }
1971
1972 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1973 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1974     CompositingQuality *quality)
1975 {
1976     if(!graphics || !quality)
1977         return InvalidParameter;
1978
1979     *quality = graphics->compqual;
1980
1981     return Ok;
1982 }
1983
1984 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1985 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1986     InterpolationMode *mode)
1987 {
1988     if(!graphics || !mode)
1989         return InvalidParameter;
1990
1991     *mode = graphics->interpolation;
1992
1993     return Ok;
1994 }
1995
1996 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1997 {
1998     if(!graphics || !scale)
1999         return InvalidParameter;
2000
2001     *scale = graphics->scale;
2002
2003     return Ok;
2004 }
2005
2006 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
2007 {
2008     if(!graphics || !unit)
2009         return InvalidParameter;
2010
2011     *unit = graphics->unit;
2012
2013     return Ok;
2014 }
2015
2016 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
2017 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2018     *mode)
2019 {
2020     if(!graphics || !mode)
2021         return InvalidParameter;
2022
2023     *mode = graphics->pixeloffset;
2024
2025     return Ok;
2026 }
2027
2028 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
2029 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
2030 {
2031     if(!graphics || !mode)
2032         return InvalidParameter;
2033
2034     *mode = graphics->smoothing;
2035
2036     return Ok;
2037 }
2038
2039 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
2040 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
2041     TextRenderingHint *hint)
2042 {
2043     if(!graphics || !hint)
2044         return InvalidParameter;
2045
2046     *hint = graphics->texthint;
2047
2048     return Ok;
2049 }
2050
2051 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2052 {
2053     if(!graphics || !matrix)
2054         return InvalidParameter;
2055
2056     *matrix = *graphics->worldtrans;
2057     return Ok;
2058 }
2059
2060 /* Find the smallest rectangle that bounds the text when it is printed in rect
2061  * according to the format options listed in format. If rect has 0 width and
2062  * height, then just find the smallest rectangle that bounds the text when it's
2063  * printed at location (rect->X, rect-Y). */
2064 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
2065     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
2066     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
2067     INT *codepointsfitted, INT *linesfilled)
2068 {
2069     HFONT oldfont;
2070     WCHAR* stringdup;
2071     INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
2072         nheight;
2073     SIZE size;
2074
2075     if(!graphics || !string || !font || !rect)
2076         return InvalidParameter;
2077
2078     if(codepointsfitted || linesfilled){
2079         FIXME("not implemented for given parameters\n");
2080         return NotImplemented;
2081     }
2082
2083     if(format)
2084         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2085
2086     if(length == -1) length = lstrlenW(string);
2087
2088     stringdup = GdipAlloc(length * sizeof(WCHAR));
2089     if(!stringdup) return OutOfMemory;
2090
2091     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2092     nwidth = roundr(rect->Width);
2093     nheight = roundr(rect->Height);
2094
2095     if((nwidth == 0) && (nheight == 0))
2096         nwidth = nheight = INT_MAX;
2097
2098     for(i = 0, j = 0; i < length; i++){
2099         if(!isprintW(string[i]) && (string[i] != '\n'))
2100             continue;
2101
2102         stringdup[j] = string[i];
2103         j++;
2104     }
2105
2106     stringdup[j] = 0;
2107     length = j;
2108
2109     while(sum < length){
2110         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2111                               nwidth, &fit, NULL, &size);
2112         fitcpy = fit;
2113
2114         if(fit == 0)
2115             break;
2116
2117         for(lret = 0; lret < fit; lret++)
2118             if(*(stringdup + sum + lret) == '\n')
2119                 break;
2120
2121         /* Line break code (may look strange, but it imitates windows). */
2122         if(lret < fit)
2123             fit = lret;    /* this is not an off-by-one error */
2124         else if(fit < (length - sum)){
2125             if(*(stringdup + sum + fit) == ' ')
2126                 while(*(stringdup + sum + fit) == ' ')
2127                     fit++;
2128             else
2129                 while(*(stringdup + sum + fit - 1) != ' '){
2130                     fit--;
2131
2132                     if(*(stringdup + sum + fit) == '\t')
2133                         break;
2134
2135                     if(fit == 0){
2136                         fit = fitcpy;
2137                         break;
2138                     }
2139                 }
2140         }
2141
2142         GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
2143                               nwidth, &j, NULL, &size);
2144
2145         sum += fit + (lret < fitcpy ? 1 : 0);
2146         height += size.cy;
2147         max_width = max(max_width, size.cx);
2148
2149         if(height > nheight)
2150             break;
2151
2152         /* Stop if this was a linewrap (but not if it was a linebreak). */
2153         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2154             break;
2155     }
2156
2157     bounds->X = rect->X;
2158     bounds->Y = rect->Y;
2159     bounds->Width = (REAL)max_width;
2160     bounds->Height = (REAL) min(height, nheight);
2161
2162     GdipFree(stringdup);
2163     DeleteObject(SelectObject(graphics->hdc, oldfont));
2164
2165     return Ok;
2166 }
2167
2168 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
2169 {
2170     static int calls;
2171
2172     if(!graphics)
2173         return InvalidParameter;
2174
2175     if(!(calls++))
2176         FIXME("graphics state not implemented\n");
2177
2178     return NotImplemented;
2179 }
2180
2181 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
2182     GpMatrixOrder order)
2183 {
2184     if(!graphics)
2185         return InvalidParameter;
2186
2187     return GdipRotateMatrix(graphics->worldtrans, angle, order);
2188 }
2189
2190 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
2191 {
2192     static int calls;
2193
2194     if(!graphics || !state)
2195         return InvalidParameter;
2196
2197     if(!(calls++))
2198         FIXME("graphics state not implemented\n");
2199
2200     return NotImplemented;
2201 }
2202
2203 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
2204     REAL sy, GpMatrixOrder order)
2205 {
2206     if(!graphics)
2207         return InvalidParameter;
2208
2209     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
2210 }
2211
2212 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
2213     CompositingMode mode)
2214 {
2215     if(!graphics)
2216         return InvalidParameter;
2217
2218     graphics->compmode = mode;
2219
2220     return Ok;
2221 }
2222
2223 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
2224     CompositingQuality quality)
2225 {
2226     if(!graphics)
2227         return InvalidParameter;
2228
2229     graphics->compqual = quality;
2230
2231     return Ok;
2232 }
2233
2234 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
2235     InterpolationMode mode)
2236 {
2237     if(!graphics)
2238         return InvalidParameter;
2239
2240     graphics->interpolation = mode;
2241
2242     return Ok;
2243 }
2244
2245 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
2246 {
2247     if(!graphics || (scale <= 0.0))
2248         return InvalidParameter;
2249
2250     graphics->scale = scale;
2251
2252     return Ok;
2253 }
2254
2255 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
2256 {
2257     if(!graphics || (unit == UnitWorld))
2258         return InvalidParameter;
2259
2260     graphics->unit = unit;
2261
2262     return Ok;
2263 }
2264
2265 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2266     mode)
2267 {
2268     if(!graphics)
2269         return InvalidParameter;
2270
2271     graphics->pixeloffset = mode;
2272
2273     return Ok;
2274 }
2275
2276 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
2277 {
2278     if(!graphics)
2279         return InvalidParameter;
2280
2281     graphics->smoothing = mode;
2282
2283     return Ok;
2284 }
2285
2286 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
2287     TextRenderingHint hint)
2288 {
2289     if(!graphics)
2290         return InvalidParameter;
2291
2292     graphics->texthint = hint;
2293
2294     return Ok;
2295 }
2296
2297 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2298 {
2299     if(!graphics || !matrix)
2300         return InvalidParameter;
2301
2302     GdipDeleteMatrix(graphics->worldtrans);
2303     return GdipCloneMatrix(matrix, &graphics->worldtrans);
2304 }
2305
2306 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
2307     REAL dy, GpMatrixOrder order)
2308 {
2309     if(!graphics)
2310         return InvalidParameter;
2311
2312     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
2313 }
2314
2315 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
2316                                      INT width, INT height,
2317                                      CombineMode combineMode)
2318 {
2319     static int calls;
2320
2321     if(!(calls++))
2322         FIXME("not implemented\n");
2323
2324     return NotImplemented;
2325 }
2326
2327 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
2328                                      CombineMode combineMode)
2329 {
2330     static int calls;
2331
2332     if(!(calls++))
2333         FIXME("not implemented\n");
2334
2335     return NotImplemented;
2336 }
2337
2338 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpGraphics *graphics,
2339     UINT limitDpi)
2340 {
2341     static int calls;
2342
2343     if(!(calls++))
2344         FIXME("not implemented\n");
2345
2346     return NotImplemented;
2347 }
2348
2349 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
2350     INT count)
2351 {
2352     INT save_state;
2353     POINT *pti;
2354
2355     if(!graphics || !pen || count<=0)
2356         return InvalidParameter;
2357
2358     pti = GdipAlloc(sizeof(POINT) * count);
2359
2360     save_state = prepare_dc(graphics, pen);
2361     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2362
2363     transform_and_round_points(graphics, pti, (GpPointF*)points, count);
2364     Polygon(graphics->hdc, pti, count);
2365
2366     restore_dc(graphics, save_state);
2367     GdipFree(pti);
2368
2369     return Ok;
2370 }
2371
2372 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
2373     INT count)
2374 {
2375     GpStatus ret;
2376     GpPointF *ptf;
2377     INT i;
2378
2379     if(count<=0)    return InvalidParameter;
2380     ptf = GdipAlloc(sizeof(GpPointF) * count);
2381
2382     for(i = 0;i < count; i++){
2383         ptf[i].X = (REAL)points[i].X;
2384         ptf[i].Y = (REAL)points[i].Y;
2385     }
2386
2387     ret = GdipDrawPolygon(graphics,pen,ptf,count);
2388     GdipFree(ptf);
2389
2390     return ret;
2391 }
2392
2393 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
2394 {
2395     if(!graphics || !dpi)
2396         return InvalidParameter;
2397
2398     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
2399
2400     return Ok;
2401 }
2402
2403 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
2404 {
2405     if(!graphics || !dpi)
2406         return InvalidParameter;
2407
2408     *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
2409
2410     return Ok;
2411 }
2412
2413 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
2414     GpMatrixOrder order)
2415 {
2416     GpMatrix m;
2417     GpStatus ret;
2418
2419     if(!graphics || !matrix)
2420         return InvalidParameter;
2421
2422     m = *(graphics->worldtrans);
2423
2424     ret = GdipMultiplyMatrix(&m, (GpMatrix*)matrix, order);
2425     if(ret == Ok)
2426         *(graphics->worldtrans) = m;
2427
2428     return ret;
2429 }
2430
2431 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
2432 {
2433     FIXME("(%p, %p): stub\n", graphics, hdc);
2434
2435     *hdc = NULL;
2436     return NotImplemented;
2437 }
2438
2439 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
2440 {
2441     FIXME("(%p, %p): stub\n", graphics, hdc);
2442
2443     return NotImplemented;
2444 }