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