wined3d: Enable the multi-threaded command stream by default.
[wine.git] / dlls / gdiplus / graphicspath.c
blob8b13fc5b2a0f2717a2639b1da4c66f53359d2d77
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 if ((x2 == mp[0].X && y2 == mp[0].Y && x3 == mp[1].X && y3 == mp[1].Y) ||
146 (x2 == mp[3].X && y2 == mp[3].Y && x3 == mp[4].X && y3 == mp[4].Y))
147 return TRUE;
149 pt = end->pt;
150 pt_st = start->pt;
151 /* check flatness as a half of distance between middle point and a linearized path */
152 if(fabs(((pt.Y - pt_st.Y)*mp[2].X + (pt_st.X - pt.X)*mp[2].Y +
153 (pt_st.Y*pt.X - pt_st.X*pt.Y))) <=
154 (0.5 * flatness*sqrtf((powf(pt.Y - pt_st.Y, 2.0) + powf(pt_st.X - pt.X, 2.0))))){
155 return TRUE;
157 else
158 /* add a middle point */
159 if(!(node = add_path_list_node(start, mp[2].X, mp[2].Y, PathPointTypeLine)))
160 return FALSE;
162 /* do the same with halves */
163 flatten_bezier(start, mp[0].X, mp[0].Y, mp[1].X, mp[1].Y, node, flatness);
164 flatten_bezier(node, mp[3].X, mp[3].Y, mp[4].X, mp[4].Y, end, flatness);
166 return TRUE;
169 /*******************************************************************************
170 * GdipAddPathArc [GDIPLUS.1]
172 * Add an elliptical arc to the given path.
174 * PARAMS
175 * path [I/O] Path that the arc is appended to
176 * x1 [I] X coordinate of the boundary box
177 * y1 [I] Y coordinate of the boundary box
178 * x2 [I] Width of the boundary box
179 * y2 [I] Height of the boundary box
180 * startAngle [I] Starting angle of the arc, clockwise
181 * sweepAngle [I] Angle of the arc, clockwise
183 * RETURNS
184 * InvalidParameter If the given path is invalid
185 * OutOfMemory If memory allocation fails, i.e. the path cannot be lengthened
186 * Ok If everything works out as expected
188 * NOTES
189 * This functions takes the newfigure value of the given path into account,
190 * i.e. the arc is connected to the end of the given path if it was set to
191 * FALSE, otherwise the arc's first point gets the PathPointTypeStart value.
192 * In both cases, the value of newfigure of the given path is FALSE
193 * afterwards.
195 GpStatus WINGDIPAPI GdipAddPathArc(GpPath *path, REAL x1, REAL y1, REAL x2,
196 REAL y2, REAL startAngle, REAL sweepAngle)
198 INT count, old_count, i;
200 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
201 path, x1, y1, x2, y2, startAngle, sweepAngle);
203 if(!path)
204 return InvalidParameter;
206 count = arc2polybezier(NULL, x1, y1, x2, y2, startAngle, sweepAngle);
208 if(count == 0)
209 return Ok;
210 if(!lengthen_path(path, count))
211 return OutOfMemory;
213 old_count = path->pathdata.Count;
214 arc2polybezier(&path->pathdata.Points[old_count], x1, y1, x2, y2,
215 startAngle, sweepAngle);
217 for(i = 0; i < count; i++){
218 path->pathdata.Types[old_count + i] = PathPointTypeBezier;
221 path->pathdata.Types[old_count] =
222 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
223 path->newfigure = FALSE;
224 path->pathdata.Count += count;
226 return Ok;
229 /*******************************************************************************
230 * GdipAddPathArcI [GDUPLUS.2]
232 * See GdipAddPathArc
234 GpStatus WINGDIPAPI GdipAddPathArcI(GpPath *path, INT x1, INT y1, INT x2,
235 INT y2, REAL startAngle, REAL sweepAngle)
237 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
238 path, x1, y1, x2, y2, startAngle, sweepAngle);
240 return GdipAddPathArc(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,startAngle,sweepAngle);
243 GpStatus WINGDIPAPI GdipAddPathBezier(GpPath *path, REAL x1, REAL y1, REAL x2,
244 REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
246 INT old_count;
248 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
249 path, x1, y1, x2, y2, x3, y3, x4, y4);
251 if(!path)
252 return InvalidParameter;
254 if(!lengthen_path(path, 4))
255 return OutOfMemory;
257 old_count = path->pathdata.Count;
259 path->pathdata.Points[old_count].X = x1;
260 path->pathdata.Points[old_count].Y = y1;
261 path->pathdata.Points[old_count + 1].X = x2;
262 path->pathdata.Points[old_count + 1].Y = y2;
263 path->pathdata.Points[old_count + 2].X = x3;
264 path->pathdata.Points[old_count + 2].Y = y3;
265 path->pathdata.Points[old_count + 3].X = x4;
266 path->pathdata.Points[old_count + 3].Y = y4;
268 path->pathdata.Types[old_count] =
269 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
270 path->pathdata.Types[old_count + 1] = PathPointTypeBezier;
271 path->pathdata.Types[old_count + 2] = PathPointTypeBezier;
272 path->pathdata.Types[old_count + 3] = PathPointTypeBezier;
274 path->newfigure = FALSE;
275 path->pathdata.Count += 4;
277 return Ok;
280 GpStatus WINGDIPAPI GdipAddPathBezierI(GpPath *path, INT x1, INT y1, INT x2,
281 INT y2, INT x3, INT y3, INT x4, INT y4)
283 TRACE("(%p, %d, %d, %d, %d, %d, %d, %d, %d)\n",
284 path, x1, y1, x2, y2, x3, y3, x4, y4);
286 return GdipAddPathBezier(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,(REAL)x3,(REAL)y3,
287 (REAL)x4,(REAL)y4);
290 GpStatus WINGDIPAPI GdipAddPathBeziers(GpPath *path, GDIPCONST GpPointF *points,
291 INT count)
293 INT i, old_count;
295 TRACE("(%p, %p, %d)\n", path, points, count);
297 if(!path || !points || ((count - 1) % 3))
298 return InvalidParameter;
300 if(!lengthen_path(path, count))
301 return OutOfMemory;
303 old_count = path->pathdata.Count;
305 for(i = 0; i < count; i++){
306 path->pathdata.Points[old_count + i].X = points[i].X;
307 path->pathdata.Points[old_count + i].Y = points[i].Y;
308 path->pathdata.Types[old_count + i] = PathPointTypeBezier;
311 path->pathdata.Types[old_count] =
312 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
313 path->newfigure = FALSE;
314 path->pathdata.Count += count;
316 return Ok;
319 GpStatus WINGDIPAPI GdipAddPathBeziersI(GpPath *path, GDIPCONST GpPoint *points,
320 INT count)
322 GpPointF *ptsF;
323 GpStatus ret;
324 INT i;
326 TRACE("(%p, %p, %d)\n", path, points, count);
328 if(!points || ((count - 1) % 3))
329 return InvalidParameter;
331 ptsF = heap_alloc_zero(sizeof(GpPointF) * count);
332 if(!ptsF)
333 return OutOfMemory;
335 for(i = 0; i < count; i++){
336 ptsF[i].X = (REAL)points[i].X;
337 ptsF[i].Y = (REAL)points[i].Y;
340 ret = GdipAddPathBeziers(path, ptsF, count);
341 heap_free(ptsF);
343 return ret;
346 GpStatus WINGDIPAPI GdipAddPathClosedCurve(GpPath *path, GDIPCONST GpPointF *points,
347 INT count)
349 TRACE("(%p, %p, %d)\n", path, points, count);
351 return GdipAddPathClosedCurve2(path, points, count, 1.0);
354 GpStatus WINGDIPAPI GdipAddPathClosedCurveI(GpPath *path, GDIPCONST GpPoint *points,
355 INT count)
357 TRACE("(%p, %p, %d)\n", path, points, count);
359 return GdipAddPathClosedCurve2I(path, points, count, 1.0);
362 GpStatus WINGDIPAPI GdipAddPathClosedCurve2(GpPath *path, GDIPCONST GpPointF *points,
363 INT count, REAL tension)
365 INT i, len_pt = (count + 1)*3-2;
366 GpPointF *pt;
367 GpPointF *pts;
368 REAL x1, x2, y1, y2;
369 GpStatus stat;
371 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
373 if(!path || !points || count <= 1)
374 return InvalidParameter;
376 pt = heap_alloc_zero(len_pt * sizeof(GpPointF));
377 pts = heap_alloc_zero((count + 1)*sizeof(GpPointF));
378 if(!pt || !pts){
379 heap_free(pt);
380 heap_free(pts);
381 return OutOfMemory;
384 /* copy source points to extend with the last one */
385 memcpy(pts, points, sizeof(GpPointF)*count);
386 pts[count] = pts[0];
388 tension = tension * TENSION_CONST;
390 for(i = 0; i < count-1; i++){
391 calc_curve_bezier(&(pts[i]), tension, &x1, &y1, &x2, &y2);
393 pt[3*i+2].X = x1;
394 pt[3*i+2].Y = y1;
395 pt[3*i+3].X = pts[i+1].X;
396 pt[3*i+3].Y = pts[i+1].Y;
397 pt[3*i+4].X = x2;
398 pt[3*i+4].Y = y2;
401 /* points [len_pt-2] and [0] are calculated
402 separately to connect splines properly */
403 pts[0] = points[count-1];
404 pts[1] = points[0]; /* equals to start and end of a resulting path */
405 pts[2] = points[1];
407 calc_curve_bezier(pts, tension, &x1, &y1, &x2, &y2);
408 pt[len_pt-2].X = x1;
409 pt[len_pt-2].Y = y1;
410 pt[0].X = pts[1].X;
411 pt[0].Y = pts[1].Y;
412 pt[1].X = x2;
413 pt[1].Y = y2;
414 /* close path */
415 pt[len_pt-1].X = pt[0].X;
416 pt[len_pt-1].Y = pt[0].Y;
418 stat = GdipAddPathBeziers(path, pt, len_pt);
420 /* close figure */
421 if(stat == Ok){
422 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
423 path->newfigure = TRUE;
426 heap_free(pts);
427 heap_free(pt);
429 return stat;
432 GpStatus WINGDIPAPI GdipAddPathClosedCurve2I(GpPath *path, GDIPCONST GpPoint *points,
433 INT count, REAL tension)
435 GpPointF *ptf;
436 INT i;
437 GpStatus stat;
439 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
441 if(!path || !points || count <= 1)
442 return InvalidParameter;
444 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
445 if(!ptf)
446 return OutOfMemory;
448 for(i = 0; i < count; i++){
449 ptf[i].X = (REAL)points[i].X;
450 ptf[i].Y = (REAL)points[i].Y;
453 stat = GdipAddPathClosedCurve2(path, ptf, count, tension);
455 heap_free(ptf);
457 return stat;
460 GpStatus WINGDIPAPI GdipAddPathCurve(GpPath *path, GDIPCONST GpPointF *points, INT count)
462 TRACE("(%p, %p, %d)\n", path, points, count);
464 if(!path || !points || count <= 1)
465 return InvalidParameter;
467 return GdipAddPathCurve2(path, points, count, 1.0);
470 GpStatus WINGDIPAPI GdipAddPathCurveI(GpPath *path, GDIPCONST GpPoint *points, INT count)
472 TRACE("(%p, %p, %d)\n", path, points, count);
474 if(!path || !points || count <= 1)
475 return InvalidParameter;
477 return GdipAddPathCurve2I(path, points, count, 1.0);
480 GpStatus WINGDIPAPI GdipAddPathCurve2(GpPath *path, GDIPCONST GpPointF *points, INT count,
481 REAL tension)
483 INT i, len_pt = count*3-2;
484 GpPointF *pt;
485 REAL x1, x2, y1, y2;
486 GpStatus stat;
488 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
490 if(!path || !points || count <= 1)
491 return InvalidParameter;
493 pt = heap_alloc_zero(len_pt * sizeof(GpPointF));
494 if(!pt)
495 return OutOfMemory;
497 tension = tension * TENSION_CONST;
499 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
500 tension, &x1, &y1);
502 pt[0].X = points[0].X;
503 pt[0].Y = points[0].Y;
504 pt[1].X = x1;
505 pt[1].Y = y1;
507 for(i = 0; i < count-2; i++){
508 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
510 pt[3*i+2].X = x1;
511 pt[3*i+2].Y = y1;
512 pt[3*i+3].X = points[i+1].X;
513 pt[3*i+3].Y = points[i+1].Y;
514 pt[3*i+4].X = x2;
515 pt[3*i+4].Y = y2;
518 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
519 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
521 pt[len_pt-2].X = x1;
522 pt[len_pt-2].Y = y1;
523 pt[len_pt-1].X = points[count-1].X;
524 pt[len_pt-1].Y = points[count-1].Y;
526 stat = GdipAddPathBeziers(path, pt, len_pt);
528 heap_free(pt);
530 return stat;
533 GpStatus WINGDIPAPI GdipAddPathCurve2I(GpPath *path, GDIPCONST GpPoint *points,
534 INT count, REAL tension)
536 GpPointF *ptf;
537 INT i;
538 GpStatus stat;
540 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
542 if(!path || !points || count <= 1)
543 return InvalidParameter;
545 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
546 if(!ptf)
547 return OutOfMemory;
549 for(i = 0; i < count; i++){
550 ptf[i].X = (REAL)points[i].X;
551 ptf[i].Y = (REAL)points[i].Y;
554 stat = GdipAddPathCurve2(path, ptf, count, tension);
556 heap_free(ptf);
558 return stat;
561 GpStatus WINGDIPAPI GdipAddPathCurve3(GpPath *path, GDIPCONST GpPointF *points,
562 INT count, INT offset, INT nseg, REAL tension)
564 TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
566 if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
567 return InvalidParameter;
569 return GdipAddPathCurve2(path, &points[offset], nseg + 1, tension);
572 GpStatus WINGDIPAPI GdipAddPathCurve3I(GpPath *path, GDIPCONST GpPoint *points,
573 INT count, INT offset, INT nseg, REAL tension)
575 TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
577 if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
578 return InvalidParameter;
580 return GdipAddPathCurve2I(path, &points[offset], nseg + 1, tension);
583 GpStatus WINGDIPAPI GdipAddPathEllipse(GpPath *path, REAL x, REAL y, REAL width,
584 REAL height)
586 INT old_count, numpts;
588 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
590 if(!path)
591 return InvalidParameter;
593 if(!lengthen_path(path, MAX_ARC_PTS))
594 return OutOfMemory;
596 old_count = path->pathdata.Count;
597 if((numpts = arc2polybezier(&path->pathdata.Points[old_count], x, y, width,
598 height, 0.0, 360.0)) != MAX_ARC_PTS){
599 ERR("expected %d points but got %d\n", MAX_ARC_PTS, numpts);
600 return GenericError;
603 memset(&path->pathdata.Types[old_count + 1], PathPointTypeBezier,
604 MAX_ARC_PTS - 1);
606 /* An ellipse is an intrinsic figure (always is its own subpath). */
607 path->pathdata.Types[old_count] = PathPointTypeStart;
608 path->pathdata.Types[old_count + MAX_ARC_PTS - 1] |= PathPointTypeCloseSubpath;
609 path->newfigure = TRUE;
610 path->pathdata.Count += MAX_ARC_PTS;
612 return Ok;
615 GpStatus WINGDIPAPI GdipAddPathEllipseI(GpPath *path, INT x, INT y, INT width,
616 INT height)
618 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
620 return GdipAddPathEllipse(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
623 GpStatus WINGDIPAPI GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
624 INT count)
626 INT i, old_count;
628 TRACE("(%p, %p, %d)\n", path, points, count);
630 if(!path || !points)
631 return InvalidParameter;
633 if(!lengthen_path(path, count))
634 return OutOfMemory;
636 old_count = path->pathdata.Count;
638 for(i = 0; i < count; i++){
639 path->pathdata.Points[old_count + i].X = points[i].X;
640 path->pathdata.Points[old_count + i].Y = points[i].Y;
641 path->pathdata.Types[old_count + i] = PathPointTypeLine;
644 if(path->newfigure){
645 path->pathdata.Types[old_count] = PathPointTypeStart;
646 path->newfigure = FALSE;
649 path->pathdata.Count += count;
651 return Ok;
654 GpStatus WINGDIPAPI GdipAddPathLine2I(GpPath *path, GDIPCONST GpPoint *points, INT count)
656 GpPointF *pointsF;
657 INT i;
658 GpStatus stat;
660 TRACE("(%p, %p, %d)\n", path, points, count);
662 if(count <= 0)
663 return InvalidParameter;
665 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
666 if(!pointsF) return OutOfMemory;
668 for(i = 0;i < count; i++){
669 pointsF[i].X = (REAL)points[i].X;
670 pointsF[i].Y = (REAL)points[i].Y;
673 stat = GdipAddPathLine2(path, pointsF, count);
675 heap_free(pointsF);
677 return stat;
680 /*************************************************************************
681 * GdipAddPathLine [GDIPLUS.21]
683 * Add two points to the given path.
685 * PARAMS
686 * path [I/O] Path that the line is appended to
687 * x1 [I] X coordinate of the first point of the line
688 * y1 [I] Y coordinate of the first point of the line
689 * x2 [I] X coordinate of the second point of the line
690 * y2 [I] Y coordinate of the second point of the line
692 * RETURNS
693 * InvalidParameter If the first parameter is not a valid path
694 * OutOfMemory If the path cannot be lengthened, i.e. memory allocation fails
695 * Ok If everything works out as expected
697 * NOTES
698 * This functions takes the newfigure value of the given path into account,
699 * i.e. the two new points are connected to the end of the given path if it
700 * was set to FALSE, otherwise the first point is given the PathPointTypeStart
701 * value. In both cases, the value of newfigure of the given path is FALSE
702 * afterwards.
704 GpStatus WINGDIPAPI GdipAddPathLine(GpPath *path, REAL x1, REAL y1, REAL x2, REAL y2)
706 INT old_count;
708 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x1, y1, x2, y2);
710 if(!path)
711 return InvalidParameter;
713 if(!lengthen_path(path, 2))
714 return OutOfMemory;
716 old_count = path->pathdata.Count;
718 path->pathdata.Points[old_count].X = x1;
719 path->pathdata.Points[old_count].Y = y1;
720 path->pathdata.Points[old_count + 1].X = x2;
721 path->pathdata.Points[old_count + 1].Y = y2;
723 path->pathdata.Types[old_count] =
724 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
725 path->pathdata.Types[old_count + 1] = PathPointTypeLine;
727 path->newfigure = FALSE;
728 path->pathdata.Count += 2;
730 return Ok;
733 /*************************************************************************
734 * GdipAddPathLineI [GDIPLUS.21]
736 * See GdipAddPathLine
738 GpStatus WINGDIPAPI GdipAddPathLineI(GpPath *path, INT x1, INT y1, INT x2, INT y2)
740 TRACE("(%p, %d, %d, %d, %d)\n", path, x1, y1, x2, y2);
742 return GdipAddPathLine(path, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
745 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
746 BOOL connect)
748 INT old_count, count;
750 TRACE("(%p, %p, %d)\n", path, addingPath, connect);
752 if(!path || !addingPath)
753 return InvalidParameter;
755 old_count = path->pathdata.Count;
756 count = addingPath->pathdata.Count;
758 if(!lengthen_path(path, count))
759 return OutOfMemory;
761 memcpy(&path->pathdata.Points[old_count], addingPath->pathdata.Points,
762 count * sizeof(GpPointF));
763 memcpy(&path->pathdata.Types[old_count], addingPath->pathdata.Types, count);
765 if(path->newfigure || !connect)
766 path->pathdata.Types[old_count] = PathPointTypeStart;
767 else
768 path->pathdata.Types[old_count] = PathPointTypeLine;
770 path->newfigure = FALSE;
771 path->pathdata.Count += count;
773 return Ok;
776 GpStatus WINGDIPAPI GdipAddPathPie(GpPath *path, REAL x, REAL y, REAL width, REAL height,
777 REAL startAngle, REAL sweepAngle)
779 GpPointF *ptf;
780 GpStatus status;
781 INT i, count;
783 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
784 path, x, y, width, height, startAngle, sweepAngle);
786 if(!path)
787 return InvalidParameter;
789 /* on zero width/height only start point added */
790 if(width <= 1e-7 || height <= 1e-7){
791 if(!lengthen_path(path, 1))
792 return OutOfMemory;
793 path->pathdata.Points[0].X = x + width / 2.0;
794 path->pathdata.Points[0].Y = y + height / 2.0;
795 path->pathdata.Types[0] = PathPointTypeStart | PathPointTypeCloseSubpath;
796 path->pathdata.Count = 1;
797 return InvalidParameter;
800 count = arc2polybezier(NULL, x, y, width, height, startAngle, sweepAngle);
802 if(count == 0)
803 return Ok;
805 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
806 if(!ptf)
807 return OutOfMemory;
809 arc2polybezier(ptf, x, y, width, height, startAngle, sweepAngle);
811 status = GdipAddPathLine(path, x + width/2, y + height/2, ptf[0].X, ptf[0].Y);
812 if(status != Ok){
813 heap_free(ptf);
814 return status;
816 /* one spline is already added as a line endpoint */
817 if(!lengthen_path(path, count - 1)){
818 heap_free(ptf);
819 return OutOfMemory;
822 memcpy(&(path->pathdata.Points[path->pathdata.Count]), &(ptf[1]),sizeof(GpPointF)*(count-1));
823 for(i = 0; i < count-1; i++)
824 path->pathdata.Types[path->pathdata.Count+i] = PathPointTypeBezier;
826 path->pathdata.Count += count-1;
828 GdipClosePathFigure(path);
830 heap_free(ptf);
832 return status;
835 GpStatus WINGDIPAPI GdipAddPathPieI(GpPath *path, INT x, INT y, INT width, INT height,
836 REAL startAngle, REAL sweepAngle)
838 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
839 path, x, y, width, height, startAngle, sweepAngle);
841 return GdipAddPathPie(path, (REAL)x, (REAL)y, (REAL)width, (REAL)height, startAngle, sweepAngle);
844 GpStatus WINGDIPAPI GdipAddPathPolygon(GpPath *path, GDIPCONST GpPointF *points, INT count)
846 INT old_count;
848 TRACE("(%p, %p, %d)\n", path, points, count);
850 if(!path || !points || count < 3)
851 return InvalidParameter;
853 if(!lengthen_path(path, count))
854 return OutOfMemory;
856 old_count = path->pathdata.Count;
858 memcpy(&path->pathdata.Points[old_count], points, count*sizeof(GpPointF));
859 memset(&path->pathdata.Types[old_count + 1], PathPointTypeLine, count - 1);
861 /* A polygon is an intrinsic figure */
862 path->pathdata.Types[old_count] = PathPointTypeStart;
863 path->pathdata.Types[old_count + count - 1] |= PathPointTypeCloseSubpath;
864 path->newfigure = TRUE;
865 path->pathdata.Count += count;
867 return Ok;
870 GpStatus WINGDIPAPI GdipAddPathPolygonI(GpPath *path, GDIPCONST GpPoint *points, INT count)
872 GpPointF *ptf;
873 GpStatus status;
874 INT i;
876 TRACE("(%p, %p, %d)\n", path, points, count);
878 if(!points || count < 3)
879 return InvalidParameter;
881 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
882 if(!ptf)
883 return OutOfMemory;
885 for(i = 0; i < count; i++){
886 ptf[i].X = (REAL)points[i].X;
887 ptf[i].Y = (REAL)points[i].Y;
890 status = GdipAddPathPolygon(path, ptf, count);
892 heap_free(ptf);
894 return status;
897 static float fromfixedpoint(const FIXED v)
899 float f = ((float)v.fract) / (1<<(sizeof(v.fract)*8));
900 f += v.value;
901 return f;
904 struct format_string_args
906 GpPath *path;
907 float maxY;
908 float scale;
909 float ascent;
912 static GpStatus format_string_callback(HDC dc,
913 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
914 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
915 INT lineno, const RectF *bounds, INT *underlined_indexes,
916 INT underlined_index_count, void *priv)
918 static const MAT2 identity = { {0,1}, {0,0}, {0,0}, {0,1} };
919 struct format_string_args *args = priv;
920 GpPath *path = args->path;
921 GpStatus status = Ok;
922 float x = rect->X + (bounds->X - rect->X) * args->scale;
923 float y = rect->Y + (bounds->Y - rect->Y) * args->scale;
924 int i;
926 if (underlined_index_count)
927 FIXME("hotkey underlines not drawn yet\n");
929 if (y + bounds->Height * args->scale > args->maxY)
930 args->maxY = y + bounds->Height * args->scale;
932 for (i = index; i < length; ++i)
934 GLYPHMETRICS gm;
935 TTPOLYGONHEADER *ph = NULL, *origph;
936 char *start;
937 DWORD len, ofs = 0;
938 len = GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, 0, NULL, &identity);
939 if (len == GDI_ERROR)
941 status = GenericError;
942 break;
944 origph = ph = heap_alloc_zero(len);
945 start = (char *)ph;
946 if (!ph || !lengthen_path(path, len / sizeof(POINTFX)))
948 heap_free(ph);
949 status = OutOfMemory;
950 break;
952 GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, len, start, &identity);
954 ofs = 0;
955 while (ofs < len)
957 DWORD ofs_start = ofs;
958 ph = (TTPOLYGONHEADER*)&start[ofs];
959 path->pathdata.Types[path->pathdata.Count] = PathPointTypeStart;
960 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(ph->pfxStart.x) * args->scale;
961 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(ph->pfxStart.y) * args->scale;
962 TRACE("Starting at count %i with pos %f, %f)\n", path->pathdata.Count, x, y);
963 ofs += sizeof(*ph);
964 while (ofs - ofs_start < ph->cb)
966 TTPOLYCURVE *curve = (TTPOLYCURVE*)&start[ofs];
967 int j;
968 ofs += sizeof(TTPOLYCURVE) + (curve->cpfx - 1) * sizeof(POINTFX);
970 switch (curve->wType)
972 case TT_PRIM_LINE:
973 for (j = 0; j < curve->cpfx; ++j)
975 path->pathdata.Types[path->pathdata.Count] = PathPointTypeLine;
976 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
977 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
979 break;
980 case TT_PRIM_CSPLINE:
981 for (j = 0; j < curve->cpfx; ++j)
983 path->pathdata.Types[path->pathdata.Count] = PathPointTypeBezier;
984 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
985 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
987 break;
988 default:
989 ERR("Unhandled type: %u\n", curve->wType);
990 status = GenericError;
993 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
995 path->newfigure = TRUE;
996 x += gm.gmCellIncX * args->scale;
997 y += gm.gmCellIncY * args->scale;
999 heap_free(origph);
1000 if (status != Ok)
1001 break;
1004 return status;
1007 GpStatus WINGDIPAPI GdipAddPathString(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat* format)
1009 GpFont *font;
1010 GpStatus status;
1011 LOGFONTW lfw;
1012 HANDLE hfont;
1013 HDC dc;
1014 GpGraphics *graphics;
1015 GpPath *backup;
1016 struct format_string_args args;
1017 int i;
1018 UINT16 native_height;
1019 RectF scaled_layout_rect;
1020 TEXTMETRICW textmetric;
1022 TRACE("(%p, %s, %d, %p, %d, %f, %p, %p)\n", path, debugstr_w(string), length, family, style, emSize, layoutRect, format);
1023 if (!path || !string || !family || !emSize || !layoutRect || !format)
1024 return InvalidParameter;
1026 status = GdipGetEmHeight(family, style, &native_height);
1027 if (status != Ok)
1028 return status;
1030 scaled_layout_rect.X = layoutRect->X;
1031 scaled_layout_rect.Y = layoutRect->Y;
1032 scaled_layout_rect.Width = layoutRect->Width * native_height / emSize;
1033 scaled_layout_rect.Height = layoutRect->Height * native_height / emSize;
1035 if ((status = GdipClonePath(path, &backup)) != Ok)
1036 return status;
1038 dc = CreateCompatibleDC(0);
1039 status = GdipCreateFromHDC(dc, &graphics);
1040 if (status != Ok)
1042 DeleteDC(dc);
1043 GdipDeletePath(backup);
1044 return status;
1047 status = GdipCreateFont(family, native_height, style, UnitPixel, &font);
1048 if (status != Ok)
1050 GdipDeleteGraphics(graphics);
1051 DeleteDC(dc);
1052 GdipDeletePath(backup);
1053 return status;
1056 get_log_fontW(font, graphics, &lfw);
1057 GdipDeleteFont(font);
1058 GdipDeleteGraphics(graphics);
1060 hfont = CreateFontIndirectW(&lfw);
1061 if (!hfont)
1063 WARN("Failed to create font\n");
1064 DeleteDC(dc);
1065 GdipDeletePath(backup);
1066 return GenericError;
1069 SelectObject(dc, hfont);
1071 GetTextMetricsW(dc, &textmetric);
1073 args.path = path;
1074 args.maxY = 0;
1075 args.scale = emSize / native_height;
1076 args.ascent = textmetric.tmAscent * args.scale;
1077 status = gdip_format_string(dc, string, length, NULL, &scaled_layout_rect,
1078 format, TRUE, format_string_callback, &args);
1080 DeleteDC(dc);
1081 DeleteObject(hfont);
1083 if (status != Ok) /* free backup */
1085 heap_free(path->pathdata.Points);
1086 heap_free(path->pathdata.Types);
1087 *path = *backup;
1088 heap_free(backup);
1089 return status;
1091 if (format && format->line_align == StringAlignmentCenter && layoutRect->Y + args.maxY < layoutRect->Height)
1093 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1094 inc /= 2;
1095 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1096 path->pathdata.Points[i].Y += inc;
1097 } else if (format && format->line_align == StringAlignmentFar) {
1098 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1099 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1100 path->pathdata.Points[i].Y += inc;
1102 GdipDeletePath(backup);
1103 return status;
1106 GpStatus WINGDIPAPI GdipAddPathStringI(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1108 if (layoutRect)
1110 RectF layoutRectF = {
1111 (REAL)layoutRect->X,
1112 (REAL)layoutRect->Y,
1113 (REAL)layoutRect->Width,
1114 (REAL)layoutRect->Height
1116 return GdipAddPathString(path, string, length, family, style, emSize, &layoutRectF, format);
1118 return InvalidParameter;
1121 /*************************************************************************
1122 * GdipClonePath [GDIPLUS.53]
1124 * Duplicate the given path in memory.
1126 * PARAMS
1127 * path [I] The path to be duplicated
1128 * clone [O] Pointer to the new path
1130 * RETURNS
1131 * InvalidParameter If the input path is invalid
1132 * OutOfMemory If allocation of needed memory fails
1133 * Ok If everything works out as expected
1135 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1137 TRACE("(%p, %p)\n", path, clone);
1139 if(!path || !clone)
1140 return InvalidParameter;
1142 *clone = heap_alloc_zero(sizeof(GpPath));
1143 if(!*clone) return OutOfMemory;
1145 **clone = *path;
1147 (*clone)->pathdata.Points = heap_alloc_zero(path->datalen * sizeof(PointF));
1148 (*clone)->pathdata.Types = heap_alloc_zero(path->datalen);
1149 if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1150 heap_free((*clone)->pathdata.Points);
1151 heap_free((*clone)->pathdata.Types);
1152 heap_free(*clone);
1153 return OutOfMemory;
1156 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1157 path->datalen * sizeof(PointF));
1158 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1160 return Ok;
1163 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1165 TRACE("(%p)\n", path);
1167 if(!path)
1168 return InvalidParameter;
1170 if(path->pathdata.Count > 0){
1171 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1172 path->newfigure = TRUE;
1175 return Ok;
1178 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1180 INT i;
1182 TRACE("(%p)\n", path);
1184 if(!path)
1185 return InvalidParameter;
1187 for(i = 1; i < path->pathdata.Count; i++){
1188 if(path->pathdata.Types[i] == PathPointTypeStart)
1189 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1192 path->newfigure = TRUE;
1194 return Ok;
1197 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1199 TRACE("(%d, %p)\n", fill, path);
1201 if(!path)
1202 return InvalidParameter;
1204 *path = heap_alloc_zero(sizeof(GpPath));
1205 if(!*path) return OutOfMemory;
1207 (*path)->fill = fill;
1208 (*path)->newfigure = TRUE;
1210 return Ok;
1213 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1214 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1216 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1218 if(!path)
1219 return InvalidParameter;
1221 *path = heap_alloc_zero(sizeof(GpPath));
1222 if(!*path) return OutOfMemory;
1224 (*path)->pathdata.Points = heap_alloc_zero(count * sizeof(PointF));
1225 (*path)->pathdata.Types = heap_alloc_zero(count);
1227 if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
1228 heap_free((*path)->pathdata.Points);
1229 heap_free((*path)->pathdata.Types);
1230 heap_free(*path);
1231 return OutOfMemory;
1234 memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
1235 memcpy((*path)->pathdata.Types, types, count);
1236 (*path)->pathdata.Count = count;
1237 (*path)->datalen = count;
1239 (*path)->fill = fill;
1240 (*path)->newfigure = TRUE;
1242 return Ok;
1245 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1246 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1248 GpPointF *ptF;
1249 GpStatus ret;
1250 INT i;
1252 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1254 ptF = heap_alloc_zero(sizeof(GpPointF)*count);
1256 for(i = 0;i < count; i++){
1257 ptF[i].X = (REAL)points[i].X;
1258 ptF[i].Y = (REAL)points[i].Y;
1261 ret = GdipCreatePath2(ptF, types, count, fill, path);
1263 heap_free(ptF);
1265 return ret;
1268 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1270 TRACE("(%p)\n", path);
1272 if(!path)
1273 return InvalidParameter;
1275 heap_free(path->pathdata.Points);
1276 heap_free(path->pathdata.Types);
1277 heap_free(path);
1279 return Ok;
1282 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1284 path_list_node_t *list, *node;
1285 GpPointF pt;
1286 INT i = 1;
1287 INT startidx = 0;
1288 GpStatus stat;
1290 TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1292 if(!path)
1293 return InvalidParameter;
1295 if(path->pathdata.Count == 0)
1296 return Ok;
1298 stat = GdipTransformPath(path, matrix);
1299 if(stat != Ok)
1300 return stat;
1302 pt = path->pathdata.Points[0];
1303 if(!init_path_list(&list, pt.X, pt.Y))
1304 return OutOfMemory;
1306 node = list;
1308 while(i < path->pathdata.Count){
1310 BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1311 path_list_node_t *start;
1313 pt = path->pathdata.Points[i];
1315 /* save last start point index */
1316 if(type == PathPointTypeStart)
1317 startidx = i;
1319 /* always add line points and start points */
1320 if((type == PathPointTypeStart) || (type == PathPointTypeLine)){
1321 if(!add_path_list_node(node, pt.X, pt.Y, path->pathdata.Types[i]))
1322 goto memout;
1324 node = node->next;
1325 ++i;
1326 continue;
1329 /* Bezier curve */
1331 /* test for closed figure */
1332 if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1333 pt = path->pathdata.Points[startidx];
1334 ++i;
1336 else
1338 i += 2;
1339 pt = path->pathdata.Points[i];
1342 start = node;
1343 /* add Bezier end point */
1344 type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1345 if(!add_path_list_node(node, pt.X, pt.Y, type))
1346 goto memout;
1347 node = node->next;
1349 /* flatten curve */
1350 if(!flatten_bezier(start, path->pathdata.Points[i-2].X, path->pathdata.Points[i-2].Y,
1351 path->pathdata.Points[i-1].X, path->pathdata.Points[i-1].Y,
1352 node, flatness))
1353 goto memout;
1355 ++i;
1356 }/* while */
1358 /* store path data back */
1359 i = path_list_count(list);
1360 if(!lengthen_path(path, i))
1361 goto memout;
1362 path->pathdata.Count = i;
1364 node = list;
1365 for(i = 0; i < path->pathdata.Count; i++){
1366 path->pathdata.Points[i] = node->pt;
1367 path->pathdata.Types[i] = node->type;
1368 node = node->next;
1371 free_path_list(list);
1372 return Ok;
1374 memout:
1375 free_path_list(list);
1376 return OutOfMemory;
1379 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1381 TRACE("(%p, %p)\n", path, pathData);
1383 if(!path || !pathData)
1384 return InvalidParameter;
1386 /* Only copy data. pathData allocation/freeing controlled by wrapper class.
1387 Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
1388 memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
1389 memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
1391 return Ok;
1394 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1396 TRACE("(%p, %p)\n", path, fillmode);
1398 if(!path || !fillmode)
1399 return InvalidParameter;
1401 *fillmode = path->fill;
1403 return Ok;
1406 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1408 INT count;
1410 TRACE("(%p, %p)\n", path, lastPoint);
1412 if(!path || !lastPoint)
1413 return InvalidParameter;
1415 count = path->pathdata.Count;
1416 if(count > 0)
1417 *lastPoint = path->pathdata.Points[count-1];
1419 return Ok;
1422 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1424 TRACE("(%p, %p, %d)\n", path, points, count);
1426 if(!path)
1427 return InvalidParameter;
1429 if(count < path->pathdata.Count)
1430 return InsufficientBuffer;
1432 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1434 return Ok;
1437 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1439 GpStatus ret;
1440 GpPointF *ptf;
1441 INT i;
1443 TRACE("(%p, %p, %d)\n", path, points, count);
1445 if(count <= 0)
1446 return InvalidParameter;
1448 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
1449 if(!ptf) return OutOfMemory;
1451 ret = GdipGetPathPoints(path,ptf,count);
1452 if(ret == Ok)
1453 for(i = 0;i < count;i++){
1454 points[i].X = gdip_round(ptf[i].X);
1455 points[i].Y = gdip_round(ptf[i].Y);
1457 heap_free(ptf);
1459 return ret;
1462 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1464 TRACE("(%p, %p, %d)\n", path, types, count);
1466 if(!path)
1467 return InvalidParameter;
1469 if(count < path->pathdata.Count)
1470 return InsufficientBuffer;
1472 memcpy(types, path->pathdata.Types, path->pathdata.Count);
1474 return Ok;
1477 /* Windows expands the bounding box to the maximum possible bounding box
1478 * for a given pen. For example, if a line join can extend past the point
1479 * it's joining by x units, the bounding box is extended by x units in every
1480 * direction (even though this is too conservative for most cases). */
1481 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
1482 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1484 GpPointF * points, temp_pts[4];
1485 INT count, i;
1486 REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
1488 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1490 /* Matrix and pen can be null. */
1491 if(!path || !bounds)
1492 return InvalidParameter;
1494 /* If path is empty just return. */
1495 count = path->pathdata.Count;
1496 if(count == 0){
1497 bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
1498 return Ok;
1501 points = path->pathdata.Points;
1503 low_x = high_x = points[0].X;
1504 low_y = high_y = points[0].Y;
1506 for(i = 1; i < count; i++){
1507 low_x = min(low_x, points[i].X);
1508 low_y = min(low_y, points[i].Y);
1509 high_x = max(high_x, points[i].X);
1510 high_y = max(high_y, points[i].Y);
1513 width = high_x - low_x;
1514 height = high_y - low_y;
1516 /* This looks unusual but it's the only way I can imitate windows. */
1517 if(matrix){
1518 temp_pts[0].X = low_x;
1519 temp_pts[0].Y = low_y;
1520 temp_pts[1].X = low_x;
1521 temp_pts[1].Y = high_y;
1522 temp_pts[2].X = high_x;
1523 temp_pts[2].Y = high_y;
1524 temp_pts[3].X = high_x;
1525 temp_pts[3].Y = low_y;
1527 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1528 low_x = temp_pts[0].X;
1529 low_y = temp_pts[0].Y;
1531 for(i = 1; i < 4; i++){
1532 low_x = min(low_x, temp_pts[i].X);
1533 low_y = min(low_y, temp_pts[i].Y);
1536 temp = width;
1537 width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
1538 height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
1541 if(pen){
1542 path_width = pen->width / 2.0;
1544 if(count > 2)
1545 path_width = max(path_width, pen->width * pen->miterlimit / 2.0);
1546 /* FIXME: this should probably also check for the startcap */
1547 if(pen->endcap & LineCapNoAnchor)
1548 path_width = max(path_width, pen->width * 2.2);
1550 low_x -= path_width;
1551 low_y -= path_width;
1552 width += 2.0 * path_width;
1553 height += 2.0 * path_width;
1556 bounds->X = low_x;
1557 bounds->Y = low_y;
1558 bounds->Width = width;
1559 bounds->Height = height;
1561 return Ok;
1564 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1565 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1567 GpStatus ret;
1568 GpRectF boundsF;
1570 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1572 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
1574 if(ret == Ok){
1575 bounds->X = gdip_round(boundsF.X);
1576 bounds->Y = gdip_round(boundsF.Y);
1577 bounds->Width = gdip_round(boundsF.Width);
1578 bounds->Height = gdip_round(boundsF.Height);
1581 return ret;
1584 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1586 TRACE("(%p, %p)\n", path, count);
1588 if(!path)
1589 return InvalidParameter;
1591 *count = path->pathdata.Count;
1593 return Ok;
1596 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1598 INT i, count;
1599 INT start = 0; /* position in reversed path */
1600 GpPathData revpath;
1602 TRACE("(%p)\n", path);
1604 if(!path)
1605 return InvalidParameter;
1607 count = path->pathdata.Count;
1609 if(count == 0) return Ok;
1611 revpath.Points = heap_alloc_zero(sizeof(GpPointF)*count);
1612 revpath.Types = heap_alloc_zero(sizeof(BYTE)*count);
1613 revpath.Count = count;
1614 if(!revpath.Points || !revpath.Types){
1615 heap_free(revpath.Points);
1616 heap_free(revpath.Types);
1617 return OutOfMemory;
1620 for(i = 0; i < count; i++){
1622 /* find next start point */
1623 if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1624 INT j;
1625 for(j = start; j <= i; j++){
1626 revpath.Points[j] = path->pathdata.Points[count-j-1];
1627 revpath.Types[j] = path->pathdata.Types[count-j-1];
1629 /* mark start point */
1630 revpath.Types[start] = PathPointTypeStart;
1631 /* set 'figure' endpoint type */
1632 if(i-start > 1){
1633 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1634 revpath.Types[i] |= revpath.Types[i-1];
1636 else
1637 revpath.Types[i] = path->pathdata.Types[start];
1639 start = i+1;
1643 memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1644 memcpy(path->pathdata.Types, revpath.Types, sizeof(BYTE)*count);
1646 heap_free(revpath.Points);
1647 heap_free(revpath.Types);
1649 return Ok;
1652 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1653 GpPen *pen, GpGraphics *graphics, BOOL *result)
1655 TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1657 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1660 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1661 GpPen *pen, GpGraphics *graphics, BOOL *result)
1663 GpStatus stat;
1664 GpPath *wide_path;
1665 GpMatrix *transform = NULL;
1667 TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1669 if(!path || !pen)
1670 return InvalidParameter;
1672 stat = GdipClonePath(path, &wide_path);
1674 if (stat != Ok)
1675 return stat;
1677 if (pen->unit == UnitPixel && graphics != NULL)
1679 stat = GdipCreateMatrix(&transform);
1681 if (stat == Ok)
1682 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1683 CoordinateSpaceWorld, transform);
1686 if (stat == Ok)
1687 stat = GdipWidenPath(wide_path, pen, transform, 1.0);
1689 if (pen->unit == UnitPixel && graphics != NULL)
1691 if (stat == Ok)
1692 stat = GdipInvertMatrix(transform);
1694 if (stat == Ok)
1695 stat = GdipTransformPath(wide_path, transform);
1698 if (stat == Ok)
1699 stat = GdipIsVisiblePathPoint(wide_path, x, y, graphics, result);
1701 GdipDeleteMatrix(transform);
1703 GdipDeletePath(wide_path);
1705 return stat;
1708 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1710 TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1712 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1715 /*****************************************************************************
1716 * GdipIsVisiblePathPoint [GDIPLUS.@]
1718 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1720 GpRegion *region;
1721 HRGN hrgn;
1722 GpStatus status;
1724 if(!path || !result) return InvalidParameter;
1726 status = GdipCreateRegionPath(path, &region);
1727 if(status != Ok)
1728 return status;
1730 status = GdipGetRegionHRgn(region, graphics, &hrgn);
1731 if(status != Ok){
1732 GdipDeleteRegion(region);
1733 return status;
1736 *result = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1738 DeleteObject(hrgn);
1739 GdipDeleteRegion(region);
1741 return Ok;
1744 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1746 TRACE("(%p)\n", path);
1748 if(!path)
1749 return InvalidParameter;
1751 path->newfigure = TRUE;
1753 return Ok;
1756 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1758 TRACE("(%p)\n", path);
1760 if(!path)
1761 return InvalidParameter;
1763 path->pathdata.Count = 0;
1764 path->newfigure = TRUE;
1765 path->fill = FillModeAlternate;
1767 return Ok;
1770 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1772 TRACE("(%p, %d)\n", path, fill);
1774 if(!path)
1775 return InvalidParameter;
1777 path->fill = fill;
1779 return Ok;
1782 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1784 TRACE("(%p, %p)\n", path, matrix);
1786 if(!path)
1787 return InvalidParameter;
1789 if(path->pathdata.Count == 0 || !matrix)
1790 return Ok;
1792 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1793 path->pathdata.Count);
1796 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1797 GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1798 REAL height, WarpMode warpmode, REAL flatness)
1800 FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1801 points, count, x, y, width, height, warpmode, flatness);
1803 return NotImplemented;
1806 static void add_bevel_point(const GpPointF *endpoint, const GpPointF *nextpoint,
1807 GpPen *pen, int right_side, path_list_node_t **last_point)
1809 REAL segment_dy = nextpoint->Y-endpoint->Y;
1810 REAL segment_dx = nextpoint->X-endpoint->X;
1811 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1812 REAL distance = pen->width/2.0;
1813 REAL bevel_dx, bevel_dy;
1815 if (segment_length == 0.0)
1817 *last_point = add_path_list_node(*last_point, endpoint->X,
1818 endpoint->Y, PathPointTypeLine);
1819 return;
1822 if (right_side)
1824 bevel_dx = -distance * segment_dy / segment_length;
1825 bevel_dy = distance * segment_dx / segment_length;
1827 else
1829 bevel_dx = distance * segment_dy / segment_length;
1830 bevel_dy = -distance * segment_dx / segment_length;
1833 *last_point = add_path_list_node(*last_point, endpoint->X + bevel_dx,
1834 endpoint->Y + bevel_dy, PathPointTypeLine);
1837 static void widen_joint(const GpPointF *p1, const GpPointF *p2, const GpPointF *p3,
1838 GpPen* pen, path_list_node_t **last_point)
1840 switch (pen->join)
1842 case LineJoinMiter:
1843 case LineJoinMiterClipped:
1844 if ((p2->X - p1->X) * (p3->Y - p1->Y) > (p2->Y - p1->Y) * (p3->X - p1->X))
1846 float distance = pen->width/2.0;
1847 float length_0 = sqrtf((p2->X-p1->X)*(p2->X-p1->X)+(p2->Y-p1->Y)*(p2->Y-p1->Y));
1848 float length_1 = sqrtf((p3->X-p2->X)*(p3->X-p2->X)+(p3->Y-p2->Y)*(p3->Y-p2->Y));
1849 float dx0 = distance * (p2->X - p1->X) / length_0;
1850 float dy0 = distance * (p2->Y - p1->Y) / length_0;
1851 float dx1 = distance * (p3->X - p2->X) / length_1;
1852 float dy1 = distance * (p3->Y - p2->Y) / length_1;
1853 float det = (dy0*dx1 - dx0*dy1);
1854 float dx = (dx0*dx1*(dx0-dx1) + dy0*dy0*dx1 - dy1*dy1*dx0)/det;
1855 float dy = (dy0*dy1*(dy0-dy1) + dx0*dx0*dy1 - dx1*dx1*dy0)/det;
1856 if (dx*dx + dy*dy < pen->miterlimit*pen->miterlimit * distance*distance)
1858 *last_point = add_path_list_node(*last_point, p2->X + dx,
1859 p2->Y + dy, PathPointTypeLine);
1860 break;
1862 else if (pen->join == LineJoinMiter)
1864 static int once;
1865 if (!once++)
1866 FIXME("should add a clipped corner\n");
1868 /* else fall-through */
1870 /* else fall-through */
1871 default:
1872 case LineJoinBevel:
1873 add_bevel_point(p2, p1, pen, 1, last_point);
1874 add_bevel_point(p2, p3, pen, 0, last_point);
1875 break;
1879 static void widen_cap(const GpPointF *endpoint, const GpPointF *nextpoint,
1880 GpPen *pen, GpLineCap cap, GpCustomLineCap *custom, int add_first_points,
1881 int add_last_point, path_list_node_t **last_point)
1883 switch (cap)
1885 default:
1886 case LineCapFlat:
1887 if (add_first_points)
1888 add_bevel_point(endpoint, nextpoint, pen, 1, last_point);
1889 if (add_last_point)
1890 add_bevel_point(endpoint, nextpoint, pen, 0, last_point);
1891 break;
1892 case LineCapSquare:
1894 REAL segment_dy = nextpoint->Y-endpoint->Y;
1895 REAL segment_dx = nextpoint->X-endpoint->X;
1896 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1897 REAL distance = pen->width/2.0;
1898 REAL bevel_dx, bevel_dy;
1899 REAL extend_dx, extend_dy;
1901 extend_dx = -distance * segment_dx / segment_length;
1902 extend_dy = -distance * segment_dy / segment_length;
1904 bevel_dx = -distance * segment_dy / segment_length;
1905 bevel_dy = distance * segment_dx / segment_length;
1907 if (add_first_points)
1908 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx + bevel_dx,
1909 endpoint->Y + extend_dy + bevel_dy, PathPointTypeLine);
1911 if (add_last_point)
1912 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx - bevel_dx,
1913 endpoint->Y + extend_dy - bevel_dy, PathPointTypeLine);
1915 break;
1917 case LineCapRound:
1919 REAL segment_dy = nextpoint->Y-endpoint->Y;
1920 REAL segment_dx = nextpoint->X-endpoint->X;
1921 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1922 REAL distance = pen->width/2.0;
1923 REAL dx, dy, dx2, dy2;
1924 const REAL control_point_distance = 0.5522847498307935; /* 4/3 * (sqrt(2) - 1) */
1926 if (add_first_points)
1928 dx = -distance * segment_dx / segment_length;
1929 dy = -distance * segment_dy / segment_length;
1931 dx2 = dx * control_point_distance;
1932 dy2 = dy * control_point_distance;
1934 /* first 90-degree arc */
1935 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
1936 endpoint->Y - dx, PathPointTypeLine);
1938 *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
1939 endpoint->Y - dx + dy2, PathPointTypeBezier);
1941 *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
1942 endpoint->Y + dy - dx2, PathPointTypeBezier);
1944 /* midpoint */
1945 *last_point = add_path_list_node(*last_point, endpoint->X + dx,
1946 endpoint->Y + dy, PathPointTypeBezier);
1948 /* second 90-degree arc */
1949 *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
1950 endpoint->Y + dy + dx2, PathPointTypeBezier);
1952 *last_point = add_path_list_node(*last_point, endpoint->X - dy + dx2,
1953 endpoint->Y + dx + dy2, PathPointTypeBezier);
1955 *last_point = add_path_list_node(*last_point, endpoint->X - dy,
1956 endpoint->Y + dx, PathPointTypeBezier);
1958 break;
1960 case LineCapTriangle:
1962 REAL segment_dy = nextpoint->Y-endpoint->Y;
1963 REAL segment_dx = nextpoint->X-endpoint->X;
1964 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1965 REAL distance = pen->width/2.0;
1966 REAL dx, dy;
1968 dx = distance * segment_dx / segment_length;
1969 dy = distance * segment_dy / segment_length;
1971 if (add_first_points) {
1972 add_bevel_point(endpoint, nextpoint, pen, 1, last_point);
1974 *last_point = add_path_list_node(*last_point, endpoint->X - dx,
1975 endpoint->Y - dy, PathPointTypeLine);
1977 if (add_last_point)
1978 add_bevel_point(endpoint, nextpoint, pen, 0, last_point);
1979 break;
1984 static void widen_open_figure(const GpPointF *points, GpPen *pen, int start, int end,
1985 GpLineCap start_cap, GpCustomLineCap *start_custom, GpLineCap end_cap,
1986 GpCustomLineCap *end_custom, path_list_node_t **last_point)
1988 int i;
1989 path_list_node_t *prev_point;
1991 if (end <= start)
1992 return;
1994 prev_point = *last_point;
1996 widen_cap(&points[start], &points[start+1],
1997 pen, start_cap, start_custom, FALSE, TRUE, last_point);
1999 for (i=start+1; i<end; i++)
2000 widen_joint(&points[i-1], &points[i],
2001 &points[i+1], pen, last_point);
2003 widen_cap(&points[end], &points[end-1],
2004 pen, end_cap, end_custom, TRUE, TRUE, last_point);
2006 for (i=end-1; i>start; i--)
2007 widen_joint(&points[i+1], &points[i],
2008 &points[i-1], pen, last_point);
2010 widen_cap(&points[start], &points[start+1],
2011 pen, start_cap, start_custom, TRUE, FALSE, last_point);
2013 prev_point->next->type = PathPointTypeStart;
2014 (*last_point)->type |= PathPointTypeCloseSubpath;
2017 static void widen_closed_figure(GpPath *path, GpPen *pen, int start, int end,
2018 path_list_node_t **last_point)
2020 int i;
2021 path_list_node_t *prev_point;
2023 if (end <= start)
2024 return;
2026 /* left outline */
2027 prev_point = *last_point;
2029 widen_joint(&path->pathdata.Points[end], &path->pathdata.Points[start],
2030 &path->pathdata.Points[start+1], pen, last_point);
2032 for (i=start+1; i<end; i++)
2033 widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
2034 &path->pathdata.Points[i+1], pen, last_point);
2036 widen_joint(&path->pathdata.Points[end-1], &path->pathdata.Points[end],
2037 &path->pathdata.Points[start], pen, last_point);
2039 prev_point->next->type = PathPointTypeStart;
2040 (*last_point)->type |= PathPointTypeCloseSubpath;
2042 /* right outline */
2043 prev_point = *last_point;
2045 widen_joint(&path->pathdata.Points[start], &path->pathdata.Points[end],
2046 &path->pathdata.Points[end-1], pen, last_point);
2048 for (i=end-1; i>start; i--)
2049 widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
2050 &path->pathdata.Points[i-1], pen, last_point);
2052 widen_joint(&path->pathdata.Points[start+1], &path->pathdata.Points[start],
2053 &path->pathdata.Points[end], pen, last_point);
2055 prev_point->next->type = PathPointTypeStart;
2056 (*last_point)->type |= PathPointTypeCloseSubpath;
2059 static void widen_dashed_figure(GpPath *path, GpPen *pen, int start, int end,
2060 int closed, path_list_node_t **last_point)
2062 int i, j;
2063 REAL dash_pos=0.0;
2064 int dash_index=0;
2065 const REAL *dash_pattern;
2066 REAL *dash_pattern_scaled;
2067 int dash_count;
2068 GpPointF *tmp_points;
2069 REAL segment_dy;
2070 REAL segment_dx;
2071 REAL segment_length;
2072 REAL segment_pos;
2073 int num_tmp_points=0;
2074 int draw_start_cap=0;
2075 static const REAL dash_dot_dot[6] = { 3.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
2077 if (end <= start)
2078 return;
2080 switch (pen->dash)
2082 case DashStyleDash:
2083 default:
2084 dash_pattern = dash_dot_dot;
2085 dash_count = 2;
2086 break;
2087 case DashStyleDot:
2088 dash_pattern = &dash_dot_dot[2];
2089 dash_count = 2;
2090 break;
2091 case DashStyleDashDot:
2092 dash_pattern = dash_dot_dot;
2093 dash_count = 4;
2094 break;
2095 case DashStyleDashDotDot:
2096 dash_pattern = dash_dot_dot;
2097 dash_count = 6;
2098 break;
2099 case DashStyleCustom:
2100 dash_pattern = pen->dashes;
2101 dash_count = pen->numdashes;
2102 break;
2105 dash_pattern_scaled = heap_alloc(dash_count * sizeof(REAL));
2106 if (!dash_pattern_scaled) return;
2108 for (i = 0; i < dash_count; i++)
2109 dash_pattern_scaled[i] = pen->width * dash_pattern[i];
2111 tmp_points = heap_alloc_zero((end - start + 2) * sizeof(GpPoint));
2112 if (!tmp_points) {
2113 heap_free(dash_pattern_scaled);
2114 return; /* FIXME */
2117 if (!closed)
2118 draw_start_cap = 1;
2120 for (j=start; j <= end; j++)
2122 if (j == start)
2124 if (closed)
2125 i = end;
2126 else
2127 continue;
2129 else
2130 i = j-1;
2132 segment_dy = path->pathdata.Points[j].Y - path->pathdata.Points[i].Y;
2133 segment_dx = path->pathdata.Points[j].X - path->pathdata.Points[i].X;
2134 segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2135 segment_pos = 0.0;
2137 while (1)
2139 if (dash_pos == 0.0)
2141 if ((dash_index % 2) == 0)
2143 /* start dash */
2144 num_tmp_points = 1;
2145 tmp_points[0].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2146 tmp_points[0].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2148 else
2150 /* end dash */
2151 tmp_points[num_tmp_points].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2152 tmp_points[num_tmp_points].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2154 widen_open_figure(tmp_points, pen, 0, num_tmp_points,
2155 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2156 LineCapFlat, NULL, last_point);
2157 draw_start_cap = 0;
2158 num_tmp_points = 0;
2162 if (dash_pattern_scaled[dash_index] - dash_pos > segment_length - segment_pos)
2164 /* advance to next segment */
2165 if ((dash_index % 2) == 0)
2167 tmp_points[num_tmp_points] = path->pathdata.Points[j];
2168 num_tmp_points++;
2170 dash_pos += segment_length - segment_pos;
2171 break;
2173 else
2175 /* advance to next dash in pattern */
2176 segment_pos += dash_pattern_scaled[dash_index] - dash_pos;
2177 dash_pos = 0.0;
2178 if (++dash_index == dash_count)
2179 dash_index = 0;
2180 continue;
2185 if (dash_index % 2 == 0 && num_tmp_points != 0)
2187 /* last dash overflows last segment */
2188 widen_open_figure(tmp_points, pen, 0, num_tmp_points-1,
2189 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2190 closed ? LineCapFlat : pen->endcap, pen->customend, last_point);
2193 heap_free(dash_pattern_scaled);
2194 heap_free(tmp_points);
2197 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
2198 REAL flatness)
2200 GpPath *flat_path=NULL;
2201 GpStatus status;
2202 path_list_node_t *points=NULL, *last_point=NULL;
2203 int i, subpath_start=0, new_length;
2204 BYTE type;
2206 TRACE("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
2208 if (!path || !pen)
2209 return InvalidParameter;
2211 if (path->pathdata.Count <= 1)
2212 return OutOfMemory;
2214 status = GdipClonePath(path, &flat_path);
2216 if (status == Ok)
2217 status = GdipFlattenPath(flat_path, pen->unit == UnitPixel ? matrix : NULL, flatness);
2219 if (status == Ok && !init_path_list(&points, 314.0, 22.0))
2220 status = OutOfMemory;
2222 if (status == Ok)
2224 last_point = points;
2226 if (pen->endcap > LineCapTriangle)
2227 FIXME("unimplemented end cap %x\n", pen->endcap);
2229 if (pen->startcap > LineCapTriangle)
2230 FIXME("unimplemented start cap %x\n", pen->startcap);
2232 if (pen->dashcap != DashCapFlat)
2233 FIXME("unimplemented dash cap %d\n", pen->dashcap);
2235 if (pen->join == LineJoinRound)
2236 FIXME("unimplemented line join %d\n", pen->join);
2238 if (pen->align != PenAlignmentCenter)
2239 FIXME("unimplemented pen alignment %d\n", pen->align);
2241 for (i=0; i < flat_path->pathdata.Count; i++)
2243 type = flat_path->pathdata.Types[i];
2245 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
2246 subpath_start = i;
2248 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
2250 if (pen->dash != DashStyleSolid)
2251 widen_dashed_figure(flat_path, pen, subpath_start, i, 1, &last_point);
2252 else
2253 widen_closed_figure(flat_path, pen, subpath_start, i, &last_point);
2255 else if (i == flat_path->pathdata.Count-1 ||
2256 (flat_path->pathdata.Types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
2258 if (pen->dash != DashStyleSolid)
2259 widen_dashed_figure(flat_path, pen, subpath_start, i, 0, &last_point);
2260 else
2261 widen_open_figure(flat_path->pathdata.Points, pen, subpath_start, i, pen->startcap, pen->customstart, pen->endcap, pen->customend, &last_point);
2265 new_length = path_list_count(points)-1;
2267 if (!lengthen_path(path, new_length))
2268 status = OutOfMemory;
2271 if (status == Ok)
2273 path->pathdata.Count = new_length;
2275 last_point = points->next;
2276 for (i = 0; i < new_length; i++)
2278 path->pathdata.Points[i] = last_point->pt;
2279 path->pathdata.Types[i] = last_point->type;
2280 last_point = last_point->next;
2283 path->fill = FillModeWinding;
2286 free_path_list(points);
2288 GdipDeletePath(flat_path);
2290 if (status == Ok && pen->unit != UnitPixel)
2291 status = GdipTransformPath(path, matrix);
2293 return status;
2296 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
2297 REAL width, REAL height)
2299 GpPath *backup;
2300 GpPointF ptf[2];
2301 GpStatus retstat;
2302 BOOL old_new;
2304 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
2306 if(!path)
2307 return InvalidParameter;
2309 /* make a backup copy of path data */
2310 if((retstat = GdipClonePath(path, &backup)) != Ok)
2311 return retstat;
2313 /* rectangle should start as new path */
2314 old_new = path->newfigure;
2315 path->newfigure = TRUE;
2316 if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
2317 path->newfigure = old_new;
2318 goto fail;
2321 ptf[0].X = x+width;
2322 ptf[0].Y = y+height;
2323 ptf[1].X = x;
2324 ptf[1].Y = y+height;
2326 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
2327 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
2329 /* free backup */
2330 GdipDeletePath(backup);
2331 return Ok;
2333 fail:
2334 /* reverting */
2335 heap_free(path->pathdata.Points);
2336 heap_free(path->pathdata.Types);
2337 memcpy(path, backup, sizeof(*path));
2338 heap_free(backup);
2340 return retstat;
2343 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
2344 INT width, INT height)
2346 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
2348 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2351 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
2353 GpPath *backup;
2354 GpStatus retstat;
2355 INT i;
2357 TRACE("(%p, %p, %d)\n", path, rects, count);
2359 /* count == 0 - verified condition */
2360 if(!path || !rects || count == 0)
2361 return InvalidParameter;
2363 if(count < 0)
2364 return OutOfMemory;
2366 /* make a backup copy */
2367 if((retstat = GdipClonePath(path, &backup)) != Ok)
2368 return retstat;
2370 for(i = 0; i < count; i++){
2371 if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
2372 goto fail;
2375 /* free backup */
2376 GdipDeletePath(backup);
2377 return Ok;
2379 fail:
2380 /* reverting */
2381 heap_free(path->pathdata.Points);
2382 heap_free(path->pathdata.Types);
2383 memcpy(path, backup, sizeof(*path));
2384 heap_free(backup);
2386 return retstat;
2389 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
2391 GpRectF *rectsF;
2392 GpStatus retstat;
2393 INT i;
2395 TRACE("(%p, %p, %d)\n", path, rects, count);
2397 if(!rects || count == 0)
2398 return InvalidParameter;
2400 if(count < 0)
2401 return OutOfMemory;
2403 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
2405 for(i = 0;i < count;i++){
2406 rectsF[i].X = (REAL)rects[i].X;
2407 rectsF[i].Y = (REAL)rects[i].Y;
2408 rectsF[i].Width = (REAL)rects[i].Width;
2409 rectsF[i].Height = (REAL)rects[i].Height;
2412 retstat = GdipAddPathRectangles(path, rectsF, count);
2413 heap_free(rectsF);
2415 return retstat;
2418 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
2420 INT count;
2422 TRACE("(%p)\n", path);
2424 if(!path)
2425 return InvalidParameter;
2427 count = path->pathdata.Count;
2429 /* set marker flag */
2430 if(count > 0)
2431 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
2433 return Ok;
2436 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
2438 INT count;
2439 INT i;
2441 TRACE("(%p)\n", path);
2443 if(!path)
2444 return InvalidParameter;
2446 count = path->pathdata.Count;
2448 for(i = 0; i < count - 1; i++){
2449 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
2452 return Ok;
2455 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
2457 FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
2458 return NotImplemented;
2461 #define FLAGS_INTPATH 0x4000
2463 struct path_header
2465 DWORD version;
2466 DWORD count;
2467 DWORD flags;
2470 /* Test to see if the path could be stored as an array of shorts */
2471 static BOOL is_integer_path(const GpPath *path)
2473 int i;
2475 if (!path->pathdata.Count) return FALSE;
2477 for (i = 0; i < path->pathdata.Count; i++)
2479 short x, y;
2480 x = gdip_round(path->pathdata.Points[i].X);
2481 y = gdip_round(path->pathdata.Points[i].Y);
2482 if (path->pathdata.Points[i].X != (REAL)x || path->pathdata.Points[i].Y != (REAL)y)
2483 return FALSE;
2485 return TRUE;
2488 DWORD write_path_data(GpPath *path, void *data)
2490 struct path_header *header = data;
2491 BOOL integer_path = is_integer_path(path);
2492 DWORD i, size;
2493 BYTE *types;
2495 size = sizeof(struct path_header) + path->pathdata.Count;
2496 if (integer_path)
2497 size += sizeof(short[2]) * path->pathdata.Count;
2498 else
2499 size += sizeof(float[2]) * path->pathdata.Count;
2500 size = (size + 3) & ~3;
2502 if (!data) return size;
2504 header->version = VERSION_MAGIC2;
2505 header->count = path->pathdata.Count;
2506 header->flags = integer_path ? FLAGS_INTPATH : 0;
2508 if (integer_path)
2510 short *points = (short*)(header + 1);
2511 for (i = 0; i < path->pathdata.Count; i++)
2513 points[2*i] = path->pathdata.Points[i].X;
2514 points[2*i + 1] = path->pathdata.Points[i].Y;
2516 types = (BYTE*)(points + 2*i);
2518 else
2520 float *points = (float*)(header + 1);
2521 for (i = 0; i < path->pathdata.Count; i++)
2523 points[2*i] = path->pathdata.Points[i].X;
2524 points[2*i + 1] = path->pathdata.Points[i].Y;
2526 types = (BYTE*)(points + 2*i);
2529 for (i=0; i<path->pathdata.Count; i++)
2530 types[i] = path->pathdata.Types[i];
2531 memset(types + i, 0, ((path->pathdata.Count + 3) & ~3) - path->pathdata.Count);
2532 return size;