MagickCore  7.0.9
draw.c
Go to the documentation of this file.
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-2019 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
23 % %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
26 % %
27 % https://imagemagick.org/script/license.php %
28 % %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
34 % %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 % Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38 % rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39 % Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40 % (www.appligent.com) contributed the dash pattern, linecap stroking
41 % algorithm, and minor rendering improvements.
42 %
43 */
44 
45 /*
46  Include declarations.
47 */
48 #include "MagickCore/studio.h"
49 #include "MagickCore/annotate.h"
50 #include "MagickCore/artifact.h"
51 #include "MagickCore/blob.h"
52 #include "MagickCore/cache.h"
54 #include "MagickCore/cache-view.h"
55 #include "MagickCore/channel.h"
56 #include "MagickCore/color.h"
58 #include "MagickCore/composite.h"
60 #include "MagickCore/constitute.h"
61 #include "MagickCore/draw.h"
63 #include "MagickCore/enhance.h"
64 #include "MagickCore/exception.h"
66 #include "MagickCore/gem.h"
67 #include "MagickCore/geometry.h"
69 #include "MagickCore/list.h"
70 #include "MagickCore/log.h"
72 #include "MagickCore/monitor.h"
74 #include "MagickCore/option.h"
75 #include "MagickCore/paint.h"
78 #include "MagickCore/property.h"
79 #include "MagickCore/resample.h"
81 #include "MagickCore/resource_.h"
82 #include "MagickCore/splay-tree.h"
83 #include "MagickCore/string_.h"
86 #include "MagickCore/token.h"
88 #include "MagickCore/utility.h"
89 
90 /*
91  Define declarations.
92 */
93 #define BezierQuantum 200
94 #define PrimitiveExtentPad 2048
95 #define MaxBezierCoordinates 4194304
96 #define ThrowPointExpectedException(token,exception) \
97 { \
98  (void) ThrowMagickException(exception,GetMagickModule(),DrawError, \
99  "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
100  status=MagickFalse; \
101  break; \
102 }
103 
104 /*
105  Typedef declarations.
106 */
107 typedef struct _EdgeInfo
108 {
111 
112  double
114 
115  PointInfo
117 
118  size_t
120 
121  ssize_t
123 
126 
127  size_t
129 } EdgeInfo;
130 
131 typedef struct _ElementInfo
132 {
133  double
134  cx,
135  cy,
136  major,
137  minor,
138  angle;
139 } ElementInfo;
140 
141 typedef struct _MVGInfo
142 {
145 
146  size_t
148 
149  ssize_t
151 
152  PointInfo
154 
157 } MVGInfo;
158 
159 typedef struct _PolygonInfo
160 {
161  EdgeInfo
163 
164  size_t
166 } PolygonInfo;
167 
168 typedef enum
169 {
175 } PathInfoCode;
176 
177 typedef struct _PathInfo
178 {
179  PointInfo
181 
184 } PathInfo;
185 
186 /*
187  Forward declarations.
188 */
189 static Image
190  *DrawClippingMask(Image *,const DrawInfo *,const char *,const char *,
191  ExceptionInfo *);
192 
193 static MagickBooleanType
194  DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
195  ExceptionInfo *),
196  RenderMVGContent(Image *,const DrawInfo *,const size_t,ExceptionInfo *),
197  TraceArc(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
198  TraceArcPath(MVGInfo *,const PointInfo,const PointInfo,const PointInfo,
199  const double,const MagickBooleanType,const MagickBooleanType),
200  TraceBezier(MVGInfo *,const size_t),
201  TraceCircle(MVGInfo *,const PointInfo,const PointInfo),
202  TraceEllipse(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
206  TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
207 
208 static PrimitiveInfo
209  *TraceStrokePolygon(const Image *,const DrawInfo *,const PrimitiveInfo *);
210 
211 static size_t
212  TracePath(MVGInfo *,const char *,ExceptionInfo *);
213 
214 /*
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % %
217 % %
218 % %
219 % A c q u i r e D r a w I n f o %
220 % %
221 % %
222 % %
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 %
225 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
226 %
227 % The format of the AcquireDrawInfo method is:
228 %
229 % DrawInfo *AcquireDrawInfo(void)
230 %
231 */
233 {
234  DrawInfo
235  *draw_info;
236 
237  draw_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*draw_info));
238  GetDrawInfo((ImageInfo *) NULL,draw_info);
239  return(draw_info);
240 }
241 
242 /*
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 % %
245 % %
246 % %
247 % C l o n e D r a w I n f o %
248 % %
249 % %
250 % %
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 %
253 % CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
254 % is specified, a new DrawInfo structure is created initialized to default
255 % values.
256 %
257 % The format of the CloneDrawInfo method is:
258 %
259 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
260 % const DrawInfo *draw_info)
261 %
262 % A description of each parameter follows:
263 %
264 % o image_info: the image info.
265 %
266 % o draw_info: the draw info.
267 %
268 */
270  const DrawInfo *draw_info)
271 {
272  DrawInfo
273  *clone_info;
274 
276  *exception;
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  exception=AcquireExceptionInfo();
283  if (draw_info->id != (char *) NULL)
284  (void) CloneString(&clone_info->id,draw_info->id);
285  if (draw_info->primitive != (char *) NULL)
286  (void) CloneString(&clone_info->primitive,draw_info->primitive);
287  if (draw_info->geometry != (char *) NULL)
288  (void) CloneString(&clone_info->geometry,draw_info->geometry);
289  clone_info->compliance=draw_info->compliance;
290  clone_info->viewbox=draw_info->viewbox;
291  clone_info->affine=draw_info->affine;
292  clone_info->gravity=draw_info->gravity;
293  clone_info->fill=draw_info->fill;
294  clone_info->stroke=draw_info->stroke;
295  clone_info->stroke_width=draw_info->stroke_width;
296  if (draw_info->fill_pattern != (Image *) NULL)
297  clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
298  exception);
299  if (draw_info->stroke_pattern != (Image *) NULL)
300  clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
301  MagickTrue,exception);
302  clone_info->stroke_antialias=draw_info->stroke_antialias;
303  clone_info->text_antialias=draw_info->text_antialias;
304  clone_info->fill_rule=draw_info->fill_rule;
305  clone_info->linecap=draw_info->linecap;
306  clone_info->linejoin=draw_info->linejoin;
307  clone_info->miterlimit=draw_info->miterlimit;
308  clone_info->dash_offset=draw_info->dash_offset;
309  clone_info->decorate=draw_info->decorate;
310  clone_info->compose=draw_info->compose;
311  if (draw_info->text != (char *) NULL)
312  (void) CloneString(&clone_info->text,draw_info->text);
313  if (draw_info->font != (char *) NULL)
314  (void) CloneString(&clone_info->font,draw_info->font);
315  if (draw_info->metrics != (char *) NULL)
316  (void) CloneString(&clone_info->metrics,draw_info->metrics);
317  if (draw_info->family != (char *) NULL)
318  (void) CloneString(&clone_info->family,draw_info->family);
319  clone_info->style=draw_info->style;
320  clone_info->stretch=draw_info->stretch;
321  clone_info->weight=draw_info->weight;
322  if (draw_info->encoding != (char *) NULL)
323  (void) CloneString(&clone_info->encoding,draw_info->encoding);
324  clone_info->pointsize=draw_info->pointsize;
325  clone_info->kerning=draw_info->kerning;
326  clone_info->interline_spacing=draw_info->interline_spacing;
327  clone_info->interword_spacing=draw_info->interword_spacing;
328  clone_info->direction=draw_info->direction;
329  if (draw_info->density != (char *) NULL)
330  (void) CloneString(&clone_info->density,draw_info->density);
331  clone_info->align=draw_info->align;
332  clone_info->undercolor=draw_info->undercolor;
333  clone_info->border_color=draw_info->border_color;
334  if (draw_info->server_name != (char *) NULL)
335  (void) CloneString(&clone_info->server_name,draw_info->server_name);
336  if (draw_info->dash_pattern != (double *) NULL)
337  {
338  register ssize_t
339  x;
340 
341  for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
342  clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) (2*x+2),
343  sizeof(*clone_info->dash_pattern));
344  if (clone_info->dash_pattern == (double *) NULL)
346  "UnableToAllocateDashPattern");
347  (void) memset(clone_info->dash_pattern,0,(size_t) (2*x+2)*
348  sizeof(*clone_info->dash_pattern));
349  (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(size_t)
350  (x+1)*sizeof(*clone_info->dash_pattern));
351  }
352  clone_info->gradient=draw_info->gradient;
353  if (draw_info->gradient.stops != (StopInfo *) NULL)
354  {
355  size_t
356  number_stops;
357 
358  number_stops=clone_info->gradient.number_stops;
359  clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
360  number_stops,sizeof(*clone_info->gradient.stops));
361  if (clone_info->gradient.stops == (StopInfo *) NULL)
363  "UnableToAllocateDashPattern");
364  (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
365  (size_t) number_stops*sizeof(*clone_info->gradient.stops));
366  }
367  clone_info->bounds=draw_info->bounds;
368  clone_info->fill_alpha=draw_info->fill_alpha;
369  clone_info->stroke_alpha=draw_info->stroke_alpha;
370  clone_info->element_reference=draw_info->element_reference;
371  clone_info->clip_path=draw_info->clip_path;
372  clone_info->clip_units=draw_info->clip_units;
373  if (draw_info->clip_mask != (char *) NULL)
374  (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
375  if (draw_info->clipping_mask != (Image *) NULL)
376  clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
377  MagickTrue,exception);
378  if (draw_info->composite_mask != (Image *) NULL)
379  clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
380  MagickTrue,exception);
381  clone_info->render=draw_info->render;
382  clone_info->debug=IsEventLogging();
383  exception=DestroyExceptionInfo(exception);
384  return(clone_info);
385 }
386 
387 /*
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 % %
390 % %
391 % %
392 + C o n v e r t P a t h T o P o l y g o n %
393 % %
394 % %
395 % %
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 %
398 % ConvertPathToPolygon() converts a path to the more efficient sorted
399 % rendering form.
400 %
401 % The format of the ConvertPathToPolygon method is:
402 %
403 % PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
404 %
405 % A description of each parameter follows:
406 %
407 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
408 % rendering form of type PolygonInfo.
409 %
410 % o draw_info: Specifies a pointer to an DrawInfo structure.
411 %
412 % o path_info: Specifies a pointer to an PathInfo structure.
413 %
414 %
415 */
416 
417 #if defined(__cplusplus) || defined(c_plusplus)
418 extern "C" {
419 #endif
420 
421 static int DrawCompareEdges(const void *p_edge,const void *q_edge)
422 {
423 #define DrawCompareEdge(p,q) \
424 { \
425  if (((p)-(q)) < 0.0) \
426  return(-1); \
427  if (((p)-(q)) > 0.0) \
428  return(1); \
429 }
430 
431  register const PointInfo
432  *p,
433  *q;
434 
435  /*
436  Edge sorting for right-handed coordinate system.
437  */
438  p=((const EdgeInfo *) p_edge)->points;
439  q=((const EdgeInfo *) q_edge)->points;
440  DrawCompareEdge(p[0].y,q[0].y);
441  DrawCompareEdge(p[0].x,q[0].x);
442  DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
443  (q[1].x-q[0].x));
444  DrawCompareEdge(p[1].y,q[1].y);
445  DrawCompareEdge(p[1].x,q[1].x);
446  return(0);
447 }
448 
449 #if defined(__cplusplus) || defined(c_plusplus)
450 }
451 #endif
452 
453 static void LogPolygonInfo(const PolygonInfo *polygon_info)
454 {
455  register EdgeInfo
456  *p;
457 
458  register ssize_t
459  i,
460  j;
461 
462  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
463  p=polygon_info->edges;
464  for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
465  {
466  (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
467  (double) i);
468  (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
469  p->direction != MagickFalse ? "down" : "up");
470  (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
471  p->ghostline != MagickFalse ? "transparent" : "opaque");
473  " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
474  p->bounds.x2,p->bounds.y2);
475  for (j=0; j < (ssize_t) p->number_points; j++)
476  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g,%g",
477  p->points[j].x,p->points[j].y);
478  p++;
479  }
480  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
481 }
482 
483 static void ReversePoints(PointInfo *points,const size_t number_points)
484 {
485  PointInfo
486  point;
487 
488  register ssize_t
489  i;
490 
491  for (i=0; i < (ssize_t) (number_points >> 1); i++)
492  {
493  point=points[i];
494  points[i]=points[number_points-(i+1)];
495  points[number_points-(i+1)]=point;
496  }
497 }
498 
499 static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
500 {
501  long
502  direction,
503  next_direction;
504 
505  PointInfo
506  point,
507  *points;
508 
510  *polygon_info;
511 
513  bounds;
514 
515  register ssize_t
516  i,
517  n;
518 
520  ghostline;
521 
522  size_t
523  edge,
524  number_edges,
525  number_points;
526 
527  /*
528  Convert a path to the more efficient sorted rendering form.
529  */
530  polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
531  if (polygon_info == (PolygonInfo *) NULL)
532  return((PolygonInfo *) NULL);
533  number_edges=16;
534  polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
535  sizeof(*polygon_info->edges));
536  if (polygon_info->edges == (EdgeInfo *) NULL)
537  return((PolygonInfo *) NULL);
538  (void) memset(polygon_info->edges,0,number_edges*
539  sizeof(*polygon_info->edges));
540  direction=0;
541  edge=0;
542  ghostline=MagickFalse;
543  n=0;
544  number_points=0;
545  points=(PointInfo *) NULL;
546  (void) memset(&point,0,sizeof(point));
547  (void) memset(&bounds,0,sizeof(bounds));
548  polygon_info->edges[edge].number_points=(size_t) n;
549  polygon_info->edges[edge].scanline=0.0;
550  polygon_info->edges[edge].highwater=0;
551  polygon_info->edges[edge].ghostline=ghostline;
552  polygon_info->edges[edge].direction=(ssize_t) direction;
553  polygon_info->edges[edge].points=points;
554  polygon_info->edges[edge].bounds=bounds;
555  polygon_info->number_edges=0;
556  for (i=0; path_info[i].code != EndCode; i++)
557  {
558  if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
559  (path_info[i].code == GhostlineCode))
560  {
561  /*
562  Move to.
563  */
564  if ((points != (PointInfo *) NULL) && (n >= 2))
565  {
566  if (edge == number_edges)
567  {
568  number_edges<<=1;
569  polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
570  polygon_info->edges,(size_t) number_edges,
571  sizeof(*polygon_info->edges));
572  if (polygon_info->edges == (EdgeInfo *) NULL)
573  return((PolygonInfo *) NULL);
574  }
575  polygon_info->edges[edge].number_points=(size_t) n;
576  polygon_info->edges[edge].scanline=(-1.0);
577  polygon_info->edges[edge].highwater=0;
578  polygon_info->edges[edge].ghostline=ghostline;
579  polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
580  if (direction < 0)
581  ReversePoints(points,(size_t) n);
582  polygon_info->edges[edge].points=points;
583  polygon_info->edges[edge].bounds=bounds;
584  polygon_info->edges[edge].bounds.y1=points[0].y;
585  polygon_info->edges[edge].bounds.y2=points[n-1].y;
586  points=(PointInfo *) NULL;
587  ghostline=MagickFalse;
588  edge++;
589  }
590  if (points == (PointInfo *) NULL)
591  {
592  number_points=16;
593  points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
594  sizeof(*points));
595  if (points == (PointInfo *) NULL)
596  return((PolygonInfo *) NULL);
597  }
598  ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
599  point=path_info[i].point;
600  points[0]=point;
601  bounds.x1=point.x;
602  bounds.x2=point.x;
603  direction=0;
604  n=1;
605  continue;
606  }
607  /*
608  Line to.
609  */
610  next_direction=((path_info[i].point.y > point.y) ||
611  ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
612  (path_info[i].point.x > point.x))) ? 1 : -1;
613  if ((points != (PointInfo *) NULL) && (direction != 0) &&
614  (direction != next_direction))
615  {
616  /*
617  New edge.
618  */
619  point=points[n-1];
620  if (edge == number_edges)
621  {
622  number_edges<<=1;
623  polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
624  polygon_info->edges,(size_t) number_edges,
625  sizeof(*polygon_info->edges));
626  if (polygon_info->edges == (EdgeInfo *) NULL)
627  return((PolygonInfo *) NULL);
628  }
629  polygon_info->edges[edge].number_points=(size_t) n;
630  polygon_info->edges[edge].scanline=(-1.0);
631  polygon_info->edges[edge].highwater=0;
632  polygon_info->edges[edge].ghostline=ghostline;
633  polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
634  if (direction < 0)
635  ReversePoints(points,(size_t) n);
636  polygon_info->edges[edge].points=points;
637  polygon_info->edges[edge].bounds=bounds;
638  polygon_info->edges[edge].bounds.y1=points[0].y;
639  polygon_info->edges[edge].bounds.y2=points[n-1].y;
640  number_points=16;
641  points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
642  sizeof(*points));
643  if (points == (PointInfo *) NULL)
644  return((PolygonInfo *) NULL);
645  n=1;
646  ghostline=MagickFalse;
647  points[0]=point;
648  bounds.x1=point.x;
649  bounds.x2=point.x;
650  edge++;
651  }
652  direction=next_direction;
653  if (points == (PointInfo *) NULL)
654  continue;
655  if (n == (ssize_t) number_points)
656  {
657  number_points<<=1;
658  points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
659  sizeof(*points));
660  if (points == (PointInfo *) NULL)
661  return((PolygonInfo *) NULL);
662  }
663  point=path_info[i].point;
664  points[n]=point;
665  if (point.x < bounds.x1)
666  bounds.x1=point.x;
667  if (point.x > bounds.x2)
668  bounds.x2=point.x;
669  n++;
670  }
671  if (points != (PointInfo *) NULL)
672  {
673  if (n < 2)
674  points=(PointInfo *) RelinquishMagickMemory(points);
675  else
676  {
677  if (edge == number_edges)
678  {
679  number_edges<<=1;
680  polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
681  polygon_info->edges,(size_t) number_edges,
682  sizeof(*polygon_info->edges));
683  if (polygon_info->edges == (EdgeInfo *) NULL)
684  return((PolygonInfo *) NULL);
685  }
686  polygon_info->edges[edge].number_points=(size_t) n;
687  polygon_info->edges[edge].scanline=(-1.0);
688  polygon_info->edges[edge].highwater=0;
689  polygon_info->edges[edge].ghostline=ghostline;
690  polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
691  if (direction < 0)
692  ReversePoints(points,(size_t) n);
693  polygon_info->edges[edge].points=points;
694  polygon_info->edges[edge].bounds=bounds;
695  polygon_info->edges[edge].bounds.y1=points[0].y;
696  polygon_info->edges[edge].bounds.y2=points[n-1].y;
697  ghostline=MagickFalse;
698  edge++;
699  }
700  }
701  polygon_info->number_edges=edge;
702  qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
703  sizeof(*polygon_info->edges),DrawCompareEdges);
704  if (IsEventLogging() != MagickFalse)
705  LogPolygonInfo(polygon_info);
706  return(polygon_info);
707 }
708 
709 /*
710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711 % %
712 % %
713 % %
714 + C o n v e r t P r i m i t i v e T o P a t h %
715 % %
716 % %
717 % %
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 %
720 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
721 % path structure.
722 %
723 % The format of the ConvertPrimitiveToPath method is:
724 %
725 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
726 % const PrimitiveInfo *primitive_info)
727 %
728 % A description of each parameter follows:
729 %
730 % o Method ConvertPrimitiveToPath returns a vector path structure of type
731 % PathInfo.
732 %
733 % o draw_info: a structure of type DrawInfo.
734 %
735 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
736 %
737 %
738 */
739 
740 static void LogPathInfo(const PathInfo *path_info)
741 {
742  register const PathInfo
743  *p;
744 
745  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
746  for (p=path_info; p->code != EndCode; p++)
748  " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
749  "moveto ghostline" : p->code == OpenCode ? "moveto open" :
750  p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
751  "?");
752  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
753 }
754 
755 static PathInfo *ConvertPrimitiveToPath(const PrimitiveInfo *primitive_info)
756 {
758  closed_subpath;
759 
760  PathInfo
761  *path_info;
762 
764  code;
765 
766  PointInfo
767  p,
768  q;
769 
770  register ssize_t
771  i,
772  n;
773 
774  ssize_t
775  coordinates,
776  start;
777 
778  /*
779  Converts a PrimitiveInfo structure into a vector path structure.
780  */
781  switch (primitive_info->primitive)
782  {
783  case AlphaPrimitive:
784  case ColorPrimitive:
785  case ImagePrimitive:
786  case PointPrimitive:
787  case TextPrimitive:
788  return((PathInfo *) NULL);
789  default:
790  break;
791  }
792  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
793  path_info=(PathInfo *) AcquireQuantumMemory((size_t) (3UL*i+1UL),
794  sizeof(*path_info));
795  if (path_info == (PathInfo *) NULL)
796  return((PathInfo *) NULL);
797  coordinates=0;
798  closed_subpath=MagickFalse;
799  n=0;
800  p.x=(-1.0);
801  p.y=(-1.0);
802  q.x=(-1.0);
803  q.y=(-1.0);
804  start=0;
805  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
806  {
807  code=LineToCode;
808  if (coordinates <= 0)
809  {
810  /*
811  New subpath.
812  */
813  coordinates=(ssize_t) primitive_info[i].coordinates;
814  p=primitive_info[i].point;
815  start=n;
816  code=MoveToCode;
817  closed_subpath=primitive_info[i].closed_subpath;
818  }
819  coordinates--;
820  if ((code == MoveToCode) || (coordinates <= 0) ||
821  (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
822  (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
823  {
824  /*
825  Eliminate duplicate points.
826  */
827  path_info[n].code=code;
828  path_info[n].point=primitive_info[i].point;
829  q=primitive_info[i].point;
830  n++;
831  }
832  if (coordinates > 0)
833  continue; /* next point in current subpath */
834  if (closed_subpath != MagickFalse)
835  {
836  closed_subpath=MagickFalse;
837  continue;
838  }
839  /*
840  Mark the p point as open if the subpath is not closed.
841  */
842  path_info[start].code=OpenCode;
843  path_info[n].code=GhostlineCode;
844  path_info[n].point=primitive_info[i].point;
845  n++;
846  path_info[n].code=LineToCode;
847  path_info[n].point=p;
848  n++;
849  }
850  path_info[n].code=EndCode;
851  path_info[n].point.x=0.0;
852  path_info[n].point.y=0.0;
853  if (IsEventLogging() != MagickFalse)
854  LogPathInfo(path_info);
855  path_info=(PathInfo *) ResizeQuantumMemory(path_info,(size_t) (n+1),
856  sizeof(*path_info));
857  return(path_info);
858 }
859 
860 /*
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 % %
863 % %
864 % %
865 % D e s t r o y D r a w I n f o %
866 % %
867 % %
868 % %
869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 %
871 % DestroyDrawInfo() deallocates memory associated with an DrawInfo structure.
872 %
873 % The format of the DestroyDrawInfo method is:
874 %
875 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
876 %
877 % A description of each parameter follows:
878 %
879 % o draw_info: the draw info.
880 %
881 */
883 {
884  assert(draw_info != (DrawInfo *) NULL);
885  if (draw_info->debug != MagickFalse)
886  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
887  assert(draw_info->signature == MagickCoreSignature);
888  if (draw_info->id != (char *) NULL)
889  draw_info->id=DestroyString(draw_info->id);
890  if (draw_info->primitive != (char *) NULL)
891  draw_info->primitive=DestroyString(draw_info->primitive);
892  if (draw_info->text != (char *) NULL)
893  draw_info->text=DestroyString(draw_info->text);
894  if (draw_info->geometry != (char *) NULL)
895  draw_info->geometry=DestroyString(draw_info->geometry);
896  if (draw_info->fill_pattern != (Image *) NULL)
897  draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
898  if (draw_info->stroke_pattern != (Image *) NULL)
899  draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
900  if (draw_info->font != (char *) NULL)
901  draw_info->font=DestroyString(draw_info->font);
902  if (draw_info->metrics != (char *) NULL)
903  draw_info->metrics=DestroyString(draw_info->metrics);
904  if (draw_info->family != (char *) NULL)
905  draw_info->family=DestroyString(draw_info->family);
906  if (draw_info->encoding != (char *) NULL)
907  draw_info->encoding=DestroyString(draw_info->encoding);
908  if (draw_info->density != (char *) NULL)
909  draw_info->density=DestroyString(draw_info->density);
910  if (draw_info->server_name != (char *) NULL)
911  draw_info->server_name=(char *)
913  if (draw_info->dash_pattern != (double *) NULL)
914  draw_info->dash_pattern=(double *) RelinquishMagickMemory(
915  draw_info->dash_pattern);
916  if (draw_info->gradient.stops != (StopInfo *) NULL)
918  draw_info->gradient.stops);
919  if (draw_info->clip_mask != (char *) NULL)
920  draw_info->clip_mask=DestroyString(draw_info->clip_mask);
921  if (draw_info->clipping_mask != (Image *) NULL)
922  draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
923  if (draw_info->composite_mask != (Image *) NULL)
924  draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
925  draw_info->signature=(~MagickCoreSignature);
926  draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
927  return(draw_info);
928 }
929 
930 /*
931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 % %
933 % %
934 % %
935 + D e s t r o y E d g e %
936 % %
937 % %
938 % %
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 %
941 % DestroyEdge() destroys the specified polygon edge.
942 %
943 % The format of the DestroyEdge method is:
944 %
945 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
946 %
947 % A description of each parameter follows:
948 %
949 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
950 %
951 % o edge: the polygon edge number to destroy.
952 %
953 */
954 static size_t DestroyEdge(PolygonInfo *polygon_info,
955  const size_t edge)
956 {
957  assert(edge < polygon_info->number_edges);
958  polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
959  polygon_info->edges[edge].points);
960  polygon_info->number_edges--;
961  if (edge < polygon_info->number_edges)
962  (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
963  (size_t) (polygon_info->number_edges-edge)*sizeof(*polygon_info->edges));
964  return(polygon_info->number_edges);
965 }
966 
967 /*
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969 % %
970 % %
971 % %
972 + D e s t r o y P o l y g o n I n f o %
973 % %
974 % %
975 % %
976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
977 %
978 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
979 %
980 % The format of the DestroyPolygonInfo method is:
981 %
982 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
983 %
984 % A description of each parameter follows:
985 %
986 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
987 %
988 */
990 {
991  register ssize_t
992  i;
993 
994  for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
995  polygon_info->edges[i].points=(PointInfo *)
996  RelinquishMagickMemory(polygon_info->edges[i].points);
997  polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(polygon_info->edges);
998  return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
999 }
1000 
1001 /*
1002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1003 % %
1004 % %
1005 % %
1006 % D r a w A f f i n e I m a g e %
1007 % %
1008 % %
1009 % %
1010 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1011 %
1012 % DrawAffineImage() composites the source over the destination image as
1013 % dictated by the affine transform.
1014 %
1015 % The format of the DrawAffineImage method is:
1016 %
1017 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
1018 % const AffineMatrix *affine,ExceptionInfo *exception)
1019 %
1020 % A description of each parameter follows:
1021 %
1022 % o image: the image.
1023 %
1024 % o source: the source image.
1025 %
1026 % o affine: the affine transform.
1027 %
1028 % o exception: return any errors or warnings in this structure.
1029 %
1030 */
1031 
1032 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
1033  const double y,const SegmentInfo *edge)
1034 {
1035  double
1036  intercept,
1037  z;
1038 
1039  register double
1040  x;
1041 
1042  SegmentInfo
1043  inverse_edge;
1044 
1045  /*
1046  Determine left and right edges.
1047  */
1048  inverse_edge.x1=edge->x1;
1049  inverse_edge.y1=edge->y1;
1050  inverse_edge.x2=edge->x2;
1051  inverse_edge.y2=edge->y2;
1052  z=affine->ry*y+affine->tx;
1053  if (affine->sx >= MagickEpsilon)
1054  {
1055  intercept=(-z/affine->sx);
1056  x=intercept;
1057  if (x > inverse_edge.x1)
1058  inverse_edge.x1=x;
1059  intercept=(-z+(double) image->columns)/affine->sx;
1060  x=intercept;
1061  if (x < inverse_edge.x2)
1062  inverse_edge.x2=x;
1063  }
1064  else
1065  if (affine->sx < -MagickEpsilon)
1066  {
1067  intercept=(-z+(double) image->columns)/affine->sx;
1068  x=intercept;
1069  if (x > inverse_edge.x1)
1070  inverse_edge.x1=x;
1071  intercept=(-z/affine->sx);
1072  x=intercept;
1073  if (x < inverse_edge.x2)
1074  inverse_edge.x2=x;
1075  }
1076  else
1077  if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1078  {
1079  inverse_edge.x2=edge->x1;
1080  return(inverse_edge);
1081  }
1082  /*
1083  Determine top and bottom edges.
1084  */
1085  z=affine->sy*y+affine->ty;
1086  if (affine->rx >= MagickEpsilon)
1087  {
1088  intercept=(-z/affine->rx);
1089  x=intercept;
1090  if (x > inverse_edge.x1)
1091  inverse_edge.x1=x;
1092  intercept=(-z+(double) image->rows)/affine->rx;
1093  x=intercept;
1094  if (x < inverse_edge.x2)
1095  inverse_edge.x2=x;
1096  }
1097  else
1098  if (affine->rx < -MagickEpsilon)
1099  {
1100  intercept=(-z+(double) image->rows)/affine->rx;
1101  x=intercept;
1102  if (x > inverse_edge.x1)
1103  inverse_edge.x1=x;
1104  intercept=(-z/affine->rx);
1105  x=intercept;
1106  if (x < inverse_edge.x2)
1107  inverse_edge.x2=x;
1108  }
1109  else
1110  if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1111  {
1112  inverse_edge.x2=edge->x2;
1113  return(inverse_edge);
1114  }
1115  return(inverse_edge);
1116 }
1117 
1119 {
1120  AffineMatrix
1121  inverse_affine;
1122 
1123  double
1124  determinant;
1125 
1126  determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
1127  affine->ry);
1128  inverse_affine.sx=determinant*affine->sy;
1129  inverse_affine.rx=determinant*(-affine->rx);
1130  inverse_affine.ry=determinant*(-affine->ry);
1131  inverse_affine.sy=determinant*affine->sx;
1132  inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1133  inverse_affine.ry;
1134  inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1135  inverse_affine.sy;
1136  return(inverse_affine);
1137 }
1138 
1140  const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1141 {
1142  AffineMatrix
1143  inverse_affine;
1144 
1145  CacheView
1146  *image_view,
1147  *source_view;
1148 
1150  status;
1151 
1152  PixelInfo
1153  zero;
1154 
1155  PointInfo
1156  extent[4],
1157  min,
1158  max;
1159 
1160  register ssize_t
1161  i;
1162 
1163  SegmentInfo
1164  edge;
1165 
1166  ssize_t
1167  start,
1168  stop,
1169  y;
1170 
1171  /*
1172  Determine bounding box.
1173  */
1174  assert(image != (Image *) NULL);
1175  assert(image->signature == MagickCoreSignature);
1176  if (image->debug != MagickFalse)
1177  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1178  assert(source != (const Image *) NULL);
1179  assert(source->signature == MagickCoreSignature);
1180  assert(affine != (AffineMatrix *) NULL);
1181  extent[0].x=0.0;
1182  extent[0].y=0.0;
1183  extent[1].x=(double) source->columns-1.0;
1184  extent[1].y=0.0;
1185  extent[2].x=(double) source->columns-1.0;
1186  extent[2].y=(double) source->rows-1.0;
1187  extent[3].x=0.0;
1188  extent[3].y=(double) source->rows-1.0;
1189  for (i=0; i < 4; i++)
1190  {
1191  PointInfo
1192  point;
1193 
1194  point=extent[i];
1195  extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1196  extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1197  }
1198  min=extent[0];
1199  max=extent[0];
1200  for (i=1; i < 4; i++)
1201  {
1202  if (min.x > extent[i].x)
1203  min.x=extent[i].x;
1204  if (min.y > extent[i].y)
1205  min.y=extent[i].y;
1206  if (max.x < extent[i].x)
1207  max.x=extent[i].x;
1208  if (max.y < extent[i].y)
1209  max.y=extent[i].y;
1210  }
1211  /*
1212  Affine transform image.
1213  */
1214  if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1215  return(MagickFalse);
1216  status=MagickTrue;
1217  edge.x1=MagickMax(min.x,0.0);
1218  edge.y1=MagickMax(min.y,0.0);
1219  edge.x2=MagickMin(max.x,(double) image->columns-1.0);
1220  edge.y2=MagickMin(max.y,(double) image->rows-1.0);
1221  inverse_affine=InverseAffineMatrix(affine);
1222  GetPixelInfo(image,&zero);
1223  start=(ssize_t) ceil(edge.y1-0.5);
1224  stop=(ssize_t) floor(edge.y2+0.5);
1225  source_view=AcquireVirtualCacheView(source,exception);
1226  image_view=AcquireAuthenticCacheView(image,exception);
1227 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1228  #pragma omp parallel for schedule(static) shared(status) \
1229  magick_number_threads(source,image,stop-start,1)
1230 #endif
1231  for (y=start; y <= stop; y++)
1232  {
1233  PixelInfo
1234  composite,
1235  pixel;
1236 
1237  PointInfo
1238  point;
1239 
1240  register ssize_t
1241  x;
1242 
1243  register Quantum
1244  *magick_restrict q;
1245 
1246  SegmentInfo
1247  inverse_edge;
1248 
1249  ssize_t
1250  x_offset;
1251 
1252  inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1253  if (inverse_edge.x2 < inverse_edge.x1)
1254  continue;
1255  q=GetCacheViewAuthenticPixels(image_view,(ssize_t) ceil(inverse_edge.x1-
1256  0.5),y,(size_t) (floor(inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),
1257  1,exception);
1258  if (q == (Quantum *) NULL)
1259  continue;
1260  pixel=zero;
1261  composite=zero;
1262  x_offset=0;
1263  for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1264  {
1265  point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1266  inverse_affine.tx;
1267  point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1268  inverse_affine.ty;
1269  status=InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1270  point.x,point.y,&pixel,exception);
1271  if (status == MagickFalse)
1272  break;
1273  GetPixelInfoPixel(image,q,&composite);
1274  CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1275  &composite);
1276  SetPixelViaPixelInfo(image,&composite,q);
1277  x_offset++;
1278  q+=GetPixelChannels(image);
1279  }
1280  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1281  status=MagickFalse;
1282  }
1283  source_view=DestroyCacheView(source_view);
1284  image_view=DestroyCacheView(image_view);
1285  return(status);
1286 }
1287 
1288 /*
1289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1290 % %
1291 % %
1292 % %
1293 + D r a w B o u n d i n g R e c t a n g l e s %
1294 % %
1295 % %
1296 % %
1297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1298 %
1299 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1300 % is only useful for developers debugging the rendering algorithm.
1301 %
1302 % The format of the DrawBoundingRectangles method is:
1303 %
1304 % MagickBooleanType DrawBoundingRectangles(Image *image,
1305 % const DrawInfo *draw_info,PolygonInfo *polygon_info,
1306 % ExceptionInfo *exception)
1307 %
1308 % A description of each parameter follows:
1309 %
1310 % o image: the image.
1311 %
1312 % o draw_info: the draw info.
1313 %
1314 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1315 %
1316 % o exception: return any errors or warnings in this structure.
1317 %
1318 */
1319 
1320 static inline double SaneStrokeWidth(const Image *image,
1321  const DrawInfo *draw_info)
1322 {
1323  return(MagickMin((double) draw_info->stroke_width,
1324  (2.0*sqrt(2.0)+MagickEpsilon)*MagickMax(image->columns,image->rows)));
1325 }
1326 
1328  const DrawInfo *draw_info,const PolygonInfo *polygon_info,
1329  ExceptionInfo *exception)
1330 {
1331  double
1332  mid;
1333 
1334  DrawInfo
1335  *clone_info;
1336 
1338  status;
1339 
1340  PointInfo
1341  end,
1342  resolution,
1343  start;
1344 
1346  primitive_info[6];
1347 
1348  register ssize_t
1349  i;
1350 
1351  SegmentInfo
1352  bounds;
1353 
1354  ssize_t
1355  coordinates;
1356 
1357  (void) memset(primitive_info,0,sizeof(primitive_info));
1358  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1359  status=QueryColorCompliance("#000F",AllCompliance,&clone_info->fill,
1360  exception);
1361  if (status == MagickFalse)
1362  {
1363  clone_info=DestroyDrawInfo(clone_info);
1364  return(MagickFalse);
1365  }
1366  resolution.x=96.0;
1367  resolution.y=96.0;
1368  if (clone_info->density != (char *) NULL)
1369  {
1370  GeometryInfo
1371  geometry_info;
1372 
1374  flags;
1375 
1376  flags=ParseGeometry(clone_info->density,&geometry_info);
1377  resolution.x=geometry_info.rho;
1378  resolution.y=geometry_info.sigma;
1379  if ((flags & SigmaValue) == MagickFalse)
1380  resolution.y=resolution.x;
1381  }
1382  mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1383  SaneStrokeWidth(image,clone_info)/2.0;
1384  bounds.x1=0.0;
1385  bounds.y1=0.0;
1386  bounds.x2=0.0;
1387  bounds.y2=0.0;
1388  if (polygon_info != (PolygonInfo *) NULL)
1389  {
1390  bounds=polygon_info->edges[0].bounds;
1391  for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1392  {
1393  if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1394  bounds.x1=polygon_info->edges[i].bounds.x1;
1395  if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1396  bounds.y1=polygon_info->edges[i].bounds.y1;
1397  if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1398  bounds.x2=polygon_info->edges[i].bounds.x2;
1399  if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1400  bounds.y2=polygon_info->edges[i].bounds.y2;
1401  }
1402  bounds.x1-=mid;
1403  bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1404  image->columns ? (double) image->columns-1 : bounds.x1;
1405  bounds.y1-=mid;
1406  bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1407  image->rows ? (double) image->rows-1 : bounds.y1;
1408  bounds.x2+=mid;
1409  bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1410  image->columns ? (double) image->columns-1 : bounds.x2;
1411  bounds.y2+=mid;
1412  bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1413  image->rows ? (double) image->rows-1 : bounds.y2;
1414  for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1415  {
1416  if (polygon_info->edges[i].direction != 0)
1417  status=QueryColorCompliance("#f00",AllCompliance,&clone_info->stroke,
1418  exception);
1419  else
1420  status=QueryColorCompliance("#0f0",AllCompliance,&clone_info->stroke,
1421  exception);
1422  if (status == MagickFalse)
1423  break;
1424  start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1425  start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1426  end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1427  end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1428  primitive_info[0].primitive=RectanglePrimitive;
1429  status&=TraceRectangle(primitive_info,start,end);
1430  primitive_info[0].method=ReplaceMethod;
1431  coordinates=(ssize_t) primitive_info[0].coordinates;
1432  primitive_info[coordinates].primitive=UndefinedPrimitive;
1433  status=DrawPrimitive(image,clone_info,primitive_info,exception);
1434  if (status == MagickFalse)
1435  break;
1436  }
1437  if (i < (ssize_t) polygon_info->number_edges)
1438  {
1439  clone_info=DestroyDrawInfo(clone_info);
1440  return(status == 0 ? MagickFalse : MagickTrue);
1441  }
1442  }
1443  status=QueryColorCompliance("#00f",AllCompliance,&clone_info->stroke,
1444  exception);
1445  if (status == MagickFalse)
1446  {
1447  clone_info=DestroyDrawInfo(clone_info);
1448  return(MagickFalse);
1449  }
1450  start.x=(double) (bounds.x1-mid);
1451  start.y=(double) (bounds.y1-mid);
1452  end.x=(double) (bounds.x2+mid);
1453  end.y=(double) (bounds.y2+mid);
1454  primitive_info[0].primitive=RectanglePrimitive;
1455  status&=TraceRectangle(primitive_info,start,end);
1456  primitive_info[0].method=ReplaceMethod;
1457  coordinates=(ssize_t) primitive_info[0].coordinates;
1458  primitive_info[coordinates].primitive=UndefinedPrimitive;
1459  status=DrawPrimitive(image,clone_info,primitive_info,exception);
1460  clone_info=DestroyDrawInfo(clone_info);
1461  return(status == 0 ? MagickFalse : MagickTrue);
1462 }
1463 
1464 /*
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1466 % %
1467 % %
1468 % %
1469 % D r a w C l i p P a t h %
1470 % %
1471 % %
1472 % %
1473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1474 %
1475 % DrawClipPath() draws the clip path on the image mask.
1476 %
1477 % The format of the DrawClipPath method is:
1478 %
1479 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1480 % const char *id,ExceptionInfo *exception)
1481 %
1482 % A description of each parameter follows:
1483 %
1484 % o image: the image.
1485 %
1486 % o draw_info: the draw info.
1487 %
1488 % o id: the clip path id.
1489 %
1490 % o exception: return any errors or warnings in this structure.
1491 %
1492 */
1494  const DrawInfo *draw_info,const char *id,ExceptionInfo *exception)
1495 {
1496  const char
1497  *clip_path;
1498 
1499  Image
1500  *clipping_mask;
1501 
1503  status;
1504 
1505  clip_path=GetImageArtifact(image,id);
1506  if (clip_path == (const char *) NULL)
1507  return(MagickFalse);
1508  clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1509  exception);
1510  if (clipping_mask == (Image *) NULL)
1511  return(MagickFalse);
1512  status=SetImageMask(image,WritePixelMask,clipping_mask,exception);
1513  clipping_mask=DestroyImage(clipping_mask);
1514  return(status);
1515 }
1516 
1517 /*
1518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1519 % %
1520 % %
1521 % %
1522 % D r a w C l i p p i n g M a s k %
1523 % %
1524 % %
1525 % %
1526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1527 %
1528 % DrawClippingMask() draws the clip path and returns it as an image clipping
1529 % mask.
1530 %
1531 % The format of the DrawClippingMask method is:
1532 %
1533 % Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1534 % const char *id,const char *clip_path,ExceptionInfo *exception)
1535 %
1536 % A description of each parameter follows:
1537 %
1538 % o image: the image.
1539 %
1540 % o draw_info: the draw info.
1541 %
1542 % o id: the clip path id.
1543 %
1544 % o clip_path: the clip path.
1545 %
1546 % o exception: return any errors or warnings in this structure.
1547 %
1548 */
1549 static Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1550  const char *id,const char *clip_path,ExceptionInfo *exception)
1551 {
1552  DrawInfo
1553  *clone_info;
1554 
1555  Image
1556  *clip_mask,
1557  *separate_mask;
1558 
1560  status;
1561 
1562  /*
1563  Draw a clip path.
1564  */
1565  assert(image != (Image *) NULL);
1566  assert(image->signature == MagickCoreSignature);
1567  if (image->debug != MagickFalse)
1568  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1569  assert(draw_info != (const DrawInfo *) NULL);
1570  clip_mask=AcquireImage((const ImageInfo *) NULL,exception);
1571  status=SetImageExtent(clip_mask,image->columns,image->rows,exception);
1572  if (status == MagickFalse)
1573  return(DestroyImage(clip_mask));
1574  status=SetImageMask(clip_mask,WritePixelMask,(Image *) NULL,exception);
1575  status=QueryColorCompliance("#0000",AllCompliance,
1576  &clip_mask->background_color,exception);
1579  status=SetImageBackgroundColor(clip_mask,exception);
1580  if (image->debug != MagickFalse)
1581  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1582  id);
1583  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1584  (void) CloneString(&clone_info->primitive,clip_path);
1585  status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1586  exception);
1587  if (clone_info->clip_mask != (char *) NULL)
1588  clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1589  status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1590  exception);
1591  clone_info->stroke_width=0.0;
1592  clone_info->alpha=OpaqueAlpha;
1593  clone_info->clip_path=MagickTrue;
1594  status=RenderMVGContent(clip_mask,clone_info,0,exception);
1595  clone_info=DestroyDrawInfo(clone_info);
1596  separate_mask=SeparateImage(clip_mask,AlphaChannel,exception);
1597  if (separate_mask != (Image *) NULL)
1598  {
1599  clip_mask=DestroyImage(clip_mask);
1600  clip_mask=separate_mask;
1601  status=NegateImage(clip_mask,MagickFalse,exception);
1602  if (status == MagickFalse)
1603  clip_mask=DestroyImage(clip_mask);
1604  }
1605  if (image->debug != MagickFalse)
1606  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1607  return(clip_mask);
1608 }
1609 
1610 /*
1611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1612 % %
1613 % %
1614 % %
1615 % D r a w C o m p o s i t e M a s k %
1616 % %
1617 % %
1618 % %
1619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1620 %
1621 % DrawCompositeMask() draws the mask path and returns it as an image mask.
1622 %
1623 % The format of the DrawCompositeMask method is:
1624 %
1625 % Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1626 % const char *id,const char *mask_path,ExceptionInfo *exception)
1627 %
1628 % A description of each parameter follows:
1629 %
1630 % o image: the image.
1631 %
1632 % o draw_info: the draw info.
1633 %
1634 % o id: the mask path id.
1635 %
1636 % o mask_path: the mask path.
1637 %
1638 % o exception: return any errors or warnings in this structure.
1639 %
1640 */
1641 static Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1642  const char *id,const char *mask_path,ExceptionInfo *exception)
1643 {
1644  Image
1645  *composite_mask,
1646  *separate_mask;
1647 
1648  DrawInfo
1649  *clone_info;
1650 
1652  status;
1653 
1654  /*
1655  Draw a mask path.
1656  */
1657  assert(image != (Image *) NULL);
1658  assert(image->signature == MagickCoreSignature);
1659  if (image->debug != MagickFalse)
1660  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1661  assert(draw_info != (const DrawInfo *) NULL);
1662  composite_mask=AcquireImage((const ImageInfo *) NULL,exception);
1663  status=SetImageExtent(composite_mask,image->columns,image->rows,exception);
1664  if (status == MagickFalse)
1665  return(DestroyImage(composite_mask));
1666  status=SetImageMask(composite_mask,CompositePixelMask,(Image *) NULL,
1667  exception);
1668  status=QueryColorCompliance("#0000",AllCompliance,
1669  &composite_mask->background_color,exception);
1672  (void) SetImageBackgroundColor(composite_mask,exception);
1673  if (image->debug != MagickFalse)
1674  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin mask-path %s",
1675  id);
1676  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1677  (void) CloneString(&clone_info->primitive,mask_path);
1678  status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1679  exception);
1680  status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1681  exception);
1682  clone_info->stroke_width=0.0;
1683  clone_info->alpha=OpaqueAlpha;
1684  status=RenderMVGContent(composite_mask,clone_info,0,exception);
1685  clone_info=DestroyDrawInfo(clone_info);
1686  separate_mask=SeparateImage(composite_mask,AlphaChannel,exception);
1687  if (separate_mask != (Image *) NULL)
1688  {
1689  composite_mask=DestroyImage(composite_mask);
1690  composite_mask=separate_mask;
1691  status=NegateImage(composite_mask,MagickFalse,exception);
1692  if (status == MagickFalse)
1693  composite_mask=DestroyImage(composite_mask);
1694  }
1695  if (image->debug != MagickFalse)
1696  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end mask-path");
1697  return(composite_mask);
1698 }
1699 
1700 /*
1701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1702 % %
1703 % %
1704 % %
1705 + D r a w D a s h P o l y g o n %
1706 % %
1707 % %
1708 % %
1709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710 %
1711 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1712 % image while respecting the dash offset and dash pattern attributes.
1713 %
1714 % The format of the DrawDashPolygon method is:
1715 %
1716 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1717 % const PrimitiveInfo *primitive_info,Image *image,
1718 % ExceptionInfo *exception)
1719 %
1720 % A description of each parameter follows:
1721 %
1722 % o draw_info: the draw info.
1723 %
1724 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1725 %
1726 % o image: the image.
1727 %
1728 % o exception: return any errors or warnings in this structure.
1729 %
1730 */
1732  const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1733 {
1734  double
1735  length,
1736  maximum_length,
1737  offset,
1738  scale,
1739  total_length;
1740 
1741  DrawInfo
1742  *clone_info;
1743 
1745  status;
1746 
1748  *dash_polygon;
1749 
1750  register double
1751  dx,
1752  dy;
1753 
1754  register ssize_t
1755  i;
1756 
1757  size_t
1758  number_vertices;
1759 
1760  ssize_t
1761  j,
1762  n;
1763 
1764  assert(draw_info != (const DrawInfo *) NULL);
1765  if (image->debug != MagickFalse)
1766  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1767  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1768  number_vertices=(size_t) i;
1769  dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1770  (2UL*number_vertices+32UL),sizeof(*dash_polygon));
1771  if (dash_polygon == (PrimitiveInfo *) NULL)
1772  return(MagickFalse);
1773  (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1774  sizeof(*dash_polygon));
1775  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1776  clone_info->miterlimit=0;
1777  dash_polygon[0]=primitive_info[0];
1778  scale=ExpandAffine(&draw_info->affine);
1779  length=scale*draw_info->dash_pattern[0];
1780  offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1781  scale*draw_info->dash_offset : 0.0;
1782  j=1;
1783  for (n=0; offset > 0.0; j=0)
1784  {
1785  if (draw_info->dash_pattern[n] <= 0.0)
1786  break;
1787  length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1788  if (offset > length)
1789  {
1790  offset-=length;
1791  n++;
1792  length=scale*draw_info->dash_pattern[n];
1793  continue;
1794  }
1795  if (offset < length)
1796  {
1797  length-=offset;
1798  offset=0.0;
1799  break;
1800  }
1801  offset=0.0;
1802  n++;
1803  }
1804  status=MagickTrue;
1805  maximum_length=0.0;
1806  total_length=0.0;
1807  for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1808  {
1809  dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1810  dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1811  maximum_length=hypot(dx,dy);
1812  if (maximum_length > MaxBezierCoordinates)
1813  break;
1814  if (fabs(length) < MagickEpsilon)
1815  {
1816  if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1817  n++;
1818  if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1819  n=0;
1820  length=scale*draw_info->dash_pattern[n];
1821  }
1822  for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1823  {
1824  total_length+=length;
1825  if ((n & 0x01) != 0)
1826  {
1827  dash_polygon[0]=primitive_info[0];
1828  dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1829  total_length*PerceptibleReciprocal(maximum_length));
1830  dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1831  total_length*PerceptibleReciprocal(maximum_length));
1832  j=1;
1833  }
1834  else
1835  {
1836  if ((j+1) > (ssize_t) number_vertices)
1837  break;
1838  dash_polygon[j]=primitive_info[i-1];
1839  dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1840  total_length*PerceptibleReciprocal(maximum_length));
1841  dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1842  total_length*PerceptibleReciprocal(maximum_length));
1843  dash_polygon[j].coordinates=1;
1844  j++;
1845  dash_polygon[0].coordinates=(size_t) j;
1846  dash_polygon[j].primitive=UndefinedPrimitive;
1847  status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1848  if (status == MagickFalse)
1849  break;
1850  }
1851  if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1852  n++;
1853  if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1854  n=0;
1855  length=scale*draw_info->dash_pattern[n];
1856  }
1857  length-=(maximum_length-total_length);
1858  if ((n & 0x01) != 0)
1859  continue;
1860  dash_polygon[j]=primitive_info[i];
1861  dash_polygon[j].coordinates=1;
1862  j++;
1863  }
1864  if ((status != MagickFalse) && (total_length < maximum_length) &&
1865  ((n & 0x01) == 0) && (j > 1))
1866  {
1867  dash_polygon[j]=primitive_info[i-1];
1868  dash_polygon[j].point.x+=MagickEpsilon;
1869  dash_polygon[j].point.y+=MagickEpsilon;
1870  dash_polygon[j].coordinates=1;
1871  j++;
1872  dash_polygon[0].coordinates=(size_t) j;
1873  dash_polygon[j].primitive=UndefinedPrimitive;
1874  status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1875  }
1876  dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1877  clone_info=DestroyDrawInfo(clone_info);
1878  if (image->debug != MagickFalse)
1879  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1880  return(status != 0 ? MagickTrue : MagickFalse);
1881 }
1882 
1883 /*
1884 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1885 % %
1886 % %
1887 % %
1888 % D r a w G r a d i e n t I m a g e %
1889 % %
1890 % %
1891 % %
1892 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1893 %
1894 % DrawGradientImage() draws a linear gradient on the image.
1895 %
1896 % The format of the DrawGradientImage method is:
1897 %
1898 % MagickBooleanType DrawGradientImage(Image *image,
1899 % const DrawInfo *draw_info,ExceptionInfo *exception)
1900 %
1901 % A description of each parameter follows:
1902 %
1903 % o image: the image.
1904 %
1905 % o draw_info: the draw info.
1906 %
1907 % o exception: return any errors or warnings in this structure.
1908 %
1909 */
1910 
1911 static inline double GetStopColorOffset(const GradientInfo *gradient,
1912  const ssize_t x,const ssize_t y)
1913 {
1914  switch (gradient->type)
1915  {
1916  case UndefinedGradient:
1917  case LinearGradient:
1918  {
1919  double
1920  gamma,
1921  length,
1922  offset,
1923  scale;
1924 
1925  PointInfo
1926  p,
1927  q;
1928 
1929  const SegmentInfo
1930  *gradient_vector;
1931 
1932  gradient_vector=(&gradient->gradient_vector);
1933  p.x=gradient_vector->x2-gradient_vector->x1;
1934  p.y=gradient_vector->y2-gradient_vector->y1;
1935  q.x=(double) x-gradient_vector->x1;
1936  q.y=(double) y-gradient_vector->y1;
1937  length=sqrt(q.x*q.x+q.y*q.y);
1938  gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1939  gamma=PerceptibleReciprocal(gamma);
1940  scale=p.x*q.x+p.y*q.y;
1941  offset=gamma*scale*length;
1942  return(offset);
1943  }
1944  case RadialGradient:
1945  {
1946  PointInfo
1947  v;
1948 
1949  if (gradient->spread == RepeatSpread)
1950  {
1951  v.x=(double) x-gradient->center.x;
1952  v.y=(double) y-gradient->center.y;
1953  return(sqrt(v.x*v.x+v.y*v.y));
1954  }
1955  v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1956  gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1957  gradient->angle))))*PerceptibleReciprocal(gradient->radii.x);
1958  v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1959  gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1960  gradient->angle))))*PerceptibleReciprocal(gradient->radii.y);
1961  return(sqrt(v.x*v.x+v.y*v.y));
1962  }
1963  }
1964  return(0.0);
1965 }
1966 
1967 static int StopInfoCompare(const void *x,const void *y)
1968 {
1969  StopInfo
1970  *stop_1,
1971  *stop_2;
1972 
1973  stop_1=(StopInfo *) x;
1974  stop_2=(StopInfo *) y;
1975  if (stop_1->offset > stop_2->offset)
1976  return(1);
1977  if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
1978  return(0);
1979  return(-1);
1980 }
1981 
1983  const DrawInfo *draw_info,ExceptionInfo *exception)
1984 {
1985  CacheView
1986  *image_view;
1987 
1988  const GradientInfo
1989  *gradient;
1990 
1991  const SegmentInfo
1992  *gradient_vector;
1993 
1994  double
1995  length;
1996 
1998  status;
1999 
2000  PixelInfo
2001  zero;
2002 
2003  PointInfo
2004  point;
2005 
2007  bounding_box;
2008 
2009  ssize_t
2010  y;
2011 
2012  /*
2013  Draw linear or radial gradient on image.
2014  */
2015  assert(image != (Image *) NULL);
2016  assert(image->signature == MagickCoreSignature);
2017  if (image->debug != MagickFalse)
2018  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2019  assert(draw_info != (const DrawInfo *) NULL);
2020  gradient=(&draw_info->gradient);
2021  qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
2022  StopInfoCompare);
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  GetPixelInfo(image,&zero);
2030  image_view=AcquireAuthenticCacheView(image,exception);
2031 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2032  #pragma omp parallel for schedule(static) shared(status) \
2033  magick_number_threads(image,image,bounding_box.height-bounding_box.y,1)
2034 #endif
2035  for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
2036  {
2037  PixelInfo
2038  composite,
2039  pixel;
2040 
2041  double
2042  alpha,
2043  offset;
2044 
2045  register Quantum
2046  *magick_restrict q;
2047 
2048  register ssize_t
2049  i,
2050  x;
2051 
2052  ssize_t
2053  j;
2054 
2055  if (status == MagickFalse)
2056  continue;
2057  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2058  if (q == (Quantum *) NULL)
2059  {
2060  status=MagickFalse;
2061  continue;
2062  }
2063  pixel=zero;
2064  composite=zero;
2065  offset=GetStopColorOffset(gradient,0,y);
2066  if (gradient->type != RadialGradient)
2067  offset*=PerceptibleReciprocal(length);
2068  for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
2069  {
2070  GetPixelInfoPixel(image,q,&pixel);
2071  switch (gradient->spread)
2072  {
2073  case UndefinedSpread:
2074  case PadSpread:
2075  {
2076  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
2077  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
2078  {
2079  offset=GetStopColorOffset(gradient,x,y);
2080  if (gradient->type != RadialGradient)
2081  offset*=PerceptibleReciprocal(length);
2082  }
2083  for (i=0; i < (ssize_t) gradient->number_stops; i++)
2084  if (offset < gradient->stops[i].offset)
2085  break;
2086  if ((offset < 0.0) || (i == 0))
2087  composite=gradient->stops[0].color;
2088  else
2089  if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2090  composite=gradient->stops[gradient->number_stops-1].color;
2091  else
2092  {
2093  j=i;
2094  i--;
2095  alpha=(offset-gradient->stops[i].offset)/
2096  (gradient->stops[j].offset-gradient->stops[i].offset);
2097  CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2098  &gradient->stops[j].color,alpha,&composite);
2099  }
2100  break;
2101  }
2102  case ReflectSpread:
2103  {
2104  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
2105  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
2106  {
2107  offset=GetStopColorOffset(gradient,x,y);
2108  if (gradient->type != RadialGradient)
2109  offset*=PerceptibleReciprocal(length);
2110  }
2111  if (offset < 0.0)
2112  offset=(-offset);
2113  if ((ssize_t) fmod(offset,2.0) == 0)
2114  offset=fmod(offset,1.0);
2115  else
2116  offset=1.0-fmod(offset,1.0);
2117  for (i=0; i < (ssize_t) gradient->number_stops; i++)
2118  if (offset < gradient->stops[i].offset)
2119  break;
2120  if (i == 0)
2121  composite=gradient->stops[0].color;
2122  else
2123  if (i == (ssize_t) gradient->number_stops)
2124  composite=gradient->stops[gradient->number_stops-1].color;
2125  else
2126  {
2127  j=i;
2128  i--;
2129  alpha=(offset-gradient->stops[i].offset)/
2130  (gradient->stops[j].offset-gradient->stops[i].offset);
2131  CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2132  &gradient->stops[j].color,alpha,&composite);
2133  }
2134  break;
2135  }
2136  case RepeatSpread:
2137  {
2139  antialias;
2140 
2141  double
2142  repeat;
2143 
2144  antialias=MagickFalse;
2145  repeat=0.0;
2146  if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
2147  (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
2148  {
2149  offset=GetStopColorOffset(gradient,x,y);
2150  if (gradient->type == LinearGradient)
2151  {
2152  repeat=fmod(offset,length);
2153  if (repeat < 0.0)
2154  repeat=length-fmod(-repeat,length);
2155  else
2156  repeat=fmod(offset,length);
2157  antialias=(repeat < length) && ((repeat+1.0) > length) ?
2159  offset=PerceptibleReciprocal(length)*repeat;
2160  }
2161  else
2162  {
2163  repeat=fmod(offset,gradient->radius);
2164  if (repeat < 0.0)
2165  repeat=gradient->radius-fmod(-repeat,gradient->radius);
2166  else
2167  repeat=fmod(offset,gradient->radius);
2168  antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2169  MagickFalse;
2170  offset=repeat/gradient->radius;
2171  }
2172  }
2173  for (i=0; i < (ssize_t) gradient->number_stops; i++)
2174  if (offset < gradient->stops[i].offset)
2175  break;
2176  if (i == 0)
2177  composite=gradient->stops[0].color;
2178  else
2179  if (i == (ssize_t) gradient->number_stops)
2180  composite=gradient->stops[gradient->number_stops-1].color;
2181  else
2182  {
2183  j=i;
2184  i--;
2185  alpha=(offset-gradient->stops[i].offset)/
2186  (gradient->stops[j].offset-gradient->stops[i].offset);
2187  if (antialias != MagickFalse)
2188  {
2189  if (gradient->type == LinearGradient)
2190  alpha=length-repeat;
2191  else
2192  alpha=gradient->radius-repeat;
2193  i=0;
2194  j=(ssize_t) gradient->number_stops-1L;
2195  }
2196  CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2197  &gradient->stops[j].color,alpha,&composite);
2198  }
2199  break;
2200  }
2201  }
2202  CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
2203  &pixel);
2204  SetPixelViaPixelInfo(image,&pixel,q);
2205  q+=GetPixelChannels(image);
2206  }
2207  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2208  status=MagickFalse;
2209  }
2210  image_view=DestroyCacheView(image_view);
2211  return(status);
2212 }
2213 
2214 /*
2215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2216 % %
2217 % %
2218 % %
2219 % D r a w I m a g e %
2220 % %
2221 % %
2222 % %
2223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2224 %
2225 % DrawImage() draws a graphic primitive on your image. The primitive
2226 % may be represented as a string or filename. Precede the filename with an
2227 % "at" sign (@) and the contents of the file are drawn on the image. You
2228 % can affect how text is drawn by setting one or more members of the draw
2229 % info structure.
2230 %
2231 % The format of the DrawImage method is:
2232 %
2233 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
2234 % ExceptionInfo *exception)
2235 %
2236 % A description of each parameter follows:
2237 %
2238 % o image: the image.
2239 %
2240 % o draw_info: the draw info.
2241 %
2242 % o exception: return any errors or warnings in this structure.
2243 %
2244 */
2245 
2247  const size_t pad)
2248 {
2249  double
2250  extent;
2251 
2252  size_t
2253  quantum;
2254 
2255  /*
2256  Check if there is enough storage for drawing pimitives.
2257  */
2258  extent=(double) mvg_info->offset+pad+PrimitiveExtentPad;
2259  quantum=sizeof(**mvg_info->primitive_info);
2260  if (((extent*quantum) < (double) SSIZE_MAX) &&
2261  ((extent*quantum) < (double) GetMaxMemoryRequest()))
2262  {
2263  if (extent <= (double) *mvg_info->extent)
2264  return(MagickTrue);
2266  *mvg_info->primitive_info,(size_t) extent,quantum);
2267  if (*mvg_info->primitive_info != (PrimitiveInfo *) NULL)
2268  {
2269  register ssize_t
2270  i;
2271 
2272  *mvg_info->extent=(size_t) extent;
2273  for (i=mvg_info->offset+1; i < (ssize_t) extent; i++)
2274  (*mvg_info->primitive_info)[i].primitive=UndefinedPrimitive;
2275  return(MagickTrue);
2276  }
2277  }
2278  /*
2279  Reallocation failed, allocate a primitive to facilitate unwinding.
2280  */
2281  (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
2282  ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2283  if (*mvg_info->primitive_info != (PrimitiveInfo *) NULL)
2285  *mvg_info->primitive_info);
2287  PrimitiveExtentPad*quantum);
2288  (void) memset(*mvg_info->primitive_info,0,PrimitiveExtentPad*quantum);
2289  *mvg_info->extent=1;
2290  return(MagickFalse);
2291 }
2292 
2293 MagickExport int MVGMacroCompare(const void *target,const void *source)
2294 {
2295  const char
2296  *p,
2297  *q;
2298 
2299  p=(const char *) target;
2300  q=(const char *) source;
2301  return(strcmp(p,q));
2302 }
2303 
2304 static SplayTreeInfo *GetMVGMacros(const char *primitive)
2305 {
2306  char
2307  *macro,
2308  *token;
2309 
2310  const char
2311  *q;
2312 
2313  size_t
2314  extent;
2315 
2317  *macros;
2318 
2319  /*
2320  Scan graphic primitives for definitions and classes.
2321  */
2322  if (primitive == (const char *) NULL)
2323  return((SplayTreeInfo *) NULL);
2326  macro=AcquireString(primitive);
2327  token=AcquireString(primitive);
2328  extent=strlen(token)+MagickPathExtent;
2329  for (q=primitive; *q != '\0'; )
2330  {
2331  if (GetNextToken(q,&q,extent,token) < 1)
2332  break;
2333  if (*token == '\0')
2334  break;
2335  if (LocaleCompare("push",token) == 0)
2336  {
2337  register const char
2338  *end,
2339  *start;
2340 
2341  (void) GetNextToken(q,&q,extent,token);
2342  if (*q == '"')
2343  {
2344  char
2345  name[MagickPathExtent];
2346 
2347  const char
2348  *p;
2349 
2350  ssize_t
2351  n;
2352 
2353  /*
2354  Named macro (e.g. push graphic-context "wheel").
2355  */
2356  (void) GetNextToken(q,&q,extent,token);
2357  start=q;
2358  end=q;
2359  (void) CopyMagickString(name,token,MagickPathExtent);
2360  n=1;
2361  for (p=q; *p != '\0'; )
2362  {
2363  if (GetNextToken(p,&p,extent,token) < 1)
2364  break;
2365  if (*token == '\0')
2366  break;
2367  if (LocaleCompare(token,"pop") == 0)
2368  {
2369  end=p-strlen(token)-1;
2370  n--;
2371  }
2372  if (LocaleCompare(token,"push") == 0)
2373  n++;
2374  if ((n == 0) && (end > start))
2375  {
2376  /*
2377  Extract macro.
2378  */
2379  (void) GetNextToken(p,&p,extent,token);
2380  (void) CopyMagickString(macro,start,(size_t) (end-start));
2381  (void) AddValueToSplayTree(macros,ConstantString(name),
2382  ConstantString(macro));
2383  break;
2384  }
2385  }
2386  }
2387  }
2388  }
2389  token=DestroyString(token);
2390  macro=DestroyString(macro);
2391  return(macros);
2392 }
2393 
2394 static inline MagickBooleanType IsPoint(const char *point)
2395 {
2396  char
2397  *p;
2398 
2399  double
2400  value;
2401 
2402  value=StringToDouble(point,&p);
2403  return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2404  MagickTrue);
2405 }
2406 
2407 static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2408  const PointInfo point)
2409 {
2410  primitive_info->coordinates=1;
2411  primitive_info->closed_subpath=MagickFalse;
2412  primitive_info->point=point;
2413  return(MagickTrue);
2414 }
2415 
2417  const DrawInfo *draw_info,const size_t depth,ExceptionInfo *exception)
2418 {
2419 #define RenderImageTag "Render/Image"
2420 
2421  AffineMatrix
2422  affine,
2423  current;
2424 
2425  char
2426  keyword[MagickPathExtent],
2427  geometry[MagickPathExtent],
2428  *next_token,
2429  pattern[MagickPathExtent],
2430  *primitive,
2431  *token;
2432 
2433  const char
2434  *q;
2435 
2436  double
2437  angle,
2438  coordinates,
2439  cursor,
2440  factor,
2441  primitive_extent;
2442 
2443  DrawInfo
2444  *clone_info,
2445  **graphic_context;
2446 
2448  proceed;
2449 
2451  status;
2452 
2453  MVGInfo
2454  mvg_info;
2455 
2456  PointInfo
2457  point;
2458 
2460  *primitive_info;
2461 
2463  primitive_type;
2464 
2465  register const char
2466  *p;
2467 
2468  register ssize_t
2469  i,
2470  x;
2471 
2472  SegmentInfo
2473  bounds;
2474 
2475  size_t
2476  extent,
2477  number_points,
2478  number_stops;
2479 
2481  *macros;
2482 
2483  ssize_t
2484  defsDepth,
2485  j,
2486  k,
2487  n,
2488  symbolDepth;
2489 
2490  StopInfo
2491  *stops;
2492 
2493  TypeMetric
2494  metrics;
2495 
2496  assert(image != (Image *) NULL);
2497  assert(image->signature == MagickCoreSignature);
2498  if (image->debug != MagickFalse)
2499  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2500  assert(draw_info != (DrawInfo *) NULL);
2501  assert(draw_info->signature == MagickCoreSignature);
2502  if (image->debug != MagickFalse)
2503  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2504  if (depth > MagickMaxRecursionDepth)
2505  ThrowBinaryException(DrawError,"VectorGraphicsNestedTooDeeply",
2506  image->filename);
2507  if ((draw_info->primitive == (char *) NULL) ||
2508  (*draw_info->primitive == '\0'))
2509  return(MagickFalse);
2510  if (image->debug != MagickFalse)
2511  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
2512  if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2513  return(MagickFalse);
2514  if (image->alpha_trait == UndefinedPixelTrait)
2515  {
2516  status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2517  if (status == MagickFalse)
2518  return(MagickFalse);
2519  }
2520  if ((*draw_info->primitive == '@') && (strlen(draw_info->primitive) > 1) &&
2521  (*(draw_info->primitive+1) != '-') && (depth == 0))
2522  primitive=FileToString(draw_info->primitive+1,~0UL,exception);
2523  else
2524  primitive=AcquireString(draw_info->primitive);
2525  if (primitive == (char *) NULL)
2526  return(MagickFalse);
2527  primitive_extent=(double) strlen(primitive);
2528  (void) SetImageArtifact(image,"mvg:vector-graphics",primitive);
2529  n=0;
2530  number_stops=0;
2531  stops=(StopInfo *) NULL;
2532  /*
2533  Allocate primitive info memory.
2534  */
2535  graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2536  if (graphic_context == (DrawInfo **) NULL)
2537  {
2538  primitive=DestroyString(primitive);
2539  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2540  image->filename);
2541  }
2542  number_points=PrimitiveExtentPad;
2543  primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
2544  sizeof(*primitive_info));
2545  if (primitive_info == (PrimitiveInfo *) NULL)
2546  {
2547  primitive=DestroyString(primitive);
2548  for ( ; n >= 0; n--)
2549  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2550  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2551  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2552  image->filename);
2553  }
2554  (void) memset(primitive_info,0,(size_t) number_points*
2555  sizeof(*primitive_info));
2556  (void) memset(&mvg_info,0,sizeof(mvg_info));
2557  mvg_info.primitive_info=(&primitive_info);
2558  mvg_info.extent=(&number_points);
2559  mvg_info.exception=exception;
2560  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2561  graphic_context[n]->viewbox=image->page;
2562  if ((image->page.width == 0) || (image->page.height == 0))
2563  {
2564  graphic_context[n]->viewbox.width=image->columns;
2565  graphic_context[n]->viewbox.height=image->rows;
2566  }
2567  token=AcquireString(primitive);
2568  extent=strlen(token)+MagickPathExtent;
2569  defsDepth=0;
2570  symbolDepth=0;
2571  cursor=0.0;
2572  macros=GetMVGMacros(primitive);
2573  status=MagickTrue;
2574  for (q=primitive; *q != '\0'; )
2575  {
2576  /*
2577  Interpret graphic primitive.
2578  */
2579  if (GetNextToken(q,&q,MagickPathExtent,keyword) < 1)
2580  break;
2581  if (*keyword == '\0')
2582  break;
2583  if (*keyword == '#')
2584  {
2585  /*
2586  Comment.
2587  */
2588  while ((*q != '\n') && (*q != '\0'))
2589  q++;
2590  continue;
2591  }
2592  p=q-strlen(keyword)-1;
2593  primitive_type=UndefinedPrimitive;
2594  current=graphic_context[n]->affine;
2595  GetAffineMatrix(&affine);
2596  *token='\0';
2597  switch (*keyword)
2598  {
2599  case ';':
2600  break;
2601  case 'a':
2602  case 'A':
2603  {
2604  if (LocaleCompare("affine",keyword) == 0)
2605  {
2606  (void) GetNextToken(q,&q,extent,token);
2607  affine.sx=StringToDouble(token,&next_token);
2608  if (token == next_token)
2609  ThrowPointExpectedException(token,exception);
2610  (void) GetNextToken(q,&q,extent,token);
2611  if (*token == ',')
2612  (void) GetNextToken(q,&q,extent,token);
2613  affine.rx=StringToDouble(token,&next_token);
2614  if (token == next_token)
2615  ThrowPointExpectedException(token,exception);
2616  (void) GetNextToken(q,&q,extent,token);
2617  if (*token == ',')
2618  (void) GetNextToken(q,&q,extent,token);
2619  affine.ry=StringToDouble(token,&next_token);
2620  if (token == next_token)
2621  ThrowPointExpectedException(token,exception);
2622  (void) GetNextToken(q,&q,extent,token);
2623  if (*token == ',')
2624  (void) GetNextToken(q,&q,extent,token);
2625  affine.sy=StringToDouble(token,&next_token);
2626  if (token == next_token)
2627  ThrowPointExpectedException(token,exception);
2628  (void) GetNextToken(q,&q,extent,token);
2629  if (*token == ',')
2630  (void) GetNextToken(q,&q,extent,token);
2631  affine.tx=StringToDouble(token,&next_token);
2632  if (token == next_token)
2633  ThrowPointExpectedException(token,exception);
2634  (void) GetNextToken(q,&q,extent,token);
2635  if (*token == ',')
2636  (void) GetNextToken(q,&q,extent,token);
2637  affine.ty=StringToDouble(token,&next_token);
2638  if (token == next_token)
2639  ThrowPointExpectedException(token,exception);
2640  break;
2641  }
2642  if (LocaleCompare("alpha",keyword) == 0)
2643  {
2644  primitive_type=AlphaPrimitive;
2645  break;
2646  }
2647  if (LocaleCompare("arc",keyword) == 0)
2648  {
2649  primitive_type=ArcPrimitive;
2650  break;
2651  }
2652  status=MagickFalse;
2653  break;
2654  }
2655  case 'b':
2656  case 'B':
2657  {
2658  if (LocaleCompare("bezier",keyword) == 0)
2659  {
2660  primitive_type=BezierPrimitive;
2661  break;
2662  }
2663  if (LocaleCompare("border-color",keyword) == 0)
2664  {
2665  (void) GetNextToken(q,&q,extent,token);
2666  status&=QueryColorCompliance(token,AllCompliance,
2667  &graphic_context[n]->border_color,exception);
2668  break;
2669  }
2670  status=MagickFalse;
2671  break;
2672  }
2673  case 'c':
2674  case 'C':
2675  {
2676  if (LocaleCompare("class",keyword) == 0)
2677  {
2678  const char
2679  *mvg_class;
2680 
2681  (void) GetNextToken(q,&q,extent,token);
2682  if (*token == '\0')
2683  {
2684  status=MagickFalse;
2685  break;
2686  }
2687  if (LocaleCompare(token,graphic_context[n]->id) == 0)
2688  break;
2689  mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2690  if (mvg_class != (const char *) NULL)
2691  {
2692  char
2693  *elements;
2694 
2695  ssize_t
2696  offset;
2697 
2698  /*
2699  Inject class elements in stream.
2700  */
2701  offset=(ssize_t) (p-primitive);
2702  elements=AcquireString(primitive);
2703  elements[offset]='\0';
2704  (void) ConcatenateString(&elements,mvg_class);
2705  (void) ConcatenateString(&elements,"\n");
2706  (void) ConcatenateString(&elements,q);
2707  primitive=DestroyString(primitive);
2708  primitive=elements;
2709  q=primitive+offset;
2710  }
2711  break;
2712  }
2713  if (LocaleCompare("clip-path",keyword) == 0)
2714  {
2715  const char
2716  *clip_path;
2717 
2718  /*
2719  Take a node from within the MVG document, and duplicate it here.
2720  */
2721  (void) GetNextToken(q,&q,extent,token);
2722  if (*token == '\0')
2723  {
2724  status=MagickFalse;
2725  break;
2726  }
2727  (void) CloneString(&graphic_context[n]->clip_mask,token);
2728  clip_path=(const char *) GetValueFromSplayTree(macros,token);
2729  if (clip_path != (const char *) NULL)
2730  {
2731  if (graphic_context[n]->clipping_mask != (Image *) NULL)
2732  graphic_context[n]->clipping_mask=
2733  DestroyImage(graphic_context[n]->clipping_mask);
2734  graphic_context[n]->clipping_mask=DrawClippingMask(image,
2735  graphic_context[n],token,clip_path,exception);
2736  if (graphic_context[n]->compliance != SVGCompliance)
2737  {
2738  clip_path=(const char *) GetValueFromSplayTree(macros,
2739  graphic_context[n]->clip_mask);
2740  if (clip_path != (const char *) NULL)
2741  (void) SetImageArtifact(image,
2742  graphic_context[n]->clip_mask,clip_path);
2743  status&=DrawClipPath(image,graphic_context[n],
2744  graphic_context[n]->clip_mask,exception);
2745  }
2746  }
2747  break;
2748  }
2749  if (LocaleCompare("clip-rule",keyword) == 0)
2750  {
2751  ssize_t
2752  fill_rule;
2753 
2754  (void) GetNextToken(q,&q,extent,token);
2756  token);
2757  if (fill_rule == -1)
2758  {
2759  status=MagickFalse;
2760  break;
2761  }
2762  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2763  break;
2764  }
2765  if (LocaleCompare("clip-units",keyword) == 0)
2766  {
2767  ssize_t
2768  clip_units;
2769 
2770  (void) GetNextToken(q,&q,extent,token);
2772  token);
2773  if (clip_units == -1)
2774  {
2775  status=MagickFalse;
2776  break;
2777  }
2778  graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2779  if (clip_units == ObjectBoundingBox)
2780  {
2781  GetAffineMatrix(&current);
2782  affine.sx=draw_info->bounds.x2;
2783  affine.sy=draw_info->bounds.y2;
2784  affine.tx=draw_info->bounds.x1;
2785  affine.ty=draw_info->bounds.y1;
2786  break;
2787  }
2788  break;
2789  }
2790  if (LocaleCompare("circle",keyword) == 0)
2791  {
2792  primitive_type=CirclePrimitive;
2793  break;
2794  }
2795  if (LocaleCompare("color",keyword) == 0)
2796  {
2797  primitive_type=ColorPrimitive;
2798  break;
2799  }
2800  if (LocaleCompare("compliance",keyword) == 0)
2801  {
2802  /*
2803  MVG compliance associates a clipping mask with an image; SVG
2804  compliance associates a clipping mask with a graphics context.
2805  */
2806  (void) GetNextToken(q,&q,extent,token);
2807  graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2809  break;
2810  }
2811  status=MagickFalse;
2812  break;
2813  }
2814  case 'd':
2815  case 'D':
2816  {
2817  if (LocaleCompare("decorate",keyword) == 0)
2818  {
2819  ssize_t
2820  decorate;
2821 
2822  (void) GetNextToken(q,&q,extent,token);
2824  token);
2825  if (decorate == -1)
2826  {
2827  status=MagickFalse;
2828  break;
2829  }
2830  graphic_context[n]->decorate=(DecorationType) decorate;
2831  break;
2832  }
2833  if (LocaleCompare("density",keyword) == 0)
2834  {
2835  (void) GetNextToken(q,&q,extent,token);
2836  (void) CloneString(&graphic_context[n]->density,token);
2837  break;
2838  }
2839  if (LocaleCompare("direction",keyword) == 0)
2840  {
2841  ssize_t
2842  direction;
2843 
2844  (void) GetNextToken(q,&q,extent,token);
2846  token);
2847  if (direction == -1)
2848  status=MagickFalse;
2849  else
2850  graphic_context[n]->direction=(DirectionType) direction;
2851  break;
2852  }
2853  status=MagickFalse;
2854  break;
2855  }
2856  case 'e':
2857  case 'E':
2858  {
2859  if (LocaleCompare("ellipse",keyword) == 0)
2860  {
2861  primitive_type=EllipsePrimitive;
2862  break;
2863  }
2864  if (LocaleCompare("encoding",keyword) == 0)
2865  {
2866  (void) GetNextToken(q,&q,extent,token);
2867  (void) CloneString(&graphic_context[n]->encoding,token);
2868  break;
2869  }
2870  status=MagickFalse;
2871  break;
2872  }
2873  case 'f':
2874  case 'F':
2875  {
2876  if (LocaleCompare("fill",keyword) == 0)
2877  {
2878  (void) GetNextToken(q,&q,extent,token);
2879  if (graphic_context[n]->clip_path != MagickFalse)
2880  break;
2881  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2882  if (GetImageArtifact(image,pattern) != (const char *) NULL)
2883  (void) DrawPatternPath(image,draw_info,token,
2884  &graphic_context[n]->fill_pattern,exception);
2885  else
2886  {
2887  status&=QueryColorCompliance(token,AllCompliance,
2888  &graphic_context[n]->fill,exception);
2889  if (graphic_context[n]->fill_alpha != OpaqueAlpha)
2890  graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2891  }
2892  break;
2893  }
2894  if (LocaleCompare("fill-opacity",keyword) == 0)
2895  {
2896  double
2897  opacity;
2898 
2899  (void) GetNextToken(q,&q,extent,token);
2900  if (graphic_context[n]->clip_path != MagickFalse)
2901  break;
2902  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2903  opacity=MagickMin(MagickMax(factor*
2904  StringToDouble(token,&next_token),0.0),1.0);
2905  if (token == next_token)
2906  ThrowPointExpectedException(token,exception);
2907  graphic_context[n]->fill_alpha*=opacity;
2908  if (graphic_context[n]->fill.alpha != TransparentAlpha)
2909  graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2910  else
2911  graphic_context[n]->fill.alpha=(MagickRealType)
2912  ClampToQuantum(QuantumRange*(1.0-opacity));
2913  break;
2914  }
2915  if (LocaleCompare("fill-rule",keyword) == 0)
2916  {
2917  ssize_t
2918  fill_rule;
2919 
2920  (void) GetNextToken(q,&q,extent,token);
2922  token);
2923  if (fill_rule == -1)
2924  {
2925  status=MagickFalse;
2926  break;
2927  }
2928  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2929  break;
2930  }
2931  if (LocaleCompare("font",keyword) == 0)
2932  {
2933  (void) GetNextToken(q,&q,extent,token);
2934  (void) CloneString(&graphic_context[n]->font,token);
2935  if (LocaleCompare("none",token) == 0)
2936  graphic_context[n]->font=(char *) RelinquishMagickMemory(
2937  graphic_context[n]->font);
2938  break;
2939  }
2940  if (LocaleCompare("font-family",keyword) == 0)
2941  {
2942  (void) GetNextToken(q,&q,extent,token);
2943  (void) CloneString(&graphic_context[n]->family,token);
2944  break;
2945  }
2946  if (LocaleCompare("font-size",keyword) == 0)
2947  {
2948  (void) GetNextToken(q,&q,extent,token);
2949  graphic_context[n]->pointsize=StringToDouble(token,&next_token);
2950  if (token == next_token)
2951  ThrowPointExpectedException(token,exception);
2952  break;
2953  }
2954  if (LocaleCompare("font-stretch",keyword) == 0)
2955  {
2956  ssize_t
2957  stretch;
2958 
2959  (void) GetNextToken(q,&q,extent,token);
2961  if (stretch == -1)
2962  {
2963  status=MagickFalse;
2964  break;
2965  }
2966  graphic_context[n]->stretch=(StretchType) stretch;
2967  break;
2968  }
2969  if (LocaleCompare("font-style",keyword) == 0)
2970  {
2971  ssize_t
2972  style;
2973 
2974  (void) GetNextToken(q,&q,extent,token);
2976  if (style == -1)
2977  {
2978  status=MagickFalse;
2979  break;
2980  }
2981  graphic_context[n]->style=(StyleType) style;
2982  break;
2983  }
2984  if (LocaleCompare("font-weight",keyword) == 0)
2985  {
2986  ssize_t
2987  weight;
2988 
2989  (void) GetNextToken(q,&q,extent,token);
2991  if (weight == -1)
2992  weight=(ssize_t) StringToUnsignedLong(token);
2993  graphic_context[n]->weight=(size_t) weight;
2994  break;
2995  }
2996  status=MagickFalse;
2997  break;
2998  }
2999  case 'g':
3000  case 'G':
3001  {
3002  if (LocaleCompare("gradient-units",keyword) == 0)
3003  {
3004  (void) GetNextToken(q,&q,extent,token);
3005  break;
3006  }
3007  if (LocaleCompare("gravity",keyword) == 0)
3008  {
3009  ssize_t
3010  gravity;
3011 
3012  (void) GetNextToken(q,&q,extent,token);
3014  if (gravity == -1)
3015  {
3016  status=MagickFalse;
3017  break;
3018  }
3019  graphic_context[n]->gravity=(GravityType) gravity;
3020  break;
3021  }
3022  status=MagickFalse;
3023  break;
3024  }
3025  case 'i':
3026  case 'I':
3027  {
3028  if (LocaleCompare("image",keyword) == 0)
3029  {
3030  ssize_t
3031  compose;
3032 
3033  primitive_type=ImagePrimitive;
3034  (void) GetNextToken(q,&q,extent,token);
3036  if (compose == -1)
3037  {
3038  status=MagickFalse;
3039  break;
3040  }
3041  graphic_context[n]->compose=(CompositeOperator) compose;
3042  break;
3043  }
3044  if (LocaleCompare("interline-spacing",keyword) == 0)
3045  {
3046  (void) GetNextToken(q,&q,extent,token);
3047  graphic_context[n]->interline_spacing=StringToDouble(token,
3048  &next_token);
3049  if (token == next_token)
3050  ThrowPointExpectedException(token,exception);
3051  break;
3052  }
3053  if (LocaleCompare("interword-spacing",keyword) == 0)
3054  {
3055  (void) GetNextToken(q,&q,extent,token);
3056  graphic_context[n]->interword_spacing=StringToDouble(token,
3057  &next_token);
3058  if (token == next_token)
3059  ThrowPointExpectedException(token,exception);
3060  break;
3061  }
3062  status=MagickFalse;
3063  break;
3064  }
3065  case 'k':
3066  case 'K':
3067  {
3068  if (LocaleCompare("kerning",keyword) == 0)
3069  {
3070  (void) GetNextToken(q,&q,extent,token);
3071  graphic_context[n]->kerning=StringToDouble(token,&next_token);
3072  if (token == next_token)
3073  ThrowPointExpectedException(token,exception);
3074  break;
3075  }
3076  status=MagickFalse;
3077  break;
3078  }
3079  case 'l':
3080  case 'L':
3081  {
3082  if (LocaleCompare("letter-spacing",keyword) == 0)
3083  {
3084  (void) GetNextToken(q,&q,extent,token);
3085  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3086  clone_info->text=AcquireString(" ");
3087  status&=GetTypeMetrics(image,clone_info,&metrics,exception);
3088  graphic_context[n]->kerning=metrics.width*
3089  StringToDouble(token,&next_token);
3090  clone_info=DestroyDrawInfo(clone_info);
3091  if (token == next_token)
3092  ThrowPointExpectedException(token,exception);
3093  break;
3094  }
3095  if (LocaleCompare("line",keyword) == 0)
3096  {
3097  primitive_type=LinePrimitive;
3098  break;
3099  }
3100  status=MagickFalse;
3101  break;
3102  }
3103  case 'm':
3104  case 'M':
3105  {
3106  if (LocaleCompare("mask",keyword) == 0)
3107  {
3108  const char
3109  *mask_path;
3110 
3111  /*
3112  Take a node from within the MVG document, and duplicate it here.
3113  */
3114  (void) GetNextToken(q,&q,extent,token);
3115  mask_path=(const char *) GetValueFromSplayTree(macros,token);
3116  if (mask_path != (const char *) NULL)
3117  {
3118  if (graphic_context[n]->composite_mask != (Image *) NULL)
3119  graphic_context[n]->composite_mask=
3120  DestroyImage(graphic_context[n]->composite_mask);
3121  graphic_context[n]->composite_mask=DrawCompositeMask(image,
3122  graphic_context[n],token,mask_path,exception);
3123  if (graphic_context[n]->compliance != SVGCompliance)
3124  status=SetImageMask(image,CompositePixelMask,
3125  graphic_context[n]->composite_mask,exception);
3126  }
3127  break;
3128  }
3129  break;
3130  }
3131  case 'o':
3132  case 'O':
3133  {
3134  if (LocaleCompare("offset",keyword) == 0)
3135  {
3136  (void) GetNextToken(q,&q,extent,token);
3137  break;
3138  }
3139  if (LocaleCompare("opacity",keyword) == 0)
3140  {
3141  double
3142  opacity;
3143 
3144  (void) GetNextToken(q,&q,extent,token);
3145  if (graphic_context[n]->clip_path != MagickFalse)
3146  break;
3147  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3148  opacity=MagickMin(MagickMax(factor*
3149  StringToDouble(token,&next_token),0.0),1.0);
3150  if (token == next_token)
3151  ThrowPointExpectedException(token,exception);
3152  graphic_context[n]->fill_alpha*=opacity;
3153  graphic_context[n]->stroke_alpha*=opacity;
3154  break;
3155  }
3156  status=MagickFalse;
3157  break;
3158  }
3159  case 'p':
3160  case 'P':
3161  {
3162  if (LocaleCompare("path",keyword) == 0)
3163  {
3164  primitive_type=PathPrimitive;
3165  break;
3166  }
3167  if (LocaleCompare("point",keyword) == 0)
3168  {
3169  primitive_type=PointPrimitive;
3170  break;
3171  }
3172  if (LocaleCompare("polyline",keyword) == 0)
3173  {
3174  primitive_type=PolylinePrimitive;
3175  break;
3176  }
3177  if (LocaleCompare("polygon",keyword) == 0)
3178  {
3179  primitive_type=PolygonPrimitive;
3180  break;
3181  }
3182  if (LocaleCompare("pop",keyword) == 0)
3183  {
3184  if (GetNextToken(q,&q,extent,token) < 1)
3185  break;
3186  if (LocaleCompare("class",token) == 0)
3187  break;
3188  if (LocaleCompare("clip-path",token) == 0)
3189  break;
3190  if (LocaleCompare("defs",token) == 0)
3191  {
3192  defsDepth--;
3193  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3194  MagickTrue;
3195  break;
3196  }
3197  if (LocaleCompare("gradient",token) == 0)
3198  break;
3199  if (LocaleCompare("graphic-context",token) == 0)
3200  {
3201  if (n <= 0)
3202  {
3203  (void) ThrowMagickException(exception,GetMagickModule(),
3204  DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
3205  status=MagickFalse;
3206  n=0;
3207  break;
3208  }
3209  if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3210  (graphic_context[n]->compliance != SVGCompliance))
3211  if (LocaleCompare(graphic_context[n]->clip_mask,
3212  graphic_context[n-1]->clip_mask) != 0)
3213  status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3214  exception);
3215  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3216  n--;
3217  break;
3218  }
3219  if (LocaleCompare("mask",token) == 0)
3220  break;
3221  if (LocaleCompare("pattern",token) == 0)
3222  break;
3223  if (LocaleCompare("symbol",token) == 0)
3224  {
3225  symbolDepth--;
3226  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3227  MagickTrue;
3228  break;
3229  }
3230  status=MagickFalse;
3231  break;
3232  }
3233  if (LocaleCompare("push",keyword) == 0)
3234  {
3235  if (GetNextToken(q,&q,extent,token) < 1)
3236  break;
3237  if (LocaleCompare("class",token) == 0)
3238  {
3239  /*
3240  Class context.
3241  */
3242  for (p=q; *q != '\0'; )
3243  {
3244  if (GetNextToken(q,&q,extent,token) < 1)
3245  break;
3246  if (LocaleCompare(token,"pop") != 0)
3247  continue;
3248  (void) GetNextToken(q,(const char **) NULL,extent,token);
3249  if (LocaleCompare(token,"class") != 0)
3250  continue;
3251  break;
3252  }
3253  (void) GetNextToken(q,&q,extent,token);
3254  break;
3255  }
3256  if (LocaleCompare("clip-path",token) == 0)
3257  {
3258  (void) GetNextToken(q,&q,extent,token);
3259  for (p=q; *q != '\0'; )
3260  {
3261  if (GetNextToken(q,&q,extent,token) < 1)
3262  break;
3263  if (LocaleCompare(token,"pop") != 0)
3264  continue;
3265  (void) GetNextToken(q,(const char **) NULL,extent,token);
3266  if (LocaleCompare(token,"clip-path") != 0)
3267  continue;
3268  break;
3269  }
3270  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3271  {
3272  status=MagickFalse;
3273  break;
3274  }
3275  (void) GetNextToken(q,&q,extent,token);
3276  break;
3277  }
3278  if (LocaleCompare("defs",token) == 0)
3279  {
3280  defsDepth++;
3281  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3282  MagickTrue;
3283  break;
3284  }
3285  if (LocaleCompare("gradient",token) == 0)
3286  {
3287  char
3288  key[2*MagickPathExtent],
3289  name[MagickPathExtent],
3290  type[MagickPathExtent];
3291 
3292  SegmentInfo
3293  segment;
3294 
3295  (void) GetNextToken(q,&q,extent,token);
3296  (void) CopyMagickString(name,token,MagickPathExtent);
3297  (void) GetNextToken(q,&q,extent,token);
3298  (void) CopyMagickString(type,token,MagickPathExtent);
3299  (void) GetNextToken(q,&q,extent,token);
3300  segment.x1=StringToDouble(token,&next_token);
3301  if (token == next_token)
3302  ThrowPointExpectedException(token,exception);
3303  (void) GetNextToken(q,&q,extent,token);
3304  if (*token == ',')
3305  (void) GetNextToken(q,&q,extent,token);
3306  segment.y1=StringToDouble(token,&next_token);
3307  if (token == next_token)
3308  ThrowPointExpectedException(token,exception);
3309  (void) GetNextToken(q,&q,extent,token);
3310  if (*token == ',')
3311  (void) GetNextToken(q,&q,extent,token);
3312  segment.x2=StringToDouble(token,&next_token);
3313  if (token == next_token)
3314  ThrowPointExpectedException(token,exception);
3315  (void) GetNextToken(q,&q,extent,token);
3316  if (*token == ',')
3317  (void) GetNextToken(q,&q,extent,token);
3318  segment.y2=StringToDouble(token,&next_token);
3319  if (token == next_token)
3320  ThrowPointExpectedException(token,exception);
3321  if (LocaleCompare(type,"radial") == 0)
3322  {
3323  (void) GetNextToken(q,&q,extent,token);
3324  if (*token == ',')
3325  (void) GetNextToken(q,&q,extent,token);
3326  }
3327  for (p=q; *q != '\0'; )
3328  {
3329  if (GetNextToken(q,&q,extent,token) < 1)
3330  break;
3331  if (LocaleCompare(token,"pop") != 0)
3332  continue;
3333  (void) GetNextToken(q,(const char **) NULL,extent,token);
3334  if (LocaleCompare(token,"gradient") != 0)
3335  continue;
3336  break;
3337  }
3338  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3339  {
3340  status=MagickFalse;
3341  break;
3342  }
3343  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3344  bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3345  graphic_context[n]->affine.ry*segment.y1+
3346  graphic_context[n]->affine.tx;
3347  bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3348  graphic_context[n]->affine.sy*segment.y1+
3349  graphic_context[n]->affine.ty;
3350  bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3351  graphic_context[n]->affine.ry*segment.y2+
3352  graphic_context[n]->affine.tx;
3353  bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3354  graphic_context[n]->affine.sy*segment.y2+
3355  graphic_context[n]->affine.ty;
3356  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3357  (void) SetImageArtifact(image,key,token);
3358  (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
3359  (void) SetImageArtifact(image,key,type);
3360  (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3361  name);
3362  (void) FormatLocaleString(geometry,MagickPathExtent,
3363  "%gx%g%+.15g%+.15g",
3364  MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3365  MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3366  bounds.x1,bounds.y1);
3367  (void) SetImageArtifact(image,key,geometry);
3368  (void) GetNextToken(q,&q,extent,token);
3369  break;
3370  }
3371  if (LocaleCompare("graphic-context",token) == 0)
3372  {
3373  n++;
3374  graphic_context=(DrawInfo **) ResizeQuantumMemory(
3375  graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3376  if (graphic_context == (DrawInfo **) NULL)
3377  {
3378  (void) ThrowMagickException(exception,GetMagickModule(),
3379  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3380  image->filename);
3381  break;
3382  }
3383  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3384  graphic_context[n-1]);
3385  if (*q == '"')
3386  {
3387  (void) GetNextToken(q,&q,extent,token);
3388  (void) CloneString(&graphic_context[n]->id,token);
3389  }
3390  break;
3391  }
3392  if (LocaleCompare("mask",token) == 0)
3393  {
3394  (void) GetNextToken(q,&q,extent,token);
3395  break;
3396  }
3397  if (LocaleCompare("pattern",token) == 0)
3398  {
3399  char
3400  key[2*MagickPathExtent],
3401  name[MagickPathExtent];
3402 
3404  bounds;
3405 
3406  (void) GetNextToken(q,&q,extent,token);
3407  (void) CopyMagickString(name,token,MagickPathExtent);
3408  (void) GetNextToken(q,&q,extent,token);
3409  bounds.x=(ssize_t) ceil(StringToDouble(token,&next_token)-0.5);
3410  if (token == next_token)
3411  ThrowPointExpectedException(token,exception);
3412  (void) GetNextToken(q,&q,extent,token);
3413  if (*token == ',')
3414  (void) GetNextToken(q,&q,extent,token);
3415  bounds.y=(ssize_t) ceil(StringToDouble(token,&next_token)-0.5);
3416  if (token == next_token)
3417  ThrowPointExpectedException(token,exception);
3418  (void) GetNextToken(q,&q,extent,token);
3419  if (*token == ',')
3420  (void) GetNextToken(q,&q,extent,token);
3421  bounds.width=(size_t) floor(StringToDouble(token,&next_token)+
3422  0.5);
3423  if (token == next_token)
3424  ThrowPointExpectedException(token,exception);
3425  (void) GetNextToken(q,&q,extent,token);
3426  if (*token == ',')
3427  (void) GetNextToken(q,&q,extent,token);
3428  bounds.height=(size_t) floor(StringToDouble(token,&next_token)+
3429  0.5);
3430  if (token == next_token)
3431  ThrowPointExpectedException(token,exception);
3432  for (p=q; *q != '\0'; )
3433  {
3434  if (GetNextToken(q,&q,extent,token) < 1)
3435  break;
3436  if (LocaleCompare(token,"pop") != 0)
3437  continue;
3438  (void) GetNextToken(q,(const char **) NULL,extent,token);
3439  if (LocaleCompare(token,"pattern") != 0)
3440  continue;
3441  break;
3442  }
3443  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3444  {
3445  status=MagickFalse;
3446  break;
3447  }
3448  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3449  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3450  (void) SetImageArtifact(image,key,token);
3451  (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3452  name);
3453  (void) FormatLocaleString(geometry,MagickPathExtent,
3454  "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
3455  bounds.height,(double) bounds.x,(double) bounds.y);
3456  (void) SetImageArtifact(image,key,geometry);
3457  (void) GetNextToken(q,&q,extent,token);
3458  break;
3459  }
3460  if (LocaleCompare("symbol",token) == 0)
3461  {
3462  symbolDepth++;
3463  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3464  MagickTrue;
3465  break;
3466  }
3467  status=MagickFalse;
3468  break;
3469  }
3470  status=MagickFalse;
3471  break;
3472  }
3473  case 'r':
3474  case 'R':
3475  {
3476  if (LocaleCompare("rectangle",keyword) == 0)
3477  {
3478  primitive_type=RectanglePrimitive;
3479  break;
3480  }
3481  if (LocaleCompare("rotate",keyword) == 0)
3482  {
3483  (void) GetNextToken(q,&q,extent,token);
3484  angle=StringToDouble(token,&next_token);
3485  if (token == next_token)
3486  ThrowPointExpectedException(token,exception);
3487  affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3488  affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3489  affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3490  affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3491  break;
3492  }
3493  if (LocaleCompare("roundRectangle",keyword) == 0)
3494  {
3495  primitive_type=RoundRectanglePrimitive;
3496  break;
3497  }
3498  status=MagickFalse;
3499  break;
3500  }
3501  case 's':
3502  case 'S':
3503  {
3504  if (LocaleCompare("scale",keyword) == 0)
3505  {
3506  (void) GetNextToken(q,&q,extent,token);
3507  affine.sx=StringToDouble(token,&next_token);
3508  if (token == next_token)
3509  ThrowPointExpectedException(token,exception);
3510  (void) GetNextToken(q,&q,extent,token);
3511  if (*token == ',')
3512  (void) GetNextToken(q,&q,extent,token);
3513  affine.sy=StringToDouble(token,&next_token);
3514  if (token == next_token)
3515  ThrowPointExpectedException(token,exception);
3516  break;
3517  }
3518  if (LocaleCompare("skewX",keyword) == 0)
3519  {
3520  (void) GetNextToken(q,&q,extent,token);
3521  angle=StringToDouble(token,&next_token);
3522  if (token == next_token)
3523  ThrowPointExpectedException(token,exception);
3524  affine.ry=sin(DegreesToRadians(angle));
3525  break;
3526  }
3527  if (LocaleCompare("skewY",keyword) == 0)
3528  {
3529  (void) GetNextToken(q,&q,extent,token);
3530  angle=StringToDouble(token,&next_token);
3531  if (token == next_token)
3532  ThrowPointExpectedException(token,exception);
3533  affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3534  break;
3535  }
3536  if (LocaleCompare("stop-color",keyword) == 0)
3537  {
3538  PixelInfo
3539  stop_color;
3540 
3541  number_stops++;
3542  if (number_stops == 1)
3543  stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
3544  else
3545  if (number_stops > 2)
3546  stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3547  sizeof(*stops));
3548  if (stops == (StopInfo *) NULL)
3549  {
3550  (void) ThrowMagickException(exception,GetMagickModule(),
3551  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3552  image->filename);
3553  break;
3554  }
3555  (void) GetNextToken(q,&q,extent,token);
3556  status&=QueryColorCompliance(token,AllCompliance,&stop_color,
3557  exception);
3558  stops[number_stops-1].color=stop_color;
3559  (void) GetNextToken(q,&q,extent,token);
3560  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3561  stops[number_stops-1].offset=factor*StringToDouble(token,
3562  &next_token);
3563  if (token == next_token)
3564  ThrowPointExpectedException(token,exception);
3565  break;
3566  }
3567  if (LocaleCompare("stroke",keyword) == 0)
3568  {
3569  (void) GetNextToken(q,&q,extent,token);
3570  if (graphic_context[n]->clip_path != MagickFalse)
3571  break;
3572  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
3573  if (GetImageArtifact(image,pattern) != (const char *) NULL)
3574  (void) DrawPatternPath(image,draw_info,token,
3575  &graphic_context[n]->stroke_pattern,exception);
3576  else
3577  {
3578  status&=QueryColorCompliance(token,AllCompliance,
3579  &graphic_context[n]->stroke,exception);
3580  if (graphic_context[n]->stroke_alpha != OpaqueAlpha)
3581  graphic_context[n]->stroke.alpha=
3582  graphic_context[n]->stroke_alpha;
3583  }
3584  break;
3585  }
3586  if (LocaleCompare("stroke-antialias",keyword) == 0)
3587  {
3588  (void) GetNextToken(q,&q,extent,token);
3589  graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3591  break;
3592  }
3593  if (LocaleCompare("stroke-dasharray",keyword) == 0)
3594  {
3595  if (graphic_context[n]->dash_pattern != (double *) NULL)
3596  graphic_context[n]->dash_pattern=(double *)
3597  RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3598  if (IsPoint(q) != MagickFalse)
3599  {
3600  const char
3601  *r;
3602 
3603  r=q;
3604  (void) GetNextToken(r,&r,extent,token);
3605  if (*token == ',')
3606  (void) GetNextToken(r,&r,extent,token);
3607  for (x=0; IsPoint(token) != MagickFalse; x++)
3608  {
3609  (void) GetNextToken(r,&r,extent,token);
3610  if (*token == ',')
3611  (void) GetNextToken(r,&r,extent,token);
3612  }
3613  graphic_context[n]->dash_pattern=(double *)
3614  AcquireQuantumMemory((size_t) (2*x+2),
3615  sizeof(*graphic_context[n]->dash_pattern));
3616  if (graphic_context[n]->dash_pattern == (double *) NULL)
3617  {
3618  (void) ThrowMagickException(exception,GetMagickModule(),
3619  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3620  image->filename);
3621  status=MagickFalse;
3622  break;
3623  }
3624  (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3625  (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3626  for (j=0; j < x; j++)
3627  {
3628  (void) GetNextToken(q,&q,extent,token);
3629  if (*token == ',')
3630  (void) GetNextToken(q,&q,extent,token);
3631  graphic_context[n]->dash_pattern[j]=StringToDouble(token,
3632  &next_token);
3633  if (token == next_token)
3634  ThrowPointExpectedException(token,exception);
3635  if (graphic_context[n]->dash_pattern[j] < 0.0)
3636  status=MagickFalse;
3637  }
3638  if ((x & 0x01) != 0)
3639  for ( ; j < (2*x); j++)
3640  graphic_context[n]->dash_pattern[j]=
3641  graphic_context[n]->dash_pattern[j-x];
3642  graphic_context[n]->dash_pattern[j]=0.0;
3643  break;
3644  }
3645  (void) GetNextToken(q,&q,extent,token);
3646  break;
3647  }
3648  if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3649  {
3650  (void) GetNextToken(q,&q,extent,token);
3651  graphic_context[n]->dash_offset=StringToDouble(token,&next_token);
3652  if (token == next_token)
3653  ThrowPointExpectedException(token,exception);
3654  break;
3655  }
3656  if (LocaleCompare("stroke-linecap",keyword) == 0)
3657  {
3658  ssize_t
3659  linecap;
3660 
3661  (void) GetNextToken(q,&q,extent,token);
3663  if (linecap == -1)
3664  {
3665  status=MagickFalse;
3666  break;
3667  }
3668  graphic_context[n]->linecap=(LineCap) linecap;
3669  break;
3670  }
3671  if (LocaleCompare("stroke-linejoin",keyword) == 0)
3672  {
3673  ssize_t
3674  linejoin;
3675 
3676  (void) GetNextToken(q,&q,extent,token);
3678  token);
3679  if (linejoin == -1)
3680  {
3681  status=MagickFalse;
3682  break;
3683  }
3684  graphic_context[n]->linejoin=(LineJoin) linejoin;
3685  break;
3686  }
3687  if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3688  {
3689  (void) GetNextToken(q,&q,extent,token);
3690  graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3691  break;
3692  }
3693  if (LocaleCompare("stroke-opacity",keyword) == 0)
3694  {
3695  double
3696  opacity;
3697 
3698  (void) GetNextToken(q,&q,extent,token);
3699  if (graphic_context[n]->clip_path != MagickFalse)
3700  break;
3701  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3702  opacity=MagickMin(MagickMax(factor*
3703  StringToDouble(token,&next_token),0.0),1.0);
3704  if (token == next_token)
3705  ThrowPointExpectedException(token,exception);
3706  graphic_context[n]->stroke_alpha*=opacity;
3707  if (graphic_context[n]->stroke.alpha != TransparentAlpha)
3708  graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3709  else
3710  graphic_context[n]->stroke.alpha=(MagickRealType)
3711  ClampToQuantum(QuantumRange*(1.0-opacity));
3712  break;
3713  }
3714  if (LocaleCompare("stroke-width",keyword) == 0)
3715  {
3716  (void) GetNextToken(q,&q,extent,token);
3717  if (graphic_context[n]->clip_path != MagickFalse)
3718  break;
3719  graphic_context[n]->stroke_width=StringToDouble(token,&next_token);
3720  if (token == next_token)
3721  ThrowPointExpectedException(token,exception);
3722  break;
3723  }
3724  status=MagickFalse;
3725  break;
3726  }
3727  case 't':
3728  case 'T':
3729  {
3730  if (LocaleCompare("text",keyword) == 0)
3731  {
3732  primitive_type=TextPrimitive;
3733  break;
3734  }
3735  if (LocaleCompare("text-align",keyword) == 0)
3736  {
3737  ssize_t
3738  align;
3739 
3740  (void) GetNextToken(q,&q,extent,token);
3742  if (align == -1)
3743  {
3744  status=MagickFalse;
3745  break;
3746  }
3747  graphic_context[n]->align=(AlignType) align;
3748  break;
3749  }
3750  if (LocaleCompare("text-anchor",keyword) == 0)
3751  {
3752  ssize_t
3753  align;
3754 
3755  (void) GetNextToken(q,&q,extent,token);
3757  if (align == -1)
3758  {
3759  status=MagickFalse;
3760  break;
3761  }
3762  graphic_context[n]->align=(AlignType) align;
3763  break;
3764  }
3765  if (LocaleCompare("text-antialias",keyword) == 0)
3766  {
3767  (void) GetNextToken(q,&q,extent,token);
3768  graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3770  break;
3771  }
3772  if (LocaleCompare("text-undercolor",keyword) == 0)
3773  {
3774  (void) GetNextToken(q,&q,extent,token);
3775  status&=QueryColorCompliance(token,AllCompliance,
3776  &graphic_context[n]->undercolor,exception);
3777  break;
3778  }
3779  if (LocaleCompare("translate",keyword) == 0)
3780  {
3781  (void) GetNextToken(q,&q,extent,token);
3782  affine.tx=StringToDouble(token,&next_token);
3783  if (token == next_token)
3784  ThrowPointExpectedException(token,exception);
3785  (void) GetNextToken(q,&q,extent,token);
3786  if (*token == ',')
3787  (void) GetNextToken(q,&q,extent,token);
3788  affine.ty=StringToDouble(token,&next_token);
3789  if (token == next_token)
3790  ThrowPointExpectedException(token,exception);
3791  cursor=0.0;
3792  break;
3793  }
3794  status=MagickFalse;
3795  break;
3796  }
3797  case 'u':
3798  case 'U':
3799  {
3800  if (LocaleCompare("use",keyword) == 0)
3801  {
3802  const char
3803  *use;
3804 
3805  /*
3806  Get a macro from the MVG document, and "use" it here.
3807  */
3808  (void) GetNextToken(q,&q,extent,token);
3809  use=(const char *) GetValueFromSplayTree(macros,token);
3810  if (use != (const char *) NULL)
3811  {
3812  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3813  (void) CloneString(&clone_info->primitive,use);
3814  status=RenderMVGContent(image,clone_info,depth+1,exception);
3815  clone_info=DestroyDrawInfo(clone_info);
3816  }
3817  break;
3818  }
3819  break;
3820  }
3821  case 'v':
3822  case 'V':
3823  {
3824  if (LocaleCompare("viewbox",keyword) == 0)
3825  {
3826  (void) GetNextToken(q,&q,extent,token);
3827  graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
3828  &next_token)-0.5);
3829  if (token == next_token)
3830  ThrowPointExpectedException(token,exception);
3831  (void) GetNextToken(q,&q,extent,token);
3832  if (*token == ',')
3833  (void) GetNextToken(q,&q,extent,token);
3834  graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
3835  &next_token)-0.5);
3836  if (token == next_token)
3837  ThrowPointExpectedException(token,exception);
3838  (void) GetNextToken(q,&q,extent,token);
3839  if (*token == ',')
3840  (void) GetNextToken(q,&q,extent,token);
3841  graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
3842  token,&next_token)+0.5);
3843  if (token == next_token)
3844  ThrowPointExpectedException(token,exception);
3845  (void) GetNextToken(q,&q,extent,token);
3846  if (*token == ',')
3847  (void) GetNextToken(q,&q,extent,token);
3848  graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
3849  token,&next_token)+0.5);
3850  if (token == next_token)
3851  ThrowPointExpectedException(token,exception);
3852  break;
3853  }
3854  status=MagickFalse;
3855  break;
3856  }
3857  case 'w':
3858  case 'W':
3859  {
3860  if (LocaleCompare("word-spacing",keyword) == 0)
3861  {
3862  (void) GetNextToken(q,&q,extent,token);
3863  graphic_context[n]->interword_spacing=StringToDouble(token,
3864  &next_token);
3865  if (token == next_token)
3866  ThrowPointExpectedException(token,exception);
3867  break;
3868  }
3869  status=MagickFalse;
3870  break;
3871  }
3872  default:
3873  {
3874  status=MagickFalse;
3875  break;
3876  }
3877  }
3878  if (status == MagickFalse)
3879  break;
3880  if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
3881  (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
3882  (fabs(affine.sy-1.0) >= MagickEpsilon) ||
3883  (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
3884  {
3885  graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
3886  graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
3887  graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
3888  graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
3889  graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
3890  current.tx;
3891  graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
3892  current.ty;
3893  }
3894  if (primitive_type == UndefinedPrimitive)
3895  {
3896  if (*q == '\0')
3897  {
3898  if (number_stops > 1)
3899  {
3900  GradientType
3901  type;
3902 
3903  type=LinearGradient;
3904  if (draw_info->gradient.type == RadialGradient)
3905  type=RadialGradient;
3906  (void) GradientImage(image,type,PadSpread,stops,number_stops,
3907  exception);
3908  }
3909  if (number_stops > 0)
3910  stops=(StopInfo *) RelinquishMagickMemory(stops);
3911  }
3912  if ((image->debug != MagickFalse) && (q > p))
3913  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
3914  (q-p-1),p);
3915  continue;
3916  }
3917  /*
3918  Parse the primitive attributes.
3919  */
3920  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3921  if ((primitive_info[i].primitive == TextPrimitive) ||
3922  (primitive_info[i].primitive == ImagePrimitive))
3923  if (primitive_info[i].text != (char *) NULL)
3924  primitive_info[i].text=DestroyString(primitive_info[i].text);
3925  i=0;
3926  mvg_info.offset=i;
3927  j=0;
3928  primitive_info[0].point.x=0.0;
3929  primitive_info[0].point.y=0.0;
3930  primitive_info[0].coordinates=0;
3931  primitive_info[0].method=FloodfillMethod;
3932  primitive_info[0].closed_subpath=MagickFalse;
3933  for (x=0; *q != '\0'; x++)
3934  {
3935  /*
3936  Define points.
3937  */
3938  if (IsPoint(q) == MagickFalse)
3939  break;
3940  (void) GetNextToken(q,&q,extent,token);
3941  point.x=StringToDouble(token,&next_token);
3942  if (token == next_token)
3943  ThrowPointExpectedException(token,exception);
3944  (void) GetNextToken(q,&q,extent,token);
3945  if (*token == ',')
3946  (void) GetNextToken(q,&q,extent,token);
3947  point.y=StringToDouble(token,&next_token);
3948  if (token == next_token)
3949  ThrowPointExpectedException(token,exception);
3950  (void) GetNextToken(q,(const char **) NULL,extent,token);
3951  if (*token == ',')
3952  (void) GetNextToken(q,&q,extent,token);
3953  primitive_info[i].primitive=primitive_type;
3954  primitive_info[i].point=point;
3955  primitive_info[i].coordinates=0;
3956  primitive_info[i].method=FloodfillMethod;
3957  primitive_info[i].closed_subpath=MagickFalse;
3958  i++;
3959  mvg_info.offset=i;
3960  if (i < (ssize_t) number_points)
3961  continue;
3962  status&=CheckPrimitiveExtent(&mvg_info,number_points);
3963  }
3964  if (status == MagickFalse)
3965  break;
3966  if ((primitive_info[j].primitive == TextPrimitive) ||
3967  (primitive_info[j].primitive == ImagePrimitive))
3968  if (primitive_info[j].text != (char *) NULL)
3969  primitive_info[j].text=DestroyString(primitive_info[j].text);
3970  primitive_info[j].primitive=primitive_type;
3971  primitive_info[j].coordinates=(size_t) x;
3972  primitive_info[j].method=FloodfillMethod;
3973  primitive_info[j].closed_subpath=MagickFalse;
3974  /*
3975  Circumscribe primitive within a circle.
3976  */
3977  bounds.x1=primitive_info[j].point.x;
3978  bounds.y1=primitive_info[j].point.y;
3979  bounds.x2=primitive_info[j].point.x;
3980  bounds.y2=primitive_info[j].point.y;
3981  for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
3982  {
3983  point=primitive_info[j+k].point;
3984  if (point.x < bounds.x1)
3985  bounds.x1=point.x;
3986  if (point.y < bounds.y1)
3987  bounds.y1=point.y;
3988  if (point.x > bounds.x2)
3989  bounds.x2=point.x;
3990  if (point.y > bounds.y2)
3991  bounds.y2=point.y;
3992  }
3993  /*
3994  Speculate how many points our primitive might consume.
3995  */
3996  coordinates=(double) primitive_info[j].coordinates;
3997  switch (primitive_type)
3998  {
3999  case RectanglePrimitive:
4000  {
4001  coordinates*=5.0;
4002  break;
4003  }
4005  {
4006  double
4007  alpha,
4008  beta,
4009  radius;
4010 
4011  alpha=bounds.x2-bounds.x1;
4012  beta=bounds.y2-bounds.y1;
4013  radius=hypot((double) alpha,(double) beta);
4014  coordinates*=5.0;
4015  coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4016  BezierQuantum+360.0;
4017  break;
4018  }
4019  case BezierPrimitive:
4020  {
4021  coordinates=(double) (BezierQuantum*primitive_info[j].coordinates);
4022  if (primitive_info[j].coordinates > (107*BezierQuantum))
4023  {
4024  (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
4025  "TooManyBezierCoordinates","`%s'",token);
4026  status=MagickFalse;
4027  break;
4028  }
4029  break;
4030  }
4031  case PathPrimitive:
4032  {
4033  char
4034  *s,
4035  *t;
4036 
4037  (void) GetNextToken(q,&q,extent,token);
4038  coordinates=1.0;
4039  t=token;
4040  for (s=token; *s != '\0'; s=t)
4041  {
4042  double
4043  value;
4044 
4045  value=StringToDouble(s,&t);
4046  (void) value;
4047  if (s == t)
4048  {
4049  t++;
4050  continue;
4051  }
4052  coordinates++;
4053  }
4054  for (s=token; *s != '\0'; s++)
4055  if (strspn(s,"AaCcQqSsTt") != 0)
4056  coordinates+=(20.0*BezierQuantum)+360.0;
4057  break;
4058  }
4059  case CirclePrimitive:
4060  case ArcPrimitive:
4061  case EllipsePrimitive:
4062  {
4063  double
4064  alpha,
4065  beta,
4066  radius;
4067 
4068  alpha=bounds.x2-bounds.x1;
4069  beta=bounds.y2-bounds.y1;
4070  radius=hypot(alpha,beta);
4071  coordinates=2.0*(ceil(MagickPI*radius))+6.0*BezierQuantum+360.0;
4072  break;
4073  }
4074  default:
4075  break;
4076  }
4077  if (coordinates > MaxBezierCoordinates)
4078  {
4079  (void) ThrowMagickException(exception,GetMagickModule(),
4080  ResourceLimitError,"TooManyBezierCoordinates","`%s'",token);
4081  status=MagickFalse;
4082  }
4083  if (status == MagickFalse)
4084  break;
4085  if (((size_t) (i+coordinates)) >= number_points)
4086  {
4087  /*
4088  Resize based on speculative points required by primitive.
4089  */
4090  number_points+=coordinates+1;
4091  if (number_points < (size_t) coordinates)
4092  {
4093  (void) ThrowMagickException(exception,GetMagickModule(),
4094  ResourceLimitError,"MemoryAllocationFailed","`%s'",
4095  image->filename);
4096  break;
4097  }
4098  mvg_info.offset=i;
4099  status&=CheckPrimitiveExtent(&mvg_info,number_points);
4100  }
4101  status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4102  if (status == MagickFalse)
4103  break;
4104  mvg_info.offset=j;
4105  switch (primitive_type)
4106  {
4107  case PointPrimitive:
4108  default:
4109  {
4110  if (primitive_info[j].coordinates != 1)
4111  {
4112  status=MagickFalse;
4113  break;
4114  }
4115  status&=TracePoint(primitive_info+j,primitive_info[j].point);
4116  i=(ssize_t) (j+primitive_info[j].coordinates);
4117  break;
4118  }
4119  case LinePrimitive:
4120  {
4121  if (primitive_info[j].coordinates != 2)
4122  {
4123  status=MagickFalse;
4124  break;
4125  }
4126  status&=TraceLine(primitive_info+j,primitive_info[j].point,
4127  primitive_info[j+1].point);
4128  i=(ssize_t) (j+primitive_info[j].coordinates);
4129  break;
4130  }
4131  case RectanglePrimitive:
4132  {
4133  if (primitive_info[j].coordinates != 2)
4134  {
4135  status=MagickFalse;
4136  break;
4137  }
4138  status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4139  primitive_info[j+1].point);
4140  i=(ssize_t) (j+primitive_info[j].coordinates);
4141  break;
4142  }
4144  {
4145  if (primitive_info[j].coordinates != 3)
4146  {
4147  status=MagickFalse;
4148  break;
4149  }
4150  if ((primitive_info[j+2].point.x < 0.0) ||
4151  (primitive_info[j+2].point.y < 0.0))
4152  {
4153  status=MagickFalse;
4154  break;
4155  }
4156  if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4157  {
4158  status=MagickFalse;
4159  break;
4160  }
4161  if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4162  {
4163  status=MagickFalse;
4164  break;
4165  }
4166  status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4167  primitive_info[j+1].point,primitive_info[j+2].point);
4168  i=(ssize_t) (j+primitive_info[j].coordinates);
4169  break;
4170  }
4171  case ArcPrimitive:
4172  {
4173  if (primitive_info[j].coordinates != 3)
4174  {
4175  primitive_type=UndefinedPrimitive;
4176  break;
4177  }
4178  status&=TraceArc(&mvg_info,primitive_info[j].point,
4179  primitive_info[j+1].point,primitive_info[j+2].point);
4180  i=(ssize_t) (j+primitive_info[j].coordinates);
4181  break;
4182  }
4183  case EllipsePrimitive:
4184  {
4185  if (primitive_info[j].coordinates != 3)
4186  {
4187  status=MagickFalse;
4188  break;
4189  }
4190  if ((primitive_info[j+1].point.x < 0.0) ||
4191  (primitive_info[j+1].point.y < 0.0))
4192  {
4193  status=MagickFalse;
4194  break;
4195  }
4196  status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4197  primitive_info[j+1].point,primitive_info[j+2].point);
4198  i=(ssize_t) (j+primitive_info[j].coordinates);
4199  break;
4200  }
4201  case CirclePrimitive:
4202  {
4203  if (primitive_info[j].coordinates != 2)
4204  {
4205  status=MagickFalse;
4206  break;
4207  }
4208  status&=TraceCircle(&mvg_info,primitive_info[j].point,
4209  primitive_info[j+1].point);
4210  i=(ssize_t) (j+primitive_info[j].coordinates);
4211  break;
4212  }
4213  case PolylinePrimitive:
4214  {
4215  if (primitive_info[j].coordinates < 1)
4216  {
4217  status=MagickFalse;
4218  break;
4219  }
4220  break;
4221  }
4222  case PolygonPrimitive:
4223  {
4224  if (primitive_info[j].coordinates < 3)
4225  {
4226  status=MagickFalse;
4227  break;
4228  }
4229  primitive_info[i]=primitive_info[j];
4230  primitive_info[i].coordinates=0;
4231  primitive_info[j].coordinates++;
4232  primitive_info[j].closed_subpath=MagickTrue;
4233  i++;
4234  break;
4235  }
4236  case BezierPrimitive:
4237  {
4238  if (primitive_info[j].coordinates < 3)
4239  {
4240  status=MagickFalse;
4241  break;
4242  }
4243  status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4244  i=(ssize_t) (j+primitive_info[j].coordinates);
4245  break;
4246  }
4247  case PathPrimitive:
4248  {
4249  coordinates=(double) TracePath(&mvg_info,token,exception);
4250  if (coordinates == 0.0)
4251  {
4252  status=MagickFalse;
4253  break;
4254  }
4255  i=(ssize_t) (j+coordinates);
4256  break;
4257  }
4258  case AlphaPrimitive:
4259  case ColorPrimitive:
4260  {
4261  ssize_t
4262  method;
4263 
4264  if (primitive_info[j].coordinates != 1)
4265  {
4266  status=MagickFalse;
4267  break;
4268  }
4269  (void) GetNextToken(q,&q,extent,token);
4271  if (method == -1)
4272  {
4273  status=MagickFalse;
4274  break;
4275  }
4276  primitive_info[j].method=(PaintMethod) method;
4277  break;
4278  }
4279  case TextPrimitive:
4280  {
4281  char
4282  geometry[MagickPathExtent];
4283 
4284  if (primitive_info[j].coordinates != 1)
4285  {
4286  status=MagickFalse;
4287  break;
4288  }
4289  if (*token != ',')
4290  (void) GetNextToken(q,&q,extent,token);
4291  (void) CloneString(&primitive_info[j].text,token);
4292  /*
4293  Compute text cursor offset.
4294  */
4295  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4296  if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4297  (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4298  {
4299  mvg_info.point=primitive_info->point;
4300  primitive_info->point.x+=cursor;
4301  }
4302  else
4303  {
4304  mvg_info.point=primitive_info->point;
4305  cursor=0.0;
4306  }
4307  (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4308  primitive_info->point.x,primitive_info->point.y);
4309  clone_info->render=MagickFalse;
4310  clone_info->text=AcquireString(token);
4311  status&=GetTypeMetrics(image,clone_info,&metrics,exception);
4312  clone_info=DestroyDrawInfo(clone_info);
4313  cursor+=metrics.width;
4314  if (graphic_context[n]->compliance != SVGCompliance)
4315  cursor=0.0;
4316  break;
4317  }
4318  case ImagePrimitive:
4319  {
4320  if (primitive_info[j].coordinates != 2)
4321  {
4322  status=MagickFalse;
4323  break;
4324  }
4325  (void) GetNextToken(q,&q,extent,token);
4326  (void) CloneString(&primitive_info[j].text,token);
4327  break;
4328  }
4329  }
4330  mvg_info.offset=i;
4331  if ((image->debug != MagickFalse) && (q > p))
4332  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p-1),
4333  p);
4334  if (status == MagickFalse)
4335  break;
4336  primitive_info[i].primitive=UndefinedPrimitive;
4337  if (i == 0)
4338  continue;
4339  /*
4340  Transform points.
4341  */
4342  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4343  {
4344  point=primitive_info[i].point;
4345  primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4346  graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4347  primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4348  graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4349  point=primitive_info[i].point;
4350  if (point.x < graphic_context[n]->bounds.x1)
4351  graphic_context[n]->bounds.x1=point.x;
4352  if (point.y < graphic_context[n]->bounds.y1)
4353  graphic_context[n]->bounds.y1=point.y;
4354  if (point.x > graphic_context[n]->bounds.x2)
4355  graphic_context[n]->bounds.x2=point.x;
4356  if (point.y > graphic_context[n]->bounds.y2)
4357  graphic_context[n]->bounds.y2=point.y;
4358  if (primitive_info[i].primitive == ImagePrimitive)
4359  break;
4360  if (i >= (ssize_t) number_points)
4361  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4362  }
4363  if (graphic_context[n]->render != MagickFalse)
4364  {
4365  if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4366  (graphic_context[n]->clip_mask != (char *) NULL) &&
4367  (LocaleCompare(graphic_context[n]->clip_mask,
4368  graphic_context[n-1]->clip_mask) != 0))
4369  {
4370  const char
4371  *clip_path;
4372 
4373  clip_path=(const char *) GetValueFromSplayTree(macros,
4374  graphic_context[n]->clip_mask);
4375  if (clip_path != (const char *) NULL)
4376  (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4377  clip_path);
4378  status&=DrawClipPath(image,graphic_context[n],
4379  graphic_context[n]->clip_mask,exception);
4380  }
4381  status&=DrawPrimitive(image,graphic_context[n],primitive_info,
4382  exception);
4383  }
4384  proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4385  primitive_extent);
4386  if (proceed == MagickFalse)
4387  break;
4388  if (status == 0)
4389  break;
4390  }
4391  if (image->debug != MagickFalse)
4392  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4393  /*
4394  Relinquish resources.
4395  */
4396  macros=DestroySplayTree(macros);
4397  token=DestroyString(token);
4398  if (primitive_info != (PrimitiveInfo *) NULL)
4399  {
4400  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4401  if ((primitive_info[i].primitive == TextPrimitive) ||
4402  (primitive_info[i].primitive == ImagePrimitive))
4403  if (primitive_info[i].text != (char *) NULL)
4404  primitive_info[i].text=DestroyString(primitive_info[i].text);
4405  primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4406  }
4407  primitive=DestroyString(primitive);
4408  if (stops != (StopInfo *) NULL)
4409  stops=(StopInfo *) RelinquishMagickMemory(stops);
4410  for ( ; n >= 0; n--)
4411  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4412  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4413  if (status == MagickFalse)
4414  ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
4415  keyword);
4416  return(status != 0 ? MagickTrue : MagickFalse);
4417 }
4418 
4420  ExceptionInfo *exception)
4421 {
4422  return(RenderMVGContent(image,draw_info,0,exception));
4423 }
4424 
4425 /*
4426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4427 % %
4428 % %
4429 % %
4430 % D r a w P a t t e r n P a t h %
4431 % %
4432 % %
4433 % %
4434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4435 %
4436 % DrawPatternPath() draws a pattern.
4437 %
4438 % The format of the DrawPatternPath method is:
4439 %
4440 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4441 % const char *name,Image **pattern,ExceptionInfo *exception)
4442 %
4443 % A description of each parameter follows:
4444 %
4445 % o image: the image.
4446 %
4447 % o draw_info: the draw info.
4448 %
4449 % o name: the pattern name.
4450 %
4451 % o image: the image.
4452 %
4453 % o exception: return any errors or warnings in this structure.
4454 %
4455 */
4457  const DrawInfo *draw_info,const char *name,Image **pattern,
4458  ExceptionInfo *exception)
4459 {
4460  char
4461  property[MagickPathExtent];
4462 
4463  const char
4464  *geometry,
4465  *path,
4466  *type;
4467 
4468  DrawInfo
4469  *clone_info;
4470 
4471  ImageInfo
4472  *image_info;
4473 
4475  status;
4476 
4477  assert(image != (Image *) NULL);
4478  assert(image->signature == MagickCoreSignature);
4479  if (image->debug != MagickFalse)
4480  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4481  assert(draw_info != (const DrawInfo *) NULL);
4482  assert(name != (const char *) NULL);
4483  (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
4484  path=GetImageArtifact(image,property);
4485  if (path == (const char *) NULL)
4486  return(MagickFalse);
4487  (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
4488  geometry=GetImageArtifact(image,property);
4489  if (geometry == (const char *) NULL)
4490  return(MagickFalse);
4491  if ((*pattern) != (Image *) NULL)
4492  *pattern=DestroyImage(*pattern);
4493  image_info=AcquireImageInfo();
4494  image_info->size=AcquireString(geometry);
4495  *pattern=AcquireImage(image_info,exception);
4496  image_info=DestroyImageInfo(image_info);
4497  (void) QueryColorCompliance("#000000ff",AllCompliance,
4498  &(*pattern)->background_color,exception);
4499  (void) SetImageBackgroundColor(*pattern,exception);
4500  if (image->debug != MagickFalse)
4502  "begin pattern-path %s %s",name,geometry);
4503  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4504  clone_info->fill_pattern=NewImageList();
4505  clone_info->stroke_pattern=NewImageList();
4506  (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
4507  type=GetImageArtifact(image,property);
4508  if (type != (const char *) NULL)
4511  (void) CloneString(&clone_info->primitive,path);
4512  status=RenderMVGContent(*pattern,clone_info,0,exception);
4513  clone_info=DestroyDrawInfo(clone_info);
4514  if (image->debug != MagickFalse)
4515  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4516  return(status);
4517 }
4518 
4519 /*
4520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4521 % %
4522 % %
4523 % %
4524 + D r a w P o l y g o n P r i m i t i v e %
4525 % %
4526 % %
4527 % %
4528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4529 %
4530 % DrawPolygonPrimitive() draws a polygon on the image.
4531 %
4532 % The format of the DrawPolygonPrimitive method is:
4533 %
4534 % MagickBooleanType DrawPolygonPrimitive(Image *image,
4535 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4536 % ExceptionInfo *exception)
4537 %
4538 % A description of each parameter follows:
4539 %
4540 % o image: the image.
4541 %
4542 % o draw_info: the draw info.
4543 %
4544 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4545 %
4546 % o exception: return any errors or warnings in this structure.
4547 %
4548 */
4549 
4551 {
4552  register ssize_t
4553  i;
4554 
4555  assert(polygon_info != (PolygonInfo **) NULL);
4556  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4557  if (polygon_info[i] != (PolygonInfo *) NULL)
4558  polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4559  polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4560  return(polygon_info);
4561 }
4562 
4564  const PrimitiveInfo *primitive_info)
4565 {
4566  PathInfo
4567  *magick_restrict path_info;
4568 
4569  PolygonInfo
4570  **polygon_info;
4571 
4572  register ssize_t
4573  i;
4574 
4575  size_t
4576  number_threads;
4577 
4578  number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4579  polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4580  sizeof(*polygon_info));
4581  if (polygon_info == (PolygonInfo **) NULL)
4582  return((PolygonInfo **) NULL);
4583  (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4584  path_info=ConvertPrimitiveToPath(primitive_info);
4585  if (path_info == (PathInfo *) NULL)
4586  return(DestroyPolygonThreadSet(polygon_info));
4587  for (i=0; i < (ssize_t) number_threads; i++)
4588  {
4589  polygon_info[i]=ConvertPathToPolygon(path_info);
4590  if (polygon_info[i] == (PolygonInfo *) NULL)
4591  return(DestroyPolygonThreadSet(polygon_info));
4592  }
4593  path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4594  return(polygon_info);
4595 }
4596 
4597 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
4598  const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4599  const ssize_t y,double *stroke_alpha)
4600 {
4601  double
4602  alpha,
4603  beta,
4604  distance,
4605  subpath_alpha;
4606 
4607  PointInfo
4608  delta;
4609 
4610  register const PointInfo
4611  *q;
4612 
4613  register EdgeInfo
4614  *p;
4615 
4616  register ssize_t
4617  i;
4618 
4619  ssize_t
4620  j,
4621  winding_number;
4622 
4623  /*
4624  Compute fill & stroke opacity for this (x,y) point.
4625  */
4626  *stroke_alpha=0.0;
4627  subpath_alpha=0.0;
4628  p=polygon_info->edges;
4629  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4630  {
4631  if ((double) y <= (p->bounds.y1-mid-0.5))
4632  break;
4633  if ((double) y > (p->bounds.y2+mid+0.5))
4634  {
4635  (void) DestroyEdge(polygon_info,(size_t) j);
4636  continue;
4637  }
4638  if (((double) x <= (p->bounds.x1-mid-0.5)) ||
4639  ((double) x > (p->bounds.x2+mid+0.5)))
4640  continue;
4641  i=(ssize_t) MagickMax((double) p->highwater,1.0);
4642  for ( ; i < (ssize_t) p->number_points; i++)
4643  {
4644  if ((double) y <= (p->points[i-1].y-mid-0.5))
4645  break;
4646  if ((double) y > (p->points[i].y+mid+0.5))
4647  continue;
4648  if (p->scanline != (double) y)
4649  {
4650  p->scanline=(double) y;
4651  p->highwater=(size_t) i;
4652  }
4653  /*
4654  Compute distance between a point and an edge.
4655  */
4656  q=p->points+i-1;
4657  delta.x=(q+1)->x-q->x;
4658  delta.y=(q+1)->y-q->y;
4659  beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4660  if (beta <= 0.0)
4661  {
4662  delta.x=(double) x-q->x;
4663  delta.y=(double) y-q->y;
4664  distance=delta.x*delta.x+delta.y*delta.y;
4665  }
4666  else
4667  {
4668  alpha=delta.x*delta.x+delta.y*delta.y;
4669  if (beta >= alpha)
4670  {
4671  delta.x=(double) x-(q+1)->x;
4672  delta.y=(double) y-(q+1)->y;
4673  distance=delta.x*delta.x+delta.y*delta.y;
4674  }
4675  else
4676  {
4677  alpha=PerceptibleReciprocal(alpha);
4678  beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4679  distance=alpha*beta*beta;
4680  }
4681  }
4682  /*
4683  Compute stroke & subpath opacity.
4684  */
4685  beta=0.0;
4686  if (p->ghostline == MagickFalse)
4687  {
4688  alpha=mid+0.5;
4689  if ((*stroke_alpha < 1.0) &&
4690  (distance <= ((alpha+0.25)*(alpha+0.25))))
4691  {
4692  alpha=mid-0.5;
4693  if (distance <= ((alpha+0.25)*(alpha+0.25)))
4694  *stroke_alpha=1.0;
4695  else
4696  {
4697  beta=1.0;
4698  if (fabs(distance-1.0) >= MagickEpsilon)
4699  beta=sqrt((double) distance);
4700  alpha=beta-mid-0.5;
4701  if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
4702  *stroke_alpha=(alpha-0.25)*(alpha-0.25);
4703  }
4704  }
4705  }
4706  if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
4707  continue;
4708  if (distance <= 0.0)
4709  {
4710  subpath_alpha=1.0;
4711  continue;
4712  }
4713  if (distance > 1.0)
4714  continue;
4715  if (fabs(beta) < MagickEpsilon)
4716  {
4717  beta=1.0;
4718  if (fabs(distance-1.0) >= MagickEpsilon)
4719  beta=sqrt(distance);
4720  }
4721  alpha=beta-1.0;
4722  if (subpath_alpha < (alpha*alpha))
4723  subpath_alpha=alpha*alpha;
4724  }
4725  }
4726  /*
4727  Compute fill opacity.
4728  */
4729  if (fill == MagickFalse)
4730  return(0.0);
4731  if (subpath_alpha >= 1.0)
4732  return(1.0);
4733  /*
4734  Determine winding number.
4735  */
4736  winding_number=0;
4737  p=polygon_info->edges;
4738  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4739  {
4740  if ((double) y <= p->bounds.y1)
4741  break;
4742  if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
4743  continue;
4744  if ((double) x > p->bounds.x2)
4745  {
4746  winding_number+=p->direction ? 1 : -1;
4747  continue;
4748  }
4749  i=(ssize_t) MagickMax((double) p->highwater,1.0);
4750  for ( ; i < (ssize_t) (p->number_points-1); i++)
4751  if ((double) y <= p->points[i].y)
4752  break;
4753  q=p->points+i-1;
4754  if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4755  winding_number+=p->direction ? 1 : -1;
4756  }
4757  if (fill_rule != NonZeroRule)
4758  {
4759  if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4760  return(1.0);
4761  }
4762  else
4763  if (MagickAbsoluteValue(winding_number) != 0)
4764  return(1.0);
4765  return(subpath_alpha);
4766 }
4767 
4769  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4770  ExceptionInfo *exception)
4771 {
4772  CacheView
4773  *image_view;
4774 
4776  fill,
4777  status;
4778 
4779  double
4780  mid;
4781 
4782  PolygonInfo
4783  **magick_restrict polygon_info;
4784 
4785  register EdgeInfo
4786  *p;
4787 
4788  register ssize_t
4789  i;
4790 
4791  SegmentInfo
4792  bounds;
4793 
4794  ssize_t
4795  start_y,
4796  stop_y,
4797  y;
4798 
4799  assert(image != (Image *) NULL);
4800  assert(image->signature == MagickCoreSignature);
4801  if (image->debug != MagickFalse)
4802  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4803  assert(draw_info != (DrawInfo *) NULL);
4804  assert(draw_info->signature == MagickCoreSignature);
4805  assert(primitive_info != (PrimitiveInfo *) NULL);
4806  if (primitive_info->coordinates <= 1)
4807  return(MagickTrue);
4808  /*
4809  Compute bounding box.
4810  */
4811  polygon_info=AcquirePolygonThreadSet(primitive_info);
4812  if (polygon_info == (PolygonInfo **) NULL)
4813  return(MagickFalse);
4814 DisableMSCWarning(4127)
4815  if (0)
4816  {
4817  status=DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
4818  if (status == MagickFalse)
4819  {
4820  polygon_info=DestroyPolygonThreadSet(polygon_info);
4821  return(status);
4822  }
4823  }
4825  if (image->debug != MagickFalse)
4826  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4827  fill=(primitive_info->method == FillToBorderMethod) ||
4828  (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4829  mid=ExpandAffine(&draw_info->affine)*SaneStrokeWidth(image,draw_info)/2.0;
4830  bounds=polygon_info[0]->edges[0].bounds;
4831  for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4832  {
4833  p=polygon_info[0]->edges+i;
4834  if (p->bounds.x1 < bounds.x1)
4835  bounds.x1=p->bounds.x1;
4836  if (p->bounds.y1 < bounds.y1)
4837  bounds.y1=p->bounds.y1;
4838  if (p->bounds.x2 > bounds.x2)
4839  bounds.x2=p->bounds.x2;
4840  if (p->bounds.y2 > bounds.y2)
4841  bounds.y2=p->bounds.y2;
4842  }
4843  bounds.x1-=(mid+1.0);
4844  bounds.y1-=(mid+1.0);
4845  bounds.x2+=(mid+1.0);
4846  bounds.y2+=(mid+1.0);
4847  if ((bounds.x1 >= (double) image->columns) ||
4848  (bounds.y1 >= (double) image->rows) ||
4849  (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
4850  {
4851  polygon_info=DestroyPolygonThreadSet(polygon_info);
4852  return(MagickTrue); /* virtual polygon */
4853  }
4854  bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
4855  (double) image->columns-1.0 : bounds.x1;
4856  bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
4857  (double) image->rows-1.0 : bounds.y1;
4858  bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
4859  (double) image->columns-1.0 : bounds.x2;
4860  bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
4861  (double) image->rows-1.0 : bounds.y2;
4862  status=MagickTrue;
4863  image_view=AcquireAuthenticCacheView(image,exception);
4864  if ((primitive_info->coordinates == 1) ||
4865  (polygon_info[0]->number_edges == 0))
4866  {
4867  /*
4868  Draw point.
4869  */
4870  start_y=(ssize_t) ceil(bounds.y1-0.5);
4871  stop_y=(ssize_t) floor(bounds.y2+0.5);
4872 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4873  #pragma omp parallel for schedule(static) shared(status) \
4874  magick_number_threads(image,image,stop_y-start_y+1,1)
4875 #endif
4876  for (y=start_y; y <= stop_y; y++)
4877  {
4879  sync;
4880 
4881  PixelInfo
4882  pixel;
4883 
4884  register ssize_t
4885  x;
4886 
4887  register Quantum
4888  *magick_restrict q;
4889 
4890  ssize_t
4891  start_x,
4892  stop_x;
4893 
4894  if (status == MagickFalse)
4895  continue;
4896  start_x=(ssize_t) ceil(bounds.x1-0.5);
4897  stop_x=(ssize_t) floor(bounds.x2+0.5);
4898  x=start_x;
4899  q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
4900  exception);
4901  if (q == (Quantum *) NULL)
4902  {
4903  status=MagickFalse;
4904  continue;
4905  }
4906  GetPixelInfo(image,&pixel);
4907  for ( ; x <= stop_x; x++)
4908  {
4909  if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
4910  (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
4911  {
4912  GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
4913  SetPixelViaPixelInfo(image,&pixel,q);
4914  }
4915  q+=GetPixelChannels(image);
4916  }
4917  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4918  if (sync == MagickFalse)
4919  status=MagickFalse;
4920  }
4921  image_view=DestroyCacheView(image_view);
4922  polygon_info=DestroyPolygonThreadSet(polygon_info);
4923  if (image->debug != MagickFalse)
4925  " end draw-polygon");
4926  return(status);
4927  }
4928  /*
4929  Draw polygon or line.
4930  */
4931  start_y=(ssize_t) ceil(bounds.y1-0.5);
4932  stop_y=(ssize_t) floor(bounds.y2+0.5);
4933 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4934  #pragma omp parallel for schedule(static) shared(status) \
4935  magick_number_threads(image,image,stop_y-start_y+1,1)
4936 #endif
4937  for (y=start_y; y <= stop_y; y++)
4938  {
4939  const int
4940  id = GetOpenMPThreadId();
4941 
4942  register Quantum
4943  *magick_restrict q;
4944 
4945  register ssize_t
4946  x;
4947 
4948  ssize_t
4949  start_x,
4950  stop_x;
4951 
4952  if (status == MagickFalse)
4953  continue;
4954  start_x=(ssize_t) ceil(bounds.x1-0.5);
4955  stop_x=(ssize_t) floor(bounds.x2+0.5);
4956  q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+
4957  1),1,exception);
4958  if (q == (Quantum *) NULL)
4959  {
4960  status=MagickFalse;
4961  continue;
4962  }
4963  for (x=start_x; x <= stop_x; x++)
4964  {
4965  double
4966  fill_alpha,
4967  stroke_alpha;
4968 
4969  PixelInfo
4970  fill_color,
4971  stroke_color;
4972 
4973  /*
4974  Fill and/or stroke.
4975  */
4976  fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
4977  x,y,&stroke_alpha);
4978  if (draw_info->stroke_antialias == MagickFalse)
4979  {
4980  fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4981  stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4982  }
4983  GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
4984  CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
4985  (double) GetPixelAlpha(image,q),q);
4986  GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
4987  CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
4988  (double) GetPixelAlpha(image,q),q);
4989  q+=GetPixelChannels(image);
4990  }
4991  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4992  status=MagickFalse;
4993  }
4994  image_view=DestroyCacheView(image_view);
4995  polygon_info=DestroyPolygonThreadSet(polygon_info);
4996  if (image->debug != MagickFalse)
4997  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4998  return(status);
4999 }
5000 
5001 /*
5002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5003 % %
5004 % %
5005 % %
5006 % D r a w P r i m i t i v e %
5007 % %
5008 % %
5009 % %
5010 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5011 %
5012 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
5013 %
5014 % The format of the DrawPrimitive method is:
5015 %
5016 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
5017 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
5018 %
5019 % A description of each parameter follows:
5020 %
5021 % o image: the image.
5022 %
5023 % o draw_info: the draw info.
5024 %
5025 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5026 %
5027 % o exception: return any errors or warnings in this structure.
5028 %
5029 */
5030 
5031 static inline double ConstrainCoordinate(double x)
5032 {
5033  if (x < (double) -(SSIZE_MAX-512))
5034  return((double) -(SSIZE_MAX-512));
5035  if (x > (double) (SSIZE_MAX-512))
5036  return((double) (SSIZE_MAX-512));
5037  return(x);
5038 }
5039 
5040 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
5041 {
5042  const char
5043  *methods[] =
5044  {
5045  "point",
5046  "replace",
5047  "floodfill",
5048  "filltoborder",
5049  "reset",
5050  "?"
5051  };
5052 
5053  PointInfo
5054  p,
5055  point,
5056  q;
5057 
5058  register ssize_t
5059  i,
5060  x;
5061 
5062  ssize_t
5063  coordinates,
5064  y;
5065 
5066  x=(ssize_t) ceil(primitive_info->point.x-0.5);
5067  y=(ssize_t) ceil(primitive_info->point.y-0.5);
5068  switch (primitive_info->primitive)
5069  {
5070  case AlphaPrimitive:
5071  {
5073  "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
5074  methods[primitive_info->method]);
5075  return;
5076  }
5077  case ColorPrimitive:
5078  {
5080  "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
5081  methods[primitive_info->method]);
5082  return;
5083  }
5084  case ImagePrimitive:
5085  {
5087  "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
5088  return;
5089  }
5090  case PointPrimitive:
5091  {
5093  "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
5094  methods[primitive_info->method]);
5095  return;
5096  }
5097  case TextPrimitive:
5098  {
5100  "TextPrimitive %.20g,%.20g",(double) x,(double) y);
5101  return;
5102  }
5103  default:
5104  break;
5105  }
5106  coordinates=0;
5107  p=primitive_info[0].point;
5108  q.x=(-1.0);
5109  q.y=(-1.0);
5110  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5111  {
5112  point=primitive_info[i].point;
5113  if (coordinates <= 0)
5114  {
5115  coordinates=(ssize_t) primitive_info[i].coordinates;
5117  " begin open (%.20g)",(double) coordinates);
5118  p=point;
5119  }
5120  point=primitive_info[i].point;
5121  if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5122  (fabs(q.y-point.y) >= MagickEpsilon))
5124  " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5125  else
5127  " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5128  q=point;
5129  coordinates--;
5130  if (coordinates > 0)
5131  continue;
5132  if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5133  (fabs(p.y-point.y) >= MagickEpsilon))
5134  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
5135  (double) coordinates);
5136  else
5137  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
5138  (double) coordinates);
5139  }
5140 }
5141 
5143  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5144  ExceptionInfo *exception)
5145 {
5146  CacheView
5147  *image_view;
5148 
5150  status;
5151 
5152  register ssize_t
5153  i,
5154  x;
5155 
5156  ssize_t
5157  y;
5158 
5159  if (image->debug != MagickFalse)
5160  {
5162  " begin draw-primitive");
5164  " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5165  draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5166  draw_info->affine.tx,draw_info->affine.ty);
5167  }
5168  status=MagickTrue;
5169  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5170  ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5171  (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5172  status=SetImageColorspace(image,sRGBColorspace,exception);
5173  if (draw_info->compliance == SVGCompliance)
5174  {
5175  status&=SetImageMask(image,WritePixelMask,draw_info->clipping_mask,
5176  exception);
5177  status&=SetImageMask(image,CompositePixelMask,draw_info->composite_mask,
5178  exception);
5179  }
5180  x=(ssize_t) ceil(ConstrainCoordinate(primitive_info->point.x-0.5));
5181  y=(ssize_t) ceil(ConstrainCoordinate(primitive_info->point.y-0.5));
5182  image_view=AcquireAuthenticCacheView(image,exception);
5183  switch (primitive_info->primitive)
5184  {
5185  case AlphaPrimitive:
5186  {
5187  if (image->alpha_trait == UndefinedPixelTrait)
5188  (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
5189  switch (primitive_info->method)
5190  {
5191  case PointMethod:
5192  default:
5193  {
5194  PixelInfo
5195  pixel;
5196 
5197  register Quantum
5198  *q;
5199 
5200  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5201  if (q == (Quantum *) NULL)
5202  break;
5203  GetFillColor(draw_info,x,y,&pixel,exception);
5204  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5205  (void) SyncCacheViewAuthenticPixels(image_view,exception);
5206  break;
5207  }
5208  case ReplaceMethod:
5209  {
5211  sync;
5212 
5213  PixelInfo
5214  pixel,
5215  target;
5216 
5217  (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
5218  exception);
5219  GetPixelInfo(image,&pixel);
5220  for (y=0; y < (ssize_t) image->rows; y++)
5221  {
5222  register Quantum
5223  *magick_restrict q;
5224 
5225  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5226  exception);
5227  if (q == (Quantum *) NULL)
5228  break;
5229  for (x=0; x < (ssize_t) image->columns; x++)
5230  {
5231  GetPixelInfoPixel(image,q,&pixel);
5232  if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5233  {
5234  q+=GetPixelChannels(image);
5235  continue;
5236  }
5237  GetFillColor(draw_info,x,y,&pixel,exception);
5238  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5239  q+=GetPixelChannels(image);
5240  }
5241  sync=SyncCacheViewAuthenticPixels(image_view,exception);
5242  if (sync == MagickFalse)
5243  break;
5244  }
5245  break;
5246  }
5247  case FloodfillMethod:
5248  case FillToBorderMethod:
5249  {
5250  ChannelType
5251  channel_mask;
5252 
5253  PixelInfo
5254  target;
5255 
5257  &target,exception);
5258  if (primitive_info->method == FillToBorderMethod)
5259  {
5260  target.red=(double) draw_info->border_color.red;
5261  target.green=(double) draw_info->border_color.green;
5262  target.blue=(double) draw_info->border_color.blue;
5263  }
5264  channel_mask=SetImageChannelMask(image,AlphaChannel);
5265  status&=FloodfillPaintImage(image,draw_info,&target,x,y,
5266  primitive_info->method == FloodfillMethod ? MagickFalse :
5267  MagickTrue,exception);
5268  (void) SetImageChannelMask(image,channel_mask);
5269  break;
5270  }
5271  case ResetMethod:
5272  {
5274  sync;
5275 
5276  PixelInfo
5277  pixel;
5278 
5279  for (y=0; y < (ssize_t) image->rows; y++)
5280  {
5281  register Quantum
5282  *magick_restrict q;
5283 
5284  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5285  exception);
5286  if (q == (Quantum *) NULL)
5287  break;
5288  for (x=0; x < (ssize_t) image->columns; x++)
5289  {
5290  GetFillColor(draw_info,x,y,&pixel,exception);
5291  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5292  q+=GetPixelChannels(image);
5293  }
5294  sync=SyncCacheViewAuthenticPixels(image_view,exception);
5295  if (sync == MagickFalse)
5296  break;
5297  }
5298  break;
5299  }
5300  }
5301  break;
5302  }
5303  case ColorPrimitive:
5304  {
5305  switch (primitive_info->method)
5306  {
5307  case PointMethod:
5308  default:
5309  {
5310  PixelInfo
5311  pixel;
5312 
5313  register Quantum
5314  *q;
5315 
5316  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5317  if (q == (Quantum *) NULL)
5318  break;
5319  GetPixelInfo(image,&pixel);
5320  GetFillColor(draw_info,x,y,&pixel,exception);
5321  SetPixelViaPixelInfo(image,&pixel,q);
5322  (void) SyncCacheViewAuthenticPixels(image_view,exception);
5323  break;
5324  }
5325  case ReplaceMethod:
5326  {
5328  sync;
5329 
5330  PixelInfo
5331  pixel,
5332  target;
5333 
5334  (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
5335  exception);
5336  for (y=0; y < (ssize_t) image->rows; y++)
5337  {
5338  register Quantum
5339  *magick_restrict q;
5340 
5341  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5342  exception);
5343  if (q == (Quantum *) NULL)
5344  break;
5345  for (x=0; x < (ssize_t) image->columns; x++)
5346  {
5347  GetPixelInfoPixel(image,q,&pixel);
5348  if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5349  {
5350  q+=GetPixelChannels(image);
5351  continue;
5352  }
5353  GetFillColor(draw_info,x,y,&pixel,exception);
5354  SetPixelViaPixelInfo(image,&pixel,q);
5355  q+=GetPixelChannels(image);
5356  }
5357  sync=SyncCacheViewAuthenticPixels(image_view,exception);
5358  if (sync == MagickFalse)
5359  break;
5360  }
5361  break;
5362  }
5363  case FloodfillMethod:
5364  case FillToBorderMethod:
5365  {
5366  PixelInfo
5367  target;
5368 
5370  &target,exception);
5371  if (primitive_info->method == FillToBorderMethod)
5372  {
5373  target.red=(double) draw_info->border_color.red;
5374  target.green=(double) draw_info->border_color.green;
5375  target.blue=(double) draw_info->border_color.blue;
5376  }
5377  status&=FloodfillPaintImage(image,draw_info,&target,x,y,
5378  primitive_info->method == FloodfillMethod ? MagickFalse :
5379  MagickTrue,exception);
5380  break;
5381  }
5382  case ResetMethod:
5383  {
5385  sync;
5386 
5387  PixelInfo
5388  pixel;
5389 
5390  GetPixelInfo(image,&pixel);
5391  for (y=0; y < (ssize_t) image->rows; y++)
5392  {
5393  register Quantum
5394  *magick_restrict q;
5395 
5396  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5397  exception);
5398  if (q == (Quantum *) NULL)
5399  break;
5400  for (x=0; x < (ssize_t) image->columns; x++)
5401  {
5402  GetFillColor(draw_info,x,y,&pixel,exception);
5403  SetPixelViaPixelInfo(image,&pixel,q);
5404  q+=GetPixelChannels(image);
5405  }
5406  sync=SyncCacheViewAuthenticPixels(image_view,exception);
5407  if (sync == MagickFalse)
5408  break;
5409  }
5410  break;
5411  }
5412  }
5413  break;
5414  }
5415  case ImagePrimitive:
5416  {
5417  AffineMatrix
5418  affine;
5419 
5420  char
5421  composite_geometry[MagickPathExtent];
5422 
5423  Image
5424  *composite_image,
5425  *composite_images;
5426 
5427  ImageInfo
5428  *clone_info;
5429 
5431  geometry;
5432 
5433  ssize_t
5434  x1,
5435  y1;
5436 
5437  if (primitive_info->text == (char *) NULL)
5438  break;
5439  clone_info=AcquireImageInfo();
5440  composite_images=(Image *) NULL;
5441  if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5442  composite_images=ReadInlineImage(clone_info,primitive_info->text,
5443  exception);
5444  else
5445  if (*primitive_info->text != '\0')
5446  {
5447  (void) CopyMagickString(clone_info->filename,primitive_info->text,
5449  composite_images=ReadImage(clone_info,exception);
5450  }
5451  clone_info=DestroyImageInfo(clone_info);
5452  if (composite_images == (Image *) NULL)
5453  {
5454  status=0;
5455  break;
5456  }
5457  composite_image=RemoveFirstImageFromList(&composite_images);
5458  composite_images=DestroyImageList(composite_images);
5459<