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