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