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