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