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 ThrowPointExpectedException(token,exception) \
99 (void) ThrowMagickException(exception,GetMagickModule(),DrawError, \
100 "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
101 status=MagickFalse; \
191 *DrawClippingMask(Image *,
const DrawInfo *,
const char *,
const char *,
194static MagickBooleanType
195 DrawStrokePolygon(Image *,
const DrawInfo *,
const PrimitiveInfo *,
197 RenderMVGContent(Image *,
const DrawInfo *,
const size_t,ExceptionInfo *),
198 TraceArc(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo),
199 TraceArcPath(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo,
200 const double,
const MagickBooleanType,
const MagickBooleanType),
201 TraceBezier(MVGInfo *,
const size_t),
202 TraceCircle(MVGInfo *,
const PointInfo,
const PointInfo),
203 TraceEllipse(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo),
204 TraceLine(PrimitiveInfo *,
const PointInfo,
const PointInfo),
205 TraceRectangle(PrimitiveInfo *,
const PointInfo,
const PointInfo),
206 TraceRoundRectangle(MVGInfo *,
const PointInfo,
const PointInfo,PointInfo),
207 TraceSquareLinecap(PrimitiveInfo *,
const size_t,
const double);
210 *TraceStrokePolygon(
const DrawInfo *,
const PrimitiveInfo *,ExceptionInfo *);
213 TracePath(MVGInfo *,
const char *,ExceptionInfo *);
233MagickExport DrawInfo *AcquireDrawInfo(
void)
238 draw_info=(DrawInfo *) AcquireCriticalMemory(
sizeof(*draw_info));
239 GetDrawInfo((ImageInfo *) NULL,draw_info);
270MagickExport DrawInfo *CloneDrawInfo(
const ImageInfo *image_info,
271 const DrawInfo *draw_info)
279 clone_info=(DrawInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
280 GetDrawInfo(image_info,clone_info);
281 if (draw_info == (DrawInfo *) NULL)
283 exception=AcquireExceptionInfo();
284 if (draw_info->id != (
char *) NULL)
285 (void) CloneString(&clone_info->id,draw_info->id);
286 if (draw_info->primitive != (
char *) NULL)
287 (void) CloneString(&clone_info->primitive,draw_info->primitive);
288 if (draw_info->geometry != (
char *) NULL)
289 (void) CloneString(&clone_info->geometry,draw_info->geometry);
290 clone_info->compliance=draw_info->compliance;
291 clone_info->viewbox=draw_info->viewbox;
292 clone_info->affine=draw_info->affine;
293 clone_info->gravity=draw_info->gravity;
294 clone_info->fill=draw_info->fill;
295 clone_info->stroke=draw_info->stroke;
296 clone_info->stroke_width=draw_info->stroke_width;
297 if (draw_info->fill_pattern != (Image *) NULL)
298 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
300 if (draw_info->stroke_pattern != (Image *) NULL)
301 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
302 MagickTrue,exception);
303 clone_info->stroke_antialias=draw_info->stroke_antialias;
304 clone_info->text_antialias=draw_info->text_antialias;
305 clone_info->fill_rule=draw_info->fill_rule;
306 clone_info->linecap=draw_info->linecap;
307 clone_info->linejoin=draw_info->linejoin;
308 clone_info->miterlimit=draw_info->miterlimit;
309 clone_info->dash_offset=draw_info->dash_offset;
310 clone_info->decorate=draw_info->decorate;
311 clone_info->compose=draw_info->compose;
312 if (draw_info->text != (
char *) NULL)
313 (void) CloneString(&clone_info->text,draw_info->text);
314 if (draw_info->font != (
char *) NULL)
315 (void) CloneString(&clone_info->font,draw_info->font);
316 if (draw_info->metrics != (
char *) NULL)
317 (void) CloneString(&clone_info->metrics,draw_info->metrics);
318 if (draw_info->family != (
char *) NULL)
319 (void) CloneString(&clone_info->family,draw_info->family);
320 clone_info->style=draw_info->style;
321 clone_info->stretch=draw_info->stretch;
322 clone_info->weight=draw_info->weight;
323 if (draw_info->encoding != (
char *) NULL)
324 (void) CloneString(&clone_info->encoding,draw_info->encoding);
325 clone_info->pointsize=draw_info->pointsize;
326 clone_info->kerning=draw_info->kerning;
327 clone_info->interline_spacing=draw_info->interline_spacing;
328 clone_info->interword_spacing=draw_info->interword_spacing;
329 clone_info->direction=draw_info->direction;
330 clone_info->word_break=draw_info->word_break;
331 if (draw_info->density != (
char *) NULL)
332 (void) CloneString(&clone_info->density,draw_info->density);
333 clone_info->align=draw_info->align;
334 clone_info->undercolor=draw_info->undercolor;
335 clone_info->border_color=draw_info->border_color;
336 if (draw_info->server_name != (
char *) NULL)
337 (void) CloneString(&clone_info->server_name,draw_info->server_name);
338 if (draw_info->dash_pattern != (
double *) NULL)
343 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
344 clone_info->dash_pattern=(
double *) AcquireQuantumMemory((
size_t) (x+1),
345 sizeof(*clone_info->dash_pattern));
346 if (clone_info->dash_pattern == (
double *) NULL)
347 ThrowFatalException(ResourceLimitFatalError,
348 "UnableToAllocateDashPattern");
349 (void) memset(clone_info->dash_pattern,0,(
size_t) (x+1)*
350 sizeof(*clone_info->dash_pattern));
351 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(
size_t)
352 x*
sizeof(*clone_info->dash_pattern));
354 clone_info->gradient=draw_info->gradient;
355 if (draw_info->gradient.stops != (StopInfo *) NULL)
360 number_stops=clone_info->gradient.number_stops;
361 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((
size_t)
362 number_stops,
sizeof(*clone_info->gradient.stops));
363 if (clone_info->gradient.stops == (StopInfo *) NULL)
364 ThrowFatalException(ResourceLimitFatalError,
365 "UnableToAllocateDashPattern");
366 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
367 (
size_t) number_stops*
sizeof(*clone_info->gradient.stops));
369 clone_info->bounds=draw_info->bounds;
370 clone_info->fill_alpha=draw_info->fill_alpha;
371 clone_info->stroke_alpha=draw_info->stroke_alpha;
372 clone_info->element_reference=draw_info->element_reference;
373 clone_info->clip_path=draw_info->clip_path;
374 clone_info->clip_units=draw_info->clip_units;
375 if (draw_info->clip_mask != (
char *) NULL)
376 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
377 if (draw_info->clipping_mask != (Image *) NULL)
378 clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
379 MagickTrue,exception);
380 if (draw_info->composite_mask != (Image *) NULL)
381 clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
382 MagickTrue,exception);
383 clone_info->render=draw_info->render;
384 clone_info->debug=draw_info->debug;
385 exception=DestroyExceptionInfo(exception);
420static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
425 if (polygon_info->edges != (EdgeInfo *) NULL)
427 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
428 if (polygon_info->edges[i].points != (PointInfo *) NULL)
429 polygon_info->edges[i].points=(PointInfo *)
430 RelinquishMagickMemory(polygon_info->edges[i].points);
431 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(
432 polygon_info->edges);
434 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
436#if defined(__cplusplus) || defined(c_plusplus)
440static int DrawCompareEdges(
const void *p_edge,
const void *q_edge)
442#define DrawCompareEdge(p,q) \
444 if (((p)-(q)) < 0.0) \
446 if (((p)-(q)) > 0.0) \
457 p=((
const EdgeInfo *) p_edge)->points;
458 q=((
const EdgeInfo *) q_edge)->points;
459 DrawCompareEdge(p[0].y,q[0].y);
460 DrawCompareEdge(p[0].x,q[0].x);
461 DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
463 DrawCompareEdge(p[1].y,q[1].y);
464 DrawCompareEdge(p[1].x,q[1].x);
468#if defined(__cplusplus) || defined(c_plusplus)
472static void LogPolygonInfo(
const PolygonInfo *polygon_info)
481 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin active-edge");
482 p=polygon_info->edges;
483 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
485 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" edge %.20g:",
487 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" direction: %s",
488 p->direction != MagickFalse ?
"down" :
"up");
489 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" ghostline: %s",
490 p->ghostline != MagickFalse ?
"transparent" :
"opaque");
491 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
492 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
493 p->bounds.x2,p->bounds.y2);
494 for (j=0; j < (ssize_t) p->number_points; j++)
495 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %g,%g",
496 p->points[j].x,p->points[j].y);
499 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end active-edge");
502static void ReversePoints(PointInfo *points,
const size_t number_points)
510 for (i=0; i < (number_points >> 1); i++)
513 points[i]=points[number_points-(i+1)];
514 points[number_points-(i+1)]=point;
518static PolygonInfo *ConvertPathToPolygon(
const PathInfo *path_info,
519 ExceptionInfo *exception)
550 polygon_info=(PolygonInfo *) AcquireMagickMemory(
sizeof(*polygon_info));
551 if (polygon_info == (PolygonInfo *) NULL)
553 (void) ThrowMagickException(exception,GetMagickModule(),
554 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
555 return((PolygonInfo *) NULL);
558 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
559 sizeof(*polygon_info->edges));
560 if (polygon_info->edges == (EdgeInfo *) NULL)
562 (void) ThrowMagickException(exception,GetMagickModule(),
563 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
564 return(DestroyPolygonInfo(polygon_info));
566 (void) memset(polygon_info->edges,0,number_edges*
567 sizeof(*polygon_info->edges));
570 ghostline=MagickFalse;
573 points=(PointInfo *) NULL;
574 (void) memset(&point,0,
sizeof(point));
575 (void) memset(&bounds,0,
sizeof(bounds));
576 polygon_info->edges[edge].number_points=(size_t) n;
577 polygon_info->edges[edge].scanline=0.0;
578 polygon_info->edges[edge].highwater=0;
579 polygon_info->edges[edge].ghostline=ghostline;
580 polygon_info->edges[edge].direction=(ssize_t) direction;
581 polygon_info->edges[edge].points=points;
582 polygon_info->edges[edge].bounds=bounds;
583 polygon_info->number_edges=0;
584 for (i=0; path_info[i].code != EndCode; i++)
586 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
587 (path_info[i].code == GhostlineCode))
592 if ((points != (PointInfo *) NULL) && (n >= 2))
594 if (edge == number_edges)
597 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
598 polygon_info->edges,(
size_t) number_edges,
599 sizeof(*polygon_info->edges));
600 if (polygon_info->edges == (EdgeInfo *) NULL)
602 (void) ThrowMagickException(exception,GetMagickModule(),
603 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
604 points=(PointInfo *) RelinquishMagickMemory(points);
605 return(DestroyPolygonInfo(polygon_info));
608 polygon_info->edges[edge].number_points=(size_t) n;
609 polygon_info->edges[edge].scanline=(-1.0);
610 polygon_info->edges[edge].highwater=0;
611 polygon_info->edges[edge].ghostline=ghostline;
612 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
614 ReversePoints(points,(
size_t) n);
615 polygon_info->edges[edge].points=points;
616 polygon_info->edges[edge].bounds=bounds;
617 polygon_info->edges[edge].bounds.y1=points[0].y;
618 polygon_info->edges[edge].bounds.y2=points[n-1].y;
619 points=(PointInfo *) NULL;
620 ghostline=MagickFalse;
622 polygon_info->number_edges=edge;
624 if (points == (PointInfo *) NULL)
627 points=(PointInfo *) AcquireQuantumMemory((
size_t) number_points,
629 if (points == (PointInfo *) NULL)
631 (void) ThrowMagickException(exception,GetMagickModule(),
632 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
633 return(DestroyPolygonInfo(polygon_info));
636 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
637 point=path_info[i].point;
648 next_direction=((path_info[i].point.y > point.y) ||
649 ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
650 (path_info[i].point.x > point.x))) ? 1 : -1;
651 if ((points != (PointInfo *) NULL) && (direction != 0) &&
652 (direction != next_direction))
658 if (edge == number_edges)
661 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
662 polygon_info->edges,(
size_t) number_edges,
663 sizeof(*polygon_info->edges));
664 if (polygon_info->edges == (EdgeInfo *) NULL)
666 (void) ThrowMagickException(exception,GetMagickModule(),
667 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
668 points=(PointInfo *) RelinquishMagickMemory(points);
669 return(DestroyPolygonInfo(polygon_info));
672 polygon_info->edges[edge].number_points=(size_t) n;
673 polygon_info->edges[edge].scanline=(-1.0);
674 polygon_info->edges[edge].highwater=0;
675 polygon_info->edges[edge].ghostline=ghostline;
676 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
678 ReversePoints(points,(
size_t) n);
679 polygon_info->edges[edge].points=points;
680 polygon_info->edges[edge].bounds=bounds;
681 polygon_info->edges[edge].bounds.y1=points[0].y;
682 polygon_info->edges[edge].bounds.y2=points[n-1].y;
683 polygon_info->number_edges=edge+1;
684 points=(PointInfo *) NULL;
686 points=(PointInfo *) AcquireQuantumMemory((
size_t) number_points,
688 if (points == (PointInfo *) NULL)
690 (void) ThrowMagickException(exception,GetMagickModule(),
691 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
692 return(DestroyPolygonInfo(polygon_info));
695 ghostline=MagickFalse;
701 direction=next_direction;
702 if (points == (PointInfo *) NULL)
704 if (n == (ssize_t) number_points)
707 points=(PointInfo *) ResizeQuantumMemory(points,(
size_t) number_points,
709 if (points == (PointInfo *) NULL)
711 (void) ThrowMagickException(exception,GetMagickModule(),
712 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
713 return(DestroyPolygonInfo(polygon_info));
716 point=path_info[i].point;
718 if (point.x < bounds.x1)
720 if (point.x > bounds.x2)
724 if (points != (PointInfo *) NULL)
727 points=(PointInfo *) RelinquishMagickMemory(points);
730 if (edge == number_edges)
733 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
734 polygon_info->edges,(
size_t) number_edges,
735 sizeof(*polygon_info->edges));
736 if (polygon_info->edges == (EdgeInfo *) NULL)
738 (void) ThrowMagickException(exception,GetMagickModule(),
739 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
740 return(DestroyPolygonInfo(polygon_info));
743 polygon_info->edges[edge].number_points=(size_t) n;
744 polygon_info->edges[edge].scanline=(-1.0);
745 polygon_info->edges[edge].highwater=0;
746 polygon_info->edges[edge].ghostline=ghostline;
747 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
749 ReversePoints(points,(
size_t) n);
750 polygon_info->edges[edge].points=points;
751 polygon_info->edges[edge].bounds=bounds;
752 polygon_info->edges[edge].bounds.y1=points[0].y;
753 polygon_info->edges[edge].bounds.y2=points[n-1].y;
754 points=(PointInfo *) NULL;
755 ghostline=MagickFalse;
757 polygon_info->number_edges=edge;
760 polygon_info->number_edges=edge;
761 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(polygon_info->edges,
762 polygon_info->number_edges,
sizeof(*polygon_info->edges));
763 if (polygon_info->edges == (EdgeInfo *) NULL)
765 (void) ThrowMagickException(exception,GetMagickModule(),
766 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
767 return(DestroyPolygonInfo(polygon_info));
769 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
774 edge_info=polygon_info->edges+i;
775 edge_info->points=(PointInfo *) ResizeQuantumMemory(edge_info->points,
776 edge_info->number_points,
sizeof(*edge_info->points));
777 if (edge_info->points == (PointInfo *) NULL)
779 (void) ThrowMagickException(exception,GetMagickModule(),
780 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
781 return(DestroyPolygonInfo(polygon_info));
784 qsort(polygon_info->edges,(
size_t) polygon_info->number_edges,
785 sizeof(*polygon_info->edges),DrawCompareEdges);
786 if ((GetLogEventMask() & DrawEvent) != 0)
787 LogPolygonInfo(polygon_info);
788 return(polygon_info);
821static void LogPathInfo(
const PathInfo *path_info)
826 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin vector-path");
827 for (p=path_info; p->code != EndCode; p++)
828 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
829 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
830 "moveto ghostline" : p->code == OpenCode ?
"moveto open" :
831 p->code == MoveToCode ?
"moveto" : p->code == LineToCode ?
"lineto" :
833 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end vector-path");
836static PathInfo *ConvertPrimitiveToPath(
const PrimitiveInfo *primitive_info,
837 ExceptionInfo *exception)
863 switch (primitive_info->primitive)
870 return((PathInfo *) NULL);
874 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
875 path_info=(PathInfo *) AcquireQuantumMemory((
size_t) (3UL*i+1UL),
877 if (path_info == (PathInfo *) NULL)
879 (void) ThrowMagickException(exception,GetMagickModule(),
880 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
881 return((PathInfo *) NULL);
884 closed_subpath=MagickFalse;
891 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
894 if (coordinates <= 0)
899 coordinates=primitive_info[i].coordinates;
900 p=primitive_info[i].point;
903 closed_subpath=primitive_info[i].closed_subpath;
906 if ((code == MoveToCode) || (coordinates <= 0) ||
907 (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
908 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
913 path_info[n].code=code;
914 path_info[n].point=primitive_info[i].point;
915 q=primitive_info[i].point;
920 if (closed_subpath != MagickFalse)
922 closed_subpath=MagickFalse;
928 path_info[start].code=OpenCode;
929 path_info[n].code=GhostlineCode;
930 path_info[n].point=primitive_info[i].point;
932 path_info[n].code=LineToCode;
933 path_info[n].point=p;
936 path_info[n].code=EndCode;
937 path_info[n].point.x=0.0;
938 path_info[n].point.y=0.0;
939 if (IsEventLogging() != MagickFalse)
940 LogPathInfo(path_info);
941 path_info=(PathInfo *) ResizeQuantumMemory(path_info,(
size_t) (n+1),
968MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
970 assert(draw_info != (DrawInfo *) NULL);
971 assert(draw_info->signature == MagickCoreSignature);
972 if (IsEventLogging() != MagickFalse)
973 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
974 if (draw_info->id != (
char *) NULL)
975 draw_info->id=DestroyString(draw_info->id);
976 if (draw_info->primitive != (
char *) NULL)
977 draw_info->primitive=DestroyString(draw_info->primitive);
978 if (draw_info->text != (
char *) NULL)
979 draw_info->text=DestroyString(draw_info->text);
980 if (draw_info->geometry != (
char *) NULL)
981 draw_info->geometry=DestroyString(draw_info->geometry);
982 if (draw_info->fill_pattern != (Image *) NULL)
983 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
984 if (draw_info->stroke_pattern != (Image *) NULL)
985 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
986 if (draw_info->font != (
char *) NULL)
987 draw_info->font=DestroyString(draw_info->font);
988 if (draw_info->metrics != (
char *) NULL)
989 draw_info->metrics=DestroyString(draw_info->metrics);
990 if (draw_info->family != (
char *) NULL)
991 draw_info->family=DestroyString(draw_info->family);
992 if (draw_info->encoding != (
char *) NULL)
993 draw_info->encoding=DestroyString(draw_info->encoding);
994 if (draw_info->density != (
char *) NULL)
995 draw_info->density=DestroyString(draw_info->density);
996 if (draw_info->server_name != (
char *) NULL)
997 draw_info->server_name=(
char *)
998 RelinquishMagickMemory(draw_info->server_name);
999 if (draw_info->dash_pattern != (
double *) NULL)
1000 draw_info->dash_pattern=(
double *) RelinquishMagickMemory(
1001 draw_info->dash_pattern);
1002 if (draw_info->gradient.stops != (StopInfo *) NULL)
1003 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
1004 draw_info->gradient.stops);
1005 if (draw_info->clip_mask != (
char *) NULL)
1006 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
1007 if (draw_info->clipping_mask != (Image *) NULL)
1008 draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
1009 if (draw_info->composite_mask != (Image *) NULL)
1010 draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
1011 if (draw_info->image_info != (ImageInfo *) NULL)
1012 draw_info->image_info=DestroyImageInfo(draw_info->image_info);
1013 draw_info->signature=(~MagickCoreSignature);
1014 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
1049static SegmentInfo AffineEdge(
const Image *image,
const AffineMatrix *affine,
1050 const double y,
const SegmentInfo *edge)
1065 inverse_edge.x1=edge->x1;
1066 inverse_edge.y1=edge->y1;
1067 inverse_edge.x2=edge->x2;
1068 inverse_edge.y2=edge->y2;
1069 z=affine->ry*y+affine->tx;
1070 if (affine->sx >= MagickEpsilon)
1072 intercept=(-z/affine->sx);
1074 if (x > inverse_edge.x1)
1076 intercept=(-z+(double) image->columns)/affine->sx;
1078 if (x < inverse_edge.x2)
1082 if (affine->sx < -MagickEpsilon)
1084 intercept=(-z+(double) image->columns)/affine->sx;
1086 if (x > inverse_edge.x1)
1088 intercept=(-z/affine->sx);
1090 if (x < inverse_edge.x2)
1094 if ((z < 0.0) || ((
size_t) floor(z+0.5) >= image->columns))
1096 inverse_edge.x2=edge->x1;
1097 return(inverse_edge);
1102 z=affine->sy*y+affine->ty;
1103 if (affine->rx >= MagickEpsilon)
1105 intercept=(-z/affine->rx);
1107 if (x > inverse_edge.x1)
1109 intercept=(-z+(double) image->rows)/affine->rx;
1111 if (x < inverse_edge.x2)
1115 if (affine->rx < -MagickEpsilon)
1117 intercept=(-z+(double) image->rows)/affine->rx;
1119 if (x > inverse_edge.x1)
1121 intercept=(-z/affine->rx);
1123 if (x < inverse_edge.x2)
1127 if ((z < 0.0) || ((
size_t) floor(z+0.5) >= image->rows))
1129 inverse_edge.x2=edge->x2;
1130 return(inverse_edge);
1132 return(inverse_edge);
1135static AffineMatrix InverseAffineMatrix(
const AffineMatrix *affine)
1143 determinant=MagickSafeReciprocal(affine->sx*affine->sy-affine->rx*
1145 inverse_affine.sx=determinant*affine->sy;
1146 inverse_affine.rx=determinant*(-affine->rx);
1147 inverse_affine.ry=determinant*(-affine->ry);
1148 inverse_affine.sy=determinant*affine->sx;
1149 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1151 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1153 return(inverse_affine);
1156MagickExport MagickBooleanType DrawAffineImage(Image *image,
1157 const Image *source,
const AffineMatrix *affine,ExceptionInfo *exception)
1191 assert(image != (Image *) NULL);
1192 assert(image->signature == MagickCoreSignature);
1193 if (IsEventLogging() != MagickFalse)
1194 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1195 assert(source != (
const Image *) NULL);
1196 assert(source->signature == MagickCoreSignature);
1197 assert(affine != (AffineMatrix *) NULL);
1200 extent[1].x=(double) source->columns;
1202 extent[2].x=(double) source->columns;
1203 extent[2].y=(double) source->rows;
1205 extent[3].y=(double) source->rows;
1206 for (i=0; i < 4; i++)
1212 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1213 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1217 for (i=1; i < 4; i++)
1219 if (min.x > extent[i].x)
1221 if (min.y > extent[i].y)
1223 if (max.x < extent[i].x)
1225 if (max.y < extent[i].y)
1231 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1232 return(MagickFalse);
1238 inverse_affine=InverseAffineMatrix(affine);
1241 if (edge.y2 > ((
double) image->rows-1.0))
1242 edge.y2=(double) image->rows-1.0;
1243 GetPixelInfo(image,&zero);
1244 start=CastDoubleToSsizeT(ceil(edge.y1-0.5));
1245 stop=CastDoubleToSsizeT(floor(edge.y2+0.5));
1246 source_view=AcquireVirtualCacheView(source,exception);
1247 image_view=AcquireAuthenticCacheView(image,exception);
1248#if defined(MAGICKCORE_OPENMP_SUPPORT)
1249 #pragma omp parallel for schedule(static) shared(status) \
1250 magick_number_threads(source,image,(size_t) (stop-start),2)
1252 for (y=start; y <= stop; y++)
1270 if (status == MagickFalse)
1272 inverse_edge=AffineEdge(source,&inverse_affine,(
double) y,&edge);
1273 if (inverse_edge.x2 < inverse_edge.x1)
1275 if (inverse_edge.x1 < 0.0)
1276 inverse_edge.x1=0.0;
1277 if (inverse_edge.x2 > ((
double) image->columns-1.0))
1278 inverse_edge.x2=(double) image->columns-1.0;
1279 q=GetCacheViewAuthenticPixels(image_view,CastDoubleToSsizeT(
1280 ceil(inverse_edge.x1-0.5)),y,(
size_t) CastDoubleToSsizeT(floor(
1281 inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),1,exception);
1282 if (q == (Quantum *) NULL)
1286 for (x=CastDoubleToSsizeT(ceil(inverse_edge.x1-0.5));
1287 x <= CastDoubleToSsizeT(floor(inverse_edge.x2+0.5)); x++)
1289 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1291 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1293 status=InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1294 point.x,point.y,&pixel,exception);
1295 if (status == MagickFalse)
1297 GetPixelInfoPixel(image,q,&composite);
1298 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1300 SetPixelViaPixelInfo(image,&composite,q);
1301 q+=(ptrdiff_t) GetPixelChannels(image);
1303 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1306 source_view=DestroyCacheView(source_view);
1307 image_view=DestroyCacheView(image_view);
1343static MagickBooleanType DrawBoundingRectangles(Image *image,
1344 const DrawInfo *draw_info,
const PolygonInfo *polygon_info,
1345 ExceptionInfo *exception)
1373 (void) memset(primitive_info,0,
sizeof(primitive_info));
1374 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1375 status=QueryColorCompliance(
"#000F",AllCompliance,&clone_info->fill,
1377 if (status == MagickFalse)
1379 clone_info=DestroyDrawInfo(clone_info);
1380 return(MagickFalse);
1384 if (clone_info->density != (
char *) NULL)
1392 flags=ParseGeometry(clone_info->density,&geometry_info);
1393 if ((flags & RhoValue) != 0)
1394 resolution.x=geometry_info.rho;
1395 resolution.y=resolution.x;
1396 if ((flags & SigmaValue) != 0)
1397 resolution.y=geometry_info.sigma;
1399 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1400 clone_info->stroke_width/2.0;
1405 if (polygon_info != (PolygonInfo *) NULL)
1407 bounds=polygon_info->edges[0].bounds;
1408 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1410 if (polygon_info->edges[i].bounds.x1 < (
double) bounds.x1)
1411 bounds.x1=polygon_info->edges[i].bounds.x1;
1412 if (polygon_info->edges[i].bounds.y1 < (
double) bounds.y1)
1413 bounds.y1=polygon_info->edges[i].bounds.y1;
1414 if (polygon_info->edges[i].bounds.x2 > (
double) bounds.x2)
1415 bounds.x2=polygon_info->edges[i].bounds.x2;
1416 if (polygon_info->edges[i].bounds.y2 > (
double) bounds.y2)
1417 bounds.y2=polygon_info->edges[i].bounds.y2;
1420 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1421 image->columns ? (
double) image->columns-1 : bounds.x1;
1423 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1424 image->rows ? (
double) image->rows-1 : bounds.y1;
1426 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1427 image->columns ? (
double) image->columns-1 : bounds.x2;
1429 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1430 image->rows ? (
double) image->rows-1 : bounds.y2;
1431 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1433 if (polygon_info->edges[i].direction != 0)
1434 status=QueryColorCompliance(
"#f00",AllCompliance,&clone_info->stroke,
1437 status=QueryColorCompliance(
"#0f0",AllCompliance,&clone_info->stroke,
1439 if (status == MagickFalse)
1441 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1442 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1443 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1444 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1445 primitive_info[0].primitive=RectanglePrimitive;
1446 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1447 primitive_info[0].method=ReplaceMethod;
1448 coordinates=(ssize_t) primitive_info[0].coordinates;
1449 primitive_info[coordinates].primitive=UndefinedPrimitive;
1450 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1451 if (status == MagickFalse)
1454 if (i < (ssize_t) polygon_info->number_edges)
1456 clone_info=DestroyDrawInfo(clone_info);
1457 return(status == 0 ? MagickFalse : MagickTrue);
1460 status=QueryColorCompliance(
"#00f",AllCompliance,&clone_info->stroke,
1462 if (status == MagickFalse)
1464 clone_info=DestroyDrawInfo(clone_info);
1465 return(MagickFalse);
1467 start.x=(double) (bounds.x1-mid);
1468 start.y=(double) (bounds.y1-mid);
1469 end.x=(double) (bounds.x2+mid);
1470 end.y=(double) (bounds.y2+mid);
1471 primitive_info[0].primitive=RectanglePrimitive;
1472 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1473 primitive_info[0].method=ReplaceMethod;
1474 coordinates=(ssize_t) primitive_info[0].coordinates;
1475 primitive_info[coordinates].primitive=UndefinedPrimitive;
1476 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1477 clone_info=DestroyDrawInfo(clone_info);
1478 return(status == 0 ? MagickFalse : MagickTrue);
1510MagickExport MagickBooleanType DrawClipPath(Image *image,
1511 const DrawInfo *draw_info,
const char *
id,ExceptionInfo *exception)
1522 clip_path=GetImageArtifact(image,
id);
1523 if (clip_path == (
const char *) NULL)
1524 return(MagickFalse);
1525 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1527 if (clipping_mask == (Image *) NULL)
1528 return(MagickFalse);
1529 status=SetImageMask(image,WritePixelMask,clipping_mask,exception);
1530 clipping_mask=DestroyImage(clipping_mask);
1566static Image *DrawClippingMask(Image *image,
const DrawInfo *draw_info,
1567 const char *
id,
const char *clip_path,ExceptionInfo *exception)
1582 assert(image != (Image *) NULL);
1583 assert(image->signature == MagickCoreSignature);
1584 assert(draw_info != (
const DrawInfo *) NULL);
1585 if (IsEventLogging() != MagickFalse)
1586 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1587 clip_mask=AcquireImage((
const ImageInfo *) NULL,exception);
1588 status=SetImageExtent(clip_mask,image->columns,image->rows,exception);
1589 if (status == MagickFalse)
1590 return(DestroyImage(clip_mask));
1591 status=SetImageMask(clip_mask,WritePixelMask,(Image *) NULL,exception);
1592 status=QueryColorCompliance(
"#0000",AllCompliance,
1593 &clip_mask->background_color,exception);
1594 clip_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1595 clip_mask->background_color.alpha_trait=BlendPixelTrait;
1596 status=SetImageBackgroundColor(clip_mask,exception);
1597 if (draw_info->debug != MagickFalse)
1598 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin clip-path %s",
1600 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1601 (void) CloneString(&clone_info->primitive,clip_path);
1602 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1604 if (clone_info->clip_mask != (
char *) NULL)
1605 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1606 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1608 clone_info->stroke_width=0.0;
1609 clone_info->alpha=OpaqueAlpha;
1610 clone_info->clip_path=MagickTrue;
1611 status=RenderMVGContent(clip_mask,clone_info,0,exception);
1612 clone_info=DestroyDrawInfo(clone_info);
1613 separate_mask=SeparateImage(clip_mask,AlphaChannel,exception);
1614 if (separate_mask == (Image *) NULL)
1618 clip_mask=DestroyImage(clip_mask);
1619 clip_mask=separate_mask;
1620 status&=(MagickStatusType) NegateImage(clip_mask,MagickFalse,exception);
1622 if (status == MagickFalse)
1623 clip_mask=DestroyImage(clip_mask);
1624 if (draw_info->debug != MagickFalse)
1625 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end clip-path");
1660static Image *DrawCompositeMask(Image *image,
const DrawInfo *draw_info,
1661 const char *
id,
const char *mask_path,ExceptionInfo *exception)
1676 assert(image != (Image *) NULL);
1677 assert(image->signature == MagickCoreSignature);
1678 assert(draw_info != (
const DrawInfo *) NULL);
1679 if (IsEventLogging() != MagickFalse)
1680 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1681 composite_mask=AcquireImage((
const ImageInfo *) NULL,exception);
1682 status=SetImageExtent(composite_mask,image->columns,image->rows,exception);
1683 if (status == MagickFalse)
1684 return(DestroyImage(composite_mask));
1685 status=SetImageMask(composite_mask,CompositePixelMask,(Image *) NULL,
1687 status=QueryColorCompliance(
"#0000",AllCompliance,
1688 &composite_mask->background_color,exception);
1689 composite_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1690 composite_mask->background_color.alpha_trait=BlendPixelTrait;
1691 (void) SetImageBackgroundColor(composite_mask,exception);
1692 if (draw_info->debug != MagickFalse)
1693 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin mask-path %s",
1695 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1696 (void) CloneString(&clone_info->primitive,mask_path);
1697 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1699 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1701 clone_info->stroke_width=0.0;
1702 clone_info->alpha=OpaqueAlpha;
1703 status=RenderMVGContent(composite_mask,clone_info,0,exception);
1704 clone_info=DestroyDrawInfo(clone_info);
1705 separate_mask=SeparateImage(composite_mask,AlphaChannel,exception);
1706 if (separate_mask != (Image *) NULL)
1708 composite_mask=DestroyImage(composite_mask);
1709 composite_mask=separate_mask;
1710 status=NegateImage(composite_mask,MagickFalse,exception);
1711 if (status == MagickFalse)
1712 composite_mask=DestroyImage(composite_mask);
1714 if (status == MagickFalse)
1715 composite_mask=DestroyImage(composite_mask);
1716 if (draw_info->debug != MagickFalse)
1717 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end mask-path");
1718 return(composite_mask);
1752static MagickBooleanType DrawDashPolygon(
const DrawInfo *draw_info,
1753 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1783 assert(draw_info != (
const DrawInfo *) NULL);
1784 if (draw_info->debug != MagickFalse)
1785 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-dash");
1786 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1787 number_vertices=(size_t) i;
1788 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
1789 (2UL*number_vertices+32UL),
sizeof(*dash_polygon));
1790 if (dash_polygon == (PrimitiveInfo *) NULL)
1792 (void) ThrowMagickException(exception,GetMagickModule(),
1793 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1794 return(MagickFalse);
1796 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1797 sizeof(*dash_polygon));
1798 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1799 clone_info->miterlimit=0;
1800 dash_polygon[0]=primitive_info[0];
1801 dash_polygon[0].closed_subpath=MagickFalse;
1802 scale=ExpandAffine(&draw_info->affine);
1803 length=scale*draw_info->dash_pattern[0];
1804 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1805 scale*draw_info->dash_offset : 0.0;
1807 for (n=0; offset > 0.0; j=0)
1809 if (draw_info->dash_pattern[n] <= 0.0)
1811 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1812 if (offset > length)
1816 length=scale*draw_info->dash_pattern[n];
1819 if (offset < length)
1831 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1833 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1834 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1835 maximum_length=hypot(dx,dy);
1836 if (maximum_length > (
double) (MaxBezierCoordinates >> 2))
1838 if (fabs(length) < MagickEpsilon)
1840 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1842 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1844 length=scale*draw_info->dash_pattern[n];
1846 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1848 total_length+=length;
1849 if ((n & 0x01) != 0)
1851 dash_polygon[0]=primitive_info[0];
1852 dash_polygon[0].closed_subpath=MagickFalse;
1853 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1854 total_length*MagickSafeReciprocal(maximum_length));
1855 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1856 total_length*MagickSafeReciprocal(maximum_length));
1861 if ((j+1) > (ssize_t) number_vertices)
1863 dash_polygon[j]=primitive_info[i-1];
1864 dash_polygon[j].closed_subpath=MagickFalse;
1865 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1866 total_length*MagickSafeReciprocal(maximum_length));
1867 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1868 total_length*MagickSafeReciprocal(maximum_length));
1869 dash_polygon[j].coordinates=1;
1871 dash_polygon[0].coordinates=(size_t) j;
1872 dash_polygon[j].primitive=UndefinedPrimitive;
1873 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1874 dash_polygon,exception);
1875 if (status == MagickFalse)
1878 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1880 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1882 length=scale*draw_info->dash_pattern[n];
1884 length-=(maximum_length-total_length);
1885 if ((n & 0x01) != 0)
1887 dash_polygon[j]=primitive_info[i];
1888 dash_polygon[j].coordinates=1;
1891 if ((status != MagickFalse) && (total_length < maximum_length) &&
1892 ((n & 0x01) == 0) && (j > 1))
1894 dash_polygon[j]=primitive_info[i-1];
1895 dash_polygon[j].closed_subpath=MagickFalse;
1896 dash_polygon[j].point.x+=MagickEpsilon;
1897 dash_polygon[j].point.y+=MagickEpsilon;
1898 dash_polygon[j].coordinates=1;
1900 dash_polygon[0].coordinates=(size_t) j;
1901 dash_polygon[j].primitive=UndefinedPrimitive;
1902 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1903 dash_polygon,exception);
1905 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1906 clone_info=DestroyDrawInfo(clone_info);
1907 if (draw_info->debug != MagickFalse)
1908 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-dash");
1909 return(status != 0 ? MagickTrue : MagickFalse);
1940static inline double GetStopColorOffset(
const GradientInfo *gradient,
1941 const ssize_t x,
const ssize_t y)
1943 switch (gradient->type)
1945 case UndefinedGradient:
1946 case LinearGradient:
1961 gradient_vector=(&gradient->gradient_vector);
1962 p.x=gradient_vector->x2-gradient_vector->x1;
1963 p.y=gradient_vector->y2-gradient_vector->y1;
1964 q.x=(double) x-gradient_vector->x1;
1965 q.y=(double) y-gradient_vector->y1;
1966 length=sqrt(q.x*q.x+q.y*q.y);
1967 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1968 gamma=MagickSafeReciprocal(gamma);
1969 scale=p.x*q.x+p.y*q.y;
1970 offset=gamma*scale*length;
1973 case RadialGradient:
1978 if (gradient->spread == RepeatSpread)
1980 v.x=(double) x-gradient->center.x;
1981 v.y=(double) y-gradient->center.y;
1982 return(sqrt(v.x*v.x+v.y*v.y));
1984 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1985 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1986 gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1987 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1988 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1989 gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1990 return(sqrt(v.x*v.x+v.y*v.y));
1996static int StopInfoCompare(
const void *x,
const void *y)
2002 stop_1=(StopInfo *) x;
2003 stop_2=(StopInfo *) y;
2004 if (stop_1->offset > stop_2->offset)
2006 if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
2011MagickExport MagickBooleanType DrawGradientImage(Image *image,
2012 const DrawInfo *draw_info,ExceptionInfo *exception)
2047 assert(image != (Image *) NULL);
2048 assert(image->signature == MagickCoreSignature);
2049 assert(draw_info != (
const DrawInfo *) NULL);
2050 if (IsEventLogging() != MagickFalse)
2051 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2052 gradient=(&draw_info->gradient);
2053 qsort(gradient->stops,gradient->number_stops,
sizeof(StopInfo),
2055 gradient_vector=(&gradient->gradient_vector);
2056 point.x=gradient_vector->x2-gradient_vector->x1;
2057 point.y=gradient_vector->y2-gradient_vector->y1;
2058 length=sqrt(point.x*point.x+point.y*point.y);
2059 bounding_box=gradient->bounding_box;
2061 GetPixelInfo(image,&zero);
2062 image_view=AcquireAuthenticCacheView(image,exception);
2063 height=(size_t) (bounding_box.y+(ssize_t) bounding_box.height);
2064#if defined(MAGICKCORE_OPENMP_SUPPORT)
2065 #pragma omp parallel for schedule(static) shared(status) \
2066 magick_number_threads(image,image,height,1)
2068 for (y=bounding_box.y; y < (ssize_t) height; y++)
2089 if (status == MagickFalse)
2091 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(
size_t)
2092 bounding_box.width,1,exception);
2093 if (q == (Quantum *) NULL)
2100 offset=GetStopColorOffset(gradient,0,y);
2101 if (gradient->type != RadialGradient)
2102 offset*=MagickSafeReciprocal(length);
2103 width=(size_t) (bounding_box.x+(ssize_t) bounding_box.width);
2104 for (x=bounding_box.x; x < (ssize_t) width; x++)
2106 GetPixelInfoPixel(image,q,&pixel);
2107 switch (gradient->spread)
2109 case UndefinedSpread:
2112 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2113 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2115 offset=GetStopColorOffset(gradient,x,y);
2116 if (gradient->type != RadialGradient)
2117 offset*=MagickSafeReciprocal(length);
2119 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2120 if (offset < gradient->stops[i].offset)
2122 if ((offset < 0.0) || (i == 0))
2123 composite=gradient->stops[0].color;
2125 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2126 composite=gradient->stops[gradient->number_stops-1].color;
2131 alpha=(offset-gradient->stops[i].offset)/
2132 (gradient->stops[j].offset-gradient->stops[i].offset);
2133 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2134 &gradient->stops[j].color,alpha,&composite);
2140 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2141 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2143 offset=GetStopColorOffset(gradient,x,y);
2144 if (gradient->type != RadialGradient)
2145 offset*=MagickSafeReciprocal(length);
2149 if ((ssize_t) fmod(offset,2.0) == 0)
2150 offset=fmod(offset,1.0);
2152 offset=1.0-fmod(offset,1.0);
2153 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2154 if (offset < gradient->stops[i].offset)
2157 composite=gradient->stops[0].color;
2159 if (i == (ssize_t) gradient->number_stops)
2160 composite=gradient->stops[gradient->number_stops-1].color;
2165 alpha=(offset-gradient->stops[i].offset)/
2166 (gradient->stops[j].offset-gradient->stops[i].offset);
2167 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2168 &gradient->stops[j].color,alpha,&composite);
2180 antialias=MagickFalse;
2182 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2183 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2185 offset=GetStopColorOffset(gradient,x,y);
2186 if (gradient->type == LinearGradient)
2188 repeat=fmod(offset,length);
2190 repeat=length-fmod(-repeat,length);
2192 repeat=fmod(offset,length);
2193 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2194 MagickTrue : MagickFalse;
2195 offset=MagickSafeReciprocal(length)*repeat;
2199 repeat=fmod(offset,gradient->radius);
2201 repeat=gradient->radius-fmod(-repeat,gradient->radius);
2203 repeat=fmod(offset,gradient->radius);
2204 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2206 offset=repeat*MagickSafeReciprocal(gradient->radius);
2209 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2210 if (offset < gradient->stops[i].offset)
2213 composite=gradient->stops[0].color;
2215 if (i == (ssize_t) gradient->number_stops)
2216 composite=gradient->stops[gradient->number_stops-1].color;
2221 alpha=(offset-gradient->stops[i].offset)/
2222 (gradient->stops[j].offset-gradient->stops[i].offset);
2223 if (antialias != MagickFalse)
2225 if (gradient->type == LinearGradient)
2226 alpha=length-repeat;
2228 alpha=gradient->radius-repeat;
2230 j=(ssize_t) gradient->number_stops-1L;
2232 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2233 &gradient->stops[j].color,alpha,&composite);
2238 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
2240 SetPixelViaPixelInfo(image,&pixel,q);
2241 q+=(ptrdiff_t) GetPixelChannels(image);
2243 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2246 image_view=DestroyCacheView(image_view);
2282static inline MagickBooleanType CheckPrimitiveExtent(MVGInfo *mvg_info,
2297 if ((mvg_info == (MVGInfo *) NULL) ||
2298 (mvg_info->primitive_info == (PrimitiveInfo **) NULL) ||
2299 (*mvg_info->primitive_info == (PrimitiveInfo *) NULL) ||
2300 (mvg_info->extent == (
size_t *) NULL))
2301 return(MagickFalse);
2302 proposed_extent=mvg_info->offset+pad+PrimitiveExtentPad+1.0;
2303 if ((proposed_extent <= 0.0) || (proposed_extent > (
double) MAGICK_SIZE_MAX))
2304 return(MagickFalse);
2305 extent=CastDoubleToSizeT(ceil(proposed_extent));
2306 if (extent <= *mvg_info->extent)
2308 if (extent > (GetMaxMemoryRequest()/
sizeof(PrimitiveInfo)))
2309 return(MagickFalse);
2310 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(
2311 *mvg_info->primitive_info,extent,
sizeof(PrimitiveInfo));
2312 if (primitive_info == (PrimitiveInfo *) NULL)
2317 extent=(size_t) PrimitiveExtentPad;
2318 primitive_info=(PrimitiveInfo *) AcquireCriticalMemory(extent*
2319 sizeof(*primitive_info));
2320 (void) memset(primitive_info,0,extent*
sizeof(*primitive_info));
2321 *mvg_info->primitive_info=primitive_info;
2322 *mvg_info->extent=extent;
2324 ThrowMagickException(mvg_info->exception,GetMagickModule(),
2325 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2326 return(MagickFalse);
2331 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2333 primitive_info[i].primitive=UndefinedPrimitive;
2334 primitive_info[i].text=(
char *) NULL;
2336 *mvg_info->primitive_info=primitive_info;
2337 *mvg_info->extent=extent;
2341static inline double GetDrawValue(
const char *magick_restrict
string,
2342 char **magick_restrict sentinel)
2345 **magick_restrict q;
2351 value=InterpretLocaleValue(
string,q);
2356static int MVGMacroCompare(
const void *target,
const void *source)
2362 p=(
const char *) target;
2363 q=(
const char *) source;
2364 return(strcmp(p,q));
2367static SplayTreeInfo *GetMVGMacros(
const char *primitive)
2385 if (primitive == (
const char *) NULL)
2386 return((SplayTreeInfo *) NULL);
2387 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2388 RelinquishMagickMemory);
2389 macro=AcquireString(primitive);
2390 token=AcquireString(primitive);
2391 extent=strlen(token)+MagickPathExtent;
2392 for (q=primitive; *q !=
'\0'; )
2394 if (GetNextToken(q,&q,extent,token) < 1)
2398 if (LocaleCompare(
"push",token) == 0)
2404 (void) GetNextToken(q,&q,extent,token);
2408 name[MagickPathExtent];
2419 (void) GetNextToken(q,&q,extent,token);
2422 (void) CopyMagickString(name,token,MagickPathExtent);
2424 for (p=q; *p !=
'\0'; )
2426 if (GetNextToken(p,&p,extent,token) < 1)
2430 if (LocaleCompare(token,
"pop") == 0)
2432 end=p-strlen(token)-1;
2435 if (LocaleCompare(token,
"push") == 0)
2437 if ((n == 0) && (end >= start))
2440 length=(size_t) (end-start);
2445 (void) GetNextToken(p,&p,extent,token);
2448 (void) CopyMagickString(macro,start,length);
2449 (void) AddValueToSplayTree(macros,ConstantString(name),
2450 ConstantString(macro));
2458 token=DestroyString(token);
2459 macro=DestroyString(macro);
2463static inline MagickBooleanType IsPoint(
const char *point)
2471 value=GetDrawValue(point,&p);
2472 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2476static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2477 const PointInfo point)
2479 primitive_info->point=point;
2480 primitive_info->coordinates=1;
2481 primitive_info->closed_subpath=MagickFalse;
2482 primitive_info->text=(
char *) NULL;
2486static MagickBooleanType RenderMVGContent(Image *image,
2487 const DrawInfo *draw_info,
const size_t depth,ExceptionInfo *exception)
2489#define RenderImageTag "Render/Image"
2496 keyword[MagickPathExtent],
2497 geometry[MagickPathExtent],
2499 pattern[MagickPathExtent],
2563 assert(image != (Image *) NULL);
2564 assert(image->signature == MagickCoreSignature);
2565 assert(draw_info != (DrawInfo *) NULL);
2566 assert(draw_info->signature == MagickCoreSignature);
2567 if (IsEventLogging() != MagickFalse)
2568 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2569 if (depth > MagickMaxRecursionDepth)
2570 ThrowBinaryException(DrawError,
"VectorGraphicsNestedTooDeeply",
2572 if ((draw_info->primitive == (
char *) NULL) ||
2573 (*draw_info->primitive ==
'\0'))
2574 return(MagickFalse);
2575 if (draw_info->debug != MagickFalse)
2576 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"begin draw-image");
2577 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2578 return(MagickFalse);
2579 if ((image->alpha_trait & BlendPixelTrait) == 0)
2581 status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2582 if (status == MagickFalse)
2583 return(MagickFalse);
2585 if ((*draw_info->primitive ==
'@') && (strlen(draw_info->primitive) > 1) &&
2586 (*(draw_info->primitive+1) !=
'-') && (depth == 0))
2587 primitive=FileToString(draw_info->primitive,~0UL,exception);
2589 primitive=AcquireString(draw_info->primitive);
2590 if (primitive == (
char *) NULL)
2591 return(MagickFalse);
2592 primitive_extent=(double) strlen(primitive);
2593 (void) SetImageArtifact(image,
"mvg:vector-graphics",primitive);
2596 stops=(StopInfo *) NULL;
2600 graphic_context=(DrawInfo **) AcquireMagickMemory(
sizeof(*graphic_context));
2601 if (graphic_context == (DrawInfo **) NULL)
2603 primitive=DestroyString(primitive);
2604 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2607 number_points=(size_t) PrimitiveExtentPad;
2608 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
2609 (number_points+1),
sizeof(*primitive_info));
2610 if (primitive_info == (PrimitiveInfo *) NULL)
2612 primitive=DestroyString(primitive);
2613 for ( ; n >= 0; n--)
2614 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2615 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2616 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2619 (void) memset(primitive_info,0,(
size_t) (number_points+1)*
2620 sizeof(*primitive_info));
2621 (void) memset(&mvg_info,0,
sizeof(mvg_info));
2622 mvg_info.primitive_info=(&primitive_info);
2623 mvg_info.extent=(&number_points);
2624 mvg_info.exception=exception;
2625 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2626 graphic_context[n]->viewbox=image->page;
2627 if ((image->page.width == 0) || (image->page.height == 0))
2629 graphic_context[n]->viewbox.width=image->columns;
2630 graphic_context[n]->viewbox.height=image->rows;
2632 token=AcquireString(primitive);
2633 extent=strlen(token)+MagickPathExtent;
2637 macros=GetMVGMacros(primitive);
2639 for (q=primitive; *q !=
'\0'; )
2644 if (GetNextToken(q,&q,MagickPathExtent,keyword) < 1)
2646 if (*keyword ==
'\0')
2648 if (*keyword ==
'#')
2653 while ((*q !=
'\n') && (*q !=
'\0'))
2657 p=q-strlen(keyword)-1;
2658 primitive_type=UndefinedPrimitive;
2659 current=graphic_context[n]->affine;
2660 GetAffineMatrix(&affine);
2669 if (LocaleCompare(
"affine",keyword) == 0)
2671 (void) GetNextToken(q,&q,extent,token);
2672 affine.sx=GetDrawValue(token,&next_token);
2673 if (token == next_token)
2674 ThrowPointExpectedException(token,exception);
2675 (void) GetNextToken(q,&q,extent,token);
2677 (void) GetNextToken(q,&q,extent,token);
2678 affine.ry=GetDrawValue(token,&next_token);
2679 if (token == next_token)
2680 ThrowPointExpectedException(token,exception);
2681 (void) GetNextToken(q,&q,extent,token);
2683 (void) GetNextToken(q,&q,extent,token);
2684 affine.rx=GetDrawValue(token,&next_token);
2685 if (token == next_token)
2686 ThrowPointExpectedException(token,exception);
2687 (void) GetNextToken(q,&q,extent,token);
2689 (void) GetNextToken(q,&q,extent,token);
2690 affine.sy=GetDrawValue(token,&next_token);
2691 if (token == next_token)
2692 ThrowPointExpectedException(token,exception);
2693 (void) GetNextToken(q,&q,extent,token);
2695 (void) GetNextToken(q,&q,extent,token);
2696 affine.tx=GetDrawValue(token,&next_token);
2697 if (token == next_token)
2698 ThrowPointExpectedException(token,exception);
2699 (void) GetNextToken(q,&q,extent,token);
2701 (void) GetNextToken(q,&q,extent,token);
2702 affine.ty=GetDrawValue(token,&next_token);
2703 if (token == next_token)
2704 ThrowPointExpectedException(token,exception);
2707 if (LocaleCompare(
"alpha",keyword) == 0)
2709 primitive_type=AlphaPrimitive;
2712 if (LocaleCompare(
"arc",keyword) == 0)
2714 primitive_type=ArcPrimitive;
2723 if (LocaleCompare(
"bezier",keyword) == 0)
2725 primitive_type=BezierPrimitive;
2728 if (LocaleCompare(
"border-color",keyword) == 0)
2730 (void) GetNextToken(q,&q,extent,token);
2731 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
2732 &graphic_context[n]->border_color,exception);
2741 if (LocaleCompare(
"class",keyword) == 0)
2746 (void) GetNextToken(q,&q,extent,token);
2747 if ((*token ==
'\0') || (*token ==
';'))
2755 for (i=0; i <= n; i++)
2756 if (LocaleCompare(token,graphic_context[i]->
id) == 0)
2760 if (classDepth++ > MagickMaxRecursionDepth)
2762 (void) ThrowMagickException(exception,GetMagickModule(),
2763 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",token);
2767 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2768 if ((graphic_context[n]->render != MagickFalse) &&
2769 (mvg_class != (
const char *) NULL) && (p > primitive))
2780 (void) CloneString(&graphic_context[n]->
id,token);
2781 offset=(ssize_t) (p-primitive);
2782 elements=AcquireString(primitive);
2783 elements[offset]=
'\0';
2784 (void) ConcatenateString(&elements,mvg_class);
2785 (void) ConcatenateString(&elements,
"\n");
2786 (void) ConcatenateString(&elements,q);
2787 primitive=DestroyString(primitive);
2793 if (LocaleCompare(
"clip-path",keyword) == 0)
2801 (void) GetNextToken(q,&q,extent,token);
2807 (void) CloneString(&graphic_context[n]->clip_mask,token);
2808 clip_path=(
const char *) GetValueFromSplayTree(macros,token);
2809 if (clip_path != (
const char *) NULL)
2811 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2812 graphic_context[n]->clipping_mask=
2813 DestroyImage(graphic_context[n]->clipping_mask);
2814 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2815 graphic_context[n],token,clip_path,exception);
2816 if (graphic_context[n]->compliance != SVGCompliance)
2818 clip_path=(
const char *) GetValueFromSplayTree(macros,
2819 graphic_context[n]->clip_mask);
2820 if (clip_path != (
const char *) NULL)
2821 (void) SetImageArtifact(image,
2822 graphic_context[n]->clip_mask,clip_path);
2823 status&=(MagickStatusType) DrawClipPath(image,
2824 graphic_context[n],graphic_context[n]->clip_mask,
2830 if (LocaleCompare(
"clip-rule",keyword) == 0)
2835 (void) GetNextToken(q,&q,extent,token);
2836 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2838 if (fill_rule == -1)
2843 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2846 if (LocaleCompare(
"clip-units",keyword) == 0)
2851 (void) GetNextToken(q,&q,extent,token);
2852 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2854 if (clip_units == -1)
2859 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2860 if (clip_units == ObjectBoundingBox)
2862 GetAffineMatrix(¤t);
2863 affine.sx=draw_info->bounds.x2;
2864 affine.sy=draw_info->bounds.y2;
2865 affine.tx=draw_info->bounds.x1;
2866 affine.ty=draw_info->bounds.y1;
2871 if (LocaleCompare(
"circle",keyword) == 0)
2873 primitive_type=CirclePrimitive;
2876 if (LocaleCompare(
"color",keyword) == 0)
2878 primitive_type=ColorPrimitive;
2881 if (LocaleCompare(
"compliance",keyword) == 0)
2887 (void) GetNextToken(q,&q,extent,token);
2888 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2889 MagickComplianceOptions,MagickFalse,token);
2892 if (LocaleCompare(
"currentColor",keyword) == 0)
2894 (void) GetNextToken(q,&q,extent,token);
2903 if (LocaleCompare(
"decorate",keyword) == 0)
2908 (void) GetNextToken(q,&q,extent,token);
2909 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2916 graphic_context[n]->decorate=(DecorationType) decorate;
2919 if (LocaleCompare(
"density",keyword) == 0)
2921 (void) GetNextToken(q,&q,extent,token);
2922 (void) CloneString(&graphic_context[n]->density,token);
2925 if (LocaleCompare(
"direction",keyword) == 0)
2930 (void) GetNextToken(q,&q,extent,token);
2931 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2933 if (direction == -1)
2936 graphic_context[n]->direction=(DirectionType) direction;
2945 if (LocaleCompare(
"ellipse",keyword) == 0)
2947 primitive_type=EllipsePrimitive;
2950 if (LocaleCompare(
"encoding",keyword) == 0)
2952 (void) GetNextToken(q,&q,extent,token);
2953 (void) CloneString(&graphic_context[n]->encoding,token);
2962 if (LocaleCompare(
"fill",keyword) == 0)
2967 (void) GetNextToken(q,&q,extent,token);
2968 if (graphic_context[n]->clip_path != MagickFalse)
2970 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2971 if (mvg_class != (
const char *) NULL)
2973 (void) DrawPatternPath(image,draw_info,mvg_class,
2974 &graphic_context[n]->fill_pattern,exception);
2977 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
2978 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
2980 (void) DrawPatternPath(image,draw_info,token,
2981 &graphic_context[n]->fill_pattern,exception);
2984 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
2985 &graphic_context[n]->fill,exception);
2986 if (graphic_context[n]->fill_alpha != (
double) OpaqueAlpha)
2987 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2990 if (LocaleCompare(
"fill-opacity",keyword) == 0)
2995 (void) GetNextToken(q,&q,extent,token);
2996 if (graphic_context[n]->clip_path != MagickFalse)
2998 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
2999 opacity=MagickMin(MagickMax(factor*
3000 GetDrawValue(token,&next_token),0.0),1.0);
3001 if (token == next_token)
3002 ThrowPointExpectedException(token,exception);
3003 if (graphic_context[n]->compliance == SVGCompliance)
3004 graphic_context[n]->fill_alpha*=opacity;
3006 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3007 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
3008 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3010 graphic_context[n]->fill.alpha=(MagickRealType)
3011 ClampToQuantum((
double) QuantumRange*opacity);
3012 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3015 if (LocaleCompare(
"fill-rule",keyword) == 0)
3020 (void) GetNextToken(q,&q,extent,token);
3021 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
3023 if (fill_rule == -1)
3028 graphic_context[n]->fill_rule=(FillRule) fill_rule;
3031 if (LocaleCompare(
"font",keyword) == 0)
3033 (void) GetNextToken(q,&q,extent,token);
3034 (void) CloneString(&graphic_context[n]->font,token);
3035 if (LocaleCompare(
"none",token) == 0)
3036 graphic_context[n]->font=(
char *) RelinquishMagickMemory(
3037 graphic_context[n]->font);
3040 if (LocaleCompare(
"font-family",keyword) == 0)
3042 (void) GetNextToken(q,&q,extent,token);
3043 (void) CloneString(&graphic_context[n]->family,token);
3046 if (LocaleCompare(
"font-size",keyword) == 0)
3048 (void) GetNextToken(q,&q,extent,token);
3049 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3050 if (token == next_token)
3051 ThrowPointExpectedException(token,exception);
3054 if (LocaleCompare(
"font-stretch",keyword) == 0)
3059 (void) GetNextToken(q,&q,extent,token);
3060 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3066 graphic_context[n]->stretch=(StretchType) stretch;
3069 if (LocaleCompare(
"font-style",keyword) == 0)
3074 (void) GetNextToken(q,&q,extent,token);
3075 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3081 graphic_context[n]->style=(StyleType) style;
3084 if (LocaleCompare(
"font-weight",keyword) == 0)
3089 (void) GetNextToken(q,&q,extent,token);
3090 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3092 weight=(ssize_t) StringToUnsignedLong(token);
3093 graphic_context[n]->weight=(size_t) weight;
3102 if (LocaleCompare(
"gradient-units",keyword) == 0)
3104 (void) GetNextToken(q,&q,extent,token);
3107 if (LocaleCompare(
"gravity",keyword) == 0)
3112 (void) GetNextToken(q,&q,extent,token);
3113 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3119 graphic_context[n]->gravity=(GravityType) gravity;
3128 if (LocaleCompare(
"image",keyword) == 0)
3133 primitive_type=ImagePrimitive;
3134 (void) GetNextToken(q,&q,extent,token);
3135 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3141 graphic_context[n]->compose=(CompositeOperator) compose;
3144 if (LocaleCompare(
"interline-spacing",keyword) == 0)
3146 (void) GetNextToken(q,&q,extent,token);
3147 graphic_context[n]->interline_spacing=GetDrawValue(token,
3149 if (token == next_token)
3150 ThrowPointExpectedException(token,exception);
3153 if (LocaleCompare(
"interword-spacing",keyword) == 0)
3155 (void) GetNextToken(q,&q,extent,token);
3156 graphic_context[n]->interword_spacing=GetDrawValue(token,
3158 if (token == next_token)
3159 ThrowPointExpectedException(token,exception);
3168 if (LocaleCompare(
"kerning",keyword) == 0)
3170 (void) GetNextToken(q,&q,extent,token);
3171 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3172 if (token == next_token)
3173 ThrowPointExpectedException(token,exception);
3182 if (LocaleCompare(
"letter-spacing",keyword) == 0)
3184 (void) GetNextToken(q,&q,extent,token);
3185 if (IsPoint(token) == MagickFalse)
3187 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3188 clone_info->text=AcquireString(
" ");
3189 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,&metrics,
3191 graphic_context[n]->kerning=metrics.width*
3192 GetDrawValue(token,&next_token);
3193 clone_info=DestroyDrawInfo(clone_info);
3194 if (token == next_token)
3195 ThrowPointExpectedException(token,exception);
3198 if (LocaleCompare(
"line",keyword) == 0)
3200 primitive_type=LinePrimitive;
3209 if (LocaleCompare(
"mask",keyword) == 0)
3217 (void) GetNextToken(q,&q,extent,token);
3218 mask_path=(
const char *) GetValueFromSplayTree(macros,token);
3219 if (mask_path != (
const char *) NULL)
3221 if (graphic_context[n]->composite_mask != (Image *) NULL)
3222 graphic_context[n]->composite_mask=
3223 DestroyImage(graphic_context[n]->composite_mask);
3224 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3225 graphic_context[n],token,mask_path,exception);
3226 if (graphic_context[n]->compliance != SVGCompliance)
3227 status=SetImageMask(image,CompositePixelMask,
3228 graphic_context[n]->composite_mask,exception);
3238 if (LocaleCompare(
"offset",keyword) == 0)
3240 (void) GetNextToken(q,&q,extent,token);
3243 if (LocaleCompare(
"opacity",keyword) == 0)
3248 (void) GetNextToken(q,&q,extent,token);
3249 if (graphic_context[n]->clip_path != MagickFalse)
3251 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3252 opacity=MagickMin(MagickMax(factor*
3253 GetDrawValue(token,&next_token),0.0),1.0);
3254 if (token == next_token)
3255 ThrowPointExpectedException(token,exception);
3256 if (graphic_context[n]->compliance == SVGCompliance)
3258 graphic_context[n]->fill_alpha*=opacity;
3259 graphic_context[n]->stroke_alpha*=opacity;
3263 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3264 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3266 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
3268 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3269 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3273 graphic_context[n]->fill.alpha=(MagickRealType)
3274 ClampToQuantum((
double) QuantumRange*opacity);
3275 graphic_context[n]->stroke.alpha=(MagickRealType)
3276 ClampToQuantum((
double) QuantumRange*opacity);
3278 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3279 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3288 if (LocaleCompare(
"path",keyword) == 0)
3290 primitive_type=PathPrimitive;
3293 if (LocaleCompare(
"point",keyword) == 0)
3295 primitive_type=PointPrimitive;
3298 if (LocaleCompare(
"polyline",keyword) == 0)
3300 primitive_type=PolylinePrimitive;
3303 if (LocaleCompare(
"polygon",keyword) == 0)
3305 primitive_type=PolygonPrimitive;
3308 if (LocaleCompare(
"pop",keyword) == 0)
3310 if (GetNextToken(q,&q,extent,token) < 1)
3312 if (LocaleCompare(
"class",token) == 0)
3314 if (LocaleCompare(
"clip-path",token) == 0)
3316 if (LocaleCompare(
"defs",token) == 0)
3319 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3323 if (LocaleCompare(
"gradient",token) == 0)
3325 if (LocaleCompare(
"graphic-context",token) == 0)
3329 (void) ThrowMagickException(exception,GetMagickModule(),
3330 DrawError,
"UnbalancedGraphicContextPushPop",
"`%s'",token);
3335 if ((graphic_context[n]->clip_mask != (
char *) NULL) &&
3336 (graphic_context[n]->compliance != SVGCompliance))
3337 if (LocaleCompare(graphic_context[n]->clip_mask,
3338 graphic_context[n-1]->clip_mask) != 0)
3339 status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3341 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3345 if (LocaleCompare(
"mask",token) == 0)
3347 if (LocaleCompare(
"pattern",token) == 0)
3349 if (LocaleCompare(
"symbol",token) == 0)
3352 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3359 if (LocaleCompare(
"push",keyword) == 0)
3361 if (GetNextToken(q,&q,extent,token) < 1)
3363 if (LocaleCompare(
"class",token) == 0)
3368 for (p=q; *q !=
'\0'; )
3370 if (GetNextToken(q,&q,extent,token) < 1)
3372 if (LocaleCompare(token,
"pop") != 0)
3374 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3375 if (LocaleCompare(token,
"class") != 0)
3379 (void) GetNextToken(q,&q,extent,token);
3382 if (LocaleCompare(
"clip-path",token) == 0)
3384 (void) GetNextToken(q,&q,extent,token);
3385 for (p=q; *q !=
'\0'; )
3387 if (GetNextToken(q,&q,extent,token) < 1)
3389 if (LocaleCompare(token,
"pop") != 0)
3391 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3392 if (LocaleCompare(token,
"clip-path") != 0)
3396 if ((q == (
char *) NULL) || (p == (
char *) NULL) || ((q-4) < p))
3401 (void) GetNextToken(q,&q,extent,token);
3404 if (LocaleCompare(
"defs",token) == 0)
3407 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3411 if (LocaleCompare(
"gradient",token) == 0)
3414 key[2*MagickPathExtent],
3415 name[MagickPathExtent],
3416 type[MagickPathExtent];
3421 (void) GetNextToken(q,&q,extent,token);
3422 (void) CopyMagickString(name,token,MagickPathExtent);
3423 (void) GetNextToken(q,&q,extent,token);
3424 (void) CopyMagickString(type,token,MagickPathExtent);
3425 (void) GetNextToken(q,&q,extent,token);
3426 segment.x1=GetDrawValue(token,&next_token);
3427 if (token == next_token)
3428 ThrowPointExpectedException(token,exception);
3429 (void) GetNextToken(q,&q,extent,token);
3431 (void) GetNextToken(q,&q,extent,token);
3432 segment.y1=GetDrawValue(token,&next_token);
3433 if (token == next_token)
3434 ThrowPointExpectedException(token,exception);
3435 (void) GetNextToken(q,&q,extent,token);
3437 (void) GetNextToken(q,&q,extent,token);
3438 segment.x2=GetDrawValue(token,&next_token);
3439 if (token == next_token)
3440 ThrowPointExpectedException(token,exception);
3441 (void) GetNextToken(q,&q,extent,token);
3443 (void) GetNextToken(q,&q,extent,token);
3444 segment.y2=GetDrawValue(token,&next_token);
3445 if (token == next_token)
3446 ThrowPointExpectedException(token,exception);
3447 if (LocaleCompare(type,
"radial") == 0)
3449 (void) GetNextToken(q,&q,extent,token);
3451 (void) GetNextToken(q,&q,extent,token);
3453 for (p=q; *q !=
'\0'; )
3455 if (GetNextToken(q,&q,extent,token) < 1)
3457 if (LocaleCompare(token,
"pop") != 0)
3459 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3460 if (LocaleCompare(token,
"gradient") != 0)
3464 if ((q == (
char *) NULL) || (*q ==
'\0') ||
3465 (p == (
char *) NULL) || ((q-4) < p) ||
3466 ((
size_t) (q-p+4+1) > extent))
3471 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3472 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3473 graphic_context[n]->affine.ry*segment.y1+
3474 graphic_context[n]->affine.tx;
3475 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3476 graphic_context[n]->affine.sy*segment.y1+
3477 graphic_context[n]->affine.ty;
3478 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3479 graphic_context[n]->affine.ry*segment.y2+
3480 graphic_context[n]->affine.tx;
3481 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3482 graphic_context[n]->affine.sy*segment.y2+
3483 graphic_context[n]->affine.ty;
3484 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3485 (void) SetImageArtifact(image,key,token);
3486 (void) FormatLocaleString(key,MagickPathExtent,
"%s-type",name);
3487 (void) SetImageArtifact(image,key,type);
3488 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3490 (void) FormatLocaleString(geometry,MagickPathExtent,
3491 "%gx%g%+.15g%+.15g",
3492 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3493 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3494 bounds.x1,bounds.y1);
3495 (void) SetImageArtifact(image,key,geometry);
3496 (void) GetNextToken(q,&q,extent,token);
3499 if (LocaleCompare(
"graphic-context",token) == 0)
3502 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3503 graphic_context,(
size_t) (n+1),
sizeof(*graphic_context));
3504 if (graphic_context == (DrawInfo **) NULL)
3506 (void) ThrowMagickException(exception,GetMagickModule(),
3507 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3512 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3513 graphic_context[n-1]);
3516 (void) GetNextToken(q,&q,extent,token);
3517 (void) CloneString(&graphic_context[n]->
id,token);
3519 if (n > MagickMaxRecursionDepth)
3521 (void) ThrowMagickException(exception,GetMagickModule(),
3522 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",
3528 if (LocaleCompare(
"mask",token) == 0)
3530 (void) GetNextToken(q,&q,extent,token);
3533 if (LocaleCompare(
"pattern",token) == 0)
3536 key[2*MagickPathExtent],
3537 name[MagickPathExtent];
3542 (void) GetNextToken(q,&q,extent,token);
3543 (void) CopyMagickString(name,token,MagickPathExtent);
3544 (void) GetNextToken(q,&q,extent,token);
3545 region.x=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3547 if (token == next_token)
3548 ThrowPointExpectedException(token,exception);
3549 (void) GetNextToken(q,&q,extent,token);
3551 (void) GetNextToken(q,&q,extent,token);
3552 region.y=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3554 if (token == next_token)
3555 ThrowPointExpectedException(token,exception);
3556 (void) GetNextToken(q,&q,extent,token);
3558 (void) GetNextToken(q,&q,extent,token);
3559 region.width=CastDoubleToSizeT(floor(GetDrawValue(token,
3561 if (token == next_token)
3562 ThrowPointExpectedException(token,exception);
3563 (void) GetNextToken(q,&q,extent,token);
3565 (void) GetNextToken(q,&q,extent,token);
3566 region.height=CastDoubleToSizeT(GetDrawValue(token,
3568 if (token == next_token)
3569 ThrowPointExpectedException(token,exception);
3570 for (p=q; *q !=
'\0'; )
3572 if (GetNextToken(q,&q,extent,token) < 1)
3574 if (LocaleCompare(token,
"pop") != 0)
3576 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3577 if (LocaleCompare(token,
"pattern") != 0)
3581 if ((q == (
char *) NULL) || (p == (
char *) NULL) ||
3582 ((q-4) < p) || ((
size_t) (q-p+4+1) > extent))
3587 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3588 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3589 (void) SetImageArtifact(image,key,token);
3590 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3592 (void) FormatLocaleString(geometry,MagickPathExtent,
3593 "%.20gx%.20g%+.20g%+.20g",(
double) region.width,(
double)
3594 region.height,(
double) region.x,(
double) region.y);
3595 (void) SetImageArtifact(image,key,geometry);
3596 (void) GetNextToken(q,&q,extent,token);
3599 if (LocaleCompare(
"symbol",token) == 0)
3602 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3615 if (LocaleCompare(
"rectangle",keyword) == 0)
3617 primitive_type=RectanglePrimitive;
3620 if (LocaleCompare(
"rotate",keyword) == 0)
3622 (void) GetNextToken(q,&q,extent,token);
3623 angle=GetDrawValue(token,&next_token);
3624 if (token == next_token)
3625 ThrowPointExpectedException(token,exception);
3626 affine.sx=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3627 affine.rx=sin(DegreesToRadians(fmod((
double) angle,360.0)));
3628 affine.ry=(-sin(DegreesToRadians(fmod((
double) angle,360.0))));
3629 affine.sy=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3632 if (LocaleCompare(
"roundRectangle",keyword) == 0)
3634 primitive_type=RoundRectanglePrimitive;
3643 if (LocaleCompare(
"scale",keyword) == 0)
3645 (void) GetNextToken(q,&q,extent,token);
3646 affine.sx=GetDrawValue(token,&next_token);
3647 if (token == next_token)
3648 ThrowPointExpectedException(token,exception);
3649 (void) GetNextToken(q,&q,extent,token);
3651 (void) GetNextToken(q,&q,extent,token);
3652 affine.sy=GetDrawValue(token,&next_token);
3653 if (token == next_token)
3654 ThrowPointExpectedException(token,exception);
3657 if (LocaleCompare(
"skewX",keyword) == 0)
3659 (void) GetNextToken(q,&q,extent,token);
3660 angle=GetDrawValue(token,&next_token);
3661 if (token == next_token)
3662 ThrowPointExpectedException(token,exception);
3663 affine.ry=sin(DegreesToRadians(angle));
3666 if (LocaleCompare(
"skewY",keyword) == 0)
3668 (void) GetNextToken(q,&q,extent,token);
3669 angle=GetDrawValue(token,&next_token);
3670 if (token == next_token)
3671 ThrowPointExpectedException(token,exception);
3672 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3675 if (LocaleCompare(
"stop-color",keyword) == 0)
3681 if (number_stops == 1)
3682 stops=(StopInfo *) AcquireQuantumMemory(2,
sizeof(*stops));
3684 if (number_stops > 2)
3685 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3687 if (stops == (StopInfo *) NULL)
3689 (void) ThrowMagickException(exception,GetMagickModule(),
3690 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3695 (void) GetNextToken(q,&q,extent,token);
3696 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3697 &stop_color,exception);
3698 stops[number_stops-1].color=stop_color;
3699 (void) GetNextToken(q,&q,extent,token);
3700 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3701 stops[number_stops-1].offset=factor*GetDrawValue(token,
3703 if (token == next_token)
3704 ThrowPointExpectedException(token,exception);
3707 if (LocaleCompare(
"stroke",keyword) == 0)
3712 (void) GetNextToken(q,&q,extent,token);
3713 if (graphic_context[n]->clip_path != MagickFalse)
3715 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
3716 if (mvg_class != (
const char *) NULL)
3718 (void) DrawPatternPath(image,draw_info,mvg_class,
3719 &graphic_context[n]->stroke_pattern,exception);
3722 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
3723 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
3725 (void) DrawPatternPath(image,draw_info,token,
3726 &graphic_context[n]->stroke_pattern,exception);
3729 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3730 &graphic_context[n]->stroke,exception);
3731 if (graphic_context[n]->stroke_alpha != (
double) OpaqueAlpha)
3732 graphic_context[n]->stroke.alpha=
3733 graphic_context[n]->stroke_alpha;
3736 if (LocaleCompare(
"stroke-antialias",keyword) == 0)
3738 (void) GetNextToken(q,&q,extent,token);
3739 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3740 MagickTrue : MagickFalse;
3743 if (LocaleCompare(
"stroke-dasharray",keyword) == 0)
3745 if (graphic_context[n]->dash_pattern != (
double *) NULL)
3746 graphic_context[n]->dash_pattern=(
double *)
3747 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3748 if (IsPoint(q) != MagickFalse)
3754 (void) GetNextToken(r,&r,extent,token);
3756 (void) GetNextToken(r,&r,extent,token);
3757 for (x=0; IsPoint(token) != MagickFalse; x++)
3759 (void) GetNextToken(r,&r,extent,token);
3761 (void) GetNextToken(r,&r,extent,token);
3763 graphic_context[n]->dash_pattern=(
double *)
3764 AcquireQuantumMemory((
size_t) (2*x+2),
3765 sizeof(*graphic_context[n]->dash_pattern));
3766 if (graphic_context[n]->dash_pattern == (
double *) NULL)
3768 (void) ThrowMagickException(exception,GetMagickModule(),
3769 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3774 (void) memset(graphic_context[n]->dash_pattern,0,(
size_t)
3775 (2*x+2)*
sizeof(*graphic_context[n]->dash_pattern));
3776 for (j=0; j < x; j++)
3778 (void) GetNextToken(q,&q,extent,token);
3780 (void) GetNextToken(q,&q,extent,token);
3781 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3783 if (token == next_token)
3784 ThrowPointExpectedException(token,exception);
3785 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3788 if ((x & 0x01) != 0)
3789 for ( ; j < (2*x); j++)
3790 graphic_context[n]->dash_pattern[j]=
3791 graphic_context[n]->dash_pattern[j-x];
3792 graphic_context[n]->dash_pattern[j]=0.0;
3795 (void) GetNextToken(q,&q,extent,token);
3798 if (LocaleCompare(
"stroke-dashoffset",keyword) == 0)
3800 (void) GetNextToken(q,&q,extent,token);
3801 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3802 if (token == next_token)
3803 ThrowPointExpectedException(token,exception);
3806 if (LocaleCompare(
"stroke-linecap",keyword) == 0)
3811 (void) GetNextToken(q,&q,extent,token);
3812 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3818 graphic_context[n]->linecap=(LineCap) linecap;
3821 if (LocaleCompare(
"stroke-linejoin",keyword) == 0)
3826 (void) GetNextToken(q,&q,extent,token);
3827 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3834 graphic_context[n]->linejoin=(LineJoin) linejoin;
3837 if (LocaleCompare(
"stroke-miterlimit",keyword) == 0)
3839 (void) GetNextToken(q,&q,extent,token);
3840 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3843 if (LocaleCompare(
"stroke-opacity",keyword) == 0)
3848 (void) GetNextToken(q,&q,extent,token);
3849 if (graphic_context[n]->clip_path != MagickFalse)
3851 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3852 opacity=MagickMin(MagickMax(factor*GetDrawValue(token,&next_token),
3854 if (token == next_token)
3855 ThrowPointExpectedException(token,exception);
3856 if (graphic_context[n]->compliance == SVGCompliance)
3857 graphic_context[n]->stroke_alpha*=opacity;
3859 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3860 if (graphic_context[n]->stroke.alpha != (
double) TransparentAlpha)
3861 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3863 graphic_context[n]->stroke.alpha=(MagickRealType)
3864 ClampToQuantum((
double) QuantumRange*opacity);
3865 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3868 if (LocaleCompare(
"stroke-width",keyword) == 0)
3870 (void) GetNextToken(q,&q,extent,token);
3871 if (graphic_context[n]->clip_path != MagickFalse)
3873 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3874 if ((token == next_token) ||
3875 (graphic_context[n]->stroke_width < 0.0))
3876 ThrowPointExpectedException(token,exception);
3885 if (LocaleCompare(
"text",keyword) == 0)
3887 primitive_type=TextPrimitive;
3891 if (LocaleCompare(
"text-align",keyword) == 0)
3896 (void) GetNextToken(q,&q,extent,token);
3897 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3903 graphic_context[n]->align=(AlignType) align;
3906 if (LocaleCompare(
"text-anchor",keyword) == 0)
3911 (void) GetNextToken(q,&q,extent,token);
3912 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3918 graphic_context[n]->align=(AlignType) align;
3921 if (LocaleCompare(
"text-antialias",keyword) == 0)
3923 (void) GetNextToken(q,&q,extent,token);
3924 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3925 MagickTrue : MagickFalse;
3928 if (LocaleCompare(
"text-undercolor",keyword) == 0)
3930 (void) GetNextToken(q,&q,extent,token);
3931 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3932 &graphic_context[n]->undercolor,exception);
3935 if (LocaleCompare(
"translate",keyword) == 0)
3937 (void) GetNextToken(q,&q,extent,token);
3938 affine.tx=GetDrawValue(token,&next_token);
3939 if (token == next_token)
3940 ThrowPointExpectedException(token,exception);
3941 (void) GetNextToken(q,&q,extent,token);
3943 (void) GetNextToken(q,&q,extent,token);
3944 affine.ty=GetDrawValue(token,&next_token);
3945 if (token == next_token)
3946 ThrowPointExpectedException(token,exception);
3956 if (LocaleCompare(
"use",keyword) == 0)
3964 (void) GetNextToken(q,&q,extent,token);
3965 use=(
const char *) GetValueFromSplayTree(macros,token);
3966 if (use != (
const char *) NULL)
3968 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3969 (void) CloneString(&clone_info->primitive,use);
3970 status=RenderMVGContent(image,clone_info,depth+1,exception);
3971 clone_info=DestroyDrawInfo(clone_info);
3981 if (LocaleCompare(
"viewbox",keyword) == 0)
3983 (void) GetNextToken(q,&q,extent,token);
3984 graphic_context[n]->viewbox.x=CastDoubleToSsizeT(ceil(
3985 GetDrawValue(token,&next_token)-0.5));
3986 if (token == next_token)
3987 ThrowPointExpectedException(token,exception);
3988 (void) GetNextToken(q,&q,extent,token);
3990 (void) GetNextToken(q,&q,extent,token);
3991 graphic_context[n]->viewbox.y=CastDoubleToSsizeT(
3992 ceil(GetDrawValue(token,&next_token)-0.5));
3993 if (token == next_token)
3994 ThrowPointExpectedException(token,exception);
3995 (void) GetNextToken(q,&q,extent,token);
3997 (void) GetNextToken(q,&q,extent,token);
3998 graphic_context[n]->viewbox.width=CastDoubleToSizeT(floor(
3999 GetDrawValue(token,&next_token)+0.5));
4000 if (token == next_token)
4001 ThrowPointExpectedException(token,exception);
4002 (void) GetNextToken(q,&q,extent,token);
4004 (void) GetNextToken(q,&q,extent,token);
4005 graphic_context[n]->viewbox.height=CastDoubleToSizeT(floor(
4006 GetDrawValue(token,&next_token)+0.5));
4007 if (token == next_token)
4008 ThrowPointExpectedException(token,exception);
4017 if (LocaleCompare(
"word-spacing",keyword) == 0)
4019 (void) GetNextToken(q,&q,extent,token);
4020 graphic_context[n]->interword_spacing=GetDrawValue(token,
4022 if (token == next_token)
4023 ThrowPointExpectedException(token,exception);
4035 if (status == MagickFalse)
4037 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
4038 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
4039 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
4040 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
4042 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
4043 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
4044 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
4045 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
4046 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
4048 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4051 if (primitive_type == UndefinedPrimitive)
4055 if (number_stops > 1)
4060 type=LinearGradient;
4061 if (draw_info->gradient.type == RadialGradient)
4062 type=RadialGradient;
4063 (void) GradientImage(image,type,PadSpread,stops,number_stops,
4066 if (number_stops > 0)
4067 stops=(StopInfo *) RelinquishMagickMemory(stops);
4069 if ((draw_info->debug != MagickFalse) && (q > p))
4070 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int)
4077 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4078 if (primitive_info[i].text != (
char *) NULL)
4079 primitive_info[i].text=DestroyString(primitive_info[i].text);
4083 primitive_info[0].primitive=primitive_type;
4084 primitive_info[0].point.x=0.0;
4085 primitive_info[0].point.y=0.0;
4086 primitive_info[0].coordinates=0;
4087 primitive_info[0].method=FloodfillMethod;
4088 primitive_info[0].closed_subpath=MagickFalse;
4089 for (x=0; *q !=
'\0'; x++)
4094 if (IsPoint(q) == MagickFalse)
4096 (void) GetNextToken(q,&q,extent,token);
4097 point.x=GetDrawValue(token,&next_token);
4098 if (token == next_token)
4099 ThrowPointExpectedException(token,exception);
4100 (void) GetNextToken(q,&q,extent,token);
4102 (void) GetNextToken(q,&q,extent,token);
4103 point.y=GetDrawValue(token,&next_token);
4104 if (token == next_token)
4105 ThrowPointExpectedException(token,exception);
4106 (void) GetNextToken(q,(
const char **) NULL,extent,token);
4108 (void) GetNextToken(q,&q,extent,token);
4109 primitive_info[i].primitive=primitive_type;
4110 primitive_info[i].point=point;
4111 primitive_info[i].coordinates=0;
4112 primitive_info[i].method=FloodfillMethod;
4113 primitive_info[i].closed_subpath=MagickFalse;
4116 if (i < (ssize_t) number_points)
4118 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4120 primitive_info=(*mvg_info.primitive_info);
4122 if (status == MagickFalse)
4124 if (primitive_info[j].text != (
char *) NULL)
4125 primitive_info[j].text=DestroyString(primitive_info[j].text);
4126 primitive_info[j].primitive=primitive_type;
4127 primitive_info[j].coordinates=(size_t) x;
4128 primitive_info[j].method=FloodfillMethod;
4129 primitive_info[j].closed_subpath=MagickFalse;
4133 bounds.x1=primitive_info[j].point.x;
4134 bounds.y1=primitive_info[j].point.y;
4135 bounds.x2=primitive_info[j].point.x;
4136 bounds.y2=primitive_info[j].point.y;
4137 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4139 point=primitive_info[j+k].point;
4140 if (point.x < bounds.x1)
4142 if (point.y < bounds.y1)
4144 if (point.x > bounds.x2)
4146 if (point.y > bounds.y2)
4152 coordinates=(double) primitive_info[j].coordinates;
4153 switch (primitive_type)
4155 case RectanglePrimitive:
4160 case RoundRectanglePrimitive:
4167 alpha=bounds.x2-bounds.x1;
4168 beta=bounds.y2-bounds.y1;
4169 radius=hypot(alpha,beta);
4171 coordinates+=2.0*((size_t) ceil((
double) MagickPI*radius))+6.0*
4172 BezierQuantum+360.0;
4175 case BezierPrimitive:
4177 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4186 (void) GetNextToken(q,&q,extent,token);
4189 for (s=token; *s !=
'\0'; s=t)
4194 value=GetDrawValue(s,&t);
4203 for (s=token; *s !=
'\0'; s++)
4204 if (strspn(s,
"AaCcQqSsTt") != 0)
4205 coordinates+=(20.0*BezierQuantum)+360.0;
4211 if (status == MagickFalse)
4213 if (((
size_t) (i+coordinates)) >= number_points)
4218 number_points+=(size_t) coordinates+1;
4219 if (number_points < (
size_t) coordinates)
4221 (void) ThrowMagickException(exception,GetMagickModule(),
4222 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
4228 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4230 primitive_info=(*mvg_info.primitive_info);
4232 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,
4233 PrimitiveExtentPad);
4234 primitive_info=(*mvg_info.primitive_info);
4235 if (status == MagickFalse)
4238 switch (primitive_type)
4240 case PointPrimitive:
4243 if (primitive_info[j].coordinates != 1)
4248 status&=(MagickStatusType) TracePoint(primitive_info+j,
4249 primitive_info[j].point);
4250 primitive_info=(*mvg_info.primitive_info);
4251 i=j+(ssize_t) primitive_info[j].coordinates;
4256 if (primitive_info[j].coordinates != 2)
4261 status&=(MagickStatusType) TraceLine(primitive_info+j,
4262 primitive_info[j].point,primitive_info[j+1].point);
4263 primitive_info=(*mvg_info.primitive_info);
4264 i=j+(ssize_t) primitive_info[j].coordinates;
4267 case RectanglePrimitive:
4269 if (primitive_info[j].coordinates != 2)
4274 status&=(MagickStatusType) TraceRectangle(primitive_info+j,
4275 primitive_info[j].point,primitive_info[j+1].point);
4276 primitive_info=(*mvg_info.primitive_info);
4277 i=j+(ssize_t) primitive_info[j].coordinates;
4280 case RoundRectanglePrimitive:
4282 if (primitive_info[j].coordinates != 3)
4287 if ((primitive_info[j+2].point.x < 0.0) ||
4288 (primitive_info[j+2].point.y < 0.0))
4293 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4298 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4303 status&=(MagickStatusType) TraceRoundRectangle(&mvg_info,
4304 primitive_info[j].point,primitive_info[j+1].point,
4305 primitive_info[j+2].point);
4306 primitive_info=(*mvg_info.primitive_info);
4307 i=j+(ssize_t) primitive_info[j].coordinates;
4312 if (primitive_info[j].coordinates != 3)
4317 status&=(MagickStatusType) TraceArc(&mvg_info,primitive_info[j].point,
4318 primitive_info[j+1].point,primitive_info[j+2].point);
4319 primitive_info=(*mvg_info.primitive_info);
4320 i=j+(ssize_t) primitive_info[j].coordinates;
4323 case EllipsePrimitive:
4325 if (primitive_info[j].coordinates != 3)
4330 if ((primitive_info[j+1].point.x < 0.0) ||
4331 (primitive_info[j+1].point.y < 0.0))
4336 status&=(MagickStatusType) TraceEllipse(&mvg_info,
4337 primitive_info[j].point,primitive_info[j+1].point,
4338 primitive_info[j+2].point);
4339 primitive_info=(*mvg_info.primitive_info);
4340 i=j+(ssize_t) primitive_info[j].coordinates;
4343 case CirclePrimitive:
4345 if (primitive_info[j].coordinates != 2)
4350 status&=(MagickStatusType) TraceCircle(&mvg_info,
4351 primitive_info[j].point,primitive_info[j+1].point);
4352 primitive_info=(*mvg_info.primitive_info);
4353 i=j+(ssize_t) primitive_info[j].coordinates;
4356 case PolylinePrimitive:
4358 if (primitive_info[j].coordinates < 1)
4365 case PolygonPrimitive:
4367 if (primitive_info[j].coordinates < 3)
4372 primitive_info[i]=primitive_info[j];
4373 primitive_info[i].coordinates=0;
4374 primitive_info[j].coordinates++;
4375 primitive_info[j].closed_subpath=MagickTrue;
4379 case BezierPrimitive:
4381 if (primitive_info[j].coordinates < 3)
4386 status&=(MagickStatusType) TraceBezier(&mvg_info,
4387 primitive_info[j].coordinates);
4388 primitive_info=(*mvg_info.primitive_info);
4389 i=j+(ssize_t) primitive_info[j].coordinates;
4394 coordinates=(double) TracePath(&mvg_info,token,exception);
4395 primitive_info=(*mvg_info.primitive_info);
4396 if (coordinates < 0.0)
4401 i=(ssize_t) (j+coordinates);
4404 case AlphaPrimitive:
4405 case ColorPrimitive:
4410 if (primitive_info[j].coordinates != 1)
4415 (void) GetNextToken(q,&q,extent,token);
4416 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4422 primitive_info[j].method=(PaintMethod) method;
4427 if (primitive_info[j].coordinates != 1)
4433 (void) GetNextToken(q,&q,extent,token);
4434 (void) CloneString(&primitive_info[j].text,token);
4438 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4439 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4440 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4442 mvg_info.point=primitive_info->point;
4443 primitive_info->point.x+=cursor;
4447 mvg_info.point=primitive_info->point;
4450 clone_info->render=MagickFalse;
4451 clone_info->text=AcquireString(token);
4452 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,
4453 &metrics,exception);
4454 clone_info=DestroyDrawInfo(clone_info);
4455 cursor+=metrics.width;
4456 if (graphic_context[n]->compliance != SVGCompliance)
4460 case ImagePrimitive:
4462 if (primitive_info[j].coordinates != 2)
4467 (void) GetNextToken(q,&q,extent,token);
4468 (void) CloneString(&primitive_info[j].text,token);
4475 primitive_info[i].primitive=UndefinedPrimitive;
4476 if ((draw_info->debug != MagickFalse) && (q > p))
4477 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int) (q-p),p);
4481 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4482 &graphic_context[n]->affine));
4483 primitive_info=(*mvg_info.primitive_info);
4486 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4487 graphic_context[n]->stroke_width);
4488 primitive_info=(*mvg_info.primitive_info);
4496 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4498 point=primitive_info[i].point;
4499 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4500 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4501 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4502 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4503 point=primitive_info[i].point;
4504 if (point.x < graphic_context[n]->bounds.x1)
4505 graphic_context[n]->bounds.x1=point.x;
4506 if (point.y < graphic_context[n]->bounds.y1)
4507 graphic_context[n]->bounds.y1=point.y;
4508 if (point.x > graphic_context[n]->bounds.x2)
4509 graphic_context[n]->bounds.x2=point.x;
4510 if (point.y > graphic_context[n]->bounds.y2)
4511 graphic_context[n]->bounds.y2=point.y;
4512 if (primitive_info[i].primitive == ImagePrimitive)
4514 if (i >= (ssize_t) number_points)
4515 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
4517 if (graphic_context[n]->render != MagickFalse)
4519 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4520 (graphic_context[n]->clip_mask != (
char *) NULL) &&
4521 (LocaleCompare(graphic_context[n]->clip_mask,
4522 graphic_context[n-1]->clip_mask) != 0))
4527 clip_path=(
const char *) GetValueFromSplayTree(macros,
4528 graphic_context[n]->clip_mask);
4529 if (clip_path != (
const char *) NULL)
4530 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4532 status&=(MagickStatusType) DrawClipPath(image,graphic_context[n],
4533 graphic_context[n]->clip_mask,exception);
4535 status&=(MagickStatusType) DrawPrimitive(image,graphic_context[n],
4536 primitive_info,exception);
4538 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4540 if (proceed == MagickFalse)
4545 if (draw_info->debug != MagickFalse)
4546 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end draw-image");
4550 macros=DestroySplayTree(macros);
4551 token=DestroyString(token);
4552 if (primitive_info != (PrimitiveInfo *) NULL)
4554 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4555 if (primitive_info[i].text != (
char *) NULL)
4556 primitive_info[i].text=DestroyString(primitive_info[i].text);
4557 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4559 primitive=DestroyString(primitive);
4560 if (stops != (StopInfo *) NULL)
4561 stops=(StopInfo *) RelinquishMagickMemory(stops);
4562 for ( ; n >= 0; n--)
4563 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4564 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4565 if ((status == MagickFalse) && (exception->severity < ErrorException))
4566 ThrowBinaryException(DrawError,
"NonconformingDrawingPrimitiveDefinition",
4568 return(status != 0 ? MagickTrue : MagickFalse);
4571MagickExport MagickBooleanType DrawImage(Image *image,
const DrawInfo *draw_info,
4572 ExceptionInfo *exception)
4574 return(RenderMVGContent(image,draw_info,0,exception));
4608MagickExport MagickBooleanType DrawPatternPath(Image *image,
4609 const DrawInfo *draw_info,
const char *name,Image **pattern,
4610 ExceptionInfo *exception)
4613 property[MagickPathExtent];
4629 assert(image != (Image *) NULL);
4630 assert(image->signature == MagickCoreSignature);
4631 assert(draw_info != (
const DrawInfo *) NULL);
4632 assert(name != (
const char *) NULL);
4633 if (IsEventLogging() != MagickFalse)
4634 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4635 (void) FormatLocaleString(property,MagickPathExtent,
"%s",name);
4636 path=GetImageArtifact(image,property);
4637 if (path == (
const char *) NULL)
4638 return(MagickFalse);
4639 (void) FormatLocaleString(property,MagickPathExtent,
"%s-geometry",name);
4640 geometry=GetImageArtifact(image,property);
4641 if (geometry == (
const char *) NULL)
4642 return(MagickFalse);
4643 if ((*pattern) != (Image *) NULL)
4644 *pattern=DestroyImage(*pattern);
4645 image_info=AcquireImageInfo();
4646 image_info->size=AcquireString(geometry);
4647 *pattern=AcquireImage(image_info,exception);
4648 image_info=DestroyImageInfo(image_info);
4649 (void) QueryColorCompliance(
"#00000000",AllCompliance,
4650 &(*pattern)->background_color,exception);
4651 (void) SetImageBackgroundColor(*pattern,exception);
4652 if (draw_info->debug != MagickFalse)
4653 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4654 "begin pattern-path %s %s",name,geometry);
4655 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4656 if (clone_info->fill_pattern != (Image *) NULL)
4657 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4658 if (clone_info->stroke_pattern != (Image *) NULL)
4659 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4660 (void) FormatLocaleString(property,MagickPathExtent,
"%s-type",name);
4661 type=GetImageArtifact(image,property);
4662 if (type != (
const char *) NULL)
4663 clone_info->gradient.type=(GradientType) ParseCommandOption(
4664 MagickGradientOptions,MagickFalse,type);
4665 (void) CloneString(&clone_info->primitive,path);
4666 status=RenderMVGContent(*pattern,clone_info,0,exception);
4667 clone_info=DestroyDrawInfo(clone_info);
4668 if (draw_info->debug != MagickFalse)
4669 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end pattern-path");
4704static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4709 assert(polygon_info != (PolygonInfo **) NULL);
4710 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4711 if (polygon_info[i] != (PolygonInfo *) NULL)
4712 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4713 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4714 return(polygon_info);
4717static PolygonInfo **AcquirePolygonTLS(
const PrimitiveInfo *primitive_info,
4718 ExceptionInfo *exception)
4721 *magick_restrict path_info;
4729 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4730 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4731 sizeof(*polygon_info));
4732 if (polygon_info == (PolygonInfo **) NULL)
4734 (void) ThrowMagickException(exception,GetMagickModule(),
4735 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4736 return((PolygonInfo **) NULL);
4738 (void) memset(polygon_info,0,number_threads*
sizeof(*polygon_info));
4739 path_info=ConvertPrimitiveToPath(primitive_info,exception);
4740 if (path_info == (PathInfo *) NULL)
4741 return(DestroyPolygonTLS(polygon_info));
4742 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4743 if (polygon_info[0] == (PolygonInfo *) NULL)
4745 (void) ThrowMagickException(exception,GetMagickModule(),
4746 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4747 return(DestroyPolygonTLS(polygon_info));
4749 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4750 return(polygon_info);
4753static MagickBooleanType ClonePolygonEdgesTLS(PolygonInfo **polygon_info,
4754 const size_t number_threads,ExceptionInfo *exception)
4759 for (i=1; i < (ssize_t) number_threads; i++)
4767 polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4768 sizeof(*polygon_info[i]));
4769 if (polygon_info[i] == (PolygonInfo *) NULL)
4771 (void) ThrowMagickException(exception,GetMagickModule(),
4772 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4773 return(MagickFalse);
4775 polygon_info[i]->number_edges=0;
4776 edge_info=polygon_info[0]->edges;
4777 polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4778 polygon_info[0]->number_edges,
sizeof(*edge_info));
4779 if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4781 (void) ThrowMagickException(exception,GetMagickModule(),
4782 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4783 return(MagickFalse);
4785 (void) memcpy(polygon_info[i]->edges,edge_info,
4786 polygon_info[0]->number_edges*
sizeof(*edge_info));
4787 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4788 polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4789 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4790 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4792 edge_info=polygon_info[0]->edges+j;
4793 polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4794 edge_info->number_points,
sizeof(*edge_info));
4795 if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4797 (void) ThrowMagickException(exception,GetMagickModule(),
4798 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4799 return(MagickFalse);
4801 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4802 edge_info->number_points*
sizeof(*edge_info->points));
4808static size_t DestroyEdge(PolygonInfo *polygon_info,
const ssize_t edge)
4810 assert(edge < (ssize_t) polygon_info->number_edges);
4811 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4812 polygon_info->edges[edge].points);
4813 polygon_info->number_edges--;
4814 if (edge < (ssize_t) polygon_info->number_edges)
4815 (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4816 (polygon_info->number_edges-(
size_t) edge)*
sizeof(*polygon_info->edges));
4817 return(polygon_info->number_edges);
4820static double GetFillAlpha(PolygonInfo *polygon_info,
const double mid,
4821 const MagickBooleanType fill,
const FillRule fill_rule,
const ssize_t x,
4822 const ssize_t y,
double *stroke_alpha)
4849 p=polygon_info->edges;
4850 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4852 if ((
double) y <= (p->bounds.y1-mid-0.5))
4854 if ((
double) y > (p->bounds.y2+mid+0.5))
4857 (void) DestroyEdge(polygon_info,j--);
4860 if (((
double) x <= (p->bounds.x1-mid-0.5)) ||
4861 ((
double) x > (p->bounds.x2+mid+0.5)))
4863 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4864 for ( ; i < (ssize_t) p->number_points; i++)
4866 if ((
double) y <= (p->points[i-1].y-mid-0.5))
4868 if ((
double) y > (p->points[i].y+mid+0.5))
4870 if (p->scanline != (
double) y)
4872 p->scanline=(double) y;
4873 p->highwater=(size_t) i;
4879 delta.x=(q+1)->x-q->x;
4880 delta.y=(q+1)->y-q->y;
4881 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4887 delta.x=(double) x-q->x;
4888 delta.y=(double) y-q->y;
4889 distance=delta.x*delta.x+delta.y*delta.y;
4893 alpha=delta.x*delta.x+delta.y*delta.y;
4899 delta.x=(double) x-(q+1)->x;
4900 delta.y=(double) y-(q+1)->y;
4901 distance=delta.x*delta.x+delta.y*delta.y;
4908 alpha=MagickSafeReciprocal(alpha);
4909 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4910 distance=alpha*beta*beta;
4917 if (p->ghostline == MagickFalse)
4920 if ((*stroke_alpha < 1.0) &&
4921 (distance <= ((alpha+0.25)*(alpha+0.25))))
4924 if (distance <= ((alpha+0.25)*(alpha+0.25)))
4929 if (fabs(distance-1.0) >= MagickEpsilon)
4930 beta=sqrt((
double) distance);
4932 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
4933 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
4937 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
4939 if (distance <= 0.0)
4946 if (fabs(beta) < MagickEpsilon)
4949 if (fabs(distance-1.0) >= MagickEpsilon)
4950 beta=sqrt(distance);
4953 if (subpath_alpha < (alpha*alpha))
4954 subpath_alpha=alpha*alpha;
4960 if (fill == MagickFalse)
4962 if (subpath_alpha >= 1.0)
4968 p=polygon_info->edges;
4969 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4971 if ((
double) y <= p->bounds.y1)
4973 if (((
double) y > p->bounds.y2) || ((
double) x <= p->bounds.x1))
4975 if ((
double) x > p->bounds.x2)
4977 winding_number+=p->direction != 0 ? 1 : -1;
4980 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4981 for ( ; i < (ssize_t) (p->number_points-1); i++)
4982 if ((
double) y <= p->points[i].y)
4985 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4986 winding_number+=p->direction != 0 ? 1 : -1;
4988 if (fill_rule != NonZeroRule)
4990 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4994 if (MagickAbsoluteValue(winding_number) != 0)
4996 return(subpath_alpha);
4999static MagickBooleanType DrawPolygonPrimitive(Image *image,
5000 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5001 ExceptionInfo *exception)
5003 typedef struct _ExtentInfo
5032 **magick_restrict polygon_info;
5044 assert(image != (Image *) NULL);
5045 assert(image->signature == MagickCoreSignature);
5046 assert(draw_info != (DrawInfo *) NULL);
5047 assert(draw_info->signature == MagickCoreSignature);
5048 assert(primitive_info != (PrimitiveInfo *) NULL);
5049 if (IsEventLogging() != MagickFalse)
5050 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5051 if (primitive_info->coordinates <= 1)
5056 polygon_info=AcquirePolygonTLS(primitive_info,exception);
5057 if (polygon_info == (PolygonInfo **) NULL)
5058 return(MagickFalse);
5059 if (draw_info->debug != MagickFalse)
5060 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-polygon");
5061 fill=(primitive_info->method == FillToBorderMethod) ||
5062 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
5063 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5064 bounds=polygon_info[0]->edges[0].bounds;
5065 artifact=GetImageArtifact(image,
"draw:render-bounding-rectangles");
5066 if (IsStringTrue(artifact) != MagickFalse)
5067 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
5068 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
5070 p=polygon_info[0]->edges+i;
5071 if (p->bounds.x1 < bounds.x1)
5072 bounds.x1=p->bounds.x1;
5073 if (p->bounds.y1 < bounds.y1)
5074 bounds.y1=p->bounds.y1;
5075 if (p->bounds.x2 > bounds.x2)
5076 bounds.x2=p->bounds.x2;
5077 if (p->bounds.y2 > bounds.y2)
5078 bounds.y2=p->bounds.y2;
5080 bounds.x1-=(mid+1.0);
5081 bounds.y1-=(mid+1.0);
5082 bounds.x2+=(mid+1.0);
5083 bounds.y2+=(mid+1.0);
5084 if ((bounds.x1 >= (
double) image->columns) ||
5085 (bounds.y1 >= (
double) image->rows) ||
5086 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5088 polygon_info=DestroyPolygonTLS(polygon_info);
5091 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5092 (
double) image->columns-1.0 : bounds.x1;
5093 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5094 (
double) image->rows-1.0 : bounds.y1;
5095 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5096 (
double) image->columns-1.0 : bounds.x2;
5097 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5098 (
double) image->rows-1.0 : bounds.y2;
5099 poly_extent.x1=CastDoubleToSsizeT(ceil(bounds.x1-0.5));
5100 poly_extent.y1=CastDoubleToSsizeT(ceil(bounds.y1-0.5));
5101 poly_extent.x2=CastDoubleToSsizeT(floor(bounds.x2+0.5));
5102 poly_extent.y2=CastDoubleToSsizeT(floor(bounds.y2+0.5));
5103 number_threads=(size_t) GetMagickNumberThreads(image,image,(
size_t)
5104 (poly_extent.y2-poly_extent.y1+1),1);
5105 status=ClonePolygonEdgesTLS(polygon_info,number_threads,exception);
5106 if (status == MagickFalse)
5108 polygon_info=DestroyPolygonTLS(polygon_info);
5111 image_view=AcquireAuthenticCacheView(image,exception);
5112 if ((primitive_info->coordinates == 1) ||
5113 (polygon_info[0]->number_edges == 0))
5118#if defined(MAGICKCORE_OPENMP_SUPPORT)
5119 #pragma omp parallel for schedule(static) shared(status) \
5120 num_threads((int) number_threads)
5122 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5133 if (status == MagickFalse)
5136 q=GetCacheViewAuthenticPixels(image_view,x,y,(
size_t) (poly_extent.x2-
5138 if (q == (Quantum *) NULL)
5143 GetPixelInfo(image,&pixel);
5144 for ( ; x <= poly_extent.x2; x++)
5146 if ((x == CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5))) &&
5147 (y == CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5))))
5149 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&pixel,
5151 SetPixelViaPixelInfo(image,&pixel,q);
5153 q+=(ptrdiff_t) GetPixelChannels(image);
5155 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5158 image_view=DestroyCacheView(image_view);
5159 polygon_info=DestroyPolygonTLS(polygon_info);
5160 if (draw_info->debug != MagickFalse)
5161 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5162 " end draw-polygon");
5168#if defined(MAGICKCORE_OPENMP_SUPPORT)
5169 #pragma omp parallel for schedule(static) shared(status) \
5170 num_threads((int) number_threads)
5172 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5175 id = GetOpenMPThreadId();
5183 if (status == MagickFalse)
5185 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(
size_t)
5186 (poly_extent.x2-poly_extent.x1+1),1,exception);
5187 if (q == (Quantum *) NULL)
5192 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5205 fill_alpha=GetFillAlpha(polygon_info[
id],mid,fill,draw_info->fill_rule,
5207 if (draw_info->stroke_antialias == MagickFalse)
5209 fill_alpha=fill_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5210 stroke_alpha=stroke_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5212 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&fill_color,
5214 CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
5215 (
double) GetPixelAlpha(image,q),q);
5216 GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&stroke_color,
5218 CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
5219 (
double) GetPixelAlpha(image,q),q);
5220 q+=(ptrdiff_t) GetPixelChannels(image);
5222 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5225 image_view=DestroyCacheView(image_view);
5226 polygon_info=DestroyPolygonTLS(polygon_info);
5227 if (draw_info->debug != MagickFalse)
5228 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-polygon");
5261static void LogPrimitiveInfo(
const PrimitiveInfo *primitive_info)
5287 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5288 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5289 switch (primitive_info->primitive)
5291 case AlphaPrimitive:
5293 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5294 "AlphaPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5295 methods[primitive_info->method]);
5298 case ColorPrimitive:
5300 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5301 "ColorPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5302 methods[primitive_info->method]);
5305 case ImagePrimitive:
5307 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5308 "ImagePrimitive %.20g,%.20g",(
double) x,(
double) y);
5311 case PointPrimitive:
5313 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5314 "PointPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5315 methods[primitive_info->method]);
5320 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5321 "TextPrimitive %.20g,%.20g",(
double) x,(
double) y);
5328 p=primitive_info[0].point;
5331 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5333 point=primitive_info[i].point;
5334 if (coordinates <= 0)
5336 coordinates=(ssize_t) primitive_info[i].coordinates;
5337 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5338 " begin open (%.20g)",(
double) coordinates);
5341 point=primitive_info[i].point;
5342 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5343 (fabs(q.y-point.y) >= MagickEpsilon))
5344 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5345 " %.20g: %.18g,%.18g",(
double) coordinates,point.x,point.y);
5347 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
5348 " %.20g: %g %g (duplicate)",(
double) coordinates,point.x,point.y);
5351 if (coordinates > 0)
5353 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5354 (fabs(p.y-point.y) >= MagickEpsilon))
5355 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end last (%.20g)",
5356 (
double) coordinates);
5358 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end open (%.20g)",
5359 (
double) coordinates);
5363MagickExport MagickBooleanType DrawPrimitive(Image *image,
5364 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5365 ExceptionInfo *exception)
5380 if (draw_info->debug != MagickFalse)
5382 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5383 " begin draw-primitive");
5384 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5385 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5386 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5387 draw_info->affine.tx,draw_info->affine.ty);
5390 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5391 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5392 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5393 status&=(MagickStatusType) SetImageColorspace(image,sRGBColorspace,
5395 if (draw_info->compliance == SVGCompliance)
5397 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5398 draw_info->clipping_mask,exception);
5399 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5400 draw_info->composite_mask,exception);
5402 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5403 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5404 image_view=AcquireAuthenticCacheView(image,exception);
5405 switch (primitive_info->primitive)
5407 case AlphaPrimitive:
5409 if ((image->alpha_trait & BlendPixelTrait) == 0)
5410 status&=(MagickStatusType) SetImageAlphaChannel(image,
5411 OpaqueAlphaChannel,exception);
5412 switch (primitive_info->method)
5423 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5424 if (q == (Quantum *) NULL)
5426 GetFillColor(draw_info,x,y,&pixel,exception);
5427 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5428 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5438 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5439 x,y,&target,exception);
5440 GetPixelInfo(image,&pixel);
5441 for (y=0; y < (ssize_t) image->rows; y++)
5446 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5448 if (q == (Quantum *) NULL)
5450 for (x=0; x < (ssize_t) image->columns; x++)
5452 GetPixelInfoPixel(image,q,&pixel);
5453 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5455 q+=(ptrdiff_t) GetPixelChannels(image);
5458 GetFillColor(draw_info,x,y,&pixel,exception);
5459 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5460 q+=(ptrdiff_t) GetPixelChannels(image);
5462 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5464 if (status == MagickFalse)
5469 case FloodfillMethod:
5470 case FillToBorderMethod:
5478 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5479 TileVirtualPixelMethod,x,y,&target,exception);
5480 if (primitive_info->method == FillToBorderMethod)
5482 target.red=(double) draw_info->border_color.red;
5483 target.green=(double) draw_info->border_color.green;
5484 target.blue=(double) draw_info->border_color.blue;
5486 channel_mask=SetImageChannelMask(image,AlphaChannel);
5487 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5488 &target,x,y,primitive_info->method == FloodfillMethod ?
5489 MagickFalse : MagickTrue,exception);
5490 (void) SetImageChannelMask(image,channel_mask);
5498 for (y=0; y < (ssize_t) image->rows; y++)
5503 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5505 if (q == (Quantum *) NULL)
5507 for (x=0; x < (ssize_t) image->columns; x++)
5509 GetFillColor(draw_info,x,y,&pixel,exception);
5510 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5511 q+=(ptrdiff_t) GetPixelChannels(image);
5513 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5515 if (status == MagickFalse)
5523 case ColorPrimitive:
5525 switch (primitive_info->method)
5536 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5537 if (q == (Quantum *) NULL)
5539 GetPixelInfo(image,&pixel);
5540 GetFillColor(draw_info,x,y,&pixel,exception);
5541 SetPixelViaPixelInfo(image,&pixel,q);
5542 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5552 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5553 x,y,&target,exception);
5554 for (y=0; y < (ssize_t) image->rows; y++)
5559 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5561 if (q == (Quantum *) NULL)
5563 for (x=0; x < (ssize_t) image->columns; x++)
5565 GetPixelInfoPixel(image,q,&pixel);
5566 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5568 q+=(ptrdiff_t) GetPixelChannels(image);
5571 GetFillColor(draw_info,x,y,&pixel,exception);
5572 SetPixelViaPixelInfo(image,&pixel,q);
5573 q+=(ptrdiff_t) GetPixelChannels(image);
5575 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5577 if (status == MagickFalse)
5582 case FloodfillMethod:
5583 case FillToBorderMethod:
5588 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5589 TileVirtualPixelMethod,x,y,&target,exception);
5590 if (primitive_info->method == FillToBorderMethod)
5592 target.red=(double) draw_info->border_color.red;
5593 target.green=(double) draw_info->border_color.green;
5594 target.blue=(double) draw_info->border_color.blue;
5596 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5597 &target,x,y,primitive_info->method == FloodfillMethod ?
5598 MagickFalse : MagickTrue,exception);
5606 GetPixelInfo(image,&pixel);
5607 for (y=0; y < (ssize_t) image->rows; y++)
5612 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5614 if (q == (Quantum *) NULL)
5616 for (x=0; x < (ssize_t) image->columns; x++)
5618 GetFillColor(draw_info,x,y,&pixel,exception);
5619 SetPixelViaPixelInfo(image,&pixel,q);
5620 q+=(ptrdiff_t) GetPixelChannels(image);
5622 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5624 if (status == MagickFalse)
5632 case ImagePrimitive:
5638 composite_geometry[MagickPathExtent],
5639 magic[MagickPathExtent] = {
'\0'};
5655 if (primitive_info->text == (
char *) NULL)
5657 clone_info=AcquireImageInfo();
5658 composite_images=(Image *) NULL;
5659 if (LocaleNCompare(primitive_info->text,
"data:",5) == 0)
5660 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5663 if (*primitive_info->text !=
'\0')
5668 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5670 (void) SetImageInfo(clone_info,1,exception);
5671 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5673 if (clone_info->size != (
char *) NULL)
5674 clone_info->size=DestroyString(clone_info->size);
5675 if (clone_info->extract != (
char *) NULL)
5676 clone_info->extract=DestroyString(clone_info->extract);
5677 GetPathComponent(clone_info->filename,MagickPath,magic);
5679 composite_images=ReadImage(clone_info,exception);
5681 (
void) ThrowMagickException(exception,GetMagickModule(),
5682 FileOpenError,
"UnableToOpenFile",
"`%s'",clone_info->filename);
5684 clone_info=DestroyImageInfo(clone_info);
5685 if (composite_images == (Image *) NULL)
5690 composite_image=RemoveFirstImageFromList(&composite_images);
5691 composite_images=DestroyImageList(composite_images);
5692 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5693 NULL,(
void *) NULL);
5694 x1=CastDoubleToSsizeT(ceil(primitive_info[1].point.x-0.5));
5695 y1=CastDoubleToSsizeT(ceil(primitive_info[1].point.y-0.5));
5696 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5697 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5702 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5703 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
5704 composite_image->filter=image->filter;
5705 status&=(MagickStatusType) TransformImage(&composite_image,
5706 (
char *) NULL,composite_geometry,exception);
5708 if (composite_image->alpha_trait == UndefinedPixelTrait)
5709 status&=(MagickStatusType) SetImageAlphaChannel(composite_image,
5710 OpaqueAlphaChannel,exception);
5711 if (draw_info->alpha != OpaqueAlpha)
5712 status&=(MagickStatusType) SetImageAlpha(composite_image,
5713 draw_info->alpha,exception);
5714 SetGeometry(image,&geometry);
5715 image->gravity=draw_info->gravity;
5718 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5719 "%.20gx%.20g%+.20g%+.20g",(
double) composite_image->columns,(
double)
5720 composite_image->rows,(
double) geometry.x,(
double) geometry.y);
5721 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
5722 affine=draw_info->affine;
5723 affine.tx=(double) geometry.x;
5724 affine.ty=(double) geometry.y;
5725 composite_image->interpolate=image->interpolate;
5726 if ((draw_info->compose == OverCompositeOp) ||
5727 (draw_info->compose == SrcOverCompositeOp))
5728 status&=(MagickStatusType) DrawAffineImage(image,composite_image,
5731 status&=(MagickStatusType) CompositeImage(image,composite_image,
5732 draw_info->compose,MagickTrue,geometry.x,geometry.y,exception);
5733 composite_image=DestroyImage(composite_image);
5736 case PointPrimitive:
5744 if ((y < 0) || (y >= (ssize_t) image->rows))
5746 if ((x < 0) || (x >= (ssize_t) image->columns))
5748 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5749 if (q == (Quantum *) NULL)
5751 GetFillColor(draw_info,x,y,&fill_color,exception);
5752 CompositePixelOver(image,&fill_color,(
double) fill_color.alpha,q,(
double)
5753 GetPixelAlpha(image,q),q);
5754 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5761 geometry[MagickPathExtent];
5766 if (primitive_info->text == (
char *) NULL)
5768 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5769 (void) CloneString(&clone_info->text,primitive_info->text);
5770 (void) FormatLocaleString(geometry,MagickPathExtent,
"%+f%+f",
5771 primitive_info->point.x,primitive_info->point.y);
5772 (void) CloneString(&clone_info->geometry,geometry);
5773 status&=(MagickStatusType) AnnotateImage(image,clone_info,exception);
5774 clone_info=DestroyDrawInfo(clone_info);
5786 if (IsEventLogging() != MagickFalse)
5787 LogPrimitiveInfo(primitive_info);
5788 scale=ExpandAffine(&draw_info->affine);
5789 if ((draw_info->dash_pattern != (
double *) NULL) &&
5790 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5791 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5792 (draw_info->stroke.alpha != (
double) TransparentAlpha))
5797 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5798 clone_info->stroke_width=0.0;
5799 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5800 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5801 primitive_info,exception);
5802 clone_info=DestroyDrawInfo(clone_info);
5803 if (status != MagickFalse)
5804 status&=(MagickStatusType) DrawDashPolygon(draw_info,primitive_info,
5808 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5810 ((draw_info->stroke.alpha != (
double) TransparentAlpha) ||
5811 (draw_info->stroke_pattern != (Image *) NULL)))
5823 closed_path=primitive_info[0].closed_subpath;
5824 i=(ssize_t) primitive_info[0].coordinates;
5825 point_x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5826 point_y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5827 if ((point_x < MagickEpsilon) && (point_y < MagickEpsilon))
5828 closed_path=MagickTrue;
5829 if ((((draw_info->linecap == RoundCap) ||
5830 (closed_path != MagickFalse)) &&
5831 (draw_info->linejoin == RoundJoin)) ||
5832 (primitive_info[i].primitive != UndefinedPrimitive))
5834 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5835 primitive_info,exception);
5838 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5839 clone_info->stroke_width=0.0;
5840 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5841 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5842 primitive_info,exception);
5843 clone_info=DestroyDrawInfo(clone_info);
5844 if (status != MagickFalse)
5845 status&=(MagickStatusType) DrawStrokePolygon(image,draw_info,
5846 primitive_info,exception);
5849 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5850 primitive_info,exception);
5854 image_view=DestroyCacheView(image_view);
5855 if (draw_info->compliance == SVGCompliance)
5857 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5858 (Image *) NULL,exception);
5859 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5860 (Image *) NULL,exception);
5862 if (draw_info->debug != MagickFalse)
5863 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-primitive");
5864 return(status != 0 ? MagickTrue : MagickFalse);
5897static MagickBooleanType DrawRoundLinecap(Image *image,
5898 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5899 ExceptionInfo *exception)
5907 if (primitive_info->coordinates < 1)
5908 return(MagickFalse);
5909 for (i=0; i < 4; i++)
5910 linecap[i]=(*primitive_info);
5911 linecap[0].coordinates=4;
5912 linecap[1].point.x+=2.0*MagickEpsilon;
5913 linecap[2].point.x+=2.0*MagickEpsilon;
5914 linecap[2].point.y+=2.0*MagickEpsilon;
5915 linecap[3].point.y+=2.0*MagickEpsilon;
5916 linecap[4].primitive=UndefinedPrimitive;
5917 return(DrawPolygonPrimitive(image,draw_info,linecap,exception));
5920static MagickBooleanType DrawStrokePolygon(Image *image,
5921 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5922 ExceptionInfo *exception)
5943 if (draw_info->debug != MagickFalse)
5944 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5945 " begin draw-stroke-polygon");
5946 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5947 clone_info->fill=draw_info->stroke;
5948 if (clone_info->fill_pattern != (Image *) NULL)
5949 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5950 if (clone_info->stroke_pattern != (Image *) NULL)
5951 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5952 MagickTrue,exception);
5953 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5954 clone_info->stroke_width=0.0;
5955 clone_info->fill_rule=NonZeroRule;
5957 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
5959 if (p->coordinates == 1)
5961 stroke_polygon=TraceStrokePolygon(draw_info,p,exception);
5962 if (stroke_polygon == (PrimitiveInfo *) NULL)
5967 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5968 stroke_polygon,exception);
5969 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5972 q=p+p->coordinates-1;
5973 closed_path=p->closed_subpath;
5974 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5976 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,p,
5978 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,q,
5982 clone_info=DestroyDrawInfo(clone_info);
5983 if (draw_info->debug != MagickFalse)
5984 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5985 " end draw-stroke-polygon");
5986 return(status != 0 ? MagickTrue : MagickFalse);
6012MagickExport
void GetAffineMatrix(AffineMatrix *affine_matrix)
6014 if (IsEventLogging() != MagickFalse)
6015 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6016 assert(affine_matrix != (AffineMatrix *) NULL);
6017 (void) memset(affine_matrix,0,
sizeof(*affine_matrix));
6018 affine_matrix->sx=1.0;
6019 affine_matrix->sy=1.0;
6046MagickExport
void GetDrawInfo(
const ImageInfo *image_info,DrawInfo *draw_info)
6060 assert(draw_info != (DrawInfo *) NULL);
6061 if (IsEventLogging() != MagickFalse)
6062 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6063 (void) memset(draw_info,0,
sizeof(*draw_info));
6064 draw_info->image_info=CloneImageInfo(image_info);
6065 GetAffineMatrix(&draw_info->affine);
6066 exception=AcquireExceptionInfo();
6067 (void) QueryColorCompliance(
"#000F",AllCompliance,&draw_info->fill,
6069 (void) QueryColorCompliance(
"#FFF0",AllCompliance,&draw_info->stroke,
6071 draw_info->stroke_antialias=draw_info->image_info->antialias;
6072 draw_info->stroke_width=1.0;
6073 draw_info->fill_rule=EvenOddRule;
6074 draw_info->alpha=OpaqueAlpha;
6075 draw_info->fill_alpha=OpaqueAlpha;
6076 draw_info->stroke_alpha=OpaqueAlpha;
6077 draw_info->linecap=ButtCap;
6078 draw_info->linejoin=MiterJoin;
6079 draw_info->miterlimit=10;
6080 draw_info->decorate=NoDecoration;
6081 draw_info->pointsize=12.0;
6082 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
6083 draw_info->compose=OverCompositeOp;
6084 draw_info->render=MagickTrue;
6085 draw_info->clip_path=MagickFalse;
6086 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
6087 MagickTrue : MagickFalse;
6088 if (draw_info->image_info->font != (
char *) NULL)
6089 draw_info->font=AcquireString(draw_info->image_info->font);
6090 if (draw_info->image_info->density != (
char *) NULL)
6091 draw_info->density=AcquireString(draw_info->image_info->density);
6092 draw_info->text_antialias=draw_info->image_info->antialias;
6093 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
6094 draw_info->pointsize=draw_info->image_info->pointsize;
6095 draw_info->border_color=draw_info->image_info->border_color;
6096 if (draw_info->image_info->server_name != (
char *) NULL)
6097 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
6098 option=GetImageOption(draw_info->image_info,
"direction");
6099 if (option != (
const char *) NULL)
6100 draw_info->direction=(DirectionType) ParseCommandOption(
6101 MagickDirectionOptions,MagickFalse,option);
6103 draw_info->direction=UndefinedDirection;
6104 option=GetImageOption(draw_info->image_info,
"encoding");
6105 if (option != (
const char *) NULL)
6106 (void) CloneString(&draw_info->encoding,option);
6107 option=GetImageOption(draw_info->image_info,
"family");
6108 if (option != (
const char *) NULL)
6109 (void) CloneString(&draw_info->family,option);
6110 option=GetImageOption(draw_info->image_info,
"fill");
6111 if (option != (
const char *) NULL)
6112 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
6114 option=GetImageOption(draw_info->image_info,
"gravity");
6115 if (option != (
const char *) NULL)
6116 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
6117 MagickFalse,option);
6118 option=GetImageOption(draw_info->image_info,
"interline-spacing");
6119 if (option != (
const char *) NULL)
6120 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6121 option=GetImageOption(draw_info->image_info,
"interword-spacing");
6122 if (option != (
const char *) NULL)
6123 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6124 option=GetImageOption(draw_info->image_info,
"kerning");
6125 if (option != (
const char *) NULL)
6126 draw_info->kerning=GetDrawValue(option,&next_token);
6127 option=GetImageOption(draw_info->image_info,
"stroke");
6128 if (option != (
const char *) NULL)
6129 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
6131 option=GetImageOption(draw_info->image_info,
"strokewidth");
6132 if (option != (
const char *) NULL)
6133 draw_info->stroke_width=GetDrawValue(option,&next_token);
6134 option=GetImageOption(draw_info->image_info,
"style");
6135 if (option != (
const char *) NULL)
6136 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6137 MagickFalse,option);
6138 option=GetImageOption(draw_info->image_info,
"undercolor");
6139 if (option != (
const char *) NULL)
6140 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
6142 option=GetImageOption(draw_info->image_info,
"weight");
6143 if (option != (
const char *) NULL)
6148 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6150 weight=(ssize_t) StringToUnsignedLong(option);
6151 draw_info->weight=(size_t) weight;
6153 option=GetImageOption(draw_info->image_info,
"word-break");
6154 if (option != (
const char *) NULL)
6155 draw_info->word_break=(WordBreakType) ParseCommandOption(
6156 MagickWordBreakOptions,MagickFalse,option);
6157 exception=DestroyExceptionInfo(exception);
6158 draw_info->signature=MagickCoreSignature;
6186static inline double Permutate(
const ssize_t n,
const ssize_t k)
6195 for (i=k+1; i <= n; i++)
6197 for (i=1; i <= (n-k); i++)
6218static MagickBooleanType TraceArc(MVGInfo *mvg_info,
const PointInfo start,
6219 const PointInfo end,
const PointInfo degrees)
6225 center.x=0.5*(end.x+start.x);
6226 center.y=0.5*(end.y+start.y);
6227 radius.x=fabs(center.x-start.x);
6228 radius.y=fabs(center.y-start.y);
6229 return(TraceEllipse(mvg_info,center,radius,degrees));
6232static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,
const PointInfo start,
6233 const PointInfo end,
const PointInfo arc,
const double angle,
6234 const MagickBooleanType large_arc,
const MagickBooleanType sweep)
6271 offset=mvg_info->offset;
6272 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6273 primitive_info->coordinates=0;
6274 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6275 (fabs(start.y-end.y) < MagickEpsilon))
6276 return(TracePoint(primitive_info,end));
6277 radii.x=fabs(arc.x);
6278 radii.y=fabs(arc.y);
6279 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6280 return(TraceLine(primitive_info,start,end));
6281 cosine=cos(DegreesToRadians(fmod((
double) angle,360.0)));
6282 sine=sin(DegreesToRadians(fmod((
double) angle,360.0)));
6283 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6284 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6285 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6287 if (delta < MagickEpsilon)
6288 return(TraceLine(primitive_info,start,end));
6291 radii.x*=sqrt((
double) delta);
6292 radii.y*=sqrt((
double) delta);
6294 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6295 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6296 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6297 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6298 alpha=points[1].x-points[0].x;
6299 beta=points[1].y-points[0].y;
6300 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6301 return(TraceLine(primitive_info,start,end));
6302 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6307 factor=sqrt((
double) factor);
6308 if (sweep == large_arc)
6311 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6312 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6313 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6314 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6315 if ((theta < 0.0) && (sweep != MagickFalse))
6316 theta+=2.0*MagickPI;
6318 if ((theta > 0.0) && (sweep == MagickFalse))
6319 theta-=2.0*MagickPI;
6320 arc_segments=(size_t) CastDoubleToSsizeT(ceil(fabs((
double) (theta/(0.5*
6321 MagickPI+MagickEpsilon)))));
6324 for (i=0; i < (ssize_t) arc_segments; i++)
6326 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6327 gamma=(8.0/3.0)*sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))*
6328 sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))/
6329 sin(fmod((
double) beta,DegreesToRadians(360.0)));
6330 points[0].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) i*theta/
6331 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((
double) (alpha+
6332 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6333 points[0].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) i*theta/
6334 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((
double) (alpha+
6335 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6336 points[2].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) (i+1)*
6337 theta/arc_segments),DegreesToRadians(360.0))));
6338 points[2].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) (i+1)*
6339 theta/arc_segments),DegreesToRadians(360.0))));
6340 points[1].x=(double) (points[2].x+gamma*sin(fmod((
double) (alpha+(
double)
6341 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6342 points[1].y=(double) (points[2].y-gamma*cos(fmod((
double) (alpha+(
double)
6343 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6344 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6345 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6346 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6348 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6350 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6352 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6354 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6356 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6358 if (i == (ssize_t) (arc_segments-1))
6360 status&=(MagickStatusType) TraceBezier(mvg_info,4);
6363 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6364 mvg_info->offset+=(ssize_t) p->coordinates;
6365 p+=(ptrdiff_t) p->coordinates;
6368 return(MagickFalse);
6369 mvg_info->offset=offset;
6370 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6371 primitive_info->coordinates=(size_t) (p-primitive_info);
6372 primitive_info->closed_subpath=MagickFalse;
6373 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6375 p->primitive=primitive_info->primitive;
6381static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6382 const size_t number_coordinates)
6411 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6412 quantum=number_coordinates;
6413 for (i=0; i < (ssize_t) number_coordinates; i++)
6415 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6417 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6418 if (alpha > (
double) GetMaxMemoryRequest())
6420 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6421 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6422 return(MagickFalse);
6424 if (alpha > (
double) quantum)
6425 quantum=(size_t) alpha;
6426 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6427 if (alpha > (
double) quantum)
6428 quantum=(size_t) alpha;
6431 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6432 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6433 if (quantum > (
double) GetMaxMemoryRequest())
6435 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6436 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6437 return(MagickFalse);
6439 coefficients=(
double *) AcquireQuantumMemory(number_coordinates,
6440 sizeof(*coefficients));
6441 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6443 if ((coefficients == (
double *) NULL) || (points == (PointInfo *) NULL))
6445 if (points != (PointInfo *) NULL)
6446 points=(PointInfo *) RelinquishMagickMemory(points);
6447 if (coefficients != (
double *) NULL)
6448 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6449 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6450 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6451 return(MagickFalse);
6453 control_points=quantum*number_coordinates;
6454 if (CheckPrimitiveExtent(mvg_info,(
double) control_points+1) == MagickFalse)
6456 points=(PointInfo *) RelinquishMagickMemory(points);
6457 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6458 return(MagickFalse);
6460 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6464 end=primitive_info[number_coordinates-1].point;
6465 for (i=0; i < (ssize_t) number_coordinates; i++)
6466 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6468 for (i=0; i < (ssize_t) control_points; i++)
6473 alpha=pow((
double) (1.0-weight),(
double) number_coordinates-1.0);
6474 for (j=0; j < (ssize_t) number_coordinates; j++)
6476 point.x+=alpha*coefficients[j]*p->point.x;
6477 point.y+=alpha*coefficients[j]*p->point.y;
6478 alpha*=weight/(1.0-weight);
6482 weight+=1.0/control_points;
6488 for (i=0; i < (ssize_t) control_points; i++)
6490 if (TracePoint(p,points[i]) == MagickFalse)
6492 points=(PointInfo *) RelinquishMagickMemory(points);
6493 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6494 return(MagickFalse);
6496 p+=(ptrdiff_t) p->coordinates;
6498 if (TracePoint(p,end) == MagickFalse)
6500 points=(PointInfo *) RelinquishMagickMemory(points);
6501 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6502 return(MagickFalse);
6504 p+=(ptrdiff_t) p->coordinates;
6505 primitive_info->coordinates=(size_t) (p-primitive_info);
6506 primitive_info->closed_subpath=MagickFalse;
6507 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6509 p->primitive=primitive_info->primitive;
6512 points=(PointInfo *) RelinquishMagickMemory(points);
6513 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6517static MagickBooleanType TraceCircle(MVGInfo *mvg_info,
const PointInfo start,
6518 const PointInfo end)
6529 alpha=end.x-start.x;
6531 radius=hypot((
double) alpha,(
double) beta);
6532 offset.x=(double) radius;
6533 offset.y=(double) radius;
6536 return(TraceEllipse(mvg_info,start,offset,degrees));
6539static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,
const PointInfo center,
6540 const PointInfo radii,
const PointInfo arc)
6565 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6566 primitive_info->coordinates=0;
6567 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6569 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6570 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6571 angle.x=DegreesToRadians(arc.x);
6575 angle.y=DegreesToRadians(y);
6576 coordinates=ceil((angle.y-angle.x)/step+1.0);
6577 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6578 return(MagickFalse);
6580 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6581 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6583 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6584 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6585 if (i++ >= (ssize_t) coordinates)
6587 if (TracePoint(p,point) == MagickFalse)
6588 return(MagickFalse);
6589 p+=(ptrdiff_t) p->coordinates;
6591 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6592 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6593 if (TracePoint(p,point) == MagickFalse)
6594 return(MagickFalse);
6595 p+=(ptrdiff_t) p->coordinates;
6596 primitive_info->coordinates=(size_t) (p-primitive_info);
6597 primitive_info->closed_subpath=MagickFalse;
6598 x=fabs(primitive_info[0].point.x-
6599 primitive_info[primitive_info->coordinates-1].point.x);
6600 y=fabs(primitive_info[0].point.y-
6601 primitive_info[primitive_info->coordinates-1].point.y);
6602 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6603 primitive_info->closed_subpath=MagickTrue;
6604 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6606 p->primitive=primitive_info->primitive;
6612static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6613 const PointInfo start,
const PointInfo end)
6615 if (TracePoint(primitive_info,start) == MagickFalse)
6616 return(MagickFalse);
6617 if (TracePoint(primitive_info+1,end) == MagickFalse)
6618 return(MagickFalse);
6619 (primitive_info+1)->primitive=primitive_info->primitive;
6620 primitive_info->coordinates=2;
6621 primitive_info->closed_subpath=MagickFalse;
6625static ssize_t TracePath(MVGInfo *mvg_info,
const char *path,
6626 ExceptionInfo *exception)
6630 token[MagickPathExtent] =
"";
6648 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6671 subpath_offset=mvg_info->offset;
6672 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6675 number_coordinates=0;
6677 primitive_type=primitive_info->primitive;
6679 for (p=path; *p !=
'\0'; )
6681 if (status == MagickFalse)
6683 while (isspace((
int) ((
unsigned char) *p)) != 0)
6687 last_attribute=attribute;
6688 attribute=(int) (*p++);
6698 large_arc = MagickFalse,
6699 sweep = MagickFalse;
6709 (void) GetNextToken(p,&p,MagickPathExtent,token);
6711 (void) GetNextToken(p,&p,MagickPathExtent,token);
6712 arc.x=GetDrawValue(token,&next_token);
6713 if (token == next_token)
6714 ThrowPointExpectedException(token,exception);
6715 (void) GetNextToken(p,&p,MagickPathExtent,token);
6717 (void) GetNextToken(p,&p,MagickPathExtent,token);
6718 arc.y=GetDrawValue(token,&next_token);
6719 if (token == next_token)
6720 ThrowPointExpectedException(token,exception);
6721 (void) GetNextToken(p,&p,MagickPathExtent,token);
6723 (void) GetNextToken(p,&p,MagickPathExtent,token);
6724 angle=GetDrawValue(token,&next_token);
6725 if (token == next_token)
6726 ThrowPointExpectedException(token,exception);
6727 (void) GetNextToken(p,&p,MagickPathExtent,token);
6729 (void) GetNextToken(p,&p,MagickPathExtent,token);
6730 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6731 (void) GetNextToken(p,&p,MagickPathExtent,token);
6733 (void) GetNextToken(p,&p,MagickPathExtent,token);
6734 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6736 (void) GetNextToken(p,&p,MagickPathExtent,token);
6737 (void) GetNextToken(p,&p,MagickPathExtent,token);
6739 (void) GetNextToken(p,&p,MagickPathExtent,token);
6740 x=GetDrawValue(token,&next_token);
6741 if (token == next_token)
6742 ThrowPointExpectedException(token,exception);
6743 (void) GetNextToken(p,&p,MagickPathExtent,token);
6745 (void) GetNextToken(p,&p,MagickPathExtent,token);
6746 y=GetDrawValue(token,&next_token);
6747 if (token == next_token)
6748 ThrowPointExpectedException(token,exception);
6749 end.x=(double) (attribute == (
int)
'A' ? x : point.x+x);
6750 end.y=(double) (attribute == (
int)
'A' ? y : point.y+y);
6751 if (TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep) == MagickFalse)
6753 q=(*mvg_info->primitive_info)+mvg_info->offset;
6754 mvg_info->offset+=(ssize_t) q->coordinates;
6755 q+=(ptrdiff_t) q->coordinates;
6757 while (isspace((
int) ((
unsigned char) *p)) != 0)
6761 }
while (IsPoint(p) != MagickFalse);
6773 for (i=1; i < 4; i++)
6775 (void) GetNextToken(p,&p,MagickPathExtent,token);
6777 (void) GetNextToken(p,&p,MagickPathExtent,token);
6778 x=GetDrawValue(token,&next_token);
6779 if (token == next_token)
6780 ThrowPointExpectedException(token,exception);
6781 (void) GetNextToken(p,&p,MagickPathExtent,token);
6783 (void) GetNextToken(p,&p,MagickPathExtent,token);
6784 y=GetDrawValue(token,&next_token);
6785 if (token == next_token)
6786 ThrowPointExpectedException(token,exception);
6787 end.x=(double) (attribute == (
int)
'C' ? x : point.x+x);
6788 end.y=(double) (attribute == (
int)
'C' ? y : point.y+y);
6791 for (i=0; i < 4; i++)
6792 (q+i)->point=points[i];
6793 if (TraceBezier(mvg_info,4) == MagickFalse)
6795 q=(*mvg_info->primitive_info)+mvg_info->offset;
6796 mvg_info->offset+=(ssize_t) q->coordinates;
6797 q+=(ptrdiff_t) q->coordinates;
6799 while (isspace((
int) ((
unsigned char) *p)) != 0)
6803 }
while (IsPoint(p) != MagickFalse);
6811 (void) GetNextToken(p,&p,MagickPathExtent,token);
6813 (void) GetNextToken(p,&p,MagickPathExtent,token);
6814 x=GetDrawValue(token,&next_token);
6815 if (token == next_token)
6816 ThrowPointExpectedException(token,exception);
6817 point.x=(double) (attribute == (
int)
'H' ? x: point.x+x);
6818 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6820 q=(*mvg_info->primitive_info)+mvg_info->offset;
6821 if (TracePoint(q,point) == MagickFalse)
6823 mvg_info->offset+=(ssize_t) q->coordinates;
6824 q+=(ptrdiff_t) q->coordinates;
6825 while (isspace((
int) ((
unsigned char) *p)) != 0)
6829 }
while (IsPoint(p) != MagickFalse);
6840 (void) GetNextToken(p,&p,MagickPathExtent,token);
6842 (void) GetNextToken(p,&p,MagickPathExtent,token);
6843 x=GetDrawValue(token,&next_token);
6844 if (token == next_token)
6845 ThrowPointExpectedException(token,exception);
6846 (void) GetNextToken(p,&p,MagickPathExtent,token);
6848 (void) GetNextToken(p,&p,MagickPathExtent,token);
6849 y=GetDrawValue(token,&next_token);
6850 if (token == next_token)
6851 ThrowPointExpectedException(token,exception);
6852 point.x=(double) (attribute == (
int)
'L' ? x : point.x+x);
6853 point.y=(double) (attribute == (
int)
'L' ? y : point.y+y);
6854 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6856 q=(*mvg_info->primitive_info)+mvg_info->offset;
6857 if (TracePoint(q,point) == MagickFalse)
6859 mvg_info->offset+=(ssize_t) q->coordinates;
6860 q+=(ptrdiff_t) q->coordinates;
6861 while (isspace((
int) ((
unsigned char) *p)) != 0)
6865 }
while (IsPoint(p) != MagickFalse);
6874 if (mvg_info->offset != subpath_offset)
6876 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6877 primitive_info->coordinates=(size_t) (q-primitive_info);
6878 number_coordinates+=primitive_info->coordinates;
6880 subpath_offset=mvg_info->offset;
6885 (void) GetNextToken(p,&p,MagickPathExtent,token);
6887 (void) GetNextToken(p,&p,MagickPathExtent,token);
6888 x=GetDrawValue(token,&next_token);
6889 if (token == next_token)
6890 ThrowPointExpectedException(token,exception);
6891 (void) GetNextToken(p,&p,MagickPathExtent,token);
6893 (void) GetNextToken(p,&p,MagickPathExtent,token);
6894 y=GetDrawValue(token,&next_token);
6895 if (token == next_token)
6896 ThrowPointExpectedException(token,exception);
6897 point.x=(double) (attribute == (
int)
'M' ? x : point.x+x);
6898 point.y=(double) (attribute == (
int)
'M' ? y : point.y+y);
6902 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6904 q=(*mvg_info->primitive_info)+mvg_info->offset;
6905 if (TracePoint(q,point) == MagickFalse)
6907 mvg_info->offset+=(ssize_t) q->coordinates;
6908 q+=(ptrdiff_t) q->coordinates;
6909 while (isspace((
int) ((
unsigned char) *p)) != 0)
6913 }
while (IsPoint(p) != MagickFalse);
6925 for (i=1; i < 3; i++)
6927 (void) GetNextToken(p,&p,MagickPathExtent,token);
6929 (void) GetNextToken(p,&p,MagickPathExtent,token);
6930 x=GetDrawValue(token,&next_token);
6931 if (token == next_token)
6932 ThrowPointExpectedException(token,exception);
6933 (void) GetNextToken(p,&p,MagickPathExtent,token);
6935 (void) GetNextToken(p,&p,MagickPathExtent,token);
6936 y=GetDrawValue(token,&next_token);
6937 if (token == next_token)
6938 ThrowPointExpectedException(token,exception);
6941 end.x=(double) (attribute == (
int)
'Q' ? x : point.x+x);
6942 end.y=(double) (attribute == (
int)
'Q' ? y : point.y+y);
6945 for (i=0; i < 3; i++)
6946 (q+i)->point=points[i];
6947 if (TraceBezier(mvg_info,3) == MagickFalse)
6949 q=(*mvg_info->primitive_info)+mvg_info->offset;
6950 mvg_info->offset+=(ssize_t) q->coordinates;
6951 q+=(ptrdiff_t) q->coordinates;
6953 while (isspace((
int) ((
unsigned char) *p)) != 0)
6957 }
while (IsPoint(p) != MagickFalse);
6968 points[0]=points[3];
6969 points[1].x=2.0*points[3].x-points[2].x;
6970 points[1].y=2.0*points[3].y-points[2].y;
6971 for (i=2; i < 4; i++)
6973 (void) GetNextToken(p,&p,MagickPathExtent,token);
6975 (void) GetNextToken(p,&p,MagickPathExtent,token);
6976 x=GetDrawValue(token,&next_token);
6977 if (token == next_token)
6978 ThrowPointExpectedException(token,exception);
6979 (void) GetNextToken(p,&p,MagickPathExtent,token);
6981 (void) GetNextToken(p,&p,MagickPathExtent,token);
6982 y=GetDrawValue(token,&next_token);
6983 if (token == next_token)
6984 ThrowPointExpectedException(token,exception);
6987 end.x=(double) (attribute == (
int)
'S' ? x : point.x+x);
6988 end.y=(double) (attribute == (
int)
'S' ? y : point.y+y);
6991 if (strchr(
"CcSs",last_attribute) == (
char *) NULL)
6996 for (i=0; i < 4; i++)
6997 (q+i)->point=points[i];
6998 if (TraceBezier(mvg_info,4) == MagickFalse)
7000 q=(*mvg_info->primitive_info)+mvg_info->offset;
7001 mvg_info->offset+=(ssize_t) q->coordinates;
7002 q+=(ptrdiff_t) q->coordinates;
7004 last_attribute=attribute;
7005 while (isspace((
int) ((
unsigned char) *p)) != 0)
7009 }
while (IsPoint(p) != MagickFalse);
7020 points[0]=points[2];
7021 points[1].x=2.0*points[2].x-points[1].x;
7022 points[1].y=2.0*points[2].y-points[1].y;
7023 for (i=2; i < 3; i++)
7025 (void) GetNextToken(p,&p,MagickPathExtent,token);
7027 (void) GetNextToken(p,&p,MagickPathExtent,token);
7028 x=GetDrawValue(token,&next_token);
7029 if (token == next_token)
7030 ThrowPointExpectedException(token,exception);
7031 (void) GetNextToken(p,&p,MagickPathExtent,token);
7033 (void) GetNextToken(p,&p,MagickPathExtent,token);
7034 y=GetDrawValue(token,&next_token);
7035 if (token == next_token)
7036 ThrowPointExpectedException(token,exception);
7037 end.x=(double) (attribute == (
int)
'T' ? x : point.x+x);
7038 end.y=(double) (attribute == (
int)
'T' ? y : point.y+y);
7041 if (status == MagickFalse)
7043 if (strchr(
"QqTt",last_attribute) == (
char *) NULL)
7048 for (i=0; i < 3; i++)
7049 (q+i)->point=points[i];
7050 if (TraceBezier(mvg_info,3) == MagickFalse)
7052 q=(*mvg_info->primitive_info)+mvg_info->offset;
7053 mvg_info->offset+=(ssize_t) q->coordinates;
7054 q+=(ptrdiff_t) q->coordinates;
7056 last_attribute=attribute;
7057 while (isspace((
int) ((
unsigned char) *p)) != 0)
7061 }
while (IsPoint(p) != MagickFalse);
7072 (void) GetNextToken(p,&p,MagickPathExtent,token);
7074 (void) GetNextToken(p,&p,MagickPathExtent,token);
7075 y=GetDrawValue(token,&next_token);
7076 if (token == next_token)
7077 ThrowPointExpectedException(token,exception);
7078 point.y=(double) (attribute == (
int)
'V' ? y : point.y+y);
7079 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7081 q=(*mvg_info->primitive_info)+mvg_info->offset;
7082 if (TracePoint(q,point) == MagickFalse)
7084 mvg_info->offset+=(ssize_t) q->coordinates;
7085 q+=(ptrdiff_t) q->coordinates;
7086 while (isspace((
int) ((
unsigned char) *p)) != 0)
7090 }
while (IsPoint(p) != MagickFalse);
7100 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7102 q=(*mvg_info->primitive_info)+mvg_info->offset;
7103 if (TracePoint(q,point) == MagickFalse)
7105 mvg_info->offset+=(ssize_t) q->coordinates;
7106 q+=(ptrdiff_t) q->coordinates;
7107 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7108 primitive_info->coordinates=(size_t) (q-primitive_info);
7109 primitive_info->closed_subpath=MagickTrue;
7110 number_coordinates+=primitive_info->coordinates;
7112 subpath_offset=mvg_info->offset;
7118 ThrowPointExpectedException(token,exception);
7123 if (status == MagickFalse)
7125 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7126 primitive_info->coordinates=(size_t) (q-primitive_info);
7127 number_coordinates+=primitive_info->coordinates;
7128 for (i=0; i < (ssize_t) number_coordinates; i++)
7131 q->primitive=primitive_type;
7133 q->method=FillToBorderMethod;
7136 return((ssize_t) number_coordinates);
7139static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7140 const PointInfo start,
const PointInfo end)
7152 if (TracePoint(p,start) == MagickFalse)
7153 return(MagickFalse);
7154 p+=(ptrdiff_t) p->coordinates;
7157 if (TracePoint(p,point) == MagickFalse)
7158 return(MagickFalse);
7159 p+=(ptrdiff_t) p->coordinates;
7160 if (TracePoint(p,end) == MagickFalse)
7161 return(MagickFalse);
7162 p+=(ptrdiff_t) p->coordinates;
7165 if (TracePoint(p,point) == MagickFalse)
7166 return(MagickFalse);
7167 p+=(ptrdiff_t) p->coordinates;
7168 if (TracePoint(p,start) == MagickFalse)
7169 return(MagickFalse);
7170 p+=(ptrdiff_t) p->coordinates;
7171 primitive_info->coordinates=(size_t) (p-primitive_info);
7172 primitive_info->closed_subpath=MagickTrue;
7173 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7175 p->primitive=primitive_info->primitive;
7181static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7182 const PointInfo start,
const PointInfo end,PointInfo arc)
7201 offset=mvg_info->offset;
7202 segment.x=fabs(end.x-start.x);
7203 segment.y=fabs(end.y-start.y);
7204 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7206 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7209 if (arc.x > (0.5*segment.x))
7210 arc.x=0.5*segment.x;
7211 if (arc.y > (0.5*segment.y))
7212 arc.y=0.5*segment.y;
7213 point.x=start.x+segment.x-arc.x;
7214 point.y=start.y+arc.y;
7217 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7218 return(MagickFalse);
7219 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7220 mvg_info->offset+=(ssize_t) p->coordinates;
7221 point.x=start.x+segment.x-arc.x;
7222 point.y=start.y+segment.y-arc.y;
7225 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7226 return(MagickFalse);
7227 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7228 mvg_info->offset+=(ssize_t) p->coordinates;
7229 point.x=start.x+arc.x;
7230 point.y=start.y+segment.y-arc.y;
7233 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7234 return(MagickFalse);
7235 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7236 mvg_info->offset+=(ssize_t) p->coordinates;
7237 point.x=start.x+arc.x;
7238 point.y=start.y+arc.y;
7241 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7242 return(MagickFalse);
7243 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7244 mvg_info->offset+=(ssize_t) p->coordinates;
7245 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7246 return(MagickFalse);
7247 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7248 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7249 return(MagickFalse);
7250 p+=(ptrdiff_t) p->coordinates;
7251 mvg_info->offset=offset;
7252 primitive_info=(*mvg_info->primitive_info)+offset;
7253 primitive_info->coordinates=(size_t) (p-primitive_info);
7254 primitive_info->closed_subpath=MagickTrue;
7255 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7257 p->primitive=primitive_info->primitive;
7263static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7264 const size_t number_vertices,
const double offset)
7281 for (i=1; i < (ssize_t) number_vertices; i++)
7283 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7284 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7285 if ((fabs((
double) dx) >= MagickEpsilon) ||
7286 (fabs((
double) dy) >= MagickEpsilon))
7289 if (i == (ssize_t) number_vertices)
7290 i=(ssize_t) number_vertices-1L;
7291 distance=hypot((
double) dx,(
double) dy);
7292 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7293 dx*(distance+offset)/distance);
7294 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7295 dy*(distance+offset)/distance);
7296 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7298 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7299 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7300 if ((fabs((
double) dx) >= MagickEpsilon) ||
7301 (fabs((
double) dy) >= MagickEpsilon))
7304 distance=hypot((
double) dx,(
double) dy);
7305 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7306 dx*(distance+offset)/distance);
7307 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7308 dy*(distance+offset)/distance);
7312static PrimitiveInfo *TraceStrokePolygon(
const DrawInfo *draw_info,
7313 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7315#define MaxStrokePad (6*BezierQuantum+360)
7316#define CheckPathExtent(pad_p,pad_q) \
7318 if ((pad_p) > MaxBezierCoordinates) \
7319 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7321 if ((p+(ptrdiff_t) (pad_p)) >= (ssize_t) extent_p) \
7323 if (~extent_p < (pad_p)) \
7324 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7327 extent_p+=(pad_p); \
7328 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7329 MaxStrokePad,sizeof(*stroke_p)); \
7332 if ((pad_q) > MaxBezierCoordinates) \
7333 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7335 if ((q+(ptrdiff_t) (pad_q)) >= (ssize_t) extent_q) \
7337 if (~extent_q < (pad_q)) \
7338 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7341 extent_q+=(pad_q); \
7342 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7343 MaxStrokePad,sizeof(*stroke_q)); \
7346 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7348 if (stroke_p != (PointInfo *) NULL) \
7349 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7350 if (stroke_q != (PointInfo *) NULL) \
7351 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7352 polygon_primitive=(PrimitiveInfo *) \
7353 RelinquishMagickMemory(polygon_primitive); \
7354 (void) ThrowMagickException(exception,GetMagickModule(), \
7355 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7356 return((PrimitiveInfo *) NULL); \
7360 typedef struct _StrokeSegment
7406 inverse_slope = {0.0, 0.0},
7413 number_vertices=primitive_info->coordinates;
7414 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7415 number_vertices+2UL,
sizeof(*polygon_primitive));
7416 if (polygon_primitive == (PrimitiveInfo *) NULL)
7418 (void) ThrowMagickException(exception,GetMagickModule(),
7419 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7420 return((PrimitiveInfo *) NULL);
7422 (void) memcpy(polygon_primitive,primitive_info,(
size_t) number_vertices*
7423 sizeof(*polygon_primitive));
7424 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7425 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7426 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7427 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7428 if ((draw_info->linejoin == RoundJoin) ||
7429 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
7431 polygon_primitive[number_vertices]=primitive_info[1];
7434 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7438 closed_path=primitive_info[0].closed_subpath;
7441 for (n=1; n < (ssize_t) number_vertices; n++)
7443 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7444 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7445 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7448 if (n == (ssize_t) number_vertices)
7450 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7455 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7456 sizeof(*stroke_polygon));
7457 stroke_polygon[0]=polygon_primitive[0];
7458 stroke_polygon[0].coordinates=0;
7459 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7461 return(stroke_polygon);
7463 n=(ssize_t) number_vertices-1L;
7465 extent_p=2*number_vertices;
7466 extent_q=2*number_vertices;
7467 stroke_p=(PointInfo *) AcquireQuantumMemory((
size_t) extent_p+MaxStrokePad,
7469 stroke_q=(PointInfo *) AcquireQuantumMemory((
size_t) extent_q+MaxStrokePad,
7471 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7473 if (stroke_p != (PointInfo *) NULL)
7474 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7475 if (stroke_q != (PointInfo *) NULL)
7476 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7477 polygon_primitive=(PrimitiveInfo *)
7478 RelinquishMagickMemory(polygon_primitive);
7479 (void) ThrowMagickException(exception,GetMagickModule(),
7480 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7481 return((PrimitiveInfo *) NULL);
7484 inverse_slope.p=0.0;
7485 if (fabs(dx.p) < MagickEpsilon)
7488 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7490 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7493 if (fabs(dy.p) < MagickEpsilon)
7496 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7498 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7503 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7505 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7506 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7507 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7508 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7509 offset.x=sqrt((
double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7510 offset.y=(double) (offset.x*inverse_slope.p);
7511 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7513 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7514 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7515 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7516 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7517 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7518 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7519 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7520 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7524 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7525 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7526 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7527 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7528 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7529 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7530 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7531 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7538 stroke_q[p++]=box_q[0];
7539 stroke_p[q++]=box_p[0];
7540 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7545 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7546 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7547 dot_product=dx.q*dx.q+dy.q*dy.q;
7548 if (dot_product < 0.25)
7551 inverse_slope.q=0.0;
7552 if (fabs(dx.q) < MagickEpsilon)
7555 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7557 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7560 if (fabs(dy.q) < MagickEpsilon)
7563 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7565 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7570 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7572 offset.x=sqrt((
double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7573 offset.y=(double) (offset.x*inverse_slope.q);
7574 dot_product=dy.q*offset.x-dx.q*offset.y;
7575 if (dot_product > 0.0)
7577 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7578 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7579 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7580 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7581 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7582 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7583 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7584 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7588 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7589 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7590 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7591 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7592 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7593 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7594 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7595 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7597 if (fabs((
double) (slope.p-slope.q)) < MagickEpsilon)
7604 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7605 box_p[3].y)/(slope.p-slope.q));
7606 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7607 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7608 box_q[3].y)/(slope.p-slope.q));
7609 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7611 DisableMSCWarning(4127)
7612 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7614 dot_product=dx.q*dy.p-dx.p*dy.q;
7615 if (dot_product <= 0.0)
7616 switch (draw_info->linejoin)
7620 stroke_q[q++]=box_q[1];
7621 stroke_q[q++]=box_q[2];
7622 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7623 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7624 if (dot_product <= miterlimit)
7625 stroke_p[p++]=box_p[4];
7628 stroke_p[p++]=box_p[1];
7629 stroke_p[p++]=box_p[2];
7635 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7636 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7637 if (dot_product <= miterlimit)
7639 stroke_q[q++]=box_q[4];
7640 stroke_p[p++]=box_p[4];
7644 stroke_q[q++]=box_q[1];
7645 stroke_q[q++]=box_q[2];
7646 stroke_p[p++]=box_p[1];
7647 stroke_p[p++]=box_p[2];
7653 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7654 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7655 if (dot_product <= miterlimit)
7656 stroke_p[p++]=box_p[4];
7659 stroke_p[p++]=box_p[1];
7660 stroke_p[p++]=box_p[2];
7662 center=polygon_primitive[n].point;
7663 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7664 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7665 if (theta.q < theta.p)
7666 theta.q+=2.0*MagickPI;
7667 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.q-
7668 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7669 DisableMSCWarning(4127)
7670 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7672 stroke_q[q].x=box_q[1].x;
7673 stroke_q[q].y=box_q[1].y;
7675 for (j=1; j < (ssize_t) arc_segments; j++)
7677 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7678 stroke_q[q].x=(double) (center.x+mid*cos(fmod((
double)
7679 (theta.p+delta_theta),DegreesToRadians(360.0))));
7680 stroke_q[q].y=(double) (center.y+mid*sin(fmod((
double)
7681 (theta.p+delta_theta),DegreesToRadians(360.0))));
7684 stroke_q[q++]=box_q[2];
7691 switch (draw_info->linejoin)
7695 stroke_p[p++]=box_p[1];
7696 stroke_p[p++]=box_p[2];
7697 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7698 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7699 if (dot_product <= miterlimit)
7700 stroke_q[q++]=box_q[4];
7703 stroke_q[q++]=box_q[1];
7704 stroke_q[q++]=box_q[2];
7710 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7711 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7712 if (dot_product <= miterlimit)
7714 stroke_q[q++]=box_q[4];
7715 stroke_p[p++]=box_p[4];
7719 stroke_q[q++]=box_q[1];
7720 stroke_q[q++]=box_q[2];
7721 stroke_p[p++]=box_p[1];
7722 stroke_p[p++]=box_p[2];
7728 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7729 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7730 if (dot_product <= miterlimit)
7731 stroke_q[q++]=box_q[4];
7734 stroke_q[q++]=box_q[1];
7735 stroke_q[q++]=box_q[2];
7737 center=polygon_primitive[n].point;
7738 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7739 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7740 if (theta.p < theta.q)
7741 theta.p+=2.0*MagickPI;
7742 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.p-
7743 theta.q)/(2.0*sqrt((
double) (MagickSafeReciprocal(mid)))))));
7744 DisableMSCWarning(4127)
7745 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7747 stroke_p[p++]=box_p[1];
7748 for (j=1; j < (ssize_t) arc_segments; j++)
7750 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7751 stroke_p[p].x=(double) (center.x+mid*cos(fmod((
double)
7752 (theta.p+delta_theta),DegreesToRadians(360.0))));
7753 stroke_p[p].y=(double) (center.y+mid*sin(fmod((
double)
7754 (theta.p+delta_theta),DegreesToRadians(360.0))));
7757 stroke_p[p++]=box_p[2];
7764 inverse_slope.p=inverse_slope.q;
7773 stroke_p[p++]=box_p[1];
7774 stroke_q[q++]=box_q[1];
7778 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7779 (p+q+2L),(
size_t) (closed_path+2L)*
sizeof(*stroke_polygon));
7780 if (stroke_polygon == (PrimitiveInfo *) NULL)
7782 (void) ThrowMagickException(exception,GetMagickModule(),
7783 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7784 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7785 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7786 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7788 return(stroke_polygon);
7790 for (i=0; i < (ssize_t) p; i++)
7792 stroke_polygon[i]=polygon_primitive[0];
7793 stroke_polygon[i].point=stroke_p[i];
7795 if (closed_path != MagickFalse)
7797 stroke_polygon[i]=polygon_primitive[0];
7798 stroke_polygon[i].point=stroke_polygon[0].point;
7801 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7803 stroke_polygon[i]=polygon_primitive[0];
7804 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7806 if (closed_path != MagickFalse)
7808 stroke_polygon[i]=polygon_primitive[0];
7809 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7812 stroke_polygon[i]=polygon_primitive[0];
7813 stroke_polygon[i].point=stroke_polygon[0].point;
7815 stroke_polygon[i].primitive=UndefinedPrimitive;
7816 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7817 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7818 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7819 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7820 return(stroke_polygon);