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