MagickCore 6.9.13-47
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
draw.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% DDDD RRRR AAA W W %
7% D D R R A A W W %
8% D D RRRR AAAAA W W W %
9% D D R RN A A WW WW %
10% DDDD R R A A W W %
11% %
12% %
13% MagickCore Image Drawing Methods %
14% %
15% %
16% Software Design %
17% Cristy %
18% July 1998 %
19% %
20% %
21% Copyright 1999 ImageMagick Studio LLC, a non-profit organization %
22% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% https://imagemagick.org/license/ %
28% %
29% Unless required by applicable law or agreed to in writing, software %
30% distributed under the License is distributed on an "AS IS" BASIS, %
31% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32% See the License for the specific language governing permissions and %
33% limitations under the License. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37% Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38% rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39% Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40% (www.appligent.com) contributed the dash pattern, linecap stroking
41% algorithm, and minor rendering improvements.
42%
43*/
44
45/*
46 Include declarations.
47*/
48#include "magick/studio.h"
49#include "magick/annotate.h"
50#include "magick/artifact.h"
51#include "magick/blob.h"
52#include "magick/cache.h"
53#include "magick/cache-private.h"
54#include "magick/cache-view.h"
55#include "magick/channel.h"
56#include "magick/color.h"
57#include "magick/color-private.h"
58#include "magick/colorspace.h"
59#include "magick/colorspace-private.h"
60#include "magick/composite.h"
61#include "magick/composite-private.h"
62#include "magick/constitute.h"
63#include "magick/draw.h"
64#include "magick/draw-private.h"
65#include "magick/enhance.h"
66#include "magick/exception.h"
67#include "magick/exception-private.h"
68#include "magick/gem.h"
69#include "magick/geometry.h"
70#include "magick/image-private.h"
71#include "magick/list.h"
72#include "magick/log.h"
73#include "magick/magick.h"
74#include "magick/memory-private.h"
75#include "magick/monitor.h"
76#include "magick/monitor-private.h"
77#include "magick/option.h"
78#include "magick/paint.h"
79#include "magick/pixel-accessor.h"
80#include "magick/pixel-private.h"
81#include "magick/property.h"
82#include "magick/resample.h"
83#include "magick/resample-private.h"
84#include "magick/resource_.h"
85#include "magick/splay-tree.h"
86#include "magick/string_.h"
87#include "magick/string-private.h"
88#include "magick/thread-private.h"
89#include "magick/token.h"
90#include "magick/transform.h"
91#include "magick/utility.h"
92
93/*
94 Define declarations.
95*/
96#define AntialiasThreshold (1.0/3.0)
97#define BezierQuantum 200
98#define PrimitiveExtentPad 4296
99#define MaxBezierCoordinates 67108864
100#define ThrowPointExpectedException(image,token) \
101{ \
102 (void) ThrowMagickException(&(image)->exception,GetMagickModule(),DrawError, \
103 "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
104 status=MagickFalse; \
105 break; \
106}
107
108/*
109 Typedef declarations.
110*/
111typedef struct _EdgeInfo
112{
113 SegmentInfo
114 bounds;
115
116 double
117 scanline;
118
119 PointInfo
120 *points;
121
122 size_t
123 number_points;
124
125 ssize_t
126 direction;
127
128 MagickBooleanType
129 ghostline;
130
131 size_t
132 highwater;
133} EdgeInfo;
134
135typedef struct _ElementInfo
136{
137 double
138 cx,
139 cy,
140 major,
141 minor,
142 angle;
143} ElementInfo;
144
145typedef struct _MVGInfo
146{
147 PrimitiveInfo
148 **primitive_info;
149
150 size_t
151 *extent;
152
153 ssize_t
154 offset;
155
156 PointInfo
157 point;
158
159 ExceptionInfo
160 *exception;
161} MVGInfo;
162
163typedef struct _PolygonInfo
164{
165 EdgeInfo
166 *edges;
167
168 size_t
169 number_edges;
170} PolygonInfo;
171
172typedef enum
173{
174 MoveToCode,
175 OpenCode,
176 GhostlineCode,
177 LineToCode,
178 EndCode
179} PathInfoCode;
180
181typedef struct _PathInfo
182{
183 PointInfo
184 point;
185
186 PathInfoCode
187 code;
188} PathInfo;
189
190/*
191 Forward declarations.
192*/
193static Image
194 *DrawClippingMask(Image *,const DrawInfo *,const char *,const char *,
195 ExceptionInfo *);
196
197static MagickBooleanType
198 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *),
199 RenderMVGContent(Image *,const DrawInfo *,const size_t),
200 TraceArc(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
201 TraceArcPath(MVGInfo *,const PointInfo,const PointInfo,const PointInfo,
202 const double,const MagickBooleanType,const MagickBooleanType),
203 TraceBezier(MVGInfo *,const size_t),
204 TraceCircle(MVGInfo *,const PointInfo,const PointInfo),
205 TraceEllipse(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
206 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
207 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
208 TraceRoundRectangle(MVGInfo *,const PointInfo,const PointInfo,PointInfo),
209 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
210
211static PrimitiveInfo
212 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *,ExceptionInfo *);
213
214static ssize_t
215 TracePath(Image *,MVGInfo *,const char *);
216
217/*
218%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219% %
220% %
221% %
222% A c q u i r e D r a w I n f o %
223% %
224% %
225% %
226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227%
228% AcquireDrawInfo() returns a DrawInfo structure properly initialized.
229%
230% The format of the AcquireDrawInfo method is:
231%
232% DrawInfo *AcquireDrawInfo(void)
233%
234*/
235MagickExport DrawInfo *AcquireDrawInfo(void)
236{
237 DrawInfo
238 *draw_info;
239
240 draw_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*draw_info));
241 GetDrawInfo((ImageInfo *) NULL,draw_info);
242 return(draw_info);
243}
244
245/*
246%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247% %
248% %
249% %
250% C l o n e D r a w I n f o %
251% %
252% %
253% %
254%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255%
256% CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
257% is specified, a new DrawInfo structure is created initialized to default
258% values.
259%
260% The format of the CloneDrawInfo method is:
261%
262% DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
263% const DrawInfo *draw_info)
264%
265% A description of each parameter follows:
266%
267% o image_info: the image info.
268%
269% o draw_info: the draw info.
270%
271*/
272MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
273 const DrawInfo *draw_info)
274{
275 DrawInfo
276 *clone_info;
277
278 clone_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*clone_info));
279 GetDrawInfo(image_info,clone_info);
280 if (draw_info == (DrawInfo *) NULL)
281 return(clone_info);
282 if (draw_info->id != (char *) NULL)
283 (void) CloneString(&clone_info->id,draw_info->id);
284 if (draw_info->primitive != (char *) NULL)
285 (void) CloneString(&clone_info->primitive,draw_info->primitive);
286 if (draw_info->geometry != (char *) NULL)
287 (void) CloneString(&clone_info->geometry,draw_info->geometry);
288 clone_info->compliance=draw_info->compliance;
289 clone_info->viewbox=draw_info->viewbox;
290 clone_info->affine=draw_info->affine;
291 clone_info->gravity=draw_info->gravity;
292 clone_info->fill=draw_info->fill;
293 clone_info->stroke=draw_info->stroke;
294 clone_info->stroke_width=draw_info->stroke_width;
295 if (draw_info->fill_pattern != (Image *) NULL)
296 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
297 &draw_info->fill_pattern->exception);
298 else
299 if (draw_info->tile != (Image *) NULL)
300 clone_info->fill_pattern=CloneImage(draw_info->tile,0,0,MagickTrue,
301 &draw_info->tile->exception);
302 clone_info->tile=NewImageList(); /* tile is deprecated */
303 if (draw_info->stroke_pattern != (Image *) NULL)
304 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
305 MagickTrue,&draw_info->stroke_pattern->exception);
306 clone_info->stroke_antialias=draw_info->stroke_antialias;
307 clone_info->text_antialias=draw_info->text_antialias;
308 clone_info->fill_rule=draw_info->fill_rule;
309 clone_info->linecap=draw_info->linecap;
310 clone_info->linejoin=draw_info->linejoin;
311 clone_info->miterlimit=draw_info->miterlimit;
312 clone_info->dash_offset=draw_info->dash_offset;
313 clone_info->decorate=draw_info->decorate;
314 clone_info->compose=draw_info->compose;
315 if (draw_info->text != (char *) NULL)
316 (void) CloneString(&clone_info->text,draw_info->text);
317 if (draw_info->font != (char *) NULL)
318 (void) CloneString(&clone_info->font,draw_info->font);
319 if (draw_info->metrics != (char *) NULL)
320 (void) CloneString(&clone_info->metrics,draw_info->metrics);
321 if (draw_info->family != (char *) NULL)
322 (void) CloneString(&clone_info->family,draw_info->family);
323 clone_info->style=draw_info->style;
324 clone_info->stretch=draw_info->stretch;
325 clone_info->weight=draw_info->weight;
326 if (draw_info->encoding != (char *) NULL)
327 (void) CloneString(&clone_info->encoding,draw_info->encoding);
328 clone_info->pointsize=draw_info->pointsize;
329 clone_info->kerning=draw_info->kerning;
330 clone_info->interline_spacing=draw_info->interline_spacing;
331 clone_info->interword_spacing=draw_info->interword_spacing;
332 clone_info->direction=draw_info->direction;
333 if (draw_info->density != (char *) NULL)
334 (void) CloneString(&clone_info->density,draw_info->density);
335 clone_info->align=draw_info->align;
336 clone_info->undercolor=draw_info->undercolor;
337 clone_info->border_color=draw_info->border_color;
338 if (draw_info->server_name != (char *) NULL)
339 (void) CloneString(&clone_info->server_name,draw_info->server_name);
340 if (draw_info->dash_pattern != (double *) NULL)
341 {
342 ssize_t
343 x;
344
345 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
346 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) (2*x+2),
347 sizeof(*clone_info->dash_pattern));
348 if (clone_info->dash_pattern == (double *) NULL)
349 ThrowFatalException(ResourceLimitFatalError,
350 "UnableToAllocateDashPattern");
351 (void) memset(clone_info->dash_pattern,0,(size_t) (2*x+2)*
352 sizeof(*clone_info->dash_pattern));
353 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(size_t)
354 (x+1)*sizeof(*clone_info->dash_pattern));
355 }
356 clone_info->gradient=draw_info->gradient;
357 if (draw_info->gradient.stops != (StopInfo *) NULL)
358 {
359 size_t
360 number_stops;
361
362 number_stops=clone_info->gradient.number_stops;
363 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
364 number_stops,sizeof(*clone_info->gradient.stops));
365 if (clone_info->gradient.stops == (StopInfo *) NULL)
366 ThrowFatalException(ResourceLimitFatalError,
367 "UnableToAllocateDashPattern");
368 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
369 (size_t) number_stops*sizeof(*clone_info->gradient.stops));
370 }
371 clone_info->bounds=draw_info->bounds;
372 clone_info->fill_opacity=draw_info->fill_opacity;
373 clone_info->stroke_opacity=draw_info->stroke_opacity;
374 clone_info->element_reference=draw_info->element_reference;
375 clone_info->clip_path=draw_info->clip_path;
376 clone_info->clip_units=draw_info->clip_units;
377 if (draw_info->clip_mask != (char *) NULL)
378 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
379 if (draw_info->clipping_mask != (Image *) NULL)
380 clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
381 MagickTrue,&draw_info->clipping_mask->exception);
382 if (draw_info->composite_mask != (Image *) NULL)
383 clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
384 MagickTrue,&draw_info->composite_mask->exception);
385 clone_info->render=draw_info->render;
386 clone_info->debug=draw_info->debug;
387 return(clone_info);
388}
389
390/*
391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392% %
393% %
394% %
395+ C o n v e r t P a t h T o P o l y g o n %
396% %
397% %
398% %
399%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400%
401% ConvertPathToPolygon() converts a path to the more efficient sorted
402% rendering form.
403%
404% The format of the ConvertPathToPolygon method is:
405%
406% PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info,
407% ExceptionInfo *exception)
408%
409% A description of each parameter follows:
410%
411% o ConvertPathToPolygon() returns the path in a more efficient sorted
412% rendering form of type PolygonInfo.
413%
414% o draw_info: Specifies a pointer to an DrawInfo structure.
415%
416% o path_info: Specifies a pointer to an PathInfo structure.
417%
418% o exception: return any errors or warnings in this structure.
419%
420*/
421
422static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
423{
424 ssize_t
425 i;
426
427 if (polygon_info->edges != (EdgeInfo *) NULL)
428 {
429 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
430 if (polygon_info->edges[i].points != (PointInfo *) NULL)
431 polygon_info->edges[i].points=(PointInfo *)
432 RelinquishMagickMemory(polygon_info->edges[i].points);
433 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(
434 polygon_info->edges);
435 }
436 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
437}
438
439#if defined(__cplusplus) || defined(c_plusplus)
440extern "C" {
441#endif
442
443static int DrawCompareEdges(const void *p_edge,const void *q_edge)
444{
445#define DrawCompareEdge(p,q) \
446{ \
447 if (((p)-(q)) < 0.0) \
448 return(-1); \
449 if (((p)-(q)) > 0.0) \
450 return(1); \
451}
452
453 const PointInfo
454 *p,
455 *q;
456
457 /*
458 Edge sorting for right-handed coordinate system.
459 */
460 p=((const EdgeInfo *) p_edge)->points;
461 q=((const EdgeInfo *) q_edge)->points;
462 DrawCompareEdge(p[0].y,q[0].y);
463 DrawCompareEdge(p[0].x,q[0].x);
464 DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
465 (q[1].x-q[0].x));
466 DrawCompareEdge(p[1].y,q[1].y);
467 DrawCompareEdge(p[1].x,q[1].x);
468 return(0);
469}
470
471#if defined(__cplusplus) || defined(c_plusplus)
472}
473#endif
474
475static void LogPolygonInfo(const PolygonInfo *polygon_info)
476{
477 EdgeInfo
478 *p;
479
480 ssize_t
481 i,
482 j;
483
484 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
485 p=polygon_info->edges;
486 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
487 {
488 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
489 (double) i);
490 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
491 p->direction != MagickFalse ? "down" : "up");
492 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
493 p->ghostline != MagickFalse ? "transparent" : "opaque");
494 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
495 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
496 p->bounds.x2,p->bounds.y2);
497 for (j=0; j < (ssize_t) p->number_points; j++)
498 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g,%g",
499 p->points[j].x,p->points[j].y);
500 p++;
501 }
502 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
503}
504
505static void ReversePoints(PointInfo *points,const size_t number_points)
506{
507 PointInfo
508 point;
509
510 ssize_t
511 i;
512
513 for (i=0; i < (ssize_t) (number_points >> 1); i++)
514 {
515 point=points[i];
516 points[i]=points[number_points-(i+1)];
517 points[number_points-(i+1)]=point;
518 }
519}
520
521static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info,
522 ExceptionInfo *exception)
523{
524 long
525 direction,
526 next_direction;
527
528 PointInfo
529 point,
530 *points;
531
532 PolygonInfo
533 *polygon_info;
534
535 SegmentInfo
536 bounds;
537
538 ssize_t
539 i,
540 n;
541
542 MagickBooleanType
543 ghostline;
544
545 size_t
546 edge,
547 number_edges,
548 number_points;
549
550 /*
551 Convert a path to the more efficient sorted rendering form.
552 */
553 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
554 if (polygon_info == (PolygonInfo *) NULL)
555 {
556 (void) ThrowMagickException(exception,GetMagickModule(),
557 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
558 return((PolygonInfo *) NULL);
559 }
560 number_edges=16;
561 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
562 sizeof(*polygon_info->edges));
563 if (polygon_info->edges == (EdgeInfo *) NULL)
564 {
565 (void) ThrowMagickException(exception,GetMagickModule(),
566 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
567 return(DestroyPolygonInfo(polygon_info));
568 }
569 (void) memset(polygon_info->edges,0,number_edges*
570 sizeof(*polygon_info->edges));
571 direction=0;
572 edge=0;
573 ghostline=MagickFalse;
574 n=0;
575 number_points=0;
576 points=(PointInfo *) NULL;
577 (void) memset(&point,0,sizeof(point));
578 (void) memset(&bounds,0,sizeof(bounds));
579 polygon_info->edges[edge].number_points=(size_t) n;
580 polygon_info->edges[edge].scanline=0.0;
581 polygon_info->edges[edge].highwater=0;
582 polygon_info->edges[edge].ghostline=ghostline;
583 polygon_info->edges[edge].direction=(ssize_t) direction;
584 polygon_info->edges[edge].points=points;
585 polygon_info->edges[edge].bounds=bounds;
586 polygon_info->number_edges=0;
587 for (i=0; path_info[i].code != EndCode; i++)
588 {
589 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
590 (path_info[i].code == GhostlineCode))
591 {
592 /*
593 Move to.
594 */
595 if ((points != (PointInfo *) NULL) && (n >= 2))
596 {
597 if (edge == number_edges)
598 {
599 number_edges<<=1;
600 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
601 polygon_info->edges,(size_t) number_edges,
602 sizeof(*polygon_info->edges));
603 if (polygon_info->edges == (EdgeInfo *) NULL)
604 {
605 (void) ThrowMagickException(exception,GetMagickModule(),
606 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
607 points=(PointInfo *) RelinquishMagickMemory(points);
608 return(DestroyPolygonInfo(polygon_info));
609 }
610 }
611 polygon_info->edges[edge].number_points=(size_t) n;
612 polygon_info->edges[edge].scanline=(-1.0);
613 polygon_info->edges[edge].highwater=0;
614 polygon_info->edges[edge].ghostline=ghostline;
615 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
616 if (direction < 0)
617 ReversePoints(points,(size_t) n);
618 polygon_info->edges[edge].points=points;
619 polygon_info->edges[edge].bounds=bounds;
620 polygon_info->edges[edge].bounds.y1=points[0].y;
621 polygon_info->edges[edge].bounds.y2=points[n-1].y;
622 points=(PointInfo *) NULL;
623 ghostline=MagickFalse;
624 edge++;
625 polygon_info->number_edges=edge;
626 }
627 if (points == (PointInfo *) NULL)
628 {
629 number_points=16;
630 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
631 sizeof(*points));
632 if (points == (PointInfo *) NULL)
633 {
634 (void) ThrowMagickException(exception,GetMagickModule(),
635 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
636 return(DestroyPolygonInfo(polygon_info));
637 }
638 }
639 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
640 point=path_info[i].point;
641 points[0]=point;
642 bounds.x1=point.x;
643 bounds.x2=point.x;
644 direction=0;
645 n=1;
646 continue;
647 }
648 /*
649 Line to.
650 */
651 next_direction=((path_info[i].point.y > point.y) ||
652 ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
653 (path_info[i].point.x > point.x))) ? 1 : -1;
654 if ((points != (PointInfo *) NULL) && (direction != 0) &&
655 (direction != next_direction))
656 {
657 /*
658 New edge.
659 */
660 point=points[n-1];
661 if (edge == number_edges)
662 {
663 number_edges<<=1;
664 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
665 polygon_info->edges,(size_t) number_edges,
666 sizeof(*polygon_info->edges));
667 if (polygon_info->edges == (EdgeInfo *) NULL)
668 {
669 (void) ThrowMagickException(exception,GetMagickModule(),
670 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
671 points=(PointInfo *) RelinquishMagickMemory(points);
672 return(DestroyPolygonInfo(polygon_info));
673 }
674 }
675 polygon_info->edges[edge].number_points=(size_t) n;
676 polygon_info->edges[edge].scanline=(-1.0);
677 polygon_info->edges[edge].highwater=0;
678 polygon_info->edges[edge].ghostline=ghostline;
679 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
680 if (direction < 0)
681 ReversePoints(points,(size_t) n);
682 polygon_info->edges[edge].points=points;
683 polygon_info->edges[edge].bounds=bounds;
684 polygon_info->edges[edge].bounds.y1=points[0].y;
685 polygon_info->edges[edge].bounds.y2=points[n-1].y;
686 polygon_info->number_edges=edge+1;
687 points=(PointInfo *) NULL;
688 number_points=16;
689 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
690 sizeof(*points));
691 if (points == (PointInfo *) NULL)
692 {
693 (void) ThrowMagickException(exception,GetMagickModule(),
694 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
695 return(DestroyPolygonInfo(polygon_info));
696 }
697 n=1;
698 ghostline=MagickFalse;
699 points[0]=point;
700 bounds.x1=point.x;
701 bounds.x2=point.x;
702 edge++;
703 }
704 direction=next_direction;
705 if (points == (PointInfo *) NULL)
706 continue;
707 if (n == (ssize_t) number_points)
708 {
709 number_points<<=1;
710 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
711 sizeof(*points));
712 if (points == (PointInfo *) NULL)
713 {
714 (void) ThrowMagickException(exception,GetMagickModule(),
715 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
716 return(DestroyPolygonInfo(polygon_info));
717 }
718 }
719 point=path_info[i].point;
720 points[n]=point;
721 if (point.x < bounds.x1)
722 bounds.x1=point.x;
723 if (point.x > bounds.x2)
724 bounds.x2=point.x;
725 n++;
726 }
727 if (points != (PointInfo *) NULL)
728 {
729 if (n < 2)
730 points=(PointInfo *) RelinquishMagickMemory(points);
731 else
732 {
733 if (edge == number_edges)
734 {
735 number_edges<<=1;
736 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
737 polygon_info->edges,(size_t) number_edges,
738 sizeof(*polygon_info->edges));
739 if (polygon_info->edges == (EdgeInfo *) NULL)
740 {
741 (void) ThrowMagickException(exception,GetMagickModule(),
742 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
743 return(DestroyPolygonInfo(polygon_info));
744 }
745 }
746 polygon_info->edges[edge].number_points=(size_t) n;
747 polygon_info->edges[edge].scanline=(-1.0);
748 polygon_info->edges[edge].highwater=0;
749 polygon_info->edges[edge].ghostline=ghostline;
750 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
751 if (direction < 0)
752 ReversePoints(points,(size_t) n);
753 polygon_info->edges[edge].points=points;
754 polygon_info->edges[edge].bounds=bounds;
755 polygon_info->edges[edge].bounds.y1=points[0].y;
756 polygon_info->edges[edge].bounds.y2=points[n-1].y;
757 points=(PointInfo *) NULL;
758 ghostline=MagickFalse;
759 edge++;
760 polygon_info->number_edges=edge;
761 }
762 }
763 polygon_info->number_edges=edge;
764 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(polygon_info->edges,
765 polygon_info->number_edges,sizeof(*polygon_info->edges));
766 if (polygon_info->edges == (EdgeInfo *) NULL)
767 {
768 (void) ThrowMagickException(exception,GetMagickModule(),
769 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
770 return(DestroyPolygonInfo(polygon_info));
771 }
772 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
773 {
774 EdgeInfo
775 *edge_info;
776
777 edge_info=polygon_info->edges+i;
778 edge_info->points=(PointInfo *) ResizeQuantumMemory(edge_info->points,
779 edge_info->number_points,sizeof(*edge_info->points));
780 if (edge_info->points == (PointInfo *) NULL)
781 {
782 (void) ThrowMagickException(exception,GetMagickModule(),
783 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
784 return(DestroyPolygonInfo(polygon_info));
785 }
786 }
787 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
788 sizeof(*polygon_info->edges),DrawCompareEdges);
789 if ((GetLogEventMask() & DrawEvent) != 0)
790 LogPolygonInfo(polygon_info);
791 return(polygon_info);
792}
793
794/*
795%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796% %
797% %
798% %
799+ C o n v e r t P r i m i t i v e T o P a t h %
800% %
801% %
802% %
803%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804%
805% ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
806% path structure.
807%
808% The format of the ConvertPrimitiveToPath method is:
809%
810% PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
811% const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
812%
813% A description of each parameter follows:
814%
815% o ConvertPrimitiveToPath() returns a vector path structure of type
816% PathInfo.
817%
818% o draw_info: a structure of type DrawInfo.
819%
820% o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
821%
822%
823*/
824
825static void LogPathInfo(const PathInfo *path_info)
826{
827 const PathInfo
828 *p;
829
830 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
831 for (p=path_info; p->code != EndCode; p++)
832 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
833 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
834 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
835 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
836 "?");
837 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
838}
839
840static PathInfo *ConvertPrimitiveToPath(
841 const DrawInfo *magick_unused(draw_info),const PrimitiveInfo *primitive_info,
842 ExceptionInfo *exception)
843{
844 MagickBooleanType
845 closed_subpath;
846
847 PathInfo
848 *path_info;
849
850 PathInfoCode
851 code;
852
853 PointInfo
854 p,
855 q;
856
857 ssize_t
858 i,
859 n;
860
861 ssize_t
862 coordinates,
863 start;
864
865 magick_unreferenced(draw_info);
866
867 /*
868 Converts a PrimitiveInfo structure into a vector path structure.
869 */
870 switch (primitive_info->primitive)
871 {
872 case PointPrimitive:
873 case ColorPrimitive:
874 case MattePrimitive:
875 case TextPrimitive:
876 case ImagePrimitive:
877 return((PathInfo *) NULL);
878 default:
879 break;
880 }
881 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
882 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (3UL*i+1UL),
883 sizeof(*path_info));
884 if (path_info == (PathInfo *) NULL)
885 {
886 (void) ThrowMagickException(exception,GetMagickModule(),
887 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
888 return((PathInfo *) NULL);
889 }
890 coordinates=0;
891 closed_subpath=MagickFalse;
892 n=0;
893 p.x=(-1.0);
894 p.y=(-1.0);
895 q.x=(-1.0);
896 q.y=(-1.0);
897 start=0;
898 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
899 {
900 code=LineToCode;
901 if (coordinates <= 0)
902 {
903 /*
904 New subpath.
905 */
906 coordinates=(ssize_t) primitive_info[i].coordinates;
907 p=primitive_info[i].point;
908 start=n;
909 code=MoveToCode;
910 closed_subpath=primitive_info[i].closed_subpath;
911 }
912 coordinates--;
913 if ((code == MoveToCode) || (coordinates <= 0) ||
914 (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
915 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
916 {
917 /*
918 Eliminate duplicate points.
919 */
920 path_info[n].code=code;
921 path_info[n].point=primitive_info[i].point;
922 q=primitive_info[i].point;
923 n++;
924 }
925 if (coordinates > 0)
926 continue; /* next point in current subpath */
927 if (closed_subpath != MagickFalse)
928 {
929 closed_subpath=MagickFalse;
930 continue;
931 }
932 /*
933 Mark the p point as open if the subpath is not closed.
934 */
935 path_info[start].code=OpenCode;
936 path_info[n].code=GhostlineCode;
937 path_info[n].point=primitive_info[i].point;
938 n++;
939 path_info[n].code=LineToCode;
940 path_info[n].point=p;
941 n++;
942 }
943 path_info[n].code=EndCode;
944 path_info[n].point.x=0.0;
945 path_info[n].point.y=0.0;
946 if (IsEventLogging() != MagickFalse)
947 LogPathInfo(path_info);
948 path_info=(PathInfo *) ResizeQuantumMemory(path_info,(size_t) (n+1),
949 sizeof(*path_info));
950 return(path_info);
951}
952
953/*
954%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955% %
956% %
957% %
958% D e s t r o y D r a w I n f o %
959% %
960% %
961% %
962%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963%
964% DestroyDrawInfo() deallocates memory associated with an DrawInfo structure.
965%
966% The format of the DestroyDrawInfo method is:
967%
968% DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
969%
970% A description of each parameter follows:
971%
972% o draw_info: the draw info.
973%
974*/
975MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
976{
977 assert(draw_info != (DrawInfo *) NULL);
978 assert(draw_info->signature == MagickCoreSignature);
979 if (IsEventLogging() != MagickFalse)
980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
981 if (draw_info->id != (char *) NULL)
982 draw_info->id=DestroyString(draw_info->id);
983 if (draw_info->primitive != (char *) NULL)
984 draw_info->primitive=DestroyString(draw_info->primitive);
985 if (draw_info->text != (char *) NULL)
986 draw_info->text=DestroyString(draw_info->text);
987 if (draw_info->geometry != (char *) NULL)
988 draw_info->geometry=DestroyString(draw_info->geometry);
989 if (draw_info->tile != (Image *) NULL)
990 draw_info->tile=DestroyImage(draw_info->tile);
991 if (draw_info->fill_pattern != (Image *) NULL)
992 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
993 if (draw_info->stroke_pattern != (Image *) NULL)
994 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
995 if (draw_info->font != (char *) NULL)
996 draw_info->font=DestroyString(draw_info->font);
997 if (draw_info->metrics != (char *) NULL)
998 draw_info->metrics=DestroyString(draw_info->metrics);
999 if (draw_info->family != (char *) NULL)
1000 draw_info->family=DestroyString(draw_info->family);
1001 if (draw_info->encoding != (char *) NULL)
1002 draw_info->encoding=DestroyString(draw_info->encoding);
1003 if (draw_info->density != (char *) NULL)
1004 draw_info->density=DestroyString(draw_info->density);
1005 if (draw_info->server_name != (char *) NULL)
1006 draw_info->server_name=(char *)
1007 RelinquishMagickMemory(draw_info->server_name);
1008 if (draw_info->dash_pattern != (double *) NULL)
1009 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
1010 draw_info->dash_pattern);
1011 if (draw_info->gradient.stops != (StopInfo *) NULL)
1012 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
1013 draw_info->gradient.stops);
1014 if (draw_info->clip_mask != (char *) NULL)
1015 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
1016 if (draw_info->clipping_mask != (Image *) NULL)
1017 draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
1018 if (draw_info->composite_mask != (Image *) NULL)
1019 draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
1020 if (draw_info->image_info != (ImageInfo *) NULL)
1021 draw_info->image_info=DestroyImageInfo(draw_info->image_info);
1022 draw_info->signature=(~MagickCoreSignature);
1023 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
1024 return(draw_info);
1025}
1026
1027/*
1028%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1029% %
1030% %
1031% %
1032% D r a w A f f i n e I m a g e %
1033% %
1034% %
1035% %
1036%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1037%
1038% DrawAffineImage() composites the source over the destination image as
1039% dictated by the affine transform.
1040%
1041% The format of the DrawAffineImage method is:
1042%
1043% MagickBooleanType DrawAffineImage(Image *image,const Image *source,
1044% const AffineMatrix *affine)
1045%
1046% A description of each parameter follows:
1047%
1048% o image: the image.
1049%
1050% o source: the source image.
1051%
1052% o affine: the affine transform.
1053%
1054*/
1055
1056static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
1057 const double y,const SegmentInfo *edge)
1058{
1059 double
1060 intercept,
1061 z;
1062
1063 double
1064 x;
1065
1066 SegmentInfo
1067 inverse_edge;
1068
1069 /*
1070 Determine left and right edges.
1071 */
1072 inverse_edge.x1=edge->x1;
1073 inverse_edge.y1=edge->y1;
1074 inverse_edge.x2=edge->x2;
1075 inverse_edge.y2=edge->y2;
1076 z=affine->ry*y+affine->tx;
1077 if (affine->sx >= MagickEpsilon)
1078 {
1079 intercept=(-z/affine->sx);
1080 x=intercept;
1081 if (x > inverse_edge.x1)
1082 inverse_edge.x1=x;
1083 intercept=(-z+(double) image->columns)/affine->sx;
1084 x=intercept;
1085 if (x < inverse_edge.x2)
1086 inverse_edge.x2=x;
1087 }
1088 else
1089 if (affine->sx < -MagickEpsilon)
1090 {
1091 intercept=(-z+(double) image->columns)/affine->sx;
1092 x=intercept;
1093 if (x > inverse_edge.x1)
1094 inverse_edge.x1=x;
1095 intercept=(-z/affine->sx);
1096 x=intercept;
1097 if (x < inverse_edge.x2)
1098 inverse_edge.x2=x;
1099 }
1100 else
1101 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1102 {
1103 inverse_edge.x2=edge->x1;
1104 return(inverse_edge);
1105 }
1106 /*
1107 Determine top and bottom edges.
1108 */
1109 z=affine->sy*y+affine->ty;
1110 if (affine->rx >= MagickEpsilon)
1111 {
1112 intercept=(-z/affine->rx);
1113 x=intercept;
1114 if (x > inverse_edge.x1)
1115 inverse_edge.x1=x;
1116 intercept=(-z+(double) image->rows)/affine->rx;
1117 x=intercept;
1118 if (x < inverse_edge.x2)
1119 inverse_edge.x2=x;
1120 }
1121 else
1122 if (affine->rx < -MagickEpsilon)
1123 {
1124 intercept=(-z+(double) image->rows)/affine->rx;
1125 x=intercept;
1126 if (x > inverse_edge.x1)
1127 inverse_edge.x1=x;
1128 intercept=(-z/affine->rx);
1129 x=intercept;
1130 if (x < inverse_edge.x2)
1131 inverse_edge.x2=x;
1132 }
1133 else
1134 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1135 {
1136 inverse_edge.x2=edge->x2;
1137 return(inverse_edge);
1138 }
1139 return(inverse_edge);
1140}
1141
1142static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1143{
1144 AffineMatrix
1145 inverse_affine;
1146
1147 double
1148 determinant;
1149
1150 determinant=MagickSafeReciprocal(affine->sx*affine->sy-affine->rx*
1151 affine->ry);
1152 inverse_affine.sx=determinant*affine->sy;
1153 inverse_affine.rx=determinant*(-affine->rx);
1154 inverse_affine.ry=determinant*(-affine->ry);
1155 inverse_affine.sy=determinant*affine->sx;
1156 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1157 inverse_affine.ry;
1158 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1159 inverse_affine.sy;
1160 return(inverse_affine);
1161}
1162
1163MagickExport MagickBooleanType DrawAffineImage(Image *image,
1164 const Image *source,const AffineMatrix *affine)
1165{
1166 AffineMatrix
1167 inverse_affine;
1168
1169 CacheView
1170 *image_view,
1171 *source_view;
1172
1173 ExceptionInfo
1174 *exception;
1175
1176 MagickBooleanType
1177 status;
1178
1179 MagickPixelPacket
1180 zero;
1181
1182 PointInfo
1183 extent[4],
1184 min,
1185 max,
1186 point;
1187
1188 ssize_t
1189 i;
1190
1191 SegmentInfo
1192 edge;
1193
1194 ssize_t
1195 start,
1196 stop,
1197 y;
1198
1199 /*
1200 Determine bounding box.
1201 */
1202 assert(image != (Image *) NULL);
1203 assert(image->signature == MagickCoreSignature);
1204 assert(source != (const Image *) NULL);
1205 assert(source->signature == MagickCoreSignature);
1206 if (IsEventLogging() != MagickFalse)
1207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1208 assert(affine != (AffineMatrix *) NULL);
1209 extent[0].x=0.0;
1210 extent[0].y=0.0;
1211 extent[1].x=(double) source->columns;
1212 extent[1].y=0.0;
1213 extent[2].x=(double) source->columns;
1214 extent[2].y=(double) source->rows;
1215 extent[3].x=0.0;
1216 extent[3].y=(double) source->rows;
1217 for (i=0; i < 4; i++)
1218 {
1219 point=extent[i];
1220 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1221 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1222 }
1223 min=extent[0];
1224 max=extent[0];
1225 for (i=1; i < 4; i++)
1226 {
1227 if (min.x > extent[i].x)
1228 min.x=extent[i].x;
1229 if (min.y > extent[i].y)
1230 min.y=extent[i].y;
1231 if (max.x < extent[i].x)
1232 max.x=extent[i].x;
1233 if (max.y < extent[i].y)
1234 max.y=extent[i].y;
1235 }
1236 /*
1237 Affine transform image.
1238 */
1239 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
1240 return(MagickFalse);
1241 status=MagickTrue;
1242 edge.x1=min.x;
1243 edge.y1=min.y;
1244 edge.x2=max.x;
1245 edge.y2=max.y;
1246 inverse_affine=InverseAffineMatrix(affine);
1247 if (edge.y1 < 0.0)
1248 edge.y1=0.0;
1249 if (edge.y2 > (image->rows-1.0))
1250 edge.y2=image->rows-1.0;
1251 GetMagickPixelPacket(image,&zero);
1252 exception=(&image->exception);
1253 start=CastDoubleToLong(ceil(edge.y1-0.5));
1254 stop=CastDoubleToLong(floor(edge.y2+0.5));
1255 source_view=AcquireVirtualCacheView(source,exception);
1256 image_view=AcquireAuthenticCacheView(image,exception);
1257#if defined(MAGICKCORE_OPENMP_SUPPORT)
1258 #pragma omp parallel for schedule(static) shared(status) \
1259 magick_number_threads(source,image,stop-start,1)
1260#endif
1261 for (y=start; y <= stop; y++)
1262 {
1263 IndexPacket
1264 *magick_restrict indexes;
1265
1266 MagickPixelPacket
1267 composite,
1268 pixel;
1269
1270 PointInfo
1271 point;
1272
1273 PixelPacket
1274 *magick_restrict q;
1275
1276 SegmentInfo
1277 inverse_edge;
1278
1279 ssize_t
1280 x,
1281 x_offset;
1282
1283 if (status == MagickFalse)
1284 continue;
1285 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1286 if (inverse_edge.x2 < inverse_edge.x1)
1287 continue;
1288 if (inverse_edge.x1 < 0.0)
1289 inverse_edge.x1=0.0;
1290 if (inverse_edge.x2 > image->columns-1.0)
1291 inverse_edge.x2=image->columns-1.0;
1292 q=GetCacheViewAuthenticPixels(image_view,CastDoubleToLong(
1293 ceil(inverse_edge.x1-0.5)),y,(size_t) CastDoubleToLong(floor(
1294 inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),1,exception);
1295 if (q == (PixelPacket *) NULL)
1296 continue;
1297 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1298 pixel=zero;
1299 composite=zero;
1300 x_offset=0;
1301 for (x=CastDoubleToLong(ceil(inverse_edge.x1-0.5));
1302 x <= CastDoubleToLong(floor(inverse_edge.x2+0.5)); x++)
1303 {
1304 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1305 inverse_affine.tx;
1306 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1307 inverse_affine.ty;
1308 status=InterpolateMagickPixelPacket(source,source_view,
1309 UndefinedInterpolatePixel,point.x,point.y,&pixel,exception);
1310 if (status == MagickFalse)
1311 break;
1312 SetMagickPixelPacket(image,q,indexes+x_offset,&composite);
1313 MagickPixelCompositeOver(&pixel,pixel.opacity,&composite,
1314 composite.opacity,&composite);
1315 SetPixelPacket(image,&composite,q,indexes+x_offset);
1316 x_offset++;
1317 q++;
1318 }
1319 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1320 status=MagickFalse;
1321 }
1322 source_view=DestroyCacheView(source_view);
1323 image_view=DestroyCacheView(image_view);
1324 return(status);
1325}
1326
1327/*
1328%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1329% %
1330% %
1331% %
1332+ D r a w B o u n d i n g R e c t a n g l e s %
1333% %
1334% %
1335% %
1336%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1337%
1338% DrawBoundingRectangles() draws the bounding rectangles on the image. This
1339% is only useful for developers debugging the rendering algorithm.
1340%
1341% The format of the DrawBoundingRectangles method is:
1342%
1343% MagickBooleanType DrawBoundingRectangles(Image *image,
1344% const DrawInfo *draw_info,PolygonInfo *polygon_info)
1345%
1346% A description of each parameter follows:
1347%
1348% o image: the image.
1349%
1350% o draw_info: the draw info.
1351%
1352% o polygon_info: Specifies a pointer to a PolygonInfo structure.
1353%
1354*/
1355
1356static MagickBooleanType DrawBoundingRectangles(Image *image,
1357 const DrawInfo *draw_info,const PolygonInfo *polygon_info)
1358{
1359 double
1360 mid;
1361
1362 DrawInfo
1363 *clone_info;
1364
1365 MagickStatusType
1366 status;
1367
1368 PointInfo
1369 end,
1370 resolution,
1371 start;
1372
1373 PrimitiveInfo
1374 primitive_info[6];
1375
1376 ssize_t
1377 i;
1378
1379 SegmentInfo
1380 bounds;
1381
1382 ssize_t
1383 coordinates;
1384
1385 (void) memset(primitive_info,0,sizeof(primitive_info));
1386 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1387 status=QueryColorDatabase("#0000",&clone_info->fill,&image->exception);
1388 if (status == MagickFalse)
1389 {
1390 clone_info=DestroyDrawInfo(clone_info);
1391 return(MagickFalse);
1392 }
1393 resolution.x=96.0;
1394 resolution.y=96.0;
1395 if (clone_info->density != (char *) NULL)
1396 {
1397 GeometryInfo
1398 geometry_info;
1399
1400 MagickStatusType
1401 flags;
1402
1403 flags=ParseGeometry(clone_info->density,&geometry_info);
1404 if ((flags & RhoValue) != 0)
1405 resolution.x=geometry_info.rho;
1406 resolution.y=resolution.x;
1407 if ((flags & SigmaValue) != 0)
1408 resolution.y=geometry_info.sigma;
1409 }
1410 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1411 clone_info->stroke_width/2.0;
1412 bounds.x1=0.0;
1413 bounds.y1=0.0;
1414 bounds.x2=0.0;
1415 bounds.y2=0.0;
1416 if (polygon_info != (PolygonInfo *) NULL)
1417 {
1418 bounds=polygon_info->edges[0].bounds;
1419 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1420 {
1421 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1422 bounds.x1=polygon_info->edges[i].bounds.x1;
1423 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1424 bounds.y1=polygon_info->edges[i].bounds.y1;
1425 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1426 bounds.x2=polygon_info->edges[i].bounds.x2;
1427 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1428 bounds.y2=polygon_info->edges[i].bounds.y2;
1429 }
1430 bounds.x1-=mid;
1431 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1432 image->columns ? (double) image->columns-1 : bounds.x1;
1433 bounds.y1-=mid;
1434 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1435 image->rows ? (double) image->rows-1 : bounds.y1;
1436 bounds.x2+=mid;
1437 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1438 image->columns ? (double) image->columns-1 : bounds.x2;
1439 bounds.y2+=mid;
1440 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1441 image->rows ? (double) image->rows-1 : bounds.y2;
1442 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1443 {
1444 if (polygon_info->edges[i].direction != 0)
1445 status=QueryColorDatabase("#f00",&clone_info->stroke,
1446 &image->exception);
1447 else
1448 status=QueryColorDatabase("#0f0",&clone_info->stroke,
1449 &image->exception);
1450 if (status == MagickFalse)
1451 break;
1452 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1453 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1454 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1455 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1456 primitive_info[0].primitive=RectanglePrimitive;
1457 status&=TraceRectangle(primitive_info,start,end);
1458 primitive_info[0].method=ReplaceMethod;
1459 coordinates=(ssize_t) primitive_info[0].coordinates;
1460 primitive_info[coordinates].primitive=UndefinedPrimitive;
1461 status=DrawPrimitive(image,clone_info,primitive_info);
1462 if (status == MagickFalse)
1463 break;
1464 }
1465 if (i < (ssize_t) polygon_info->number_edges)
1466 {
1467 clone_info=DestroyDrawInfo(clone_info);
1468 return(status == 0 ? MagickFalse : MagickTrue);
1469 }
1470 }
1471 status=QueryColorDatabase("#00f",&clone_info->stroke,&image->exception);
1472 if (status == MagickFalse)
1473 {
1474 clone_info=DestroyDrawInfo(clone_info);
1475 return(MagickFalse);
1476 }
1477 start.x=(double) (bounds.x1-mid);
1478 start.y=(double) (bounds.y1-mid);
1479 end.x=(double) (bounds.x2+mid);
1480 end.y=(double) (bounds.y2+mid);
1481 primitive_info[0].primitive=RectanglePrimitive;
1482 status&=TraceRectangle(primitive_info,start,end);
1483 primitive_info[0].method=ReplaceMethod;
1484 coordinates=(ssize_t) primitive_info[0].coordinates;
1485 primitive_info[coordinates].primitive=UndefinedPrimitive;
1486 status=DrawPrimitive(image,clone_info,primitive_info);
1487 clone_info=DestroyDrawInfo(clone_info);
1488 return(status == 0 ? MagickFalse : MagickTrue);
1489}
1490
1491/*
1492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1493% %
1494% %
1495% %
1496% D r a w C l i p P a t h %
1497% %
1498% %
1499% %
1500%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1501%
1502% DrawClipPath() draws the clip path on the image mask.
1503%
1504% The format of the DrawClipPath method is:
1505%
1506% MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1507% const char *id)
1508%
1509% A description of each parameter follows:
1510%
1511% o image: the image.
1512%
1513% o draw_info: the draw info.
1514%
1515% o id: the clip path id.
1516%
1517*/
1518MagickExport MagickBooleanType DrawClipPath(Image *image,
1519 const DrawInfo *draw_info,const char *id)
1520{
1521 const char
1522 *clip_path;
1523
1524 Image
1525 *clipping_mask;
1526
1527 MagickBooleanType
1528 status;
1529
1530 clip_path=GetImageArtifact(image,id);
1531 if (clip_path == (const char *) NULL)
1532 return(MagickFalse);
1533 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1534 &image->exception);
1535 if (clipping_mask == (Image *) NULL)
1536 return(MagickFalse);
1537 status=SetImageClipMask(image,clipping_mask);
1538 clipping_mask=DestroyImage(clipping_mask);
1539 return(status);
1540}
1541
1542/*
1543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1544% %
1545% %
1546% %
1547% D r a w C l i p p i n g M a s k %
1548% %
1549% %
1550% %
1551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1552%
1553% DrawClippingMask() draws the clip path and returns it as an image clipping
1554% mask.
1555%
1556% The format of the DrawClippingMask method is:
1557%
1558% Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1559% const char *id,const char *clip_path,ExceptionInfo *exception)
1560%
1561% A description of each parameter follows:
1562%
1563% o image: the image.
1564%
1565% o draw_info: the draw info.
1566%
1567% o id: the clip path id.
1568%
1569% o clip_path: the clip path.
1570%
1571% o exception: return any errors or warnings in this structure.
1572%
1573*/
1574static Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1575 const char *id,const char *clip_path,ExceptionInfo *exception)
1576{
1577 DrawInfo
1578 *clone_info;
1579
1580 Image
1581 *clip_mask;
1582
1583 MagickStatusType
1584 status;
1585
1586 /*
1587 Draw a clip path.
1588 */
1589 assert(image != (Image *) NULL);
1590 assert(image->signature == MagickCoreSignature);
1591 assert(draw_info != (const DrawInfo *) NULL);
1592 if (IsEventLogging() != MagickFalse)
1593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1594 clip_mask=AcquireImage((const ImageInfo *) NULL);
1595 status=SetImageExtent(clip_mask,image->columns,image->rows);
1596 if (status == MagickFalse)
1597 return(DestroyImage(clip_mask));
1598 status=SetImageClipMask(image,(Image *) NULL);
1599 status=QueryColorCompliance("#0000",AllCompliance,
1600 &clip_mask->background_color,exception);
1601 clip_mask->background_color.opacity=(Quantum) TransparentOpacity;
1602 status=SetImageBackgroundColor(clip_mask);
1603 if (draw_info->debug != MagickFalse)
1604 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1605 id);
1606 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1607 (void) CloneString(&clone_info->primitive,clip_path);
1608 status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1609 exception);
1610 if (clone_info->clip_mask != (char *) NULL)
1611 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1612 (void) QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1613 exception);
1614 clone_info->stroke_width=0.0;
1615 clone_info->opacity=OpaqueOpacity;
1616 clone_info->clip_path=MagickTrue;
1617 status=RenderMVGContent(clip_mask,clone_info,0);
1618 clone_info=DestroyDrawInfo(clone_info);
1619 status&=SeparateImageChannel(clip_mask,TrueAlphaChannel);
1620 if (draw_info->compliance != SVGCompliance)
1621 status&=NegateImage(clip_mask,MagickFalse);
1622 if (status == MagickFalse)
1623 clip_mask=DestroyImage(clip_mask);
1624 if (draw_info->debug != MagickFalse)
1625 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1626 return(clip_mask);
1627}
1628
1629/*
1630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1631% %
1632% %
1633% %
1634% D r a w C o m p o s i t e M a s k %
1635% %
1636% %
1637% %
1638%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1639%
1640% DrawCompositeMask() draws the mask path and returns it as an image mask.
1641%
1642% The format of the DrawCompositeMask method is:
1643%
1644% Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1645% const char *id,const char *mask_path,ExceptionInfo *exception)
1646%
1647% A description of each parameter follows:
1648%
1649% o image: the image.
1650%
1651% o draw_info: the draw info.
1652%
1653% o id: the mask path id.
1654%
1655% o mask_path: the mask path.
1656%
1657% o exception: return any errors or warnings in this structure.
1658%
1659*/
1660static Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1661 const char *id,const char *mask_path,ExceptionInfo *exception)
1662{
1663 Image
1664 *composite_mask;
1665
1666 DrawInfo
1667 *clone_info;
1668
1669 MagickStatusType
1670 status;
1671
1672 /*
1673 Draw a mask path.
1674 */
1675 assert(image != (Image *) NULL);
1676 assert(image->signature == MagickCoreSignature);
1677 assert(draw_info != (const DrawInfo *) NULL);
1678 if (IsEventLogging() != MagickFalse)
1679 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1680 composite_mask=AcquireImage((const ImageInfo *) NULL);
1681 status=SetImageExtent(composite_mask,image->columns,image->rows);
1682 if (status == MagickFalse)
1683 return(DestroyImage(composite_mask));
1684 status=SetImageMask(image,(Image *) NULL);
1685 status=QueryColorCompliance("#0000",AllCompliance,
1686 &composite_mask->background_color,exception);
1687 composite_mask->background_color.opacity=(Quantum) TransparentOpacity;
1688 (void) SetImageBackgroundColor(composite_mask);
1689 if (draw_info->debug != MagickFalse)
1690 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin mask-path %s",
1691 id);
1692 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1693 (void) CloneString(&clone_info->primitive,mask_path);
1694 status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1695 exception);
1696 status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1697 exception);
1698 clone_info->stroke_width=0.0;
1699 clone_info->opacity=OpaqueOpacity;
1700 status=RenderMVGContent(composite_mask,clone_info,0);
1701 clone_info=DestroyDrawInfo(clone_info);
1702 status&=SeparateImageChannel(composite_mask,TrueAlphaChannel);
1703 status&=NegateImage(composite_mask,MagickFalse);
1704 if (status == MagickFalse)
1705 composite_mask=DestroyImage(composite_mask);
1706 if (draw_info->debug != MagickFalse)
1707 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end mask-path");
1708 return(composite_mask);
1709}
1710
1711/*
1712%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1713% %
1714% %
1715% %
1716+ D r a w D a s h P o l y g o n %
1717% %
1718% %
1719% %
1720%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721%
1722% DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1723% image while respecting the dash offset and dash pattern attributes.
1724%
1725% The format of the DrawDashPolygon method is:
1726%
1727% MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1728% const PrimitiveInfo *primitive_info,Image *image)
1729%
1730% A description of each parameter follows:
1731%
1732% o draw_info: the draw info.
1733%
1734% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1735%
1736% o image: the image.
1737%
1738%
1739*/
1740static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1741 const PrimitiveInfo *primitive_info,Image *image)
1742{
1743 double
1744 dx,
1745 dy,
1746 length,
1747 maximum_length,
1748 offset,
1749 scale,
1750 total_length;
1751
1752 DrawInfo
1753 *clone_info;
1754
1755 MagickStatusType
1756 status;
1757
1758 PrimitiveInfo
1759 *dash_polygon;
1760
1761 ssize_t
1762 i;
1763
1764 size_t
1765 number_vertices;
1766
1767 ssize_t
1768 j,
1769 n;
1770
1771 assert(draw_info != (const DrawInfo *) NULL);
1772 if (draw_info->debug != MagickFalse)
1773 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1774 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1775 number_vertices=(size_t) i;
1776 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1777 (2UL*number_vertices+32UL),sizeof(*dash_polygon));
1778 if (dash_polygon == (PrimitiveInfo *) NULL)
1779 {
1780 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1781 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1782 return(MagickFalse);
1783 }
1784 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1785 sizeof(*dash_polygon));
1786 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1787 clone_info->miterlimit=0;
1788 dash_polygon[0]=primitive_info[0];
1789 dash_polygon[0].closed_subpath=MagickFalse;
1790 scale=ExpandAffine(&draw_info->affine);
1791 length=scale*draw_info->dash_pattern[0];
1792 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1793 scale*draw_info->dash_offset : 0.0;
1794 j=1;
1795 for (n=0; offset > 0.0; j=0)
1796 {
1797 if (draw_info->dash_pattern[n] <= 0.0)
1798 break;
1799 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1800 if (offset > length)
1801 {
1802 offset-=length;
1803 n++;
1804 length=scale*draw_info->dash_pattern[n];
1805 continue;
1806 }
1807 if (offset < length)
1808 {
1809 length-=offset;
1810 offset=0.0;
1811 break;
1812 }
1813 offset=0.0;
1814 n++;
1815 }
1816 status=MagickTrue;
1817 maximum_length=0.0;
1818 total_length=0.0;
1819 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1820 {
1821 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1822 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1823 maximum_length=hypot(dx,dy);
1824 if (maximum_length > (double) (MaxBezierCoordinates >> 2))
1825 continue;
1826 if (fabs(length) < MagickEpsilon)
1827 {
1828 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1829 n++;
1830 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1831 n=0;
1832 length=scale*draw_info->dash_pattern[n];
1833 }
1834 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1835 {
1836 total_length+=length;
1837 if ((n & 0x01) != 0)
1838 {
1839 dash_polygon[0]=primitive_info[0];
1840 dash_polygon[0].closed_subpath=MagickFalse;
1841 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1842 total_length*MagickSafeReciprocal(maximum_length));
1843 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1844 total_length*MagickSafeReciprocal(maximum_length));
1845 j=1;
1846 }
1847 else
1848 {
1849 if ((j+1) > (ssize_t) number_vertices)
1850 break;
1851 dash_polygon[j]=primitive_info[i-1];
1852 dash_polygon[j].closed_subpath=MagickFalse;
1853 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1854 total_length*MagickSafeReciprocal(maximum_length));
1855 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1856 total_length*MagickSafeReciprocal(maximum_length));
1857 dash_polygon[j].coordinates=1;
1858 j++;
1859 dash_polygon[0].coordinates=(size_t) j;
1860 dash_polygon[j].primitive=UndefinedPrimitive;
1861 status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1862 if (status == MagickFalse)
1863 break;
1864 }
1865 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1866 n++;
1867 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1868 n=0;
1869 length=scale*draw_info->dash_pattern[n];
1870 }
1871 length-=(maximum_length-total_length);
1872 if ((n & 0x01) != 0)
1873 continue;
1874 dash_polygon[j]=primitive_info[i];
1875 dash_polygon[j].coordinates=1;
1876 j++;
1877 }
1878 if ((status != MagickFalse) && (total_length < maximum_length) &&
1879 ((n & 0x01) == 0) && (j > 1))
1880 {
1881 dash_polygon[j]=primitive_info[i-1];
1882 dash_polygon[j].closed_subpath=MagickFalse;
1883 dash_polygon[j].point.x+=MagickEpsilon;
1884 dash_polygon[j].point.y+=MagickEpsilon;
1885 dash_polygon[j].coordinates=1;
1886 j++;
1887 dash_polygon[0].coordinates=(size_t) j;
1888 dash_polygon[j].primitive=UndefinedPrimitive;
1889 status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1890 }
1891 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1892 clone_info=DestroyDrawInfo(clone_info);
1893 if (draw_info->debug != MagickFalse)
1894 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1895 return(status != 0 ? MagickTrue : MagickFalse);
1896}
1897
1898/*
1899%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1900% %
1901% %
1902% %
1903% D r a w G r a d i e n t I m a g e %
1904% %
1905% %
1906% %
1907%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1908%
1909% DrawGradientImage() draws a linear gradient on the image.
1910%
1911% The format of the DrawGradientImage method is:
1912%
1913% MagickBooleanType DrawGradientImage(Image *image,
1914% const DrawInfo *draw_info)
1915%
1916% A description of each parameter follows:
1917%
1918% o image: the image.
1919%
1920% o draw_info: the draw info.
1921%
1922*/
1923
1924static inline double GetStopColorOffset(const GradientInfo *gradient,
1925 const ssize_t x,const ssize_t y)
1926{
1927 switch (gradient->type)
1928 {
1929 case UndefinedGradient:
1930 case LinearGradient:
1931 {
1932 double
1933 gamma,
1934 length,
1935 offset,
1936 scale;
1937
1938 PointInfo
1939 p,
1940 q;
1941
1942 const SegmentInfo
1943 *gradient_vector;
1944
1945 gradient_vector=(&gradient->gradient_vector);
1946 p.x=gradient_vector->x2-gradient_vector->x1;
1947 p.y=gradient_vector->y2-gradient_vector->y1;
1948 q.x=(double) x-gradient_vector->x1;
1949 q.y=(double) y-gradient_vector->y1;
1950 length=sqrt(q.x*q.x+q.y*q.y);
1951 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1952 gamma=MagickSafeReciprocal(gamma);
1953 scale=p.x*q.x+p.y*q.y;
1954 offset=gamma*scale*length;
1955 return(offset);
1956 }
1957 case RadialGradient:
1958 {
1959 PointInfo
1960 v;
1961
1962 if (gradient->spread == RepeatSpread)
1963 {
1964 v.x=(double) x-gradient->center.x;
1965 v.y=(double) y-gradient->center.y;
1966 return(sqrt(v.x*v.x+v.y*v.y));
1967 }
1968 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1969 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1970 gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1971 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1972 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1973 gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1974 return(sqrt(v.x*v.x+v.y*v.y));
1975 }
1976 }
1977 return(0.0);
1978}
1979
1980MagickExport MagickBooleanType DrawGradientImage(Image *image,
1981 const DrawInfo *draw_info)
1982{
1983 CacheView
1984 *image_view;
1985
1986 const GradientInfo
1987 *gradient;
1988
1989 const SegmentInfo
1990 *gradient_vector;
1991
1992 double
1993 length;
1994
1995 ExceptionInfo
1996 *exception;
1997
1998 MagickBooleanType
1999 status;
2000
2001 MagickPixelPacket
2002 zero;
2003
2004 PointInfo
2005 point;
2006
2007 RectangleInfo
2008 bounding_box;
2009
2010 ssize_t
2011 height,
2012 y;
2013
2014 /*
2015 Draw linear or radial gradient on image.
2016 */
2017 assert(image != (Image *) NULL);
2018 assert(image->signature == MagickCoreSignature);
2019 assert(draw_info != (const DrawInfo *) NULL);
2020 if (IsEventLogging() != MagickFalse)
2021 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2022 gradient=(&draw_info->gradient);
2023 gradient_vector=(&gradient->gradient_vector);
2024 point.x=gradient_vector->x2-gradient_vector->x1;
2025 point.y=gradient_vector->y2-gradient_vector->y1;
2026 length=sqrt(point.x*point.x+point.y*point.y);
2027 bounding_box=gradient->bounding_box;
2028 status=MagickTrue;
2029 exception=(&image->exception);
2030 GetMagickPixelPacket(image,&zero);
2031 image_view=AcquireAuthenticCacheView(image,exception);
2032 height=(size_t) (bounding_box.y+bounding_box.height);
2033#if defined(MAGICKCORE_OPENMP_SUPPORT)
2034 #pragma omp parallel for schedule(static) shared(status) \
2035 magick_number_threads(image,image,height,1)
2036#endif
2037 for (y=bounding_box.y; y < (ssize_t) height; y++)
2038 {
2039 double
2040 alpha,
2041 offset;
2042
2043 MagickPixelPacket
2044 composite,
2045 pixel;
2046
2047 IndexPacket
2048 *magick_restrict indexes;
2049
2050 PixelPacket
2051 *magick_restrict q;
2052
2053 ssize_t
2054 i,
2055 j,
2056 width,
2057 x;
2058
2059 if (status == MagickFalse)
2060 continue;
2061 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(size_t)
2062 bounding_box.width,1,exception);
2063 if (q == (PixelPacket *) NULL)
2064 {
2065 status=MagickFalse;
2066 continue;
2067 }
2068 indexes=GetCacheViewAuthenticIndexQueue(image_view);
2069 pixel=zero;
2070 composite=zero;
2071 offset=GetStopColorOffset(gradient,0,y);
2072 if (gradient->type != RadialGradient)
2073 offset*=MagickSafeReciprocal(length);
2074 width=(size_t) (bounding_box.x+bounding_box.width);
2075 for (x=bounding_box.x; x < (ssize_t) width; x++)
2076 {
2077 SetMagickPixelPacket(image,q,indexes+x,&pixel);
2078 switch (gradient->spread)
2079 {
2080 case UndefinedSpread:
2081 case PadSpread:
2082 {
2083 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2084 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2085 {
2086 offset=GetStopColorOffset(gradient,x,y);
2087 if (gradient->type != RadialGradient)
2088 offset*=MagickSafeReciprocal(length);
2089 }
2090 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2091 if (offset < gradient->stops[i].offset)
2092 break;
2093 if ((offset < 0.0) || (i == 0))
2094 composite=gradient->stops[0].color;
2095 else
2096 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2097 composite=gradient->stops[gradient->number_stops-1].color;
2098 else
2099 {
2100 j=i;
2101 i--;
2102 alpha=(offset-gradient->stops[i].offset)/
2103 (gradient->stops[j].offset-gradient->stops[i].offset);
2104 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2105 &gradient->stops[j].color,alpha,&composite);
2106 }
2107 break;
2108 }
2109 case ReflectSpread:
2110 {
2111 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2112 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2113 {
2114 offset=GetStopColorOffset(gradient,x,y);
2115 if (gradient->type != RadialGradient)
2116 offset*=MagickSafeReciprocal(length);
2117 }
2118 if (offset < 0.0)
2119 offset=(-offset);
2120 if ((ssize_t) fmod(offset,2.0) == 0)
2121 offset=fmod(offset,1.0);
2122 else
2123 offset=1.0-fmod(offset,1.0);
2124 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2125 if (offset < gradient->stops[i].offset)
2126 break;
2127 if (i == 0)
2128 composite=gradient->stops[0].color;
2129 else
2130 if (i == (ssize_t) gradient->number_stops)
2131 composite=gradient->stops[gradient->number_stops-1].color;
2132 else
2133 {
2134 j=i;
2135 i--;
2136 alpha=(offset-gradient->stops[i].offset)/
2137 (gradient->stops[j].offset-gradient->stops[i].offset);
2138 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2139 &gradient->stops[j].color,alpha,&composite);
2140 }
2141 break;
2142 }
2143 case RepeatSpread:
2144 {
2145 double
2146 repeat;
2147
2148 MagickBooleanType
2149 antialias;
2150
2151 antialias=MagickFalse;
2152 repeat=0.0;
2153 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2154 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2155 {
2156 offset=GetStopColorOffset(gradient,x,y);
2157 if (gradient->type == LinearGradient)
2158 {
2159 repeat=fmod(offset,length);
2160 if (repeat < 0.0)
2161 repeat=length-fmod(-repeat,length);
2162 else
2163 repeat=fmod(offset,length);
2164 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2165 MagickTrue : MagickFalse;
2166 offset=MagickSafeReciprocal(length)*repeat;
2167 }
2168 else
2169 {
2170 repeat=fmod(offset,(double) gradient->radius);
2171 if (repeat < 0.0)
2172 repeat=gradient->radius-fmod(-repeat,
2173 (double) gradient->radius);
2174 else
2175 repeat=fmod(offset,(double) gradient->radius);
2176 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2177 MagickFalse;
2178 offset=repeat*MagickSafeReciprocal(gradient->radius);
2179 }
2180 }
2181 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2182 if (offset < gradient->stops[i].offset)
2183 break;
2184 if (i == 0)
2185 composite=gradient->stops[0].color;
2186 else
2187 if (i == (ssize_t) gradient->number_stops)
2188 composite=gradient->stops[gradient->number_stops-1].color;
2189 else
2190 {
2191 j=i;
2192 i--;
2193 alpha=(offset-gradient->stops[i].offset)/
2194 (gradient->stops[j].offset-gradient->stops[i].offset);
2195 if (antialias != MagickFalse)
2196 {
2197 if (gradient->type == LinearGradient)
2198 alpha=length-repeat;
2199 else
2200 alpha=gradient->radius-repeat;
2201 i=0;
2202 j=(ssize_t) gradient->number_stops-1L;
2203 }
2204 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2205 &gradient->stops[j].color,alpha,&composite);
2206 }
2207 break;
2208 }
2209 }
2210 MagickPixelCompositeOver(&composite,composite.opacity,&pixel,
2211 pixel.opacity,&pixel);
2212 SetPixelPacket(image,&pixel,q,indexes+x);
2213 q++;
2214 }
2215 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2216 status=MagickFalse;
2217 }
2218 image_view=DestroyCacheView(image_view);
2219 return(status);
2220}
2221
2222/*
2223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2224% %
2225% %
2226% %
2227% D r a w I m a g e %
2228% %
2229% %
2230% %
2231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2232%
2233% DrawImage() draws a graphic primitive on your image. The primitive
2234% may be represented as a string or filename. Precede the filename with an
2235% "at" sign (@) and the contents of the file are drawn on the image. You
2236% can affect how text is drawn by setting one or more members of the draw
2237% info structure.
2238%
2239% The format of the DrawImage method is:
2240%
2241% MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info)
2242%
2243% A description of each parameter follows:
2244%
2245% o image: the image.
2246%
2247% o draw_info: the draw info.
2248%
2249*/
2250
2251static inline MagickBooleanType CheckPrimitiveExtent(MVGInfo *mvg_info,
2252 const double pad)
2253{
2254 PrimitiveInfo
2255 *primitive_info;
2256
2257 size_t
2258 extent;
2259
2260 ssize_t
2261 i;
2262
2263 /*
2264 Pad is double, but extent must be element count.
2265 */
2266 extent=(size_t) (mvg_info->offset+pad+PrimitiveExtentPad+1);
2267 if (extent <= *mvg_info->extent)
2268 return(MagickTrue);
2269 if (extent >= GetMaxMemoryRequest())
2270 return(MagickFalse);
2271 /*
2272 Attempt to grow the primitive_info array.
2273 */
2274 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(
2275 *mvg_info->primitive_info,extent,sizeof(PrimitiveInfo));
2276 if (primitive_info == (PrimitiveInfo *) NULL)
2277 {
2278 /*
2279 Allocation failed: reset to minimal safe state
2280 */
2281 ThrowMagickException(mvg_info->exception,GetMagickModule(),
2282 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2283 primitive_info=(PrimitiveInfo *) AcquireCriticalMemory((size_t)
2284 (PrimitiveExtentPad+1)*sizeof(PrimitiveInfo));
2285 (void) memset(primitive_info,0,(size_t) (PrimitiveExtentPad+1)*
2286 sizeof(PrimitiveInfo));
2287 *mvg_info->primitive_info=primitive_info;
2288 *mvg_info->extent=(size_t) (PrimitiveExtentPad+1);
2289 mvg_info->offset=0;
2290 return(MagickFalse);
2291 }
2292 /*
2293 Initialize newly allocated elements.
2294 */
2295 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2296 {
2297 primitive_info[i].primitive=UndefinedPrimitive;
2298 primitive_info[i].text=(char *) NULL;
2299 }
2300 *mvg_info->primitive_info=primitive_info;
2301 *mvg_info->extent=extent;
2302 return(MagickTrue);
2303}
2304
2305static inline double GetDrawValue(const char *magick_restrict string,
2306 char **magick_restrict sentinel)
2307{
2308 char
2309 **magick_restrict q;
2310
2311 double
2312 value;
2313
2314 q=sentinel;
2315 value=InterpretLocaleValue(string,q);
2316 sentinel=q;
2317 return(value);
2318}
2319
2320static int MVGMacroCompare(const void *target,const void *source)
2321{
2322 const char
2323 *p,
2324 *q;
2325
2326 p=(const char *) target;
2327 q=(const char *) source;
2328 return(strcmp(p,q));
2329}
2330
2331static SplayTreeInfo *GetMVGMacros(const char *primitive)
2332{
2333 char
2334 *macro,
2335 *token;
2336
2337 const char
2338 *q;
2339
2340 size_t
2341 extent;
2342
2343 SplayTreeInfo
2344 *macros;
2345
2346 /*
2347 Scan graphic primitives for definitions and classes.
2348 */
2349 if (primitive == (const char *) NULL)
2350 return((SplayTreeInfo *) NULL);
2351 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2352 RelinquishMagickMemory);
2353 macro=AcquireString(primitive);
2354 token=AcquireString(primitive);
2355 extent=strlen(token)+MagickPathExtent;
2356 for (q=primitive; *q != '\0'; )
2357 {
2358 if (GetNextToken(q,&q,extent,token) < 1)
2359 break;
2360 if (*token == '\0')
2361 break;
2362 if (LocaleCompare("push",token) == 0)
2363 {
2364 const char
2365 *end,
2366 *start;
2367
2368 (void) GetNextToken(q,&q,extent,token);
2369 if (*q == '"')
2370 {
2371 char
2372 name[MagickPathExtent];
2373
2374 const char
2375 *p;
2376
2377 ssize_t
2378 n;
2379
2380 /*
2381 Named macro (e.g. push graphic-context "wheel").
2382 */
2383 (void) GetNextToken(q,&q,extent,token);
2384 start=q;
2385 end=q;
2386 (void) CopyMagickString(name,token,MagickPathExtent);
2387 n=1;
2388 for (p=q; *p != '\0'; )
2389 {
2390 if (GetNextToken(p,&p,extent,token) < 1)
2391 break;
2392 if (*token == '\0')
2393 break;
2394 if (LocaleCompare(token,"pop") == 0)
2395 {
2396 end=p-strlen(token)-1;
2397 n--;
2398 }
2399 if (LocaleCompare(token,"push") == 0)
2400 n++;
2401 if ((n == 0) && (end >= start))
2402 {
2403 size_t
2404 length=(size_t) (end-start);
2405
2406 /*
2407 Extract macro.
2408 */
2409 (void) GetNextToken(p,&p,extent,token);
2410 if (length > 0)
2411 {
2412 (void) CopyMagickString(macro,start,length);
2413 (void) AddValueToSplayTree(macros,ConstantString(name),
2414 ConstantString(macro));
2415 }
2416 break;
2417 }
2418 }
2419 }
2420 }
2421 }
2422 token=DestroyString(token);
2423 macro=DestroyString(macro);
2424 return(macros);
2425}
2426
2427static inline MagickBooleanType IsPoint(const char *point)
2428{
2429 char
2430 *p;
2431
2432 double
2433 value;
2434
2435 value=GetDrawValue(point,&p);
2436 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2437 MagickTrue);
2438}
2439
2440static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2441 const PointInfo point)
2442{
2443 primitive_info->coordinates=1;
2444 primitive_info->closed_subpath=MagickFalse;
2445 primitive_info->point=point;
2446 return(MagickTrue);
2447}
2448
2449static MagickBooleanType RenderMVGContent(Image *image,
2450 const DrawInfo *draw_info,const size_t depth)
2451{
2452#define RenderImageTag "Render/Image"
2453
2454 AffineMatrix
2455 affine,
2456 current;
2457
2458 char
2459 key[2*MaxTextExtent],
2460 keyword[MaxTextExtent],
2461 geometry[MaxTextExtent],
2462 name[MaxTextExtent],
2463 *next_token,
2464 pattern[MaxTextExtent],
2465 *primitive,
2466 *token;
2467
2468 const char
2469 *p,
2470 *q;
2471
2472 double
2473 angle,
2474 coordinates,
2475 cursor,
2476 factor,
2477 primitive_extent;
2478
2479 DrawInfo
2480 *clone_info,
2481 **graphic_context;
2482
2483 MagickBooleanType
2484 proceed;
2485
2486 MagickStatusType
2487 status;
2488
2489 MVGInfo
2490 mvg_info;
2491
2492 PointInfo
2493 point;
2494
2495 PixelPacket
2496 start_color;
2497
2498 PrimitiveInfo
2499 *primitive_info;
2500
2501 PrimitiveType
2502 primitive_type;
2503
2504 SegmentInfo
2505 bounds;
2506
2507 size_t
2508 extent,
2509 number_points;
2510
2511 SplayTreeInfo
2512 *macros;
2513
2514 ssize_t
2515 defsDepth,
2516 i,
2517 j,
2518 k,
2519 n,
2520 symbolDepth,
2521 x;
2522
2523 TypeMetric
2524 metrics;
2525
2526 assert(image != (Image *) NULL);
2527 assert(image->signature == MagickCoreSignature);
2528 assert(draw_info != (DrawInfo *) NULL);
2529 assert(draw_info->signature == MagickCoreSignature);
2530 if (IsEventLogging() != MagickFalse)
2531 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2532 if (depth > MagickMaxRecursionDepth)
2533 ThrowBinaryImageException(DrawError,"VectorGraphicsNestedTooDeeply",
2534 image->filename);
2535 if ((draw_info->primitive == (char *) NULL) ||
2536 (*draw_info->primitive == '\0'))
2537 return(MagickFalse);
2538 if (draw_info->debug != MagickFalse)
2539 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
2540 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
2541 return(MagickFalse);
2542 if (image->matte == MagickFalse)
2543 {
2544 status=SetImageAlphaChannel(image,OpaqueAlphaChannel);
2545 if (status == MagickFalse)
2546 return(MagickFalse);
2547 }
2548 primitive=(char *) NULL;
2549 if ((*draw_info->primitive == '@') && (strlen(draw_info->primitive) > 1) &&
2550 (*(draw_info->primitive+1) != '-') && (depth == 0))
2551 primitive=FileToString(draw_info->primitive,~0UL,&image->exception);
2552 else
2553 primitive=AcquireString(draw_info->primitive);
2554 if (primitive == (char *) NULL)
2555 return(MagickFalse);
2556 primitive_extent=(double) strlen(primitive);
2557 (void) SetImageArtifact(image,"mvg:vector-graphics",primitive);
2558 n=0;
2559 /*
2560 Allocate primitive info memory.
2561 */
2562 graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2563 if (graphic_context == (DrawInfo **) NULL)
2564 {
2565 primitive=DestroyString(primitive);
2566 ThrowBinaryImageException(ResourceLimitError,"MemoryAllocationFailed",
2567 image->filename);
2568 }
2569 number_points=(size_t) PrimitiveExtentPad;
2570 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
2571 (number_points+1),sizeof(*primitive_info));
2572 if (primitive_info == (PrimitiveInfo *) NULL)
2573 {
2574 primitive=DestroyString(primitive);
2575 for ( ; n >= 0; n--)
2576 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2577 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2578 ThrowBinaryImageException(ResourceLimitError,"MemoryAllocationFailed",
2579 image->filename);
2580 }
2581 (void) memset(primitive_info,0,(size_t) (number_points+1)*
2582 sizeof(*primitive_info));
2583 (void) memset(&mvg_info,0,sizeof(mvg_info));
2584 mvg_info.primitive_info=(&primitive_info);
2585 mvg_info.extent=(&number_points);
2586 mvg_info.exception=(&image->exception);
2587 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2588 graphic_context[n]->viewbox=image->page;
2589 if ((image->page.width == 0) || (image->page.height == 0))
2590 {
2591 graphic_context[n]->viewbox.width=image->columns;
2592 graphic_context[n]->viewbox.height=image->rows;
2593 }
2594 token=AcquireString(primitive);
2595 extent=strlen(token)+MaxTextExtent;
2596 cursor=0.0;
2597 defsDepth=0;
2598 symbolDepth=0;
2599 macros=GetMVGMacros(primitive);
2600 status=QueryColorDatabase("#000000",&start_color,&image->exception);
2601 for (q=primitive; *q != '\0'; )
2602 {
2603 /*
2604 Interpret graphic primitive.
2605 */
2606 if (GetNextToken(q,&q,MaxTextExtent,keyword) < 1)
2607 break;
2608 if (*keyword == '\0')
2609 break;
2610 if (*keyword == '#')
2611 {
2612 /*
2613 Comment.
2614 */
2615 while ((*q != '\n') && (*q != '\0'))
2616 q++;
2617 continue;
2618 }
2619 p=q-strlen(keyword)-1;
2620 primitive_type=UndefinedPrimitive;
2621 current=graphic_context[n]->affine;
2622 GetAffineMatrix(&affine);
2623 *token='\0';
2624 switch (*keyword)
2625 {
2626 case ';':
2627 break;
2628 case 'a':
2629 case 'A':
2630 {
2631 if (LocaleCompare("affine",keyword) == 0)
2632 {
2633 (void) GetNextToken(q,&q,extent,token);
2634 affine.sx=GetDrawValue(token,&next_token);
2635 if (token == next_token)
2636 ThrowPointExpectedException(image,token);
2637 (void) GetNextToken(q,&q,extent,token);
2638 if (*token == ',')
2639 (void) GetNextToken(q,&q,extent,token);
2640 affine.ry=GetDrawValue(token,&next_token);
2641 if (token == next_token)
2642 ThrowPointExpectedException(image,token);
2643 (void) GetNextToken(q,&q,extent,token);
2644 if (*token == ',')
2645 (void) GetNextToken(q,&q,extent,token);
2646 affine.rx=GetDrawValue(token,&next_token);
2647 if (token == next_token)
2648 ThrowPointExpectedException(image,token);
2649 (void) GetNextToken(q,&q,extent,token);
2650 if (*token == ',')
2651 (void) GetNextToken(q,&q,extent,token);
2652 affine.sy=GetDrawValue(token,&next_token);
2653 if (token == next_token)
2654 ThrowPointExpectedException(image,token);
2655 (void) GetNextToken(q,&q,extent,token);
2656 if (*token == ',')
2657 (void) GetNextToken(q,&q,extent,token);
2658 affine.tx=GetDrawValue(token,&next_token);
2659 if (token == next_token)
2660 ThrowPointExpectedException(image,token);
2661 (void) GetNextToken(q,&q,extent,token);
2662 if (*token == ',')
2663 (void) GetNextToken(q,&q,extent,token);
2664 affine.ty=GetDrawValue(token,&next_token);
2665 if (token == next_token)
2666 ThrowPointExpectedException(image,token);
2667 break;
2668 }
2669 if (LocaleCompare("arc",keyword) == 0)
2670 {
2671 primitive_type=ArcPrimitive;
2672 break;
2673 }
2674 status=MagickFalse;
2675 break;
2676 }
2677 case 'b':
2678 case 'B':
2679 {
2680 if (LocaleCompare("bezier",keyword) == 0)
2681 {
2682 primitive_type=BezierPrimitive;
2683 break;
2684 }
2685 if (LocaleCompare("border-color",keyword) == 0)
2686 {
2687 (void) GetNextToken(q,&q,extent,token);
2688 status&=QueryColorDatabase(token,&graphic_context[n]->border_color,
2689 &image->exception);
2690 break;
2691 }
2692 status=MagickFalse;
2693 break;
2694 }
2695 case 'c':
2696 case 'C':
2697 {
2698 if (LocaleCompare("class",keyword) == 0)
2699 {
2700 const char
2701 *mvg_class;
2702
2703 (void) GetNextToken(q,&q,extent,token);
2704 if ((*token == '\0') || (*token == ';'))
2705 {
2706 status=MagickFalse;
2707 break;
2708 }
2709 /*
2710 Identify recursion.
2711 */
2712 for (i=0; i <= n; i++)
2713 if (LocaleCompare(token,graphic_context[i]->id) == 0)
2714 break;
2715 if (i <= n)
2716 break;
2717 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2718 if ((graphic_context[n]->render != MagickFalse) &&
2719 (mvg_class != (const char *) NULL) && (p > primitive))
2720 {
2721 char
2722 *elements;
2723
2724 ssize_t
2725 offset;
2726
2727 /*
2728 Inject class elements in stream.
2729 */
2730 (void) CloneString(&graphic_context[n]->id,token);
2731 offset=(ssize_t) (p-primitive);
2732 elements=AcquireString(primitive);
2733 elements[offset]='\0';
2734 (void) ConcatenateString(&elements,mvg_class);
2735 (void) ConcatenateString(&elements,"\n");
2736 (void) ConcatenateString(&elements,q);
2737 primitive=DestroyString(primitive);
2738 primitive=elements;
2739 q=primitive+offset;
2740 }
2741 break;
2742 }
2743 if (LocaleCompare("clip-path",keyword) == 0)
2744 {
2745 const char
2746 *clip_path;
2747
2748 /*
2749 Take a node from within the MVG document, and duplicate it here.
2750 */
2751 (void) GetNextToken(q,&q,extent,token);
2752 if (*token == '\0')
2753 {
2754 status=MagickFalse;
2755 break;
2756 }
2757 (void) CloneString(&graphic_context[n]->clip_mask,token);
2758 clip_path=(const char *) GetValueFromSplayTree(macros,token);
2759 if (clip_path != (const char *) NULL)
2760 {
2761 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2762 graphic_context[n]->clipping_mask=
2763 DestroyImage(graphic_context[n]->clipping_mask);
2764 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2765 graphic_context[n],token,clip_path,&image->exception);
2766 if (graphic_context[n]->compliance != SVGCompliance)
2767 {
2768 const char
2769 *clip_path;
2770
2771 clip_path=(const char *) GetValueFromSplayTree(macros,
2772 graphic_context[n]->clip_mask);
2773 if (clip_path != (const char *) NULL)
2774 (void) SetImageArtifact(image,
2775 graphic_context[n]->clip_mask,clip_path);
2776 status&=DrawClipPath(image,graphic_context[n],
2777 graphic_context[n]->clip_mask);
2778 }
2779 }
2780 break;
2781 }
2782 if (LocaleCompare("clip-rule",keyword) == 0)
2783 {
2784 ssize_t
2785 fill_rule;
2786
2787 (void) GetNextToken(q,&q,extent,token);
2788 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2789 token);
2790 if (fill_rule == -1)
2791 {
2792 status=MagickFalse;
2793 break;
2794 }
2795 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2796 break;
2797 }
2798 if (LocaleCompare("clip-units",keyword) == 0)
2799 {
2800 ssize_t
2801 clip_units;
2802
2803 (void) GetNextToken(q,&q,extent,token);
2804 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2805 token);
2806 if (clip_units == -1)
2807 {
2808 status=MagickFalse;
2809 break;
2810 }
2811 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2812 if (clip_units == ObjectBoundingBox)
2813 {
2814 GetAffineMatrix(&current);
2815 affine.sx=draw_info->bounds.x2;
2816 affine.sy=draw_info->bounds.y2;
2817 affine.tx=draw_info->bounds.x1;
2818 affine.ty=draw_info->bounds.y1;
2819 break;
2820 }
2821 break;
2822 }
2823 if (LocaleCompare("circle",keyword) == 0)
2824 {
2825 primitive_type=CirclePrimitive;
2826 break;
2827 }
2828 if (LocaleCompare("color",keyword) == 0)
2829 {
2830 primitive_type=ColorPrimitive;
2831 break;
2832 }
2833 if (LocaleCompare("compliance",keyword) == 0)
2834 {
2835 /*
2836 MVG compliance associates a clipping mask with an image; SVG
2837 compliance associates a clipping mask with a graphics context.
2838 */
2839 (void) GetNextToken(q,&q,extent,token);
2840 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2841 MagickComplianceOptions,MagickFalse,token);
2842 break;
2843 }
2844 if (LocaleCompare("currentColor",keyword) == 0)
2845 {
2846 (void) GetNextToken(q,&q,extent,token);
2847 break;
2848 }
2849 status=MagickFalse;
2850 break;
2851 }
2852 case 'd':
2853 case 'D':
2854 {
2855 if (LocaleCompare("decorate",keyword) == 0)
2856 {
2857 ssize_t
2858 decorate;
2859
2860 (void) GetNextToken(q,&q,extent,token);
2861 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2862 token);
2863 if (decorate == -1)
2864 {
2865 status=MagickFalse;
2866 break;
2867 }
2868 graphic_context[n]->decorate=(DecorationType) decorate;
2869 break;
2870 }
2871 if (LocaleCompare("density",keyword) == 0)
2872 {
2873 (void) GetNextToken(q,&q,extent,token);
2874 (void) CloneString(&graphic_context[n]->density,token);
2875 break;
2876 }
2877 if (LocaleCompare("direction",keyword) == 0)
2878 {
2879 ssize_t
2880 direction;
2881
2882 (void) GetNextToken(q,&q,extent,token);
2883 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2884 token);
2885 if (direction == -1)
2886 status=MagickFalse;
2887 else
2888 graphic_context[n]->direction=(DirectionType) direction;
2889 break;
2890 }
2891 status=MagickFalse;
2892 break;
2893 }
2894 case 'e':
2895 case 'E':
2896 {
2897 if (LocaleCompare("ellipse",keyword) == 0)
2898 {
2899 primitive_type=EllipsePrimitive;
2900 break;
2901 }
2902 if (LocaleCompare("encoding",keyword) == 0)
2903 {
2904 (void) GetNextToken(q,&q,extent,token);
2905 (void) CloneString(&graphic_context[n]->encoding,token);
2906 break;
2907 }
2908 status=MagickFalse;
2909 break;
2910 }
2911 case 'f':
2912 case 'F':
2913 {
2914 if (LocaleCompare("fill",keyword) == 0)
2915 {
2916 const char
2917 *mvg_class;
2918
2919 (void) GetNextToken(q,&q,extent,token);
2920 if (graphic_context[n]->clip_path != MagickFalse)
2921 break;
2922 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2923 if (mvg_class != (const char *) NULL)
2924 {
2925 (void) DrawPatternPath(image,draw_info,mvg_class,
2926 &graphic_context[n]->fill_pattern);
2927 break;
2928 }
2929 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2930 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2931 {
2932 (void) DrawPatternPath(image,draw_info,token,
2933 &graphic_context[n]->fill_pattern);
2934 break;
2935 }
2936 status&=QueryColorDatabase(token,&graphic_context[n]->fill,
2937 &image->exception);
2938 if (graphic_context[n]->fill_opacity != (double) OpaqueOpacity)
2939 graphic_context[n]->fill.opacity=ClampToQuantum(
2940 graphic_context[n]->fill_opacity);
2941 break;
2942 }
2943 if (LocaleCompare("fill-opacity",keyword) == 0)
2944 {
2945 double
2946 opacity;
2947
2948 (void) GetNextToken(q,&q,extent,token);
2949 if (graphic_context[n]->clip_path != MagickFalse)
2950 break;
2951 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2952 opacity=MagickMin(MagickMax(factor*
2953 GetDrawValue(token,&next_token),0.0),1.0);
2954 if (token == next_token)
2955 ThrowPointExpectedException(image,token);
2956 if (graphic_context[n]->compliance == SVGCompliance)
2957 graphic_context[n]->fill_opacity*=(1.0-opacity);
2958 else
2959 graphic_context[n]->fill_opacity=((MagickRealType) QuantumRange-
2960 graphic_context[n]->fill_opacity)*(1.0-opacity);
2961 if (graphic_context[n]->fill.opacity != TransparentOpacity)
2962 graphic_context[n]->fill.opacity=(Quantum)
2963 graphic_context[n]->fill_opacity;
2964 else
2965 graphic_context[n]->fill.opacity=ClampToQuantum((MagickRealType)
2966 QuantumRange*(1.0-opacity));
2967 break;
2968 }
2969 if (LocaleCompare("fill-rule",keyword) == 0)
2970 {
2971 ssize_t
2972 fill_rule;
2973
2974 (void) GetNextToken(q,&q,extent,token);
2975 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2976 token);
2977 if (fill_rule == -1)
2978 {
2979 status=MagickFalse;
2980 break;
2981 }
2982 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2983 break;
2984 }
2985 if (LocaleCompare("font",keyword) == 0)
2986 {
2987 (void) GetNextToken(q,&q,extent,token);
2988 (void) CloneString(&graphic_context[n]->font,token);
2989 if (LocaleCompare("none",token) == 0)
2990 graphic_context[n]->font=(char *) RelinquishMagickMemory(
2991 graphic_context[n]->font);
2992 break;
2993 }
2994 if (LocaleCompare("font-family",keyword) == 0)
2995 {
2996 (void) GetNextToken(q,&q,extent,token);
2997 (void) CloneString(&graphic_context[n]->family,token);
2998 break;
2999 }
3000 if (LocaleCompare("font-size",keyword) == 0)
3001 {
3002 (void) GetNextToken(q,&q,extent,token);
3003 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3004 if (token == next_token)
3005 ThrowPointExpectedException(image,token);
3006 break;
3007 }
3008 if (LocaleCompare("font-stretch",keyword) == 0)
3009 {
3010 ssize_t
3011 stretch;
3012
3013 (void) GetNextToken(q,&q,extent,token);
3014 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3015 if (stretch == -1)
3016 {
3017 status=MagickFalse;
3018 break;
3019 }
3020 graphic_context[n]->stretch=(StretchType) stretch;
3021 break;
3022 }
3023 if (LocaleCompare("font-style",keyword) == 0)
3024 {
3025 ssize_t
3026 style;
3027
3028 (void) GetNextToken(q,&q,extent,token);
3029 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3030 if (style == -1)
3031 {
3032 status=MagickFalse;
3033 break;
3034 }
3035 graphic_context[n]->style=(StyleType) style;
3036 break;
3037 }
3038 if (LocaleCompare("font-weight",keyword) == 0)
3039 {
3040 ssize_t
3041 weight;
3042
3043 (void) GetNextToken(q,&q,extent,token);
3044 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3045 if (weight == -1)
3046 weight=(ssize_t) StringToUnsignedLong(token);
3047 graphic_context[n]->weight=(size_t) weight;
3048 break;
3049 }
3050 status=MagickFalse;
3051 break;
3052 }
3053 case 'g':
3054 case 'G':
3055 {
3056 if (LocaleCompare("gradient-units",keyword) == 0)
3057 {
3058 (void) GetNextToken(q,&q,extent,token);
3059 break;
3060 }
3061 if (LocaleCompare("gravity",keyword) == 0)
3062 {
3063 ssize_t
3064 gravity;
3065
3066 (void) GetNextToken(q,&q,extent,token);
3067 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3068 if (gravity == -1)
3069 {
3070 status=MagickFalse;
3071 break;
3072 }
3073 graphic_context[n]->gravity=(GravityType) gravity;
3074 break;
3075 }
3076 status=MagickFalse;
3077 break;
3078 }
3079 case 'i':
3080 case 'I':
3081 {
3082 if (LocaleCompare("image",keyword) == 0)
3083 {
3084 ssize_t
3085 compose;
3086
3087 primitive_type=ImagePrimitive;
3088 (void) GetNextToken(q,&q,extent,token);
3089 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3090 if (compose == -1)
3091 {
3092 status=MagickFalse;
3093 break;
3094 }
3095 graphic_context[n]->compose=(CompositeOperator) compose;
3096 break;
3097 }
3098 if (LocaleCompare("interline-spacing",keyword) == 0)
3099 {
3100 (void) GetNextToken(q,&q,extent,token);
3101 graphic_context[n]->interline_spacing=GetDrawValue(token,
3102 &next_token);
3103 if (token == next_token)
3104 ThrowPointExpectedException(image,token);
3105 break;
3106 }
3107 if (LocaleCompare("interword-spacing",keyword) == 0)
3108 {
3109 (void) GetNextToken(q,&q,extent,token);
3110 graphic_context[n]->interword_spacing=GetDrawValue(token,
3111 &next_token);
3112 if (token == next_token)
3113 ThrowPointExpectedException(image,token);
3114 break;
3115 }
3116 status=MagickFalse;
3117 break;
3118 }
3119 case 'k':
3120 case 'K':
3121 {
3122 if (LocaleCompare("kerning",keyword) == 0)
3123 {
3124 (void) GetNextToken(q,&q,extent,token);
3125 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3126 if (token == next_token)
3127 ThrowPointExpectedException(image,token);
3128 break;
3129 }
3130 status=MagickFalse;
3131 break;
3132 }
3133 case 'l':
3134 case 'L':
3135 {
3136 if (LocaleCompare("letter-spacing",keyword) == 0)
3137 {
3138 (void) GetNextToken(q,&q,extent,token);
3139 if (IsPoint(token) == MagickFalse)
3140 break;
3141 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3142 clone_info->text=AcquireString(" ");
3143 status&=GetTypeMetrics(image,clone_info,&metrics);
3144 graphic_context[n]->kerning=metrics.width*
3145 GetDrawValue(token,&next_token);
3146 clone_info=DestroyDrawInfo(clone_info);
3147 if (token == next_token)
3148 ThrowPointExpectedException(image,token);
3149 break;
3150 }
3151 if (LocaleCompare("line",keyword) == 0)
3152 {
3153 primitive_type=LinePrimitive;
3154 break;
3155 }
3156 status=MagickFalse;
3157 break;
3158 }
3159 case 'm':
3160 case 'M':
3161 {
3162 if (LocaleCompare("mask",keyword) == 0)
3163 {
3164 const char
3165 *mask_path;
3166
3167 /*
3168 Take a node from within the MVG document, and duplicate it here.
3169 */
3170 (void) GetNextToken(q,&q,extent,token);
3171 mask_path=(const char *) GetValueFromSplayTree(macros,token);
3172 if (mask_path != (const char *) NULL)
3173 {
3174 if (graphic_context[n]->composite_mask != (Image *) NULL)
3175 graphic_context[n]->composite_mask=
3176 DestroyImage(graphic_context[n]->composite_mask);
3177 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3178 graphic_context[n],token,mask_path,&image->exception);
3179 if (graphic_context[n]->compliance != SVGCompliance)
3180 status=SetImageMask(image,graphic_context[n]->composite_mask);
3181 }
3182 break;
3183 }
3184 if (LocaleCompare("matte",keyword) == 0)
3185 {
3186 primitive_type=MattePrimitive;
3187 break;
3188 }
3189 status=MagickFalse;
3190 break;
3191 }
3192 case 'o':
3193 case 'O':
3194 {
3195 if (LocaleCompare("offset",keyword) == 0)
3196 {
3197 (void) GetNextToken(q,&q,extent,token);
3198 break;
3199 }
3200 if (LocaleCompare("opacity",keyword) == 0)
3201 {
3202 double
3203 opacity;
3204
3205 (void) GetNextToken(q,&q,extent,token);
3206 if (graphic_context[n]->clip_path != MagickFalse)
3207 break;
3208 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3209 opacity=1.0-MagickMin(MagickMax(factor*
3210 GetDrawValue(token,&next_token),0.0),1.0);
3211 if (token == next_token)
3212 ThrowPointExpectedException(image,token);
3213 if (graphic_context[n]->compliance == SVGCompliance)
3214 {
3215 graphic_context[n]->fill_opacity*=opacity;
3216 graphic_context[n]->stroke_opacity*=opacity;
3217 }
3218 else
3219 {
3220 graphic_context[n]->fill_opacity=(double) QuantumRange*opacity;
3221 graphic_context[n]->stroke_opacity=(double) QuantumRange*
3222 opacity;
3223 }
3224 if (graphic_context[n]->fill.opacity != (double) TransparentOpacity)
3225 {
3226 graphic_context[n]->fill.opacity=
3227 graphic_context[n]->fill_opacity;
3228 graphic_context[n]->stroke.opacity=
3229 graphic_context[n]->stroke_opacity;
3230 }
3231 else
3232 {
3233 graphic_context[n]->fill.opacity=(MagickRealType)
3234 ClampToQuantum((double) QuantumRange*opacity);
3235 graphic_context[n]->stroke.opacity=(MagickRealType)
3236 ClampToQuantum((double) QuantumRange*opacity);
3237 }
3238 break;
3239 }
3240 status=MagickFalse;
3241 break;
3242 }
3243 case 'p':
3244 case 'P':
3245 {
3246 if (LocaleCompare("path",keyword) == 0)
3247 {
3248 primitive_type=PathPrimitive;
3249 break;
3250 }
3251 if (LocaleCompare("point",keyword) == 0)
3252 {
3253 primitive_type=PointPrimitive;
3254 break;
3255 }
3256 if (LocaleCompare("polyline",keyword) == 0)
3257 {
3258 primitive_type=PolylinePrimitive;
3259 break;
3260 }
3261 if (LocaleCompare("polygon",keyword) == 0)
3262 {
3263 primitive_type=PolygonPrimitive;
3264 break;
3265 }
3266 if (LocaleCompare("pop",keyword) == 0)
3267 {
3268 if (GetNextToken(q,&q,extent,token) < 1)
3269 break;
3270 if (LocaleCompare("class",token) == 0)
3271 break;
3272 if (LocaleCompare("clip-path",token) == 0)
3273 break;
3274 if (LocaleCompare("defs",token) == 0)
3275 {
3276 defsDepth--;
3277 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3278 MagickTrue;
3279 break;
3280 }
3281 if (LocaleCompare("gradient",token) == 0)
3282 break;
3283 if (LocaleCompare("graphic-context",token) == 0)
3284 {
3285 if (n <= 0)
3286 {
3287 (void) ThrowMagickException(&image->exception,
3288 GetMagickModule(),DrawError,
3289 "UnbalancedGraphicContextPushPop","`%s'",token);
3290 status=MagickFalse;
3291 n=0;
3292 break;
3293 }
3294 if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3295 (graphic_context[n]->compliance != SVGCompliance))
3296 if (LocaleCompare(graphic_context[n]->clip_mask,
3297 graphic_context[n-1]->clip_mask) != 0)
3298 status=SetImageClipMask(image,(Image *) NULL);
3299 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3300 n--;
3301 break;
3302 }
3303 if (LocaleCompare("mask",token) == 0)
3304 break;
3305 if (LocaleCompare("pattern",token) == 0)
3306 break;
3307 if (LocaleCompare("symbol",token) == 0)
3308 {
3309 symbolDepth--;
3310 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3311 MagickTrue;
3312 break;
3313 }
3314 status=MagickFalse;
3315 break;
3316 }
3317 if (LocaleCompare("push",keyword) == 0)
3318 {
3319 if (GetNextToken(q,&q,extent,token) < 1)
3320 break;
3321 if (LocaleCompare("class",token) == 0)
3322 {
3323 /*
3324 Class context.
3325 */
3326 for (p=q; *q != '\0'; )
3327 {
3328 if (GetNextToken(q,&q,extent,token) < 1)
3329 break;
3330 if (LocaleCompare(token,"pop") != 0)
3331 continue;
3332 (void) GetNextToken(q,(const char **) NULL,extent,token);
3333 if (LocaleCompare(token,"class") != 0)
3334 continue;
3335 break;
3336 }
3337 (void) GetNextToken(q,&q,extent,token);
3338 break;
3339 }
3340 if (LocaleCompare("clip-path",token) == 0)
3341 {
3342 (void) GetNextToken(q,&q,extent,token);
3343 for (p=q; *q != '\0'; )
3344 {
3345 if (GetNextToken(q,&q,extent,token) < 1)
3346 break;
3347 if (LocaleCompare(token,"pop") != 0)
3348 continue;
3349 (void) GetNextToken(q,(const char **) NULL,extent,token);
3350 if (LocaleCompare(token,"clip-path") != 0)
3351 continue;
3352 break;
3353 }
3354 if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3355 {
3356 status=MagickFalse;
3357 break;
3358 }
3359 (void) GetNextToken(q,&q,extent,token);
3360 break;
3361 }
3362 if (LocaleCompare("defs",token) == 0)
3363 {
3364 defsDepth++;
3365 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3366 MagickTrue;
3367 break;
3368 }
3369 if (LocaleCompare("gradient",token) == 0)
3370 {
3371 char
3372 key[2*MaxTextExtent],
3373 name[MaxTextExtent],
3374 type[MaxTextExtent];
3375
3376 SegmentInfo
3377 segment;
3378
3379 (void) GetNextToken(q,&q,extent,token);
3380 (void) CopyMagickString(name,token,MaxTextExtent);
3381 (void) GetNextToken(q,&q,extent,token);
3382 (void) CopyMagickString(type,token,MaxTextExtent);
3383 (void) GetNextToken(q,&q,extent,token);
3384 segment.x1=GetDrawValue(token,&next_token);
3385 if (token == next_token)
3386 ThrowPointExpectedException(image,token);
3387 (void) GetNextToken(q,&q,extent,token);
3388 if (*token == ',')
3389 (void) GetNextToken(q,&q,extent,token);
3390 segment.y1=GetDrawValue(token,&next_token);
3391 if (token == next_token)
3392 ThrowPointExpectedException(image,token);
3393 (void) GetNextToken(q,&q,extent,token);
3394 if (*token == ',')
3395 (void) GetNextToken(q,&q,extent,token);
3396 segment.x2=GetDrawValue(token,&next_token);
3397 if (token == next_token)
3398 ThrowPointExpectedException(image,token);
3399 (void) GetNextToken(q,&q,extent,token);
3400 if (*token == ',')
3401 (void) GetNextToken(q,&q,extent,token);
3402 segment.y2=GetDrawValue(token,&next_token);
3403 if (token == next_token)
3404 ThrowPointExpectedException(image,token);
3405 if (LocaleCompare(type,"radial") == 0)
3406 {
3407 (void) GetNextToken(q,&q,extent,token);
3408 if (*token == ',')
3409 (void) GetNextToken(q,&q,extent,token);
3410 }
3411 for (p=q; *q != '\0'; )
3412 {
3413 if (GetNextToken(q,&q,extent,token) < 1)
3414 break;
3415 if (LocaleCompare(token,"pop") != 0)
3416 continue;
3417 (void) GetNextToken(q,(const char **) NULL,extent,token);
3418 if (LocaleCompare(token,"gradient") != 0)
3419 continue;
3420 break;
3421 }
3422 if ((q == (char *) NULL) || (*q == '\0') ||
3423 (p == (char *) NULL) || ((q-4) < p) ||
3424 ((q-p+4+1) > MagickPathExtent))
3425 {
3426 status=MagickFalse;
3427 break;
3428 }
3429 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3430 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3431 graphic_context[n]->affine.ry*segment.y1+
3432 graphic_context[n]->affine.tx;
3433 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3434 graphic_context[n]->affine.sy*segment.y1+
3435 graphic_context[n]->affine.ty;
3436 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3437 graphic_context[n]->affine.ry*segment.y2+
3438 graphic_context[n]->affine.tx;
3439 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3440 graphic_context[n]->affine.sy*segment.y2+
3441 graphic_context[n]->affine.ty;
3442 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
3443 (void) SetImageArtifact(image,key,token);
3444 (void) FormatLocaleString(key,MaxTextExtent,"%s-type",name);
3445 (void) SetImageArtifact(image,key,type);
3446 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
3447 (void) FormatLocaleString(geometry,MaxTextExtent,
3448 "%gx%g%+.15g%+.15g",
3449 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3450 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3451 bounds.x1,bounds.y1);
3452 (void) SetImageArtifact(image,key,geometry);
3453 (void) GetNextToken(q,&q,extent,token);
3454 break;
3455 }
3456 if (LocaleCompare("graphic-context",token) == 0)
3457 {
3458 n++;
3459 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3460 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3461 if (graphic_context == (DrawInfo **) NULL)
3462 {
3463 (void) ThrowMagickException(&image->exception,
3464 GetMagickModule(),ResourceLimitError,
3465 "MemoryAllocationFailed","`%s'",image->filename);
3466 status=MagickFalse;
3467 break;
3468 }
3469 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3470 graphic_context[n-1]);
3471 if (*q == '"')
3472 {
3473 (void) GetNextToken(q,&q,extent,token);
3474 (void) CloneString(&graphic_context[n]->id,token);
3475 }
3476 if (n > MagickMaxRecursionDepth)
3477 {
3478 (void) ThrowMagickException(&image->exception,
3479 GetMagickModule(),DrawError,"VectorGraphicsNestedTooDeeply",
3480 "`%s'",image->filename);
3481 status=MagickFalse;
3482 }
3483 break;
3484 }
3485 if (LocaleCompare("mask",token) == 0)
3486 {
3487 (void) GetNextToken(q,&q,extent,token);
3488 break;
3489 }
3490 if (LocaleCompare("pattern",token) == 0)
3491 {
3492 RectangleInfo
3493 bounds;
3494
3495 (void) GetNextToken(q,&q,extent,token);
3496 (void) CopyMagickString(name,token,MaxTextExtent);
3497 (void) GetNextToken(q,&q,extent,token);
3498 bounds.x=CastDoubleToLong(ceil(GetDrawValue(token,
3499 &next_token)-0.5));
3500 if (token == next_token)
3501 ThrowPointExpectedException(image,token);
3502 (void) GetNextToken(q,&q,extent,token);
3503 if (*token == ',')
3504 (void) GetNextToken(q,&q,extent,token);
3505 bounds.y=CastDoubleToLong(ceil(GetDrawValue(token,
3506 &next_token)-0.5));
3507 if (token == next_token)
3508 ThrowPointExpectedException(image,token);
3509 (void) GetNextToken(q,&q,extent,token);
3510 if (*token == ',')
3511 (void) GetNextToken(q,&q,extent,token);
3512 bounds.width=CastDoubleToUnsigned(GetDrawValue(token,
3513 &next_token)+0.5);
3514 if (token == next_token)
3515 ThrowPointExpectedException(image,token);
3516 (void) GetNextToken(q,&q,extent,token);
3517 if (*token == ',')
3518 (void) GetNextToken(q,&q,extent,token);
3519 bounds.height=CastDoubleToUnsigned(GetDrawValue(token,
3520 &next_token)+0.5);
3521 if (token == next_token)
3522 ThrowPointExpectedException(image,token);
3523 for (p=q; *q != '\0'; )
3524 {
3525 if (GetNextToken(q,&q,extent,token) < 1)
3526 break;
3527 if (LocaleCompare(token,"pop") != 0)
3528 continue;
3529 (void) GetNextToken(q,(const char **) NULL,extent,token);
3530 if (LocaleCompare(token,"pattern") != 0)
3531 continue;
3532 break;
3533 }
3534 if ((q == (char *) NULL) || (p == (char *) NULL) ||
3535 ((q-4) < p) || ((size_t) (q-p+4+1) > extent))
3536 {
3537 status=MagickFalse;
3538 break;
3539 }
3540 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3541 (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
3542 (void) SetImageArtifact(image,key,token);
3543 (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
3544 (void) FormatLocaleString(geometry,MaxTextExtent,
3545 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
3546 bounds.height,(double) bounds.x,(double) bounds.y);
3547 (void) SetImageArtifact(image,key,geometry);
3548 (void) GetNextToken(q,&q,extent,token);
3549 break;
3550 }
3551 if (LocaleCompare("symbol",token) == 0)
3552 {
3553 symbolDepth++;
3554 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3555 MagickTrue;
3556 break;
3557 }
3558 status=MagickFalse;
3559 break;
3560 }
3561 status=MagickFalse;
3562 break;
3563 }
3564 case 'r':
3565 case 'R':
3566 {
3567 if (LocaleCompare("rectangle",keyword) == 0)
3568 {
3569 primitive_type=RectanglePrimitive;
3570 break;
3571 }
3572 if (LocaleCompare("rotate",keyword) == 0)
3573 {
3574 (void) GetNextToken(q,&q,extent,token);
3575 angle=GetDrawValue(token,&next_token);
3576 if (token == next_token)
3577 ThrowPointExpectedException(image,token);
3578 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3579 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3580 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3581 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3582 break;
3583 }
3584 if (LocaleCompare("roundRectangle",keyword) == 0)
3585 {
3586 primitive_type=RoundRectanglePrimitive;
3587 break;
3588 }
3589 status=MagickFalse;
3590 break;
3591 }
3592 case 's':
3593 case 'S':
3594 {
3595 if (LocaleCompare("scale",keyword) == 0)
3596 {
3597 (void) GetNextToken(q,&q,extent,token);
3598 affine.sx=GetDrawValue(token,&next_token);
3599 if (token == next_token)
3600 ThrowPointExpectedException(image,token);
3601 (void) GetNextToken(q,&q,extent,token);
3602 if (*token == ',')
3603 (void) GetNextToken(q,&q,extent,token);
3604 affine.sy=GetDrawValue(token,&next_token);
3605 if (token == next_token)
3606 ThrowPointExpectedException(image,token);
3607 break;
3608 }
3609 if (LocaleCompare("skewX",keyword) == 0)
3610 {
3611 (void) GetNextToken(q,&q,extent,token);
3612 angle=GetDrawValue(token,&next_token);
3613 if (token == next_token)
3614 ThrowPointExpectedException(image,token);
3615 affine.ry=sin(DegreesToRadians(angle));
3616 break;
3617 }
3618 if (LocaleCompare("skewY",keyword) == 0)
3619 {
3620 (void) GetNextToken(q,&q,extent,token);
3621 angle=GetDrawValue(token,&next_token);
3622 if (token == next_token)
3623 ThrowPointExpectedException(image,token);
3624 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3625 break;
3626 }
3627 if (LocaleCompare("stop-color",keyword) == 0)
3628 {
3629 GradientType
3630 type;
3631
3632 PixelPacket
3633 stop_color;
3634
3635 (void) GetNextToken(q,&q,extent,token);
3636 status&=QueryColorDatabase(token,&stop_color,&image->exception);
3637 type=LinearGradient;
3638 if (draw_info->gradient.type == RadialGradient)
3639 type=RadialGradient;
3640 (void) GradientImage(image,type,PadSpread,&start_color,&stop_color);
3641 start_color=stop_color;
3642 (void) GetNextToken(q,&q,extent,token);
3643 break;
3644 }
3645 if (LocaleCompare("stroke",keyword) == 0)
3646 {
3647 const char
3648 *mvg_class;
3649
3650 (void) GetNextToken(q,&q,extent,token);
3651 if (graphic_context[n]->clip_path != MagickFalse)
3652 break;
3653 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
3654 if (mvg_class != (const char *) NULL)
3655 {
3656 (void) DrawPatternPath(image,draw_info,mvg_class,
3657 &graphic_context[n]->stroke_pattern);
3658 break;
3659 }
3660 (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
3661 if (GetImageArtifact(image,pattern) != (const char *) NULL)
3662 {
3663 (void) DrawPatternPath(image,draw_info,token,
3664 &graphic_context[n]->stroke_pattern);
3665 break;
3666 }
3667 status&=QueryColorDatabase(token,&graphic_context[n]->stroke,
3668 &image->exception);
3669 if (graphic_context[n]->stroke_opacity != (MagickRealType) OpaqueOpacity)
3670 graphic_context[n]->stroke.opacity=ClampToQuantum(
3671 graphic_context[n]->stroke_opacity);
3672 break;
3673 }
3674 if (LocaleCompare("stroke-antialias",keyword) == 0)
3675 {
3676 (void) GetNextToken(q,&q,extent,token);
3677 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3678 MagickTrue : MagickFalse;
3679 break;
3680 }
3681 if (LocaleCompare("stroke-dasharray",keyword) == 0)
3682 {
3683 if (graphic_context[n]->dash_pattern != (double *) NULL)
3684 graphic_context[n]->dash_pattern=(double *)
3685 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3686 if (IsPoint(q) != MagickFalse)
3687 {
3688 const char
3689 *p;
3690
3691 p=q;
3692 (void) GetNextToken(p,&p,extent,token);
3693 if (*token == ',')
3694 (void) GetNextToken(p,&p,extent,token);
3695 for (x=0; IsPoint(token) != MagickFalse; x++)
3696 {
3697 (void) GetNextToken(p,&p,extent,token);
3698 if (*token == ',')
3699 (void) GetNextToken(p,&p,extent,token);
3700 }
3701 graphic_context[n]->dash_pattern=(double *)
3702 AcquireQuantumMemory((size_t) (2*x+2),
3703 sizeof(*graphic_context[n]->dash_pattern));
3704 if (graphic_context[n]->dash_pattern == (double *) NULL)
3705 {
3706 (void) ThrowMagickException(&image->exception,
3707 GetMagickModule(),ResourceLimitError,
3708 "MemoryAllocationFailed","`%s'",image->filename);
3709 status=MagickFalse;
3710 break;
3711 }
3712 (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3713 (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3714 for (j=0; j < x; j++)
3715 {
3716 (void) GetNextToken(q,&q,extent,token);
3717 if (*token == ',')
3718 (void) GetNextToken(q,&q,extent,token);
3719 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3720 &next_token);
3721 if (token == next_token)
3722 ThrowPointExpectedException(image,token);
3723 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3724 status=MagickFalse;
3725 }
3726 if ((x & 0x01) != 0)
3727 for ( ; j < (2*x); j++)
3728 graphic_context[n]->dash_pattern[j]=
3729 graphic_context[n]->dash_pattern[j-x];
3730 graphic_context[n]->dash_pattern[j]=0.0;
3731 break;
3732 }
3733 (void) GetNextToken(q,&q,extent,token);
3734 break;
3735 }
3736 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3737 {
3738 (void) GetNextToken(q,&q,extent,token);
3739 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3740 if (token == next_token)
3741 ThrowPointExpectedException(image,token);
3742 break;
3743 }
3744 if (LocaleCompare("stroke-linecap",keyword) == 0)
3745 {
3746 ssize_t
3747 linecap;
3748
3749 (void) GetNextToken(q,&q,extent,token);
3750 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3751 if (linecap == -1)
3752 {
3753 status=MagickFalse;
3754 break;
3755 }
3756 graphic_context[n]->linecap=(LineCap) linecap;
3757 break;
3758 }
3759 if (LocaleCompare("stroke-linejoin",keyword) == 0)
3760 {
3761 ssize_t
3762 linejoin;
3763
3764 (void) GetNextToken(q,&q,extent,token);
3765 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3766 token);
3767 if (linejoin == -1)
3768 {
3769 status=MagickFalse;
3770 break;
3771 }
3772 graphic_context[n]->linejoin=(LineJoin) linejoin;
3773 break;
3774 }
3775 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3776 {
3777 (void) GetNextToken(q,&q,extent,token);
3778 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3779 break;
3780 }
3781 if (LocaleCompare("stroke-opacity",keyword) == 0)
3782 {
3783 double
3784 opacity;
3785
3786 (void) GetNextToken(q,&q,extent,token);
3787 if (graphic_context[n]->clip_path != MagickFalse)
3788 break;
3789 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3790 opacity=MagickMin(MagickMax(factor*
3791 GetDrawValue(token,&next_token),0.0),1.0);
3792 if (token == next_token)
3793 ThrowPointExpectedException(image,token);
3794 if (graphic_context[n]->compliance == SVGCompliance)
3795 graphic_context[n]->stroke_opacity*=(1.0-opacity);
3796 else
3797 graphic_context[n]->stroke_opacity=((MagickRealType) QuantumRange-
3798 graphic_context[n]->stroke_opacity)*(1.0-opacity);
3799 if (graphic_context[n]->stroke.opacity != TransparentOpacity)
3800 graphic_context[n]->stroke.opacity=(Quantum)
3801 graphic_context[n]->stroke_opacity;
3802 else
3803 graphic_context[n]->stroke.opacity=ClampToQuantum(
3804 (MagickRealType) QuantumRange*opacity);
3805 break;
3806 }
3807 if (LocaleCompare("stroke-width",keyword) == 0)
3808 {
3809 (void) GetNextToken(q,&q,extent,token);
3810 if (graphic_context[n]->clip_path != MagickFalse)
3811 break;
3812 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3813 if ((token == next_token) ||
3814 (graphic_context[n]->stroke_width < 0.0))
3815 ThrowPointExpectedException(image,token);
3816 break;
3817 }
3818 status=MagickFalse;
3819 break;
3820 }
3821 case 't':
3822 case 'T':
3823 {
3824 if (LocaleCompare("text",keyword) == 0)
3825 {
3826 primitive_type=TextPrimitive;
3827 cursor=0.0;
3828 break;
3829 }
3830 if (LocaleCompare("text-align",keyword) == 0)
3831 {
3832 ssize_t
3833 align;
3834
3835 (void) GetNextToken(q,&q,extent,token);
3836 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3837 if (align == -1)
3838 {
3839 status=MagickFalse;
3840 break;
3841 }
3842 graphic_context[n]->align=(AlignType) align;
3843 break;
3844 }
3845 if (LocaleCompare("text-anchor",keyword) == 0)
3846 {
3847 ssize_t
3848 align;
3849
3850 (void) GetNextToken(q,&q,extent,token);
3851 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3852 if (align == -1)
3853 {
3854 status=MagickFalse;
3855 break;
3856 }
3857 graphic_context[n]->align=(AlignType) align;
3858 break;
3859 }
3860 if (LocaleCompare("text-antialias",keyword) == 0)
3861 {
3862 (void) GetNextToken(q,&q,extent,token);
3863 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3864 MagickTrue : MagickFalse;
3865 break;
3866 }
3867 if (LocaleCompare("text-undercolor",keyword) == 0)
3868 {
3869 (void) GetNextToken(q,&q,extent,token);
3870 status&=QueryColorDatabase(token,&graphic_context[n]->undercolor,
3871 &image->exception);
3872 break;
3873 }
3874 if (LocaleCompare("translate",keyword) == 0)
3875 {
3876 (void) GetNextToken(q,&q,extent,token);
3877 affine.tx=GetDrawValue(token,&next_token);
3878 if (token == next_token)
3879 ThrowPointExpectedException(image,token);
3880 (void) GetNextToken(q,&q,extent,token);
3881 if (*token == ',')
3882 (void) GetNextToken(q,&q,extent,token);
3883 affine.ty=GetDrawValue(token,&next_token);
3884 if (token == next_token)
3885 ThrowPointExpectedException(image,token);
3886 break;
3887 }
3888 status=MagickFalse;
3889 break;
3890 }
3891 case 'u':
3892 case 'U':
3893 {
3894 if (LocaleCompare("use",keyword) == 0)
3895 {
3896 const char
3897 *use;
3898
3899 /*
3900 Get a macro from the MVG document, and "use" it here.
3901 */
3902 (void) GetNextToken(q,&q,extent,token);
3903 use=(const char *) GetValueFromSplayTree(macros,token);
3904 if (use != (const char *) NULL)
3905 {
3906 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3907 (void) CloneString(&clone_info->primitive,use);
3908 status=RenderMVGContent(image,clone_info,depth+1);
3909 clone_info=DestroyDrawInfo(clone_info);
3910 }
3911 break;
3912 }
3913 status=MagickFalse;
3914 break;
3915 }
3916 case 'v':
3917 case 'V':
3918 {
3919 if (LocaleCompare("viewbox",keyword) == 0)
3920 {
3921 (void) GetNextToken(q,&q,extent,token);
3922 graphic_context[n]->viewbox.x=CastDoubleToLong(ceil(
3923 GetDrawValue(token,&next_token)-0.5));
3924 if (token == next_token)
3925 ThrowPointExpectedException(image,token);
3926 (void) GetNextToken(q,&q,extent,token);
3927 if (*token == ',')
3928 (void) GetNextToken(q,&q,extent,token);
3929 graphic_context[n]->viewbox.y=CastDoubleToLong(ceil(
3930 GetDrawValue(token,&next_token)-0.5));
3931 if (token == next_token)
3932 ThrowPointExpectedException(image,token);
3933 (void) GetNextToken(q,&q,extent,token);
3934 if (*token == ',')
3935 (void) GetNextToken(q,&q,extent,token);
3936 graphic_context[n]->viewbox.width=CastDoubleToUnsigned(
3937 GetDrawValue(token,&next_token)+0.5);
3938 if (token == next_token)
3939 ThrowPointExpectedException(image,token);
3940 (void) GetNextToken(q,&q,extent,token);
3941 if (*token == ',')
3942 (void) GetNextToken(q,&q,extent,token);
3943 graphic_context[n]->viewbox.height=CastDoubleToUnsigned(
3944 GetDrawValue(token,&next_token)+0.5);
3945 if (token == next_token)
3946 ThrowPointExpectedException(image,token);
3947 break;
3948 }
3949 status=MagickFalse;
3950 break;
3951 }
3952 case 'w':
3953 case 'W':
3954 {
3955 if (LocaleCompare("word-spacing",keyword) == 0)
3956 {
3957 (void) GetNextToken(q,&q,extent,token);
3958 graphic_context[n]->interword_spacing=GetDrawValue(token,
3959 &next_token);
3960 if (token == next_token)
3961 ThrowPointExpectedException(image,token);
3962 break;
3963 }
3964 status=MagickFalse;
3965 break;
3966 }
3967 default:
3968 {
3969 status=MagickFalse;
3970 break;
3971 }
3972 }
3973 if (status == MagickFalse)
3974 break;
3975 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
3976 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
3977 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
3978 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
3979 {
3980 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
3981 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
3982 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
3983 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
3984 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
3985 current.tx;
3986 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
3987 current.ty;
3988 }
3989 if (primitive_type == UndefinedPrimitive)
3990 {
3991 if ((draw_info->debug != MagickFalse) && (q > p))
3992 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
3993 (q-p-1),p);
3994 continue;
3995 }
3996 /*
3997 Parse the primitive attributes.
3998 */
3999 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4000 if (primitive_info[i].text != (char *) NULL)
4001 primitive_info[i].text=DestroyString(primitive_info[i].text);
4002 i=0;
4003 mvg_info.offset=i;
4004 j=0;
4005 primitive_info[0].point.x=0.0;
4006 primitive_info[0].point.y=0.0;
4007 primitive_info[0].coordinates=0;
4008 primitive_info[0].method=FloodfillMethod;
4009 primitive_info[0].closed_subpath=MagickFalse;
4010 for (x=0; *q != '\0'; x++)
4011 {
4012 /*
4013 Define points.
4014 */
4015 if (IsPoint(q) == MagickFalse)
4016 break;
4017 (void) GetNextToken(q,&q,extent,token);
4018 point.x=GetDrawValue(token,&next_token);
4019 if (token == next_token)
4020 ThrowPointExpectedException(image,token);
4021 (void) GetNextToken(q,&q,extent,token);
4022 if (*token == ',')
4023 (void) GetNextToken(q,&q,extent,token);
4024 point.y=GetDrawValue(token,&next_token);
4025 if (token == next_token)
4026 ThrowPointExpectedException(image,token);
4027 (void) GetNextToken(q,(const char **) NULL,extent,token);
4028 if (*token == ',')
4029 (void) GetNextToken(q,&q,extent,token);
4030 primitive_info[i].primitive=primitive_type;
4031 primitive_info[i].point=point;
4032 primitive_info[i].coordinates=0;
4033 primitive_info[i].method=FloodfillMethod;
4034 primitive_info[i].closed_subpath=MagickFalse;
4035 i++;
4036 mvg_info.offset=i;
4037 if (i < (ssize_t) number_points)
4038 continue;
4039 status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4040 primitive_info=(*mvg_info.primitive_info);
4041 }
4042 if (status == MagickFalse)
4043 break;
4044 if (primitive_info[j].text != (char *) NULL)
4045 primitive_info[j].text=DestroyString(primitive_info[j].text);
4046 primitive_info[j].primitive=primitive_type;
4047 primitive_info[j].coordinates=(size_t) x;
4048 primitive_info[j].method=FloodfillMethod;
4049 primitive_info[j].closed_subpath=MagickFalse;
4050 /*
4051 Circumscribe primitive within a circle.
4052 */
4053 bounds.x1=primitive_info[j].point.x;
4054 bounds.y1=primitive_info[j].point.y;
4055 bounds.x2=primitive_info[j].point.x;
4056 bounds.y2=primitive_info[j].point.y;
4057 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4058 {
4059 point=primitive_info[j+k].point;
4060 if (point.x < bounds.x1)
4061 bounds.x1=point.x;
4062 if (point.y < bounds.y1)
4063 bounds.y1=point.y;
4064 if (point.x > bounds.x2)
4065 bounds.x2=point.x;
4066 if (point.y > bounds.y2)
4067 bounds.y2=point.y;
4068 }
4069 /*
4070 Speculate how many points our primitive might consume.
4071 */
4072 coordinates=(double) primitive_info[j].coordinates;
4073 switch (primitive_type)
4074 {
4075 case RectanglePrimitive:
4076 {
4077 coordinates*=5.0;
4078 break;
4079 }
4080 case RoundRectanglePrimitive:
4081 {
4082 double
4083 alpha,
4084 beta,
4085 radius;
4086
4087 alpha=bounds.x2-bounds.x1;
4088 beta=bounds.y2-bounds.y1;
4089 radius=hypot(alpha,beta);
4090 coordinates*=5.0;
4091 coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4092 BezierQuantum+360.0;
4093 break;
4094 }
4095 case BezierPrimitive:
4096 {
4097 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4098 break;
4099 }
4100 case PathPrimitive:
4101 {
4102 char
4103 *s,
4104 *t;
4105
4106 (void) GetNextToken(q,&q,extent,token);
4107 coordinates=1.0;
4108 t=token;
4109 for (s=token; *s != '\0'; s=t)
4110 {
4111 double
4112 value;
4113
4114 value=GetDrawValue(s,&t);
4115 (void) value;
4116 if (s == t)
4117 {
4118 t++;
4119 continue;
4120 }
4121 coordinates++;
4122 }
4123 for (s=token; *s != '\0'; s++)
4124 if (strspn(s,"AaCcQqSsTt") != 0)
4125 coordinates+=(20.0*BezierQuantum)+360.0;
4126 break;
4127 }
4128 default:
4129 break;
4130 }
4131 if (status == MagickFalse)
4132 break;
4133 if (((size_t) (i+coordinates)) >= number_points)
4134 {
4135 /*
4136 Resize based on speculative points required by primitive.
4137 */
4138 number_points+=coordinates+1;
4139 if (number_points < (size_t) coordinates)
4140 {
4141 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4142 ResourceLimitError,"MemoryAllocationFailed","`%s'",
4143 image->filename);
4144 status=MagickFalse;
4145 break;
4146 }
4147 mvg_info.offset=i;
4148 status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4149 primitive_info=(*mvg_info.primitive_info);
4150 }
4151 status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4152 primitive_info=(*mvg_info.primitive_info);
4153 if (status == MagickFalse)
4154 break;
4155 mvg_info.offset=j;
4156 switch (primitive_type)
4157 {
4158 case PointPrimitive:
4159 default:
4160 {
4161 if (primitive_info[j].coordinates != 1)
4162 {
4163 status=MagickFalse;
4164 break;
4165 }
4166 status&=TracePoint(primitive_info+j,primitive_info[j].point);
4167 primitive_info=(*mvg_info.primitive_info);
4168 i=(ssize_t) (j+primitive_info[j].coordinates);
4169 break;
4170 }
4171 case LinePrimitive:
4172 {
4173 if (primitive_info[j].coordinates != 2)
4174 {
4175 status=MagickFalse;
4176 break;
4177 }
4178 status&=TraceLine(primitive_info+j,primitive_info[j].point,
4179 primitive_info[j+1].point);
4180 primitive_info=(*mvg_info.primitive_info);
4181 i=(ssize_t) (j+primitive_info[j].coordinates);
4182 break;
4183 }
4184 case RectanglePrimitive:
4185 {
4186 if (primitive_info[j].coordinates != 2)
4187 {
4188 status=MagickFalse;
4189 break;
4190 }
4191 status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4192 primitive_info[j+1].point);
4193 primitive_info=(*mvg_info.primitive_info);
4194 i=(ssize_t) (j+primitive_info[j].coordinates);
4195 break;
4196 }
4197 case RoundRectanglePrimitive:
4198 {
4199 if (primitive_info[j].coordinates != 3)
4200 {
4201 status=MagickFalse;
4202 break;
4203 }
4204 if ((primitive_info[j+2].point.x < 0.0) ||
4205 (primitive_info[j+2].point.y < 0.0))
4206 {
4207 status=MagickFalse;
4208 break;
4209 }
4210 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4211 {
4212 status=MagickFalse;
4213 break;
4214 }
4215 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4216 {
4217 status=MagickFalse;
4218 break;
4219 }
4220 status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4221 primitive_info[j+1].point,primitive_info[j+2].point);
4222 primitive_info=(*mvg_info.primitive_info);
4223 i=(ssize_t) (j+primitive_info[j].coordinates);
4224 break;
4225 }
4226 case ArcPrimitive:
4227 {
4228 if (primitive_info[j].coordinates != 3)
4229 {
4230 status=MagickFalse;
4231 break;
4232 }
4233 status&=TraceArc(&mvg_info,primitive_info[j].point,
4234 primitive_info[j+1].point,primitive_info[j+2].point);
4235 primitive_info=(*mvg_info.primitive_info);
4236 i=(ssize_t) (j+primitive_info[j].coordinates);
4237 break;
4238 }
4239 case EllipsePrimitive:
4240 {
4241 if (primitive_info[j].coordinates != 3)
4242 {
4243 status=MagickFalse;
4244 break;
4245 }
4246 if ((primitive_info[j+1].point.x < 0.0) ||
4247 (primitive_info[j+1].point.y < 0.0))
4248 {
4249 status=MagickFalse;
4250 break;
4251 }
4252 status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4253 primitive_info[j+1].point,primitive_info[j+2].point);
4254 primitive_info=(*mvg_info.primitive_info);
4255 i=(ssize_t) (j+primitive_info[j].coordinates);
4256 break;
4257 }
4258 case CirclePrimitive:
4259 {
4260 if (primitive_info[j].coordinates != 2)
4261 {
4262 status=MagickFalse;
4263 break;
4264 }
4265 status&=TraceCircle(&mvg_info,primitive_info[j].point,
4266 primitive_info[j+1].point);
4267 primitive_info=(*mvg_info.primitive_info);
4268 i=(ssize_t) (j+primitive_info[j].coordinates);
4269 break;
4270 }
4271 case PolylinePrimitive:
4272 {
4273 if (primitive_info[j].coordinates < 1)
4274 {
4275 status=MagickFalse;
4276 break;
4277 }
4278 break;
4279 }
4280 case PolygonPrimitive:
4281 {
4282 if (primitive_info[j].coordinates < 3)
4283 {
4284 status=MagickFalse;
4285 break;
4286 }
4287 primitive_info[i]=primitive_info[j];
4288 primitive_info[i].coordinates=0;
4289 primitive_info[j].coordinates++;
4290 primitive_info[j].closed_subpath=MagickTrue;
4291 i++;
4292 break;
4293 }
4294 case BezierPrimitive:
4295 {
4296 if (primitive_info[j].coordinates < 3)
4297 {
4298 status=MagickFalse;
4299 break;
4300 }
4301 status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4302 primitive_info=(*mvg_info.primitive_info);
4303 i=(ssize_t) (j+primitive_info[j].coordinates);
4304 break;
4305 }
4306 case PathPrimitive:
4307 {
4308 coordinates=(double) TracePath(image,&mvg_info,token);
4309 primitive_info=(*mvg_info.primitive_info);
4310 if (coordinates < 0.0)
4311 {
4312 status=MagickFalse;
4313 break;
4314 }
4315 i=(ssize_t) (j+coordinates);
4316 break;
4317 }
4318 case ColorPrimitive:
4319 case MattePrimitive:
4320 {
4321 ssize_t
4322 method;
4323
4324 if (primitive_info[j].coordinates != 1)
4325 {
4326 status=MagickFalse;
4327 break;
4328 }
4329 (void) GetNextToken(q,&q,extent,token);
4330 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4331 if (method == -1)
4332 {
4333 status=MagickFalse;
4334 break;
4335 }
4336 primitive_info[j].method=(PaintMethod) method;
4337 break;
4338 }
4339 case TextPrimitive:
4340 {
4341 char
4342 geometry[MagickPathExtent];
4343
4344 if (primitive_info[j].coordinates != 1)
4345 {
4346 status=MagickFalse;
4347 break;
4348 }
4349 if (*token != ',')
4350 (void) GetNextToken(q,&q,extent,token);
4351 (void) CloneString(&primitive_info[j].text,token);
4352 /*
4353 Compute text cursor offset.
4354 */
4355 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4356 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4357 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4358 {
4359 mvg_info.point=primitive_info->point;
4360 primitive_info->point.x+=cursor;
4361 }
4362 else
4363 {
4364 mvg_info.point=primitive_info->point;
4365 cursor=0.0;
4366 }
4367 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4368 primitive_info->point.x,primitive_info->point.y);
4369 clone_info->render=MagickFalse;
4370 clone_info->text=AcquireString(token);
4371 status&=GetTypeMetrics(image,clone_info,&metrics);
4372 clone_info=DestroyDrawInfo(clone_info);
4373 cursor+=metrics.width;
4374 if (graphic_context[n]->compliance != SVGCompliance)
4375 cursor=0.0;
4376 break;
4377 }
4378 case ImagePrimitive:
4379 {
4380 if (primitive_info[j].coordinates != 2)
4381 {
4382 status=MagickFalse;
4383 break;
4384 }
4385 (void) GetNextToken(q,&q,extent,token);
4386 (void) CloneString(&primitive_info[j].text,token);
4387 break;
4388 }
4389 }
4390 mvg_info.offset=i;
4391 if (status == 0)
4392 break;
4393 primitive_info[i].primitive=UndefinedPrimitive;
4394 if ((draw_info->debug != MagickFalse) && (q > p))
4395 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
4396 /*
4397 Sanity check.
4398 */
4399 status&=CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4400 &graphic_context[n]->affine));
4401 primitive_info=(*mvg_info.primitive_info);
4402 if (status == 0)
4403 break;
4404 status&=CheckPrimitiveExtent(&mvg_info,(double)
4405 graphic_context[n]->stroke_width);
4406 primitive_info=(*mvg_info.primitive_info);
4407 if (status == 0)
4408 break;
4409 if (i == 0)
4410 continue;
4411 /*
4412 Transform points.
4413 */
4414 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4415 {
4416 point=primitive_info[i].point;
4417 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4418 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4419 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4420 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4421 point=primitive_info[i].point;
4422 if (point.x < graphic_context[n]->bounds.x1)
4423 graphic_context[n]->bounds.x1=point.x;
4424 if (point.y < graphic_context[n]->bounds.y1)
4425 graphic_context[n]->bounds.y1=point.y;
4426 if (point.x > graphic_context[n]->bounds.x2)
4427 graphic_context[n]->bounds.x2=point.x;
4428 if (point.y > graphic_context[n]->bounds.y2)
4429 graphic_context[n]->bounds.y2=point.y;
4430 if (primitive_info[i].primitive == ImagePrimitive)
4431 break;
4432 if (i >= (ssize_t) number_points)
4433 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4434 }
4435 if (graphic_context[n]->render != MagickFalse)
4436 {
4437 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4438 (graphic_context[n]->clip_mask != (char *) NULL) &&
4439 (LocaleCompare(graphic_context[n]->clip_mask,
4440 graphic_context[n-1]->clip_mask) != 0))
4441 {
4442 const char
4443 *clip_path;
4444
4445 clip_path=(const char *) GetValueFromSplayTree(macros,
4446 graphic_context[n]->clip_mask);
4447 if (clip_path != (const char *) NULL)
4448 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4449 clip_path);
4450 status&=DrawClipPath(image,graphic_context[n],
4451 graphic_context[n]->clip_mask);
4452 }
4453 status&=DrawPrimitive(image,graphic_context[n],primitive_info);
4454 }
4455 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4456 primitive_extent);
4457 if (proceed == MagickFalse)
4458 break;
4459 if (status == 0)
4460 break;
4461 }
4462 if (draw_info->debug != MagickFalse)
4463 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4464 /*
4465 Relinquish resources.
4466 */
4467 macros=DestroySplayTree(macros);
4468 token=DestroyString(token);
4469 if (primitive_info != (PrimitiveInfo *) NULL)
4470 {
4471 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4472 if (primitive_info[i].text != (char *) NULL)
4473 primitive_info[i].text=DestroyString(primitive_info[i].text);
4474 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4475 }
4476 primitive=DestroyString(primitive);
4477 for ( ; n >= 0; n--)
4478 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4479 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4480 if (status == MagickFalse)
4481 ThrowBinaryImageException(DrawError,
4482 "NonconformingDrawingPrimitiveDefinition",keyword);
4483 return(status != 0 ? MagickTrue : MagickFalse);
4484}
4485
4486MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info)
4487{
4488 return(RenderMVGContent(image,draw_info,0));
4489}
4490
4491/*
4492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4493% %
4494% %
4495% %
4496% D r a w P a t t e r n P a t h %
4497% %
4498% %
4499% %
4500%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4501%
4502% DrawPatternPath() draws a pattern.
4503%
4504% The format of the DrawPatternPath method is:
4505%
4506% MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4507% const char *name,Image **pattern)
4508%
4509% A description of each parameter follows:
4510%
4511% o image: the image.
4512%
4513% o draw_info: the draw info.
4514%
4515% o name: the pattern name.
4516%
4517% o image: the image.
4518%
4519*/
4520MagickExport MagickBooleanType DrawPatternPath(Image *image,
4521 const DrawInfo *draw_info,const char *name,Image **pattern)
4522{
4523 char
4524 property[MaxTextExtent];
4525
4526 const char
4527 *geometry,
4528 *path,
4529 *type;
4530
4531 DrawInfo
4532 *clone_info;
4533
4534 ImageInfo
4535 *image_info;
4536
4537 MagickBooleanType
4538 status;
4539
4540 assert(image != (Image *) NULL);
4541 assert(image->signature == MagickCoreSignature);
4542 assert(draw_info != (const DrawInfo *) NULL);
4543 if (IsEventLogging() != MagickFalse)
4544 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4545 assert(name != (const char *) NULL);
4546 (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
4547 path=GetImageArtifact(image,property);
4548 if (path == (const char *) NULL)
4549 return(MagickFalse);
4550 (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
4551 geometry=GetImageArtifact(image,property);
4552 if (geometry == (const char *) NULL)
4553 return(MagickFalse);
4554 if ((*pattern) != (Image *) NULL)
4555 *pattern=DestroyImage(*pattern);
4556 image_info=AcquireImageInfo();
4557 image_info->size=AcquireString(geometry);
4558 *pattern=AcquireImage(image_info);
4559 image_info=DestroyImageInfo(image_info);
4560 (void) QueryColorDatabase("#00000000",&(*pattern)->background_color,
4561 &image->exception);
4562 (void) SetImageBackgroundColor(*pattern);
4563 if (draw_info->debug != MagickFalse)
4564 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4565 "begin pattern-path %s %s",name,geometry);
4566 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4567 if (clone_info->fill_pattern != (Image *) NULL)
4568 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4569 if (clone_info->stroke_pattern != (Image *) NULL)
4570 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4571 (void) FormatLocaleString(property,MaxTextExtent,"%s-type",name);
4572 type=GetImageArtifact(image,property);
4573 if (type != (const char *) NULL)
4574 clone_info->gradient.type=(GradientType) ParseCommandOption(
4575 MagickGradientOptions,MagickFalse,type);
4576 (void) CloneString(&clone_info->primitive,path);
4577 status=RenderMVGContent(*pattern,clone_info,0);
4578 clone_info=DestroyDrawInfo(clone_info);
4579 if (draw_info->debug != MagickFalse)
4580 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4581 return(status);
4582}
4583
4584/*
4585%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4586% %
4587% %
4588% %
4589+ D r a w P o l y g o n P r i m i t i v e %
4590% %
4591% %
4592% %
4593%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4594%
4595% DrawPolygonPrimitive() draws a polygon on the image.
4596%
4597% The format of the DrawPolygonPrimitive method is:
4598%
4599% MagickBooleanType DrawPolygonPrimitive(Image *image,
4600% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4601%
4602% A description of each parameter follows:
4603%
4604% o image: the image.
4605%
4606% o draw_info: the draw info.
4607%
4608% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4609%
4610*/
4611
4612static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4613{
4614 ssize_t
4615 i;
4616
4617 assert(polygon_info != (PolygonInfo **) NULL);
4618 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4619 if (polygon_info[i] != (PolygonInfo *) NULL)
4620 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4621 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4622 return(polygon_info);
4623}
4624
4625static PolygonInfo **AcquirePolygonTLS(const DrawInfo *draw_info,
4626 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4627{
4628 PathInfo
4629 *magick_restrict path_info;
4630
4631 PolygonInfo
4632 **polygon_info;
4633
4634 size_t
4635 number_threads;
4636
4637 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4638 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4639 sizeof(*polygon_info));
4640 if (polygon_info == (PolygonInfo **) NULL)
4641 {
4642 (void) ThrowMagickException(exception,GetMagickModule(),
4643 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4644 return((PolygonInfo **) NULL);
4645 }
4646 (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4647 path_info=ConvertPrimitiveToPath(draw_info,primitive_info,exception);
4648 if (path_info == (PathInfo *) NULL)
4649 return(DestroyPolygonTLS(polygon_info));
4650 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4651 if (polygon_info[0] == (PolygonInfo *) NULL)
4652 {
4653 (void) ThrowMagickException(exception,GetMagickModule(),
4654 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4655 return(DestroyPolygonTLS(polygon_info));
4656 }
4657 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4658 return(polygon_info);
4659}
4660
4661static MagickBooleanType AcquirePolygonEdgesTLS(PolygonInfo **polygon_info,
4662 const size_t number_threads,ExceptionInfo *exception)
4663{
4664 ssize_t
4665 i;
4666
4667 for (i=1; i < (ssize_t) number_threads; i++)
4668 {
4669 EdgeInfo
4670 *edge_info;
4671
4672 ssize_t
4673 j;
4674
4675 polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4676 sizeof(*polygon_info[i]));
4677 if (polygon_info[i] == (PolygonInfo *) NULL)
4678 {
4679 (void) ThrowMagickException(exception,GetMagickModule(),
4680 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4681 return(MagickFalse);
4682 }
4683 polygon_info[i]->number_edges=0;
4684 edge_info=polygon_info[0]->edges;
4685 polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4686 polygon_info[0]->number_edges,sizeof(*edge_info));
4687 if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4688 {
4689 (void) ThrowMagickException(exception,GetMagickModule(),
4690 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4691 return(MagickFalse);
4692 }
4693 (void) memcpy(polygon_info[i]->edges,edge_info,
4694 polygon_info[0]->number_edges*sizeof(*edge_info));
4695 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4696 polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4697 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4698 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4699 {
4700 edge_info=polygon_info[0]->edges+j;
4701 polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4702 edge_info->number_points,sizeof(*edge_info));
4703 if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4704 {
4705 (void) ThrowMagickException(exception,GetMagickModule(),
4706 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4707 return(MagickFalse);
4708 }
4709 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4710 edge_info->number_points*sizeof(*edge_info->points));
4711 }
4712 }
4713 return(MagickTrue);
4714}
4715
4716static size_t DestroyEdge(PolygonInfo *polygon_info,const ssize_t edge)
4717{
4718 assert(edge < (ssize_t) polygon_info->number_edges);
4719 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4720 polygon_info->edges[edge].points);
4721 polygon_info->number_edges--;
4722 if (edge < (ssize_t) polygon_info->number_edges)
4723 (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4724 (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
4725 return(polygon_info->number_edges);
4726}
4727
4728static double GetOpacityPixel(PolygonInfo *polygon_info,const double mid,
4729 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4730 const ssize_t y,double *stroke_opacity)
4731{
4732 double
4733 alpha,
4734 beta,
4735 distance,
4736 subpath_opacity;
4737
4738 PointInfo
4739 delta;
4740
4741 EdgeInfo
4742 *p;
4743
4744 const PointInfo
4745 *q;
4746
4747 ssize_t
4748 i;
4749
4750 ssize_t
4751 j,
4752 winding_number;
4753
4754 /*
4755 Compute fill & stroke opacity for this (x,y) point.
4756 */
4757 *stroke_opacity=0.0;
4758 subpath_opacity=0.0;
4759 p=polygon_info->edges;
4760 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4761 {
4762 if ((double) y <= (p->bounds.y1-mid-0.5))
4763 break;
4764 if ((double) y > (p->bounds.y2+mid+0.5))
4765 {
4766 p--;
4767 (void) DestroyEdge(polygon_info,j--);
4768 continue;
4769 }
4770 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
4771 ((double) x > (p->bounds.x2+mid+0.5)))
4772 continue;
4773 i=(ssize_t) MagickMax((double) p->highwater,1.0);
4774 for ( ; i < (ssize_t) p->number_points; i++)
4775 {
4776 if ((double) y <= (p->points[i-1].y-mid-0.5))
4777 break;
4778 if ((double) y > (p->points[i].y+mid+0.5))
4779 continue;
4780 if (p->scanline != (double) y)
4781 {
4782 p->scanline=(double) y;
4783 p->highwater=(size_t) i;
4784 }
4785 /*
4786 Compute distance between a point and an edge.
4787 */
4788 q=p->points+i-1;
4789 delta.x=(q+1)->x-q->x;
4790 delta.y=(q+1)->y-q->y;
4791 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4792 if (beta <= 0.0)
4793 {
4794 delta.x=(double) x-q->x;
4795 delta.y=(double) y-q->y;
4796 distance=delta.x*delta.x+delta.y*delta.y;
4797 }
4798 else
4799 {
4800 alpha=delta.x*delta.x+delta.y*delta.y;
4801 if (beta >= alpha)
4802 {
4803 delta.x=(double) x-(q+1)->x;
4804 delta.y=(double) y-(q+1)->y;
4805 distance=delta.x*delta.x+delta.y*delta.y;
4806 }
4807 else
4808 {
4809 alpha=MagickSafeReciprocal(alpha);
4810 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4811 distance=alpha*beta*beta;
4812 }
4813 }
4814 /*
4815 Compute stroke & subpath opacity.
4816 */
4817 beta=0.0;
4818 if (p->ghostline == MagickFalse)
4819 {
4820 alpha=mid+0.5;
4821 if ((*stroke_opacity < 1.0) &&
4822 (distance <= ((alpha+0.25)*(alpha+0.25))))
4823 {
4824 alpha=mid-0.5;
4825 if (distance <= ((alpha+0.25)*(alpha+0.25)))
4826 *stroke_opacity=1.0;
4827 else
4828 {
4829 beta=1.0;
4830 if (fabs(distance-1.0) >= MagickEpsilon)
4831 beta=sqrt((double) distance);
4832 alpha=beta-mid-0.5;
4833 if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
4834 *stroke_opacity=(alpha-0.25)*(alpha-0.25);
4835 }
4836 }
4837 }
4838 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
4839 continue;
4840 if (distance <= 0.0)
4841 {
4842 subpath_opacity=1.0;
4843 continue;
4844 }
4845 if (distance > 1.0)
4846 continue;
4847 if (fabs(beta) < MagickEpsilon)
4848 {
4849 beta=1.0;
4850 if (fabs(distance-1.0) >= MagickEpsilon)
4851 beta=sqrt(distance);
4852 }
4853 alpha=beta-1.0;
4854 if (subpath_opacity < (alpha*alpha))
4855 subpath_opacity=alpha*alpha;
4856 }
4857 }
4858 /*
4859 Compute fill opacity.
4860 */
4861 if (fill == MagickFalse)
4862 return(0.0);
4863 if (subpath_opacity >= 1.0)
4864 return(1.0);
4865 /*
4866 Determine winding number.
4867 */
4868 winding_number=0;
4869 p=polygon_info->edges;
4870 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4871 {
4872 if ((double) y <= p->bounds.y1)
4873 break;
4874 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
4875 continue;
4876 if ((double) x > p->bounds.x2)
4877 {
4878 winding_number+=p->direction != 0 ? 1 : -1;
4879 continue;
4880 }
4881 i=(ssize_t) MagickMax((double) p->highwater,1.0);
4882 for ( ; i < (ssize_t) (p->number_points-1); i++)
4883 if ((double) y <= p->points[i].y)
4884 break;
4885 q=p->points+i-1;
4886 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4887 winding_number+=p->direction != 0 ? 1 : -1;
4888 }
4889 if (fill_rule != NonZeroRule)
4890 {
4891 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4892 return(1.0);
4893 }
4894 else
4895 if (MagickAbsoluteValue(winding_number) != 0)
4896 return(1.0);
4897 return(subpath_opacity);
4898}
4899
4900static MagickBooleanType DrawPolygonPrimitive(Image *image,
4901 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4902{
4903 typedef struct _ExtentInfo
4904 {
4905 ssize_t
4906 x1,
4907 y1,
4908 x2,
4909 y2;
4910 } ExtentInfo;
4911
4912 CacheView
4913 *image_view;
4914
4915 const char
4916 *artifact;
4917
4918 double
4919 mid;
4920
4921 ExceptionInfo
4922 *exception;
4923
4924 ExtentInfo
4925 poly_extent;
4926
4927 MagickBooleanType
4928 fill,
4929 status;
4930
4931 PolygonInfo
4932 **magick_restrict polygon_info;
4933
4934 EdgeInfo
4935 *p;
4936
4937 SegmentInfo
4938 bounds;
4939
4940 size_t
4941 number_threads = 1;
4942
4943 ssize_t
4944 i,
4945 y;
4946
4947 assert(image != (Image *) NULL);
4948 assert(image->signature == MagickCoreSignature);
4949 assert(draw_info != (DrawInfo *) NULL);
4950 assert(draw_info->signature == MagickCoreSignature);
4951 assert(primitive_info != (PrimitiveInfo *) NULL);
4952 if (IsEventLogging() != MagickFalse)
4953 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4954 if (primitive_info->coordinates <= 1)
4955 return(MagickTrue);
4956 /*
4957 Compute bounding box.
4958 */
4959 polygon_info=AcquirePolygonTLS(draw_info,primitive_info,&image->exception);
4960 if (polygon_info == (PolygonInfo **) NULL)
4961 return(MagickFalse);
4962 if (draw_info->debug != MagickFalse)
4963 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4964 fill=(primitive_info->method == FillToBorderMethod) ||
4965 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4966 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4967 bounds=polygon_info[0]->edges[0].bounds;
4968 artifact=GetImageArtifact(image,"draw:render-bounding-rectangles");
4969 if (IsStringTrue(artifact) != MagickFalse)
4970 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0]);
4971 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4972 {
4973 p=polygon_info[0]->edges+i;
4974 if (p->bounds.x1 < bounds.x1)
4975 bounds.x1=p->bounds.x1;
4976 if (p->bounds.y1 < bounds.y1)
4977 bounds.y1=p->bounds.y1;
4978 if (p->bounds.x2 > bounds.x2)
4979 bounds.x2=p->bounds.x2;
4980 if (p->bounds.y2 > bounds.y2)
4981 bounds.y2=p->bounds.y2;
4982 }
4983 bounds.x1-=(mid+1.0);
4984 bounds.y1-=(mid+1.0);
4985 bounds.x2+=(mid+1.0);
4986 bounds.y2+=(mid+1.0);
4987 if ((bounds.x1 >= (double) image->columns) ||
4988 (bounds.y1 >= (double) image->rows) ||
4989 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
4990 {
4991 polygon_info=DestroyPolygonTLS(polygon_info);
4992 return(MagickTrue); /* virtual polygon */
4993 }
4994 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
4995 (double) image->columns-1.0 : bounds.x1;
4996 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
4997 (double) image->rows-1.0 : bounds.y1;
4998 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
4999 (double) image->columns-1.0 : bounds.x2;
5000 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5001 (double) image->rows-1.0 : bounds.y2;
5002 poly_extent.x1=CastDoubleToLong(ceil(bounds.x1-0.5));
5003 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5004 poly_extent.x2=CastDoubleToLong(floor(bounds.x2+0.5));
5005 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5006 number_threads=GetMagickNumberThreads(image,image,poly_extent.y2-
5007 poly_extent.y1+1,1);
5008 status=AcquirePolygonEdgesTLS(polygon_info,number_threads,&image->exception);
5009 if (status == MagickFalse)
5010 {
5011 polygon_info=DestroyPolygonTLS(polygon_info);
5012 return(status);
5013 }
5014 status=MagickTrue;
5015 exception=(&image->exception);
5016 image_view=AcquireAuthenticCacheView(image,exception);
5017 if ((primitive_info->coordinates == 1) ||
5018 (polygon_info[0]->number_edges == 0))
5019 {
5020 /*
5021 Draw point.
5022 */
5023#if defined(MAGICKCORE_OPENMP_SUPPORT)
5024 #pragma omp parallel for schedule(static) shared(status) \
5025 num_threads(number_threads)
5026#endif
5027 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5028 {
5029 MagickBooleanType
5030 sync;
5031
5032 PixelPacket
5033 *magick_restrict q;
5034
5035 ssize_t
5036 x;
5037
5038 if (status == MagickFalse)
5039 continue;
5040 x=poly_extent.x1;
5041 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (poly_extent.x2-
5042 x+1),1,exception);
5043 if (q == (PixelPacket *) NULL)
5044 {
5045 status=MagickFalse;
5046 continue;
5047 }
5048 for ( ; x <= poly_extent.x2; x++)
5049 {
5050 if ((x == CastDoubleToLong(ceil(primitive_info->point.x-0.5))) &&
5051 (y == CastDoubleToLong(ceil(primitive_info->point.y-0.5))))
5052 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,q);
5053 q++;
5054 }
5055 sync=SyncCacheViewAuthenticPixels(image_view,exception);
5056 if (sync == MagickFalse)
5057 status=MagickFalse;
5058 }
5059 image_view=DestroyCacheView(image_view);
5060 polygon_info=DestroyPolygonTLS(polygon_info);
5061 if (draw_info->debug != MagickFalse)
5062 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5063 " end draw-polygon");
5064 return(status);
5065 }
5066 /*
5067 Draw polygon or line.
5068 */
5069 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5070 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5071#if defined(MAGICKCORE_OPENMP_SUPPORT)
5072 #pragma omp parallel for schedule(static) shared(status) \
5073 num_threads(number_threads)
5074#endif
5075 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5076 {
5077 const int
5078 id = GetOpenMPThreadId();
5079
5080 PixelPacket
5081 fill_color,
5082 stroke_color;
5083
5084 PixelPacket
5085 *magick_restrict q;
5086
5087 ssize_t
5088 x;
5089
5090 if (status == MagickFalse)
5091 continue;
5092 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(size_t)
5093 (poly_extent.x2-poly_extent.x1+1),1,exception);
5094 if (q == (PixelPacket *) NULL)
5095 {
5096 status=MagickFalse;
5097 continue;
5098 }
5099 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5100 {
5101 double
5102 fill_opacity,
5103 stroke_opacity;
5104
5105 /*
5106 Fill and/or stroke.
5107 */
5108 fill_opacity=GetOpacityPixel(polygon_info[id],mid,fill,
5109 draw_info->fill_rule,x,y,&stroke_opacity);
5110 if (draw_info->stroke_antialias == MagickFalse)
5111 {
5112 fill_opacity=fill_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5113 stroke_opacity=stroke_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5114 }
5115 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5116 &fill_color);
5117 fill_opacity=(double) ((MagickRealType) QuantumRange-fill_opacity*
5118 ((MagickRealType) QuantumRange-(MagickRealType) fill_color.opacity));
5119 MagickCompositeOver(&fill_color,(MagickRealType) fill_opacity,q,
5120 (MagickRealType) q->opacity,q);
5121 (void) GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5122 &stroke_color);
5123 stroke_opacity=(double) ((MagickRealType) QuantumRange-stroke_opacity*
5124 ((MagickRealType) QuantumRange-(MagickRealType) stroke_color.opacity));
5125 MagickCompositeOver(&stroke_color,(MagickRealType) stroke_opacity,q,
5126 (MagickRealType) q->opacity,q);
5127 q++;
5128 }
5129 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5130 status=MagickFalse;
5131 }
5132 image_view=DestroyCacheView(image_view);
5133 polygon_info=DestroyPolygonTLS(polygon_info);
5134 if (draw_info->debug != MagickFalse)
5135 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
5136 return(status);
5137}
5138
5139/*
5140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5141% %
5142% %
5143% %
5144% D r a w P r i m i t i v e %
5145% %
5146% %
5147% %
5148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5149%
5150% DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
5151%
5152% The format of the DrawPrimitive method is:
5153%
5154% MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
5155% PrimitiveInfo *primitive_info)
5156%
5157% A description of each parameter follows:
5158%
5159% o image: the image.
5160%
5161% o draw_info: the draw info.
5162%
5163% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5164%
5165*/
5166static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
5167{
5168 const char
5169 *methods[] =
5170 {
5171 "point",
5172 "replace",
5173 "floodfill",
5174 "filltoborder",
5175 "reset",
5176 "?"
5177 };
5178
5179 PointInfo
5180 p,
5181 q,
5182 point;
5183
5184 ssize_t
5185 i,
5186 x;
5187
5188 ssize_t
5189 coordinates,
5190 y;
5191
5192 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5193 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5194 switch (primitive_info->primitive)
5195 {
5196 case PointPrimitive:
5197 {
5198 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5199 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
5200 methods[primitive_info->method]);
5201 return;
5202 }
5203 case ColorPrimitive:
5204 {
5205 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5206 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
5207 methods[primitive_info->method]);
5208 return;
5209 }
5210 case MattePrimitive:
5211 {
5212 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5213 "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
5214 methods[primitive_info->method]);
5215 return;
5216 }
5217 case TextPrimitive:
5218 {
5219 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5220 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
5221 return;
5222 }
5223 case ImagePrimitive:
5224 {
5225 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5226 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
5227 return;
5228 }
5229 default:
5230 break;
5231 }
5232 coordinates=0;
5233 p=primitive_info[0].point;
5234 q.x=(-1.0);
5235 q.y=(-1.0);
5236 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5237 {
5238 point=primitive_info[i].point;
5239 if (coordinates <= 0)
5240 {
5241 coordinates=(ssize_t) primitive_info[i].coordinates;
5242 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5243 " begin open (%.20g)",(double) coordinates);
5244 p=point;
5245 }
5246 point=primitive_info[i].point;
5247 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5248 (fabs(q.y-point.y) >= MagickEpsilon))
5249 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5250 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5251 else
5252 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5253 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5254 q=point;
5255 coordinates--;
5256 if (coordinates > 0)
5257 continue;
5258 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5259 (fabs(p.y-point.y) >= MagickEpsilon))
5260 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
5261 (double) coordinates);
5262 else
5263 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
5264 (double) coordinates);
5265 }
5266}
5267
5268MagickExport MagickBooleanType DrawPrimitive(Image *image,
5269 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5270{
5271 CacheView
5272 *image_view;
5273
5274 ExceptionInfo
5275 *exception;
5276
5277 MagickStatusType
5278 status;
5279
5280 ssize_t
5281 i,
5282 x;
5283
5284 ssize_t
5285 y;
5286
5287 if (draw_info->debug != MagickFalse)
5288 {
5289 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5290 " begin draw-primitive");
5291 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5292 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5293 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5294 draw_info->affine.tx,draw_info->affine.ty);
5295 }
5296 exception=(&image->exception);
5297 status=MagickTrue;
5298 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5299 ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
5300 (IsPixelGray(&draw_info->stroke) == MagickFalse)))
5301 status=SetImageColorspace(image,sRGBColorspace);
5302 if (draw_info->compliance == SVGCompliance)
5303 {
5304 status&=SetImageClipMask(image,draw_info->clipping_mask);
5305 status&=SetImageMask(image,draw_info->composite_mask);
5306 }
5307 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5308 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5309 image_view=AcquireAuthenticCacheView(image,exception);
5310 switch (primitive_info->primitive)
5311 {
5312 case ColorPrimitive:
5313 {
5314 switch (primitive_info->method)
5315 {
5316 case PointMethod:
5317 default:
5318 {
5319 PixelPacket
5320 *q;
5321
5322 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5323 if (q == (PixelPacket *) NULL)
5324 break;
5325 (void) GetFillColor(draw_info,x,y,q);
5326 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5327 break;
5328 }
5329 case ReplaceMethod:
5330 {
5331 PixelPacket
5332 target;
5333
5334 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5335 for (y=0; y < (ssize_t) image->rows; y++)
5336 {
5337 PixelPacket
5338 *magick_restrict q;
5339
5340 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5341 exception);
5342 if (q == (PixelPacket *) NULL)
5343 break;
5344 for (x=0; x < (ssize_t) image->columns; x++)
5345 {
5346 if (IsColorSimilar(image,q,&target) == MagickFalse)
5347 {
5348 q++;
5349 continue;
5350 }
5351 (void) GetFillColor(draw_info,x,y,q);
5352 q++;
5353 }
5354 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5355 if (status == MagickFalse)
5356 break;
5357 }
5358 break;
5359 }
5360 case FloodfillMethod:
5361 case FillToBorderMethod:
5362 {
5363 MagickPixelPacket
5364 target;
5365
5366 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5367 if (primitive_info->method == FillToBorderMethod)
5368 {
5369 target.red=(MagickRealType) draw_info->border_color.red;
5370 target.green=(MagickRealType) draw_info->border_color.green;
5371 target.blue=(MagickRealType) draw_info->border_color.blue;
5372 }
5373 status&=FloodfillPaintImage(image,DefaultChannels,draw_info,&target,x,
5374 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5375 MagickTrue);
5376 break;
5377 }
5378 case ResetMethod:
5379 {
5380 for (y=0; y < (ssize_t) image->rows; y++)
5381 {
5382 PixelPacket
5383 *magick_restrict q;
5384
5385 ssize_t
5386 x;
5387
5388 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5389 exception);
5390 if (q == (PixelPacket *) NULL)
5391 break;
5392 for (x=0; x < (ssize_t) image->columns; x++)
5393 {
5394 (void) GetFillColor(draw_info,x,y,q);
5395 q++;
5396 }
5397 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5398 if (status == MagickFalse)
5399 break;
5400 }
5401 break;
5402 }
5403 }
5404 break;
5405 }
5406 case MattePrimitive:
5407 {
5408 if (image->matte == MagickFalse)
5409 status&=SetImageAlphaChannel(image,OpaqueAlphaChannel);
5410 switch (primitive_info->method)
5411 {
5412 case PointMethod:
5413 default:
5414 {
5415 PixelPacket
5416 pixel;
5417
5418 PixelPacket
5419 *q;
5420
5421 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5422 if (q == (PixelPacket *) NULL)
5423 break;
5424 (void) GetFillColor(draw_info,x,y,&pixel);
5425 SetPixelOpacity(q,pixel.opacity);
5426 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5427 break;
5428 }
5429 case ReplaceMethod:
5430 {
5431 PixelPacket
5432 pixel,
5433 target;
5434
5435 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5436 for (y=0; y < (ssize_t) image->rows; y++)
5437 {
5438 PixelPacket
5439 *magick_restrict q;
5440
5441 ssize_t
5442 x;
5443
5444 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5445 exception);
5446 if (q == (PixelPacket *) NULL)
5447 break;
5448 for (x=0; x < (ssize_t) image->columns; x++)
5449 {
5450 if (IsColorSimilar(image,q,&target) == MagickFalse)
5451 {
5452 q++;
5453 continue;
5454 }
5455 (void) GetFillColor(draw_info,x,y,&pixel);
5456 SetPixelOpacity(q,pixel.opacity);
5457 q++;
5458 }
5459 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5460 if (status == MagickFalse)
5461 break;
5462 }
5463 break;
5464 }
5465 case FloodfillMethod:
5466 case FillToBorderMethod:
5467 {
5468 MagickPixelPacket
5469 target;
5470
5471 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5472 if (primitive_info->method == FillToBorderMethod)
5473 {
5474 target.red=(MagickRealType) draw_info->border_color.red;
5475 target.green=(MagickRealType) draw_info->border_color.green;
5476 target.blue=(MagickRealType) draw_info->border_color.blue;
5477 }
5478 status&=FloodfillPaintImage(image,OpacityChannel,draw_info,&target,x,
5479 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5480 MagickTrue);
5481 break;
5482 }
5483 case ResetMethod:
5484 {
5485 PixelPacket
5486 pixel;
5487
5488 for (y=0; y < (ssize_t) image->rows; y++)
5489 {
5490 PixelPacket
5491 *magick_restrict q;
5492
5493 ssize_t
5494 x;
5495
5496 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5497 exception);
5498 if (q == (PixelPacket *) NULL)
5499 break;
5500 for (x=0; x < (ssize_t) image->columns; x++)
5501 {
5502 (void) GetFillColor(draw_info,x,y,&pixel);
5503 SetPixelOpacity(q,pixel.opacity);
5504 q++;
5505 }
5506 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5507 if (status == MagickFalse)
5508 break;
5509 }
5510 break;
5511 }
5512 }
5513 break;
5514 }
5515 case ImagePrimitive:
5516 {
5517 AffineMatrix
5518 affine;
5519
5520 char
5521 composite_geometry[MaxTextExtent];
5522
5523 Image
5524 *composite_image,
5525 *composite_images;
5526
5527 ImageInfo
5528 *clone_info;
5529
5530 RectangleInfo
5531 geometry;
5532
5533 ssize_t
5534 x1,
5535 y1;
5536
5537 if (primitive_info->text == (char *) NULL)
5538 break;
5539 clone_info=AcquireImageInfo();
5540 composite_images=(Image *) NULL;
5541 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5542 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5543 &image->exception);
5544 else
5545 if (*primitive_info->text != '\0')
5546 {
5547 /*
5548 Read composite image.
5549 */
5550 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5551 MagickPathExtent);
5552 (void) SetImageInfo(clone_info,1,exception);
5553 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5554 MagickPathExtent);
5555 if (clone_info->size != (char *) NULL)
5556 clone_info->size=DestroyString(clone_info->size);
5557 if (clone_info->extract != (char *) NULL)
5558 clone_info->extract=DestroyString(clone_info->extract);
5559 if ((LocaleCompare(clone_info->magick,"ftp") != 0) &&
5560 (LocaleCompare(clone_info->magick,"http") != 0) &&
5561 (LocaleCompare(clone_info->magick,"https") != 0) &&
5562 (LocaleCompare(clone_info->magick,"mvg") != 0) &&
5563 (LocaleCompare(clone_info->magick,"vid") != 0))
5564 composite_images=ReadImage(clone_info,exception);
5565 else
5566 (void) ThrowMagickException(exception,GetMagickModule(),
5567 FileOpenError,"UnableToOpenFile","`%s'",clone_info->filename);
5568 }
5569 clone_info=DestroyImageInfo(clone_info);
5570 if (composite_images == (Image *) NULL)
5571 {
5572 status=0;
5573 break;
5574 }
5575 composite_image=RemoveFirstImageFromList(&composite_images);
5576 composite_images=DestroyImageList(composite_images);
5577 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5578 NULL,(void *) NULL);
5579 x1=CastDoubleToLong(ceil(primitive_info[1].point.x-0.5));
5580 y1=CastDoubleToLong(ceil(primitive_info[1].point.y-0.5));
5581 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5582 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5583 {
5584 char
5585 geometry[MaxTextExtent];
5586
5587 /*
5588 Resize image.
5589 */
5590 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
5591 primitive_info[1].point.x,primitive_info[1].point.y);
5592 composite_image->filter=image->filter;
5593 status&=TransformImage(&composite_image,(char *) NULL,geometry);
5594 }
5595 if (composite_image->matte == MagickFalse)
5596 status&=SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
5597 if (draw_info->opacity != OpaqueOpacity)
5598 status&=SetImageOpacity(composite_image,draw_info->opacity);
5599 SetGeometry(image,&geometry);
5600 image->gravity=draw_info->gravity;
5601 geometry.x=x;
5602 geometry.y=y;
5603 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
5604 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
5605 composite_image->rows,(double) geometry.x,(double) geometry.y);
5606 (void) ParseGravityGeometry(image,composite_geometry,&geometry,
5607 &image->exception);
5608 affine=draw_info->affine;
5609 affine.tx=(double) geometry.x;
5610 affine.ty=(double) geometry.y;
5611 composite_image->interpolate=image->interpolate;
5612 if ((draw_info->compose == OverCompositeOp) ||
5613 (draw_info->compose == SrcOverCompositeOp))
5614 status&=DrawAffineImage(image,composite_image,&affine);
5615 else
5616 status&=CompositeImage(image,draw_info->compose,composite_image,
5617 geometry.x,geometry.y);
5618 composite_image=DestroyImage(composite_image);
5619 break;
5620 }
5621 case PointPrimitive:
5622 {
5623 PixelPacket
5624 fill_color;
5625
5626 PixelPacket
5627 *q;
5628
5629 if ((y < 0) || (y >= (ssize_t) image->rows))
5630 break;
5631 if ((x < 0) || (x >= (ssize_t) image->columns))
5632 break;
5633 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5634 if (q == (PixelPacket *) NULL)
5635 break;
5636 (void) GetFillColor(draw_info,x,y,&fill_color);
5637 MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
5638 (MagickRealType) q->opacity,q);
5639 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5640 break;
5641 }
5642 case TextPrimitive:
5643 {
5644 char
5645 geometry[MaxTextExtent];
5646
5647 DrawInfo
5648 *clone_info;
5649
5650 if (primitive_info->text == (char *) NULL)
5651 break;
5652 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5653 (void) CloneString(&clone_info->text,primitive_info->text);
5654 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
5655 primitive_info->point.x,primitive_info->point.y);
5656 (void) CloneString(&clone_info->geometry,geometry);
5657 status&=AnnotateImage(image,clone_info);
5658 clone_info=DestroyDrawInfo(clone_info);
5659 break;
5660 }
5661 default:
5662 {
5663 double
5664 mid,
5665 scale;
5666
5667 DrawInfo
5668 *clone_info;
5669
5670 if (IsEventLogging() != MagickFalse)
5671 LogPrimitiveInfo(primitive_info);
5672 scale=ExpandAffine(&draw_info->affine);
5673 if ((draw_info->dash_pattern != (double *) NULL) &&
5674 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5675 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5676 (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
5677 {
5678 /*
5679 Draw dash polygon.
5680 */
5681 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5682 clone_info->stroke_width=0.0;
5683 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5684 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5685 clone_info=DestroyDrawInfo(clone_info);
5686 if (status != MagickFalse)
5687 status&=DrawDashPolygon(draw_info,primitive_info,image);
5688 break;
5689 }
5690 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5691 if ((mid > 1.0) &&
5692 ((draw_info->stroke.opacity != (Quantum) TransparentOpacity) ||
5693 (draw_info->stroke_pattern != (Image *) NULL)))
5694 {
5695 double
5696 x,
5697 y;
5698
5699 MagickBooleanType
5700 closed_path;
5701
5702 /*
5703 Draw strokes while respecting line cap/join attributes.
5704 */
5705 closed_path=primitive_info[0].closed_subpath;
5706 i=(ssize_t) primitive_info[0].coordinates;
5707 x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5708 y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5709 if ((x < MagickEpsilon) && (y < MagickEpsilon))
5710 closed_path=MagickTrue;
5711 if ((((draw_info->linecap == RoundCap) ||
5712 (closed_path != MagickFalse)) &&
5713 (draw_info->linejoin == RoundJoin)) ||
5714 (primitive_info[i].primitive != UndefinedPrimitive))
5715 {
5716 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5717 break;
5718 }
5719 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5720 clone_info->stroke_width=0.0;
5721 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5722 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5723 clone_info=DestroyDrawInfo(clone_info);
5724 if (status != MagickFalse)
5725 status&=DrawStrokePolygon(image,draw_info,primitive_info);
5726 break;
5727 }
5728 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5729 break;
5730 }
5731 }
5732 image_view=DestroyCacheView(image_view);
5733 if (draw_info->compliance == SVGCompliance)
5734 {
5735 status&=SetImageClipMask(image,(Image *) NULL);
5736 status&=SetImageMask(image,(Image *) NULL);
5737 }
5738 if (draw_info->debug != MagickFalse)
5739 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
5740 return(status != 0 ? MagickTrue : MagickFalse);
5741}
5742
5743/*
5744%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5745% %
5746% %
5747% %
5748+ D r a w S t r o k e P o l y g o n %
5749% %
5750% %
5751% %
5752%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5753%
5754% DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
5755% the image while respecting the line cap and join attributes.
5756%
5757% The format of the DrawStrokePolygon method is:
5758%
5759% MagickBooleanType DrawStrokePolygon(Image *image,
5760% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5761%
5762% A description of each parameter follows:
5763%
5764% o image: the image.
5765%
5766% o draw_info: the draw info.
5767%
5768% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5769%
5770%
5771*/
5772
5773static MagickBooleanType DrawRoundLinecap(Image *image,
5774 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5775{
5776 PrimitiveInfo
5777 linecap[5];
5778
5779 ssize_t
5780 i;
5781
5782 if (primitive_info->coordinates < 1)
5783 return(MagickFalse);
5784 for (i=0; i < 4; i++)
5785 linecap[i]=(*primitive_info);
5786 linecap[0].coordinates=4;
5787 linecap[1].point.x+=2.0*MagickEpsilon;
5788 linecap[2].point.x+=2.0*MagickEpsilon;
5789 linecap[2].point.y+=2.0*MagickEpsilon;
5790 linecap[3].point.y+=2.0*MagickEpsilon;
5791 linecap[4].primitive=UndefinedPrimitive;
5792 return(DrawPolygonPrimitive(image,draw_info,linecap));
5793}
5794
5795static MagickBooleanType DrawStrokePolygon(Image *image,
5796 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5797{
5798 DrawInfo
5799 *clone_info;
5800
5801 MagickBooleanType
5802 closed_path;
5803
5804 MagickStatusType
5805 status;
5806
5807 PrimitiveInfo
5808 *stroke_polygon;
5809
5810 const PrimitiveInfo
5811 *p,
5812 *q;
5813
5814 /*
5815 Draw stroked polygon.
5816 */
5817 if (draw_info->debug != MagickFalse)
5818 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5819 " begin draw-stroke-polygon");
5820 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5821 clone_info->fill=draw_info->stroke;
5822 if (clone_info->fill_pattern != (Image *) NULL)
5823 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5824 if (clone_info->stroke_pattern != (Image *) NULL)
5825 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5826 MagickTrue,&clone_info->stroke_pattern->exception);
5827 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5828 clone_info->stroke_width=0.0;
5829 clone_info->fill_rule=NonZeroRule;
5830 status=MagickTrue;
5831 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
5832 {
5833 if (p->coordinates == 1)
5834 continue;
5835 stroke_polygon=TraceStrokePolygon(draw_info,p,&image->exception);
5836 if (stroke_polygon == (PrimitiveInfo *) NULL)
5837 {
5838 status=0;
5839 break;
5840 }
5841 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
5842 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5843 if (status == 0)
5844 break;
5845 q=p+p->coordinates-1;
5846 closed_path=p->closed_subpath;
5847 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5848 {
5849 status&=DrawRoundLinecap(image,draw_info,p);
5850 status&=DrawRoundLinecap(image,draw_info,q);
5851 }
5852 }
5853 clone_info=DestroyDrawInfo(clone_info);
5854 if (draw_info->debug != MagickFalse)
5855 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5856 " end draw-stroke-polygon");
5857 return(status != 0 ? MagickTrue : MagickFalse);
5858}
5859
5860/*
5861%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5862% %
5863% %
5864% %
5865% G e t A f f i n e M a t r i x %
5866% %
5867% %
5868% %
5869%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5870%
5871% GetAffineMatrix() returns an AffineMatrix initialized to the identity
5872% matrix.
5873%
5874% The format of the GetAffineMatrix method is:
5875%
5876% void GetAffineMatrix(AffineMatrix *affine_matrix)
5877%
5878% A description of each parameter follows:
5879%
5880% o affine_matrix: the affine matrix.
5881%
5882*/
5883MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
5884{
5885 assert(affine_matrix != (AffineMatrix *) NULL);
5886 if (IsEventLogging() != MagickFalse)
5887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5888 (void) memset(affine_matrix,0,sizeof(*affine_matrix));
5889 affine_matrix->sx=1.0;
5890 affine_matrix->sy=1.0;
5891}
5892
5893/*
5894%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5895% %
5896% %
5897% %
5898+ G e t D r a w I n f o %
5899% %
5900% %
5901% %
5902%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5903%
5904% GetDrawInfo() initializes draw_info to default values from image_info.
5905%
5906% The format of the GetDrawInfo method is:
5907%
5908% void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5909%
5910% A description of each parameter follows:
5911%
5912% o image_info: the image info..
5913%
5914% o draw_info: the draw info.
5915%
5916*/
5917MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5918{
5919 char
5920 *next_token;
5921
5922 const char
5923 *option;
5924
5925 ExceptionInfo
5926 *exception;
5927
5928 /*
5929 Initialize draw attributes.
5930 */
5931 assert(draw_info != (DrawInfo *) NULL);
5932 if (IsEventLogging() != MagickFalse)
5933 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5934 (void) memset(draw_info,0,sizeof(*draw_info));
5935 draw_info->image_info=CloneImageInfo(image_info);
5936 GetAffineMatrix(&draw_info->affine);
5937 exception=AcquireExceptionInfo();
5938 (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
5939 (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
5940 draw_info->stroke_antialias=draw_info->image_info->antialias;
5941 draw_info->stroke_width=1.0;
5942 draw_info->fill_rule=EvenOddRule;
5943 draw_info->opacity=OpaqueOpacity;
5944 draw_info->fill_opacity=OpaqueOpacity;
5945 draw_info->stroke_opacity=OpaqueOpacity;
5946 draw_info->linecap=ButtCap;
5947 draw_info->linejoin=MiterJoin;
5948 draw_info->miterlimit=10;
5949 draw_info->decorate=NoDecoration;
5950 if (draw_info->image_info->font != (char *) NULL)
5951 draw_info->font=AcquireString(draw_info->image_info->font);
5952 if (draw_info->image_info->density != (char *) NULL)
5953 draw_info->density=AcquireString(draw_info->image_info->density);
5954 draw_info->text_antialias=draw_info->image_info->antialias;
5955 draw_info->pointsize=12.0;
5956 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
5957 draw_info->pointsize=draw_info->image_info->pointsize;
5958 draw_info->undercolor.opacity=(Quantum) TransparentOpacity;
5959 draw_info->border_color=draw_info->image_info->border_color;
5960 draw_info->compose=OverCompositeOp;
5961 if (draw_info->image_info->server_name != (char *) NULL)
5962 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
5963 draw_info->render=MagickTrue;
5964 draw_info->clip_path=MagickFalse;
5965 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
5966 MagickTrue : MagickFalse;
5967 option=GetImageOption(draw_info->image_info,"direction");
5968 if (option != (const char *) NULL)
5969 draw_info->direction=(DirectionType) ParseCommandOption(
5970 MagickDirectionOptions,MagickFalse,option);
5971 else
5972 draw_info->direction=UndefinedDirection;
5973 option=GetImageOption(draw_info->image_info,"encoding");
5974 if (option != (const char *) NULL)
5975 (void) CloneString(&draw_info->encoding,option);
5976 option=GetImageOption(draw_info->image_info,"family");
5977 if (option != (const char *) NULL)
5978 (void) CloneString(&draw_info->family,option);
5979 option=GetImageOption(draw_info->image_info,"fill");
5980 if (option != (const char *) NULL)
5981 (void) QueryColorDatabase(option,&draw_info->fill,exception);
5982 option=GetImageOption(draw_info->image_info,"gravity");
5983 if (option != (const char *) NULL)
5984 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
5985 MagickFalse,option);
5986 option=GetImageOption(draw_info->image_info,"interline-spacing");
5987 if (option != (const char *) NULL)
5988 draw_info->interline_spacing=GetDrawValue(option,&next_token);
5989 option=GetImageOption(draw_info->image_info,"interword-spacing");
5990 if (option != (const char *) NULL)
5991 draw_info->interword_spacing=GetDrawValue(option,&next_token);
5992 option=GetImageOption(draw_info->image_info,"kerning");
5993 if (option != (const char *) NULL)
5994 draw_info->kerning=GetDrawValue(option,&next_token);
5995 option=GetImageOption(draw_info->image_info,"stroke");
5996 if (option != (const char *) NULL)
5997 (void) QueryColorDatabase(option,&draw_info->stroke,exception);
5998 option=GetImageOption(draw_info->image_info,"strokewidth");
5999 if (option != (const char *) NULL)
6000 draw_info->stroke_width=GetDrawValue(option,&next_token);
6001 option=GetImageOption(draw_info->image_info,"style");
6002 if (option != (const char *) NULL)
6003 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6004 MagickFalse,option);
6005 option=GetImageOption(draw_info->image_info,"undercolor");
6006 if (option != (const char *) NULL)
6007 (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
6008 option=GetImageOption(draw_info->image_info,"weight");
6009 if (option != (const char *) NULL)
6010 {
6011 ssize_t
6012 weight;
6013
6014 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6015 if (weight == -1)
6016 weight=(ssize_t) StringToUnsignedLong(option);
6017 draw_info->weight=(size_t) weight;
6018 }
6019 exception=DestroyExceptionInfo(exception);
6020 draw_info->signature=MagickCoreSignature;
6021}
6022
6023/*
6024%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6025% %
6026% %
6027% %
6028+ P e r m u t a t e %
6029% %
6030% %
6031% %
6032%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6033%
6034% Permutate() returns the permutation of the (n,k).
6035%
6036% The format of the Permutate method is:
6037%
6038% void Permutate(ssize_t n,ssize_t k)
6039%
6040% A description of each parameter follows:
6041%
6042% o n:
6043%
6044% o k:
6045%
6046%
6047*/
6048static inline double Permutate(const ssize_t n,const ssize_t k)
6049{
6050 double
6051 r;
6052
6053 ssize_t
6054 i;
6055
6056 r=1.0;
6057 for (i=k+1; i <= n; i++)
6058 r*=i;
6059 for (i=1; i <= (n-k); i++)
6060 r/=i;
6061 return(r);
6062}
6063
6064/*
6065%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6066% %
6067% %
6068% %
6069+ T r a c e P r i m i t i v e %
6070% %
6071% %
6072% %
6073%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6074%
6075% TracePrimitive is a collection of methods for generating graphic
6076% primitives such as arcs, ellipses, paths, etc.
6077%
6078*/
6079
6080static MagickBooleanType TraceArc(MVGInfo *mvg_info,const PointInfo start,
6081 const PointInfo end,const PointInfo degrees)
6082{
6083 PointInfo
6084 center,
6085 radius;
6086
6087 center.x=0.5*(end.x+start.x);
6088 center.y=0.5*(end.y+start.y);
6089 radius.x=fabs(center.x-start.x);
6090 radius.y=fabs(center.y-start.y);
6091 return(TraceEllipse(mvg_info,center,radius,degrees));
6092}
6093
6094static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,const PointInfo start,
6095 const PointInfo end,const PointInfo arc,const double angle,
6096 const MagickBooleanType large_arc,const MagickBooleanType sweep)
6097{
6098 double
6099 alpha,
6100 beta,
6101 delta,
6102 factor,
6103 gamma,
6104 theta;
6105
6106 MagickStatusType
6107 status;
6108
6109 PointInfo
6110 center,
6111 points[3],
6112 radii;
6113
6114 double
6115 cosine,
6116 sine;
6117
6118 PrimitiveInfo
6119 *primitive_info;
6120
6121 PrimitiveInfo
6122 *p;
6123
6124 ssize_t
6125 i;
6126
6127 size_t
6128 arc_segments;
6129
6130 ssize_t
6131 offset;
6132
6133 offset=mvg_info->offset;
6134 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6135 primitive_info->coordinates=0;
6136 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6137 (fabs(start.y-end.y) < MagickEpsilon))
6138 return(TracePoint(primitive_info,end));
6139 radii.x=fabs(arc.x);
6140 radii.y=fabs(arc.y);
6141 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6142 return(TraceLine(primitive_info,start,end));
6143 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
6144 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
6145 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6146 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6147 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6148 (radii.y*radii.y);
6149 if (delta < MagickEpsilon)
6150 return(TraceLine(primitive_info,start,end));
6151 if (delta > 1.0)
6152 {
6153 radii.x*=sqrt((double) delta);
6154 radii.y*=sqrt((double) delta);
6155 }
6156 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6157 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6158 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6159 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6160 alpha=points[1].x-points[0].x;
6161 beta=points[1].y-points[0].y;
6162 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6163 return(TraceLine(primitive_info,start,end));
6164 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6165 if (factor <= 0.0)
6166 factor=0.0;
6167 else
6168 {
6169 factor=sqrt((double) factor);
6170 if (sweep == large_arc)
6171 factor=(-factor);
6172 }
6173 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6174 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6175 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6176 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6177 if ((theta < 0.0) && (sweep != MagickFalse))
6178 theta+=2.0*MagickPI;
6179 else
6180 if ((theta > 0.0) && (sweep == MagickFalse))
6181 theta-=2.0*MagickPI;
6182 arc_segments=(size_t) CastDoubleToLong(ceil(fabs((double) (theta/(0.5*
6183 MagickPI+MagickEpsilon)))));
6184 p=primitive_info;
6185 status=MagickTrue;
6186 for (i=0; i < (ssize_t) arc_segments; i++)
6187 {
6188 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6189 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
6190 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
6191 sin(fmod((double) beta,DegreesToRadians(360.0)));
6192 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
6193 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
6194 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6195 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
6196 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
6197 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6198 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
6199 theta/arc_segments),DegreesToRadians(360.0))));
6200 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
6201 theta/arc_segments),DegreesToRadians(360.0))));
6202 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
6203 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6204 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
6205 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6206 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6207 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6208 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6209 points[0].y);
6210 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6211 points[0].y);
6212 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6213 points[1].y);
6214 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6215 points[1].y);
6216 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6217 points[2].y);
6218 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6219 points[2].y);
6220 if (i == (ssize_t) (arc_segments-1))
6221 (p+3)->point=end;
6222 status&=TraceBezier(mvg_info,4);
6223 if (status == 0)
6224 break;
6225 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6226 mvg_info->offset+=p->coordinates;
6227 p+=(ptrdiff_t) p->coordinates;
6228 }
6229 if (status == 0)
6230 return(MagickFalse);
6231 mvg_info->offset=offset;
6232 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6233 primitive_info->coordinates=(size_t) (p-primitive_info);
6234 primitive_info->closed_subpath=MagickFalse;
6235 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6236 {
6237 p->primitive=primitive_info->primitive;
6238 p--;
6239 }
6240 return(MagickTrue);
6241}
6242
6243static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6244 const size_t number_coordinates)
6245{
6246 double
6247 alpha,
6248 *coefficients,
6249 weight;
6250
6251 PointInfo
6252 end,
6253 point,
6254 *points;
6255
6256 PrimitiveInfo
6257 *primitive_info;
6258
6259 PrimitiveInfo
6260 *p;
6261
6262 ssize_t
6263 i,
6264 j;
6265
6266 size_t
6267 control_points,
6268 quantum;
6269
6270 /*
6271 Allocate coefficients.
6272 */
6273 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6274 quantum=number_coordinates;
6275 for (i=0; i < (ssize_t) number_coordinates; i++)
6276 {
6277 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6278 {
6279 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6280 if (alpha > (double) GetMaxMemoryRequest())
6281 {
6282 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6283 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6284 return(MagickFalse);
6285 }
6286 if (alpha > (double) quantum)
6287 quantum=(size_t) alpha;
6288 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6289 if (alpha > (double) quantum)
6290 quantum=(size_t) alpha;
6291 }
6292 }
6293 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6294 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6295 if (quantum > (double) GetMaxMemoryRequest())
6296 {
6297 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6298 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6299 return(MagickFalse);
6300 }
6301 coefficients=(double *) AcquireQuantumMemory(number_coordinates,
6302 sizeof(*coefficients));
6303 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6304 sizeof(*points));
6305 if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
6306 {
6307 if (points != (PointInfo *) NULL)
6308 points=(PointInfo *) RelinquishMagickMemory(points);
6309 if (coefficients != (double *) NULL)
6310 coefficients=(double *) RelinquishMagickMemory(coefficients);
6311 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6312 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6313 return(MagickFalse);
6314 }
6315 control_points=quantum*number_coordinates;
6316 if (CheckPrimitiveExtent(mvg_info,(double) control_points+1) == MagickFalse)
6317 {
6318 points=(PointInfo *) RelinquishMagickMemory(points);
6319 coefficients=(double *) RelinquishMagickMemory(coefficients);
6320 return(MagickFalse);
6321 }
6322 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6323 /*
6324 Compute bezier points.
6325 */
6326 end=primitive_info[number_coordinates-1].point;
6327 for (i=0; i < (ssize_t) number_coordinates; i++)
6328 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6329 weight=0.0;
6330 for (i=0; i < (ssize_t) control_points; i++)
6331 {
6332 p=primitive_info;
6333 point.x=0.0;
6334 point.y=0.0;
6335 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
6336 for (j=0; j < (ssize_t) number_coordinates; j++)
6337 {
6338 point.x+=alpha*coefficients[j]*p->point.x;
6339 point.y+=alpha*coefficients[j]*p->point.y;
6340 alpha*=weight/(1.0-weight);
6341 p++;
6342 }
6343 points[i]=point;
6344 weight+=1.0/control_points;
6345 }
6346 /*
6347 Bezier curves are just short segmented polys.
6348 */
6349 p=primitive_info;
6350 for (i=0; i < (ssize_t) control_points; i++)
6351 {
6352 if (TracePoint(p,points[i]) == MagickFalse)
6353 {
6354 points=(PointInfo *) RelinquishMagickMemory(points);
6355 coefficients=(double *) RelinquishMagickMemory(coefficients);
6356 return(MagickFalse);
6357 }
6358 p+=(ptrdiff_t) p->coordinates;
6359 }
6360 if (TracePoint(p,end) == MagickFalse)
6361 {
6362 points=(PointInfo *) RelinquishMagickMemory(points);
6363 coefficients=(double *) RelinquishMagickMemory(coefficients);
6364 return(MagickFalse);
6365 }
6366 p+=(ptrdiff_t) p->coordinates;
6367 primitive_info->coordinates=(size_t) (p-primitive_info);
6368 primitive_info->closed_subpath=MagickFalse;
6369 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6370 {
6371 p->primitive=primitive_info->primitive;
6372 p--;
6373 }
6374 points=(PointInfo *) RelinquishMagickMemory(points);
6375 coefficients=(double *) RelinquishMagickMemory(coefficients);
6376 return(MagickTrue);
6377}
6378
6379static MagickBooleanType TraceCircle(MVGInfo *mvg_info,const PointInfo start,
6380 const PointInfo end)
6381{
6382 double
6383 alpha,
6384 beta,
6385 radius;
6386
6387 PointInfo
6388 offset,
6389 degrees;
6390
6391 alpha=end.x-start.x;
6392 beta=end.y-start.y;
6393 radius=hypot((double) alpha,(double) beta);
6394 offset.x=(double) radius;
6395 offset.y=(double) radius;
6396 degrees.x=0.0;
6397 degrees.y=360.0;
6398 return(TraceEllipse(mvg_info,start,offset,degrees));
6399}
6400
6401static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,const PointInfo center,
6402 const PointInfo radii,const PointInfo arc)
6403{
6404 double
6405 coordinates,
6406 delta,
6407 step,
6408 x,
6409 y;
6410
6411 PointInfo
6412 angle,
6413 point;
6414
6415 PrimitiveInfo
6416 *primitive_info;
6417
6418 PrimitiveInfo
6419 *p;
6420
6421 ssize_t
6422 i;
6423
6424 /*
6425 Ellipses are just short segmented polys.
6426 */
6427 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6428 primitive_info->coordinates=0;
6429 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6430 return(MagickTrue);
6431 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6432 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6433 angle.x=DegreesToRadians(arc.x);
6434 y=arc.y;
6435 while (y < arc.x)
6436 y+=360.0;
6437 angle.y=DegreesToRadians(y);
6438 coordinates=ceil((angle.y-angle.x)/step+1.0);
6439 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6440 return(MagickFalse);
6441 i=0;
6442 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6443 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6444 {
6445 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6446 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6447 if (i++ >= (ssize_t) coordinates)
6448 break;
6449 if (TracePoint(p,point) == MagickFalse)
6450 return(MagickFalse);
6451 p+=(ptrdiff_t) p->coordinates;
6452 }
6453 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6454 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6455 if (TracePoint(p,point) == MagickFalse)
6456 return(MagickFalse);
6457 p+=(ptrdiff_t) p->coordinates;
6458 primitive_info->coordinates=(size_t) (p-primitive_info);
6459 primitive_info->closed_subpath=MagickFalse;
6460 x=fabs(primitive_info[0].point.x-
6461 primitive_info[primitive_info->coordinates-1].point.x);
6462 y=fabs(primitive_info[0].point.y-
6463 primitive_info[primitive_info->coordinates-1].point.y);
6464 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6465 primitive_info->closed_subpath=MagickTrue;
6466 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6467 {
6468 p->primitive=primitive_info->primitive;
6469 p--;
6470 }
6471 return(MagickTrue);
6472}
6473
6474static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6475 const PointInfo start,const PointInfo end)
6476{
6477 if (TracePoint(primitive_info,start) == MagickFalse)
6478 return(MagickFalse);
6479 if (TracePoint(primitive_info+1,end) == MagickFalse)
6480 return(MagickFalse);
6481 (primitive_info+1)->primitive=primitive_info->primitive;
6482 primitive_info->coordinates=2;
6483 primitive_info->closed_subpath=MagickFalse;
6484 return(MagickTrue);
6485}
6486
6487static ssize_t TracePath(Image *image,MVGInfo *mvg_info,const char *path)
6488{
6489 char
6490 *next_token,
6491 token[MaxTextExtent] = "";
6492
6493 const char
6494 *p;
6495
6496 double
6497 x,
6498 y;
6499
6500 int
6501 attribute,
6502 last_attribute;
6503
6504 MagickStatusType
6505 status;
6506
6507 PointInfo
6508 end = {0.0, 0.0},
6509 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6510 point = {0.0, 0.0},
6511 start = {0.0, 0.0};
6512
6513 PrimitiveInfo
6514 *primitive_info;
6515
6516 PrimitiveType
6517 primitive_type;
6518
6519 PrimitiveInfo
6520 *q;
6521
6522 ssize_t
6523 i;
6524
6525 size_t
6526 number_coordinates,
6527 z_count;
6528
6529 ssize_t
6530 subpath_offset;
6531
6532 subpath_offset=mvg_info->offset;
6533 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6534 status=MagickTrue;
6535 attribute=0;
6536 number_coordinates=0;
6537 z_count=0;
6538 *token='\0';
6539 primitive_type=primitive_info->primitive;
6540 q=primitive_info;
6541 for (p=path; *p != '\0'; )
6542 {
6543 if (status == MagickFalse)
6544 break;
6545 while (isspace((int) ((unsigned char) *p)) != 0)
6546 p++;
6547 if (*p == '\0')
6548 break;
6549 last_attribute=attribute;
6550 attribute=(int) (*p++);
6551 switch (attribute)
6552 {
6553 case 'a':
6554 case 'A':
6555 {
6556 double
6557 angle = 0.0;
6558
6559 MagickBooleanType
6560 large_arc = MagickFalse,
6561 sweep = MagickFalse;
6562
6563 PointInfo
6564 arc = {0.0, 0.0};
6565
6566 /*
6567 Elliptical arc.
6568 */
6569 do
6570 {
6571 (void) GetNextToken(p,&p,MaxTextExtent,token);
6572 if (*token == ',')
6573 (void) GetNextToken(p,&p,MaxTextExtent,token);
6574 arc.x=GetDrawValue(token,&next_token);
6575 if (token == next_token)
6576 ThrowPointExpectedException(image,token);
6577 (void) GetNextToken(p,&p,MaxTextExtent,token);
6578 if (*token == ',')
6579 (void) GetNextToken(p,&p,MaxTextExtent,token);
6580 arc.y=GetDrawValue(token,&next_token);
6581 if (token == next_token)
6582 ThrowPointExpectedException(image,token);
6583 (void) GetNextToken(p,&p,MaxTextExtent,token);
6584 if (*token == ',')
6585 (void) GetNextToken(p,&p,MaxTextExtent,token);
6586 angle=GetDrawValue(token,&next_token);
6587 if (token == next_token)
6588 ThrowPointExpectedException(image,token);
6589 (void) GetNextToken(p,&p,MaxTextExtent,token);
6590 if (*token == ',')
6591 (void) GetNextToken(p,&p,MaxTextExtent,token);
6592 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6593 (void) GetNextToken(p,&p,MaxTextExtent,token);
6594 if (*token == ',')
6595 (void) GetNextToken(p,&p,MaxTextExtent,token);
6596 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6597 if (*token == ',')
6598 (void) GetNextToken(p,&p,MaxTextExtent,token);
6599 (void) GetNextToken(p,&p,MaxTextExtent,token);
6600 if (*token == ',')
6601 (void) GetNextToken(p,&p,MaxTextExtent,token);
6602 x=GetDrawValue(token,&next_token);
6603 if (token == next_token)
6604 ThrowPointExpectedException(image,token);
6605 (void) GetNextToken(p,&p,MaxTextExtent,token);
6606 if (*token == ',')
6607 (void) GetNextToken(p,&p,MaxTextExtent,token);
6608 y=GetDrawValue(token,&next_token);
6609 if (token == next_token)
6610 ThrowPointExpectedException(image,token);
6611 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
6612 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
6613 status&=TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep);
6614 q=(*mvg_info->primitive_info)+mvg_info->offset;
6615 mvg_info->offset+=q->coordinates;
6616 q+=(ptrdiff_t) q->coordinates;
6617 point=end;
6618 while (isspace((int) ((unsigned char) *p)) != 0)
6619 p++;
6620 if (*p == ',')
6621 p++;
6622 } while (IsPoint(p) != MagickFalse);
6623 break;
6624 }
6625 case 'c':
6626 case 'C':
6627 {
6628 /*
6629 Cubic Bézier curve.
6630 */
6631 do
6632 {
6633 points[0]=point;
6634 for (i=1; i < 4; i++)
6635 {
6636 (void) GetNextToken(p,&p,MaxTextExtent,token);
6637 if (*token == ',')
6638 (void) GetNextToken(p,&p,MaxTextExtent,token);
6639 x=GetDrawValue(token,&next_token);
6640 if (token == next_token)
6641 ThrowPointExpectedException(image,token);
6642 (void) GetNextToken(p,&p,MaxTextExtent,token);
6643 if (*token == ',')
6644 (void) GetNextToken(p,&p,MaxTextExtent,token);
6645 y=GetDrawValue(token,&next_token);
6646 if (token == next_token)
6647 ThrowPointExpectedException(image,token);
6648 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
6649 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
6650 points[i]=end;
6651 }
6652 for (i=0; i < 4; i++)
6653 (q+i)->point=points[i];
6654 if (TraceBezier(mvg_info,4) == MagickFalse)
6655 return(-1);
6656 q=(*mvg_info->primitive_info)+mvg_info->offset;
6657 mvg_info->offset+=q->coordinates;
6658 q+=(ptrdiff_t) q->coordinates;
6659 point=end;
6660 while (isspace((int) ((unsigned char) *p)) != 0)
6661 p++;
6662 if (*p == ',')
6663 p++;
6664 } while (IsPoint(p) != MagickFalse);
6665 break;
6666 }
6667 case 'H':
6668 case 'h':
6669 {
6670 do
6671 {
6672 (void) GetNextToken(p,&p,MaxTextExtent,token);
6673 if (*token == ',')
6674 (void) GetNextToken(p,&p,MaxTextExtent,token);
6675 x=GetDrawValue(token,&next_token);
6676 if (token == next_token)
6677 ThrowPointExpectedException(image,token);
6678 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
6679 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6680 return(-1);
6681 q=(*mvg_info->primitive_info)+mvg_info->offset;
6682 if (TracePoint(q,point) == MagickFalse)
6683 return(-1);
6684 mvg_info->offset+=q->coordinates;
6685 q+=(ptrdiff_t) q->coordinates;
6686 while (isspace((int) ((unsigned char) *p)) != 0)
6687 p++;
6688 if (*p == ',')
6689 p++;
6690 } while (IsPoint(p) != MagickFalse);
6691 break;
6692 }
6693 case 'l':
6694 case 'L':
6695 {
6696 /*
6697 Line to.
6698 */
6699 do
6700 {
6701 (void) GetNextToken(p,&p,MaxTextExtent,token);
6702 if (*token == ',')
6703 (void) GetNextToken(p,&p,MaxTextExtent,token);
6704 x=GetDrawValue(token,&next_token);
6705 if (token == next_token)
6706 ThrowPointExpectedException(image,token);
6707 (void) GetNextToken(p,&p,MaxTextExtent,token);
6708 if (*token == ',')
6709 (void) GetNextToken(p,&p,MaxTextExtent,token);
6710 y=GetDrawValue(token,&next_token);
6711 if (token == next_token)
6712 ThrowPointExpectedException(image,token);
6713 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
6714 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
6715 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6716 return(-1);
6717 q=(*mvg_info->primitive_info)+mvg_info->offset;
6718 if (TracePoint(q,point) == MagickFalse)
6719 return(-1);
6720 mvg_info->offset+=q->coordinates;
6721 q+=(ptrdiff_t) q->coordinates;
6722 while (isspace((int) ((unsigned char) *p)) != 0)
6723 p++;
6724 if (*p == ',')
6725 p++;
6726 } while (IsPoint(p) != MagickFalse);
6727 break;
6728 }
6729 case 'M':
6730 case 'm':
6731 {
6732 /*
6733 Move to.
6734 */
6735 if (mvg_info->offset != subpath_offset)
6736 {
6737 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6738 primitive_info->coordinates=(size_t) (q-primitive_info);
6739 number_coordinates+=primitive_info->coordinates;
6740 primitive_info=q;
6741 subpath_offset=mvg_info->offset;
6742 }
6743 i=0;
6744 do
6745 {
6746 (void) GetNextToken(p,&p,MaxTextExtent,token);
6747 if (*token == ',')
6748 (void) GetNextToken(p,&p,MaxTextExtent,token);
6749 x=GetDrawValue(token,&next_token);
6750 if (token == next_token)
6751 ThrowPointExpectedException(image,token);
6752 (void) GetNextToken(p,&p,MaxTextExtent,token);
6753 if (*token == ',')
6754 (void) GetNextToken(p,&p,MaxTextExtent,token);
6755 y=GetDrawValue(token,&next_token);
6756 if (token == next_token)
6757 ThrowPointExpectedException(image,token);
6758 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
6759 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
6760 if (i == 0)
6761 start=point;
6762 i++;
6763 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6764 return(-1);
6765 q=(*mvg_info->primitive_info)+mvg_info->offset;
6766 if (TracePoint(q,point) == MagickFalse)
6767 return(-1);
6768 mvg_info->offset+=q->coordinates;
6769 q+=(ptrdiff_t) q->coordinates;
6770 while (isspace((int) ((unsigned char) *p)) != 0)
6771 p++;
6772 if (*p == ',')
6773 p++;
6774 } while (IsPoint(p) != MagickFalse);
6775 break;
6776 }
6777 case 'q':
6778 case 'Q':
6779 {
6780 /*
6781 Quadratic Bézier curve.
6782 */
6783 do
6784 {
6785 points[0]=point;
6786 for (i=1; i < 3; i++)
6787 {
6788 (void) GetNextToken(p,&p,MaxTextExtent,token);
6789 if (*token == ',')
6790 (void) GetNextToken(p,&p,MaxTextExtent,token);
6791 x=GetDrawValue(token,&next_token);
6792 if (token == next_token)
6793 ThrowPointExpectedException(image,token);
6794 (void) GetNextToken(p,&p,MaxTextExtent,token);
6795 if (*token == ',')
6796 (void) GetNextToken(p,&p,MaxTextExtent,token);
6797 y=GetDrawValue(token,&next_token);
6798 if (token == next_token)
6799 ThrowPointExpectedException(image,token);
6800 if (*p == ',')
6801 p++;
6802 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
6803 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
6804 points[i]=end;
6805 }
6806 for (i=0; i < 3; i++)
6807 (q+i)->point=points[i];
6808 if (TraceBezier(mvg_info,3) == MagickFalse)
6809 return(-1);
6810 q=(*mvg_info->primitive_info)+mvg_info->offset;
6811 mvg_info->offset+=q->coordinates;
6812 q+=(ptrdiff_t) q->coordinates;
6813 point=end;
6814 while (isspace((int) ((unsigned char) *p)) != 0)
6815 p++;
6816 if (*p == ',')
6817 p++;
6818 } while (IsPoint(p) != MagickFalse);
6819 break;
6820 }
6821 case 's':
6822 case 'S':
6823 {
6824 /*
6825 Cubic Bézier curve.
6826 */
6827 do
6828 {
6829 points[0]=points[3];
6830 points[1].x=2.0*points[3].x-points[2].x;
6831 points[1].y=2.0*points[3].y-points[2].y;
6832 for (i=2; i < 4; i++)
6833 {
6834 (void) GetNextToken(p,&p,MaxTextExtent,token);
6835 if (*token == ',')
6836 (void) GetNextToken(p,&p,MaxTextExtent,token);
6837 x=GetDrawValue(token,&next_token);
6838 if (token == next_token)
6839 ThrowPointExpectedException(image,token);
6840 (void) GetNextToken(p,&p,MaxTextExtent,token);
6841 if (*token == ',')
6842 (void) GetNextToken(p,&p,MaxTextExtent,token);
6843 y=GetDrawValue(token,&next_token);
6844 if (token == next_token)
6845 ThrowPointExpectedException(image,token);
6846 if (*p == ',')
6847 p++;
6848 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
6849 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
6850 points[i]=end;
6851 }
6852 if (strchr("CcSs",last_attribute) == (char *) NULL)
6853 {
6854 points[0]=point;
6855 points[1]=point;
6856 }
6857 for (i=0; i < 4; i++)
6858 (q+i)->point=points[i];
6859 if (TraceBezier(mvg_info,4) == MagickFalse)
6860 return(-1);
6861 q=(*mvg_info->primitive_info)+mvg_info->offset;
6862 mvg_info->offset+=q->coordinates;
6863 q+=(ptrdiff_t) q->coordinates;
6864 point=end;
6865 last_attribute=attribute;
6866 while (isspace((int) ((unsigned char) *p)) != 0)
6867 p++;
6868 if (*p == ',')
6869 p++;
6870 } while (IsPoint(p) != MagickFalse);
6871 break;
6872 }
6873 case 't':
6874 case 'T':
6875 {
6876 /*
6877 Quadratic Bézier curve.
6878 */
6879 do
6880 {
6881 points[0]=points[2];
6882 points[1].x=2.0*points[2].x-points[1].x;
6883 points[1].y=2.0*points[2].y-points[1].y;
6884 for (i=2; i < 3; i++)
6885 {
6886 (void) GetNextToken(p,&p,MaxTextExtent,token);
6887 if (*token == ',')
6888 (void) GetNextToken(p,&p,MaxTextExtent,token);
6889 x=GetDrawValue(token,&next_token);
6890 if (token == next_token)
6891 ThrowPointExpectedException(image,token);
6892 (void) GetNextToken(p,&p,MaxTextExtent,token);
6893 if (*token == ',')
6894 (void) GetNextToken(p,&p,MaxTextExtent,token);
6895 y=GetDrawValue(token,&next_token);
6896 if (token == next_token)
6897 ThrowPointExpectedException(image,token);
6898 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
6899 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
6900 points[i]=end;
6901 }
6902 if (status == MagickFalse)
6903 break;
6904 if (strchr("QqTt",last_attribute) == (char *) NULL)
6905 {
6906 points[0]=point;
6907 points[1]=point;
6908 }
6909 for (i=0; i < 3; i++)
6910 (q+i)->point=points[i];
6911 if (TraceBezier(mvg_info,3) == MagickFalse)
6912 return(-1);
6913 q=(*mvg_info->primitive_info)+mvg_info->offset;
6914 mvg_info->offset+=q->coordinates;
6915 q+=(ptrdiff_t) q->coordinates;
6916 point=end;
6917 last_attribute=attribute;
6918 while (isspace((int) ((unsigned char) *p)) != 0)
6919 p++;
6920 if (*p == ',')
6921 p++;
6922 } while (IsPoint(p) != MagickFalse);
6923 break;
6924 }
6925 case 'v':
6926 case 'V':
6927 {
6928 /*
6929 Line to.
6930 */
6931 do
6932 {
6933 (void) GetNextToken(p,&p,MaxTextExtent,token);
6934 if (*token == ',')
6935 (void) GetNextToken(p,&p,MaxTextExtent,token);
6936 y=GetDrawValue(token,&next_token);
6937 if (token == next_token)
6938 ThrowPointExpectedException(image,token);
6939 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
6940 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6941 return(-1);
6942 q=(*mvg_info->primitive_info)+mvg_info->offset;
6943 if (TracePoint(q,point) == MagickFalse)
6944 return(-1);
6945 mvg_info->offset+=q->coordinates;
6946 q+=(ptrdiff_t) q->coordinates;
6947 while (isspace((int) ((unsigned char) *p)) != 0)
6948 p++;
6949 if (*p == ',')
6950 p++;
6951 } while (IsPoint(p) != MagickFalse);
6952 break;
6953 }
6954 case 'z':
6955 case 'Z':
6956 {
6957 /*
6958 Close path.
6959 */
6960 point=start;
6961 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6962 return(-1);
6963 q=(*mvg_info->primitive_info)+mvg_info->offset;
6964 if (TracePoint(q,point) == MagickFalse)
6965 return(-1);
6966 mvg_info->offset+=q->coordinates;
6967 q+=(ptrdiff_t) q->coordinates;
6968 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6969 primitive_info->coordinates=(size_t) (q-primitive_info);
6970 primitive_info->closed_subpath=MagickTrue;
6971 number_coordinates+=primitive_info->coordinates;
6972 primitive_info=q;
6973 subpath_offset=mvg_info->offset;
6974 z_count++;
6975 break;
6976 }
6977 default:
6978 {
6979 ThrowPointExpectedException(image,token);
6980 break;
6981 }
6982 }
6983 }
6984 if (status == MagickFalse)
6985 return(-1);
6986 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6987 primitive_info->coordinates=(size_t) (q-primitive_info);
6988 number_coordinates+=primitive_info->coordinates;
6989 for (i=0; i < (ssize_t) number_coordinates; i++)
6990 {
6991 q--;
6992 q->primitive=primitive_type;
6993 if (z_count > 1)
6994 q->method=FillToBorderMethod;
6995 }
6996 q=primitive_info;
6997 return((ssize_t) number_coordinates);
6998}
6999
7000static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7001 const PointInfo start,const PointInfo end)
7002{
7003 PointInfo
7004 point;
7005
7006 PrimitiveInfo
7007 *p;
7008
7009 ssize_t
7010 i;
7011
7012 p=primitive_info;
7013 if (TracePoint(p,start) == MagickFalse)
7014 return(MagickFalse);
7015 p+=(ptrdiff_t) p->coordinates;
7016 point.x=start.x;
7017 point.y=end.y;
7018 if (TracePoint(p,point) == MagickFalse)
7019 return(MagickFalse);
7020 p+=(ptrdiff_t) p->coordinates;
7021 if (TracePoint(p,end) == MagickFalse)
7022 return(MagickFalse);
7023 p+=(ptrdiff_t) p->coordinates;
7024 point.x=end.x;
7025 point.y=start.y;
7026 if (TracePoint(p,point) == MagickFalse)
7027 return(MagickFalse);
7028 p+=(ptrdiff_t) p->coordinates;
7029 if (TracePoint(p,start) == MagickFalse)
7030 return(MagickFalse);
7031 p+=(ptrdiff_t) p->coordinates;
7032 primitive_info->coordinates=(size_t) (p-primitive_info);
7033 primitive_info->closed_subpath=MagickTrue;
7034 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7035 {
7036 p->primitive=primitive_info->primitive;
7037 p--;
7038 }
7039 return(MagickTrue);
7040}
7041
7042static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7043 const PointInfo start,const PointInfo end,PointInfo arc)
7044{
7045 PointInfo
7046 degrees,
7047 point,
7048 segment;
7049
7050 PrimitiveInfo
7051 *primitive_info;
7052
7053 PrimitiveInfo
7054 *p;
7055
7056 ssize_t
7057 i;
7058
7059 ssize_t
7060 offset;
7061
7062 offset=mvg_info->offset;
7063 segment.x=fabs(end.x-start.x);
7064 segment.y=fabs(end.y-start.y);
7065 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7066 {
7067 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7068 return(MagickTrue);
7069 }
7070 if (arc.x > (0.5*segment.x))
7071 arc.x=0.5*segment.x;
7072 if (arc.y > (0.5*segment.y))
7073 arc.y=0.5*segment.y;
7074 point.x=start.x+segment.x-arc.x;
7075 point.y=start.y+arc.y;
7076 degrees.x=270.0;
7077 degrees.y=360.0;
7078 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7079 return(MagickFalse);
7080 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7081 mvg_info->offset+=p->coordinates;
7082 point.x=start.x+segment.x-arc.x;
7083 point.y=start.y+segment.y-arc.y;
7084 degrees.x=0.0;
7085 degrees.y=90.0;
7086 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7087 return(MagickFalse);
7088 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7089 mvg_info->offset+=p->coordinates;
7090 point.x=start.x+arc.x;
7091 point.y=start.y+segment.y-arc.y;
7092 degrees.x=90.0;
7093 degrees.y=180.0;
7094 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7095 return(MagickFalse);
7096 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7097 mvg_info->offset+=p->coordinates;
7098 point.x=start.x+arc.x;
7099 point.y=start.y+arc.y;
7100 degrees.x=180.0;
7101 degrees.y=270.0;
7102 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7103 return(MagickFalse);
7104 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7105 mvg_info->offset+=p->coordinates;
7106 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7107 return(MagickFalse);
7108 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7109 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7110 return(MagickFalse);
7111 p+=(ptrdiff_t) p->coordinates;
7112 mvg_info->offset=offset;
7113 primitive_info=(*mvg_info->primitive_info)+offset;
7114 primitive_info->coordinates=(size_t) (p-primitive_info);
7115 primitive_info->closed_subpath=MagickTrue;
7116 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7117 {
7118 p->primitive=primitive_info->primitive;
7119 p--;
7120 }
7121 return(MagickTrue);
7122}
7123
7124static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7125 const size_t number_vertices,const double offset)
7126{
7127 double
7128 distance;
7129
7130 double
7131 dx,
7132 dy;
7133
7134 ssize_t
7135 i;
7136
7137 ssize_t
7138 j;
7139
7140 dx=0.0;
7141 dy=0.0;
7142 for (i=1; i < (ssize_t) number_vertices; i++)
7143 {
7144 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7145 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7146 if ((fabs((double) dx) >= MagickEpsilon) ||
7147 (fabs((double) dy) >= MagickEpsilon))
7148 break;
7149 }
7150 if (i == (ssize_t) number_vertices)
7151 i=(ssize_t) number_vertices-1L;
7152 distance=hypot((double) dx,(double) dy);
7153 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7154 dx*(distance+offset)/distance);
7155 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7156 dy*(distance+offset)/distance);
7157 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7158 {
7159 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7160 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7161 if ((fabs((double) dx) >= MagickEpsilon) ||
7162 (fabs((double) dy) >= MagickEpsilon))
7163 break;
7164 }
7165 distance=hypot((double) dx,(double) dy);
7166 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7167 dx*(distance+offset)/distance);
7168 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7169 dy*(distance+offset)/distance);
7170 return(MagickTrue);
7171}
7172
7173static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
7174 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7175{
7176#define MaxStrokePad (6*BezierQuantum+360)
7177#define CheckPathExtent(pad_p,pad_q) \
7178{ \
7179 if ((pad_p) > MaxBezierCoordinates) \
7180 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7181 else \
7182 if ((ssize_t) (p+(pad_p)) >= (ssize_t) extent_p) \
7183 { \
7184 if (~extent_p < (pad_p)) \
7185 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7186 else \
7187 { \
7188 extent_p+=(pad_p); \
7189 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7190 MaxStrokePad,sizeof(*stroke_p)); \
7191 } \
7192 } \
7193 if ((pad_q) > MaxBezierCoordinates) \
7194 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7195 else \
7196 if ((ssize_t) (q+(pad_q)) >= (ssize_t) extent_q) \
7197 { \
7198 if (~extent_q < (pad_q)) \
7199 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7200 else \
7201 { \
7202 extent_q+=(pad_q); \
7203 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7204 MaxStrokePad,sizeof(*stroke_q)); \
7205 } \
7206 } \
7207 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7208 { \
7209 if (stroke_p != (PointInfo *) NULL) \
7210 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7211 if (stroke_q != (PointInfo *) NULL) \
7212 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7213 polygon_primitive=(PrimitiveInfo *) \
7214 RelinquishMagickMemory(polygon_primitive); \
7215 (void) ThrowMagickException(exception,GetMagickModule(), \
7216 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7217 return((PrimitiveInfo *) NULL); \
7218 } \
7219}
7220
7221 typedef struct _StrokeSegment
7222 {
7223 double
7224 p,
7225 q;
7226 } StrokeSegment;
7227
7228 double
7229 delta_theta,
7230 dot_product,
7231 mid,
7232 miterlimit;
7233
7234 MagickBooleanType
7235 closed_path;
7236
7237 PointInfo
7238 box_p[5],
7239 box_q[5],
7240 center,
7241 offset,
7242 *stroke_p,
7243 *stroke_q;
7244
7245 PrimitiveInfo
7246 *polygon_primitive,
7247 *stroke_polygon;
7248
7249 ssize_t
7250 i;
7251
7252 size_t
7253 arc_segments,
7254 extent_p,
7255 extent_q,
7256 number_vertices;
7257
7258 ssize_t
7259 j,
7260 n,
7261 p,
7262 q;
7263
7264 StrokeSegment
7265 dx = {0.0, 0.0},
7266 dy = {0.0, 0.0},
7267 inverse_slope = {0.0, 0.0},
7268 slope = {0.0, 0.0},
7269 theta = {0.0, 0.0};
7270
7271 /*
7272 Allocate paths.
7273 */
7274 number_vertices=primitive_info->coordinates;
7275 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7276 number_vertices+2UL,sizeof(*polygon_primitive));
7277 if (polygon_primitive == (PrimitiveInfo *) NULL)
7278 {
7279 (void) ThrowMagickException(exception,GetMagickModule(),
7280 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7281 return((PrimitiveInfo *) NULL);
7282 }
7283 (void) memcpy(polygon_primitive,primitive_info,(size_t) number_vertices*
7284 sizeof(*polygon_primitive));
7285 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7286 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7287 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7288 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7289 if ((draw_info->linejoin == MiterJoin) ||
7290 ((draw_info->linejoin == RoundJoin) && (closed_path != MagickFalse)))
7291 {
7292 polygon_primitive[number_vertices]=primitive_info[1];
7293 number_vertices++;
7294 }
7295 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7296 /*
7297 Compute the slope for the first line segment, p.
7298 */
7299 closed_path=primitive_info[0].closed_subpath;
7300 dx.p=0.0;
7301 dy.p=0.0;
7302 for (n=1; n < (ssize_t) number_vertices; n++)
7303 {
7304 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7305 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7306 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7307 break;
7308 }
7309 if (n == (ssize_t) number_vertices)
7310 {
7311 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7312 {
7313 /*
7314 Zero length subpath.
7315 */
7316 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7317 sizeof(*stroke_polygon));
7318 stroke_polygon[0]=polygon_primitive[0];
7319 stroke_polygon[0].coordinates=0;
7320 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7321 polygon_primitive);
7322 return(stroke_polygon);
7323 }
7324 n=(ssize_t) number_vertices-1L;
7325 }
7326 extent_p=2*number_vertices;
7327 extent_q=2*number_vertices;
7328 stroke_p=(PointInfo *) AcquireQuantumMemory((size_t) extent_p+MaxStrokePad,
7329 sizeof(*stroke_p));
7330 stroke_q=(PointInfo *) AcquireQuantumMemory((size_t) extent_q+MaxStrokePad,
7331 sizeof(*stroke_q));
7332 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7333 {
7334 if (stroke_p != (PointInfo *) NULL)
7335 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7336 if (stroke_q != (PointInfo *) NULL)
7337 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7338 polygon_primitive=(PrimitiveInfo *)
7339 RelinquishMagickMemory(polygon_primitive);
7340 (void) ThrowMagickException(exception,GetMagickModule(),
7341 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7342 return((PrimitiveInfo *) NULL);
7343 }
7344 slope.p=0.0;
7345 inverse_slope.p=0.0;
7346 if (fabs(dx.p) < MagickEpsilon)
7347 {
7348 if (dx.p >= 0.0)
7349 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7350 else
7351 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7352 }
7353 else
7354 if (fabs(dy.p) < MagickEpsilon)
7355 {
7356 if (dy.p >= 0.0)
7357 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7358 else
7359 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7360 }
7361 else
7362 {
7363 slope.p=dy.p/dx.p;
7364 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7365 }
7366 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7367 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7368 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7369 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7370 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7371 offset.y=(double) (offset.x*inverse_slope.p);
7372 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7373 {
7374 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7375 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7376 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7377 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7378 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7379 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7380 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7381 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7382 }
7383 else
7384 {
7385 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7386 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7387 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7388 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7389 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7390 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7391 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7392 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7393 }
7394 /*
7395 Create strokes for the line join attribute: bevel, miter, round.
7396 */
7397 p=0;
7398 q=0;
7399 stroke_q[p++]=box_q[0];
7400 stroke_p[q++]=box_p[0];
7401 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7402 {
7403 /*
7404 Compute the slope for this line segment, q.
7405 */
7406 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7407 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7408 dot_product=dx.q*dx.q+dy.q*dy.q;
7409 if (dot_product < 0.25)
7410 continue;
7411 slope.q=0.0;
7412 inverse_slope.q=0.0;
7413 if (fabs(dx.q) < MagickEpsilon)
7414 {
7415 if (dx.q >= 0.0)
7416 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7417 else
7418 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7419 }
7420 else
7421 if (fabs(dy.q) < MagickEpsilon)
7422 {
7423 if (dy.q >= 0.0)
7424 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7425 else
7426 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7427 }
7428 else
7429 {
7430 slope.q=dy.q/dx.q;
7431 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7432 }
7433 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7434 offset.y=(double) (offset.x*inverse_slope.q);
7435 dot_product=dy.q*offset.x-dx.q*offset.y;
7436 if (dot_product > 0.0)
7437 {
7438 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7439 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7440 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7441 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7442 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7443 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7444 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7445 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7446 }
7447 else
7448 {
7449 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7450 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7451 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7452 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7453 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7454 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7455 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7456 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7457 }
7458 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
7459 {
7460 box_p[4]=box_p[1];
7461 box_q[4]=box_q[1];
7462 }
7463 else
7464 {
7465 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7466 box_p[3].y)/(slope.p-slope.q));
7467 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7468 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7469 box_q[3].y)/(slope.p-slope.q));
7470 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7471 }
7472 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7473 dot_product=dx.q*dy.p-dx.p*dy.q;
7474 if (dot_product <= 0.0)
7475 switch (draw_info->linejoin)
7476 {
7477 case BevelJoin:
7478 {
7479 stroke_q[q++]=box_q[1];
7480 stroke_q[q++]=box_q[2];
7481 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7482 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7483 if (dot_product <= miterlimit)
7484 stroke_p[p++]=box_p[4];
7485 else
7486 {
7487 stroke_p[p++]=box_p[1];
7488 stroke_p[p++]=box_p[2];
7489 }
7490 break;
7491 }
7492 case MiterJoin:
7493 {
7494 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7495 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7496 if (dot_product <= miterlimit)
7497 {
7498 stroke_q[q++]=box_q[4];
7499 stroke_p[p++]=box_p[4];
7500 }
7501 else
7502 {
7503 stroke_q[q++]=box_q[1];
7504 stroke_q[q++]=box_q[2];
7505 stroke_p[p++]=box_p[1];
7506 stroke_p[p++]=box_p[2];
7507 }
7508 break;
7509 }
7510 case RoundJoin:
7511 {
7512 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7513 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7514 if (dot_product <= miterlimit)
7515 stroke_p[p++]=box_p[4];
7516 else
7517 {
7518 stroke_p[p++]=box_p[1];
7519 stroke_p[p++]=box_p[2];
7520 }
7521 center=polygon_primitive[n].point;
7522 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7523 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7524 if (theta.q < theta.p)
7525 theta.q+=2.0*MagickPI;
7526 arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.q-
7527 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7528 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7529 stroke_q[q].x=box_q[1].x;
7530 stroke_q[q].y=box_q[1].y;
7531 q++;
7532 for (j=1; j < (ssize_t) arc_segments; j++)
7533 {
7534 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7535 stroke_q[q].x=(double) (center.x+mid*cos(fmod((double)
7536 (theta.p+delta_theta),DegreesToRadians(360.0))));
7537 stroke_q[q].y=(double) (center.y+mid*sin(fmod((double)
7538 (theta.p+delta_theta),DegreesToRadians(360.0))));
7539 q++;
7540 }
7541 stroke_q[q++]=box_q[2];
7542 break;
7543 }
7544 default:
7545 break;
7546 }
7547 else
7548 switch (draw_info->linejoin)
7549 {
7550 case BevelJoin:
7551 {
7552 stroke_p[p++]=box_p[1];
7553 stroke_p[p++]=box_p[2];
7554 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7555 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7556 if (dot_product <= miterlimit)
7557 stroke_q[q++]=box_q[4];
7558 else
7559 {
7560 stroke_q[q++]=box_q[1];
7561 stroke_q[q++]=box_q[2];
7562 }
7563 break;
7564 }
7565 case MiterJoin:
7566 {
7567 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7568 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7569 if (dot_product <= miterlimit)
7570 {
7571 stroke_q[q++]=box_q[4];
7572 stroke_p[p++]=box_p[4];
7573 }
7574 else
7575 {
7576 stroke_q[q++]=box_q[1];
7577 stroke_q[q++]=box_q[2];
7578 stroke_p[p++]=box_p[1];
7579 stroke_p[p++]=box_p[2];
7580 }
7581 break;
7582 }
7583 case RoundJoin:
7584 {
7585 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7586 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7587 if (dot_product <= miterlimit)
7588 stroke_q[q++]=box_q[4];
7589 else
7590 {
7591 stroke_q[q++]=box_q[1];
7592 stroke_q[q++]=box_q[2];
7593 }
7594 center=polygon_primitive[n].point;
7595 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7596 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7597 if (theta.p < theta.q)
7598 theta.p+=2.0*MagickPI;
7599 arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.p-
7600 theta.q)/(2.0*sqrt((double) (MagickSafeReciprocal(mid)))))));
7601 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7602 stroke_p[p++]=box_p[1];
7603 for (j=1; j < (ssize_t) arc_segments; j++)
7604 {
7605 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7606 stroke_p[p].x=(double) (center.x+mid*cos(fmod((double)
7607 (theta.p+delta_theta),DegreesToRadians(360.0))));
7608 stroke_p[p].y=(double) (center.y+mid*sin(fmod((double)
7609 (theta.p+delta_theta),DegreesToRadians(360.0))));
7610 p++;
7611 }
7612 stroke_p[p++]=box_p[2];
7613 break;
7614 }
7615 default:
7616 break;
7617 }
7618 slope.p=slope.q;
7619 inverse_slope.p=inverse_slope.q;
7620 box_p[0]=box_p[2];
7621 box_p[1]=box_p[3];
7622 box_q[0]=box_q[2];
7623 box_q[1]=box_q[3];
7624 dx.p=dx.q;
7625 dy.p=dy.q;
7626 n=i;
7627 }
7628 stroke_p[p++]=box_p[1];
7629 stroke_q[q++]=box_q[1];
7630 /*
7631 Trace stroked polygon.
7632 */
7633 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7634 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
7635 if (stroke_polygon == (PrimitiveInfo *) NULL)
7636 {
7637 (void) ThrowMagickException(exception,GetMagickModule(),
7638 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7639 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7640 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7641 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7642 polygon_primitive);
7643 return(stroke_polygon);
7644 }
7645 for (i=0; i < (ssize_t) p; i++)
7646 {
7647 stroke_polygon[i]=polygon_primitive[0];
7648 stroke_polygon[i].point=stroke_p[i];
7649 }
7650 if (closed_path != MagickFalse)
7651 {
7652 stroke_polygon[i]=polygon_primitive[0];
7653 stroke_polygon[i].point=stroke_polygon[0].point;
7654 i++;
7655 }
7656 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7657 {
7658 stroke_polygon[i]=polygon_primitive[0];
7659 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7660 }
7661 if (closed_path != MagickFalse)
7662 {
7663 stroke_polygon[i]=polygon_primitive[0];
7664 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7665 i++;
7666 }
7667 stroke_polygon[i]=polygon_primitive[0];
7668 stroke_polygon[i].point=stroke_polygon[0].point;
7669 i++;
7670 stroke_polygon[i].primitive=UndefinedPrimitive;
7671 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7672 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7673 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7674 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7675 return(stroke_polygon);
7676}