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 ThrowMagickException(mvg_info->exception, GetMagickModule(),
2318 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2319 return(MagickFalse);
2324 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2326 primitive_info[i].primitive=UndefinedPrimitive;
2327 primitive_info[i].text=(
char *) NULL;
2329 *mvg_info->primitive_info=primitive_info;
2330 *mvg_info->extent=extent;
2334static inline double GetDrawValue(
const char *magick_restrict
string,
2335 char **magick_restrict sentinel)
2338 **magick_restrict q;
2344 value=InterpretLocaleValue(
string,q);
2349static int MVGMacroCompare(
const void *target,
const void *source)
2355 p=(
const char *) target;
2356 q=(
const char *) source;
2357 return(strcmp(p,q));
2360static SplayTreeInfo *GetMVGMacros(
const char *primitive)
2378 if (primitive == (
const char *) NULL)
2379 return((SplayTreeInfo *) NULL);
2380 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2381 RelinquishMagickMemory);
2382 macro=AcquireString(primitive);
2383 token=AcquireString(primitive);
2384 extent=strlen(token)+MagickPathExtent;
2385 for (q=primitive; *q !=
'\0'; )
2387 if (GetNextToken(q,&q,extent,token) < 1)
2391 if (LocaleCompare(
"push",token) == 0)
2397 (void) GetNextToken(q,&q,extent,token);
2401 name[MagickPathExtent];
2412 (void) GetNextToken(q,&q,extent,token);
2415 (void) CopyMagickString(name,token,MagickPathExtent);
2417 for (p=q; *p !=
'\0'; )
2419 if (GetNextToken(p,&p,extent,token) < 1)
2423 if (LocaleCompare(token,
"pop") == 0)
2425 end=p-strlen(token)-1;
2428 if (LocaleCompare(token,
"push") == 0)
2430 if ((n == 0) && (end >= start))
2433 length=(size_t) (end-start);
2438 (void) GetNextToken(p,&p,extent,token);
2441 (void) CopyMagickString(macro,start,length);
2442 (void) AddValueToSplayTree(macros,ConstantString(name),
2443 ConstantString(macro));
2451 token=DestroyString(token);
2452 macro=DestroyString(macro);
2456static inline MagickBooleanType IsPoint(
const char *point)
2464 value=GetDrawValue(point,&p);
2465 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2469static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2470 const PointInfo point)
2472 primitive_info->coordinates=1;
2473 primitive_info->closed_subpath=MagickFalse;
2474 primitive_info->point=point;
2478static MagickBooleanType RenderMVGContent(Image *image,
2479 const DrawInfo *draw_info,
const size_t depth,ExceptionInfo *exception)
2481#define RenderImageTag "Render/Image"
2488 keyword[MagickPathExtent],
2489 geometry[MagickPathExtent],
2491 pattern[MagickPathExtent],
2554 assert(image != (Image *) NULL);
2555 assert(image->signature == MagickCoreSignature);
2556 assert(draw_info != (DrawInfo *) NULL);
2557 assert(draw_info->signature == MagickCoreSignature);
2558 if (IsEventLogging() != MagickFalse)
2559 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2560 if (depth > MagickMaxRecursionDepth)
2561 ThrowBinaryException(DrawError,
"VectorGraphicsNestedTooDeeply",
2563 if ((draw_info->primitive == (
char *) NULL) ||
2564 (*draw_info->primitive ==
'\0'))
2565 return(MagickFalse);
2566 if (draw_info->debug != MagickFalse)
2567 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"begin draw-image");
2568 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2569 return(MagickFalse);
2570 if ((image->alpha_trait & BlendPixelTrait) == 0)
2572 status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2573 if (status == MagickFalse)
2574 return(MagickFalse);
2576 if ((*draw_info->primitive ==
'@') && (strlen(draw_info->primitive) > 1) &&
2577 (*(draw_info->primitive+1) !=
'-') && (depth == 0))
2578 primitive=FileToString(draw_info->primitive,~0UL,exception);
2580 primitive=AcquireString(draw_info->primitive);
2581 if (primitive == (
char *) NULL)
2582 return(MagickFalse);
2583 primitive_extent=(double) strlen(primitive);
2584 (void) SetImageArtifact(image,
"mvg:vector-graphics",primitive);
2587 stops=(StopInfo *) NULL;
2591 graphic_context=(DrawInfo **) AcquireMagickMemory(
sizeof(*graphic_context));
2592 if (graphic_context == (DrawInfo **) NULL)
2594 primitive=DestroyString(primitive);
2595 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2598 number_points=(size_t) PrimitiveExtentPad;
2599 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
2600 (number_points+1),
sizeof(*primitive_info));
2601 if (primitive_info == (PrimitiveInfo *) NULL)
2603 primitive=DestroyString(primitive);
2604 for ( ; n >= 0; n--)
2605 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2606 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2607 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2610 (void) memset(primitive_info,0,(
size_t) (number_points+1)*
2611 sizeof(*primitive_info));
2612 (void) memset(&mvg_info,0,
sizeof(mvg_info));
2613 mvg_info.primitive_info=(&primitive_info);
2614 mvg_info.extent=(&number_points);
2615 mvg_info.exception=exception;
2616 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2617 graphic_context[n]->viewbox=image->page;
2618 if ((image->page.width == 0) || (image->page.height == 0))
2620 graphic_context[n]->viewbox.width=image->columns;
2621 graphic_context[n]->viewbox.height=image->rows;
2623 token=AcquireString(primitive);
2624 extent=strlen(token)+MagickPathExtent;
2628 macros=GetMVGMacros(primitive);
2630 for (q=primitive; *q !=
'\0'; )
2635 if (GetNextToken(q,&q,MagickPathExtent,keyword) < 1)
2637 if (*keyword ==
'\0')
2639 if (*keyword ==
'#')
2644 while ((*q !=
'\n') && (*q !=
'\0'))
2648 p=q-strlen(keyword)-1;
2649 primitive_type=UndefinedPrimitive;
2650 current=graphic_context[n]->affine;
2651 GetAffineMatrix(&affine);
2660 if (LocaleCompare(
"affine",keyword) == 0)
2662 (void) GetNextToken(q,&q,extent,token);
2663 affine.sx=GetDrawValue(token,&next_token);
2664 if (token == next_token)
2665 ThrowPointExpectedException(token,exception);
2666 (void) GetNextToken(q,&q,extent,token);
2668 (void) GetNextToken(q,&q,extent,token);
2669 affine.ry=GetDrawValue(token,&next_token);
2670 if (token == next_token)
2671 ThrowPointExpectedException(token,exception);
2672 (void) GetNextToken(q,&q,extent,token);
2674 (void) GetNextToken(q,&q,extent,token);
2675 affine.rx=GetDrawValue(token,&next_token);
2676 if (token == next_token)
2677 ThrowPointExpectedException(token,exception);
2678 (void) GetNextToken(q,&q,extent,token);
2680 (void) GetNextToken(q,&q,extent,token);
2681 affine.sy=GetDrawValue(token,&next_token);
2682 if (token == next_token)
2683 ThrowPointExpectedException(token,exception);
2684 (void) GetNextToken(q,&q,extent,token);
2686 (void) GetNextToken(q,&q,extent,token);
2687 affine.tx=GetDrawValue(token,&next_token);
2688 if (token == next_token)
2689 ThrowPointExpectedException(token,exception);
2690 (void) GetNextToken(q,&q,extent,token);
2692 (void) GetNextToken(q,&q,extent,token);
2693 affine.ty=GetDrawValue(token,&next_token);
2694 if (token == next_token)
2695 ThrowPointExpectedException(token,exception);
2698 if (LocaleCompare(
"alpha",keyword) == 0)
2700 primitive_type=AlphaPrimitive;
2703 if (LocaleCompare(
"arc",keyword) == 0)
2705 primitive_type=ArcPrimitive;
2714 if (LocaleCompare(
"bezier",keyword) == 0)
2716 primitive_type=BezierPrimitive;
2719 if (LocaleCompare(
"border-color",keyword) == 0)
2721 (void) GetNextToken(q,&q,extent,token);
2722 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
2723 &graphic_context[n]->border_color,exception);
2732 if (LocaleCompare(
"class",keyword) == 0)
2737 (void) GetNextToken(q,&q,extent,token);
2738 if ((*token ==
'\0') || (*token ==
';'))
2746 for (i=0; i <= n; i++)
2747 if (LocaleCompare(token,graphic_context[i]->
id) == 0)
2751 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2752 if ((graphic_context[n]->render != MagickFalse) &&
2753 (mvg_class != (
const char *) NULL) && (p > primitive))
2764 (void) CloneString(&graphic_context[n]->
id,token);
2765 offset=(ssize_t) (p-primitive);
2766 elements=AcquireString(primitive);
2767 elements[offset]=
'\0';
2768 (void) ConcatenateString(&elements,mvg_class);
2769 (void) ConcatenateString(&elements,
"\n");
2770 (void) ConcatenateString(&elements,q);
2771 primitive=DestroyString(primitive);
2777 if (LocaleCompare(
"clip-path",keyword) == 0)
2785 (void) GetNextToken(q,&q,extent,token);
2791 (void) CloneString(&graphic_context[n]->clip_mask,token);
2792 clip_path=(
const char *) GetValueFromSplayTree(macros,token);
2793 if (clip_path != (
const char *) NULL)
2795 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2796 graphic_context[n]->clipping_mask=
2797 DestroyImage(graphic_context[n]->clipping_mask);
2798 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2799 graphic_context[n],token,clip_path,exception);
2800 if (graphic_context[n]->compliance != SVGCompliance)
2802 clip_path=(
const char *) GetValueFromSplayTree(macros,
2803 graphic_context[n]->clip_mask);
2804 if (clip_path != (
const char *) NULL)
2805 (void) SetImageArtifact(image,
2806 graphic_context[n]->clip_mask,clip_path);
2807 status&=(MagickStatusType) DrawClipPath(image,
2808 graphic_context[n],graphic_context[n]->clip_mask,
2814 if (LocaleCompare(
"clip-rule",keyword) == 0)
2819 (void) GetNextToken(q,&q,extent,token);
2820 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2822 if (fill_rule == -1)
2827 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2830 if (LocaleCompare(
"clip-units",keyword) == 0)
2835 (void) GetNextToken(q,&q,extent,token);
2836 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2838 if (clip_units == -1)
2843 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2844 if (clip_units == ObjectBoundingBox)
2846 GetAffineMatrix(¤t);
2847 affine.sx=draw_info->bounds.x2;
2848 affine.sy=draw_info->bounds.y2;
2849 affine.tx=draw_info->bounds.x1;
2850 affine.ty=draw_info->bounds.y1;
2855 if (LocaleCompare(
"circle",keyword) == 0)
2857 primitive_type=CirclePrimitive;
2860 if (LocaleCompare(
"color",keyword) == 0)
2862 primitive_type=ColorPrimitive;
2865 if (LocaleCompare(
"compliance",keyword) == 0)
2871 (void) GetNextToken(q,&q,extent,token);
2872 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2873 MagickComplianceOptions,MagickFalse,token);
2876 if (LocaleCompare(
"currentColor",keyword) == 0)
2878 (void) GetNextToken(q,&q,extent,token);
2887 if (LocaleCompare(
"decorate",keyword) == 0)
2892 (void) GetNextToken(q,&q,extent,token);
2893 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2900 graphic_context[n]->decorate=(DecorationType) decorate;
2903 if (LocaleCompare(
"density",keyword) == 0)
2905 (void) GetNextToken(q,&q,extent,token);
2906 (void) CloneString(&graphic_context[n]->density,token);
2909 if (LocaleCompare(
"direction",keyword) == 0)
2914 (void) GetNextToken(q,&q,extent,token);
2915 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2917 if (direction == -1)
2920 graphic_context[n]->direction=(DirectionType) direction;
2929 if (LocaleCompare(
"ellipse",keyword) == 0)
2931 primitive_type=EllipsePrimitive;
2934 if (LocaleCompare(
"encoding",keyword) == 0)
2936 (void) GetNextToken(q,&q,extent,token);
2937 (void) CloneString(&graphic_context[n]->encoding,token);
2946 if (LocaleCompare(
"fill",keyword) == 0)
2951 (void) GetNextToken(q,&q,extent,token);
2952 if (graphic_context[n]->clip_path != MagickFalse)
2954 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2955 if (mvg_class != (
const char *) NULL)
2957 (void) DrawPatternPath(image,draw_info,mvg_class,
2958 &graphic_context[n]->fill_pattern,exception);
2961 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
2962 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
2964 (void) DrawPatternPath(image,draw_info,token,
2965 &graphic_context[n]->fill_pattern,exception);
2968 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
2969 &graphic_context[n]->fill,exception);
2970 if (graphic_context[n]->fill_alpha != (
double) OpaqueAlpha)
2971 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2974 if (LocaleCompare(
"fill-opacity",keyword) == 0)
2979 (void) GetNextToken(q,&q,extent,token);
2980 if (graphic_context[n]->clip_path != MagickFalse)
2982 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
2983 opacity=MagickMin(MagickMax(factor*
2984 GetDrawValue(token,&next_token),0.0),1.0);
2985 if (token == next_token)
2986 ThrowPointExpectedException(token,exception);
2987 if (graphic_context[n]->compliance == SVGCompliance)
2988 graphic_context[n]->fill_alpha*=opacity;
2990 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
2991 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
2992 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
2994 graphic_context[n]->fill.alpha=(MagickRealType)
2995 ClampToQuantum((
double) QuantumRange*opacity);
2996 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
2999 if (LocaleCompare(
"fill-rule",keyword) == 0)
3004 (void) GetNextToken(q,&q,extent,token);
3005 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
3007 if (fill_rule == -1)
3012 graphic_context[n]->fill_rule=(FillRule) fill_rule;
3015 if (LocaleCompare(
"font",keyword) == 0)
3017 (void) GetNextToken(q,&q,extent,token);
3018 (void) CloneString(&graphic_context[n]->font,token);
3019 if (LocaleCompare(
"none",token) == 0)
3020 graphic_context[n]->font=(
char *) RelinquishMagickMemory(
3021 graphic_context[n]->font);
3024 if (LocaleCompare(
"font-family",keyword) == 0)
3026 (void) GetNextToken(q,&q,extent,token);
3027 (void) CloneString(&graphic_context[n]->family,token);
3030 if (LocaleCompare(
"font-size",keyword) == 0)
3032 (void) GetNextToken(q,&q,extent,token);
3033 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3034 if (token == next_token)
3035 ThrowPointExpectedException(token,exception);
3038 if (LocaleCompare(
"font-stretch",keyword) == 0)
3043 (void) GetNextToken(q,&q,extent,token);
3044 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3050 graphic_context[n]->stretch=(StretchType) stretch;
3053 if (LocaleCompare(
"font-style",keyword) == 0)
3058 (void) GetNextToken(q,&q,extent,token);
3059 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3065 graphic_context[n]->style=(StyleType) style;
3068 if (LocaleCompare(
"font-weight",keyword) == 0)
3073 (void) GetNextToken(q,&q,extent,token);
3074 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3076 weight=(ssize_t) StringToUnsignedLong(token);
3077 graphic_context[n]->weight=(size_t) weight;
3086 if (LocaleCompare(
"gradient-units",keyword) == 0)
3088 (void) GetNextToken(q,&q,extent,token);
3091 if (LocaleCompare(
"gravity",keyword) == 0)
3096 (void) GetNextToken(q,&q,extent,token);
3097 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3103 graphic_context[n]->gravity=(GravityType) gravity;
3112 if (LocaleCompare(
"image",keyword) == 0)
3117 primitive_type=ImagePrimitive;
3118 (void) GetNextToken(q,&q,extent,token);
3119 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3125 graphic_context[n]->compose=(CompositeOperator) compose;
3128 if (LocaleCompare(
"interline-spacing",keyword) == 0)
3130 (void) GetNextToken(q,&q,extent,token);
3131 graphic_context[n]->interline_spacing=GetDrawValue(token,
3133 if (token == next_token)
3134 ThrowPointExpectedException(token,exception);
3137 if (LocaleCompare(
"interword-spacing",keyword) == 0)
3139 (void) GetNextToken(q,&q,extent,token);
3140 graphic_context[n]->interword_spacing=GetDrawValue(token,
3142 if (token == next_token)
3143 ThrowPointExpectedException(token,exception);
3152 if (LocaleCompare(
"kerning",keyword) == 0)
3154 (void) GetNextToken(q,&q,extent,token);
3155 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3156 if (token == next_token)
3157 ThrowPointExpectedException(token,exception);
3166 if (LocaleCompare(
"letter-spacing",keyword) == 0)
3168 (void) GetNextToken(q,&q,extent,token);
3169 if (IsPoint(token) == MagickFalse)
3171 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3172 clone_info->text=AcquireString(
" ");
3173 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,&metrics,
3175 graphic_context[n]->kerning=metrics.width*
3176 GetDrawValue(token,&next_token);
3177 clone_info=DestroyDrawInfo(clone_info);
3178 if (token == next_token)
3179 ThrowPointExpectedException(token,exception);
3182 if (LocaleCompare(
"line",keyword) == 0)
3184 primitive_type=LinePrimitive;
3193 if (LocaleCompare(
"mask",keyword) == 0)
3201 (void) GetNextToken(q,&q,extent,token);
3202 mask_path=(
const char *) GetValueFromSplayTree(macros,token);
3203 if (mask_path != (
const char *) NULL)
3205 if (graphic_context[n]->composite_mask != (Image *) NULL)
3206 graphic_context[n]->composite_mask=
3207 DestroyImage(graphic_context[n]->composite_mask);
3208 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3209 graphic_context[n],token,mask_path,exception);
3210 if (graphic_context[n]->compliance != SVGCompliance)
3211 status=SetImageMask(image,CompositePixelMask,
3212 graphic_context[n]->composite_mask,exception);
3222 if (LocaleCompare(
"offset",keyword) == 0)
3224 (void) GetNextToken(q,&q,extent,token);
3227 if (LocaleCompare(
"opacity",keyword) == 0)
3232 (void) GetNextToken(q,&q,extent,token);
3233 if (graphic_context[n]->clip_path != MagickFalse)
3235 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3236 opacity=MagickMin(MagickMax(factor*
3237 GetDrawValue(token,&next_token),0.0),1.0);
3238 if (token == next_token)
3239 ThrowPointExpectedException(token,exception);
3240 if (graphic_context[n]->compliance == SVGCompliance)
3242 graphic_context[n]->fill_alpha*=opacity;
3243 graphic_context[n]->stroke_alpha*=opacity;
3247 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3248 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3250 if (graphic_context[n]->fill.alpha != (
double) TransparentAlpha)
3252 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3253 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3257 graphic_context[n]->fill.alpha=(MagickRealType)
3258 ClampToQuantum((
double) QuantumRange*opacity);
3259 graphic_context[n]->stroke.alpha=(MagickRealType)
3260 ClampToQuantum((
double) QuantumRange*opacity);
3262 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3263 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3272 if (LocaleCompare(
"path",keyword) == 0)
3274 primitive_type=PathPrimitive;
3277 if (LocaleCompare(
"point",keyword) == 0)
3279 primitive_type=PointPrimitive;
3282 if (LocaleCompare(
"polyline",keyword) == 0)
3284 primitive_type=PolylinePrimitive;
3287 if (LocaleCompare(
"polygon",keyword) == 0)
3289 primitive_type=PolygonPrimitive;
3292 if (LocaleCompare(
"pop",keyword) == 0)
3294 if (GetNextToken(q,&q,extent,token) < 1)
3296 if (LocaleCompare(
"class",token) == 0)
3298 if (LocaleCompare(
"clip-path",token) == 0)
3300 if (LocaleCompare(
"defs",token) == 0)
3303 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3307 if (LocaleCompare(
"gradient",token) == 0)
3309 if (LocaleCompare(
"graphic-context",token) == 0)
3313 (void) ThrowMagickException(exception,GetMagickModule(),
3314 DrawError,
"UnbalancedGraphicContextPushPop",
"`%s'",token);
3319 if ((graphic_context[n]->clip_mask != (
char *) NULL) &&
3320 (graphic_context[n]->compliance != SVGCompliance))
3321 if (LocaleCompare(graphic_context[n]->clip_mask,
3322 graphic_context[n-1]->clip_mask) != 0)
3323 status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3325 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3329 if (LocaleCompare(
"mask",token) == 0)
3331 if (LocaleCompare(
"pattern",token) == 0)
3333 if (LocaleCompare(
"symbol",token) == 0)
3336 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3343 if (LocaleCompare(
"push",keyword) == 0)
3345 if (GetNextToken(q,&q,extent,token) < 1)
3347 if (LocaleCompare(
"class",token) == 0)
3352 for (p=q; *q !=
'\0'; )
3354 if (GetNextToken(q,&q,extent,token) < 1)
3356 if (LocaleCompare(token,
"pop") != 0)
3358 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3359 if (LocaleCompare(token,
"class") != 0)
3363 (void) GetNextToken(q,&q,extent,token);
3366 if (LocaleCompare(
"clip-path",token) == 0)
3368 (void) GetNextToken(q,&q,extent,token);
3369 for (p=q; *q !=
'\0'; )
3371 if (GetNextToken(q,&q,extent,token) < 1)
3373 if (LocaleCompare(token,
"pop") != 0)
3375 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3376 if (LocaleCompare(token,
"clip-path") != 0)
3380 if ((q == (
char *) NULL) || (p == (
char *) NULL) || ((q-4) < p))
3385 (void) GetNextToken(q,&q,extent,token);
3388 if (LocaleCompare(
"defs",token) == 0)
3391 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3395 if (LocaleCompare(
"gradient",token) == 0)
3398 key[2*MagickPathExtent],
3399 name[MagickPathExtent],
3400 type[MagickPathExtent];
3405 (void) GetNextToken(q,&q,extent,token);
3406 (void) CopyMagickString(name,token,MagickPathExtent);
3407 (void) GetNextToken(q,&q,extent,token);
3408 (void) CopyMagickString(type,token,MagickPathExtent);
3409 (void) GetNextToken(q,&q,extent,token);
3410 segment.x1=GetDrawValue(token,&next_token);
3411 if (token == next_token)
3412 ThrowPointExpectedException(token,exception);
3413 (void) GetNextToken(q,&q,extent,token);
3415 (void) GetNextToken(q,&q,extent,token);
3416 segment.y1=GetDrawValue(token,&next_token);
3417 if (token == next_token)
3418 ThrowPointExpectedException(token,exception);
3419 (void) GetNextToken(q,&q,extent,token);
3421 (void) GetNextToken(q,&q,extent,token);
3422 segment.x2=GetDrawValue(token,&next_token);
3423 if (token == next_token)
3424 ThrowPointExpectedException(token,exception);
3425 (void) GetNextToken(q,&q,extent,token);
3427 (void) GetNextToken(q,&q,extent,token);
3428 segment.y2=GetDrawValue(token,&next_token);
3429 if (token == next_token)
3430 ThrowPointExpectedException(token,exception);
3431 if (LocaleCompare(type,
"radial") == 0)
3433 (void) GetNextToken(q,&q,extent,token);
3435 (void) GetNextToken(q,&q,extent,token);
3437 for (p=q; *q !=
'\0'; )
3439 if (GetNextToken(q,&q,extent,token) < 1)
3441 if (LocaleCompare(token,
"pop") != 0)
3443 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3444 if (LocaleCompare(token,
"gradient") != 0)
3448 if ((q == (
char *) NULL) || (*q ==
'\0') ||
3449 (p == (
char *) NULL) || ((q-4) < p) ||
3450 ((
size_t) (q-p+4+1) > extent))
3455 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3456 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3457 graphic_context[n]->affine.ry*segment.y1+
3458 graphic_context[n]->affine.tx;
3459 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3460 graphic_context[n]->affine.sy*segment.y1+
3461 graphic_context[n]->affine.ty;
3462 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3463 graphic_context[n]->affine.ry*segment.y2+
3464 graphic_context[n]->affine.tx;
3465 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3466 graphic_context[n]->affine.sy*segment.y2+
3467 graphic_context[n]->affine.ty;
3468 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3469 (void) SetImageArtifact(image,key,token);
3470 (void) FormatLocaleString(key,MagickPathExtent,
"%s-type",name);
3471 (void) SetImageArtifact(image,key,type);
3472 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3474 (void) FormatLocaleString(geometry,MagickPathExtent,
3475 "%gx%g%+.15g%+.15g",
3476 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3477 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3478 bounds.x1,bounds.y1);
3479 (void) SetImageArtifact(image,key,geometry);
3480 (void) GetNextToken(q,&q,extent,token);
3483 if (LocaleCompare(
"graphic-context",token) == 0)
3486 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3487 graphic_context,(
size_t) (n+1),
sizeof(*graphic_context));
3488 if (graphic_context == (DrawInfo **) NULL)
3490 (void) ThrowMagickException(exception,GetMagickModule(),
3491 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3496 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3497 graphic_context[n-1]);
3500 (void) GetNextToken(q,&q,extent,token);
3501 (void) CloneString(&graphic_context[n]->
id,token);
3503 if (n > MagickMaxRecursionDepth)
3505 (void) ThrowMagickException(exception,GetMagickModule(),
3506 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",
3512 if (LocaleCompare(
"mask",token) == 0)
3514 (void) GetNextToken(q,&q,extent,token);
3517 if (LocaleCompare(
"pattern",token) == 0)
3520 key[2*MagickPathExtent],
3521 name[MagickPathExtent];
3526 (void) GetNextToken(q,&q,extent,token);
3527 (void) CopyMagickString(name,token,MagickPathExtent);
3528 (void) GetNextToken(q,&q,extent,token);
3529 region.x=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3531 if (token == next_token)
3532 ThrowPointExpectedException(token,exception);
3533 (void) GetNextToken(q,&q,extent,token);
3535 (void) GetNextToken(q,&q,extent,token);
3536 region.y=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3538 if (token == next_token)
3539 ThrowPointExpectedException(token,exception);
3540 (void) GetNextToken(q,&q,extent,token);
3542 (void) GetNextToken(q,&q,extent,token);
3543 region.width=CastDoubleToSizeT(floor(GetDrawValue(token,
3545 if (token == next_token)
3546 ThrowPointExpectedException(token,exception);
3547 (void) GetNextToken(q,&q,extent,token);
3549 (void) GetNextToken(q,&q,extent,token);
3550 region.height=CastDoubleToSizeT(GetDrawValue(token,
3552 if (token == next_token)
3553 ThrowPointExpectedException(token,exception);
3554 for (p=q; *q !=
'\0'; )
3556 if (GetNextToken(q,&q,extent,token) < 1)
3558 if (LocaleCompare(token,
"pop") != 0)
3560 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3561 if (LocaleCompare(token,
"pattern") != 0)
3565 if ((q == (
char *) NULL) || (p == (
char *) NULL) ||
3566 ((q-4) < p) || ((
size_t) (q-p+4+1) > extent))
3571 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3572 (void) FormatLocaleString(key,MagickPathExtent,
"%s",name);
3573 (void) SetImageArtifact(image,key,token);
3574 (void) FormatLocaleString(key,MagickPathExtent,
"%s-geometry",
3576 (void) FormatLocaleString(geometry,MagickPathExtent,
3577 "%.20gx%.20g%+.20g%+.20g",(
double) region.width,(
double)
3578 region.height,(
double) region.x,(
double) region.y);
3579 (void) SetImageArtifact(image,key,geometry);
3580 (void) GetNextToken(q,&q,extent,token);
3583 if (LocaleCompare(
"symbol",token) == 0)
3586 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3599 if (LocaleCompare(
"rectangle",keyword) == 0)
3601 primitive_type=RectanglePrimitive;
3604 if (LocaleCompare(
"rotate",keyword) == 0)
3606 (void) GetNextToken(q,&q,extent,token);
3607 angle=GetDrawValue(token,&next_token);
3608 if (token == next_token)
3609 ThrowPointExpectedException(token,exception);
3610 affine.sx=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3611 affine.rx=sin(DegreesToRadians(fmod((
double) angle,360.0)));
3612 affine.ry=(-sin(DegreesToRadians(fmod((
double) angle,360.0))));
3613 affine.sy=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3616 if (LocaleCompare(
"roundRectangle",keyword) == 0)
3618 primitive_type=RoundRectanglePrimitive;
3627 if (LocaleCompare(
"scale",keyword) == 0)
3629 (void) GetNextToken(q,&q,extent,token);
3630 affine.sx=GetDrawValue(token,&next_token);
3631 if (token == next_token)
3632 ThrowPointExpectedException(token,exception);
3633 (void) GetNextToken(q,&q,extent,token);
3635 (void) GetNextToken(q,&q,extent,token);
3636 affine.sy=GetDrawValue(token,&next_token);
3637 if (token == next_token)
3638 ThrowPointExpectedException(token,exception);
3641 if (LocaleCompare(
"skewX",keyword) == 0)
3643 (void) GetNextToken(q,&q,extent,token);
3644 angle=GetDrawValue(token,&next_token);
3645 if (token == next_token)
3646 ThrowPointExpectedException(token,exception);
3647 affine.ry=sin(DegreesToRadians(angle));
3650 if (LocaleCompare(
"skewY",keyword) == 0)
3652 (void) GetNextToken(q,&q,extent,token);
3653 angle=GetDrawValue(token,&next_token);
3654 if (token == next_token)
3655 ThrowPointExpectedException(token,exception);
3656 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3659 if (LocaleCompare(
"stop-color",keyword) == 0)
3665 if (number_stops == 1)
3666 stops=(StopInfo *) AcquireQuantumMemory(2,
sizeof(*stops));
3668 if (number_stops > 2)
3669 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3671 if (stops == (StopInfo *) NULL)
3673 (void) ThrowMagickException(exception,GetMagickModule(),
3674 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3679 (void) GetNextToken(q,&q,extent,token);
3680 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3681 &stop_color,exception);
3682 stops[number_stops-1].color=stop_color;
3683 (void) GetNextToken(q,&q,extent,token);
3684 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3685 stops[number_stops-1].offset=factor*GetDrawValue(token,
3687 if (token == next_token)
3688 ThrowPointExpectedException(token,exception);
3691 if (LocaleCompare(
"stroke",keyword) == 0)
3696 (void) GetNextToken(q,&q,extent,token);
3697 if (graphic_context[n]->clip_path != MagickFalse)
3699 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
3700 if (mvg_class != (
const char *) NULL)
3702 (void) DrawPatternPath(image,draw_info,mvg_class,
3703 &graphic_context[n]->stroke_pattern,exception);
3706 (void) FormatLocaleString(pattern,MagickPathExtent,
"%s",token);
3707 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
3709 (void) DrawPatternPath(image,draw_info,token,
3710 &graphic_context[n]->stroke_pattern,exception);
3713 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3714 &graphic_context[n]->stroke,exception);
3715 if (graphic_context[n]->stroke_alpha != (
double) OpaqueAlpha)
3716 graphic_context[n]->stroke.alpha=
3717 graphic_context[n]->stroke_alpha;
3720 if (LocaleCompare(
"stroke-antialias",keyword) == 0)
3722 (void) GetNextToken(q,&q,extent,token);
3723 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3724 MagickTrue : MagickFalse;
3727 if (LocaleCompare(
"stroke-dasharray",keyword) == 0)
3729 if (graphic_context[n]->dash_pattern != (
double *) NULL)
3730 graphic_context[n]->dash_pattern=(
double *)
3731 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3732 if (IsPoint(q) != MagickFalse)
3738 (void) GetNextToken(r,&r,extent,token);
3740 (void) GetNextToken(r,&r,extent,token);
3741 for (x=0; IsPoint(token) != MagickFalse; x++)
3743 (void) GetNextToken(r,&r,extent,token);
3745 (void) GetNextToken(r,&r,extent,token);
3747 graphic_context[n]->dash_pattern=(
double *)
3748 AcquireQuantumMemory((
size_t) (2*x+2),
3749 sizeof(*graphic_context[n]->dash_pattern));
3750 if (graphic_context[n]->dash_pattern == (
double *) NULL)
3752 (void) ThrowMagickException(exception,GetMagickModule(),
3753 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
3758 (void) memset(graphic_context[n]->dash_pattern,0,(
size_t)
3759 (2*x+2)*
sizeof(*graphic_context[n]->dash_pattern));
3760 for (j=0; j < x; j++)
3762 (void) GetNextToken(q,&q,extent,token);
3764 (void) GetNextToken(q,&q,extent,token);
3765 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3767 if (token == next_token)
3768 ThrowPointExpectedException(token,exception);
3769 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3772 if ((x & 0x01) != 0)
3773 for ( ; j < (2*x); j++)
3774 graphic_context[n]->dash_pattern[j]=
3775 graphic_context[n]->dash_pattern[j-x];
3776 graphic_context[n]->dash_pattern[j]=0.0;
3779 (void) GetNextToken(q,&q,extent,token);
3782 if (LocaleCompare(
"stroke-dashoffset",keyword) == 0)
3784 (void) GetNextToken(q,&q,extent,token);
3785 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3786 if (token == next_token)
3787 ThrowPointExpectedException(token,exception);
3790 if (LocaleCompare(
"stroke-linecap",keyword) == 0)
3795 (void) GetNextToken(q,&q,extent,token);
3796 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3802 graphic_context[n]->linecap=(LineCap) linecap;
3805 if (LocaleCompare(
"stroke-linejoin",keyword) == 0)
3810 (void) GetNextToken(q,&q,extent,token);
3811 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3818 graphic_context[n]->linejoin=(LineJoin) linejoin;
3821 if (LocaleCompare(
"stroke-miterlimit",keyword) == 0)
3823 (void) GetNextToken(q,&q,extent,token);
3824 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3827 if (LocaleCompare(
"stroke-opacity",keyword) == 0)
3832 (void) GetNextToken(q,&q,extent,token);
3833 if (graphic_context[n]->clip_path != MagickFalse)
3835 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3836 opacity=MagickMin(MagickMax(factor*GetDrawValue(token,&next_token),
3838 if (token == next_token)
3839 ThrowPointExpectedException(token,exception);
3840 if (graphic_context[n]->compliance == SVGCompliance)
3841 graphic_context[n]->stroke_alpha*=opacity;
3843 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3844 if (graphic_context[n]->stroke.alpha != (
double) TransparentAlpha)
3845 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3847 graphic_context[n]->stroke.alpha=(MagickRealType)
3848 ClampToQuantum((
double) QuantumRange*opacity);
3849 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3852 if (LocaleCompare(
"stroke-width",keyword) == 0)
3854 (void) GetNextToken(q,&q,extent,token);
3855 if (graphic_context[n]->clip_path != MagickFalse)
3857 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3858 if ((token == next_token) ||
3859 (graphic_context[n]->stroke_width < 0.0))
3860 ThrowPointExpectedException(token,exception);
3869 if (LocaleCompare(
"text",keyword) == 0)
3871 primitive_type=TextPrimitive;
3875 if (LocaleCompare(
"text-align",keyword) == 0)
3880 (void) GetNextToken(q,&q,extent,token);
3881 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3887 graphic_context[n]->align=(AlignType) align;
3890 if (LocaleCompare(
"text-anchor",keyword) == 0)
3895 (void) GetNextToken(q,&q,extent,token);
3896 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3902 graphic_context[n]->align=(AlignType) align;
3905 if (LocaleCompare(
"text-antialias",keyword) == 0)
3907 (void) GetNextToken(q,&q,extent,token);
3908 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3909 MagickTrue : MagickFalse;
3912 if (LocaleCompare(
"text-undercolor",keyword) == 0)
3914 (void) GetNextToken(q,&q,extent,token);
3915 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3916 &graphic_context[n]->undercolor,exception);
3919 if (LocaleCompare(
"translate",keyword) == 0)
3921 (void) GetNextToken(q,&q,extent,token);
3922 affine.tx=GetDrawValue(token,&next_token);
3923 if (token == next_token)
3924 ThrowPointExpectedException(token,exception);
3925 (void) GetNextToken(q,&q,extent,token);
3927 (void) GetNextToken(q,&q,extent,token);
3928 affine.ty=GetDrawValue(token,&next_token);
3929 if (token == next_token)
3930 ThrowPointExpectedException(token,exception);
3940 if (LocaleCompare(
"use",keyword) == 0)
3948 (void) GetNextToken(q,&q,extent,token);
3949 use=(
const char *) GetValueFromSplayTree(macros,token);
3950 if (use != (
const char *) NULL)
3952 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3953 (void) CloneString(&clone_info->primitive,use);
3954 status=RenderMVGContent(image,clone_info,depth+1,exception);
3955 clone_info=DestroyDrawInfo(clone_info);
3965 if (LocaleCompare(
"viewbox",keyword) == 0)
3967 (void) GetNextToken(q,&q,extent,token);
3968 graphic_context[n]->viewbox.x=CastDoubleToSsizeT(ceil(
3969 GetDrawValue(token,&next_token)-0.5));
3970 if (token == next_token)
3971 ThrowPointExpectedException(token,exception);
3972 (void) GetNextToken(q,&q,extent,token);
3974 (void) GetNextToken(q,&q,extent,token);
3975 graphic_context[n]->viewbox.y=CastDoubleToSsizeT(
3976 ceil(GetDrawValue(token,&next_token)-0.5));
3977 if (token == next_token)
3978 ThrowPointExpectedException(token,exception);
3979 (void) GetNextToken(q,&q,extent,token);
3981 (void) GetNextToken(q,&q,extent,token);
3982 graphic_context[n]->viewbox.width=CastDoubleToSizeT(floor(
3983 GetDrawValue(token,&next_token)+0.5));
3984 if (token == next_token)
3985 ThrowPointExpectedException(token,exception);
3986 (void) GetNextToken(q,&q,extent,token);
3988 (void) GetNextToken(q,&q,extent,token);
3989 graphic_context[n]->viewbox.height=CastDoubleToSizeT(floor(
3990 GetDrawValue(token,&next_token)+0.5));
3991 if (token == next_token)
3992 ThrowPointExpectedException(token,exception);
4001 if (LocaleCompare(
"word-spacing",keyword) == 0)
4003 (void) GetNextToken(q,&q,extent,token);
4004 graphic_context[n]->interword_spacing=GetDrawValue(token,
4006 if (token == next_token)
4007 ThrowPointExpectedException(token,exception);
4019 if (status == MagickFalse)
4021 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
4022 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
4023 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
4024 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
4026 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
4027 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
4028 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
4029 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
4030 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
4032 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4035 if (primitive_type == UndefinedPrimitive)
4039 if (number_stops > 1)
4044 type=LinearGradient;
4045 if (draw_info->gradient.type == RadialGradient)
4046 type=RadialGradient;
4047 (void) GradientImage(image,type,PadSpread,stops,number_stops,
4050 if (number_stops > 0)
4051 stops=(StopInfo *) RelinquishMagickMemory(stops);
4053 if ((draw_info->debug != MagickFalse) && (q > p))
4054 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int)
4061 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4062 if (primitive_info[i].text != (
char *) NULL)
4063 primitive_info[i].text=DestroyString(primitive_info[i].text);
4067 primitive_info[0].primitive=primitive_type;
4068 primitive_info[0].point.x=0.0;
4069 primitive_info[0].point.y=0.0;
4070 primitive_info[0].coordinates=0;
4071 primitive_info[0].method=FloodfillMethod;
4072 primitive_info[0].closed_subpath=MagickFalse;
4073 for (x=0; *q !=
'\0'; x++)
4078 if (IsPoint(q) == MagickFalse)
4080 (void) GetNextToken(q,&q,extent,token);
4081 point.x=GetDrawValue(token,&next_token);
4082 if (token == next_token)
4083 ThrowPointExpectedException(token,exception);
4084 (void) GetNextToken(q,&q,extent,token);
4086 (void) GetNextToken(q,&q,extent,token);
4087 point.y=GetDrawValue(token,&next_token);
4088 if (token == next_token)
4089 ThrowPointExpectedException(token,exception);
4090 (void) GetNextToken(q,(
const char **) NULL,extent,token);
4092 (void) GetNextToken(q,&q,extent,token);
4093 primitive_info[i].primitive=primitive_type;
4094 primitive_info[i].point=point;
4095 primitive_info[i].coordinates=0;
4096 primitive_info[i].method=FloodfillMethod;
4097 primitive_info[i].closed_subpath=MagickFalse;
4100 if (i < (ssize_t) number_points)
4102 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4104 primitive_info=(*mvg_info.primitive_info);
4106 if (status == MagickFalse)
4108 if (primitive_info[j].text != (
char *) NULL)
4109 primitive_info[j].text=DestroyString(primitive_info[j].text);
4110 primitive_info[j].primitive=primitive_type;
4111 primitive_info[j].coordinates=(size_t) x;
4112 primitive_info[j].method=FloodfillMethod;
4113 primitive_info[j].closed_subpath=MagickFalse;
4117 bounds.x1=primitive_info[j].point.x;
4118 bounds.y1=primitive_info[j].point.y;
4119 bounds.x2=primitive_info[j].point.x;
4120 bounds.y2=primitive_info[j].point.y;
4121 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4123 point=primitive_info[j+k].point;
4124 if (point.x < bounds.x1)
4126 if (point.y < bounds.y1)
4128 if (point.x > bounds.x2)
4130 if (point.y > bounds.y2)
4136 coordinates=(double) primitive_info[j].coordinates;
4137 switch (primitive_type)
4139 case RectanglePrimitive:
4144 case RoundRectanglePrimitive:
4151 alpha=bounds.x2-bounds.x1;
4152 beta=bounds.y2-bounds.y1;
4153 radius=hypot(alpha,beta);
4155 coordinates+=2.0*((size_t) ceil((
double) MagickPI*radius))+6.0*
4156 BezierQuantum+360.0;
4159 case BezierPrimitive:
4161 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4170 (void) GetNextToken(q,&q,extent,token);
4173 for (s=token; *s !=
'\0'; s=t)
4178 value=GetDrawValue(s,&t);
4187 for (s=token; *s !=
'\0'; s++)
4188 if (strspn(s,
"AaCcQqSsTt") != 0)
4189 coordinates+=(20.0*BezierQuantum)+360.0;
4195 if (status == MagickFalse)
4197 if (((
size_t) (i+coordinates)) >= number_points)
4202 number_points+=(size_t) coordinates+1;
4203 if (number_points < (
size_t) coordinates)
4205 (void) ThrowMagickException(exception,GetMagickModule(),
4206 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
4212 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4214 primitive_info=(*mvg_info.primitive_info);
4216 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,
4217 PrimitiveExtentPad);
4218 primitive_info=(*mvg_info.primitive_info);
4219 if (status == MagickFalse)
4222 switch (primitive_type)
4224 case PointPrimitive:
4227 if (primitive_info[j].coordinates != 1)
4232 status&=(MagickStatusType) TracePoint(primitive_info+j,
4233 primitive_info[j].point);
4234 primitive_info=(*mvg_info.primitive_info);
4235 i=j+(ssize_t) primitive_info[j].coordinates;
4240 if (primitive_info[j].coordinates != 2)
4245 status&=(MagickStatusType) TraceLine(primitive_info+j,
4246 primitive_info[j].point,primitive_info[j+1].point);
4247 primitive_info=(*mvg_info.primitive_info);
4248 i=j+(ssize_t) primitive_info[j].coordinates;
4251 case RectanglePrimitive:
4253 if (primitive_info[j].coordinates != 2)
4258 status&=(MagickStatusType) TraceRectangle(primitive_info+j,
4259 primitive_info[j].point,primitive_info[j+1].point);
4260 primitive_info=(*mvg_info.primitive_info);
4261 i=j+(ssize_t) primitive_info[j].coordinates;
4264 case RoundRectanglePrimitive:
4266 if (primitive_info[j].coordinates != 3)
4271 if ((primitive_info[j+2].point.x < 0.0) ||
4272 (primitive_info[j+2].point.y < 0.0))
4277 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4282 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4287 status&=(MagickStatusType) TraceRoundRectangle(&mvg_info,
4288 primitive_info[j].point,primitive_info[j+1].point,
4289 primitive_info[j+2].point);
4290 primitive_info=(*mvg_info.primitive_info);
4291 i=j+(ssize_t) primitive_info[j].coordinates;
4296 if (primitive_info[j].coordinates != 3)
4301 status&=(MagickStatusType) TraceArc(&mvg_info,primitive_info[j].point,
4302 primitive_info[j+1].point,primitive_info[j+2].point);
4303 primitive_info=(*mvg_info.primitive_info);
4304 i=j+(ssize_t) primitive_info[j].coordinates;
4307 case EllipsePrimitive:
4309 if (primitive_info[j].coordinates != 3)
4314 if ((primitive_info[j+1].point.x < 0.0) ||
4315 (primitive_info[j+1].point.y < 0.0))
4320 status&=(MagickStatusType) TraceEllipse(&mvg_info,
4321 primitive_info[j].point,primitive_info[j+1].point,
4322 primitive_info[j+2].point);
4323 primitive_info=(*mvg_info.primitive_info);
4324 i=j+(ssize_t) primitive_info[j].coordinates;
4327 case CirclePrimitive:
4329 if (primitive_info[j].coordinates != 2)
4334 status&=(MagickStatusType) TraceCircle(&mvg_info,
4335 primitive_info[j].point,primitive_info[j+1].point);
4336 primitive_info=(*mvg_info.primitive_info);
4337 i=j+(ssize_t) primitive_info[j].coordinates;
4340 case PolylinePrimitive:
4342 if (primitive_info[j].coordinates < 1)
4349 case PolygonPrimitive:
4351 if (primitive_info[j].coordinates < 3)
4356 primitive_info[i]=primitive_info[j];
4357 primitive_info[i].coordinates=0;
4358 primitive_info[j].coordinates++;
4359 primitive_info[j].closed_subpath=MagickTrue;
4363 case BezierPrimitive:
4365 if (primitive_info[j].coordinates < 3)
4370 status&=(MagickStatusType) TraceBezier(&mvg_info,
4371 primitive_info[j].coordinates);
4372 primitive_info=(*mvg_info.primitive_info);
4373 i=j+(ssize_t) primitive_info[j].coordinates;
4378 coordinates=(double) TracePath(&mvg_info,token,exception);
4379 primitive_info=(*mvg_info.primitive_info);
4380 if (coordinates < 0.0)
4385 i=(ssize_t) (j+coordinates);
4388 case AlphaPrimitive:
4389 case ColorPrimitive:
4394 if (primitive_info[j].coordinates != 1)
4399 (void) GetNextToken(q,&q,extent,token);
4400 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4406 primitive_info[j].method=(PaintMethod) method;
4411 if (primitive_info[j].coordinates != 1)
4417 (void) GetNextToken(q,&q,extent,token);
4418 (void) CloneString(&primitive_info[j].text,token);
4422 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4423 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4424 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4426 mvg_info.point=primitive_info->point;
4427 primitive_info->point.x+=cursor;
4431 mvg_info.point=primitive_info->point;
4434 clone_info->render=MagickFalse;
4435 clone_info->text=AcquireString(token);
4436 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,
4437 &metrics,exception);
4438 clone_info=DestroyDrawInfo(clone_info);
4439 cursor+=metrics.width;
4440 if (graphic_context[n]->compliance != SVGCompliance)
4444 case ImagePrimitive:
4446 if (primitive_info[j].coordinates != 2)
4451 (void) GetNextToken(q,&q,extent,token);
4452 (void) CloneString(&primitive_info[j].text,token);
4459 primitive_info[i].primitive=UndefinedPrimitive;
4460 if ((draw_info->debug != MagickFalse) && (q > p))
4461 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int) (q-p),p);
4465 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4466 &graphic_context[n]->affine));
4467 primitive_info=(*mvg_info.primitive_info);
4470 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(
double)
4471 graphic_context[n]->stroke_width);
4472 primitive_info=(*mvg_info.primitive_info);
4480 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4482 point=primitive_info[i].point;
4483 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4484 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4485 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4486 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4487 point=primitive_info[i].point;
4488 if (point.x < graphic_context[n]->bounds.x1)
4489 graphic_context[n]->bounds.x1=point.x;
4490 if (point.y < graphic_context[n]->bounds.y1)
4491 graphic_context[n]->bounds.y1=point.y;
4492 if (point.x > graphic_context[n]->bounds.x2)
4493 graphic_context[n]->bounds.x2=point.x;
4494 if (point.y > graphic_context[n]->bounds.y2)
4495 graphic_context[n]->bounds.y2=point.y;
4496 if (primitive_info[i].primitive == ImagePrimitive)
4498 if (i >= (ssize_t) number_points)
4499 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
4501 if (graphic_context[n]->render != MagickFalse)
4503 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4504 (graphic_context[n]->clip_mask != (
char *) NULL) &&
4505 (LocaleCompare(graphic_context[n]->clip_mask,
4506 graphic_context[n-1]->clip_mask) != 0))
4511 clip_path=(
const char *) GetValueFromSplayTree(macros,
4512 graphic_context[n]->clip_mask);
4513 if (clip_path != (
const char *) NULL)
4514 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4516 status&=(MagickStatusType) DrawClipPath(image,graphic_context[n],
4517 graphic_context[n]->clip_mask,exception);
4519 status&=(MagickStatusType) DrawPrimitive(image,graphic_context[n],
4520 primitive_info,exception);
4522 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4524 if (proceed == MagickFalse)
4529 if (draw_info->debug != MagickFalse)
4530 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end draw-image");
4534 macros=DestroySplayTree(macros);
4535 token=DestroyString(token);
4536 if (primitive_info != (PrimitiveInfo *) NULL)
4538 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4539 if (primitive_info[i].text != (
char *) NULL)
4540 primitive_info[i].text=DestroyString(primitive_info[i].text);
4541 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4543 primitive=DestroyString(primitive);
4544 if (stops != (StopInfo *) NULL)
4545 stops=(StopInfo *) RelinquishMagickMemory(stops);
4546 for ( ; n >= 0; n--)
4547 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4548 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4549 if ((status == MagickFalse) && (exception->severity < ErrorException))
4550 ThrowBinaryException(DrawError,
"NonconformingDrawingPrimitiveDefinition",
4552 return(status != 0 ? MagickTrue : MagickFalse);
4555MagickExport MagickBooleanType DrawImage(Image *image,
const DrawInfo *draw_info,
4556 ExceptionInfo *exception)
4558 return(RenderMVGContent(image,draw_info,0,exception));
4592MagickExport MagickBooleanType DrawPatternPath(Image *image,
4593 const DrawInfo *draw_info,
const char *name,Image **pattern,
4594 ExceptionInfo *exception)
4597 property[MagickPathExtent];
4613 assert(image != (Image *) NULL);
4614 assert(image->signature == MagickCoreSignature);
4615 assert(draw_info != (
const DrawInfo *) NULL);
4616 assert(name != (
const char *) NULL);
4617 if (IsEventLogging() != MagickFalse)
4618 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4619 (void) FormatLocaleString(property,MagickPathExtent,
"%s",name);
4620 path=GetImageArtifact(image,property);
4621 if (path == (
const char *) NULL)
4622 return(MagickFalse);
4623 (void) FormatLocaleString(property,MagickPathExtent,
"%s-geometry",name);
4624 geometry=GetImageArtifact(image,property);
4625 if (geometry == (
const char *) NULL)
4626 return(MagickFalse);
4627 if ((*pattern) != (Image *) NULL)
4628 *pattern=DestroyImage(*pattern);
4629 image_info=AcquireImageInfo();
4630 image_info->size=AcquireString(geometry);
4631 *pattern=AcquireImage(image_info,exception);
4632 image_info=DestroyImageInfo(image_info);
4633 (void) QueryColorCompliance(
"#00000000",AllCompliance,
4634 &(*pattern)->background_color,exception);
4635 (void) SetImageBackgroundColor(*pattern,exception);
4636 if (draw_info->debug != MagickFalse)
4637 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4638 "begin pattern-path %s %s",name,geometry);
4639 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4640 if (clone_info->fill_pattern != (Image *) NULL)
4641 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4642 if (clone_info->stroke_pattern != (Image *) NULL)
4643 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4644 (void) FormatLocaleString(property,MagickPathExtent,
"%s-type",name);
4645 type=GetImageArtifact(image,property);
4646 if (type != (
const char *) NULL)
4647 clone_info->gradient.type=(GradientType) ParseCommandOption(
4648 MagickGradientOptions,MagickFalse,type);
4649 (void) CloneString(&clone_info->primitive,path);
4650 status=RenderMVGContent(*pattern,clone_info,0,exception);
4651 clone_info=DestroyDrawInfo(clone_info);
4652 if (draw_info->debug != MagickFalse)
4653 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end pattern-path");
4688static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4693 assert(polygon_info != (PolygonInfo **) NULL);
4694 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4695 if (polygon_info[i] != (PolygonInfo *) NULL)
4696 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4697 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4698 return(polygon_info);
4701static PolygonInfo **AcquirePolygonTLS(
const PrimitiveInfo *primitive_info,
4702 ExceptionInfo *exception)
4705 *magick_restrict path_info;
4713 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4714 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4715 sizeof(*polygon_info));
4716 if (polygon_info == (PolygonInfo **) NULL)
4718 (void) ThrowMagickException(exception,GetMagickModule(),
4719 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4720 return((PolygonInfo **) NULL);
4722 (void) memset(polygon_info,0,number_threads*
sizeof(*polygon_info));
4723 path_info=ConvertPrimitiveToPath(primitive_info,exception);
4724 if (path_info == (PathInfo *) NULL)
4725 return(DestroyPolygonTLS(polygon_info));
4726 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4727 if (polygon_info[0] == (PolygonInfo *) NULL)
4729 (void) ThrowMagickException(exception,GetMagickModule(),
4730 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4731 return(DestroyPolygonTLS(polygon_info));
4733 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4734 return(polygon_info);
4737static MagickBooleanType ClonePolygonEdgesTLS(PolygonInfo **polygon_info,
4738 const size_t number_threads,ExceptionInfo *exception)
4743 for (i=1; i < (ssize_t) number_threads; i++)
4751 polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4752 sizeof(*polygon_info[i]));
4753 if (polygon_info[i] == (PolygonInfo *) NULL)
4755 (void) ThrowMagickException(exception,GetMagickModule(),
4756 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4757 return(MagickFalse);
4759 polygon_info[i]->number_edges=0;
4760 edge_info=polygon_info[0]->edges;
4761 polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4762 polygon_info[0]->number_edges,
sizeof(*edge_info));
4763 if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4765 (void) ThrowMagickException(exception,GetMagickModule(),
4766 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4767 return(MagickFalse);
4769 (void) memcpy(polygon_info[i]->edges,edge_info,
4770 polygon_info[0]->number_edges*
sizeof(*edge_info));
4771 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4772 polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4773 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4774 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4776 edge_info=polygon_info[0]->edges+j;
4777 polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4778 edge_info->number_points,
sizeof(*edge_info));
4779 if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4781 (void) ThrowMagickException(exception,GetMagickModule(),
4782 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
4783 return(MagickFalse);
4785 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4786 edge_info->number_points*
sizeof(*edge_info->points));
4792static size_t DestroyEdge(PolygonInfo *polygon_info,
const ssize_t edge)
4794 assert(edge < (ssize_t) polygon_info->number_edges);
4795 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4796 polygon_info->edges[edge].points);
4797 polygon_info->number_edges--;
4798 if (edge < (ssize_t) polygon_info->number_edges)
4799 (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4800 (polygon_info->number_edges-(
size_t) edge)*
sizeof(*polygon_info->edges));
4801 return(polygon_info->number_edges);
4804static double GetFillAlpha(PolygonInfo *polygon_info,
const double mid,
4805 const MagickBooleanType fill,
const FillRule fill_rule,
const ssize_t x,
4806 const ssize_t y,
double *stroke_alpha)
4833 p=polygon_info->edges;
4834 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4836 if ((
double) y <= (p->bounds.y1-mid-0.5))
4838 if ((
double) y > (p->bounds.y2+mid+0.5))
4841 (void) DestroyEdge(polygon_info,j--);
4844 if (((
double) x <= (p->bounds.x1-mid-0.5)) ||
4845 ((
double) x > (p->bounds.x2+mid+0.5)))
4847 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4848 for ( ; i < (ssize_t) p->number_points; i++)
4850 if ((
double) y <= (p->points[i-1].y-mid-0.5))
4852 if ((
double) y > (p->points[i].y+mid+0.5))
4854 if (p->scanline != (
double) y)
4856 p->scanline=(double) y;
4857 p->highwater=(size_t) i;
4863 delta.x=(q+1)->x-q->x;
4864 delta.y=(q+1)->y-q->y;
4865 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4871 delta.x=(double) x-q->x;
4872 delta.y=(double) y-q->y;
4873 distance=delta.x*delta.x+delta.y*delta.y;
4877 alpha=delta.x*delta.x+delta.y*delta.y;
4883 delta.x=(double) x-(q+1)->x;
4884 delta.y=(double) y-(q+1)->y;
4885 distance=delta.x*delta.x+delta.y*delta.y;
4892 alpha=MagickSafeReciprocal(alpha);
4893 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4894 distance=alpha*beta*beta;
4901 if (p->ghostline == MagickFalse)
4904 if ((*stroke_alpha < 1.0) &&
4905 (distance <= ((alpha+0.25)*(alpha+0.25))))
4908 if (distance <= ((alpha+0.25)*(alpha+0.25)))
4913 if (fabs(distance-1.0) >= MagickEpsilon)
4914 beta=sqrt((
double) distance);
4916 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
4917 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
4921 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
4923 if (distance <= 0.0)
4930 if (fabs(beta) < MagickEpsilon)
4933 if (fabs(distance-1.0) >= MagickEpsilon)
4934 beta=sqrt(distance);
4937 if (subpath_alpha < (alpha*alpha))
4938 subpath_alpha=alpha*alpha;
4944 if (fill == MagickFalse)
4946 if (subpath_alpha >= 1.0)
4952 p=polygon_info->edges;
4953 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4955 if ((
double) y <= p->bounds.y1)
4957 if (((
double) y > p->bounds.y2) || ((
double) x <= p->bounds.x1))
4959 if ((
double) x > p->bounds.x2)
4961 winding_number+=p->direction != 0 ? 1 : -1;
4964 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4965 for ( ; i < (ssize_t) (p->number_points-1); i++)
4966 if ((
double) y <= p->points[i].y)
4969 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4970 winding_number+=p->direction != 0 ? 1 : -1;
4972 if (fill_rule != NonZeroRule)
4974 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4978 if (MagickAbsoluteValue(winding_number) != 0)
4980 return(subpath_alpha);
4983static MagickBooleanType DrawPolygonPrimitive(Image *image,
4984 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
4985 ExceptionInfo *exception)
4987 typedef struct _ExtentInfo
5016 **magick_restrict polygon_info;
5028 assert(image != (Image *) NULL);
5029 assert(image->signature == MagickCoreSignature);
5030 assert(draw_info != (DrawInfo *) NULL);
5031 assert(draw_info->signature == MagickCoreSignature);
5032 assert(primitive_info != (PrimitiveInfo *) NULL);
5033 if (IsEventLogging() != MagickFalse)
5034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5035 if (primitive_info->coordinates <= 1)
5040 polygon_info=AcquirePolygonTLS(primitive_info,exception);
5041 if (polygon_info == (PolygonInfo **) NULL)
5042 return(MagickFalse);
5043 if (draw_info->debug != MagickFalse)
5044 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-polygon");
5045 fill=(primitive_info->method == FillToBorderMethod) ||
5046 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
5047 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5048 bounds=polygon_info[0]->edges[0].bounds;
5049 artifact=GetImageArtifact(image,
"draw:render-bounding-rectangles");
5050 if (IsStringTrue(artifact) != MagickFalse)
5051 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
5052 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
5054 p=polygon_info[0]->edges+i;
5055 if (p->bounds.x1 < bounds.x1)
5056 bounds.x1=p->bounds.x1;
5057 if (p->bounds.y1 < bounds.y1)
5058 bounds.y1=p->bounds.y1;
5059 if (p->bounds.x2 > bounds.x2)
5060 bounds.x2=p->bounds.x2;
5061 if (p->bounds.y2 > bounds.y2)
5062 bounds.y2=p->bounds.y2;
5064 bounds.x1-=(mid+1.0);
5065 bounds.y1-=(mid+1.0);
5066 bounds.x2+=(mid+1.0);
5067 bounds.y2+=(mid+1.0);
5068 if ((bounds.x1 >= (
double) image->columns) ||
5069 (bounds.y1 >= (
double) image->rows) ||
5070 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5072 polygon_info=DestroyPolygonTLS(polygon_info);
5075 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5076 (
double) image->columns-1.0 : bounds.x1;
5077 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5078 (
double) image->rows-1.0 : bounds.y1;
5079 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5080 (
double) image->columns-1.0 : bounds.x2;
5081 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5082 (
double) image->rows-1.0 : bounds.y2;
5083 poly_extent.x1=CastDoubleToSsizeT(ceil(bounds.x1-0.5));
5084 poly_extent.y1=CastDoubleToSsizeT(ceil(bounds.y1-0.5));
5085 poly_extent.x2=CastDoubleToSsizeT(floor(bounds.x2+0.5));
5086 poly_extent.y2=CastDoubleToSsizeT(floor(bounds.y2+0.5));
5087 number_threads=(size_t) GetMagickNumberThreads(image,image,(
size_t)
5088 (poly_extent.y2-poly_extent.y1+1),1);
5089 status=ClonePolygonEdgesTLS(polygon_info,number_threads,exception);
5090 if (status == MagickFalse)
5092 polygon_info=DestroyPolygonTLS(polygon_info);
5095 image_view=AcquireAuthenticCacheView(image,exception);
5096 if ((primitive_info->coordinates == 1) ||
5097 (polygon_info[0]->number_edges == 0))
5102#if defined(MAGICKCORE_OPENMP_SUPPORT)
5103 #pragma omp parallel for schedule(static) shared(status) \
5104 num_threads((int) number_threads)
5106 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5117 if (status == MagickFalse)
5120 q=GetCacheViewAuthenticPixels(image_view,x,y,(
size_t) (poly_extent.x2-
5122 if (q == (Quantum *) NULL)
5127 GetPixelInfo(image,&pixel);
5128 for ( ; x <= poly_extent.x2; x++)
5130 if ((x == CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5))) &&
5131 (y == CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5))))
5133 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&pixel,
5135 SetPixelViaPixelInfo(image,&pixel,q);
5137 q+=(ptrdiff_t) GetPixelChannels(image);
5139 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5142 image_view=DestroyCacheView(image_view);
5143 polygon_info=DestroyPolygonTLS(polygon_info);
5144 if (draw_info->debug != MagickFalse)
5145 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5146 " end draw-polygon");
5152#if defined(MAGICKCORE_OPENMP_SUPPORT)
5153 #pragma omp parallel for schedule(static) shared(status) \
5154 num_threads((int) number_threads)
5156 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5159 id = GetOpenMPThreadId();
5167 if (status == MagickFalse)
5169 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(
size_t)
5170 (poly_extent.x2-poly_extent.x1+1),1,exception);
5171 if (q == (Quantum *) NULL)
5176 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5189 fill_alpha=GetFillAlpha(polygon_info[
id],mid,fill,draw_info->fill_rule,
5191 if (draw_info->stroke_antialias == MagickFalse)
5193 fill_alpha=fill_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5194 stroke_alpha=stroke_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5196 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&fill_color,
5198 CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
5199 (
double) GetPixelAlpha(image,q),q);
5200 GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&stroke_color,
5202 CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
5203 (
double) GetPixelAlpha(image,q),q);
5204 q+=(ptrdiff_t) GetPixelChannels(image);
5206 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5209 image_view=DestroyCacheView(image_view);
5210 polygon_info=DestroyPolygonTLS(polygon_info);
5211 if (draw_info->debug != MagickFalse)
5212 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-polygon");
5245static void LogPrimitiveInfo(
const PrimitiveInfo *primitive_info)
5271 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5272 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5273 switch (primitive_info->primitive)
5275 case AlphaPrimitive:
5277 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5278 "AlphaPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5279 methods[primitive_info->method]);
5282 case ColorPrimitive:
5284 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5285 "ColorPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5286 methods[primitive_info->method]);
5289 case ImagePrimitive:
5291 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5292 "ImagePrimitive %.20g,%.20g",(
double) x,(
double) y);
5295 case PointPrimitive:
5297 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5298 "PointPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5299 methods[primitive_info->method]);
5304 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5305 "TextPrimitive %.20g,%.20g",(
double) x,(
double) y);
5312 p=primitive_info[0].point;
5315 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5317 point=primitive_info[i].point;
5318 if (coordinates <= 0)
5320 coordinates=(ssize_t) primitive_info[i].coordinates;
5321 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5322 " begin open (%.20g)",(
double) coordinates);
5325 point=primitive_info[i].point;
5326 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5327 (fabs(q.y-point.y) >= MagickEpsilon))
5328 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5329 " %.20g: %.18g,%.18g",(
double) coordinates,point.x,point.y);
5331 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
5332 " %.20g: %g %g (duplicate)",(
double) coordinates,point.x,point.y);
5335 if (coordinates > 0)
5337 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5338 (fabs(p.y-point.y) >= MagickEpsilon))
5339 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end last (%.20g)",
5340 (
double) coordinates);
5342 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end open (%.20g)",
5343 (
double) coordinates);
5347MagickExport MagickBooleanType DrawPrimitive(Image *image,
5348 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5349 ExceptionInfo *exception)
5364 if (draw_info->debug != MagickFalse)
5366 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5367 " begin draw-primitive");
5368 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5369 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5370 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5371 draw_info->affine.tx,draw_info->affine.ty);
5374 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5375 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5376 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5377 status&=(MagickStatusType) SetImageColorspace(image,sRGBColorspace,
5379 if (draw_info->compliance == SVGCompliance)
5381 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5382 draw_info->clipping_mask,exception);
5383 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5384 draw_info->composite_mask,exception);
5386 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5387 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5388 image_view=AcquireAuthenticCacheView(image,exception);
5389 switch (primitive_info->primitive)
5391 case AlphaPrimitive:
5393 if ((image->alpha_trait & BlendPixelTrait) == 0)
5394 status&=(MagickStatusType) SetImageAlphaChannel(image,
5395 OpaqueAlphaChannel,exception);
5396 switch (primitive_info->method)
5407 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5408 if (q == (Quantum *) NULL)
5410 GetFillColor(draw_info,x,y,&pixel,exception);
5411 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5412 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5422 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5423 x,y,&target,exception);
5424 GetPixelInfo(image,&pixel);
5425 for (y=0; y < (ssize_t) image->rows; y++)
5430 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5432 if (q == (Quantum *) NULL)
5434 for (x=0; x < (ssize_t) image->columns; x++)
5436 GetPixelInfoPixel(image,q,&pixel);
5437 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5439 q+=(ptrdiff_t) GetPixelChannels(image);
5442 GetFillColor(draw_info,x,y,&pixel,exception);
5443 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5444 q+=(ptrdiff_t) GetPixelChannels(image);
5446 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5448 if (status == MagickFalse)
5453 case FloodfillMethod:
5454 case FillToBorderMethod:
5462 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5463 TileVirtualPixelMethod,x,y,&target,exception);
5464 if (primitive_info->method == FillToBorderMethod)
5466 target.red=(double) draw_info->border_color.red;
5467 target.green=(double) draw_info->border_color.green;
5468 target.blue=(double) draw_info->border_color.blue;
5470 channel_mask=SetImageChannelMask(image,AlphaChannel);
5471 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5472 &target,x,y,primitive_info->method == FloodfillMethod ?
5473 MagickFalse : MagickTrue,exception);
5474 (void) SetImageChannelMask(image,channel_mask);
5482 for (y=0; y < (ssize_t) image->rows; y++)
5487 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5489 if (q == (Quantum *) NULL)
5491 for (x=0; x < (ssize_t) image->columns; x++)
5493 GetFillColor(draw_info,x,y,&pixel,exception);
5494 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5495 q+=(ptrdiff_t) GetPixelChannels(image);
5497 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5499 if (status == MagickFalse)
5507 case ColorPrimitive:
5509 switch (primitive_info->method)
5520 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5521 if (q == (Quantum *) NULL)
5523 GetPixelInfo(image,&pixel);
5524 GetFillColor(draw_info,x,y,&pixel,exception);
5525 SetPixelViaPixelInfo(image,&pixel,q);
5526 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5536 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5537 x,y,&target,exception);
5538 for (y=0; y < (ssize_t) image->rows; y++)
5543 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5545 if (q == (Quantum *) NULL)
5547 for (x=0; x < (ssize_t) image->columns; x++)
5549 GetPixelInfoPixel(image,q,&pixel);
5550 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5552 q+=(ptrdiff_t) GetPixelChannels(image);
5555 GetFillColor(draw_info,x,y,&pixel,exception);
5556 SetPixelViaPixelInfo(image,&pixel,q);
5557 q+=(ptrdiff_t) GetPixelChannels(image);
5559 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5561 if (status == MagickFalse)
5566 case FloodfillMethod:
5567 case FillToBorderMethod:
5572 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5573 TileVirtualPixelMethod,x,y,&target,exception);
5574 if (primitive_info->method == FillToBorderMethod)
5576 target.red=(double) draw_info->border_color.red;
5577 target.green=(double) draw_info->border_color.green;
5578 target.blue=(double) draw_info->border_color.blue;
5580 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5581 &target,x,y,primitive_info->method == FloodfillMethod ?
5582 MagickFalse : MagickTrue,exception);
5590 GetPixelInfo(image,&pixel);
5591 for (y=0; y < (ssize_t) image->rows; y++)
5596 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5598 if (q == (Quantum *) NULL)
5600 for (x=0; x < (ssize_t) image->columns; x++)
5602 GetFillColor(draw_info,x,y,&pixel,exception);
5603 SetPixelViaPixelInfo(image,&pixel,q);
5604 q+=(ptrdiff_t) GetPixelChannels(image);
5606 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5608 if (status == MagickFalse)
5616 case ImagePrimitive:
5622 composite_geometry[MagickPathExtent];
5638 if (primitive_info->text == (
char *) NULL)
5640 clone_info=AcquireImageInfo();
5641 composite_images=(Image *) NULL;
5642 if (LocaleNCompare(primitive_info->text,
"data:",5) == 0)
5643 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5646 if (*primitive_info->text !=
'\0')
5651 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5653 (void) SetImageInfo(clone_info,1,exception);
5654 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5656 if (clone_info->size != (
char *) NULL)
5657 clone_info->size=DestroyString(clone_info->size);
5658 if (clone_info->extract != (
char *) NULL)
5659 clone_info->extract=DestroyString(clone_info->extract);
5660 if ((LocaleCompare(clone_info->magick,
"ftp") != 0) &&
5661 (LocaleCompare(clone_info->magick,
"http") != 0) &&
5662 (LocaleCompare(clone_info->magick,
"https") != 0) &&
5663 (LocaleCompare(clone_info->magick,
"mvg") != 0) &&
5664 (LocaleCompare(clone_info->magick,
"vid") != 0))
5665 composite_images=ReadImage(clone_info,exception);
5667 (
void) ThrowMagickException(exception,GetMagickModule(),
5668 FileOpenError,
"UnableToOpenFile",
"`%s'",clone_info->filename);
5670 clone_info=DestroyImageInfo(clone_info);
5671 if (composite_images == (Image *) NULL)
5676 composite_image=RemoveFirstImageFromList(&composite_images);
5677 composite_images=DestroyImageList(composite_images);
5678 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5679 NULL,(
void *) NULL);
5680 x1=CastDoubleToSsizeT(ceil(primitive_info[1].point.x-0.5));
5681 y1=CastDoubleToSsizeT(ceil(primitive_info[1].point.y-0.5));
5682 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5683 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5688 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5689 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
5690 composite_image->filter=image->filter;
5691 status&=(MagickStatusType) TransformImage(&composite_image,
5692 (
char *) NULL,composite_geometry,exception);
5694 if (composite_image->alpha_trait == UndefinedPixelTrait)
5695 status&=(MagickStatusType) SetImageAlphaChannel(composite_image,
5696 OpaqueAlphaChannel,exception);
5697 if (draw_info->alpha != OpaqueAlpha)
5698 status&=(MagickStatusType) SetImageAlpha(composite_image,
5699 draw_info->alpha,exception);
5700 SetGeometry(image,&geometry);
5701 image->gravity=draw_info->gravity;
5704 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5705 "%.20gx%.20g%+.20g%+.20g",(
double) composite_image->columns,(
double)
5706 composite_image->rows,(
double) geometry.x,(
double) geometry.y);
5707 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
5708 affine=draw_info->affine;
5709 affine.tx=(double) geometry.x;
5710 affine.ty=(double) geometry.y;
5711 composite_image->interpolate=image->interpolate;
5712 if ((draw_info->compose == OverCompositeOp) ||
5713 (draw_info->compose == SrcOverCompositeOp))
5714 status&=(MagickStatusType) DrawAffineImage(image,composite_image,
5717 status&=(MagickStatusType) CompositeImage(image,composite_image,
5718 draw_info->compose,MagickTrue,geometry.x,geometry.y,exception);
5719 composite_image=DestroyImage(composite_image);
5722 case PointPrimitive:
5730 if ((y < 0) || (y >= (ssize_t) image->rows))
5732 if ((x < 0) || (x >= (ssize_t) image->columns))
5734 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5735 if (q == (Quantum *) NULL)
5737 GetFillColor(draw_info,x,y,&fill_color,exception);
5738 CompositePixelOver(image,&fill_color,(
double) fill_color.alpha,q,(
double)
5739 GetPixelAlpha(image,q),q);
5740 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5747 geometry[MagickPathExtent];
5752 if (primitive_info->text == (
char *) NULL)
5754 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5755 (void) CloneString(&clone_info->text,primitive_info->text);
5756 (void) FormatLocaleString(geometry,MagickPathExtent,
"%+f%+f",
5757 primitive_info->point.x,primitive_info->point.y);
5758 (void) CloneString(&clone_info->geometry,geometry);
5759 status&=(MagickStatusType) AnnotateImage(image,clone_info,exception);
5760 clone_info=DestroyDrawInfo(clone_info);
5772 if (IsEventLogging() != MagickFalse)
5773 LogPrimitiveInfo(primitive_info);
5774 scale=ExpandAffine(&draw_info->affine);
5775 if ((draw_info->dash_pattern != (
double *) NULL) &&
5776 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5777 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5778 (draw_info->stroke.alpha != (
double) TransparentAlpha))
5783 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5784 clone_info->stroke_width=0.0;
5785 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5786 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5787 primitive_info,exception);
5788 clone_info=DestroyDrawInfo(clone_info);
5789 if (status != MagickFalse)
5790 status&=(MagickStatusType) DrawDashPolygon(draw_info,primitive_info,
5794 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5796 ((draw_info->stroke.alpha != (
double) TransparentAlpha) ||
5797 (draw_info->stroke_pattern != (Image *) NULL)))
5809 closed_path=primitive_info[0].closed_subpath;
5810 i=(ssize_t) primitive_info[0].coordinates;
5811 point_x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5812 point_y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5813 if ((point_x < MagickEpsilon) && (point_y < MagickEpsilon))
5814 closed_path=MagickTrue;
5815 if ((((draw_info->linecap == RoundCap) ||
5816 (closed_path != MagickFalse)) &&
5817 (draw_info->linejoin == RoundJoin)) ||
5818 (primitive_info[i].primitive != UndefinedPrimitive))
5820 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5821 primitive_info,exception);
5824 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5825 clone_info->stroke_width=0.0;
5826 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5827 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5828 primitive_info,exception);
5829 clone_info=DestroyDrawInfo(clone_info);
5830 if (status != MagickFalse)
5831 status&=(MagickStatusType) DrawStrokePolygon(image,draw_info,
5832 primitive_info,exception);
5835 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5836 primitive_info,exception);
5840 image_view=DestroyCacheView(image_view);
5841 if (draw_info->compliance == SVGCompliance)
5843 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5844 (Image *) NULL,exception);
5845 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5846 (Image *) NULL,exception);
5848 if (draw_info->debug != MagickFalse)
5849 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-primitive");
5850 return(status != 0 ? MagickTrue : MagickFalse);
5883static MagickBooleanType DrawRoundLinecap(Image *image,
5884 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5885 ExceptionInfo *exception)
5893 if (primitive_info->coordinates < 1)
5894 return(MagickFalse);
5895 for (i=0; i < 4; i++)
5896 linecap[i]=(*primitive_info);
5897 linecap[0].coordinates=4;
5898 linecap[1].point.x+=2.0*MagickEpsilon;
5899 linecap[2].point.x+=2.0*MagickEpsilon;
5900 linecap[2].point.y+=2.0*MagickEpsilon;
5901 linecap[3].point.y+=2.0*MagickEpsilon;
5902 linecap[4].primitive=UndefinedPrimitive;
5903 return(DrawPolygonPrimitive(image,draw_info,linecap,exception));
5906static MagickBooleanType DrawStrokePolygon(Image *image,
5907 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info,
5908 ExceptionInfo *exception)
5929 if (draw_info->debug != MagickFalse)
5930 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5931 " begin draw-stroke-polygon");
5932 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5933 clone_info->fill=draw_info->stroke;
5934 if (clone_info->fill_pattern != (Image *) NULL)
5935 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5936 if (clone_info->stroke_pattern != (Image *) NULL)
5937 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5938 MagickTrue,exception);
5939 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5940 clone_info->stroke_width=0.0;
5941 clone_info->fill_rule=NonZeroRule;
5943 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
5945 if (p->coordinates == 1)
5947 stroke_polygon=TraceStrokePolygon(draw_info,p,exception);
5948 if (stroke_polygon == (PrimitiveInfo *) NULL)
5953 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5954 stroke_polygon,exception);
5955 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5958 q=p+p->coordinates-1;
5959 closed_path=p->closed_subpath;
5960 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5962 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,p,
5964 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,q,
5968 clone_info=DestroyDrawInfo(clone_info);
5969 if (draw_info->debug != MagickFalse)
5970 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5971 " end draw-stroke-polygon");
5972 return(status != 0 ? MagickTrue : MagickFalse);
5998MagickExport
void GetAffineMatrix(AffineMatrix *affine_matrix)
6000 if (IsEventLogging() != MagickFalse)
6001 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6002 assert(affine_matrix != (AffineMatrix *) NULL);
6003 (void) memset(affine_matrix,0,
sizeof(*affine_matrix));
6004 affine_matrix->sx=1.0;
6005 affine_matrix->sy=1.0;
6032MagickExport
void GetDrawInfo(
const ImageInfo *image_info,DrawInfo *draw_info)
6046 assert(draw_info != (DrawInfo *) NULL);
6047 if (IsEventLogging() != MagickFalse)
6048 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6049 (void) memset(draw_info,0,
sizeof(*draw_info));
6050 draw_info->image_info=CloneImageInfo(image_info);
6051 GetAffineMatrix(&draw_info->affine);
6052 exception=AcquireExceptionInfo();
6053 (void) QueryColorCompliance(
"#000F",AllCompliance,&draw_info->fill,
6055 (void) QueryColorCompliance(
"#FFF0",AllCompliance,&draw_info->stroke,
6057 draw_info->stroke_antialias=draw_info->image_info->antialias;
6058 draw_info->stroke_width=1.0;
6059 draw_info->fill_rule=EvenOddRule;
6060 draw_info->alpha=OpaqueAlpha;
6061 draw_info->fill_alpha=OpaqueAlpha;
6062 draw_info->stroke_alpha=OpaqueAlpha;
6063 draw_info->linecap=ButtCap;
6064 draw_info->linejoin=MiterJoin;
6065 draw_info->miterlimit=10;
6066 draw_info->decorate=NoDecoration;
6067 draw_info->pointsize=12.0;
6068 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
6069 draw_info->compose=OverCompositeOp;
6070 draw_info->render=MagickTrue;
6071 draw_info->clip_path=MagickFalse;
6072 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
6073 MagickTrue : MagickFalse;
6074 if (draw_info->image_info->font != (
char *) NULL)
6075 draw_info->font=AcquireString(draw_info->image_info->font);
6076 if (draw_info->image_info->density != (
char *) NULL)
6077 draw_info->density=AcquireString(draw_info->image_info->density);
6078 draw_info->text_antialias=draw_info->image_info->antialias;
6079 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
6080 draw_info->pointsize=draw_info->image_info->pointsize;
6081 draw_info->border_color=draw_info->image_info->border_color;
6082 if (draw_info->image_info->server_name != (
char *) NULL)
6083 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
6084 option=GetImageOption(draw_info->image_info,
"direction");
6085 if (option != (
const char *) NULL)
6086 draw_info->direction=(DirectionType) ParseCommandOption(
6087 MagickDirectionOptions,MagickFalse,option);
6089 draw_info->direction=UndefinedDirection;
6090 option=GetImageOption(draw_info->image_info,
"encoding");
6091 if (option != (
const char *) NULL)
6092 (void) CloneString(&draw_info->encoding,option);
6093 option=GetImageOption(draw_info->image_info,
"family");
6094 if (option != (
const char *) NULL)
6095 (void) CloneString(&draw_info->family,option);
6096 option=GetImageOption(draw_info->image_info,
"fill");
6097 if (option != (
const char *) NULL)
6098 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
6100 option=GetImageOption(draw_info->image_info,
"gravity");
6101 if (option != (
const char *) NULL)
6102 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
6103 MagickFalse,option);
6104 option=GetImageOption(draw_info->image_info,
"interline-spacing");
6105 if (option != (
const char *) NULL)
6106 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6107 option=GetImageOption(draw_info->image_info,
"interword-spacing");
6108 if (option != (
const char *) NULL)
6109 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6110 option=GetImageOption(draw_info->image_info,
"kerning");
6111 if (option != (
const char *) NULL)
6112 draw_info->kerning=GetDrawValue(option,&next_token);
6113 option=GetImageOption(draw_info->image_info,
"stroke");
6114 if (option != (
const char *) NULL)
6115 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
6117 option=GetImageOption(draw_info->image_info,
"strokewidth");
6118 if (option != (
const char *) NULL)
6119 draw_info->stroke_width=GetDrawValue(option,&next_token);
6120 option=GetImageOption(draw_info->image_info,
"style");
6121 if (option != (
const char *) NULL)
6122 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6123 MagickFalse,option);
6124 option=GetImageOption(draw_info->image_info,
"undercolor");
6125 if (option != (
const char *) NULL)
6126 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
6128 option=GetImageOption(draw_info->image_info,
"weight");
6129 if (option != (
const char *) NULL)
6134 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6136 weight=(ssize_t) StringToUnsignedLong(option);
6137 draw_info->weight=(size_t) weight;
6139 option=GetImageOption(draw_info->image_info,
"word-break");
6140 if (option != (
const char *) NULL)
6141 draw_info->word_break=(WordBreakType) ParseCommandOption(
6142 MagickWordBreakOptions,MagickFalse,option);
6143 exception=DestroyExceptionInfo(exception);
6144 draw_info->signature=MagickCoreSignature;
6172static inline double Permutate(
const ssize_t n,
const ssize_t k)
6181 for (i=k+1; i <= n; i++)
6183 for (i=1; i <= (n-k); i++)
6204static MagickBooleanType TraceArc(MVGInfo *mvg_info,
const PointInfo start,
6205 const PointInfo end,
const PointInfo degrees)
6211 center.x=0.5*(end.x+start.x);
6212 center.y=0.5*(end.y+start.y);
6213 radius.x=fabs(center.x-start.x);
6214 radius.y=fabs(center.y-start.y);
6215 return(TraceEllipse(mvg_info,center,radius,degrees));
6218static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,
const PointInfo start,
6219 const PointInfo end,
const PointInfo arc,
const double angle,
6220 const MagickBooleanType large_arc,
const MagickBooleanType sweep)
6257 offset=mvg_info->offset;
6258 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6259 primitive_info->coordinates=0;
6260 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6261 (fabs(start.y-end.y) < MagickEpsilon))
6262 return(TracePoint(primitive_info,end));
6263 radii.x=fabs(arc.x);
6264 radii.y=fabs(arc.y);
6265 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6266 return(TraceLine(primitive_info,start,end));
6267 cosine=cos(DegreesToRadians(fmod((
double) angle,360.0)));
6268 sine=sin(DegreesToRadians(fmod((
double) angle,360.0)));
6269 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6270 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6271 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6273 if (delta < MagickEpsilon)
6274 return(TraceLine(primitive_info,start,end));
6277 radii.x*=sqrt((
double) delta);
6278 radii.y*=sqrt((
double) delta);
6280 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6281 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6282 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6283 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6284 alpha=points[1].x-points[0].x;
6285 beta=points[1].y-points[0].y;
6286 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6287 return(TraceLine(primitive_info,start,end));
6288 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6293 factor=sqrt((
double) factor);
6294 if (sweep == large_arc)
6297 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6298 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6299 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6300 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6301 if ((theta < 0.0) && (sweep != MagickFalse))
6302 theta+=2.0*MagickPI;
6304 if ((theta > 0.0) && (sweep == MagickFalse))
6305 theta-=2.0*MagickPI;
6306 arc_segments=(size_t) CastDoubleToSsizeT(ceil(fabs((
double) (theta/(0.5*
6307 MagickPI+MagickEpsilon)))));
6310 for (i=0; i < (ssize_t) arc_segments; i++)
6312 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6313 gamma=(8.0/3.0)*sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))*
6314 sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))/
6315 sin(fmod((
double) beta,DegreesToRadians(360.0)));
6316 points[0].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) i*theta/
6317 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((
double) (alpha+
6318 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6319 points[0].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) i*theta/
6320 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((
double) (alpha+
6321 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6322 points[2].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) (i+1)*
6323 theta/arc_segments),DegreesToRadians(360.0))));
6324 points[2].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) (i+1)*
6325 theta/arc_segments),DegreesToRadians(360.0))));
6326 points[1].x=(double) (points[2].x+gamma*sin(fmod((
double) (alpha+(
double)
6327 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6328 points[1].y=(double) (points[2].y-gamma*cos(fmod((
double) (alpha+(
double)
6329 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6330 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6331 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6332 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6334 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6336 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6338 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6340 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6342 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6344 if (i == (ssize_t) (arc_segments-1))
6346 status&=(MagickStatusType) TraceBezier(mvg_info,4);
6349 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6350 mvg_info->offset+=(ssize_t) p->coordinates;
6351 p+=(ptrdiff_t) p->coordinates;
6354 return(MagickFalse);
6355 mvg_info->offset=offset;
6356 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6357 primitive_info->coordinates=(size_t) (p-primitive_info);
6358 primitive_info->closed_subpath=MagickFalse;
6359 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6361 p->primitive=primitive_info->primitive;
6367static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6368 const size_t number_coordinates)
6397 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6398 quantum=number_coordinates;
6399 for (i=0; i < (ssize_t) number_coordinates; i++)
6401 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6403 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6404 if (alpha > (
double) GetMaxMemoryRequest())
6406 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6407 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6408 return(MagickFalse);
6410 if (alpha > (
double) quantum)
6411 quantum=(size_t) alpha;
6412 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6413 if (alpha > (
double) quantum)
6414 quantum=(size_t) alpha;
6417 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6418 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6419 if (quantum > (
double) GetMaxMemoryRequest())
6421 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6422 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6423 return(MagickFalse);
6425 coefficients=(
double *) AcquireQuantumMemory(number_coordinates,
6426 sizeof(*coefficients));
6427 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6429 if ((coefficients == (
double *) NULL) || (points == (PointInfo *) NULL))
6431 if (points != (PointInfo *) NULL)
6432 points=(PointInfo *) RelinquishMagickMemory(points);
6433 if (coefficients != (
double *) NULL)
6434 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6435 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6436 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6437 return(MagickFalse);
6439 control_points=quantum*number_coordinates;
6440 if (CheckPrimitiveExtent(mvg_info,(
double) control_points+1) == MagickFalse)
6442 points=(PointInfo *) RelinquishMagickMemory(points);
6443 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6444 return(MagickFalse);
6446 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6450 end=primitive_info[number_coordinates-1].point;
6451 for (i=0; i < (ssize_t) number_coordinates; i++)
6452 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6454 for (i=0; i < (ssize_t) control_points; i++)
6459 alpha=pow((
double) (1.0-weight),(
double) number_coordinates-1.0);
6460 for (j=0; j < (ssize_t) number_coordinates; j++)
6462 point.x+=alpha*coefficients[j]*p->point.x;
6463 point.y+=alpha*coefficients[j]*p->point.y;
6464 alpha*=weight/(1.0-weight);
6468 weight+=1.0/control_points;
6474 for (i=0; i < (ssize_t) control_points; i++)
6476 if (TracePoint(p,points[i]) == MagickFalse)
6478 points=(PointInfo *) RelinquishMagickMemory(points);
6479 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6480 return(MagickFalse);
6482 p+=(ptrdiff_t) p->coordinates;
6484 if (TracePoint(p,end) == MagickFalse)
6486 points=(PointInfo *) RelinquishMagickMemory(points);
6487 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6488 return(MagickFalse);
6490 p+=(ptrdiff_t) p->coordinates;
6491 primitive_info->coordinates=(size_t) (p-primitive_info);
6492 primitive_info->closed_subpath=MagickFalse;
6493 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6495 p->primitive=primitive_info->primitive;
6498 points=(PointInfo *) RelinquishMagickMemory(points);
6499 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6503static MagickBooleanType TraceCircle(MVGInfo *mvg_info,
const PointInfo start,
6504 const PointInfo end)
6515 alpha=end.x-start.x;
6517 radius=hypot((
double) alpha,(
double) beta);
6518 offset.x=(double) radius;
6519 offset.y=(double) radius;
6522 return(TraceEllipse(mvg_info,start,offset,degrees));
6525static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,
const PointInfo center,
6526 const PointInfo radii,
const PointInfo arc)
6551 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6552 primitive_info->coordinates=0;
6553 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6555 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6556 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6557 angle.x=DegreesToRadians(arc.x);
6561 angle.y=DegreesToRadians(y);
6562 coordinates=ceil((angle.y-angle.x)/step+1.0);
6563 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6564 return(MagickFalse);
6566 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6567 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6569 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6570 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6571 if (i++ >= (ssize_t) coordinates)
6573 if (TracePoint(p,point) == MagickFalse)
6574 return(MagickFalse);
6575 p+=(ptrdiff_t) p->coordinates;
6577 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6578 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6579 if (TracePoint(p,point) == MagickFalse)
6580 return(MagickFalse);
6581 p+=(ptrdiff_t) p->coordinates;
6582 primitive_info->coordinates=(size_t) (p-primitive_info);
6583 primitive_info->closed_subpath=MagickFalse;
6584 x=fabs(primitive_info[0].point.x-
6585 primitive_info[primitive_info->coordinates-1].point.x);
6586 y=fabs(primitive_info[0].point.y-
6587 primitive_info[primitive_info->coordinates-1].point.y);
6588 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6589 primitive_info->closed_subpath=MagickTrue;
6590 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6592 p->primitive=primitive_info->primitive;
6598static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6599 const PointInfo start,
const PointInfo end)
6601 if (TracePoint(primitive_info,start) == MagickFalse)
6602 return(MagickFalse);
6603 if (TracePoint(primitive_info+1,end) == MagickFalse)
6604 return(MagickFalse);
6605 (primitive_info+1)->primitive=primitive_info->primitive;
6606 primitive_info->coordinates=2;
6607 primitive_info->closed_subpath=MagickFalse;
6611static ssize_t TracePath(MVGInfo *mvg_info,
const char *path,
6612 ExceptionInfo *exception)
6616 token[MagickPathExtent] =
"";
6634 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6657 subpath_offset=mvg_info->offset;
6658 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6661 number_coordinates=0;
6663 primitive_type=primitive_info->primitive;
6665 for (p=path; *p !=
'\0'; )
6667 if (status == MagickFalse)
6669 while (isspace((
int) ((
unsigned char) *p)) != 0)
6673 last_attribute=attribute;
6674 attribute=(int) (*p++);
6684 large_arc = MagickFalse,
6685 sweep = MagickFalse;
6695 (void) GetNextToken(p,&p,MagickPathExtent,token);
6697 (void) GetNextToken(p,&p,MagickPathExtent,token);
6698 arc.x=GetDrawValue(token,&next_token);
6699 if (token == next_token)
6700 ThrowPointExpectedException(token,exception);
6701 (void) GetNextToken(p,&p,MagickPathExtent,token);
6703 (void) GetNextToken(p,&p,MagickPathExtent,token);
6704 arc.y=GetDrawValue(token,&next_token);
6705 if (token == next_token)
6706 ThrowPointExpectedException(token,exception);
6707 (void) GetNextToken(p,&p,MagickPathExtent,token);
6709 (void) GetNextToken(p,&p,MagickPathExtent,token);
6710 angle=GetDrawValue(token,&next_token);
6711 if (token == next_token)
6712 ThrowPointExpectedException(token,exception);
6713 (void) GetNextToken(p,&p,MagickPathExtent,token);
6715 (void) GetNextToken(p,&p,MagickPathExtent,token);
6716 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6717 (void) GetNextToken(p,&p,MagickPathExtent,token);
6719 (void) GetNextToken(p,&p,MagickPathExtent,token);
6720 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6722 (void) GetNextToken(p,&p,MagickPathExtent,token);
6723 (void) GetNextToken(p,&p,MagickPathExtent,token);
6725 (void) GetNextToken(p,&p,MagickPathExtent,token);
6726 x=GetDrawValue(token,&next_token);
6727 if (token == next_token)
6728 ThrowPointExpectedException(token,exception);
6729 (void) GetNextToken(p,&p,MagickPathExtent,token);
6731 (void) GetNextToken(p,&p,MagickPathExtent,token);
6732 y=GetDrawValue(token,&next_token);
6733 if (token == next_token)
6734 ThrowPointExpectedException(token,exception);
6735 end.x=(double) (attribute == (
int)
'A' ? x : point.x+x);
6736 end.y=(double) (attribute == (
int)
'A' ? y : point.y+y);
6737 if (TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep) == MagickFalse)
6739 q=(*mvg_info->primitive_info)+mvg_info->offset;
6740 mvg_info->offset+=(ssize_t) q->coordinates;
6741 q+=(ptrdiff_t) q->coordinates;
6743 while (isspace((
int) ((
unsigned char) *p)) != 0)
6747 }
while (IsPoint(p) != MagickFalse);
6759 for (i=1; i < 4; i++)
6761 (void) GetNextToken(p,&p,MagickPathExtent,token);
6763 (void) GetNextToken(p,&p,MagickPathExtent,token);
6764 x=GetDrawValue(token,&next_token);
6765 if (token == next_token)
6766 ThrowPointExpectedException(token,exception);
6767 (void) GetNextToken(p,&p,MagickPathExtent,token);
6769 (void) GetNextToken(p,&p,MagickPathExtent,token);
6770 y=GetDrawValue(token,&next_token);
6771 if (token == next_token)
6772 ThrowPointExpectedException(token,exception);
6773 end.x=(double) (attribute == (
int)
'C' ? x : point.x+x);
6774 end.y=(double) (attribute == (
int)
'C' ? y : point.y+y);
6777 for (i=0; i < 4; i++)
6778 (q+i)->point=points[i];
6779 if (TraceBezier(mvg_info,4) == MagickFalse)
6781 q=(*mvg_info->primitive_info)+mvg_info->offset;
6782 mvg_info->offset+=(ssize_t) q->coordinates;
6783 q+=(ptrdiff_t) q->coordinates;
6785 while (isspace((
int) ((
unsigned char) *p)) != 0)
6789 }
while (IsPoint(p) != MagickFalse);
6797 (void) GetNextToken(p,&p,MagickPathExtent,token);
6799 (void) GetNextToken(p,&p,MagickPathExtent,token);
6800 x=GetDrawValue(token,&next_token);
6801 if (token == next_token)
6802 ThrowPointExpectedException(token,exception);
6803 point.x=(double) (attribute == (
int)
'H' ? x: point.x+x);
6804 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6806 q=(*mvg_info->primitive_info)+mvg_info->offset;
6807 if (TracePoint(q,point) == MagickFalse)
6809 mvg_info->offset+=(ssize_t) q->coordinates;
6810 q+=(ptrdiff_t) q->coordinates;
6811 while (isspace((
int) ((
unsigned char) *p)) != 0)
6815 }
while (IsPoint(p) != MagickFalse);
6826 (void) GetNextToken(p,&p,MagickPathExtent,token);
6828 (void) GetNextToken(p,&p,MagickPathExtent,token);
6829 x=GetDrawValue(token,&next_token);
6830 if (token == next_token)
6831 ThrowPointExpectedException(token,exception);
6832 (void) GetNextToken(p,&p,MagickPathExtent,token);
6834 (void) GetNextToken(p,&p,MagickPathExtent,token);
6835 y=GetDrawValue(token,&next_token);
6836 if (token == next_token)
6837 ThrowPointExpectedException(token,exception);
6838 point.x=(double) (attribute == (
int)
'L' ? x : point.x+x);
6839 point.y=(double) (attribute == (
int)
'L' ? y : point.y+y);
6840 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6842 q=(*mvg_info->primitive_info)+mvg_info->offset;
6843 if (TracePoint(q,point) == MagickFalse)
6845 mvg_info->offset+=(ssize_t) q->coordinates;
6846 q+=(ptrdiff_t) q->coordinates;
6847 while (isspace((
int) ((
unsigned char) *p)) != 0)
6851 }
while (IsPoint(p) != MagickFalse);
6860 if (mvg_info->offset != subpath_offset)
6862 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6863 primitive_info->coordinates=(size_t) (q-primitive_info);
6864 number_coordinates+=primitive_info->coordinates;
6866 subpath_offset=mvg_info->offset;
6871 (void) GetNextToken(p,&p,MagickPathExtent,token);
6873 (void) GetNextToken(p,&p,MagickPathExtent,token);
6874 x=GetDrawValue(token,&next_token);
6875 if (token == next_token)
6876 ThrowPointExpectedException(token,exception);
6877 (void) GetNextToken(p,&p,MagickPathExtent,token);
6879 (void) GetNextToken(p,&p,MagickPathExtent,token);
6880 y=GetDrawValue(token,&next_token);
6881 if (token == next_token)
6882 ThrowPointExpectedException(token,exception);
6883 point.x=(double) (attribute == (
int)
'M' ? x : point.x+x);
6884 point.y=(double) (attribute == (
int)
'M' ? y : point.y+y);
6888 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6890 q=(*mvg_info->primitive_info)+mvg_info->offset;
6891 if (TracePoint(q,point) == MagickFalse)
6893 mvg_info->offset+=(ssize_t) q->coordinates;
6894 q+=(ptrdiff_t) q->coordinates;
6895 while (isspace((
int) ((
unsigned char) *p)) != 0)
6899 }
while (IsPoint(p) != MagickFalse);
6911 for (i=1; i < 3; i++)
6913 (void) GetNextToken(p,&p,MagickPathExtent,token);
6915 (void) GetNextToken(p,&p,MagickPathExtent,token);
6916 x=GetDrawValue(token,&next_token);
6917 if (token == next_token)
6918 ThrowPointExpectedException(token,exception);
6919 (void) GetNextToken(p,&p,MagickPathExtent,token);
6921 (void) GetNextToken(p,&p,MagickPathExtent,token);
6922 y=GetDrawValue(token,&next_token);
6923 if (token == next_token)
6924 ThrowPointExpectedException(token,exception);
6927 end.x=(double) (attribute == (
int)
'Q' ? x : point.x+x);
6928 end.y=(double) (attribute == (
int)
'Q' ? y : point.y+y);
6931 for (i=0; i < 3; i++)
6932 (q+i)->point=points[i];
6933 if (TraceBezier(mvg_info,3) == MagickFalse)
6935 q=(*mvg_info->primitive_info)+mvg_info->offset;
6936 mvg_info->offset+=(ssize_t) q->coordinates;
6937 q+=(ptrdiff_t) q->coordinates;
6939 while (isspace((
int) ((
unsigned char) *p)) != 0)
6943 }
while (IsPoint(p) != MagickFalse);
6954 points[0]=points[3];
6955 points[1].x=2.0*points[3].x-points[2].x;
6956 points[1].y=2.0*points[3].y-points[2].y;
6957 for (i=2; i < 4; i++)
6959 (void) GetNextToken(p,&p,MagickPathExtent,token);
6961 (void) GetNextToken(p,&p,MagickPathExtent,token);
6962 x=GetDrawValue(token,&next_token);
6963 if (token == next_token)
6964 ThrowPointExpectedException(token,exception);
6965 (void) GetNextToken(p,&p,MagickPathExtent,token);
6967 (void) GetNextToken(p,&p,MagickPathExtent,token);
6968 y=GetDrawValue(token,&next_token);
6969 if (token == next_token)
6970 ThrowPointExpectedException(token,exception);
6973 end.x=(double) (attribute == (
int)
'S' ? x : point.x+x);
6974 end.y=(double) (attribute == (
int)
'S' ? y : point.y+y);
6977 if (strchr(
"CcSs",last_attribute) == (
char *) NULL)
6982 for (i=0; i < 4; i++)
6983 (q+i)->point=points[i];
6984 if (TraceBezier(mvg_info,4) == MagickFalse)
6986 q=(*mvg_info->primitive_info)+mvg_info->offset;
6987 mvg_info->offset+=(ssize_t) q->coordinates;
6988 q+=(ptrdiff_t) q->coordinates;
6990 last_attribute=attribute;
6991 while (isspace((
int) ((
unsigned char) *p)) != 0)
6995 }
while (IsPoint(p) != MagickFalse);
7006 points[0]=points[2];
7007 points[1].x=2.0*points[2].x-points[1].x;
7008 points[1].y=2.0*points[2].y-points[1].y;
7009 for (i=2; i < 3; i++)
7011 (void) GetNextToken(p,&p,MagickPathExtent,token);
7013 (void) GetNextToken(p,&p,MagickPathExtent,token);
7014 x=GetDrawValue(token,&next_token);
7015 if (token == next_token)
7016 ThrowPointExpectedException(token,exception);
7017 (void) GetNextToken(p,&p,MagickPathExtent,token);
7019 (void) GetNextToken(p,&p,MagickPathExtent,token);
7020 y=GetDrawValue(token,&next_token);
7021 if (token == next_token)
7022 ThrowPointExpectedException(token,exception);
7023 end.x=(double) (attribute == (
int)
'T' ? x : point.x+x);
7024 end.y=(double) (attribute == (
int)
'T' ? y : point.y+y);
7027 if (status == MagickFalse)
7029 if (strchr(
"QqTt",last_attribute) == (
char *) NULL)
7034 for (i=0; i < 3; i++)
7035 (q+i)->point=points[i];
7036 if (TraceBezier(mvg_info,3) == MagickFalse)
7038 q=(*mvg_info->primitive_info)+mvg_info->offset;
7039 mvg_info->offset+=(ssize_t) q->coordinates;
7040 q+=(ptrdiff_t) q->coordinates;
7042 last_attribute=attribute;
7043 while (isspace((
int) ((
unsigned char) *p)) != 0)
7047 }
while (IsPoint(p) != MagickFalse);
7058 (void) GetNextToken(p,&p,MagickPathExtent,token);
7060 (void) GetNextToken(p,&p,MagickPathExtent,token);
7061 y=GetDrawValue(token,&next_token);
7062 if (token == next_token)
7063 ThrowPointExpectedException(token,exception);
7064 point.y=(double) (attribute == (
int)
'V' ? y : point.y+y);
7065 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7067 q=(*mvg_info->primitive_info)+mvg_info->offset;
7068 if (TracePoint(q,point) == MagickFalse)
7070 mvg_info->offset+=(ssize_t) q->coordinates;
7071 q+=(ptrdiff_t) q->coordinates;
7072 while (isspace((
int) ((
unsigned char) *p)) != 0)
7076 }
while (IsPoint(p) != MagickFalse);
7086 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7088 q=(*mvg_info->primitive_info)+mvg_info->offset;
7089 if (TracePoint(q,point) == MagickFalse)
7091 mvg_info->offset+=(ssize_t) q->coordinates;
7092 q+=(ptrdiff_t) q->coordinates;
7093 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7094 primitive_info->coordinates=(size_t) (q-primitive_info);
7095 primitive_info->closed_subpath=MagickTrue;
7096 number_coordinates+=primitive_info->coordinates;
7098 subpath_offset=mvg_info->offset;
7104 ThrowPointExpectedException(token,exception);
7109 if (status == MagickFalse)
7111 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7112 primitive_info->coordinates=(size_t) (q-primitive_info);
7113 number_coordinates+=primitive_info->coordinates;
7114 for (i=0; i < (ssize_t) number_coordinates; i++)
7117 q->primitive=primitive_type;
7119 q->method=FillToBorderMethod;
7122 return((ssize_t) number_coordinates);
7125static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7126 const PointInfo start,
const PointInfo end)
7138 if (TracePoint(p,start) == MagickFalse)
7139 return(MagickFalse);
7140 p+=(ptrdiff_t) p->coordinates;
7143 if (TracePoint(p,point) == MagickFalse)
7144 return(MagickFalse);
7145 p+=(ptrdiff_t) p->coordinates;
7146 if (TracePoint(p,end) == MagickFalse)
7147 return(MagickFalse);
7148 p+=(ptrdiff_t) p->coordinates;
7151 if (TracePoint(p,point) == MagickFalse)
7152 return(MagickFalse);
7153 p+=(ptrdiff_t) p->coordinates;
7154 if (TracePoint(p,start) == MagickFalse)
7155 return(MagickFalse);
7156 p+=(ptrdiff_t) p->coordinates;
7157 primitive_info->coordinates=(size_t) (p-primitive_info);
7158 primitive_info->closed_subpath=MagickTrue;
7159 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7161 p->primitive=primitive_info->primitive;
7167static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7168 const PointInfo start,
const PointInfo end,PointInfo arc)
7187 offset=mvg_info->offset;
7188 segment.x=fabs(end.x-start.x);
7189 segment.y=fabs(end.y-start.y);
7190 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7192 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7195 if (arc.x > (0.5*segment.x))
7196 arc.x=0.5*segment.x;
7197 if (arc.y > (0.5*segment.y))
7198 arc.y=0.5*segment.y;
7199 point.x=start.x+segment.x-arc.x;
7200 point.y=start.y+arc.y;
7203 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7204 return(MagickFalse);
7205 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7206 mvg_info->offset+=(ssize_t) p->coordinates;
7207 point.x=start.x+segment.x-arc.x;
7208 point.y=start.y+segment.y-arc.y;
7211 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7212 return(MagickFalse);
7213 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7214 mvg_info->offset+=(ssize_t) p->coordinates;
7215 point.x=start.x+arc.x;
7216 point.y=start.y+segment.y-arc.y;
7219 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7220 return(MagickFalse);
7221 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7222 mvg_info->offset+=(ssize_t) p->coordinates;
7223 point.x=start.x+arc.x;
7224 point.y=start.y+arc.y;
7227 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7228 return(MagickFalse);
7229 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7230 mvg_info->offset+=(ssize_t) p->coordinates;
7231 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7232 return(MagickFalse);
7233 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7234 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7235 return(MagickFalse);
7236 p+=(ptrdiff_t) p->coordinates;
7237 mvg_info->offset=offset;
7238 primitive_info=(*mvg_info->primitive_info)+offset;
7239 primitive_info->coordinates=(size_t) (p-primitive_info);
7240 primitive_info->closed_subpath=MagickTrue;
7241 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7243 p->primitive=primitive_info->primitive;
7249static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7250 const size_t number_vertices,
const double offset)
7267 for (i=1; i < (ssize_t) number_vertices; i++)
7269 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7270 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7271 if ((fabs((
double) dx) >= MagickEpsilon) ||
7272 (fabs((
double) dy) >= MagickEpsilon))
7275 if (i == (ssize_t) number_vertices)
7276 i=(ssize_t) number_vertices-1L;
7277 distance=hypot((
double) dx,(
double) dy);
7278 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7279 dx*(distance+offset)/distance);
7280 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7281 dy*(distance+offset)/distance);
7282 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7284 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7285 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7286 if ((fabs((
double) dx) >= MagickEpsilon) ||
7287 (fabs((
double) dy) >= MagickEpsilon))
7290 distance=hypot((
double) dx,(
double) dy);
7291 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7292 dx*(distance+offset)/distance);
7293 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7294 dy*(distance+offset)/distance);
7298static PrimitiveInfo *TraceStrokePolygon(
const DrawInfo *draw_info,
7299 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7301#define MaxStrokePad (6*BezierQuantum+360)
7302#define CheckPathExtent(pad_p,pad_q) \
7304 if ((pad_p) > MaxBezierCoordinates) \
7305 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7307 if ((p+(ptrdiff_t) (pad_p)) >= (ssize_t) extent_p) \
7309 if (~extent_p < (pad_p)) \
7310 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7313 extent_p+=(pad_p); \
7314 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7315 MaxStrokePad,sizeof(*stroke_p)); \
7318 if ((pad_q) > MaxBezierCoordinates) \
7319 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7321 if ((q+(ptrdiff_t) (pad_q)) >= (ssize_t) extent_q) \
7323 if (~extent_q < (pad_q)) \
7324 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7327 extent_q+=(pad_q); \
7328 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7329 MaxStrokePad,sizeof(*stroke_q)); \
7332 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7334 if (stroke_p != (PointInfo *) NULL) \
7335 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7336 if (stroke_q != (PointInfo *) NULL) \
7337 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7338 polygon_primitive=(PrimitiveInfo *) \
7339 RelinquishMagickMemory(polygon_primitive); \
7340 (void) ThrowMagickException(exception,GetMagickModule(), \
7341 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7342 return((PrimitiveInfo *) NULL); \
7346 typedef struct _StrokeSegment
7392 inverse_slope = {0.0, 0.0},
7399 number_vertices=primitive_info->coordinates;
7400 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7401 number_vertices+2UL,
sizeof(*polygon_primitive));
7402 if (polygon_primitive == (PrimitiveInfo *) NULL)
7404 (void) ThrowMagickException(exception,GetMagickModule(),
7405 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7406 return((PrimitiveInfo *) NULL);
7408 (void) memcpy(polygon_primitive,primitive_info,(
size_t) number_vertices*
7409 sizeof(*polygon_primitive));
7410 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7411 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7412 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7413 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7414 if ((draw_info->linejoin == RoundJoin) ||
7415 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
7417 polygon_primitive[number_vertices]=primitive_info[1];
7420 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7424 closed_path=primitive_info[0].closed_subpath;
7427 for (n=1; n < (ssize_t) number_vertices; n++)
7429 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7430 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7431 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7434 if (n == (ssize_t) number_vertices)
7436 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7441 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7442 sizeof(*stroke_polygon));
7443 stroke_polygon[0]=polygon_primitive[0];
7444 stroke_polygon[0].coordinates=0;
7445 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7447 return(stroke_polygon);
7449 n=(ssize_t) number_vertices-1L;
7451 extent_p=2*number_vertices;
7452 extent_q=2*number_vertices;
7453 stroke_p=(PointInfo *) AcquireQuantumMemory((
size_t) extent_p+MaxStrokePad,
7455 stroke_q=(PointInfo *) AcquireQuantumMemory((
size_t) extent_q+MaxStrokePad,
7457 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7459 if (stroke_p != (PointInfo *) NULL)
7460 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7461 if (stroke_q != (PointInfo *) NULL)
7462 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7463 polygon_primitive=(PrimitiveInfo *)
7464 RelinquishMagickMemory(polygon_primitive);
7465 (void) ThrowMagickException(exception,GetMagickModule(),
7466 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7467 return((PrimitiveInfo *) NULL);
7470 inverse_slope.p=0.0;
7471 if (fabs(dx.p) < MagickEpsilon)
7474 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7476 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7479 if (fabs(dy.p) < MagickEpsilon)
7482 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7484 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7489 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7491 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7492 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7493 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7494 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7495 offset.x=sqrt((
double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7496 offset.y=(double) (offset.x*inverse_slope.p);
7497 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7499 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7500 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7501 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7502 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7503 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7504 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7505 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7506 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7510 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7511 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7512 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7513 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7514 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7515 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7516 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7517 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7524 stroke_q[p++]=box_q[0];
7525 stroke_p[q++]=box_p[0];
7526 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7531 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7532 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7533 dot_product=dx.q*dx.q+dy.q*dy.q;
7534 if (dot_product < 0.25)
7537 inverse_slope.q=0.0;
7538 if (fabs(dx.q) < MagickEpsilon)
7541 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7543 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7546 if (fabs(dy.q) < MagickEpsilon)
7549 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7551 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7556 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7558 offset.x=sqrt((
double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7559 offset.y=(double) (offset.x*inverse_slope.q);
7560 dot_product=dy.q*offset.x-dx.q*offset.y;
7561 if (dot_product > 0.0)
7563 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7564 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7565 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7566 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7567 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7568 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7569 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7570 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7574 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7575 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7576 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7577 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7578 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7579 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7580 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7581 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7583 if (fabs((
double) (slope.p-slope.q)) < MagickEpsilon)
7590 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7591 box_p[3].y)/(slope.p-slope.q));
7592 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7593 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7594 box_q[3].y)/(slope.p-slope.q));
7595 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7597 DisableMSCWarning(4127)
7598 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7600 dot_product=dx.q*dy.p-dx.p*dy.q;
7601 if (dot_product <= 0.0)
7602 switch (draw_info->linejoin)
7606 stroke_q[q++]=box_q[1];
7607 stroke_q[q++]=box_q[2];
7608 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7609 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7610 if (dot_product <= miterlimit)
7611 stroke_p[p++]=box_p[4];
7614 stroke_p[p++]=box_p[1];
7615 stroke_p[p++]=box_p[2];
7621 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7622 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7623 if (dot_product <= miterlimit)
7625 stroke_q[q++]=box_q[4];
7626 stroke_p[p++]=box_p[4];
7630 stroke_q[q++]=box_q[1];
7631 stroke_q[q++]=box_q[2];
7632 stroke_p[p++]=box_p[1];
7633 stroke_p[p++]=box_p[2];
7639 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7640 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7641 if (dot_product <= miterlimit)
7642 stroke_p[p++]=box_p[4];
7645 stroke_p[p++]=box_p[1];
7646 stroke_p[p++]=box_p[2];
7648 center=polygon_primitive[n].point;
7649 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7650 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7651 if (theta.q < theta.p)
7652 theta.q+=2.0*MagickPI;
7653 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.q-
7654 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7655 DisableMSCWarning(4127)
7656 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7658 stroke_q[q].x=box_q[1].x;
7659 stroke_q[q].y=box_q[1].y;
7661 for (j=1; j < (ssize_t) arc_segments; j++)
7663 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7664 stroke_q[q].x=(double) (center.x+mid*cos(fmod((
double)
7665 (theta.p+delta_theta),DegreesToRadians(360.0))));
7666 stroke_q[q].y=(double) (center.y+mid*sin(fmod((
double)
7667 (theta.p+delta_theta),DegreesToRadians(360.0))));
7670 stroke_q[q++]=box_q[2];
7677 switch (draw_info->linejoin)
7681 stroke_p[p++]=box_p[1];
7682 stroke_p[p++]=box_p[2];
7683 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7684 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7685 if (dot_product <= miterlimit)
7686 stroke_q[q++]=box_q[4];
7689 stroke_q[q++]=box_q[1];
7690 stroke_q[q++]=box_q[2];
7696 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7697 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7698 if (dot_product <= miterlimit)
7700 stroke_q[q++]=box_q[4];
7701 stroke_p[p++]=box_p[4];
7705 stroke_q[q++]=box_q[1];
7706 stroke_q[q++]=box_q[2];
7707 stroke_p[p++]=box_p[1];
7708 stroke_p[p++]=box_p[2];
7714 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7715 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7716 if (dot_product <= miterlimit)
7717 stroke_q[q++]=box_q[4];
7720 stroke_q[q++]=box_q[1];
7721 stroke_q[q++]=box_q[2];
7723 center=polygon_primitive[n].point;
7724 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7725 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7726 if (theta.p < theta.q)
7727 theta.p+=2.0*MagickPI;
7728 arc_segments=(size_t) CastDoubleToSsizeT(ceil((
double) ((theta.p-
7729 theta.q)/(2.0*sqrt((
double) (MagickSafeReciprocal(mid)))))));
7730 DisableMSCWarning(4127)
7731 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7733 stroke_p[p++]=box_p[1];
7734 for (j=1; j < (ssize_t) arc_segments; j++)
7736 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7737 stroke_p[p].x=(double) (center.x+mid*cos(fmod((
double)
7738 (theta.p+delta_theta),DegreesToRadians(360.0))));
7739 stroke_p[p].y=(double) (center.y+mid*sin(fmod((
double)
7740 (theta.p+delta_theta),DegreesToRadians(360.0))));
7743 stroke_p[p++]=box_p[2];
7750 inverse_slope.p=inverse_slope.q;
7759 stroke_p[p++]=box_p[1];
7760 stroke_q[q++]=box_q[1];
7764 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7765 (p+q+2L),(
size_t) (closed_path+2L)*
sizeof(*stroke_polygon));
7766 if (stroke_polygon == (PrimitiveInfo *) NULL)
7768 (void) ThrowMagickException(exception,GetMagickModule(),
7769 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7770 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7771 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7772 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7774 return(stroke_polygon);
7776 for (i=0; i < (ssize_t) p; i++)
7778 stroke_polygon[i]=polygon_primitive[0];
7779 stroke_polygon[i].point=stroke_p[i];
7781 if (closed_path != MagickFalse)
7783 stroke_polygon[i]=polygon_primitive[0];
7784 stroke_polygon[i].point=stroke_polygon[0].point;
7787 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7789 stroke_polygon[i]=polygon_primitive[0];
7790 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7792 if (closed_path != MagickFalse)
7794 stroke_polygon[i]=polygon_primitive[0];
7795 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7798 stroke_polygon[i]=polygon_primitive[0];
7799 stroke_polygon[i].point=stroke_polygon[0].point;
7801 stroke_polygon[i].primitive=UndefinedPrimitive;
7802 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7803 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7804 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7805 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7806 return(stroke_polygon);