48#include "magick/studio.h"
49#include "magick/annotate.h"
50#include "magick/artifact.h"
51#include "magick/blob.h"
52#include "magick/cache.h"
53#include "magick/cache-private.h"
54#include "magick/cache-view.h"
55#include "magick/channel.h"
56#include "magick/color.h"
57#include "magick/color-private.h"
58#include "magick/colorspace.h"
59#include "magick/colorspace-private.h"
60#include "magick/composite.h"
61#include "magick/composite-private.h"
62#include "magick/constitute.h"
63#include "magick/draw.h"
64#include "magick/draw-private.h"
65#include "magick/enhance.h"
66#include "magick/exception.h"
67#include "magick/exception-private.h"
68#include "magick/gem.h"
69#include "magick/geometry.h"
70#include "magick/image-private.h"
71#include "magick/list.h"
72#include "magick/log.h"
73#include "magick/magick.h"
74#include "magick/memory-private.h"
75#include "magick/monitor.h"
76#include "magick/monitor-private.h"
77#include "magick/option.h"
78#include "magick/paint.h"
79#include "magick/pixel-accessor.h"
80#include "magick/pixel-private.h"
81#include "magick/property.h"
82#include "magick/resample.h"
83#include "magick/resample-private.h"
84#include "magick/resource_.h"
85#include "magick/splay-tree.h"
86#include "magick/string_.h"
87#include "magick/string-private.h"
88#include "magick/thread-private.h"
89#include "magick/token.h"
90#include "magick/transform.h"
91#include "magick/utility.h"
96#define AntialiasThreshold (1.0/3.0)
97#define BezierQuantum 200
98#define PrimitiveExtentPad 4296.0
99#define MaxBezierCoordinates 67108864
100#define ThrowPointExpectedException(image,token) \
102 (void) ThrowMagickException(&(image)->exception,GetMagickModule(),DrawError, \
103 "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
104 status=MagickFalse; \
194 *DrawClippingMask(Image *,
const DrawInfo *,
const char *,
const char *,
197static MagickBooleanType
198 DrawStrokePolygon(Image *,
const DrawInfo *,
const PrimitiveInfo *),
199 RenderMVGContent(Image *,
const DrawInfo *,
const size_t),
200 TraceArc(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo),
201 TraceArcPath(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo,
202 const double,
const MagickBooleanType,
const MagickBooleanType),
203 TraceBezier(MVGInfo *,
const size_t),
204 TraceCircle(MVGInfo *,
const PointInfo,
const PointInfo),
205 TraceEllipse(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo),
206 TraceLine(PrimitiveInfo *,
const PointInfo,
const PointInfo),
207 TraceRectangle(PrimitiveInfo *,
const PointInfo,
const PointInfo),
208 TraceRoundRectangle(MVGInfo *,
const PointInfo,
const PointInfo,PointInfo),
209 TraceSquareLinecap(PrimitiveInfo *,
const size_t,
const double);
212 *TraceStrokePolygon(
const DrawInfo *,
const PrimitiveInfo *,ExceptionInfo *);
215 TracePath(Image *,MVGInfo *,
const char *);
235MagickExport DrawInfo *AcquireDrawInfo(
void)
240 draw_info=(DrawInfo *) AcquireCriticalMemory(
sizeof(*draw_info));
241 GetDrawInfo((ImageInfo *) NULL,draw_info);
272MagickExport DrawInfo *CloneDrawInfo(
const ImageInfo *image_info,
273 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 if (draw_info->id != (
char *) NULL)
283 (void) CloneString(&clone_info->id,draw_info->id);
284 if (draw_info->primitive != (
char *) NULL)
285 (void) CloneString(&clone_info->primitive,draw_info->primitive);
286 if (draw_info->geometry != (
char *) NULL)
287 (void) CloneString(&clone_info->geometry,draw_info->geometry);
288 clone_info->compliance=draw_info->compliance;
289 clone_info->viewbox=draw_info->viewbox;
290 clone_info->affine=draw_info->affine;
291 clone_info->gravity=draw_info->gravity;
292 clone_info->fill=draw_info->fill;
293 clone_info->stroke=draw_info->stroke;
294 clone_info->stroke_width=draw_info->stroke_width;
295 if (draw_info->fill_pattern != (Image *) NULL)
296 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
297 &draw_info->fill_pattern->exception);
299 if (draw_info->tile != (Image *) NULL)
300 clone_info->fill_pattern=CloneImage(draw_info->tile,0,0,MagickTrue,
301 &draw_info->tile->exception);
302 clone_info->tile=NewImageList();
303 if (draw_info->stroke_pattern != (Image *) NULL)
304 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
305 MagickTrue,&draw_info->stroke_pattern->exception);
306 clone_info->stroke_antialias=draw_info->stroke_antialias;
307 clone_info->text_antialias=draw_info->text_antialias;
308 clone_info->fill_rule=draw_info->fill_rule;
309 clone_info->linecap=draw_info->linecap;
310 clone_info->linejoin=draw_info->linejoin;
311 clone_info->miterlimit=draw_info->miterlimit;
312 clone_info->dash_offset=draw_info->dash_offset;
313 clone_info->decorate=draw_info->decorate;
314 clone_info->compose=draw_info->compose;
315 if (draw_info->text != (
char *) NULL)
316 (void) CloneString(&clone_info->text,draw_info->text);
317 if (draw_info->font != (
char *) NULL)
318 (void) CloneString(&clone_info->font,draw_info->font);
319 if (draw_info->metrics != (
char *) NULL)
320 (void) CloneString(&clone_info->metrics,draw_info->metrics);
321 if (draw_info->family != (
char *) NULL)
322 (void) CloneString(&clone_info->family,draw_info->family);
323 clone_info->style=draw_info->style;
324 clone_info->stretch=draw_info->stretch;
325 clone_info->weight=draw_info->weight;
326 if (draw_info->encoding != (
char *) NULL)
327 (void) CloneString(&clone_info->encoding,draw_info->encoding);
328 clone_info->pointsize=draw_info->pointsize;
329 clone_info->kerning=draw_info->kerning;
330 clone_info->interline_spacing=draw_info->interline_spacing;
331 clone_info->interword_spacing=draw_info->interword_spacing;
332 clone_info->direction=draw_info->direction;
333 if (draw_info->density != (
char *) NULL)
334 (void) CloneString(&clone_info->density,draw_info->density);
335 clone_info->align=draw_info->align;
336 clone_info->undercolor=draw_info->undercolor;
337 clone_info->border_color=draw_info->border_color;
338 if (draw_info->server_name != (
char *) NULL)
339 (void) CloneString(&clone_info->server_name,draw_info->server_name);
340 if (draw_info->dash_pattern != (
double *) NULL)
345 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
346 clone_info->dash_pattern=(
double *) AcquireQuantumMemory((
size_t) (2*x+2),
347 sizeof(*clone_info->dash_pattern));
348 if (clone_info->dash_pattern == (
double *) NULL)
349 ThrowFatalException(ResourceLimitFatalError,
350 "UnableToAllocateDashPattern");
351 (void) memset(clone_info->dash_pattern,0,(
size_t) (2*x+2)*
352 sizeof(*clone_info->dash_pattern));
353 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(
size_t)
354 (x+1)*
sizeof(*clone_info->dash_pattern));
356 clone_info->gradient=draw_info->gradient;
357 if (draw_info->gradient.stops != (StopInfo *) NULL)
362 number_stops=clone_info->gradient.number_stops;
363 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((
size_t)
364 number_stops,
sizeof(*clone_info->gradient.stops));
365 if (clone_info->gradient.stops == (StopInfo *) NULL)
366 ThrowFatalException(ResourceLimitFatalError,
367 "UnableToAllocateDashPattern");
368 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
369 (
size_t) number_stops*
sizeof(*clone_info->gradient.stops));
371 clone_info->bounds=draw_info->bounds;
372 clone_info->fill_opacity=draw_info->fill_opacity;
373 clone_info->stroke_opacity=draw_info->stroke_opacity;
374 clone_info->element_reference=draw_info->element_reference;
375 clone_info->clip_path=draw_info->clip_path;
376 clone_info->clip_units=draw_info->clip_units;
377 if (draw_info->clip_mask != (
char *) NULL)
378 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
379 if (draw_info->clipping_mask != (Image *) NULL)
380 clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
381 MagickTrue,&draw_info->clipping_mask->exception);
382 if (draw_info->composite_mask != (Image *) NULL)
383 clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
384 MagickTrue,&draw_info->composite_mask->exception);
385 clone_info->render=draw_info->render;
386 clone_info->debug=draw_info->debug;
422static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
427 if (polygon_info->edges != (EdgeInfo *) NULL)
429 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
430 if (polygon_info->edges[i].points != (PointInfo *) NULL)
431 polygon_info->edges[i].points=(PointInfo *)
432 RelinquishMagickMemory(polygon_info->edges[i].points);
433 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(
434 polygon_info->edges);
436 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
439#if defined(__cplusplus) || defined(c_plusplus)
443static int DrawCompareEdges(
const void *p_edge,
const void *q_edge)
445#define DrawCompareEdge(p,q) \
447 if (((p)-(q)) < 0.0) \
449 if (((p)-(q)) > 0.0) \
460 p=((
const EdgeInfo *) p_edge)->points;
461 q=((
const EdgeInfo *) q_edge)->points;
462 DrawCompareEdge(p[0].y,q[0].y);
463 DrawCompareEdge(p[0].x,q[0].x);
464 DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
466 DrawCompareEdge(p[1].y,q[1].y);
467 DrawCompareEdge(p[1].x,q[1].x);
471#if defined(__cplusplus) || defined(c_plusplus)
475static void LogPolygonInfo(
const PolygonInfo *polygon_info)
484 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin active-edge");
485 p=polygon_info->edges;
486 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
488 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" edge %.20g:",
490 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" direction: %s",
491 p->direction != MagickFalse ?
"down" :
"up");
492 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" ghostline: %s",
493 p->ghostline != MagickFalse ?
"transparent" :
"opaque");
494 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
495 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
496 p->bounds.x2,p->bounds.y2);
497 for (j=0; j < (ssize_t) p->number_points; j++)
498 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %g,%g",
499 p->points[j].x,p->points[j].y);
502 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end active-edge");
505static void ReversePoints(PointInfo *points,
const size_t number_points)
513 for (i=0; i < (ssize_t) (number_points >> 1); i++)
516 points[i]=points[number_points-(i+1)];
517 points[number_points-(i+1)]=point;
521static PolygonInfo *ConvertPathToPolygon(
const PathInfo *path_info,
522 ExceptionInfo *exception)
553 polygon_info=(PolygonInfo *) AcquireMagickMemory(
sizeof(*polygon_info));
554 if (polygon_info == (PolygonInfo *) NULL)
556 (void) ThrowMagickException(exception,GetMagickModule(),
557 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
558 return((PolygonInfo *) NULL);
561 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
562 sizeof(*polygon_info->edges));
563 if (polygon_info->edges == (EdgeInfo *) NULL)
565 (void) ThrowMagickException(exception,GetMagickModule(),
566 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
567 return(DestroyPolygonInfo(polygon_info));
569 (void) memset(polygon_info->edges,0,number_edges*
570 sizeof(*polygon_info->edges));
573 ghostline=MagickFalse;
576 points=(PointInfo *) NULL;
577 (void) memset(&point,0,
sizeof(point));
578 (void) memset(&bounds,0,
sizeof(bounds));
579 polygon_info->edges[edge].number_points=(size_t) n;
580 polygon_info->edges[edge].scanline=0.0;
581 polygon_info->edges[edge].highwater=0;
582 polygon_info->edges[edge].ghostline=ghostline;
583 polygon_info->edges[edge].direction=(ssize_t) direction;
584 polygon_info->edges[edge].points=points;
585 polygon_info->edges[edge].bounds=bounds;
586 polygon_info->number_edges=0;
587 for (i=0; path_info[i].code != EndCode; i++)
589 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
590 (path_info[i].code == GhostlineCode))
595 if ((points != (PointInfo *) NULL) && (n >= 2))
597 if (edge == number_edges)
600 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
601 polygon_info->edges,(
size_t) number_edges,
602 sizeof(*polygon_info->edges));
603 if (polygon_info->edges == (EdgeInfo *) NULL)
605 (void) ThrowMagickException(exception,GetMagickModule(),
606 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
607 points=(PointInfo *) RelinquishMagickMemory(points);
608 return(DestroyPolygonInfo(polygon_info));
611 polygon_info->edges[edge].number_points=(size_t) n;
612 polygon_info->edges[edge].scanline=(-1.0);
613 polygon_info->edges[edge].highwater=0;
614 polygon_info->edges[edge].ghostline=ghostline;
615 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
617 ReversePoints(points,(
size_t) n);
618 polygon_info->edges[edge].points=points;
619 polygon_info->edges[edge].bounds=bounds;
620 polygon_info->edges[edge].bounds.y1=points[0].y;
621 polygon_info->edges[edge].bounds.y2=points[n-1].y;
622 points=(PointInfo *) NULL;
623 ghostline=MagickFalse;
625 polygon_info->number_edges=edge;
627 if (points == (PointInfo *) NULL)
630 points=(PointInfo *) AcquireQuantumMemory((
size_t) number_points,
632 if (points == (PointInfo *) NULL)
634 (void) ThrowMagickException(exception,GetMagickModule(),
635 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
636 return(DestroyPolygonInfo(polygon_info));
639 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
640 point=path_info[i].point;
651 next_direction=((path_info[i].point.y > point.y) ||
652 ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
653 (path_info[i].point.x > point.x))) ? 1 : -1;
654 if ((points != (PointInfo *) NULL) && (direction != 0) &&
655 (direction != next_direction))
661 if (edge == number_edges)
664 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
665 polygon_info->edges,(
size_t) number_edges,
666 sizeof(*polygon_info->edges));
667 if (polygon_info->edges == (EdgeInfo *) NULL)
669 (void) ThrowMagickException(exception,GetMagickModule(),
670 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
671 points=(PointInfo *) RelinquishMagickMemory(points);
672 return(DestroyPolygonInfo(polygon_info));
675 polygon_info->edges[edge].number_points=(size_t) n;
676 polygon_info->edges[edge].scanline=(-1.0);
677 polygon_info->edges[edge].highwater=0;
678 polygon_info->edges[edge].ghostline=ghostline;
679 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
681 ReversePoints(points,(
size_t) n);
682 polygon_info->edges[edge].points=points;
683 polygon_info->edges[edge].bounds=bounds;
684 polygon_info->edges[edge].bounds.y1=points[0].y;
685 polygon_info->edges[edge].bounds.y2=points[n-1].y;
686 polygon_info->number_edges=edge+1;
687 points=(PointInfo *) NULL;
689 points=(PointInfo *) AcquireQuantumMemory((
size_t) number_points,
691 if (points == (PointInfo *) NULL)
693 (void) ThrowMagickException(exception,GetMagickModule(),
694 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
695 return(DestroyPolygonInfo(polygon_info));
698 ghostline=MagickFalse;
704 direction=next_direction;
705 if (points == (PointInfo *) NULL)
707 if (n == (ssize_t) number_points)
710 points=(PointInfo *) ResizeQuantumMemory(points,(
size_t) number_points,
712 if (points == (PointInfo *) NULL)
714 (void) ThrowMagickException(exception,GetMagickModule(),
715 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
716 return(DestroyPolygonInfo(polygon_info));
719 point=path_info[i].point;
721 if (point.x < bounds.x1)
723 if (point.x > bounds.x2)
727 if (points != (PointInfo *) NULL)
730 points=(PointInfo *) RelinquishMagickMemory(points);
733 if (edge == number_edges)
736 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
737 polygon_info->edges,(
size_t) number_edges,
738 sizeof(*polygon_info->edges));
739 if (polygon_info->edges == (EdgeInfo *) NULL)
741 (void) ThrowMagickException(exception,GetMagickModule(),
742 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
743 return(DestroyPolygonInfo(polygon_info));
746 polygon_info->edges[edge].number_points=(size_t) n;
747 polygon_info->edges[edge].scanline=(-1.0);
748 polygon_info->edges[edge].highwater=0;
749 polygon_info->edges[edge].ghostline=ghostline;
750 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
752 ReversePoints(points,(
size_t) n);
753 polygon_info->edges[edge].points=points;
754 polygon_info->edges[edge].bounds=bounds;
755 polygon_info->edges[edge].bounds.y1=points[0].y;
756 polygon_info->edges[edge].bounds.y2=points[n-1].y;
757 points=(PointInfo *) NULL;
758 ghostline=MagickFalse;
760 polygon_info->number_edges=edge;
763 polygon_info->number_edges=edge;
764 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(polygon_info->edges,
765 polygon_info->number_edges,
sizeof(*polygon_info->edges));
766 if (polygon_info->edges == (EdgeInfo *) NULL)
768 (void) ThrowMagickException(exception,GetMagickModule(),
769 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
770 return(DestroyPolygonInfo(polygon_info));
772 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
777 edge_info=polygon_info->edges+i;
778 edge_info->points=(PointInfo *) ResizeQuantumMemory(edge_info->points,
779 edge_info->number_points,
sizeof(*edge_info->points));
780 if (edge_info->points == (PointInfo *) NULL)
782 (void) ThrowMagickException(exception,GetMagickModule(),
783 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
784 return(DestroyPolygonInfo(polygon_info));
787 qsort(polygon_info->edges,(
size_t) polygon_info->number_edges,
788 sizeof(*polygon_info->edges),DrawCompareEdges);
789 if ((GetLogEventMask() & DrawEvent) != 0)
790 LogPolygonInfo(polygon_info);
791 return(polygon_info);
825static void LogPathInfo(
const PathInfo *path_info)
830 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin vector-path");
831 for (p=path_info; p->code != EndCode; p++)
832 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
833 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
834 "moveto ghostline" : p->code == OpenCode ?
"moveto open" :
835 p->code == MoveToCode ?
"moveto" : p->code == LineToCode ?
"lineto" :
837 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end vector-path");
840static PathInfo *ConvertPrimitiveToPath(
841 const DrawInfo *magick_unused(draw_info),
const PrimitiveInfo *primitive_info,
842 ExceptionInfo *exception)
865 magick_unreferenced(draw_info);
870 switch (primitive_info->primitive)
877 return((PathInfo *) NULL);
881 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
882 path_info=(PathInfo *) AcquireQuantumMemory((
size_t) (3UL*i+1UL),
884 if (path_info == (PathInfo *) NULL)
886 (void) ThrowMagickException(exception,GetMagickModule(),
887 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
888 return((PathInfo *) NULL);
891 closed_subpath=MagickFalse;
898 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
901 if (coordinates <= 0)
906 coordinates=(ssize_t) primitive_info[i].coordinates;
907 p=primitive_info[i].point;
910 closed_subpath=primitive_info[i].closed_subpath;
913 if ((code == MoveToCode) || (coordinates <= 0) ||
914 (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
915 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
920 path_info[n].code=code;
921 path_info[n].point=primitive_info[i].point;
922 q=primitive_info[i].point;
927 if (closed_subpath != MagickFalse)
929 closed_subpath=MagickFalse;
935 path_info[start].code=OpenCode;
936 path_info[n].code=GhostlineCode;
937 path_info[n].point=primitive_info[i].point;
939 path_info[n].code=LineToCode;
940 path_info[n].point=p;
943 path_info[n].code=EndCode;
944 path_info[n].point.x=0.0;
945 path_info[n].point.y=0.0;
946 if (IsEventLogging() != MagickFalse)
947 LogPathInfo(path_info);
948 path_info=(PathInfo *) ResizeQuantumMemory(path_info,(
size_t) (n+1),
975MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
977 assert(draw_info != (DrawInfo *) NULL);
978 assert(draw_info->signature == MagickCoreSignature);
979 if (IsEventLogging() != MagickFalse)
980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
981 if (draw_info->id != (
char *) NULL)
982 draw_info->id=DestroyString(draw_info->id);
983 if (draw_info->primitive != (
char *) NULL)
984 draw_info->primitive=DestroyString(draw_info->primitive);
985 if (draw_info->text != (
char *) NULL)
986 draw_info->text=DestroyString(draw_info->text);
987 if (draw_info->geometry != (
char *) NULL)
988 draw_info->geometry=DestroyString(draw_info->geometry);
989 if (draw_info->tile != (Image *) NULL)
990 draw_info->tile=DestroyImage(draw_info->tile);
991 if (draw_info->fill_pattern != (Image *) NULL)
992 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
993 if (draw_info->stroke_pattern != (Image *) NULL)
994 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
995 if (draw_info->font != (
char *) NULL)
996 draw_info->font=DestroyString(draw_info->font);
997 if (draw_info->metrics != (
char *) NULL)
998 draw_info->metrics=DestroyString(draw_info->metrics);
999 if (draw_info->family != (
char *) NULL)
1000 draw_info->family=DestroyString(draw_info->family);
1001 if (draw_info->encoding != (
char *) NULL)
1002 draw_info->encoding=DestroyString(draw_info->encoding);
1003 if (draw_info->density != (
char *) NULL)
1004 draw_info->density=DestroyString(draw_info->density);
1005 if (draw_info->server_name != (
char *) NULL)
1006 draw_info->server_name=(
char *)
1007 RelinquishMagickMemory(draw_info->server_name);
1008 if (draw_info->dash_pattern != (
double *) NULL)
1009 draw_info->dash_pattern=(
double *) RelinquishMagickMemory(
1010 draw_info->dash_pattern);
1011 if (draw_info->gradient.stops != (StopInfo *) NULL)
1012 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
1013 draw_info->gradient.stops);
1014 if (draw_info->clip_mask != (
char *) NULL)
1015 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
1016 if (draw_info->clipping_mask != (Image *) NULL)
1017 draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
1018 if (draw_info->composite_mask != (Image *) NULL)
1019 draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
1020 if (draw_info->image_info != (ImageInfo *) NULL)
1021 draw_info->image_info=DestroyImageInfo(draw_info->image_info);
1022 draw_info->signature=(~MagickCoreSignature);
1023 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
1056static SegmentInfo AffineEdge(
const Image *image,
const AffineMatrix *affine,
1057 const double y,
const SegmentInfo *edge)
1072 inverse_edge.x1=edge->x1;
1073 inverse_edge.y1=edge->y1;
1074 inverse_edge.x2=edge->x2;
1075 inverse_edge.y2=edge->y2;
1076 z=affine->ry*y+affine->tx;
1077 if (affine->sx >= MagickEpsilon)
1079 intercept=(-z/affine->sx);
1081 if (x > inverse_edge.x1)
1083 intercept=(-z+(double) image->columns)/affine->sx;
1085 if (x < inverse_edge.x2)
1089 if (affine->sx < -MagickEpsilon)
1091 intercept=(-z+(double) image->columns)/affine->sx;
1093 if (x > inverse_edge.x1)
1095 intercept=(-z/affine->sx);
1097 if (x < inverse_edge.x2)
1101 if ((z < 0.0) || ((
size_t) floor(z+0.5) >= image->columns))
1103 inverse_edge.x2=edge->x1;
1104 return(inverse_edge);
1109 z=affine->sy*y+affine->ty;
1110 if (affine->rx >= MagickEpsilon)
1112 intercept=(-z/affine->rx);
1114 if (x > inverse_edge.x1)
1116 intercept=(-z+(double) image->rows)/affine->rx;
1118 if (x < inverse_edge.x2)
1122 if (affine->rx < -MagickEpsilon)
1124 intercept=(-z+(double) image->rows)/affine->rx;
1126 if (x > inverse_edge.x1)
1128 intercept=(-z/affine->rx);
1130 if (x < inverse_edge.x2)
1134 if ((z < 0.0) || ((
size_t) floor(z+0.5) >= image->rows))
1136 inverse_edge.x2=edge->x2;
1137 return(inverse_edge);
1139 return(inverse_edge);
1142static AffineMatrix InverseAffineMatrix(
const AffineMatrix *affine)
1150 determinant=MagickSafeReciprocal(affine->sx*affine->sy-affine->rx*
1152 inverse_affine.sx=determinant*affine->sy;
1153 inverse_affine.rx=determinant*(-affine->rx);
1154 inverse_affine.ry=determinant*(-affine->ry);
1155 inverse_affine.sy=determinant*affine->sx;
1156 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1158 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1160 return(inverse_affine);
1163MagickExport MagickBooleanType DrawAffineImage(Image *image,
1164 const Image *source,
const AffineMatrix *affine)
1202 assert(image != (Image *) NULL);
1203 assert(image->signature == MagickCoreSignature);
1204 assert(source != (
const Image *) NULL);
1205 assert(source->signature == MagickCoreSignature);
1206 if (IsEventLogging() != MagickFalse)
1207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1208 assert(affine != (AffineMatrix *) NULL);
1211 extent[1].x=(double) source->columns;
1213 extent[2].x=(double) source->columns;
1214 extent[2].y=(double) source->rows;
1216 extent[3].y=(double) source->rows;
1217 for (i=0; i < 4; i++)
1220 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1221 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1225 for (i=1; i < 4; i++)
1227 if (min.x > extent[i].x)
1229 if (min.y > extent[i].y)
1231 if (max.x < extent[i].x)
1233 if (max.y < extent[i].y)
1239 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
1240 return(MagickFalse);
1246 inverse_affine=InverseAffineMatrix(affine);
1249 if (edge.y2 > (image->rows-1.0))
1250 edge.y2=image->rows-1.0;
1251 GetMagickPixelPacket(image,&zero);
1252 exception=(&image->exception);
1253 start=CastDoubleToLong(ceil(edge.y1-0.5));
1254 stop=CastDoubleToLong(floor(edge.y2+0.5));
1255 source_view=AcquireVirtualCacheView(source,exception);
1256 image_view=AcquireAuthenticCacheView(image,exception);
1257#if defined(MAGICKCORE_OPENMP_SUPPORT)
1258 #pragma omp parallel for schedule(static) shared(status) \
1259 magick_number_threads(source,image,stop-start,1)
1261 for (y=start; y <= stop; y++)
1264 *magick_restrict indexes;
1283 if (status == MagickFalse)
1285 inverse_edge=AffineEdge(source,&inverse_affine,(
double) y,&edge);
1286 if (inverse_edge.x2 < inverse_edge.x1)
1288 if (inverse_edge.x1 < 0.0)
1289 inverse_edge.x1=0.0;
1290 if (inverse_edge.x2 > image->columns-1.0)
1291 inverse_edge.x2=image->columns-1.0;
1292 q=GetCacheViewAuthenticPixels(image_view,CastDoubleToLong(
1293 ceil(inverse_edge.x1-0.5)),y,(
size_t) CastDoubleToLong(floor(
1294 inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),1,exception);
1295 if (q == (PixelPacket *) NULL)
1297 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1301 for (x=CastDoubleToLong(ceil(inverse_edge.x1-0.5));
1302 x <= CastDoubleToLong(floor(inverse_edge.x2+0.5)); x++)
1304 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1306 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1308 status=InterpolateMagickPixelPacket(source,source_view,
1309 UndefinedInterpolatePixel,point.x,point.y,&pixel,exception);
1310 if (status == MagickFalse)
1312 SetMagickPixelPacket(image,q,indexes+x_offset,&composite);
1313 MagickPixelCompositeOver(&pixel,pixel.opacity,&composite,
1314 composite.opacity,&composite);
1315 SetPixelPacket(image,&composite,q,indexes+x_offset);
1319 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1322 source_view=DestroyCacheView(source_view);
1323 image_view=DestroyCacheView(image_view);
1356static MagickBooleanType DrawBoundingRectangles(Image *image,
1357 const DrawInfo *draw_info,
const PolygonInfo *polygon_info)
1385 (void) memset(primitive_info,0,
sizeof(primitive_info));
1386 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1387 status=QueryColorDatabase(
"#0000",&clone_info->fill,&image->exception);
1388 if (status == MagickFalse)
1390 clone_info=DestroyDrawInfo(clone_info);
1391 return(MagickFalse);
1395 if (clone_info->density != (
char *) NULL)
1403 flags=ParseGeometry(clone_info->density,&geometry_info);
1404 if ((flags & RhoValue) != 0)
1405 resolution.x=geometry_info.rho;
1406 resolution.y=resolution.x;
1407 if ((flags & SigmaValue) != 0)
1408 resolution.y=geometry_info.sigma;
1410 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1411 clone_info->stroke_width/2.0;
1416 if (polygon_info != (PolygonInfo *) NULL)
1418 bounds=polygon_info->edges[0].bounds;
1419 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1421 if (polygon_info->edges[i].bounds.x1 < (
double) bounds.x1)
1422 bounds.x1=polygon_info->edges[i].bounds.x1;
1423 if (polygon_info->edges[i].bounds.y1 < (
double) bounds.y1)
1424 bounds.y1=polygon_info->edges[i].bounds.y1;
1425 if (polygon_info->edges[i].bounds.x2 > (
double) bounds.x2)
1426 bounds.x2=polygon_info->edges[i].bounds.x2;
1427 if (polygon_info->edges[i].bounds.y2 > (
double) bounds.y2)
1428 bounds.y2=polygon_info->edges[i].bounds.y2;
1431 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1432 image->columns ? (
double) image->columns-1 : bounds.x1;
1434 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1435 image->rows ? (
double) image->rows-1 : bounds.y1;
1437 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1438 image->columns ? (
double) image->columns-1 : bounds.x2;
1440 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1441 image->rows ? (
double) image->rows-1 : bounds.y2;
1442 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1444 if (polygon_info->edges[i].direction != 0)
1445 status=QueryColorDatabase(
"#f00",&clone_info->stroke,
1448 status=QueryColorDatabase(
"#0f0",&clone_info->stroke,
1450 if (status == MagickFalse)
1452 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1453 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1454 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1455 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1456 primitive_info[0].primitive=RectanglePrimitive;
1457 status&=TraceRectangle(primitive_info,start,end);
1458 primitive_info[0].method=ReplaceMethod;
1459 coordinates=(ssize_t) primitive_info[0].coordinates;
1460 primitive_info[coordinates].primitive=UndefinedPrimitive;
1461 status=DrawPrimitive(image,clone_info,primitive_info);
1462 if (status == MagickFalse)
1465 if (i < (ssize_t) polygon_info->number_edges)
1467 clone_info=DestroyDrawInfo(clone_info);
1468 return(status == 0 ? MagickFalse : MagickTrue);
1471 status=QueryColorDatabase(
"#00f",&clone_info->stroke,&image->exception);
1472 if (status == MagickFalse)
1474 clone_info=DestroyDrawInfo(clone_info);
1475 return(MagickFalse);
1477 start.x=(double) (bounds.x1-mid);
1478 start.y=(double) (bounds.y1-mid);
1479 end.x=(double) (bounds.x2+mid);
1480 end.y=(double) (bounds.y2+mid);
1481 primitive_info[0].primitive=RectanglePrimitive;
1482 status&=TraceRectangle(primitive_info,start,end);
1483 primitive_info[0].method=ReplaceMethod;
1484 coordinates=(ssize_t) primitive_info[0].coordinates;
1485 primitive_info[coordinates].primitive=UndefinedPrimitive;
1486 status=DrawPrimitive(image,clone_info,primitive_info);
1487 clone_info=DestroyDrawInfo(clone_info);
1488 return(status == 0 ? MagickFalse : MagickTrue);
1518MagickExport MagickBooleanType DrawClipPath(Image *image,
1519 const DrawInfo *draw_info,
const char *
id)
1530 clip_path=GetImageArtifact(image,
id);
1531 if (clip_path == (
const char *) NULL)
1532 return(MagickFalse);
1533 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1535 if (clipping_mask == (Image *) NULL)
1536 return(MagickFalse);
1537 status=SetImageClipMask(image,clipping_mask);
1538 clipping_mask=DestroyImage(clipping_mask);
1574static Image *DrawClippingMask(Image *image,
const DrawInfo *draw_info,
1575 const char *
id,
const char *clip_path,ExceptionInfo *exception)
1589 assert(image != (Image *) NULL);
1590 assert(image->signature == MagickCoreSignature);
1591 assert(draw_info != (
const DrawInfo *) NULL);
1592 if (IsEventLogging() != MagickFalse)
1593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1594 clip_mask=AcquireImage((
const ImageInfo *) NULL);
1595 status=SetImageExtent(clip_mask,image->columns,image->rows);
1596 if (status == MagickFalse)
1597 return(DestroyImage(clip_mask));
1598 status=SetImageClipMask(image,(Image *) NULL);
1599 status=QueryColorCompliance(
"#0000",AllCompliance,
1600 &clip_mask->background_color,exception);
1601 clip_mask->background_color.opacity=(Quantum) TransparentOpacity;
1602 status=SetImageBackgroundColor(clip_mask);
1603 if (draw_info->debug != MagickFalse)
1604 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin clip-path %s",
1606 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1607 (void) CloneString(&clone_info->primitive,clip_path);
1608 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1610 if (clone_info->clip_mask != (
char *) NULL)
1611 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1612 (void) QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1614 clone_info->stroke_width=0.0;
1615 clone_info->opacity=OpaqueOpacity;
1616 clone_info->clip_path=MagickTrue;
1617 status=RenderMVGContent(clip_mask,clone_info,0);
1618 clone_info=DestroyDrawInfo(clone_info);
1619 status&=SeparateImageChannel(clip_mask,TrueAlphaChannel);
1620 if (draw_info->compliance != SVGCompliance)
1621 status&=NegateImage(clip_mask,MagickFalse);
1622 if (status == MagickFalse)
1623 clip_mask=DestroyImage(clip_mask);
1624 if (draw_info->debug != MagickFalse)
1625 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end clip-path");
1660static Image *DrawCompositeMask(Image *image,
const DrawInfo *draw_info,
1661 const char *
id,
const char *mask_path,ExceptionInfo *exception)
1675 assert(image != (Image *) NULL);
1676 assert(image->signature == MagickCoreSignature);
1677 assert(draw_info != (
const DrawInfo *) NULL);
1678 if (IsEventLogging() != MagickFalse)
1679 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1680 composite_mask=AcquireImage((
const ImageInfo *) NULL);
1681 status=SetImageExtent(composite_mask,image->columns,image->rows);
1682 if (status == MagickFalse)
1683 return(DestroyImage(composite_mask));
1684 status=SetImageMask(image,(Image *) NULL);
1685 status=QueryColorCompliance(
"#0000",AllCompliance,
1686 &composite_mask->background_color,exception);
1687 composite_mask->background_color.opacity=(Quantum) TransparentOpacity;
1688 (void) SetImageBackgroundColor(composite_mask);
1689 if (draw_info->debug != MagickFalse)
1690 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin mask-path %s",
1692 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1693 (void) CloneString(&clone_info->primitive,mask_path);
1694 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1696 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1698 clone_info->stroke_width=0.0;
1699 clone_info->opacity=OpaqueOpacity;
1700 status=RenderMVGContent(composite_mask,clone_info,0);
1701 clone_info=DestroyDrawInfo(clone_info);
1702 status&=SeparateImageChannel(composite_mask,TrueAlphaChannel);
1703 status&=NegateImage(composite_mask,MagickFalse);
1704 if (status == MagickFalse)
1705 composite_mask=DestroyImage(composite_mask);
1706 if (draw_info->debug != MagickFalse)
1707 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end mask-path");
1708 return(composite_mask);
1740static MagickBooleanType DrawDashPolygon(
const DrawInfo *draw_info,
1741 const PrimitiveInfo *primitive_info,Image *image)
1771 assert(draw_info != (
const DrawInfo *) NULL);
1772 if (draw_info->debug != MagickFalse)
1773 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-dash");
1774 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1775 number_vertices=(size_t) i;
1776 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
1777 (2UL*number_vertices+32UL),
sizeof(*dash_polygon));
1778 if (dash_polygon == (PrimitiveInfo *) NULL)
1780 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1781 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1782 return(MagickFalse);
1784 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1785 sizeof(*dash_polygon));
1786 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1787 clone_info->miterlimit=0;
1788 dash_polygon[0]=primitive_info[0];
1789 dash_polygon[0].closed_subpath=MagickFalse;
1790 scale=ExpandAffine(&draw_info->affine);
1791 length=scale*draw_info->dash_pattern[0];
1792 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1793 scale*draw_info->dash_offset : 0.0;
1795 for (n=0; offset > 0.0; j=0)
1797 if (draw_info->dash_pattern[n] <= 0.0)
1799 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1800 if (offset > length)
1804 length=scale*draw_info->dash_pattern[n];
1807 if (offset < length)
1819 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1821 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1822 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1823 maximum_length=hypot(dx,dy);
1824 if (maximum_length > (
double) (MaxBezierCoordinates >> 2))
1826 if (fabs(length) < MagickEpsilon)
1828 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1830 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1832 length=scale*draw_info->dash_pattern[n];
1834 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1836 total_length+=length;
1837 if ((n & 0x01) != 0)
1839 dash_polygon[0]=primitive_info[0];
1840 dash_polygon[0].closed_subpath=MagickFalse;
1841 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1842 total_length*MagickSafeReciprocal(maximum_length));
1843 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1844 total_length*MagickSafeReciprocal(maximum_length));
1849 if ((j+1) > (ssize_t) number_vertices)
1851 dash_polygon[j]=primitive_info[i-1];
1852 dash_polygon[j].closed_subpath=MagickFalse;
1853 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1854 total_length*MagickSafeReciprocal(maximum_length));
1855 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1856 total_length*MagickSafeReciprocal(maximum_length));
1857 dash_polygon[j].coordinates=1;
1859 dash_polygon[0].coordinates=(size_t) j;
1860 dash_polygon[j].primitive=UndefinedPrimitive;
1861 status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1862 if (status == MagickFalse)
1865 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1867 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1869 length=scale*draw_info->dash_pattern[n];
1871 length-=(maximum_length-total_length);
1872 if ((n & 0x01) != 0)
1874 dash_polygon[j]=primitive_info[i];
1875 dash_polygon[j].coordinates=1;
1878 if ((status != MagickFalse) && (total_length < maximum_length) &&
1879 ((n & 0x01) == 0) && (j > 1))
1881 dash_polygon[j]=primitive_info[i-1];
1882 dash_polygon[j].closed_subpath=MagickFalse;
1883 dash_polygon[j].point.x+=MagickEpsilon;
1884 dash_polygon[j].point.y+=MagickEpsilon;
1885 dash_polygon[j].coordinates=1;
1887 dash_polygon[0].coordinates=(size_t) j;
1888 dash_polygon[j].primitive=UndefinedPrimitive;
1889 status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1891 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1892 clone_info=DestroyDrawInfo(clone_info);
1893 if (draw_info->debug != MagickFalse)
1894 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-dash");
1895 return(status != 0 ? MagickTrue : MagickFalse);
1924static inline double GetStopColorOffset(
const GradientInfo *gradient,
1925 const ssize_t x,
const ssize_t y)
1927 switch (gradient->type)
1929 case UndefinedGradient:
1930 case LinearGradient:
1945 gradient_vector=(&gradient->gradient_vector);
1946 p.x=gradient_vector->x2-gradient_vector->x1;
1947 p.y=gradient_vector->y2-gradient_vector->y1;
1948 q.x=(double) x-gradient_vector->x1;
1949 q.y=(double) y-gradient_vector->y1;
1950 length=sqrt(q.x*q.x+q.y*q.y);
1951 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1952 gamma=MagickSafeReciprocal(gamma);
1953 scale=p.x*q.x+p.y*q.y;
1954 offset=gamma*scale*length;
1957 case RadialGradient:
1962 if (gradient->spread == RepeatSpread)
1964 v.x=(double) x-gradient->center.x;
1965 v.y=(double) y-gradient->center.y;
1966 return(sqrt(v.x*v.x+v.y*v.y));
1968 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1969 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1970 gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1971 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1972 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1973 gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1974 return(sqrt(v.x*v.x+v.y*v.y));
1980MagickExport MagickBooleanType DrawGradientImage(Image *image,
1981 const DrawInfo *draw_info)
2017 assert(image != (Image *) NULL);
2018 assert(image->signature == MagickCoreSignature);
2019 assert(draw_info != (
const DrawInfo *) NULL);
2020 if (IsEventLogging() != MagickFalse)
2021 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2022 gradient=(&draw_info->gradient);
2023 gradient_vector=(&gradient->gradient_vector);
2024 point.x=gradient_vector->x2-gradient_vector->x1;
2025 point.y=gradient_vector->y2-gradient_vector->y1;
2026 length=sqrt(point.x*point.x+point.y*point.y);
2027 bounding_box=gradient->bounding_box;
2029 exception=(&image->exception);
2030 GetMagickPixelPacket(image,&zero);
2031 image_view=AcquireAuthenticCacheView(image,exception);
2032 height=(size_t) (bounding_box.y+bounding_box.height);
2033#if defined(MAGICKCORE_OPENMP_SUPPORT)
2034 #pragma omp parallel for schedule(static) shared(status) \
2035 magick_number_threads(image,image,height,1)
2037 for (y=bounding_box.y; y < (ssize_t) height; y++)
2048 *magick_restrict indexes;
2059 if (status == MagickFalse)
2061 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(
size_t)
2062 bounding_box.width,1,exception);
2063 if (q == (PixelPacket *) NULL)
2068 indexes=GetCacheViewAuthenticIndexQueue(image_view);
2071 offset=GetStopColorOffset(gradient,0,y);
2072 if (gradient->type != RadialGradient)
2073 offset*=MagickSafeReciprocal(length);
2074 width=(size_t) (bounding_box.x+bounding_box.width);
2075 for (x=bounding_box.x; x < (ssize_t) width; x++)
2077 SetMagickPixelPacket(image,q,indexes+x,&pixel);
2078 switch (gradient->spread)
2080 case UndefinedSpread:
2083 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2084 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2086 offset=GetStopColorOffset(gradient,x,y);
2087 if (gradient->type != RadialGradient)
2088 offset*=MagickSafeReciprocal(length);
2090 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2091 if (offset < gradient->stops[i].offset)
2093 if ((offset < 0.0) || (i == 0))
2094 composite=gradient->stops[0].color;
2096 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2097 composite=gradient->stops[gradient->number_stops-1].color;
2102 alpha=(offset-gradient->stops[i].offset)/
2103 (gradient->stops[j].offset-gradient->stops[i].offset);
2104 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2105 &gradient->stops[j].color,alpha,&composite);
2111 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2112 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2114 offset=GetStopColorOffset(gradient,x,y);
2115 if (gradient->type != RadialGradient)
2116 offset*=MagickSafeReciprocal(length);
2120 if ((ssize_t) fmod(offset,2.0) == 0)
2121 offset=fmod(offset,1.0);
2123 offset=1.0-fmod(offset,1.0);
2124 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2125 if (offset < gradient->stops[i].offset)
2128 composite=gradient->stops[0].color;
2130 if (i == (ssize_t) gradient->number_stops)
2131 composite=gradient->stops[gradient->number_stops-1].color;
2136 alpha=(offset-gradient->stops[i].offset)/
2137 (gradient->stops[j].offset-gradient->stops[i].offset);
2138 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2139 &gradient->stops[j].color,alpha,&composite);
2151 antialias=MagickFalse;
2153 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2154 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2156 offset=GetStopColorOffset(gradient,x,y);
2157 if (gradient->type == LinearGradient)
2159 repeat=fmod(offset,length);
2161 repeat=length-fmod(-repeat,length);
2163 repeat=fmod(offset,length);
2164 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2165 MagickTrue : MagickFalse;
2166 offset=MagickSafeReciprocal(length)*repeat;
2170 repeat=fmod(offset,(
double) gradient->radius);
2172 repeat=gradient->radius-fmod(-repeat,
2173 (
double) gradient->radius);
2175 repeat=fmod(offset,(
double) gradient->radius);
2176 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2178 offset=repeat*MagickSafeReciprocal(gradient->radius);
2181 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2182 if (offset < gradient->stops[i].offset)
2185 composite=gradient->stops[0].color;
2187 if (i == (ssize_t) gradient->number_stops)
2188 composite=gradient->stops[gradient->number_stops-1].color;
2193 alpha=(offset-gradient->stops[i].offset)/
2194 (gradient->stops[j].offset-gradient->stops[i].offset);
2195 if (antialias != MagickFalse)
2197 if (gradient->type == LinearGradient)
2198 alpha=length-repeat;
2200 alpha=gradient->radius-repeat;
2202 j=(ssize_t) gradient->number_stops-1L;
2204 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2205 &gradient->stops[j].color,alpha,&composite);
2210 MagickPixelCompositeOver(&composite,composite.opacity,&pixel,
2211 pixel.opacity,&pixel);
2212 SetPixelPacket(image,&pixel,q,indexes+x);
2215 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2218 image_view=DestroyCacheView(image_view);
2251static inline MagickBooleanType CheckPrimitiveExtent(MVGInfo *mvg_info,
2266 if ((mvg_info == (MVGInfo *) NULL) ||
2267 (mvg_info->primitive_info == (PrimitiveInfo **) NULL) ||
2268 (*mvg_info->primitive_info == (PrimitiveInfo *) NULL) ||
2269 (mvg_info->extent == (
size_t *) NULL))
2270 return(MagickFalse);
2271 proposed_extent=mvg_info->offset+pad+PrimitiveExtentPad+1.0;
2272 if ((proposed_extent <= 0.0) || (proposed_extent > (
double) MAGICK_SIZE_MAX))
2273 return(MagickFalse);
2274 extent=CastDoubleToSizeT(ceil(proposed_extent));
2275 if (extent <= *mvg_info->extent)
2277 if (extent > (GetMaxMemoryRequest()/
sizeof(PrimitiveInfo)))
2278 return(MagickFalse);
2279 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(
2280 *mvg_info->primitive_info,extent+1,
sizeof(PrimitiveInfo));
2281 primitive_info[extent].primitive=UndefinedPrimitive;
2282 primitive_info[extent].text=(
char *) NULL;
2283 if (primitive_info == (PrimitiveInfo *) NULL)
2288 extent=(size_t) PrimitiveExtentPad;
2289 primitive_info=(PrimitiveInfo *) AcquireCriticalMemory(extent*
2290 sizeof(*primitive_info));
2291 (void) memset(primitive_info,0,extent*
sizeof(*primitive_info));
2292 *mvg_info->primitive_info=primitive_info;
2293 *mvg_info->extent=extent;
2295 ThrowMagickException(mvg_info->exception,GetMagickModule(),
2296 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2297 return(MagickFalse);
2302 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2304 primitive_info[i].primitive=UndefinedPrimitive;
2305 primitive_info[i].text=(
char *) NULL;
2307 *mvg_info->primitive_info=primitive_info;
2308 *mvg_info->extent=extent;
2312static inline double GetDrawValue(
const char *magick_restrict
string,
2313 char **magick_restrict sentinel)
2316 **magick_restrict q;
2322 value=InterpretLocaleValue(
string,q);
2327static int MVGMacroCompare(
const void *target,
const void *source)
2333 p=(
const char *) target;
2334 q=(
const char *) source;
2335 return(strcmp(p,q));
2338static SplayTreeInfo *GetMVGMacros(
const char *primitive)
2356 if (primitive == (
const char *) NULL)
2357 return((SplayTreeInfo *) NULL);
2358 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2359 RelinquishMagickMemory);
2360 macro=AcquireString(primitive);
2361 token=AcquireString(primitive);
2362 extent=strlen(token)+MagickPathExtent;
2363 for (q=primitive; *q !=
'\0'; )
2365 if (GetNextToken(q,&q,extent,token) < 1)
2369 if (LocaleCompare(
"push",token) == 0)
2375 (void) GetNextToken(q,&q,extent,token);
2379 name[MagickPathExtent];
2390 (void) GetNextToken(q,&q,extent,token);
2393 (void) CopyMagickString(name,token,MagickPathExtent);
2395 for (p=q; *p !=
'\0'; )
2397 if (GetNextToken(p,&p,extent,token) < 1)
2401 if (LocaleCompare(token,
"pop") == 0)
2403 end=p-strlen(token)-1;
2406 if (LocaleCompare(token,
"push") == 0)
2408 if ((n == 0) && (end >= start))
2411 length=(size_t) (end-start);
2416 (void) GetNextToken(p,&p,extent,token);
2419 (void) CopyMagickString(macro,start,length);
2420 (void) AddValueToSplayTree(macros,ConstantString(name),
2421 ConstantString(macro));
2429 token=DestroyString(token);
2430 macro=DestroyString(macro);
2434static inline MagickBooleanType IsValidListChar(
int c)
2436 if ((c >=
'0') && (c <=
'9'))
2450 return(MagickFalse);
2455static inline MagickBooleanType IsValidPoint(
const char *point)
2463 value=GetDrawValue(point,&p);
2464 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2468static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2469 const PointInfo point)
2471 primitive_info->point=point;
2472 primitive_info->coordinates=1;
2473 primitive_info->closed_subpath=MagickFalse;
2474 primitive_info->text=(
char *) NULL;
2478static MagickBooleanType RenderMVGContent(Image *image,
2479 const DrawInfo *draw_info,
const size_t depth)
2481#define RenderImageTag "Render/Image"
2488 key[2*MaxTextExtent],
2489 keyword[MaxTextExtent],
2490 geometry[MaxTextExtent],
2491 name[MaxTextExtent],
2493 pattern[MaxTextExtent],
2556 assert(image != (Image *) NULL);
2557 assert(image->signature == MagickCoreSignature);
2558 assert(draw_info != (DrawInfo *) NULL);
2559 assert(draw_info->signature == MagickCoreSignature);
2560 if (IsEventLogging() != MagickFalse)
2561 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2562 if (depth > MagickMaxRecursionDepth)
2563 ThrowBinaryImageException(DrawError,
"VectorGraphicsNestedTooDeeply",
2565 if ((draw_info->primitive == (
char *) NULL) ||
2566 (*draw_info->primitive ==
'\0'))
2567 return(MagickFalse);
2568 if (draw_info->debug != MagickFalse)
2569 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"begin draw-image");
2570 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
2571 return(MagickFalse);
2572 if (image->matte == MagickFalse)
2574 status=SetImageAlphaChannel(image,OpaqueAlphaChannel);
2575 if (status == MagickFalse)
2576 return(MagickFalse);
2578 primitive=(
char *) NULL;
2579 if ((*draw_info->primitive ==
'@') && (strlen(draw_info->primitive) > 1) &&
2580 (*(draw_info->primitive+1) !=
'-') && (depth == 0))
2581 primitive=FileToString(draw_info->primitive,~0UL,&image->exception);
2583 primitive=AcquireString(draw_info->primitive);
2584 if (primitive == (
char *) NULL)
2585 return(MagickFalse);
2586 primitive_extent=(double) strlen(primitive);
2587 (void) SetImageArtifact(image,
"mvg:vector-graphics",primitive);
2592 graphic_context=(DrawInfo **) AcquireMagickMemory(
sizeof(*graphic_context));
2593 if (graphic_context == (DrawInfo **) NULL)
2595 primitive=DestroyString(primitive);
2596 ThrowBinaryImageException(ResourceLimitError,
"MemoryAllocationFailed",
2599 number_points=(size_t) PrimitiveExtentPad;
2600 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
2601 (number_points+1),
sizeof(*primitive_info));
2602 if (primitive_info == (PrimitiveInfo *) NULL)
2604 primitive=DestroyString(primitive);
2605 for ( ; n >= 0; n--)
2606 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2607 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2608 ThrowBinaryImageException(ResourceLimitError,
"MemoryAllocationFailed",
2611 (void) memset(primitive_info,0,(
size_t) (number_points+1)*
2612 sizeof(*primitive_info));
2613 (void) memset(&mvg_info,0,
sizeof(mvg_info));
2614 mvg_info.primitive_info=(&primitive_info);
2615 mvg_info.extent=(&number_points);
2616 mvg_info.exception=(&image->exception);
2617 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2618 graphic_context[n]->viewbox=image->page;
2619 if ((image->page.width == 0) || (image->page.height == 0))
2621 graphic_context[n]->viewbox.width=image->columns;
2622 graphic_context[n]->viewbox.height=image->rows;
2624 token=AcquireString(primitive);
2625 extent=strlen(token)+MaxTextExtent;
2629 macros=GetMVGMacros(primitive);
2630 status=QueryColorDatabase(
"#000000",&start_color,&image->exception);
2631 for (q=primitive; *q !=
'\0'; )
2636 if (GetNextToken(q,&q,MaxTextExtent,keyword) < 1)
2638 if (*keyword ==
'\0')
2640 if (*keyword ==
'#')
2645 while ((*q !=
'\n') && (*q !=
'\0'))
2649 p=q-strlen(keyword)-1;
2650 primitive_type=UndefinedPrimitive;
2651 current=graphic_context[n]->affine;
2652 GetAffineMatrix(&affine);
2661 if (LocaleCompare(
"affine",keyword) == 0)
2663 (void) GetNextToken(q,&q,extent,token);
2664 affine.sx=GetDrawValue(token,&next_token);
2665 if (token == next_token)
2666 ThrowPointExpectedException(image,token);
2667 (void) GetNextToken(q,&q,extent,token);
2668 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2669 ThrowPointExpectedException(image,token);
2671 (void) GetNextToken(q,&q,extent,token);
2672 affine.ry=GetDrawValue(token,&next_token);
2673 if (token == next_token)
2674 ThrowPointExpectedException(image,token);
2675 (void) GetNextToken(q,&q,extent,token);
2676 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2677 ThrowPointExpectedException(image,token);
2679 (void) GetNextToken(q,&q,extent,token);
2680 affine.rx=GetDrawValue(token,&next_token);
2681 if (token == next_token)
2682 ThrowPointExpectedException(image,token);
2683 (void) GetNextToken(q,&q,extent,token);
2684 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2685 ThrowPointExpectedException(image,token);
2687 (void) GetNextToken(q,&q,extent,token);
2688 affine.sy=GetDrawValue(token,&next_token);
2689 if (token == next_token)
2690 ThrowPointExpectedException(image,token);
2691 (void) GetNextToken(q,&q,extent,token);
2692 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2693 ThrowPointExpectedException(image,token);
2695 (void) GetNextToken(q,&q,extent,token);
2696 affine.tx=GetDrawValue(token,&next_token);
2697 if (token == next_token)
2698 ThrowPointExpectedException(image,token);
2699 (void) GetNextToken(q,&q,extent,token);
2700 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2701 ThrowPointExpectedException(image,token);
2703 (void) GetNextToken(q,&q,extent,token);
2704 affine.ty=GetDrawValue(token,&next_token);
2705 if (token == next_token)
2706 ThrowPointExpectedException(image,token);
2709 if (LocaleCompare(
"arc",keyword) == 0)
2711 primitive_type=ArcPrimitive;
2720 if (LocaleCompare(
"bezier",keyword) == 0)
2722 primitive_type=BezierPrimitive;
2725 if (LocaleCompare(
"border-color",keyword) == 0)
2727 (void) GetNextToken(q,&q,extent,token);
2728 status&=QueryColorDatabase(token,&graphic_context[n]->border_color,
2738 if (LocaleCompare(
"class",keyword) == 0)
2743 (void) GetNextToken(q,&q,extent,token);
2744 if ((*token ==
'\0') || (*token ==
';'))
2752 for (i=0; i <= n; i++)
2753 if (LocaleCompare(token,graphic_context[i]->
id) == 0)
2757 if (classDepth++ > MagickMaxRecursionDepth)
2759 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2760 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",token);
2764 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2765 if ((graphic_context[n]->render != MagickFalse) &&
2766 (mvg_class != (
const char *) NULL) && (p > primitive))
2777 (void) CloneString(&graphic_context[n]->
id,token);
2778 offset=(ssize_t) (p-primitive);
2779 elements=AcquireString(primitive);
2780 elements[offset]=
'\0';
2781 (void) ConcatenateString(&elements,mvg_class);
2782 (void) ConcatenateString(&elements,
"\n");
2783 (void) ConcatenateString(&elements,q);
2784 primitive=DestroyString(primitive);
2790 if (LocaleCompare(
"clip-path",keyword) == 0)
2798 (void) GetNextToken(q,&q,extent,token);
2804 (void) CloneString(&graphic_context[n]->clip_mask,token);
2805 clip_path=(
const char *) GetValueFromSplayTree(macros,token);
2806 if (clip_path != (
const char *) NULL)
2808 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2809 graphic_context[n]->clipping_mask=
2810 DestroyImage(graphic_context[n]->clipping_mask);
2811 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2812 graphic_context[n],token,clip_path,&image->exception);
2813 if (graphic_context[n]->compliance != SVGCompliance)
2818 clip_path=(
const char *) GetValueFromSplayTree(macros,
2819 graphic_context[n]->clip_mask);
2820 if (clip_path != (
const char *) NULL)
2821 (void) SetImageArtifact(image,
2822 graphic_context[n]->clip_mask,clip_path);
2823 status&=DrawClipPath(image,graphic_context[n],
2824 graphic_context[n]->clip_mask);
2829 if (LocaleCompare(
"clip-rule",keyword) == 0)
2834 (void) GetNextToken(q,&q,extent,token);
2835 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2837 if (fill_rule == -1)
2842 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2845 if (LocaleCompare(
"clip-units",keyword) == 0)
2850 (void) GetNextToken(q,&q,extent,token);
2851 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2853 if (clip_units == -1)
2858 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2859 if (clip_units == ObjectBoundingBox)
2861 GetAffineMatrix(¤t);
2862 affine.sx=draw_info->bounds.x2;
2863 affine.sy=draw_info->bounds.y2;
2864 affine.tx=draw_info->bounds.x1;
2865 affine.ty=draw_info->bounds.y1;
2870 if (LocaleCompare(
"circle",keyword) == 0)
2872 primitive_type=CirclePrimitive;
2875 if (LocaleCompare(
"color",keyword) == 0)
2877 primitive_type=ColorPrimitive;
2880 if (LocaleCompare(
"compliance",keyword) == 0)
2886 (void) GetNextToken(q,&q,extent,token);
2887 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2888 MagickComplianceOptions,MagickFalse,token);
2891 if (LocaleCompare(
"currentColor",keyword) == 0)
2893 (void) GetNextToken(q,&q,extent,token);
2902 if (LocaleCompare(
"decorate",keyword) == 0)
2907 (void) GetNextToken(q,&q,extent,token);
2908 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2915 graphic_context[n]->decorate=(DecorationType) decorate;
2918 if (LocaleCompare(
"density",keyword) == 0)
2920 (void) GetNextToken(q,&q,extent,token);
2921 (void) CloneString(&graphic_context[n]->density,token);
2924 if (LocaleCompare(
"direction",keyword) == 0)
2929 (void) GetNextToken(q,&q,extent,token);
2930 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2932 if (direction == -1)
2935 graphic_context[n]->direction=(DirectionType) direction;
2944 if (LocaleCompare(
"ellipse",keyword) == 0)
2946 primitive_type=EllipsePrimitive;
2949 if (LocaleCompare(
"encoding",keyword) == 0)
2951 (void) GetNextToken(q,&q,extent,token);
2952 (void) CloneString(&graphic_context[n]->encoding,token);
2961 if (LocaleCompare(
"fill",keyword) == 0)
2966 (void) GetNextToken(q,&q,extent,token);
2967 if (graphic_context[n]->clip_path != MagickFalse)
2969 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2970 if (mvg_class != (
const char *) NULL)
2972 (void) DrawPatternPath(image,draw_info,mvg_class,
2973 &graphic_context[n]->fill_pattern);
2976 (void) FormatLocaleString(pattern,MaxTextExtent,
"%s",token);
2977 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
2979 (void) DrawPatternPath(image,draw_info,token,
2980 &graphic_context[n]->fill_pattern);
2983 status&=QueryColorDatabase(token,&graphic_context[n]->fill,
2985 if (graphic_context[n]->fill_opacity != (
double) OpaqueOpacity)
2986 graphic_context[n]->fill.opacity=ClampToQuantum(
2987 graphic_context[n]->fill_opacity);
2990 if (LocaleCompare(
"fill-opacity",keyword) == 0)
2995 (void) GetNextToken(q,&q,extent,token);
2996 if (graphic_context[n]->clip_path != MagickFalse)
2998 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
2999 opacity=MagickMin(MagickMax(factor*
3000 GetDrawValue(token,&next_token),0.0),1.0);
3001 if (token == next_token)
3002 ThrowPointExpectedException(image,token);
3003 if (graphic_context[n]->compliance == SVGCompliance)
3004 graphic_context[n]->fill_opacity*=(1.0-opacity);
3006 graphic_context[n]->fill_opacity=((MagickRealType) QuantumRange-
3007 graphic_context[n]->fill_opacity)*(1.0-opacity);
3008 if (graphic_context[n]->fill.opacity != TransparentOpacity)
3009 graphic_context[n]->fill.opacity=(Quantum)
3010 graphic_context[n]->fill_opacity;
3012 graphic_context[n]->fill.opacity=ClampToQuantum((MagickRealType)
3013 QuantumRange*(1.0-opacity));
3016 if (LocaleCompare(
"fill-rule",keyword) == 0)
3021 (void) GetNextToken(q,&q,extent,token);
3022 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
3024 if (fill_rule == -1)
3029 graphic_context[n]->fill_rule=(FillRule) fill_rule;
3032 if (LocaleCompare(
"font",keyword) == 0)
3034 (void) GetNextToken(q,&q,extent,token);
3035 (void) CloneString(&graphic_context[n]->font,token);
3036 if (LocaleCompare(
"none",token) == 0)
3037 graphic_context[n]->font=(
char *) RelinquishMagickMemory(
3038 graphic_context[n]->font);
3041 if (LocaleCompare(
"font-family",keyword) == 0)
3043 (void) GetNextToken(q,&q,extent,token);
3044 (void) CloneString(&graphic_context[n]->family,token);
3047 if (LocaleCompare(
"font-size",keyword) == 0)
3049 (void) GetNextToken(q,&q,extent,token);
3050 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3051 if (token == next_token)
3052 ThrowPointExpectedException(image,token);
3055 if (LocaleCompare(
"font-stretch",keyword) == 0)
3060 (void) GetNextToken(q,&q,extent,token);
3061 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3067 graphic_context[n]->stretch=(StretchType) stretch;
3070 if (LocaleCompare(
"font-style",keyword) == 0)
3075 (void) GetNextToken(q,&q,extent,token);
3076 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3082 graphic_context[n]->style=(StyleType) style;
3085 if (LocaleCompare(
"font-weight",keyword) == 0)
3090 (void) GetNextToken(q,&q,extent,token);
3091 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3093 weight=(ssize_t) StringToUnsignedLong(token);
3094 graphic_context[n]->weight=(size_t) weight;
3103 if (LocaleCompare(
"gradient-units",keyword) == 0)
3105 (void) GetNextToken(q,&q,extent,token);
3108 if (LocaleCompare(
"gravity",keyword) == 0)
3113 (void) GetNextToken(q,&q,extent,token);
3114 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3120 graphic_context[n]->gravity=(GravityType) gravity;
3129 if (LocaleCompare(
"image",keyword) == 0)
3134 primitive_type=ImagePrimitive;
3135 (void) GetNextToken(q,&q,extent,token);
3136 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3142 graphic_context[n]->compose=(CompositeOperator) compose;
3145 if (LocaleCompare(
"interline-spacing",keyword) == 0)
3147 (void) GetNextToken(q,&q,extent,token);
3148 graphic_context[n]->interline_spacing=GetDrawValue(token,
3150 if (token == next_token)
3151 ThrowPointExpectedException(image,token);
3154 if (LocaleCompare(
"interword-spacing",keyword) == 0)
3156 (void) GetNextToken(q,&q,extent,token);
3157 graphic_context[n]->interword_spacing=GetDrawValue(token,
3159 if (token == next_token)
3160 ThrowPointExpectedException(image,token);
3169 if (LocaleCompare(
"kerning",keyword) == 0)
3171 (void) GetNextToken(q,&q,extent,token);
3172 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3173 if (token == next_token)
3174 ThrowPointExpectedException(image,token);
3183 if (LocaleCompare(
"letter-spacing",keyword) == 0)
3185 (void) GetNextToken(q,&q,extent,token);
3186 if (IsValidPoint(token) == MagickFalse)
3188 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3189 clone_info->text=AcquireString(
" ");
3190 status&=GetTypeMetrics(image,clone_info,&metrics);
3191 graphic_context[n]->kerning=metrics.width*
3192 GetDrawValue(token,&next_token);
3193 clone_info=DestroyDrawInfo(clone_info);
3194 if (token == next_token)
3195 ThrowPointExpectedException(image,token);
3198 if (LocaleCompare(
"line",keyword) == 0)
3200 primitive_type=LinePrimitive;
3209 if (LocaleCompare(
"mask",keyword) == 0)
3217 (void) GetNextToken(q,&q,extent,token);
3218 mask_path=(
const char *) GetValueFromSplayTree(macros,token);
3219 if (mask_path != (
const char *) NULL)
3221 if (graphic_context[n]->composite_mask != (Image *) NULL)
3222 graphic_context[n]->composite_mask=
3223 DestroyImage(graphic_context[n]->composite_mask);
3224 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3225 graphic_context[n],token,mask_path,&image->exception);
3226 if (graphic_context[n]->compliance != SVGCompliance)
3227 status=SetImageMask(image,graphic_context[n]->composite_mask);
3231 if (LocaleCompare(
"matte",keyword) == 0)
3233 primitive_type=MattePrimitive;
3242 if (LocaleCompare(
"offset",keyword) == 0)
3244 (void) GetNextToken(q,&q,extent,token);
3247 if (LocaleCompare(
"opacity",keyword) == 0)
3252 (void) GetNextToken(q,&q,extent,token);
3253 if (graphic_context[n]->clip_path != MagickFalse)
3255 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3256 opacity=1.0-MagickMin(MagickMax(factor*
3257 GetDrawValue(token,&next_token),0.0),1.0);
3258 if (token == next_token)
3259 ThrowPointExpectedException(image,token);
3260 if (graphic_context[n]->compliance == SVGCompliance)
3262 graphic_context[n]->fill_opacity*=opacity;
3263 graphic_context[n]->stroke_opacity*=opacity;
3267 graphic_context[n]->fill_opacity=(double) QuantumRange*opacity;
3268 graphic_context[n]->stroke_opacity=(double) QuantumRange*
3271 if (graphic_context[n]->fill.opacity != (
double) TransparentOpacity)
3273 graphic_context[n]->fill.opacity=
3274 graphic_context[n]->fill_opacity;
3275 graphic_context[n]->stroke.opacity=
3276 graphic_context[n]->stroke_opacity;
3280 graphic_context[n]->fill.opacity=(MagickRealType)
3281 ClampToQuantum((
double) QuantumRange*opacity);
3282 graphic_context[n]->stroke.opacity=(MagickRealType)
3283 ClampToQuantum((
double) QuantumRange*opacity);
3293 if (LocaleCompare(
"path",keyword) == 0)
3295 primitive_type=PathPrimitive;
3298 if (LocaleCompare(
"point",keyword) == 0)
3300 primitive_type=PointPrimitive;
3303 if (LocaleCompare(
"polyline",keyword) == 0)
3305 primitive_type=PolylinePrimitive;
3308 if (LocaleCompare(
"polygon",keyword) == 0)
3310 primitive_type=PolygonPrimitive;
3313 if (LocaleCompare(
"pop",keyword) == 0)
3315 if (GetNextToken(q,&q,extent,token) < 1)
3317 if (LocaleCompare(
"class",token) == 0)
3319 if (LocaleCompare(
"clip-path",token) == 0)
3321 if (LocaleCompare(
"defs",token) == 0)
3324 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3328 if (LocaleCompare(
"gradient",token) == 0)
3330 if (LocaleCompare(
"graphic-context",token) == 0)
3334 (void) ThrowMagickException(&image->exception,
3335 GetMagickModule(),DrawError,
3336 "UnbalancedGraphicContextPushPop",
"`%s'",token);
3341 if ((graphic_context[n]->clip_mask != (
char *) NULL) &&
3342 (graphic_context[n]->compliance != SVGCompliance))
3343 if (LocaleCompare(graphic_context[n]->clip_mask,
3344 graphic_context[n-1]->clip_mask) != 0)
3345 status=SetImageClipMask(image,(Image *) NULL);
3346 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3350 if (LocaleCompare(
"mask",token) == 0)
3352 if (LocaleCompare(
"pattern",token) == 0)
3354 if (LocaleCompare(
"symbol",token) == 0)
3357 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3364 if (LocaleCompare(
"push",keyword) == 0)
3366 if (GetNextToken(q,&q,extent,token) < 1)
3368 if (LocaleCompare(
"class",token) == 0)
3373 for (p=q; *q !=
'\0'; )
3375 if (GetNextToken(q,&q,extent,token) < 1)
3377 if (LocaleCompare(token,
"pop") != 0)
3379 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3380 if (LocaleCompare(token,
"class") != 0)
3384 (void) GetNextToken(q,&q,extent,token);
3387 if (LocaleCompare(
"clip-path",token) == 0)
3389 (void) GetNextToken(q,&q,extent,token);
3390 for (p=q; *q !=
'\0'; )
3392 if (GetNextToken(q,&q,extent,token) < 1)
3394 if (LocaleCompare(token,
"pop") != 0)
3396 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3397 if (LocaleCompare(token,
"clip-path") != 0)
3401 if ((q == (
char *) NULL) || (p == (
char *) NULL) || ((q-4) < p))
3406 (void) GetNextToken(q,&q,extent,token);
3409 if (LocaleCompare(
"defs",token) == 0)
3412 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3416 if (LocaleCompare(
"gradient",token) == 0)
3419 key[2*MaxTextExtent],
3420 name[MaxTextExtent],
3421 type[MaxTextExtent];
3426 (void) GetNextToken(q,&q,extent,token);
3427 (void) CopyMagickString(name,token,MaxTextExtent);
3428 (void) GetNextToken(q,&q,extent,token);
3429 (void) CopyMagickString(type,token,MaxTextExtent);
3430 (void) GetNextToken(q,&q,extent,token);
3431 segment.x1=GetDrawValue(token,&next_token);
3432 if (token == next_token)
3433 ThrowPointExpectedException(image,token);
3434 (void) GetNextToken(q,&q,extent,token);
3435 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3436 ThrowPointExpectedException(image,token);
3438 (void) GetNextToken(q,&q,extent,token);
3439 segment.y1=GetDrawValue(token,&next_token);
3440 if (token == next_token)
3441 ThrowPointExpectedException(image,token);
3442 (void) GetNextToken(q,&q,extent,token);
3443 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3444 ThrowPointExpectedException(image,token);
3446 (void) GetNextToken(q,&q,extent,token);
3447 segment.x2=GetDrawValue(token,&next_token);
3448 if (token == next_token)
3449 ThrowPointExpectedException(image,token);
3450 (void) GetNextToken(q,&q,extent,token);
3451 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3452 ThrowPointExpectedException(image,token);
3454 (void) GetNextToken(q,&q,extent,token);
3455 segment.y2=GetDrawValue(token,&next_token);
3456 if (token == next_token)
3457 ThrowPointExpectedException(image,token);
3458 if (LocaleCompare(type,
"radial") == 0)
3460 (void) GetNextToken(q,&q,extent,token);
3461 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3462 ThrowPointExpectedException(image,token);
3464 (void) GetNextToken(q,&q,extent,token);
3466 for (p=q; *q !=
'\0'; )
3468 if (GetNextToken(q,&q,extent,token) < 1)
3470 if (LocaleCompare(token,
"pop") != 0)
3472 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3473 if (LocaleCompare(token,
"gradient") != 0)
3477 if ((q == (
char *) NULL) || (*q ==
'\0') ||
3478 (p == (
char *) NULL) || ((q-4) < p) ||
3479 ((q-p+4+1) > extent))
3484 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3485 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3486 graphic_context[n]->affine.ry*segment.y1+
3487 graphic_context[n]->affine.tx;
3488 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3489 graphic_context[n]->affine.sy*segment.y1+
3490 graphic_context[n]->affine.ty;
3491 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3492 graphic_context[n]->affine.ry*segment.y2+
3493 graphic_context[n]->affine.tx;
3494 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3495 graphic_context[n]->affine.sy*segment.y2+
3496 graphic_context[n]->affine.ty;
3497 (void) FormatLocaleString(key,MaxTextExtent,
"%s",name);
3498 (void) SetImageArtifact(image,key,token);
3499 (void) FormatLocaleString(key,MaxTextExtent,
"%s-type",name);
3500 (void) SetImageArtifact(image,key,type);
3501 (void) FormatLocaleString(key,MaxTextExtent,
"%s-geometry",name);
3502 (void) FormatLocaleString(geometry,MaxTextExtent,
3503 "%gx%g%+.15g%+.15g",
3504 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3505 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3506 bounds.x1,bounds.y1);
3507 (void) SetImageArtifact(image,key,geometry);
3508 (void) GetNextToken(q,&q,extent,token);
3511 if (LocaleCompare(
"graphic-context",token) == 0)
3514 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3515 graphic_context,(
size_t) (n+1),
sizeof(*graphic_context));
3516 if (graphic_context == (DrawInfo **) NULL)
3518 (void) ThrowMagickException(&image->exception,
3519 GetMagickModule(),ResourceLimitError,
3520 "MemoryAllocationFailed",
"`%s'",image->filename);
3524 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3525 graphic_context[n-1]);
3528 (void) GetNextToken(q,&q,extent,token);
3529 (void) CloneString(&graphic_context[n]->
id,token);
3531 if (n > MagickMaxRecursionDepth)
3533 (void) ThrowMagickException(&image->exception,
3534 GetMagickModule(),DrawError,
3535 "VectorGraphicsNestedTooDeeply",
"`%s'",image->filename);
3540 if (LocaleCompare(
"mask",token) == 0)
3542 (void) GetNextToken(q,&q,extent,token);
3545 if (LocaleCompare(
"pattern",token) == 0)
3550 (void) GetNextToken(q,&q,extent,token);
3551 (void) CopyMagickString(name,token,MaxTextExtent);
3552 (void) GetNextToken(q,&q,extent,token);
3553 bounds.x=CastDoubleToLong(ceil(GetDrawValue(token,
3555 if (token == next_token)
3556 ThrowPointExpectedException(image,token);
3557 (void) GetNextToken(q,&q,extent,token);
3558 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3559 ThrowPointExpectedException(image,token);
3561 (void) GetNextToken(q,&q,extent,token);
3562 bounds.y=CastDoubleToLong(ceil(GetDrawValue(token,
3564 if (token == next_token)
3565 ThrowPointExpectedException(image,token);
3566 (void) GetNextToken(q,&q,extent,token);
3567 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3568 ThrowPointExpectedException(image,token);
3570 (void) GetNextToken(q,&q,extent,token);
3571 bounds.width=CastDoubleToUnsigned(GetDrawValue(token,
3573 if (token == next_token)
3574 ThrowPointExpectedException(image,token);
3575 (void) GetNextToken(q,&q,extent,token);
3576 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3577 ThrowPointExpectedException(image,token);
3579 (void) GetNextToken(q,&q,extent,token);
3580 bounds.height=CastDoubleToUnsigned(GetDrawValue(token,
3582 if (token == next_token)
3583 ThrowPointExpectedException(image,token);
3584 for (p=q; *q !=
'\0'; )
3586 if (GetNextToken(q,&q,extent,token) < 1)
3588 if (LocaleCompare(token,
"pop") != 0)
3590 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3591 if (LocaleCompare(token,
"pattern") != 0)
3595 if ((q == (
char *) NULL) || (p == (
char *) NULL) ||
3596 ((q-4) < p) || ((
size_t) (q-p+4+1) > extent))
3601 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3602 (void) FormatLocaleString(key,MaxTextExtent,
"%s",name);
3603 (void) SetImageArtifact(image,key,token);
3604 (void) FormatLocaleString(key,MaxTextExtent,
"%s-geometry",name);
3605 (void) FormatLocaleString(geometry,MaxTextExtent,
3606 "%.20gx%.20g%+.20g%+.20g",(
double) bounds.width,(
double)
3607 bounds.height,(
double) bounds.x,(
double) bounds.y);
3608 (void) SetImageArtifact(image,key,geometry);
3609 (void) GetNextToken(q,&q,extent,token);
3612 if (LocaleCompare(
"symbol",token) == 0)
3615 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3628 if (LocaleCompare(
"rectangle",keyword) == 0)
3630 primitive_type=RectanglePrimitive;
3633 if (LocaleCompare(
"rotate",keyword) == 0)
3635 (void) GetNextToken(q,&q,extent,token);
3636 angle=GetDrawValue(token,&next_token);
3637 if (token == next_token)
3638 ThrowPointExpectedException(image,token);
3639 affine.sx=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3640 affine.rx=sin(DegreesToRadians(fmod((
double) angle,360.0)));
3641 affine.ry=(-sin(DegreesToRadians(fmod((
double) angle,360.0))));
3642 affine.sy=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3645 if (LocaleCompare(
"roundRectangle",keyword) == 0)
3647 primitive_type=RoundRectanglePrimitive;
3656 if (LocaleCompare(
"scale",keyword) == 0)
3658 (void) GetNextToken(q,&q,extent,token);
3659 affine.sx=GetDrawValue(token,&next_token);
3660 if (token == next_token)
3661 ThrowPointExpectedException(image,token);
3662 (void) GetNextToken(q,&q,extent,token);
3663 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3664 ThrowPointExpectedException(image,token);
3666 (void) GetNextToken(q,&q,extent,token);
3667 affine.sy=GetDrawValue(token,&next_token);
3668 if (token == next_token)
3669 ThrowPointExpectedException(image,token);
3672 if (LocaleCompare(
"skewX",keyword) == 0)
3674 (void) GetNextToken(q,&q,extent,token);
3675 angle=GetDrawValue(token,&next_token);
3676 if (token == next_token)
3677 ThrowPointExpectedException(image,token);
3678 affine.ry=sin(DegreesToRadians(angle));
3681 if (LocaleCompare(
"skewY",keyword) == 0)
3683 (void) GetNextToken(q,&q,extent,token);
3684 angle=GetDrawValue(token,&next_token);
3685 if (token == next_token)
3686 ThrowPointExpectedException(image,token);
3687 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3690 if (LocaleCompare(
"stop-color",keyword) == 0)
3698 (void) GetNextToken(q,&q,extent,token);
3699 status&=QueryColorDatabase(token,&stop_color,&image->exception);
3700 type=LinearGradient;
3701 if (draw_info->gradient.type == RadialGradient)
3702 type=RadialGradient;
3703 (void) GradientImage(image,type,PadSpread,&start_color,&stop_color);
3704 start_color=stop_color;
3705 (void) GetNextToken(q,&q,extent,token);
3708 if (LocaleCompare(
"stroke",keyword) == 0)
3713 (void) GetNextToken(q,&q,extent,token);
3714 if (graphic_context[n]->clip_path != MagickFalse)
3716 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
3717 if (mvg_class != (
const char *) NULL)
3719 (void) DrawPatternPath(image,draw_info,mvg_class,
3720 &graphic_context[n]->stroke_pattern);
3723 (void) FormatLocaleString(pattern,MaxTextExtent,
"%s",token);
3724 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
3726 (void) DrawPatternPath(image,draw_info,token,
3727 &graphic_context[n]->stroke_pattern);
3730 status&=QueryColorDatabase(token,&graphic_context[n]->stroke,
3732 if (graphic_context[n]->stroke_opacity != (MagickRealType) OpaqueOpacity)
3733 graphic_context[n]->stroke.opacity=ClampToQuantum(
3734 graphic_context[n]->stroke_opacity);
3737 if (LocaleCompare(
"stroke-antialias",keyword) == 0)
3739 (void) GetNextToken(q,&q,extent,token);
3740 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3741 MagickTrue : MagickFalse;
3744 if (LocaleCompare(
"stroke-dasharray",keyword) == 0)
3746 if (graphic_context[n]->dash_pattern != (
double *) NULL)
3747 graphic_context[n]->dash_pattern=(
double *)
3748 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3749 if (IsValidPoint(q) != MagickFalse)
3755 (void) GetNextToken(p,&p,extent,token);
3756 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3757 ThrowPointExpectedException(image,token);
3759 (void) GetNextToken(p,&p,extent,token);
3760 for (x=0; IsValidPoint(token) != MagickFalse; x++)
3762 (void) GetNextToken(p,&p,extent,token);
3764 (void) GetNextToken(p,&p,extent,token);
3766 graphic_context[n]->dash_pattern=(
double *)
3767 AcquireQuantumMemory((
size_t) (2*x+2),
3768 sizeof(*graphic_context[n]->dash_pattern));
3769 if (graphic_context[n]->dash_pattern == (
double *) NULL)
3771 (void) ThrowMagickException(&image->exception,
3772 GetMagickModule(),ResourceLimitError,
3773 "MemoryAllocationFailed",
"`%s'",image->filename);
3777 (void) memset(graphic_context[n]->dash_pattern,0,(
size_t)
3778 (2*x+2)*
sizeof(*graphic_context[n]->dash_pattern));
3779 for (j=0; j < x; j++)
3781 (void) GetNextToken(q,&q,extent,token);
3782 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3783 ThrowPointExpectedException(image,token);
3785 (void) GetNextToken(q,&q,extent,token);
3786 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3788 if (token == next_token)
3789 ThrowPointExpectedException(image,token);
3790 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3793 if ((x & 0x01) != 0)
3794 for ( ; j < (2*x); j++)
3795 graphic_context[n]->dash_pattern[j]=
3796 graphic_context[n]->dash_pattern[j-x];
3797 graphic_context[n]->dash_pattern[j]=0.0;
3800 (void) GetNextToken(q,&q,extent,token);
3803 if (LocaleCompare(
"stroke-dashoffset",keyword) == 0)
3805 (void) GetNextToken(q,&q,extent,token);
3806 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3807 if (token == next_token)
3808 ThrowPointExpectedException(image,token);
3811 if (LocaleCompare(
"stroke-linecap",keyword) == 0)
3816 (void) GetNextToken(q,&q,extent,token);
3817 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3823 graphic_context[n]->linecap=(LineCap) linecap;
3826 if (LocaleCompare(
"stroke-linejoin",keyword) == 0)
3831 (void) GetNextToken(q,&q,extent,token);
3832 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3839 graphic_context[n]->linejoin=(LineJoin) linejoin;
3842 if (LocaleCompare(
"stroke-miterlimit",keyword) == 0)
3844 (void) GetNextToken(q,&q,extent,token);
3845 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3848 if (LocaleCompare(
"stroke-opacity",keyword) == 0)
3853 (void) GetNextToken(q,&q,extent,token);
3854 if (graphic_context[n]->clip_path != MagickFalse)
3856 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3857 opacity=MagickMin(MagickMax(factor*
3858 GetDrawValue(token,&next_token),0.0),1.0);
3859 if (token == next_token)
3860 ThrowPointExpectedException(image,token);
3861 if (graphic_context[n]->compliance == SVGCompliance)
3862 graphic_context[n]->stroke_opacity*=(1.0-opacity);
3864 graphic_context[n]->stroke_opacity=((MagickRealType) QuantumRange-
3865 graphic_context[n]->stroke_opacity)*(1.0-opacity);
3866 if (graphic_context[n]->stroke.opacity != TransparentOpacity)
3867 graphic_context[n]->stroke.opacity=(Quantum)
3868 graphic_context[n]->stroke_opacity;
3870 graphic_context[n]->stroke.opacity=ClampToQuantum(
3871 (MagickRealType) QuantumRange*opacity);
3874 if (LocaleCompare(
"stroke-width",keyword) == 0)
3876 (void) GetNextToken(q,&q,extent,token);
3877 if (graphic_context[n]->clip_path != MagickFalse)
3879 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3880 if ((token == next_token) ||
3881 (graphic_context[n]->stroke_width < 0.0))
3882 ThrowPointExpectedException(image,token);
3891 if (LocaleCompare(
"text",keyword) == 0)
3893 primitive_type=TextPrimitive;
3897 if (LocaleCompare(
"text-align",keyword) == 0)
3902 (void) GetNextToken(q,&q,extent,token);
3903 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3909 graphic_context[n]->align=(AlignType) align;
3912 if (LocaleCompare(
"text-anchor",keyword) == 0)
3917 (void) GetNextToken(q,&q,extent,token);
3918 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3924 graphic_context[n]->align=(AlignType) align;
3927 if (LocaleCompare(
"text-antialias",keyword) == 0)
3929 (void) GetNextToken(q,&q,extent,token);
3930 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3931 MagickTrue : MagickFalse;
3934 if (LocaleCompare(
"text-undercolor",keyword) == 0)
3936 (void) GetNextToken(q,&q,extent,token);
3937 status&=QueryColorDatabase(token,&graphic_context[n]->undercolor,
3941 if (LocaleCompare(
"translate",keyword) == 0)
3943 (void) GetNextToken(q,&q,extent,token);
3944 affine.tx=GetDrawValue(token,&next_token);
3945 if (token == next_token)
3946 ThrowPointExpectedException(image,token);
3947 (void) GetNextToken(q,&q,extent,token);
3948 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3949 ThrowPointExpectedException(image,token);
3951 (void) GetNextToken(q,&q,extent,token);
3952 affine.ty=GetDrawValue(token,&next_token);
3953 if (token == next_token)
3954 ThrowPointExpectedException(image,token);
3963 if (LocaleCompare(
"use",keyword) == 0)
3971 (void) GetNextToken(q,&q,extent,token);
3972 use=(
const char *) GetValueFromSplayTree(macros,token);
3973 if (use != (
const char *) NULL)
3975 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3976 (void) CloneString(&clone_info->primitive,use);
3977 status=RenderMVGContent(image,clone_info,depth+1);
3978 clone_info=DestroyDrawInfo(clone_info);
3988 if (LocaleCompare(
"viewbox",keyword) == 0)
3990 (void) GetNextToken(q,&q,extent,token);
3991 graphic_context[n]->viewbox.x=CastDoubleToLong(ceil(
3992 GetDrawValue(token,&next_token)-0.5));
3993 if (token == next_token)
3994 ThrowPointExpectedException(image,token);
3995 (void) GetNextToken(q,&q,extent,token);
3996 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3997 ThrowPointExpectedException(image,token);
3999 (void) GetNextToken(q,&q,extent,token);
4000 graphic_context[n]->viewbox.y=CastDoubleToLong(ceil(
4001 GetDrawValue(token,&next_token)-0.5));
4002 if (token == next_token)
4003 ThrowPointExpectedException(image,token);
4004 (void) GetNextToken(q,&q,extent,token);
4005 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4006 ThrowPointExpectedException(image,token);
4008 (void) GetNextToken(q,&q,extent,token);
4009 graphic_context[n]->viewbox.width=CastDoubleToUnsigned(
4010 GetDrawValue(token,&next_token)+0.5);
4011 if (token == next_token)
4012 ThrowPointExpectedException(image,token);
4013 (void) GetNextToken(q,&q,extent,token);
4014 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4015 ThrowPointExpectedException(image,token);
4017 (void) GetNextToken(q,&q,extent,token);
4018 graphic_context[n]->viewbox.height=CastDoubleToUnsigned(
4019 GetDrawValue(token,&next_token)+0.5);
4020 if (token == next_token)
4021 ThrowPointExpectedException(image,token);
4030 if (LocaleCompare(
"word-spacing",keyword) == 0)
4032 (void) GetNextToken(q,&q,extent,token);
4033 graphic_context[n]->interword_spacing=GetDrawValue(token,
4035 if (token == next_token)
4036 ThrowPointExpectedException(image,token);
4048 if (status == MagickFalse)
4050 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
4051 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
4052 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
4053 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
4055 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
4056 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
4057 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
4058 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
4059 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
4061 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4064 if (primitive_type == UndefinedPrimitive)
4066 if ((draw_info->debug != MagickFalse) && (q > p))
4067 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int)
4074 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4075 if (primitive_info[i].text != (
char *) NULL)
4076 primitive_info[i].text=DestroyString(primitive_info[i].text);
4080 primitive_info[0].primitive=primitive_type;
4081 primitive_info[0].point.x=0.0;
4082 primitive_info[0].point.y=0.0;
4083 primitive_info[0].coordinates=0;
4084 primitive_info[0].method=FloodfillMethod;
4085 primitive_info[0].closed_subpath=MagickFalse;
4086 for (x=0; *q !=
'\0'; x++)
4091 if (IsValidPoint(q) == MagickFalse)
4093 (void) GetNextToken(q,&q,extent,token);
4094 point.x=GetDrawValue(token,&next_token);
4095 if (token == next_token)
4096 ThrowPointExpectedException(image,token);
4097 (void) GetNextToken(q,&q,extent,token);
4098 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4099 ThrowPointExpectedException(image,token);
4101 (void) GetNextToken(q,&q,extent,token);
4102 point.y=GetDrawValue(token,&next_token);
4103 if (token == next_token)
4104 ThrowPointExpectedException(image,token);
4105 (void) GetNextToken(q,(
const char **) NULL,extent,token);
4107 (void) GetNextToken(q,&q,extent,token);
4108 primitive_info[i].primitive=primitive_type;
4109 primitive_info[i].point=point;
4110 primitive_info[i].coordinates=0;
4111 primitive_info[i].method=FloodfillMethod;
4112 primitive_info[i].closed_subpath=MagickFalse;
4115 if (i < (ssize_t) number_points)
4117 status&=CheckPrimitiveExtent(&mvg_info,(
double) number_points);
4118 primitive_info=(*mvg_info.primitive_info);
4119 if (status == MagickFalse)
4122 if (status == MagickFalse)
4124 if (primitive_info[j].text != (
char *) NULL)
4125 primitive_info[j].text=DestroyString(primitive_info[j].text);
4126 primitive_info[j].primitive=primitive_type;
4127 primitive_info[j].coordinates=(size_t) x;
4128 primitive_info[j].method=FloodfillMethod;
4129 primitive_info[j].closed_subpath=MagickFalse;
4133 bounds.x1=primitive_info[j].point.x;
4134 bounds.y1=primitive_info[j].point.y;
4135 bounds.x2=primitive_info[j].point.x;
4136 bounds.y2=primitive_info[j].point.y;
4137 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4139 point=primitive_info[j+k].point;
4140 if (point.x < bounds.x1)
4142 if (point.y < bounds.y1)
4144 if (point.x > bounds.x2)
4146 if (point.y > bounds.y2)
4152 coordinates=(double) primitive_info[j].coordinates;
4153 switch (primitive_type)
4155 case RectanglePrimitive:
4160 case RoundRectanglePrimitive:
4167 alpha=bounds.x2-bounds.x1;
4168 beta=bounds.y2-bounds.y1;
4169 radius=hypot(alpha,beta);
4171 coordinates+=2.0*((size_t) ceil((
double) MagickPI*radius))+6.0*
4172 BezierQuantum+360.0;
4175 case BezierPrimitive:
4177 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4186 (void) GetNextToken(q,&q,extent,token);
4189 for (s=token; *s !=
'\0'; s=t)
4194 value=GetDrawValue(s,&t);
4203 for (s=token; *s !=
'\0'; s++)
4204 if (strspn(s,
"AaCcQqSsTt") != 0)
4205 coordinates+=(20.0*BezierQuantum)+360.0;
4211 if (status == MagickFalse)
4213 if (((
size_t) (i+coordinates)) >= number_points)
4218 number_points+=coordinates+1;
4219 if (number_points < (
size_t) coordinates)
4221 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4222 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
4228 status&=CheckPrimitiveExtent(&mvg_info,(
double) number_points);
4229 primitive_info=(*mvg_info.primitive_info);
4230 if (status == MagickFalse)
4233 status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4234 primitive_info=(*mvg_info.primitive_info);
4235 if (status == MagickFalse)
4238 switch (primitive_type)
4240 case PointPrimitive:
4243 if (primitive_info[j].coordinates != 1)
4248 status&=TracePoint(primitive_info+j,primitive_info[j].point);
4249 primitive_info=(*mvg_info.primitive_info);
4250 if (status == MagickFalse)
4252 i=(ssize_t) (j+primitive_info[j].coordinates);
4257 if (primitive_info[j].coordinates != 2)
4262 status&=TraceLine(primitive_info+j,primitive_info[j].point,
4263 primitive_info[j+1].point);
4264 primitive_info=(*mvg_info.primitive_info);
4265 if (status == MagickFalse)
4267 i=(ssize_t) (j+primitive_info[j].coordinates);
4270 case RectanglePrimitive:
4272 if (primitive_info[j].coordinates != 2)
4277 status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4278 primitive_info[j+1].point);
4279 primitive_info=(*mvg_info.primitive_info);
4280 if (status == MagickFalse)
4282 i=(ssize_t) (j+primitive_info[j].coordinates);
4285 case RoundRectanglePrimitive:
4287 if (primitive_info[j].coordinates != 3)
4292 if ((primitive_info[j+2].point.x < 0.0) ||
4293 (primitive_info[j+2].point.y < 0.0))
4298 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4303 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4308 status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4309 primitive_info[j+1].point,primitive_info[j+2].point);
4310 primitive_info=(*mvg_info.primitive_info);
4311 if (status == MagickFalse)
4313 i=(ssize_t) (j+primitive_info[j].coordinates);
4318 if (primitive_info[j].coordinates != 3)
4323 status&=TraceArc(&mvg_info,primitive_info[j].point,
4324 primitive_info[j+1].point,primitive_info[j+2].point);
4325 primitive_info=(*mvg_info.primitive_info);
4326 if (status == MagickFalse)
4328 i=(ssize_t) (j+primitive_info[j].coordinates);
4331 case EllipsePrimitive:
4333 if (primitive_info[j].coordinates != 3)
4338 if ((primitive_info[j+1].point.x < 0.0) ||
4339 (primitive_info[j+1].point.y < 0.0))
4344 status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4345 primitive_info[j+1].point,primitive_info[j+2].point);
4346 primitive_info=(*mvg_info.primitive_info);
4347 if (status == MagickFalse)
4349 i=(ssize_t) (j+primitive_info[j].coordinates);
4352 case CirclePrimitive:
4354 if (primitive_info[j].coordinates != 2)
4359 status&=TraceCircle(&mvg_info,primitive_info[j].point,
4360 primitive_info[j+1].point);
4361 primitive_info=(*mvg_info.primitive_info);
4362 if (status == MagickFalse)
4364 i=(ssize_t) (j+primitive_info[j].coordinates);
4367 case PolylinePrimitive:
4369 if (primitive_info[j].coordinates < 1)
4376 case PolygonPrimitive:
4378 if (primitive_info[j].coordinates < 3)
4383 primitive_info[i]=primitive_info[j];
4384 primitive_info[i].coordinates=0;
4385 primitive_info[j].coordinates++;
4386 primitive_info[j].closed_subpath=MagickTrue;
4390 case BezierPrimitive:
4392 if (primitive_info[j].coordinates < 3)
4397 status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4398 primitive_info=(*mvg_info.primitive_info);
4399 if (status == MagickFalse)
4401 i=(ssize_t) (j+primitive_info[j].coordinates);
4406 coordinates=(double) TracePath(image,&mvg_info,token);
4407 primitive_info=(*mvg_info.primitive_info);
4408 if (status == MagickFalse)
4410 if (coordinates < 0.0)
4415 i=(ssize_t) (j+coordinates);
4418 case ColorPrimitive:
4419 case MattePrimitive:
4424 if (primitive_info[j].coordinates != 1)
4429 (void) GetNextToken(q,&q,extent,token);
4430 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4436 primitive_info[j].method=(PaintMethod) method;
4442 geometry[MagickPathExtent];
4444 if (primitive_info[j].coordinates != 1)
4450 (void) GetNextToken(q,&q,extent,token);
4451 (void) CloneString(&primitive_info[j].text,token);
4455 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4456 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4457 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4459 mvg_info.point=primitive_info->point;
4460 primitive_info->point.x+=cursor;
4464 mvg_info.point=primitive_info->point;
4467 (void) FormatLocaleString(geometry,MagickPathExtent,
"%+f%+f",
4468 primitive_info->point.x,primitive_info->point.y);
4469 clone_info->render=MagickFalse;
4470 clone_info->text=AcquireString(token);
4471 status&=GetTypeMetrics(image,clone_info,&metrics);
4472 clone_info=DestroyDrawInfo(clone_info);
4473 cursor+=metrics.width;
4474 if (graphic_context[n]->compliance != SVGCompliance)
4478 case ImagePrimitive:
4480 if (primitive_info[j].coordinates != 2)
4485 (void) GetNextToken(q,&q,extent,token);
4486 (void) CloneString(&primitive_info[j].text,token);
4493 primitive_info[i].primitive=UndefinedPrimitive;
4494 if ((draw_info->debug != MagickFalse) && (q > p))
4495 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int) (q-p),p);
4499 status&=CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4500 &graphic_context[n]->affine));
4501 primitive_info=(*mvg_info.primitive_info);
4504 status&=CheckPrimitiveExtent(&mvg_info,(
double)
4505 graphic_context[n]->stroke_width);
4506 primitive_info=(*mvg_info.primitive_info);
4514 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4516 point=primitive_info[i].point;
4517 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4518 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4519 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4520 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4521 point=primitive_info[i].point;
4522 if (point.x < graphic_context[n]->bounds.x1)
4523 graphic_context[n]->bounds.x1=point.x;
4524 if (point.y < graphic_context[n]->bounds.y1)
4525 graphic_context[n]->bounds.y1=point.y;
4526 if (point.x > graphic_context[n]->bounds.x2)
4527 graphic_context[n]->bounds.x2=point.x;
4528 if (point.y > graphic_context[n]->bounds.y2)
4529 graphic_context[n]->bounds.y2=point.y;
4530 if (primitive_info[i].primitive == ImagePrimitive)
4532 if (i >= (ssize_t) number_points)
4533 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
4535 if (graphic_context[n]->render != MagickFalse)
4537 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4538 (graphic_context[n]->clip_mask != (
char *) NULL) &&
4539 (LocaleCompare(graphic_context[n]->clip_mask,
4540 graphic_context[n-1]->clip_mask) != 0))
4545 clip_path=(
const char *) GetValueFromSplayTree(macros,
4546 graphic_context[n]->clip_mask);
4547 if (clip_path != (
const char *) NULL)
4548 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4550 status&=DrawClipPath(image,graphic_context[n],
4551 graphic_context[n]->clip_mask);
4553 status&=DrawPrimitive(image,graphic_context[n],primitive_info);
4555 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4557 if (proceed == MagickFalse)
4562 if (draw_info->debug != MagickFalse)
4563 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end draw-image");
4567 macros=DestroySplayTree(macros);
4568 token=DestroyString(token);
4569 if (primitive_info != (PrimitiveInfo *) NULL)
4571 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4572 if (primitive_info[i].text != (
char *) NULL)
4573 primitive_info[i].text=DestroyString(primitive_info[i].text);
4574 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4576 primitive=DestroyString(primitive);
4577 for ( ; n >= 0; n--)
4578 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4579 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4580 if (status == MagickFalse)
4581 ThrowBinaryImageException(DrawError,
4582 "NonconformingDrawingPrimitiveDefinition",keyword);
4583 return(status != 0 ? MagickTrue : MagickFalse);
4586MagickExport MagickBooleanType DrawImage(Image *image,
const DrawInfo *draw_info)
4588 return(RenderMVGContent(image,draw_info,0));
4620MagickExport MagickBooleanType DrawPatternPath(Image *image,
4621 const DrawInfo *draw_info,
const char *name,Image **pattern)
4624 property[MaxTextExtent];
4640 assert(image != (Image *) NULL);
4641 assert(image->signature == MagickCoreSignature);
4642 assert(draw_info != (
const DrawInfo *) NULL);
4643 if (IsEventLogging() != MagickFalse)
4644 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4645 assert(name != (
const char *) NULL);
4646 (void) FormatLocaleString(property,MaxTextExtent,
"%s",name);
4647 path=GetImageArtifact(image,property);
4648 if (path == (
const char *) NULL)
4649 return(MagickFalse);
4650 (void) FormatLocaleString(property,MaxTextExtent,
"%s-geometry",name);
4651 geometry=GetImageArtifact(image,property);
4652 if (geometry == (
const char *) NULL)
4653 return(MagickFalse);
4654 if ((*pattern) != (Image *) NULL)
4655 *pattern=DestroyImage(*pattern);
4656 image_info=AcquireImageInfo();
4657 image_info->size=AcquireString(geometry);
4658 *pattern=AcquireImage(image_info);
4659 image_info=DestroyImageInfo(image_info);
4660 (void) QueryColorDatabase(
"#00000000",&(*pattern)->background_color,
4662 (void) SetImageBackgroundColor(*pattern);
4663 if (draw_info->debug != MagickFalse)
4664 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4665 "begin pattern-path %s %s",name,geometry);
4666 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4667 if (clone_info->fill_pattern != (Image *) NULL)
4668 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4669 if (clone_info->stroke_pattern != (Image *) NULL)
4670 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4671 (void) FormatLocaleString(property,MaxTextExtent,
"%s-type",name);
4672 type=GetImageArtifact(image,property);
4673 if (type != (
const char *) NULL)
4674 clone_info->gradient.type=(GradientType) ParseCommandOption(
4675 MagickGradientOptions,MagickFalse,type);
4676 (void) CloneString(&clone_info->primitive,path);
4677 status=RenderMVGContent(*pattern,clone_info,0);
4678 clone_info=DestroyDrawInfo(clone_info);
4679 if (draw_info->debug != MagickFalse)
4680 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end pattern-path");
4712static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4717 assert(polygon_info != (PolygonInfo **) NULL);
4718 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4719 if (polygon_info[i] != (PolygonInfo *) NULL)
4720 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4721 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4722 return(polygon_info);
4725static PolygonInfo **AcquirePolygonTLS(
const DrawInfo *draw_info,
4726 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
4729 *magick_restrict path_info;
4737 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4738 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4739 sizeof(*polygon_info));
4740 if (polygon_info == (PolygonInfo **) NULL)
4742 (void) ThrowMagickException(exception,GetMagickModule(),
4743 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4744 return((PolygonInfo **) NULL);
4746 (void) memset(polygon_info,0,number_threads*
sizeof(*polygon_info));
4747 path_info=ConvertPrimitiveToPath(draw_info,primitive_info,exception);
4748 if (path_info == (PathInfo *) NULL)
4749 return(DestroyPolygonTLS(polygon_info));
4750 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4751 if (polygon_info[0] == (PolygonInfo *) NULL)
4753 (void) ThrowMagickException(exception,GetMagickModule(),
4754 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4755 return(DestroyPolygonTLS(polygon_info));
4757 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4758 return(polygon_info);
4761static MagickBooleanType AcquirePolygonEdgesTLS(PolygonInfo **polygon_info,
4762 const size_t number_threads,ExceptionInfo *exception)
4767 for (i=1; i < (ssize_t) number_threads; i++)
4775 polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4776 sizeof(*polygon_info[i]));
4777 if (polygon_info[i] == (PolygonInfo *) NULL)
4779 (void) ThrowMagickException(exception,GetMagickModule(),
4780 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4781 return(MagickFalse);
4783 polygon_info[i]->number_edges=0;
4784 edge_info=polygon_info[0]->edges;
4785 polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4786 polygon_info[0]->number_edges,
sizeof(*edge_info));
4787 if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4789 (void) ThrowMagickException(exception,GetMagickModule(),
4790 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4791 return(MagickFalse);
4793 (void) memcpy(polygon_info[i]->edges,edge_info,
4794 polygon_info[0]->number_edges*
sizeof(*edge_info));
4795 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4796 polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4797 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4798 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4800 edge_info=polygon_info[0]->edges+j;
4801 polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4802 edge_info->number_points,
sizeof(*edge_info));
4803 if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4805 (void) ThrowMagickException(exception,GetMagickModule(),
4806 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4807 return(MagickFalse);
4809 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4810 edge_info->number_points*
sizeof(*edge_info->points));
4816static size_t DestroyEdge(PolygonInfo *polygon_info,
const ssize_t edge)
4818 assert(edge < (ssize_t) polygon_info->number_edges);
4819 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4820 polygon_info->edges[edge].points);
4821 polygon_info->number_edges--;
4822 if (edge < (ssize_t) polygon_info->number_edges)
4823 (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4824 (
size_t) (polygon_info->number_edges-edge)*
sizeof(*polygon_info->edges));
4825 return(polygon_info->number_edges);
4828static double GetOpacityPixel(PolygonInfo *polygon_info,
const double mid,
4829 const MagickBooleanType fill,
const FillRule fill_rule,
const ssize_t x,
4830 const ssize_t y,
double *stroke_opacity)
4857 *stroke_opacity=0.0;
4858 subpath_opacity=0.0;
4859 p=polygon_info->edges;
4860 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4862 if ((
double) y <= (p->bounds.y1-mid-0.5))
4864 if ((
double) y > (p->bounds.y2+mid+0.5))
4867 (void) DestroyEdge(polygon_info,j--);
4870 if (((
double) x <= (p->bounds.x1-mid-0.5)) ||
4871 ((
double) x > (p->bounds.x2+mid+0.5)))
4873 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4874 for ( ; i < (ssize_t) p->number_points; i++)
4876 if ((
double) y <= (p->points[i-1].y-mid-0.5))
4878 if ((
double) y > (p->points[i].y+mid+0.5))
4880 if (p->scanline != (
double) y)
4882 p->scanline=(double) y;
4883 p->highwater=(size_t) i;
4889 delta.x=(q+1)->x-q->x;
4890 delta.y=(q+1)->y-q->y;
4891 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4894 delta.x=(double) x-q->x;
4895 delta.y=(double) y-q->y;
4896 distance=delta.x*delta.x+delta.y*delta.y;
4900 alpha=delta.x*delta.x+delta.y*delta.y;
4903 delta.x=(double) x-(q+1)->x;
4904 delta.y=(double) y-(q+1)->y;
4905 distance=delta.x*delta.x+delta.y*delta.y;
4909 alpha=MagickSafeReciprocal(alpha);
4910 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4911 distance=alpha*beta*beta;
4918 if (p->ghostline == MagickFalse)
4921 if ((*stroke_opacity < 1.0) &&
4922 (distance <= ((alpha+0.25)*(alpha+0.25))))
4925 if (distance <= ((alpha+0.25)*(alpha+0.25)))
4926 *stroke_opacity=1.0;
4930 if (fabs(distance-1.0) >= MagickEpsilon)
4931 beta=sqrt((
double) distance);
4933 if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
4934 *stroke_opacity=(alpha-0.25)*(alpha-0.25);
4938 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
4940 if (distance <= 0.0)
4942 subpath_opacity=1.0;
4947 if (fabs(beta) < MagickEpsilon)
4950 if (fabs(distance-1.0) >= MagickEpsilon)
4951 beta=sqrt(distance);
4954 if (subpath_opacity < (alpha*alpha))
4955 subpath_opacity=alpha*alpha;
4961 if (fill == MagickFalse)
4963 if (subpath_opacity >= 1.0)
4969 p=polygon_info->edges;
4970 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4972 if ((
double) y <= p->bounds.y1)
4974 if (((
double) y > p->bounds.y2) || ((
double) x <= p->bounds.x1))
4976 if ((
double) x > p->bounds.x2)
4978 winding_number+=p->direction != 0 ? 1 : -1;
4981 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4982 for ( ; i < (ssize_t) (p->number_points-1); i++)
4983 if ((
double) y <= p->points[i].y)
4986 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4987 winding_number+=p->direction != 0 ? 1 : -1;
4989 if (fill_rule != NonZeroRule)
4991 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4995 if (MagickAbsoluteValue(winding_number) != 0)
4997 return(subpath_opacity);
5000static MagickBooleanType DrawPolygonPrimitive(Image *image,
5001 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info)
5003 typedef struct _ExtentInfo
5032 **magick_restrict polygon_info;
5047 assert(image != (Image *) NULL);
5048 assert(image->signature == MagickCoreSignature);
5049 assert(draw_info != (DrawInfo *) NULL);
5050 assert(draw_info->signature == MagickCoreSignature);
5051 assert(primitive_info != (PrimitiveInfo *) NULL);
5052 if (IsEventLogging() != MagickFalse)
5053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5054 if (primitive_info->coordinates <= 1)
5059 polygon_info=AcquirePolygonTLS(draw_info,primitive_info,&image->exception);
5060 if (polygon_info == (PolygonInfo **) NULL)
5061 return(MagickFalse);
5062 if (draw_info->debug != MagickFalse)
5063 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-polygon");
5064 fill=(primitive_info->method == FillToBorderMethod) ||
5065 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
5066 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5067 bounds=polygon_info[0]->edges[0].bounds;
5068 artifact=GetImageArtifact(image,
"draw:render-bounding-rectangles");
5069 if (IsStringTrue(artifact) != MagickFalse)
5070 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0]);
5071 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
5073 p=polygon_info[0]->edges+i;
5074 if (p->bounds.x1 < bounds.x1)
5075 bounds.x1=p->bounds.x1;
5076 if (p->bounds.y1 < bounds.y1)
5077 bounds.y1=p->bounds.y1;
5078 if (p->bounds.x2 > bounds.x2)
5079 bounds.x2=p->bounds.x2;
5080 if (p->bounds.y2 > bounds.y2)
5081 bounds.y2=p->bounds.y2;
5083 bounds.x1-=(mid+1.0);
5084 bounds.y1-=(mid+1.0);
5085 bounds.x2+=(mid+1.0);
5086 bounds.y2+=(mid+1.0);
5087 if ((bounds.x1 >= (
double) image->columns) ||
5088 (bounds.y1 >= (
double) image->rows) ||
5089 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5091 polygon_info=DestroyPolygonTLS(polygon_info);
5094 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5095 (
double) image->columns-1.0 : bounds.x1;
5096 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5097 (
double) image->rows-1.0 : bounds.y1;
5098 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5099 (
double) image->columns-1.0 : bounds.x2;
5100 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5101 (
double) image->rows-1.0 : bounds.y2;
5102 poly_extent.x1=CastDoubleToLong(ceil(bounds.x1-0.5));
5103 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5104 poly_extent.x2=CastDoubleToLong(floor(bounds.x2+0.5));
5105 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5106 number_threads=GetMagickNumberThreads(image,image,poly_extent.y2-
5107 poly_extent.y1+1,1);
5108 status=AcquirePolygonEdgesTLS(polygon_info,number_threads,&image->exception);
5109 if (status == MagickFalse)
5111 polygon_info=DestroyPolygonTLS(polygon_info);
5115 exception=(&image->exception);
5116 image_view=AcquireAuthenticCacheView(image,exception);
5117 if ((primitive_info->coordinates == 1) ||
5118 (polygon_info[0]->number_edges == 0))
5123#if defined(MAGICKCORE_OPENMP_SUPPORT)
5124 #pragma omp parallel for schedule(static) shared(status) \
5125 num_threads(number_threads)
5127 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5138 if (status == MagickFalse)
5141 q=GetCacheViewAuthenticPixels(image_view,x,y,(
size_t) (poly_extent.x2-
5143 if (q == (PixelPacket *) NULL)
5148 for ( ; x <= poly_extent.x2; x++)
5150 if ((x == CastDoubleToLong(ceil(primitive_info->point.x-0.5))) &&
5151 (y == CastDoubleToLong(ceil(primitive_info->point.y-0.5))))
5152 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,q);
5155 sync=SyncCacheViewAuthenticPixels(image_view,exception);
5156 if (sync == MagickFalse)
5159 image_view=DestroyCacheView(image_view);
5160 polygon_info=DestroyPolygonTLS(polygon_info);
5161 if (draw_info->debug != MagickFalse)
5162 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5163 " end draw-polygon");
5169 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5170 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5171#if defined(MAGICKCORE_OPENMP_SUPPORT)
5172 #pragma omp parallel for schedule(static) shared(status) \
5173 num_threads(number_threads)
5175 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5178 id = GetOpenMPThreadId();
5190 if (status == MagickFalse)
5192 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(
size_t)
5193 (poly_extent.x2-poly_extent.x1+1),1,exception);
5194 if (q == (PixelPacket *) NULL)
5199 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5208 fill_opacity=GetOpacityPixel(polygon_info[
id],mid,fill,
5209 draw_info->fill_rule,x,y,&stroke_opacity);
5210 if (draw_info->stroke_antialias == MagickFalse)
5212 fill_opacity=fill_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5213 stroke_opacity=stroke_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5215 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5217 fill_opacity=(double) ((MagickRealType) QuantumRange-fill_opacity*
5218 ((MagickRealType) QuantumRange-(MagickRealType) fill_color.opacity));
5219 MagickCompositeOver(&fill_color,(MagickRealType) fill_opacity,q,
5220 (MagickRealType) q->opacity,q);
5221 (void) GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5223 stroke_opacity=(double) ((MagickRealType) QuantumRange-stroke_opacity*
5224 ((MagickRealType) QuantumRange-(MagickRealType) stroke_color.opacity));
5225 MagickCompositeOver(&stroke_color,(MagickRealType) stroke_opacity,q,
5226 (MagickRealType) q->opacity,q);
5229 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5232 image_view=DestroyCacheView(image_view);
5233 polygon_info=DestroyPolygonTLS(polygon_info);
5234 if (draw_info->debug != MagickFalse)
5235 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-polygon");
5266static void LogPrimitiveInfo(
const PrimitiveInfo *primitive_info)
5292 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5293 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5294 switch (primitive_info->primitive)
5296 case PointPrimitive:
5298 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5299 "PointPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5300 methods[primitive_info->method]);
5303 case ColorPrimitive:
5305 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5306 "ColorPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5307 methods[primitive_info->method]);
5310 case MattePrimitive:
5312 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5313 "MattePrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5314 methods[primitive_info->method]);
5319 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5320 "TextPrimitive %.20g,%.20g",(
double) x,(
double) y);
5323 case ImagePrimitive:
5325 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5326 "ImagePrimitive %.20g,%.20g",(
double) x,(
double) y);
5333 p=primitive_info[0].point;
5336 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5338 point=primitive_info[i].point;
5339 if (coordinates <= 0)
5341 coordinates=(ssize_t) primitive_info[i].coordinates;
5342 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5343 " begin open (%.20g)",(
double) coordinates);
5346 point=primitive_info[i].point;
5347 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5348 (fabs(q.y-point.y) >= MagickEpsilon))
5349 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5350 " %.20g: %.18g,%.18g",(
double) coordinates,point.x,point.y);
5352 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
5353 " %.20g: %g %g (duplicate)",(
double) coordinates,point.x,point.y);
5356 if (coordinates > 0)
5358 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5359 (fabs(p.y-point.y) >= MagickEpsilon))
5360 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end last (%.20g)",
5361 (
double) coordinates);
5363 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end open (%.20g)",
5364 (
double) coordinates);
5368MagickExport MagickBooleanType DrawPrimitive(Image *image,
5369 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info)
5387 if (draw_info->debug != MagickFalse)
5389 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5390 " begin draw-primitive");
5391 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5392 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5393 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5394 draw_info->affine.tx,draw_info->affine.ty);
5396 exception=(&image->exception);
5398 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5399 ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
5400 (IsPixelGray(&draw_info->stroke) == MagickFalse)))
5401 status=SetImageColorspace(image,sRGBColorspace);
5402 if (draw_info->compliance == SVGCompliance)
5404 status&=SetImageClipMask(image,draw_info->clipping_mask);
5405 status&=SetImageMask(image,draw_info->composite_mask);
5407 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5408 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5409 image_view=AcquireAuthenticCacheView(image,exception);
5410 switch (primitive_info->primitive)
5412 case ColorPrimitive:
5414 switch (primitive_info->method)
5422 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5423 if (q == (PixelPacket *) NULL)
5425 (void) GetFillColor(draw_info,x,y,q);
5426 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5434 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5435 for (y=0; y < (ssize_t) image->rows; y++)
5440 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5442 if (q == (PixelPacket *) NULL)
5444 for (x=0; x < (ssize_t) image->columns; x++)
5446 if (IsColorSimilar(image,q,&target) == MagickFalse)
5451 (void) GetFillColor(draw_info,x,y,q);
5454 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5455 if (status == MagickFalse)
5460 case FloodfillMethod:
5461 case FillToBorderMethod:
5466 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5467 if (primitive_info->method == FillToBorderMethod)
5469 target.red=(MagickRealType) draw_info->border_color.red;
5470 target.green=(MagickRealType) draw_info->border_color.green;
5471 target.blue=(MagickRealType) draw_info->border_color.blue;
5473 status&=FloodfillPaintImage(image,DefaultChannels,draw_info,&target,x,
5474 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5480 for (y=0; y < (ssize_t) image->rows; y++)
5488 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5490 if (q == (PixelPacket *) NULL)
5492 for (x=0; x < (ssize_t) image->columns; x++)
5494 (void) GetFillColor(draw_info,x,y,q);
5497 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5498 if (status == MagickFalse)
5506 case MattePrimitive:
5508 if (image->matte == MagickFalse)
5509 status&=SetImageAlphaChannel(image,OpaqueAlphaChannel);
5510 switch (primitive_info->method)
5521 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5522 if (q == (PixelPacket *) NULL)
5524 (void) GetFillColor(draw_info,x,y,&pixel);
5525 SetPixelOpacity(q,pixel.opacity);
5526 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5535 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5536 for (y=0; y < (ssize_t) image->rows; y++)
5544 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5546 if (q == (PixelPacket *) NULL)
5548 for (x=0; x < (ssize_t) image->columns; x++)
5550 if (IsColorSimilar(image,q,&target) == MagickFalse)
5555 (void) GetFillColor(draw_info,x,y,&pixel);
5556 SetPixelOpacity(q,pixel.opacity);
5559 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5560 if (status == MagickFalse)
5565 case FloodfillMethod:
5566 case FillToBorderMethod:
5571 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5572 if (primitive_info->method == FillToBorderMethod)
5574 target.red=(MagickRealType) draw_info->border_color.red;
5575 target.green=(MagickRealType) draw_info->border_color.green;
5576 target.blue=(MagickRealType) draw_info->border_color.blue;
5578 status&=FloodfillPaintImage(image,OpacityChannel,draw_info,&target,x,
5579 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5588 for (y=0; y < (ssize_t) image->rows; y++)
5596 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5598 if (q == (PixelPacket *) NULL)
5600 for (x=0; x < (ssize_t) image->columns; x++)
5602 (void) GetFillColor(draw_info,x,y,&pixel);
5603 SetPixelOpacity(q,pixel.opacity);
5606 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5607 if (status == MagickFalse)
5615 case ImagePrimitive:
5621 composite_geometry[MaxTextExtent],
5622 magic[MagickPathExtent] = {
'\0'};
5638 if (primitive_info->text == (
char *) NULL)
5640 clone_info=AcquireImageInfo();
5641 composite_images=(Image *) NULL;
5642 if (LocaleNCompare(primitive_info->text,
"data:",5) == 0)
5643 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5646 if (*primitive_info->text !=
'\0')
5651 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5653 (void) SetImageInfo(clone_info,1,exception);
5654 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5656 if (clone_info->size != (
char *) NULL)
5657 clone_info->size=DestroyString(clone_info->size);
5658 if (clone_info->extract != (
char *) NULL)
5659 clone_info->extract=DestroyString(clone_info->extract);
5660 GetPathComponent(clone_info->filename,MagickPath,magic);
5662 composite_images=ReadImage(clone_info,exception);
5664 (
void) ThrowMagickException(exception,GetMagickModule(),
5665 FileOpenError,
"UnableToOpenFile",
"`%s'",clone_info->filename);
5667 clone_info=DestroyImageInfo(clone_info);
5668 if (composite_images == (Image *) NULL)
5673 composite_image=RemoveFirstImageFromList(&composite_images);
5674 composite_images=DestroyImageList(composite_images);
5675 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5676 NULL,(
void *) NULL);
5677 x1=CastDoubleToLong(ceil(primitive_info[1].point.x-0.5));
5678 y1=CastDoubleToLong(ceil(primitive_info[1].point.y-0.5));
5679 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5680 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5683 geometry[MaxTextExtent];
5688 (void) FormatLocaleString(geometry,MaxTextExtent,
"%gx%g!",
5689 primitive_info[1].point.x,primitive_info[1].point.y);
5690 composite_image->filter=image->filter;
5691 status&=TransformImage(&composite_image,(
char *) NULL,geometry);
5693 if (composite_image->matte == MagickFalse)
5694 status&=SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
5695 if (draw_info->opacity != OpaqueOpacity)
5696 status&=SetImageOpacity(composite_image,draw_info->opacity);
5697 SetGeometry(image,&geometry);
5698 image->gravity=draw_info->gravity;
5701 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
5702 "%.20gx%.20g%+.20g%+.20g",(
double) composite_image->columns,(
double)
5703 composite_image->rows,(
double) geometry.x,(
double) geometry.y);
5704 (void) ParseGravityGeometry(image,composite_geometry,&geometry,
5706 affine=draw_info->affine;
5707 affine.tx=(double) geometry.x;
5708 affine.ty=(double) geometry.y;
5709 composite_image->interpolate=image->interpolate;
5710 if ((draw_info->compose == OverCompositeOp) ||
5711 (draw_info->compose == SrcOverCompositeOp))
5712 status&=DrawAffineImage(image,composite_image,&affine);
5714 status&=CompositeImage(image,draw_info->compose,composite_image,
5715 geometry.x,geometry.y);
5716 composite_image=DestroyImage(composite_image);
5719 case PointPrimitive:
5727 if ((y < 0) || (y >= (ssize_t) image->rows))
5729 if ((x < 0) || (x >= (ssize_t) image->columns))
5731 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5732 if (q == (PixelPacket *) NULL)
5734 (void) GetFillColor(draw_info,x,y,&fill_color);
5735 MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
5736 (MagickRealType) q->opacity,q);
5737 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5743 geometry[MaxTextExtent];
5748 if (primitive_info->text == (
char *) NULL)
5750 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5751 (void) CloneString(&clone_info->text,primitive_info->text);
5752 (void) FormatLocaleString(geometry,MaxTextExtent,
"%+f%+f",
5753 primitive_info->point.x,primitive_info->point.y);
5754 (void) CloneString(&clone_info->geometry,geometry);
5755 status&=AnnotateImage(image,clone_info);
5756 clone_info=DestroyDrawInfo(clone_info);
5768 if (IsEventLogging() != MagickFalse)
5769 LogPrimitiveInfo(primitive_info);
5770 scale=ExpandAffine(&draw_info->affine);
5771 if ((draw_info->dash_pattern != (
double *) NULL) &&
5772 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5773 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5774 (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
5779 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5780 clone_info->stroke_width=0.0;
5781 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5782 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5783 clone_info=DestroyDrawInfo(clone_info);
5784 if (status != MagickFalse)
5785 status&=DrawDashPolygon(draw_info,primitive_info,image);
5788 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5790 ((draw_info->stroke.opacity != (Quantum) TransparentOpacity) ||
5791 (draw_info->stroke_pattern != (Image *) NULL)))
5803 closed_path=primitive_info[0].closed_subpath;
5804 i=(ssize_t) primitive_info[0].coordinates;
5805 x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5806 y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5807 if ((x < MagickEpsilon) && (y < MagickEpsilon))
5808 closed_path=MagickTrue;
5809 if ((((draw_info->linecap == RoundCap) ||
5810 (closed_path != MagickFalse)) &&
5811 (draw_info->linejoin == RoundJoin)) ||
5812 (primitive_info[i].primitive != UndefinedPrimitive))
5814 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5817 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5818 clone_info->stroke_width=0.0;
5819 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5820 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5821 clone_info=DestroyDrawInfo(clone_info);
5822 if (status != MagickFalse)
5823 status&=DrawStrokePolygon(image,draw_info,primitive_info);
5826 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5830 image_view=DestroyCacheView(image_view);
5831 if (draw_info->compliance == SVGCompliance)
5833 status&=SetImageClipMask(image,(Image *) NULL);
5834 status&=SetImageMask(image,(Image *) NULL);
5836 if (draw_info->debug != MagickFalse)
5837 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-primitive");
5838 return(status != 0 ? MagickTrue : MagickFalse);
5871static MagickBooleanType DrawRoundLinecap(Image *image,
5872 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info)
5880 if (primitive_info->coordinates < 1)
5881 return(MagickFalse);
5882 for (i=0; i < 4; i++)
5883 linecap[i]=(*primitive_info);
5884 linecap[0].coordinates=4;
5885 linecap[1].point.x+=2.0*MagickEpsilon;
5886 linecap[2].point.x+=2.0*MagickEpsilon;
5887 linecap[2].point.y+=2.0*MagickEpsilon;
5888 linecap[3].point.y+=2.0*MagickEpsilon;
5889 linecap[4].primitive=UndefinedPrimitive;
5890 return(DrawPolygonPrimitive(image,draw_info,linecap));
5893static MagickBooleanType DrawStrokePolygon(Image *image,
5894 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info)
5915 if (draw_info->debug != MagickFalse)
5916 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5917 " begin draw-stroke-polygon");
5918 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5919 clone_info->fill=draw_info->stroke;
5920 if (clone_info->fill_pattern != (Image *) NULL)
5921 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5922 if (clone_info->stroke_pattern != (Image *) NULL)
5923 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5924 MagickTrue,&clone_info->stroke_pattern->exception);
5925 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5926 clone_info->stroke_width=0.0;
5927 clone_info->fill_rule=NonZeroRule;
5929 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
5931 if (p->coordinates == 1)
5933 stroke_polygon=TraceStrokePolygon(draw_info,p,&image->exception);
5934 if (stroke_polygon == (PrimitiveInfo *) NULL)
5939 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
5940 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5943 q=p+p->coordinates-1;
5944 closed_path=p->closed_subpath;
5945 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5947 status&=DrawRoundLinecap(image,draw_info,p);
5948 status&=DrawRoundLinecap(image,draw_info,q);
5951 clone_info=DestroyDrawInfo(clone_info);
5952 if (draw_info->debug != MagickFalse)
5953 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5954 " end draw-stroke-polygon");
5955 return(status != 0 ? MagickTrue : MagickFalse);
5981MagickExport
void GetAffineMatrix(AffineMatrix *affine_matrix)
5983 assert(affine_matrix != (AffineMatrix *) NULL);
5984 if (IsEventLogging() != MagickFalse)
5985 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5986 (void) memset(affine_matrix,0,
sizeof(*affine_matrix));
5987 affine_matrix->sx=1.0;
5988 affine_matrix->sy=1.0;
6015MagickExport
void GetDrawInfo(
const ImageInfo *image_info,DrawInfo *draw_info)
6029 assert(draw_info != (DrawInfo *) NULL);
6030 if (IsEventLogging() != MagickFalse)
6031 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6032 (void) memset(draw_info,0,
sizeof(*draw_info));
6033 draw_info->image_info=CloneImageInfo(image_info);
6034 GetAffineMatrix(&draw_info->affine);
6035 exception=AcquireExceptionInfo();
6036 (void) QueryColorDatabase(
"#000F",&draw_info->fill,exception);
6037 (void) QueryColorDatabase(
"#FFF0",&draw_info->stroke,exception);
6038 draw_info->stroke_antialias=draw_info->image_info->antialias;
6039 draw_info->stroke_width=1.0;
6040 draw_info->fill_rule=EvenOddRule;
6041 draw_info->opacity=OpaqueOpacity;
6042 draw_info->fill_opacity=OpaqueOpacity;
6043 draw_info->stroke_opacity=OpaqueOpacity;
6044 draw_info->linecap=ButtCap;
6045 draw_info->linejoin=MiterJoin;
6046 draw_info->miterlimit=10;
6047 draw_info->decorate=NoDecoration;
6048 if (draw_info->image_info->font != (
char *) NULL)
6049 draw_info->font=AcquireString(draw_info->image_info->font);
6050 if (draw_info->image_info->density != (
char *) NULL)
6051 draw_info->density=AcquireString(draw_info->image_info->density);
6052 draw_info->text_antialias=draw_info->image_info->antialias;
6053 draw_info->pointsize=12.0;
6054 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
6055 draw_info->pointsize=draw_info->image_info->pointsize;
6056 draw_info->undercolor.opacity=(Quantum) TransparentOpacity;
6057 draw_info->border_color=draw_info->image_info->border_color;
6058 draw_info->compose=OverCompositeOp;
6059 if (draw_info->image_info->server_name != (
char *) NULL)
6060 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
6061 draw_info->render=MagickTrue;
6062 draw_info->clip_path=MagickFalse;
6063 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
6064 MagickTrue : MagickFalse;
6065 option=GetImageOption(draw_info->image_info,
"direction");
6066 if (option != (
const char *) NULL)
6067 draw_info->direction=(DirectionType) ParseCommandOption(
6068 MagickDirectionOptions,MagickFalse,option);
6070 draw_info->direction=UndefinedDirection;
6071 option=GetImageOption(draw_info->image_info,
"encoding");
6072 if (option != (
const char *) NULL)
6073 (void) CloneString(&draw_info->encoding,option);
6074 option=GetImageOption(draw_info->image_info,
"family");
6075 if (option != (
const char *) NULL)
6076 (void) CloneString(&draw_info->family,option);
6077 option=GetImageOption(draw_info->image_info,
"fill");
6078 if (option != (
const char *) NULL)
6079 (void) QueryColorDatabase(option,&draw_info->fill,exception);
6080 option=GetImageOption(draw_info->image_info,
"gravity");
6081 if (option != (
const char *) NULL)
6082 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
6083 MagickFalse,option);
6084 option=GetImageOption(draw_info->image_info,
"interline-spacing");
6085 if (option != (
const char *) NULL)
6086 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6087 option=GetImageOption(draw_info->image_info,
"interword-spacing");
6088 if (option != (
const char *) NULL)
6089 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6090 option=GetImageOption(draw_info->image_info,
"kerning");
6091 if (option != (
const char *) NULL)
6092 draw_info->kerning=GetDrawValue(option,&next_token);
6093 option=GetImageOption(draw_info->image_info,
"stroke");
6094 if (option != (
const char *) NULL)
6095 (void) QueryColorDatabase(option,&draw_info->stroke,exception);
6096 option=GetImageOption(draw_info->image_info,
"strokewidth");
6097 if (option != (
const char *) NULL)
6098 draw_info->stroke_width=GetDrawValue(option,&next_token);
6099 option=GetImageOption(draw_info->image_info,
"style");
6100 if (option != (
const char *) NULL)
6101 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6102 MagickFalse,option);
6103 option=GetImageOption(draw_info->image_info,
"undercolor");
6104 if (option != (
const char *) NULL)
6105 (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
6106 option=GetImageOption(draw_info->image_info,
"weight");
6107 if (option != (
const char *) NULL)
6112 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6114 weight=(ssize_t) StringToUnsignedLong(option);
6115 draw_info->weight=(size_t) weight;
6117 exception=DestroyExceptionInfo(exception);
6118 draw_info->signature=MagickCoreSignature;
6146static inline double Permutate(
const ssize_t n,
const ssize_t k)
6155 for (i=k+1; i <= n; i++)
6157 for (i=1; i <= (n-k); i++)
6178static MagickBooleanType TraceArc(MVGInfo *mvg_info,
const PointInfo start,
6179 const PointInfo end,
const PointInfo degrees)
6185 center.x=0.5*(end.x+start.x);
6186 center.y=0.5*(end.y+start.y);
6187 radius.x=fabs(center.x-start.x);
6188 radius.y=fabs(center.y-start.y);
6189 return(TraceEllipse(mvg_info,center,radius,degrees));
6192static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,
const PointInfo start,
6193 const PointInfo end,
const PointInfo arc,
const double angle,
6194 const MagickBooleanType large_arc,
const MagickBooleanType sweep)
6231 offset=mvg_info->offset;
6232 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6233 primitive_info->coordinates=0;
6234 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6235 (fabs(start.y-end.y) < MagickEpsilon))
6236 return(TracePoint(primitive_info,end));
6237 radii.x=fabs(arc.x);
6238 radii.y=fabs(arc.y);
6239 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6240 return(TraceLine(primitive_info,start,end));
6241 cosine=cos(DegreesToRadians(fmod((
double) angle,360.0)));
6242 sine=sin(DegreesToRadians(fmod((
double) angle,360.0)));
6243 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6244 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6245 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6247 if (delta < MagickEpsilon)
6248 return(TraceLine(primitive_info,start,end));
6251 radii.x*=sqrt((
double) delta);
6252 radii.y*=sqrt((
double) delta);
6254 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6255 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6256 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6257 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6258 alpha=points[1].x-points[0].x;
6259 beta=points[1].y-points[0].y;
6260 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6261 return(TraceLine(primitive_info,start,end));
6262 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6267 factor=sqrt((
double) factor);
6268 if (sweep == large_arc)
6271 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6272 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6273 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6274 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6275 if ((theta < 0.0) && (sweep != MagickFalse))
6276 theta+=2.0*MagickPI;
6278 if ((theta > 0.0) && (sweep == MagickFalse))
6279 theta-=2.0*MagickPI;
6280 arc_segments=(size_t) CastDoubleToLong(ceil(fabs((
double) (theta/(0.5*
6281 MagickPI+MagickEpsilon)))));
6284 for (i=0; i < (ssize_t) arc_segments; i++)
6286 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6287 gamma=(8.0/3.0)*sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))*
6288 sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))/
6289 sin(fmod((
double) beta,DegreesToRadians(360.0)));
6290 points[0].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) i*theta/
6291 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((
double) (alpha+
6292 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6293 points[0].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) i*theta/
6294 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((
double) (alpha+
6295 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6296 points[2].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) (i+1)*
6297 theta/arc_segments),DegreesToRadians(360.0))));
6298 points[2].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) (i+1)*
6299 theta/arc_segments),DegreesToRadians(360.0))));
6300 points[1].x=(double) (points[2].x+gamma*sin(fmod((
double) (alpha+(
double)
6301 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6302 points[1].y=(double) (points[2].y-gamma*cos(fmod((
double) (alpha+(
double)
6303 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6304 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6305 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6306 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6308 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6310 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6312 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6314 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6316 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6318 if (i == (ssize_t) (arc_segments-1))
6320 status&=TraceBezier(mvg_info,4);
6323 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6324 mvg_info->offset+=p->coordinates;
6325 p+=(ptrdiff_t) p->coordinates;
6328 return(MagickFalse);
6329 mvg_info->offset=offset;
6330 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6331 primitive_info->coordinates=(size_t) (p-primitive_info);
6332 primitive_info->closed_subpath=MagickFalse;
6333 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6335 p->primitive=primitive_info->primitive;
6341static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6342 const size_t number_coordinates)
6371 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6372 quantum=number_coordinates;
6373 for (i=0; i < (ssize_t) number_coordinates; i++)
6375 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6377 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6378 if (alpha > (
double) GetMaxMemoryRequest())
6380 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6381 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6382 return(MagickFalse);
6384 if (alpha > (
double) quantum)
6385 quantum=(size_t) alpha;
6386 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6387 if (alpha > (
double) quantum)
6388 quantum=(size_t) alpha;
6391 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6392 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6393 if (quantum > (
double) GetMaxMemoryRequest())
6395 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6396 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6397 return(MagickFalse);
6399 coefficients=(
double *) AcquireQuantumMemory(number_coordinates,
6400 sizeof(*coefficients));
6401 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6403 if ((coefficients == (
double *) NULL) || (points == (PointInfo *) NULL))
6405 if (points != (PointInfo *) NULL)
6406 points=(PointInfo *) RelinquishMagickMemory(points);
6407 if (coefficients != (
double *) NULL)
6408 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6409 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6410 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6411 return(MagickFalse);
6413 control_points=quantum*number_coordinates;
6414 if (CheckPrimitiveExtent(mvg_info,(
double) control_points+1) == MagickFalse)
6416 points=(PointInfo *) RelinquishMagickMemory(points);
6417 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6418 return(MagickFalse);
6420 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6424 end=primitive_info[number_coordinates-1].point;
6425 for (i=0; i < (ssize_t) number_coordinates; i++)
6426 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6428 for (i=0; i < (ssize_t) control_points; i++)
6433 alpha=pow((
double) (1.0-weight),(
double) number_coordinates-1.0);
6434 for (j=0; j < (ssize_t) number_coordinates; j++)
6436 point.x+=alpha*coefficients[j]*p->point.x;
6437 point.y+=alpha*coefficients[j]*p->point.y;
6438 alpha*=weight/(1.0-weight);
6442 weight+=1.0/control_points;
6448 for (i=0; i < (ssize_t) control_points; i++)
6450 if (TracePoint(p,points[i]) == MagickFalse)
6452 points=(PointInfo *) RelinquishMagickMemory(points);
6453 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6454 return(MagickFalse);
6456 p+=(ptrdiff_t) p->coordinates;
6458 if (TracePoint(p,end) == MagickFalse)
6460 points=(PointInfo *) RelinquishMagickMemory(points);
6461 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6462 return(MagickFalse);
6464 p+=(ptrdiff_t) p->coordinates;
6465 primitive_info->coordinates=(size_t) (p-primitive_info);
6466 primitive_info->closed_subpath=MagickFalse;
6467 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6469 p->primitive=primitive_info->primitive;
6472 points=(PointInfo *) RelinquishMagickMemory(points);
6473 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6477static MagickBooleanType TraceCircle(MVGInfo *mvg_info,
const PointInfo start,
6478 const PointInfo end)
6489 alpha=end.x-start.x;
6491 radius=hypot((
double) alpha,(
double) beta);
6492 offset.x=(double) radius;
6493 offset.y=(double) radius;
6496 return(TraceEllipse(mvg_info,start,offset,degrees));
6499static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,
const PointInfo center,
6500 const PointInfo radii,
const PointInfo arc)
6525 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6526 primitive_info->coordinates=0;
6527 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6529 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6530 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6531 angle.x=DegreesToRadians(arc.x);
6535 angle.y=DegreesToRadians(y);
6536 coordinates=ceil((angle.y-angle.x)/step+1.0);
6537 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6538 return(MagickFalse);
6540 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6541 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6543 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6544 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6545 if (i++ >= (ssize_t) coordinates)
6547 if (TracePoint(p,point) == MagickFalse)
6548 return(MagickFalse);
6549 p+=(ptrdiff_t) p->coordinates;
6551 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6552 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6553 if (TracePoint(p,point) == MagickFalse)
6554 return(MagickFalse);
6555 p+=(ptrdiff_t) p->coordinates;
6556 primitive_info->coordinates=(size_t) (p-primitive_info);
6557 primitive_info->closed_subpath=MagickFalse;
6558 x=fabs(primitive_info[0].point.x-
6559 primitive_info[primitive_info->coordinates-1].point.x);
6560 y=fabs(primitive_info[0].point.y-
6561 primitive_info[primitive_info->coordinates-1].point.y);
6562 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6563 primitive_info->closed_subpath=MagickTrue;
6564 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6566 p->primitive=primitive_info->primitive;
6572static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6573 const PointInfo start,
const PointInfo end)
6575 if (TracePoint(primitive_info,start) == MagickFalse)
6576 return(MagickFalse);
6577 if (TracePoint(primitive_info+1,end) == MagickFalse)
6578 return(MagickFalse);
6579 (primitive_info+1)->primitive=primitive_info->primitive;
6580 primitive_info->coordinates=2;
6581 primitive_info->closed_subpath=MagickFalse;
6585static ssize_t TracePath(Image *image,MVGInfo *mvg_info,
const char *path)
6589 token[MaxTextExtent] =
"";
6607 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6630 subpath_offset=mvg_info->offset;
6631 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6634 number_coordinates=0;
6637 primitive_type=primitive_info->primitive;
6639 for (p=path; *p !=
'\0'; )
6641 if (status == MagickFalse)
6643 while (isspace((
int) ((
unsigned char) *p)) != 0)
6647 last_attribute=attribute;
6648 attribute=(int) (*p++);
6658 large_arc = MagickFalse,
6659 sweep = MagickFalse;
6669 (void) GetNextToken(p,&p,MaxTextExtent,token);
6670 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6671 ThrowPointExpectedException(image,token);
6673 (void) GetNextToken(p,&p,MaxTextExtent,token);
6674 arc.x=GetDrawValue(token,&next_token);
6675 if (token == next_token)
6676 ThrowPointExpectedException(image,token);
6677 (void) GetNextToken(p,&p,MaxTextExtent,token);
6678 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6679 ThrowPointExpectedException(image,token);
6681 (void) GetNextToken(p,&p,MaxTextExtent,token);
6682 arc.y=GetDrawValue(token,&next_token);
6683 if (token == next_token)
6684 ThrowPointExpectedException(image,token);
6685 (void) GetNextToken(p,&p,MaxTextExtent,token);
6686 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6687 ThrowPointExpectedException(image,token);
6689 (void) GetNextToken(p,&p,MaxTextExtent,token);
6690 angle=GetDrawValue(token,&next_token);
6691 if (token == next_token)
6692 ThrowPointExpectedException(image,token);
6693 (void) GetNextToken(p,&p,MaxTextExtent,token);
6694 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6695 ThrowPointExpectedException(image,token);
6697 (void) GetNextToken(p,&p,MaxTextExtent,token);
6698 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6699 (void) GetNextToken(p,&p,MaxTextExtent,token);
6700 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6701 ThrowPointExpectedException(image,token);
6703 (void) GetNextToken(p,&p,MaxTextExtent,token);
6704 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6705 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6706 ThrowPointExpectedException(image,token);
6708 (void) GetNextToken(p,&p,MaxTextExtent,token);
6709 (void) GetNextToken(p,&p,MaxTextExtent,token);
6710 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6711 ThrowPointExpectedException(image,token);
6713 (void) GetNextToken(p,&p,MaxTextExtent,token);
6714 x=GetDrawValue(token,&next_token);
6715 if (token == next_token)
6716 ThrowPointExpectedException(image,token);
6717 (void) GetNextToken(p,&p,MaxTextExtent,token);
6718 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6719 ThrowPointExpectedException(image,token);
6721 (void) GetNextToken(p,&p,MaxTextExtent,token);
6722 y=GetDrawValue(token,&next_token);
6723 if (token == next_token)
6724 ThrowPointExpectedException(image,token);
6725 end.x=(double) (attribute == (
int)
'A' ? x : point.x+x);
6726 end.y=(double) (attribute == (
int)
'A' ? y : point.y+y);
6727 status&=TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep);
6728 q=(*mvg_info->primitive_info)+mvg_info->offset;
6729 mvg_info->offset+=q->coordinates;
6730 q+=(ptrdiff_t) q->coordinates;
6732 while (isspace((
int) ((
unsigned char) *p)) != 0)
6736 }
while (IsValidPoint(p) != MagickFalse);
6748 for (i=1; i < 4; i++)
6750 (void) GetNextToken(p,&p,MaxTextExtent,token);
6751 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6752 ThrowPointExpectedException(image,token);
6754 (void) GetNextToken(p,&p,MaxTextExtent,token);
6755 x=GetDrawValue(token,&next_token);
6756 if (token == next_token)
6757 ThrowPointExpectedException(image,token);
6758 (void) GetNextToken(p,&p,MaxTextExtent,token);
6759 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6760 ThrowPointExpectedException(image,token);
6762 (void) GetNextToken(p,&p,MaxTextExtent,token);
6763 y=GetDrawValue(token,&next_token);
6764 if (token == next_token)
6765 ThrowPointExpectedException(image,token);
6766 end.x=(double) (attribute == (
int)
'C' ? x : point.x+x);
6767 end.y=(double) (attribute == (
int)
'C' ? y : point.y+y);
6770 for (i=0; i < 4; i++)
6771 (q+i)->point=points[i];
6772 if (TraceBezier(mvg_info,4) == MagickFalse)
6774 q=(*mvg_info->primitive_info)+mvg_info->offset;
6775 mvg_info->offset+=q->coordinates;
6776 q+=(ptrdiff_t) q->coordinates;
6778 while (isspace((
int) ((
unsigned char) *p)) != 0)
6782 }
while (IsValidPoint(p) != MagickFalse);
6790 (void) GetNextToken(p,&p,MaxTextExtent,token);
6791 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6792 ThrowPointExpectedException(image,token);
6794 (void) GetNextToken(p,&p,MaxTextExtent,token);
6795 x=GetDrawValue(token,&next_token);
6796 if (token == next_token)
6797 ThrowPointExpectedException(image,token);
6798 point.x=(double) (attribute == (
int)
'H' ? x: point.x+x);
6799 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6801 q=(*mvg_info->primitive_info)+mvg_info->offset;
6802 if (TracePoint(q,point) == MagickFalse)
6804 mvg_info->offset+=q->coordinates;
6805 q+=(ptrdiff_t) q->coordinates;
6806 while (isspace((
int) ((
unsigned char) *p)) != 0)
6810 }
while (IsValidPoint(p) != MagickFalse);
6821 (void) GetNextToken(p,&p,MaxTextExtent,token);
6822 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6823 ThrowPointExpectedException(image,token);
6825 (void) GetNextToken(p,&p,MaxTextExtent,token);
6826 x=GetDrawValue(token,&next_token);
6827 if (token == next_token)
6828 ThrowPointExpectedException(image,token);
6829 (void) GetNextToken(p,&p,MaxTextExtent,token);
6830 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6831 ThrowPointExpectedException(image,token);
6833 (void) GetNextToken(p,&p,MaxTextExtent,token);
6834 y=GetDrawValue(token,&next_token);
6835 if (token == next_token)
6836 ThrowPointExpectedException(image,token);
6837 point.x=(double) (attribute == (
int)
'L' ? x : point.x+x);
6838 point.y=(double) (attribute == (
int)
'L' ? y : point.y+y);
6839 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6841 q=(*mvg_info->primitive_info)+mvg_info->offset;
6842 if (TracePoint(q,point) == MagickFalse)
6844 mvg_info->offset+=q->coordinates;
6845 q+=(ptrdiff_t) q->coordinates;
6846 while (isspace((
int) ((
unsigned char) *p)) != 0)
6850 }
while (IsValidPoint(p) != MagickFalse);
6859 if (mvg_info->offset != subpath_offset)
6861 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6862 primitive_info->coordinates=(size_t) (q-primitive_info);
6863 number_coordinates+=primitive_info->coordinates;
6865 subpath_offset=mvg_info->offset;
6870 (void) GetNextToken(p,&p,MaxTextExtent,token);
6871 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6872 ThrowPointExpectedException(image,token);
6874 (void) GetNextToken(p,&p,MaxTextExtent,token);
6875 x=GetDrawValue(token,&next_token);
6876 if (token == next_token)
6877 ThrowPointExpectedException(image,token);
6878 (void) GetNextToken(p,&p,MaxTextExtent,token);
6879 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6880 ThrowPointExpectedException(image,token);
6882 (void) GetNextToken(p,&p,MaxTextExtent,token);
6883 y=GetDrawValue(token,&next_token);
6884 if (token == next_token)
6885 ThrowPointExpectedException(image,token);
6886 point.x=(double) (attribute == (
int)
'M' ? x : point.x+x);
6887 point.y=(double) (attribute == (
int)
'M' ? y : point.y+y);
6891 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6893 q=(*mvg_info->primitive_info)+mvg_info->offset;
6894 if (TracePoint(q,point) == MagickFalse)
6896 mvg_info->offset+=q->coordinates;
6897 q+=(ptrdiff_t) q->coordinates;
6898 while (isspace((
int) ((
unsigned char) *p)) != 0)
6902 }
while (IsValidPoint(p) != MagickFalse);
6914 for (i=1; i < 3; i++)
6916 (void) GetNextToken(p,&p,MaxTextExtent,token);
6917 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6918 ThrowPointExpectedException(image,token);
6920 (void) GetNextToken(p,&p,MaxTextExtent,token);
6921 x=GetDrawValue(token,&next_token);
6922 if (token == next_token)
6923 ThrowPointExpectedException(image,token);
6924 (void) GetNextToken(p,&p,MaxTextExtent,token);
6925 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6926 ThrowPointExpectedException(image,token);
6928 (void) GetNextToken(p,&p,MaxTextExtent,token);
6929 y=GetDrawValue(token,&next_token);
6930 if (token == next_token)
6931 ThrowPointExpectedException(image,token);
6934 end.x=(double) (attribute == (
int)
'Q' ? x : point.x+x);
6935 end.y=(double) (attribute == (
int)
'Q' ? y : point.y+y);
6938 for (i=0; i < 3; i++)
6939 (q+i)->point=points[i];
6940 if (TraceBezier(mvg_info,3) == MagickFalse)
6942 q=(*mvg_info->primitive_info)+mvg_info->offset;
6943 mvg_info->offset+=q->coordinates;
6944 q+=(ptrdiff_t) q->coordinates;
6946 while (isspace((
int) ((
unsigned char) *p)) != 0)
6950 }
while (IsValidPoint(p) != MagickFalse);
6961 points[0]=points[3];
6962 points[1].x=2.0*points[3].x-points[2].x;
6963 points[1].y=2.0*points[3].y-points[2].y;
6964 for (i=2; i < 4; i++)
6966 (void) GetNextToken(p,&p,MaxTextExtent,token);
6967 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6968 ThrowPointExpectedException(image,token);
6970 (void) GetNextToken(p,&p,MaxTextExtent,token);
6971 x=GetDrawValue(token,&next_token);
6972 if (token == next_token)
6973 ThrowPointExpectedException(image,token);
6974 (void) GetNextToken(p,&p,MaxTextExtent,token);
6975 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6976 ThrowPointExpectedException(image,token);
6978 (void) GetNextToken(p,&p,MaxTextExtent,token);
6979 y=GetDrawValue(token,&next_token);
6980 if (token == next_token)
6981 ThrowPointExpectedException(image,token);
6984 end.x=(double) (attribute == (
int)
'S' ? x : point.x+x);
6985 end.y=(double) (attribute == (
int)
'S' ? y : point.y+y);
6988 if (strchr(
"CcSs",last_attribute) == (
char *) NULL)
6993 for (i=0; i < 4; i++)
6994 (q+i)->point=points[i];
6995 if (TraceBezier(mvg_info,4) == MagickFalse)
6997 q=(*mvg_info->primitive_info)+mvg_info->offset;
6998 mvg_info->offset+=q->coordinates;
6999 q+=(ptrdiff_t) q->coordinates;
7001 last_attribute=attribute;
7002 while (isspace((
int) ((
unsigned char) *p)) != 0)
7006 }
while (IsValidPoint(p) != MagickFalse);
7017 points[0]=points[2];
7018 points[1].x=2.0*points[2].x-points[1].x;
7019 points[1].y=2.0*points[2].y-points[1].y;
7020 for (i=2; i < 3; i++)
7022 (void) GetNextToken(p,&p,MaxTextExtent,token);
7023 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7024 ThrowPointExpectedException(image,token);
7026 (void) GetNextToken(p,&p,MaxTextExtent,token);
7027 x=GetDrawValue(token,&next_token);
7028 if (token == next_token)
7029 ThrowPointExpectedException(image,token);
7030 (void) GetNextToken(p,&p,MaxTextExtent,token);
7031 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7032 ThrowPointExpectedException(image,token);
7034 (void) GetNextToken(p,&p,MaxTextExtent,token);
7035 y=GetDrawValue(token,&next_token);
7036 if (token == next_token)
7037 ThrowPointExpectedException(image,token);
7038 end.x=(double) (attribute == (
int)
'T' ? x : point.x+x);
7039 end.y=(double) (attribute == (
int)
'T' ? y : point.y+y);
7042 if (status == MagickFalse)
7044 if (strchr(
"QqTt",last_attribute) == (
char *) NULL)
7049 for (i=0; i < 3; i++)
7050 (q+i)->point=points[i];
7051 if (TraceBezier(mvg_info,3) == MagickFalse)
7053 q=(*mvg_info->primitive_info)+mvg_info->offset;
7054 mvg_info->offset+=q->coordinates;
7055 q+=(ptrdiff_t) q->coordinates;
7057 last_attribute=attribute;
7058 while (isspace((
int) ((
unsigned char) *p)) != 0)
7062 }
while (IsValidPoint(p) != MagickFalse);
7073 (void) GetNextToken(p,&p,MaxTextExtent,token);
7074 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7075 ThrowPointExpectedException(image,token);
7077 (void) GetNextToken(p,&p,MaxTextExtent,token);
7078 y=GetDrawValue(token,&next_token);
7079 if (token == next_token)
7080 ThrowPointExpectedException(image,token);
7081 point.y=(double) (attribute == (
int)
'V' ? y : point.y+y);
7082 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7084 q=(*mvg_info->primitive_info)+mvg_info->offset;
7085 if (TracePoint(q,point) == MagickFalse)
7087 mvg_info->offset+=q->coordinates;
7088 q+=(ptrdiff_t) q->coordinates;
7089 while (isspace((
int) ((
unsigned char) *p)) != 0)
7093 }
while (IsValidPoint(p) != MagickFalse);
7103 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7105 q=(*mvg_info->primitive_info)+mvg_info->offset;
7106 if (TracePoint(q,point) == MagickFalse)
7108 mvg_info->offset+=q->coordinates;
7109 q+=(ptrdiff_t) q->coordinates;
7110 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7111 primitive_info->coordinates=(size_t) (q-primitive_info);
7112 primitive_info->closed_subpath=MagickTrue;
7113 number_coordinates+=primitive_info->coordinates;
7115 subpath_offset=mvg_info->offset;
7121 ThrowPointExpectedException(image,token);
7126 if (status == MagickFalse)
7128 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7129 primitive_info->coordinates=(size_t) (q-primitive_info);
7130 number_coordinates+=primitive_info->coordinates;
7131 for (i=0; i < (ssize_t) number_coordinates; i++)
7134 q->primitive=primitive_type;
7136 q->method=FillToBorderMethod;
7139 return((ssize_t) number_coordinates);
7142static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7143 const PointInfo start,
const PointInfo end)
7155 if (TracePoint(p,start) == MagickFalse)
7156 return(MagickFalse);
7157 p+=(ptrdiff_t) p->coordinates;
7160 if (TracePoint(p,point) == MagickFalse)
7161 return(MagickFalse);
7162 p+=(ptrdiff_t) p->coordinates;
7163 if (TracePoint(p,end) == MagickFalse)
7164 return(MagickFalse);
7165 p+=(ptrdiff_t) p->coordinates;
7168 if (TracePoint(p,point) == MagickFalse)
7169 return(MagickFalse);
7170 p+=(ptrdiff_t) p->coordinates;
7171 if (TracePoint(p,start) == MagickFalse)
7172 return(MagickFalse);
7173 p+=(ptrdiff_t) p->coordinates;
7174 primitive_info->coordinates=(size_t) (p-primitive_info);
7175 primitive_info->closed_subpath=MagickTrue;
7176 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7178 p->primitive=primitive_info->primitive;
7184static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7185 const PointInfo start,
const PointInfo end,PointInfo arc)
7204 offset=mvg_info->offset;
7205 segment.x=fabs(end.x-start.x);
7206 segment.y=fabs(end.y-start.y);
7207 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7209 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7212 if (arc.x > (0.5*segment.x))
7213 arc.x=0.5*segment.x;
7214 if (arc.y > (0.5*segment.y))
7215 arc.y=0.5*segment.y;
7216 point.x=start.x+segment.x-arc.x;
7217 point.y=start.y+arc.y;
7220 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7221 return(MagickFalse);
7222 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7223 mvg_info->offset+=p->coordinates;
7224 point.x=start.x+segment.x-arc.x;
7225 point.y=start.y+segment.y-arc.y;
7228 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7229 return(MagickFalse);
7230 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7231 mvg_info->offset+=p->coordinates;
7232 point.x=start.x+arc.x;
7233 point.y=start.y+segment.y-arc.y;
7236 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7237 return(MagickFalse);
7238 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7239 mvg_info->offset+=p->coordinates;
7240 point.x=start.x+arc.x;
7241 point.y=start.y+arc.y;
7244 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7245 return(MagickFalse);
7246 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7247 mvg_info->offset+=p->coordinates;
7248 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7249 return(MagickFalse);
7250 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7251 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7252 return(MagickFalse);
7253 p+=(ptrdiff_t) p->coordinates;
7254 mvg_info->offset=offset;
7255 primitive_info=(*mvg_info->primitive_info)+offset;
7256 primitive_info->coordinates=(size_t) (p-primitive_info);
7257 primitive_info->closed_subpath=MagickTrue;
7258 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7260 p->primitive=primitive_info->primitive;
7266static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7267 const size_t number_vertices,
const double offset)
7284 for (i=1; i < (ssize_t) number_vertices; i++)
7286 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7287 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7288 if ((fabs((
double) dx) >= MagickEpsilon) ||
7289 (fabs((
double) dy) >= MagickEpsilon))
7292 if (i == (ssize_t) number_vertices)
7293 i=(ssize_t) number_vertices-1L;
7294 distance=hypot((
double) dx,(
double) dy);
7295 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7296 dx*(distance+offset)/distance);
7297 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7298 dy*(distance+offset)/distance);
7299 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7301 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7302 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7303 if ((fabs((
double) dx) >= MagickEpsilon) ||
7304 (fabs((
double) dy) >= MagickEpsilon))
7307 distance=hypot((
double) dx,(
double) dy);
7308 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7309 dx*(distance+offset)/distance);
7310 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7311 dy*(distance+offset)/distance);
7315static PrimitiveInfo *TraceStrokePolygon(
const DrawInfo *draw_info,
7316 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7318#define MaxStrokePad (6*BezierQuantum+360)
7319#define CheckPathExtent(pad_p,pad_q) \
7321 if ((pad_p) > MaxBezierCoordinates) \
7322 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7324 if ((ssize_t) (p+(pad_p)) >= (ssize_t) extent_p) \
7326 if (~extent_p < (pad_p)) \
7327 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7330 extent_p+=(pad_p); \
7331 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7332 MaxStrokePad,sizeof(*stroke_p)); \
7335 if ((pad_q) > MaxBezierCoordinates) \
7336 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7338 if ((ssize_t) (q+(pad_q)) >= (ssize_t) extent_q) \
7340 if (~extent_q < (pad_q)) \
7341 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7344 extent_q+=(pad_q); \
7345 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7346 MaxStrokePad,sizeof(*stroke_q)); \
7349 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7351 if (stroke_p != (PointInfo *) NULL) \
7352 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7353 if (stroke_q != (PointInfo *) NULL) \
7354 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7355 polygon_primitive=(PrimitiveInfo *) \
7356 RelinquishMagickMemory(polygon_primitive); \
7357 (void) ThrowMagickException(exception,GetMagickModule(), \
7358 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7359 return((PrimitiveInfo *) NULL); \
7363 typedef struct _StrokeSegment
7409 inverse_slope = {0.0, 0.0},
7416 number_vertices=primitive_info->coordinates;
7417 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7418 number_vertices+2UL,
sizeof(*polygon_primitive));
7419 if (polygon_primitive == (PrimitiveInfo *) NULL)
7421 (void) ThrowMagickException(exception,GetMagickModule(),
7422 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7423 return((PrimitiveInfo *) NULL);
7425 (void) memcpy(polygon_primitive,primitive_info,(
size_t) number_vertices*
7426 sizeof(*polygon_primitive));
7427 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7428 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7429 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7430 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7431 if ((draw_info->linejoin == MiterJoin) ||
7432 ((draw_info->linejoin == RoundJoin) && (closed_path != MagickFalse)))
7434 polygon_primitive[number_vertices]=primitive_info[1];
7437 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7441 closed_path=primitive_info[0].closed_subpath;
7444 for (n=1; n < (ssize_t) number_vertices; n++)
7446 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7447 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7448 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7451 if (n == (ssize_t) number_vertices)
7453 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7458 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7459 sizeof(*stroke_polygon));
7460 stroke_polygon[0]=polygon_primitive[0];
7461 stroke_polygon[0].coordinates=0;
7462 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7464 return(stroke_polygon);
7466 n=(ssize_t) number_vertices-1L;
7468 extent_p=2*number_vertices;
7469 extent_q=2*number_vertices;
7470 stroke_p=(PointInfo *) AcquireQuantumMemory((
size_t) extent_p+MaxStrokePad,
7472 stroke_q=(PointInfo *) AcquireQuantumMemory((
size_t) extent_q+MaxStrokePad,
7474 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7476 if (stroke_p != (PointInfo *) NULL)
7477 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7478 if (stroke_q != (PointInfo *) NULL)
7479 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7480 polygon_primitive=(PrimitiveInfo *)
7481 RelinquishMagickMemory(polygon_primitive);
7482 (void) ThrowMagickException(exception,GetMagickModule(),
7483 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7484 return((PrimitiveInfo *) NULL);
7487 inverse_slope.p=0.0;
7488 if (fabs(dx.p) < MagickEpsilon)
7491 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7493 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7496 if (fabs(dy.p) < MagickEpsilon)
7499 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7501 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7506 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7508 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7509 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7510 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7511 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7512 offset.x=sqrt((
double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7513 offset.y=(double) (offset.x*inverse_slope.p);
7514 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7516 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7517 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7518 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7519 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7520 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7521 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7522 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7523 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7527 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7528 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7529 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7530 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7531 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7532 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7533 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7534 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7541 stroke_q[p++]=box_q[0];
7542 stroke_p[q++]=box_p[0];
7543 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7548 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7549 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7550 dot_product=dx.q*dx.q+dy.q*dy.q;
7551 if (dot_product < 0.25)
7554 inverse_slope.q=0.0;
7555 if (fabs(dx.q) < MagickEpsilon)
7558 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7560 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7563 if (fabs(dy.q) < MagickEpsilon)
7566 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7568 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7573 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7575 offset.x=sqrt((
double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7576 offset.y=(double) (offset.x*inverse_slope.q);
7577 dot_product=dy.q*offset.x-dx.q*offset.y;
7578 if (dot_product > 0.0)
7580 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7581 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7582 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7583 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7584 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7585 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7586 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7587 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7591 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7592 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7593 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7594 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7595 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7596 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7597 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7598 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7600 if (fabs((
double) (slope.p-slope.q)) < MagickEpsilon)
7607 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7608 box_p[3].y)/(slope.p-slope.q));
7609 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7610 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7611 box_q[3].y)/(slope.p-slope.q));
7612 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7614 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7615 dot_product=dx.q*dy.p-dx.p*dy.q;
7616 if (dot_product <= 0.0)
7617 switch (draw_info->linejoin)
7621 stroke_q[q++]=box_q[1];
7622 stroke_q[q++]=box_q[2];
7623 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7624 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7625 if (dot_product <= miterlimit)
7626 stroke_p[p++]=box_p[4];
7629 stroke_p[p++]=box_p[1];
7630 stroke_p[p++]=box_p[2];
7636 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7637 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7638 if (dot_product <= miterlimit)
7640 stroke_q[q++]=box_q[4];
7641 stroke_p[p++]=box_p[4];
7645 stroke_q[q++]=box_q[1];
7646 stroke_q[q++]=box_q[2];
7647 stroke_p[p++]=box_p[1];
7648 stroke_p[p++]=box_p[2];
7654 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7655 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7656 if (dot_product <= miterlimit)
7657 stroke_p[p++]=box_p[4];
7660 stroke_p[p++]=box_p[1];
7661 stroke_p[p++]=box_p[2];
7663 center=polygon_primitive[n].point;
7664 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7665 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7666 if (theta.q < theta.p)
7667 theta.q+=2.0*MagickPI;
7668 arc_segments=(size_t) CastDoubleToLong(ceil((
double) ((theta.q-
7669 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7670 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7671 stroke_q[q].x=box_q[1].x;
7672 stroke_q[q].y=box_q[1].y;
7674 for (j=1; j < (ssize_t) arc_segments; j++)
7676 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7677 stroke_q[q].x=(double) (center.x+mid*cos(fmod((
double)
7678 (theta.p+delta_theta),DegreesToRadians(360.0))));
7679 stroke_q[q].y=(double) (center.y+mid*sin(fmod((
double)
7680 (theta.p+delta_theta),DegreesToRadians(360.0))));
7683 stroke_q[q++]=box_q[2];
7690 switch (draw_info->linejoin)
7694 stroke_p[p++]=box_p[1];
7695 stroke_p[p++]=box_p[2];
7696 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7697 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7698 if (dot_product <= miterlimit)
7699 stroke_q[q++]=box_q[4];
7702 stroke_q[q++]=box_q[1];
7703 stroke_q[q++]=box_q[2];
7709 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7710 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7711 if (dot_product <= miterlimit)
7713 stroke_q[q++]=box_q[4];
7714 stroke_p[p++]=box_p[4];
7718 stroke_q[q++]=box_q[1];
7719 stroke_q[q++]=box_q[2];
7720 stroke_p[p++]=box_p[1];
7721 stroke_p[p++]=box_p[2];
7727 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7728 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7729 if (dot_product <= miterlimit)
7730 stroke_q[q++]=box_q[4];
7733 stroke_q[q++]=box_q[1];
7734 stroke_q[q++]=box_q[2];
7736 center=polygon_primitive[n].point;
7737 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7738 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7739 if (theta.p < theta.q)
7740 theta.p+=2.0*MagickPI;
7741 arc_segments=(size_t) CastDoubleToLong(ceil((
double) ((theta.p-
7742 theta.q)/(2.0*sqrt((
double) (MagickSafeReciprocal(mid)))))));
7743 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7744 stroke_p[p++]=box_p[1];
7745 for (j=1; j < (ssize_t) arc_segments; j++)
7747 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7748 stroke_p[p].x=(double) (center.x+mid*cos(fmod((
double)
7749 (theta.p+delta_theta),DegreesToRadians(360.0))));
7750 stroke_p[p].y=(double) (center.y+mid*sin(fmod((
double)
7751 (theta.p+delta_theta),DegreesToRadians(360.0))));
7754 stroke_p[p++]=box_p[2];
7761 inverse_slope.p=inverse_slope.q;
7770 stroke_p[p++]=box_p[1];
7771 stroke_q[q++]=box_q[1];
7775 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7776 (p+q+2UL*closed_path+2UL),
sizeof(*stroke_polygon));
7777 if (stroke_polygon == (PrimitiveInfo *) NULL)
7779 (void) ThrowMagickException(exception,GetMagickModule(),
7780 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7781 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7782 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7783 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7785 return(stroke_polygon);
7787 for (i=0; i < (ssize_t) p; i++)
7789 stroke_polygon[i]=polygon_primitive[0];
7790 stroke_polygon[i].point=stroke_p[i];
7792 if (closed_path != MagickFalse)
7794 stroke_polygon[i]=polygon_primitive[0];
7795 stroke_polygon[i].point=stroke_polygon[0].point;
7798 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7800 stroke_polygon[i]=polygon_primitive[0];
7801 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7803 if (closed_path != MagickFalse)
7805 stroke_polygon[i]=polygon_primitive[0];
7806 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7809 stroke_polygon[i]=polygon_primitive[0];
7810 stroke_polygon[i].point=stroke_polygon[0].point;
7812 stroke_polygon[i].primitive=UndefinedPrimitive;
7813 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7814 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7815 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7816 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7817 return(stroke_polygon);