po: Update French translation.
[wine] / dlls / gdiplus / graphicspath.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
20 #include <stdarg.h>
21 #include <math.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27
28 #include "objbase.h"
29
30 #include "gdiplus.h"
31 #include "gdiplus_private.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
35
36 typedef struct path_list_node_t path_list_node_t;
37 struct path_list_node_t {
38     GpPointF pt;
39     BYTE type; /* PathPointTypeStart or PathPointTypeLine */
40     path_list_node_t *next;
41 };
42
43 /* init list */
44 static BOOL init_path_list(path_list_node_t **node, REAL x, REAL y)
45 {
46     *node = GdipAlloc(sizeof(path_list_node_t));
47     if(!*node)
48         return FALSE;
49
50     (*node)->pt.X = x;
51     (*node)->pt.Y = y;
52     (*node)->type = PathPointTypeStart;
53     (*node)->next = NULL;
54
55     return TRUE;
56 }
57
58 /* free all nodes including argument */
59 static void free_path_list(path_list_node_t *node)
60 {
61     path_list_node_t *n = node;
62
63     while(n){
64         n = n->next;
65         GdipFree(node);
66         node = n;
67     }
68 }
69
70 /* Add a node after 'node' */
71 /*
72  * Returns
73  *  pointer on success
74  *  NULL    on allocation problems
75  */
76 static path_list_node_t* add_path_list_node(path_list_node_t *node, REAL x, REAL y, BOOL type)
77 {
78     path_list_node_t *new;
79
80     new = GdipAlloc(sizeof(path_list_node_t));
81     if(!new)
82         return NULL;
83
84     new->pt.X  = x;
85     new->pt.Y  = y;
86     new->type  = type;
87     new->next  = node->next;
88     node->next = new;
89
90     return new;
91 }
92
93 /* returns element count */
94 static INT path_list_count(path_list_node_t *node)
95 {
96     INT count = 1;
97
98     while((node = node->next))
99         ++count;
100
101     return count;
102 }
103
104 /* GdipFlattenPath helper */
105 /*
106  * Used to recursively flatten single Bezier curve
107  * Parameters:
108  *  - start   : pointer to start point node;
109  *  - (x2, y2): first control point;
110  *  - (x3, y3): second control point;
111  *  - end     : pointer to end point node
112  *  - flatness: admissible error of linear approximation.
113  *
114  * Return value:
115  *  TRUE : success
116  *  FALSE: out of memory
117  *
118  * TODO: used quality criteria should be revised to match native as
119  *       closer as possible.
120  */
121 static BOOL flatten_bezier(path_list_node_t *start, REAL x2, REAL y2, REAL x3, REAL y3,
122                            path_list_node_t *end, REAL flatness)
123 {
124     /* this 5 middle points with start/end define to half-curves */
125     GpPointF mp[5];
126     GpPointF pt, pt_st;
127     path_list_node_t *node;
128
129     /* calculate bezier curve middle points == new control points */
130     mp[0].X = (start->pt.X + x2) / 2.0;
131     mp[0].Y = (start->pt.Y + y2) / 2.0;
132     /* middle point between control points */
133     pt.X = (x2 + x3) / 2.0;
134     pt.Y = (y2 + y3) / 2.0;
135     mp[1].X = (mp[0].X + pt.X) / 2.0;
136     mp[1].Y = (mp[0].Y + pt.Y) / 2.0;
137     mp[4].X = (end->pt.X + x3) / 2.0;
138     mp[4].Y = (end->pt.Y + y3) / 2.0;
139     mp[3].X = (mp[4].X + pt.X) / 2.0;
140     mp[3].Y = (mp[4].Y + pt.Y) / 2.0;
141
142     mp[2].X = (mp[1].X + mp[3].X) / 2.0;
143     mp[2].Y = (mp[1].Y + mp[3].Y) / 2.0;
144
145     pt = end->pt;
146     pt_st = start->pt;
147     /* check flatness as a half of distance between middle point and a linearized path */
148     if(fabs(((pt.Y - pt_st.Y)*mp[2].X + (pt_st.X - pt.X)*mp[2].Y +
149         (pt_st.Y*pt.X - pt_st.X*pt.Y))) <=
150         (0.5 * flatness*sqrtf((powf(pt.Y - pt_st.Y, 2.0) + powf(pt_st.X - pt.X, 2.0))))){
151         return TRUE;
152     }
153     else
154         /* add a middle point */
155         if(!(node = add_path_list_node(start, mp[2].X, mp[2].Y, PathPointTypeLine)))
156             return FALSE;
157
158     /* do the same with halves */
159     flatten_bezier(start, mp[0].X, mp[0].Y, mp[1].X, mp[1].Y, node, flatness);
160     flatten_bezier(node,  mp[3].X, mp[3].Y, mp[4].X, mp[4].Y, end,  flatness);
161
162     return TRUE;
163 }
164
165 GpStatus WINGDIPAPI GdipAddPathArc(GpPath *path, REAL x1, REAL y1, REAL x2,
166     REAL y2, REAL startAngle, REAL sweepAngle)
167 {
168     INT count, old_count, i;
169
170     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
171           path, x1, y1, x2, y2, startAngle, sweepAngle);
172
173     if(!path)
174         return InvalidParameter;
175
176     count = arc2polybezier(NULL, x1, y1, x2, y2, startAngle, sweepAngle);
177
178     if(count == 0)
179         return Ok;
180     if(!lengthen_path(path, count))
181         return OutOfMemory;
182
183     old_count = path->pathdata.Count;
184     arc2polybezier(&path->pathdata.Points[old_count], x1, y1, x2, y2,
185                    startAngle, sweepAngle);
186
187     for(i = 0; i < count; i++){
188         path->pathdata.Types[old_count + i] = PathPointTypeBezier;
189     }
190
191     path->pathdata.Types[old_count] =
192         (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
193     path->newfigure = FALSE;
194     path->pathdata.Count += count;
195
196     return Ok;
197 }
198
199 GpStatus WINGDIPAPI GdipAddPathArcI(GpPath *path, INT x1, INT y1, INT x2,
200    INT y2, REAL startAngle, REAL sweepAngle)
201 {
202     TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
203           path, x1, y1, x2, y2, startAngle, sweepAngle);
204
205     return GdipAddPathArc(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,startAngle,sweepAngle);
206 }
207
208 GpStatus WINGDIPAPI GdipAddPathBezier(GpPath *path, REAL x1, REAL y1, REAL x2,
209     REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
210 {
211     INT old_count;
212
213     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
214           path, x1, y1, x2, y2, x3, y3, x4, y4);
215
216     if(!path)
217         return InvalidParameter;
218
219     if(!lengthen_path(path, 4))
220         return OutOfMemory;
221
222     old_count = path->pathdata.Count;
223
224     path->pathdata.Points[old_count].X = x1;
225     path->pathdata.Points[old_count].Y = y1;
226     path->pathdata.Points[old_count + 1].X = x2;
227     path->pathdata.Points[old_count + 1].Y = y2;
228     path->pathdata.Points[old_count + 2].X = x3;
229     path->pathdata.Points[old_count + 2].Y = y3;
230     path->pathdata.Points[old_count + 3].X = x4;
231     path->pathdata.Points[old_count + 3].Y = y4;
232
233     path->pathdata.Types[old_count] =
234         (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
235     path->pathdata.Types[old_count + 1] = PathPointTypeBezier;
236     path->pathdata.Types[old_count + 2] = PathPointTypeBezier;
237     path->pathdata.Types[old_count + 3] = PathPointTypeBezier;
238
239     path->newfigure = FALSE;
240     path->pathdata.Count += 4;
241
242     return Ok;
243 }
244
245 GpStatus WINGDIPAPI GdipAddPathBezierI(GpPath *path, INT x1, INT y1, INT x2,
246     INT y2, INT x3, INT y3, INT x4, INT y4)
247 {
248     TRACE("(%p, %d, %d, %d, %d, %d, %d, %d, %d)\n",
249           path, x1, y1, x2, y2, x3, y3, x4, y4);
250
251     return GdipAddPathBezier(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,(REAL)x3,(REAL)y3,
252                                   (REAL)x4,(REAL)y4);
253 }
254
255 GpStatus WINGDIPAPI GdipAddPathBeziers(GpPath *path, GDIPCONST GpPointF *points,
256     INT count)
257 {
258     INT i, old_count;
259
260     TRACE("(%p, %p, %d)\n", path, points, count);
261
262     if(!path || !points || ((count - 1) % 3))
263         return InvalidParameter;
264
265     if(!lengthen_path(path, count))
266         return OutOfMemory;
267
268     old_count = path->pathdata.Count;
269
270     for(i = 0; i < count; i++){
271         path->pathdata.Points[old_count + i].X = points[i].X;
272         path->pathdata.Points[old_count + i].Y = points[i].Y;
273         path->pathdata.Types[old_count + i] = PathPointTypeBezier;
274     }
275
276     path->pathdata.Types[old_count] =
277         (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
278     path->newfigure = FALSE;
279     path->pathdata.Count += count;
280
281     return Ok;
282 }
283
284 GpStatus WINGDIPAPI GdipAddPathBeziersI(GpPath *path, GDIPCONST GpPoint *points,
285     INT count)
286 {
287     GpPointF *ptsF;
288     GpStatus ret;
289     INT i;
290
291     TRACE("(%p, %p, %d)\n", path, points, count);
292
293     if(!points || ((count - 1) % 3))
294         return InvalidParameter;
295
296     ptsF = GdipAlloc(sizeof(GpPointF) * count);
297     if(!ptsF)
298         return OutOfMemory;
299
300     for(i = 0; i < count; i++){
301         ptsF[i].X = (REAL)points[i].X;
302         ptsF[i].Y = (REAL)points[i].Y;
303     }
304
305     ret = GdipAddPathBeziers(path, ptsF, count);
306     GdipFree(ptsF);
307
308     return ret;
309 }
310
311 GpStatus WINGDIPAPI GdipAddPathClosedCurve(GpPath *path, GDIPCONST GpPointF *points,
312     INT count)
313 {
314     TRACE("(%p, %p, %d)\n", path, points, count);
315
316     return GdipAddPathClosedCurve2(path, points, count, 1.0);
317 }
318
319 GpStatus WINGDIPAPI GdipAddPathClosedCurveI(GpPath *path, GDIPCONST GpPoint *points,
320     INT count)
321 {
322     TRACE("(%p, %p, %d)\n", path, points, count);
323
324     return GdipAddPathClosedCurve2I(path, points, count, 1.0);
325 }
326
327 GpStatus WINGDIPAPI GdipAddPathClosedCurve2(GpPath *path, GDIPCONST GpPointF *points,
328     INT count, REAL tension)
329 {
330     INT i, len_pt = (count + 1)*3-2;
331     GpPointF *pt;
332     GpPointF *pts;
333     REAL x1, x2, y1, y2;
334     GpStatus stat;
335
336     TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
337
338     if(!path || !points || count <= 1)
339         return InvalidParameter;
340
341     pt = GdipAlloc(len_pt * sizeof(GpPointF));
342     pts = GdipAlloc((count + 1)*sizeof(GpPointF));
343     if(!pt || !pts){
344         GdipFree(pt);
345         GdipFree(pts);
346         return OutOfMemory;
347     }
348
349     /* copy source points to extend with the last one */
350     memcpy(pts, points, sizeof(GpPointF)*count);
351     pts[count] = pts[0];
352
353     tension = tension * TENSION_CONST;
354
355     for(i = 0; i < count-1; i++){
356         calc_curve_bezier(&(pts[i]), tension, &x1, &y1, &x2, &y2);
357
358         pt[3*i+2].X = x1;
359         pt[3*i+2].Y = y1;
360         pt[3*i+3].X = pts[i+1].X;
361         pt[3*i+3].Y = pts[i+1].Y;
362         pt[3*i+4].X = x2;
363         pt[3*i+4].Y = y2;
364     }
365
366     /* points [len_pt-2] and [0] are calculated
367        separately to connect splines properly */
368     pts[0] = points[count-1];
369     pts[1] = points[0]; /* equals to start and end of a resulting path */
370     pts[2] = points[1];
371
372     calc_curve_bezier(pts, tension, &x1, &y1, &x2, &y2);
373     pt[len_pt-2].X = x1;
374     pt[len_pt-2].Y = y1;
375     pt[0].X = pts[1].X;
376     pt[0].Y = pts[1].Y;
377     pt[1].X = x2;
378     pt[1].Y = y2;
379     /* close path */
380     pt[len_pt-1].X = pt[0].X;
381     pt[len_pt-1].Y = pt[0].Y;
382
383     stat = GdipAddPathBeziers(path, pt, len_pt);
384
385     /* close figure */
386     if(stat == Ok){
387         path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
388         path->newfigure = TRUE;
389     }
390
391     GdipFree(pts);
392     GdipFree(pt);
393
394     return stat;
395 }
396
397 GpStatus WINGDIPAPI GdipAddPathClosedCurve2I(GpPath *path, GDIPCONST GpPoint *points,
398     INT count, REAL tension)
399 {
400     GpPointF *ptf;
401     INT i;
402     GpStatus stat;
403
404     TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
405
406     if(!path || !points || count <= 1)
407         return InvalidParameter;
408
409     ptf = GdipAlloc(sizeof(GpPointF)*count);
410     if(!ptf)
411         return OutOfMemory;
412
413     for(i = 0; i < count; i++){
414         ptf[i].X = (REAL)points[i].X;
415         ptf[i].Y = (REAL)points[i].Y;
416     }
417
418     stat = GdipAddPathClosedCurve2(path, ptf, count, tension);
419
420     GdipFree(ptf);
421
422     return stat;
423 }
424
425 GpStatus WINGDIPAPI GdipAddPathCurve(GpPath *path, GDIPCONST GpPointF *points, INT count)
426 {
427     TRACE("(%p, %p, %d)\n", path, points, count);
428
429     if(!path || !points || count <= 1)
430         return InvalidParameter;
431
432     return GdipAddPathCurve2(path, points, count, 1.0);
433 }
434
435 GpStatus WINGDIPAPI GdipAddPathCurveI(GpPath *path, GDIPCONST GpPoint *points, INT count)
436 {
437     TRACE("(%p, %p, %d)\n", path, points, count);
438
439     if(!path || !points || count <= 1)
440         return InvalidParameter;
441
442     return GdipAddPathCurve2I(path, points, count, 1.0);
443 }
444
445 GpStatus WINGDIPAPI GdipAddPathCurve2(GpPath *path, GDIPCONST GpPointF *points, INT count,
446     REAL tension)
447 {
448     INT i, len_pt = count*3-2;
449     GpPointF *pt;
450     REAL x1, x2, y1, y2;
451     GpStatus stat;
452
453     TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
454
455     if(!path || !points || count <= 1)
456         return InvalidParameter;
457
458     pt = GdipAlloc(len_pt * sizeof(GpPointF));
459     if(!pt)
460         return OutOfMemory;
461
462     tension = tension * TENSION_CONST;
463
464     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
465         tension, &x1, &y1);
466
467     pt[0].X = points[0].X;
468     pt[0].Y = points[0].Y;
469     pt[1].X = x1;
470     pt[1].Y = y1;
471
472     for(i = 0; i < count-2; i++){
473         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
474
475         pt[3*i+2].X = x1;
476         pt[3*i+2].Y = y1;
477         pt[3*i+3].X = points[i+1].X;
478         pt[3*i+3].Y = points[i+1].Y;
479         pt[3*i+4].X = x2;
480         pt[3*i+4].Y = y2;
481     }
482
483     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
484         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
485
486     pt[len_pt-2].X = x1;
487     pt[len_pt-2].Y = y1;
488     pt[len_pt-1].X = points[count-1].X;
489     pt[len_pt-1].Y = points[count-1].Y;
490
491     stat = GdipAddPathBeziers(path, pt, len_pt);
492
493     GdipFree(pt);
494
495     return stat;
496 }
497
498 GpStatus WINGDIPAPI GdipAddPathCurve2I(GpPath *path, GDIPCONST GpPoint *points,
499     INT count, REAL tension)
500 {
501     GpPointF *ptf;
502     INT i;
503     GpStatus stat;
504
505     TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
506
507     if(!path || !points || count <= 1)
508         return InvalidParameter;
509
510     ptf = GdipAlloc(sizeof(GpPointF)*count);
511     if(!ptf)
512         return OutOfMemory;
513
514     for(i = 0; i < count; i++){
515         ptf[i].X = (REAL)points[i].X;
516         ptf[i].Y = (REAL)points[i].Y;
517     }
518
519     stat = GdipAddPathCurve2(path, ptf, count, tension);
520
521     GdipFree(ptf);
522
523     return stat;
524 }
525
526 GpStatus WINGDIPAPI GdipAddPathCurve3(GpPath *path, GDIPCONST GpPointF *points,
527     INT count, INT offset, INT nseg, REAL tension)
528 {
529     TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
530
531     if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
532         return InvalidParameter;
533
534     return GdipAddPathCurve2(path, &points[offset], nseg + 1, tension);
535 }
536
537 GpStatus WINGDIPAPI GdipAddPathCurve3I(GpPath *path, GDIPCONST GpPoint *points,
538     INT count, INT offset, INT nseg, REAL tension)
539 {
540     TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
541
542     if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
543         return InvalidParameter;
544
545     return GdipAddPathCurve2I(path, &points[offset], nseg + 1, tension);
546 }
547
548 GpStatus WINGDIPAPI GdipAddPathEllipse(GpPath *path, REAL x, REAL y, REAL width,
549     REAL height)
550 {
551     INT old_count, numpts;
552
553     TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
554
555     if(!path)
556         return InvalidParameter;
557
558     if(!lengthen_path(path, MAX_ARC_PTS))
559         return OutOfMemory;
560
561     old_count = path->pathdata.Count;
562     if((numpts = arc2polybezier(&path->pathdata.Points[old_count],  x, y, width,
563                                height, 0.0, 360.0)) != MAX_ARC_PTS){
564         ERR("expected %d points but got %d\n", MAX_ARC_PTS, numpts);
565         return GenericError;
566     }
567
568     memset(&path->pathdata.Types[old_count + 1], PathPointTypeBezier,
569            MAX_ARC_PTS - 1);
570
571     /* An ellipse is an intrinsic figure (always is its own subpath). */
572     path->pathdata.Types[old_count] = PathPointTypeStart;
573     path->pathdata.Types[old_count + MAX_ARC_PTS - 1] |= PathPointTypeCloseSubpath;
574     path->newfigure = TRUE;
575     path->pathdata.Count += MAX_ARC_PTS;
576
577     return Ok;
578 }
579
580 GpStatus WINGDIPAPI GdipAddPathEllipseI(GpPath *path, INT x, INT y, INT width,
581     INT height)
582 {
583     TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
584
585     return GdipAddPathEllipse(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
586 }
587
588 GpStatus WINGDIPAPI GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
589     INT count)
590 {
591     INT i, old_count;
592
593     TRACE("(%p, %p, %d)\n", path, points, count);
594
595     if(!path || !points)
596         return InvalidParameter;
597
598     if(!lengthen_path(path, count))
599         return OutOfMemory;
600
601     old_count = path->pathdata.Count;
602
603     for(i = 0; i < count; i++){
604         path->pathdata.Points[old_count + i].X = points[i].X;
605         path->pathdata.Points[old_count + i].Y = points[i].Y;
606         path->pathdata.Types[old_count + i] = PathPointTypeLine;
607     }
608
609     if(path->newfigure){
610         path->pathdata.Types[old_count] = PathPointTypeStart;
611         path->newfigure = FALSE;
612     }
613
614     path->pathdata.Count += count;
615
616     return Ok;
617 }
618
619 GpStatus WINGDIPAPI GdipAddPathLine2I(GpPath *path, GDIPCONST GpPoint *points, INT count)
620 {
621     GpPointF *pointsF;
622     INT i;
623     GpStatus stat;
624
625     TRACE("(%p, %p, %d)\n", path, points, count);
626
627     if(count <= 0)
628         return InvalidParameter;
629
630     pointsF = GdipAlloc(sizeof(GpPointF) * count);
631     if(!pointsF)    return OutOfMemory;
632
633     for(i = 0;i < count; i++){
634         pointsF[i].X = (REAL)points[i].X;
635         pointsF[i].Y = (REAL)points[i].Y;
636     }
637
638     stat = GdipAddPathLine2(path, pointsF, count);
639
640     GdipFree(pointsF);
641
642     return stat;
643 }
644
645 GpStatus WINGDIPAPI GdipAddPathLine(GpPath *path, REAL x1, REAL y1, REAL x2, REAL y2)
646 {
647     INT old_count;
648
649     TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x1, y1, x2, y2);
650
651     if(!path)
652         return InvalidParameter;
653
654     if(!lengthen_path(path, 2))
655         return OutOfMemory;
656
657     old_count = path->pathdata.Count;
658
659     path->pathdata.Points[old_count].X = x1;
660     path->pathdata.Points[old_count].Y = y1;
661     path->pathdata.Points[old_count + 1].X = x2;
662     path->pathdata.Points[old_count + 1].Y = y2;
663
664     path->pathdata.Types[old_count] =
665         (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
666     path->pathdata.Types[old_count + 1] = PathPointTypeLine;
667
668     path->newfigure = FALSE;
669     path->pathdata.Count += 2;
670
671     return Ok;
672 }
673
674 GpStatus WINGDIPAPI GdipAddPathLineI(GpPath *path, INT x1, INT y1, INT x2, INT y2)
675 {
676     TRACE("(%p, %d, %d, %d, %d)\n", path, x1, y1, x2, y2);
677
678     return GdipAddPathLine(path, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
679 }
680
681 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
682     BOOL connect)
683 {
684     INT old_count, count;
685
686     TRACE("(%p, %p, %d)\n", path, addingPath, connect);
687
688     if(!path || !addingPath)
689         return InvalidParameter;
690
691     old_count = path->pathdata.Count;
692     count = addingPath->pathdata.Count;
693
694     if(!lengthen_path(path, count))
695         return OutOfMemory;
696
697     memcpy(&path->pathdata.Points[old_count], addingPath->pathdata.Points,
698            count * sizeof(GpPointF));
699     memcpy(&path->pathdata.Types[old_count], addingPath->pathdata.Types, count);
700
701     if(path->newfigure || !connect)
702         path->pathdata.Types[old_count] = PathPointTypeStart;
703     else
704         path->pathdata.Types[old_count] = PathPointTypeLine;
705
706     path->newfigure = FALSE;
707     path->pathdata.Count += count;
708
709     return Ok;
710 }
711
712 GpStatus WINGDIPAPI GdipAddPathPie(GpPath *path, REAL x, REAL y, REAL width, REAL height,
713     REAL startAngle, REAL sweepAngle)
714 {
715     GpPointF *ptf;
716     GpStatus status;
717     INT i, count;
718
719     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
720           path, x, y, width, height, startAngle, sweepAngle);
721
722     if(!path)
723         return InvalidParameter;
724
725     /* on zero width/height only start point added */
726     if(width <= 1e-7 || height <= 1e-7){
727         if(!lengthen_path(path, 1))
728             return OutOfMemory;
729         path->pathdata.Points[0].X = x + width  / 2.0;
730         path->pathdata.Points[0].Y = y + height / 2.0;
731         path->pathdata.Types[0] = PathPointTypeStart | PathPointTypeCloseSubpath;
732         path->pathdata.Count = 1;
733         return InvalidParameter;
734     }
735
736     count = arc2polybezier(NULL, x, y, width, height, startAngle, sweepAngle);
737
738     if(count == 0)
739         return Ok;
740
741     ptf = GdipAlloc(sizeof(GpPointF)*count);
742     if(!ptf)
743         return OutOfMemory;
744
745     arc2polybezier(ptf, x, y, width, height, startAngle, sweepAngle);
746
747     status = GdipAddPathLine(path, x + width/2, y + height/2, ptf[0].X, ptf[0].Y);
748     if(status != Ok){
749         GdipFree(ptf);
750         return status;
751     }
752     /* one spline is already added as a line endpoint */
753     if(!lengthen_path(path, count - 1)){
754         GdipFree(ptf);
755         return OutOfMemory;
756     }
757
758     memcpy(&(path->pathdata.Points[path->pathdata.Count]), &(ptf[1]),sizeof(GpPointF)*(count-1));
759     for(i = 0; i < count-1; i++)
760         path->pathdata.Types[path->pathdata.Count+i] = PathPointTypeBezier;
761
762     path->pathdata.Count += count-1;
763
764     GdipClosePathFigure(path);
765
766     GdipFree(ptf);
767
768     return status;
769 }
770
771 GpStatus WINGDIPAPI GdipAddPathPieI(GpPath *path, INT x, INT y, INT width, INT height,
772     REAL startAngle, REAL sweepAngle)
773 {
774     TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
775           path, x, y, width, height, startAngle, sweepAngle);
776
777     return GdipAddPathPie(path, (REAL)x, (REAL)y, (REAL)width, (REAL)height, startAngle, sweepAngle);
778 }
779
780 GpStatus WINGDIPAPI GdipAddPathPolygon(GpPath *path, GDIPCONST GpPointF *points, INT count)
781 {
782     INT old_count;
783
784     TRACE("(%p, %p, %d)\n", path, points, count);
785
786     if(!path || !points || count < 3)
787         return InvalidParameter;
788
789     if(!lengthen_path(path, count))
790         return OutOfMemory;
791
792     old_count = path->pathdata.Count;
793
794     memcpy(&path->pathdata.Points[old_count], points, count*sizeof(GpPointF));
795     memset(&path->pathdata.Types[old_count + 1], PathPointTypeLine, count - 1);
796
797     /* A polygon is an intrinsic figure */
798     path->pathdata.Types[old_count] = PathPointTypeStart;
799     path->pathdata.Types[old_count + count - 1] |= PathPointTypeCloseSubpath;
800     path->newfigure = TRUE;
801     path->pathdata.Count += count;
802
803     return Ok;
804 }
805
806 GpStatus WINGDIPAPI GdipAddPathPolygonI(GpPath *path, GDIPCONST GpPoint *points, INT count)
807 {
808     GpPointF *ptf;
809     GpStatus status;
810     INT i;
811
812     TRACE("(%p, %p, %d)\n", path, points, count);
813
814     if(!points || count < 3)
815         return InvalidParameter;
816
817     ptf = GdipAlloc(sizeof(GpPointF) * count);
818     if(!ptf)
819         return OutOfMemory;
820
821     for(i = 0; i < count; i++){
822         ptf[i].X = (REAL)points[i].X;
823         ptf[i].Y = (REAL)points[i].Y;
824     }
825
826     status = GdipAddPathPolygon(path, ptf, count);
827
828     GdipFree(ptf);
829
830     return status;
831 }
832
833 static float fromfixedpoint(const FIXED v)
834 {
835     float f = ((float)v.fract) / (1<<(sizeof(v.fract)*8));
836     f += v.value;
837     return f;
838 }
839
840 struct format_string_args
841 {
842     GpPath *path;
843     UINT maxY;
844 };
845
846 static GpStatus format_string_callback(HDC dc,
847     GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
848     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
849     INT lineno, const RectF *bounds, INT *underlined_indexes,
850     INT underlined_index_count, void *priv)
851 {
852     static const MAT2 identity = { {0,1}, {0,0}, {0,0}, {0,1} };
853     struct format_string_args *args = priv;
854     GpPath *path = args->path;
855     GpStatus status = Ok;
856     float x = bounds->X;
857     float y = bounds->Y;
858     int i;
859
860     if (underlined_index_count)
861         FIXME("hotkey underlines not drawn yet\n");
862
863     for (i = index; i < length; ++i)
864     {
865         GLYPHMETRICS gm;
866         TTPOLYGONHEADER *ph = NULL;
867         char *start;
868         DWORD len, ofs = 0;
869         UINT bb_end;
870         len = GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, 0, NULL, &identity);
871         if (len == GDI_ERROR)
872         {
873             status = GenericError;
874             break;
875         }
876         ph = GdipAlloc(len);
877         start = (char *)ph;
878         if (!ph || !lengthen_path(path, len / sizeof(POINTFX)))
879         {
880             status = OutOfMemory;
881             break;
882         }
883         GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, len, start, &identity);
884         bb_end = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y;
885         if (bb_end + y > args->maxY)
886             args->maxY = bb_end + y;
887
888         ofs = 0;
889         while (ofs < len)
890         {
891             DWORD ofs_start = ofs;
892             ph = (TTPOLYGONHEADER*)&start[ofs];
893             path->pathdata.Types[path->pathdata.Count] = PathPointTypeStart;
894             path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(ph->pfxStart.x);
895             path->pathdata.Points[path->pathdata.Count++].Y = y + bb_end - fromfixedpoint(ph->pfxStart.y);
896             TRACE("Starting at count %i with pos %f, %f)\n", path->pathdata.Count, x, y);
897             ofs += sizeof(*ph);
898             while (ofs - ofs_start < ph->cb)
899             {
900                 TTPOLYCURVE *curve = (TTPOLYCURVE*)&start[ofs];
901                 int j;
902                 ofs += sizeof(TTPOLYCURVE) + (curve->cpfx - 1) * sizeof(POINTFX);
903
904                 switch (curve->wType)
905                 {
906                 case TT_PRIM_LINE:
907                     for (j = 0; j < curve->cpfx; ++j)
908                     {
909                         path->pathdata.Types[path->pathdata.Count] = PathPointTypeLine;
910                         path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x);
911                         path->pathdata.Points[path->pathdata.Count++].Y = y + bb_end - fromfixedpoint(curve->apfx[j].y);
912                     }
913                     break;
914                 case TT_PRIM_CSPLINE:
915                     for (j = 0; j < curve->cpfx; ++j)
916                     {
917                         path->pathdata.Types[path->pathdata.Count] = PathPointTypeBezier;
918                         path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x);
919                         path->pathdata.Points[path->pathdata.Count++].Y = y + bb_end - fromfixedpoint(curve->apfx[j].y);
920                     }
921                     break;
922                 default:
923                     ERR("Unhandled type: %u\n", curve->wType);
924                     status = GenericError;
925                 }
926             }
927             path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
928         }
929         path->newfigure = TRUE;
930         x += gm.gmCellIncX;
931         y += gm.gmCellIncY;
932
933         GdipFree(ph);
934         if (status != Ok)
935             break;
936     }
937
938     return status;
939 }
940
941 GpStatus WINGDIPAPI GdipAddPathString(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat* format)
942 {
943     GpFont *font;
944     GpStatus status;
945     LOGFONTW lfw;
946     HANDLE hfont;
947     HDC dc;
948     GpPath *backup;
949     struct format_string_args args;
950     int i;
951
952     FIXME("(%p, %s, %d, %p, %d, %f, %p, %p): stub\n", path, debugstr_w(string), length, family, style, emSize, layoutRect, format);
953     if (!path || !string || !family || !emSize || !layoutRect || !format)
954         return InvalidParameter;
955
956     status = GdipCreateFont(family, emSize, style, UnitPixel, &font);
957     if (status != Ok)
958         return status;
959
960     get_log_fontW(font, NULL, &lfw);
961     hfont = CreateFontIndirectW(&lfw);
962     if (!hfont)
963     {
964         WARN("Failed to create font\n");
965         return GenericError;
966     }
967
968     if ((status = GdipClonePath(path, &backup)) != Ok)
969     {
970         DeleteObject(hfont);
971         return status;
972     }
973
974     dc = CreateCompatibleDC(0);
975     SelectObject(dc, hfont);
976
977     args.path = path;
978     args.maxY = 0;
979     status = gdip_format_string(dc, string, length, NULL, layoutRect, format, format_string_callback, &args);
980
981     DeleteDC(dc);
982     DeleteObject(hfont);
983
984     if (status != Ok) /* free backup */
985     {
986         GdipFree(path->pathdata.Points);
987         GdipFree(path->pathdata.Types);
988         *path = *backup;
989         GdipFree(backup);
990         return status;
991     }
992     if (format && format->vertalign == StringAlignmentCenter && layoutRect->Y + args.maxY < layoutRect->Height)
993     {
994         float inc = layoutRect->Height - args.maxY - layoutRect->Y;
995         inc /= 2;
996         for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
997             path->pathdata.Points[i].Y += inc;
998     } else if (format && format->vertalign == StringAlignmentFar) {
999         float inc = layoutRect->Height - args.maxY - layoutRect->Y;
1000         for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1001             path->pathdata.Points[i].Y += inc;
1002     }
1003     GdipDeletePath(backup);
1004     return status;
1005 }
1006
1007 GpStatus WINGDIPAPI GdipAddPathStringI(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1008 {
1009     if (layoutRect)
1010     {
1011         RectF layoutRectF = {
1012             (REAL)layoutRect->X,
1013             (REAL)layoutRect->Y,
1014             (REAL)layoutRect->Width,
1015             (REAL)layoutRect->Height
1016         };
1017         return GdipAddPathString(path, string, length, family, style, emSize, &layoutRectF, format);
1018     }
1019     return InvalidParameter;
1020 }
1021
1022 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1023 {
1024     TRACE("(%p, %p)\n", path, clone);
1025
1026     if(!path || !clone)
1027         return InvalidParameter;
1028
1029     *clone = GdipAlloc(sizeof(GpPath));
1030     if(!*clone) return OutOfMemory;
1031
1032     **clone = *path;
1033
1034     (*clone)->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
1035     (*clone)->pathdata.Types = GdipAlloc(path->datalen);
1036     if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1037         GdipFree(*clone);
1038         GdipFree((*clone)->pathdata.Points);
1039         GdipFree((*clone)->pathdata.Types);
1040         return OutOfMemory;
1041     }
1042
1043     memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1044            path->datalen * sizeof(PointF));
1045     memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1046
1047     return Ok;
1048 }
1049
1050 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1051 {
1052     TRACE("(%p)\n", path);
1053
1054     if(!path)
1055         return InvalidParameter;
1056
1057     if(path->pathdata.Count > 0){
1058         path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1059         path->newfigure = TRUE;
1060     }
1061
1062     return Ok;
1063 }
1064
1065 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1066 {
1067     INT i;
1068
1069     TRACE("(%p)\n", path);
1070
1071     if(!path)
1072         return InvalidParameter;
1073
1074     for(i = 1; i < path->pathdata.Count; i++){
1075         if(path->pathdata.Types[i] == PathPointTypeStart)
1076             path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1077     }
1078
1079     path->newfigure = TRUE;
1080
1081     return Ok;
1082 }
1083
1084 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1085 {
1086     TRACE("(%d, %p)\n", fill, path);
1087
1088     if(!path)
1089         return InvalidParameter;
1090
1091     *path = GdipAlloc(sizeof(GpPath));
1092     if(!*path)  return OutOfMemory;
1093
1094     (*path)->fill = fill;
1095     (*path)->newfigure = TRUE;
1096
1097     return Ok;
1098 }
1099
1100 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1101     GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1102 {
1103     TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1104
1105     if(!path)
1106         return InvalidParameter;
1107
1108     *path = GdipAlloc(sizeof(GpPath));
1109     if(!*path)  return OutOfMemory;
1110
1111     (*path)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
1112     (*path)->pathdata.Types = GdipAlloc(count);
1113
1114     if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
1115         GdipFree((*path)->pathdata.Points);
1116         GdipFree((*path)->pathdata.Types);
1117         GdipFree(*path);
1118         return OutOfMemory;
1119     }
1120
1121     memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
1122     memcpy((*path)->pathdata.Types, types, count);
1123     (*path)->pathdata.Count = count;
1124     (*path)->datalen = count;
1125
1126     (*path)->fill = fill;
1127     (*path)->newfigure = TRUE;
1128
1129     return Ok;
1130 }
1131
1132 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1133     GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1134 {
1135     GpPointF *ptF;
1136     GpStatus ret;
1137     INT i;
1138
1139     TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1140
1141     ptF = GdipAlloc(sizeof(GpPointF)*count);
1142
1143     for(i = 0;i < count; i++){
1144         ptF[i].X = (REAL)points[i].X;
1145         ptF[i].Y = (REAL)points[i].Y;
1146     }
1147
1148     ret = GdipCreatePath2(ptF, types, count, fill, path);
1149
1150     GdipFree(ptF);
1151
1152     return ret;
1153 }
1154
1155 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1156 {
1157     TRACE("(%p)\n", path);
1158
1159     if(!path)
1160         return InvalidParameter;
1161
1162     GdipFree(path->pathdata.Points);
1163     GdipFree(path->pathdata.Types);
1164     GdipFree(path);
1165
1166     return Ok;
1167 }
1168
1169 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1170 {
1171     path_list_node_t *list, *node;
1172     GpPointF pt;
1173     INT i = 1;
1174     INT startidx = 0;
1175
1176     TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1177
1178     if(!path)
1179         return InvalidParameter;
1180
1181     if(matrix){
1182         WARN("transformation not supported yet!\n");
1183         return NotImplemented;
1184     }
1185
1186     if(path->pathdata.Count == 0)
1187         return Ok;
1188
1189     pt = path->pathdata.Points[0];
1190     if(!init_path_list(&list, pt.X, pt.Y))
1191         return OutOfMemory;
1192
1193     node = list;
1194
1195     while(i < path->pathdata.Count){
1196
1197         BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1198         path_list_node_t *start;
1199
1200         pt = path->pathdata.Points[i];
1201
1202         /* save last start point index */
1203         if(type == PathPointTypeStart)
1204             startidx = i;
1205
1206         /* always add line points and start points */
1207         if((type == PathPointTypeStart) || (type == PathPointTypeLine)){
1208             if(!add_path_list_node(node, pt.X, pt.Y, path->pathdata.Types[i]))
1209                 goto memout;
1210
1211             node = node->next;
1212             ++i;
1213             continue;
1214         }
1215
1216         /* Bezier curve */
1217
1218         /* test for closed figure */
1219         if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1220             pt = path->pathdata.Points[startidx];
1221             ++i;
1222         }
1223         else
1224         {
1225             i += 2;
1226             pt = path->pathdata.Points[i];
1227         };
1228
1229         start = node;
1230         /* add Bezier end point */
1231         type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1232         if(!add_path_list_node(node, pt.X, pt.Y, type))
1233             goto memout;
1234         node = node->next;
1235
1236         /* flatten curve */
1237         if(!flatten_bezier(start, path->pathdata.Points[i-2].X, path->pathdata.Points[i-2].Y,
1238                                   path->pathdata.Points[i-1].X, path->pathdata.Points[i-1].Y,
1239                            node, flatness))
1240             goto memout;
1241
1242         ++i;
1243     }/* while */
1244
1245     /* store path data back */
1246     i = path_list_count(list);
1247     if(!lengthen_path(path, i))
1248         goto memout;
1249     path->pathdata.Count = i;
1250
1251     node = list;
1252     for(i = 0; i < path->pathdata.Count; i++){
1253         path->pathdata.Points[i] = node->pt;
1254         path->pathdata.Types[i]  = node->type;
1255         node = node->next;
1256     }
1257
1258     free_path_list(list);
1259     return Ok;
1260
1261 memout:
1262     free_path_list(list);
1263     return OutOfMemory;
1264 }
1265
1266 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1267 {
1268     TRACE("(%p, %p)\n", path, pathData);
1269
1270     if(!path || !pathData)
1271         return InvalidParameter;
1272
1273     /* Only copy data. pathData allocation/freeing controlled by wrapper class.
1274        Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
1275     memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
1276     memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
1277
1278     return Ok;
1279 }
1280
1281 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1282 {
1283     TRACE("(%p, %p)\n", path, fillmode);
1284
1285     if(!path || !fillmode)
1286         return InvalidParameter;
1287
1288     *fillmode = path->fill;
1289
1290     return Ok;
1291 }
1292
1293 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1294 {
1295     INT count;
1296
1297     TRACE("(%p, %p)\n", path, lastPoint);
1298
1299     if(!path || !lastPoint)
1300         return InvalidParameter;
1301
1302     count = path->pathdata.Count;
1303     if(count > 0)
1304         *lastPoint = path->pathdata.Points[count-1];
1305
1306     return Ok;
1307 }
1308
1309 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1310 {
1311     TRACE("(%p, %p, %d)\n", path, points, count);
1312
1313     if(!path)
1314         return InvalidParameter;
1315
1316     if(count < path->pathdata.Count)
1317         return InsufficientBuffer;
1318
1319     memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1320
1321     return Ok;
1322 }
1323
1324 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1325 {
1326     GpStatus ret;
1327     GpPointF *ptf;
1328     INT i;
1329
1330     TRACE("(%p, %p, %d)\n", path, points, count);
1331
1332     if(count <= 0)
1333         return InvalidParameter;
1334
1335     ptf = GdipAlloc(sizeof(GpPointF)*count);
1336     if(!ptf)    return OutOfMemory;
1337
1338     ret = GdipGetPathPoints(path,ptf,count);
1339     if(ret == Ok)
1340         for(i = 0;i < count;i++){
1341             points[i].X = roundr(ptf[i].X);
1342             points[i].Y = roundr(ptf[i].Y);
1343         };
1344     GdipFree(ptf);
1345
1346     return ret;
1347 }
1348
1349 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1350 {
1351     TRACE("(%p, %p, %d)\n", path, types, count);
1352
1353     if(!path)
1354         return InvalidParameter;
1355
1356     if(count < path->pathdata.Count)
1357         return InsufficientBuffer;
1358
1359     memcpy(types, path->pathdata.Types, path->pathdata.Count);
1360
1361     return Ok;
1362 }
1363
1364 /* Windows expands the bounding box to the maximum possible bounding box
1365  * for a given pen.  For example, if a line join can extend past the point
1366  * it's joining by x units, the bounding box is extended by x units in every
1367  * direction (even though this is too conservative for most cases). */
1368 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
1369     GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1370 {
1371     GpPointF * points, temp_pts[4];
1372     INT count, i;
1373     REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
1374
1375     TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1376
1377     /* Matrix and pen can be null. */
1378     if(!path || !bounds)
1379         return InvalidParameter;
1380
1381     /* If path is empty just return. */
1382     count = path->pathdata.Count;
1383     if(count == 0){
1384         bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
1385         return Ok;
1386     }
1387
1388     points = path->pathdata.Points;
1389
1390     low_x = high_x = points[0].X;
1391     low_y = high_y = points[0].Y;
1392
1393     for(i = 1; i < count; i++){
1394         low_x = min(low_x, points[i].X);
1395         low_y = min(low_y, points[i].Y);
1396         high_x = max(high_x, points[i].X);
1397         high_y = max(high_y, points[i].Y);
1398     }
1399
1400     width = high_x - low_x;
1401     height = high_y - low_y;
1402
1403     /* This looks unusual but it's the only way I can imitate windows. */
1404     if(matrix){
1405         temp_pts[0].X = low_x;
1406         temp_pts[0].Y = low_y;
1407         temp_pts[1].X = low_x;
1408         temp_pts[1].Y = high_y;
1409         temp_pts[2].X = high_x;
1410         temp_pts[2].Y = high_y;
1411         temp_pts[3].X = high_x;
1412         temp_pts[3].Y = low_y;
1413
1414         GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1415         low_x = temp_pts[0].X;
1416         low_y = temp_pts[0].Y;
1417
1418         for(i = 1; i < 4; i++){
1419             low_x = min(low_x, temp_pts[i].X);
1420             low_y = min(low_y, temp_pts[i].Y);
1421         }
1422
1423         temp = width;
1424         width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
1425         height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
1426     }
1427
1428     if(pen){
1429         path_width = pen->width / 2.0;
1430
1431         if(count > 2)
1432             path_width = max(path_width,  pen->width * pen->miterlimit / 2.0);
1433         /* FIXME: this should probably also check for the startcap */
1434         if(pen->endcap & LineCapNoAnchor)
1435             path_width = max(path_width,  pen->width * 2.2);
1436
1437         low_x -= path_width;
1438         low_y -= path_width;
1439         width += 2.0 * path_width;
1440         height += 2.0 * path_width;
1441     }
1442
1443     bounds->X = low_x;
1444     bounds->Y = low_y;
1445     bounds->Width = width;
1446     bounds->Height = height;
1447
1448     return Ok;
1449 }
1450
1451 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1452     GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1453 {
1454     GpStatus ret;
1455     GpRectF boundsF;
1456
1457     TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1458
1459     ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
1460
1461     if(ret == Ok){
1462         bounds->X      = roundr(boundsF.X);
1463         bounds->Y      = roundr(boundsF.Y);
1464         bounds->Width  = roundr(boundsF.Width);
1465         bounds->Height = roundr(boundsF.Height);
1466     }
1467
1468     return ret;
1469 }
1470
1471 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1472 {
1473     TRACE("(%p, %p)\n", path, count);
1474
1475     if(!path)
1476         return InvalidParameter;
1477
1478     *count = path->pathdata.Count;
1479
1480     return Ok;
1481 }
1482
1483 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1484 {
1485     INT i, count;
1486     INT start = 0; /* position in reversed path */
1487     GpPathData revpath;
1488
1489     TRACE("(%p)\n", path);
1490
1491     if(!path)
1492         return InvalidParameter;
1493
1494     count = path->pathdata.Count;
1495
1496     if(count == 0) return Ok;
1497
1498     revpath.Points = GdipAlloc(sizeof(GpPointF)*count);
1499     revpath.Types  = GdipAlloc(sizeof(BYTE)*count);
1500     revpath.Count  = count;
1501     if(!revpath.Points || !revpath.Types){
1502         GdipFree(revpath.Points);
1503         GdipFree(revpath.Types);
1504         return OutOfMemory;
1505     }
1506
1507     for(i = 0; i < count; i++){
1508
1509         /* find next start point */
1510         if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1511             INT j;
1512             for(j = start; j <= i; j++){
1513                 revpath.Points[j] = path->pathdata.Points[count-j-1];
1514                 revpath.Types[j] = path->pathdata.Types[count-j-1];
1515             }
1516             /* mark start point */
1517             revpath.Types[start] = PathPointTypeStart;
1518             /* set 'figure' endpoint type */
1519             if(i-start > 1){
1520                 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1521                 revpath.Types[i] |= revpath.Types[i-1];
1522             }
1523             else
1524                 revpath.Types[i] = path->pathdata.Types[start];
1525
1526             start = i+1;
1527         }
1528     }
1529
1530     memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1531     memcpy(path->pathdata.Types,  revpath.Types,  sizeof(BYTE)*count);
1532
1533     GdipFree(revpath.Points);
1534     GdipFree(revpath.Types);
1535
1536     return Ok;
1537 }
1538
1539 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1540     GpPen *pen, GpGraphics *graphics, BOOL *result)
1541 {
1542     TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1543
1544     return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1545 }
1546
1547 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1548     GpPen *pen, GpGraphics *graphics, BOOL *result)
1549 {
1550     static int calls;
1551
1552     TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1553
1554     if(!path || !pen)
1555         return InvalidParameter;
1556
1557     if(!(calls++))
1558         FIXME("not implemented\n");
1559
1560     return NotImplemented;
1561 }
1562
1563 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1564 {
1565     TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1566
1567     return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1568 }
1569
1570 /*****************************************************************************
1571  * GdipIsVisiblePathPoint [GDIPLUS.@]
1572  */
1573 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1574 {
1575     GpRegion *region;
1576     HRGN hrgn;
1577     GpStatus status;
1578
1579     if(!path || !result) return InvalidParameter;
1580
1581     status = GdipCreateRegionPath(path, &region);
1582     if(status != Ok)
1583         return status;
1584
1585     status = GdipGetRegionHRgn(region, graphics, &hrgn);
1586     if(status != Ok){
1587         GdipDeleteRegion(region);
1588         return status;
1589     }
1590
1591     *result = PtInRegion(hrgn, roundr(x), roundr(y));
1592
1593     DeleteObject(hrgn);
1594     GdipDeleteRegion(region);
1595
1596     return Ok;
1597 }
1598
1599 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1600 {
1601     TRACE("(%p)\n", path);
1602
1603     if(!path)
1604         return InvalidParameter;
1605
1606     path->newfigure = TRUE;
1607
1608     return Ok;
1609 }
1610
1611 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1612 {
1613     TRACE("(%p)\n", path);
1614
1615     if(!path)
1616         return InvalidParameter;
1617
1618     path->pathdata.Count = 0;
1619     path->newfigure = TRUE;
1620     path->fill = FillModeAlternate;
1621
1622     return Ok;
1623 }
1624
1625 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1626 {
1627     TRACE("(%p, %d)\n", path, fill);
1628
1629     if(!path)
1630         return InvalidParameter;
1631
1632     path->fill = fill;
1633
1634     return Ok;
1635 }
1636
1637 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1638 {
1639     TRACE("(%p, %p)\n", path, matrix);
1640
1641     if(!path)
1642         return InvalidParameter;
1643
1644     if(path->pathdata.Count == 0)
1645         return Ok;
1646
1647     return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1648                                      path->pathdata.Count);
1649 }
1650
1651 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1652     GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1653     REAL height, WarpMode warpmode, REAL flatness)
1654 {
1655     FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1656         points, count, x, y, width, height, warpmode, flatness);
1657
1658     return NotImplemented;
1659 }
1660
1661 static void add_bevel_point(const GpPointF *endpoint, const GpPointF *nextpoint,
1662     GpPen *pen, int right_side, path_list_node_t **last_point)
1663 {
1664     REAL segment_dy = nextpoint->Y-endpoint->Y;
1665     REAL segment_dx = nextpoint->X-endpoint->X;
1666     REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1667     REAL distance = pen->width/2.0;
1668     REAL bevel_dx, bevel_dy;
1669
1670     if (right_side)
1671     {
1672         bevel_dx = -distance * segment_dy / segment_length;
1673         bevel_dy = distance * segment_dx / segment_length;
1674     }
1675     else
1676     {
1677         bevel_dx = distance * segment_dy / segment_length;
1678         bevel_dy = -distance * segment_dx / segment_length;
1679     }
1680
1681     *last_point = add_path_list_node(*last_point, endpoint->X + bevel_dx,
1682         endpoint->Y + bevel_dy, PathPointTypeLine);
1683 }
1684
1685 static void widen_joint(const GpPointF *p1, const GpPointF *p2, const GpPointF *p3,
1686     GpPen* pen, path_list_node_t **last_point)
1687 {
1688     switch (pen->join)
1689     {
1690     default:
1691     case LineJoinBevel:
1692         add_bevel_point(p2, p1, pen, 1, last_point);
1693         add_bevel_point(p2, p3, pen, 0, last_point);
1694         break;
1695     }
1696 }
1697
1698 static void widen_cap(const GpPointF *endpoint, const GpPointF *nextpoint,
1699     GpPen *pen, GpLineCap cap, GpCustomLineCap *custom, int add_first_points,
1700     int add_last_point, path_list_node_t **last_point)
1701 {
1702     switch (cap)
1703     {
1704     default:
1705     case LineCapFlat:
1706         if (add_first_points)
1707             add_bevel_point(endpoint, nextpoint, pen, 1, last_point);
1708         if (add_last_point)
1709             add_bevel_point(endpoint, nextpoint, pen, 0, last_point);
1710         break;
1711     }
1712 }
1713
1714 static void widen_open_figure(GpPath *path, GpPen *pen, int start, int end,
1715     path_list_node_t **last_point)
1716 {
1717     int i;
1718
1719     if (end <= start)
1720         return;
1721
1722     widen_cap(&path->pathdata.Points[start], &path->pathdata.Points[start+1],
1723         pen, pen->startcap, pen->customstart, FALSE, TRUE, last_point);
1724
1725     (*last_point)->type = PathPointTypeStart;
1726
1727     for (i=start+1; i<end; i++)
1728         widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
1729             &path->pathdata.Points[i+1], pen, last_point);
1730
1731     widen_cap(&path->pathdata.Points[end], &path->pathdata.Points[end-1],
1732         pen, pen->endcap, pen->customend, TRUE, TRUE, last_point);
1733
1734     for (i=end-1; i>start; i--)
1735         widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
1736             &path->pathdata.Points[i-1], pen, last_point);
1737
1738     widen_cap(&path->pathdata.Points[start], &path->pathdata.Points[start+1],
1739         pen, pen->startcap, pen->customstart, TRUE, FALSE, last_point);
1740
1741     (*last_point)->type |= PathPointTypeCloseSubpath;
1742 }
1743
1744 static void widen_closed_figure(GpPath *path, GpPen *pen, int start, int end,
1745     path_list_node_t **last_point)
1746 {
1747     int i;
1748     path_list_node_t *prev_point;
1749
1750     if (end <= start+1)
1751         return;
1752
1753     /* left outline */
1754     prev_point = *last_point;
1755
1756     widen_joint(&path->pathdata.Points[end], &path->pathdata.Points[start],
1757         &path->pathdata.Points[start+1], pen, last_point);
1758
1759     for (i=start+1; i<end; i++)
1760         widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
1761             &path->pathdata.Points[i+1], pen, last_point);
1762
1763     widen_joint(&path->pathdata.Points[end-1], &path->pathdata.Points[end],
1764         &path->pathdata.Points[start], pen, last_point);
1765
1766     prev_point->next->type = PathPointTypeStart;
1767     (*last_point)->type |= PathPointTypeCloseSubpath;
1768
1769     /* right outline */
1770     prev_point = *last_point;
1771
1772     widen_joint(&path->pathdata.Points[start], &path->pathdata.Points[end],
1773         &path->pathdata.Points[end-1], pen, last_point);
1774
1775     for (i=end-1; i>start; i--)
1776         widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
1777             &path->pathdata.Points[i-1], pen, last_point);
1778
1779     widen_joint(&path->pathdata.Points[start+1], &path->pathdata.Points[start],
1780         &path->pathdata.Points[end], pen, last_point);
1781
1782     prev_point->next->type = PathPointTypeStart;
1783     (*last_point)->type |= PathPointTypeCloseSubpath;
1784 }
1785
1786 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
1787     REAL flatness)
1788 {
1789     GpPath *flat_path=NULL;
1790     GpStatus status;
1791     path_list_node_t *points=NULL, *last_point=NULL;
1792     int i, subpath_start=0, new_length;
1793     BYTE type;
1794
1795     TRACE("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
1796
1797     if (!path || !pen)
1798         return InvalidParameter;
1799
1800     if (path->pathdata.Count <= 1)
1801         return OutOfMemory;
1802
1803     status = GdipClonePath(path, &flat_path);
1804
1805     if (status == Ok)
1806         status = GdipFlattenPath(flat_path, matrix, flatness);
1807
1808     if (status == Ok && !init_path_list(&points, 314.0, 22.0))
1809         status = OutOfMemory;
1810
1811     if (status == Ok)
1812     {
1813         last_point = points;
1814
1815         if (pen->endcap != LineCapFlat)
1816             FIXME("unimplemented end cap %x\n", pen->endcap);
1817
1818         if (pen->startcap != LineCapFlat)
1819             FIXME("unimplemented start cap %x\n", pen->startcap);
1820
1821         if (pen->dashcap != DashCapFlat)
1822             FIXME("unimplemented dash cap %d\n", pen->dashcap);
1823
1824         if (pen->join != LineJoinBevel)
1825             FIXME("unimplemented line join %d\n", pen->join);
1826
1827         if (pen->dash != DashStyleSolid)
1828             FIXME("unimplemented dash style %d\n", pen->dash);
1829
1830         if (pen->align != PenAlignmentCenter)
1831             FIXME("unimplemented pen alignment %d\n", pen->align);
1832
1833         for (i=0; i < flat_path->pathdata.Count; i++)
1834         {
1835             type = flat_path->pathdata.Types[i];
1836
1837             if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1838                 subpath_start = i;
1839
1840             if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
1841             {
1842                 widen_closed_figure(flat_path, pen, subpath_start, i, &last_point);
1843             }
1844             else if (i == flat_path->pathdata.Count-1 ||
1845                 (flat_path->pathdata.Types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
1846             {
1847                 widen_open_figure(flat_path, pen, subpath_start, i, &last_point);
1848             }
1849         }
1850
1851         new_length = path_list_count(points)-1;
1852
1853         if (!lengthen_path(path, new_length))
1854             status = OutOfMemory;
1855     }
1856
1857     if (status == Ok)
1858     {
1859         path->pathdata.Count = new_length;
1860
1861         last_point = points->next;
1862         for (i = 0; i < new_length; i++)
1863         {
1864             path->pathdata.Points[i] = last_point->pt;
1865             path->pathdata.Types[i] = last_point->type;
1866             last_point = last_point->next;
1867         }
1868
1869         path->fill = FillModeWinding;
1870     }
1871
1872     free_path_list(points);
1873
1874     GdipDeletePath(flat_path);
1875
1876     return status;
1877 }
1878
1879 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
1880     REAL width, REAL height)
1881 {
1882     GpPath *backup;
1883     GpPointF ptf[2];
1884     GpStatus retstat;
1885     BOOL old_new;
1886
1887     TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
1888
1889     if(!path)
1890         return InvalidParameter;
1891
1892     /* make a backup copy of path data */
1893     if((retstat = GdipClonePath(path, &backup)) != Ok)
1894         return retstat;
1895
1896     /* rectangle should start as new path */
1897     old_new = path->newfigure;
1898     path->newfigure = TRUE;
1899     if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
1900         path->newfigure = old_new;
1901         goto fail;
1902     }
1903
1904     ptf[0].X = x+width;
1905     ptf[0].Y = y+height;
1906     ptf[1].X = x;
1907     ptf[1].Y = y+height;
1908
1909     if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok)  goto fail;
1910     path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
1911
1912     /* free backup */
1913     GdipDeletePath(backup);
1914     return Ok;
1915
1916 fail:
1917     /* reverting */
1918     GdipFree(path->pathdata.Points);
1919     GdipFree(path->pathdata.Types);
1920     memcpy(path, backup, sizeof(*path));
1921     GdipFree(backup);
1922
1923     return retstat;
1924 }
1925
1926 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
1927     INT width, INT height)
1928 {
1929     TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
1930
1931     return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1932 }
1933
1934 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
1935 {
1936     GpPath *backup;
1937     GpStatus retstat;
1938     INT i;
1939
1940     TRACE("(%p, %p, %d)\n", path, rects, count);
1941
1942     /* count == 0 - verified condition  */
1943     if(!path || !rects || count == 0)
1944         return InvalidParameter;
1945
1946     if(count < 0)
1947         return OutOfMemory;
1948
1949     /* make a backup copy */
1950     if((retstat = GdipClonePath(path, &backup)) != Ok)
1951         return retstat;
1952
1953     for(i = 0; i < count; i++){
1954         if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
1955             goto fail;
1956     }
1957
1958     /* free backup */
1959     GdipDeletePath(backup);
1960     return Ok;
1961
1962 fail:
1963     /* reverting */
1964     GdipFree(path->pathdata.Points);
1965     GdipFree(path->pathdata.Types);
1966     memcpy(path, backup, sizeof(*path));
1967     GdipFree(backup);
1968
1969     return retstat;
1970 }
1971
1972 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
1973 {
1974     GpRectF *rectsF;
1975     GpStatus retstat;
1976     INT i;
1977
1978     TRACE("(%p, %p, %d)\n", path, rects, count);
1979
1980     if(!rects || count == 0)
1981         return InvalidParameter;
1982
1983     if(count < 0)
1984         return OutOfMemory;
1985
1986     rectsF = GdipAlloc(sizeof(GpRectF)*count);
1987
1988     for(i = 0;i < count;i++){
1989         rectsF[i].X      = (REAL)rects[i].X;
1990         rectsF[i].Y      = (REAL)rects[i].Y;
1991         rectsF[i].Width  = (REAL)rects[i].Width;
1992         rectsF[i].Height = (REAL)rects[i].Height;
1993     }
1994
1995     retstat = GdipAddPathRectangles(path, rectsF, count);
1996     GdipFree(rectsF);
1997
1998     return retstat;
1999 }
2000
2001 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
2002 {
2003     INT count;
2004
2005     TRACE("(%p)\n", path);
2006
2007     if(!path)
2008         return InvalidParameter;
2009
2010     count = path->pathdata.Count;
2011
2012     /* set marker flag */
2013     if(count > 0)
2014         path->pathdata.Types[count-1] |= PathPointTypePathMarker;
2015
2016     return Ok;
2017 }
2018
2019 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
2020 {
2021     INT count;
2022     INT i;
2023
2024     TRACE("(%p)\n", path);
2025
2026     if(!path)
2027         return InvalidParameter;
2028
2029     count = path->pathdata.Count;
2030
2031     for(i = 0; i < count - 1; i++){
2032         path->pathdata.Types[i] &= ~PathPointTypePathMarker;
2033     }
2034
2035     return Ok;
2036 }
2037
2038 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
2039 {
2040    FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
2041    return NotImplemented;
2042 }