MagickCore  6.9.13-50
Convert, Edit, Or Compose Bitmap Images
draw.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % DDDD RRRR AAA W W %
7 % D D R R A A W W %
8 % D D RRRR AAAAA W W W %
9 % D D R RN A A WW WW %
10 % DDDD R R A A W W %
11 % %
12 % %
13 % MagickCore Image Drawing Methods %
14 % %
15 % %
16 % Software Design %
17 % Cristy %
18 % July 1998 %
19 % %
20 % %
21 % Copyright 1999 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
23 % %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
26 % %
27 % https://imagemagick.org/license/ %
28 % %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
34 % %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
42 %
43 */
44 
45 /*
46  Include declarations.
47 */
48 #include "magick/studio.h"
49 #include "magick/annotate.h"
50 #include "magick/artifact.h"
51 #include "magick/blob.h"
52 #include "magick/cache.h"
53 #include "magick/cache-private.h"
54 #include "magick/cache-view.h"
55 #include "magick/channel.h"
56 #include "magick/color.h"
57 #include "magick/color-private.h"
58 #include "magick/colorspace.h"
59 #include "magick/colorspace-private.h"
60 #include "magick/composite.h"
61 #include "magick/composite-private.h"
62 #include "magick/constitute.h"
63 #include "magick/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=MagickSafeReciprocal(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  dash_polygon[0].closed_subpath=MagickFalse;
1790  scale=ExpandAffine(&draw_info->affine);
1791  length=scale*draw_info->dash_pattern[0];
1792  offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1793  scale*draw_info->dash_offset : 0.0;
1794  j=1;
1795  for (n=0; offset > 0.0; j=0)
1796  {
1797  if (draw_info->dash_pattern[n] <= 0.0)
1798  break;
1799  length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1800  if (offset > length)
1801  {
1802  offset-=length;
1803  n++;
1804  length=scale*draw_info->dash_pattern[n];
1805  continue;
1806  }
1807  if (offset < length)
1808  {
1809  length-=offset;
1810  offset=0.0;
1811  break;
1812  }
1813  offset=0.0;
1814  n++;
1815  }
1816  status=MagickTrue;
1817  maximum_length=0.0;
1818  total_length=0.0;
1819  for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1820  {
1821  dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1822  dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1823  maximum_length=hypot(dx,dy);
1824  if (maximum_length > (double) (MaxBezierCoordinates >> 2))
1825  continue;
1826  if (fabs(length) < MagickEpsilon)
1827  {
1828  if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1829  n++;
1830  if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1831  n=0;
1832  length=scale*draw_info->dash_pattern[n];
1833  }
1834  for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1835  {
1836  total_length+=length;
1837  if ((n & 0x01) != 0)
1838  {
1839  dash_polygon[0]=primitive_info[0];
1840  dash_polygon[0].closed_subpath=MagickFalse;
1841  dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1842  total_length*MagickSafeReciprocal(maximum_length));
1843  dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1844  total_length*MagickSafeReciprocal(maximum_length));
1845  j=1;
1846  }
1847  else
1848  {
1849  if ((j+1) > (ssize_t) number_vertices)
1850  break;
1851  dash_polygon[j]=primitive_info[i-1];
1852  dash_polygon[j].closed_subpath=MagickFalse;
1853  dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1854  total_length*MagickSafeReciprocal(maximum_length));
1855  dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1856  total_length*MagickSafeReciprocal(maximum_length));
1857  dash_polygon[j].coordinates=1;
1858  j++;
1859  dash_polygon[0].coordinates=(size_t) j;
1860  dash_polygon[j].primitive=UndefinedPrimitive;
1861  status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1862  if (status == MagickFalse)
1863  break;
1864  }
1865  if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1866  n++;
1867  if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1868  n=0;
1869  length=scale*draw_info->dash_pattern[n];
1870  }
1871  length-=(maximum_length-total_length);
1872  if ((n & 0x01) != 0)
1873  continue;
1874  dash_polygon[j]=primitive_info[i];
1875  dash_polygon[j].coordinates=1;
1876  j++;
1877  }
1878  if ((status != MagickFalse) && (total_length < maximum_length) &&
1879  ((n & 0x01) == 0) && (j > 1))
1880  {
1881  dash_polygon[j]=primitive_info[i-1];
1882  dash_polygon[j].closed_subpath=MagickFalse;
1883  dash_polygon[j].point.x+=MagickEpsilon;
1884  dash_polygon[j].point.y+=MagickEpsilon;
1885  dash_polygon[j].coordinates=1;
1886  j++;
1887  dash_polygon[0].coordinates=(size_t) j;
1888  dash_polygon[j].primitive=UndefinedPrimitive;
1889  status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1890  }
1891  dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1892  clone_info=DestroyDrawInfo(clone_info);
1893  if (draw_info->debug != MagickFalse)
1894  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1895  return(status != 0 ? MagickTrue : MagickFalse);
1896 }
1897 
1898 /*
1899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1900 % %
1901 % %
1902 % %
1903 % D r a w G r a d i e n t I m a g e %
1904 % %
1905 % %
1906 % %
1907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1908 %
1909 % DrawGradientImage() draws a linear gradient on the image.
1910 %
1911 % The format of the DrawGradientImage method is:
1912 %
1913 % MagickBooleanType DrawGradientImage(Image *image,
1914 % const DrawInfo *draw_info)
1915 %
1916 % A description of each parameter follows:
1917 %
1918 % o image: the image.
1919 %
1920 % o draw_info: the draw info.
1921 %
1922 */
1923 
1924 static inline double GetStopColorOffset(const GradientInfo *gradient,
1925  const ssize_t x,const ssize_t y)
1926 {
1927  switch (gradient->type)
1928  {
1929  case UndefinedGradient:
1930  case LinearGradient:
1931  {
1932  double
1933  gamma,
1934  length,
1935  offset,
1936  scale;
1937 
1938  PointInfo
1939  p,
1940  q;
1941 
1942  const SegmentInfo
1943  *gradient_vector;
1944 
1945  gradient_vector=(&gradient->gradient_vector);
1946  p.x=gradient_vector->x2-gradient_vector->x1;
1947  p.y=gradient_vector->y2-gradient_vector->y1;
1948  q.x=(double) x-gradient_vector->x1;
1949  q.y=(double) y-gradient_vector->y1;
1950  length=sqrt(q.x*q.x+q.y*q.y);
1951  gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1952  gamma=MagickSafeReciprocal(gamma);
1953  scale=p.x*q.x+p.y*q.y;
1954  offset=gamma*scale*length;
1955  return(offset);
1956  }
1957  case RadialGradient:
1958  {
1959  PointInfo
1960  v;
1961 
1962  if (gradient->spread == RepeatSpread)
1963  {
1964  v.x=(double) x-gradient->center.x;
1965  v.y=(double) y-gradient->center.y;
1966  return(sqrt(v.x*v.x+v.y*v.y));
1967  }
1968  v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1969  gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1970  gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1971  v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1972  gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1973  gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1974  return(sqrt(v.x*v.x+v.y*v.y));
1975  }
1976  }
1977  return(0.0);
1978 }
1979 
1980 MagickExport MagickBooleanType DrawGradientImage(Image *image,
1981  const DrawInfo *draw_info)
1982 {
1983  CacheView
1984  *image_view;
1985 
1986  const GradientInfo
1987  *gradient;
1988 
1989  const SegmentInfo
1990  *gradient_vector;
1991 
1992  double
1993  length;
1994 
1996  *exception;
1997 
1998  MagickBooleanType
1999  status;
2000 
2002  zero;
2003 
2004  PointInfo
2005  point;
2006 
2008  bounding_box;
2009 
2010  ssize_t
2011  height,
2012  y;
2013 
2014  /*
2015  Draw linear or radial gradient on image.
2016  */
2017  assert(image != (Image *) NULL);
2018  assert(image->signature == MagickCoreSignature);
2019  assert(draw_info != (const DrawInfo *) NULL);
2020  if (IsEventLogging() != MagickFalse)
2021  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2022  gradient=(&draw_info->gradient);
2023  gradient_vector=(&gradient->gradient_vector);
2024  point.x=gradient_vector->x2-gradient_vector->x1;
2025  point.y=gradient_vector->y2-gradient_vector->y1;
2026  length=sqrt(point.x*point.x+point.y*point.y);
2027  bounding_box=gradient->bounding_box;
2028  status=MagickTrue;
2029  exception=(&image->exception);
2030  GetMagickPixelPacket(image,&zero);
2031  image_view=AcquireAuthenticCacheView(image,exception);
2032  height=(size_t) (bounding_box.y+bounding_box.height);
2033 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2034  #pragma omp parallel for schedule(static) shared(status) \
2035  magick_number_threads(image,image,height,1)
2036 #endif
2037  for (y=bounding_box.y; y < (ssize_t) height; y++)
2038  {
2039  double
2040  alpha,
2041  offset;
2042 
2044  composite,
2045  pixel;
2046 
2047  IndexPacket
2048  *magick_restrict indexes;
2049 
2050  PixelPacket
2051  *magick_restrict q;
2052 
2053  ssize_t
2054  i,
2055  j,
2056  width,
2057  x;
2058 
2059  if (status == MagickFalse)
2060  continue;
2061  q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(size_t)
2062  bounding_box.width,1,exception);
2063  if (q == (PixelPacket *) NULL)
2064  {
2065  status=MagickFalse;
2066  continue;
2067  }
2068  indexes=GetCacheViewAuthenticIndexQueue(image_view);
2069  pixel=zero;
2070  composite=zero;
2071  offset=GetStopColorOffset(gradient,0,y);
2072  if (gradient->type != RadialGradient)
2073  offset*=MagickSafeReciprocal(length);
2074  width=(size_t) (bounding_box.x+bounding_box.width);
2075  for (x=bounding_box.x; x < (ssize_t) width; x++)
2076  {
2077  SetMagickPixelPacket(image,q,indexes+x,&pixel);
2078  switch (gradient->spread)
2079  {
2080  case UndefinedSpread:
2081  case PadSpread:
2082  {
2083  if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2084  (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2085  {
2086  offset=GetStopColorOffset(gradient,x,y);
2087  if (gradient->type != RadialGradient)
2088  offset*=MagickSafeReciprocal(length);
2089  }
2090  for (i=0; i < (ssize_t) gradient->number_stops; i++)
2091  if (offset < gradient->stops[i].offset)
2092  break;
2093  if ((offset < 0.0) || (i == 0))
2094  composite=gradient->stops[0].color;
2095  else
2096  if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2097  composite=gradient->stops[gradient->number_stops-1].color;
2098  else
2099  {
2100  j=i;
2101  i--;
2102  alpha=(offset-gradient->stops[i].offset)/
2103  (gradient->stops[j].offset-gradient->stops[i].offset);
2104  MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2105  &gradient->stops[j].color,alpha,&composite);
2106  }
2107  break;
2108  }
2109  case ReflectSpread:
2110  {
2111  if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2112  (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2113  {
2114  offset=GetStopColorOffset(gradient,x,y);
2115  if (gradient->type != RadialGradient)
2116  offset*=MagickSafeReciprocal(length);
2117  }
2118  if (offset < 0.0)
2119  offset=(-offset);
2120  if ((ssize_t) fmod(offset,2.0) == 0)
2121  offset=fmod(offset,1.0);
2122  else
2123  offset=1.0-fmod(offset,1.0);
2124  for (i=0; i < (ssize_t) gradient->number_stops; i++)
2125  if (offset < gradient->stops[i].offset)
2126  break;
2127  if (i == 0)
2128  composite=gradient->stops[0].color;
2129  else
2130  if (i == (ssize_t) gradient->number_stops)
2131  composite=gradient->stops[gradient->number_stops-1].color;
2132  else
2133  {
2134  j=i;
2135  i--;
2136  alpha=(offset-gradient->stops[i].offset)/
2137  (gradient->stops[j].offset-gradient->stops[i].offset);
2138  MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2139  &gradient->stops[j].color,alpha,&composite);
2140  }
2141  break;
2142  }
2143  case RepeatSpread:
2144  {
2145  double
2146  repeat;
2147 
2148  MagickBooleanType
2149  antialias;
2150 
2151  antialias=MagickFalse;
2152  repeat=0.0;
2153  if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2154  (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2155  {
2156  offset=GetStopColorOffset(gradient,x,y);
2157  if (gradient->type == LinearGradient)
2158  {
2159  repeat=fmod(offset,length);
2160  if (repeat < 0.0)
2161  repeat=length-fmod(-repeat,length);
2162  else
2163  repeat=fmod(offset,length);
2164  antialias=(repeat < length) && ((repeat+1.0) > length) ?
2165  MagickTrue : MagickFalse;
2166  offset=MagickSafeReciprocal(length)*repeat;
2167  }
2168  else
2169  {
2170  repeat=fmod(offset,(double) gradient->radius);
2171  if (repeat < 0.0)
2172  repeat=gradient->radius-fmod(-repeat,
2173  (double) gradient->radius);
2174  else
2175  repeat=fmod(offset,(double) gradient->radius);
2176  antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2177  MagickFalse;
2178  offset=repeat*MagickSafeReciprocal(gradient->radius);
2179  }
2180  }
2181  for (i=0; i < (ssize_t) gradient->number_stops; i++)
2182  if (offset < gradient->stops[i].offset)
2183  break;
2184  if (i == 0)
2185  composite=gradient->stops[0].color;
2186  else
2187  if (i == (ssize_t) gradient->number_stops)
2188  composite=gradient->stops[gradient->number_stops-1].color;
2189  else
2190  {
2191  j=i;
2192  i--;
2193  alpha=(offset-gradient->stops[i].offset)/
2194  (gradient->stops[j].offset-gradient->stops[i].offset);
2195  if (antialias != MagickFalse)
2196  {
2197  if (gradient->type == LinearGradient)
2198  alpha=length-repeat;
2199  else
2200  alpha=gradient->radius-repeat;
2201  i=0;
2202  j=(ssize_t) gradient->number_stops-1L;
2203  }
2204  MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2205  &gradient->stops[j].color,alpha,&composite);
2206  }
2207  break;
2208  }
2209  }
2210  MagickPixelCompositeOver(&composite,composite.opacity,&pixel,
2211  pixel.opacity,&pixel);
2212  SetPixelPacket(image,&pixel,q,indexes+x);
2213  q++;
2214  }
2215  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2216  status=MagickFalse;
2217  }
2218  image_view=DestroyCacheView(image_view);
2219  return(status);
2220 }
2221 
2222 /*
2223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2224 % %
2225 % %
2226 % %
2227 % D r a w I m a g e %
2228 % %
2229 % %
2230 % %
2231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2232 %
2233 % DrawImage() draws a graphic primitive on your image. The primitive
2234 % may be represented as a string or filename. Precede the filename with an
2235 % "at" sign (@) and the contents of the file are drawn on the image. You
2236 % can affect how text is drawn by setting one or more members of the draw
2237 % info structure.
2238 %
2239 % The format of the DrawImage method is:
2240 %
2241 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info)
2242 %
2243 % A description of each parameter follows:
2244 %
2245 % o image: the image.
2246 %
2247 % o draw_info: the draw info.
2248 %
2249 */
2250 
2251 static inline MagickBooleanType CheckPrimitiveExtent(MVGInfo *mvg_info,
2252  const double pad)
2253 {
2254  double
2255  proposed_extent;
2256 
2258  *primitive_info;
2259 
2260  size_t
2261  extent;
2262 
2263  ssize_t
2264  i;
2265 
2266  if ((mvg_info == (MVGInfo *) NULL) ||
2267  (mvg_info->primitive_info == (PrimitiveInfo **) NULL) ||
2268  (*mvg_info->primitive_info == (PrimitiveInfo *) NULL) ||
2269  (mvg_info->extent == (size_t *) NULL))
2270  return(MagickFalse);
2271  proposed_extent=mvg_info->offset+pad+PrimitiveExtentPad+1.0;
2272  if ((proposed_extent <= 0.0) || (proposed_extent > (double) MAGICK_SIZE_MAX))
2273  return(MagickFalse);
2274  extent=CastDoubleToSizeT(ceil(proposed_extent));
2275  if (extent <= *mvg_info->extent)
2276  return(MagickTrue);
2277  if (extent > (GetMaxMemoryRequest()/sizeof(PrimitiveInfo)))
2278  return(MagickFalse);
2279  primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(
2280  *mvg_info->primitive_info,extent,sizeof(PrimitiveInfo));
2281  if (primitive_info == (PrimitiveInfo *) NULL)
2282  {
2283  /*
2284  Create a stack to unwind; report failure.
2285  */
2286  extent=(size_t) PrimitiveExtentPad;
2287  primitive_info=(PrimitiveInfo *) AcquireCriticalMemory(extent*
2288  sizeof(*primitive_info));
2289  (void) memset(primitive_info,0,extent*sizeof(*primitive_info));
2290  *mvg_info->primitive_info=primitive_info;
2291  *mvg_info->extent=extent;
2292  mvg_info->offset=0;
2293  ThrowMagickException(mvg_info->exception,GetMagickModule(),
2294  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2295  return(MagickFalse);
2296  }
2297  /*
2298  Commit updated buffer.
2299  */
2300  for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2301  {
2302  primitive_info[i].primitive=UndefinedPrimitive;
2303  primitive_info[i].text=(char *) NULL;
2304  }
2305  *mvg_info->primitive_info=primitive_info;
2306  *mvg_info->extent=extent;
2307  return(MagickTrue);
2308 }
2309 
2310 static inline double GetDrawValue(const char *magick_restrict string,
2311  char **magick_restrict sentinel)
2312 {
2313  char
2314  **magick_restrict q;
2315 
2316  double
2317  value;
2318 
2319  q=sentinel;
2320  value=InterpretLocaleValue(string,q);
2321  sentinel=q;
2322  return(value);
2323 }
2324 
2325 static int MVGMacroCompare(const void *target,const void *source)
2326 {
2327  const char
2328  *p,
2329  *q;
2330 
2331  p=(const char *) target;
2332  q=(const char *) source;
2333  return(strcmp(p,q));
2334 }
2335 
2336 static SplayTreeInfo *GetMVGMacros(const char *primitive)
2337 {
2338  char
2339  *macro,
2340  *token;
2341 
2342  const char
2343  *q;
2344 
2345  size_t
2346  extent;
2347 
2349  *macros;
2350 
2351  /*
2352  Scan graphic primitives for definitions and classes.
2353  */
2354  if (primitive == (const char *) NULL)
2355  return((SplayTreeInfo *) NULL);
2356  macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2357  RelinquishMagickMemory);
2358  macro=AcquireString(primitive);
2359  token=AcquireString(primitive);
2360  extent=strlen(token)+MagickPathExtent;
2361  for (q=primitive; *q != '\0'; )
2362  {
2363  if (GetNextToken(q,&q,extent,token) < 1)
2364  break;
2365  if (*token == '\0')
2366  break;
2367  if (LocaleCompare("push",token) == 0)
2368  {
2369  const char
2370  *end,
2371  *start;
2372 
2373  (void) GetNextToken(q,&q,extent,token);
2374  if (*q == '"')
2375  {
2376  char
2377  name[MagickPathExtent];
2378 
2379  const char
2380  *p;
2381 
2382  ssize_t
2383  n;
2384 
2385  /*
2386  Named macro (e.g. push graphic-context "wheel").
2387  */
2388  (void) GetNextToken(q,&q,extent,token);
2389  start=q;
2390  end=q;
2391  (void) CopyMagickString(name,token,MagickPathExtent);
2392  n=1;
2393  for (p=q; *p != '\0'; )
2394  {
2395  if (GetNextToken(p,&p,extent,token) < 1)
2396  break;
2397  if (*token == '\0')
2398  break;
2399  if (LocaleCompare(token,"pop") == 0)
2400  {
2401  end=p-strlen(token)-1;
2402  n--;
2403  }
2404  if (LocaleCompare(token,"push") == 0)
2405  n++;
2406  if ((n == 0) && (end >= start))
2407  {
2408  size_t
2409  length=(size_t) (end-start);
2410 
2411  /*
2412  Extract macro.
2413  */
2414  (void) GetNextToken(p,&p,extent,token);
2415  if (length > 0)
2416  {
2417  (void) CopyMagickString(macro,start,length);
2418  (void) AddValueToSplayTree(macros,ConstantString(name),
2419  ConstantString(macro));
2420  }
2421  break;
2422  }
2423  }
2424  }
2425  }
2426  }
2427  token=DestroyString(token);
2428  macro=DestroyString(macro);
2429  return(macros);
2430 }
2431 
2432 static inline MagickBooleanType IsPoint(const char *point)
2433 {
2434  char
2435  *p;
2436 
2437  double
2438  value;
2439 
2440  value=GetDrawValue(point,&p);
2441  return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2442  MagickTrue);
2443 }
2444 
2445 static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2446  const PointInfo point)
2447 {
2448  primitive_info->point=point;
2449  primitive_info->coordinates=1;
2450  primitive_info->closed_subpath=MagickFalse;
2451  primitive_info->text=(char *) NULL;
2452  return(MagickTrue);
2453 }
2454 
2455 static MagickBooleanType RenderMVGContent(Image *image,
2456  const DrawInfo *draw_info,const size_t depth)
2457 {
2458 #define RenderImageTag "Render/Image"
2459 
2460  AffineMatrix
2461  affine,
2462  current;
2463 
2464  char
2465  key[2*MaxTextExtent],
2466  keyword[MaxTextExtent],
2467  geometry[MaxTextExtent],
2468  name[MaxTextExtent],
2469  *next_token,
2470  pattern[MaxTextExtent],
2471  *primitive,
2472  *token;
2473 
2474  const char
2475  *p,
2476  *q;
2477 
2478  double
2479  angle,
2480  coordinates,
2481  cursor,
2482  factor,
2483  primitive_extent;
2484 
2485  DrawInfo
2486  *clone_info,
2487  **graphic_context;
2488 
2489  MagickBooleanType
2490  proceed;
2491 
2492  MagickStatusType
2493  status;
2494 
2495  MVGInfo
2496  mvg_info;
2497 
2498  PointInfo
2499  point;
2500 
2501  PixelPacket
2502  start_color;
2503 
2505  *primitive_info;
2506 
2507  PrimitiveType
2508  primitive_type;
2509 
2510  SegmentInfo
2511  bounds;
2512 
2513  size_t
2514  extent,
2515  number_points;
2516 
2518  *macros;
2519 
2520  ssize_t
2521  classDepth = 0,
2522  defsDepth,
2523  i,
2524  j,
2525  k,
2526  n,
2527  symbolDepth,
2528  x;
2529 
2530  TypeMetric
2531  metrics;
2532 
2533  assert(image != (Image *) NULL);
2534  assert(image->signature == MagickCoreSignature);
2535  assert(draw_info != (DrawInfo *) NULL);
2536  assert(draw_info->signature == MagickCoreSignature);
2537  if (IsEventLogging() != MagickFalse)
2538  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2539  if (depth > MagickMaxRecursionDepth)
2540  ThrowBinaryImageException(DrawError,"VectorGraphicsNestedTooDeeply",
2541  image->filename);
2542  if ((draw_info->primitive == (char *) NULL) ||
2543  (*draw_info->primitive == '\0'))
2544  return(MagickFalse);
2545  if (draw_info->debug != MagickFalse)
2546  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
2547  if (SetImageStorageClass(image,DirectClass) == MagickFalse)
2548  return(MagickFalse);
2549  if (image->matte == MagickFalse)
2550  {
2551  status=SetImageAlphaChannel(image,OpaqueAlphaChannel);
2552  if (status == MagickFalse)
2553  return(MagickFalse);
2554  }
2555  primitive=(char *) NULL;
2556  if ((*draw_info->primitive == '@') && (strlen(draw_info->primitive) > 1) &&
2557  (*(draw_info->primitive+1) != '-') && (depth == 0))
2558  primitive=FileToString(draw_info->primitive,~0UL,&image->exception);
2559  else
2560  primitive=AcquireString(draw_info->primitive);
2561  if (primitive == (char *) NULL)
2562  return(MagickFalse);
2563  primitive_extent=(double) strlen(primitive);
2564  (void) SetImageArtifact(image,"mvg:vector-graphics",primitive);
2565  n=0;
2566  /*
2567  Allocate primitive info memory.
2568  */
2569  graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2570  if (graphic_context == (DrawInfo **) NULL)
2571  {
2572  primitive=DestroyString(primitive);
2573  ThrowBinaryImageException(ResourceLimitError,"MemoryAllocationFailed",
2574  image->filename);
2575  }
2576  number_points=(size_t) PrimitiveExtentPad;
2577  primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
2578  (number_points+1),sizeof(*primitive_info));
2579  if (primitive_info == (PrimitiveInfo *) NULL)
2580  {
2581  primitive=DestroyString(primitive);
2582  for ( ; n >= 0; n--)
2583  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2584  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2585  ThrowBinaryImageException(ResourceLimitError,"MemoryAllocationFailed",
2586  image->filename);
2587  }
2588  (void) memset(primitive_info,0,(size_t) (number_points+1)*
2589  sizeof(*primitive_info));
2590  (void) memset(&mvg_info,0,sizeof(mvg_info));
2591  mvg_info.primitive_info=(&primitive_info);
2592  mvg_info.extent=(&number_points);
2593  mvg_info.exception=(&image->exception);
2594  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2595  graphic_context[n]->viewbox=image->page;
2596  if ((image->page.width == 0) || (image->page.height == 0))
2597  {
2598  graphic_context[n]->viewbox.width=image->columns;
2599  graphic_context[n]->viewbox.height=image->rows;
2600  }
2601  token=AcquireString(primitive);
2602  extent=strlen(token)+MaxTextExtent;
2603  cursor=0.0;
2604  defsDepth=0;
2605  symbolDepth=0;
2606  macros=GetMVGMacros(primitive);
2607  status=QueryColorDatabase("#000000",&start_color,&image->exception);
2608  for (q=primitive; *q != '\0'; )
2609  {
2610  /*
2611  Interpret graphic primitive.
2612  */
2613  if (GetNextToken(q,&q,MaxTextExtent,keyword) < 1)
2614  break;
2615  if (*keyword == '\0')
2616  break;
2617  if (*keyword == '#')
2618  {
2619  /*
2620  Comment.
2621  */
2622  while ((*q != '\n') && (*q != '\0'))
2623  q++;
2624  continue;
2625  }
2626  p=q-strlen(keyword)-1;
2627  primitive_type=UndefinedPrimitive;
2628  current=graphic_context[n]->affine;
2629  GetAffineMatrix(&affine);
2630  *token='\0';
2631  switch (*keyword)
2632  {
2633  case ';':
2634  break;
2635  case 'a':
2636  case 'A':
2637  {
2638  if (LocaleCompare("affine",keyword) == 0)
2639  {
2640  (void) GetNextToken(q,&q,extent,token);
2641  affine.sx=GetDrawValue(token,&next_token);
2642  if (token == next_token)
2643  ThrowPointExpectedException(image,token);
2644  (void) GetNextToken(q,&q,extent,token);
2645  if (*token == ',')
2646  (void) GetNextToken(q,&q,extent,token);
2647  affine.ry=GetDrawValue(token,&next_token);
2648  if (token == next_token)
2649  ThrowPointExpectedException(image,token);
2650  (void) GetNextToken(q,&q,extent,token);
2651  if (*token == ',')
2652  (void) GetNextToken(q,&q,extent,token);
2653  affine.rx=GetDrawValue(token,&next_token);
2654  if (token == next_token)
2655  ThrowPointExpectedException(image,token);
2656  (void) GetNextToken(q,&q,extent,token);
2657  if (*token == ',')
2658  (void) GetNextToken(q,&q,extent,token);
2659  affine.sy=GetDrawValue(token,&next_token);
2660  if (token == next_token)
2661  ThrowPointExpectedException(image,token);
2662  (void) GetNextToken(q,&q,extent,token);
2663  if (*token == ',')
2664  (void) GetNextToken(q,&q,extent,token);
2665  affine.tx=GetDrawValue(token,&next_token);
2666  if (token == next_token)
2667  ThrowPointExpectedException(image,token);
2668  (void) GetNextToken(q,&q,extent,token);
2669  if (*token == ',')
2670  (void) GetNextToken(q,&q,extent,token);
2671  affine.ty=GetDrawValue(token,&next_token);
2672  if (token == next_token)
2673  ThrowPointExpectedException(image,token);
2674  break;
2675  }
2676  if (LocaleCompare("arc",keyword) == 0)
2677  {
2678  primitive_type=ArcPrimitive;
2679  break;
2680  }
2681  status=MagickFalse;
2682  break;
2683  }
2684  case 'b':
2685  case 'B':
2686  {
2687  if (LocaleCompare("bezier",keyword) == 0)
2688  {
2689  primitive_type=BezierPrimitive;
2690  break;
2691  }
2692  if (LocaleCompare("border-color",keyword) == 0)
2693  {
2694  (void) GetNextToken(q,&q,extent,token);
2695  status&=QueryColorDatabase(token,&graphic_context[n]->border_color,
2696  &image->exception);
2697  break;
2698  }
2699  status=MagickFalse;
2700  break;
2701  }
2702  case 'c':
2703  case 'C':
2704  {
2705  if (LocaleCompare("class",keyword) == 0)
2706  {
2707  const char
2708  *mvg_class;
2709 
2710  (void) GetNextToken(q,&q,extent,token);
2711  if ((*token == '\0') || (*token == ';'))
2712  {
2713  status=MagickFalse;
2714  break;
2715  }
2716  /*
2717  Identify recursion.
2718  */
2719  for (i=0; i <= n; i++)
2720  if (LocaleCompare(token,graphic_context[i]->id) == 0)
2721  break;
2722  if (i <= n)
2723  break;
2724  if (classDepth++ > MagickMaxRecursionDepth)
2725  {
2726  (void) ThrowMagickException(&image->exception,GetMagickModule(),
2727  DrawError,"VectorGraphicsNestedTooDeeply","`%s'",token);
2728  status=MagickFalse;
2729  break;
2730  }
2731  mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2732  if ((graphic_context[n]->render != MagickFalse) &&
2733  (mvg_class != (const char *) NULL) && (p > primitive))
2734  {
2735  char
2736  *elements;
2737 
2738  ssize_t
2739  offset;
2740 
2741  /*
2742  Inject class elements in stream.
2743  */
2744  (void) CloneString(&graphic_context[n]->id,token);
2745  offset=(ssize_t) (p-primitive);
2746  elements=AcquireString(primitive);
2747  elements[offset]='\0';
2748  (void) ConcatenateString(&elements,mvg_class);
2749  (void) ConcatenateString(&elements,"\n");
2750  (void) ConcatenateString(&elements,q);
2751  primitive=DestroyString(primitive);
2752  primitive=elements;
2753  q=primitive+offset;
2754  }
2755  break;
2756  }
2757  if (LocaleCompare("clip-path",keyword) == 0)
2758  {
2759  const char
2760  *clip_path;
2761 
2762  /*
2763  Take a node from within the MVG document, and duplicate it here.
2764  */
2765  (void) GetNextToken(q,&q,extent,token);
2766  if (*token == '\0')
2767  {
2768  status=MagickFalse;
2769  break;
2770  }
2771  (void) CloneString(&graphic_context[n]->clip_mask,token);
2772  clip_path=(const char *) GetValueFromSplayTree(macros,token);
2773  if (clip_path != (const char *) NULL)
2774  {
2775  if (graphic_context[n]->clipping_mask != (Image *) NULL)
2776  graphic_context[n]->clipping_mask=
2777  DestroyImage(graphic_context[n]->clipping_mask);
2778  graphic_context[n]->clipping_mask=DrawClippingMask(image,
2779  graphic_context[n],token,clip_path,&image->exception);
2780  if (graphic_context[n]->compliance != SVGCompliance)
2781  {
2782  const char
2783  *clip_path;
2784 
2785  clip_path=(const char *) GetValueFromSplayTree(macros,
2786  graphic_context[n]->clip_mask);
2787  if (clip_path != (const char *) NULL)
2788  (void) SetImageArtifact(image,
2789  graphic_context[n]->clip_mask,clip_path);
2790  status&=DrawClipPath(image,graphic_context[n],
2791  graphic_context[n]->clip_mask);
2792  }
2793  }
2794  break;
2795  }
2796  if (LocaleCompare("clip-rule",keyword) == 0)
2797  {
2798  ssize_t
2799  fill_rule;
2800 
2801  (void) GetNextToken(q,&q,extent,token);
2802  fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2803  token);
2804  if (fill_rule == -1)
2805  {
2806  status=MagickFalse;
2807  break;
2808  }
2809  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2810  break;
2811  }
2812  if (LocaleCompare("clip-units",keyword) == 0)
2813  {
2814  ssize_t
2815  clip_units;
2816 
2817  (void) GetNextToken(q,&q,extent,token);
2818  clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2819  token);
2820  if (clip_units == -1)
2821  {
2822  status=MagickFalse;
2823  break;
2824  }
2825  graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2826  if (clip_units == ObjectBoundingBox)
2827  {
2828  GetAffineMatrix(&current);
2829  affine.sx=draw_info->bounds.x2;
2830  affine.sy=draw_info->bounds.y2;
2831  affine.tx=draw_info->bounds.x1;
2832  affine.ty=draw_info->bounds.y1;
2833  break;
2834  }
2835  break;
2836  }
2837  if (LocaleCompare("circle",keyword) == 0)
2838  {
2839  primitive_type=CirclePrimitive;
2840  break;
2841  }
2842  if (LocaleCompare("color",keyword) == 0)
2843  {
2844  primitive_type=ColorPrimitive;
2845  break;
2846  }
2847  if (LocaleCompare("compliance",keyword) == 0)
2848  {
2849  /*
2850  MVG compliance associates a clipping mask with an image; SVG
2851  compliance associates a clipping mask with a graphics context.
2852  */
2853  (void) GetNextToken(q,&q,extent,token);
2854  graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2855  MagickComplianceOptions,MagickFalse,token);
2856  break;
2857  }
2858  if (LocaleCompare("currentColor",keyword) == 0)
2859  {
2860  (void) GetNextToken(q,&q,extent,token);
2861  break;
2862  }
2863  status=MagickFalse;
2864  break;
2865  }
2866  case 'd':
2867  case 'D':
2868  {
2869  if (LocaleCompare("decorate",keyword) == 0)
2870  {
2871  ssize_t
2872  decorate;
2873 
2874  (void) GetNextToken(q,&q,extent,token);
2875  decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2876  token);
2877  if (decorate == -1)
2878  {
2879  status=MagickFalse;
2880  break;
2881  }
2882  graphic_context[n]->decorate=(DecorationType) decorate;
2883  break;
2884  }
2885  if (LocaleCompare("density",keyword) == 0)
2886  {
2887  (void) GetNextToken(q,&q,extent,token);
2888  (void) CloneString(&graphic_context[n]->density,token);
2889  break;
2890  }
2891  if (LocaleCompare("direction",keyword) == 0)
2892  {
2893  ssize_t
2894  direction;
2895 
2896  (void) GetNextToken(q,&q,extent,token);
2897  direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2898  token);
2899  if (direction == -1)
2900  status=MagickFalse;
2901  else
2902  graphic_context[n]->direction=(DirectionType) direction;
2903  break;
2904  }
2905  status=MagickFalse;
2906  break;
2907  }
2908  case 'e':
2909  case 'E':
2910  {
2911  if (LocaleCompare("ellipse",keyword) == 0)
2912  {
2913  primitive_type=EllipsePrimitive;
2914  break;
2915  }
2916  if (LocaleCompare("encoding",keyword) == 0)
2917  {
2918  (void) GetNextToken(q,&q,extent,token);
2919  (void) CloneString(&graphic_context[n]->encoding,token);
2920  break;
2921  }
2922  status=MagickFalse;
2923  break;
2924  }
2925  case 'f':
2926  case 'F':
2927  {
2928  if (LocaleCompare("fill",keyword) == 0)
2929  {
2930  const char
2931  *mvg_class;
2932 
2933  (void) GetNextToken(q,&q,extent,token);
2934  if (graphic_context[n]->clip_path != MagickFalse)
2935  break;
2936  mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2937  if (mvg_class != (const char *) NULL)
2938  {
2939  (void) DrawPatternPath(image,draw_info,mvg_class,
2940  &graphic_context[n]->fill_pattern);
2941  break;
2942  }
2943  (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
2944  if (GetImageArtifact(image,pattern) != (const char *) NULL)
2945  {
2946  (void) DrawPatternPath(image,draw_info,token,
2947  &graphic_context[n]->fill_pattern);
2948  break;
2949  }
2950  status&=QueryColorDatabase(token,&graphic_context[n]->fill,
2951  &image->exception);
2952  if (graphic_context[n]->fill_opacity != (double) OpaqueOpacity)
2953  graphic_context[n]->fill.opacity=ClampToQuantum(
2954  graphic_context[n]->fill_opacity);
2955  break;
2956  }
2957  if (LocaleCompare("fill-opacity",keyword) == 0)
2958  {
2959  double
2960  opacity;
2961 
2962  (void) GetNextToken(q,&q,extent,token);
2963  if (graphic_context[n]->clip_path != MagickFalse)
2964  break;
2965  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2966  opacity=MagickMin(MagickMax(factor*
2967  GetDrawValue(token,&next_token),0.0),1.0);
2968  if (token == next_token)
2969  ThrowPointExpectedException(image,token);
2970  if (graphic_context[n]->compliance == SVGCompliance)
2971  graphic_context[n]->fill_opacity*=(1.0-opacity);
2972  else
2973  graphic_context[n]->fill_opacity=((MagickRealType) QuantumRange-
2974  graphic_context[n]->fill_opacity)*(1.0-opacity);
2975  if (graphic_context[n]->fill.opacity != TransparentOpacity)
2976  graphic_context[n]->fill.opacity=(Quantum)
2977  graphic_context[n]->fill_opacity;
2978  else
2979  graphic_context[n]->fill.opacity=ClampToQuantum((MagickRealType)
2980  QuantumRange*(1.0-opacity));
2981  break;
2982  }
2983  if (LocaleCompare("fill-rule",keyword) == 0)
2984  {
2985  ssize_t
2986  fill_rule;
2987 
2988  (void) GetNextToken(q,&q,extent,token);
2989  fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2990  token);
2991  if (fill_rule == -1)
2992  {
2993  status=MagickFalse;
2994  break;
2995  }
2996  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2997  break;
2998  }
2999  if (LocaleCompare("font",keyword) == 0)
3000  {
3001  (void) GetNextToken(q,&q,extent,token);
3002  (void) CloneString(&graphic_context[n]->font,token);
3003  if (LocaleCompare("none",token) == 0)
3004  graphic_context[n]->font=(char *) RelinquishMagickMemory(
3005  graphic_context[n]->font);
3006  break;
3007  }
3008  if (LocaleCompare("font-family",keyword) == 0)
3009  {
3010  (void) GetNextToken(q,&q,extent,token);
3011  (void) CloneString(&graphic_context[n]->family,token);
3012  break;
3013  }
3014  if (LocaleCompare("font-size",keyword) == 0)
3015  {
3016  (void) GetNextToken(q,&q,extent,token);
3017  graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3018  if (token == next_token)
3019  ThrowPointExpectedException(image,token);
3020  break;
3021  }
3022  if (LocaleCompare("font-stretch",keyword) == 0)
3023  {
3024  ssize_t
3025  stretch;
3026 
3027  (void) GetNextToken(q,&q,extent,token);
3028  stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3029  if (stretch == -1)
3030  {
3031  status=MagickFalse;
3032  break;
3033  }
3034  graphic_context[n]->stretch=(StretchType) stretch;
3035  break;
3036  }
3037  if (LocaleCompare("font-style",keyword) == 0)
3038  {
3039  ssize_t
3040  style;
3041 
3042  (void) GetNextToken(q,&q,extent,token);
3043  style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3044  if (style == -1)
3045  {
3046  status=MagickFalse;
3047  break;
3048  }
3049  graphic_context[n]->style=(StyleType) style;
3050  break;
3051  }
3052  if (LocaleCompare("font-weight",keyword) == 0)
3053  {
3054  ssize_t
3055  weight;
3056 
3057  (void) GetNextToken(q,&q,extent,token);
3058  weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3059  if (weight == -1)
3060  weight=(ssize_t) StringToUnsignedLong(token);
3061  graphic_context[n]->weight=(size_t) weight;
3062  break;
3063  }
3064  status=MagickFalse;
3065  break;
3066  }
3067  case 'g':
3068  case 'G':
3069  {
3070  if (LocaleCompare("gradient-units",keyword) == 0)
3071  {
3072  (void) GetNextToken(q,&q,extent,token);
3073  break;
3074  }
3075  if (LocaleCompare("gravity",keyword) == 0)
3076  {
3077  ssize_t
3078  gravity;
3079 
3080  (void) GetNextToken(q,&q,extent,token);
3081  gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3082  if (gravity == -1)
3083  {
3084  status=MagickFalse;
3085  break;
3086  }
3087  graphic_context[n]->gravity=(GravityType) gravity;
3088  break;
3089  }
3090  status=MagickFalse;
3091  break;
3092  }
3093  case 'i':
3094  case 'I':
3095  {
3096  if (LocaleCompare("image",keyword) == 0)
3097  {
3098  ssize_t
3099  compose;
3100 
3101  primitive_type=ImagePrimitive;
3102  (void) GetNextToken(q,&q,extent,token);
3103  compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3104  if (compose == -1)
3105  {
3106  status=MagickFalse;
3107  break;
3108  }
3109  graphic_context[n]->compose=(CompositeOperator) compose;
3110  break;
3111  }
3112  if (LocaleCompare("interline-spacing",keyword) == 0)
3113  {
3114  (void) GetNextToken(q,&q,extent,token);
3115  graphic_context[n]->interline_spacing=GetDrawValue(token,
3116  &next_token);
3117  if (token == next_token)
3118  ThrowPointExpectedException(image,token);
3119  break;
3120  }
3121  if (LocaleCompare("interword-spacing",keyword) == 0)
3122  {
3123  (void) GetNextToken(q,&q,extent,token);
3124  graphic_context[n]->interword_spacing=GetDrawValue(token,
3125  &next_token);
3126  if (token == next_token)
3127  ThrowPointExpectedException(image,token);
3128  break;
3129  }
3130  status=MagickFalse;
3131  break;
3132  }
3133  case 'k':
3134  case 'K':
3135  {
3136  if (LocaleCompare("kerning",keyword) == 0)
3137  {
3138  (void) GetNextToken(q,&q,extent,token);
3139  graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3140  if (token == next_token)
3141  ThrowPointExpectedException(image,token);
3142  break;
3143  }
3144  status=MagickFalse;
3145  break;
3146  }
3147  case 'l':
3148  case 'L':
3149  {
3150  if (LocaleCompare("letter-spacing",keyword) == 0)
3151  {
3152  (void) GetNextToken(q,&q,extent,token);
3153  if (IsPoint(token) == MagickFalse)
3154  break;
3155  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3156  clone_info->text=AcquireString(" ");
3157  status&=GetTypeMetrics(image,clone_info,&metrics);
3158  graphic_context[n]->kerning=metrics.width*
3159  GetDrawValue(token,&next_token);
3160  clone_info=DestroyDrawInfo(clone_info);
3161  if (token == next_token)
3162  ThrowPointExpectedException(image,token);
3163  break;
3164  }
3165  if (LocaleCompare("line",keyword) == 0)
3166  {
3167  primitive_type=LinePrimitive;
3168  break;
3169  }
3170  status=MagickFalse;
3171  break;
3172  }
3173  case 'm':
3174  case 'M':
3175  {
3176  if (LocaleCompare("mask",keyword) == 0)
3177  {
3178  const char
3179  *mask_path;
3180 
3181  /*
3182  Take a node from within the MVG document, and duplicate it here.
3183  */
3184  (void) GetNextToken(q,&q,extent,token);
3185  mask_path=(const char *) GetValueFromSplayTree(macros,token);
3186  if (mask_path != (const char *) NULL)
3187  {
3188  if (graphic_context[n]->composite_mask != (Image *) NULL)
3189  graphic_context[n]->composite_mask=
3190  DestroyImage(graphic_context[n]->composite_mask);
3191  graphic_context[n]->composite_mask=DrawCompositeMask(image,
3192  graphic_context[n],token,mask_path,&image->exception);
3193  if (graphic_context[n]->compliance != SVGCompliance)
3194  status=SetImageMask(image,graphic_context[n]->composite_mask);
3195  }
3196  break;
3197  }
3198  if (LocaleCompare("matte",keyword) == 0)
3199  {
3200  primitive_type=MattePrimitive;
3201  break;
3202  }
3203  status=MagickFalse;
3204  break;
3205  }
3206  case 'o':
3207  case 'O':
3208  {
3209  if (LocaleCompare("offset",keyword) == 0)
3210  {
3211  (void) GetNextToken(q,&q,extent,token);
3212  break;
3213  }
3214  if (LocaleCompare("opacity",keyword) == 0)
3215  {
3216  double
3217  opacity;
3218 
3219  (void) GetNextToken(q,&q,extent,token);
3220  if (graphic_context[n]->clip_path != MagickFalse)
3221  break;
3222  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3223  opacity=1.0-MagickMin(MagickMax(factor*
3224  GetDrawValue(token,&next_token),0.0),1.0);
3225  if (token == next_token)
3226  ThrowPointExpectedException(image,token);
3227  if (graphic_context[n]->compliance == SVGCompliance)
3228  {
3229  graphic_context[n]->fill_opacity*=opacity;
3230  graphic_context[n]->stroke_opacity*=opacity;
3231  }
3232  else
3233  {
3234  graphic_context[n]->fill_opacity=(double) QuantumRange*opacity;
3235  graphic_context[n]->stroke_opacity=(double) QuantumRange*
3236  opacity;
3237  }
3238  if (graphic_context[n]->fill.opacity != (double) TransparentOpacity)
3239  {
3240  graphic_context[n]->fill.opacity=
3241  graphic_context[n]->fill_opacity;
3242  graphic_context[n]->stroke.opacity=
3243  graphic_context[n]->stroke_opacity;
3244  }
3245  else
3246  {
3247  graphic_context[n]->fill.opacity=(MagickRealType)
3248  ClampToQuantum((double) QuantumRange*opacity);
3249  graphic_context[n]->stroke.opacity=(MagickRealType)
3250  ClampToQuantum((double) QuantumRange*opacity);
3251  }
3252  break;
3253  }
3254  status=MagickFalse;
3255  break;
3256  }
3257  case 'p':
3258  case 'P':
3259  {
3260  if (LocaleCompare("path",keyword) == 0)
3261  {
3262  primitive_type=PathPrimitive;
3263  break;
3264  }
3265  if (LocaleCompare("point",keyword) == 0)
3266  {
3267  primitive_type=PointPrimitive;
3268  break;
3269  }
3270  if (LocaleCompare("polyline",keyword) == 0)
3271  {
3272  primitive_type=PolylinePrimitive;
3273  break;
3274  }
3275  if (LocaleCompare("polygon",keyword) == 0)
3276  {
3277  primitive_type=PolygonPrimitive;
3278  break;
3279  }
3280  if (LocaleCompare("pop",keyword) == 0)
3281  {
3282  if (GetNextToken(q,&q,extent,token) < 1)
3283  break;
3284  if (LocaleCompare("class",token) == 0)
3285  break;
3286  if (LocaleCompare("clip-path",token) == 0)
3287  break;
3288  if (LocaleCompare("defs",token) == 0)
3289  {
3290  defsDepth--;
3291  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3292  MagickTrue;
3293  break;
3294  }
3295  if (LocaleCompare("gradient",token) == 0)
3296  break;
3297  if (LocaleCompare("graphic-context",token) == 0)
3298  {
3299  if (n <= 0)
3300  {
3301  (void) ThrowMagickException(&image->exception,
3302  GetMagickModule(),DrawError,
3303  "UnbalancedGraphicContextPushPop","`%s'",token);
3304  status=MagickFalse;
3305  n=0;
3306  break;
3307  }
3308  if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3309  (graphic_context[n]->compliance != SVGCompliance))
3310  if (LocaleCompare(graphic_context[n]->clip_mask,
3311  graphic_context[n-1]->clip_mask) != 0)
3312  status=SetImageClipMask(image,(Image *) NULL);
3313  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3314  n--;
3315  break;
3316  }
3317  if (LocaleCompare("mask",token) == 0)
3318  break;
3319  if (LocaleCompare("pattern",token) == 0)
3320  break;
3321  if (LocaleCompare("symbol",token) == 0)
3322  {
3323  symbolDepth--;
3324  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3325  MagickTrue;
3326  break;
3327  }
3328  status=MagickFalse;
3329  break;
3330  }
3331  if (LocaleCompare("push",keyword) == 0)
3332  {
3333  if (GetNextToken(q,&q,extent,token) < 1)
3334  break;
3335  if (LocaleCompare("class",token) == 0)
3336  {
3337  /*
3338  Class context.
3339  */
3340  for (p=q; *q != '\0'; )
3341  {
3342  if (GetNextToken(q,&q,extent,token) < 1)
3343  break;
3344  if (LocaleCompare(token,"pop") != 0)
3345  continue;
3346  (void) GetNextToken(q,(const char **) NULL,extent,token);
3347  if (LocaleCompare(token,"class") != 0)
3348  continue;
3349  break;
3350  }
3351  (void) GetNextToken(q,&q,extent,token);
3352  break;
3353  }
3354  if (LocaleCompare("clip-path",token) == 0)
3355  {
3356  (void) GetNextToken(q,&q,extent,token);
3357  for (p=q; *q != '\0'; )
3358  {
3359  if (GetNextToken(q,&q,extent,token) < 1)
3360  break;
3361  if (LocaleCompare(token,"pop") != 0)
3362  continue;
3363  (void) GetNextToken(q,(const char **) NULL,extent,token);
3364  if (LocaleCompare(token,"clip-path") != 0)
3365  continue;
3366  break;
3367  }
3368  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3369  {
3370  status=MagickFalse;
3371  break;
3372  }
3373  (void) GetNextToken(q,&q,extent,token);
3374  break;
3375  }
3376  if (LocaleCompare("defs",token) == 0)
3377  {
3378  defsDepth++;
3379  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3380  MagickTrue;
3381  break;
3382  }
3383  if (LocaleCompare("gradient",token) == 0)
3384  {
3385  char
3386  key[2*MaxTextExtent],
3387  name[MaxTextExtent],
3388  type[MaxTextExtent];
3389 
3390  SegmentInfo
3391  segment;
3392 
3393  (void) GetNextToken(q,&q,extent,token);
3394  (void) CopyMagickString(name,token,MaxTextExtent);
3395  (void) GetNextToken(q,&q,extent,token);
3396  (void) CopyMagickString(type,token,MaxTextExtent);
3397  (void) GetNextToken(q,&q,extent,token);
3398  segment.x1=GetDrawValue(token,&next_token);
3399  if (token == next_token)
3400  ThrowPointExpectedException(image,token);
3401  (void) GetNextToken(q,&q,extent,token);
3402  if (*token == ',')
3403  (void) GetNextToken(q,&q,extent,token);
3404  segment.y1=GetDrawValue(token,&next_token);
3405  if (token == next_token)
3406  ThrowPointExpectedException(image,token);
3407  (void) GetNextToken(q,&q,extent,token);
3408  if (*token == ',')
3409  (void) GetNextToken(q,&q,extent,token);
3410  segment.x2=GetDrawValue(token,&next_token);
3411  if (token == next_token)
3412  ThrowPointExpectedException(image,token);
3413  (void) GetNextToken(q,&q,extent,token);
3414  if (*token == ',')
3415  (void) GetNextToken(q,&q,extent,token);
3416  segment.y2=GetDrawValue(token,&next_token);
3417  if (token == next_token)
3418  ThrowPointExpectedException(image,token);
3419  if (LocaleCompare(type,"radial") == 0)
3420  {
3421  (void) GetNextToken(q,&q,extent,token);
3422  if (*token == ',')
3423  (void) GetNextToken(q,&q,extent,token);
3424  }
3425  for (p=q; *q != '\0'; )
3426  {
3427  if (GetNextToken(q,&q,extent,token) < 1)
3428  break;
3429  if (LocaleCompare(token,"pop") != 0)
3430  continue;
3431  (void) GetNextToken(q,(const char **) NULL,extent,token);
3432  if (LocaleCompare(token,"gradient") != 0)
3433  continue;
3434  break;
3435  }
3436  if ((q == (char *) NULL) || (*q == '\0') ||
3437  (p == (char *) NULL) || ((q-4) < p) ||
3438  ((q-p+4+1) > extent))
3439  {
3440  status=MagickFalse;
3441  break;
3442  }
3443  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3444  bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3445  graphic_context[n]->affine.ry*segment.y1+
3446  graphic_context[n]->affine.tx;
3447  bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3448  graphic_context[n]->affine.sy*segment.y1+
3449  graphic_context[n]->affine.ty;
3450  bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3451  graphic_context[n]->affine.ry*segment.y2+
3452  graphic_context[n]->affine.tx;
3453  bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3454  graphic_context[n]->affine.sy*segment.y2+
3455  graphic_context[n]->affine.ty;
3456  (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
3457  (void) SetImageArtifact(image,key,token);
3458  (void) FormatLocaleString(key,MaxTextExtent,"%s-type",name);
3459  (void) SetImageArtifact(image,key,type);
3460  (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
3461  (void) FormatLocaleString(geometry,MaxTextExtent,
3462  "%gx%g%+.15g%+.15g",
3463  MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3464  MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3465  bounds.x1,bounds.y1);
3466  (void) SetImageArtifact(image,key,geometry);
3467  (void) GetNextToken(q,&q,extent,token);
3468  break;
3469  }
3470  if (LocaleCompare("graphic-context",token) == 0)
3471  {
3472  n++;
3473  graphic_context=(DrawInfo **) ResizeQuantumMemory(
3474  graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3475  if (graphic_context == (DrawInfo **) NULL)
3476  {
3477  (void) ThrowMagickException(&image->exception,
3478  GetMagickModule(),ResourceLimitError,
3479  "MemoryAllocationFailed","`%s'",image->filename);
3480  status=MagickFalse;
3481  break;
3482  }
3483  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3484  graphic_context[n-1]);
3485  if (*q == '"')
3486  {
3487  (void) GetNextToken(q,&q,extent,token);
3488  (void) CloneString(&graphic_context[n]->id,token);
3489  }
3490  if (n > MagickMaxRecursionDepth)
3491  {
3492  (void) ThrowMagickException(&image->exception,
3493  GetMagickModule(),DrawError,
3494  "VectorGraphicsNestedTooDeeply","`%s'",image->filename);
3495  status=MagickFalse;
3496  }
3497  break;
3498  }
3499  if (LocaleCompare("mask",token) == 0)
3500  {
3501  (void) GetNextToken(q,&q,extent,token);
3502  break;
3503  }
3504  if (LocaleCompare("pattern",token) == 0)
3505  {
3507  bounds;
3508 
3509  (void) GetNextToken(q,&q,extent,token);
3510  (void) CopyMagickString(name,token,MaxTextExtent);
3511  (void) GetNextToken(q,&q,extent,token);
3512  bounds.x=CastDoubleToLong(ceil(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.y=CastDoubleToLong(ceil(GetDrawValue(token,
3520  &next_token)-0.5));
3521  if (token == next_token)
3522  ThrowPointExpectedException(image,token);
3523  (void) GetNextToken(q,&q,extent,token);
3524  if (*token == ',')
3525  (void) GetNextToken(q,&q,extent,token);
3526  bounds.width=CastDoubleToUnsigned(GetDrawValue(token,
3527  &next_token)+0.5);
3528  if (token == next_token)
3529  ThrowPointExpectedException(image,token);
3530  (void) GetNextToken(q,&q,extent,token);
3531  if (*token == ',')
3532  (void) GetNextToken(q,&q,extent,token);
3533  bounds.height=CastDoubleToUnsigned(GetDrawValue(token,
3534  &next_token)+0.5);
3535  if (token == next_token)
3536  ThrowPointExpectedException(image,token);
3537  for (p=q; *q != '\0'; )
3538  {
3539  if (GetNextToken(q,&q,extent,token) < 1)
3540  break;
3541  if (LocaleCompare(token,"pop") != 0)
3542  continue;
3543  (void) GetNextToken(q,(const char **) NULL,extent,token);
3544  if (LocaleCompare(token,"pattern") != 0)
3545  continue;
3546  break;
3547  }
3548  if ((q == (char *) NULL) || (p == (char *) NULL) ||
3549  ((q-4) < p) || ((size_t) (q-p+4+1) > extent))
3550  {
3551  status=MagickFalse;
3552  break;
3553  }
3554  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3555  (void) FormatLocaleString(key,MaxTextExtent,"%s",name);
3556  (void) SetImageArtifact(image,key,token);
3557  (void) FormatLocaleString(key,MaxTextExtent,"%s-geometry",name);
3558  (void) FormatLocaleString(geometry,MaxTextExtent,
3559  "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
3560  bounds.height,(double) bounds.x,(double) bounds.y);
3561  (void) SetImageArtifact(image,key,geometry);
3562  (void) GetNextToken(q,&q,extent,token);
3563  break;
3564  }
3565  if (LocaleCompare("symbol",token) == 0)
3566  {
3567  symbolDepth++;
3568  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3569  MagickTrue;
3570  break;
3571  }
3572  status=MagickFalse;
3573  break;
3574  }
3575  status=MagickFalse;
3576  break;
3577  }
3578  case 'r':
3579  case 'R':
3580  {
3581  if (LocaleCompare("rectangle",keyword) == 0)
3582  {
3583  primitive_type=RectanglePrimitive;
3584  break;
3585  }
3586  if (LocaleCompare("rotate",keyword) == 0)
3587  {
3588  (void) GetNextToken(q,&q,extent,token);
3589  angle=GetDrawValue(token,&next_token);
3590  if (token == next_token)
3591  ThrowPointExpectedException(image,token);
3592  affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3593  affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3594  affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3595  affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3596  break;
3597  }
3598  if (LocaleCompare("roundRectangle",keyword) == 0)
3599  {
3600  primitive_type=RoundRectanglePrimitive;
3601  break;
3602  }
3603  status=MagickFalse;
3604  break;
3605  }
3606  case 's':
3607  case 'S':
3608  {
3609  if (LocaleCompare("scale",keyword) == 0)
3610  {
3611  (void) GetNextToken(q,&q,extent,token);
3612  affine.sx=GetDrawValue(token,&next_token);
3613  if (token == next_token)
3614  ThrowPointExpectedException(image,token);
3615  (void) GetNextToken(q,&q,extent,token);
3616  if (*token == ',')
3617  (void) GetNextToken(q,&q,extent,token);
3618  affine.sy=GetDrawValue(token,&next_token);
3619  if (token == next_token)
3620  ThrowPointExpectedException(image,token);
3621  break;
3622  }
3623  if (LocaleCompare("skewX",keyword) == 0)
3624  {
3625  (void) GetNextToken(q,&q,extent,token);
3626  angle=GetDrawValue(token,&next_token);
3627  if (token == next_token)
3628  ThrowPointExpectedException(image,token);
3629  affine.ry=sin(DegreesToRadians(angle));
3630  break;
3631  }
3632  if (LocaleCompare("skewY",keyword) == 0)
3633  {
3634  (void) GetNextToken(q,&q,extent,token);
3635  angle=GetDrawValue(token,&next_token);
3636  if (token == next_token)
3637  ThrowPointExpectedException(image,token);
3638  affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3639  break;
3640  }
3641  if (LocaleCompare("stop-color",keyword) == 0)
3642  {
3643  GradientType
3644  type;
3645 
3646  PixelPacket
3647  stop_color;
3648 
3649  (void) GetNextToken(q,&q,extent,token);
3650  status&=QueryColorDatabase(token,&stop_color,&image->exception);
3651  type=LinearGradient;
3652  if (draw_info->gradient.type == RadialGradient)
3653  type=RadialGradient;
3654  (void) GradientImage(image,type,PadSpread,&start_color,&stop_color);
3655  start_color=stop_color;
3656  (void) GetNextToken(q,&q,extent,token);
3657  break;
3658  }
3659  if (LocaleCompare("stroke",keyword) == 0)
3660  {
3661  const char
3662  *mvg_class;
3663 
3664  (void) GetNextToken(q,&q,extent,token);
3665  if (graphic_context[n]->clip_path != MagickFalse)
3666  break;
3667  mvg_class=(const char *) GetValueFromSplayTree(macros,token);
3668  if (mvg_class != (const char *) NULL)
3669  {
3670  (void) DrawPatternPath(image,draw_info,mvg_class,
3671  &graphic_context[n]->stroke_pattern);
3672  break;
3673  }
3674  (void) FormatLocaleString(pattern,MaxTextExtent,"%s",token);
3675  if (GetImageArtifact(image,pattern) != (const char *) NULL)
3676  {
3677  (void) DrawPatternPath(image,draw_info,token,
3678  &graphic_context[n]->stroke_pattern);
3679  break;
3680  }
3681  status&=QueryColorDatabase(token,&graphic_context[n]->stroke,
3682  &image->exception);
3683  if (graphic_context[n]->stroke_opacity != (MagickRealType) OpaqueOpacity)
3684  graphic_context[n]->stroke.opacity=ClampToQuantum(
3685  graphic_context[n]->stroke_opacity);
3686  break;
3687  }
3688  if (LocaleCompare("stroke-antialias",keyword) == 0)
3689  {
3690  (void) GetNextToken(q,&q,extent,token);
3691  graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3692  MagickTrue : MagickFalse;
3693  break;
3694  }
3695  if (LocaleCompare("stroke-dasharray",keyword) == 0)
3696  {
3697  if (graphic_context[n]->dash_pattern != (double *) NULL)
3698  graphic_context[n]->dash_pattern=(double *)
3699  RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3700  if (IsPoint(q) != MagickFalse)
3701  {
3702  const char
3703  *p;
3704 
3705  p=q;
3706  (void) GetNextToken(p,&p,extent,token);
3707  if (*token == ',')
3708  (void) GetNextToken(p,&p,extent,token);
3709  for (x=0; IsPoint(token) != MagickFalse; x++)
3710  {
3711  (void) GetNextToken(p,&p,extent,token);
3712  if (*token == ',')
3713  (void) GetNextToken(p,&p,extent,token);
3714  }
3715  graphic_context[n]->dash_pattern=(double *)
3716  AcquireQuantumMemory((size_t) (2*x+2),
3717  sizeof(*graphic_context[n]->dash_pattern));
3718  if (graphic_context[n]->dash_pattern == (double *) NULL)
3719  {
3720  (void) ThrowMagickException(&image->exception,
3721  GetMagickModule(),ResourceLimitError,
3722  "MemoryAllocationFailed","`%s'",image->filename);
3723  status=MagickFalse;
3724  break;
3725  }
3726  (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3727  (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3728  for (j=0; j < x; j++)
3729  {
3730  (void) GetNextToken(q,&q,extent,token);
3731  if (*token == ',')
3732  (void) GetNextToken(q,&q,extent,token);
3733  graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3734  &next_token);
3735  if (token == next_token)
3736  ThrowPointExpectedException(image,token);
3737  if (graphic_context[n]->dash_pattern[j] <= 0.0)
3738  status=MagickFalse;
3739  }
3740  if ((x & 0x01) != 0)
3741  for ( ; j < (2*x); j++)
3742  graphic_context[n]->dash_pattern[j]=
3743  graphic_context[n]->dash_pattern[j-x];
3744  graphic_context[n]->dash_pattern[j]=0.0;
3745  break;
3746  }
3747  (void) GetNextToken(q,&q,extent,token);
3748  break;
3749  }
3750  if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3751  {
3752  (void) GetNextToken(q,&q,extent,token);
3753  graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3754  if (token == next_token)
3755  ThrowPointExpectedException(image,token);
3756  break;
3757  }
3758  if (LocaleCompare("stroke-linecap",keyword) == 0)
3759  {
3760  ssize_t
3761  linecap;
3762 
3763  (void) GetNextToken(q,&q,extent,token);
3764  linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3765  if (linecap == -1)
3766  {
3767  status=MagickFalse;
3768  break;
3769  }
3770  graphic_context[n]->linecap=(LineCap) linecap;
3771  break;
3772  }
3773  if (LocaleCompare("stroke-linejoin",keyword) == 0)
3774  {
3775  ssize_t
3776  linejoin;
3777 
3778  (void) GetNextToken(q,&q,extent,token);
3779  linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3780  token);
3781  if (linejoin == -1)
3782  {
3783  status=MagickFalse;
3784  break;
3785  }
3786  graphic_context[n]->linejoin=(LineJoin) linejoin;
3787  break;
3788  }
3789  if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3790  {
3791  (void) GetNextToken(q,&q,extent,token);
3792  graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3793  break;
3794  }
3795  if (LocaleCompare("stroke-opacity",keyword) == 0)
3796  {
3797  double
3798  opacity;
3799 
3800  (void) GetNextToken(q,&q,extent,token);
3801  if (graphic_context[n]->clip_path != MagickFalse)
3802  break;
3803  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3804  opacity=MagickMin(MagickMax(factor*
3805  GetDrawValue(token,&next_token),0.0),1.0);
3806  if (token == next_token)
3807  ThrowPointExpectedException(image,token);
3808  if (graphic_context[n]->compliance == SVGCompliance)
3809  graphic_context[n]->stroke_opacity*=(1.0-opacity);
3810  else
3811  graphic_context[n]->stroke_opacity=((MagickRealType) QuantumRange-
3812  graphic_context[n]->stroke_opacity)*(1.0-opacity);
3813  if (graphic_context[n]->stroke.opacity != TransparentOpacity)
3814  graphic_context[n]->stroke.opacity=(Quantum)
3815  graphic_context[n]->stroke_opacity;
3816  else
3817  graphic_context[n]->stroke.opacity=ClampToQuantum(
3818  (MagickRealType) QuantumRange*opacity);
3819  break;
3820  }
3821  if (LocaleCompare("stroke-width",keyword) == 0)
3822  {
3823  (void) GetNextToken(q,&q,extent,token);
3824  if (graphic_context[n]->clip_path != MagickFalse)
3825  break;
3826  graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3827  if ((token == next_token) ||
3828  (graphic_context[n]->stroke_width < 0.0))
3829  ThrowPointExpectedException(image,token);
3830  break;
3831  }
3832  status=MagickFalse;
3833  break;
3834  }
3835  case 't':
3836  case 'T':
3837  {
3838  if (LocaleCompare("text",keyword) == 0)
3839  {
3840  primitive_type=TextPrimitive;
3841  cursor=0.0;
3842  break;
3843  }
3844  if (LocaleCompare("text-align",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-anchor",keyword) == 0)
3860  {
3861  ssize_t
3862  align;
3863 
3864  (void) GetNextToken(q,&q,extent,token);
3865  align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3866  if (align == -1)
3867  {
3868  status=MagickFalse;
3869  break;
3870  }
3871  graphic_context[n]->align=(AlignType) align;
3872  break;
3873  }
3874  if (LocaleCompare("text-antialias",keyword) == 0)
3875  {
3876  (void) GetNextToken(q,&q,extent,token);
3877  graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3878  MagickTrue : MagickFalse;
3879  break;
3880  }
3881  if (LocaleCompare("text-undercolor",keyword) == 0)
3882  {
3883  (void) GetNextToken(q,&q,extent,token);
3884  status&=QueryColorDatabase(token,&graphic_context[n]->undercolor,
3885  &image->exception);
3886  break;
3887  }
3888  if (LocaleCompare("translate",keyword) == 0)
3889  {
3890  (void) GetNextToken(q,&q,extent,token);
3891  affine.tx=GetDrawValue(token,&next_token);
3892  if (token == next_token)
3893  ThrowPointExpectedException(image,token);
3894  (void) GetNextToken(q,&q,extent,token);
3895  if (*token == ',')
3896  (void) GetNextToken(q,&q,extent,token);
3897  affine.ty=GetDrawValue(token,&next_token);
3898  if (token == next_token)
3899  ThrowPointExpectedException(image,token);
3900  break;
3901  }
3902  status=MagickFalse;
3903  break;
3904  }
3905  case 'u':
3906  case 'U':
3907  {
3908  if (LocaleCompare("use",keyword) == 0)
3909  {
3910  const char
3911  *use;
3912 
3913  /*
3914  Get a macro from the MVG document, and "use" it here.
3915  */
3916  (void) GetNextToken(q,&q,extent,token);
3917  use=(const char *) GetValueFromSplayTree(macros,token);
3918  if (use != (const char *) NULL)
3919  {
3920  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3921  (void) CloneString(&clone_info->primitive,use);
3922  status=RenderMVGContent(image,clone_info,depth+1);
3923  clone_info=DestroyDrawInfo(clone_info);
3924  }
3925  break;
3926  }
3927  status=MagickFalse;
3928  break;
3929  }
3930  case 'v':
3931  case 'V':
3932  {
3933  if (LocaleCompare("viewbox",keyword) == 0)
3934  {
3935  (void) GetNextToken(q,&q,extent,token);
3936  graphic_context[n]->viewbox.x=CastDoubleToLong(ceil(
3937  GetDrawValue(token,&next_token)-0.5));
3938  if (token == next_token)
3939  ThrowPointExpectedException(image,token);
3940  (void) GetNextToken(q,&q,extent,token);
3941  if (*token == ',')
3942  (void) GetNextToken(q,&q,extent,token);
3943  graphic_context[n]->viewbox.y=CastDoubleToLong(ceil(
3944  GetDrawValue(token,&next_token)-0.5));
3945  if (token == next_token)
3946  ThrowPointExpectedException(image,token);
3947  (void) GetNextToken(q,&q,extent,token);
3948  if (*token == ',')
3949  (void) GetNextToken(q,&q,extent,token);
3950  graphic_context[n]->viewbox.width=CastDoubleToUnsigned(
3951  GetDrawValue(token,&next_token)+0.5);
3952  if (token == next_token)
3953  ThrowPointExpectedException(image,token);
3954  (void) GetNextToken(q,&q,extent,token);
3955  if (*token == ',')
3956  (void) GetNextToken(q,&q,extent,token);
3957  graphic_context[n]->viewbox.height=CastDoubleToUnsigned(
3958  GetDrawValue(token,&next_token)+0.5);
3959  if (token == next_token)
3960  ThrowPointExpectedException(image,token);
3961  break;
3962  }
3963  status=MagickFalse;
3964  break;
3965  }
3966  case 'w':
3967  case 'W':
3968  {
3969  if (LocaleCompare("word-spacing",keyword) == 0)
3970  {
3971  (void) GetNextToken(q,&q,extent,token);
3972  graphic_context[n]->interword_spacing=GetDrawValue(token,
3973  &next_token);
3974  if (token == next_token)
3975  ThrowPointExpectedException(image,token);
3976  break;
3977  }
3978  status=MagickFalse;
3979  break;
3980  }
3981  default:
3982  {
3983  status=MagickFalse;
3984  break;
3985  }
3986  }
3987  if (status == MagickFalse)
3988  break;
3989  if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
3990  (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
3991  (fabs(affine.sy-1.0) >= MagickEpsilon) ||
3992  (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
3993  {
3994  graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
3995  graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
3996  graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
3997  graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
3998  graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
3999  current.tx;
4000  graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4001  current.ty;
4002  }
4003  if (primitive_type == UndefinedPrimitive)
4004  {
4005  if ((draw_info->debug != MagickFalse) && (q > p))
4006  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
4007  (q-p-1),p);
4008  continue;
4009  }
4010  /*
4011  Parse the primitive attributes.
4012  */
4013  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4014  if (primitive_info[i].text != (char *) NULL)
4015  primitive_info[i].text=DestroyString(primitive_info[i].text);
4016  i=0;
4017  mvg_info.offset=i;
4018  j=0;
4019  primitive_info[0].primitive=primitive_type;
4020  primitive_info[0].point.x=0.0;
4021  primitive_info[0].point.y=0.0;
4022  primitive_info[0].coordinates=0;
4023  primitive_info[0].method=FloodfillMethod;
4024  primitive_info[0].closed_subpath=MagickFalse;
4025  for (x=0; *q != '\0'; x++)
4026  {
4027  /*
4028  Define points.
4029  */
4030  if (IsPoint(q) == MagickFalse)
4031  break;
4032  (void) GetNextToken(q,&q,extent,token);
4033  point.x=GetDrawValue(token,&next_token);
4034  if (token == next_token)
4035  ThrowPointExpectedException(image,token);
4036  (void) GetNextToken(q,&q,extent,token);
4037  if (*token == ',')
4038  (void) GetNextToken(q,&q,extent,token);
4039  point.y=GetDrawValue(token,&next_token);
4040  if (token == next_token)
4041  ThrowPointExpectedException(image,token);
4042  (void) GetNextToken(q,(const char **) NULL,extent,token);
4043  if (*token == ',')
4044  (void) GetNextToken(q,&q,extent,token);
4045  primitive_info[i].primitive=primitive_type;
4046  primitive_info[i].point=point;
4047  primitive_info[i].coordinates=0;
4048  primitive_info[i].method=FloodfillMethod;
4049  primitive_info[i].closed_subpath=MagickFalse;
4050  i++;
4051  mvg_info.offset=i;
4052  if (i < (ssize_t) number_points)
4053  continue;
4054  status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4055  primitive_info=(*mvg_info.primitive_info);
4056  }
4057  if (status == MagickFalse)
4058  break;
4059  if (primitive_info[j].text != (char *) NULL)
4060  primitive_info[j].text=DestroyString(primitive_info[j].text);
4061  primitive_info[j].primitive=primitive_type;
4062  primitive_info[j].coordinates=(size_t) x;
4063  primitive_info[j].method=FloodfillMethod;
4064  primitive_info[j].closed_subpath=MagickFalse;
4065  /*
4066  Circumscribe primitive within a circle.
4067  */
4068  bounds.x1=primitive_info[j].point.x;
4069  bounds.y1=primitive_info[j].point.y;
4070  bounds.x2=primitive_info[j].point.x;
4071  bounds.y2=primitive_info[j].point.y;
4072  for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4073  {
4074  point=primitive_info[j+k].point;
4075  if (point.x < bounds.x1)
4076  bounds.x1=point.x;
4077  if (point.y < bounds.y1)
4078  bounds.y1=point.y;
4079  if (point.x > bounds.x2)
4080  bounds.x2=point.x;
4081  if (point.y > bounds.y2)
4082  bounds.y2=point.y;
4083  }
4084  /*
4085  Speculate how many points our primitive might consume.
4086  */
4087  coordinates=(double) primitive_info[j].coordinates;
4088  switch (primitive_type)
4089  {
4090  case RectanglePrimitive:
4091  {
4092  coordinates*=5.0;
4093  break;
4094  }
4095  case RoundRectanglePrimitive:
4096  {
4097  double
4098  alpha,
4099  beta,
4100  radius;
4101 
4102  alpha=bounds.x2-bounds.x1;
4103  beta=bounds.y2-bounds.y1;
4104  radius=hypot(alpha,beta);
4105  coordinates*=5.0;
4106  coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4107  BezierQuantum+360.0;
4108  break;
4109  }
4110  case BezierPrimitive:
4111  {
4112  coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4113  break;
4114  }
4115  case PathPrimitive:
4116  {
4117  char
4118  *s,
4119  *t;
4120 
4121  (void) GetNextToken(q,&q,extent,token);
4122  coordinates=1.0;
4123  t=token;
4124  for (s=token; *s != '\0'; s=t)
4125  {
4126  double
4127  value;
4128 
4129  value=GetDrawValue(s,&t);
4130  (void) value;
4131  if (s == t)
4132  {
4133  t++;
4134  continue;
4135  }
4136  coordinates++;
4137  }
4138  for (s=token; *s != '\0'; s++)
4139  if (strspn(s,"AaCcQqSsTt") != 0)
4140  coordinates+=(20.0*BezierQuantum)+360.0;
4141  break;
4142  }
4143  default:
4144  break;
4145  }
4146  if (status == MagickFalse)
4147  break;
4148  if (((size_t) (i+coordinates)) >= number_points)
4149  {
4150  /*
4151  Resize based on speculative points required by primitive.
4152  */
4153  number_points+=coordinates+1;
4154  if (number_points < (size_t) coordinates)
4155  {
4156  (void) ThrowMagickException(&image->exception,GetMagickModule(),
4157  ResourceLimitError,"MemoryAllocationFailed","`%s'",
4158  image->filename);
4159  status=MagickFalse;
4160  break;
4161  }
4162  mvg_info.offset=i;
4163  status&=CheckPrimitiveExtent(&mvg_info,(double) number_points);
4164  primitive_info=(*mvg_info.primitive_info);
4165  }
4166  status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4167  primitive_info=(*mvg_info.primitive_info);
4168  if (status == MagickFalse)
4169  break;
4170  mvg_info.offset=j;
4171  switch (primitive_type)
4172  {
4173  case PointPrimitive:
4174  default:
4175  {
4176  if (primitive_info[j].coordinates != 1)
4177  {
4178  status=MagickFalse;
4179  break;
4180  }
4181  status&=TracePoint(primitive_info+j,primitive_info[j].point);
4182  primitive_info=(*mvg_info.primitive_info);
4183  i=(ssize_t) (j+primitive_info[j].coordinates);
4184  break;
4185  }
4186  case LinePrimitive:
4187  {
4188  if (primitive_info[j].coordinates != 2)
4189  {
4190  status=MagickFalse;
4191  break;
4192  }
4193  status&=TraceLine(primitive_info+j,primitive_info[j].point,
4194  primitive_info[j+1].point);
4195  primitive_info=(*mvg_info.primitive_info);
4196  i=(ssize_t) (j+primitive_info[j].coordinates);
4197  break;
4198  }
4199  case RectanglePrimitive:
4200  {
4201  if (primitive_info[j].coordinates != 2)
4202  {
4203  status=MagickFalse;
4204  break;
4205  }
4206  status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4207  primitive_info[j+1].point);
4208  primitive_info=(*mvg_info.primitive_info);
4209  i=(ssize_t) (j+primitive_info[j].coordinates);
4210  break;
4211  }
4212  case RoundRectanglePrimitive:
4213  {
4214  if (primitive_info[j].coordinates != 3)
4215  {
4216  status=MagickFalse;
4217  break;
4218  }
4219  if ((primitive_info[j+2].point.x < 0.0) ||
4220  (primitive_info[j+2].point.y < 0.0))
4221  {
4222  status=MagickFalse;
4223  break;
4224  }
4225  if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4226  {
4227  status=MagickFalse;
4228  break;
4229  }
4230  if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4231  {
4232  status=MagickFalse;
4233  break;
4234  }
4235  status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4236  primitive_info[j+1].point,primitive_info[j+2].point);
4237  primitive_info=(*mvg_info.primitive_info);
4238  i=(ssize_t) (j+primitive_info[j].coordinates);
4239  break;
4240  }
4241  case ArcPrimitive:
4242  {
4243  if (primitive_info[j].coordinates != 3)
4244  {
4245  status=MagickFalse;
4246  break;
4247  }
4248  status&=TraceArc(&mvg_info,primitive_info[j].point,
4249  primitive_info[j+1].point,primitive_info[j+2].point);
4250  primitive_info=(*mvg_info.primitive_info);
4251  i=(ssize_t) (j+primitive_info[j].coordinates);
4252  break;
4253  }
4254  case EllipsePrimitive:
4255  {
4256  if (primitive_info[j].coordinates != 3)
4257  {
4258  status=MagickFalse;
4259  break;
4260  }
4261  if ((primitive_info[j+1].point.x < 0.0) ||
4262  (primitive_info[j+1].point.y < 0.0))
4263  {
4264  status=MagickFalse;
4265  break;
4266  }
4267  status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4268  primitive_info[j+1].point,primitive_info[j+2].point);
4269  primitive_info=(*mvg_info.primitive_info);
4270  i=(ssize_t) (j+primitive_info[j].coordinates);
4271  break;
4272  }
4273  case CirclePrimitive:
4274  {
4275  if (primitive_info[j].coordinates != 2)
4276  {
4277  status=MagickFalse;
4278  break;
4279  }
4280  status&=TraceCircle(&mvg_info,primitive_info[j].point,
4281  primitive_info[j+1].point);
4282  primitive_info=(*mvg_info.primitive_info);
4283  i=(ssize_t) (j+primitive_info[j].coordinates);
4284  break;
4285  }
4286  case PolylinePrimitive:
4287  {
4288  if (primitive_info[j].coordinates < 1)
4289  {
4290  status=MagickFalse;
4291  break;
4292  }
4293  break;
4294  }
4295  case PolygonPrimitive:
4296  {
4297  if (primitive_info[j].coordinates < 3)
4298  {
4299  status=MagickFalse;
4300  break;
4301  }
4302  primitive_info[i]=primitive_info[j];
4303  primitive_info[i].coordinates=0;
4304  primitive_info[j].coordinates++;
4305  primitive_info[j].closed_subpath=MagickTrue;
4306  i++;
4307  break;
4308  }
4309  case BezierPrimitive:
4310  {
4311  if (primitive_info[j].coordinates < 3)
4312  {
4313  status=MagickFalse;
4314  break;
4315  }
4316  status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4317  primitive_info=(*mvg_info.primitive_info);
4318  i=(ssize_t) (j+primitive_info[j].coordinates);
4319  break;
4320  }
4321  case PathPrimitive:
4322  {
4323  coordinates=(double) TracePath(image,&mvg_info,token);
4324  primitive_info=(*mvg_info.primitive_info);
4325  if (coordinates < 0.0)
4326  {
4327  status=MagickFalse;
4328  break;
4329  }
4330  i=(ssize_t) (j+coordinates);
4331  break;
4332  }
4333  case ColorPrimitive:
4334  case MattePrimitive:
4335  {
4336  ssize_t
4337  method;
4338 
4339  if (primitive_info[j].coordinates != 1)
4340  {
4341  status=MagickFalse;
4342  break;
4343  }
4344  (void) GetNextToken(q,&q,extent,token);
4345  method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4346  if (method == -1)
4347  {
4348  status=MagickFalse;
4349  break;
4350  }
4351  primitive_info[j].method=(PaintMethod) method;
4352  break;
4353  }
4354  case TextPrimitive:
4355  {
4356  char
4357  geometry[MagickPathExtent];
4358 
4359  if (primitive_info[j].coordinates != 1)
4360  {
4361  status=MagickFalse;
4362  break;
4363  }
4364  if (*token != ',')
4365  (void) GetNextToken(q,&q,extent,token);
4366  (void) CloneString(&primitive_info[j].text,token);
4367  /*
4368  Compute text cursor offset.
4369  */
4370  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4371  if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4372  (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4373  {
4374  mvg_info.point=primitive_info->point;
4375  primitive_info->point.x+=cursor;
4376  }
4377  else
4378  {
4379  mvg_info.point=primitive_info->point;
4380  cursor=0.0;
4381  }
4382  (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4383  primitive_info->point.x,primitive_info->point.y);
4384  clone_info->render=MagickFalse;
4385  clone_info->text=AcquireString(token);
4386  status&=GetTypeMetrics(image,clone_info,&metrics);
4387  clone_info=DestroyDrawInfo(clone_info);
4388  cursor+=metrics.width;
4389  if (graphic_context[n]->compliance != SVGCompliance)
4390  cursor=0.0;
4391  break;
4392  }
4393  case ImagePrimitive:
4394  {
4395  if (primitive_info[j].coordinates != 2)
4396  {
4397  status=MagickFalse;
4398  break;
4399  }
4400  (void) GetNextToken(q,&q,extent,token);
4401  (void) CloneString(&primitive_info[j].text,token);
4402  break;
4403  }
4404  }
4405  mvg_info.offset=i;
4406  if (status == 0)
4407  break;
4408  primitive_info[i].primitive=UndefinedPrimitive;
4409  if ((draw_info->debug != MagickFalse) && (q > p))
4410  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
4411  /*
4412  Sanity check.
4413  */
4414  status&=CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4415  &graphic_context[n]->affine));
4416  primitive_info=(*mvg_info.primitive_info);
4417  if (status == 0)
4418  break;
4419  status&=CheckPrimitiveExtent(&mvg_info,(double)
4420  graphic_context[n]->stroke_width);
4421  primitive_info=(*mvg_info.primitive_info);
4422  if (status == 0)
4423  break;
4424  if (i == 0)
4425  continue;
4426  /*
4427  Transform points.
4428  */
4429  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4430  {
4431  point=primitive_info[i].point;
4432  primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4433  graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4434  primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4435  graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4436  point=primitive_info[i].point;
4437  if (point.x < graphic_context[n]->bounds.x1)
4438  graphic_context[n]->bounds.x1=point.x;
4439  if (point.y < graphic_context[n]->bounds.y1)
4440  graphic_context[n]->bounds.y1=point.y;
4441  if (point.x > graphic_context[n]->bounds.x2)
4442  graphic_context[n]->bounds.x2=point.x;
4443  if (point.y > graphic_context[n]->bounds.y2)
4444  graphic_context[n]->bounds.y2=point.y;
4445  if (primitive_info[i].primitive == ImagePrimitive)
4446  break;
4447  if (i >= (ssize_t) number_points)
4448  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4449  }
4450  if (graphic_context[n]->render != MagickFalse)
4451  {
4452  if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4453  (graphic_context[n]->clip_mask != (char *) NULL) &&
4454  (LocaleCompare(graphic_context[n]->clip_mask,
4455  graphic_context[n-1]->clip_mask) != 0))
4456  {
4457  const char
4458  *clip_path;
4459 
4460  clip_path=(const char *) GetValueFromSplayTree(macros,
4461  graphic_context[n]->clip_mask);
4462  if (clip_path != (const char *) NULL)
4463  (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4464  clip_path);
4465  status&=DrawClipPath(image,graphic_context[n],
4466  graphic_context[n]->clip_mask);
4467  }
4468  status&=DrawPrimitive(image,graphic_context[n],primitive_info);
4469  }
4470  proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4471  primitive_extent);
4472  if (proceed == MagickFalse)
4473  break;
4474  if (status == 0)
4475  break;
4476  }
4477  if (draw_info->debug != MagickFalse)
4478  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4479  /*
4480  Relinquish resources.
4481  */
4482  macros=DestroySplayTree(macros);
4483  token=DestroyString(token);
4484  if (primitive_info != (PrimitiveInfo *) NULL)
4485  {
4486  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4487  if (primitive_info[i].text != (char *) NULL)
4488  primitive_info[i].text=DestroyString(primitive_info[i].text);
4489  primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4490  }
4491  primitive=DestroyString(primitive);
4492  for ( ; n >= 0; n--)
4493  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4494  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4495  if (status == MagickFalse)
4496  ThrowBinaryImageException(DrawError,
4497  "NonconformingDrawingPrimitiveDefinition",keyword);
4498  return(status != 0 ? MagickTrue : MagickFalse);
4499 }
4500 
4501 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info)
4502 {
4503  return(RenderMVGContent(image,draw_info,0));
4504 }
4505 
4506 /*
4507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4508 % %
4509 % %
4510 % %
4511 % D r a w P a t t e r n P a t h %
4512 % %
4513 % %
4514 % %
4515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4516 %
4517 % DrawPatternPath() draws a pattern.
4518 %
4519 % The format of the DrawPatternPath method is:
4520 %
4521 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4522 % const char *name,Image **pattern)
4523 %
4524 % A description of each parameter follows:
4525 %
4526 % o image: the image.
4527 %
4528 % o draw_info: the draw info.
4529 %
4530 % o name: the pattern name.
4531 %
4532 % o image: the image.
4533 %
4534 */
4535 MagickExport MagickBooleanType DrawPatternPath(Image *image,
4536  const DrawInfo *draw_info,const char *name,Image **pattern)
4537 {
4538  char
4539  property[MaxTextExtent];
4540 
4541  const char
4542  *geometry,
4543  *path,
4544  *type;
4545 
4546  DrawInfo
4547  *clone_info;
4548 
4549  ImageInfo
4550  *image_info;
4551 
4552  MagickBooleanType
4553  status;
4554 
4555  assert(image != (Image *) NULL);
4556  assert(image->signature == MagickCoreSignature);
4557  assert(draw_info != (const DrawInfo *) NULL);
4558  if (IsEventLogging() != MagickFalse)
4559  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4560  assert(name != (const char *) NULL);
4561  (void) FormatLocaleString(property,MaxTextExtent,"%s",name);
4562  path=GetImageArtifact(image,property);
4563  if (path == (const char *) NULL)
4564  return(MagickFalse);
4565  (void) FormatLocaleString(property,MaxTextExtent,"%s-geometry",name);
4566  geometry=GetImageArtifact(image,property);
4567  if (geometry == (const char *) NULL)
4568  return(MagickFalse);
4569  if ((*pattern) != (Image *) NULL)
4570  *pattern=DestroyImage(*pattern);
4571  image_info=AcquireImageInfo();
4572  image_info->size=AcquireString(geometry);
4573  *pattern=AcquireImage(image_info);
4574  image_info=DestroyImageInfo(image_info);
4575  (void) QueryColorDatabase("#00000000",&(*pattern)->background_color,
4576  &image->exception);
4577  (void) SetImageBackgroundColor(*pattern);
4578  if (draw_info->debug != MagickFalse)
4579  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4580  "begin pattern-path %s %s",name,geometry);
4581  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4582  if (clone_info->fill_pattern != (Image *) NULL)
4583  clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4584  if (clone_info->stroke_pattern != (Image *) NULL)
4585  clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4586  (void) FormatLocaleString(property,MaxTextExtent,"%s-type",name);
4587  type=GetImageArtifact(image,property);
4588  if (type != (const char *) NULL)
4589  clone_info->gradient.type=(GradientType) ParseCommandOption(
4590  MagickGradientOptions,MagickFalse,type);
4591  (void) CloneString(&clone_info->primitive,path);
4592  status=RenderMVGContent(*pattern,clone_info,0);
4593  clone_info=DestroyDrawInfo(clone_info);
4594  if (draw_info->debug != MagickFalse)
4595  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4596  return(status);
4597 }
4598 
4599 /*
4600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4601 % %
4602 % %
4603 % %
4604 + D r a w P o l y g o n P r i m i t i v e %
4605 % %
4606 % %
4607 % %
4608 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4609 %
4610 % DrawPolygonPrimitive() draws a polygon on the image.
4611 %
4612 % The format of the DrawPolygonPrimitive method is:
4613 %
4614 % MagickBooleanType DrawPolygonPrimitive(Image *image,
4615 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4616 %
4617 % A description of each parameter follows:
4618 %
4619 % o image: the image.
4620 %
4621 % o draw_info: the draw info.
4622 %
4623 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4624 %
4625 */
4626 
4627 static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4628 {
4629  ssize_t
4630  i;
4631 
4632  assert(polygon_info != (PolygonInfo **) NULL);
4633  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4634  if (polygon_info[i] != (PolygonInfo *) NULL)
4635  polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4636  polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4637  return(polygon_info);
4638 }
4639 
4640 static PolygonInfo **AcquirePolygonTLS(const DrawInfo *draw_info,
4641  const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4642 {
4643  PathInfo
4644  *magick_restrict path_info;
4645 
4646  PolygonInfo
4647  **polygon_info;
4648 
4649  size_t
4650  number_threads;
4651 
4652  number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4653  polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4654  sizeof(*polygon_info));
4655  if (polygon_info == (PolygonInfo **) NULL)
4656  {
4657  (void) ThrowMagickException(exception,GetMagickModule(),
4658  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4659  return((PolygonInfo **) NULL);
4660  }
4661  (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4662  path_info=ConvertPrimitiveToPath(draw_info,primitive_info,exception);
4663  if (path_info == (PathInfo *) NULL)
4664  return(DestroyPolygonTLS(polygon_info));
4665  polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4666  if (polygon_info[0] == (PolygonInfo *) NULL)
4667  {
4668  (void) ThrowMagickException(exception,GetMagickModule(),
4669  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4670  return(DestroyPolygonTLS(polygon_info));
4671  }
4672  path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4673  return(polygon_info);
4674 }
4675 
4676 static MagickBooleanType AcquirePolygonEdgesTLS(PolygonInfo **polygon_info,
4677  const size_t number_threads,ExceptionInfo *exception)
4678 {
4679  ssize_t
4680  i;
4681 
4682  for (i=1; i < (ssize_t) number_threads; i++)
4683  {
4684  EdgeInfo
4685  *edge_info;
4686 
4687  ssize_t
4688  j;
4689 
4690  polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4691  sizeof(*polygon_info[i]));
4692  if (polygon_info[i] == (PolygonInfo *) NULL)
4693  {
4694  (void) ThrowMagickException(exception,GetMagickModule(),
4695  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4696  return(MagickFalse);
4697  }
4698  polygon_info[i]->number_edges=0;
4699  edge_info=polygon_info[0]->edges;
4700  polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4701  polygon_info[0]->number_edges,sizeof(*edge_info));
4702  if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4703  {
4704  (void) ThrowMagickException(exception,GetMagickModule(),
4705  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4706  return(MagickFalse);
4707  }
4708  (void) memcpy(polygon_info[i]->edges,edge_info,
4709  polygon_info[0]->number_edges*sizeof(*edge_info));
4710  for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4711  polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4712  polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4713  for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4714  {
4715  edge_info=polygon_info[0]->edges+j;
4716  polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4717  edge_info->number_points,sizeof(*edge_info));
4718  if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4719  {
4720  (void) ThrowMagickException(exception,GetMagickModule(),
4721  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4722  return(MagickFalse);
4723  }
4724  (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4725  edge_info->number_points*sizeof(*edge_info->points));
4726  }
4727  }
4728  return(MagickTrue);
4729 }
4730 
4731 static size_t DestroyEdge(PolygonInfo *polygon_info,const ssize_t edge)
4732 {
4733  assert(edge < (ssize_t) polygon_info->number_edges);
4734  polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4735  polygon_info->edges[edge].points);
4736  polygon_info->number_edges--;
4737  if (edge < (ssize_t) polygon_info->number_edges)
4738  (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4739  (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
4740  return(polygon_info->number_edges);
4741 }
4742 
4743 static double GetOpacityPixel(PolygonInfo *polygon_info,const double mid,
4744  const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4745  const ssize_t y,double *stroke_opacity)
4746 {
4747  double
4748  alpha,
4749  beta,
4750  distance,
4751  subpath_opacity;
4752 
4753  PointInfo
4754  delta;
4755 
4756  EdgeInfo
4757  *p;
4758 
4759  const PointInfo
4760  *q;
4761 
4762  ssize_t
4763  i;
4764 
4765  ssize_t
4766  j,
4767  winding_number;
4768 
4769  /*
4770  Compute fill & stroke opacity for this (x,y) point.
4771  */
4772  *stroke_opacity=0.0;
4773  subpath_opacity=0.0;
4774  p=polygon_info->edges;
4775  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4776  {
4777  if ((double) y <= (p->bounds.y1-mid-0.5))
4778  break;
4779  if ((double) y > (p->bounds.y2+mid+0.5))
4780  {
4781  p--;
4782  (void) DestroyEdge(polygon_info,j--);
4783  continue;
4784  }
4785  if (((double) x <= (p->bounds.x1-mid-0.5)) ||
4786  ((double) x > (p->bounds.x2+mid+0.5)))
4787  continue;
4788  i=(ssize_t) MagickMax((double) p->highwater,1.0);
4789  for ( ; i < (ssize_t) p->number_points; i++)
4790  {
4791  if ((double) y <= (p->points[i-1].y-mid-0.5))
4792  break;
4793  if ((double) y > (p->points[i].y+mid+0.5))
4794  continue;
4795  if (p->scanline != (double) y)
4796  {
4797  p->scanline=(double) y;
4798  p->highwater=(size_t) i;
4799  }
4800  /*
4801  Compute distance between a point and an edge.
4802  */
4803  q=p->points+i-1;
4804  delta.x=(q+1)->x-q->x;
4805  delta.y=(q+1)->y-q->y;
4806  beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4807  if (beta <= 0.0)
4808  {
4809  delta.x=(double) x-q->x;
4810  delta.y=(double) y-q->y;
4811  distance=delta.x*delta.x+delta.y*delta.y;
4812  }
4813  else
4814  {
4815  alpha=delta.x*delta.x+delta.y*delta.y;
4816  if (beta >= alpha)
4817  {
4818  delta.x=(double) x-(q+1)->x;
4819  delta.y=(double) y-(q+1)->y;
4820  distance=delta.x*delta.x+delta.y*delta.y;
4821  }
4822  else
4823  {
4824  alpha=MagickSafeReciprocal(alpha);
4825  beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4826  distance=alpha*beta*beta;
4827  }
4828  }
4829  /*
4830  Compute stroke & subpath opacity.
4831  */
4832  beta=0.0;
4833  if (p->ghostline == MagickFalse)
4834  {
4835  alpha=mid+0.5;
4836  if ((*stroke_opacity < 1.0) &&
4837  (distance <= ((alpha+0.25)*(alpha+0.25))))
4838  {
4839  alpha=mid-0.5;
4840  if (distance <= ((alpha+0.25)*(alpha+0.25)))
4841  *stroke_opacity=1.0;
4842  else
4843  {
4844  beta=1.0;
4845  if (fabs(distance-1.0) >= MagickEpsilon)
4846  beta=sqrt((double) distance);
4847  alpha=beta-mid-0.5;
4848  if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
4849  *stroke_opacity=(alpha-0.25)*(alpha-0.25);
4850  }
4851  }
4852  }
4853  if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
4854  continue;
4855  if (distance <= 0.0)
4856  {
4857  subpath_opacity=1.0;
4858  continue;
4859  }
4860  if (distance > 1.0)
4861  continue;
4862  if (fabs(beta) < MagickEpsilon)
4863  {
4864  beta=1.0;
4865  if (fabs(distance-1.0) >= MagickEpsilon)
4866  beta=sqrt(distance);
4867  }
4868  alpha=beta-1.0;
4869  if (subpath_opacity < (alpha*alpha))
4870  subpath_opacity=alpha*alpha;
4871  }
4872  }
4873  /*
4874  Compute fill opacity.
4875  */
4876  if (fill == MagickFalse)
4877  return(0.0);
4878  if (subpath_opacity >= 1.0)
4879  return(1.0);
4880  /*
4881  Determine winding number.
4882  */
4883  winding_number=0;
4884  p=polygon_info->edges;
4885  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4886  {
4887  if ((double) y <= p->bounds.y1)
4888  break;
4889  if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
4890  continue;
4891  if ((double) x > p->bounds.x2)
4892  {
4893  winding_number+=p->direction != 0 ? 1 : -1;
4894  continue;
4895  }
4896  i=(ssize_t) MagickMax((double) p->highwater,1.0);
4897  for ( ; i < (ssize_t) (p->number_points-1); i++)
4898  if ((double) y <= p->points[i].y)
4899  break;
4900  q=p->points+i-1;
4901  if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4902  winding_number+=p->direction != 0 ? 1 : -1;
4903  }
4904  if (fill_rule != NonZeroRule)
4905  {
4906  if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4907  return(1.0);
4908  }
4909  else
4910  if (MagickAbsoluteValue(winding_number) != 0)
4911  return(1.0);
4912  return(subpath_opacity);
4913 }
4914 
4915 static MagickBooleanType DrawPolygonPrimitive(Image *image,
4916  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
4917 {
4918  typedef struct _ExtentInfo
4919  {
4920  ssize_t
4921  x1,
4922  y1,
4923  x2,
4924  y2;
4925  } ExtentInfo;
4926 
4927  CacheView
4928  *image_view;
4929 
4930  const char
4931  *artifact;
4932 
4933  double
4934  mid;
4935 
4937  *exception;
4938 
4939  ExtentInfo
4940  poly_extent;
4941 
4942  MagickBooleanType
4943  fill,
4944  status;
4945 
4946  PolygonInfo
4947  **magick_restrict polygon_info;
4948 
4949  EdgeInfo
4950  *p;
4951 
4952  SegmentInfo
4953  bounds;
4954 
4955  size_t
4956  number_threads = 1;
4957 
4958  ssize_t
4959  i,
4960  y;
4961 
4962  assert(image != (Image *) NULL);
4963  assert(image->signature == MagickCoreSignature);
4964  assert(draw_info != (DrawInfo *) NULL);
4965  assert(draw_info->signature == MagickCoreSignature);
4966  assert(primitive_info != (PrimitiveInfo *) NULL);
4967  if (IsEventLogging() != MagickFalse)
4968  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4969  if (primitive_info->coordinates <= 1)
4970  return(MagickTrue);
4971  /*
4972  Compute bounding box.
4973  */
4974  polygon_info=AcquirePolygonTLS(draw_info,primitive_info,&image->exception);
4975  if (polygon_info == (PolygonInfo **) NULL)
4976  return(MagickFalse);
4977  if (draw_info->debug != MagickFalse)
4978  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4979  fill=(primitive_info->method == FillToBorderMethod) ||
4980  (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4981  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
4982  bounds=polygon_info[0]->edges[0].bounds;
4983  artifact=GetImageArtifact(image,"draw:render-bounding-rectangles");
4984  if (IsStringTrue(artifact) != MagickFalse)
4985  (void) DrawBoundingRectangles(image,draw_info,polygon_info[0]);
4986  for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4987  {
4988  p=polygon_info[0]->edges+i;
4989  if (p->bounds.x1 < bounds.x1)
4990  bounds.x1=p->bounds.x1;
4991  if (p->bounds.y1 < bounds.y1)
4992  bounds.y1=p->bounds.y1;
4993  if (p->bounds.x2 > bounds.x2)
4994  bounds.x2=p->bounds.x2;
4995  if (p->bounds.y2 > bounds.y2)
4996  bounds.y2=p->bounds.y2;
4997  }
4998  bounds.x1-=(mid+1.0);
4999  bounds.y1-=(mid+1.0);
5000  bounds.x2+=(mid+1.0);
5001  bounds.y2+=(mid+1.0);
5002  if ((bounds.x1 >= (double) image->columns) ||
5003  (bounds.y1 >= (double) image->rows) ||
5004  (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5005  {
5006  polygon_info=DestroyPolygonTLS(polygon_info);
5007  return(MagickTrue); /* virtual polygon */
5008  }
5009  bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5010  (double) image->columns-1.0 : bounds.x1;
5011  bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5012  (double) image->rows-1.0 : bounds.y1;
5013  bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5014  (double) image->columns-1.0 : bounds.x2;
5015  bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5016  (double) image->rows-1.0 : bounds.y2;
5017  poly_extent.x1=CastDoubleToLong(ceil(bounds.x1-0.5));
5018  poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5019  poly_extent.x2=CastDoubleToLong(floor(bounds.x2+0.5));
5020  poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5021  number_threads=GetMagickNumberThreads(image,image,poly_extent.y2-
5022  poly_extent.y1+1,1);
5023  status=AcquirePolygonEdgesTLS(polygon_info,number_threads,&image->exception);
5024  if (status == MagickFalse)
5025  {
5026  polygon_info=DestroyPolygonTLS(polygon_info);
5027  return(status);
5028  }
5029  status=MagickTrue;
5030  exception=(&image->exception);
5031  image_view=AcquireAuthenticCacheView(image,exception);
5032  if ((primitive_info->coordinates == 1) ||
5033  (polygon_info[0]->number_edges == 0))
5034  {
5035  /*
5036  Draw point.
5037  */
5038 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5039  #pragma omp parallel for schedule(static) shared(status) \
5040  num_threads(number_threads)
5041 #endif
5042  for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5043  {
5044  MagickBooleanType
5045  sync;
5046 
5047  PixelPacket
5048  *magick_restrict q;
5049 
5050  ssize_t
5051  x;
5052 
5053  if (status == MagickFalse)
5054  continue;
5055  x=poly_extent.x1;
5056  q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (poly_extent.x2-
5057  x+1),1,exception);
5058  if (q == (PixelPacket *) NULL)
5059  {
5060  status=MagickFalse;
5061  continue;
5062  }
5063  for ( ; x <= poly_extent.x2; x++)
5064  {
5065  if ((x == CastDoubleToLong(ceil(primitive_info->point.x-0.5))) &&
5066  (y == CastDoubleToLong(ceil(primitive_info->point.y-0.5))))
5067  (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,q);
5068  q++;
5069  }
5070  sync=SyncCacheViewAuthenticPixels(image_view,exception);
5071  if (sync == MagickFalse)
5072  status=MagickFalse;
5073  }
5074  image_view=DestroyCacheView(image_view);
5075  polygon_info=DestroyPolygonTLS(polygon_info);
5076  if (draw_info->debug != MagickFalse)
5077  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5078  " end draw-polygon");
5079  return(status);
5080  }
5081  /*
5082  Draw polygon or line.
5083  */
5084  poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5085  poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5086 #if defined(MAGICKCORE_OPENMP_SUPPORT)
5087  #pragma omp parallel for schedule(static) shared(status) \
5088  num_threads(number_threads)
5089 #endif
5090  for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5091  {
5092  const int
5093  id = GetOpenMPThreadId();
5094 
5095  PixelPacket
5096  fill_color,
5097  stroke_color;
5098 
5099  PixelPacket
5100  *magick_restrict q;
5101 
5102  ssize_t
5103  x;
5104 
5105  if (status == MagickFalse)
5106  continue;
5107  q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(size_t)
5108  (poly_extent.x2-poly_extent.x1+1),1,exception);
5109  if (q == (PixelPacket *) NULL)
5110  {
5111  status=MagickFalse;
5112  continue;
5113  }
5114  for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5115  {
5116  double
5117  fill_opacity,
5118  stroke_opacity;
5119 
5120  /*
5121  Fill and/or stroke.
5122  */
5123  fill_opacity=GetOpacityPixel(polygon_info[id],mid,fill,
5124  draw_info->fill_rule,x,y,&stroke_opacity);
5125  if (draw_info->stroke_antialias == MagickFalse)
5126  {
5127  fill_opacity=fill_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5128  stroke_opacity=stroke_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5129  }
5130  (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5131  &fill_color);
5132  fill_opacity=(double) ((MagickRealType) QuantumRange-fill_opacity*
5133  ((MagickRealType) QuantumRange-(MagickRealType) fill_color.opacity));
5134  MagickCompositeOver(&fill_color,(MagickRealType) fill_opacity,q,
5135  (MagickRealType) q->opacity,q);
5136  (void) GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5137  &stroke_color);
5138  stroke_opacity=(double) ((MagickRealType) QuantumRange-stroke_opacity*
5139  ((MagickRealType) QuantumRange-(MagickRealType) stroke_color.opacity));
5140  MagickCompositeOver(&stroke_color,(MagickRealType) stroke_opacity,q,
5141  (MagickRealType) q->opacity,q);
5142  q++;
5143  }
5144  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5145  status=MagickFalse;
5146  }
5147  image_view=DestroyCacheView(image_view);
5148  polygon_info=DestroyPolygonTLS(polygon_info);
5149  if (draw_info->debug != MagickFalse)
5150  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
5151  return(status);
5152 }
5153 
5154 /*
5155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5156 % %
5157 % %
5158 % %
5159 % D r a w P r i m i t i v e %
5160 % %
5161 % %
5162 % %
5163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5164 %
5165 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
5166 %
5167 % The format of the DrawPrimitive method is:
5168 %
5169 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
5170 % PrimitiveInfo *primitive_info)
5171 %
5172 % A description of each parameter follows:
5173 %
5174 % o image: the image.
5175 %
5176 % o draw_info: the draw info.
5177 %
5178 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5179 %
5180 */
5181 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
5182 {
5183  const char
5184  *methods[] =
5185  {
5186  "point",
5187  "replace",
5188  "floodfill",
5189  "filltoborder",
5190  "reset",
5191  "?"
5192  };
5193 
5194  PointInfo
5195  p,
5196  q,
5197  point;
5198 
5199  ssize_t
5200  i,
5201  x;
5202 
5203  ssize_t
5204  coordinates,
5205  y;
5206 
5207  x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5208  y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5209  switch (primitive_info->primitive)
5210  {
5211  case PointPrimitive:
5212  {
5213  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5214  "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
5215  methods[primitive_info->method]);
5216  return;
5217  }
5218  case ColorPrimitive:
5219  {
5220  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5221  "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
5222  methods[primitive_info->method]);
5223  return;
5224  }
5225  case MattePrimitive:
5226  {
5227  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5228  "MattePrimitive %.20g,%.20g %s",(double) x,(double) y,
5229  methods[primitive_info->method]);
5230  return;
5231  }
5232  case TextPrimitive:
5233  {
5234  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5235  "TextPrimitive %.20g,%.20g",(double) x,(double) y);
5236  return;
5237  }
5238  case ImagePrimitive:
5239  {
5240  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5241  "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
5242  return;
5243  }
5244  default:
5245  break;
5246  }
5247  coordinates=0;
5248  p=primitive_info[0].point;
5249  q.x=(-1.0);
5250  q.y=(-1.0);
5251  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5252  {
5253  point=primitive_info[i].point;
5254  if (coordinates <= 0)
5255  {
5256  coordinates=(ssize_t) primitive_info[i].coordinates;
5257  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5258  " begin open (%.20g)",(double) coordinates);
5259  p=point;
5260  }
5261  point=primitive_info[i].point;
5262  if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5263  (fabs(q.y-point.y) >= MagickEpsilon))
5264  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5265  " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5266  else
5267  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5268  " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5269  q=point;
5270  coordinates--;
5271  if (coordinates > 0)
5272  continue;
5273  if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5274  (fabs(p.y-point.y) >= MagickEpsilon))
5275  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
5276  (double) coordinates);
5277  else
5278  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
5279  (double) coordinates);
5280  }
5281 }
5282 
5283 MagickExport MagickBooleanType DrawPrimitive(Image *image,
5284  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5285 {
5286  CacheView
5287  *image_view;
5288 
5290  *exception;
5291 
5292  MagickStatusType
5293  status;
5294 
5295  ssize_t
5296  i,
5297  x;
5298 
5299  ssize_t
5300  y;
5301 
5302  if (draw_info->debug != MagickFalse)
5303  {
5304  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5305  " begin draw-primitive");
5306  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5307  " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5308  draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5309  draw_info->affine.tx,draw_info->affine.ty);
5310  }
5311  exception=(&image->exception);
5312  status=MagickTrue;
5313  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5314  ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
5315  (IsPixelGray(&draw_info->stroke) == MagickFalse)))
5316  status=SetImageColorspace(image,sRGBColorspace);
5317  if (draw_info->compliance == SVGCompliance)
5318  {
5319  status&=SetImageClipMask(image,draw_info->clipping_mask);
5320  status&=SetImageMask(image,draw_info->composite_mask);
5321  }
5322  x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5323  y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5324  image_view=AcquireAuthenticCacheView(image,exception);
5325  switch (primitive_info->primitive)
5326  {
5327  case ColorPrimitive:
5328  {
5329  switch (primitive_info->method)
5330  {
5331  case PointMethod:
5332  default:
5333  {
5334  PixelPacket
5335  *q;
5336 
5337  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5338  if (q == (PixelPacket *) NULL)
5339  break;
5340  (void) GetFillColor(draw_info,x,y,q);
5341  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5342  break;
5343  }
5344  case ReplaceMethod:
5345  {
5346  PixelPacket
5347  target;
5348 
5349  status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5350  for (y=0; y < (ssize_t) image->rows; y++)
5351  {
5352  PixelPacket
5353  *magick_restrict q;
5354 
5355  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5356  exception);
5357  if (q == (PixelPacket *) NULL)
5358  break;
5359  for (x=0; x < (ssize_t) image->columns; x++)
5360  {
5361  if (IsColorSimilar(image,q,&target) == MagickFalse)
5362  {
5363  q++;
5364  continue;
5365  }
5366  (void) GetFillColor(draw_info,x,y,q);
5367  q++;
5368  }
5369  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5370  if (status == MagickFalse)
5371  break;
5372  }
5373  break;
5374  }
5375  case FloodfillMethod:
5376  case FillToBorderMethod:
5377  {
5379  target;
5380 
5381  status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5382  if (primitive_info->method == FillToBorderMethod)
5383  {
5384  target.red=(MagickRealType) draw_info->border_color.red;
5385  target.green=(MagickRealType) draw_info->border_color.green;
5386  target.blue=(MagickRealType) draw_info->border_color.blue;
5387  }
5388  status&=FloodfillPaintImage(image,DefaultChannels,draw_info,&target,x,
5389  y,primitive_info->method == FloodfillMethod ? MagickFalse :
5390  MagickTrue);
5391  break;
5392  }
5393  case ResetMethod:
5394  {
5395  for (y=0; y < (ssize_t) image->rows; y++)
5396  {
5397  PixelPacket
5398  *magick_restrict q;
5399 
5400  ssize_t
5401  x;
5402 
5403  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5404  exception);
5405  if (q == (PixelPacket *) NULL)
5406  break;
5407  for (x=0; x < (ssize_t) image->columns; x++)
5408  {
5409  (void) GetFillColor(draw_info,x,y,q);
5410  q++;
5411  }
5412  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5413  if (status == MagickFalse)
5414  break;
5415  }
5416  break;
5417  }
5418  }
5419  break;
5420  }
5421  case MattePrimitive:
5422  {
5423  if (image->matte == MagickFalse)
5424  status&=SetImageAlphaChannel(image,OpaqueAlphaChannel);
5425  switch (primitive_info->method)
5426  {
5427  case PointMethod:
5428  default:
5429  {
5430  PixelPacket
5431  pixel;
5432 
5433  PixelPacket
5434  *q;
5435 
5436  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5437  if (q == (PixelPacket *) NULL)
5438  break;
5439  (void) GetFillColor(draw_info,x,y,&pixel);
5440  SetPixelOpacity(q,pixel.opacity);
5441  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5442  break;
5443  }
5444  case ReplaceMethod:
5445  {
5446  PixelPacket
5447  pixel,
5448  target;
5449 
5450  status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5451  for (y=0; y < (ssize_t) image->rows; y++)
5452  {
5453  PixelPacket
5454  *magick_restrict q;
5455 
5456  ssize_t
5457  x;
5458 
5459  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5460  exception);
5461  if (q == (PixelPacket *) NULL)
5462  break;
5463  for (x=0; x < (ssize_t) image->columns; x++)
5464  {
5465  if (IsColorSimilar(image,q,&target) == MagickFalse)
5466  {
5467  q++;
5468  continue;
5469  }
5470  (void) GetFillColor(draw_info,x,y,&pixel);
5471  SetPixelOpacity(q,pixel.opacity);
5472  q++;
5473  }
5474  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5475  if (status == MagickFalse)
5476  break;
5477  }
5478  break;
5479  }
5480  case FloodfillMethod:
5481  case FillToBorderMethod:
5482  {
5484  target;
5485 
5486  status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5487  if (primitive_info->method == FillToBorderMethod)
5488  {
5489  target.red=(MagickRealType) draw_info->border_color.red;
5490  target.green=(MagickRealType) draw_info->border_color.green;
5491  target.blue=(MagickRealType) draw_info->border_color.blue;
5492  }
5493  status&=FloodfillPaintImage(image,OpacityChannel,draw_info,&target,x,
5494  y,primitive_info->method == FloodfillMethod ? MagickFalse :
5495  MagickTrue);
5496  break;
5497  }
5498  case ResetMethod:
5499  {
5500  PixelPacket
5501  pixel;
5502 
5503  for (y=0; y < (ssize_t) image->rows; y++)
5504  {
5505  PixelPacket
5506  *magick_restrict q;
5507 
5508  ssize_t
5509  x;
5510 
5511  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5512  exception);
5513  if (q == (PixelPacket *) NULL)
5514  break;
5515  for (x=0; x < (ssize_t) image->columns; x++)
5516  {
5517  (void) GetFillColor(draw_info,x,y,&pixel);
5518  SetPixelOpacity(q,pixel.opacity);
5519  q++;
5520  }
5521  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5522  if (status == MagickFalse)
5523  break;
5524  }
5525  break;
5526  }
5527  }
5528  break;
5529  }
5530  case ImagePrimitive:
5531  {
5532  AffineMatrix
5533  affine;
5534 
5535  char
5536  composite_geometry[MaxTextExtent],
5537  magic[MagickPathExtent] = {'\0'};
5538 
5539  Image
5540  *composite_image,
5541  *composite_images;
5542 
5543  ImageInfo
5544  *clone_info;
5545 
5547  geometry;
5548 
5549  ssize_t
5550  x1,
5551  y1;
5552 
5553  if (primitive_info->text == (char *) NULL)
5554  break;
5555  clone_info=AcquireImageInfo();
5556  composite_images=(Image *) NULL;
5557  if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5558  composite_images=ReadInlineImage(clone_info,primitive_info->text,
5559  &image->exception);
5560  else
5561  if (*primitive_info->text != '\0')
5562  {
5563  /*
5564  Read composite image.
5565  */
5566  (void) CopyMagickString(clone_info->filename,primitive_info->text,
5567  MagickPathExtent);
5568  (void) SetImageInfo(clone_info,1,exception);
5569  (void) CopyMagickString(clone_info->filename,primitive_info->text,
5570  MagickPathExtent);
5571  if (clone_info->size != (char *) NULL)
5572  clone_info->size=DestroyString(clone_info->size);
5573  if (clone_info->extract != (char *) NULL)
5574  clone_info->extract=DestroyString(clone_info->extract);
5575  GetPathComponent(clone_info->filename,MagickPath,magic);
5576  if (*magic == '\0')
5577  composite_images=ReadImage(clone_info,exception);
5578  else
5579  (void) ThrowMagickException(exception,GetMagickModule(),
5580  FileOpenError,"UnableToOpenFile","`%s'",clone_info->filename);
5581  }
5582  clone_info=DestroyImageInfo(clone_info);
5583  if (composite_images == (Image *) NULL)
5584  {
5585  status=0;
5586  break;
5587  }
5588  composite_image=RemoveFirstImageFromList(&composite_images);
5589  composite_images=DestroyImageList(composite_images);
5590  (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5591  NULL,(void *) NULL);
5592  x1=CastDoubleToLong(ceil(primitive_info[1].point.x-0.5));
5593  y1=CastDoubleToLong(ceil(primitive_info[1].point.y-0.5));
5594  if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5595  ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5596  {
5597  char
5598  geometry[MaxTextExtent];
5599 
5600  /*
5601  Resize image.
5602  */
5603  (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
5604  primitive_info[1].point.x,primitive_info[1].point.y);
5605  composite_image->filter=image->filter;
5606  status&=TransformImage(&composite_image,(char *) NULL,geometry);
5607  }
5608  if (composite_image->matte == MagickFalse)
5609  status&=SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
5610  if (draw_info->opacity != OpaqueOpacity)
5611  status&=SetImageOpacity(composite_image,draw_info->opacity);
5612  SetGeometry(image,&geometry);
5613  image->gravity=draw_info->gravity;
5614  geometry.x=x;
5615  geometry.y=y;
5616  (void) FormatLocaleString(composite_geometry,MaxTextExtent,
5617  "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
5618  composite_image->rows,(double) geometry.x,(double) geometry.y);
5619  (void) ParseGravityGeometry(image,composite_geometry,&geometry,
5620  &image->exception);
5621  affine=draw_info->affine;
5622  affine.tx=(double) geometry.x;
5623  affine.ty=(double) geometry.y;
5624  composite_image->interpolate=image->interpolate;
5625  if ((draw_info->compose == OverCompositeOp) ||
5626  (draw_info->compose == SrcOverCompositeOp))
5627  status&=DrawAffineImage(image,composite_image,&affine);
5628  else
5629  status&=CompositeImage(image,draw_info->compose,composite_image,
5630  geometry.x,geometry.y);
5631  composite_image=DestroyImage(composite_image);
5632  break;
5633  }
5634  case PointPrimitive:
5635  {
5636  PixelPacket
5637  fill_color;
5638 
5639  PixelPacket
5640  *q;
5641 
5642  if ((y < 0) || (y >= (ssize_t) image->rows))
5643  break;
5644  if ((x < 0) || (x >= (ssize_t) image->columns))
5645  break;
5646  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5647  if (q == (PixelPacket *) NULL)
5648  break;
5649  (void) GetFillColor(draw_info,x,y,&fill_color);
5650  MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
5651  (MagickRealType) q->opacity,q);
5652  status&=SyncCacheViewAuthenticPixels(image_view,exception);
5653  break;
5654  }
5655  case TextPrimitive:
5656  {
5657  char
5658  geometry[MaxTextExtent];
5659 
5660  DrawInfo
5661  *clone_info;
5662 
5663  if (primitive_info->text == (char *) NULL)
5664  break;
5665  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5666  (void) CloneString(&clone_info->text,primitive_info->text);
5667  (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
5668  primitive_info->point.x,primitive_info->point.y);
5669  (void) CloneString(&clone_info->geometry,geometry);
5670  status&=AnnotateImage(image,clone_info);
5671  clone_info=DestroyDrawInfo(clone_info);
5672  break;
5673  }
5674  default:
5675  {
5676  double
5677  mid,
5678  scale;
5679 
5680  DrawInfo
5681  *clone_info;
5682 
5683  if (IsEventLogging() != MagickFalse)
5684  LogPrimitiveInfo(primitive_info);
5685  scale=ExpandAffine(&draw_info->affine);
5686  if ((draw_info->dash_pattern != (double *) NULL) &&
5687  (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5688  (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5689  (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
5690  {
5691  /*
5692  Draw dash polygon.
5693  */
5694  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5695  clone_info->stroke_width=0.0;
5696  clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5697  status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5698  clone_info=DestroyDrawInfo(clone_info);
5699  if (status != MagickFalse)
5700  status&=DrawDashPolygon(draw_info,primitive_info,image);
5701  break;
5702  }
5703  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5704  if ((mid > 1.0) &&
5705  ((draw_info->stroke.opacity != (Quantum) TransparentOpacity) ||
5706  (draw_info->stroke_pattern != (Image *) NULL)))
5707  {
5708  double
5709  x,
5710  y;
5711 
5712  MagickBooleanType
5713  closed_path;
5714 
5715  /*
5716  Draw strokes while respecting line cap/join attributes.
5717  */
5718  closed_path=primitive_info[0].closed_subpath;
5719  i=(ssize_t) primitive_info[0].coordinates;
5720  x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5721  y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5722  if ((x < MagickEpsilon) && (y < MagickEpsilon))
5723  closed_path=MagickTrue;
5724  if ((((draw_info->linecap == RoundCap) ||
5725  (closed_path != MagickFalse)) &&
5726  (draw_info->linejoin == RoundJoin)) ||
5727  (primitive_info[i].primitive != UndefinedPrimitive))
5728  {
5729  status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5730  break;
5731  }
5732  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5733  clone_info->stroke_width=0.0;
5734  clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5735  status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5736  clone_info=DestroyDrawInfo(clone_info);
5737  if (status != MagickFalse)
5738  status&=DrawStrokePolygon(image,draw_info,primitive_info);
5739  break;
5740  }
5741  status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5742  break;
5743  }
5744  }
5745  image_view=DestroyCacheView(image_view);
5746  if (draw_info->compliance == SVGCompliance)
5747  {
5748  status&=SetImageClipMask(image,(Image *) NULL);
5749  status&=SetImageMask(image,(Image *) NULL);
5750  }
5751  if (draw_info->debug != MagickFalse)
5752  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
5753  return(status != 0 ? MagickTrue : MagickFalse);
5754 }
5755 
5756 /*
5757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5758 % %
5759 % %
5760 % %
5761 + D r a w S t r o k e P o l y g o n %
5762 % %
5763 % %
5764 % %
5765 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5766 %
5767 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
5768 % the image while respecting the line cap and join attributes.
5769 %
5770 % The format of the DrawStrokePolygon method is:
5771 %
5772 % MagickBooleanType DrawStrokePolygon(Image *image,
5773 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5774 %
5775 % A description of each parameter follows:
5776 %
5777 % o image: the image.
5778 %
5779 % o draw_info: the draw info.
5780 %
5781 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5782 %
5783 %
5784 */
5785 
5786 static MagickBooleanType DrawRoundLinecap(Image *image,
5787  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5788 {
5790  linecap[5];
5791 
5792  ssize_t
5793  i;
5794 
5795  if (primitive_info->coordinates < 1)
5796  return(MagickFalse);
5797  for (i=0; i < 4; i++)
5798  linecap[i]=(*primitive_info);
5799  linecap[0].coordinates=4;
5800  linecap[1].point.x+=2.0*MagickEpsilon;
5801  linecap[2].point.x+=2.0*MagickEpsilon;
5802  linecap[2].point.y+=2.0*MagickEpsilon;
5803  linecap[3].point.y+=2.0*MagickEpsilon;
5804  linecap[4].primitive=UndefinedPrimitive;
5805  return(DrawPolygonPrimitive(image,draw_info,linecap));
5806 }
5807 
5808 static MagickBooleanType DrawStrokePolygon(Image *image,
5809  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5810 {
5811  DrawInfo
5812  *clone_info;
5813 
5814  MagickBooleanType
5815  closed_path;
5816 
5817  MagickStatusType
5818  status;
5819 
5821  *stroke_polygon;
5822 
5823  const PrimitiveInfo
5824  *p,
5825  *q;
5826 
5827  /*
5828  Draw stroked polygon.
5829  */
5830  if (draw_info->debug != MagickFalse)
5831  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5832  " begin draw-stroke-polygon");
5833  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5834  clone_info->fill=draw_info->stroke;
5835  if (clone_info->fill_pattern != (Image *) NULL)
5836  clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5837  if (clone_info->stroke_pattern != (Image *) NULL)
5838  clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5839  MagickTrue,&clone_info->stroke_pattern->exception);
5840  clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5841  clone_info->stroke_width=0.0;
5842  clone_info->fill_rule=NonZeroRule;
5843  status=MagickTrue;
5844  for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
5845  {
5846  if (p->coordinates == 1)
5847  continue;
5848  stroke_polygon=TraceStrokePolygon(draw_info,p,&image->exception);
5849  if (stroke_polygon == (PrimitiveInfo *) NULL)
5850  {
5851  status=0;
5852  break;
5853  }
5854  status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
5855  stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5856  if (status == 0)
5857  break;
5858  q=p+p->coordinates-1;
5859  closed_path=p->closed_subpath;
5860  if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5861  {
5862  status&=DrawRoundLinecap(image,draw_info,p);
5863  status&=DrawRoundLinecap(image,draw_info,q);
5864  }
5865  }
5866  clone_info=DestroyDrawInfo(clone_info);
5867  if (draw_info->debug != MagickFalse)
5868  (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5869  " end draw-stroke-polygon");
5870  return(status != 0 ? MagickTrue : MagickFalse);
5871 }
5872 
5873 /*
5874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5875 % %
5876 % %
5877 % %
5878 % G e t A f f i n e M a t r i x %
5879 % %
5880 % %
5881 % %
5882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5883 %
5884 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
5885 % matrix.
5886 %
5887 % The format of the GetAffineMatrix method is:
5888 %
5889 % void GetAffineMatrix(AffineMatrix *affine_matrix)
5890 %
5891 % A description of each parameter follows:
5892 %
5893 % o affine_matrix: the affine matrix.
5894 %
5895 */
5896 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
5897 {
5898  assert(affine_matrix != (AffineMatrix *) NULL);
5899  if (IsEventLogging() != MagickFalse)
5900  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5901  (void) memset(affine_matrix,0,sizeof(*affine_matrix));
5902  affine_matrix->sx=1.0;
5903  affine_matrix->sy=1.0;
5904 }
5905 
5906 /*
5907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5908 % %
5909 % %
5910 % %
5911 + G e t D r a w I n f o %
5912 % %
5913 % %
5914 % %
5915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5916 %
5917 % GetDrawInfo() initializes draw_info to default values from image_info.
5918 %
5919 % The format of the GetDrawInfo method is:
5920 %
5921 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5922 %
5923 % A description of each parameter follows:
5924 %
5925 % o image_info: the image info..
5926 %
5927 % o draw_info: the draw info.
5928 %
5929 */
5930 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5931 {
5932  char
5933  *next_token;
5934 
5935  const char
5936  *option;
5937 
5939  *exception;
5940 
5941  /*
5942  Initialize draw attributes.
5943  */
5944  assert(draw_info != (DrawInfo *) NULL);
5945  if (IsEventLogging() != MagickFalse)
5946  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5947  (void) memset(draw_info,0,sizeof(*draw_info));
5948  draw_info->image_info=CloneImageInfo(image_info);
5949  GetAffineMatrix(&draw_info->affine);
5950  exception=AcquireExceptionInfo();
5951  (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
5952  (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
5953  draw_info->stroke_antialias=draw_info->image_info->antialias;
5954  draw_info->stroke_width=1.0;
5955  draw_info->fill_rule=EvenOddRule;
5956  draw_info->opacity=OpaqueOpacity;
5957  draw_info->fill_opacity=OpaqueOpacity;
5958  draw_info->stroke_opacity=OpaqueOpacity;
5959  draw_info->linecap=ButtCap;
5960  draw_info->linejoin=MiterJoin;
5961  draw_info->miterlimit=10;
5962  draw_info->decorate=NoDecoration;
5963  if (draw_info->image_info->font != (char *) NULL)
5964  draw_info->font=AcquireString(draw_info->image_info->font);
5965  if (draw_info->image_info->density != (char *) NULL)
5966  draw_info->density=AcquireString(draw_info->image_info->density);
5967  draw_info->text_antialias=draw_info->image_info->antialias;
5968  draw_info->pointsize=12.0;
5969  if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
5970  draw_info->pointsize=draw_info->image_info->pointsize;
5971  draw_info->undercolor.opacity=(Quantum) TransparentOpacity;
5972  draw_info->border_color=draw_info->image_info->border_color;
5973  draw_info->compose=OverCompositeOp;
5974  if (draw_info->image_info->server_name != (char *) NULL)
5975  draw_info->server_name=AcquireString(draw_info->image_info->server_name);
5976  draw_info->render=MagickTrue;
5977  draw_info->clip_path=MagickFalse;
5978  draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
5979  MagickTrue : MagickFalse;
5980  option=GetImageOption(draw_info->image_info,"direction");
5981  if (option != (const char *) NULL)
5982  draw_info->direction=(DirectionType) ParseCommandOption(
5983  MagickDirectionOptions,MagickFalse,option);
5984  else
5985  draw_info->direction=UndefinedDirection;
5986  option=GetImageOption(draw_info->image_info,"encoding");
5987  if (option != (const char *) NULL)
5988  (void) CloneString(&draw_info->encoding,option);
5989  option=GetImageOption(draw_info->image_info,"family");
5990  if (option != (const char *) NULL)
5991  (void) CloneString(&draw_info->family,option);
5992  option=GetImageOption(draw_info->image_info,"fill");
5993  if (option != (const char *) NULL)
5994  (void) QueryColorDatabase(option,&draw_info->fill,exception);
5995  option=GetImageOption(draw_info->image_info,"gravity");
5996  if (option != (const char *) NULL)
5997  draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
5998  MagickFalse,option);
5999  option=GetImageOption(draw_info->image_info,"interline-spacing");
6000  if (option != (const char *) NULL)
6001  draw_info->interline_spacing=GetDrawValue(option,&next_token);
6002  option=GetImageOption(draw_info->image_info,"interword-spacing");
6003  if (option != (const char *) NULL)
6004  draw_info->interword_spacing=GetDrawValue(option,&next_token);
6005  option=GetImageOption(draw_info->image_info,"kerning");
6006  if (option != (const char *) NULL)
6007  draw_info->kerning=GetDrawValue(option,&next_token);
6008  option=GetImageOption(draw_info->image_info,"stroke");
6009  if (option != (const char *) NULL)
6010  (void) QueryColorDatabase(option,&draw_info->stroke,exception);
6011  option=GetImageOption(draw_info->image_info,"strokewidth");
6012  if (option != (const char *) NULL)
6013  draw_info->stroke_width=GetDrawValue(option,&next_token);
6014  option=GetImageOption(draw_info->image_info,"style");
6015  if (option != (const char *) NULL)
6016  draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6017  MagickFalse,option);
6018  option=GetImageOption(draw_info->image_info,"undercolor");
6019  if (option != (const char *) NULL)
6020  (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
6021  option=GetImageOption(draw_info->image_info,"weight");
6022  if (option != (const char *) NULL)
6023  {
6024  ssize_t
6025  weight;
6026 
6027  weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6028  if (weight == -1)
6029  weight=(ssize_t) StringToUnsignedLong(option);
6030  draw_info->weight=(size_t) weight;
6031  }
6032  exception=DestroyExceptionInfo(exception);
6033  draw_info->signature=MagickCoreSignature;
6034 }
6035 
6036 /*
6037 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6038 % %
6039 % %
6040 % %
6041 + P e r m u t a t e %
6042 % %
6043 % %
6044 % %
6045 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6046 %
6047 % Permutate() returns the permutation of the (n,k).
6048 %
6049 % The format of the Permutate method is:
6050 %
6051 % void Permutate(ssize_t n,ssize_t k)
6052 %
6053 % A description of each parameter follows:
6054 %
6055 % o n:
6056 %
6057 % o k:
6058 %
6059 %
6060 */
6061 static inline double Permutate(const ssize_t n,const ssize_t k)
6062 {
6063  double
6064  r;
6065 
6066  ssize_t
6067  i;
6068 
6069  r=1.0;
6070  for (i=k+1; i <= n; i++)
6071  r*=i;
6072  for (i=1; i <= (n-k); i++)
6073  r/=i;
6074  return(r);
6075 }
6076 
6077 /*
6078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6079 % %
6080 % %
6081 % %
6082 + T r a c e P r i m i t i v e %
6083 % %
6084 % %
6085 % %
6086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6087 %
6088 % TracePrimitive is a collection of methods for generating graphic
6089 % primitives such as arcs, ellipses, paths, etc.
6090 %
6091 */
6092 
6093 static MagickBooleanType TraceArc(MVGInfo *mvg_info,const PointInfo start,
6094  const PointInfo end,const PointInfo degrees)
6095 {
6096  PointInfo
6097  center,
6098  radius;
6099 
6100  center.x=0.5*(end.x+start.x);
6101  center.y=0.5*(end.y+start.y);
6102  radius.x=fabs(center.x-start.x);
6103  radius.y=fabs(center.y-start.y);
6104  return(TraceEllipse(mvg_info,center,radius,degrees));
6105 }
6106 
6107 static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,const PointInfo start,
6108  const PointInfo end,const PointInfo arc,const double angle,
6109  const MagickBooleanType large_arc,const MagickBooleanType sweep)
6110 {
6111  double
6112  alpha,
6113  beta,
6114  delta,
6115  factor,
6116  gamma,
6117  theta;
6118 
6119  MagickStatusType
6120  status;
6121 
6122  PointInfo
6123  center,
6124  points[3],
6125  radii;
6126 
6127  double
6128  cosine,
6129  sine;
6130 
6132  *primitive_info;
6133 
6135  *p;
6136 
6137  ssize_t
6138  i;
6139 
6140  size_t
6141  arc_segments;
6142 
6143  ssize_t
6144  offset;
6145 
6146  offset=mvg_info->offset;
6147  primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6148  primitive_info->coordinates=0;
6149  if ((fabs(start.x-end.x) < MagickEpsilon) &&
6150  (fabs(start.y-end.y) < MagickEpsilon))
6151  return(TracePoint(primitive_info,end));
6152  radii.x=fabs(arc.x);
6153  radii.y=fabs(arc.y);
6154  if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6155  return(TraceLine(primitive_info,start,end));
6156  cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
6157  sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
6158  center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6159  center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6160  delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6161  (radii.y*radii.y);
6162  if (delta < MagickEpsilon)
6163  return(TraceLine(primitive_info,start,end));
6164  if (delta > 1.0)
6165  {
6166  radii.x*=sqrt((double) delta);
6167  radii.y*=sqrt((double) delta);
6168  }
6169  points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6170  points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6171  points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6172  points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6173  alpha=points[1].x-points[0].x;
6174  beta=points[1].y-points[0].y;
6175  if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6176  return(TraceLine(primitive_info,start,end));
6177  factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6178  if (factor <= 0.0)
6179  factor=0.0;
6180  else
6181  {
6182  factor=sqrt((double) factor);
6183  if (sweep == large_arc)
6184  factor=(-factor);
6185  }
6186  center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6187  center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6188  alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6189  theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6190  if ((theta < 0.0) && (sweep != MagickFalse))
6191  theta+=2.0*MagickPI;
6192  else
6193  if ((theta > 0.0) && (sweep == MagickFalse))
6194  theta-=2.0*MagickPI;
6195  arc_segments=(size_t) CastDoubleToLong(ceil(fabs((double) (theta/(0.5*
6196  MagickPI+MagickEpsilon)))));
6197  p=primitive_info;
6198  status=MagickTrue;
6199  for (i=0; i < (ssize_t) arc_segments; i++)
6200  {
6201  beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6202  gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
6203  sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
6204  sin(fmod((double) beta,DegreesToRadians(360.0)));
6205  points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
6206  arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
6207  (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6208  points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
6209  arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
6210  (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6211  points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
6212  theta/arc_segments),DegreesToRadians(360.0))));
6213  points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
6214  theta/arc_segments),DegreesToRadians(360.0))));
6215  points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
6216  (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6217  points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
6218  (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6219  p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6220  p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6221  (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6222  points[0].y);
6223  (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6224  points[0].y);
6225  (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6226  points[1].y);
6227  (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6228  points[1].y);
6229  (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6230  points[2].y);
6231  (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6232  points[2].y);
6233  if (i == (ssize_t) (arc_segments-1))
6234  (p+3)->point=end;
6235  status&=TraceBezier(mvg_info,4);
6236  if (status == 0)
6237  break;
6238  p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6239  mvg_info->offset+=p->coordinates;
6240  p+=(ptrdiff_t) p->coordinates;
6241  }
6242  if (status == 0)
6243  return(MagickFalse);
6244  mvg_info->offset=offset;
6245  primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6246  primitive_info->coordinates=(size_t) (p-primitive_info);
6247  primitive_info->closed_subpath=MagickFalse;
6248  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6249  {
6250  p->primitive=primitive_info->primitive;
6251  p--;
6252  }
6253  return(MagickTrue);
6254 }
6255 
6256 static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6257  const size_t number_coordinates)
6258 {
6259  double
6260  alpha,
6261  *coefficients,
6262  weight;
6263 
6264  PointInfo
6265  end,
6266  point,
6267  *points;
6268 
6270  *primitive_info;
6271 
6273  *p;
6274 
6275  ssize_t
6276  i,
6277  j;
6278 
6279  size_t
6280  control_points,
6281  quantum;
6282 
6283  /*
6284  Allocate coefficients.
6285  */
6286  primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6287  quantum=number_coordinates;
6288  for (i=0; i < (ssize_t) number_coordinates; i++)
6289  {
6290  for (j=i+1; j < (ssize_t) number_coordinates; j++)
6291  {
6292  alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6293  if (alpha > (double) GetMaxMemoryRequest())
6294  {
6295  (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6296  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6297  return(MagickFalse);
6298  }
6299  if (alpha > (double) quantum)
6300  quantum=(size_t) alpha;
6301  alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6302  if (alpha > (double) quantum)
6303  quantum=(size_t) alpha;
6304  }
6305  }
6306  primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6307  quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6308  if (quantum > (double) GetMaxMemoryRequest())
6309  {
6310  (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6311  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6312  return(MagickFalse);
6313  }
6314  coefficients=(double *) AcquireQuantumMemory(number_coordinates,
6315  sizeof(*coefficients));
6316  points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6317  sizeof(*points));
6318  if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
6319  {
6320  if (points != (PointInfo *) NULL)
6321  points=(PointInfo *) RelinquishMagickMemory(points);
6322  if (coefficients != (double *) NULL)
6323  coefficients=(double *) RelinquishMagickMemory(coefficients);
6324  (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6325  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6326  return(MagickFalse);
6327  }
6328  control_points=quantum*number_coordinates;
6329  if (CheckPrimitiveExtent(mvg_info,(double) control_points+1) == MagickFalse)
6330  {
6331  points=(PointInfo *) RelinquishMagickMemory(points);
6332  coefficients=(double *) RelinquishMagickMemory(coefficients);
6333  return(MagickFalse);
6334  }
6335  primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6336  /*
6337  Compute bezier points.
6338  */
6339  end=primitive_info[number_coordinates-1].point;
6340  for (i=0; i < (ssize_t) number_coordinates; i++)
6341  coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6342  weight=0.0;
6343  for (i=0; i < (ssize_t) control_points; i++)
6344  {
6345  p=primitive_info;
6346  point.x=0.0;
6347  point.y=0.0;
6348  alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
6349  for (j=0; j < (ssize_t) number_coordinates; j++)
6350  {
6351  point.x+=alpha*coefficients[j]*p->point.x;
6352  point.y+=alpha*coefficients[j]*p->point.y;
6353  alpha*=weight/(1.0-weight);
6354  p++;
6355  }
6356  points[i]=point;
6357  weight+=1.0/control_points;
6358  }
6359  /*
6360  Bezier curves are just short segmented polys.
6361  */
6362  p=primitive_info;
6363  for (i=0; i < (ssize_t) control_points; i++)
6364  {
6365  if (TracePoint(p,points[i]) == MagickFalse)
6366  {
6367  points=(PointInfo *) RelinquishMagickMemory(points);
6368  coefficients=(double *) RelinquishMagickMemory(coefficients);
6369  return(MagickFalse);
6370  }
6371  p+=(ptrdiff_t) p->coordinates;
6372  }
6373  if (TracePoint(p,end) == MagickFalse)
6374  {
6375  points=(PointInfo *) RelinquishMagickMemory(points);
6376  coefficients=(double *) RelinquishMagickMemory(coefficients);
6377  return(MagickFalse);
6378  }
6379  p+=(ptrdiff_t) p->coordinates;
6380  primitive_info->coordinates=(size_t) (p-primitive_info);
6381  primitive_info->closed_subpath=MagickFalse;
6382  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6383  {
6384  p->primitive=primitive_info->primitive;
6385  p--;
6386  }
6387  points=(PointInfo *) RelinquishMagickMemory(points);
6388  coefficients=(double *) RelinquishMagickMemory(coefficients);
6389  return(MagickTrue);
6390 }
6391 
6392 static MagickBooleanType TraceCircle(MVGInfo *mvg_info,const PointInfo start,
6393  const PointInfo end)
6394 {
6395  double
6396  alpha,
6397  beta,
6398  radius;
6399 
6400  PointInfo
6401  offset,
6402  degrees;
6403 
6404  alpha=end.x-start.x;
6405  beta=end.y-start.y;
6406  radius=hypot((double) alpha,(double) beta);
6407  offset.x=(double) radius;
6408  offset.y=(double) radius;
6409  degrees.x=0.0;
6410  degrees.y=360.0;
6411  return(TraceEllipse(mvg_info,start,offset,degrees));
6412 }
6413 
6414 static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,const PointInfo center,
6415  const PointInfo radii,const PointInfo arc)
6416 {
6417  double
6418  coordinates,
6419  delta,
6420  step,
6421  x,
6422  y;
6423 
6424  PointInfo
6425  angle,
6426  point;
6427 
6429  *primitive_info;
6430 
6432  *p;
6433 
6434  ssize_t
6435  i;
6436 
6437  /*
6438  Ellipses are just short segmented polys.
6439  */
6440  primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6441  primitive_info->coordinates=0;
6442  if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6443  return(MagickTrue);
6444  delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6445  step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6446  angle.x=DegreesToRadians(arc.x);
6447  y=arc.y;
6448  while (y < arc.x)
6449  y+=360.0;
6450  angle.y=DegreesToRadians(y);
6451  coordinates=ceil((angle.y-angle.x)/step+1.0);
6452  if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6453  return(MagickFalse);
6454  i=0;
6455  primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6456  for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6457  {
6458  point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6459  point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6460  if (i++ >= (ssize_t) coordinates)
6461  break;
6462  if (TracePoint(p,point) == MagickFalse)
6463  return(MagickFalse);
6464  p+=(ptrdiff_t) p->coordinates;
6465  }
6466  point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6467  point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6468  if (TracePoint(p,point) == MagickFalse)
6469  return(MagickFalse);
6470  p+=(ptrdiff_t) p->coordinates;
6471  primitive_info->coordinates=(size_t) (p-primitive_info);
6472  primitive_info->closed_subpath=MagickFalse;
6473  x=fabs(primitive_info[0].point.x-
6474  primitive_info[primitive_info->coordinates-1].point.x);
6475  y=fabs(primitive_info[0].point.y-
6476  primitive_info[primitive_info->coordinates-1].point.y);
6477  if ((x < MagickEpsilon) && (y < MagickEpsilon))
6478  primitive_info->closed_subpath=MagickTrue;
6479  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6480  {
6481  p->primitive=primitive_info->primitive;
6482  p--;
6483  }
6484  return(MagickTrue);
6485 }
6486 
6487 static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6488  const PointInfo start,const PointInfo end)
6489 {
6490  if (TracePoint(primitive_info,start) == MagickFalse)
6491  return(MagickFalse);
6492  if (TracePoint(primitive_info+1,end) == MagickFalse)
6493  return(MagickFalse);
6494  (primitive_info+1)->primitive=primitive_info->primitive;
6495  primitive_info->coordinates=2;
6496  primitive_info->closed_subpath=MagickFalse;
6497  return(MagickTrue);
6498 }
6499 
6500 static ssize_t TracePath(Image *image,MVGInfo *mvg_info,const char *path)
6501 {
6502  char
6503  *next_token,
6504  token[MaxTextExtent] = "";
6505 
6506  const char
6507  *p;
6508 
6509  double
6510  x,
6511  y;
6512 
6513  int
6514  attribute,
6515  last_attribute;
6516 
6517  MagickStatusType
6518  status;
6519 
6520  PointInfo
6521  end = {0.0, 0.0},
6522  points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6523  point = {0.0, 0.0},
6524  start = {0.0, 0.0};
6525 
6527  *primitive_info;
6528 
6529  PrimitiveType
6530  primitive_type;
6531 
6533  *q;
6534 
6535  ssize_t
6536  i;
6537 
6538  size_t
6539  number_coordinates,
6540  z_count;
6541 
6542  ssize_t
6543  subpath_offset;
6544 
6545  subpath_offset=mvg_info->offset;
6546  primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6547  status=MagickTrue;
6548  attribute=0;
6549  number_coordinates=0;
6550  z_count=0;
6551  *token='\0';
6552  primitive_type=primitive_info->primitive;
6553  q=primitive_info;
6554  for (p=path; *p != '\0'; )
6555  {
6556  if (status == MagickFalse)
6557  break;
6558  while (isspace((int) ((unsigned char) *p)) != 0)
6559  p++;
6560  if (*p == '\0')
6561  break;
6562  last_attribute=attribute;
6563  attribute=(int) (*p++);
6564  switch (attribute)
6565  {
6566  case 'a':
6567  case 'A':
6568  {
6569  double
6570  angle = 0.0;
6571 
6572  MagickBooleanType
6573  large_arc = MagickFalse,
6574  sweep = MagickFalse;
6575 
6576  PointInfo
6577  arc = {0.0, 0.0};
6578 
6579  /*
6580  Elliptical arc.
6581  */
6582  do
6583  {
6584  (void) GetNextToken(p,&p,MaxTextExtent,token);
6585  if (*token == ',')
6586  (void) GetNextToken(p,&p,MaxTextExtent,token);
6587  arc.x=GetDrawValue(token,&next_token);
6588  if (token == next_token)
6589  ThrowPointExpectedException(image,token);
6590  (void) GetNextToken(p,&p,MaxTextExtent,token);
6591  if (*token == ',')
6592  (void) GetNextToken(p,&p,MaxTextExtent,token);
6593  arc.y=GetDrawValue(token,&next_token);
6594  if (token == next_token)
6595  ThrowPointExpectedException(image,token);
6596  (void) GetNextToken(p,&p,MaxTextExtent,token);
6597  if (*token == ',')
6598  (void) GetNextToken(p,&p,MaxTextExtent,token);
6599  angle=GetDrawValue(token,&next_token);
6600  if (token == next_token)
6601  ThrowPointExpectedException(image,token);
6602  (void) GetNextToken(p,&p,MaxTextExtent,token);
6603  if (*token == ',')
6604  (void) GetNextToken(p,&p,MaxTextExtent,token);
6605  large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6606  (void) GetNextToken(p,&p,MaxTextExtent,token);
6607  if (*token == ',')
6608  (void) GetNextToken(p,&p,MaxTextExtent,token);
6609  sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6610  if (*token == ',')
6611  (void) GetNextToken(p,&p,MaxTextExtent,token);
6612  (void) GetNextToken(p,&p,MaxTextExtent,token);
6613  if (*token == ',')
6614  (void) GetNextToken(p,&p,MaxTextExtent,token);
6615  x=GetDrawValue(token,&next_token);
6616  if (token == next_token)
6617  ThrowPointExpectedException(image,token);
6618  (void) GetNextToken(p,&p,MaxTextExtent,token);
6619  if (*token == ',')
6620  (void) GetNextToken(p,&p,MaxTextExtent,token);
6621  y=GetDrawValue(token,&next_token);
6622  if (token == next_token)
6623  ThrowPointExpectedException(image,token);
6624  end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
6625  end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
6626  status&=TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep);
6627  q=(*mvg_info->primitive_info)+mvg_info->offset;
6628  mvg_info->offset+=q->coordinates;
6629  q+=(ptrdiff_t) q->coordinates;
6630  point=end;
6631  while (isspace((int) ((unsigned char) *p)) != 0)
6632  p++;
6633  if (*p == ',')
6634  p++;
6635  } while (IsPoint(p) != MagickFalse);
6636  break;
6637  }
6638  case 'c':
6639  case 'C':
6640  {
6641  /*
6642  Cubic Bézier curve.
6643  */
6644  do
6645  {
6646  points[0]=point;
6647  for (i=1; i < 4; i++)
6648  {
6649  (void) GetNextToken(p,&p,MaxTextExtent,token);
6650  if (*token == ',')
6651  (void) GetNextToken(p,&p,MaxTextExtent,token);
6652  x=GetDrawValue(token,&next_token);
6653  if (token == next_token)
6654  ThrowPointExpectedException(image,token);
6655  (void) GetNextToken(p,&p,MaxTextExtent,token);
6656  if (*token == ',')
6657  (void) GetNextToken(p,&p,MaxTextExtent,token);
6658  y=GetDrawValue(token,&next_token);
6659  if (token == next_token)
6660  ThrowPointExpectedException(image,token);
6661  end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
6662  end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
6663  points[i]=end;
6664  }
6665  for (i=0; i < 4; i++)
6666  (q+i)->point=points[i];
6667  if (TraceBezier(mvg_info,4) == MagickFalse)
6668  return(-1);
6669  q=(*mvg_info->primitive_info)+mvg_info->offset;
6670  mvg_info->offset+=q->coordinates;
6671  q+=(ptrdiff_t) q->coordinates;
6672  point=end;
6673  while (isspace((int) ((unsigned char) *p)) != 0)
6674  p++;
6675  if (*p == ',')
6676  p++;
6677  } while (IsPoint(p) != MagickFalse);
6678  break;
6679  }
6680  case 'H':
6681  case 'h':
6682  {
6683  do
6684  {
6685  (void) GetNextToken(p,&p,MaxTextExtent,token);
6686  if (*token == ',')
6687  (void) GetNextToken(p,&p,MaxTextExtent,token);
6688  x=GetDrawValue(token,&next_token);
6689  if (token == next_token)
6690  ThrowPointExpectedException(image,token);
6691  point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
6692  if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6693  return(-1);
6694  q=(*mvg_info->primitive_info)+mvg_info->offset;
6695  if (TracePoint(q,point) == MagickFalse)
6696  return(-1);
6697  mvg_info->offset+=q->coordinates;
6698  q+=(ptrdiff_t) q->coordinates;
6699  while (isspace((int) ((unsigned char) *p)) != 0)
6700  p++;
6701  if (*p == ',')
6702  p++;
6703  } while (IsPoint(p) != MagickFalse);
6704  break;
6705  }
6706  case 'l':
6707  case 'L':
6708  {
6709  /*
6710  Line to.
6711  */
6712  do
6713  {
6714  (void) GetNextToken(p,&p,MaxTextExtent,token);
6715  if (*token == ',')
6716  (void) GetNextToken(p,&p,MaxTextExtent,token);
6717  x=GetDrawValue(token,&next_token);
6718  if (token == next_token)
6719  ThrowPointExpectedException(image,token);
6720  (void) GetNextToken(p,&p,MaxTextExtent,token);
6721  if (*token == ',')
6722  (void) GetNextToken(p,&p,MaxTextExtent,token);
6723  y=GetDrawValue(token,&next_token);
6724  if (token == next_token)
6725  ThrowPointExpectedException(image,token);
6726  point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
6727  point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
6728  if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6729  return(-1);
6730  q=(*mvg_info->primitive_info)+mvg_info->offset;
6731  if (TracePoint(q,point) == MagickFalse)
6732  return(-1);
6733  mvg_info->offset+=q->coordinates;
6734  q+=(ptrdiff_t) q->coordinates;
6735  while (isspace((int) ((unsigned char) *p)) != 0)
6736  p++;
6737  if (*p == ',')
6738  p++;
6739  } while (IsPoint(p) != MagickFalse);
6740  break;
6741  }
6742  case 'M':
6743  case 'm':
6744  {
6745  /*
6746  Move to.
6747  */
6748  if (mvg_info->offset != subpath_offset)
6749  {
6750  primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6751  primitive_info->coordinates=(size_t) (q-primitive_info);
6752  number_coordinates+=primitive_info->coordinates;
6753  primitive_info=q;
6754  subpath_offset=mvg_info->offset;
6755  }
6756  i=0;
6757  do
6758  {
6759  (void) GetNextToken(p,&p,MaxTextExtent,token);
6760  if (*token == ',')
6761  (void) GetNextToken(p,&p,MaxTextExtent,token);
6762  x=GetDrawValue(token,&next_token);
6763  if (token == next_token)
6764  ThrowPointExpectedException(image,token);
6765  (void) GetNextToken(p,&p,MaxTextExtent,token);
6766  if (*token == ',')
6767  (void) GetNextToken(p,&p,MaxTextExtent,token);
6768  y=GetDrawValue(token,&next_token);
6769  if (token == next_token)
6770  ThrowPointExpectedException(image,token);
6771  point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
6772  point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
6773  if (i == 0)
6774  start=point;
6775  i++;
6776  if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6777  return(-1);
6778  q=(*mvg_info->primitive_info)+mvg_info->offset;
6779  if (TracePoint(q,point) == MagickFalse)
6780  return(-1);
6781  mvg_info->offset+=q->coordinates;
6782  q+=(ptrdiff_t) q->coordinates;
6783  while (isspace((int) ((unsigned char) *p)) != 0)
6784  p++;
6785  if (*p == ',')
6786  p++;
6787  } while (IsPoint(p) != MagickFalse);
6788  break;
6789  }
6790  case 'q':
6791  case 'Q':
6792  {
6793  /*
6794  Quadratic Bézier curve.
6795  */
6796  do
6797  {
6798  points[0]=point;
6799  for (i=1; i < 3; i++)
6800  {
6801  (void) GetNextToken(p,&p,MaxTextExtent,token);
6802  if (*token == ',')
6803  (void) GetNextToken(p,&p,MaxTextExtent,token);
6804  x=GetDrawValue(token,&next_token);
6805  if (token == next_token)
6806  ThrowPointExpectedException(image,token);
6807  (void) GetNextToken(p,&p,MaxTextExtent,token);
6808  if (*token == ',')
6809  (void) GetNextToken(p,&p,MaxTextExtent,token);
6810  y=GetDrawValue(token,&next_token);
6811  if (token == next_token)
6812  ThrowPointExpectedException(image,token);
6813  if (*p == ',')
6814  p++;
6815  end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
6816  end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
6817  points[i]=end;
6818  }
6819  for (i=0; i < 3; i++)
6820  (q+i)->point=points[i];
6821  if (TraceBezier(mvg_info,3) == MagickFalse)
6822  return(-1);
6823  q=(*mvg_info->primitive_info)+mvg_info->offset;
6824  mvg_info->offset+=q->coordinates;
6825  q+=(ptrdiff_t) q->coordinates;
6826  point=end;
6827  while (isspace((int) ((unsigned char) *p)) != 0)
6828  p++;
6829  if (*p == ',')
6830  p++;
6831  } while (IsPoint(p) != MagickFalse);
6832  break;
6833  }
6834  case 's':
6835  case 'S':
6836  {
6837  /*
6838  Cubic Bézier curve.
6839  */
6840  do
6841  {
6842  points[0]=points[3];
6843  points[1].x=2.0*points[3].x-points[2].x;
6844  points[1].y=2.0*points[3].y-points[2].y;
6845  for (i=2; i < 4; i++)
6846  {
6847  (void) GetNextToken(p,&p,MaxTextExtent,token);
6848  if (*token == ',')
6849  (void) GetNextToken(p,&p,MaxTextExtent,token);
6850  x=GetDrawValue(token,&next_token);
6851  if (token == next_token)
6852  ThrowPointExpectedException(image,token);
6853  (void) GetNextToken(p,&p,MaxTextExtent,token);
6854  if (*token == ',')
6855  (void) GetNextToken(p,&p,MaxTextExtent,token);
6856  y=GetDrawValue(token,&next_token);
6857  if (token == next_token)
6858  ThrowPointExpectedException(image,token);
6859  if (*p == ',')
6860  p++;
6861  end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
6862  end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
6863  points[i]=end;
6864  }
6865  if (strchr("CcSs",last_attribute) == (char *) NULL)
6866  {
6867  points[0]=point;
6868  points[1]=point;
6869  }
6870  for (i=0; i < 4; i++)
6871  (q+i)->point=points[i];
6872  if (TraceBezier(mvg_info,4) == MagickFalse)
6873  return(-1);
6874  q=(*mvg_info->primitive_info)+mvg_info->offset;
6875  mvg_info->offset+=q->coordinates;
6876  q+=(ptrdiff_t) q->coordinates;
6877  point=end;
6878  last_attribute=attribute;
6879  while (isspace((int) ((unsigned char) *p)) != 0)
6880  p++;
6881  if (*p == ',')
6882  p++;
6883  } while (IsPoint(p) != MagickFalse);
6884  break;
6885  }
6886  case 't':
6887  case 'T':
6888  {
6889  /*
6890  Quadratic Bézier curve.
6891  */
6892  do
6893  {
6894  points[0]=points[2];
6895  points[1].x=2.0*points[2].x-points[1].x;
6896  points[1].y=2.0*points[2].y-points[1].y;
6897  for (i=2; i < 3; i++)
6898  {
6899  (void) GetNextToken(p,&p,MaxTextExtent,token);
6900  if (*token == ',')
6901  (void) GetNextToken(p,&p,MaxTextExtent,token);
6902  x=GetDrawValue(token,&next_token);
6903  if (token == next_token)
6904  ThrowPointExpectedException(image,token);
6905  (void) GetNextToken(p,&p,MaxTextExtent,token);
6906  if (*token == ',')
6907  (void) GetNextToken(p,&p,MaxTextExtent,token);
6908  y=GetDrawValue(token,&next_token);
6909  if (token == next_token)
6910  ThrowPointExpectedException(image,token);
6911  end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
6912  end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
6913  points[i]=end;
6914  }
6915  if (status == MagickFalse)
6916  break;
6917  if (strchr("QqTt",last_attribute) == (char *) NULL)
6918  {
6919  points[0]=point;
6920  points[1]=point;
6921  }
6922  for (i=0; i < 3; i++)
6923  (q+i)->point=points[i];
6924  if (TraceBezier(mvg_info,3) == MagickFalse)
6925  return(-1);
6926  q=(*mvg_info->primitive_info)+mvg_info->offset;
6927  mvg_info->offset+=q->coordinates;
6928  q+=(ptrdiff_t) q->coordinates;
6929  point=end;
6930  last_attribute=attribute;
6931  while (isspace((int) ((unsigned char) *p)) != 0)
6932  p++;
6933  if (*p == ',')
6934  p++;
6935  } while (IsPoint(p) != MagickFalse);
6936  break;
6937  }
6938  case 'v':
6939  case 'V':
6940  {
6941  /*
6942  Line to.
6943  */
6944  do
6945  {
6946  (void) GetNextToken(p,&p,MaxTextExtent,token);
6947  if (*token == ',')
6948  (void) GetNextToken(p,&p,MaxTextExtent,token);
6949  y=GetDrawValue(token,&next_token);
6950  if (token == next_token)
6951  ThrowPointExpectedException(image,token);
6952  point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
6953  if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6954  return(-1);
6955  q=(*mvg_info->primitive_info)+mvg_info->offset;
6956  if (TracePoint(q,point) == MagickFalse)
6957  return(-1);
6958  mvg_info->offset+=q->coordinates;
6959  q+=(ptrdiff_t) q->coordinates;
6960  while (isspace((int) ((unsigned char) *p)) != 0)
6961  p++;
6962  if (*p == ',')
6963  p++;
6964  } while (IsPoint(p) != MagickFalse);
6965  break;
6966  }
6967  case 'z':
6968  case 'Z':
6969  {
6970  /*
6971  Close path.
6972  */
6973  point=start;
6974  if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6975  return(-1);
6976  q=(*mvg_info->primitive_info)+mvg_info->offset;
6977  if (TracePoint(q,point) == MagickFalse)
6978  return(-1);
6979  mvg_info->offset+=q->coordinates;
6980  q+=(ptrdiff_t) q->coordinates;
6981  primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6982  primitive_info->coordinates=(size_t) (q-primitive_info);
6983  primitive_info->closed_subpath=MagickTrue;
6984  number_coordinates+=primitive_info->coordinates;
6985  primitive_info=q;
6986  subpath_offset=mvg_info->offset;
6987  z_count++;
6988  break;
6989  }
6990  default:
6991  {
6992  ThrowPointExpectedException(image,token);
6993  break;
6994  }
6995  }
6996  }
6997  if (status == MagickFalse)
6998  return(-1);
6999  primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7000  primitive_info->coordinates=(size_t) (q-primitive_info);
7001  number_coordinates+=primitive_info->coordinates;
7002  for (i=0; i < (ssize_t) number_coordinates; i++)
7003  {
7004  q--;
7005  q->primitive=primitive_type;
7006  if (z_count > 1)
7007  q->method=FillToBorderMethod;
7008  }
7009  q=primitive_info;
7010  return((ssize_t) number_coordinates);
7011 }
7012 
7013 static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7014  const PointInfo start,const PointInfo end)
7015 {
7016  PointInfo
7017  point;
7018 
7020  *p;
7021 
7022  ssize_t
7023  i;
7024 
7025  p=primitive_info;
7026  if (TracePoint(p,start) == MagickFalse)
7027  return(MagickFalse);
7028  p+=(ptrdiff_t) p->coordinates;
7029  point.x=start.x;
7030  point.y=end.y;
7031  if (TracePoint(p,point) == MagickFalse)
7032  return(MagickFalse);
7033  p+=(ptrdiff_t) p->coordinates;
7034  if (TracePoint(p,end) == MagickFalse)
7035  return(MagickFalse);
7036  p+=(ptrdiff_t) p->coordinates;
7037  point.x=end.x;
7038  point.y=start.y;
7039  if (TracePoint(p,point) == MagickFalse)
7040  return(MagickFalse);
7041  p+=(ptrdiff_t) p->coordinates;
7042  if (TracePoint(p,start) == MagickFalse)
7043  return(MagickFalse);
7044  p+=(ptrdiff_t) p->coordinates;
7045  primitive_info->coordinates=(size_t) (p-primitive_info);
7046  primitive_info->closed_subpath=MagickTrue;
7047  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7048  {
7049  p->primitive=primitive_info->primitive;
7050  p--;
7051  }
7052  return(MagickTrue);
7053 }
7054 
7055 static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7056  const PointInfo start,const PointInfo end,PointInfo arc)
7057 {
7058  PointInfo
7059  degrees,
7060  point,
7061  segment;
7062 
7064  *primitive_info;
7065 
7067  *p;
7068 
7069  ssize_t
7070  i;
7071 
7072  ssize_t
7073  offset;
7074 
7075  offset=mvg_info->offset;
7076  segment.x=fabs(end.x-start.x);
7077  segment.y=fabs(end.y-start.y);
7078  if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7079  {
7080  (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7081  return(MagickTrue);
7082  }
7083  if (arc.x > (0.5*segment.x))
7084  arc.x=0.5*segment.x;
7085  if (arc.y > (0.5*segment.y))
7086  arc.y=0.5*segment.y;
7087  point.x=start.x+segment.x-arc.x;
7088  point.y=start.y+arc.y;
7089  degrees.x=270.0;
7090  degrees.y=360.0;
7091  if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7092  return(MagickFalse);
7093  p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7094  mvg_info->offset+=p->coordinates;
7095  point.x=start.x+segment.x-arc.x;
7096  point.y=start.y+segment.y-arc.y;
7097  degrees.x=0.0;
7098  degrees.y=90.0;
7099  if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7100  return(MagickFalse);
7101  p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7102  mvg_info->offset+=p->coordinates;
7103  point.x=start.x+arc.x;
7104  point.y=start.y+segment.y-arc.y;
7105  degrees.x=90.0;
7106  degrees.y=180.0;
7107  if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7108  return(MagickFalse);
7109  p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7110  mvg_info->offset+=p->coordinates;
7111  point.x=start.x+arc.x;
7112  point.y=start.y+arc.y;
7113  degrees.x=180.0;
7114  degrees.y=270.0;
7115  if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7116  return(MagickFalse);
7117  p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7118  mvg_info->offset+=p->coordinates;
7119  if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7120  return(MagickFalse);
7121  p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7122  if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7123  return(MagickFalse);
7124  p+=(ptrdiff_t) p->coordinates;
7125  mvg_info->offset=offset;
7126  primitive_info=(*mvg_info->primitive_info)+offset;
7127  primitive_info->coordinates=(size_t) (p-primitive_info);
7128  primitive_info->closed_subpath=MagickTrue;
7129  for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7130  {
7131  p->primitive=primitive_info->primitive;
7132  p--;
7133  }
7134  return(MagickTrue);
7135 }
7136 
7137 static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7138  const size_t number_vertices,const double offset)
7139 {
7140  double
7141  distance;
7142 
7143  double
7144  dx,
7145  dy;
7146 
7147  ssize_t
7148  i;
7149 
7150  ssize_t
7151  j;
7152 
7153  dx=0.0;
7154  dy=0.0;
7155  for (i=1; i < (ssize_t) number_vertices; i++)
7156  {
7157  dx=primitive_info[0].point.x-primitive_info[i].point.x;
7158  dy=primitive_info[0].point.y-primitive_info[i].point.y;
7159  if ((fabs((double) dx) >= MagickEpsilon) ||
7160  (fabs((double) dy) >= MagickEpsilon))
7161  break;
7162  }
7163  if (i == (ssize_t) number_vertices)
7164  i=(ssize_t) number_vertices-1L;
7165  distance=hypot((double) dx,(double) dy);
7166  primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7167  dx*(distance+offset)/distance);
7168  primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7169  dy*(distance+offset)/distance);
7170  for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7171  {
7172  dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7173  dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7174  if ((fabs((double) dx) >= MagickEpsilon) ||
7175  (fabs((double) dy) >= MagickEpsilon))
7176  break;
7177  }
7178  distance=hypot((double) dx,(double) dy);
7179  primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7180  dx*(distance+offset)/distance);
7181  primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7182  dy*(distance+offset)/distance);
7183  return(MagickTrue);
7184 }
7185 
7186 static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
7187  const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7188 {
7189 #define MaxStrokePad (6*BezierQuantum+360)
7190 #define CheckPathExtent(pad_p,pad_q) \
7191 { \
7192  if ((pad_p) > MaxBezierCoordinates) \
7193  stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7194  else \
7195  if ((ssize_t) (p+(pad_p)) >= (ssize_t) extent_p) \
7196  { \
7197  if (~extent_p < (pad_p)) \
7198  stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7199  else \
7200  { \
7201  extent_p+=(pad_p); \
7202  stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7203  MaxStrokePad,sizeof(*stroke_p)); \
7204  } \
7205  } \
7206  if ((pad_q) > MaxBezierCoordinates) \
7207  stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7208  else \
7209  if ((ssize_t) (q+(pad_q)) >= (ssize_t) extent_q) \
7210  { \
7211  if (~extent_q < (pad_q)) \
7212  stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7213  else \
7214  { \
7215  extent_q+=(pad_q); \
7216  stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7217  MaxStrokePad,sizeof(*stroke_q)); \
7218  } \
7219  } \
7220  if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7221  { \
7222  if (stroke_p != (PointInfo *) NULL) \
7223  stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7224  if (stroke_q != (PointInfo *) NULL) \
7225  stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7226  polygon_primitive=(PrimitiveInfo *) \
7227  RelinquishMagickMemory(polygon_primitive); \
7228  (void) ThrowMagickException(exception,GetMagickModule(), \
7229  ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7230  return((PrimitiveInfo *) NULL); \
7231  } \
7232 }
7233 
7234  typedef struct _StrokeSegment
7235  {
7236  double
7237  p,
7238  q;
7239  } StrokeSegment;
7240 
7241  double
7242  delta_theta,
7243  dot_product,
7244  mid,
7245  miterlimit;
7246 
7247  MagickBooleanType
7248  closed_path;
7249 
7250  PointInfo
7251  box_p[5],
7252  box_q[5],
7253  center,
7254  offset,
7255  *stroke_p,
7256  *stroke_q;
7257 
7259  *polygon_primitive,
7260  *stroke_polygon;
7261 
7262  ssize_t
7263  i;
7264 
7265  size_t
7266  arc_segments,
7267  extent_p,
7268  extent_q,
7269  number_vertices;
7270 
7271  ssize_t
7272  j,
7273  n,
7274  p,
7275  q;
7276 
7277  StrokeSegment
7278  dx = {0.0, 0.0},
7279  dy = {0.0, 0.0},
7280  inverse_slope = {0.0, 0.0},
7281  slope = {0.0, 0.0},
7282  theta = {0.0, 0.0};
7283 
7284  /*
7285  Allocate paths.
7286  */
7287  number_vertices=primitive_info->coordinates;
7288  polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7289  number_vertices+2UL,sizeof(*polygon_primitive));
7290  if (polygon_primitive == (PrimitiveInfo *) NULL)
7291  {
7292  (void) ThrowMagickException(exception,GetMagickModule(),
7293  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7294  return((PrimitiveInfo *) NULL);
7295  }
7296  (void) memcpy(polygon_primitive,primitive_info,(size_t) number_vertices*
7297  sizeof(*polygon_primitive));
7298  offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7299  offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7300  closed_path=(fabs(offset.x) < MagickEpsilon) &&
7301  (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7302  if ((draw_info->linejoin == MiterJoin) ||
7303  ((draw_info->linejoin == RoundJoin) && (closed_path != MagickFalse)))
7304  {
7305  polygon_primitive[number_vertices]=primitive_info[1];
7306  number_vertices++;
7307  }
7308  polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7309  /*
7310  Compute the slope for the first line segment, p.
7311  */
7312  closed_path=primitive_info[0].closed_subpath;
7313  dx.p=0.0;
7314  dy.p=0.0;
7315  for (n=1; n < (ssize_t) number_vertices; n++)
7316  {
7317  dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7318  dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7319  if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7320  break;
7321  }
7322  if (n == (ssize_t) number_vertices)
7323  {
7324  if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7325  {
7326  /*
7327  Zero length subpath.
7328  */
7329  stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7330  sizeof(*stroke_polygon));
7331  stroke_polygon[0]=polygon_primitive[0];
7332  stroke_polygon[0].coordinates=0;
7333  polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7334  polygon_primitive);
7335  return(stroke_polygon);
7336  }
7337  n=(ssize_t) number_vertices-1L;
7338  }
7339  extent_p=2*number_vertices;
7340  extent_q=2*number_vertices;
7341  stroke_p=(PointInfo *) AcquireQuantumMemory((size_t) extent_p+MaxStrokePad,
7342  sizeof(*stroke_p));
7343  stroke_q=(PointInfo *) AcquireQuantumMemory((size_t) extent_q+MaxStrokePad,
7344  sizeof(*stroke_q));
7345  if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7346  {
7347  if (stroke_p != (PointInfo *) NULL)
7348  stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7349  if (stroke_q != (PointInfo *) NULL)
7350  stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7351  polygon_primitive=(PrimitiveInfo *)
7352  RelinquishMagickMemory(polygon_primitive);
7353  (void) ThrowMagickException(exception,GetMagickModule(),
7354  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7355  return((PrimitiveInfo *) NULL);
7356  }
7357  slope.p=0.0;
7358  inverse_slope.p=0.0;
7359  if (fabs(dx.p) < MagickEpsilon)
7360  {
7361  if (dx.p >= 0.0)
7362  slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7363  else
7364  slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7365  }
7366  else
7367  if (fabs(dy.p) < MagickEpsilon)
7368  {
7369  if (dy.p >= 0.0)
7370  inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7371  else
7372  inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7373  }
7374  else
7375  {
7376  slope.p=dy.p/dx.p;
7377  inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7378  }
7379  mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7380  miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7381  if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7382  (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7383  offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7384  offset.y=(double) (offset.x*inverse_slope.p);
7385  if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7386  {
7387  box_p[0].x=polygon_primitive[0].point.x-offset.x;
7388  box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7389  box_p[1].x=polygon_primitive[n].point.x-offset.x;
7390  box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7391  box_q[0].x=polygon_primitive[0].point.x+offset.x;
7392  box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7393  box_q[1].x=polygon_primitive[n].point.x+offset.x;
7394  box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7395  }
7396  else
7397  {
7398  box_p[0].x=polygon_primitive[0].point.x+offset.x;
7399  box_p[0].y=polygon_primitive[0].point.y+offset.y;
7400  box_p[1].x=polygon_primitive[n].point.x+offset.x;
7401  box_p[1].y=polygon_primitive[n].point.y+offset.y;
7402  box_q[0].x=polygon_primitive[0].point.x-offset.x;
7403  box_q[0].y=polygon_primitive[0].point.y-offset.y;
7404  box_q[1].x=polygon_primitive[n].point.x-offset.x;
7405  box_q[1].y=polygon_primitive[n].point.y-offset.y;
7406  }
7407  /*
7408  Create strokes for the line join attribute: bevel, miter, round.
7409  */
7410  p=0;
7411  q=0;
7412  stroke_q[p++]=box_q[0];
7413  stroke_p[q++]=box_p[0];
7414  for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7415  {
7416  /*
7417  Compute the slope for this line segment, q.
7418  */
7419  dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7420  dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7421  dot_product=dx.q*dx.q+dy.q*dy.q;
7422  if (dot_product < 0.25)
7423  continue;
7424  slope.q=0.0;
7425  inverse_slope.q=0.0;
7426  if (fabs(dx.q) < MagickEpsilon)
7427  {
7428  if (dx.q >= 0.0)
7429  slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7430  else
7431  slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7432  }
7433  else
7434  if (fabs(dy.q) < MagickEpsilon)
7435  {
7436  if (dy.q >= 0.0)
7437  inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7438  else
7439  inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7440  }
7441  else
7442  {
7443  slope.q=dy.q/dx.q;
7444  inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7445  }
7446  offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7447  offset.y=(double) (offset.x*inverse_slope.q);
7448  dot_product=dy.q*offset.x-dx.q*offset.y;
7449  if (dot_product > 0.0)
7450  {
7451  box_p[2].x=polygon_primitive[n].point.x-offset.x;
7452  box_p[2].y=polygon_primitive[n].point.y-offset.y;
7453  box_p[3].x=polygon_primitive[i].point.x-offset.x;
7454  box_p[3].y=polygon_primitive[i].point.y-offset.y;
7455  box_q[2].x=polygon_primitive[n].point.x+offset.x;
7456  box_q[2].y=polygon_primitive[n].point.y+offset.y;
7457  box_q[3].x=polygon_primitive[i].point.x+offset.x;
7458  box_q[3].y=polygon_primitive[i].point.y+offset.y;
7459  }
7460  else
7461  {
7462  box_p[2].x=polygon_primitive[n].point.x+offset.x;
7463  box_p[2].y=polygon_primitive[n].point.y+offset.y;
7464  box_p[3].x=polygon_primitive[i].point.x+offset.x;
7465  box_p[3].y=polygon_primitive[i].point.y+offset.y;
7466  box_q[2].x=polygon_primitive[n].point.x-offset.x;
7467  box_q[2].y=polygon_primitive[n].point.y-offset.y;
7468  box_q[3].x=polygon_primitive[i].point.x-offset.x;
7469  box_q[3].y=polygon_primitive[i].point.y-offset.y;
7470  }
7471  if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
7472  {
7473  box_p[4]=box_p[1];
7474  box_q[4]=box_q[1];
7475  }
7476  else
7477  {
7478  box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7479  box_p[3].y)/(slope.p-slope.q));
7480  box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7481  box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7482  box_q[3].y)/(slope.p-slope.q));
7483  box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7484  }
7485  CheckPathExtent(MaxStrokePad,MaxStrokePad);
7486  dot_product=dx.q*dy.p-dx.p*dy.q;
7487  if (dot_product <= 0.0)
7488  switch (draw_info->linejoin)
7489  {
7490  case BevelJoin:
7491  {
7492  stroke_q[q++]=box_q[1];
7493  stroke_q[q++]=box_q[2];
7494  dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7495  (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7496  if (dot_product <= miterlimit)
7497  stroke_p[p++]=box_p[4];
7498  else
7499  {
7500  stroke_p[p++]=box_p[1];
7501  stroke_p[p++]=box_p[2];
7502  }
7503  break;
7504  }
7505  case MiterJoin:
7506  {
7507  dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7508  (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7509  if (dot_product <= miterlimit)
7510  {
7511  stroke_q[q++]=box_q[4];
7512  stroke_p[p++]=box_p[4];
7513  }
7514  else
7515  {
7516  stroke_q[q++]=box_q[1];
7517  stroke_q[q++]=box_q[2];
7518  stroke_p[p++]=box_p[1];
7519  stroke_p[p++]=box_p[2];
7520  }
7521  break;
7522  }
7523  case RoundJoin:
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  stroke_p[p++]=box_p[4];
7529  else
7530  {
7531  stroke_p[p++]=box_p[1];
7532  stroke_p[p++]=box_p[2];
7533  }
7534  center=polygon_primitive[n].point;
7535  theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7536  theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7537  if (theta.q < theta.p)
7538  theta.q+=2.0*MagickPI;
7539  arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.q-
7540  theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7541  CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7542  stroke_q[q].x=box_q[1].x;
7543  stroke_q[q].y=box_q[1].y;
7544  q++;
7545  for (j=1; j < (ssize_t) arc_segments; j++)
7546  {
7547  delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7548  stroke_q[q].x=(double) (center.x+mid*cos(fmod((double)
7549  (theta.p+delta_theta),DegreesToRadians(360.0))));
7550  stroke_q[q].y=(double) (center.y+mid*sin(fmod((double)
7551  (theta.p+delta_theta),DegreesToRadians(360.0))));
7552  q++;
7553  }
7554  stroke_q[q++]=box_q[2];
7555  break;
7556  }
7557  default:
7558  break;
7559  }
7560  else
7561  switch (draw_info->linejoin)
7562  {
7563  case BevelJoin:
7564  {
7565  stroke_p[p++]=box_p[1];
7566  stroke_p[p++]=box_p[2];
7567  dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7568  (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7569  if (dot_product <= miterlimit)
7570  stroke_q[q++]=box_q[4];
7571  else
7572  {
7573  stroke_q[q++]=box_q[1];
7574  stroke_q[q++]=box_q[2];
7575  }
7576  break;
7577  }
7578  case MiterJoin:
7579  {
7580  dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7581  (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7582  if (dot_product <= miterlimit)
7583  {
7584  stroke_q[q++]=box_q[4];
7585  stroke_p[p++]=box_p[4];
7586  }
7587  else
7588  {
7589  stroke_q[q++]=box_q[1];
7590  stroke_q[q++]=box_q[2];
7591  stroke_p[p++]=box_p[1];
7592  stroke_p[p++]=box_p[2];
7593  }
7594  break;
7595  }
7596  case RoundJoin:
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  stroke_q[q++]=box_q[4];
7602  else
7603  {
7604  stroke_q[q++]=box_q[1];
7605  stroke_q[q++]=box_q[2];
7606  }
7607  center=polygon_primitive[n].point;
7608  theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7609  theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7610  if (theta.p < theta.q)
7611  theta.p+=2.0*MagickPI;
7612  arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.p-
7613  theta.q)/(2.0*sqrt((double) (MagickSafeReciprocal(mid)))))));
7614  CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7615  stroke_p[p++]=box_p[1];
7616  for (j=1; j < (ssize_t) arc_segments; j++)
7617  {
7618  delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7619  stroke_p[p].x=(double) (center.x+mid*cos(fmod((double)
7620  (theta.p+delta_theta),DegreesToRadians(360.0))));
7621  stroke_p[p].y=(double) (center.y+mid*sin(fmod((double)
7622  (theta.p+delta_theta),DegreesToRadians(360.0))));
7623  p++;
7624  }
7625  stroke_p[p++]=box_p[2];
7626  break;
7627  }
7628  default:
7629  break;
7630  }
7631  slope.p=slope.q;
7632  inverse_slope.p=inverse_slope.q;
7633  box_p[0]=box_p[2];
7634  box_p[1]=box_p[3];
7635  box_q[0]=box_q[2];
7636  box_q[1]=box_q[3];
7637  dx.p=dx.q;
7638  dy.p=dy.q;
7639  n=i;
7640  }
7641  stroke_p[p++]=box_p[1];
7642  stroke_q[q++]=box_q[1];
7643  /*
7644  Trace stroked polygon.
7645  */
7646  stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7647  (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
7648  if (stroke_polygon == (PrimitiveInfo *) NULL)
7649  {
7650  (void) ThrowMagickException(exception,GetMagickModule(),
7651  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7652  stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7653  stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7654  polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7655  polygon_primitive);
7656  return(stroke_polygon);
7657  }
7658  for (i=0; i < (ssize_t) p; i++)
7659  {
7660  stroke_polygon[i]=polygon_primitive[0];
7661  stroke_polygon[i].point=stroke_p[i];
7662  }
7663  if (closed_path != MagickFalse)
7664  {
7665  stroke_polygon[i]=polygon_primitive[0];
7666  stroke_polygon[i].point=stroke_polygon[0].point;
7667  i++;
7668  }
7669  for ( ; i < (ssize_t) (p+q+closed_path); i++)
7670  {
7671  stroke_polygon[i]=polygon_primitive[0];
7672  stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7673  }
7674  if (closed_path != MagickFalse)
7675  {
7676  stroke_polygon[i]=polygon_primitive[0];
7677  stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7678  i++;
7679  }
7680  stroke_polygon[i]=polygon_primitive[0];
7681  stroke_polygon[i].point=stroke_polygon[0].point;
7682  i++;
7683  stroke_polygon[i].primitive=UndefinedPrimitive;
7684  stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7685  stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7686  stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7687  polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7688  return(stroke_polygon);
7689 }
Definition: draw.c:145
Definition: image.h:133