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