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,"mvg") != 0) &&
5572 (LocaleCompare(clone_info->magick,"vid") != 0))
5573 composite_images=ReadImage(clone_info,exception);
5574 else
5575 (void) ThrowMagickException(exception,GetMagickModule(),
5576 FileOpenError,"UnableToOpenFile","`%s'",clone_info->filename);
5577 }
5578 clone_info=DestroyImageInfo(clone_info);
5579 if (composite_images == (Image *) NULL)
5580 {
5581 status=0;
5582 break;
5583 }
5584 composite_image=RemoveFirstImageFromList(&composite_images);
5585 composite_images=DestroyImageList(composite_images);
5586 (void) SetImageProgressMonitor(composite_image,(MagickProgressMonitor)
5587 NULL,(void *) NULL);
5588 x1=CastDoubleToLong(ceil(primitive_info[1].point.x-0.5));
5589 y1=CastDoubleToLong(ceil(primitive_info[1].point.y-0.5));
5590 if (((x1 != 0L) && (x1 != (ssize_t) composite_image->columns)) ||
5591 ((y1 != 0L) && (y1 != (ssize_t) composite_image->rows)))
5592 {
5593 char
5594 geometry[MaxTextExtent];
5595
5596 /*
5597 Resize image.
5598 */
5599 (void) FormatLocaleString(geometry,MaxTextExtent,"%gx%g!",
5600 primitive_info[1].point.x,primitive_info[1].point.y);
5601 composite_image->filter=image->filter;
5602 status&=TransformImage(&composite_image,(char *) NULL,geometry);
5603 }
5604 if (composite_image->matte == MagickFalse)
5605 status&=SetImageAlphaChannel(composite_image,OpaqueAlphaChannel);
5606 if (draw_info->opacity != OpaqueOpacity)
5607 status&=SetImageOpacity(composite_image,draw_info->opacity);
5608 SetGeometry(image,&geometry);
5609 image->gravity=draw_info->gravity;
5610 geometry.x=x;
5611 geometry.y=y;
5612 (void) FormatLocaleString(composite_geometry,MaxTextExtent,
5613 "%.20gx%.20g%+.20g%+.20g",(double) composite_image->columns,(double)
5614 composite_image->rows,(double) geometry.x,(double) geometry.y);
5615 (void) ParseGravityGeometry(image,composite_geometry,&geometry,
5616 &image->exception);
5617 affine=draw_info->affine;
5618 affine.tx=(double) geometry.x;
5619 affine.ty=(double) geometry.y;
5620 composite_image->interpolate=image->interpolate;
5621 if ((draw_info->compose == OverCompositeOp) ||
5622 (draw_info->compose == SrcOverCompositeOp))
5623 status&=DrawAffineImage(image,composite_image,&affine);
5624 else
5625 status&=CompositeImage(image,draw_info->compose,composite_image,
5626 geometry.x,geometry.y);
5627 composite_image=DestroyImage(composite_image);
5628 break;
5629 }
5630 case PointPrimitive:
5631 {
5633 fill_color;
5634
5636 *q;
5637
5638 if ((y < 0) || (y >= (ssize_t) image->rows))
5639 break;
5640 if ((x < 0) || (x >= (ssize_t) image->columns))
5641 break;
5642 q=GetCacheViewAuthenticPixels(image_view,x,y,1,1,exception);
5643 if (q == (PixelPacket *) NULL)
5644 break;
5645 (void) GetFillColor(draw_info,x,y,&fill_color);
5646 MagickCompositeOver(&fill_color,(MagickRealType) fill_color.opacity,q,
5647 (MagickRealType) q->opacity,q);
5648 status&=SyncCacheViewAuthenticPixels(image_view,exception);
5649 break;
5650 }
5651 case TextPrimitive:
5652 {
5653 char
5654 geometry[MaxTextExtent];
5655
5656 DrawInfo
5657 *clone_info;
5658
5659 if (primitive_info->text == (char *) NULL)
5660 break;
5661 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5662 (void) CloneString(&clone_info->text,primitive_info->text);
5663 (void) FormatLocaleString(geometry,MaxTextExtent,"%+f%+f",
5664 primitive_info->point.x,primitive_info->point.y);
5665 (void) CloneString(&clone_info->geometry,geometry);
5666 status&=AnnotateImage(image,clone_info);
5667 clone_info=DestroyDrawInfo(clone_info);
5668 break;
5669 }
5670 default:
5671 {
5672 double
5673 mid,
5674 scale;
5675
5676 DrawInfo
5677 *clone_info;
5678
5679 if (IsEventLogging() != MagickFalse)
5680 LogPrimitiveInfo(primitive_info);
5681 scale=ExpandAffine(&draw_info->affine);
5682 if ((draw_info->dash_pattern != (double *) NULL) &&
5683 (fabs(draw_info->dash_pattern[0]) >= MagickEpsilon) &&
5684 (fabs(scale*draw_info->stroke_width) >= MagickEpsilon) &&
5685 (draw_info->stroke.opacity != (Quantum) TransparentOpacity))
5686 {
5687 /*
5688 Draw dash polygon.
5689 */
5690 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5691 clone_info->stroke_width=0.0;
5692 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5693 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5694 clone_info=DestroyDrawInfo(clone_info);
5695 if (status != MagickFalse)
5696 status&=DrawDashPolygon(draw_info,primitive_info,image);
5697 break;
5698 }
5699 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
5700 if ((mid > 1.0) &&
5701 ((draw_info->stroke.opacity != (Quantum) TransparentOpacity) ||
5702 (draw_info->stroke_pattern != (Image *) NULL)))
5703 {
5704 double
5705 x,
5706 y;
5707
5708 MagickBooleanType
5709 closed_path;
5710
5711 /*
5712 Draw strokes while respecting line cap/join attributes.
5713 */
5714 closed_path=primitive_info[0].closed_subpath;
5715 i=(ssize_t) primitive_info[0].coordinates;
5716 x=fabs(primitive_info[i-1].point.x-primitive_info[0].point.x);
5717 y=fabs(primitive_info[i-1].point.y-primitive_info[0].point.y);
5718 if ((x < MagickEpsilon) && (y < MagickEpsilon))
5719 closed_path=MagickTrue;
5720 if ((((draw_info->linecap == RoundCap) ||
5721 (closed_path != MagickFalse)) &&
5722 (draw_info->linejoin == RoundJoin)) ||
5723 (primitive_info[i].primitive != UndefinedPrimitive))
5724 {
5725 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5726 break;
5727 }
5728 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5729 clone_info->stroke_width=0.0;
5730 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5731 status&=DrawPolygonPrimitive(image,clone_info,primitive_info);
5732 clone_info=DestroyDrawInfo(clone_info);
5733 if (status != MagickFalse)
5734 status&=DrawStrokePolygon(image,draw_info,primitive_info);
5735 break;
5736 }
5737 status&=DrawPolygonPrimitive(image,draw_info,primitive_info);
5738 break;
5739 }
5740 }
5741 image_view=DestroyCacheView(image_view);
5742 if (draw_info->compliance == SVGCompliance)
5743 {
5744 status&=SetImageClipMask(image,(Image *) NULL);
5745 status&=SetImageMask(image,(Image *) NULL);
5746 }
5747 if (draw_info->debug != MagickFalse)
5748 (void) LogMagickEvent(DrawEvent,GetMagickModule()," end draw-primitive");
5749 return(status != 0 ? MagickTrue : MagickFalse);
5750}
5751
5752/*
5753%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5754% %
5755% %
5756% %
5757+ D r a w S t r o k e P o l y g o n %
5758% %
5759% %
5760% %
5761%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5762%
5763% DrawStrokePolygon() draws a stroked polygon (line, rectangle, ellipse) on
5764% the image while respecting the line cap and join attributes.
5765%
5766% The format of the DrawStrokePolygon method is:
5767%
5768% MagickBooleanType DrawStrokePolygon(Image *image,
5769% const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5770%
5771% A description of each parameter follows:
5772%
5773% o image: the image.
5774%
5775% o draw_info: the draw info.
5776%
5777% o primitive_info: Specifies a pointer to a PrimitiveInfo structure.
5778%
5779%
5780*/
5781
5782static MagickBooleanType DrawRoundLinecap(Image *image,
5783 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5784{
5786 linecap[5];
5787
5788 ssize_t
5789 i;
5790
5791 for (i=0; i < 4; i++)
5792 linecap[i]=(*primitive_info);
5793 linecap[0].coordinates=4;
5794 linecap[1].point.x+=2.0*MagickEpsilon;
5795 linecap[2].point.x+=2.0*MagickEpsilon;
5796 linecap[2].point.y+=2.0*MagickEpsilon;
5797 linecap[3].point.y+=2.0*MagickEpsilon;
5798 linecap[4].primitive=UndefinedPrimitive;
5799 return(DrawPolygonPrimitive(image,draw_info,linecap));
5800}
5801
5802static MagickBooleanType DrawStrokePolygon(Image *image,
5803 const DrawInfo *draw_info,const PrimitiveInfo *primitive_info)
5804{
5805 DrawInfo
5806 *clone_info;
5807
5808 MagickBooleanType
5809 closed_path;
5810
5811 MagickStatusType
5812 status;
5813
5815 *stroke_polygon;
5816
5817 const PrimitiveInfo
5818 *p,
5819 *q;
5820
5821 /*
5822 Draw stroked polygon.
5823 */
5824 if (draw_info->debug != MagickFalse)
5825 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5826 " begin draw-stroke-polygon");
5827 clone_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
5828 clone_info->fill=draw_info->stroke;
5829 if (clone_info->fill_pattern != (Image *) NULL)
5830 clone_info->fill_pattern=DestroyImage(clone_info->fill_pattern);
5831 if (clone_info->stroke_pattern != (Image *) NULL)
5832 clone_info->fill_pattern=CloneImage(clone_info->stroke_pattern,0,0,
5833 MagickTrue,&clone_info->stroke_pattern->exception);
5834 clone_info->stroke.opacity=(Quantum) TransparentOpacity;
5835 clone_info->stroke_width=0.0;
5836 clone_info->fill_rule=NonZeroRule;
5837 status=MagickTrue;
5838 for (p=primitive_info; p->primitive != UndefinedPrimitive; p+=p->coordinates)
5839 {
5840 if (p->coordinates == 1)
5841 continue;
5842 stroke_polygon=TraceStrokePolygon(draw_info,p,&image->exception);
5843 if (stroke_polygon == (PrimitiveInfo *) NULL)
5844 {
5845 status=0;
5846 break;
5847 }
5848 status&=DrawPolygonPrimitive(image,clone_info,stroke_polygon);
5849 stroke_polygon=(PrimitiveInfo *) RelinquishMagickMemory(stroke_polygon);
5850 if (status == 0)
5851 break;
5852 q=p+p->coordinates-1;
5853 closed_path=p->closed_subpath;
5854 if ((draw_info->linecap == RoundCap) && (closed_path == MagickFalse))
5855 {
5856 status&=DrawRoundLinecap(image,draw_info,p);
5857 status&=DrawRoundLinecap(image,draw_info,q);
5858 }
5859 }
5860 clone_info=DestroyDrawInfo(clone_info);
5861 if (draw_info->debug != MagickFalse)
5862 (void) LogMagickEvent(DrawEvent,GetMagickModule(),
5863 " end draw-stroke-polygon");
5864 return(status != 0 ? MagickTrue : MagickFalse);
5865}
5866
5867/*
5868%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5869% %
5870% %
5871% %
5872% G e t A f f i n e M a t r i x %
5873% %
5874% %
5875% %
5876%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5877%
5878% GetAffineMatrix() returns an AffineMatrix initialized to the identity
5879% matrix.
5880%
5881% The format of the GetAffineMatrix method is:
5882%
5883% void GetAffineMatrix(AffineMatrix *affine_matrix)
5884%
5885% A description of each parameter follows:
5886%
5887% o affine_matrix: the affine matrix.
5888%
5889*/
5890MagickExport void GetAffineMatrix(AffineMatrix *affine_matrix)
5891{
5892 assert(affine_matrix != (AffineMatrix *) NULL);
5893 if (IsEventLogging() != MagickFalse)
5894 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5895 (void) memset(affine_matrix,0,sizeof(*affine_matrix));
5896 affine_matrix->sx=1.0;
5897 affine_matrix->sy=1.0;
5898}
5899
5900/*
5901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5902% %
5903% %
5904% %
5905+ G e t D r a w I n f o %
5906% %
5907% %
5908% %
5909%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5910%
5911% GetDrawInfo() initializes draw_info to default values from image_info.
5912%
5913% The format of the GetDrawInfo method is:
5914%
5915% void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5916%
5917% A description of each parameter follows:
5918%
5919% o image_info: the image info..
5920%
5921% o draw_info: the draw info.
5922%
5923*/
5924MagickExport void GetDrawInfo(const ImageInfo *image_info,DrawInfo *draw_info)
5925{
5926 char
5927 *next_token;
5928
5929 const char
5930 *option;
5931
5933 *exception;
5934
5935 /*
5936 Initialize draw attributes.
5937 */
5938 assert(draw_info != (DrawInfo *) NULL);
5939 if (IsEventLogging() != MagickFalse)
5940 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
5941 (void) memset(draw_info,0,sizeof(*draw_info));
5942 draw_info->image_info=CloneImageInfo(image_info);
5943 GetAffineMatrix(&draw_info->affine);
5944 exception=AcquireExceptionInfo();
5945 (void) QueryColorDatabase("#000F",&draw_info->fill,exception);
5946 (void) QueryColorDatabase("#FFF0",&draw_info->stroke,exception);
5947 draw_info->stroke_antialias=draw_info->image_info->antialias;
5948 draw_info->stroke_width=1.0;
5949 draw_info->fill_rule=EvenOddRule;
5950 draw_info->opacity=OpaqueOpacity;
5951 draw_info->fill_opacity=OpaqueOpacity;
5952 draw_info->stroke_opacity=OpaqueOpacity;
5953 draw_info->linecap=ButtCap;
5954 draw_info->linejoin=MiterJoin;
5955 draw_info->miterlimit=10;
5956 draw_info->decorate=NoDecoration;
5957 if (draw_info->image_info->font != (char *) NULL)
5958 draw_info->font=AcquireString(draw_info->image_info->font);
5959 if (draw_info->image_info->density != (char *) NULL)
5960 draw_info->density=AcquireString(draw_info->image_info->density);
5961 draw_info->text_antialias=draw_info->image_info->antialias;
5962 draw_info->pointsize=12.0;
5963 if (fabs(draw_info->image_info->pointsize) >= MagickEpsilon)
5964 draw_info->pointsize=draw_info->image_info->pointsize;
5965 draw_info->undercolor.opacity=(Quantum) TransparentOpacity;
5966 draw_info->border_color=draw_info->image_info->border_color;
5967 draw_info->compose=OverCompositeOp;
5968 if (draw_info->image_info->server_name != (char *) NULL)
5969 draw_info->server_name=AcquireString(draw_info->image_info->server_name);
5970 draw_info->render=MagickTrue;
5971 draw_info->clip_path=MagickFalse;
5972 draw_info->debug=(GetLogEventMask() & (DrawEvent | AnnotateEvent)) != 0 ?
5973 MagickTrue : MagickFalse;
5974 option=GetImageOption(draw_info->image_info,"direction");
5975 if (option != (const char *) NULL)
5976 draw_info->direction=(DirectionType) ParseCommandOption(
5977 MagickDirectionOptions,MagickFalse,option);
5978 else
5979 draw_info->direction=UndefinedDirection;
5980 option=GetImageOption(draw_info->image_info,"encoding");
5981 if (option != (const char *) NULL)
5982 (void) CloneString(&draw_info->encoding,option);
5983 option=GetImageOption(draw_info->image_info,"family");
5984 if (option != (const char *) NULL)
5985 (void) CloneString(&draw_info->family,option);
5986 option=GetImageOption(draw_info->image_info,"fill");
5987 if (option != (const char *) NULL)
5988 (void) QueryColorDatabase(option,&draw_info->fill,exception);
5989 option=GetImageOption(draw_info->image_info,"gravity");
5990 if (option != (const char *) NULL)
5991 draw_info->gravity=(GravityType) ParseCommandOption(MagickGravityOptions,
5992 MagickFalse,option);
5993 option=GetImageOption(draw_info->image_info,"interline-spacing");
5994 if (option != (const char *) NULL)
5995 draw_info->interline_spacing=GetDrawValue(option,&next_token);
5996 option=GetImageOption(draw_info->image_info,"interword-spacing");
5997 if (option != (const char *) NULL)
5998 draw_info->interword_spacing=GetDrawValue(option,&next_token);
5999 option=GetImageOption(draw_info->image_info,"kerning");
6000 if (option != (const char *) NULL)
6001 draw_info->kerning=GetDrawValue(option,&next_token);
6002 option=GetImageOption(draw_info->image_info,"stroke");
6003 if (option != (const char *) NULL)
6004 (void) QueryColorDatabase(option,&draw_info->stroke,exception);
6005 option=GetImageOption(draw_info->image_info,"strokewidth");
6006 if (option != (const char *) NULL)
6007 draw_info->stroke_width=GetDrawValue(option,&next_token);
6008 option=GetImageOption(draw_info->image_info,"style");
6009 if (option != (const char *) NULL)
6010 draw_info->style=(StyleType) ParseCommandOption(MagickStyleOptions,
6011 MagickFalse,option);
6012 option=GetImageOption(draw_info->image_info,"undercolor");
6013 if (option != (const char *) NULL)
6014 (void) QueryColorDatabase(option,&draw_info->undercolor,exception);
6015 option=GetImageOption(draw_info->image_info,"weight");
6016 if (option != (const char *) NULL)
6017 {
6018 ssize_t
6019 weight;
6020
6021 weight=ParseCommandOption(MagickWeightOptions,MagickFalse,option);
6022 if (weight == -1)
6023 weight=(ssize_t) StringToUnsignedLong(option);
6024 draw_info->weight=(size_t) weight;
6025 }
6026 exception=DestroyExceptionInfo(exception);
6027 draw_info->signature=MagickCoreSignature;
6028}
6029
6030/*
6031%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6032% %
6033% %
6034% %
6035+ P e r m u t a t e %
6036% %
6037% %
6038% %
6039%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6040%
6041% Permutate() returns the permutation of the (n,k).
6042%
6043% The format of the Permutate method is:
6044%
6045% void Permutate(ssize_t n,ssize_t k)
6046%
6047% A description of each parameter follows:
6048%
6049% o n:
6050%
6051% o k:
6052%
6053%
6054*/
6055static inline double Permutate(const ssize_t n,const ssize_t k)
6056{
6057 double
6058 r;
6059
6060 ssize_t
6061 i;
6062
6063 r=1.0;
6064 for (i=k+1; i <= n; i++)
6065 r*=i;
6066 for (i=1; i <= (n-k); i++)
6067 r/=i;
6068 return(r);
6069}
6070
6071/*
6072%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6073% %
6074% %
6075% %
6076+ T r a c e P r i m i t i v e %
6077% %
6078% %
6079% %
6080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6081%
6082% TracePrimitive is a collection of methods for generating graphic
6083% primitives such as arcs, ellipses, paths, etc.
6084%
6085*/
6086
6087static MagickBooleanType TraceArc(MVGInfo *mvg_info,const PointInfo start,
6088 const PointInfo end,const PointInfo degrees)
6089{
6090 PointInfo
6091 center,
6092 radius;
6093
6094 center.x=0.5*(end.x+start.x);
6095 center.y=0.5*(end.y+start.y);
6096 radius.x=fabs(center.x-start.x);
6097 radius.y=fabs(center.y-start.y);
6098 return(TraceEllipse(mvg_info,center,radius,degrees));
6099}
6100
6101static MagickBooleanType TraceArcPath(MVGInfo *mvg_info,const PointInfo start,
6102 const PointInfo end,const PointInfo arc,const double angle,
6103 const MagickBooleanType large_arc,const MagickBooleanType sweep)
6104{
6105 double
6106 alpha,
6107 beta,
6108 delta,
6109 factor,
6110 gamma,
6111 theta;
6112
6113 MagickStatusType
6114 status;
6115
6116 PointInfo
6117 center,
6118 points[3],
6119 radii;
6120
6121 double
6122 cosine,
6123 sine;
6124
6126 *primitive_info;
6127
6129 *p;
6130
6131 ssize_t
6132 i;
6133
6134 size_t
6135 arc_segments;
6136
6137 ssize_t
6138 offset;
6139
6140 offset=mvg_info->offset;
6141 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6142 primitive_info->coordinates=0;
6143 if ((fabs(start.x-end.x) < MagickEpsilon) &&
6144 (fabs(start.y-end.y) < MagickEpsilon))
6145 return(TracePoint(primitive_info,end));
6146 radii.x=fabs(arc.x);
6147 radii.y=fabs(arc.y);
6148 if ((radii.x < MagickEpsilon) || (radii.y < MagickEpsilon))
6149 return(TraceLine(primitive_info,start,end));
6150 cosine=cos(DegreesToRadians(fmod((double) angle,360.0)));
6151 sine=sin(DegreesToRadians(fmod((double) angle,360.0)));
6152 center.x=(double) (cosine*(end.x-start.x)/2+sine*(end.y-start.y)/2);
6153 center.y=(double) (cosine*(end.y-start.y)/2-sine*(end.x-start.x)/2);
6154 delta=(center.x*center.x)/(radii.x*radii.x)+(center.y*center.y)/
6155 (radii.y*radii.y);
6156 if (delta < MagickEpsilon)
6157 return(TraceLine(primitive_info,start,end));
6158 if (delta > 1.0)
6159 {
6160 radii.x*=sqrt((double) delta);
6161 radii.y*=sqrt((double) delta);
6162 }
6163 points[0].x=(double) (cosine*start.x/radii.x+sine*start.y/radii.x);
6164 points[0].y=(double) (cosine*start.y/radii.y-sine*start.x/radii.y);
6165 points[1].x=(double) (cosine*end.x/radii.x+sine*end.y/radii.x);
6166 points[1].y=(double) (cosine*end.y/radii.y-sine*end.x/radii.y);
6167 alpha=points[1].x-points[0].x;
6168 beta=points[1].y-points[0].y;
6169 if (fabs(alpha*alpha+beta*beta) < MagickEpsilon)
6170 return(TraceLine(primitive_info,start,end));
6171 factor=PerceptibleReciprocal(alpha*alpha+beta*beta)-0.25;
6172 if (factor <= 0.0)
6173 factor=0.0;
6174 else
6175 {
6176 factor=sqrt((double) factor);
6177 if (sweep == large_arc)
6178 factor=(-factor);
6179 }
6180 center.x=(double) ((points[0].x+points[1].x)/2-factor*beta);
6181 center.y=(double) ((points[0].y+points[1].y)/2+factor*alpha);
6182 alpha=atan2(points[0].y-center.y,points[0].x-center.x);
6183 theta=atan2(points[1].y-center.y,points[1].x-center.x)-alpha;
6184 if ((theta < 0.0) && (sweep != MagickFalse))
6185 theta+=2.0*MagickPI;
6186 else
6187 if ((theta > 0.0) && (sweep == MagickFalse))
6188 theta-=2.0*MagickPI;
6189 arc_segments=(size_t) CastDoubleToLong(ceil(fabs((double) (theta/(0.5*
6190 MagickPI+MagickEpsilon)))));
6191 p=primitive_info;
6192 status=MagickTrue;
6193 for (i=0; i < (ssize_t) arc_segments; i++)
6194 {
6195 beta=0.5*((alpha+(i+1)*theta/arc_segments)-(alpha+i*theta/arc_segments));
6196 gamma=(8.0/3.0)*sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))*
6197 sin(fmod((double) (0.5*beta),DegreesToRadians(360.0)))/
6198 sin(fmod((double) beta,DegreesToRadians(360.0)));
6199 points[0].x=(double) (center.x+cos(fmod((double) (alpha+(double) i*theta/
6200 arc_segments),DegreesToRadians(360.0)))-gamma*sin(fmod((double) (alpha+
6201 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6202 points[0].y=(double) (center.y+sin(fmod((double) (alpha+(double) i*theta/
6203 arc_segments),DegreesToRadians(360.0)))+gamma*cos(fmod((double) (alpha+
6204 (double) i*theta/arc_segments),DegreesToRadians(360.0))));
6205 points[2].x=(double) (center.x+cos(fmod((double) (alpha+(double) (i+1)*
6206 theta/arc_segments),DegreesToRadians(360.0))));
6207 points[2].y=(double) (center.y+sin(fmod((double) (alpha+(double) (i+1)*
6208 theta/arc_segments),DegreesToRadians(360.0))));
6209 points[1].x=(double) (points[2].x+gamma*sin(fmod((double) (alpha+(double)
6210 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6211 points[1].y=(double) (points[2].y-gamma*cos(fmod((double) (alpha+(double)
6212 (i+1)*theta/arc_segments),DegreesToRadians(360.0))));
6213 p->point.x=(p == primitive_info) ? start.x : (p-1)->point.x;
6214 p->point.y=(p == primitive_info) ? start.y : (p-1)->point.y;
6215 (p+1)->point.x=(double) (cosine*radii.x*points[0].x-sine*radii.y*
6216 points[0].y);
6217 (p+1)->point.y=(double) (sine*radii.x*points[0].x+cosine*radii.y*
6218 points[0].y);
6219 (p+2)->point.x=(double) (cosine*radii.x*points[1].x-sine*radii.y*
6220 points[1].y);
6221 (p+2)->point.y=(double) (sine*radii.x*points[1].x+cosine*radii.y*
6222 points[1].y);
6223 (p+3)->point.x=(double) (cosine*radii.x*points[2].x-sine*radii.y*
6224 points[2].y);
6225 (p+3)->point.y=(double) (sine*radii.x*points[2].x+cosine*radii.y*
6226 points[2].y);
6227 if (i == (ssize_t) (arc_segments-1))
6228 (p+3)->point=end;
6229 status&=TraceBezier(mvg_info,4);
6230 if (status == 0)
6231 break;
6232 p=(*mvg_info->primitive_info)+mvg_info->offset;
6233 mvg_info->offset+=p->coordinates;
6234 p+=p->coordinates;
6235 }
6236 if (status == 0)
6237 return(MagickFalse);
6238 mvg_info->offset=offset;
6239 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6240 primitive_info->coordinates=(size_t) (p-primitive_info);
6241 primitive_info->closed_subpath=MagickFalse;
6242 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6243 {
6244 p->primitive=primitive_info->primitive;
6245 p--;
6246 }
6247 return(MagickTrue);
6248}
6249
6250static MagickBooleanType TraceBezier(MVGInfo *mvg_info,
6251 const size_t number_coordinates)
6252{
6253 double
6254 alpha,
6255 *coefficients,
6256 weight;
6257
6258 PointInfo
6259 end,
6260 point,
6261 *points;
6262
6264 *primitive_info;
6265
6267 *p;
6268
6269 ssize_t
6270 i,
6271 j;
6272
6273 size_t
6274 control_points,
6275 quantum;
6276
6277 /*
6278 Allocate coefficients.
6279 */
6280 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6281 quantum=number_coordinates;
6282 for (i=0; i < (ssize_t) number_coordinates; i++)
6283 {
6284 for (j=i+1; j < (ssize_t) number_coordinates; j++)
6285 {
6286 alpha=fabs(primitive_info[j].point.x-primitive_info[i].point.x);
6287 if (alpha > (double) MAGICK_SSIZE_MAX)
6288 {
6289 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6290 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6291 return(MagickFalse);
6292 }
6293 if (alpha > (double) quantum)
6294 quantum=(size_t) alpha;
6295 alpha=fabs(primitive_info[j].point.y-primitive_info[i].point.y);
6296 if (alpha > (double) MAGICK_SSIZE_MAX)
6297 {
6298 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6299 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6300 return(MagickFalse);
6301 }
6302 if (alpha > (double) quantum)
6303 quantum=(size_t) alpha;
6304 }
6305 }
6306 coefficients=(double *) AcquireQuantumMemory(number_coordinates,
6307 sizeof(*coefficients));
6308 quantum=MagickMin(quantum/number_coordinates,BezierQuantum);
6309 points=(PointInfo *) AcquireQuantumMemory(quantum,number_coordinates*
6310 sizeof(*points));
6311 if ((coefficients == (double *) NULL) || (points == (PointInfo *) NULL))
6312 {
6313 if (points != (PointInfo *) NULL)
6314 points=(PointInfo *) RelinquishMagickMemory(points);
6315 if (coefficients != (double *) NULL)
6316 coefficients=(double *) RelinquishMagickMemory(coefficients);
6317 (void) ThrowMagickException(mvg_info->exception,GetMagickModule(),
6318 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
6319 return(MagickFalse);
6320 }
6321 control_points=quantum*number_coordinates;
6322 if (CheckPrimitiveExtent(mvg_info,(double) control_points+1) == MagickFalse)
6323 {
6324 points=(PointInfo *) RelinquishMagickMemory(points);
6325 coefficients=(double *) RelinquishMagickMemory(coefficients);
6326 return(MagickFalse);
6327 }
6328 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6329 /*
6330 Compute bezier points.
6331 */
6332 end=primitive_info[number_coordinates-1].point;
6333 for (i=0; i < (ssize_t) number_coordinates; i++)
6334 coefficients[i]=Permutate((ssize_t) number_coordinates-1,i);
6335 weight=0.0;
6336 for (i=0; i < (ssize_t) control_points; i++)
6337 {
6338 p=primitive_info;
6339 point.x=0.0;
6340 point.y=0.0;
6341 alpha=pow((double) (1.0-weight),(double) number_coordinates-1.0);
6342 for (j=0; j < (ssize_t) number_coordinates; j++)
6343 {
6344 point.x+=alpha*coefficients[j]*p->point.x;
6345 point.y+=alpha*coefficients[j]*p->point.y;
6346 alpha*=weight/(1.0-weight);
6347 p++;
6348 }
6349 points[i]=point;
6350 weight+=1.0/control_points;
6351 }
6352 /*
6353 Bezier curves are just short segmented polys.
6354 */
6355 p=primitive_info;
6356 for (i=0; i < (ssize_t) control_points; i++)
6357 {
6358 if (TracePoint(p,points[i]) == MagickFalse)
6359 {
6360 points=(PointInfo *) RelinquishMagickMemory(points);
6361 coefficients=(double *) RelinquishMagickMemory(coefficients);
6362 return(MagickFalse);
6363 }
6364 p+=p->coordinates;
6365 }
6366 if (TracePoint(p,end) == MagickFalse)
6367 {
6368 points=(PointInfo *) RelinquishMagickMemory(points);
6369 coefficients=(double *) RelinquishMagickMemory(coefficients);
6370 return(MagickFalse);
6371 }
6372 p+=p->coordinates;
6373 primitive_info->coordinates=(size_t) (p-primitive_info);
6374 primitive_info->closed_subpath=MagickFalse;
6375 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6376 {
6377 p->primitive=primitive_info->primitive;
6378 p--;
6379 }
6380 points=(PointInfo *) RelinquishMagickMemory(points);
6381 coefficients=(double *) RelinquishMagickMemory(coefficients);
6382 return(MagickTrue);
6383}
6384
6385static MagickBooleanType TraceCircle(MVGInfo *mvg_info,const PointInfo start,
6386 const PointInfo end)
6387{
6388 double
6389 alpha,
6390 beta,
6391 radius;
6392
6393 PointInfo
6394 offset,
6395 degrees;
6396
6397 alpha=end.x-start.x;
6398 beta=end.y-start.y;
6399 radius=hypot((double) alpha,(double) beta);
6400 offset.x=(double) radius;
6401 offset.y=(double) radius;
6402 degrees.x=0.0;
6403 degrees.y=360.0;
6404 return(TraceEllipse(mvg_info,start,offset,degrees));
6405}
6406
6407static MagickBooleanType TraceEllipse(MVGInfo *mvg_info,const PointInfo center,
6408 const PointInfo radii,const PointInfo arc)
6409{
6410 double
6411 coordinates,
6412 delta,
6413 step,
6414 x,
6415 y;
6416
6417 PointInfo
6418 angle,
6419 point;
6420
6422 *primitive_info;
6423
6425 *p;
6426
6427 ssize_t
6428 i;
6429
6430 /*
6431 Ellipses are just short segmented polys.
6432 */
6433 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6434 primitive_info->coordinates=0;
6435 if ((fabs(radii.x) < MagickEpsilon) || (fabs(radii.y) < MagickEpsilon))
6436 return(MagickTrue);
6437 delta=2.0*PerceptibleReciprocal(MagickMax(radii.x,radii.y));
6438 step=MagickPI/8.0;
6439 if ((delta >= 0.0) && (delta < (MagickPI/8.0)))
6440 step=MagickPI/4.0/(MagickPI*PerceptibleReciprocal(delta)/2.0);
6441 angle.x=DegreesToRadians(arc.x);
6442 y=arc.y;
6443 while (y < arc.x)
6444 y+=360.0;
6445 angle.y=DegreesToRadians(y);
6446 coordinates=ceil((angle.y-angle.x)/step+1.0);
6447 if (CheckPrimitiveExtent(mvg_info,coordinates) == MagickFalse)
6448 return(MagickFalse);
6449 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6450 for (p=primitive_info; angle.x < angle.y; angle.x+=step)
6451 {
6452 point.x=cos(fmod(angle.x,DegreesToRadians(360.0)))*radii.x+center.x;
6453 point.y=sin(fmod(angle.x,DegreesToRadians(360.0)))*radii.y+center.y;
6454 if (TracePoint(p,point) == MagickFalse)
6455 return(MagickFalse);
6456 p+=p->coordinates;
6457 }
6458 point.x=cos(fmod(angle.y,DegreesToRadians(360.0)))*radii.x+center.x;
6459 point.y=sin(fmod(angle.y,DegreesToRadians(360.0)))*radii.y+center.y;
6460 if (TracePoint(p,point) == MagickFalse)
6461 return(MagickFalse);
6462 p+=p->coordinates;
6463 primitive_info->coordinates=(size_t) (p-primitive_info);
6464 primitive_info->closed_subpath=MagickFalse;
6465 x=fabs(primitive_info[0].point.x-
6466 primitive_info[primitive_info->coordinates-1].point.x);
6467 y=fabs(primitive_info[0].point.y-
6468 primitive_info[primitive_info->coordinates-1].point.y);
6469 if ((x < MagickEpsilon) && (y < MagickEpsilon))
6470 primitive_info->closed_subpath=MagickTrue;
6471 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
6472 {
6473 p->primitive=primitive_info->primitive;
6474 p--;
6475 }
6476 return(MagickTrue);
6477}
6478
6479static MagickBooleanType TraceLine(PrimitiveInfo *primitive_info,
6480 const PointInfo start,const PointInfo end)
6481{
6482 if (TracePoint(primitive_info,start) == MagickFalse)
6483 return(MagickFalse);
6484 if (TracePoint(primitive_info+1,end) == MagickFalse)
6485 return(MagickFalse);
6486 (primitive_info+1)->primitive=primitive_info->primitive;
6487 primitive_info->coordinates=2;
6488 primitive_info->closed_subpath=MagickFalse;
6489 return(MagickTrue);
6490}
6491
6492static ssize_t TracePath(Image *image,MVGInfo *mvg_info,const char *path)
6493{
6494 char
6495 *next_token,
6496 token[MaxTextExtent] = "";
6497
6498 const char
6499 *p;
6500
6501 double
6502 x,
6503 y;
6504
6505 int
6506 attribute,
6507 last_attribute;
6508
6509 MagickStatusType
6510 status;
6511
6512 PointInfo
6513 end = {0.0, 0.0},
6514 points[4] = { {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0} },
6515 point = {0.0, 0.0},
6516 start = {0.0, 0.0};
6517
6519 *primitive_info;
6520
6521 PrimitiveType
6522 primitive_type;
6523
6525 *q;
6526
6527 ssize_t
6528 i;
6529
6530 size_t
6531 number_coordinates,
6532 z_count;
6533
6534 ssize_t
6535 subpath_offset;
6536
6537 subpath_offset=mvg_info->offset;
6538 primitive_info=(*mvg_info->primitive_info)+mvg_info->offset;
6539 status=MagickTrue;
6540 attribute=0;
6541 number_coordinates=0;
6542 z_count=0;
6543 *token='\0';
6544 primitive_type=primitive_info->primitive;
6545 q=primitive_info;
6546 for (p=path; *p != '\0'; )
6547 {
6548 if (status == MagickFalse)
6549 break;
6550 while (isspace((int) ((unsigned char) *p)) != 0)
6551 p++;
6552 if (*p == '\0')
6553 break;
6554 last_attribute=attribute;
6555 attribute=(int) (*p++);
6556 switch (attribute)
6557 {
6558 case 'a':
6559 case 'A':
6560 {
6561 double
6562 angle = 0.0;
6563
6564 MagickBooleanType
6565 large_arc = MagickFalse,
6566 sweep = MagickFalse;
6567
6568 PointInfo
6569 arc = {0.0, 0.0};
6570
6571 /*
6572 Elliptical arc.
6573 */
6574 do
6575 {
6576 (void) GetNextToken(p,&p,MaxTextExtent,token);
6577 if (*token == ',')
6578 (void) GetNextToken(p,&p,MaxTextExtent,token);
6579 arc.x=GetDrawValue(token,&next_token);
6580 if (token == next_token)
6581 ThrowPointExpectedException(image,token);
6582 (void) GetNextToken(p,&p,MaxTextExtent,token);
6583 if (*token == ',')
6584 (void) GetNextToken(p,&p,MaxTextExtent,token);
6585 arc.y=GetDrawValue(token,&next_token);
6586 if (token == next_token)
6587 ThrowPointExpectedException(image,token);
6588 (void) GetNextToken(p,&p,MaxTextExtent,token);
6589 if (*token == ',')
6590 (void) GetNextToken(p,&p,MaxTextExtent,token);
6591 angle=GetDrawValue(token,&next_token);
6592 if (token == next_token)
6593 ThrowPointExpectedException(image,token);
6594 (void) GetNextToken(p,&p,MaxTextExtent,token);
6595 if (*token == ',')
6596 (void) GetNextToken(p,&p,MaxTextExtent,token);
6597 large_arc=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6598 (void) GetNextToken(p,&p,MaxTextExtent,token);
6599 if (*token == ',')
6600 (void) GetNextToken(p,&p,MaxTextExtent,token);
6601 sweep=StringToLong(token) != 0 ? MagickTrue : MagickFalse;
6602 if (*token == ',')
6603 (void) GetNextToken(p,&p,MaxTextExtent,token);
6604 (void) GetNextToken(p,&p,MaxTextExtent,token);
6605 if (*token == ',')
6606 (void) GetNextToken(p,&p,MaxTextExtent,token);
6607 x=GetDrawValue(token,&next_token);
6608 if (token == next_token)
6609 ThrowPointExpectedException(image,token);
6610 (void) GetNextToken(p,&p,MaxTextExtent,token);
6611 if (*token == ',')
6612 (void) GetNextToken(p,&p,MaxTextExtent,token);
6613 y=GetDrawValue(token,&next_token);
6614 if (token == next_token)
6615 ThrowPointExpectedException(image,token);
6616 end.x=(double) (attribute == (int) 'A' ? x : point.x+x);
6617 end.y=(double) (attribute == (int) 'A' ? y : point.y+y);
6618 status&=TraceArcPath(mvg_info,point,end,arc,angle,large_arc,sweep);
6619 q=(*mvg_info->primitive_info)+mvg_info->offset;
6620 mvg_info->offset+=q->coordinates;
6621 q+=q->coordinates;
6622 point=end;
6623 while (isspace((int) ((unsigned char) *p)) != 0)
6624 p++;
6625 if (*p == ',')
6626 p++;
6627 } while (IsPoint(p) != MagickFalse);
6628 break;
6629 }
6630 case 'c':
6631 case 'C':
6632 {
6633 /*
6634 Cubic Bézier curve.
6635 */
6636 do
6637 {
6638 points[0]=point;
6639 for (i=1; i < 4; i++)
6640 {
6641 (void) GetNextToken(p,&p,MaxTextExtent,token);
6642 if (*token == ',')
6643 (void) GetNextToken(p,&p,MaxTextExtent,token);
6644 x=GetDrawValue(token,&next_token);
6645 if (token == next_token)
6646 ThrowPointExpectedException(image,token);
6647 (void) GetNextToken(p,&p,MaxTextExtent,token);
6648 if (*token == ',')
6649 (void) GetNextToken(p,&p,MaxTextExtent,token);
6650 y=GetDrawValue(token,&next_token);
6651 if (token == next_token)
6652 ThrowPointExpectedException(image,token);
6653 end.x=(double) (attribute == (int) 'C' ? x : point.x+x);
6654 end.y=(double) (attribute == (int) 'C' ? y : point.y+y);
6655 points[i]=end;
6656 }
6657 for (i=0; i < 4; i++)
6658 (q+i)->point=points[i];
6659 if (TraceBezier(mvg_info,4) == MagickFalse)
6660 return(-1);
6661 q=(*mvg_info->primitive_info)+mvg_info->offset;
6662 mvg_info->offset+=q->coordinates;
6663 q+=q->coordinates;
6664 point=end;
6665 while (isspace((int) ((unsigned char) *p)) != 0)
6666 p++;
6667 if (*p == ',')
6668 p++;
6669 } while (IsPoint(p) != MagickFalse);
6670 break;
6671 }
6672 case 'H':
6673 case 'h':
6674 {
6675 do
6676 {
6677 (void) GetNextToken(p,&p,MaxTextExtent,token);
6678 if (*token == ',')
6679 (void) GetNextToken(p,&p,MaxTextExtent,token);
6680 x=GetDrawValue(token,&next_token);
6681 if (token == next_token)
6682 ThrowPointExpectedException(image,token);
6683 point.x=(double) (attribute == (int) 'H' ? x: point.x+x);
6684 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6685 return(-1);
6686 q=(*mvg_info->primitive_info)+mvg_info->offset;
6687 if (TracePoint(q,point) == MagickFalse)
6688 return(-1);
6689 mvg_info->offset+=q->coordinates;
6690 q+=q->coordinates;
6691 while (isspace((int) ((unsigned char) *p)) != 0)
6692 p++;
6693 if (*p == ',')
6694 p++;
6695 } while (IsPoint(p) != MagickFalse);
6696 break;
6697 }
6698 case 'l':
6699 case 'L':
6700 {
6701 /*
6702 Line to.
6703 */
6704 do
6705 {
6706 (void) GetNextToken(p,&p,MaxTextExtent,token);
6707 if (*token == ',')
6708 (void) GetNextToken(p,&p,MaxTextExtent,token);
6709 x=GetDrawValue(token,&next_token);
6710 if (token == next_token)
6711 ThrowPointExpectedException(image,token);
6712 (void) GetNextToken(p,&p,MaxTextExtent,token);
6713 if (*token == ',')
6714 (void) GetNextToken(p,&p,MaxTextExtent,token);
6715 y=GetDrawValue(token,&next_token);
6716 if (token == next_token)
6717 ThrowPointExpectedException(image,token);
6718 point.x=(double) (attribute == (int) 'L' ? x : point.x+x);
6719 point.y=(double) (attribute == (int) 'L' ? y : point.y+y);
6720 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6721 return(-1);
6722 q=(*mvg_info->primitive_info)+mvg_info->offset;
6723 if (TracePoint(q,point) == MagickFalse)
6724 return(-1);
6725 mvg_info->offset+=q->coordinates;
6726 q+=q->coordinates;
6727 while (isspace((int) ((unsigned char) *p)) != 0)
6728 p++;
6729 if (*p == ',')
6730 p++;
6731 } while (IsPoint(p) != MagickFalse);
6732 break;
6733 }
6734 case 'M':
6735 case 'm':
6736 {
6737 /*
6738 Move to.
6739 */
6740 if (mvg_info->offset != subpath_offset)
6741 {
6742 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6743 primitive_info->coordinates=(size_t) (q-primitive_info);
6744 number_coordinates+=primitive_info->coordinates;
6745 primitive_info=q;
6746 subpath_offset=mvg_info->offset;
6747 }
6748 i=0;
6749 do
6750 {
6751 (void) GetNextToken(p,&p,MaxTextExtent,token);
6752 if (*token == ',')
6753 (void) GetNextToken(p,&p,MaxTextExtent,token);
6754 x=GetDrawValue(token,&next_token);
6755 if (token == next_token)
6756 ThrowPointExpectedException(image,token);
6757 (void) GetNextToken(p,&p,MaxTextExtent,token);
6758 if (*token == ',')
6759 (void) GetNextToken(p,&p,MaxTextExtent,token);
6760 y=GetDrawValue(token,&next_token);
6761 if (token == next_token)
6762 ThrowPointExpectedException(image,token);
6763 point.x=(double) (attribute == (int) 'M' ? x : point.x+x);
6764 point.y=(double) (attribute == (int) 'M' ? y : point.y+y);
6765 if (i == 0)
6766 start=point;
6767 i++;
6768 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6769 return(-1);
6770 q=(*mvg_info->primitive_info)+mvg_info->offset;
6771 if (TracePoint(q,point) == MagickFalse)
6772 return(-1);
6773 mvg_info->offset+=q->coordinates;
6774 q+=q->coordinates;
6775 while (isspace((int) ((unsigned char) *p)) != 0)
6776 p++;
6777 if (*p == ',')
6778 p++;
6779 } while (IsPoint(p) != MagickFalse);
6780 break;
6781 }
6782 case 'q':
6783 case 'Q':
6784 {
6785 /*
6786 Quadratic Bézier curve.
6787 */
6788 do
6789 {
6790 points[0]=point;
6791 for (i=1; i < 3; i++)
6792 {
6793 (void) GetNextToken(p,&p,MaxTextExtent,token);
6794 if (*token == ',')
6795 (void) GetNextToken(p,&p,MaxTextExtent,token);
6796 x=GetDrawValue(token,&next_token);
6797 if (token == next_token)
6798 ThrowPointExpectedException(image,token);
6799 (void) GetNextToken(p,&p,MaxTextExtent,token);
6800 if (*token == ',')
6801 (void) GetNextToken(p,&p,MaxTextExtent,token);
6802 y=GetDrawValue(token,&next_token);
6803 if (token == next_token)
6804 ThrowPointExpectedException(image,token);
6805 if (*p == ',')
6806 p++;
6807 end.x=(double) (attribute == (int) 'Q' ? x : point.x+x);
6808 end.y=(double) (attribute == (int) 'Q' ? y : point.y+y);
6809 points[i]=end;
6810 }
6811 for (i=0; i < 3; i++)
6812 (q+i)->point=points[i];
6813 if (TraceBezier(mvg_info,3) == MagickFalse)
6814 return(-1);
6815 q=(*mvg_info->primitive_info)+mvg_info->offset;
6816 mvg_info->offset+=q->coordinates;
6817 q+=q->coordinates;
6818 point=end;
6819 while (isspace((int) ((unsigned char) *p)) != 0)
6820 p++;
6821 if (*p == ',')
6822 p++;
6823 } while (IsPoint(p) != MagickFalse);
6824 break;
6825 }
6826 case 's':
6827 case 'S':
6828 {
6829 /*
6830 Cubic Bézier curve.
6831 */
6832 do
6833 {
6834 points[0]=points[3];
6835 points[1].x=2.0*points[3].x-points[2].x;
6836 points[1].y=2.0*points[3].y-points[2].y;
6837 for (i=2; i < 4; i++)
6838 {
6839 (void) GetNextToken(p,&p,MaxTextExtent,token);
6840 if (*token == ',')
6841 (void) GetNextToken(p,&p,MaxTextExtent,token);
6842 x=GetDrawValue(token,&next_token);
6843 if (token == next_token)
6844 ThrowPointExpectedException(image,token);
6845 (void) GetNextToken(p,&p,MaxTextExtent,token);
6846 if (*token == ',')
6847 (void) GetNextToken(p,&p,MaxTextExtent,token);
6848 y=GetDrawValue(token,&next_token);
6849 if (token == next_token)
6850 ThrowPointExpectedException(image,token);
6851 if (*p == ',')
6852 p++;
6853 end.x=(double) (attribute == (int) 'S' ? x : point.x+x);
6854 end.y=(double) (attribute == (int) 'S' ? y : point.y+y);
6855 points[i]=end;
6856 }
6857 if (strchr("CcSs",last_attribute) == (char *) NULL)
6858 {
6859 points[0]=point;
6860 points[1]=point;
6861 }
6862 for (i=0; i < 4; i++)
6863 (q+i)->point=points[i];
6864 if (TraceBezier(mvg_info,4) == MagickFalse)
6865 return(-1);
6866 q=(*mvg_info->primitive_info)+mvg_info->offset;
6867 mvg_info->offset+=q->coordinates;
6868 q+=q->coordinates;
6869 point=end;
6870 last_attribute=attribute;
6871 while (isspace((int) ((unsigned char) *p)) != 0)
6872 p++;
6873 if (*p == ',')
6874 p++;
6875 } while (IsPoint(p) != MagickFalse);
6876 break;
6877 }
6878 case 't':
6879 case 'T':
6880 {
6881 /*
6882 Quadratic Bézier curve.
6883 */
6884 do
6885 {
6886 points[0]=points[2];
6887 points[1].x=2.0*points[2].x-points[1].x;
6888 points[1].y=2.0*points[2].y-points[1].y;
6889 for (i=2; i < 3; i++)
6890 {
6891 (void) GetNextToken(p,&p,MaxTextExtent,token);
6892 if (*token == ',')
6893 (void) GetNextToken(p,&p,MaxTextExtent,token);
6894 x=GetDrawValue(token,&next_token);
6895 if (token == next_token)
6896 ThrowPointExpectedException(image,token);
6897 (void) GetNextToken(p,&p,MaxTextExtent,token);
6898 if (*token == ',')
6899 (void) GetNextToken(p,&p,MaxTextExtent,token);
6900 y=GetDrawValue(token,&next_token);
6901 if (token == next_token)
6902 ThrowPointExpectedException(image,token);
6903 end.x=(double) (attribute == (int) 'T' ? x : point.x+x);
6904 end.y=(double) (attribute == (int) 'T' ? y : point.y+y);
6905 points[i]=end;
6906 }
6907 if (status == MagickFalse)
6908 break;
6909 if (strchr("QqTt",last_attribute) == (char *) NULL)
6910 {
6911 points[0]=point;
6912 points[1]=point;
6913 }
6914 for (i=0; i < 3; i++)
6915 (q+i)->point=points[i];
6916 if (TraceBezier(mvg_info,3) == MagickFalse)
6917 return(-1);
6918 q=(*mvg_info->primitive_info)+mvg_info->offset;
6919 mvg_info->offset+=q->coordinates;
6920 q+=q->coordinates;
6921 point=end;
6922 last_attribute=attribute;
6923 while (isspace((int) ((unsigned char) *p)) != 0)
6924 p++;
6925 if (*p == ',')
6926 p++;
6927 } while (IsPoint(p) != MagickFalse);
6928 break;
6929 }
6930 case 'v':
6931 case 'V':
6932 {
6933 /*
6934 Line to.
6935 */
6936 do
6937 {
6938 (void) GetNextToken(p,&p,MaxTextExtent,token);
6939 if (*token == ',')
6940 (void) GetNextToken(p,&p,MaxTextExtent,token);
6941 y=GetDrawValue(token,&next_token);
6942 if (token == next_token)
6943 ThrowPointExpectedException(image,token);
6944 point.y=(double) (attribute == (int) 'V' ? y : point.y+y);
6945 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6946 return(-1);
6947 q=(*mvg_info->primitive_info)+mvg_info->offset;
6948 if (TracePoint(q,point) == MagickFalse)
6949 return(-1);
6950 mvg_info->offset+=q->coordinates;
6951 q+=q->coordinates;
6952 while (isspace((int) ((unsigned char) *p)) != 0)
6953 p++;
6954 if (*p == ',')
6955 p++;
6956 } while (IsPoint(p) != MagickFalse);
6957 break;
6958 }
6959 case 'z':
6960 case 'Z':
6961 {
6962 /*
6963 Close path.
6964 */
6965 point=start;
6966 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
6967 return(-1);
6968 q=(*mvg_info->primitive_info)+mvg_info->offset;
6969 if (TracePoint(q,point) == MagickFalse)
6970 return(-1);
6971 mvg_info->offset+=q->coordinates;
6972 q+=q->coordinates;
6973 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6974 primitive_info->coordinates=(size_t) (q-primitive_info);
6975 primitive_info->closed_subpath=MagickTrue;
6976 number_coordinates+=primitive_info->coordinates;
6977 primitive_info=q;
6978 subpath_offset=mvg_info->offset;
6979 z_count++;
6980 break;
6981 }
6982 default:
6983 {
6984 ThrowPointExpectedException(image,token);
6985 break;
6986 }
6987 }
6988 }
6989 if (status == MagickFalse)
6990 return(-1);
6991 primitive_info=(*mvg_info->primitive_info)+subpath_offset;
6992 primitive_info->coordinates=(size_t) (q-primitive_info);
6993 number_coordinates+=primitive_info->coordinates;
6994 for (i=0; i < (ssize_t) number_coordinates; i++)
6995 {
6996 q--;
6997 q->primitive=primitive_type;
6998 if (z_count > 1)
6999 q->method=FillToBorderMethod;
7000 }
7001 q=primitive_info;
7002 return((ssize_t) number_coordinates);
7003}
7004
7005static MagickBooleanType TraceRectangle(PrimitiveInfo *primitive_info,
7006 const PointInfo start,const PointInfo end)
7007{
7008 PointInfo
7009 point;
7010
7012 *p;
7013
7014 ssize_t
7015 i;
7016
7017 p=primitive_info;
7018 if (TracePoint(p,start) == MagickFalse)
7019 return(MagickFalse);
7020 p+=p->coordinates;
7021 point.x=start.x;
7022 point.y=end.y;
7023 if (TracePoint(p,point) == MagickFalse)
7024 return(MagickFalse);
7025 p+=p->coordinates;
7026 if (TracePoint(p,end) == MagickFalse)
7027 return(MagickFalse);
7028 p+=p->coordinates;
7029 point.x=end.x;
7030 point.y=start.y;
7031 if (TracePoint(p,point) == MagickFalse)
7032 return(MagickFalse);
7033 p+=p->coordinates;
7034 if (TracePoint(p,start) == MagickFalse)
7035 return(MagickFalse);
7036 p+=p->coordinates;
7037 primitive_info->coordinates=(size_t) (p-primitive_info);
7038 primitive_info->closed_subpath=MagickTrue;
7039 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7040 {
7041 p->primitive=primitive_info->primitive;
7042 p--;
7043 }
7044 return(MagickTrue);
7045}
7046
7047static MagickBooleanType TraceRoundRectangle(MVGInfo *mvg_info,
7048 const PointInfo start,const PointInfo end,PointInfo arc)
7049{
7050 PointInfo
7051 degrees,
7052 point,
7053 segment;
7054
7056 *primitive_info;
7057
7059 *p;
7060
7061 ssize_t
7062 i;
7063
7064 ssize_t
7065 offset;
7066
7067 offset=mvg_info->offset;
7068 segment.x=fabs(end.x-start.x);
7069 segment.y=fabs(end.y-start.y);
7070 if ((segment.x < MagickEpsilon) || (segment.y < MagickEpsilon))
7071 {
7072 (*mvg_info->primitive_info+mvg_info->offset)->coordinates=0;
7073 return(MagickTrue);
7074 }
7075 if (arc.x > (0.5*segment.x))
7076 arc.x=0.5*segment.x;
7077 if (arc.y > (0.5*segment.y))
7078 arc.y=0.5*segment.y;
7079 point.x=start.x+segment.x-arc.x;
7080 point.y=start.y+arc.y;
7081 degrees.x=270.0;
7082 degrees.y=360.0;
7083 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7084 return(MagickFalse);
7085 p=(*mvg_info->primitive_info)+mvg_info->offset;
7086 mvg_info->offset+=p->coordinates;
7087 point.x=start.x+segment.x-arc.x;
7088 point.y=start.y+segment.y-arc.y;
7089 degrees.x=0.0;
7090 degrees.y=90.0;
7091 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7092 return(MagickFalse);
7093 p=(*mvg_info->primitive_info)+mvg_info->offset;
7094 mvg_info->offset+=p->coordinates;
7095 point.x=start.x+arc.x;
7096 point.y=start.y+segment.y-arc.y;
7097 degrees.x=90.0;
7098 degrees.y=180.0;
7099 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7100 return(MagickFalse);
7101 p=(*mvg_info->primitive_info)+mvg_info->offset;
7102 mvg_info->offset+=p->coordinates;
7103 point.x=start.x+arc.x;
7104 point.y=start.y+arc.y;
7105 degrees.x=180.0;
7106 degrees.y=270.0;
7107 if (TraceEllipse(mvg_info,point,arc,degrees) == MagickFalse)
7108 return(MagickFalse);
7109 p=(*mvg_info->primitive_info)+mvg_info->offset;
7110 mvg_info->offset+=p->coordinates;
7111 if (CheckPrimitiveExtent(mvg_info,PrimitiveExtentPad) == MagickFalse)
7112 return(MagickFalse);
7113 p=(*mvg_info->primitive_info)+mvg_info->offset;
7114 if (TracePoint(p,(*mvg_info->primitive_info+offset)->point) == MagickFalse)
7115 return(MagickFalse);
7116 p+=p->coordinates;
7117 mvg_info->offset=offset;
7118 primitive_info=(*mvg_info->primitive_info)+offset;
7119 primitive_info->coordinates=(size_t) (p-primitive_info);
7120 primitive_info->closed_subpath=MagickTrue;
7121 for (i=0; i < (ssize_t) primitive_info->coordinates; i++)
7122 {
7123 p->primitive=primitive_info->primitive;
7124 p--;
7125 }
7126 return(MagickTrue);
7127}
7128
7129static MagickBooleanType TraceSquareLinecap(PrimitiveInfo *primitive_info,
7130 const size_t number_vertices,const double offset)
7131{
7132 double
7133 distance;
7134
7135 double
7136 dx,
7137 dy;
7138
7139 ssize_t
7140 i;
7141
7142 ssize_t
7143 j;
7144
7145 dx=0.0;
7146 dy=0.0;
7147 for (i=1; i < (ssize_t) number_vertices; i++)
7148 {
7149 dx=primitive_info[0].point.x-primitive_info[i].point.x;
7150 dy=primitive_info[0].point.y-primitive_info[i].point.y;
7151 if ((fabs((double) dx) >= MagickEpsilon) ||
7152 (fabs((double) dy) >= MagickEpsilon))
7153 break;
7154 }
7155 if (i == (ssize_t) number_vertices)
7156 i=(ssize_t) number_vertices-1L;
7157 distance=hypot((double) dx,(double) dy);
7158 primitive_info[0].point.x=(double) (primitive_info[i].point.x+
7159 dx*(distance+offset)/distance);
7160 primitive_info[0].point.y=(double) (primitive_info[i].point.y+
7161 dy*(distance+offset)/distance);
7162 for (j=(ssize_t) number_vertices-2; j >= 0; j--)
7163 {
7164 dx=primitive_info[number_vertices-1].point.x-primitive_info[j].point.x;
7165 dy=primitive_info[number_vertices-1].point.y-primitive_info[j].point.y;
7166 if ((fabs((double) dx) >= MagickEpsilon) ||
7167 (fabs((double) dy) >= MagickEpsilon))
7168 break;
7169 }
7170 distance=hypot((double) dx,(double) dy);
7171 primitive_info[number_vertices-1].point.x=(double) (primitive_info[j].point.x+
7172 dx*(distance+offset)/distance);
7173 primitive_info[number_vertices-1].point.y=(double) (primitive_info[j].point.y+
7174 dy*(distance+offset)/distance);
7175 return(MagickTrue);
7176}
7177
7178static PrimitiveInfo *TraceStrokePolygon(const DrawInfo *draw_info,
7179 const PrimitiveInfo *primitive_info,ExceptionInfo *exception)
7180{
7181#define MaxStrokePad (6*BezierQuantum+360)
7182#define CheckPathExtent(pad_p,pad_q) \
7183{ \
7184 if ((pad_p) > MaxBezierCoordinates) \
7185 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7186 else \
7187 if ((ssize_t) (p+(pad_p)) >= (ssize_t) extent_p) \
7188 { \
7189 if (~extent_p < (pad_p)) \
7190 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7191 else \
7192 { \
7193 extent_p+=(pad_p); \
7194 stroke_p=(PointInfo *) ResizeQuantumMemory(stroke_p,extent_p+ \
7195 MaxStrokePad,sizeof(*stroke_p)); \
7196 } \
7197 } \
7198 if ((pad_q) > MaxBezierCoordinates) \
7199 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7200 else \
7201 if ((ssize_t) (q+(pad_q)) >= (ssize_t) extent_q) \
7202 { \
7203 if (~extent_q < (pad_q)) \
7204 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7205 else \
7206 { \
7207 extent_q+=(pad_q); \
7208 stroke_q=(PointInfo *) ResizeQuantumMemory(stroke_q,extent_q+ \
7209 MaxStrokePad,sizeof(*stroke_q)); \
7210 } \
7211 } \
7212 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL)) \
7213 { \
7214 if (stroke_p != (PointInfo *) NULL) \
7215 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p); \
7216 if (stroke_q != (PointInfo *) NULL) \
7217 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q); \
7218 polygon_primitive=(PrimitiveInfo *) \
7219 RelinquishMagickMemory(polygon_primitive); \
7220 (void) ThrowMagickException(exception,GetMagickModule(), \
7221 ResourceLimitError,"MemoryAllocationFailed","`%s'",""); \
7222 return((PrimitiveInfo *) NULL); \
7223 } \
7224}
7225
7226 typedef struct _StrokeSegment
7227 {
7228 double
7229 p,
7230 q;
7231 } StrokeSegment;
7232
7233 double
7234 delta_theta,
7235 dot_product,
7236 mid,
7237 miterlimit;
7238
7239 MagickBooleanType
7240 closed_path;
7241
7242 PointInfo
7243 box_p[5],
7244 box_q[5],
7245 center,
7246 offset,
7247 *stroke_p,
7248 *stroke_q;
7249
7251 *polygon_primitive,
7252 *stroke_polygon;
7253
7254 ssize_t
7255 i;
7256
7257 size_t
7258 arc_segments,
7259 extent_p,
7260 extent_q,
7261 number_vertices;
7262
7263 ssize_t
7264 j,
7265 n,
7266 p,
7267 q;
7268
7269 StrokeSegment
7270 dx = {0.0, 0.0},
7271 dy = {0.0, 0.0},
7272 inverse_slope = {0.0, 0.0},
7273 slope = {0.0, 0.0},
7274 theta = {0.0, 0.0};
7275
7276 /*
7277 Allocate paths.
7278 */
7279 number_vertices=primitive_info->coordinates;
7280 polygon_primitive=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7281 number_vertices+2UL,sizeof(*polygon_primitive));
7282 if (polygon_primitive == (PrimitiveInfo *) NULL)
7283 {
7284 (void) ThrowMagickException(exception,GetMagickModule(),
7285 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7286 return((PrimitiveInfo *) NULL);
7287 }
7288 (void) memcpy(polygon_primitive,primitive_info,(size_t) number_vertices*
7289 sizeof(*polygon_primitive));
7290 offset.x=primitive_info[number_vertices-1].point.x-primitive_info[0].point.x;
7291 offset.y=primitive_info[number_vertices-1].point.y-primitive_info[0].point.y;
7292 closed_path=(fabs(offset.x) < MagickEpsilon) &&
7293 (fabs(offset.y) < MagickEpsilon) ? MagickTrue : MagickFalse;
7294 if (((draw_info->linejoin == RoundJoin) ||
7295 (draw_info->linejoin == MiterJoin)) && (closed_path != MagickFalse))
7296 {
7297 polygon_primitive[number_vertices]=primitive_info[1];
7298 number_vertices++;
7299 }
7300 polygon_primitive[number_vertices].primitive=UndefinedPrimitive;
7301 /*
7302 Compute the slope for the first line segment, p.
7303 */
7304 dx.p=0.0;
7305 dy.p=0.0;
7306 for (n=1; n < (ssize_t) number_vertices; n++)
7307 {
7308 dx.p=polygon_primitive[n].point.x-polygon_primitive[0].point.x;
7309 dy.p=polygon_primitive[n].point.y-polygon_primitive[0].point.y;
7310 if ((fabs(dx.p) >= MagickEpsilon) || (fabs(dy.p) >= MagickEpsilon))
7311 break;
7312 }
7313 if (n == (ssize_t) number_vertices)
7314 {
7315 if ((draw_info->linecap != RoundCap) || (closed_path != MagickFalse))
7316 {
7317 /*
7318 Zero length subpath.
7319 */
7320 stroke_polygon=(PrimitiveInfo *) AcquireCriticalMemory(
7321 sizeof(*stroke_polygon));
7322 stroke_polygon[0]=polygon_primitive[0];
7323 stroke_polygon[0].coordinates=0;
7324 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7325 polygon_primitive);
7326 return(stroke_polygon);
7327 }
7328 n=(ssize_t) number_vertices-1L;
7329 }
7330 extent_p=2*number_vertices;
7331 extent_q=2*number_vertices;
7332 stroke_p=(PointInfo *) AcquireQuantumMemory((size_t) extent_p+MaxStrokePad,
7333 sizeof(*stroke_p));
7334 stroke_q=(PointInfo *) AcquireQuantumMemory((size_t) extent_q+MaxStrokePad,
7335 sizeof(*stroke_q));
7336 if ((stroke_p == (PointInfo *) NULL) || (stroke_q == (PointInfo *) NULL))
7337 {
7338 if (stroke_p != (PointInfo *) NULL)
7339 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7340 if (stroke_q != (PointInfo *) NULL)
7341 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7342 polygon_primitive=(PrimitiveInfo *)
7343 RelinquishMagickMemory(polygon_primitive);
7344 (void) ThrowMagickException(exception,GetMagickModule(),
7345 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7346 return((PrimitiveInfo *) NULL);
7347 }
7348 slope.p=0.0;
7349 inverse_slope.p=0.0;
7350 if (fabs(dx.p) < MagickEpsilon)
7351 {
7352 if (dx.p >= 0.0)
7353 slope.p=dy.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7354 else
7355 slope.p=dy.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7356 }
7357 else
7358 if (fabs(dy.p) < MagickEpsilon)
7359 {
7360 if (dy.p >= 0.0)
7361 inverse_slope.p=dx.p < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7362 else
7363 inverse_slope.p=dx.p < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7364 }
7365 else
7366 {
7367 slope.p=dy.p/dx.p;
7368 inverse_slope.p=(-1.0*PerceptibleReciprocal(slope.p));
7369 }
7370 mid=ExpandAffine(&draw_info->affine)*draw_info->stroke_width/2.0;
7371 miterlimit=(double) (draw_info->miterlimit*draw_info->miterlimit*mid*mid);
7372 if ((draw_info->linecap == SquareCap) && (closed_path == MagickFalse))
7373 (void) TraceSquareLinecap(polygon_primitive,number_vertices,mid);
7374 offset.x=sqrt((double) (mid*mid/(inverse_slope.p*inverse_slope.p+1.0)));
7375 offset.y=(double) (offset.x*inverse_slope.p);
7376 if ((dy.p*offset.x-dx.p*offset.y) > 0.0)
7377 {
7378 box_p[0].x=polygon_primitive[0].point.x-offset.x;
7379 box_p[0].y=polygon_primitive[0].point.y-offset.x*inverse_slope.p;
7380 box_p[1].x=polygon_primitive[n].point.x-offset.x;
7381 box_p[1].y=polygon_primitive[n].point.y-offset.x*inverse_slope.p;
7382 box_q[0].x=polygon_primitive[0].point.x+offset.x;
7383 box_q[0].y=polygon_primitive[0].point.y+offset.x*inverse_slope.p;
7384 box_q[1].x=polygon_primitive[n].point.x+offset.x;
7385 box_q[1].y=polygon_primitive[n].point.y+offset.x*inverse_slope.p;
7386 }
7387 else
7388 {
7389 box_p[0].x=polygon_primitive[0].point.x+offset.x;
7390 box_p[0].y=polygon_primitive[0].point.y+offset.y;
7391 box_p[1].x=polygon_primitive[n].point.x+offset.x;
7392 box_p[1].y=polygon_primitive[n].point.y+offset.y;
7393 box_q[0].x=polygon_primitive[0].point.x-offset.x;
7394 box_q[0].y=polygon_primitive[0].point.y-offset.y;
7395 box_q[1].x=polygon_primitive[n].point.x-offset.x;
7396 box_q[1].y=polygon_primitive[n].point.y-offset.y;
7397 }
7398 /*
7399 Create strokes for the line join attribute: bevel, miter, round.
7400 */
7401 p=0;
7402 q=0;
7403 stroke_q[p++]=box_q[0];
7404 stroke_p[q++]=box_p[0];
7405 for (i=(ssize_t) n+1; i < (ssize_t) number_vertices; i++)
7406 {
7407 /*
7408 Compute the slope for this line segment, q.
7409 */
7410 dx.q=polygon_primitive[i].point.x-polygon_primitive[n].point.x;
7411 dy.q=polygon_primitive[i].point.y-polygon_primitive[n].point.y;
7412 dot_product=dx.q*dx.q+dy.q*dy.q;
7413 if (dot_product < 0.25)
7414 continue;
7415 slope.q=0.0;
7416 inverse_slope.q=0.0;
7417 if (fabs(dx.q) < MagickEpsilon)
7418 {
7419 if (dx.q >= 0.0)
7420 slope.q=dy.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7421 else
7422 slope.q=dy.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7423 }
7424 else
7425 if (fabs(dy.q) < MagickEpsilon)
7426 {
7427 if (dy.q >= 0.0)
7428 inverse_slope.q=dx.q < 0.0 ? -1.0/MagickEpsilon : 1.0/MagickEpsilon;
7429 else
7430 inverse_slope.q=dx.q < 0.0 ? 1.0/MagickEpsilon : -1.0/MagickEpsilon;
7431 }
7432 else
7433 {
7434 slope.q=dy.q/dx.q;
7435 inverse_slope.q=(-1.0*PerceptibleReciprocal(slope.q));
7436 }
7437 offset.x=sqrt((double) (mid*mid/(inverse_slope.q*inverse_slope.q+1.0)));
7438 offset.y=(double) (offset.x*inverse_slope.q);
7439 dot_product=dy.q*offset.x-dx.q*offset.y;
7440 if (dot_product > 0.0)
7441 {
7442 box_p[2].x=polygon_primitive[n].point.x-offset.x;
7443 box_p[2].y=polygon_primitive[n].point.y-offset.y;
7444 box_p[3].x=polygon_primitive[i].point.x-offset.x;
7445 box_p[3].y=polygon_primitive[i].point.y-offset.y;
7446 box_q[2].x=polygon_primitive[n].point.x+offset.x;
7447 box_q[2].y=polygon_primitive[n].point.y+offset.y;
7448 box_q[3].x=polygon_primitive[i].point.x+offset.x;
7449 box_q[3].y=polygon_primitive[i].point.y+offset.y;
7450 }
7451 else
7452 {
7453 box_p[2].x=polygon_primitive[n].point.x+offset.x;
7454 box_p[2].y=polygon_primitive[n].point.y+offset.y;
7455 box_p[3].x=polygon_primitive[i].point.x+offset.x;
7456 box_p[3].y=polygon_primitive[i].point.y+offset.y;
7457 box_q[2].x=polygon_primitive[n].point.x-offset.x;
7458 box_q[2].y=polygon_primitive[n].point.y-offset.y;
7459 box_q[3].x=polygon_primitive[i].point.x-offset.x;
7460 box_q[3].y=polygon_primitive[i].point.y-offset.y;
7461 }
7462 if (fabs((double) (slope.p-slope.q)) < MagickEpsilon)
7463 {
7464 box_p[4]=box_p[1];
7465 box_q[4]=box_q[1];
7466 }
7467 else
7468 {
7469 box_p[4].x=(double) ((slope.p*box_p[0].x-box_p[0].y-slope.q*box_p[3].x+
7470 box_p[3].y)/(slope.p-slope.q));
7471 box_p[4].y=(double) (slope.p*(box_p[4].x-box_p[0].x)+box_p[0].y);
7472 box_q[4].x=(double) ((slope.p*box_q[0].x-box_q[0].y-slope.q*box_q[3].x+
7473 box_q[3].y)/(slope.p-slope.q));
7474 box_q[4].y=(double) (slope.p*(box_q[4].x-box_q[0].x)+box_q[0].y);
7475 }
7476 CheckPathExtent(MaxStrokePad,MaxStrokePad);
7477 dot_product=dx.q*dy.p-dx.p*dy.q;
7478 if (dot_product <= 0.0)
7479 switch (draw_info->linejoin)
7480 {
7481 case BevelJoin:
7482 {
7483 stroke_q[q++]=box_q[1];
7484 stroke_q[q++]=box_q[2];
7485 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7486 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7487 if (dot_product <= miterlimit)
7488 stroke_p[p++]=box_p[4];
7489 else
7490 {
7491 stroke_p[p++]=box_p[1];
7492 stroke_p[p++]=box_p[2];
7493 }
7494 break;
7495 }
7496 case MiterJoin:
7497 {
7498 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7499 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7500 if (dot_product <= miterlimit)
7501 {
7502 stroke_q[q++]=box_q[4];
7503 stroke_p[p++]=box_p[4];
7504 }
7505 else
7506 {
7507 stroke_q[q++]=box_q[1];
7508 stroke_q[q++]=box_q[2];
7509 stroke_p[p++]=box_p[1];
7510 stroke_p[p++]=box_p[2];
7511 }
7512 break;
7513 }
7514 case RoundJoin:
7515 {
7516 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7517 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7518 if (dot_product <= miterlimit)
7519 stroke_p[p++]=box_p[4];
7520 else
7521 {
7522 stroke_p[p++]=box_p[1];
7523 stroke_p[p++]=box_p[2];
7524 }
7525 center=polygon_primitive[n].point;
7526 theta.p=atan2(box_q[1].y-center.y,box_q[1].x-center.x);
7527 theta.q=atan2(box_q[2].y-center.y,box_q[2].x-center.x);
7528 if (theta.q < theta.p)
7529 theta.q+=2.0*MagickPI;
7530 arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.q-
7531 theta.p)/(2.0*sqrt(PerceptibleReciprocal(mid))))));
7532 CheckPathExtent(MaxStrokePad,arc_segments+MaxStrokePad);
7533 stroke_q[q].x=box_q[1].x;
7534 stroke_q[q].y=box_q[1].y;
7535 q++;
7536 for (j=1; j < (ssize_t) arc_segments; j++)
7537 {
7538 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7539 stroke_q[q].x=(double) (center.x+mid*cos(fmod((double)
7540 (theta.p+delta_theta),DegreesToRadians(360.0))));
7541 stroke_q[q].y=(double) (center.y+mid*sin(fmod((double)
7542 (theta.p+delta_theta),DegreesToRadians(360.0))));
7543 q++;
7544 }
7545 stroke_q[q++]=box_q[2];
7546 break;
7547 }
7548 default:
7549 break;
7550 }
7551 else
7552 switch (draw_info->linejoin)
7553 {
7554 case BevelJoin:
7555 {
7556 stroke_p[p++]=box_p[1];
7557 stroke_p[p++]=box_p[2];
7558 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7559 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7560 if (dot_product <= miterlimit)
7561 stroke_q[q++]=box_q[4];
7562 else
7563 {
7564 stroke_q[q++]=box_q[1];
7565 stroke_q[q++]=box_q[2];
7566 }
7567 break;
7568 }
7569 case MiterJoin:
7570 {
7571 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7572 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7573 if (dot_product <= miterlimit)
7574 {
7575 stroke_q[q++]=box_q[4];
7576 stroke_p[p++]=box_p[4];
7577 }
7578 else
7579 {
7580 stroke_q[q++]=box_q[1];
7581 stroke_q[q++]=box_q[2];
7582 stroke_p[p++]=box_p[1];
7583 stroke_p[p++]=box_p[2];
7584 }
7585 break;
7586 }
7587 case RoundJoin:
7588 {
7589 dot_product=(box_q[4].x-box_p[4].x)*(box_q[4].x-box_p[4].x)+
7590 (box_q[4].y-box_p[4].y)*(box_q[4].y-box_p[4].y);
7591 if (dot_product <= miterlimit)
7592 stroke_q[q++]=box_q[4];
7593 else
7594 {
7595 stroke_q[q++]=box_q[1];
7596 stroke_q[q++]=box_q[2];
7597 }
7598 center=polygon_primitive[n].point;
7599 theta.p=atan2(box_p[1].y-center.y,box_p[1].x-center.x);
7600 theta.q=atan2(box_p[2].y-center.y,box_p[2].x-center.x);
7601 if (theta.p < theta.q)
7602 theta.p+=2.0*MagickPI;
7603 arc_segments=(size_t) CastDoubleToLong(ceil((double) ((theta.p-
7604 theta.q)/(2.0*sqrt((double) (PerceptibleReciprocal(mid)))))));
7605 CheckPathExtent(arc_segments+MaxStrokePad,MaxStrokePad);
7606 stroke_p[p++]=box_p[1];
7607 for (j=1; j < (ssize_t) arc_segments; j++)
7608 {
7609 delta_theta=(double) (j*(theta.q-theta.p)/arc_segments);
7610 stroke_p[p].x=(double) (center.x+mid*cos(fmod((double)
7611 (theta.p+delta_theta),DegreesToRadians(360.0))));
7612 stroke_p[p].y=(double) (center.y+mid*sin(fmod((double)
7613 (theta.p+delta_theta),DegreesToRadians(360.0))));
7614 p++;
7615 }
7616 stroke_p[p++]=box_p[2];
7617 break;
7618 }
7619 default:
7620 break;
7621 }
7622 slope.p=slope.q;
7623 inverse_slope.p=inverse_slope.q;
7624 box_p[0]=box_p[2];
7625 box_p[1]=box_p[3];
7626 box_q[0]=box_q[2];
7627 box_q[1]=box_q[3];
7628 dx.p=dx.q;
7629 dy.p=dy.q;
7630 n=i;
7631 }
7632 stroke_p[p++]=box_p[1];
7633 stroke_q[q++]=box_q[1];
7634 /*
7635 Trace stroked polygon.
7636 */
7637 stroke_polygon=(PrimitiveInfo *) AcquireQuantumMemory((size_t)
7638 (p+q+2UL*closed_path+2UL),sizeof(*stroke_polygon));
7639 if (stroke_polygon == (PrimitiveInfo *) NULL)
7640 {
7641 (void) ThrowMagickException(exception,GetMagickModule(),
7642 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
7643 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7644 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7645 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(
7646 polygon_primitive);
7647 return(stroke_polygon);
7648 }
7649 for (i=0; i < (ssize_t) p; i++)
7650 {
7651 stroke_polygon[i]=polygon_primitive[0];
7652 stroke_polygon[i].point=stroke_p[i];
7653 }
7654 if (closed_path != MagickFalse)
7655 {
7656 stroke_polygon[i]=polygon_primitive[0];
7657 stroke_polygon[i].point=stroke_polygon[0].point;
7658 i++;
7659 }
7660 for ( ; i < (ssize_t) (p+q+closed_path); i++)
7661 {
7662 stroke_polygon[i]=polygon_primitive[0];
7663 stroke_polygon[i].point=stroke_q[p+q+closed_path-(i+1)];
7664 }
7665 if (closed_path != MagickFalse)
7666 {
7667 stroke_polygon[i]=polygon_primitive[0];
7668 stroke_polygon[i].point=stroke_polygon[p+closed_path].point;
7669 i++;
7670 }
7671 stroke_polygon[i]=polygon_primitive[0];
7672 stroke_polygon[i].point=stroke_polygon[0].point;
7673 i++;
7674 stroke_polygon[i].primitive=UndefinedPrimitive;
7675 stroke_polygon[0].coordinates=(size_t) (p+q+2*closed_path+1);
7676 stroke_p=(PointInfo *) RelinquishMagickMemory(stroke_p);
7677 stroke_q=(PointInfo *) RelinquishMagickMemory(stroke_q);
7678 polygon_primitive=(PrimitiveInfo *) RelinquishMagickMemory(polygon_primitive);
7679 return(stroke_polygon);
7680}