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