MagickCore 6.9.13
Loading...
Searching...
No Matches
annotate.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% AAA N N N N OOO TTTTT AAA TTTTT EEEEE %
7% A A NN N NN N O O T A A T E %
8% AAAAA N N N N N N O O T AAAAA T EEE %
9% A A N NN N NN O O T A A T E %
10% A A N N N N OOO T A A T EEEEE %
11% %
12% %
13% MagickCore Image Annotation Methods %
14% %
15% Software Design %
16% Cristy %
17% July 1992 %
18% %
19% %
20% Copyright 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36% Digital Applications (www.digapp.com) contributed the stroked text algorithm.
37% It was written by Leonard Rosenthol.
38%
39%
40*/
41
42/*
43 Include declarations.
44*/
45#include "magick/studio.h"
46#include "magick/annotate.h"
47#include "magick/attribute.h"
48#include "magick/cache-private.h"
49#include "magick/cache-view.h"
50#include "magick/channel.h"
51#include "magick/client.h"
52#include "magick/color.h"
53#include "magick/color-private.h"
54#include "magick/colorspace-private.h"
55#include "magick/composite.h"
56#include "magick/composite-private.h"
57#include "magick/constitute.h"
58#include "magick/draw.h"
59#include "magick/draw-private.h"
60#include "magick/exception.h"
61#include "magick/exception-private.h"
62#include "magick/gem.h"
63#include "magick/geometry.h"
64#include "magick/image-private.h"
65#include "magick/log.h"
66#include "magick/quantum.h"
67#include "magick/quantum-private.h"
68#include "magick/pixel-accessor.h"
69#include "magick/policy.h"
70#include "magick/property.h"
71#include "magick/resource_.h"
72#include "magick/semaphore.h"
73#include "magick/statistic.h"
74#include "magick/string_.h"
75#include "magick/token.h"
76#include "magick/token-private.h"
77#include "magick/transform.h"
78#include "magick/type.h"
79#include "magick/utility.h"
80#include "magick/utility-private.h"
81#include "magick/xwindow-private.h"
82#if defined(MAGICKCORE_FREETYPE_DELEGATE)
83#if defined(__MINGW32__)
84# undef interface
85#endif
86#include <ft2build.h>
87#if defined(FT_FREETYPE_H)
88# include FT_FREETYPE_H
89#else
90# include <freetype/freetype.h>
91#endif
92#if defined(FT_GLYPH_H)
93# include FT_GLYPH_H
94#else
95# include <freetype/ftglyph.h>
96#endif
97#if defined(FT_OUTLINE_H)
98# include FT_OUTLINE_H
99#else
100# include <freetype/ftoutln.h>
101#endif
102#if defined(FT_BBOX_H)
103# include FT_BBOX_H
104#else
105# include <freetype/ftbbox.h>
106#endif /* defined(FT_BBOX_H) */
107#endif
108#if defined(MAGICKCORE_RAQM_DELEGATE)
109#include <raqm.h>
110#endif
111typedef struct _GraphemeInfo
112{
113 ssize_t
114 index,
115 x_offset,
116 x_advance,
117 y_offset,
118 y_advance;
119
120 size_t
121 cluster;
122} GraphemeInfo;
123
124/*
125 Annotate semaphores.
126*/
127static SemaphoreInfo
128 *annotate_semaphore = (SemaphoreInfo *) NULL;
129
130/*
131 Forward declarations.
132*/
133static MagickBooleanType
134 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *),
135 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *),
136 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
137 TypeMetric *),
138 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *);
139
140/*
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142% %
143% %
144% %
145+ A n n o t a t e C o m p o n e n t G e n e s i s %
146% %
147% %
148% %
149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150%
151% AnnotateComponentGenesis() instantiates the annotate component.
152%
153% The format of the AnnotateComponentGenesis method is:
154%
155% MagickBooleanType AnnotateComponentGenesis(void)
156%
157*/
158MagickExport MagickBooleanType AnnotateComponentGenesis(void)
159{
160 if (annotate_semaphore == (SemaphoreInfo *) NULL)
161 annotate_semaphore=AllocateSemaphoreInfo();
162 return(MagickTrue);
163}
164
165/*
166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167% %
168% %
169% %
170+ A n n o t a t e C o m p o n e n t T e r m i n u s %
171% %
172% %
173% %
174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175%
176% AnnotateComponentTerminus() destroys the annotate component.
177%
178% The format of the AnnotateComponentTerminus method is:
179%
180% AnnotateComponentTerminus(void)
181%
182*/
183MagickExport void AnnotateComponentTerminus(void)
184{
185 if (annotate_semaphore == (SemaphoreInfo *) NULL)
186 ActivateSemaphoreInfo(&annotate_semaphore);
187 DestroySemaphoreInfo(&annotate_semaphore);
188}
189
190/*
191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192% %
193% %
194% %
195% A n n o t a t e I m a g e %
196% %
197% %
198% %
199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200%
201% AnnotateImage() annotates an image with text.
202%
203% The format of the AnnotateImage method is:
204%
205% MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info)
206%
207% A description of each parameter follows:
208%
209% o image: the image.
210%
211% o draw_info: the draw info.
212%
213*/
214MagickExport MagickBooleanType AnnotateImage(Image *image,
215 const DrawInfo *draw_info)
216{
217 char
218 *p,
219 color[MaxTextExtent],
220 primitive[MaxTextExtent],
221 *text,
222 **textlist;
223
224 DrawInfo
225 *annotate,
226 *annotate_info;
227
228 GeometryInfo
229 geometry_info;
230
231 MagickBooleanType
232 status;
233
234 PixelPacket
235 pixel;
236
237 PointInfo
238 offset;
239
240 RectangleInfo
241 geometry;
242
243 size_t
244 height,
245 number_lines;
246
247 ssize_t
248 i;
249
250 TypeMetric
251 metrics;
252
253 assert(image != (Image *) NULL);
254 assert(image->signature == MagickCoreSignature);
255 if (IsEventLogging() != MagickFalse)
256 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
257 assert(draw_info != (DrawInfo *) NULL);
258 assert(draw_info->signature == MagickCoreSignature);
259 if (draw_info->text == (char *) NULL)
260 return(MagickFalse);
261 if (*draw_info->text == '\0')
262 return(MagickTrue);
263 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
264 text=annotate->text;
265 annotate->text=(char *) NULL;
266 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
267 number_lines=1;
268 for (p=text; *p != '\0'; p++)
269 if (*p == '\n')
270 number_lines++;
271 textlist=(char **) AcquireQuantumMemory(number_lines+1,sizeof(*textlist));
272 if (textlist == (char **) NULL)
273 {
274 annotate_info=DestroyDrawInfo(annotate_info);
275 annotate=DestroyDrawInfo(annotate);
276 text=DestroyString(text);
277 return(MagickFalse);
278 }
279 p=text;
280 for (i=0; i < (ssize_t) number_lines; i++)
281 {
282 char
283 *q;
284
285 textlist[i]=p;
286 for (q=p; *q != '\0'; q++)
287 if ((*q == '\r') || (*q == '\n'))
288 break;
289 if (*q == '\r')
290 {
291 *q='\0';
292 q++;
293 }
294 *q='\0';
295 p=q+1;
296 }
297 textlist[i]=(char *) NULL;
298 SetGeometry(image,&geometry);
299 SetGeometryInfo(&geometry_info);
300 if (annotate_info->geometry != (char *) NULL)
301 {
302 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
303 &image->exception);
304 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
305 }
306 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
307 {
308 annotate_info=DestroyDrawInfo(annotate_info);
309 annotate=DestroyDrawInfo(annotate);
310 textlist=(char **) RelinquishMagickMemory(textlist);
311 text=DestroyString(text);
312 return(MagickFalse);
313 }
314 if (IsGrayColorspace(image->colorspace) != MagickFalse)
315 (void) SetImageColorspace(image,sRGBColorspace);
316 status=MagickTrue;
317 (void) memset(&metrics,0,sizeof(metrics));
318 for (i=0; textlist[i] != (char *) NULL; i++)
319 {
320 if (*textlist[i] == '\0')
321 continue;
322 /*
323 Position text relative to image.
324 */
325 annotate_info->affine.tx=geometry_info.xi-image->page.x;
326 annotate_info->affine.ty=geometry_info.psi-image->page.y;
327 (void) CloneString(&annotate->text,textlist[i]);
328 if ((metrics.width == 0) || (annotate->gravity != NorthWestGravity))
329 (void) GetTypeMetrics(image,annotate,&metrics);
330 height=CastDoubleToUnsigned(metrics.ascent-metrics.descent+0.5);
331 if (height == 0)
332 height=draw_info->pointsize;
333 height+=(size_t) floor(draw_info->interline_spacing+0.5);
334 switch (annotate->gravity)
335 {
336 case UndefinedGravity:
337 default:
338 {
339 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
340 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
341 break;
342 }
343 case (GravityType) NorthWestGravity:
344 {
345 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
346 annotate_info->affine.ry*height+annotate_info->affine.ry*
347 (metrics.ascent+metrics.descent);
348 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
349 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
350 annotate_info->affine.sy*metrics.ascent;
351 break;
352 }
353 case (GravityType) NorthGravity:
354 {
355 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
356 geometry.width/2.0+i*annotate_info->affine.ry*height-
357 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
358 (metrics.ascent+metrics.descent);
359 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
360 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
361 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
362 metrics.width/2.0;
363 break;
364 }
365 case (GravityType) NorthEastGravity:
366 {
367 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
368 geometry.width+i*annotate_info->affine.ry*height-
369 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
370 (metrics.ascent+metrics.descent)-1.0;
371 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
372 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
373 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
374 metrics.width;
375 break;
376 }
377 case (GravityType) WestGravity:
378 {
379 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
380 annotate_info->affine.ry*height+annotate_info->affine.ry*
381 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
382 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
383 geometry.height/2.0+i*annotate_info->affine.sy*height+
384 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
385 (number_lines-1.0)*height)/2.0;
386 break;
387 }
388 case (GravityType) StaticGravity:
389 case (GravityType) CenterGravity:
390 {
391 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
392 geometry.width/2.0+i*annotate_info->affine.ry*height-
393 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
394 (metrics.ascent+metrics.descent-(number_lines-1)*height)/2.0;
395 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
396 geometry.height/2.0+i*annotate_info->affine.sy*height-
397 annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
398 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
399 break;
400 }
401 case (GravityType) EastGravity:
402 {
403 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
404 geometry.width+i*annotate_info->affine.ry*height-
405 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
406 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0-1.0;
407 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
408 geometry.height/2.0+i*annotate_info->affine.sy*height-
409 annotate_info->affine.rx*metrics.width+annotate_info->affine.sy*
410 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
411 break;
412 }
413 case (GravityType) SouthWestGravity:
414 {
415 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
416 annotate_info->affine.ry*height-annotate_info->affine.ry*
417 (number_lines-1.0)*height;
418 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
419 geometry.height+i*annotate_info->affine.sy*height-
420 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
421 break;
422 }
423 case (GravityType) SouthGravity:
424 {
425 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
426 geometry.width/2.0+i*annotate_info->affine.ry*height-
427 annotate_info->affine.sx*metrics.width/2.0-annotate_info->affine.ry*
428 (number_lines-1.0)*height/2.0;
429 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
430 geometry.height+i*annotate_info->affine.sy*height-
431 annotate_info->affine.rx*metrics.width/2.0-annotate_info->affine.sy*
432 (number_lines-1.0)*height+metrics.descent;
433 break;
434 }
435 case (GravityType) SouthEastGravity:
436 {
437 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
438 geometry.width+i*annotate_info->affine.ry*height-
439 annotate_info->affine.sx*metrics.width-annotate_info->affine.ry*
440 (number_lines-1.0)*height-1.0;
441 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
442 geometry.height+i*annotate_info->affine.sy*height-
443 annotate_info->affine.rx*metrics.width-annotate_info->affine.sy*
444 (number_lines-1.0)*height+metrics.descent;
445 break;
446 }
447 }
448 switch (annotate->align)
449 {
450 case LeftAlign:
451 {
452 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
453 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
454 break;
455 }
456 case CenterAlign:
457 {
458 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
459 annotate_info->affine.sx*metrics.width/2.0;
460 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
461 annotate_info->affine.rx*metrics.width/2.0;
462 break;
463 }
464 case RightAlign:
465 {
466 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
467 annotate_info->affine.sx*metrics.width;
468 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
469 annotate_info->affine.rx*metrics.width;
470 break;
471 }
472 default:
473 break;
474 }
475 if (draw_info->undercolor.opacity != TransparentOpacity)
476 {
477 DrawInfo
478 *undercolor_info;
479
480 /*
481 Text box.
482 */
483 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
484 undercolor_info->fill=draw_info->undercolor;
485 undercolor_info->affine=draw_info->affine;
486 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
487 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
488 (void) FormatLocaleString(primitive,MaxTextExtent,
489 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(double) height);
490 (void) CloneString(&undercolor_info->primitive,primitive);
491 (void) DrawImage(image,undercolor_info);
492 (void) DestroyDrawInfo(undercolor_info);
493 }
494 annotate_info->affine.tx=offset.x;
495 annotate_info->affine.ty=offset.y;
496 pixel=annotate_info->fill;
497 if (annotate_info->stroke.opacity != TransparentOpacity)
498 pixel=annotate_info->stroke;
499 (void) QueryColorname(image,&pixel,SVGCompliance,color,&image->exception);
500 (void) FormatLocaleString(primitive,MagickPathExtent,"stroke %s "
501 "stroke-width %g line 0,0 %g,0",color,(double)
502 metrics.underline_thickness,(double) metrics.width);
503 /*
504 Annotate image with text.
505 */
506 switch (annotate->decorate)
507 {
508 case OverlineDecoration:
509 {
510 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
511 metrics.descent-metrics.underline_position));
512 (void) CloneString(&annotate_info->primitive,primitive);
513 (void) DrawImage(image,annotate_info);
514 break;
515 }
516 case UnderlineDecoration:
517 {
518 annotate_info->affine.ty-=(draw_info->affine.sy*
519 metrics.underline_position);
520 (void) CloneString(&annotate_info->primitive,primitive);
521 (void) DrawImage(image,annotate_info);
522 break;
523 }
524 default:
525 break;
526 }
527 status=RenderType(image,annotate,&offset,&metrics);
528 if (status == MagickFalse)
529 break;
530 if (annotate->decorate == LineThroughDecoration)
531 {
532 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
533 metrics.underline_position+metrics.descent*2)/2.0);
534 (void) CloneString(&annotate_info->primitive,primitive);
535 (void) DrawImage(image,annotate_info);
536 }
537 }
538 /*
539 Relinquish resources.
540 */
541 annotate_info=DestroyDrawInfo(annotate_info);
542 annotate=DestroyDrawInfo(annotate);
543 textlist=(char **) RelinquishMagickMemory(textlist);
544 text=DestroyString(text);
545 return(status);
546}
547
548/*
549%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550% %
551% %
552% %
553% F o r m a t M a g i c k C a p t i o n %
554% %
555% %
556% %
557%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558%
559% FormatMagickCaption() formats a caption so that it fits within the image
560% width. It returns the number of lines in the formatted caption.
561%
562% The format of the FormatMagickCaption method is:
563%
564% ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
565% const MagickBooleanType split,TypeMetric *metrics,char **caption)
566%
567% A description of each parameter follows.
568%
569% o image: The image.
570%
571% o draw_info: the draw info.
572%
573% o split: when no convenient line breaks-- insert newline.
574%
575% o metrics: Return the font metrics in this structure.
576%
577% o caption: the caption.
578%
579*/
580
581static inline char *ReplaceSpaceWithNewline(char **caption,char *space)
582{
583 size_t
584 octets;
585
586 if ((caption == (char **) NULL) || (space == (char *) NULL))
587 return((char *) NULL);
588 octets=(size_t) GetUTFOctets(space);
589 if (octets == 1)
590 *space='\n';
591 else
592 {
593 char
594 *target;
595
596 size_t
597 length;
598
599 ssize_t
600 offset;
601
602 length=strlen(*caption);
603 *space='\n';
604 offset=space-(*caption);
605 if (offset >= 0)
606 {
607 target=AcquireString(*caption);
608 CopyMagickString(target,*caption,(size_t) offset+2);
609 ConcatenateMagickString(target,space+octets,length);
610 (void) DestroyString(*caption);
611 *caption=target;
612 space=(*caption)+offset;
613 }
614 }
615 return(space);
616}
617
618MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
619 const MagickBooleanType split,TypeMetric *metrics,char **caption)
620{
621 char
622 *p,
623 *q,
624 *s;
625
626 MagickBooleanType
627 status;
628
629 size_t
630 width;
631
632 ssize_t
633 i,
634 n;
635
636 if ((caption == (char **) NULL) || (*caption == (char *) NULL))
637 return(-1);
638 q=draw_info->text;
639 s=(char *) NULL;
640 width=0;
641 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
642 {
643 int
644 code;
645
646 code=GetUTFCode(p);
647 if (code == '\n')
648 {
649 q=draw_info->text;
650 continue;
651 }
652 if ((IsUTFSpace(code) != MagickFalse) &&
653 (IsNonBreakingUTFSpace(code) == MagickFalse))
654 {
655 s=p;
656 if (width > image->columns)
657 p=ReplaceSpaceWithNewline(caption,s);
658 }
659 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
660 *q++=(*(p+i));
661 *q='\0';
662 status=GetTypeMetrics(image,draw_info,metrics);
663 if (status == MagickFalse)
664 break;
665 width=CastDoubleToUnsigned(metrics->width+draw_info->stroke_width+0.5);
666 if (width <= image->columns)
667 continue;
668 if (s != (char *) NULL)
669 p=ReplaceSpaceWithNewline(caption,s);
670 else
671 if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
672 {
673 /*
674 No convenient line breaks-- insert newline.
675 */
676 n=p-(*caption);
677 if ((n > 0) && ((*caption)[n-1] != '\n'))
678 {
679 char
680 *target;
681
682 target=AcquireString(*caption);
683 CopyMagickString(target,*caption,(size_t) n+1);
684 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
685 ConcatenateMagickString(target,p,strlen(*caption)+2);
686 (void) DestroyString(*caption);
687 *caption=target;
688 p=(*caption)+n;
689 }
690 }
691 q=draw_info->text;
692 s=(char *) NULL;
693 }
694 n=0;
695 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
696 if (GetUTFCode(p) == '\n')
697 n++;
698 return(n);
699}
700
701/*
702%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703% %
704% %
705% %
706% G e t M u l t i l i n e T y p e M e t r i c s %
707% %
708% %
709% %
710%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711%
712% GetMultilineTypeMetrics() returns the following information for the
713% specified font and text:
714%
715% character width
716% character height
717% ascender
718% descender
719% text width
720% text height
721% maximum horizontal advance
722% bounds: x1
723% bounds: y1
724% bounds: x2
725% bounds: y2
726% origin: x
727% origin: y
728% underline position
729% underline thickness
730%
731% This method is like GetTypeMetrics() but it returns the maximum text width
732% and height for multiple lines of text.
733%
734% The format of the GetMultilineTypeMetrics method is:
735%
736% MagickBooleanType GetMultilineTypeMetrics(Image *image,
737% const DrawInfo *draw_info,TypeMetric *metrics)
738%
739% A description of each parameter follows:
740%
741% o image: the image.
742%
743% o draw_info: the draw info.
744%
745% o metrics: Return the font metrics in this structure.
746%
747*/
748MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
749 const DrawInfo *draw_info,TypeMetric *metrics)
750{
751 char
752 **textlist;
753
754 double
755 height;
756
757 DrawInfo
758 *annotate_info;
759
760 MagickBooleanType
761 status;
762
763 MagickSizeType
764 size;
765
766 ssize_t
767 i;
768
769 size_t
770 count;
771
772 TypeMetric
773 extent;
774
775 assert(image != (Image *) NULL);
776 assert(image->signature == MagickCoreSignature);
777 if (IsEventLogging() != MagickFalse)
778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
779 assert(draw_info != (DrawInfo *) NULL);
780 assert(draw_info->text != (char *) NULL);
781 assert(draw_info->signature == MagickCoreSignature);
782 if (*draw_info->text == '\0')
783 {
784 (void) ThrowMagickException(&image->exception,GetMagickModule(),
785 OptionError,"LabelExpected","`%s'",image->filename);
786 return(MagickFalse);
787 }
788 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
789 annotate_info->text=DestroyString(annotate_info->text);
790 /*
791 Convert newlines to multiple lines of text.
792 */
793 textlist=StringToStrings(draw_info->text,&count);
794 if (textlist == (char **) NULL)
795 {
796 annotate_info=DestroyDrawInfo(annotate_info);
797 return(MagickFalse);
798 }
799 annotate_info->render=MagickFalse;
800 annotate_info->direction=UndefinedDirection;
801 (void) memset(metrics,0,sizeof(*metrics));
802 (void) memset(&extent,0,sizeof(extent));
803 /*
804 Find the widest of the text lines.
805 */
806 annotate_info->text=textlist[0];
807 status=GetTypeMetrics(image,annotate_info,&extent);
808 *metrics=extent;
809 height=(count*(size_t) (metrics->ascent-metrics->descent+
810 0.5)+(count-1)*draw_info->interline_spacing);
811 size=(MagickSizeType) fabs(height);
812 if (AcquireMagickResource(HeightResource,size) == MagickFalse)
813 {
814 (void) ThrowMagickException(&image->exception,GetMagickModule(),
815 ImageError,"WidthOrHeightExceedsLimit","`%s'",image->filename);
816 status=MagickFalse;
817 }
818 else
819 {
820 for (i=1; i < (ssize_t) count; i++)
821 {
822 annotate_info->text=textlist[i];
823 status=GetTypeMetrics(image,annotate_info,&extent);
824 if (status == MagickFalse)
825 break;
826 if (extent.width > metrics->width)
827 *metrics=extent;
828 size=(MagickSizeType) fabs(extent.width);
829 if (AcquireMagickResource(WidthResource,size) == MagickFalse)
830 {
831 (void) ThrowMagickException(&image->exception,GetMagickModule(),
832 ImageError,"WidthOrHeightExceedsLimit","`%s'",image->filename);
833 status=MagickFalse;
834 break;
835 }
836 }
837 metrics->height=(double) height;
838 }
839 /*
840 Relinquish resources.
841 */
842 annotate_info->text=(char *) NULL;
843 annotate_info=DestroyDrawInfo(annotate_info);
844 for (i=0; i < (ssize_t) count; i++)
845 textlist[i]=DestroyString(textlist[i]);
846 textlist=(char **) RelinquishMagickMemory(textlist);
847 return(status);
848}
849
850/*
851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852% %
853% %
854% %
855% G e t T y p e M e t r i c s %
856% %
857% %
858% %
859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860%
861% GetTypeMetrics() returns the following information for the specified font
862% and text:
863%
864% character width
865% character height
866% ascender
867% descender
868% text width
869% text height
870% maximum horizontal advance
871% bounds: x1
872% bounds: y1
873% bounds: x2
874% bounds: y2
875% origin: x
876% origin: y
877% underline position
878% underline thickness
879%
880% The format of the GetTypeMetrics method is:
881%
882% MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
883% TypeMetric *metrics)
884%
885% A description of each parameter follows:
886%
887% o image: the image.
888%
889% o draw_info: the draw info.
890%
891% o metrics: Return the font metrics in this structure.
892%
893*/
894MagickExport MagickBooleanType GetTypeMetrics(Image *image,
895 const DrawInfo *draw_info,TypeMetric *metrics)
896{
897 DrawInfo
898 *annotate_info;
899
900 MagickBooleanType
901 status;
902
903 PointInfo
904 offset;
905
906 assert(image != (Image *) NULL);
907 assert(image->signature == MagickCoreSignature);
908 if (IsEventLogging() != MagickFalse)
909 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
910 assert(draw_info != (DrawInfo *) NULL);
911 assert(draw_info->text != (char *) NULL);
912 assert(draw_info->signature == MagickCoreSignature);
913 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
914 annotate_info->render=MagickFalse;
915 annotate_info->direction=UndefinedDirection;
916 (void) memset(metrics,0,sizeof(*metrics));
917 offset.x=0.0;
918 offset.y=0.0;
919 status=RenderType(image,annotate_info,&offset,metrics);
920 if (draw_info->debug != MagickFalse)
921 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
922 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
923 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
924 "underline position: %g; underline thickness: %g",annotate_info->text,
925 metrics->width,metrics->height,metrics->ascent,metrics->descent,
926 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
927 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
928 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
929 metrics->underline_position,metrics->underline_thickness);
930 annotate_info=DestroyDrawInfo(annotate_info);
931 return(status);
932}
933
934/*
935%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
936% %
937% %
938% %
939+ R e n d e r T y p e %
940% %
941% %
942% %
943%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944%
945% RenderType() renders text on the image. It also returns the bounding box of
946% the text relative to the image.
947%
948% The format of the RenderType method is:
949%
950% MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
951% const PointInfo *offset,TypeMetric *metrics)
952%
953% A description of each parameter follows:
954%
955% o image: the image.
956%
957% o draw_info: the draw info.
958%
959% o offset: (x,y) location of text relative to image.
960%
961% o metrics: bounding box of text.
962%
963*/
964static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
965 const PointInfo *offset,TypeMetric *metrics)
966{
967 char
968 *font;
969
970 const TypeInfo
971 *type_info;
972
973 DrawInfo
974 *annotate_info;
975
976 ExceptionInfo
977 *sans_exception;
978
979 MagickBooleanType
980 status;
981
982 type_info=(const TypeInfo *) NULL;
983 if (draw_info->font != (char *) NULL)
984 {
985 if (*draw_info->font == '@')
986 {
987 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
988 metrics);
989 return(status);
990 }
991 if (*draw_info->font == '-')
992 return(RenderX11(image,draw_info,offset,metrics));
993 if (*draw_info->font == '^')
994 return(RenderPostscript(image,draw_info,offset,metrics));
995 if (IsPathAccessible(draw_info->font) != MagickFalse)
996 {
997 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
998 metrics);
999 return(status);
1000 }
1001 type_info=GetTypeInfo(draw_info->font,&image->exception);
1002 if (type_info == (const TypeInfo *) NULL)
1003 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1004 TypeWarning,"UnableToReadFont","`%s'",draw_info->font);
1005 }
1006 if ((type_info == (const TypeInfo *) NULL) &&
1007 (draw_info->family != (const char *) NULL))
1008 {
1009 if (strpbrk(draw_info->family,",'\"") == (char *) NULL)
1010 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
1011 draw_info->stretch,draw_info->weight,&image->exception);
1012 if (type_info == (const TypeInfo *) NULL)
1013 {
1014 char
1015 **family;
1016
1017 int
1018 number_families;
1019
1020 ssize_t
1021 i;
1022
1023 /*
1024 Parse font family list.
1025 */
1026 family=StringToArgv(draw_info->family,&number_families);
1027 for (i=1; i < (ssize_t) number_families; i++)
1028 {
1029 (void) SubstituteString(&family[i],",","");
1030 type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1031 draw_info->stretch,draw_info->weight,&image->exception);
1032 if ((type_info != (const TypeInfo *) NULL) &&
1033 (LocaleCompare(family[i],type_info->family) == 0))
1034 break;
1035 }
1036 for (i=0; i < (ssize_t) number_families; i++)
1037 family[i]=DestroyString(family[i]);
1038 family=(char **) RelinquishMagickMemory(family);
1039 if (type_info == (const TypeInfo *) NULL)
1040 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1041 TypeWarning,"UnableToReadFont","`%s'",draw_info->family);
1042 }
1043 }
1044 font=GetPolicyValue("system:font");
1045 if (font != (char *) NULL)
1046 {
1047 if (IsPathAccessible(font) != MagickFalse)
1048 {
1049 /*
1050 Render with default system font.
1051 */
1052 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1053 annotate_info->font=font;
1054 status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1055 offset,metrics);
1056 annotate_info=DestroyDrawInfo(annotate_info);
1057 return(status);
1058 }
1059 font=DestroyString(font);
1060 }
1061 sans_exception=AcquireExceptionInfo();
1062 if (type_info == (const TypeInfo *) NULL)
1063 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
1064 draw_info->stretch,draw_info->weight,sans_exception);
1065 if (type_info == (const TypeInfo *) NULL)
1066 type_info=GetTypeInfo("*",sans_exception);
1067 sans_exception=DestroyExceptionInfo(sans_exception);
1068 if (type_info == (const TypeInfo *) NULL)
1069 {
1070 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics);
1071 return(status);
1072 }
1073 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1074 annotate_info->face=type_info->face;
1075 if (type_info->metrics != (char *) NULL)
1076 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1077 if (type_info->glyphs != (char *) NULL)
1078 (void) CloneString(&annotate_info->font,type_info->glyphs);
1079 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics);
1080 annotate_info=DestroyDrawInfo(annotate_info);
1081 return(status);
1082}
1083
1084/*
1085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1086% %
1087% %
1088% %
1089+ R e n d e r F r e e t y p e %
1090% %
1091% %
1092% %
1093%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1094%
1095% RenderFreetype() renders text on the image with a Truetype font. It also
1096% returns the bounding box of the text relative to the image.
1097%
1098% The format of the RenderFreetype method is:
1099%
1100% MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
1101% const char *encoding,const PointInfo *offset,TypeMetric *metrics)
1102%
1103% A description of each parameter follows:
1104%
1105% o image: the image.
1106%
1107% o draw_info: the draw info.
1108%
1109% o encoding: the font encoding.
1110%
1111% o offset: (x,y) location of text relative to image.
1112%
1113% o metrics: bounding box of text.
1114%
1115*/
1116
1117#if defined(MAGICKCORE_FREETYPE_DELEGATE)
1118
1119#if defined(MAGICKCORE_RAQM_DELEGATE)
1120static size_t ComplexRaqmTextLayout(const Image *image,
1121 const DrawInfo *draw_info,const char *text,const size_t length,
1122 const FT_Face face,GraphemeInfo **grapheme,ExceptionInfo *exception)
1123{
1124 const char
1125 *features;
1126
1127 raqm_t
1128 *rq;
1129
1130 raqm_glyph_t
1131 *glyphs;
1132
1133 size_t
1134 extent;
1135
1136 ssize_t
1137 i;
1138
1139 (void) exception;
1140 extent=0;
1141 rq=raqm_create();
1142 if (rq == (raqm_t *) NULL)
1143 goto cleanup;
1144 if (raqm_set_text_utf8(rq,text,length) == 0)
1145 goto cleanup;
1146 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1147 goto cleanup;
1148 if (raqm_set_freetype_face(rq,face) == 0)
1149 goto cleanup;
1150 features=GetImageProperty(image,"type:features");
1151 if (features != (const char *) NULL)
1152 {
1153 char
1154 breaker,
1155 quote,
1156 *token;
1157
1158 int
1159 next,
1160 status_token;
1161
1162 TokenInfo
1163 *token_info;
1164
1165 next=0;
1166 token_info=AcquireTokenInfo();
1167 token=AcquireString("");
1168 status_token=Tokenizer(token_info,0,token,50,features,"",",","",'\0',
1169 &breaker,&next,&quote);
1170 while (status_token == 0)
1171 {
1172 raqm_add_font_feature(rq,token,(int) strlen(token));
1173 status_token=Tokenizer(token_info,0,token,50,features,"",",","",'\0',
1174 &breaker,&next,&quote);
1175 }
1176 token_info=DestroyTokenInfo(token_info);
1177 token=DestroyString(token);
1178 }
1179 if (raqm_layout(rq) == 0)
1180 goto cleanup;
1181 glyphs=raqm_get_glyphs(rq,&extent);
1182 if (glyphs == (raqm_glyph_t *) NULL)
1183 {
1184 extent=0;
1185 goto cleanup;
1186 }
1187 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(extent,sizeof(**grapheme));
1188 if (*grapheme == (GraphemeInfo *) NULL)
1189 {
1190 extent=0;
1191 goto cleanup;
1192 }
1193 for (i=0; i < (ssize_t) extent; i++)
1194 {
1195 (*grapheme)[i].index=glyphs[i].index;
1196 (*grapheme)[i].x_offset=(size_t) glyphs[i].x_offset;
1197 (*grapheme)[i].x_advance=(size_t) glyphs[i].x_advance;
1198 (*grapheme)[i].y_offset=(size_t) glyphs[i].y_offset;
1199 (*grapheme)[i].y_advance=(size_t) glyphs[i].y_advance;
1200 (*grapheme)[i].cluster=glyphs[i].cluster;
1201 }
1202
1203cleanup:
1204 raqm_destroy(rq);
1205 return(extent);
1206#else
1207static size_t ComplexTextLayout(const DrawInfo *draw_info,const char *text,
1208 const size_t length,const FT_Face face,const FT_Int32 flags,
1209 GraphemeInfo **grapheme)
1210{
1211 const char
1212 *p;
1213
1214 ssize_t
1215 i;
1216
1217 ssize_t
1218 last_glyph;
1219
1220 /*
1221 Simple layout for bi-directional text (right-to-left or left-to-right).
1222 */
1223 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(length+1,sizeof(**grapheme));
1224 if (*grapheme == (GraphemeInfo *) NULL)
1225 return(0);
1226 last_glyph=0;
1227 p=text;
1228 for (i=0; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p), i++)
1229 {
1230 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,GetUTFCode(p));
1231 (*grapheme)[i].x_offset=0;
1232 (*grapheme)[i].y_offset=0;
1233 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1234 {
1235 if (FT_HAS_KERNING(face))
1236 {
1237 FT_Error
1238 ft_status;
1239
1240 FT_Vector
1241 kerning;
1242
1243 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1244 (*grapheme)[i].index,ft_kerning_default,&kerning);
1245 if (ft_status == 0)
1246 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1247 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1248 }
1249 }
1250 (void) FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1251 (*grapheme)[i].x_advance=face->glyph->advance.x;
1252 (*grapheme)[i].y_advance=face->glyph->advance.y;
1253 (*grapheme)[i].cluster=p-text;
1254 last_glyph=(*grapheme)[i].index;
1255 }
1256 return((size_t) i);
1257#endif
1258}
1259
1260static void FTCloseStream(FT_Stream stream)
1261{
1262 FILE *file = (FILE *) stream->descriptor.pointer;
1263 if (file != (FILE *) NULL)
1264 (void) fclose(file);
1265 stream->descriptor.pointer=NULL;
1266}
1267
1268static unsigned long FTReadStream(FT_Stream stream,unsigned long offset,
1269 unsigned char *buffer,unsigned long count)
1270{
1271 FILE *file = (FILE *) stream->descriptor.pointer;
1272 if (file == (FILE *) NULL)
1273 return(0);
1274 if (count == 0) /* seek operation */
1275 {
1276 if (offset > stream->size)
1277 return(1);
1278
1279 return((unsigned long) fseek(file,(off_t) offset,SEEK_SET));
1280 }
1281 return((unsigned long) fread(buffer,1,count,file));
1282}
1283
1284static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1285{
1286 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1287 MagickFalse);
1288}
1289
1290static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1291 DrawInfo *draw_info)
1292{
1293 AffineMatrix
1294 affine;
1295
1296 char
1297 path[MaxTextExtent];
1298
1299 affine=draw_info->affine;
1300 (void) FormatLocaleString(path,MaxTextExtent,"C%g,%g %g,%g %g,%g",affine.tx+
1301 p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,
1302 affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1303 (void) ConcatenateString(&draw_info->primitive,path);
1304 return(0);
1305}
1306
1307static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1308{
1309 AffineMatrix
1310 affine;
1311
1312 char
1313 path[MaxTextExtent];
1314
1315 affine=draw_info->affine;
1316 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+to->x/64.0,
1317 affine.ty-to->y/64.0);
1318 (void) ConcatenateString(&draw_info->primitive,path);
1319 return(0);
1320}
1321
1322static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1323{
1324 AffineMatrix
1325 affine;
1326
1327 char
1328 path[MaxTextExtent];
1329
1330 affine=draw_info->affine;
1331 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+to->x/64.0,
1332 affine.ty-to->y/64.0);
1333 (void) ConcatenateString(&draw_info->primitive,path);
1334 return(0);
1335}
1336
1337static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1338 DrawInfo *draw_info)
1339{
1340 AffineMatrix
1341 affine;
1342
1343 char
1344 path[MaxTextExtent];
1345
1346 affine=draw_info->affine;
1347 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",affine.tx+
1348 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1349 to->y/64.0);
1350 (void) ConcatenateString(&draw_info->primitive,path);
1351 return(0);
1352}
1353
1354static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1355{
1356#if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1357 return(FT_Error_String(ft_status));
1358#else
1359 magick_unreferenced(ft_status);
1360 return((const char *) NULL);
1361#endif
1362}
1363
1364static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1365 const char *encoding,const PointInfo *offset,TypeMetric *metrics)
1366{
1367#if !defined(FT_OPEN_PATHNAME)
1368#define FT_OPEN_PATHNAME ft_open_pathname
1369#endif
1370
1371#define ThrowFreetypeErrorException(tag,ft_status,value) \
1372{ \
1373 const char \
1374 *error_string=FreetypeErrorMessage(ft_status); \
1375 if (error_string != (const char *) NULL) \
1376 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1377 tag,"`%s (%s)'",value, error_string); \
1378 else \
1379 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1380 tag,"`%s'",value); \
1381}
1382
1383 typedef struct _GlyphInfo
1384 {
1385 FT_UInt
1386 id;
1387
1388 FT_Vector
1389 origin;
1390
1391 FT_Glyph
1392 image;
1393 } GlyphInfo;
1394
1395 char
1396 *p;
1397
1398 const char
1399 *value;
1400
1401 DrawInfo
1402 *annotate_info;
1403
1404 ExceptionInfo
1405 *exception;
1406
1407 FT_BBox
1408 bounds;
1409
1410 FT_Encoding
1411 encoding_type;
1412
1413 FT_Error
1414 ft_status;
1415
1416 FT_Face
1417 face;
1418
1419 FT_Int32
1420 flags;
1421
1422 FT_Library
1423 library;
1424
1425 FT_Long
1426 face_index;
1427
1428 FT_Matrix
1429 affine;
1430
1431 FT_Open_Args
1432 args;
1433
1434 FT_StreamRec
1435 *stream;
1436
1437 FT_UInt
1438 first_glyph_id,
1439 last_glyph_id,
1440 missing_glyph_id;
1441
1442 FT_Vector
1443 origin;
1444
1445 GlyphInfo
1446 glyph;
1447
1448 GraphemeInfo
1449 *grapheme;
1450
1451 MagickBooleanType
1452 status;
1453
1454 PointInfo
1455 resolution;
1456
1457 ssize_t
1458 i;
1459
1460 size_t
1461 length;
1462
1463 ssize_t
1464 code,
1465 last_character,
1466 y;
1467
1468 static FT_Outline_Funcs
1469 OutlineMethods =
1470 {
1471 (FT_Outline_MoveTo_Func) TraceMoveTo,
1472 (FT_Outline_LineTo_Func) TraceLineTo,
1473 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1474 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1475 0, 0
1476 };
1477
1478 struct stat
1479 attributes;
1480
1481 unsigned char
1482 *utf8;
1483
1484 /*
1485 Initialize Truetype library.
1486 */
1487 exception=(&image->exception);
1488 ft_status=FT_Init_FreeType(&library);
1489 if (ft_status != 0)
1490 ThrowFreetypeErrorException("UnableToInitializeFreetypeLibrary",ft_status,
1491 image->filename);
1492 face_index=(FT_Long) draw_info->face;
1493 /*
1494 Open font face.
1495 */
1496 (void) memset(&args,0,sizeof(args));
1497 if (draw_info->font == (char *) NULL)
1498 {
1499 const TypeInfo *type_info = GetTypeInfo("*",exception);
1500 if (type_info != (const TypeInfo *) NULL)
1501 args.pathname=ConstantString(type_info->glyphs);
1502 }
1503 else
1504 if (*draw_info->font != '@')
1505 args.pathname=ConstantString(draw_info->font);
1506 else
1507 {
1508 /*
1509 Extract face index, e.g. @msgothic[1].
1510 */
1511 ImageInfo *image_info=AcquireImageInfo();
1512 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1513 MagickPathExtent);
1514 (void) SetImageInfo(image_info,0,exception);
1515 face_index=(FT_Long) image_info->scene;
1516 args.pathname=ConstantString(image_info->filename);
1517 image_info=DestroyImageInfo(image_info);
1518 }
1519 /*
1520 Configure streaming interface.
1521 */
1522 stream=(FT_StreamRec *) AcquireCriticalMemory(sizeof(*stream));
1523 (void) memset(stream,0,sizeof(*stream));
1524 if (stat(args.pathname,&attributes) == 0)
1525 stream->size=attributes.st_size >= 0 ? (unsigned long)
1526 attributes.st_size : 0;
1527 stream->descriptor.pointer=fopen_utf8(args.pathname,"rb");
1528 stream->read=(&FTReadStream);
1529 stream->close=(&FTCloseStream);
1530 args.flags=FT_OPEN_STREAM;
1531 args.stream=stream;
1532 face=(FT_Face) NULL;
1533 ft_status=FT_Open_Face(library,&args,face_index,&face);
1534 if (ft_status != 0)
1535 {
1536 (void) FT_Done_FreeType(library);
1537 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1538 ThrowFreetypeErrorException("UnableToReadFont",ft_status,args.pathname);
1539 args.pathname=DestroyString(args.pathname);
1540 return(MagickFalse);
1541 }
1542 args.pathname=DestroyString(args.pathname);
1543 if ((draw_info->metrics != (char *) NULL) &&
1544 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1545 (void) FT_Attach_File(face,draw_info->metrics);
1546 encoding_type=FT_ENCODING_UNICODE;
1547 ft_status=FT_Select_Charmap(face,encoding_type);
1548 if ((ft_status != 0) && (face->num_charmaps != 0))
1549 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1550 if (encoding != (const char *) NULL)
1551 {
1552 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1553 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1554 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1555 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1556 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1557 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1558 if (LocaleCompare(encoding,"AppleRoman") == 0)
1559 encoding_type=FT_ENCODING_APPLE_ROMAN;
1560 if (LocaleCompare(encoding,"BIG5") == 0)
1561 encoding_type=FT_ENCODING_BIG5;
1562#if defined(FT_ENCODING_PRC)
1563 if (LocaleCompare(encoding,"GB2312") == 0)
1564 encoding_type=FT_ENCODING_PRC;
1565#endif
1566#if defined(FT_ENCODING_JOHAB)
1567 if (LocaleCompare(encoding,"Johab") == 0)
1568 encoding_type=FT_ENCODING_JOHAB;
1569#endif
1570#if defined(FT_ENCODING_ADOBE_LATIN_1)
1571 if (LocaleCompare(encoding,"Latin-1") == 0)
1572 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1573#endif
1574#if defined(FT_ENCODING_ADOBE_LATIN_2)
1575 if (LocaleCompare(encoding,"Latin-2") == 0)
1576 encoding_type=FT_ENCODING_OLD_LATIN_2;
1577#endif
1578 if (LocaleCompare(encoding,"None") == 0)
1579 encoding_type=FT_ENCODING_NONE;
1580 if (LocaleCompare(encoding,"SJIScode") == 0)
1581 encoding_type=FT_ENCODING_SJIS;
1582 if (LocaleCompare(encoding,"Symbol") == 0)
1583 encoding_type=FT_ENCODING_MS_SYMBOL;
1584 if (LocaleCompare(encoding,"Unicode") == 0)
1585 encoding_type=FT_ENCODING_UNICODE;
1586 if (LocaleCompare(encoding,"Wansung") == 0)
1587 encoding_type=FT_ENCODING_WANSUNG;
1588 ft_status=FT_Select_Charmap(face,encoding_type);
1589 if (ft_status != 0)
1590 {
1591 (void) FT_Done_Face(face);
1592 (void) FT_Done_FreeType(library);
1593 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1594 ThrowFreetypeErrorException("UnrecognizedFontEncoding",ft_status,
1595 encoding);
1596 return(MagickFalse);
1597 }
1598 }
1599 /*
1600 Set text size.
1601 */
1602 resolution.x=DefaultResolution;
1603 resolution.y=DefaultResolution;
1604 if (draw_info->density != (char *) NULL)
1605 {
1606 GeometryInfo
1607 geometry_info;
1608
1609 MagickStatusType
1610 flags;
1611
1612 flags=ParseGeometry(draw_info->density,&geometry_info);
1613 if ((flags & RhoValue) != 0)
1614 resolution.x=geometry_info.rho;
1615 resolution.y=resolution.x;
1616 if ((flags & SigmaValue) != 0)
1617 resolution.y=geometry_info.sigma;
1618 }
1619 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1620 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1621 (FT_UInt) resolution.y);
1622 if (ft_status != 0)
1623 {
1624 (void) FT_Done_Face(face);
1625 (void) FT_Done_FreeType(library);
1626 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1627 ThrowFreetypeErrorException("UnableToReadFont",ft_status,
1628 draw_info->font);
1629 return(MagickFalse);
1630 }
1631 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1632 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1633 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1634 metrics->descent=(double) face->size->metrics.descender/64.0;
1635 if (face->size->metrics.ascender == 0)
1636 {
1637 /*
1638 Sanitize buggy ascender and descender values.
1639 */
1640 metrics->ascent=face->size->metrics.y_ppem;
1641 if (face->size->metrics.descender == 0)
1642 metrics->descent=face->size->metrics.y_ppem/-3.5;
1643 }
1644 metrics->width=0;
1645 metrics->origin.x=0;
1646 metrics->origin.y=0;
1647 metrics->height=(double) face->size->metrics.height/64.0;
1648 metrics->max_advance=0.0;
1649 if (face->size->metrics.max_advance > MagickEpsilon)
1650 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1651 metrics->bounds.x1=0.0;
1652 metrics->bounds.y1=metrics->descent;
1653 metrics->bounds.x2=metrics->ascent+metrics->descent;
1654 metrics->bounds.y2=metrics->ascent+metrics->descent;
1655 metrics->underline_position=face->underline_position*
1656 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1657 metrics->underline_thickness=face->underline_thickness*
1658 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1659 first_glyph_id=0;
1660 FT_Get_First_Char(face,&first_glyph_id);
1661 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0') ||
1662 (first_glyph_id == 0))
1663 {
1664 (void) FT_Done_Face(face);
1665 (void) FT_Done_FreeType(library);
1666 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1667 return(MagickTrue);
1668 }
1669 /*
1670 Compute bounding box.
1671 */
1672 if (draw_info->debug != MagickFalse)
1673 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1674 "font-encoding %s; text-encoding %s; pointsize %g",
1675 draw_info->font != (char *) NULL ? draw_info->font : "none",
1676 encoding != (char *) NULL ? encoding : "none",
1677 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1678 draw_info->pointsize);
1679 flags=FT_LOAD_DEFAULT;
1680 if (draw_info->render == MagickFalse)
1681 flags=FT_LOAD_NO_BITMAP;
1682 if (draw_info->text_antialias == MagickFalse)
1683 flags|=FT_LOAD_TARGET_MONO;
1684 else
1685 {
1686#if defined(FT_LOAD_TARGET_LIGHT)
1687 flags|=FT_LOAD_TARGET_LIGHT;
1688#elif defined(FT_LOAD_TARGET_LCD)
1689 flags|=FT_LOAD_TARGET_LCD;
1690#endif
1691 }
1692 value=GetImageProperty(image,"type:hinting");
1693 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1694 flags|=FT_LOAD_NO_HINTING;
1695 glyph.id=0;
1696 glyph.image=(FT_Glyph) NULL;
1697 last_glyph_id=0;
1698 origin.x=0;
1699 origin.y=0;
1700 affine.xx=65536L;
1701 affine.yx=0L;
1702 affine.xy=0L;
1703 affine.yy=65536L;
1704 if (draw_info->render != MagickFalse)
1705 {
1706 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1707 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1708 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1709 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1710 }
1711 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1712 if (annotate_info->dash_pattern != (double *) NULL)
1713 annotate_info->dash_pattern[0]=0.0;
1714 (void) CloneString(&annotate_info->primitive,"path '");
1715 status=MagickTrue;
1716 if (draw_info->render != MagickFalse)
1717 {
1718 if (image->storage_class != DirectClass)
1719 (void) SetImageStorageClass(image,DirectClass);
1720 if (image->matte == MagickFalse)
1721 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1722 }
1723 for (p=draw_info->text; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
1724 if (GetUTFCode(p) < 0)
1725 break;
1726 utf8=(unsigned char *) NULL;
1727 if (GetUTFCode(p) == 0)
1728 p=draw_info->text;
1729 else
1730 {
1731 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1732 if (utf8 != (unsigned char *) NULL)
1733 p=(char *) utf8;
1734 }
1735 grapheme=(GraphemeInfo *) NULL;
1736#if defined(MAGICKCORE_RAQM_DELEGATE)
1737 length=ComplexRaqmTextLayout(image,draw_info,p,strlen(p),face,&grapheme,
1738 exception);
1739#else
1740 length=ComplexTextLayout(draw_info,p,strlen(p),face,flags,&grapheme);
1741#endif
1742 missing_glyph_id=FT_Get_Char_Index(face,' ');
1743 code=0;
1744 last_character=(ssize_t) length-1;
1745 for (i=0; i < (ssize_t) length; i++)
1746 {
1747 FT_BitmapGlyph
1748 bitmap;
1749
1750 FT_Outline
1751 outline;
1752
1753 PointInfo
1754 point;
1755
1756 /*
1757 Render UTF-8 sequence.
1758 */
1759 glyph.id=grapheme[i].index;
1760 if (glyph.id == 0)
1761 glyph.id=missing_glyph_id;
1762 if ((glyph.id != 0) && (last_glyph_id != 0))
1763 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1764 glyph.origin=origin;
1765 glyph.origin.x+=grapheme[i].x_offset;
1766 glyph.origin.y+=grapheme[i].y_offset;
1767 if (glyph.image != (FT_Glyph) NULL)
1768 {
1769 FT_Done_Glyph(glyph.image);
1770 glyph.image=(FT_Glyph) NULL;
1771 }
1772 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1773 if (ft_status != 0)
1774 continue;
1775 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1776 if (ft_status != 0)
1777 continue;
1778 outline=((FT_OutlineGlyph) glyph.image)->outline;
1779 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1780 (IsEmptyOutline(outline) == MagickFalse))
1781 continue;
1782 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1783 if (ft_status != 0)
1784 continue;
1785 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1786 metrics->bounds.x1=(double) bounds.xMin;
1787 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1788 metrics->bounds.y1=(double) bounds.yMin;
1789 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1790 metrics->bounds.x2=(double) bounds.xMax;
1791 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1792 metrics->bounds.y2=(double) bounds.yMax;
1793 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1794 (draw_info->stroke_pattern != (Image *) NULL)) &&
1795 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1796 {
1797 /*
1798 Trace the glyph.
1799 */
1800 annotate_info->affine.tx=glyph.origin.x/64.0;
1801 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1802 if (IsEmptyOutline(outline) == MagickFalse)
1803 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1804 annotate_info);
1805 }
1806 FT_Vector_Transform(&glyph.origin,&affine);
1807 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1808 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1809 (FT_Vector *) NULL,MagickTrue);
1810 if (ft_status != 0)
1811 continue;
1812 bitmap=(FT_BitmapGlyph) glyph.image;
1813 point.x=offset->x+bitmap->left;
1814 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1815 point.x+=(origin.x/64.0);
1816 point.y=offset->y-bitmap->top;
1817 if (draw_info->render != MagickFalse)
1818 {
1819 CacheView
1820 *image_view;
1821
1822 unsigned char
1823 *p;
1824
1825 MagickBooleanType
1826 transparent_fill;
1827
1828 /*
1829 Rasterize the glyph.
1830 */
1831 transparent_fill=((draw_info->fill.opacity == TransparentOpacity) &&
1832 (draw_info->fill_pattern == (Image *) NULL) &&
1833 (draw_info->stroke.opacity == TransparentOpacity) &&
1834 (draw_info->stroke_pattern == (Image *) NULL)) ? MagickTrue :
1835 MagickFalse;
1836 p=bitmap->bitmap.buffer;
1837 image_view=AcquireAuthenticCacheView(image,exception);
1838#if defined(MAGICKCORE_OPENMP_SUPPORT)
1839 #pragma omp parallel for schedule(static) shared(status) \
1840 magick_number_threads(image,image,bitmap->bitmap.rows,4)
1841#endif
1842 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1843 {
1844 MagickBooleanType
1845 active,
1846 sync;
1847
1848 MagickRealType
1849 fill_opacity;
1850
1851 PixelPacket
1852 fill_color,
1853 *magick_restrict q;
1854
1855 ssize_t
1856 x;
1857
1858 ssize_t
1859 n,
1860 x_offset,
1861 y_offset;
1862
1863 if (status == MagickFalse)
1864 continue;
1865 x_offset=CastDoubleToLong(ceil(point.x-0.5));
1866 y_offset=CastDoubleToLong(ceil(point.y+y-0.5));
1867 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1868 continue;
1869 q=(PixelPacket *) NULL;
1870 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1871 active=MagickFalse;
1872 else
1873 {
1874 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1875 bitmap->bitmap.width,1,exception);
1876 active=q != (PixelPacket *) NULL ? MagickTrue : MagickFalse;
1877 }
1878 n=y*bitmap->bitmap.pitch;
1879 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
1880 {
1881 x_offset=CastDoubleToLong(ceil(point.x+x-0.5));
1882 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1883 {
1884 if (q != (PixelPacket *) NULL)
1885 q++;
1886 continue;
1887 }
1888 fill_opacity=1.0;
1889 if (bitmap->bitmap.buffer != (unsigned char *) NULL)
1890 {
1891 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
1892 fill_opacity=(MagickRealType) (p[n])/(
1893 bitmap->bitmap.num_grays-1);
1894 else
1895 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1896 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1897 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1898 }
1899 if (draw_info->text_antialias == MagickFalse)
1900 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1901 if (active == MagickFalse)
1902 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1903 exception);
1904 if (q == (PixelPacket *) NULL)
1905 continue;
1906 if (transparent_fill == MagickFalse)
1907 {
1908 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
1909 fill_opacity=(double) QuantumRange-(double) fill_opacity*
1910 ((double) QuantumRange-(double) fill_color.opacity);
1911 MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q);
1912 }
1913 else
1914 {
1915 double
1916 Sa,
1917 Da;
1918
1919 Da=1.0-(QuantumScale*((double) QuantumRange-(double)
1920 q->opacity));
1921 Sa=fill_opacity;
1922 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*(double)
1923 QuantumRange;
1924 SetPixelAlpha(q,fill_opacity);
1925 }
1926 if (active == MagickFalse)
1927 {
1928 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1929 if (sync == MagickFalse)
1930 status=MagickFalse;
1931 }
1932 q++;
1933 }
1934 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1935 if (sync == MagickFalse)
1936 status=MagickFalse;
1937 }
1938 image_view=DestroyCacheView(image_view);
1939 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1940 (draw_info->stroke_pattern != (Image *) NULL)) &&
1941 (status != MagickFalse))
1942 {
1943 /*
1944 Draw text stroke.
1945 */
1946 annotate_info->linejoin=RoundJoin;
1947 annotate_info->affine.tx=offset->x;
1948 annotate_info->affine.ty=offset->y;
1949 (void) ConcatenateString(&annotate_info->primitive,"'");
1950 if (strlen(annotate_info->primitive) > 7)
1951 (void) DrawImage(image,annotate_info);
1952 (void) CloneString(&annotate_info->primitive,"path '");
1953 }
1954 }
1955 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1956 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
1957 (IsUTFSpace(code) == MagickFalse))
1958 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
1959 else
1960 if (i == last_character)
1961 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
1962 else
1963 origin.x+=(FT_Pos) grapheme[i].x_advance;
1964 origin.y+=(FT_Pos) grapheme[i].y_advance;
1965 metrics->origin.x=(double) origin.x;
1966 metrics->origin.y=(double) origin.y;
1967 if (metrics->origin.x > metrics->width)
1968 metrics->width=metrics->origin.x;
1969 last_glyph_id=glyph.id;
1970 code=GetUTFCode(p+grapheme[i].cluster);
1971 }
1972 if (grapheme != (GraphemeInfo *) NULL)
1973 grapheme=(GraphemeInfo *) RelinquishMagickMemory(grapheme);
1974 if (utf8 != (unsigned char *) NULL)
1975 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
1976 if (glyph.image != (FT_Glyph) NULL)
1977 {
1978 FT_Done_Glyph(glyph.image);
1979 glyph.image=(FT_Glyph) NULL;
1980 }
1981 /*
1982 Determine font metrics.
1983 */
1984 metrics->bounds.x1/=64.0;
1985 metrics->bounds.y1/=64.0;
1986 metrics->bounds.x2/=64.0;
1987 metrics->bounds.y2/=64.0;
1988 metrics->origin.x/=64.0;
1989 metrics->origin.y/=64.0;
1990 metrics->width/=64.0;
1991 /*
1992 Relinquish resources.
1993 */
1994 annotate_info=DestroyDrawInfo(annotate_info);
1995 (void) FT_Done_Face(face);
1996 (void) FT_Done_FreeType(library);
1997 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1998 return(status);
1999}
2000#else
2001static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
2002 const char *magick_unused(encoding),const PointInfo *offset,
2003 TypeMetric *metrics)
2004{
2005 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2006 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (Freetype)",
2007 draw_info->font != (char *) NULL ? draw_info->font : "none");
2008 return(RenderPostscript(image,draw_info,offset,metrics));
2009}
2010#endif
2011
2012/*
2013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2014% %
2015% %
2016% %
2017+ R e n d e r P o s t s c r i p t %
2018% %
2019% %
2020% %
2021%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2022%
2023% RenderPostscript() renders text on the image with a Postscript font. It
2024% also returns the bounding box of the text relative to the image.
2025%
2026% The format of the RenderPostscript method is:
2027%
2028% MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
2029% const PointInfo *offset,TypeMetric *metrics)
2030%
2031% A description of each parameter follows:
2032%
2033% o image: the image.
2034%
2035% o draw_info: the draw info.
2036%
2037% o offset: (x,y) location of text relative to image.
2038%
2039% o metrics: bounding box of text.
2040%
2041*/
2042
2043static char *EscapeParenthesis(const char *source)
2044{
2045 char
2046 *destination;
2047
2048 char
2049 *q;
2050
2051 const char
2052 *p;
2053
2054 size_t
2055 length;
2056
2057 assert(source != (const char *) NULL);
2058 length=0;
2059 for (p=source; *p != '\0'; p++)
2060 {
2061 if ((*p == '\\') || (*p == '(') || (*p == ')'))
2062 {
2063 if (~length < 1)
2064 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
2065 length++;
2066 }
2067 length++;
2068 }
2069 destination=(char *) NULL;
2070 if (~length >= (MaxTextExtent-1))
2071 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
2072 sizeof(*destination));
2073 if (destination == (char *) NULL)
2074 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
2075 *destination='\0';
2076 q=destination;
2077 for (p=source; *p != '\0'; p++)
2078 {
2079 if ((*p == '\\') || (*p == '(') || (*p == ')'))
2080 *q++='\\';
2081 *q++=(*p);
2082 }
2083 *q='\0';
2084 return(destination);
2085}
2086
2087static MagickBooleanType RenderPostscript(Image *image,
2088 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics)
2089{
2090 char
2091 filename[MaxTextExtent],
2092 geometry[MaxTextExtent],
2093 *text;
2094
2095 FILE
2096 *file;
2097
2098 Image
2099 *annotate_image;
2100
2101 ImageInfo
2102 *annotate_info;
2103
2104 int
2105 unique_file;
2106
2107 MagickBooleanType
2108 identity,
2109 status;
2110
2111 PointInfo
2112 extent,
2113 point,
2114 resolution;
2115
2116 ssize_t
2117 i;
2118
2119 size_t
2120 length;
2121
2122 ssize_t
2123 y;
2124
2125 /*
2126 Render label with a Postscript font.
2127 */
2128 if (draw_info->debug != MagickFalse)
2129 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2130 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
2131 draw_info->font : "none",draw_info->pointsize);
2132 file=(FILE *) NULL;
2133 unique_file=AcquireUniqueFileResource(filename);
2134 if (unique_file != -1)
2135 file=fdopen(unique_file,"wb");
2136 if ((unique_file == -1) || (file == (FILE *) NULL))
2137 {
2138 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
2139 filename);
2140 return(MagickFalse);
2141 }
2142 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
2143 (void) FormatLocaleFile(file,"/ReencodeType\n");
2144 (void) FormatLocaleFile(file,"{\n");
2145 (void) FormatLocaleFile(file," findfont dup length\n");
2146 (void) FormatLocaleFile(file,
2147 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2148 (void) FormatLocaleFile(file,
2149 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2150 (void) FormatLocaleFile(file,"} bind def\n");
2151 /*
2152 Sample to compute bounding box.
2153 */
2154 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2155 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2156 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2157 extent.x=0.0;
2158 extent.y=0.0;
2159 length=strlen(draw_info->text);
2160 for (i=0; i <= (ssize_t) (length+2); i++)
2161 {
2162 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2163 draw_info->affine.ry*2.0*draw_info->pointsize);
2164 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2165 draw_info->affine.sy*2.0*draw_info->pointsize);
2166 if (point.x > extent.x)
2167 extent.x=point.x;
2168 if (point.y > extent.y)
2169 extent.y=point.y;
2170 }
2171 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2172 extent.x/2.0,extent.y/2.0);
2173 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
2174 draw_info->pointsize);
2175 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
2176 (strchr(draw_info->font,'/') != (char *) NULL))
2177 (void) FormatLocaleFile(file,
2178 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2179 else
2180 (void) FormatLocaleFile(file,
2181 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2182 draw_info->font);
2183 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
2184 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2185 draw_info->affine.sy);
2186 text=EscapeParenthesis(draw_info->text);
2187 if (identity == MagickFalse)
2188 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2189 text);
2190 (void) FormatLocaleFile(file,"(%s) show\n",text);
2191 text=DestroyString(text);
2192 (void) FormatLocaleFile(file,"showpage\n");
2193 (void) fclose(file);
2194 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
2195 floor(extent.x+0.5),floor(extent.y+0.5));
2196 annotate_info=AcquireImageInfo();
2197 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
2198 filename);
2199 (void) CloneString(&annotate_info->page,geometry);
2200 if (draw_info->density != (char *) NULL)
2201 (void) CloneString(&annotate_info->density,draw_info->density);
2202 annotate_info->antialias=draw_info->text_antialias;
2203 annotate_image=ReadImage(annotate_info,&image->exception);
2204 CatchException(&image->exception);
2205 annotate_info=DestroyImageInfo(annotate_info);
2206 (void) RelinquishUniqueFileResource(filename);
2207 if (annotate_image == (Image *) NULL)
2208 return(MagickFalse);
2209 resolution.x=DefaultResolution;
2210 resolution.y=DefaultResolution;
2211 if (draw_info->density != (char *) NULL)
2212 {
2213 GeometryInfo
2214 geometry_info;
2215
2216 MagickStatusType
2217 flags;
2218
2219 flags=ParseGeometry(draw_info->density,&geometry_info);
2220 if ((flags & RhoValue) != 0)
2221 resolution.x=geometry_info.rho;
2222 resolution.y=resolution.x;
2223 if ((flags & SigmaValue) != 0)
2224 resolution.y=geometry_info.sigma;
2225 }
2226 if (identity == MagickFalse)
2227 (void) TransformImage(&annotate_image,"0x0",(char *) NULL);
2228 else
2229 {
2230 RectangleInfo
2231 crop_info;
2232
2233 crop_info=GetImageBoundingBox(annotate_image,&annotate_image->exception);
2234 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2235 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2236 crop_info.y=CastDoubleToLong(ceil((resolution.y/DefaultResolution)*
2237 extent.y/8.0-0.5));
2238 (void) FormatLocaleString(geometry,MaxTextExtent,
2239 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
2240 crop_info.height,(double) crop_info.x,(double) crop_info.y);
2241 (void) TransformImage(&annotate_image,geometry,(char *) NULL);
2242 }
2243 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2244 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2245 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2246 metrics->ascent=metrics->pixels_per_em.y;
2247 metrics->descent=metrics->pixels_per_em.y/-5.0;
2248 metrics->width=(double) annotate_image->columns/
2249 ExpandAffine(&draw_info->affine);
2250 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2251 metrics->max_advance=metrics->pixels_per_em.x;
2252 metrics->bounds.x1=0.0;
2253 metrics->bounds.y1=metrics->descent;
2254 metrics->bounds.x2=metrics->ascent+metrics->descent;
2255 metrics->bounds.y2=metrics->ascent+metrics->descent;
2256 metrics->underline_position=(-2.0);
2257 metrics->underline_thickness=1.0;
2258 if (draw_info->render == MagickFalse)
2259 {
2260 annotate_image=DestroyImage(annotate_image);
2261 return(MagickTrue);
2262 }
2263 if (draw_info->fill.opacity != TransparentOpacity)
2264 {
2265 ExceptionInfo
2266 *exception;
2267
2268 MagickBooleanType
2269 sync;
2270
2271 PixelPacket
2272 fill_color;
2273
2274 CacheView
2275 *annotate_view;
2276
2277 /*
2278 Render fill color.
2279 */
2280 if (image->matte == MagickFalse)
2281 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
2282 if (annotate_image->matte == MagickFalse)
2283 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel);
2284 fill_color=draw_info->fill;
2285 status=MagickTrue;
2286 exception=(&image->exception);
2287 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2288#if defined(MAGICKCORE_OPENMP_SUPPORT)
2289 #pragma omp parallel for schedule(static) shared(status) \
2290 magick_number_threads(annotate_image,annotate_image,annotate_image->rows,4)
2291#endif
2292 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2293 {
2294 PixelPacket
2295 *magick_restrict q;
2296
2297 ssize_t
2298 x;
2299
2300 if (status == MagickFalse)
2301 continue;
2302 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2303 1,exception);
2304 if (q == (PixelPacket *) NULL)
2305 {
2306 status=MagickFalse;
2307 continue;
2308 }
2309 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2310 {
2311 (void) GetFillColor(draw_info,x,y,&fill_color);
2312 SetPixelAlpha(q,ClampToQuantum(((((double) QuantumRange-
2313 GetPixelIntensity(annotate_image,q))*((double) QuantumRange-
2314 (double) fill_color.opacity))/(double) QuantumRange)));
2315 SetPixelRed(q,fill_color.red);
2316 SetPixelGreen(q,fill_color.green);
2317 SetPixelBlue(q,fill_color.blue);
2318 q++;
2319 }
2320 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2321 if (sync == MagickFalse)
2322 status=MagickFalse;
2323 }
2324 annotate_view=DestroyCacheView(annotate_view);
2325 (void) CompositeImage(image,OverCompositeOp,annotate_image,
2326 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2327 metrics->descent)-0.5));
2328 }
2329 annotate_image=DestroyImage(annotate_image);
2330 return(MagickTrue);
2331}
2332
2333/*
2334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2335% %
2336% %
2337% %
2338+ R e n d e r X 1 1 %
2339% %
2340% %
2341% %
2342%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2343%
2344% RenderX11() renders text on the image with an X11 font. It also returns the
2345% bounding box of the text relative to the image.
2346%
2347% The format of the RenderX11 method is:
2348%
2349% MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
2350% const PointInfo *offset,TypeMetric *metrics)
2351%
2352% A description of each parameter follows:
2353%
2354% o image: the image.
2355%
2356% o draw_info: the draw info.
2357%
2358% o offset: (x,y) location of text relative to image.
2359%
2360% o metrics: bounding box of text.
2361%
2362*/
2363static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
2364 const PointInfo *offset,TypeMetric *metrics)
2365{
2366 MagickBooleanType
2367 status;
2368
2369 if (annotate_semaphore == (SemaphoreInfo *) NULL)
2370 ActivateSemaphoreInfo(&annotate_semaphore);
2371 LockSemaphoreInfo(annotate_semaphore);
2372 status=XRenderImage(image,draw_info,offset,metrics);
2373 UnlockSemaphoreInfo(annotate_semaphore);
2374 return(status);
2375}