gdiplus: Add basic implementation of GdipWidenPath.
[wine/multimedia.git] / dlls / gdiplus / graphicspath.c
blob8096507661d62309d99ea9934b758544bc45906b
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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 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);
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 = GdipAlloc(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 GdipFree(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 = 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;
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 GdipFree(pts);
392 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 = GdipAlloc(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 GdipFree(ptf);
750 return status;
752 /* one spline is already added as a line endpoint */
753 if(!lengthen_path(path, count - 1)){
754 GdipFree(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 GdipFree(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 = GdipAlloc(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 GdipFree(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 UINT maxY;
846 static GpStatus format_string_callback(HDC dc,
847 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
848 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
849 INT lineno, const RectF *bounds, INT *underlined_indexes,
850 INT underlined_index_count, void *priv)
852 static const MAT2 identity = { {0,1}, {0,0}, {0,0}, {0,1} };
853 struct format_string_args *args = priv;
854 GpPath *path = args->path;
855 GpStatus status = Ok;
856 float x = bounds->X;
857 float y = bounds->Y;
858 int i;
860 if (underlined_index_count)
861 FIXME("hotkey underlines not drawn yet\n");
863 for (i = index; i < length; ++i)
865 GLYPHMETRICS gm;
866 TTPOLYGONHEADER *ph = NULL;
867 char *start;
868 DWORD len, ofs = 0;
869 UINT bb_end;
870 len = GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, 0, NULL, &identity);
871 if (len == GDI_ERROR)
873 status = GenericError;
874 break;
876 ph = GdipAlloc(len);
877 start = (char *)ph;
878 if (!ph || !lengthen_path(path, len / sizeof(POINTFX)))
880 status = OutOfMemory;
881 break;
883 GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, len, start, &identity);
884 bb_end = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y;
885 if (bb_end + y > args->maxY)
886 args->maxY = bb_end + y;
888 ofs = 0;
889 while (ofs < len)
891 DWORD ofs_start = ofs;
892 ph = (TTPOLYGONHEADER*)&start[ofs];
893 path->pathdata.Types[path->pathdata.Count] = PathPointTypeStart;
894 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(ph->pfxStart.x);
895 path->pathdata.Points[path->pathdata.Count++].Y = y + bb_end - fromfixedpoint(ph->pfxStart.y);
896 TRACE("Starting at count %i with pos %f, %f)\n", path->pathdata.Count, x, y);
897 ofs += sizeof(*ph);
898 while (ofs - ofs_start < ph->cb)
900 TTPOLYCURVE *curve = (TTPOLYCURVE*)&start[ofs];
901 int j;
902 ofs += sizeof(TTPOLYCURVE) + (curve->cpfx - 1) * sizeof(POINTFX);
904 switch (curve->wType)
906 case TT_PRIM_LINE:
907 for (j = 0; j < curve->cpfx; ++j)
909 path->pathdata.Types[path->pathdata.Count] = PathPointTypeLine;
910 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x);
911 path->pathdata.Points[path->pathdata.Count++].Y = y + bb_end - fromfixedpoint(curve->apfx[j].y);
913 break;
914 case TT_PRIM_CSPLINE:
915 for (j = 0; j < curve->cpfx; ++j)
917 path->pathdata.Types[path->pathdata.Count] = PathPointTypeBezier;
918 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x);
919 path->pathdata.Points[path->pathdata.Count++].Y = y + bb_end - fromfixedpoint(curve->apfx[j].y);
921 break;
922 default:
923 ERR("Unhandled type: %u\n", curve->wType);
924 status = GenericError;
927 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
929 path->newfigure = TRUE;
930 x += gm.gmCellIncX;
931 y += gm.gmCellIncY;
933 GdipFree(ph);
934 if (status != Ok)
935 break;
938 return status;
941 GpStatus WINGDIPAPI GdipAddPathString(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat* format)
943 GpFont *font;
944 GpStatus status;
945 HANDLE hfont;
946 HDC dc;
947 GpPath *backup;
948 struct format_string_args args;
949 int i;
951 FIXME("(%p, %s, %d, %p, %d, %f, %p, %p): stub\n", path, debugstr_w(string), length, family, style, emSize, layoutRect, format);
952 if (!path || !string || !family || !emSize || !layoutRect || !format)
953 return InvalidParameter;
955 status = GdipCreateFont(family, emSize, style, UnitPixel, &font);
956 if (status != Ok)
957 return status;
959 hfont = CreateFontIndirectW(&font->lfw);
960 if (!hfont)
962 WARN("Failed to create font\n");
963 return GenericError;
966 if ((status = GdipClonePath(path, &backup)) != Ok)
968 DeleteObject(hfont);
969 return status;
972 dc = CreateCompatibleDC(0);
973 SelectObject(dc, hfont);
975 args.path = path;
976 args.maxY = 0;
977 status = gdip_format_string(dc, string, length, NULL, layoutRect, format, format_string_callback, &args);
979 DeleteDC(dc);
980 DeleteObject(hfont);
982 if (status != Ok) /* free backup */
984 GdipFree(path->pathdata.Points);
985 GdipFree(path->pathdata.Types);
986 *path = *backup;
987 GdipFree(backup);
988 return status;
990 if (format && format->vertalign == StringAlignmentCenter && layoutRect->Y + args.maxY < layoutRect->Height)
992 float inc = layoutRect->Height - args.maxY - layoutRect->Y;
993 inc /= 2;
994 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
995 path->pathdata.Points[i].Y += inc;
996 } else if (format && format->vertalign == StringAlignmentFar) {
997 float inc = layoutRect->Height - args.maxY - layoutRect->Y;
998 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
999 path->pathdata.Points[i].Y += inc;
1001 GdipDeletePath(backup);
1002 return status;
1005 GpStatus WINGDIPAPI GdipAddPathStringI(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1007 if (layoutRect)
1009 RectF layoutRectF = {
1010 (REAL)layoutRect->X,
1011 (REAL)layoutRect->Y,
1012 (REAL)layoutRect->Width,
1013 (REAL)layoutRect->Height
1015 return GdipAddPathString(path, string, length, family, style, emSize, &layoutRectF, format);
1017 return InvalidParameter;
1020 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1022 TRACE("(%p, %p)\n", path, clone);
1024 if(!path || !clone)
1025 return InvalidParameter;
1027 *clone = GdipAlloc(sizeof(GpPath));
1028 if(!*clone) return OutOfMemory;
1030 **clone = *path;
1032 (*clone)->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
1033 (*clone)->pathdata.Types = GdipAlloc(path->datalen);
1034 if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1035 GdipFree(*clone);
1036 GdipFree((*clone)->pathdata.Points);
1037 GdipFree((*clone)->pathdata.Types);
1038 return OutOfMemory;
1041 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1042 path->datalen * sizeof(PointF));
1043 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1045 return Ok;
1048 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1050 TRACE("(%p)\n", path);
1052 if(!path)
1053 return InvalidParameter;
1055 if(path->pathdata.Count > 0){
1056 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1057 path->newfigure = TRUE;
1060 return Ok;
1063 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1065 INT i;
1067 TRACE("(%p)\n", path);
1069 if(!path)
1070 return InvalidParameter;
1072 for(i = 1; i < path->pathdata.Count; i++){
1073 if(path->pathdata.Types[i] == PathPointTypeStart)
1074 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1077 path->newfigure = TRUE;
1079 return Ok;
1082 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1084 TRACE("(%d, %p)\n", fill, path);
1086 if(!path)
1087 return InvalidParameter;
1089 *path = GdipAlloc(sizeof(GpPath));
1090 if(!*path) return OutOfMemory;
1092 (*path)->fill = fill;
1093 (*path)->newfigure = TRUE;
1095 return Ok;
1098 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1099 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1101 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1103 if(!path)
1104 return InvalidParameter;
1106 *path = GdipAlloc(sizeof(GpPath));
1107 if(!*path) return OutOfMemory;
1109 (*path)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
1110 (*path)->pathdata.Types = GdipAlloc(count);
1112 if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
1113 GdipFree((*path)->pathdata.Points);
1114 GdipFree((*path)->pathdata.Types);
1115 GdipFree(*path);
1116 return OutOfMemory;
1119 memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
1120 memcpy((*path)->pathdata.Types, types, count);
1121 (*path)->pathdata.Count = count;
1122 (*path)->datalen = count;
1124 (*path)->fill = fill;
1125 (*path)->newfigure = TRUE;
1127 return Ok;
1130 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1131 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1133 GpPointF *ptF;
1134 GpStatus ret;
1135 INT i;
1137 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1139 ptF = GdipAlloc(sizeof(GpPointF)*count);
1141 for(i = 0;i < count; i++){
1142 ptF[i].X = (REAL)points[i].X;
1143 ptF[i].Y = (REAL)points[i].Y;
1146 ret = GdipCreatePath2(ptF, types, count, fill, path);
1148 GdipFree(ptF);
1150 return ret;
1153 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1155 TRACE("(%p)\n", path);
1157 if(!path)
1158 return InvalidParameter;
1160 GdipFree(path->pathdata.Points);
1161 GdipFree(path->pathdata.Types);
1162 GdipFree(path);
1164 return Ok;
1167 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1169 path_list_node_t *list, *node;
1170 GpPointF pt;
1171 INT i = 1;
1172 INT startidx = 0;
1174 TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1176 if(!path)
1177 return InvalidParameter;
1179 if(matrix){
1180 WARN("transformation not supported yet!\n");
1181 return NotImplemented;
1184 if(path->pathdata.Count == 0)
1185 return Ok;
1187 pt = path->pathdata.Points[0];
1188 if(!init_path_list(&list, pt.X, pt.Y))
1189 return OutOfMemory;
1191 node = list;
1193 while(i < path->pathdata.Count){
1195 BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1196 path_list_node_t *start;
1198 pt = path->pathdata.Points[i];
1200 /* save last start point index */
1201 if(type == PathPointTypeStart)
1202 startidx = i;
1204 /* always add line points and start points */
1205 if((type == PathPointTypeStart) || (type == PathPointTypeLine)){
1206 if(!add_path_list_node(node, pt.X, pt.Y, path->pathdata.Types[i]))
1207 goto memout;
1209 node = node->next;
1210 ++i;
1211 continue;
1214 /* Bezier curve */
1216 /* test for closed figure */
1217 if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1218 pt = path->pathdata.Points[startidx];
1219 ++i;
1221 else
1223 i += 2;
1224 pt = path->pathdata.Points[i];
1227 start = node;
1228 /* add Bezier end point */
1229 type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1230 if(!add_path_list_node(node, pt.X, pt.Y, type))
1231 goto memout;
1232 node = node->next;
1234 /* flatten curve */
1235 if(!flatten_bezier(start, path->pathdata.Points[i-2].X, path->pathdata.Points[i-2].Y,
1236 path->pathdata.Points[i-1].X, path->pathdata.Points[i-1].Y,
1237 node, flatness))
1238 goto memout;
1240 ++i;
1241 }/* while */
1243 /* store path data back */
1244 i = path_list_count(list);
1245 if(!lengthen_path(path, i))
1246 goto memout;
1247 path->pathdata.Count = i;
1249 node = list;
1250 for(i = 0; i < path->pathdata.Count; i++){
1251 path->pathdata.Points[i] = node->pt;
1252 path->pathdata.Types[i] = node->type;
1253 node = node->next;
1256 free_path_list(list);
1257 return Ok;
1259 memout:
1260 free_path_list(list);
1261 return OutOfMemory;
1264 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1266 TRACE("(%p, %p)\n", path, pathData);
1268 if(!path || !pathData)
1269 return InvalidParameter;
1271 /* Only copy data. pathData allocation/freeing controlled by wrapper class.
1272 Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
1273 memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
1274 memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
1276 return Ok;
1279 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1281 TRACE("(%p, %p)\n", path, fillmode);
1283 if(!path || !fillmode)
1284 return InvalidParameter;
1286 *fillmode = path->fill;
1288 return Ok;
1291 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1293 INT count;
1295 TRACE("(%p, %p)\n", path, lastPoint);
1297 if(!path || !lastPoint)
1298 return InvalidParameter;
1300 count = path->pathdata.Count;
1301 if(count > 0)
1302 *lastPoint = path->pathdata.Points[count-1];
1304 return Ok;
1307 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1309 TRACE("(%p, %p, %d)\n", path, points, count);
1311 if(!path)
1312 return InvalidParameter;
1314 if(count < path->pathdata.Count)
1315 return InsufficientBuffer;
1317 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1319 return Ok;
1322 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1324 GpStatus ret;
1325 GpPointF *ptf;
1326 INT i;
1328 TRACE("(%p, %p, %d)\n", path, points, count);
1330 if(count <= 0)
1331 return InvalidParameter;
1333 ptf = GdipAlloc(sizeof(GpPointF)*count);
1334 if(!ptf) return OutOfMemory;
1336 ret = GdipGetPathPoints(path,ptf,count);
1337 if(ret == Ok)
1338 for(i = 0;i < count;i++){
1339 points[i].X = roundr(ptf[i].X);
1340 points[i].Y = roundr(ptf[i].Y);
1342 GdipFree(ptf);
1344 return ret;
1347 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1349 TRACE("(%p, %p, %d)\n", path, types, count);
1351 if(!path)
1352 return InvalidParameter;
1354 if(count < path->pathdata.Count)
1355 return InsufficientBuffer;
1357 memcpy(types, path->pathdata.Types, path->pathdata.Count);
1359 return Ok;
1362 /* Windows expands the bounding box to the maximum possible bounding box
1363 * for a given pen. For example, if a line join can extend past the point
1364 * it's joining by x units, the bounding box is extended by x units in every
1365 * direction (even though this is too conservative for most cases). */
1366 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
1367 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1369 GpPointF * points, temp_pts[4];
1370 INT count, i;
1371 REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
1373 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1375 /* Matrix and pen can be null. */
1376 if(!path || !bounds)
1377 return InvalidParameter;
1379 /* If path is empty just return. */
1380 count = path->pathdata.Count;
1381 if(count == 0){
1382 bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
1383 return Ok;
1386 points = path->pathdata.Points;
1388 low_x = high_x = points[0].X;
1389 low_y = high_y = points[0].Y;
1391 for(i = 1; i < count; i++){
1392 low_x = min(low_x, points[i].X);
1393 low_y = min(low_y, points[i].Y);
1394 high_x = max(high_x, points[i].X);
1395 high_y = max(high_y, points[i].Y);
1398 width = high_x - low_x;
1399 height = high_y - low_y;
1401 /* This looks unusual but it's the only way I can imitate windows. */
1402 if(matrix){
1403 temp_pts[0].X = low_x;
1404 temp_pts[0].Y = low_y;
1405 temp_pts[1].X = low_x;
1406 temp_pts[1].Y = high_y;
1407 temp_pts[2].X = high_x;
1408 temp_pts[2].Y = high_y;
1409 temp_pts[3].X = high_x;
1410 temp_pts[3].Y = low_y;
1412 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1413 low_x = temp_pts[0].X;
1414 low_y = temp_pts[0].Y;
1416 for(i = 1; i < 4; i++){
1417 low_x = min(low_x, temp_pts[i].X);
1418 low_y = min(low_y, temp_pts[i].Y);
1421 temp = width;
1422 width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
1423 height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
1426 if(pen){
1427 path_width = pen->width / 2.0;
1429 if(count > 2)
1430 path_width = max(path_width, pen->width * pen->miterlimit / 2.0);
1431 /* FIXME: this should probably also check for the startcap */
1432 if(pen->endcap & LineCapNoAnchor)
1433 path_width = max(path_width, pen->width * 2.2);
1435 low_x -= path_width;
1436 low_y -= path_width;
1437 width += 2.0 * path_width;
1438 height += 2.0 * path_width;
1441 bounds->X = low_x;
1442 bounds->Y = low_y;
1443 bounds->Width = width;
1444 bounds->Height = height;
1446 return Ok;
1449 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1450 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1452 GpStatus ret;
1453 GpRectF boundsF;
1455 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1457 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
1459 if(ret == Ok){
1460 bounds->X = roundr(boundsF.X);
1461 bounds->Y = roundr(boundsF.Y);
1462 bounds->Width = roundr(boundsF.Width);
1463 bounds->Height = roundr(boundsF.Height);
1466 return ret;
1469 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1471 TRACE("(%p, %p)\n", path, count);
1473 if(!path)
1474 return InvalidParameter;
1476 *count = path->pathdata.Count;
1478 return Ok;
1481 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1483 INT i, count;
1484 INT start = 0; /* position in reversed path */
1485 GpPathData revpath;
1487 TRACE("(%p)\n", path);
1489 if(!path)
1490 return InvalidParameter;
1492 count = path->pathdata.Count;
1494 if(count == 0) return Ok;
1496 revpath.Points = GdipAlloc(sizeof(GpPointF)*count);
1497 revpath.Types = GdipAlloc(sizeof(BYTE)*count);
1498 revpath.Count = count;
1499 if(!revpath.Points || !revpath.Types){
1500 GdipFree(revpath.Points);
1501 GdipFree(revpath.Types);
1502 return OutOfMemory;
1505 for(i = 0; i < count; i++){
1507 /* find next start point */
1508 if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1509 INT j;
1510 for(j = start; j <= i; j++){
1511 revpath.Points[j] = path->pathdata.Points[count-j-1];
1512 revpath.Types[j] = path->pathdata.Types[count-j-1];
1514 /* mark start point */
1515 revpath.Types[start] = PathPointTypeStart;
1516 /* set 'figure' endpoint type */
1517 if(i-start > 1){
1518 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1519 revpath.Types[i] |= revpath.Types[i-1];
1521 else
1522 revpath.Types[i] = path->pathdata.Types[start];
1524 start = i+1;
1528 memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1529 memcpy(path->pathdata.Types, revpath.Types, sizeof(BYTE)*count);
1531 GdipFree(revpath.Points);
1532 GdipFree(revpath.Types);
1534 return Ok;
1537 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1538 GpPen *pen, GpGraphics *graphics, BOOL *result)
1540 TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1542 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1545 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1546 GpPen *pen, GpGraphics *graphics, BOOL *result)
1548 static int calls;
1550 TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1552 if(!path || !pen)
1553 return InvalidParameter;
1555 if(!(calls++))
1556 FIXME("not implemented\n");
1558 return NotImplemented;
1561 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1563 TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1565 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1568 /*****************************************************************************
1569 * GdipIsVisiblePathPoint [GDIPLUS.@]
1571 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1573 GpRegion *region;
1574 HRGN hrgn;
1575 GpStatus status;
1577 if(!path || !result) return InvalidParameter;
1579 status = GdipCreateRegionPath(path, &region);
1580 if(status != Ok)
1581 return status;
1583 status = GdipGetRegionHRgn(region, graphics, &hrgn);
1584 if(status != Ok){
1585 GdipDeleteRegion(region);
1586 return status;
1589 *result = PtInRegion(hrgn, roundr(x), roundr(y));
1591 DeleteObject(hrgn);
1592 GdipDeleteRegion(region);
1594 return Ok;
1597 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1599 TRACE("(%p)\n", path);
1601 if(!path)
1602 return InvalidParameter;
1604 path->newfigure = TRUE;
1606 return Ok;
1609 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1611 TRACE("(%p)\n", path);
1613 if(!path)
1614 return InvalidParameter;
1616 path->pathdata.Count = 0;
1617 path->newfigure = TRUE;
1618 path->fill = FillModeAlternate;
1620 return Ok;
1623 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1625 TRACE("(%p, %d)\n", path, fill);
1627 if(!path)
1628 return InvalidParameter;
1630 path->fill = fill;
1632 return Ok;
1635 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1637 TRACE("(%p, %p)\n", path, matrix);
1639 if(!path)
1640 return InvalidParameter;
1642 if(path->pathdata.Count == 0)
1643 return Ok;
1645 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1646 path->pathdata.Count);
1649 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1650 GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1651 REAL height, WarpMode warpmode, REAL flatness)
1653 FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1654 points, count, x, y, width, height, warpmode, flatness);
1656 return NotImplemented;
1659 static void add_bevel_point(const GpPointF *endpoint, const GpPointF *nextpoint,
1660 GpPen *pen, int right_side, path_list_node_t **last_point)
1662 REAL segment_dy = nextpoint->Y-endpoint->Y;
1663 REAL segment_dx = nextpoint->X-endpoint->X;
1664 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1665 REAL distance = pen->width/2.0;
1666 REAL bevel_dx, bevel_dy;
1668 if (right_side)
1670 bevel_dx = -distance * segment_dy / segment_length;
1671 bevel_dy = distance * segment_dx / segment_length;
1673 else
1675 bevel_dx = distance * segment_dy / segment_length;
1676 bevel_dy = -distance * segment_dx / segment_length;
1679 *last_point = add_path_list_node(*last_point, endpoint->X + bevel_dx,
1680 endpoint->Y + bevel_dy, PathPointTypeLine);
1683 static void widen_joint(const GpPointF *p1, const GpPointF *p2, const GpPointF *p3,
1684 GpPen* pen, path_list_node_t **last_point)
1686 switch (pen->join)
1688 default:
1689 case LineJoinBevel:
1690 add_bevel_point(p2, p1, pen, 1, last_point);
1691 add_bevel_point(p2, p3, pen, 0, last_point);
1692 break;
1696 static void widen_cap(const GpPointF *endpoint, const GpPointF *nextpoint,
1697 GpPen *pen, GpLineCap cap, GpCustomLineCap *custom, int add_first_points,
1698 int add_last_point, path_list_node_t **last_point)
1700 switch (cap)
1702 default:
1703 case LineCapFlat:
1704 if (add_first_points)
1705 add_bevel_point(endpoint, nextpoint, pen, 1, last_point);
1706 if (add_last_point)
1707 add_bevel_point(endpoint, nextpoint, pen, 0, last_point);
1708 break;
1712 static void widen_open_figure(GpPath *path, GpPen *pen, int start, int end,
1713 path_list_node_t **last_point)
1715 int i;
1717 if (end <= start)
1718 return;
1720 widen_cap(&path->pathdata.Points[start], &path->pathdata.Points[start+1],
1721 pen, pen->startcap, pen->customstart, FALSE, TRUE, last_point);
1723 (*last_point)->type = PathPointTypeStart;
1725 for (i=start+1; i<end; i++)
1726 widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
1727 &path->pathdata.Points[i+1], pen, last_point);
1729 widen_cap(&path->pathdata.Points[end], &path->pathdata.Points[end-1],
1730 pen, pen->endcap, pen->customend, TRUE, TRUE, last_point);
1732 for (i=end-1; i>start; i--)
1733 widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
1734 &path->pathdata.Points[i-1], pen, last_point);
1736 widen_cap(&path->pathdata.Points[start], &path->pathdata.Points[start+1],
1737 pen, pen->startcap, pen->customstart, TRUE, FALSE, last_point);
1739 (*last_point)->type |= PathPointTypeCloseSubpath;
1742 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
1743 REAL flatness)
1745 GpPath *flat_path=NULL;
1746 GpStatus status;
1747 path_list_node_t *points=NULL, *last_point=NULL;
1748 int i, subpath_start=0, new_length;
1749 BYTE type;
1751 TRACE("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
1753 if (!path || !pen)
1754 return InvalidParameter;
1756 if (path->pathdata.Count <= 1)
1757 return OutOfMemory;
1759 status = GdipClonePath(path, &flat_path);
1761 if (status == Ok)
1762 status = GdipFlattenPath(flat_path, matrix, flatness);
1764 if (status == Ok && !init_path_list(&points, 314.0, 22.0))
1765 status = OutOfMemory;
1767 if (status == Ok)
1769 last_point = points;
1771 if (pen->endcap != LineCapFlat)
1772 FIXME("unimplemented end cap %x\n", pen->endcap);
1774 if (pen->startcap != LineCapFlat)
1775 FIXME("unimplemented start cap %x\n", pen->startcap);
1777 if (pen->dashcap != DashCapFlat)
1778 FIXME("unimplemented dash cap %d\n", pen->dashcap);
1780 if (pen->join != LineJoinBevel)
1781 FIXME("unimplemented line join %d\n", pen->join);
1783 if (pen->dash != DashStyleSolid)
1784 FIXME("unimplemented dash style %d\n", pen->dash);
1786 if (pen->align != PenAlignmentCenter)
1787 FIXME("unimplemented pen alignment %d\n", pen->align);
1789 for (i=0; i < flat_path->pathdata.Count; i++)
1791 type = flat_path->pathdata.Types[i];
1793 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1794 subpath_start = i;
1796 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
1798 FIXME("closed figures unimplemented\n");
1800 else if (i == flat_path->pathdata.Count-1 ||
1801 (flat_path->pathdata.Types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
1803 widen_open_figure(flat_path, pen, subpath_start, i, &last_point);
1807 new_length = path_list_count(points)-1;
1809 if (!lengthen_path(path, new_length))
1810 status = OutOfMemory;
1813 if (status == Ok)
1815 path->pathdata.Count = new_length;
1817 last_point = points->next;
1818 for (i = 0; i < new_length; i++)
1820 path->pathdata.Points[i] = last_point->pt;
1821 path->pathdata.Types[i] = last_point->type;
1822 last_point = last_point->next;
1825 path->fill = FillModeWinding;
1828 free_path_list(points);
1830 GdipDeletePath(flat_path);
1832 return status;
1835 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
1836 REAL width, REAL height)
1838 GpPath *backup;
1839 GpPointF ptf[2];
1840 GpStatus retstat;
1841 BOOL old_new;
1843 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
1845 if(!path)
1846 return InvalidParameter;
1848 /* make a backup copy of path data */
1849 if((retstat = GdipClonePath(path, &backup)) != Ok)
1850 return retstat;
1852 /* rectangle should start as new path */
1853 old_new = path->newfigure;
1854 path->newfigure = TRUE;
1855 if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
1856 path->newfigure = old_new;
1857 goto fail;
1860 ptf[0].X = x+width;
1861 ptf[0].Y = y+height;
1862 ptf[1].X = x;
1863 ptf[1].Y = y+height;
1865 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
1866 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
1868 /* free backup */
1869 GdipDeletePath(backup);
1870 return Ok;
1872 fail:
1873 /* reverting */
1874 GdipFree(path->pathdata.Points);
1875 GdipFree(path->pathdata.Types);
1876 memcpy(path, backup, sizeof(*path));
1877 GdipFree(backup);
1879 return retstat;
1882 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
1883 INT width, INT height)
1885 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
1887 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1890 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
1892 GpPath *backup;
1893 GpStatus retstat;
1894 INT i;
1896 TRACE("(%p, %p, %d)\n", path, rects, count);
1898 /* count == 0 - verified condition */
1899 if(!path || !rects || count == 0)
1900 return InvalidParameter;
1902 if(count < 0)
1903 return OutOfMemory;
1905 /* make a backup copy */
1906 if((retstat = GdipClonePath(path, &backup)) != Ok)
1907 return retstat;
1909 for(i = 0; i < count; i++){
1910 if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
1911 goto fail;
1914 /* free backup */
1915 GdipDeletePath(backup);
1916 return Ok;
1918 fail:
1919 /* reverting */
1920 GdipFree(path->pathdata.Points);
1921 GdipFree(path->pathdata.Types);
1922 memcpy(path, backup, sizeof(*path));
1923 GdipFree(backup);
1925 return retstat;
1928 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
1930 GpRectF *rectsF;
1931 GpStatus retstat;
1932 INT i;
1934 TRACE("(%p, %p, %d)\n", path, rects, count);
1936 if(!rects || count == 0)
1937 return InvalidParameter;
1939 if(count < 0)
1940 return OutOfMemory;
1942 rectsF = GdipAlloc(sizeof(GpRectF)*count);
1944 for(i = 0;i < count;i++){
1945 rectsF[i].X = (REAL)rects[i].X;
1946 rectsF[i].Y = (REAL)rects[i].Y;
1947 rectsF[i].Width = (REAL)rects[i].Width;
1948 rectsF[i].Height = (REAL)rects[i].Height;
1951 retstat = GdipAddPathRectangles(path, rectsF, count);
1952 GdipFree(rectsF);
1954 return retstat;
1957 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
1959 INT count;
1961 TRACE("(%p)\n", path);
1963 if(!path)
1964 return InvalidParameter;
1966 count = path->pathdata.Count;
1968 /* set marker flag */
1969 if(count > 0)
1970 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
1972 return Ok;
1975 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
1977 INT count;
1978 INT i;
1980 TRACE("(%p)\n", path);
1982 if(!path)
1983 return InvalidParameter;
1985 count = path->pathdata.Count;
1987 for(i = 0; i < count - 1; i++){
1988 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
1991 return Ok;
1994 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
1996 FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
1997 return NotImplemented;