[GDIPLUS]
[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 "gdiplus_private.h"
21
22 typedef struct path_list_node_t path_list_node_t;
23 struct path_list_node_t {
24 GpPointF pt;
25 BYTE type; /* PathPointTypeStart or PathPointTypeLine */
26 path_list_node_t *next;
27 };
28
29 /* init list */
30 static BOOL init_path_list(path_list_node_t **node, REAL x, REAL y)
31 {
32 *node = GdipAlloc(sizeof(path_list_node_t));
33 if(!*node)
34 return FALSE;
35
36 (*node)->pt.X = x;
37 (*node)->pt.Y = y;
38 (*node)->type = PathPointTypeStart;
39 (*node)->next = NULL;
40
41 return TRUE;
42 }
43
44 /* free all nodes including argument */
45 static void free_path_list(path_list_node_t *node)
46 {
47 path_list_node_t *n = node;
48
49 while(n){
50 n = n->next;
51 GdipFree(node);
52 node = n;
53 }
54 }
55
56 /* Add a node after 'node' */
57 /*
58 * Returns
59 * pointer on success
60 * NULL on allocation problems
61 */
62 static path_list_node_t* add_path_list_node(path_list_node_t *node, REAL x, REAL y, BOOL type)
63 {
64 path_list_node_t *new;
65
66 new = GdipAlloc(sizeof(path_list_node_t));
67 if(!new)
68 return NULL;
69
70 new->pt.X = x;
71 new->pt.Y = y;
72 new->type = type;
73 new->next = node->next;
74 node->next = new;
75
76 return new;
77 }
78
79 /* returns element count */
80 static INT path_list_count(path_list_node_t *node)
81 {
82 INT count = 1;
83
84 while((node = node->next))
85 ++count;
86
87 return count;
88 }
89
90 /* GdipFlattenPath helper */
91 /*
92 * Used to recursively flatten single Bezier curve
93 * Parameters:
94 * - start : pointer to start point node;
95 * - (x2, y2): first control point;
96 * - (x3, y3): second control point;
97 * - end : pointer to end point node
98 * - flatness: admissible error of linear approximation.
99 *
100 * Return value:
101 * TRUE : success
102 * FALSE: out of memory
103 *
104 * TODO: used quality criteria should be revised to match native as
105 * closer as possible.
106 */
107 static BOOL flatten_bezier(path_list_node_t *start, REAL x2, REAL y2, REAL x3, REAL y3,
108 path_list_node_t *end, REAL flatness)
109 {
110 /* this 5 middle points with start/end define to half-curves */
111 GpPointF mp[5];
112 GpPointF pt, pt_st;
113 path_list_node_t *node;
114
115 /* calculate bezier curve middle points == new control points */
116 mp[0].X = (start->pt.X + x2) / 2.0;
117 mp[0].Y = (start->pt.Y + y2) / 2.0;
118 /* middle point between control points */
119 pt.X = (x2 + x3) / 2.0;
120 pt.Y = (y2 + y3) / 2.0;
121 mp[1].X = (mp[0].X + pt.X) / 2.0;
122 mp[1].Y = (mp[0].Y + pt.Y) / 2.0;
123 mp[4].X = (end->pt.X + x3) / 2.0;
124 mp[4].Y = (end->pt.Y + y3) / 2.0;
125 mp[3].X = (mp[4].X + pt.X) / 2.0;
126 mp[3].Y = (mp[4].Y + pt.Y) / 2.0;
127
128 mp[2].X = (mp[1].X + mp[3].X) / 2.0;
129 mp[2].Y = (mp[1].Y + mp[3].Y) / 2.0;
130
131 pt = end->pt;
132 pt_st = start->pt;
133 /* check flatness as a half of distance between middle point and a linearized path */
134 if(fabs(((pt.Y - pt_st.Y)*mp[2].X + (pt_st.X - pt.X)*mp[2].Y +
135 (pt_st.Y*pt.X - pt_st.X*pt.Y))) <=
136 (0.5 * flatness*sqrtf((powf(pt.Y - pt_st.Y, 2.0) + powf(pt_st.X - pt.X, 2.0))))){
137 return TRUE;
138 }
139 else
140 /* add a middle point */
141 if(!(node = add_path_list_node(start, mp[2].X, mp[2].Y, PathPointTypeLine)))
142 return FALSE;
143
144 /* do the same with halves */
145 flatten_bezier(start, mp[0].X, mp[0].Y, mp[1].X, mp[1].Y, node, flatness);
146 flatten_bezier(node, mp[3].X, mp[3].Y, mp[4].X, mp[4].Y, end, flatness);
147
148 return TRUE;
149 }
150
151 GpStatus WINGDIPAPI GdipAddPathArc(GpPath *path, REAL x1, REAL y1, REAL x2,
152 REAL y2, REAL startAngle, REAL sweepAngle)
153 {
154 INT count, old_count, i;
155
156 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
157 path, x1, y1, x2, y2, startAngle, sweepAngle);
158
159 if(!path)
160 return InvalidParameter;
161
162 count = arc2polybezier(NULL, x1, y1, x2, y2, startAngle, sweepAngle);
163
164 if(count == 0)
165 return Ok;
166 if(!lengthen_path(path, count))
167 return OutOfMemory;
168
169 old_count = path->pathdata.Count;
170 arc2polybezier(&path->pathdata.Points[old_count], x1, y1, x2, y2,
171 startAngle, sweepAngle);
172
173 for(i = 0; i < count; i++){
174 path->pathdata.Types[old_count + i] = PathPointTypeBezier;
175 }
176
177 path->pathdata.Types[old_count] =
178 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
179 path->newfigure = FALSE;
180 path->pathdata.Count += count;
181
182 return Ok;
183 }
184
185 GpStatus WINGDIPAPI GdipAddPathArcI(GpPath *path, INT x1, INT y1, INT x2,
186 INT y2, REAL startAngle, REAL sweepAngle)
187 {
188 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
189 path, x1, y1, x2, y2, startAngle, sweepAngle);
190
191 return GdipAddPathArc(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,startAngle,sweepAngle);
192 }
193
194 GpStatus WINGDIPAPI GdipAddPathBezier(GpPath *path, REAL x1, REAL y1, REAL x2,
195 REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
196 {
197 INT old_count;
198
199 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
200 path, x1, y1, x2, y2, x3, y3, x4, y4);
201
202 if(!path)
203 return InvalidParameter;
204
205 if(!lengthen_path(path, 4))
206 return OutOfMemory;
207
208 old_count = path->pathdata.Count;
209
210 path->pathdata.Points[old_count].X = x1;
211 path->pathdata.Points[old_count].Y = y1;
212 path->pathdata.Points[old_count + 1].X = x2;
213 path->pathdata.Points[old_count + 1].Y = y2;
214 path->pathdata.Points[old_count + 2].X = x3;
215 path->pathdata.Points[old_count + 2].Y = y3;
216 path->pathdata.Points[old_count + 3].X = x4;
217 path->pathdata.Points[old_count + 3].Y = y4;
218
219 path->pathdata.Types[old_count] =
220 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
221 path->pathdata.Types[old_count + 1] = PathPointTypeBezier;
222 path->pathdata.Types[old_count + 2] = PathPointTypeBezier;
223 path->pathdata.Types[old_count + 3] = PathPointTypeBezier;
224
225 path->newfigure = FALSE;
226 path->pathdata.Count += 4;
227
228 return Ok;
229 }
230
231 GpStatus WINGDIPAPI GdipAddPathBezierI(GpPath *path, INT x1, INT y1, INT x2,
232 INT y2, INT x3, INT y3, INT x4, INT y4)
233 {
234 TRACE("(%p, %d, %d, %d, %d, %d, %d, %d, %d)\n",
235 path, x1, y1, x2, y2, x3, y3, x4, y4);
236
237 return GdipAddPathBezier(path,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2,(REAL)x3,(REAL)y3,
238 (REAL)x4,(REAL)y4);
239 }
240
241 GpStatus WINGDIPAPI GdipAddPathBeziers(GpPath *path, GDIPCONST GpPointF *points,
242 INT count)
243 {
244 INT i, old_count;
245
246 TRACE("(%p, %p, %d)\n", path, points, count);
247
248 if(!path || !points || ((count - 1) % 3))
249 return InvalidParameter;
250
251 if(!lengthen_path(path, count))
252 return OutOfMemory;
253
254 old_count = path->pathdata.Count;
255
256 for(i = 0; i < count; i++){
257 path->pathdata.Points[old_count + i].X = points[i].X;
258 path->pathdata.Points[old_count + i].Y = points[i].Y;
259 path->pathdata.Types[old_count + i] = PathPointTypeBezier;
260 }
261
262 path->pathdata.Types[old_count] =
263 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
264 path->newfigure = FALSE;
265 path->pathdata.Count += count;
266
267 return Ok;
268 }
269
270 GpStatus WINGDIPAPI GdipAddPathBeziersI(GpPath *path, GDIPCONST GpPoint *points,
271 INT count)
272 {
273 GpPointF *ptsF;
274 GpStatus ret;
275 INT i;
276
277 TRACE("(%p, %p, %d)\n", path, points, count);
278
279 if(!points || ((count - 1) % 3))
280 return InvalidParameter;
281
282 ptsF = GdipAlloc(sizeof(GpPointF) * count);
283 if(!ptsF)
284 return OutOfMemory;
285
286 for(i = 0; i < count; i++){
287 ptsF[i].X = (REAL)points[i].X;
288 ptsF[i].Y = (REAL)points[i].Y;
289 }
290
291 ret = GdipAddPathBeziers(path, ptsF, count);
292 GdipFree(ptsF);
293
294 return ret;
295 }
296
297 GpStatus WINGDIPAPI GdipAddPathClosedCurve(GpPath *path, GDIPCONST GpPointF *points,
298 INT count)
299 {
300 TRACE("(%p, %p, %d)\n", path, points, count);
301
302 return GdipAddPathClosedCurve2(path, points, count, 1.0);
303 }
304
305 GpStatus WINGDIPAPI GdipAddPathClosedCurveI(GpPath *path, GDIPCONST GpPoint *points,
306 INT count)
307 {
308 TRACE("(%p, %p, %d)\n", path, points, count);
309
310 return GdipAddPathClosedCurve2I(path, points, count, 1.0);
311 }
312
313 GpStatus WINGDIPAPI GdipAddPathClosedCurve2(GpPath *path, GDIPCONST GpPointF *points,
314 INT count, REAL tension)
315 {
316 INT i, len_pt = (count + 1)*3-2;
317 GpPointF *pt;
318 GpPointF *pts;
319 REAL x1, x2, y1, y2;
320 GpStatus stat;
321
322 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
323
324 if(!path || !points || count <= 1)
325 return InvalidParameter;
326
327 pt = GdipAlloc(len_pt * sizeof(GpPointF));
328 pts = GdipAlloc((count + 1)*sizeof(GpPointF));
329 if(!pt || !pts){
330 GdipFree(pt);
331 GdipFree(pts);
332 return OutOfMemory;
333 }
334
335 /* copy source points to extend with the last one */
336 memcpy(pts, points, sizeof(GpPointF)*count);
337 pts[count] = pts[0];
338
339 tension = tension * TENSION_CONST;
340
341 for(i = 0; i < count-1; i++){
342 calc_curve_bezier(&(pts[i]), tension, &x1, &y1, &x2, &y2);
343
344 pt[3*i+2].X = x1;
345 pt[3*i+2].Y = y1;
346 pt[3*i+3].X = pts[i+1].X;
347 pt[3*i+3].Y = pts[i+1].Y;
348 pt[3*i+4].X = x2;
349 pt[3*i+4].Y = y2;
350 }
351
352 /* points [len_pt-2] and [0] are calculated
353 separately to connect splines properly */
354 pts[0] = points[count-1];
355 pts[1] = points[0]; /* equals to start and end of a resulting path */
356 pts[2] = points[1];
357
358 calc_curve_bezier(pts, tension, &x1, &y1, &x2, &y2);
359 pt[len_pt-2].X = x1;
360 pt[len_pt-2].Y = y1;
361 pt[0].X = pts[1].X;
362 pt[0].Y = pts[1].Y;
363 pt[1].X = x2;
364 pt[1].Y = y2;
365 /* close path */
366 pt[len_pt-1].X = pt[0].X;
367 pt[len_pt-1].Y = pt[0].Y;
368
369 stat = GdipAddPathBeziers(path, pt, len_pt);
370
371 /* close figure */
372 if(stat == Ok){
373 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
374 path->newfigure = TRUE;
375 }
376
377 GdipFree(pts);
378 GdipFree(pt);
379
380 return stat;
381 }
382
383 GpStatus WINGDIPAPI GdipAddPathClosedCurve2I(GpPath *path, GDIPCONST GpPoint *points,
384 INT count, REAL tension)
385 {
386 GpPointF *ptf;
387 INT i;
388 GpStatus stat;
389
390 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
391
392 if(!path || !points || count <= 1)
393 return InvalidParameter;
394
395 ptf = GdipAlloc(sizeof(GpPointF)*count);
396 if(!ptf)
397 return OutOfMemory;
398
399 for(i = 0; i < count; i++){
400 ptf[i].X = (REAL)points[i].X;
401 ptf[i].Y = (REAL)points[i].Y;
402 }
403
404 stat = GdipAddPathClosedCurve2(path, ptf, count, tension);
405
406 GdipFree(ptf);
407
408 return stat;
409 }
410
411 GpStatus WINGDIPAPI GdipAddPathCurve(GpPath *path, GDIPCONST GpPointF *points, INT count)
412 {
413 TRACE("(%p, %p, %d)\n", path, points, count);
414
415 if(!path || !points || count <= 1)
416 return InvalidParameter;
417
418 return GdipAddPathCurve2(path, points, count, 1.0);
419 }
420
421 GpStatus WINGDIPAPI GdipAddPathCurveI(GpPath *path, GDIPCONST GpPoint *points, INT count)
422 {
423 TRACE("(%p, %p, %d)\n", path, points, count);
424
425 if(!path || !points || count <= 1)
426 return InvalidParameter;
427
428 return GdipAddPathCurve2I(path, points, count, 1.0);
429 }
430
431 GpStatus WINGDIPAPI GdipAddPathCurve2(GpPath *path, GDIPCONST GpPointF *points, INT count,
432 REAL tension)
433 {
434 INT i, len_pt = count*3-2;
435 GpPointF *pt;
436 REAL x1, x2, y1, y2;
437 GpStatus stat;
438
439 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
440
441 if(!path || !points || count <= 1)
442 return InvalidParameter;
443
444 pt = GdipAlloc(len_pt * sizeof(GpPointF));
445 if(!pt)
446 return OutOfMemory;
447
448 tension = tension * TENSION_CONST;
449
450 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
451 tension, &x1, &y1);
452
453 pt[0].X = points[0].X;
454 pt[0].Y = points[0].Y;
455 pt[1].X = x1;
456 pt[1].Y = y1;
457
458 for(i = 0; i < count-2; i++){
459 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
460
461 pt[3*i+2].X = x1;
462 pt[3*i+2].Y = y1;
463 pt[3*i+3].X = points[i+1].X;
464 pt[3*i+3].Y = points[i+1].Y;
465 pt[3*i+4].X = x2;
466 pt[3*i+4].Y = y2;
467 }
468
469 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
470 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
471
472 pt[len_pt-2].X = x1;
473 pt[len_pt-2].Y = y1;
474 pt[len_pt-1].X = points[count-1].X;
475 pt[len_pt-1].Y = points[count-1].Y;
476
477 stat = GdipAddPathBeziers(path, pt, len_pt);
478
479 GdipFree(pt);
480
481 return stat;
482 }
483
484 GpStatus WINGDIPAPI GdipAddPathCurve2I(GpPath *path, GDIPCONST GpPoint *points,
485 INT count, REAL tension)
486 {
487 GpPointF *ptf;
488 INT i;
489 GpStatus stat;
490
491 TRACE("(%p, %p, %d, %.2f)\n", path, points, count, tension);
492
493 if(!path || !points || count <= 1)
494 return InvalidParameter;
495
496 ptf = GdipAlloc(sizeof(GpPointF)*count);
497 if(!ptf)
498 return OutOfMemory;
499
500 for(i = 0; i < count; i++){
501 ptf[i].X = (REAL)points[i].X;
502 ptf[i].Y = (REAL)points[i].Y;
503 }
504
505 stat = GdipAddPathCurve2(path, ptf, count, tension);
506
507 GdipFree(ptf);
508
509 return stat;
510 }
511
512 GpStatus WINGDIPAPI GdipAddPathCurve3(GpPath *path, GDIPCONST GpPointF *points,
513 INT count, INT offset, INT nseg, REAL tension)
514 {
515 TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
516
517 if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
518 return InvalidParameter;
519
520 return GdipAddPathCurve2(path, &points[offset], nseg + 1, tension);
521 }
522
523 GpStatus WINGDIPAPI GdipAddPathCurve3I(GpPath *path, GDIPCONST GpPoint *points,
524 INT count, INT offset, INT nseg, REAL tension)
525 {
526 TRACE("(%p, %p, %d, %d, %d, %.2f)\n", path, points, count, offset, nseg, tension);
527
528 if(!path || !points || offset + 1 >= count || count - offset < nseg + 1)
529 return InvalidParameter;
530
531 return GdipAddPathCurve2I(path, &points[offset], nseg + 1, tension);
532 }
533
534 GpStatus WINGDIPAPI GdipAddPathEllipse(GpPath *path, REAL x, REAL y, REAL width,
535 REAL height)
536 {
537 INT old_count, numpts;
538
539 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
540
541 if(!path)
542 return InvalidParameter;
543
544 if(!lengthen_path(path, MAX_ARC_PTS))
545 return OutOfMemory;
546
547 old_count = path->pathdata.Count;
548 if((numpts = arc2polybezier(&path->pathdata.Points[old_count], x, y, width,
549 height, 0.0, 360.0)) != MAX_ARC_PTS){
550 ERR("expected %d points but got %d\n", MAX_ARC_PTS, numpts);
551 return GenericError;
552 }
553
554 memset(&path->pathdata.Types[old_count + 1], PathPointTypeBezier,
555 MAX_ARC_PTS - 1);
556
557 /* An ellipse is an intrinsic figure (always is its own subpath). */
558 path->pathdata.Types[old_count] = PathPointTypeStart;
559 path->pathdata.Types[old_count + MAX_ARC_PTS - 1] |= PathPointTypeCloseSubpath;
560 path->newfigure = TRUE;
561 path->pathdata.Count += MAX_ARC_PTS;
562
563 return Ok;
564 }
565
566 GpStatus WINGDIPAPI GdipAddPathEllipseI(GpPath *path, INT x, INT y, INT width,
567 INT height)
568 {
569 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
570
571 return GdipAddPathEllipse(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
572 }
573
574 GpStatus WINGDIPAPI GdipAddPathLine2(GpPath *path, GDIPCONST GpPointF *points,
575 INT count)
576 {
577 INT i, old_count;
578
579 TRACE("(%p, %p, %d)\n", path, points, count);
580
581 if(!path || !points)
582 return InvalidParameter;
583
584 if(!lengthen_path(path, count))
585 return OutOfMemory;
586
587 old_count = path->pathdata.Count;
588
589 for(i = 0; i < count; i++){
590 path->pathdata.Points[old_count + i].X = points[i].X;
591 path->pathdata.Points[old_count + i].Y = points[i].Y;
592 path->pathdata.Types[old_count + i] = PathPointTypeLine;
593 }
594
595 if(path->newfigure){
596 path->pathdata.Types[old_count] = PathPointTypeStart;
597 path->newfigure = FALSE;
598 }
599
600 path->pathdata.Count += count;
601
602 return Ok;
603 }
604
605 GpStatus WINGDIPAPI GdipAddPathLine2I(GpPath *path, GDIPCONST GpPoint *points, INT count)
606 {
607 GpPointF *pointsF;
608 INT i;
609 GpStatus stat;
610
611 TRACE("(%p, %p, %d)\n", path, points, count);
612
613 if(count <= 0)
614 return InvalidParameter;
615
616 pointsF = GdipAlloc(sizeof(GpPointF) * count);
617 if(!pointsF) return OutOfMemory;
618
619 for(i = 0;i < count; i++){
620 pointsF[i].X = (REAL)points[i].X;
621 pointsF[i].Y = (REAL)points[i].Y;
622 }
623
624 stat = GdipAddPathLine2(path, pointsF, count);
625
626 GdipFree(pointsF);
627
628 return stat;
629 }
630
631 GpStatus WINGDIPAPI GdipAddPathLine(GpPath *path, REAL x1, REAL y1, REAL x2, REAL y2)
632 {
633 INT old_count;
634
635 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x1, y1, x2, y2);
636
637 if(!path)
638 return InvalidParameter;
639
640 if(!lengthen_path(path, 2))
641 return OutOfMemory;
642
643 old_count = path->pathdata.Count;
644
645 path->pathdata.Points[old_count].X = x1;
646 path->pathdata.Points[old_count].Y = y1;
647 path->pathdata.Points[old_count + 1].X = x2;
648 path->pathdata.Points[old_count + 1].Y = y2;
649
650 path->pathdata.Types[old_count] =
651 (path->newfigure ? PathPointTypeStart : PathPointTypeLine);
652 path->pathdata.Types[old_count + 1] = PathPointTypeLine;
653
654 path->newfigure = FALSE;
655 path->pathdata.Count += 2;
656
657 return Ok;
658 }
659
660 GpStatus WINGDIPAPI GdipAddPathLineI(GpPath *path, INT x1, INT y1, INT x2, INT y2)
661 {
662 TRACE("(%p, %d, %d, %d, %d)\n", path, x1, y1, x2, y2);
663
664 return GdipAddPathLine(path, (REAL)x1, (REAL)y1, (REAL)x2, (REAL)y2);
665 }
666
667 GpStatus WINGDIPAPI GdipAddPathPath(GpPath *path, GDIPCONST GpPath* addingPath,
668 BOOL connect)
669 {
670 INT old_count, count;
671
672 TRACE("(%p, %p, %d)\n", path, addingPath, connect);
673
674 if(!path || !addingPath)
675 return InvalidParameter;
676
677 old_count = path->pathdata.Count;
678 count = addingPath->pathdata.Count;
679
680 if(!lengthen_path(path, count))
681 return OutOfMemory;
682
683 memcpy(&path->pathdata.Points[old_count], addingPath->pathdata.Points,
684 count * sizeof(GpPointF));
685 memcpy(&path->pathdata.Types[old_count], addingPath->pathdata.Types, count);
686
687 if(path->newfigure || !connect)
688 path->pathdata.Types[old_count] = PathPointTypeStart;
689 else
690 path->pathdata.Types[old_count] = PathPointTypeLine;
691
692 path->newfigure = FALSE;
693 path->pathdata.Count += count;
694
695 return Ok;
696 }
697
698 GpStatus WINGDIPAPI GdipAddPathPie(GpPath *path, REAL x, REAL y, REAL width, REAL height,
699 REAL startAngle, REAL sweepAngle)
700 {
701 GpPointF *ptf;
702 GpStatus status;
703 INT i, count;
704
705 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
706 path, x, y, width, height, startAngle, sweepAngle);
707
708 if(!path)
709 return InvalidParameter;
710
711 /* on zero width/height only start point added */
712 if(width <= 1e-7 || height <= 1e-7){
713 if(!lengthen_path(path, 1))
714 return OutOfMemory;
715 path->pathdata.Points[0].X = x + width / 2.0;
716 path->pathdata.Points[0].Y = y + height / 2.0;
717 path->pathdata.Types[0] = PathPointTypeStart | PathPointTypeCloseSubpath;
718 path->pathdata.Count = 1;
719 return InvalidParameter;
720 }
721
722 count = arc2polybezier(NULL, x, y, width, height, startAngle, sweepAngle);
723
724 if(count == 0)
725 return Ok;
726
727 ptf = GdipAlloc(sizeof(GpPointF)*count);
728 if(!ptf)
729 return OutOfMemory;
730
731 arc2polybezier(ptf, x, y, width, height, startAngle, sweepAngle);
732
733 status = GdipAddPathLine(path, x + width/2, y + height/2, ptf[0].X, ptf[0].Y);
734 if(status != Ok){
735 GdipFree(ptf);
736 return status;
737 }
738 /* one spline is already added as a line endpoint */
739 if(!lengthen_path(path, count - 1)){
740 GdipFree(ptf);
741 return OutOfMemory;
742 }
743
744 memcpy(&(path->pathdata.Points[path->pathdata.Count]), &(ptf[1]),sizeof(GpPointF)*(count-1));
745 for(i = 0; i < count-1; i++)
746 path->pathdata.Types[path->pathdata.Count+i] = PathPointTypeBezier;
747
748 path->pathdata.Count += count-1;
749
750 GdipClosePathFigure(path);
751
752 GdipFree(ptf);
753
754 return status;
755 }
756
757 GpStatus WINGDIPAPI GdipAddPathPieI(GpPath *path, INT x, INT y, INT width, INT height,
758 REAL startAngle, REAL sweepAngle)
759 {
760 TRACE("(%p, %d, %d, %d, %d, %.2f, %.2f)\n",
761 path, x, y, width, height, startAngle, sweepAngle);
762
763 return GdipAddPathPie(path, (REAL)x, (REAL)y, (REAL)width, (REAL)height, startAngle, sweepAngle);
764 }
765
766 GpStatus WINGDIPAPI GdipAddPathPolygon(GpPath *path, GDIPCONST GpPointF *points, INT count)
767 {
768 INT old_count;
769
770 TRACE("(%p, %p, %d)\n", path, points, count);
771
772 if(!path || !points || count < 3)
773 return InvalidParameter;
774
775 if(!lengthen_path(path, count))
776 return OutOfMemory;
777
778 old_count = path->pathdata.Count;
779
780 memcpy(&path->pathdata.Points[old_count], points, count*sizeof(GpPointF));
781 memset(&path->pathdata.Types[old_count + 1], PathPointTypeLine, count - 1);
782
783 /* A polygon is an intrinsic figure */
784 path->pathdata.Types[old_count] = PathPointTypeStart;
785 path->pathdata.Types[old_count + count - 1] |= PathPointTypeCloseSubpath;
786 path->newfigure = TRUE;
787 path->pathdata.Count += count;
788
789 return Ok;
790 }
791
792 GpStatus WINGDIPAPI GdipAddPathPolygonI(GpPath *path, GDIPCONST GpPoint *points, INT count)
793 {
794 GpPointF *ptf;
795 GpStatus status;
796 INT i;
797
798 TRACE("(%p, %p, %d)\n", path, points, count);
799
800 if(!points || count < 3)
801 return InvalidParameter;
802
803 ptf = GdipAlloc(sizeof(GpPointF) * count);
804 if(!ptf)
805 return OutOfMemory;
806
807 for(i = 0; i < count; i++){
808 ptf[i].X = (REAL)points[i].X;
809 ptf[i].Y = (REAL)points[i].Y;
810 }
811
812 status = GdipAddPathPolygon(path, ptf, count);
813
814 GdipFree(ptf);
815
816 return status;
817 }
818
819 static float fromfixedpoint(const FIXED v)
820 {
821 float f = ((float)v.fract) / (1<<(sizeof(v.fract)*8));
822 f += v.value;
823 return f;
824 }
825
826 struct format_string_args
827 {
828 GpPath *path;
829 float maxY;
830 float scale;
831 float ascent;
832 };
833
834 static GpStatus format_string_callback(HDC dc,
835 GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
836 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
837 INT lineno, const RectF *bounds, INT *underlined_indexes,
838 INT underlined_index_count, void *priv)
839 {
840 static const MAT2 identity = { {0,1}, {0,0}, {0,0}, {0,1} };
841 struct format_string_args *args = priv;
842 GpPath *path = args->path;
843 GpStatus status = Ok;
844 float x = rect->X + (bounds->X - rect->X) * args->scale;
845 float y = rect->Y + (bounds->Y - rect->Y) * args->scale;
846 int i;
847
848 if (underlined_index_count)
849 FIXME("hotkey underlines not drawn yet\n");
850
851 if (y + bounds->Height * args->scale > args->maxY)
852 args->maxY = y + bounds->Height * args->scale;
853
854 for (i = index; i < length; ++i)
855 {
856 GLYPHMETRICS gm;
857 TTPOLYGONHEADER *ph = NULL;
858 char *start;
859 DWORD len, ofs = 0;
860 len = GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, 0, NULL, &identity);
861 if (len == GDI_ERROR)
862 {
863 status = GenericError;
864 break;
865 }
866 ph = GdipAlloc(len);
867 start = (char *)ph;
868 if (!ph || !lengthen_path(path, len / sizeof(POINTFX)))
869 {
870 status = OutOfMemory;
871 break;
872 }
873 GetGlyphOutlineW(dc, string[i], GGO_BEZIER, &gm, len, start, &identity);
874
875 ofs = 0;
876 while (ofs < len)
877 {
878 DWORD ofs_start = ofs;
879 ph = (TTPOLYGONHEADER*)&start[ofs];
880 path->pathdata.Types[path->pathdata.Count] = PathPointTypeStart;
881 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(ph->pfxStart.x) * args->scale;
882 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(ph->pfxStart.y) * args->scale;
883 TRACE("Starting at count %i with pos %f, %f)\n", path->pathdata.Count, x, y);
884 ofs += sizeof(*ph);
885 while (ofs - ofs_start < ph->cb)
886 {
887 TTPOLYCURVE *curve = (TTPOLYCURVE*)&start[ofs];
888 int j;
889 ofs += sizeof(TTPOLYCURVE) + (curve->cpfx - 1) * sizeof(POINTFX);
890
891 switch (curve->wType)
892 {
893 case TT_PRIM_LINE:
894 for (j = 0; j < curve->cpfx; ++j)
895 {
896 path->pathdata.Types[path->pathdata.Count] = PathPointTypeLine;
897 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
898 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
899 }
900 break;
901 case TT_PRIM_CSPLINE:
902 for (j = 0; j < curve->cpfx; ++j)
903 {
904 path->pathdata.Types[path->pathdata.Count] = PathPointTypeBezier;
905 path->pathdata.Points[path->pathdata.Count].X = x + fromfixedpoint(curve->apfx[j].x) * args->scale;
906 path->pathdata.Points[path->pathdata.Count++].Y = y + args->ascent - fromfixedpoint(curve->apfx[j].y) * args->scale;
907 }
908 break;
909 default:
910 ERR("Unhandled type: %u\n", curve->wType);
911 status = GenericError;
912 }
913 }
914 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
915 }
916 path->newfigure = TRUE;
917 x += gm.gmCellIncX * args->scale;
918 y += gm.gmCellIncY * args->scale;
919
920 GdipFree(ph);
921 if (status != Ok)
922 break;
923 }
924
925 return status;
926 }
927
928 GpStatus WINGDIPAPI GdipAddPathString(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat* format)
929 {
930 GpFont *font;
931 GpStatus status;
932 LOGFONTW lfw;
933 HANDLE hfont;
934 HDC dc;
935 GpGraphics *graphics;
936 GpPath *backup;
937 struct format_string_args args;
938 int i;
939 UINT16 native_height;
940 RectF scaled_layout_rect;
941 TEXTMETRICW textmetric;
942
943 TRACE("(%p, %s, %d, %p, %d, %f, %p, %p)\n", path, debugstr_w(string), length, family, style, emSize, layoutRect, format);
944 if (!path || !string || !family || !emSize || !layoutRect || !format)
945 return InvalidParameter;
946
947 status = GdipGetEmHeight(family, style, &native_height);
948 if (status != Ok)
949 return status;
950
951 scaled_layout_rect.X = layoutRect->X;
952 scaled_layout_rect.Y = layoutRect->Y;
953 scaled_layout_rect.Width = layoutRect->Width * native_height / emSize;
954 scaled_layout_rect.Height = layoutRect->Height * native_height / emSize;
955
956 if ((status = GdipClonePath(path, &backup)) != Ok)
957 return status;
958
959 dc = CreateCompatibleDC(0);
960 status = GdipCreateFromHDC(dc, &graphics);
961 if (status != Ok)
962 {
963 DeleteDC(dc);
964 GdipDeletePath(backup);
965 return status;
966 }
967
968 status = GdipCreateFont(family, native_height, style, UnitPixel, &font);
969 if (status != Ok)
970 {
971 GdipDeleteGraphics(graphics);
972 DeleteDC(dc);
973 GdipDeletePath(backup);
974 return status;
975 }
976
977 get_log_fontW(font, graphics, &lfw);
978 GdipDeleteFont(font);
979 GdipDeleteGraphics(graphics);
980
981 hfont = CreateFontIndirectW(&lfw);
982 if (!hfont)
983 {
984 WARN("Failed to create font\n");
985 DeleteDC(dc);
986 GdipDeletePath(backup);
987 return GenericError;
988 }
989
990 SelectObject(dc, hfont);
991
992 GetTextMetricsW(dc, &textmetric);
993
994 args.path = path;
995 args.maxY = 0;
996 args.scale = emSize / native_height;
997 args.ascent = textmetric.tmAscent * args.scale;
998 status = gdip_format_string(dc, string, length, NULL, &scaled_layout_rect,
999 format, TRUE, format_string_callback, &args);
1000
1001 DeleteDC(dc);
1002 DeleteObject(hfont);
1003
1004 if (status != Ok) /* free backup */
1005 {
1006 GdipFree(path->pathdata.Points);
1007 GdipFree(path->pathdata.Types);
1008 *path = *backup;
1009 GdipFree(backup);
1010 return status;
1011 }
1012 if (format && format->vertalign == StringAlignmentCenter && layoutRect->Y + args.maxY < layoutRect->Height)
1013 {
1014 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1015 inc /= 2;
1016 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1017 path->pathdata.Points[i].Y += inc;
1018 } else if (format && format->vertalign == StringAlignmentFar) {
1019 float inc = layoutRect->Height + layoutRect->Y - args.maxY;
1020 for (i = backup->pathdata.Count; i < path->pathdata.Count; ++i)
1021 path->pathdata.Points[i].Y += inc;
1022 }
1023 GdipDeletePath(backup);
1024 return status;
1025 }
1026
1027 GpStatus WINGDIPAPI GdipAddPathStringI(GpPath* path, GDIPCONST WCHAR* string, INT length, GDIPCONST GpFontFamily* family, INT style, REAL emSize, GDIPCONST Rect* layoutRect, GDIPCONST GpStringFormat* format)
1028 {
1029 if (layoutRect)
1030 {
1031 RectF layoutRectF = {
1032 (REAL)layoutRect->X,
1033 (REAL)layoutRect->Y,
1034 (REAL)layoutRect->Width,
1035 (REAL)layoutRect->Height
1036 };
1037 return GdipAddPathString(path, string, length, family, style, emSize, &layoutRectF, format);
1038 }
1039 return InvalidParameter;
1040 }
1041
1042 GpStatus WINGDIPAPI GdipClonePath(GpPath* path, GpPath **clone)
1043 {
1044 TRACE("(%p, %p)\n", path, clone);
1045
1046 if(!path || !clone)
1047 return InvalidParameter;
1048
1049 *clone = GdipAlloc(sizeof(GpPath));
1050 if(!*clone) return OutOfMemory;
1051
1052 **clone = *path;
1053
1054 (*clone)->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
1055 (*clone)->pathdata.Types = GdipAlloc(path->datalen);
1056 if(!(*clone)->pathdata.Points || !(*clone)->pathdata.Types){
1057 GdipFree((*clone)->pathdata.Points);
1058 GdipFree((*clone)->pathdata.Types);
1059 GdipFree(*clone);
1060 return OutOfMemory;
1061 }
1062
1063 memcpy((*clone)->pathdata.Points, path->pathdata.Points,
1064 path->datalen * sizeof(PointF));
1065 memcpy((*clone)->pathdata.Types, path->pathdata.Types, path->datalen);
1066
1067 return Ok;
1068 }
1069
1070 GpStatus WINGDIPAPI GdipClosePathFigure(GpPath* path)
1071 {
1072 TRACE("(%p)\n", path);
1073
1074 if(!path)
1075 return InvalidParameter;
1076
1077 if(path->pathdata.Count > 0){
1078 path->pathdata.Types[path->pathdata.Count - 1] |= PathPointTypeCloseSubpath;
1079 path->newfigure = TRUE;
1080 }
1081
1082 return Ok;
1083 }
1084
1085 GpStatus WINGDIPAPI GdipClosePathFigures(GpPath* path)
1086 {
1087 INT i;
1088
1089 TRACE("(%p)\n", path);
1090
1091 if(!path)
1092 return InvalidParameter;
1093
1094 for(i = 1; i < path->pathdata.Count; i++){
1095 if(path->pathdata.Types[i] == PathPointTypeStart)
1096 path->pathdata.Types[i-1] |= PathPointTypeCloseSubpath;
1097 }
1098
1099 path->newfigure = TRUE;
1100
1101 return Ok;
1102 }
1103
1104 GpStatus WINGDIPAPI GdipCreatePath(GpFillMode fill, GpPath **path)
1105 {
1106 TRACE("(%d, %p)\n", fill, path);
1107
1108 if(!path)
1109 return InvalidParameter;
1110
1111 *path = GdipAlloc(sizeof(GpPath));
1112 if(!*path) return OutOfMemory;
1113
1114 (*path)->fill = fill;
1115 (*path)->newfigure = TRUE;
1116
1117 return Ok;
1118 }
1119
1120 GpStatus WINGDIPAPI GdipCreatePath2(GDIPCONST GpPointF* points,
1121 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1122 {
1123 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1124
1125 if(!path)
1126 return InvalidParameter;
1127
1128 *path = GdipAlloc(sizeof(GpPath));
1129 if(!*path) return OutOfMemory;
1130
1131 (*path)->pathdata.Points = GdipAlloc(count * sizeof(PointF));
1132 (*path)->pathdata.Types = GdipAlloc(count);
1133
1134 if(!(*path)->pathdata.Points || !(*path)->pathdata.Types){
1135 GdipFree((*path)->pathdata.Points);
1136 GdipFree((*path)->pathdata.Types);
1137 GdipFree(*path);
1138 return OutOfMemory;
1139 }
1140
1141 memcpy((*path)->pathdata.Points, points, count * sizeof(PointF));
1142 memcpy((*path)->pathdata.Types, types, count);
1143 (*path)->pathdata.Count = count;
1144 (*path)->datalen = count;
1145
1146 (*path)->fill = fill;
1147 (*path)->newfigure = TRUE;
1148
1149 return Ok;
1150 }
1151
1152 GpStatus WINGDIPAPI GdipCreatePath2I(GDIPCONST GpPoint* points,
1153 GDIPCONST BYTE* types, INT count, GpFillMode fill, GpPath **path)
1154 {
1155 GpPointF *ptF;
1156 GpStatus ret;
1157 INT i;
1158
1159 TRACE("(%p, %p, %d, %d, %p)\n", points, types, count, fill, path);
1160
1161 ptF = GdipAlloc(sizeof(GpPointF)*count);
1162
1163 for(i = 0;i < count; i++){
1164 ptF[i].X = (REAL)points[i].X;
1165 ptF[i].Y = (REAL)points[i].Y;
1166 }
1167
1168 ret = GdipCreatePath2(ptF, types, count, fill, path);
1169
1170 GdipFree(ptF);
1171
1172 return ret;
1173 }
1174
1175 GpStatus WINGDIPAPI GdipDeletePath(GpPath *path)
1176 {
1177 TRACE("(%p)\n", path);
1178
1179 if(!path)
1180 return InvalidParameter;
1181
1182 GdipFree(path->pathdata.Points);
1183 GdipFree(path->pathdata.Types);
1184 GdipFree(path);
1185
1186 return Ok;
1187 }
1188
1189 GpStatus WINGDIPAPI GdipFlattenPath(GpPath *path, GpMatrix* matrix, REAL flatness)
1190 {
1191 path_list_node_t *list, *node;
1192 GpPointF pt;
1193 INT i = 1;
1194 INT startidx = 0;
1195 GpStatus stat;
1196
1197 TRACE("(%p, %p, %.2f)\n", path, matrix, flatness);
1198
1199 if(!path)
1200 return InvalidParameter;
1201
1202 if(path->pathdata.Count == 0)
1203 return Ok;
1204
1205 if(matrix){
1206 stat = GdipTransformPath(path, matrix);
1207 if (stat != Ok)
1208 return stat;
1209 }
1210
1211 pt = path->pathdata.Points[0];
1212 if(!init_path_list(&list, pt.X, pt.Y))
1213 return OutOfMemory;
1214
1215 node = list;
1216
1217 while(i < path->pathdata.Count){
1218
1219 BYTE type = path->pathdata.Types[i] & PathPointTypePathTypeMask;
1220 path_list_node_t *start;
1221
1222 pt = path->pathdata.Points[i];
1223
1224 /* save last start point index */
1225 if(type == PathPointTypeStart)
1226 startidx = i;
1227
1228 /* always add line points and start points */
1229 if((type == PathPointTypeStart) || (type == PathPointTypeLine)){
1230 if(!add_path_list_node(node, pt.X, pt.Y, path->pathdata.Types[i]))
1231 goto memout;
1232
1233 node = node->next;
1234 ++i;
1235 continue;
1236 }
1237
1238 /* Bezier curve */
1239
1240 /* test for closed figure */
1241 if(path->pathdata.Types[i+1] & PathPointTypeCloseSubpath){
1242 pt = path->pathdata.Points[startidx];
1243 ++i;
1244 }
1245 else
1246 {
1247 i += 2;
1248 pt = path->pathdata.Points[i];
1249 };
1250
1251 start = node;
1252 /* add Bezier end point */
1253 type = (path->pathdata.Types[i] & ~PathPointTypePathTypeMask) | PathPointTypeLine;
1254 if(!add_path_list_node(node, pt.X, pt.Y, type))
1255 goto memout;
1256 node = node->next;
1257
1258 /* flatten curve */
1259 if(!flatten_bezier(start, path->pathdata.Points[i-2].X, path->pathdata.Points[i-2].Y,
1260 path->pathdata.Points[i-1].X, path->pathdata.Points[i-1].Y,
1261 node, flatness))
1262 goto memout;
1263
1264 ++i;
1265 }/* while */
1266
1267 /* store path data back */
1268 i = path_list_count(list);
1269 if(!lengthen_path(path, i))
1270 goto memout;
1271 path->pathdata.Count = i;
1272
1273 node = list;
1274 for(i = 0; i < path->pathdata.Count; i++){
1275 path->pathdata.Points[i] = node->pt;
1276 path->pathdata.Types[i] = node->type;
1277 node = node->next;
1278 }
1279
1280 free_path_list(list);
1281 return Ok;
1282
1283 memout:
1284 free_path_list(list);
1285 return OutOfMemory;
1286 }
1287
1288 GpStatus WINGDIPAPI GdipGetPathData(GpPath *path, GpPathData* pathData)
1289 {
1290 TRACE("(%p, %p)\n", path, pathData);
1291
1292 if(!path || !pathData)
1293 return InvalidParameter;
1294
1295 /* Only copy data. pathData allocation/freeing controlled by wrapper class.
1296 Assumed that pathData is enough wide to get all data - controlled by wrapper too. */
1297 memcpy(pathData->Points, path->pathdata.Points, sizeof(PointF) * pathData->Count);
1298 memcpy(pathData->Types , path->pathdata.Types , pathData->Count);
1299
1300 return Ok;
1301 }
1302
1303 GpStatus WINGDIPAPI GdipGetPathFillMode(GpPath *path, GpFillMode *fillmode)
1304 {
1305 TRACE("(%p, %p)\n", path, fillmode);
1306
1307 if(!path || !fillmode)
1308 return InvalidParameter;
1309
1310 *fillmode = path->fill;
1311
1312 return Ok;
1313 }
1314
1315 GpStatus WINGDIPAPI GdipGetPathLastPoint(GpPath* path, GpPointF* lastPoint)
1316 {
1317 INT count;
1318
1319 TRACE("(%p, %p)\n", path, lastPoint);
1320
1321 if(!path || !lastPoint)
1322 return InvalidParameter;
1323
1324 count = path->pathdata.Count;
1325 if(count > 0)
1326 *lastPoint = path->pathdata.Points[count-1];
1327
1328 return Ok;
1329 }
1330
1331 GpStatus WINGDIPAPI GdipGetPathPoints(GpPath *path, GpPointF* points, INT count)
1332 {
1333 TRACE("(%p, %p, %d)\n", path, points, count);
1334
1335 if(!path)
1336 return InvalidParameter;
1337
1338 if(count < path->pathdata.Count)
1339 return InsufficientBuffer;
1340
1341 memcpy(points, path->pathdata.Points, path->pathdata.Count * sizeof(GpPointF));
1342
1343 return Ok;
1344 }
1345
1346 GpStatus WINGDIPAPI GdipGetPathPointsI(GpPath *path, GpPoint* points, INT count)
1347 {
1348 GpStatus ret;
1349 GpPointF *ptf;
1350 INT i;
1351
1352 TRACE("(%p, %p, %d)\n", path, points, count);
1353
1354 if(count <= 0)
1355 return InvalidParameter;
1356
1357 ptf = GdipAlloc(sizeof(GpPointF)*count);
1358 if(!ptf) return OutOfMemory;
1359
1360 ret = GdipGetPathPoints(path,ptf,count);
1361 if(ret == Ok)
1362 for(i = 0;i < count;i++){
1363 points[i].X = gdip_round(ptf[i].X);
1364 points[i].Y = gdip_round(ptf[i].Y);
1365 };
1366 GdipFree(ptf);
1367
1368 return ret;
1369 }
1370
1371 GpStatus WINGDIPAPI GdipGetPathTypes(GpPath *path, BYTE* types, INT count)
1372 {
1373 TRACE("(%p, %p, %d)\n", path, types, count);
1374
1375 if(!path)
1376 return InvalidParameter;
1377
1378 if(count < path->pathdata.Count)
1379 return InsufficientBuffer;
1380
1381 memcpy(types, path->pathdata.Types, path->pathdata.Count);
1382
1383 return Ok;
1384 }
1385
1386 /* Windows expands the bounding box to the maximum possible bounding box
1387 * for a given pen. For example, if a line join can extend past the point
1388 * it's joining by x units, the bounding box is extended by x units in every
1389 * direction (even though this is too conservative for most cases). */
1390 GpStatus WINGDIPAPI GdipGetPathWorldBounds(GpPath* path, GpRectF* bounds,
1391 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1392 {
1393 GpPointF * points, temp_pts[4];
1394 INT count, i;
1395 REAL path_width = 1.0, width, height, temp, low_x, low_y, high_x, high_y;
1396
1397 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1398
1399 /* Matrix and pen can be null. */
1400 if(!path || !bounds)
1401 return InvalidParameter;
1402
1403 /* If path is empty just return. */
1404 count = path->pathdata.Count;
1405 if(count == 0){
1406 bounds->X = bounds->Y = bounds->Width = bounds->Height = 0.0;
1407 return Ok;
1408 }
1409
1410 points = path->pathdata.Points;
1411
1412 low_x = high_x = points[0].X;
1413 low_y = high_y = points[0].Y;
1414
1415 for(i = 1; i < count; i++){
1416 low_x = min(low_x, points[i].X);
1417 low_y = min(low_y, points[i].Y);
1418 high_x = max(high_x, points[i].X);
1419 high_y = max(high_y, points[i].Y);
1420 }
1421
1422 width = high_x - low_x;
1423 height = high_y - low_y;
1424
1425 /* This looks unusual but it's the only way I can imitate windows. */
1426 if(matrix){
1427 temp_pts[0].X = low_x;
1428 temp_pts[0].Y = low_y;
1429 temp_pts[1].X = low_x;
1430 temp_pts[1].Y = high_y;
1431 temp_pts[2].X = high_x;
1432 temp_pts[2].Y = high_y;
1433 temp_pts[3].X = high_x;
1434 temp_pts[3].Y = low_y;
1435
1436 GdipTransformMatrixPoints((GpMatrix*)matrix, temp_pts, 4);
1437 low_x = temp_pts[0].X;
1438 low_y = temp_pts[0].Y;
1439
1440 for(i = 1; i < 4; i++){
1441 low_x = min(low_x, temp_pts[i].X);
1442 low_y = min(low_y, temp_pts[i].Y);
1443 }
1444
1445 temp = width;
1446 width = height * fabs(matrix->matrix[2]) + width * fabs(matrix->matrix[0]);
1447 height = height * fabs(matrix->matrix[3]) + temp * fabs(matrix->matrix[1]);
1448 }
1449
1450 if(pen){
1451 path_width = pen->width / 2.0;
1452
1453 if(count > 2)
1454 path_width = max(path_width, pen->width * pen->miterlimit / 2.0);
1455 /* FIXME: this should probably also check for the startcap */
1456 if(pen->endcap & LineCapNoAnchor)
1457 path_width = max(path_width, pen->width * 2.2);
1458
1459 low_x -= path_width;
1460 low_y -= path_width;
1461 width += 2.0 * path_width;
1462 height += 2.0 * path_width;
1463 }
1464
1465 bounds->X = low_x;
1466 bounds->Y = low_y;
1467 bounds->Width = width;
1468 bounds->Height = height;
1469
1470 return Ok;
1471 }
1472
1473 GpStatus WINGDIPAPI GdipGetPathWorldBoundsI(GpPath* path, GpRect* bounds,
1474 GDIPCONST GpMatrix *matrix, GDIPCONST GpPen *pen)
1475 {
1476 GpStatus ret;
1477 GpRectF boundsF;
1478
1479 TRACE("(%p, %p, %p, %p)\n", path, bounds, matrix, pen);
1480
1481 ret = GdipGetPathWorldBounds(path,&boundsF,matrix,pen);
1482
1483 if(ret == Ok){
1484 bounds->X = gdip_round(boundsF.X);
1485 bounds->Y = gdip_round(boundsF.Y);
1486 bounds->Width = gdip_round(boundsF.Width);
1487 bounds->Height = gdip_round(boundsF.Height);
1488 }
1489
1490 return ret;
1491 }
1492
1493 GpStatus WINGDIPAPI GdipGetPointCount(GpPath *path, INT *count)
1494 {
1495 TRACE("(%p, %p)\n", path, count);
1496
1497 if(!path)
1498 return InvalidParameter;
1499
1500 *count = path->pathdata.Count;
1501
1502 return Ok;
1503 }
1504
1505 GpStatus WINGDIPAPI GdipReversePath(GpPath* path)
1506 {
1507 INT i, count;
1508 INT start = 0; /* position in reversed path */
1509 GpPathData revpath;
1510
1511 TRACE("(%p)\n", path);
1512
1513 if(!path)
1514 return InvalidParameter;
1515
1516 count = path->pathdata.Count;
1517
1518 if(count == 0) return Ok;
1519
1520 revpath.Points = GdipAlloc(sizeof(GpPointF)*count);
1521 revpath.Types = GdipAlloc(sizeof(BYTE)*count);
1522 revpath.Count = count;
1523 if(!revpath.Points || !revpath.Types){
1524 GdipFree(revpath.Points);
1525 GdipFree(revpath.Types);
1526 return OutOfMemory;
1527 }
1528
1529 for(i = 0; i < count; i++){
1530
1531 /* find next start point */
1532 if(path->pathdata.Types[count-i-1] == PathPointTypeStart){
1533 INT j;
1534 for(j = start; j <= i; j++){
1535 revpath.Points[j] = path->pathdata.Points[count-j-1];
1536 revpath.Types[j] = path->pathdata.Types[count-j-1];
1537 }
1538 /* mark start point */
1539 revpath.Types[start] = PathPointTypeStart;
1540 /* set 'figure' endpoint type */
1541 if(i-start > 1){
1542 revpath.Types[i] = path->pathdata.Types[count-start-1] & ~PathPointTypePathTypeMask;
1543 revpath.Types[i] |= revpath.Types[i-1];
1544 }
1545 else
1546 revpath.Types[i] = path->pathdata.Types[start];
1547
1548 start = i+1;
1549 }
1550 }
1551
1552 memcpy(path->pathdata.Points, revpath.Points, sizeof(GpPointF)*count);
1553 memcpy(path->pathdata.Types, revpath.Types, sizeof(BYTE)*count);
1554
1555 GdipFree(revpath.Points);
1556 GdipFree(revpath.Types);
1557
1558 return Ok;
1559 }
1560
1561 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPointI(GpPath* path, INT x, INT y,
1562 GpPen *pen, GpGraphics *graphics, BOOL *result)
1563 {
1564 TRACE("(%p, %d, %d, %p, %p, %p)\n", path, x, y, pen, graphics, result);
1565
1566 return GdipIsOutlineVisiblePathPoint(path, x, y, pen, graphics, result);
1567 }
1568
1569 GpStatus WINGDIPAPI GdipIsOutlineVisiblePathPoint(GpPath* path, REAL x, REAL y,
1570 GpPen *pen, GpGraphics *graphics, BOOL *result)
1571 {
1572 static int calls;
1573
1574 TRACE("(%p,%0.2f,%0.2f,%p,%p,%p)\n", path, x, y, pen, graphics, result);
1575
1576 if(!path || !pen)
1577 return InvalidParameter;
1578
1579 if(!(calls++))
1580 FIXME("not implemented\n");
1581
1582 return NotImplemented;
1583 }
1584
1585 GpStatus WINGDIPAPI GdipIsVisiblePathPointI(GpPath* path, INT x, INT y, GpGraphics *graphics, BOOL *result)
1586 {
1587 TRACE("(%p, %d, %d, %p, %p)\n", path, x, y, graphics, result);
1588
1589 return GdipIsVisiblePathPoint(path, x, y, graphics, result);
1590 }
1591
1592 /*****************************************************************************
1593 * GdipIsVisiblePathPoint [GDIPLUS.@]
1594 */
1595 GpStatus WINGDIPAPI GdipIsVisiblePathPoint(GpPath* path, REAL x, REAL y, GpGraphics *graphics, BOOL *result)
1596 {
1597 GpRegion *region;
1598 HRGN hrgn;
1599 GpStatus status;
1600
1601 if(!path || !result) return InvalidParameter;
1602
1603 status = GdipCreateRegionPath(path, &region);
1604 if(status != Ok)
1605 return status;
1606
1607 status = GdipGetRegionHRgn(region, graphics, &hrgn);
1608 if(status != Ok){
1609 GdipDeleteRegion(region);
1610 return status;
1611 }
1612
1613 *result = PtInRegion(hrgn, gdip_round(x), gdip_round(y));
1614
1615 DeleteObject(hrgn);
1616 GdipDeleteRegion(region);
1617
1618 return Ok;
1619 }
1620
1621 GpStatus WINGDIPAPI GdipStartPathFigure(GpPath *path)
1622 {
1623 TRACE("(%p)\n", path);
1624
1625 if(!path)
1626 return InvalidParameter;
1627
1628 path->newfigure = TRUE;
1629
1630 return Ok;
1631 }
1632
1633 GpStatus WINGDIPAPI GdipResetPath(GpPath *path)
1634 {
1635 TRACE("(%p)\n", path);
1636
1637 if(!path)
1638 return InvalidParameter;
1639
1640 path->pathdata.Count = 0;
1641 path->newfigure = TRUE;
1642 path->fill = FillModeAlternate;
1643
1644 return Ok;
1645 }
1646
1647 GpStatus WINGDIPAPI GdipSetPathFillMode(GpPath *path, GpFillMode fill)
1648 {
1649 TRACE("(%p, %d)\n", path, fill);
1650
1651 if(!path)
1652 return InvalidParameter;
1653
1654 path->fill = fill;
1655
1656 return Ok;
1657 }
1658
1659 GpStatus WINGDIPAPI GdipTransformPath(GpPath *path, GpMatrix *matrix)
1660 {
1661 TRACE("(%p, %p)\n", path, matrix);
1662
1663 if(!path)
1664 return InvalidParameter;
1665
1666 if(path->pathdata.Count == 0)
1667 return Ok;
1668
1669 return GdipTransformMatrixPoints(matrix, path->pathdata.Points,
1670 path->pathdata.Count);
1671 }
1672
1673 GpStatus WINGDIPAPI GdipWarpPath(GpPath *path, GpMatrix* matrix,
1674 GDIPCONST GpPointF *points, INT count, REAL x, REAL y, REAL width,
1675 REAL height, WarpMode warpmode, REAL flatness)
1676 {
1677 FIXME("(%p,%p,%p,%i,%0.2f,%0.2f,%0.2f,%0.2f,%i,%0.2f)\n", path, matrix,
1678 points, count, x, y, width, height, warpmode, flatness);
1679
1680 return NotImplemented;
1681 }
1682
1683 static void add_bevel_point(const GpPointF *endpoint, const GpPointF *nextpoint,
1684 GpPen *pen, int right_side, path_list_node_t **last_point)
1685 {
1686 REAL segment_dy = nextpoint->Y-endpoint->Y;
1687 REAL segment_dx = nextpoint->X-endpoint->X;
1688 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1689 REAL distance = pen->width/2.0;
1690 REAL bevel_dx, bevel_dy;
1691
1692 if (segment_length == 0.0)
1693 {
1694 *last_point = add_path_list_node(*last_point, endpoint->X,
1695 endpoint->Y, PathPointTypeLine);
1696 return;
1697 }
1698
1699 if (right_side)
1700 {
1701 bevel_dx = -distance * segment_dy / segment_length;
1702 bevel_dy = distance * segment_dx / segment_length;
1703 }
1704 else
1705 {
1706 bevel_dx = distance * segment_dy / segment_length;
1707 bevel_dy = -distance * segment_dx / segment_length;
1708 }
1709
1710 *last_point = add_path_list_node(*last_point, endpoint->X + bevel_dx,
1711 endpoint->Y + bevel_dy, PathPointTypeLine);
1712 }
1713
1714 static void widen_joint(const GpPointF *p1, const GpPointF *p2, const GpPointF *p3,
1715 GpPen* pen, path_list_node_t **last_point)
1716 {
1717 switch (pen->join)
1718 {
1719 case LineJoinMiter:
1720 case LineJoinMiterClipped:
1721 if ((p2->X - p1->X) * (p3->Y - p1->Y) > (p2->Y - p1->Y) * (p3->X - p1->X))
1722 {
1723 float distance = pen->width/2.0;
1724 float length_0 = sqrtf((p2->X-p1->X)*(p2->X-p1->X)+(p2->Y-p1->Y)*(p2->Y-p1->Y));
1725 float length_1 = sqrtf((p3->X-p2->X)*(p3->X-p2->X)+(p3->Y-p2->Y)*(p3->Y-p2->Y));
1726 float dx0 = distance * (p2->X - p1->X) / length_0;
1727 float dy0 = distance * (p2->Y - p1->Y) / length_0;
1728 float dx1 = distance * (p3->X - p2->X) / length_1;
1729 float dy1 = distance * (p3->Y - p2->Y) / length_1;
1730 float det = (dy0*dx1 - dx0*dy1);
1731 float dx = (dx0*dx1*(dx0-dx1) + dy0*dy0*dx1 - dy1*dy1*dx0)/det;
1732 float dy = (dy0*dy1*(dy0-dy1) + dx0*dx0*dy1 - dx1*dx1*dy0)/det;
1733 if (dx*dx + dy*dy < pen->miterlimit*pen->miterlimit * distance*distance)
1734 {
1735 *last_point = add_path_list_node(*last_point, p2->X + dx,
1736 p2->Y + dy, PathPointTypeLine);
1737 break;
1738 }
1739 else if (pen->join == LineJoinMiter)
1740 {
1741 static int once;
1742 if (!once++)
1743 FIXME("should add a clipped corner\n");
1744 }
1745 /* else fall-through */
1746 }
1747 /* else fall-through */
1748 default:
1749 case LineJoinBevel:
1750 add_bevel_point(p2, p1, pen, 1, last_point);
1751 add_bevel_point(p2, p3, pen, 0, last_point);
1752 break;
1753 }
1754 }
1755
1756 static void widen_cap(const GpPointF *endpoint, const GpPointF *nextpoint,
1757 GpPen *pen, GpLineCap cap, GpCustomLineCap *custom, int add_first_points,
1758 int add_last_point, path_list_node_t **last_point)
1759 {
1760 switch (cap)
1761 {
1762 default:
1763 case LineCapFlat:
1764 if (add_first_points)
1765 add_bevel_point(endpoint, nextpoint, pen, 1, last_point);
1766 if (add_last_point)
1767 add_bevel_point(endpoint, nextpoint, pen, 0, last_point);
1768 break;
1769 case LineCapSquare:
1770 {
1771 REAL segment_dy = nextpoint->Y-endpoint->Y;
1772 REAL segment_dx = nextpoint->X-endpoint->X;
1773 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1774 REAL distance = pen->width/2.0;
1775 REAL bevel_dx, bevel_dy;
1776 REAL extend_dx, extend_dy;
1777
1778 extend_dx = -distance * segment_dx / segment_length;
1779 extend_dy = -distance * segment_dy / segment_length;
1780
1781 bevel_dx = -distance * segment_dy / segment_length;
1782 bevel_dy = distance * segment_dx / segment_length;
1783
1784 if (add_first_points)
1785 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx + bevel_dx,
1786 endpoint->Y + extend_dy + bevel_dy, PathPointTypeLine);
1787
1788 if (add_last_point)
1789 *last_point = add_path_list_node(*last_point, endpoint->X + extend_dx - bevel_dx,
1790 endpoint->Y + extend_dy - bevel_dy, PathPointTypeLine);
1791
1792 break;
1793 }
1794 case LineCapRound:
1795 {
1796 REAL segment_dy = nextpoint->Y-endpoint->Y;
1797 REAL segment_dx = nextpoint->X-endpoint->X;
1798 REAL segment_length = sqrtf(segment_dy*segment_dy + segment_dx*segment_dx);
1799 REAL distance = pen->width/2.0;
1800 REAL dx, dy, dx2, dy2;
1801 const REAL control_point_distance = 0.5522847498307935; /* 4/3 * (sqrt(2) - 1) */
1802
1803 if (add_first_points)
1804 {
1805 dx = -distance * segment_dx / segment_length;
1806 dy = -distance * segment_dy / segment_length;
1807
1808 dx2 = dx * control_point_distance;
1809 dy2 = dy * control_point_distance;
1810
1811 /* first 90-degree arc */
1812 *last_point = add_path_list_node(*last_point, endpoint->X + dy,
1813 endpoint->Y - dx, PathPointTypeLine);
1814
1815 *last_point = add_path_list_node(*last_point, endpoint->X + dy + dx2,
1816 endpoint->Y - dx + dy2, PathPointTypeBezier);
1817
1818 *last_point = add_path_list_node(*last_point, endpoint->X + dx + dy2,
1819 endpoint->Y + dy - dx2, PathPointTypeBezier);
1820
1821 /* midpoint */
1822 *last_point = add_path_list_node(*last_point, endpoint->X + dx,
1823 endpoint->Y + dy, PathPointTypeBezier);
1824
1825 /* second 90-degree arc */
1826 *last_point = add_path_list_node(*last_point, endpoint->X + dx - dy2,
1827 endpoint->Y + dy + dx2, PathPointTypeBezier);
1828
1829 *last_point = add_path_list_node(*last_point, endpoint->X - dy + dx2,
1830 endpoint->Y + dx + dy2, PathPointTypeBezier);
1831
1832 *last_point = add_path_list_node(*last_point, endpoint->X - dy,
1833 endpoint->Y + dx, PathPointTypeBezier);
1834 }
1835 break;
1836 }
1837 }
1838 }
1839
1840 static void widen_open_figure(GpPath *path, GpPen *pen, int start, int end,
1841 path_list_node_t **last_point)
1842 {
1843 int i;
1844 path_list_node_t *prev_point;
1845
1846 if (end <= start)
1847 return;
1848
1849 prev_point = *last_point;
1850
1851 widen_cap(&path->pathdata.Points[start], &path->pathdata.Points[start+1],
1852 pen, pen->startcap, pen->customstart, FALSE, TRUE, last_point);
1853
1854 for (i=start+1; i<end; i++)
1855 widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
1856 &path->pathdata.Points[i+1], pen, last_point);
1857
1858 widen_cap(&path->pathdata.Points[end], &path->pathdata.Points[end-1],
1859 pen, pen->endcap, pen->customend, TRUE, TRUE, last_point);
1860
1861 for (i=end-1; i>start; i--)
1862 widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
1863 &path->pathdata.Points[i-1], pen, last_point);
1864
1865 widen_cap(&path->pathdata.Points[start], &path->pathdata.Points[start+1],
1866 pen, pen->startcap, pen->customstart, TRUE, FALSE, last_point);
1867
1868 prev_point->next->type = PathPointTypeStart;
1869 (*last_point)->type |= PathPointTypeCloseSubpath;
1870 }
1871
1872 static void widen_closed_figure(GpPath *path, GpPen *pen, int start, int end,
1873 path_list_node_t **last_point)
1874 {
1875 int i;
1876 path_list_node_t *prev_point;
1877
1878 if (end <= start+1)
1879 return;
1880
1881 /* left outline */
1882 prev_point = *last_point;
1883
1884 widen_joint(&path->pathdata.Points[end], &path->pathdata.Points[start],
1885 &path->pathdata.Points[start+1], pen, last_point);
1886
1887 for (i=start+1; i<end; i++)
1888 widen_joint(&path->pathdata.Points[i-1], &path->pathdata.Points[i],
1889 &path->pathdata.Points[i+1], pen, last_point);
1890
1891 widen_joint(&path->pathdata.Points[end-1], &path->pathdata.Points[end],
1892 &path->pathdata.Points[start], pen, last_point);
1893
1894 prev_point->next->type = PathPointTypeStart;
1895 (*last_point)->type |= PathPointTypeCloseSubpath;
1896
1897 /* right outline */
1898 prev_point = *last_point;
1899
1900 widen_joint(&path->pathdata.Points[start], &path->pathdata.Points[end],
1901 &path->pathdata.Points[end-1], pen, last_point);
1902
1903 for (i=end-1; i>start; i--)
1904 widen_joint(&path->pathdata.Points[i+1], &path->pathdata.Points[i],
1905 &path->pathdata.Points[i-1], pen, last_point);
1906
1907 widen_joint(&path->pathdata.Points[start+1], &path->pathdata.Points[start],
1908 &path->pathdata.Points[end], pen, last_point);
1909
1910 prev_point->next->type = PathPointTypeStart;
1911 (*last_point)->type |= PathPointTypeCloseSubpath;
1912 }
1913
1914 GpStatus WINGDIPAPI GdipWidenPath(GpPath *path, GpPen *pen, GpMatrix *matrix,
1915 REAL flatness)
1916 {
1917 GpPath *flat_path=NULL;
1918 GpStatus status;
1919 path_list_node_t *points=NULL, *last_point=NULL;
1920 int i, subpath_start=0, new_length;
1921 BYTE type;
1922
1923 TRACE("(%p,%p,%p,%0.2f)\n", path, pen, matrix, flatness);
1924
1925 if (!path || !pen)
1926 return InvalidParameter;
1927
1928 if (path->pathdata.Count <= 1)
1929 return OutOfMemory;
1930
1931 status = GdipClonePath(path, &flat_path);
1932
1933 if (status == Ok)
1934 status = GdipFlattenPath(flat_path, matrix, flatness);
1935
1936 if (status == Ok && !init_path_list(&points, 314.0, 22.0))
1937 status = OutOfMemory;
1938
1939 if (status == Ok)
1940 {
1941 last_point = points;
1942
1943 if (pen->endcap > LineCapRound)
1944 FIXME("unimplemented end cap %x\n", pen->endcap);
1945
1946 if (pen->startcap > LineCapRound)
1947 FIXME("unimplemented start cap %x\n", pen->startcap);
1948
1949 if (pen->dashcap != DashCapFlat)
1950 FIXME("unimplemented dash cap %d\n", pen->dashcap);
1951
1952 if (pen->join == LineJoinRound)
1953 FIXME("unimplemented line join %d\n", pen->join);
1954
1955 if (pen->dash != DashStyleSolid)
1956 FIXME("unimplemented dash style %d\n", pen->dash);
1957
1958 if (pen->align != PenAlignmentCenter)
1959 FIXME("unimplemented pen alignment %d\n", pen->align);
1960
1961 for (i=0; i < flat_path->pathdata.Count; i++)
1962 {
1963 type = flat_path->pathdata.Types[i];
1964
1965 if ((type&PathPointTypePathTypeMask) == PathPointTypeStart)
1966 subpath_start = i;
1967
1968 if ((type&PathPointTypeCloseSubpath) == PathPointTypeCloseSubpath)
1969 {
1970 widen_closed_figure(flat_path, pen, subpath_start, i, &last_point);
1971 }
1972 else if (i == flat_path->pathdata.Count-1 ||
1973 (flat_path->pathdata.Types[i+1]&PathPointTypePathTypeMask) == PathPointTypeStart)
1974 {
1975 widen_open_figure(flat_path, pen, subpath_start, i, &last_point);
1976 }
1977 }
1978
1979 new_length = path_list_count(points)-1;
1980
1981 if (!lengthen_path(path, new_length))
1982 status = OutOfMemory;
1983 }
1984
1985 if (status == Ok)
1986 {
1987 path->pathdata.Count = new_length;
1988
1989 last_point = points->next;
1990 for (i = 0; i < new_length; i++)
1991 {
1992 path->pathdata.Points[i] = last_point->pt;
1993 path->pathdata.Types[i] = last_point->type;
1994 last_point = last_point->next;
1995 }
1996
1997 path->fill = FillModeWinding;
1998 }
1999
2000 free_path_list(points);
2001
2002 GdipDeletePath(flat_path);
2003
2004 return status;
2005 }
2006
2007 GpStatus WINGDIPAPI GdipAddPathRectangle(GpPath *path, REAL x, REAL y,
2008 REAL width, REAL height)
2009 {
2010 GpPath *backup;
2011 GpPointF ptf[2];
2012 GpStatus retstat;
2013 BOOL old_new;
2014
2015 TRACE("(%p, %.2f, %.2f, %.2f, %.2f)\n", path, x, y, width, height);
2016
2017 if(!path)
2018 return InvalidParameter;
2019
2020 /* make a backup copy of path data */
2021 if((retstat = GdipClonePath(path, &backup)) != Ok)
2022 return retstat;
2023
2024 /* rectangle should start as new path */
2025 old_new = path->newfigure;
2026 path->newfigure = TRUE;
2027 if((retstat = GdipAddPathLine(path,x,y,x+width,y)) != Ok){
2028 path->newfigure = old_new;
2029 goto fail;
2030 }
2031
2032 ptf[0].X = x+width;
2033 ptf[0].Y = y+height;
2034 ptf[1].X = x;
2035 ptf[1].Y = y+height;
2036
2037 if((retstat = GdipAddPathLine2(path, ptf, 2)) != Ok) goto fail;
2038 path->pathdata.Types[path->pathdata.Count-1] |= PathPointTypeCloseSubpath;
2039
2040 /* free backup */
2041 GdipDeletePath(backup);
2042 return Ok;
2043
2044 fail:
2045 /* reverting */
2046 GdipFree(path->pathdata.Points);
2047 GdipFree(path->pathdata.Types);
2048 memcpy(path, backup, sizeof(*path));
2049 GdipFree(backup);
2050
2051 return retstat;
2052 }
2053
2054 GpStatus WINGDIPAPI GdipAddPathRectangleI(GpPath *path, INT x, INT y,
2055 INT width, INT height)
2056 {
2057 TRACE("(%p, %d, %d, %d, %d)\n", path, x, y, width, height);
2058
2059 return GdipAddPathRectangle(path,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2060 }
2061
2062 GpStatus WINGDIPAPI GdipAddPathRectangles(GpPath *path, GDIPCONST GpRectF *rects, INT count)
2063 {
2064 GpPath *backup;
2065 GpStatus retstat;
2066 INT i;
2067
2068 TRACE("(%p, %p, %d)\n", path, rects, count);
2069
2070 /* count == 0 - verified condition */
2071 if(!path || !rects || count == 0)
2072 return InvalidParameter;
2073
2074 if(count < 0)
2075 return OutOfMemory;
2076
2077 /* make a backup copy */
2078 if((retstat = GdipClonePath(path, &backup)) != Ok)
2079 return retstat;
2080
2081 for(i = 0; i < count; i++){
2082 if((retstat = GdipAddPathRectangle(path,rects[i].X,rects[i].Y,rects[i].Width,rects[i].Height)) != Ok)
2083 goto fail;
2084 }
2085
2086 /* free backup */
2087 GdipDeletePath(backup);
2088 return Ok;
2089
2090 fail:
2091 /* reverting */
2092 GdipFree(path->pathdata.Points);
2093 GdipFree(path->pathdata.Types);
2094 memcpy(path, backup, sizeof(*path));
2095 GdipFree(backup);
2096
2097 return retstat;
2098 }
2099
2100 GpStatus WINGDIPAPI GdipAddPathRectanglesI(GpPath *path, GDIPCONST GpRect *rects, INT count)
2101 {
2102 GpRectF *rectsF;
2103 GpStatus retstat;
2104 INT i;
2105
2106 TRACE("(%p, %p, %d)\n", path, rects, count);
2107
2108 if(!rects || count == 0)
2109 return InvalidParameter;
2110
2111 if(count < 0)
2112 return OutOfMemory;
2113
2114 rectsF = GdipAlloc(sizeof(GpRectF)*count);
2115
2116 for(i = 0;i < count;i++){
2117 rectsF[i].X = (REAL)rects[i].X;
2118 rectsF[i].Y = (REAL)rects[i].Y;
2119 rectsF[i].Width = (REAL)rects[i].Width;
2120 rectsF[i].Height = (REAL)rects[i].Height;
2121 }
2122
2123 retstat = GdipAddPathRectangles(path, rectsF, count);
2124 GdipFree(rectsF);
2125
2126 return retstat;
2127 }
2128
2129 GpStatus WINGDIPAPI GdipSetPathMarker(GpPath* path)
2130 {
2131 INT count;
2132
2133 TRACE("(%p)\n", path);
2134
2135 if(!path)
2136 return InvalidParameter;
2137
2138 count = path->pathdata.Count;
2139
2140 /* set marker flag */
2141 if(count > 0)
2142 path->pathdata.Types[count-1] |= PathPointTypePathMarker;
2143
2144 return Ok;
2145 }
2146
2147 GpStatus WINGDIPAPI GdipClearPathMarkers(GpPath* path)
2148 {
2149 INT count;
2150 INT i;
2151
2152 TRACE("(%p)\n", path);
2153
2154 if(!path)
2155 return InvalidParameter;
2156
2157 count = path->pathdata.Count;
2158
2159 for(i = 0; i < count - 1; i++){
2160 path->pathdata.Types[i] &= ~PathPointTypePathMarker;
2161 }
2162
2163 return Ok;
2164 }
2165
2166 GpStatus WINGDIPAPI GdipWindingModeOutline(GpPath *path, GpMatrix *matrix, REAL flatness)
2167 {
2168 FIXME("stub: %p, %p, %.2f\n", path, matrix, flatness);
2169 return NotImplemented;
2170 }