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