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  primitive=(char *) NULL;
2512  if (*draw_info->primitive != '@')
2513  primitive=AcquireString(draw_info->primitive);
2514  else
2515  if ((strlen(draw_info->primitive) > 1) &&
2516  (*(draw_info->primitive+1) != '-'))
2517  primitive=FileToString(draw_info->primitive+1,~0UL,exception);
2518  if (primitive == (char *) NULL)
2519  return(MagickFalse);
2520  primitive_extent=(double) strlen(primitive);
2521  (void) SetImageArtifact(image,"mvg:vector-graphics",primitive);
2522  n=0;
2523  number_stops=0;
2524  stops=(StopInfo *) NULL;
2525  /*
2526  Allocate primitive info memory.
2527  */
2528  graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2529  if (graphic_context == (DrawInfo **) NULL)
2530  {
2531  primitive=DestroyString(primitive);
2532  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2533  image->filename);
2534  }
2535  number_points=PrimitiveExtentPad;
2536  primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
2537  sizeof(*primitive_info));
2538  if (primitive_info == (PrimitiveInfo *) NULL)
2539  {
2540  primitive=DestroyString(primitive);
2541  for ( ; n >= 0; n--)
2542  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2543  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2544  ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2545  image->filename);
2546  }
2547  (void) memset(primitive_info,0,(size_t) number_points*
2548  sizeof(*primitive_info));
2549  (void) memset(&mvg_info,0,sizeof(mvg_info));
2550  mvg_info.primitive_info=(&primitive_info);
2551  mvg_info.extent=(&number_points);
2552  mvg_info.exception=exception;
2553  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2554  graphic_context[n]->viewbox=image->page;
2555  if ((image->page.width == 0) || (image->page.height == 0))
2556  {
2557  graphic_context[n]->viewbox.width=image->columns;
2558  graphic_context[n]->viewbox.height=image->rows;
2559  }
2560  token=AcquireString(primitive);
2561  extent=strlen(token)+MagickPathExtent;
2562  defsDepth=0;
2563  symbolDepth=0;
2564  cursor=0.0;
2565  macros=GetMVGMacros(primitive);
2566  status=MagickTrue;
2567  for (q=primitive; *q != '\0'; )
2568  {
2569  /*
2570  Interpret graphic primitive.
2571  */
2572  GetNextToken(q,&q,MagickPathExtent,keyword);
2573  if (*keyword == '\0')
2574  break;
2575  if (*keyword == '#')
2576  {
2577  /*
2578  Comment.
2579  */
2580  while ((*q != '\n') && (*q != '\0'))
2581  q++;
2582  continue;
2583  }
2584  p=q-strlen(keyword)-1;
2585  primitive_type=UndefinedPrimitive;
2586  current=graphic_context[n]->affine;
2587  GetAffineMatrix(&affine);
2588  switch (*keyword)
2589  {
2590  case ';':
2591  break;
2592  case 'a':
2593  case 'A':
2594  {
2595  if (LocaleCompare("affine",keyword) == 0)
2596  {
2597  GetNextToken(q,&q,extent,token);
2598  affine.sx=StringToDouble(token,&next_token);
2599  if (token == next_token)
2600  ThrowPointExpectedException(token,exception);
2601  GetNextToken(q,&q,extent,token);
2602  if (*token == ',')
2603  GetNextToken(q,&q,extent,token);
2604  affine.rx=StringToDouble(token,&next_token);
2605  if (token == next_token)
2606  ThrowPointExpectedException(token,exception);
2607  GetNextToken(q,&q,extent,token);
2608  if (*token == ',')
2609  GetNextToken(q,&q,extent,token);
2610  affine.ry=StringToDouble(token,&next_token);
2611  if (token == next_token)
2612  ThrowPointExpectedException(token,exception);
2613  GetNextToken(q,&q,extent,token);
2614  if (*token == ',')
2615  GetNextToken(q,&q,extent,token);
2616  affine.sy=StringToDouble(token,&next_token);
2617  if (token == next_token)
2618  ThrowPointExpectedException(token,exception);
2619  GetNextToken(q,&q,extent,token);
2620  if (*token == ',')
2621  GetNextToken(q,&q,extent,token);
2622  affine.tx=StringToDouble(token,&next_token);
2623  if (token == next_token)
2624  ThrowPointExpectedException(token,exception);
2625  GetNextToken(q,&q,extent,token);
2626  if (*token == ',')
2627  GetNextToken(q,&q,extent,token);
2628  affine.ty=StringToDouble(token,&next_token);
2629  if (token == next_token)
2630  ThrowPointExpectedException(token,exception);
2631  break;
2632  }
2633  if (LocaleCompare("alpha",keyword) == 0)
2634  {
2635  primitive_type=AlphaPrimitive;
2636  break;
2637  }
2638  if (LocaleCompare("arc",keyword) == 0)
2639  {
2640  primitive_type=ArcPrimitive;
2641  break;
2642  }
2643  status=MagickFalse;
2644  break;
2645  }
2646  case 'b':
2647  case 'B':
2648  {
2649  if (LocaleCompare("bezier",keyword) == 0)
2650  {
2651  primitive_type=BezierPrimitive;
2652  break;
2653  }
2654  if (LocaleCompare("border-color",keyword) == 0)
2655  {
2656  GetNextToken(q,&q,extent,token);
2657  status&=QueryColorCompliance(token,AllCompliance,
2658  &graphic_context[n]->border_color,exception);
2659  break;
2660  }
2661  status=MagickFalse;
2662  break;
2663  }
2664  case 'c':
2665  case 'C':
2666  {
2667  if (LocaleCompare("class",keyword) == 0)
2668  {
2669  const char
2670  *mvg_class;
2671 
2672  GetNextToken(q,&q,extent,token);
2673  if (*token == '\0')
2674  {
2675  status=MagickFalse;
2676  break;
2677  }
2678  mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2679  if (mvg_class != (const char *) NULL)
2680  {
2681  char
2682  *elements;
2683 
2684  ssize_t
2685  offset;
2686 
2687  /*
2688  Inject class elements in stream.
2689  */
2690  offset=(ssize_t) (p-primitive);
2691  elements=AcquireString(primitive);
2692  elements[offset]='\0';
2693  (void) ConcatenateString(&elements,mvg_class);
2694  (void) ConcatenateString(&elements,"\n");
2695  (void) ConcatenateString(&elements,q);
2696  primitive=DestroyString(primitive);
2697  primitive=elements;
2698  q=primitive+offset;
2699  }
2700  break;
2701  }
2702  if (LocaleCompare("clip-path",keyword) == 0)
2703  {
2704  const char
2705  *clip_path;
2706 
2707  /*
2708  Take a node from within the MVG document, and duplicate it here.
2709  */
2710  GetNextToken(q,&q,extent,token);
2711  if (*token == '\0')
2712  {
2713  status=MagickFalse;
2714  break;
2715  }
2716  (void) CloneString(&graphic_context[n]->clip_mask,token);
2717  clip_path=(const char *) GetValueFromSplayTree(macros,token);
2718  if (clip_path != (const char *) NULL)
2719  {
2720  if (graphic_context[n]->clipping_mask != (Image *) NULL)
2721  graphic_context[n]->clipping_mask=
2722  DestroyImage(graphic_context[n]->clipping_mask);
2723  graphic_context[n]->clipping_mask=DrawClippingMask(image,
2724  graphic_context[n],token,clip_path,exception);
2725  if (draw_info->compliance != SVGCompliance)
2726  {
2727  const char
2728  *clip_path;
2729 
2730  clip_path=(const char *) GetValueFromSplayTree(macros,
2731  graphic_context[n]->clip_mask);
2732  if (clip_path != (const char *) NULL)
2733  (void) SetImageArtifact(image,
2734  graphic_context[n]->clip_mask,clip_path);
2735  status&=DrawClipPath(image,graphic_context[n],
2736  graphic_context[n]->clip_mask,exception);
2737  }
2738  }
2739  break;
2740  }
2741  if (LocaleCompare("clip-rule",keyword) == 0)
2742  {
2743  ssize_t
2744  fill_rule;
2745 
2746  GetNextToken(q,&q,extent,token);
2748  token);
2749  if (fill_rule == -1)
2750  {
2751  status=MagickFalse;
2752  break;
2753  }
2754  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2755  break;
2756  }
2757  if (LocaleCompare("clip-units",keyword) == 0)
2758  {
2759  ssize_t
2760  clip_units;
2761 
2762  GetNextToken(q,&q,extent,token);
2764  token);
2765  if (clip_units == -1)
2766  {
2767  status=MagickFalse;
2768  break;
2769  }
2770  graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2771  if (clip_units == ObjectBoundingBox)
2772  {
2773  GetAffineMatrix(&current);
2774  affine.sx=draw_info->bounds.x2;
2775  affine.sy=draw_info->bounds.y2;
2776  affine.tx=draw_info->bounds.x1;
2777  affine.ty=draw_info->bounds.y1;
2778  break;
2779  }
2780  break;
2781  }
2782  if (LocaleCompare("circle",keyword) == 0)
2783  {
2784  primitive_type=CirclePrimitive;
2785  break;
2786  }
2787  if (LocaleCompare("color",keyword) == 0)
2788  {
2789  primitive_type=ColorPrimitive;
2790  break;
2791  }
2792  if (LocaleCompare("compliance",keyword) == 0)
2793  {
2794  /*
2795  MVG compliance associates a clipping mask with an image; SVG
2796  compliance associates a clipping mask with a graphics context.
2797  */
2798  GetNextToken(q,&q,extent,token);
2799  graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2801  break;
2802  }
2803  status=MagickFalse;
2804  break;
2805  }
2806  case 'd':
2807  case 'D':
2808  {
2809  if (LocaleCompare("decorate",keyword) == 0)
2810  {
2811  ssize_t
2812  decorate;
2813 
2814  GetNextToken(q,&q,extent,token);
2816  token);
2817  if (decorate == -1)
2818  {
2819  status=MagickFalse;
2820  break;
2821  }
2822  graphic_context[n]->decorate=(DecorationType) decorate;
2823  break;
2824  }
2825  if (LocaleCompare("density",keyword) == 0)
2826  {
2827  GetNextToken(q,&q,extent,token);
2828  (void) CloneString(&graphic_context[n]->density,token);
2829  break;
2830  }
2831  if (LocaleCompare("direction",keyword) == 0)
2832  {
2833  ssize_t
2834  direction;
2835 
2836  GetNextToken(q,&q,extent,token);
2838  token);
2839  if (direction == -1)
2840  status=MagickFalse;
2841  else
2842  graphic_context[n]->direction=(DirectionType) direction;
2843  break;
2844  }
2845  status=MagickFalse;
2846  break;
2847  }
2848  case 'e':
2849  case 'E':
2850  {
2851  if (LocaleCompare("ellipse",keyword) == 0)
2852  {
2853  primitive_type=EllipsePrimitive;
2854  break;
2855  }
2856  if (LocaleCompare("encoding",keyword) == 0)
2857  {
2858  GetNextToken(q,&q,extent,token);
2859  (void) CloneString(&graphic_context[n]->encoding,token);
2860  break;
2861  }
2862  status=MagickFalse;
2863  break;
2864  }
2865  case 'f':
2866  case 'F':
2867  {
2868  if (LocaleCompare("fill",keyword) == 0)
2869  {
2870  GetNextToken(q,&q,extent,token);
2871  if (graphic_context[n]->clip_path != MagickFalse)
2872  break;
2873  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2874  if (GetImageArtifact(image,pattern) != (const char *) NULL)
2875  (void) DrawPatternPath(image,draw_info,token,
2876  &graphic_context[n]->fill_pattern,exception);
2877  else
2878  {
2879  status&=QueryColorCompliance(token,AllCompliance,
2880  &graphic_context[n]->fill,exception);
2881  if (graphic_context[n]->fill_alpha != OpaqueAlpha)
2882  graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2883  }
2884  break;
2885  }
2886  if (LocaleCompare("fill-opacity",keyword) == 0)
2887  {
2888  double
2889  opacity;
2890 
2891  GetNextToken(q,&q,extent,token);
2892  if (graphic_context[n]->clip_path != MagickFalse)
2893  break;
2894  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2895  opacity=MagickMin(MagickMax(factor*
2896  StringToDouble(token,&next_token),0.0),1.0);
2897  if (token == next_token)
2898  ThrowPointExpectedException(token,exception);
2899  graphic_context[n]->fill_alpha*=opacity;
2900  if (graphic_context[n]->fill.alpha != TransparentAlpha)
2901  graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2902  else
2903  graphic_context[n]->fill.alpha=(MagickRealType)
2904  ClampToQuantum(QuantumRange*(1.0-opacity));
2905  break;
2906  }
2907  if (LocaleCompare("fill-rule",keyword) == 0)
2908  {
2909  ssize_t
2910  fill_rule;
2911 
2912  GetNextToken(q,&q,extent,token);
2914  token);
2915  if (fill_rule == -1)
2916  {
2917  status=MagickFalse;
2918  break;
2919  }
2920  graphic_context[n]->fill_rule=(FillRule) fill_rule;
2921  break;
2922  }
2923  if (LocaleCompare("font",keyword) == 0)
2924  {
2925  GetNextToken(q,&q,extent,token);
2926  (void) CloneString(&graphic_context[n]->font,token);
2927  if (LocaleCompare("none",token) == 0)
2928  graphic_context[n]->font=(char *) RelinquishMagickMemory(
2929  graphic_context[n]->font);
2930  break;
2931  }
2932  if (LocaleCompare("font-family",keyword) == 0)
2933  {
2934  GetNextToken(q,&q,extent,token);
2935  (void) CloneString(&graphic_context[n]->family,token);
2936  break;
2937  }
2938  if (LocaleCompare("font-size",keyword) == 0)
2939  {
2940  GetNextToken(q,&q,extent,token);
2941  graphic_context[n]->pointsize=StringToDouble(token,&next_token);
2942  if (token == next_token)
2943  ThrowPointExpectedException(token,exception);
2944  break;
2945  }
2946  if (LocaleCompare("font-stretch",keyword) == 0)
2947  {
2948  ssize_t
2949  stretch;
2950 
2951  GetNextToken(q,&q,extent,token);
2953  if (stretch == -1)
2954  {
2955  status=MagickFalse;
2956  break;
2957  }
2958  graphic_context[n]->stretch=(StretchType) stretch;
2959  break;
2960  }
2961  if (LocaleCompare("font-style",keyword) == 0)
2962  {
2963  ssize_t
2964  style;
2965 
2966  GetNextToken(q,&q,extent,token);
2968  if (style == -1)
2969  {
2970  status=MagickFalse;
2971  break;
2972  }
2973  graphic_context[n]->style=(StyleType) style;
2974  break;
2975  }
2976  if (LocaleCompare("font-weight",keyword) == 0)
2977  {
2978  ssize_t
2979  weight;
2980 
2981  GetNextToken(q,&q,extent,token);
2983  if (weight == -1)
2984  weight=(ssize_t) StringToUnsignedLong(token);
2985  graphic_context[n]->weight=(size_t) weight;
2986  break;
2987  }
2988  status=MagickFalse;
2989  break;
2990  }
2991  case 'g':
2992  case 'G':
2993  {
2994  if (LocaleCompare("gradient-units",keyword) == 0)
2995  {
2996  GetNextToken(q,&q,extent,token);
2997  break;
2998  }
2999  if (LocaleCompare("gravity",keyword) == 0)
3000  {
3001  ssize_t
3002  gravity;
3003 
3004  GetNextToken(q,&q,extent,token);
3006  if (gravity == -1)
3007  {
3008  status=MagickFalse;
3009  break;
3010  }
3011  graphic_context[n]->gravity=(GravityType) gravity;
3012  break;
3013  }
3014  status=MagickFalse;
3015  break;
3016  }
3017  case 'i':
3018  case 'I':
3019  {
3020  if (LocaleCompare("image",keyword) == 0)
3021  {
3022  ssize_t
3023  compose;
3024 
3025  primitive_type=ImagePrimitive;
3026  GetNextToken(q,&q,extent,token);
3028  if (compose == -1)
3029  {
3030  status=MagickFalse;
3031  break;
3032  }
3033  graphic_context[n]->compose=(CompositeOperator) compose;
3034  break;
3035  }
3036  if (LocaleCompare("interline-spacing",keyword) == 0)
3037  {
3038  GetNextToken(q,&q,extent,token);
3039  graphic_context[n]->interline_spacing=StringToDouble(token,
3040  &next_token);
3041  if (token == next_token)
3042  ThrowPointExpectedException(token,exception);
3043  break;
3044  }
3045  if (LocaleCompare("interword-spacing",keyword) == 0)
3046  {
3047  GetNextToken(q,&q,extent,token);
3048  graphic_context[n]->interword_spacing=StringToDouble(token,
3049  &next_token);
3050  if (token == next_token)
3051  ThrowPointExpectedException(token,exception);
3052  break;
3053  }
3054  status=MagickFalse;
3055  break;
3056  }
3057  case 'k':
3058  case 'K':
3059  {
3060  if (LocaleCompare("kerning",keyword) == 0)
3061  {
3062  GetNextToken(q,&q,extent,token);
3063  graphic_context[n]->kerning=StringToDouble(token,&next_token);
3064  if (token == next_token)
3065  ThrowPointExpectedException(token,exception);
3066  break;
3067  }
3068  status=MagickFalse;
3069  break;
3070  }
3071  case 'l':
3072  case 'L':
3073  {
3074  if (LocaleCompare("letter-spacing",keyword) == 0)
3075  {
3076  GetNextToken(q,&q,extent,token);
3077  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3078  clone_info->text=AcquireString(" ");
3079  status&=GetTypeMetrics(image,clone_info,&metrics,exception);
3080  graphic_context[n]->kerning=metrics.width*
3081  StringToDouble(token,&next_token);
3082  clone_info=DestroyDrawInfo(clone_info);
3083  if (token == next_token)
3084  ThrowPointExpectedException(token,exception);
3085  break;
3086  }
3087  if (LocaleCompare("line",keyword) == 0)
3088  {
3089  primitive_type=LinePrimitive;
3090  break;
3091  }
3092  status=MagickFalse;
3093  break;
3094  }
3095  case 'm':
3096  case 'M':
3097  {
3098  if (LocaleCompare("mask",keyword) == 0)
3099  {
3100  const char
3101  *mask_path;
3102 
3103  /*
3104  Take a node from within the MVG document, and duplicate it here.
3105  */
3106  GetNextToken(q,&q,extent,token);
3107  mask_path=(const char *) GetValueFromSplayTree(macros,token);
3108  if (mask_path != (const char *) NULL)
3109  {
3110  if (graphic_context[n]->composite_mask != (Image *) NULL)
3111  graphic_context[n]->composite_mask=
3112  DestroyImage(graphic_context[n]->composite_mask);
3113  graphic_context[n]->composite_mask=DrawCompositeMask(image,
3114  graphic_context[n],token,mask_path,exception);
3115  if (draw_info->compliance != SVGCompliance)
3116  status=SetImageMask(image,CompositePixelMask,
3117  graphic_context[n]->composite_mask,exception);
3118  }
3119  break;
3120  }
3121  break;
3122  }
3123  case 'o':
3124  case 'O':
3125  {
3126  if (LocaleCompare("offset",keyword) == 0)
3127  {
3128  GetNextToken(q,&q,extent,token);
3129  break;
3130  }
3131  if (LocaleCompare("opacity",keyword) == 0)
3132  {
3133  double
3134  opacity;
3135 
3136  GetNextToken(q,&q,extent,token);
3137  if (graphic_context[n]->clip_path != MagickFalse)
3138  break;
3139  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3140  opacity=MagickMin(MagickMax(factor*
3141  StringToDouble(token,&next_token),0.0),1.0);
3142  if (token == next_token)
3143  ThrowPointExpectedException(token,exception);
3144  graphic_context[n]->fill_alpha*=opacity;
3145  graphic_context[n]->stroke_alpha*=opacity;
3146  break;
3147  }
3148  status=MagickFalse;
3149  break;
3150  }
3151  case 'p':
3152  case 'P':
3153  {
3154  if (LocaleCompare("path",keyword) == 0)
3155  {
3156  primitive_type=PathPrimitive;
3157  break;
3158  }
3159  if (LocaleCompare("point",keyword) == 0)
3160  {
3161  primitive_type=PointPrimitive;
3162  break;
3163  }
3164  if (LocaleCompare("polyline",keyword) == 0)
3165  {
3166  primitive_type=PolylinePrimitive;
3167  break;
3168  }
3169  if (LocaleCompare("polygon",keyword) == 0)
3170  {
3171  primitive_type=PolygonPrimitive;
3172  break;
3173  }
3174  if (LocaleCompare("pop",keyword) == 0)
3175  {
3176  GetNextToken(q,&q,extent,token);
3177  if (LocaleCompare("class",token) == 0)
3178  break;
3179  if (LocaleCompare("clip-path",token) == 0)
3180  break;
3181  if (LocaleCompare("defs",token) == 0)
3182  {
3183  defsDepth--;
3184  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3185  MagickTrue;
3186  break;
3187  }
3188  if (LocaleCompare("gradient",token) == 0)
3189  break;
3190  if (LocaleCompare("graphic-context",token) == 0)
3191  {
3192  if (n <= 0)
3193  {
3194  (void) ThrowMagickException(exception,GetMagickModule(),
3195  DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
3196  status=MagickFalse;
3197  n=0;
3198  break;
3199  }
3200  if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3201  (draw_info->compliance != SVGCompliance))
3202  if (LocaleCompare(graphic_context[n]->clip_mask,
3203  graphic_context[n-1]->clip_mask) != 0)
3204  status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3205  exception);
3206  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3207  n--;
3208  break;
3209  }
3210  if (LocaleCompare("mask",token) == 0)
3211  break;
3212  if (LocaleCompare("pattern",token) == 0)
3213  break;
3214  if (LocaleCompare("symbol",token) == 0)
3215  {
3216  symbolDepth--;
3217  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3218  MagickTrue;
3219  break;
3220  }
3221  status=MagickFalse;
3222  break;
3223  }
3224  if (LocaleCompare("push",keyword) == 0)
3225  {
3226  GetNextToken(q,&q,extent,token);
3227  if (LocaleCompare("class",token) == 0)
3228  {
3229  /*
3230  Class context.
3231  */
3232  for (p=q; *q != '\0'; )
3233  {
3234  GetNextToken(q,&q,extent,token);
3235  if (LocaleCompare(token,"pop") != 0)
3236  continue;
3237  GetNextToken(q,(const char **) NULL,extent,token);
3238  if (LocaleCompare(token,"class") != 0)
3239  continue;
3240  break;
3241  }
3242  GetNextToken(q,&q,extent,token);
3243  break;
3244  }
3245  if (LocaleCompare("clip-path",token) == 0)
3246  {
3247  GetNextToken(q,&q,extent,token);
3248  for (p=q; *q != '\0'; )
3249  {
3250  GetNextToken(q,&q,extent,token);
3251  if (LocaleCompare(token,"pop") != 0)
3252  continue;
3253  GetNextToken(q,(const char **) NULL,extent,token);
3254  if (LocaleCompare(token,"clip-path") != 0)
3255  continue;
3256  break;
3257  }
3258  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3259  {
3260  status=MagickFalse;
3261  break;
3262  }
3263  GetNextToken(q,&q,extent,token);
3264  break;
3265  }
3266  if (LocaleCompare("defs",token) == 0)
3267  {
3268  defsDepth++;
3269  graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3270  MagickTrue;
3271  break;
3272  }
3273  if (LocaleCompare("gradient",token) == 0)
3274  {
3275  char
3276  key[2*MagickPathExtent],
3277  name[MagickPathExtent],
3278  type[MagickPathExtent];
3279 
3280  SegmentInfo
3281  segment;
3282 
3283  GetNextToken(q,&q,extent,token);
3284  (void) CopyMagickString(name,token,MagickPathExtent);
3285  GetNextToken(q,&q,extent,token);
3286  (void) CopyMagickString(type,token,MagickPathExtent);
3287  GetNextToken(q,&q,extent,token);
3288  segment.x1=StringToDouble(token,&next_token);
3289  if (token == next_token)
3290  ThrowPointExpectedException(token,exception);
3291  GetNextToken(q,&q,extent,token);
3292  if (*token == ',')
3293  GetNextToken(q,&q,extent,token);
3294  segment.y1=StringToDouble(token,&next_token);
3295  if (token == next_token)
3296  ThrowPointExpectedException(token,exception);
3297  GetNextToken(q,&q,extent,token);
3298  if (*token == ',')
3299  GetNextToken(q,&q,extent,token);
3300  segment.x2=StringToDouble(token,&next_token);
3301  if (token == next_token)
3302  ThrowPointExpectedException(token,exception);
3303  GetNextToken(q,&q,extent,token);
3304  if (*token == ',')
3305  GetNextToken(q,&q,extent,token);
3306  segment.y2=StringToDouble(token,&next_token);
3307  if (token == next_token)
3308  ThrowPointExpectedException(token,exception);
3309  if (LocaleCompare(type,"radial") == 0)
3310  {
3311  GetNextToken(q,&q,extent,token);
3312  if (*token == ',')
3313  GetNextToken(q,&q,extent,token);
3314  }
3315  for (p=q; *q != '\0'; )
3316  {
3317  GetNextToken(q,&q,extent,token);
3318  if (LocaleCompare(token,"pop") != 0)
3319  continue;
3320  GetNextToken(q,(const char **) NULL,extent,token);
3321  if (LocaleCompare(token,"gradient") != 0)
3322  continue;
3323  break;
3324  }
3325  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3326  {
3327  status=MagickFalse;
3328  break;
3329  }
3330  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3331  bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3332  graphic_context[n]->affine.ry*segment.y1+
3333  graphic_context[n]->affine.tx;
3334  bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3335  graphic_context[n]->affine.sy*segment.y1+
3336  graphic_context[n]->affine.ty;
3337  bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3338  graphic_context[n]->affine.ry*segment.y2+
3339  graphic_context[n]->affine.tx;
3340  bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3341  graphic_context[n]->affine.sy*segment.y2+
3342  graphic_context[n]->affine.ty;
3343  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3344  (void) SetImageArtifact(image,key,token);
3345  (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
3346  (void) SetImageArtifact(image,key,type);
3347  (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3348  name);
3349  (void) FormatLocaleString(geometry,MagickPathExtent,
3350  "%gx%g%+.15g%+.15g",
3351  MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3352  MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3353  bounds.x1,bounds.y1);
3354  (void) SetImageArtifact(image,key,geometry);
3355  GetNextToken(q,&q,extent,token);
3356  break;
3357  }
3358  if (LocaleCompare("graphic-context",token) == 0)
3359  {
3360  n++;
3361  graphic_context=(DrawInfo **) ResizeQuantumMemory(
3362  graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3363  if (graphic_context == (DrawInfo **) NULL)
3364  {
3365  (void) ThrowMagickException(exception,GetMagickModule(),
3366  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3367  image->filename);
3368  break;
3369  }
3370  graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3371  graphic_context[n-1]);
3372  if (*q == '"')
3373  GetNextToken(q,&q,extent,token);
3374  break;
3375  }
3376  if (LocaleCompare("mask",token) == 0)
3377  {
3378  GetNextToken(q,&q,extent,token);
3379  break;
3380  }
3381  if (LocaleCompare("pattern",token) == 0)
3382  {
3383  char
3384  key[2*MagickPathExtent],
3385  name[MagickPathExtent];
3386 
3388  bounds;
3389 
3390  GetNextToken(q,&q,extent,token);
3391  (void) CopyMagickString(name,token,MagickPathExtent);
3392  GetNextToken(q,&q,extent,token);
3393  bounds.x=(ssize_t) ceil(StringToDouble(token,&next_token)-0.5);
3394  if (token == next_token)
3395  ThrowPointExpectedException(token,exception);
3396  GetNextToken(q,&q,extent,token);
3397  if (*token == ',')
3398  GetNextToken(q,&q,extent,token);
3399  bounds.y=(ssize_t) ceil(StringToDouble(token,&next_token)-0.5);
3400  if (token == next_token)
3401  ThrowPointExpectedException(token,exception);
3402  GetNextToken(q,&q,extent,token);
3403  if (*token == ',')
3404  GetNextToken(q,&q,extent,token);
3405  bounds.width=(size_t) floor(StringToDouble(token,&next_token)+
3406  0.5);
3407  if (token == next_token)
3408  ThrowPointExpectedException(token,exception);
3409  GetNextToken(q,&q,extent,token);
3410  if (*token == ',')
3411  GetNextToken(q,&q,extent,token);
3412  bounds.height=(size_t) floor(StringToDouble(token,&next_token)+
3413  0.5);
3414  if (token == next_token)
3415  ThrowPointExpectedException(token,exception);
3416  for (p=q; *q != '\0'; )
3417  {
3418  GetNextToken(q,&q,extent,token);
3419  if (LocaleCompare(token,"pop") != 0)
3420  continue;
3421  GetNextToken(q,(const char **) NULL,extent,token);
3422  if (LocaleCompare(token,"pattern") != 0)
3423  continue;
3424  break;
3425  }
3426  if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3427  {
3428  status=MagickFalse;
3429  break;
3430  }
3431  (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3432  (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3433  (void) SetImageArtifact(image,key,token);
3434  (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3435  name);
3436  (void) FormatLocaleString(geometry,MagickPathExtent,
3437  "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
3438  bounds.height,(double) bounds.x,(double) bounds.y);
3439  (void) SetImageArtifact(image,key,geometry);
3440  GetNextToken(q,&q,extent,token);
3441  break;
3442  }
3443  if (LocaleCompare("symbol",token) == 0)
3444  {
3445  symbolDepth++;
3446  graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3447  MagickTrue;
3448  break;
3449  }
3450  status=MagickFalse;
3451  break;
3452  }
3453  status=MagickFalse;
3454  break;
3455  }
3456  case 'r':
3457  case 'R':
3458  {
3459  if (LocaleCompare("rectangle",keyword) == 0)
3460  {
3461  primitive_type=RectanglePrimitive;
3462  break;
3463  }
3464  if (LocaleCompare("rotate",keyword) == 0)
3465  {
3466  GetNextToken(q,&q,extent,token);
3467  angle=StringToDouble(token,&next_token);
3468  if (token == next_token)
3469  ThrowPointExpectedException(token,exception);
3470  affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3471  affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3472  affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3473  affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3474  break;
3475  }
3476  if (LocaleCompare("roundRectangle",keyword) == 0)
3477  {
3478  primitive_type=RoundRectanglePrimitive;
3479  break;
3480  }
3481  status=MagickFalse;
3482  break;
3483  }
3484  case 's':
3485  case 'S':
3486  {
3487  if (LocaleCompare("scale",keyword) == 0)
3488  {
3489  GetNextToken(q,&q,extent,token);
3490  affine.sx=StringToDouble(token,&next_token);
3491  if (token == next_token)
3492  ThrowPointExpectedException(token,exception);
3493  GetNextToken(q,&q,extent,token);
3494  if (*token == ',')
3495  GetNextToken(q,&q,extent,token);
3496  affine.sy=StringToDouble(token,&next_token);
3497  if (token == next_token)
3498  ThrowPointExpectedException(token,exception);
3499  break;
3500  }
3501  if (LocaleCompare("skewX",keyword) == 0)
3502  {
3503  GetNextToken(q,&q,extent,token);
3504  angle=StringToDouble(token,&next_token);
3505  if (token == next_token)
3506  ThrowPointExpectedException(token,exception);
3507  affine.ry=sin(DegreesToRadians(angle));
3508  break;
3509  }
3510  if (LocaleCompare("skewY",keyword) == 0)
3511  {
3512  GetNextToken(q,&q,extent,token);
3513  angle=StringToDouble(token,&next_token);
3514  if (token == next_token)
3515  ThrowPointExpectedException(token,exception);
3516  affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3517  break;
3518  }
3519  if (LocaleCompare("stop-color",keyword) == 0)
3520  {
3521  PixelInfo
3522  stop_color;
3523 
3524  number_stops++;
3525  if (number_stops == 1)
3526  stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
3527  else
3528  if (number_stops > 2)
3529  stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3530  sizeof(*stops));
3531  if (stops == (StopInfo *) NULL)
3532  {
3533  (void) ThrowMagickException(exception,GetMagickModule(),
3534  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3535  image->filename);
3536  break;
3537  }
3538  GetNextToken(q,&q,extent,token);
3539  status&=QueryColorCompliance(token,AllCompliance,&stop_color,
3540  exception);
3541  stops[number_stops-1].color=stop_color;
3542  GetNextToken(q,&q,extent,token);
3543  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3544  stops[number_stops-1].offset=factor*StringToDouble(token,
3545  &next_token);
3546  if (token == next_token)
3547  ThrowPointExpectedException(token,exception);
3548  break;
3549  }
3550  if (LocaleCompare("stroke",keyword) == 0)
3551  {
3552  GetNextToken(q,&q,extent,token);
3553  if (graphic_context[n]->clip_path != MagickFalse)
3554  break;
3555  (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
3556  if (GetImageArtifact(image,pattern) != (const char *) NULL)
3557  (void) DrawPatternPath(image,draw_info,token,
3558  &graphic_context[n]->stroke_pattern,exception);
3559  else
3560  {
3561  status&=QueryColorCompliance(token,AllCompliance,
3562  &graphic_context[n]->stroke,exception);
3563  if (graphic_context[n]->stroke_alpha != OpaqueAlpha)
3564  graphic_context[n]->stroke.alpha=
3565  graphic_context[n]->stroke_alpha;
3566  }
3567  break;
3568  }
3569  if (LocaleCompare("stroke-antialias",keyword) == 0)
3570  {
3571  GetNextToken(q,&q,extent,token);
3572  graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3574  break;
3575  }
3576  if (LocaleCompare("stroke-dasharray",keyword) == 0)
3577  {
3578  if (graphic_context[n]->dash_pattern != (double *) NULL)
3579  graphic_context[n]->dash_pattern=(double *)
3580  RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3581  if (IsPoint(q) != MagickFalse)
3582  {
3583  const char
3584  *r;
3585 
3586  r=q;
3587  GetNextToken(r,&r,extent,token);
3588  if (*token == ',')
3589  GetNextToken(r,&r,extent,token);
3590  for (x=0; IsPoint(token) != MagickFalse; x++)
3591  {
3592  GetNextToken(r,&r,extent,token);
3593  if (*token == ',')
3594  GetNextToken(r,&r,extent,token);
3595  }
3596  graphic_context[n]->dash_pattern=(double *)
3597  AcquireQuantumMemory((size_t) (2*x+2),
3598  sizeof(*graphic_context[n]->dash_pattern));
3599  if (graphic_context[n]->dash_pattern == (double *) NULL)
3600  {
3601  (void) ThrowMagickException(exception,GetMagickModule(),
3602  ResourceLimitError,"MemoryAllocationFailed","`%s'",
3603  image->filename);
3604  status=MagickFalse;
3605  break;
3606  }
3607  (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3608  (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3609  for (j=0; j < x; j++)
3610  {
3611  GetNextToken(q,&q,extent,token);
3612  if (*token == ',')
3613  GetNextToken(q,&q,extent,token);
3614  graphic_context[n]->dash_pattern[j]=StringToDouble(token,
3615  &next_token);
3616  if (token == next_token)
3617  ThrowPointExpectedException(token,exception);
3618  if (graphic_context[n]->dash_pattern[j] < 0.0)
3619  status=MagickFalse;
3620  }
3621  if ((x & 0x01) != 0)
3622  for ( ; j < (2*x); j++)
3623  graphic_context[n]->dash_pattern[j]=
3624  graphic_context[n]->dash_pattern[j-x];
3625  graphic_context[n]->dash_pattern[j]=0.0;
3626  break;
3627  }
3628  GetNextToken(q,&q,extent,token);
3629  break;
3630  }
3631  if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3632  {
3633  GetNextToken(q,&q,extent,token);
3634  graphic_context[n]->dash_offset=StringToDouble(token,&next_token);
3635  if (token == next_token)
3636  ThrowPointExpectedException(token,exception);
3637  break;
3638  }
3639  if (LocaleCompare("stroke-linecap",keyword) == 0)
3640  {
3641  ssize_t
3642  linecap;
3643 
3644  GetNextToken(q,&q,extent,token);
3646  if (linecap == -1)
3647  {
3648  status=MagickFalse;
3649  break;
3650  }
3651  graphic_context[n]->linecap=(LineCap) linecap;
3652  break;
3653  }
3654  if (LocaleCompare("stroke-linejoin",keyword) == 0)
3655  {
3656  ssize_t
3657  linejoin;
3658 
3659  GetNextToken(q,&q,extent,token);
3661  token);
3662  if (linejoin == -1)
3663  {
3664  status=MagickFalse;
3665  break;
3666  }
3667  graphic_context[n]->linejoin=(LineJoin) linejoin;
3668  break;
3669  }
3670  if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3671  {
3672  GetNextToken(q,&q,extent,token);
3673  graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3674  break;
3675  }
3676  if (LocaleCompare("stroke-opacity",keyword) == 0)
3677  {
3678  double
3679  opacity;
3680 
3681  GetNextToken(q,&q,extent,token);
3682  if (graphic_context[n]->clip_path != MagickFalse)
3683  break;
3684  factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3685  opacity=MagickMin(MagickMax(factor*
3686  StringToDouble(token,&next_token),0.0),1.0);
3687  if (token == next_token)
3688  ThrowPointExpectedException(token,exception);
3689  graphic_context[n]->stroke_alpha*=opacity;
3690  if (graphic_context[n]->stroke.alpha != TransparentAlpha)
3691  graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3692  else
3693  graphic_context[n]->stroke.alpha=(MagickRealType)
3694  ClampToQuantum(QuantumRange*(1.0-opacity));
3695  break;
3696  }
3697  if (LocaleCompare("stroke-width",keyword) == 0)
3698  {
3699  GetNextToken(q,&q,extent,token);
3700  if (graphic_context[n]->clip_path != MagickFalse)
3701  break;
3702  graphic_context[n]->stroke_width=StringToDouble(token,&next_token);
3703  if (token == next_token)
3704  ThrowPointExpectedException(token,exception);
3705  break;
3706  }
3707  status=MagickFalse;
3708  break;
3709  }
3710  case 't':
3711  case 'T':
3712  {
3713  if (LocaleCompare("text",keyword) == 0)
3714  {
3715  primitive_type=TextPrimitive;
3716  cursor=0.0;
3717  break;
3718  }
3719  if (LocaleCompare("text-align",keyword) == 0)
3720  {
3721  ssize_t
3722  align;
3723 
3724  GetNextToken(q,&q,extent,token);
3726  if (align == -1)
3727  {
3728  status=MagickFalse;
3729  break;
3730  }
3731  graphic_context[n]->align=(AlignType) align;
3732  break;
3733  }
3734  if (LocaleCompare("text-anchor",keyword) == 0)
3735  {
3736  ssize_t
3737  align;
3738 
3739  GetNextToken(q,&q,extent,token);
3741  if (align == -1)
3742  {
3743  status=MagickFalse;
3744  break;
3745  }
3746  graphic_context[n]->align=(AlignType) align;
3747  break;
3748  }
3749  if (LocaleCompare("text-antialias",keyword) == 0)
3750  {
3751  GetNextToken(q,&q,extent,token);
3752  graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3754  break;
3755  }
3756  if (LocaleCompare("text-undercolor",keyword) == 0)
3757  {
3758  GetNextToken(q,&q,extent,token);
3759  status&=QueryColorCompliance(token,AllCompliance,
3760  &graphic_context[n]->undercolor,exception);
3761  break;
3762  }
3763  if (LocaleCompare("translate",keyword) == 0)
3764  {
3765  GetNextToken(q,&q,extent,token);
3766  affine.tx=StringToDouble(token,&next_token);
3767  if (token == next_token)
3768  ThrowPointExpectedException(token,exception);
3769  GetNextToken(q,&q,extent,token);
3770  if (*token == ',')
3771  GetNextToken(q,&q,extent,token);
3772  affine.ty=StringToDouble(token,&next_token);
3773  if (token == next_token)
3774  ThrowPointExpectedException(token,exception);
3775  cursor=0.0;
3776  break;
3777  }
3778  status=MagickFalse;
3779  break;
3780  }
3781  case 'u':
3782  case 'U':
3783  {
3784  if (LocaleCompare("use",keyword) == 0)
3785  {
3786  const char
3787  *use;
3788 
3789  /*
3790  Get a macro from the MVG document, and "use" it here.
3791  */
3792  GetNextToken(q,&q,extent,token);
3793  use=(const char *) GetValueFromSplayTree(macros,token);
3794  if (use != (const char *) NULL)
3795  {
3796  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3797  (void) CloneString(&clone_info->primitive,use);
3798  status=RenderMVGContent(image,clone_info,depth+1,exception);
3799  clone_info=DestroyDrawInfo(clone_info);
3800  }
3801  break;
3802  }
3803  break;
3804  }
3805  case 'v':
3806  case 'V':
3807  {
3808  if (LocaleCompare("viewbox",keyword) == 0)
3809  {
3810  GetNextToken(q,&q,extent,token);
3811  graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
3812  &next_token)-0.5);
3813  if (token == next_token)
3814  ThrowPointExpectedException(token,exception);
3815  GetNextToken(q,&q,extent,token);
3816  if (*token == ',')
3817  GetNextToken(q,&q,extent,token);
3818  graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
3819  &next_token)-0.5);
3820  if (token == next_token)
3821  ThrowPointExpectedException(token,exception);
3822  GetNextToken(q,&q,extent,token);
3823  if (*token == ',')
3824  GetNextToken(q,&q,extent,token);
3825  graphic_context[n]->viewbox.width=(size_t) floor(StringToDouble(
3826  token,&next_token)+0.5);
3827  if (token == next_token)
3828  ThrowPointExpectedException(token,exception);
3829  GetNextToken(q,&q,extent,token);
3830  if (*token == ',')
3831  GetNextToken(q,&q,extent,token);
3832  graphic_context[n]->viewbox.height=(size_t) floor(StringToDouble(
3833  token,&next_token)+0.5);
3834  if (token == next_token)
3835  ThrowPointExpectedException(token,exception);
3836  break;
3837  }
3838  status=MagickFalse;
3839  break;
3840  }
3841  case 'w':
3842  case 'W':
3843  {
3844  if (LocaleCompare("word-spacing",keyword) == 0)
3845  {
3846  GetNextToken(q,&q,extent,token);
3847  graphic_context[n]->interword_spacing=StringToDouble(token,
3848  &next_token);
3849  if (token == next_token)
3850  ThrowPointExpectedException(token,exception);
3851  break;
3852  }
3853  status=MagickFalse;
3854  break;
3855  }
3856  default:
3857  {
3858  status=MagickFalse;
3859  break;
3860  }
3861  }
3862  if (status == MagickFalse)
3863  break;
3864  if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
3865  (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
3866  (fabs(affine.sy-1.0) >= MagickEpsilon) ||
3867  (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
3868  {
3869  graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
3870  graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
3871  graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
3872  graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
3873  graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
3874  current.tx;
3875  graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
3876  current.ty;
3877  }
3878  if (primitive_type == UndefinedPrimitive)
3879  {
3880  if (*q == '\0')
3881  {
3882  if (number_stops > 1)
3883  {
3884  GradientType
3885  type;
3886 
3887  type=LinearGradient;
3888  if (draw_info->gradient.type == RadialGradient)
3889  type=RadialGradient;
3890  (void) GradientImage(image,type,PadSpread,stops,number_stops,
3891  exception);
3892  }
3893  if (number_stops > 0)
3894  stops=(StopInfo *) RelinquishMagickMemory(stops);
3895  }
3896  if ((image->debug != MagickFalse) && (q > p))
3897  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
3898  (q-p-1),p);
3899  continue;
3900  }
3901  /*
3902  Parse the primitive attributes.
3903  */
3904  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
3905  if ((primitive_info[i].primitive == TextPrimitive) ||
3906  (primitive_info[i].primitive == ImagePrimitive))
3907  if (primitive_info[i].text != (char *) NULL)
3908  primitive_info[i].text=DestroyString(primitive_info[i].text);
3909  i=0;
3910  mvg_info.offset=i;
3911  j=0;
3912  primitive_info[0].point.x=0.0;
3913  primitive_info[0].point.y=0.0;
3914  primitive_info[0].coordinates=0;
3915  primitive_info[0].method=FloodfillMethod;
3916  primitive_info[0].closed_subpath=MagickFalse;
3917  for (x=0; *q != '\0'; x++)
3918  {
3919  /*
3920  Define points.
3921  */
3922  if (IsPoint(q) == MagickFalse)
3923  break;
3924  GetNextToken(q,&q,extent,token);
3925  point.x=StringToDouble(token,&next_token);
3926  if (token == next_token)
3927  ThrowPointExpectedException(token,exception);
3928  GetNextToken(q,&q,extent,token);
3929  if (*token == ',')
3930  GetNextToken(q,&q,extent,token);
3931  point.y=StringToDouble(token,&next_token);
3932  if (token == next_token)
3933  ThrowPointExpectedException(token,exception);
3934  GetNextToken(q,(const char **) NULL,extent,token);
3935  if (*token == ',')
3936  GetNextToken(q,&q,extent,token);
3937  primitive_info[i].primitive=primitive_type;
3938  primitive_info[i].point=point;
3939  primitive_info[i].coordinates=0;
3940  primitive_info[i].method=FloodfillMethod;
3941  primitive_info[i].closed_subpath=MagickFalse;
3942  i++;
3943  mvg_info.offset=i;
3944  if (i < (ssize_t) number_points)
3945  continue;
3946  status&=CheckPrimitiveExtent(&mvg_info,number_points);
3947  }
3948  if (status == MagickFalse)
3949  break;
3950  if ((primitive_info[j].primitive == TextPrimitive) ||
3951  (primitive_info[j].primitive == ImagePrimitive))
3952  if (primitive_info[j].text != (char *) NULL)
3953  primitive_info[j].text=DestroyString(primitive_info[j].text);
3954  primitive_info[j].primitive=primitive_type;
3955  primitive_info[j].coordinates=(size_t) x;
3956  primitive_info[j].method=FloodfillMethod;
3957  primitive_info[j].closed_subpath=MagickFalse;
3958  /*
3959  Circumscribe primitive within a circle.
3960  */
3961  bounds.x1=primitive_info[j].point.x;
3962  bounds.y1=primitive_info[j].point.y;
3963  bounds.x2=primitive_info[j].point.x;
3964  bounds.y2=primitive_info[j].point.y;
3965  for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
3966  {
3967  point=primitive_info[j+k].point;
3968  if (point.x < bounds.x1)
3969  bounds.x1=point.x;
3970  if (point.y < bounds.y1)
3971  bounds.y1=point.y;
3972  if (point.x > bounds.x2)
3973  bounds.x2=point.x;
3974  if (point.y > bounds.y2)
3975  bounds.y2=point.y;
3976  }
3977  /*
3978  Speculate how many points our primitive might consume.
3979  */
3980  coordinates=(double) primitive_info[j].coordinates;
3981  switch (primitive_type)
3982  {
3983  case RectanglePrimitive:
3984  {
3985  coordinates*=5.0;
3986  break;
3987  }
3989  {
3990  double
3991  alpha,
3992  beta,
3993  radius;
3994 
3995  alpha=bounds.x2-bounds.x1;
3996  beta=bounds.y2-bounds.y1;
3997  radius=hypot((double) alpha,(double) beta);
3998  coordinates*=5.0;
3999  coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4000  BezierQuantum+360.0;
4001  break;
4002  }
4003  case BezierPrimitive:
4004  {
4005  coordinates=(double) (BezierQuantum*primitive_info[j].coordinates);
4006  if (primitive_info[j].coordinates > (107*BezierQuantum))
4007  {
4008  (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
4009  "TooManyBezierCoordinates","`%s'",token);
4010  status=MagickFalse;
4011  break;
4012  }
4013  break;
4014  }
4015  case PathPrimitive:
4016  {
4017  char
4018  *s,
4019  *t;
4020 
4021  GetNextToken(q,&q,extent,token);
4022  coordinates=1.0;
4023  t=token;
4024  for (s=token; *s != '\0'; s=t)
4025  {
4026  double
4027  value;
4028 
4029  value=StringToDouble(s,&t);
4030  (void) value;
4031  if (s == t)
4032  {
4033  t++;
4034  continue;
4035  }
4036  coordinates++;
4037  }
4038  for (s=token; *s != '\0'; s++)
4039  if (strspn(s,"AaCcQqSsTt") != 0)
4040  coordinates+=(20.0*BezierQuantum)+360.0;
4041  break;
4042  }
4043  case CirclePrimitive:
4044  case ArcPrimitive:
4045  case EllipsePrimitive:
4046  {
4047  double
4048  alpha,
4049  beta,
4050  radius;
4051 
4052  alpha=bounds.x2-bounds.x1;
4053  beta=bounds.y2-bounds.y1;
4054  radius=hypot(alpha,beta);
4055  coordinates=2.0*(ceil(MagickPI*radius))+6.0*BezierQuantum+360.0;
4056  break;
4057  }
4058  default:
4059  break;
4060  }
4061  if (coordinates > MaxBezierCoordinates)
4062  {
4063  (void) ThrowMagickException(exception,GetMagickModule(),
4064  ResourceLimitError,"TooManyBezierCoordinates","`%s'",token);
4065  status=MagickFalse;
4066  }
4067  if (status == MagickFalse)
4068  break;
4069  if (((size_t) (i+coordinates)) >= number_points)
4070  {
4071  /*
4072  Resize based on speculative points required by primitive.
4073  */
4074  number_points+=coordinates+1;
4075  if (number_points < (size_t) coordinates)
4076  {
4077  (void) ThrowMagickException(exception,GetMagickModule(),
4078  ResourceLimitError,"MemoryAllocationFailed","`%s'",
4079  image->filename);
4080  break;
4081  }
4082  mvg_info.offset=i;
4083  status&=CheckPrimitiveExtent(&mvg_info,number_points);
4084  }
4085  status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4086  if (status == MagickFalse)
4087  break;
4088  mvg_info.offset=j;
4089  switch (primitive_type)
4090  {
4091  case PointPrimitive:
4092  default:
4093  {
4094  if (primitive_info[j].coordinates != 1)
4095  {
4096  status=MagickFalse;
4097  break;
4098  }
4099  status&=TracePoint(primitive_info+j,primitive_info[j].point);
4100  i=(ssize_t) (j+primitive_info[j].coordinates);
4101  break;
4102  }
4103  case LinePrimitive:
4104  {
4105  if (primitive_info[j].coordinates != 2)
4106  {
4107  status=MagickFalse;
4108  break;
4109  }
4110  status&=TraceLine(primitive_info+j,primitive_info[j].point,
4111  primitive_info[j+1].point);
4112  i=(ssize_t) (j+primitive_info[j].coordinates);
4113  break;
4114  }
4115  case RectanglePrimitive:
4116  {
4117  if (primitive_info[j].coordinates != 2)
4118  {
4119  status=MagickFalse;
4120  break;
4121  }
4122  status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4123  primitive_info[j+1].point);
4124  i=(ssize_t) (j+primitive_info[j].coordinates);
4125  break;
4126  }
4128  {
4129  if (primitive_info[j].coordinates != 3)
4130  {
4131  status=MagickFalse;
4132  break;
4133  }
4134  if ((primitive_info[j+2].point.x < 0.0) ||
4135  (primitive_info[j+2].point.y < 0.0))
4136  {
4137  status=MagickFalse;
4138  break;
4139  }
4140  if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4141  {
4142  status=MagickFalse;
4143  break;
4144  }
4145  if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4146  {
4147  status=MagickFalse;
4148  break;
4149  }
4150  status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4151  primitive_info[j+1].point,primitive_info[j+2].point);
4152  i=(ssize_t) (j+primitive_info[j].coordinates);
4153  break;
4154  }
4155  case ArcPrimitive:
4156  {
4157  if (primitive_info[j].coordinates != 3)
4158  {
4159  primitive_type=UndefinedPrimitive;
4160  break;
4161  }
4162  status&=TraceArc(&mvg_info,primitive_info[j].point,
4163  primitive_info[j+1].point,primitive_info[j+2].point);
4164  i=(ssize_t) (j+primitive_info[j].coordinates);
4165  break;
4166  }
4167  case EllipsePrimitive:
4168  {
4169  if (primitive_info[j].coordinates != 3)
4170  {
4171  status=MagickFalse;
4172  break;
4173  }
4174  if ((primitive_info[j+1].point.x < 0.0) ||
4175  (primitive_info[j+1].point.y < 0.0))
4176  {
4177  status=MagickFalse;
4178  break;
4179  }
4180  status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4181  primitive_info[j+1].point,primitive_info[j+2].point);
4182  i=(ssize_t) (j+primitive_info[j].coordinates);
4183  break;
4184  }
4185  case CirclePrimitive:
4186  {
4187  if (primitive_info[j].coordinates != 2)
4188  {
4189  status=MagickFalse;
4190  break;
4191  }
4192  status&=TraceCircle(&mvg_info,primitive_info[j].point,
4193  primitive_info[j+1].point);
4194  i=(ssize_t) (j+primitive_info[j].coordinates);
4195  break;
4196  }
4197  case PolylinePrimitive:
4198  {
4199  if (primitive_info[j].coordinates < 1)
4200  {
4201  status=MagickFalse;
4202  break;
4203  }
4204  break;
4205  }
4206  case PolygonPrimitive:
4207  {
4208  if (primitive_info[j].coordinates < 3)
4209  {
4210  status=MagickFalse;
4211  break;
4212  }
4213  primitive_info[i]=primitive_info[j];
4214  primitive_info[i].coordinates=0;
4215  primitive_info[j].coordinates++;
4216  primitive_info[j].closed_subpath=MagickTrue;
4217  i++;
4218  break;
4219  }
4220  case BezierPrimitive:
4221  {
4222  if (primitive_info[j].coordinates < 3)
4223  {
4224  status=MagickFalse;
4225  break;
4226  }
4227  status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4228  i=(ssize_t) (j+primitive_info[j].coordinates);
4229  break;
4230  }
4231  case PathPrimitive:
4232  {
4233  coordinates=(double) TracePath(&mvg_info,token,exception);
4234  if (coordinates == 0.0)
4235  {
4236  status=MagickFalse;
4237  break;
4238  }
4239  i=(ssize_t) (j+coordinates);
4240  break;
4241  }
4242  case AlphaPrimitive:
4243  case ColorPrimitive:
4244  {
4245  ssize_t
4246  method;
4247 
4248  if (primitive_info[j].coordinates != 1)
4249  {
4250  status=MagickFalse;
4251  break;
4252  }
4253  GetNextToken(q,&q,extent,token);
4255  if (method == -1)
4256  {
4257  status=MagickFalse;
4258  break;
4259  }
4260  primitive_info[j].method=(PaintMethod) method;
4261  break;
4262  }
4263  case TextPrimitive:
4264  {
4265  char
4266  geometry[MagickPathExtent];
4267 
4268  if (primitive_info[j].coordinates != 1)
4269  {
4270  status=MagickFalse;
4271  break;
4272  }
4273  if (*token != ',')
4274  GetNextToken(q,&q,extent,token);
4275  (void) CloneString(&primitive_info[j].text,token);
4276  /*
4277  Compute text cursor offset.
4278  */
4279  clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4280  if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4281  (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4282  {
4283  mvg_info.point=primitive_info->point;
4284  primitive_info->point.x+=cursor;
4285  }
4286  else
4287  {
4288  mvg_info.point=primitive_info->point;
4289  cursor=0.0;
4290  }
4291  (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4292  primitive_info->point.x,primitive_info->point.y);
4293  clone_info->render=MagickFalse;
4294  clone_info->text=AcquireString(token);
4295  status&=GetTypeMetrics(image,clone_info,&metrics,exception);
4296  clone_info=DestroyDrawInfo(clone_info);
4297  cursor+=metrics.width;
4298  break;
4299  }
4300  case ImagePrimitive:
4301  {
4302  if (primitive_info[j].coordinates != 2)
4303  {
4304  status=MagickFalse;
4305  break;
4306  }
4307  GetNextToken(q,&q,extent,token);
4308  (void) CloneString(&primitive_info[j].text,token);
4309  break;
4310  }
4311  }
4312  mvg_info.offset=i;
4313  if ((image->debug != MagickFalse) && (q > p))
4314  (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p-1),
4315  p);
4316  if (status == MagickFalse)
4317  break;
4318  primitive_info[i].primitive=UndefinedPrimitive;
4319  if (i == 0)
4320  continue;
4321  /*
4322  Transform points.
4323  */
4324  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4325  {
4326  point=primitive_info[i].point;
4327  primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4328  graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4329  primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4330  graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4331  point=primitive_info[i].point;
4332  if (point.x < graphic_context[n]->bounds.x1)
4333  graphic_context[n]->bounds.x1=point.x;
4334  if (point.y < graphic_context[n]->bounds.y1)
4335  graphic_context[n]->bounds.y1=point.y;
4336  if (point.x > graphic_context[n]->bounds.x2)
4337  graphic_context[n]->bounds.x2=point.x;
4338  if (point.y > graphic_context[n]->bounds.y2)
4339  graphic_context[n]->bounds.y2=point.y;
4340  if (primitive_info[i].primitive == ImagePrimitive)
4341  break;
4342  if (i >= (ssize_t) number_points)
4343  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4344  }
4345  if (graphic_context[n]->render != MagickFalse)
4346  {
4347  if ((n != 0) && (draw_info->compliance != SVGCompliance) &&
4348  (graphic_context[n]->clip_mask != (char *) NULL) &&
4349  (LocaleCompare(graphic_context[n]->clip_mask,
4350  graphic_context[n-1]->clip_mask) != 0))
4351  {
4352  const char
4353  *clip_path;
4354 
4355  clip_path=(const char *) GetValueFromSplayTree(macros,
4356  graphic_context[n]->clip_mask);
4357  if (clip_path != (const char *) NULL)
4358  (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4359  clip_path);
4360  status&=DrawClipPath(image,graphic_context[n],
4361  graphic_context[n]->clip_mask,exception);
4362  }
4363  status&=DrawPrimitive(image,graphic_context[n],primitive_info,
4364  exception);
4365  }
4366  proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4367  primitive_extent);
4368  if (proceed == MagickFalse)
4369  break;
4370  if (status == 0)
4371  break;
4372  }
4373  if (image->debug != MagickFalse)
4374  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4375  /*
4376  Relinquish resources.
4377  */
4378  macros=DestroySplayTree(macros);
4379  token=DestroyString(token);
4380  if (primitive_info != (PrimitiveInfo *) NULL)
4381  {
4382  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4383  if ((primitive_info[i].primitive == TextPrimitive) ||
4384  (primitive_info[i].primitive == ImagePrimitive))
4385  if (primitive_info[i].text != (char *) NULL)
4386  primitive_info[i].text=DestroyString(primitive_info[i].text);
4387  primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4388  }
4389  primitive=DestroyString(primitive);
4390  if (stops != (StopInfo *) NULL)
4391  stops=(StopInfo *) RelinquishMagickMemory(stops);
4392  for ( ; n >= 0; n--)
4393  graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4394  graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4395  if (status == MagickFalse)
4396  ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
4397  keyword);
4398  return(status != 0 ? MagickTrue : MagickFalse);
4399 }
4400 
4402  ExceptionInfo *exception)
4403 {
4404  return(RenderMVGContent(image,draw_info,0,exception));
4405 }
4406 
4407 /*
4408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4409 % %
4410 % %
4411 % %
4412 % D r a w P a t t e r n P a t h %
4413 % %
4414 % %
4415 % %
4416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4417 %
4418 % DrawPatternPath() draws a pattern.
4419 %
4420 % The format of the DrawPatternPath method is:
4421 %
4422 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4423 % const char *name,Image **pattern,ExceptionInfo *exception)
4424 %
4425 % A description of each parameter follows:
4426 %
4427 % o image: the image.
4428 %
4429 % o draw_info: the draw info.
4430 %
4431 % o name: the pattern name.
4432 %
4433 % o image: the image.
4434 %
4435 % o exception: return any errors or warnings in this structure.
4436 %
4437 */
4439  const DrawInfo *draw_info,const char *name,Image **pattern,
4440  ExceptionInfo *exception)
4441 {
4442  char
4443  property[MagickPathExtent];
4444 
4445  const char
4446  *geometry,
4447  *path,
4448  *type;
4449 
4450  DrawInfo
4451  *clone_info;
4452 
4453  ImageInfo
4454  *image_info;
4455 
4457  status;
4458 
4459  assert(image != (Image *) NULL);
4460  assert(image->signature == MagickCoreSignature);
4461  if (image->debug != MagickFalse)
4462  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4463  assert(draw_info != (const DrawInfo *) NULL);
4464  assert(name != (const char *) NULL);
4465  (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
4466  path=GetImageArtifact(image,property);
4467  if (path == (const char *) NULL)
4468  return(MagickFalse);
4469  (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
4470  geometry=GetImageArtifact(image,property);
4471  if (geometry == (const char *) NULL)
4472  return(MagickFalse);
4473  if ((*pattern) != (Image *) NULL)
4474  *pattern=DestroyImage(*pattern);
4475  image_info=AcquireImageInfo();
4476  image_info->size=AcquireString(geometry);
4477  *pattern=AcquireImage(image_info,exception);
4478  image_info=DestroyImageInfo(image_info);
4479  (void) QueryColorCompliance("#000000ff",AllCompliance,
4480  &(*pattern)->background_color,exception);
4481  (void) SetImageBackgroundColor(*pattern,exception);
4482  if (image->debug != MagickFalse)
4484  "begin pattern-path %s %s",name,geometry);
4485  clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4486  clone_info->fill_pattern=NewImageList();
4487  clone_info->stroke_pattern=NewImageList();
4488  (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
4489  type=GetImageArtifact(image,property);
4490  if (type != (const char *) NULL)
4493  (void) CloneString(&clone_info->primitive,path);
4494  status=RenderMVGContent(*pattern,clone_info,0,exception);
4495  clone_info=DestroyDrawInfo(clone_info);
4496  if (image->debug != MagickFalse)
4497  (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4498  return(status);
4499 }
4500 
4501 /*
4502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4503 % %
4504 % %
4505 % %
4506 + D r a w P o l y g o n P r i m i t i v e %
4507 % %
4508 % %
4509 % %
4510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4511 %
4512 % DrawPolygonPrimitive() draws a polygon on the image.
4513 %
4514 % The format of the DrawPolygonPrimitive method is:
4515 %
4516 % MagickBooleanType DrawPolygonPrimitive(Image *image,
4517 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4518 % ExceptionInfo *exception)
4519 %
4520 % A description of each parameter follows:
4521 %
4522 % o image: the image.
4523 %
4524 % o draw_info: the draw info.
4525 %
4526 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4527 %
4528 % o exception: return any errors or warnings in this structure.
4529 %
4530 */
4531 
4533 {
4534  register ssize_t
4535  i;
4536 
4537  assert(polygon_info != (PolygonInfo **) NULL);
4538  for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4539  if (polygon_info[i] != (PolygonInfo *) NULL)
4540  polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4541  polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4542  return(polygon_info);
4543 }
4544 
4546  const PrimitiveInfo *primitive_info)
4547 {
4548  PathInfo
4549  *magick_restrict path_info;
4550 
4551  PolygonInfo
4552  **polygon_info;
4553 
4554  register ssize_t
4555  i;
4556 
4557  size_t
4558  number_threads;
4559 
4560  number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4561  polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4562  sizeof(*polygon_info));
4563  if (polygon_info == (PolygonInfo **) NULL)
4564  return((PolygonInfo **) NULL);
4565  (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4566  path_info=ConvertPrimitiveToPath(primitive_info);
4567  if (path_info == (PathInfo *) NULL)
4568  return(DestroyPolygonThreadSet(polygon_info));
4569  for (i=0; i < (ssize_t) number_threads; i++)
4570  {
4571  polygon_info[i]=ConvertPathToPolygon(path_info);
4572  if (polygon_info[i] == (PolygonInfo *) NULL)
4573  return(DestroyPolygonThreadSet(polygon_info));
4574  }
4575  path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4576  return(polygon_info);
4577 }
4578 
4579 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
4580  const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4581  const ssize_t y,double *stroke_alpha)
4582 {
4583  double
4584  alpha,
4585  beta,
4586  distance,
4587  subpath_alpha;
4588 
4589  PointInfo
4590  delta;
4591 
4592  register const PointInfo
4593  *q;
4594 
4595  register EdgeInfo
4596  *p;
4597 
4598  register ssize_t
4599  i;
4600 
4601  ssize_t
4602  j,
4603  winding_number;
4604 
4605  /*
4606  Compute fill & stroke opacity for this (x,y) point.
4607  */
4608  *stroke_alpha=0.0;
4609  subpath_alpha=0.0;
4610  p=polygon_info->edges;
4611  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4612  {
4613  if ((double) y <= (p->bounds.y1-mid-0.5))
4614  break;
4615  if ((double) y > (p->bounds.y2+mid+0.5))
4616  {
4617  (void) DestroyEdge(polygon_info,(size_t) j);
4618  continue;
4619  }
4620  if (((double) x <= (p->bounds.x1-mid-0.5)) ||
4621  ((double) x > (p->bounds.x2+mid+0.5)))
4622  continue;
4623  i=(ssize_t) MagickMax((double) p->highwater,1.0);
4624  for ( ; i < (ssize_t) p->number_points; i++)
4625  {
4626  if ((double) y <= (p->points[i-1].y-mid-0.5))
4627  break;
4628  if ((double) y > (p->points[i].y+mid+0.5))
4629  continue;
4630  if (p->scanline != (double) y)
4631  {
4632  p->scanline=(double) y;
4633  p->highwater=(size_t) i;
4634  }
4635  /*
4636  Compute distance between a point and an edge.
4637  */
4638  q=p->points+i-1;
4639  delta.x=(q+1)->x-q->x;
4640  delta.y=(q+1)->y-q->y;
4641  beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4642  if (beta <= 0.0)
4643  {
4644  delta.x=(double) x-q->x;
4645  delta.y=(double) y-q->y;
4646  distance=delta.x*delta.x+delta.y*delta.y;
4647  }
4648  else
4649  {
4650  alpha=delta.x*delta.x+delta.y*delta.y;
4651  if (beta >= alpha)
4652  {
4653  delta.x=(double) x-(q+1)->x;
4654  delta.y=(double) y-(q+1)->y;
4655  distance=delta.x*delta.x+delta.y*delta.y;
4656  }
4657  else
4658  {
4659  alpha=PerceptibleReciprocal(alpha);
4660  beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4661  distance=alpha*beta*beta;
4662  }
4663  }
4664  /*
4665  Compute stroke & subpath opacity.
4666  */
4667  beta=0.0;
4668  if (p->ghostline == MagickFalse)
4669  {
4670  alpha=mid+0.5;
4671  if ((*stroke_alpha < 1.0) &&
4672  (distance <= ((alpha+0.25)*(alpha+0.25))))
4673  {
4674  alpha=mid-0.5;
4675  if (distance <= ((alpha+0.25)*(alpha+0.25)))
4676  *stroke_alpha=1.0;
4677  else
4678  {
4679  beta=1.0;
4680  if (fabs(distance-1.0) >= MagickEpsilon)
4681  beta=sqrt((double) distance);
4682  alpha=beta-mid-0.5;
4683  if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
4684  *stroke_alpha=(alpha-0.25)*(alpha-0.25);
4685  }
4686  }
4687  }
4688  if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
4689  continue;
4690  if (distance <= 0.0)
4691  {
4692  subpath_alpha=1.0;
4693  continue;
4694  }
4695  if (distance > 1.0)
4696  continue;
4697  if (fabs(beta) < MagickEpsilon)
4698  {
4699  beta=1.0;
4700  if (fabs(distance-1.0) >= MagickEpsilon)
4701  beta=sqrt(distance);
4702  }
4703  alpha=beta-1.0;
4704  if (subpath_alpha < (alpha*alpha))
4705  subpath_alpha=alpha*alpha;
4706  }
4707  }
4708  /*
4709  Compute fill opacity.
4710  */
4711  if (fill == MagickFalse)
4712  return(0.0);
4713  if (subpath_alpha >= 1.0)
4714  return(1.0);
4715  /*
4716  Determine winding number.
4717  */
4718  winding_number=0;
4719  p=polygon_info->edges;
4720  for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4721  {
4722  if ((double) y <= p->bounds.y1)
4723  break;
4724  if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
4725  continue;
4726  if ((double) x > p->bounds.x2)
4727  {
4728  winding_number+=p->direction ? 1 : -1;
4729  continue;
4730  }
4731  i=(ssize_t) MagickMax((double) p->highwater,1.0);
4732  for ( ; i < (ssize_t) (p->number_points-1); i++)
4733  if ((double) y <= p->points[i].y)
4734  break;
4735  q=p->points+i-1;
4736  if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4737  winding_number+=p->direction ? 1 : -1;
4738  }
4739  if (fill_rule != NonZeroRule)
4740  {
4741  if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4742  return(1.0);
4743  }
4744  else
4745  if (MagickAbsoluteValue(winding_number) != 0)
4746  return(1.0);
4747  return(subpath_alpha);
4748 }
4749 
4751  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4752  ExceptionInfo *exception)
4753 {
4754  CacheView
4755  *image_view;
4756 
4758  fill,
4759  status;
4760 
4761  double
4762  mid;
4763 
4764  PolygonInfo
4765  **magick_restrict polygon_info;
4766 
4767  register EdgeInfo
4768  *p;
4769 
4770  register ssize_t
4771  i;
4772 
4773  SegmentInfo
4774  bounds;
4775 
4776  ssize_t
4777  start_y,
4778  stop_y,
4779  y;
4780 
4781  assert(image != (Image *) NULL);
4782  assert(image->signature == MagickCoreSignature);
4783  if (image->debug != MagickFalse)
4784  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4785  assert(draw_info != (DrawInfo *) NULL);
4786  assert(draw_info->signature == MagickCoreSignature);
4787  assert(primitive_info != (PrimitiveInfo *) NULL);
4788  if (primitive_info->coordinates <= 1)
4789  return(MagickTrue);
4790  /*
4791  Compute bounding box.
4792  */
4793  polygon_info=AcquirePolygonThreadSet(primitive_info);
4794  if (polygon_info == (PolygonInfo **) NULL)
4795  return(MagickFalse);
4796 DisableMSCWarning(4127)
4797  if (0)
4798  {
4799  status=DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
4800  if (status == MagickFalse)
4801  {
4802  polygon_info=DestroyPolygonThreadSet(polygon_info);
4803  return(status);
4804  }
4805  }
4807  if (image->debug != MagickFalse)
4808  (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4809  fill=(primitive_info->method == FillToBorderMethod) ||
4810  (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4811  mid=ExpandAffine(&draw_info->affine)*SaneStrokeWidth(image,draw_info)/2.0;
4812  bounds=polygon_info[0]->edges[0].bounds;
4813  for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4814  {
4815  p=polygon_info[0]->edges+i;
4816  if (p->bounds.x1 < bounds.x1)
4817  bounds.x1=p->bounds.x1;
4818  if (p->bounds.y1 < bounds.y1)
4819  bounds.y1=p->bounds.y1;
4820  if (p->bounds.x2 > bounds.x2)
4821  bounds.x2=p->bounds.x2;
4822  if (p->bounds.y2 > bounds.y2)
4823  bounds.y2=p->bounds.y2;
4824  }
4825  bounds.x1-=(mid+1.0);
4826  bounds.y1-=(mid+1.0);
4827  bounds.x2+=(mid+1.0);
4828  bounds.y2+=(mid+1.0);
4829  if ((bounds.x1 >= (double) image->columns) ||
4830  (bounds.y1 >= (double) image->rows) ||
4831  (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
4832  {
4833  polygon_info=DestroyPolygonThreadSet(polygon_info);
4834  return(MagickTrue); /* virtual polygon */
4835  }
4836  bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
4837  (double) image->columns-1.0 : bounds.x1;
4838  bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
4839  (double) image->rows-1.0 : bounds.y1;
4840  bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
4841  (double) image->columns-1.0 : bounds.x2;
4842  bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
4843  (double) image->rows-1.0 : bounds.y2;
4844  status=MagickTrue;
4845  image_view=AcquireAuthenticCacheView(image,exception);
4846  if ((primitive_info->coordinates == 1) ||
4847  (polygon_info[0]->number_edges == 0))
4848  {
4849  /*
4850  Draw point.
4851  */
4852  start_y=(ssize_t) ceil(bounds.y1-0.5);
4853  stop_y=(ssize_t) floor(bounds.y2+0.5);
4854 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4855  #pragma omp parallel for schedule(static) shared(status) \
4856  magick_number_threads(image,image,stop_y-start_y+1,1)
4857 #endif
4858  for (y=start_y; y <= stop_y; y++)
4859  {
4861  sync;
4862 
4863  PixelInfo
4864  pixel;
4865 
4866  register ssize_t
4867  x;
4868 
4869  register Quantum
4870  *magick_restrict q;
4871 
4872  ssize_t
4873  start_x,
4874  stop_x;
4875 
4876  if (status == MagickFalse)
4877  continue;
4878  start_x=(ssize_t) ceil(bounds.x1-0.5);
4879  stop_x=(ssize_t) floor(bounds.x2+0.5);
4880  x=start_x;
4881  q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
4882  exception);
4883  if (q == (Quantum *) NULL)
4884  {
4885  status=MagickFalse;
4886  continue;
4887  }
4888  GetPixelInfo(image,&pixel);
4889  for ( ; x <= stop_x; x++)
4890  {
4891  if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
4892  (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
4893  {
4894  GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
4895  SetPixelViaPixelInfo(image,&pixel,q);
4896  }
4897  q+=GetPixelChannels(image);
4898  }
4899  sync=SyncCacheViewAuthenticPixels(image_view,exception);
4900  if (sync == MagickFalse)
4901  status=MagickFalse;
4902  }
4903  image_view=DestroyCacheView(image_view);
4904  polygon_info=DestroyPolygonThreadSet(polygon_info);
4905  if (image->debug != MagickFalse)
4907  " end draw-polygon");
4908  return(status);
4909  }
4910  /*
4911  Draw polygon or line.
4912  */
4913  start_y=(ssize_t) ceil(bounds.y1-0.5);
4914  stop_y=(ssize_t) floor(bounds.y2+0.5);
4915 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4916  #pragma omp parallel for schedule(static) shared(status) \
4917  magick_number_threads(image,image,stop_y-start_y+1,1)
4918 #endif
4919  for (y=start_y; y <= stop_y; y++)
4920  {
4921  const int
4922  id = GetOpenMPThreadId();
4923 
4924  register Quantum
4925  *magick_restrict q;
4926 
4927  register ssize_t
4928  x;
4929 
4930  ssize_t
4931  start_x,
4932  stop_x;
4933 
4934  if (status == MagickFalse)
4935  continue;
4936  start_x=(ssize_t) ceil(bounds.x1-0.5);
4937  stop_x=(ssize_t) floor(bounds.x2+0.5);
4938  q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+
4939  1),1,exception);
4940  if (q == (Quantum *) NULL)
4941  {
4942  status=MagickFalse;
4943  continue;
4944  }
4945  for (x=start_x; x <= stop_x; x++)
4946  {
4947  double
4948  fill_alpha,
4949  stroke_alpha;
4950 
4951  PixelInfo
4952  fill_color,
4953  stroke_color;
4954 
4955  /*
4956  Fill and/or stroke.
4957  */
4958  fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
4959  x,y,&stroke_alpha);
4960  if (draw_info->stroke_antialias == MagickFalse)
4961  {
4962  fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4963  stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4964  }
4965  GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
4966  CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
4967  (double) GetPixelAlpha(image,q),q);
4968  GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
4969  CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
4970  (double) GetPixelAlpha(image,q),q);
4971  q+=GetPixelChannels(image);
4972  }
4973  if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4974  status=MagickFalse;
4975  }
4976  image_view=DestroyCacheView(image_view);
4977  polygon_info=DestroyPolygonThreadSet(polygon_info);
4978  if (image->debug != MagickFalse)
4979  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4980  return(status);
4981 }
4982 
4983 /*
4984 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4985 % %
4986 % %
4987 % %
4988 % D r a w P r i m i t i v e %
4989 % %
4990 % %
4991 % %
4992 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4993 %
4994 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4995 %
4996 % The format of the DrawPrimitive method is:
4997 %
4998 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4999 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
5000 %
5001 % A description of each parameter follows:
5002 %
5003 % o image: the image.
5004 %
5005 % o draw_info: the draw info.
5006 %
5007 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5008 %
5009 % o exception: return any errors or warnings in this structure.
5010 %
5011 */
5012 
5013 static inline double ConstrainCoordinate(double x)
5014 {
5015  if (x < (double) -SSIZE_MAX)
5016  return((double) -SSIZE_MAX);
5017  if (x > (double) SSIZE_MAX)
5018  return((double) SSIZE_MAX);
5019  return(x);
5020 }
5021 
5022 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
5023 {
5024  const char
5025  *methods[] =
5026  {
5027  "point",
5028  "replace",
5029  "floodfill",
5030  "filltoborder",
5031  "reset",
5032  "?"
5033  };
5034 
5035  PointInfo
5036  p,
5037  point,
5038  q;
5039 
5040  register ssize_t
5041  i,
5042  x;
5043 
5044  ssize_t
5045  coordinates,
5046  y;
5047 
5048  x=(ssize_t) ceil(primitive_info->point.x-0.5);
5049  y=(ssize_t) ceil(primitive_info->point.y-0.5);
5050  switch (primitive_info->primitive)
5051  {
5052  case AlphaPrimitive:
5053  {
5055  "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
5056  methods[primitive_info->method]);
5057  return;
5058  }
5059  case ColorPrimitive:
5060  {
5062  "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
5063  methods[primitive_info->method]);
5064  return;
5065  }
5066  case ImagePrimitive:
5067  {
5069  "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
5070  return;
5071  }
5072  case PointPrimitive:
5073  {
5075  "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
5076  methods[primitive_info->method]);
5077  return;
5078  }
5079  case TextPrimitive:
5080  {
5082  "TextPrimitive %.20g,%.20g",(double) x,(double) y);
5083  return;
5084  }
5085  default:
5086  break;
5087  }
5088  coordinates=0;
5089  p=primitive_info[0].point;
5090  q.x=(-1.0);
5091  q.y=(-1.0);
5092  for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5093  {
5094  point=primitive_info[i].point;
5095  if (coordinates <= 0)
5096  {
5097  coordinates=(ssize_t) primitive_info[i].coordinates;
5099  " begin open (%.20g)",(double) coordinates);
5100  p=point;
5101  }
5102  point=primitive_info[i].point;
5103  if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5104  (fabs(q.y-point.y) >= MagickEpsilon))
5106  " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5107  else
5109  " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5110  q=point;
5111  coordinates--;
5112  if (coordinates > 0)
5113  continue;
5114  if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5115  (fabs(p.y-point.y) >= MagickEpsilon))
5116  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
5117  (double) coordinates);
5118  else
5119  (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
5120  (double) coordinates);
5121  }
5122 }
5123 
5125  const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5126  ExceptionInfo *exception)
5127 {
5128  CacheView
5129  *image_view;
5130 
5132  status;
5133 
5134  register ssize_t
5135  i,
5136  x;
5137 
5138  ssize_t
5139  y;
5140 
5141  if (image->debug != MagickFalse)
5142  {
5144  " begin draw-primitive");
5146  " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5147  draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5148  draw_info->affine.tx,draw_info->affine.ty);
5149  }
5150  status=MagickTrue;
5151  if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5152  ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5153  (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5154  status=SetImageColorspace(image,sRGBColorspace,exception);
5155  if (draw_info->compliance == SVGCompliance)
5156  {
5157  status&=SetImageMask(image,WritePixelMask,draw_info->clipping_mask,
5158  exception);
5159  status&=SetImageMask(image,CompositePixelMask,draw_info->composite_mask,
5160  exception);
5161  }
5162  x=(ssize_t) ceil(ConstrainCoordinate(primitive_info->point.x-0.5));
5163  y=(ssize_t) ceil(ConstrainCoordinate(primitive_info->point.y-0.5));
5164  image_view=AcquireAuthenticCacheView(image,exception);
5165  switch (primitive_info->primitive)
5166  {
5167  case AlphaPrimitive:
5168  {
5169  if (image->alpha_trait == UndefinedPixelTrait)
5170  (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
5171  switch (primitive_info->method)
5172  {
5173  case PointMethod:
5174  default:
5175  {
5176  PixelInfo
5177  pixel;
5178 
5179  register Quantum
5180  *q;
5181 
5182  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5183  if (q == (Quantum *) NULL)
5184  break;
5185  GetFillColor(draw_info,x,y,&pixel,exception);
5186  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5187  (void) SyncCacheViewAuthenticPixels(image_view,exception);
5188  break;
5189  }
5190  case ReplaceMethod:
5191  {
5193  sync;
5194 
5195  PixelInfo
5196  pixel,
5197  target;
5198 
5199  (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
5200  exception);
5201  GetPixelInfo(image,&pixel);
5202  for (y=0; y < (ssize_t) image->rows; y++)
5203  {
5204  register Quantum
5205  *magick_restrict q;
5206 
5207  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5208  exception);
5209  if (q == (Quantum *) NULL)
5210  break;
5211  for (x=0; x < (ssize_t) image->columns; x++)
5212  {
5213  GetPixelInfoPixel(image,q,&pixel);
5214  if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5215  {
5216  q+=GetPixelChannels(image);
5217  continue;
5218  }
5219  GetFillColor(draw_info,x,y,&pixel,exception);
5220  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5221  q+=GetPixelChannels(image);
5222  }
5223  sync=SyncCacheViewAuthenticPixels(image_view,exception);
5224  if (sync == MagickFalse)
5225  break;
5226  }
5227  break;
5228  }
5229  case FloodfillMethod:
5230  case FillToBorderMethod:
5231  {
5232  ChannelType
5233  channel_mask;
5234 
5235  PixelInfo
5236  target;
5237 
5239  &target,exception);
5240  if (primitive_info->method == FillToBorderMethod)
5241  {
5242  target.red=(double) draw_info->border_color.red;
5243  target.green=(double) draw_info->border_color.green;
5244  target.blue=(double) draw_info->border_color.blue;
5245  }
5246  channel_mask=SetImageChannelMask(image,AlphaChannel);
5247  status&=FloodfillPaintImage(image,draw_info,&target,x,y,
5248  primitive_info->method == FloodfillMethod ? MagickFalse :
5249  MagickTrue,exception);
5250  (void) SetImageChannelMask(image,channel_mask);
5251  break;
5252  }
5253  case ResetMethod:
5254  {
5256  sync;
5257 
5258  PixelInfo
5259  pixel;
5260 
5261  for (y=0; y < (ssize_t) image->rows; y++)
5262  {
5263  register Quantum
5264  *magick_restrict q;
5265 
5266  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5267  exception);
5268  if (q == (Quantum *) NULL)
5269  break;
5270  for (x=0; x < (ssize_t) image->columns; x++)
5271  {
5272  GetFillColor(draw_info,x,y,&pixel,exception);
5273  SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5274  q+=GetPixelChannels(image);
5275  }
5276  sync=SyncCacheViewAuthenticPixels(image_view,exception);
5277  if (sync == MagickFalse)
5278  break;
5279  }
5280  break;
5281  }
5282  }
5283  break;
5284  }
5285  case ColorPrimitive:
5286  {
5287  switch (primitive_info->method)
5288  {
5289  case PointMethod:
5290  default:
5291  {
5292  PixelInfo
5293  pixel;
5294 
5295  register Quantum
5296  *q;
5297 
5298  q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5299  if (q == (Quantum *) NULL)
5300  break;
5301  GetPixelInfo(image,&pixel);
5302  GetFillColor(draw_info,x,y,&pixel,exception);
5303  SetPixelViaPixelInfo(image,&pixel,q);
5304  (void) SyncCacheViewAuthenticPixels(image_view,exception);
5305  break;
5306  }
5307  case ReplaceMethod:
5308  {
5310  sync;
5311 
5312  PixelInfo
5313  pixel,
5314  target;
5315 
5316  (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
5317  exception);
5318  for (y=0; y < (ssize_t) image->rows; y++)
5319  {
5320  register Quantum
5321  *magick_restrict q;
5322 
5323  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5324  exception);
5325  if (q == (Quantum *) NULL)
5326  break;
5327  for (x=0; x < (ssize_t) image->columns; x++)
5328  {
5329  GetPixelInfoPixel(image,q,&pixel);
5330  if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5331  {
5332  q+=GetPixelChannels(image);
5333  continue;
5334  }
5335  GetFillColor(draw_info,x,y,&pixel,exception);
5336  SetPixelViaPixelInfo(image,&pixel,q);
5337  q+=GetPixelChannels(image);
5338  }
5339  sync=SyncCacheViewAuthenticPixels(image_view,exception);
5340  if (sync == MagickFalse)
5341  break;
5342  }
5343  break;
5344  }
5345  case FloodfillMethod:
5346  case FillToBorderMethod:
5347  {
5348  PixelInfo
5349  target;
5350 
5352  &target,exception);
5353  if (primitive_info->method == FillToBorderMethod)
5354  {
5355  target.red=(double) draw_info->border_color.red;
5356  target.green=(double) draw_info->border_color.green;
5357  target.blue=(double) draw_info->border_color.blue;
5358  }
5359  status&=FloodfillPaintImage(image,draw_info,&target,x,y,
5360  primitive_info->method == FloodfillMethod ? MagickFalse :
5361  MagickTrue,exception);
5362  break;
5363  }
5364  case ResetMethod:
5365  {
5367  sync;
5368 
5369  PixelInfo
5370  pixel;
5371 
5372  GetPixelInfo(image,&pixel);
5373  for (y=0; y < (ssize_t) image->rows; y++)
5374  {
5375  register Quantum
5376  *magick_restrict q;
5377 
5378  q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5379  exception);
5380  if (q == (Quantum *) NULL)
5381  break;
5382  for (x=0; x < (ssize_t) image->columns; x++)
5383  {
5384  GetFillColor(draw_info,x,y,&pixel,exception);
5385  SetPixelViaPixelInfo(image,&pixel,q);
5386  q+=GetPixelChannels(image);
5387  }
5388  sync=SyncCacheViewAuthenticPixels(image_view,exception);
5389  if (sync == MagickFalse)
5390  break;
5391  }
5392  break;
5393  }
5394  }
5395  break;
5396  }
5397  case ImagePrimitive:
5398  {
5399  AffineMatrix
5400  affine;
5401 
5402  char
5403  composite_geometry[MagickPathExtent];
5404 
5405  Image
5406  *composite_image,
5407  *composite_images;
5408 
5409  ImageInfo
5410  *clone_info;
5411 
5413  geometry;
5414 
5415  ssize_t
5416  x1,
5417  y1;
5418 
5419  if (primitive_info->text == (char *) NULL)
5420  break;
5421  clone_info=AcquireImageInfo();
5422  if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5423  composite_images=ReadInlineImage(clone_info,primitive_info->text,
5424  exception);
5425  else
5426  {
5427  (void) CopyMagickString(clone_info->filename,primitive_info->text,
5429  composite_images=ReadImage(clone_info,exception);
5430  }
5431  clone_info=DestroyImageInfo(clone_info);
5432  if (composite_images == (Image *) NULL)
5433  {
5434  status=0;
5435  break;
5436  }
5437  composite_image=RemoveFirstImageFromList(&composite_images);
5438  composite_images=DestroyImageList(composite_images);
5439  (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5440  NULL,(void *) NULL);
5441  x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
5442  y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
5443  if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5444  ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5445  {
5446  /*
5447  Resize image.
5448  */
5449  (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5450  "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
5451  composite_image->filter=image->filter;
5452  (void) TransformImage(&composite_image,(char *) NULL,
5453  composite_geometry,exception);
5454  }
5455  if (composite_image->alpha_trait == UndefinedPixelTrait)
5456  (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
5457  exception);
5458  if (draw_info->alpha != OpaqueAlpha)
5459  (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
5460  SetGeometry(image,&geometry);
5461  image->gravity=draw_info->gravity;
5462  geometry.x=x;
5463  geometry.y=y;
5464&#