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