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