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