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