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