MagickCore 7.1.1-43
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
draw.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% DDDD RRRR AAA W W %
7% D D R R A A W W %
8% D D RRRR AAAAA W W W %
9% D D R RN A A WW WW %
10% DDDD R R A A W W %
11% %
12% %
13% MagickCore Image Drawing Methods %
14% %
15% %
16% Software Design %
17% Cristy %
18% July 1998 %
19% %
20% %
21% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
22% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% https://imagemagick.org/license/ %
28% %
29% Unless required by applicable law or agreed to in writing, software %
30% distributed under the License is distributed on an "AS IS" BASIS, %
31% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32% See the License for the specific language governing permissions and %
33% limitations under the License. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37% Bill Radcliffe of Corbis (www.corbis.com) contributed the polygon
38% rendering code based on Paul Heckbert's "Concave Polygon Scan Conversion",
39% Graphics Gems, 1990. Leonard Rosenthal and David Harr of Appligent
40% (www.appligent.com) contributed the dash pattern, linecap stroking
41% algorithm, and minor rendering improvements.
42%
43*/
44␌
45/*
46 Include declarations.
47*/
48#include "MagickCore/studio.h"
49#include "MagickCore/annotate.h"
50#include "MagickCore/artifact.h"
51#include "MagickCore/blob.h"
52#include "MagickCore/cache.h"
53#include "MagickCore/cache-private.h"
54#include "MagickCore/cache-view.h"
55#include "MagickCore/channel.h"
56#include "MagickCore/color.h"
57#include "MagickCore/colorspace-private.h"
58#include "MagickCore/composite.h"
59#include "MagickCore/composite-private.h"
60#include "MagickCore/constitute.h"
61#include "MagickCore/draw.h"
62#include "MagickCore/draw-private.h"
63#include "MagickCore/enhance.h"
64#include "MagickCore/exception.h"
65#include "MagickCore/exception-private.h"
66#include "MagickCore/gem.h"
67#include "MagickCore/geometry.h"
68#include "MagickCore/image-private.h"
69#include "MagickCore/list.h"
70#include "MagickCore/log.h"
71#include "MagickCore/magick.h"
72#include "MagickCore/memory-private.h"
73#include "MagickCore/monitor.h"
74#include "MagickCore/monitor-private.h"
75#include "MagickCore/option.h"
76#include "MagickCore/paint.h"
77#include "MagickCore/pixel-accessor.h"
78#include "MagickCore/property.h"
79#include "MagickCore/resample.h"
80#include "MagickCore/resample-private.h"
81#include "MagickCore/resource_.h"
82#include "MagickCore/splay-tree.h"
83#include "MagickCore/string_.h"
84#include "MagickCore/string-private.h"
85#include "MagickCore/thread-private.h"
86#include "MagickCore/token.h"
87#include "MagickCore/transform-private.h"
88#include "MagickCore/utility.h"
89␌
90/*
91 Define declarations.
92*/
93#define AntialiasThreshold (1.0/3.0)
94#define BezierQuantum 200
95#define PrimitiveExtentPad 4296.0
96#define MaxBezierCoordinates 67108864
97#define MacroExpansionLimit 262144
98#define ThrowPointExpectedException(token,exception) \
99{ \
100 (void) ThrowMagickException(exception,GetMagickModule(),DrawError, \
101 "NonconformingDrawingPrimitiveDefinition","`%s'",token); \
102 status=MagickFalse; \
103 break; \
104}
105
106
107static inline float MagickSafeReciprocal(const float x) {
108 return PerceptibleReciprocal(x);
109}
110␌
111/*
112 Typedef declarations.
113*/
114typedef struct _EdgeInfo
115{
117 bounds;
118
119 double
120 scanline;
121
123 *points;
124
125 size_t
126 number_points;
127
128 ssize_t
129 direction;
130
131 MagickBooleanType
132 ghostline;
133
134 size_t
135 highwater;
136} EdgeInfo;
137
138typedef struct _ElementInfo
139{
140 double
141 cx,
142 cy,
143 major,
144 minor,
145 angle;
147
148typedef struct _MVGInfo
149{
151 **primitive_info;
152
153 size_t
154 *extent;
155
156 ssize_t
157 offset;
158
160 point;
161
163 *exception;
164} MVGInfo;
165
166typedef struct _PolygonInfo
167{
169 *edges;
170
171 size_t
172 number_edges;
174
175typedef enum
176{
177 MoveToCode,
178 OpenCode,
179 GhostlineCode,
180 LineToCode,
181 EndCode
182} PathInfoCode;
183
184typedef struct _PathInfo
185{
187 point;
188
189 PathInfoCode
190 code;
191} PathInfo;
192␌
193/*
194 Forward declarations.
195*/
196static Image
197 *DrawClippingMask(Image *,const DrawInfo *,const char *,const char *,
198 ExceptionInfo *);
199
200static MagickBooleanType
201 DrawStrokePolygon(Image *,const DrawInfo *,const PrimitiveInfo *,
202 ExceptionInfo *),
203 RenderMVGContent(Image *,const DrawInfo *,const size_t,ExceptionInfo *),
204 TraceArc(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
205 TraceArcPath(MVGInfo *,const PointInfo,const PointInfo,const PointInfo,
206 const double,const MagickBooleanType,const MagickBooleanType),
207 TraceBezier(MVGInfo *,const size_t),
208 TraceCircle(MVGInfo *,const PointInfo,const PointInfo),
209 TraceEllipse(MVGInfo *,const PointInfo,const PointInfo,const PointInfo),
210 TraceLine(PrimitiveInfo *,const PointInfo,const PointInfo),
211 TraceRectangle(PrimitiveInfo *,const PointInfo,const PointInfo),
212 TraceRoundRectangle(MVGInfo *,const PointInfo,const PointInfo,PointInfo),
213 TraceSquareLinecap(PrimitiveInfo *,const size_t,const double);
214
215static PrimitiveInfo
216 *TraceStrokePolygon(const DrawInfo *,const PrimitiveInfo *,ExceptionInfo *);
217
218static ssize_t
219 TracePath(MVGInfo *,const char *,ExceptionInfo *);
220␌
221/*
222%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223% %
224% %
225% %
226% A c q u i r e D r a w I n f o %
227% %
228% %
229% %
230%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231%
232% AcquireDrawInfo() returns a DrawInfo structure properly initialized.
233%
234% The format of the AcquireDrawInfo method is:
235%
236% DrawInfo *AcquireDrawInfo(void)
237%
238*/
239MagickExport DrawInfo *AcquireDrawInfo(void)
240{
242 *draw_info;
243
244 draw_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*draw_info));
245 GetDrawInfo((ImageInfo *) NULL,draw_info);
246 return(draw_info);
247}
248␌
249/*
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251% %
252% %
253% %
254% C l o n e D r a w I n f o %
255% %
256% %
257% %
258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259%
260% CloneDrawInfo() makes a copy of the given draw_info structure. If NULL
261% is specified, a new DrawInfo structure is created initialized to default
262% values.
263%
264% The format of the CloneDrawInfo method is:
265%
266% DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
267% const DrawInfo *draw_info)
268%
269% A description of each parameter follows:
270%
271% o image_info: the image info.
272%
273% o draw_info: the draw info.
274%
275*/
276MagickExport DrawInfo *CloneDrawInfo(const ImageInfo *image_info,
277 const DrawInfo *draw_info)
278{
280 *clone_info;
281
283 *exception;
284
285 clone_info=(DrawInfo *) AcquireCriticalMemory(sizeof(*clone_info));
286 GetDrawInfo(image_info,clone_info);
287 if (draw_info == (DrawInfo *) NULL)
288 return(clone_info);
289 exception=AcquireExceptionInfo();
290 if (draw_info->id != (char *) NULL)
291 (void) CloneString(&clone_info->id,draw_info->id);
292 if (draw_info->primitive != (char *) NULL)
293 (void) CloneString(&clone_info->primitive,draw_info->primitive);
294 if (draw_info->geometry != (char *) NULL)
295 (void) CloneString(&clone_info->geometry,draw_info->geometry);
296 clone_info->compliance=draw_info->compliance;
297 clone_info->viewbox=draw_info->viewbox;
298 clone_info->affine=draw_info->affine;
299 clone_info->gravity=draw_info->gravity;
300 clone_info->fill=draw_info->fill;
301 clone_info->stroke=draw_info->stroke;
302 clone_info->stroke_width=draw_info->stroke_width;
303 if (draw_info->fill_pattern != (Image *) NULL)
304 clone_info->fill_pattern=CloneImage(draw_info->fill_pattern,0,0,MagickTrue,
305 exception);
306 if (draw_info->stroke_pattern != (Image *) NULL)
307 clone_info->stroke_pattern=CloneImage(draw_info->stroke_pattern,0,0,
308 MagickTrue,exception);
309 clone_info->stroke_antialias=draw_info->stroke_antialias;
310 clone_info->text_antialias=draw_info->text_antialias;
311 clone_info->fill_rule=draw_info->fill_rule;
312 clone_info->linecap=draw_info->linecap;
313 clone_info->linejoin=draw_info->linejoin;
314 clone_info->miterlimit=draw_info->miterlimit;
315 clone_info->dash_offset=draw_info->dash_offset;
316 clone_info->decorate=draw_info->decorate;
317 clone_info->compose=draw_info->compose;
318 if (draw_info->text != (char *) NULL)
319 (void) CloneString(&clone_info->text,draw_info->text);
320 if (draw_info->font != (char *) NULL)
321 (void) CloneString(&clone_info->font,draw_info->font);
322 if (draw_info->metrics != (char *) NULL)
323 (void) CloneString(&clone_info->metrics,draw_info->metrics);
324 if (draw_info->family != (char *) NULL)
325 (void) CloneString(&clone_info->family,draw_info->family);
326 clone_info->style=draw_info->style;
327 clone_info->stretch=draw_info->stretch;
328 clone_info->weight=draw_info->weight;
329 if (draw_info->encoding != (char *) NULL)
330 (void) CloneString(&clone_info->encoding,draw_info->encoding);
331 clone_info->pointsize=draw_info->pointsize;
332 clone_info->kerning=draw_info->kerning;
333 clone_info->interline_spacing=draw_info->interline_spacing;
334 clone_info->interword_spacing=draw_info->interword_spacing;
335 clone_info->direction=draw_info->direction;
336 clone_info->word_break=draw_info->word_break;
337 if (draw_info->density != (char *) NULL)
338 (void) CloneString(&clone_info->density,draw_info->density);
339 clone_info->align=draw_info->align;
340 clone_info->undercolor=draw_info->undercolor;
341 clone_info->border_color=draw_info->border_color;
342 if (draw_info->server_name != (char *) NULL)
343 (void) CloneString(&clone_info->server_name,draw_info->server_name);
344 if (draw_info->dash_pattern != (double *) NULL)
345 {
346 ssize_t
347 x;
348
349 for (x=0; fabs(draw_info->dash_pattern[x]) >= MagickEpsilon; x++) ;
350 clone_info->dash_pattern=(double *) AcquireQuantumMemory((size_t) (x+1),
351 sizeof(*clone_info->dash_pattern));
352 if (clone_info->dash_pattern == (double *) NULL)
353 ThrowFatalException(ResourceLimitFatalError,
354 "UnableToAllocateDashPattern");
355 (void) memset(clone_info->dash_pattern,0,(size_t) (x+1)*
356 sizeof(*clone_info->dash_pattern));
357 (void) memcpy(clone_info->dash_pattern,draw_info->dash_pattern,(size_t)
358 x*sizeof(*clone_info->dash_pattern));
359 }
360 clone_info->gradient=draw_info->gradient;
361 if (draw_info->gradient.stops != (StopInfo *) NULL)
362 {
363 size_t
364 number_stops;
365
366 number_stops=clone_info->gradient.number_stops;
367 clone_info->gradient.stops=(StopInfo *) AcquireQuantumMemory((size_t)
368 number_stops,sizeof(*clone_info->gradient.stops));
369 if (clone_info->gradient.stops == (StopInfo *) NULL)
370 ThrowFatalException(ResourceLimitFatalError,
371 "UnableToAllocateDashPattern");
372 (void) memcpy(clone_info->gradient.stops,draw_info->gradient.stops,
373 (size_t) number_stops*sizeof(*clone_info->gradient.stops));
374 }
375 clone_info->bounds=draw_info->bounds;
376 clone_info->fill_alpha=draw_info->fill_alpha;
377 clone_info->stroke_alpha=draw_info->stroke_alpha;
378 clone_info->element_reference=draw_info->element_reference;
379 clone_info->clip_path=draw_info->clip_path;
380 clone_info->clip_units=draw_info->clip_units;
381 if (draw_info->clip_mask != (char *) NULL)
382 (void) CloneString(&clone_info->clip_mask,draw_info->clip_mask);
383 if (draw_info->clipping_mask != (Image *) NULL)
384 clone_info->clipping_mask=CloneImage(draw_info->clipping_mask,0,0,
385 MagickTrue,exception);
386 if (draw_info->composite_mask != (Image *) NULL)
387 clone_info->composite_mask=CloneImage(draw_info->composite_mask,0,0,
388 MagickTrue,exception);
389 clone_info->render=draw_info->render;
390 clone_info->debug=draw_info->debug;
391 clone_info->macro_expansion=draw_info->macro_expansion;
392 exception=DestroyExceptionInfo(exception);
393 return(clone_info);
394}
395␌
396/*
397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398% %
399% %
400% %
401+ C o n v e r t P a t h T o P o l y g o n %
402% %
403% %
404% %
405%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406%
407% ConvertPathToPolygon() converts a path to the more efficient sorted
408% rendering form.
409%
410% The format of the ConvertPathToPolygon method is:
411%
412% PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info,
413% ExceptionInfo *exception)
414%
415% A description of each parameter follows:
416%
417% o ConvertPathToPolygon() returns the path in a more efficient sorted
418% rendering form of type PolygonInfo.
419%
420% o draw_info: Specifies a pointer to an DrawInfo structure.
421%
422% o path_info: Specifies a pointer to an PathInfo structure.
423%
424%
425*/
426
427static PolygonInfo *DestroyPolygonInfo(PolygonInfo *polygon_info)
428{
429 ssize_t
430 i;
431
432 if (polygon_info->edges != (EdgeInfo *) NULL)
433 {
434 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
435 if (polygon_info->edges[i].points != (PointInfo *) NULL)
436 polygon_info->edges[i].points=(PointInfo *)
437 RelinquishMagickMemory(polygon_info->edges[i].points);
438 polygon_info->edges=(EdgeInfo *) RelinquishMagickMemory(
439 polygon_info->edges);
440 }
441 return((PolygonInfo *) RelinquishMagickMemory(polygon_info));
442}
443#if defined(__cplusplus) || defined(c_plusplus)
444extern "C" {
445#endif
446
447static int DrawCompareEdges(const void *p_edge,const void *q_edge)
448{
449#define DrawCompareEdge(p,q) \
450{ \
451 if (((p)-(q)) < 0.0) \
452 return(-1); \
453 if (((p)-(q)) > 0.0) \
454 return(1); \
455}
456
457 const PointInfo
458 *p,
459 *q;
460
461 /*
462 Edge sorting for right-handed coordinate system.
463 */
464 p=((const EdgeInfo *) p_edge)->points;
465 q=((const EdgeInfo *) q_edge)->points;
466 DrawCompareEdge(p[0].y,q[0].y);
467 DrawCompareEdge(p[0].x,q[0].x);
468 DrawCompareEdge((p[1].x-p[0].x)*(q[1].y-q[0].y),(p[1].y-p[0].y)*
469 (q[1].x-q[0].x));
470 DrawCompareEdge(p[1].y,q[1].y);
471 DrawCompareEdge(p[1].x,q[1].x);
472 return(0);
473}
474
475#if defined(__cplusplus) || defined(c_plusplus)
476}
477#endif
478
479static void LogPolygonInfo(const PolygonInfo *polygon_info)
480{
482 *p;
483
484 ssize_t
485 i,
486 j;
487
488 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin active-edge");
489 p=polygon_info->edges;
490 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
491 {
492 (void) LogMagickEvent(DrawEvent,GetMagickModule()," edge %.20g:",
493 (double) i);
494 (void) LogMagickEvent(DrawEvent,GetMagickModule()," direction: %s",
495 p->direction != MagickFalse ? "down" : "up");
496 (void) LogMagickEvent(DrawEvent,GetMagickModule()," ghostline: %s",
497 p->ghostline != MagickFalse ? "transparent" : "opaque");
498 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
499 " bounds: %g,%g - %g,%g",p->bounds.x1,p->bounds.y1,
500 p->bounds.x2,p->bounds.y2);
501 for (j=0; j < (ssize_t) p->number_points; j++)
502 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %g,%g",
503 p->points[j].x,p->points[j].y);
504 p++;
505 }
506 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end active-edge");
507}
508
509static void ReversePoints(PointInfo *points,const size_t number_points)
510{
512 point;
513
514 size_t
515 i;
516
517 for (i=0; i < (number_points >> 1); i++)
518 {
519 point=points[i];
520 points[i]=points[number_points-(i+1)];
521 points[number_points-(i+1)]=point;
522 }
523}
524
525static PolygonInfo *ConvertPathToPolygon(const PathInfo *path_info,
526 ExceptionInfo *exception)
527{
528 long
529 direction,
530 next_direction;
531
533 point,
534 *points;
535
537 *polygon_info;
538
540 bounds;
541
542 ssize_t
543 i,
544 n;
545
546 MagickBooleanType
547 ghostline;
548
549 size_t
550 edge,
551 number_edges,
552 number_points;
553
554 /*
555 Convert a path to the more efficient sorted rendering form.
556 */
557 polygon_info=(PolygonInfo *) AcquireMagickMemory(sizeof(*polygon_info));
558 if (polygon_info == (PolygonInfo *) NULL)
559 {
560 (void) ThrowMagickException(exception,GetMagickModule(),
561 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
562 return((PolygonInfo *) NULL);
563 }
564 number_edges=16;
565 polygon_info->edges=(EdgeInfo *) AcquireQuantumMemory(number_edges,
566 sizeof(*polygon_info->edges));
567 if (polygon_info->edges == (EdgeInfo *) NULL)
568 {
569 (void) ThrowMagickException(exception,GetMagickModule(),
570 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
571 return(DestroyPolygonInfo(polygon_info));
572 }
573 (void) memset(polygon_info->edges,0,number_edges*
574 sizeof(*polygon_info->edges));
575 direction=0;
576 edge=0;
577 ghostline=MagickFalse;
578 n=0;
579 number_points=0;
580 points=(PointInfo *) NULL;
581 (void) memset(&point,0,sizeof(point));
582 (void) memset(&bounds,0,sizeof(bounds));
583 polygon_info->edges[edge].number_points=(size_t) n;
584 polygon_info->edges[edge].scanline=0.0;
585 polygon_info->edges[edge].highwater=0;
586 polygon_info->edges[edge].ghostline=ghostline;
587 polygon_info->edges[edge].direction=(ssize_t) direction;
588 polygon_info->edges[edge].points=points;
589 polygon_info->edges[edge].bounds=bounds;
590 polygon_info->number_edges=0;
591 for (i=0; path_info[i].code != EndCode; i++)
592 {
593 if ((path_info[i].code == MoveToCode) || (path_info[i].code == OpenCode) ||
594 (path_info[i].code == GhostlineCode))
595 {
596 /*
597 Move to.
598 */
599 if ((points != (PointInfo *) NULL) && (n >= 2))
600 {
601 if (edge == number_edges)
602 {
603 number_edges<<=1;
604 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
605 polygon_info->edges,(size_t) number_edges,
606 sizeof(*polygon_info->edges));
607 if (polygon_info->edges == (EdgeInfo *) NULL)
608 {
609 (void) ThrowMagickException(exception,GetMagickModule(),
610 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
611 points=(PointInfo *) RelinquishMagickMemory(points);
612 return(DestroyPolygonInfo(polygon_info));
613 }
614 }
615 polygon_info->edges[edge].number_points=(size_t) n;
616 polygon_info->edges[edge].scanline=(-1.0);
617 polygon_info->edges[edge].highwater=0;
618 polygon_info->edges[edge].ghostline=ghostline;
619 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
620 if (direction < 0)
621 ReversePoints(points,(size_t) n);
622 polygon_info->edges[edge].points=points;
623 polygon_info->edges[edge].bounds=bounds;
624 polygon_info->edges[edge].bounds.y1=points[0].y;
625 polygon_info->edges[edge].bounds.y2=points[n-1].y;
626 points=(PointInfo *) NULL;
627 ghostline=MagickFalse;
628 edge++;
629 polygon_info->number_edges=edge;
630 }
631 if (points == (PointInfo *) NULL)
632 {
633 number_points=16;
634 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
635 sizeof(*points));
636 if (points == (PointInfo *) NULL)
637 {
638 (void) ThrowMagickException(exception,GetMagickModule(),
639 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
640 return(DestroyPolygonInfo(polygon_info));
641 }
642 }
643 ghostline=path_info[i].code == GhostlineCode ? MagickTrue : MagickFalse;
644 point=path_info[i].point;
645 points[0]=point;
646 bounds.x1=point.x;
647 bounds.x2=point.x;
648 direction=0;
649 n=1;
650 continue;
651 }
652 /*
653 Line to.
654 */
655 next_direction=((path_info[i].point.y > point.y) ||
656 ((fabs(path_info[i].point.y-point.y) < MagickEpsilon) &&
657 (path_info[i].point.x > point.x))) ? 1 : -1;
658 if ((points != (PointInfo *) NULL) && (direction != 0) &&
659 (direction != next_direction))
660 {
661 /*
662 New edge.
663 */
664 point=points[n-1];
665 if (edge == number_edges)
666 {
667 number_edges<<=1;
668 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
669 polygon_info->edges,(size_t) number_edges,
670 sizeof(*polygon_info->edges));
671 if (polygon_info->edges == (EdgeInfo *) NULL)
672 {
673 (void) ThrowMagickException(exception,GetMagickModule(),
674 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
675 points=(PointInfo *) RelinquishMagickMemory(points);
676 return(DestroyPolygonInfo(polygon_info));
677 }
678 }
679 polygon_info->edges[edge].number_points=(size_t) n;
680 polygon_info->edges[edge].scanline=(-1.0);
681 polygon_info->edges[edge].highwater=0;
682 polygon_info->edges[edge].ghostline=ghostline;
683 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
684 if (direction < 0)
685 ReversePoints(points,(size_t) n);
686 polygon_info->edges[edge].points=points;
687 polygon_info->edges[edge].bounds=bounds;
688 polygon_info->edges[edge].bounds.y1=points[0].y;
689 polygon_info->edges[edge].bounds.y2=points[n-1].y;
690 polygon_info->number_edges=edge+1;
691 points=(PointInfo *) NULL;
692 number_points=16;
693 points=(PointInfo *) AcquireQuantumMemory((size_t) number_points,
694 sizeof(*points));
695 if (points == (PointInfo *) NULL)
696 {
697 (void) ThrowMagickException(exception,GetMagickModule(),
698 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
699 return(DestroyPolygonInfo(polygon_info));
700 }
701 n=1;
702 ghostline=MagickFalse;
703 points[0]=point;
704 bounds.x1=point.x;
705 bounds.x2=point.x;
706 edge++;
707 }
708 direction=next_direction;
709 if (points == (PointInfo *) NULL)
710 continue;
711 if (n == (ssize_t) number_points)
712 {
713 number_points<<=1;
714 points=(PointInfo *) ResizeQuantumMemory(points,(size_t) number_points,
715 sizeof(*points));
716 if (points == (PointInfo *) NULL)
717 {
718 (void) ThrowMagickException(exception,GetMagickModule(),
719 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
720 return(DestroyPolygonInfo(polygon_info));
721 }
722 }
723 point=path_info[i].point;
724 points[n]=point;
725 if (point.x < bounds.x1)
726 bounds.x1=point.x;
727 if (point.x > bounds.x2)
728 bounds.x2=point.x;
729 n++;
730 }
731 if (points != (PointInfo *) NULL)
732 {
733 if (n < 2)
734 points=(PointInfo *) RelinquishMagickMemory(points);
735 else
736 {
737 if (edge == number_edges)
738 {
739 number_edges<<=1;
740 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(
741 polygon_info->edges,(size_t) number_edges,
742 sizeof(*polygon_info->edges));
743 if (polygon_info->edges == (EdgeInfo *) NULL)
744 {
745 (void) ThrowMagickException(exception,GetMagickModule(),
746 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
747 return(DestroyPolygonInfo(polygon_info));
748 }
749 }
750 polygon_info->edges[edge].number_points=(size_t) n;
751 polygon_info->edges[edge].scanline=(-1.0);
752 polygon_info->edges[edge].highwater=0;
753 polygon_info->edges[edge].ghostline=ghostline;
754 polygon_info->edges[edge].direction=(ssize_t) (direction > 0);
755 if (direction < 0)
756 ReversePoints(points,(size_t) n);
757 polygon_info->edges[edge].points=points;
758 polygon_info->edges[edge].bounds=bounds;
759 polygon_info->edges[edge].bounds.y1=points[0].y;
760 polygon_info->edges[edge].bounds.y2=points[n-1].y;
761 points=(PointInfo *) NULL;
762 ghostline=MagickFalse;
763 edge++;
764 polygon_info->number_edges=edge;
765 }
766 }
767 polygon_info->number_edges=edge;
768 polygon_info->edges=(EdgeInfo *) ResizeQuantumMemory(polygon_info->edges,
769 polygon_info->number_edges,sizeof(*polygon_info->edges));
770 if (polygon_info->edges == (EdgeInfo *) NULL)
771 {
772 (void) ThrowMagickException(exception,GetMagickModule(),
773 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
774 return(DestroyPolygonInfo(polygon_info));
775 }
776 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
777 {
779 *edge_info;
780
781 edge_info=polygon_info->edges+i;
782 edge_info->points=(PointInfo *) ResizeQuantumMemory(edge_info->points,
783 edge_info->number_points,sizeof(*edge_info->points));
784 if (edge_info->points == (PointInfo *) NULL)
785 {
786 (void) ThrowMagickException(exception,GetMagickModule(),
787 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
788 return(DestroyPolygonInfo(polygon_info));
789 }
790 }
791 qsort(polygon_info->edges,(size_t) polygon_info->number_edges,
792 sizeof(*polygon_info->edges),DrawCompareEdges);
793 if ((GetLogEventMask() & DrawEvent) != 0)
794 LogPolygonInfo(polygon_info);
795 return(polygon_info);
796}
797␌
798/*
799%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800% %
801% %
802% %
803+ C o n v e r t P r i m i t i v e T o P a t h %
804% %
805% %
806% %
807%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
808%
809% ConvertPrimitiveToPath() converts a PrimitiveInfo structure into a vector
810% path structure.
811%
812% The format of the ConvertPrimitiveToPath method is:
813%
814% PathInfo *ConvertPrimitiveToPath(const DrawInfo *draw_info,
815% const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
816%
817% A description of each parameter follows:
818%
819% o ConvertPrimitiveToPath() returns a vector path structure of type
820% PathInfo.
821%
822% o draw_info: a structure of type DrawInfo.
823%
824% o primitive_info: Specifies a pointer to an PrimitiveInfo structure.
825%
826*/
827
828static void LogPathInfo(const PathInfo *path_info)
829{
830 const PathInfo
831 *p;
832
833 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin vector-path");
834 for (p=path_info; p->code != EndCode; p++)
835 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
836 " %g,%g %s",p->point.x,p->point.y,p->code == GhostlineCode ?
837 "moveto ghostline" : p->code == OpenCode ? "moveto open" :
838 p->code == MoveToCode ? "moveto" : p->code == LineToCode ? "lineto" :
839 "?");
840 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end vector-path");
841}
842
843static PathInfo *ConvertPrimitiveToPath(const PrimitiveInfo *primitive_info,
844 ExceptionInfo *exception)
845{
846 MagickBooleanType
847 closed_subpath;
848
850 *path_info;
851
852 PathInfoCode
853 code;
854
856 p,
857 q;
858
859 ssize_t
860 n,
861 start;
862
863 size_t
864 coordinates,
865 i;
866
867 /*
868 Converts a PrimitiveInfo structure into a vector path structure.
869 */
870 switch (primitive_info->primitive)
871 {
872 case AlphaPrimitive:
873 case ColorPrimitive:
874 case ImagePrimitive:
875 case PointPrimitive:
876 case TextPrimitive:
877 return((PathInfo *) NULL);
878 default:
879 break;
880 }
881 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
882 path_info=(PathInfo *) AcquireQuantumMemory((size_t) (3UL*i+1UL),
883 sizeof(*path_info));
884 if (path_info == (PathInfo *) NULL)
885 {
886 (void) ThrowMagickException(exception,GetMagickModule(),
887 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
888 return((PathInfo *) NULL);
889 }
890 coordinates=0;
891 closed_subpath=MagickFalse;
892 n=0;
893 p.x=(-1.0);
894 p.y=(-1.0);
895 q.x=(-1.0);
896 q.y=(-1.0);
897 start=0;
898 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
899 {
900 code=LineToCode;
901 if (coordinates <= 0)
902 {
903 /*
904 New subpath.
905 */
906 coordinates=primitive_info[i].coordinates;
907 p=primitive_info[i].point;
908 start=n;
909 code=MoveToCode;
910 closed_subpath=primitive_info[i].closed_subpath;
911 }
912 coordinates--;
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))
916 {
917 /*
918 Eliminate duplicate points.
919 */
920 path_info[n].code=code;
921 path_info[n].point=primitive_info[i].point;
922 q=primitive_info[i].point;
923 n++;
924 }
925 if (coordinates > 0)
926 continue; /* next point in current subpath */
927 if (closed_subpath != MagickFalse)
928 {
929 closed_subpath=MagickFalse;
930 continue;
931 }
932 /*
933 Mark the p point as open if the subpath is not closed.
934 */
935 path_info[start].code=OpenCode;
936 path_info[n].code=GhostlineCode;
937 path_info[n].point=primitive_info[i].point;
938 n++;
939 path_info[n].code=LineToCode;
940 path_info[n].point=p;
941 n++;
942 }
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),
949 sizeof(*path_info));
950 return(path_info);
951}
952␌
953/*
954%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955% %
956% %
957% %
958% D e s t r o y D r a w I n f o %
959% %
960% %
961% %
962%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963%
964% DestroyDrawInfo() deallocates memory associated with an DrawInfo structure.
965%
966% The format of the DestroyDrawInfo method is:
967%
968% DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
969%
970% A description of each parameter follows:
971%
972% o draw_info: the draw info.
973%
974*/
975MagickExport DrawInfo *DestroyDrawInfo(DrawInfo *draw_info)
976{
977 assert(draw_info != (DrawInfo *) NULL);
978 assert(draw_info->signature == MagickCoreSignature);
979 if (IsEventLogging() != MagickFalse)
980 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
981 if (draw_info->id != (char *) NULL)
982 draw_info->id=DestroyString(draw_info->id);
983 if (draw_info->primitive != (char *) NULL)
984 draw_info->primitive=DestroyString(draw_info->primitive);
985 if (draw_info->text != (char *) NULL)
986 draw_info->text=DestroyString(draw_info->text);
987 if (draw_info->geometry != (char *) NULL)
988 draw_info->geometry=DestroyString(draw_info->geometry);
989 if (draw_info->fill_pattern != (Image *) NULL)
990 draw_info->fill_pattern=DestroyImage(draw_info->fill_pattern);
991 if (draw_info->stroke_pattern != (Image *) NULL)
992 draw_info->stroke_pattern=DestroyImage(draw_info->stroke_pattern);
993 if (draw_info->font != (char *) NULL)
994 draw_info->font=DestroyString(draw_info->font);
995 if (draw_info->metrics != (char *) NULL)
996 draw_info->metrics=DestroyString(draw_info->metrics);
997 if (draw_info->family != (char *) NULL)
998 draw_info->family=DestroyString(draw_info->family);
999 if (draw_info->encoding != (char *) NULL)
1000 draw_info->encoding=DestroyString(draw_info->encoding);
1001 if (draw_info->density != (char *) NULL)
1002 draw_info->density=DestroyString(draw_info->density);
1003 if (draw_info->server_name != (char *) NULL)
1004 draw_info->server_name=(char *)
1005 RelinquishMagickMemory(draw_info->server_name);
1006 if (draw_info->dash_pattern != (double *) NULL)
1007 draw_info->dash_pattern=(double *) RelinquishMagickMemory(
1008 draw_info->dash_pattern);
1009 if (draw_info->gradient.stops != (StopInfo *) NULL)
1010 draw_info->gradient.stops=(StopInfo *) RelinquishMagickMemory(
1011 draw_info->gradient.stops);
1012 if (draw_info->clip_mask != (char *) NULL)
1013 draw_info->clip_mask=DestroyString(draw_info->clip_mask);
1014 if (draw_info->clipping_mask != (Image *) NULL)
1015 draw_info->clipping_mask=DestroyImage(draw_info->clipping_mask);
1016 if (draw_info->composite_mask != (Image *) NULL)
1017 draw_info->composite_mask=DestroyImage(draw_info->composite_mask);
1018 if (draw_info->image_info != (ImageInfo *) NULL)
1019 draw_info->image_info=DestroyImageInfo(draw_info->image_info);
1020 draw_info->signature=(~MagickCoreSignature);
1021 draw_info=(DrawInfo *) RelinquishMagickMemory(draw_info);
1022 return(draw_info);
1023}
1024␌
1025/*
1026%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027% %
1028% %
1029% %
1030% D r a w A f f i n e I m a g e %
1031% %
1032% %
1033% %
1034%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1035%
1036% DrawAffineImage() composites the source over the destination image as
1037% dictated by the affine transform.
1038%
1039% The format of the DrawAffineImage method is:
1040%
1041% MagickBooleanType DrawAffineImage(Image *image,const Image *source,
1042% const AffineMatrix *affine,ExceptionInfo *exception)
1043%
1044% A description of each parameter follows:
1045%
1046% o image: the image.
1047%
1048% o source: the source image.
1049%
1050% o affine: the affine transform.
1051%
1052% o exception: return any errors or warnings in this structure.
1053%
1054*/
1055
1056static SegmentInfo AffineEdge(const Image *image,const AffineMatrix *affine,
1057 const double y,const SegmentInfo *edge)
1058{
1059 double
1060 intercept,
1061 z;
1062
1063 double
1064 x;
1065
1067 inverse_edge;
1068
1069 /*
1070 Determine left and right edges.
1071 */
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)
1078 {
1079 intercept=(-z/affine->sx);
1080 x=intercept;
1081 if (x > inverse_edge.x1)
1082 inverse_edge.x1=x;
1083 intercept=(-z+(double) image->columns)/affine->sx;
1084 x=intercept;
1085 if (x < inverse_edge.x2)
1086 inverse_edge.x2=x;
1087 }
1088 else
1089 if (affine->sx < -MagickEpsilon)
1090 {
1091 intercept=(-z+(double) image->columns)/affine->sx;
1092 x=intercept;
1093 if (x > inverse_edge.x1)
1094 inverse_edge.x1=x;
1095 intercept=(-z/affine->sx);
1096 x=intercept;
1097 if (x < inverse_edge.x2)
1098 inverse_edge.x2=x;
1099 }
1100 else
1101 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->columns))
1102 {
1103 inverse_edge.x2=edge->x1;
1104 return(inverse_edge);
1105 }
1106 /*
1107 Determine top and bottom edges.
1108 */
1109 z=affine->sy*y+affine->ty;
1110 if (affine->rx >= MagickEpsilon)
1111 {
1112 intercept=(-z/affine->rx);
1113 x=intercept;
1114 if (x > inverse_edge.x1)
1115 inverse_edge.x1=x;
1116 intercept=(-z+(double) image->rows)/affine->rx;
1117 x=intercept;
1118 if (x < inverse_edge.x2)
1119 inverse_edge.x2=x;
1120 }
1121 else
1122 if (affine->rx < -MagickEpsilon)
1123 {
1124 intercept=(-z+(double) image->rows)/affine->rx;
1125 x=intercept;
1126 if (x > inverse_edge.x1)
1127 inverse_edge.x1=x;
1128 intercept=(-z/affine->rx);
1129 x=intercept;
1130 if (x < inverse_edge.x2)
1131 inverse_edge.x2=x;
1132 }
1133 else
1134 if ((z < 0.0) || ((size_t) floor(z+0.5) >= image->rows))
1135 {
1136 inverse_edge.x2=edge->x2;
1137 return(inverse_edge);
1138 }
1139 return(inverse_edge);
1140}
1141
1142static AffineMatrix InverseAffineMatrix(const AffineMatrix *affine)
1143{
1145 inverse_affine;
1146
1147 double
1148 determinant;
1149
1150 determinant=MagickSafeReciprocal(affine->sx*affine->sy-affine->rx*
1151 affine->ry);
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*
1157 inverse_affine.ry;
1158 inverse_affine.ty=(-affine->tx)*inverse_affine.rx-affine->ty*
1159 inverse_affine.sy;
1160 return(inverse_affine);
1161}
1162
1163MagickExport MagickBooleanType DrawAffineImage(Image *image,
1164 const Image *source,const AffineMatrix *affine,ExceptionInfo *exception)
1165{
1167 inverse_affine;
1168
1169 CacheView
1170 *image_view,
1171 *source_view;
1172
1173 MagickBooleanType
1174 status;
1175
1176 PixelInfo
1177 zero;
1178
1179 PointInfo
1180 extent[4],
1181 min,
1182 max;
1183
1184 ssize_t
1185 i;
1186
1188 edge;
1189
1190 ssize_t
1191 start,
1192 stop,
1193 y;
1194
1195 /*
1196 Determine bounding box.
1197 */
1198 assert(image != (Image *) NULL);
1199 assert(image->signature == MagickCoreSignature);
1200 if (IsEventLogging() != MagickFalse)
1201 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1202 assert(source != (const Image *) NULL);
1203 assert(source->signature == MagickCoreSignature);
1204 assert(affine != (AffineMatrix *) NULL);
1205 extent[0].x=0.0;
1206 extent[0].y=0.0;
1207 extent[1].x=(double) source->columns;
1208 extent[1].y=0.0;
1209 extent[2].x=(double) source->columns;
1210 extent[2].y=(double) source->rows;
1211 extent[3].x=0.0;
1212 extent[3].y=(double) source->rows;
1213 for (i=0; i < 4; i++)
1214 {
1215 PointInfo
1216 point;
1217
1218 point=extent[i];
1219 extent[i].x=point.x*affine->sx+point.y*affine->ry+affine->tx;
1220 extent[i].y=point.x*affine->rx+point.y*affine->sy+affine->ty;
1221 }
1222 min=extent[0];
1223 max=extent[0];
1224 for (i=1; i < 4; i++)
1225 {
1226 if (min.x > extent[i].x)
1227 min.x=extent[i].x;
1228 if (min.y > extent[i].y)
1229 min.y=extent[i].y;
1230 if (max.x < extent[i].x)
1231 max.x=extent[i].x;
1232 if (max.y < extent[i].y)
1233 max.y=extent[i].y;
1234 }
1235 /*
1236 Affine transform image.
1237 */
1238 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
1239 return(MagickFalse);
1240 status=MagickTrue;
1241 edge.x1=min.x;
1242 edge.y1=min.y;
1243 edge.x2=max.x;
1244 edge.y2=max.y;
1245 inverse_affine=InverseAffineMatrix(affine);
1246 if (edge.y1 < 0.0)
1247 edge.y1=0.0;
1248 if (edge.y2 > ((double) image->rows-1.0))
1249 edge.y2=(double) image->rows-1.0;
1250 GetPixelInfo(image,&zero);
1251 start=CastDoubleToSsizeT(ceil(edge.y1-0.5));
1252 stop=CastDoubleToSsizeT(floor(edge.y2+0.5));
1253 source_view=AcquireVirtualCacheView(source,exception);
1254 image_view=AcquireAuthenticCacheView(image,exception);
1255#if defined(MAGICKCORE_OPENMP_SUPPORT)
1256 #pragma omp parallel for schedule(static) shared(status) \
1257 magick_number_threads(source,image,(size_t) (stop-start),2)
1258#endif
1259 for (y=start; y <= stop; y++)
1260 {
1261 PixelInfo
1262 composite,
1263 pixel;
1264
1265 PointInfo
1266 point;
1267
1268 Quantum
1269 *magick_restrict q;
1270
1272 inverse_edge;
1273
1274 ssize_t
1275 x;
1276
1277 if (status == MagickFalse)
1278 continue;
1279 inverse_edge=AffineEdge(source,&inverse_affine,(double) y,&edge);
1280 if (inverse_edge.x2 < inverse_edge.x1)
1281 continue;
1282 if (inverse_edge.x1 < 0.0)
1283 inverse_edge.x1=0.0;
1284 if (inverse_edge.x2 > ((double) image->columns-1.0))
1285 inverse_edge.x2=(double) image->columns-1.0;
1286 q=GetCacheViewAuthenticPixels(image_view,CastDoubleToSsizeT(
1287 ceil(inverse_edge.x1-0.5)),y,(size_t) CastDoubleToSsizeT(floor(
1288 inverse_edge.x2+0.5)-ceil(inverse_edge.x1-0.5)+1),1,exception);
1289 if (q == (Quantum *) NULL)
1290 continue;
1291 pixel=zero;
1292 composite=zero;
1293 for (x=CastDoubleToSsizeT(ceil(inverse_edge.x1-0.5));
1294 x <= CastDoubleToSsizeT(floor(inverse_edge.x2+0.5)); x++)
1295 {
1296 point.x=(double) x*inverse_affine.sx+y*inverse_affine.ry+
1297 inverse_affine.tx;
1298 point.y=(double) x*inverse_affine.rx+y*inverse_affine.sy+
1299 inverse_affine.ty;
1300 status=InterpolatePixelInfo(source,source_view,UndefinedInterpolatePixel,
1301 point.x,point.y,&pixel,exception);
1302 if (status == MagickFalse)
1303 break;
1304 GetPixelInfoPixel(image,q,&composite);
1305 CompositePixelInfoOver(&pixel,pixel.alpha,&composite,composite.alpha,
1306 &composite);
1307 SetPixelViaPixelInfo(image,&composite,q);
1308 q+=(ptrdiff_t) GetPixelChannels(image);
1309 }
1310 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1311 status=MagickFalse;
1312 }
1313 source_view=DestroyCacheView(source_view);
1314 image_view=DestroyCacheView(image_view);
1315 return(status);
1316}
1317␌
1318/*
1319%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1320% %
1321% %
1322% %
1323+ D r a w B o u n d i n g R e c t a n g l e s %
1324% %
1325% %
1326% %
1327%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1328%
1329% DrawBoundingRectangles() draws the bounding rectangles on the image. This
1330% is only useful for developers debugging the rendering algorithm.
1331%
1332% The format of the DrawBoundingRectangles method is:
1333%
1334% MagickBooleanType DrawBoundingRectangles(Image *image,
1335% const DrawInfo *draw_info,PolygonInfo *polygon_info,
1336% ExceptionInfo *exception)
1337%
1338% A description of each parameter follows:
1339%
1340% o image: the image.
1341%
1342% o draw_info: the draw info.
1343%
1344% o polygon_info: Specifies a pointer to a PolygonInfo structure.
1345%
1346% o exception: return any errors or warnings in this structure.
1347%
1348*/
1349
1350static MagickBooleanType DrawBoundingRectangles(Image *image,
1351 const DrawInfo *draw_info,const PolygonInfo *polygon_info,
1352 ExceptionInfo *exception)
1353{
1354 double
1355 mid;
1356
1357 DrawInfo
1358 *clone_info;
1359
1360 MagickStatusType
1361 status;
1362
1363 PointInfo
1364 end,
1365 resolution,
1366 start;
1367
1369 primitive_info[6];
1370
1371 ssize_t
1372 i;
1373
1375 bounds;
1376
1377 ssize_t
1378 coordinates;
1379
1380 (void) memset(primitive_info,0,sizeof(primitive_info));
1381 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1382 status=QueryColorCompliance("#000F",AllCompliance,&clone_info->fill,
1383 exception);
1384 if (status == MagickFalse)
1385 {
1386 clone_info=DestroyDrawInfo(clone_info);
1387 return(MagickFalse);
1388 }
1389 resolution.x=96.0;
1390 resolution.y=96.0;
1391 if (clone_info->density != (char *) NULL)
1392 {
1394 geometry_info;
1395
1396 MagickStatusType
1397 flags;
1398
1399 flags=ParseGeometry(clone_info->density,&geometry_info);
1400 if ((flags & RhoValue) != 0)
1401 resolution.x=geometry_info.rho;
1402 resolution.y=resolution.x;
1403 if ((flags & SigmaValue) != 0)
1404 resolution.y=geometry_info.sigma;
1405 }
1406 mid=(resolution.x/96.0)*ExpandAffine(&clone_info->affine)*
1407 clone_info->stroke_width/2.0;
1408 bounds.x1=0.0;
1409 bounds.y1=0.0;
1410 bounds.x2=0.0;
1411 bounds.y2=0.0;
1412 if (polygon_info != (PolygonInfo *) NULL)
1413 {
1414 bounds=polygon_info->edges[0].bounds;
1415 for (i=1; i < (ssize_t) polygon_info->number_edges; i++)
1416 {
1417 if (polygon_info->edges[i].bounds.x1 < (double) bounds.x1)
1418 bounds.x1=polygon_info->edges[i].bounds.x1;
1419 if (polygon_info->edges[i].bounds.y1 < (double) bounds.y1)
1420 bounds.y1=polygon_info->edges[i].bounds.y1;
1421 if (polygon_info->edges[i].bounds.x2 > (double) bounds.x2)
1422 bounds.x2=polygon_info->edges[i].bounds.x2;
1423 if (polygon_info->edges[i].bounds.y2 > (double) bounds.y2)
1424 bounds.y2=polygon_info->edges[i].bounds.y2;
1425 }
1426 bounds.x1-=mid;
1427 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double)
1428 image->columns ? (double) image->columns-1 : bounds.x1;
1429 bounds.y1-=mid;
1430 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double)
1431 image->rows ? (double) image->rows-1 : bounds.y1;
1432 bounds.x2+=mid;
1433 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double)
1434 image->columns ? (double) image->columns-1 : bounds.x2;
1435 bounds.y2+=mid;
1436 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double)
1437 image->rows ? (double) image->rows-1 : bounds.y2;
1438 for (i=0; i < (ssize_t) polygon_info->number_edges; i++)
1439 {
1440 if (polygon_info->edges[i].direction != 0)
1441 status=QueryColorCompliance("#f00",AllCompliance,&clone_info->stroke,
1442 exception);
1443 else
1444 status=QueryColorCompliance("#0f0",AllCompliance,&clone_info->stroke,
1445 exception);
1446 if (status == MagickFalse)
1447 break;
1448 start.x=(double) (polygon_info->edges[i].bounds.x1-mid);
1449 start.y=(double) (polygon_info->edges[i].bounds.y1-mid);
1450 end.x=(double) (polygon_info->edges[i].bounds.x2+mid);
1451 end.y=(double) (polygon_info->edges[i].bounds.y2+mid);
1452 primitive_info[0].primitive=RectanglePrimitive;
1453 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1454 primitive_info[0].method=ReplaceMethod;
1455 coordinates=(ssize_t) primitive_info[0].coordinates;
1456 primitive_info[coordinates].primitive=UndefinedPrimitive;
1457 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1458 if (status == MagickFalse)
1459 break;
1460 }
1461 if (i < (ssize_t) polygon_info->number_edges)
1462 {
1463 clone_info=DestroyDrawInfo(clone_info);
1464 return(status == 0 ? MagickFalse : MagickTrue);
1465 }
1466 }
1467 status=QueryColorCompliance("#00f",AllCompliance,&clone_info->stroke,
1468 exception);
1469 if (status == MagickFalse)
1470 {
1471 clone_info=DestroyDrawInfo(clone_info);
1472 return(MagickFalse);
1473 }
1474 start.x=(double) (bounds.x1-mid);
1475 start.y=(double) (bounds.y1-mid);
1476 end.x=(double) (bounds.x2+mid);
1477 end.y=(double) (bounds.y2+mid);
1478 primitive_info[0].primitive=RectanglePrimitive;
1479 status&=(MagickStatusType) TraceRectangle(primitive_info,start,end);
1480 primitive_info[0].method=ReplaceMethod;
1481 coordinates=(ssize_t) primitive_info[0].coordinates;
1482 primitive_info[coordinates].primitive=UndefinedPrimitive;
1483 status=DrawPrimitive(image,clone_info,primitive_info,exception);
1484 clone_info=DestroyDrawInfo(clone_info);
1485 return(status == 0 ? MagickFalse : MagickTrue);
1486}
1487␌
1488/*
1489%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1490% %
1491% %
1492% %
1493% D r a w C l i p P a t h %
1494% %
1495% %
1496% %
1497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1498%
1499% DrawClipPath() draws the clip path on the image mask.
1500%
1501% The format of the DrawClipPath method is:
1502%
1503% MagickBooleanType DrawClipPath(Image *image,const DrawInfo *draw_info,
1504% const char *id,ExceptionInfo *exception)
1505%
1506% A description of each parameter follows:
1507%
1508% o image: the image.
1509%
1510% o draw_info: the draw info.
1511%
1512% o id: the clip path id.
1513%
1514% o exception: return any errors or warnings in this structure.
1515%
1516*/
1517MagickExport MagickBooleanType DrawClipPath(Image *image,
1518 const DrawInfo *draw_info,const char *id,ExceptionInfo *exception)
1519{
1520 const char
1521 *clip_path;
1522
1523 Image
1524 *clipping_mask;
1525
1526 MagickBooleanType
1527 status;
1528
1529 clip_path=GetImageArtifact(image,id);
1530 if (clip_path == (const char *) NULL)
1531 return(MagickFalse);
1532 clipping_mask=DrawClippingMask(image,draw_info,draw_info->clip_mask,clip_path,
1533 exception);
1534 if (clipping_mask == (Image *) NULL)
1535 return(MagickFalse);
1536 status=SetImageMask(image,WritePixelMask,clipping_mask,exception);
1537 clipping_mask=DestroyImage(clipping_mask);
1538 return(status);
1539}
1540␌
1541/*
1542%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1543% %
1544% %
1545% %
1546% D r a w C l i p p i n g M a s k %
1547% %
1548% %
1549% %
1550%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1551%
1552% DrawClippingMask() draws the clip path and returns it as an image clipping
1553% mask.
1554%
1555% The format of the DrawClippingMask method is:
1556%
1557% Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1558% const char *id,const char *clip_path,ExceptionInfo *exception)
1559%
1560% A description of each parameter follows:
1561%
1562% o image: the image.
1563%
1564% o draw_info: the draw info.
1565%
1566% o id: the clip path id.
1567%
1568% o clip_path: the clip path.
1569%
1570% o exception: return any errors or warnings in this structure.
1571%
1572*/
1573static Image *DrawClippingMask(Image *image,const DrawInfo *draw_info,
1574 const char *id,const char *clip_path,ExceptionInfo *exception)
1575{
1576 DrawInfo
1577 *clone_info;
1578
1579 Image
1580 *clip_mask,
1581 *separate_mask;
1582
1583 MagickStatusType
1584 status;
1585
1586 /*
1587 Draw a clip path.
1588 */
1589 assert(image != (Image *) NULL);
1590 assert(image->signature == MagickCoreSignature);
1591 assert(draw_info != (const DrawInfo *) NULL);
1592 if (IsEventLogging() != MagickFalse)
1593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1594 clip_mask=AcquireImage((const ImageInfo *) NULL,exception);
1595 status=SetImageExtent(clip_mask,image->columns,image->rows,exception);
1596 if (status == MagickFalse)
1597 return(DestroyImage(clip_mask));
1598 status=SetImageMask(clip_mask,WritePixelMask,(Image *) NULL,exception);
1599 status=QueryColorCompliance("#0000",AllCompliance,
1600 &clip_mask->background_color,exception);
1601 clip_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1602 clip_mask->background_color.alpha_trait=BlendPixelTrait;
1603 status=SetImageBackgroundColor(clip_mask,exception);
1604 if (draw_info->debug != MagickFalse)
1605 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin clip-path %s",
1606 id);
1607 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1608 (void) CloneString(&clone_info->primitive,clip_path);
1609 status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1610 exception);
1611 if (clone_info->clip_mask != (char *) NULL)
1612 clone_info->clip_mask=DestroyString(clone_info->clip_mask);
1613 status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1614 exception);
1615 clone_info->stroke_width=0.0;
1616 clone_info->alpha=OpaqueAlpha;
1617 clone_info->clip_path=MagickTrue;
1618 status=RenderMVGContent(clip_mask,clone_info,0,exception);
1619 clone_info=DestroyDrawInfo(clone_info);
1620 separate_mask=SeparateImage(clip_mask,AlphaChannel,exception);
1621 if (separate_mask == (Image *) NULL)
1622 status=MagickFalse;
1623 else
1624 {
1625 clip_mask=DestroyImage(clip_mask);
1626 clip_mask=separate_mask;
1627 status&=(MagickStatusType) NegateImage(clip_mask,MagickFalse,exception);
1628 }
1629 if (status == MagickFalse)
1630 clip_mask=DestroyImage(clip_mask);
1631 if (draw_info->debug != MagickFalse)
1632 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end clip-path");
1633 return(clip_mask);
1634}
1635␌
1636/*
1637%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1638% %
1639% %
1640% %
1641% D r a w C o m p o s i t e M a s k %
1642% %
1643% %
1644% %
1645%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1646%
1647% DrawCompositeMask() draws the mask path and returns it as an image mask.
1648%
1649% The format of the DrawCompositeMask method is:
1650%
1651% Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1652% const char *id,const char *mask_path,ExceptionInfo *exception)
1653%
1654% A description of each parameter follows:
1655%
1656% o image: the image.
1657%
1658% o draw_info: the draw info.
1659%
1660% o id: the mask path id.
1661%
1662% o mask_path: the mask path.
1663%
1664% o exception: return any errors or warnings in this structure.
1665%
1666*/
1667static Image *DrawCompositeMask(Image *image,const DrawInfo *draw_info,
1668 const char *id,const char *mask_path,ExceptionInfo *exception)
1669{
1670 Image
1671 *composite_mask,
1672 *separate_mask;
1673
1674 DrawInfo
1675 *clone_info;
1676
1677 MagickStatusType
1678 status;
1679
1680 /*
1681 Draw a mask path.
1682 */
1683 assert(image != (Image *) NULL);
1684 assert(image->signature == MagickCoreSignature);
1685 assert(draw_info != (const DrawInfo *) NULL);
1686 if (IsEventLogging() != MagickFalse)
1687 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1688 composite_mask=AcquireImage((const ImageInfo *) NULL,exception);
1689 status=SetImageExtent(composite_mask,image->columns,image->rows,exception);
1690 if (status == MagickFalse)
1691 return(DestroyImage(composite_mask));
1692 status=SetImageMask(composite_mask,CompositePixelMask,(Image *) NULL,
1693 exception);
1694 status=QueryColorCompliance("#0000",AllCompliance,
1695 &composite_mask->background_color,exception);
1696 composite_mask->background_color.alpha=(MagickRealType) TransparentAlpha;
1697 composite_mask->background_color.alpha_trait=BlendPixelTrait;
1698 (void) SetImageBackgroundColor(composite_mask,exception);
1699 if (draw_info->debug != MagickFalse)
1700 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"\nbegin mask-path %s",
1701 id);
1702 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1703 (void) CloneString(&clone_info->primitive,mask_path);
1704 status=QueryColorCompliance("#ffffff",AllCompliance,&clone_info->fill,
1705 exception);
1706 status=QueryColorCompliance("#00000000",AllCompliance,&clone_info->stroke,
1707 exception);
1708 clone_info->stroke_width=0.0;
1709 clone_info->alpha=OpaqueAlpha;
1710 status=RenderMVGContent(composite_mask,clone_info,0,exception);
1711 clone_info=DestroyDrawInfo(clone_info);
1712 separate_mask=SeparateImage(composite_mask,AlphaChannel,exception);
1713 if (separate_mask != (Image *) NULL)
1714 {
1715 composite_mask=DestroyImage(composite_mask);
1716 composite_mask=separate_mask;
1717 status=NegateImage(composite_mask,MagickFalse,exception);
1718 if (status == MagickFalse)
1719 composite_mask=DestroyImage(composite_mask);
1720 }
1721 if (status == MagickFalse)
1722 composite_mask=DestroyImage(composite_mask);
1723 if (draw_info->debug != MagickFalse)
1724 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end mask-path");
1725 return(composite_mask);
1726}
1727␌
1728/*
1729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1730% %
1731% %
1732% %
1733+ D r a w D a s h P o l y g o n %
1734% %
1735% %
1736% %
1737%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1738%
1739% DrawDashPolygon() draws a dashed polygon (line, rectangle, ellipse) on the
1740% image while respecting the dash offset and dash pattern attributes.
1741%
1742% The format of the DrawDashPolygon method is:
1743%
1744% MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1745% const PrimitiveInfo *primitive_info,Image *image,
1746% ExceptionInfo *exception)
1747%
1748% A description of each parameter follows:
1749%
1750% o draw_info: the draw info.
1751%
1752% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
1753%
1754% o image: the image.
1755%
1756% o exception: return any errors or warnings in this structure.
1757%
1758*/
1759static MagickBooleanType DrawDashPolygon(const DrawInfo *draw_info,
1760 const PrimitiveInfo *primitive_info,Image *image,ExceptionInfo *exception)
1761{
1762 double
1763 dx,
1764 dy,
1765 length,
1766 maximum_length,
1767 offset,
1768 scale,
1769 total_length;
1770
1771 DrawInfo
1772 *clone_info;
1773
1774 MagickStatusType
1775 status;
1776
1778 *dash_polygon;
1779
1780 ssize_t
1781 i;
1782
1783 size_t
1784 number_vertices;
1785
1786 ssize_t
1787 j,
1788 n;
1789
1790 assert(draw_info != (const DrawInfo *) NULL);
1791 if (draw_info->debug != MagickFalse)
1792 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-dash");
1793 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++) ;
1794 number_vertices=(size_t) i;
1795 dash_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
1796 (2UL*number_vertices+32UL),sizeof(*dash_polygon));
1797 if (dash_polygon == (PrimitiveInfo *) NULL)
1798 {
1799 (void) ThrowMagickException(exception,GetMagickModule(),
1800 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
1801 return(MagickFalse);
1802 }
1803 (void) memset(dash_polygon,0,(2UL*number_vertices+32UL)*
1804 sizeof(*dash_polygon));
1805 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1806 clone_info->miterlimit=0;
1807 dash_polygon[0]=primitive_info[0];
1808 dash_polygon[0].closed_subpath=MagickFalse;
1809 scale=ExpandAffine(&draw_info->affine);
1810 length=scale*draw_info->dash_pattern[0];
1811 offset=fabs(draw_info->dash_offset) >= MagickEpsilon ?
1812 scale*draw_info->dash_offset : 0.0;
1813 j=1;
1814 for (n=0; offset > 0.0; j=0)
1815 {
1816 if (draw_info->dash_pattern[n] <= 0.0)
1817 break;
1818 length=scale*(draw_info->dash_pattern[n]+(n == 0 ? -0.5 : 0.5));
1819 if (offset > length)
1820 {
1821 offset-=length;
1822 n++;
1823 length=scale*draw_info->dash_pattern[n];
1824 continue;
1825 }
1826 if (offset < length)
1827 {
1828 length-=offset;
1829 offset=0.0;
1830 break;
1831 }
1832 offset=0.0;
1833 n++;
1834 }
1835 status=MagickTrue;
1836 maximum_length=0.0;
1837 total_length=0.0;
1838 for (i=1; (i < (ssize_t) number_vertices) && (length >= 0.0); i++)
1839 {
1840 dx=primitive_info[i].point.x-primitive_info[i-1].point.x;
1841 dy=primitive_info[i].point.y-primitive_info[i-1].point.y;
1842 maximum_length=hypot(dx,dy);
1843 if (maximum_length > (double) (MaxBezierCoordinates >> 2))
1844 continue;
1845 if (fabs(length) < MagickEpsilon)
1846 {
1847 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1848 n++;
1849 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1850 n=0;
1851 length=scale*draw_info->dash_pattern[n];
1852 }
1853 for (total_length=0.0; (length >= 0.0) && (maximum_length >= (total_length+length)); )
1854 {
1855 total_length+=length;
1856 if ((n & 0x01) != 0)
1857 {
1858 dash_polygon[0]=primitive_info[0];
1859 dash_polygon[0].closed_subpath=MagickFalse;
1860 dash_polygon[0].point.x=(double) (primitive_info[i-1].point.x+dx*
1861 total_length*MagickSafeReciprocal(maximum_length));
1862 dash_polygon[0].point.y=(double) (primitive_info[i-1].point.y+dy*
1863 total_length*MagickSafeReciprocal(maximum_length));
1864 j=1;
1865 }
1866 else
1867 {
1868 if ((j+1) > (ssize_t) number_vertices)
1869 break;
1870 dash_polygon[j]=primitive_info[i-1];
1871 dash_polygon[j].closed_subpath=MagickFalse;
1872 dash_polygon[j].point.x=(double) (primitive_info[i-1].point.x+dx*
1873 total_length*MagickSafeReciprocal(maximum_length));
1874 dash_polygon[j].point.y=(double) (primitive_info[i-1].point.y+dy*
1875 total_length*MagickSafeReciprocal(maximum_length));
1876 dash_polygon[j].coordinates=1;
1877 j++;
1878 dash_polygon[0].coordinates=(size_t) j;
1879 dash_polygon[j].primitive=UndefinedPrimitive;
1880 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1881 dash_polygon,exception);
1882 if (status == MagickFalse)
1883 break;
1884 }
1885 if (fabs(draw_info->dash_pattern[n]) >= MagickEpsilon)
1886 n++;
1887 if (fabs(draw_info->dash_pattern[n]) < MagickEpsilon)
1888 n=0;
1889 length=scale*draw_info->dash_pattern[n];
1890 }
1891 length-=(maximum_length-total_length);
1892 if ((n & 0x01) != 0)
1893 continue;
1894 dash_polygon[j]=primitive_info[i];
1895 dash_polygon[j].coordinates=1;
1896 j++;
1897 }
1898 if ((status != MagickFalse) && (total_length < maximum_length) &&
1899 ((n & 0x01) == 0) && (j > 1))
1900 {
1901 dash_polygon[j]=primitive_info[i-1];
1902 dash_polygon[j].closed_subpath=MagickFalse;
1903 dash_polygon[j].point.x+=MagickEpsilon;
1904 dash_polygon[j].point.y+=MagickEpsilon;
1905 dash_polygon[j].coordinates=1;
1906 j++;
1907 dash_polygon[0].coordinates=(size_t) j;
1908 dash_polygon[j].primitive=UndefinedPrimitive;
1909 status&=(MagickStatusType) DrawStrokePolygon(image,clone_info,
1910 dash_polygon,exception);
1911 }
1912 dash_polygon=(PrimitiveInfo *) RelinquishMagickMemory(dash_polygon);
1913 clone_info=DestroyDrawInfo(clone_info);
1914 if (draw_info->debug != MagickFalse)
1915 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-dash");
1916 return(status != 0 ? MagickTrue : MagickFalse);
1917}
1918␌
1919/*
1920%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1921% %
1922% %
1923% %
1924% D r a w G r a d i e n t I m a g e %
1925% %
1926% %
1927% %
1928%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1929%
1930% DrawGradientImage() draws a linear gradient on the image.
1931%
1932% The format of the DrawGradientImage method is:
1933%
1934% MagickBooleanType DrawGradientImage(Image *image,
1935% const DrawInfo *draw_info,ExceptionInfo *exception)
1936%
1937% A description of each parameter follows:
1938%
1939% o image: the image.
1940%
1941% o draw_info: the draw info.
1942%
1943% o exception: return any errors or warnings in this structure.
1944%
1945*/
1946
1947static inline double GetStopColorOffset(const GradientInfo *gradient,
1948 const ssize_t x,const ssize_t y)
1949{
1950 switch (gradient->type)
1951 {
1952 case UndefinedGradient:
1953 case LinearGradient:
1954 {
1955 double
1956 gamma,
1957 length,
1958 offset,
1959 scale;
1960
1961 PointInfo
1962 p,
1963 q;
1964
1965 const SegmentInfo
1966 *gradient_vector;
1967
1968 gradient_vector=(&gradient->gradient_vector);
1969 p.x=gradient_vector->x2-gradient_vector->x1;
1970 p.y=gradient_vector->y2-gradient_vector->y1;
1971 q.x=(double) x-gradient_vector->x1;
1972 q.y=(double) y-gradient_vector->y1;
1973 length=sqrt(q.x*q.x+q.y*q.y);
1974 gamma=sqrt(p.x*p.x+p.y*p.y)*length;
1975 gamma=MagickSafeReciprocal(gamma);
1976 scale=p.x*q.x+p.y*q.y;
1977 offset=gamma*scale*length;
1978 return(offset);
1979 }
1980 case RadialGradient:
1981 {
1982 PointInfo
1983 v;
1984
1985 if (gradient->spread == RepeatSpread)
1986 {
1987 v.x=(double) x-gradient->center.x;
1988 v.y=(double) y-gradient->center.y;
1989 return(sqrt(v.x*v.x+v.y*v.y));
1990 }
1991 v.x=(double) (((x-gradient->center.x)*cos(DegreesToRadians(
1992 gradient->angle)))+((y-gradient->center.y)*sin(DegreesToRadians(
1993 gradient->angle))))*MagickSafeReciprocal(gradient->radii.x);
1994 v.y=(double) (((x-gradient->center.x)*sin(DegreesToRadians(
1995 gradient->angle)))-((y-gradient->center.y)*cos(DegreesToRadians(
1996 gradient->angle))))*MagickSafeReciprocal(gradient->radii.y);
1997 return(sqrt(v.x*v.x+v.y*v.y));
1998 }
1999 }
2000 return(0.0);
2001}
2002
2003static int StopInfoCompare(const void *x,const void *y)
2004{
2005 StopInfo
2006 *stop_1,
2007 *stop_2;
2008
2009 stop_1=(StopInfo *) x;
2010 stop_2=(StopInfo *) y;
2011 if (stop_1->offset > stop_2->offset)
2012 return(1);
2013 if (fabs(stop_1->offset-stop_2->offset) <= MagickEpsilon)
2014 return(0);
2015 return(-1);
2016}
2017
2018MagickExport MagickBooleanType DrawGradientImage(Image *image,
2019 const DrawInfo *draw_info,ExceptionInfo *exception)
2020{
2021 CacheView
2022 *image_view;
2023
2024 const GradientInfo
2025 *gradient;
2026
2027 const SegmentInfo
2028 *gradient_vector;
2029
2030 double
2031 length;
2032
2033 MagickBooleanType
2034 status;
2035
2036 PixelInfo
2037 zero;
2038
2039 PointInfo
2040 point;
2041
2043 bounding_box;
2044
2045 size_t
2046 height;
2047
2048 ssize_t
2049 y;
2050
2051 /*
2052 Draw linear or radial gradient on image.
2053 */
2054 assert(image != (Image *) NULL);
2055 assert(image->signature == MagickCoreSignature);
2056 assert(draw_info != (const DrawInfo *) NULL);
2057 if (IsEventLogging() != MagickFalse)
2058 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2059 gradient=(&draw_info->gradient);
2060 qsort(gradient->stops,gradient->number_stops,sizeof(StopInfo),
2061 StopInfoCompare);
2062 gradient_vector=(&gradient->gradient_vector);
2063 point.x=gradient_vector->x2-gradient_vector->x1;
2064 point.y=gradient_vector->y2-gradient_vector->y1;
2065 length=sqrt(point.x*point.x+point.y*point.y);
2066 bounding_box=gradient->bounding_box;
2067 status=MagickTrue;
2068 GetPixelInfo(image,&zero);
2069 image_view=AcquireAuthenticCacheView(image,exception);
2070 height=(size_t) (bounding_box.y+(ssize_t) bounding_box.height);
2071#if defined(MAGICKCORE_OPENMP_SUPPORT)
2072 #pragma omp parallel for schedule(static) shared(status) \
2073 magick_number_threads(image,image,height,1)
2074#endif
2075 for (y=bounding_box.y; y < (ssize_t) height; y++)
2076 {
2077 double
2078 alpha,
2079 offset;
2080
2081 PixelInfo
2082 composite,
2083 pixel;
2084
2085 Quantum
2086 *magick_restrict q;
2087
2088 size_t
2089 width;
2090
2091 ssize_t
2092 i,
2093 j,
2094 x;
2095
2096 if (status == MagickFalse)
2097 continue;
2098 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,y,(size_t)
2099 bounding_box.width,1,exception);
2100 if (q == (Quantum *) NULL)
2101 {
2102 status=MagickFalse;
2103 continue;
2104 }
2105 pixel=zero;
2106 composite=zero;
2107 offset=GetStopColorOffset(gradient,0,y);
2108 if (gradient->type != RadialGradient)
2109 offset*=MagickSafeReciprocal(length);
2110 width=(size_t) (bounding_box.x+(ssize_t) bounding_box.width);
2111 for (x=bounding_box.x; x < (ssize_t) width; x++)
2112 {
2113 GetPixelInfoPixel(image,q,&pixel);
2114 switch (gradient->spread)
2115 {
2116 case UndefinedSpread:
2117 case PadSpread:
2118 {
2119 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2120 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2121 {
2122 offset=GetStopColorOffset(gradient,x,y);
2123 if (gradient->type != RadialGradient)
2124 offset*=MagickSafeReciprocal(length);
2125 }
2126 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2127 if (offset < gradient->stops[i].offset)
2128 break;
2129 if ((offset < 0.0) || (i == 0))
2130 composite=gradient->stops[0].color;
2131 else
2132 if ((offset > 1.0) || (i == (ssize_t) gradient->number_stops))
2133 composite=gradient->stops[gradient->number_stops-1].color;
2134 else
2135 {
2136 j=i;
2137 i--;
2138 alpha=(offset-gradient->stops[i].offset)/
2139 (gradient->stops[j].offset-gradient->stops[i].offset);
2140 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2141 &gradient->stops[j].color,alpha,&composite);
2142 }
2143 break;
2144 }
2145 case ReflectSpread:
2146 {
2147 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2148 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2149 {
2150 offset=GetStopColorOffset(gradient,x,y);
2151 if (gradient->type != RadialGradient)
2152 offset*=MagickSafeReciprocal(length);
2153 }
2154 if (offset < 0.0)
2155 offset=(-offset);
2156 if ((ssize_t) fmod(offset,2.0) == 0)
2157 offset=fmod(offset,1.0);
2158 else
2159 offset=1.0-fmod(offset,1.0);
2160 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2161 if (offset < gradient->stops[i].offset)
2162 break;
2163 if (i == 0)
2164 composite=gradient->stops[0].color;
2165 else
2166 if (i == (ssize_t) gradient->number_stops)
2167 composite=gradient->stops[gradient->number_stops-1].color;
2168 else
2169 {
2170 j=i;
2171 i--;
2172 alpha=(offset-gradient->stops[i].offset)/
2173 (gradient->stops[j].offset-gradient->stops[i].offset);
2174 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2175 &gradient->stops[j].color,alpha,&composite);
2176 }
2177 break;
2178 }
2179 case RepeatSpread:
2180 {
2181 double
2182 repeat;
2183
2184 MagickBooleanType
2185 antialias;
2186
2187 antialias=MagickFalse;
2188 repeat=0.0;
2189 if ((x != CastDoubleToSsizeT(ceil(gradient_vector->x1-0.5))) ||
2190 (y != CastDoubleToSsizeT(ceil(gradient_vector->y1-0.5))))
2191 {
2192 offset=GetStopColorOffset(gradient,x,y);
2193 if (gradient->type == LinearGradient)
2194 {
2195 repeat=fmod(offset,length);
2196 if (repeat < 0.0)
2197 repeat=length-fmod(-repeat,length);
2198 else
2199 repeat=fmod(offset,length);
2200 antialias=(repeat < length) && ((repeat+1.0) > length) ?
2201 MagickTrue : MagickFalse;
2202 offset=MagickSafeReciprocal(length)*repeat;
2203 }
2204 else
2205 {
2206 repeat=fmod(offset,gradient->radius);
2207 if (repeat < 0.0)
2208 repeat=gradient->radius-fmod(-repeat,gradient->radius);
2209 else
2210 repeat=fmod(offset,gradient->radius);
2211 antialias=repeat+1.0 > gradient->radius ? MagickTrue :
2212 MagickFalse;
2213 offset=repeat*MagickSafeReciprocal(gradient->radius);
2214 }
2215 }
2216 for (i=0; i < (ssize_t) gradient->number_stops; i++)
2217 if (offset < gradient->stops[i].offset)
2218 break;
2219 if (i == 0)
2220 composite=gradient->stops[0].color;
2221 else
2222 if (i == (ssize_t) gradient->number_stops)
2223 composite=gradient->stops[gradient->number_stops-1].color;
2224 else
2225 {
2226 j=i;
2227 i--;
2228 alpha=(offset-gradient->stops[i].offset)/
2229 (gradient->stops[j].offset-gradient->stops[i].offset);
2230 if (antialias != MagickFalse)
2231 {
2232 if (gradient->type == LinearGradient)
2233 alpha=length-repeat;
2234 else
2235 alpha=gradient->radius-repeat;
2236 i=0;
2237 j=(ssize_t) gradient->number_stops-1L;
2238 }
2239 CompositePixelInfoBlend(&gradient->stops[i].color,1.0-alpha,
2240 &gradient->stops[j].color,alpha,&composite);
2241 }
2242 break;
2243 }
2244 }
2245 CompositePixelInfoOver(&composite,composite.alpha,&pixel,pixel.alpha,
2246 &pixel);
2247 SetPixelViaPixelInfo(image,&pixel,q);
2248 q+=(ptrdiff_t) GetPixelChannels(image);
2249 }
2250 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2251 status=MagickFalse;
2252 }
2253 image_view=DestroyCacheView(image_view);
2254 return(status);
2255}
2256␌
2257/*
2258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2259% %
2260% %
2261% %
2262% D r a w I m a g e %
2263% %
2264% %
2265% %
2266%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2267%
2268% DrawImage() draws a graphic primitive on your image. The primitive
2269% may be represented as a string or filename. Precede the filename with an
2270% "at" sign (@) and the contents of the file are drawn on the image. You
2271% can affect how text is drawn by setting one or more members of the draw
2272% info structure.
2273%
2274% The format of the DrawImage method is:
2275%
2276% MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
2277% ExceptionInfo *exception)
2278%
2279% A description of each parameter follows:
2280%
2281% o image: the image.
2282%
2283% o draw_info: the draw info.
2284%
2285% o exception: return any errors or warnings in this structure.
2286%
2287*/
2288
2289static inline MagickBooleanType CheckPrimitiveExtent(MVGInfo *mvg_info,
2290 const double pad)
2291{
2292 double
2293 proposed_extent;
2294
2296 *primitive_info;
2297
2298 size_t
2299 extent;
2300
2301 ssize_t
2302 i;
2303
2304 if ((mvg_info == (MVGInfo *) NULL) ||
2305 (mvg_info->primitive_info == (PrimitiveInfo **) NULL) ||
2306 (*mvg_info->primitive_info == (PrimitiveInfo *) NULL) ||
2307 (mvg_info->extent == (size_t *) NULL))
2308 return(MagickFalse);
2309 proposed_extent=mvg_info->offset+pad+PrimitiveExtentPad+1.0;
2310 if ((proposed_extent <= 0.0) || (proposed_extent > (double) MAGICK_SIZE_MAX))
2311 return(MagickFalse);
2312 extent=CastDoubleToSizeT(ceil(proposed_extent));
2313 if (extent <= *mvg_info->extent)
2314 return(MagickTrue);
2315 if (extent > (GetMaxMemoryRequest()/sizeof(PrimitiveInfo)))
2316 return(MagickFalse);
2317 primitive_info=(PrimitiveInfo *) ResizeQuantumMemory(
2318 *mvg_info->primitive_info,extent+1,sizeof(PrimitiveInfo));
2319 if (primitive_info == (PrimitiveInfo *) NULL)
2320 {
2321 /*
2322 Create a stack to unwind; report failure.
2323 */
2324 extent=(size_t) PrimitiveExtentPad;
2325 primitive_info=(PrimitiveInfo *) AcquireCriticalMemory(extent*
2326 sizeof(*primitive_info));
2327 (void) memset(primitive_info,0,extent*sizeof(*primitive_info));
2328 *mvg_info->primitive_info=primitive_info;
2329 *mvg_info->extent=extent;
2330 mvg_info->offset=0;
2331 ThrowMagickException(mvg_info->exception,GetMagickModule(),
2332 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2333 return(MagickFalse);
2334 }
2335 primitive_info[extent].primitive=UndefinedPrimitive;
2336 primitive_info[extent].text=(char *) NULL;
2337 /*
2338 Commit updated buffer.
2339 */
2340 for (i=(ssize_t) *mvg_info->extent; i < (ssize_t) extent; i++)
2341 {
2342 primitive_info[i].primitive=UndefinedPrimitive;
2343 primitive_info[i].text=(char *) NULL;
2344 }
2345 *mvg_info->primitive_info=primitive_info;
2346 *mvg_info->extent=extent;
2347 return(MagickTrue);
2348}
2349
2350static inline double GetDrawValue(const char *magick_restrict string,
2351 char **magick_restrict sentinel)
2352{
2353 char
2354 **magick_restrict q;
2355
2356 double
2357 value;
2358
2359 q=sentinel;
2360 value=InterpretLocaleValue(string,q);
2361 sentinel=q;
2362 return(value);
2363}
2364
2365static int MVGMacroCompare(const void *target,const void *source)
2366{
2367 const char
2368 *p,
2369 *q;
2370
2371 p=(const char *) target;
2372 q=(const char *) source;
2373 return(strcmp(p,q));
2374}
2375
2376static SplayTreeInfo *GetMVGMacros(const char *primitive,
2377 ExceptionInfo *exception)
2378{
2379 char
2380 *macro,
2381 *token;
2382
2383 const char
2384 *q;
2385
2386 size_t
2387 extent;
2388
2390 *macros;
2391
2392 /*
2393 Scan graphic primitives for definitions and classes.
2394 */
2395 if (primitive == (const char *) NULL)
2396 return((SplayTreeInfo *) NULL);
2397 macros=NewSplayTree(MVGMacroCompare,RelinquishMagickMemory,
2398 RelinquishMagickMemory);
2399 macro=AcquireString(primitive);
2400 token=AcquireString(primitive);
2401 extent=strlen(token)+MagickPathExtent;
2402 for (q=primitive; *q != '\0'; )
2403 {
2404 if (GetNextToken(q,&q,extent,token) < 1)
2405 break;
2406 if (*token == '\0')
2407 break;
2408 if (LocaleCompare("push",token) == 0)
2409 {
2410 const char
2411 *end,
2412 *start;
2413
2414 (void) GetNextToken(q,&q,extent,token);
2415 if (*q == '"')
2416 {
2417 char
2418 name[MagickPathExtent];
2419
2420 const char
2421 *p;
2422
2423 ssize_t
2424 n;
2425
2426 /*
2427 Named macro (e.g. push graphic-context "wheel").
2428 */
2429 (void) GetNextToken(q,&q,extent,token);
2430 start=q;
2431 end=q;
2432 (void) CopyMagickString(name,token,MagickPathExtent);
2433 n=1;
2434 for (p=q; *p != '\0'; )
2435 {
2436 if (GetNextToken(p,&p,extent,token) < 1)
2437 break;
2438 if (*token == '\0')
2439 break;
2440 if (LocaleCompare(token,"pop") == 0)
2441 {
2442 end=p-strlen(token)-1;
2443 n--;
2444 }
2445 if (LocaleCompare(token,"push") == 0)
2446 {
2447 if (n++ > MagickMaxRecursionDepth)
2448 {
2449 (void) ThrowMagickException(exception,GetMagickModule(),
2450 DrawError,"VectorGraphicsNestedTooDeeply","`%s'",token);
2451 break;
2452 }
2453 }
2454 if ((n == 0) && (end >= start))
2455 {
2456 size_t
2457 length=(size_t) (end-start);
2458
2459 /*
2460 Extract macro.
2461 */
2462 (void) GetNextToken(p,&p,extent,token);
2463 if (length > 0)
2464 {
2465 (void) CopyMagickString(macro,start,length);
2466 (void) AddValueToSplayTree(macros,ConstantString(name),
2467 ConstantString(macro));
2468 }
2469 break;
2470 }
2471 }
2472 }
2473 }
2474 }
2475 token=DestroyString(token);
2476 macro=DestroyString(macro);
2477 return(macros);
2478}
2479
2480static inline MagickBooleanType IsValidListChar(int c)
2481{
2482 if ((c >= '0') && (c <= '9'))
2483 return(MagickTrue);
2484 switch (c)
2485 {
2486 case '.':
2487 case '+':
2488 case '-':
2489 case ',':
2490 case ' ':
2491 case '\t':
2492 case '\r':
2493 case '\n':
2494 break;
2495 default:
2496 return(MagickFalse);
2497 }
2498 return(MagickTrue);
2499}
2500
2501static inline MagickBooleanType IsValidPoint(const char *point)
2502{
2503 char
2504 *p;
2505
2506 double
2507 value;
2508
2509 value=GetDrawValue(point,&p);
2510 return((fabs(value) < MagickEpsilon) && (p == point) ? MagickFalse :
2511 MagickTrue);
2512}
2513
2514static inline MagickBooleanType TracePoint(PrimitiveInfo *primitive_info,
2515 const PointInfo point)
2516{
2517 primitive_info->point=point;
2518 primitive_info->coordinates=1;
2519 primitive_info->closed_subpath=MagickFalse;
2520 primitive_info->text=(char *) NULL;
2521 return(MagickTrue);
2522}
2523
2524static MagickBooleanType RenderMVGContent(Image *image,
2525 const DrawInfo *draw_info,const size_t depth,ExceptionInfo *exception)
2526{
2527#define RenderImageTag "Render/Image"
2528
2530 affine,
2531 current;
2532
2533 char
2534 keyword[MagickPathExtent],
2535 geometry[MagickPathExtent],
2536 *next_token,
2537 pattern[MagickPathExtent],
2538 *primitive,
2539 *token;
2540
2541 const char
2542 *p,
2543 *q;
2544
2545 double
2546 angle,
2547 coordinates,
2548 cursor,
2549 factor,
2550 primitive_extent;
2551
2552 DrawInfo
2553 *clone_info,
2554 **graphic_context;
2555
2556 MagickBooleanType
2557 proceed;
2558
2559 MagickStatusType
2560 status;
2561
2562 MVGInfo
2563 mvg_info;
2564
2565 PointInfo
2566 point;
2567
2569 *primitive_info;
2570
2571 PrimitiveType
2572 primitive_type;
2573
2575 bounds;
2576
2577 size_t
2578 extent,
2579 number_points,
2580 number_stops;
2581
2583 *macros;
2584
2585 ssize_t
2586 classDepth = 0,
2587 defsDepth,
2588 i,
2589 j,
2590 k,
2591 n,
2592 symbolDepth,
2593 x;
2594
2595 StopInfo
2596 *stops;
2597
2599 metrics;
2600
2601 assert(image != (Image *) NULL);
2602 assert(image->signature == MagickCoreSignature);
2603 assert(draw_info != (DrawInfo *) NULL);
2604 assert(draw_info->signature == MagickCoreSignature);
2605 if (IsEventLogging() != MagickFalse)
2606 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2607 if (depth > MagickMaxRecursionDepth)
2608 ThrowBinaryException(DrawError,"VectorGraphicsNestedTooDeeply",
2609 image->filename);
2610 if ((draw_info->primitive == (char *) NULL) ||
2611 (*draw_info->primitive == '\0'))
2612 return(MagickFalse);
2613 if (draw_info->debug != MagickFalse)
2614 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"begin draw-image");
2615 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
2616 return(MagickFalse);
2617 if ((image->alpha_trait & BlendPixelTrait) == 0)
2618 {
2619 status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2620 if (status == MagickFalse)
2621 return(MagickFalse);
2622 }
2623 if ((*draw_info->primitive == '@') && (strlen(draw_info->primitive) > 1) &&
2624 (*(draw_info->primitive+1) != '-') && (depth == 0))
2625 primitive=FileToString(draw_info->primitive,~0UL,exception);
2626 else
2627 primitive=AcquireString(draw_info->primitive);
2628 if (primitive == (char *) NULL)
2629 return(MagickFalse);
2630 primitive_extent=(double) strlen(primitive);
2631 (void) SetImageArtifact(image,"mvg:vector-graphics",primitive);
2632 n=0;
2633 number_stops=0;
2634 stops=(StopInfo *) NULL;
2635 /*
2636 Allocate primitive info memory.
2637 */
2638 graphic_context=(DrawInfo **) AcquireMagickMemory(sizeof(*graphic_context));
2639 if (graphic_context == (DrawInfo **) NULL)
2640 {
2641 primitive=DestroyString(primitive);
2642 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2643 image->filename);
2644 }
2645 number_points=(size_t) PrimitiveExtentPad;
2646 primitive_info=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
2647 (number_points+1),sizeof(*primitive_info));
2648 if (primitive_info == (PrimitiveInfo *) NULL)
2649 {
2650 primitive=DestroyString(primitive);
2651 for ( ; n >= 0; n--)
2652 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
2653 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
2654 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2655 image->filename);
2656 }
2657 (void) memset(primitive_info,0,(size_t) (number_points+1)*
2658 sizeof(*primitive_info));
2659 (void) memset(&mvg_info,0,sizeof(mvg_info));
2660 mvg_info.primitive_info=(&primitive_info);
2661 mvg_info.extent=(&number_points);
2662 mvg_info.exception=exception;
2663 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,draw_info);
2664 graphic_context[n]->viewbox=image->page;
2665 if ((image->page.width == 0) || (image->page.height == 0))
2666 {
2667 graphic_context[n]->viewbox.width=image->columns;
2668 graphic_context[n]->viewbox.height=image->rows;
2669 }
2670 token=AcquireString(primitive);
2671 extent=strlen(token)+MagickPathExtent;
2672 defsDepth=0;
2673 symbolDepth=0;
2674 cursor=0.0;
2675 macros=GetMVGMacros(primitive,exception);
2676 status=MagickTrue;
2677 for (q=primitive; *q != '\0'; )
2678 {
2679 /*
2680 Interpret graphic primitive.
2681 */
2682 if (GetNextToken(q,&q,MagickPathExtent,keyword) < 1)
2683 break;
2684 if (*keyword == '\0')
2685 break;
2686 if (*keyword == '#')
2687 {
2688 /*
2689 Comment.
2690 */
2691 while ((*q != '\n') && (*q != '\0'))
2692 q++;
2693 continue;
2694 }
2695 p=q-strlen(keyword)-1;
2696 primitive_type=UndefinedPrimitive;
2697 current=graphic_context[n]->affine;
2698 GetAffineMatrix(&affine);
2699 *token='\0';
2700 switch (*keyword)
2701 {
2702 case ';':
2703 break;
2704 case 'a':
2705 case 'A':
2706 {
2707 if (LocaleCompare("affine",keyword) == 0)
2708 {
2709 (void) GetNextToken(q,&q,extent,token);
2710 affine.sx=GetDrawValue(token,&next_token);
2711 if (token == next_token)
2712 ThrowPointExpectedException(token,exception);
2713 (void) GetNextToken(q,&q,extent,token);
2714 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
2715 ThrowPointExpectedException(token,exception);
2716 if (*token == ',')
2717 (void) GetNextToken(q,&q,extent,token);
2718 affine.ry=GetDrawValue(token,&next_token);
2719 if (token == next_token)
2720 ThrowPointExpectedException(token,exception);
2721 (void) GetNextToken(q,&q,extent,token);
2722 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
2723 ThrowPointExpectedException(token,exception);
2724 if (*token == ',')
2725 (void) GetNextToken(q,&q,extent,token);
2726 affine.rx=GetDrawValue(token,&next_token);
2727 if (token == next_token)
2728 ThrowPointExpectedException(token,exception);
2729 (void) GetNextToken(q,&q,extent,token);
2730 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
2731 ThrowPointExpectedException(token,exception);
2732 if (*token == ',')
2733 (void) GetNextToken(q,&q,extent,token);
2734 affine.sy=GetDrawValue(token,&next_token);
2735 if (token == next_token)
2736 ThrowPointExpectedException(token,exception);
2737 (void) GetNextToken(q,&q,extent,token);
2738 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
2739 ThrowPointExpectedException(token,exception);
2740 if (*token == ',')
2741 (void) GetNextToken(q,&q,extent,token);
2742 affine.tx=GetDrawValue(token,&next_token);
2743 if (token == next_token)
2744 ThrowPointExpectedException(token,exception);
2745 (void) GetNextToken(q,&q,extent,token);
2746 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
2747 ThrowPointExpectedException(token,exception);
2748 if (*token == ',')
2749 (void) GetNextToken(q,&q,extent,token);
2750 affine.ty=GetDrawValue(token,&next_token);
2751 if (token == next_token)
2752 ThrowPointExpectedException(token,exception);
2753 break;
2754 }
2755 if (LocaleCompare("alpha",keyword) == 0)
2756 {
2757 primitive_type=AlphaPrimitive;
2758 break;
2759 }
2760 if (LocaleCompare("arc",keyword) == 0)
2761 {
2762 primitive_type=ArcPrimitive;
2763 break;
2764 }
2765 status=MagickFalse;
2766 break;
2767 }
2768 case 'b':
2769 case 'B':
2770 {
2771 if (LocaleCompare("bezier",keyword) == 0)
2772 {
2773 primitive_type=BezierPrimitive;
2774 break;
2775 }
2776 if (LocaleCompare("border-color",keyword) == 0)
2777 {
2778 (void) GetNextToken(q,&q,extent,token);
2779 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
2780 &graphic_context[n]->border_color,exception);
2781 break;
2782 }
2783 status=MagickFalse;
2784 break;
2785 }
2786 case 'c':
2787 case 'C':
2788 {
2789 if (LocaleCompare("class",keyword) == 0)
2790 {
2791 const char
2792 *mvg_class;
2793
2794 (void) GetNextToken(q,&q,extent,token);
2795 if ((*token == '\0') || (*token == ';'))
2796 {
2797 status=MagickFalse;
2798 break;
2799 }
2800 /*
2801 Identify recursion.
2802 */
2803 for (i=0; i <= n; i++)
2804 if (LocaleCompare(token,graphic_context[i]->id) == 0)
2805 break;
2806 if (i <= n)
2807 break;
2808 if (classDepth++ > MagickMaxRecursionDepth)
2809 {
2810 (void) ThrowMagickException(exception,GetMagickModule(),
2811 DrawError,"VectorGraphicsNestedTooDeeply","`%s'",token);
2812 status=MagickFalse;
2813 break;
2814 }
2815 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
2816 if ((graphic_context[n]->render != MagickFalse) &&
2817 (mvg_class != (const char *) NULL) && (p > primitive))
2818 {
2819 char
2820 *elements;
2821
2822 ssize_t
2823 offset;
2824
2825 /*
2826 Inject class elements in stream.
2827 */
2828 (void) CloneString(&graphic_context[n]->id,token);
2829 offset=(ssize_t) (p-primitive);
2830 elements=AcquireString(primitive);
2831 elements[offset]='\0';
2832 (void) ConcatenateString(&elements,mvg_class);
2833 (void) ConcatenateString(&elements,"\n");
2834 (void) ConcatenateString(&elements,q);
2835 primitive=DestroyString(primitive);
2836 primitive=elements;
2837 q=primitive+offset;
2838 }
2839 break;
2840 }
2841 if (LocaleCompare("clip-path",keyword) == 0)
2842 {
2843 const char
2844 *clip_path;
2845
2846 /*
2847 Take a node from within the MVG document, and duplicate it here.
2848 */
2849 (void) GetNextToken(q,&q,extent,token);
2850 if (*token == '\0')
2851 {
2852 status=MagickFalse;
2853 break;
2854 }
2855 (void) CloneString(&graphic_context[n]->clip_mask,token);
2856 clip_path=(const char *) GetValueFromSplayTree(macros,token);
2857 if (clip_path != (const char *) NULL)
2858 {
2859 if (graphic_context[n]->clipping_mask != (Image *) NULL)
2860 graphic_context[n]->clipping_mask=
2861 DestroyImage(graphic_context[n]->clipping_mask);
2862 graphic_context[n]->clipping_mask=DrawClippingMask(image,
2863 graphic_context[n],token,clip_path,exception);
2864 if (graphic_context[n]->compliance != SVGCompliance)
2865 {
2866 clip_path=(const char *) GetValueFromSplayTree(macros,
2867 graphic_context[n]->clip_mask);
2868 if (clip_path != (const char *) NULL)
2869 (void) SetImageArtifact(image,
2870 graphic_context[n]->clip_mask,clip_path);
2871 status&=(MagickStatusType) DrawClipPath(image,
2872 graphic_context[n],graphic_context[n]->clip_mask,
2873 exception);
2874 }
2875 }
2876 break;
2877 }
2878 if (LocaleCompare("clip-rule",keyword) == 0)
2879 {
2880 ssize_t
2881 fill_rule;
2882
2883 (void) GetNextToken(q,&q,extent,token);
2884 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
2885 token);
2886 if (fill_rule == -1)
2887 {
2888 status=MagickFalse;
2889 break;
2890 }
2891 graphic_context[n]->fill_rule=(FillRule) fill_rule;
2892 break;
2893 }
2894 if (LocaleCompare("clip-units",keyword) == 0)
2895 {
2896 ssize_t
2897 clip_units;
2898
2899 (void) GetNextToken(q,&q,extent,token);
2900 clip_units=ParseCommandOption(MagickClipPathOptions,MagickFalse,
2901 token);
2902 if (clip_units == -1)
2903 {
2904 status=MagickFalse;
2905 break;
2906 }
2907 graphic_context[n]->clip_units=(ClipPathUnits) clip_units;
2908 if (clip_units == ObjectBoundingBox)
2909 {
2910 GetAffineMatrix(&current);
2911 affine.sx=draw_info->bounds.x2;
2912 affine.sy=draw_info->bounds.y2;
2913 affine.tx=draw_info->bounds.x1;
2914 affine.ty=draw_info->bounds.y1;
2915 break;
2916 }
2917 break;
2918 }
2919 if (LocaleCompare("circle",keyword) == 0)
2920 {
2921 primitive_type=CirclePrimitive;
2922 break;
2923 }
2924 if (LocaleCompare("color",keyword) == 0)
2925 {
2926 primitive_type=ColorPrimitive;
2927 break;
2928 }
2929 if (LocaleCompare("compliance",keyword) == 0)
2930 {
2931 /*
2932 MVG compliance associates a clipping mask with an image; SVG
2933 compliance associates a clipping mask with a graphics context.
2934 */
2935 (void) GetNextToken(q,&q,extent,token);
2936 graphic_context[n]->compliance=(ComplianceType) ParseCommandOption(
2937 MagickComplianceOptions,MagickFalse,token);
2938 break;
2939 }
2940 if (LocaleCompare("currentColor",keyword) == 0)
2941 {
2942 (void) GetNextToken(q,&q,extent,token);
2943 break;
2944 }
2945 status=MagickFalse;
2946 break;
2947 }
2948 case 'd':
2949 case 'D':
2950 {
2951 if (LocaleCompare("decorate",keyword) == 0)
2952 {
2953 ssize_t
2954 decorate;
2955
2956 (void) GetNextToken(q,&q,extent,token);
2957 decorate=ParseCommandOption(MagickDecorateOptions,MagickFalse,
2958 token);
2959 if (decorate == -1)
2960 {
2961 status=MagickFalse;
2962 break;
2963 }
2964 graphic_context[n]->decorate=(DecorationType) decorate;
2965 break;
2966 }
2967 if (LocaleCompare("density",keyword) == 0)
2968 {
2969 (void) GetNextToken(q,&q,extent,token);
2970 (void) CloneString(&graphic_context[n]->density,token);
2971 break;
2972 }
2973 if (LocaleCompare("direction",keyword) == 0)
2974 {
2975 ssize_t
2976 direction;
2977
2978 (void) GetNextToken(q,&q,extent,token);
2979 direction=ParseCommandOption(MagickDirectionOptions,MagickFalse,
2980 token);
2981 if (direction == -1)
2982 status=MagickFalse;
2983 else
2984 graphic_context[n]->direction=(DirectionType) direction;
2985 break;
2986 }
2987 status=MagickFalse;
2988 break;
2989 }
2990 case 'e':
2991 case 'E':
2992 {
2993 if (LocaleCompare("ellipse",keyword) == 0)
2994 {
2995 primitive_type=EllipsePrimitive;
2996 break;
2997 }
2998 if (LocaleCompare("encoding",keyword) == 0)
2999 {
3000 (void) GetNextToken(q,&q,extent,token);
3001 (void) CloneString(&graphic_context[n]->encoding,token);
3002 break;
3003 }
3004 status=MagickFalse;
3005 break;
3006 }
3007 case 'f':
3008 case 'F':
3009 {
3010 if (LocaleCompare("fill",keyword) == 0)
3011 {
3012 const char
3013 *mvg_class;
3014
3015 (void) GetNextToken(q,&q,extent,token);
3016 if (graphic_context[n]->clip_path != MagickFalse)
3017 break;
3018 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
3019 if (mvg_class != (const char *) NULL)
3020 {
3021 (void) DrawPatternPath(image,draw_info,mvg_class,
3022 &graphic_context[n]->fill_pattern,exception);
3023 break;
3024 }
3025 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
3026 if (GetImageArtifact(image,pattern) != (const char *) NULL)
3027 {
3028 (void) DrawPatternPath(image,draw_info,token,
3029 &graphic_context[n]->fill_pattern,exception);
3030 break;
3031 }
3032 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3033 &graphic_context[n]->fill,exception);
3034 if (graphic_context[n]->fill_alpha != (double) OpaqueAlpha)
3035 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3036 break;
3037 }
3038 if (LocaleCompare("fill-opacity",keyword) == 0)
3039 {
3040 double
3041 opacity;
3042
3043 (void) GetNextToken(q,&q,extent,token);
3044 if (graphic_context[n]->clip_path != MagickFalse)
3045 break;
3046 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3047 opacity=MagickMin(MagickMax(factor*
3048 GetDrawValue(token,&next_token),0.0),1.0);
3049 if (token == next_token)
3050 ThrowPointExpectedException(token,exception);
3051 if (graphic_context[n]->compliance == SVGCompliance)
3052 graphic_context[n]->fill_alpha*=opacity;
3053 else
3054 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3055 if (graphic_context[n]->fill.alpha != (double) TransparentAlpha)
3056 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3057 else
3058 graphic_context[n]->fill.alpha=(MagickRealType)
3059 ClampToQuantum((double) QuantumRange*opacity);
3060 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3061 break;
3062 }
3063 if (LocaleCompare("fill-rule",keyword) == 0)
3064 {
3065 ssize_t
3066 fill_rule;
3067
3068 (void) GetNextToken(q,&q,extent,token);
3069 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
3070 token);
3071 if (fill_rule == -1)
3072 {
3073 status=MagickFalse;
3074 break;
3075 }
3076 graphic_context[n]->fill_rule=(FillRule) fill_rule;
3077 break;
3078 }
3079 if (LocaleCompare("font",keyword) == 0)
3080 {
3081 (void) GetNextToken(q,&q,extent,token);
3082 (void) CloneString(&graphic_context[n]->font,token);
3083 if (LocaleCompare("none",token) == 0)
3084 graphic_context[n]->font=(char *) RelinquishMagickMemory(
3085 graphic_context[n]->font);
3086 break;
3087 }
3088 if (LocaleCompare("font-family",keyword) == 0)
3089 {
3090 (void) GetNextToken(q,&q,extent,token);
3091 (void) CloneString(&graphic_context[n]->family,token);
3092 break;
3093 }
3094 if (LocaleCompare("font-size",keyword) == 0)
3095 {
3096 (void) GetNextToken(q,&q,extent,token);
3097 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3098 if (token == next_token)
3099 ThrowPointExpectedException(token,exception);
3100 break;
3101 }
3102 if (LocaleCompare("font-stretch",keyword) == 0)
3103 {
3104 ssize_t
3105 stretch;
3106
3107 (void) GetNextToken(q,&q,extent,token);
3108 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3109 if (stretch == -1)
3110 {
3111 status=MagickFalse;
3112 break;
3113 }
3114 graphic_context[n]->stretch=(StretchType) stretch;
3115 break;
3116 }
3117 if (LocaleCompare("font-style",keyword) == 0)
3118 {
3119 ssize_t
3120 style;
3121
3122 (void) GetNextToken(q,&q,extent,token);
3123 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3124 if (style == -1)
3125 {
3126 status=MagickFalse;
3127 break;
3128 }
3129 graphic_context[n]->style=(StyleType) style;
3130 break;
3131 }
3132 if (LocaleCompare("font-weight",keyword) == 0)
3133 {
3134 ssize_t
3135 weight;
3136
3137 (void) GetNextToken(q,&q,extent,token);
3138 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3139 if (weight == -1)
3140 weight=(ssize_t) StringToUnsignedLong(token);
3141 graphic_context[n]->weight=(size_t) weight;
3142 break;
3143 }
3144 status=MagickFalse;
3145 break;
3146 }
3147 case 'g':
3148 case 'G':
3149 {
3150 if (LocaleCompare("gradient-units",keyword) == 0)
3151 {
3152 (void) GetNextToken(q,&q,extent,token);
3153 break;
3154 }
3155 if (LocaleCompare("gravity",keyword) == 0)
3156 {
3157 ssize_t
3158 gravity;
3159
3160 (void) GetNextToken(q,&q,extent,token);
3161 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3162 if (gravity == -1)
3163 {
3164 status=MagickFalse;
3165 break;
3166 }
3167 graphic_context[n]->gravity=(GravityType) gravity;
3168 break;
3169 }
3170 status=MagickFalse;
3171 break;
3172 }
3173 case 'i':
3174 case 'I':
3175 {
3176 if (LocaleCompare("image",keyword) == 0)
3177 {
3178 ssize_t
3179 compose;
3180
3181 primitive_type=ImagePrimitive;
3182 (void) GetNextToken(q,&q,extent,token);
3183 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3184 if (compose == -1)
3185 {
3186 status=MagickFalse;
3187 break;
3188 }
3189 graphic_context[n]->compose=(CompositeOperator) compose;
3190 break;
3191 }
3192 if (LocaleCompare("interline-spacing",keyword) == 0)
3193 {
3194 (void) GetNextToken(q,&q,extent,token);
3195 graphic_context[n]->interline_spacing=GetDrawValue(token,
3196 &next_token);
3197 if (token == next_token)
3198 ThrowPointExpectedException(token,exception);
3199 break;
3200 }
3201 if (LocaleCompare("interword-spacing",keyword) == 0)
3202 {
3203 (void) GetNextToken(q,&q,extent,token);
3204 graphic_context[n]->interword_spacing=GetDrawValue(token,
3205 &next_token);
3206 if (token == next_token)
3207 ThrowPointExpectedException(token,exception);
3208 break;
3209 }
3210 status=MagickFalse;
3211 break;
3212 }
3213 case 'k':
3214 case 'K':
3215 {
3216 if (LocaleCompare("kerning",keyword) == 0)
3217 {
3218 (void) GetNextToken(q,&q,extent,token);
3219 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3220 if (token == next_token)
3221 ThrowPointExpectedException(token,exception);
3222 break;
3223 }
3224 status=MagickFalse;
3225 break;
3226 }
3227 case 'l':
3228 case 'L':
3229 {
3230 if (LocaleCompare("letter-spacing",keyword) == 0)
3231 {
3232 (void) GetNextToken(q,&q,extent,token);
3233 if (IsValidPoint(token) == MagickFalse)
3234 break;
3235 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3236 clone_info->text=AcquireString(" ");
3237 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,&metrics,
3238 exception);
3239 graphic_context[n]->kerning=metrics.width*
3240 GetDrawValue(token,&next_token);
3241 clone_info=DestroyDrawInfo(clone_info);
3242 if (token == next_token)
3243 ThrowPointExpectedException(token,exception);
3244 break;
3245 }
3246 if (LocaleCompare("line",keyword) == 0)
3247 {
3248 primitive_type=LinePrimitive;
3249 break;
3250 }
3251 status=MagickFalse;
3252 break;
3253 }
3254 case 'm':
3255 case 'M':
3256 {
3257 if (LocaleCompare("mask",keyword) == 0)
3258 {
3259 const char
3260 *mask_path;
3261
3262 /*
3263 Take a node from within the MVG document, and duplicate it here.
3264 */
3265 (void) GetNextToken(q,&q,extent,token);
3266 mask_path=(const char *) GetValueFromSplayTree(macros,token);
3267 if (mask_path != (const char *) NULL)
3268 {
3269 if (graphic_context[n]->composite_mask != (Image *) NULL)
3270 graphic_context[n]->composite_mask=
3271 DestroyImage(graphic_context[n]->composite_mask);
3272 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3273 graphic_context[n],token,mask_path,exception);
3274 if (graphic_context[n]->compliance != SVGCompliance)
3275 status=SetImageMask(image,CompositePixelMask,
3276 graphic_context[n]->composite_mask,exception);
3277 }
3278 break;
3279 }
3280 status=MagickFalse;
3281 break;
3282 }
3283 case 'o':
3284 case 'O':
3285 {
3286 if (LocaleCompare("offset",keyword) == 0)
3287 {
3288 (void) GetNextToken(q,&q,extent,token);
3289 break;
3290 }
3291 if (LocaleCompare("opacity",keyword) == 0)
3292 {
3293 double
3294 opacity;
3295
3296 (void) GetNextToken(q,&q,extent,token);
3297 if (graphic_context[n]->clip_path != MagickFalse)
3298 break;
3299 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3300 opacity=MagickMin(MagickMax(factor*
3301 GetDrawValue(token,&next_token),0.0),1.0);
3302 if (token == next_token)
3303 ThrowPointExpectedException(token,exception);
3304 if (graphic_context[n]->compliance == SVGCompliance)
3305 {
3306 graphic_context[n]->fill_alpha*=opacity;
3307 graphic_context[n]->stroke_alpha*=opacity;
3308 }
3309 else
3310 {
3311 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3312 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3313 }
3314 if (graphic_context[n]->fill.alpha != (double) TransparentAlpha)
3315 {
3316 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3317 graphic_context[n]->stroke.alpha=
3318 graphic_context[n]->stroke_alpha;
3319 }
3320 else
3321 {
3322 graphic_context[n]->fill.alpha=(MagickRealType)
3323 ClampToQuantum((double) QuantumRange*opacity);
3324 graphic_context[n]->stroke.alpha=(MagickRealType)
3325 ClampToQuantum((double) QuantumRange*opacity);
3326 }
3327 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3328 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3329 break;
3330 }
3331 status=MagickFalse;
3332 break;
3333 }
3334 case 'p':
3335 case 'P':
3336 {
3337 if (LocaleCompare("path",keyword) == 0)
3338 {
3339 primitive_type=PathPrimitive;
3340 break;
3341 }
3342 if (LocaleCompare("point",keyword) == 0)
3343 {
3344 primitive_type=PointPrimitive;
3345 break;
3346 }
3347 if (LocaleCompare("polyline",keyword) == 0)
3348 {
3349 primitive_type=PolylinePrimitive;
3350 break;
3351 }
3352 if (LocaleCompare("polygon",keyword) == 0)
3353 {
3354 primitive_type=PolygonPrimitive;
3355 break;
3356 }
3357 if (LocaleCompare("pop",keyword) == 0)
3358 {
3359 if (GetNextToken(q,&q,extent,token) < 1)
3360 break;
3361 if (LocaleCompare("class",token) == 0)
3362 break;
3363 if (LocaleCompare("clip-path",token) == 0)
3364 break;
3365 if (LocaleCompare("defs",token) == 0)
3366 {
3367 defsDepth--;
3368 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3369 MagickTrue;
3370 break;
3371 }
3372 if (LocaleCompare("gradient",token) == 0)
3373 break;
3374 if (LocaleCompare("graphic-context",token) == 0)
3375 {
3376 if (n <= 0)
3377 {
3378 (void) ThrowMagickException(exception,GetMagickModule(),
3379 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
3380 status=MagickFalse;
3381 n=0;
3382 break;
3383 }
3384 if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3385 (graphic_context[n]->compliance != SVGCompliance))
3386 if (LocaleCompare(graphic_context[n]->clip_mask,
3387 graphic_context[n-1]->clip_mask) != 0)
3388 status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3389 exception);
3390 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3391 n--;
3392 break;
3393 }
3394 if (LocaleCompare("mask",token) == 0)
3395 break;
3396 if (LocaleCompare("pattern",token) == 0)
3397 break;
3398 if (LocaleCompare("symbol",token) == 0)
3399 {
3400 symbolDepth--;
3401 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3402 MagickTrue;
3403 break;
3404 }
3405 status=MagickFalse;
3406 break;
3407 }
3408 if (LocaleCompare("push",keyword) == 0)
3409 {
3410 if (GetNextToken(q,&q,extent,token) < 1)
3411 break;
3412 if (LocaleCompare("class",token) == 0)
3413 {
3414 /*
3415 Class context.
3416 */
3417 for (p=q; *q != '\0'; )
3418 {
3419 if (GetNextToken(q,&q,extent,token) < 1)
3420 break;
3421 if (LocaleCompare(token,"pop") != 0)
3422 continue;
3423 (void) GetNextToken(q,(const char **) NULL,extent,token);
3424 if (LocaleCompare(token,"class") != 0)
3425 continue;
3426 break;
3427 }
3428 (void) GetNextToken(q,&q,extent,token);
3429 break;
3430 }
3431 if (LocaleCompare("clip-path",token) == 0)
3432 {
3433 (void) GetNextToken(q,&q,extent,token);
3434 for (p=q; *q != '\0'; )
3435 {
3436 if (GetNextToken(q,&q,extent,token) < 1)
3437 break;
3438 if (LocaleCompare(token,"pop") != 0)
3439 continue;
3440 (void) GetNextToken(q,(const char **) NULL,extent,token);
3441 if (LocaleCompare(token,"clip-path") != 0)
3442 continue;
3443 break;
3444 }
3445 if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3446 {
3447 status=MagickFalse;
3448 break;
3449 }
3450 (void) GetNextToken(q,&q,extent,token);
3451 break;
3452 }
3453 if (LocaleCompare("defs",token) == 0)
3454 {
3455 defsDepth++;
3456 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3457 MagickTrue;
3458 break;
3459 }
3460 if (LocaleCompare("gradient",token) == 0)
3461 {
3462 char
3463 key[2*MagickPathExtent],
3464 name[MagickPathExtent],
3465 type[MagickPathExtent];
3466
3468 segment;
3469
3470 (void) GetNextToken(q,&q,extent,token);
3471 (void) CopyMagickString(name,token,MagickPathExtent);
3472 (void) GetNextToken(q,&q,extent,token);
3473 (void) CopyMagickString(type,token,MagickPathExtent);
3474 (void) GetNextToken(q,&q,extent,token);
3475 segment.x1=GetDrawValue(token,&next_token);
3476 if (token == next_token)
3477 ThrowPointExpectedException(token,exception);
3478 (void) GetNextToken(q,&q,extent,token);
3479 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3480 ThrowPointExpectedException(token,exception);
3481 if (*token == ',')
3482 (void) GetNextToken(q,&q,extent,token);
3483 segment.y1=GetDrawValue(token,&next_token);
3484 if (token == next_token)
3485 ThrowPointExpectedException(token,exception);
3486 (void) GetNextToken(q,&q,extent,token);
3487 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3488 ThrowPointExpectedException(token,exception);
3489 if (*token == ',')
3490 (void) GetNextToken(q,&q,extent,token);
3491 segment.x2=GetDrawValue(token,&next_token);
3492 if (token == next_token)
3493 ThrowPointExpectedException(token,exception);
3494 (void) GetNextToken(q,&q,extent,token);
3495 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3496 ThrowPointExpectedException(token,exception);
3497 if (*token == ',')
3498 (void) GetNextToken(q,&q,extent,token);
3499 segment.y2=GetDrawValue(token,&next_token);
3500 if (token == next_token)
3501 ThrowPointExpectedException(token,exception);
3502 if (LocaleCompare(type,"radial") == 0)
3503 {
3504 (void) GetNextToken(q,&q,extent,token);
3505 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3506 ThrowPointExpectedException(token,exception);
3507 if (*token == ',')
3508 (void) GetNextToken(q,&q,extent,token);
3509 }
3510 for (p=q; *q != '\0'; )
3511 {
3512 if (GetNextToken(q,&q,extent,token) < 1)
3513 break;
3514 if (LocaleCompare(token,"pop") != 0)
3515 continue;
3516 (void) GetNextToken(q,(const char **) NULL,extent,token);
3517 if (LocaleCompare(token,"gradient") != 0)
3518 continue;
3519 break;
3520 }
3521 if ((q == (char *) NULL) || (*q == '\0') ||
3522 (p == (char *) NULL) || ((q-4) < p) ||
3523 ((size_t) (q-p+4+1) > extent))
3524 {
3525 status=MagickFalse;
3526 break;
3527 }
3528 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3529 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3530 graphic_context[n]->affine.ry*segment.y1+
3531 graphic_context[n]->affine.tx;
3532 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3533 graphic_context[n]->affine.sy*segment.y1+
3534 graphic_context[n]->affine.ty;
3535 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3536 graphic_context[n]->affine.ry*segment.y2+
3537 graphic_context[n]->affine.tx;
3538 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3539 graphic_context[n]->affine.sy*segment.y2+
3540 graphic_context[n]->affine.ty;
3541 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3542 (void) SetImageArtifact(image,key,token);
3543 (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
3544 (void) SetImageArtifact(image,key,type);
3545 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3546 name);
3547 (void) FormatLocaleString(geometry,MagickPathExtent,
3548 "%gx%g%+.15g%+.15g",
3549 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3550 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3551 bounds.x1,bounds.y1);
3552 (void) SetImageArtifact(image,key,geometry);
3553 (void) GetNextToken(q,&q,extent,token);
3554 break;
3555 }
3556 if (LocaleCompare("graphic-context",token) == 0)
3557 {
3558 n++;
3559 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3560 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3561 if (graphic_context == (DrawInfo **) NULL)
3562 {
3563 (void) ThrowMagickException(exception,GetMagickModule(),
3564 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3565 image->filename);
3566 status=MagickFalse;
3567 break;
3568 }
3569 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3570 graphic_context[n-1]);
3571 if (*q == '"')
3572 {
3573 (void) GetNextToken(q,&q,extent,token);
3574 (void) CloneString(&graphic_context[n]->id,token);
3575 }
3576 if (n > MagickMaxRecursionDepth)
3577 {
3578 (void) ThrowMagickException(exception,GetMagickModule(),
3579 DrawError,"VectorGraphicsNestedTooDeeply","`%s'",
3580 image->filename);
3581 status=MagickFalse;
3582 }
3583 break;
3584 }
3585 if (LocaleCompare("mask",token) == 0)
3586 {
3587 (void) GetNextToken(q,&q,extent,token);
3588 break;
3589 }
3590 if (LocaleCompare("pattern",token) == 0)
3591 {
3592 char
3593 key[2*MagickPathExtent],
3594 name[MagickPathExtent];
3595
3597 region;
3598
3599 (void) GetNextToken(q,&q,extent,token);
3600 (void) CopyMagickString(name,token,MagickPathExtent);
3601 (void) GetNextToken(q,&q,extent,token);
3602 region.x=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3603 &next_token)-0.5));
3604 if (token == next_token)
3605 ThrowPointExpectedException(token,exception);
3606 (void) GetNextToken(q,&q,extent,token);
3607 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3608 ThrowPointExpectedException(token,exception);
3609 if (*token == ',')
3610 (void) GetNextToken(q,&q,extent,token);
3611 region.y=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3612 &next_token)-0.5));
3613 if (token == next_token)
3614 ThrowPointExpectedException(token,exception);
3615 (void) GetNextToken(q,&q,extent,token);
3616 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3617 ThrowPointExpectedException(token,exception);
3618 if (*token == ',')
3619 (void) GetNextToken(q,&q,extent,token);
3620 region.width=CastDoubleToSizeT(floor(GetDrawValue(token,
3621 &next_token)+0.5));
3622 if (token == next_token)
3623 ThrowPointExpectedException(token,exception);
3624 (void) GetNextToken(q,&q,extent,token);
3625 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3626 ThrowPointExpectedException(token,exception);
3627 if (*token == ',')
3628 (void) GetNextToken(q,&q,extent,token);
3629 region.height=CastDoubleToSizeT(GetDrawValue(token,
3630 &next_token)+0.5);
3631 if (token == next_token)
3632 ThrowPointExpectedException(token,exception);
3633 for (p=q; *q != '\0'; )
3634 {
3635 if (GetNextToken(q,&q,extent,token) < 1)
3636 break;
3637 if (LocaleCompare(token,"pop") != 0)
3638 continue;
3639 (void) GetNextToken(q,(const char **) NULL,extent,token);
3640 if (LocaleCompare(token,"pattern") != 0)
3641 continue;
3642 break;
3643 }
3644 if ((q == (char *) NULL) || (p == (char *) NULL) ||
3645 ((q-4) < p) || ((size_t) (q-p+4+1) > extent))
3646 {
3647 status=MagickFalse;
3648 break;
3649 }
3650 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3651 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3652 (void) SetImageArtifact(image,key,token);
3653 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3654 name);
3655 (void) FormatLocaleString(geometry,MagickPathExtent,
3656 "%.20gx%.20g%+.20g%+.20g",(double) region.width,(double)
3657 region.height,(double) region.x,(double) region.y);
3658 (void) SetImageArtifact(image,key,geometry);
3659 (void) GetNextToken(q,&q,extent,token);
3660 break;
3661 }
3662 if (LocaleCompare("symbol",token) == 0)
3663 {
3664 symbolDepth++;
3665 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3666 MagickTrue;
3667 break;
3668 }
3669 status=MagickFalse;
3670 break;
3671 }
3672 status=MagickFalse;
3673 break;
3674 }
3675 case 'r':
3676 case 'R':
3677 {
3678 if (LocaleCompare("rectangle",keyword) == 0)
3679 {
3680 primitive_type=RectanglePrimitive;
3681 break;
3682 }
3683 if (LocaleCompare("rotate",keyword) == 0)
3684 {
3685 (void) GetNextToken(q,&q,extent,token);
3686 angle=GetDrawValue(token,&next_token);
3687 if (token == next_token)
3688 ThrowPointExpectedException(token,exception);
3689 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3690 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3691 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3692 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3693 break;
3694 }
3695 if (LocaleCompare("roundRectangle",keyword) == 0)
3696 {
3697 primitive_type=RoundRectanglePrimitive;
3698 break;
3699 }
3700 status=MagickFalse;
3701 break;
3702 }
3703 case 's':
3704 case 'S':
3705 {
3706 if (LocaleCompare("scale",keyword) == 0)
3707 {
3708 (void) GetNextToken(q,&q,extent,token);
3709 affine.sx=GetDrawValue(token,&next_token);
3710 if (token == next_token)
3711 ThrowPointExpectedException(token,exception);
3712 (void) GetNextToken(q,&q,extent,token);
3713 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3714 ThrowPointExpectedException(token,exception);
3715 if (*token == ',')
3716 (void) GetNextToken(q,&q,extent,token);
3717 affine.sy=GetDrawValue(token,&next_token);
3718 if (token == next_token)
3719 ThrowPointExpectedException(token,exception);
3720 break;
3721 }
3722 if (LocaleCompare("skewX",keyword) == 0)
3723 {
3724 (void) GetNextToken(q,&q,extent,token);
3725 angle=GetDrawValue(token,&next_token);
3726 if (token == next_token)
3727 ThrowPointExpectedException(token,exception);
3728 affine.ry=sin(DegreesToRadians(angle));
3729 break;
3730 }
3731 if (LocaleCompare("skewY",keyword) == 0)
3732 {
3733 (void) GetNextToken(q,&q,extent,token);
3734 angle=GetDrawValue(token,&next_token);
3735 if (token == next_token)
3736 ThrowPointExpectedException(token,exception);
3737 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3738 break;
3739 }
3740 if (LocaleCompare("stop-color",keyword) == 0)
3741 {
3742 PixelInfo
3743 stop_color;
3744
3745 number_stops++;
3746 if (number_stops == 1)
3747 stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
3748 else
3749 if (number_stops > 2)
3750 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3751 sizeof(*stops));
3752 if (stops == (StopInfo *) NULL)
3753 {
3754 (void) ThrowMagickException(exception,GetMagickModule(),
3755 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3756 image->filename);
3757 status=MagickFalse;
3758 break;
3759 }
3760 (void) GetNextToken(q,&q,extent,token);
3761 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3762 &stop_color,exception);
3763 stops[number_stops-1].color=stop_color;
3764 (void) GetNextToken(q,&q,extent,token);
3765 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3766 stops[number_stops-1].offset=factor*GetDrawValue(token,
3767 &next_token);
3768 if (token == next_token)
3769 ThrowPointExpectedException(token,exception);
3770 break;
3771 }
3772 if (LocaleCompare("stroke",keyword) == 0)
3773 {
3774 const char
3775 *mvg_class;
3776
3777 (void) GetNextToken(q,&q,extent,token);
3778 if (graphic_context[n]->clip_path != MagickFalse)
3779 break;
3780 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
3781 if (mvg_class != (const char *) NULL)
3782 {
3783 (void) DrawPatternPath(image,draw_info,mvg_class,
3784 &graphic_context[n]->stroke_pattern,exception);
3785 break;
3786 }
3787 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
3788 if (GetImageArtifact(image,pattern) != (const char *) NULL)
3789 {
3790 (void) DrawPatternPath(image,draw_info,token,
3791 &graphic_context[n]->stroke_pattern,exception);
3792 break;
3793 }
3794 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3795 &graphic_context[n]->stroke,exception);
3796 if (graphic_context[n]->stroke_alpha != (double) OpaqueAlpha)
3797 graphic_context[n]->stroke.alpha=
3798 graphic_context[n]->stroke_alpha;
3799 break;
3800 }
3801 if (LocaleCompare("stroke-antialias",keyword) == 0)
3802 {
3803 (void) GetNextToken(q,&q,extent,token);
3804 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3805 MagickTrue : MagickFalse;
3806 break;
3807 }
3808 if (LocaleCompare("stroke-dasharray",keyword) == 0)
3809 {
3810 if (graphic_context[n]->dash_pattern != (double *) NULL)
3811 graphic_context[n]->dash_pattern=(double *)
3812 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3813 if (IsValidPoint(q) != MagickFalse)
3814 {
3815 const char
3816 *r;
3817
3818 r=q;
3819 (void) GetNextToken(r,&r,extent,token);
3820 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3821 ThrowPointExpectedException(token,exception);
3822 if (*token == ',')
3823 (void) GetNextToken(r,&r,extent,token);
3824 for (x=0; IsValidPoint(token) != MagickFalse; x++)
3825 {
3826 (void) GetNextToken(r,&r,extent,token);
3827 if (*token == ',')
3828 (void) GetNextToken(r,&r,extent,token);
3829 }
3830 graphic_context[n]->dash_pattern=(double *)
3831 AcquireQuantumMemory((size_t) (2*x+2),
3832 sizeof(*graphic_context[n]->dash_pattern));
3833 if (graphic_context[n]->dash_pattern == (double *) NULL)
3834 {
3835 (void) ThrowMagickException(exception,GetMagickModule(),
3836 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3837 image->filename);
3838 status=MagickFalse;
3839 break;
3840 }
3841 (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3842 (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3843 for (j=0; j < x; j++)
3844 {
3845 (void) GetNextToken(q,&q,extent,token);
3846 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3847 ThrowPointExpectedException(token,exception);
3848 if (*token == ',')
3849 (void) GetNextToken(q,&q,extent,token);
3850 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3851 &next_token);
3852 if (token == next_token)
3853 ThrowPointExpectedException(token,exception);
3854 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3855 status=MagickFalse;
3856 }
3857 if (((x & 0x01) != 0) && (j == x))
3858 for ( ; j < (2*x); j++)
3859 graphic_context[n]->dash_pattern[j]=
3860 graphic_context[n]->dash_pattern[j-x];
3861 graphic_context[n]->dash_pattern[j]=0.0;
3862 break;
3863 }
3864 (void) GetNextToken(q,&q,extent,token);
3865 break;
3866 }
3867 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3868 {
3869 (void) GetNextToken(q,&q,extent,token);
3870 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3871 if (token == next_token)
3872 ThrowPointExpectedException(token,exception);
3873 break;
3874 }
3875 if (LocaleCompare("stroke-linecap",keyword) == 0)
3876 {
3877 ssize_t
3878 linecap;
3879
3880 (void) GetNextToken(q,&q,extent,token);
3881 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3882 if (linecap == -1)
3883 {
3884 status=MagickFalse;
3885 break;
3886 }
3887 graphic_context[n]->linecap=(LineCap) linecap;
3888 break;
3889 }
3890 if (LocaleCompare("stroke-linejoin",keyword) == 0)
3891 {
3892 ssize_t
3893 linejoin;
3894
3895 (void) GetNextToken(q,&q,extent,token);
3896 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3897 token);
3898 if (linejoin == -1)
3899 {
3900 status=MagickFalse;
3901 break;
3902 }
3903 graphic_context[n]->linejoin=(LineJoin) linejoin;
3904 break;
3905 }
3906 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3907 {
3908 (void) GetNextToken(q,&q,extent,token);
3909 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3910 break;
3911 }
3912 if (LocaleCompare("stroke-opacity",keyword) == 0)
3913 {
3914 double
3915 opacity;
3916
3917 (void) GetNextToken(q,&q,extent,token);
3918 if (graphic_context[n]->clip_path != MagickFalse)
3919 break;
3920 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3921 opacity=MagickMin(MagickMax(factor*GetDrawValue(token,&next_token),
3922 0.0),1.0);
3923 if (token == next_token)
3924 ThrowPointExpectedException(token,exception);
3925 if (graphic_context[n]->compliance == SVGCompliance)
3926 graphic_context[n]->stroke_alpha*=opacity;
3927 else
3928 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3929 if (graphic_context[n]->stroke.alpha != (double) TransparentAlpha)
3930 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3931 else
3932 graphic_context[n]->stroke.alpha=(MagickRealType)
3933 ClampToQuantum((double) QuantumRange*opacity);
3934 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3935 break;
3936 }
3937 if (LocaleCompare("stroke-width",keyword) == 0)
3938 {
3939 (void) GetNextToken(q,&q,extent,token);
3940 if (graphic_context[n]->clip_path != MagickFalse)
3941 break;
3942 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3943 if ((token == next_token) ||
3944 (graphic_context[n]->stroke_width < 0.0))
3945 ThrowPointExpectedException(token,exception);
3946 break;
3947 }
3948 status=MagickFalse;
3949 break;
3950 }
3951 case 't':
3952 case 'T':
3953 {
3954 if (LocaleCompare("text",keyword) == 0)
3955 {
3956 primitive_type=TextPrimitive;
3957 cursor=0.0;
3958 break;
3959 }
3960 if (LocaleCompare("text-align",keyword) == 0)
3961 {
3962 ssize_t
3963 align;
3964
3965 (void) GetNextToken(q,&q,extent,token);
3966 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3967 if (align == -1)
3968 {
3969 status=MagickFalse;
3970 break;
3971 }
3972 graphic_context[n]->align=(AlignType) align;
3973 break;
3974 }
3975 if (LocaleCompare("text-anchor",keyword) == 0)
3976 {
3977 ssize_t
3978 align;
3979
3980 (void) GetNextToken(q,&q,extent,token);
3981 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3982 if (align == -1)
3983 {
3984 status=MagickFalse;
3985 break;
3986 }
3987 graphic_context[n]->align=(AlignType) align;
3988 break;
3989 }
3990 if (LocaleCompare("text-antialias",keyword) == 0)
3991 {
3992 (void) GetNextToken(q,&q,extent,token);
3993 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
3994 MagickTrue : MagickFalse;
3995 break;
3996 }
3997 if (LocaleCompare("text-undercolor",keyword) == 0)
3998 {
3999 (void) GetNextToken(q,&q,extent,token);
4000 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
4001 &graphic_context[n]->undercolor,exception);
4002 break;
4003 }
4004 if (LocaleCompare("translate",keyword) == 0)
4005 {
4006 (void) GetNextToken(q,&q,extent,token);
4007 affine.tx=GetDrawValue(token,&next_token);
4008 if (token == next_token)
4009 ThrowPointExpectedException(token,exception);
4010 (void) GetNextToken(q,&q,extent,token);
4011 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4012 ThrowPointExpectedException(token,exception);
4013 if (*token == ',')
4014 (void) GetNextToken(q,&q,extent,token);
4015 affine.ty=GetDrawValue(token,&next_token);
4016 if (token == next_token)
4017 ThrowPointExpectedException(token,exception);
4018 cursor=0.0;
4019 break;
4020 }
4021 status=MagickFalse;
4022 break;
4023 }
4024 case 'u':
4025 case 'U':
4026 {
4027 if (LocaleCompare("use",keyword) == 0)
4028 {
4029 const char
4030 *use;
4031
4032 /*
4033 Get a macro from the MVG document, and "use" it here.
4034 */
4035 (void) GetNextToken(q,&q,extent,token);
4036 use=(const char *) GetValueFromSplayTree(macros,token);
4037 if (use != (const char *) NULL)
4038 {
4039 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4040 if (draw_info->macro_expansion != (size_t *) NULL)
4041 {
4042 if (*draw_info->macro_expansion >= MacroExpansionLimit)
4043 {
4044 (void) ThrowMagickException(exception,GetMagickModule(),
4045 DrawError,"NonconformingDrawingPrimitiveDefinition",
4046 "`%s'",token);
4047 clone_info=DestroyDrawInfo(clone_info);
4048 return(MagickFalse);
4049 }
4050 (*draw_info->macro_expansion)++;
4051 }
4052 (void) CloneString(&clone_info->primitive,use);
4053 status=RenderMVGContent(image,clone_info,depth+1,exception);
4054 clone_info=DestroyDrawInfo(clone_info);
4055 }
4056 break;
4057 }
4058 status=MagickFalse;
4059 break;
4060 }
4061 case 'v':
4062 case 'V':
4063 {
4064 if (LocaleCompare("viewbox",keyword) == 0)
4065 {
4066 (void) GetNextToken(q,&q,extent,token);
4067 graphic_context[n]->viewbox.x=CastDoubleToSsizeT(ceil(
4068 GetDrawValue(token,&next_token)-0.5));
4069 if (token == next_token)
4070 ThrowPointExpectedException(token,exception);
4071 (void) GetNextToken(q,&q,extent,token);
4072 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4073 ThrowPointExpectedException(token,exception);
4074 if (*token == ',')
4075 (void) GetNextToken(q,&q,extent,token);
4076 graphic_context[n]->viewbox.y=CastDoubleToSsizeT(
4077 ceil(GetDrawValue(token,&next_token)-0.5));
4078 if (token == next_token)
4079 ThrowPointExpectedException(token,exception);
4080 (void) GetNextToken(q,&q,extent,token);
4081 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4082 ThrowPointExpectedException(token,exception);
4083 if (*token == ',')
4084 (void) GetNextToken(q,&q,extent,token);
4085 graphic_context[n]->viewbox.width=CastDoubleToSizeT(floor(
4086 GetDrawValue(token,&next_token)+0.5));
4087 if (token == next_token)
4088 ThrowPointExpectedException(token,exception);
4089 (void) GetNextToken(q,&q,extent,token);
4090 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4091 ThrowPointExpectedException(token,exception);
4092 if (*token == ',')
4093 (void) GetNextToken(q,&q,extent,token);
4094 graphic_context[n]->viewbox.height=CastDoubleToSizeT(floor(
4095 GetDrawValue(token,&next_token)+0.5));
4096 if (token == next_token)
4097 ThrowPointExpectedException(token,exception);
4098 break;
4099 }
4100 status=MagickFalse;
4101 break;
4102 }
4103 case 'w':
4104 case 'W':
4105 {
4106 if (LocaleCompare("word-spacing",keyword) == 0)
4107 {
4108 (void) GetNextToken(q,&q,extent,token);
4109 graphic_context[n]->interword_spacing=GetDrawValue(token,
4110 &next_token);
4111 if (token == next_token)
4112 ThrowPointExpectedException(token,exception);
4113 break;
4114 }
4115 status=MagickFalse;
4116 break;
4117 }
4118 default:
4119 {
4120 status=MagickFalse;
4121 break;
4122 }
4123 }
4124 if (status == MagickFalse)
4125 break;
4126 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
4127 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
4128 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
4129 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
4130 {
4131 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
4132 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
4133 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
4134 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
4135 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
4136 current.tx;
4137 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4138 current.ty;
4139 }
4140 if (primitive_type == UndefinedPrimitive)
4141 {
4142 if (*q == '\0')
4143 {
4144 if (number_stops > 1)
4145 {
4146 GradientType
4147 type;
4148
4149 type=LinearGradient;
4150 if (draw_info->gradient.type == RadialGradient)
4151 type=RadialGradient;
4152 (void) GradientImage(image,type,PadSpread,stops,number_stops,
4153 exception);
4154 }
4155 if (number_stops > 0)
4156 stops=(StopInfo *) RelinquishMagickMemory(stops);
4157 }
4158 if ((draw_info->debug != MagickFalse) && (q > p))
4159 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
4160 (q-p-1),p);
4161 continue;
4162 }
4163 /*
4164 Parse the primitive attributes.
4165 */
4166 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4167 if (primitive_info[i].text != (char *) NULL)
4168 primitive_info[i].text=DestroyString(primitive_info[i].text);
4169 i=0;
4170 mvg_info.offset=i;
4171 j=0;
4172 primitive_info[0].primitive=primitive_type;
4173 primitive_info[0].point.x=0.0;
4174 primitive_info[0].point.y=0.0;
4175 primitive_info[0].coordinates=0;
4176 primitive_info[0].method=FloodfillMethod;
4177 primitive_info[0].closed_subpath=MagickFalse;
4178 for (x=0; *q != '\0'; x++)
4179 {
4180 /*
4181 Define points.
4182 */
4183 if (IsValidPoint(q) == MagickFalse)
4184 break;
4185 (void) GetNextToken(q,&q,extent,token);
4186 point.x=GetDrawValue(token,&next_token);
4187 if (token == next_token)
4188 ThrowPointExpectedException(token,exception);
4189 (void) GetNextToken(q,&q,extent,token);
4190 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4191 ThrowPointExpectedException(token,exception);
4192 if (*token == ',')
4193 (void) GetNextToken(q,&q,extent,token);
4194 point.y=GetDrawValue(token,&next_token);
4195 if (token == next_token)
4196 ThrowPointExpectedException(token,exception);
4197 (void) GetNextToken(q,(const char **) NULL,extent,token);
4198 if (*token == ',')
4199 (void) GetNextToken(q,&q,extent,token);
4200 primitive_info[i].primitive=primitive_type;
4201 primitive_info[i].point=point;
4202 primitive_info[i].coordinates=0;
4203 primitive_info[i].method=FloodfillMethod;
4204 primitive_info[i].closed_subpath=MagickFalse;
4205 i++;
4206 mvg_info.offset=i;
4207 if (i < (ssize_t) number_points)
4208 continue;
4209 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(double)
4210 number_points);
4211 primitive_info=(*mvg_info.primitive_info);
4212 if (status == MagickFalse)
4213 break;
4214 }
4215 if (status == MagickFalse)
4216 break;
4217 if (primitive_info[j].text != (char *) NULL)
4218 primitive_info[j].text=DestroyString(primitive_info[j].text);
4219 primitive_info[j].primitive=primitive_type;
4220 primitive_info[j].coordinates=(size_t) x;
4221 primitive_info[j].method=FloodfillMethod;
4222 primitive_info[j].closed_subpath=MagickFalse;
4223 /*
4224 Circumscribe primitive within a circle.
4225 */
4226 bounds.x1=primitive_info[j].point.x;
4227 bounds.y1=primitive_info[j].point.y;
4228 bounds.x2=primitive_info[j].point.x;
4229 bounds.y2=primitive_info[j].point.y;
4230 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4231 {
4232 point=primitive_info[j+k].point;
4233 if (point.x < bounds.x1)
4234 bounds.x1=point.x;
4235 if (point.y < bounds.y1)
4236 bounds.y1=point.y;
4237 if (point.x > bounds.x2)
4238 bounds.x2=point.x;
4239 if (point.y > bounds.y2)
4240 bounds.y2=point.y;
4241 }
4242 /*
4243 Speculate how many points our primitive might consume.
4244 */
4245 coordinates=(double) primitive_info[j].coordinates;
4246 switch (primitive_type)
4247 {
4248 case RectanglePrimitive:
4249 {
4250 coordinates*=5.0;
4251 break;
4252 }
4253 case RoundRectanglePrimitive:
4254 {
4255 double
4256 alpha,
4257 beta,
4258 radius;
4259
4260 alpha=bounds.x2-bounds.x1;
4261 beta=bounds.y2-bounds.y1;
4262 radius=hypot(alpha,beta);
4263 coordinates*=5.0;
4264 coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4265 BezierQuantum+360.0;
4266 break;
4267 }
4268 case BezierPrimitive:
4269 {
4270 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4271 break;
4272 }
4273 case PathPrimitive:
4274 {
4275 char
4276 *s,
4277 *t;
4278
4279 (void) GetNextToken(q,&q,extent,token);
4280 coordinates=1.0;
4281 t=token;
4282 for (s=token; *s != '\0'; s=t)
4283 {
4284 double
4285 value;
4286
4287 value=GetDrawValue(s,&t);
4288 (void) value;
4289 if (s == t)
4290 {
4291 t++;
4292 continue;
4293 }
4294 coordinates++;
4295 }
4296 for (s=token; *s != '\0'; s++)
4297 if (strspn(s,"AaCcQqSsTt") != 0)
4298 coordinates+=(20.0*BezierQuantum)+360.0;
4299 break;
4300 }
4301 default:
4302 break;
4303 }
4304 if (status == MagickFalse)
4305 break;
4306 if (((size_t) (i+coordinates)) >= number_points)
4307 {
4308 /*
4309 Resize based on speculative points required by primitive.
4310 */
4311 number_points+=(size_t) coordinates+1;
4312 if (number_points < (size_t) coordinates)
4313 {
4314 (void) ThrowMagickException(exception,GetMagickModule(),
4315 ResourceLimitError,"MemoryAllocationFailed","`%s'",
4316 image->filename);
4317 status=MagickFalse;
4318 break;
4319 }
4320 mvg_info.offset=i;
4321 status&=CheckPrimitiveExtent(&mvg_info,(double) number_points+
4322 (double) coordinates+1.0);
4323 primitive_info=(*mvg_info.primitive_info);
4324 if (status == MagickFalse)
4325 break;
4326 }
4327 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,
4328 PrimitiveExtentPad);
4329 primitive_info=(*mvg_info.primitive_info);
4330 if (status == MagickFalse)
4331 break;
4332 mvg_info.offset=j;
4333 switch (primitive_type)
4334 {
4335 case PointPrimitive:
4336 default:
4337 {
4338 if (primitive_info[j].coordinates != 1)
4339 {
4340 status=MagickFalse;
4341 break;
4342 }
4343 status&=(MagickStatusType) TracePoint(primitive_info+j,
4344 primitive_info[j].point);
4345 primitive_info=(*mvg_info.primitive_info);
4346 if (status == MagickFalse)
4347 break;
4348 i=j+(ssize_t) primitive_info[j].coordinates;
4349 break;
4350 }
4351 case LinePrimitive:
4352 {
4353 if (primitive_info[j].coordinates != 2)
4354 {
4355 status=MagickFalse;
4356 break;
4357 }
4358 status&=(MagickStatusType) TraceLine(primitive_info+j,
4359 primitive_info[j].point,primitive_info[j+1].point);
4360 primitive_info=(*mvg_info.primitive_info);
4361 if (status == MagickFalse)
4362 break;
4363 i=j+(ssize_t) primitive_info[j].coordinates;
4364 break;
4365 }
4366 case RectanglePrimitive:
4367 {
4368 if (primitive_info[j].coordinates != 2)
4369 {
4370 status=MagickFalse;
4371 break;
4372 }
4373 status&=(MagickStatusType) TraceRectangle(primitive_info+j,
4374 primitive_info[j].point,primitive_info[j+1].point);
4375 primitive_info=(*mvg_info.primitive_info);
4376 if (status == MagickFalse)
4377 break;
4378 i=j+(ssize_t) primitive_info[j].coordinates;
4379 break;
4380 }
4381 case RoundRectanglePrimitive:
4382 {
4383 if (primitive_info[j].coordinates != 3)
4384 {
4385 status=MagickFalse;
4386 break;
4387 }
4388 if ((primitive_info[j+2].point.x < 0.0) ||
4389 (primitive_info[j+2].point.y < 0.0))
4390 {
4391 status=MagickFalse;
4392 break;
4393 }
4394 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4395 {
4396 status=MagickFalse;
4397 break;
4398 }
4399 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4400 {
4401 status=MagickFalse;
4402 break;
4403 }
4404 status&=(MagickStatusType) TraceRoundRectangle(&mvg_info,
4405 primitive_info[j].point,primitive_info[j+1].point,
4406 primitive_info[j+2].point);
4407 primitive_info=(*mvg_info.primitive_info);
4408 if (status == MagickFalse)
4409 break;
4410 i=j+(ssize_t) primitive_info[j].coordinates;
4411 break;
4412 }
4413 case ArcPrimitive:
4414 {
4415 if (primitive_info[j].coordinates != 3)
4416 {
4417 status=MagickFalse;
4418 break;
4419 }
4420 status&=(MagickStatusType) TraceArc(&mvg_info,primitive_info[j].point,
4421 primitive_info[j+1].point,primitive_info[j+2].point);
4422 primitive_info=(*mvg_info.primitive_info);
4423 if (status == MagickFalse)
4424 break;
4425 i=j+(ssize_t) primitive_info[j].coordinates;
4426 break;
4427 }
4428 case EllipsePrimitive:
4429 {
4430 if (primitive_info[j].coordinates != 3)
4431 {
4432 status=MagickFalse;
4433 break;
4434 }
4435 if ((primitive_info[j+1].point.x < 0.0) ||
4436 (primitive_info[j+1].point.y < 0.0))
4437 {
4438 status=MagickFalse;
4439 break;
4440 }
4441 status&=(MagickStatusType) TraceEllipse(&mvg_info,
4442 primitive_info[j].point,primitive_info[j+1].point,
4443 primitive_info[j+2].point);
4444 primitive_info=(*mvg_info.primitive_info);
4445 if (status == MagickFalse)
4446 break;
4447 i=j+(ssize_t) primitive_info[j].coordinates;
4448 break;
4449 }
4450 case CirclePrimitive:
4451 {
4452 if (primitive_info[j].coordinates != 2)
4453 {
4454 status=MagickFalse;
4455 break;
4456 }
4457 status&=(MagickStatusType) TraceCircle(&mvg_info,
4458 primitive_info[j].point,primitive_info[j+1].point);
4459 primitive_info=(*mvg_info.primitive_info);
4460 if (status == MagickFalse)
4461 break;
4462 i=j+(ssize_t) primitive_info[j].coordinates;
4463 break;
4464 }
4465 case PolylinePrimitive:
4466 {
4467 if (primitive_info[j].coordinates < 1)
4468 {
4469 status=MagickFalse;
4470 break;
4471 }
4472 break;
4473 }
4474 case PolygonPrimitive:
4475 {
4476 if (primitive_info[j].coordinates < 3)
4477 {
4478 status=MagickFalse;
4479 break;
4480 }
4481 primitive_info[i]=primitive_info[j];
4482 primitive_info[i].coordinates=0;
4483 primitive_info[j].coordinates++;
4484 primitive_info[j].closed_subpath=MagickTrue;
4485 i++;
4486 break;
4487 }
4488 case BezierPrimitive:
4489 {
4490 if (primitive_info[j].coordinates < 3)
4491 {
4492 status=MagickFalse;
4493 break;
4494 }
4495 status&=(MagickStatusType) TraceBezier(&mvg_info,
4496 primitive_info[j].coordinates);
4497 primitive_info=(*mvg_info.primitive_info);
4498 if (status == MagickFalse)
4499 break;
4500 i=j+(ssize_t) primitive_info[j].coordinates;
4501 break;
4502 }
4503 case PathPrimitive:
4504 {
4505 coordinates=(double) TracePath(&mvg_info,token,exception);
4506 primitive_info=(*mvg_info.primitive_info);
4507 if (status == MagickFalse)
4508 break;
4509 if (coordinates < 0.0)
4510 {
4511 status=MagickFalse;
4512 break;
4513 }
4514 i=(ssize_t) (j+coordinates);
4515 break;
4516 }
4517 case AlphaPrimitive:
4518 case ColorPrimitive:
4519 {
4520 ssize_t
4521 method;
4522
4523 if (primitive_info[j].coordinates != 1)
4524 {
4525 status=MagickFalse;
4526 break;
4527 }
4528 (void) GetNextToken(q,&q,extent,token);
4529 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4530 if (method == -1)
4531 {
4532 status=MagickFalse;
4533 break;
4534 }
4535 primitive_info[j].method=(PaintMethod) method;
4536 break;
4537 }
4538 case TextPrimitive:
4539 {
4540 if (primitive_info[j].coordinates != 1)
4541 {
4542 status=MagickFalse;
4543 break;
4544 }
4545 if (*token != ',')
4546 (void) GetNextToken(q,&q,extent,token);
4547 (void) CloneString(&primitive_info[j].text,token);
4548 /*
4549 Compute text cursor offset.
4550 */
4551 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4552 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4553 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4554 {
4555 mvg_info.point=primitive_info->point;
4556 primitive_info->point.x+=cursor;
4557 }
4558 else
4559 {
4560 mvg_info.point=primitive_info->point;
4561 cursor=0.0;
4562 }
4563 clone_info->render=MagickFalse;
4564 clone_info->text=AcquireString(token);
4565 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,
4566 &metrics,exception);
4567 clone_info=DestroyDrawInfo(clone_info);
4568 cursor+=metrics.width;
4569 if (graphic_context[n]->compliance != SVGCompliance)
4570 cursor=0.0;
4571 break;
4572 }
4573 case ImagePrimitive:
4574 {
4575 if (primitive_info[j].coordinates != 2)
4576 {
4577 status=MagickFalse;
4578 break;
4579 }
4580 (void) GetNextToken(q,&q,extent,token);
4581 (void) CloneString(&primitive_info[j].text,token);
4582 break;
4583 }
4584 }
4585 mvg_info.offset=i;
4586 if (status == 0)
4587 break;
4588 primitive_info[i].primitive=UndefinedPrimitive;
4589 if ((draw_info->debug != MagickFalse) && (q > p))
4590 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
4591 /*
4592 Sanity check.
4593 */
4594 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4595 &graphic_context[n]->affine));
4596 primitive_info=(*mvg_info.primitive_info);
4597 if (status == 0)
4598 break;
4599 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(double)
4600 graphic_context[n]->stroke_width);
4601 primitive_info=(*mvg_info.primitive_info);
4602 if (status == 0)
4603 break;
4604 if (i == 0)
4605 continue;
4606 /*
4607 Transform points.
4608 */
4609 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4610 {
4611 point=primitive_info[i].point;
4612 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4613 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4614 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4615 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4616 point=primitive_info[i].point;
4617 if (point.x < graphic_context[n]->bounds.x1)
4618 graphic_context[n]->bounds.x1=point.x;
4619 if (point.y < graphic_context[n]->bounds.y1)
4620 graphic_context[n]->bounds.y1=point.y;
4621 if (point.x > graphic_context[n]->bounds.x2)
4622 graphic_context[n]->bounds.x2=point.x;
4623 if (point.y > graphic_context[n]->bounds.y2)
4624 graphic_context[n]->bounds.y2=point.y;
4625 if (primitive_info[i].primitive == ImagePrimitive)
4626 break;
4627 if (i >= (ssize_t) number_points)
4628 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4629 }
4630 if (graphic_context[n]->render != MagickFalse)
4631 {
4632 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4633 (graphic_context[n]->clip_mask != (char *) NULL) &&
4634 (LocaleCompare(graphic_context[n]->clip_mask,
4635 graphic_context[n-1]->clip_mask) != 0))
4636 {
4637 const char
4638 *clip_path;
4639
4640 clip_path=(const char *) GetValueFromSplayTree(macros,
4641 graphic_context[n]->clip_mask);
4642 if (clip_path != (const char *) NULL)
4643 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4644 clip_path);
4645 status&=(MagickStatusType) DrawClipPath(image,graphic_context[n],
4646 graphic_context[n]->clip_mask,exception);
4647 }
4648 status&=(MagickStatusType) DrawPrimitive(image,graphic_context[n],
4649 primitive_info,exception);
4650 }
4651 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4652 primitive_extent);
4653 if (proceed == MagickFalse)
4654 break;
4655 if (status == 0)
4656 break;
4657 }
4658 if (draw_info->debug != MagickFalse)
4659 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4660 /*
4661 Relinquish resources.
4662 */
4663 macros=DestroySplayTree(macros);
4664 token=DestroyString(token);
4665 if (primitive_info != (PrimitiveInfo *) NULL)
4666 {
4667 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4668 if (primitive_info[i].text != (char *) NULL)
4669 primitive_info[i].text=DestroyString(primitive_info[i].text);
4670 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4671 }
4672 primitive=DestroyString(primitive);
4673 if (stops != (StopInfo *) NULL)
4674 stops=(StopInfo *) RelinquishMagickMemory(stops);
4675 for ( ; n >= 0; n--)
4676 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4677 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4678 if ((status == MagickFalse) && (exception->severity < ErrorException))
4679 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
4680 keyword);
4681 return(status != 0 ? MagickTrue : MagickFalse);
4682}
4683
4684MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
4685 ExceptionInfo *exception)
4686{
4687 MagickBooleanType
4688 status;
4689
4690 size_t
4691 macro_expansion = 0;
4692
4693 ((DrawInfo *) draw_info)->macro_expansion=(&macro_expansion);
4694 status=RenderMVGContent(image,draw_info,0,exception);
4695 ((DrawInfo *) draw_info)->macro_expansion=(size_t *) NULL;
4696 return(status);
4697}
4698␌
4699/*
4700%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4701% %
4702% %
4703% %
4704% D r a w P a t t e r n P a t h %
4705% %
4706% %
4707% %
4708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4709%
4710% DrawPatternPath() draws a pattern.
4711%
4712% The format of the DrawPatternPath method is:
4713%
4714% MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4715% const char *name,Image **pattern,ExceptionInfo *exception)
4716%
4717% A description of each parameter follows:
4718%
4719% o image: the image.
4720%
4721% o draw_info: the draw info.
4722%
4723% o name: the pattern name.
4724%
4725% o image: the image.
4726%
4727% o exception: return any errors or warnings in this structure.
4728%
4729*/
4730MagickExport MagickBooleanType DrawPatternPath(Image *image,
4731 const DrawInfo *draw_info,const char *name,Image **pattern,
4732 ExceptionInfo *exception)
4733{
4734 char
4735 property[MagickPathExtent];
4736
4737 const char
4738 *geometry,
4739 *path,
4740 *type;
4741
4742 DrawInfo
4743 *clone_info;
4744
4745 ImageInfo
4746 *image_info;
4747
4748 MagickBooleanType
4749 status;
4750
4751 assert(image != (Image *) NULL);
4752 assert(image->signature == MagickCoreSignature);
4753 assert(draw_info != (const DrawInfo *) NULL);
4754 assert(name != (const char *) NULL);
4755 if (IsEventLogging() != MagickFalse)
4756 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4757 (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
4758 path=GetImageArtifact(image,property);
4759 if (path == (const char *) NULL)
4760 return(MagickFalse);
4761 (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
4762 geometry=GetImageArtifact(image,property);
4763 if (geometry == (const char *) NULL)
4764 return(MagickFalse);
4765 if ((*pattern) != (Image *) NULL)
4766 *pattern=DestroyImage(*pattern);
4767 image_info=AcquireImageInfo();
4768 image_info->size=AcquireString(geometry);
4769 *pattern=AcquireImage(image_info,exception);
4770 image_info=DestroyImageInfo(image_info);
4771 (void) QueryColorCompliance("#00000000",AllCompliance,
4772 &(*pattern)->background_color,exception);
4773 (void) SetImageBackgroundColor(*pattern,exception);
4774 if (draw_info->debug != MagickFalse)
4775 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4776 "begin pattern-path %s %s",name,geometry);
4777 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4778 if (clone_info->fill_pattern != (Image *) NULL)
4779 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4780 if (clone_info->stroke_pattern != (Image *) NULL)
4781 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4782 (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
4783 type=GetImageArtifact(image,property);
4784 if (type != (const char *) NULL)
4785 clone_info->gradient.type=(GradientType) ParseCommandOption(
4786 MagickGradientOptions,MagickFalse,type);
4787 (void) CloneString(&clone_info->primitive,path);
4788 status=RenderMVGContent(*pattern,clone_info,0,exception);
4789 clone_info=DestroyDrawInfo(clone_info);
4790 if (draw_info->debug != MagickFalse)
4791 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4792 return(status);
4793}
4794␌
4795/*
4796%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4797% %
4798% %
4799% %
4800+ D r a w P o l y g o n P r i m i t i v e %
4801% %
4802% %
4803% %
4804%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4805%
4806% DrawPolygonPrimitive() draws a polygon on the image.
4807%
4808% The format of the DrawPolygonPrimitive method is:
4809%
4810% MagickBooleanType DrawPolygonPrimitive(Image *image,
4811% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4812% ExceptionInfo *exception)
4813%
4814% A description of each parameter follows:
4815%
4816% o image: the image.
4817%
4818% o draw_info: the draw info.
4819%
4820% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4821%
4822% o exception: return any errors or warnings in this structure.
4823%
4824*/
4825
4826static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4827{
4828 ssize_t
4829 i;
4830
4831 assert(polygon_info != (PolygonInfo **) NULL);
4832 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4833 if (polygon_info[i] != (PolygonInfo *) NULL)
4834 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4835 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4836 return(polygon_info);
4837}
4838
4839static PolygonInfo **AcquirePolygonTLS(const PrimitiveInfo *primitive_info,
4840 ExceptionInfo *exception)
4841{
4842 PathInfo
4843 *magick_restrict path_info;
4844
4846 **polygon_info;
4847
4848 size_t
4849 number_threads;
4850
4851 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4852 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4853 sizeof(*polygon_info));
4854 if (polygon_info == (PolygonInfo **) NULL)
4855 {
4856 (void) ThrowMagickException(exception,GetMagickModule(),
4857 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4858 return((PolygonInfo **) NULL);
4859 }
4860 (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4861 path_info=ConvertPrimitiveToPath(primitive_info,exception);
4862 if (path_info == (PathInfo *) NULL)
4863 return(DestroyPolygonTLS(polygon_info));
4864 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4865 if (polygon_info[0] == (PolygonInfo *) NULL)
4866 {
4867 (void) ThrowMagickException(exception,GetMagickModule(),
4868 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4869 return(DestroyPolygonTLS(polygon_info));
4870 }
4871 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4872 return(polygon_info);
4873}
4874
4875static MagickBooleanType ClonePolygonEdgesTLS(PolygonInfo **polygon_info,
4876 const size_t number_threads,ExceptionInfo *exception)
4877{
4878 ssize_t
4879 i;
4880
4881 for (i=1; i < (ssize_t) number_threads; i++)
4882 {
4883 EdgeInfo
4884 *edge_info;
4885
4886 ssize_t
4887 j;
4888
4889 polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4890 sizeof(*polygon_info[i]));
4891 if (polygon_info[i] == (PolygonInfo *) NULL)
4892 {
4893 (void) ThrowMagickException(exception,GetMagickModule(),
4894 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4895 return(MagickFalse);
4896 }
4897 polygon_info[i]->number_edges=0;
4898 edge_info=polygon_info[0]->edges;
4899 polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4900 polygon_info[0]->number_edges,sizeof(*edge_info));
4901 if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4902 {
4903 (void) ThrowMagickException(exception,GetMagickModule(),
4904 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4905 return(MagickFalse);
4906 }
4907 (void) memcpy(polygon_info[i]->edges,edge_info,
4908 polygon_info[0]->number_edges*sizeof(*edge_info));
4909 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4910 polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4911 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4912 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4913 {
4914 edge_info=polygon_info[0]->edges+j;
4915 polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4916 edge_info->number_points,sizeof(*edge_info));
4917 if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4918 {
4919 (void) ThrowMagickException(exception,GetMagickModule(),
4920 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4921 return(MagickFalse);
4922 }
4923 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4924 edge_info->number_points*sizeof(*edge_info->points));
4925 }
4926 }
4927 return(MagickTrue);
4928}
4929
4930static size_t DestroyEdge(PolygonInfo *polygon_info,const ssize_t edge)
4931{
4932 assert(edge < (ssize_t) polygon_info->number_edges);
4933 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4934 polygon_info->edges[edge].points);
4935 polygon_info->number_edges--;
4936 if (edge < (ssize_t) polygon_info->number_edges)
4937 (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4938 (polygon_info->number_edges-(size_t) edge)*sizeof(*polygon_info->edges));
4939 return(polygon_info->number_edges);
4940}
4941
4942static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
4943 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4944 const ssize_t y,double *stroke_alpha)
4945{
4946 double
4947 alpha,
4948 beta,
4949 distance,
4950 subpath_alpha;
4951
4952 const PointInfo
4953 *q;
4954
4955 EdgeInfo
4956 *p;
4957
4958 PointInfo
4959 delta;
4960
4961 ssize_t
4962 i,
4963 j,
4964 winding_number;
4965
4966 /*
4967 Compute fill & stroke opacity for this (x,y) point.
4968 */
4969 *stroke_alpha=0.0;
4970 subpath_alpha=0.0;
4971 p=polygon_info->edges;
4972 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
4973 {
4974 if ((double) y <= (p->bounds.y1-mid-0.5))
4975 break;
4976 if ((double) y > (p->bounds.y2+mid+0.5))
4977 {
4978 p--;
4979 (void) DestroyEdge(polygon_info,j--);
4980 continue;
4981 }
4982 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
4983 ((double) x > (p->bounds.x2+mid+0.5)))
4984 continue;
4985 i=(ssize_t) MagickMax((double) p->highwater,1.0);
4986 for ( ; i < (ssize_t) p->number_points; i++)
4987 {
4988 if ((double) y <= (p->points[i-1].y-mid-0.5))
4989 break;
4990 if ((double) y > (p->points[i].y+mid+0.5))
4991 continue;
4992 if (p->scanline != (double) y)
4993 {
4994 p->scanline=(double) y;
4995 p->highwater=(size_t) i;
4996 }
4997 /*
4998 Compute distance between a point and an edge.
4999 */
5000 q=p->points+i-1;
5001 delta.x=(q+1)->x-q->x;
5002 delta.y=(q+1)->y-q->y;
5003 beta=delta.x*(x-q->x)+delta.y*(y-q->y); /* segLen*point-cos(theta) */
5004 if (beta <= 0.0)
5005 {
5006 /*
5007 Cosine <= 0, point is closest to q.
5008 */
5009 delta.x=(double) x-q->x;
5010 delta.y=(double) y-q->y;
5011 distance=delta.x*delta.x+delta.y*delta.y;
5012 }
5013 else
5014 {
5015 alpha=delta.x*delta.x+delta.y*delta.y; /* segLen*segLen */
5016 if (beta >= alpha)
5017 {
5018 /*
5019 Point is closest to q+1.
5020 */
5021 delta.x=(double) x-(q+1)->x;
5022 delta.y=(double) y-(q+1)->y;
5023 distance=delta.x*delta.x+delta.y*delta.y;
5024 }
5025 else
5026 {
5027 /*
5028 Point is closest to point between q & q+1.
5029 */
5030 alpha=MagickSafeReciprocal(alpha);
5031 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
5032 distance=alpha*beta*beta;
5033 }
5034 }
5035 /*
5036 Compute stroke & subpath opacity.
5037 */
5038 beta=0.0;
5039 if (p->ghostline == MagickFalse)
5040 {
5041 alpha=mid+0.5;
5042 if ((*stroke_alpha < 1.0) &&
5043 (distance <= ((alpha+0.25)*(alpha+0.25))))
5044 {
5045 alpha=mid-0.5;
5046 if (distance <= ((alpha+0.25)*(alpha+0.25)))
5047 *stroke_alpha=1.0;
5048 else
5049 {
5050 beta=1.0;
5051 if (fabs(distance-1.0) >= MagickEpsilon)
5052 beta=sqrt((double) distance);
5053 alpha=beta-mid-0.5;
5054 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
5055 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
5056 }
5057 }
5058 }
5059 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
5060 continue;
5061 if (distance <= 0.0)
5062 {
5063 subpath_alpha=1.0;
5064 continue;
5065 }
5066 if (distance > 1.0)
5067 continue;
5068 if (fabs(beta) < MagickEpsilon)
5069 {
5070 beta=1.0;
5071 if (fabs(distance-1.0) >= MagickEpsilon)
5072 beta=sqrt(distance);
5073 }
5074 alpha=beta-1.0;
5075 if (subpath_alpha < (alpha*alpha))
5076 subpath_alpha=alpha*alpha;
5077 }
5078 }
5079 /*
5080 Compute fill opacity.
5081 */
5082 if (fill == MagickFalse)
5083 return(0.0);
5084 if (subpath_alpha >= 1.0)
5085 return(1.0);
5086 /*
5087 Determine winding number.
5088 */
5089 winding_number=0;
5090 p=polygon_info->edges;
5091 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
5092 {
5093 if ((double) y <= p->bounds.y1)
5094 break;
5095 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
5096 continue;
5097 if ((double) x > p->bounds.x2)
5098 {
5099 winding_number+=p->direction != 0 ? 1 : -1;
5100 continue;
5101 }
5102 i=(ssize_t) MagickMax((double) p->highwater,1.0);
5103 for ( ; i < (ssize_t) (p->number_points-1); i++)
5104 if ((double) y <= p->points[i].y)
5105 break;
5106 q=p->points+i-1;
5107 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
5108 winding_number+=p->direction != 0 ? 1 : -1;
5109 }
5110 if (fill_rule != NonZeroRule)
5111 {
5112 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
5113 return(1.0);
5114 }
5115 else
5116 if (MagickAbsoluteValue(winding_number) != 0)
5117 return(1.0);
5118 return(subpath_alpha);
5119}
5120
5121static MagickBooleanType DrawPolygonPrimitive(Image *image,
5122 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5123 ExceptionInfo *exception)
5124{
5125 typedef struct _ExtentInfo
5126 {
5127 ssize_t
5128 x1,
5129 y1,
5130 x2,
5131 y2;
5132 } ExtentInfo;
5133
5134 CacheView
5135 *image_view;
5136
5137 const char
5138 *artifact;
5139
5140 double
5141 mid;
5142
5143 EdgeInfo
5144 *p;
5145
5146 ExtentInfo
5147 poly_extent;
5148
5149 MagickBooleanType
5150 fill,
5151 status;
5152
5154 **magick_restrict polygon_info;
5155
5157 bounds;
5158
5159 size_t
5160 number_threads;
5161
5162 ssize_t
5163 i,
5164 y;
5165
5166 assert(image != (Image *) NULL);
5167 assert(image->signature == MagickCoreSignature);
5168 assert(draw_info != (DrawInfo *) NULL);
5169 assert(draw_info->signature == MagickCoreSignature);
5170 assert(primitive_info != (PrimitiveInfo *) NULL);
5171 if (IsEventLogging() != MagickFalse)
5172 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5173 if (primitive_info->coordinates <= 1)
5174 return(MagickTrue);
5175 /*
5176 Compute bounding box.
5177 */
5178 polygon_info=AcquirePolygonTLS(primitive_info,exception);
5179 if (polygon_info == (PolygonInfo **) NULL)
5180 return(MagickFalse);
5181 if (draw_info->debug != MagickFalse)
5182 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
5183 fill=(primitive_info->method == FillToBorderMethod) ||
5184 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
5185 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5186 bounds=polygon_info[0]->edges[0].bounds;
5187 artifact=GetImageArtifact(image,"draw:render-bounding-rectangles");
5188 if (IsStringTrue(artifact) != MagickFalse)
5189 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
5190 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
5191 {
5192 p=polygon_info[0]->edges+i;
5193 if (p->bounds.x1 < bounds.x1)
5194 bounds.x1=p->bounds.x1;
5195 if (p->bounds.y1 < bounds.y1)
5196 bounds.y1=p->bounds.y1;
5197 if (p->bounds.x2 > bounds.x2)
5198 bounds.x2=p->bounds.x2;
5199 if (p->bounds.y2 > bounds.y2)
5200 bounds.y2=p->bounds.y2;
5201 }
5202 bounds.x1-=(mid+1.0);
5203 bounds.y1-=(mid+1.0);
5204 bounds.x2+=(mid+1.0);
5205 bounds.y2+=(mid+1.0);
5206 if ((bounds.x1 >= (double) image->columns) ||
5207 (bounds.y1 >= (double) image->rows) ||
5208 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5209 {
5210 polygon_info=DestroyPolygonTLS(polygon_info);
5211 return(MagickTrue); /* virtual polygon */
5212 }
5213 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5214 (double) image->columns-1.0 : bounds.x1;
5215 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5216 (double) image->rows-1.0 : bounds.y1;
5217 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5218 (double) image->columns-1.0 : bounds.x2;
5219 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5220 (double) image->rows-1.0 : bounds.y2;
5221 poly_extent.x1=CastDoubleToSsizeT(ceil(bounds.x1-0.5));
5222 poly_extent.y1=CastDoubleToSsizeT(ceil(bounds.y1-0.5));
5223 poly_extent.x2=CastDoubleToSsizeT(floor(bounds.x2+0.5));
5224 poly_extent.y2=CastDoubleToSsizeT(floor(bounds.y2+0.5));
5225 number_threads=(size_t) GetMagickNumberThreads(image,image,(size_t)
5226 (poly_extent.y2-poly_extent.y1+1),1);
5227 status=ClonePolygonEdgesTLS(polygon_info,number_threads,exception);
5228 if (status == MagickFalse)
5229 {
5230 polygon_info=DestroyPolygonTLS(polygon_info);
5231 return(status);
5232 }
5233 image_view=AcquireAuthenticCacheView(image,exception);
5234 if ((primitive_info->coordinates == 1) ||
5235 (polygon_info[0]->number_edges == 0))
5236 {
5237 /*
5238 Draw point.
5239 */
5240#if defined(MAGICKCORE_OPENMP_SUPPORT)
5241 #pragma omp parallel for schedule(static) shared(status) \
5242 num_threads((int) number_threads)
5243#endif
5244 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5245 {
5246 PixelInfo
5247 pixel;
5248
5249 ssize_t
5250 x;
5251
5252 Quantum
5253 *magick_restrict q;
5254
5255 if (status == MagickFalse)
5256 continue;
5257 x=poly_extent.x1;
5258 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (poly_extent.x2-
5259 x+1),1,exception);
5260 if (q == (Quantum *) NULL)
5261 {
5262 status=MagickFalse;
5263 continue;
5264 }
5265 GetPixelInfo(image,&pixel);
5266 for ( ; x <= poly_extent.x2; x++)
5267 {
5268 if ((x == CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5))) &&
5269 (y == CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5))))
5270 {
5271 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&pixel,
5272 exception);
5273 SetPixelViaPixelInfo(image,&pixel,q);
5274 }
5275 q+=(ptrdiff_t) GetPixelChannels(image);
5276 }
5277 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5278 status=MagickFalse;
5279 }
5280 image_view=DestroyCacheView(image_view);
5281 polygon_info=DestroyPolygonTLS(polygon_info);
5282 if (draw_info->debug != MagickFalse)
5283 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5284 " end draw-polygon");
5285 return(status);
5286 }
5287 /*
5288 Draw polygon or line.
5289 */
5290#if defined(MAGICKCORE_OPENMP_SUPPORT)
5291 #pragma omp parallel for schedule(static) shared(status) \
5292 num_threads((int) number_threads)
5293#endif
5294 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5295 {
5296 const int
5297 id = GetOpenMPThreadId();
5298
5299 Quantum
5300 *magick_restrict q;
5301
5302 ssize_t
5303 x;
5304
5305 if (status == MagickFalse)
5306 continue;
5307 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(size_t)
5308 (poly_extent.x2-poly_extent.x1+1),1,exception);
5309 if (q == (Quantum *) NULL)
5310 {
5311 status=MagickFalse;
5312 continue;
5313 }
5314 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5315 {
5316 double
5317 fill_alpha,
5318 stroke_alpha;
5319
5320 PixelInfo
5321 fill_color,
5322 stroke_color;
5323
5324 /*
5325 Fill and/or stroke.
5326 */
5327 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
5328 x,y,&stroke_alpha);
5329 if (draw_info->stroke_antialias == MagickFalse)
5330 {
5331 fill_alpha=fill_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5332 stroke_alpha=stroke_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5333 }
5334 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&fill_color,
5335 exception);
5336 CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
5337 (double) GetPixelAlpha(image,q),q);
5338 GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&stroke_color,
5339 exception);
5340 CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
5341 (double) GetPixelAlpha(image,q),q);
5342 q+=(ptrdiff_t) GetPixelChannels(image);
5343 }
5344 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5345 status=MagickFalse;
5346 }
5347 image_view=DestroyCacheView(image_view);
5348 polygon_info=DestroyPolygonTLS(polygon_info);
5349 if (draw_info->debug != MagickFalse)
5350 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
5351 return(status);
5352}
5353␌
5354/*
5355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5356% %
5357% %
5358% %
5359% D r a w P r i m i t i v e %
5360% %
5361% %
5362% %
5363%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5364%
5365% DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
5366%
5367% The format of the DrawPrimitive method is:
5368%
5369% MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
5370% PrimitiveInfo *primitive_info,ExceptionInfo *exception)
5371%
5372% A description of each parameter follows:
5373%
5374% o image: the image.
5375%
5376% o draw_info: the draw info.
5377%
5378% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5379%
5380% o exception: return any errors or warnings in this structure.
5381%
5382*/
5383static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
5384{
5385 const char
5386 *methods[] =
5387 {
5388 "point",
5389 "replace",
5390 "floodfill",
5391 "filltoborder",
5392 "reset",
5393 "?"
5394 };
5395
5396 PointInfo
5397 p,
5398 point,
5399 q;
5400
5401 ssize_t
5402 i,
5403 x;
5404
5405 ssize_t
5406 coordinates,
5407 y;
5408
5409 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5410 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5411 switch (primitive_info->primitive)
5412 {
5413 case AlphaPrimitive:
5414 {
5415 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5416 "AlphaPrimitive %.20g,%.20g %s",(double) x,(double) y,
5417 methods[primitive_info->method]);
5418 return;
5419 }
5420 case ColorPrimitive:
5421 {
5422 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5423 "ColorPrimitive %.20g,%.20g %s",(double) x,(double) y,
5424 methods[primitive_info->method]);
5425 return;
5426 }
5427 case ImagePrimitive:
5428 {
5429 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5430 "ImagePrimitive %.20g,%.20g",(double) x,(double) y);
5431 return;
5432 }
5433 case PointPrimitive:
5434 {
5435 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5436 "PointPrimitive %.20g,%.20g %s",(double) x,(double) y,
5437 methods[primitive_info->method]);
5438 return;
5439 }
5440 case TextPrimitive:
5441 {
5442 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5443 "TextPrimitive %.20g,%.20g",(double) x,(double) y);
5444 return;
5445 }
5446 default:
5447 break;
5448 }
5449 coordinates=0;
5450 p=primitive_info[0].point;
5451 q.x=(-1.0);
5452 q.y=(-1.0);
5453 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5454 {
5455 point=primitive_info[i].point;
5456 if (coordinates <= 0)
5457 {
5458 coordinates=(ssize_t) primitive_info[i].coordinates;
5459 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5460 " begin open (%.20g)",(double) coordinates);
5461 p=point;
5462 }
5463 point=primitive_info[i].point;
5464 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5465 (fabs(q.y-point.y) >= MagickEpsilon))
5466 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5467 " %.20g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5468 else
5469 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5470 " %.20g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5471 q=point;
5472 coordinates--;
5473 if (coordinates > 0)
5474 continue;
5475 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5476 (fabs(p.y-point.y) >= MagickEpsilon))
5477 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.20g)",
5478 (double) coordinates);
5479 else
5480 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.20g)",
5481 (double) coordinates);
5482 }
5483}
5484
5485MagickExport MagickBooleanType DrawPrimitive(Image *image,
5486 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5487 ExceptionInfo *exception)
5488{
5489 CacheView
5490 *image_view;
5491
5492 MagickStatusType
5493 status;
5494
5495 ssize_t
5496 i,
5497 x;
5498
5499 ssize_t
5500 y;
5501
5502 if (draw_info->debug != MagickFalse)
5503 {
5504 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5505 " begin draw-primitive");
5506 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5507 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5508 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5509 draw_info->affine.tx,draw_info->affine.ty);
5510 }
5511 status=MagickTrue;
5512 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5513 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5514 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5515 status&=(MagickStatusType) SetImageColorspace(image,sRGBColorspace,
5516 exception);
5517 if (draw_info->compliance == SVGCompliance)
5518 {
5519 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5520 draw_info->clipping_mask,exception);
5521 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5522 draw_info->composite_mask,exception);
5523 }
5524 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5525 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5526 image_view=AcquireAuthenticCacheView(image,exception);
5527 switch (primitive_info->primitive)
5528 {
5529 case AlphaPrimitive:
5530 {
5531 if ((image->alpha_trait & BlendPixelTrait) == 0)
5532 status&=(MagickStatusType) SetImageAlphaChannel(image,
5533 OpaqueAlphaChannel,exception);
5534 switch (primitive_info->method)
5535 {
5536 case PointMethod:
5537 default:
5538 {
5539 PixelInfo
5540 pixel;
5541
5542 Quantum
5543 *q;
5544
5545 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5546 if (q == (Quantum *) NULL)
5547 break;
5548 GetFillColor(draw_info,x,y,&pixel,exception);
5549 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5550 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5551 exception);
5552 break;
5553 }
5554 case ReplaceMethod:
5555 {
5556 PixelInfo
5557 pixel,
5558 target;
5559
5560 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5561 x,y,&target,exception);
5562 GetPixelInfo(image,&pixel);
5563 for (y=0; y < (ssize_t) image->rows; y++)
5564 {
5565 Quantum
5566 *magick_restrict q;
5567
5568 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5569 exception);
5570 if (q == (Quantum *) NULL)
5571 break;
5572 for (x=0; x < (ssize_t) image->columns; x++)
5573 {
5574 GetPixelInfoPixel(image,q,&pixel);
5575 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5576 {
5577 q+=(ptrdiff_t) GetPixelChannels(image);
5578 continue;
5579 }
5580 GetFillColor(draw_info,x,y,&pixel,exception);
5581 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5582 q+=(ptrdiff_t) GetPixelChannels(image);
5583 }
5584 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5585 exception);
5586 if (status == MagickFalse)
5587 break;
5588 }
5589 break;
5590 }
5591 case FloodfillMethod:
5592 case FillToBorderMethod:
5593 {
5594 ChannelType
5595 channel_mask;
5596
5597 PixelInfo
5598 target;
5599
5600 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5601 TileVirtualPixelMethod,x,y,&target,exception);
5602 if (primitive_info->method == FillToBorderMethod)
5603 {
5604 target.red=(double) draw_info->border_color.red;
5605 target.green=(double) draw_info->border_color.green;
5606 target.blue=(double) draw_info->border_color.blue;
5607 }
5608 channel_mask=SetImageChannelMask(image,AlphaChannel);
5609 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5610 &target,x,y,primitive_info->method == FloodfillMethod ?
5611 MagickFalse : MagickTrue,exception);
5612 (void) SetImageChannelMask(image,channel_mask);
5613 break;
5614 }
5615 case ResetMethod:
5616 {
5617 PixelInfo
5618 pixel;
5619
5620 for (y=0; y < (ssize_t) image->rows; y++)
5621 {
5622 Quantum
5623 *magick_restrict q;
5624
5625 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5626 exception);
5627 if (q == (Quantum *) NULL)
5628 break;
5629 for (x=0; x < (ssize_t) image->columns; x++)
5630 {
5631 GetFillColor(draw_info,x,y,&pixel,exception);
5632 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5633 q+=(ptrdiff_t) GetPixelChannels(image);
5634 }
5635 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5636 exception);
5637 if (status == MagickFalse)
5638 break;
5639 }
5640 break;
5641 }
5642 }
5643 break;
5644 }
5645 case ColorPrimitive:
5646 {
5647 switch (primitive_info->method)
5648 {
5649 case PointMethod:
5650 default:
5651 {
5652 PixelInfo
5653 pixel;
5654
5655 Quantum
5656 *q;
5657
5658 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5659 if (q == (Quantum *) NULL)
5660 break;
5661 GetPixelInfo(image,&pixel);
5662 GetFillColor(draw_info,x,y,&pixel,exception);
5663 SetPixelViaPixelInfo(image,&pixel,q);
5664 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5665 exception);
5666 break;
5667 }
5668 case ReplaceMethod:
5669 {
5670 PixelInfo
5671 pixel,
5672 target;
5673
5674 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5675 x,y,&target,exception);
5676 for (y=0; y < (ssize_t) image->rows; y++)
5677 {
5678 Quantum
5679 *magick_restrict q;
5680
5681 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5682 exception);
5683 if (q == (Quantum *) NULL)
5684 break;
5685 for (x=0; x < (ssize_t) image->columns; x++)
5686 {
5687 GetPixelInfoPixel(image,q,&pixel);
5688 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5689 {
5690 q+=(ptrdiff_t) GetPixelChannels(image);
5691 continue;
5692 }
5693 GetFillColor(draw_info,x,y,&pixel,exception);
5694 SetPixelViaPixelInfo(image,&pixel,q);
5695 q+=(ptrdiff_t) GetPixelChannels(image);
5696 }
5697 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5698 exception);
5699 if (status == MagickFalse)
5700 break;
5701 }
5702 break;
5703 }
5704 case FloodfillMethod:
5705 case FillToBorderMethod:
5706 {
5707 PixelInfo
5708 target;
5709
5710 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5711 TileVirtualPixelMethod,x,y,&target,exception);
5712 if (primitive_info->method == FillToBorderMethod)
5713 {
5714 target.red=(double) draw_info->border_color.red;
5715 target.green=(double) draw_info->border_color.green;
5716 target.blue=(double) draw_info->border_color.blue;
5717 }
5718 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5719 &target,x,y,primitive_info->method == FloodfillMethod ?
5720 MagickFalse : MagickTrue,exception);
5721 break;
5722 }
5723 case ResetMethod:
5724 {
5725 PixelInfo
5726 pixel;
5727
5728 GetPixelInfo(image,&pixel);
5729 for (y=0; y < (ssize_t) image->rows; y++)
5730 {
5731 Quantum
5732 *magick_restrict q;
5733
5734 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5735 exception);
5736 if (q == (Quantum *) NULL)
5737 break;
5738 for (x=0; x < (ssize_t) image->columns; x++)
5739 {
5740 GetFillColor(draw_info,x,y,&pixel,exception);
5741 SetPixelViaPixelInfo(image,&pixel,q);
5742 q+=(ptrdiff_t) GetPixelChannels(image);
5743 }
5744 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5745 exception);
5746 if (status == MagickFalse)
5747 break;
5748 }
5749 break;
5750 }
5751 }
5752 break;
5753 }
5754 case ImagePrimitive:
5755 {
5757 affine;
5758
5759 char
5760 composite_geometry[MagickPathExtent],
5761 magic[MagickPathExtent] = {'\0'};
5762
5763 Image
5764 *composite_image,
5765 *composite_images;
5766
5767 ImageInfo
5768 *clone_info;
5769
5771 geometry;
5772
5773 ssize_t
5774 x1,
5775 y1;
5776
5777 if (primitive_info->text == (char *) NULL)
5778 break;
5779 clone_info=AcquireImageInfo();
5780 composite_images=(Image *) NULL;
5781 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5782 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5783 exception);
5784 else
5785 if (*primitive_info->text != '\0')
5786 {
5787 /*
5788 Read composite image.
5789 */
5790 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5791 MagickPathExtent);
5792 (void) SetImageInfo(clone_info,1,exception);
5793 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5794 MagickPathExtent);
5795 if (clone_info->size != (char *) NULL)
5796 clone_info->size=DestroyString(clone_info->size);
5797 if (clone_info->extract != (char *) NULL)
5798 clone_info->extract=DestroyString(clone_info->extract);
5799 GetPathComponent(clone_info->filename,MagickPath,magic);
5800 if (*magic == '\0')
5801 composite_images=ReadImage(clone_info,exception);
5802 else
5803 (void) ThrowMagickException(exception,GetMagickModule(),
5804 FileOpenError,"UnableToOpenFile","`%s'",clone_info->filename);
5805 }
5806 clone_info=DestroyImageInfo(clone_info);
5807 if (composite_images == (Image *) NULL)
5808 {
5809 status=MagickFalse;
5810 break;
5811 }
5812 composite_image=RemoveFirstImageFromList(&composite_images);
5813 composite_images=DestroyImageList(composite_images);
5814 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5815 NULL,(void *) NULL);
5816 x1=CastDoubleToSsizeT(ceil(primitive_info[1].point.x-0.5));
5817 y1=CastDoubleToSsizeT(ceil(primitive_info[1].point.y-0.5));
5818 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5819 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5820 {
5821 /*
5822 Resize image.
5823 */
5824 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5825 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
5826 composite_image->filter=image->filter;
5827 status&=(MagickStatusType) TransformImage(&composite_image,
5828 (char *) NULL,composite_geometry,exception);
5829 }
5830 if (composite_image->alpha_trait == UndefinedPixelTrait)
5831 status&=(MagickStatusType) SetImageAlphaChannel(composite_image,
5832 OpaqueAlphaChannel,exception);
5833 if (draw_info->alpha != OpaqueAlpha)
5834 status&=(MagickStatusType) SetImageAlpha(composite_image,
5835 draw_info->alpha,exception);
5836 SetGeometry(image,&geometry);
5837 image->gravity=draw_info->gravity;
5838 geometry.x=x;
5839 geometry.y=y;
5840 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5841 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
5842 composite_image->rows,(double) geometry.x,(double) geometry.y);
5843 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
5844 affine=draw_info->affine;
5845 affine.tx=(double) geometry.x;
5846 affine.ty=(double) geometry.y;
5847 composite_image->interpolate=image->interpolate;
5848 if ((draw_info->compose == OverCompositeOp) ||
5849 (draw_info->compose == SrcOverCompositeOp))
5850 status&=(MagickStatusType) DrawAffineImage(image,composite_image,
5851 &affine,exception);
5852 else
5853 status&=(MagickStatusType) CompositeImage(image,composite_image,
5854 draw_info->compose,MagickTrue,geometry.x,geometry.y,exception);
5855 composite_image=DestroyImage(composite_image);
5856 break;
5857 }
5858 case PointPrimitive:
5859 {
5860 PixelInfo
5861 fill_color;
5862
5863 Quantum
5864 *q;
5865
5866 if ((y < 0) || (y >= (ssize_t) image->rows))
5867 break;
5868 if ((x < 0) || (x >= (ssize_t) image->columns))
5869 break;
5870 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5871 if (q == (Quantum *) NULL)
5872 break;
5873 GetFillColor(draw_info,x,y,&fill_color,exception);
5874 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,(double)
5875 GetPixelAlpha(image,q),q);
5876 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5877 exception);
5878 break;
5879 }
5880 case TextPrimitive:
5881 {
5882 char
5883 geometry[MagickPathExtent];
5884
5885 DrawInfo
5886 *clone_info;
5887
5888 if (primitive_info->text == (char *) NULL)
5889 break;
5890 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5891 (void) CloneString(&clone_info->text,primitive_info->text);
5892 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
5893 primitive_info->point.x,primitive_info->point.y);
5894 (void) CloneString(&clone_info->geometry,geometry);
5895 status&=(MagickStatusType) AnnotateImage(image,clone_info,exception);
5896 clone_info=DestroyDrawInfo(clone_info);
5897 break;
5898 }
5899 default:
5900 {
5901 double
5902 mid,
5903 scale;
5904
5905 DrawInfo
5906 *clone_info;
5907
5908 if (IsEventLogging() != MagickFalse)
5909 LogPrimitiveInfo(primitive_info);
5910 scale=ExpandAffine(&draw_info->affine);
5911 if ((draw_info->dash_pattern != (double *) NULL) &&
5912 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5913 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5914 (draw_info->stroke.alpha != (double) TransparentAlpha))
5915 {
5916 /*
5917 Draw dash polygon.
5918 */
5919 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5920 clone_info->stroke_width=0.0;
5921 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5922 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5923 primitive_info,exception);
5924 clone_info=DestroyDrawInfo(clone_info);
5925 if (status != MagickFalse)
5926 status&=(MagickStatusType) DrawDashPolygon(draw_info,primitive_info,
5927 image,exception);
5928 break;
5929 }
5930 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5931 if ((mid > 1.0) &&
5932 ((draw_info->stroke.alpha != (double) TransparentAlpha) ||
5933 (draw_info->stroke_pattern != (Image *) NULL)))
5934 {
5935 double
5936 point_x,
5937 point_y;
5938
5939 MagickBooleanType
5940 closed_path;
5941
5942 /*
5943 Draw strokes while respecting line cap/join attributes.
5944 */
5945 closed_path=primitive_info[0].closed_subpath;
5946 i=(ssize_t) primitive_info[0].coordinates;
5947 point_x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5948 point_y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5949 if ((point_x < MagickEpsilon) && (point_y < MagickEpsilon))
5950 closed_path=MagickTrue;
5951 if ((((draw_info->linecap == RoundCap) ||
5952 (closed_path != MagickFalse)) &&
5953 (draw_info->linejoin == RoundJoin)) ||
5954 (primitive_info[i].primitive != UndefinedPrimitive))
5955 {
5956 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5957 primitive_info,exception);
5958 break;
5959 }
5960 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5961 clone_info->stroke_width=0.0;
5962 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5963 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5964 primitive_info,exception);
5965 clone_info=DestroyDrawInfo(clone_info);
5966 if (status != MagickFalse)
5967 status&=(MagickStatusType) DrawStrokePolygon(image,draw_info,
5968 primitive_info,exception);
5969 break;
5970 }
5971 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5972 primitive_info,exception);
5973 break;
5974 }
5975 }
5976 image_view=DestroyCacheView(image_view);
5977 if (draw_info->compliance == SVGCompliance)
5978 {
5979 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5980 (Image *) NULL,exception);
5981 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5982 (Image *) NULL,exception);
5983 }
5984 if (draw_info->debug != MagickFalse)
5985 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
5986 return(status != 0 ? MagickTrue : MagickFalse);
5987}
5988␌
5989/*
5990%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5991% %
5992% %
5993% %
5994+ D r a w S t r o k e P o l y g o n %
5995% %
5996% %
5997% %
5998%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5999%
6000% DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
6001% the image while respecting the line cap and join attributes.
6002%
6003% The format of the DrawStrokePolygon method is:
6004%
6005% MagickBooleanType DrawStrokePolygon(Image *image,
6006% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
6007%
6008% A description of each parameter follows:
6009%
6010% o image: the image.
6011%
6012% o draw_info: the draw info.
6013%
6014% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
6015%
6016%
6017*/
6018
6019static MagickBooleanType DrawRoundLinecap(Image *image,
6020 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
6021 ExceptionInfo *exception)
6022{
6024 linecap[5];
6025
6026 ssize_t
6027 i;
6028
6029 if (primitive_info->coordinates < 1)
6030 return(MagickFalse);
6031 for (i=0; i < 4; i++)
6032 linecap[i]=(*primitive_info);
6033 linecap[0].coordinates=4;
6034 linecap[1].point.x+=2.0*MagickEpsilon;
6035 linecap[2].point.x+=2.0*MagickEpsilon;
6036 linecap[2].point.y+=2.0*MagickEpsilon;
6037 linecap[3].point.y+=2.0*MagickEpsilon;
6038 linecap[4].primitive=UndefinedPrimitive;
6039 return(DrawPolygonPrimitive(image,draw_info,linecap,exception));
6040}
6041
6042static MagickBooleanType DrawStrokePolygon(Image *image,
6043 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
6044 ExceptionInfo *exception)
6045{
6046 DrawInfo
6047 *clone_info;
6048
6049 MagickBooleanType
6050 closed_path;
6051
6052 MagickStatusType
6053 status;
6054
6056 *stroke_polygon;
6057
6058 const PrimitiveInfo
6059 *p,
6060 *q;
6061
6062 /*
6063 Draw stroked polygon.
6064 */
6065 if (draw_info->debug != MagickFalse)
6066 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
6067 " begin draw-stroke-polygon");
6068 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
6069 clone_info->fill=draw_info->stroke;
6070 if (clone_info->fill_pattern != (Image *) NULL)
6071 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
6072 if (clone_info->stroke_pattern != (Image *) NULL)
6073 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
6074 MagickTrue,exception);
6075 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
6076 clone_info->stroke_width=0.0;
6077 clone_info->fill_rule=NonZeroRule;
6078 status=MagickTrue;
6079 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
6080 {
6081 if (p->coordinates == 1)
6082 continue;
6083 stroke_polygon=TraceStrokePolygon(draw_info,p,exception);
6084 if (stroke_polygon == (PrimitiveInfo *) NULL)
6085 {
6086 status=0;
6087 break;
6088 }
6089 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
6090 stroke_polygon,exception);
6091 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
6092 if (status == 0)
6093 break;
6094 q=p+p->coordinates-1;
6095 closed_path=p->closed_subpath;
6096 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
6097 {
6098 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,p,
6099 exception);
6100 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,q,
6101 exception);
6102 }
6103 }
6104 clone_info=DestroyDrawInfo(clone_info);
6105 if (draw_info->debug != MagickFalse)
6106 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
6107 " end draw-stroke-polygon");
6108 return(status != 0 ? MagickTrue : MagickFalse);
6109}
6110␌
6111/*
6112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6113% %
6114% %
6115% %
6116% G e t A f f i n e M a t r i x %
6117% %
6118% %
6119% %
6120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6121%
6122% GetAffineMatrix() returns an AffineMatrix initialized to the identity
6123% matrix.
6124%
6125% The format of the GetAffineMatrix method is:
6126%
6127% void GetAffineMatrix(AffineMatrix *affine_matrix)
6128%
6129% A description of each parameter follows:
6130%
6131% o affine_matrix: the affine matrix.
6132%
6133*/
6134MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
6135{
6136 if (IsEventLogging() != MagickFalse)
6137 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
6138 assert(affine_matrix != (AffineMatrix *) NULL);
6139 (void) memset(affine_matrix,0,sizeof(*affine_matrix));
6140 affine_matrix->sx=1.0;
6141 affine_matrix->sy=1.0;
6142}
6143␌
6144/*
6145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6146% %
6147% %
6148% %
6149+ G e t D r a w I n f o %
6150% %
6151% %
6152% %
6153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6154%
6155% GetDrawInfo() initializes draw_info to default values from image_info.
6156%
6157% The format of the GetDrawInfo method is:
6158%
6159% void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
6160%
6161% A description of each parameter follows:
6162%
6163% o image_info: the image info..
6164%
6165% o draw_info: the draw info.
6166%
6167*/
6168MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
6169{
6170 char
6171 *next_token;
6172
6173 const char
6174 *option;
6175
6177 *exception;
6178
6179 /*
6180 Initialize draw attributes.
6181 */
6182 assert(draw_info != (DrawInfo *) NULL);
6183 if (IsEventLogging() != MagickFalse)
6184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
6185 (void) memset(draw_info,0,sizeof(*draw_info));
6186 draw_info->image_info=CloneImageInfo(image_info);
6187 GetAffineMatrix(&draw_info->affine);
6188 exception=AcquireExceptionInfo();
6189 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
6190 exception);
6191 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
6192 exception);
6193 draw_info->stroke_antialias=draw_info->image_info->antialias;
6194 draw_info->stroke_width=1.0;
6195 draw_info->fill_rule=EvenOddRule;
6196 draw_info->alpha=OpaqueAlpha;
6197 draw_info->fill_alpha=OpaqueAlpha;
6198 draw_info->stroke_alpha=OpaqueAlpha;
6199 draw_info->linecap=ButtCap;
6200 draw_info->linejoin=MiterJoin;
6201 draw_info->miterlimit=10;
6202 draw_info->decorate=NoDecoration;
6203 draw_info->pointsize=12.0;
6204 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
6205 draw_info->compose=OverCompositeOp;
6206 draw_info->render=MagickTrue;
6207 draw_info->clip_path=MagickFalse;
6208 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
6209 MagickTrue : MagickFalse;
6210 draw_info->macro_expansion=(size_t *) NULL;
6211 if (draw_info->image_info->font != (char *) NULL)
6212 draw_info->font=AcquireString(draw_info->image_info->font);
6213 if (draw_info->image_info->density != (char *) NULL)
6214 draw_info->density=AcquireString(draw_info->image_info->density);
6215 draw_info->text_antialias=draw_info->image_info->antialias;
6216 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
6217 draw_info->pointsize=draw_info->image_info->pointsize;
6218 draw_info->border_color=draw_info->image_info->border_color;
6219 if (draw_info->image_info->server_name != (char *) NULL)
6220 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
6221 option=GetImageOption(draw_info->image_info,"direction");
6222 if (option != (const char *) NULL)
6223 draw_info->direction=(DirectionType) ParseCommandOption(
6224 MagickDirectionOptions,MagickFalse,option);
6225 else
6226 draw_info->direction=UndefinedDirection;
6227 option=GetImageOption(draw_info->image_info,"encoding");
6228 if (option != (const char *) NULL)
6229 (void) CloneString(&draw_info->encoding,option);
6230 option=GetImageOption(draw_info->image_info,"family");
6231 if (option != (const char *) NULL)
6232 (void) CloneString(&draw_info->family,option);
6233 option=GetImageOption(draw_info->image_info,"fill");
6234 if (option != (const char *) NULL)
6235 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
6236 exception);
6237 option=GetImageOption(draw_info->image_info,"gravity");
6238 if (option != (const char *) NULL)
6239 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
6240 MagickFalse,option);
6241 option=GetImageOption(draw_info->image_info,"interline-spacing");
6242 if (option != (const char *) NULL)
6243 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6244 option=GetImageOption(draw_info->image_info,"interword-spacing");
6245 if (option != (const char *) NULL)
6246 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6247 option=GetImageOption(draw_info->image_info,"kerning");
6248 if (option != (const char *) NULL)
6249 draw_info->kerning=GetDrawValue(option,&next_token);
6250 option=GetImageOption(draw_info->image_info,"stroke");
6251 if (option != (const char *) NULL)
6252 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
6253 exception);
6254 option=GetImageOption(draw_info->image_info,"strokewidth");
6255 if (option != (const char *) NULL)
6256 draw_info->stroke_width=GetDrawValue(option,&next_token);
6257 option=GetImageOption(draw_info->image_info,"style");
6258 if (option != (const char *) NULL)
6259 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6260 MagickFalse,option);
6261 option=GetImageOption(draw_info->image_info,"undercolor");
6262 if (option != (const char *) NULL)
6263 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
6264 exception);
6265 option=GetImageOption(draw_info->image_info,"weight");
6266 if (option != (const char *) NULL)
6267 {
6268 ssize_t
6269 weight;
6270
6271 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6272 if (weight == -1)
6273 weight=(ssize_t) StringToUnsignedLong(option);
6274 draw_info->weight=(size_t) weight;
6275 }
6276 option=GetImageOption(draw_info->image_info,"word-break");
6277 if (option != (const char *) NULL)
6278 draw_info->word_break=(WordBreakType) ParseCommandOption(
6279 MagickWordBreakOptions,MagickFalse,option);
6280 exception=DestroyExceptionInfo(exception);
6281 draw_info->signature=MagickCoreSignature;
6282}
6283␌
6284/*
6285%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6286% %
6287% %
6288% %
6289+ P e r m u t a t e %
6290% %
6291% %
6292% %
6293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6294%
6295% Permutate() returns the permutation of the (n,k).
6296%
6297% The format of the Permutate method is:
6298%
6299% void Permutate(ssize_t n,ssize_t k)
6300%
6301% A description of each parameter follows:
6302%
6303% o n:
6304%
6305% o k:
6306%
6307%
6308*/
6309static inline double Permutate(const ssize_t n,const ssize_t k)
6310{
6311 double
6312 r;
6313
6314 ssize_t
6315 i;
6316
6317 r=1.0;
6318 for (i=k+1; i <= n; i++)
6319 r*=i;
6320 for (i=1; i <= (n-k); i++)
6321 r/=i;
6322 return(r);
6323}
6324␌
6325/*
6326%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6327% %
6328% %
6329% %
6330+ T r a c e P r i m i t i v e %
6331% %
6332% %
6333% %
6334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6335%
6336% TracePrimitive is a collection of methods for generating graphic
6337% primitives such as arcs, ellipses, paths, etc.
6338%
6339*/
6340
6341static MagickBooleanType TraceArc(MVGInfo *mvg_info,const PointInfo start,
6342 const PointInfo end,const PointInfo degrees)
6343{
6344 PointInfo
6345 center,
6346 radius;
6347
6348 center.x=0.5*(end.x+start.x);
6349 center.y=0.5*(end.y+start.y);
6350 radius.x=fabs(center.x-start.x);
6351 radius.y=fabs(center.y-start.y);
6352 return(TraceEllipse(mvg_info,center,radius,degrees));
6353}
6354
6355static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,const PointInfo start,
6356 const PointInfo end,const PointInfo arc,const double angle,
6357 const MagickBooleanType large_arc,const MagickBooleanType sweep)
6358{
6359 double
6360 alpha,
6361 beta,
6362 delta,
6363 factor,
6364 gamma,
6365 theta;
6366
6367 MagickStatusType
6368 status;
6369
6370 PointInfo
6371 center,
6372 points[3],
6373 radii;
6374
6375 double
6376 cosine,
6377 sine;
6378
6380 *primitive_info;
6381
6383 *p;
6384
6385 ssize_t
6386 i;
6387
6388 size_t
6389 arc_segments;
6390
6391 ssize_t
6392 offset;
6393
6394 offset=mvg_info->offset;
6395 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6396 primitive_info->coordinates=0;
6397 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6398 (fabs(start.y-end.y) < MagickEpsilon))
6399 return(TracePoint(primitive_info,end));
6400 radii.x=fabs(arc.x);
6401 radii.y=fabs(arc.y);
6402 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6403 return(TraceLine(primitive_info,start,end));
6404 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
6405 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
6406 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6407 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6408 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6409 (radii.y*radii.y);
6410 if (delta < MagickEpsilon)
6411 return(TraceLine(primitive_info,start,end));
6412 if (delta > 1.0)
6413 {
6414 radii.x*=sqrt((double) delta);
6415 radii.y*=sqrt((double) delta);
6416 }
6417 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6418 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6419 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6420 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6421 alpha=points[1].x-points[0].x;
6422 beta=points[1].y-points[0].y;
6423 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6424 return(TraceLine(primitive_info,start,end));
6425 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6426 if (factor <= 0.0)
6427 factor=0.0;
6428 else
6429 {
6430 factor=sqrt((double) factor);
6431 if (sweep == large_arc)
6432 factor=(-factor);
6433 }
6434 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6435 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6436 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6437 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6438 if ((theta < 0.0) && (sweep != MagickFalse))
6439 theta+=2.0*MagickPI;
6440 else
6441 if ((theta > 0.0) && (sweep == MagickFalse))
6442 theta-=2.0*MagickPI;
6443 arc_segments=(size_t) CastDoubleToSsizeT(ceil(fabs((double) (theta/(0.5*
6444 MagickPI+MagickEpsilon)))));
6445 status=MagickTrue;
6446 p=primitive_info;
6447 for (i=0; i < (ssize_t) arc_segments; i++)
6448 {
6449 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6450 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
6451 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
6452 sin(fmod((double) beta,DegreesToRadians(360.0)));
6453 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
6454 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
6455 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6456 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
6457 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
6458 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6459 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
6460 theta/arc_segments),DegreesToRadians(360.0))));
6461 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
6462 theta/arc_segments),DegreesToRadians(360.0))));
6463 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
6464 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6465 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
6466 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6467 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6468 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6469 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6470 points[0].y);
6471 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6472 points[0].y);
6473 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6474 points[1].y);
6475 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6476 points[1].y);
6477 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6478 points[2].y);
6479 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6480 points[2].y);
6481 if (i == (ssize_t) (arc_segments-1))
6482 (p+3)->point=end;
6483 status&=(MagickStatusType) TraceBezier(mvg_info,4);
6484 if (status == 0)
6485 break;
6486 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6487 mvg_info->offset+=(ssize_t) p->coordinates;
6488 p+=(ptrdiff_t) p->coordinates;
6489 }
6490 if (status == 0)
6491 return(MagickFalse);
6492 mvg_info->offset=offset;
6493 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6494 primitive_info->coordinates=(size_t) (p-primitive_info);
6495 primitive_info->closed_subpath=MagickFalse;
6496 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6497 {
6498 p->primitive=primitive_info->primitive;
6499 p--;
6500 }
6501 return(MagickTrue);
6502}
6503
6504static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6505 const size_t number_coordinates)
6506{
6507 double
6508 alpha,
6509 *coefficients,
6510 weight;
6511
6512 PointInfo
6513 end,
6514 point,
6515 *points;
6516
6518 *primitive_info;
6519
6521 *p;
6522
6523 ssize_t
6524 i,
6525 j;
6526
6527 size_t
6528 control_points,
6529 quantum;
6530
6531 /*
6532 Allocate coefficients.
6533 */
6534 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6535 quantum=number_coordinates;
6536 for (i=0; i < (ssize_t) number_coordinates; i++)
6537 {
6538 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6539 {
6540 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6541 if (alpha > (double) GetMaxMemoryRequest())
6542 {
6543 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6544 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6545 return(MagickFalse);
6546 }
6547 if (alpha > (double) quantum)
6548 quantum=(size_t) alpha;
6549 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6550 if (alpha > (double) quantum)
6551 quantum=(size_t) alpha;
6552 }
6553 }
6554 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6555 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6556 if (quantum > (double) GetMaxMemoryRequest())
6557 {
6558 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6559 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6560 return(MagickFalse);
6561 }
6562 coefficients=(double *) AcquireQuantumMemory(number_coordinates,
6563 sizeof(*coefficients));
6564 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6565 sizeof(*points));
6566 if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
6567 {
6568 if (points != (PointInfo *) NULL)
6569 points=(PointInfo *) RelinquishMagickMemory(points);
6570 if (coefficients != (double *) NULL)
6571 coefficients=(double *) RelinquishMagickMemory(coefficients);
6572 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6573 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6574 return(MagickFalse);
6575 }
6576 control_points=quantum*number_coordinates;
6577 if (CheckPrimitiveExtent(mvg_info,(double) control_points+1) == MagickFalse)
6578 {
6579 points=(PointInfo *) RelinquishMagickMemory(points);
6580 coefficients=(double *) RelinquishMagickMemory(coefficients);
6581 return(MagickFalse);
6582 }
6583 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6584 /*
6585 Compute bezier points.
6586 */
6587 end=primitive_info[number_coordinates-1].point;
6588 for (i=0; i < (ssize_t) number_coordinates; i++)
6589 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6590 weight=0.0;
6591 for (i=0; i < (ssize_t) control_points; i++)
6592 {
6593 p=primitive_info;
6594 point.x=0.0;
6595 point.y=0.0;
6596 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
6597 for (j=0; j < (ssize_t) number_coordinates; j++)
6598 {
6599 point.x+=alpha*coefficients[j]*p->point.x;
6600 point.y+=alpha*coefficients[j]*p->point.y;
6601 alpha*=weight/(1.0-weight);
6602 p++;
6603 }
6604 points[i]=point;
6605 weight+=1.0/control_points;
6606 }
6607 /*
6608 Bezier curves are just short segmented polys.
6609 */
6610 p=primitive_info;
6611 for (i=0; i < (ssize_t) control_points; i++)
6612 {
6613 if (TracePoint(p,points[i]) == MagickFalse)
6614 {
6615 points=(PointInfo *) RelinquishMagickMemory(points);
6616 coefficients=(double *) RelinquishMagickMemory(coefficients);
6617 return(MagickFalse);
6618 }
6619 p+=(ptrdiff_t) p->coordinates;
6620 }
6621 if (TracePoint(p,end) == MagickFalse)
6622 {
6623 points=(PointInfo *) RelinquishMagickMemory(points);
6624 coefficients=(double *) RelinquishMagickMemory(coefficients);
6625 return(MagickFalse);
6626 }
6627 p+=(ptrdiff_t) p->coordinates;
6628 primitive_info->coordinates=(size_t) (p-primitive_info);
6629 primitive_info->closed_subpath=MagickFalse;
6630 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6631 {
6632 p->primitive=primitive_info->primitive;
6633 p--;
6634 }
6635 points=(PointInfo *) RelinquishMagickMemory(points);
6636 coefficients=(double *) RelinquishMagickMemory(coefficients);
6637 return(MagickTrue);
6638}
6639
6640static MagickBooleanType TraceCircle(MVGInfo *mvg_info,const PointInfo start,
6641 const PointInfo end)
6642{
6643 double
6644 alpha,
6645 beta,
6646 radius;
6647
6648 PointInfo
6649 offset,
6650 degrees;
6651
6652 alpha=end.x-start.x;
6653 beta=end.y-start.y;
6654 radius=hypot((double) alpha,(double) beta);
6655 offset.x=(double) radius;
6656 offset.y=(double) radius;
6657 degrees.x=0.0;
6658 degrees.y=360.0;
6659 return(TraceEllipse(mvg_info,start,offset,degrees));
6660}
6661
6662static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,const PointInfo center,
6663 const PointInfo radii,const PointInfo arc)
6664{
6665 double
6666 coordinates,
6667 delta,
6668 step,
6669 x,
6670 y;
6671
6672 PointInfo
6673 angle,
6674 point;
6675
6677 *primitive_info;
6678
6680 *p;
6681
6682 ssize_t
6683 i;
6684
6685 /*
6686 Ellipses are just short segmented polys.
6687 */
6688 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6689 primitive_info->coordinates=0;
6690 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6691 return(MagickTrue);
6692 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6693 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6694 angle.x=DegreesToRadians(arc.x);
6695 y=arc.y;
6696 while (y < arc.x)
6697 y+=360.0;
6698 angle.y=DegreesToRadians(y);
6699 coordinates=ceil((angle.y-angle.x)/step+1.0);
6700 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6701 return(MagickFalse);
6702 i=0;
6703 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6704 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6705 {
6706 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6707 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6708 if (i++ >= (ssize_t) coordinates)
6709 break;
6710 if (TracePoint(p,point) == MagickFalse)
6711 return(MagickFalse);
6712 p+=(ptrdiff_t) p->coordinates;
6713 }
6714 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6715 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6716 if (TracePoint(p,point) == MagickFalse)
6717 return(MagickFalse);
6718 p+=(ptrdiff_t) p->coordinates;
6719 primitive_info->coordinates=(size_t) (p-primitive_info);
6720 primitive_info->closed_subpath=MagickFalse;
6721 x=fabs(primitive_info[0].point.x-
6722 primitive_info[primitive_info->coordinates-1].point.x);
6723 y=fabs(primitive_info[0].point.y-
6724 primitive_info[primitive_info->coordinates-1].point.y);
6725 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6726 primitive_info->closed_subpath=MagickTrue;
6727 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6728 {
6729 p->primitive=primitive_info->primitive;
6730 p--;
6731 }
6732 return(MagickTrue);
6733}
6734
6735static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6736 const PointInfo start,const PointInfo end)
6737{
6738 if (TracePoint(primitive_info,start) == MagickFalse)
6739 return(MagickFalse);
6740 if (TracePoint(primitive_info+1,end) == MagickFalse)
6741 return(MagickFalse);
6742 (primitive_info+1)->primitive=primitive_info->primitive;
6743 primitive_info->coordinates=2;
6744 primitive_info->closed_subpath=MagickFalse;
6745 return(MagickTrue);
6746}
6747
6748static ssize_t TracePath(MVGInfo *mvg_info,const char *path,
6749 ExceptionInfo *exception)
6750{
6751 char
6752 *next_token,
6753 token[MagickPathExtent] = "";
6754
6755 const char
6756 *p;
6757
6758 double
6759 x,
6760 y;
6761
6762 int
6763 attribute,
6764 last_attribute;
6765
6766 MagickBooleanType
6767 status;
6768
6769 PointInfo
6770 end = {0.0, 0.0},
6771 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6772 point = {0.0, 0.0},
6773 start = {0.0, 0.0};
6774
6776 *primitive_info;
6777
6778 PrimitiveType
6779 primitive_type;
6780
6782 *q;
6783
6784 ssize_t
6785 i;
6786
6787 size_t
6788 number_coordinates,
6789 z_count;
6790
6791 ssize_t
6792 subpath_offset;
6793
6794 subpath_offset=mvg_info->offset;
6795 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6796 status=MagickTrue;
6797 attribute=0;
6798 number_coordinates=0;
6799 z_count=0;
6800 primitive_type=primitive_info->primitive;
6801 q=primitive_info;
6802 for (p=path; *p != '\0'; )
6803 {
6804 if (status == MagickFalse)
6805 break;
6806 while (isspace((int) ((unsigned char) *p)) != 0)
6807 p++;
6808 if (*p == '\0')
6809 break;
6810 last_attribute=attribute;
6811 attribute=(int) (*p++);
6812 switch (attribute)
6813 {
6814 case 'a':
6815 case 'A':
6816 {
6817 double
6818 angle = 0.0;
6819
6820 MagickBooleanType
6821 large_arc = MagickFalse,
6822 sweep = MagickFalse;
6823
6824 PointInfo
6825 arc = {0.0, 0.0};
6826
6827 /*
6828 Elliptical arc.
6829 */
6830 do
6831 {
6832 (void) GetNextToken(p,&p,MagickPathExtent,token);
6833 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6834 ThrowPointExpectedException(token,exception);
6835 if (*token == ',')
6836 (void) GetNextToken(p,&p,MagickPathExtent,token);
6837 arc.x=GetDrawValue(token,&next_token);
6838 if (token == next_token)
6839 ThrowPointExpectedException(token,exception);
6840 (void) GetNextToken(p,&p,MagickPathExtent,token);
6841 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6842 ThrowPointExpectedException(token,exception);
6843 if (*token == ',')
6844 (void) GetNextToken(p,&p,MagickPathExtent,token);
6845 arc.y=GetDrawValue(token,&next_token);
6846 if (token == next_token)
6847 ThrowPointExpectedException(token,exception);
6848 (void) GetNextToken(p,&p,MagickPathExtent,token);
6849 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6850 ThrowPointExpectedException(token,exception);
6851 if (*token == ',')
6852 (void) GetNextToken(p,&p,MagickPathExtent,token);
6853 angle=GetDrawValue(token,&next_token);
6854 if (token == next_token)
6855 ThrowPointExpectedException(token,exception);
6856 (void) GetNextToken(p,&p,MagickPathExtent,token);
6857 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6858 ThrowPointExpectedException(token,exception);
6859 if (*token == ',')
6860 (void) GetNextToken(p,&p,MagickPathExtent,token);
6861 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6862 (void) GetNextToken(p,&p,MagickPathExtent,token);
6863 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6864 ThrowPointExpectedException(token,exception);
6865 if (*token == ',')
6866 (void) GetNextToken(p,&p,MagickPathExtent,token);
6867 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6868 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6869 ThrowPointExpectedException(token,exception);
6870 if (*token == ',')
6871 (void) GetNextToken(p,&p,MagickPathExtent,token);
6872 (void) GetNextToken(p,&p,MagickPathExtent,token);
6873 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6874 ThrowPointExpectedException(token,exception);
6875 if (*token == ',')
6876 (void) GetNextToken(p,&p,MagickPathExtent,token);
6877 x=GetDrawValue(token,&next_token);
6878 if (token == next_token)
6879 ThrowPointExpectedException(token,exception);
6880 (void) GetNextToken(p,&p,MagickPathExtent,token);
6881 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6882 ThrowPointExpectedException(token,exception);
6883 if (*token == ',')
6884 (void) GetNextToken(p,&p,MagickPathExtent,token);
6885 y=GetDrawValue(token,&next_token);
6886 if (token == next_token)
6887 ThrowPointExpectedException(token,exception);
6888 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
6889 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
6890 if (TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep) == MagickFalse)
6891 return(-1);
6892 q=(*mvg_info->primitive_info)+mvg_info->offset;
6893 mvg_info->offset+=(ssize_t) q->coordinates;
6894 q+=(ptrdiff_t) q->coordinates;
6895 point=end;
6896 while (isspace((int) ((unsigned char) *p)) != 0)
6897 p++;
6898 if (*p == ',')
6899 p++;
6900 } while (IsValidPoint(p) != MagickFalse);
6901 break;
6902 }
6903 case 'c':
6904 case 'C':
6905 {
6906 /*
6907 Cubic Bézier curve.
6908 */
6909 do
6910 {
6911 points[0]=point;
6912 for (i=1; i < 4; i++)
6913 {
6914 (void) GetNextToken(p,&p,MagickPathExtent,token);
6915 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6916 ThrowPointExpectedException(token,exception);
6917 if (*token == ',')
6918 (void) GetNextToken(p,&p,MagickPathExtent,token);
6919 x=GetDrawValue(token,&next_token);
6920 if (token == next_token)
6921 ThrowPointExpectedException(token,exception);
6922 (void) GetNextToken(p,&p,MagickPathExtent,token);
6923 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6924 ThrowPointExpectedException(token,exception);
6925 if (*token == ',')
6926 (void) GetNextToken(p,&p,MagickPathExtent,token);
6927 y=GetDrawValue(token,&next_token);
6928 if (token == next_token)
6929 ThrowPointExpectedException(token,exception);
6930 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
6931 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
6932 points[i]=end;
6933 }
6934 for (i=0; i < 4; i++)
6935 (q+i)->point=points[i];
6936 if (TraceBezier(mvg_info,4) == MagickFalse)
6937 return(-1);
6938 q=(*mvg_info->primitive_info)+mvg_info->offset;
6939 mvg_info->offset+=(ssize_t) q->coordinates;
6940 q+=(ptrdiff_t) q->coordinates;
6941 point=end;
6942 while (isspace((int) ((unsigned char) *p)) != 0)
6943 p++;
6944 if (*p == ',')
6945 p++;
6946 } while (IsValidPoint(p) != MagickFalse);
6947 break;
6948 }
6949 case 'H':
6950 case 'h':
6951 {
6952 do
6953 {
6954 (void) GetNextToken(p,&p,MagickPathExtent,token);
6955 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6956 ThrowPointExpectedException(token,exception);
6957 if (*token == ',')
6958 (void) GetNextToken(p,&p,MagickPathExtent,token);
6959 x=GetDrawValue(token,&next_token);
6960 if (token == next_token)
6961 ThrowPointExpectedException(token,exception);
6962 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
6963 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6964 return(-1);
6965 q=(*mvg_info->primitive_info)+mvg_info->offset;
6966 if (TracePoint(q,point) == MagickFalse)
6967 return(-1);
6968 mvg_info->offset+=(ssize_t) q->coordinates;
6969 q+=(ptrdiff_t) q->coordinates;
6970 while (isspace((int) ((unsigned char) *p)) != 0)
6971 p++;
6972 if (*p == ',')
6973 p++;
6974 } while (IsValidPoint(p) != MagickFalse);
6975 break;
6976 }
6977 case 'l':
6978 case 'L':
6979 {
6980 /*
6981 Line to.
6982 */
6983 do
6984 {
6985 (void) GetNextToken(p,&p,MagickPathExtent,token);
6986 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6987 ThrowPointExpectedException(token,exception);
6988 if (*token == ',')
6989 (void) GetNextToken(p,&p,MagickPathExtent,token);
6990 x=GetDrawValue(token,&next_token);
6991 if (token == next_token)
6992 ThrowPointExpectedException(token,exception);
6993 (void) GetNextToken(p,&p,MagickPathExtent,token);
6994 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6995 ThrowPointExpectedException(token,exception);
6996 if (*token == ',')
6997 (void) GetNextToken(p,&p,MagickPathExtent,token);
6998 y=GetDrawValue(token,&next_token);
6999 if (token == next_token)
7000 ThrowPointExpectedException(token,exception);
7001 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
7002 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
7003 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7004 return(-1);
7005 q=(*mvg_info->primitive_info)+mvg_info->offset;
7006 if (TracePoint(q,point) == MagickFalse)
7007 return(-1);
7008 mvg_info->offset+=(ssize_t) q->coordinates;
7009 q+=(ptrdiff_t) q->coordinates;
7010 while (isspace((int) ((unsigned char) *p)) != 0)
7011 p++;
7012 if (*p == ',')
7013 p++;
7014 } while (IsValidPoint(p) != MagickFalse);
7015 break;
7016 }
7017 case 'M':
7018 case 'm':
7019 {
7020 /*
7021 Move to.
7022 */
7023 if (mvg_info->offset != subpath_offset)
7024 {
7025 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7026 primitive_info->coordinates=(size_t) (q-primitive_info);
7027 number_coordinates+=primitive_info->coordinates;
7028 primitive_info=q;
7029 subpath_offset=mvg_info->offset;
7030 }
7031 i=0;
7032 do
7033 {
7034 (void) GetNextToken(p,&p,MagickPathExtent,token);
7035 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7036 ThrowPointExpectedException(token,exception);
7037 if (*token == ',')
7038 (void) GetNextToken(p,&p,MagickPathExtent,token);
7039 x=GetDrawValue(token,&next_token);
7040 if (token == next_token)
7041 ThrowPointExpectedException(token,exception);
7042 (void) GetNextToken(p,&p,MagickPathExtent,token);
7043 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7044 ThrowPointExpectedException(token,exception);
7045 if (*token == ',')
7046 (void) GetNextToken(p,&p,MagickPathExtent,token);
7047 y=GetDrawValue(token,&next_token);
7048 if (token == next_token)
7049 ThrowPointExpectedException(token,exception);
7050 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
7051 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
7052 if (i == 0)
7053 start=point;
7054 i++;
7055 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7056 return(-1);
7057 q=(*mvg_info->primitive_info)+mvg_info->offset;
7058 if (TracePoint(q,point) == MagickFalse)
7059 return(-1);
7060 mvg_info->offset+=(ssize_t) q->coordinates;
7061 q+=(ptrdiff_t) q->coordinates;
7062 while (isspace((int) ((unsigned char) *p)) != 0)
7063 p++;
7064 if (*p == ',')
7065 p++;
7066 } while (IsValidPoint(p) != MagickFalse);
7067 break;
7068 }
7069 case 'q':
7070 case 'Q':
7071 {
7072 /*
7073 Quadratic Bézier curve.
7074 */
7075 do
7076 {
7077 points[0]=point;
7078 for (i=1; i < 3; i++)
7079 {
7080 (void) GetNextToken(p,&p,MagickPathExtent,token);
7081 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7082 ThrowPointExpectedException(token,exception);
7083 if (*token == ',')
7084 (void) GetNextToken(p,&p,MagickPathExtent,token);
7085 x=GetDrawValue(token,&next_token);
7086 if (token == next_token)
7087 ThrowPointExpectedException(token,exception);
7088 (void) GetNextToken(p,&p,MagickPathExtent,token);
7089 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7090 ThrowPointExpectedException(token,exception);
7091 if (*token == ',')
7092 (void) GetNextToken(p,&p,MagickPathExtent,token);
7093 y=GetDrawValue(token,&next_token);
7094 if (token == next_token)
7095 ThrowPointExpectedException(token,exception);
7096 if (*p == ',')
7097 p++;
7098 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
7099 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
7100 points[i]=end;
7101 }
7102 for (i=0; i < 3; i++)
7103 (q+i)->point=points[i];
7104 if (TraceBezier(mvg_info,3) == MagickFalse)
7105 return(-1);
7106 q=(*mvg_info->primitive_info)+mvg_info->offset;
7107 mvg_info->offset+=(ssize_t) q->coordinates;
7108 q+=(ptrdiff_t) q->coordinates;
7109 point=end;
7110 while (isspace((int) ((unsigned char) *p)) != 0)
7111 p++;
7112 if (*p == ',')
7113 p++;
7114 } while (IsValidPoint(p) != MagickFalse);
7115 break;
7116 }
7117 case 's':
7118 case 'S':
7119 {
7120 /*
7121 Cubic Bézier curve.
7122 */
7123 do
7124 {
7125 points[0]=points[3];
7126 points[1].x=2.0*points[3].x-points[2].x;
7127 points[1].y=2.0*points[3].y-points[2].y;
7128 for (i=2; i < 4; i++)
7129 {
7130 (void) GetNextToken(p,&p,MagickPathExtent,token);
7131 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7132 ThrowPointExpectedException(token,exception);
7133 if (*token == ',')
7134 (void) GetNextToken(p,&p,MagickPathExtent,token);
7135 x=GetDrawValue(token,&next_token);
7136 if (token == next_token)
7137 ThrowPointExpectedException(token,exception);
7138 (void) GetNextToken(p,&p,MagickPathExtent,token);
7139 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7140 ThrowPointExpectedException(token,exception);
7141 if (*token == ',')
7142 (void) GetNextToken(p,&p,MagickPathExtent,token);
7143 y=GetDrawValue(token,&next_token);
7144 if (token == next_token)
7145 ThrowPointExpectedException(token,exception);
7146 if (*p == ',')
7147 p++;
7148 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
7149 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
7150 points[i]=end;
7151 }
7152 if (strchr("CcSs",last_attribute) == (char *) NULL)
7153 {
7154 points[0]=point;
7155 points[1]=point;
7156 }
7157 for (i=0; i < 4; i++)
7158 (q+i)->point=points[i];
7159 if (TraceBezier(mvg_info,4) == MagickFalse)
7160 return(-1);
7161 q=(*mvg_info->primitive_info)+mvg_info->offset;
7162 mvg_info->offset+=(ssize_t) q->coordinates;
7163 q+=(ptrdiff_t) q->coordinates;
7164 point=end;
7165 last_attribute=attribute;
7166 while (isspace((int) ((unsigned char) *p)) != 0)
7167 p++;
7168 if (*p == ',')
7169 p++;
7170 } while (IsValidPoint(p) != MagickFalse);
7171 break;
7172 }
7173 case 't':
7174 case 'T':
7175 {
7176 /*
7177 Quadratic Bézier curve.
7178 */
7179 do
7180 {
7181 points[0]=points[2];
7182 points[1].x=2.0*points[2].x-points[1].x;
7183 points[1].y=2.0*points[2].y-points[1].y;
7184 for (i=2; i < 3; i++)
7185 {
7186 (void) GetNextToken(p,&p,MagickPathExtent,token);
7187 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7188 ThrowPointExpectedException(token,exception);
7189 if (*token == ',')
7190 (void) GetNextToken(p,&p,MagickPathExtent,token);
7191 x=GetDrawValue(token,&next_token);
7192 if (token == next_token)
7193 ThrowPointExpectedException(token,exception);
7194 (void) GetNextToken(p,&p,MagickPathExtent,token);
7195 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7196 ThrowPointExpectedException(token,exception);
7197 if (*token == ',')
7198 (void) GetNextToken(p,&p,MagickPathExtent,token);
7199 y=GetDrawValue(token,&next_token);
7200 if (token == next_token)
7201 ThrowPointExpectedException(token,exception);
7202 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
7203 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
7204 points[i]=end;
7205 }
7206 if (status == MagickFalse)
7207 break;
7208 if (strchr("QqTt",last_attribute) == (char *) NULL)
7209 {
7210 points[0]=point;
7211 points[1]=point;
7212 }
7213 for (i=0; i < 3; i++)
7214 (q+i)->point=points[i];
7215 if (TraceBezier(mvg_info,3) == MagickFalse)
7216 return(-1);
7217 q=(*mvg_info->primitive_info)+mvg_info->offset;
7218 mvg_info->offset+=(ssize_t) q->coordinates;
7219 q+=(ptrdiff_t) q->coordinates;
7220 point=end;
7221 last_attribute=attribute;
7222 while (isspace((int) ((unsigned char) *p)) != 0)
7223 p++;
7224 if (*p == ',')
7225 p++;
7226 } while (IsValidPoint(p) != MagickFalse);
7227 break;
7228 }
7229 case 'v':
7230 case 'V':
7231 {
7232 /*
7233 Line to.
7234 */
7235 do
7236 {
7237 (void) GetNextToken(p,&p,MagickPathExtent,token);
7238 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7239 ThrowPointExpectedException(token,exception);
7240 if (*token == ',')
7241 (void) GetNextToken(p,&p,MagickPathExtent,token);
7242 y=GetDrawValue(token,&next_token);
7243 if (token == next_token)
7244 ThrowPointExpectedException(token,exception);
7245 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
7246 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7247 return(-1);
7248 q=(*mvg_info->primitive_info)+mvg_info->offset;
7249 if (TracePoint(q,point) == MagickFalse)
7250 return(-1);
7251 mvg_info->offset+=(ssize_t) q->coordinates;
7252 q+=(ptrdiff_t) q->coordinates;
7253 while (isspace((int) ((unsigned char) *p)) != 0)
7254 p++;
7255 if (*p == ',')
7256 p++;
7257 } while (IsValidPoint(p) != MagickFalse);
7258 break;
7259 }
7260 case 'z':
7261 case 'Z':
7262 {
7263 /*
7264 Close path.
7265 */
7266 point=start;
7267 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7268 return(-1);
7269 q=(*mvg_info->primitive_info)+mvg_info->offset;
7270 if (TracePoint(q,point) == MagickFalse)
7271 return(-1);
7272 mvg_info->offset+=(ssize_t) q->coordinates;
7273 q+=(ptrdiff_t) q->coordinates;
7274 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7275 primitive_info->coordinates=(size_t) (q-primitive_info);
7276 primitive_info->closed_subpath=MagickTrue;
7277 number_coordinates+=primitive_info->coordinates;
7278 primitive_info=q;
7279 subpath_offset=mvg_info->offset;
7280 z_count++;
7281 break;
7282 }
7283 default:
7284 {
7285 ThrowPointExpectedException(token,exception);
7286 break;
7287 }
7288 }
7289 }
7290 if (status == MagickFalse)
7291 return(-1);
7292 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7293 primitive_info->coordinates=(size_t) (q-primitive_info);
7294 number_coordinates+=primitive_info->coordinates;
7295 for (i=0; i < (ssize_t) number_coordinates; i++)
7296 {
7297 q--;
7298 q->primitive=primitive_type;
7299 if (z_count > 1)
7300 q->method=FillToBorderMethod;
7301 }
7302 q=primitive_info;
7303 return((ssize_t) number_coordinates);
7304}
7305
7306static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7307 const PointInfo start,const PointInfo end)
7308{
7309 PointInfo
7310 point;
7311
7313 *p;
7314
7315 ssize_t
7316 i;
7317
7318 p=primitive_info;
7319 if (TracePoint(p,start) == MagickFalse)
7320 return(MagickFalse);
7321 p+=(ptrdiff_t) p->coordinates;
7322 point.x=start.x;
7323 point.y=end.y;
7324 if (TracePoint(p,point) == MagickFalse)
7325 return(MagickFalse);
7326 p+=(ptrdiff_t) p->coordinates;
7327 if (TracePoint(p,end) == MagickFalse)
7328 return(MagickFalse);
7329 p+=(ptrdiff_t) p->coordinates;
7330 point.x=end.x;
7331 point.y=start.y;
7332 if (TracePoint(p,point) == MagickFalse)
7333 return(MagickFalse);
7334 p+=(ptrdiff_t) p->coordinates;
7335 if (TracePoint(p,start) == MagickFalse)
7336 return(MagickFalse);
7337 p+=(ptrdiff_t) p->coordinates;
7338 primitive_info->coordinates=(size_t) (p-primitive_info);
7339 primitive_info->closed_subpath=MagickTrue;
7340 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7341 {
7342 p->primitive=primitive_info->primitive;
7343 p--;
7344 }
7345 return(MagickTrue);
7346}
7347
7348static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7349 const PointInfo start,const PointInfo end,PointInfo arc)
7350{
7351 PointInfo
7352 degrees,
7353 point,
7354 segment;
7355
7357 *primitive_info;
7358
7360 *p;
7361
7362 ssize_t
7363 i;
7364
7365 ssize_t
7366 offset;
7367
7368 offset=mvg_info->offset;
7369 segment.x=fabs(end.x-start.x);
7370 segment.y=fabs(end.y-start.y);
7371 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7372 {
7373 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7374 return(MagickTrue);
7375 }
7376 if (arc.x > (0.5*segment.x))
7377 arc.x=0.5*segment.x;
7378 if (arc.y > (0.5*segment.y))
7379 arc.y=0.5*segment.y;
7380 point.x=start.x+segment.x-arc.x;
7381 point.y=start.y+arc.y;
7382 degrees.x=270.0;
7383 degrees.y=360.0;
7384 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7385 return(MagickFalse);
7386 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7387 mvg_info->offset+=(ssize_t) p->coordinates;
7388 point.x=start.x+segment.x-arc.x;
7389 point.y=start.y+segment.y-arc.y;
7390 degrees.x=0.0;
7391 degrees.y=90.0;
7392 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7393 return(MagickFalse);
7394 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7395 mvg_info->offset+=(ssize_t) p->coordinates;
7396 point.x=start.x+arc.x;
7397 point.y=start.y+segment.y-arc.y;
7398 degrees.x=90.0;
7399 degrees.y=180.0;
7400 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7401 return(MagickFalse);
7402 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7403 mvg_info->offset+=(ssize_t) p->coordinates;
7404 point.x=start.x+arc.x;
7405 point.y=start.y+arc.y;
7406 degrees.x=180.0;
7407 degrees.y=270.0;
7408 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7409 return(MagickFalse);
7410 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7411 mvg_info->offset+=(ssize_t) p->coordinates;
7412 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7413 return(MagickFalse);
7414 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7415 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7416 return(MagickFalse);
7417 p+=(ptrdiff_t) p->coordinates;
7418 mvg_info->offset=offset;
7419 primitive_info=(*mvg_info->primitive_info)+offset;
7420 primitive_info->coordinates=(size_t) (p-primitive_info);
7421 primitive_info->closed_subpath=MagickTrue;
7422 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7423 {
7424 p->primitive=primitive_info->primitive;
7425 p--;
7426 }
7427 return(MagickTrue);
7428}
7429
7430static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7431 const size_t number_vertices,const double offset)
7432{
7433 double
7434 distance;
7435
7436 double
7437 dx,
7438 dy;
7439
7440 ssize_t
7441 i;
7442
7443 ssize_t
7444 j;
7445
7446 dx=0.0;
7447 dy=0.0;
7448 for (i=1; i < (ssize_t) number_vertices; i++)
7449 {
7450 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7451 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7452 if ((fabs((double) dx) >= MagickEpsilon) ||
7453 (fabs((double) dy) >= MagickEpsilon))
7454 break;
7455 }
7456 if (i == (ssize_t) number_vertices)
7457 i=(ssize_t) number_vertices-1L;
7458 distance=hypot((double) dx,(double) dy);
7459 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7460 dx*(distance+offset)/distance);
7461 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7462 dy*(distance+offset)/distance);
7463 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7464 {
7465 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7466 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7467 if ((fabs((double) dx) >= MagickEpsilon) ||
7468 (fabs((double) dy) >= MagickEpsilon))
7469 break;
7470 }
7471 distance=hypot((double) dx,(double) dy);
7472 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7473 dx*(distance+offset)/distance);
7474 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7475 dy*(distance+offset)/distance);
7476 return(MagickTrue);
7477}
7478
7479static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
7480 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7481{
7482#define MaxStrokePad (6*BezierQuantum+360)
7483#define CheckPathExtent(pad_p,pad_q) \
7484{ \
7485 if ((pad_p) > MaxBezierCoordinates) \
7486 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7487 else \
7488 if ((p+(ptrdiff_t) (pad_p)) >= (ssize_t) extent_p) \
7489 { \
7490 if (~extent_p < (pad_p)) \
7491 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7492 else \
7493 { \
7494 extent_p+=(pad_p); \
7495 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7496 MaxStrokePad,sizeof(*stroke_p)); \
7497 } \
7498 } \
7499 if ((pad_q) > MaxBezierCoordinates) \
7500 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7501 else \
7502 if ((q+(ptrdiff_t) (pad_q)) >= (ssize_t) extent_q) \
7503 { \
7504 if (~extent_q < (pad_q)) \
7505 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7506 else \
7507 { \
7508 extent_q+=(pad_q); \
7509 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7510 MaxStrokePad,sizeof(*stroke_q)); \
7511 } \
7512 } \
7513 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7514 { \
7515 if (stroke_p != (PointInfo *) NULL) \
7516 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7517 if (stroke_q != (PointInfo *) NULL) \
7518 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7519 polygon_primitive=(PrimitiveInfo *) \
7520 RelinquishMagickMemory(polygon_primitive); \
7521 (void) ThrowMagickException(exception,GetMagickModule(), \
7522 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7523 return((PrimitiveInfo *) NULL); \
7524 } \
7525}
7526
7527 typedef struct _StrokeSegment
7528 {
7529 double
7530 p,
7531 q;
7532 } StrokeSegment;
7533
7534 double
7535 delta_theta,
7536 dot_product,
7537 mid,
7538 miterlimit;
7539
7540 MagickBooleanType
7541 closed_path;
7542
7543 PointInfo
7544 box_p[5],
7545 box_q[5],
7546 center,
7547 offset,
7548 *stroke_p,
7549 *stroke_q;
7550
7552 *polygon_primitive,
7553 *stroke_polygon;
7554
7555 ssize_t
7556 i;
7557
7558 size_t
7559 arc_segments,
7560 extent_p,
7561 extent_q,
7562 number_vertices;
7563
7564 ssize_t
7565 j,
7566 n,
7567 p,
7568 q;
7569
7570 StrokeSegment
7571 dx = {0.0, 0.0},
7572 dy = {0.0, 0.0},
7573 inverse_slope = {0.0, 0.0},
7574 slope = {0.0, 0.0},
7575 theta = {0.0, 0.0};
7576
7577 /*
7578 Allocate paths.
7579 */
7580 number_vertices=primitive_info->coordinates;
7581 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7582 number_vertices+2UL,sizeof(*polygon_primitive));
7583 if (polygon_primitive == (PrimitiveInfo *) NULL)
7584 {
7585 (void) ThrowMagickException(exception,GetMagickModule(),
7586 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7587 return((PrimitiveInfo *) NULL);
7588 }
7589 (void) memcpy(polygon_primitive,primitive_info,(size_t) number_vertices*
7590 sizeof(*polygon_primitive));
7591 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7592 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7593 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7594 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7595 if ((draw_info->linejoin == RoundJoin) ||
7596 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
7597 {
7598 polygon_primitive[number_vertices]=primitive_info[1];
7599 number_vertices++;
7600 }
7601 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7602 /*
7603 Compute the slope for the first line segment, p.
7604 */
7605 closed_path=primitive_info[0].closed_subpath;
7606 dx.p=0.0;
7607 dy.p=0.0;
7608 for (n=1; n < (ssize_t) number_vertices; n++)
7609 {
7610 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7611 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7612 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7613 break;
7614 }
7615 if (n == (ssize_t) number_vertices)
7616 {
7617 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7618 {
7619 /*
7620 Zero length subpath.
7621 */
7622 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7623 sizeof(*stroke_polygon));
7624 stroke_polygon[0]=polygon_primitive[0];
7625 stroke_polygon[0].coordinates=0;
7626 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7627 polygon_primitive);
7628 return(stroke_polygon);
7629 }
7630 n=(ssize_t) number_vertices-1L;
7631 }
7632 extent_p=2*number_vertices;
7633 extent_q=2*number_vertices;
7634 stroke_p=(PointInfo *) AcquireQuantumMemory((size_t) extent_p+MaxStrokePad,
7635 sizeof(*stroke_p));
7636 stroke_q=(PointInfo *) AcquireQuantumMemory((size_t) extent_q+MaxStrokePad,
7637 sizeof(*stroke_q));
7638 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7639 {
7640 if (stroke_p != (PointInfo *) NULL)
7641 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7642 if (stroke_q != (PointInfo *) NULL)
7643 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7644 polygon_primitive=(PrimitiveInfo *)
7645 RelinquishMagickMemory(polygon_primitive);
7646 (void) ThrowMagickException(exception,GetMagickModule(),
7647 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7648 return((PrimitiveInfo *) NULL);
7649 }
7650 slope.p=0.0;
7651 inverse_slope.p=0.0;
7652 if (fabs(dx.p) < MagickEpsilon)
7653 {
7654 if (dx.p >= 0.0)
7655 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7656 else
7657 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7658 }
7659 else
7660 if (fabs(dy.p) < MagickEpsilon)
7661 {
7662 if (dy.p >= 0.0)
7663 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7664 else
7665 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7666 }
7667 else
7668 {
7669 slope.p=dy.p/dx.p;
7670 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7671 }
7672 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7673 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7674 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7675 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7676 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7677 offset.y=(double) (offset.x*inverse_slope.p);
7678 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7679 {
7680 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7681 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7682 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7683 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7684 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7685 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7686 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7687 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7688 }
7689 else
7690 {
7691 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7692 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7693 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7694 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7695 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7696 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7697 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7698 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7699 }
7700 /*
7701 Create strokes for the line join attribute: bevel, miter, round.
7702 */
7703 p=0;
7704 q=0;
7705 stroke_q[p++]=box_q[0];
7706 stroke_p[q++]=box_p[0];
7707 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7708 {
7709 /*
7710 Compute the slope for this line segment, q.
7711 */
7712 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7713 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7714 dot_product=dx.q*dx.q+dy.q*dy.q;
7715 if (dot_product < 0.25)
7716 continue;
7717 slope.q=0.0;
7718 inverse_slope.q=0.0;
7719 if (fabs(dx.q) < MagickEpsilon)
7720 {
7721 if (dx.q >= 0.0)
7722 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7723 else
7724 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7725 }
7726 else
7727 if (fabs(dy.q) < MagickEpsilon)
7728 {
7729 if (dy.q >= 0.0)
7730 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7731 else
7732 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7733 }
7734 else
7735 {
7736 slope.q=dy.q/dx.q;
7737 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7738 }
7739 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7740 offset.y=(double) (offset.x*inverse_slope.q);
7741 dot_product=dy.q*offset.x-dx.q*offset.y;
7742 if (dot_product > 0.0)
7743 {
7744 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7745 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7746 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7747 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7748 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7749 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7750 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7751 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7752 }
7753 else
7754 {
7755 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7756 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7757 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7758 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7759 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7760 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7761 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7762 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7763 }
7764 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
7765 {
7766 box_p[4]=box_p[1];
7767 box_q[4]=box_q[1];
7768 }
7769 else
7770 {
7771 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7772 box_p[3].y)/(slope.p-slope.q));
7773 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7774 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7775 box_q[3].y)/(slope.p-slope.q));
7776 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7777 }
7778 DisableMSCWarning(4127)
7779 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7780 RestoreMSCWarning
7781 dot_product=dx.q*dy.p-dx.p*dy.q;
7782 if (dot_product <= 0.0)
7783 switch (draw_info->linejoin)
7784 {
7785 case BevelJoin:
7786 {
7787 stroke_q[q++]=box_q[1];
7788 stroke_q[q++]=box_q[2];
7789 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7790 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7791 if (dot_product <= miterlimit)
7792 stroke_p[p++]=box_p[4];
7793 else
7794 {
7795 stroke_p[p++]=box_p[1];
7796 stroke_p[p++]=box_p[2];
7797 }
7798 break;
7799 }
7800 case MiterJoin:
7801 {
7802 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7803 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7804 if (dot_product <= miterlimit)
7805 {
7806 stroke_q[q++]=box_q[4];
7807 stroke_p[p++]=box_p[4];
7808 }
7809 else
7810 {
7811 stroke_q[q++]=box_q[1];
7812 stroke_q[q++]=box_q[2];
7813 stroke_p[p++]=box_p[1];
7814 stroke_p[p++]=box_p[2];
7815 }
7816 break;
7817 }
7818 case RoundJoin:
7819 {
7820 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7821 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7822 if (dot_product <= miterlimit)
7823 stroke_p[p++]=box_p[4];
7824 else
7825 {
7826 stroke_p[p++]=box_p[1];
7827 stroke_p[p++]=box_p[2];
7828 }
7829 center=polygon_primitive[n].point;
7830 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7831 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7832 if (theta.q < theta.p)
7833 theta.q+=2.0*MagickPI;
7834 arc_segments=(size_t) CastDoubleToSsizeT(ceil((double) ((theta.q-
7835 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7836 DisableMSCWarning(4127)
7837 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7838 RestoreMSCWarning
7839 stroke_q[q].x=box_q[1].x;
7840 stroke_q[q].y=box_q[1].y;
7841 q++;
7842 for (j=1; j < (ssize_t) arc_segments; j++)
7843 {
7844 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7845 stroke_q[q].x=(double) (center.x+mid*cos(fmod((double)
7846 (theta.p+delta_theta),DegreesToRadians(360.0))));
7847 stroke_q[q].y=(double) (center.y+mid*sin(fmod((double)
7848 (theta.p+delta_theta),DegreesToRadians(360.0))));
7849 q++;
7850 }
7851 stroke_q[q++]=box_q[2];
7852 break;
7853 }
7854 default:
7855 break;
7856 }
7857 else
7858 switch (draw_info->linejoin)
7859 {
7860 case BevelJoin:
7861 {
7862 stroke_p[p++]=box_p[1];
7863 stroke_p[p++]=box_p[2];
7864 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7865 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7866 if (dot_product <= miterlimit)
7867 stroke_q[q++]=box_q[4];
7868 else
7869 {
7870 stroke_q[q++]=box_q[1];
7871 stroke_q[q++]=box_q[2];
7872 }
7873 break;
7874 }
7875 case MiterJoin:
7876 {
7877 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7878 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7879 if (dot_product <= miterlimit)
7880 {
7881 stroke_q[q++]=box_q[4];
7882 stroke_p[p++]=box_p[4];
7883 }
7884 else
7885 {
7886 stroke_q[q++]=box_q[1];
7887 stroke_q[q++]=box_q[2];
7888 stroke_p[p++]=box_p[1];
7889 stroke_p[p++]=box_p[2];
7890 }
7891 break;
7892 }
7893 case RoundJoin:
7894 {
7895 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7896 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7897 if (dot_product <= miterlimit)
7898 stroke_q[q++]=box_q[4];
7899 else
7900 {
7901 stroke_q[q++]=box_q[1];
7902 stroke_q[q++]=box_q[2];
7903 }
7904 center=polygon_primitive[n].point;
7905 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7906 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7907 if (theta.p < theta.q)
7908 theta.p+=2.0*MagickPI;
7909 arc_segments=(size_t) CastDoubleToSsizeT(ceil((double) ((theta.p-
7910 theta.q)/(2.0*sqrt((double) (MagickSafeReciprocal(mid)))))));
7911 DisableMSCWarning(4127)
7912 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7913 RestoreMSCWarning
7914 stroke_p[p++]=box_p[1];
7915 for (j=1; j < (ssize_t) arc_segments; j++)
7916 {
7917 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7918 stroke_p[p].x=(double) (center.x+mid*cos(fmod((double)
7919 (theta.p+delta_theta),DegreesToRadians(360.0))));
7920 stroke_p[p].y=(double) (center.y+mid*sin(fmod((double)
7921 (theta.p+delta_theta),DegreesToRadians(360.0))));
7922 p++;
7923 }
7924 stroke_p[p++]=box_p[2];
7925 break;
7926 }
7927 default:
7928 break;
7929 }
7930 slope.p=slope.q;
7931 inverse_slope.p=inverse_slope.q;
7932 box_p[0]=box_p[2];
7933 box_p[1]=box_p[3];
7934 box_q[0]=box_q[2];
7935 box_q[1]=box_q[3];
7936 dx.p=dx.q;
7937 dy.p=dy.q;
7938 n=i;
7939 }
7940 stroke_p[p++]=box_p[1];
7941 stroke_q[q++]=box_q[1];
7942 /*
7943 Trace stroked polygon.
7944 */
7945 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7946 (p+q+2L),(size_t) (closed_path+2L)*sizeof(*stroke_polygon));
7947 if (stroke_polygon == (PrimitiveInfo *) NULL)
7948 {
7949 (void) ThrowMagickException(exception,GetMagickModule(),
7950 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7951 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7952 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7953 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7954 polygon_primitive);
7955 return(stroke_polygon);
7956 }
7957 for (i=0; i < (ssize_t) p; i++)
7958 {
7959 stroke_polygon[i]=polygon_primitive[0];
7960 stroke_polygon[i].point=stroke_p[i];
7961 }
7962 if (closed_path != MagickFalse)
7963 {
7964 stroke_polygon[i]=polygon_primitive[0];
7965 stroke_polygon[i].point=stroke_polygon[0].point;
7966 i++;
7967 }
7968 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7969 {
7970 stroke_polygon[i]=polygon_primitive[0];
7971 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7972 }
7973 if (closed_path != MagickFalse)
7974 {
7975 stroke_polygon[i]=polygon_primitive[0];
7976 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7977 i++;
7978 }
7979 stroke_polygon[i]=polygon_primitive[0];
7980 stroke_polygon[i].point=stroke_polygon[0].point;
7981 i++;
7982 stroke_polygon[i].primitive=UndefinedPrimitive;
7983 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7984 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7985 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7986 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7987 return(stroke_polygon);
7988}