combase: Fix the trailing linefeed of a TRACE().
[wine/zf.git] / dlls / gdiplus / graphicspath.c
blob79e231bf1b2ffff36ec3a1d0117e472a7dd7b9a0
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 /* GdipAddPath* helper
171 * Several GdipAddPath functions are expected to add onto an open figure.
172 * So if the first point being added is an exact match to the last point
173 * of the existing line, that point should not be added.
175 * Parameters:
176 * path : path to which points should be added
177 * points : array of points to add
178 * count : number of points to add (at least 1)
179 * type : type of the points being added
181 * Return value:
182 * OutOfMemory : out of memory, could not lengthen path
183 * Ok : success
185 static GpStatus extend_current_figure(GpPath *path, GDIPCONST PointF *points, INT count, BYTE type)
187 INT insert_index = path->pathdata.Count;
188 BYTE first_point_type = (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
190 if(!path->newfigure &&
191 path->pathdata.Points[insert_index-1].X == points[0].X &&
192 path->pathdata.Points[insert_index-1].Y == points[0].Y)
194 points++;
195 count--;
196 first_point_type = type;
199 if(!count)
200 return Ok;
202 if(!lengthen_path(path, count))
203 return OutOfMemory;
205 memcpy(path->pathdata.Points + insert_index, points, sizeof(GpPointF)*count);
206 path->pathdata.Types[insert_index] = first_point_type;
207 memset(path->pathdata.Types + insert_index + 1, type, count - 1);
209 path->newfigure = FALSE;
210 path->pathdata.Count += count;
212 return Ok;
215 /*******************************************************************************
216 * GdipAddPathArc [GDIPLUS.1]
218 * Add an elliptical arc to the given path.
220 * PARAMS
221 * path [I/O] Path that the arc is appended to
222 * x1 [I] X coordinate of the boundary box
223 * y1 [I] Y coordinate of the boundary box
224 * x2 [I] Width of the boundary box
225 * y2 [I] Height of the boundary box
226 * startAngle [I] Starting angle of the arc, clockwise
227 * sweepAngle [I] Angle of the arc, clockwise
229 * RETURNS
230 * InvalidParameter If the given path is invalid
231 * OutOfMemory If memory allocation fails, i.e. the path cannot be lengthened
232 * Ok If everything works out as expected
234 * NOTES
235 * This functions takes the newfigure value of the given path into account,
236 * i.e. the arc is connected to the end of the given path if it was set to
237 * FALSE, otherwise the arc's first point gets the PathPointTypeStart value.
238 * In both cases, the value of newfigure of the given path is FALSE
239 * afterwards.
241 GpStatus WINGDIPAPI GdipAddPathArc(GpPath *path, REAL x1, REAL y1, REAL x2,
242 REAL y2, REAL startAngle, REAL sweepAngle)
244 GpPointF *points;
245 GpStatus status;
246 INT count;
248 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
249 path, x1, y1, x2, y2, startAngle, sweepAngle);
251 if(!path)
252 return InvalidParameter;
254 count = arc2polybezier(NULL, x1, y1, x2, y2, startAngle, sweepAngle);
255 if(count == 0)
256 return Ok;
258 points = heap_alloc_zero(sizeof(GpPointF)*count);
259 if(!points)
260 return OutOfMemory;
262 arc2polybezier(points, x1, y1, x2, y2, startAngle, sweepAngle);
264 status = extend_current_figure(path, points, count, PathPointTypeBezier);
266 heap_free(points);
267 return status;
270 /*******************************************************************************
271 * GdipAddPathArcI [GDUPLUS.2]
273 * See GdipAddPathArc
275 GpStatus WINGDIPAPI GdipAddPathArcI(GpPath *path, INT x1, INT y1, INT x2,
276 INT y2, REAL startAngle, REAL sweepAngle)
278 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
279 path, x1, y1, x2, y2, startAngle, sweepAngle);
281 return GdipAddPathArc(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,startAngle,sweepAngle);
284 GpStatus WINGDIPAPI GdipAddPathBezier(GpPath *path, REAL x1, REAL y1, REAL x2,
285 REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
287 PointF points[4];
289 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
290 path, x1, y1, x2, y2, x3, y3, x4, y4);
292 if(!path)
293 return InvalidParameter;
295 points[0].X = x1;
296 points[0].Y = y1;
297 points[1].X = x2;
298 points[1].Y = y2;
299 points[2].X = x3;
300 points[2].Y = y3;
301 points[3].X = x4;
302 points[3].Y = y4;
304 return extend_current_figure(path, points, 4, PathPointTypeBezier);
307 GpStatus WINGDIPAPI GdipAddPathBezierI(GpPath *path, INT x1, INT y1, INT x2,
308 INT y2, INT x3, INT y3, INT x4, INT y4)
310 TRACE("(%p, %d, %d, %d, %d, %d, %d, %d, %d)\n",
311 path, x1, y1, x2, y2, x3, y3, x4, y4);
313 return GdipAddPathBezier(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,(REAL)x3,(REAL)y3,
314 (REAL)x4,(REAL)y4);
317 GpStatus WINGDIPAPI GdipAddPathBeziers(GpPath *path, GDIPCONST GpPointF *points,
318 INT count)
320 TRACE("(%p, %p, %d)\n", path, points, count);
322 if(!path || !points || ((count - 1) % 3))
323 return InvalidParameter;
325 return extend_current_figure(path, points, count, PathPointTypeBezier);
328 GpStatus WINGDIPAPI GdipAddPathBeziersI(GpPath *path, GDIPCONST GpPoint *points,
329 INT count)
331 GpPointF *ptsF;
332 GpStatus ret;
333 INT i;
335 TRACE("(%p, %p, %d)\n", path, points, count);
337 if(!points || ((count - 1) % 3))
338 return InvalidParameter;
340 ptsF = heap_alloc_zero(sizeof(GpPointF) * count);
341 if(!ptsF)
342 return OutOfMemory;
344 for(i = 0; i < count; i++){
345 ptsF[i].X = (REAL)points[i].X;
346 ptsF[i].Y = (REAL)points[i].Y;
349 ret = GdipAddPathBeziers(path, ptsF, count);
350 heap_free(ptsF);
352 return ret;
355 GpStatus WINGDIPAPI GdipAddPathClosedCurve(GpPath *path, GDIPCONST GpPointF *points,
356 INT count)
358 TRACE("(%p, %p, %d)\n", path, points, count);
360 return GdipAddPathClosedCurve2(path, points, count, 1.0);
363 GpStatus WINGDIPAPI GdipAddPathClosedCurveI(GpPath *path, GDIPCONST GpPoint *points,
364 INT count)
366 TRACE("(%p, %p, %d)\n", path, points, count);
368 return GdipAddPathClosedCurve2I(path, points, count, 1.0);
371 GpStatus WINGDIPAPI GdipAddPathClosedCurve2(GpPath *path, GDIPCONST GpPointF *points,
372 INT count, REAL tension)
374 INT i, len_pt = (count + 1)*3-2;
375 GpPointF *pt;
376 GpPointF *pts;
377 REAL x1, x2, y1, y2;
378 GpStatus stat;
380 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
382 if(!path || !points || count <= 1)
383 return InvalidParameter;
385 pt = heap_alloc_zero(len_pt * sizeof(GpPointF));
386 pts = heap_alloc_zero((count + 1)*sizeof(GpPointF));
387 if(!pt || !pts){
388 heap_free(pt);
389 heap_free(pts);
390 return OutOfMemory;
393 /* copy source points to extend with the last one */
394 memcpy(pts, points, sizeof(GpPointF)*count);
395 pts[count] = pts[0];
397 tension = tension * TENSION_CONST;
399 for(i = 0; i < count-1; i++){
400 calc_curve_bezier(&(pts[i]), tension, &x1, &y1, &x2, &y2);
402 pt[3*i+2].X = x1;
403 pt[3*i+2].Y = y1;
404 pt[3*i+3].X = pts[i+1].X;
405 pt[3*i+3].Y = pts[i+1].Y;
406 pt[3*i+4].X = x2;
407 pt[3*i+4].Y = y2;
410 /* points [len_pt-2] and [0] are calculated
411 separately to connect splines properly */
412 pts[0] = points[count-1];
413 pts[1] = points[0]; /* equals to start and end of a resulting path */
414 pts[2] = points[1];
416 calc_curve_bezier(pts, tension, &x1, &y1, &x2, &y2);
417 pt[len_pt-2].X = x1;
418 pt[len_pt-2].Y = y1;
419 pt[0].X = pts[1].X;
420 pt[0].Y = pts[1].Y;
421 pt[1].X = x2;
422 pt[1].Y = y2;
423 /* close path */
424 pt[len_pt-1].X = pt[0].X;
425 pt[len_pt-1].Y = pt[0].Y;
427 stat = extend_current_figure(path, pt, len_pt, PathPointTypeBezier);
429 /* close figure */
430 if(stat == Ok){
431 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
432 path->newfigure = TRUE;
435 heap_free(pts);
436 heap_free(pt);
438 return stat;
441 GpStatus WINGDIPAPI GdipAddPathClosedCurve2I(GpPath *path, GDIPCONST GpPoint *points,
442 INT count, REAL tension)
444 GpPointF *ptf;
445 INT i;
446 GpStatus stat;
448 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
450 if(!path || !points || count <= 1)
451 return InvalidParameter;
453 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
454 if(!ptf)
455 return OutOfMemory;
457 for(i = 0; i < count; i++){
458 ptf[i].X = (REAL)points[i].X;
459 ptf[i].Y = (REAL)points[i].Y;
462 stat = GdipAddPathClosedCurve2(path, ptf, count, tension);
464 heap_free(ptf);
466 return stat;
469 GpStatus WINGDIPAPI GdipAddPathCurve(GpPath *path, GDIPCONST GpPointF *points, INT count)
471 TRACE("(%p, %p, %d)\n", path, points, count);
473 if(!path || !points || count <= 1)
474 return InvalidParameter;
476 return GdipAddPathCurve2(path, points, count, 1.0);
479 GpStatus WINGDIPAPI GdipAddPathCurveI(GpPath *path, GDIPCONST GpPoint *points, INT count)
481 TRACE("(%p, %p, %d)\n", path, points, count);
483 if(!path || !points || count <= 1)
484 return InvalidParameter;
486 return GdipAddPathCurve2I(path, points, count, 1.0);
489 GpStatus WINGDIPAPI GdipAddPathCurve2(GpPath *path, GDIPCONST GpPointF *points, INT count,
490 REAL tension)
492 INT i, len_pt = count*3-2;
493 GpPointF *pt;
494 REAL x1, x2, y1, y2;
495 GpStatus stat;
497 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
499 if(!path || !points || count <= 1)
500 return InvalidParameter;
502 pt = heap_alloc_zero(len_pt * sizeof(GpPointF));
503 if(!pt)
504 return OutOfMemory;
506 tension = tension * TENSION_CONST;
508 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
509 tension, &x1, &y1);
511 pt[0].X = points[0].X;
512 pt[0].Y = points[0].Y;
513 pt[1].X = x1;
514 pt[1].Y = y1;
516 for(i = 0; i < count-2; i++){
517 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
519 pt[3*i+2].X = x1;
520 pt[3*i+2].Y = y1;
521 pt[3*i+3].X = points[i+1].X;
522 pt[3*i+3].Y = points[i+1].Y;
523 pt[3*i+4].X = x2;
524 pt[3*i+4].Y = y2;
527 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
528 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
530 pt[len_pt-2].X = x1;
531 pt[len_pt-2].Y = y1;
532 pt[len_pt-1].X = points[count-1].X;
533 pt[len_pt-1].Y = points[count-1].Y;
535 stat = extend_current_figure(path, pt, len_pt, PathPointTypeBezier);
537 heap_free(pt);
539 return stat;
542 GpStatus WINGDIPAPI GdipAddPathCurve2I(GpPath *path, GDIPCONST GpPoint *points,
543 INT count, REAL tension)
545 GpPointF *ptf;
546 INT i;
547 GpStatus stat;
549 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
551 if(!path || !points || count <= 1)
552 return InvalidParameter;
554 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
555 if(!ptf)
556 return OutOfMemory;
558 for(i = 0; i < count; i++){
559 ptf[i].X = (REAL)points[i].X;
560 ptf[i].Y = (REAL)points[i].Y;
563 stat = GdipAddPathCurve2(path, ptf, count, tension);
565 heap_free(ptf);
567 return stat;
570 GpStatus WINGDIPAPI GdipAddPathCurve3(GpPath *path, GDIPCONST GpPointF *points,
571 INT count, INT offset, INT nseg, REAL tension)
573 TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
575 if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
576 return InvalidParameter;
578 return GdipAddPathCurve2(path, &points[offset], nseg + 1, tension);
581 GpStatus WINGDIPAPI GdipAddPathCurve3I(GpPath *path, GDIPCONST GpPoint *points,
582 INT count, INT offset, INT nseg, REAL tension)
584 TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
586 if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
587 return InvalidParameter;
589 return GdipAddPathCurve2I(path, &points[offset], nseg + 1, tension);
592 GpStatus WINGDIPAPI GdipAddPathEllipse(GpPath *path, REAL x, REAL y, REAL width,
593 REAL height)
595 INT old_count, numpts;
597 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
599 if(!path)
600 return InvalidParameter;
602 if(!lengthen_path(path, MAX_ARC_PTS))
603 return OutOfMemory;
605 old_count = path->pathdata.Count;
606 if((numpts = arc2polybezier(&path->pathdata.Points[old_count], x, y, width,
607 height, 0.0, 360.0)) != MAX_ARC_PTS){
608 ERR("expected %d points but got %d\n", MAX_ARC_PTS, numpts);
609 return GenericError;
612 memset(&path->pathdata.Types[old_count + 1], PathPointTypeBezier,
613 MAX_ARC_PTS - 1);
615 /* An ellipse is an intrinsic figure (always is its own subpath). */
616 path->pathdata.Types[old_count] = PathPointTypeStart;
617 path->pathdata.Types[old_count + MAX_ARC_PTS - 1] |= PathPointTypeCloseSubpath;
618 path->newfigure = TRUE;
619 path->pathdata.Count += MAX_ARC_PTS;
621 return Ok;
624 GpStatus WINGDIPAPI GdipAddPathEllipseI(GpPath *path, INT x, INT y, INT width,
625 INT height)
627 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
629 return GdipAddPathEllipse(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
632 GpStatus WINGDIPAPI GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
633 INT count)
635 TRACE("(%p, %p, %d)\n", path, points, count);
637 if(!path || !points || count < 1)
638 return InvalidParameter;
640 return extend_current_figure(path, points, count, PathPointTypeLine);
643 GpStatus WINGDIPAPI GdipAddPathLine2I(GpPath *path, GDIPCONST GpPoint *points, INT count)
645 GpPointF *pointsF;
646 INT i;
647 GpStatus stat;
649 TRACE("(%p, %p, %d)\n", path, points, count);
651 if(count <= 0)
652 return InvalidParameter;
654 pointsF = heap_alloc_zero(sizeof(GpPointF) * count);
655 if(!pointsF) return OutOfMemory;
657 for(i = 0;i < count; i++){
658 pointsF[i].X = (REAL)points[i].X;
659 pointsF[i].Y = (REAL)points[i].Y;
662 stat = GdipAddPathLine2(path, pointsF, count);
664 heap_free(pointsF);
666 return stat;
669 /*************************************************************************
670 * GdipAddPathLine [GDIPLUS.21]
672 * Add two points to the given path.
674 * PARAMS
675 * path [I/O] Path that the line is appended to
676 * x1 [I] X coordinate of the first point of the line
677 * y1 [I] Y coordinate of the first point of the line
678 * x2 [I] X coordinate of the second point of the line
679 * y2 [I] Y coordinate of the second point of the line
681 * RETURNS
682 * InvalidParameter If the first parameter is not a valid path
683 * OutOfMemory If the path cannot be lengthened, i.e. memory allocation fails
684 * Ok If everything works out as expected
686 * NOTES
687 * This functions takes the newfigure value of the given path into account,
688 * i.e. the two new points are connected to the end of the given path if it
689 * was set to FALSE, otherwise the first point is given the PathPointTypeStart
690 * value. In both cases, the value of newfigure of the given path is FALSE
691 * afterwards.
693 GpStatus WINGDIPAPI GdipAddPathLine(GpPath *path, REAL x1, REAL y1, REAL x2, REAL y2)
695 PointF points[2];
697 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x1, y1, x2, y2);
699 if(!path)
700 return InvalidParameter;
702 points[0].X = x1;
703 points[0].Y = y1;
704 points[1].X = x2;
705 points[1].Y = y2;
707 return extend_current_figure(path, points, 2, PathPointTypeLine);
710 /*************************************************************************
711 * GdipAddPathLineI [GDIPLUS.21]
713 * See GdipAddPathLine
715 GpStatus WINGDIPAPI GdipAddPathLineI(GpPath *path, INT x1, INT y1, INT x2, INT y2)
717 TRACE("(%p, %d, %d, %d, %d)\n", path, x1, y1, x2, y2);
719 return GdipAddPathLine(path, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
722 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
723 BOOL connect)
725 INT old_count, count;
727 TRACE("(%p, %p, %d)\n", path, addingPath, connect);
729 if(!path || !addingPath)
730 return InvalidParameter;
732 old_count = path->pathdata.Count;
733 count = addingPath->pathdata.Count;
735 if(!lengthen_path(path, count))
736 return OutOfMemory;
738 memcpy(&path->pathdata.Points[old_count], addingPath->pathdata.Points,
739 count * sizeof(GpPointF));
740 memcpy(&path->pathdata.Types[old_count], addingPath->pathdata.Types, count);
742 if(path->newfigure || !connect)
743 path->pathdata.Types[old_count] = PathPointTypeStart;
744 else
745 path->pathdata.Types[old_count] = PathPointTypeLine;
747 path->newfigure = FALSE;
748 path->pathdata.Count += count;
750 return Ok;
753 GpStatus WINGDIPAPI GdipAddPathPie(GpPath *path, REAL x, REAL y, REAL width, REAL height,
754 REAL startAngle, REAL sweepAngle)
756 GpPointF *ptf;
757 GpStatus status;
758 INT i, count;
760 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
761 path, x, y, width, height, startAngle, sweepAngle);
763 if(!path)
764 return InvalidParameter;
766 /* on zero width/height only start point added */
767 if(width <= 1e-7 || height <= 1e-7){
768 if(!lengthen_path(path, 1))
769 return OutOfMemory;
770 path->pathdata.Points[0].X = x + width / 2.0;
771 path->pathdata.Points[0].Y = y + height / 2.0;
772 path->pathdata.Types[0] = PathPointTypeStart | PathPointTypeCloseSubpath;
773 path->pathdata.Count = 1;
774 return InvalidParameter;
777 count = arc2polybezier(NULL, x, y, width, height, startAngle, sweepAngle);
779 if(count == 0)
780 return Ok;
782 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
783 if(!ptf)
784 return OutOfMemory;
786 arc2polybezier(ptf, x, y, width, height, startAngle, sweepAngle);
788 status = GdipAddPathLine(path, x + width/2, y + height/2, ptf[0].X, ptf[0].Y);
789 if(status != Ok){
790 heap_free(ptf);
791 return status;
793 /* one spline is already added as a line endpoint */
794 if(!lengthen_path(path, count - 1)){
795 heap_free(ptf);
796 return OutOfMemory;
799 memcpy(&(path->pathdata.Points[path->pathdata.Count]), &(ptf[1]),sizeof(GpPointF)*(count-1));
800 for(i = 0; i < count-1; i++)
801 path->pathdata.Types[path->pathdata.Count+i] = PathPointTypeBezier;
803 path->pathdata.Count += count-1;
805 GdipClosePathFigure(path);
807 heap_free(ptf);
809 return status;
812 GpStatus WINGDIPAPI GdipAddPathPieI(GpPath *path, INT x, INT y, INT width, INT height,
813 REAL startAngle, REAL sweepAngle)
815 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
816 path, x, y, width, height, startAngle, sweepAngle);
818 return GdipAddPathPie(path, (REAL)x, (REAL)y, (REAL)width, (REAL)height, startAngle, sweepAngle);
821 GpStatus WINGDIPAPI GdipAddPathPolygon(GpPath *path, GDIPCONST GpPointF *points, INT count)
823 INT old_count;
825 TRACE("(%p, %p, %d)\n", path, points, count);
827 if(!path || !points || count < 3)
828 return InvalidParameter;
830 if(!lengthen_path(path, count))
831 return OutOfMemory;
833 old_count = path->pathdata.Count;
835 memcpy(&path->pathdata.Points[old_count], points, count*sizeof(GpPointF));
836 memset(&path->pathdata.Types[old_count + 1], PathPointTypeLine, count - 1);
838 /* A polygon is an intrinsic figure */
839 path->pathdata.Types[old_count] = PathPointTypeStart;
840 path->pathdata.Types[old_count + count - 1] |= PathPointTypeCloseSubpath;
841 path->newfigure = TRUE;
842 path->pathdata.Count += count;
844 return Ok;
847 GpStatus WINGDIPAPI GdipAddPathPolygonI(GpPath *path, GDIPCONST GpPoint *points, INT count)
849 GpPointF *ptf;
850 GpStatus status;
851 INT i;
853 TRACE("(%p, %p, %d)\n", path, points, count);
855 if(!points || count < 3)
856 return InvalidParameter;
858 ptf = heap_alloc_zero(sizeof(GpPointF) * count);
859 if(!ptf)
860 return OutOfMemory;
862 for(i = 0; i < count; i++){
863 ptf[i].X = (REAL)points[i].X;
864 ptf[i].Y = (REAL)points[i].Y;
867 status = GdipAddPathPolygon(path, ptf, count);
869 heap_free(ptf);
871 return status;
874 static float fromfixedpoint(const FIXED v)
876 float f = ((float)v.fract) / (1<<(sizeof(v.fract)*8));
877 f += v.value;
878 return f;
881 struct format_string_args
883 GpPath *path;
884 float maxY;
885 float scale;
886 float ascent;
889 static GpStatus format_string_callback(HDC dc,
890 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
891 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
892 INT lineno, const RectF *bounds, INT *underlined_indexes,
893 INT underlined_index_count, void *priv)
895 static const MAT2 identity = { {0,1}, {0,0}, {0,0}, {0,1} };
896 struct format_string_args *args = priv;
897 GpPath *path = args->path;
898 GpStatus status = Ok;
899 float x = rect->X + (bounds->X - rect->X) * args->scale;
900 float y = rect->Y + (bounds->Y - rect->Y) * args->scale;
901 int i;
903 if (underlined_index_count)
904 FIXME("hotkey underlines not drawn yet\n");
906 if (y + bounds->Height * args->scale > args->maxY)
907 args->maxY = y + bounds->Height * args->scale;
909 for (i = index; i < length; ++i)
911 GLYPHMETRICS gm;
912 TTPOLYGONHEADER *ph = NULL, *origph;
913 char *start;
914 DWORD len, ofs = 0;
915 len = GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, 0, NULL, &identity);
916 if (len == GDI_ERROR)
918 status = GenericError;
919 break;
921 origph = ph = heap_alloc_zero(len);
922 start = (char *)ph;
923 if (!ph || !lengthen_path(path, len / sizeof(POINTFX)))
925 heap_free(ph);
926 status = OutOfMemory;
927 break;
929 GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, len, start, &identity);
931 ofs = 0;
932 while (ofs < len)
934 DWORD ofs_start = ofs;
935 ph = (TTPOLYGONHEADER*)&start[ofs];
936 path->pathdata.Types[path->pathdata.Count] = PathPointTypeStart;
937 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(ph->pfxStart.x) * args->scale;
938 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(ph->pfxStart.y) * args->scale;
939 TRACE("Starting at count %i with pos %f, %f)\n", path->pathdata.Count, x, y);
940 ofs += sizeof(*ph);
941 while (ofs - ofs_start < ph->cb)
943 TTPOLYCURVE *curve = (TTPOLYCURVE*)&start[ofs];
944 int j;
945 ofs += sizeof(TTPOLYCURVE) + (curve->cpfx - 1) * sizeof(POINTFX);
947 switch (curve->wType)
949 case TT_PRIM_LINE:
950 for (j = 0; j < curve->cpfx; ++j)
952 path->pathdata.Types[path->pathdata.Count] = PathPointTypeLine;
953 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
954 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
956 break;
957 case TT_PRIM_CSPLINE:
958 for (j = 0; j < curve->cpfx; ++j)
960 path->pathdata.Types[path->pathdata.Count] = PathPointTypeBezier;
961 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
962 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
964 break;
965 default:
966 ERR("Unhandled type: %u\n", curve->wType);
967 status = GenericError;
970 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
972 path->newfigure = TRUE;
973 x += gm.gmCellIncX * args->scale;
974 y += gm.gmCellIncY * args->scale;
976 heap_free(origph);
977 if (status != Ok)
978 break;
981 return status;
984 GpStatus WINGDIPAPI GdipAddPathString(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat* format)
986 GpFont *font;
987 GpStatus status;
988 LOGFONTW lfw;
989 HANDLE hfont;
990 HDC dc;
991 GpGraphics *graphics;
992 GpPath *backup;
993 struct format_string_args args;
994 int i;
995 UINT16 native_height;
996 RectF scaled_layout_rect;
997 TEXTMETRICW textmetric;
999 TRACE("(%p, %s, %d, %p, %d, %f, %p, %p)\n", path, debugstr_w(string), length, family, style, emSize, layoutRect, format);
1000 if (!path || !string || !family || !emSize || !layoutRect || !format)
1001 return InvalidParameter;
1003 status = GdipGetEmHeight(family, style, &native_height);
1004 if (status != Ok)
1005 return status;
1007 scaled_layout_rect.X = layoutRect->X;
1008 scaled_layout_rect.Y = layoutRect->Y;
1009 scaled_layout_rect.Width = layoutRect->Width * native_height / emSize;
1010 scaled_layout_rect.Height = layoutRect->Height * native_height / emSize;
1012 if ((status = GdipClonePath(path, &backup)) != Ok)
1013 return status;
1015 dc = CreateCompatibleDC(0);
1016 status = GdipCreateFromHDC(dc, &graphics);
1017 if (status != Ok)
1019 DeleteDC(dc);
1020 GdipDeletePath(backup);
1021 return status;
1024 status = GdipCreateFont(family, native_height, style, UnitPixel, &font);
1025 if (status != Ok)
1027 GdipDeleteGraphics(graphics);
1028 DeleteDC(dc);
1029 GdipDeletePath(backup);
1030 return status;
1033 get_log_fontW(font, graphics, &lfw);
1034 GdipDeleteFont(font);
1035 GdipDeleteGraphics(graphics);
1037 hfont = CreateFontIndirectW(&lfw);
1038 if (!hfont)
1040 WARN("Failed to create font\n");
1041 DeleteDC(dc);
1042 GdipDeletePath(backup);
1043 return GenericError;
1046 SelectObject(dc, hfont);
1048 GetTextMetricsW(dc, &textmetric);
1050 args.path = path;
1051 args.maxY = 0;
1052 args.scale = emSize / native_height;
1053 args.ascent = textmetric.tmAscent * args.scale;
1054 status = gdip_format_string(dc, string, length, NULL, &scaled_layout_rect,
1055 format, TRUE, format_string_callback, &args);
1057 DeleteDC(dc);
1058 DeleteObject(hfont);
1060 if (status != Ok) /* free backup */
1062 heap_free(path->pathdata.Points);
1063 heap_free(path->pathdata.Types);
1064 *path = *backup;
1065 heap_free(backup);
1066 return status;
1068 if (format->line_align == StringAlignmentCenter && layoutRect->Y + args.maxY < layoutRect->Height)
1070 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1071 inc /= 2;
1072 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1073 path->pathdata.Points[i].Y += inc;
1074 } else if (format->line_align == StringAlignmentFar) {
1075 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1076 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1077 path->pathdata.Points[i].Y += inc;
1079 GdipDeletePath(backup);
1080 return status;
1083 GpStatus WINGDIPAPI GdipAddPathStringI(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1085 if (layoutRect)
1087 RectF layoutRectF = {
1088 (REAL)layoutRect->X,
1089 (REAL)layoutRect->Y,
1090 (REAL)layoutRect->Width,
1091 (REAL)layoutRect->Height
1093 return GdipAddPathString(path, string, length, family, style, emSize, &layoutRectF, format);
1095 return InvalidParameter;
1098 /*************************************************************************
1099 * GdipClonePath [GDIPLUS.53]
1101 * Duplicate the given path in memory.
1103 * PARAMS
1104 * path [I] The path to be duplicated
1105 * clone [O] Pointer to the new path
1107 * RETURNS
1108 * InvalidParameter If the input path is invalid
1109 * OutOfMemory If allocation of needed memory fails
1110 * Ok If everything works out as expected
1112 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1114 TRACE("(%p, %p)\n", path, clone);
1116 if(!path || !clone)
1117 return InvalidParameter;
1119 *clone = heap_alloc_zero(sizeof(GpPath));
1120 if(!*clone) return OutOfMemory;
1122 **clone = *path;
1124 (*clone)->pathdata.Points = heap_alloc_zero(path->datalen * sizeof(PointF));
1125 (*clone)->pathdata.Types = heap_alloc_zero(path->datalen);
1126 if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1127 heap_free((*clone)->pathdata.Points);
1128 heap_free((*clone)->pathdata.Types);
1129 heap_free(*clone);
1130 return OutOfMemory;
1133 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1134 path->datalen * sizeof(PointF));
1135 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1137 return Ok;
1140 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1142 TRACE("(%p)\n", path);
1144 if(!path)
1145 return InvalidParameter;
1147 if(path->pathdata.Count > 0){
1148 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1149 path->newfigure = TRUE;
1152 return Ok;
1155 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1157 INT i;
1159 TRACE("(%p)\n", path);
1161 if(!path)
1162 return InvalidParameter;
1164 for(i = 1; i < path->pathdata.Count; i++){
1165 if(path->pathdata.Types[i] == PathPointTypeStart)
1166 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1169 path->newfigure = TRUE;
1171 return Ok;
1174 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1176 TRACE("(%d, %p)\n", fill, path);
1178 if(!path)
1179 return InvalidParameter;
1181 *path = heap_alloc_zero(sizeof(GpPath));
1182 if(!*path) return OutOfMemory;
1184 (*path)->fill = fill;
1185 (*path)->newfigure = TRUE;
1187 return Ok;
1190 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1191 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1193 int i;
1195 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1197 if(!points || !types || !path)
1198 return InvalidParameter;
1200 if(count <= 0) {
1201 *path = NULL;
1202 return OutOfMemory;
1205 *path = heap_alloc_zero(sizeof(GpPath));
1206 if(!*path) return OutOfMemory;
1208 if(count > 1 && (types[count-1] & PathPointTypePathTypeMask) == PathPointTypeStart)
1209 count = 0;
1211 for(i = 1; i < count; i++) {
1212 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier) {
1213 if(i+2 < count &&
1214 (types[i+1] & PathPointTypePathTypeMask) == PathPointTypeBezier &&
1215 (types[i+2] & PathPointTypePathTypeMask) == PathPointTypeBezier)
1216 i += 2;
1217 else {
1218 count = 0;
1219 break;
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 if(count > 0)
1237 (*path)->pathdata.Types[0] = PathPointTypeStart;
1238 (*path)->pathdata.Count = count;
1239 (*path)->datalen = count;
1241 (*path)->fill = fill;
1242 (*path)->newfigure = TRUE;
1244 return Ok;
1247 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1248 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1250 GpPointF *ptF;
1251 GpStatus ret;
1252 INT i;
1254 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1256 ptF = heap_alloc_zero(sizeof(GpPointF)*count);
1258 for(i = 0;i < count; i++){
1259 ptF[i].X = (REAL)points[i].X;
1260 ptF[i].Y = (REAL)points[i].Y;
1263 ret = GdipCreatePath2(ptF, types, count, fill, path);
1265 heap_free(ptF);
1267 return ret;
1270 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1272 TRACE("(%p)\n", path);
1274 if(!path)
1275 return InvalidParameter;
1277 heap_free(path->pathdata.Points);
1278 heap_free(path->pathdata.Types);
1279 heap_free(path);
1281 return Ok;
1284 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1286 path_list_node_t *list, *node;
1287 GpPointF pt;
1288 INT i = 1;
1289 INT startidx = 0;
1290 GpStatus stat;
1292 TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1294 if(!path)
1295 return InvalidParameter;
1297 if(path->pathdata.Count == 0)
1298 return Ok;
1300 stat = GdipTransformPath(path, matrix);
1301 if(stat != Ok)
1302 return stat;
1304 pt = path->pathdata.Points[0];
1305 if(!init_path_list(&list, pt.X, pt.Y))
1306 return OutOfMemory;
1308 node = list;
1310 while(i < path->pathdata.Count){
1312 BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1313 path_list_node_t *start;
1315 pt = path->pathdata.Points[i];
1317 /* save last start point index */
1318 if(type == PathPointTypeStart)
1319 startidx = i;
1321 /* always add line points and start points */
1322 if((type == PathPointTypeStart) || (type == PathPointTypeLine)){
1323 if(!add_path_list_node(node, pt.X, pt.Y, path->pathdata.Types[i]))
1324 goto memout;
1326 node = node->next;
1327 ++i;
1328 continue;
1331 /* Bezier curve */
1333 /* test for closed figure */
1334 if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1335 pt = path->pathdata.Points[startidx];
1336 ++i;
1338 else
1340 i += 2;
1341 pt = path->pathdata.Points[i];
1344 start = node;
1345 /* add Bezier end point */
1346 type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1347 if(!add_path_list_node(node, pt.X, pt.Y, type))
1348 goto memout;
1349 node = node->next;
1351 /* flatten curve */
1352 if(!flatten_bezier(start, path->pathdata.Points[i-2].X, path->pathdata.Points[i-2].Y,
1353 path->pathdata.Points[i-1].X, path->pathdata.Points[i-1].Y,
1354 node, flatness))
1355 goto memout;
1357 ++i;
1358 }/* while */
1360 /* store path data back */
1361 i = path_list_count(list);
1362 if(!lengthen_path(path, i))
1363 goto memout;
1364 path->pathdata.Count = i;
1366 node = list;
1367 for(i = 0; i < path->pathdata.Count; i++){
1368 path->pathdata.Points[i] = node->pt;
1369 path->pathdata.Types[i] = node->type;
1370 node = node->next;
1373 free_path_list(list);
1374 return Ok;
1376 memout:
1377 free_path_list(list);
1378 return OutOfMemory;
1381 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1383 TRACE("(%p, %p)\n", path, pathData);
1385 if(!path || !pathData)
1386 return InvalidParameter;
1388 /* Only copy data. pathData allocation/freeing controlled by wrapper class.
1389 Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
1390 memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
1391 memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
1393 return Ok;
1396 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1398 TRACE("(%p, %p)\n", path, fillmode);
1400 if(!path || !fillmode)
1401 return InvalidParameter;
1403 *fillmode = path->fill;
1405 return Ok;
1408 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1410 INT count;
1412 TRACE("(%p, %p)\n", path, lastPoint);
1414 if(!path || !lastPoint)
1415 return InvalidParameter;
1417 count = path->pathdata.Count;
1418 if(count > 0)
1419 *lastPoint = path->pathdata.Points[count-1];
1421 return Ok;
1424 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1426 TRACE("(%p, %p, %d)\n", path, points, count);
1428 if(!path)
1429 return InvalidParameter;
1431 if(count < path->pathdata.Count)
1432 return InsufficientBuffer;
1434 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1436 return Ok;
1439 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1441 GpStatus ret;
1442 GpPointF *ptf;
1443 INT i;
1445 TRACE("(%p, %p, %d)\n", path, points, count);
1447 if(count <= 0)
1448 return InvalidParameter;
1450 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
1451 if(!ptf) return OutOfMemory;
1453 ret = GdipGetPathPoints(path,ptf,count);
1454 if(ret == Ok)
1455 for(i = 0;i < count;i++){
1456 points[i].X = gdip_round(ptf[i].X);
1457 points[i].Y = gdip_round(ptf[i].Y);
1459 heap_free(ptf);
1461 return ret;
1464 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1466 TRACE("(%p, %p, %d)\n", path, types, count);
1468 if(!path)
1469 return InvalidParameter;
1471 if(count < path->pathdata.Count)
1472 return InsufficientBuffer;
1474 memcpy(types, path->pathdata.Types, path->pathdata.Count);
1476 return Ok;
1479 /* Windows expands the bounding box to the maximum possible bounding box
1480 * for a given pen. For example, if a line join can extend past the point
1481 * it's joining by x units, the bounding box is extended by x units in every
1482 * direction (even though this is too conservative for most cases). */
1483 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
1484 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1486 GpPointF * points, temp_pts[4];
1487 INT count, i;
1488 REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
1490 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1492 /* Matrix and pen can be null. */
1493 if(!path || !bounds)
1494 return InvalidParameter;
1496 /* If path is empty just return. */
1497 count = path->pathdata.Count;
1498 if(count == 0){
1499 bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
1500 return Ok;
1503 points = path->pathdata.Points;
1505 low_x = high_x = points[0].X;
1506 low_y = high_y = points[0].Y;
1508 for(i = 1; i < count; i++){
1509 low_x = min(low_x, points[i].X);
1510 low_y = min(low_y, points[i].Y);
1511 high_x = max(high_x, points[i].X);
1512 high_y = max(high_y, points[i].Y);
1515 width = high_x - low_x;
1516 height = high_y - low_y;
1518 /* This looks unusual but it's the only way I can imitate windows. */
1519 if(matrix){
1520 temp_pts[0].X = low_x;
1521 temp_pts[0].Y = low_y;
1522 temp_pts[1].X = low_x;
1523 temp_pts[1].Y = high_y;
1524 temp_pts[2].X = high_x;
1525 temp_pts[2].Y = high_y;
1526 temp_pts[3].X = high_x;
1527 temp_pts[3].Y = low_y;
1529 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1530 low_x = temp_pts[0].X;
1531 low_y = temp_pts[0].Y;
1533 for(i = 1; i < 4; i++){
1534 low_x = min(low_x, temp_pts[i].X);
1535 low_y = min(low_y, temp_pts[i].Y);
1538 temp = width;
1539 width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
1540 height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
1543 if(pen){
1544 path_width = pen->width / 2.0;
1546 if(count > 2)
1547 path_width = max(path_width, pen->width * pen->miterlimit / 2.0);
1548 /* FIXME: this should probably also check for the startcap */
1549 if(pen->endcap & LineCapNoAnchor)
1550 path_width = max(path_width, pen->width * 2.2);
1552 low_x -= path_width;
1553 low_y -= path_width;
1554 width += 2.0 * path_width;
1555 height += 2.0 * path_width;
1558 bounds->X = low_x;
1559 bounds->Y = low_y;
1560 bounds->Width = width;
1561 bounds->Height = height;
1563 return Ok;
1566 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1567 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1569 GpStatus ret;
1570 GpRectF boundsF;
1572 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1574 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
1576 if(ret == Ok){
1577 bounds->X = gdip_round(boundsF.X);
1578 bounds->Y = gdip_round(boundsF.Y);
1579 bounds->Width = gdip_round(boundsF.Width);
1580 bounds->Height = gdip_round(boundsF.Height);
1583 return ret;
1586 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1588 TRACE("(%p, %p)\n", path, count);
1590 if(!path)
1591 return InvalidParameter;
1593 *count = path->pathdata.Count;
1595 return Ok;
1598 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1600 INT i, count;
1601 INT start = 0; /* position in reversed path */
1602 GpPathData revpath;
1604 TRACE("(%p)\n", path);
1606 if(!path)
1607 return InvalidParameter;
1609 count = path->pathdata.Count;
1611 if(count == 0) return Ok;
1613 revpath.Points = heap_alloc_zero(sizeof(GpPointF)*count);
1614 revpath.Types = heap_alloc_zero(sizeof(BYTE)*count);
1615 revpath.Count = count;
1616 if(!revpath.Points || !revpath.Types){
1617 heap_free(revpath.Points);
1618 heap_free(revpath.Types);
1619 return OutOfMemory;
1622 for(i = 0; i < count; i++){
1624 /* find next start point */
1625 if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1626 INT j;
1627 for(j = start; j <= i; j++){
1628 revpath.Points[j] = path->pathdata.Points[count-j-1];
1629 revpath.Types[j] = path->pathdata.Types[count-j-1];
1631 /* mark start point */
1632 revpath.Types[start] = PathPointTypeStart;
1633 /* set 'figure' endpoint type */
1634 if(i-start > 1){
1635 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1636 revpath.Types[i] |= revpath.Types[i-1];
1638 else
1639 revpath.Types[i] = path->pathdata.Types[start];
1641 start = i+1;
1645 memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1646 memcpy(path->pathdata.Types, revpath.Types, sizeof(BYTE)*count);
1648 heap_free(revpath.Points);
1649 heap_free(revpath.Types);
1651 return Ok;
1654 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1655 GpPen *pen, GpGraphics *graphics, BOOL *result)
1657 TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1659 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1662 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1663 GpPen *pen, GpGraphics *graphics, BOOL *result)
1665 GpStatus stat;
1666 GpPath *wide_path;
1667 GpMatrix *transform = NULL;
1669 TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1671 if(!path || !pen)
1672 return InvalidParameter;
1674 stat = GdipClonePath(path, &wide_path);
1676 if (stat != Ok)
1677 return stat;
1679 if (pen->unit == UnitPixel && graphics != NULL)
1681 stat = GdipCreateMatrix(&transform);
1683 if (stat == Ok)
1684 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1685 CoordinateSpaceWorld, transform);
1688 if (stat == Ok)
1689 stat = GdipWidenPath(wide_path, pen, transform, 1.0);
1691 if (pen->unit == UnitPixel && graphics != NULL)
1693 if (stat == Ok)
1694 stat = GdipInvertMatrix(transform);
1696 if (stat == Ok)
1697 stat = GdipTransformPath(wide_path, transform);
1700 if (stat == Ok)
1701 stat = GdipIsVisiblePathPoint(wide_path, x, y, graphics, result);
1703 GdipDeleteMatrix(transform);
1705 GdipDeletePath(wide_path);
1707 return stat;
1710 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1712 TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1714 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1717 /*****************************************************************************
1718 * GdipIsVisiblePathPoint [GDIPLUS.@]
1720 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1722 GpRegion *region;
1723 HRGN hrgn;
1724 GpStatus status;
1726 if(!path || !result) return InvalidParameter;
1728 status = GdipCreateRegionPath(path, &region);
1729 if(status != Ok)
1730 return status;
1732 status = GdipGetRegionHRgn(region, graphics, &hrgn);
1733 if(status != Ok){
1734 GdipDeleteRegion(region);
1735 return status;
1738 *result = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1740 DeleteObject(hrgn);
1741 GdipDeleteRegion(region);
1743 return Ok;
1746 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1748 TRACE("(%p)\n", path);
1750 if(!path)
1751 return InvalidParameter;
1753 path->newfigure = TRUE;
1755 return Ok;
1758 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1760 TRACE("(%p)\n", path);
1762 if(!path)
1763 return InvalidParameter;
1765 path->pathdata.Count = 0;
1766 path->newfigure = TRUE;
1767 path->fill = FillModeAlternate;
1769 return Ok;
1772 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1774 TRACE("(%p, %d)\n", path, fill);
1776 if(!path)
1777 return InvalidParameter;
1779 path->fill = fill;
1781 return Ok;
1784 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1786 TRACE("(%p, %p)\n", path, matrix);
1788 if(!path)
1789 return InvalidParameter;
1791 if(path->pathdata.Count == 0 || !matrix)
1792 return Ok;
1794 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1795 path->pathdata.Count);
1798 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1799 GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1800 REAL height, WarpMode warpmode, REAL flatness)
1802 FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1803 points, count, x, y, width, height, warpmode, flatness);
1805 return NotImplemented;
1808 static void add_bevel_point(const GpPointF *endpoint, const GpPointF *nextpoint,
1809 REAL pen_width, int right_side, path_list_node_t **last_point)
1811 REAL segment_dy = nextpoint->Y-endpoint->Y;
1812 REAL segment_dx = nextpoint->X-endpoint->X;
1813 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1814 REAL distance = pen_width / 2.0;
1815 REAL bevel_dx, bevel_dy;
1817 if (segment_length == 0.0)
1819 *last_point = add_path_list_node(*last_point, endpoint->X,
1820 endpoint->Y, PathPointTypeLine);
1821 return;
1824 if (right_side)
1826 bevel_dx = -distance * segment_dy / segment_length;
1827 bevel_dy = distance * segment_dx / segment_length;
1829 else
1831 bevel_dx = distance * segment_dy / segment_length;
1832 bevel_dy = -distance * segment_dx / segment_length;
1835 *last_point = add_path_list_node(*last_point, endpoint->X + bevel_dx,
1836 endpoint->Y + bevel_dy, PathPointTypeLine);
1839 static void widen_joint(const GpPointF *p1, const GpPointF *p2, const GpPointF *p3,
1840 GpPen* pen, REAL pen_width, path_list_node_t **last_point)
1842 switch (pen->join)
1844 case LineJoinMiter:
1845 case LineJoinMiterClipped:
1846 if ((p2->X - p1->X) * (p3->Y - p1->Y) > (p2->Y - p1->Y) * (p3->X - p1->X))
1848 float distance = pen_width / 2.0;
1849 float length_0 = sqrtf((p2->X-p1->X)*(p2->X-p1->X)+(p2->Y-p1->Y)*(p2->Y-p1->Y));
1850 float length_1 = sqrtf((p3->X-p2->X)*(p3->X-p2->X)+(p3->Y-p2->Y)*(p3->Y-p2->Y));
1851 float dx0 = distance * (p2->X - p1->X) / length_0;
1852 float dy0 = distance * (p2->Y - p1->Y) / length_0;
1853 float dx1 = distance * (p3->X - p2->X) / length_1;
1854 float dy1 = distance * (p3->Y - p2->Y) / length_1;
1855 float det = (dy0*dx1 - dx0*dy1);
1856 float dx = (dx0*dx1*(dx0-dx1) + dy0*dy0*dx1 - dy1*dy1*dx0)/det;
1857 float dy = (dy0*dy1*(dy0-dy1) + dx0*dx0*dy1 - dx1*dx1*dy0)/det;
1858 if (dx*dx + dy*dy < pen->miterlimit*pen->miterlimit * distance*distance)
1860 *last_point = add_path_list_node(*last_point, p2->X + dx,
1861 p2->Y + dy, PathPointTypeLine);
1862 break;
1864 else if (pen->join == LineJoinMiter)
1866 static int once;
1867 if (!once++)
1868 FIXME("should add a clipped corner\n");
1870 /* else fall-through */
1872 /* else fall-through */
1873 default:
1874 case LineJoinBevel:
1875 add_bevel_point(p2, p1, pen_width, 1, last_point);
1876 add_bevel_point(p2, p3, pen_width, 0, last_point);
1877 break;
1881 static void widen_cap(const GpPointF *endpoint, const GpPointF *nextpoint,
1882 REAL pen_width, GpLineCap cap, GpCustomLineCap *custom, int add_first_points,
1883 int add_last_point, path_list_node_t **last_point)
1885 switch (cap)
1887 default:
1888 case LineCapFlat:
1889 if (add_first_points)
1890 add_bevel_point(endpoint, nextpoint, pen_width, 1, last_point);
1891 if (add_last_point)
1892 add_bevel_point(endpoint, nextpoint, pen_width, 0, last_point);
1893 break;
1894 case LineCapSquare:
1896 REAL segment_dy = nextpoint->Y-endpoint->Y;
1897 REAL segment_dx = nextpoint->X-endpoint->X;
1898 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1899 REAL distance = pen_width / 2.0;
1900 REAL bevel_dx, bevel_dy;
1901 REAL extend_dx, extend_dy;
1903 extend_dx = -distance * segment_dx / segment_length;
1904 extend_dy = -distance * segment_dy / segment_length;
1906 bevel_dx = -distance * segment_dy / segment_length;
1907 bevel_dy = distance * segment_dx / segment_length;
1909 if (add_first_points)
1910 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx + bevel_dx,
1911 endpoint->Y + extend_dy + bevel_dy, PathPointTypeLine);
1913 if (add_last_point)
1914 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx - bevel_dx,
1915 endpoint->Y + extend_dy - bevel_dy, PathPointTypeLine);
1917 break;
1919 case LineCapRound:
1921 REAL segment_dy = nextpoint->Y-endpoint->Y;
1922 REAL segment_dx = nextpoint->X-endpoint->X;
1923 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1924 REAL distance = pen_width / 2.0;
1925 REAL dx, dy, dx2, dy2;
1926 const REAL control_point_distance = 0.5522847498307935; /* 4/3 * (sqrt(2) - 1) */
1928 if (add_first_points)
1930 dx = -distance * segment_dx / segment_length;
1931 dy = -distance * segment_dy / segment_length;
1933 dx2 = dx * control_point_distance;
1934 dy2 = dy * control_point_distance;
1936 /* first 90-degree arc */
1937 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
1938 endpoint->Y - dx, PathPointTypeLine);
1940 *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
1941 endpoint->Y - dx + dy2, PathPointTypeBezier);
1943 *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
1944 endpoint->Y + dy - dx2, PathPointTypeBezier);
1946 /* midpoint */
1947 *last_point = add_path_list_node(*last_point, endpoint->X + dx,
1948 endpoint->Y + dy, PathPointTypeBezier);
1950 /* second 90-degree arc */
1951 *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
1952 endpoint->Y + dy + dx2, PathPointTypeBezier);
1954 *last_point = add_path_list_node(*last_point, endpoint->X - dy + dx2,
1955 endpoint->Y + dx + dy2, PathPointTypeBezier);
1957 *last_point = add_path_list_node(*last_point, endpoint->X - dy,
1958 endpoint->Y + dx, PathPointTypeBezier);
1960 else if (add_last_point)
1961 add_bevel_point(endpoint, nextpoint, pen_width, 0, last_point);
1962 break;
1964 case LineCapTriangle:
1966 REAL segment_dy = nextpoint->Y-endpoint->Y;
1967 REAL segment_dx = nextpoint->X-endpoint->X;
1968 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1969 REAL distance = pen_width / 2.0;
1970 REAL dx, dy;
1972 dx = distance * segment_dx / segment_length;
1973 dy = distance * segment_dy / segment_length;
1975 if (add_first_points) {
1976 add_bevel_point(endpoint, nextpoint, pen_width, 1, last_point);
1978 *last_point = add_path_list_node(*last_point, endpoint->X - dx,
1979 endpoint->Y - dy, PathPointTypeLine);
1981 if (add_first_points || add_last_point)
1982 add_bevel_point(endpoint, nextpoint, pen_width, 0, last_point);
1983 break;
1988 static void add_anchor(const GpPointF *endpoint, const GpPointF *nextpoint,
1989 REAL pen_width, GpLineCap cap, GpCustomLineCap *custom, path_list_node_t **last_point)
1991 switch (cap)
1993 default:
1994 case LineCapNoAnchor:
1995 return;
1996 case LineCapSquareAnchor:
1998 REAL segment_dy = nextpoint->Y-endpoint->Y;
1999 REAL segment_dx = nextpoint->X-endpoint->X;
2000 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2001 REAL distance = pen_width / sqrtf(2.0);
2002 REAL par_dx, par_dy;
2003 REAL perp_dx, perp_dy;
2005 par_dx = -distance * segment_dx / segment_length;
2006 par_dy = -distance * segment_dy / segment_length;
2008 perp_dx = -distance * segment_dy / segment_length;
2009 perp_dy = distance * segment_dx / segment_length;
2011 *last_point = add_path_list_node(*last_point, endpoint->X - par_dx - perp_dx,
2012 endpoint->Y - par_dy - perp_dy, PathPointTypeStart);
2013 *last_point = add_path_list_node(*last_point, endpoint->X - par_dx + perp_dx,
2014 endpoint->Y - par_dy + perp_dy, PathPointTypeLine);
2015 *last_point = add_path_list_node(*last_point, endpoint->X + par_dx + perp_dx,
2016 endpoint->Y + par_dy + perp_dy, PathPointTypeLine);
2017 *last_point = add_path_list_node(*last_point, endpoint->X + par_dx - perp_dx,
2018 endpoint->Y + par_dy - perp_dy, PathPointTypeLine);
2019 break;
2021 case LineCapRoundAnchor:
2023 REAL segment_dy = nextpoint->Y-endpoint->Y;
2024 REAL segment_dx = nextpoint->X-endpoint->X;
2025 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2026 REAL dx, dy, dx2, dy2;
2027 const REAL control_point_distance = 0.55228475; /* 4/3 * (sqrt(2) - 1) */
2029 dx = -pen_width * segment_dx / segment_length;
2030 dy = -pen_width * segment_dy / segment_length;
2032 dx2 = dx * control_point_distance;
2033 dy2 = dy * control_point_distance;
2035 /* starting point */
2036 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
2037 endpoint->Y - dx, PathPointTypeStart);
2039 /* first 90-degree arc */
2040 *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
2041 endpoint->Y - dx + dy2, PathPointTypeBezier);
2042 *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
2043 endpoint->Y + dy - dx2, PathPointTypeBezier);
2044 *last_point = add_path_list_node(*last_point, endpoint->X + dx,
2045 endpoint->Y + dy, PathPointTypeBezier);
2047 /* second 90-degree arc */
2048 *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
2049 endpoint->Y + dy + dx2, PathPointTypeBezier);
2050 *last_point = add_path_list_node(*last_point, endpoint->X - dy + dx2,
2051 endpoint->Y + dx + dy2, PathPointTypeBezier);
2052 *last_point = add_path_list_node(*last_point, endpoint->X - dy,
2053 endpoint->Y + dx, PathPointTypeBezier);
2055 /* third 90-degree arc */
2056 *last_point = add_path_list_node(*last_point, endpoint->X - dy - dx2,
2057 endpoint->Y + dx - dy2, PathPointTypeBezier);
2058 *last_point = add_path_list_node(*last_point, endpoint->X - dx - dy2,
2059 endpoint->Y - dy + dx2, PathPointTypeBezier);
2060 *last_point = add_path_list_node(*last_point, endpoint->X - dx,
2061 endpoint->Y - dy, PathPointTypeBezier);
2063 /* fourth 90-degree arc */
2064 *last_point = add_path_list_node(*last_point, endpoint->X - dx + dy2,
2065 endpoint->Y - dy - dx2, PathPointTypeBezier);
2066 *last_point = add_path_list_node(*last_point, endpoint->X + dy - dx2,
2067 endpoint->Y - dx - dy2, PathPointTypeBezier);
2068 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
2069 endpoint->Y - dx, PathPointTypeBezier);
2071 break;
2073 case LineCapDiamondAnchor:
2075 REAL segment_dy = nextpoint->Y-endpoint->Y;
2076 REAL segment_dx = nextpoint->X-endpoint->X;
2077 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2078 REAL par_dx, par_dy;
2079 REAL perp_dx, perp_dy;
2081 par_dx = -pen_width * segment_dx / segment_length;
2082 par_dy = -pen_width * segment_dy / segment_length;
2084 perp_dx = -pen_width * segment_dy / segment_length;
2085 perp_dy = pen_width * segment_dx / segment_length;
2087 *last_point = add_path_list_node(*last_point, endpoint->X + par_dx,
2088 endpoint->Y + par_dy, PathPointTypeStart);
2089 *last_point = add_path_list_node(*last_point, endpoint->X - perp_dx,
2090 endpoint->Y - perp_dy, PathPointTypeLine);
2091 *last_point = add_path_list_node(*last_point, endpoint->X - par_dx,
2092 endpoint->Y - par_dy, PathPointTypeLine);
2093 *last_point = add_path_list_node(*last_point, endpoint->X + perp_dx,
2094 endpoint->Y + perp_dy, PathPointTypeLine);
2095 break;
2099 (*last_point)->type |= PathPointTypeCloseSubpath;
2102 static void widen_open_figure(const GpPointF *points, int start, int end,
2103 GpPen *pen, REAL pen_width, GpLineCap start_cap, GpCustomLineCap *start_custom,
2104 GpLineCap end_cap, GpCustomLineCap *end_custom, path_list_node_t **last_point)
2106 int i;
2107 path_list_node_t *prev_point;
2109 if (end <= start || pen_width == 0.0)
2110 return;
2112 prev_point = *last_point;
2114 widen_cap(&points[start], &points[start+1],
2115 pen_width, start_cap, start_custom, FALSE, TRUE, last_point);
2117 for (i=start+1; i<end; i++)
2118 widen_joint(&points[i-1], &points[i], &points[i+1],
2119 pen, pen_width, last_point);
2121 widen_cap(&points[end], &points[end-1],
2122 pen_width, end_cap, end_custom, TRUE, TRUE, last_point);
2124 for (i=end-1; i>start; i--)
2125 widen_joint(&points[i+1], &points[i], &points[i-1],
2126 pen, pen_width, last_point);
2128 widen_cap(&points[start], &points[start+1],
2129 pen_width, start_cap, start_custom, TRUE, FALSE, last_point);
2131 prev_point->next->type = PathPointTypeStart;
2132 (*last_point)->type |= PathPointTypeCloseSubpath;
2135 static void widen_closed_figure(GpPath *path, int start, int end,
2136 GpPen *pen, REAL pen_width, path_list_node_t **last_point)
2138 int i;
2139 path_list_node_t *prev_point;
2141 if (end <= start || pen_width == 0.0)
2142 return;
2144 /* left outline */
2145 prev_point = *last_point;
2147 widen_joint(&path->pathdata.Points[end], &path->pathdata.Points[start],
2148 &path->pathdata.Points[start+1], pen, pen_width, last_point);
2150 for (i=start+1; i<end; i++)
2151 widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
2152 &path->pathdata.Points[i+1], pen, pen_width, last_point);
2154 widen_joint(&path->pathdata.Points[end-1], &path->pathdata.Points[end],
2155 &path->pathdata.Points[start], pen, pen_width, last_point);
2157 prev_point->next->type = PathPointTypeStart;
2158 (*last_point)->type |= PathPointTypeCloseSubpath;
2160 /* right outline */
2161 prev_point = *last_point;
2163 widen_joint(&path->pathdata.Points[start], &path->pathdata.Points[end],
2164 &path->pathdata.Points[end-1], pen, pen_width, last_point);
2166 for (i=end-1; i>start; i--)
2167 widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
2168 &path->pathdata.Points[i-1], pen, pen_width, last_point);
2170 widen_joint(&path->pathdata.Points[start+1], &path->pathdata.Points[start],
2171 &path->pathdata.Points[end], pen, pen_width, last_point);
2173 prev_point->next->type = PathPointTypeStart;
2174 (*last_point)->type |= PathPointTypeCloseSubpath;
2177 static void widen_dashed_figure(GpPath *path, int start, int end, int closed,
2178 GpPen *pen, REAL pen_width, path_list_node_t **last_point)
2180 int i, j;
2181 REAL dash_pos=0.0;
2182 int dash_index=0;
2183 const REAL *dash_pattern;
2184 REAL *dash_pattern_scaled;
2185 int dash_count;
2186 GpPointF *tmp_points;
2187 REAL segment_dy;
2188 REAL segment_dx;
2189 REAL segment_length;
2190 REAL segment_pos;
2191 int num_tmp_points=0;
2192 int draw_start_cap=0;
2193 static const REAL dash_dot_dot[6] = { 3.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
2195 if (end <= start || pen_width == 0.0)
2196 return;
2198 switch (pen->dash)
2200 case DashStyleDash:
2201 default:
2202 dash_pattern = dash_dot_dot;
2203 dash_count = 2;
2204 break;
2205 case DashStyleDot:
2206 dash_pattern = &dash_dot_dot[2];
2207 dash_count = 2;
2208 break;
2209 case DashStyleDashDot:
2210 dash_pattern = dash_dot_dot;
2211 dash_count = 4;
2212 break;
2213 case DashStyleDashDotDot:
2214 dash_pattern = dash_dot_dot;
2215 dash_count = 6;
2216 break;
2217 case DashStyleCustom:
2218 dash_pattern = pen->dashes;
2219 dash_count = pen->numdashes;
2220 break;
2223 dash_pattern_scaled = heap_alloc(dash_count * sizeof(REAL));
2224 if (!dash_pattern_scaled) return;
2226 for (i = 0; i < dash_count; i++)
2227 dash_pattern_scaled[i] = pen->width * dash_pattern[i];
2229 tmp_points = heap_alloc_zero((end - start + 2) * sizeof(GpPoint));
2230 if (!tmp_points) {
2231 heap_free(dash_pattern_scaled);
2232 return; /* FIXME */
2235 if (!closed)
2236 draw_start_cap = 1;
2238 for (j=start; j <= end; j++)
2240 if (j == start)
2242 if (closed)
2243 i = end;
2244 else
2245 continue;
2247 else
2248 i = j-1;
2250 segment_dy = path->pathdata.Points[j].Y - path->pathdata.Points[i].Y;
2251 segment_dx = path->pathdata.Points[j].X - path->pathdata.Points[i].X;
2252 segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2253 segment_pos = 0.0;
2255 while (1)
2257 if (dash_pos == 0.0)
2259 if ((dash_index % 2) == 0)
2261 /* start dash */
2262 num_tmp_points = 1;
2263 tmp_points[0].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2264 tmp_points[0].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2266 else
2268 /* end dash */
2269 tmp_points[num_tmp_points].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2270 tmp_points[num_tmp_points].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2272 widen_open_figure(tmp_points, 0, num_tmp_points, pen, pen_width,
2273 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2274 LineCapFlat, NULL, last_point);
2275 draw_start_cap = 0;
2276 num_tmp_points = 0;
2280 if (dash_pattern_scaled[dash_index] - dash_pos > segment_length - segment_pos)
2282 /* advance to next segment */
2283 if ((dash_index % 2) == 0)
2285 tmp_points[num_tmp_points] = path->pathdata.Points[j];
2286 num_tmp_points++;
2288 dash_pos += segment_length - segment_pos;
2289 break;
2291 else
2293 /* advance to next dash in pattern */
2294 segment_pos += dash_pattern_scaled[dash_index] - dash_pos;
2295 dash_pos = 0.0;
2296 if (++dash_index == dash_count)
2297 dash_index = 0;
2298 continue;
2303 if (dash_index % 2 == 0 && num_tmp_points != 0)
2305 /* last dash overflows last segment */
2306 widen_open_figure(tmp_points, 0, num_tmp_points-1, pen, pen_width,
2307 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2308 closed ? LineCapFlat : pen->endcap, pen->customend, last_point);
2311 heap_free(dash_pattern_scaled);
2312 heap_free(tmp_points);
2315 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
2316 REAL flatness)
2318 GpPath *flat_path=NULL;
2319 GpStatus status;
2320 path_list_node_t *points=NULL, *last_point=NULL;
2321 int i, subpath_start=0, new_length;
2323 TRACE("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
2325 if (!path || !pen)
2326 return InvalidParameter;
2328 if (path->pathdata.Count <= 1)
2329 return OutOfMemory;
2331 status = GdipClonePath(path, &flat_path);
2333 if (status == Ok)
2334 status = GdipFlattenPath(flat_path, pen->unit == UnitPixel ? matrix : NULL, flatness);
2336 if (status == Ok && !init_path_list(&points, 314.0, 22.0))
2337 status = OutOfMemory;
2339 if (status == Ok)
2341 REAL anchor_pen_width = max(pen->width, 2.0);
2342 REAL pen_width = (pen->unit == UnitWorld) ? max(pen->width, 1.0) : pen->width;
2343 BYTE *types = flat_path->pathdata.Types;
2345 last_point = points;
2347 if (pen->endcap > LineCapDiamondAnchor)
2348 FIXME("unimplemented end cap %x\n", pen->endcap);
2350 if (pen->startcap > LineCapDiamondAnchor)
2351 FIXME("unimplemented start cap %x\n", pen->startcap);
2353 if (pen->dashcap != DashCapFlat)
2354 FIXME("unimplemented dash cap %d\n", pen->dashcap);
2356 if (pen->join == LineJoinRound)
2357 FIXME("unimplemented line join %d\n", pen->join);
2359 if (pen->align != PenAlignmentCenter)
2360 FIXME("unimplemented pen alignment %d\n", pen->align);
2362 for (i=0; i < flat_path->pathdata.Count; i++)
2364 if ((types[i]&PathPointTypePathTypeMask) == PathPointTypeStart)
2365 subpath_start = i;
2367 if ((types[i]&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
2369 if (pen->dash != DashStyleSolid)
2370 widen_dashed_figure(flat_path, subpath_start, i, 1, pen, pen_width, &last_point);
2371 else
2372 widen_closed_figure(flat_path, subpath_start, i, pen, pen_width, &last_point);
2374 else if (i == flat_path->pathdata.Count-1 ||
2375 (types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
2377 if (pen->dash != DashStyleSolid)
2378 widen_dashed_figure(flat_path, subpath_start, i, 0, pen, pen_width, &last_point);
2379 else
2380 widen_open_figure(flat_path->pathdata.Points, subpath_start, i, pen, pen_width, pen->startcap, pen->customstart, pen->endcap, pen->customend, &last_point);
2384 for (i=0; i < flat_path->pathdata.Count; i++)
2386 if ((types[i]&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
2387 continue;
2389 if ((types[i]&PathPointTypePathTypeMask) == PathPointTypeStart)
2390 subpath_start = i;
2392 if (i == flat_path->pathdata.Count-1 ||
2393 (types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
2395 if (pen->startcap & LineCapAnchorMask)
2396 add_anchor(&flat_path->pathdata.Points[subpath_start],
2397 &flat_path->pathdata.Points[subpath_start+1],
2398 anchor_pen_width, pen->startcap, pen->customstart, &last_point);
2400 if (pen->endcap & LineCapAnchorMask)
2401 add_anchor(&flat_path->pathdata.Points[i],
2402 &flat_path->pathdata.Points[i-1],
2403 anchor_pen_width, pen->endcap, pen->customend, &last_point);
2407 new_length = path_list_count(points)-1;
2409 if (!lengthen_path(path, new_length))
2410 status = OutOfMemory;
2413 if (status == Ok)
2415 path->pathdata.Count = new_length;
2417 last_point = points->next;
2418 for (i = 0; i < new_length; i++)
2420 path->pathdata.Points[i] = last_point->pt;
2421 path->pathdata.Types[i] = last_point->type;
2422 last_point = last_point->next;
2425 path->fill = FillModeWinding;
2428 free_path_list(points);
2430 GdipDeletePath(flat_path);
2432 if (status == Ok && pen->unit != UnitPixel)
2433 status = GdipTransformPath(path, matrix);
2435 return status;
2438 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
2439 REAL width, REAL height)
2441 GpPath *backup;
2442 GpPointF ptf[2];
2443 GpStatus retstat;
2444 BOOL old_new;
2446 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
2448 if(!path)
2449 return InvalidParameter;
2451 if (width <= 0.0 || height <= 0.0)
2452 return Ok;
2454 /* make a backup copy of path data */
2455 if((retstat = GdipClonePath(path, &backup)) != Ok)
2456 return retstat;
2458 /* rectangle should start as new path */
2459 old_new = path->newfigure;
2460 path->newfigure = TRUE;
2461 if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
2462 path->newfigure = old_new;
2463 goto fail;
2466 ptf[0].X = x+width;
2467 ptf[0].Y = y+height;
2468 ptf[1].X = x;
2469 ptf[1].Y = y+height;
2471 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
2472 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
2474 /* free backup */
2475 GdipDeletePath(backup);
2476 return Ok;
2478 fail:
2479 /* reverting */
2480 heap_free(path->pathdata.Points);
2481 heap_free(path->pathdata.Types);
2482 memcpy(path, backup, sizeof(*path));
2483 heap_free(backup);
2485 return retstat;
2488 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
2489 INT width, INT height)
2491 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
2493 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2496 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
2498 GpPath *backup;
2499 GpStatus retstat;
2500 INT i;
2502 TRACE("(%p, %p, %d)\n", path, rects, count);
2504 /* count == 0 - verified condition */
2505 if(!path || !rects || count == 0)
2506 return InvalidParameter;
2508 if(count < 0)
2509 return OutOfMemory;
2511 /* make a backup copy */
2512 if((retstat = GdipClonePath(path, &backup)) != Ok)
2513 return retstat;
2515 for(i = 0; i < count; i++){
2516 if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
2517 goto fail;
2520 /* free backup */
2521 GdipDeletePath(backup);
2522 return Ok;
2524 fail:
2525 /* reverting */
2526 heap_free(path->pathdata.Points);
2527 heap_free(path->pathdata.Types);
2528 memcpy(path, backup, sizeof(*path));
2529 heap_free(backup);
2531 return retstat;
2534 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
2536 GpRectF *rectsF;
2537 GpStatus retstat;
2538 INT i;
2540 TRACE("(%p, %p, %d)\n", path, rects, count);
2542 if(!rects || count == 0)
2543 return InvalidParameter;
2545 if(count < 0)
2546 return OutOfMemory;
2548 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
2550 for(i = 0;i < count;i++){
2551 rectsF[i].X = (REAL)rects[i].X;
2552 rectsF[i].Y = (REAL)rects[i].Y;
2553 rectsF[i].Width = (REAL)rects[i].Width;
2554 rectsF[i].Height = (REAL)rects[i].Height;
2557 retstat = GdipAddPathRectangles(path, rectsF, count);
2558 heap_free(rectsF);
2560 return retstat;
2563 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
2565 INT count;
2567 TRACE("(%p)\n", path);
2569 if(!path)
2570 return InvalidParameter;
2572 count = path->pathdata.Count;
2574 /* set marker flag */
2575 if(count > 0)
2576 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
2578 return Ok;
2581 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
2583 INT count;
2584 INT i;
2586 TRACE("(%p)\n", path);
2588 if(!path)
2589 return InvalidParameter;
2591 count = path->pathdata.Count;
2593 for(i = 0; i < count - 1; i++){
2594 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
2597 return Ok;
2600 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
2602 FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
2603 return NotImplemented;
2606 #define FLAGS_INTPATH 0x4000
2608 struct path_header
2610 DWORD version;
2611 DWORD count;
2612 DWORD flags;
2615 /* Test to see if the path could be stored as an array of shorts */
2616 static BOOL is_integer_path(const GpPath *path)
2618 int i;
2620 if (!path->pathdata.Count) return FALSE;
2622 for (i = 0; i < path->pathdata.Count; i++)
2624 short x, y;
2625 x = gdip_round(path->pathdata.Points[i].X);
2626 y = gdip_round(path->pathdata.Points[i].Y);
2627 if (path->pathdata.Points[i].X != (REAL)x || path->pathdata.Points[i].Y != (REAL)y)
2628 return FALSE;
2630 return TRUE;
2633 DWORD write_path_data(GpPath *path, void *data)
2635 struct path_header *header = data;
2636 BOOL integer_path = is_integer_path(path);
2637 DWORD i, size;
2638 BYTE *types;
2640 size = sizeof(struct path_header) + path->pathdata.Count;
2641 if (integer_path)
2642 size += sizeof(short[2]) * path->pathdata.Count;
2643 else
2644 size += sizeof(float[2]) * path->pathdata.Count;
2645 size = (size + 3) & ~3;
2647 if (!data) return size;
2649 header->version = VERSION_MAGIC2;
2650 header->count = path->pathdata.Count;
2651 header->flags = integer_path ? FLAGS_INTPATH : 0;
2653 if (integer_path)
2655 short *points = (short*)(header + 1);
2656 for (i = 0; i < path->pathdata.Count; i++)
2658 points[2*i] = path->pathdata.Points[i].X;
2659 points[2*i + 1] = path->pathdata.Points[i].Y;
2661 types = (BYTE*)(points + 2*i);
2663 else
2665 float *points = (float*)(header + 1);
2666 for (i = 0; i < path->pathdata.Count; i++)
2668 points[2*i] = path->pathdata.Points[i].X;
2669 points[2*i + 1] = path->pathdata.Points[i].Y;
2671 types = (BYTE*)(points + 2*i);
2674 for (i=0; i<path->pathdata.Count; i++)
2675 types[i] = path->pathdata.Types[i];
2676 memset(types + i, 0, ((path->pathdata.Count + 3) & ~3) - path->pathdata.Count);
2677 return size;