48#include "magick/studio.h"
49#include "magick/annotate.h"
50#include "magick/artifact.h"
51#include "magick/blob.h"
52#include "magick/cache.h"
53#include "magick/cache-private.h"
54#include "magick/cache-view.h"
55#include "magick/channel.h"
56#include "magick/color.h"
57#include "magick/color-private.h"
58#include "magick/colorspace.h"
59#include "magick/colorspace-private.h"
60#include "magick/composite.h"
61#include "magick/composite-private.h"
62#include "magick/constitute.h"
63#include "magick/draw.h"
64#include "magick/draw-private.h"
65#include "magick/enhance.h"
66#include "magick/exception.h"
67#include "magick/exception-private.h"
68#include "magick/gem.h"
69#include "magick/geometry.h"
70#include "magick/image-private.h"
71#include "magick/list.h"
72#include "magick/log.h"
73#include "magick/magick.h"
74#include "magick/memory-private.h"
75#include "magick/monitor.h"
76#include "magick/monitor-private.h"
77#include "magick/option.h"
78#include "magick/paint.h"
79#include "magick/pixel-accessor.h"
80#include "magick/pixel-private.h"
81#include "magick/property.h"
82#include "magick/resample.h"
83#include "magick/resample-private.h"
84#include "magick/resource_.h"
85#include "magick/splay-tree.h"
86#include "magick/string_.h"
87#include "magick/string-private.h"
88#include "magick/thread-private.h"
89#include "magick/token.h"
90#include "magick/transform.h"
91#include "magick/utility.h"
96#define AntialiasThreshold (1.0/3.0)
97#define BezierQuantum 200
98#define PrimitiveExtentPad 4296.0
99#define MaxBezierCoordinates 67108864
100#define ThrowPointExpectedException(image,token) \
102 (void) ThrowMagickException(&(image)->exception,GetMagickModule(),DrawError, \
103 "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
104 status=MagickFalse; \
194 *DrawClippingMask(Image *,
const DrawInfo *,
const char *,
const char *,
197static MagickBooleanType
198 DrawStrokePolygon(Image *,
const DrawInfo *,
const PrimitiveInfo *),
199 RenderMVGContent(Image *,
const DrawInfo *,
const size_t),
200 TraceArc(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo),
201 TraceArcPath(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo,
202 const double,
const MagickBooleanType,
const MagickBooleanType),
203 TraceBezier(MVGInfo *,
const size_t),
204 TraceCircle(MVGInfo *,
const PointInfo,
const PointInfo),
205 TraceEllipse(MVGInfo *,
const PointInfo,
const PointInfo,
const PointInfo),
206 TraceLine(PrimitiveInfo *,
const PointInfo,
const PointInfo),
207 TraceRectangle(PrimitiveInfo *,
const PointInfo,
const PointInfo),
208 TraceRoundRectangle(MVGInfo *,
const PointInfo,
const PointInfo,PointInfo),
209 TraceSquareLinecap(PrimitiveInfo *,
const size_t,
const double);
212 *TraceStrokePolygon(
const DrawInfo *,
const PrimitiveInfo *,ExceptionInfo *);
215 TracePath(Image *,MVGInfo *,
const char *);
235MagickExport DrawInfo *AcquireDrawInfo(
void)
240 draw_info=(DrawInfo *) AcquireCriticalMemory(
sizeof(*draw_info));
241 GetDrawInfo((ImageInfo *) NULL,draw_info);
272MagickExport DrawInfo *CloneDrawInfo(
const ImageInfo *image_info,
273 const DrawInfo *draw_info)
278 clone_info=(DrawInfo *) AcquireCriticalMemory(
sizeof(*clone_info));
279 GetDrawInfo(image_info,clone_info);
280 if (draw_info == (DrawInfo *) NULL)
282 if (draw_info->id != (
char *) NULL)
283 (void) CloneString(&clone_info->id,draw_info->id);
284 if (draw_info->primitive != (
char *) NULL)
285 (void) CloneString(&clone_info->primitive,draw_info->primitive);
286 if (draw_info->geometry != (
char *) NULL)
287 (void) CloneString(&clone_info->geometry,draw_info->geometry);
288 clone_info->compliance=draw_info->compliance;
289 clone_info->viewbox=draw_info->viewbox;
290 clone_info->affine=draw_info->affine;
291 clone_info->gravity=draw_info->gravity;
292 clone_info->fill=draw_info->fill;
293 clone_info->stroke=draw_info->stroke;
294 clone_info->stroke_width=draw_info->stroke_width;
295 if (draw_info->fill_pattern != (Image *) NULL)
296 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
297 &draw_info->fill_pattern->exception);
299 if (draw_info->tile != (Image *) NULL)
300 clone_info->fill_pattern=CloneImage(draw_info->tile,0,0,MagickTrue,
301 &draw_info->tile->exception);
302 clone_info->tile=NewImageList();
303 if (draw_info->stroke_pattern != (Image *) NULL)
304 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
305 MagickTrue,&draw_info->stroke_pattern->exception);
306 clone_info->stroke_antialias=draw_info->stroke_antialias;
307 clone_info->text_antialias=draw_info->text_antialias;
308 clone_info->fill_rule=draw_info->fill_rule;
309 clone_info->linecap=draw_info->linecap;
310 clone_info->linejoin=draw_info->linejoin;
311 clone_info->miterlimit=draw_info->miterlimit;
312 clone_info->dash_offset=draw_info->dash_offset;
313 clone_info->decorate=draw_info->decorate;
314 clone_info->compose=draw_info->compose;
315 if (draw_info->text != (
char *) NULL)
316 (void) CloneString(&clone_info->text,draw_info->text);
317 if (draw_info->font != (
char *) NULL)
318 (void) CloneString(&clone_info->font,draw_info->font);
319 if (draw_info->metrics != (
char *) NULL)
320 (void) CloneString(&clone_info->metrics,draw_info->metrics);
321 if (draw_info->family != (
char *) NULL)
322 (void) CloneString(&clone_info->family,draw_info->family);
323 clone_info->style=draw_info->style;
324 clone_info->stretch=draw_info->stretch;
325 clone_info->weight=draw_info->weight;
326 if (draw_info->encoding != (
char *) NULL)
327 (void) CloneString(&clone_info->encoding,draw_info->encoding);
328 clone_info->pointsize=draw_info->pointsize;
329 clone_info->kerning=draw_info->kerning;
330 clone_info->interline_spacing=draw_info->interline_spacing;
331 clone_info->interword_spacing=draw_info->interword_spacing;
332 clone_info->direction=draw_info->direction;
333 if (draw_info->density != (
char *) NULL)
334 (void) CloneString(&clone_info->density,draw_info->density);
335 clone_info->align=draw_info->align;
336 clone_info->undercolor=draw_info->undercolor;
337 clone_info->border_color=draw_info->border_color;
338 if (draw_info->server_name != (
char *) NULL)
339 (void) CloneString(&clone_info->server_name,draw_info->server_name);
340 if (draw_info->dash_pattern != (
double *) NULL)
345 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
346 clone_info->dash_pattern=(
double *) AcquireQuantumMemory((
size_t) (2*x+2),
347 sizeof(*clone_info->dash_pattern));
348 if (clone_info->dash_pattern == (
double *) NULL)
349 ThrowFatalException(ResourceLimitFatalError,
350 "UnableToAllocateDashPattern");
351 (void) memset(clone_info->dash_pattern,0,(
size_t) (2*x+2)*
352 sizeof(*clone_info->dash_pattern));
353 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(
size_t)
354 (x+1)*
sizeof(*clone_info->dash_pattern));
356 clone_info->gradient=draw_info->gradient;
357 if (draw_info->gradient.stops != (StopInfo *) NULL)
362 number_stops=clone_info->gradient.number_stops;
363 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((
size_t)
364 number_stops,
sizeof(*clone_info->gradient.stops));
365 if (clone_info->gradient.stops == (StopInfo *) NULL)
366 ThrowFatalException(ResourceLimitFatalError,
367 "UnableToAllocateDashPattern");
368 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
369 (
size_t) number_stops*
sizeof(*clone_info->gradient.stops));
371 clone_info->bounds=draw_info->bounds;
372 clone_info->fill_opacity=draw_info->fill_opacity;
373 clone_info->stroke_opacity=draw_info->stroke_opacity;
374 clone_info->element_reference=draw_info->element_reference;
375 clone_info->clip_path=draw_info->clip_path;
376 clone_info->clip_units=draw_info->clip_units;
377 if (draw_info->clip_mask != (
char *) NULL)
378 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
379 if (draw_info->clipping_mask != (Image *) NULL)
380 clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
381 MagickTrue,&draw_info->clipping_mask->exception);
382 if (draw_info->composite_mask != (Image *) NULL)
383 clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
384 MagickTrue,&draw_info->composite_mask->exception);
385 clone_info->render=draw_info->render;
386 clone_info->debug=draw_info->debug;
422static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
427 if (polygon_info->edges != (EdgeInfo *) NULL)
429 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
430 if (polygon_info->edges[i].points != (PointInfo *) NULL)
431 polygon_info->edges[i].points=(PointInfo *)
432 RelinquishMagickMemory(polygon_info->edges[i].points);
433 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(
434 polygon_info->edges);
436 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
439#if defined(__cplusplus) || defined(c_plusplus)
443static int DrawCompareEdges(
const void *p_edge,
const void *q_edge)
445#define DrawCompareEdge(p,q) \
447 if (((p)-(q)) < 0.0) \
449 if (((p)-(q)) > 0.0) \
460 p=((
const EdgeInfo *) p_edge)->points;
461 q=((
const EdgeInfo *) q_edge)->points;
462 DrawCompareEdge(p[0].y,q[0].y);
463 DrawCompareEdge(p[0].x,q[0].x);
464 DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
466 DrawCompareEdge(p[1].y,q[1].y);
467 DrawCompareEdge(p[1].x,q[1].x);
471#if defined(__cplusplus) || defined(c_plusplus)
475static void LogPolygonInfo(
const PolygonInfo *polygon_info)
484 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin active-edge");
485 p=polygon_info->edges;
486 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
488 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" edge %.20g:",
490 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" direction: %s",
491 p->direction != MagickFalse ?
"down" :
"up");
492 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" ghostline: %s",
493 p->ghostline != MagickFalse ?
"transparent" :
"opaque");
494 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
495 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
496 p->bounds.x2,p->bounds.y2);
497 for (j=0; j < (ssize_t) p->number_points; j++)
498 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %g,%g",
499 p->points[j].x,p->points[j].y);
502 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end active-edge");
505static void ReversePoints(PointInfo *points,
const size_t number_points)
513 for (i=0; i < (ssize_t) (number_points >> 1); i++)
516 points[i]=points[number_points-(i+1)];
517 points[number_points-(i+1)]=point;
521static PolygonInfo *ConvertPathToPolygon(
const PathInfo *path_info,
522 ExceptionInfo *exception)
553 polygon_info=(PolygonInfo *) AcquireMagickMemory(
sizeof(*polygon_info));
554 if (polygon_info == (PolygonInfo *) NULL)
556 (void) ThrowMagickException(exception,GetMagickModule(),
557 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
558 return((PolygonInfo *) NULL);
561 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
562 sizeof(*polygon_info->edges));
563 if (polygon_info->edges == (EdgeInfo *) NULL)
565 (void) ThrowMagickException(exception,GetMagickModule(),
566 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
567 return(DestroyPolygonInfo(polygon_info));
569 (void) memset(polygon_info->edges,0,number_edges*
570 sizeof(*polygon_info->edges));
573 ghostline=MagickFalse;
576 points=(PointInfo *) NULL;
577 (void) memset(&point,0,
sizeof(point));
578 (void) memset(&bounds,0,
sizeof(bounds));
579 polygon_info->edges[edge].number_points=(size_t) n;
580 polygon_info->edges[edge].scanline=0.0;
581 polygon_info->edges[edge].highwater=0;
582 polygon_info->edges[edge].ghostline=ghostline;
583 polygon_info->edges[edge].direction=(ssize_t) direction;
584 polygon_info->edges[edge].points=points;
585 polygon_info->edges[edge].bounds=bounds;
586 polygon_info->number_edges=0;
587 for (i=0; path_info[i].code != EndCode; i++)
589 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
590 (path_info[i].code == GhostlineCode))
595 if ((points != (PointInfo *) NULL) && (n >= 2))
597 if (edge == number_edges)
600 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
601 polygon_info->edges,(
size_t) number_edges,
602 sizeof(*polygon_info->edges));
603 if (polygon_info->edges == (EdgeInfo *) NULL)
605 (void) ThrowMagickException(exception,GetMagickModule(),
606 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
607 points=(PointInfo *) RelinquishMagickMemory(points);
608 return(DestroyPolygonInfo(polygon_info));
611 polygon_info->edges[edge].number_points=(size_t) n;
612 polygon_info->edges[edge].scanline=(-1.0);
613 polygon_info->edges[edge].highwater=0;
614 polygon_info->edges[edge].ghostline=ghostline;
615 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
617 ReversePoints(points,(
size_t) n);
618 polygon_info->edges[edge].points=points;
619 polygon_info->edges[edge].bounds=bounds;
620 polygon_info->edges[edge].bounds.y1=points[0].y;
621 polygon_info->edges[edge].bounds.y2=points[n-1].y;
622 points=(PointInfo *) NULL;
623 ghostline=MagickFalse;
625 polygon_info->number_edges=edge;
627 if (points == (PointInfo *) NULL)
630 points=(PointInfo *) AcquireQuantumMemory((
size_t) number_points,
632 if (points == (PointInfo *) NULL)
634 (void) ThrowMagickException(exception,GetMagickModule(),
635 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
636 return(DestroyPolygonInfo(polygon_info));
639 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
640 point=path_info[i].point;
651 next_direction=((path_info[i].point.y > point.y) ||
652 ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
653 (path_info[i].point.x > point.x))) ? 1 : -1;
654 if ((points != (PointInfo *) NULL) && (direction != 0) &&
655 (direction != next_direction))
661 if (edge == number_edges)
664 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
665 polygon_info->edges,(
size_t) number_edges,
666 sizeof(*polygon_info->edges));
667 if (polygon_info->edges == (EdgeInfo *) NULL)
669 (void) ThrowMagickException(exception,GetMagickModule(),
670 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
671 points=(PointInfo *) RelinquishMagickMemory(points);
672 return(DestroyPolygonInfo(polygon_info));
675 polygon_info->edges[edge].number_points=(size_t) n;
676 polygon_info->edges[edge].scanline=(-1.0);
677 polygon_info->edges[edge].highwater=0;
678 polygon_info->edges[edge].ghostline=ghostline;
679 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
681 ReversePoints(points,(
size_t) n);
682 polygon_info->edges[edge].points=points;
683 polygon_info->edges[edge].bounds=bounds;
684 polygon_info->edges[edge].bounds.y1=points[0].y;
685 polygon_info->edges[edge].bounds.y2=points[n-1].y;
686 polygon_info->number_edges=edge+1;
687 points=(PointInfo *) NULL;
689 points=(PointInfo *) AcquireQuantumMemory((
size_t) number_points,
691 if (points == (PointInfo *) NULL)
693 (void) ThrowMagickException(exception,GetMagickModule(),
694 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
695 return(DestroyPolygonInfo(polygon_info));
698 ghostline=MagickFalse;
704 direction=next_direction;
705 if (points == (PointInfo *) NULL)
707 if (n == (ssize_t) number_points)
710 points=(PointInfo *) ResizeQuantumMemory(points,(
size_t) number_points,
712 if (points == (PointInfo *) NULL)
714 (void) ThrowMagickException(exception,GetMagickModule(),
715 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
716 return(DestroyPolygonInfo(polygon_info));
719 point=path_info[i].point;
721 if (point.x < bounds.x1)
723 if (point.x > bounds.x2)
727 if (points != (PointInfo *) NULL)
730 points=(PointInfo *) RelinquishMagickMemory(points);
733 if (edge == number_edges)
736 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
737 polygon_info->edges,(
size_t) number_edges,
738 sizeof(*polygon_info->edges));
739 if (polygon_info->edges == (EdgeInfo *) NULL)
741 (void) ThrowMagickException(exception,GetMagickModule(),
742 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
743 return(DestroyPolygonInfo(polygon_info));
746 polygon_info->edges[edge].number_points=(size_t) n;
747 polygon_info->edges[edge].scanline=(-1.0);
748 polygon_info->edges[edge].highwater=0;
749 polygon_info->edges[edge].ghostline=ghostline;
750 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
752 ReversePoints(points,(
size_t) n);
753 polygon_info->edges[edge].points=points;
754 polygon_info->edges[edge].bounds=bounds;
755 polygon_info->edges[edge].bounds.y1=points[0].y;
756 polygon_info->edges[edge].bounds.y2=points[n-1].y;
757 points=(PointInfo *) NULL;
758 ghostline=MagickFalse;
760 polygon_info->number_edges=edge;
763 polygon_info->number_edges=edge;
764 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(polygon_info->edges,
765 polygon_info->number_edges,
sizeof(*polygon_info->edges));
766 if (polygon_info->edges == (EdgeInfo *) NULL)
768 (void) ThrowMagickException(exception,GetMagickModule(),
769 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
770 return(DestroyPolygonInfo(polygon_info));
772 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
777 edge_info=polygon_info->edges+i;
778 edge_info->points=(PointInfo *) ResizeQuantumMemory(edge_info->points,
779 edge_info->number_points,
sizeof(*edge_info->points));
780 if (edge_info->points == (PointInfo *) NULL)
782 (void) ThrowMagickException(exception,GetMagickModule(),
783 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
784 return(DestroyPolygonInfo(polygon_info));
787 qsort(polygon_info->edges,(
size_t) polygon_info->number_edges,
788 sizeof(*polygon_info->edges),DrawCompareEdges);
789 if ((GetLogEventMask() & DrawEvent) != 0)
790 LogPolygonInfo(polygon_info);
791 return(polygon_info);
825static void LogPathInfo(
const PathInfo *path_info)
830 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin vector-path");
831 for (p=path_info; p->code != EndCode; p++)
832 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
833 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
834 "moveto ghostline" : p->code == OpenCode ?
"moveto open" :
835 p->code == MoveToCode ?
"moveto" : p->code == LineToCode ?
"lineto" :
837 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end vector-path");
840static PathInfo *ConvertPrimitiveToPath(
841 const DrawInfo *magick_unused(draw_info),
const PrimitiveInfo *primitive_info,
842 ExceptionInfo *exception)
865 magick_unreferenced(draw_info);
870 switch (primitive_info->primitive)
877 return((PathInfo *) NULL);
881 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
882 path_info=(PathInfo *) AcquireQuantumMemory((
size_t) (3UL*i+1UL),
884 if (path_info == (PathInfo *) NULL)
886 (void) ThrowMagickException(exception,GetMagickModule(),
887 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
888 return((PathInfo *) NULL);
891 closed_subpath=MagickFalse;
898 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
901 if (coordinates <= 0)
906 coordinates=(ssize_t) primitive_info[i].coordinates;
907 p=primitive_info[i].point;
910 closed_subpath=primitive_info[i].closed_subpath;
913 if ((code == MoveToCode) || (coordinates <= 0) ||
914 (fabs(q.x-primitive_info[i].point.x) >= MagickEpsilon) ||
915 (fabs(q.y-primitive_info[i].point.y) >= MagickEpsilon))
920 path_info[n].code=code;
921 path_info[n].point=primitive_info[i].point;
922 q=primitive_info[i].point;
927 if (closed_subpath != MagickFalse)
929 closed_subpath=MagickFalse;
935 path_info[start].code=OpenCode;
936 path_info[n].code=GhostlineCode;
937 path_info[n].point=primitive_info[i].point;
939 path_info[n].code=LineToCode;
940 path_info[n].point=p;
943 path_info[n].code=EndCode;
944 path_info[n].point.x=0.0;
945 path_info[n].point.y=0.0;
946 if (IsEventLogging() != MagickFalse)
947 LogPathInfo(path_info);
948 path_info=(PathInfo *) ResizeQuantumMemory(path_info,(
size_t) (n+1),
975MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
977 assert(draw_info != (DrawInfo *) NULL);
978 assert(draw_info->signature == MagickCoreSignature);
979 if (IsEventLogging() != MagickFalse)
980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
981 if (draw_info->id != (
char *) NULL)
982 draw_info->id=DestroyString(draw_info->id);
983 if (draw_info->primitive != (
char *) NULL)
984 draw_info->primitive=DestroyString(draw_info->primitive);
985 if (draw_info->text != (
char *) NULL)
986 draw_info->text=DestroyString(draw_info->text);
987 if (draw_info->geometry != (
char *) NULL)
988 draw_info->geometry=DestroyString(draw_info->geometry);
989 if (draw_info->tile != (Image *) NULL)
990 draw_info->tile=DestroyImage(draw_info->tile);
991 if (draw_info->fill_pattern != (Image *) NULL)
992 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
993 if (draw_info->stroke_pattern != (Image *) NULL)
994 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
995 if (draw_info->font != (
char *) NULL)
996 draw_info->font=DestroyString(draw_info->font);
997 if (draw_info->metrics != (
char *) NULL)
998 draw_info->metrics=DestroyString(draw_info->metrics);
999 if (draw_info->family != (
char *) NULL)
1000 draw_info->family=DestroyString(draw_info->family);
1001 if (draw_info->encoding != (
char *) NULL)
1002 draw_info->encoding=DestroyString(draw_info->encoding);
1003 if (draw_info->density != (
char *) NULL)
1004 draw_info->density=DestroyString(draw_info->density);
1005 if (draw_info->server_name != (
char *) NULL)
1006 draw_info->server_name=(
char *)
1007 RelinquishMagickMemory(draw_info->server_name);
1008 if (draw_info->dash_pattern != (
double *) NULL)
1009 draw_info->dash_pattern=(
double *) RelinquishMagickMemory(
1010 draw_info->dash_pattern);
1011 if (draw_info->gradient.stops != (StopInfo *) NULL)
1012 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
1013 draw_info->gradient.stops);
1014 if (draw_info->clip_mask != (
char *) NULL)
1015 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
1016 if (draw_info->clipping_mask != (Image *) NULL)
1017 draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
1018 if (draw_info->composite_mask != (Image *) NULL)
1019 draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
1020 if (draw_info->image_info != (ImageInfo *) NULL)
1021 draw_info->image_info=DestroyImageInfo(draw_info->image_info);
1022 draw_info->signature=(~MagickCoreSignature);
1023 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
1056static SegmentInfo AffineEdge(
const Image *image,
const AffineMatrix *affine,
1057 const double y,
const SegmentInfo *edge)
1072 inverse_edge.x1=edge->x1;
1073 inverse_edge.y1=edge->y1;
1074 inverse_edge.x2=edge->x2;
1075 inverse_edge.y2=edge->y2;
1076 z=affine->ry*y+affine->tx;
1077 if (affine->sx >= MagickEpsilon)
1079 intercept=(-z/affine->sx);
1081 if (x > inverse_edge.x1)
1083 intercept=(-z+(double) image->columns)/affine->sx;
1085 if (x < inverse_edge.x2)
1089 if (affine->sx < -MagickEpsilon)
1091 intercept=(-z+(double) image->columns)/affine->sx;
1093 if (x > inverse_edge.x1)
1095 intercept=(-z/affine->sx);
1097 if (x < inverse_edge.x2)
1101 if ((z < 0.0) || ((
size_t) floor(z+0.5) >= image->columns))
1103 inverse_edge.x2=edge->x1;
1104 return(inverse_edge);
1109 z=affine->sy*y+affine->ty;
1110 if (affine->rx >= MagickEpsilon)
1112 intercept=(-z/affine->rx);
1114 if (x > inverse_edge.x1)
1116 intercept=(-z+(double) image->rows)/affine->rx;
1118 if (x < inverse_edge.x2)
1122 if (affine->rx < -MagickEpsilon)
1124 intercept=(-z+(double) image->rows)/affine->rx;
1126 if (x > inverse_edge.x1)
1128 intercept=(-z/affine->rx);
1130 if (x < inverse_edge.x2)
1134 if ((z < 0.0) || ((
size_t) floor(z+0.5) >= image->rows))
1136 inverse_edge.x2=edge->x2;
1137 return(inverse_edge);
1139 return(inverse_edge);
1142static AffineMatrix InverseAffineMatrix(
const AffineMatrix *affine)
1150 determinant=MagickSafeReciprocal(affine->sx*affine->sy-affine->rx*
1152 inverse_affine.sx=determinant*affine->sy;
1153 inverse_affine.rx=determinant*(-affine->rx);
1154 inverse_affine.ry=determinant*(-affine->ry);
1155 inverse_affine.sy=determinant*affine->sx;
1156 inverse_affine.tx=(-affine->tx)*inverse_affine.sx-affine->ty*
1158 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1160 return(inverse_affine);
1163MagickExport MagickBooleanType DrawAffineImage(Image *image,
1164 const Image *source,
const AffineMatrix *affine)
1202 assert(image != (Image *) NULL);
1203 assert(image->signature == MagickCoreSignature);
1204 assert(source != (
const Image *) NULL);
1205 assert(source->signature == MagickCoreSignature);
1206 if (IsEventLogging() != MagickFalse)
1207 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1208 assert(affine != (AffineMatrix *) NULL);
1211 extent[1].x=(double) source->columns;
1213 extent[2].x=(double) source->columns;
1214 extent[2].y=(double) source->rows;
1216 extent[3].y=(double) source->rows;
1217 for (i=0; i < 4; i++)
1220 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1221 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1225 for (i=1; i < 4; i++)
1227 if (min.x > extent[i].x)
1229 if (min.y > extent[i].y)
1231 if (max.x < extent[i].x)
1233 if (max.y < extent[i].y)
1239 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
1240 return(MagickFalse);
1246 inverse_affine=InverseAffineMatrix(affine);
1249 if (edge.y2 > (image->rows-1.0))
1250 edge.y2=image->rows-1.0;
1251 GetMagickPixelPacket(image,&zero);
1252 exception=(&image->exception);
1253 start=CastDoubleToLong(ceil(edge.y1-0.5));
1254 stop=CastDoubleToLong(floor(edge.y2+0.5));
1255 source_view=AcquireVirtualCacheView(source,exception);
1256 image_view=AcquireAuthenticCacheView(image,exception);
1257#if defined(MAGICKCORE_OPENMP_SUPPORT)
1258 #pragma omp parallel for schedule(static) shared(status) \
1259 magick_number_threads(source,image,stop-start,1)
1261 for (y=start; y <= stop; y++)
1264 *magick_restrict indexes;
1283 if (status == MagickFalse)
1285 inverse_edge=AffineEdge(source,&inverse_affine,(
double) y,&edge);
1286 if (inverse_edge.x2 < inverse_edge.x1)
1288 if (inverse_edge.x1 < 0.0)
1289 inverse_edge.x1=0.0;
1290 if (inverse_edge.x2 > image->columns-1.0)
1291 inverse_edge.x2=image->columns-1.0;
1292 q=GetCacheViewAuthenticPixels(image_view,CastDoubleToLong(
1293 ceil(inverse_edge.x1-0.5)),y,(
size_t) CastDoubleToLong(floor(
1294 inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),1,exception);
1295 if (q == (PixelPacket *) NULL)
1297 indexes=GetCacheViewAuthenticIndexQueue(image_view);
1301 for (x=CastDoubleToLong(ceil(inverse_edge.x1-0.5));
1302 x <= CastDoubleToLong(floor(inverse_edge.x2+0.5)); x++)
1304 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1306 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1308 status=InterpolateMagickPixelPacket(source,source_view,
1309 UndefinedInterpolatePixel,point.x,point.y,&pixel,exception);
1310 if (status == MagickFalse)
1312 SetMagickPixelPacket(image,q,indexes+x_offset,&composite);
1313 MagickPixelCompositeOver(&pixel,pixel.opacity,&composite,
1314 composite.opacity,&composite);
1315 SetPixelPacket(image,&composite,q,indexes+x_offset);
1319 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1322 source_view=DestroyCacheView(source_view);
1323 image_view=DestroyCacheView(image_view);
1356static MagickBooleanType DrawBoundingRectangles(Image *image,
1357 const DrawInfo *draw_info,
const PolygonInfo *polygon_info)
1385 (void) memset(primitive_info,0,
sizeof(primitive_info));
1386 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1387 status=QueryColorDatabase(
"#0000",&clone_info->fill,&image->exception);
1388 if (status == MagickFalse)
1390 clone_info=DestroyDrawInfo(clone_info);
1391 return(MagickFalse);
1395 if (clone_info->density != (
char *) NULL)
1403 flags=ParseGeometry(clone_info->density,&geometry_info);
1404 if ((flags & RhoValue) != 0)
1405 resolution.x=geometry_info.rho;
1406 resolution.y=resolution.x;
1407 if ((flags & SigmaValue) != 0)
1408 resolution.y=geometry_info.sigma;
1410 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1411 clone_info->stroke_width/2.0;
1416 if (polygon_info != (PolygonInfo *) NULL)
1418 bounds=polygon_info->edges[0].bounds;
1419 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1421 if (polygon_info->edges[i].bounds.x1 < (
double) bounds.x1)
1422 bounds.x1=polygon_info->edges[i].bounds.x1;
1423 if (polygon_info->edges[i].bounds.y1 < (
double) bounds.y1)
1424 bounds.y1=polygon_info->edges[i].bounds.y1;
1425 if (polygon_info->edges[i].bounds.x2 > (
double) bounds.x2)
1426 bounds.x2=polygon_info->edges[i].bounds.x2;
1427 if (polygon_info->edges[i].bounds.y2 > (
double) bounds.y2)
1428 bounds.y2=polygon_info->edges[i].bounds.y2;
1431 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1432 image->columns ? (
double) image->columns-1 : bounds.x1;
1434 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1435 image->rows ? (
double) image->rows-1 : bounds.y1;
1437 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1438 image->columns ? (
double) image->columns-1 : bounds.x2;
1440 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1441 image->rows ? (
double) image->rows-1 : bounds.y2;
1442 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1444 if (polygon_info->edges[i].direction != 0)
1445 status=QueryColorDatabase(
"#f00",&clone_info->stroke,
1448 status=QueryColorDatabase(
"#0f0",&clone_info->stroke,
1450 if (status == MagickFalse)
1452 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1453 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1454 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1455 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1456 primitive_info[0].primitive=RectanglePrimitive;
1457 status&=TraceRectangle(primitive_info,start,end);
1458 primitive_info[0].method=ReplaceMethod;
1459 coordinates=(ssize_t) primitive_info[0].coordinates;
1460 primitive_info[coordinates].primitive=UndefinedPrimitive;
1461 status=DrawPrimitive(image,clone_info,primitive_info);
1462 if (status == MagickFalse)
1465 if (i < (ssize_t) polygon_info->number_edges)
1467 clone_info=DestroyDrawInfo(clone_info);
1468 return(status == 0 ? MagickFalse : MagickTrue);
1471 status=QueryColorDatabase(
"#00f",&clone_info->stroke,&image->exception);
1472 if (status == MagickFalse)
1474 clone_info=DestroyDrawInfo(clone_info);
1475 return(MagickFalse);
1477 start.x=(double) (bounds.x1-mid);
1478 start.y=(double) (bounds.y1-mid);
1479 end.x=(double) (bounds.x2+mid);
1480 end.y=(double) (bounds.y2+mid);
1481 primitive_info[0].primitive=RectanglePrimitive;
1482 status&=TraceRectangle(primitive_info,start,end);
1483 primitive_info[0].method=ReplaceMethod;
1484 coordinates=(ssize_t) primitive_info[0].coordinates;
1485 primitive_info[coordinates].primitive=UndefinedPrimitive;
1486 status=DrawPrimitive(image,clone_info,primitive_info);
1487 clone_info=DestroyDrawInfo(clone_info);
1488 return(status == 0 ? MagickFalse : MagickTrue);
1518MagickExport MagickBooleanType DrawClipPath(Image *image,
1519 const DrawInfo *draw_info,
const char *
id)
1530 clip_path=GetImageArtifact(image,
id);
1531 if (clip_path == (
const char *) NULL)
1532 return(MagickFalse);
1533 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1535 if (clipping_mask == (Image *) NULL)
1536 return(MagickFalse);
1537 status=SetImageClipMask(image,clipping_mask);
1538 clipping_mask=DestroyImage(clipping_mask);
1574static Image *DrawClippingMask(Image *image,
const DrawInfo *draw_info,
1575 const char *
id,
const char *clip_path,ExceptionInfo *exception)
1589 assert(image != (Image *) NULL);
1590 assert(image->signature == MagickCoreSignature);
1591 assert(draw_info != (
const DrawInfo *) NULL);
1592 if (IsEventLogging() != MagickFalse)
1593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1594 clip_mask=AcquireImage((
const ImageInfo *) NULL);
1595 status=SetImageExtent(clip_mask,image->columns,image->rows);
1596 if (status == MagickFalse)
1597 return(DestroyImage(clip_mask));
1598 status=SetImageClipMask(image,(Image *) NULL);
1599 status=QueryColorCompliance(
"#0000",AllCompliance,
1600 &clip_mask->background_color,exception);
1601 clip_mask->background_color.opacity=(Quantum) TransparentOpacity;
1602 status=SetImageBackgroundColor(clip_mask);
1603 if (draw_info->debug != MagickFalse)
1604 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin clip-path %s",
1606 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1607 (void) CloneString(&clone_info->primitive,clip_path);
1608 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1610 if (clone_info->clip_mask != (
char *) NULL)
1611 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1612 (void) QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1614 clone_info->stroke_width=0.0;
1615 clone_info->opacity=OpaqueOpacity;
1616 clone_info->clip_path=MagickTrue;
1617 status=RenderMVGContent(clip_mask,clone_info,0);
1618 clone_info=DestroyDrawInfo(clone_info);
1619 status&=SeparateImageChannel(clip_mask,TrueAlphaChannel);
1620 if (draw_info->compliance != SVGCompliance)
1621 status&=NegateImage(clip_mask,MagickFalse);
1622 if (status == MagickFalse)
1623 clip_mask=DestroyImage(clip_mask);
1624 if (draw_info->debug != MagickFalse)
1625 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end clip-path");
1660static Image *DrawCompositeMask(Image *image,
const DrawInfo *draw_info,
1661 const char *
id,
const char *mask_path,ExceptionInfo *exception)
1675 assert(image != (Image *) NULL);
1676 assert(image->signature == MagickCoreSignature);
1677 assert(draw_info != (
const DrawInfo *) NULL);
1678 if (IsEventLogging() != MagickFalse)
1679 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1680 composite_mask=AcquireImage((
const ImageInfo *) NULL);
1681 status=SetImageExtent(composite_mask,image->columns,image->rows);
1682 if (status == MagickFalse)
1683 return(DestroyImage(composite_mask));
1684 status=SetImageMask(image,(Image *) NULL);
1685 status=QueryColorCompliance(
"#0000",AllCompliance,
1686 &composite_mask->background_color,exception);
1687 composite_mask->background_color.opacity=(Quantum) TransparentOpacity;
1688 (void) SetImageBackgroundColor(composite_mask);
1689 if (draw_info->debug != MagickFalse)
1690 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"\nbegin mask-path %s",
1692 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1693 (void) CloneString(&clone_info->primitive,mask_path);
1694 status=QueryColorCompliance(
"#ffffff",AllCompliance,&clone_info->fill,
1696 status=QueryColorCompliance(
"#00000000",AllCompliance,&clone_info->stroke,
1698 clone_info->stroke_width=0.0;
1699 clone_info->opacity=OpaqueOpacity;
1700 status=RenderMVGContent(composite_mask,clone_info,0);
1701 clone_info=DestroyDrawInfo(clone_info);
1702 status&=SeparateImageChannel(composite_mask,TrueAlphaChannel);
1703 status&=NegateImage(composite_mask,MagickFalse);
1704 if (status == MagickFalse)
1705 composite_mask=DestroyImage(composite_mask);
1706 if (draw_info->debug != MagickFalse)
1707 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end mask-path");
1708 return(composite_mask);
1740static MagickBooleanType DrawDashPolygon(
const DrawInfo *draw_info,
1741 const PrimitiveInfo *primitive_info,Image *image)
1771 assert(draw_info != (
const DrawInfo *) NULL);
1772 if (draw_info->debug != MagickFalse)
1773 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-dash");
1774 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1775 number_vertices=(size_t) i;
1776 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
1777 (2UL*number_vertices+32UL),
sizeof(*dash_polygon));
1778 if (dash_polygon == (PrimitiveInfo *) NULL)
1780 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1781 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
1782 return(MagickFalse);
1784 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1785 sizeof(*dash_polygon));
1786 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1787 clone_info->miterlimit=0;
1788 dash_polygon[0]=primitive_info[0];
1789 dash_polygon[0].closed_subpath=MagickFalse;
1790 scale=ExpandAffine(&draw_info->affine);
1791 length=scale*draw_info->dash_pattern[0];
1792 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1793 scale*draw_info->dash_offset : 0.0;
1795 for (n=0; offset > 0.0; j=0)
1797 if (draw_info->dash_pattern[n] <= 0.0)
1799 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1800 if (offset > length)
1804 length=scale*draw_info->dash_pattern[n];
1807 if (offset < length)
1819 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1821 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1822 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1823 maximum_length=hypot(dx,dy);
1824 if (maximum_length > (
double) (MaxBezierCoordinates >> 2))
1826 if (fabs(length) < MagickEpsilon)
1828 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1830 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1832 length=scale*draw_info->dash_pattern[n];
1834 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1836 total_length+=length;
1837 if ((n & 0x01) != 0)
1839 dash_polygon[0]=primitive_info[0];
1840 dash_polygon[0].closed_subpath=MagickFalse;
1841 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1842 total_length*MagickSafeReciprocal(maximum_length));
1843 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1844 total_length*MagickSafeReciprocal(maximum_length));
1849 if ((j+1) > (ssize_t) number_vertices)
1851 dash_polygon[j]=primitive_info[i-1];
1852 dash_polygon[j].closed_subpath=MagickFalse;
1853 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1854 total_length*MagickSafeReciprocal(maximum_length));
1855 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1856 total_length*MagickSafeReciprocal(maximum_length));
1857 dash_polygon[j].coordinates=1;
1859 dash_polygon[0].coordinates=(size_t) j;
1860 dash_polygon[j].primitive=UndefinedPrimitive;
1861 status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1862 if (status == MagickFalse)
1865 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1867 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1869 length=scale*draw_info->dash_pattern[n];
1871 length-=(maximum_length-total_length);
1872 if ((n & 0x01) != 0)
1874 dash_polygon[j]=primitive_info[i];
1875 dash_polygon[j].coordinates=1;
1878 if ((status != MagickFalse) && (total_length < maximum_length) &&
1879 ((n & 0x01) == 0) && (j > 1))
1881 dash_polygon[j]=primitive_info[i-1];
1882 dash_polygon[j].closed_subpath=MagickFalse;
1883 dash_polygon[j].point.x+=MagickEpsilon;
1884 dash_polygon[j].point.y+=MagickEpsilon;
1885 dash_polygon[j].coordinates=1;
1887 dash_polygon[0].coordinates=(size_t) j;
1888 dash_polygon[j].primitive=UndefinedPrimitive;
1889 status&=DrawStrokePolygon(image,clone_info,dash_polygon);
1891 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1892 clone_info=DestroyDrawInfo(clone_info);
1893 if (draw_info->debug != MagickFalse)
1894 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-dash");
1895 return(status != 0 ? MagickTrue : MagickFalse);
1924static inline double GetStopColorOffset(
const GradientInfo *gradient,
1925 const ssize_t x,
const ssize_t y)
1927 switch (gradient->type)
1929 case UndefinedGradient:
1930 case LinearGradient:
1945 gradient_vector=(&gradient->gradient_vector);
1946 p.x=gradient_vector->x2-gradient_vector->x1;
1947 p.y=gradient_vector->y2-gradient_vector->y1;
1948 q.x=(double) x-gradient_vector->x1;
1949 q.y=(double) y-gradient_vector->y1;
1950 length=sqrt(q.x*q.x+q.y*q.y);
1951 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1952 gamma=MagickSafeReciprocal(gamma);
1953 scale=p.x*q.x+p.y*q.y;
1954 offset=gamma*scale*length;
1957 case RadialGradient:
1962 if (gradient->spread == RepeatSpread)
1964 v.x=(double) x-gradient->center.x;
1965 v.y=(double) y-gradient->center.y;
1966 return(sqrt(v.x*v.x+v.y*v.y));
1968 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1969 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1970 gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1971 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1972 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1973 gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1974 return(sqrt(v.x*v.x+v.y*v.y));
1980MagickExport MagickBooleanType DrawGradientImage(Image *image,
1981 const DrawInfo *draw_info)
2017 assert(image != (Image *) NULL);
2018 assert(image->signature == MagickCoreSignature);
2019 assert(draw_info != (
const DrawInfo *) NULL);
2020 if (IsEventLogging() != MagickFalse)
2021 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2022 gradient=(&draw_info->gradient);
2023 gradient_vector=(&gradient->gradient_vector);
2024 point.x=gradient_vector->x2-gradient_vector->x1;
2025 point.y=gradient_vector->y2-gradient_vector->y1;
2026 length=sqrt(point.x*point.x+point.y*point.y);
2027 bounding_box=gradient->bounding_box;
2029 exception=(&image->exception);
2030 GetMagickPixelPacket(image,&zero);
2031 image_view=AcquireAuthenticCacheView(image,exception);
2032 height=(size_t) (bounding_box.y+bounding_box.height);
2033#if defined(MAGICKCORE_OPENMP_SUPPORT)
2034 #pragma omp parallel for schedule(static) shared(status) \
2035 magick_number_threads(image,image,height,1)
2037 for (y=bounding_box.y; y < (ssize_t) height; y++)
2048 *magick_restrict indexes;
2059 if (status == MagickFalse)
2061 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(
size_t)
2062 bounding_box.width,1,exception);
2063 if (q == (PixelPacket *) NULL)
2068 indexes=GetCacheViewAuthenticIndexQueue(image_view);
2071 offset=GetStopColorOffset(gradient,0,y);
2072 if (gradient->type != RadialGradient)
2073 offset*=MagickSafeReciprocal(length);
2074 width=(size_t) (bounding_box.x+bounding_box.width);
2075 for (x=bounding_box.x; x < (ssize_t) width; x++)
2077 SetMagickPixelPacket(image,q,indexes+x,&pixel);
2078 switch (gradient->spread)
2080 case UndefinedSpread:
2083 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2084 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2086 offset=GetStopColorOffset(gradient,x,y);
2087 if (gradient->type != RadialGradient)
2088 offset*=MagickSafeReciprocal(length);
2090 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2091 if (offset < gradient->stops[i].offset)
2093 if ((offset < 0.0) || (i == 0))
2094 composite=gradient->stops[0].color;
2096 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2097 composite=gradient->stops[gradient->number_stops-1].color;
2102 alpha=(offset-gradient->stops[i].offset)/
2103 (gradient->stops[j].offset-gradient->stops[i].offset);
2104 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2105 &gradient->stops[j].color,alpha,&composite);
2111 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2112 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2114 offset=GetStopColorOffset(gradient,x,y);
2115 if (gradient->type != RadialGradient)
2116 offset*=MagickSafeReciprocal(length);
2120 if ((ssize_t) fmod(offset,2.0) == 0)
2121 offset=fmod(offset,1.0);
2123 offset=1.0-fmod(offset,1.0);
2124 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2125 if (offset < gradient->stops[i].offset)
2128 composite=gradient->stops[0].color;
2130 if (i == (ssize_t) gradient->number_stops)
2131 composite=gradient->stops[gradient->number_stops-1].color;
2136 alpha=(offset-gradient->stops[i].offset)/
2137 (gradient->stops[j].offset-gradient->stops[i].offset);
2138 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2139 &gradient->stops[j].color,alpha,&composite);
2151 antialias=MagickFalse;
2153 if ((x != CastDoubleToLong(ceil(gradient_vector->x1-0.5))) ||
2154 (y != CastDoubleToLong(ceil(gradient_vector->y1-0.5))))
2156 offset=GetStopColorOffset(gradient,x,y);
2157 if (gradient->type == LinearGradient)
2159 repeat=fmod(offset,length);
2161 repeat=length-fmod(-repeat,length);
2163 repeat=fmod(offset,length);
2164 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2165 MagickTrue : MagickFalse;
2166 offset=MagickSafeReciprocal(length)*repeat;
2170 repeat=fmod(offset,(
double) gradient->radius);
2172 repeat=gradient->radius-fmod(-repeat,
2173 (
double) gradient->radius);
2175 repeat=fmod(offset,(
double) gradient->radius);
2176 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2178 offset=repeat*MagickSafeReciprocal(gradient->radius);
2181 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2182 if (offset < gradient->stops[i].offset)
2185 composite=gradient->stops[0].color;
2187 if (i == (ssize_t) gradient->number_stops)
2188 composite=gradient->stops[gradient->number_stops-1].color;
2193 alpha=(offset-gradient->stops[i].offset)/
2194 (gradient->stops[j].offset-gradient->stops[i].offset);
2195 if (antialias != MagickFalse)
2197 if (gradient->type == LinearGradient)
2198 alpha=length-repeat;
2200 alpha=gradient->radius-repeat;
2202 j=(ssize_t) gradient->number_stops-1L;
2204 MagickPixelCompositeBlend(&gradient->stops[i].color,1.0-alpha,
2205 &gradient->stops[j].color,alpha,&composite);
2210 MagickPixelCompositeOver(&composite,composite.opacity,&pixel,
2211 pixel.opacity,&pixel);
2212 SetPixelPacket(image,&pixel,q,indexes+x);
2215 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2218 image_view=DestroyCacheView(image_view);
2251static inline MagickBooleanType CheckPrimitiveExtent(MVGInfo *mvg_info,
2266 if ((mvg_info == (MVGInfo *) NULL) ||
2267 (mvg_info->primitive_info == (PrimitiveInfo **) NULL) ||
2268 (*mvg_info->primitive_info == (PrimitiveInfo *) NULL) ||
2269 (mvg_info->extent == (
size_t *) NULL))
2270 return(MagickFalse);
2271 proposed_extent=mvg_info->offset+pad+PrimitiveExtentPad+1.0;
2272 if ((proposed_extent <= 0.0) || (proposed_extent > (
double) MAGICK_SIZE_MAX))
2273 return(MagickFalse);
2274 extent=CastDoubleToSizeT(ceil(proposed_extent));
2275 if (extent <= *mvg_info->extent)
2277 if (extent > (GetMaxMemoryRequest()/
sizeof(PrimitiveInfo)))
2278 return(MagickFalse);
2279 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(
2280 *mvg_info->primitive_info,extent,
sizeof(PrimitiveInfo));
2281 if (primitive_info == (PrimitiveInfo *) NULL)
2286 extent=(size_t) PrimitiveExtentPad;
2287 primitive_info=(PrimitiveInfo *) AcquireCriticalMemory(extent*
2288 sizeof(*primitive_info));
2289 (void) memset(primitive_info,0,extent*
sizeof(*primitive_info));
2290 *mvg_info->primitive_info=primitive_info;
2291 *mvg_info->extent=extent;
2293 ThrowMagickException(mvg_info->exception,GetMagickModule(),
2294 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
2295 return(MagickFalse);
2300 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2302 primitive_info[i].primitive=UndefinedPrimitive;
2303 primitive_info[i].text=(
char *) NULL;
2305 *mvg_info->primitive_info=primitive_info;
2306 *mvg_info->extent=extent;
2310static inline double GetDrawValue(
const char *magick_restrict
string,
2311 char **magick_restrict sentinel)
2314 **magick_restrict q;
2320 value=InterpretLocaleValue(
string,q);
2325static int MVGMacroCompare(
const void *target,
const void *source)
2331 p=(
const char *) target;
2332 q=(
const char *) source;
2333 return(strcmp(p,q));
2336static SplayTreeInfo *GetMVGMacros(
const char *primitive)
2354 if (primitive == (
const char *) NULL)
2355 return((SplayTreeInfo *) NULL);
2356 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2357 RelinquishMagickMemory);
2358 macro=AcquireString(primitive);
2359 token=AcquireString(primitive);
2360 extent=strlen(token)+MagickPathExtent;
2361 for (q=primitive; *q !=
'\0'; )
2363 if (GetNextToken(q,&q,extent,token) < 1)
2367 if (LocaleCompare(
"push",token) == 0)
2373 (void) GetNextToken(q,&q,extent,token);
2377 name[MagickPathExtent];
2388 (void) GetNextToken(q,&q,extent,token);
2391 (void) CopyMagickString(name,token,MagickPathExtent);
2393 for (p=q; *p !=
'\0'; )
2395 if (GetNextToken(p,&p,extent,token) < 1)
2399 if (LocaleCompare(token,
"pop") == 0)
2401 end=p-strlen(token)-1;
2404 if (LocaleCompare(token,
"push") == 0)
2406 if ((n == 0) && (end >= start))
2409 length=(size_t) (end-start);
2414 (void) GetNextToken(p,&p,extent,token);
2417 (void) CopyMagickString(macro,start,length);
2418 (void) AddValueToSplayTree(macros,ConstantString(name),
2419 ConstantString(macro));
2427 token=DestroyString(token);
2428 macro=DestroyString(macro);
2432static inline MagickBooleanType IsValidListChar(
int c)
2434 if ((c >=
'0') && (c <=
'9'))
2448 return(MagickFalse);
2453static inline MagickBooleanType IsValidPoint(
const char *point)
2461 value=GetDrawValue(point,&p);
2462 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2466static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2467 const PointInfo point)
2469 primitive_info->point=point;
2470 primitive_info->coordinates=1;
2471 primitive_info->closed_subpath=MagickFalse;
2472 primitive_info->text=(
char *) NULL;
2476static MagickBooleanType RenderMVGContent(Image *image,
2477 const DrawInfo *draw_info,
const size_t depth)
2479#define RenderImageTag "Render/Image"
2486 key[2*MaxTextExtent],
2487 keyword[MaxTextExtent],
2488 geometry[MaxTextExtent],
2489 name[MaxTextExtent],
2491 pattern[MaxTextExtent],
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 ThrowBinaryImageException(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) == MagickFalse)
2569 return(MagickFalse);
2570 if (image->matte == MagickFalse)
2572 status=SetImageAlphaChannel(image,OpaqueAlphaChannel);
2573 if (status == MagickFalse)
2574 return(MagickFalse);
2576 primitive=(
char *) NULL;
2577 if ((*draw_info->primitive ==
'@') && (strlen(draw_info->primitive) > 1) &&
2578 (*(draw_info->primitive+1) !=
'-') && (depth == 0))
2579 primitive=FileToString(draw_info->primitive,~0UL,&image->exception);
2581 primitive=AcquireString(draw_info->primitive);
2582 if (primitive == (
char *) NULL)
2583 return(MagickFalse);
2584 primitive_extent=(double) strlen(primitive);
2585 (void) SetImageArtifact(image,
"mvg:vector-graphics",primitive);
2590 graphic_context=(DrawInfo **) AcquireMagickMemory(
sizeof(*graphic_context));
2591 if (graphic_context == (DrawInfo **) NULL)
2593 primitive=DestroyString(primitive);
2594 ThrowBinaryImageException(ResourceLimitError,
"MemoryAllocationFailed",
2597 number_points=(size_t) PrimitiveExtentPad;
2598 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
2599 (number_points+1),
sizeof(*primitive_info));
2600 if (primitive_info == (PrimitiveInfo *) NULL)
2602 primitive=DestroyString(primitive);
2603 for ( ; n >= 0; n--)
2604 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2605 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2606 ThrowBinaryImageException(ResourceLimitError,
"MemoryAllocationFailed",
2609 (void) memset(primitive_info,0,(
size_t) (number_points+1)*
2610 sizeof(*primitive_info));
2611 (void) memset(&mvg_info,0,
sizeof(mvg_info));
2612 mvg_info.primitive_info=(&primitive_info);
2613 mvg_info.extent=(&number_points);
2614 mvg_info.exception=(&image->exception);
2615 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2616 graphic_context[n]->viewbox=image->page;
2617 if ((image->page.width == 0) || (image->page.height == 0))
2619 graphic_context[n]->viewbox.width=image->columns;
2620 graphic_context[n]->viewbox.height=image->rows;
2622 token=AcquireString(primitive);
2623 extent=strlen(token)+MaxTextExtent;
2627 macros=GetMVGMacros(primitive);
2628 status=QueryColorDatabase(
"#000000",&start_color,&image->exception);
2629 for (q=primitive; *q !=
'\0'; )
2634 if (GetNextToken(q,&q,MaxTextExtent,keyword) < 1)
2636 if (*keyword ==
'\0')
2638 if (*keyword ==
'#')
2643 while ((*q !=
'\n') && (*q !=
'\0'))
2647 p=q-strlen(keyword)-1;
2648 primitive_type=UndefinedPrimitive;
2649 current=graphic_context[n]->affine;
2650 GetAffineMatrix(&affine);
2659 if (LocaleCompare(
"affine",keyword) == 0)
2661 (void) GetNextToken(q,&q,extent,token);
2662 affine.sx=GetDrawValue(token,&next_token);
2663 if (token == next_token)
2664 ThrowPointExpectedException(image,token);
2665 (void) GetNextToken(q,&q,extent,token);
2666 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2667 ThrowPointExpectedException(image,token);
2669 (void) GetNextToken(q,&q,extent,token);
2670 affine.ry=GetDrawValue(token,&next_token);
2671 if (token == next_token)
2672 ThrowPointExpectedException(image,token);
2673 (void) GetNextToken(q,&q,extent,token);
2674 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2675 ThrowPointExpectedException(image,token);
2677 (void) GetNextToken(q,&q,extent,token);
2678 affine.rx=GetDrawValue(token,&next_token);
2679 if (token == next_token)
2680 ThrowPointExpectedException(image,token);
2681 (void) GetNextToken(q,&q,extent,token);
2682 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2683 ThrowPointExpectedException(image,token);
2685 (void) GetNextToken(q,&q,extent,token);
2686 affine.sy=GetDrawValue(token,&next_token);
2687 if (token == next_token)
2688 ThrowPointExpectedException(image,token);
2689 (void) GetNextToken(q,&q,extent,token);
2690 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2691 ThrowPointExpectedException(image,token);
2693 (void) GetNextToken(q,&q,extent,token);
2694 affine.tx=GetDrawValue(token,&next_token);
2695 if (token == next_token)
2696 ThrowPointExpectedException(image,token);
2697 (void) GetNextToken(q,&q,extent,token);
2698 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
2699 ThrowPointExpectedException(image,token);
2701 (void) GetNextToken(q,&q,extent,token);
2702 affine.ty=GetDrawValue(token,&next_token);
2703 if (token == next_token)
2704 ThrowPointExpectedException(image,token);
2707 if (LocaleCompare(
"arc",keyword) == 0)
2709 primitive_type=ArcPrimitive;
2718 if (LocaleCompare(
"bezier",keyword) == 0)
2720 primitive_type=BezierPrimitive;
2723 if (LocaleCompare(
"border-color",keyword) == 0)
2725 (void) GetNextToken(q,&q,extent,token);
2726 status&=QueryColorDatabase(token,&graphic_context[n]->border_color,
2736 if (LocaleCompare(
"class",keyword) == 0)
2741 (void) GetNextToken(q,&q,extent,token);
2742 if ((*token ==
'\0') || (*token ==
';'))
2750 for (i=0; i <= n; i++)
2751 if (LocaleCompare(token,graphic_context[i]->
id) == 0)
2755 if (classDepth++ > MagickMaxRecursionDepth)
2757 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2758 DrawError,
"VectorGraphicsNestedTooDeeply",
"`%s'",token);
2762 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2763 if ((graphic_context[n]->render != MagickFalse) &&
2764 (mvg_class != (
const char *) NULL) && (p > primitive))
2775 (void) CloneString(&graphic_context[n]->
id,token);
2776 offset=(ssize_t) (p-primitive);
2777 elements=AcquireString(primitive);
2778 elements[offset]=
'\0';
2779 (void) ConcatenateString(&elements,mvg_class);
2780 (void) ConcatenateString(&elements,
"\n");
2781 (void) ConcatenateString(&elements,q);
2782 primitive=DestroyString(primitive);
2788 if (LocaleCompare(
"clip-path",keyword) == 0)
2796 (void) GetNextToken(q,&q,extent,token);
2802 (void) CloneString(&graphic_context[n]->clip_mask,token);
2803 clip_path=(
const char *) GetValueFromSplayTree(macros,token);
2804 if (clip_path != (
const char *) NULL)
2806 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2807 graphic_context[n]->clipping_mask=
2808 DestroyImage(graphic_context[n]->clipping_mask);
2809 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2810 graphic_context[n],token,clip_path,&image->exception);
2811 if (graphic_context[n]->compliance != SVGCompliance)
2816 clip_path=(
const char *) GetValueFromSplayTree(macros,
2817 graphic_context[n]->clip_mask);
2818 if (clip_path != (
const char *) NULL)
2819 (void) SetImageArtifact(image,
2820 graphic_context[n]->clip_mask,clip_path);
2821 status&=DrawClipPath(image,graphic_context[n],
2822 graphic_context[n]->clip_mask);
2827 if (LocaleCompare(
"clip-rule",keyword) == 0)
2832 (void) GetNextToken(q,&q,extent,token);
2833 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2835 if (fill_rule == -1)
2840 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2843 if (LocaleCompare(
"clip-units",keyword) == 0)
2848 (void) GetNextToken(q,&q,extent,token);
2849 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2851 if (clip_units == -1)
2856 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2857 if (clip_units == ObjectBoundingBox)
2859 GetAffineMatrix(¤t);
2860 affine.sx=draw_info->bounds.x2;
2861 affine.sy=draw_info->bounds.y2;
2862 affine.tx=draw_info->bounds.x1;
2863 affine.ty=draw_info->bounds.y1;
2868 if (LocaleCompare(
"circle",keyword) == 0)
2870 primitive_type=CirclePrimitive;
2873 if (LocaleCompare(
"color",keyword) == 0)
2875 primitive_type=ColorPrimitive;
2878 if (LocaleCompare(
"compliance",keyword) == 0)
2884 (void) GetNextToken(q,&q,extent,token);
2885 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2886 MagickComplianceOptions,MagickFalse,token);
2889 if (LocaleCompare(
"currentColor",keyword) == 0)
2891 (void) GetNextToken(q,&q,extent,token);
2900 if (LocaleCompare(
"decorate",keyword) == 0)
2905 (void) GetNextToken(q,&q,extent,token);
2906 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2913 graphic_context[n]->decorate=(DecorationType) decorate;
2916 if (LocaleCompare(
"density",keyword) == 0)
2918 (void) GetNextToken(q,&q,extent,token);
2919 (void) CloneString(&graphic_context[n]->density,token);
2922 if (LocaleCompare(
"direction",keyword) == 0)
2927 (void) GetNextToken(q,&q,extent,token);
2928 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2930 if (direction == -1)
2933 graphic_context[n]->direction=(DirectionType) direction;
2942 if (LocaleCompare(
"ellipse",keyword) == 0)
2944 primitive_type=EllipsePrimitive;
2947 if (LocaleCompare(
"encoding",keyword) == 0)
2949 (void) GetNextToken(q,&q,extent,token);
2950 (void) CloneString(&graphic_context[n]->encoding,token);
2959 if (LocaleCompare(
"fill",keyword) == 0)
2964 (void) GetNextToken(q,&q,extent,token);
2965 if (graphic_context[n]->clip_path != MagickFalse)
2967 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
2968 if (mvg_class != (
const char *) NULL)
2970 (void) DrawPatternPath(image,draw_info,mvg_class,
2971 &graphic_context[n]->fill_pattern);
2974 (void) FormatLocaleString(pattern,MaxTextExtent,
"%s",token);
2975 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
2977 (void) DrawPatternPath(image,draw_info,token,
2978 &graphic_context[n]->fill_pattern);
2981 status&=QueryColorDatabase(token,&graphic_context[n]->fill,
2983 if (graphic_context[n]->fill_opacity != (
double) OpaqueOpacity)
2984 graphic_context[n]->fill.opacity=ClampToQuantum(
2985 graphic_context[n]->fill_opacity);
2988 if (LocaleCompare(
"fill-opacity",keyword) == 0)
2993 (void) GetNextToken(q,&q,extent,token);
2994 if (graphic_context[n]->clip_path != MagickFalse)
2996 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
2997 opacity=MagickMin(MagickMax(factor*
2998 GetDrawValue(token,&next_token),0.0),1.0);
2999 if (token == next_token)
3000 ThrowPointExpectedException(image,token);
3001 if (graphic_context[n]->compliance == SVGCompliance)
3002 graphic_context[n]->fill_opacity*=(1.0-opacity);
3004 graphic_context[n]->fill_opacity=((MagickRealType) QuantumRange-
3005 graphic_context[n]->fill_opacity)*(1.0-opacity);
3006 if (graphic_context[n]->fill.opacity != TransparentOpacity)
3007 graphic_context[n]->fill.opacity=(Quantum)
3008 graphic_context[n]->fill_opacity;
3010 graphic_context[n]->fill.opacity=ClampToQuantum((MagickRealType)
3011 QuantumRange*(1.0-opacity));
3014 if (LocaleCompare(
"fill-rule",keyword) == 0)
3019 (void) GetNextToken(q,&q,extent,token);
3020 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
3022 if (fill_rule == -1)
3027 graphic_context[n]->fill_rule=(FillRule) fill_rule;
3030 if (LocaleCompare(
"font",keyword) == 0)
3032 (void) GetNextToken(q,&q,extent,token);
3033 (void) CloneString(&graphic_context[n]->font,token);
3034 if (LocaleCompare(
"none",token) == 0)
3035 graphic_context[n]->font=(
char *) RelinquishMagickMemory(
3036 graphic_context[n]->font);
3039 if (LocaleCompare(
"font-family",keyword) == 0)
3041 (void) GetNextToken(q,&q,extent,token);
3042 (void) CloneString(&graphic_context[n]->family,token);
3045 if (LocaleCompare(
"font-size",keyword) == 0)
3047 (void) GetNextToken(q,&q,extent,token);
3048 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3049 if (token == next_token)
3050 ThrowPointExpectedException(image,token);
3053 if (LocaleCompare(
"font-stretch",keyword) == 0)
3058 (void) GetNextToken(q,&q,extent,token);
3059 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3065 graphic_context[n]->stretch=(StretchType) stretch;
3068 if (LocaleCompare(
"font-style",keyword) == 0)
3073 (void) GetNextToken(q,&q,extent,token);
3074 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3080 graphic_context[n]->style=(StyleType) style;
3083 if (LocaleCompare(
"font-weight",keyword) == 0)
3088 (void) GetNextToken(q,&q,extent,token);
3089 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3091 weight=(ssize_t) StringToUnsignedLong(token);
3092 graphic_context[n]->weight=(size_t) weight;
3101 if (LocaleCompare(
"gradient-units",keyword) == 0)
3103 (void) GetNextToken(q,&q,extent,token);
3106 if (LocaleCompare(
"gravity",keyword) == 0)
3111 (void) GetNextToken(q,&q,extent,token);
3112 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3118 graphic_context[n]->gravity=(GravityType) gravity;
3127 if (LocaleCompare(
"image",keyword) == 0)
3132 primitive_type=ImagePrimitive;
3133 (void) GetNextToken(q,&q,extent,token);
3134 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3140 graphic_context[n]->compose=(CompositeOperator) compose;
3143 if (LocaleCompare(
"interline-spacing",keyword) == 0)
3145 (void) GetNextToken(q,&q,extent,token);
3146 graphic_context[n]->interline_spacing=GetDrawValue(token,
3148 if (token == next_token)
3149 ThrowPointExpectedException(image,token);
3152 if (LocaleCompare(
"interword-spacing",keyword) == 0)
3154 (void) GetNextToken(q,&q,extent,token);
3155 graphic_context[n]->interword_spacing=GetDrawValue(token,
3157 if (token == next_token)
3158 ThrowPointExpectedException(image,token);
3167 if (LocaleCompare(
"kerning",keyword) == 0)
3169 (void) GetNextToken(q,&q,extent,token);
3170 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3171 if (token == next_token)
3172 ThrowPointExpectedException(image,token);
3181 if (LocaleCompare(
"letter-spacing",keyword) == 0)
3183 (void) GetNextToken(q,&q,extent,token);
3184 if (IsValidPoint(token) == MagickFalse)
3186 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3187 clone_info->text=AcquireString(
" ");
3188 status&=GetTypeMetrics(image,clone_info,&metrics);
3189 graphic_context[n]->kerning=metrics.width*
3190 GetDrawValue(token,&next_token);
3191 clone_info=DestroyDrawInfo(clone_info);
3192 if (token == next_token)
3193 ThrowPointExpectedException(image,token);
3196 if (LocaleCompare(
"line",keyword) == 0)
3198 primitive_type=LinePrimitive;
3207 if (LocaleCompare(
"mask",keyword) == 0)
3215 (void) GetNextToken(q,&q,extent,token);
3216 mask_path=(
const char *) GetValueFromSplayTree(macros,token);
3217 if (mask_path != (
const char *) NULL)
3219 if (graphic_context[n]->composite_mask != (Image *) NULL)
3220 graphic_context[n]->composite_mask=
3221 DestroyImage(graphic_context[n]->composite_mask);
3222 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3223 graphic_context[n],token,mask_path,&image->exception);
3224 if (graphic_context[n]->compliance != SVGCompliance)
3225 status=SetImageMask(image,graphic_context[n]->composite_mask);
3229 if (LocaleCompare(
"matte",keyword) == 0)
3231 primitive_type=MattePrimitive;
3240 if (LocaleCompare(
"offset",keyword) == 0)
3242 (void) GetNextToken(q,&q,extent,token);
3245 if (LocaleCompare(
"opacity",keyword) == 0)
3250 (void) GetNextToken(q,&q,extent,token);
3251 if (graphic_context[n]->clip_path != MagickFalse)
3253 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3254 opacity=1.0-MagickMin(MagickMax(factor*
3255 GetDrawValue(token,&next_token),0.0),1.0);
3256 if (token == next_token)
3257 ThrowPointExpectedException(image,token);
3258 if (graphic_context[n]->compliance == SVGCompliance)
3260 graphic_context[n]->fill_opacity*=opacity;
3261 graphic_context[n]->stroke_opacity*=opacity;
3265 graphic_context[n]->fill_opacity=(double) QuantumRange*opacity;
3266 graphic_context[n]->stroke_opacity=(double) QuantumRange*
3269 if (graphic_context[n]->fill.opacity != (
double) TransparentOpacity)
3271 graphic_context[n]->fill.opacity=
3272 graphic_context[n]->fill_opacity;
3273 graphic_context[n]->stroke.opacity=
3274 graphic_context[n]->stroke_opacity;
3278 graphic_context[n]->fill.opacity=(MagickRealType)
3279 ClampToQuantum((
double) QuantumRange*opacity);
3280 graphic_context[n]->stroke.opacity=(MagickRealType)
3281 ClampToQuantum((
double) QuantumRange*opacity);
3291 if (LocaleCompare(
"path",keyword) == 0)
3293 primitive_type=PathPrimitive;
3296 if (LocaleCompare(
"point",keyword) == 0)
3298 primitive_type=PointPrimitive;
3301 if (LocaleCompare(
"polyline",keyword) == 0)
3303 primitive_type=PolylinePrimitive;
3306 if (LocaleCompare(
"polygon",keyword) == 0)
3308 primitive_type=PolygonPrimitive;
3311 if (LocaleCompare(
"pop",keyword) == 0)
3313 if (GetNextToken(q,&q,extent,token) < 1)
3315 if (LocaleCompare(
"class",token) == 0)
3317 if (LocaleCompare(
"clip-path",token) == 0)
3319 if (LocaleCompare(
"defs",token) == 0)
3322 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3326 if (LocaleCompare(
"gradient",token) == 0)
3328 if (LocaleCompare(
"graphic-context",token) == 0)
3332 (void) ThrowMagickException(&image->exception,
3333 GetMagickModule(),DrawError,
3334 "UnbalancedGraphicContextPushPop",
"`%s'",token);
3339 if ((graphic_context[n]->clip_mask != (
char *) NULL) &&
3340 (graphic_context[n]->compliance != SVGCompliance))
3341 if (LocaleCompare(graphic_context[n]->clip_mask,
3342 graphic_context[n-1]->clip_mask) != 0)
3343 status=SetImageClipMask(image,(Image *) NULL);
3344 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3348 if (LocaleCompare(
"mask",token) == 0)
3350 if (LocaleCompare(
"pattern",token) == 0)
3352 if (LocaleCompare(
"symbol",token) == 0)
3355 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3362 if (LocaleCompare(
"push",keyword) == 0)
3364 if (GetNextToken(q,&q,extent,token) < 1)
3366 if (LocaleCompare(
"class",token) == 0)
3371 for (p=q; *q !=
'\0'; )
3373 if (GetNextToken(q,&q,extent,token) < 1)
3375 if (LocaleCompare(token,
"pop") != 0)
3377 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3378 if (LocaleCompare(token,
"class") != 0)
3382 (void) GetNextToken(q,&q,extent,token);
3385 if (LocaleCompare(
"clip-path",token) == 0)
3387 (void) GetNextToken(q,&q,extent,token);
3388 for (p=q; *q !=
'\0'; )
3390 if (GetNextToken(q,&q,extent,token) < 1)
3392 if (LocaleCompare(token,
"pop") != 0)
3394 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3395 if (LocaleCompare(token,
"clip-path") != 0)
3399 if ((q == (
char *) NULL) || (p == (
char *) NULL) || ((q-4) < p))
3404 (void) GetNextToken(q,&q,extent,token);
3407 if (LocaleCompare(
"defs",token) == 0)
3410 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3414 if (LocaleCompare(
"gradient",token) == 0)
3417 key[2*MaxTextExtent],
3418 name[MaxTextExtent],
3419 type[MaxTextExtent];
3424 (void) GetNextToken(q,&q,extent,token);
3425 (void) CopyMagickString(name,token,MaxTextExtent);
3426 (void) GetNextToken(q,&q,extent,token);
3427 (void) CopyMagickString(type,token,MaxTextExtent);
3428 (void) GetNextToken(q,&q,extent,token);
3429 segment.x1=GetDrawValue(token,&next_token);
3430 if (token == next_token)
3431 ThrowPointExpectedException(image,token);
3432 (void) GetNextToken(q,&q,extent,token);
3433 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3434 ThrowPointExpectedException(image,token);
3436 (void) GetNextToken(q,&q,extent,token);
3437 segment.y1=GetDrawValue(token,&next_token);
3438 if (token == next_token)
3439 ThrowPointExpectedException(image,token);
3440 (void) GetNextToken(q,&q,extent,token);
3441 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3442 ThrowPointExpectedException(image,token);
3444 (void) GetNextToken(q,&q,extent,token);
3445 segment.x2=GetDrawValue(token,&next_token);
3446 if (token == next_token)
3447 ThrowPointExpectedException(image,token);
3448 (void) GetNextToken(q,&q,extent,token);
3449 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3450 ThrowPointExpectedException(image,token);
3452 (void) GetNextToken(q,&q,extent,token);
3453 segment.y2=GetDrawValue(token,&next_token);
3454 if (token == next_token)
3455 ThrowPointExpectedException(image,token);
3456 if (LocaleCompare(type,
"radial") == 0)
3458 (void) GetNextToken(q,&q,extent,token);
3459 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3460 ThrowPointExpectedException(image,token);
3462 (void) GetNextToken(q,&q,extent,token);
3464 for (p=q; *q !=
'\0'; )
3466 if (GetNextToken(q,&q,extent,token) < 1)
3468 if (LocaleCompare(token,
"pop") != 0)
3470 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3471 if (LocaleCompare(token,
"gradient") != 0)
3475 if ((q == (
char *) NULL) || (*q ==
'\0') ||
3476 (p == (
char *) NULL) || ((q-4) < p) ||
3477 ((q-p+4+1) > extent))
3482 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3483 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3484 graphic_context[n]->affine.ry*segment.y1+
3485 graphic_context[n]->affine.tx;
3486 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3487 graphic_context[n]->affine.sy*segment.y1+
3488 graphic_context[n]->affine.ty;
3489 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3490 graphic_context[n]->affine.ry*segment.y2+
3491 graphic_context[n]->affine.tx;
3492 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3493 graphic_context[n]->affine.sy*segment.y2+
3494 graphic_context[n]->affine.ty;
3495 (void) FormatLocaleString(key,MaxTextExtent,
"%s",name);
3496 (void) SetImageArtifact(image,key,token);
3497 (void) FormatLocaleString(key,MaxTextExtent,
"%s-type",name);
3498 (void) SetImageArtifact(image,key,type);
3499 (void) FormatLocaleString(key,MaxTextExtent,
"%s-geometry",name);
3500 (void) FormatLocaleString(geometry,MaxTextExtent,
3501 "%gx%g%+.15g%+.15g",
3502 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3503 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3504 bounds.x1,bounds.y1);
3505 (void) SetImageArtifact(image,key,geometry);
3506 (void) GetNextToken(q,&q,extent,token);
3509 if (LocaleCompare(
"graphic-context",token) == 0)
3512 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3513 graphic_context,(
size_t) (n+1),
sizeof(*graphic_context));
3514 if (graphic_context == (DrawInfo **) NULL)
3516 (void) ThrowMagickException(&image->exception,
3517 GetMagickModule(),ResourceLimitError,
3518 "MemoryAllocationFailed",
"`%s'",image->filename);
3522 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3523 graphic_context[n-1]);
3526 (void) GetNextToken(q,&q,extent,token);
3527 (void) CloneString(&graphic_context[n]->
id,token);
3529 if (n > MagickMaxRecursionDepth)
3531 (void) ThrowMagickException(&image->exception,
3532 GetMagickModule(),DrawError,
3533 "VectorGraphicsNestedTooDeeply",
"`%s'",image->filename);
3538 if (LocaleCompare(
"mask",token) == 0)
3540 (void) GetNextToken(q,&q,extent,token);
3543 if (LocaleCompare(
"pattern",token) == 0)
3548 (void) GetNextToken(q,&q,extent,token);
3549 (void) CopyMagickString(name,token,MaxTextExtent);
3550 (void) GetNextToken(q,&q,extent,token);
3551 bounds.x=CastDoubleToLong(ceil(GetDrawValue(token,
3553 if (token == next_token)
3554 ThrowPointExpectedException(image,token);
3555 (void) GetNextToken(q,&q,extent,token);
3556 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3557 ThrowPointExpectedException(image,token);
3559 (void) GetNextToken(q,&q,extent,token);
3560 bounds.y=CastDoubleToLong(ceil(GetDrawValue(token,
3562 if (token == next_token)
3563 ThrowPointExpectedException(image,token);
3564 (void) GetNextToken(q,&q,extent,token);
3565 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3566 ThrowPointExpectedException(image,token);
3568 (void) GetNextToken(q,&q,extent,token);
3569 bounds.width=CastDoubleToUnsigned(GetDrawValue(token,
3571 if (token == next_token)
3572 ThrowPointExpectedException(image,token);
3573 (void) GetNextToken(q,&q,extent,token);
3574 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3575 ThrowPointExpectedException(image,token);
3577 (void) GetNextToken(q,&q,extent,token);
3578 bounds.height=CastDoubleToUnsigned(GetDrawValue(token,
3580 if (token == next_token)
3581 ThrowPointExpectedException(image,token);
3582 for (p=q; *q !=
'\0'; )
3584 if (GetNextToken(q,&q,extent,token) < 1)
3586 if (LocaleCompare(token,
"pop") != 0)
3588 (void) GetNextToken(q,(
const char **) NULL,extent,token);
3589 if (LocaleCompare(token,
"pattern") != 0)
3593 if ((q == (
char *) NULL) || (p == (
char *) NULL) ||
3594 ((q-4) < p) || ((
size_t) (q-p+4+1) > extent))
3599 (void) CopyMagickString(token,p,(
size_t) (q-p-4+1));
3600 (void) FormatLocaleString(key,MaxTextExtent,
"%s",name);
3601 (void) SetImageArtifact(image,key,token);
3602 (void) FormatLocaleString(key,MaxTextExtent,
"%s-geometry",name);
3603 (void) FormatLocaleString(geometry,MaxTextExtent,
3604 "%.20gx%.20g%+.20g%+.20g",(
double) bounds.width,(
double)
3605 bounds.height,(
double) bounds.x,(
double) bounds.y);
3606 (void) SetImageArtifact(image,key,geometry);
3607 (void) GetNextToken(q,&q,extent,token);
3610 if (LocaleCompare(
"symbol",token) == 0)
3613 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3626 if (LocaleCompare(
"rectangle",keyword) == 0)
3628 primitive_type=RectanglePrimitive;
3631 if (LocaleCompare(
"rotate",keyword) == 0)
3633 (void) GetNextToken(q,&q,extent,token);
3634 angle=GetDrawValue(token,&next_token);
3635 if (token == next_token)
3636 ThrowPointExpectedException(image,token);
3637 affine.sx=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3638 affine.rx=sin(DegreesToRadians(fmod((
double) angle,360.0)));
3639 affine.ry=(-sin(DegreesToRadians(fmod((
double) angle,360.0))));
3640 affine.sy=cos(DegreesToRadians(fmod((
double) angle,360.0)));
3643 if (LocaleCompare(
"roundRectangle",keyword) == 0)
3645 primitive_type=RoundRectanglePrimitive;
3654 if (LocaleCompare(
"scale",keyword) == 0)
3656 (void) GetNextToken(q,&q,extent,token);
3657 affine.sx=GetDrawValue(token,&next_token);
3658 if (token == next_token)
3659 ThrowPointExpectedException(image,token);
3660 (void) GetNextToken(q,&q,extent,token);
3661 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3662 ThrowPointExpectedException(image,token);
3664 (void) GetNextToken(q,&q,extent,token);
3665 affine.sy=GetDrawValue(token,&next_token);
3666 if (token == next_token)
3667 ThrowPointExpectedException(image,token);
3670 if (LocaleCompare(
"skewX",keyword) == 0)
3672 (void) GetNextToken(q,&q,extent,token);
3673 angle=GetDrawValue(token,&next_token);
3674 if (token == next_token)
3675 ThrowPointExpectedException(image,token);
3676 affine.ry=sin(DegreesToRadians(angle));
3679 if (LocaleCompare(
"skewY",keyword) == 0)
3681 (void) GetNextToken(q,&q,extent,token);
3682 angle=GetDrawValue(token,&next_token);
3683 if (token == next_token)
3684 ThrowPointExpectedException(image,token);
3685 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3688 if (LocaleCompare(
"stop-color",keyword) == 0)
3696 (void) GetNextToken(q,&q,extent,token);
3697 status&=QueryColorDatabase(token,&stop_color,&image->exception);
3698 type=LinearGradient;
3699 if (draw_info->gradient.type == RadialGradient)
3700 type=RadialGradient;
3701 (void) GradientImage(image,type,PadSpread,&start_color,&stop_color);
3702 start_color=stop_color;
3703 (void) GetNextToken(q,&q,extent,token);
3706 if (LocaleCompare(
"stroke",keyword) == 0)
3711 (void) GetNextToken(q,&q,extent,token);
3712 if (graphic_context[n]->clip_path != MagickFalse)
3714 mvg_class=(
const char *) GetValueFromSplayTree(macros,token);
3715 if (mvg_class != (
const char *) NULL)
3717 (void) DrawPatternPath(image,draw_info,mvg_class,
3718 &graphic_context[n]->stroke_pattern);
3721 (void) FormatLocaleString(pattern,MaxTextExtent,
"%s",token);
3722 if (GetImageArtifact(image,pattern) != (
const char *) NULL)
3724 (void) DrawPatternPath(image,draw_info,token,
3725 &graphic_context[n]->stroke_pattern);
3728 status&=QueryColorDatabase(token,&graphic_context[n]->stroke,
3730 if (graphic_context[n]->stroke_opacity != (MagickRealType) OpaqueOpacity)
3731 graphic_context[n]->stroke.opacity=ClampToQuantum(
3732 graphic_context[n]->stroke_opacity);
3735 if (LocaleCompare(
"stroke-antialias",keyword) == 0)
3737 (void) GetNextToken(q,&q,extent,token);
3738 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3739 MagickTrue : MagickFalse;
3742 if (LocaleCompare(
"stroke-dasharray",keyword) == 0)
3744 if (graphic_context[n]->dash_pattern != (
double *) NULL)
3745 graphic_context[n]->dash_pattern=(
double *)
3746 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3747 if (IsValidPoint(q) != MagickFalse)
3753 (void) GetNextToken(p,&p,extent,token);
3754 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3755 ThrowPointExpectedException(image,token);
3757 (void) GetNextToken(p,&p,extent,token);
3758 for (x=0; IsValidPoint(token) != MagickFalse; x++)
3760 (void) GetNextToken(p,&p,extent,token);
3762 (void) GetNextToken(p,&p,extent,token);
3764 graphic_context[n]->dash_pattern=(
double *)
3765 AcquireQuantumMemory((
size_t) (2*x+2),
3766 sizeof(*graphic_context[n]->dash_pattern));
3767 if (graphic_context[n]->dash_pattern == (
double *) NULL)
3769 (void) ThrowMagickException(&image->exception,
3770 GetMagickModule(),ResourceLimitError,
3771 "MemoryAllocationFailed",
"`%s'",image->filename);
3775 (void) memset(graphic_context[n]->dash_pattern,0,(
size_t)
3776 (2*x+2)*
sizeof(*graphic_context[n]->dash_pattern));
3777 for (j=0; j < x; j++)
3779 (void) GetNextToken(q,&q,extent,token);
3780 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3781 ThrowPointExpectedException(image,token);
3783 (void) GetNextToken(q,&q,extent,token);
3784 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3786 if (token == next_token)
3787 ThrowPointExpectedException(image,token);
3788 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3791 if ((x & 0x01) != 0)
3792 for ( ; j < (2*x); j++)
3793 graphic_context[n]->dash_pattern[j]=
3794 graphic_context[n]->dash_pattern[j-x];
3795 graphic_context[n]->dash_pattern[j]=0.0;
3798 (void) GetNextToken(q,&q,extent,token);
3801 if (LocaleCompare(
"stroke-dashoffset",keyword) == 0)
3803 (void) GetNextToken(q,&q,extent,token);
3804 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3805 if (token == next_token)
3806 ThrowPointExpectedException(image,token);
3809 if (LocaleCompare(
"stroke-linecap",keyword) == 0)
3814 (void) GetNextToken(q,&q,extent,token);
3815 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3821 graphic_context[n]->linecap=(LineCap) linecap;
3824 if (LocaleCompare(
"stroke-linejoin",keyword) == 0)
3829 (void) GetNextToken(q,&q,extent,token);
3830 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3837 graphic_context[n]->linejoin=(LineJoin) linejoin;
3840 if (LocaleCompare(
"stroke-miterlimit",keyword) == 0)
3842 (void) GetNextToken(q,&q,extent,token);
3843 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3846 if (LocaleCompare(
"stroke-opacity",keyword) == 0)
3851 (void) GetNextToken(q,&q,extent,token);
3852 if (graphic_context[n]->clip_path != MagickFalse)
3854 factor=strchr(token,
'%') != (
char *) NULL ? 0.01 : 1.0;
3855 opacity=MagickMin(MagickMax(factor*
3856 GetDrawValue(token,&next_token),0.0),1.0);
3857 if (token == next_token)
3858 ThrowPointExpectedException(image,token);
3859 if (graphic_context[n]->compliance == SVGCompliance)
3860 graphic_context[n]->stroke_opacity*=(1.0-opacity);
3862 graphic_context[n]->stroke_opacity=((MagickRealType) QuantumRange-
3863 graphic_context[n]->stroke_opacity)*(1.0-opacity);
3864 if (graphic_context[n]->stroke.opacity != TransparentOpacity)
3865 graphic_context[n]->stroke.opacity=(Quantum)
3866 graphic_context[n]->stroke_opacity;
3868 graphic_context[n]->stroke.opacity=ClampToQuantum(
3869 (MagickRealType) QuantumRange*opacity);
3872 if (LocaleCompare(
"stroke-width",keyword) == 0)
3874 (void) GetNextToken(q,&q,extent,token);
3875 if (graphic_context[n]->clip_path != MagickFalse)
3877 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3878 if ((token == next_token) ||
3879 (graphic_context[n]->stroke_width < 0.0))
3880 ThrowPointExpectedException(image,token);
3889 if (LocaleCompare(
"text",keyword) == 0)
3891 primitive_type=TextPrimitive;
3895 if (LocaleCompare(
"text-align",keyword) == 0)
3900 (void) GetNextToken(q,&q,extent,token);
3901 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3907 graphic_context[n]->align=(AlignType) align;
3910 if (LocaleCompare(
"text-anchor",keyword) == 0)
3915 (void) GetNextToken(q,&q,extent,token);
3916 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3922 graphic_context[n]->align=(AlignType) align;
3925 if (LocaleCompare(
"text-antialias",keyword) == 0)
3927 (void) GetNextToken(q,&q,extent,token);
3928 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3929 MagickTrue : MagickFalse;
3932 if (LocaleCompare(
"text-undercolor",keyword) == 0)
3934 (void) GetNextToken(q,&q,extent,token);
3935 status&=QueryColorDatabase(token,&graphic_context[n]->undercolor,
3939 if (LocaleCompare(
"translate",keyword) == 0)
3941 (void) GetNextToken(q,&q,extent,token);
3942 affine.tx=GetDrawValue(token,&next_token);
3943 if (token == next_token)
3944 ThrowPointExpectedException(image,token);
3945 (void) GetNextToken(q,&q,extent,token);
3946 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3947 ThrowPointExpectedException(image,token);
3949 (void) GetNextToken(q,&q,extent,token);
3950 affine.ty=GetDrawValue(token,&next_token);
3951 if (token == next_token)
3952 ThrowPointExpectedException(image,token);
3961 if (LocaleCompare(
"use",keyword) == 0)
3969 (void) GetNextToken(q,&q,extent,token);
3970 use=(
const char *) GetValueFromSplayTree(macros,token);
3971 if (use != (
const char *) NULL)
3973 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3974 (void) CloneString(&clone_info->primitive,use);
3975 status=RenderMVGContent(image,clone_info,depth+1);
3976 clone_info=DestroyDrawInfo(clone_info);
3986 if (LocaleCompare(
"viewbox",keyword) == 0)
3988 (void) GetNextToken(q,&q,extent,token);
3989 graphic_context[n]->viewbox.x=CastDoubleToLong(ceil(
3990 GetDrawValue(token,&next_token)-0.5));
3991 if (token == next_token)
3992 ThrowPointExpectedException(image,token);
3993 (void) GetNextToken(q,&q,extent,token);
3994 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
3995 ThrowPointExpectedException(image,token);
3997 (void) GetNextToken(q,&q,extent,token);
3998 graphic_context[n]->viewbox.y=CastDoubleToLong(ceil(
3999 GetDrawValue(token,&next_token)-0.5));
4000 if (token == next_token)
4001 ThrowPointExpectedException(image,token);
4002 (void) GetNextToken(q,&q,extent,token);
4003 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4004 ThrowPointExpectedException(image,token);
4006 (void) GetNextToken(q,&q,extent,token);
4007 graphic_context[n]->viewbox.width=CastDoubleToUnsigned(
4008 GetDrawValue(token,&next_token)+0.5);
4009 if (token == next_token)
4010 ThrowPointExpectedException(image,token);
4011 (void) GetNextToken(q,&q,extent,token);
4012 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4013 ThrowPointExpectedException(image,token);
4015 (void) GetNextToken(q,&q,extent,token);
4016 graphic_context[n]->viewbox.height=CastDoubleToUnsigned(
4017 GetDrawValue(token,&next_token)+0.5);
4018 if (token == next_token)
4019 ThrowPointExpectedException(image,token);
4028 if (LocaleCompare(
"word-spacing",keyword) == 0)
4030 (void) GetNextToken(q,&q,extent,token);
4031 graphic_context[n]->interword_spacing=GetDrawValue(token,
4033 if (token == next_token)
4034 ThrowPointExpectedException(image,token);
4046 if (status == MagickFalse)
4048 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
4049 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
4050 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
4051 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
4053 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
4054 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
4055 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
4056 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
4057 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
4059 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4062 if (primitive_type == UndefinedPrimitive)
4064 if ((draw_info->debug != MagickFalse) && (q > p))
4065 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int)
4072 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4073 if (primitive_info[i].text != (
char *) NULL)
4074 primitive_info[i].text=DestroyString(primitive_info[i].text);
4078 primitive_info[0].primitive=primitive_type;
4079 primitive_info[0].point.x=0.0;
4080 primitive_info[0].point.y=0.0;
4081 primitive_info[0].coordinates=0;
4082 primitive_info[0].method=FloodfillMethod;
4083 primitive_info[0].closed_subpath=MagickFalse;
4084 for (x=0; *q !=
'\0'; x++)
4089 if (IsValidPoint(q) == MagickFalse)
4091 (void) GetNextToken(q,&q,extent,token);
4092 point.x=GetDrawValue(token,&next_token);
4093 if (token == next_token)
4094 ThrowPointExpectedException(image,token);
4095 (void) GetNextToken(q,&q,extent,token);
4096 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
4097 ThrowPointExpectedException(image,token);
4099 (void) GetNextToken(q,&q,extent,token);
4100 point.y=GetDrawValue(token,&next_token);
4101 if (token == next_token)
4102 ThrowPointExpectedException(image,token);
4103 (void) GetNextToken(q,(
const char **) NULL,extent,token);
4105 (void) GetNextToken(q,&q,extent,token);
4106 primitive_info[i].primitive=primitive_type;
4107 primitive_info[i].point=point;
4108 primitive_info[i].coordinates=0;
4109 primitive_info[i].method=FloodfillMethod;
4110 primitive_info[i].closed_subpath=MagickFalse;
4113 if (i < (ssize_t) number_points)
4115 status&=CheckPrimitiveExtent(&mvg_info,(
double) number_points);
4116 primitive_info=(*mvg_info.primitive_info);
4118 if (status == MagickFalse)
4120 if (primitive_info[j].text != (
char *) NULL)
4121 primitive_info[j].text=DestroyString(primitive_info[j].text);
4122 primitive_info[j].primitive=primitive_type;
4123 primitive_info[j].coordinates=(size_t) x;
4124 primitive_info[j].method=FloodfillMethod;
4125 primitive_info[j].closed_subpath=MagickFalse;
4129 bounds.x1=primitive_info[j].point.x;
4130 bounds.y1=primitive_info[j].point.y;
4131 bounds.x2=primitive_info[j].point.x;
4132 bounds.y2=primitive_info[j].point.y;
4133 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4135 point=primitive_info[j+k].point;
4136 if (point.x < bounds.x1)
4138 if (point.y < bounds.y1)
4140 if (point.x > bounds.x2)
4142 if (point.y > bounds.y2)
4148 coordinates=(double) primitive_info[j].coordinates;
4149 switch (primitive_type)
4151 case RectanglePrimitive:
4156 case RoundRectanglePrimitive:
4163 alpha=bounds.x2-bounds.x1;
4164 beta=bounds.y2-bounds.y1;
4165 radius=hypot(alpha,beta);
4167 coordinates+=2.0*((size_t) ceil((
double) MagickPI*radius))+6.0*
4168 BezierQuantum+360.0;
4171 case BezierPrimitive:
4173 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4182 (void) GetNextToken(q,&q,extent,token);
4185 for (s=token; *s !=
'\0'; s=t)
4190 value=GetDrawValue(s,&t);
4199 for (s=token; *s !=
'\0'; s++)
4200 if (strspn(s,
"AaCcQqSsTt") != 0)
4201 coordinates+=(20.0*BezierQuantum)+360.0;
4207 if (status == MagickFalse)
4209 if (((
size_t) (i+coordinates)) >= number_points)
4214 number_points+=coordinates+1;
4215 if (number_points < (
size_t) coordinates)
4217 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4218 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
4224 status&=CheckPrimitiveExtent(&mvg_info,(
double) number_points);
4225 primitive_info=(*mvg_info.primitive_info);
4227 status&=CheckPrimitiveExtent(&mvg_info,PrimitiveExtentPad);
4228 primitive_info=(*mvg_info.primitive_info);
4229 if (status == MagickFalse)
4232 switch (primitive_type)
4234 case PointPrimitive:
4237 if (primitive_info[j].coordinates != 1)
4242 status&=TracePoint(primitive_info+j,primitive_info[j].point);
4243 primitive_info=(*mvg_info.primitive_info);
4244 i=(ssize_t) (j+primitive_info[j].coordinates);
4249 if (primitive_info[j].coordinates != 2)
4254 status&=TraceLine(primitive_info+j,primitive_info[j].point,
4255 primitive_info[j+1].point);
4256 primitive_info=(*mvg_info.primitive_info);
4257 i=(ssize_t) (j+primitive_info[j].coordinates);
4260 case RectanglePrimitive:
4262 if (primitive_info[j].coordinates != 2)
4267 status&=TraceRectangle(primitive_info+j,primitive_info[j].point,
4268 primitive_info[j+1].point);
4269 primitive_info=(*mvg_info.primitive_info);
4270 i=(ssize_t) (j+primitive_info[j].coordinates);
4273 case RoundRectanglePrimitive:
4275 if (primitive_info[j].coordinates != 3)
4280 if ((primitive_info[j+2].point.x < 0.0) ||
4281 (primitive_info[j+2].point.y < 0.0))
4286 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4291 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4296 status&=TraceRoundRectangle(&mvg_info,primitive_info[j].point,
4297 primitive_info[j+1].point,primitive_info[j+2].point);
4298 primitive_info=(*mvg_info.primitive_info);
4299 i=(ssize_t) (j+primitive_info[j].coordinates);
4304 if (primitive_info[j].coordinates != 3)
4309 status&=TraceArc(&mvg_info,primitive_info[j].point,
4310 primitive_info[j+1].point,primitive_info[j+2].point);
4311 primitive_info=(*mvg_info.primitive_info);
4312 i=(ssize_t) (j+primitive_info[j].coordinates);
4315 case EllipsePrimitive:
4317 if (primitive_info[j].coordinates != 3)
4322 if ((primitive_info[j+1].point.x < 0.0) ||
4323 (primitive_info[j+1].point.y < 0.0))
4328 status&=TraceEllipse(&mvg_info,primitive_info[j].point,
4329 primitive_info[j+1].point,primitive_info[j+2].point);
4330 primitive_info=(*mvg_info.primitive_info);
4331 i=(ssize_t) (j+primitive_info[j].coordinates);
4334 case CirclePrimitive:
4336 if (primitive_info[j].coordinates != 2)
4341 status&=TraceCircle(&mvg_info,primitive_info[j].point,
4342 primitive_info[j+1].point);
4343 primitive_info=(*mvg_info.primitive_info);
4344 i=(ssize_t) (j+primitive_info[j].coordinates);
4347 case PolylinePrimitive:
4349 if (primitive_info[j].coordinates < 1)
4356 case PolygonPrimitive:
4358 if (primitive_info[j].coordinates < 3)
4363 primitive_info[i]=primitive_info[j];
4364 primitive_info[i].coordinates=0;
4365 primitive_info[j].coordinates++;
4366 primitive_info[j].closed_subpath=MagickTrue;
4370 case BezierPrimitive:
4372 if (primitive_info[j].coordinates < 3)
4377 status&=TraceBezier(&mvg_info,primitive_info[j].coordinates);
4378 primitive_info=(*mvg_info.primitive_info);
4379 i=(ssize_t) (j+primitive_info[j].coordinates);
4384 coordinates=(double) TracePath(image,&mvg_info,token);
4385 primitive_info=(*mvg_info.primitive_info);
4386 if (coordinates < 0.0)
4391 i=(ssize_t) (j+coordinates);
4394 case ColorPrimitive:
4395 case MattePrimitive:
4400 if (primitive_info[j].coordinates != 1)
4405 (void) GetNextToken(q,&q,extent,token);
4406 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4412 primitive_info[j].method=(PaintMethod) method;
4418 geometry[MagickPathExtent];
4420 if (primitive_info[j].coordinates != 1)
4426 (void) GetNextToken(q,&q,extent,token);
4427 (void) CloneString(&primitive_info[j].text,token);
4431 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4432 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4433 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4435 mvg_info.point=primitive_info->point;
4436 primitive_info->point.x+=cursor;
4440 mvg_info.point=primitive_info->point;
4443 (void) FormatLocaleString(geometry,MagickPathExtent,
"%+f%+f",
4444 primitive_info->point.x,primitive_info->point.y);
4445 clone_info->render=MagickFalse;
4446 clone_info->text=AcquireString(token);
4447 status&=GetTypeMetrics(image,clone_info,&metrics);
4448 clone_info=DestroyDrawInfo(clone_info);
4449 cursor+=metrics.width;
4450 if (graphic_context[n]->compliance != SVGCompliance)
4454 case ImagePrimitive:
4456 if (primitive_info[j].coordinates != 2)
4461 (void) GetNextToken(q,&q,extent,token);
4462 (void) CloneString(&primitive_info[j].text,token);
4469 primitive_info[i].primitive=UndefinedPrimitive;
4470 if ((draw_info->debug != MagickFalse) && (q > p))
4471 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" %.*s",(
int) (q-p),p);
4475 status&=CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4476 &graphic_context[n]->affine));
4477 primitive_info=(*mvg_info.primitive_info);
4480 status&=CheckPrimitiveExtent(&mvg_info,(
double)
4481 graphic_context[n]->stroke_width);
4482 primitive_info=(*mvg_info.primitive_info);
4490 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4492 point=primitive_info[i].point;
4493 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4494 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4495 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4496 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4497 point=primitive_info[i].point;
4498 if (point.x < graphic_context[n]->bounds.x1)
4499 graphic_context[n]->bounds.x1=point.x;
4500 if (point.y < graphic_context[n]->bounds.y1)
4501 graphic_context[n]->bounds.y1=point.y;
4502 if (point.x > graphic_context[n]->bounds.x2)
4503 graphic_context[n]->bounds.x2=point.x;
4504 if (point.y > graphic_context[n]->bounds.y2)
4505 graphic_context[n]->bounds.y2=point.y;
4506 if (primitive_info[i].primitive == ImagePrimitive)
4508 if (i >= (ssize_t) number_points)
4509 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
4511 if (graphic_context[n]->render != MagickFalse)
4513 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4514 (graphic_context[n]->clip_mask != (
char *) NULL) &&
4515 (LocaleCompare(graphic_context[n]->clip_mask,
4516 graphic_context[n-1]->clip_mask) != 0))
4521 clip_path=(
const char *) GetValueFromSplayTree(macros,
4522 graphic_context[n]->clip_mask);
4523 if (clip_path != (
const char *) NULL)
4524 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4526 status&=DrawClipPath(image,graphic_context[n],
4527 graphic_context[n]->clip_mask);
4529 status&=DrawPrimitive(image,graphic_context[n],primitive_info);
4531 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4533 if (proceed == MagickFalse)
4538 if (draw_info->debug != MagickFalse)
4539 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
"end draw-image");
4543 macros=DestroySplayTree(macros);
4544 token=DestroyString(token);
4545 if (primitive_info != (PrimitiveInfo *) NULL)
4547 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4548 if (primitive_info[i].text != (
char *) NULL)
4549 primitive_info[i].text=DestroyString(primitive_info[i].text);
4550 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4552 primitive=DestroyString(primitive);
4553 for ( ; n >= 0; n--)
4554 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4555 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4556 if (status == MagickFalse)
4557 ThrowBinaryImageException(DrawError,
4558 "NonconformingDrawingPrimitiveDefinition",keyword);
4559 return(status != 0 ? MagickTrue : MagickFalse);
4562MagickExport MagickBooleanType DrawImage(Image *image,
const DrawInfo *draw_info)
4564 return(RenderMVGContent(image,draw_info,0));
4596MagickExport MagickBooleanType DrawPatternPath(Image *image,
4597 const DrawInfo *draw_info,
const char *name,Image **pattern)
4600 property[MaxTextExtent];
4616 assert(image != (Image *) NULL);
4617 assert(image->signature == MagickCoreSignature);
4618 assert(draw_info != (
const DrawInfo *) NULL);
4619 if (IsEventLogging() != MagickFalse)
4620 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4621 assert(name != (
const char *) NULL);
4622 (void) FormatLocaleString(property,MaxTextExtent,
"%s",name);
4623 path=GetImageArtifact(image,property);
4624 if (path == (
const char *) NULL)
4625 return(MagickFalse);
4626 (void) FormatLocaleString(property,MaxTextExtent,
"%s-geometry",name);
4627 geometry=GetImageArtifact(image,property);
4628 if (geometry == (
const char *) NULL)
4629 return(MagickFalse);
4630 if ((*pattern) != (Image *) NULL)
4631 *pattern=DestroyImage(*pattern);
4632 image_info=AcquireImageInfo();
4633 image_info->size=AcquireString(geometry);
4634 *pattern=AcquireImage(image_info);
4635 image_info=DestroyImageInfo(image_info);
4636 (void) QueryColorDatabase(
"#00000000",&(*pattern)->background_color,
4638 (void) SetImageBackgroundColor(*pattern);
4639 if (draw_info->debug != MagickFalse)
4640 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4641 "begin pattern-path %s %s",name,geometry);
4642 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4643 if (clone_info->fill_pattern != (Image *) NULL)
4644 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4645 if (clone_info->stroke_pattern != (Image *) NULL)
4646 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4647 (void) FormatLocaleString(property,MaxTextExtent,
"%s-type",name);
4648 type=GetImageArtifact(image,property);
4649 if (type != (
const char *) NULL)
4650 clone_info->gradient.type=(GradientType) ParseCommandOption(
4651 MagickGradientOptions,MagickFalse,type);
4652 (void) CloneString(&clone_info->primitive,path);
4653 status=RenderMVGContent(*pattern,clone_info,0);
4654 clone_info=DestroyDrawInfo(clone_info);
4655 if (draw_info->debug != MagickFalse)
4656 (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 DrawInfo *draw_info,
4702 const PrimitiveInfo *primitive_info,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(draw_info,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 AcquirePolygonEdgesTLS(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 (
size_t) (polygon_info->number_edges-edge)*
sizeof(*polygon_info->edges));
4801 return(polygon_info->number_edges);
4804static double GetOpacityPixel(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_opacity)
4833 *stroke_opacity=0.0;
4834 subpath_opacity=0.0;
4835 p=polygon_info->edges;
4836 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4838 if ((
double) y <= (p->bounds.y1-mid-0.5))
4840 if ((
double) y > (p->bounds.y2+mid+0.5))
4843 (void) DestroyEdge(polygon_info,j--);
4846 if (((
double) x <= (p->bounds.x1-mid-0.5)) ||
4847 ((
double) x > (p->bounds.x2+mid+0.5)))
4849 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4850 for ( ; i < (ssize_t) p->number_points; i++)
4852 if ((
double) y <= (p->points[i-1].y-mid-0.5))
4854 if ((
double) y > (p->points[i].y+mid+0.5))
4856 if (p->scanline != (
double) y)
4858 p->scanline=(double) y;
4859 p->highwater=(size_t) i;
4865 delta.x=(q+1)->x-q->x;
4866 delta.y=(q+1)->y-q->y;
4867 beta=delta.x*(x-q->x)+delta.y*(y-q->y);
4870 delta.x=(double) x-q->x;
4871 delta.y=(double) y-q->y;
4872 distance=delta.x*delta.x+delta.y*delta.y;
4876 alpha=delta.x*delta.x+delta.y*delta.y;
4879 delta.x=(double) x-(q+1)->x;
4880 delta.y=(double) y-(q+1)->y;
4881 distance=delta.x*delta.x+delta.y*delta.y;
4885 alpha=MagickSafeReciprocal(alpha);
4886 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
4887 distance=alpha*beta*beta;
4894 if (p->ghostline == MagickFalse)
4897 if ((*stroke_opacity < 1.0) &&
4898 (distance <= ((alpha+0.25)*(alpha+0.25))))
4901 if (distance <= ((alpha+0.25)*(alpha+0.25)))
4902 *stroke_opacity=1.0;
4906 if (fabs(distance-1.0) >= MagickEpsilon)
4907 beta=sqrt((
double) distance);
4909 if (*stroke_opacity < ((alpha-0.25)*(alpha-0.25)))
4910 *stroke_opacity=(alpha-0.25)*(alpha-0.25);
4914 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_opacity >= 1.0))
4916 if (distance <= 0.0)
4918 subpath_opacity=1.0;
4923 if (fabs(beta) < MagickEpsilon)
4926 if (fabs(distance-1.0) >= MagickEpsilon)
4927 beta=sqrt(distance);
4930 if (subpath_opacity < (alpha*alpha))
4931 subpath_opacity=alpha*alpha;
4937 if (fill == MagickFalse)
4939 if (subpath_opacity >= 1.0)
4945 p=polygon_info->edges;
4946 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4948 if ((
double) y <= p->bounds.y1)
4950 if (((
double) y > p->bounds.y2) || ((
double) x <= p->bounds.x1))
4952 if ((
double) x > p->bounds.x2)
4954 winding_number+=p->direction != 0 ? 1 : -1;
4957 i=(ssize_t) MagickMax((
double) p->highwater,1.0);
4958 for ( ; i < (ssize_t) (p->number_points-1); i++)
4959 if ((
double) y <= p->points[i].y)
4962 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
4963 winding_number+=p->direction != 0 ? 1 : -1;
4965 if (fill_rule != NonZeroRule)
4967 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
4971 if (MagickAbsoluteValue(winding_number) != 0)
4973 return(subpath_opacity);
4976static MagickBooleanType DrawPolygonPrimitive(Image *image,
4977 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info)
4979 typedef struct _ExtentInfo
5008 **magick_restrict polygon_info;
5023 assert(image != (Image *) NULL);
5024 assert(image->signature == MagickCoreSignature);
5025 assert(draw_info != (DrawInfo *) NULL);
5026 assert(draw_info->signature == MagickCoreSignature);
5027 assert(primitive_info != (PrimitiveInfo *) NULL);
5028 if (IsEventLogging() != MagickFalse)
5029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
5030 if (primitive_info->coordinates <= 1)
5035 polygon_info=AcquirePolygonTLS(draw_info,primitive_info,&image->exception);
5036 if (polygon_info == (PolygonInfo **) NULL)
5037 return(MagickFalse);
5038 if (draw_info->debug != MagickFalse)
5039 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" begin draw-polygon");
5040 fill=(primitive_info->method == FillToBorderMethod) ||
5041 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
5042 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5043 bounds=polygon_info[0]->edges[0].bounds;
5044 artifact=GetImageArtifact(image,
"draw:render-bounding-rectangles");
5045 if (IsStringTrue(artifact) != MagickFalse)
5046 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0]);
5047 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
5049 p=polygon_info[0]->edges+i;
5050 if (p->bounds.x1 < bounds.x1)
5051 bounds.x1=p->bounds.x1;
5052 if (p->bounds.y1 < bounds.y1)
5053 bounds.y1=p->bounds.y1;
5054 if (p->bounds.x2 > bounds.x2)
5055 bounds.x2=p->bounds.x2;
5056 if (p->bounds.y2 > bounds.y2)
5057 bounds.y2=p->bounds.y2;
5059 bounds.x1-=(mid+1.0);
5060 bounds.y1-=(mid+1.0);
5061 bounds.x2+=(mid+1.0);
5062 bounds.y2+=(mid+1.0);
5063 if ((bounds.x1 >= (
double) image->columns) ||
5064 (bounds.y1 >= (
double) image->rows) ||
5065 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5067 polygon_info=DestroyPolygonTLS(polygon_info);
5070 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5071 (
double) image->columns-1.0 : bounds.x1;
5072 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5073 (
double) image->rows-1.0 : bounds.y1;
5074 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5075 (
double) image->columns-1.0 : bounds.x2;
5076 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5077 (
double) image->rows-1.0 : bounds.y2;
5078 poly_extent.x1=CastDoubleToLong(ceil(bounds.x1-0.5));
5079 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5080 poly_extent.x2=CastDoubleToLong(floor(bounds.x2+0.5));
5081 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5082 number_threads=GetMagickNumberThreads(image,image,poly_extent.y2-
5083 poly_extent.y1+1,1);
5084 status=AcquirePolygonEdgesTLS(polygon_info,number_threads,&image->exception);
5085 if (status == MagickFalse)
5087 polygon_info=DestroyPolygonTLS(polygon_info);
5091 exception=(&image->exception);
5092 image_view=AcquireAuthenticCacheView(image,exception);
5093 if ((primitive_info->coordinates == 1) ||
5094 (polygon_info[0]->number_edges == 0))
5099#if defined(MAGICKCORE_OPENMP_SUPPORT)
5100 #pragma omp parallel for schedule(static) shared(status) \
5101 num_threads(number_threads)
5103 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5114 if (status == MagickFalse)
5117 q=GetCacheViewAuthenticPixels(image_view,x,y,(
size_t) (poly_extent.x2-
5119 if (q == (PixelPacket *) NULL)
5124 for ( ; x <= poly_extent.x2; x++)
5126 if ((x == CastDoubleToLong(ceil(primitive_info->point.x-0.5))) &&
5127 (y == CastDoubleToLong(ceil(primitive_info->point.y-0.5))))
5128 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,q);
5131 sync=SyncCacheViewAuthenticPixels(image_view,exception);
5132 if (sync == MagickFalse)
5135 image_view=DestroyCacheView(image_view);
5136 polygon_info=DestroyPolygonTLS(polygon_info);
5137 if (draw_info->debug != MagickFalse)
5138 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5139 " end draw-polygon");
5145 poly_extent.y1=CastDoubleToLong(ceil(bounds.y1-0.5));
5146 poly_extent.y2=CastDoubleToLong(floor(bounds.y2+0.5));
5147#if defined(MAGICKCORE_OPENMP_SUPPORT)
5148 #pragma omp parallel for schedule(static) shared(status) \
5149 num_threads(number_threads)
5151 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5154 id = GetOpenMPThreadId();
5166 if (status == MagickFalse)
5168 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(
size_t)
5169 (poly_extent.x2-poly_extent.x1+1),1,exception);
5170 if (q == (PixelPacket *) NULL)
5175 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5184 fill_opacity=GetOpacityPixel(polygon_info[
id],mid,fill,
5185 draw_info->fill_rule,x,y,&stroke_opacity);
5186 if (draw_info->stroke_antialias == MagickFalse)
5188 fill_opacity=fill_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5189 stroke_opacity=stroke_opacity >= AntialiasThreshold ? 1.0 : 0.0;
5191 (void) GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5193 fill_opacity=(double) ((MagickRealType) QuantumRange-fill_opacity*
5194 ((MagickRealType) QuantumRange-(MagickRealType) fill_color.opacity));
5195 MagickCompositeOver(&fill_color,(MagickRealType) fill_opacity,q,
5196 (MagickRealType) q->opacity,q);
5197 (void) GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,
5199 stroke_opacity=(double) ((MagickRealType) QuantumRange-stroke_opacity*
5200 ((MagickRealType) QuantumRange-(MagickRealType) stroke_color.opacity));
5201 MagickCompositeOver(&stroke_color,(MagickRealType) stroke_opacity,q,
5202 (MagickRealType) q->opacity,q);
5205 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5208 image_view=DestroyCacheView(image_view);
5209 polygon_info=DestroyPolygonTLS(polygon_info);
5210 if (draw_info->debug != MagickFalse)
5211 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-polygon");
5242static void LogPrimitiveInfo(
const PrimitiveInfo *primitive_info)
5268 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5269 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5270 switch (primitive_info->primitive)
5272 case PointPrimitive:
5274 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5275 "PointPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5276 methods[primitive_info->method]);
5279 case ColorPrimitive:
5281 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5282 "ColorPrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5283 methods[primitive_info->method]);
5286 case MattePrimitive:
5288 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5289 "MattePrimitive %.20g,%.20g %s",(
double) x,(
double) y,
5290 methods[primitive_info->method]);
5295 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5296 "TextPrimitive %.20g,%.20g",(
double) x,(
double) y);
5299 case ImagePrimitive:
5301 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5302 "ImagePrimitive %.20g,%.20g",(
double) x,(
double) y);
5309 p=primitive_info[0].point;
5312 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5314 point=primitive_info[i].point;
5315 if (coordinates <= 0)
5317 coordinates=(ssize_t) primitive_info[i].coordinates;
5318 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5319 " begin open (%.20g)",(
double) coordinates);
5322 point=primitive_info[i].point;
5323 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5324 (fabs(q.y-point.y) >= MagickEpsilon))
5325 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5326 " %.20g: %.18g,%.18g",(
double) coordinates,point.x,point.y);
5328 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
5329 " %.20g: %g %g (duplicate)",(
double) coordinates,point.x,point.y);
5332 if (coordinates > 0)
5334 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5335 (fabs(p.y-point.y) >= MagickEpsilon))
5336 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end last (%.20g)",
5337 (
double) coordinates);
5339 (
void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end open (%.20g)",
5340 (
double) coordinates);
5344MagickExport MagickBooleanType DrawPrimitive(Image *image,
5345 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info)
5363 if (draw_info->debug != MagickFalse)
5365 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5366 " begin draw-primitive");
5367 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5368 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5369 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5370 draw_info->affine.tx,draw_info->affine.ty);
5372 exception=(&image->exception);
5374 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5375 ((IsPixelGray(&draw_info->fill) == MagickFalse) ||
5376 (IsPixelGray(&draw_info->stroke) == MagickFalse)))
5377 status=SetImageColorspace(image,sRGBColorspace);
5378 if (draw_info->compliance == SVGCompliance)
5380 status&=SetImageClipMask(image,draw_info->clipping_mask);
5381 status&=SetImageMask(image,draw_info->composite_mask);
5383 x=CastDoubleToLong(ceil(primitive_info->point.x-0.5));
5384 y=CastDoubleToLong(ceil(primitive_info->point.y-0.5));
5385 image_view=AcquireAuthenticCacheView(image,exception);
5386 switch (primitive_info->primitive)
5388 case ColorPrimitive:
5390 switch (primitive_info->method)
5398 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5399 if (q == (PixelPacket *) NULL)
5401 (void) GetFillColor(draw_info,x,y,q);
5402 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5410 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5411 for (y=0; y < (ssize_t) image->rows; y++)
5416 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5418 if (q == (PixelPacket *) NULL)
5420 for (x=0; x < (ssize_t) image->columns; x++)
5422 if (IsColorSimilar(image,q,&target) == MagickFalse)
5427 (void) GetFillColor(draw_info,x,y,q);
5430 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5431 if (status == MagickFalse)
5436 case FloodfillMethod:
5437 case FillToBorderMethod:
5442 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5443 if (primitive_info->method == FillToBorderMethod)
5445 target.red=(MagickRealType) draw_info->border_color.red;
5446 target.green=(MagickRealType) draw_info->border_color.green;
5447 target.blue=(MagickRealType) draw_info->border_color.blue;
5449 status&=FloodfillPaintImage(image,DefaultChannels,draw_info,&target,x,
5450 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5456 for (y=0; y < (ssize_t) image->rows; y++)
5464 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5466 if (q == (PixelPacket *) NULL)
5468 for (x=0; x < (ssize_t) image->columns; x++)
5470 (void) GetFillColor(draw_info,x,y,q);
5473 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5474 if (status == MagickFalse)
5482 case MattePrimitive:
5484 if (image->matte == MagickFalse)
5485 status&=SetImageAlphaChannel(image,OpaqueAlphaChannel);
5486 switch (primitive_info->method)
5497 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5498 if (q == (PixelPacket *) NULL)
5500 (void) GetFillColor(draw_info,x,y,&pixel);
5501 SetPixelOpacity(q,pixel.opacity);
5502 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5511 status&=GetOneCacheViewVirtualPixel(image_view,x,y,&target,exception);
5512 for (y=0; y < (ssize_t) image->rows; y++)
5520 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5522 if (q == (PixelPacket *) NULL)
5524 for (x=0; x < (ssize_t) image->columns; x++)
5526 if (IsColorSimilar(image,q,&target) == MagickFalse)
5531 (void) GetFillColor(draw_info,x,y,&pixel);
5532 SetPixelOpacity(q,pixel.opacity);
5535 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5536 if (status == MagickFalse)
5541 case FloodfillMethod:
5542 case FillToBorderMethod:
5547 status&=GetOneVirtualMagickPixel(image,x,y,&target,exception);
5548 if (primitive_info->method == FillToBorderMethod)
5550 target.red=(MagickRealType) draw_info->border_color.red;
5551 target.green=(MagickRealType) draw_info->border_color.green;
5552 target.blue=(MagickRealType) draw_info->border_color.blue;
5554 status&=FloodfillPaintImage(image,OpacityChannel,draw_info,&target,x,
5555 y,primitive_info->method == FloodfillMethod ? MagickFalse :
5564 for (y=0; y < (ssize_t) image->rows; y++)
5572 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5574 if (q == (PixelPacket *) NULL)
5576 for (x=0; x < (ssize_t) image->columns; x++)
5578 (void) GetFillColor(draw_info,x,y,&pixel);
5579 SetPixelOpacity(q,pixel.opacity);
5582 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5583 if (status == MagickFalse)
5591 case ImagePrimitive:
5597 composite_geometry[MaxTextExtent],
5598 magic[MagickPathExtent] = {
'\0'};
5614 if (primitive_info->text == (
char *) NULL)
5616 clone_info=AcquireImageInfo();
5617 composite_images=(Image *) NULL;
5618 if (LocaleNCompare(primitive_info->text,
"data:",5) == 0)
5619 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5622 if (*primitive_info->text !=
'\0')
5627 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5629 (void) SetImageInfo(clone_info,1,exception);
5630 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5632 if (clone_info->size != (
char *) NULL)
5633 clone_info->size=DestroyString(clone_info->size);
5634 if (clone_info->extract != (
char *) NULL)
5635 clone_info->extract=DestroyString(clone_info->extract);
5636 GetPathComponent(clone_info->filename,MagickPath,magic);
5638 composite_images=ReadImage(clone_info,exception);
5640 (
void) ThrowMagickException(exception,GetMagickModule(),
5641 FileOpenError,
"UnableToOpenFile",
"`%s'",clone_info->filename);
5643 clone_info=DestroyImageInfo(clone_info);
5644 if (composite_images == (Image *) NULL)
5649 composite_image=RemoveFirstImageFromList(&composite_images);
5650 composite_images=DestroyImageList(composite_images);
5651 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5652 NULL,(
void *) NULL);
5653 x1=CastDoubleToLong(ceil(primitive_info[1].point.x-0.5));
5654 y1=CastDoubleToLong(ceil(primitive_info[1].point.y-0.5));
5655 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5656 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5659 geometry[MaxTextExtent];
5664 (void) FormatLocaleString(geometry,MaxTextExtent,
"%gx%g!",
5665 primitive_info[1].point.x,primitive_info[1].point.y);
5666 composite_image->filter=image->filter;
5667 status&=TransformImage(&composite_image,(
char *) NULL,geometry);
5669 if (composite_image->matte == MagickFalse)
5670 status&=SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
5671 if (draw_info->opacity != OpaqueOpacity)
5672 status&=SetImageOpacity(composite_image,draw_info->opacity);
5673 SetGeometry(image,&geometry);
5674 image->gravity=draw_info->gravity;
5677 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
5678 "%.20gx%.20g%+.20g%+.20g",(
double) composite_image->columns,(
double)
5679 composite_image->rows,(
double) geometry.x,(
double) geometry.y);
5680 (void) ParseGravityGeometry(image,composite_geometry,&geometry,
5682 affine=draw_info->affine;
5683 affine.tx=(double) geometry.x;
5684 affine.ty=(double) geometry.y;
5685 composite_image->interpolate=image->interpolate;
5686 if ((draw_info->compose == OverCompositeOp) ||
5687 (draw_info->compose == SrcOverCompositeOp))
5688 status&=DrawAffineImage(image,composite_image,&affine);
5690 status&=CompositeImage(image,draw_info->compose,composite_image,
5691 geometry.x,geometry.y);
5692 composite_image=DestroyImage(composite_image);
5695 case PointPrimitive:
5703 if ((y < 0) || (y >= (ssize_t) image->rows))
5705 if ((x < 0) || (x >= (ssize_t) image->columns))
5707 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5708 if (q == (PixelPacket *) NULL)
5710 (void) GetFillColor(draw_info,x,y,&fill_color);
5711 MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
5712 (MagickRealType) q->opacity,q);
5713 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5719 geometry[MaxTextExtent];
5724 if (primitive_info->text == (
char *) NULL)
5726 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5727 (void) CloneString(&clone_info->text,primitive_info->text);
5728 (void) FormatLocaleString(geometry,MaxTextExtent,
"%+f%+f",
5729 primitive_info->point.x,primitive_info->point.y);
5730 (void) CloneString(&clone_info->geometry,geometry);
5731 status&=AnnotateImage(image,clone_info);
5732 clone_info=DestroyDrawInfo(clone_info);
5744 if (IsEventLogging() != MagickFalse)
5745 LogPrimitiveInfo(primitive_info);
5746 scale=ExpandAffine(&draw_info->affine);
5747 if ((draw_info->dash_pattern != (
double *) NULL) &&
5748 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5749 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5750 (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
5755 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5756 clone_info->stroke_width=0.0;
5757 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5758 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5759 clone_info=DestroyDrawInfo(clone_info);
5760 if (status != MagickFalse)
5761 status&=DrawDashPolygon(draw_info,primitive_info,image);
5764 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5766 ((draw_info->stroke.opacity != (Quantum) TransparentOpacity) ||
5767 (draw_info->stroke_pattern != (Image *) NULL)))
5779 closed_path=primitive_info[0].closed_subpath;
5780 i=(ssize_t) primitive_info[0].coordinates;
5781 x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5782 y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5783 if ((x < MagickEpsilon) && (y < MagickEpsilon))
5784 closed_path=MagickTrue;
5785 if ((((draw_info->linecap == RoundCap) ||
5786 (closed_path != MagickFalse)) &&
5787 (draw_info->linejoin == RoundJoin)) ||
5788 (primitive_info[i].primitive != UndefinedPrimitive))
5790 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5793 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5794 clone_info->stroke_width=0.0;
5795 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5796 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5797 clone_info=DestroyDrawInfo(clone_info);
5798 if (status != MagickFalse)
5799 status&=DrawStrokePolygon(image,draw_info,primitive_info);
5802 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5806 image_view=DestroyCacheView(image_view);
5807 if (draw_info->compliance == SVGCompliance)
5809 status&=SetImageClipMask(image,(Image *) NULL);
5810 status&=SetImageMask(image,(Image *) NULL);
5812 if (draw_info->debug != MagickFalse)
5813 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
" end draw-primitive");
5814 return(status != 0 ? MagickTrue : MagickFalse);
5847static MagickBooleanType DrawRoundLinecap(Image *image,
5848 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info)
5856 if (primitive_info->coordinates < 1)
5857 return(MagickFalse);
5858 for (i=0; i < 4; i++)
5859 linecap[i]=(*primitive_info);
5860 linecap[0].coordinates=4;
5861 linecap[1].point.x+=2.0*MagickEpsilon;
5862 linecap[2].point.x+=2.0*MagickEpsilon;
5863 linecap[2].point.y+=2.0*MagickEpsilon;
5864 linecap[3].point.y+=2.0*MagickEpsilon;
5865 linecap[4].primitive=UndefinedPrimitive;
5866 return(DrawPolygonPrimitive(image,draw_info,linecap));
5869static MagickBooleanType DrawStrokePolygon(Image *image,
5870 const DrawInfo *draw_info,
const PrimitiveInfo *primitive_info)
5891 if (draw_info->debug != MagickFalse)
5892 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5893 " begin draw-stroke-polygon");
5894 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5895 clone_info->fill=draw_info->stroke;
5896 if (clone_info->fill_pattern != (Image *) NULL)
5897 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5898 if (clone_info->stroke_pattern != (Image *) NULL)
5899 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5900 MagickTrue,&clone_info->stroke_pattern->exception);
5901 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5902 clone_info->stroke_width=0.0;
5903 clone_info->fill_rule=NonZeroRule;
5905 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
5907 if (p->coordinates == 1)
5909 stroke_polygon=TraceStrokePolygon(draw_info,p,&image->exception);
5910 if (stroke_polygon == (PrimitiveInfo *) NULL)
5915 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
5916 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5919 q=p+p->coordinates-1;
5920 closed_path=p->closed_subpath;
5921 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5923 status&=DrawRoundLinecap(image,draw_info,p);
5924 status&=DrawRoundLinecap(image,draw_info,q);
5927 clone_info=DestroyDrawInfo(clone_info);
5928 if (draw_info->debug != MagickFalse)
5929 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5930 " end draw-stroke-polygon");
5931 return(status != 0 ? MagickTrue : MagickFalse);
5957MagickExport
void GetAffineMatrix(AffineMatrix *affine_matrix)
5959 assert(affine_matrix != (AffineMatrix *) NULL);
5960 if (IsEventLogging() != MagickFalse)
5961 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
5962 (void) memset(affine_matrix,0,
sizeof(*affine_matrix));
5963 affine_matrix->sx=1.0;
5964 affine_matrix->sy=1.0;
5991MagickExport
void GetDrawInfo(
const ImageInfo *image_info,DrawInfo *draw_info)
6005 assert(draw_info != (DrawInfo *) NULL);
6006 if (IsEventLogging() != MagickFalse)
6007 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
6008 (void) memset(draw_info,0,
sizeof(*draw_info));
6009 draw_info->image_info=CloneImageInfo(image_info);
6010 GetAffineMatrix(&draw_info->affine);
6011 exception=AcquireExceptionInfo();
6012 (void) QueryColorDatabase(
"#000F",&draw_info->fill,exception);
6013 (void) QueryColorDatabase(
"#FFF0",&draw_info->stroke,exception);
6014 draw_info->stroke_antialias=draw_info->image_info->antialias;
6015 draw_info->stroke_width=1.0;
6016 draw_info->fill_rule=EvenOddRule;
6017 draw_info->opacity=OpaqueOpacity;
6018 draw_info->fill_opacity=OpaqueOpacity;
6019 draw_info->stroke_opacity=OpaqueOpacity;
6020 draw_info->linecap=ButtCap;
6021 draw_info->linejoin=MiterJoin;
6022 draw_info->miterlimit=10;
6023 draw_info->decorate=NoDecoration;
6024 if (draw_info->image_info->font != (
char *) NULL)
6025 draw_info->font=AcquireString(draw_info->image_info->font);
6026 if (draw_info->image_info->density != (
char *) NULL)
6027 draw_info->density=AcquireString(draw_info->image_info->density);
6028 draw_info->text_antialias=draw_info->image_info->antialias;
6029 draw_info->pointsize=12.0;
6030 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
6031 draw_info->pointsize=draw_info->image_info->pointsize;
6032 draw_info->undercolor.opacity=(Quantum) TransparentOpacity;
6033 draw_info->border_color=draw_info->image_info->border_color;
6034 draw_info->compose=OverCompositeOp;
6035 if (draw_info->image_info->server_name != (
char *) NULL)
6036 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
6037 draw_info->render=MagickTrue;
6038 draw_info->clip_path=MagickFalse;
6039 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
6040 MagickTrue : MagickFalse;
6041 option=GetImageOption(draw_info->image_info,
"direction");
6042 if (option != (
const char *) NULL)
6043 draw_info->direction=(DirectionType) ParseCommandOption(
6044 MagickDirectionOptions,MagickFalse,option);
6046 draw_info->direction=UndefinedDirection;
6047 option=GetImageOption(draw_info->image_info,
"encoding");
6048 if (option != (
const char *) NULL)
6049 (void) CloneString(&draw_info->encoding,option);
6050 option=GetImageOption(draw_info->image_info,
"family");
6051 if (option != (
const char *) NULL)
6052 (void) CloneString(&draw_info->family,option);
6053 option=GetImageOption(draw_info->image_info,
"fill");
6054 if (option != (
const char *) NULL)
6055 (void) QueryColorDatabase(option,&draw_info->fill,exception);
6056 option=GetImageOption(draw_info->image_info,
"gravity");
6057 if (option != (
const char *) NULL)
6058 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
6059 MagickFalse,option);
6060 option=GetImageOption(draw_info->image_info,
"interline-spacing");
6061 if (option != (
const char *) NULL)
6062 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6063 option=GetImageOption(draw_info->image_info,
"interword-spacing");
6064 if (option != (
const char *) NULL)
6065 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6066 option=GetImageOption(draw_info->image_info,
"kerning");
6067 if (option != (
const char *) NULL)
6068 draw_info->kerning=GetDrawValue(option,&next_token);
6069 option=GetImageOption(draw_info->image_info,
"stroke");
6070 if (option != (
const char *) NULL)
6071 (void) QueryColorDatabase(option,&draw_info->stroke,exception);
6072 option=GetImageOption(draw_info->image_info,
"strokewidth");
6073 if (option != (
const char *) NULL)
6074 draw_info->stroke_width=GetDrawValue(option,&next_token);
6075 option=GetImageOption(draw_info->image_info,
"style");
6076 if (option != (
const char *) NULL)
6077 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6078 MagickFalse,option);
6079 option=GetImageOption(draw_info->image_info,
"undercolor");
6080 if (option != (
const char *) NULL)
6081 (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
6082 option=GetImageOption(draw_info->image_info,
"weight");
6083 if (option != (
const char *) NULL)
6088 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6090 weight=(ssize_t) StringToUnsignedLong(option);
6091 draw_info->weight=(size_t) weight;
6093 exception=DestroyExceptionInfo(exception);
6094 draw_info->signature=MagickCoreSignature;
6122static inline double Permutate(
const ssize_t n,
const ssize_t k)
6131 for (i=k+1; i <= n; i++)
6133 for (i=1; i <= (n-k); i++)
6154static MagickBooleanType TraceArc(MVGInfo *mvg_info,
const PointInfo start,
6155 const PointInfo end,
const PointInfo degrees)
6161 center.x=0.5*(end.x+start.x);
6162 center.y=0.5*(end.y+start.y);
6163 radius.x=fabs(center.x-start.x);
6164 radius.y=fabs(center.y-start.y);
6165 return(TraceEllipse(mvg_info,center,radius,degrees));
6168static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,
const PointInfo start,
6169 const PointInfo end,
const PointInfo arc,
const double angle,
6170 const MagickBooleanType large_arc,
const MagickBooleanType sweep)
6207 offset=mvg_info->offset;
6208 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6209 primitive_info->coordinates=0;
6210 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6211 (fabs(start.y-end.y) < MagickEpsilon))
6212 return(TracePoint(primitive_info,end));
6213 radii.x=fabs(arc.x);
6214 radii.y=fabs(arc.y);
6215 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6216 return(TraceLine(primitive_info,start,end));
6217 cosine=cos(DegreesToRadians(fmod((
double) angle,360.0)));
6218 sine=sin(DegreesToRadians(fmod((
double) angle,360.0)));
6219 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6220 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6221 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6223 if (delta < MagickEpsilon)
6224 return(TraceLine(primitive_info,start,end));
6227 radii.x*=sqrt((
double) delta);
6228 radii.y*=sqrt((
double) delta);
6230 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6231 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6232 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6233 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6234 alpha=points[1].x-points[0].x;
6235 beta=points[1].y-points[0].y;
6236 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6237 return(TraceLine(primitive_info,start,end));
6238 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6243 factor=sqrt((
double) factor);
6244 if (sweep == large_arc)
6247 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6248 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6249 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6250 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6251 if ((theta < 0.0) && (sweep != MagickFalse))
6252 theta+=2.0*MagickPI;
6254 if ((theta > 0.0) && (sweep == MagickFalse))
6255 theta-=2.0*MagickPI;
6256 arc_segments=(size_t) CastDoubleToLong(ceil(fabs((
double) (theta/(0.5*
6257 MagickPI+MagickEpsilon)))));
6260 for (i=0; i < (ssize_t) arc_segments; i++)
6262 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6263 gamma=(8.0/3.0)*sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))*
6264 sin(fmod((
double) (0.5*beta),DegreesToRadians(360.0)))/
6265 sin(fmod((
double) beta,DegreesToRadians(360.0)));
6266 points[0].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) i*theta/
6267 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((
double) (alpha+
6268 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6269 points[0].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) i*theta/
6270 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((
double) (alpha+
6271 (
double) i*theta/arc_segments),DegreesToRadians(360.0))));
6272 points[2].x=(double) (center.x+cos(fmod((
double) (alpha+(
double) (i+1)*
6273 theta/arc_segments),DegreesToRadians(360.0))));
6274 points[2].y=(double) (center.y+sin(fmod((
double) (alpha+(
double) (i+1)*
6275 theta/arc_segments),DegreesToRadians(360.0))));
6276 points[1].x=(double) (points[2].x+gamma*sin(fmod((
double) (alpha+(
double)
6277 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6278 points[1].y=(double) (points[2].y-gamma*cos(fmod((
double) (alpha+(
double)
6279 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6280 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6281 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6282 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6284 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6286 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6288 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6290 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6292 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6294 if (i == (ssize_t) (arc_segments-1))
6296 status&=TraceBezier(mvg_info,4);
6299 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6300 mvg_info->offset+=p->coordinates;
6301 p+=(ptrdiff_t) p->coordinates;
6304 return(MagickFalse);
6305 mvg_info->offset=offset;
6306 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6307 primitive_info->coordinates=(size_t) (p-primitive_info);
6308 primitive_info->closed_subpath=MagickFalse;
6309 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6311 p->primitive=primitive_info->primitive;
6317static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6318 const size_t number_coordinates)
6347 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6348 quantum=number_coordinates;
6349 for (i=0; i < (ssize_t) number_coordinates; i++)
6351 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6353 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6354 if (alpha > (
double) GetMaxMemoryRequest())
6356 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6357 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6358 return(MagickFalse);
6360 if (alpha > (
double) quantum)
6361 quantum=(size_t) alpha;
6362 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6363 if (alpha > (
double) quantum)
6364 quantum=(size_t) alpha;
6367 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6368 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6369 if (quantum > (
double) GetMaxMemoryRequest())
6371 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6372 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6373 return(MagickFalse);
6375 coefficients=(
double *) AcquireQuantumMemory(number_coordinates,
6376 sizeof(*coefficients));
6377 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6379 if ((coefficients == (
double *) NULL) || (points == (PointInfo *) NULL))
6381 if (points != (PointInfo *) NULL)
6382 points=(PointInfo *) RelinquishMagickMemory(points);
6383 if (coefficients != (
double *) NULL)
6384 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6385 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6386 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
6387 return(MagickFalse);
6389 control_points=quantum*number_coordinates;
6390 if (CheckPrimitiveExtent(mvg_info,(
double) control_points+1) == MagickFalse)
6392 points=(PointInfo *) RelinquishMagickMemory(points);
6393 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6394 return(MagickFalse);
6396 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6400 end=primitive_info[number_coordinates-1].point;
6401 for (i=0; i < (ssize_t) number_coordinates; i++)
6402 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6404 for (i=0; i < (ssize_t) control_points; i++)
6409 alpha=pow((
double) (1.0-weight),(
double) number_coordinates-1.0);
6410 for (j=0; j < (ssize_t) number_coordinates; j++)
6412 point.x+=alpha*coefficients[j]*p->point.x;
6413 point.y+=alpha*coefficients[j]*p->point.y;
6414 alpha*=weight/(1.0-weight);
6418 weight+=1.0/control_points;
6424 for (i=0; i < (ssize_t) control_points; i++)
6426 if (TracePoint(p,points[i]) == MagickFalse)
6428 points=(PointInfo *) RelinquishMagickMemory(points);
6429 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6430 return(MagickFalse);
6432 p+=(ptrdiff_t) p->coordinates;
6434 if (TracePoint(p,end) == MagickFalse)
6436 points=(PointInfo *) RelinquishMagickMemory(points);
6437 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6438 return(MagickFalse);
6440 p+=(ptrdiff_t) p->coordinates;
6441 primitive_info->coordinates=(size_t) (p-primitive_info);
6442 primitive_info->closed_subpath=MagickFalse;
6443 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6445 p->primitive=primitive_info->primitive;
6448 points=(PointInfo *) RelinquishMagickMemory(points);
6449 coefficients=(
double *) RelinquishMagickMemory(coefficients);
6453static MagickBooleanType TraceCircle(MVGInfo *mvg_info,
const PointInfo start,
6454 const PointInfo end)
6465 alpha=end.x-start.x;
6467 radius=hypot((
double) alpha,(
double) beta);
6468 offset.x=(double) radius;
6469 offset.y=(double) radius;
6472 return(TraceEllipse(mvg_info,start,offset,degrees));
6475static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,
const PointInfo center,
6476 const PointInfo radii,
const PointInfo arc)
6501 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6502 primitive_info->coordinates=0;
6503 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6505 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6506 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6507 angle.x=DegreesToRadians(arc.x);
6511 angle.y=DegreesToRadians(y);
6512 coordinates=ceil((angle.y-angle.x)/step+1.0);
6513 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6514 return(MagickFalse);
6516 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6517 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6519 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6520 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6521 if (i++ >= (ssize_t) coordinates)
6523 if (TracePoint(p,point) == MagickFalse)
6524 return(MagickFalse);
6525 p+=(ptrdiff_t) p->coordinates;
6527 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6528 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6529 if (TracePoint(p,point) == MagickFalse)
6530 return(MagickFalse);
6531 p+=(ptrdiff_t) p->coordinates;
6532 primitive_info->coordinates=(size_t) (p-primitive_info);
6533 primitive_info->closed_subpath=MagickFalse;
6534 x=fabs(primitive_info[0].point.x-
6535 primitive_info[primitive_info->coordinates-1].point.x);
6536 y=fabs(primitive_info[0].point.y-
6537 primitive_info[primitive_info->coordinates-1].point.y);
6538 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6539 primitive_info->closed_subpath=MagickTrue;
6540 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6542 p->primitive=primitive_info->primitive;
6548static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6549 const PointInfo start,
const PointInfo end)
6551 if (TracePoint(primitive_info,start) == MagickFalse)
6552 return(MagickFalse);
6553 if (TracePoint(primitive_info+1,end) == MagickFalse)
6554 return(MagickFalse);
6555 (primitive_info+1)->primitive=primitive_info->primitive;
6556 primitive_info->coordinates=2;
6557 primitive_info->closed_subpath=MagickFalse;
6561static ssize_t TracePath(Image *image,MVGInfo *mvg_info,
const char *path)
6565 token[MaxTextExtent] =
"";
6583 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6606 subpath_offset=mvg_info->offset;
6607 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6610 number_coordinates=0;
6613 primitive_type=primitive_info->primitive;
6615 for (p=path; *p !=
'\0'; )
6617 if (status == MagickFalse)
6619 while (isspace((
int) ((
unsigned char) *p)) != 0)
6623 last_attribute=attribute;
6624 attribute=(int) (*p++);
6634 large_arc = MagickFalse,
6635 sweep = MagickFalse;
6645 (void) GetNextToken(p,&p,MaxTextExtent,token);
6646 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6647 ThrowPointExpectedException(image,token);
6649 (void) GetNextToken(p,&p,MaxTextExtent,token);
6650 arc.x=GetDrawValue(token,&next_token);
6651 if (token == next_token)
6652 ThrowPointExpectedException(image,token);
6653 (void) GetNextToken(p,&p,MaxTextExtent,token);
6654 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6655 ThrowPointExpectedException(image,token);
6657 (void) GetNextToken(p,&p,MaxTextExtent,token);
6658 arc.y=GetDrawValue(token,&next_token);
6659 if (token == next_token)
6660 ThrowPointExpectedException(image,token);
6661 (void) GetNextToken(p,&p,MaxTextExtent,token);
6662 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6663 ThrowPointExpectedException(image,token);
6665 (void) GetNextToken(p,&p,MaxTextExtent,token);
6666 angle=GetDrawValue(token,&next_token);
6667 if (token == next_token)
6668 ThrowPointExpectedException(image,token);
6669 (void) GetNextToken(p,&p,MaxTextExtent,token);
6670 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6671 ThrowPointExpectedException(image,token);
6673 (void) GetNextToken(p,&p,MaxTextExtent,token);
6674 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6675 (void) GetNextToken(p,&p,MaxTextExtent,token);
6676 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6677 ThrowPointExpectedException(image,token);
6679 (void) GetNextToken(p,&p,MaxTextExtent,token);
6680 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6681 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6682 ThrowPointExpectedException(image,token);
6684 (void) GetNextToken(p,&p,MaxTextExtent,token);
6685 (void) GetNextToken(p,&p,MaxTextExtent,token);
6686 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6687 ThrowPointExpectedException(image,token);
6689 (void) GetNextToken(p,&p,MaxTextExtent,token);
6690 x=GetDrawValue(token,&next_token);
6691 if (token == next_token)
6692 ThrowPointExpectedException(image,token);
6693 (void) GetNextToken(p,&p,MaxTextExtent,token);
6694 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6695 ThrowPointExpectedException(image,token);
6697 (void) GetNextToken(p,&p,MaxTextExtent,token);
6698 y=GetDrawValue(token,&next_token);
6699 if (token == next_token)
6700 ThrowPointExpectedException(image,token);
6701 end.x=(double) (attribute == (
int)
'A' ? x : point.x+x);
6702 end.y=(double) (attribute == (
int)
'A' ? y : point.y+y);
6703 status&=TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep);
6704 q=(*mvg_info->primitive_info)+mvg_info->offset;
6705 mvg_info->offset+=q->coordinates;
6706 q+=(ptrdiff_t) q->coordinates;
6708 while (isspace((
int) ((
unsigned char) *p)) != 0)
6712 }
while (IsValidPoint(p) != MagickFalse);
6724 for (i=1; i < 4; i++)
6726 (void) GetNextToken(p,&p,MaxTextExtent,token);
6727 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6728 ThrowPointExpectedException(image,token);
6730 (void) GetNextToken(p,&p,MaxTextExtent,token);
6731 x=GetDrawValue(token,&next_token);
6732 if (token == next_token)
6733 ThrowPointExpectedException(image,token);
6734 (void) GetNextToken(p,&p,MaxTextExtent,token);
6735 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6736 ThrowPointExpectedException(image,token);
6738 (void) GetNextToken(p,&p,MaxTextExtent,token);
6739 y=GetDrawValue(token,&next_token);
6740 if (token == next_token)
6741 ThrowPointExpectedException(image,token);
6742 end.x=(double) (attribute == (
int)
'C' ? x : point.x+x);
6743 end.y=(double) (attribute == (
int)
'C' ? y : point.y+y);
6746 for (i=0; i < 4; i++)
6747 (q+i)->point=points[i];
6748 if (TraceBezier(mvg_info,4) == MagickFalse)
6750 q=(*mvg_info->primitive_info)+mvg_info->offset;
6751 mvg_info->offset+=q->coordinates;
6752 q+=(ptrdiff_t) q->coordinates;
6754 while (isspace((
int) ((
unsigned char) *p)) != 0)
6758 }
while (IsValidPoint(p) != MagickFalse);
6766 (void) GetNextToken(p,&p,MaxTextExtent,token);
6767 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6768 ThrowPointExpectedException(image,token);
6770 (void) GetNextToken(p,&p,MaxTextExtent,token);
6771 x=GetDrawValue(token,&next_token);
6772 if (token == next_token)
6773 ThrowPointExpectedException(image,token);
6774 point.x=(double) (attribute == (
int)
'H' ? x: point.x+x);
6775 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6777 q=(*mvg_info->primitive_info)+mvg_info->offset;
6778 if (TracePoint(q,point) == MagickFalse)
6780 mvg_info->offset+=q->coordinates;
6781 q+=(ptrdiff_t) q->coordinates;
6782 while (isspace((
int) ((
unsigned char) *p)) != 0)
6786 }
while (IsValidPoint(p) != MagickFalse);
6797 (void) GetNextToken(p,&p,MaxTextExtent,token);
6798 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6799 ThrowPointExpectedException(image,token);
6801 (void) GetNextToken(p,&p,MaxTextExtent,token);
6802 x=GetDrawValue(token,&next_token);
6803 if (token == next_token)
6804 ThrowPointExpectedException(image,token);
6805 (void) GetNextToken(p,&p,MaxTextExtent,token);
6806 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6807 ThrowPointExpectedException(image,token);
6809 (void) GetNextToken(p,&p,MaxTextExtent,token);
6810 y=GetDrawValue(token,&next_token);
6811 if (token == next_token)
6812 ThrowPointExpectedException(image,token);
6813 point.x=(double) (attribute == (
int)
'L' ? x : point.x+x);
6814 point.y=(double) (attribute == (
int)
'L' ? y : point.y+y);
6815 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6817 q=(*mvg_info->primitive_info)+mvg_info->offset;
6818 if (TracePoint(q,point) == MagickFalse)
6820 mvg_info->offset+=q->coordinates;
6821 q+=(ptrdiff_t) q->coordinates;
6822 while (isspace((
int) ((
unsigned char) *p)) != 0)
6826 }
while (IsValidPoint(p) != MagickFalse);
6835 if (mvg_info->offset != subpath_offset)
6837 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6838 primitive_info->coordinates=(size_t) (q-primitive_info);
6839 number_coordinates+=primitive_info->coordinates;
6841 subpath_offset=mvg_info->offset;
6846 (void) GetNextToken(p,&p,MaxTextExtent,token);
6847 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6848 ThrowPointExpectedException(image,token);
6850 (void) GetNextToken(p,&p,MaxTextExtent,token);
6851 x=GetDrawValue(token,&next_token);
6852 if (token == next_token)
6853 ThrowPointExpectedException(image,token);
6854 (void) GetNextToken(p,&p,MaxTextExtent,token);
6855 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6856 ThrowPointExpectedException(image,token);
6858 (void) GetNextToken(p,&p,MaxTextExtent,token);
6859 y=GetDrawValue(token,&next_token);
6860 if (token == next_token)
6861 ThrowPointExpectedException(image,token);
6862 point.x=(double) (attribute == (
int)
'M' ? x : point.x+x);
6863 point.y=(double) (attribute == (
int)
'M' ? y : point.y+y);
6867 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6869 q=(*mvg_info->primitive_info)+mvg_info->offset;
6870 if (TracePoint(q,point) == MagickFalse)
6872 mvg_info->offset+=q->coordinates;
6873 q+=(ptrdiff_t) q->coordinates;
6874 while (isspace((
int) ((
unsigned char) *p)) != 0)
6878 }
while (IsValidPoint(p) != MagickFalse);
6890 for (i=1; i < 3; i++)
6892 (void) GetNextToken(p,&p,MaxTextExtent,token);
6893 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6894 ThrowPointExpectedException(image,token);
6896 (void) GetNextToken(p,&p,MaxTextExtent,token);
6897 x=GetDrawValue(token,&next_token);
6898 if (token == next_token)
6899 ThrowPointExpectedException(image,token);
6900 (void) GetNextToken(p,&p,MaxTextExtent,token);
6901 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6902 ThrowPointExpectedException(image,token);
6904 (void) GetNextToken(p,&p,MaxTextExtent,token);
6905 y=GetDrawValue(token,&next_token);
6906 if (token == next_token)
6907 ThrowPointExpectedException(image,token);
6910 end.x=(double) (attribute == (
int)
'Q' ? x : point.x+x);
6911 end.y=(double) (attribute == (
int)
'Q' ? y : point.y+y);
6914 for (i=0; i < 3; i++)
6915 (q+i)->point=points[i];
6916 if (TraceBezier(mvg_info,3) == MagickFalse)
6918 q=(*mvg_info->primitive_info)+mvg_info->offset;
6919 mvg_info->offset+=q->coordinates;
6920 q+=(ptrdiff_t) q->coordinates;
6922 while (isspace((
int) ((
unsigned char) *p)) != 0)
6926 }
while (IsValidPoint(p) != MagickFalse);
6937 points[0]=points[3];
6938 points[1].x=2.0*points[3].x-points[2].x;
6939 points[1].y=2.0*points[3].y-points[2].y;
6940 for (i=2; i < 4; i++)
6942 (void) GetNextToken(p,&p,MaxTextExtent,token);
6943 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6944 ThrowPointExpectedException(image,token);
6946 (void) GetNextToken(p,&p,MaxTextExtent,token);
6947 x=GetDrawValue(token,&next_token);
6948 if (token == next_token)
6949 ThrowPointExpectedException(image,token);
6950 (void) GetNextToken(p,&p,MaxTextExtent,token);
6951 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
6952 ThrowPointExpectedException(image,token);
6954 (void) GetNextToken(p,&p,MaxTextExtent,token);
6955 y=GetDrawValue(token,&next_token);
6956 if (token == next_token)
6957 ThrowPointExpectedException(image,token);
6960 end.x=(double) (attribute == (
int)
'S' ? x : point.x+x);
6961 end.y=(double) (attribute == (
int)
'S' ? y : point.y+y);
6964 if (strchr(
"CcSs",last_attribute) == (
char *) NULL)
6969 for (i=0; i < 4; i++)
6970 (q+i)->point=points[i];
6971 if (TraceBezier(mvg_info,4) == MagickFalse)
6973 q=(*mvg_info->primitive_info)+mvg_info->offset;
6974 mvg_info->offset+=q->coordinates;
6975 q+=(ptrdiff_t) q->coordinates;
6977 last_attribute=attribute;
6978 while (isspace((
int) ((
unsigned char) *p)) != 0)
6982 }
while (IsValidPoint(p) != MagickFalse);
6993 points[0]=points[2];
6994 points[1].x=2.0*points[2].x-points[1].x;
6995 points[1].y=2.0*points[2].y-points[1].y;
6996 for (i=2; i < 3; i++)
6998 (void) GetNextToken(p,&p,MaxTextExtent,token);
6999 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7000 ThrowPointExpectedException(image,token);
7002 (void) GetNextToken(p,&p,MaxTextExtent,token);
7003 x=GetDrawValue(token,&next_token);
7004 if (token == next_token)
7005 ThrowPointExpectedException(image,token);
7006 (void) GetNextToken(p,&p,MaxTextExtent,token);
7007 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7008 ThrowPointExpectedException(image,token);
7010 (void) GetNextToken(p,&p,MaxTextExtent,token);
7011 y=GetDrawValue(token,&next_token);
7012 if (token == next_token)
7013 ThrowPointExpectedException(image,token);
7014 end.x=(double) (attribute == (
int)
'T' ? x : point.x+x);
7015 end.y=(double) (attribute == (
int)
'T' ? y : point.y+y);
7018 if (status == MagickFalse)
7020 if (strchr(
"QqTt",last_attribute) == (
char *) NULL)
7025 for (i=0; i < 3; i++)
7026 (q+i)->point=points[i];
7027 if (TraceBezier(mvg_info,3) == MagickFalse)
7029 q=(*mvg_info->primitive_info)+mvg_info->offset;
7030 mvg_info->offset+=q->coordinates;
7031 q+=(ptrdiff_t) q->coordinates;
7033 last_attribute=attribute;
7034 while (isspace((
int) ((
unsigned char) *p)) != 0)
7038 }
while (IsValidPoint(p) != MagickFalse);
7049 (void) GetNextToken(p,&p,MaxTextExtent,token);
7050 if (IsValidListChar((
int) ((
unsigned char) *token)) == MagickFalse)
7051 ThrowPointExpectedException(image,token);
7053 (void) GetNextToken(p,&p,MaxTextExtent,token);
7054 y=GetDrawValue(token,&next_token);
7055 if (token == next_token)
7056 ThrowPointExpectedException(image,token);
7057 point.y=(double) (attribute == (
int)
'V' ? y : point.y+y);
7058 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7060 q=(*mvg_info->primitive_info)+mvg_info->offset;
7061 if (TracePoint(q,point) == MagickFalse)
7063 mvg_info->offset+=q->coordinates;
7064 q+=(ptrdiff_t) q->coordinates;
7065 while (isspace((
int) ((
unsigned char) *p)) != 0)
7069 }
while (IsValidPoint(p) != MagickFalse);
7079 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7081 q=(*mvg_info->primitive_info)+mvg_info->offset;
7082 if (TracePoint(q,point) == MagickFalse)
7084 mvg_info->offset+=q->coordinates;
7085 q+=(ptrdiff_t) q->coordinates;
7086 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7087 primitive_info->coordinates=(size_t) (q-primitive_info);
7088 primitive_info->closed_subpath=MagickTrue;
7089 number_coordinates+=primitive_info->coordinates;
7091 subpath_offset=mvg_info->offset;
7097 ThrowPointExpectedException(image,token);
7102 if (status == MagickFalse)
7104 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7105 primitive_info->coordinates=(size_t) (q-primitive_info);
7106 number_coordinates+=primitive_info->coordinates;
7107 for (i=0; i < (ssize_t) number_coordinates; i++)
7110 q->primitive=primitive_type;
7112 q->method=FillToBorderMethod;
7115 return((ssize_t) number_coordinates);
7118static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7119 const PointInfo start,
const PointInfo end)
7131 if (TracePoint(p,start) == MagickFalse)
7132 return(MagickFalse);
7133 p+=(ptrdiff_t) p->coordinates;
7136 if (TracePoint(p,point) == MagickFalse)
7137 return(MagickFalse);
7138 p+=(ptrdiff_t) p->coordinates;
7139 if (TracePoint(p,end) == MagickFalse)
7140 return(MagickFalse);
7141 p+=(ptrdiff_t) p->coordinates;
7144 if (TracePoint(p,point) == MagickFalse)
7145 return(MagickFalse);
7146 p+=(ptrdiff_t) p->coordinates;
7147 if (TracePoint(p,start) == MagickFalse)
7148 return(MagickFalse);
7149 p+=(ptrdiff_t) p->coordinates;
7150 primitive_info->coordinates=(size_t) (p-primitive_info);
7151 primitive_info->closed_subpath=MagickTrue;
7152 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7154 p->primitive=primitive_info->primitive;
7160static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7161 const PointInfo start,
const PointInfo end,PointInfo arc)
7180 offset=mvg_info->offset;
7181 segment.x=fabs(end.x-start.x);
7182 segment.y=fabs(end.y-start.y);
7183 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7185 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7188 if (arc.x > (0.5*segment.x))
7189 arc.x=0.5*segment.x;
7190 if (arc.y > (0.5*segment.y))
7191 arc.y=0.5*segment.y;
7192 point.x=start.x+segment.x-arc.x;
7193 point.y=start.y+arc.y;
7196 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7197 return(MagickFalse);
7198 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7199 mvg_info->offset+=p->coordinates;
7200 point.x=start.x+segment.x-arc.x;
7201 point.y=start.y+segment.y-arc.y;
7204 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7205 return(MagickFalse);
7206 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7207 mvg_info->offset+=p->coordinates;
7208 point.x=start.x+arc.x;
7209 point.y=start.y+segment.y-arc.y;
7212 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7213 return(MagickFalse);
7214 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7215 mvg_info->offset+=p->coordinates;
7216 point.x=start.x+arc.x;
7217 point.y=start.y+arc.y;
7220 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7221 return(MagickFalse);
7222 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7223 mvg_info->offset+=p->coordinates;
7224 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7225 return(MagickFalse);
7226 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7227 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7228 return(MagickFalse);
7229 p+=(ptrdiff_t) p->coordinates;
7230 mvg_info->offset=offset;
7231 primitive_info=(*mvg_info->primitive_info)+offset;
7232 primitive_info->coordinates=(size_t) (p-primitive_info);
7233 primitive_info->closed_subpath=MagickTrue;
7234 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7236 p->primitive=primitive_info->primitive;
7242static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7243 const size_t number_vertices,
const double offset)
7260 for (i=1; i < (ssize_t) number_vertices; i++)
7262 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7263 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7264 if ((fabs((
double) dx) >= MagickEpsilon) ||
7265 (fabs((
double) dy) >= MagickEpsilon))
7268 if (i == (ssize_t) number_vertices)
7269 i=(ssize_t) number_vertices-1L;
7270 distance=hypot((
double) dx,(
double) dy);
7271 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7272 dx*(distance+offset)/distance);
7273 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7274 dy*(distance+offset)/distance);
7275 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7277 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7278 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7279 if ((fabs((
double) dx) >= MagickEpsilon) ||
7280 (fabs((
double) dy) >= MagickEpsilon))
7283 distance=hypot((
double) dx,(
double) dy);
7284 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7285 dx*(distance+offset)/distance);
7286 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7287 dy*(distance+offset)/distance);
7291static PrimitiveInfo *TraceStrokePolygon(
const DrawInfo *draw_info,
7292 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7294#define MaxStrokePad (6*BezierQuantum+360)
7295#define CheckPathExtent(pad_p,pad_q) \
7297 if ((pad_p) > MaxBezierCoordinates) \
7298 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7300 if ((ssize_t) (p+(pad_p)) >= (ssize_t) extent_p) \
7302 if (~extent_p < (pad_p)) \
7303 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7306 extent_p+=(pad_p); \
7307 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7308 MaxStrokePad,sizeof(*stroke_p)); \
7311 if ((pad_q) > MaxBezierCoordinates) \
7312 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7314 if ((ssize_t) (q+(pad_q)) >= (ssize_t) extent_q) \
7316 if (~extent_q < (pad_q)) \
7317 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7320 extent_q+=(pad_q); \
7321 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7322 MaxStrokePad,sizeof(*stroke_q)); \
7325 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7327 if (stroke_p != (PointInfo *) NULL) \
7328 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7329 if (stroke_q != (PointInfo *) NULL) \
7330 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7331 polygon_primitive=(PrimitiveInfo *) \
7332 RelinquishMagickMemory(polygon_primitive); \
7333 (void) ThrowMagickException(exception,GetMagickModule(), \
7334 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7335 return((PrimitiveInfo *) NULL); \
7339 typedef struct _StrokeSegment
7385 inverse_slope = {0.0, 0.0},
7392 number_vertices=primitive_info->coordinates;
7393 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7394 number_vertices+2UL,
sizeof(*polygon_primitive));
7395 if (polygon_primitive == (PrimitiveInfo *) NULL)
7397 (void) ThrowMagickException(exception,GetMagickModule(),
7398 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7399 return((PrimitiveInfo *) NULL);
7401 (void) memcpy(polygon_primitive,primitive_info,(
size_t) number_vertices*
7402 sizeof(*polygon_primitive));
7403 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7404 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7405 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7406 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7407 if ((draw_info->linejoin == MiterJoin) ||
7408 ((draw_info->linejoin == RoundJoin) && (closed_path != MagickFalse)))
7410 polygon_primitive[number_vertices]=primitive_info[1];
7413 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7417 closed_path=primitive_info[0].closed_subpath;
7420 for (n=1; n < (ssize_t) number_vertices; n++)
7422 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7423 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7424 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7427 if (n == (ssize_t) number_vertices)
7429 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7434 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7435 sizeof(*stroke_polygon));
7436 stroke_polygon[0]=polygon_primitive[0];
7437 stroke_polygon[0].coordinates=0;
7438 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7440 return(stroke_polygon);
7442 n=(ssize_t) number_vertices-1L;
7444 extent_p=2*number_vertices;
7445 extent_q=2*number_vertices;
7446 stroke_p=(PointInfo *) AcquireQuantumMemory((
size_t) extent_p+MaxStrokePad,
7448 stroke_q=(PointInfo *) AcquireQuantumMemory((
size_t) extent_q+MaxStrokePad,
7450 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7452 if (stroke_p != (PointInfo *) NULL)
7453 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7454 if (stroke_q != (PointInfo *) NULL)
7455 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7456 polygon_primitive=(PrimitiveInfo *)
7457 RelinquishMagickMemory(polygon_primitive);
7458 (void) ThrowMagickException(exception,GetMagickModule(),
7459 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7460 return((PrimitiveInfo *) NULL);
7463 inverse_slope.p=0.0;
7464 if (fabs(dx.p) < MagickEpsilon)
7467 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7469 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7472 if (fabs(dy.p) < MagickEpsilon)
7475 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7477 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7482 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7484 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7485 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7486 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7487 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7488 offset.x=sqrt((
double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7489 offset.y=(double) (offset.x*inverse_slope.p);
7490 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7492 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7493 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7494 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7495 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7496 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7497 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7498 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7499 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7503 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7504 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7505 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7506 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7507 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7508 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7509 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7510 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7517 stroke_q[p++]=box_q[0];
7518 stroke_p[q++]=box_p[0];
7519 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7524 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7525 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7526 dot_product=dx.q*dx.q+dy.q*dy.q;
7527 if (dot_product < 0.25)
7530 inverse_slope.q=0.0;
7531 if (fabs(dx.q) < MagickEpsilon)
7534 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7536 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7539 if (fabs(dy.q) < MagickEpsilon)
7542 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7544 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7549 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7551 offset.x=sqrt((
double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7552 offset.y=(double) (offset.x*inverse_slope.q);
7553 dot_product=dy.q*offset.x-dx.q*offset.y;
7554 if (dot_product > 0.0)
7556 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7557 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7558 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7559 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7560 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7561 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7562 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7563 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7567 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7568 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7569 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7570 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7571 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7572 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7573 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7574 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7576 if (fabs((
double) (slope.p-slope.q)) < MagickEpsilon)
7583 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7584 box_p[3].y)/(slope.p-slope.q));
7585 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7586 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7587 box_q[3].y)/(slope.p-slope.q));
7588 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7590 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7591 dot_product=dx.q*dy.p-dx.p*dy.q;
7592 if (dot_product <= 0.0)
7593 switch (draw_info->linejoin)
7597 stroke_q[q++]=box_q[1];
7598 stroke_q[q++]=box_q[2];
7599 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7600 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7601 if (dot_product <= miterlimit)
7602 stroke_p[p++]=box_p[4];
7605 stroke_p[p++]=box_p[1];
7606 stroke_p[p++]=box_p[2];
7612 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7613 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7614 if (dot_product <= miterlimit)
7616 stroke_q[q++]=box_q[4];
7617 stroke_p[p++]=box_p[4];
7621 stroke_q[q++]=box_q[1];
7622 stroke_q[q++]=box_q[2];
7623 stroke_p[p++]=box_p[1];
7624 stroke_p[p++]=box_p[2];
7630 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7631 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7632 if (dot_product <= miterlimit)
7633 stroke_p[p++]=box_p[4];
7636 stroke_p[p++]=box_p[1];
7637 stroke_p[p++]=box_p[2];
7639 center=polygon_primitive[n].point;
7640 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7641 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7642 if (theta.q < theta.p)
7643 theta.q+=2.0*MagickPI;
7644 arc_segments=(size_t) CastDoubleToLong(ceil((
double) ((theta.q-
7645 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7646 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7647 stroke_q[q].x=box_q[1].x;
7648 stroke_q[q].y=box_q[1].y;
7650 for (j=1; j < (ssize_t) arc_segments; j++)
7652 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7653 stroke_q[q].x=(double) (center.x+mid*cos(fmod((
double)
7654 (theta.p+delta_theta),DegreesToRadians(360.0))));
7655 stroke_q[q].y=(double) (center.y+mid*sin(fmod((
double)
7656 (theta.p+delta_theta),DegreesToRadians(360.0))));
7659 stroke_q[q++]=box_q[2];
7666 switch (draw_info->linejoin)
7670 stroke_p[p++]=box_p[1];
7671 stroke_p[p++]=box_p[2];
7672 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7673 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7674 if (dot_product <= miterlimit)
7675 stroke_q[q++]=box_q[4];
7678 stroke_q[q++]=box_q[1];
7679 stroke_q[q++]=box_q[2];
7685 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7686 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7687 if (dot_product <= miterlimit)
7689 stroke_q[q++]=box_q[4];
7690 stroke_p[p++]=box_p[4];
7694 stroke_q[q++]=box_q[1];
7695 stroke_q[q++]=box_q[2];
7696 stroke_p[p++]=box_p[1];
7697 stroke_p[p++]=box_p[2];
7703 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7704 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7705 if (dot_product <= miterlimit)
7706 stroke_q[q++]=box_q[4];
7709 stroke_q[q++]=box_q[1];
7710 stroke_q[q++]=box_q[2];
7712 center=polygon_primitive[n].point;
7713 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7714 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7715 if (theta.p < theta.q)
7716 theta.p+=2.0*MagickPI;
7717 arc_segments=(size_t) CastDoubleToLong(ceil((
double) ((theta.p-
7718 theta.q)/(2.0*sqrt((
double) (MagickSafeReciprocal(mid)))))));
7719 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7720 stroke_p[p++]=box_p[1];
7721 for (j=1; j < (ssize_t) arc_segments; j++)
7723 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7724 stroke_p[p].x=(double) (center.x+mid*cos(fmod((
double)
7725 (theta.p+delta_theta),DegreesToRadians(360.0))));
7726 stroke_p[p].y=(double) (center.y+mid*sin(fmod((
double)
7727 (theta.p+delta_theta),DegreesToRadians(360.0))));
7730 stroke_p[p++]=box_p[2];
7737 inverse_slope.p=inverse_slope.q;
7746 stroke_p[p++]=box_p[1];
7747 stroke_q[q++]=box_q[1];
7751 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((
size_t)
7752 (p+q+2UL*closed_path+2UL),
sizeof(*stroke_polygon));
7753 if (stroke_polygon == (PrimitiveInfo *) NULL)
7755 (void) ThrowMagickException(exception,GetMagickModule(),
7756 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
"");
7757 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7758 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7759 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7761 return(stroke_polygon);
7763 for (i=0; i < (ssize_t) p; i++)
7765 stroke_polygon[i]=polygon_primitive[0];
7766 stroke_polygon[i].point=stroke_p[i];
7768 if (closed_path != MagickFalse)
7770 stroke_polygon[i]=polygon_primitive[0];
7771 stroke_polygon[i].point=stroke_polygon[0].point;
7774 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7776 stroke_polygon[i]=polygon_primitive[0];
7777 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7779 if (closed_path != MagickFalse)
7781 stroke_polygon[i]=polygon_primitive[0];
7782 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7785 stroke_polygon[i]=polygon_primitive[0];
7786 stroke_polygon[i].point=stroke_polygon[0].point;
7788 stroke_polygon[i].primitive=UndefinedPrimitive;
7789 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7790 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7791 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7792 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7793 return(stroke_polygon);