gdiplus: Handle StringFormatFlagsNoWrap in GdipDrawString.
[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
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "wingdi.h"
26 #include "wine/unicode.h"
27
28 #define COBJMACROS
29 #include "objbase.h"
30 #include "ocidl.h"
31 #include "olectl.h"
32 #include "ole2.h"
33
34 #include "winreg.h"
35 #include "shlwapi.h"
36
37 #include "gdiplus.h"
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
42
43 /* looks-right constants */
44 #define TENSION_CONST (0.3)
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 /* GdipDrawCurve helper function.
197  * Calculates Bezier points from cardinal spline points. */
198 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
199     REAL *y1, REAL *x2, REAL *y2)
200 {
201     REAL xdiff, ydiff;
202
203     /* calculate tangent */
204     xdiff = pts[2].X - pts[0].X;
205     ydiff = pts[2].Y - pts[0].Y;
206
207     /* apply tangent to get control points */
208     *x1 = pts[1].X - tension * xdiff;
209     *y1 = pts[1].Y - tension * ydiff;
210     *x2 = pts[1].X + tension * xdiff;
211     *y2 = pts[1].Y + tension * ydiff;
212 }
213
214 /* GdipDrawCurve helper function.
215  * Calculates Bezier points from cardinal spline endpoints. */
216 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
217     REAL tension, REAL *x, REAL *y)
218 {
219     /* tangent at endpoints is the line from the endpoint to the adjacent point */
220     *x = roundr(tension * (xadj - xend) + xend);
221     *y = roundr(tension * (yadj - yend) + yend);
222 }
223
224 /* Draws the linecap the specified color and size on the hdc.  The linecap is in
225  * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
226  * should not be called on an hdc that has a path you care about. */
227 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
228     const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
229 {
230     HGDIOBJ oldbrush = NULL, oldpen = NULL;
231     GpMatrix *matrix = NULL;
232     HBRUSH brush = NULL;
233     HPEN pen = NULL;
234     PointF ptf[4], *custptf = NULL;
235     POINT pt[4], *custpt = NULL;
236     BYTE *tp = NULL;
237     REAL theta, dsmall, dbig, dx, dy = 0.0;
238     INT i, count;
239     LOGBRUSH lb;
240     BOOL customstroke;
241
242     if((x1 == x2) && (y1 == y2))
243         return;
244
245     theta = gdiplus_atan2(y2 - y1, x2 - x1);
246
247     customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
248     if(!customstroke){
249         brush = CreateSolidBrush(color);
250         lb.lbStyle = BS_SOLID;
251         lb.lbColor = color;
252         lb.lbHatch = 0;
253         pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
254                            PS_JOIN_MITER, 1, &lb, 0,
255                            NULL);
256         oldbrush = SelectObject(graphics->hdc, brush);
257         oldpen = SelectObject(graphics->hdc, pen);
258     }
259
260     switch(cap){
261         case LineCapFlat:
262             break;
263         case LineCapSquare:
264         case LineCapSquareAnchor:
265         case LineCapDiamondAnchor:
266             size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
267             if(cap == LineCapDiamondAnchor){
268                 dsmall = cos(theta + M_PI_2) * size;
269                 dbig = sin(theta + M_PI_2) * size;
270             }
271             else{
272                 dsmall = cos(theta + M_PI_4) * size;
273                 dbig = sin(theta + M_PI_4) * size;
274             }
275
276             ptf[0].X = x2 - dsmall;
277             ptf[1].X = x2 + dbig;
278
279             ptf[0].Y = y2 - dbig;
280             ptf[3].Y = y2 + dsmall;
281
282             ptf[1].Y = y2 - dsmall;
283             ptf[2].Y = y2 + dbig;
284
285             ptf[3].X = x2 - dbig;
286             ptf[2].X = x2 + dsmall;
287
288             transform_and_round_points(graphics, pt, ptf, 4);
289             Polygon(graphics->hdc, pt, 4);
290
291             break;
292         case LineCapArrowAnchor:
293             size = size * 4.0 / sqrt(3.0);
294
295             dx = cos(M_PI / 6.0 + theta) * size;
296             dy = sin(M_PI / 6.0 + theta) * size;
297
298             ptf[0].X = x2 - dx;
299             ptf[0].Y = y2 - dy;
300
301             dx = cos(- M_PI / 6.0 + theta) * size;
302             dy = sin(- M_PI / 6.0 + theta) * size;
303
304             ptf[1].X = x2 - dx;
305             ptf[1].Y = y2 - dy;
306
307             ptf[2].X = x2;
308             ptf[2].Y = y2;
309
310             transform_and_round_points(graphics, pt, ptf, 3);
311             Polygon(graphics->hdc, pt, 3);
312
313             break;
314         case LineCapRoundAnchor:
315             dx = dy = ANCHOR_WIDTH * size / 2.0;
316
317             ptf[0].X = x2 - dx;
318             ptf[0].Y = y2 - dy;
319             ptf[1].X = x2 + dx;
320             ptf[1].Y = y2 + dy;
321
322             transform_and_round_points(graphics, pt, ptf, 2);
323             Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
324
325             break;
326         case LineCapTriangle:
327             size = size / 2.0;
328             dx = cos(M_PI_2 + theta) * size;
329             dy = sin(M_PI_2 + theta) * size;
330
331             ptf[0].X = x2 - dx;
332             ptf[0].Y = y2 - dy;
333             ptf[1].X = x2 + dx;
334             ptf[1].Y = y2 + dy;
335
336             dx = cos(theta) * size;
337             dy = sin(theta) * size;
338
339             ptf[2].X = x2 + dx;
340             ptf[2].Y = y2 + dy;
341
342             transform_and_round_points(graphics, pt, ptf, 3);
343             Polygon(graphics->hdc, pt, 3);
344
345             break;
346         case LineCapRound:
347             dx = dy = size / 2.0;
348
349             ptf[0].X = x2 - dx;
350             ptf[0].Y = y2 - dy;
351             ptf[1].X = x2 + dx;
352             ptf[1].Y = y2 + dy;
353
354             dx = -cos(M_PI_2 + theta) * size;
355             dy = -sin(M_PI_2 + theta) * size;
356
357             ptf[2].X = x2 - dx;
358             ptf[2].Y = y2 - dy;
359             ptf[3].X = x2 + dx;
360             ptf[3].Y = y2 + dy;
361
362             transform_and_round_points(graphics, pt, ptf, 4);
363             Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
364                 pt[2].y, pt[3].x, pt[3].y);
365
366             break;
367         case LineCapCustom:
368             if(!custom)
369                 break;
370
371             count = custom->pathdata.Count;
372             custptf = GdipAlloc(count * sizeof(PointF));
373             custpt = GdipAlloc(count * sizeof(POINT));
374             tp = GdipAlloc(count);
375
376             if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
377                 goto custend;
378
379             memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
380
381             GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
382             GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
383                              MatrixOrderAppend);
384             GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
385             GdipTransformMatrixPoints(matrix, custptf, count);
386
387             transform_and_round_points(graphics, custpt, custptf, count);
388
389             for(i = 0; i < count; i++)
390                 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
391
392             if(custom->fill){
393                 BeginPath(graphics->hdc);
394                 PolyDraw(graphics->hdc, custpt, tp, count);
395                 EndPath(graphics->hdc);
396                 StrokeAndFillPath(graphics->hdc);
397             }
398             else
399                 PolyDraw(graphics->hdc, custpt, tp, count);
400
401 custend:
402             GdipFree(custptf);
403             GdipFree(custpt);
404             GdipFree(tp);
405             GdipDeleteMatrix(matrix);
406             break;
407         default:
408             break;
409     }
410
411     if(!customstroke){
412         SelectObject(graphics->hdc, oldbrush);
413         SelectObject(graphics->hdc, oldpen);
414         DeleteObject(brush);
415         DeleteObject(pen);
416     }
417 }
418
419 /* Shortens the line by the given percent by changing x2, y2.
420  * If percent is > 1.0 then the line will change direction.
421  * If percent is negative it can lengthen the line. */
422 static void shorten_line_percent(REAL x1, REAL  y1, REAL *x2, REAL *y2, REAL percent)
423 {
424     REAL dist, theta, dx, dy;
425
426     if((y1 == *y2) && (x1 == *x2))
427         return;
428
429     dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
430     theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
431     dx = cos(theta) * dist;
432     dy = sin(theta) * dist;
433
434     *x2 = *x2 + dx;
435     *y2 = *y2 + dy;
436 }
437
438 /* Shortens the line by the given amount by changing x2, y2.
439  * If the amount is greater than the distance, the line will become length 0.
440  * If the amount is negative, it can lengthen the line. */
441 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
442 {
443     REAL dx, dy, percent;
444
445     dx = *x2 - x1;
446     dy = *y2 - y1;
447     if(dx == 0 && dy == 0)
448         return;
449
450     percent = amt / sqrt(dx * dx + dy * dy);
451     if(percent >= 1.0){
452         *x2 = x1;
453         *y2 = y1;
454         return;
455     }
456
457     shorten_line_percent(x1, y1, x2, y2, percent);
458 }
459
460 /* Draws lines between the given points, and if caps is true then draws an endcap
461  * at the end of the last line. */
462 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
463     GDIPCONST GpPointF * pt, INT count, BOOL caps)
464 {
465     POINT *pti = NULL;
466     GpPointF *ptcopy = NULL;
467     GpStatus status = GenericError;
468
469     if(!count)
470         return Ok;
471
472     pti = GdipAlloc(count * sizeof(POINT));
473     ptcopy = GdipAlloc(count * sizeof(GpPointF));
474
475     if(!pti || !ptcopy){
476         status = OutOfMemory;
477         goto end;
478     }
479
480     memcpy(ptcopy, pt, count * sizeof(GpPointF));
481
482     if(caps){
483         if(pen->endcap == LineCapArrowAnchor)
484             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
485                              &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
486         else if((pen->endcap == LineCapCustom) && pen->customend)
487             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
488                              &ptcopy[count-1].X, &ptcopy[count-1].Y,
489                              pen->customend->inset * pen->width);
490
491         if(pen->startcap == LineCapArrowAnchor)
492             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
493                              &ptcopy[0].X, &ptcopy[0].Y, pen->width);
494         else if((pen->startcap == LineCapCustom) && pen->customstart)
495             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
496                              &ptcopy[0].X, &ptcopy[0].Y,
497                              pen->customstart->inset * pen->width);
498
499         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
500                  pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
501         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
502                          pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);\
503     }
504
505     transform_and_round_points(graphics, pti, ptcopy, count);
506
507     Polyline(graphics->hdc, pti, count);
508
509 end:
510     GdipFree(pti);
511     GdipFree(ptcopy);
512
513     return status;
514 }
515
516 /* Conducts a linear search to find the bezier points that will back off
517  * the endpoint of the curve by a distance of amt. Linear search works
518  * better than binary in this case because there are multiple solutions,
519  * and binary searches often find a bad one. I don't think this is what
520  * Windows does but short of rendering the bezier without GDI's help it's
521  * the best we can do. If rev then work from the start of the passed points
522  * instead of the end. */
523 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
524 {
525     GpPointF origpt[4];
526     REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
527     INT i, first = 0, second = 1, third = 2, fourth = 3;
528
529     if(rev){
530         first = 3;
531         second = 2;
532         third = 1;
533         fourth = 0;
534     }
535
536     origx = pt[fourth].X;
537     origy = pt[fourth].Y;
538     memcpy(origpt, pt, sizeof(GpPointF) * 4);
539
540     for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
541         /* reset bezier points to original values */
542         memcpy(pt, origpt, sizeof(GpPointF) * 4);
543         /* Perform magic on bezier points. Order is important here.*/
544         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
545         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
546         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
547         shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
548         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
549         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
550
551         dx = pt[fourth].X - origx;
552         dy = pt[fourth].Y - origy;
553
554         diff = sqrt(dx * dx + dy * dy);
555         percent += 0.0005 * amt;
556     }
557 }
558
559 /* Draws bezier curves between given points, and if caps is true then draws an
560  * endcap at the end of the last line. */
561 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
562     GDIPCONST GpPointF * pt, INT count, BOOL caps)
563 {
564     POINT *pti;
565     GpPointF *ptcopy;
566     GpStatus status = GenericError;
567
568     if(!count)
569         return Ok;
570
571     pti = GdipAlloc(count * sizeof(POINT));
572     ptcopy = GdipAlloc(count * sizeof(GpPointF));
573
574     if(!pti || !ptcopy){
575         status = OutOfMemory;
576         goto end;
577     }
578
579     memcpy(ptcopy, pt, count * sizeof(GpPointF));
580
581     if(caps){
582         if(pen->endcap == LineCapArrowAnchor)
583             shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
584         else if((pen->endcap == LineCapCustom) && pen->customend)
585             shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
586                                FALSE);
587
588         if(pen->startcap == LineCapArrowAnchor)
589             shorten_bezier_amt(ptcopy, pen->width, TRUE);
590         else if((pen->startcap == LineCapCustom) && pen->customstart)
591             shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
592
593         /* the direction of the line cap is parallel to the direction at the
594          * end of the bezier (which, if it has been shortened, is not the same
595          * as the direction from pt[count-2] to pt[count-1]) */
596         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
597             pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
598             pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
599             pt[count - 1].X, pt[count - 1].Y);
600
601         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
602             pt[0].X - (ptcopy[0].X - ptcopy[1].X),
603             pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
604     }
605
606     transform_and_round_points(graphics, pti, ptcopy, count);
607
608     PolyBezier(graphics->hdc, pti, count);
609
610     status = Ok;
611
612 end:
613     GdipFree(pti);
614     GdipFree(ptcopy);
615
616     return status;
617 }
618
619 /* Draws a combination of bezier curves and lines between points. */
620 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
621     GDIPCONST BYTE * types, INT count, BOOL caps)
622 {
623     POINT *pti = GdipAlloc(count * sizeof(POINT));
624     BYTE *tp = GdipAlloc(count);
625     GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
626     INT i, j;
627     GpStatus status = GenericError;
628
629     if(!count){
630         status = Ok;
631         goto end;
632     }
633     if(!pti || !tp || !ptcopy){
634         status = OutOfMemory;
635         goto end;
636     }
637
638     for(i = 1; i < count; i++){
639         if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
640             if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
641                 || !(types[i + 1] & PathPointTypeBezier)){
642                 ERR("Bad bezier points\n");
643                 goto end;
644             }
645             i += 2;
646         }
647     }
648
649     memcpy(ptcopy, pt, count * sizeof(GpPointF));
650
651     /* If we are drawing caps, go through the points and adjust them accordingly,
652      * and draw the caps. */
653     if(caps){
654         switch(types[count - 1] & PathPointTypePathTypeMask){
655             case PathPointTypeBezier:
656                 if(pen->endcap == LineCapArrowAnchor)
657                     shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
658                 else if((pen->endcap == LineCapCustom) && pen->customend)
659                     shorten_bezier_amt(&ptcopy[count - 4],
660                                        pen->width * pen->customend->inset, FALSE);
661
662                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
663                     pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
664                     pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
665                     pt[count - 1].X, pt[count - 1].Y);
666
667                 break;
668             case PathPointTypeLine:
669                 if(pen->endcap == LineCapArrowAnchor)
670                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
671                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
672                                      pen->width);
673                 else if((pen->endcap == LineCapCustom) && pen->customend)
674                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
675                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
676                                      pen->customend->inset * pen->width);
677
678                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
679                          pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
680                          pt[count - 1].Y);
681
682                 break;
683             default:
684                 ERR("Bad path last point\n");
685                 goto end;
686         }
687
688         /* Find start of points */
689         for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
690             == PathPointTypeStart); j++);
691
692         switch(types[j] & PathPointTypePathTypeMask){
693             case PathPointTypeBezier:
694                 if(pen->startcap == LineCapArrowAnchor)
695                     shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
696                 else if((pen->startcap == LineCapCustom) && pen->customstart)
697                     shorten_bezier_amt(&ptcopy[j - 1],
698                                        pen->width * pen->customstart->inset, TRUE);
699
700                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
701                     pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
702                     pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
703                     pt[j - 1].X, pt[j - 1].Y);
704
705                 break;
706             case PathPointTypeLine:
707                 if(pen->startcap == LineCapArrowAnchor)
708                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
709                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
710                                      pen->width);
711                 else if((pen->startcap == LineCapCustom) && pen->customstart)
712                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
713                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
714                                      pen->customstart->inset * pen->width);
715
716                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
717                          pt[j].X, pt[j].Y, pt[j - 1].X,
718                          pt[j - 1].Y);
719
720                 break;
721             default:
722                 ERR("Bad path points\n");
723                 goto end;
724         }
725     }
726
727     transform_and_round_points(graphics, pti, ptcopy, count);
728
729     for(i = 0; i < count; i++){
730         tp[i] = convert_path_point_type(types[i]);
731     }
732
733     PolyDraw(graphics->hdc, pti, tp, count);
734
735     status = Ok;
736
737 end:
738     GdipFree(pti);
739     GdipFree(ptcopy);
740     GdipFree(tp);
741
742     return status;
743 }
744
745 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
746 {
747     GpStatus retval;
748
749     if(hdc == NULL)
750         return OutOfMemory;
751
752     if(graphics == NULL)
753         return InvalidParameter;
754
755     *graphics = GdipAlloc(sizeof(GpGraphics));
756     if(!*graphics)  return OutOfMemory;
757
758     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
759         GdipFree(*graphics);
760         return retval;
761     }
762
763     (*graphics)->hdc = hdc;
764     (*graphics)->hwnd = NULL;
765     (*graphics)->smoothing = SmoothingModeDefault;
766     (*graphics)->compqual = CompositingQualityDefault;
767     (*graphics)->interpolation = InterpolationModeDefault;
768     (*graphics)->pixeloffset = PixelOffsetModeDefault;
769     (*graphics)->compmode = CompositingModeSourceOver;
770     (*graphics)->unit = UnitDisplay;
771     (*graphics)->scale = 1.0;
772
773     return Ok;
774 }
775
776 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
777 {
778     GpStatus ret;
779
780     if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
781         return ret;
782
783     (*graphics)->hwnd = hwnd;
784
785     return Ok;
786 }
787
788 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
789     GpMetafile **metafile)
790 {
791     static int calls;
792
793     if(!hemf || !metafile)
794         return InvalidParameter;
795
796     if(!(calls++))
797         FIXME("not implemented\n");
798
799     return NotImplemented;
800 }
801
802 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
803     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
804 {
805     IStream *stream = NULL;
806     UINT read;
807     BYTE* copy;
808     HENHMETAFILE hemf;
809     GpStatus retval = GenericError;
810
811     if(!hwmf || !metafile || !placeable)
812         return InvalidParameter;
813
814     *metafile = NULL;
815     read = GetMetaFileBitsEx(hwmf, 0, NULL);
816     if(!read)
817         return GenericError;
818     copy = GdipAlloc(read);
819     GetMetaFileBitsEx(hwmf, read, copy);
820
821     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
822     GdipFree(copy);
823
824     read = GetEnhMetaFileBits(hemf, 0, NULL);
825     copy = GdipAlloc(read);
826     GetEnhMetaFileBits(hemf, read, copy);
827     DeleteEnhMetaFile(hemf);
828
829     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
830         ERR("could not make stream\n");
831         GdipFree(copy);
832         goto err;
833     }
834
835     *metafile = GdipAlloc(sizeof(GpMetafile));
836     if(!*metafile){
837         retval = OutOfMemory;
838         goto err;
839     }
840
841     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
842         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
843         goto err;
844
845
846     (*metafile)->image.type = ImageTypeMetafile;
847     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
848     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
849     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
850                     - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
851     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
852                    - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
853     (*metafile)->unit = UnitInch;
854
855     if(delete)
856         DeleteMetaFile(hwmf);
857
858     return Ok;
859
860 err:
861     GdipFree(*metafile);
862     IStream_Release(stream);
863     return retval;
864 }
865
866 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
867     UINT access, IStream **stream)
868 {
869     DWORD dwMode;
870     HRESULT ret;
871
872     if(!stream || !filename)
873         return InvalidParameter;
874
875     if(access & GENERIC_WRITE)
876         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
877     else if(access & GENERIC_READ)
878         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
879     else
880         return InvalidParameter;
881
882     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
883
884     return hresult_to_status(ret);
885 }
886
887 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
888 {
889     if(!graphics) return InvalidParameter;
890     if(graphics->hwnd)
891         ReleaseDC(graphics->hwnd, graphics->hdc);
892
893     GdipDeleteMatrix(graphics->worldtrans);
894     HeapFree(GetProcessHeap(), 0, graphics);
895
896     return Ok;
897 }
898
899 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
900     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
901 {
902     INT save_state, num_pts;
903     GpPointF points[MAX_ARC_PTS];
904     GpStatus retval;
905
906     if(!graphics || !pen)
907         return InvalidParameter;
908
909     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
910
911     save_state = prepare_dc(graphics, pen);
912
913     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
914
915     restore_dc(graphics, save_state);
916
917     return retval;
918 }
919
920 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
921     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
922 {
923     INT save_state;
924     GpPointF pt[4];
925     GpStatus retval;
926
927     if(!graphics || !pen)
928         return InvalidParameter;
929
930     pt[0].X = x1;
931     pt[0].Y = y1;
932     pt[1].X = x2;
933     pt[1].Y = y2;
934     pt[2].X = x3;
935     pt[2].Y = y3;
936     pt[3].X = x4;
937     pt[3].Y = y4;
938
939     save_state = prepare_dc(graphics, pen);
940
941     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
942
943     restore_dc(graphics, save_state);
944
945     return retval;
946 }
947
948 /* Approximates cardinal spline with Bezier curves. */
949 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
950     GDIPCONST GpPointF *points, INT count, REAL tension)
951 {
952     /* PolyBezier expects count*3-2 points. */
953     INT i, len_pt = count*3-2, save_state;
954     GpPointF *pt;
955     REAL x1, x2, y1, y2;
956     GpStatus retval;
957
958     if(!graphics || !pen)
959         return InvalidParameter;
960
961     pt = GdipAlloc(len_pt * sizeof(GpPointF));
962     tension = tension * TENSION_CONST;
963
964     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
965         tension, &x1, &y1);
966
967     pt[0].X = points[0].X;
968     pt[0].Y = points[0].Y;
969     pt[1].X = x1;
970     pt[1].Y = y1;
971
972     for(i = 0; i < count-2; i++){
973         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
974
975         pt[3*i+2].X = x1;
976         pt[3*i+2].Y = y1;
977         pt[3*i+3].X = points[i+1].X;
978         pt[3*i+3].Y = points[i+1].Y;
979         pt[3*i+4].X = x2;
980         pt[3*i+4].Y = y2;
981     }
982
983     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
984         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
985
986     pt[len_pt-2].X = x1;
987     pt[len_pt-2].Y = y1;
988     pt[len_pt-1].X = points[count-1].X;
989     pt[len_pt-1].Y = points[count-1].Y;
990
991     save_state = prepare_dc(graphics, pen);
992
993     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
994
995     GdipFree(pt);
996     restore_dc(graphics, save_state);
997
998     return retval;
999 }
1000
1001 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1002     INT y)
1003 {
1004     UINT width, height, srcw, srch;
1005
1006     if(!graphics || !image)
1007         return InvalidParameter;
1008
1009     GdipGetImageWidth(image, &width);
1010     GdipGetImageHeight(image, &height);
1011
1012     srcw = width * (((REAL) INCH_HIMETRIC) /
1013             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1014     srch = height * (((REAL) INCH_HIMETRIC) /
1015             ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1016
1017     if(image->type != ImageTypeMetafile){
1018         y += height;
1019         height *= -1;
1020     }
1021
1022     IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1023                     0, 0, srcw, srch, NULL);
1024
1025     return Ok;
1026 }
1027
1028 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1029 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1030      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1031      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1032      DrawImageAbort callback, VOID * callbackData)
1033 {
1034     GpPointF ptf[3];
1035     POINT pti[3];
1036     REAL dx, dy;
1037
1038     TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1039           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1040           callbackData);
1041
1042     if(!graphics || !image || !points || !imageAttributes || count != 3)
1043          return InvalidParameter;
1044
1045     if(srcUnit == UnitInch)
1046         dx = dy = (REAL) INCH_HIMETRIC;
1047     else if(srcUnit == UnitPixel){
1048         dx = ((REAL) INCH_HIMETRIC) /
1049              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1050         dy = ((REAL) INCH_HIMETRIC) /
1051              ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1052     }
1053     else
1054         return NotImplemented;
1055
1056     memcpy(ptf, points, 3 * sizeof(GpPointF));
1057     transform_and_round_points(graphics, pti, ptf, 3);
1058
1059     /* IPicture renders bitmaps with the y-axis reversed
1060      * FIXME: flipping for unknown image type might not be correct. */
1061     if(image->type != ImageTypeMetafile){
1062         INT temp;
1063         temp = pti[0].y;
1064         pti[0].y = pti[2].y;
1065         pti[2].y = temp;
1066     }
1067
1068     if(IPicture_Render(image->picture, graphics->hdc,
1069         pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1070         srcx * dx, srcy * dy,
1071         srcwidth * dx, srcheight * dy,
1072         NULL) != S_OK){
1073         if(callback)
1074             callback(callbackData);
1075         return GenericError;
1076     }
1077
1078     return Ok;
1079 }
1080
1081 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1082     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1083     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1084     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1085     VOID * callbackData)
1086 {
1087     GpPointF points[3];
1088
1089     points[0].X = dstx;
1090     points[0].Y = dsty;
1091     points[1].X = dstx + dstwidth;
1092     points[1].Y = dsty;
1093     points[2].X = dstx;
1094     points[2].Y = dsty + dstheight;
1095
1096     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1097                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1098 }
1099
1100 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1101     REAL y1, REAL x2, REAL y2)
1102 {
1103     INT save_state;
1104     GpPointF pt[2];
1105     GpStatus retval;
1106
1107     if(!pen || !graphics)
1108         return InvalidParameter;
1109
1110     pt[0].X = x1;
1111     pt[0].Y = y1;
1112     pt[1].X = x2;
1113     pt[1].Y = y2;
1114
1115     save_state = prepare_dc(graphics, pen);
1116
1117     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1118
1119     restore_dc(graphics, save_state);
1120
1121     return retval;
1122 }
1123
1124 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1125     INT y1, INT x2, INT y2)
1126 {
1127     INT save_state;
1128     GpPointF pt[2];
1129     GpStatus retval;
1130
1131     if(!pen || !graphics)
1132         return InvalidParameter;
1133
1134     pt[0].X = (REAL)x1;
1135     pt[0].Y = (REAL)y1;
1136     pt[1].X = (REAL)x2;
1137     pt[1].Y = (REAL)y2;
1138
1139     save_state = prepare_dc(graphics, pen);
1140
1141     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1142
1143     restore_dc(graphics, save_state);
1144
1145     return retval;
1146 }
1147
1148 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1149     GpPointF *points, INT count)
1150 {
1151     INT save_state;
1152     GpStatus retval;
1153
1154     if(!pen || !graphics || (count < 2))
1155         return InvalidParameter;
1156
1157     save_state = prepare_dc(graphics, pen);
1158
1159     retval = draw_polyline(graphics, pen, points, count, TRUE);
1160
1161     restore_dc(graphics, save_state);
1162
1163     return retval;
1164 }
1165
1166 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1167 {
1168     INT save_state;
1169     GpStatus retval;
1170
1171     if(!pen || !graphics)
1172         return InvalidParameter;
1173
1174     save_state = prepare_dc(graphics, pen);
1175
1176     retval = draw_poly(graphics, pen, path->pathdata.Points,
1177                        path->pathdata.Types, path->pathdata.Count, TRUE);
1178
1179     restore_dc(graphics, save_state);
1180
1181     return retval;
1182 }
1183
1184 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1185     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1186 {
1187     INT save_state;
1188
1189     if(!graphics || !pen)
1190         return InvalidParameter;
1191
1192     save_state = prepare_dc(graphics, pen);
1193     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1194
1195     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1196
1197     restore_dc(graphics, save_state);
1198
1199     return Ok;
1200 }
1201
1202 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1203     INT y, INT width, INT height)
1204 {
1205     INT save_state;
1206     GpPointF ptf[4];
1207     POINT pti[4];
1208
1209     if(!pen || !graphics)
1210         return InvalidParameter;
1211
1212     ptf[0].X = x;
1213     ptf[0].Y = y;
1214     ptf[1].X = x + width;
1215     ptf[1].Y = y;
1216     ptf[2].X = x + width;
1217     ptf[2].Y = y + height;
1218     ptf[3].X = x;
1219     ptf[3].Y = y + height;
1220
1221     save_state = prepare_dc(graphics, pen);
1222     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1223
1224     transform_and_round_points(graphics, pti, ptf, 4);
1225     Polygon(graphics->hdc, pti, 4);
1226
1227     restore_dc(graphics, save_state);
1228
1229     return Ok;
1230 }
1231
1232 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1233     GpRectF* rects, INT count)
1234 {
1235     GpPointF *ptf;
1236     POINT *pti;
1237     INT save_state, i;
1238
1239     if(!graphics || !pen || !rects || count < 1)
1240         return InvalidParameter;
1241
1242     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1243     pti = GdipAlloc(4 * count * sizeof(POINT));
1244
1245     if(!ptf || !pti){
1246         GdipFree(ptf);
1247         GdipFree(pti);
1248         return OutOfMemory;
1249     }
1250
1251     for(i = 0; i < count; i++){
1252         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1253         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1254         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1255         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1256     }
1257
1258     save_state = prepare_dc(graphics, pen);
1259     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1260
1261     transform_and_round_points(graphics, pti, ptf, 4 * count);
1262
1263     for(i = 0; i < count; i++)
1264         Polygon(graphics->hdc, &pti[4 * i], 4);
1265
1266     restore_dc(graphics, save_state);
1267
1268     GdipFree(ptf);
1269     GdipFree(pti);
1270
1271     return Ok;
1272 }
1273
1274 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1275     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1276     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1277 {
1278     HRGN rgn;
1279     HFONT gdifont;
1280     LOGFONTW lfw;
1281     TEXTMETRICW textmet;
1282     GpPointF pt[2], rectcpy[4];
1283     POINT corners[4];
1284     WCHAR* stringdup;
1285     REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1286     INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth;
1287     SIZE size;
1288
1289     if(!graphics || !string || !font || !brush || !rect)
1290         return InvalidParameter;
1291
1292     if((brush->bt != BrushTypeSolidColor)){
1293         FIXME("not implemented for given parameters\n");
1294         return NotImplemented;
1295     }
1296
1297     if(format)
1298         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1299
1300     if(length == -1) length = lstrlenW(string);
1301
1302     stringdup = GdipAlloc(length * sizeof(WCHAR));
1303     if(!stringdup) return OutOfMemory;
1304
1305     save_state = SaveDC(graphics->hdc);
1306     SetBkMode(graphics->hdc, TRANSPARENT);
1307     SetTextColor(graphics->hdc, brush->lb.lbColor);
1308
1309     rectcpy[3].X = rectcpy[0].X = rect->X;
1310     rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1311     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1312     rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1313     transform_and_round_points(graphics, corners, rectcpy, 4);
1314     rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1315                      (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1316                      / rect->Width;
1317     nwidth = roundr(rel_width * rect->Width);
1318     rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1319                       (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1320                       / rect->Height;
1321
1322     rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1323     SelectClipRgn(graphics->hdc, rgn);
1324
1325     /* Use gdi to find the font, then perform transformations on it (height,
1326      * width, angle). */
1327     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1328     GetTextMetricsW(graphics->hdc, &textmet);
1329     memcpy(&lfw, &font->lfw, sizeof(LOGFONTW));
1330
1331     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1332     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1333
1334     pt[0].X = 0.0;
1335     pt[0].Y = 0.0;
1336     pt[1].X = 1.0;
1337     pt[1].Y = 0.0;
1338     GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1339     angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1340     ang_cos = cos(angle);
1341     ang_sin = sin(angle);
1342     lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1343
1344     gdifont = CreateFontIndirectW(&lfw);
1345     DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1346
1347     for(i = 0, j = 0; i < length; i++){
1348         if(!isprintW(string[i]) && (string[i] != '\n'))
1349             continue;
1350
1351         stringdup[j] = string[i];
1352         j++;
1353     }
1354
1355     stringdup[j] = 0;
1356     length = j;
1357
1358     while(sum < length){
1359         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1360                               nwidth, &fit, NULL, &size);
1361         fitcpy = fit;
1362
1363         if(fit == 0){
1364             TabbedTextOutW(graphics->hdc,
1365                            corners[0].x + roundr(ang_sin * (REAL) height),
1366                            corners[0].y + roundr(ang_cos * (REAL) height),
1367                            stringdup + sum, 1, 0, 0, 0);
1368             break;
1369         }
1370
1371         for(lret = 0; lret < fit; lret++)
1372             if(*(stringdup + sum + lret) == '\n')
1373                 break;
1374
1375         /* Line break code (may look strange, but it imitates windows). */
1376         if(lret < fit)
1377             fit = lret;    /* this is not an off-by-one error */
1378         else if(fit < (length - sum)){
1379             if(*(stringdup + sum + fit) == ' ')
1380                 while(*(stringdup + sum + fit) == ' ')
1381                     fit++;
1382             else
1383                 while(*(stringdup + sum + fit - 1) != ' '){
1384                     fit--;
1385
1386                     if(*(stringdup + sum + fit) == '\t')
1387                         break;
1388
1389                     if(fit == 0){
1390                         fit = fitcpy;
1391                         break;
1392                     }
1393                 }
1394         }
1395
1396         TabbedTextOutW(graphics->hdc,
1397                        corners[0].x - roundr(ang_sin * (REAL) height),
1398                        corners[0].y + roundr(ang_cos * (REAL) height),
1399                        stringdup + sum, min(length - sum, fit), 0, 0, 0);
1400
1401         sum += fit + (lret < fitcpy ? 1 : 0);
1402         height += size.cy;
1403
1404         if(height > roundr(rect->Height * rel_height))
1405             break;
1406
1407         /* Stop if this was a linewrap (but not if it was a linebreak). */
1408         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1409             break;
1410     }
1411
1412     DeleteObject(rgn);
1413     DeleteObject(gdifont);
1414
1415     RestoreDC(graphics->hdc, save_state);
1416
1417     return Ok;
1418 }
1419
1420 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1421 {
1422     INT save_state;
1423     GpStatus retval;
1424
1425     if(!brush || !graphics || !path)
1426         return InvalidParameter;
1427
1428     save_state = SaveDC(graphics->hdc);
1429     EndPath(graphics->hdc);
1430     SelectObject(graphics->hdc, brush->gdibrush);
1431     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1432                                                                     : WINDING));
1433
1434     BeginPath(graphics->hdc);
1435     retval = draw_poly(graphics, NULL, path->pathdata.Points,
1436                        path->pathdata.Types, path->pathdata.Count, FALSE);
1437
1438     if(retval != Ok)
1439         goto end;
1440
1441     EndPath(graphics->hdc);
1442     FillPath(graphics->hdc);
1443
1444     retval = Ok;
1445
1446 end:
1447     RestoreDC(graphics->hdc, save_state);
1448
1449     return retval;
1450 }
1451
1452 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1453     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1454 {
1455     INT save_state;
1456
1457     if(!graphics || !brush)
1458         return InvalidParameter;
1459
1460     save_state = SaveDC(graphics->hdc);
1461     EndPath(graphics->hdc);
1462     SelectObject(graphics->hdc, brush->gdibrush);
1463     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1464
1465     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1466
1467     RestoreDC(graphics->hdc, save_state);
1468
1469     return Ok;
1470 }
1471
1472 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1473     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1474 {
1475     INT save_state;
1476     GpPointF *ptf = NULL;
1477     POINT *pti = NULL;
1478     GpStatus retval = Ok;
1479
1480     if(!graphics || !brush || !points || !count)
1481         return InvalidParameter;
1482
1483     ptf = GdipAlloc(count * sizeof(GpPointF));
1484     pti = GdipAlloc(count * sizeof(POINT));
1485     if(!ptf || !pti){
1486         retval = OutOfMemory;
1487         goto end;
1488     }
1489
1490     memcpy(ptf, points, count * sizeof(GpPointF));
1491
1492     save_state = SaveDC(graphics->hdc);
1493     EndPath(graphics->hdc);
1494     SelectObject(graphics->hdc, brush->gdibrush);
1495     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1496     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1497                                                                   : WINDING));
1498
1499     transform_and_round_points(graphics, pti, ptf, count);
1500     Polygon(graphics->hdc, pti, count);
1501
1502     RestoreDC(graphics->hdc, save_state);
1503
1504 end:
1505     GdipFree(ptf);
1506     GdipFree(pti);
1507
1508     return retval;
1509 }
1510
1511 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1512     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1513 {
1514     INT save_state, i;
1515     GpPointF *ptf = NULL;
1516     POINT *pti = NULL;
1517     GpStatus retval = Ok;
1518
1519     if(!graphics || !brush || !points || !count)
1520         return InvalidParameter;
1521
1522     ptf = GdipAlloc(count * sizeof(GpPointF));
1523     pti = GdipAlloc(count * sizeof(POINT));
1524     if(!ptf || !pti){
1525         retval = OutOfMemory;
1526         goto end;
1527     }
1528
1529     for(i = 0; i < count; i ++){
1530         ptf[i].X = (REAL) points[i].X;
1531         ptf[i].Y = (REAL) points[i].Y;
1532     }
1533
1534     save_state = SaveDC(graphics->hdc);
1535     EndPath(graphics->hdc);
1536     SelectObject(graphics->hdc, brush->gdibrush);
1537     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1538     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1539                                                                   : WINDING));
1540
1541     transform_and_round_points(graphics, pti, ptf, count);
1542     Polygon(graphics->hdc, pti, count);
1543
1544     RestoreDC(graphics->hdc, save_state);
1545
1546 end:
1547     GdipFree(ptf);
1548     GdipFree(pti);
1549
1550     return retval;
1551 }
1552
1553 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1554     REAL x, REAL y, REAL width, REAL height)
1555 {
1556     INT save_state;
1557     GpPointF ptf[4];
1558     POINT pti[4];
1559
1560     if(!graphics || !brush)
1561         return InvalidParameter;
1562
1563     ptf[0].X = x;
1564     ptf[0].Y = y;
1565     ptf[1].X = x + width;
1566     ptf[1].Y = y;
1567     ptf[2].X = x + width;
1568     ptf[2].Y = y + height;
1569     ptf[3].X = x;
1570     ptf[3].Y = y + height;
1571
1572     save_state = SaveDC(graphics->hdc);
1573     EndPath(graphics->hdc);
1574     SelectObject(graphics->hdc, brush->gdibrush);
1575     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1576
1577     transform_and_round_points(graphics, pti, ptf, 4);
1578
1579     Polygon(graphics->hdc, pti, 4);
1580
1581     RestoreDC(graphics->hdc, save_state);
1582
1583     return Ok;
1584 }
1585
1586 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1587     INT x, INT y, INT width, INT height)
1588 {
1589     INT save_state;
1590     GpPointF ptf[4];
1591     POINT pti[4];
1592
1593     if(!graphics || !brush)
1594         return InvalidParameter;
1595
1596     ptf[0].X = x;
1597     ptf[0].Y = y;
1598     ptf[1].X = x + width;
1599     ptf[1].Y = y;
1600     ptf[2].X = x + width;
1601     ptf[2].Y = y + height;
1602     ptf[3].X = x;
1603     ptf[3].Y = y + height;
1604
1605     save_state = SaveDC(graphics->hdc);
1606     EndPath(graphics->hdc);
1607     SelectObject(graphics->hdc, brush->gdibrush);
1608     SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1609
1610     transform_and_round_points(graphics, pti, ptf, 4);
1611
1612     Polygon(graphics->hdc, pti, 4);
1613
1614     RestoreDC(graphics->hdc, save_state);
1615
1616     return Ok;
1617 }
1618
1619 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
1620 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
1621     CompositingMode *mode)
1622 {
1623     if(!graphics || !mode)
1624         return InvalidParameter;
1625
1626     *mode = graphics->compmode;
1627
1628     return Ok;
1629 }
1630
1631 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1632 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1633     CompositingQuality *quality)
1634 {
1635     if(!graphics || !quality)
1636         return InvalidParameter;
1637
1638     *quality = graphics->compqual;
1639
1640     return Ok;
1641 }
1642
1643 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1644 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1645     InterpolationMode *mode)
1646 {
1647     if(!graphics || !mode)
1648         return InvalidParameter;
1649
1650     *mode = graphics->interpolation;
1651
1652     return Ok;
1653 }
1654
1655 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1656 {
1657     if(!graphics || !scale)
1658         return InvalidParameter;
1659
1660     *scale = graphics->scale;
1661
1662     return Ok;
1663 }
1664
1665 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1666 {
1667     if(!graphics || !unit)
1668         return InvalidParameter;
1669
1670     *unit = graphics->unit;
1671
1672     return Ok;
1673 }
1674
1675 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1676 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1677     *mode)
1678 {
1679     if(!graphics || !mode)
1680         return InvalidParameter;
1681
1682     *mode = graphics->pixeloffset;
1683
1684     return Ok;
1685 }
1686
1687 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1688 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1689 {
1690     if(!graphics || !mode)
1691         return InvalidParameter;
1692
1693     *mode = graphics->smoothing;
1694
1695     return Ok;
1696 }
1697
1698 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
1699 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
1700     TextRenderingHint *hint)
1701 {
1702     if(!graphics || !hint)
1703         return InvalidParameter;
1704
1705     *hint = graphics->texthint;
1706
1707     return Ok;
1708 }
1709
1710 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1711 {
1712     if(!graphics || !matrix)
1713         return InvalidParameter;
1714
1715     memcpy(matrix, graphics->worldtrans, sizeof(GpMatrix));
1716     return Ok;
1717 }
1718
1719 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
1720     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
1721     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
1722     INT *codepointsfitted, INT *linesfilled)
1723 {
1724     HFONT oldfont;
1725     WCHAR* stringdup;
1726     INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth;
1727     SIZE size;
1728
1729     if(!graphics || !string || !font || !rect)
1730         return InvalidParameter;
1731
1732     if(format || codepointsfitted || linesfilled){
1733         FIXME("not implemented for given parameters\n");
1734         if(format)
1735             TRACE("format attr is %d\n", format->attr);
1736         return NotImplemented;
1737     }
1738
1739     if(length == -1) length = lstrlenW(string);
1740
1741     stringdup = GdipAlloc(length * sizeof(WCHAR));
1742     if(!stringdup) return OutOfMemory;
1743
1744     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1745     nwidth = roundr(rect->Width);
1746
1747     for(i = 0, j = 0; i < length; i++){
1748         if(!isprintW(string[i]) && (string[i] != '\n'))
1749             continue;
1750
1751         stringdup[j] = string[i];
1752         j++;
1753     }
1754
1755     stringdup[j] = 0;
1756     length = j;
1757
1758     while(sum < length){
1759         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1760                               nwidth, &fit, NULL, &size);
1761         fitcpy = fit;
1762
1763         if(fit == 0)
1764             break;
1765
1766         for(lret = 0; lret < fit; lret++)
1767             if(*(stringdup + sum + lret) == '\n')
1768                 break;
1769
1770         /* Line break code (may look strange, but it imitates windows). */
1771         if(lret < fit)
1772             fit = lret;    /* this is not an off-by-one error */
1773         else if(fit < (length - sum)){
1774             if(*(stringdup + sum + fit) == ' ')
1775                 while(*(stringdup + sum + fit) == ' ')
1776                     fit++;
1777             else
1778                 while(*(stringdup + sum + fit - 1) != ' '){
1779                     fit--;
1780
1781                     if(*(stringdup + sum + fit) == '\t')
1782                         break;
1783
1784                     if(fit == 0){
1785                         fit = fitcpy;
1786                         break;
1787                     }
1788                 }
1789         }
1790
1791         GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
1792                               nwidth, &j, NULL, &size);
1793
1794         sum += fit + (lret < fitcpy ? 1 : 0);
1795         height += size.cy;
1796         max_width = max(max_width, size.cx);
1797
1798         if(height > roundr(rect->Height))
1799             break;
1800     }
1801
1802     bounds->X = rect->X;
1803     bounds->Y = rect->Y;
1804     bounds->Width = (REAL)max_width;
1805     bounds->Height = min((REAL)height, rect->Height);
1806
1807     DeleteObject(SelectObject(graphics->hdc, oldfont));
1808
1809     return Ok;
1810 }
1811
1812 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1813 {
1814     static int calls;
1815
1816     if(!graphics)
1817         return InvalidParameter;
1818
1819     if(!(calls++))
1820         FIXME("graphics state not implemented\n");
1821
1822     return NotImplemented;
1823 }
1824
1825 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
1826     GpMatrixOrder order)
1827 {
1828     if(!graphics)
1829         return InvalidParameter;
1830
1831     return GdipRotateMatrix(graphics->worldtrans, angle, order);
1832 }
1833
1834 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1835 {
1836     static int calls;
1837
1838     if(!graphics || !state)
1839         return InvalidParameter;
1840
1841     if(!(calls++))
1842         FIXME("graphics state not implemented\n");
1843
1844     return NotImplemented;
1845 }
1846
1847 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
1848     REAL sy, GpMatrixOrder order)
1849 {
1850     if(!graphics)
1851         return InvalidParameter;
1852
1853     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
1854 }
1855
1856 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
1857     CompositingMode mode)
1858 {
1859     if(!graphics)
1860         return InvalidParameter;
1861
1862     graphics->compmode = mode;
1863
1864     return Ok;
1865 }
1866
1867 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1868     CompositingQuality quality)
1869 {
1870     if(!graphics)
1871         return InvalidParameter;
1872
1873     graphics->compqual = quality;
1874
1875     return Ok;
1876 }
1877
1878 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1879     InterpolationMode mode)
1880 {
1881     if(!graphics)
1882         return InvalidParameter;
1883
1884     graphics->interpolation = mode;
1885
1886     return Ok;
1887 }
1888
1889 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
1890 {
1891     if(!graphics || (scale <= 0.0))
1892         return InvalidParameter;
1893
1894     graphics->scale = scale;
1895
1896     return Ok;
1897 }
1898
1899 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
1900 {
1901     if(!graphics || (unit == UnitWorld))
1902         return InvalidParameter;
1903
1904     graphics->unit = unit;
1905
1906     return Ok;
1907 }
1908
1909 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1910     mode)
1911 {
1912     if(!graphics)
1913         return InvalidParameter;
1914
1915     graphics->pixeloffset = mode;
1916
1917     return Ok;
1918 }
1919
1920 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1921 {
1922     if(!graphics)
1923         return InvalidParameter;
1924
1925     graphics->smoothing = mode;
1926
1927     return Ok;
1928 }
1929
1930 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
1931     TextRenderingHint hint)
1932 {
1933     if(!graphics)
1934         return InvalidParameter;
1935
1936     graphics->texthint = hint;
1937
1938     return Ok;
1939 }
1940
1941 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1942 {
1943     if(!graphics || !matrix)
1944         return InvalidParameter;
1945
1946     GdipDeleteMatrix(graphics->worldtrans);
1947     return GdipCloneMatrix(matrix, &graphics->worldtrans);
1948 }
1949
1950 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
1951     REAL dy, GpMatrixOrder order)
1952 {
1953     if(!graphics)
1954         return InvalidParameter;
1955
1956     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
1957 }