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