48#include "MagickCore/studio.h"
49#include "MagickCore/annotate.h"
50#include "MagickCore/artifact.h"
51#include "MagickCore/blob.h"
52#include "MagickCore/cache.h"
53#include "MagickCore/cache-private.h"
54#include "MagickCore/cache-view.h"
55#include "MagickCore/channel.h"
56#include "MagickCore/color.h"
57#include "MagickCore/colorspace-private.h"
58#include "MagickCore/composite.h"
59#include "MagickCore/composite-private.h"
60#include "MagickCore/constitute.h"
61#include "MagickCore/draw.h"
62#include "MagickCore/draw-private.h"
63#include "MagickCore/enhance.h"
64#include "MagickCore/exception.h"
65#include "MagickCore/exception-private.h"
66#include "MagickCore/gem.h"
67#include "MagickCore/geometry.h"
68#include "MagickCore/image-private.h"
69#include "MagickCore/list.h"
70#include "MagickCore/log.h"
71#include "MagickCore/magick.h"
72#include "MagickCore/memory-private.h"
73#include "MagickCore/monitor.h"
74#include "MagickCore/monitor-private.h"
75#include "MagickCore/option.h"
76#include "MagickCore/paint.h"
77#include "MagickCore/pixel-accessor.h"
78#include "MagickCore/property.h"
79#include "MagickCore/resample.h"
80#include "MagickCore/resample-private.h"
81#include "MagickCore/resource_.h"
82#include "MagickCore/splay-tree.h"
83#include "MagickCore/string_.h"
84#include "MagickCore/string-private.h"
85#include "MagickCore/thread-private.h"
86#include "MagickCore/token.h"
87#include "MagickCore/transform-private.h"
88#include "MagickCore/utility.h"
93#define AntialiasThreshold (1.0/3.0)
94#define BezierQuantum 200
95#define PrimitiveExtentPad 4296.0
96#define MaxBezierCoordinates 67108864
97#define MacroExpansionLimit 262144
98#define ThrowPointExpectedException(token,exception) \
100 (void) ThrowMagickException(exception,GetMagickModule(),DrawError, \
101 "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
102 status=MagickFalse; \
107static inline float MagickSafeReciprocal(
const float x) {
108 return PerceptibleReciprocal(x);
197 *DrawClippingMask(
Image *,
const DrawInfo *,
const char *,
const char *,
200static MagickBooleanType
206 const double,
const MagickBooleanType,
const MagickBooleanType),
207 TraceBezier(
MVGInfo *,
const size_t),
213 TraceSquareLinecap(
PrimitiveInfo *,
const size_t,
const double);
239MagickExport
DrawInfo *AcquireDrawInfo(
void)
244 draw_info=(
DrawInfo *) AcquireCriticalMemory(
sizeof(*draw_info));
245 GetDrawInfo((
ImageInfo *) NULL,draw_info);
285 clone_info=(
DrawInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
286 GetDrawInfo(image_info,clone_info);
289 exception=AcquireExceptionInfo();
290 if (draw_info->id != (
char *) NULL)
291 (void) CloneString(&clone_info->id,draw_info->id);
292 if (draw_info->primitive != (
char *) NULL)
293 (
void) CloneString(&clone_info->primitive,draw_info->primitive);
294 if (draw_info->geometry != (
char *) NULL)
295 (void) CloneString(&clone_info->geometry,draw_info->geometry);
296 clone_info->compliance=draw_info->compliance;
297 clone_info->viewbox=draw_info->viewbox;
298 clone_info->affine=draw_info->affine;
299 clone_info->gravity=draw_info->gravity;
300 clone_info->fill=draw_info->fill;
301 clone_info->stroke=draw_info->stroke;
302 clone_info->stroke_width=draw_info->stroke_width;
303 if (draw_info->fill_pattern != (
Image *) NULL)
304 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
306 if (draw_info->stroke_pattern != (
Image *) NULL)
307 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
308 MagickTrue,exception);
309 clone_info->stroke_antialias=draw_info->stroke_antialias;
310 clone_info->text_antialias=draw_info->text_antialias;
311 clone_info->fill_rule=draw_info->fill_rule;
312 clone_info->linecap=draw_info->linecap;
313 clone_info->linejoin=draw_info->linejoin;
314 clone_info->miterlimit=draw_info->miterlimit;
315 clone_info->dash_offset=draw_info->dash_offset;
316 clone_info->decorate=draw_info->decorate;
317 clone_info->compose=draw_info->compose;
318 if (draw_info->text != (
char *) NULL)
319 (
void) CloneString(&clone_info->text,draw_info->text);
320 if (draw_info->font != (
char *) NULL)
321 (void) CloneString(&clone_info->font,draw_info->font);
322 if (draw_info->metrics != (
char *) NULL)
323 (
void) CloneString(&clone_info->metrics,draw_info->metrics);
324 if (draw_info->family != (
char *) NULL)
325 (void) CloneString(&clone_info->family,draw_info->family);
326 clone_info->style=draw_info->style;
327 clone_info->stretch=draw_info->stretch;
328 clone_info->weight=draw_info->weight;
329 if (draw_info->encoding != (
char *) NULL)
330 (
void) CloneString(&clone_info->encoding,draw_info->encoding);
331 clone_info->pointsize=draw_info->pointsize;
332 clone_info->kerning=draw_info->kerning;
333 clone_info->interline_spacing=draw_info->interline_spacing;
334 clone_info->interword_spacing=draw_info->interword_spacing;
335 clone_info->direction=draw_info->direction;
336 clone_info->word_break=draw_info->word_break;
337 if (draw_info->density != (
char *) NULL)
338 (void) CloneString(&clone_info->density,draw_info->density);
339 clone_info->align=draw_info->align;
340 clone_info->undercolor=draw_info->undercolor;
341 clone_info->border_color=draw_info->border_color;
342 if (draw_info->server_name != (
char *) NULL)
343 (
void) CloneString(&clone_info->server_name,draw_info->server_name);
344 if (draw_info->dash_pattern != (
double *) NULL)
349 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
350 clone_info->dash_pattern=(
double *) AcquireQuantumMemory((
size_t) (x+1),
351 sizeof(*clone_info->dash_pattern));
352 if (clone_info->dash_pattern == (
double *) NULL)
353 ThrowFatalException(ResourceLimitFatalError,
354 "UnableToAllocateDashPattern");
355 (void) memset(clone_info->dash_pattern,0,(
size_t) (x+1)*
356 sizeof(*clone_info->dash_pattern));
357 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(
size_t)
358 x*
sizeof(*clone_info->dash_pattern));
360 clone_info->gradient=draw_info->gradient;
361 if (draw_info->gradient.stops != (
StopInfo *) NULL)
366 number_stops=clone_info->gradient.number_stops;
367 clone_info->gradient.stops=(
StopInfo *) AcquireQuantumMemory((
size_t)
368 number_stops,
sizeof(*clone_info->gradient.stops));
369 if (clone_info->gradient.stops == (
StopInfo *) NULL)
370 ThrowFatalException(ResourceLimitFatalError,
371 "UnableToAllocateDashPattern");
372 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
373 (
size_t) number_stops*
sizeof(*clone_info->gradient.stops));
375 clone_info->bounds=draw_info->bounds;
376 clone_info->fill_alpha=draw_info->fill_alpha;
377 clone_info->stroke_alpha=draw_info->stroke_alpha;
378 clone_info->element_reference=draw_info->element_reference;
379 clone_info->clip_path=draw_info->clip_path;
380 clone_info->clip_units=draw_info->clip_units;
381 if (draw_info->clip_mask != (
char *) NULL)
382 (
void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
383 if (draw_info->clipping_mask != (
Image *) NULL)
384 clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
385 MagickTrue,exception);
386 if (draw_info->composite_mask != (
Image *) NULL)
387 clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
388 MagickTrue,exception);
389 clone_info->render=draw_info->render;
390 clone_info->debug=draw_info->debug;
391 clone_info->macro_expansion=draw_info->macro_expansion;
392 exception=DestroyExceptionInfo(exception);
432 if (polygon_info->edges != (
EdgeInfo *) NULL)
434 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
435 if (polygon_info->edges[i].points != (
PointInfo *) NULL)
436 polygon_info->edges[i].points=(
PointInfo *)
437 RelinquishMagickMemory(polygon_info->edges[i].points);
438 polygon_info->edges=(
EdgeInfo *) RelinquishMagickMemory(
439 polygon_info->edges);
441 return((
PolygonInfo *) RelinquishMagickMemory(polygon_info));
443#if defined(__cplusplus) || defined(c_plusplus)
447static int DrawCompareEdges(
const void *p_edge,
const void *q_edge)
449#define DrawCompareEdge(p,q) \
451 if (((p)-(q)) < 0.0) \
453 if (((p)-(q)) > 0.0) \
464 p=((
const EdgeInfo *) p_edge)->points;
465 q=((
const EdgeInfo *) q_edge)->points;
466 DrawCompareEdge(p[0].y,q[0].y);
467 DrawCompareEdge(p[0].x,q[0].x);
468 DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
470 DrawCompareEdge(p[1].y,q[1].y);
471 DrawCompareEdge(p[1].x,q[1].x);
475#if defined(__cplusplus) || defined(c_plusplus)
479static void LogPolygonInfo(
const PolygonInfo *polygon_info)
488 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin active-edge");
489 p=polygon_info->edges;
490 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
492 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" edge %.20g:",
494 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" direction: %s",
495 p->direction != MagickFalse ?
"down" :
"up");
496 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" ghostline: %s",
497 p->ghostline != MagickFalse ?
"transparent" :
"opaque");
498 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
499 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
500 p->bounds.x2,p->bounds.y2);
501 for (j=0; j < (ssize_t) p->number_points; j++)
502 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %g,%g",
503 p->points[j].x,p->points[j].y);
506 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end active-edge");
509static void ReversePoints(
PointInfo *points,
const size_t number_points)
517 for (i=0; i < (number_points >> 1); i++)
520 points[i]=points[number_points-(i+1)];
521 points[number_points-(i+1)]=point;
557 polygon_info=(
PolygonInfo *) AcquireMagickMemory(
sizeof(*polygon_info));
560 (void) ThrowMagickException(exception,GetMagickModule(),
561 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
565 polygon_info->edges=(
EdgeInfo *) AcquireQuantumMemory(number_edges,
566 sizeof(*polygon_info->edges));
567 if (polygon_info->edges == (
EdgeInfo *) NULL)
569 (void) ThrowMagickException(exception,GetMagickModule(),
570 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
571 return(DestroyPolygonInfo(polygon_info));
573 (void) memset(polygon_info->edges,0,number_edges*
574 sizeof(*polygon_info->edges));
577 ghostline=MagickFalse;
581 (void) memset(&point,0,
sizeof(point));
582 (void) memset(&bounds,0,
sizeof(bounds));
583 polygon_info->edges[edge].number_points=(size_t) n;
584 polygon_info->edges[edge].scanline=0.0;
585 polygon_info->edges[edge].highwater=0;
586 polygon_info->edges[edge].ghostline=ghostline;
587 polygon_info->edges[edge].direction=(ssize_t) direction;
588 polygon_info->edges[edge].points=points;
589 polygon_info->edges[edge].bounds=bounds;
590 polygon_info->number_edges=0;
591 for (i=0; path_info[i].code != EndCode; i++)
593 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
594 (path_info[i].code == GhostlineCode))
599 if ((points != (
PointInfo *) NULL) && (n >= 2))
601 if (edge == number_edges)
604 polygon_info->edges=(
EdgeInfo *) ResizeQuantumMemory(
605 polygon_info->edges,(
size_t) number_edges,
606 sizeof(*polygon_info->edges));
607 if (polygon_info->edges == (
EdgeInfo *) NULL)
609 (void) ThrowMagickException(exception,GetMagickModule(),
610 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
611 points=(
PointInfo *) RelinquishMagickMemory(points);
612 return(DestroyPolygonInfo(polygon_info));
615 polygon_info->edges[edge].number_points=(size_t) n;
616 polygon_info->edges[edge].scanline=(-1.0);
617 polygon_info->edges[edge].highwater=0;
618 polygon_info->edges[edge].ghostline=ghostline;
619 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
621 ReversePoints(points,(
size_t) n);
622 polygon_info->edges[edge].points=points;
623 polygon_info->edges[edge].bounds=bounds;
624 polygon_info->edges[edge].bounds.y1=points[0].y;
625 polygon_info->edges[edge].bounds.y2=points[n-1].y;
627 ghostline=MagickFalse;
629 polygon_info->number_edges=edge;
634 points=(
PointInfo *) AcquireQuantumMemory((
size_t) number_points,
638 (void) ThrowMagickException(exception,GetMagickModule(),
639 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
640 return(DestroyPolygonInfo(polygon_info));
643 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
644 point=path_info[i].point;
655 next_direction=((path_info[i].point.y > point.y) ||
656 ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
657 (path_info[i].point.x > point.x))) ? 1 : -1;
658 if ((points != (
PointInfo *) NULL) && (direction != 0) &&
659 (direction != next_direction))
665 if (edge == number_edges)
668 polygon_info->edges=(
EdgeInfo *) ResizeQuantumMemory(
669 polygon_info->edges,(
size_t) number_edges,
670 sizeof(*polygon_info->edges));
671 if (polygon_info->edges == (
EdgeInfo *) NULL)
673 (void) ThrowMagickException(exception,GetMagickModule(),
674 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
675 points=(
PointInfo *) RelinquishMagickMemory(points);
676 return(DestroyPolygonInfo(polygon_info));
679 polygon_info->edges[edge].number_points=(size_t) n;
680 polygon_info->edges[edge].scanline=(-1.0);
681 polygon_info->edges[edge].highwater=0;
682 polygon_info->edges[edge].ghostline=ghostline;
683 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
685 ReversePoints(points,(
size_t) n);
686 polygon_info->edges[edge].points=points;
687 polygon_info->edges[edge].bounds=bounds;
688 polygon_info->edges[edge].bounds.y1=points[0].y;
689 polygon_info->edges[edge].bounds.y2=points[n-1].y;
690 polygon_info->number_edges=edge+1;
693 points=(
PointInfo *) AcquireQuantumMemory((
size_t) number_points,
697 (void) ThrowMagickException(exception,GetMagickModule(),
698 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
699 return(DestroyPolygonInfo(polygon_info));
702 ghostline=MagickFalse;
708 direction=next_direction;
711 if (n == (ssize_t) number_points)
714 points=(
PointInfo *) ResizeQuantumMemory(points,(
size_t) number_points,
718 (void) ThrowMagickException(exception,GetMagickModule(),
719 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
720 return(DestroyPolygonInfo(polygon_info));
723 point=path_info[i].point;
725 if (point.x < bounds.x1)
727 if (point.x > bounds.x2)
734 points=(
PointInfo *) RelinquishMagickMemory(points);
737 if (edge == number_edges)
740 polygon_info->edges=(
EdgeInfo *) ResizeQuantumMemory(
741 polygon_info->edges,(
size_t) number_edges,
742 sizeof(*polygon_info->edges));
743 if (polygon_info->edges == (
EdgeInfo *) NULL)
745 (void) ThrowMagickException(exception,GetMagickModule(),
746 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
747 return(DestroyPolygonInfo(polygon_info));
750 polygon_info->edges[edge].number_points=(size_t) n;
751 polygon_info->edges[edge].scanline=(-1.0);
752 polygon_info->edges[edge].highwater=0;
753 polygon_info->edges[edge].ghostline=ghostline;
754 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
756 ReversePoints(points,(
size_t) n);
757 polygon_info->edges[edge].points=points;
758 polygon_info->edges[edge].bounds=bounds;
759 polygon_info->edges[edge].bounds.y1=points[0].y;
760 polygon_info->edges[edge].bounds.y2=points[n-1].y;
762 ghostline=MagickFalse;
764 polygon_info->number_edges=edge;
767 polygon_info->number_edges=edge;
768 polygon_info->edges=(
EdgeInfo *) ResizeQuantumMemory(polygon_info->edges,
769 polygon_info->number_edges,
sizeof(*polygon_info->edges));
770 if (polygon_info->edges == (
EdgeInfo *) NULL)
772 (void) ThrowMagickException(exception,GetMagickModule(),
773 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
774 return(DestroyPolygonInfo(polygon_info));
776 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
781 edge_info=polygon_info->edges+i;
782 edge_info->points=(
PointInfo *) ResizeQuantumMemory(edge_info->points,
783 edge_info->number_points,
sizeof(*edge_info->points));
784 if (edge_info->points == (
PointInfo *) NULL)
786 (void) ThrowMagickException(exception,GetMagickModule(),
787 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
788 return(DestroyPolygonInfo(polygon_info));
791 qsort(polygon_info->edges,(
size_t) polygon_info->number_edges,
792 sizeof(*polygon_info->edges),DrawCompareEdges);
793 if ((GetLogEventMask() & DrawEvent) != 0)
794 LogPolygonInfo(polygon_info);
795 return(polygon_info);
828static void LogPathInfo(
const PathInfo *path_info)
833 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin vector-path");
834 for (p=path_info; p->code != EndCode; p++)
835 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
836 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
837 "moveto ghostline" : p->code == OpenCode ?
"moveto open" :
838 p->code == MoveToCode ?
"moveto" : p->code == LineToCode ?
"lineto" :
840 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end vector-path");
870 switch (primitive_info->primitive)
881 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
882 path_info=(
PathInfo *) AcquireQuantumMemory((
size_t) (3UL*i+1UL),
886 (void) ThrowMagickException(exception,GetMagickModule(),
887 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
891 closed_subpath=MagickFalse;
898 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
901 if (coordinates <= 0)
906 coordinates=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),
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->fill_pattern != (
Image *) NULL)
990 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
991 if (draw_info->stroke_pattern != (
Image *) NULL)
992 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
993 if (draw_info->font != (
char *) NULL)
994 draw_info->font=DestroyString(draw_info->font);
995 if (draw_info->metrics != (
char *) NULL)
996 draw_info->metrics=DestroyString(draw_info->metrics);
997 if (draw_info->family != (
char *) NULL)
998 draw_info->family=DestroyString(draw_info->family);
999 if (draw_info->encoding != (
char *) NULL)
1000 draw_info->encoding=DestroyString(draw_info->encoding);
1001 if (draw_info->density != (
char *) NULL)
1002 draw_info->density=DestroyString(draw_info->density);
1003 if (draw_info->server_name != (
char *) NULL)
1004 draw_info->server_name=(
char *)
1005 RelinquishMagickMemory(draw_info->server_name);
1006 if (draw_info->dash_pattern != (
double *) NULL)
1007 draw_info->dash_pattern=(
double *) RelinquishMagickMemory(
1008 draw_info->dash_pattern);
1009 if (draw_info->gradient.stops != (
StopInfo *) NULL)
1010 draw_info->gradient.stops=(
StopInfo *) RelinquishMagickMemory(
1011 draw_info->gradient.stops);
1012 if (draw_info->clip_mask != (
char *) NULL)
1013 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
1014 if (draw_info->clipping_mask != (
Image *) NULL)
1015 draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
1016 if (draw_info->composite_mask != (
Image *) NULL)
1017 draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
1018 if (draw_info->image_info != (
ImageInfo *) NULL)
1019 draw_info->image_info=DestroyImageInfo(draw_info->image_info);
1020 draw_info->signature=(~MagickCoreSignature);
1021 draw_info=(
DrawInfo *) RelinquishMagickMemory(draw_info);
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);
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,
1198 assert(image != (
Image *) NULL);
1199 assert(image->signature == MagickCoreSignature);
1200 if (IsEventLogging() != MagickFalse)
1201 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1202 assert(source != (
const Image *) NULL);
1203 assert(source->signature == MagickCoreSignature);
1207 extent[1].x=(double) source->columns;
1209 extent[2].x=(double) source->columns;
1210 extent[2].y=(double) source->rows;
1212 extent[3].y=(double) source->rows;
1213 for (i=0; i < 4; i++)
1219 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1220 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1224 for (i=1; i < 4; i++)
1226 if (min.x > extent[i].x)
1228 if (min.y > extent[i].y)
1230 if (max.x < extent[i].x)
1232 if (max.y < extent[i].y)
1238 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1239 return(MagickFalse);
1245 inverse_affine=InverseAffineMatrix(affine);
1248 if (edge.y2 > ((
double) image->rows-1.0))
1249 edge.y2=(double) image->rows-1.0;
1250 GetPixelInfo(image,&zero);
1251 start=CastDoubleToSsizeT(ceil(edge.y1-0.5));
1252 stop=CastDoubleToSsizeT(floor(edge.y2+0.5));
1253 source_view=AcquireVirtualCacheView(source,exception);
1254 image_view=AcquireAuthenticCacheView(image,exception);
1255#if defined(MAGICKCORE_OPENMP_SUPPORT)
1256 #pragma omp parallel for schedule(static) shared(status) \
1257 magick_number_threads(source,image,(size_t) (stop-start),2)
1259 for (y=start; y <= stop; y++)
1277 if (status == MagickFalse)
1279 inverse_edge=AffineEdge(source,&inverse_affine,(
double) y,&edge);
1280 if (inverse_edge.x2 < inverse_edge.x1)
1282 if (inverse_edge.x1 < 0.0)
1283 inverse_edge.x1=0.0;
1284 if (inverse_edge.x2 > ((
double) image->columns-1.0))
1285 inverse_edge.x2=(double) image->columns-1.0;
1286 q=GetCacheViewAuthenticPixels(image_view,CastDoubleToSsizeT(
1287 ceil(inverse_edge.x1-0.5)),y,(
size_t) CastDoubleToSsizeT(floor(
1288 inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),1,exception);
1289 if (q == (Quantum *) NULL)
1293 for (x=CastDoubleToSsizeT(ceil(inverse_edge.x1-0.5));
1294 x <= CastDoubleToSsizeT(floor(inverse_edge.x2+0.5)); x++)
1296 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1298 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1300 status=InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1301 point.x,point.y,&pixel,exception);
1302 if (status == MagickFalse)
1304 GetPixelInfoPixel(image,q,&composite);
1305 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1307 SetPixelViaPixelInfo(image,&composite,q);
1308 q+=(ptrdiff_t) GetPixelChannels(image);
1310 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1313 source_view=DestroyCacheView(source_view);
1314 image_view=DestroyCacheView(image_view);
1350static MagickBooleanType DrawBoundingRectangles(
Image *image,
1380 (void) memset(primitive_info,0,
sizeof(primitive_info));
1381 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1382 status=QueryColorCompliance(
"#000F",AllCompliance,&clone_info->fill,
1384 if (status == MagickFalse)
1386 clone_info=DestroyDrawInfo(clone_info);
1387 return(MagickFalse);
1391 if (clone_info->density != (
char *) NULL)
1399 flags=ParseGeometry(clone_info->density,&geometry_info);
1400 if ((flags & RhoValue) != 0)
1401 resolution.x=geometry_info.rho;
1402 resolution.y=resolution.x;
1403 if ((flags & SigmaValue) != 0)
1404 resolution.y=geometry_info.sigma;
1406 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1407 clone_info->stroke_width/2.0;
1414 bounds=polygon_info->edges[0].bounds;
1415 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1417 if (polygon_info->edges[i].bounds.x1 < (
double) bounds.x1)
1418 bounds.x1=polygon_info->edges[i].bounds.x1;
1419 if (polygon_info->edges[i].bounds.y1 < (
double) bounds.y1)
1420 bounds.y1=polygon_info->edges[i].bounds.y1;
1421 if (polygon_info->edges[i].bounds.x2 > (
double) bounds.x2)
1422 bounds.x2=polygon_info->edges[i].bounds.x2;
1423 if (polygon_info->edges[i].bounds.y2 > (
double) bounds.y2)
1424 bounds.y2=polygon_info->edges[i].bounds.y2;
1427 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1428 image->columns ? (
double) image->columns-1 : bounds.x1;
1430 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1431 image->rows ? (
double) image->rows-1 : bounds.y1;
1433 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1434 image->columns ? (
double) image->columns-1 : bounds.x2;
1436 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1437 image->rows ? (
double) image->rows-1 : bounds.y2;
1438 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1440 if (polygon_info->edges[i].direction != 0)
1441 status=QueryColorCompliance(
"#f00",AllCompliance,&clone_info->stroke,
1444 status=QueryColorCompliance(
"#0f0",AllCompliance,&clone_info->stroke,
1446 if (status == MagickFalse)
1448 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1449 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1450 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1451 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1452 primitive_info[0].primitive=RectanglePrimitive;
1453 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1454 primitive_info[0].method=ReplaceMethod;
1455 coordinates=(ssize_t) primitive_info[0].coordinates;
1456 primitive_info[coordinates].primitive=UndefinedPrimitive;
1457 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1458 if (status == MagickFalse)
1461 if (i < (ssize_t) polygon_info->number_edges)
1463 clone_info=DestroyDrawInfo(clone_info);
1464 return(status == 0 ? MagickFalse : MagickTrue);
1467 status=QueryColorCompliance(
"#00f",AllCompliance,&clone_info->stroke,
1469 if (status == MagickFalse)
1471 clone_info=DestroyDrawInfo(clone_info);
1472 return(MagickFalse);
1474 start.x=(double) (bounds.x1-mid);
1475 start.y=(double) (bounds.y1-mid);
1476 end.x=(double) (bounds.x2+mid);
1477 end.y=(double) (bounds.y2+mid);
1478 primitive_info[0].primitive=RectanglePrimitive;
1479 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1480 primitive_info[0].method=ReplaceMethod;
1481 coordinates=(ssize_t) primitive_info[0].coordinates;
1482 primitive_info[coordinates].primitive=UndefinedPrimitive;
1483 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1484 clone_info=DestroyDrawInfo(clone_info);
1485 return(status == 0 ? MagickFalse : MagickTrue);
1517MagickExport MagickBooleanType DrawClipPath(
Image *image,
1529 clip_path=GetImageArtifact(image,
id);
1530 if (clip_path == (
const char *) NULL)
1531 return(MagickFalse);
1532 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1534 if (clipping_mask == (
Image *) NULL)
1535 return(MagickFalse);
1536 status=SetImageMask(image,WritePixelMask,clipping_mask,exception);
1537 clipping_mask=DestroyImage(clipping_mask);
1574 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,exception);
1595 status=SetImageExtent(clip_mask,image->columns,image->rows,exception);
1596 if (status == MagickFalse)
1597 return(DestroyImage(clip_mask));
1598 status=SetImageMask(clip_mask,WritePixelMask,(
Image *) NULL,exception);
1599 status=QueryColorCompliance(
"#0000",AllCompliance,
1600 &clip_mask->background_color,exception);
1601 clip_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1602 clip_mask->background_color.alpha_trait=BlendPixelTrait;
1603 status=SetImageBackgroundColor(clip_mask,exception);
1604 if (draw_info->debug != MagickFalse)
1605 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin clip-path %s",
1607 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1608 (void) CloneString(&clone_info->primitive,clip_path);
1609 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1611 if (clone_info->clip_mask != (
char *) NULL)
1612 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1613 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1615 clone_info->stroke_width=0.0;
1616 clone_info->alpha=OpaqueAlpha;
1617 clone_info->clip_path=MagickTrue;
1618 status=RenderMVGContent(clip_mask,clone_info,0,exception);
1619 clone_info=DestroyDrawInfo(clone_info);
1620 separate_mask=SeparateImage(clip_mask,AlphaChannel,exception);
1621 if (separate_mask == (
Image *) NULL)
1625 clip_mask=DestroyImage(clip_mask);
1626 clip_mask=separate_mask;
1627 status&=(MagickStatusType) NegateImage(clip_mask,MagickFalse,exception);
1629 if (status == MagickFalse)
1630 clip_mask=DestroyImage(clip_mask);
1631 if (draw_info->debug != MagickFalse)
1632 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end clip-path");
1668 const char *
id,
const char *mask_path,
ExceptionInfo *exception)
1683 assert(image != (
Image *) NULL);
1684 assert(image->signature == MagickCoreSignature);
1685 assert(draw_info != (
const DrawInfo *) NULL);
1686 if (IsEventLogging() != MagickFalse)
1687 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1688 composite_mask=AcquireImage((
const ImageInfo *) NULL,exception);
1689 status=SetImageExtent(composite_mask,image->columns,image->rows,exception);
1690 if (status == MagickFalse)
1691 return(DestroyImage(composite_mask));
1692 status=SetImageMask(composite_mask,CompositePixelMask,(
Image *) NULL,
1694 status=QueryColorCompliance(
"#0000",AllCompliance,
1695 &composite_mask->background_color,exception);
1696 composite_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1697 composite_mask->background_color.alpha_trait=BlendPixelTrait;
1698 (void) SetImageBackgroundColor(composite_mask,exception);
1699 if (draw_info->debug != MagickFalse)
1700 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin mask-path %s",
1702 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1703 (void) CloneString(&clone_info->primitive,mask_path);
1704 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1706 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1708 clone_info->stroke_width=0.0;
1709 clone_info->alpha=OpaqueAlpha;
1710 status=RenderMVGContent(composite_mask,clone_info,0,exception);
1711 clone_info=DestroyDrawInfo(clone_info);
1712 separate_mask=SeparateImage(composite_mask,AlphaChannel,exception);
1713 if (separate_mask != (
Image *) NULL)
1715 composite_mask=DestroyImage(composite_mask);
1716 composite_mask=separate_mask;
1717 status=NegateImage(composite_mask,MagickFalse,exception);
1718 if (status == MagickFalse)
1719 composite_mask=DestroyImage(composite_mask);
1721 if (status == MagickFalse)
1722 composite_mask=DestroyImage(composite_mask);
1723 if (draw_info->debug != MagickFalse)
1724 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end mask-path");
1725 return(composite_mask);
1759static MagickBooleanType DrawDashPolygon(
const DrawInfo *draw_info,
1790 assert(draw_info != (
const DrawInfo *) NULL);
1791 if (draw_info->debug != MagickFalse)
1792 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-dash");
1793 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1794 number_vertices=(size_t) i;
1795 dash_polygon=(
PrimitiveInfo *) AcquireQuantumMemory((
size_t)
1796 (2UL*number_vertices+32UL),
sizeof(*dash_polygon));
1799 (void) ThrowMagickException(exception,GetMagickModule(),
1800 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1801 return(MagickFalse);
1803 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1804 sizeof(*dash_polygon));
1805 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1806 clone_info->miterlimit=0;
1807 dash_polygon[0]=primitive_info[0];
1808 dash_polygon[0].closed_subpath=MagickFalse;
1809 scale=ExpandAffine(&draw_info->affine);
1810 length=scale*draw_info->dash_pattern[0];
1811 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1812 scale*draw_info->dash_offset : 0.0;
1814 for (n=0; offset > 0.0; j=0)
1816 if (draw_info->dash_pattern[n] <= 0.0)
1818 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1819 if (offset > length)
1823 length=scale*draw_info->dash_pattern[n];
1826 if (offset < length)
1838 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1840 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1841 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1842 maximum_length=hypot(dx,dy);
1843 if (maximum_length > (
double) (MaxBezierCoordinates >> 2))
1845 if (fabs(length) < MagickEpsilon)
1847 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1849 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1851 length=scale*draw_info->dash_pattern[n];
1853 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1855 total_length+=length;
1856 if ((n & 0x01) != 0)
1858 dash_polygon[0]=primitive_info[0];
1859 dash_polygon[0].closed_subpath=MagickFalse;
1860 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1861 total_length*MagickSafeReciprocal(maximum_length));
1862 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1863 total_length*MagickSafeReciprocal(maximum_length));
1868 if ((j+1) > (ssize_t) number_vertices)
1870 dash_polygon[j]=primitive_info[i-1];
1871 dash_polygon[j].closed_subpath=MagickFalse;
1872 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1873 total_length*MagickSafeReciprocal(maximum_length));
1874 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1875 total_length*MagickSafeReciprocal(maximum_length));
1876 dash_polygon[j].coordinates=1;
1878 dash_polygon[0].coordinates=(size_t) j;
1879 dash_polygon[j].primitive=UndefinedPrimitive;
1880 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1881 dash_polygon,exception);
1882 if (status == MagickFalse)
1885 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1887 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1889 length=scale*draw_info->dash_pattern[n];
1891 length-=(maximum_length-total_length);
1892 if ((n & 0x01) != 0)
1894 dash_polygon[j]=primitive_info[i];
1895 dash_polygon[j].coordinates=1;
1898 if ((status != MagickFalse) && (total_length < maximum_length) &&
1899 ((n & 0x01) == 0) && (j > 1))
1901 dash_polygon[j]=primitive_info[i-1];
1902 dash_polygon[j].closed_subpath=MagickFalse;
1903 dash_polygon[j].point.x+=MagickEpsilon;
1904 dash_polygon[j].point.y+=MagickEpsilon;
1905 dash_polygon[j].coordinates=1;
1907 dash_polygon[0].coordinates=(size_t) j;
1908 dash_polygon[j].primitive=UndefinedPrimitive;
1909 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1910 dash_polygon,exception);
1912 dash_polygon=(
PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1913 clone_info=DestroyDrawInfo(clone_info);
1914 if (draw_info->debug != MagickFalse)
1915 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-dash");
1916 return(status != 0 ? MagickTrue : MagickFalse);
1947static inline double GetStopColorOffset(
const GradientInfo *gradient,
1948 const ssize_t x,
const ssize_t y)
1950 switch (gradient->type)
1952 case UndefinedGradient:
1953 case LinearGradient:
1968 gradient_vector=(&gradient->gradient_vector);
1969 p.x=gradient_vector->x2-gradient_vector->x1;
1970 p.y=gradient_vector->y2-gradient_vector->y1;
1971 q.x=(double) x-gradient_vector->x1;
1972 q.y=(double) y-gradient_vector->y1;
1973 length=sqrt(q.x*q.x+q.y*q.y);
1974 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1975 gamma=MagickSafeReciprocal(gamma);
1976 scale=p.x*q.x+p.y*q.y;
1977 offset=gamma*scale*length;
1980 case RadialGradient:
1985 if (gradient->spread == RepeatSpread)
1987 v.x=(double) x-gradient->center.x;
1988 v.y=(double) y-gradient->center.y;
1989 return(sqrt(v.x*v.x+v.y*v.y));
1991 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1992 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1993 gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1994 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1995 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1996 gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1997 return(sqrt(v.x*v.x+v.y*v.y));
2003static int StopInfoCompare(
const void *x,
const void *y)
2011 if (stop_1->offset > stop_2->offset)
2013 if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
2018MagickExport MagickBooleanType DrawGradientImage(
Image *image,
2054 assert(image != (
Image *) NULL);
2055 assert(image->signature == MagickCoreSignature);
2056 assert(draw_info != (
const DrawInfo *) NULL);
2057 if (IsEventLogging() != MagickFalse)
2058 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2059 gradient=(&draw_info->gradient);
2060 qsort(gradient->stops,gradient->number_stops,
sizeof(
StopInfo),
2062 gradient_vector=(&gradient->gradient_vector);
2063 point.x=gradient_vector->x2-gradient_vector->x1;
2064 point.y=gradient_vector->y2-gradient_vector->y1;
2065 length=sqrt(point.x*point.x+point.y*point.y);
2066 bounding_box=gradient->bounding_box;
2068 GetPixelInfo(image,&zero);
2069 image_view=AcquireAuthenticCacheView(image,exception);
2070 height=(size_t) (bounding_box.y+(ssize_t) bounding_box.height);
2071#if defined(MAGICKCORE_OPENMP_SUPPORT)
2072 #pragma omp parallel for schedule(static) shared(status) \
2073 magick_number_threads(image,image,height,1)
2075 for (y=bounding_box.y; y < (ssize_t) height; y++)
2096 if (status == MagickFalse)
2098 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(
size_t)
2099 bounding_box.width,1,exception);
2100 if (q == (Quantum *) NULL)
2107 offset=GetStopColorOffset(gradient,0,y);
2108 if (gradient->type != RadialGradient)
2109 offset*=MagickSafeReciprocal(length);
2110 width=(size_t) (bounding_box.x+(ssize_t) bounding_box.width);
2111 for (x=bounding_box.x; x < (ssize_t) width; x++)
2113 GetPixelInfoPixel(image,q,&pixel);
2114 switch (gradient->spread)
2116 case UndefinedSpread:
2119 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2120 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2122 offset=GetStopColorOffset(gradient,x,y);
2123 if (gradient->type != RadialGradient)
2124 offset*=MagickSafeReciprocal(length);
2126 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2127 if (offset < gradient->stops[i].offset)
2129 if ((offset < 0.0) || (i == 0))
2130 composite=gradient->stops[0].color;
2132 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2133 composite=gradient->stops[gradient->number_stops-1].color;
2138 alpha=(offset-gradient->stops[i].offset)/
2139 (gradient->stops[j].offset-gradient->stops[i].offset);
2140 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2141 &gradient->stops[j].color,alpha,&composite);
2147 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2148 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2150 offset=GetStopColorOffset(gradient,x,y);
2151 if (gradient->type != RadialGradient)
2152 offset*=MagickSafeReciprocal(length);
2156 if ((ssize_t) fmod(offset,2.0) == 0)
2157 offset=fmod(offset,1.0);
2159 offset=1.0-fmod(offset,1.0);
2160 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2161 if (offset < gradient->stops[i].offset)
2164 composite=gradient->stops[0].color;
2166 if (i == (ssize_t) gradient->number_stops)
2167 composite=gradient->stops[gradient->number_stops-1].color;
2172 alpha=(offset-gradient->stops[i].offset)/
2173 (gradient->stops[j].offset-gradient->stops[i].offset);
2174 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2175 &gradient->stops[j].color,alpha,&composite);
2187 antialias=MagickFalse;
2189 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2190 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2192 offset=GetStopColorOffset(gradient,x,y);
2193 if (gradient->type == LinearGradient)
2195 repeat=fmod(offset,length);
2197 repeat=length-fmod(-repeat,length);
2199 repeat=fmod(offset,length);
2200 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2201 MagickTrue : MagickFalse;
2202 offset=MagickSafeReciprocal(length)*repeat;
2206 repeat=fmod(offset,gradient->radius);
2208 repeat=gradient->radius-fmod(-repeat,gradient->radius);
2210 repeat=fmod(offset,gradient->radius);
2211 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2213 offset=repeat*MagickSafeReciprocal(gradient->radius);
2216 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2217 if (offset < gradient->stops[i].offset)
2220 composite=gradient->stops[0].color;
2222 if (i == (ssize_t) gradient->number_stops)
2223 composite=gradient->stops[gradient->number_stops-1].color;
2228 alpha=(offset-gradient->stops[i].offset)/
2229 (gradient->stops[j].offset-gradient->stops[i].offset);
2230 if (antialias != MagickFalse)
2232 if (gradient->type == LinearGradient)
2233 alpha=length-repeat;
2235 alpha=gradient->radius-repeat;
2237 j=(ssize_t) gradient->number_stops-1L;
2239 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2240 &gradient->stops[j].color,alpha,&composite);
2245 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
2247 SetPixelViaPixelInfo(image,&pixel,q);
2248 q+=(ptrdiff_t) GetPixelChannels(image);
2250 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2253 image_view=DestroyCacheView(image_view);
2289static inline MagickBooleanType CheckPrimitiveExtent(
MVGInfo *mvg_info,
2304 if ((mvg_info == (
MVGInfo *) NULL) ||
2307 (mvg_info->extent == (
size_t *) NULL))
2308 return(MagickFalse);
2309 proposed_extent=mvg_info->offset+pad+PrimitiveExtentPad+1.0;
2310 if ((proposed_extent <= 0.0) || (proposed_extent > (
double) MAGICK_SIZE_MAX))
2311 return(MagickFalse);
2312 extent=CastDoubleToSizeT(ceil(proposed_extent));
2313 if (extent <= *mvg_info->extent)
2316 return(MagickFalse);
2324 extent=(size_t) PrimitiveExtentPad;
2325 primitive_info=(
PrimitiveInfo *) AcquireCriticalMemory(extent*
2326 sizeof(*primitive_info));
2327 (void) memset(primitive_info,0,extent*
sizeof(*primitive_info));
2328 *mvg_info->primitive_info=primitive_info;
2329 *mvg_info->extent=extent;
2331 ThrowMagickException(mvg_info->exception,GetMagickModule(),
2332 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2333 return(MagickFalse);
2335 primitive_info[extent].primitive=UndefinedPrimitive;
2336 primitive_info[extent].text=(
char *) NULL;
2340 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2342 primitive_info[i].primitive=UndefinedPrimitive;
2343 primitive_info[i].text=(
char *) NULL;
2345 *mvg_info->primitive_info=primitive_info;
2346 *mvg_info->extent=extent;
2350static inline double GetDrawValue(
const char *magick_restrict
string,
2351 char **magick_restrict sentinel)
2354 **magick_restrict q;
2360 value=InterpretLocaleValue(
string,q);
2365static int MVGMacroCompare(
const void *target,
const void *source)
2371 p=(
const char *) target;
2372 q=(
const char *) source;
2373 return(strcmp(p,q));
2395 if (primitive == (
const char *) NULL)
2397 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2398 RelinquishMagickMemory);
2399 macro=AcquireString(primitive);
2400 token=AcquireString(primitive);
2401 extent=strlen(token)+MagickPathExtent;
2402 for (q=primitive; *q !=
'\0'; )
2404 if (GetNextToken(q,&q,extent,token) < 1)
2408 if (LocaleCompare(
"push",token) == 0)
2414 (void) GetNextToken(q,&q,extent,token);
2418 name[MagickPathExtent];
2429 (void) GetNextToken(q,&q,extent,token);
2432 (void) CopyMagickString(name,token,MagickPathExtent);
2434 for (p=q; *p !=
'\0'; )
2436 if (GetNextToken(p,&p,extent,token) < 1)
2440 if (LocaleCompare(token,
"pop") == 0)
2442 end=p-strlen(token)-1;
2445 if (LocaleCompare(token,
"push") == 0)
2447 if (n++ > MagickMaxRecursionDepth)
2449 (void) ThrowMagickException(exception,GetMagickModule(),
2450 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",token);
2454 if ((n == 0) && (end >= start))
2457 length=(size_t) (end-start);
2462 (void) GetNextToken(p,&p,extent,token);
2465 (void) CopyMagickString(macro,start,length);
2466 (void) AddValueToSplayTree(macros,ConstantString(name),
2467 ConstantString(macro));
2475 token=DestroyString(token);
2476 macro=DestroyString(macro);
2480static inline MagickBooleanType IsValidListChar(
int c)
2482 if ((c >=
'0') && (c <=
'9'))
2496 return(MagickFalse);
2501static inline MagickBooleanType IsValidPoint(
const char *point)
2509 value=GetDrawValue(point,&p);
2510 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2514static inline MagickBooleanType TracePoint(
PrimitiveInfo *primitive_info,
2517 primitive_info->point=point;
2518 primitive_info->coordinates=1;
2519 primitive_info->closed_subpath=MagickFalse;
2520 primitive_info->text=(
char *) NULL;
2524static MagickBooleanType RenderMVGContent(
Image *image,
2527#define RenderImageTag "Render/Image"
2534 keyword[MagickPathExtent],
2535 geometry[MagickPathExtent],
2537 pattern[MagickPathExtent],
2601 assert(image != (
Image *) NULL);
2602 assert(image->signature == MagickCoreSignature);
2603 assert(draw_info != (
DrawInfo *) NULL);
2604 assert(draw_info->signature == MagickCoreSignature);
2605 if (IsEventLogging() != MagickFalse)
2606 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2607 if (depth > MagickMaxRecursionDepth)
2608 ThrowBinaryException(DrawError,
"VectorGraphicsNestedTooDeeply",
2610 if ((draw_info->primitive == (
char *) NULL) ||
2611 (*draw_info->primitive ==
'\0'))
2612 return(MagickFalse);
2613 if (draw_info->debug != MagickFalse)
2614 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"begin draw-image");
2615 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2616 return(MagickFalse);
2617 if ((image->alpha_trait & BlendPixelTrait) == 0)
2619 status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2620 if (status == MagickFalse)
2621 return(MagickFalse);
2623 if ((*draw_info->primitive ==
'@') && (strlen(draw_info->primitive) > 1) &&
2624 (*(draw_info->primitive+1) !=
'-') && (depth == 0))
2625 primitive=FileToString(draw_info->primitive,~0UL,exception);
2627 primitive=AcquireString(draw_info->primitive);
2628 if (primitive == (
char *) NULL)
2629 return(MagickFalse);
2630 primitive_extent=(double) strlen(primitive);
2631 (void) SetImageArtifact(image,
"mvg:vector-graphics",primitive);
2638 graphic_context=(
DrawInfo **) AcquireMagickMemory(
sizeof(*graphic_context));
2639 if (graphic_context == (
DrawInfo **) NULL)
2641 primitive=DestroyString(primitive);
2642 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2645 number_points=(size_t) PrimitiveExtentPad;
2646 primitive_info=(
PrimitiveInfo *) AcquireQuantumMemory((
size_t)
2647 (number_points+1),
sizeof(*primitive_info));
2650 primitive=DestroyString(primitive);
2651 for ( ; n >= 0; n--)
2652 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2653 graphic_context=(
DrawInfo **) RelinquishMagickMemory(graphic_context);
2654 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2657 (void) memset(primitive_info,0,(
size_t) (number_points+1)*
2658 sizeof(*primitive_info));
2659 (void) memset(&mvg_info,0,
sizeof(mvg_info));
2660 mvg_info.primitive_info=(&primitive_info);
2661 mvg_info.extent=(&number_points);
2662 mvg_info.exception=exception;
2663 graphic_context[n]=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
2664 graphic_context[n]->viewbox=image->page;
2665 if ((image->page.width == 0) || (image->page.height == 0))
2667 graphic_context[n]->viewbox.width=image->columns;
2668 graphic_context[n]->viewbox.height=image->rows;
2670 token=AcquireString(primitive);
2671 extent=strlen(token)+MagickPathExtent;
2675 macros=GetMVGMacros(primitive,exception);
2677 for (q=primitive; *q !=
'\0'; )
2682 if (GetNextToken(q,&q,MagickPathExtent,keyword) < 1)
2684 if (*keyword ==
'\0')
2686 if (*keyword ==
'#')
2691 while ((*q !=
'\n') && (*q !=
'\0'))
2695 p=q-strlen(keyword)-1;
2696 primitive_type=UndefinedPrimitive;
2697 current=graphic_context[n]->affine;
2698 GetAffineMatrix(&affine);
2707 if (LocaleCompare(
"affine",keyword) == 0)
2709 (void) GetNextToken(q,&q,extent,token);
2710 affine.sx=GetDrawValue(token,&next_token);
2711 if (token == next_token)
2712 ThrowPointExpectedException(token,exception);
2713 (void) GetNextToken(q,&q,extent,token);
2714 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2715 ThrowPointExpectedException(token,exception);
2717 (void) GetNextToken(q,&q,extent,token);
2718 affine.ry=GetDrawValue(token,&next_token);
2719 if (token == next_token)
2720 ThrowPointExpectedException(token,exception);
2721 (void) GetNextToken(q,&q,extent,token);
2722 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2723 ThrowPointExpectedException(token,exception);
2725 (void) GetNextToken(q,&q,extent,token);
2726 affine.rx=GetDrawValue(token,&next_token);
2727 if (token == next_token)
2728 ThrowPointExpectedException(token,exception);
2729 (void) GetNextToken(q,&q,extent,token);
2730 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2731 ThrowPointExpectedException(token,exception);
2733 (void) GetNextToken(q,&q,extent,token);
2734 affine.sy=GetDrawValue(token,&next_token);
2735 if (token == next_token)
2736 ThrowPointExpectedException(token,exception);
2737 (void) GetNextToken(q,&q,extent,token);
2738 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2739 ThrowPointExpectedException(token,exception);
2741 (void) GetNextToken(q,&q,extent,token);
2742 affine.tx=GetDrawValue(token,&next_token);
2743 if (token == next_token)
2744 ThrowPointExpectedException(token,exception);
2745 (void) GetNextToken(q,&q,extent,token);
2746 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2747 ThrowPointExpectedException(token,exception);
2749 (void) GetNextToken(q,&q,extent,token);
2750 affine.ty=GetDrawValue(token,&next_token);
2751 if (token == next_token)
2752 ThrowPointExpectedException(token,exception);
2755 if (LocaleCompare(
"alpha",keyword) == 0)
2757 primitive_type=AlphaPrimitive;
2760 if (LocaleCompare(
"arc",keyword) == 0)
2762 primitive_type=ArcPrimitive;
2771 if (LocaleCompare(
"bezier",keyword) == 0)
2773 primitive_type=BezierPrimitive;
2776 if (LocaleCompare(
"border-color",keyword) == 0)
2778 (void) GetNextToken(q,&q,extent,token);
2779 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
2780 &graphic_context[n]->border_color,exception);
2789 if (LocaleCompare(
"class",keyword) == 0)
2794 (void) GetNextToken(q,&q,extent,token);
2795 if ((*token ==
'\0') || (*token ==
';'))
2803 for (i=0; i <= n; i++)
2804 if (LocaleCompare(token,graphic_context[i]->
id) == 0)
2808 if (classDepth++ > MagickMaxRecursionDepth)
2810 (void) ThrowMagickException(exception,GetMagickModule(),
2811 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",token);
2815 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2816 if ((graphic_context[n]->render != MagickFalse) &&
2817 (mvg_class != (
const char *) NULL) && (p > primitive))
2828 (void) CloneString(&graphic_context[n]->
id,token);
2829 offset=(ssize_t) (p-primitive);
2830 elements=AcquireString(primitive);
2831 elements[offset]=
'\0';
2832 (void) ConcatenateString(&elements,mvg_class);
2833 (void) ConcatenateString(&elements,
"\n");
2834 (void) ConcatenateString(&elements,q);
2835 primitive=DestroyString(primitive);
2841 if (LocaleCompare(
"clip-path",keyword) == 0)
2849 (void) GetNextToken(q,&q,extent,token);
2855 (void) CloneString(&graphic_context[n]->clip_mask,token);
2856 clip_path=(
const char *) GetValueFromSplayTree(macros,token);
2857 if (clip_path != (
const char *) NULL)
2859 if (graphic_context[n]->clipping_mask != (
Image *) NULL)
2860 graphic_context[n]->clipping_mask=
2861 DestroyImage(graphic_context[n]->clipping_mask);
2862 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2863 graphic_context[n],token,clip_path,exception);
2864 if (graphic_context[n]->compliance != SVGCompliance)
2866 clip_path=(
const char *) GetValueFromSplayTree(macros,
2867 graphic_context[n]->clip_mask);
2868 if (clip_path != (
const char *) NULL)
2869 (
void) SetImageArtifact(image,
2870 graphic_context[n]->clip_mask,clip_path);
2871 status&=(MagickStatusType) DrawClipPath(image,
2872 graphic_context[n],graphic_context[n]->clip_mask,
2878 if (LocaleCompare(
"clip-rule",keyword) == 0)
2883 (void) GetNextToken(q,&q,extent,token);
2884 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2886 if (fill_rule == -1)
2891 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2894 if (LocaleCompare(
"clip-units",keyword) == 0)
2899 (void) GetNextToken(q,&q,extent,token);
2900 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2902 if (clip_units == -1)
2907 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2908 if (clip_units == ObjectBoundingBox)
2910 GetAffineMatrix(¤t);
2911 affine.sx=draw_info->bounds.x2;
2912 affine.sy=draw_info->bounds.y2;
2913 affine.tx=draw_info->bounds.x1;
2914 affine.ty=draw_info->bounds.y1;
2919 if (LocaleCompare(
"circle",keyword) == 0)
2921 primitive_type=CirclePrimitive;
2924 if (LocaleCompare(
"color",keyword) == 0)
2926 primitive_type=ColorPrimitive;
2929 if (LocaleCompare(
"compliance",keyword) == 0)
2935 (void) GetNextToken(q,&q,extent,token);
2936 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2937 MagickComplianceOptions,MagickFalse,token);
2940 if (LocaleCompare(
"currentColor",keyword) == 0)
2942 (void) GetNextToken(q,&q,extent,token);
2951 if (LocaleCompare(
"decorate",keyword) == 0)
2956 (void) GetNextToken(q,&q,extent,token);
2957 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2964 graphic_context[n]->decorate=(DecorationType) decorate;
2967 if (LocaleCompare(
"density",keyword) == 0)
2969 (void) GetNextToken(q,&q,extent,token);
2970 (void) CloneString(&graphic_context[n]->density,token);
2973 if (LocaleCompare(
"direction",keyword) == 0)
2978 (void) GetNextToken(q,&q,extent,token);
2979 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2981 if (direction == -1)
2984 graphic_context[n]->direction=(DirectionType) direction;
2993 if (LocaleCompare(
"ellipse",keyword) == 0)
2995 primitive_type=EllipsePrimitive;
2998 if (LocaleCompare(
"encoding",keyword) == 0)
3000 (void) GetNextToken(q,&q,extent,token);
3001 (void) CloneString(&graphic_context[n]->encoding,token);
3010 if (LocaleCompare(
"fill",keyword) == 0)
3015 (void) GetNextToken(q,&q,extent,token);
3016 if (graphic_context[n]->clip_path != MagickFalse)
3018 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
3019 if (mvg_class != (
const char *) NULL)
3021 (void) DrawPatternPath(image,draw_info,mvg_class,
3022 &graphic_context[n]->fill_pattern,exception);
3025 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
3026 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
3028 (void) DrawPatternPath(image,draw_info,token,
3029 &graphic_context[n]->fill_pattern,exception);
3032 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3033 &graphic_context[n]->fill,exception);
3034 if (graphic_context[n]->fill_alpha != (
double) OpaqueAlpha)
3035 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3038 if (LocaleCompare(
"fill-opacity",keyword) == 0)
3043 (void) GetNextToken(q,&q,extent,token);
3044 if (graphic_context[n]->clip_path != MagickFalse)
3046 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3047 opacity=MagickMin(MagickMax(factor*
3048 GetDrawValue(token,&next_token),0.0),1.0);
3049 if (token == next_token)
3050 ThrowPointExpectedException(token,exception);
3051 if (graphic_context[n]->compliance == SVGCompliance)
3052 graphic_context[n]->fill_alpha*=opacity;
3054 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3055 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
3056 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3058 graphic_context[n]->fill.alpha=(MagickRealType)
3059 ClampToQuantum((
double) QuantumRange*opacity);
3060 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3063 if (LocaleCompare(
"fill-rule",keyword) == 0)
3068 (void) GetNextToken(q,&q,extent,token);
3069 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
3071 if (fill_rule == -1)
3076 graphic_context[n]->fill_rule=(FillRule) fill_rule;
3079 if (LocaleCompare(
"font",keyword) == 0)
3081 (void) GetNextToken(q,&q,extent,token);
3082 (void) CloneString(&graphic_context[n]->font,token);
3083 if (LocaleCompare(
"none",token) == 0)
3084 graphic_context[n]->font=(
char *) RelinquishMagickMemory(
3085 graphic_context[n]->font);
3088 if (LocaleCompare(
"font-family",keyword) == 0)
3090 (void) GetNextToken(q,&q,extent,token);
3091 (void) CloneString(&graphic_context[n]->family,token);
3094 if (LocaleCompare(
"font-size",keyword) == 0)
3096 (void) GetNextToken(q,&q,extent,token);
3097 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3098 if (token == next_token)
3099 ThrowPointExpectedException(token,exception);
3102 if (LocaleCompare(
"font-stretch",keyword) == 0)
3107 (void) GetNextToken(q,&q,extent,token);
3108 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3114 graphic_context[n]->stretch=(StretchType) stretch;
3117 if (LocaleCompare(
"font-style",keyword) == 0)
3122 (void) GetNextToken(q,&q,extent,token);
3123 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3129 graphic_context[n]->style=(StyleType) style;
3132 if (LocaleCompare(
"font-weight",keyword) == 0)
3137 (void) GetNextToken(q,&q,extent,token);
3138 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3140 weight=(ssize_t) StringToUnsignedLong(token);
3141 graphic_context[n]->weight=(size_t) weight;
3150 if (LocaleCompare(
"gradient-units",keyword) == 0)
3152 (void) GetNextToken(q,&q,extent,token);
3155 if (LocaleCompare(
"gravity",keyword) == 0)
3160 (void) GetNextToken(q,&q,extent,token);
3161 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3167 graphic_context[n]->gravity=(GravityType) gravity;
3176 if (LocaleCompare(
"image",keyword) == 0)
3181 primitive_type=ImagePrimitive;
3182 (void) GetNextToken(q,&q,extent,token);
3183 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3189 graphic_context[n]->compose=(CompositeOperator) compose;
3192 if (LocaleCompare(
"interline-spacing",keyword) == 0)
3194 (void) GetNextToken(q,&q,extent,token);
3195 graphic_context[n]->interline_spacing=GetDrawValue(token,
3197 if (token == next_token)
3198 ThrowPointExpectedException(token,exception);
3201 if (LocaleCompare(
"interword-spacing",keyword) == 0)
3203 (void) GetNextToken(q,&q,extent,token);
3204 graphic_context[n]->interword_spacing=GetDrawValue(token,
3206 if (token == next_token)
3207 ThrowPointExpectedException(token,exception);
3216 if (LocaleCompare(
"kerning",keyword) == 0)
3218 (void) GetNextToken(q,&q,extent,token);
3219 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3220 if (token == next_token)
3221 ThrowPointExpectedException(token,exception);
3230 if (LocaleCompare(
"letter-spacing",keyword) == 0)
3232 (void) GetNextToken(q,&q,extent,token);
3233 if (IsValidPoint(token) == MagickFalse)
3235 clone_info=CloneDrawInfo((
ImageInfo *) NULL,graphic_context[n]);
3236 clone_info->text=AcquireString(
" ");
3237 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,&metrics,
3239 graphic_context[n]->kerning=metrics.width*
3240 GetDrawValue(token,&next_token);
3241 clone_info=DestroyDrawInfo(clone_info);
3242 if (token == next_token)
3243 ThrowPointExpectedException(token,exception);
3246 if (LocaleCompare(
"line",keyword) == 0)
3248 primitive_type=LinePrimitive;
3257 if (LocaleCompare(
"mask",keyword) == 0)
3265 (void) GetNextToken(q,&q,extent,token);
3266 mask_path=(
const char *) GetValueFromSplayTree(macros,token);
3267 if (mask_path != (
const char *) NULL)
3269 if (graphic_context[n]->composite_mask != (
Image *) NULL)
3270 graphic_context[n]->composite_mask=
3271 DestroyImage(graphic_context[n]->composite_mask);
3272 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3273 graphic_context[n],token,mask_path,exception);
3274 if (graphic_context[n]->compliance != SVGCompliance)
3275 status=SetImageMask(image,CompositePixelMask,
3276 graphic_context[n]->composite_mask,exception);
3286 if (LocaleCompare(
"offset",keyword) == 0)
3288 (void) GetNextToken(q,&q,extent,token);
3291 if (LocaleCompare(
"opacity",keyword) == 0)
3296 (void) GetNextToken(q,&q,extent,token);
3297 if (graphic_context[n]->clip_path != MagickFalse)
3299 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3300 opacity=MagickMin(MagickMax(factor*
3301 GetDrawValue(token,&next_token),0.0),1.0);
3302 if (token == next_token)
3303 ThrowPointExpectedException(token,exception);
3304 if (graphic_context[n]->compliance == SVGCompliance)
3306 graphic_context[n]->fill_alpha*=opacity;
3307 graphic_context[n]->stroke_alpha*=opacity;
3311 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3312 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3314 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
3316 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3317 graphic_context[n]->stroke.alpha=
3318 graphic_context[n]->stroke_alpha;
3322 graphic_context[n]->fill.alpha=(MagickRealType)
3323 ClampToQuantum((
double) QuantumRange*opacity);
3324 graphic_context[n]->stroke.alpha=(MagickRealType)
3325 ClampToQuantum((
double) QuantumRange*opacity);
3327 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3328 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3337 if (LocaleCompare(
"path",keyword) == 0)
3339 primitive_type=PathPrimitive;
3342 if (LocaleCompare(
"point",keyword) == 0)
3344 primitive_type=PointPrimitive;
3347 if (LocaleCompare(
"polyline",keyword) == 0)
3349 primitive_type=PolylinePrimitive;
3352 if (LocaleCompare(
"polygon",keyword) == 0)
3354 primitive_type=PolygonPrimitive;
3357 if (LocaleCompare(
"pop",keyword) == 0)
3359 if (GetNextToken(q,&q,extent,token) < 1)
3361 if (LocaleCompare(
"class",token) == 0)
3363 if (LocaleCompare(
"clip-path",token) == 0)
3365 if (LocaleCompare(
"defs",token) == 0)
3368 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3372 if (LocaleCompare(
"gradient",token) == 0)
3374 if (LocaleCompare(
"graphic-context",token) == 0)
3378 (void) ThrowMagickException(exception,GetMagickModule(),
3379 DrawError,
"UnbalancedGraphicContextPushPop",
"`%s'",token);
3384 if ((graphic_context[n]->clip_mask != (
char *) NULL) &&
3385 (graphic_context[n]->compliance != SVGCompliance))
3386 if (LocaleCompare(graphic_context[n]->clip_mask,
3387 graphic_context[n-1]->clip_mask) != 0)
3388 status=SetImageMask(image,WritePixelMask,(
Image *) NULL,
3390 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3394 if (LocaleCompare(
"mask",token) == 0)
3396 if (LocaleCompare(
"pattern",token) == 0)
3398 if (LocaleCompare(
"symbol",token) == 0)
3401 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3408 if (LocaleCompare(
"push",keyword) == 0)
3410 if (GetNextToken(q,&q,extent,token) < 1)
3412 if (LocaleCompare(
"class",token) == 0)
3417 for (p=q; *q !=
'\0'; )
3419 if (GetNextToken(q,&q,extent,token) < 1)
3421 if (LocaleCompare(token,
"pop") != 0)
3423 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3424 if (LocaleCompare(token,
"class") != 0)
3428 (void) GetNextToken(q,&q,extent,token);
3431 if (LocaleCompare(
"clip-path",token) == 0)
3433 (void) GetNextToken(q,&q,extent,token);
3434 for (p=q; *q !=
'\0'; )
3436 if (GetNextToken(q,&q,extent,token) < 1)
3438 if (LocaleCompare(token,
"pop") != 0)
3440 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3441 if (LocaleCompare(token,
"clip-path") != 0)
3445 if ((q == (
char *) NULL) || (p == (
char *) NULL) || ((q-4) < p))
3450 (void) GetNextToken(q,&q,extent,token);
3453 if (LocaleCompare(
"defs",token) == 0)
3456 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3460 if (LocaleCompare(
"gradient",token) == 0)
3463 key[2*MagickPathExtent],
3464 name[MagickPathExtent],
3465 type[MagickPathExtent];
3470 (void) GetNextToken(q,&q,extent,token);
3471 (void) CopyMagickString(name,token,MagickPathExtent);
3472 (void) GetNextToken(q,&q,extent,token);
3473 (void) CopyMagickString(type,token,MagickPathExtent);
3474 (void) GetNextToken(q,&q,extent,token);
3475 segment.x1=GetDrawValue(token,&next_token);
3476 if (token == next_token)
3477 ThrowPointExpectedException(token,exception);
3478 (void) GetNextToken(q,&q,extent,token);
3479 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3480 ThrowPointExpectedException(token,exception);
3482 (void) GetNextToken(q,&q,extent,token);
3483 segment.y1=GetDrawValue(token,&next_token);
3484 if (token == next_token)
3485 ThrowPointExpectedException(token,exception);
3486 (void) GetNextToken(q,&q,extent,token);
3487 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3488 ThrowPointExpectedException(token,exception);
3490 (void) GetNextToken(q,&q,extent,token);
3491 segment.x2=GetDrawValue(token,&next_token);
3492 if (token == next_token)
3493 ThrowPointExpectedException(token,exception);
3494 (void) GetNextToken(q,&q,extent,token);
3495 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3496 ThrowPointExpectedException(token,exception);
3498 (void) GetNextToken(q,&q,extent,token);
3499 segment.y2=GetDrawValue(token,&next_token);
3500 if (token == next_token)
3501 ThrowPointExpectedException(token,exception);
3502 if (LocaleCompare(type,
"radial") == 0)
3504 (void) GetNextToken(q,&q,extent,token);
3505 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3506 ThrowPointExpectedException(token,exception);
3508 (void) GetNextToken(q,&q,extent,token);
3510 for (p=q; *q !=
'\0'; )
3512 if (GetNextToken(q,&q,extent,token) < 1)
3514 if (LocaleCompare(token,
"pop") != 0)
3516 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3517 if (LocaleCompare(token,
"gradient") != 0)
3521 if ((q == (
char *) NULL) || (*q ==
'\0') ||
3522 (p == (
char *) NULL) || ((q-4) < p) ||
3523 ((size_t) (q-p+4+1) > extent))
3528 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3529 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3530 graphic_context[n]->affine.ry*segment.y1+
3531 graphic_context[n]->affine.tx;
3532 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3533 graphic_context[n]->affine.sy*segment.y1+
3534 graphic_context[n]->affine.ty;
3535 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3536 graphic_context[n]->affine.ry*segment.y2+
3537 graphic_context[n]->affine.tx;
3538 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3539 graphic_context[n]->affine.sy*segment.y2+
3540 graphic_context[n]->affine.ty;
3541 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3542 (void) SetImageArtifact(image,key,token);
3543 (void) FormatLocaleString(key,MagickPathExtent,
"%s-type",name);
3544 (void) SetImageArtifact(image,key,type);
3545 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3547 (void) FormatLocaleString(geometry,MagickPathExtent,
3548 "%gx%g%+.15g%+.15g",
3549 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3550 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3551 bounds.x1,bounds.y1);
3552 (void) SetImageArtifact(image,key,geometry);
3553 (void) GetNextToken(q,&q,extent,token);
3556 if (LocaleCompare(
"graphic-context",token) == 0)
3559 graphic_context=(
DrawInfo **) ResizeQuantumMemory(
3560 graphic_context,(
size_t) (n+1),
sizeof(*graphic_context));
3561 if (graphic_context == (
DrawInfo **) NULL)
3563 (void) ThrowMagickException(exception,GetMagickModule(),
3564 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3569 graphic_context[n]=CloneDrawInfo((
ImageInfo *) NULL,
3570 graphic_context[n-1]);
3573 (void) GetNextToken(q,&q,extent,token);
3574 (void) CloneString(&graphic_context[n]->
id,token);
3576 if (n > MagickMaxRecursionDepth)
3578 (void) ThrowMagickException(exception,GetMagickModule(),
3579 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",
3585 if (LocaleCompare(
"mask",token) == 0)
3587 (void) GetNextToken(q,&q,extent,token);
3590 if (LocaleCompare(
"pattern",token) == 0)
3593 key[2*MagickPathExtent],
3594 name[MagickPathExtent];
3599 (void) GetNextToken(q,&q,extent,token);
3600 (void) CopyMagickString(name,token,MagickPathExtent);
3601 (void) GetNextToken(q,&q,extent,token);
3602 region.x=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3604 if (token == next_token)
3605 ThrowPointExpectedException(token,exception);
3606 (void) GetNextToken(q,&q,extent,token);
3607 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3608 ThrowPointExpectedException(token,exception);
3610 (void) GetNextToken(q,&q,extent,token);
3611 region.y=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3613 if (token == next_token)
3614 ThrowPointExpectedException(token,exception);
3615 (void) GetNextToken(q,&q,extent,token);
3616 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3617 ThrowPointExpectedException(token,exception);
3619 (void) GetNextToken(q,&q,extent,token);
3620 region.width=CastDoubleToSizeT(floor(GetDrawValue(token,
3622 if (token == next_token)
3623 ThrowPointExpectedException(token,exception);
3624 (void) GetNextToken(q,&q,extent,token);
3625 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3626 ThrowPointExpectedException(token,exception);
3628 (void) GetNextToken(q,&q,extent,token);
3629 region.height=CastDoubleToSizeT(GetDrawValue(token,
3631 if (token == next_token)
3632 ThrowPointExpectedException(token,exception);
3633 for (p=q; *q !=
'\0'; )
3635 if (GetNextToken(q,&q,extent,token) < 1)
3637 if (LocaleCompare(token,
"pop") != 0)
3639 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3640 if (LocaleCompare(token,
"pattern") != 0)
3644 if ((q == (
char *) NULL) || (p == (
char *) NULL) ||
3645 ((q-4) < p) || ((size_t) (q-p+4+1) > extent))
3650 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3651 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3652 (void) SetImageArtifact(image,key,token);
3653 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3655 (void) FormatLocaleString(geometry,MagickPathExtent,
3656 "%.20gx%.20g%+.20g%+.20g",(
double) region.width,(double)
3657 region.height,(
double) region.x,(double) region.y);
3658 (void) SetImageArtifact(image,key,geometry);
3659 (void) GetNextToken(q,&q,extent,token);
3662 if (LocaleCompare(
"symbol",token) == 0)
3665 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3678 if (LocaleCompare(
"rectangle",keyword) == 0)
3680 primitive_type=RectanglePrimitive;
3683 if (LocaleCompare(
"rotate",keyword) == 0)
3685 (void) GetNextToken(q,&q,extent,token);
3686 angle=GetDrawValue(token,&next_token);
3687 if (token == next_token)
3688 ThrowPointExpectedException(token,exception);
3689 affine.sx=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3690 affine.rx=sin(DegreesToRadians(fmod((
double) angle,360.0)));
3691 affine.ry=(-sin(DegreesToRadians(fmod((
double) angle,360.0))));
3692 affine.sy=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3695 if (LocaleCompare(
"roundRectangle",keyword) == 0)
3697 primitive_type=RoundRectanglePrimitive;
3706 if (LocaleCompare(
"scale",keyword) == 0)
3708 (void) GetNextToken(q,&q,extent,token);
3709 affine.sx=GetDrawValue(token,&next_token);
3710 if (token == next_token)
3711 ThrowPointExpectedException(token,exception);
3712 (void) GetNextToken(q,&q,extent,token);
3713 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3714 ThrowPointExpectedException(token,exception);
3716 (void) GetNextToken(q,&q,extent,token);
3717 affine.sy=GetDrawValue(token,&next_token);
3718 if (token == next_token)
3719 ThrowPointExpectedException(token,exception);
3722 if (LocaleCompare(
"skewX",keyword) == 0)
3724 (void) GetNextToken(q,&q,extent,token);
3725 angle=GetDrawValue(token,&next_token);
3726 if (token == next_token)
3727 ThrowPointExpectedException(token,exception);
3728 affine.ry=sin(DegreesToRadians(angle));
3731 if (LocaleCompare(
"skewY",keyword) == 0)
3733 (void) GetNextToken(q,&q,extent,token);
3734 angle=GetDrawValue(token,&next_token);
3735 if (token == next_token)
3736 ThrowPointExpectedException(token,exception);
3737 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3740 if (LocaleCompare(
"stop-color",keyword) == 0)
3746 if (number_stops == 1)
3747 stops=(
StopInfo *) AcquireQuantumMemory(2,
sizeof(*stops));
3749 if (number_stops > 2)
3750 stops=(
StopInfo *) ResizeQuantumMemory(stops,number_stops,
3754 (void) ThrowMagickException(exception,GetMagickModule(),
3755 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3760 (void) GetNextToken(q,&q,extent,token);
3761 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3762 &stop_color,exception);
3763 stops[number_stops-1].color=stop_color;
3764 (void) GetNextToken(q,&q,extent,token);
3765 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3766 stops[number_stops-1].offset=factor*GetDrawValue(token,
3768 if (token == next_token)
3769 ThrowPointExpectedException(token,exception);
3772 if (LocaleCompare(
"stroke",keyword) == 0)
3777 (void) GetNextToken(q,&q,extent,token);
3778 if (graphic_context[n]->clip_path != MagickFalse)
3780 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
3781 if (mvg_class != (
const char *) NULL)
3783 (void) DrawPatternPath(image,draw_info,mvg_class,
3784 &graphic_context[n]->stroke_pattern,exception);
3787 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
3788 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
3790 (void) DrawPatternPath(image,draw_info,token,
3791 &graphic_context[n]->stroke_pattern,exception);
3794 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3795 &graphic_context[n]->stroke,exception);
3796 if (graphic_context[n]->stroke_alpha != (
double) OpaqueAlpha)
3797 graphic_context[n]->stroke.alpha=
3798 graphic_context[n]->stroke_alpha;
3801 if (LocaleCompare(
"stroke-antialias",keyword) == 0)
3803 (void) GetNextToken(q,&q,extent,token);
3804 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3805 MagickTrue : MagickFalse;
3808 if (LocaleCompare(
"stroke-dasharray",keyword) == 0)
3810 if (graphic_context[n]->dash_pattern != (
double *) NULL)
3811 graphic_context[n]->dash_pattern=(
double *)
3812 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3813 if (IsValidPoint(q) != MagickFalse)
3819 (void) GetNextToken(r,&r,extent,token);
3820 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3821 ThrowPointExpectedException(token,exception);
3823 (void) GetNextToken(r,&r,extent,token);
3824 for (x=0; IsValidPoint(token) != MagickFalse; x++)
3826 (void) GetNextToken(r,&r,extent,token);
3828 (void) GetNextToken(r,&r,extent,token);
3830 graphic_context[n]->dash_pattern=(
double *)
3831 AcquireQuantumMemory((
size_t) (2*x+2),
3832 sizeof(*graphic_context[n]->dash_pattern));
3833 if (graphic_context[n]->dash_pattern == (
double *) NULL)
3835 (void) ThrowMagickException(exception,GetMagickModule(),
3836 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3841 (void) memset(graphic_context[n]->dash_pattern,0,(
size_t)
3842 (2*x+2)*
sizeof(*graphic_context[n]->dash_pattern));
3843 for (j=0; j < x; j++)
3845 (void) GetNextToken(q,&q,extent,token);
3846 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3847 ThrowPointExpectedException(token,exception);
3849 (void) GetNextToken(q,&q,extent,token);
3850 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3852 if (token == next_token)
3853 ThrowPointExpectedException(token,exception);
3854 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3857 if (((x & 0x01) != 0) && (j == x))
3858 for ( ; j < (2*x); j++)
3859 graphic_context[n]->dash_pattern[j]=
3860 graphic_context[n]->dash_pattern[j-x];
3861 graphic_context[n]->dash_pattern[j]=0.0;
3864 (void) GetNextToken(q,&q,extent,token);
3867 if (LocaleCompare(
"stroke-dashoffset",keyword) == 0)
3869 (void) GetNextToken(q,&q,extent,token);
3870 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3871 if (token == next_token)
3872 ThrowPointExpectedException(token,exception);
3875 if (LocaleCompare(
"stroke-linecap",keyword) == 0)
3880 (void) GetNextToken(q,&q,extent,token);
3881 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3887 graphic_context[n]->linecap=(LineCap) linecap;
3890 if (LocaleCompare(
"stroke-linejoin",keyword) == 0)
3895 (void) GetNextToken(q,&q,extent,token);
3896 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3903 graphic_context[n]->linejoin=(LineJoin) linejoin;
3906 if (LocaleCompare(
"stroke-miterlimit",keyword) == 0)
3908 (void) GetNextToken(q,&q,extent,token);
3909 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3912 if (LocaleCompare(
"stroke-opacity",keyword) == 0)
3917 (void) GetNextToken(q,&q,extent,token);
3918 if (graphic_context[n]->clip_path != MagickFalse)
3920 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3921 opacity=MagickMin(MagickMax(factor*GetDrawValue(token,&next_token),
3923 if (token == next_token)
3924 ThrowPointExpectedException(token,exception);
3925 if (graphic_context[n]->compliance == SVGCompliance)
3926 graphic_context[n]->stroke_alpha*=opacity;
3928 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3929 if (graphic_context[n]->stroke.alpha != (
double) TransparentAlpha)
3930 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3932 graphic_context[n]->stroke.alpha=(MagickRealType)
3933 ClampToQuantum((
double) QuantumRange*opacity);
3934 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3937 if (LocaleCompare(
"stroke-width",keyword) == 0)
3939 (void) GetNextToken(q,&q,extent,token);
3940 if (graphic_context[n]->clip_path != MagickFalse)
3942 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3943 if ((token == next_token) ||
3944 (graphic_context[n]->stroke_width < 0.0))
3945 ThrowPointExpectedException(token,exception);
3954 if (LocaleCompare(
"text",keyword) == 0)
3956 primitive_type=TextPrimitive;
3960 if (LocaleCompare(
"text-align",keyword) == 0)
3965 (void) GetNextToken(q,&q,extent,token);
3966 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3972 graphic_context[n]->align=(AlignType) align;
3975 if (LocaleCompare(
"text-anchor",keyword) == 0)
3980 (void) GetNextToken(q,&q,extent,token);
3981 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3987 graphic_context[n]->align=(AlignType) align;
3990 if (LocaleCompare(
"text-antialias",keyword) == 0)
3992 (void) GetNextToken(q,&q,extent,token);
3993 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3994 MagickTrue : MagickFalse;
3997 if (LocaleCompare(
"text-undercolor",keyword) == 0)
3999 (void) GetNextToken(q,&q,extent,token);
4000 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
4001 &graphic_context[n]->undercolor,exception);
4004 if (LocaleCompare(
"translate",keyword) == 0)
4006 (void) GetNextToken(q,&q,extent,token);
4007 affine.tx=GetDrawValue(token,&next_token);
4008 if (token == next_token)
4009 ThrowPointExpectedException(token,exception);
4010 (void) GetNextToken(q,&q,extent,token);
4011 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4012 ThrowPointExpectedException(token,exception);
4014 (void) GetNextToken(q,&q,extent,token);
4015 affine.ty=GetDrawValue(token,&next_token);
4016 if (token == next_token)
4017 ThrowPointExpectedException(token,exception);
4027 if (LocaleCompare(
"use",keyword) == 0)
4035 (void) GetNextToken(q,&q,extent,token);
4036 use=(
const char *) GetValueFromSplayTree(macros,token);
4037 if (use != (
const char *) NULL)
4039 clone_info=CloneDrawInfo((
ImageInfo *) NULL,graphic_context[n]);
4040 if (draw_info->macro_expansion != (
size_t *) NULL)
4042 if (*draw_info->macro_expansion >= MacroExpansionLimit)
4044 (void) ThrowMagickException(exception,GetMagickModule(),
4045 DrawError,
"NonconformingDrawingPrimitiveDefinition",
4047 clone_info=DestroyDrawInfo(clone_info);
4048 return(MagickFalse);
4050 (*draw_info->macro_expansion)++;
4052 (void) CloneString(&clone_info->primitive,use);
4053 status=RenderMVGContent(image,clone_info,depth+1,exception);
4054 clone_info=DestroyDrawInfo(clone_info);
4064 if (LocaleCompare(
"viewbox",keyword) == 0)
4066 (void) GetNextToken(q,&q,extent,token);
4067 graphic_context[n]->viewbox.x=CastDoubleToSsizeT(ceil(
4068 GetDrawValue(token,&next_token)-0.5));
4069 if (token == next_token)
4070 ThrowPointExpectedException(token,exception);
4071 (void) GetNextToken(q,&q,extent,token);
4072 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4073 ThrowPointExpectedException(token,exception);
4075 (void) GetNextToken(q,&q,extent,token);
4076 graphic_context[n]->viewbox.y=CastDoubleToSsizeT(
4077 ceil(GetDrawValue(token,&next_token)-0.5));
4078 if (token == next_token)
4079 ThrowPointExpectedException(token,exception);
4080 (void) GetNextToken(q,&q,extent,token);
4081 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4082 ThrowPointExpectedException(token,exception);
4084 (void) GetNextToken(q,&q,extent,token);
4085 graphic_context[n]->viewbox.width=CastDoubleToSizeT(floor(
4086 GetDrawValue(token,&next_token)+0.5));
4087 if (token == next_token)
4088 ThrowPointExpectedException(token,exception);
4089 (void) GetNextToken(q,&q,extent,token);
4090 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4091 ThrowPointExpectedException(token,exception);
4093 (void) GetNextToken(q,&q,extent,token);
4094 graphic_context[n]->viewbox.height=CastDoubleToSizeT(floor(
4095 GetDrawValue(token,&next_token)+0.5));
4096 if (token == next_token)
4097 ThrowPointExpectedException(token,exception);
4106 if (LocaleCompare(
"word-spacing",keyword) == 0)
4108 (void) GetNextToken(q,&q,extent,token);
4109 graphic_context[n]->interword_spacing=GetDrawValue(token,
4111 if (token == next_token)
4112 ThrowPointExpectedException(token,exception);
4124 if (status == MagickFalse)
4126 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
4127 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
4128 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
4129 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
4131 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
4132 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
4133 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
4134 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
4135 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
4137 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4140 if (primitive_type == UndefinedPrimitive)
4144 if (number_stops > 1)
4149 type=LinearGradient;
4150 if (draw_info->gradient.type == RadialGradient)
4151 type=RadialGradient;
4152 (void) GradientImage(image,type,PadSpread,stops,number_stops,
4155 if (number_stops > 0)
4156 stops=(
StopInfo *) RelinquishMagickMemory(stops);
4158 if ((draw_info->debug != MagickFalse) && (q > p))
4159 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(int)
4166 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4167 if (primitive_info[i].text != (
char *) NULL)
4168 primitive_info[i].text=DestroyString(primitive_info[i].text);
4172 primitive_info[0].primitive=primitive_type;
4173 primitive_info[0].point.x=0.0;
4174 primitive_info[0].point.y=0.0;
4175 primitive_info[0].coordinates=0;
4176 primitive_info[0].method=FloodfillMethod;
4177 primitive_info[0].closed_subpath=MagickFalse;
4178 for (x=0; *q !=
'\0'; x++)
4183 if (IsValidPoint(q) == MagickFalse)
4185 (void) GetNextToken(q,&q,extent,token);
4186 point.x=GetDrawValue(token,&next_token);
4187 if (token == next_token)
4188 ThrowPointExpectedException(token,exception);
4189 (void) GetNextToken(q,&q,extent,token);
4190 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4191 ThrowPointExpectedException(token,exception);
4193 (void) GetNextToken(q,&q,extent,token);
4194 point.y=GetDrawValue(token,&next_token);
4195 if (token == next_token)
4196 ThrowPointExpectedException(token,exception);
4197 (void) GetNextToken(q,(
const char **) NULL,extent,token);
4199 (void) GetNextToken(q,&q,extent,token);
4200 primitive_info[i].primitive=primitive_type;
4201 primitive_info[i].point=point;
4202 primitive_info[i].coordinates=0;
4203 primitive_info[i].method=FloodfillMethod;
4204 primitive_info[i].closed_subpath=MagickFalse;
4207 if (i < (ssize_t) number_points)
4209 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4211 primitive_info=(*mvg_info.primitive_info);
4212 if (status == MagickFalse)
4215 if (status == MagickFalse)
4217 if (primitive_info[j].text != (
char *) NULL)
4218 primitive_info[j].text=DestroyString(primitive_info[j].text);
4219 primitive_info[j].primitive=primitive_type;
4220 primitive_info[j].coordinates=(size_t) x;
4221 primitive_info[j].method=FloodfillMethod;
4222 primitive_info[j].closed_subpath=MagickFalse;
4226 bounds.x1=primitive_info[j].point.x;
4227 bounds.y1=primitive_info[j].point.y;
4228 bounds.x2=primitive_info[j].point.x;
4229 bounds.y2=primitive_info[j].point.y;
4230 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4232 point=primitive_info[j+k].point;
4233 if (point.x < bounds.x1)
4235 if (point.y < bounds.y1)
4237 if (point.x > bounds.x2)
4239 if (point.y > bounds.y2)
4245 coordinates=(double) primitive_info[j].coordinates;
4246 switch (primitive_type)
4248 case RectanglePrimitive:
4253 case RoundRectanglePrimitive:
4260 alpha=bounds.x2-bounds.x1;
4261 beta=bounds.y2-bounds.y1;
4262 radius=hypot(alpha,beta);
4264 coordinates+=2.0*((size_t) ceil((
double) MagickPI*radius))+6.0*
4265 BezierQuantum+360.0;
4268 case BezierPrimitive:
4270 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4279 (void) GetNextToken(q,&q,extent,token);
4282 for (s=token; *s !=
'\0'; s=t)
4287 value=GetDrawValue(s,&t);
4296 for (s=token; *s !=
'\0'; s++)
4297 if (strspn(s,
"AaCcQqSsTt") != 0)
4298 coordinates+=(20.0*BezierQuantum)+360.0;
4304 if (status == MagickFalse)
4306 if (((
size_t) (i+coordinates)) >= number_points)
4311 number_points+=(size_t) coordinates+1;
4312 if (number_points < (
size_t) coordinates)
4314 (void) ThrowMagickException(exception,GetMagickModule(),
4315 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
4321 status&=CheckPrimitiveExtent(&mvg_info,(
double) number_points+
4322 (
double) coordinates+1.0);
4323 primitive_info=(*mvg_info.primitive_info);
4324 if (status == MagickFalse)
4327 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,
4328 PrimitiveExtentPad);
4329 primitive_info=(*mvg_info.primitive_info);
4330 if (status == MagickFalse)
4333 switch (primitive_type)
4335 case PointPrimitive:
4338 if (primitive_info[j].coordinates != 1)
4343 status&=(MagickStatusType) TracePoint(primitive_info+j,
4344 primitive_info[j].point);
4345 primitive_info=(*mvg_info.primitive_info);
4346 if (status == MagickFalse)
4348 i=j+(ssize_t) primitive_info[j].coordinates;
4353 if (primitive_info[j].coordinates != 2)
4358 status&=(MagickStatusType) TraceLine(primitive_info+j,
4359 primitive_info[j].point,primitive_info[j+1].point);
4360 primitive_info=(*mvg_info.primitive_info);
4361 if (status == MagickFalse)
4363 i=j+(ssize_t) primitive_info[j].coordinates;
4366 case RectanglePrimitive:
4368 if (primitive_info[j].coordinates != 2)
4373 status&=(MagickStatusType) TraceRectangle(primitive_info+j,
4374 primitive_info[j].point,primitive_info[j+1].point);
4375 primitive_info=(*mvg_info.primitive_info);
4376 if (status == MagickFalse)
4378 i=j+(ssize_t) primitive_info[j].coordinates;
4381 case RoundRectanglePrimitive:
4383 if (primitive_info[j].coordinates != 3)
4388 if ((primitive_info[j+2].point.x < 0.0) ||
4389 (primitive_info[j+2].point.y < 0.0))
4394 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4399 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4404 status&=(MagickStatusType) TraceRoundRectangle(&mvg_info,
4405 primitive_info[j].point,primitive_info[j+1].point,
4406 primitive_info[j+2].point);
4407 primitive_info=(*mvg_info.primitive_info);
4408 if (status == MagickFalse)
4410 i=j+(ssize_t) primitive_info[j].coordinates;
4415 if (primitive_info[j].coordinates != 3)
4420 status&=(MagickStatusType) TraceArc(&mvg_info,primitive_info[j].point,
4421 primitive_info[j+1].point,primitive_info[j+2].point);
4422 primitive_info=(*mvg_info.primitive_info);
4423 if (status == MagickFalse)
4425 i=j+(ssize_t) primitive_info[j].coordinates;
4428 case EllipsePrimitive:
4430 if (primitive_info[j].coordinates != 3)
4435 if ((primitive_info[j+1].point.x < 0.0) ||
4436 (primitive_info[j+1].point.y < 0.0))
4441 status&=(MagickStatusType) TraceEllipse(&mvg_info,
4442 primitive_info[j].point,primitive_info[j+1].point,
4443 primitive_info[j+2].point);
4444 primitive_info=(*mvg_info.primitive_info);
4445 if (status == MagickFalse)
4447 i=j+(ssize_t) primitive_info[j].coordinates;
4450 case CirclePrimitive:
4452 if (primitive_info[j].coordinates != 2)
4457 status&=(MagickStatusType) TraceCircle(&mvg_info,
4458 primitive_info[j].point,primitive_info[j+1].point);
4459 primitive_info=(*mvg_info.primitive_info);
4460 if (status == MagickFalse)
4462 i=j+(ssize_t) primitive_info[j].coordinates;
4465 case PolylinePrimitive:
4467 if (primitive_info[j].coordinates < 1)
4474 case PolygonPrimitive:
4476 if (primitive_info[j].coordinates < 3)
4481 primitive_info[i]=primitive_info[j];
4482 primitive_info[i].coordinates=0;
4483 primitive_info[j].coordinates++;
4484 primitive_info[j].closed_subpath=MagickTrue;
4488 case BezierPrimitive:
4490 if (primitive_info[j].coordinates < 3)
4495 status&=(MagickStatusType) TraceBezier(&mvg_info,
4496 primitive_info[j].coordinates);
4497 primitive_info=(*mvg_info.primitive_info);
4498 if (status == MagickFalse)
4500 i=j+(ssize_t) primitive_info[j].coordinates;
4505 coordinates=(double) TracePath(&mvg_info,token,exception);
4506 primitive_info=(*mvg_info.primitive_info);
4507 if (status == MagickFalse)
4509 if (coordinates < 0.0)
4514 i=(ssize_t) (j+coordinates);
4517 case AlphaPrimitive:
4518 case ColorPrimitive:
4523 if (primitive_info[j].coordinates != 1)
4528 (void) GetNextToken(q,&q,extent,token);
4529 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4535 primitive_info[j].method=(PaintMethod) method;
4540 if (primitive_info[j].coordinates != 1)
4546 (void) GetNextToken(q,&q,extent,token);
4547 (void) CloneString(&primitive_info[j].text,token);
4551 clone_info=CloneDrawInfo((
ImageInfo *) NULL,graphic_context[n]);
4552 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4553 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4555 mvg_info.point=primitive_info->point;
4556 primitive_info->point.x+=cursor;
4560 mvg_info.point=primitive_info->point;
4563 clone_info->render=MagickFalse;
4564 clone_info->text=AcquireString(token);
4565 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,
4566 &metrics,exception);
4567 clone_info=DestroyDrawInfo(clone_info);
4568 cursor+=metrics.width;
4569 if (graphic_context[n]->compliance != SVGCompliance)
4573 case ImagePrimitive:
4575 if (primitive_info[j].coordinates != 2)
4580 (void) GetNextToken(q,&q,extent,token);
4581 (void) CloneString(&primitive_info[j].text,token);
4588 primitive_info[i].primitive=UndefinedPrimitive;
4589 if ((draw_info->debug != MagickFalse) && (q > p))
4590 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(int) (q-p),p);
4594 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4595 &graphic_context[n]->affine));
4596 primitive_info=(*mvg_info.primitive_info);
4599 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4600 graphic_context[n]->stroke_width);
4601 primitive_info=(*mvg_info.primitive_info);
4609 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4611 point=primitive_info[i].point;
4612 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4613 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4614 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4615 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4616 point=primitive_info[i].point;
4617 if (point.x < graphic_context[n]->bounds.x1)
4618 graphic_context[n]->bounds.x1=point.x;
4619 if (point.y < graphic_context[n]->bounds.y1)
4620 graphic_context[n]->bounds.y1=point.y;
4621 if (point.x > graphic_context[n]->bounds.x2)
4622 graphic_context[n]->bounds.x2=point.x;
4623 if (point.y > graphic_context[n]->bounds.y2)
4624 graphic_context[n]->bounds.y2=point.y;
4625 if (primitive_info[i].primitive == ImagePrimitive)
4627 if (i >= (ssize_t) number_points)
4628 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
4630 if (graphic_context[n]->render != MagickFalse)
4632 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4633 (graphic_context[n]->clip_mask != (
char *) NULL) &&
4634 (LocaleCompare(graphic_context[n]->clip_mask,
4635 graphic_context[n-1]->clip_mask) != 0))
4640 clip_path=(
const char *) GetValueFromSplayTree(macros,
4641 graphic_context[n]->clip_mask);
4642 if (clip_path != (
const char *) NULL)
4643 (
void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4645 status&=(MagickStatusType) DrawClipPath(image,graphic_context[n],
4646 graphic_context[n]->clip_mask,exception);
4648 status&=(MagickStatusType) DrawPrimitive(image,graphic_context[n],
4649 primitive_info,exception);
4651 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4653 if (proceed == MagickFalse)
4658 if (draw_info->debug != MagickFalse)
4659 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end draw-image");
4663 macros=DestroySplayTree(macros);
4664 token=DestroyString(token);
4667 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4668 if (primitive_info[i].text != (
char *) NULL)
4669 primitive_info[i].text=DestroyString(primitive_info[i].text);
4670 primitive_info=(
PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4672 primitive=DestroyString(primitive);
4674 stops=(
StopInfo *) RelinquishMagickMemory(stops);
4675 for ( ; n >= 0; n--)
4676 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4677 graphic_context=(
DrawInfo **) RelinquishMagickMemory(graphic_context);
4678 if ((status == MagickFalse) && (exception->severity < ErrorException))
4679 ThrowBinaryException(DrawError,
"NonconformingDrawingPrimitiveDefinition",
4681 return(status != 0 ? MagickTrue : MagickFalse);
4684MagickExport MagickBooleanType DrawImage(
Image *image,
const DrawInfo *draw_info,
4691 macro_expansion = 0;
4693 ((
DrawInfo *) draw_info)->macro_expansion=(¯o_expansion);
4694 status=RenderMVGContent(image,draw_info,0,exception);
4695 ((
DrawInfo *) draw_info)->macro_expansion=(
size_t *) NULL;
4730MagickExport MagickBooleanType DrawPatternPath(
Image *image,
4735 property[MagickPathExtent];
4751 assert(image != (
Image *) NULL);
4752 assert(image->signature == MagickCoreSignature);
4753 assert(draw_info != (
const DrawInfo *) NULL);
4754 assert(name != (
const char *) NULL);
4755 if (IsEventLogging() != MagickFalse)
4756 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4757 (void) FormatLocaleString(property,MagickPathExtent,
"%s",name);
4758 path=GetImageArtifact(image,property);
4759 if (path == (
const char *) NULL)
4760 return(MagickFalse);
4761 (void) FormatLocaleString(property,MagickPathExtent,
"%s-geometry",name);
4762 geometry=GetImageArtifact(image,property);
4763 if (geometry == (
const char *) NULL)
4764 return(MagickFalse);
4765 if ((*pattern) != (
Image *) NULL)
4766 *pattern=DestroyImage(*pattern);
4767 image_info=AcquireImageInfo();
4768 image_info->size=AcquireString(geometry);
4769 *pattern=AcquireImage(image_info,exception);
4770 image_info=DestroyImageInfo(image_info);
4771 (void) QueryColorCompliance(
"#00000000",AllCompliance,
4772 &(*pattern)->background_color,exception);
4773 (void) SetImageBackgroundColor(*pattern,exception);
4774 if (draw_info->debug != MagickFalse)
4775 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4776 "begin pattern-path %s %s",name,geometry);
4777 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
4778 if (clone_info->fill_pattern != (
Image *) NULL)
4779 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4780 if (clone_info->stroke_pattern != (
Image *) NULL)
4781 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4782 (void) FormatLocaleString(property,MagickPathExtent,
"%s-type",name);
4783 type=GetImageArtifact(image,property);
4784 if (type != (
const char *) NULL)
4785 clone_info->gradient.type=(GradientType) ParseCommandOption(
4786 MagickGradientOptions,MagickFalse,type);
4787 (void) CloneString(&clone_info->primitive,path);
4788 status=RenderMVGContent(*pattern,clone_info,0,exception);
4789 clone_info=DestroyDrawInfo(clone_info);
4790 if (draw_info->debug != MagickFalse)
4791 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end pattern-path");
4832 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4834 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4835 polygon_info=(
PolygonInfo **) RelinquishMagickMemory(polygon_info);
4836 return(polygon_info);
4843 *magick_restrict path_info;
4851 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4852 polygon_info=(
PolygonInfo **) AcquireQuantumMemory(number_threads,
4853 sizeof(*polygon_info));
4856 (void) ThrowMagickException(exception,GetMagickModule(),
4857 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4860 (void) memset(polygon_info,0,number_threads*
sizeof(*polygon_info));
4861 path_info=ConvertPrimitiveToPath(primitive_info,exception);
4862 if (path_info == (
PathInfo *) NULL)
4863 return(DestroyPolygonTLS(polygon_info));
4864 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4867 (void) ThrowMagickException(exception,GetMagickModule(),
4868 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4869 return(DestroyPolygonTLS(polygon_info));
4871 path_info=(
PathInfo *) RelinquishMagickMemory(path_info);
4872 return(polygon_info);
4875static MagickBooleanType ClonePolygonEdgesTLS(
PolygonInfo **polygon_info,
4881 for (i=1; i < (ssize_t) number_threads; i++)
4889 polygon_info[i]=(
PolygonInfo *) AcquireMagickMemory(
4890 sizeof(*polygon_info[i]));
4893 (void) ThrowMagickException(exception,GetMagickModule(),
4894 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4895 return(MagickFalse);
4897 polygon_info[i]->number_edges=0;
4898 edge_info=polygon_info[0]->edges;
4899 polygon_info[i]->edges=(
EdgeInfo *) AcquireQuantumMemory(
4900 polygon_info[0]->number_edges,
sizeof(*edge_info));
4901 if (polygon_info[i]->edges == (
EdgeInfo *) NULL)
4903 (void) ThrowMagickException(exception,GetMagickModule(),
4904 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4905 return(MagickFalse);
4907 (void) memcpy(polygon_info[i]->edges,edge_info,
4908 polygon_info[0]->number_edges*
sizeof(*edge_info));
4909 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4910 polygon_info[i]->edges[j].points=(
PointInfo *) NULL;
4911 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4912 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4914 edge_info=polygon_info[0]->edges+j;
4915 polygon_info[i]->edges[j].points=(
PointInfo *) AcquireQuantumMemory(
4916 edge_info->number_points,
sizeof(*edge_info));
4917 if (polygon_info[i]->edges[j].points == (
PointInfo *) NULL)
4919 (void) ThrowMagickException(exception,GetMagickModule(),
4920 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4921 return(MagickFalse);
4923 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4924 edge_info->number_points*
sizeof(*edge_info->points));
4930static size_t DestroyEdge(
PolygonInfo *polygon_info,
const ssize_t edge)
4932 assert(edge < (ssize_t) polygon_info->number_edges);
4933 polygon_info->edges[edge].points=(
PointInfo *) RelinquishMagickMemory(
4934 polygon_info->edges[edge].points);
4935 polygon_info->number_edges--;
4936 if (edge < (ssize_t) polygon_info->number_edges)
4937 (
void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4938 (polygon_info->number_edges-(
size_t) edge)*
sizeof(*polygon_info->edges));
4939 return(polygon_info->number_edges);
4942static double GetFillAlpha(
PolygonInfo *polygon_info,
const double mid,
4943 const MagickBooleanType fill,
const FillRule fill_rule,
const ssize_t x,
4944 const ssize_t y,
double *stroke_alpha)
4971 p=polygon_info->edges;
4972 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4974 if ((
double) y <= (p->bounds.y1-mid-0.5))
4976 if ((
double) y > (p->bounds.y2+mid+0.5))
4979 (void) DestroyEdge(polygon_info,j--);
4982 if (((
double) x <= (p->bounds.x1-mid-0.5)) ||
4983 ((double) x > (p->bounds.x2+mid+0.5)))
4985 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4986 for ( ; i < (ssize_t) p->number_points; i++)
4988 if ((
double) y <= (p->points[i-1].y-mid-0.5))
4990 if ((
double) y > (p->points[i].y+mid+0.5))
4992 if (p->scanline != (
double) y)
4994 p->scanline=(double) y;
4995 p->highwater=(size_t) i;
5001 delta.x=(q+1)->x-q->x;
5002 delta.y=(q+1)->y-q->y;
5003 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
5009 delta.x=(double) x-q->x;
5010 delta.y=(double) y-q->y;
5011 distance=delta.x*delta.x+delta.y*delta.y;
5015 alpha=delta.x*delta.x+delta.y*delta.y;
5021 delta.x=(double) x-(q+1)->x;
5022 delta.y=(double) y-(q+1)->y;
5023 distance=delta.x*delta.x+delta.y*delta.y;
5030 alpha=MagickSafeReciprocal(alpha);
5031 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
5032 distance=alpha*beta*beta;
5039 if (p->ghostline == MagickFalse)
5042 if ((*stroke_alpha < 1.0) &&
5043 (distance <= ((alpha+0.25)*(alpha+0.25))))
5046 if (distance <= ((alpha+0.25)*(alpha+0.25)))
5051 if (fabs(distance-1.0) >= MagickEpsilon)
5052 beta=sqrt((
double) distance);
5054 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
5055 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
5059 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
5061 if (distance <= 0.0)
5068 if (fabs(beta) < MagickEpsilon)
5071 if (fabs(distance-1.0) >= MagickEpsilon)
5072 beta=sqrt(distance);
5075 if (subpath_alpha < (alpha*alpha))
5076 subpath_alpha=alpha*alpha;
5082 if (fill == MagickFalse)
5084 if (subpath_alpha >= 1.0)
5090 p=polygon_info->edges;
5091 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
5093 if ((
double) y <= p->bounds.y1)
5095 if (((
double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
5097 if ((
double) x > p->bounds.x2)
5099 winding_number+=p->direction != 0 ? 1 : -1;
5102 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
5103 for ( ; i < (ssize_t) (p->number_points-1); i++)
5104 if ((
double) y <= p->points[i].y)
5107 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
5108 winding_number+=p->direction != 0 ? 1 : -1;
5110 if (fill_rule != NonZeroRule)
5112 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
5116 if (MagickAbsoluteValue(winding_number) != 0)
5118 return(subpath_alpha);
5121static MagickBooleanType DrawPolygonPrimitive(
Image *image,
5125 typedef struct _ExtentInfo
5154 **magick_restrict polygon_info;
5166 assert(image != (
Image *) NULL);
5167 assert(image->signature == MagickCoreSignature);
5168 assert(draw_info != (
DrawInfo *) NULL);
5169 assert(draw_info->signature == MagickCoreSignature);
5171 if (IsEventLogging() != MagickFalse)
5172 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5173 if (primitive_info->coordinates <= 1)
5178 polygon_info=AcquirePolygonTLS(primitive_info,exception);
5180 return(MagickFalse);
5181 if (draw_info->debug != MagickFalse)
5182 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-polygon");
5183 fill=(primitive_info->method == FillToBorderMethod) ||
5184 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
5185 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5186 bounds=polygon_info[0]->edges[0].bounds;
5187 artifact=GetImageArtifact(image,
"draw:render-bounding-rectangles");
5188 if (IsStringTrue(artifact) != MagickFalse)
5189 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
5190 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
5192 p=polygon_info[0]->edges+i;
5193 if (p->bounds.x1 < bounds.x1)
5194 bounds.x1=p->bounds.x1;
5195 if (p->bounds.y1 < bounds.y1)
5196 bounds.y1=p->bounds.y1;
5197 if (p->bounds.x2 > bounds.x2)
5198 bounds.x2=p->bounds.x2;
5199 if (p->bounds.y2 > bounds.y2)
5200 bounds.y2=p->bounds.y2;
5202 bounds.x1-=(mid+1.0);
5203 bounds.y1-=(mid+1.0);
5204 bounds.x2+=(mid+1.0);
5205 bounds.y2+=(mid+1.0);
5206 if ((bounds.x1 >= (
double) image->columns) ||
5207 (bounds.y1 >= (
double) image->rows) ||
5208 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5210 polygon_info=DestroyPolygonTLS(polygon_info);
5213 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5214 (
double) image->columns-1.0 : bounds.x1;
5215 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5216 (
double) image->rows-1.0 : bounds.y1;
5217 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5218 (
double) image->columns-1.0 : bounds.x2;
5219 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5220 (
double) image->rows-1.0 : bounds.y2;
5221 poly_extent.x1=CastDoubleToSsizeT(ceil(bounds.x1-0.5));
5222 poly_extent.y1=CastDoubleToSsizeT(ceil(bounds.y1-0.5));
5223 poly_extent.x2=CastDoubleToSsizeT(floor(bounds.x2+0.5));
5224 poly_extent.y2=CastDoubleToSsizeT(floor(bounds.y2+0.5));
5225 number_threads=(size_t) GetMagickNumberThreads(image,image,(
size_t)
5226 (poly_extent.y2-poly_extent.y1+1),1);
5227 status=ClonePolygonEdgesTLS(polygon_info,number_threads,exception);
5228 if (status == MagickFalse)
5230 polygon_info=DestroyPolygonTLS(polygon_info);
5233 image_view=AcquireAuthenticCacheView(image,exception);
5234 if ((primitive_info->coordinates == 1) ||
5235 (polygon_info[0]->number_edges == 0))
5240#if defined(MAGICKCORE_OPENMP_SUPPORT)
5241 #pragma omp parallel for schedule(static) shared(status) \
5242 num_threads((int) number_threads)
5244 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5255 if (status == MagickFalse)
5258 q=GetCacheViewAuthenticPixels(image_view,x,y,(
size_t) (poly_extent.x2-
5260 if (q == (Quantum *) NULL)
5265 GetPixelInfo(image,&pixel);
5266 for ( ; x <= poly_extent.x2; x++)
5268 if ((x == CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5))) &&
5269 (y == CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5))))
5271 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&pixel,
5273 SetPixelViaPixelInfo(image,&pixel,q);
5275 q+=(ptrdiff_t) GetPixelChannels(image);
5277 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5280 image_view=DestroyCacheView(image_view);
5281 polygon_info=DestroyPolygonTLS(polygon_info);
5282 if (draw_info->debug != MagickFalse)
5283 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5284 " end draw-polygon");
5290#if defined(MAGICKCORE_OPENMP_SUPPORT)
5291 #pragma omp parallel for schedule(static) shared(status) \
5292 num_threads((int) number_threads)
5294 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5297 id = GetOpenMPThreadId();
5305 if (status == MagickFalse)
5307 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(
size_t)
5308 (poly_extent.x2-poly_extent.x1+1),1,exception);
5309 if (q == (Quantum *) NULL)
5314 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5327 fill_alpha=GetFillAlpha(polygon_info[
id],mid,fill,draw_info->fill_rule,
5329 if (draw_info->stroke_antialias == MagickFalse)
5331 fill_alpha=fill_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5332 stroke_alpha=stroke_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5334 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&fill_color,
5336 CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
5337 (
double) GetPixelAlpha(image,q),q);
5338 GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&stroke_color,
5340 CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
5341 (
double) GetPixelAlpha(image,q),q);
5342 q+=(ptrdiff_t) GetPixelChannels(image);
5344 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5347 image_view=DestroyCacheView(image_view);
5348 polygon_info=DestroyPolygonTLS(polygon_info);
5349 if (draw_info->debug != MagickFalse)
5350 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-polygon");
5383static void LogPrimitiveInfo(
const PrimitiveInfo *primitive_info)
5409 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5410 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5411 switch (primitive_info->primitive)
5413 case AlphaPrimitive:
5415 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5416 "AlphaPrimitive %.20g,%.20g %s",(double) x,(
double) y,
5417 methods[primitive_info->method]);
5420 case ColorPrimitive:
5422 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5423 "ColorPrimitive %.20g,%.20g %s",(double) x,(
double) y,
5424 methods[primitive_info->method]);
5427 case ImagePrimitive:
5429 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5430 "ImagePrimitive %.20g,%.20g",(double) x,(
double) y);
5433 case PointPrimitive:
5435 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5436 "PointPrimitive %.20g,%.20g %s",(double) x,(
double) y,
5437 methods[primitive_info->method]);
5442 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5443 "TextPrimitive %.20g,%.20g",(double) x,(
double) y);
5450 p=primitive_info[0].point;
5453 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5455 point=primitive_info[i].point;
5456 if (coordinates <= 0)
5458 coordinates=(ssize_t) primitive_info[i].coordinates;
5459 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5460 " begin open (%.20g)",(double) coordinates);
5463 point=primitive_info[i].point;
5464 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5465 (fabs(q.y-point.y) >= MagickEpsilon))
5466 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5467 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5469 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
5470 " %.20g: %g %g (duplicate)",(
double) coordinates,point.x,point.y);
5473 if (coordinates > 0)
5475 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5476 (fabs(p.y-point.y) >= MagickEpsilon))
5477 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end last (%.20g)",
5478 (double) coordinates);
5480 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end open (%.20g)",
5481 (
double) coordinates);
5485MagickExport MagickBooleanType DrawPrimitive(
Image *image,
5502 if (draw_info->debug != MagickFalse)
5504 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5505 " begin draw-primitive");
5506 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5507 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5508 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5509 draw_info->affine.tx,draw_info->affine.ty);
5512 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5513 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5514 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5515 status&=(MagickStatusType) SetImageColorspace(image,sRGBColorspace,
5517 if (draw_info->compliance == SVGCompliance)
5519 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5520 draw_info->clipping_mask,exception);
5521 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5522 draw_info->composite_mask,exception);
5524 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5525 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5526 image_view=AcquireAuthenticCacheView(image,exception);
5527 switch (primitive_info->primitive)
5529 case AlphaPrimitive:
5531 if ((image->alpha_trait & BlendPixelTrait) == 0)
5532 status&=(MagickStatusType) SetImageAlphaChannel(image,
5533 OpaqueAlphaChannel,exception);
5534 switch (primitive_info->method)
5545 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5546 if (q == (Quantum *) NULL)
5548 GetFillColor(draw_info,x,y,&pixel,exception);
5549 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5550 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5560 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5561 x,y,&target,exception);
5562 GetPixelInfo(image,&pixel);
5563 for (y=0; y < (ssize_t) image->rows; y++)
5568 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5570 if (q == (Quantum *) NULL)
5572 for (x=0; x < (ssize_t) image->columns; x++)
5574 GetPixelInfoPixel(image,q,&pixel);
5575 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5577 q+=(ptrdiff_t) GetPixelChannels(image);
5580 GetFillColor(draw_info,x,y,&pixel,exception);
5581 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5582 q+=(ptrdiff_t) GetPixelChannels(image);
5584 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5586 if (status == MagickFalse)
5591 case FloodfillMethod:
5592 case FillToBorderMethod:
5600 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5601 TileVirtualPixelMethod,x,y,&target,exception);
5602 if (primitive_info->method == FillToBorderMethod)
5604 target.red=(double) draw_info->border_color.red;
5605 target.green=(double) draw_info->border_color.green;
5606 target.blue=(double) draw_info->border_color.blue;
5608 channel_mask=SetImageChannelMask(image,AlphaChannel);
5609 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5610 &target,x,y,primitive_info->method == FloodfillMethod ?
5611 MagickFalse : MagickTrue,exception);
5612 (void) SetImageChannelMask(image,channel_mask);
5620 for (y=0; y < (ssize_t) image->rows; y++)
5625 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5627 if (q == (Quantum *) NULL)
5629 for (x=0; x < (ssize_t) image->columns; x++)
5631 GetFillColor(draw_info,x,y,&pixel,exception);
5632 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5633 q+=(ptrdiff_t) GetPixelChannels(image);
5635 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5637 if (status == MagickFalse)
5645 case ColorPrimitive:
5647 switch (primitive_info->method)
5658 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5659 if (q == (Quantum *) NULL)
5661 GetPixelInfo(image,&pixel);
5662 GetFillColor(draw_info,x,y,&pixel,exception);
5663 SetPixelViaPixelInfo(image,&pixel,q);
5664 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5674 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5675 x,y,&target,exception);
5676 for (y=0; y < (ssize_t) image->rows; y++)
5681 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5683 if (q == (Quantum *) NULL)
5685 for (x=0; x < (ssize_t) image->columns; x++)
5687 GetPixelInfoPixel(image,q,&pixel);
5688 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5690 q+=(ptrdiff_t) GetPixelChannels(image);
5693 GetFillColor(draw_info,x,y,&pixel,exception);
5694 SetPixelViaPixelInfo(image,&pixel,q);
5695 q+=(ptrdiff_t) GetPixelChannels(image);
5697 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5699 if (status == MagickFalse)
5704 case FloodfillMethod:
5705 case FillToBorderMethod:
5710 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5711 TileVirtualPixelMethod,x,y,&target,exception);
5712 if (primitive_info->method == FillToBorderMethod)
5714 target.red=(double) draw_info->border_color.red;
5715 target.green=(double) draw_info->border_color.green;
5716 target.blue=(double) draw_info->border_color.blue;
5718 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5719 &target,x,y,primitive_info->method == FloodfillMethod ?
5720 MagickFalse : MagickTrue,exception);
5728 GetPixelInfo(image,&pixel);
5729 for (y=0; y < (ssize_t) image->rows; y++)
5734 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5736 if (q == (Quantum *) NULL)
5738 for (x=0; x < (ssize_t) image->columns; x++)
5740 GetFillColor(draw_info,x,y,&pixel,exception);
5741 SetPixelViaPixelInfo(image,&pixel,q);
5742 q+=(ptrdiff_t) GetPixelChannels(image);
5744 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5746 if (status == MagickFalse)
5754 case ImagePrimitive:
5760 composite_geometry[MagickPathExtent],
5761 magic[MagickPathExtent] = {
'\0'};
5777 if (primitive_info->text == (
char *) NULL)
5779 clone_info=AcquireImageInfo();
5780 composite_images=(
Image *) NULL;
5781 if (LocaleNCompare(primitive_info->text,
"data:",5) == 0)
5782 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5785 if (*primitive_info->text !=
'\0')
5790 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5792 (void) SetImageInfo(clone_info,1,exception);
5793 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5795 if (clone_info->size != (
char *) NULL)
5796 clone_info->size=DestroyString(clone_info->size);
5797 if (clone_info->extract != (
char *) NULL)
5798 clone_info->extract=DestroyString(clone_info->extract);
5799 GetPathComponent(clone_info->filename,MagickPath,magic);
5801 composite_images=ReadImage(clone_info,exception);
5803 (
void) ThrowMagickException(exception,GetMagickModule(),
5804 FileOpenError,
"UnableToOpenFile",
"`%s'",clone_info->filename);
5806 clone_info=DestroyImageInfo(clone_info);
5807 if (composite_images == (
Image *) NULL)
5812 composite_image=RemoveFirstImageFromList(&composite_images);
5813 composite_images=DestroyImageList(composite_images);
5814 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5815 NULL,(
void *) NULL);
5816 x1=CastDoubleToSsizeT(ceil(primitive_info[1].point.x-0.5));
5817 y1=CastDoubleToSsizeT(ceil(primitive_info[1].point.y-0.5));
5818 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5819 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5824 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5825 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
5826 composite_image->filter=image->filter;
5827 status&=(MagickStatusType) TransformImage(&composite_image,
5828 (
char *) NULL,composite_geometry,exception);
5830 if (composite_image->alpha_trait == UndefinedPixelTrait)
5831 status&=(MagickStatusType) SetImageAlphaChannel(composite_image,
5832 OpaqueAlphaChannel,exception);
5833 if (draw_info->alpha != OpaqueAlpha)
5834 status&=(MagickStatusType) SetImageAlpha(composite_image,
5835 draw_info->alpha,exception);
5836 SetGeometry(image,&geometry);
5837 image->gravity=draw_info->gravity;
5840 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5841 "%.20gx%.20g%+.20g%+.20g",(
double) composite_image->columns,(double)
5842 composite_image->rows,(
double) geometry.x,(double) geometry.y);
5843 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
5844 affine=draw_info->affine;
5845 affine.tx=(double) geometry.x;
5846 affine.ty=(double) geometry.y;
5847 composite_image->interpolate=image->interpolate;
5848 if ((draw_info->compose == OverCompositeOp) ||
5849 (draw_info->compose == SrcOverCompositeOp))
5850 status&=(MagickStatusType) DrawAffineImage(image,composite_image,
5853 status&=(MagickStatusType) CompositeImage(image,composite_image,
5854 draw_info->compose,MagickTrue,geometry.x,geometry.y,exception);
5855 composite_image=DestroyImage(composite_image);
5858 case PointPrimitive:
5866 if ((y < 0) || (y >= (ssize_t) image->rows))
5868 if ((x < 0) || (x >= (ssize_t) image->columns))
5870 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5871 if (q == (Quantum *) NULL)
5873 GetFillColor(draw_info,x,y,&fill_color,exception);
5874 CompositePixelOver(image,&fill_color,(
double) fill_color.alpha,q,(
double)
5875 GetPixelAlpha(image,q),q);
5876 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5883 geometry[MagickPathExtent];
5888 if (primitive_info->text == (
char *) NULL)
5890 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
5891 (void) CloneString(&clone_info->text,primitive_info->text);
5892 (void) FormatLocaleString(geometry,MagickPathExtent,
"%+f%+f",
5893 primitive_info->point.x,primitive_info->point.y);
5894 (void) CloneString(&clone_info->geometry,geometry);
5895 status&=(MagickStatusType) AnnotateImage(image,clone_info,exception);
5896 clone_info=DestroyDrawInfo(clone_info);
5908 if (IsEventLogging() != MagickFalse)
5909 LogPrimitiveInfo(primitive_info);
5910 scale=ExpandAffine(&draw_info->affine);
5911 if ((draw_info->dash_pattern != (
double *) NULL) &&
5912 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5913 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5914 (draw_info->stroke.alpha != (
double) TransparentAlpha))
5919 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
5920 clone_info->stroke_width=0.0;
5921 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5922 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5923 primitive_info,exception);
5924 clone_info=DestroyDrawInfo(clone_info);
5925 if (status != MagickFalse)
5926 status&=(MagickStatusType) DrawDashPolygon(draw_info,primitive_info,
5930 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5932 ((draw_info->stroke.alpha != (
double) TransparentAlpha) ||
5933 (draw_info->stroke_pattern != (
Image *) NULL)))
5945 closed_path=primitive_info[0].closed_subpath;
5946 i=(ssize_t) primitive_info[0].coordinates;
5947 point_x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5948 point_y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5949 if ((point_x < MagickEpsilon) && (point_y < MagickEpsilon))
5950 closed_path=MagickTrue;
5951 if ((((draw_info->linecap == RoundCap) ||
5952 (closed_path != MagickFalse)) &&
5953 (draw_info->linejoin == RoundJoin)) ||
5954 (primitive_info[i].primitive != UndefinedPrimitive))
5956 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5957 primitive_info,exception);
5960 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
5961 clone_info->stroke_width=0.0;
5962 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5963 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5964 primitive_info,exception);
5965 clone_info=DestroyDrawInfo(clone_info);
5966 if (status != MagickFalse)
5967 status&=(MagickStatusType) DrawStrokePolygon(image,draw_info,
5968 primitive_info,exception);
5971 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5972 primitive_info,exception);
5976 image_view=DestroyCacheView(image_view);
5977 if (draw_info->compliance == SVGCompliance)
5979 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5980 (
Image *) NULL,exception);
5981 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5982 (
Image *) NULL,exception);
5984 if (draw_info->debug != MagickFalse)
5985 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-primitive");
5986 return(status != 0 ? MagickTrue : MagickFalse);
6019static MagickBooleanType DrawRoundLinecap(
Image *image,
6029 if (primitive_info->coordinates < 1)
6030 return(MagickFalse);
6031 for (i=0; i < 4; i++)
6032 linecap[i]=(*primitive_info);
6033 linecap[0].coordinates=4;
6034 linecap[1].point.x+=2.0*MagickEpsilon;
6035 linecap[2].point.x+=2.0*MagickEpsilon;
6036 linecap[2].point.y+=2.0*MagickEpsilon;
6037 linecap[3].point.y+=2.0*MagickEpsilon;
6038 linecap[4].primitive=UndefinedPrimitive;
6039 return(DrawPolygonPrimitive(image,draw_info,linecap,exception));
6042static MagickBooleanType DrawStrokePolygon(
Image *image,
6065 if (draw_info->debug != MagickFalse)
6066 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
6067 " begin draw-stroke-polygon");
6068 clone_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
6069 clone_info->fill=draw_info->stroke;
6070 if (clone_info->fill_pattern != (
Image *) NULL)
6071 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
6072 if (clone_info->stroke_pattern != (
Image *) NULL)
6073 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
6074 MagickTrue,exception);
6075 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
6076 clone_info->stroke_width=0.0;
6077 clone_info->fill_rule=NonZeroRule;
6079 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
6081 if (p->coordinates == 1)
6083 stroke_polygon=TraceStrokePolygon(draw_info,p,exception);
6089 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
6090 stroke_polygon,exception);
6091 stroke_polygon=(
PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
6094 q=p+p->coordinates-1;
6095 closed_path=p->closed_subpath;
6096 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
6098 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,p,
6100 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,q,
6104 clone_info=DestroyDrawInfo(clone_info);
6105 if (draw_info->debug != MagickFalse)
6106 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
6107 " end draw-stroke-polygon");
6108 return(status != 0 ? MagickTrue : MagickFalse);
6134MagickExport
void GetAffineMatrix(
AffineMatrix *affine_matrix)
6136 if (IsEventLogging() != MagickFalse)
6137 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6139 (void) memset(affine_matrix,0,
sizeof(*affine_matrix));
6140 affine_matrix->sx=1.0;
6141 affine_matrix->sy=1.0;
6182 assert(draw_info != (
DrawInfo *) NULL);
6183 if (IsEventLogging() != MagickFalse)
6184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6185 (void) memset(draw_info,0,
sizeof(*draw_info));
6186 draw_info->image_info=CloneImageInfo(image_info);
6187 GetAffineMatrix(&draw_info->affine);
6188 exception=AcquireExceptionInfo();
6189 (void) QueryColorCompliance(
"#000F",AllCompliance,&draw_info->fill,
6191 (void) QueryColorCompliance(
"#FFF0",AllCompliance,&draw_info->stroke,
6193 draw_info->stroke_antialias=draw_info->image_info->antialias;
6194 draw_info->stroke_width=1.0;
6195 draw_info->fill_rule=EvenOddRule;
6196 draw_info->alpha=OpaqueAlpha;
6197 draw_info->fill_alpha=OpaqueAlpha;
6198 draw_info->stroke_alpha=OpaqueAlpha;
6199 draw_info->linecap=ButtCap;
6200 draw_info->linejoin=MiterJoin;
6201 draw_info->miterlimit=10;
6202 draw_info->decorate=NoDecoration;
6203 draw_info->pointsize=12.0;
6204 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
6205 draw_info->compose=OverCompositeOp;
6206 draw_info->render=MagickTrue;
6207 draw_info->clip_path=MagickFalse;
6208 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
6209 MagickTrue : MagickFalse;
6210 draw_info->macro_expansion=(
size_t *) NULL;
6211 if (draw_info->image_info->font != (
char *) NULL)
6212 draw_info->font=AcquireString(draw_info->image_info->font);
6213 if (draw_info->image_info->density != (
char *) NULL)
6214 draw_info->density=AcquireString(draw_info->image_info->density);
6215 draw_info->text_antialias=draw_info->image_info->antialias;
6216 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
6217 draw_info->pointsize=draw_info->image_info->pointsize;
6218 draw_info->border_color=draw_info->image_info->border_color;
6219 if (draw_info->image_info->server_name != (
char *) NULL)
6220 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
6221 option=GetImageOption(draw_info->image_info,
"direction");
6222 if (option != (
const char *) NULL)
6223 draw_info->direction=(DirectionType) ParseCommandOption(
6224 MagickDirectionOptions,MagickFalse,option);
6226 draw_info->direction=UndefinedDirection;
6227 option=GetImageOption(draw_info->image_info,
"encoding");
6228 if (option != (
const char *) NULL)
6229 (void) CloneString(&draw_info->encoding,option);
6230 option=GetImageOption(draw_info->image_info,
"family");
6231 if (option != (
const char *) NULL)
6232 (void) CloneString(&draw_info->family,option);
6233 option=GetImageOption(draw_info->image_info,
"fill");
6234 if (option != (
const char *) NULL)
6235 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
6237 option=GetImageOption(draw_info->image_info,
"gravity");
6238 if (option != (
const char *) NULL)
6239 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
6240 MagickFalse,option);
6241 option=GetImageOption(draw_info->image_info,
"interline-spacing");
6242 if (option != (
const char *) NULL)
6243 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6244 option=GetImageOption(draw_info->image_info,
"interword-spacing");
6245 if (option != (
const char *) NULL)
6246 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6247 option=GetImageOption(draw_info->image_info,
"kerning");
6248 if (option != (
const char *) NULL)
6249 draw_info->kerning=GetDrawValue(option,&next_token);
6250 option=GetImageOption(draw_info->image_info,
"stroke");
6251 if (option != (
const char *) NULL)
6252 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
6254 option=GetImageOption(draw_info->image_info,
"strokewidth");
6255 if (option != (
const char *) NULL)
6256 draw_info->stroke_width=GetDrawValue(option,&next_token);
6257 option=GetImageOption(draw_info->image_info,
"style");
6258 if (option != (
const char *) NULL)
6259 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6260 MagickFalse,option);
6261 option=GetImageOption(draw_info->image_info,
"undercolor");
6262 if (option != (
const char *) NULL)
6263 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
6265 option=GetImageOption(draw_info->image_info,
"weight");
6266 if (option != (
const char *) NULL)
6271 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6273 weight=(ssize_t) StringToUnsignedLong(option);
6274 draw_info->weight=(size_t) weight;
6276 option=GetImageOption(draw_info->image_info,
"word-break");
6277 if (option != (
const char *) NULL)
6278 draw_info->word_break=(WordBreakType) ParseCommandOption(
6279 MagickWordBreakOptions,MagickFalse,option);
6280 exception=DestroyExceptionInfo(exception);
6281 draw_info->signature=MagickCoreSignature;
6309static inline double Permutate(
const ssize_t n,
const ssize_t k)
6318 for (i=k+1; i <= n; i++)
6320 for (i=1; i <= (n-k); i++)
6348 center.x=0.5*(end.x+start.x);
6349 center.y=0.5*(end.y+start.y);
6350 radius.x=fabs(center.x-start.x);
6351 radius.y=fabs(center.y-start.y);
6352 return(TraceEllipse(mvg_info,center,radius,degrees));
6355static MagickBooleanType TraceArcPath(
MVGInfo *mvg_info,
const PointInfo start,
6357 const MagickBooleanType large_arc,
const MagickBooleanType sweep)
6394 offset=mvg_info->offset;
6395 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6396 primitive_info->coordinates=0;
6397 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6398 (fabs(start.y-end.y) < MagickEpsilon))
6399 return(TracePoint(primitive_info,end));
6400 radii.x=fabs(arc.x);
6401 radii.y=fabs(arc.y);
6402 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6403 return(TraceLine(primitive_info,start,end));
6404 cosine=cos(DegreesToRadians(fmod((
double) angle,360.0)));
6405 sine=sin(DegreesToRadians(fmod((
double) angle,360.0)));
6406 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6407 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6408 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6410 if (delta < MagickEpsilon)
6411 return(TraceLine(primitive_info,start,end));
6414 radii.x*=sqrt((
double) delta);
6415 radii.y*=sqrt((
double) delta);
6417 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6418 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6419 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6420 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6421 alpha=points[1].x-points[0].x;
6422 beta=points[1].y-points[0].y;
6423 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6424 return(TraceLine(primitive_info,start,end));
6425 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6430 factor=sqrt((
double) factor);
6431 if (sweep == large_arc)
6434 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6435 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6436 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6437 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6438 if ((theta < 0.0) && (sweep != MagickFalse))
6439 theta+=2.0*MagickPI;
6441 if ((theta > 0.0) && (sweep == MagickFalse))
6442 theta-=2.0*MagickPI;
6443 arc_segments=(size_t) CastDoubleToSsizeT(ceil(fabs((
double) (theta/(0.5*
6444 MagickPI+MagickEpsilon)))));
6447 for (i=0; i < (ssize_t) arc_segments; i++)
6449 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6450 gamma=(8.0/3.0)*sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))*
6451 sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))/
6452 sin(fmod((
double) beta,DegreesToRadians(360.0)));
6453 points[0].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) i*theta/
6454 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((
double) (alpha+
6455 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6456 points[0].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) i*theta/
6457 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((
double) (alpha+
6458 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6459 points[2].x=(double) (center.x+cos(fmod((
double) (alpha+(double) (i+1)*
6460 theta/arc_segments),DegreesToRadians(360.0))));
6461 points[2].y=(double) (center.y+sin(fmod((
double) (alpha+(double) (i+1)*
6462 theta/arc_segments),DegreesToRadians(360.0))));
6463 points[1].x=(double) (points[2].x+gamma*sin(fmod((
double) (alpha+(double)
6464 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6465 points[1].y=(double) (points[2].y-gamma*cos(fmod((
double) (alpha+(double)
6466 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6467 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6468 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6469 (p+1)->point.x=(
double) (cosine*radii.x*points[0].x-sine*radii.y*
6471 (p+1)->point.y=(
double) (sine*radii.x*points[0].x+cosine*radii.y*
6473 (p+2)->point.x=(
double) (cosine*radii.x*points[1].x-sine*radii.y*
6475 (p+2)->point.y=(
double) (sine*radii.x*points[1].x+cosine*radii.y*
6477 (p+3)->point.x=(
double) (cosine*radii.x*points[2].x-sine*radii.y*
6479 (p+3)->point.y=(
double) (sine*radii.x*points[2].x+cosine*radii.y*
6481 if (i == (ssize_t) (arc_segments-1))
6483 status&=(MagickStatusType) TraceBezier(mvg_info,4);
6486 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6487 mvg_info->offset+=(ssize_t) p->coordinates;
6488 p+=(ptrdiff_t) p->coordinates;
6491 return(MagickFalse);
6492 mvg_info->offset=offset;
6493 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6494 primitive_info->coordinates=(size_t) (p-primitive_info);
6495 primitive_info->closed_subpath=MagickFalse;
6496 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6498 p->primitive=primitive_info->primitive;
6504static MagickBooleanType TraceBezier(
MVGInfo *mvg_info,
6505 const size_t number_coordinates)
6534 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6535 quantum=number_coordinates;
6536 for (i=0; i < (ssize_t) number_coordinates; i++)
6538 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6540 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6541 if (alpha > (
double) GetMaxMemoryRequest())
6543 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6544 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6545 return(MagickFalse);
6547 if (alpha > (
double) quantum)
6548 quantum=(
size_t) alpha;
6549 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6550 if (alpha > (
double) quantum)
6551 quantum=(size_t) alpha;
6554 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6555 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6556 if (quantum > (
double) GetMaxMemoryRequest())
6558 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6559 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6560 return(MagickFalse);
6562 coefficients=(
double *) AcquireQuantumMemory(number_coordinates,
6563 sizeof(*coefficients));
6564 points=(
PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6566 if ((coefficients == (
double *) NULL) || (points == (
PointInfo *) NULL))
6569 points=(
PointInfo *) RelinquishMagickMemory(points);
6570 if (coefficients != (
double *) NULL)
6571 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6572 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6573 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6574 return(MagickFalse);
6576 control_points=quantum*number_coordinates;
6577 if (CheckPrimitiveExtent(mvg_info,(
double) control_points+1) == MagickFalse)
6579 points=(
PointInfo *) RelinquishMagickMemory(points);
6580 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6581 return(MagickFalse);
6583 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6587 end=primitive_info[number_coordinates-1].point;
6588 for (i=0; i < (ssize_t) number_coordinates; i++)
6589 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6591 for (i=0; i < (ssize_t) control_points; i++)
6596 alpha=pow((
double) (1.0-weight),(
double) number_coordinates-1.0);
6597 for (j=0; j < (ssize_t) number_coordinates; j++)
6599 point.x+=alpha*coefficients[j]*p->point.x;
6600 point.y+=alpha*coefficients[j]*p->point.y;
6601 alpha*=weight/(1.0-weight);
6605 weight+=1.0/control_points;
6611 for (i=0; i < (ssize_t) control_points; i++)
6613 if (TracePoint(p,points[i]) == MagickFalse)
6615 points=(
PointInfo *) RelinquishMagickMemory(points);
6616 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6617 return(MagickFalse);
6619 p+=(ptrdiff_t) p->coordinates;
6621 if (TracePoint(p,end) == MagickFalse)
6623 points=(
PointInfo *) RelinquishMagickMemory(points);
6624 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6625 return(MagickFalse);
6627 p+=(ptrdiff_t) p->coordinates;
6628 primitive_info->coordinates=(size_t) (p-primitive_info);
6629 primitive_info->closed_subpath=MagickFalse;
6630 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6632 p->primitive=primitive_info->primitive;
6635 points=(
PointInfo *) RelinquishMagickMemory(points);
6636 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6640static MagickBooleanType TraceCircle(
MVGInfo *mvg_info,
const PointInfo start,
6652 alpha=end.x-start.x;
6654 radius=hypot((
double) alpha,(
double) beta);
6655 offset.x=(double) radius;
6656 offset.y=(double) radius;
6659 return(TraceEllipse(mvg_info,start,offset,degrees));
6662static MagickBooleanType TraceEllipse(
MVGInfo *mvg_info,
const PointInfo center,
6688 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6689 primitive_info->coordinates=0;
6690 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6692 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6693 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6694 angle.x=DegreesToRadians(arc.x);
6698 angle.y=DegreesToRadians(y);
6699 coordinates=ceil((angle.y-angle.x)/step+1.0);
6700 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6701 return(MagickFalse);
6703 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6704 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6706 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6707 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6708 if (i++ >= (ssize_t) coordinates)
6710 if (TracePoint(p,point) == MagickFalse)
6711 return(MagickFalse);
6712 p+=(ptrdiff_t) p->coordinates;
6714 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6715 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6716 if (TracePoint(p,point) == MagickFalse)
6717 return(MagickFalse);
6718 p+=(ptrdiff_t) p->coordinates;
6719 primitive_info->coordinates=(size_t) (p-primitive_info);
6720 primitive_info->closed_subpath=MagickFalse;
6721 x=fabs(primitive_info[0].point.x-
6722 primitive_info[primitive_info->coordinates-1].point.x);
6723 y=fabs(primitive_info[0].point.y-
6724 primitive_info[primitive_info->coordinates-1].point.y);
6725 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6726 primitive_info->closed_subpath=MagickTrue;
6727 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6729 p->primitive=primitive_info->primitive;
6735static MagickBooleanType TraceLine(
PrimitiveInfo *primitive_info,
6738 if (TracePoint(primitive_info,start) == MagickFalse)
6739 return(MagickFalse);
6740 if (TracePoint(primitive_info+1,end) == MagickFalse)
6741 return(MagickFalse);
6742 (primitive_info+1)->primitive=primitive_info->primitive;
6743 primitive_info->coordinates=2;
6744 primitive_info->closed_subpath=MagickFalse;
6748static ssize_t TracePath(
MVGInfo *mvg_info,
const char *path,
6753 token[MagickPathExtent] =
"";
6771 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6794 subpath_offset=mvg_info->offset;
6795 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6798 number_coordinates=0;
6800 primitive_type=primitive_info->primitive;
6802 for (p=path; *p !=
'\0'; )
6804 if (status == MagickFalse)
6806 while (isspace((
int) ((
unsigned char) *p)) != 0)
6810 last_attribute=attribute;
6811 attribute=(int) (*p++);
6821 large_arc = MagickFalse,
6822 sweep = MagickFalse;
6832 (void) GetNextToken(p,&p,MagickPathExtent,token);
6833 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6834 ThrowPointExpectedException(token,exception);
6836 (void) GetNextToken(p,&p,MagickPathExtent,token);
6837 arc.x=GetDrawValue(token,&next_token);
6838 if (token == next_token)
6839 ThrowPointExpectedException(token,exception);
6840 (void) GetNextToken(p,&p,MagickPathExtent,token);
6841 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6842 ThrowPointExpectedException(token,exception);
6844 (void) GetNextToken(p,&p,MagickPathExtent,token);
6845 arc.y=GetDrawValue(token,&next_token);
6846 if (token == next_token)
6847 ThrowPointExpectedException(token,exception);
6848 (void) GetNextToken(p,&p,MagickPathExtent,token);
6849 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6850 ThrowPointExpectedException(token,exception);
6852 (void) GetNextToken(p,&p,MagickPathExtent,token);
6853 angle=GetDrawValue(token,&next_token);
6854 if (token == next_token)
6855 ThrowPointExpectedException(token,exception);
6856 (void) GetNextToken(p,&p,MagickPathExtent,token);
6857 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6858 ThrowPointExpectedException(token,exception);
6860 (void) GetNextToken(p,&p,MagickPathExtent,token);
6861 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6862 (void) GetNextToken(p,&p,MagickPathExtent,token);
6863 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6864 ThrowPointExpectedException(token,exception);
6866 (void) GetNextToken(p,&p,MagickPathExtent,token);
6867 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6868 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6869 ThrowPointExpectedException(token,exception);
6871 (void) GetNextToken(p,&p,MagickPathExtent,token);
6872 (void) GetNextToken(p,&p,MagickPathExtent,token);
6873 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6874 ThrowPointExpectedException(token,exception);
6876 (void) GetNextToken(p,&p,MagickPathExtent,token);
6877 x=GetDrawValue(token,&next_token);
6878 if (token == next_token)
6879 ThrowPointExpectedException(token,exception);
6880 (void) GetNextToken(p,&p,MagickPathExtent,token);
6881 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6882 ThrowPointExpectedException(token,exception);
6884 (void) GetNextToken(p,&p,MagickPathExtent,token);
6885 y=GetDrawValue(token,&next_token);
6886 if (token == next_token)
6887 ThrowPointExpectedException(token,exception);
6888 end.x=(double) (attribute == (
int)
'A' ? x : point.x+x);
6889 end.y=(double) (attribute == (
int)
'A' ? y : point.y+y);
6890 if (TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep) == MagickFalse)
6892 q=(*mvg_info->primitive_info)+mvg_info->offset;
6893 mvg_info->offset+=(ssize_t) q->coordinates;
6894 q+=(ptrdiff_t) q->coordinates;
6896 while (isspace((
int) ((
unsigned char) *p)) != 0)
6900 }
while (IsValidPoint(p) != MagickFalse);
6912 for (i=1; i < 4; i++)
6914 (void) GetNextToken(p,&p,MagickPathExtent,token);
6915 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6916 ThrowPointExpectedException(token,exception);
6918 (void) GetNextToken(p,&p,MagickPathExtent,token);
6919 x=GetDrawValue(token,&next_token);
6920 if (token == next_token)
6921 ThrowPointExpectedException(token,exception);
6922 (void) GetNextToken(p,&p,MagickPathExtent,token);
6923 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6924 ThrowPointExpectedException(token,exception);
6926 (void) GetNextToken(p,&p,MagickPathExtent,token);
6927 y=GetDrawValue(token,&next_token);
6928 if (token == next_token)
6929 ThrowPointExpectedException(token,exception);
6930 end.x=(double) (attribute == (
int)
'C' ? x : point.x+x);
6931 end.y=(double) (attribute == (
int)
'C' ? y : point.y+y);
6934 for (i=0; i < 4; i++)
6935 (q+i)->point=points[i];
6936 if (TraceBezier(mvg_info,4) == MagickFalse)
6938 q=(*mvg_info->primitive_info)+mvg_info->offset;
6939 mvg_info->offset+=(ssize_t) q->coordinates;
6940 q+=(ptrdiff_t) q->coordinates;
6942 while (isspace((
int) ((
unsigned char) *p)) != 0)
6946 }
while (IsValidPoint(p) != MagickFalse);
6954 (void) GetNextToken(p,&p,MagickPathExtent,token);
6955 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6956 ThrowPointExpectedException(token,exception);
6958 (void) GetNextToken(p,&p,MagickPathExtent,token);
6959 x=GetDrawValue(token,&next_token);
6960 if (token == next_token)
6961 ThrowPointExpectedException(token,exception);
6962 point.x=(double) (attribute == (
int)
'H' ? x: point.x+x);
6963 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6965 q=(*mvg_info->primitive_info)+mvg_info->offset;
6966 if (TracePoint(q,point) == MagickFalse)
6968 mvg_info->offset+=(ssize_t) q->coordinates;
6969 q+=(ptrdiff_t) q->coordinates;
6970 while (isspace((
int) ((
unsigned char) *p)) != 0)
6974 }
while (IsValidPoint(p) != MagickFalse);
6985 (void) GetNextToken(p,&p,MagickPathExtent,token);
6986 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6987 ThrowPointExpectedException(token,exception);
6989 (void) GetNextToken(p,&p,MagickPathExtent,token);
6990 x=GetDrawValue(token,&next_token);
6991 if (token == next_token)
6992 ThrowPointExpectedException(token,exception);
6993 (void) GetNextToken(p,&p,MagickPathExtent,token);
6994 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6995 ThrowPointExpectedException(token,exception);
6997 (void) GetNextToken(p,&p,MagickPathExtent,token);
6998 y=GetDrawValue(token,&next_token);
6999 if (token == next_token)
7000 ThrowPointExpectedException(token,exception);
7001 point.x=(double) (attribute == (
int)
'L' ? x : point.x+x);
7002 point.y=(double) (attribute == (
int)
'L' ? y : point.y+y);
7003 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7005 q=(*mvg_info->primitive_info)+mvg_info->offset;
7006 if (TracePoint(q,point) == MagickFalse)
7008 mvg_info->offset+=(ssize_t) q->coordinates;
7009 q+=(ptrdiff_t) q->coordinates;
7010 while (isspace((
int) ((
unsigned char) *p)) != 0)
7014 }
while (IsValidPoint(p) != MagickFalse);
7023 if (mvg_info->offset != subpath_offset)
7025 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7026 primitive_info->coordinates=(size_t) (q-primitive_info);
7027 number_coordinates+=primitive_info->coordinates;
7029 subpath_offset=mvg_info->offset;
7034 (void) GetNextToken(p,&p,MagickPathExtent,token);
7035 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7036 ThrowPointExpectedException(token,exception);
7038 (void) GetNextToken(p,&p,MagickPathExtent,token);
7039 x=GetDrawValue(token,&next_token);
7040 if (token == next_token)
7041 ThrowPointExpectedException(token,exception);
7042 (void) GetNextToken(p,&p,MagickPathExtent,token);
7043 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7044 ThrowPointExpectedException(token,exception);
7046 (void) GetNextToken(p,&p,MagickPathExtent,token);
7047 y=GetDrawValue(token,&next_token);
7048 if (token == next_token)
7049 ThrowPointExpectedException(token,exception);
7050 point.x=(double) (attribute == (
int)
'M' ? x : point.x+x);
7051 point.y=(double) (attribute == (
int)
'M' ? y : point.y+y);
7055 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7057 q=(*mvg_info->primitive_info)+mvg_info->offset;
7058 if (TracePoint(q,point) == MagickFalse)
7060 mvg_info->offset+=(ssize_t) q->coordinates;
7061 q+=(ptrdiff_t) q->coordinates;
7062 while (isspace((
int) ((
unsigned char) *p)) != 0)
7066 }
while (IsValidPoint(p) != MagickFalse);
7078 for (i=1; i < 3; i++)
7080 (void) GetNextToken(p,&p,MagickPathExtent,token);
7081 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7082 ThrowPointExpectedException(token,exception);
7084 (void) GetNextToken(p,&p,MagickPathExtent,token);
7085 x=GetDrawValue(token,&next_token);
7086 if (token == next_token)
7087 ThrowPointExpectedException(token,exception);
7088 (void) GetNextToken(p,&p,MagickPathExtent,token);
7089 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7090 ThrowPointExpectedException(token,exception);
7092 (void) GetNextToken(p,&p,MagickPathExtent,token);
7093 y=GetDrawValue(token,&next_token);
7094 if (token == next_token)
7095 ThrowPointExpectedException(token,exception);
7098 end.x=(double) (attribute == (
int)
'Q' ? x : point.x+x);
7099 end.y=(double) (attribute == (
int)
'Q' ? y : point.y+y);
7102 for (i=0; i < 3; i++)
7103 (q+i)->point=points[i];
7104 if (TraceBezier(mvg_info,3) == MagickFalse)
7106 q=(*mvg_info->primitive_info)+mvg_info->offset;
7107 mvg_info->offset+=(ssize_t) q->coordinates;
7108 q+=(ptrdiff_t) q->coordinates;
7110 while (isspace((
int) ((
unsigned char) *p)) != 0)
7114 }
while (IsValidPoint(p) != MagickFalse);
7125 points[0]=points[3];
7126 points[1].x=2.0*points[3].x-points[2].x;
7127 points[1].y=2.0*points[3].y-points[2].y;
7128 for (i=2; i < 4; i++)
7130 (void) GetNextToken(p,&p,MagickPathExtent,token);
7131 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7132 ThrowPointExpectedException(token,exception);
7134 (void) GetNextToken(p,&p,MagickPathExtent,token);
7135 x=GetDrawValue(token,&next_token);
7136 if (token == next_token)
7137 ThrowPointExpectedException(token,exception);
7138 (void) GetNextToken(p,&p,MagickPathExtent,token);
7139 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7140 ThrowPointExpectedException(token,exception);
7142 (void) GetNextToken(p,&p,MagickPathExtent,token);
7143 y=GetDrawValue(token,&next_token);
7144 if (token == next_token)
7145 ThrowPointExpectedException(token,exception);
7148 end.x=(double) (attribute == (
int)
'S' ? x : point.x+x);
7149 end.y=(double) (attribute == (
int)
'S' ? y : point.y+y);
7152 if (strchr(
"CcSs",last_attribute) == (
char *) NULL)
7157 for (i=0; i < 4; i++)
7158 (q+i)->point=points[i];
7159 if (TraceBezier(mvg_info,4) == MagickFalse)
7161 q=(*mvg_info->primitive_info)+mvg_info->offset;
7162 mvg_info->offset+=(ssize_t) q->coordinates;
7163 q+=(ptrdiff_t) q->coordinates;
7165 last_attribute=attribute;
7166 while (isspace((
int) ((
unsigned char) *p)) != 0)
7170 }
while (IsValidPoint(p) != MagickFalse);
7181 points[0]=points[2];
7182 points[1].x=2.0*points[2].x-points[1].x;
7183 points[1].y=2.0*points[2].y-points[1].y;
7184 for (i=2; i < 3; i++)
7186 (void) GetNextToken(p,&p,MagickPathExtent,token);
7187 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7188 ThrowPointExpectedException(token,exception);
7190 (void) GetNextToken(p,&p,MagickPathExtent,token);
7191 x=GetDrawValue(token,&next_token);
7192 if (token == next_token)
7193 ThrowPointExpectedException(token,exception);
7194 (void) GetNextToken(p,&p,MagickPathExtent,token);
7195 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7196 ThrowPointExpectedException(token,exception);
7198 (void) GetNextToken(p,&p,MagickPathExtent,token);
7199 y=GetDrawValue(token,&next_token);
7200 if (token == next_token)
7201 ThrowPointExpectedException(token,exception);
7202 end.x=(double) (attribute == (
int)
'T' ? x : point.x+x);
7203 end.y=(double) (attribute == (
int)
'T' ? y : point.y+y);
7206 if (status == MagickFalse)
7208 if (strchr(
"QqTt",last_attribute) == (
char *) NULL)
7213 for (i=0; i < 3; i++)
7214 (q+i)->point=points[i];
7215 if (TraceBezier(mvg_info,3) == MagickFalse)
7217 q=(*mvg_info->primitive_info)+mvg_info->offset;
7218 mvg_info->offset+=(ssize_t) q->coordinates;
7219 q+=(ptrdiff_t) q->coordinates;
7221 last_attribute=attribute;
7222 while (isspace((
int) ((
unsigned char) *p)) != 0)
7226 }
while (IsValidPoint(p) != MagickFalse);
7237 (void) GetNextToken(p,&p,MagickPathExtent,token);
7238 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7239 ThrowPointExpectedException(token,exception);
7241 (void) GetNextToken(p,&p,MagickPathExtent,token);
7242 y=GetDrawValue(token,&next_token);
7243 if (token == next_token)
7244 ThrowPointExpectedException(token,exception);
7245 point.y=(double) (attribute == (
int)
'V' ? y : point.y+y);
7246 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7248 q=(*mvg_info->primitive_info)+mvg_info->offset;
7249 if (TracePoint(q,point) == MagickFalse)
7251 mvg_info->offset+=(ssize_t) q->coordinates;
7252 q+=(ptrdiff_t) q->coordinates;
7253 while (isspace((
int) ((
unsigned char) *p)) != 0)
7257 }
while (IsValidPoint(p) != MagickFalse);
7267 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7269 q=(*mvg_info->primitive_info)+mvg_info->offset;
7270 if (TracePoint(q,point) == MagickFalse)
7272 mvg_info->offset+=(ssize_t) q->coordinates;
7273 q+=(ptrdiff_t) q->coordinates;
7274 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7275 primitive_info->coordinates=(size_t) (q-primitive_info);
7276 primitive_info->closed_subpath=MagickTrue;
7277 number_coordinates+=primitive_info->coordinates;
7279 subpath_offset=mvg_info->offset;
7285 ThrowPointExpectedException(token,exception);
7290 if (status == MagickFalse)
7292 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7293 primitive_info->coordinates=(size_t) (q-primitive_info);
7294 number_coordinates+=primitive_info->coordinates;
7295 for (i=0; i < (ssize_t) number_coordinates; i++)
7298 q->primitive=primitive_type;
7300 q->method=FillToBorderMethod;
7303 return((ssize_t) number_coordinates);
7306static MagickBooleanType TraceRectangle(
PrimitiveInfo *primitive_info,
7319 if (TracePoint(p,start) == MagickFalse)
7320 return(MagickFalse);
7321 p+=(ptrdiff_t) p->coordinates;
7324 if (TracePoint(p,point) == MagickFalse)
7325 return(MagickFalse);
7326 p+=(ptrdiff_t) p->coordinates;
7327 if (TracePoint(p,end) == MagickFalse)
7328 return(MagickFalse);
7329 p+=(ptrdiff_t) p->coordinates;
7332 if (TracePoint(p,point) == MagickFalse)
7333 return(MagickFalse);
7334 p+=(ptrdiff_t) p->coordinates;
7335 if (TracePoint(p,start) == MagickFalse)
7336 return(MagickFalse);
7337 p+=(ptrdiff_t) p->coordinates;
7338 primitive_info->coordinates=(size_t) (p-primitive_info);
7339 primitive_info->closed_subpath=MagickTrue;
7340 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7342 p->primitive=primitive_info->primitive;
7348static MagickBooleanType TraceRoundRectangle(
MVGInfo *mvg_info,
7368 offset=mvg_info->offset;
7369 segment.x=fabs(end.x-start.x);
7370 segment.y=fabs(end.y-start.y);
7371 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7373 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7376 if (arc.x > (0.5*segment.x))
7377 arc.x=0.5*segment.x;
7378 if (arc.y > (0.5*segment.y))
7379 arc.y=0.5*segment.y;
7380 point.x=start.x+segment.x-arc.x;
7381 point.y=start.y+arc.y;
7384 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7385 return(MagickFalse);
7386 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7387 mvg_info->offset+=(ssize_t) p->coordinates;
7388 point.x=start.x+segment.x-arc.x;
7389 point.y=start.y+segment.y-arc.y;
7392 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7393 return(MagickFalse);
7394 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7395 mvg_info->offset+=(ssize_t) p->coordinates;
7396 point.x=start.x+arc.x;
7397 point.y=start.y+segment.y-arc.y;
7400 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7401 return(MagickFalse);
7402 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7403 mvg_info->offset+=(ssize_t) p->coordinates;
7404 point.x=start.x+arc.x;
7405 point.y=start.y+arc.y;
7408 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7409 return(MagickFalse);
7410 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7411 mvg_info->offset+=(ssize_t) p->coordinates;
7412 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7413 return(MagickFalse);
7414 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7415 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7416 return(MagickFalse);
7417 p+=(ptrdiff_t) p->coordinates;
7418 mvg_info->offset=offset;
7419 primitive_info=(*mvg_info->primitive_info)+offset;
7420 primitive_info->coordinates=(size_t) (p-primitive_info);
7421 primitive_info->closed_subpath=MagickTrue;
7422 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7424 p->primitive=primitive_info->primitive;
7430static MagickBooleanType TraceSquareLinecap(
PrimitiveInfo *primitive_info,
7431 const size_t number_vertices,
const double offset)
7448 for (i=1; i < (ssize_t) number_vertices; i++)
7450 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7451 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7452 if ((fabs((
double) dx) >= MagickEpsilon) ||
7453 (fabs((
double) dy) >= MagickEpsilon))
7456 if (i == (ssize_t) number_vertices)
7457 i=(ssize_t) number_vertices-1L;
7458 distance=hypot((
double) dx,(
double) dy);
7459 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7460 dx*(distance+offset)/distance);
7461 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7462 dy*(distance+offset)/distance);
7463 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7465 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7466 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7467 if ((fabs((
double) dx) >= MagickEpsilon) ||
7468 (fabs((
double) dy) >= MagickEpsilon))
7471 distance=hypot((
double) dx,(
double) dy);
7472 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7473 dx*(distance+offset)/distance);
7474 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7475 dy*(distance+offset)/distance);
7482#define MaxStrokePad (6*BezierQuantum+360)
7483#define CheckPathExtent(pad_p,pad_q) \
7485 if ((pad_p) > MaxBezierCoordinates) \
7486 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7488 if ((p+(ptrdiff_t) (pad_p)) >= (ssize_t) extent_p) \
7490 if (~extent_p < (pad_p)) \
7491 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7494 extent_p+=(pad_p); \
7495 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7496 MaxStrokePad,sizeof(*stroke_p)); \
7499 if ((pad_q) > MaxBezierCoordinates) \
7500 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7502 if ((q+(ptrdiff_t) (pad_q)) >= (ssize_t) extent_q) \
7504 if (~extent_q < (pad_q)) \
7505 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7508 extent_q+=(pad_q); \
7509 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7510 MaxStrokePad,sizeof(*stroke_q)); \
7513 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7515 if (stroke_p != (PointInfo *) NULL) \
7516 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7517 if (stroke_q != (PointInfo *) NULL) \
7518 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7519 polygon_primitive=(PrimitiveInfo *) \
7520 RelinquishMagickMemory(polygon_primitive); \
7521 (void) ThrowMagickException(exception,GetMagickModule(), \
7522 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7523 return((PrimitiveInfo *) NULL); \
7527 typedef struct _StrokeSegment
7573 inverse_slope = {0.0, 0.0},
7580 number_vertices=primitive_info->coordinates;
7581 polygon_primitive=(
PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7582 number_vertices+2UL,
sizeof(*polygon_primitive));
7585 (void) ThrowMagickException(exception,GetMagickModule(),
7586 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7589 (void) memcpy(polygon_primitive,primitive_info,(
size_t) number_vertices*
7590 sizeof(*polygon_primitive));
7591 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7592 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7593 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7594 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7595 if ((draw_info->linejoin == RoundJoin) ||
7596 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
7598 polygon_primitive[number_vertices]=primitive_info[1];
7601 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7605 closed_path=primitive_info[0].closed_subpath;
7608 for (n=1; n < (ssize_t) number_vertices; n++)
7610 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7611 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7612 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7615 if (n == (ssize_t) number_vertices)
7617 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7623 sizeof(*stroke_polygon));
7624 stroke_polygon[0]=polygon_primitive[0];
7625 stroke_polygon[0].coordinates=0;
7628 return(stroke_polygon);
7630 n=(ssize_t) number_vertices-1L;
7632 extent_p=2*number_vertices;
7633 extent_q=2*number_vertices;
7634 stroke_p=(
PointInfo *) AcquireQuantumMemory((
size_t) extent_p+MaxStrokePad,
7636 stroke_q=(
PointInfo *) AcquireQuantumMemory((
size_t) extent_q+MaxStrokePad,
7641 stroke_p=(
PointInfo *) RelinquishMagickMemory(stroke_p);
7643 stroke_q=(
PointInfo *) RelinquishMagickMemory(stroke_q);
7645 RelinquishMagickMemory(polygon_primitive);
7646 (void) ThrowMagickException(exception,GetMagickModule(),
7647 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7651 inverse_slope.p=0.0;
7652 if (fabs(dx.p) < MagickEpsilon)
7655 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7657 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7660 if (fabs(dy.p) < MagickEpsilon)
7663 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7665 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7670 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7672 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7673 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7674 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7675 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7676 offset.x=sqrt((
double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7677 offset.y=(double) (offset.x*inverse_slope.p);
7678 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7680 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7681 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7682 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7683 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7684 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7685 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7686 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7687 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7691 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7692 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7693 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7694 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7695 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7696 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7697 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7698 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7705 stroke_q[p++]=box_q[0];
7706 stroke_p[q++]=box_p[0];
7707 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7712 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7713 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7714 dot_product=dx.q*dx.q+dy.q*dy.q;
7715 if (dot_product < 0.25)
7718 inverse_slope.q=0.0;
7719 if (fabs(dx.q) < MagickEpsilon)
7722 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7724 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7727 if (fabs(dy.q) < MagickEpsilon)
7730 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7732 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7737 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7739 offset.x=sqrt((
double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7740 offset.y=(double) (offset.x*inverse_slope.q);
7741 dot_product=dy.q*offset.x-dx.q*offset.y;
7742 if (dot_product > 0.0)
7744 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7745 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7746 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7747 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7748 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7749 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7750 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7751 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7755 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7756 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7757 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7758 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7759 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7760 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7761 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7762 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7764 if (fabs((
double) (slope.p-slope.q)) < MagickEpsilon)
7771 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7772 box_p[3].y)/(slope.p-slope.q));
7773 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7774 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7775 box_q[3].y)/(slope.p-slope.q));
7776 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7778 DisableMSCWarning(4127)
7779 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7781 dot_product=dx.q*dy.p-dx.p*dy.q;
7782 if (dot_product <= 0.0)
7783 switch (draw_info->linejoin)
7787 stroke_q[q++]=box_q[1];
7788 stroke_q[q++]=box_q[2];
7789 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7790 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7791 if (dot_product <= miterlimit)
7792 stroke_p[p++]=box_p[4];
7795 stroke_p[p++]=box_p[1];
7796 stroke_p[p++]=box_p[2];
7802 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7803 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7804 if (dot_product <= miterlimit)
7806 stroke_q[q++]=box_q[4];
7807 stroke_p[p++]=box_p[4];
7811 stroke_q[q++]=box_q[1];
7812 stroke_q[q++]=box_q[2];
7813 stroke_p[p++]=box_p[1];
7814 stroke_p[p++]=box_p[2];
7820 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7821 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7822 if (dot_product <= miterlimit)
7823 stroke_p[p++]=box_p[4];
7826 stroke_p[p++]=box_p[1];
7827 stroke_p[p++]=box_p[2];
7829 center=polygon_primitive[n].point;
7830 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7831 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7832 if (theta.q < theta.p)
7833 theta.q+=2.0*MagickPI;
7834 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.q-
7835 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7836 DisableMSCWarning(4127)
7837 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7839 stroke_q[q].x=box_q[1].x;
7840 stroke_q[q].y=box_q[1].y;
7842 for (j=1; j < (ssize_t) arc_segments; j++)
7844 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7845 stroke_q[q].x=(double) (center.x+mid*cos(fmod((
double)
7846 (theta.p+delta_theta),DegreesToRadians(360.0))));
7847 stroke_q[q].y=(double) (center.y+mid*sin(fmod((
double)
7848 (theta.p+delta_theta),DegreesToRadians(360.0))));
7851 stroke_q[q++]=box_q[2];
7858 switch (draw_info->linejoin)
7862 stroke_p[p++]=box_p[1];
7863 stroke_p[p++]=box_p[2];
7864 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7865 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7866 if (dot_product <= miterlimit)
7867 stroke_q[q++]=box_q[4];
7870 stroke_q[q++]=box_q[1];
7871 stroke_q[q++]=box_q[2];
7877 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7878 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7879 if (dot_product <= miterlimit)
7881 stroke_q[q++]=box_q[4];
7882 stroke_p[p++]=box_p[4];
7886 stroke_q[q++]=box_q[1];
7887 stroke_q[q++]=box_q[2];
7888 stroke_p[p++]=box_p[1];
7889 stroke_p[p++]=box_p[2];
7895 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7896 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7897 if (dot_product <= miterlimit)
7898 stroke_q[q++]=box_q[4];
7901 stroke_q[q++]=box_q[1];
7902 stroke_q[q++]=box_q[2];
7904 center=polygon_primitive[n].point;
7905 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7906 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7907 if (theta.p < theta.q)
7908 theta.p+=2.0*MagickPI;
7909 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.p-
7910 theta.q)/(2.0*sqrt((
double) (MagickSafeReciprocal(mid)))))));
7911 DisableMSCWarning(4127)
7912 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7914 stroke_p[p++]=box_p[1];
7915 for (j=1; j < (ssize_t) arc_segments; j++)
7917 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7918 stroke_p[p].x=(double) (center.x+mid*cos(fmod((
double)
7919 (theta.p+delta_theta),DegreesToRadians(360.0))));
7920 stroke_p[p].y=(double) (center.y+mid*sin(fmod((
double)
7921 (theta.p+delta_theta),DegreesToRadians(360.0))));
7924 stroke_p[p++]=box_p[2];
7931 inverse_slope.p=inverse_slope.q;
7940 stroke_p[p++]=box_p[1];
7941 stroke_q[q++]=box_q[1];
7945 stroke_polygon=(
PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7946 (p+q+2L),(
size_t) (closed_path+2L)*
sizeof(*stroke_polygon));
7949 (void) ThrowMagickException(exception,GetMagickModule(),
7950 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7951 stroke_p=(
PointInfo *) RelinquishMagickMemory(stroke_p);
7952 stroke_q=(
PointInfo *) RelinquishMagickMemory(stroke_q);
7955 return(stroke_polygon);
7957 for (i=0; i < (ssize_t) p; i++)
7959 stroke_polygon[i]=polygon_primitive[0];
7960 stroke_polygon[i].point=stroke_p[i];
7962 if (closed_path != MagickFalse)
7964 stroke_polygon[i]=polygon_primitive[0];
7965 stroke_polygon[i].point=stroke_polygon[0].point;
7968 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7970 stroke_polygon[i]=polygon_primitive[0];
7971 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7973 if (closed_path != MagickFalse)
7975 stroke_polygon[i]=polygon_primitive[0];
7976 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7979 stroke_polygon[i]=polygon_primitive[0];
7980 stroke_polygon[i].point=stroke_polygon[0].point;
7982 stroke_polygon[i].primitive=UndefinedPrimitive;
7983 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7984 stroke_p=(
PointInfo *) RelinquishMagickMemory(stroke_p);
7985 stroke_q=(
PointInfo *) RelinquishMagickMemory(stroke_q);
7986 polygon_primitive=(
PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7987 return(stroke_polygon);