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