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