dsound: Avoid signed-unsigned integer comparisons.
[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     float maxY;
844     float scale;
845     float ascent;
846 };
847
848 static GpStatus format_string_callback(HDC dc,
849     GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
850     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
851     INT lineno, const RectF *bounds, INT *underlined_indexes,
852     INT underlined_index_count, void *priv)
853 {
854     static const MAT2 identity = { {0,1}, {0,0}, {0,0}, {0,1} };
855     struct format_string_args *args = priv;
856     GpPath *path = args->path;
857     GpStatus status = Ok;
858     float x = rect->X + (bounds->X - rect->X) * args->scale;
859     float y = rect->Y + (bounds->Y - rect->Y) * args->scale;
860     int i;
861
862     if (underlined_index_count)
863         FIXME("hotkey underlines not drawn yet\n");
864
865     if (y + bounds->Height * args->scale > args->maxY)
866         args->maxY = y + bounds->Height * args->scale;
867
868     for (i = index; i < length; ++i)
869     {
870         GLYPHMETRICS gm;
871         TTPOLYGONHEADER *ph = NULL;
872         char *start;
873         DWORD len, ofs = 0;
874         len = GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, 0, NULL, &identity);
875         if (len == GDI_ERROR)
876         {
877             status = GenericError;
878             break;
879         }
880         ph = GdipAlloc(len);
881         start = (char *)ph;
882         if (!ph || !lengthen_path(path, len / sizeof(POINTFX)))
883         {
884             status = OutOfMemory;
885             break;
886         }
887         GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, len, start, &identity);
888
889         ofs = 0;
890         while (ofs < len)
891         {
892             DWORD ofs_start = ofs;
893             ph = (TTPOLYGONHEADER*)&start[ofs];
894             path->pathdata.Types[path->pathdata.Count] = PathPointTypeStart;
895             path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(ph->pfxStart.x) * args->scale;
896             path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(ph->pfxStart.y) * args->scale;
897             TRACE("Starting at count %i with pos %f, %f)\n", path->pathdata.Count, x, y);
898             ofs += sizeof(*ph);
899             while (ofs - ofs_start < ph->cb)
900             {
901                 TTPOLYCURVE *curve = (TTPOLYCURVE*)&start[ofs];
902                 int j;
903                 ofs += sizeof(TTPOLYCURVE) + (curve->cpfx - 1) * sizeof(POINTFX);
904
905                 switch (curve->wType)
906                 {
907                 case TT_PRIM_LINE:
908                     for (j = 0; j < curve->cpfx; ++j)
909                     {
910                         path->pathdata.Types[path->pathdata.Count] = PathPointTypeLine;
911                         path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
912                         path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
913                     }
914                     break;
915                 case TT_PRIM_CSPLINE:
916                     for (j = 0; j < curve->cpfx; ++j)
917                     {
918                         path->pathdata.Types[path->pathdata.Count] = PathPointTypeBezier;
919                         path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
920                         path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
921                     }
922                     break;
923                 default:
924                     ERR("Unhandled type: %u\n", curve->wType);
925                     status = GenericError;
926                 }
927             }
928             path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
929         }
930         path->newfigure = TRUE;
931         x += gm.gmCellIncX * args->scale;
932         y += gm.gmCellIncY * args->scale;
933
934         GdipFree(ph);
935         if (status != Ok)
936             break;
937     }
938
939     return status;
940 }
941
942 GpStatus WINGDIPAPI GdipAddPathString(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat* format)
943 {
944     GpFont *font;
945     GpStatus status;
946     LOGFONTW lfw;
947     HANDLE hfont;
948     HDC dc;
949     GpGraphics *graphics;
950     GpPath *backup;
951     struct format_string_args args;
952     int i;
953     UINT16 native_height;
954     RectF scaled_layout_rect;
955     TEXTMETRICW textmetric;
956
957     TRACE("(%p, %s, %d, %p, %d, %f, %p, %p)\n", path, debugstr_w(string), length, family, style, emSize, layoutRect, format);
958     if (!path || !string || !family || !emSize || !layoutRect || !format)
959         return InvalidParameter;
960
961     status = GdipGetEmHeight(family, style, &native_height);
962     if (status != Ok)
963         return status;
964
965     scaled_layout_rect.X = layoutRect->X;
966     scaled_layout_rect.Y = layoutRect->Y;
967     scaled_layout_rect.Width = layoutRect->Width * native_height / emSize;
968     scaled_layout_rect.Height = layoutRect->Height * native_height / emSize;
969
970     if ((status = GdipClonePath(path, &backup)) != Ok)
971         return status;
972
973     dc = CreateCompatibleDC(0);
974     status = GdipCreateFromHDC(dc, &graphics);
975     if (status != Ok)
976     {
977         DeleteDC(dc);
978         GdipDeletePath(backup);
979         return status;
980     }
981
982     status = GdipCreateFont(family, native_height, style, UnitPixel, &font);
983     if (status != Ok)
984     {
985         GdipDeleteGraphics(graphics);
986         DeleteDC(dc);
987         GdipDeletePath(backup);
988         return status;
989     }
990
991     get_log_fontW(font, graphics, &lfw);
992     GdipDeleteFont(font);
993     GdipDeleteGraphics(graphics);
994
995     hfont = CreateFontIndirectW(&lfw);
996     if (!hfont)
997     {
998         WARN("Failed to create font\n");
999         DeleteDC(dc);
1000         GdipDeletePath(backup);
1001         return GenericError;
1002     }
1003
1004     SelectObject(dc, hfont);
1005
1006     GetTextMetricsW(dc, &textmetric);
1007
1008     args.path = path;
1009     args.maxY = 0;
1010     args.scale = emSize / native_height;
1011     args.ascent = textmetric.tmAscent * args.scale;
1012     status = gdip_format_string(dc, string, length, NULL, &scaled_layout_rect, format, format_string_callback, &args);
1013
1014     DeleteDC(dc);
1015     DeleteObject(hfont);
1016
1017     if (status != Ok) /* free backup */
1018     {
1019         GdipFree(path->pathdata.Points);
1020         GdipFree(path->pathdata.Types);
1021         *path = *backup;
1022         GdipFree(backup);
1023         return status;
1024     }
1025     if (format && format->vertalign == StringAlignmentCenter && layoutRect->Y + args.maxY < layoutRect->Height)
1026     {
1027         float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1028         inc /= 2;
1029         for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1030             path->pathdata.Points[i].Y += inc;
1031     } else if (format && format->vertalign == StringAlignmentFar) {
1032         float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1033         for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1034             path->pathdata.Points[i].Y += inc;
1035     }
1036     GdipDeletePath(backup);
1037     return status;
1038 }
1039
1040 GpStatus WINGDIPAPI GdipAddPathStringI(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1041 {
1042     if (layoutRect)
1043     {
1044         RectF layoutRectF = {
1045             (REAL)layoutRect->X,
1046             (REAL)layoutRect->Y,
1047             (REAL)layoutRect->Width,
1048             (REAL)layoutRect->Height
1049         };
1050         return GdipAddPathString(path, string, length, family, style, emSize, &layoutRectF, format);
1051     }
1052     return InvalidParameter;
1053 }
1054
1055 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1056 {
1057     TRACE("(%p, %p)\n", path, clone);
1058
1059     if(!path || !clone)
1060         return InvalidParameter;
1061
1062     *clone = GdipAlloc(sizeof(GpPath));
1063     if(!*clone) return OutOfMemory;
1064
1065     **clone = *path;
1066
1067     (*clone)->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
1068     (*clone)->pathdata.Types = GdipAlloc(path->datalen);
1069     if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1070         GdipFree((*clone)->pathdata.Points);
1071         GdipFree((*clone)->pathdata.Types);
1072         GdipFree(*clone);
1073         return OutOfMemory;
1074     }
1075
1076     memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1077            path->datalen * sizeof(PointF));
1078     memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1079
1080     return Ok;
1081 }
1082
1083 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1084 {
1085     TRACE("(%p)\n", path);
1086
1087     if(!path)
1088         return InvalidParameter;
1089
1090     if(path->pathdata.Count > 0){
1091         path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1092         path->newfigure = TRUE;
1093     }
1094
1095     return Ok;
1096 }
1097
1098 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1099 {
1100     INT i;
1101
1102     TRACE("(%p)\n", path);
1103
1104     if(!path)
1105         return InvalidParameter;
1106
1107     for(i = 1; i < path->pathdata.Count; i++){
1108         if(path->pathdata.Types[i] == PathPointTypeStart)
1109             path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1110     }
1111
1112     path->newfigure = TRUE;
1113
1114     return Ok;
1115 }
1116
1117 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1118 {
1119     TRACE("(%d, %p)\n", fill, path);
1120
1121     if(!path)
1122         return InvalidParameter;
1123
1124     *path = GdipAlloc(sizeof(GpPath));
1125     if(!*path)  return OutOfMemory;
1126
1127     (*path)->fill = fill;
1128     (*path)->newfigure = TRUE;
1129
1130     return Ok;
1131 }
1132
1133 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1134     GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1135 {
1136     TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1137
1138     if(!path)
1139         return InvalidParameter;
1140
1141     *path = GdipAlloc(sizeof(GpPath));
1142     if(!*path)  return OutOfMemory;
1143
1144     (*path)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
1145     (*path)->pathdata.Types = GdipAlloc(count);
1146
1147     if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
1148         GdipFree((*path)->pathdata.Points);
1149         GdipFree((*path)->pathdata.Types);
1150         GdipFree(*path);
1151         return OutOfMemory;
1152     }
1153
1154     memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
1155     memcpy((*path)->pathdata.Types, types, count);
1156     (*path)->pathdata.Count = count;
1157     (*path)->datalen = count;
1158
1159     (*path)->fill = fill;
1160     (*path)->newfigure = TRUE;
1161
1162     return Ok;
1163 }
1164
1165 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1166     GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1167 {
1168     GpPointF *ptF;
1169     GpStatus ret;
1170     INT i;
1171
1172     TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1173
1174     ptF = GdipAlloc(sizeof(GpPointF)*count);
1175
1176     for(i = 0;i < count; i++){
1177         ptF[i].X = (REAL)points[i].X;
1178         ptF[i].Y = (REAL)points[i].Y;
1179     }
1180
1181     ret = GdipCreatePath2(ptF, types, count, fill, path);
1182
1183     GdipFree(ptF);
1184
1185     return ret;
1186 }
1187
1188 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1189 {
1190     TRACE("(%p)\n", path);
1191
1192     if(!path)
1193         return InvalidParameter;
1194
1195     GdipFree(path->pathdata.Points);
1196     GdipFree(path->pathdata.Types);
1197     GdipFree(path);
1198
1199     return Ok;
1200 }
1201
1202 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1203 {
1204     path_list_node_t *list, *node;
1205     GpPointF pt;
1206     INT i = 1;
1207     INT startidx = 0;
1208     GpStatus stat;
1209
1210     TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1211
1212     if(!path)
1213         return InvalidParameter;
1214
1215     if(path->pathdata.Count == 0)
1216         return Ok;
1217
1218     if(matrix){
1219         stat = GdipTransformPath(path, matrix);
1220         if (stat != Ok)
1221             return stat;
1222     }
1223
1224     pt = path->pathdata.Points[0];
1225     if(!init_path_list(&list, pt.X, pt.Y))
1226         return OutOfMemory;
1227
1228     node = list;
1229
1230     while(i < path->pathdata.Count){
1231
1232         BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1233         path_list_node_t *start;
1234
1235         pt = path->pathdata.Points[i];
1236
1237         /* save last start point index */
1238         if(type == PathPointTypeStart)
1239             startidx = i;
1240
1241         /* always add line points and start points */
1242         if((type == PathPointTypeStart) || (type == PathPointTypeLine)){
1243             if(!add_path_list_node(node, pt.X, pt.Y, path->pathdata.Types[i]))
1244                 goto memout;
1245
1246             node = node->next;
1247             ++i;
1248             continue;
1249         }
1250
1251         /* Bezier curve */
1252
1253         /* test for closed figure */
1254         if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1255             pt = path->pathdata.Points[startidx];
1256             ++i;
1257         }
1258         else
1259         {
1260             i += 2;
1261             pt = path->pathdata.Points[i];
1262         };
1263
1264         start = node;
1265         /* add Bezier end point */
1266         type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1267         if(!add_path_list_node(node, pt.X, pt.Y, type))
1268             goto memout;
1269         node = node->next;
1270
1271         /* flatten curve */
1272         if(!flatten_bezier(start, path->pathdata.Points[i-2].X, path->pathdata.Points[i-2].Y,
1273                                   path->pathdata.Points[i-1].X, path->pathdata.Points[i-1].Y,
1274                            node, flatness))
1275             goto memout;
1276
1277         ++i;
1278     }/* while */
1279
1280     /* store path data back */
1281     i = path_list_count(list);
1282     if(!lengthen_path(path, i))
1283         goto memout;
1284     path->pathdata.Count = i;
1285
1286     node = list;
1287     for(i = 0; i < path->pathdata.Count; i++){
1288         path->pathdata.Points[i] = node->pt;
1289         path->pathdata.Types[i]  = node->type;
1290         node = node->next;
1291     }
1292
1293     free_path_list(list);
1294     return Ok;
1295
1296 memout:
1297     free_path_list(list);
1298     return OutOfMemory;
1299 }
1300
1301 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1302 {
1303     TRACE("(%p, %p)\n", path, pathData);
1304
1305     if(!path || !pathData)
1306         return InvalidParameter;
1307
1308     /* Only copy data. pathData allocation/freeing controlled by wrapper class.
1309        Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
1310     memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
1311     memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
1312
1313     return Ok;
1314 }
1315
1316 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1317 {
1318     TRACE("(%p, %p)\n", path, fillmode);
1319
1320     if(!path || !fillmode)
1321         return InvalidParameter;
1322
1323     *fillmode = path->fill;
1324
1325     return Ok;
1326 }
1327
1328 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1329 {
1330     INT count;
1331
1332     TRACE("(%p, %p)\n", path, lastPoint);
1333
1334     if(!path || !lastPoint)
1335         return InvalidParameter;
1336
1337     count = path->pathdata.Count;
1338     if(count > 0)
1339         *lastPoint = path->pathdata.Points[count-1];
1340
1341     return Ok;
1342 }
1343
1344 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1345 {
1346     TRACE("(%p, %p, %d)\n", path, points, count);
1347
1348     if(!path)
1349         return InvalidParameter;
1350
1351     if(count < path->pathdata.Count)
1352         return InsufficientBuffer;
1353
1354     memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1355
1356     return Ok;
1357 }
1358
1359 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1360 {
1361     GpStatus ret;
1362     GpPointF *ptf;
1363     INT i;
1364
1365     TRACE("(%p, %p, %d)\n", path, points, count);
1366
1367     if(count <= 0)
1368         return InvalidParameter;
1369
1370     ptf = GdipAlloc(sizeof(GpPointF)*count);
1371     if(!ptf)    return OutOfMemory;
1372
1373     ret = GdipGetPathPoints(path,ptf,count);
1374     if(ret == Ok)
1375         for(i = 0;i < count;i++){
1376             points[i].X = gdip_round(ptf[i].X);
1377             points[i].Y = gdip_round(ptf[i].Y);
1378         };
1379     GdipFree(ptf);
1380
1381     return ret;
1382 }
1383
1384 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1385 {
1386     TRACE("(%p, %p, %d)\n", path, types, count);
1387
1388     if(!path)
1389         return InvalidParameter;
1390
1391     if(count < path->pathdata.Count)
1392         return InsufficientBuffer;
1393
1394     memcpy(types, path->pathdata.Types, path->pathdata.Count);
1395
1396     return Ok;
1397 }
1398
1399 /* Windows expands the bounding box to the maximum possible bounding box
1400  * for a given pen.  For example, if a line join can extend past the point
1401  * it's joining by x units, the bounding box is extended by x units in every
1402  * direction (even though this is too conservative for most cases). */
1403 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
1404     GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1405 {
1406     GpPointF * points, temp_pts[4];
1407     INT count, i;
1408     REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
1409
1410     TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1411
1412     /* Matrix and pen can be null. */
1413     if(!path || !bounds)
1414         return InvalidParameter;
1415
1416     /* If path is empty just return. */
1417     count = path->pathdata.Count;
1418     if(count == 0){
1419         bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
1420         return Ok;
1421     }
1422
1423     points = path->pathdata.Points;
1424
1425     low_x = high_x = points[0].X;
1426     low_y = high_y = points[0].Y;
1427
1428     for(i = 1; i < count; i++){
1429         low_x = min(low_x, points[i].X);
1430         low_y = min(low_y, points[i].Y);
1431         high_x = max(high_x, points[i].X);
1432         high_y = max(high_y, points[i].Y);
1433     }
1434
1435     width = high_x - low_x;
1436     height = high_y - low_y;
1437
1438     /* This looks unusual but it's the only way I can imitate windows. */
1439     if(matrix){
1440         temp_pts[0].X = low_x;
1441         temp_pts[0].Y = low_y;
1442         temp_pts[1].X = low_x;
1443         temp_pts[1].Y = high_y;
1444         temp_pts[2].X = high_x;
1445         temp_pts[2].Y = high_y;
1446         temp_pts[3].X = high_x;
1447         temp_pts[3].Y = low_y;
1448
1449         GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1450         low_x = temp_pts[0].X;
1451         low_y = temp_pts[0].Y;
1452
1453         for(i = 1; i < 4; i++){
1454             low_x = min(low_x, temp_pts[i].X);
1455             low_y = min(low_y, temp_pts[i].Y);
1456         }
1457
1458         temp = width;
1459         width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
1460         height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
1461     }
1462
1463     if(pen){
1464         path_width = pen->width / 2.0;
1465
1466         if(count > 2)
1467             path_width = max(path_width,  pen->width * pen->miterlimit / 2.0);
1468         /* FIXME: this should probably also check for the startcap */
1469         if(pen->endcap & LineCapNoAnchor)
1470             path_width = max(path_width,  pen->width * 2.2);
1471
1472         low_x -= path_width;
1473         low_y -= path_width;
1474         width += 2.0 * path_width;
1475         height += 2.0 * path_width;
1476     }
1477
1478     bounds->X = low_x;
1479     bounds->Y = low_y;
1480     bounds->Width = width;
1481     bounds->Height = height;
1482
1483     return Ok;
1484 }
1485
1486 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1487     GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1488 {
1489     GpStatus ret;
1490     GpRectF boundsF;
1491
1492     TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1493
1494     ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
1495
1496     if(ret == Ok){
1497         bounds->X      = gdip_round(boundsF.X);
1498         bounds->Y      = gdip_round(boundsF.Y);
1499         bounds->Width  = gdip_round(boundsF.Width);
1500         bounds->Height = gdip_round(boundsF.Height);
1501     }
1502
1503     return ret;
1504 }
1505
1506 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1507 {
1508     TRACE("(%p, %p)\n", path, count);
1509
1510     if(!path)
1511         return InvalidParameter;
1512
1513     *count = path->pathdata.Count;
1514
1515     return Ok;
1516 }
1517
1518 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1519 {
1520     INT i, count;
1521     INT start = 0; /* position in reversed path */
1522     GpPathData revpath;
1523
1524     TRACE("(%p)\n", path);
1525
1526     if(!path)
1527         return InvalidParameter;
1528
1529     count = path->pathdata.Count;
1530
1531     if(count == 0) return Ok;
1532
1533     revpath.Points = GdipAlloc(sizeof(GpPointF)*count);
1534     revpath.Types  = GdipAlloc(sizeof(BYTE)*count);
1535     revpath.Count  = count;
1536     if(!revpath.Points || !revpath.Types){
1537         GdipFree(revpath.Points);
1538         GdipFree(revpath.Types);
1539         return OutOfMemory;
1540     }
1541
1542     for(i = 0; i < count; i++){
1543
1544         /* find next start point */
1545         if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1546             INT j;
1547             for(j = start; j <= i; j++){
1548                 revpath.Points[j] = path->pathdata.Points[count-j-1];
1549                 revpath.Types[j] = path->pathdata.Types[count-j-1];
1550             }
1551             /* mark start point */
1552             revpath.Types[start] = PathPointTypeStart;
1553             /* set 'figure' endpoint type */
1554             if(i-start > 1){
1555                 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1556                 revpath.Types[i] |= revpath.Types[i-1];
1557             }
1558             else
1559                 revpath.Types[i] = path->pathdata.Types[start];
1560
1561             start = i+1;
1562         }
1563     }
1564
1565     memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1566     memcpy(path->pathdata.Types,  revpath.Types,  sizeof(BYTE)*count);
1567
1568     GdipFree(revpath.Points);
1569     GdipFree(revpath.Types);
1570
1571     return Ok;
1572 }
1573
1574 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1575     GpPen *pen, GpGraphics *graphics, BOOL *result)
1576 {
1577     TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1578
1579     return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1580 }
1581
1582 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1583     GpPen *pen, GpGraphics *graphics, BOOL *result)
1584 {
1585     static int calls;
1586
1587     TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1588
1589     if(!path || !pen)
1590         return InvalidParameter;
1591
1592     if(!(calls++))
1593         FIXME("not implemented\n");
1594
1595     return NotImplemented;
1596 }
1597
1598 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1599 {
1600     TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1601
1602     return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1603 }
1604
1605 /*****************************************************************************
1606  * GdipIsVisiblePathPoint [GDIPLUS.@]
1607  */
1608 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1609 {
1610     GpRegion *region;
1611     HRGN hrgn;
1612     GpStatus status;
1613
1614     if(!path || !result) return InvalidParameter;
1615
1616     status = GdipCreateRegionPath(path, &region);
1617     if(status != Ok)
1618         return status;
1619
1620     status = GdipGetRegionHRgn(region, graphics, &hrgn);
1621     if(status != Ok){
1622         GdipDeleteRegion(region);
1623         return status;
1624     }
1625
1626     *result = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1627
1628     DeleteObject(hrgn);
1629     GdipDeleteRegion(region);
1630
1631     return Ok;
1632 }
1633
1634 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1635 {
1636     TRACE("(%p)\n", path);
1637
1638     if(!path)
1639         return InvalidParameter;
1640
1641     path->newfigure = TRUE;
1642
1643     return Ok;
1644 }
1645
1646 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1647 {
1648     TRACE("(%p)\n", path);
1649
1650     if(!path)
1651         return InvalidParameter;
1652
1653     path->pathdata.Count = 0;
1654     path->newfigure = TRUE;
1655     path->fill = FillModeAlternate;
1656
1657     return Ok;
1658 }
1659
1660 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1661 {
1662     TRACE("(%p, %d)\n", path, fill);
1663
1664     if(!path)
1665         return InvalidParameter;
1666
1667     path->fill = fill;
1668
1669     return Ok;
1670 }
1671
1672 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1673 {
1674     TRACE("(%p, %p)\n", path, matrix);
1675
1676     if(!path)
1677         return InvalidParameter;
1678
1679     if(path->pathdata.Count == 0)
1680         return Ok;
1681
1682     return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1683                                      path->pathdata.Count);
1684 }
1685
1686 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1687     GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1688     REAL height, WarpMode warpmode, REAL flatness)
1689 {
1690     FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1691         points, count, x, y, width, height, warpmode, flatness);
1692
1693     return NotImplemented;
1694 }
1695
1696 static void add_bevel_point(const GpPointF *endpoint, const GpPointF *nextpoint,
1697     GpPen *pen, int right_side, path_list_node_t **last_point)
1698 {
1699     REAL segment_dy = nextpoint->Y-endpoint->Y;
1700     REAL segment_dx = nextpoint->X-endpoint->X;
1701     REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1702     REAL distance = pen->width/2.0;
1703     REAL bevel_dx, bevel_dy;
1704
1705     if (right_side)
1706     {
1707         bevel_dx = -distance * segment_dy / segment_length;
1708         bevel_dy = distance * segment_dx / segment_length;
1709     }
1710     else
1711     {
1712         bevel_dx = distance * segment_dy / segment_length;
1713         bevel_dy = -distance * segment_dx / segment_length;
1714     }
1715
1716     *last_point = add_path_list_node(*last_point, endpoint->X + bevel_dx,
1717         endpoint->Y + bevel_dy, PathPointTypeLine);
1718 }
1719
1720 static void widen_joint(const GpPointF *p1, const GpPointF *p2, const GpPointF *p3,
1721     GpPen* pen, path_list_node_t **last_point)
1722 {
1723     switch (pen->join)
1724     {
1725     case LineJoinMiter:
1726     case LineJoinMiterClipped:
1727         if ((p2->X - p1->X) * (p3->Y - p1->Y) > (p2->Y - p1->Y) * (p3->X - p1->X))
1728         {
1729             float distance = pen->width/2.0;
1730             float length_0 = sqrtf((p2->X-p1->X)*(p2->X-p1->X)+(p2->Y-p1->Y)*(p2->Y-p1->Y));
1731             float length_1 = sqrtf((p3->X-p2->X)*(p3->X-p2->X)+(p3->Y-p2->Y)*(p3->Y-p2->Y));
1732             float dx0 = distance * (p2->X - p1->X) / length_0;
1733             float dy0 = distance * (p2->Y - p1->Y) / length_0;
1734             float dx1 = distance * (p3->X - p2->X) / length_1;
1735             float dy1 = distance * (p3->Y - p2->Y) / length_1;
1736             float det = (dy0*dx1 - dx0*dy1);
1737             float dx = (dx0*dx1*(dx0-dx1) + dy0*dy0*dx1 - dy1*dy1*dx0)/det;
1738             float dy = (dy0*dy1*(dy0-dy1) + dx0*dx0*dy1 - dx1*dx1*dy0)/det;
1739             if (dx*dx + dy*dy < pen->miterlimit*pen->miterlimit * distance*distance)
1740             {
1741                 *last_point = add_path_list_node(*last_point, p2->X + dx,
1742                     p2->Y + dy, PathPointTypeLine);
1743                 break;
1744             }
1745             else if (pen->join == LineJoinMiter)
1746             {
1747                 static int once;
1748                 if (!once++)
1749                     FIXME("should add a clipped corner\n");
1750             }
1751             /* else fall-through */
1752         }
1753         /* else fall-through */
1754     default:
1755     case LineJoinBevel:
1756         add_bevel_point(p2, p1, pen, 1, last_point);
1757         add_bevel_point(p2, p3, pen, 0, last_point);
1758         break;
1759     }
1760 }
1761
1762 static void widen_cap(const GpPointF *endpoint, const GpPointF *nextpoint,
1763     GpPen *pen, GpLineCap cap, GpCustomLineCap *custom, int add_first_points,
1764     int add_last_point, path_list_node_t **last_point)
1765 {
1766     switch (cap)
1767     {
1768     default:
1769     case LineCapFlat:
1770         if (add_first_points)
1771             add_bevel_point(endpoint, nextpoint, pen, 1, last_point);
1772         if (add_last_point)
1773             add_bevel_point(endpoint, nextpoint, pen, 0, last_point);
1774         break;
1775     case LineCapSquare:
1776     {
1777         REAL segment_dy = nextpoint->Y-endpoint->Y;
1778         REAL segment_dx = nextpoint->X-endpoint->X;
1779         REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1780         REAL distance = pen->width/2.0;
1781         REAL bevel_dx, bevel_dy;
1782         REAL extend_dx, extend_dy;
1783
1784         extend_dx = -distance * segment_dx / segment_length;
1785         extend_dy = -distance * segment_dy / segment_length;
1786
1787         bevel_dx = -distance * segment_dy / segment_length;
1788         bevel_dy = distance * segment_dx / segment_length;
1789
1790         if (add_first_points)
1791             *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx + bevel_dx,
1792                 endpoint->Y + extend_dy + bevel_dy, PathPointTypeLine);
1793
1794         if (add_last_point)
1795             *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx - bevel_dx,
1796                 endpoint->Y + extend_dy - bevel_dy, PathPointTypeLine);
1797
1798         break;
1799     }
1800     case LineCapRound:
1801     {
1802         REAL segment_dy = nextpoint->Y-endpoint->Y;
1803         REAL segment_dx = nextpoint->X-endpoint->X;
1804         REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1805         REAL distance = pen->width/2.0;
1806         REAL dx, dy, dx2, dy2;
1807         const REAL control_point_distance = 0.5522847498307935; /* 4/3 * (sqrt(2) - 1) */
1808
1809         if (add_first_points)
1810         {
1811             dx = -distance * segment_dx / segment_length;
1812             dy = -distance * segment_dy / segment_length;
1813
1814             dx2 = dx * control_point_distance;
1815             dy2 = dy * control_point_distance;
1816
1817             /* first 90-degree arc */
1818             *last_point = add_path_list_node(*last_point, endpoint->X + dy,
1819                 endpoint->Y - dx, PathPointTypeLine);
1820
1821             *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
1822                 endpoint->Y - dx + dy2, PathPointTypeBezier);
1823
1824             *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
1825                 endpoint->Y + dy - dx2, PathPointTypeBezier);
1826
1827             /* midpoint */
1828             *last_point = add_path_list_node(*last_point, endpoint->X + dx,
1829                 endpoint->Y + dy, PathPointTypeBezier);
1830
1831             /* second 90-degree arc */
1832             *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
1833                 endpoint->Y + dy + dx2, PathPointTypeBezier);
1834
1835             *last_point = add_path_list_node(*last_point, endpoint->X - dy + dx2,
1836                 endpoint->Y + dx + dy2, PathPointTypeBezier);
1837
1838             *last_point = add_path_list_node(*last_point, endpoint->X - dy,
1839                 endpoint->Y + dx, PathPointTypeBezier);
1840         }
1841         break;
1842     }
1843     }
1844 }
1845
1846 static void widen_open_figure(GpPath *path, GpPen *pen, int start, int end,
1847     path_list_node_t **last_point)
1848 {
1849     int i;
1850     path_list_node_t *prev_point;
1851
1852     if (end <= start)
1853         return;
1854
1855     prev_point = *last_point;
1856
1857     widen_cap(&path->pathdata.Points[start], &path->pathdata.Points[start+1],
1858         pen, pen->startcap, pen->customstart, FALSE, TRUE, last_point);
1859
1860     for (i=start+1; i<end; i++)
1861         widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
1862             &path->pathdata.Points[i+1], pen, last_point);
1863
1864     widen_cap(&path->pathdata.Points[end], &path->pathdata.Points[end-1],
1865         pen, pen->endcap, pen->customend, TRUE, TRUE, last_point);
1866
1867     for (i=end-1; i>start; i--)
1868         widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
1869             &path->pathdata.Points[i-1], pen, last_point);
1870
1871     widen_cap(&path->pathdata.Points[start], &path->pathdata.Points[start+1],
1872         pen, pen->startcap, pen->customstart, TRUE, FALSE, last_point);
1873
1874     prev_point->next->type = PathPointTypeStart;
1875     (*last_point)->type |= PathPointTypeCloseSubpath;
1876 }
1877
1878 static void widen_closed_figure(GpPath *path, GpPen *pen, int start, int end,
1879     path_list_node_t **last_point)
1880 {
1881     int i;
1882     path_list_node_t *prev_point;
1883
1884     if (end <= start+1)
1885         return;
1886
1887     /* left outline */
1888     prev_point = *last_point;
1889
1890     widen_joint(&path->pathdata.Points[end], &path->pathdata.Points[start],
1891         &path->pathdata.Points[start+1], pen, last_point);
1892
1893     for (i=start+1; i<end; i++)
1894         widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
1895             &path->pathdata.Points[i+1], pen, last_point);
1896
1897     widen_joint(&path->pathdata.Points[end-1], &path->pathdata.Points[end],
1898         &path->pathdata.Points[start], pen, last_point);
1899
1900     prev_point->next->type = PathPointTypeStart;
1901     (*last_point)->type |= PathPointTypeCloseSubpath;
1902
1903     /* right outline */
1904     prev_point = *last_point;
1905
1906     widen_joint(&path->pathdata.Points[start], &path->pathdata.Points[end],
1907         &path->pathdata.Points[end-1], pen, last_point);
1908
1909     for (i=end-1; i>start; i--)
1910         widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
1911             &path->pathdata.Points[i-1], pen, last_point);
1912
1913     widen_joint(&path->pathdata.Points[start+1], &path->pathdata.Points[start],
1914         &path->pathdata.Points[end], pen, last_point);
1915
1916     prev_point->next->type = PathPointTypeStart;
1917     (*last_point)->type |= PathPointTypeCloseSubpath;
1918 }
1919
1920 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
1921     REAL flatness)
1922 {
1923     GpPath *flat_path=NULL;
1924     GpStatus status;
1925     path_list_node_t *points=NULL, *last_point=NULL;
1926     int i, subpath_start=0, new_length;
1927     BYTE type;
1928
1929     TRACE("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
1930
1931     if (!path || !pen)
1932         return InvalidParameter;
1933
1934     if (path->pathdata.Count <= 1)
1935         return OutOfMemory;
1936
1937     status = GdipClonePath(path, &flat_path);
1938
1939     if (status == Ok)
1940         status = GdipFlattenPath(flat_path, matrix, flatness);
1941
1942     if (status == Ok && !init_path_list(&points, 314.0, 22.0))
1943         status = OutOfMemory;
1944
1945     if (status == Ok)
1946     {
1947         last_point = points;
1948
1949         if (pen->endcap > LineCapRound)
1950             FIXME("unimplemented end cap %x\n", pen->endcap);
1951
1952         if (pen->startcap > LineCapRound)
1953             FIXME("unimplemented start cap %x\n", pen->startcap);
1954
1955         if (pen->dashcap != DashCapFlat)
1956             FIXME("unimplemented dash cap %d\n", pen->dashcap);
1957
1958         if (pen->join == LineJoinRound)
1959             FIXME("unimplemented line join %d\n", pen->join);
1960
1961         if (pen->dash != DashStyleSolid)
1962             FIXME("unimplemented dash style %d\n", pen->dash);
1963
1964         if (pen->align != PenAlignmentCenter)
1965             FIXME("unimplemented pen alignment %d\n", pen->align);
1966
1967         for (i=0; i < flat_path->pathdata.Count; i++)
1968         {
1969             type = flat_path->pathdata.Types[i];
1970
1971             if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1972                 subpath_start = i;
1973
1974             if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
1975             {
1976                 widen_closed_figure(flat_path, pen, subpath_start, i, &last_point);
1977             }
1978             else if (i == flat_path->pathdata.Count-1 ||
1979                 (flat_path->pathdata.Types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
1980             {
1981                 widen_open_figure(flat_path, pen, subpath_start, i, &last_point);
1982             }
1983         }
1984
1985         new_length = path_list_count(points)-1;
1986
1987         if (!lengthen_path(path, new_length))
1988             status = OutOfMemory;
1989     }
1990
1991     if (status == Ok)
1992     {
1993         path->pathdata.Count = new_length;
1994
1995         last_point = points->next;
1996         for (i = 0; i < new_length; i++)
1997         {
1998             path->pathdata.Points[i] = last_point->pt;
1999             path->pathdata.Types[i] = last_point->type;
2000             last_point = last_point->next;
2001         }
2002
2003         path->fill = FillModeWinding;
2004     }
2005
2006     free_path_list(points);
2007
2008     GdipDeletePath(flat_path);
2009
2010     return status;
2011 }
2012
2013 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
2014     REAL width, REAL height)
2015 {
2016     GpPath *backup;
2017     GpPointF ptf[2];
2018     GpStatus retstat;
2019     BOOL old_new;
2020
2021     TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
2022
2023     if(!path)
2024         return InvalidParameter;
2025
2026     /* make a backup copy of path data */
2027     if((retstat = GdipClonePath(path, &backup)) != Ok)
2028         return retstat;
2029
2030     /* rectangle should start as new path */
2031     old_new = path->newfigure;
2032     path->newfigure = TRUE;
2033     if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
2034         path->newfigure = old_new;
2035         goto fail;
2036     }
2037
2038     ptf[0].X = x+width;
2039     ptf[0].Y = y+height;
2040     ptf[1].X = x;
2041     ptf[1].Y = y+height;
2042
2043     if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok)  goto fail;
2044     path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
2045
2046     /* free backup */
2047     GdipDeletePath(backup);
2048     return Ok;
2049
2050 fail:
2051     /* reverting */
2052     GdipFree(path->pathdata.Points);
2053     GdipFree(path->pathdata.Types);
2054     memcpy(path, backup, sizeof(*path));
2055     GdipFree(backup);
2056
2057     return retstat;
2058 }
2059
2060 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
2061     INT width, INT height)
2062 {
2063     TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
2064
2065     return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2066 }
2067
2068 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
2069 {
2070     GpPath *backup;
2071     GpStatus retstat;
2072     INT i;
2073
2074     TRACE("(%p, %p, %d)\n", path, rects, count);
2075
2076     /* count == 0 - verified condition  */
2077     if(!path || !rects || count == 0)
2078         return InvalidParameter;
2079
2080     if(count < 0)
2081         return OutOfMemory;
2082
2083     /* make a backup copy */
2084     if((retstat = GdipClonePath(path, &backup)) != Ok)
2085         return retstat;
2086
2087     for(i = 0; i < count; i++){
2088         if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
2089             goto fail;
2090     }
2091
2092     /* free backup */
2093     GdipDeletePath(backup);
2094     return Ok;
2095
2096 fail:
2097     /* reverting */
2098     GdipFree(path->pathdata.Points);
2099     GdipFree(path->pathdata.Types);
2100     memcpy(path, backup, sizeof(*path));
2101     GdipFree(backup);
2102
2103     return retstat;
2104 }
2105
2106 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
2107 {
2108     GpRectF *rectsF;
2109     GpStatus retstat;
2110     INT i;
2111
2112     TRACE("(%p, %p, %d)\n", path, rects, count);
2113
2114     if(!rects || count == 0)
2115         return InvalidParameter;
2116
2117     if(count < 0)
2118         return OutOfMemory;
2119
2120     rectsF = GdipAlloc(sizeof(GpRectF)*count);
2121
2122     for(i = 0;i < count;i++){
2123         rectsF[i].X      = (REAL)rects[i].X;
2124         rectsF[i].Y      = (REAL)rects[i].Y;
2125         rectsF[i].Width  = (REAL)rects[i].Width;
2126         rectsF[i].Height = (REAL)rects[i].Height;
2127     }
2128
2129     retstat = GdipAddPathRectangles(path, rectsF, count);
2130     GdipFree(rectsF);
2131
2132     return retstat;
2133 }
2134
2135 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
2136 {
2137     INT count;
2138
2139     TRACE("(%p)\n", path);
2140
2141     if(!path)
2142         return InvalidParameter;
2143
2144     count = path->pathdata.Count;
2145
2146     /* set marker flag */
2147     if(count > 0)
2148         path->pathdata.Types[count-1] |= PathPointTypePathMarker;
2149
2150     return Ok;
2151 }
2152
2153 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
2154 {
2155     INT count;
2156     INT i;
2157
2158     TRACE("(%p)\n", path);
2159
2160     if(!path)
2161         return InvalidParameter;
2162
2163     count = path->pathdata.Count;
2164
2165     for(i = 0; i < count - 1; i++){
2166         path->pathdata.Types[i] &= ~PathPointTypePathMarker;
2167     }
2168
2169     return Ok;
2170 }
2171
2172 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
2173 {
2174    FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
2175    return NotImplemented;
2176 }