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