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