2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % D D RRRR AAAAA W W W %
13 % MagickCore Image Drawing Methods %
21 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % https://imagemagick.org/script/license.php %
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. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
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"
53 #include "MagickCore/cache-private.h"
54 #include "MagickCore/cache-view.h"
55 #include "MagickCore/channel.h"
56 #include "MagickCore/color.h"
57 #include "MagickCore/colorspace-private.h"
58 #include "MagickCore/composite.h"
59 #include "MagickCore/composite-private.h"
60 #include "MagickCore/constitute.h"
61 #include "MagickCore/draw.h"
62 #include "MagickCore/draw-private.h"
63 #include "MagickCore/enhance.h"
64 #include "MagickCore/exception.h"
65 #include "MagickCore/exception-private.h"
66 #include "MagickCore/gem.h"
67 #include "MagickCore/geometry.h"
68 #include "MagickCore/image-private.h"
69 #include "MagickCore/list.h"
70 #include "MagickCore/log.h"
71 #include "MagickCore/memory-private.h"
72 #include "MagickCore/monitor.h"
73 #include "MagickCore/monitor-private.h"
74 #include "MagickCore/option.h"
75 #include "MagickCore/paint.h"
76 #include "MagickCore/pixel-accessor.h"
77 #include "MagickCore/pixel-private.h"
78 #include "MagickCore/property.h"
79 #include "MagickCore/resample.h"
80 #include "MagickCore/resample-private.h"
81 #include "MagickCore/resource_.h"
82 #include "MagickCore/splay-tree.h"
83 #include "MagickCore/string_.h"
84 #include "MagickCore/string-private.h"
85 #include "MagickCore/thread-private.h"
86 #include "MagickCore/token.h"
87 #include "MagickCore/transform-private.h"
88 #include "MagickCore/utility.h"
93 #define BezierQuantum 200
94 #define PrimitiveExtentPad 128
95 #define MaxBezierCoordinates 4194304
96 #define ThrowPointExpectedException(token,exception) \
98 (void) ThrowMagickException(exception,GetMagickModule(),DrawError, \
99 "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
100 status=MagickFalse; \
105 Typedef declarations.
107 typedef struct _EdgeInfo
131 typedef struct _ElementInfo
141 typedef struct _MVGInfo
159 typedef struct _PolygonInfo
177 typedef struct _PathInfo
187 Forward declarations.
190 *DrawClippingMask(Image *,const DrawInfo *,const char *,const char *,
193 static MagickBooleanType
194 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
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),
203 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
204 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
205 TraceRoundRectangle(MVGInfo *,const PointInfo,const PointInfo,PointInfo),
206 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
209 *TraceStrokePolygon(const Image *,const DrawInfo *,const PrimitiveInfo *);
212 TracePath(MVGInfo *,const char *,ExceptionInfo *);
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 % A c q u i r e D r a w I n f o %
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 % AcquireDrawInfo() returns a DrawInfo structure properly initialized.
227 % The format of the AcquireDrawInfo method is:
229 % DrawInfo *AcquireDrawInfo(void)
232 MagickExport DrawInfo *AcquireDrawInfo(void)
237 draw_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*draw_info));
238 GetDrawInfo((ImageInfo *) NULL,draw_info);
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % C l o n e D r a w I n f o %
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
257 % The format of the CloneDrawInfo method is:
259 % DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
260 % const DrawInfo *draw_info)
262 % A description of each parameter follows:
264 % o image_info: the image info.
266 % o draw_info: the draw info.
269 MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
270 const DrawInfo *draw_info)
278 clone_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*clone_info));
279 GetDrawInfo(image_info,clone_info);
280 if (draw_info == (DrawInfo *) NULL)
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,
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)
339 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
340 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) (x+4),
341 sizeof(*clone_info->dash_pattern));
342 if (clone_info->dash_pattern == (double *) NULL)
343 ThrowFatalException(ResourceLimitFatalError,
344 "UnableToAllocateDashPattern");
345 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(size_t)
346 (x+4)*sizeof(*clone_info->dash_pattern));
348 clone_info->gradient=draw_info->gradient;
349 if (draw_info->gradient.stops != (StopInfo *) NULL)
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)
358 ThrowFatalException(ResourceLimitFatalError,
359 "UnableToAllocateDashPattern");
360 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
361 (size_t) number_stops*sizeof(*clone_info->gradient.stops));
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);
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 + C o n v e r t P a t h T o P o l y g o n %
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 % ConvertPathToPolygon() converts a path to the more efficient sorted
397 % The format of the ConvertPathToPolygon method is:
399 % PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
401 % A description of each parameter follows:
403 % o Method ConvertPathToPolygon returns the path in a more efficient sorted
404 % rendering form of type PolygonInfo.
406 % o draw_info: Specifies a pointer to an DrawInfo structure.
408 % o path_info: Specifies a pointer to an PathInfo structure.
413 #if defined(__cplusplus) || defined(c_plusplus)
417 static int DrawCompareEdges(const void *p_edge,const void *q_edge)
419 #define DrawCompareEdge(p,q) \
421 if (((p)-(q)) < 0.0) \
423 if (((p)-(q)) > 0.0) \
427 register const PointInfo
432 Edge sorting for right-handed coordinate system.
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)*
440 DrawCompareEdge(p[1].y,q[1].y);
441 DrawCompareEdge(p[1].x,q[1].x);
445 #if defined(__cplusplus) || defined(c_plusplus)
449 static void LogPolygonInfo(const PolygonInfo *polygon_info)
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++)
462 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
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");
468 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
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);
476 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
479 static void ReversePoints(PointInfo *points,const size_t number_points)
487 for (i=0; i < (ssize_t) (number_points >> 1); i++)
490 points[i]=points[number_points-(i+1)];
491 points[number_points-(i+1)]=point;
495 static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info)
524 Convert a path to the more efficient sorted rendering form.
526 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
527 if (polygon_info == (PolygonInfo *) NULL)
528 return((PolygonInfo *) NULL);
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));
538 ghostline=MagickFalse;
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++)
554 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
555 (path_info[i].code == GhostlineCode))
560 if ((points != (PointInfo *) NULL) && (n >= 2))
562 if (edge == number_edges)
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);
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);
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;
586 if (points == (PointInfo *) NULL)
589 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
591 if (points == (PointInfo *) NULL)
592 return((PolygonInfo *) NULL);
594 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
595 point=path_info[i].point;
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))
616 if (edge == number_edges)
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);
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);
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;
637 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
639 if (points == (PointInfo *) NULL)
640 return((PolygonInfo *) NULL);
642 ghostline=MagickFalse;
648 direction=next_direction;
649 if (points == (PointInfo *) NULL)
651 if (n == (ssize_t) number_points)
654 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
656 if (points == (PointInfo *) NULL)
657 return((PolygonInfo *) NULL);
659 point=path_info[i].point;
661 if (point.x < bounds.x1)
663 if (point.x > bounds.x2)
667 if (points != (PointInfo *) NULL)
670 points=(PointInfo *) RelinquishMagickMemory(points);
673 if (edge == number_edges)
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);
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);
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;
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);
706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710 + C o n v e r t P r i m i t i v e T o P a t h %
714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716 % ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
719 % The format of the ConvertPrimitiveToPath method is:
721 % PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
722 % const PrimitiveInfo *primitive_info)
724 % A description of each parameter follows:
726 % o Method ConvertPrimitiveToPath returns a vector path structure of type
729 % o draw_info: a structure of type DrawInfo.
731 % o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
736 static void LogPathInfo(const PathInfo *path_info)
738 register const PathInfo
741 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
742 for (p=path_info; p->code != EndCode; p++)
743 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
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" :
748 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
751 static PathInfo *ConvertPrimitiveToPath(const PrimitiveInfo *primitive_info)
775 Converts a PrimitiveInfo structure into a vector path structure.
777 switch (primitive_info->primitive)
784 return((PathInfo *) NULL);
788 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
789 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (3UL*i+1UL),
791 if (path_info == (PathInfo *) NULL)
792 return((PathInfo *) NULL);
794 closed_subpath=MagickFalse;
801 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
804 if (coordinates <= 0)
809 coordinates=(ssize_t) primitive_info[i].coordinates;
810 p=primitive_info[i].point;
813 closed_subpath=primitive_info[i].closed_subpath;
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))
821 Eliminate duplicate points.
823 path_info[n].code=code;
824 path_info[n].point=primitive_info[i].point;
825 q=primitive_info[i].point;
829 continue; /* next point in current subpath */
830 if (closed_subpath != MagickFalse)
832 closed_subpath=MagickFalse;
836 Mark the p point as open if the subpath is not closed.
838 path_info[start].code=OpenCode;
839 path_info[n].code=GhostlineCode;
840 path_info[n].point=primitive_info[i].point;
842 path_info[n].code=LineToCode;
843 path_info[n].point=p;
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),
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % D e s t r o y D r a w I n f o %
865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 % DestroyDrawInfo() deallocates memory associated with an DrawInfo structure.
869 % The format of the DestroyDrawInfo method is:
871 % DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
873 % A description of each parameter follows:
875 % o draw_info: the draw info.
878 MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
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 *)
906 RelinquishMagickMemory(draw_info->server_name);
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)
911 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
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);
925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
929 + D e s t r o y E d g e %
933 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
935 % DestroyEdge() destroys the specified polygon edge.
937 % The format of the DestroyEdge method is:
939 % ssize_t DestroyEdge(PolygonInfo *polygon_info,const int edge)
941 % A description of each parameter follows:
943 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
945 % o edge: the polygon edge number to destroy.
948 static size_t DestroyEdge(PolygonInfo *polygon_info,
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);
962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966 + D e s t r o y P o l y g o n I n f o %
970 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972 % DestroyPolygonInfo() destroys the PolygonInfo data structure.
974 % The format of the DestroyPolygonInfo method is:
976 % PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
978 % A description of each parameter follows:
980 % o polygon_info: Specifies a pointer to an PolygonInfo structure.
983 static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
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));
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 % D r a w A f f i n e I m a g e %
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006 % DrawAffineImage() composites the source over the destination image as
1007 % dictated by the affine transform.
1009 % The format of the DrawAffineImage method is:
1011 % MagickBooleanType DrawAffineImage(Image *image,const Image *source,
1012 % const AffineMatrix *affine,ExceptionInfo *exception)
1014 % A description of each parameter follows:
1016 % o image: the image.
1018 % o source: the source image.
1020 % o affine: the affine transform.
1022 % o exception: return any errors or warnings in this structure.
1026 static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
1027 const double y,const SegmentInfo *edge)
1040 Determine left and right edges.
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)
1049 intercept=(-z/affine->sx);
1051 if (x > inverse_edge.x1)
1053 intercept=(-z+(double) image->columns)/affine->sx;
1055 if (x < inverse_edge.x2)
1059 if (affine->sx < -MagickEpsilon)
1061 intercept=(-z+(double) image->columns)/affine->sx;
1063 if (x > inverse_edge.x1)
1065 intercept=(-z/affine->sx);
1067 if (x < inverse_edge.x2)
1071 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1073 inverse_edge.x2=edge->x1;
1074 return(inverse_edge);
1077 Determine top and bottom edges.
1079 z=affine->sy*y+affine->ty;
1080 if (affine->rx >= MagickEpsilon)
1082 intercept=(-z/affine->rx);
1084 if (x > inverse_edge.x1)
1086 intercept=(-z+(double) image->rows)/affine->rx;
1088 if (x < inverse_edge.x2)
1092 if (affine->rx < -MagickEpsilon)
1094 intercept=(-z+(double) image->rows)/affine->rx;
1096 if (x > inverse_edge.x1)
1098 intercept=(-z/affine->rx);
1100 if (x < inverse_edge.x2)
1104 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1106 inverse_edge.x2=edge->x2;
1107 return(inverse_edge);
1109 return(inverse_edge);
1112 static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1120 determinant=PerceptibleReciprocal(affine->sx*affine->sy-affine->rx*
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*
1128 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1130 return(inverse_affine);
1133 MagickExport MagickBooleanType DrawAffineImage(Image *image,
1134 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1166 Determine bounding box.
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);
1177 extent[1].x=(double) source->columns-1.0;
1179 extent[2].x=(double) source->columns-1.0;
1180 extent[2].y=(double) source->rows-1.0;
1182 extent[3].y=(double) source->rows-1.0;
1183 for (i=0; i < 4; 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;
1194 for (i=1; i < 4; i++)
1196 if (min.x > extent[i].x)
1198 if (min.y > extent[i].y)
1200 if (max.x < extent[i].x)
1202 if (max.y < extent[i].y)
1206 Affine transform image.
1208 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1209 return(MagickFalse);
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)
1225 for (y=start; y <= stop; y++)
1246 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1247 if (inverse_edge.x2 < inverse_edge.x1)
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),
1252 if (q == (Quantum *) NULL)
1257 for (x=(ssize_t) ceil(inverse_edge.x1-0.5); x <= (ssize_t) floor(inverse_edge.x2+0.5); x++)
1259 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1261 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1263 status=InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1264 point.x,point.y,&pixel,exception);
1265 if (status == MagickFalse)
1267 GetPixelInfoPixel(image,q,&composite);
1268 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1270 SetPixelViaPixelInfo(image,&composite,q);
1272 q+=GetPixelChannels(image);
1274 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1277 source_view=DestroyCacheView(source_view);
1278 image_view=DestroyCacheView(image_view);
1283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1287 + D r a w B o u n d i n g R e c t a n g l e s %
1291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1293 % DrawBoundingRectangles() draws the bounding rectangles on the image. This
1294 % is only useful for developers debugging the rendering algorithm.
1296 % The format of the DrawBoundingRectangles method is:
1298 % MagickBooleanType DrawBoundingRectangles(Image *image,
1299 % const DrawInfo *draw_info,PolygonInfo *polygon_info,
1300 % ExceptionInfo *exception)
1302 % A description of each parameter follows:
1304 % o image: the image.
1306 % o draw_info: the draw info.
1308 % o polygon_info: Specifies a pointer to a PolygonInfo structure.
1310 % o exception: return any errors or warnings in this structure.
1314 static inline double SaneStrokeWidth(const Image *image,
1315 const DrawInfo *draw_info)
1317 return(MagickMin((double) draw_info->stroke_width,
1318 (2.0*sqrt(2.0)+MagickEpsilon)*MagickMax(image->columns,image->rows)));
1321 static MagickBooleanType DrawBoundingRectangles(Image *image,
1322 const DrawInfo *draw_info,const PolygonInfo *polygon_info,
1323 ExceptionInfo *exception)
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,
1355 if (status == MagickFalse)
1357 clone_info=DestroyDrawInfo(clone_info);
1362 if (clone_info->density != (char *) NULL)
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;
1376 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1377 SaneStrokeWidth(image,clone_info)/2.0;
1382 if (polygon_info != (PolygonInfo *) NULL)
1384 bounds=polygon_info->edges[0].bounds;
1385 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
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;
1397 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1398 image->columns ? (double) image->columns-1 : bounds.x1;
1400 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1401 image->rows ? (double) image->rows-1 : bounds.y1;
1403 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1404 image->columns ? (double) image->columns-1 : bounds.x2;
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++)
1410 if (polygon_info->edges[i].direction != 0)
1411 status=QueryColorCompliance("#f00",AllCompliance,&clone_info->stroke,
1414 status=QueryColorCompliance("#0f0",AllCompliance,&clone_info->stroke,
1416 if (status == MagickFalse)
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 if (TraceRectangle(primitive_info,start,end) == MagickFalse)
1425 primitive_info[0].method=ReplaceMethod;
1426 coordinates=(ssize_t) primitive_info[0].coordinates;
1427 primitive_info[coordinates].primitive=UndefinedPrimitive;
1428 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1429 if (status == MagickFalse)
1432 if (i < (ssize_t) polygon_info->number_edges)
1434 clone_info=DestroyDrawInfo(clone_info);
1438 status=QueryColorCompliance("#00f",AllCompliance,&clone_info->stroke,
1440 if (status == MagickFalse)
1442 clone_info=DestroyDrawInfo(clone_info);
1445 start.x=(double) (bounds.x1-mid);
1446 start.y=(double) (bounds.y1-mid);
1447 end.x=(double) (bounds.x2+mid);
1448 end.y=(double) (bounds.y2+mid);
1449 primitive_info[0].primitive=RectanglePrimitive;
1450 if (TraceRectangle(primitive_info,start,end) == MagickFalse)
1452 primitive_info[0].method=ReplaceMethod;
1453 coordinates=(ssize_t) primitive_info[0].coordinates;
1454 primitive_info[coordinates].primitive=UndefinedPrimitive;
1455 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1456 clone_info=DestroyDrawInfo(clone_info);
1461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1465 % D r a w C l i p P a t h %
1469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1471 % DrawClipPath() draws the clip path on the image mask.
1473 % The format of the DrawClipPath method is:
1475 % MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1476 % const char *id,ExceptionInfo *exception)
1478 % A description of each parameter follows:
1480 % o image: the image.
1482 % o draw_info: the draw info.
1484 % o id: the clip path id.
1486 % o exception: return any errors or warnings in this structure.
1489 MagickExport MagickBooleanType DrawClipPath(Image *image,
1490 const DrawInfo *draw_info,const char *id,ExceptionInfo *exception)
1501 clip_path=GetImageArtifact(image,id);
1502 if (clip_path == (const char *) NULL)
1503 return(MagickFalse);
1504 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1506 if (clipping_mask == (Image *) NULL)
1507 return(MagickFalse);
1508 status=SetImageMask(image,WritePixelMask,clipping_mask,exception);
1509 clipping_mask=DestroyImage(clipping_mask);
1514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1518 % D r a w C l i p p i n g M a s k %
1522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1524 % DrawClippingMask() draws the clip path and returns it as an image clipping
1527 % The format of the DrawClippingMask method is:
1529 % Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1530 % const char *id,const char *clip_path,ExceptionInfo *exception)
1532 % A description of each parameter follows:
1534 % o image: the image.
1536 % o draw_info: the draw info.
1538 % o id: the clip path id.
1540 % o clip_path: the clip path.
1542 % o exception: return any errors or warnings in this structure.
1545 static Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1546 const char *id,const char *clip_path,ExceptionInfo *exception)
1561 assert(image != (Image *) NULL);
1562 assert(image->signature == MagickCoreSignature);
1563 if (image->debug != MagickFalse)
1564 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1565 assert(draw_info != (const DrawInfo *) NULL);
1566 clip_mask=AcquireImage((const ImageInfo *) NULL,exception);
1567 status=SetImageExtent(clip_mask,image->columns,image->rows,exception);
1568 if (status == MagickFalse)
1569 return(DestroyImage(clip_mask));
1570 status=SetImageMask(clip_mask,WritePixelMask,(Image *) NULL,exception);
1571 status=QueryColorCompliance("#0000",AllCompliance,
1572 &clip_mask->background_color,exception);
1573 clip_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1574 clip_mask->background_color.alpha_trait=BlendPixelTrait;
1575 status=SetImageBackgroundColor(clip_mask,exception);
1576 if (image->debug != MagickFalse)
1577 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1579 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1580 (void) CloneString(&clone_info->primitive,clip_path);
1581 status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1583 if (clone_info->clip_mask != (char *) NULL)
1584 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1585 status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1587 clone_info->stroke_width=0.0;
1588 clone_info->alpha=OpaqueAlpha;
1589 clone_info->clip_path=MagickTrue;
1590 status=RenderMVGContent(clip_mask,clone_info,1,exception);
1591 clone_info=DestroyDrawInfo(clone_info);
1592 separate_mask=SeparateImage(clip_mask,AlphaChannel,exception);
1593 if (separate_mask != (Image *) NULL)
1595 clip_mask=DestroyImage(clip_mask);
1596 clip_mask=separate_mask;
1597 status=NegateImage(clip_mask,MagickFalse,exception);
1598 if (status == MagickFalse)
1599 clip_mask=DestroyImage(clip_mask);
1601 if (image->debug != MagickFalse)
1602 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611 % D r a w C o m p o s i t e M a s k %
1615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617 % DrawCompositeMask() draws the mask path and returns it as an image mask.
1619 % The format of the DrawCompositeMask method is:
1621 % Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1622 % const char *id,const char *mask_path,ExceptionInfo *exception)
1624 % A description of each parameter follows:
1626 % o image: the image.
1628 % o draw_info: the draw info.
1630 % o id: the mask path id.
1632 % o mask_path: the mask path.
1634 % o exception: return any errors or warnings in this structure.
1637 static Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1638 const char *id,const char *mask_path,ExceptionInfo *exception)
1653 assert(image != (Image *) NULL);
1654 assert(image->signature == MagickCoreSignature);
1655 if (image->debug != MagickFalse)
1656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1657 assert(draw_info != (const DrawInfo *) NULL);
1658 composite_mask=AcquireImage((const ImageInfo *) NULL,exception);
1659 status=SetImageExtent(composite_mask,image->columns,image->rows,exception);
1660 if (status == MagickFalse)
1661 return(DestroyImage(composite_mask));
1662 status=SetImageMask(composite_mask,CompositePixelMask,(Image *) NULL,
1664 status=QueryColorCompliance("#0000",AllCompliance,
1665 &composite_mask->background_color,exception);
1666 composite_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1667 composite_mask->background_color.alpha_trait=BlendPixelTrait;
1668 (void) SetImageBackgroundColor(composite_mask,exception);
1669 if (image->debug != MagickFalse)
1670 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin mask-path %s",
1672 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1673 (void) CloneString(&clone_info->primitive,mask_path);
1674 status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1676 status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1678 clone_info->stroke_width=0.0;
1679 clone_info->alpha=OpaqueAlpha;
1680 status=RenderMVGContent(composite_mask,clone_info,1,exception);
1681 clone_info=DestroyDrawInfo(clone_info);
1682 separate_mask=SeparateImage(composite_mask,AlphaChannel,exception);
1683 if (separate_mask != (Image *) NULL)
1685 composite_mask=DestroyImage(composite_mask);
1686 composite_mask=separate_mask;
1687 status=NegateImage(composite_mask,MagickFalse,exception);
1688 if (status == MagickFalse)
1689 composite_mask=DestroyImage(composite_mask);
1691 if (image->debug != MagickFalse)
1692 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end mask-path");
1693 return(composite_mask);
1697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1701 + D r a w D a s h P o l y g o n %
1705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1707 % DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1708 % image while respecting the dash offset and dash pattern attributes.
1710 % The format of the DrawDashPolygon method is:
1712 % MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1713 % const PrimitiveInfo *primitive_info,Image *image,
1714 % ExceptionInfo *exception)
1716 % A description of each parameter follows:
1718 % o draw_info: the draw info.
1720 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1722 % o image: the image.
1724 % o exception: return any errors or warnings in this structure.
1727 static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1728 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1760 assert(draw_info != (const DrawInfo *) NULL);
1761 if (image->debug != MagickFalse)
1762 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1763 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1764 number_vertices=(size_t) i;
1765 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1766 (2UL*number_vertices+32UL),sizeof(*dash_polygon));
1767 if (dash_polygon == (PrimitiveInfo *) NULL)
1768 return(MagickFalse);
1769 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1770 sizeof(*dash_polygon));
1771 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1772 clone_info->miterlimit=0;
1773 dash_polygon[0]=primitive_info[0];
1774 scale=ExpandAffine(&draw_info->affine);
1775 length=scale*draw_info->dash_pattern[0];
1776 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1777 scale*draw_info->dash_offset : 0.0;
1779 for (n=0; offset > 0.0; j=0)
1781 if (draw_info->dash_pattern[n] <= 0.0)
1783 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1784 if (offset > length)
1788 length=scale*draw_info->dash_pattern[n];
1791 if (offset < length)
1803 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1805 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1806 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1807 maximum_length=hypot(dx,dy);
1808 if (maximum_length > MaxBezierCoordinates)
1810 if (fabs(length) < MagickEpsilon)
1813 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1815 length=scale*draw_info->dash_pattern[n];
1817 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1819 total_length+=length;
1820 if ((n & 0x01) != 0)
1822 dash_polygon[0]=primitive_info[0];
1823 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1824 total_length*PerceptibleReciprocal(maximum_length));
1825 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1826 total_length*PerceptibleReciprocal(maximum_length));
1831 if ((j+1) > (ssize_t) number_vertices)
1833 dash_polygon[j]=primitive_info[i-1];
1834 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1835 total_length*PerceptibleReciprocal(maximum_length));
1836 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1837 total_length*PerceptibleReciprocal(maximum_length));
1838 dash_polygon[j].coordinates=1;
1840 dash_polygon[0].coordinates=(size_t) j;
1841 dash_polygon[j].primitive=UndefinedPrimitive;
1842 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1845 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1847 length=scale*draw_info->dash_pattern[n];
1849 length-=(maximum_length-total_length);
1850 if ((n & 0x01) != 0)
1852 dash_polygon[j]=primitive_info[i];
1853 dash_polygon[j].coordinates=1;
1856 if ((total_length < maximum_length) && ((n & 0x01) == 0) && (j > 1))
1858 dash_polygon[j]=primitive_info[i-1];
1859 dash_polygon[j].point.x+=MagickEpsilon;
1860 dash_polygon[j].point.y+=MagickEpsilon;
1861 dash_polygon[j].coordinates=1;
1863 dash_polygon[0].coordinates=(size_t) j;
1864 dash_polygon[j].primitive=UndefinedPrimitive;
1865 status&=DrawStrokePolygon(image,clone_info,dash_polygon,exception);
1867 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1868 clone_info=DestroyDrawInfo(clone_info);
1869 if (image->debug != MagickFalse)
1870 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1871 return(status != 0 ? MagickTrue : MagickFalse);
1875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1879 % D r a w G r a d i e n t I m a g e %
1883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1885 % DrawGradientImage() draws a linear gradient on the image.
1887 % The format of the DrawGradientImage method is:
1889 % MagickBooleanType DrawGradientImage(Image *image,
1890 % const DrawInfo *draw_info,ExceptionInfo *exception)
1892 % A description of each parameter follows:
1894 % o image: the image.
1896 % o draw_info: the draw info.
1898 % o exception: return any errors or warnings in this structure.
1902 static inline double GetStopColorOffset(const GradientInfo *gradient,
1903 const ssize_t x,const ssize_t y)
1905 switch (gradient->type)
1907 case UndefinedGradient:
1908 case LinearGradient:
1923 gradient_vector=(&gradient->gradient_vector);
1924 p.x=gradient_vector->x2-gradient_vector->x1;
1925 p.y=gradient_vector->y2-gradient_vector->y1;
1926 q.x=(double) x-gradient_vector->x1;
1927 q.y=(double) y-gradient_vector->y1;
1928 length=sqrt(q.x*q.x+q.y*q.y);
1929 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1930 gamma=PerceptibleReciprocal(gamma);
1931 scale=p.x*q.x+p.y*q.y;
1932 offset=gamma*scale*length;
1935 case RadialGradient:
1940 if (gradient->spread == RepeatSpread)
1942 v.x=(double) x-gradient->center.x;
1943 v.y=(double) y-gradient->center.y;
1944 return(sqrt(v.x*v.x+v.y*v.y));
1946 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1947 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1948 gradient->angle))))*PerceptibleReciprocal(gradient->radii.x);
1949 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1950 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1951 gradient->angle))))*PerceptibleReciprocal(gradient->radii.y);
1952 return(sqrt(v.x*v.x+v.y*v.y));
1958 static int StopInfoCompare(const void *x,const void *y)
1964 stop_1=(StopInfo *) x;
1965 stop_2=(StopInfo *) y;
1966 if (stop_1->offset > stop_2->offset)
1968 if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
1973 MagickExport MagickBooleanType DrawGradientImage(Image *image,
1974 const DrawInfo *draw_info,ExceptionInfo *exception)
2004 Draw linear or radial gradient on image.
2006 assert(image != (Image *) NULL);
2007 assert(image->signature == MagickCoreSignature);
2008 if (image->debug != MagickFalse)
2009 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2010 assert(draw_info != (const DrawInfo *) NULL);
2011 gradient=(&draw_info->gradient);
2012 qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
2014 gradient_vector=(&gradient->gradient_vector);
2015 point.x=gradient_vector->x2-gradient_vector->x1;
2016 point.y=gradient_vector->y2-gradient_vector->y1;
2017 length=sqrt(point.x*point.x+point.y*point.y);
2018 bounding_box=gradient->bounding_box;
2020 GetPixelInfo(image,&zero);
2021 image_view=AcquireAuthenticCacheView(image,exception);
2022 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2023 #pragma omp parallel for schedule(static) shared(status) \
2024 magick_number_threads(image,image,bounding_box.height-bounding_box.y,1)
2026 for (y=bounding_box.y; y < (ssize_t) bounding_box.height; y++)
2046 if (status == MagickFalse)
2048 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2049 if (q == (Quantum *) NULL)
2056 offset=GetStopColorOffset(gradient,0,y);
2057 if (gradient->type != RadialGradient)
2058 offset*=PerceptibleReciprocal(length);
2059 for (x=bounding_box.x; x < (ssize_t) bounding_box.width; x++)
2061 GetPixelInfoPixel(image,q,&pixel);
2062 switch (gradient->spread)
2064 case UndefinedSpread:
2067 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
2068 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
2070 offset=GetStopColorOffset(gradient,x,y);
2071 if (gradient->type != RadialGradient)
2072 offset*=PerceptibleReciprocal(length);
2074 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2075 if (offset < gradient->stops[i].offset)
2077 if ((offset < 0.0) || (i == 0))
2078 composite=gradient->stops[0].color;
2080 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2081 composite=gradient->stops[gradient->number_stops-1].color;
2086 alpha=(offset-gradient->stops[i].offset)/
2087 (gradient->stops[j].offset-gradient->stops[i].offset);
2088 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2089 &gradient->stops[j].color,alpha,&composite);
2095 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
2096 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
2098 offset=GetStopColorOffset(gradient,x,y);
2099 if (gradient->type != RadialGradient)
2100 offset*=PerceptibleReciprocal(length);
2104 if ((ssize_t) fmod(offset,2.0) == 0)
2105 offset=fmod(offset,1.0);
2107 offset=1.0-fmod(offset,1.0);
2108 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2109 if (offset < gradient->stops[i].offset)
2112 composite=gradient->stops[0].color;
2114 if (i == (ssize_t) gradient->number_stops)
2115 composite=gradient->stops[gradient->number_stops-1].color;
2120 alpha=(offset-gradient->stops[i].offset)/
2121 (gradient->stops[j].offset-gradient->stops[i].offset);
2122 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2123 &gradient->stops[j].color,alpha,&composite);
2135 antialias=MagickFalse;
2137 if ((x != (ssize_t) ceil(gradient_vector->x1-0.5)) ||
2138 (y != (ssize_t) ceil(gradient_vector->y1-0.5)))
2140 offset=GetStopColorOffset(gradient,x,y);
2141 if (gradient->type == LinearGradient)
2143 repeat=fmod(offset,length);
2145 repeat=length-fmod(-repeat,length);
2147 repeat=fmod(offset,length);
2148 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2149 MagickTrue : MagickFalse;
2150 offset=PerceptibleReciprocal(length)*repeat;
2154 repeat=fmod(offset,gradient->radius);
2156 repeat=gradient->radius-fmod(-repeat,gradient->radius);
2158 repeat=fmod(offset,gradient->radius);
2159 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2161 offset=repeat/gradient->radius;
2164 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2165 if (offset < gradient->stops[i].offset)
2168 composite=gradient->stops[0].color;
2170 if (i == (ssize_t) gradient->number_stops)
2171 composite=gradient->stops[gradient->number_stops-1].color;
2176 alpha=(offset-gradient->stops[i].offset)/
2177 (gradient->stops[j].offset-gradient->stops[i].offset);
2178 if (antialias != MagickFalse)
2180 if (gradient->type == LinearGradient)
2181 alpha=length-repeat;
2183 alpha=gradient->radius-repeat;
2185 j=(ssize_t) gradient->number_stops-1L;
2187 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2188 &gradient->stops[j].color,alpha,&composite);
2193 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
2195 SetPixelViaPixelInfo(image,&pixel,q);
2196 q+=GetPixelChannels(image);
2198 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2201 image_view=DestroyCacheView(image_view);
2206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2210 % D r a w I m a g e %
2214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2216 % DrawImage() draws a graphic primitive on your image. The primitive
2217 % may be represented as a string or filename. Precede the filename with an
2218 % "at" sign (@) and the contents of the file are drawn on the image. You
2219 % can affect how text is drawn by setting one or more members of the draw
2222 % The format of the DrawImage method is:
2224 % MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
2225 % ExceptionInfo *exception)
2227 % A description of each parameter follows:
2229 % o image: the image.
2231 % o draw_info: the draw info.
2233 % o exception: return any errors or warnings in this structure.
2237 static MagickBooleanType CheckPrimitiveExtent(MVGInfo *mvg_info,
2247 Check if there is enough storage for drawing pimitives.
2249 extent=(double) mvg_info->offset+pad+PrimitiveExtentPad;
2250 quantum=sizeof(**mvg_info->primitive_info);
2251 if (((extent*quantum) < (double) SSIZE_MAX) &&
2252 ((extent*quantum) < (double) GetMaxMemoryRequest()))
2254 if (extent <= (double) *mvg_info->extent)
2256 *mvg_info->primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(
2257 *mvg_info->primitive_info,(size_t) extent,quantum);
2258 if (*mvg_info->primitive_info != (PrimitiveInfo *) NULL)
2260 (void) memset(*mvg_info->primitive_info+*mvg_info->extent,0,
2261 (extent-(*mvg_info->extent))*quantum);
2262 *mvg_info->extent=(size_t) extent;
2267 Reallocation failed, allocate a primitive to facilitate unwinding.
2269 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
2270 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2271 if (*mvg_info->primitive_info != (PrimitiveInfo *) NULL)
2272 *mvg_info->primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(
2273 *mvg_info->primitive_info);
2274 *mvg_info->primitive_info=(PrimitiveInfo *) AcquireCriticalMemory(
2275 PrimitiveExtentPad*quantum);
2276 (void) memset(*mvg_info->primitive_info,0,PrimitiveExtentPad*quantum);
2277 *mvg_info->extent=1;
2278 return(MagickFalse);
2281 static SplayTreeInfo *GetMVGMacros(const char *primitive)
2296 Scan graphic primitives for definitions and classes.
2298 if (primitive == (const char *) NULL)
2299 return((SplayTreeInfo *) NULL);
2300 macros=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
2301 RelinquishMagickMemory);
2302 token=AcquireString(primitive);
2303 extent=strlen(token)+MagickPathExtent;
2304 for (q=primitive; *q != '\0'; )
2306 GetNextToken(q,&q,extent,token);
2309 if (LocaleCompare("push",token) == 0)
2315 GetNextToken(q,&q,extent,token);
2319 name[MagickPathExtent];
2328 Named macro (e.g. push graphic-context "wheel").
2330 GetNextToken(q,&q,extent,token);
2333 (void) CopyMagickString(name,token,MagickPathExtent);
2335 for (p=q; *p != '\0'; )
2337 GetNextToken(p,&p,extent,token);
2340 if (LocaleCompare(token,"pop") == 0)
2342 end=p-strlen(token)-1;
2345 if (LocaleCompare(token,"push") == 0)
2347 if ((n == 0) && (end > start))
2355 GetNextToken(p,&p,extent,token);
2356 macro=AcquireString(start);
2357 macro[end-start]='\0';
2358 (void) AddValueToSplayTree(macros,ConstantString(name),
2359 ConstantString(macro));
2360 macro=DestroyString(macro);
2367 token=DestroyString(token);
2371 static inline MagickBooleanType IsPoint(const char *point)
2379 value=StringToDouble(point,&p);
2380 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2384 static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2385 const PointInfo point)
2387 primitive_info->coordinates=1;
2388 primitive_info->closed_subpath=MagickFalse;
2389 primitive_info->point=point;
2393 static MagickBooleanType RenderMVGContent(Image *image,
2394 const DrawInfo *draw_info,const size_t depth,ExceptionInfo *exception)
2396 #define RenderImageTag "Render/Image"
2403 keyword[MagickPathExtent],
2404 geometry[MagickPathExtent],
2406 pattern[MagickPathExtent],
2473 assert(image != (Image *) NULL);
2474 assert(image->signature == MagickCoreSignature);
2475 if (image->debug != MagickFalse)
2476 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2477 assert(draw_info != (DrawInfo *) NULL);
2478 assert(draw_info->signature == MagickCoreSignature);
2479 if (image->debug != MagickFalse)
2480 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2481 if (depth > MagickMaxRecursionDepth)
2482 ThrowBinaryException(DrawError,"VectorGraphicsNestedTooDeeply",
2484 if ((draw_info->primitive == (char *) NULL) ||
2485 (*draw_info->primitive == '\0'))
2486 return(MagickFalse);
2487 if (image->debug != MagickFalse)
2488 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
2489 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2490 return(MagickFalse);
2491 if (image->alpha_trait == UndefinedPixelTrait)
2493 status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2494 if (status == MagickFalse)
2495 return(status != 0 ? MagickTrue : MagickFalse);
2497 primitive=(char *) NULL;
2498 if (*draw_info->primitive != '@')
2499 primitive=AcquireString(draw_info->primitive);
2501 if ((strlen(draw_info->primitive) > 1) &&
2502 (*(draw_info->primitive+1) != '-'))
2503 primitive=FileToString(draw_info->primitive+1,~0UL,exception);
2504 if (primitive == (char *) NULL)
2505 return(MagickFalse);
2506 primitive_extent=(double) strlen(primitive);
2507 (void) SetImageArtifact(image,"MVG",primitive);
2510 stops=(StopInfo *) NULL;
2512 Allocate primitive info memory.
2514 graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2515 if (graphic_context == (DrawInfo **) NULL)
2517 primitive=DestroyString(primitive);
2518 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2521 number_points=PrimitiveExtentPad;
2522 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t) number_points,
2523 sizeof(*primitive_info));
2524 if (primitive_info == (PrimitiveInfo *) NULL)
2526 primitive=DestroyString(primitive);
2527 for ( ; n >= 0; n--)
2528 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2529 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2530 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2533 (void) memset(primitive_info,0,(size_t) number_points*
2534 sizeof(*primitive_info));
2535 mvg_info.primitive_info=(&primitive_info);
2536 mvg_info.extent=(&number_points);
2538 mvg_info.exception=exception;
2539 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2540 graphic_context[n]->viewbox=image->page;
2541 if ((image->page.width == 0) || (image->page.height == 0))
2543 graphic_context[n]->viewbox.width=image->columns;
2544 graphic_context[n]->viewbox.height=image->rows;
2546 token=AcquireString(primitive);
2547 extent=strlen(token)+MagickPathExtent;
2551 macros=GetMVGMacros(primitive);
2553 for (q=primitive; *q != '\0'; )
2556 Interpret graphic primitive.
2558 GetNextToken(q,&q,MagickPathExtent,keyword);
2559 if (*keyword == '\0')
2561 if (*keyword == '#')
2566 while ((*q != '\n') && (*q != '\0'))
2570 p=q-strlen(keyword)-1;
2571 primitive_type=UndefinedPrimitive;
2572 current=graphic_context[n]->affine;
2573 GetAffineMatrix(&affine);
2581 if (LocaleCompare("affine",keyword) == 0)
2583 GetNextToken(q,&q,extent,token);
2584 affine.sx=StringToDouble(token,&next_token);
2585 if (token == next_token)
2586 ThrowPointExpectedException(token,exception);
2587 GetNextToken(q,&q,extent,token);
2589 GetNextToken(q,&q,extent,token);
2590 affine.rx=StringToDouble(token,&next_token);
2591 if (token == next_token)
2592 ThrowPointExpectedException(token,exception);
2593 GetNextToken(q,&q,extent,token);
2595 GetNextToken(q,&q,extent,token);
2596 affine.ry=StringToDouble(token,&next_token);
2597 if (token == next_token)
2598 ThrowPointExpectedException(token,exception);
2599 GetNextToken(q,&q,extent,token);
2601 GetNextToken(q,&q,extent,token);
2602 affine.sy=StringToDouble(token,&next_token);
2603 if (token == next_token)
2604 ThrowPointExpectedException(token,exception);
2605 GetNextToken(q,&q,extent,token);
2607 GetNextToken(q,&q,extent,token);
2608 affine.tx=StringToDouble(token,&next_token);
2609 if (token == next_token)
2610 ThrowPointExpectedException(token,exception);
2611 GetNextToken(q,&q,extent,token);
2613 GetNextToken(q,&q,extent,token);
2614 affine.ty=StringToDouble(token,&next_token);
2615 if (token == next_token)
2616 ThrowPointExpectedException(token,exception);
2619 if (LocaleCompare("alpha",keyword) == 0)
2621 primitive_type=AlphaPrimitive;
2624 if (LocaleCompare("arc",keyword) == 0)
2626 primitive_type=ArcPrimitive;
2635 if (LocaleCompare("bezier",keyword) == 0)
2637 primitive_type=BezierPrimitive;
2640 if (LocaleCompare("border-color",keyword) == 0)
2642 GetNextToken(q,&q,extent,token);
2643 status&=QueryColorCompliance(token,AllCompliance,
2644 &graphic_context[n]->border_color,exception);
2653 if (LocaleCompare("class",keyword) == 0)
2658 GetNextToken(q,&q,extent,token);
2664 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2665 if (mvg_class != (const char *) NULL)
2674 Inject class elements in stream.
2676 offset=(ssize_t) (p-primitive);
2677 elements=AcquireString(primitive);
2678 elements[offset]='\0';
2679 (void) ConcatenateString(&elements,mvg_class);
2680 (void) ConcatenateString(&elements,"\n");
2681 (void) ConcatenateString(&elements,q);
2682 primitive=DestroyString(primitive);
2688 if (LocaleCompare("clip-path",keyword) == 0)
2694 Take a node from within the MVG document, and duplicate it here.
2696 GetNextToken(q,&q,extent,token);
2702 (void) CloneString(&graphic_context[n]->clip_mask,token);
2703 clip_path=(const char *) GetValueFromSplayTree(macros,token);
2704 if (clip_path != (const char *) NULL)
2706 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2707 graphic_context[n]->clipping_mask=
2708 DestroyImage(graphic_context[n]->clipping_mask);
2709 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2710 graphic_context[n],token,clip_path,exception);
2711 if (draw_info->compliance != SVGCompliance)
2712 status&=DrawClipPath(image,graphic_context[n],
2713 graphic_context[n]->clip_mask,exception);
2717 if (LocaleCompare("clip-rule",keyword) == 0)
2722 GetNextToken(q,&q,extent,token);
2723 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2725 if (fill_rule == -1)
2730 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2733 if (LocaleCompare("clip-units",keyword) == 0)
2738 GetNextToken(q,&q,extent,token);
2739 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2741 if (clip_units == -1)
2746 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2747 if (clip_units == ObjectBoundingBox)
2749 GetAffineMatrix(¤t);
2750 affine.sx=draw_info->bounds.x2;
2751 affine.sy=draw_info->bounds.y2;
2752 affine.tx=draw_info->bounds.x1;
2753 affine.ty=draw_info->bounds.y1;
2758 if (LocaleCompare("circle",keyword) == 0)
2760 primitive_type=CirclePrimitive;
2763 if (LocaleCompare("color",keyword) == 0)
2765 primitive_type=ColorPrimitive;
2768 if (LocaleCompare("compliance",keyword) == 0)
2771 MVG compliance associates a clipping mask with an image; SVG
2772 compliance associates a clipping mask with a graphics context.
2774 GetNextToken(q,&q,extent,token);
2775 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2776 MagickComplianceOptions,MagickFalse,token);
2785 if (LocaleCompare("decorate",keyword) == 0)
2790 GetNextToken(q,&q,extent,token);
2791 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2798 graphic_context[n]->decorate=(DecorationType) decorate;
2801 if (LocaleCompare("density",keyword) == 0)
2803 GetNextToken(q,&q,extent,token);
2804 (void) CloneString(&graphic_context[n]->density,token);
2807 if (LocaleCompare("direction",keyword) == 0)
2812 GetNextToken(q,&q,extent,token);
2813 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2815 if (direction == -1)
2818 graphic_context[n]->direction=(DirectionType) direction;
2827 if (LocaleCompare("ellipse",keyword) == 0)
2829 primitive_type=EllipsePrimitive;
2832 if (LocaleCompare("encoding",keyword) == 0)
2834 GetNextToken(q,&q,extent,token);
2835 (void) CloneString(&graphic_context[n]->encoding,token);
2844 if (LocaleCompare("fill",keyword) == 0)
2846 GetNextToken(q,&q,extent,token);
2847 if (graphic_context[n]->clip_path != MagickFalse)
2849 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
2850 if (GetImageArtifact(image,pattern) != (const char *) NULL)
2851 (void) DrawPatternPath(image,draw_info,token,
2852 &graphic_context[n]->fill_pattern,exception);
2855 status&=QueryColorCompliance(token,AllCompliance,
2856 &graphic_context[n]->fill,exception);
2857 if (graphic_context[n]->fill_alpha != OpaqueAlpha)
2858 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2862 if (LocaleCompare("fill-opacity",keyword) == 0)
2867 GetNextToken(q,&q,extent,token);
2868 if (graphic_context[n]->clip_path != MagickFalse)
2870 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
2871 opacity=MagickMin(MagickMax(factor*
2872 StringToDouble(token,&next_token),0.0),1.0);
2873 if (token == next_token)
2874 ThrowPointExpectedException(token,exception);
2875 graphic_context[n]->fill_alpha*=opacity;
2876 if (graphic_context[n]->fill_alpha != OpaqueAlpha)
2877 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2880 if (LocaleCompare("fill-rule",keyword) == 0)
2885 GetNextToken(q,&q,extent,token);
2886 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2888 if (fill_rule == -1)
2893 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2896 if (LocaleCompare("font",keyword) == 0)
2898 GetNextToken(q,&q,extent,token);
2899 (void) CloneString(&graphic_context[n]->font,token);
2900 if (LocaleCompare("none",token) == 0)
2901 graphic_context[n]->font=(char *) RelinquishMagickMemory(
2902 graphic_context[n]->font);
2905 if (LocaleCompare("font-family",keyword) == 0)
2907 GetNextToken(q,&q,extent,token);
2908 (void) CloneString(&graphic_context[n]->family,token);
2911 if (LocaleCompare("font-size",keyword) == 0)
2913 GetNextToken(q,&q,extent,token);
2914 graphic_context[n]->pointsize=StringToDouble(token,&next_token);
2915 if (token == next_token)
2916 ThrowPointExpectedException(token,exception);
2919 if (LocaleCompare("font-stretch",keyword) == 0)
2924 GetNextToken(q,&q,extent,token);
2925 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
2931 graphic_context[n]->stretch=(StretchType) stretch;
2934 if (LocaleCompare("font-style",keyword) == 0)
2939 GetNextToken(q,&q,extent,token);
2940 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
2946 graphic_context[n]->style=(StyleType) style;
2949 if (LocaleCompare("font-weight",keyword) == 0)
2954 GetNextToken(q,&q,extent,token);
2955 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
2957 weight=(ssize_t) StringToUnsignedLong(token);
2958 graphic_context[n]->weight=(size_t) weight;
2967 if (LocaleCompare("gradient-units",keyword) == 0)
2969 GetNextToken(q,&q,extent,token);
2972 if (LocaleCompare("gravity",keyword) == 0)
2977 GetNextToken(q,&q,extent,token);
2978 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
2984 graphic_context[n]->gravity=(GravityType) gravity;
2993 if (LocaleCompare("image",keyword) == 0)
2998 primitive_type=ImagePrimitive;
2999 GetNextToken(q,&q,extent,token);
3000 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3006 graphic_context[n]->compose=(CompositeOperator) compose;
3009 if (LocaleCompare("interline-spacing",keyword) == 0)
3011 GetNextToken(q,&q,extent,token);
3012 graphic_context[n]->interline_spacing=StringToDouble(token,
3014 if (token == next_token)
3015 ThrowPointExpectedException(token,exception);
3018 if (LocaleCompare("interword-spacing",keyword) == 0)
3020 GetNextToken(q,&q,extent,token);
3021 graphic_context[n]->interword_spacing=StringToDouble(token,
3023 if (token == next_token)
3024 ThrowPointExpectedException(token,exception);
3033 if (LocaleCompare("kerning",keyword) == 0)
3035 GetNextToken(q,&q,extent,token);
3036 graphic_context[n]->kerning=StringToDouble(token,&next_token);
3037 if (token == next_token)
3038 ThrowPointExpectedException(token,exception);
3047 if (LocaleCompare("letter-spacing",keyword) == 0)
3049 GetNextToken(q,&q,extent,token);
3050 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3051 clone_info->text=AcquireString(" ");
3052 status&=GetTypeMetrics(image,clone_info,&metrics,exception);
3053 graphic_context[n]->kerning=metrics.width*
3054 StringToDouble(token,&next_token);
3055 clone_info=DestroyDrawInfo(clone_info);
3056 if (token == next_token)
3057 ThrowPointExpectedException(token,exception);
3060 if (LocaleCompare("line",keyword) == 0)
3062 primitive_type=LinePrimitive;
3071 if (LocaleCompare("mask",keyword) == 0)
3077 Take a node from within the MVG document, and duplicate it here.
3079 GetNextToken(q,&q,extent,token);
3080 mask_path=(const char *) GetValueFromSplayTree(macros,token);
3081 if (mask_path != (const char *) NULL)
3083 if (graphic_context[n]->composite_mask != (Image *) NULL)
3084 graphic_context[n]->composite_mask=
3085 DestroyImage(graphic_context[n]->composite_mask);
3086 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3087 graphic_context[n],token,mask_path,exception);
3088 if (draw_info->compliance != SVGCompliance)
3089 status=SetImageMask(image,CompositePixelMask,
3090 graphic_context[n]->composite_mask,exception);
3099 if (LocaleCompare("offset",keyword) == 0)
3101 GetNextToken(q,&q,extent,token);
3104 if (LocaleCompare("opacity",keyword) == 0)
3109 GetNextToken(q,&q,extent,token);
3110 if (graphic_context[n]->clip_path != MagickFalse)
3112 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3113 opacity=MagickMin(MagickMax(factor*
3114 StringToDouble(token,&next_token),0.0),1.0);
3115 if (token == next_token)
3116 ThrowPointExpectedException(token,exception);
3117 graphic_context[n]->fill_alpha*=opacity;
3118 if (graphic_context[n]->fill_alpha != OpaqueAlpha)
3119 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3120 graphic_context[n]->stroke_alpha*=opacity;
3121 if (graphic_context[n]->stroke_alpha != OpaqueAlpha)
3122 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3131 if (LocaleCompare("path",keyword) == 0)
3133 primitive_type=PathPrimitive;
3136 if (LocaleCompare("point",keyword) == 0)
3138 primitive_type=PointPrimitive;
3141 if (LocaleCompare("polyline",keyword) == 0)
3143 primitive_type=PolylinePrimitive;
3146 if (LocaleCompare("polygon",keyword) == 0)
3148 primitive_type=PolygonPrimitive;
3151 if (LocaleCompare("pop",keyword) == 0)
3153 GetNextToken(q,&q,extent,token);
3154 if (LocaleCompare("class",token) == 0)
3156 if (LocaleCompare("clip-path",token) == 0)
3158 if (LocaleCompare("defs",token) == 0)
3161 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3165 if (LocaleCompare("gradient",token) == 0)
3167 if (LocaleCompare("graphic-context",token) == 0)
3171 (void) ThrowMagickException(exception,GetMagickModule(),
3172 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
3177 if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3178 (draw_info->compliance != SVGCompliance))
3179 if (LocaleCompare(graphic_context[n]->clip_mask,
3180 graphic_context[n-1]->clip_mask) != 0)
3181 status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3183 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3187 if (LocaleCompare("mask",token) == 0)
3189 if (LocaleCompare("pattern",token) == 0)
3191 if (LocaleCompare("symbol",token) == 0)
3194 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3201 if (LocaleCompare("push",keyword) == 0)
3203 GetNextToken(q,&q,extent,token);
3204 if (LocaleCompare("class",token) == 0)
3209 for (p=q; *q != '\0'; )
3211 GetNextToken(q,&q,extent,token);
3212 if (LocaleCompare(token,"pop") != 0)
3214 GetNextToken(q,(const char **) NULL,extent,token);
3215 if (LocaleCompare(token,"class") != 0)
3219 GetNextToken(q,&q,extent,token);
3222 if (LocaleCompare("clip-path",token) == 0)
3225 name[MaxTextExtent];
3230 GetNextToken(q,&q,extent,token);
3231 (void) FormatLocaleString(name,MaxTextExtent,"%s",token);
3232 clip_path=(const char *) GetValueFromSplayTree(macros,name);
3233 if (clip_path != (const char *) NULL)
3234 (void) SetImageArtifact(image,name,clip_path);
3237 if (LocaleCompare("defs",token) == 0)
3240 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3244 if (LocaleCompare("gradient",token) == 0)
3247 key[2*MagickPathExtent],
3248 name[MagickPathExtent],
3249 type[MagickPathExtent];
3254 GetNextToken(q,&q,extent,token);
3255 (void) CopyMagickString(name,token,MagickPathExtent);
3256 GetNextToken(q,&q,extent,token);
3257 (void) CopyMagickString(type,token,MagickPathExtent);
3258 GetNextToken(q,&q,extent,token);
3259 segment.x1=StringToDouble(token,&next_token);
3260 if (token == next_token)
3261 ThrowPointExpectedException(token,exception);
3262 GetNextToken(q,&q,extent,token);
3264 GetNextToken(q,&q,extent,token);
3265 segment.y1=StringToDouble(token,&next_token);
3266 if (token == next_token)
3267 ThrowPointExpectedException(token,exception);
3268 GetNextToken(q,&q,extent,token);
3270 GetNextToken(q,&q,extent,token);
3271 segment.x2=StringToDouble(token,&next_token);
3272 if (token == next_token)
3273 ThrowPointExpectedException(token,exception);
3274 GetNextToken(q,&q,extent,token);
3276 GetNextToken(q,&q,extent,token);
3277 segment.y2=StringToDouble(token,&next_token);
3278 if (token == next_token)
3279 ThrowPointExpectedException(token,exception);
3280 if (LocaleCompare(type,"radial") == 0)
3282 GetNextToken(q,&q,extent,token);
3284 GetNextToken(q,&q,extent,token);
3286 for (p=q; *q != '\0'; )
3288 GetNextToken(q,&q,extent,token);
3289 if (LocaleCompare(token,"pop") != 0)
3291 GetNextToken(q,(const char **) NULL,extent,token);
3292 if (LocaleCompare(token,"gradient") != 0)
3296 if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3301 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3302 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3303 graphic_context[n]->affine.ry*segment.y1+
3304 graphic_context[n]->affine.tx;
3305 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3306 graphic_context[n]->affine.sy*segment.y1+
3307 graphic_context[n]->affine.ty;
3308 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3309 graphic_context[n]->affine.ry*segment.y2+
3310 graphic_context[n]->affine.tx;
3311 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3312 graphic_context[n]->affine.sy*segment.y2+
3313 graphic_context[n]->affine.ty;
3314 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3315 (void) SetImageArtifact(image,key,token);
3316 (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
3317 (void) SetImageArtifact(image,key,type);
3318 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3320 (void) FormatLocaleString(geometry,MagickPathExtent,
3321 "%gx%g%+.15g%+.15g",
3322 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3323 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3324 bounds.x1,bounds.y1);
3325 (void) SetImageArtifact(image,key,geometry);
3326 GetNextToken(q,&q,extent,token);
3329 if (LocaleCompare("graphic-context",token) == 0)
3332 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3333 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3334 if (graphic_context == (DrawInfo **) NULL)
3336 (void) ThrowMagickException(exception,GetMagickModule(),
3337 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3341 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3342 graphic_context[n-1]);
3344 GetNextToken(q,&q,extent,token);
3347 if (LocaleCompare("mask",token) == 0)
3349 GetNextToken(q,&q,extent,token);
3352 if (LocaleCompare("pattern",token) == 0)
3355 key[2*MagickPathExtent],
3356 name[MagickPathExtent];
3361 GetNextToken(q,&q,extent,token);
3362 (void) CopyMagickString(name,token,MagickPathExtent);
3363 GetNextToken(q,&q,extent,token);
3364 bounds.x=(ssize_t) ceil(StringToDouble(token,&next_token)-0.5);
3365 if (token == next_token)
3366 ThrowPointExpectedException(token,exception);
3367 GetNextToken(q,&q,extent,token);
3369 GetNextToken(q,&q,extent,token);
3370 bounds.y=(ssize_t) ceil(StringToDouble(token,&next_token)-0.5);
3371 if (token == next_token)
3372 ThrowPointExpectedException(token,exception);
3373 GetNextToken(q,&q,extent,token);
3375 GetNextToken(q,&q,extent,token);
3376 bounds.width=(size_t) floor(StringToDouble(token,&next_token)+
3378 if (token == next_token)
3379 ThrowPointExpectedException(token,exception);
3380 GetNextToken(q,&q,extent,token);
3382 GetNextToken(q,&q,extent,token);
3383 bounds.height=(size_t) floor(StringToDouble(token,&next_token)+
3385 if (token == next_token)
3386 ThrowPointExpectedException(token,exception);
3387 for (p=q; *q != '\0'; )
3389 GetNextToken(q,&q,extent,token);
3390 if (LocaleCompare(token,"pop") != 0)
3392 GetNextToken(q,(const char **) NULL,extent,token);
3393 if (LocaleCompare(token,"pattern") != 0)
3397 if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3402 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3403 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3404 (void) SetImageArtifact(image,key,token);
3405 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3407 (void) FormatLocaleString(geometry,MagickPathExtent,
3408 "%.20gx%.20g%+.20g%+.20g",(double) bounds.width,(double)
3409 bounds.height,(double) bounds.x,(double) bounds.y);
3410 (void) SetImageArtifact(image,key,geometry);
3411 GetNextToken(q,&q,extent,token);
3414 if (LocaleCompare("symbol",token) == 0)
3417 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3430 if (LocaleCompare("rectangle",keyword) == 0)
3432 primitive_type=RectanglePrimitive;
3435 if (LocaleCompare("rotate",keyword) == 0)
3437 GetNextToken(q,&q,extent,token);
3438 angle=StringToDouble(token,&next_token);
3439 if (token == next_token)
3440 ThrowPointExpectedException(token,exception);
3441 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3442 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3443 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3444 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3447 if (LocaleCompare("roundRectangle",keyword) == 0)
3449 primitive_type=RoundRectanglePrimitive;
3458 if (LocaleCompare("scale",keyword) == 0)
3460 GetNextToken(q,&q,extent,token);
3461 affine.sx=StringToDouble(token,&next_token);
3462 if (token == next_token)
3463 ThrowPointExpectedException(token,exception);
3464 GetNextToken(q,&q,extent,token);
3466 GetNextToken(q,&q,extent,token);
3467 affine.sy=StringToDouble(token,&next_token);
3468 if (token == next_token)
3469 ThrowPointExpectedException(token,exception);
3472 if (LocaleCompare("skewX",keyword) == 0)
3474 GetNextToken(q,&q,extent,token);
3475 angle=StringToDouble(token,&next_token);
3476 if (token == next_token)
3477 ThrowPointExpectedException(token,exception);
3478 affine.ry=sin(DegreesToRadians(angle));
3481 if (LocaleCompare("skewY",keyword) == 0)
3483 GetNextToken(q,&q,extent,token);
3484 angle=StringToDouble(token,&next_token);
3485 if (token == next_token)
3486 ThrowPointExpectedException(token,exception);
3487 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3490 if (LocaleCompare("stop-color",keyword) == 0)
3496 if (number_stops == 1)
3497 stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
3499 if (number_stops > 2)
3500 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3502 if (stops == (StopInfo *) NULL)
3504 (void) ThrowMagickException(exception,GetMagickModule(),
3505 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3509 GetNextToken(q,&q,extent,token);
3510 status&=QueryColorCompliance(token,AllCompliance,&stop_color,
3512 stops[number_stops-1].color=stop_color;
3513 GetNextToken(q,&q,extent,token);
3514 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3515 stops[number_stops-1].offset=factor*StringToDouble(token,
3517 if (token == next_token)
3518 ThrowPointExpectedException(token,exception);
3521 if (LocaleCompare("stroke",keyword) == 0)
3523 GetNextToken(q,&q,extent,token);
3524 if (graphic_context[n]->clip_path != MagickFalse)
3526 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
3527 if (GetImageArtifact(image,pattern) != (const char *) NULL)
3528 (void) DrawPatternPath(image,draw_info,token,
3529 &graphic_context[n]->stroke_pattern,exception);
3532 status&=QueryColorCompliance(token,AllCompliance,
3533 &graphic_context[n]->stroke,exception);
3534 if (graphic_context[n]->stroke_alpha != OpaqueAlpha)
3535 graphic_context[n]->stroke.alpha=
3536 graphic_context[n]->stroke_alpha;
3540 if (LocaleCompare("stroke-antialias",keyword) == 0)
3542 GetNextToken(q,&q,extent,token);
3543 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3544 MagickTrue : MagickFalse;
3547 if (LocaleCompare("stroke-dasharray",keyword) == 0)
3549 if (graphic_context[n]->dash_pattern != (double *) NULL)
3550 graphic_context[n]->dash_pattern=(double *)
3551 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3552 if (IsPoint(q) != MagickFalse)
3558 GetNextToken(r,&r,extent,token);
3560 GetNextToken(r,&r,extent,token);
3561 for (x=0; IsPoint(token) != MagickFalse; x++)
3563 GetNextToken(r,&r,extent,token);
3565 GetNextToken(r,&r,extent,token);
3567 graphic_context[n]->dash_pattern=(double *)
3568 AcquireQuantumMemory((size_t) (2*x+4),
3569 sizeof(*graphic_context[n]->dash_pattern));
3570 if (graphic_context[n]->dash_pattern == (double *) NULL)
3572 (void) ThrowMagickException(exception,GetMagickModule(),
3573 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3578 (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3579 (2*x+4)*sizeof(*graphic_context[n]->dash_pattern));
3580 for (j=0; j < x; j++)
3582 GetNextToken(q,&q,extent,token);
3584 GetNextToken(q,&q,extent,token);
3585 graphic_context[n]->dash_pattern[j]=StringToDouble(token,
3587 if (token == next_token)
3588 ThrowPointExpectedException(token,exception);
3589 if (graphic_context[n]->dash_pattern[j] < 0.0)
3592 if ((x & 0x01) != 0)
3593 for ( ; j < (2*x); j++)
3594 graphic_context[n]->dash_pattern[j]=
3595 graphic_context[n]->dash_pattern[j-x];
3596 graphic_context[n]->dash_pattern[j]=0.0;
3599 GetNextToken(q,&q,extent,token);
3602 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3604 GetNextToken(q,&q,extent,token);
3605 graphic_context[n]->dash_offset=StringToDouble(token,&next_token);
3606 if (token == next_token)
3607 ThrowPointExpectedException(token,exception);
3610 if (LocaleCompare("stroke-linecap",keyword) == 0)
3615 GetNextToken(q,&q,extent,token);
3616 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3622 graphic_context[n]->linecap=(LineCap) linecap;
3625 if (LocaleCompare("stroke-linejoin",keyword) == 0)
3630 GetNextToken(q,&q,extent,token);
3631 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3638 graphic_context[n]->linejoin=(LineJoin) linejoin;
3641 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3643 GetNextToken(q,&q,extent,token);
3644 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3647 if (LocaleCompare("stroke-opacity",keyword) == 0)
3652 GetNextToken(q,&q,extent,token);
3653 if (graphic_context[n]->clip_path != MagickFalse)
3655 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3656 opacity=MagickMin(MagickMax(factor*
3657 StringToDouble(token,&next_token),0.0),1.0);
3658 if (token == next_token)
3659 ThrowPointExpectedException(token,exception);
3660 graphic_context[n]->stroke_alpha*=opacity;
3661 if (graphic_context[n]->stroke_alpha != OpaqueAlpha)
3662 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3665 if (LocaleCompare("stroke-width",keyword) == 0)
3667 GetNextToken(q,&q,extent,token);
3668 if (graphic_context[n]->clip_path != MagickFalse)
3670 graphic_context[n]->stroke_width=StringToDouble(token,&next_token);
3671 if (token == next_token)
3672 ThrowPointExpectedException(token,exception);
3681 if (LocaleCompare("text",keyword) == 0)
3683 primitive_type=TextPrimitive;
3687 if (LocaleCompare("text-align",keyword) == 0)
3692 GetNextToken(q,&q,extent,token);
3693 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3699 graphic_context[n]->align=(AlignType) align;
3702 if (LocaleCompare("text-anchor",keyword) == 0)
3707 GetNextToken(q,&q,extent,token);
3708 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3714 graphic_context[n]->align=(AlignType) align;
3717 if (LocaleCompare("text-antialias",keyword) == 0)
3719 GetNextToken(q,&q,extent,token);
3720 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3721 MagickTrue : MagickFalse;
3724 if (LocaleCompare("text-undercolor",keyword) == 0)
3726 GetNextToken(q,&q,extent,token);
3727 status&=QueryColorCompliance(token,AllCompliance,
3728 &graphic_context[n]->undercolor,exception);
3731 if (LocaleCompare("translate",keyword) == 0)
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);
3739 GetNextToken(q,&q,extent,token);
3740 affine.ty=StringToDouble(token,&next_token);
3741 if (token == next_token)
3742 ThrowPointExpectedException(token,exception);
3752 if (LocaleCompare("use",keyword) == 0)
3758 Get a macro from the MVG document, and "use" it here.
3760 GetNextToken(q,&q,extent,token);
3761 use=(const char *) GetValueFromSplayTree(macros,token);
3762 if (use != (const char *) NULL)
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);
3776 if (LocaleCompare("viewbox",keyword) == 0)
3778 GetNextToken(q,&q,extent,token);
3779 graphic_context[n]->viewbox.x=(ssize_t) ceil(StringToDouble(token,
3781 if (token == next_token)
3782 ThrowPointExpectedException(token,exception);
3783 GetNextToken(q,&q,extent,token);
3785 GetNextToken(q,&q,extent,token);
3786 graphic_context[n]->viewbox.y=(ssize_t) ceil(StringToDouble(token,
3788 if (token == next_token)
3789 ThrowPointExpectedException(token,exception);
3790 GetNextToken(q,&q,extent,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);
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);
3812 if (LocaleCompare("word-spacing",keyword) == 0)
3814 GetNextToken(q,&q,extent,token);
3815 graphic_context[n]->interword_spacing=StringToDouble(token,
3817 if (token == next_token)
3818 ThrowPointExpectedException(token,exception);
3830 if (status == MagickFalse)
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))
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+
3843 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
3846 if (primitive_type == UndefinedPrimitive)
3850 if (number_stops > 1)
3855 type=LinearGradient;
3856 if (draw_info->gradient.type == RadialGradient)
3857 type=RadialGradient;
3858 (void) GradientImage(image,type,PadSpread,stops,number_stops,
3861 if (number_stops > 0)
3862 stops=(StopInfo *) RelinquishMagickMemory(stops);
3864 if ((image->debug != MagickFalse) && (q > p))
3865 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
3870 Parse the primitive attributes.
3875 primitive_info[0].point.x=0.0;
3876 primitive_info[0].point.y=0.0;
3877 primitive_info[0].coordinates=0;
3878 primitive_info[0].method=FloodfillMethod;
3879 primitive_info[0].closed_subpath=MagickFalse;
3880 for (x=0; *q != '\0'; x++)
3885 if (IsPoint(q) == MagickFalse)
3887 GetNextToken(q,&q,extent,token);
3888 point.x=StringToDouble(token,&next_token);
3889 if (token == next_token)
3890 ThrowPointExpectedException(token,exception);
3891 GetNextToken(q,&q,extent,token);
3893 GetNextToken(q,&q,extent,token);
3894 point.y=StringToDouble(token,&next_token);
3895 if (token == next_token)
3896 ThrowPointExpectedException(token,exception);
3897 GetNextToken(q,(const char **) NULL,extent,token);
3899 GetNextToken(q,&q,extent,token);
3900 primitive_info[i].primitive=primitive_type;
3901 primitive_info[i].point=point;
3902 primitive_info[i].coordinates=0;
3903 primitive_info[i].method=FloodfillMethod;
3904 primitive_info[i].closed_subpath=MagickFalse;
3907 if (i < (ssize_t) number_points)
3909 status&=CheckPrimitiveExtent(&mvg_info,number_points);
3911 if (status == MagickFalse)
3913 primitive_info[j].primitive=primitive_type;
3914 primitive_info[j].coordinates=(size_t) x;
3915 primitive_info[j].method=FloodfillMethod;
3916 primitive_info[j].closed_subpath=MagickFalse;
3917 primitive_info[j].text=(char *) NULL;
3919 Circumscribe primitive within a circle.
3921 bounds.x1=primitive_info[j].point.x;
3922 bounds.y1=primitive_info[j].point.y;
3923 bounds.x2=primitive_info[j].point.x;
3924 bounds.y2=primitive_info[j].point.y;
3925 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
3927 point=primitive_info[j+k].point;
3928 if (point.x < bounds.x1)
3930 if (point.y < bounds.y1)
3932 if (point.x > bounds.x2)
3934 if (point.y > bounds.y2)
3938 Speculate how many points our primitive might consume.
3940 coordinates=(double) primitive_info[j].coordinates;
3941 switch (primitive_type)
3943 case RectanglePrimitive:
3948 case RoundRectanglePrimitive:
3955 alpha=bounds.x2-bounds.x1;
3956 beta=bounds.y2-bounds.y1;
3957 radius=hypot((double) alpha,(double) beta);
3959 coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
3960 BezierQuantum+360.0;
3963 case BezierPrimitive:
3965 coordinates=(double) (BezierQuantum*primitive_info[j].coordinates);
3966 if (primitive_info[j].coordinates > (107*BezierQuantum))
3968 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
3969 "TooManyBezierCoordinates","`%s'",token);
3981 GetNextToken(q,&q,extent,token);
3984 for (s=token; *s != '\0'; s=t)
3989 value=StringToDouble(s,&t);
3998 for (s=token; *s != '\0'; s++)
3999 if (strspn(s,"AaCcQqSsTt") != 0)
4000 coordinates+=(20.0*BezierQuantum)+360.0;
4003 case CirclePrimitive:
4005 case EllipsePrimitive:
4012 alpha=bounds.x2-bounds.x1;
4013 beta=bounds.y2-bounds.y1;
4014 radius=hypot(alpha,beta);
4015 coordinates=2.0*(ceil(MagickPI*radius))+6.0*BezierQuantum+360.0;
4016 if (coordinates > (MaxBezierCoordinates/4))
4018 (void) ThrowMagickException(exception,GetMagickModule(),DrawError,
4019 "TooManyBezierCoordinates","`%s'",token);
4027 if (coordinates > MaxBezierCoordinates)
4029 (void) ThrowMagickException(exception,GetMagickModule(),
4030 ResourceLimitError,"MemoryAllocationFailed","`%s'",token);
4033 if (status == MagickFalse)
4035 if (((size_t) (i+coordinates)) >= number_points)
4038 Resize based on speculative points required by primitive.
4040 number_points+=coordinates+1;
4041 if (number_points < (size_t) coordinates)
4043 (void) ThrowMagickException(exception,GetMagickModule(),
4044 ResourceLimitError,"MemoryAllocationFailed","`%s'",
4049 status&=CheckPrimitiveExtent(&mvg_info,number_points);
4051 status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4052 if (status == MagickFalse)
4055 switch (primitive_type)
4057 case PointPrimitive:
4060 if (primitive_info[j].coordinates != 1)
4065 status&=TracePoint(primitive_info+j,primitive_info[j].point);
4066 i=(ssize_t) (j+primitive_info[j].coordinates);
4071 if (primitive_info[j].coordinates != 2)
4076 status&=TraceLine(primitive_info+j,primitive_info[j].point,
4077 primitive_info[j+1].point);
4078 i=(ssize_t) (j+primitive_info[j].coordinates);
4081 case RectanglePrimitive:
4083 if (primitive_info[j].coordinates != 2)
4088 status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4089 primitive_info[j+1].point);
4090 i=(ssize_t) (j+primitive_info[j].coordinates);
4093 case RoundRectanglePrimitive:
4095 if (primitive_info[j].coordinates != 3)
4100 if ((primitive_info[j+2].point.x < 0.0) ||
4101 (primitive_info[j+2].point.y < 0.0))
4106 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4111 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4116 status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4117 primitive_info[j+1].point,primitive_info[j+2].point);
4118 i=(ssize_t) (j+primitive_info[j].coordinates);
4123 if (primitive_info[j].coordinates != 3)
4125 primitive_type=UndefinedPrimitive;
4128 status&=TraceArc(&mvg_info,primitive_info[j].point,
4129 primitive_info[j+1].point,primitive_info[j+2].point);
4130 i=(ssize_t) (j+primitive_info[j].coordinates);
4133 case EllipsePrimitive:
4135 if (primitive_info[j].coordinates != 3)
4140 if ((primitive_info[j+1].point.x < 0.0) ||
4141 (primitive_info[j+1].point.y < 0.0))
4146 status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4147 primitive_info[j+1].point,primitive_info[j+2].point);
4148 i=(ssize_t) (j+primitive_info[j].coordinates);
4151 case CirclePrimitive:
4153 if (primitive_info[j].coordinates != 2)
4158 status&=TraceCircle(&mvg_info,primitive_info[j].point,
4159 primitive_info[j+1].point);
4160 i=(ssize_t) (j+primitive_info[j].coordinates);
4163 case PolylinePrimitive:
4165 if (primitive_info[j].coordinates < 1)
4172 case PolygonPrimitive:
4174 if (primitive_info[j].coordinates < 3)
4179 primitive_info[i]=primitive_info[j];
4180 primitive_info[i].coordinates=0;
4181 primitive_info[j].coordinates++;
4182 primitive_info[j].closed_subpath=MagickTrue;
4186 case BezierPrimitive:
4188 if (primitive_info[j].coordinates < 3)
4193 status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4194 i=(ssize_t) (j+primitive_info[j].coordinates);
4199 coordinates=(double) TracePath(&mvg_info,token,exception);
4200 if (coordinates == 0.0)
4205 i=(ssize_t) (j+coordinates);
4208 case AlphaPrimitive:
4209 case ColorPrimitive:
4214 if (primitive_info[j].coordinates != 1)
4219 GetNextToken(q,&q,extent,token);
4220 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4226 primitive_info[j].method=(PaintMethod) method;
4232 geometry[MagickPathExtent];
4234 if (primitive_info[j].coordinates != 1)
4240 GetNextToken(q,&q,extent,token);
4241 (void) CloneString(&primitive_info[j].text,token);
4243 Compute text cursor offset.
4245 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4246 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4247 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4249 mvg_info.point=primitive_info->point;
4250 primitive_info->point.x+=cursor;
4254 mvg_info.point=primitive_info->point;
4257 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
4258 primitive_info->point.x,primitive_info->point.y);
4259 clone_info->render=MagickFalse;
4260 clone_info->text=AcquireString(token);
4261 status&=GetTypeMetrics(image,clone_info,&metrics,exception);
4262 clone_info=DestroyDrawInfo(clone_info);
4263 cursor+=metrics.width;
4266 case ImagePrimitive:
4268 if (primitive_info[j].coordinates != 2)
4273 GetNextToken(q,&q,extent,token);
4274 (void) CloneString(&primitive_info[j].text,token);
4279 if ((image->debug != MagickFalse) && (q > p))
4280 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p-1),
4282 if (status == MagickFalse)
4284 primitive_info[i].primitive=UndefinedPrimitive;
4290 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4292 point=primitive_info[i].point;
4293 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4294 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4295 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4296 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4297 point=primitive_info[i].point;
4298 if (point.x < graphic_context[n]->bounds.x1)
4299 graphic_context[n]->bounds.x1=point.x;
4300 if (point.y < graphic_context[n]->bounds.y1)
4301 graphic_context[n]->bounds.y1=point.y;
4302 if (point.x > graphic_context[n]->bounds.x2)
4303 graphic_context[n]->bounds.x2=point.x;
4304 if (point.y > graphic_context[n]->bounds.y2)
4305 graphic_context[n]->bounds.y2=point.y;
4306 if (primitive_info[i].primitive == ImagePrimitive)
4308 if (i >= (ssize_t) number_points)
4309 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4311 if (graphic_context[n]->render != MagickFalse)
4313 if ((n != 0) && (draw_info->compliance != SVGCompliance) &&
4314 (graphic_context[n]->clip_mask != (char *) NULL) &&
4315 (LocaleCompare(graphic_context[n]->clip_mask,
4316 graphic_context[n-1]->clip_mask) != 0))
4317 status&=DrawClipPath(image,graphic_context[n],
4318 graphic_context[n]->clip_mask,exception);
4319 status&=DrawPrimitive(image,graphic_context[n],primitive_info,
4322 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4324 if (proceed == MagickFalse)
4329 if (image->debug != MagickFalse)
4330 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4332 Relinquish resources.
4334 macros=DestroySplayTree(macros);
4335 token=DestroyString(token);
4336 if (primitive_info != (PrimitiveInfo *) NULL)
4338 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4339 if ((primitive_info[i].primitive == TextPrimitive) ||
4340 (primitive_info[i].primitive == ImagePrimitive))
4341 if (primitive_info[i].text != (char *) NULL)
4342 primitive_info[i].text=DestroyString(primitive_info[i].text);
4343 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4345 primitive=DestroyString(primitive);
4346 if (stops != (StopInfo *) NULL)
4347 stops=(StopInfo *) RelinquishMagickMemory(stops);
4348 for ( ; n >= 0; n--)
4349 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4350 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4351 if (status == MagickFalse)
4352 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
4354 return(status != 0 ? MagickTrue : MagickFalse);
4357 MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
4358 ExceptionInfo *exception)
4360 return(RenderMVGContent(image,draw_info,1,exception));
4364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4368 % D r a w P a t t e r n P a t h %
4372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4374 % DrawPatternPath() draws a pattern.
4376 % The format of the DrawPatternPath method is:
4378 % MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4379 % const char *name,Image **pattern,ExceptionInfo *exception)
4381 % A description of each parameter follows:
4383 % o image: the image.
4385 % o draw_info: the draw info.
4387 % o name: the pattern name.
4389 % o image: the image.
4391 % o exception: return any errors or warnings in this structure.
4394 MagickExport MagickBooleanType DrawPatternPath(Image *image,
4395 const DrawInfo *draw_info,const char *name,Image **pattern,
4396 ExceptionInfo *exception)
4399 property[MagickPathExtent];
4415 assert(image != (Image *) NULL);
4416 assert(image->signature == MagickCoreSignature);
4417 if (image->debug != MagickFalse)
4418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4419 assert(draw_info != (const DrawInfo *) NULL);
4420 assert(name != (const char *) NULL);
4421 (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
4422 path=GetImageArtifact(image,property);
4423 if (path == (const char *) NULL)
4424 return(MagickFalse);
4425 (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
4426 geometry=GetImageArtifact(image,property);
4427 if (geometry == (const char *) NULL)
4428 return(MagickFalse);
4429 if ((*pattern) != (Image *) NULL)
4430 *pattern=DestroyImage(*pattern);
4431 image_info=AcquireImageInfo();
4432 image_info->size=AcquireString(geometry);
4433 *pattern=AcquireImage(image_info,exception);
4434 image_info=DestroyImageInfo(image_info);
4435 (void) QueryColorCompliance("#000000ff",AllCompliance,
4436 &(*pattern)->background_color,exception);
4437 (void) SetImageBackgroundColor(*pattern,exception);
4438 if (image->debug != MagickFalse)
4439 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4440 "begin pattern-path %s %s",name,geometry);
4441 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4442 clone_info->fill_pattern=NewImageList();
4443 clone_info->stroke_pattern=NewImageList();
4444 (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
4445 type=GetImageArtifact(image,property);
4446 if (type != (const char *) NULL)
4447 clone_info->gradient.type=(GradientType) ParseCommandOption(
4448 MagickGradientOptions,MagickFalse,type);
4449 (void) CloneString(&clone_info->primitive,path);
4450 status=RenderMVGContent(*pattern,clone_info,1,exception);
4451 clone_info=DestroyDrawInfo(clone_info);
4452 if (image->debug != MagickFalse)
4453 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4462 + D r a w P o l y g o n P r i m i t i v e %
4466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4468 % DrawPolygonPrimitive() draws a polygon on the image.
4470 % The format of the DrawPolygonPrimitive method is:
4472 % MagickBooleanType DrawPolygonPrimitive(Image *image,
4473 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4474 % ExceptionInfo *exception)
4476 % A description of each parameter follows:
4478 % o image: the image.
4480 % o draw_info: the draw info.
4482 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4484 % o exception: return any errors or warnings in this structure.
4488 static PolygonInfo **DestroyPolygonThreadSet(PolygonInfo **polygon_info)
4493 assert(polygon_info != (PolygonInfo **) NULL);
4494 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4495 if (polygon_info[i] != (PolygonInfo *) NULL)
4496 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4497 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4498 return(polygon_info);
4501 static PolygonInfo **AcquirePolygonThreadSet(
4502 const PrimitiveInfo *primitive_info)
4505 *magick_restrict path_info;
4516 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4517 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4518 sizeof(*polygon_info));
4519 if (polygon_info == (PolygonInfo **) NULL)
4520 return((PolygonInfo **) NULL);
4521 (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4522 path_info=ConvertPrimitiveToPath(primitive_info);
4523 if (path_info == (PathInfo *) NULL)
4524 return(DestroyPolygonThreadSet(polygon_info));
4525 for (i=0; i < (ssize_t) number_threads; i++)
4527 polygon_info[i]=ConvertPathToPolygon(path_info);
4528 if (polygon_info[i] == (PolygonInfo *) NULL)
4529 return(DestroyPolygonThreadSet(polygon_info));
4531 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4532 return(polygon_info);
4535 static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
4536 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4537 const ssize_t y,double *stroke_alpha)
4548 register const PointInfo
4562 Compute fill & stroke opacity for this (x,y) point.
4566 p=polygon_info->edges;
4567 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4569 if ((double) y <= (p->bounds.y1-mid-0.5))
4571 if ((double) y > (p->bounds.y2+mid+0.5))
4573 (void) DestroyEdge(polygon_info,(size_t) j);
4576 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
4577 ((double) x > (p->bounds.x2+mid+0.5)))
4579 i=(ssize_t) MagickMax((double) p->highwater,1.0);
4580 for ( ; i < (ssize_t) p->number_points; i++)
4582 if ((double) y <= (p->points[i-1].y-mid-0.5))
4584 if ((double) y > (p->points[i].y+mid+0.5))
4586 if (p->scanline != (double) y)
4588 p->scanline=(double) y;
4589 p->highwater=(size_t) i;
4592 Compute distance between a point and an edge.
4595 delta.x=(q+1)->x-q->x;
4596 delta.y=(q+1)->y-q->y;
4597 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4600 delta.x=(double) x-q->x;
4601 delta.y=(double) y-q->y;
4602 distance=delta.x*delta.x+delta.y*delta.y;
4606 alpha=delta.x*delta.x+delta.y*delta.y;
4609 delta.x=(double) x-(q+1)->x;
4610 delta.y=(double) y-(q+1)->y;
4611 distance=delta.x*delta.x+delta.y*delta.y;
4615 alpha=PerceptibleReciprocal(alpha);
4616 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4617 distance=alpha*beta*beta;
4621 Compute stroke & subpath opacity.
4624 if (p->ghostline == MagickFalse)
4627 if ((*stroke_alpha < 1.0) &&
4628 (distance <= ((alpha+0.25)*(alpha+0.25))))
4631 if (distance <= ((alpha+0.25)*(alpha+0.25)))
4636 if (fabs(distance-1.0) >= MagickEpsilon)
4637 beta=sqrt((double) distance);
4639 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
4640 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
4644 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
4646 if (distance <= 0.0)
4653 if (fabs(beta) < MagickEpsilon)
4656 if (fabs(distance-1.0) >= MagickEpsilon)
4657 beta=sqrt(distance);
4660 if (subpath_alpha < (alpha*alpha))
4661 subpath_alpha=alpha*alpha;
4665 Compute fill opacity.
4667 if (fill == MagickFalse)
4669 if (subpath_alpha >= 1.0)
4672 Determine winding number.
4675 p=polygon_info->edges;
4676 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4678 if ((double) y <= p->bounds.y1)
4680 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
4682 if ((double) x > p->bounds.x2)
4684 winding_number+=p->direction ? 1 : -1;
4687 i=(ssize_t) MagickMax((double) p->highwater,1.0);
4688 for ( ; i < (ssize_t) (p->number_points-1); i++)
4689 if ((double) y <= p->points[i].y)
4692 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4693 winding_number+=p->direction ? 1 : -1;
4695 if (fill_rule != NonZeroRule)
4697 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4701 if (MagickAbsoluteValue(winding_number) != 0)
4703 return(subpath_alpha);
4706 static MagickBooleanType DrawPolygonPrimitive(Image *image,
4707 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4708 ExceptionInfo *exception)
4721 **magick_restrict polygon_info;
4737 assert(image != (Image *) NULL);
4738 assert(image->signature == MagickCoreSignature);
4739 if (image->debug != MagickFalse)
4740 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4741 assert(draw_info != (DrawInfo *) NULL);
4742 assert(draw_info->signature == MagickCoreSignature);
4743 assert(primitive_info != (PrimitiveInfo *) NULL);
4744 if (primitive_info->coordinates <= 1)
4747 Compute bounding box.
4749 polygon_info=AcquirePolygonThreadSet(primitive_info);
4750 if (polygon_info == (PolygonInfo **) NULL)
4751 return(MagickFalse);
4752 DisableMSCWarning(4127)
4755 status=DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
4756 if (status == MagickFalse)
4758 polygon_info=DestroyPolygonThreadSet(polygon_info);
4763 if (image->debug != MagickFalse)
4764 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
4765 fill=(primitive_info->method == FillToBorderMethod) ||
4766 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
4767 mid=ExpandAffine(&draw_info->affine)*SaneStrokeWidth(image,draw_info)/2.0;
4768 bounds=polygon_info[0]->edges[0].bounds;
4769 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
4771 p=polygon_info[0]->edges+i;
4772 if (p->bounds.x1 < bounds.x1)
4773 bounds.x1=p->bounds.x1;
4774 if (p->bounds.y1 < bounds.y1)
4775 bounds.y1=p->bounds.y1;
4776 if (p->bounds.x2 > bounds.x2)
4777 bounds.x2=p->bounds.x2;
4778 if (p->bounds.y2 > bounds.y2)
4779 bounds.y2=p->bounds.y2;
4781 bounds.x1-=(mid+1.0);
4782 bounds.y1-=(mid+1.0);
4783 bounds.x2+=(mid+1.0);
4784 bounds.y2+=(mid+1.0);
4785 if ((bounds.x1 >= (double) image->columns) ||
4786 (bounds.y1 >= (double) image->rows) ||
4787 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
4789 polygon_info=DestroyPolygonThreadSet(polygon_info);
4790 return(MagickTrue); /* virtual polygon */
4792 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
4793 (double) image->columns-1.0 : bounds.x1;
4794 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
4795 (double) image->rows-1.0 : bounds.y1;
4796 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
4797 (double) image->columns-1.0 : bounds.x2;
4798 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
4799 (double) image->rows-1.0 : bounds.y2;
4801 image_view=AcquireAuthenticCacheView(image,exception);
4802 if ((primitive_info->coordinates == 1) ||
4803 (polygon_info[0]->number_edges == 0))
4808 start_y=(ssize_t) ceil(bounds.y1-0.5);
4809 stop_y=(ssize_t) floor(bounds.y2+0.5);
4810 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4811 #pragma omp parallel for schedule(static) shared(status) \
4812 magick_number_threads(image,image,stop_y-start_y+1,1)
4814 for (y=start_y; y <= stop_y; y++)
4832 if (status == MagickFalse)
4834 start_x=(ssize_t) ceil(bounds.x1-0.5);
4835 stop_x=(ssize_t) floor(bounds.x2+0.5);
4837 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (stop_x-x+1),1,
4839 if (q == (Quantum *) NULL)
4844 GetPixelInfo(image,&pixel);
4845 for ( ; x <= stop_x; x++)
4847 if ((x == (ssize_t) ceil(primitive_info->point.x-0.5)) &&
4848 (y == (ssize_t) ceil(primitive_info->point.y-0.5)))
4850 GetFillColor(draw_info,x-start_x,y-start_y,&pixel,exception);
4851 SetPixelViaPixelInfo(image,&pixel,q);
4853 q+=GetPixelChannels(image);
4855 sync=SyncCacheViewAuthenticPixels(image_view,exception);
4856 if (sync == MagickFalse)
4859 image_view=DestroyCacheView(image_view);
4860 polygon_info=DestroyPolygonThreadSet(polygon_info);
4861 if (image->debug != MagickFalse)
4862 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4863 " end draw-polygon");
4867 Draw polygon or line.
4869 start_y=(ssize_t) ceil(bounds.y1-0.5);
4870 stop_y=(ssize_t) floor(bounds.y2+0.5);
4871 #if defined(MAGICKCORE_OPENMP_SUPPORT)
4872 #pragma omp parallel for schedule(static) shared(status) \
4873 magick_number_threads(image,image,stop_y-start_y+1,1)
4875 for (y=start_y; y <= stop_y; y++)
4878 id = GetOpenMPThreadId();
4890 if (status == MagickFalse)
4892 start_x=(ssize_t) ceil(bounds.x1-0.5);
4893 stop_x=(ssize_t) floor(bounds.x2+0.5);
4894 q=GetCacheViewAuthenticPixels(image_view,start_x,y,(size_t) (stop_x-start_x+
4896 if (q == (Quantum *) NULL)
4901 for (x=start_x; x <= stop_x; x++)
4914 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
4916 if (draw_info->stroke_antialias == MagickFalse)
4918 fill_alpha=fill_alpha > 0.25 ? 1.0 : 0.0;
4919 stroke_alpha=stroke_alpha > 0.25 ? 1.0 : 0.0;
4921 GetFillColor(draw_info,x-start_x,y-start_y,&fill_color,exception);
4922 CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
4923 (double) GetPixelAlpha(image,q),q);
4924 GetStrokeColor(draw_info,x-start_x,y-start_y,&stroke_color,exception);
4925 CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
4926 (double) GetPixelAlpha(image,q),q);
4927 q+=GetPixelChannels(image);
4929 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
4932 image_view=DestroyCacheView(image_view);
4933 polygon_info=DestroyPolygonThreadSet(polygon_info);
4934 if (image->debug != MagickFalse)
4935 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
4940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4944 % D r a w P r i m i t i v e %
4948 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4950 % DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
4952 % The format of the DrawPrimitive method is:
4954 % MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
4955 % PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4957 % A description of each parameter follows:
4959 % o image: the image.
4961 % o draw_info: the draw info.
4963 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4965 % o exception: return any errors or warnings in this structure.
4969 static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
4995 x=(ssize_t) ceil(primitive_info->point.x-0.5);
4996 y=(ssize_t) ceil(primitive_info->point.y-0.5);
4997 switch (primitive_info->primitive)
4999 case AlphaPrimitive:
5001 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5002 "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
5003 methods[primitive_info->method]);
5006 case ColorPrimitive:
5008 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5009 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
5010 methods[primitive_info->method]);
5013 case ImagePrimitive:
5015 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5016 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
5019 case PointPrimitive:
5021 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5022 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
5023 methods[primitive_info->method]);
5028 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5029 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
5036 p=primitive_info[0].point;
5039 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5041 point=primitive_info[i].point;
5042 if (coordinates <= 0)
5044 coordinates=(ssize_t) primitive_info[i].coordinates;
5045 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5046 " begin open (%.20g)",(double) coordinates);
5049 point=primitive_info[i].point;
5050 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5051 (fabs(q.y-point.y) >= MagickEpsilon))
5052 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5053 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5055 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5056 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5059 if (coordinates > 0)
5061 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5062 (fabs(p.y-point.y) >= MagickEpsilon))
5063 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
5064 (double) coordinates);
5066 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
5067 (double) coordinates);
5071 MagickExport MagickBooleanType DrawPrimitive(Image *image,
5072 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5073 ExceptionInfo *exception)
5088 if (image->debug != MagickFalse)
5090 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5091 " begin draw-primitive");
5092 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5093 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5094 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5095 draw_info->affine.tx,draw_info->affine.ty);
5098 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5099 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5100 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5101 status=SetImageColorspace(image,sRGBColorspace,exception);
5102 if (draw_info->compliance == SVGCompliance)
5104 status&=SetImageMask(image,WritePixelMask,draw_info->clipping_mask,
5106 status&=SetImageMask(image,CompositePixelMask,draw_info->composite_mask,
5109 x=(ssize_t) ceil(primitive_info->point.x-0.5);
5110 y=(ssize_t) ceil(primitive_info->point.y-0.5);
5111 image_view=AcquireAuthenticCacheView(image,exception);
5112 switch (primitive_info->primitive)
5114 case AlphaPrimitive:
5116 if (image->alpha_trait == UndefinedPixelTrait)
5117 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
5118 switch (primitive_info->method)
5129 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5130 if (q == (Quantum *) NULL)
5132 GetFillColor(draw_info,x,y,&pixel,exception);
5133 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5134 (void) SyncCacheViewAuthenticPixels(image_view,exception);
5146 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
5148 GetPixelInfo(image,&pixel);
5149 for (y=0; y < (ssize_t) image->rows; y++)
5154 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5156 if (q == (Quantum *) NULL)
5158 for (x=0; x < (ssize_t) image->columns; x++)
5160 GetPixelInfoPixel(image,q,&pixel);
5161 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5163 q+=GetPixelChannels(image);
5166 GetFillColor(draw_info,x,y,&pixel,exception);
5167 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5168 q+=GetPixelChannels(image);
5170 sync=SyncCacheViewAuthenticPixels(image_view,exception);
5171 if (sync == MagickFalse)
5176 case FloodfillMethod:
5177 case FillToBorderMethod:
5185 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
5187 if (primitive_info->method == FillToBorderMethod)
5189 target.red=(double) draw_info->border_color.red;
5190 target.green=(double) draw_info->border_color.green;
5191 target.blue=(double) draw_info->border_color.blue;
5193 channel_mask=SetImageChannelMask(image,AlphaChannel);
5194 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
5195 primitive_info->method == FloodfillMethod ? MagickFalse :
5196 MagickTrue,exception);
5197 (void) SetImageChannelMask(image,channel_mask);
5208 for (y=0; y < (ssize_t) image->rows; y++)
5213 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5215 if (q == (Quantum *) NULL)
5217 for (x=0; x < (ssize_t) image->columns; x++)
5219 GetFillColor(draw_info,x,y,&pixel,exception);
5220 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5221 q+=GetPixelChannels(image);
5223 sync=SyncCacheViewAuthenticPixels(image_view,exception);
5224 if (sync == MagickFalse)
5232 case ColorPrimitive:
5234 switch (primitive_info->method)
5245 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5246 if (q == (Quantum *) NULL)
5248 GetPixelInfo(image,&pixel);
5249 GetFillColor(draw_info,x,y,&pixel,exception);
5250 SetPixelViaPixelInfo(image,&pixel,q);
5251 (void) SyncCacheViewAuthenticPixels(image_view,exception);
5263 (void) GetOneCacheViewVirtualPixelInfo(image_view,x,y,&target,
5265 for (y=0; y < (ssize_t) image->rows; y++)
5270 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5272 if (q == (Quantum *) NULL)
5274 for (x=0; x < (ssize_t) image->columns; x++)
5276 GetPixelInfoPixel(image,q,&pixel);
5277 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5279 q+=GetPixelChannels(image);
5282 GetFillColor(draw_info,x,y,&pixel,exception);
5283 SetPixelViaPixelInfo(image,&pixel,q);
5284 q+=GetPixelChannels(image);
5286 sync=SyncCacheViewAuthenticPixels(image_view,exception);
5287 if (sync == MagickFalse)
5292 case FloodfillMethod:
5293 case FillToBorderMethod:
5298 (void) GetOneVirtualPixelInfo(image,TileVirtualPixelMethod,x,y,
5300 if (primitive_info->method == FillToBorderMethod)
5302 target.red=(double) draw_info->border_color.red;
5303 target.green=(double) draw_info->border_color.green;
5304 target.blue=(double) draw_info->border_color.blue;
5306 status&=FloodfillPaintImage(image,draw_info,&target,x,y,
5307 primitive_info->method == FloodfillMethod ? MagickFalse :
5308 MagickTrue,exception);
5319 GetPixelInfo(image,&pixel);
5320 for (y=0; y < (ssize_t) image->rows; y++)
5325 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5327 if (q == (Quantum *) NULL)
5329 for (x=0; x < (ssize_t) image->columns; x++)
5331 GetFillColor(draw_info,x,y,&pixel,exception);
5332 SetPixelViaPixelInfo(image,&pixel,q);
5333 q+=GetPixelChannels(image);
5335 sync=SyncCacheViewAuthenticPixels(image_view,exception);
5336 if (sync == MagickFalse)
5344 case ImagePrimitive:
5350 composite_geometry[MagickPathExtent];
5366 if (primitive_info->text == (char *) NULL)
5368 clone_info=AcquireImageInfo();
5369 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5370 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5374 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5376 composite_images=ReadImage(clone_info,exception);
5378 clone_info=DestroyImageInfo(clone_info);
5379 if (composite_images == (Image *) NULL)
5384 composite_image=RemoveFirstImageFromList(&composite_images);
5385 composite_images=DestroyImageList(composite_images);
5386 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5387 NULL,(void *) NULL);
5388 x1=(ssize_t) ceil(primitive_info[1].point.x-0.5);
5389 y1=(ssize_t) ceil(primitive_info[1].point.y-0.5);
5390 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5391 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5396 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5397 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
5398 composite_image->filter=image->filter;
5399 (void) TransformImage(&composite_image,(char *) NULL,
5400 composite_geometry,exception);
5402 if (composite_image->alpha_trait == UndefinedPixelTrait)
5403 (void) SetImageAlphaChannel(composite_image,OpaqueAlphaChannel,
5405 if (draw_info->alpha != OpaqueAlpha)
5406 (void) SetImageAlpha(composite_image,draw_info->alpha,exception);
5407 SetGeometry(image,&geometry);
5408 image->gravity=draw_info->gravity;
5411 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5412 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
5413 composite_image->rows,(double) geometry.x,(double) geometry.y);
5414 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
5415 affine=draw_info->affine;
5416 affine.tx=(double) geometry.x;
5417 affine.ty=(double) geometry.y;
5418 composite_image->interpolate=image->interpolate;
5419 status&=DrawAffineImage(image,composite_image,&affine,exception);
5420 composite_image=DestroyImage(composite_image);
5423 case PointPrimitive:
5431 if ((y < 0) || (y >= (ssize_t) image->rows))
5433 if ((x < 0) || (x >= (ssize_t) image->columns))
5435 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5436 if (q == (Quantum *) NULL)
5438 GetFillColor(draw_info,x,y,&fill_color,exception);
5439 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,
5440 (double) GetPixelAlpha(image,q),q);
5441 (void) SyncCacheViewAuthenticPixels(image_view,exception);
5447 geometry[MagickPathExtent];
5452 if (primitive_info->text == (char *) NULL)
5454 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5455 (void) CloneString(&clone_info->text,primitive_info->text);
5456 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
5457 primitive_info->point.x,primitive_info->point.y);
5458 (void) CloneString(&clone_info->geometry,geometry);
5459 status&=AnnotateImage(image,clone_info,exception);
5460 clone_info=DestroyDrawInfo(clone_info);
5472 if (IsEventLogging() != MagickFalse)
5473 LogPrimitiveInfo(primitive_info);
5474 scale=ExpandAffine(&draw_info->affine);
5475 if ((draw_info->dash_pattern != (double *) NULL) &&
5476 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5477 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5478 (draw_info->stroke.alpha != (Quantum) TransparentAlpha))
5483 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5484 clone_info->stroke_width=0.0;
5485 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5486 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
5488 clone_info=DestroyDrawInfo(clone_info);
5489 status=DrawDashPolygon(draw_info,primitive_info,image,exception);
5492 mid=ExpandAffine(&draw_info->affine)*SaneStrokeWidth(image,draw_info)/2.0;
5494 ((draw_info->stroke.alpha != (Quantum) TransparentAlpha) ||
5495 (draw_info->stroke_pattern != (Image *) NULL)))
5505 Draw strokes while respecting line cap/join attributes.
5507 closed_path=primitive_info[0].closed_subpath;
5508 i=(ssize_t) primitive_info[0].coordinates;
5509 x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5510 y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5511 if ((x < MagickEpsilon) && (y < MagickEpsilon))
5512 closed_path=MagickTrue;
5513 if ((((draw_info->linecap == RoundCap) ||
5514 (closed_path != MagickFalse)) &&
5515 (draw_info->linejoin == RoundJoin)) ||
5516 (primitive_info[i].primitive != UndefinedPrimitive))
5518 status=DrawPolygonPrimitive(image,draw_info,primitive_info,
5522 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5523 clone_info->stroke_width=0.0;
5524 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5525 status&=DrawPolygonPrimitive(image,clone_info,primitive_info,
5527 clone_info=DestroyDrawInfo(clone_info);
5528 status&=DrawStrokePolygon(image,draw_info,primitive_info,exception);
5531 status&=DrawPolygonPrimitive(image,draw_info,primitive_info,exception);
5535 image_view=DestroyCacheView(image_view);
5536 if (draw_info->compliance == SVGCompliance)
5538 status&=SetImageMask(image,WritePixelMask,(Image *) NULL,exception);
5539 status&=SetImageMask(image,CompositePixelMask,(Image *) NULL,exception);
5541 if (image->debug != MagickFalse)
5542 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
5543 return(status != 0 ? MagickTrue : MagickFalse);
5547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5551 + D r a w S t r o k e P o l y g o n %
5555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5557 % DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
5558 % the image while respecting the line cap and join attributes.
5560 % The format of the DrawStrokePolygon method is:
5562 % MagickBooleanType DrawStrokePolygon(Image *image,
5563 % const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5565 % A description of each parameter follows:
5567 % o image: the image.
5569 % o draw_info: the draw info.
5571 % o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5576 static MagickBooleanType DrawRoundLinecap(Image *image,
5577 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5578 ExceptionInfo *exception)
5586 for (i=0; i < 4; i++)
5587 linecap[i]=(*primitive_info);
5588 linecap[0].coordinates=4;
5589 linecap[1].point.x+=2.0*MagickEpsilon;
5590 linecap[2].point.x+=2.0*MagickEpsilon;
5591 linecap[2].point.y+=2.0*MagickEpsilon;
5592 linecap[3].point.y+=2.0*MagickEpsilon;
5593 linecap[4].primitive=UndefinedPrimitive;
5594 return(DrawPolygonPrimitive(image,draw_info,linecap,exception));
5597 static MagickBooleanType DrawStrokePolygon(Image *image,
5598 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5599 ExceptionInfo *exception)
5613 register const PrimitiveInfo
5618 Draw stroked polygon.
5620 if (image->debug != MagickFalse)
5621 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5622 " begin draw-stroke-polygon");
5623 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5624 clone_info->fill=draw_info->stroke;
5625 if (clone_info->fill_pattern != (Image *) NULL)
5626 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5627 if (clone_info->stroke_pattern != (Image *) NULL)
5628 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5629 MagickTrue,exception);
5630 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5631 clone_info->stroke_width=0.0;
5632 clone_info->fill_rule=NonZeroRule;
5634 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
5636 if (p->coordinates == 1)
5638 stroke_polygon=TraceStrokePolygon(image,draw_info,p);
5639 if (stroke_polygon == (PrimitiveInfo *) NULL)
5642 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5645 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon,exception);
5646 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5649 q=p+p->coordinates-1;
5650 closed_path=p->closed_subpath;
5651 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5653 status&=DrawRoundLinecap(image,draw_info,p,exception);
5654 status&=DrawRoundLinecap(image,draw_info,q,exception);
5657 clone_info=DestroyDrawInfo(clone_info);
5658 if (image->debug != MagickFalse)
5659 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5660 " end draw-stroke-polygon");
5661 return(status != 0 ? MagickTrue : MagickFalse);
5665 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5669 % G e t A f f i n e M a t r i x %
5673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5675 % GetAffineMatrix() returns an AffineMatrix initialized to the identity
5678 % The format of the GetAffineMatrix method is:
5680 % void GetAffineMatrix(AffineMatrix *affine_matrix)
5682 % A description of each parameter follows:
5684 % o affine_matrix: the affine matrix.
5687 MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
5689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5690 assert(affine_matrix != (AffineMatrix *) NULL);
5691 (void) memset(affine_matrix,0,sizeof(*affine_matrix));
5692 affine_matrix->sx=1.0;
5693 affine_matrix->sy=1.0;
5697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5701 + G e t D r a w I n f o %
5705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5707 % GetDrawInfo() initializes draw_info to default values from image_info.
5709 % The format of the GetDrawInfo method is:
5711 % void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5713 % A description of each parameter follows:
5715 % o image_info: the image info..
5717 % o draw_info: the draw info.
5720 MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5735 Initialize draw attributes.
5737 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5738 assert(draw_info != (DrawInfo *) NULL);
5739 (void) memset(draw_info,0,sizeof(*draw_info));
5740 clone_info=CloneImageInfo(image_info);
5741 GetAffineMatrix(&draw_info->affine);
5742 exception=AcquireExceptionInfo();
5743 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
5745 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
5747 draw_info->stroke_antialias=clone_info->antialias;
5748 draw_info->stroke_width=1.0;
5749 draw_info->fill_rule=EvenOddRule;
5750 draw_info->alpha=OpaqueAlpha;
5751 draw_info->fill_alpha=OpaqueAlpha;
5752 draw_info->stroke_alpha=OpaqueAlpha;
5753 draw_info->linecap=ButtCap;
5754 draw_info->linejoin=MiterJoin;
5755 draw_info->miterlimit=10;
5756 draw_info->decorate=NoDecoration;
5757 draw_info->pointsize=12.0;
5758 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
5759 draw_info->compose=OverCompositeOp;
5760 draw_info->render=MagickTrue;
5761 draw_info->clip_path=MagickFalse;
5762 draw_info->debug=IsEventLogging();
5763 if (clone_info->font != (char *) NULL)
5764 draw_info->font=AcquireString(clone_info->font);
5765 if (clone_info->density != (char *) NULL)
5766 draw_info->density=AcquireString(clone_info->density);
5767 draw_info->text_antialias=clone_info->antialias;
5768 if (fabs(clone_info->pointsize) >= MagickEpsilon)
5769 draw_info->pointsize=clone_info->pointsize;
5770 draw_info->border_color=clone_info->border_color;
5771 if (clone_info->server_name != (char *) NULL)
5772 draw_info->server_name=AcquireString(clone_info->server_name);
5773 option=GetImageOption(clone_info,"direction");
5774 if (option != (const char *) NULL)
5775 draw_info->direction=(DirectionType) ParseCommandOption(
5776 MagickDirectionOptions,MagickFalse,option);
5778 draw_info->direction=UndefinedDirection;
5779 option=GetImageOption(clone_info,"encoding");
5780 if (option != (const char *) NULL)
5781 (void) CloneString(&draw_info->encoding,option);
5782 option=GetImageOption(clone_info,"family");
5783 if (option != (const char *) NULL)
5784 (void) CloneString(&draw_info->family,option);
5785 option=GetImageOption(clone_info,"fill");
5786 if (option != (const char *) NULL)
5787 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
5789 option=GetImageOption(clone_info,"gravity");
5790 if (option != (const char *) NULL)
5791 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
5792 MagickFalse,option);
5793 option=GetImageOption(clone_info,"interline-spacing");
5794 if (option != (const char *) NULL)
5795 draw_info->interline_spacing=StringToDouble(option,&next_token);
5796 option=GetImageOption(clone_info,"interword-spacing");
5797 if (option != (const char *) NULL)
5798 draw_info->interword_spacing=StringToDouble(option,&next_token);
5799 option=GetImageOption(clone_info,"kerning");
5800 if (option != (const char *) NULL)
5801 draw_info->kerning=StringToDouble(option,&next_token);
5802 option=GetImageOption(clone_info,"stroke");
5803 if (option != (const char *) NULL)
5804 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
5806 option=GetImageOption(clone_info,"strokewidth");
5807 if (option != (const char *) NULL)
5808 draw_info->stroke_width=StringToDouble(option,&next_token);
5809 option=GetImageOption(clone_info,"style");
5810 if (option != (const char *) NULL)
5811 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
5812 MagickFalse,option);
5813 option=GetImageOption(clone_info,"undercolor");
5814 if (option != (const char *) NULL)
5815 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
5817 option=GetImageOption(clone_info,"weight");
5818 if (option != (const char *) NULL)
5823 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
5825 weight=(ssize_t) StringToUnsignedLong(option);
5826 draw_info->weight=(size_t) weight;
5828 exception=DestroyExceptionInfo(exception);
5829 draw_info->signature=MagickCoreSignature;
5830 clone_info=DestroyImageInfo(clone_info);
5834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5838 + P e r m u t a t e %
5842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5844 % Permutate() returns the permuation of the (n,k).
5846 % The format of the Permutate method is:
5848 % void Permutate(ssize_t n,ssize_t k)
5850 % A description of each parameter follows:
5858 static inline double Permutate(const ssize_t n,const ssize_t k)
5867 for (i=k+1; i <= n; i++)
5869 for (i=1; i <= (n-k); i++)
5875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5879 + T r a c e P r i m i t i v e %
5883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5885 % TracePrimitive is a collection of methods for generating graphic
5886 % primitives such as arcs, ellipses, paths, etc.
5890 static MagickBooleanType TraceArc(MVGInfo *mvg_info,const PointInfo start,
5891 const PointInfo end,const PointInfo degrees)
5897 center.x=0.5*(end.x+start.x);
5898 center.y=0.5*(end.y+start.y);
5899 radius.x=fabs(center.x-start.x);
5900 radius.y=fabs(center.y-start.y);
5901 return(TraceEllipse(mvg_info,center,radius,degrees));
5904 static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,const PointInfo start,
5905 const PointInfo end,const PointInfo arc,const double angle,
5906 const MagickBooleanType large_arc,const MagickBooleanType sweep)
5931 register PrimitiveInfo
5943 offset=mvg_info->offset;
5944 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
5945 primitive_info->coordinates=0;
5946 if ((fabs(start.x-end.x) < MagickEpsilon) &&
5947 (fabs(start.y-end.y) < MagickEpsilon))
5948 return(TracePoint(primitive_info,end));
5949 radii.x=fabs(arc.x);
5950 radii.y=fabs(arc.y);
5951 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
5952 return(TraceLine(primitive_info,start,end));
5953 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
5954 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
5955 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
5956 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
5957 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
5959 if (delta < MagickEpsilon)
5960 return(TraceLine(primitive_info,start,end));
5963 radii.x*=sqrt((double) delta);
5964 radii.y*=sqrt((double) delta);
5966 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
5967 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
5968 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
5969 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
5970 alpha=points[1].x-points[0].x;
5971 beta=points[1].y-points[0].y;
5972 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
5977 factor=sqrt((double) factor);
5978 if (sweep == large_arc)
5981 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
5982 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
5983 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
5984 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
5985 if ((theta < 0.0) && (sweep != MagickFalse))
5986 theta+=2.0*MagickPI;
5988 if ((theta > 0.0) && (sweep == MagickFalse))
5989 theta-=2.0*MagickPI;
5990 arc_segments=(size_t) ceil(fabs((double) (theta/(0.5*MagickPI+
5994 for (i=0; i < (ssize_t) arc_segments; i++)
5996 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
5997 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
5998 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
5999 sin(fmod((double) beta,DegreesToRadians(360.0)));
6000 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
6001 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
6002 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6003 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
6004 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
6005 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6006 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
6007 theta/arc_segments),DegreesToRadians(360.0))));
6008 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
6009 theta/arc_segments),DegreesToRadians(360.0))));
6010 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
6011 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6012 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
6013 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6014 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6015 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6016 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6018 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6020 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6022 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6024 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6026 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6028 if (i == (ssize_t) (arc_segments-1))
6030 if (TraceBezier(mvg_info,4) == MagickFalse)
6032 p=(*mvg_info->primitive_info)+mvg_info->offset;
6033 mvg_info->offset+=p->coordinates;
6036 mvg_info->offset=offset;
6037 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6038 primitive_info->coordinates=(size_t) (p-primitive_info);
6039 primitive_info->closed_subpath=MagickFalse;
6040 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6042 p->primitive=primitive_info->primitive;
6048 static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6049 const size_t number_coordinates)
6064 register PrimitiveInfo
6076 Allocate coefficients.
6078 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6079 quantum=number_coordinates;
6080 for (i=0; i < (ssize_t) number_coordinates; i++)
6082 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6084 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6085 if (alpha > (double) quantum)
6086 quantum=(size_t) alpha;
6087 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6088 if (alpha > (double) quantum)
6089 quantum=(size_t) alpha;
6092 quantum=(size_t) MagickMin((double) quantum/number_coordinates,
6093 (double) BezierQuantum);
6094 control_points=quantum*number_coordinates;
6095 if (CheckPrimitiveExtent(mvg_info,control_points+1) == MagickFalse)
6096 return(MagickFalse);
6097 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6098 coefficients=(double *) AcquireQuantumMemory((size_t)
6099 number_coordinates,sizeof(*coefficients));
6100 points=(PointInfo *) AcquireQuantumMemory((size_t) control_points,
6102 if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
6103 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
6105 Compute bezier points.
6107 end=primitive_info[number_coordinates-1].point;
6108 for (i=0; i < (ssize_t) number_coordinates; i++)
6109 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6111 for (i=0; i < (ssize_t) control_points; i++)
6116 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
6117 for (j=0; j < (ssize_t) number_coordinates; j++)
6119 point.x+=alpha*coefficients[j]*p->point.x;
6120 point.y+=alpha*coefficients[j]*p->point.y;
6121 alpha*=weight/(1.0-weight);
6125 weight+=1.0/control_points;
6128 Bezier curves are just short segmented polys.
6131 for (i=0; i < (ssize_t) control_points; i++)
6133 if (TracePoint(p,points[i]) == MagickFalse)
6134 return(MagickFalse);
6137 if (TracePoint(p,end) == MagickFalse)
6138 return(MagickFalse);
6140 primitive_info->coordinates=(size_t) (p-primitive_info);
6141 primitive_info->closed_subpath=MagickFalse;
6142 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6144 p->primitive=primitive_info->primitive;
6147 points=(PointInfo *) RelinquishMagickMemory(points);
6148 coefficients=(double *) RelinquishMagickMemory(coefficients);
6152 static MagickBooleanType TraceCircle(MVGInfo *mvg_info,const PointInfo start,
6153 const PointInfo end)
6164 alpha=end.x-start.x;
6166 radius=hypot((double) alpha,(double) beta);
6167 offset.x=(double) radius;
6168 offset.y=(double) radius;
6171 return(TraceEllipse(mvg_info,start,offset,degrees));
6174 static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,const PointInfo center,
6175 const PointInfo radii,const PointInfo arc)
6191 register PrimitiveInfo
6198 Ellipses are just short segmented polys.
6200 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6201 primitive_info->coordinates=0;
6202 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6204 delta=2.0*PerceptibleReciprocal(MagickMax(radii.x,radii.y));
6206 if ((delta >= 0.0) && (delta < (MagickPI/8.0)))
6207 step=MagickPI/4.0/(MagickPI*PerceptibleReciprocal(delta)/2.0);
6208 angle.x=DegreesToRadians(arc.x);
6212 angle.y=DegreesToRadians(y);
6213 coordinates=ceil((angle.y-angle.x)/step+1.0);
6214 if ((coordinates > (double) SSIZE_MAX) ||
6215 (coordinates > (double) GetMaxMemoryRequest()))
6217 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6218 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6219 return(MagickFalse);
6221 if (CheckPrimitiveExtent(mvg_info,(size_t) coordinates) == MagickFalse)
6222 return(MagickFalse);
6223 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6224 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6226 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6227 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6228 if (TracePoint(p,point) == MagickFalse)
6229 return(MagickFalse);
6232 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6233 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6234 if (TracePoint(p,point) == MagickFalse)
6235 return(MagickFalse);
6237 primitive_info->coordinates=(size_t) (p-primitive_info);
6238 primitive_info->closed_subpath=MagickFalse;
6239 x=fabs(primitive_info[0].point.x-
6240 primitive_info[primitive_info->coordinates-1].point.x);
6241 y=fabs(primitive_info[0].point.y-
6242 primitive_info[primitive_info->coordinates-1].point.y);
6243 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6244 primitive_info->closed_subpath=MagickTrue;
6245 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6247 p->primitive=primitive_info->primitive;
6253 static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6254 const PointInfo start,const PointInfo end)
6256 if (TracePoint(primitive_info,start) == MagickFalse)
6257 return(MagickFalse);
6258 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6259 (fabs(start.y-end.y) < MagickEpsilon))
6261 primitive_info->primitive=PointPrimitive;
6262 primitive_info->coordinates=1;
6265 if (TracePoint(primitive_info+1,end) == MagickFalse)
6266 return(MagickFalse);
6267 (primitive_info+1)->primitive=primitive_info->primitive;
6268 primitive_info->coordinates=2;
6269 primitive_info->closed_subpath=MagickFalse;
6273 static size_t TracePath(MVGInfo *mvg_info,const char *path,
6274 ExceptionInfo *exception)
6278 token[MagickPathExtent];
6296 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6306 register PrimitiveInfo
6319 subpath_offset=mvg_info->offset;
6320 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6323 number_coordinates=0;
6325 primitive_type=primitive_info->primitive;
6327 for (p=path; *p != '\0'; )
6329 if (status == MagickFalse)
6331 while (isspace((int) ((unsigned char) *p)) != 0)
6335 last_attribute=attribute;
6336 attribute=(int) (*p++);
6346 large_arc = MagickFalse,
6347 sweep = MagickFalse;
6357 GetNextToken(p,&p,MagickPathExtent,token);
6359 GetNextToken(p,&p,MagickPathExtent,token);
6360 arc.x=StringToDouble(token,&next_token);
6361 if (token == next_token)
6362 ThrowPointExpectedException(token,exception);
6363 GetNextToken(p,&p,MagickPathExtent,token);
6365 GetNextToken(p,&p,MagickPathExtent,token);
6366 arc.y=StringToDouble(token,&next_token);
6367 if (token == next_token)
6368 ThrowPointExpectedException(token,exception);
6369 GetNextToken(p,&p,MagickPathExtent,token);
6371 GetNextToken(p,&p,MagickPathExtent,token);
6372 angle=StringToDouble(token,&next_token);
6373 if (token == next_token)
6374 ThrowPointExpectedException(token,exception);
6375 GetNextToken(p,&p,MagickPathExtent,token);
6377 GetNextToken(p,&p,MagickPathExtent,token);
6378 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6379 GetNextToken(p,&p,MagickPathExtent,token);
6381 GetNextToken(p,&p,MagickPathExtent,token);
6382 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6384 GetNextToken(p,&p,MagickPathExtent,token);
6385 GetNextToken(p,&p,MagickPathExtent,token);
6387 GetNextToken(p,&p,MagickPathExtent,token);
6388 x=StringToDouble(token,&next_token);
6389 if (token == next_token)
6390 ThrowPointExpectedException(token,exception);
6391 GetNextToken(p,&p,MagickPathExtent,token);
6393 GetNextToken(p,&p,MagickPathExtent,token);
6394 y=StringToDouble(token,&next_token);
6395 if (token == next_token)
6396 ThrowPointExpectedException(token,exception);
6397 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
6398 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
6399 if (TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep) == MagickFalse)
6401 q=(*mvg_info->primitive_info)+mvg_info->offset;
6402 mvg_info->offset+=q->coordinates;
6405 while (isspace((int) ((unsigned char) *p)) != 0)
6409 } while (IsPoint(p) != MagickFalse);
6416 Cubic Bézier curve.
6421 for (i=1; i < 4; i++)
6423 GetNextToken(p,&p,MagickPathExtent,token);
6425 GetNextToken(p,&p,MagickPathExtent,token);
6426 x=StringToDouble(token,&next_token);
6427 if (token == next_token)
6428 ThrowPointExpectedException(token,exception);
6429 GetNextToken(p,&p,MagickPathExtent,token);
6431 GetNextToken(p,&p,MagickPathExtent,token);
6432 y=StringToDouble(token,&next_token);
6433 if (token == next_token)
6434 ThrowPointExpectedException(token,exception);
6435 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
6436 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
6439 for (i=0; i < 4; i++)
6440 (q+i)->point=points[i];
6441 if (TraceBezier(mvg_info,4) == MagickFalse)
6443 q=(*mvg_info->primitive_info)+mvg_info->offset;
6444 mvg_info->offset+=q->coordinates;
6447 while (isspace((int) ((unsigned char) *p)) != 0)
6451 } while (IsPoint(p) != MagickFalse);
6459 GetNextToken(p,&p,MagickPathExtent,token);
6461 GetNextToken(p,&p,MagickPathExtent,token);
6462 x=StringToDouble(token,&next_token);
6463 if (token == next_token)
6464 ThrowPointExpectedException(token,exception);
6465 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
6466 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6468 q=(*mvg_info->primitive_info)+mvg_info->offset;
6469 if (TracePoint(q,point) == MagickFalse)
6471 mvg_info->offset+=q->coordinates;
6473 while (isspace((int) ((unsigned char) *p)) != 0)
6477 } while (IsPoint(p) != MagickFalse);
6488 GetNextToken(p,&p,MagickPathExtent,token);
6490 GetNextToken(p,&p,MagickPathExtent,token);
6491 x=StringToDouble(token,&next_token);
6492 if (token == next_token)
6493 ThrowPointExpectedException(token,exception);
6494 GetNextToken(p,&p,MagickPathExtent,token);
6496 GetNextToken(p,&p,MagickPathExtent,token);
6497 y=StringToDouble(token,&next_token);
6498 if (token == next_token)
6499 ThrowPointExpectedException(token,exception);
6500 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
6501 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
6502 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6504 q=(*mvg_info->primitive_info)+mvg_info->offset;
6505 if (TracePoint(q,point) == MagickFalse)
6507 mvg_info->offset+=q->coordinates;
6509 while (isspace((int) ((unsigned char) *p)) != 0)
6513 } while (IsPoint(p) != MagickFalse);
6522 if (mvg_info->offset != subpath_offset)
6524 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6525 primitive_info->coordinates=(size_t) (q-primitive_info);
6526 number_coordinates+=primitive_info->coordinates;
6528 subpath_offset=mvg_info->offset;
6533 GetNextToken(p,&p,MagickPathExtent,token);
6535 GetNextToken(p,&p,MagickPathExtent,token);
6536 x=StringToDouble(token,&next_token);
6537 if (token == next_token)
6538 ThrowPointExpectedException(token,exception);
6539 GetNextToken(p,&p,MagickPathExtent,token);
6541 GetNextToken(p,&p,MagickPathExtent,token);
6542 y=StringToDouble(token,&next_token);
6543 if (token == next_token)
6544 ThrowPointExpectedException(token,exception);
6545 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
6546 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
6550 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6552 q=(*mvg_info->primitive_info)+mvg_info->offset;
6553 if (TracePoint(q,point) == MagickFalse)
6555 mvg_info->offset+=q->coordinates;
6557 while (isspace((int) ((unsigned char) *p)) != 0)
6561 } while (IsPoint(p) != MagickFalse);
6568 Quadratic Bézier curve.
6573 for (i=1; i < 3; i++)
6575 GetNextToken(p,&p,MagickPathExtent,token);
6577 GetNextToken(p,&p,MagickPathExtent,token);
6578 x=StringToDouble(token,&next_token);
6579 if (token == next_token)
6580 ThrowPointExpectedException(token,exception);
6581 GetNextToken(p,&p,MagickPathExtent,token);
6583 GetNextToken(p,&p,MagickPathExtent,token);
6584 y=StringToDouble(token,&next_token);
6585 if (token == next_token)
6586 ThrowPointExpectedException(token,exception);
6589 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
6590 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
6593 for (i=0; i < 3; i++)
6594 (q+i)->point=points[i];
6595 if (TraceBezier(mvg_info,3) == MagickFalse)
6597 q=(*mvg_info->primitive_info)+mvg_info->offset;
6598 mvg_info->offset+=q->coordinates;
6601 while (isspace((int) ((unsigned char) *p)) != 0)
6605 } while (IsPoint(p) != MagickFalse);
6612 Cubic Bézier curve.
6616 points[0]=points[3];
6617 points[1].x=2.0*points[3].x-points[2].x;
6618 points[1].y=2.0*points[3].y-points[2].y;
6619 for (i=2; i < 4; i++)
6621 GetNextToken(p,&p,MagickPathExtent,token);
6623 GetNextToken(p,&p,MagickPathExtent,token);
6624 x=StringToDouble(token,&next_token);
6625 if (token == next_token)
6626 ThrowPointExpectedException(token,exception);
6627 GetNextToken(p,&p,MagickPathExtent,token);
6629 GetNextToken(p,&p,MagickPathExtent,token);
6630 y=StringToDouble(token,&next_token);
6631 if (token == next_token)
6632 ThrowPointExpectedException(token,exception);
6635 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
6636 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
6639 if (strchr("CcSs",last_attribute) == (char *) NULL)
6644 for (i=0; i < 4; i++)
6645 (q+i)->point=points[i];
6646 if (TraceBezier(mvg_info,4) == MagickFalse)
6648 q=(*mvg_info->primitive_info)+mvg_info->offset;
6649 mvg_info->offset+=q->coordinates;
6652 last_attribute=attribute;
6653 while (isspace((int) ((unsigned char) *p)) != 0)
6657 } while (IsPoint(p) != MagickFalse);
6664 Quadratic Bézier curve.
6668 points[0]=points[2];
6669 points[1].x=2.0*points[2].x-points[1].x;
6670 points[1].y=2.0*points[2].y-points[1].y;
6671 for (i=2; i < 3; i++)
6673 GetNextToken(p,&p,MagickPathExtent,token);
6675 GetNextToken(p,&p,MagickPathExtent,token);
6676 x=StringToDouble(token,&next_token);
6677 if (token == next_token)
6678 ThrowPointExpectedException(token,exception);
6679 GetNextToken(p,&p,MagickPathExtent,token);
6681 GetNextToken(p,&p,MagickPathExtent,token);
6682 y=StringToDouble(token,&next_token);
6683 if (token == next_token)
6684 ThrowPointExpectedException(token,exception);
6685 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
6686 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
6689 if (status == MagickFalse)
6691 if (strchr("QqTt",last_attribute) == (char *) NULL)
6696 for (i=0; i < 3; i++)
6697 (q+i)->point=points[i];
6698 if (TraceBezier(mvg_info,3) == MagickFalse)
6700 q=(*mvg_info->primitive_info)+mvg_info->offset;
6701 mvg_info->offset+=q->coordinates;
6704 last_attribute=attribute;
6705 while (isspace((int) ((unsigned char) *p)) != 0)
6709 } while (IsPoint(p) != MagickFalse);
6720 GetNextToken(p,&p,MagickPathExtent,token);
6722 GetNextToken(p,&p,MagickPathExtent,token);
6723 y=StringToDouble(token,&next_token);
6724 if (token == next_token)
6725 ThrowPointExpectedException(token,exception);
6726 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
6727 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6729 q=(*mvg_info->primitive_info)+mvg_info->offset;
6730 if (TracePoint(q,point) == MagickFalse)
6732 mvg_info->offset+=q->coordinates;
6734 while (isspace((int) ((unsigned char) *p)) != 0)
6738 } while (IsPoint(p) != MagickFalse);
6748 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6750 q=(*mvg_info->primitive_info)+mvg_info->offset;
6751 if (TracePoint(q,point) == MagickFalse)
6753 mvg_info->offset+=q->coordinates;
6755 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6756 primitive_info->coordinates=(size_t) (q-primitive_info);
6757 primitive_info->closed_subpath=MagickTrue;
6758 number_coordinates+=primitive_info->coordinates;
6760 subpath_offset=mvg_info->offset;
6766 ThrowPointExpectedException(token,exception);
6771 if (status == MagickFalse)
6773 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6774 primitive_info->coordinates=(size_t) (q-primitive_info);
6775 number_coordinates+=primitive_info->coordinates;
6776 for (i=0; i < (ssize_t) number_coordinates; i++)
6779 q->primitive=primitive_type;
6781 q->method=FillToBorderMethod;
6784 return(number_coordinates);
6787 static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
6788 const PointInfo start,const PointInfo end)
6793 register PrimitiveInfo
6799 if ((fabs(start.x-end.x) < MagickEpsilon) ||
6800 (fabs(start.y-end.y) < MagickEpsilon))
6802 primitive_info->coordinates=0;
6806 if (TracePoint(p,start) == MagickFalse)
6807 return(MagickFalse);
6811 if (TracePoint(p,point) == MagickFalse)
6812 return(MagickFalse);
6814 if (TracePoint(p,end) == MagickFalse)
6815 return(MagickFalse);
6819 if (TracePoint(p,point) == MagickFalse)
6820 return(MagickFalse);
6822 if (TracePoint(p,start) == MagickFalse)
6823 return(MagickFalse);
6825 primitive_info->coordinates=(size_t) (p-primitive_info);
6826 primitive_info->closed_subpath=MagickTrue;
6827 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6829 p->primitive=primitive_info->primitive;
6835 static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
6836 const PointInfo start,const PointInfo end,PointInfo arc)
6846 register PrimitiveInfo
6855 offset=mvg_info->offset;
6856 segment.x=fabs(end.x-start.x);
6857 segment.y=fabs(end.y-start.y);
6858 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
6860 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
6863 if (arc.x > (0.5*segment.x))
6864 arc.x=0.5*segment.x;
6865 if (arc.y > (0.5*segment.y))
6866 arc.y=0.5*segment.y;
6867 point.x=start.x+segment.x-arc.x;
6868 point.y=start.y+arc.y;
6871 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
6872 return(MagickFalse);
6873 p=(*mvg_info->primitive_info)+mvg_info->offset;
6874 mvg_info->offset+=p->coordinates;
6875 point.x=start.x+segment.x-arc.x;
6876 point.y=start.y+segment.y-arc.y;
6879 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
6880 return(MagickFalse);
6881 p=(*mvg_info->primitive_info)+mvg_info->offset;
6882 mvg_info->offset+=p->coordinates;
6883 point.x=start.x+arc.x;
6884 point.y=start.y+segment.y-arc.y;
6887 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
6888 return(MagickFalse);
6889 p=(*mvg_info->primitive_info)+mvg_info->offset;
6890 mvg_info->offset+=p->coordinates;
6891 point.x=start.x+arc.x;
6892 point.y=start.y+arc.y;
6895 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
6896 return(MagickFalse);
6897 p=(*mvg_info->primitive_info)+mvg_info->offset;
6898 mvg_info->offset+=p->coordinates;
6899 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6900 return(MagickFalse);
6901 p=(*mvg_info->primitive_info)+mvg_info->offset;
6902 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
6903 return(MagickFalse);
6905 mvg_info->offset=offset;
6906 primitive_info=(*mvg_info->primitive_info)+offset;
6907 primitive_info->coordinates=(size_t) (p-primitive_info);
6908 primitive_info->closed_subpath=MagickTrue;
6909 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6911 p->primitive=primitive_info->primitive;
6917 static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
6918 const size_t number_vertices,const double offset)
6935 for (i=1; i < (ssize_t) number_vertices; i++)
6937 dx=primitive_info[0].point.x-primitive_info[i].point.x;
6938 dy=primitive_info[0].point.y-primitive_info[i].point.y;
6939 if ((fabs((double) dx) >= MagickEpsilon) ||
6940 (fabs((double) dy) >= MagickEpsilon))
6943 if (i == (ssize_t) number_vertices)
6944 i=(ssize_t) number_vertices-1L;
6945 distance=hypot((double) dx,(double) dy);
6946 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
6947 dx*(distance+offset)/distance);
6948 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
6949 dy*(distance+offset)/distance);
6950 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
6952 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
6953 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
6954 if ((fabs((double) dx) >= MagickEpsilon) ||
6955 (fabs((double) dy) >= MagickEpsilon))
6958 distance=hypot((double) dx,(double) dy);
6959 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
6960 dx*(distance+offset)/distance);
6961 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
6962 dy*(distance+offset)/distance);
6966 static PrimitiveInfo *TraceStrokePolygon(const Image *image,
6967 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
6969 #define CheckPathExtent(pad) \
6970 if ((ssize_t) (q+(pad)) >= (ssize_t) max_strokes) \
6972 if (~max_strokes < (pad)) \
6974 path_p=(PointInfo *) RelinquishMagickMemory(path_p); \
6975 path_q=(PointInfo *) RelinquishMagickMemory(path_q); \
6979 max_strokes+=(pad); \
6980 path_p=(PointInfo *) ResizeQuantumMemory(path_p,max_strokes, \
6982 path_q=(PointInfo *) ResizeQuantumMemory(path_q,max_strokes, \
6985 if ((path_p == (PointInfo *) NULL) || (path_q == (PointInfo *) NULL)) \
6987 if (path_p != (PointInfo *) NULL) \
6988 path_p=(PointInfo *) RelinquishMagickMemory(path_p); \
6989 if (path_q != (PointInfo *) NULL) \
6990 path_q=(PointInfo *) RelinquishMagickMemory(path_q); \
6991 polygon_primitive=(PrimitiveInfo *) \
6992 RelinquishMagickMemory(polygon_primitive); \
6993 return((PrimitiveInfo *) NULL); \
6997 typedef struct _LineSegment
7013 inverse_slope = {0,0},
7049 number_vertices=primitive_info->coordinates;
7050 max_strokes=2*number_vertices+6*BezierQuantum+360;
7051 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7052 number_vertices+2UL,sizeof(*polygon_primitive));
7053 if (polygon_primitive == (PrimitiveInfo *) NULL)
7054 return((PrimitiveInfo *) NULL);
7055 (void) memcpy(polygon_primitive,primitive_info,(size_t) number_vertices*
7056 sizeof(*polygon_primitive));
7057 closed_path=primitive_info[0].closed_subpath;
7058 if (((draw_info->linejoin == RoundJoin) ||
7059 (draw_info->linejoin == MiterJoin)) && (closed_path != MagickFalse))
7061 polygon_primitive[number_vertices]=primitive_info[1];
7064 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7066 Compute the slope for the first line segment, p.
7070 for (n=1; n < (ssize_t) number_vertices; n++)
7072 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7073 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7074 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7077 if (n == (ssize_t) number_vertices)
7079 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7082 Zero length subpath.
7084 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7085 sizeof(*stroke_polygon));
7086 stroke_polygon[0]=polygon_primitive[0];
7087 stroke_polygon[0].coordinates=0;
7088 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7090 return(stroke_polygon);
7092 n=(ssize_t) number_vertices-1L;
7094 path_p=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
7096 if (path_p == (PointInfo *) NULL)
7098 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7100 return((PrimitiveInfo *) NULL);
7102 path_q=(PointInfo *) AcquireQuantumMemory((size_t) max_strokes,
7104 if (path_q == (PointInfo *) NULL)
7106 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
7107 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7109 return((PrimitiveInfo *) NULL);
7112 inverse_slope.p=0.0;
7113 if (fabs(dx.p) < MagickEpsilon)
7116 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7118 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7121 if (fabs(dy.p) < MagickEpsilon)
7124 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7126 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7131 inverse_slope.p=(-1.0/slope.p);
7133 mid=ExpandAffine(&draw_info->affine)*SaneStrokeWidth(image,draw_info)/2.0;
7134 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7135 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7136 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7137 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7138 offset.y=(double) (offset.x*inverse_slope.p);
7139 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7141 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7142 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7143 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7144 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7145 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7146 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7147 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7148 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7152 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7153 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7154 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7155 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7156 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7157 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7158 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7159 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7162 Create strokes for the line join attribute: bevel, miter, round.
7166 path_q[p++]=box_q[0];
7167 path_p[q++]=box_p[0];
7168 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7171 Compute the slope for this line segment, q.
7173 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7174 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7175 dot_product=dx.q*dx.q+dy.q*dy.q;
7176 if (dot_product < 0.25)
7179 inverse_slope.q=0.0;
7180 if (fabs(dx.q) < MagickEpsilon)
7183 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7185 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7188 if (fabs(dy.q) < MagickEpsilon)
7191 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7193 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7198 inverse_slope.q=(-1.0/slope.q);
7200 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7201 offset.y=(double) (offset.x*inverse_slope.q);
7202 dot_product=dy.q*offset.x-dx.q*offset.y;
7203 if (dot_product > 0.0)
7205 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7206 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7207 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7208 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7209 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7210 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7211 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7212 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7216 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7217 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7218 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7219 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7220 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7221 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7222 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7223 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7225 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
7232 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7233 box_p[3].y)/(slope.p-slope.q));
7234 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7235 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7236 box_q[3].y)/(slope.p-slope.q));
7237 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7239 CheckPathExtent(6*BezierQuantum+360);
7240 dot_product=dx.q*dy.p-dx.p*dy.q;
7241 if (dot_product <= 0.0)
7242 switch (draw_info->linejoin)
7246 path_q[q++]=box_q[1];
7247 path_q[q++]=box_q[2];
7248 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7249 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7250 if (dot_product <= miterlimit)
7251 path_p[p++]=box_p[4];
7254 path_p[p++]=box_p[1];
7255 path_p[p++]=box_p[2];
7261 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7262 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7263 if (dot_product <= miterlimit)
7265 path_q[q++]=box_q[4];
7266 path_p[p++]=box_p[4];
7270 path_q[q++]=box_q[1];
7271 path_q[q++]=box_q[2];
7272 path_p[p++]=box_p[1];
7273 path_p[p++]=box_p[2];
7279 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7280 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7281 if (dot_product <= miterlimit)
7282 path_p[p++]=box_p[4];
7285 path_p[p++]=box_p[1];
7286 path_p[p++]=box_p[2];
7288 center=polygon_primitive[n].point;
7289 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7290 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7291 if (theta.q < theta.p)
7292 theta.q+=2.0*MagickPI;
7293 arc_segments=(size_t) ceil((double) ((theta.q-theta.p)/
7294 (2.0*sqrt((double) (1.0/mid)))));
7295 CheckPathExtent(arc_segments+6*BezierQuantum+360);
7296 path_q[q].x=box_q[1].x;
7297 path_q[q].y=box_q[1].y;
7299 for (j=1; j < (ssize_t) arc_segments; j++)
7301 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7302 path_q[q].x=(double) (center.x+mid*cos(fmod((double)
7303 (theta.p+delta_theta),DegreesToRadians(360.0))));
7304 path_q[q].y=(double) (center.y+mid*sin(fmod((double)
7305 (theta.p+delta_theta),DegreesToRadians(360.0))));
7308 path_q[q++]=box_q[2];
7315 switch (draw_info->linejoin)
7319 path_p[p++]=box_p[1];
7320 path_p[p++]=box_p[2];
7321 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7322 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7323 if (dot_product <= miterlimit)
7324 path_q[q++]=box_q[4];
7327 path_q[q++]=box_q[1];
7328 path_q[q++]=box_q[2];
7334 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7335 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7336 if (dot_product <= miterlimit)
7338 path_q[q++]=box_q[4];
7339 path_p[p++]=box_p[4];
7343 path_q[q++]=box_q[1];
7344 path_q[q++]=box_q[2];
7345 path_p[p++]=box_p[1];
7346 path_p[p++]=box_p[2];
7352 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7353 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7354 if (dot_product <= miterlimit)
7355 path_q[q++]=box_q[4];
7358 path_q[q++]=box_q[1];
7359 path_q[q++]=box_q[2];
7361 center=polygon_primitive[n].point;
7362 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7363 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7364 if (theta.p < theta.q)
7365 theta.p+=2.0*MagickPI;
7366 arc_segments=(size_t) ceil((double) ((theta.p-theta.q)/
7367 (2.0*sqrt((double) (1.0/mid)))));
7368 CheckPathExtent(arc_segments+6*BezierQuantum+360);
7369 path_p[p++]=box_p[1];
7370 for (j=1; j < (ssize_t) arc_segments; j++)
7372 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7373 path_p[p].x=(double) (center.x+mid*cos(fmod((double)
7374 (theta.p+delta_theta),DegreesToRadians(360.0))));
7375 path_p[p].y=(double) (center.y+mid*sin(fmod((double)
7376 (theta.p+delta_theta),DegreesToRadians(360.0))));
7379 path_p[p++]=box_p[2];
7386 inverse_slope.p=inverse_slope.q;
7395 path_p[p++]=box_p[1];
7396 path_q[q++]=box_q[1];
7398 Trace stroked polygon.
7400 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7401 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
7402 if (stroke_polygon != (PrimitiveInfo *) NULL)
7404 for (i=0; i < (ssize_t) p; i++)
7406 stroke_polygon[i]=polygon_primitive[0];
7407 stroke_polygon[i].point=path_p[i];
7409 if (closed_path != MagickFalse)
7411 stroke_polygon[i]=polygon_primitive[0];
7412 stroke_polygon[i].point=stroke_polygon[0].point;
7415 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7417 stroke_polygon[i]=polygon_primitive[0];
7418 stroke_polygon[i].point=path_q[p+q+closed_path-(i+1)];
7420 if (closed_path != MagickFalse)
7422 stroke_polygon[i]=polygon_primitive[0];
7423 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7426 stroke_polygon[i]=polygon_primitive[0];
7427 stroke_polygon[i].point=stroke_polygon[0].point;
7429 stroke_polygon[i].primitive=UndefinedPrimitive;
7430 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7432 path_p=(PointInfo *) RelinquishMagickMemory(path_p);
7433 path_q=(PointInfo *) RelinquishMagickMemory(path_q);
7434 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7435 return(stroke_polygon);