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