rasapi32: Update spec file.
[wine/zf.git] / dlls / gdiplus / graphicspath.c
blobce2666eedab9f9457f17418cd706937d5ffc6e10
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,
1084 INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1086 RectF rect;
1088 if (!layoutRect)
1089 return InvalidParameter;
1091 set_rect(&rect, layoutRect->X, layoutRect->Y, layoutRect->Width, layoutRect->Height);
1092 return GdipAddPathString(path, string, length, family, style, emSize, &rect, format);
1095 /*************************************************************************
1096 * GdipClonePath [GDIPLUS.53]
1098 * Duplicate the given path in memory.
1100 * PARAMS
1101 * path [I] The path to be duplicated
1102 * clone [O] Pointer to the new path
1104 * RETURNS
1105 * InvalidParameter If the input path is invalid
1106 * OutOfMemory If allocation of needed memory fails
1107 * Ok If everything works out as expected
1109 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1111 TRACE("(%p, %p)\n", path, clone);
1113 if(!path || !clone)
1114 return InvalidParameter;
1116 *clone = heap_alloc_zero(sizeof(GpPath));
1117 if(!*clone) return OutOfMemory;
1119 **clone = *path;
1121 (*clone)->pathdata.Points = heap_alloc_zero(path->datalen * sizeof(PointF));
1122 (*clone)->pathdata.Types = heap_alloc_zero(path->datalen);
1123 if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1124 heap_free((*clone)->pathdata.Points);
1125 heap_free((*clone)->pathdata.Types);
1126 heap_free(*clone);
1127 return OutOfMemory;
1130 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1131 path->datalen * sizeof(PointF));
1132 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1134 return Ok;
1137 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1139 TRACE("(%p)\n", path);
1141 if(!path)
1142 return InvalidParameter;
1144 if(path->pathdata.Count > 0){
1145 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1146 path->newfigure = TRUE;
1149 return Ok;
1152 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1154 INT i;
1156 TRACE("(%p)\n", path);
1158 if(!path)
1159 return InvalidParameter;
1161 for(i = 1; i < path->pathdata.Count; i++){
1162 if(path->pathdata.Types[i] == PathPointTypeStart)
1163 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1166 path->newfigure = TRUE;
1168 return Ok;
1171 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1173 TRACE("(%d, %p)\n", fill, path);
1175 if(!path)
1176 return InvalidParameter;
1178 *path = heap_alloc_zero(sizeof(GpPath));
1179 if(!*path) return OutOfMemory;
1181 (*path)->fill = fill;
1182 (*path)->newfigure = TRUE;
1184 return Ok;
1187 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1188 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1190 int i;
1192 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1194 if(!points || !types || !path)
1195 return InvalidParameter;
1197 if(count <= 0) {
1198 *path = NULL;
1199 return OutOfMemory;
1202 *path = heap_alloc_zero(sizeof(GpPath));
1203 if(!*path) return OutOfMemory;
1205 if(count > 1 && (types[count-1] & PathPointTypePathTypeMask) == PathPointTypeStart)
1206 count = 0;
1208 for(i = 1; i < count; i++) {
1209 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier) {
1210 if(i+2 < count &&
1211 (types[i+1] & PathPointTypePathTypeMask) == PathPointTypeBezier &&
1212 (types[i+2] & PathPointTypePathTypeMask) == PathPointTypeBezier)
1213 i += 2;
1214 else {
1215 count = 0;
1216 break;
1221 (*path)->pathdata.Points = heap_alloc_zero(count * sizeof(PointF));
1222 (*path)->pathdata.Types = heap_alloc_zero(count);
1224 if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
1225 heap_free((*path)->pathdata.Points);
1226 heap_free((*path)->pathdata.Types);
1227 heap_free(*path);
1228 return OutOfMemory;
1231 memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
1232 memcpy((*path)->pathdata.Types, types, count);
1233 if(count > 0)
1234 (*path)->pathdata.Types[0] = PathPointTypeStart;
1235 (*path)->pathdata.Count = count;
1236 (*path)->datalen = count;
1238 (*path)->fill = fill;
1239 (*path)->newfigure = TRUE;
1241 return Ok;
1244 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1245 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1247 GpPointF *ptF;
1248 GpStatus ret;
1249 INT i;
1251 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1253 ptF = heap_alloc_zero(sizeof(GpPointF)*count);
1255 for(i = 0;i < count; i++){
1256 ptF[i].X = (REAL)points[i].X;
1257 ptF[i].Y = (REAL)points[i].Y;
1260 ret = GdipCreatePath2(ptF, types, count, fill, path);
1262 heap_free(ptF);
1264 return ret;
1267 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1269 TRACE("(%p)\n", path);
1271 if(!path)
1272 return InvalidParameter;
1274 heap_free(path->pathdata.Points);
1275 heap_free(path->pathdata.Types);
1276 heap_free(path);
1278 return Ok;
1281 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1283 path_list_node_t *list, *node;
1284 GpPointF pt;
1285 INT i = 1;
1286 INT startidx = 0;
1287 GpStatus stat;
1289 TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1291 if(!path)
1292 return InvalidParameter;
1294 if(path->pathdata.Count == 0)
1295 return Ok;
1297 stat = GdipTransformPath(path, matrix);
1298 if(stat != Ok)
1299 return stat;
1301 pt = path->pathdata.Points[0];
1302 if(!init_path_list(&list, pt.X, pt.Y))
1303 return OutOfMemory;
1305 node = list;
1307 while(i < path->pathdata.Count){
1309 BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1310 path_list_node_t *start;
1312 pt = path->pathdata.Points[i];
1314 /* save last start point index */
1315 if(type == PathPointTypeStart)
1316 startidx = i;
1318 /* always add line points and start points */
1319 if((type == PathPointTypeStart) || (type == PathPointTypeLine)){
1320 if(!add_path_list_node(node, pt.X, pt.Y, path->pathdata.Types[i]))
1321 goto memout;
1323 node = node->next;
1324 ++i;
1325 continue;
1328 /* Bezier curve */
1330 /* test for closed figure */
1331 if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1332 pt = path->pathdata.Points[startidx];
1333 ++i;
1335 else
1337 i += 2;
1338 pt = path->pathdata.Points[i];
1341 start = node;
1342 /* add Bezier end point */
1343 type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1344 if(!add_path_list_node(node, pt.X, pt.Y, type))
1345 goto memout;
1346 node = node->next;
1348 /* flatten curve */
1349 if(!flatten_bezier(start, path->pathdata.Points[i-2].X, path->pathdata.Points[i-2].Y,
1350 path->pathdata.Points[i-1].X, path->pathdata.Points[i-1].Y,
1351 node, flatness))
1352 goto memout;
1354 ++i;
1355 }/* while */
1357 /* store path data back */
1358 i = path_list_count(list);
1359 if(!lengthen_path(path, i))
1360 goto memout;
1361 path->pathdata.Count = i;
1363 node = list;
1364 for(i = 0; i < path->pathdata.Count; i++){
1365 path->pathdata.Points[i] = node->pt;
1366 path->pathdata.Types[i] = node->type;
1367 node = node->next;
1370 free_path_list(list);
1371 return Ok;
1373 memout:
1374 free_path_list(list);
1375 return OutOfMemory;
1378 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1380 TRACE("(%p, %p)\n", path, pathData);
1382 if(!path || !pathData)
1383 return InvalidParameter;
1385 /* Only copy data. pathData allocation/freeing controlled by wrapper class.
1386 Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
1387 memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
1388 memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
1390 return Ok;
1393 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1395 TRACE("(%p, %p)\n", path, fillmode);
1397 if(!path || !fillmode)
1398 return InvalidParameter;
1400 *fillmode = path->fill;
1402 return Ok;
1405 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1407 INT count;
1409 TRACE("(%p, %p)\n", path, lastPoint);
1411 if(!path || !lastPoint)
1412 return InvalidParameter;
1414 count = path->pathdata.Count;
1415 if(count > 0)
1416 *lastPoint = path->pathdata.Points[count-1];
1418 return Ok;
1421 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1423 TRACE("(%p, %p, %d)\n", path, points, count);
1425 if(!path)
1426 return InvalidParameter;
1428 if(count < path->pathdata.Count)
1429 return InsufficientBuffer;
1431 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1433 return Ok;
1436 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1438 GpStatus ret;
1439 GpPointF *ptf;
1440 INT i;
1442 TRACE("(%p, %p, %d)\n", path, points, count);
1444 if(count <= 0)
1445 return InvalidParameter;
1447 ptf = heap_alloc_zero(sizeof(GpPointF)*count);
1448 if(!ptf) return OutOfMemory;
1450 ret = GdipGetPathPoints(path,ptf,count);
1451 if(ret == Ok)
1452 for(i = 0;i < count;i++){
1453 points[i].X = gdip_round(ptf[i].X);
1454 points[i].Y = gdip_round(ptf[i].Y);
1456 heap_free(ptf);
1458 return ret;
1461 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1463 TRACE("(%p, %p, %d)\n", path, types, count);
1465 if(!path)
1466 return InvalidParameter;
1468 if(count < path->pathdata.Count)
1469 return InsufficientBuffer;
1471 memcpy(types, path->pathdata.Types, path->pathdata.Count);
1473 return Ok;
1476 /* Windows expands the bounding box to the maximum possible bounding box
1477 * for a given pen. For example, if a line join can extend past the point
1478 * it's joining by x units, the bounding box is extended by x units in every
1479 * direction (even though this is too conservative for most cases). */
1480 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
1481 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1483 GpPointF * points, temp_pts[4];
1484 INT count, i;
1485 REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
1487 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1489 /* Matrix and pen can be null. */
1490 if(!path || !bounds)
1491 return InvalidParameter;
1493 /* If path is empty just return. */
1494 count = path->pathdata.Count;
1495 if(count == 0){
1496 bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
1497 return Ok;
1500 points = path->pathdata.Points;
1502 low_x = high_x = points[0].X;
1503 low_y = high_y = points[0].Y;
1505 for(i = 1; i < count; i++){
1506 low_x = min(low_x, points[i].X);
1507 low_y = min(low_y, points[i].Y);
1508 high_x = max(high_x, points[i].X);
1509 high_y = max(high_y, points[i].Y);
1512 width = high_x - low_x;
1513 height = high_y - low_y;
1515 /* This looks unusual but it's the only way I can imitate windows. */
1516 if(matrix){
1517 temp_pts[0].X = low_x;
1518 temp_pts[0].Y = low_y;
1519 temp_pts[1].X = low_x;
1520 temp_pts[1].Y = high_y;
1521 temp_pts[2].X = high_x;
1522 temp_pts[2].Y = high_y;
1523 temp_pts[3].X = high_x;
1524 temp_pts[3].Y = low_y;
1526 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1527 low_x = temp_pts[0].X;
1528 low_y = temp_pts[0].Y;
1530 for(i = 1; i < 4; i++){
1531 low_x = min(low_x, temp_pts[i].X);
1532 low_y = min(low_y, temp_pts[i].Y);
1535 temp = width;
1536 width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
1537 height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
1540 if(pen){
1541 path_width = pen->width / 2.0;
1543 if(count > 2)
1544 path_width = max(path_width, pen->width * pen->miterlimit / 2.0);
1545 /* FIXME: this should probably also check for the startcap */
1546 if(pen->endcap & LineCapNoAnchor)
1547 path_width = max(path_width, pen->width * 2.2);
1549 low_x -= path_width;
1550 low_y -= path_width;
1551 width += 2.0 * path_width;
1552 height += 2.0 * path_width;
1555 bounds->X = low_x;
1556 bounds->Y = low_y;
1557 bounds->Width = width;
1558 bounds->Height = height;
1560 return Ok;
1563 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1564 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1566 GpStatus ret;
1567 GpRectF boundsF;
1569 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1571 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
1573 if(ret == Ok){
1574 bounds->X = gdip_round(boundsF.X);
1575 bounds->Y = gdip_round(boundsF.Y);
1576 bounds->Width = gdip_round(boundsF.Width);
1577 bounds->Height = gdip_round(boundsF.Height);
1580 return ret;
1583 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1585 TRACE("(%p, %p)\n", path, count);
1587 if(!path)
1588 return InvalidParameter;
1590 *count = path->pathdata.Count;
1592 return Ok;
1595 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1597 INT i, count;
1598 INT start = 0; /* position in reversed path */
1599 GpPathData revpath;
1601 TRACE("(%p)\n", path);
1603 if(!path)
1604 return InvalidParameter;
1606 count = path->pathdata.Count;
1608 if(count == 0) return Ok;
1610 revpath.Points = heap_alloc_zero(sizeof(GpPointF)*count);
1611 revpath.Types = heap_alloc_zero(sizeof(BYTE)*count);
1612 revpath.Count = count;
1613 if(!revpath.Points || !revpath.Types){
1614 heap_free(revpath.Points);
1615 heap_free(revpath.Types);
1616 return OutOfMemory;
1619 for(i = 0; i < count; i++){
1621 /* find next start point */
1622 if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1623 INT j;
1624 for(j = start; j <= i; j++){
1625 revpath.Points[j] = path->pathdata.Points[count-j-1];
1626 revpath.Types[j] = path->pathdata.Types[count-j-1];
1628 /* mark start point */
1629 revpath.Types[start] = PathPointTypeStart;
1630 /* set 'figure' endpoint type */
1631 if(i-start > 1){
1632 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1633 revpath.Types[i] |= revpath.Types[i-1];
1635 else
1636 revpath.Types[i] = path->pathdata.Types[start];
1638 start = i+1;
1642 memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1643 memcpy(path->pathdata.Types, revpath.Types, sizeof(BYTE)*count);
1645 heap_free(revpath.Points);
1646 heap_free(revpath.Types);
1648 return Ok;
1651 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1652 GpPen *pen, GpGraphics *graphics, BOOL *result)
1654 TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1656 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1659 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1660 GpPen *pen, GpGraphics *graphics, BOOL *result)
1662 GpStatus stat;
1663 GpPath *wide_path;
1664 GpMatrix *transform = NULL;
1666 TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1668 if(!path || !pen)
1669 return InvalidParameter;
1671 stat = GdipClonePath(path, &wide_path);
1673 if (stat != Ok)
1674 return stat;
1676 if (pen->unit == UnitPixel && graphics != NULL)
1678 stat = GdipCreateMatrix(&transform);
1680 if (stat == Ok)
1681 stat = get_graphics_transform(graphics, CoordinateSpaceDevice,
1682 CoordinateSpaceWorld, transform);
1685 if (stat == Ok)
1686 stat = GdipWidenPath(wide_path, pen, transform, 1.0);
1688 if (pen->unit == UnitPixel && graphics != NULL)
1690 if (stat == Ok)
1691 stat = GdipInvertMatrix(transform);
1693 if (stat == Ok)
1694 stat = GdipTransformPath(wide_path, transform);
1697 if (stat == Ok)
1698 stat = GdipIsVisiblePathPoint(wide_path, x, y, graphics, result);
1700 GdipDeleteMatrix(transform);
1702 GdipDeletePath(wide_path);
1704 return stat;
1707 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1709 TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1711 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1714 /*****************************************************************************
1715 * GdipIsVisiblePathPoint [GDIPLUS.@]
1717 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1719 GpRegion *region;
1720 HRGN hrgn;
1721 GpStatus status;
1723 if(!path || !result) return InvalidParameter;
1725 status = GdipCreateRegionPath(path, &region);
1726 if(status != Ok)
1727 return status;
1729 status = GdipGetRegionHRgn(region, graphics, &hrgn);
1730 if(status != Ok){
1731 GdipDeleteRegion(region);
1732 return status;
1735 *result = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1737 DeleteObject(hrgn);
1738 GdipDeleteRegion(region);
1740 return Ok;
1743 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1745 TRACE("(%p)\n", path);
1747 if(!path)
1748 return InvalidParameter;
1750 path->newfigure = TRUE;
1752 return Ok;
1755 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1757 TRACE("(%p)\n", path);
1759 if(!path)
1760 return InvalidParameter;
1762 path->pathdata.Count = 0;
1763 path->newfigure = TRUE;
1764 path->fill = FillModeAlternate;
1766 return Ok;
1769 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1771 TRACE("(%p, %d)\n", path, fill);
1773 if(!path)
1774 return InvalidParameter;
1776 path->fill = fill;
1778 return Ok;
1781 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1783 TRACE("(%p, %p)\n", path, matrix);
1785 if(!path)
1786 return InvalidParameter;
1788 if(path->pathdata.Count == 0 || !matrix)
1789 return Ok;
1791 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1792 path->pathdata.Count);
1795 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1796 GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1797 REAL height, WarpMode warpmode, REAL flatness)
1799 FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1800 points, count, x, y, width, height, warpmode, flatness);
1802 return NotImplemented;
1805 static void add_bevel_point(const GpPointF *endpoint, const GpPointF *nextpoint,
1806 REAL pen_width, int right_side, path_list_node_t **last_point)
1808 REAL segment_dy = nextpoint->Y-endpoint->Y;
1809 REAL segment_dx = nextpoint->X-endpoint->X;
1810 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1811 REAL distance = pen_width / 2.0;
1812 REAL bevel_dx, bevel_dy;
1814 if (segment_length == 0.0)
1816 *last_point = add_path_list_node(*last_point, endpoint->X,
1817 endpoint->Y, PathPointTypeLine);
1818 return;
1821 if (right_side)
1823 bevel_dx = -distance * segment_dy / segment_length;
1824 bevel_dy = distance * segment_dx / segment_length;
1826 else
1828 bevel_dx = distance * segment_dy / segment_length;
1829 bevel_dy = -distance * segment_dx / segment_length;
1832 *last_point = add_path_list_node(*last_point, endpoint->X + bevel_dx,
1833 endpoint->Y + bevel_dy, PathPointTypeLine);
1836 static void widen_joint(const GpPointF *p1, const GpPointF *p2, const GpPointF *p3,
1837 GpPen* pen, REAL pen_width, path_list_node_t **last_point)
1839 switch (pen->join)
1841 case LineJoinMiter:
1842 case LineJoinMiterClipped:
1843 if ((p2->X - p1->X) * (p3->Y - p1->Y) > (p2->Y - p1->Y) * (p3->X - p1->X))
1845 float distance = pen_width / 2.0;
1846 float length_0 = sqrtf((p2->X-p1->X)*(p2->X-p1->X)+(p2->Y-p1->Y)*(p2->Y-p1->Y));
1847 float length_1 = sqrtf((p3->X-p2->X)*(p3->X-p2->X)+(p3->Y-p2->Y)*(p3->Y-p2->Y));
1848 float dx0 = distance * (p2->X - p1->X) / length_0;
1849 float dy0 = distance * (p2->Y - p1->Y) / length_0;
1850 float dx1 = distance * (p3->X - p2->X) / length_1;
1851 float dy1 = distance * (p3->Y - p2->Y) / length_1;
1852 float det = (dy0*dx1 - dx0*dy1);
1853 float dx = (dx0*dx1*(dx0-dx1) + dy0*dy0*dx1 - dy1*dy1*dx0)/det;
1854 float dy = (dy0*dy1*(dy0-dy1) + dx0*dx0*dy1 - dx1*dx1*dy0)/det;
1855 if (dx*dx + dy*dy < pen->miterlimit*pen->miterlimit * distance*distance)
1857 *last_point = add_path_list_node(*last_point, p2->X + dx,
1858 p2->Y + dy, PathPointTypeLine);
1859 break;
1861 else if (pen->join == LineJoinMiter)
1863 static int once;
1864 if (!once++)
1865 FIXME("should add a clipped corner\n");
1867 /* else fall-through */
1869 /* else fall-through */
1870 default:
1871 case LineJoinBevel:
1872 add_bevel_point(p2, p1, pen_width, 1, last_point);
1873 add_bevel_point(p2, p3, pen_width, 0, last_point);
1874 break;
1878 static void widen_cap(const GpPointF *endpoint, const GpPointF *nextpoint,
1879 REAL pen_width, GpLineCap cap, GpCustomLineCap *custom, int add_first_points,
1880 int add_last_point, path_list_node_t **last_point)
1882 switch (cap)
1884 default:
1885 case LineCapFlat:
1886 if (add_first_points)
1887 add_bevel_point(endpoint, nextpoint, pen_width, 1, last_point);
1888 if (add_last_point)
1889 add_bevel_point(endpoint, nextpoint, pen_width, 0, last_point);
1890 break;
1891 case LineCapSquare:
1893 REAL segment_dy = nextpoint->Y-endpoint->Y;
1894 REAL segment_dx = nextpoint->X-endpoint->X;
1895 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1896 REAL distance = pen_width / 2.0;
1897 REAL bevel_dx, bevel_dy;
1898 REAL extend_dx, extend_dy;
1900 extend_dx = -distance * segment_dx / segment_length;
1901 extend_dy = -distance * segment_dy / segment_length;
1903 bevel_dx = -distance * segment_dy / segment_length;
1904 bevel_dy = distance * segment_dx / segment_length;
1906 if (add_first_points)
1907 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx + bevel_dx,
1908 endpoint->Y + extend_dy + bevel_dy, PathPointTypeLine);
1910 if (add_last_point)
1911 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx - bevel_dx,
1912 endpoint->Y + extend_dy - bevel_dy, PathPointTypeLine);
1914 break;
1916 case LineCapRound:
1918 REAL segment_dy = nextpoint->Y-endpoint->Y;
1919 REAL segment_dx = nextpoint->X-endpoint->X;
1920 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1921 REAL distance = pen_width / 2.0;
1922 REAL dx, dy, dx2, dy2;
1923 const REAL control_point_distance = 0.5522847498307935; /* 4/3 * (sqrt(2) - 1) */
1925 if (add_first_points)
1927 dx = -distance * segment_dx / segment_length;
1928 dy = -distance * segment_dy / segment_length;
1930 dx2 = dx * control_point_distance;
1931 dy2 = dy * control_point_distance;
1933 /* first 90-degree arc */
1934 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
1935 endpoint->Y - dx, PathPointTypeLine);
1937 *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
1938 endpoint->Y - dx + dy2, PathPointTypeBezier);
1940 *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
1941 endpoint->Y + dy - dx2, PathPointTypeBezier);
1943 /* midpoint */
1944 *last_point = add_path_list_node(*last_point, endpoint->X + dx,
1945 endpoint->Y + dy, PathPointTypeBezier);
1947 /* second 90-degree arc */
1948 *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
1949 endpoint->Y + dy + dx2, PathPointTypeBezier);
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 - dy,
1955 endpoint->Y + dx, PathPointTypeBezier);
1957 else if (add_last_point)
1958 add_bevel_point(endpoint, nextpoint, pen_width, 0, last_point);
1959 break;
1961 case LineCapTriangle:
1963 REAL segment_dy = nextpoint->Y-endpoint->Y;
1964 REAL segment_dx = nextpoint->X-endpoint->X;
1965 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1966 REAL distance = pen_width / 2.0;
1967 REAL dx, dy;
1969 dx = distance * segment_dx / segment_length;
1970 dy = distance * segment_dy / segment_length;
1972 if (add_first_points) {
1973 add_bevel_point(endpoint, nextpoint, pen_width, 1, last_point);
1975 *last_point = add_path_list_node(*last_point, endpoint->X - dx,
1976 endpoint->Y - dy, PathPointTypeLine);
1978 if (add_first_points || add_last_point)
1979 add_bevel_point(endpoint, nextpoint, pen_width, 0, last_point);
1980 break;
1985 static void add_anchor(const GpPointF *endpoint, const GpPointF *nextpoint,
1986 REAL pen_width, GpLineCap cap, GpCustomLineCap *custom, path_list_node_t **last_point)
1988 switch (cap)
1990 default:
1991 case LineCapNoAnchor:
1992 return;
1993 case LineCapSquareAnchor:
1995 REAL segment_dy = nextpoint->Y-endpoint->Y;
1996 REAL segment_dx = nextpoint->X-endpoint->X;
1997 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1998 REAL distance = pen_width / sqrtf(2.0);
1999 REAL par_dx, par_dy;
2000 REAL perp_dx, perp_dy;
2002 par_dx = -distance * segment_dx / segment_length;
2003 par_dy = -distance * segment_dy / segment_length;
2005 perp_dx = -distance * segment_dy / segment_length;
2006 perp_dy = distance * segment_dx / segment_length;
2008 *last_point = add_path_list_node(*last_point, endpoint->X - par_dx - perp_dx,
2009 endpoint->Y - par_dy - perp_dy, PathPointTypeStart);
2010 *last_point = add_path_list_node(*last_point, endpoint->X - par_dx + perp_dx,
2011 endpoint->Y - par_dy + perp_dy, PathPointTypeLine);
2012 *last_point = add_path_list_node(*last_point, endpoint->X + par_dx + perp_dx,
2013 endpoint->Y + par_dy + perp_dy, PathPointTypeLine);
2014 *last_point = add_path_list_node(*last_point, endpoint->X + par_dx - perp_dx,
2015 endpoint->Y + par_dy - perp_dy, PathPointTypeLine);
2016 break;
2018 case LineCapRoundAnchor:
2020 REAL segment_dy = nextpoint->Y-endpoint->Y;
2021 REAL segment_dx = nextpoint->X-endpoint->X;
2022 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2023 REAL dx, dy, dx2, dy2;
2024 const REAL control_point_distance = 0.55228475; /* 4/3 * (sqrt(2) - 1) */
2026 dx = -pen_width * segment_dx / segment_length;
2027 dy = -pen_width * segment_dy / segment_length;
2029 dx2 = dx * control_point_distance;
2030 dy2 = dy * control_point_distance;
2032 /* starting point */
2033 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
2034 endpoint->Y - dx, PathPointTypeStart);
2036 /* first 90-degree arc */
2037 *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
2038 endpoint->Y - dx + dy2, PathPointTypeBezier);
2039 *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
2040 endpoint->Y + dy - dx2, PathPointTypeBezier);
2041 *last_point = add_path_list_node(*last_point, endpoint->X + dx,
2042 endpoint->Y + dy, PathPointTypeBezier);
2044 /* second 90-degree arc */
2045 *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
2046 endpoint->Y + dy + dx2, PathPointTypeBezier);
2047 *last_point = add_path_list_node(*last_point, endpoint->X - dy + dx2,
2048 endpoint->Y + dx + dy2, PathPointTypeBezier);
2049 *last_point = add_path_list_node(*last_point, endpoint->X - dy,
2050 endpoint->Y + dx, PathPointTypeBezier);
2052 /* third 90-degree arc */
2053 *last_point = add_path_list_node(*last_point, endpoint->X - dy - dx2,
2054 endpoint->Y + dx - dy2, PathPointTypeBezier);
2055 *last_point = add_path_list_node(*last_point, endpoint->X - dx - dy2,
2056 endpoint->Y - dy + dx2, PathPointTypeBezier);
2057 *last_point = add_path_list_node(*last_point, endpoint->X - dx,
2058 endpoint->Y - dy, PathPointTypeBezier);
2060 /* fourth 90-degree arc */
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 + dy - dx2,
2064 endpoint->Y - dx - dy2, PathPointTypeBezier);
2065 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
2066 endpoint->Y - dx, PathPointTypeBezier);
2068 break;
2070 case LineCapDiamondAnchor:
2072 REAL segment_dy = nextpoint->Y-endpoint->Y;
2073 REAL segment_dx = nextpoint->X-endpoint->X;
2074 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2075 REAL par_dx, par_dy;
2076 REAL perp_dx, perp_dy;
2078 par_dx = -pen_width * segment_dx / segment_length;
2079 par_dy = -pen_width * segment_dy / segment_length;
2081 perp_dx = -pen_width * segment_dy / segment_length;
2082 perp_dy = pen_width * segment_dx / segment_length;
2084 *last_point = add_path_list_node(*last_point, endpoint->X + par_dx,
2085 endpoint->Y + par_dy, PathPointTypeStart);
2086 *last_point = add_path_list_node(*last_point, endpoint->X - perp_dx,
2087 endpoint->Y - perp_dy, PathPointTypeLine);
2088 *last_point = add_path_list_node(*last_point, endpoint->X - par_dx,
2089 endpoint->Y - par_dy, PathPointTypeLine);
2090 *last_point = add_path_list_node(*last_point, endpoint->X + perp_dx,
2091 endpoint->Y + perp_dy, PathPointTypeLine);
2092 break;
2096 (*last_point)->type |= PathPointTypeCloseSubpath;
2099 static void widen_open_figure(const GpPointF *points, int start, int end,
2100 GpPen *pen, REAL pen_width, GpLineCap start_cap, GpCustomLineCap *start_custom,
2101 GpLineCap end_cap, GpCustomLineCap *end_custom, path_list_node_t **last_point)
2103 int i;
2104 path_list_node_t *prev_point;
2106 if (end <= start || pen_width == 0.0)
2107 return;
2109 prev_point = *last_point;
2111 widen_cap(&points[start], &points[start+1],
2112 pen_width, start_cap, start_custom, FALSE, TRUE, last_point);
2114 for (i=start+1; i<end; i++)
2115 widen_joint(&points[i-1], &points[i], &points[i+1],
2116 pen, pen_width, last_point);
2118 widen_cap(&points[end], &points[end-1],
2119 pen_width, end_cap, end_custom, TRUE, TRUE, last_point);
2121 for (i=end-1; i>start; i--)
2122 widen_joint(&points[i+1], &points[i], &points[i-1],
2123 pen, pen_width, last_point);
2125 widen_cap(&points[start], &points[start+1],
2126 pen_width, start_cap, start_custom, TRUE, FALSE, last_point);
2128 prev_point->next->type = PathPointTypeStart;
2129 (*last_point)->type |= PathPointTypeCloseSubpath;
2132 static void widen_closed_figure(GpPath *path, int start, int end,
2133 GpPen *pen, REAL pen_width, path_list_node_t **last_point)
2135 int i;
2136 path_list_node_t *prev_point;
2138 if (end <= start || pen_width == 0.0)
2139 return;
2141 /* left outline */
2142 prev_point = *last_point;
2144 widen_joint(&path->pathdata.Points[end], &path->pathdata.Points[start],
2145 &path->pathdata.Points[start+1], pen, pen_width, last_point);
2147 for (i=start+1; i<end; i++)
2148 widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
2149 &path->pathdata.Points[i+1], pen, pen_width, last_point);
2151 widen_joint(&path->pathdata.Points[end-1], &path->pathdata.Points[end],
2152 &path->pathdata.Points[start], pen, pen_width, last_point);
2154 prev_point->next->type = PathPointTypeStart;
2155 (*last_point)->type |= PathPointTypeCloseSubpath;
2157 /* right outline */
2158 prev_point = *last_point;
2160 widen_joint(&path->pathdata.Points[start], &path->pathdata.Points[end],
2161 &path->pathdata.Points[end-1], pen, pen_width, last_point);
2163 for (i=end-1; i>start; i--)
2164 widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
2165 &path->pathdata.Points[i-1], pen, pen_width, last_point);
2167 widen_joint(&path->pathdata.Points[start+1], &path->pathdata.Points[start],
2168 &path->pathdata.Points[end], pen, pen_width, last_point);
2170 prev_point->next->type = PathPointTypeStart;
2171 (*last_point)->type |= PathPointTypeCloseSubpath;
2174 static void widen_dashed_figure(GpPath *path, int start, int end, int closed,
2175 GpPen *pen, REAL pen_width, path_list_node_t **last_point)
2177 int i, j;
2178 REAL dash_pos=0.0;
2179 int dash_index=0;
2180 const REAL *dash_pattern;
2181 REAL *dash_pattern_scaled;
2182 int dash_count;
2183 GpPointF *tmp_points;
2184 REAL segment_dy;
2185 REAL segment_dx;
2186 REAL segment_length;
2187 REAL segment_pos;
2188 int num_tmp_points=0;
2189 int draw_start_cap=0;
2190 static const REAL dash_dot_dot[6] = { 3.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
2192 if (end <= start || pen_width == 0.0)
2193 return;
2195 switch (pen->dash)
2197 case DashStyleDash:
2198 default:
2199 dash_pattern = dash_dot_dot;
2200 dash_count = 2;
2201 break;
2202 case DashStyleDot:
2203 dash_pattern = &dash_dot_dot[2];
2204 dash_count = 2;
2205 break;
2206 case DashStyleDashDot:
2207 dash_pattern = dash_dot_dot;
2208 dash_count = 4;
2209 break;
2210 case DashStyleDashDotDot:
2211 dash_pattern = dash_dot_dot;
2212 dash_count = 6;
2213 break;
2214 case DashStyleCustom:
2215 dash_pattern = pen->dashes;
2216 dash_count = pen->numdashes;
2217 break;
2220 dash_pattern_scaled = heap_alloc(dash_count * sizeof(REAL));
2221 if (!dash_pattern_scaled) return;
2223 for (i = 0; i < dash_count; i++)
2224 dash_pattern_scaled[i] = pen->width * dash_pattern[i];
2226 tmp_points = heap_alloc_zero((end - start + 2) * sizeof(GpPoint));
2227 if (!tmp_points) {
2228 heap_free(dash_pattern_scaled);
2229 return; /* FIXME */
2232 if (!closed)
2233 draw_start_cap = 1;
2235 for (j=start; j <= end; j++)
2237 if (j == start)
2239 if (closed)
2240 i = end;
2241 else
2242 continue;
2244 else
2245 i = j-1;
2247 segment_dy = path->pathdata.Points[j].Y - path->pathdata.Points[i].Y;
2248 segment_dx = path->pathdata.Points[j].X - path->pathdata.Points[i].X;
2249 segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
2250 segment_pos = 0.0;
2252 while (1)
2254 if (dash_pos == 0.0)
2256 if ((dash_index % 2) == 0)
2258 /* start dash */
2259 num_tmp_points = 1;
2260 tmp_points[0].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2261 tmp_points[0].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2263 else
2265 /* end dash */
2266 tmp_points[num_tmp_points].X = path->pathdata.Points[i].X + segment_dx * segment_pos / segment_length;
2267 tmp_points[num_tmp_points].Y = path->pathdata.Points[i].Y + segment_dy * segment_pos / segment_length;
2269 widen_open_figure(tmp_points, 0, num_tmp_points, pen, pen_width,
2270 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2271 LineCapFlat, NULL, last_point);
2272 draw_start_cap = 0;
2273 num_tmp_points = 0;
2277 if (dash_pattern_scaled[dash_index] - dash_pos > segment_length - segment_pos)
2279 /* advance to next segment */
2280 if ((dash_index % 2) == 0)
2282 tmp_points[num_tmp_points] = path->pathdata.Points[j];
2283 num_tmp_points++;
2285 dash_pos += segment_length - segment_pos;
2286 break;
2288 else
2290 /* advance to next dash in pattern */
2291 segment_pos += dash_pattern_scaled[dash_index] - dash_pos;
2292 dash_pos = 0.0;
2293 if (++dash_index == dash_count)
2294 dash_index = 0;
2295 continue;
2300 if (dash_index % 2 == 0 && num_tmp_points != 0)
2302 /* last dash overflows last segment */
2303 widen_open_figure(tmp_points, 0, num_tmp_points-1, pen, pen_width,
2304 draw_start_cap ? pen->startcap : LineCapFlat, pen->customstart,
2305 closed ? LineCapFlat : pen->endcap, pen->customend, last_point);
2308 heap_free(dash_pattern_scaled);
2309 heap_free(tmp_points);
2312 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
2313 REAL flatness)
2315 GpPath *flat_path=NULL;
2316 GpStatus status;
2317 path_list_node_t *points=NULL, *last_point=NULL;
2318 int i, subpath_start=0, new_length;
2320 TRACE("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
2322 if (!path || !pen)
2323 return InvalidParameter;
2325 if (path->pathdata.Count <= 1)
2326 return OutOfMemory;
2328 status = GdipClonePath(path, &flat_path);
2330 if (status == Ok)
2331 status = GdipFlattenPath(flat_path, pen->unit == UnitPixel ? matrix : NULL, flatness);
2333 if (status == Ok && !init_path_list(&points, 314.0, 22.0))
2334 status = OutOfMemory;
2336 if (status == Ok)
2338 REAL anchor_pen_width = max(pen->width, 2.0);
2339 REAL pen_width = (pen->unit == UnitWorld) ? max(pen->width, 1.0) : pen->width;
2340 BYTE *types = flat_path->pathdata.Types;
2342 last_point = points;
2344 if (pen->endcap > LineCapDiamondAnchor)
2345 FIXME("unimplemented end cap %x\n", pen->endcap);
2347 if (pen->startcap > LineCapDiamondAnchor)
2348 FIXME("unimplemented start cap %x\n", pen->startcap);
2350 if (pen->dashcap != DashCapFlat)
2351 FIXME("unimplemented dash cap %d\n", pen->dashcap);
2353 if (pen->join == LineJoinRound)
2354 FIXME("unimplemented line join %d\n", pen->join);
2356 if (pen->align != PenAlignmentCenter)
2357 FIXME("unimplemented pen alignment %d\n", pen->align);
2359 for (i=0; i < flat_path->pathdata.Count; i++)
2361 if ((types[i]&PathPointTypePathTypeMask) == PathPointTypeStart)
2362 subpath_start = i;
2364 if ((types[i]&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
2366 if (pen->dash != DashStyleSolid)
2367 widen_dashed_figure(flat_path, subpath_start, i, 1, pen, pen_width, &last_point);
2368 else
2369 widen_closed_figure(flat_path, subpath_start, i, pen, pen_width, &last_point);
2371 else if (i == flat_path->pathdata.Count-1 ||
2372 (types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
2374 if (pen->dash != DashStyleSolid)
2375 widen_dashed_figure(flat_path, subpath_start, i, 0, pen, pen_width, &last_point);
2376 else
2377 widen_open_figure(flat_path->pathdata.Points, subpath_start, i, pen, pen_width, pen->startcap, pen->customstart, pen->endcap, pen->customend, &last_point);
2381 for (i=0; i < flat_path->pathdata.Count; i++)
2383 if ((types[i]&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
2384 continue;
2386 if ((types[i]&PathPointTypePathTypeMask) == PathPointTypeStart)
2387 subpath_start = i;
2389 if (i == flat_path->pathdata.Count-1 ||
2390 (types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
2392 if (pen->startcap & LineCapAnchorMask)
2393 add_anchor(&flat_path->pathdata.Points[subpath_start],
2394 &flat_path->pathdata.Points[subpath_start+1],
2395 anchor_pen_width, pen->startcap, pen->customstart, &last_point);
2397 if (pen->endcap & LineCapAnchorMask)
2398 add_anchor(&flat_path->pathdata.Points[i],
2399 &flat_path->pathdata.Points[i-1],
2400 anchor_pen_width, pen->endcap, pen->customend, &last_point);
2404 new_length = path_list_count(points)-1;
2406 if (!lengthen_path(path, new_length))
2407 status = OutOfMemory;
2410 if (status == Ok)
2412 path->pathdata.Count = new_length;
2414 last_point = points->next;
2415 for (i = 0; i < new_length; i++)
2417 path->pathdata.Points[i] = last_point->pt;
2418 path->pathdata.Types[i] = last_point->type;
2419 last_point = last_point->next;
2422 path->fill = FillModeWinding;
2425 free_path_list(points);
2427 GdipDeletePath(flat_path);
2429 if (status == Ok && pen->unit != UnitPixel)
2430 status = GdipTransformPath(path, matrix);
2432 return status;
2435 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
2436 REAL width, REAL height)
2438 GpPath *backup;
2439 GpPointF ptf[2];
2440 GpStatus retstat;
2441 BOOL old_new;
2443 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
2445 if(!path)
2446 return InvalidParameter;
2448 if (width <= 0.0 || height <= 0.0)
2449 return Ok;
2451 /* make a backup copy of path data */
2452 if((retstat = GdipClonePath(path, &backup)) != Ok)
2453 return retstat;
2455 /* rectangle should start as new path */
2456 old_new = path->newfigure;
2457 path->newfigure = TRUE;
2458 if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
2459 path->newfigure = old_new;
2460 goto fail;
2463 ptf[0].X = x+width;
2464 ptf[0].Y = y+height;
2465 ptf[1].X = x;
2466 ptf[1].Y = y+height;
2468 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
2469 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
2471 /* free backup */
2472 GdipDeletePath(backup);
2473 return Ok;
2475 fail:
2476 /* reverting */
2477 heap_free(path->pathdata.Points);
2478 heap_free(path->pathdata.Types);
2479 memcpy(path, backup, sizeof(*path));
2480 heap_free(backup);
2482 return retstat;
2485 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
2486 INT width, INT height)
2488 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
2490 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2493 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
2495 GpPath *backup;
2496 GpStatus retstat;
2497 INT i;
2499 TRACE("(%p, %p, %d)\n", path, rects, count);
2501 /* count == 0 - verified condition */
2502 if(!path || !rects || count == 0)
2503 return InvalidParameter;
2505 if(count < 0)
2506 return OutOfMemory;
2508 /* make a backup copy */
2509 if((retstat = GdipClonePath(path, &backup)) != Ok)
2510 return retstat;
2512 for(i = 0; i < count; i++){
2513 if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
2514 goto fail;
2517 /* free backup */
2518 GdipDeletePath(backup);
2519 return Ok;
2521 fail:
2522 /* reverting */
2523 heap_free(path->pathdata.Points);
2524 heap_free(path->pathdata.Types);
2525 memcpy(path, backup, sizeof(*path));
2526 heap_free(backup);
2528 return retstat;
2531 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
2533 GpRectF *rectsF;
2534 GpStatus retstat;
2535 INT i;
2537 TRACE("(%p, %p, %d)\n", path, rects, count);
2539 if(!rects || count == 0)
2540 return InvalidParameter;
2542 if(count < 0)
2543 return OutOfMemory;
2545 rectsF = heap_alloc_zero(sizeof(GpRectF)*count);
2547 for(i = 0;i < count;i++)
2548 set_rect(&rectsF[i], rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
2550 retstat = GdipAddPathRectangles(path, rectsF, count);
2551 heap_free(rectsF);
2553 return retstat;
2556 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
2558 INT count;
2560 TRACE("(%p)\n", path);
2562 if(!path)
2563 return InvalidParameter;
2565 count = path->pathdata.Count;
2567 /* set marker flag */
2568 if(count > 0)
2569 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
2571 return Ok;
2574 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
2576 INT count;
2577 INT i;
2579 TRACE("(%p)\n", path);
2581 if(!path)
2582 return InvalidParameter;
2584 count = path->pathdata.Count;
2586 for(i = 0; i < count - 1; i++){
2587 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
2590 return Ok;
2593 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
2595 FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
2596 return NotImplemented;
2599 #define FLAGS_INTPATH 0x4000
2601 struct path_header
2603 DWORD version;
2604 DWORD count;
2605 DWORD flags;
2608 /* Test to see if the path could be stored as an array of shorts */
2609 static BOOL is_integer_path(const GpPath *path)
2611 int i;
2613 if (!path->pathdata.Count) return FALSE;
2615 for (i = 0; i < path->pathdata.Count; i++)
2617 short x, y;
2618 x = gdip_round(path->pathdata.Points[i].X);
2619 y = gdip_round(path->pathdata.Points[i].Y);
2620 if (path->pathdata.Points[i].X != (REAL)x || path->pathdata.Points[i].Y != (REAL)y)
2621 return FALSE;
2623 return TRUE;
2626 DWORD write_path_data(GpPath *path, void *data)
2628 struct path_header *header = data;
2629 BOOL integer_path = is_integer_path(path);
2630 DWORD i, size;
2631 BYTE *types;
2633 size = sizeof(struct path_header) + path->pathdata.Count;
2634 if (integer_path)
2635 size += sizeof(short[2]) * path->pathdata.Count;
2636 else
2637 size += sizeof(float[2]) * path->pathdata.Count;
2638 size = (size + 3) & ~3;
2640 if (!data) return size;
2642 header->version = VERSION_MAGIC2;
2643 header->count = path->pathdata.Count;
2644 header->flags = integer_path ? FLAGS_INTPATH : 0;
2646 if (integer_path)
2648 short *points = (short*)(header + 1);
2649 for (i = 0; i < path->pathdata.Count; i++)
2651 points[2*i] = path->pathdata.Points[i].X;
2652 points[2*i + 1] = path->pathdata.Points[i].Y;
2654 types = (BYTE*)(points + 2*i);
2656 else
2658 float *points = (float*)(header + 1);
2659 for (i = 0; i < path->pathdata.Count; i++)
2661 points[2*i] = path->pathdata.Points[i].X;
2662 points[2*i + 1] = path->pathdata.Points[i].Y;
2664 types = (BYTE*)(points + 2*i);
2667 for (i=0; i<path->pathdata.Count; i++)
2668 types[i] = path->pathdata.Types[i];
2669 memset(types + i, 0, ((path->pathdata.Count + 3) & ~3) - path->pathdata.Count);
2670 return size;