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