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