ws2_32: Return the right value for SO_CONNECT_TIME if the socket is not connected.
[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 halfs */
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, void *priv)
850 {
851     static const MAT2 identity = { {0,1}, {0,0}, {0,0}, {0,1} };
852     struct format_string_args *args = priv;
853     GpPath *path = args->path;
854     GpStatus status = Ok;
855     float x = bounds->X;
856     float y = bounds->Y;
857     int i;
858
859     for (i = index; i < length; ++i)
860     {
861         GLYPHMETRICS gm;
862         TTPOLYGONHEADER *ph = NULL;
863         char *start;
864         DWORD len, ofs = 0;
865         UINT bb_end;
866         len = GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, 0, NULL, &identity);
867         if (len == GDI_ERROR)
868         {
869             status = GenericError;
870             break;
871         }
872         ph = GdipAlloc(len);
873         start = (char *)ph;
874         if (!ph || !lengthen_path(path, len / sizeof(POINTFX)))
875         {
876             status = OutOfMemory;
877             break;
878         }
879         GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, len, start, &identity);
880         bb_end = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y;
881         if (bb_end + y > args->maxY)
882             args->maxY = bb_end + y;
883
884         ofs = 0;
885         while (ofs < len)
886         {
887             DWORD ofs_start = ofs;
888             ph = (TTPOLYGONHEADER*)&start[ofs];
889             path->pathdata.Types[path->pathdata.Count] = PathPointTypeStart;
890             path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(ph->pfxStart.x);
891             path->pathdata.Points[path->pathdata.Count++].Y = y + bb_end - fromfixedpoint(ph->pfxStart.y);
892             TRACE("Starting at count %i with pos %f, %f)\n", path->pathdata.Count, x, y);
893             ofs += sizeof(*ph);
894             while (ofs - ofs_start < ph->cb)
895             {
896                 TTPOLYCURVE *curve = (TTPOLYCURVE*)&start[ofs];
897                 int j;
898                 ofs += sizeof(TTPOLYCURVE) + (curve->cpfx - 1) * sizeof(POINTFX);
899
900                 switch (curve->wType)
901                 {
902                 case TT_PRIM_LINE:
903                     for (j = 0; j < curve->cpfx; ++j)
904                     {
905                         path->pathdata.Types[path->pathdata.Count] = PathPointTypeLine;
906                         path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x);
907                         path->pathdata.Points[path->pathdata.Count++].Y = y + bb_end - fromfixedpoint(curve->apfx[j].y);
908                     }
909                     break;
910                 case TT_PRIM_CSPLINE:
911                     for (j = 0; j < curve->cpfx; ++j)
912                     {
913                         path->pathdata.Types[path->pathdata.Count] = PathPointTypeBezier;
914                         path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x);
915                         path->pathdata.Points[path->pathdata.Count++].Y = y + bb_end - fromfixedpoint(curve->apfx[j].y);
916                     }
917                     break;
918                 default:
919                     ERR("Unhandled type: %u\n", curve->wType);
920                     status = GenericError;
921                 }
922             }
923             path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
924         }
925         path->newfigure = TRUE;
926         x += gm.gmCellIncX;
927         y += gm.gmCellIncY;
928
929         GdipFree(ph);
930         if (status != Ok)
931             break;
932     }
933
934     return status;
935 }
936
937 GpStatus WINGDIPAPI GdipAddPathString(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat* format)
938 {
939     GpFont *font;
940     GpStatus status;
941     HANDLE hfont;
942     HDC dc;
943     GpPath *backup;
944     struct format_string_args args;
945     int i;
946
947     FIXME("(%p, %s, %d, %p, %d, %f, %p, %p): stub\n", path, debugstr_w(string), length, family, style, emSize, layoutRect, format);
948     if (!path || !string || !family || !emSize || !layoutRect || !format)
949         return InvalidParameter;
950
951     status = GdipCreateFont(family, emSize, style, UnitPixel, &font);
952     if (status != Ok)
953         return status;
954
955     hfont = CreateFontIndirectW(&font->lfw);
956     if (!hfont)
957     {
958         WARN("Failed to create font\n");
959         return GenericError;
960     }
961
962     if ((status = GdipClonePath(path, &backup)) != Ok)
963     {
964         DeleteObject(hfont);
965         return status;
966     }
967
968     dc = CreateCompatibleDC(0);
969     SelectObject(dc, hfont);
970
971     args.path = path;
972     args.maxY = 0;
973     status = gdip_format_string(dc, string, length, NULL, layoutRect, format, format_string_callback, &args);
974
975     DeleteDC(dc);
976     DeleteObject(hfont);
977
978     if (status != Ok) /* free backup */
979     {
980         GdipFree(path->pathdata.Points);
981         GdipFree(path->pathdata.Types);
982         *path = *backup;
983         GdipFree(backup);
984         return status;
985     }
986     if (format && format->vertalign == StringAlignmentCenter && layoutRect->Y + args.maxY < layoutRect->Height)
987     {
988         float inc = layoutRect->Height - args.maxY - layoutRect->Y;
989         inc /= 2;
990         for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
991             path->pathdata.Points[i].Y += inc;
992     } else if (format && format->vertalign == StringAlignmentFar) {
993         float inc = layoutRect->Height - args.maxY - layoutRect->Y;
994         for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
995             path->pathdata.Points[i].Y += inc;
996     }
997     GdipDeletePath(backup);
998     return status;
999 }
1000
1001 GpStatus WINGDIPAPI GdipAddPathStringI(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1002 {
1003     if (layoutRect)
1004     {
1005         RectF layoutRectF = {
1006             (REAL)layoutRect->X,
1007             (REAL)layoutRect->Y,
1008             (REAL)layoutRect->Width,
1009             (REAL)layoutRect->Height
1010         };
1011         return GdipAddPathString(path, string, length, family, style, emSize, &layoutRectF, format);
1012     }
1013     return InvalidParameter;
1014 }
1015
1016 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1017 {
1018     TRACE("(%p, %p)\n", path, clone);
1019
1020     if(!path || !clone)
1021         return InvalidParameter;
1022
1023     *clone = GdipAlloc(sizeof(GpPath));
1024     if(!*clone) return OutOfMemory;
1025
1026     **clone = *path;
1027
1028     (*clone)->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
1029     (*clone)->pathdata.Types = GdipAlloc(path->datalen);
1030     if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1031         GdipFree(*clone);
1032         GdipFree((*clone)->pathdata.Points);
1033         GdipFree((*clone)->pathdata.Types);
1034         return OutOfMemory;
1035     }
1036
1037     memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1038            path->datalen * sizeof(PointF));
1039     memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1040
1041     return Ok;
1042 }
1043
1044 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1045 {
1046     TRACE("(%p)\n", path);
1047
1048     if(!path)
1049         return InvalidParameter;
1050
1051     if(path->pathdata.Count > 0){
1052         path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1053         path->newfigure = TRUE;
1054     }
1055
1056     return Ok;
1057 }
1058
1059 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1060 {
1061     INT i;
1062
1063     TRACE("(%p)\n", path);
1064
1065     if(!path)
1066         return InvalidParameter;
1067
1068     for(i = 1; i < path->pathdata.Count; i++){
1069         if(path->pathdata.Types[i] == PathPointTypeStart)
1070             path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1071     }
1072
1073     path->newfigure = TRUE;
1074
1075     return Ok;
1076 }
1077
1078 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1079 {
1080     TRACE("(%d, %p)\n", fill, path);
1081
1082     if(!path)
1083         return InvalidParameter;
1084
1085     *path = GdipAlloc(sizeof(GpPath));
1086     if(!*path)  return OutOfMemory;
1087
1088     (*path)->fill = fill;
1089     (*path)->newfigure = TRUE;
1090
1091     return Ok;
1092 }
1093
1094 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1095     GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1096 {
1097     TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1098
1099     if(!path)
1100         return InvalidParameter;
1101
1102     *path = GdipAlloc(sizeof(GpPath));
1103     if(!*path)  return OutOfMemory;
1104
1105     (*path)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
1106     (*path)->pathdata.Types = GdipAlloc(count);
1107
1108     if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
1109         GdipFree((*path)->pathdata.Points);
1110         GdipFree((*path)->pathdata.Types);
1111         GdipFree(*path);
1112         return OutOfMemory;
1113     }
1114
1115     memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
1116     memcpy((*path)->pathdata.Types, types, count);
1117     (*path)->pathdata.Count = count;
1118     (*path)->datalen = count;
1119
1120     (*path)->fill = fill;
1121     (*path)->newfigure = TRUE;
1122
1123     return Ok;
1124 }
1125
1126 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1127     GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1128 {
1129     GpPointF *ptF;
1130     GpStatus ret;
1131     INT i;
1132
1133     TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1134
1135     ptF = GdipAlloc(sizeof(GpPointF)*count);
1136
1137     for(i = 0;i < count; i++){
1138         ptF[i].X = (REAL)points[i].X;
1139         ptF[i].Y = (REAL)points[i].Y;
1140     }
1141
1142     ret = GdipCreatePath2(ptF, types, count, fill, path);
1143
1144     GdipFree(ptF);
1145
1146     return ret;
1147 }
1148
1149 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1150 {
1151     TRACE("(%p)\n", path);
1152
1153     if(!path)
1154         return InvalidParameter;
1155
1156     GdipFree(path->pathdata.Points);
1157     GdipFree(path->pathdata.Types);
1158     GdipFree(path);
1159
1160     return Ok;
1161 }
1162
1163 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1164 {
1165     path_list_node_t *list, *node;
1166     GpPointF pt;
1167     INT i = 1;
1168     INT startidx = 0;
1169
1170     TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1171
1172     if(!path)
1173         return InvalidParameter;
1174
1175     if(matrix){
1176         WARN("transformation not supported yet!\n");
1177         return NotImplemented;
1178     }
1179
1180     if(path->pathdata.Count == 0)
1181         return Ok;
1182
1183     pt = path->pathdata.Points[0];
1184     if(!init_path_list(&list, pt.X, pt.Y))
1185         return OutOfMemory;
1186
1187     node = list;
1188
1189     while(i < path->pathdata.Count){
1190
1191         BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1192         path_list_node_t *start;
1193
1194         pt = path->pathdata.Points[i];
1195
1196         /* save last start point index */
1197         if(type == PathPointTypeStart)
1198             startidx = i;
1199
1200         /* always add line points and start points */
1201         if((type == PathPointTypeStart) || (type == PathPointTypeLine)){
1202             if(!add_path_list_node(node, pt.X, pt.Y, path->pathdata.Types[i]))
1203                 goto memout;
1204
1205             node = node->next;
1206             ++i;
1207             continue;
1208         }
1209
1210         /* Bezier curve always stored as 4 points */
1211         if((path->pathdata.Types[i-1] & PathPointTypePathTypeMask) != PathPointTypeStart){
1212             type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1213             if(!add_path_list_node(node, pt.X, pt.Y, type))
1214                 goto memout;
1215
1216             node = node->next;
1217         }
1218
1219         /* test for closed figure */
1220         if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1221             pt = path->pathdata.Points[startidx];
1222             ++i;
1223         }
1224         else
1225         {
1226             i += 2;
1227             pt = path->pathdata.Points[i];
1228         };
1229
1230         start = node;
1231         /* add Bezier end point */
1232         type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1233         if(!add_path_list_node(node, pt.X, pt.Y, type))
1234             goto memout;
1235         node = node->next;
1236
1237         /* flatten curve */
1238         if(!flatten_bezier(start, path->pathdata.Points[i-2].X, path->pathdata.Points[i-2].Y,
1239                                   path->pathdata.Points[i-1].X, path->pathdata.Points[i-1].Y,
1240                            node, flatness))
1241             goto memout;
1242
1243         ++i;
1244     }/* while */
1245
1246     /* store path data back */
1247     i = path_list_count(list);
1248     if(!lengthen_path(path, i))
1249         goto memout;
1250     path->pathdata.Count = i;
1251
1252     node = list;
1253     for(i = 0; i < path->pathdata.Count; i++){
1254         path->pathdata.Points[i] = node->pt;
1255         path->pathdata.Types[i]  = node->type;
1256         node = node->next;
1257     }
1258
1259     free_path_list(list);
1260     return Ok;
1261
1262 memout:
1263     free_path_list(list);
1264     return OutOfMemory;
1265 }
1266
1267 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1268 {
1269     TRACE("(%p, %p)\n", path, pathData);
1270
1271     if(!path || !pathData)
1272         return InvalidParameter;
1273
1274     /* Only copy data. pathData allocation/freeing controlled by wrapper class.
1275        Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
1276     memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
1277     memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
1278
1279     return Ok;
1280 }
1281
1282 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1283 {
1284     TRACE("(%p, %p)\n", path, fillmode);
1285
1286     if(!path || !fillmode)
1287         return InvalidParameter;
1288
1289     *fillmode = path->fill;
1290
1291     return Ok;
1292 }
1293
1294 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1295 {
1296     INT count;
1297
1298     TRACE("(%p, %p)\n", path, lastPoint);
1299
1300     if(!path || !lastPoint)
1301         return InvalidParameter;
1302
1303     count = path->pathdata.Count;
1304     if(count > 0)
1305         *lastPoint = path->pathdata.Points[count-1];
1306
1307     return Ok;
1308 }
1309
1310 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1311 {
1312     TRACE("(%p, %p, %d)\n", path, points, count);
1313
1314     if(!path)
1315         return InvalidParameter;
1316
1317     if(count < path->pathdata.Count)
1318         return InsufficientBuffer;
1319
1320     memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1321
1322     return Ok;
1323 }
1324
1325 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1326 {
1327     GpStatus ret;
1328     GpPointF *ptf;
1329     INT i;
1330
1331     TRACE("(%p, %p, %d)\n", path, points, count);
1332
1333     if(count <= 0)
1334         return InvalidParameter;
1335
1336     ptf = GdipAlloc(sizeof(GpPointF)*count);
1337     if(!ptf)    return OutOfMemory;
1338
1339     ret = GdipGetPathPoints(path,ptf,count);
1340     if(ret == Ok)
1341         for(i = 0;i < count;i++){
1342             points[i].X = roundr(ptf[i].X);
1343             points[i].Y = roundr(ptf[i].Y);
1344         };
1345     GdipFree(ptf);
1346
1347     return ret;
1348 }
1349
1350 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1351 {
1352     TRACE("(%p, %p, %d)\n", path, types, count);
1353
1354     if(!path)
1355         return InvalidParameter;
1356
1357     if(count < path->pathdata.Count)
1358         return InsufficientBuffer;
1359
1360     memcpy(types, path->pathdata.Types, path->pathdata.Count);
1361
1362     return Ok;
1363 }
1364
1365 /* Windows expands the bounding box to the maximum possible bounding box
1366  * for a given pen.  For example, if a line join can extend past the point
1367  * it's joining by x units, the bounding box is extended by x units in every
1368  * direction (even though this is too conservative for most cases). */
1369 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
1370     GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1371 {
1372     GpPointF * points, temp_pts[4];
1373     INT count, i;
1374     REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
1375
1376     TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1377
1378     /* Matrix and pen can be null. */
1379     if(!path || !bounds)
1380         return InvalidParameter;
1381
1382     /* If path is empty just return. */
1383     count = path->pathdata.Count;
1384     if(count == 0){
1385         bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
1386         return Ok;
1387     }
1388
1389     points = path->pathdata.Points;
1390
1391     low_x = high_x = points[0].X;
1392     low_y = high_y = points[0].Y;
1393
1394     for(i = 1; i < count; i++){
1395         low_x = min(low_x, points[i].X);
1396         low_y = min(low_y, points[i].Y);
1397         high_x = max(high_x, points[i].X);
1398         high_y = max(high_y, points[i].Y);
1399     }
1400
1401     width = high_x - low_x;
1402     height = high_y - low_y;
1403
1404     /* This looks unusual but it's the only way I can imitate windows. */
1405     if(matrix){
1406         temp_pts[0].X = low_x;
1407         temp_pts[0].Y = low_y;
1408         temp_pts[1].X = low_x;
1409         temp_pts[1].Y = high_y;
1410         temp_pts[2].X = high_x;
1411         temp_pts[2].Y = high_y;
1412         temp_pts[3].X = high_x;
1413         temp_pts[3].Y = low_y;
1414
1415         GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1416         low_x = temp_pts[0].X;
1417         low_y = temp_pts[0].Y;
1418
1419         for(i = 1; i < 4; i++){
1420             low_x = min(low_x, temp_pts[i].X);
1421             low_y = min(low_y, temp_pts[i].Y);
1422         }
1423
1424         temp = width;
1425         width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
1426         height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
1427     }
1428
1429     if(pen){
1430         path_width = pen->width / 2.0;
1431
1432         if(count > 2)
1433             path_width = max(path_width,  pen->width * pen->miterlimit / 2.0);
1434         /* FIXME: this should probably also check for the startcap */
1435         if(pen->endcap & LineCapNoAnchor)
1436             path_width = max(path_width,  pen->width * 2.2);
1437
1438         low_x -= path_width;
1439         low_y -= path_width;
1440         width += 2.0 * path_width;
1441         height += 2.0 * path_width;
1442     }
1443
1444     bounds->X = low_x;
1445     bounds->Y = low_y;
1446     bounds->Width = width;
1447     bounds->Height = height;
1448
1449     return Ok;
1450 }
1451
1452 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1453     GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1454 {
1455     GpStatus ret;
1456     GpRectF boundsF;
1457
1458     TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1459
1460     ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
1461
1462     if(ret == Ok){
1463         bounds->X      = roundr(boundsF.X);
1464         bounds->Y      = roundr(boundsF.Y);
1465         bounds->Width  = roundr(boundsF.Width);
1466         bounds->Height = roundr(boundsF.Height);
1467     }
1468
1469     return ret;
1470 }
1471
1472 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1473 {
1474     TRACE("(%p, %p)\n", path, count);
1475
1476     if(!path)
1477         return InvalidParameter;
1478
1479     *count = path->pathdata.Count;
1480
1481     return Ok;
1482 }
1483
1484 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1485 {
1486     INT i, count;
1487     INT start = 0; /* position in reversed path */
1488     GpPathData revpath;
1489
1490     TRACE("(%p)\n", path);
1491
1492     if(!path)
1493         return InvalidParameter;
1494
1495     count = path->pathdata.Count;
1496
1497     if(count == 0) return Ok;
1498
1499     revpath.Points = GdipAlloc(sizeof(GpPointF)*count);
1500     revpath.Types  = GdipAlloc(sizeof(BYTE)*count);
1501     revpath.Count  = count;
1502     if(!revpath.Points || !revpath.Types){
1503         GdipFree(revpath.Points);
1504         GdipFree(revpath.Types);
1505         return OutOfMemory;
1506     }
1507
1508     for(i = 0; i < count; i++){
1509
1510         /* find next start point */
1511         if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1512             INT j;
1513             for(j = start; j <= i; j++){
1514                 revpath.Points[j] = path->pathdata.Points[count-j-1];
1515                 revpath.Types[j] = path->pathdata.Types[count-j-1];
1516             }
1517             /* mark start point */
1518             revpath.Types[start] = PathPointTypeStart;
1519             /* set 'figure' endpoint type */
1520             if(i-start > 1){
1521                 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1522                 revpath.Types[i] |= revpath.Types[i-1];
1523             }
1524             else
1525                 revpath.Types[i] = path->pathdata.Types[start];
1526
1527             start = i+1;
1528         }
1529     }
1530
1531     memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1532     memcpy(path->pathdata.Types,  revpath.Types,  sizeof(BYTE)*count);
1533
1534     GdipFree(revpath.Points);
1535     GdipFree(revpath.Types);
1536
1537     return Ok;
1538 }
1539
1540 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1541     GpPen *pen, GpGraphics *graphics, BOOL *result)
1542 {
1543     TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1544
1545     return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1546 }
1547
1548 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1549     GpPen *pen, GpGraphics *graphics, BOOL *result)
1550 {
1551     static int calls;
1552
1553     TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1554
1555     if(!path || !pen)
1556         return InvalidParameter;
1557
1558     if(!(calls++))
1559         FIXME("not implemented\n");
1560
1561     return NotImplemented;
1562 }
1563
1564 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1565 {
1566     TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1567
1568     return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1569 }
1570
1571 /*****************************************************************************
1572  * GdipIsVisiblePathPoint [GDIPLUS.@]
1573  */
1574 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1575 {
1576     GpRegion *region;
1577     HRGN hrgn;
1578     GpStatus status;
1579
1580     if(!path || !result) return InvalidParameter;
1581
1582     status = GdipCreateRegionPath(path, &region);
1583     if(status != Ok)
1584         return status;
1585
1586     status = GdipGetRegionHRgn(region, graphics, &hrgn);
1587     if(status != Ok){
1588         GdipDeleteRegion(region);
1589         return status;
1590     }
1591
1592     *result = PtInRegion(hrgn, roundr(x), roundr(y));
1593
1594     DeleteObject(hrgn);
1595     GdipDeleteRegion(region);
1596
1597     return Ok;
1598 }
1599
1600 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1601 {
1602     TRACE("(%p)\n", path);
1603
1604     if(!path)
1605         return InvalidParameter;
1606
1607     path->newfigure = TRUE;
1608
1609     return Ok;
1610 }
1611
1612 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1613 {
1614     TRACE("(%p)\n", path);
1615
1616     if(!path)
1617         return InvalidParameter;
1618
1619     path->pathdata.Count = 0;
1620     path->newfigure = TRUE;
1621     path->fill = FillModeAlternate;
1622
1623     return Ok;
1624 }
1625
1626 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1627 {
1628     TRACE("(%p, %d)\n", path, fill);
1629
1630     if(!path)
1631         return InvalidParameter;
1632
1633     path->fill = fill;
1634
1635     return Ok;
1636 }
1637
1638 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1639 {
1640     TRACE("(%p, %p)\n", path, matrix);
1641
1642     if(!path)
1643         return InvalidParameter;
1644
1645     if(path->pathdata.Count == 0)
1646         return Ok;
1647
1648     return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1649                                      path->pathdata.Count);
1650 }
1651
1652 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1653     GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1654     REAL height, WarpMode warpmode, REAL flatness)
1655 {
1656     FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1657         points, count, x, y, width, height, warpmode, flatness);
1658
1659     return NotImplemented;
1660 }
1661
1662 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
1663     REAL flatness)
1664 {
1665     FIXME("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
1666
1667     return NotImplemented;
1668 }
1669
1670 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
1671     REAL width, REAL height)
1672 {
1673     GpPath *backup;
1674     GpPointF ptf[2];
1675     GpStatus retstat;
1676     BOOL old_new;
1677
1678     TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
1679
1680     if(!path)
1681         return InvalidParameter;
1682
1683     /* make a backup copy of path data */
1684     if((retstat = GdipClonePath(path, &backup)) != Ok)
1685         return retstat;
1686
1687     /* rectangle should start as new path */
1688     old_new = path->newfigure;
1689     path->newfigure = TRUE;
1690     if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
1691         path->newfigure = old_new;
1692         goto fail;
1693     }
1694
1695     ptf[0].X = x+width;
1696     ptf[0].Y = y+height;
1697     ptf[1].X = x;
1698     ptf[1].Y = y+height;
1699
1700     if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok)  goto fail;
1701     path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
1702
1703     /* free backup */
1704     GdipDeletePath(backup);
1705     return Ok;
1706
1707 fail:
1708     /* reverting */
1709     GdipFree(path->pathdata.Points);
1710     GdipFree(path->pathdata.Types);
1711     memcpy(path, backup, sizeof(*path));
1712     GdipFree(backup);
1713
1714     return retstat;
1715 }
1716
1717 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
1718     INT width, INT height)
1719 {
1720     TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
1721
1722     return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1723 }
1724
1725 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
1726 {
1727     GpPath *backup;
1728     GpStatus retstat;
1729     INT i;
1730
1731     TRACE("(%p, %p, %d)\n", path, rects, count);
1732
1733     /* count == 0 - verified condition  */
1734     if(!path || !rects || count == 0)
1735         return InvalidParameter;
1736
1737     if(count < 0)
1738         return OutOfMemory;
1739
1740     /* make a backup copy */
1741     if((retstat = GdipClonePath(path, &backup)) != Ok)
1742         return retstat;
1743
1744     for(i = 0; i < count; i++){
1745         if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
1746             goto fail;
1747     }
1748
1749     /* free backup */
1750     GdipDeletePath(backup);
1751     return Ok;
1752
1753 fail:
1754     /* reverting */
1755     GdipFree(path->pathdata.Points);
1756     GdipFree(path->pathdata.Types);
1757     memcpy(path, backup, sizeof(*path));
1758     GdipFree(backup);
1759
1760     return retstat;
1761 }
1762
1763 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
1764 {
1765     GpRectF *rectsF;
1766     GpStatus retstat;
1767     INT i;
1768
1769     TRACE("(%p, %p, %d)\n", path, rects, count);
1770
1771     if(!rects || count == 0)
1772         return InvalidParameter;
1773
1774     if(count < 0)
1775         return OutOfMemory;
1776
1777     rectsF = GdipAlloc(sizeof(GpRectF)*count);
1778
1779     for(i = 0;i < count;i++){
1780         rectsF[i].X      = (REAL)rects[i].X;
1781         rectsF[i].Y      = (REAL)rects[i].Y;
1782         rectsF[i].Width  = (REAL)rects[i].Width;
1783         rectsF[i].Height = (REAL)rects[i].Height;
1784     }
1785
1786     retstat = GdipAddPathRectangles(path, rectsF, count);
1787     GdipFree(rectsF);
1788
1789     return retstat;
1790 }
1791
1792 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
1793 {
1794     INT count;
1795
1796     TRACE("(%p)\n", path);
1797
1798     if(!path)
1799         return InvalidParameter;
1800
1801     count = path->pathdata.Count;
1802
1803     /* set marker flag */
1804     if(count > 0)
1805         path->pathdata.Types[count-1] |= PathPointTypePathMarker;
1806
1807     return Ok;
1808 }
1809
1810 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
1811 {
1812     INT count;
1813     INT i;
1814
1815     TRACE("(%p)\n", path);
1816
1817     if(!path)
1818         return InvalidParameter;
1819
1820     count = path->pathdata.Count;
1821
1822     for(i = 0; i < count - 1; i++){
1823         path->pathdata.Types[i] &= ~PathPointTypePathMarker;
1824     }
1825
1826     return Ok;
1827 }
1828
1829 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
1830 {
1831    FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
1832    return NotImplemented;
1833 }