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