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