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