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