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