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