gdiplus: Fix memory leak in format_string_callback error path.
[wine.git] / dlls / gdiplus / graphicspath.c
blob5a3356aa14c5690c05544ae567ca5188588b74ac
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
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.
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.
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
20 #include <stdarg.h>
21 #include <math.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
28 #include "objbase.h"
30 #include "gdiplus.h"
31 #include "gdiplus_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
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;
43 /* init list */
44 static BOOL init_path_list(path_list_node_t **node, REAL x, REAL y)
46 *node = heap_alloc_zero(sizeof(path_list_node_t));
47 if(!*node)
48 return FALSE;
50 (*node)->pt.X = x;
51 (*node)->pt.Y = y;
52 (*node)->type = PathPointTypeStart;
53 (*node)->next = NULL;
55 return TRUE;
58 /* free all nodes including argument */
59 static void free_path_list(path_list_node_t *node)
61 path_list_node_t *n = node;
63 while(n){
64 n = n->next;
65 heap_free(node);
66 node = n;
70 /* Add a node after 'node' */
72 * Returns
73 * pointer on success
74 * NULL on allocation problems
76 static path_list_node_t* add_path_list_node(path_list_node_t *node, REAL x, REAL y, BOOL type)
78 path_list_node_t *new;
80 new = heap_alloc_zero(sizeof(path_list_node_t));
81 if(!new)
82 return NULL;
84 new->pt.X = x;
85 new->pt.Y = y;
86 new->type = type;
87 new->next = node->next;
88 node->next = new;
90 return new;
93 /* returns element count */
94 static INT path_list_count(path_list_node_t *node)
96 INT count = 1;
98 while((node = node->next))
99 ++count;
101 return count;
104 /* GdipFlattenPath helper */
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.
114 * Return value:
115 * TRUE : success
116 * FALSE: out of memory
118 * TODO: used quality criteria should be revised to match native as
119 * closer as possible.
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)
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;
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;
142 mp[2].X = (mp[1].X + mp[3].X) / 2.0;
143 mp[2].Y = (mp[1].Y + mp[3].Y) / 2.0;
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;
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;
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);
162 return TRUE;
165 GpStatus WINGDIPAPI GdipAddPathArc(GpPath *path, REAL x1, REAL y1, REAL x2,
166 REAL y2, REAL startAngle, REAL sweepAngle)
168 INT count, old_count, i;
170 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
171 path, x1, y1, x2, y2, startAngle, sweepAngle);
173 if(!path)
174 return InvalidParameter;
176 count = arc2polybezier(NULL, x1, y1, x2, y2, startAngle, sweepAngle);
178 if(count == 0)
179 return Ok;
180 if(!lengthen_path(path, count))
181 return OutOfMemory;
183 old_count = path->pathdata.Count;
184 arc2polybezier(&path->pathdata.Points[old_count], x1, y1, x2, y2,
185 startAngle, sweepAngle);
187 for(i = 0; i < count; i++){
188 path->pathdata.Types[old_count + i] = PathPointTypeBezier;
191 path->pathdata.Types[old_count] =
192 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
193 path->newfigure = FALSE;
194 path->pathdata.Count += count;
196 return Ok;
199 GpStatus WINGDIPAPI GdipAddPathArcI(GpPath *path, INT x1, INT y1, INT x2,
200 INT y2, REAL startAngle, REAL sweepAngle)
202 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
203 path, x1, y1, x2, y2, startAngle, sweepAngle);
205 return GdipAddPathArc(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,startAngle,sweepAngle);
208 GpStatus WINGDIPAPI GdipAddPathBezier(GpPath *path, REAL x1, REAL y1, REAL x2,
209 REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
211 INT old_count;
213 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
214 path, x1, y1, x2, y2, x3, y3, x4, y4);
216 if(!path)
217 return InvalidParameter;
219 if(!lengthen_path(path, 4))
220 return OutOfMemory;
222 old_count = path->pathdata.Count;
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;
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;
239 path->newfigure = FALSE;
240 path->pathdata.Count += 4;
242 return Ok;
245 GpStatus WINGDIPAPI GdipAddPathBezierI(GpPath *path, INT x1, INT y1, INT x2,
246 INT y2, INT x3, INT y3, INT x4, INT y4)
248 TRACE("(%p, %d, %d, %d, %d, %d, %d, %d, %d)\n",
249 path, x1, y1, x2, y2, x3, y3, x4, y4);
251 return GdipAddPathBezier(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,(REAL)x3,(REAL)y3,
252 (REAL)x4,(REAL)y4);
255 GpStatus WINGDIPAPI GdipAddPathBeziers(GpPath *path, GDIPCONST GpPointF *points,
256 INT count)
258 INT i, old_count;
260 TRACE("(%p, %p, %d)\n", path, points, count);
262 if(!path || !points || ((count - 1) % 3))
263 return InvalidParameter;
265 if(!lengthen_path(path, count))
266 return OutOfMemory;
268 old_count = path->pathdata.Count;
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;
276 path->pathdata.Types[old_count] =
277 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
278 path->newfigure = FALSE;
279 path->pathdata.Count += count;
281 return Ok;
284 GpStatus WINGDIPAPI GdipAddPathBeziersI(GpPath *path, GDIPCONST GpPoint *points,
285 INT count)
287 GpPointF *ptsF;
288 GpStatus ret;
289 INT i;
291 TRACE("(%p, %p, %d)\n", path, points, count);
293 if(!points || ((count - 1) % 3))
294 return InvalidParameter;
296 ptsF = heap_alloc_zero(sizeof(GpPointF) * count);
297 if(!ptsF)
298 return OutOfMemory;
300 for(i = 0; i < count; i++){
301 ptsF[i].X = (REAL)points[i].X;
302 ptsF[i].Y = (REAL)points[i].Y;
305 ret = GdipAddPathBeziers(path, ptsF, count);
306 heap_free(ptsF);
308 return ret;
311 GpStatus WINGDIPAPI GdipAddPathClosedCurve(GpPath *path, GDIPCONST GpPointF *points,
312 INT count)
314 TRACE("(%p, %p, %d)\n", path, points, count);
316 return GdipAddPathClosedCurve2(path, points, count, 1.0);
319 GpStatus WINGDIPAPI GdipAddPathClosedCurveI(GpPath *path, GDIPCONST GpPoint *points,
320 INT count)
322 TRACE("(%p, %p, %d)\n", path, points, count);
324 return GdipAddPathClosedCurve2I(path, points, count, 1.0);
327 GpStatus WINGDIPAPI GdipAddPathClosedCurve2(GpPath *path, GDIPCONST GpPointF *points,
328 INT count, REAL tension)
330 INT i, len_pt = (count + 1)*3-2;
331 GpPointF *pt;
332 GpPointF *pts;
333 REAL x1, x2, y1, y2;
334 GpStatus stat;
336 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
338 if(!path || !points || count <= 1)
339 return InvalidParameter;
341 pt = heap_alloc_zero(len_pt * sizeof(GpPointF));
342 pts = heap_alloc_zero((count + 1)*sizeof(GpPointF));
343 if(!pt || !pts){
344 heap_free(pt);
345 heap_free(pts);
346 return OutOfMemory;
349 /* copy source points to extend with the last one */
350 memcpy(pts, points, sizeof(GpPointF)*count);
351 pts[count] = pts[0];
353 tension = tension * TENSION_CONST;
355 for(i = 0; i < count-1; i++){
356 calc_curve_bezier(&(pts[i]), tension, &x1, &y1, &x2, &y2);
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;
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];
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;
383 stat = GdipAddPathBeziers(path, pt, len_pt);
385 /* close figure */
386 if(stat == Ok){
387 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
388 path->newfigure = TRUE;
391 heap_free(pts);
392 heap_free(pt);
394 return stat;
397 GpStatus WINGDIPAPI GdipAddPathClosedCurve2I(GpPath *path, GDIPCONST GpPoint *points,
398 INT count, REAL tension)
400 GpPointF *ptf;
401 INT i;
402 GpStatus stat;
404 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
406 if(!path || !points || count <= 1)
407 return InvalidParameter;
409 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
410 if(!ptf)
411 return OutOfMemory;
413 for(i = 0; i < count; i++){
414 ptf[i].X = (REAL)points[i].X;
415 ptf[i].Y = (REAL)points[i].Y;
418 stat = GdipAddPathClosedCurve2(path, ptf, count, tension);
420 heap_free(ptf);
422 return stat;
425 GpStatus WINGDIPAPI GdipAddPathCurve(GpPath *path, GDIPCONST GpPointF *points, INT count)
427 TRACE("(%p, %p, %d)\n", path, points, count);
429 if(!path || !points || count <= 1)
430 return InvalidParameter;
432 return GdipAddPathCurve2(path, points, count, 1.0);
435 GpStatus WINGDIPAPI GdipAddPathCurveI(GpPath *path, GDIPCONST GpPoint *points, INT count)
437 TRACE("(%p, %p, %d)\n", path, points, count);
439 if(!path || !points || count <= 1)
440 return InvalidParameter;
442 return GdipAddPathCurve2I(path, points, count, 1.0);
445 GpStatus WINGDIPAPI GdipAddPathCurve2(GpPath *path, GDIPCONST GpPointF *points, INT count,
446 REAL tension)
448 INT i, len_pt = count*3-2;
449 GpPointF *pt;
450 REAL x1, x2, y1, y2;
451 GpStatus stat;
453 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
455 if(!path || !points || count <= 1)
456 return InvalidParameter;
458 pt = heap_alloc_zero(len_pt * sizeof(GpPointF));
459 if(!pt)
460 return OutOfMemory;
462 tension = tension * TENSION_CONST;
464 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
465 tension, &x1, &y1);
467 pt[0].X = points[0].X;
468 pt[0].Y = points[0].Y;
469 pt[1].X = x1;
470 pt[1].Y = y1;
472 for(i = 0; i < count-2; i++){
473 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
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;
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);
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;
491 stat = GdipAddPathBeziers(path, pt, len_pt);
493 heap_free(pt);
495 return stat;
498 GpStatus WINGDIPAPI GdipAddPathCurve2I(GpPath *path, GDIPCONST GpPoint *points,
499 INT count, REAL tension)
501 GpPointF *ptf;
502 INT i;
503 GpStatus stat;
505 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
507 if(!path || !points || count <= 1)
508 return InvalidParameter;
510 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
511 if(!ptf)
512 return OutOfMemory;
514 for(i = 0; i < count; i++){
515 ptf[i].X = (REAL)points[i].X;
516 ptf[i].Y = (REAL)points[i].Y;
519 stat = GdipAddPathCurve2(path, ptf, count, tension);
521 heap_free(ptf);
523 return stat;
526 GpStatus WINGDIPAPI GdipAddPathCurve3(GpPath *path, GDIPCONST GpPointF *points,
527 INT count, INT offset, INT nseg, REAL tension)
529 TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
531 if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
532 return InvalidParameter;
534 return GdipAddPathCurve2(path, &points[offset], nseg + 1, tension);
537 GpStatus WINGDIPAPI GdipAddPathCurve3I(GpPath *path, GDIPCONST GpPoint *points,
538 INT count, INT offset, INT nseg, REAL tension)
540 TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
542 if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
543 return InvalidParameter;
545 return GdipAddPathCurve2I(path, &points[offset], nseg + 1, tension);
548 GpStatus WINGDIPAPI GdipAddPathEllipse(GpPath *path, REAL x, REAL y, REAL width,
549 REAL height)
551 INT old_count, numpts;
553 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
555 if(!path)
556 return InvalidParameter;
558 if(!lengthen_path(path, MAX_ARC_PTS))
559 return OutOfMemory;
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;
568 memset(&path->pathdata.Types[old_count + 1], PathPointTypeBezier,
569 MAX_ARC_PTS - 1);
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;
577 return Ok;
580 GpStatus WINGDIPAPI GdipAddPathEllipseI(GpPath *path, INT x, INT y, INT width,
581 INT height)
583 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
585 return GdipAddPathEllipse(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
588 GpStatus WINGDIPAPI GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
589 INT count)
591 INT i, old_count;
593 TRACE("(%p, %p, %d)\n", path, points, count);
595 if(!path || !points)
596 return InvalidParameter;
598 if(!lengthen_path(path, count))
599 return OutOfMemory;
601 old_count = path->pathdata.Count;
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;
609 if(path->newfigure){
610 path->pathdata.Types[old_count] = PathPointTypeStart;
611 path->newfigure = FALSE;
614 path->pathdata.Count += count;
616 return Ok;
619 GpStatus WINGDIPAPI GdipAddPathLine2I(GpPath *path, GDIPCONST GpPoint *points, INT count)
621 GpPointF *pointsF;
622 INT i;
623 GpStatus stat;
625 TRACE("(%p, %p, %d)\n", path, points, count);
627 if(count <= 0)
628 return InvalidParameter;
630 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
631 if(!pointsF) return OutOfMemory;
633 for(i = 0;i < count; i++){
634 pointsF[i].X = (REAL)points[i].X;
635 pointsF[i].Y = (REAL)points[i].Y;
638 stat = GdipAddPathLine2(path, pointsF, count);
640 heap_free(pointsF);
642 return stat;
645 GpStatus WINGDIPAPI GdipAddPathLine(GpPath *path, REAL x1, REAL y1, REAL x2, REAL y2)
647 INT old_count;
649 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x1, y1, x2, y2);
651 if(!path)
652 return InvalidParameter;
654 if(!lengthen_path(path, 2))
655 return OutOfMemory;
657 old_count = path->pathdata.Count;
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;
664 path->pathdata.Types[old_count] =
665 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
666 path->pathdata.Types[old_count + 1] = PathPointTypeLine;
668 path->newfigure = FALSE;
669 path->pathdata.Count += 2;
671 return Ok;
674 GpStatus WINGDIPAPI GdipAddPathLineI(GpPath *path, INT x1, INT y1, INT x2, INT y2)
676 TRACE("(%p, %d, %d, %d, %d)\n", path, x1, y1, x2, y2);
678 return GdipAddPathLine(path, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
681 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
682 BOOL connect)
684 INT old_count, count;
686 TRACE("(%p, %p, %d)\n", path, addingPath, connect);
688 if(!path || !addingPath)
689 return InvalidParameter;
691 old_count = path->pathdata.Count;
692 count = addingPath->pathdata.Count;
694 if(!lengthen_path(path, count))
695 return OutOfMemory;
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);
701 if(path->newfigure || !connect)
702 path->pathdata.Types[old_count] = PathPointTypeStart;
703 else
704 path->pathdata.Types[old_count] = PathPointTypeLine;
706 path->newfigure = FALSE;
707 path->pathdata.Count += count;
709 return Ok;
712 GpStatus WINGDIPAPI GdipAddPathPie(GpPath *path, REAL x, REAL y, REAL width, REAL height,
713 REAL startAngle, REAL sweepAngle)
715 GpPointF *ptf;
716 GpStatus status;
717 INT i, count;
719 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
720 path, x, y, width, height, startAngle, sweepAngle);
722 if(!path)
723 return InvalidParameter;
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;
736 count = arc2polybezier(NULL, x, y, width, height, startAngle, sweepAngle);
738 if(count == 0)
739 return Ok;
741 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
742 if(!ptf)
743 return OutOfMemory;
745 arc2polybezier(ptf, x, y, width, height, startAngle, sweepAngle);
747 status = GdipAddPathLine(path, x + width/2, y + height/2, ptf[0].X, ptf[0].Y);
748 if(status != Ok){
749 heap_free(ptf);
750 return status;
752 /* one spline is already added as a line endpoint */
753 if(!lengthen_path(path, count - 1)){
754 heap_free(ptf);
755 return OutOfMemory;
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;
762 path->pathdata.Count += count-1;
764 GdipClosePathFigure(path);
766 heap_free(ptf);
768 return status;
771 GpStatus WINGDIPAPI GdipAddPathPieI(GpPath *path, INT x, INT y, INT width, INT height,
772 REAL startAngle, REAL sweepAngle)
774 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
775 path, x, y, width, height, startAngle, sweepAngle);
777 return GdipAddPathPie(path, (REAL)x, (REAL)y, (REAL)width, (REAL)height, startAngle, sweepAngle);
780 GpStatus WINGDIPAPI GdipAddPathPolygon(GpPath *path, GDIPCONST GpPointF *points, INT count)
782 INT old_count;
784 TRACE("(%p, %p, %d)\n", path, points, count);
786 if(!path || !points || count < 3)
787 return InvalidParameter;
789 if(!lengthen_path(path, count))
790 return OutOfMemory;
792 old_count = path->pathdata.Count;
794 memcpy(&path->pathdata.Points[old_count], points, count*sizeof(GpPointF));
795 memset(&path->pathdata.Types[old_count + 1], PathPointTypeLine, count - 1);
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;
803 return Ok;
806 GpStatus WINGDIPAPI GdipAddPathPolygonI(GpPath *path, GDIPCONST GpPoint *points, INT count)
808 GpPointF *ptf;
809 GpStatus status;
810 INT i;
812 TRACE("(%p, %p, %d)\n", path, points, count);
814 if(!points || count < 3)
815 return InvalidParameter;
817 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
818 if(!ptf)
819 return OutOfMemory;
821 for(i = 0; i < count; i++){
822 ptf[i].X = (REAL)points[i].X;
823 ptf[i].Y = (REAL)points[i].Y;
826 status = GdipAddPathPolygon(path, ptf, count);
828 heap_free(ptf);
830 return status;
833 static float fromfixedpoint(const FIXED v)
835 float f = ((float)v.fract) / (1<<(sizeof(v.fract)*8));
836 f += v.value;
837 return f;
840 struct format_string_args
842 GpPath *path;
843 float maxY;
844 float scale;
845 float ascent;
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)
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;
862 if (underlined_index_count)
863 FIXME("hotkey underlines not drawn yet\n");
865 if (y + bounds->Height * args->scale > args->maxY)
866 args->maxY = y + bounds->Height * args->scale;
868 for (i = index; i < length; ++i)
870 GLYPHMETRICS gm;
871 TTPOLYGONHEADER *ph = NULL, *origph;
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)
877 status = GenericError;
878 break;
880 origph = ph = heap_alloc_zero(len);
881 start = (char *)ph;
882 if (!ph || !lengthen_path(path, len / sizeof(POINTFX)))
884 heap_free(ph);
885 status = OutOfMemory;
886 break;
888 GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, len, start, &identity);
890 ofs = 0;
891 while (ofs < len)
893 DWORD ofs_start = ofs;
894 ph = (TTPOLYGONHEADER*)&start[ofs];
895 path->pathdata.Types[path->pathdata.Count] = PathPointTypeStart;
896 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(ph->pfxStart.x) * args->scale;
897 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(ph->pfxStart.y) * args->scale;
898 TRACE("Starting at count %i with pos %f, %f)\n", path->pathdata.Count, x, y);
899 ofs += sizeof(*ph);
900 while (ofs - ofs_start < ph->cb)
902 TTPOLYCURVE *curve = (TTPOLYCURVE*)&start[ofs];
903 int j;
904 ofs += sizeof(TTPOLYCURVE) + (curve->cpfx - 1) * sizeof(POINTFX);
906 switch (curve->wType)
908 case TT_PRIM_LINE:
909 for (j = 0; j < curve->cpfx; ++j)
911 path->pathdata.Types[path->pathdata.Count] = PathPointTypeLine;
912 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
913 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
915 break;
916 case TT_PRIM_CSPLINE:
917 for (j = 0; j < curve->cpfx; ++j)
919 path->pathdata.Types[path->pathdata.Count] = PathPointTypeBezier;
920 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
921 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
923 break;
924 default:
925 ERR("Unhandled type: %u\n", curve->wType);
926 status = GenericError;
929 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
931 path->newfigure = TRUE;
932 x += gm.gmCellIncX * args->scale;
933 y += gm.gmCellIncY * args->scale;
935 heap_free(origph);
936 if (status != Ok)
937 break;
940 return status;
943 GpStatus WINGDIPAPI GdipAddPathString(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat* format)
945 GpFont *font;
946 GpStatus status;
947 LOGFONTW lfw;
948 HANDLE hfont;
949 HDC dc;
950 GpGraphics *graphics;
951 GpPath *backup;
952 struct format_string_args args;
953 int i;
954 UINT16 native_height;
955 RectF scaled_layout_rect;
956 TEXTMETRICW textmetric;
958 TRACE("(%p, %s, %d, %p, %d, %f, %p, %p)\n", path, debugstr_w(string), length, family, style, emSize, layoutRect, format);
959 if (!path || !string || !family || !emSize || !layoutRect || !format)
960 return InvalidParameter;
962 status = GdipGetEmHeight(family, style, &native_height);
963 if (status != Ok)
964 return status;
966 scaled_layout_rect.X = layoutRect->X;
967 scaled_layout_rect.Y = layoutRect->Y;
968 scaled_layout_rect.Width = layoutRect->Width * native_height / emSize;
969 scaled_layout_rect.Height = layoutRect->Height * native_height / emSize;
971 if ((status = GdipClonePath(path, &backup)) != Ok)
972 return status;
974 dc = CreateCompatibleDC(0);
975 status = GdipCreateFromHDC(dc, &graphics);
976 if (status != Ok)
978 DeleteDC(dc);
979 GdipDeletePath(backup);
980 return status;
983 status = GdipCreateFont(family, native_height, style, UnitPixel, &font);
984 if (status != Ok)
986 GdipDeleteGraphics(graphics);
987 DeleteDC(dc);
988 GdipDeletePath(backup);
989 return status;
992 get_log_fontW(font, graphics, &lfw);
993 GdipDeleteFont(font);
994 GdipDeleteGraphics(graphics);
996 hfont = CreateFontIndirectW(&lfw);
997 if (!hfont)
999 WARN("Failed to create font\n");
1000 DeleteDC(dc);
1001 GdipDeletePath(backup);
1002 return GenericError;
1005 SelectObject(dc, hfont);
1007 GetTextMetricsW(dc, &textmetric);
1009 args.path = path;
1010 args.maxY = 0;
1011 args.scale = emSize / native_height;
1012 args.ascent = textmetric.tmAscent * args.scale;
1013 status = gdip_format_string(dc, string, length, NULL, &scaled_layout_rect,
1014 format, TRUE, format_string_callback, &args);
1016 DeleteDC(dc);
1017 DeleteObject(hfont);
1019 if (status != Ok) /* free backup */
1021 heap_free(path->pathdata.Points);
1022 heap_free(path->pathdata.Types);
1023 *path = *backup;
1024 heap_free(backup);
1025 return status;
1027 if (format && format->vertalign == StringAlignmentCenter && layoutRect->Y + args.maxY < layoutRect->Height)
1029 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1030 inc /= 2;
1031 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1032 path->pathdata.Points[i].Y += inc;
1033 } else if (format && format->vertalign == StringAlignmentFar) {
1034 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1035 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1036 path->pathdata.Points[i].Y += inc;
1038 GdipDeletePath(backup);
1039 return status;
1042 GpStatus WINGDIPAPI GdipAddPathStringI(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1044 if (layoutRect)
1046 RectF layoutRectF = {
1047 (REAL)layoutRect->X,
1048 (REAL)layoutRect->Y,
1049 (REAL)layoutRect->Width,
1050 (REAL)layoutRect->Height
1052 return GdipAddPathString(path, string, length, family, style, emSize, &layoutRectF, format);
1054 return InvalidParameter;
1057 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1059 TRACE("(%p, %p)\n", path, clone);
1061 if(!path || !clone)
1062 return InvalidParameter;
1064 *clone = heap_alloc_zero(sizeof(GpPath));
1065 if(!*clone) return OutOfMemory;
1067 **clone = *path;
1069 (*clone)->pathdata.Points = heap_alloc_zero(path->datalen * sizeof(PointF));
1070 (*clone)->pathdata.Types = heap_alloc_zero(path->datalen);
1071 if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1072 heap_free((*clone)->pathdata.Points);
1073 heap_free((*clone)->pathdata.Types);
1074 heap_free(*clone);
1075 return OutOfMemory;
1078 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1079 path->datalen * sizeof(PointF));
1080 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1082 return Ok;
1085 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1087 TRACE("(%p)\n", path);
1089 if(!path)
1090 return InvalidParameter;
1092 if(path->pathdata.Count > 0){
1093 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1094 path->newfigure = TRUE;
1097 return Ok;
1100 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1102 INT i;
1104 TRACE("(%p)\n", path);
1106 if(!path)
1107 return InvalidParameter;
1109 for(i = 1; i < path->pathdata.Count; i++){
1110 if(path->pathdata.Types[i] == PathPointTypeStart)
1111 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1114 path->newfigure = TRUE;
1116 return Ok;
1119 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1121 TRACE("(%d, %p)\n", fill, path);
1123 if(!path)
1124 return InvalidParameter;
1126 *path = heap_alloc_zero(sizeof(GpPath));
1127 if(!*path) return OutOfMemory;
1129 (*path)->fill = fill;
1130 (*path)->newfigure = TRUE;
1132 return Ok;
1135 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1136 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1138 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1140 if(!path)
1141 return InvalidParameter;
1143 *path = heap_alloc_zero(sizeof(GpPath));
1144 if(!*path) return OutOfMemory;
1146 (*path)->pathdata.Points = heap_alloc_zero(count * sizeof(PointF));
1147 (*path)->pathdata.Types = heap_alloc_zero(count);
1149 if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
1150 heap_free((*path)->pathdata.Points);
1151 heap_free((*path)->pathdata.Types);
1152 heap_free(*path);
1153 return OutOfMemory;
1156 memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
1157 memcpy((*path)->pathdata.Types, types, count);
1158 (*path)->pathdata.Count = count;
1159 (*path)->datalen = count;
1161 (*path)->fill = fill;
1162 (*path)->newfigure = TRUE;
1164 return Ok;
1167 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1168 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1170 GpPointF *ptF;
1171 GpStatus ret;
1172 INT i;
1174 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1176 ptF = heap_alloc_zero(sizeof(GpPointF)*count);
1178 for(i = 0;i < count; i++){
1179 ptF[i].X = (REAL)points[i].X;
1180 ptF[i].Y = (REAL)points[i].Y;
1183 ret = GdipCreatePath2(ptF, types, count, fill, path);
1185 heap_free(ptF);
1187 return ret;
1190 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1192 TRACE("(%p)\n", path);
1194 if(!path)
1195 return InvalidParameter;
1197 heap_free(path->pathdata.Points);
1198 heap_free(path->pathdata.Types);
1199 heap_free(path);
1201 return Ok;
1204 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1206 path_list_node_t *list, *node;
1207 GpPointF pt;
1208 INT i = 1;
1209 INT startidx = 0;
1210 GpStatus stat;
1212 TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1214 if(!path)
1215 return InvalidParameter;
1217 if(path->pathdata.Count == 0)
1218 return Ok;
1220 stat = GdipTransformPath(path, matrix);
1221 if(stat != Ok)
1222 return stat;
1224 pt = path->pathdata.Points[0];
1225 if(!init_path_list(&list, pt.X, pt.Y))
1226 return OutOfMemory;
1228 node = list;
1230 while(i < path->pathdata.Count){
1232 BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1233 path_list_node_t *start;
1235 pt = path->pathdata.Points[i];
1237 /* save last start point index */
1238 if(type == PathPointTypeStart)
1239 startidx = i;
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;
1246 node = node->next;
1247 ++i;
1248 continue;
1251 /* Bezier curve */
1253 /* test for closed figure */
1254 if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1255 pt = path->pathdata.Points[startidx];
1256 ++i;
1258 else
1260 i += 2;
1261 pt = path->pathdata.Points[i];
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;
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;
1277 ++i;
1278 }/* while */
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;
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;
1293 free_path_list(list);
1294 return Ok;
1296 memout:
1297 free_path_list(list);
1298 return OutOfMemory;
1301 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1303 TRACE("(%p, %p)\n", path, pathData);
1305 if(!path || !pathData)
1306 return InvalidParameter;
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);
1313 return Ok;
1316 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1318 TRACE("(%p, %p)\n", path, fillmode);
1320 if(!path || !fillmode)
1321 return InvalidParameter;
1323 *fillmode = path->fill;
1325 return Ok;
1328 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1330 INT count;
1332 TRACE("(%p, %p)\n", path, lastPoint);
1334 if(!path || !lastPoint)
1335 return InvalidParameter;
1337 count = path->pathdata.Count;
1338 if(count > 0)
1339 *lastPoint = path->pathdata.Points[count-1];
1341 return Ok;
1344 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1346 TRACE("(%p, %p, %d)\n", path, points, count);
1348 if(!path)
1349 return InvalidParameter;
1351 if(count < path->pathdata.Count)
1352 return InsufficientBuffer;
1354 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1356 return Ok;
1359 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1361 GpStatus ret;
1362 GpPointF *ptf;
1363 INT i;
1365 TRACE("(%p, %p, %d)\n", path, points, count);
1367 if(count <= 0)
1368 return InvalidParameter;
1370 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
1371 if(!ptf) return OutOfMemory;
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);
1379 heap_free(ptf);
1381 return ret;
1384 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1386 TRACE("(%p, %p, %d)\n", path, types, count);
1388 if(!path)
1389 return InvalidParameter;
1391 if(count < path->pathdata.Count)
1392 return InsufficientBuffer;
1394 memcpy(types, path->pathdata.Types, path->pathdata.Count);
1396 return Ok;
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)
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;
1410 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1412 /* Matrix and pen can be null. */
1413 if(!path || !bounds)
1414 return InvalidParameter;
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;
1423 points = path->pathdata.Points;
1425 low_x = high_x = points[0].X;
1426 low_y = high_y = points[0].Y;
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);
1435 width = high_x - low_x;
1436 height = high_y - low_y;
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;
1449 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1450 low_x = temp_pts[0].X;
1451 low_y = temp_pts[0].Y;
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);
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]);
1463 if(pen){
1464 path_width = pen->width / 2.0;
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);
1472 low_x -= path_width;
1473 low_y -= path_width;
1474 width += 2.0 * path_width;
1475 height += 2.0 * path_width;
1478 bounds->X = low_x;
1479 bounds->Y = low_y;
1480 bounds->Width = width;
1481 bounds->Height = height;
1483 return Ok;
1486 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1487 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1489 GpStatus ret;
1490 GpRectF boundsF;
1492 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1494 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
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);
1503 return ret;
1506 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1508 TRACE("(%p, %p)\n", path, count);
1510 if(!path)
1511 return InvalidParameter;
1513 *count = path->pathdata.Count;
1515 return Ok;
1518 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1520 INT i, count;
1521 INT start = 0; /* position in reversed path */
1522 GpPathData revpath;
1524 TRACE("(%p)\n", path);
1526 if(!path)
1527 return InvalidParameter;
1529 count = path->pathdata.Count;
1531 if(count == 0) return Ok;
1533 revpath.Points = heap_alloc_zero(sizeof(GpPointF)*count);
1534 revpath.Types = heap_alloc_zero(sizeof(BYTE)*count);
1535 revpath.Count = count;
1536 if(!revpath.Points || !revpath.Types){
1537 heap_free(revpath.Points);
1538 heap_free(revpath.Types);
1539 return OutOfMemory;
1542 for(i = 0; i < count; i++){
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];
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];
1558 else
1559 revpath.Types[i] = path->pathdata.Types[start];
1561 start = i+1;
1565 memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1566 memcpy(path->pathdata.Types, revpath.Types, sizeof(BYTE)*count);
1568 heap_free(revpath.Points);
1569 heap_free(revpath.Types);
1571 return Ok;
1574 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1575 GpPen *pen, GpGraphics *graphics, BOOL *result)
1577 TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1579 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1582 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1583 GpPen *pen, GpGraphics *graphics, BOOL *result)
1585 GpStatus stat;
1586 GpPath *wide_path;
1587 GpMatrix *transform = NULL;
1589 TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1591 if(!path || !pen)
1592 return InvalidParameter;
1594 stat = GdipClonePath(path, &wide_path);
1596 if (stat != Ok)
1597 return stat;
1599 if (pen->unit == UnitPixel && graphics != NULL)
1601 stat = GdipCreateMatrix(&transform);
1603 if (stat == Ok)
1604 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1605 CoordinateSpaceWorld, transform);
1608 if (stat == Ok)
1609 stat = GdipWidenPath(wide_path, pen, transform, 1.0);
1611 if (pen->unit == UnitPixel && graphics != NULL)
1613 if (stat == Ok)
1614 stat = GdipInvertMatrix(transform);
1616 if (stat == Ok)
1617 stat = GdipTransformPath(wide_path, transform);
1620 if (stat == Ok)
1621 stat = GdipIsVisiblePathPoint(wide_path, x, y, graphics, result);
1623 GdipDeleteMatrix(transform);
1625 GdipDeletePath(wide_path);
1627 return stat;
1630 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1632 TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1634 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1637 /*****************************************************************************
1638 * GdipIsVisiblePathPoint [GDIPLUS.@]
1640 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1642 GpRegion *region;
1643 HRGN hrgn;
1644 GpStatus status;
1646 if(!path || !result) return InvalidParameter;
1648 status = GdipCreateRegionPath(path, &region);
1649 if(status != Ok)
1650 return status;
1652 status = GdipGetRegionHRgn(region, graphics, &hrgn);
1653 if(status != Ok){
1654 GdipDeleteRegion(region);
1655 return status;
1658 *result = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1660 DeleteObject(hrgn);
1661 GdipDeleteRegion(region);
1663 return Ok;
1666 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1668 TRACE("(%p)\n", path);
1670 if(!path)
1671 return InvalidParameter;
1673 path->newfigure = TRUE;
1675 return Ok;
1678 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1680 TRACE("(%p)\n", path);
1682 if(!path)
1683 return InvalidParameter;
1685 path->pathdata.Count = 0;
1686 path->newfigure = TRUE;
1687 path->fill = FillModeAlternate;
1689 return Ok;
1692 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1694 TRACE("(%p, %d)\n", path, fill);
1696 if(!path)
1697 return InvalidParameter;
1699 path->fill = fill;
1701 return Ok;
1704 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1706 TRACE("(%p, %p)\n", path, matrix);
1708 if(!path)
1709 return InvalidParameter;
1711 if(path->pathdata.Count == 0 || !matrix)
1712 return Ok;
1714 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1715 path->pathdata.Count);
1718 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1719 GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1720 REAL height, WarpMode warpmode, REAL flatness)
1722 FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1723 points, count, x, y, width, height, warpmode, flatness);
1725 return NotImplemented;
1728 static void add_bevel_point(const GpPointF *endpoint, const GpPointF *nextpoint,
1729 GpPen *pen, int right_side, path_list_node_t **last_point)
1731 REAL segment_dy = nextpoint->Y-endpoint->Y;
1732 REAL segment_dx = nextpoint->X-endpoint->X;
1733 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1734 REAL distance = pen->width/2.0;
1735 REAL bevel_dx, bevel_dy;
1737 if (segment_length == 0.0)
1739 *last_point = add_path_list_node(*last_point, endpoint->X,
1740 endpoint->Y, PathPointTypeLine);
1741 return;
1744 if (right_side)
1746 bevel_dx = -distance * segment_dy / segment_length;
1747 bevel_dy = distance * segment_dx / segment_length;
1749 else
1751 bevel_dx = distance * segment_dy / segment_length;
1752 bevel_dy = -distance * segment_dx / segment_length;
1755 *last_point = add_path_list_node(*last_point, endpoint->X + bevel_dx,
1756 endpoint->Y + bevel_dy, PathPointTypeLine);
1759 static void widen_joint(const GpPointF *p1, const GpPointF *p2, const GpPointF *p3,
1760 GpPen* pen, path_list_node_t **last_point)
1762 switch (pen->join)
1764 case LineJoinMiter:
1765 case LineJoinMiterClipped:
1766 if ((p2->X - p1->X) * (p3->Y - p1->Y) > (p2->Y - p1->Y) * (p3->X - p1->X))
1768 float distance = pen->width/2.0;
1769 float length_0 = sqrtf((p2->X-p1->X)*(p2->X-p1->X)+(p2->Y-p1->Y)*(p2->Y-p1->Y));
1770 float length_1 = sqrtf((p3->X-p2->X)*(p3->X-p2->X)+(p3->Y-p2->Y)*(p3->Y-p2->Y));
1771 float dx0 = distance * (p2->X - p1->X) / length_0;
1772 float dy0 = distance * (p2->Y - p1->Y) / length_0;
1773 float dx1 = distance * (p3->X - p2->X) / length_1;
1774 float dy1 = distance * (p3->Y - p2->Y) / length_1;
1775 float det = (dy0*dx1 - dx0*dy1);
1776 float dx = (dx0*dx1*(dx0-dx1) + dy0*dy0*dx1 - dy1*dy1*dx0)/det;
1777 float dy = (dy0*dy1*(dy0-dy1) + dx0*dx0*dy1 - dx1*dx1*dy0)/det;
1778 if (dx*dx + dy*dy < pen->miterlimit*pen->miterlimit * distance*distance)
1780 *last_point = add_path_list_node(*last_point, p2->X + dx,
1781 p2->Y + dy, PathPointTypeLine);
1782 break;
1784 else if (pen->join == LineJoinMiter)
1786 static int once;
1787 if (!once++)
1788 FIXME("should add a clipped corner\n");
1790 /* else fall-through */
1792 /* else fall-through */
1793 default:
1794 case LineJoinBevel:
1795 add_bevel_point(p2, p1, pen, 1, last_point);
1796 add_bevel_point(p2, p3, pen, 0, last_point);
1797 break;
1801 static void widen_cap(const GpPointF *endpoint, const GpPointF *nextpoint,
1802 GpPen *pen, GpLineCap cap, GpCustomLineCap *custom, int add_first_points,
1803 int add_last_point, path_list_node_t **last_point)
1805 switch (cap)
1807 default:
1808 case LineCapFlat:
1809 if (add_first_points)
1810 add_bevel_point(endpoint, nextpoint, pen, 1, last_point);
1811 if (add_last_point)
1812 add_bevel_point(endpoint, nextpoint, pen, 0, last_point);
1813 break;
1814 case LineCapSquare:
1816 REAL segment_dy = nextpoint->Y-endpoint->Y;
1817 REAL segment_dx = nextpoint->X-endpoint->X;
1818 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1819 REAL distance = pen->width/2.0;
1820 REAL bevel_dx, bevel_dy;
1821 REAL extend_dx, extend_dy;
1823 extend_dx = -distance * segment_dx / segment_length;
1824 extend_dy = -distance * segment_dy / segment_length;
1826 bevel_dx = -distance * segment_dy / segment_length;
1827 bevel_dy = distance * segment_dx / segment_length;
1829 if (add_first_points)
1830 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx + bevel_dx,
1831 endpoint->Y + extend_dy + bevel_dy, PathPointTypeLine);
1833 if (add_last_point)
1834 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx - bevel_dx,
1835 endpoint->Y + extend_dy - bevel_dy, PathPointTypeLine);
1837 break;
1839 case LineCapRound:
1841 REAL segment_dy = nextpoint->Y-endpoint->Y;
1842 REAL segment_dx = nextpoint->X-endpoint->X;
1843 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1844 REAL distance = pen->width/2.0;
1845 REAL dx, dy, dx2, dy2;
1846 const REAL control_point_distance = 0.5522847498307935; /* 4/3 * (sqrt(2) - 1) */
1848 if (add_first_points)
1850 dx = -distance * segment_dx / segment_length;
1851 dy = -distance * segment_dy / segment_length;
1853 dx2 = dx * control_point_distance;
1854 dy2 = dy * control_point_distance;
1856 /* first 90-degree arc */
1857 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
1858 endpoint->Y - dx, PathPointTypeLine);
1860 *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
1861 endpoint->Y - dx + dy2, PathPointTypeBezier);
1863 *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
1864 endpoint->Y + dy - dx2, PathPointTypeBezier);
1866 /* midpoint */
1867 *last_point = add_path_list_node(*last_point, endpoint->X + dx,
1868 endpoint->Y + dy, PathPointTypeBezier);
1870 /* second 90-degree arc */
1871 *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
1872 endpoint->Y + dy + dx2, PathPointTypeBezier);
1874 *last_point = add_path_list_node(*last_point, endpoint->X - dy + dx2,
1875 endpoint->Y + dx + dy2, PathPointTypeBezier);
1877 *last_point = add_path_list_node(*last_point, endpoint->X - dy,
1878 endpoint->Y + dx, PathPointTypeBezier);
1880 break;
1885 static void widen_open_figure(const GpPointF *points, GpPen *pen, int start, int end,
1886 GpLineCap start_cap, GpCustomLineCap *start_custom, GpLineCap end_cap,
1887 GpCustomLineCap *end_custom, path_list_node_t **last_point)
1889 int i;
1890 path_list_node_t *prev_point;
1892 if (end <= start)
1893 return;
1895 prev_point = *last_point;
1897 widen_cap(&points[start], &points[start+1],
1898 pen, start_cap, start_custom, FALSE, TRUE, last_point);
1900 for (i=start+1; i<end; i++)
1901 widen_joint(&points[i-1], &points[i],
1902 &points[i+1], pen, last_point);
1904 widen_cap(&points[end], &points[end-1],
1905 pen, end_cap, end_custom, TRUE, TRUE, last_point);
1907 for (i=end-1; i>start; i--)
1908 widen_joint(&points[i+1], &points[i],
1909 &points[i-1], pen, last_point);
1911 widen_cap(&points[start], &points[start+1],
1912 pen, start_cap, start_custom, TRUE, FALSE, last_point);
1914 prev_point->next->type = PathPointTypeStart;
1915 (*last_point)->type |= PathPointTypeCloseSubpath;
1918 static void widen_closed_figure(GpPath *path, GpPen *pen, int start, int end,
1919 path_list_node_t **last_point)
1921 int i;
1922 path_list_node_t *prev_point;
1924 if (end <= start)
1925 return;
1927 /* left outline */
1928 prev_point = *last_point;
1930 widen_joint(&path->pathdata.Points[end], &path->pathdata.Points[start],
1931 &path->pathdata.Points[start+1], pen, last_point);
1933 for (i=start+1; i<end; i++)
1934 widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
1935 &path->pathdata.Points[i+1], pen, last_point);
1937 widen_joint(&path->pathdata.Points[end-1], &path->pathdata.Points[end],
1938 &path->pathdata.Points[start], pen, last_point);
1940 prev_point->next->type = PathPointTypeStart;
1941 (*last_point)->type |= PathPointTypeCloseSubpath;
1943 /* right outline */
1944 prev_point = *last_point;
1946 widen_joint(&path->pathdata.Points[start], &path->pathdata.Points[end],
1947 &path->pathdata.Points[end-1], pen, last_point);
1949 for (i=end-1; i>start; i--)
1950 widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
1951 &path->pathdata.Points[i-1], pen, last_point);
1953 widen_joint(&path->pathdata.Points[start+1], &path->pathdata.Points[start],
1954 &path->pathdata.Points[end], pen, last_point);
1956 prev_point->next->type = PathPointTypeStart;
1957 (*last_point)->type |= PathPointTypeCloseSubpath;
1960 static void widen_dashed_figure(GpPath *path, GpPen *pen, int start, int end,
1961 int closed, path_list_node_t **last_point)
1963 int i, j;
1964 REAL dash_pos=0.0;
1965 int dash_index=0;
1966 const REAL *dash_pattern;
1967 int dash_count;
1968 GpPointF *tmp_points;
1969 REAL segment_dy;
1970 REAL segment_dx;
1971 REAL segment_length;
1972 REAL segment_pos;
1973 int num_tmp_points=0;
1974 int draw_start_cap=0;
1975 static const REAL dash_dot_dot[6] = { 3.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
1977 if (end <= start)
1978 return;
1980 switch (pen->dash)
1982 case DashStyleDash:
1983 default:
1984 dash_pattern = dash_dot_dot;
1985 dash_count = 2;
1986 break;
1987 case DashStyleDot:
1988 dash_pattern = &dash_dot_dot[2];
1989 dash_count = 2;
1990 break;
1991 case DashStyleDashDot:
1992 dash_pattern = dash_dot_dot;
1993 dash_count = 4;
1994 break;
1995 case DashStyleDashDotDot:
1996 dash_pattern = dash_dot_dot;
1997 dash_count = 6;
1998 break;
1999 case DashStyleCustom:
2000 dash_pattern = pen->dashes;
2001 dash_count = pen->numdashes;
2002 break;
2005 tmp_points = heap_alloc_zero((end - start + 2) * sizeof(GpPoint));
2006 if (!tmp_points) return; /* FIXME */
2008 if (!closed)
2009 draw_start_cap = 1;
2011 for (j=start; j <= end; j++)
2013 if (j == start)
2015 if (closed)
2016 i = end;
2017 else
2018 continue;
2020 else
2021 i = j-1;
2023 segment_dy = path->pathdata.Points[j].Y - path->pathdata.Points[i].Y;
2024 segment_dx = path->pathdata.Points[j].X - path->pathdata.Points[i].X;
2025 segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2026 segment_pos = 0.0;
2028 while (1)
2030 if (dash_pos == 0.0)
2032 if ((dash_index % 2) == 0)
2034 /* start dash */
2035 num_tmp_points = 1;
2036 tmp_points[0].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2037 tmp_points[0].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2039 else
2041 /* end dash */
2042 tmp_points[num_tmp_points].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2043 tmp_points[num_tmp_points].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2045 widen_open_figure(tmp_points, pen, 0, num_tmp_points,
2046 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2047 LineCapFlat, NULL, last_point);
2048 draw_start_cap = 0;
2049 num_tmp_points = 0;
2053 if (dash_pattern[dash_index] - dash_pos > segment_length - segment_pos)
2055 /* advance to next segment */
2056 if ((dash_index % 2) == 0)
2058 tmp_points[num_tmp_points] = path->pathdata.Points[j];
2059 num_tmp_points++;
2061 dash_pos += segment_length - segment_pos;
2062 break;
2064 else
2066 /* advance to next dash in pattern */
2067 segment_pos += dash_pattern[dash_index] - dash_pos;
2068 dash_pos = 0.0;
2069 if (++dash_index == dash_count)
2070 dash_index = 0;
2071 continue;
2076 if (dash_index % 2 == 0 && num_tmp_points != 0)
2078 /* last dash overflows last segment */
2079 tmp_points[num_tmp_points] = path->pathdata.Points[end];
2080 widen_open_figure(tmp_points, pen, 0, num_tmp_points,
2081 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2082 closed ? LineCapFlat : pen->endcap, pen->customend, last_point);
2085 heap_free(tmp_points);
2088 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
2089 REAL flatness)
2091 GpPath *flat_path=NULL;
2092 GpStatus status;
2093 path_list_node_t *points=NULL, *last_point=NULL;
2094 int i, subpath_start=0, new_length;
2095 BYTE type;
2097 TRACE("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
2099 if (!path || !pen)
2100 return InvalidParameter;
2102 if (path->pathdata.Count <= 1)
2103 return OutOfMemory;
2105 status = GdipClonePath(path, &flat_path);
2107 if (status == Ok)
2108 status = GdipFlattenPath(flat_path, pen->unit == UnitPixel ? matrix : NULL, flatness);
2110 if (status == Ok && !init_path_list(&points, 314.0, 22.0))
2111 status = OutOfMemory;
2113 if (status == Ok)
2115 last_point = points;
2117 if (pen->endcap > LineCapRound)
2118 FIXME("unimplemented end cap %x\n", pen->endcap);
2120 if (pen->startcap > LineCapRound)
2121 FIXME("unimplemented start cap %x\n", pen->startcap);
2123 if (pen->dashcap != DashCapFlat)
2124 FIXME("unimplemented dash cap %d\n", pen->dashcap);
2126 if (pen->join == LineJoinRound)
2127 FIXME("unimplemented line join %d\n", pen->join);
2129 if (pen->align != PenAlignmentCenter)
2130 FIXME("unimplemented pen alignment %d\n", pen->align);
2132 for (i=0; i < flat_path->pathdata.Count; i++)
2134 type = flat_path->pathdata.Types[i];
2136 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
2137 subpath_start = i;
2139 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
2141 if (pen->dash != DashStyleSolid)
2142 widen_dashed_figure(flat_path, pen, subpath_start, i, 1, &last_point);
2143 else
2144 widen_closed_figure(flat_path, pen, subpath_start, i, &last_point);
2146 else if (i == flat_path->pathdata.Count-1 ||
2147 (flat_path->pathdata.Types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
2149 if (pen->dash != DashStyleSolid)
2150 widen_dashed_figure(flat_path, pen, subpath_start, i, 0, &last_point);
2151 else
2152 widen_open_figure(flat_path->pathdata.Points, pen, subpath_start, i, pen->startcap, pen->customstart, pen->endcap, pen->customend, &last_point);
2156 new_length = path_list_count(points)-1;
2158 if (!lengthen_path(path, new_length))
2159 status = OutOfMemory;
2162 if (status == Ok)
2164 path->pathdata.Count = new_length;
2166 last_point = points->next;
2167 for (i = 0; i < new_length; i++)
2169 path->pathdata.Points[i] = last_point->pt;
2170 path->pathdata.Types[i] = last_point->type;
2171 last_point = last_point->next;
2174 path->fill = FillModeWinding;
2177 free_path_list(points);
2179 GdipDeletePath(flat_path);
2181 if (status == Ok && pen->unit != UnitPixel)
2182 status = GdipTransformPath(path, matrix);
2184 return status;
2187 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
2188 REAL width, REAL height)
2190 GpPath *backup;
2191 GpPointF ptf[2];
2192 GpStatus retstat;
2193 BOOL old_new;
2195 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
2197 if(!path)
2198 return InvalidParameter;
2200 /* make a backup copy of path data */
2201 if((retstat = GdipClonePath(path, &backup)) != Ok)
2202 return retstat;
2204 /* rectangle should start as new path */
2205 old_new = path->newfigure;
2206 path->newfigure = TRUE;
2207 if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
2208 path->newfigure = old_new;
2209 goto fail;
2212 ptf[0].X = x+width;
2213 ptf[0].Y = y+height;
2214 ptf[1].X = x;
2215 ptf[1].Y = y+height;
2217 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
2218 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
2220 /* free backup */
2221 GdipDeletePath(backup);
2222 return Ok;
2224 fail:
2225 /* reverting */
2226 heap_free(path->pathdata.Points);
2227 heap_free(path->pathdata.Types);
2228 memcpy(path, backup, sizeof(*path));
2229 heap_free(backup);
2231 return retstat;
2234 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
2235 INT width, INT height)
2237 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
2239 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2242 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
2244 GpPath *backup;
2245 GpStatus retstat;
2246 INT i;
2248 TRACE("(%p, %p, %d)\n", path, rects, count);
2250 /* count == 0 - verified condition */
2251 if(!path || !rects || count == 0)
2252 return InvalidParameter;
2254 if(count < 0)
2255 return OutOfMemory;
2257 /* make a backup copy */
2258 if((retstat = GdipClonePath(path, &backup)) != Ok)
2259 return retstat;
2261 for(i = 0; i < count; i++){
2262 if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
2263 goto fail;
2266 /* free backup */
2267 GdipDeletePath(backup);
2268 return Ok;
2270 fail:
2271 /* reverting */
2272 heap_free(path->pathdata.Points);
2273 heap_free(path->pathdata.Types);
2274 memcpy(path, backup, sizeof(*path));
2275 heap_free(backup);
2277 return retstat;
2280 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
2282 GpRectF *rectsF;
2283 GpStatus retstat;
2284 INT i;
2286 TRACE("(%p, %p, %d)\n", path, rects, count);
2288 if(!rects || count == 0)
2289 return InvalidParameter;
2291 if(count < 0)
2292 return OutOfMemory;
2294 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
2296 for(i = 0;i < count;i++){
2297 rectsF[i].X = (REAL)rects[i].X;
2298 rectsF[i].Y = (REAL)rects[i].Y;
2299 rectsF[i].Width = (REAL)rects[i].Width;
2300 rectsF[i].Height = (REAL)rects[i].Height;
2303 retstat = GdipAddPathRectangles(path, rectsF, count);
2304 heap_free(rectsF);
2306 return retstat;
2309 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
2311 INT count;
2313 TRACE("(%p)\n", path);
2315 if(!path)
2316 return InvalidParameter;
2318 count = path->pathdata.Count;
2320 /* set marker flag */
2321 if(count > 0)
2322 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
2324 return Ok;
2327 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
2329 INT count;
2330 INT i;
2332 TRACE("(%p)\n", path);
2334 if(!path)
2335 return InvalidParameter;
2337 count = path->pathdata.Count;
2339 for(i = 0; i < count - 1; i++){
2340 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
2343 return Ok;
2346 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
2348 FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
2349 return NotImplemented;