gdiplus: Traverse and draw the whole string.
[wine.git] / dlls / gdiplus / graphicspath.c
blob656069d7ffe0c6844000b37c7a9c666e4edc2330
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 #define SQRT3 1.73205080757
38 typedef struct path_list_node_t path_list_node_t;
39 struct path_list_node_t {
40 GpPointF pt;
41 BYTE type; /* PathPointTypeStart or PathPointTypeLine */
42 path_list_node_t *next;
45 /* init list */
46 static BOOL init_path_list(path_list_node_t **node, REAL x, REAL y)
48 *node = heap_alloc_zero(sizeof(path_list_node_t));
49 if(!*node)
50 return FALSE;
52 (*node)->pt.X = x;
53 (*node)->pt.Y = y;
54 (*node)->type = PathPointTypeStart;
55 (*node)->next = NULL;
57 return TRUE;
60 /* free all nodes including argument */
61 static void free_path_list(path_list_node_t *node)
63 path_list_node_t *n = node;
65 while(n){
66 n = n->next;
67 heap_free(node);
68 node = n;
72 /* Add a node after 'node' */
74 * Returns
75 * pointer on success
76 * NULL on allocation problems
78 static path_list_node_t* add_path_list_node(path_list_node_t *node, REAL x, REAL y, BOOL type)
80 path_list_node_t *new;
82 new = heap_alloc_zero(sizeof(path_list_node_t));
83 if(!new)
84 return NULL;
86 new->pt.X = x;
87 new->pt.Y = y;
88 new->type = type;
89 new->next = node->next;
90 node->next = new;
92 return new;
95 /* returns element count */
96 static INT path_list_count(path_list_node_t *node)
98 INT count = 1;
100 while((node = node->next))
101 ++count;
103 return count;
106 /* GdipFlattenPath helper */
108 * Used to recursively flatten single Bezier curve
109 * Parameters:
110 * - start : pointer to start point node;
111 * - (x2, y2): first control point;
112 * - (x3, y3): second control point;
113 * - end : pointer to end point node
114 * - flatness: admissible error of linear approximation.
116 * Return value:
117 * TRUE : success
118 * FALSE: out of memory
120 * TODO: used quality criteria should be revised to match native as
121 * closer as possible.
123 static BOOL flatten_bezier(path_list_node_t *start, REAL x2, REAL y2, REAL x3, REAL y3,
124 path_list_node_t *end, REAL flatness)
126 /* this 5 middle points with start/end define to half-curves */
127 GpPointF mp[5];
128 GpPointF pt, pt_st;
129 path_list_node_t *node;
131 /* calculate bezier curve middle points == new control points */
132 mp[0].X = (start->pt.X + x2) / 2.0;
133 mp[0].Y = (start->pt.Y + y2) / 2.0;
134 /* middle point between control points */
135 pt.X = (x2 + x3) / 2.0;
136 pt.Y = (y2 + y3) / 2.0;
137 mp[1].X = (mp[0].X + pt.X) / 2.0;
138 mp[1].Y = (mp[0].Y + pt.Y) / 2.0;
139 mp[4].X = (end->pt.X + x3) / 2.0;
140 mp[4].Y = (end->pt.Y + y3) / 2.0;
141 mp[3].X = (mp[4].X + pt.X) / 2.0;
142 mp[3].Y = (mp[4].Y + pt.Y) / 2.0;
144 mp[2].X = (mp[1].X + mp[3].X) / 2.0;
145 mp[2].Y = (mp[1].Y + mp[3].Y) / 2.0;
147 if ((x2 == mp[0].X && y2 == mp[0].Y && x3 == mp[1].X && y3 == mp[1].Y) ||
148 (x2 == mp[3].X && y2 == mp[3].Y && x3 == mp[4].X && y3 == mp[4].Y))
149 return TRUE;
151 pt = end->pt;
152 pt_st = start->pt;
153 /* check flatness as a half of distance between middle point and a linearized path */
154 if(fabs(((pt.Y - pt_st.Y)*mp[2].X + (pt_st.X - pt.X)*mp[2].Y +
155 (pt_st.Y*pt.X - pt_st.X*pt.Y))) <=
156 (0.5 * flatness*sqrtf((powf(pt.Y - pt_st.Y, 2.0) + powf(pt_st.X - pt.X, 2.0))))){
157 return TRUE;
159 else
160 /* add a middle point */
161 if(!(node = add_path_list_node(start, mp[2].X, mp[2].Y, PathPointTypeLine)))
162 return FALSE;
164 /* do the same with halves */
165 flatten_bezier(start, mp[0].X, mp[0].Y, mp[1].X, mp[1].Y, node, flatness);
166 flatten_bezier(node, mp[3].X, mp[3].Y, mp[4].X, mp[4].Y, end, flatness);
168 return TRUE;
171 /* GdipAddPath* helper
173 * Several GdipAddPath functions are expected to add onto an open figure.
174 * So if the first point being added is an exact match to the last point
175 * of the existing line, that point should not be added.
177 * Parameters:
178 * path : path to which points should be added
179 * points : array of points to add
180 * count : number of points to add (at least 1)
181 * type : type of the points being added
183 * Return value:
184 * OutOfMemory : out of memory, could not lengthen path
185 * Ok : success
187 static GpStatus extend_current_figure(GpPath *path, GDIPCONST PointF *points, INT count, BYTE type)
189 INT insert_index = path->pathdata.Count;
190 BYTE first_point_type = (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
192 if(!path->newfigure &&
193 path->pathdata.Points[insert_index-1].X == points[0].X &&
194 path->pathdata.Points[insert_index-1].Y == points[0].Y)
196 points++;
197 count--;
198 first_point_type = type;
201 if(!count)
202 return Ok;
204 if(!lengthen_path(path, count))
205 return OutOfMemory;
207 memcpy(path->pathdata.Points + insert_index, points, sizeof(GpPointF)*count);
208 path->pathdata.Types[insert_index] = first_point_type;
209 memset(path->pathdata.Types + insert_index + 1, type, count - 1);
211 path->newfigure = FALSE;
212 path->pathdata.Count += count;
214 return Ok;
217 /*******************************************************************************
218 * GdipAddPathArc [GDIPLUS.1]
220 * Add an elliptical arc to the given path.
222 * PARAMS
223 * path [I/O] Path that the arc is appended to
224 * x1 [I] X coordinate of the boundary box
225 * y1 [I] Y coordinate of the boundary box
226 * x2 [I] Width of the boundary box
227 * y2 [I] Height of the boundary box
228 * startAngle [I] Starting angle of the arc, clockwise
229 * sweepAngle [I] Angle of the arc, clockwise
231 * RETURNS
232 * InvalidParameter If the given path is invalid
233 * OutOfMemory If memory allocation fails, i.e. the path cannot be lengthened
234 * Ok If everything works out as expected
236 * NOTES
237 * This functions takes the newfigure value of the given path into account,
238 * i.e. the arc is connected to the end of the given path if it was set to
239 * FALSE, otherwise the arc's first point gets the PathPointTypeStart value.
240 * In both cases, the value of newfigure of the given path is FALSE
241 * afterwards.
243 GpStatus WINGDIPAPI GdipAddPathArc(GpPath *path, REAL x1, REAL y1, REAL x2,
244 REAL y2, REAL startAngle, REAL sweepAngle)
246 GpPointF *points;
247 GpStatus status;
248 INT count;
250 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
251 path, x1, y1, x2, y2, startAngle, sweepAngle);
253 if(!path)
254 return InvalidParameter;
256 count = arc2polybezier(NULL, x1, y1, x2, y2, startAngle, sweepAngle);
257 if(count == 0)
258 return Ok;
260 points = heap_alloc_zero(sizeof(GpPointF)*count);
261 if(!points)
262 return OutOfMemory;
264 arc2polybezier(points, x1, y1, x2, y2, startAngle, sweepAngle);
266 status = extend_current_figure(path, points, count, PathPointTypeBezier);
268 heap_free(points);
269 return status;
272 /*******************************************************************************
273 * GdipAddPathArcI [GDUPLUS.2]
275 * See GdipAddPathArc
277 GpStatus WINGDIPAPI GdipAddPathArcI(GpPath *path, INT x1, INT y1, INT x2,
278 INT y2, REAL startAngle, REAL sweepAngle)
280 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
281 path, x1, y1, x2, y2, startAngle, sweepAngle);
283 return GdipAddPathArc(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,startAngle,sweepAngle);
286 GpStatus WINGDIPAPI GdipAddPathBezier(GpPath *path, REAL x1, REAL y1, REAL x2,
287 REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
289 PointF points[4];
291 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
292 path, x1, y1, x2, y2, x3, y3, x4, y4);
294 if(!path)
295 return InvalidParameter;
297 points[0].X = x1;
298 points[0].Y = y1;
299 points[1].X = x2;
300 points[1].Y = y2;
301 points[2].X = x3;
302 points[2].Y = y3;
303 points[3].X = x4;
304 points[3].Y = y4;
306 return extend_current_figure(path, points, 4, PathPointTypeBezier);
309 GpStatus WINGDIPAPI GdipAddPathBezierI(GpPath *path, INT x1, INT y1, INT x2,
310 INT y2, INT x3, INT y3, INT x4, INT y4)
312 TRACE("(%p, %d, %d, %d, %d, %d, %d, %d, %d)\n",
313 path, x1, y1, x2, y2, x3, y3, x4, y4);
315 return GdipAddPathBezier(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,(REAL)x3,(REAL)y3,
316 (REAL)x4,(REAL)y4);
319 GpStatus WINGDIPAPI GdipAddPathBeziers(GpPath *path, GDIPCONST GpPointF *points,
320 INT count)
322 TRACE("(%p, %p, %d)\n", path, points, count);
324 if(!path || !points || ((count - 1) % 3))
325 return InvalidParameter;
327 return extend_current_figure(path, points, count, PathPointTypeBezier);
330 GpStatus WINGDIPAPI GdipAddPathBeziersI(GpPath *path, GDIPCONST GpPoint *points,
331 INT count)
333 GpPointF *ptsF;
334 GpStatus ret;
335 INT i;
337 TRACE("(%p, %p, %d)\n", path, points, count);
339 if(!points || ((count - 1) % 3))
340 return InvalidParameter;
342 ptsF = heap_alloc_zero(sizeof(GpPointF) * count);
343 if(!ptsF)
344 return OutOfMemory;
346 for(i = 0; i < count; i++){
347 ptsF[i].X = (REAL)points[i].X;
348 ptsF[i].Y = (REAL)points[i].Y;
351 ret = GdipAddPathBeziers(path, ptsF, count);
352 heap_free(ptsF);
354 return ret;
357 GpStatus WINGDIPAPI GdipAddPathClosedCurve(GpPath *path, GDIPCONST GpPointF *points,
358 INT count)
360 TRACE("(%p, %p, %d)\n", path, points, count);
362 return GdipAddPathClosedCurve2(path, points, count, 1.0);
365 GpStatus WINGDIPAPI GdipAddPathClosedCurveI(GpPath *path, GDIPCONST GpPoint *points,
366 INT count)
368 TRACE("(%p, %p, %d)\n", path, points, count);
370 return GdipAddPathClosedCurve2I(path, points, count, 1.0);
373 GpStatus WINGDIPAPI GdipAddPathClosedCurve2(GpPath *path, GDIPCONST GpPointF *points,
374 INT count, REAL tension)
376 INT i, len_pt = (count + 1)*3-2;
377 GpPointF *pt;
378 GpPointF *pts;
379 REAL x1, x2, y1, y2;
380 GpStatus stat;
382 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
384 if(!path || !points || count <= 1)
385 return InvalidParameter;
387 pt = heap_alloc_zero(len_pt * sizeof(GpPointF));
388 pts = heap_alloc_zero((count + 1)*sizeof(GpPointF));
389 if(!pt || !pts){
390 heap_free(pt);
391 heap_free(pts);
392 return OutOfMemory;
395 /* copy source points to extend with the last one */
396 memcpy(pts, points, sizeof(GpPointF)*count);
397 pts[count] = pts[0];
399 tension = tension * TENSION_CONST;
401 for(i = 0; i < count-1; i++){
402 calc_curve_bezier(&(pts[i]), tension, &x1, &y1, &x2, &y2);
404 pt[3*i+2].X = x1;
405 pt[3*i+2].Y = y1;
406 pt[3*i+3].X = pts[i+1].X;
407 pt[3*i+3].Y = pts[i+1].Y;
408 pt[3*i+4].X = x2;
409 pt[3*i+4].Y = y2;
412 /* points [len_pt-2] and [0] are calculated
413 separately to connect splines properly */
414 pts[0] = points[count-1];
415 pts[1] = points[0]; /* equals to start and end of a resulting path */
416 pts[2] = points[1];
418 calc_curve_bezier(pts, tension, &x1, &y1, &x2, &y2);
419 pt[len_pt-2].X = x1;
420 pt[len_pt-2].Y = y1;
421 pt[0].X = pts[1].X;
422 pt[0].Y = pts[1].Y;
423 pt[1].X = x2;
424 pt[1].Y = y2;
425 /* close path */
426 pt[len_pt-1].X = pt[0].X;
427 pt[len_pt-1].Y = pt[0].Y;
429 stat = extend_current_figure(path, pt, len_pt, PathPointTypeBezier);
431 /* close figure */
432 if(stat == Ok){
433 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
434 path->newfigure = TRUE;
437 heap_free(pts);
438 heap_free(pt);
440 return stat;
443 GpStatus WINGDIPAPI GdipAddPathClosedCurve2I(GpPath *path, GDIPCONST GpPoint *points,
444 INT count, REAL tension)
446 GpPointF *ptf;
447 INT i;
448 GpStatus stat;
450 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
452 if(!path || !points || count <= 1)
453 return InvalidParameter;
455 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
456 if(!ptf)
457 return OutOfMemory;
459 for(i = 0; i < count; i++){
460 ptf[i].X = (REAL)points[i].X;
461 ptf[i].Y = (REAL)points[i].Y;
464 stat = GdipAddPathClosedCurve2(path, ptf, count, tension);
466 heap_free(ptf);
468 return stat;
471 GpStatus WINGDIPAPI GdipAddPathCurve(GpPath *path, GDIPCONST GpPointF *points, INT count)
473 TRACE("(%p, %p, %d)\n", path, points, count);
475 if(!path || !points || count <= 1)
476 return InvalidParameter;
478 return GdipAddPathCurve2(path, points, count, 1.0);
481 GpStatus WINGDIPAPI GdipAddPathCurveI(GpPath *path, GDIPCONST GpPoint *points, INT count)
483 TRACE("(%p, %p, %d)\n", path, points, count);
485 if(!path || !points || count <= 1)
486 return InvalidParameter;
488 return GdipAddPathCurve2I(path, points, count, 1.0);
491 GpStatus WINGDIPAPI GdipAddPathCurve2(GpPath *path, GDIPCONST GpPointF *points, INT count,
492 REAL tension)
494 INT i, len_pt = count*3-2;
495 GpPointF *pt;
496 REAL x1, x2, y1, y2;
497 GpStatus stat;
499 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
501 if(!path || !points || count <= 1)
502 return InvalidParameter;
504 pt = heap_alloc_zero(len_pt * sizeof(GpPointF));
505 if(!pt)
506 return OutOfMemory;
508 tension = tension * TENSION_CONST;
510 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
511 tension, &x1, &y1);
513 pt[0].X = points[0].X;
514 pt[0].Y = points[0].Y;
515 pt[1].X = x1;
516 pt[1].Y = y1;
518 for(i = 0; i < count-2; i++){
519 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
521 pt[3*i+2].X = x1;
522 pt[3*i+2].Y = y1;
523 pt[3*i+3].X = points[i+1].X;
524 pt[3*i+3].Y = points[i+1].Y;
525 pt[3*i+4].X = x2;
526 pt[3*i+4].Y = y2;
529 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
530 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
532 pt[len_pt-2].X = x1;
533 pt[len_pt-2].Y = y1;
534 pt[len_pt-1].X = points[count-1].X;
535 pt[len_pt-1].Y = points[count-1].Y;
537 stat = extend_current_figure(path, pt, len_pt, PathPointTypeBezier);
539 heap_free(pt);
541 return stat;
544 GpStatus WINGDIPAPI GdipAddPathCurve2I(GpPath *path, GDIPCONST GpPoint *points,
545 INT count, REAL tension)
547 GpPointF *ptf;
548 INT i;
549 GpStatus stat;
551 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
553 if(!path || !points || count <= 1)
554 return InvalidParameter;
556 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
557 if(!ptf)
558 return OutOfMemory;
560 for(i = 0; i < count; i++){
561 ptf[i].X = (REAL)points[i].X;
562 ptf[i].Y = (REAL)points[i].Y;
565 stat = GdipAddPathCurve2(path, ptf, count, tension);
567 heap_free(ptf);
569 return stat;
572 GpStatus WINGDIPAPI GdipAddPathCurve3(GpPath *path, GDIPCONST GpPointF *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 GdipAddPathCurve2(path, &points[offset], nseg + 1, tension);
583 GpStatus WINGDIPAPI GdipAddPathCurve3I(GpPath *path, GDIPCONST GpPoint *points,
584 INT count, INT offset, INT nseg, REAL tension)
586 TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
588 if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
589 return InvalidParameter;
591 return GdipAddPathCurve2I(path, &points[offset], nseg + 1, tension);
594 GpStatus WINGDIPAPI GdipAddPathEllipse(GpPath *path, REAL x, REAL y, REAL width,
595 REAL height)
597 INT old_count, numpts;
599 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
601 if(!path)
602 return InvalidParameter;
604 if(!lengthen_path(path, MAX_ARC_PTS))
605 return OutOfMemory;
607 old_count = path->pathdata.Count;
608 if((numpts = arc2polybezier(&path->pathdata.Points[old_count], x, y, width,
609 height, 0.0, 360.0)) != MAX_ARC_PTS){
610 ERR("expected %d points but got %d\n", MAX_ARC_PTS, numpts);
611 return GenericError;
614 memset(&path->pathdata.Types[old_count + 1], PathPointTypeBezier,
615 MAX_ARC_PTS - 1);
617 /* An ellipse is an intrinsic figure (always is its own subpath). */
618 path->pathdata.Types[old_count] = PathPointTypeStart;
619 path->pathdata.Types[old_count + MAX_ARC_PTS - 1] |= PathPointTypeCloseSubpath;
620 path->newfigure = TRUE;
621 path->pathdata.Count += MAX_ARC_PTS;
623 return Ok;
626 GpStatus WINGDIPAPI GdipAddPathEllipseI(GpPath *path, INT x, INT y, INT width,
627 INT height)
629 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
631 return GdipAddPathEllipse(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
634 GpStatus WINGDIPAPI GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
635 INT count)
637 TRACE("(%p, %p, %d)\n", path, points, count);
639 if(!path || !points || count < 1)
640 return InvalidParameter;
642 return extend_current_figure(path, points, count, PathPointTypeLine);
645 GpStatus WINGDIPAPI GdipAddPathLine2I(GpPath *path, GDIPCONST GpPoint *points, INT count)
647 GpPointF *pointsF;
648 INT i;
649 GpStatus stat;
651 TRACE("(%p, %p, %d)\n", path, points, count);
653 if(count <= 0)
654 return InvalidParameter;
656 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
657 if(!pointsF) return OutOfMemory;
659 for(i = 0;i < count; i++){
660 pointsF[i].X = (REAL)points[i].X;
661 pointsF[i].Y = (REAL)points[i].Y;
664 stat = GdipAddPathLine2(path, pointsF, count);
666 heap_free(pointsF);
668 return stat;
671 /*************************************************************************
672 * GdipAddPathLine [GDIPLUS.21]
674 * Add two points to the given path.
676 * PARAMS
677 * path [I/O] Path that the line is appended to
678 * x1 [I] X coordinate of the first point of the line
679 * y1 [I] Y coordinate of the first point of the line
680 * x2 [I] X coordinate of the second point of the line
681 * y2 [I] Y coordinate of the second point of the line
683 * RETURNS
684 * InvalidParameter If the first parameter is not a valid path
685 * OutOfMemory If the path cannot be lengthened, i.e. memory allocation fails
686 * Ok If everything works out as expected
688 * NOTES
689 * This functions takes the newfigure value of the given path into account,
690 * i.e. the two new points are connected to the end of the given path if it
691 * was set to FALSE, otherwise the first point is given the PathPointTypeStart
692 * value. In both cases, the value of newfigure of the given path is FALSE
693 * afterwards.
695 GpStatus WINGDIPAPI GdipAddPathLine(GpPath *path, REAL x1, REAL y1, REAL x2, REAL y2)
697 PointF points[2];
699 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x1, y1, x2, y2);
701 if(!path)
702 return InvalidParameter;
704 points[0].X = x1;
705 points[0].Y = y1;
706 points[1].X = x2;
707 points[1].Y = y2;
709 return extend_current_figure(path, points, 2, PathPointTypeLine);
712 /*************************************************************************
713 * GdipAddPathLineI [GDIPLUS.21]
715 * See GdipAddPathLine
717 GpStatus WINGDIPAPI GdipAddPathLineI(GpPath *path, INT x1, INT y1, INT x2, INT y2)
719 TRACE("(%p, %d, %d, %d, %d)\n", path, x1, y1, x2, y2);
721 return GdipAddPathLine(path, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
724 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
725 BOOL connect)
727 INT old_count, count;
729 TRACE("(%p, %p, %d)\n", path, addingPath, connect);
731 if(!path || !addingPath)
732 return InvalidParameter;
734 old_count = path->pathdata.Count;
735 count = addingPath->pathdata.Count;
737 if(!lengthen_path(path, count))
738 return OutOfMemory;
740 memcpy(&path->pathdata.Points[old_count], addingPath->pathdata.Points,
741 count * sizeof(GpPointF));
742 memcpy(&path->pathdata.Types[old_count], addingPath->pathdata.Types, count);
744 if(path->newfigure || !connect)
745 path->pathdata.Types[old_count] = PathPointTypeStart;
746 else
747 path->pathdata.Types[old_count] = PathPointTypeLine;
749 path->newfigure = FALSE;
750 path->pathdata.Count += count;
752 return Ok;
755 GpStatus WINGDIPAPI GdipAddPathPie(GpPath *path, REAL x, REAL y, REAL width, REAL height,
756 REAL startAngle, REAL sweepAngle)
758 GpPointF *ptf;
759 GpStatus status;
760 INT i, count;
762 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
763 path, x, y, width, height, startAngle, sweepAngle);
765 if(!path)
766 return InvalidParameter;
768 /* on zero width/height only start point added */
769 if(width <= 1e-7 || height <= 1e-7){
770 if(!lengthen_path(path, 1))
771 return OutOfMemory;
772 path->pathdata.Points[0].X = x + width / 2.0;
773 path->pathdata.Points[0].Y = y + height / 2.0;
774 path->pathdata.Types[0] = PathPointTypeStart | PathPointTypeCloseSubpath;
775 path->pathdata.Count = 1;
776 return InvalidParameter;
779 count = arc2polybezier(NULL, x, y, width, height, startAngle, sweepAngle);
781 if(count == 0)
782 return Ok;
784 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
785 if(!ptf)
786 return OutOfMemory;
788 arc2polybezier(ptf, x, y, width, height, startAngle, sweepAngle);
790 status = GdipAddPathLine(path, x + width/2, y + height/2, ptf[0].X, ptf[0].Y);
791 if(status != Ok){
792 heap_free(ptf);
793 return status;
795 /* one spline is already added as a line endpoint */
796 if(!lengthen_path(path, count - 1)){
797 heap_free(ptf);
798 return OutOfMemory;
801 memcpy(&(path->pathdata.Points[path->pathdata.Count]), &(ptf[1]),sizeof(GpPointF)*(count-1));
802 for(i = 0; i < count-1; i++)
803 path->pathdata.Types[path->pathdata.Count+i] = PathPointTypeBezier;
805 path->pathdata.Count += count-1;
807 GdipClosePathFigure(path);
809 heap_free(ptf);
811 return status;
814 GpStatus WINGDIPAPI GdipAddPathPieI(GpPath *path, INT x, INT y, INT width, INT height,
815 REAL startAngle, REAL sweepAngle)
817 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
818 path, x, y, width, height, startAngle, sweepAngle);
820 return GdipAddPathPie(path, (REAL)x, (REAL)y, (REAL)width, (REAL)height, startAngle, sweepAngle);
823 GpStatus WINGDIPAPI GdipAddPathPolygon(GpPath *path, GDIPCONST GpPointF *points, INT count)
825 INT old_count;
827 TRACE("(%p, %p, %d)\n", path, points, count);
829 if(!path || !points || count < 3)
830 return InvalidParameter;
832 if(!lengthen_path(path, count))
833 return OutOfMemory;
835 old_count = path->pathdata.Count;
837 memcpy(&path->pathdata.Points[old_count], points, count*sizeof(GpPointF));
838 memset(&path->pathdata.Types[old_count + 1], PathPointTypeLine, count - 1);
840 /* A polygon is an intrinsic figure */
841 path->pathdata.Types[old_count] = PathPointTypeStart;
842 path->pathdata.Types[old_count + count - 1] |= PathPointTypeCloseSubpath;
843 path->newfigure = TRUE;
844 path->pathdata.Count += count;
846 return Ok;
849 GpStatus WINGDIPAPI GdipAddPathPolygonI(GpPath *path, GDIPCONST GpPoint *points, INT count)
851 GpPointF *ptf;
852 GpStatus status;
853 INT i;
855 TRACE("(%p, %p, %d)\n", path, points, count);
857 if(!points || count < 3)
858 return InvalidParameter;
860 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
861 if(!ptf)
862 return OutOfMemory;
864 for(i = 0; i < count; i++){
865 ptf[i].X = (REAL)points[i].X;
866 ptf[i].Y = (REAL)points[i].Y;
869 status = GdipAddPathPolygon(path, ptf, count);
871 heap_free(ptf);
873 return status;
876 static float fromfixedpoint(const FIXED v)
878 float f = ((float)v.fract) / (1<<(sizeof(v.fract)*8));
879 f += v.value;
880 return f;
883 struct format_string_args
885 GpPath *path;
886 float maxY;
887 float scale;
888 float ascent;
891 static GpStatus format_string_callback(HDC dc,
892 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
893 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
894 INT lineno, const RectF *bounds, INT *underlined_indexes,
895 INT underlined_index_count, void *priv)
897 static const MAT2 identity = { {0,1}, {0,0}, {0,0}, {0,1} };
898 struct format_string_args *args = priv;
899 GpPath *path = args->path;
900 GpStatus status = Ok;
901 float x = rect->X + (bounds->X - rect->X) * args->scale;
902 float y = rect->Y + (bounds->Y - rect->Y) * args->scale;
903 int i;
905 if (underlined_index_count)
906 FIXME("hotkey underlines not drawn yet\n");
908 if (y + bounds->Height * args->scale > args->maxY)
909 args->maxY = y + bounds->Height * args->scale;
911 for (i = index; i < length + index; ++i)
913 GLYPHMETRICS gm;
914 TTPOLYGONHEADER *ph = NULL, *origph;
915 char *start;
916 DWORD len, ofs = 0;
917 len = GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, 0, NULL, &identity);
918 if (len == GDI_ERROR)
920 status = GenericError;
921 break;
923 origph = ph = heap_alloc_zero(len);
924 start = (char *)ph;
925 if (!ph || !lengthen_path(path, len / sizeof(POINTFX)))
927 heap_free(ph);
928 status = OutOfMemory;
929 break;
931 GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, len, start, &identity);
933 ofs = 0;
934 while (ofs < len)
936 DWORD ofs_start = ofs;
937 ph = (TTPOLYGONHEADER*)&start[ofs];
938 path->pathdata.Types[path->pathdata.Count] = PathPointTypeStart;
939 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(ph->pfxStart.x) * args->scale;
940 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(ph->pfxStart.y) * args->scale;
941 TRACE("Starting at count %i with pos %f, %f)\n", path->pathdata.Count, x, y);
942 ofs += sizeof(*ph);
943 while (ofs - ofs_start < ph->cb)
945 TTPOLYCURVE *curve = (TTPOLYCURVE*)&start[ofs];
946 int j;
947 ofs += sizeof(TTPOLYCURVE) + (curve->cpfx - 1) * sizeof(POINTFX);
949 switch (curve->wType)
951 case TT_PRIM_LINE:
952 for (j = 0; j < curve->cpfx; ++j)
954 path->pathdata.Types[path->pathdata.Count] = PathPointTypeLine;
955 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
956 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
958 break;
959 case TT_PRIM_CSPLINE:
960 for (j = 0; j < curve->cpfx; ++j)
962 path->pathdata.Types[path->pathdata.Count] = PathPointTypeBezier;
963 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
964 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
966 break;
967 default:
968 ERR("Unhandled type: %u\n", curve->wType);
969 status = GenericError;
972 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
974 path->newfigure = TRUE;
975 x += gm.gmCellIncX * args->scale;
976 y += gm.gmCellIncY * args->scale;
978 heap_free(origph);
979 if (status != Ok)
980 break;
983 return status;
986 GpStatus WINGDIPAPI GdipAddPathString(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat* format)
988 GpFont *font;
989 GpStatus status;
990 LOGFONTW lfw;
991 HANDLE hfont;
992 HDC dc;
993 GpGraphics *graphics;
994 GpPath *backup;
995 struct format_string_args args;
996 int i;
997 UINT16 native_height;
998 RectF scaled_layout_rect;
999 TEXTMETRICW textmetric;
1001 TRACE("(%p, %s, %d, %p, %d, %f, %p, %p)\n", path, debugstr_w(string), length, family, style, emSize, layoutRect, format);
1002 if (!path || !string || !family || !emSize || !layoutRect || !format)
1003 return InvalidParameter;
1005 status = GdipGetEmHeight(family, style, &native_height);
1006 if (status != Ok)
1007 return status;
1009 scaled_layout_rect.X = layoutRect->X;
1010 scaled_layout_rect.Y = layoutRect->Y;
1011 scaled_layout_rect.Width = layoutRect->Width * native_height / emSize;
1012 scaled_layout_rect.Height = layoutRect->Height * native_height / emSize;
1014 if ((status = GdipClonePath(path, &backup)) != Ok)
1015 return status;
1017 dc = CreateCompatibleDC(0);
1018 status = GdipCreateFromHDC(dc, &graphics);
1019 if (status != Ok)
1021 DeleteDC(dc);
1022 GdipDeletePath(backup);
1023 return status;
1026 status = GdipCreateFont(family, native_height, style, UnitPixel, &font);
1027 if (status != Ok)
1029 GdipDeleteGraphics(graphics);
1030 DeleteDC(dc);
1031 GdipDeletePath(backup);
1032 return status;
1035 get_log_fontW(font, graphics, &lfw);
1036 GdipDeleteFont(font);
1037 GdipDeleteGraphics(graphics);
1039 hfont = CreateFontIndirectW(&lfw);
1040 if (!hfont)
1042 WARN("Failed to create font\n");
1043 DeleteDC(dc);
1044 GdipDeletePath(backup);
1045 return GenericError;
1048 SelectObject(dc, hfont);
1050 GetTextMetricsW(dc, &textmetric);
1052 args.path = path;
1053 args.maxY = 0;
1054 args.scale = emSize / native_height;
1055 args.ascent = textmetric.tmAscent * args.scale;
1056 status = gdip_format_string(dc, string, length, NULL, &scaled_layout_rect,
1057 format, TRUE, format_string_callback, &args);
1059 DeleteDC(dc);
1060 DeleteObject(hfont);
1062 if (status != Ok) /* free backup */
1064 heap_free(path->pathdata.Points);
1065 heap_free(path->pathdata.Types);
1066 *path = *backup;
1067 heap_free(backup);
1068 return status;
1070 if (format->line_align == StringAlignmentCenter && layoutRect->Y + args.maxY < layoutRect->Height)
1072 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1073 inc /= 2;
1074 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1075 path->pathdata.Points[i].Y += inc;
1076 } else if (format->line_align == StringAlignmentFar) {
1077 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1078 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1079 path->pathdata.Points[i].Y += inc;
1081 GdipDeletePath(backup);
1082 return status;
1085 GpStatus WINGDIPAPI GdipAddPathStringI(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family,
1086 INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1088 RectF rect;
1090 if (!layoutRect)
1091 return InvalidParameter;
1093 set_rect(&rect, layoutRect->X, layoutRect->Y, layoutRect->Width, layoutRect->Height);
1094 return GdipAddPathString(path, string, length, family, style, emSize, &rect, format);
1097 /*************************************************************************
1098 * GdipClonePath [GDIPLUS.53]
1100 * Duplicate the given path in memory.
1102 * PARAMS
1103 * path [I] The path to be duplicated
1104 * clone [O] Pointer to the new path
1106 * RETURNS
1107 * InvalidParameter If the input path is invalid
1108 * OutOfMemory If allocation of needed memory fails
1109 * Ok If everything works out as expected
1111 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1113 TRACE("(%p, %p)\n", path, clone);
1115 if(!path || !clone)
1116 return InvalidParameter;
1118 *clone = heap_alloc_zero(sizeof(GpPath));
1119 if(!*clone) return OutOfMemory;
1121 **clone = *path;
1123 (*clone)->pathdata.Points = heap_alloc_zero(path->datalen * sizeof(PointF));
1124 (*clone)->pathdata.Types = heap_alloc_zero(path->datalen);
1125 if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1126 heap_free((*clone)->pathdata.Points);
1127 heap_free((*clone)->pathdata.Types);
1128 heap_free(*clone);
1129 return OutOfMemory;
1132 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1133 path->datalen * sizeof(PointF));
1134 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1136 return Ok;
1139 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1141 TRACE("(%p)\n", path);
1143 if(!path)
1144 return InvalidParameter;
1146 if(path->pathdata.Count > 0){
1147 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1148 path->newfigure = TRUE;
1151 return Ok;
1154 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1156 INT i;
1158 TRACE("(%p)\n", path);
1160 if(!path)
1161 return InvalidParameter;
1163 for(i = 1; i < path->pathdata.Count; i++){
1164 if(path->pathdata.Types[i] == PathPointTypeStart)
1165 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1168 path->newfigure = TRUE;
1170 return Ok;
1173 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1175 TRACE("(%d, %p)\n", fill, path);
1177 if(!path)
1178 return InvalidParameter;
1180 *path = heap_alloc_zero(sizeof(GpPath));
1181 if(!*path) return OutOfMemory;
1183 (*path)->fill = fill;
1184 (*path)->newfigure = TRUE;
1186 return Ok;
1189 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1190 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1192 int i;
1194 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1196 if(!points || !types || !path)
1197 return InvalidParameter;
1199 if(count <= 0) {
1200 *path = NULL;
1201 return OutOfMemory;
1204 *path = heap_alloc_zero(sizeof(GpPath));
1205 if(!*path) return OutOfMemory;
1207 if(count > 1 && (types[count-1] & PathPointTypePathTypeMask) == PathPointTypeStart)
1208 count = 0;
1210 for(i = 1; i < count; i++) {
1211 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier) {
1212 if(i+2 < count &&
1213 (types[i+1] & PathPointTypePathTypeMask) == PathPointTypeBezier &&
1214 (types[i+2] & PathPointTypePathTypeMask) == PathPointTypeBezier)
1215 i += 2;
1216 else {
1217 count = 0;
1218 break;
1223 (*path)->pathdata.Points = heap_alloc_zero(count * sizeof(PointF));
1224 (*path)->pathdata.Types = heap_alloc_zero(count);
1226 if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
1227 heap_free((*path)->pathdata.Points);
1228 heap_free((*path)->pathdata.Types);
1229 heap_free(*path);
1230 return OutOfMemory;
1233 memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
1234 memcpy((*path)->pathdata.Types, types, count);
1235 if(count > 0)
1236 (*path)->pathdata.Types[0] = PathPointTypeStart;
1237 (*path)->pathdata.Count = count;
1238 (*path)->datalen = count;
1240 (*path)->fill = fill;
1241 (*path)->newfigure = TRUE;
1243 return Ok;
1246 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1247 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1249 GpPointF *ptF;
1250 GpStatus ret;
1251 INT i;
1253 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1255 ptF = heap_alloc_zero(sizeof(GpPointF)*count);
1257 for(i = 0;i < count; i++){
1258 ptF[i].X = (REAL)points[i].X;
1259 ptF[i].Y = (REAL)points[i].Y;
1262 ret = GdipCreatePath2(ptF, types, count, fill, path);
1264 heap_free(ptF);
1266 return ret;
1269 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1271 TRACE("(%p)\n", path);
1273 if(!path)
1274 return InvalidParameter;
1276 heap_free(path->pathdata.Points);
1277 heap_free(path->pathdata.Types);
1278 heap_free(path);
1280 return Ok;
1283 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1285 path_list_node_t *list, *node;
1286 GpPointF pt;
1287 INT i = 1;
1288 INT startidx = 0;
1289 GpStatus stat;
1291 TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1293 if(!path)
1294 return InvalidParameter;
1296 if(path->pathdata.Count == 0)
1297 return Ok;
1299 stat = GdipTransformPath(path, matrix);
1300 if(stat != Ok)
1301 return stat;
1303 pt = path->pathdata.Points[0];
1304 if(!init_path_list(&list, pt.X, pt.Y))
1305 return OutOfMemory;
1307 node = list;
1309 while(i < path->pathdata.Count){
1311 BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1312 path_list_node_t *start;
1314 pt = path->pathdata.Points[i];
1316 /* save last start point index */
1317 if(type == PathPointTypeStart)
1318 startidx = i;
1320 /* always add line points and start points */
1321 if((type == PathPointTypeStart) || (type == PathPointTypeLine)){
1322 if(!add_path_list_node(node, pt.X, pt.Y, path->pathdata.Types[i]))
1323 goto memout;
1325 node = node->next;
1326 ++i;
1327 continue;
1330 /* Bezier curve */
1332 /* test for closed figure */
1333 if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1334 pt = path->pathdata.Points[startidx];
1335 ++i;
1337 else
1339 i += 2;
1340 pt = path->pathdata.Points[i];
1343 start = node;
1344 /* add Bezier end point */
1345 type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1346 if(!add_path_list_node(node, pt.X, pt.Y, type))
1347 goto memout;
1348 node = node->next;
1350 /* flatten curve */
1351 if(!flatten_bezier(start, path->pathdata.Points[i-2].X, path->pathdata.Points[i-2].Y,
1352 path->pathdata.Points[i-1].X, path->pathdata.Points[i-1].Y,
1353 node, flatness))
1354 goto memout;
1356 ++i;
1357 }/* while */
1359 /* store path data back */
1360 i = path_list_count(list);
1361 if(!lengthen_path(path, i))
1362 goto memout;
1363 path->pathdata.Count = i;
1365 node = list;
1366 for(i = 0; i < path->pathdata.Count; i++){
1367 path->pathdata.Points[i] = node->pt;
1368 path->pathdata.Types[i] = node->type;
1369 node = node->next;
1372 free_path_list(list);
1373 return Ok;
1375 memout:
1376 free_path_list(list);
1377 return OutOfMemory;
1380 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1382 TRACE("(%p, %p)\n", path, pathData);
1384 if(!path || !pathData)
1385 return InvalidParameter;
1387 /* Only copy data. pathData allocation/freeing controlled by wrapper class.
1388 Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
1389 memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
1390 memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
1392 return Ok;
1395 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1397 TRACE("(%p, %p)\n", path, fillmode);
1399 if(!path || !fillmode)
1400 return InvalidParameter;
1402 *fillmode = path->fill;
1404 return Ok;
1407 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1409 INT count;
1411 TRACE("(%p, %p)\n", path, lastPoint);
1413 if(!path || !lastPoint)
1414 return InvalidParameter;
1416 count = path->pathdata.Count;
1417 if(count > 0)
1418 *lastPoint = path->pathdata.Points[count-1];
1420 return Ok;
1423 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1425 TRACE("(%p, %p, %d)\n", path, points, count);
1427 if(!path)
1428 return InvalidParameter;
1430 if(count < path->pathdata.Count)
1431 return InsufficientBuffer;
1433 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1435 return Ok;
1438 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1440 GpStatus ret;
1441 GpPointF *ptf;
1442 INT i;
1444 TRACE("(%p, %p, %d)\n", path, points, count);
1446 if(count <= 0)
1447 return InvalidParameter;
1449 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
1450 if(!ptf) return OutOfMemory;
1452 ret = GdipGetPathPoints(path,ptf,count);
1453 if(ret == Ok)
1454 for(i = 0;i < count;i++){
1455 points[i].X = gdip_round(ptf[i].X);
1456 points[i].Y = gdip_round(ptf[i].Y);
1458 heap_free(ptf);
1460 return ret;
1463 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1465 TRACE("(%p, %p, %d)\n", path, types, count);
1467 if(!path)
1468 return InvalidParameter;
1470 if(count < path->pathdata.Count)
1471 return InsufficientBuffer;
1473 memcpy(types, path->pathdata.Types, path->pathdata.Count);
1475 return Ok;
1478 /* Windows expands the bounding box to the maximum possible bounding box
1479 * for a given pen. For example, if a line join can extend past the point
1480 * it's joining by x units, the bounding box is extended by x units in every
1481 * direction (even though this is too conservative for most cases). */
1482 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
1483 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1485 GpPointF * points, temp_pts[4];
1486 INT count, i;
1487 REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
1489 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1491 /* Matrix and pen can be null. */
1492 if(!path || !bounds)
1493 return InvalidParameter;
1495 /* If path is empty just return. */
1496 count = path->pathdata.Count;
1497 if(count == 0){
1498 bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
1499 return Ok;
1502 points = path->pathdata.Points;
1504 low_x = high_x = points[0].X;
1505 low_y = high_y = points[0].Y;
1507 for(i = 1; i < count; i++){
1508 low_x = min(low_x, points[i].X);
1509 low_y = min(low_y, points[i].Y);
1510 high_x = max(high_x, points[i].X);
1511 high_y = max(high_y, points[i].Y);
1514 width = high_x - low_x;
1515 height = high_y - low_y;
1517 /* This looks unusual but it's the only way I can imitate windows. */
1518 if(matrix){
1519 temp_pts[0].X = low_x;
1520 temp_pts[0].Y = low_y;
1521 temp_pts[1].X = low_x;
1522 temp_pts[1].Y = high_y;
1523 temp_pts[2].X = high_x;
1524 temp_pts[2].Y = high_y;
1525 temp_pts[3].X = high_x;
1526 temp_pts[3].Y = low_y;
1528 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1529 low_x = temp_pts[0].X;
1530 low_y = temp_pts[0].Y;
1532 for(i = 1; i < 4; i++){
1533 low_x = min(low_x, temp_pts[i].X);
1534 low_y = min(low_y, temp_pts[i].Y);
1537 temp = width;
1538 width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
1539 height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
1542 if(pen){
1543 path_width = pen->width / 2.0;
1545 if(count > 2)
1546 path_width = max(path_width, pen->width * pen->miterlimit / 2.0);
1547 /* FIXME: this should probably also check for the startcap */
1548 if(pen->endcap & LineCapNoAnchor)
1549 path_width = max(path_width, pen->width * 2.2);
1551 low_x -= path_width;
1552 low_y -= path_width;
1553 width += 2.0 * path_width;
1554 height += 2.0 * path_width;
1557 bounds->X = low_x;
1558 bounds->Y = low_y;
1559 bounds->Width = width;
1560 bounds->Height = height;
1562 return Ok;
1565 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1566 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1568 GpStatus ret;
1569 GpRectF boundsF;
1571 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1573 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
1575 if(ret == Ok){
1576 bounds->X = gdip_round(boundsF.X);
1577 bounds->Y = gdip_round(boundsF.Y);
1578 bounds->Width = gdip_round(boundsF.Width);
1579 bounds->Height = gdip_round(boundsF.Height);
1582 return ret;
1585 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1587 TRACE("(%p, %p)\n", path, count);
1589 if(!path)
1590 return InvalidParameter;
1592 *count = path->pathdata.Count;
1594 return Ok;
1597 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1599 INT i, count;
1600 INT start = 0; /* position in reversed path */
1601 GpPathData revpath;
1603 TRACE("(%p)\n", path);
1605 if(!path)
1606 return InvalidParameter;
1608 count = path->pathdata.Count;
1610 if(count == 0) return Ok;
1612 revpath.Points = heap_alloc_zero(sizeof(GpPointF)*count);
1613 revpath.Types = heap_alloc_zero(sizeof(BYTE)*count);
1614 revpath.Count = count;
1615 if(!revpath.Points || !revpath.Types){
1616 heap_free(revpath.Points);
1617 heap_free(revpath.Types);
1618 return OutOfMemory;
1621 for(i = 0; i < count; i++){
1623 /* find next start point */
1624 if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1625 INT j;
1626 for(j = start; j <= i; j++){
1627 revpath.Points[j] = path->pathdata.Points[count-j-1];
1628 revpath.Types[j] = path->pathdata.Types[count-j-1];
1630 /* mark start point */
1631 revpath.Types[start] = PathPointTypeStart;
1632 /* set 'figure' endpoint type */
1633 if(i-start > 1){
1634 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1635 revpath.Types[i] |= revpath.Types[i-1];
1637 else
1638 revpath.Types[i] = path->pathdata.Types[start];
1640 start = i+1;
1644 memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1645 memcpy(path->pathdata.Types, revpath.Types, sizeof(BYTE)*count);
1647 heap_free(revpath.Points);
1648 heap_free(revpath.Types);
1650 return Ok;
1653 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1654 GpPen *pen, GpGraphics *graphics, BOOL *result)
1656 TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1658 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1661 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1662 GpPen *pen, GpGraphics *graphics, BOOL *result)
1664 GpStatus stat;
1665 GpPath *wide_path;
1666 GpMatrix *transform = NULL;
1668 TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1670 if(!path || !pen)
1671 return InvalidParameter;
1673 stat = GdipClonePath(path, &wide_path);
1675 if (stat != Ok)
1676 return stat;
1678 if (pen->unit == UnitPixel && graphics != NULL)
1680 stat = GdipCreateMatrix(&transform);
1682 if (stat == Ok)
1683 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1684 CoordinateSpaceWorld, transform);
1687 if (stat == Ok)
1688 stat = GdipWidenPath(wide_path, pen, transform, 1.0);
1690 if (pen->unit == UnitPixel && graphics != NULL)
1692 if (stat == Ok)
1693 stat = GdipInvertMatrix(transform);
1695 if (stat == Ok)
1696 stat = GdipTransformPath(wide_path, transform);
1699 if (stat == Ok)
1700 stat = GdipIsVisiblePathPoint(wide_path, x, y, graphics, result);
1702 GdipDeleteMatrix(transform);
1704 GdipDeletePath(wide_path);
1706 return stat;
1709 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1711 TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1713 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1716 /*****************************************************************************
1717 * GdipIsVisiblePathPoint [GDIPLUS.@]
1719 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1721 GpRegion *region;
1722 HRGN hrgn;
1723 GpStatus status;
1725 if(!path || !result) return InvalidParameter;
1727 status = GdipCreateRegionPath(path, &region);
1728 if(status != Ok)
1729 return status;
1731 status = GdipGetRegionHRgn(region, graphics, &hrgn);
1732 if(status != Ok){
1733 GdipDeleteRegion(region);
1734 return status;
1737 *result = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1739 DeleteObject(hrgn);
1740 GdipDeleteRegion(region);
1742 return Ok;
1745 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1747 TRACE("(%p)\n", path);
1749 if(!path)
1750 return InvalidParameter;
1752 path->newfigure = TRUE;
1754 return Ok;
1757 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1759 TRACE("(%p)\n", path);
1761 if(!path)
1762 return InvalidParameter;
1764 path->pathdata.Count = 0;
1765 path->newfigure = TRUE;
1766 path->fill = FillModeAlternate;
1768 return Ok;
1771 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1773 TRACE("(%p, %d)\n", path, fill);
1775 if(!path)
1776 return InvalidParameter;
1778 path->fill = fill;
1780 return Ok;
1783 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1785 TRACE("(%p, %p)\n", path, matrix);
1787 if(!path)
1788 return InvalidParameter;
1790 if(path->pathdata.Count == 0 || !matrix)
1791 return Ok;
1793 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1794 path->pathdata.Count);
1797 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1798 GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1799 REAL height, WarpMode warpmode, REAL flatness)
1801 FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1802 points, count, x, y, width, height, warpmode, flatness);
1804 return NotImplemented;
1807 static void add_bevel_point(const GpPointF *endpoint, const GpPointF *nextpoint,
1808 REAL pen_width, int right_side, path_list_node_t **last_point)
1810 REAL segment_dy = nextpoint->Y-endpoint->Y;
1811 REAL segment_dx = nextpoint->X-endpoint->X;
1812 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1813 REAL distance = pen_width / 2.0;
1814 REAL bevel_dx, bevel_dy;
1816 if (segment_length == 0.0)
1818 *last_point = add_path_list_node(*last_point, endpoint->X,
1819 endpoint->Y, PathPointTypeLine);
1820 return;
1823 if (right_side)
1825 bevel_dx = -distance * segment_dy / segment_length;
1826 bevel_dy = distance * segment_dx / segment_length;
1828 else
1830 bevel_dx = distance * segment_dy / segment_length;
1831 bevel_dy = -distance * segment_dx / segment_length;
1834 *last_point = add_path_list_node(*last_point, endpoint->X + bevel_dx,
1835 endpoint->Y + bevel_dy, PathPointTypeLine);
1838 static void widen_joint(const GpPointF *p1, const GpPointF *p2, const GpPointF *p3,
1839 GpPen* pen, REAL pen_width, path_list_node_t **last_point)
1841 switch (pen->join)
1843 case LineJoinMiter:
1844 case LineJoinMiterClipped:
1845 if ((p2->X - p1->X) * (p3->Y - p1->Y) > (p2->Y - p1->Y) * (p3->X - p1->X))
1847 float distance = pen_width / 2.0;
1848 float length_0 = sqrtf((p2->X-p1->X)*(p2->X-p1->X)+(p2->Y-p1->Y)*(p2->Y-p1->Y));
1849 float length_1 = sqrtf((p3->X-p2->X)*(p3->X-p2->X)+(p3->Y-p2->Y)*(p3->Y-p2->Y));
1850 float dx0 = distance * (p2->X - p1->X) / length_0;
1851 float dy0 = distance * (p2->Y - p1->Y) / length_0;
1852 float dx1 = distance * (p3->X - p2->X) / length_1;
1853 float dy1 = distance * (p3->Y - p2->Y) / length_1;
1854 float det = (dy0*dx1 - dx0*dy1);
1855 float dx = (dx0*dx1*(dx0-dx1) + dy0*dy0*dx1 - dy1*dy1*dx0)/det;
1856 float dy = (dy0*dy1*(dy0-dy1) + dx0*dx0*dy1 - dx1*dx1*dy0)/det;
1857 if (dx*dx + dy*dy < pen->miterlimit*pen->miterlimit * distance*distance)
1859 *last_point = add_path_list_node(*last_point, p2->X + dx,
1860 p2->Y + dy, PathPointTypeLine);
1861 break;
1863 else if (pen->join == LineJoinMiter)
1865 static int once;
1866 if (!once++)
1867 FIXME("should add a clipped corner\n");
1869 /* else fall-through */
1871 /* else fall-through */
1872 default:
1873 case LineJoinBevel:
1874 add_bevel_point(p2, p1, pen_width, 1, last_point);
1875 add_bevel_point(p2, p3, pen_width, 0, last_point);
1876 break;
1880 static void widen_cap(const GpPointF *endpoint, const GpPointF *nextpoint,
1881 REAL pen_width, GpLineCap cap, GpCustomLineCap* custom_cap, int add_first_points,
1882 int add_last_point, path_list_node_t **last_point)
1884 switch (cap)
1886 default:
1887 case LineCapFlat:
1888 if (add_first_points)
1889 add_bevel_point(endpoint, nextpoint, pen_width, 1, last_point);
1890 if (add_last_point)
1891 add_bevel_point(endpoint, nextpoint, pen_width, 0, last_point);
1892 break;
1893 case LineCapSquare:
1894 case LineCapCustom:
1895 case LineCapArrowAnchor:
1897 REAL segment_dy = nextpoint->Y-endpoint->Y;
1898 REAL segment_dx = nextpoint->X-endpoint->X;
1899 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1900 REAL distance = pen_width / 2.0;
1901 REAL bevel_dx, bevel_dy;
1902 REAL extend_dx, extend_dy;
1904 extend_dx = distance * segment_dx / segment_length;
1905 extend_dy = distance * segment_dy / segment_length;
1907 bevel_dx = -extend_dy;
1908 bevel_dy = extend_dx;
1910 if (cap == LineCapCustom)
1912 extend_dx = -2.0 * custom_cap->inset * extend_dx;
1913 extend_dy = -2.0 * custom_cap->inset * extend_dy;
1915 else if (cap == LineCapArrowAnchor)
1917 extend_dx = -3.0 * extend_dx;
1918 extend_dy = -3.0 * extend_dy;
1921 if (add_first_points)
1922 *last_point = add_path_list_node(*last_point, endpoint->X - extend_dx + bevel_dx,
1923 endpoint->Y - extend_dy + bevel_dy, PathPointTypeLine);
1925 if (add_last_point)
1926 *last_point = add_path_list_node(*last_point, endpoint->X - extend_dx - bevel_dx,
1927 endpoint->Y - extend_dy - bevel_dy, PathPointTypeLine);
1928 break;
1930 case LineCapRound:
1932 REAL segment_dy = nextpoint->Y-endpoint->Y;
1933 REAL segment_dx = nextpoint->X-endpoint->X;
1934 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1935 REAL distance = pen_width / 2.0;
1936 REAL dx, dy, dx2, dy2;
1937 const REAL control_point_distance = 0.5522847498307935; /* 4/3 * (sqrt(2) - 1) */
1939 if (add_first_points)
1941 dx = -distance * segment_dx / segment_length;
1942 dy = -distance * segment_dy / segment_length;
1944 dx2 = dx * control_point_distance;
1945 dy2 = dy * control_point_distance;
1947 /* first 90-degree arc */
1948 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
1949 endpoint->Y - dx, PathPointTypeLine);
1951 *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
1952 endpoint->Y - dx + dy2, PathPointTypeBezier);
1954 *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
1955 endpoint->Y + dy - dx2, PathPointTypeBezier);
1957 /* midpoint */
1958 *last_point = add_path_list_node(*last_point, endpoint->X + dx,
1959 endpoint->Y + dy, PathPointTypeBezier);
1961 /* second 90-degree arc */
1962 *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
1963 endpoint->Y + dy + dx2, PathPointTypeBezier);
1965 *last_point = add_path_list_node(*last_point, endpoint->X - dy + dx2,
1966 endpoint->Y + dx + dy2, PathPointTypeBezier);
1968 *last_point = add_path_list_node(*last_point, endpoint->X - dy,
1969 endpoint->Y + dx, PathPointTypeBezier);
1971 else if (add_last_point)
1972 add_bevel_point(endpoint, nextpoint, pen_width, 0, last_point);
1973 break;
1975 case LineCapTriangle:
1977 REAL segment_dy = nextpoint->Y-endpoint->Y;
1978 REAL segment_dx = nextpoint->X-endpoint->X;
1979 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1980 REAL distance = pen_width / 2.0;
1981 REAL dx, dy;
1983 dx = distance * segment_dx / segment_length;
1984 dy = distance * segment_dy / segment_length;
1986 if (add_first_points) {
1987 add_bevel_point(endpoint, nextpoint, pen_width, 1, last_point);
1989 *last_point = add_path_list_node(*last_point, endpoint->X - dx,
1990 endpoint->Y - dy, PathPointTypeLine);
1992 if (add_first_points || add_last_point)
1993 add_bevel_point(endpoint, nextpoint, pen_width, 0, last_point);
1994 break;
1999 static void widen_open_figure(const GpPointF *points, int start, int end,
2000 GpPen *pen, REAL pen_width, GpLineCap start_cap, GpCustomLineCap* custom_start,
2001 GpLineCap end_cap, GpCustomLineCap* custom_end, path_list_node_t **last_point);
2003 static void widen_closed_figure(const GpPointF *points, int start, int end,
2004 GpPen *pen, REAL pen_width, path_list_node_t **last_point);
2006 static void add_anchor(const GpPointF *endpoint, const GpPointF *nextpoint,
2007 GpPen *pen, GpLineCap cap, GpCustomLineCap *custom, path_list_node_t **last_point)
2009 REAL pen_width = max(pen->width, 2.0);
2010 switch (cap)
2012 default:
2013 case LineCapNoAnchor:
2014 return;
2015 case LineCapSquareAnchor:
2017 REAL segment_dy = nextpoint->Y-endpoint->Y;
2018 REAL segment_dx = nextpoint->X-endpoint->X;
2019 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2020 REAL distance = pen_width / sqrtf(2.0);
2021 REAL par_dx, par_dy;
2022 REAL perp_dx, perp_dy;
2024 par_dx = -distance * segment_dx / segment_length;
2025 par_dy = -distance * segment_dy / segment_length;
2027 perp_dx = -distance * segment_dy / segment_length;
2028 perp_dy = distance * segment_dx / segment_length;
2030 *last_point = add_path_list_node(*last_point, endpoint->X - par_dx - perp_dx,
2031 endpoint->Y - par_dy - perp_dy, PathPointTypeStart);
2032 *last_point = add_path_list_node(*last_point, endpoint->X - par_dx + perp_dx,
2033 endpoint->Y - par_dy + perp_dy, PathPointTypeLine);
2034 *last_point = add_path_list_node(*last_point, endpoint->X + par_dx + perp_dx,
2035 endpoint->Y + par_dy + perp_dy, PathPointTypeLine);
2036 *last_point = add_path_list_node(*last_point, endpoint->X + par_dx - perp_dx,
2037 endpoint->Y + par_dy - perp_dy, PathPointTypeLine);
2038 break;
2040 case LineCapRoundAnchor:
2042 REAL segment_dy = nextpoint->Y-endpoint->Y;
2043 REAL segment_dx = nextpoint->X-endpoint->X;
2044 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2045 REAL dx, dy, dx2, dy2;
2046 const REAL control_point_distance = 0.55228475; /* 4/3 * (sqrt(2) - 1) */
2048 dx = -pen_width * segment_dx / segment_length;
2049 dy = -pen_width * segment_dy / segment_length;
2051 dx2 = dx * control_point_distance;
2052 dy2 = dy * control_point_distance;
2054 /* starting point */
2055 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
2056 endpoint->Y - dx, PathPointTypeStart);
2058 /* first 90-degree arc */
2059 *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
2060 endpoint->Y - dx + dy2, PathPointTypeBezier);
2061 *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
2062 endpoint->Y + dy - dx2, PathPointTypeBezier);
2063 *last_point = add_path_list_node(*last_point, endpoint->X + dx,
2064 endpoint->Y + dy, PathPointTypeBezier);
2066 /* second 90-degree arc */
2067 *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
2068 endpoint->Y + dy + dx2, PathPointTypeBezier);
2069 *last_point = add_path_list_node(*last_point, endpoint->X - dy + dx2,
2070 endpoint->Y + dx + dy2, PathPointTypeBezier);
2071 *last_point = add_path_list_node(*last_point, endpoint->X - dy,
2072 endpoint->Y + dx, PathPointTypeBezier);
2074 /* third 90-degree arc */
2075 *last_point = add_path_list_node(*last_point, endpoint->X - dy - dx2,
2076 endpoint->Y + dx - dy2, PathPointTypeBezier);
2077 *last_point = add_path_list_node(*last_point, endpoint->X - dx - dy2,
2078 endpoint->Y - dy + dx2, PathPointTypeBezier);
2079 *last_point = add_path_list_node(*last_point, endpoint->X - dx,
2080 endpoint->Y - dy, PathPointTypeBezier);
2082 /* fourth 90-degree arc */
2083 *last_point = add_path_list_node(*last_point, endpoint->X - dx + dy2,
2084 endpoint->Y - dy - dx2, PathPointTypeBezier);
2085 *last_point = add_path_list_node(*last_point, endpoint->X + dy - dx2,
2086 endpoint->Y - dx - dy2, PathPointTypeBezier);
2087 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
2088 endpoint->Y - dx, PathPointTypeBezier);
2090 break;
2092 case LineCapDiamondAnchor:
2094 REAL segment_dy = nextpoint->Y-endpoint->Y;
2095 REAL segment_dx = nextpoint->X-endpoint->X;
2096 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2097 REAL par_dx, par_dy;
2098 REAL perp_dx, perp_dy;
2100 par_dx = -pen_width * segment_dx / segment_length;
2101 par_dy = -pen_width * segment_dy / segment_length;
2103 perp_dx = -pen_width * segment_dy / segment_length;
2104 perp_dy = pen_width * segment_dx / segment_length;
2106 *last_point = add_path_list_node(*last_point, endpoint->X + par_dx,
2107 endpoint->Y + par_dy, PathPointTypeStart);
2108 *last_point = add_path_list_node(*last_point, endpoint->X - perp_dx,
2109 endpoint->Y - perp_dy, PathPointTypeLine);
2110 *last_point = add_path_list_node(*last_point, endpoint->X - par_dx,
2111 endpoint->Y - par_dy, PathPointTypeLine);
2112 *last_point = add_path_list_node(*last_point, endpoint->X + perp_dx,
2113 endpoint->Y + perp_dy, PathPointTypeLine);
2114 break;
2116 case LineCapArrowAnchor:
2118 REAL segment_dy = nextpoint->Y - endpoint->Y;
2119 REAL segment_dx = nextpoint->X - endpoint->X;
2120 REAL segment_length = sqrtf(segment_dy * segment_dy + segment_dx * segment_dx);
2121 REAL par_dx = pen_width * segment_dx / segment_length;
2122 REAL par_dy = pen_width * segment_dy / segment_length;
2123 REAL perp_dx = -par_dy;
2124 REAL perp_dy = par_dx;
2126 *last_point = add_path_list_node(*last_point, endpoint->X,
2127 endpoint->Y, PathPointTypeStart);
2128 *last_point = add_path_list_node(*last_point, endpoint->X + SQRT3 * par_dx - perp_dx,
2129 endpoint->Y + SQRT3 * par_dy - perp_dy, PathPointTypeLine);
2130 *last_point = add_path_list_node(*last_point, endpoint->X + SQRT3 * par_dx + perp_dx,
2131 endpoint->Y + SQRT3 * par_dy + perp_dy, PathPointTypeLine);
2132 break;
2134 case LineCapCustom:
2136 REAL segment_dy = nextpoint->Y - endpoint->Y;
2137 REAL segment_dx = nextpoint->X - endpoint->X;
2138 REAL segment_length = sqrtf(segment_dy * segment_dy + segment_dx * segment_dx);
2139 REAL posx, posy;
2140 REAL perp_dx, perp_dy;
2141 REAL sina, cosa;
2142 GpPointF *tmp_points;
2144 if(!custom)
2145 break;
2147 if (custom->type == CustomLineCapTypeAdjustableArrow)
2149 GpAdjustableArrowCap *arrow = (GpAdjustableArrowCap *)custom;
2150 TRACE("GpAdjustableArrowCap middle_inset: %f height: %f width: %f\n",
2151 arrow->middle_inset, arrow->height, arrow->width);
2153 else
2154 TRACE("GpCustomLineCap fill: %d basecap: %d inset: %f join: %d scale: %f pen_width:%f\n",
2155 custom->fill, custom->basecap, custom->inset, custom->join, custom->scale, pen_width);
2157 sina = -pen_width * custom->scale * segment_dx / segment_length;
2158 cosa = pen_width * custom->scale * segment_dy / segment_length;
2160 /* Coordination where cap needs to be drawn */
2161 posx = endpoint->X + sina;
2162 posy = endpoint->Y - cosa;
2164 if (!custom->fill)
2166 tmp_points = heap_alloc_zero(custom->pathdata.Count * sizeof(GpPoint));
2167 if (!tmp_points) {
2168 ERR("Out of memory\n");
2169 return;
2172 for (INT i = 0; i < custom->pathdata.Count; i++)
2174 tmp_points[i].X = posx + custom->pathdata.Points[i].X * cosa + (custom->pathdata.Points[i].Y - 1.0) * sina;
2175 tmp_points[i].Y = posy + custom->pathdata.Points[i].X * sina - (custom->pathdata.Points[i].Y - 1.0) * cosa;
2177 if ((custom->pathdata.Types[custom->pathdata.Count - 1] & PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
2178 widen_closed_figure(tmp_points, 0, custom->pathdata.Count - 1, pen, pen_width, last_point);
2179 else
2180 widen_open_figure(tmp_points, 0, custom->pathdata.Count - 1, pen, pen_width, custom->strokeEndCap, NULL, custom->strokeStartCap, NULL, last_point);
2182 else
2184 for (INT i = 0; i < custom->pathdata.Count; i++)
2186 /* rotation of CustomCap according to line */
2187 perp_dx = custom->pathdata.Points[i].X * cosa + (custom->pathdata.Points[i].Y - 1.0) * sina;
2188 perp_dy = custom->pathdata.Points[i].X * sina - (custom->pathdata.Points[i].Y - 1.0) * cosa;
2189 *last_point = add_path_list_node(*last_point, posx + perp_dx,
2190 posy + perp_dy, custom->pathdata.Types[i]);
2193 /* FIXME: The line should be adjusted by the inset value of the custom cap. */
2194 break;
2198 (*last_point)->type |= PathPointTypeCloseSubpath;
2201 static void widen_open_figure(const GpPointF *points, int start, int end,
2202 GpPen *pen, REAL pen_width, GpLineCap start_cap, GpCustomLineCap* custom_start,
2203 GpLineCap end_cap, GpCustomLineCap* custom_end, path_list_node_t **last_point)
2205 int i;
2206 path_list_node_t *prev_point;
2208 if (end <= start || pen_width == 0.0)
2209 return;
2211 prev_point = *last_point;
2213 widen_cap(&points[start], &points[start+1],
2214 pen_width, start_cap, custom_start, FALSE, TRUE, last_point);
2216 for (i=start+1; i<end; i++)
2217 widen_joint(&points[i-1], &points[i], &points[i+1],
2218 pen, pen_width, last_point);
2220 widen_cap(&points[end], &points[end-1],
2221 pen_width, end_cap, custom_end, TRUE, TRUE, last_point);
2223 for (i=end-1; i>start; i--)
2224 widen_joint(&points[i+1], &points[i], &points[i-1],
2225 pen, pen_width, last_point);
2227 widen_cap(&points[start], &points[start+1],
2228 pen_width, start_cap, custom_start, TRUE, FALSE, last_point);
2230 prev_point->next->type = PathPointTypeStart;
2231 (*last_point)->type |= PathPointTypeCloseSubpath;
2234 static void widen_closed_figure(const GpPointF *points, int start, int end,
2235 GpPen *pen, REAL pen_width, path_list_node_t **last_point)
2237 int i;
2238 path_list_node_t *prev_point;
2240 if (end <= start || pen_width == 0.0)
2241 return;
2243 /* left outline */
2244 prev_point = *last_point;
2246 widen_joint(&points[end], &points[start],
2247 &points[start+1], pen, pen_width, last_point);
2249 for (i=start+1; i<end; i++)
2250 widen_joint(&points[i-1], &points[i],
2251 &points[i+1], pen, pen_width, last_point);
2253 widen_joint(&points[end-1], &points[end],
2254 &points[start], pen, pen_width, last_point);
2256 prev_point->next->type = PathPointTypeStart;
2257 (*last_point)->type |= PathPointTypeCloseSubpath;
2259 /* right outline */
2260 prev_point = *last_point;
2262 widen_joint(&points[start], &points[end],
2263 &points[end-1], pen, pen_width, last_point);
2265 for (i=end-1; i>start; i--)
2266 widen_joint(&points[i+1], &points[i],
2267 &points[i-1], pen, pen_width, last_point);
2269 widen_joint(&points[start+1], &points[start],
2270 &points[end], pen, pen_width, last_point);
2272 prev_point->next->type = PathPointTypeStart;
2273 (*last_point)->type |= PathPointTypeCloseSubpath;
2276 static void widen_dashed_figure(GpPath *path, int start, int end, int closed,
2277 GpPen *pen, REAL pen_width, path_list_node_t **last_point)
2279 int i, j;
2280 REAL dash_pos=0.0;
2281 int dash_index=0;
2282 const REAL *dash_pattern;
2283 REAL *dash_pattern_scaled;
2284 int dash_count;
2285 GpPointF *tmp_points;
2286 REAL segment_dy;
2287 REAL segment_dx;
2288 REAL segment_length;
2289 REAL segment_pos;
2290 int num_tmp_points=0;
2291 int draw_start_cap=0;
2292 static const REAL dash_dot_dot[6] = { 3.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
2294 if (end <= start || pen_width == 0.0)
2295 return;
2297 switch (pen->dash)
2299 case DashStyleDash:
2300 default:
2301 dash_pattern = dash_dot_dot;
2302 dash_count = 2;
2303 break;
2304 case DashStyleDot:
2305 dash_pattern = &dash_dot_dot[2];
2306 dash_count = 2;
2307 break;
2308 case DashStyleDashDot:
2309 dash_pattern = dash_dot_dot;
2310 dash_count = 4;
2311 break;
2312 case DashStyleDashDotDot:
2313 dash_pattern = dash_dot_dot;
2314 dash_count = 6;
2315 break;
2316 case DashStyleCustom:
2317 dash_pattern = pen->dashes;
2318 dash_count = pen->numdashes;
2319 break;
2322 dash_pattern_scaled = heap_alloc(dash_count * sizeof(REAL));
2323 if (!dash_pattern_scaled) return;
2325 for (i = 0; i < dash_count; i++)
2326 dash_pattern_scaled[i] = pen->width * dash_pattern[i];
2328 tmp_points = heap_alloc_zero((end - start + 2) * sizeof(GpPoint));
2329 if (!tmp_points) {
2330 heap_free(dash_pattern_scaled);
2331 return; /* FIXME */
2334 if (!closed)
2335 draw_start_cap = 1;
2337 for (j=start; j <= end; j++)
2339 if (j == start)
2341 if (closed)
2342 i = end;
2343 else
2344 continue;
2346 else
2347 i = j-1;
2349 segment_dy = path->pathdata.Points[j].Y - path->pathdata.Points[i].Y;
2350 segment_dx = path->pathdata.Points[j].X - path->pathdata.Points[i].X;
2351 segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2352 segment_pos = 0.0;
2354 while (1)
2356 if (dash_pos == 0.0)
2358 if ((dash_index % 2) == 0)
2360 /* start dash */
2361 num_tmp_points = 1;
2362 tmp_points[0].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2363 tmp_points[0].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2365 else
2367 /* end dash */
2368 tmp_points[num_tmp_points].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2369 tmp_points[num_tmp_points].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2371 widen_open_figure(tmp_points, 0, num_tmp_points, pen, pen_width,
2372 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2373 LineCapFlat, NULL, last_point);
2374 draw_start_cap = 0;
2375 num_tmp_points = 0;
2379 if (dash_pattern_scaled[dash_index] - dash_pos > segment_length - segment_pos)
2381 /* advance to next segment */
2382 if ((dash_index % 2) == 0)
2384 tmp_points[num_tmp_points] = path->pathdata.Points[j];
2385 num_tmp_points++;
2387 dash_pos += segment_length - segment_pos;
2388 break;
2390 else
2392 /* advance to next dash in pattern */
2393 segment_pos += dash_pattern_scaled[dash_index] - dash_pos;
2394 dash_pos = 0.0;
2395 if (++dash_index == dash_count)
2396 dash_index = 0;
2397 continue;
2402 if (dash_index % 2 == 0 && num_tmp_points != 0)
2404 /* last dash overflows last segment */
2405 widen_open_figure(tmp_points, 0, num_tmp_points-1, pen, pen_width,
2406 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2407 closed ? LineCapFlat : pen->endcap, pen->customend, last_point);
2410 heap_free(dash_pattern_scaled);
2411 heap_free(tmp_points);
2414 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
2415 REAL flatness)
2417 GpPath *flat_path=NULL;
2418 GpStatus status;
2419 path_list_node_t *points=NULL, *last_point=NULL;
2420 int i, subpath_start=0, new_length;
2422 TRACE("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
2424 if (!path || !pen)
2425 return InvalidParameter;
2427 if (path->pathdata.Count <= 1)
2428 return OutOfMemory;
2430 status = GdipClonePath(path, &flat_path);
2432 if (status == Ok)
2433 status = GdipFlattenPath(flat_path, pen->unit == UnitPixel ? matrix : NULL, flatness);
2435 if (status == Ok && !init_path_list(&points, 314.0, 22.0))
2436 status = OutOfMemory;
2438 if (status == Ok)
2440 REAL pen_width = (pen->unit == UnitWorld) ? max(pen->width, 1.0) : pen->width;
2441 BYTE *types = flat_path->pathdata.Types;
2443 last_point = points;
2445 if (pen->dashcap != DashCapFlat)
2446 FIXME("unimplemented dash cap %d\n", pen->dashcap);
2448 if (pen->join == LineJoinRound)
2449 FIXME("unimplemented line join %d\n", pen->join);
2451 if (pen->align != PenAlignmentCenter)
2452 FIXME("unimplemented pen alignment %d\n", pen->align);
2454 if (pen->compound_array_size != 0)
2455 FIXME("unimplemented pen compoundline. Solid line will be drawn instead: %d\n", pen->compound_array_size);
2457 for (i=0; i < flat_path->pathdata.Count; i++)
2459 if ((types[i]&PathPointTypePathTypeMask) == PathPointTypeStart)
2460 subpath_start = i;
2462 if ((types[i]&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
2464 if (pen->dash != DashStyleSolid)
2465 widen_dashed_figure(flat_path, subpath_start, i, 1, pen, pen_width, &last_point);
2466 else
2467 widen_closed_figure(flat_path->pathdata.Points, subpath_start, i, pen, pen_width, &last_point);
2469 else if (i == flat_path->pathdata.Count-1 ||
2470 (types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
2472 if (pen->dash != DashStyleSolid)
2473 widen_dashed_figure(flat_path, subpath_start, i, 0, pen, pen_width, &last_point);
2474 else
2475 widen_open_figure(flat_path->pathdata.Points, subpath_start, i, pen, pen_width,
2476 pen->startcap, pen->customstart, pen->endcap, pen->customend, &last_point);
2480 for (i=0; i < flat_path->pathdata.Count; i++)
2482 if ((types[i]&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
2483 continue;
2485 if ((types[i]&PathPointTypePathTypeMask) == PathPointTypeStart)
2486 subpath_start = i;
2488 if (i == flat_path->pathdata.Count-1 ||
2489 (types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
2491 if (pen->startcap & LineCapAnchorMask)
2492 add_anchor(&flat_path->pathdata.Points[subpath_start],
2493 &flat_path->pathdata.Points[subpath_start+1],
2494 pen, pen->startcap, pen->customstart, &last_point);
2496 if (pen->endcap & LineCapAnchorMask)
2497 add_anchor(&flat_path->pathdata.Points[i],
2498 &flat_path->pathdata.Points[i-1],
2499 pen, pen->endcap, pen->customend, &last_point);
2503 new_length = path_list_count(points)-1;
2505 if (!lengthen_path(path, new_length))
2506 status = OutOfMemory;
2509 if (status == Ok)
2511 path->pathdata.Count = new_length;
2513 last_point = points->next;
2514 for (i = 0; i < new_length; i++)
2516 path->pathdata.Points[i] = last_point->pt;
2517 path->pathdata.Types[i] = last_point->type;
2518 last_point = last_point->next;
2521 path->fill = FillModeWinding;
2524 free_path_list(points);
2526 GdipDeletePath(flat_path);
2528 if (status == Ok && pen->unit != UnitPixel)
2529 status = GdipTransformPath(path, matrix);
2531 return status;
2534 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
2535 REAL width, REAL height)
2537 GpPath *backup;
2538 GpPointF ptf[2];
2539 GpStatus retstat;
2540 BOOL old_new;
2542 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
2544 if(!path)
2545 return InvalidParameter;
2547 if (width <= 0.0 || height <= 0.0)
2548 return Ok;
2550 /* make a backup copy of path data */
2551 if((retstat = GdipClonePath(path, &backup)) != Ok)
2552 return retstat;
2554 /* rectangle should start as new path */
2555 old_new = path->newfigure;
2556 path->newfigure = TRUE;
2557 if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
2558 path->newfigure = old_new;
2559 goto fail;
2562 ptf[0].X = x+width;
2563 ptf[0].Y = y+height;
2564 ptf[1].X = x;
2565 ptf[1].Y = y+height;
2567 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
2568 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
2570 /* free backup */
2571 GdipDeletePath(backup);
2572 return Ok;
2574 fail:
2575 /* reverting */
2576 heap_free(path->pathdata.Points);
2577 heap_free(path->pathdata.Types);
2578 memcpy(path, backup, sizeof(*path));
2579 heap_free(backup);
2581 return retstat;
2584 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
2585 INT width, INT height)
2587 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
2589 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2592 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
2594 GpPath *backup;
2595 GpStatus retstat;
2596 INT i;
2598 TRACE("(%p, %p, %d)\n", path, rects, count);
2600 /* count == 0 - verified condition */
2601 if(!path || !rects || count == 0)
2602 return InvalidParameter;
2604 if(count < 0)
2605 return OutOfMemory;
2607 /* make a backup copy */
2608 if((retstat = GdipClonePath(path, &backup)) != Ok)
2609 return retstat;
2611 for(i = 0; i < count; i++){
2612 if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
2613 goto fail;
2616 /* free backup */
2617 GdipDeletePath(backup);
2618 return Ok;
2620 fail:
2621 /* reverting */
2622 heap_free(path->pathdata.Points);
2623 heap_free(path->pathdata.Types);
2624 memcpy(path, backup, sizeof(*path));
2625 heap_free(backup);
2627 return retstat;
2630 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
2632 GpRectF *rectsF;
2633 GpStatus retstat;
2634 INT i;
2636 TRACE("(%p, %p, %d)\n", path, rects, count);
2638 if(!rects || count == 0)
2639 return InvalidParameter;
2641 if(count < 0)
2642 return OutOfMemory;
2644 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
2646 for(i = 0;i < count;i++)
2647 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2649 retstat = GdipAddPathRectangles(path, rectsF, count);
2650 heap_free(rectsF);
2652 return retstat;
2655 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
2657 INT count;
2659 TRACE("(%p)\n", path);
2661 if(!path)
2662 return InvalidParameter;
2664 count = path->pathdata.Count;
2666 /* set marker flag */
2667 if(count > 0)
2668 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
2670 return Ok;
2673 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
2675 INT count;
2676 INT i;
2678 TRACE("(%p)\n", path);
2680 if(!path)
2681 return InvalidParameter;
2683 count = path->pathdata.Count;
2685 for(i = 0; i < count - 1; i++){
2686 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
2689 return Ok;
2692 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
2694 FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
2695 return NotImplemented;
2698 #define FLAGS_INTPATH 0x4000
2700 struct path_header
2702 DWORD version;
2703 DWORD count;
2704 DWORD flags;
2707 /* Test to see if the path could be stored as an array of shorts */
2708 static BOOL is_integer_path(const GpPath *path)
2710 int i;
2712 if (!path->pathdata.Count) return FALSE;
2714 for (i = 0; i < path->pathdata.Count; i++)
2716 short x, y;
2717 x = gdip_round(path->pathdata.Points[i].X);
2718 y = gdip_round(path->pathdata.Points[i].Y);
2719 if (path->pathdata.Points[i].X != (REAL)x || path->pathdata.Points[i].Y != (REAL)y)
2720 return FALSE;
2722 return TRUE;
2725 DWORD write_path_data(GpPath *path, void *data)
2727 struct path_header *header = data;
2728 BOOL integer_path = is_integer_path(path);
2729 DWORD i, size;
2730 BYTE *types;
2732 size = sizeof(struct path_header) + path->pathdata.Count;
2733 if (integer_path)
2734 size += sizeof(short[2]) * path->pathdata.Count;
2735 else
2736 size += sizeof(float[2]) * path->pathdata.Count;
2737 size = (size + 3) & ~3;
2739 if (!data) return size;
2741 header->version = VERSION_MAGIC2;
2742 header->count = path->pathdata.Count;
2743 header->flags = integer_path ? FLAGS_INTPATH : 0;
2745 if (integer_path)
2747 short *points = (short*)(header + 1);
2748 for (i = 0; i < path->pathdata.Count; i++)
2750 points[2*i] = path->pathdata.Points[i].X;
2751 points[2*i + 1] = path->pathdata.Points[i].Y;
2753 types = (BYTE*)(points + 2*i);
2755 else
2757 float *points = (float*)(header + 1);
2758 for (i = 0; i < path->pathdata.Count; i++)
2760 points[2*i] = path->pathdata.Points[i].X;
2761 points[2*i + 1] = path->pathdata.Points[i].Y;
2763 types = (BYTE*)(points + 2*i);
2766 for (i=0; i<path->pathdata.Count; i++)
2767 types[i] = path->pathdata.Types[i];
2768 memset(types + i, 0, ((path->pathdata.Count + 3) & ~3) - path->pathdata.Count);
2769 return size;