MagickCore 7.1.2-34
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]->compliance != SVGCompliance) ||
3051 (graphic_context[n]->fill.alpha != (double) TransparentAlpha)))
3052 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3053 break;
3054 }
3055 if (LocaleCompare("fill-opacity",keyword) == 0)
3056 {
3057 double
3058 opacity;
3059
3060 (void) GetNextToken(q,&q,extent,token);
3061 if (graphic_context[n]->clip_path != MagickFalse)
3062 break;
3063 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3064 opacity=MagickMin(MagickMax(factor*
3065 GetDrawValue(token,&next_token),0.0),1.0);
3066 if (token == next_token)
3067 ThrowPointExpectedException(token,exception);
3068 if (graphic_context[n]->compliance == SVGCompliance)
3069 graphic_context[n]->fill_alpha*=opacity;
3070 else
3071 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3072 if (graphic_context[n]->fill.alpha != (double) TransparentAlpha)
3073 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3074 else
3075 if (graphic_context[n]->compliance != SVGCompliance)
3076 graphic_context[n]->fill.alpha=(MagickRealType)
3077 ClampToQuantum((double) QuantumRange*opacity);
3078 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3079 break;
3080 }
3081 if (LocaleCompare("fill-rule",keyword) == 0)
3082 {
3083 ssize_t
3084 fill_rule;
3085
3086 (void) GetNextToken(q,&q,extent,token);
3087 fill_rule=ParseCommandOption(MagickFillRuleOptions,MagickFalse,
3088 token);
3089 if (fill_rule == -1)
3090 {
3091 status=MagickFalse;
3092 break;
3093 }
3094 graphic_context[n]->fill_rule=(FillRule) fill_rule;
3095 break;
3096 }
3097 if (LocaleCompare("font",keyword) == 0)
3098 {
3099 (void) GetNextToken(q,&q,extent,token);
3100 (void) CloneString(&graphic_context[n]->font,token);
3101 if (LocaleCompare("none",token) == 0)
3102 graphic_context[n]->font=(char *) RelinquishMagickMemory(
3103 graphic_context[n]->font);
3104 break;
3105 }
3106 if (LocaleCompare("font-family",keyword) == 0)
3107 {
3108 (void) GetNextToken(q,&q,extent,token);
3109 (void) CloneString(&graphic_context[n]->family,token);
3110 break;
3111 }
3112 if (LocaleCompare("font-size",keyword) == 0)
3113 {
3114 (void) GetNextToken(q,&q,extent,token);
3115 graphic_context[n]->pointsize=GetDrawValue(token,&next_token);
3116 if (token == next_token)
3117 ThrowPointExpectedException(token,exception);
3118 break;
3119 }
3120 if (LocaleCompare("font-stretch",keyword) == 0)
3121 {
3122 ssize_t
3123 stretch;
3124
3125 (void) GetNextToken(q,&q,extent,token);
3126 stretch=ParseCommandOption(MagickStretchOptions,MagickFalse,token);
3127 if (stretch == -1)
3128 {
3129 status=MagickFalse;
3130 break;
3131 }
3132 graphic_context[n]->stretch=(StretchType) stretch;
3133 break;
3134 }
3135 if (LocaleCompare("font-style",keyword) == 0)
3136 {
3137 ssize_t
3138 style;
3139
3140 (void) GetNextToken(q,&q,extent,token);
3141 style=ParseCommandOption(MagickStyleOptions,MagickFalse,token);
3142 if (style == -1)
3143 {
3144 status=MagickFalse;
3145 break;
3146 }
3147 graphic_context[n]->style=(StyleType) style;
3148 break;
3149 }
3150 if (LocaleCompare("font-weight",keyword) == 0)
3151 {
3152 ssize_t
3153 weight;
3154
3155 (void) GetNextToken(q,&q,extent,token);
3156 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,token);
3157 if (weight == -1)
3158 weight=(ssize_t) StringToUnsignedLong(token);
3159 graphic_context[n]->weight=(size_t) weight;
3160 break;
3161 }
3162 status=MagickFalse;
3163 break;
3164 }
3165 case 'g':
3166 case 'G':
3167 {
3168 if (LocaleCompare("gradient-units",keyword) == 0)
3169 {
3170 (void) GetNextToken(q,&q,extent,token);
3171 break;
3172 }
3173 if (LocaleCompare("gravity",keyword) == 0)
3174 {
3175 ssize_t
3176 gravity;
3177
3178 (void) GetNextToken(q,&q,extent,token);
3179 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,token);
3180 if (gravity == -1)
3181 {
3182 status=MagickFalse;
3183 break;
3184 }
3185 graphic_context[n]->gravity=(GravityType) gravity;
3186 break;
3187 }
3188 status=MagickFalse;
3189 break;
3190 }
3191 case 'i':
3192 case 'I':
3193 {
3194 if (LocaleCompare("image",keyword) == 0)
3195 {
3196 ssize_t
3197 compose;
3198
3199 primitive_type=ImagePrimitive;
3200 (void) GetNextToken(q,&q,extent,token);
3201 compose=ParseCommandOption(MagickComposeOptions,MagickFalse,token);
3202 if (compose == -1)
3203 {
3204 status=MagickFalse;
3205 break;
3206 }
3207 graphic_context[n]->compose=(CompositeOperator) compose;
3208 break;
3209 }
3210 if (LocaleCompare("interline-spacing",keyword) == 0)
3211 {
3212 (void) GetNextToken(q,&q,extent,token);
3213 graphic_context[n]->interline_spacing=GetDrawValue(token,
3214 &next_token);
3215 if (token == next_token)
3216 ThrowPointExpectedException(token,exception);
3217 break;
3218 }
3219 if (LocaleCompare("interword-spacing",keyword) == 0)
3220 {
3221 (void) GetNextToken(q,&q,extent,token);
3222 graphic_context[n]->interword_spacing=GetDrawValue(token,
3223 &next_token);
3224 if (token == next_token)
3225 ThrowPointExpectedException(token,exception);
3226 break;
3227 }
3228 status=MagickFalse;
3229 break;
3230 }
3231 case 'k':
3232 case 'K':
3233 {
3234 if (LocaleCompare("kerning",keyword) == 0)
3235 {
3236 (void) GetNextToken(q,&q,extent,token);
3237 graphic_context[n]->kerning=GetDrawValue(token,&next_token);
3238 if (token == next_token)
3239 ThrowPointExpectedException(token,exception);
3240 break;
3241 }
3242 status=MagickFalse;
3243 break;
3244 }
3245 case 'l':
3246 case 'L':
3247 {
3248 if (LocaleCompare("letter-spacing",keyword) == 0)
3249 {
3250 (void) GetNextToken(q,&q,extent,token);
3251 if (IsValidPoint(token) == MagickFalse)
3252 break;
3253 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
3254 clone_info->text=AcquireString(" ");
3255 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,&metrics,
3256 exception);
3257 graphic_context[n]->kerning=metrics.width*
3258 GetDrawValue(token,&next_token);
3259 clone_info=DestroyDrawInfo(clone_info);
3260 if (token == next_token)
3261 ThrowPointExpectedException(token,exception);
3262 break;
3263 }
3264 if (LocaleCompare("line",keyword) == 0)
3265 {
3266 primitive_type=LinePrimitive;
3267 break;
3268 }
3269 status=MagickFalse;
3270 break;
3271 }
3272 case 'm':
3273 case 'M':
3274 {
3275 if (LocaleCompare("mask",keyword) == 0)
3276 {
3277 const char
3278 *mask_path;
3279
3280 /*
3281 Take a node from within the MVG document, and duplicate it here.
3282 */
3283 (void) GetNextToken(q,&q,extent,token);
3284 mask_path=(const char *) GetValueFromSplayTree(macros,token);
3285 if (mask_path != (const char *) NULL)
3286 {
3287 if (graphic_context[n]->composite_mask != (Image *) NULL)
3288 graphic_context[n]->composite_mask=
3289 DestroyImage(graphic_context[n]->composite_mask);
3290 graphic_context[n]->composite_mask=DrawCompositeMask(image,
3291 graphic_context[n],token,mask_path,depth+1,exception);
3292 if (graphic_context[n]->compliance != SVGCompliance)
3293 status=SetImageMask(image,CompositePixelMask,
3294 graphic_context[n]->composite_mask,exception);
3295 }
3296 break;
3297 }
3298 status=MagickFalse;
3299 break;
3300 }
3301 case 'o':
3302 case 'O':
3303 {
3304 if (LocaleCompare("offset",keyword) == 0)
3305 {
3306 (void) GetNextToken(q,&q,extent,token);
3307 break;
3308 }
3309 if (LocaleCompare("opacity",keyword) == 0)
3310 {
3311 double
3312 opacity;
3313
3314 (void) GetNextToken(q,&q,extent,token);
3315 if (graphic_context[n]->clip_path != MagickFalse)
3316 break;
3317 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3318 opacity=MagickMin(MagickMax(factor*
3319 GetDrawValue(token,&next_token),0.0),1.0);
3320 if (token == next_token)
3321 ThrowPointExpectedException(token,exception);
3322 if (graphic_context[n]->compliance == SVGCompliance)
3323 {
3324 graphic_context[n]->fill_alpha*=opacity;
3325 graphic_context[n]->stroke_alpha*=opacity;
3326 }
3327 else
3328 {
3329 graphic_context[n]->fill_alpha=(double) QuantumRange*opacity;
3330 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3331 }
3332 if (graphic_context[n]->compliance == SVGCompliance)
3333 {
3334 if (graphic_context[n]->fill.alpha != (double) TransparentAlpha)
3335 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3336 if (graphic_context[n]->stroke.alpha != (double) TransparentAlpha)
3337 graphic_context[n]->stroke.alpha=
3338 graphic_context[n]->stroke_alpha;
3339 }
3340 else if (graphic_context[n]->fill.alpha != (double) TransparentAlpha)
3341 {
3342 graphic_context[n]->fill.alpha=graphic_context[n]->fill_alpha;
3343 graphic_context[n]->stroke.alpha=
3344 graphic_context[n]->stroke_alpha;
3345 }
3346 else
3347 {
3348 graphic_context[n]->fill.alpha=(MagickRealType)
3349 ClampToQuantum((double) QuantumRange*opacity);
3350 graphic_context[n]->stroke.alpha=(MagickRealType)
3351 ClampToQuantum((double) QuantumRange*opacity);
3352 }
3353 graphic_context[n]->fill.alpha_trait=BlendPixelTrait;
3354 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3355 break;
3356 }
3357 status=MagickFalse;
3358 break;
3359 }
3360 case 'p':
3361 case 'P':
3362 {
3363 if (LocaleCompare("path",keyword) == 0)
3364 {
3365 primitive_type=PathPrimitive;
3366 break;
3367 }
3368 if (LocaleCompare("point",keyword) == 0)
3369 {
3370 primitive_type=PointPrimitive;
3371 break;
3372 }
3373 if (LocaleCompare("polyline",keyword) == 0)
3374 {
3375 primitive_type=PolylinePrimitive;
3376 break;
3377 }
3378 if (LocaleCompare("polygon",keyword) == 0)
3379 {
3380 primitive_type=PolygonPrimitive;
3381 break;
3382 }
3383 if (LocaleCompare("pop",keyword) == 0)
3384 {
3385 if (GetNextToken(q,&q,extent,token) < 1)
3386 break;
3387 if (LocaleCompare("class",token) == 0)
3388 break;
3389 if (LocaleCompare("clip-path",token) == 0)
3390 break;
3391 if (LocaleCompare("defs",token) == 0)
3392 {
3393 defsDepth--;
3394 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3395 MagickTrue;
3396 break;
3397 }
3398 if (LocaleCompare("gradient",token) == 0)
3399 break;
3400 if (LocaleCompare("graphic-context",token) == 0)
3401 {
3402 if (n <= 0)
3403 {
3404 (void) ThrowMagickException(exception,GetMagickModule(),
3405 DrawError,"UnbalancedGraphicContextPushPop","`%s'",token);
3406 status=MagickFalse;
3407 n=0;
3408 break;
3409 }
3410 if ((graphic_context[n]->clip_mask != (char *) NULL) &&
3411 (graphic_context[n]->compliance != SVGCompliance))
3412 if (LocaleCompare(graphic_context[n]->clip_mask,
3413 graphic_context[n-1]->clip_mask) != 0)
3414 status=SetImageMask(image,WritePixelMask,(Image *) NULL,
3415 exception);
3416 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
3417 n--;
3418 break;
3419 }
3420 if (LocaleCompare("mask",token) == 0)
3421 break;
3422 if (LocaleCompare("pattern",token) == 0)
3423 break;
3424 if (LocaleCompare("symbol",token) == 0)
3425 {
3426 symbolDepth--;
3427 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3428 MagickTrue;
3429 break;
3430 }
3431 status=MagickFalse;
3432 break;
3433 }
3434 if (LocaleCompare("push",keyword) == 0)
3435 {
3436 if (GetNextToken(q,&q,extent,token) < 1)
3437 break;
3438 if (LocaleCompare("class",token) == 0)
3439 {
3440 /*
3441 Class context.
3442 */
3443 for (p=q; *q != '\0'; )
3444 {
3445 if (GetNextToken(q,&q,extent,token) < 1)
3446 break;
3447 if (LocaleCompare(token,"pop") != 0)
3448 continue;
3449 (void) GetNextToken(q,(const char **) NULL,extent,token);
3450 if (LocaleCompare(token,"class") != 0)
3451 continue;
3452 break;
3453 }
3454 (void) GetNextToken(q,&q,extent,token);
3455 break;
3456 }
3457 if (LocaleCompare("clip-path",token) == 0)
3458 {
3459 (void) GetNextToken(q,&q,extent,token);
3460 for (p=q; *q != '\0'; )
3461 {
3462 if (GetNextToken(q,&q,extent,token) < 1)
3463 break;
3464 if (LocaleCompare(token,"pop") != 0)
3465 continue;
3466 (void) GetNextToken(q,(const char **) NULL,extent,token);
3467 if (LocaleCompare(token,"clip-path") != 0)
3468 continue;
3469 break;
3470 }
3471 if ((q == (char *) NULL) || (p == (char *) NULL) || ((q-4) < p))
3472 {
3473 status=MagickFalse;
3474 break;
3475 }
3476 (void) GetNextToken(q,&q,extent,token);
3477 break;
3478 }
3479 if (LocaleCompare("defs",token) == 0)
3480 {
3481 defsDepth++;
3482 graphic_context[n]->render=defsDepth > 0 ? MagickFalse :
3483 MagickTrue;
3484 break;
3485 }
3486 if (LocaleCompare("gradient",token) == 0)
3487 {
3488 char
3489 key[2*MagickPathExtent],
3490 name[MagickPathExtent],
3491 type[MagickPathExtent];
3492
3493 SegmentInfo
3494 segment;
3495
3496 (void) GetNextToken(q,&q,extent,token);
3497 (void) CopyMagickString(name,token,MagickPathExtent);
3498 (void) GetNextToken(q,&q,extent,token);
3499 (void) CopyMagickString(type,token,MagickPathExtent);
3500 (void) GetNextToken(q,&q,extent,token);
3501 segment.x1=GetDrawValue(token,&next_token);
3502 if (token == next_token)
3503 ThrowPointExpectedException(token,exception);
3504 (void) GetNextToken(q,&q,extent,token);
3505 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3506 ThrowPointExpectedException(token,exception);
3507 if (*token == ',')
3508 (void) GetNextToken(q,&q,extent,token);
3509 segment.y1=GetDrawValue(token,&next_token);
3510 if (token == next_token)
3511 ThrowPointExpectedException(token,exception);
3512 (void) GetNextToken(q,&q,extent,token);
3513 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3514 ThrowPointExpectedException(token,exception);
3515 if (*token == ',')
3516 (void) GetNextToken(q,&q,extent,token);
3517 segment.x2=GetDrawValue(token,&next_token);
3518 if (token == next_token)
3519 ThrowPointExpectedException(token,exception);
3520 (void) GetNextToken(q,&q,extent,token);
3521 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3522 ThrowPointExpectedException(token,exception);
3523 if (*token == ',')
3524 (void) GetNextToken(q,&q,extent,token);
3525 segment.y2=GetDrawValue(token,&next_token);
3526 if (token == next_token)
3527 ThrowPointExpectedException(token,exception);
3528 if (LocaleCompare(type,"radial") == 0)
3529 {
3530 (void) GetNextToken(q,&q,extent,token);
3531 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3532 ThrowPointExpectedException(token,exception);
3533 if (*token == ',')
3534 (void) GetNextToken(q,&q,extent,token);
3535 }
3536 for (p=q; *q != '\0'; )
3537 {
3538 if (GetNextToken(q,&q,extent,token) < 1)
3539 break;
3540 if (LocaleCompare(token,"pop") != 0)
3541 continue;
3542 (void) GetNextToken(q,(const char **) NULL,extent,token);
3543 if (LocaleCompare(token,"gradient") != 0)
3544 continue;
3545 break;
3546 }
3547 if ((q == (char *) NULL) || (*q == '\0') ||
3548 (p == (char *) NULL) || ((q-4) < p) ||
3549 ((size_t) (q-p+4+1) > extent))
3550 {
3551 status=MagickFalse;
3552 break;
3553 }
3554 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3555 bounds.x1=graphic_context[n]->affine.sx*segment.x1+
3556 graphic_context[n]->affine.ry*segment.y1+
3557 graphic_context[n]->affine.tx;
3558 bounds.y1=graphic_context[n]->affine.rx*segment.x1+
3559 graphic_context[n]->affine.sy*segment.y1+
3560 graphic_context[n]->affine.ty;
3561 bounds.x2=graphic_context[n]->affine.sx*segment.x2+
3562 graphic_context[n]->affine.ry*segment.y2+
3563 graphic_context[n]->affine.tx;
3564 bounds.y2=graphic_context[n]->affine.rx*segment.x2+
3565 graphic_context[n]->affine.sy*segment.y2+
3566 graphic_context[n]->affine.ty;
3567 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3568 (void) SetImageArtifact(image,key,token);
3569 (void) FormatLocaleString(key,MagickPathExtent,"%s-type",name);
3570 (void) SetImageArtifact(image,key,type);
3571 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3572 name);
3573 (void) FormatLocaleString(geometry,MagickPathExtent,
3574 "%gx%g%+.15g%+.15g",
3575 MagickMax(fabs(bounds.x2-bounds.x1+1.0),1.0),
3576 MagickMax(fabs(bounds.y2-bounds.y1+1.0),1.0),
3577 bounds.x1,bounds.y1);
3578 (void) SetImageArtifact(image,key,geometry);
3579 (void) GetNextToken(q,&q,extent,token);
3580 break;
3581 }
3582 if (LocaleCompare("graphic-context",token) == 0)
3583 {
3584 n++;
3585 graphic_context=(DrawInfo **) ResizeQuantumMemory(
3586 graphic_context,(size_t) (n+1),sizeof(*graphic_context));
3587 if (graphic_context == (DrawInfo **) NULL)
3588 {
3589 (void) ThrowMagickException(exception,GetMagickModule(),
3590 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3591 image->filename);
3592 status=MagickFalse;
3593 break;
3594 }
3595 graphic_context[n]=CloneDrawInfo((ImageInfo *) NULL,
3596 graphic_context[n-1]);
3597 if (*q == '"')
3598 {
3599 (void) GetNextToken(q,&q,extent,token);
3600 (void) CloneString(&graphic_context[n]->id,token);
3601 }
3602 if (n >= MaxVectorRecursionDepth)
3603 {
3604 (void) ThrowMagickException(exception,GetMagickModule(),
3605 DrawError,"VectorGraphicsNestedTooDeeply","`%s'",
3606 image->filename);
3607 status=MagickFalse;
3608 }
3609 break;
3610 }
3611 if (LocaleCompare("mask",token) == 0)
3612 {
3613 (void) GetNextToken(q,&q,extent,token);
3614 break;
3615 }
3616 if (LocaleCompare("pattern",token) == 0)
3617 {
3618 char
3619 key[2*MagickPathExtent],
3620 name[MagickPathExtent];
3621
3622 RectangleInfo
3623 region;
3624
3625 (void) GetNextToken(q,&q,extent,token);
3626 (void) CopyMagickString(name,token,MagickPathExtent);
3627 (void) GetNextToken(q,&q,extent,token);
3628 region.x=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3629 &next_token)-0.5));
3630 if (token == next_token)
3631 ThrowPointExpectedException(token,exception);
3632 (void) GetNextToken(q,&q,extent,token);
3633 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3634 ThrowPointExpectedException(token,exception);
3635 if (*token == ',')
3636 (void) GetNextToken(q,&q,extent,token);
3637 region.y=CastDoubleToSsizeT(ceil(GetDrawValue(token,
3638 &next_token)-0.5));
3639 if (token == next_token)
3640 ThrowPointExpectedException(token,exception);
3641 (void) GetNextToken(q,&q,extent,token);
3642 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3643 ThrowPointExpectedException(token,exception);
3644 if (*token == ',')
3645 (void) GetNextToken(q,&q,extent,token);
3646 region.width=CastDoubleToSizeT(floor(GetDrawValue(token,
3647 &next_token)+0.5));
3648 if (token == next_token)
3649 ThrowPointExpectedException(token,exception);
3650 (void) GetNextToken(q,&q,extent,token);
3651 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3652 ThrowPointExpectedException(token,exception);
3653 if (*token == ',')
3654 (void) GetNextToken(q,&q,extent,token);
3655 region.height=CastDoubleToSizeT(GetDrawValue(token,
3656 &next_token)+0.5);
3657 if (token == next_token)
3658 ThrowPointExpectedException(token,exception);
3659 for (p=q; *q != '\0'; )
3660 {
3661 if (GetNextToken(q,&q,extent,token) < 1)
3662 break;
3663 if (LocaleCompare(token,"pop") != 0)
3664 continue;
3665 (void) GetNextToken(q,(const char **) NULL,extent,token);
3666 if (LocaleCompare(token,"pattern") != 0)
3667 continue;
3668 break;
3669 }
3670 if ((q == (char *) NULL) || (p == (char *) NULL) ||
3671 ((q-4) < p) || ((size_t) (q-p+4+1) > extent))
3672 {
3673 status=MagickFalse;
3674 break;
3675 }
3676 (void) CopyMagickString(token,p,(size_t) (q-p-4+1));
3677 (void) FormatLocaleString(key,MagickPathExtent,"%s",name);
3678 (void) SetImageArtifact(image,key,token);
3679 (void) FormatLocaleString(key,MagickPathExtent,"%s-geometry",
3680 name);
3681 (void) FormatLocaleString(geometry,MagickPathExtent,
3682 "%.17gx%.17g%+.20g%+.20g",(double) region.width,(double)
3683 region.height,(double) region.x,(double) region.y);
3684 (void) SetImageArtifact(image,key,geometry);
3685 (void) GetNextToken(q,&q,extent,token);
3686 break;
3687 }
3688 if (LocaleCompare("symbol",token) == 0)
3689 {
3690 symbolDepth++;
3691 graphic_context[n]->render=symbolDepth > 0 ? MagickFalse :
3692 MagickTrue;
3693 break;
3694 }
3695 status=MagickFalse;
3696 break;
3697 }
3698 status=MagickFalse;
3699 break;
3700 }
3701 case 'r':
3702 case 'R':
3703 {
3704 if (LocaleCompare("rectangle",keyword) == 0)
3705 {
3706 primitive_type=RectanglePrimitive;
3707 break;
3708 }
3709 if (LocaleCompare("rotate",keyword) == 0)
3710 {
3711 (void) GetNextToken(q,&q,extent,token);
3712 angle=GetDrawValue(token,&next_token);
3713 if (token == next_token)
3714 ThrowPointExpectedException(token,exception);
3715 affine.sx=cos(DegreesToRadians(fmod((double) angle,360.0)));
3716 affine.rx=sin(DegreesToRadians(fmod((double) angle,360.0)));
3717 affine.ry=(-sin(DegreesToRadians(fmod((double) angle,360.0))));
3718 affine.sy=cos(DegreesToRadians(fmod((double) angle,360.0)));
3719 break;
3720 }
3721 if (LocaleCompare("roundRectangle",keyword) == 0)
3722 {
3723 primitive_type=RoundRectanglePrimitive;
3724 break;
3725 }
3726 status=MagickFalse;
3727 break;
3728 }
3729 case 's':
3730 case 'S':
3731 {
3732 if (LocaleCompare("scale",keyword) == 0)
3733 {
3734 (void) GetNextToken(q,&q,extent,token);
3735 affine.sx=GetDrawValue(token,&next_token);
3736 if (token == next_token)
3737 ThrowPointExpectedException(token,exception);
3738 (void) GetNextToken(q,&q,extent,token);
3739 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3740 ThrowPointExpectedException(token,exception);
3741 if (*token == ',')
3742 (void) GetNextToken(q,&q,extent,token);
3743 affine.sy=GetDrawValue(token,&next_token);
3744 if (token == next_token)
3745 ThrowPointExpectedException(token,exception);
3746 break;
3747 }
3748 if (LocaleCompare("skewX",keyword) == 0)
3749 {
3750 (void) GetNextToken(q,&q,extent,token);
3751 angle=GetDrawValue(token,&next_token);
3752 if (token == next_token)
3753 ThrowPointExpectedException(token,exception);
3754 affine.ry=sin(DegreesToRadians(angle));
3755 break;
3756 }
3757 if (LocaleCompare("skewY",keyword) == 0)
3758 {
3759 (void) GetNextToken(q,&q,extent,token);
3760 angle=GetDrawValue(token,&next_token);
3761 if (token == next_token)
3762 ThrowPointExpectedException(token,exception);
3763 affine.rx=(-tan(DegreesToRadians(angle)/2.0));
3764 break;
3765 }
3766 if (LocaleCompare("stop-color",keyword) == 0)
3767 {
3768 PixelInfo
3769 stop_color;
3770
3771 number_stops++;
3772 if (number_stops == 1)
3773 stops=(StopInfo *) AcquireQuantumMemory(2,sizeof(*stops));
3774 else
3775 if (number_stops > 2)
3776 stops=(StopInfo *) ResizeQuantumMemory(stops,number_stops,
3777 sizeof(*stops));
3778 if (stops == (StopInfo *) NULL)
3779 {
3780 (void) ThrowMagickException(exception,GetMagickModule(),
3781 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3782 image->filename);
3783 status=MagickFalse;
3784 break;
3785 }
3786 (void) GetNextToken(q,&q,extent,token);
3787 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3788 &stop_color,exception);
3789 stops[number_stops-1].color=stop_color;
3790 (void) GetNextToken(q,&q,extent,token);
3791 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3792 stops[number_stops-1].offset=factor*GetDrawValue(token,
3793 &next_token);
3794 if (token == next_token)
3795 ThrowPointExpectedException(token,exception);
3796 break;
3797 }
3798 if (LocaleCompare("stroke",keyword) == 0)
3799 {
3800 const char
3801 *mvg_class;
3802
3803 (void) GetNextToken(q,&q,extent,token);
3804 if (graphic_context[n]->clip_path != MagickFalse)
3805 break;
3806 mvg_class=(const char *) GetValueFromSplayTree(macros,token);
3807 if (mvg_class != (const char *) NULL)
3808 {
3809 (void) DrawPatternPath(image,draw_info,mvg_class,
3810 &graphic_context[n]->stroke_pattern,exception);
3811 break;
3812 }
3813 (void) FormatLocaleString(pattern,MagickPathExtent,"%s",token);
3814 if (GetImageArtifact(image,pattern) != (const char *) NULL)
3815 {
3816 (void) DrawPatternPath(image,draw_info,token,
3817 &graphic_context[n]->stroke_pattern,exception);
3818 break;
3819 }
3820 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
3821 &graphic_context[n]->stroke,exception);
3822 if ((graphic_context[n]->stroke_alpha != (double) OpaqueAlpha) &&
3823 ((graphic_context[n]->compliance != SVGCompliance) ||
3824 (graphic_context[n]->stroke.alpha != (double) TransparentAlpha)))
3825 graphic_context[n]->stroke.alpha=
3826 graphic_context[n]->stroke_alpha;
3827 break;
3828 }
3829 if (LocaleCompare("stroke-antialias",keyword) == 0)
3830 {
3831 (void) GetNextToken(q,&q,extent,token);
3832 graphic_context[n]->stroke_antialias=StringToLong(token) != 0 ?
3833 MagickTrue : MagickFalse;
3834 break;
3835 }
3836 if (LocaleCompare("stroke-dasharray",keyword) == 0)
3837 {
3838 if (graphic_context[n]->dash_pattern != (double *) NULL)
3839 graphic_context[n]->dash_pattern=(double *)
3840 RelinquishMagickMemory(graphic_context[n]->dash_pattern);
3841 if (IsValidPoint(q) != MagickFalse)
3842 {
3843 const char
3844 *r;
3845
3846 r=q;
3847 (void) GetNextToken(r,&r,extent,token);
3848 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3849 ThrowPointExpectedException(token,exception);
3850 if (*token == ',')
3851 (void) GetNextToken(r,&r,extent,token);
3852 for (x=0; IsValidPoint(token) != MagickFalse; x++)
3853 {
3854 (void) GetNextToken(r,&r,extent,token);
3855 if (*token == ',')
3856 (void) GetNextToken(r,&r,extent,token);
3857 }
3858 graphic_context[n]->dash_pattern=(double *)
3859 AcquireQuantumMemory((size_t) (2*x+2),
3860 sizeof(*graphic_context[n]->dash_pattern));
3861 if (graphic_context[n]->dash_pattern == (double *) NULL)
3862 {
3863 (void) ThrowMagickException(exception,GetMagickModule(),
3864 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3865 image->filename);
3866 status=MagickFalse;
3867 break;
3868 }
3869 (void) memset(graphic_context[n]->dash_pattern,0,(size_t)
3870 (2*x+2)*sizeof(*graphic_context[n]->dash_pattern));
3871 for (j=0; j < x; j++)
3872 {
3873 (void) GetNextToken(q,&q,extent,token);
3874 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
3875 ThrowPointExpectedException(token,exception);
3876 if (*token == ',')
3877 (void) GetNextToken(q,&q,extent,token);
3878 graphic_context[n]->dash_pattern[j]=GetDrawValue(token,
3879 &next_token);
3880 if (token == next_token)
3881 ThrowPointExpectedException(token,exception);
3882 if (graphic_context[n]->dash_pattern[j] <= 0.0)
3883 status=MagickFalse;
3884 }
3885 if (((x & 0x01) != 0) && (j == x))
3886 for ( ; j < (2*x); j++)
3887 graphic_context[n]->dash_pattern[j]=
3888 graphic_context[n]->dash_pattern[j-x];
3889 graphic_context[n]->dash_pattern[j]=0.0;
3890 break;
3891 }
3892 (void) GetNextToken(q,&q,extent,token);
3893 break;
3894 }
3895 if (LocaleCompare("stroke-dashoffset",keyword) == 0)
3896 {
3897 (void) GetNextToken(q,&q,extent,token);
3898 graphic_context[n]->dash_offset=GetDrawValue(token,&next_token);
3899 if (token == next_token)
3900 ThrowPointExpectedException(token,exception);
3901 break;
3902 }
3903 if (LocaleCompare("stroke-linecap",keyword) == 0)
3904 {
3905 ssize_t
3906 linecap;
3907
3908 (void) GetNextToken(q,&q,extent,token);
3909 linecap=ParseCommandOption(MagickLineCapOptions,MagickFalse,token);
3910 if (linecap == -1)
3911 {
3912 status=MagickFalse;
3913 break;
3914 }
3915 graphic_context[n]->linecap=(LineCap) linecap;
3916 break;
3917 }
3918 if (LocaleCompare("stroke-linejoin",keyword) == 0)
3919 {
3920 ssize_t
3921 linejoin;
3922
3923 (void) GetNextToken(q,&q,extent,token);
3924 linejoin=ParseCommandOption(MagickLineJoinOptions,MagickFalse,
3925 token);
3926 if (linejoin == -1)
3927 {
3928 status=MagickFalse;
3929 break;
3930 }
3931 graphic_context[n]->linejoin=(LineJoin) linejoin;
3932 break;
3933 }
3934 if (LocaleCompare("stroke-miterlimit",keyword) == 0)
3935 {
3936 (void) GetNextToken(q,&q,extent,token);
3937 graphic_context[n]->miterlimit=StringToUnsignedLong(token);
3938 break;
3939 }
3940 if (LocaleCompare("stroke-opacity",keyword) == 0)
3941 {
3942 double
3943 opacity;
3944
3945 (void) GetNextToken(q,&q,extent,token);
3946 if (graphic_context[n]->clip_path != MagickFalse)
3947 break;
3948 factor=strchr(token,'%') != (char *) NULL ? 0.01 : 1.0;
3949 opacity=MagickMin(MagickMax(factor*GetDrawValue(token,&next_token),
3950 0.0),1.0);
3951 if (token == next_token)
3952 ThrowPointExpectedException(token,exception);
3953 if (graphic_context[n]->compliance == SVGCompliance)
3954 graphic_context[n]->stroke_alpha*=opacity;
3955 else
3956 graphic_context[n]->stroke_alpha=(double) QuantumRange*opacity;
3957 if (graphic_context[n]->stroke.alpha != (double) TransparentAlpha)
3958 graphic_context[n]->stroke.alpha=graphic_context[n]->stroke_alpha;
3959 else
3960 if (graphic_context[n]->compliance != SVGCompliance)
3961 graphic_context[n]->stroke.alpha=(MagickRealType)
3962 ClampToQuantum((double) QuantumRange*opacity);
3963 graphic_context[n]->stroke.alpha_trait=BlendPixelTrait;
3964 break;
3965 }
3966 if (LocaleCompare("stroke-width",keyword) == 0)
3967 {
3968 (void) GetNextToken(q,&q,extent,token);
3969 if (graphic_context[n]->clip_path != MagickFalse)
3970 break;
3971 graphic_context[n]->stroke_width=GetDrawValue(token,&next_token);
3972 if ((token == next_token) ||
3973 (graphic_context[n]->stroke_width < 0.0))
3974 ThrowPointExpectedException(token,exception);
3975 break;
3976 }
3977 status=MagickFalse;
3978 break;
3979 }
3980 case 't':
3981 case 'T':
3982 {
3983 if (LocaleCompare("text",keyword) == 0)
3984 {
3985 primitive_type=TextPrimitive;
3986 cursor=0.0;
3987 break;
3988 }
3989 if (LocaleCompare("text-align",keyword) == 0)
3990 {
3991 ssize_t
3992 align;
3993
3994 (void) GetNextToken(q,&q,extent,token);
3995 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
3996 if (align == -1)
3997 {
3998 status=MagickFalse;
3999 break;
4000 }
4001 graphic_context[n]->align=(AlignType) align;
4002 break;
4003 }
4004 if (LocaleCompare("text-anchor",keyword) == 0)
4005 {
4006 ssize_t
4007 align;
4008
4009 (void) GetNextToken(q,&q,extent,token);
4010 align=ParseCommandOption(MagickAlignOptions,MagickFalse,token);
4011 if (align == -1)
4012 {
4013 status=MagickFalse;
4014 break;
4015 }
4016 graphic_context[n]->align=(AlignType) align;
4017 break;
4018 }
4019 if (LocaleCompare("text-antialias",keyword) == 0)
4020 {
4021 (void) GetNextToken(q,&q,extent,token);
4022 graphic_context[n]->text_antialias=StringToLong(token) != 0 ?
4023 MagickTrue : MagickFalse;
4024 break;
4025 }
4026 if (LocaleCompare("text-undercolor",keyword) == 0)
4027 {
4028 (void) GetNextToken(q,&q,extent,token);
4029 status&=(MagickStatusType) QueryColorCompliance(token,AllCompliance,
4030 &graphic_context[n]->undercolor,exception);
4031 break;
4032 }
4033 if (LocaleCompare("translate",keyword) == 0)
4034 {
4035 (void) GetNextToken(q,&q,extent,token);
4036 affine.tx=GetDrawValue(token,&next_token);
4037 if (token == next_token)
4038 ThrowPointExpectedException(token,exception);
4039 (void) GetNextToken(q,&q,extent,token);
4040 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4041 ThrowPointExpectedException(token,exception);
4042 if (*token == ',')
4043 (void) GetNextToken(q,&q,extent,token);
4044 affine.ty=GetDrawValue(token,&next_token);
4045 if (token == next_token)
4046 ThrowPointExpectedException(token,exception);
4047 cursor=0.0;
4048 break;
4049 }
4050 status=MagickFalse;
4051 break;
4052 }
4053 case 'u':
4054 case 'U':
4055 {
4056 if (LocaleCompare("use",keyword) == 0)
4057 {
4058 const char
4059 *use;
4060
4061 /*
4062 Get a macro from the MVG document, and "use" it here.
4063 */
4064 (void) GetNextToken(q,&q,extent,token);
4065 use=(const char *) GetValueFromSplayTree(macros,token);
4066 if (use != (const char *) NULL)
4067 {
4068 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4069 if (draw_info->macro_expansion != (size_t *) NULL)
4070 {
4071 if (*draw_info->macro_expansion >= MacroExpansionLimit)
4072 {
4073 (void) ThrowMagickException(exception,GetMagickModule(),
4074 DrawError,"NonconformingDrawingPrimitiveDefinition",
4075 "`%s'",token);
4076 clone_info=DestroyDrawInfo(clone_info);
4077 return(MagickFalse);
4078 }
4079 (*draw_info->macro_expansion)++;
4080 }
4081 (void) CloneString(&clone_info->primitive,use);
4082 status=RenderMVGContent(image,clone_info,depth+1,exception);
4083 clone_info=DestroyDrawInfo(clone_info);
4084 }
4085 break;
4086 }
4087 status=MagickFalse;
4088 break;
4089 }
4090 case 'v':
4091 case 'V':
4092 {
4093 if (LocaleCompare("viewbox",keyword) == 0)
4094 {
4095 (void) GetNextToken(q,&q,extent,token);
4096 graphic_context[n]->viewbox.x=CastDoubleToSsizeT(ceil(
4097 GetDrawValue(token,&next_token)-0.5));
4098 if (token == next_token)
4099 ThrowPointExpectedException(token,exception);
4100 (void) GetNextToken(q,&q,extent,token);
4101 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4102 ThrowPointExpectedException(token,exception);
4103 if (*token == ',')
4104 (void) GetNextToken(q,&q,extent,token);
4105 graphic_context[n]->viewbox.y=CastDoubleToSsizeT(
4106 ceil(GetDrawValue(token,&next_token)-0.5));
4107 if (token == next_token)
4108 ThrowPointExpectedException(token,exception);
4109 (void) GetNextToken(q,&q,extent,token);
4110 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4111 ThrowPointExpectedException(token,exception);
4112 if (*token == ',')
4113 (void) GetNextToken(q,&q,extent,token);
4114 graphic_context[n]->viewbox.width=CastDoubleToSizeT(floor(
4115 GetDrawValue(token,&next_token)+0.5));
4116 if (token == next_token)
4117 ThrowPointExpectedException(token,exception);
4118 (void) GetNextToken(q,&q,extent,token);
4119 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4120 ThrowPointExpectedException(token,exception);
4121 if (*token == ',')
4122 (void) GetNextToken(q,&q,extent,token);
4123 graphic_context[n]->viewbox.height=CastDoubleToSizeT(floor(
4124 GetDrawValue(token,&next_token)+0.5));
4125 if (token == next_token)
4126 ThrowPointExpectedException(token,exception);
4127 break;
4128 }
4129 status=MagickFalse;
4130 break;
4131 }
4132 case 'w':
4133 case 'W':
4134 {
4135 if (LocaleCompare("word-spacing",keyword) == 0)
4136 {
4137 (void) GetNextToken(q,&q,extent,token);
4138 graphic_context[n]->interword_spacing=GetDrawValue(token,
4139 &next_token);
4140 if (token == next_token)
4141 ThrowPointExpectedException(token,exception);
4142 break;
4143 }
4144 status=MagickFalse;
4145 break;
4146 }
4147 default:
4148 {
4149 status=MagickFalse;
4150 break;
4151 }
4152 }
4153 if (status == MagickFalse)
4154 break;
4155 if ((fabs(affine.sx-1.0) >= MagickEpsilon) ||
4156 (fabs(affine.rx) >= MagickEpsilon) || (fabs(affine.ry) >= MagickEpsilon) ||
4157 (fabs(affine.sy-1.0) >= MagickEpsilon) ||
4158 (fabs(affine.tx) >= MagickEpsilon) || (fabs(affine.ty) >= MagickEpsilon))
4159 {
4160 graphic_context[n]->affine.sx=current.sx*affine.sx+current.ry*affine.rx;
4161 graphic_context[n]->affine.rx=current.rx*affine.sx+current.sy*affine.rx;
4162 graphic_context[n]->affine.ry=current.sx*affine.ry+current.ry*affine.sy;
4163 graphic_context[n]->affine.sy=current.rx*affine.ry+current.sy*affine.sy;
4164 graphic_context[n]->affine.tx=current.sx*affine.tx+current.ry*affine.ty+
4165 current.tx;
4166 graphic_context[n]->affine.ty=current.rx*affine.tx+current.sy*affine.ty+
4167 current.ty;
4168 }
4169 if (primitive_type == UndefinedPrimitive)
4170 {
4171 if (*q == '\0')
4172 {
4173 if (number_stops > 1)
4174 {
4175 GradientType
4176 type;
4177
4178 type=LinearGradient;
4179 if (draw_info->gradient.type == RadialGradient)
4180 type=RadialGradient;
4181 (void) GradientImage(image,type,PadSpread,stops,number_stops,
4182 exception);
4183 }
4184 if (number_stops > 0)
4185 stops=(StopInfo *) RelinquishMagickMemory(stops);
4186 }
4187 if ((draw_info->debug != MagickFalse) && (q > p))
4188 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int)
4189 (q-p-1),p);
4190 continue;
4191 }
4192 /*
4193 Parse the primitive attributes.
4194 */
4195 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4196 if (primitive_info[i].text != (char *) NULL)
4197 primitive_info[i].text=DestroyString(primitive_info[i].text);
4198 i=0;
4199 mvg_info.offset=i;
4200 j=0;
4201 primitive_info[0].primitive=primitive_type;
4202 primitive_info[0].point.x=0.0;
4203 primitive_info[0].point.y=0.0;
4204 primitive_info[0].coordinates=0;
4205 primitive_info[0].method=FloodfillMethod;
4206 primitive_info[0].closed_subpath=MagickFalse;
4207 for (x=0; *q != '\0'; x++)
4208 {
4209 /*
4210 Define points.
4211 */
4212 if (IsValidPoint(q) == MagickFalse)
4213 break;
4214 (void) GetNextToken(q,&q,extent,token);
4215 point.x=GetDrawValue(token,&next_token);
4216 if (token == next_token)
4217 ThrowPointExpectedException(token,exception);
4218 (void) GetNextToken(q,&q,extent,token);
4219 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
4220 ThrowPointExpectedException(token,exception);
4221 if (*token == ',')
4222 (void) GetNextToken(q,&q,extent,token);
4223 point.y=GetDrawValue(token,&next_token);
4224 if (token == next_token)
4225 ThrowPointExpectedException(token,exception);
4226 (void) GetNextToken(q,(const char **) NULL,extent,token);
4227 if (*token == ',')
4228 (void) GetNextToken(q,&q,extent,token);
4229 primitive_info[i].primitive=primitive_type;
4230 primitive_info[i].point=point;
4231 primitive_info[i].coordinates=0;
4232 primitive_info[i].method=FloodfillMethod;
4233 primitive_info[i].closed_subpath=MagickFalse;
4234 i++;
4235 mvg_info.offset=i;
4236 if (i < (ssize_t) number_points)
4237 continue;
4238 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(double)
4239 number_points);
4240 primitive_info=(*mvg_info.primitive_info);
4241 if (status == MagickFalse)
4242 break;
4243 }
4244 if (status == MagickFalse)
4245 break;
4246 if (primitive_info[j].text != (char *) NULL)
4247 primitive_info[j].text=DestroyString(primitive_info[j].text);
4248 primitive_info[j].primitive=primitive_type;
4249 primitive_info[j].coordinates=(size_t) x;
4250 primitive_info[j].method=FloodfillMethod;
4251 primitive_info[j].closed_subpath=MagickFalse;
4252 /*
4253 Circumscribe primitive within a circle.
4254 */
4255 bounds.x1=primitive_info[j].point.x;
4256 bounds.y1=primitive_info[j].point.y;
4257 bounds.x2=primitive_info[j].point.x;
4258 bounds.y2=primitive_info[j].point.y;
4259 for (k=1; k < (ssize_t) primitive_info[j].coordinates; k++)
4260 {
4261 point=primitive_info[j+k].point;
4262 if (point.x < bounds.x1)
4263 bounds.x1=point.x;
4264 if (point.y < bounds.y1)
4265 bounds.y1=point.y;
4266 if (point.x > bounds.x2)
4267 bounds.x2=point.x;
4268 if (point.y > bounds.y2)
4269 bounds.y2=point.y;
4270 }
4271 /*
4272 Speculate how many points our primitive might consume.
4273 */
4274 coordinates=(double) primitive_info[j].coordinates;
4275 switch (primitive_type)
4276 {
4277 case RectanglePrimitive:
4278 {
4279 coordinates*=5.0;
4280 break;
4281 }
4282 case RoundRectanglePrimitive:
4283 {
4284 double
4285 alpha,
4286 beta,
4287 radius;
4288
4289 alpha=bounds.x2-bounds.x1;
4290 beta=bounds.y2-bounds.y1;
4291 radius=hypot(alpha,beta);
4292 coordinates*=5.0;
4293 coordinates+=2.0*((size_t) ceil((double) MagickPI*radius))+6.0*
4294 BezierQuantum+360.0;
4295 break;
4296 }
4297 case BezierPrimitive:
4298 {
4299 coordinates=(BezierQuantum*(double) primitive_info[j].coordinates);
4300 break;
4301 }
4302 case PathPrimitive:
4303 {
4304 char
4305 *s,
4306 *t;
4307
4308 (void) GetNextToken(q,&q,extent,token);
4309 coordinates=1.0;
4310 t=token;
4311 for (s=token; *s != '\0'; s=t)
4312 {
4313 double
4314 value;
4315
4316 value=GetDrawValue(s,&t);
4317 (void) value;
4318 if (s == t)
4319 {
4320 t++;
4321 continue;
4322 }
4323 coordinates++;
4324 }
4325 for (s=token; *s != '\0'; s++)
4326 if (strspn(s,"AaCcQqSsTt") != 0)
4327 coordinates+=(20.0*BezierQuantum)+360.0;
4328 break;
4329 }
4330 default:
4331 break;
4332 }
4333 if (status == MagickFalse)
4334 break;
4335 if (((size_t) (i+coordinates)) >= number_points)
4336 {
4337 /*
4338 Resize based on speculative points required by primitive.
4339 */
4340 number_points+=(size_t) coordinates+1;
4341 if (number_points < (size_t) coordinates)
4342 {
4343 (void) ThrowMagickException(exception,GetMagickModule(),
4344 ResourceLimitError,"MemoryAllocationFailed","`%s'",
4345 image->filename);
4346 status=MagickFalse;
4347 break;
4348 }
4349 mvg_info.offset=i;
4350 status&=CheckPrimitiveExtent(&mvg_info,(double) number_points+
4351 (double) coordinates+1.0);
4352 primitive_info=(*mvg_info.primitive_info);
4353 if (status == MagickFalse)
4354 break;
4355 }
4356 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,
4357 PrimitiveExtentPad);
4358 primitive_info=(*mvg_info.primitive_info);
4359 if (status == MagickFalse)
4360 break;
4361 mvg_info.offset=j;
4362 switch (primitive_type)
4363 {
4364 case PointPrimitive:
4365 default:
4366 {
4367 if (primitive_info[j].coordinates != 1)
4368 {
4369 status=MagickFalse;
4370 break;
4371 }
4372 status&=(MagickStatusType) TracePoint(primitive_info+j,
4373 primitive_info[j].point);
4374 primitive_info=(*mvg_info.primitive_info);
4375 if (status == MagickFalse)
4376 break;
4377 i=j+(ssize_t) primitive_info[j].coordinates;
4378 break;
4379 }
4380 case LinePrimitive:
4381 {
4382 if (primitive_info[j].coordinates != 2)
4383 {
4384 status=MagickFalse;
4385 break;
4386 }
4387 status&=(MagickStatusType) TraceLine(primitive_info+j,
4388 primitive_info[j].point,primitive_info[j+1].point);
4389 primitive_info=(*mvg_info.primitive_info);
4390 if (status == MagickFalse)
4391 break;
4392 i=j+(ssize_t) primitive_info[j].coordinates;
4393 break;
4394 }
4395 case RectanglePrimitive:
4396 {
4397 if (primitive_info[j].coordinates != 2)
4398 {
4399 status=MagickFalse;
4400 break;
4401 }
4402 status&=(MagickStatusType) TraceRectangle(primitive_info+j,
4403 primitive_info[j].point,primitive_info[j+1].point);
4404 primitive_info=(*mvg_info.primitive_info);
4405 if (status == MagickFalse)
4406 break;
4407 i=j+(ssize_t) primitive_info[j].coordinates;
4408 break;
4409 }
4410 case RoundRectanglePrimitive:
4411 {
4412 if (primitive_info[j].coordinates != 3)
4413 {
4414 status=MagickFalse;
4415 break;
4416 }
4417 if ((primitive_info[j+2].point.x < 0.0) ||
4418 (primitive_info[j+2].point.y < 0.0))
4419 {
4420 status=MagickFalse;
4421 break;
4422 }
4423 if ((primitive_info[j+1].point.x-primitive_info[j].point.x) < 0.0)
4424 {
4425 status=MagickFalse;
4426 break;
4427 }
4428 if ((primitive_info[j+1].point.y-primitive_info[j].point.y) < 0.0)
4429 {
4430 status=MagickFalse;
4431 break;
4432 }
4433 status&=(MagickStatusType) TraceRoundRectangle(&mvg_info,
4434 primitive_info[j].point,primitive_info[j+1].point,
4435 primitive_info[j+2].point);
4436 primitive_info=(*mvg_info.primitive_info);
4437 if (status == MagickFalse)
4438 break;
4439 i=j+(ssize_t) primitive_info[j].coordinates;
4440 break;
4441 }
4442 case ArcPrimitive:
4443 {
4444 if (primitive_info[j].coordinates != 3)
4445 {
4446 status=MagickFalse;
4447 break;
4448 }
4449 status&=(MagickStatusType) TraceArc(&mvg_info,primitive_info[j].point,
4450 primitive_info[j+1].point,primitive_info[j+2].point);
4451 primitive_info=(*mvg_info.primitive_info);
4452 if (status == MagickFalse)
4453 break;
4454 i=j+(ssize_t) primitive_info[j].coordinates;
4455 break;
4456 }
4457 case EllipsePrimitive:
4458 {
4459 if (primitive_info[j].coordinates != 3)
4460 {
4461 status=MagickFalse;
4462 break;
4463 }
4464 if ((primitive_info[j+1].point.x < 0.0) ||
4465 (primitive_info[j+1].point.y < 0.0))
4466 {
4467 status=MagickFalse;
4468 break;
4469 }
4470 status&=(MagickStatusType) TraceEllipse(&mvg_info,
4471 primitive_info[j].point,primitive_info[j+1].point,
4472 primitive_info[j+2].point);
4473 primitive_info=(*mvg_info.primitive_info);
4474 if (status == MagickFalse)
4475 break;
4476 i=j+(ssize_t) primitive_info[j].coordinates;
4477 break;
4478 }
4479 case CirclePrimitive:
4480 {
4481 if (primitive_info[j].coordinates != 2)
4482 {
4483 status=MagickFalse;
4484 break;
4485 }
4486 status&=(MagickStatusType) TraceCircle(&mvg_info,
4487 primitive_info[j].point,primitive_info[j+1].point);
4488 primitive_info=(*mvg_info.primitive_info);
4489 if (status == MagickFalse)
4490 break;
4491 i=j+(ssize_t) primitive_info[j].coordinates;
4492 break;
4493 }
4494 case PolylinePrimitive:
4495 {
4496 if (primitive_info[j].coordinates < 1)
4497 {
4498 status=MagickFalse;
4499 break;
4500 }
4501 break;
4502 }
4503 case PolygonPrimitive:
4504 {
4505 if (primitive_info[j].coordinates < 3)
4506 {
4507 status=MagickFalse;
4508 break;
4509 }
4510 primitive_info[i]=primitive_info[j];
4511 primitive_info[i].coordinates=0;
4512 primitive_info[j].coordinates++;
4513 primitive_info[j].closed_subpath=MagickTrue;
4514 i++;
4515 break;
4516 }
4517 case BezierPrimitive:
4518 {
4519 if (primitive_info[j].coordinates < 3)
4520 {
4521 status=MagickFalse;
4522 break;
4523 }
4524 status&=(MagickStatusType) TraceBezier(&mvg_info,
4525 primitive_info[j].coordinates);
4526 primitive_info=(*mvg_info.primitive_info);
4527 if (status == MagickFalse)
4528 break;
4529 i=j+(ssize_t) primitive_info[j].coordinates;
4530 break;
4531 }
4532 case PathPrimitive:
4533 {
4534 coordinates=(double) TracePath(&mvg_info,token,exception);
4535 primitive_info=(*mvg_info.primitive_info);
4536 if (status == MagickFalse)
4537 break;
4538 if (coordinates < 0.0)
4539 {
4540 status=MagickFalse;
4541 break;
4542 }
4543 i=(ssize_t) (j+coordinates);
4544 break;
4545 }
4546 case AlphaPrimitive:
4547 case ColorPrimitive:
4548 {
4549 ssize_t
4550 method;
4551
4552 if (primitive_info[j].coordinates != 1)
4553 {
4554 status=MagickFalse;
4555 break;
4556 }
4557 (void) GetNextToken(q,&q,extent,token);
4558 method=ParseCommandOption(MagickMethodOptions,MagickFalse,token);
4559 if (method == -1)
4560 {
4561 status=MagickFalse;
4562 break;
4563 }
4564 primitive_info[j].method=(PaintMethod) method;
4565 break;
4566 }
4567 case TextPrimitive:
4568 {
4569 if (primitive_info[j].coordinates != 1)
4570 {
4571 status=MagickFalse;
4572 break;
4573 }
4574 if (*token != ',')
4575 (void) GetNextToken(q,&q,extent,token);
4576 (void) CloneString(&primitive_info[j].text,token);
4577 /*
4578 Compute text cursor offset.
4579 */
4580 clone_info=CloneDrawInfo((ImageInfo *) NULL,graphic_context[n]);
4581 if ((fabs(mvg_info.point.x-primitive_info->point.x) < MagickEpsilon) &&
4582 (fabs(mvg_info.point.y-primitive_info->point.y) < MagickEpsilon))
4583 {
4584 mvg_info.point=primitive_info->point;
4585 primitive_info->point.x+=cursor;
4586 }
4587 else
4588 {
4589 mvg_info.point=primitive_info->point;
4590 cursor=0.0;
4591 }
4592 clone_info->render=MagickFalse;
4593 clone_info->text=AcquireString(token);
4594 status&=(MagickStatusType) GetTypeMetrics(image,clone_info,
4595 &metrics,exception);
4596 clone_info=DestroyDrawInfo(clone_info);
4597 cursor+=metrics.width;
4598 if (graphic_context[n]->compliance != SVGCompliance)
4599 cursor=0.0;
4600 break;
4601 }
4602 case ImagePrimitive:
4603 {
4604 if (primitive_info[j].coordinates != 2)
4605 {
4606 status=MagickFalse;
4607 break;
4608 }
4609 (void) GetNextToken(q,&q,extent,token);
4610 (void) CloneString(&primitive_info[j].text,token);
4611 break;
4612 }
4613 }
4614 mvg_info.offset=i;
4615 if (status == 0)
4616 break;
4617 primitive_info[i].primitive=UndefinedPrimitive;
4618 if ((draw_info->debug != MagickFalse) && (q > p))
4619 (void) LogMagickEvent(DrawEvent,GetMagickModule()," %.*s",(int) (q-p),p);
4620 /*
4621 Sanity check.
4622 */
4623 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,ExpandAffine(
4624 &graphic_context[n]->affine));
4625 primitive_info=(*mvg_info.primitive_info);
4626 if (status == 0)
4627 break;
4628 status&=(MagickStatusType) CheckPrimitiveExtent(&mvg_info,(double)
4629 graphic_context[n]->stroke_width);
4630 primitive_info=(*mvg_info.primitive_info);
4631 if (status == 0)
4632 break;
4633 if (i == 0)
4634 continue;
4635 /*
4636 Transform points.
4637 */
4638 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4639 {
4640 point=primitive_info[i].point;
4641 primitive_info[i].point.x=graphic_context[n]->affine.sx*point.x+
4642 graphic_context[n]->affine.ry*point.y+graphic_context[n]->affine.tx;
4643 primitive_info[i].point.y=graphic_context[n]->affine.rx*point.x+
4644 graphic_context[n]->affine.sy*point.y+graphic_context[n]->affine.ty;
4645 point=primitive_info[i].point;
4646 if (point.x < graphic_context[n]->bounds.x1)
4647 graphic_context[n]->bounds.x1=point.x;
4648 if (point.y < graphic_context[n]->bounds.y1)
4649 graphic_context[n]->bounds.y1=point.y;
4650 if (point.x > graphic_context[n]->bounds.x2)
4651 graphic_context[n]->bounds.x2=point.x;
4652 if (point.y > graphic_context[n]->bounds.y2)
4653 graphic_context[n]->bounds.y2=point.y;
4654 if (primitive_info[i].primitive == ImagePrimitive)
4655 break;
4656 if (i >= (ssize_t) number_points)
4657 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
4658 }
4659 if (graphic_context[n]->render != MagickFalse)
4660 {
4661 if ((n != 0) && (graphic_context[n]->compliance != SVGCompliance) &&
4662 (graphic_context[n]->clip_mask != (char *) NULL) &&
4663 (LocaleCompare(graphic_context[n]->clip_mask,
4664 graphic_context[n-1]->clip_mask) != 0))
4665 {
4666 const char
4667 *clip_path;
4668
4669 clip_path=(const char *) GetValueFromSplayTree(macros,
4670 graphic_context[n]->clip_mask);
4671 if (clip_path != (const char *) NULL)
4672 (void) SetImageArtifact(image,graphic_context[n]->clip_mask,
4673 clip_path);
4674 status&=(MagickStatusType) DrawClipPath_(image,graphic_context[n],
4675 graphic_context[n]->clip_mask,depth+1,exception);
4676 }
4677 status&=(MagickStatusType) DrawPrimitive(image,graphic_context[n],
4678 primitive_info,exception);
4679 }
4680 proceed=SetImageProgress(image,RenderImageTag,q-primitive,(MagickSizeType)
4681 primitive_extent);
4682 if (proceed == MagickFalse)
4683 break;
4684 if (status == 0)
4685 break;
4686 }
4687 if (draw_info->debug != MagickFalse)
4688 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end draw-image");
4689 /*
4690 Relinquish resources.
4691 */
4692 macros=DestroySplayTree(macros);
4693 token=DestroyString(token);
4694 if (primitive_info != (PrimitiveInfo *) NULL)
4695 {
4696 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
4697 if (primitive_info[i].text != (char *) NULL)
4698 primitive_info[i].text=DestroyString(primitive_info[i].text);
4699 primitive_info=(PrimitiveInfo *) RelinquishMagickMemory(primitive_info);
4700 }
4701 primitive=DestroyString(primitive);
4702 if (stops != (StopInfo *) NULL)
4703 stops=(StopInfo *) RelinquishMagickMemory(stops);
4704 for ( ; n >= 0; n--)
4705 graphic_context[n]=DestroyDrawInfo(graphic_context[n]);
4706 graphic_context=(DrawInfo **) RelinquishMagickMemory(graphic_context);
4707 if ((status == MagickFalse) && (exception->severity < ErrorException))
4708 ThrowBinaryException(DrawError,"NonconformingDrawingPrimitiveDefinition",
4709 keyword);
4710 return(status != 0 ? MagickTrue : MagickFalse);
4711}
4712
4713MagickExport MagickBooleanType DrawImage(Image *image,const DrawInfo *draw_info,
4714 ExceptionInfo *exception)
4715{
4716 MagickBooleanType
4717 status;
4718
4719 size_t
4720 macro_expansion = 0;
4721
4722 ((DrawInfo *) draw_info)->macro_expansion=(&macro_expansion);
4723 status=RenderMVGContent(image,draw_info,0,exception);
4724 ((DrawInfo *) draw_info)->macro_expansion=(size_t *) NULL;
4725 return(status);
4726}
4727␌
4728/*
4729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4730% %
4731% %
4732% %
4733% D r a w P a t t e r n P a t h %
4734% %
4735% %
4736% %
4737%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4738%
4739% DrawPatternPath() draws a pattern.
4740%
4741% The format of the DrawPatternPath method is:
4742%
4743% MagickBooleanType DrawPatternPath(Image *image,const DrawInfo *draw_info,
4744% const char *name,Image **pattern,ExceptionInfo *exception)
4745%
4746% A description of each parameter follows:
4747%
4748% o image: the image.
4749%
4750% o draw_info: the draw info.
4751%
4752% o name: the pattern name.
4753%
4754% o image: the image.
4755%
4756% o exception: return any errors or warnings in this structure.
4757%
4758*/
4759MagickExport MagickBooleanType DrawPatternPath(Image *image,
4760 const DrawInfo *draw_info,const char *name,Image **pattern,
4761 ExceptionInfo *exception)
4762{
4763 char
4764 property[MagickPathExtent];
4765
4766 const char
4767 *geometry,
4768 *path,
4769 *type;
4770
4771 DrawInfo
4772 *clone_info;
4773
4774 ImageInfo
4775 *image_info;
4776
4777 MagickBooleanType
4778 status;
4779
4780 assert(image != (Image *) NULL);
4781 assert(image->signature == MagickCoreSignature);
4782 assert(draw_info != (const DrawInfo *) NULL);
4783 assert(name != (const char *) NULL);
4784 if (IsEventLogging() != MagickFalse)
4785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
4786 (void) FormatLocaleString(property,MagickPathExtent,"%s",name);
4787 path=GetImageArtifact(image,property);
4788 if (path == (const char *) NULL)
4789 return(MagickFalse);
4790 (void) FormatLocaleString(property,MagickPathExtent,"%s-geometry",name);
4791 geometry=GetImageArtifact(image,property);
4792 if (geometry == (const char *) NULL)
4793 return(MagickFalse);
4794 if ((*pattern) != (Image *) NULL)
4795 *pattern=DestroyImage(*pattern);
4796 image_info=AcquireImageInfo();
4797 image_info->size=AcquireString(geometry);
4798 *pattern=AcquireImage(image_info,exception);
4799 image_info=DestroyImageInfo(image_info);
4800 (void) QueryColorCompliance("#00000000",AllCompliance,
4801 &(*pattern)->background_color,exception);
4802 (void) SetImageBackgroundColor(*pattern,exception);
4803 if (draw_info->debug != MagickFalse)
4804 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
4805 "begin pattern-path %s %s",name,geometry);
4806 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
4807 if (clone_info->fill_pattern != (Image *) NULL)
4808 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
4809 if (clone_info->stroke_pattern != (Image *) NULL)
4810 clone_info->stroke_pattern=DestroyImage(clone_info->stroke_pattern);
4811 (void) FormatLocaleString(property,MagickPathExtent,"%s-type",name);
4812 type=GetImageArtifact(image,property);
4813 if (type != (const char *) NULL)
4814 clone_info->gradient.type=(GradientType) ParseCommandOption(
4815 MagickGradientOptions,MagickFalse,type);
4816 (void) CloneString(&clone_info->primitive,path);
4817 status=RenderMVGContent(*pattern,clone_info,0,exception);
4818 clone_info=DestroyDrawInfo(clone_info);
4819 if (draw_info->debug != MagickFalse)
4820 (void) LogMagickEvent(DrawEvent,GetMagickModule(),"end pattern-path");
4821 return(status);
4822}
4823␌
4824/*
4825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4826% %
4827% %
4828% %
4829+ D r a w P o l y g o n P r i m i t i v e %
4830% %
4831% %
4832% %
4833%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4834%
4835% DrawPolygonPrimitive() draws a polygon on the image.
4836%
4837% The format of the DrawPolygonPrimitive method is:
4838%
4839% MagickBooleanType DrawPolygonPrimitive(Image *image,
4840% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
4841% ExceptionInfo *exception)
4842%
4843% A description of each parameter follows:
4844%
4845% o image: the image.
4846%
4847% o draw_info: the draw info.
4848%
4849% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
4850%
4851% o exception: return any errors or warnings in this structure.
4852%
4853*/
4854
4855static PolygonInfo **DestroyPolygonTLS(PolygonInfo **polygon_info)
4856{
4857 ssize_t
4858 i;
4859
4860 assert(polygon_info != (PolygonInfo **) NULL);
4861 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
4862 if (polygon_info[i] != (PolygonInfo *) NULL)
4863 polygon_info[i]=DestroyPolygonInfo(polygon_info[i]);
4864 polygon_info=(PolygonInfo **) RelinquishMagickMemory(polygon_info);
4865 return(polygon_info);
4866}
4867
4868static PolygonInfo **AcquirePolygonTLS(const PrimitiveInfo *primitive_info,
4869 ExceptionInfo *exception)
4870{
4871 PathInfo
4872 *magick_restrict path_info;
4873
4874 PolygonInfo
4875 **polygon_info;
4876
4877 size_t
4878 number_threads;
4879
4880 number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
4881 polygon_info=(PolygonInfo **) AcquireQuantumMemory(number_threads,
4882 sizeof(*polygon_info));
4883 if (polygon_info == (PolygonInfo **) NULL)
4884 {
4885 (void) ThrowMagickException(exception,GetMagickModule(),
4886 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4887 return((PolygonInfo **) NULL);
4888 }
4889 (void) memset(polygon_info,0,number_threads*sizeof(*polygon_info));
4890 path_info=ConvertPrimitiveToPath(primitive_info,exception);
4891 if (path_info == (PathInfo *) NULL)
4892 return(DestroyPolygonTLS(polygon_info));
4893 polygon_info[0]=ConvertPathToPolygon(path_info,exception);
4894 if (polygon_info[0] == (PolygonInfo *) NULL)
4895 {
4896 (void) ThrowMagickException(exception,GetMagickModule(),
4897 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4898 return(DestroyPolygonTLS(polygon_info));
4899 }
4900 path_info=(PathInfo *) RelinquishMagickMemory(path_info);
4901 return(polygon_info);
4902}
4903
4904static MagickBooleanType ClonePolygonEdgesTLS(PolygonInfo **polygon_info,
4905 const size_t number_threads,ExceptionInfo *exception)
4906{
4907 ssize_t
4908 i;
4909
4910 for (i=1; i < (ssize_t) number_threads; i++)
4911 {
4912 EdgeInfo
4913 *edge_info;
4914
4915 ssize_t
4916 j;
4917
4918 polygon_info[i]=(PolygonInfo *) AcquireMagickMemory(
4919 sizeof(*polygon_info[i]));
4920 if (polygon_info[i] == (PolygonInfo *) NULL)
4921 {
4922 (void) ThrowMagickException(exception,GetMagickModule(),
4923 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4924 return(MagickFalse);
4925 }
4926 polygon_info[i]->number_edges=0;
4927 edge_info=polygon_info[0]->edges;
4928 polygon_info[i]->edges=(EdgeInfo *) AcquireQuantumMemory(
4929 polygon_info[0]->number_edges,sizeof(*edge_info));
4930 if (polygon_info[i]->edges == (EdgeInfo *) NULL)
4931 {
4932 (void) ThrowMagickException(exception,GetMagickModule(),
4933 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4934 return(MagickFalse);
4935 }
4936 (void) memcpy(polygon_info[i]->edges,edge_info,
4937 polygon_info[0]->number_edges*sizeof(*edge_info));
4938 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4939 polygon_info[i]->edges[j].points=(PointInfo *) NULL;
4940 polygon_info[i]->number_edges=polygon_info[0]->number_edges;
4941 for (j=0; j < (ssize_t) polygon_info[i]->number_edges; j++)
4942 {
4943 edge_info=polygon_info[0]->edges+j;
4944 polygon_info[i]->edges[j].points=(PointInfo *) AcquireQuantumMemory(
4945 edge_info->number_points,sizeof(*edge_info));
4946 if (polygon_info[i]->edges[j].points == (PointInfo *) NULL)
4947 {
4948 (void) ThrowMagickException(exception,GetMagickModule(),
4949 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
4950 return(MagickFalse);
4951 }
4952 (void) memcpy(polygon_info[i]->edges[j].points,edge_info->points,
4953 edge_info->number_points*sizeof(*edge_info->points));
4954 }
4955 }
4956 return(MagickTrue);
4957}
4958
4959static size_t DestroyEdge(PolygonInfo *polygon_info,const ssize_t edge)
4960{
4961 assert(edge < (ssize_t) polygon_info->number_edges);
4962 polygon_info->edges[edge].points=(PointInfo *) RelinquishMagickMemory(
4963 polygon_info->edges[edge].points);
4964 polygon_info->number_edges--;
4965 if (edge < (ssize_t) polygon_info->number_edges)
4966 (void) memmove(polygon_info->edges+edge,polygon_info->edges+edge+1,
4967 (polygon_info->number_edges-(size_t) edge)*sizeof(*polygon_info->edges));
4968 return(polygon_info->number_edges);
4969}
4970
4971static double GetFillAlpha(PolygonInfo *polygon_info,const double mid,
4972 const MagickBooleanType fill,const FillRule fill_rule,const ssize_t x,
4973 const ssize_t y,double *stroke_alpha)
4974{
4975 double
4976 alpha,
4977 beta,
4978 distance,
4979 subpath_alpha;
4980
4981 const PointInfo
4982 *q;
4983
4984 EdgeInfo
4985 *p;
4986
4987 PointInfo
4988 delta;
4989
4990 ssize_t
4991 i,
4992 j,
4993 winding_number;
4994
4995 /*
4996 Compute fill & stroke opacity for this (x,y) point.
4997 */
4998 *stroke_alpha=0.0;
4999 subpath_alpha=0.0;
5000 p=polygon_info->edges;
5001 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
5002 {
5003 if ((double) y <= (p->bounds.y1-mid-0.5))
5004 break;
5005 if ((double) y > (p->bounds.y2+mid+0.5))
5006 {
5007 p--;
5008 (void) DestroyEdge(polygon_info,j--);
5009 continue;
5010 }
5011 if (((double) x <= (p->bounds.x1-mid-0.5)) ||
5012 ((double) x > (p->bounds.x2+mid+0.5)))
5013 continue;
5014 i=(ssize_t) MagickMax((double) p->highwater,1.0);
5015 for ( ; i < (ssize_t) p->number_points; i++)
5016 {
5017 if ((double) y <= (p->points[i-1].y-mid-0.5))
5018 break;
5019 if ((double) y > (p->points[i].y+mid+0.5))
5020 continue;
5021 if (p->scanline != (double) y)
5022 {
5023 p->scanline=(double) y;
5024 p->highwater=(size_t) i;
5025 }
5026 /*
5027 Compute distance between a point and an edge.
5028 */
5029 q=p->points+i-1;
5030 delta.x=(q+1)->x-q->x;
5031 delta.y=(q+1)->y-q->y;
5032 beta=delta.x*(x-q->x)+delta.y*(y-q->y); /* segLen*point-cos(theta) */
5033 if (beta <= 0.0)
5034 {
5035 /*
5036 Cosine <= 0, point is closest to q.
5037 */
5038 delta.x=(double) x-q->x;
5039 delta.y=(double) y-q->y;
5040 distance=delta.x*delta.x+delta.y*delta.y;
5041 }
5042 else
5043 {
5044 alpha=delta.x*delta.x+delta.y*delta.y; /* segLen*segLen */
5045 if (beta >= alpha)
5046 {
5047 /*
5048 Point is closest to q+1.
5049 */
5050 delta.x=(double) x-(q+1)->x;
5051 delta.y=(double) y-(q+1)->y;
5052 distance=delta.x*delta.x+delta.y*delta.y;
5053 }
5054 else
5055 {
5056 /*
5057 Point is closest to point between q & q+1.
5058 */
5059 alpha=MagickSafeReciprocal(alpha);
5060 beta=delta.x*(y-q->y)-delta.y*(x-q->x);
5061 distance=alpha*beta*beta;
5062 }
5063 }
5064 /*
5065 Compute stroke & subpath opacity.
5066 */
5067 beta=0.0;
5068 if (p->ghostline == MagickFalse)
5069 {
5070 alpha=mid+0.5;
5071 if ((*stroke_alpha < 1.0) &&
5072 (distance <= ((alpha+0.25)*(alpha+0.25))))
5073 {
5074 alpha=mid-0.5;
5075 if (distance <= ((alpha+0.25)*(alpha+0.25)))
5076 *stroke_alpha=1.0;
5077 else
5078 {
5079 beta=1.0;
5080 if (fabs(distance-1.0) >= MagickEpsilon)
5081 beta=sqrt((double) distance);
5082 alpha=beta-mid-0.5;
5083 if (*stroke_alpha < ((alpha-0.25)*(alpha-0.25)))
5084 *stroke_alpha=(alpha-0.25)*(alpha-0.25);
5085 }
5086 }
5087 }
5088 if ((fill == MagickFalse) || (distance > 1.0) || (subpath_alpha >= 1.0))
5089 continue;
5090 if (distance <= 0.0)
5091 {
5092 subpath_alpha=1.0;
5093 continue;
5094 }
5095 if (distance > 1.0)
5096 continue;
5097 if (fabs(beta) < MagickEpsilon)
5098 {
5099 beta=1.0;
5100 if (fabs(distance-1.0) >= MagickEpsilon)
5101 beta=sqrt(distance);
5102 }
5103 alpha=beta-1.0;
5104 if (subpath_alpha < (alpha*alpha))
5105 subpath_alpha=alpha*alpha;
5106 }
5107 }
5108 /*
5109 Compute fill opacity.
5110 */
5111 if (fill == MagickFalse)
5112 return(0.0);
5113 if (subpath_alpha >= 1.0)
5114 return(1.0);
5115 /*
5116 Determine winding number.
5117 */
5118 winding_number=0;
5119 p=polygon_info->edges;
5120 for (j=0; j < (ssize_t) polygon_info->number_edges; j++, p++)
5121 {
5122 if ((double) y <= p->bounds.y1)
5123 break;
5124 if (((double) y > p->bounds.y2) || ((double) x <= p->bounds.x1))
5125 continue;
5126 if ((double) x > p->bounds.x2)
5127 {
5128 winding_number+=p->direction != 0 ? 1 : -1;
5129 continue;
5130 }
5131 i=(ssize_t) MagickMax((double) p->highwater,1.0);
5132 for ( ; i < (ssize_t) (p->number_points-1); i++)
5133 if ((double) y <= p->points[i].y)
5134 break;
5135 q=p->points+i-1;
5136 if ((((q+1)->x-q->x)*(y-q->y)) <= (((q+1)->y-q->y)*(x-q->x)))
5137 winding_number+=p->direction != 0 ? 1 : -1;
5138 }
5139 if (fill_rule != NonZeroRule)
5140 {
5141 if ((MagickAbsoluteValue(winding_number) & 0x01) != 0)
5142 return(1.0);
5143 }
5144 else
5145 if (MagickAbsoluteValue(winding_number) != 0)
5146 return(1.0);
5147 return(subpath_alpha);
5148}
5149
5150static MagickBooleanType DrawPolygonPrimitive(Image *image,
5151 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5152 ExceptionInfo *exception)
5153{
5154 typedef struct _ExtentInfo
5155 {
5156 ssize_t
5157 x1,
5158 y1,
5159 x2,
5160 y2;
5161 } ExtentInfo;
5162
5163 CacheView
5164 *image_view;
5165
5166 const char
5167 *artifact;
5168
5169 double
5170 mid;
5171
5172 EdgeInfo
5173 *p;
5174
5175 ExtentInfo
5176 poly_extent;
5177
5178 MagickBooleanType
5179 fill,
5180 status;
5181
5182 PolygonInfo
5183 **magick_restrict polygon_info;
5184
5185 SegmentInfo
5186 bounds;
5187
5188 size_t
5189 number_threads;
5190
5191 ssize_t
5192 i,
5193 y;
5194
5195 assert(image != (Image *) NULL);
5196 assert(image->signature == MagickCoreSignature);
5197 assert(draw_info != (DrawInfo *) NULL);
5198 assert(draw_info->signature == MagickCoreSignature);
5199 assert(primitive_info != (PrimitiveInfo *) NULL);
5200 if (IsEventLogging() != MagickFalse)
5201 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
5202 if (primitive_info->coordinates <= 1)
5203 return(MagickTrue);
5204 /*
5205 Compute bounding box.
5206 */
5207 polygon_info=AcquirePolygonTLS(primitive_info,exception);
5208 if (polygon_info == (PolygonInfo **) NULL)
5209 return(MagickFalse);
5210 if (draw_info->debug != MagickFalse)
5211 (void) LogMagickEvent(DrawEvent,GetMagickModule()," begin draw-polygon");
5212 fill=(primitive_info->method == FillToBorderMethod) ||
5213 (primitive_info->method == FloodfillMethod) ? MagickTrue : MagickFalse;
5214 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5215 bounds=polygon_info[0]->edges[0].bounds;
5216 artifact=GetImageArtifact(image,"draw:render-bounding-rectangles");
5217 if (IsStringTrue(artifact) != MagickFalse)
5218 (void) DrawBoundingRectangles(image,draw_info,polygon_info[0],exception);
5219 for (i=1; i < (ssize_t) polygon_info[0]->number_edges; i++)
5220 {
5221 p=polygon_info[0]->edges+i;
5222 if (p->bounds.x1 < bounds.x1)
5223 bounds.x1=p->bounds.x1;
5224 if (p->bounds.y1 < bounds.y1)
5225 bounds.y1=p->bounds.y1;
5226 if (p->bounds.x2 > bounds.x2)
5227 bounds.x2=p->bounds.x2;
5228 if (p->bounds.y2 > bounds.y2)
5229 bounds.y2=p->bounds.y2;
5230 }
5231 bounds.x1-=(mid+1.0);
5232 bounds.y1-=(mid+1.0);
5233 bounds.x2+=(mid+1.0);
5234 bounds.y2+=(mid+1.0);
5235 if ((bounds.x1 >= (double) image->columns) ||
5236 (bounds.y1 >= (double) image->rows) ||
5237 (bounds.x2 <= 0.0) || (bounds.y2 <= 0.0))
5238 {
5239 polygon_info=DestroyPolygonTLS(polygon_info);
5240 return(MagickTrue); /* virtual polygon */
5241 }
5242 bounds.x1=bounds.x1 < 0.0 ? 0.0 : bounds.x1 >= (double) image->columns-1.0 ?
5243 (double) image->columns-1.0 : bounds.x1;
5244 bounds.y1=bounds.y1 < 0.0 ? 0.0 : bounds.y1 >= (double) image->rows-1.0 ?
5245 (double) image->rows-1.0 : bounds.y1;
5246 bounds.x2=bounds.x2 < 0.0 ? 0.0 : bounds.x2 >= (double) image->columns-1.0 ?
5247 (double) image->columns-1.0 : bounds.x2;
5248 bounds.y2=bounds.y2 < 0.0 ? 0.0 : bounds.y2 >= (double) image->rows-1.0 ?
5249 (double) image->rows-1.0 : bounds.y2;
5250 poly_extent.x1=CastDoubleToSsizeT(ceil(bounds.x1-0.5));
5251 poly_extent.y1=CastDoubleToSsizeT(ceil(bounds.y1-0.5));
5252 poly_extent.x2=CastDoubleToSsizeT(floor(bounds.x2+0.5));
5253 poly_extent.y2=CastDoubleToSsizeT(floor(bounds.y2+0.5));
5254 number_threads=(size_t) GetMagickNumberThreads(image,image,(size_t)
5255 (poly_extent.y2-poly_extent.y1+1),1);
5256 status=ClonePolygonEdgesTLS(polygon_info,number_threads,exception);
5257 if (status == MagickFalse)
5258 {
5259 polygon_info=DestroyPolygonTLS(polygon_info);
5260 return(status);
5261 }
5262 image_view=AcquireAuthenticCacheView(image,exception);
5263 if ((primitive_info->coordinates == 1) ||
5264 (polygon_info[0]->number_edges == 0))
5265 {
5266 /*
5267 Draw point.
5268 */
5269#if defined(MAGICKCORE_OPENMP_SUPPORT)
5270 #pragma omp parallel for schedule(static) shared(status) \
5271 num_threads((int) number_threads)
5272#endif
5273 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5274 {
5275 PixelInfo
5276 pixel;
5277
5278 ssize_t
5279 x;
5280
5281 Quantum
5282 *magick_restrict q;
5283
5284 if (status == MagickFalse)
5285 continue;
5286 x=poly_extent.x1;
5287 q=GetCacheViewAuthenticPixels(image_view,x,y,(size_t) (poly_extent.x2-
5288 x+1),1,exception);
5289 if (q == (Quantum *) NULL)
5290 {
5291 status=MagickFalse;
5292 continue;
5293 }
5294 GetPixelInfo(image,&pixel);
5295 for ( ; x <= poly_extent.x2; x++)
5296 {
5297 if ((x == CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5))) &&
5298 (y == CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5))))
5299 {
5300 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&pixel,
5301 exception);
5302 SetPixelViaPixelInfo(image,&pixel,q);
5303 }
5304 q+=(ptrdiff_t) GetPixelChannels(image);
5305 }
5306 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5307 status=MagickFalse;
5308 }
5309 image_view=DestroyCacheView(image_view);
5310 polygon_info=DestroyPolygonTLS(polygon_info);
5311 if (draw_info->debug != MagickFalse)
5312 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5313 " end draw-polygon");
5314 return(status);
5315 }
5316 /*
5317 Draw polygon or line.
5318 */
5319#if defined(MAGICKCORE_OPENMP_SUPPORT)
5320 #pragma omp parallel for schedule(static) shared(status) \
5321 num_threads((int) number_threads)
5322#endif
5323 for (y=poly_extent.y1; y <= poly_extent.y2; y++)
5324 {
5325 const int
5326 id = GetOpenMPThreadId();
5327
5328 Quantum
5329 *magick_restrict q;
5330
5331 ssize_t
5332 x;
5333
5334 if (status == MagickFalse)
5335 continue;
5336 q=GetCacheViewAuthenticPixels(image_view,poly_extent.x1,y,(size_t)
5337 (poly_extent.x2-poly_extent.x1+1),1,exception);
5338 if (q == (Quantum *) NULL)
5339 {
5340 status=MagickFalse;
5341 continue;
5342 }
5343 for (x=poly_extent.x1; x <= poly_extent.x2; x++)
5344 {
5345 double
5346 fill_alpha,
5347 stroke_alpha;
5348
5349 PixelInfo
5350 fill_color,
5351 stroke_color;
5352
5353 /*
5354 Fill and/or stroke.
5355 */
5356 fill_alpha=GetFillAlpha(polygon_info[id],mid,fill,draw_info->fill_rule,
5357 x,y,&stroke_alpha);
5358 if (draw_info->stroke_antialias == MagickFalse)
5359 {
5360 fill_alpha=fill_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5361 stroke_alpha=stroke_alpha >= AntialiasThreshold ? 1.0 : 0.0;
5362 }
5363 GetFillColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&fill_color,
5364 exception);
5365 CompositePixelOver(image,&fill_color,fill_alpha*fill_color.alpha,q,
5366 (double) GetPixelAlpha(image,q),q);
5367 GetStrokeColor(draw_info,x-poly_extent.x1,y-poly_extent.y1,&stroke_color,
5368 exception);
5369 CompositePixelOver(image,&stroke_color,stroke_alpha*stroke_color.alpha,q,
5370 (double) GetPixelAlpha(image,q),q);
5371 q+=(ptrdiff_t) GetPixelChannels(image);
5372 }
5373 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
5374 status=MagickFalse;
5375 }
5376 image_view=DestroyCacheView(image_view);
5377 polygon_info=DestroyPolygonTLS(polygon_info);
5378 if (draw_info->debug != MagickFalse)
5379 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-polygon");
5380 return(status);
5381}
5382␌
5383/*
5384%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5385% %
5386% %
5387% %
5388% D r a w P r i m i t i v e %
5389% %
5390% %
5391% %
5392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5393%
5394% DrawPrimitive() draws a primitive (line, rectangle, ellipse) on the image.
5395%
5396% The format of the DrawPrimitive method is:
5397%
5398% MagickBooleanType DrawPrimitive(Image *image,const DrawInfo *draw_info,
5399% PrimitiveInfo *primitive_info,ExceptionInfo *exception)
5400%
5401% A description of each parameter follows:
5402%
5403% o image: the image.
5404%
5405% o draw_info: the draw info.
5406%
5407% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5408%
5409% o exception: return any errors or warnings in this structure.
5410%
5411*/
5412static void LogPrimitiveInfo(const PrimitiveInfo *primitive_info)
5413{
5414 const char
5415 *methods[] =
5416 {
5417 "point",
5418 "replace",
5419 "floodfill",
5420 "filltoborder",
5421 "reset",
5422 "?"
5423 };
5424
5425 PointInfo
5426 p,
5427 point,
5428 q;
5429
5430 ssize_t
5431 i,
5432 x;
5433
5434 ssize_t
5435 coordinates,
5436 y;
5437
5438 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5439 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5440 switch (primitive_info->primitive)
5441 {
5442 case AlphaPrimitive:
5443 {
5444 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5445 "AlphaPrimitive %.17g,%.17g %s",(double) x,(double) y,
5446 methods[primitive_info->method]);
5447 return;
5448 }
5449 case ColorPrimitive:
5450 {
5451 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5452 "ColorPrimitive %.17g,%.17g %s",(double) x,(double) y,
5453 methods[primitive_info->method]);
5454 return;
5455 }
5456 case ImagePrimitive:
5457 {
5458 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5459 "ImagePrimitive %.17g,%.17g",(double) x,(double) y);
5460 return;
5461 }
5462 case PointPrimitive:
5463 {
5464 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5465 "PointPrimitive %.17g,%.17g %s",(double) x,(double) y,
5466 methods[primitive_info->method]);
5467 return;
5468 }
5469 case TextPrimitive:
5470 {
5471 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5472 "TextPrimitive %.17g,%.17g",(double) x,(double) y);
5473 return;
5474 }
5475 default:
5476 break;
5477 }
5478 coordinates=0;
5479 p=primitive_info[0].point;
5480 q.x=(-1.0);
5481 q.y=(-1.0);
5482 for (i=0; primitive_info[i].primitive != UndefinedPrimitive; i++)
5483 {
5484 point=primitive_info[i].point;
5485 if (coordinates <= 0)
5486 {
5487 coordinates=(ssize_t) primitive_info[i].coordinates;
5488 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5489 " begin open (%.17g)",(double) coordinates);
5490 p=point;
5491 }
5492 point=primitive_info[i].point;
5493 if ((fabs(q.x-point.x) >= MagickEpsilon) ||
5494 (fabs(q.y-point.y) >= MagickEpsilon))
5495 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5496 " %.17g: %.18g,%.18g",(double) coordinates,point.x,point.y);
5497 else
5498 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5499 " %.17g: %g %g (duplicate)",(double) coordinates,point.x,point.y);
5500 q=point;
5501 coordinates--;
5502 if (coordinates > 0)
5503 continue;
5504 if ((fabs(p.x-point.x) >= MagickEpsilon) ||
5505 (fabs(p.y-point.y) >= MagickEpsilon))
5506 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end last (%.17g)",
5507 (double) coordinates);
5508 else
5509 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end open (%.17g)",
5510 (double) coordinates);
5511 }
5512}
5513
5514MagickExport MagickBooleanType DrawPrimitive(Image *image,
5515 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
5516 ExceptionInfo *exception)
5517{
5518 CacheView
5519 *image_view;
5520
5521 MagickStatusType
5522 status;
5523
5524 ssize_t
5525 i,
5526 x;
5527
5528 ssize_t
5529 y;
5530
5531 if (draw_info->debug != MagickFalse)
5532 {
5533 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5534 " begin draw-primitive");
5535 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5536 " affine: %g,%g,%g,%g,%g,%g",draw_info->affine.sx,
5537 draw_info->affine.rx,draw_info->affine.ry,draw_info->affine.sy,
5538 draw_info->affine.tx,draw_info->affine.ty);
5539 }
5540 status=MagickTrue;
5541 if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
5542 ((IsPixelInfoGray(&draw_info->fill) == MagickFalse) ||
5543 (IsPixelInfoGray(&draw_info->stroke) == MagickFalse)))
5544 status&=(MagickStatusType) SetImageColorspace(image,sRGBColorspace,
5545 exception);
5546 if (draw_info->compliance == SVGCompliance)
5547 {
5548 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
5549 draw_info->clipping_mask,exception);
5550 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
5551 draw_info->composite_mask,exception);
5552 }
5553 x=CastDoubleToSsizeT(ceil(primitive_info->point.x-0.5));
5554 y=CastDoubleToSsizeT(ceil(primitive_info->point.y-0.5));
5555 image_view=AcquireAuthenticCacheView(image,exception);
5556 switch (primitive_info->primitive)
5557 {
5558 case AlphaPrimitive:
5559 {
5560 if ((image->alpha_trait & BlendPixelTrait) == 0)
5561 status&=(MagickStatusType) SetImageAlphaChannel(image,
5562 OpaqueAlphaChannel,exception);
5563 switch (primitive_info->method)
5564 {
5565 case PointMethod:
5566 default:
5567 {
5568 PixelInfo
5569 pixel;
5570
5571 Quantum
5572 *q;
5573
5574 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5575 if (q == (Quantum *) NULL)
5576 break;
5577 GetFillColor(draw_info,x,y,&pixel,exception);
5578 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5579 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5580 exception);
5581 break;
5582 }
5583 case ReplaceMethod:
5584 {
5585 PixelInfo
5586 pixel,
5587 target;
5588
5589 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5590 x,y,&target,exception);
5591 GetPixelInfo(image,&pixel);
5592 for (y=0; y < (ssize_t) image->rows; y++)
5593 {
5594 Quantum
5595 *magick_restrict q;
5596
5597 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5598 exception);
5599 if (q == (Quantum *) NULL)
5600 break;
5601 for (x=0; x < (ssize_t) image->columns; x++)
5602 {
5603 GetPixelInfoPixel(image,q,&pixel);
5604 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5605 {
5606 q+=(ptrdiff_t) GetPixelChannels(image);
5607 continue;
5608 }
5609 GetFillColor(draw_info,x,y,&pixel,exception);
5610 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5611 q+=(ptrdiff_t) GetPixelChannels(image);
5612 }
5613 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5614 exception);
5615 if (status == MagickFalse)
5616 break;
5617 }
5618 break;
5619 }
5620 case FloodfillMethod:
5621 case FillToBorderMethod:
5622 {
5623 ChannelType
5624 channel_mask;
5625
5626 PixelInfo
5627 target;
5628
5629 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5630 TileVirtualPixelMethod,x,y,&target,exception);
5631 if (primitive_info->method == FillToBorderMethod)
5632 {
5633 target.red=(double) draw_info->border_color.red;
5634 target.green=(double) draw_info->border_color.green;
5635 target.blue=(double) draw_info->border_color.blue;
5636 }
5637 channel_mask=SetImageChannelMask(image,AlphaChannel);
5638 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5639 &target,x,y,primitive_info->method == FloodfillMethod ?
5640 MagickFalse : MagickTrue,exception);
5641 (void) SetImageChannelMask(image,channel_mask);
5642 break;
5643 }
5644 case ResetMethod:
5645 {
5646 PixelInfo
5647 pixel;
5648
5649 for (y=0; y < (ssize_t) image->rows; y++)
5650 {
5651 Quantum
5652 *magick_restrict q;
5653
5654 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5655 exception);
5656 if (q == (Quantum *) NULL)
5657 break;
5658 for (x=0; x < (ssize_t) image->columns; x++)
5659 {
5660 GetFillColor(draw_info,x,y,&pixel,exception);
5661 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
5662 q+=(ptrdiff_t) GetPixelChannels(image);
5663 }
5664 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5665 exception);
5666 if (status == MagickFalse)
5667 break;
5668 }
5669 break;
5670 }
5671 }
5672 break;
5673 }
5674 case ColorPrimitive:
5675 {
5676 switch (primitive_info->method)
5677 {
5678 case PointMethod:
5679 default:
5680 {
5681 PixelInfo
5682 pixel;
5683
5684 Quantum
5685 *q;
5686
5687 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5688 if (q == (Quantum *) NULL)
5689 break;
5690 GetPixelInfo(image,&pixel);
5691 GetFillColor(draw_info,x,y,&pixel,exception);
5692 SetPixelViaPixelInfo(image,&pixel,q);
5693 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5694 exception);
5695 break;
5696 }
5697 case ReplaceMethod:
5698 {
5699 PixelInfo
5700 pixel,
5701 target;
5702
5703 status&=(MagickStatusType) GetOneCacheViewVirtualPixelInfo(image_view,
5704 x,y,&target,exception);
5705 for (y=0; y < (ssize_t) image->rows; y++)
5706 {
5707 Quantum
5708 *magick_restrict q;
5709
5710 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5711 exception);
5712 if (q == (Quantum *) NULL)
5713 break;
5714 for (x=0; x < (ssize_t) image->columns; x++)
5715 {
5716 GetPixelInfoPixel(image,q,&pixel);
5717 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
5718 {
5719 q+=(ptrdiff_t) GetPixelChannels(image);
5720 continue;
5721 }
5722 GetFillColor(draw_info,x,y,&pixel,exception);
5723 SetPixelViaPixelInfo(image,&pixel,q);
5724 q+=(ptrdiff_t) GetPixelChannels(image);
5725 }
5726 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5727 exception);
5728 if (status == MagickFalse)
5729 break;
5730 }
5731 break;
5732 }
5733 case FloodfillMethod:
5734 case FillToBorderMethod:
5735 {
5736 PixelInfo
5737 target;
5738
5739 status&=(MagickStatusType) GetOneVirtualPixelInfo(image,
5740 TileVirtualPixelMethod,x,y,&target,exception);
5741 if (primitive_info->method == FillToBorderMethod)
5742 {
5743 target.red=(double) draw_info->border_color.red;
5744 target.green=(double) draw_info->border_color.green;
5745 target.blue=(double) draw_info->border_color.blue;
5746 }
5747 status&=(MagickStatusType) FloodfillPaintImage(image,draw_info,
5748 &target,x,y,primitive_info->method == FloodfillMethod ?
5749 MagickFalse : MagickTrue,exception);
5750 break;
5751 }
5752 case ResetMethod:
5753 {
5754 PixelInfo
5755 pixel;
5756
5757 GetPixelInfo(image,&pixel);
5758 for (y=0; y < (ssize_t) image->rows; y++)
5759 {
5760 Quantum
5761 *magick_restrict q;
5762
5763 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
5764 exception);
5765 if (q == (Quantum *) NULL)
5766 break;
5767 for (x=0; x < (ssize_t) image->columns; x++)
5768 {
5769 GetFillColor(draw_info,x,y,&pixel,exception);
5770 SetPixelViaPixelInfo(image,&pixel,q);
5771 q+=(ptrdiff_t) GetPixelChannels(image);
5772 }
5773 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5774 exception);
5775 if (status == MagickFalse)
5776 break;
5777 }
5778 break;
5779 }
5780 }
5781 break;
5782 }
5783 case ImagePrimitive:
5784 {
5785 AffineMatrix
5786 affine;
5787
5788 char
5789 composite_geometry[MagickPathExtent];
5790
5791 Image
5792 *composite_image,
5793 *composite_images;
5794
5795 ImageInfo
5796 *clone_info;
5797
5798 RectangleInfo
5799 geometry;
5800
5801 ssize_t
5802 x1,
5803 y1;
5804
5805 if (primitive_info->text == (char *) NULL)
5806 break;
5807 clone_info=AcquireImageInfo();
5808 composite_images=(Image *) NULL;
5809 if (LocaleNCompare(primitive_info->text,"data:",5) == 0)
5810 composite_images=ReadInlineImage(clone_info,primitive_info->text,
5811 exception);
5812 else
5813 if (*primitive_info->text != '\0')
5814 {
5815 /*
5816 Read composite image.
5817 */
5818 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5819 MagickPathExtent);
5820 (void) SetImageInfo(clone_info,1,exception);
5821 (void) CopyMagickString(clone_info->filename,primitive_info->text,
5822 MagickPathExtent);
5823 if (clone_info->size != (char *) NULL)
5824 clone_info->size=DestroyString(clone_info->size);
5825 if (clone_info->extract != (char *) NULL)
5826 clone_info->extract=DestroyString(clone_info->extract);
5827 clone_info->coder_depth=draw_info->image_info->coder_depth;
5828 composite_images=StrictReadImage(clone_info,exception);
5829 draw_info->image_info->coder_depth=clone_info->coder_depth;
5830 }
5831 clone_info=DestroyImageInfo(clone_info);
5832 if (composite_images == (Image *) NULL)
5833 {
5834 status=MagickFalse;
5835 break;
5836 }
5837 composite_image=RemoveFirstImageFromList(&composite_images);
5838 composite_images=DestroyImageList(composite_images);
5839 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5840 NULL,(void *) NULL);
5841 x1=CastDoubleToSsizeT(ceil(primitive_info[1].point.x-0.5));
5842 y1=CastDoubleToSsizeT(ceil(primitive_info[1].point.y-0.5));
5843 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5844 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5845 {
5846 /*
5847 Resize image.
5848 */
5849 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5850 "%gx%g!",primitive_info[1].point.x,primitive_info[1].point.y);
5851 composite_image->filter=image->filter;
5852 status&=(MagickStatusType) TransformImage(&composite_image,
5853 (char *) NULL,composite_geometry,exception);
5854 }
5855 if (composite_image->alpha_trait == UndefinedPixelTrait)
5856 status&=(MagickStatusType) SetImageAlphaChannel(composite_image,
5857 OpaqueAlphaChannel,exception);
5858 if (draw_info->alpha != OpaqueAlpha)
5859 status&=(MagickStatusType) SetImageAlpha(composite_image,
5860 draw_info->alpha,exception);
5861 SetGeometry(image,&geometry);
5862 image->gravity=draw_info->gravity;
5863 geometry.x=x;
5864 geometry.y=y;
5865 (void) FormatLocaleString(composite_geometry,MagickPathExtent,
5866 "%.17gx%.17g%+.20g%+.20g",(double) composite_image->columns,(double)
5867 composite_image->rows,(double) geometry.x,(double) geometry.y);
5868 (void) ParseGravityGeometry(image,composite_geometry,&geometry,exception);
5869 affine=draw_info->affine;
5870 affine.tx=(double) geometry.x;
5871 affine.ty=(double) geometry.y;
5872 composite_image->interpolate=image->interpolate;
5873 if ((draw_info->compose == OverCompositeOp) ||
5874 (draw_info->compose == SrcOverCompositeOp))
5875 status&=(MagickStatusType) DrawAffineImage(image,composite_image,
5876 &affine,exception);
5877 else
5878 status&=(MagickStatusType) CompositeImage(image,composite_image,
5879 draw_info->compose,MagickTrue,geometry.x,geometry.y,exception);
5880 composite_image=DestroyImage(composite_image);
5881 break;
5882 }
5883 case PointPrimitive:
5884 {
5885 PixelInfo
5886 fill_color;
5887
5888 Quantum
5889 *q;
5890
5891 if ((y < 0) || (y >= (ssize_t) image->rows))
5892 break;
5893 if ((x < 0) || (x >= (ssize_t) image->columns))
5894 break;
5895 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5896 if (q == (Quantum *) NULL)
5897 break;
5898 GetFillColor(draw_info,x,y,&fill_color,exception);
5899 CompositePixelOver(image,&fill_color,(double) fill_color.alpha,q,(double)
5900 GetPixelAlpha(image,q),q);
5901 status&=(MagickStatusType) SyncCacheViewAuthenticPixels(image_view,
5902 exception);
5903 break;
5904 }
5905 case TextPrimitive:
5906 {
5907 char
5908 geometry[MagickPathExtent];
5909
5910 DrawInfo
5911 *clone_info;
5912
5913 if (primitive_info->text == (char *) NULL)
5914 break;
5915 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5916 (void) CloneString(&clone_info->text,primitive_info->text);
5917 (void) FormatLocaleString(geometry,MagickPathExtent,"%+f%+f",
5918 primitive_info->point.x,primitive_info->point.y);
5919 (void) CloneString(&clone_info->geometry,geometry);
5920 status&=(MagickStatusType) AnnotateImage(image,clone_info,exception);
5921 clone_info=DestroyDrawInfo(clone_info);
5922 break;
5923 }
5924 default:
5925 {
5926 double
5927 mid,
5928 scale;
5929
5930 DrawInfo
5931 *clone_info;
5932
5933 if (IsEventLogging() != MagickFalse)
5934 LogPrimitiveInfo(primitive_info);
5935 scale=ExpandAffine(&draw_info->affine);
5936 if ((draw_info->dash_pattern != (double *) NULL) &&
5937 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5938 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5939 (draw_info->stroke.alpha != (double) TransparentAlpha))
5940 {
5941 /*
5942 Draw dash polygon.
5943 */
5944 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5945 clone_info->stroke_width=0.0;
5946 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5947 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5948 primitive_info,exception);
5949 clone_info=DestroyDrawInfo(clone_info);
5950 if (status != MagickFalse)
5951 status&=(MagickStatusType) DrawDashPolygon(draw_info,primitive_info,
5952 image,exception);
5953 break;
5954 }
5955 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5956 if ((mid > 1.0) &&
5957 ((draw_info->stroke.alpha != (double) TransparentAlpha) ||
5958 (draw_info->stroke_pattern != (Image *) NULL)))
5959 {
5960 double
5961 point_x,
5962 point_y;
5963
5964 MagickBooleanType
5965 closed_path;
5966
5967 /*
5968 Draw strokes while respecting line cap/join attributes.
5969 */
5970 closed_path=primitive_info[0].closed_subpath;
5971 i=(ssize_t) primitive_info[0].coordinates;
5972 point_x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5973 point_y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5974 if ((point_x < MagickEpsilon) && (point_y < MagickEpsilon))
5975 closed_path=MagickTrue;
5976 if ((((draw_info->linecap == RoundCap) ||
5977 (closed_path != MagickFalse)) &&
5978 (draw_info->linejoin == RoundJoin)) ||
5979 (primitive_info[i].primitive != UndefinedPrimitive))
5980 {
5981 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5982 primitive_info,exception);
5983 break;
5984 }
5985 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5986 clone_info->stroke_width=0.0;
5987 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
5988 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
5989 primitive_info,exception);
5990 clone_info=DestroyDrawInfo(clone_info);
5991 if (status != MagickFalse)
5992 status&=(MagickStatusType) DrawStrokePolygon(image,draw_info,
5993 primitive_info,exception);
5994 break;
5995 }
5996 status&=(MagickStatusType) DrawPolygonPrimitive(image,draw_info,
5997 primitive_info,exception);
5998 break;
5999 }
6000 }
6001 image_view=DestroyCacheView(image_view);
6002 if (draw_info->compliance == SVGCompliance)
6003 {
6004 status&=(MagickStatusType) SetImageMask(image,WritePixelMask,
6005 (Image *) NULL,exception);
6006 status&=(MagickStatusType) SetImageMask(image,CompositePixelMask,
6007 (Image *) NULL,exception);
6008 }
6009 if (draw_info->debug != MagickFalse)
6010 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
6011 return(status != 0 ? MagickTrue : MagickFalse);
6012}
6013␌
6014/*
6015%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6016% %
6017% %
6018% %
6019+ D r a w S t r o k e P o l y g o n %
6020% %
6021% %
6022% %
6023%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6024%
6025% DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
6026% the image while respecting the line cap and join attributes.
6027%
6028% The format of the DrawStrokePolygon method is:
6029%
6030% MagickBooleanType DrawStrokePolygon(Image *image,
6031% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
6032%
6033% A description of each parameter follows:
6034%
6035% o image: the image.
6036%
6037% o draw_info: the draw info.
6038%
6039% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
6040%
6041%
6042*/
6043
6044static MagickBooleanType DrawRoundLinecap(Image *image,
6045 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
6046 ExceptionInfo *exception)
6047{
6048 PrimitiveInfo
6049 linecap[5];
6050
6051 ssize_t
6052 i;
6053
6054 if (primitive_info->coordinates < 1)
6055 return(MagickFalse);
6056 for (i=0; i < 4; i++)
6057 linecap[i]=(*primitive_info);
6058 linecap[0].coordinates=4;
6059 linecap[1].point.x+=2.0*MagickEpsilon;
6060 linecap[2].point.x+=2.0*MagickEpsilon;
6061 linecap[2].point.y+=2.0*MagickEpsilon;
6062 linecap[3].point.y+=2.0*MagickEpsilon;
6063 linecap[4].primitive=UndefinedPrimitive;
6064 return(DrawPolygonPrimitive(image,draw_info,linecap,exception));
6065}
6066
6067static MagickBooleanType DrawStrokePolygon(Image *image,
6068 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info,
6069 ExceptionInfo *exception)
6070{
6071 DrawInfo
6072 *clone_info;
6073
6074 MagickBooleanType
6075 closed_path;
6076
6077 MagickStatusType
6078 status;
6079
6080 PrimitiveInfo
6081 *stroke_polygon;
6082
6083 const PrimitiveInfo
6084 *p,
6085 *q;
6086
6087 /*
6088 Draw stroked polygon.
6089 */
6090 if (draw_info->debug != MagickFalse)
6091 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
6092 " begin draw-stroke-polygon");
6093 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
6094 clone_info->fill=draw_info->stroke;
6095 if (clone_info->fill_pattern != (Image *) NULL)
6096 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
6097 if (clone_info->stroke_pattern != (Image *) NULL)
6098 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
6099 MagickTrue,exception);
6100 clone_info->stroke.alpha=(MagickRealType) TransparentAlpha;
6101 clone_info->stroke_width=0.0;
6102 clone_info->fill_rule=NonZeroRule;
6103 status=MagickTrue;
6104 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=(ptrdiff_t) p->coordinates)
6105 {
6106 if (p->coordinates == 1)
6107 continue;
6108 stroke_polygon=TraceStrokePolygon(draw_info,p,exception);
6109 if (stroke_polygon == (PrimitiveInfo *) NULL)
6110 {
6111 status=0;
6112 break;
6113 }
6114 status&=(MagickStatusType) DrawPolygonPrimitive(image,clone_info,
6115 stroke_polygon,exception);
6116 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
6117 if (status == 0)
6118 break;
6119 q=p+p->coordinates-1;
6120 closed_path=p->closed_subpath;
6121 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
6122 {
6123 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,p,
6124 exception);
6125 status&=(MagickStatusType) DrawRoundLinecap(image,draw_info,q,
6126 exception);
6127 }
6128 }
6129 clone_info=DestroyDrawInfo(clone_info);
6130 if (draw_info->debug != MagickFalse)
6131 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
6132 " end draw-stroke-polygon");
6133 return(status != 0 ? MagickTrue : MagickFalse);
6134}
6135␌
6136/*
6137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6138% %
6139% %
6140% %
6141% G e t A f f i n e M a t r i x %
6142% %
6143% %
6144% %
6145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6146%
6147% GetAffineMatrix() returns an AffineMatrix initialized to the identity
6148% matrix.
6149%
6150% The format of the GetAffineMatrix method is:
6151%
6152% void GetAffineMatrix(AffineMatrix *affine_matrix)
6153%
6154% A description of each parameter follows:
6155%
6156% o affine_matrix: the affine matrix.
6157%
6158*/
6159MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
6160{
6161 if (IsEventLogging() != MagickFalse)
6162 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
6163 assert(affine_matrix != (AffineMatrix *) NULL);
6164 (void) memset(affine_matrix,0,sizeof(*affine_matrix));
6165 affine_matrix->sx=1.0;
6166 affine_matrix->sy=1.0;
6167}
6168␌
6169/*
6170%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6171% %
6172% %
6173% %
6174+ G e t D r a w I n f o %
6175% %
6176% %
6177% %
6178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6179%
6180% GetDrawInfo() initializes draw_info to default values from image_info.
6181%
6182% The format of the GetDrawInfo method is:
6183%
6184% void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
6185%
6186% A description of each parameter follows:
6187%
6188% o image_info: the image info..
6189%
6190% o draw_info: the draw info.
6191%
6192*/
6193MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
6194{
6195 char
6196 *next_token;
6197
6198 const char
6199 *option;
6200
6201 ExceptionInfo
6202 *exception;
6203
6204 /*
6205 Initialize draw attributes.
6206 */
6207 assert(draw_info != (DrawInfo *) NULL);
6208 if (IsEventLogging() != MagickFalse)
6209 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
6210 (void) memset(draw_info,0,sizeof(*draw_info));
6211 draw_info->image_info=CloneImageInfo(image_info);
6212 GetAffineMatrix(&draw_info->affine);
6213 exception=AcquireExceptionInfo();
6214 (void) QueryColorCompliance("#000F",AllCompliance,&draw_info->fill,
6215 exception);
6216 (void) QueryColorCompliance("#FFF0",AllCompliance,&draw_info->stroke,
6217 exception);
6218 draw_info->stroke_antialias=draw_info->image_info->antialias;
6219 draw_info->stroke_width=1.0;
6220 draw_info->fill_rule=EvenOddRule;
6221 draw_info->alpha=OpaqueAlpha;
6222 draw_info->fill_alpha=OpaqueAlpha;
6223 draw_info->stroke_alpha=OpaqueAlpha;
6224 draw_info->linecap=ButtCap;
6225 draw_info->linejoin=MiterJoin;
6226 draw_info->miterlimit=10;
6227 draw_info->decorate=NoDecoration;
6228 draw_info->pointsize=12.0;
6229 draw_info->undercolor.alpha=(MagickRealType) TransparentAlpha;
6230 draw_info->compose=OverCompositeOp;
6231 draw_info->render=MagickTrue;
6232 draw_info->clip_path=MagickFalse;
6233 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
6234 MagickTrue : MagickFalse;
6235 draw_info->macro_expansion=(size_t *) NULL;
6236 if (draw_info->image_info->font != (char *) NULL)
6237 draw_info->font=AcquireString(draw_info->image_info->font);
6238 if (draw_info->image_info->density != (char *) NULL)
6239 draw_info->density=AcquireString(draw_info->image_info->density);
6240 draw_info->text_antialias=draw_info->image_info->antialias;
6241 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
6242 draw_info->pointsize=draw_info->image_info->pointsize;
6243 draw_info->border_color=draw_info->image_info->border_color;
6244 if (draw_info->image_info->server_name != (char *) NULL)
6245 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
6246 option=GetImageOption(draw_info->image_info,"direction");
6247 if (option != (const char *) NULL)
6248 draw_info->direction=(DirectionType) ParseCommandOption(
6249 MagickDirectionOptions,MagickFalse,option);
6250 else
6251 draw_info->direction=UndefinedDirection;
6252 option=GetImageOption(draw_info->image_info,"encoding");
6253 if (option != (const char *) NULL)
6254 (void) CloneString(&draw_info->encoding,option);
6255 option=GetImageOption(draw_info->image_info,"family");
6256 if (option != (const char *) NULL)
6257 (void) CloneString(&draw_info->family,option);
6258 option=GetImageOption(draw_info->image_info,"fill");
6259 if (option != (const char *) NULL)
6260 (void) QueryColorCompliance(option,AllCompliance,&draw_info->fill,
6261 exception);
6262 option=GetImageOption(draw_info->image_info,"gravity");
6263 if (option != (const char *) NULL)
6264 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
6265 MagickFalse,option);
6266 option=GetImageOption(draw_info->image_info,"interline-spacing");
6267 if (option != (const char *) NULL)
6268 draw_info->interline_spacing=GetDrawValue(option,&next_token);
6269 option=GetImageOption(draw_info->image_info,"interword-spacing");
6270 if (option != (const char *) NULL)
6271 draw_info->interword_spacing=GetDrawValue(option,&next_token);
6272 option=GetImageOption(draw_info->image_info,"kerning");
6273 if (option != (const char *) NULL)
6274 draw_info->kerning=GetDrawValue(option,&next_token);
6275 option=GetImageOption(draw_info->image_info,"stroke");
6276 if (option != (const char *) NULL)
6277 (void) QueryColorCompliance(option,AllCompliance,&draw_info->stroke,
6278 exception);
6279 option=GetImageOption(draw_info->image_info,"strokewidth");
6280 if (option != (const char *) NULL)
6281 draw_info->stroke_width=GetDrawValue(option,&next_token);
6282 option=GetImageOption(draw_info->image_info,"style");
6283 if (option != (const char *) NULL)
6284 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6285 MagickFalse,option);
6286 option=GetImageOption(draw_info->image_info,"undercolor");
6287 if (option != (const char *) NULL)
6288 (void) QueryColorCompliance(option,AllCompliance,&draw_info->undercolor,
6289 exception);
6290 option=GetImageOption(draw_info->image_info,"weight");
6291 if (option != (const char *) NULL)
6292 {
6293 ssize_t
6294 weight;
6295
6296 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6297 if (weight == -1)
6298 weight=(ssize_t) StringToUnsignedLong(option);
6299 draw_info->weight=(size_t) weight;
6300 }
6301 option=GetImageOption(draw_info->image_info,"word-break");
6302 if (option != (const char *) NULL)
6303 draw_info->word_break=(WordBreakType) ParseCommandOption(
6304 MagickWordBreakOptions,MagickFalse,option);
6305 exception=DestroyExceptionInfo(exception);
6306 draw_info->signature=MagickCoreSignature;
6307}
6308␌
6309/*
6310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6311% %
6312% %
6313% %
6314+ P e r m u t a t e %
6315% %
6316% %
6317% %
6318%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6319%
6320% Permutate() returns the permutation of the (n,k).
6321%
6322% The format of the Permutate method is:
6323%
6324% void Permutate(ssize_t n,ssize_t k)
6325%
6326% A description of each parameter follows:
6327%
6328% o n:
6329%
6330% o k:
6331%
6332%
6333*/
6334static inline double Permutate(const ssize_t n,const ssize_t k)
6335{
6336 double
6337 r;
6338
6339 ssize_t
6340 i;
6341
6342 r=1.0;
6343 for (i=k+1; i <= n; i++)
6344 r*=i;
6345 for (i=1; i <= (n-k); i++)
6346 r/=i;
6347 return(r);
6348}
6349␌
6350/*
6351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6352% %
6353% %
6354% %
6355+ T r a c e P r i m i t i v e %
6356% %
6357% %
6358% %
6359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6360%
6361% TracePrimitive is a collection of methods for generating graphic
6362% primitives such as arcs, ellipses, paths, etc.
6363%
6364*/
6365
6366static MagickBooleanType TraceArc(MVGInfo *mvg_info,const PointInfo start,
6367 const PointInfo end,const PointInfo degrees)
6368{
6369 PointInfo
6370 center,
6371 radius;
6372
6373 center.x=0.5*(end.x+start.x);
6374 center.y=0.5*(end.y+start.y);
6375 radius.x=fabs(center.x-start.x);
6376 radius.y=fabs(center.y-start.y);
6377 return(TraceEllipse(mvg_info,center,radius,degrees));
6378}
6379
6380static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,const PointInfo start,
6381 const PointInfo end,const PointInfo arc,const double angle,
6382 const MagickBooleanType large_arc,const MagickBooleanType sweep)
6383{
6384 double
6385 alpha,
6386 beta,
6387 delta,
6388 factor,
6389 gamma,
6390 theta;
6391
6392 MagickStatusType
6393 status;
6394
6395 PointInfo
6396 center,
6397 points[3],
6398 radii;
6399
6400 double
6401 cosine,
6402 sine;
6403
6404 PrimitiveInfo
6405 *primitive_info;
6406
6407 PrimitiveInfo
6408 *p;
6409
6410 ssize_t
6411 i;
6412
6413 size_t
6414 arc_segments;
6415
6416 ssize_t
6417 offset;
6418
6419 offset=mvg_info->offset;
6420 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6421 primitive_info->coordinates=0;
6422 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6423 (fabs(start.y-end.y) < MagickEpsilon))
6424 return(TracePoint(primitive_info,end));
6425 radii.x=fabs(arc.x);
6426 radii.y=fabs(arc.y);
6427 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6428 return(TraceLine(primitive_info,start,end));
6429 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
6430 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
6431 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6432 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6433 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6434 (radii.y*radii.y);
6435 if (delta < MagickEpsilon)
6436 return(TraceLine(primitive_info,start,end));
6437 if (delta > 1.0)
6438 {
6439 radii.x*=sqrt((double) delta);
6440 radii.y*=sqrt((double) delta);
6441 }
6442 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6443 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6444 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6445 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6446 alpha=points[1].x-points[0].x;
6447 beta=points[1].y-points[0].y;
6448 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6449 return(TraceLine(primitive_info,start,end));
6450 factor=MagickSafeReciprocal(alpha*alpha+beta*beta)-0.25;
6451 if (factor <= 0.0)
6452 factor=0.0;
6453 else
6454 {
6455 factor=sqrt((double) factor);
6456 if (sweep == large_arc)
6457 factor=(-factor);
6458 }
6459 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6460 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6461 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6462 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6463 if ((theta < 0.0) && (sweep != MagickFalse))
6464 theta+=2.0*MagickPI;
6465 else
6466 if ((theta > 0.0) && (sweep == MagickFalse))
6467 theta-=2.0*MagickPI;
6468 arc_segments=(size_t) CastDoubleToSsizeT(ceil(fabs((double) (theta/(0.5*
6469 MagickPI+MagickEpsilon)))));
6470 status=MagickTrue;
6471 p=primitive_info;
6472 for (i=0; i < (ssize_t) arc_segments; i++)
6473 {
6474 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6475 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
6476 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
6477 sin(fmod((double) beta,DegreesToRadians(360.0)));
6478 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
6479 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
6480 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6481 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
6482 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
6483 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6484 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
6485 theta/arc_segments),DegreesToRadians(360.0))));
6486 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
6487 theta/arc_segments),DegreesToRadians(360.0))));
6488 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
6489 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6490 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
6491 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6492 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6493 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6494 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6495 points[0].y);
6496 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6497 points[0].y);
6498 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6499 points[1].y);
6500 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6501 points[1].y);
6502 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6503 points[2].y);
6504 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6505 points[2].y);
6506 if (i == (ssize_t) (arc_segments-1))
6507 (p+3)->point=end;
6508 status&=(MagickStatusType) TraceBezier(mvg_info,4);
6509 if (status == 0)
6510 break;
6511 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
6512 mvg_info->offset+=(ssize_t) p->coordinates;
6513 p+=(ptrdiff_t) p->coordinates;
6514 }
6515 if (status == 0)
6516 return(MagickFalse);
6517 mvg_info->offset=offset;
6518 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6519 primitive_info->coordinates=(size_t) (p-primitive_info);
6520 primitive_info->closed_subpath=MagickFalse;
6521 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6522 {
6523 p->primitive=primitive_info->primitive;
6524 p--;
6525 }
6526 return(MagickTrue);
6527}
6528
6529static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6530 const size_t number_coordinates)
6531{
6532 double
6533 alpha,
6534 *coefficients,
6535 weight;
6536
6537 PointInfo
6538 end,
6539 point,
6540 *points;
6541
6542 PrimitiveInfo
6543 *primitive_info;
6544
6545 PrimitiveInfo
6546 *p;
6547
6548 ssize_t
6549 i,
6550 j;
6551
6552 size_t
6553 control_points,
6554 quantum;
6555
6556 /*
6557 Allocate coefficients.
6558 */
6559 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6560 quantum=number_coordinates;
6561 for (i=0; i < (ssize_t) number_coordinates; i++)
6562 {
6563 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6564 {
6565 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6566 if (alpha > (double) GetMaxMemoryRequest())
6567 {
6568 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6569 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6570 return(MagickFalse);
6571 }
6572 if (alpha > (double) quantum)
6573 quantum=(size_t) alpha;
6574 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6575 if (alpha > (double) quantum)
6576 quantum=(size_t) alpha;
6577 }
6578 }
6579 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6580 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6581 coefficients=(double *) AcquireQuantumMemory(number_coordinates,
6582 sizeof(*coefficients));
6583 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6584 sizeof(*points));
6585 if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
6586 {
6587 if (points != (PointInfo *) NULL)
6588 points=(PointInfo *) RelinquishMagickMemory(points);
6589 if (coefficients != (double *) NULL)
6590 coefficients=(double *) RelinquishMagickMemory(coefficients);
6591 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6592 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6593 return(MagickFalse);
6594 }
6595 control_points=quantum*number_coordinates;
6596 if (CheckPrimitiveExtent(mvg_info,(double) control_points+1) == MagickFalse)
6597 {
6598 points=(PointInfo *) RelinquishMagickMemory(points);
6599 coefficients=(double *) RelinquishMagickMemory(coefficients);
6600 return(MagickFalse);
6601 }
6602 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6603 /*
6604 Compute bezier points.
6605 */
6606 end=primitive_info[number_coordinates-1].point;
6607 for (i=0; i < (ssize_t) number_coordinates; i++)
6608 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6609 weight=0.0;
6610 for (i=0; i < (ssize_t) control_points; i++)
6611 {
6612 p=primitive_info;
6613 point.x=0.0;
6614 point.y=0.0;
6615 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
6616 for (j=0; j < (ssize_t) number_coordinates; j++)
6617 {
6618 point.x+=alpha*coefficients[j]*p->point.x;
6619 point.y+=alpha*coefficients[j]*p->point.y;
6620 alpha*=weight/(1.0-weight);
6621 p++;
6622 }
6623 points[i]=point;
6624 weight+=1.0/control_points;
6625 }
6626 /*
6627 Bezier curves are just short segmented polys.
6628 */
6629 p=primitive_info;
6630 for (i=0; i < (ssize_t) control_points; i++)
6631 {
6632 if (TracePoint(p,points[i]) == MagickFalse)
6633 {
6634 points=(PointInfo *) RelinquishMagickMemory(points);
6635 coefficients=(double *) RelinquishMagickMemory(coefficients);
6636 return(MagickFalse);
6637 }
6638 p+=(ptrdiff_t) p->coordinates;
6639 }
6640 if (TracePoint(p,end) == MagickFalse)
6641 {
6642 points=(PointInfo *) RelinquishMagickMemory(points);
6643 coefficients=(double *) RelinquishMagickMemory(coefficients);
6644 return(MagickFalse);
6645 }
6646 p+=(ptrdiff_t) p->coordinates;
6647 primitive_info->coordinates=(size_t) (p-primitive_info);
6648 primitive_info->closed_subpath=MagickFalse;
6649 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6650 {
6651 p->primitive=primitive_info->primitive;
6652 p--;
6653 }
6654 points=(PointInfo *) RelinquishMagickMemory(points);
6655 coefficients=(double *) RelinquishMagickMemory(coefficients);
6656 return(MagickTrue);
6657}
6658
6659static MagickBooleanType TraceCircle(MVGInfo *mvg_info,const PointInfo start,
6660 const PointInfo end)
6661{
6662 double
6663 alpha,
6664 beta,
6665 radius;
6666
6667 PointInfo
6668 offset,
6669 degrees;
6670
6671 alpha=end.x-start.x;
6672 beta=end.y-start.y;
6673 radius=hypot((double) alpha,(double) beta);
6674 offset.x=(double) radius;
6675 offset.y=(double) radius;
6676 degrees.x=0.0;
6677 degrees.y=360.0;
6678 return(TraceEllipse(mvg_info,start,offset,degrees));
6679}
6680
6681static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,const PointInfo center,
6682 const PointInfo radii,const PointInfo arc)
6683{
6684 double
6685 coordinates,
6686 delta,
6687 step,
6688 x,
6689 y;
6690
6691 PointInfo
6692 angle,
6693 point;
6694
6695 PrimitiveInfo
6696 *primitive_info;
6697
6698 PrimitiveInfo
6699 *p;
6700
6701 ssize_t
6702 i;
6703
6704 /*
6705 Ellipses are just short segmented polys.
6706 */
6707 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6708 primitive_info->coordinates=0;
6709 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6710 return(MagickTrue);
6711 delta=MagickSafeReciprocal(MagickMax(radii.x,radii.y));
6712 step=MagickPI/(MagickPI*MagickSafeReciprocal(delta))/8.0;
6713 angle.x=DegreesToRadians(arc.x);
6714 y=arc.y;
6715 while (y < arc.x)
6716 y+=360.0;
6717 angle.y=DegreesToRadians(y);
6718 coordinates=ceil((angle.y-angle.x)/step+1.0);
6719 if (CheckPrimitiveExtent(mvg_info,coordinates+1) == MagickFalse)
6720 return(MagickFalse);
6721 i=0;
6722 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6723 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6724 {
6725 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6726 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6727 if (i++ >= (ssize_t) coordinates)
6728 break;
6729 if (TracePoint(p,point) == MagickFalse)
6730 return(MagickFalse);
6731 p+=(ptrdiff_t) p->coordinates;
6732 }
6733 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6734 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6735 if (TracePoint(p,point) == MagickFalse)
6736 return(MagickFalse);
6737 p+=(ptrdiff_t) p->coordinates;
6738 primitive_info->coordinates=(size_t) (p-primitive_info);
6739 primitive_info->closed_subpath=MagickFalse;
6740 x=fabs(primitive_info[0].point.x-
6741 primitive_info[primitive_info->coordinates-1].point.x);
6742 y=fabs(primitive_info[0].point.y-
6743 primitive_info[primitive_info->coordinates-1].point.y);
6744 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6745 primitive_info->closed_subpath=MagickTrue;
6746 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6747 {
6748 p->primitive=primitive_info->primitive;
6749 p--;
6750 }
6751 return(MagickTrue);
6752}
6753
6754static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6755 const PointInfo start,const PointInfo end)
6756{
6757 if (TracePoint(primitive_info,start) == MagickFalse)
6758 return(MagickFalse);
6759 if (TracePoint(primitive_info+1,end) == MagickFalse)
6760 return(MagickFalse);
6761 (primitive_info+1)->primitive=primitive_info->primitive;
6762 primitive_info->coordinates=2;
6763 primitive_info->closed_subpath=MagickFalse;
6764 return(MagickTrue);
6765}
6766
6767static ssize_t TracePath(MVGInfo *mvg_info,const char *path,
6768 ExceptionInfo *exception)
6769{
6770 char
6771 *next_token,
6772 token[MagickPathExtent] = "";
6773
6774 const char
6775 *p;
6776
6777 double
6778 x,
6779 y;
6780
6781 int
6782 attribute,
6783 last_attribute;
6784
6785 MagickBooleanType
6786 status;
6787
6788 PointInfo
6789 end = {0.0, 0.0},
6790 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6791 point = {0.0, 0.0},
6792 start = {0.0, 0.0};
6793
6794 PrimitiveInfo
6795 *primitive_info;
6796
6797 PrimitiveType
6798 primitive_type;
6799
6800 PrimitiveInfo
6801 *q;
6802
6803 ssize_t
6804 i;
6805
6806 size_t
6807 number_coordinates,
6808 z_count;
6809
6810 ssize_t
6811 subpath_offset;
6812
6813 subpath_offset=mvg_info->offset;
6814 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6815 status=MagickTrue;
6816 attribute=0;
6817 number_coordinates=0;
6818 z_count=0;
6819 primitive_type=primitive_info->primitive;
6820 q=primitive_info;
6821 for (p=path; *p != '\0'; )
6822 {
6823 if (status == MagickFalse)
6824 break;
6825 while (isspace((int) ((unsigned char) *p)) != 0)
6826 p++;
6827 if (*p == '\0')
6828 break;
6829 last_attribute=attribute;
6830 attribute=(int) (*p++);
6831 switch (attribute)
6832 {
6833 case 'a':
6834 case 'A':
6835 {
6836 double
6837 angle = 0.0;
6838
6839 MagickBooleanType
6840 large_arc = MagickFalse,
6841 sweep = MagickFalse;
6842
6843 PointInfo
6844 arc = {0.0, 0.0};
6845
6846 /*
6847 Elliptical arc.
6848 */
6849 do
6850 {
6851 (void) GetNextToken(p,&p,MagickPathExtent,token);
6852 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6853 ThrowPointExpectedException(token,exception);
6854 if (*token == ',')
6855 (void) GetNextToken(p,&p,MagickPathExtent,token);
6856 arc.x=GetDrawValue(token,&next_token);
6857 if (token == next_token)
6858 ThrowPointExpectedException(token,exception);
6859 (void) GetNextToken(p,&p,MagickPathExtent,token);
6860 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6861 ThrowPointExpectedException(token,exception);
6862 if (*token == ',')
6863 (void) GetNextToken(p,&p,MagickPathExtent,token);
6864 arc.y=GetDrawValue(token,&next_token);
6865 if (token == next_token)
6866 ThrowPointExpectedException(token,exception);
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 angle=GetDrawValue(token,&next_token);
6873 if (token == next_token)
6874 ThrowPointExpectedException(token,exception);
6875 (void) GetNextToken(p,&p,MagickPathExtent,token);
6876 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6877 ThrowPointExpectedException(token,exception);
6878 if (*token == ',')
6879 (void) GetNextToken(p,&p,MagickPathExtent,token);
6880 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6881 (void) GetNextToken(p,&p,MagickPathExtent,token);
6882 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6883 ThrowPointExpectedException(token,exception);
6884 if (*token == ',')
6885 (void) GetNextToken(p,&p,MagickPathExtent,token);
6886 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6887 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6888 ThrowPointExpectedException(token,exception);
6889 if (*token == ',')
6890 (void) GetNextToken(p,&p,MagickPathExtent,token);
6891 (void) GetNextToken(p,&p,MagickPathExtent,token);
6892 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6893 ThrowPointExpectedException(token,exception);
6894 if (*token == ',')
6895 (void) GetNextToken(p,&p,MagickPathExtent,token);
6896 x=GetDrawValue(token,&next_token);
6897 if (token == next_token)
6898 ThrowPointExpectedException(token,exception);
6899 (void) GetNextToken(p,&p,MagickPathExtent,token);
6900 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6901 ThrowPointExpectedException(token,exception);
6902 if (*token == ',')
6903 (void) GetNextToken(p,&p,MagickPathExtent,token);
6904 y=GetDrawValue(token,&next_token);
6905 if (token == next_token)
6906 ThrowPointExpectedException(token,exception);
6907 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
6908 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
6909 if (TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep) == MagickFalse)
6910 return(-1);
6911 q=(*mvg_info->primitive_info)+mvg_info->offset;
6912 mvg_info->offset+=(ssize_t) q->coordinates;
6913 q+=(ptrdiff_t) q->coordinates;
6914 point=end;
6915 while (isspace((int) ((unsigned char) *p)) != 0)
6916 p++;
6917 if (*p == ',')
6918 p++;
6919 } while (IsValidPoint(p) != MagickFalse);
6920 break;
6921 }
6922 case 'c':
6923 case 'C':
6924 {
6925 /*
6926 Cubic Bézier curve.
6927 */
6928 do
6929 {
6930 points[0]=point;
6931 for (i=1; i < 4; i++)
6932 {
6933 (void) GetNextToken(p,&p,MagickPathExtent,token);
6934 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6935 ThrowPointExpectedException(token,exception);
6936 if (*token == ',')
6937 (void) GetNextToken(p,&p,MagickPathExtent,token);
6938 x=GetDrawValue(token,&next_token);
6939 if (token == next_token)
6940 ThrowPointExpectedException(token,exception);
6941 (void) GetNextToken(p,&p,MagickPathExtent,token);
6942 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6943 ThrowPointExpectedException(token,exception);
6944 if (*token == ',')
6945 (void) GetNextToken(p,&p,MagickPathExtent,token);
6946 y=GetDrawValue(token,&next_token);
6947 if (token == next_token)
6948 ThrowPointExpectedException(token,exception);
6949 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
6950 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
6951 points[i]=end;
6952 }
6953 for (i=0; i < 4; i++)
6954 (q+i)->point=points[i];
6955 if (TraceBezier(mvg_info,4) == MagickFalse)
6956 return(-1);
6957 q=(*mvg_info->primitive_info)+mvg_info->offset;
6958 mvg_info->offset+=(ssize_t) q->coordinates;
6959 q+=(ptrdiff_t) q->coordinates;
6960 point=end;
6961 while (isspace((int) ((unsigned char) *p)) != 0)
6962 p++;
6963 if (*p == ',')
6964 p++;
6965 } while (IsValidPoint(p) != MagickFalse);
6966 break;
6967 }
6968 case 'H':
6969 case 'h':
6970 {
6971 do
6972 {
6973 (void) GetNextToken(p,&p,MagickPathExtent,token);
6974 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
6975 ThrowPointExpectedException(token,exception);
6976 if (*token == ',')
6977 (void) GetNextToken(p,&p,MagickPathExtent,token);
6978 x=GetDrawValue(token,&next_token);
6979 if (token == next_token)
6980 ThrowPointExpectedException(token,exception);
6981 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
6982 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6983 return(-1);
6984 q=(*mvg_info->primitive_info)+mvg_info->offset;
6985 if (TracePoint(q,point) == MagickFalse)
6986 return(-1);
6987 mvg_info->offset+=(ssize_t) q->coordinates;
6988 q+=(ptrdiff_t) q->coordinates;
6989 while (isspace((int) ((unsigned char) *p)) != 0)
6990 p++;
6991 if (*p == ',')
6992 p++;
6993 } while (IsValidPoint(p) != MagickFalse);
6994 break;
6995 }
6996 case 'l':
6997 case 'L':
6998 {
6999 /*
7000 Line to.
7001 */
7002 do
7003 {
7004 (void) GetNextToken(p,&p,MagickPathExtent,token);
7005 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7006 ThrowPointExpectedException(token,exception);
7007 if (*token == ',')
7008 (void) GetNextToken(p,&p,MagickPathExtent,token);
7009 x=GetDrawValue(token,&next_token);
7010 if (token == next_token)
7011 ThrowPointExpectedException(token,exception);
7012 (void) GetNextToken(p,&p,MagickPathExtent,token);
7013 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7014 ThrowPointExpectedException(token,exception);
7015 if (*token == ',')
7016 (void) GetNextToken(p,&p,MagickPathExtent,token);
7017 y=GetDrawValue(token,&next_token);
7018 if (token == next_token)
7019 ThrowPointExpectedException(token,exception);
7020 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
7021 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
7022 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7023 return(-1);
7024 q=(*mvg_info->primitive_info)+mvg_info->offset;
7025 if (TracePoint(q,point) == MagickFalse)
7026 return(-1);
7027 mvg_info->offset+=(ssize_t) q->coordinates;
7028 q+=(ptrdiff_t) q->coordinates;
7029 while (isspace((int) ((unsigned char) *p)) != 0)
7030 p++;
7031 if (*p == ',')
7032 p++;
7033 } while (IsValidPoint(p) != MagickFalse);
7034 break;
7035 }
7036 case 'M':
7037 case 'm':
7038 {
7039 /*
7040 Move to.
7041 */
7042 if (mvg_info->offset != subpath_offset)
7043 {
7044 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7045 primitive_info->coordinates=(size_t) (q-primitive_info);
7046 number_coordinates+=primitive_info->coordinates;
7047 primitive_info=q;
7048 subpath_offset=mvg_info->offset;
7049 }
7050 i=0;
7051 do
7052 {
7053 (void) GetNextToken(p,&p,MagickPathExtent,token);
7054 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7055 ThrowPointExpectedException(token,exception);
7056 if (*token == ',')
7057 (void) GetNextToken(p,&p,MagickPathExtent,token);
7058 x=GetDrawValue(token,&next_token);
7059 if (token == next_token)
7060 ThrowPointExpectedException(token,exception);
7061 (void) GetNextToken(p,&p,MagickPathExtent,token);
7062 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7063 ThrowPointExpectedException(token,exception);
7064 if (*token == ',')
7065 (void) GetNextToken(p,&p,MagickPathExtent,token);
7066 y=GetDrawValue(token,&next_token);
7067 if (token == next_token)
7068 ThrowPointExpectedException(token,exception);
7069 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
7070 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
7071 if (i == 0)
7072 start=point;
7073 i++;
7074 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7075 return(-1);
7076 q=(*mvg_info->primitive_info)+mvg_info->offset;
7077 if (TracePoint(q,point) == MagickFalse)
7078 return(-1);
7079 mvg_info->offset+=(ssize_t) q->coordinates;
7080 q+=(ptrdiff_t) q->coordinates;
7081 while (isspace((int) ((unsigned char) *p)) != 0)
7082 p++;
7083 if (*p == ',')
7084 p++;
7085 } while (IsValidPoint(p) != MagickFalse);
7086 break;
7087 }
7088 case 'q':
7089 case 'Q':
7090 {
7091 /*
7092 Quadratic Bézier curve.
7093 */
7094 do
7095 {
7096 points[0]=point;
7097 for (i=1; i < 3; i++)
7098 {
7099 (void) GetNextToken(p,&p,MagickPathExtent,token);
7100 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7101 ThrowPointExpectedException(token,exception);
7102 if (*token == ',')
7103 (void) GetNextToken(p,&p,MagickPathExtent,token);
7104 x=GetDrawValue(token,&next_token);
7105 if (token == next_token)
7106 ThrowPointExpectedException(token,exception);
7107 (void) GetNextToken(p,&p,MagickPathExtent,token);
7108 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7109 ThrowPointExpectedException(token,exception);
7110 if (*token == ',')
7111 (void) GetNextToken(p,&p,MagickPathExtent,token);
7112 y=GetDrawValue(token,&next_token);
7113 if (token == next_token)
7114 ThrowPointExpectedException(token,exception);
7115 if (*p == ',')
7116 p++;
7117 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
7118 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
7119 points[i]=end;
7120 }
7121 for (i=0; i < 3; i++)
7122 (q+i)->point=points[i];
7123 if (TraceBezier(mvg_info,3) == MagickFalse)
7124 return(-1);
7125 q=(*mvg_info->primitive_info)+mvg_info->offset;
7126 mvg_info->offset+=(ssize_t) q->coordinates;
7127 q+=(ptrdiff_t) q->coordinates;
7128 point=end;
7129 while (isspace((int) ((unsigned char) *p)) != 0)
7130 p++;
7131 if (*p == ',')
7132 p++;
7133 } while (IsValidPoint(p) != MagickFalse);
7134 break;
7135 }
7136 case 's':
7137 case 'S':
7138 {
7139 /*
7140 Cubic Bézier curve.
7141 */
7142 do
7143 {
7144 points[0]=points[3];
7145 points[1].x=2.0*points[3].x-points[2].x;
7146 points[1].y=2.0*points[3].y-points[2].y;
7147 for (i=2; i < 4; i++)
7148 {
7149 (void) GetNextToken(p,&p,MagickPathExtent,token);
7150 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7151 ThrowPointExpectedException(token,exception);
7152 if (*token == ',')
7153 (void) GetNextToken(p,&p,MagickPathExtent,token);
7154 x=GetDrawValue(token,&next_token);
7155 if (token == next_token)
7156 ThrowPointExpectedException(token,exception);
7157 (void) GetNextToken(p,&p,MagickPathExtent,token);
7158 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7159 ThrowPointExpectedException(token,exception);
7160 if (*token == ',')
7161 (void) GetNextToken(p,&p,MagickPathExtent,token);
7162 y=GetDrawValue(token,&next_token);
7163 if (token == next_token)
7164 ThrowPointExpectedException(token,exception);
7165 if (*p == ',')
7166 p++;
7167 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
7168 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
7169 points[i]=end;
7170 }
7171 if (strchr("CcSs",last_attribute) == (char *) NULL)
7172 {
7173 points[0]=point;
7174 points[1]=point;
7175 }
7176 for (i=0; i < 4; i++)
7177 (q+i)->point=points[i];
7178 if (TraceBezier(mvg_info,4) == MagickFalse)
7179 return(-1);
7180 q=(*mvg_info->primitive_info)+mvg_info->offset;
7181 mvg_info->offset+=(ssize_t) q->coordinates;
7182 q+=(ptrdiff_t) q->coordinates;
7183 point=end;
7184 last_attribute=attribute;
7185 while (isspace((int) ((unsigned char) *p)) != 0)
7186 p++;
7187 if (*p == ',')
7188 p++;
7189 } while (IsValidPoint(p) != MagickFalse);
7190 break;
7191 }
7192 case 't':
7193 case 'T':
7194 {
7195 /*
7196 Quadratic Bézier curve.
7197 */
7198 do
7199 {
7200 points[0]=points[2];
7201 points[1].x=2.0*points[2].x-points[1].x;
7202 points[1].y=2.0*points[2].y-points[1].y;
7203 for (i=2; i < 3; i++)
7204 {
7205 (void) GetNextToken(p,&p,MagickPathExtent,token);
7206 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7207 ThrowPointExpectedException(token,exception);
7208 if (*token == ',')
7209 (void) GetNextToken(p,&p,MagickPathExtent,token);
7210 x=GetDrawValue(token,&next_token);
7211 if (token == next_token)
7212 ThrowPointExpectedException(token,exception);
7213 (void) GetNextToken(p,&p,MagickPathExtent,token);
7214 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7215 ThrowPointExpectedException(token,exception);
7216 if (*token == ',')
7217 (void) GetNextToken(p,&p,MagickPathExtent,token);
7218 y=GetDrawValue(token,&next_token);
7219 if (token == next_token)
7220 ThrowPointExpectedException(token,exception);
7221 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
7222 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
7223 points[i]=end;
7224 }
7225 if (status == MagickFalse)
7226 break;
7227 if (strchr("QqTt",last_attribute) == (char *) NULL)
7228 {
7229 points[0]=point;
7230 points[1]=point;
7231 }
7232 for (i=0; i < 3; i++)
7233 (q+i)->point=points[i];
7234 if (TraceBezier(mvg_info,3) == MagickFalse)
7235 return(-1);
7236 q=(*mvg_info->primitive_info)+mvg_info->offset;
7237 mvg_info->offset+=(ssize_t) q->coordinates;
7238 q+=(ptrdiff_t) q->coordinates;
7239 point=end;
7240 last_attribute=attribute;
7241 while (isspace((int) ((unsigned char) *p)) != 0)
7242 p++;
7243 if (*p == ',')
7244 p++;
7245 } while (IsValidPoint(p) != MagickFalse);
7246 break;
7247 }
7248 case 'v':
7249 case 'V':
7250 {
7251 /*
7252 Line to.
7253 */
7254 do
7255 {
7256 (void) GetNextToken(p,&p,MagickPathExtent,token);
7257 if (IsValidListChar((int) ((unsigned char) *token)) == MagickFalse)
7258 ThrowPointExpectedException(token,exception);
7259 if (*token == ',')
7260 (void) GetNextToken(p,&p,MagickPathExtent,token);
7261 y=GetDrawValue(token,&next_token);
7262 if (token == next_token)
7263 ThrowPointExpectedException(token,exception);
7264 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
7265 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7266 return(-1);
7267 q=(*mvg_info->primitive_info)+mvg_info->offset;
7268 if (TracePoint(q,point) == MagickFalse)
7269 return(-1);
7270 mvg_info->offset+=(ssize_t) q->coordinates;
7271 q+=(ptrdiff_t) q->coordinates;
7272 while (isspace((int) ((unsigned char) *p)) != 0)
7273 p++;
7274 if (*p == ',')
7275 p++;
7276 } while (IsValidPoint(p) != MagickFalse);
7277 break;
7278 }
7279 case 'z':
7280 case 'Z':
7281 {
7282 /*
7283 Close path.
7284 */
7285 point=start;
7286 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7287 return(-1);
7288 q=(*mvg_info->primitive_info)+mvg_info->offset;
7289 if (TracePoint(q,point) == MagickFalse)
7290 return(-1);
7291 mvg_info->offset+=(ssize_t) q->coordinates;
7292 q+=(ptrdiff_t) q->coordinates;
7293 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7294 primitive_info->coordinates=(size_t) (q-primitive_info);
7295 primitive_info->closed_subpath=MagickTrue;
7296 number_coordinates+=primitive_info->coordinates;
7297 primitive_info=q;
7298 subpath_offset=mvg_info->offset;
7299 z_count++;
7300 break;
7301 }
7302 default:
7303 {
7304 ThrowPointExpectedException(token,exception);
7305 break;
7306 }
7307 }
7308 }
7309 if (status == MagickFalse)
7310 return(-1);
7311 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
7312 primitive_info->coordinates=(size_t) (q-primitive_info);
7313 number_coordinates+=primitive_info->coordinates;
7314 for (i=0; i < (ssize_t) number_coordinates; i++)
7315 {
7316 q--;
7317 q->primitive=primitive_type;
7318 if (z_count > 1)
7319 q->method=FillToBorderMethod;
7320 }
7321 q=primitive_info;
7322 return((ssize_t) number_coordinates);
7323}
7324
7325static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7326 const PointInfo start,const PointInfo end)
7327{
7328 PointInfo
7329 point;
7330
7331 PrimitiveInfo
7332 *p;
7333
7334 ssize_t
7335 i;
7336
7337 p=primitive_info;
7338 if (TracePoint(p,start) == MagickFalse)
7339 return(MagickFalse);
7340 p+=(ptrdiff_t) p->coordinates;
7341 point.x=start.x;
7342 point.y=end.y;
7343 if (TracePoint(p,point) == MagickFalse)
7344 return(MagickFalse);
7345 p+=(ptrdiff_t) p->coordinates;
7346 if (TracePoint(p,end) == MagickFalse)
7347 return(MagickFalse);
7348 p+=(ptrdiff_t) p->coordinates;
7349 point.x=end.x;
7350 point.y=start.y;
7351 if (TracePoint(p,point) == MagickFalse)
7352 return(MagickFalse);
7353 p+=(ptrdiff_t) p->coordinates;
7354 if (TracePoint(p,start) == MagickFalse)
7355 return(MagickFalse);
7356 p+=(ptrdiff_t) p->coordinates;
7357 primitive_info->coordinates=(size_t) (p-primitive_info);
7358 primitive_info->closed_subpath=MagickTrue;
7359 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7360 {
7361 p->primitive=primitive_info->primitive;
7362 p--;
7363 }
7364 return(MagickTrue);
7365}
7366
7367static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7368 const PointInfo start,const PointInfo end,PointInfo arc)
7369{
7370 PointInfo
7371 degrees,
7372 point,
7373 segment;
7374
7375 PrimitiveInfo
7376 *primitive_info;
7377
7378 PrimitiveInfo
7379 *p;
7380
7381 ssize_t
7382 i;
7383
7384 ssize_t
7385 offset;
7386
7387 offset=mvg_info->offset;
7388 segment.x=fabs(end.x-start.x);
7389 segment.y=fabs(end.y-start.y);
7390 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7391 {
7392 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7393 return(MagickTrue);
7394 }
7395 if (arc.x > (0.5*segment.x))
7396 arc.x=0.5*segment.x;
7397 if (arc.y > (0.5*segment.y))
7398 arc.y=0.5*segment.y;
7399 point.x=start.x+segment.x-arc.x;
7400 point.y=start.y+arc.y;
7401 degrees.x=270.0;
7402 degrees.y=360.0;
7403 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7404 return(MagickFalse);
7405 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7406 mvg_info->offset+=(ssize_t) p->coordinates;
7407 point.x=start.x+segment.x-arc.x;
7408 point.y=start.y+segment.y-arc.y;
7409 degrees.x=0.0;
7410 degrees.y=90.0;
7411 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7412 return(MagickFalse);
7413 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7414 mvg_info->offset+=(ssize_t) p->coordinates;
7415 point.x=start.x+arc.x;
7416 point.y=start.y+segment.y-arc.y;
7417 degrees.x=90.0;
7418 degrees.y=180.0;
7419 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7420 return(MagickFalse);
7421 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7422 mvg_info->offset+=(ssize_t) p->coordinates;
7423 point.x=start.x+arc.x;
7424 point.y=start.y+arc.y;
7425 degrees.x=180.0;
7426 degrees.y=270.0;
7427 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7428 return(MagickFalse);
7429 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7430 mvg_info->offset+=(ssize_t) p->coordinates;
7431 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7432 return(MagickFalse);
7433 p=(*mvg_info->primitive_info)+(ptrdiff_t) mvg_info->offset;
7434 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7435 return(MagickFalse);
7436 p+=(ptrdiff_t) p->coordinates;
7437 mvg_info->offset=offset;
7438 primitive_info=(*mvg_info->primitive_info)+offset;
7439 primitive_info->coordinates=(size_t) (p-primitive_info);
7440 primitive_info->closed_subpath=MagickTrue;
7441 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7442 {
7443 p->primitive=primitive_info->primitive;
7444 p--;
7445 }
7446 return(MagickTrue);
7447}
7448
7449static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7450 const size_t number_vertices,const double offset)
7451{
7452 double
7453 distance;
7454
7455 double
7456 dx,
7457 dy;
7458
7459 ssize_t
7460 i;
7461
7462 ssize_t
7463 j;
7464
7465 dx=0.0;
7466 dy=0.0;
7467 for (i=1; i < (ssize_t) number_vertices; i++)
7468 {
7469 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7470 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7471 if ((fabs((double) dx) >= MagickEpsilon) ||
7472 (fabs((double) dy) >= MagickEpsilon))
7473 break;
7474 }
7475 if (i == (ssize_t) number_vertices)
7476 i=(ssize_t) number_vertices-1L;
7477 distance=hypot((double) dx,(double) dy);
7478 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7479 dx*(distance+offset)/distance);
7480 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7481 dy*(distance+offset)/distance);
7482 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7483 {
7484 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7485 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7486 if ((fabs((double) dx) >= MagickEpsilon) ||
7487 (fabs((double) dy) >= MagickEpsilon))
7488 break;
7489 }
7490 distance=hypot((double) dx,(double) dy);
7491 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7492 dx*(distance+offset)/distance);
7493 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7494 dy*(distance+offset)/distance);
7495 return(MagickTrue);
7496}
7497
7498static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
7499 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7500{
7501#define MaxStrokePad (6*BezierQuantum+360)
7502#define CheckPathExtent(pad_p,pad_q) \
7503{ \
7504 if ((pad_p) > MaxBezierCoordinates) \
7505 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7506 else \
7507 if ((p+(ptrdiff_t) (pad_p)) >= (ssize_t) extent_p) \
7508 { \
7509 if (~extent_p < (pad_p)) \
7510 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7511 else \
7512 { \
7513 extent_p+=(pad_p); \
7514 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7515 MaxStrokePad,sizeof(*stroke_p)); \
7516 } \
7517 } \
7518 if ((pad_q) > MaxBezierCoordinates) \
7519 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7520 else \
7521 if ((q+(ptrdiff_t) (pad_q)) >= (ssize_t) extent_q) \
7522 { \
7523 if (~extent_q < (pad_q)) \
7524 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7525 else \
7526 { \
7527 extent_q+=(pad_q); \
7528 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7529 MaxStrokePad,sizeof(*stroke_q)); \
7530 } \
7531 } \
7532 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7533 { \
7534 if (stroke_p != (PointInfo *) NULL) \
7535 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7536 if (stroke_q != (PointInfo *) NULL) \
7537 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7538 polygon_primitive=(PrimitiveInfo *) \
7539 RelinquishMagickMemory(polygon_primitive); \
7540 (void) ThrowMagickException(exception,GetMagickModule(), \
7541 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7542 return((PrimitiveInfo *) NULL); \
7543 } \
7544}
7545
7546 typedef struct _StrokeSegment
7547 {
7548 double
7549 p,
7550 q;
7551 } StrokeSegment;
7552
7553 double
7554 delta_theta,
7555 dot_product,
7556 mid,
7557 miterlimit;
7558
7559 MagickBooleanType
7560 closed_path;
7561
7562 PointInfo
7563 box_p[5],
7564 box_q[5],
7565 center,
7566 offset,
7567 *stroke_p,
7568 *stroke_q;
7569
7570 PrimitiveInfo
7571 *polygon_primitive,
7572 *stroke_polygon;
7573
7574 ssize_t
7575 i;
7576
7577 size_t
7578 arc_segments,
7579 extent_p,
7580 extent_q,
7581 number_vertices;
7582
7583 ssize_t
7584 j,
7585 n,
7586 p,
7587 q;
7588
7589 StrokeSegment
7590 dx = {0.0, 0.0},
7591 dy = {0.0, 0.0},
7592 inverse_slope = {0.0, 0.0},
7593 slope = {0.0, 0.0},
7594 theta = {0.0, 0.0};
7595
7596 /*
7597 Allocate paths.
7598 */
7599 number_vertices=primitive_info->coordinates;
7600 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7601 number_vertices+2UL,sizeof(*polygon_primitive));
7602 if (polygon_primitive == (PrimitiveInfo *) NULL)
7603 {
7604 (void) ThrowMagickException(exception,GetMagickModule(),
7605 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7606 return((PrimitiveInfo *) NULL);
7607 }
7608 (void) memcpy(polygon_primitive,primitive_info,(size_t) number_vertices*
7609 sizeof(*polygon_primitive));
7610 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7611 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7612 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7613 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7614 if ((draw_info->linejoin == RoundJoin) ||
7615 ((draw_info->linejoin == MiterJoin) && (closed_path != MagickFalse)))
7616 {
7617 polygon_primitive[number_vertices]=primitive_info[1];
7618 number_vertices++;
7619 }
7620 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7621 /*
7622 Compute the slope for the first line segment, p.
7623 */
7624 closed_path=primitive_info[0].closed_subpath;
7625 dx.p=0.0;
7626 dy.p=0.0;
7627 for (n=1; n < (ssize_t) number_vertices; n++)
7628 {
7629 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7630 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7631 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7632 break;
7633 }
7634 if (n == (ssize_t) number_vertices)
7635 {
7636 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7637 {
7638 /*
7639 Zero length subpath.
7640 */
7641 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7642 sizeof(*stroke_polygon));
7643 stroke_polygon[0]=polygon_primitive[0];
7644 stroke_polygon[0].coordinates=0;
7645 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7646 polygon_primitive);
7647 return(stroke_polygon);
7648 }
7649 n=(ssize_t) number_vertices-1L;
7650 }
7651 extent_p=2*number_vertices;
7652 extent_q=2*number_vertices;
7653 stroke_p=(PointInfo *) AcquireQuantumMemory((size_t) extent_p+MaxStrokePad,
7654 sizeof(*stroke_p));
7655 stroke_q=(PointInfo *) AcquireQuantumMemory((size_t) extent_q+MaxStrokePad,
7656 sizeof(*stroke_q));
7657 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7658 {
7659 if (stroke_p != (PointInfo *) NULL)
7660 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7661 if (stroke_q != (PointInfo *) NULL)
7662 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7663 polygon_primitive=(PrimitiveInfo *)
7664 RelinquishMagickMemory(polygon_primitive);
7665 (void) ThrowMagickException(exception,GetMagickModule(),
7666 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7667 return((PrimitiveInfo *) NULL);
7668 }
7669 slope.p=0.0;
7670 inverse_slope.p=0.0;
7671 if (fabs(dx.p) < MagickEpsilon)
7672 {
7673 if (dx.p >= 0.0)
7674 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7675 else
7676 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7677 }
7678 else
7679 if (fabs(dy.p) < MagickEpsilon)
7680 {
7681 if (dy.p >= 0.0)
7682 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7683 else
7684 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7685 }
7686 else
7687 {
7688 slope.p=dy.p/dx.p;
7689 inverse_slope.p=(-1.0*MagickSafeReciprocal(slope.p));
7690 }
7691 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7692 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7693 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7694 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7695 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7696 offset.y=(double) (offset.x*inverse_slope.p);
7697 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7698 {
7699 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7700 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7701 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7702 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7703 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7704 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7705 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7706 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7707 }
7708 else
7709 {
7710 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7711 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7712 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7713 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7714 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7715 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7716 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7717 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7718 }
7719 /*
7720 Create strokes for the line join attribute: bevel, miter, round.
7721 */
7722 p=0;
7723 q=0;
7724 stroke_q[p++]=box_q[0];
7725 stroke_p[q++]=box_p[0];
7726 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7727 {
7728 /*
7729 Compute the slope for this line segment, q.
7730 */
7731 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7732 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7733 dot_product=dx.q*dx.q+dy.q*dy.q;
7734 if (dot_product < 0.25)
7735 continue;
7736 slope.q=0.0;
7737 inverse_slope.q=0.0;
7738 if (fabs(dx.q) < MagickEpsilon)
7739 {
7740 if (dx.q >= 0.0)
7741 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7742 else
7743 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7744 }
7745 else
7746 if (fabs(dy.q) < MagickEpsilon)
7747 {
7748 if (dy.q >= 0.0)
7749 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7750 else
7751 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7752 }
7753 else
7754 {
7755 slope.q=dy.q/dx.q;
7756 inverse_slope.q=(-1.0*MagickSafeReciprocal(slope.q));
7757 }
7758 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7759 offset.y=(double) (offset.x*inverse_slope.q);
7760 dot_product=dy.q*offset.x-dx.q*offset.y;
7761 if (dot_product > 0.0)
7762 {
7763 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7764 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7765 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7766 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7767 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7768 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7769 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7770 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7771 }
7772 else
7773 {
7774 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7775 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7776 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7777 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7778 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7779 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7780 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7781 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7782 }
7783 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
7784 {
7785 box_p[4]=box_p[1];
7786 box_q[4]=box_q[1];
7787 }
7788 else
7789 {
7790 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7791 box_p[3].y)/(slope.p-slope.q));
7792 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7793 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7794 box_q[3].y)/(slope.p-slope.q));
7795 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7796 }
7797 DisableMSCWarning(4127)
7798 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7799 RestoreMSCWarning
7800 dot_product=dx.q*dy.p-dx.p*dy.q;
7801 if (dot_product <= 0.0)
7802 switch (draw_info->linejoin)
7803 {
7804 case BevelJoin:
7805 {
7806 stroke_q[q++]=box_q[1];
7807 stroke_q[q++]=box_q[2];
7808 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7809 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7810 if (dot_product <= miterlimit)
7811 stroke_p[p++]=box_p[4];
7812 else
7813 {
7814 stroke_p[p++]=box_p[1];
7815 stroke_p[p++]=box_p[2];
7816 }
7817 break;
7818 }
7819 case MiterJoin:
7820 {
7821 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7822 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7823 if (dot_product <= miterlimit)
7824 {
7825 stroke_q[q++]=box_q[4];
7826 stroke_p[p++]=box_p[4];
7827 }
7828 else
7829 {
7830 stroke_q[q++]=box_q[1];
7831 stroke_q[q++]=box_q[2];
7832 stroke_p[p++]=box_p[1];
7833 stroke_p[p++]=box_p[2];
7834 }
7835 break;
7836 }
7837 case RoundJoin:
7838 {
7839 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7840 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7841 if (dot_product <= miterlimit)
7842 stroke_p[p++]=box_p[4];
7843 else
7844 {
7845 stroke_p[p++]=box_p[1];
7846 stroke_p[p++]=box_p[2];
7847 }
7848 center=polygon_primitive[n].point;
7849 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7850 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7851 if (theta.q < theta.p)
7852 theta.q+=2.0*MagickPI;
7853 arc_segments=(size_t) CastDoubleToSsizeT(ceil((double) ((theta.q-
7854 theta.p)/(2.0*sqrt(MagickSafeReciprocal(mid))))));
7855 DisableMSCWarning(4127)
7856 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7857 RestoreMSCWarning
7858 stroke_q[q].x=box_q[1].x;
7859 stroke_q[q].y=box_q[1].y;
7860 q++;
7861 for (j=1; j < (ssize_t) arc_segments; j++)
7862 {
7863 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7864 stroke_q[q].x=(double) (center.x+mid*cos(fmod((double)
7865 (theta.p+delta_theta),DegreesToRadians(360.0))));
7866 stroke_q[q].y=(double) (center.y+mid*sin(fmod((double)
7867 (theta.p+delta_theta),DegreesToRadians(360.0))));
7868 q++;
7869 }
7870 stroke_q[q++]=box_q[2];
7871 break;
7872 }
7873 default:
7874 break;
7875 }
7876 else
7877 switch (draw_info->linejoin)
7878 {
7879 case BevelJoin:
7880 {
7881 stroke_p[p++]=box_p[1];
7882 stroke_p[p++]=box_p[2];
7883 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7884 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7885 if (dot_product <= miterlimit)
7886 stroke_q[q++]=box_q[4];
7887 else
7888 {
7889 stroke_q[q++]=box_q[1];
7890 stroke_q[q++]=box_q[2];
7891 }
7892 break;
7893 }
7894 case MiterJoin:
7895 {
7896 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7897 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7898 if (dot_product <= miterlimit)
7899 {
7900 stroke_q[q++]=box_q[4];
7901 stroke_p[p++]=box_p[4];
7902 }
7903 else
7904 {
7905 stroke_q[q++]=box_q[1];
7906 stroke_q[q++]=box_q[2];
7907 stroke_p[p++]=box_p[1];
7908 stroke_p[p++]=box_p[2];
7909 }
7910 break;
7911 }
7912 case RoundJoin:
7913 {
7914 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7915 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7916 if (dot_product <= miterlimit)
7917 stroke_q[q++]=box_q[4];
7918 else
7919 {
7920 stroke_q[q++]=box_q[1];
7921 stroke_q[q++]=box_q[2];
7922 }
7923 center=polygon_primitive[n].point;
7924 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7925 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7926 if (theta.p < theta.q)
7927 theta.p+=2.0*MagickPI;
7928 arc_segments=(size_t) CastDoubleToSsizeT(ceil((double) ((theta.p-
7929 theta.q)/(2.0*sqrt((double) (MagickSafeReciprocal(mid)))))));
7930 DisableMSCWarning(4127)
7931 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7932 RestoreMSCWarning
7933 stroke_p[p++]=box_p[1];
7934 for (j=1; j < (ssize_t) arc_segments; j++)
7935 {
7936 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7937 stroke_p[p].x=(double) (center.x+mid*cos(fmod((double)
7938 (theta.p+delta_theta),DegreesToRadians(360.0))));
7939 stroke_p[p].y=(double) (center.y+mid*sin(fmod((double)
7940 (theta.p+delta_theta),DegreesToRadians(360.0))));
7941 p++;
7942 }
7943 stroke_p[p++]=box_p[2];
7944 break;
7945 }
7946 default:
7947 break;
7948 }
7949 slope.p=slope.q;
7950 inverse_slope.p=inverse_slope.q;
7951 box_p[0]=box_p[2];
7952 box_p[1]=box_p[3];
7953 box_q[0]=box_q[2];
7954 box_q[1]=box_q[3];
7955 dx.p=dx.q;
7956 dy.p=dy.q;
7957 n=i;
7958 }
7959 stroke_p[p++]=box_p[1];
7960 stroke_q[q++]=box_q[1];
7961 /*
7962 Trace stroked polygon.
7963 */
7964 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7965 (p+q+2L),(size_t) (closed_path+2L)*sizeof(*stroke_polygon));
7966 if (stroke_polygon == (PrimitiveInfo *) NULL)
7967 {
7968 (void) ThrowMagickException(exception,GetMagickModule(),
7969 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7970 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7971 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7972 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7973 polygon_primitive);
7974 return(stroke_polygon);
7975 }
7976 for (i=0; i < (ssize_t) p; i++)
7977 {
7978 stroke_polygon[i]=polygon_primitive[0];
7979 stroke_polygon[i].point=stroke_p[i];
7980 }
7981 if (closed_path != MagickFalse)
7982 {
7983 stroke_polygon[i]=polygon_primitive[0];
7984 stroke_polygon[i].point=stroke_polygon[0].point;
7985 i++;
7986 }
7987 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7988 {
7989 stroke_polygon[i]=polygon_primitive[0];
7990 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7991 }
7992 if (closed_path != MagickFalse)
7993 {
7994 stroke_polygon[i]=polygon_primitive[0];
7995 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7996 i++;
7997 }
7998 stroke_polygon[i]=polygon_primitive[0];
7999 stroke_polygon[i].point=stroke_polygon[0].point;
8000 i++;
8001 stroke_polygon[i].primitive=UndefinedPrimitive;
8002 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
8003 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
8004 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
8005 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
8006 return(stroke_polygon);
8007}