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