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