2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % MagickCore Image Annotation Methods %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % Digital Applications (www.digapp.com) contributed the stroked text algorithm.
37 % It was written by Leonard Rosenthol.
45 #include "MagickCore/studio.h"
46 #include "MagickCore/annotate.h"
47 #include "MagickCore/annotate-private.h"
48 #include "MagickCore/attribute.h"
49 #include "MagickCore/cache-private.h"
50 #include "MagickCore/cache-view.h"
51 #include "MagickCore/channel.h"
52 #include "MagickCore/client.h"
53 #include "MagickCore/color.h"
54 #include "MagickCore/color-private.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/constitute.h"
59 #include "MagickCore/draw.h"
60 #include "MagickCore/draw-private.h"
61 #include "MagickCore/enhance.h"
62 #include "MagickCore/exception.h"
63 #include "MagickCore/exception-private.h"
64 #include "MagickCore/gem.h"
65 #include "MagickCore/geometry.h"
66 #include "MagickCore/image-private.h"
67 #include "MagickCore/log.h"
68 #include "MagickCore/quantum.h"
69 #include "MagickCore/quantum-private.h"
70 #include "MagickCore/pixel-accessor.h"
71 #include "MagickCore/property.h"
72 #include "MagickCore/resource_.h"
73 #include "MagickCore/semaphore.h"
74 #include "MagickCore/statistic.h"
75 #include "MagickCore/string_.h"
76 #include "MagickCore/token.h"
77 #include "MagickCore/token-private.h"
78 #include "MagickCore/transform.h"
79 #include "MagickCore/transform-private.h"
80 #include "MagickCore/type.h"
81 #include "MagickCore/utility.h"
82 #include "MagickCore/utility-private.h"
83 #include "MagickCore/xwindow.h"
84 #include "MagickCore/xwindow-private.h"
85 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
86 #if defined(__MINGW32__) || defined(__MINGW64__)
90 #if defined(FT_FREETYPE_H)
91 # include FT_FREETYPE_H
93 # include <freetype/freetype.h>
95 #if defined(FT_GLYPH_H)
98 # include <freetype/ftglyph.h>
100 #if defined(FT_OUTLINE_H)
101 # include FT_OUTLINE_H
103 # include <freetype/ftoutln.h>
105 #if defined(FT_BBOX_H)
108 # include <freetype/ftbbox.h>
109 #endif /* defined(FT_BBOX_H) */
111 #if defined(MAGICKCORE_RAQM_DELEGATE)
114 typedef struct _GraphemeInfo
130 *annotate_semaphore = (SemaphoreInfo *) NULL;
133 Forward declarations.
135 static MagickBooleanType
136 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
138 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
140 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
141 TypeMetric *,ExceptionInfo *),
142 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 + A n n o t a t e C o m p o n e n t G e n e s i s %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % AnnotateComponentGenesis() instantiates the annotate component.
158 % The format of the AnnotateComponentGenesis method is:
160 % MagickBooleanType AnnotateComponentGenesis(void)
163 MagickPrivate MagickBooleanType AnnotateComponentGenesis(void)
165 if (annotate_semaphore == (SemaphoreInfo *) NULL)
166 annotate_semaphore=AcquireSemaphoreInfo();
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 + 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 %
179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 % AnnotateComponentTerminus() destroys the annotate component.
183 % The format of the AnnotateComponentTerminus method is:
185 % AnnotateComponentTerminus(void)
188 MagickPrivate void AnnotateComponentTerminus(void)
190 if (annotate_semaphore == (SemaphoreInfo *) NULL)
191 ActivateSemaphoreInfo(&annotate_semaphore);
192 RelinquishSemaphoreInfo(&annotate_semaphore);
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 % A n n o t a t e I m a g e %
204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 % AnnotateImage() annotates an image with text. Optionally you can include
207 % any of the following bits of information about the image by embedding
208 % the appropriate special characters:
212 % < less-than character.
213 % > greater-than character.
214 % & ampersand character.
216 % %b file size of image read in
217 % %c comment meta-data property
218 % %d directory component of path
219 % %e filename extension or suffix
220 % %f filename (including suffix)
221 % %g layer canvas page geometry (equivalent to "%Wx%H%X%Y")
222 % %h current image height in pixels
223 % %i image filename (note: becomes output filename for "info:")
224 % %k CALCULATED: number of unique colors
225 % %l label meta-data property
226 % %m image file format (file magic)
227 % %n number of images in current image sequence
228 % %o output filename (used for delegates)
229 % %p index of image in current image list
230 % %q quantum depth (compile-time constant)
231 % %r image class and colorspace
232 % %s scene number (from input unless re-assigned)
233 % %t filename without directory or extension (suffix)
234 % %u unique temporary filename (used for delegates)
235 % %w current width in pixels
236 % %x x resolution (density)
237 % %y y resolution (density)
238 % %z image depth (as read in unless modified, image save depth)
239 % %A image transparency channel enabled (true/false)
240 % %C image compression type
241 % %D image GIF dispose method
242 % %G original image size (%wx%h; before any resizes)
243 % %H page (canvas) height
244 % %M Magick filename (original file exactly as given, including read mods)
245 % %O page (canvas) offset ( = %X%Y )
246 % %P page (canvas) size ( = %Wx%H )
247 % %Q image compression quality ( 0 = default )
249 % %T image time delay (in centi-seconds)
250 % %U image resolution units
251 % %W page (canvas) width
252 % %X page (canvas) x offset (including sign)
253 % %Y page (canvas) y offset (including sign)
254 % %Z unique filename (used for delegates)
255 % %@ CALCULATED: trim bounding box (without actually trimming)
256 % %# CALCULATED: 'signature' hash of image values
258 % The format of the AnnotateImage method is:
260 % MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
261 % ExceptionInfo *exception)
263 % A description of each parameter follows:
265 % o image: the image.
267 % o draw_info: the draw info.
269 % o exception: return any errors or warnings in this structure.
272 MagickExport MagickBooleanType AnnotateImage(Image *image,
273 const DrawInfo *draw_info,ExceptionInfo *exception)
276 primitive[MagickPathExtent],
308 assert(image != (Image *) NULL);
309 assert(image->signature == MagickCoreSignature);
310 if (image->debug != MagickFalse)
311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
312 assert(draw_info != (DrawInfo *) NULL);
313 assert(draw_info->signature == MagickCoreSignature);
314 if (draw_info->text == (char *) NULL)
316 if (*draw_info->text == '\0')
318 textlist=StringToList(draw_info->text);
319 if (textlist == (char **) NULL)
321 length=strlen(textlist[0]);
322 for (i=1; textlist[i] != (char *) NULL; i++)
323 if (strlen(textlist[i]) > length)
324 length=strlen(textlist[i]);
325 number_lines=(size_t) i;
326 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
327 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
328 SetGeometry(image,&geometry);
329 SetGeometryInfo(&geometry_info);
330 if (annotate_info->geometry != (char *) NULL)
332 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
334 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
336 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
338 if (IsGrayColorspace(image->colorspace) != MagickFalse)
339 (void) SetImageColorspace(image,sRGBColorspace,exception);
341 for (i=0; textlist[i] != (char *) NULL; i++)
344 Position text relative to image.
346 annotate_info->affine.tx=geometry_info.xi-image->page.x;
347 annotate_info->affine.ty=geometry_info.psi-image->page.y;
348 (void) CloneString(&annotate->text,textlist[i]);
349 (void) GetTypeMetrics(image,annotate,&metrics,exception);
350 height=(ssize_t) (metrics.ascent-metrics.descent+
351 draw_info->interline_spacing+0.5);
352 switch (annotate->gravity)
354 case UndefinedGravity:
357 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
358 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
361 case NorthWestGravity:
363 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
364 annotate_info->affine.ry*height+annotate_info->affine.ry*
365 (metrics.ascent+metrics.descent);
366 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
367 annotate_info->affine.sy*height+annotate_info->affine.sy*
373 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
374 geometry.width/2.0+i*annotate_info->affine.ry*height-
375 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
376 (metrics.ascent+metrics.descent);
377 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
378 annotate_info->affine.sy*height+annotate_info->affine.sy*
379 metrics.ascent-annotate_info->affine.rx*metrics.width/2.0;
382 case NorthEastGravity:
384 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
385 geometry.width+i*annotate_info->affine.ry*height-
386 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
387 (metrics.ascent+metrics.descent)-1.0;
388 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
389 annotate_info->affine.sy*height+annotate_info->affine.sy*
390 metrics.ascent-annotate_info->affine.rx*metrics.width;
395 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
396 annotate_info->affine.ry*height+annotate_info->affine.ry*
397 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
398 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
399 geometry.height/2.0+i*annotate_info->affine.sy*height+
400 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
401 (number_lines-1.0)*height)/2.0;
406 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
407 geometry.width/2.0+i*annotate_info->affine.ry*height-
408 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
409 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
410 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
411 geometry.height/2.0+i*annotate_info->affine.sy*height-
412 annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
413 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
418 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
419 geometry.width+i*annotate_info->affine.ry*height-
420 annotate_info->affine.sx*metrics.width+
421 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
422 (number_lines-1.0)*height)/2.0-1.0;
423 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
424 geometry.height/2.0+i*annotate_info->affine.sy*height-
425 annotate_info->affine.rx*metrics.width+
426 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
427 (number_lines-1.0)*height)/2.0;
430 case SouthWestGravity:
432 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
433 annotate_info->affine.ry*height-annotate_info->affine.ry*
434 (number_lines-1.0)*height;
435 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
436 geometry.height+i*annotate_info->affine.sy*height-
437 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
442 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
443 geometry.width/2.0+i*annotate_info->affine.ry*height-
444 annotate_info->affine.sx*metrics.width/2.0-
445 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
446 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
447 geometry.height+i*annotate_info->affine.sy*height-
448 annotate_info->affine.rx*metrics.width/2.0-
449 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
452 case SouthEastGravity:
454 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
455 geometry.width+i*annotate_info->affine.ry*height-
456 annotate_info->affine.sx*metrics.width-
457 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
458 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
459 geometry.height+i*annotate_info->affine.sy*height-
460 annotate_info->affine.rx*metrics.width-
461 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
465 switch (annotate->align)
469 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
470 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
475 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
476 annotate_info->affine.sx*metrics.width/2.0;
477 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
478 annotate_info->affine.rx*metrics.width/2.0;
483 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
484 annotate_info->affine.sx*metrics.width;
485 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
486 annotate_info->affine.rx*metrics.width;
492 if (draw_info->undercolor.alpha != TransparentAlpha)
500 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
501 undercolor_info->fill=draw_info->undercolor;
502 undercolor_info->affine=draw_info->affine;
503 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
504 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
505 (void) FormatLocaleString(primitive,MagickPathExtent,
506 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(double) height);
507 (void) CloneString(&undercolor_info->primitive,primitive);
508 (void) DrawImage(image,undercolor_info,exception);
509 (void) DestroyDrawInfo(undercolor_info);
511 annotate_info->affine.tx=offset.x;
512 annotate_info->affine.ty=offset.y;
513 (void) FormatLocaleString(primitive,MagickPathExtent,"stroke-width %g "
514 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
515 if (annotate->decorate == OverlineDecoration)
517 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
518 metrics.descent-metrics.underline_position));
519 (void) CloneString(&annotate_info->primitive,primitive);
520 (void) DrawImage(image,annotate_info,exception);
523 if (annotate->decorate == UnderlineDecoration)
525 annotate_info->affine.ty-=(draw_info->affine.sy*
526 metrics.underline_position);
527 (void) CloneString(&annotate_info->primitive,primitive);
528 (void) DrawImage(image,annotate_info,exception);
531 Annotate image with text.
533 status=RenderType(image,annotate,&offset,&metrics,exception);
534 if (status == MagickFalse)
536 if (annotate->decorate == LineThroughDecoration)
538 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
539 metrics.underline_position+metrics.descent)/2.0);
540 (void) CloneString(&annotate_info->primitive,primitive);
541 (void) DrawImage(image,annotate_info,exception);
545 Relinquish resources.
547 annotate_info=DestroyDrawInfo(annotate_info);
548 annotate=DestroyDrawInfo(annotate);
549 for (i=0; textlist[i] != (char *) NULL; i++)
550 textlist[i]=DestroyString(textlist[i]);
551 textlist=(char **) RelinquishMagickMemory(textlist);
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % F o r m a t M a g i c k C a p t i o n %
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % FormatMagickCaption() formats a caption so that it fits within the image
567 % width. It returns the number of lines in the formatted caption.
569 % The format of the FormatMagickCaption method is:
571 % ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
572 % const MagickBooleanType split,TypeMetric *metrics,char **caption,
573 % ExceptionInfo *exception)
575 % A description of each parameter follows.
577 % o image: The image.
579 % o draw_info: the draw info.
581 % o split: when no convenient line breaks-- insert newline.
583 % o metrics: Return the font metrics in this structure.
585 % o caption: the caption.
587 % o exception: return any errors or warnings in this structure.
590 MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
591 const MagickBooleanType split,TypeMetric *metrics,char **caption,
592 ExceptionInfo *exception)
616 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
618 if ((digit == MagickFalse) && (IsUTFSpace(GetUTFCode(p)) != MagickFalse))
620 digit=((GetUTFCode(p) >= 0x0030) && (GetUTFCode(p) <= 0x0039)) ?
621 MagickTrue : MagickFalse;
622 if (GetUTFCode(p) == '\n')
624 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
627 status=GetTypeMetrics(image,draw_info,metrics,exception);
628 if (status == MagickFalse)
630 width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
631 if ((width <= image->columns) || (s == (char *) NULL))
633 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
639 if (split != MagickFalse)
645 No convenient line breaks-- insert newline.
647 target=AcquireString(*caption);
649 CopyMagickString(target,*caption,n+1);
650 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
651 ConcatenateMagickString(target,p,strlen(*caption)+2);
652 (void) DestroyString(*caption);
659 if (width > image->columns)
665 No convenient break point, force one.
667 text=AcquireString(draw_info->text);
670 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
672 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
674 if (GetUTFCode(p) == '\n')
676 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
679 status=GetTypeMetrics(image,draw_info,metrics,exception);
680 if (status == MagickFalse)
682 width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
683 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
685 (void) strcpy(text,draw_info->text);
686 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
692 if ((s != (char *) NULL) || (split != MagickFalse))
698 No convenient line breaks-- insert newline.
700 target=AcquireString(*caption);
702 CopyMagickString(target,*caption,n+1);
703 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
704 ConcatenateMagickString(target,p,strlen(*caption)+2);
705 (void) DestroyString(*caption);
712 text=DestroyString(text);
715 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
716 if (GetUTFCode(p) == '\n')
722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 % G e t M u l t i l i n e T y p e M e t r i c s %
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732 % GetMultilineTypeMetrics() returns the following information for the
733 % specified font and text:
741 % maximum horizontal advance
749 % underline thickness
751 % This method is like GetTypeMetrics() but it returns the maximum text width
752 % and height for multiple lines of text.
754 % The format of the GetMultilineTypeMetrics method is:
756 % MagickBooleanType GetMultilineTypeMetrics(Image *image,
757 % const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
759 % A description of each parameter follows:
761 % o image: the image.
763 % o draw_info: the draw info.
765 % o metrics: Return the font metrics in this structure.
767 % o exception: return any errors or warnings in this structure.
770 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
771 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
788 assert(image != (Image *) NULL);
789 assert(image->signature == MagickCoreSignature);
790 if (image->debug != MagickFalse)
791 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
792 assert(draw_info != (DrawInfo *) NULL);
793 assert(draw_info->text != (char *) NULL);
794 assert(draw_info->signature == MagickCoreSignature);
795 if (*draw_info->text == '\0')
797 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
798 annotate_info->text=DestroyString(annotate_info->text);
800 Convert newlines to multiple lines of text.
802 textlist=StringToList(draw_info->text);
803 if (textlist == (char **) NULL)
805 annotate_info->render=MagickFalse;
806 annotate_info->direction=UndefinedDirection;
807 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
808 (void) ResetMagickMemory(&extent,0,sizeof(extent));
810 Find the widest of the text lines.
812 annotate_info->text=textlist[0];
813 status=GetTypeMetrics(image,annotate_info,&extent,exception);
815 for (i=1; textlist[i] != (char *) NULL; i++)
817 annotate_info->text=textlist[i];
818 status=GetTypeMetrics(image,annotate_info,&extent,exception);
819 if (extent.width > metrics->width)
822 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
823 (i-1)*draw_info->interline_spacing);
825 Relinquish resources.
827 annotate_info->text=(char *) NULL;
828 annotate_info=DestroyDrawInfo(annotate_info);
829 for (i=0; textlist[i] != (char *) NULL; i++)
830 textlist[i]=DestroyString(textlist[i]);
831 textlist=(char **) RelinquishMagickMemory(textlist);
836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
840 % G e t T y p e M e t r i c s %
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
846 % GetTypeMetrics() returns the following information for the specified font
855 % maximum horizontal advance
863 % underline thickness
865 % The format of the GetTypeMetrics method is:
867 % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
868 % TypeMetric *metrics,ExceptionInfo *exception)
870 % A description of each parameter follows:
872 % o image: the image.
874 % o draw_info: the draw info.
876 % o metrics: Return the font metrics in this structure.
878 % o exception: return any errors or warnings in this structure.
881 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
882 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
893 assert(image != (Image *) NULL);
894 assert(image->signature == MagickCoreSignature);
895 if (image->debug != MagickFalse)
896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
897 assert(draw_info != (DrawInfo *) NULL);
898 assert(draw_info->text != (char *) NULL);
899 assert(draw_info->signature == MagickCoreSignature);
900 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
901 annotate_info->render=MagickFalse;
902 annotate_info->direction=UndefinedDirection;
903 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
906 status=RenderType(image,annotate_info,&offset,metrics,exception);
907 if (image->debug != MagickFalse)
908 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
909 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
910 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
911 "underline position: %g; underline thickness: %g",annotate_info->text,
912 metrics->width,metrics->height,metrics->ascent,metrics->descent,
913 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
914 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
915 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
916 metrics->underline_position,metrics->underline_thickness);
917 annotate_info=DestroyDrawInfo(annotate_info);
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
926 + R e n d e r T y p e %
930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 % RenderType() renders text on the image. It also returns the bounding box of
933 % the text relative to the image.
935 % The format of the RenderType method is:
937 % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
938 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
940 % A description of each parameter follows:
942 % o image: the image.
944 % o draw_info: the draw info.
946 % o offset: (x,y) location of text relative to image.
948 % o metrics: bounding box of text.
950 % o exception: return any errors or warnings in this structure.
953 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
954 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
965 type_info=(const TypeInfo *) NULL;
966 if (draw_info->font != (char *) NULL)
968 if (*draw_info->font == '@')
970 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
974 if (*draw_info->font == '-')
975 return(RenderX11(image,draw_info,offset,metrics,exception));
976 if (*draw_info->font == '^')
977 return(RenderPostscript(image,draw_info,offset,metrics,exception));
978 if (IsPathAccessible(draw_info->font) != MagickFalse)
980 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
984 type_info=GetTypeInfo(draw_info->font,exception);
985 if (type_info == (const TypeInfo *) NULL)
986 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
987 "UnableToReadFont","`%s'",draw_info->font);
989 if ((type_info == (const TypeInfo *) NULL) &&
990 (draw_info->family != (const char *) NULL))
992 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
993 draw_info->stretch,draw_info->weight,exception);
994 if (type_info == (const TypeInfo *) NULL)
995 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
996 "UnableToReadFont","`%s'",draw_info->family);
998 if (type_info == (const TypeInfo *) NULL)
999 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
1000 draw_info->stretch,draw_info->weight,exception);
1001 if (type_info == (const TypeInfo *) NULL)
1002 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
1003 draw_info->stretch,draw_info->weight,exception);
1004 if (type_info == (const TypeInfo *) NULL)
1005 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
1006 draw_info->stretch,draw_info->weight,exception);
1007 if (type_info == (const TypeInfo *) NULL)
1008 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
1009 draw_info->stretch,draw_info->weight,exception);
1010 if (type_info == (const TypeInfo *) NULL)
1011 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
1012 draw_info->stretch,draw_info->weight,exception);
1013 if (type_info == (const TypeInfo *) NULL)
1014 type_info=GetTypeInfo("*",exception);
1015 if (type_info == (const TypeInfo *) NULL)
1017 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
1021 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1022 annotate_info->face=type_info->face;
1023 if (type_info->metrics != (char *) NULL)
1024 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1025 if (type_info->glyphs != (char *) NULL)
1026 (void) CloneString(&annotate_info->font,type_info->glyphs);
1027 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
1029 annotate_info=DestroyDrawInfo(annotate_info);
1034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038 + R e n d e r F r e e t y p e %
1042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1044 % RenderFreetype() renders text on the image with a Truetype font. It also
1045 % returns the bounding box of the text relative to the image.
1047 % The format of the RenderFreetype method is:
1049 % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
1050 % const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1051 % ExceptionInfo *exception)
1053 % A description of each parameter follows:
1055 % o image: the image.
1057 % o draw_info: the draw info.
1059 % o encoding: the font encoding.
1061 % o offset: (x,y) location of text relative to image.
1063 % o metrics: bounding box of text.
1065 % o exception: return any errors or warnings in this structure.
1069 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
1071 static size_t ComplexTextLayout(const Image *image,const DrawInfo *draw_info,
1072 const char *text,const size_t length,const FT_Face face,const FT_Int32 flags,
1073 GraphemeInfo **grapheme,ExceptionInfo *exception)
1075 #if defined(MAGICKCORE_RAQM_DELEGATE)
1093 if (rq == (raqm_t *) NULL)
1095 if (raqm_set_text_utf8(rq,text,length) == 0)
1097 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1099 if (raqm_set_freetype_face(rq,face) == 0)
1101 features=GetImageProperty(image,"type:features",exception);
1102 if (features != (const char *) NULL)
1117 token_info=AcquireTokenInfo();
1118 token=AcquireString("");
1119 status_token=Tokenizer(token_info,0,token,50,features,"",",","",'\0',
1120 &breaker,&next,"e);
1121 while (status_token == 0)
1123 raqm_add_font_feature(rq,token,strlen(token));
1124 status_token=Tokenizer(token_info,0,token,50,features,"",",","",'\0',
1125 &breaker,&next,"e);
1127 token_info=DestroyTokenInfo(token_info);
1128 token=DestroyString(token);
1130 if (raqm_layout(rq) == 0)
1132 glyphs=raqm_get_glyphs(rq,&extent);
1133 if (glyphs == (raqm_glyph_t *) NULL)
1138 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(extent,sizeof(**grapheme));
1139 if (*grapheme == (GraphemeInfo *) NULL)
1144 for (i=0; i < (ssize_t) extent; i++)
1146 (*grapheme)[i].index=glyphs[i].index;
1147 (*grapheme)[i].x_offset=glyphs[i].x_offset;
1148 (*grapheme)[i].x_advance=glyphs[i].x_advance;
1149 (*grapheme)[i].y_offset=glyphs[i].y_offset;
1150 (*grapheme)[i].cluster=glyphs[i].cluster;
1169 magick_unreferenced(image);
1170 magick_unreferenced(exception);
1173 Simple layout for bi-directional text (right-to-left or left-to-right).
1175 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(length+1,sizeof(**grapheme));
1176 if (*grapheme == (GraphemeInfo *) NULL)
1180 for (i=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p), i++)
1182 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,GetUTFCode(p));
1183 (*grapheme)[i].x_offset=0;
1184 (*grapheme)[i].y_offset=0;
1185 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1187 if (FT_HAS_KERNING(face))
1192 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1193 (*grapheme)[i].index,ft_kerning_default,&kerning);
1195 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1196 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1199 ft_status=FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1200 (*grapheme)[i].x_advance=face->glyph->advance.x;
1201 (*grapheme)[i].cluster=p-text;
1202 last_glyph=(*grapheme)[i].index;
1208 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1209 DrawInfo *draw_info)
1215 path[MagickPathExtent];
1217 affine=draw_info->affine;
1218 (void) FormatLocaleString(path,MagickPathExtent,"C%g,%g %g,%g %g,%g",
1219 affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-
1220 q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1221 (void) ConcatenateString(&draw_info->primitive,path);
1225 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1231 path[MagickPathExtent];
1233 affine=draw_info->affine;
1234 (void) FormatLocaleString(path,MagickPathExtent,"L%g,%g",affine.tx+to->x/64.0,
1235 affine.ty-to->y/64.0);
1236 (void) ConcatenateString(&draw_info->primitive,path);
1240 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1246 path[MagickPathExtent];
1248 affine=draw_info->affine;
1249 (void) FormatLocaleString(path,MagickPathExtent,"M%g,%g",affine.tx+to->x/64.0,
1250 affine.ty-to->y/64.0);
1251 (void) ConcatenateString(&draw_info->primitive,path);
1255 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1256 DrawInfo *draw_info)
1262 path[MagickPathExtent];
1264 affine=draw_info->affine;
1265 (void) FormatLocaleString(path,MagickPathExtent,"Q%g,%g %g,%g",affine.tx+
1266 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1268 (void) ConcatenateString(&draw_info->primitive,path);
1272 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1273 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1274 ExceptionInfo *exception)
1276 #if !defined(FT_OPEN_PATHNAME)
1277 #define FT_OPEN_PATHNAME ft_open_pathname
1280 typedef struct _GlyphInfo
1355 static FT_Outline_Funcs
1358 (FT_Outline_MoveTo_Func) TraceMoveTo,
1359 (FT_Outline_LineTo_Func) TraceLineTo,
1360 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1361 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1369 Initialize Truetype library.
1371 ft_status=FT_Init_FreeType(&library);
1373 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1375 args.flags=FT_OPEN_PATHNAME;
1376 if (draw_info->font == (char *) NULL)
1377 args.pathname=ConstantString("helvetica");
1379 if (*draw_info->font != '@')
1380 args.pathname=ConstantString(draw_info->font);
1382 args.pathname=ConstantString(draw_info->font+1);
1383 face=(FT_Face) NULL;
1384 ft_status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
1385 args.pathname=DestroyString(args.pathname);
1388 (void) FT_Done_FreeType(library);
1389 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
1390 "UnableToReadFont","`%s'",draw_info->font);
1391 return(MagickFalse);
1393 if ((draw_info->metrics != (char *) NULL) &&
1394 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1395 (void) FT_Attach_File(face,draw_info->metrics);
1396 encoding_type=ft_encoding_unicode;
1397 ft_status=FT_Select_Charmap(face,encoding_type);
1398 if ((ft_status != 0) && (face->num_charmaps != 0))
1399 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1400 if (encoding != (const char *) NULL)
1402 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1403 encoding_type=ft_encoding_adobe_custom;
1404 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1405 encoding_type=ft_encoding_adobe_expert;
1406 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1407 encoding_type=ft_encoding_adobe_standard;
1408 if (LocaleCompare(encoding,"AppleRoman") == 0)
1409 encoding_type=ft_encoding_apple_roman;
1410 if (LocaleCompare(encoding,"BIG5") == 0)
1411 encoding_type=ft_encoding_big5;
1412 if (LocaleCompare(encoding,"GB2312") == 0)
1413 encoding_type=ft_encoding_gb2312;
1414 if (LocaleCompare(encoding,"Johab") == 0)
1415 encoding_type=ft_encoding_johab;
1416 #if defined(ft_encoding_latin_1)
1417 if (LocaleCompare(encoding,"Latin-1") == 0)
1418 encoding_type=ft_encoding_latin_1;
1420 if (LocaleCompare(encoding,"Latin-2") == 0)
1421 encoding_type=ft_encoding_latin_2;
1422 if (LocaleCompare(encoding,"None") == 0)
1423 encoding_type=ft_encoding_none;
1424 if (LocaleCompare(encoding,"SJIScode") == 0)
1425 encoding_type=ft_encoding_sjis;
1426 if (LocaleCompare(encoding,"Symbol") == 0)
1427 encoding_type=ft_encoding_symbol;
1428 if (LocaleCompare(encoding,"Unicode") == 0)
1429 encoding_type=ft_encoding_unicode;
1430 if (LocaleCompare(encoding,"Wansung") == 0)
1431 encoding_type=ft_encoding_wansung;
1432 ft_status=FT_Select_Charmap(face,encoding_type);
1435 (void) FT_Done_Face(face);
1436 (void) FT_Done_FreeType(library);
1437 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1443 resolution.x=DefaultResolution;
1444 resolution.y=DefaultResolution;
1445 if (draw_info->density != (char *) NULL)
1453 geometry_flags=ParseGeometry(draw_info->density,&geometry_info);
1454 resolution.x=geometry_info.rho;
1455 resolution.y=geometry_info.sigma;
1456 if ((geometry_flags & SigmaValue) == 0)
1457 resolution.y=resolution.x;
1459 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1460 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1461 (FT_UInt) resolution.y);
1464 (void) FT_Done_Face(face);
1465 (void) FT_Done_FreeType(library);
1466 ThrowBinaryException(TypeError,"UnableToReadFont",draw_info->font);
1468 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1469 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1470 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1471 metrics->descent=(double) face->size->metrics.descender/64.0;
1473 metrics->origin.x=0;
1474 metrics->origin.y=0;
1475 metrics->height=(double) face->size->metrics.height/64.0;
1476 metrics->max_advance=0.0;
1477 if (face->size->metrics.max_advance > MagickEpsilon)
1478 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1479 metrics->bounds.x1=0.0;
1480 metrics->bounds.y1=metrics->descent;
1481 metrics->bounds.x2=metrics->ascent+metrics->descent;
1482 metrics->bounds.y2=metrics->ascent+metrics->descent;
1483 metrics->underline_position=face->underline_position/64.0;
1484 metrics->underline_thickness=face->underline_thickness/64.0;
1485 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
1487 (void) FT_Done_Face(face);
1488 (void) FT_Done_FreeType(library);
1492 Compute bounding box.
1494 if (image->debug != MagickFalse)
1495 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1496 "font-encoding %s; text-encoding %s; pointsize %g",
1497 draw_info->font != (char *) NULL ? draw_info->font : "none",
1498 encoding != (char *) NULL ? encoding : "none",
1499 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1500 draw_info->pointsize);
1501 flags=FT_LOAD_DEFAULT;
1502 if (draw_info->render == MagickFalse)
1503 flags=FT_LOAD_NO_BITMAP;
1504 if (draw_info->text_antialias == MagickFalse)
1505 flags|=FT_LOAD_TARGET_MONO;
1508 #if defined(FT_LOAD_TARGET_LIGHT)
1509 flags|=FT_LOAD_TARGET_LIGHT;
1510 #elif defined(FT_LOAD_TARGET_LCD)
1511 flags|=FT_LOAD_TARGET_LCD;
1514 value=GetImageProperty(image,"type:hinting",exception);
1515 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1516 flags|=FT_LOAD_NO_HINTING;
1520 last_glyph.image=NULL;
1527 if (draw_info->render != MagickFalse)
1529 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1530 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1531 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1532 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1534 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1535 if (annotate_info->dash_pattern != (double *) NULL)
1536 annotate_info->dash_pattern[0]=0.0;
1537 (void) CloneString(&annotate_info->primitive,"path '");
1539 if (draw_info->render != MagickFalse)
1541 if (image->storage_class != DirectClass)
1542 (void) SetImageStorageClass(image,DirectClass,exception);
1543 if (image->alpha_trait == UndefinedPixelTrait)
1544 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1548 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1549 if (GetUTFCode(p) < 0)
1551 utf8=(unsigned char *) NULL;
1552 if (GetUTFCode(p) == 0)
1556 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1557 if (utf8 != (unsigned char *) NULL)
1560 grapheme=(GraphemeInfo *) NULL;
1561 length=ComplexTextLayout(image,draw_info,p,strlen(p),face,flags,&grapheme,
1564 for (i=0; i < (ssize_t) length; i++)
1567 Render UTF-8 sequence.
1569 glyph.id=(FT_UInt) grapheme[i].index;
1571 glyph.id=FT_Get_Char_Index(face,'?');
1572 if ((glyph.id != 0) && (last_glyph.id != 0))
1573 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1574 glyph.origin=origin;
1575 glyph.origin.x+=(FT_Pos) grapheme[i].x_offset;
1576 glyph.origin.y+=(FT_Pos) grapheme[i].y_offset;
1577 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1580 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1583 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1587 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
1588 if (bounds.xMin != 0)
1589 metrics->bounds.x1=(double) bounds.xMin;
1590 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
1591 if (bounds.yMin != 0)
1592 metrics->bounds.y1=(double) bounds.yMin;
1593 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
1594 if (bounds.xMax != 0)
1595 metrics->bounds.x2=(double) bounds.xMax;
1596 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
1597 if (bounds.yMax != 0)
1598 metrics->bounds.y2=(double) bounds.yMax;
1599 if (((draw_info->stroke.alpha != TransparentAlpha) ||
1600 (draw_info->stroke_pattern != (Image *) NULL)) &&
1601 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1606 annotate_info->affine.tx=glyph.origin.x/64.0;
1607 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1608 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->outline,
1609 &OutlineMethods,annotate_info);
1611 FT_Vector_Transform(&glyph.origin,&affine);
1612 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1613 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1614 (FT_Vector *) NULL,MagickTrue);
1617 bitmap=(FT_BitmapGlyph) glyph.image;
1618 point.x=offset->x+bitmap->left;
1619 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1620 point.x=offset->x+(origin.x >> 6);
1621 point.y=offset->y-bitmap->top;
1622 if (draw_info->render != MagickFalse)
1630 register unsigned char
1634 Rasterize the glyph.
1636 transparent_fill=((draw_info->fill.alpha == TransparentAlpha) &&
1637 (draw_info->fill_pattern == (Image *) NULL) &&
1638 (draw_info->stroke.alpha == TransparentAlpha) &&
1639 (draw_info->stroke_pattern == (Image *) NULL)) ? MagickTrue :
1641 image_view=AcquireAuthenticCacheView(image,exception);
1642 r=bitmap->bitmap.buffer;
1643 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1666 if (status == MagickFalse)
1668 x_offset=(ssize_t) ceil(point.x-0.5);
1669 y_offset=(ssize_t) ceil(point.y+y-0.5);
1670 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1673 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1677 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1678 bitmap->bitmap.width,1,exception);
1679 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1681 n=y*bitmap->bitmap.pitch-1;
1682 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
1686 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1688 if (q != (Quantum *) NULL)
1689 q+=GetPixelChannels(image);
1692 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
1693 fill_opacity=(double) (r[n])/(bitmap->bitmap.num_grays-1);
1695 fill_opacity=((r[(x >> 3)+y*bitmap->bitmap.pitch] &
1696 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1697 if (draw_info->text_antialias == MagickFalse)
1698 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1699 if (active == MagickFalse)
1700 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1702 if (q == (Quantum *) NULL)
1704 if (transparent_fill == MagickFalse)
1706 GetPixelInfo(image,&fill_color);
1707 GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1709 fill_opacity=fill_opacity*fill_color.alpha;
1710 CompositePixelOver(image,&fill_color,fill_opacity,q,
1711 GetPixelAlpha(image,q),q);
1719 Da=1.0-(QuantumScale*GetPixelAlpha(image,q));
1721 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*QuantumRange;
1722 SetPixelAlpha(image,fill_opacity,q);
1724 if (active == MagickFalse)
1726 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1727 if (sync == MagickFalse)
1730 q+=GetPixelChannels(image);
1732 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1733 if (sync == MagickFalse)
1736 image_view=DestroyCacheView(image_view);
1737 if (((draw_info->stroke.alpha != TransparentAlpha) ||
1738 (draw_info->stroke_pattern != (Image *) NULL)) &&
1739 (status != MagickFalse))
1744 annotate_info->linejoin=RoundJoin;
1745 annotate_info->affine.tx=offset->x;
1746 annotate_info->affine.ty=offset->y;
1747 (void) ConcatenateString(&annotate_info->primitive,"'");
1748 (void) DrawImage(image,annotate_info,exception);
1749 (void) CloneString(&annotate_info->primitive,"path '");
1752 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1753 metrics->width=bitmap->left+bitmap->bitmap.width;
1754 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1755 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
1756 (IsUTFSpace(code) == MagickFalse))
1757 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
1759 origin.x+=(FT_Pos) grapheme[i].x_advance;
1760 metrics->origin.x=(double) origin.x;
1761 metrics->origin.y=(double) origin.y;
1762 if (last_glyph.id != 0)
1763 FT_Done_Glyph(last_glyph.image);
1765 code=GetUTFCode(p+grapheme[i].cluster);
1767 if (grapheme != (GraphemeInfo *) NULL)
1768 grapheme=(GraphemeInfo *) RelinquishMagickMemory(grapheme);
1769 if (utf8 != (unsigned char *) NULL)
1770 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
1771 if (last_glyph.id != 0)
1772 FT_Done_Glyph(last_glyph.image);
1774 Determine font metrics.
1776 glyph.id=FT_Get_Char_Index(face,'_');
1777 glyph.origin=origin;
1778 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1781 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1784 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1788 FT_Vector_Transform(&glyph.origin,&affine);
1789 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1790 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1791 (FT_Vector *) NULL,MagickTrue);
1792 bitmap=(FT_BitmapGlyph) glyph.image;
1793 if (bitmap->left > metrics->width)
1794 metrics->width=bitmap->left;
1797 FT_Done_Glyph(glyph.image);
1799 metrics->bounds.x1/=64.0;
1800 metrics->bounds.y1/=64.0;
1801 metrics->bounds.x2/=64.0;
1802 metrics->bounds.y2/=64.0;
1803 metrics->origin.x/=64.0;
1804 metrics->origin.y/=64.0;
1806 Relinquish resources.
1808 annotate_info=DestroyDrawInfo(annotate_info);
1809 (void) FT_Done_Face(face);
1810 (void) FT_Done_FreeType(library);
1814 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1815 const char *magick_unused(encoding),const PointInfo *offset,
1816 TypeMetric *metrics,ExceptionInfo *exception)
1818 (void) ThrowMagickException(exception,GetMagickModule(),
1819 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
1820 draw_info->font != (char *) NULL ? draw_info->font : "none");
1821 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1826 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1830 + R e n d e r P o s t s c r i p t %
1834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1836 % RenderPostscript() renders text on the image with a Postscript font. It
1837 % also returns the bounding box of the text relative to the image.
1839 % The format of the RenderPostscript method is:
1841 % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1842 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1844 % A description of each parameter follows:
1846 % o image: the image.
1848 % o draw_info: the draw info.
1850 % o offset: (x,y) location of text relative to image.
1852 % o metrics: bounding box of text.
1854 % o exception: return any errors or warnings in this structure.
1858 static char *EscapeParenthesis(const char *source)
1872 assert(source != (const char *) NULL);
1874 for (p=source; *p != '\0'; p++)
1876 if ((*p == '\\') || (*p == '(') || (*p == ')'))
1879 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
1884 destination=(char *) NULL;
1885 if (~length >= (MagickPathExtent-1))
1886 destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
1887 sizeof(*destination));
1888 if (destination == (char *) NULL)
1889 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
1892 for (p=source; *p != '\0'; p++)
1894 if ((*p == '\\') || (*p == '(') || (*p == ')'))
1899 return(destination);
1902 static MagickBooleanType RenderPostscript(Image *image,
1903 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1904 ExceptionInfo *exception)
1907 filename[MagickPathExtent],
1908 geometry[MagickPathExtent],
1941 Render label with a Postscript font.
1943 if (image->debug != MagickFalse)
1944 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1945 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1946 draw_info->font : "none",draw_info->pointsize);
1948 unique_file=AcquireUniqueFileResource(filename);
1949 if (unique_file != -1)
1950 file=fdopen(unique_file,"wb");
1951 if ((unique_file == -1) || (file == (FILE *) NULL))
1953 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
1954 return(MagickFalse);
1956 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1957 (void) FormatLocaleFile(file,"/ReencodeType\n");
1958 (void) FormatLocaleFile(file,"{\n");
1959 (void) FormatLocaleFile(file," findfont dup length\n");
1960 (void) FormatLocaleFile(file,
1961 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
1962 (void) FormatLocaleFile(file,
1963 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
1964 (void) FormatLocaleFile(file,"} bind def\n");
1966 Sample to compute bounding box.
1968 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1969 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1970 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
1973 length=strlen(draw_info->text);
1974 for (i=0; i <= (ssize_t) (length+2); i++)
1976 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1977 draw_info->affine.ry*2.0*draw_info->pointsize);
1978 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1979 draw_info->affine.sy*2.0*draw_info->pointsize);
1980 if (point.x > extent.x)
1982 if (point.y > extent.y)
1985 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
1986 extent.x/2.0,extent.y/2.0);
1987 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
1988 draw_info->pointsize);
1989 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1990 (strchr(draw_info->font,'/') != (char *) NULL))
1991 (void) FormatLocaleFile(file,
1992 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1994 (void) FormatLocaleFile(file,
1995 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1997 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
1998 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1999 draw_info->affine.sy);
2000 text=EscapeParenthesis(draw_info->text);
2001 if (identity == MagickFalse)
2002 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2004 (void) FormatLocaleFile(file,"(%s) show\n",text);
2005 text=DestroyString(text);
2006 (void) FormatLocaleFile(file,"showpage\n");
2007 (void) fclose(file);
2008 (void) FormatLocaleString(geometry,MagickPathExtent,"%.20gx%.20g+0+0!",
2009 floor(extent.x+0.5),floor(extent.y+0.5));
2010 annotate_info=AcquireImageInfo();
2011 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,"ps:%s",
2013 (void) CloneString(&annotate_info->page,geometry);
2014 if (draw_info->density != (char *) NULL)
2015 (void) CloneString(&annotate_info->density,draw_info->density);
2016 annotate_info->antialias=draw_info->text_antialias;
2017 annotate_image=ReadImage(annotate_info,exception);
2018 CatchException(exception);
2019 annotate_info=DestroyImageInfo(annotate_info);
2020 (void) RelinquishUniqueFileResource(filename);
2021 if (annotate_image == (Image *) NULL)
2022 return(MagickFalse);
2023 (void) NegateImage(annotate_image,MagickFalse,exception);
2024 resolution.x=DefaultResolution;
2025 resolution.y=DefaultResolution;
2026 if (draw_info->density != (char *) NULL)
2034 flags=ParseGeometry(draw_info->density,&geometry_info);
2035 resolution.x=geometry_info.rho;
2036 resolution.y=geometry_info.sigma;
2037 if ((flags & SigmaValue) == 0)
2038 resolution.y=resolution.x;
2040 if (identity == MagickFalse)
2041 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
2047 crop_info=GetImageBoundingBox(annotate_image,exception);
2048 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2049 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2050 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
2052 (void) FormatLocaleString(geometry,MagickPathExtent,
2053 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
2054 crop_info.height,(double) crop_info.x,(double) crop_info.y);
2055 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
2057 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2058 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2059 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2060 metrics->ascent=metrics->pixels_per_em.x;
2061 metrics->descent=metrics->pixels_per_em.y/-5.0;
2062 metrics->width=(double) annotate_image->columns/
2063 ExpandAffine(&draw_info->affine);
2064 metrics->height=1.152*metrics->pixels_per_em.x;
2065 metrics->max_advance=metrics->pixels_per_em.x;
2066 metrics->bounds.x1=0.0;
2067 metrics->bounds.y1=metrics->descent;
2068 metrics->bounds.x2=metrics->ascent+metrics->descent;
2069 metrics->bounds.y2=metrics->ascent+metrics->descent;
2070 metrics->underline_position=(-2.0);
2071 metrics->underline_thickness=1.0;
2072 if (draw_info->render == MagickFalse)
2074 annotate_image=DestroyImage(annotate_image);
2077 if (draw_info->fill.alpha != TransparentAlpha)
2091 if (image->alpha_trait == UndefinedPixelTrait)
2092 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2093 if (annotate_image->alpha_trait == UndefinedPixelTrait)
2094 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
2096 fill_color=draw_info->fill;
2097 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2098 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2106 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2108 if (q == (Quantum *) NULL)
2110 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2112 GetFillColor(draw_info,x,y,&fill_color,exception);
2113 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
2114 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
2115 SetPixelRed(annotate_image,fill_color.red,q);
2116 SetPixelGreen(annotate_image,fill_color.green,q);
2117 SetPixelBlue(annotate_image,fill_color.blue,q);
2118 q+=GetPixelChannels(annotate_image);
2120 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2121 if (sync == MagickFalse)
2124 annotate_view=DestroyCacheView(annotate_view);
2125 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
2126 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2127 metrics->descent)-0.5),exception);
2129 annotate_image=DestroyImage(annotate_image);
2134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2138 + R e n d e r X 1 1 %
2142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2144 % RenderX11() renders text on the image with an X11 font. It also returns the
2145 % bounding box of the text relative to the image.
2147 % The format of the RenderX11 method is:
2149 % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
2150 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2152 % A description of each parameter follows:
2154 % o image: the image.
2156 % o draw_info: the draw info.
2158 % o offset: (x,y) location of text relative to image.
2160 % o metrics: bounding box of text.
2162 % o exception: return any errors or warnings in this structure.
2165 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
2166 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2171 if (annotate_semaphore == (SemaphoreInfo *) NULL)
2172 ActivateSemaphoreInfo(&annotate_semaphore);
2173 LockSemaphoreInfo(annotate_semaphore);
2174 status=XRenderImage(image,draw_info,offset,metrics,exception);
2175 UnlockSemaphoreInfo(annotate_semaphore);