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