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