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