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