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