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