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