136afe7bfdb0c7523504676184a285a8278a1129
[reactos.git] / reactos / dll / win32 / gdiplus / graphicspath.c
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19
20 #include <stdarg.h>
21 #include <math.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27
28 #include "objbase.h"
29
30 #include "gdiplus.h"
31 #include "gdiplus_private.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
35
36 /* make sure path has enough space for len more points */
37 static BOOL lengthen_path(GpPath *path, INT len)
38 {
39 /* initial allocation */
40 if(path->datalen == 0){
41 path->datalen = len * 2;
42
43 path->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
44 if(!path->pathdata.Points) return FALSE;
45
46 path->pathdata.Types = GdipAlloc(path->datalen);
47 if(!path->pathdata.Types){
48 GdipFree(path->pathdata.Points);
49 return FALSE;
50 }
51 }
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;
56
57 path->pathdata.Points = HeapReAlloc(GetProcessHeap(), 0,
58 path->pathdata.Points, path->datalen * sizeof(PointF));
59 if(!path->pathdata.Points) return FALSE;
60
61 path->pathdata.Types = HeapReAlloc(GetProcessHeap(), 0,
62 path->pathdata.Types, path->datalen);
63 if(!path->pathdata.Types) return FALSE;
64 }
65
66 return TRUE;
67 }
68
69 GpStatus WINGDIPAPI GdipAddPathArc(GpPath *path, REAL x1, REAL y1, REAL x2,
70 REAL y2, REAL startAngle, REAL sweepAngle)
71 {
72 INT count, old_count, i;
73
74 if(!path)
75 return InvalidParameter;
76
77 count = arc2polybezier(NULL, x1, y1, x2, y2, startAngle, sweepAngle);
78
79 if(count == 0)
80 return Ok;
81 if(!lengthen_path(path, count))
82 return OutOfMemory;
83
84 old_count = path->pathdata.Count;
85 arc2polybezier(&path->pathdata.Points[old_count], x1, y1, x2, y2,
86 startAngle, sweepAngle);
87
88 for(i = 0; i < count; i++){
89 path->pathdata.Types[old_count + i] = PathPointTypeBezier;
90 }
91
92 path->pathdata.Types[old_count] =
93 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
94 path->newfigure = FALSE;
95 path->pathdata.Count += count;
96
97 return Ok;
98 }
99
100 GpStatus WINGDIPAPI GdipAddPathArcI(GpPath *path, INT x1, INT y1, INT x2,
101 INT y2, REAL startAngle, REAL sweepAngle)
102 {
103 return GdipAddPathArc(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,startAngle,sweepAngle);
104 }
105
106 GpStatus WINGDIPAPI GdipAddPathBezier(GpPath *path, REAL x1, REAL y1, REAL x2,
107 REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
108 {
109 INT old_count;
110
111 if(!path)
112 return InvalidParameter;
113
114 if(!lengthen_path(path, 4))
115 return OutOfMemory;
116
117 old_count = path->pathdata.Count;
118
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;
127
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;
133
134 path->newfigure = FALSE;
135 path->pathdata.Count += 4;
136
137 return Ok;
138 }
139
140 GpStatus WINGDIPAPI GdipAddPathBezierI(GpPath *path, INT x1, INT y1, INT x2,
141 INT y2, INT x3, INT y3, INT x4, INT y4)
142 {
143 return GdipAddPathBezier(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,(REAL)x3,(REAL)y3,
144 (REAL)x4,(REAL)y4);
145 }
146
147 GpStatus WINGDIPAPI GdipAddPathBeziers(GpPath *path, GDIPCONST GpPointF *points,
148 INT count)
149 {
150 INT i, old_count;
151
152 if(!path || !points || ((count - 1) % 3))
153 return InvalidParameter;
154
155 if(!lengthen_path(path, count))
156 return OutOfMemory;
157
158 old_count = path->pathdata.Count;
159
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;
164 }
165
166 path->pathdata.Types[old_count] =
167 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
168 path->newfigure = FALSE;
169 path->pathdata.Count += count;
170
171 return Ok;
172 }
173
174 GpStatus WINGDIPAPI GdipAddPathBeziersI(GpPath *path, GDIPCONST GpPoint *points,
175 INT count)
176 {
177 GpPointF *ptsF;
178 GpStatus ret;
179 INT i;
180
181 if(!points || ((count - 1) % 3))
182 return InvalidParameter;
183
184 ptsF = GdipAlloc(sizeof(GpPointF) * count);
185 if(!ptsF)
186 return OutOfMemory;
187
188 for(i = 0; i < count; i++){
189 ptsF[i].X = (REAL)points[i].X;
190 ptsF[i].Y = (REAL)points[i].Y;
191 }
192
193 ret = GdipAddPathBeziers(path, ptsF, count);
194 GdipFree(ptsF);
195
196 return ret;
197 }
198
199 GpStatus WINGDIPAPI GdipAddPathClosedCurve(GpPath *path, GDIPCONST GpPointF *points,
200 INT count)
201 {
202 return GdipAddPathClosedCurve2(path, points, count, 1.0);
203 }
204
205 GpStatus WINGDIPAPI GdipAddPathClosedCurveI(GpPath *path, GDIPCONST GpPoint *points,
206 INT count)
207 {
208 return GdipAddPathClosedCurve2I(path, points, count, 1.0);
209 }
210
211 GpStatus WINGDIPAPI GdipAddPathClosedCurve2(GpPath *path, GDIPCONST GpPointF *points,
212 INT count, REAL tension)
213 {
214 INT i, len_pt = (count + 1)*3-2;
215 GpPointF *pt;
216 GpPointF *pts;
217 REAL x1, x2, y1, y2;
218 GpStatus stat;
219
220 if(!path || !points || count <= 1)
221 return InvalidParameter;
222
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;
229 }
230
231 /* copy source points to extend with the last one */
232 memcpy(pts, points, sizeof(GpPointF)*count);
233 pts[count] = pts[0];
234
235 tension = tension * TENSION_CONST;
236
237 for(i = 0; i < count-1; i++){
238 calc_curve_bezier(&(pts[i]), tension, &x1, &y1, &x2, &y2);
239
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;
246 }
247
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];
253
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;
264
265 stat = GdipAddPathBeziers(path, pt, len_pt);
266
267 /* close figure */
268 if(stat == Ok){
269 INT count = path->pathdata.Count;
270 path->pathdata.Types[count - 1] |= PathPointTypeCloseSubpath;
271 path->newfigure = TRUE;
272 }
273
274 GdipFree(pts);
275 GdipFree(pt);
276
277 return stat;
278 }
279
280 GpStatus WINGDIPAPI GdipAddPathClosedCurve2I(GpPath *path, GDIPCONST GpPoint *points,
281 INT count, REAL tension)
282 {
283 GpPointF *ptf;
284 INT i;
285 GpStatus stat;
286
287 if(!path || !points || count <= 1)
288 return InvalidParameter;
289
290 ptf = GdipAlloc(sizeof(GpPointF)*count);
291 if(!ptf)
292 return OutOfMemory;
293
294 for(i = 0; i < count; i++){
295 ptf[i].X = (REAL)points[i].X;
296 ptf[i].Y = (REAL)points[i].Y;
297 }
298
299 stat = GdipAddPathClosedCurve2(path, ptf, count, tension);
300
301 GdipFree(ptf);
302
303 return stat;
304 }
305
306 GpStatus WINGDIPAPI GdipAddPathCurve(GpPath *path, GDIPCONST GpPointF *points, INT count)
307 {
308 if(!path || !points || count <= 1)
309 return InvalidParameter;
310
311 return GdipAddPathCurve2(path, points, count, 1.0);
312 }
313
314 GpStatus WINGDIPAPI GdipAddPathCurveI(GpPath *path, GDIPCONST GpPoint *points, INT count)
315 {
316 if(!path || !points || count <= 1)
317 return InvalidParameter;
318
319 return GdipAddPathCurve2I(path, points, count, 1.0);
320 }
321
322 GpStatus WINGDIPAPI GdipAddPathCurve2(GpPath *path, GDIPCONST GpPointF *points, INT count,
323 REAL tension)
324 {
325 INT i, len_pt = count*3-2;
326 GpPointF *pt;
327 REAL x1, x2, y1, y2;
328 GpStatus stat;
329
330 if(!path || !points || count <= 1)
331 return InvalidParameter;
332
333 pt = GdipAlloc(len_pt * sizeof(GpPointF));
334 if(!pt)
335 return OutOfMemory;
336
337 tension = tension * TENSION_CONST;
338
339 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
340 tension, &x1, &y1);
341
342 pt[0].X = points[0].X;
343 pt[0].Y = points[0].Y;
344 pt[1].X = x1;
345 pt[1].Y = y1;
346
347 for(i = 0; i < count-2; i++){
348 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
349
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;
356 }
357
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);
360
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;
365
366 stat = GdipAddPathBeziers(path, pt, len_pt);
367
368 GdipFree(pt);
369
370 return stat;
371 }
372
373 GpStatus WINGDIPAPI GdipAddPathCurve2I(GpPath *path, GDIPCONST GpPoint *points,
374 INT count, REAL tension)
375 {
376 GpPointF *ptf;
377 INT i;
378 GpStatus stat;
379
380 if(!path || !points || count <= 1)
381 return InvalidParameter;
382
383 ptf = GdipAlloc(sizeof(GpPointF)*count);
384 if(!ptf)
385 return OutOfMemory;
386
387 for(i = 0; i < count; i++){
388 ptf[i].X = (REAL)points[i].X;
389 ptf[i].Y = (REAL)points[i].Y;
390 }
391
392 stat = GdipAddPathCurve2(path, ptf, count, tension);
393
394 GdipFree(ptf);
395
396 return stat;
397 }
398
399 GpStatus WINGDIPAPI GdipAddPathEllipse(GpPath *path, REAL x, REAL y, REAL width,
400 REAL height)
401 {
402 INT old_count, numpts;
403
404 if(!path)
405 return InvalidParameter;
406
407 if(!lengthen_path(path, MAX_ARC_PTS))
408 return OutOfMemory;
409
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;
415 }
416
417 memset(&path->pathdata.Types[old_count + 1], PathPointTypeBezier,
418 MAX_ARC_PTS - 1);
419
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;
425
426 return Ok;
427 }
428
429 GpStatus WINGDIPAPI GdipAddPathEllipseI(GpPath *path, INT x, INT y, INT width,
430 INT height)
431 {
432 return GdipAddPathEllipse(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
433 }
434
435 GpStatus WINGDIPAPI GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
436 INT count)
437 {
438 INT i, old_count;
439
440 if(!path || !points)
441 return InvalidParameter;
442
443 if(!lengthen_path(path, count))
444 return OutOfMemory;
445
446 old_count = path->pathdata.Count;
447
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;
452 }
453
454 if(path->newfigure){
455 path->pathdata.Types[old_count] = PathPointTypeStart;
456 path->newfigure = FALSE;
457 }
458
459 path->pathdata.Count += count;
460
461 return Ok;
462 }
463
464 GpStatus WINGDIPAPI GdipAddPathLine2I(GpPath *path, GDIPCONST GpPoint *points, INT count)
465 {
466 GpPointF *pointsF;
467 INT i;
468 GpStatus stat;
469
470 if(count <= 0)
471 return InvalidParameter;
472
473 pointsF = GdipAlloc(sizeof(GpPointF) * count);
474 if(!pointsF) return OutOfMemory;
475
476 for(i = 0;i < count; i++){
477 pointsF[i].X = (REAL)points[i].X;
478 pointsF[i].Y = (REAL)points[i].Y;
479 }
480
481 stat = GdipAddPathLine2(path, pointsF, count);
482
483 GdipFree(pointsF);
484
485 return stat;
486 }
487
488 GpStatus WINGDIPAPI GdipAddPathLine(GpPath *path, REAL x1, REAL y1, REAL x2, REAL y2)
489 {
490 INT old_count;
491
492 if(!path)
493 return InvalidParameter;
494
495 if(!lengthen_path(path, 2))
496 return OutOfMemory;
497
498 old_count = path->pathdata.Count;
499
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;
504
505 path->pathdata.Types[old_count] =
506 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
507 path->pathdata.Types[old_count + 1] = PathPointTypeLine;
508
509 path->newfigure = FALSE;
510 path->pathdata.Count += 2;
511
512 return Ok;
513 }
514
515 GpStatus WINGDIPAPI GdipAddPathLineI(GpPath *path, INT x1, INT y1, INT x2, INT y2)
516 {
517 return GdipAddPathLine(path, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
518 }
519
520 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
521 BOOL connect)
522 {
523 INT old_count, count;
524
525 if(!path || !addingPath)
526 return InvalidParameter;
527
528 old_count = path->pathdata.Count;
529 count = addingPath->pathdata.Count;
530
531 if(!lengthen_path(path, count))
532 return OutOfMemory;
533
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);
537
538 if(path->newfigure || !connect)
539 path->pathdata.Types[old_count] = PathPointTypeStart;
540 else
541 path->pathdata.Types[old_count] = PathPointTypeLine;
542
543 path->newfigure = FALSE;
544 path->pathdata.Count += count;
545
546 return Ok;
547 }
548
549 GpStatus WINGDIPAPI GdipAddPathPolygon(GpPath *path, GDIPCONST GpPointF *points, INT count)
550 {
551 INT old_count;
552
553 if(!path || !points || count < 3)
554 return InvalidParameter;
555
556 if(!lengthen_path(path, count))
557 return OutOfMemory;
558
559 old_count = path->pathdata.Count;
560
561 memcpy(&path->pathdata.Points[old_count], points, count*sizeof(GpPointF));
562 memset(&path->pathdata.Types[old_count + 1], PathPointTypeLine, count - 1);
563
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;
569
570 return Ok;
571 }
572
573 GpStatus WINGDIPAPI GdipAddPathPolygonI(GpPath *path, GDIPCONST GpPoint *points, INT count)
574 {
575 GpPointF *ptf;
576 GpStatus status;
577 INT i;
578
579 if(!points || count < 3)
580 return InvalidParameter;
581
582 ptf = GdipAlloc(sizeof(GpPointF) * count);
583 if(!ptf)
584 return OutOfMemory;
585
586 for(i = 0; i < count; i++){
587 ptf[i].X = (REAL)points[i].X;
588 ptf[i].Y = (REAL)points[i].Y;
589 }
590
591 status = GdipAddPathPolygon(path, ptf, count);
592
593 GdipFree(ptf);
594
595 return status;
596 }
597
598 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
599 {
600 if(!path || !clone)
601 return InvalidParameter;
602
603 *clone = GdipAlloc(sizeof(GpPath));
604 if(!*clone) return OutOfMemory;
605
606 **clone = *path;
607
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;
615 }
616
617 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
618 path->datalen * sizeof(PointF));
619 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
620
621 return Ok;
622 }
623
624 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
625 {
626 if(!path)
627 return InvalidParameter;
628
629 if(path->pathdata.Count > 0){
630 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
631 path->newfigure = TRUE;
632 }
633
634 return Ok;
635 }
636
637 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
638 {
639 INT i;
640
641 if(!path)
642 return InvalidParameter;
643
644 for(i = 1; i < path->pathdata.Count; i++){
645 if(path->pathdata.Types[i] == PathPointTypeStart)
646 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
647 }
648
649 path->newfigure = TRUE;
650
651 return Ok;
652 }
653
654 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
655 {
656 if(!path)
657 return InvalidParameter;
658
659 *path = GdipAlloc(sizeof(GpPath));
660 if(!*path) return OutOfMemory;
661
662 (*path)->fill = fill;
663 (*path)->newfigure = TRUE;
664
665 return Ok;
666 }
667
668 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
669 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
670 {
671 if(!path)
672 return InvalidParameter;
673
674 *path = GdipAlloc(sizeof(GpPath));
675 if(!*path) return OutOfMemory;
676
677 (*path)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
678 (*path)->pathdata.Types = GdipAlloc(count);
679
680 if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
681 GdipFree((*path)->pathdata.Points);
682 GdipFree((*path)->pathdata.Types);
683 GdipFree(*path);
684 return OutOfMemory;
685 }
686
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;
691
692 (*path)->fill = fill;
693 (*path)->newfigure = TRUE;
694
695 return Ok;
696 }
697
698 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
699 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
700 {
701 GpPointF *ptF;
702 GpStatus ret;
703 INT i;
704
705 ptF = GdipAlloc(sizeof(GpPointF)*count);
706
707 for(i = 0;i < count; i++){
708 ptF[i].X = (REAL)points[i].X;
709 ptF[i].Y = (REAL)points[i].Y;
710 }
711
712 ret = GdipCreatePath2(ptF, types, count, fill, path);
713
714 GdipFree(ptF);
715
716 return ret;
717 }
718
719 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
720 {
721 if(!path)
722 return InvalidParameter;
723
724 GdipFree(path->pathdata.Points);
725 GdipFree(path->pathdata.Types);
726 GdipFree(path);
727
728 return Ok;
729 }
730
731 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
732 {
733 if(!path || !pathData)
734 return InvalidParameter;
735
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);
740
741 return Ok;
742 }
743
744 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
745 {
746 if(!path || !fillmode)
747 return InvalidParameter;
748
749 *fillmode = path->fill;
750
751 return Ok;
752 }
753
754 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
755 {
756 INT count;
757
758 if(!path || !lastPoint)
759 return InvalidParameter;
760
761 count = path->pathdata.Count;
762 if(count > 0)
763 *lastPoint = path->pathdata.Points[count-1];
764
765 return Ok;
766 }
767
768 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
769 {
770 if(!path)
771 return InvalidParameter;
772
773 if(count < path->pathdata.Count)
774 return InsufficientBuffer;
775
776 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
777
778 return Ok;
779 }
780
781 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
782 {
783 GpStatus ret;
784 GpPointF *ptf;
785 INT i;
786
787 if(count <= 0)
788 return InvalidParameter;
789
790 ptf = GdipAlloc(sizeof(GpPointF)*count);
791 if(!ptf) return OutOfMemory;
792
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);
798 };
799 GdipFree(ptf);
800
801 return ret;
802 }
803
804 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
805 {
806 if(!path)
807 return InvalidParameter;
808
809 if(count < path->pathdata.Count)
810 return InsufficientBuffer;
811
812 memcpy(types, path->pathdata.Types, path->pathdata.Count);
813
814 return Ok;
815 }
816
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)
823 {
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;
827
828 /* Matrix and pen can be null. */
829 if(!path || !bounds)
830 return InvalidParameter;
831
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;
837 }
838
839 points = path->pathdata.Points;
840
841 low_x = high_x = points[0].X;
842 low_y = high_y = points[0].Y;
843
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);
849 }
850
851 width = high_x - low_x;
852 height = high_y - low_y;
853
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;
864
865 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
866 low_x = temp_pts[0].X;
867 low_y = temp_pts[0].Y;
868
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);
872 }
873
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]);
877 }
878
879 if(pen){
880 path_width = pen->width / 2.0;
881
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);
887
888 low_x -= path_width;
889 low_y -= path_width;
890 width += 2.0 * path_width;
891 height += 2.0 * path_width;
892 }
893
894 bounds->X = low_x;
895 bounds->Y = low_y;
896 bounds->Width = width;
897 bounds->Height = height;
898
899 return Ok;
900 }
901
902 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
903 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
904 {
905 GpStatus ret;
906 GpRectF boundsF;
907
908 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
909
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);
915 }
916
917 return ret;
918 }
919
920 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
921 {
922 if(!path)
923 return InvalidParameter;
924
925 *count = path->pathdata.Count;
926
927 return Ok;
928 }
929
930 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
931 GpPen *pen, GpGraphics *graphics, BOOL *result)
932 {
933 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
934 }
935
936 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
937 GpPen *pen, GpGraphics *graphics, BOOL *result)
938 {
939 static int calls;
940
941 if(!path || !pen)
942 return InvalidParameter;
943
944 if(!(calls++))
945 FIXME("not implemented\n");
946
947 return NotImplemented;
948 }
949
950 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
951 {
952 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
953 }
954
955 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
956 {
957 static int calls;
958
959 if(!path) return InvalidParameter;
960
961 if(!(calls++))
962 FIXME("not implemented\n");
963
964 return NotImplemented;
965 }
966
967 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
968 {
969 if(!path)
970 return InvalidParameter;
971
972 path->newfigure = TRUE;
973
974 return Ok;
975 }
976
977 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
978 {
979 if(!path)
980 return InvalidParameter;
981
982 path->pathdata.Count = 0;
983 path->newfigure = TRUE;
984 path->fill = FillModeAlternate;
985
986 return Ok;
987 }
988
989 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
990 {
991 if(!path)
992 return InvalidParameter;
993
994 path->fill = fill;
995
996 return Ok;
997 }
998
999 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1000 {
1001 if(!path)
1002 return InvalidParameter;
1003
1004 if(path->pathdata.Count == 0)
1005 return Ok;
1006
1007 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1008 path->pathdata.Count);
1009 }
1010
1011 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
1012 REAL width, REAL height)
1013 {
1014 GpPath *backup;
1015 GpPointF ptf[2];
1016 GpStatus retstat;
1017 BOOL old_new;
1018
1019 if(!path || width < 0.0 || height < 0.0)
1020 return InvalidParameter;
1021
1022 /* make a backup copy of path data */
1023 if((retstat = GdipClonePath(path, &backup)) != Ok)
1024 return retstat;
1025
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;
1032 }
1033
1034 ptf[0].X = x+width;
1035 ptf[0].Y = y+height;
1036 ptf[1].X = x;
1037 ptf[1].Y = y+height;
1038
1039 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
1040 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
1041
1042 /* free backup */
1043 GdipDeletePath(backup);
1044 return Ok;
1045
1046 fail:
1047 /* reverting */
1048 GdipDeletePath(path);
1049 GdipClonePath(backup, &path);
1050 GdipDeletePath(backup);
1051
1052 return retstat;
1053 }
1054
1055 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
1056 INT width, INT height)
1057 {
1058 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1059 }
1060
1061 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
1062 {
1063 GpPath *backup;
1064 GpStatus retstat;
1065 INT i;
1066
1067 /* count == 0 - verified condition */
1068 if(!path || !rects || count == 0)
1069 return InvalidParameter;
1070
1071 if(count < 0)
1072 return OutOfMemory;
1073
1074 /* make a backup copy */
1075 if((retstat = GdipClonePath(path, &backup)) != Ok)
1076 return retstat;
1077
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;
1081 }
1082
1083 /* free backup */
1084 GdipDeletePath(backup);
1085 return Ok;
1086
1087 fail:
1088 /* reverting */
1089 GdipDeletePath(path);
1090 GdipClonePath(backup, &path);
1091 GdipDeletePath(backup);
1092
1093 return retstat;
1094 }
1095
1096 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
1097 {
1098 GpRectF *rectsF;
1099 GpStatus retstat;
1100 INT i;
1101
1102 if(!rects || count == 0)
1103 return InvalidParameter;
1104
1105 if(count < 0)
1106 return OutOfMemory;
1107
1108 rectsF = GdipAlloc(sizeof(GpRectF)*count);
1109
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;
1115 }
1116
1117 retstat = GdipAddPathRectangles(path, rectsF, count);
1118 GdipFree(rectsF);
1119
1120 return retstat;
1121 }
1122
1123 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
1124 {
1125 INT count;
1126
1127 if(!path)
1128 return InvalidParameter;
1129
1130 count = path->pathdata.Count;
1131
1132 /* set marker flag */
1133 if(count > 0)
1134 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
1135
1136 return Ok;
1137 }
1138
1139 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
1140 {
1141 INT count;
1142 INT i;
1143
1144 if(!path)
1145 return InvalidParameter;
1146
1147 count = path->pathdata.Count;
1148
1149 for(i = 0; i < count - 1; i++){
1150 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
1151 }
1152
1153 return Ok;
1154 }