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