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