push 63c1876572cbb61a874995ad42ef27c14590d232
[wine/hacks.git] / dlls / gdiplus / graphicspath.c
blob51407c71d8242c7ba9acfa1bcc2379a1e4b2ceee
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 GdipAddPathPie(GpPath *path, REAL x, REAL y, REAL width, REAL height,
550 REAL startAngle, REAL sweepAngle)
552 GpPointF *ptf;
553 GpStatus status;
554 INT i, count;
556 if(!path)
557 return InvalidParameter;
559 count = arc2polybezier(NULL, x, y, width, height, startAngle, sweepAngle);
561 if(count == 0)
562 return Ok;
564 ptf = GdipAlloc(sizeof(GpPointF)*count);
565 if(!ptf)
566 return OutOfMemory;
568 arc2polybezier(ptf, x, y, width, height, startAngle, sweepAngle);
570 status = GdipAddPathLine(path, (width - x)/2, (height - y)/2, ptf[0].X, ptf[0].Y);
571 if(status != Ok){
572 GdipFree(ptf);
573 return status;
575 /* one spline is already added as a line endpoint */
576 if(!lengthen_path(path, count - 1)){
577 GdipFree(ptf);
578 return OutOfMemory;
581 memcpy(&(path->pathdata.Points[path->pathdata.Count]), &(ptf[1]),sizeof(GpPointF)*(count-1));
582 for(i = 0; i < count-1; i++)
583 path->pathdata.Types[path->pathdata.Count+i] = PathPointTypeBezier;
585 path->pathdata.Count += count-1;
587 GdipClosePathFigure(path);
589 GdipFree(ptf);
591 return status;
594 GpStatus WINGDIPAPI GdipAddPathPieI(GpPath *path, INT x, INT y, INT width, INT height,
595 REAL startAngle, REAL sweepAngle)
597 return GdipAddPathPieI(path, (REAL)x, (REAL)y, (REAL)width, (REAL)height, startAngle, sweepAngle);
600 GpStatus WINGDIPAPI GdipAddPathPolygon(GpPath *path, GDIPCONST GpPointF *points, INT count)
602 INT old_count;
604 if(!path || !points || count < 3)
605 return InvalidParameter;
607 if(!lengthen_path(path, count))
608 return OutOfMemory;
610 old_count = path->pathdata.Count;
612 memcpy(&path->pathdata.Points[old_count], points, count*sizeof(GpPointF));
613 memset(&path->pathdata.Types[old_count + 1], PathPointTypeLine, count - 1);
615 /* A polygon is an intrinsic figure */
616 path->pathdata.Types[old_count] = PathPointTypeStart;
617 path->pathdata.Types[old_count + count - 1] |= PathPointTypeCloseSubpath;
618 path->newfigure = TRUE;
619 path->pathdata.Count += count;
621 return Ok;
624 GpStatus WINGDIPAPI GdipAddPathPolygonI(GpPath *path, GDIPCONST GpPoint *points, INT count)
626 GpPointF *ptf;
627 GpStatus status;
628 INT i;
630 if(!points || count < 3)
631 return InvalidParameter;
633 ptf = GdipAlloc(sizeof(GpPointF) * count);
634 if(!ptf)
635 return OutOfMemory;
637 for(i = 0; i < count; i++){
638 ptf[i].X = (REAL)points[i].X;
639 ptf[i].Y = (REAL)points[i].Y;
642 status = GdipAddPathPolygon(path, ptf, count);
644 GdipFree(ptf);
646 return status;
649 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
651 if(!path || !clone)
652 return InvalidParameter;
654 *clone = GdipAlloc(sizeof(GpPath));
655 if(!*clone) return OutOfMemory;
657 **clone = *path;
659 (*clone)->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
660 (*clone)->pathdata.Types = GdipAlloc(path->datalen);
661 if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
662 GdipFree(*clone);
663 GdipFree((*clone)->pathdata.Points);
664 GdipFree((*clone)->pathdata.Types);
665 return OutOfMemory;
668 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
669 path->datalen * sizeof(PointF));
670 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
672 return Ok;
675 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
677 if(!path)
678 return InvalidParameter;
680 if(path->pathdata.Count > 0){
681 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
682 path->newfigure = TRUE;
685 return Ok;
688 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
690 INT i;
692 if(!path)
693 return InvalidParameter;
695 for(i = 1; i < path->pathdata.Count; i++){
696 if(path->pathdata.Types[i] == PathPointTypeStart)
697 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
700 path->newfigure = TRUE;
702 return Ok;
705 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
707 if(!path)
708 return InvalidParameter;
710 *path = GdipAlloc(sizeof(GpPath));
711 if(!*path) return OutOfMemory;
713 (*path)->fill = fill;
714 (*path)->newfigure = TRUE;
716 return Ok;
719 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
720 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
722 if(!path)
723 return InvalidParameter;
725 *path = GdipAlloc(sizeof(GpPath));
726 if(!*path) return OutOfMemory;
728 (*path)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
729 (*path)->pathdata.Types = GdipAlloc(count);
731 if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
732 GdipFree((*path)->pathdata.Points);
733 GdipFree((*path)->pathdata.Types);
734 GdipFree(*path);
735 return OutOfMemory;
738 memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
739 memcpy((*path)->pathdata.Types, types, count);
740 (*path)->pathdata.Count = count;
741 (*path)->datalen = count;
743 (*path)->fill = fill;
744 (*path)->newfigure = TRUE;
746 return Ok;
749 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
750 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
752 GpPointF *ptF;
753 GpStatus ret;
754 INT i;
756 ptF = GdipAlloc(sizeof(GpPointF)*count);
758 for(i = 0;i < count; i++){
759 ptF[i].X = (REAL)points[i].X;
760 ptF[i].Y = (REAL)points[i].Y;
763 ret = GdipCreatePath2(ptF, types, count, fill, path);
765 GdipFree(ptF);
767 return ret;
770 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
772 if(!path)
773 return InvalidParameter;
775 GdipFree(path->pathdata.Points);
776 GdipFree(path->pathdata.Types);
777 GdipFree(path);
779 return Ok;
782 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
784 if(!path || !pathData)
785 return InvalidParameter;
787 /* Only copy data. pathData allocation/freeing controlled by wrapper class.
788 Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
789 memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
790 memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
792 return Ok;
795 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
797 if(!path || !fillmode)
798 return InvalidParameter;
800 *fillmode = path->fill;
802 return Ok;
805 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
807 INT count;
809 if(!path || !lastPoint)
810 return InvalidParameter;
812 count = path->pathdata.Count;
813 if(count > 0)
814 *lastPoint = path->pathdata.Points[count-1];
816 return Ok;
819 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
821 if(!path)
822 return InvalidParameter;
824 if(count < path->pathdata.Count)
825 return InsufficientBuffer;
827 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
829 return Ok;
832 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
834 GpStatus ret;
835 GpPointF *ptf;
836 INT i;
838 if(count <= 0)
839 return InvalidParameter;
841 ptf = GdipAlloc(sizeof(GpPointF)*count);
842 if(!ptf) return OutOfMemory;
844 ret = GdipGetPathPoints(path,ptf,count);
845 if(ret == Ok)
846 for(i = 0;i < count;i++){
847 points[i].X = roundr(ptf[i].X);
848 points[i].Y = roundr(ptf[i].Y);
850 GdipFree(ptf);
852 return ret;
855 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
857 if(!path)
858 return InvalidParameter;
860 if(count < path->pathdata.Count)
861 return InsufficientBuffer;
863 memcpy(types, path->pathdata.Types, path->pathdata.Count);
865 return Ok;
868 /* Windows expands the bounding box to the maximum possible bounding box
869 * for a given pen. For example, if a line join can extend past the point
870 * it's joining by x units, the bounding box is extended by x units in every
871 * direction (even though this is too conservative for most cases). */
872 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
873 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
875 GpPointF * points, temp_pts[4];
876 INT count, i;
877 REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
879 /* Matrix and pen can be null. */
880 if(!path || !bounds)
881 return InvalidParameter;
883 /* If path is empty just return. */
884 count = path->pathdata.Count;
885 if(count == 0){
886 bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
887 return Ok;
890 points = path->pathdata.Points;
892 low_x = high_x = points[0].X;
893 low_y = high_y = points[0].Y;
895 for(i = 1; i < count; i++){
896 low_x = min(low_x, points[i].X);
897 low_y = min(low_y, points[i].Y);
898 high_x = max(high_x, points[i].X);
899 high_y = max(high_y, points[i].Y);
902 width = high_x - low_x;
903 height = high_y - low_y;
905 /* This looks unusual but it's the only way I can imitate windows. */
906 if(matrix){
907 temp_pts[0].X = low_x;
908 temp_pts[0].Y = low_y;
909 temp_pts[1].X = low_x;
910 temp_pts[1].Y = high_y;
911 temp_pts[2].X = high_x;
912 temp_pts[2].Y = high_y;
913 temp_pts[3].X = high_x;
914 temp_pts[3].Y = low_y;
916 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
917 low_x = temp_pts[0].X;
918 low_y = temp_pts[0].Y;
920 for(i = 1; i < 4; i++){
921 low_x = min(low_x, temp_pts[i].X);
922 low_y = min(low_y, temp_pts[i].Y);
925 temp = width;
926 width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
927 height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
930 if(pen){
931 path_width = pen->width / 2.0;
933 if(count > 2)
934 path_width = max(path_width, pen->width * pen->miterlimit / 2.0);
935 /* FIXME: this should probably also check for the startcap */
936 if(pen->endcap & LineCapNoAnchor)
937 path_width = max(path_width, pen->width * 2.2);
939 low_x -= path_width;
940 low_y -= path_width;
941 width += 2.0 * path_width;
942 height += 2.0 * path_width;
945 bounds->X = low_x;
946 bounds->Y = low_y;
947 bounds->Width = width;
948 bounds->Height = height;
950 return Ok;
953 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
954 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
956 GpStatus ret;
957 GpRectF boundsF;
959 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
961 if(ret == Ok){
962 bounds->X = roundr(boundsF.X);
963 bounds->Y = roundr(boundsF.Y);
964 bounds->Width = roundr(boundsF.Width);
965 bounds->Height = roundr(boundsF.Height);
968 return ret;
971 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
973 if(!path)
974 return InvalidParameter;
976 *count = path->pathdata.Count;
978 return Ok;
981 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
983 INT i, count;
984 INT start = 0; /* position in reversed path */
985 GpPathData revpath;
987 if(!path)
988 return InvalidParameter;
990 count = path->pathdata.Count;
992 if(count == 0) return Ok;
994 revpath.Points = GdipAlloc(sizeof(GpPointF)*count);
995 revpath.Types = GdipAlloc(sizeof(BYTE)*count);
996 revpath.Count = count;
997 if(!revpath.Points || !revpath.Types){
998 GdipFree(revpath.Points);
999 GdipFree(revpath.Types);
1000 return OutOfMemory;
1003 for(i = 0; i < count; i++){
1005 /* find next start point */
1006 if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1007 INT j;
1008 for(j = start; j <= i; j++){
1009 revpath.Points[j] = path->pathdata.Points[count-j-1];
1010 revpath.Types[j] = path->pathdata.Types[count-j-1];
1012 /* mark start point */
1013 revpath.Types[start] = PathPointTypeStart;
1014 /* set 'figure' endpoint type */
1015 if(i-start > 1){
1016 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1017 revpath.Types[i] |= revpath.Types[i-1];
1019 else
1020 revpath.Types[i] = path->pathdata.Types[start];
1022 start = i+1;
1026 memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1027 memcpy(path->pathdata.Types, revpath.Types, sizeof(BYTE)*count);
1029 GdipFree(revpath.Points);
1030 GdipFree(revpath.Types);
1032 return Ok;
1035 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1036 GpPen *pen, GpGraphics *graphics, BOOL *result)
1038 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1041 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1042 GpPen *pen, GpGraphics *graphics, BOOL *result)
1044 static int calls;
1046 if(!path || !pen)
1047 return InvalidParameter;
1049 if(!(calls++))
1050 FIXME("not implemented\n");
1052 return NotImplemented;
1055 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1057 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1060 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1062 static int calls;
1064 if(!path) return InvalidParameter;
1066 if(!(calls++))
1067 FIXME("not implemented\n");
1069 return NotImplemented;
1072 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1074 if(!path)
1075 return InvalidParameter;
1077 path->newfigure = TRUE;
1079 return Ok;
1082 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1084 if(!path)
1085 return InvalidParameter;
1087 path->pathdata.Count = 0;
1088 path->newfigure = TRUE;
1089 path->fill = FillModeAlternate;
1091 return Ok;
1094 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1096 if(!path)
1097 return InvalidParameter;
1099 path->fill = fill;
1101 return Ok;
1104 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1106 if(!path)
1107 return InvalidParameter;
1109 if(path->pathdata.Count == 0)
1110 return Ok;
1112 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1113 path->pathdata.Count);
1116 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
1117 REAL width, REAL height)
1119 GpPath *backup;
1120 GpPointF ptf[2];
1121 GpStatus retstat;
1122 BOOL old_new;
1124 if(!path || width < 0.0 || height < 0.0)
1125 return InvalidParameter;
1127 /* make a backup copy of path data */
1128 if((retstat = GdipClonePath(path, &backup)) != Ok)
1129 return retstat;
1131 /* rectangle should start as new path */
1132 old_new = path->newfigure;
1133 path->newfigure = TRUE;
1134 if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
1135 path->newfigure = old_new;
1136 goto fail;
1139 ptf[0].X = x+width;
1140 ptf[0].Y = y+height;
1141 ptf[1].X = x;
1142 ptf[1].Y = y+height;
1144 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
1145 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
1147 /* free backup */
1148 GdipDeletePath(backup);
1149 return Ok;
1151 fail:
1152 /* reverting */
1153 GdipDeletePath(path);
1154 GdipClonePath(backup, &path);
1155 GdipDeletePath(backup);
1157 return retstat;
1160 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
1161 INT width, INT height)
1163 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1166 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
1168 GpPath *backup;
1169 GpStatus retstat;
1170 INT i;
1172 /* count == 0 - verified condition */
1173 if(!path || !rects || count == 0)
1174 return InvalidParameter;
1176 if(count < 0)
1177 return OutOfMemory;
1179 /* make a backup copy */
1180 if((retstat = GdipClonePath(path, &backup)) != Ok)
1181 return retstat;
1183 for(i = 0; i < count; i++){
1184 if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
1185 goto fail;
1188 /* free backup */
1189 GdipDeletePath(backup);
1190 return Ok;
1192 fail:
1193 /* reverting */
1194 GdipDeletePath(path);
1195 GdipClonePath(backup, &path);
1196 GdipDeletePath(backup);
1198 return retstat;
1201 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
1203 GpRectF *rectsF;
1204 GpStatus retstat;
1205 INT i;
1207 if(!rects || count == 0)
1208 return InvalidParameter;
1210 if(count < 0)
1211 return OutOfMemory;
1213 rectsF = GdipAlloc(sizeof(GpRectF)*count);
1215 for(i = 0;i < count;i++){
1216 rectsF[i].X = (REAL)rects[i].X;
1217 rectsF[i].Y = (REAL)rects[i].Y;
1218 rectsF[i].Width = (REAL)rects[i].Width;
1219 rectsF[i].Height = (REAL)rects[i].Height;
1222 retstat = GdipAddPathRectangles(path, rectsF, count);
1223 GdipFree(rectsF);
1225 return retstat;
1228 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
1230 INT count;
1232 if(!path)
1233 return InvalidParameter;
1235 count = path->pathdata.Count;
1237 /* set marker flag */
1238 if(count > 0)
1239 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
1241 return Ok;
1244 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
1246 INT count;
1247 INT i;
1249 if(!path)
1250 return InvalidParameter;
1252 count = path->pathdata.Count;
1254 for(i = 0; i < count - 1; i++){
1255 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
1258 return Ok;