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