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