gdiplus: Implemented GdipAddPathClosedCurve/GdipAddPathClosedCurveI.
[wine.git] / dlls / gdiplus / graphicspath.c
blob136afe7bfdb0c7523504676184a285a8278a1129
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 /* make sure path has enough space for len more points */
37 static BOOL lengthen_path(GpPath *path, INT len)
39 /* initial allocation */
40 if(path->datalen == 0){
41 path->datalen = len * 2;
43 path->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
44 if(!path->pathdata.Points) return FALSE;
46 path->pathdata.Types = GdipAlloc(path->datalen);
47 if(!path->pathdata.Types){
48 GdipFree(path->pathdata.Points);
49 return FALSE;
52 /* reallocation, double size of arrays */
53 else if(path->datalen - path->pathdata.Count < len){
54 while(path->datalen - path->pathdata.Count < len)
55 path->datalen *= 2;
57 path->pathdata.Points = HeapReAlloc(GetProcessHeap(), 0,
58 path->pathdata.Points, path->datalen * sizeof(PointF));
59 if(!path->pathdata.Points) return FALSE;
61 path->pathdata.Types = HeapReAlloc(GetProcessHeap(), 0,
62 path->pathdata.Types, path->datalen);
63 if(!path->pathdata.Types) return FALSE;
66 return TRUE;
69 GpStatus WINGDIPAPI GdipAddPathArc(GpPath *path, REAL x1, REAL y1, REAL x2,
70 REAL y2, REAL startAngle, REAL sweepAngle)
72 INT count, old_count, i;
74 if(!path)
75 return InvalidParameter;
77 count = arc2polybezier(NULL, x1, y1, x2, y2, startAngle, sweepAngle);
79 if(count == 0)
80 return Ok;
81 if(!lengthen_path(path, count))
82 return OutOfMemory;
84 old_count = path->pathdata.Count;
85 arc2polybezier(&path->pathdata.Points[old_count], x1, y1, x2, y2,
86 startAngle, sweepAngle);
88 for(i = 0; i < count; i++){
89 path->pathdata.Types[old_count + i] = PathPointTypeBezier;
92 path->pathdata.Types[old_count] =
93 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
94 path->newfigure = FALSE;
95 path->pathdata.Count += count;
97 return Ok;
100 GpStatus WINGDIPAPI GdipAddPathArcI(GpPath *path, INT x1, INT y1, INT x2,
101 INT y2, REAL startAngle, REAL sweepAngle)
103 return GdipAddPathArc(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,startAngle,sweepAngle);
106 GpStatus WINGDIPAPI GdipAddPathBezier(GpPath *path, REAL x1, REAL y1, REAL x2,
107 REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
109 INT old_count;
111 if(!path)
112 return InvalidParameter;
114 if(!lengthen_path(path, 4))
115 return OutOfMemory;
117 old_count = path->pathdata.Count;
119 path->pathdata.Points[old_count].X = x1;
120 path->pathdata.Points[old_count].Y = y1;
121 path->pathdata.Points[old_count + 1].X = x2;
122 path->pathdata.Points[old_count + 1].Y = y2;
123 path->pathdata.Points[old_count + 2].X = x3;
124 path->pathdata.Points[old_count + 2].Y = y3;
125 path->pathdata.Points[old_count + 3].X = x4;
126 path->pathdata.Points[old_count + 3].Y = y4;
128 path->pathdata.Types[old_count] =
129 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
130 path->pathdata.Types[old_count + 1] = PathPointTypeBezier;
131 path->pathdata.Types[old_count + 2] = PathPointTypeBezier;
132 path->pathdata.Types[old_count + 3] = PathPointTypeBezier;
134 path->newfigure = FALSE;
135 path->pathdata.Count += 4;
137 return Ok;
140 GpStatus WINGDIPAPI GdipAddPathBezierI(GpPath *path, INT x1, INT y1, INT x2,
141 INT y2, INT x3, INT y3, INT x4, INT y4)
143 return GdipAddPathBezier(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,(REAL)x3,(REAL)y3,
144 (REAL)x4,(REAL)y4);
147 GpStatus WINGDIPAPI GdipAddPathBeziers(GpPath *path, GDIPCONST GpPointF *points,
148 INT count)
150 INT i, old_count;
152 if(!path || !points || ((count - 1) % 3))
153 return InvalidParameter;
155 if(!lengthen_path(path, count))
156 return OutOfMemory;
158 old_count = path->pathdata.Count;
160 for(i = 0; i < count; i++){
161 path->pathdata.Points[old_count + i].X = points[i].X;
162 path->pathdata.Points[old_count + i].Y = points[i].Y;
163 path->pathdata.Types[old_count + i] = PathPointTypeBezier;
166 path->pathdata.Types[old_count] =
167 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
168 path->newfigure = FALSE;
169 path->pathdata.Count += count;
171 return Ok;
174 GpStatus WINGDIPAPI GdipAddPathBeziersI(GpPath *path, GDIPCONST GpPoint *points,
175 INT count)
177 GpPointF *ptsF;
178 GpStatus ret;
179 INT i;
181 if(!points || ((count - 1) % 3))
182 return InvalidParameter;
184 ptsF = GdipAlloc(sizeof(GpPointF) * count);
185 if(!ptsF)
186 return OutOfMemory;
188 for(i = 0; i < count; i++){
189 ptsF[i].X = (REAL)points[i].X;
190 ptsF[i].Y = (REAL)points[i].Y;
193 ret = GdipAddPathBeziers(path, ptsF, count);
194 GdipFree(ptsF);
196 return ret;
199 GpStatus WINGDIPAPI GdipAddPathClosedCurve(GpPath *path, GDIPCONST GpPointF *points,
200 INT count)
202 return GdipAddPathClosedCurve2(path, points, count, 1.0);
205 GpStatus WINGDIPAPI GdipAddPathClosedCurveI(GpPath *path, GDIPCONST GpPoint *points,
206 INT count)
208 return GdipAddPathClosedCurve2I(path, points, count, 1.0);
211 GpStatus WINGDIPAPI GdipAddPathClosedCurve2(GpPath *path, GDIPCONST GpPointF *points,
212 INT count, REAL tension)
214 INT i, len_pt = (count + 1)*3-2;
215 GpPointF *pt;
216 GpPointF *pts;
217 REAL x1, x2, y1, y2;
218 GpStatus stat;
220 if(!path || !points || count <= 1)
221 return InvalidParameter;
223 pt = GdipAlloc(len_pt * sizeof(GpPointF));
224 pts = GdipAlloc((count + 1)*sizeof(GpPointF));
225 if(!pt || !pts){
226 GdipFree(pt);
227 GdipFree(pts);
228 return OutOfMemory;
231 /* copy source points to extend with the last one */
232 memcpy(pts, points, sizeof(GpPointF)*count);
233 pts[count] = pts[0];
235 tension = tension * TENSION_CONST;
237 for(i = 0; i < count-1; i++){
238 calc_curve_bezier(&(pts[i]), tension, &x1, &y1, &x2, &y2);
240 pt[3*i+2].X = x1;
241 pt[3*i+2].Y = y1;
242 pt[3*i+3].X = pts[i+1].X;
243 pt[3*i+3].Y = pts[i+1].Y;
244 pt[3*i+4].X = x2;
245 pt[3*i+4].Y = y2;
248 /* points [len_pt-2] and [0] are calculated
249 separetely to connect splines properly */
250 pts[0] = points[count-1];
251 pts[1] = points[0]; /* equals to start and end of a resulting path */
252 pts[2] = points[1];
254 calc_curve_bezier(pts, tension, &x1, &y1, &x2, &y2);
255 pt[len_pt-2].X = x1;
256 pt[len_pt-2].Y = y1;
257 pt[0].X = pts[1].X;
258 pt[0].Y = pts[1].Y;
259 pt[1].X = x2;
260 pt[1].Y = y2;
261 /* close path */
262 pt[len_pt-1].X = pt[0].X;
263 pt[len_pt-1].Y = pt[0].Y;
265 stat = GdipAddPathBeziers(path, pt, len_pt);
267 /* close figure */
268 if(stat == Ok){
269 INT count = path->pathdata.Count;
270 path->pathdata.Types[count - 1] |= PathPointTypeCloseSubpath;
271 path->newfigure = TRUE;
274 GdipFree(pts);
275 GdipFree(pt);
277 return stat;
280 GpStatus WINGDIPAPI GdipAddPathClosedCurve2I(GpPath *path, GDIPCONST GpPoint *points,
281 INT count, REAL tension)
283 GpPointF *ptf;
284 INT i;
285 GpStatus stat;
287 if(!path || !points || count <= 1)
288 return InvalidParameter;
290 ptf = GdipAlloc(sizeof(GpPointF)*count);
291 if(!ptf)
292 return OutOfMemory;
294 for(i = 0; i < count; i++){
295 ptf[i].X = (REAL)points[i].X;
296 ptf[i].Y = (REAL)points[i].Y;
299 stat = GdipAddPathClosedCurve2(path, ptf, count, tension);
301 GdipFree(ptf);
303 return stat;
306 GpStatus WINGDIPAPI GdipAddPathCurve(GpPath *path, GDIPCONST GpPointF *points, INT count)
308 if(!path || !points || count <= 1)
309 return InvalidParameter;
311 return GdipAddPathCurve2(path, points, count, 1.0);
314 GpStatus WINGDIPAPI GdipAddPathCurveI(GpPath *path, GDIPCONST GpPoint *points, INT count)
316 if(!path || !points || count <= 1)
317 return InvalidParameter;
319 return GdipAddPathCurve2I(path, points, count, 1.0);
322 GpStatus WINGDIPAPI GdipAddPathCurve2(GpPath *path, GDIPCONST GpPointF *points, INT count,
323 REAL tension)
325 INT i, len_pt = count*3-2;
326 GpPointF *pt;
327 REAL x1, x2, y1, y2;
328 GpStatus stat;
330 if(!path || !points || count <= 1)
331 return InvalidParameter;
333 pt = GdipAlloc(len_pt * sizeof(GpPointF));
334 if(!pt)
335 return OutOfMemory;
337 tension = tension * TENSION_CONST;
339 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
340 tension, &x1, &y1);
342 pt[0].X = points[0].X;
343 pt[0].Y = points[0].Y;
344 pt[1].X = x1;
345 pt[1].Y = y1;
347 for(i = 0; i < count-2; i++){
348 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
350 pt[3*i+2].X = x1;
351 pt[3*i+2].Y = y1;
352 pt[3*i+3].X = points[i+1].X;
353 pt[3*i+3].Y = points[i+1].Y;
354 pt[3*i+4].X = x2;
355 pt[3*i+4].Y = y2;
358 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
359 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
361 pt[len_pt-2].X = x1;
362 pt[len_pt-2].Y = y1;
363 pt[len_pt-1].X = points[count-1].X;
364 pt[len_pt-1].Y = points[count-1].Y;
366 stat = GdipAddPathBeziers(path, pt, len_pt);
368 GdipFree(pt);
370 return stat;
373 GpStatus WINGDIPAPI GdipAddPathCurve2I(GpPath *path, GDIPCONST GpPoint *points,
374 INT count, REAL tension)
376 GpPointF *ptf;
377 INT i;
378 GpStatus stat;
380 if(!path || !points || count <= 1)
381 return InvalidParameter;
383 ptf = GdipAlloc(sizeof(GpPointF)*count);
384 if(!ptf)
385 return OutOfMemory;
387 for(i = 0; i < count; i++){
388 ptf[i].X = (REAL)points[i].X;
389 ptf[i].Y = (REAL)points[i].Y;
392 stat = GdipAddPathCurve2(path, ptf, count, tension);
394 GdipFree(ptf);
396 return stat;
399 GpStatus WINGDIPAPI GdipAddPathEllipse(GpPath *path, REAL x, REAL y, REAL width,
400 REAL height)
402 INT old_count, numpts;
404 if(!path)
405 return InvalidParameter;
407 if(!lengthen_path(path, MAX_ARC_PTS))
408 return OutOfMemory;
410 old_count = path->pathdata.Count;
411 if((numpts = arc2polybezier(&path->pathdata.Points[old_count], x, y, width,
412 height, 0.0, 360.0)) != MAX_ARC_PTS){
413 ERR("expected %d points but got %d\n", MAX_ARC_PTS, numpts);
414 return GenericError;
417 memset(&path->pathdata.Types[old_count + 1], PathPointTypeBezier,
418 MAX_ARC_PTS - 1);
420 /* An ellipse is an intrinsic figure (always is its own subpath). */
421 path->pathdata.Types[old_count] = PathPointTypeStart;
422 path->pathdata.Types[old_count + MAX_ARC_PTS - 1] |= PathPointTypeCloseSubpath;
423 path->newfigure = TRUE;
424 path->pathdata.Count += MAX_ARC_PTS;
426 return Ok;
429 GpStatus WINGDIPAPI GdipAddPathEllipseI(GpPath *path, INT x, INT y, INT width,
430 INT height)
432 return GdipAddPathEllipse(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
435 GpStatus WINGDIPAPI GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
436 INT count)
438 INT i, old_count;
440 if(!path || !points)
441 return InvalidParameter;
443 if(!lengthen_path(path, count))
444 return OutOfMemory;
446 old_count = path->pathdata.Count;
448 for(i = 0; i < count; i++){
449 path->pathdata.Points[old_count + i].X = points[i].X;
450 path->pathdata.Points[old_count + i].Y = points[i].Y;
451 path->pathdata.Types[old_count + i] = PathPointTypeLine;
454 if(path->newfigure){
455 path->pathdata.Types[old_count] = PathPointTypeStart;
456 path->newfigure = FALSE;
459 path->pathdata.Count += count;
461 return Ok;
464 GpStatus WINGDIPAPI GdipAddPathLine2I(GpPath *path, GDIPCONST GpPoint *points, INT count)
466 GpPointF *pointsF;
467 INT i;
468 GpStatus stat;
470 if(count <= 0)
471 return InvalidParameter;
473 pointsF = GdipAlloc(sizeof(GpPointF) * count);
474 if(!pointsF) return OutOfMemory;
476 for(i = 0;i < count; i++){
477 pointsF[i].X = (REAL)points[i].X;
478 pointsF[i].Y = (REAL)points[i].Y;
481 stat = GdipAddPathLine2(path, pointsF, count);
483 GdipFree(pointsF);
485 return stat;
488 GpStatus WINGDIPAPI GdipAddPathLine(GpPath *path, REAL x1, REAL y1, REAL x2, REAL y2)
490 INT old_count;
492 if(!path)
493 return InvalidParameter;
495 if(!lengthen_path(path, 2))
496 return OutOfMemory;
498 old_count = path->pathdata.Count;
500 path->pathdata.Points[old_count].X = x1;
501 path->pathdata.Points[old_count].Y = y1;
502 path->pathdata.Points[old_count + 1].X = x2;
503 path->pathdata.Points[old_count + 1].Y = y2;
505 path->pathdata.Types[old_count] =
506 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
507 path->pathdata.Types[old_count + 1] = PathPointTypeLine;
509 path->newfigure = FALSE;
510 path->pathdata.Count += 2;
512 return Ok;
515 GpStatus WINGDIPAPI GdipAddPathLineI(GpPath *path, INT x1, INT y1, INT x2, INT y2)
517 return GdipAddPathLine(path, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
520 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
521 BOOL connect)
523 INT old_count, count;
525 if(!path || !addingPath)
526 return InvalidParameter;
528 old_count = path->pathdata.Count;
529 count = addingPath->pathdata.Count;
531 if(!lengthen_path(path, count))
532 return OutOfMemory;
534 memcpy(&path->pathdata.Points[old_count], addingPath->pathdata.Points,
535 count * sizeof(GpPointF));
536 memcpy(&path->pathdata.Types[old_count], addingPath->pathdata.Types, count);
538 if(path->newfigure || !connect)
539 path->pathdata.Types[old_count] = PathPointTypeStart;
540 else
541 path->pathdata.Types[old_count] = PathPointTypeLine;
543 path->newfigure = FALSE;
544 path->pathdata.Count += count;
546 return Ok;
549 GpStatus WINGDIPAPI GdipAddPathPolygon(GpPath *path, GDIPCONST GpPointF *points, INT count)
551 INT old_count;
553 if(!path || !points || count < 3)
554 return InvalidParameter;
556 if(!lengthen_path(path, count))
557 return OutOfMemory;
559 old_count = path->pathdata.Count;
561 memcpy(&path->pathdata.Points[old_count], points, count*sizeof(GpPointF));
562 memset(&path->pathdata.Types[old_count + 1], PathPointTypeLine, count - 1);
564 /* A polygon is an intrinsic figure */
565 path->pathdata.Types[old_count] = PathPointTypeStart;
566 path->pathdata.Types[old_count + count - 1] |= PathPointTypeCloseSubpath;
567 path->newfigure = TRUE;
568 path->pathdata.Count += count;
570 return Ok;
573 GpStatus WINGDIPAPI GdipAddPathPolygonI(GpPath *path, GDIPCONST GpPoint *points, INT count)
575 GpPointF *ptf;
576 GpStatus status;
577 INT i;
579 if(!points || count < 3)
580 return InvalidParameter;
582 ptf = GdipAlloc(sizeof(GpPointF) * count);
583 if(!ptf)
584 return OutOfMemory;
586 for(i = 0; i < count; i++){
587 ptf[i].X = (REAL)points[i].X;
588 ptf[i].Y = (REAL)points[i].Y;
591 status = GdipAddPathPolygon(path, ptf, count);
593 GdipFree(ptf);
595 return status;
598 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
600 if(!path || !clone)
601 return InvalidParameter;
603 *clone = GdipAlloc(sizeof(GpPath));
604 if(!*clone) return OutOfMemory;
606 **clone = *path;
608 (*clone)->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
609 (*clone)->pathdata.Types = GdipAlloc(path->datalen);
610 if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
611 GdipFree(*clone);
612 GdipFree((*clone)->pathdata.Points);
613 GdipFree((*clone)->pathdata.Types);
614 return OutOfMemory;
617 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
618 path->datalen * sizeof(PointF));
619 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
621 return Ok;
624 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
626 if(!path)
627 return InvalidParameter;
629 if(path->pathdata.Count > 0){
630 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
631 path->newfigure = TRUE;
634 return Ok;
637 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
639 INT i;
641 if(!path)
642 return InvalidParameter;
644 for(i = 1; i < path->pathdata.Count; i++){
645 if(path->pathdata.Types[i] == PathPointTypeStart)
646 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
649 path->newfigure = TRUE;
651 return Ok;
654 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
656 if(!path)
657 return InvalidParameter;
659 *path = GdipAlloc(sizeof(GpPath));
660 if(!*path) return OutOfMemory;
662 (*path)->fill = fill;
663 (*path)->newfigure = TRUE;
665 return Ok;
668 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
669 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
671 if(!path)
672 return InvalidParameter;
674 *path = GdipAlloc(sizeof(GpPath));
675 if(!*path) return OutOfMemory;
677 (*path)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
678 (*path)->pathdata.Types = GdipAlloc(count);
680 if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
681 GdipFree((*path)->pathdata.Points);
682 GdipFree((*path)->pathdata.Types);
683 GdipFree(*path);
684 return OutOfMemory;
687 memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
688 memcpy((*path)->pathdata.Types, types, count);
689 (*path)->pathdata.Count = count;
690 (*path)->datalen = count;
692 (*path)->fill = fill;
693 (*path)->newfigure = TRUE;
695 return Ok;
698 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
699 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
701 GpPointF *ptF;
702 GpStatus ret;
703 INT i;
705 ptF = GdipAlloc(sizeof(GpPointF)*count);
707 for(i = 0;i < count; i++){
708 ptF[i].X = (REAL)points[i].X;
709 ptF[i].Y = (REAL)points[i].Y;
712 ret = GdipCreatePath2(ptF, types, count, fill, path);
714 GdipFree(ptF);
716 return ret;
719 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
721 if(!path)
722 return InvalidParameter;
724 GdipFree(path->pathdata.Points);
725 GdipFree(path->pathdata.Types);
726 GdipFree(path);
728 return Ok;
731 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
733 if(!path || !pathData)
734 return InvalidParameter;
736 /* Only copy data. pathData allocation/freeing controlled by wrapper class.
737 Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
738 memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
739 memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
741 return Ok;
744 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
746 if(!path || !fillmode)
747 return InvalidParameter;
749 *fillmode = path->fill;
751 return Ok;
754 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
756 INT count;
758 if(!path || !lastPoint)
759 return InvalidParameter;
761 count = path->pathdata.Count;
762 if(count > 0)
763 *lastPoint = path->pathdata.Points[count-1];
765 return Ok;
768 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
770 if(!path)
771 return InvalidParameter;
773 if(count < path->pathdata.Count)
774 return InsufficientBuffer;
776 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
778 return Ok;
781 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
783 GpStatus ret;
784 GpPointF *ptf;
785 INT i;
787 if(count <= 0)
788 return InvalidParameter;
790 ptf = GdipAlloc(sizeof(GpPointF)*count);
791 if(!ptf) return OutOfMemory;
793 ret = GdipGetPathPoints(path,ptf,count);
794 if(ret == Ok)
795 for(i = 0;i < count;i++){
796 points[i].X = roundr(ptf[i].X);
797 points[i].Y = roundr(ptf[i].Y);
799 GdipFree(ptf);
801 return ret;
804 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
806 if(!path)
807 return InvalidParameter;
809 if(count < path->pathdata.Count)
810 return InsufficientBuffer;
812 memcpy(types, path->pathdata.Types, path->pathdata.Count);
814 return Ok;
817 /* Windows expands the bounding box to the maximum possible bounding box
818 * for a given pen. For example, if a line join can extend past the point
819 * it's joining by x units, the bounding box is extended by x units in every
820 * direction (even though this is too conservative for most cases). */
821 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
822 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
824 GpPointF * points, temp_pts[4];
825 INT count, i;
826 REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
828 /* Matrix and pen can be null. */
829 if(!path || !bounds)
830 return InvalidParameter;
832 /* If path is empty just return. */
833 count = path->pathdata.Count;
834 if(count == 0){
835 bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
836 return Ok;
839 points = path->pathdata.Points;
841 low_x = high_x = points[0].X;
842 low_y = high_y = points[0].Y;
844 for(i = 1; i < count; i++){
845 low_x = min(low_x, points[i].X);
846 low_y = min(low_y, points[i].Y);
847 high_x = max(high_x, points[i].X);
848 high_y = max(high_y, points[i].Y);
851 width = high_x - low_x;
852 height = high_y - low_y;
854 /* This looks unusual but it's the only way I can imitate windows. */
855 if(matrix){
856 temp_pts[0].X = low_x;
857 temp_pts[0].Y = low_y;
858 temp_pts[1].X = low_x;
859 temp_pts[1].Y = high_y;
860 temp_pts[2].X = high_x;
861 temp_pts[2].Y = high_y;
862 temp_pts[3].X = high_x;
863 temp_pts[3].Y = low_y;
865 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
866 low_x = temp_pts[0].X;
867 low_y = temp_pts[0].Y;
869 for(i = 1; i < 4; i++){
870 low_x = min(low_x, temp_pts[i].X);
871 low_y = min(low_y, temp_pts[i].Y);
874 temp = width;
875 width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
876 height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
879 if(pen){
880 path_width = pen->width / 2.0;
882 if(count > 2)
883 path_width = max(path_width, pen->width * pen->miterlimit / 2.0);
884 /* FIXME: this should probably also check for the startcap */
885 if(pen->endcap & LineCapNoAnchor)
886 path_width = max(path_width, pen->width * 2.2);
888 low_x -= path_width;
889 low_y -= path_width;
890 width += 2.0 * path_width;
891 height += 2.0 * path_width;
894 bounds->X = low_x;
895 bounds->Y = low_y;
896 bounds->Width = width;
897 bounds->Height = height;
899 return Ok;
902 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
903 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
905 GpStatus ret;
906 GpRectF boundsF;
908 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
910 if(ret == Ok){
911 bounds->X = roundr(boundsF.X);
912 bounds->Y = roundr(boundsF.Y);
913 bounds->Width = roundr(boundsF.Width);
914 bounds->Height = roundr(boundsF.Height);
917 return ret;
920 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
922 if(!path)
923 return InvalidParameter;
925 *count = path->pathdata.Count;
927 return Ok;
930 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
931 GpPen *pen, GpGraphics *graphics, BOOL *result)
933 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
936 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
937 GpPen *pen, GpGraphics *graphics, BOOL *result)
939 static int calls;
941 if(!path || !pen)
942 return InvalidParameter;
944 if(!(calls++))
945 FIXME("not implemented\n");
947 return NotImplemented;
950 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
952 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
955 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
957 static int calls;
959 if(!path) return InvalidParameter;
961 if(!(calls++))
962 FIXME("not implemented\n");
964 return NotImplemented;
967 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
969 if(!path)
970 return InvalidParameter;
972 path->newfigure = TRUE;
974 return Ok;
977 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
979 if(!path)
980 return InvalidParameter;
982 path->pathdata.Count = 0;
983 path->newfigure = TRUE;
984 path->fill = FillModeAlternate;
986 return Ok;
989 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
991 if(!path)
992 return InvalidParameter;
994 path->fill = fill;
996 return Ok;
999 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1001 if(!path)
1002 return InvalidParameter;
1004 if(path->pathdata.Count == 0)
1005 return Ok;
1007 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1008 path->pathdata.Count);
1011 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
1012 REAL width, REAL height)
1014 GpPath *backup;
1015 GpPointF ptf[2];
1016 GpStatus retstat;
1017 BOOL old_new;
1019 if(!path || width < 0.0 || height < 0.0)
1020 return InvalidParameter;
1022 /* make a backup copy of path data */
1023 if((retstat = GdipClonePath(path, &backup)) != Ok)
1024 return retstat;
1026 /* rectangle should start as new path */
1027 old_new = path->newfigure;
1028 path->newfigure = TRUE;
1029 if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
1030 path->newfigure = old_new;
1031 goto fail;
1034 ptf[0].X = x+width;
1035 ptf[0].Y = y+height;
1036 ptf[1].X = x;
1037 ptf[1].Y = y+height;
1039 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
1040 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
1042 /* free backup */
1043 GdipDeletePath(backup);
1044 return Ok;
1046 fail:
1047 /* reverting */
1048 GdipDeletePath(path);
1049 GdipClonePath(backup, &path);
1050 GdipDeletePath(backup);
1052 return retstat;
1055 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
1056 INT width, INT height)
1058 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1061 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
1063 GpPath *backup;
1064 GpStatus retstat;
1065 INT i;
1067 /* count == 0 - verified condition */
1068 if(!path || !rects || count == 0)
1069 return InvalidParameter;
1071 if(count < 0)
1072 return OutOfMemory;
1074 /* make a backup copy */
1075 if((retstat = GdipClonePath(path, &backup)) != Ok)
1076 return retstat;
1078 for(i = 0; i < count; i++){
1079 if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
1080 goto fail;
1083 /* free backup */
1084 GdipDeletePath(backup);
1085 return Ok;
1087 fail:
1088 /* reverting */
1089 GdipDeletePath(path);
1090 GdipClonePath(backup, &path);
1091 GdipDeletePath(backup);
1093 return retstat;
1096 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
1098 GpRectF *rectsF;
1099 GpStatus retstat;
1100 INT i;
1102 if(!rects || count == 0)
1103 return InvalidParameter;
1105 if(count < 0)
1106 return OutOfMemory;
1108 rectsF = GdipAlloc(sizeof(GpRectF)*count);
1110 for(i = 0;i < count;i++){
1111 rectsF[i].X = (REAL)rects[i].X;
1112 rectsF[i].Y = (REAL)rects[i].Y;
1113 rectsF[i].Width = (REAL)rects[i].Width;
1114 rectsF[i].Height = (REAL)rects[i].Height;
1117 retstat = GdipAddPathRectangles(path, rectsF, count);
1118 GdipFree(rectsF);
1120 return retstat;
1123 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
1125 INT count;
1127 if(!path)
1128 return InvalidParameter;
1130 count = path->pathdata.Count;
1132 /* set marker flag */
1133 if(count > 0)
1134 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
1136 return Ok;
1139 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
1141 INT count;
1142 INT i;
1144 if(!path)
1145 return InvalidParameter;
1147 count = path->pathdata.Count;
1149 for(i = 0; i < count - 1; i++){
1150 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
1153 return Ok;