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