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-2015 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 % http://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-view.h"
50 #include "MagickCore/channel.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/color.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace-private.h"
55 #include "MagickCore/composite.h"
56 #include "MagickCore/composite-private.h"
57 #include "MagickCore/constitute.h"
58 #include "MagickCore/draw.h"
59 #include "MagickCore/draw-private.h"
60 #include "MagickCore/enhance.h"
61 #include "MagickCore/exception.h"
62 #include "MagickCore/exception-private.h"
63 #include "MagickCore/gem.h"
64 #include "MagickCore/geometry.h"
65 #include "MagickCore/image-private.h"
66 #include "MagickCore/log.h"
67 #include "MagickCore/quantum.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/pixel-accessor.h"
70 #include "MagickCore/property.h"
71 #include "MagickCore/resource_.h"
72 #include "MagickCore/semaphore.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/string_.h"
75 #include "MagickCore/token-private.h"
76 #include "MagickCore/transform.h"
77 #include "MagickCore/type.h"
78 #include "MagickCore/utility.h"
79 #include "MagickCore/utility-private.h"
80 #include "MagickCore/xwindow.h"
81 #include "MagickCore/xwindow-private.h"
82 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
83 #if defined(__MINGW32__) || defined(__MINGW64__)
87 #if defined(FT_FREETYPE_H)
88 # include FT_FREETYPE_H
90 # include <freetype/freetype.h>
92 #if defined(FT_GLYPH_H)
95 # include <freetype/ftglyph.h>
97 #if defined(FT_OUTLINE_H)
98 # include FT_OUTLINE_H
100 # include <freetype/ftoutln.h>
102 #if defined(FT_BBOX_H)
105 # include <freetype/ftbbox.h>
106 #endif /* defined(FT_BBOX_H) */
113 *annotate_semaphore = (SemaphoreInfo *) NULL;
116 Forward declarations.
118 static MagickBooleanType
119 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
121 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
123 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
124 TypeMetric *,ExceptionInfo *),
125 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 + A n n o t a t e C o m p o n e n t G e n e s i s %
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 % AnnotateComponentGenesis() instantiates the annotate component.
141 % The format of the AnnotateComponentGenesis method is:
143 % MagickBooleanType AnnotateComponentGenesis(void)
146 MagickPrivate MagickBooleanType AnnotateComponentGenesis(void)
148 if (annotate_semaphore == (SemaphoreInfo *) NULL)
149 annotate_semaphore=AcquireSemaphoreInfo();
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 + 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 %
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 % AnnotateComponentTerminus() destroys the annotate component.
166 % The format of the AnnotateComponentTerminus method is:
168 % AnnotateComponentTerminus(void)
171 MagickPrivate void AnnotateComponentTerminus(void)
173 if (annotate_semaphore == (SemaphoreInfo *) NULL)
174 ActivateSemaphoreInfo(&annotate_semaphore);
175 RelinquishSemaphoreInfo(&annotate_semaphore);
179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 % A n n o t a t e I m a g e %
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 % AnnotateImage() annotates an image with text. Optionally you can include
190 % any of the following bits of information about the image by embedding
191 % the appropriate special characters:
195 % < less-than character.
196 % > greater-than character.
197 % & ampersand character.
199 % %b file size of image read in
200 % %c comment meta-data property
201 % %d directory component of path
202 % %e filename extension or suffix
203 % %f filename (including suffix)
204 % %g layer canvas page geometry (equivalent to "%Wx%H%X%Y")
205 % %h current image height in pixels
206 % %i image filename (note: becomes output filename for "info:")
207 % %k CALCULATED: number of unique colors
208 % %l label meta-data property
209 % %m image file format (file magic)
210 % %n number of images in current image sequence
211 % %o output filename (used for delegates)
212 % %p index of image in current image list
213 % %q quantum depth (compile-time constant)
214 % %r image class and colorspace
215 % %s scene number (from input unless re-assigned)
216 % %t filename without directory or extension (suffix)
217 % %u unique temporary filename (used for delegates)
218 % %w current width in pixels
219 % %x x resolution (density)
220 % %y y resolution (density)
221 % %z image depth (as read in unless modified, image save depth)
222 % %A image transparency channel enabled (true/false)
223 % %C image compression type
224 % %D image GIF dispose method
225 % %G original image size (%wx%h; before any resizes)
226 % %H page (canvas) height
227 % %M Magick filename (original file exactly as given, including read mods)
228 % %O page (canvas) offset ( = %X%Y )
229 % %P page (canvas) size ( = %Wx%H )
230 % %Q image compression quality ( 0 = default )
232 % %T image time delay (in centi-seconds)
233 % %U image resolution units
234 % %W page (canvas) width
235 % %X page (canvas) x offset (including sign)
236 % %Y page (canvas) y offset (including sign)
237 % %Z unique filename (used for delegates)
238 % %@ CALCULATED: trim bounding box (without actually trimming)
239 % %# CALCULATED: 'signature' hash of image values
241 % The format of the AnnotateImage method is:
243 % MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
244 % ExceptionInfo *exception)
246 % A description of each parameter follows:
248 % o image: the image.
250 % o draw_info: the draw info.
252 % o exception: return any errors or warnings in this structure.
255 MagickExport MagickBooleanType AnnotateImage(Image *image,
256 const DrawInfo *draw_info,ExceptionInfo *exception)
259 primitive[MaxTextExtent],
291 assert(image != (Image *) NULL);
292 assert(image->signature == MagickSignature);
293 if (image->debug != MagickFalse)
294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
295 assert(draw_info != (DrawInfo *) NULL);
296 assert(draw_info->signature == MagickSignature);
297 if (draw_info->text == (char *) NULL)
299 if (*draw_info->text == '\0')
301 textlist=StringToList(draw_info->text);
302 if (textlist == (char **) NULL)
304 length=strlen(textlist[0]);
305 for (i=1; textlist[i] != (char *) NULL; i++)
306 if (strlen(textlist[i]) > length)
307 length=strlen(textlist[i]);
308 number_lines=(size_t) i;
309 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
310 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
311 SetGeometry(image,&geometry);
312 SetGeometryInfo(&geometry_info);
313 if (annotate_info->geometry != (char *) NULL)
315 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
317 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
319 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
321 if (IsGrayColorspace(image->colorspace) != MagickFalse)
322 (void) SetImageColorspace(image,sRGBColorspace,exception);
324 for (i=0; textlist[i] != (char *) NULL; i++)
327 Position text relative to image.
329 annotate_info->affine.tx=geometry_info.xi-image->page.x;
330 annotate_info->affine.ty=geometry_info.psi-image->page.y;
331 (void) CloneString(&annotate->text,textlist[i]);
332 (void) GetTypeMetrics(image,annotate,&metrics,exception);
333 height=(ssize_t) (metrics.ascent-metrics.descent+
334 draw_info->interline_spacing+0.5);
335 switch (annotate->gravity)
337 case UndefinedGravity:
340 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
341 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
344 case NorthWestGravity:
346 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
347 annotate_info->affine.ry*height+annotate_info->affine.ry*
348 (metrics.ascent+metrics.descent);
349 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
350 annotate_info->affine.sy*height+annotate_info->affine.sy*
356 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
357 geometry.width/2.0+i*annotate_info->affine.ry*height-
358 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
359 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
360 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
361 annotate_info->affine.sy*height+annotate_info->affine.sy*
362 metrics.ascent-annotate_info->affine.rx*(metrics.width-
363 metrics.bounds.x1)/2.0;
366 case NorthEastGravity:
368 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
369 geometry.width+i*annotate_info->affine.ry*height-
370 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
371 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
372 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
373 annotate_info->affine.sy*height+annotate_info->affine.sy*
374 metrics.ascent-annotate_info->affine.rx*(metrics.width-
380 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
381 annotate_info->affine.ry*height+annotate_info->affine.ry*
382 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
383 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
384 geometry.height/2.0+i*annotate_info->affine.sy*height+
385 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
386 (number_lines-1.0)*height)/2.0;
391 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
392 geometry.width/2.0+i*annotate_info->affine.ry*height-
393 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
394 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
395 (number_lines-1)*height)/2.0;
396 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
397 geometry.height/2.0+i*annotate_info->affine.sy*height-
398 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
399 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
400 (number_lines-1.0)*height)/2.0;
405 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
406 geometry.width+i*annotate_info->affine.ry*height-
407 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
408 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
409 (number_lines-1.0)*height)/2.0-1.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+metrics.bounds.x1)+
413 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
414 (number_lines-1.0)*height)/2.0;
417 case SouthWestGravity:
419 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
420 annotate_info->affine.ry*height-annotate_info->affine.ry*
421 (number_lines-1.0)*height;
422 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
423 geometry.height+i*annotate_info->affine.sy*height-
424 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
429 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
430 geometry.width/2.0+i*annotate_info->affine.ry*height-
431 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
432 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
433 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
434 geometry.height+i*annotate_info->affine.sy*height-
435 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
436 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
439 case SouthEastGravity:
441 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
442 geometry.width+i*annotate_info->affine.ry*height-
443 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
444 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
445 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
446 geometry.height+i*annotate_info->affine.sy*height-
447 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
448 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
452 switch (annotate->align)
456 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
457 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
462 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
463 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
464 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
465 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
470 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
471 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
472 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
473 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
479 if (draw_info->undercolor.alpha != TransparentAlpha)
487 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
488 undercolor_info->fill=draw_info->undercolor;
489 undercolor_info->affine=draw_info->affine;
490 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
491 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
492 (void) FormatLocaleString(primitive,MaxTextExtent,
493 "rectangle -0.5,-0.5 %g,%.20g",metrics.origin.x,(double) height);
494 (void) CloneString(&undercolor_info->primitive,primitive);
495 (void) DrawImage(image,undercolor_info,exception);
496 (void) DestroyDrawInfo(undercolor_info);
498 annotate_info->affine.tx=offset.x;
499 annotate_info->affine.ty=offset.y;
500 (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
501 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
502 if (annotate->decorate == OverlineDecoration)
504 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
505 metrics.descent-metrics.underline_position));
506 (void) CloneString(&annotate_info->primitive,primitive);
507 (void) DrawImage(image,annotate_info,exception);
510 if (annotate->decorate == UnderlineDecoration)
512 annotate_info->affine.ty-=(draw_info->affine.sy*
513 metrics.underline_position);
514 (void) CloneString(&annotate_info->primitive,primitive);
515 (void) DrawImage(image,annotate_info,exception);
518 Annotate image with text.
520 status=RenderType(image,annotate,&offset,&metrics,exception);
521 if (status == MagickFalse)
523 if (annotate->decorate == LineThroughDecoration)
525 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
526 metrics.underline_position+metrics.descent)/2.0);
527 (void) CloneString(&annotate_info->primitive,primitive);
528 (void) DrawImage(image,annotate_info,exception);
532 Relinquish resources.
534 annotate_info=DestroyDrawInfo(annotate_info);
535 annotate=DestroyDrawInfo(annotate);
536 for (i=0; textlist[i] != (char *) NULL; i++)
537 textlist[i]=DestroyString(textlist[i]);
538 textlist=(char **) RelinquishMagickMemory(textlist);
543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 % F o r m a t M a g i c k C a p t i o n %
551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 % FormatMagickCaption() formats a caption so that it fits within the image
554 % width. It returns the number of lines in the formatted caption.
556 % The format of the FormatMagickCaption method is:
558 % ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
559 % const MagickBooleanType split,TypeMetric *metrics,char **caption,
560 % ExceptionInfo *exception)
562 % A description of each parameter follows.
564 % o image: The image.
566 % o draw_info: the draw info.
568 % o split: when no convenient line breaks-- insert newline.
570 % o metrics: Return the font metrics in this structure.
572 % o caption: the caption.
574 % o exception: return any errors or warnings in this structure.
577 MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
578 const MagickBooleanType split,TypeMetric *metrics,char **caption,
579 ExceptionInfo *exception)
601 text=AcquireString(draw_info->text);
604 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
606 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
608 if (GetUTFCode(p) == '\n')
610 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
613 status=GetTypeMetrics(image,draw_info,metrics,exception);
614 if (status == MagickFalse)
616 width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
617 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
619 (void) strcpy(text,draw_info->text);
620 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
626 if ((s != (char *) NULL) || (split != MagickFalse))
632 No convenient line breaks-- insert newline.
634 target=AcquireString(*caption);
636 CopyMagickString(target,*caption,n+1);
637 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
638 ConcatenateMagickString(target,p,strlen(*caption)+2);
639 (void) DestroyString(*caption);
646 text=DestroyString(text);
648 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
649 if (GetUTFCode(p) == '\n')
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659 % G e t M u l t i l i n e T y p e M e t r i c s %
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665 % GetMultilineTypeMetrics() returns the following information for the
666 % specified font and text:
674 % maximum horizontal advance
682 % underline thickness
684 % This method is like GetTypeMetrics() but it returns the maximum text width
685 % and height for multiple lines of text.
687 % The format of the GetMultilineTypeMetrics method is:
689 % MagickBooleanType GetMultilineTypeMetrics(Image *image,
690 % const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
692 % A description of each parameter follows:
694 % o image: the image.
696 % o draw_info: the draw info.
698 % o metrics: Return the font metrics in this structure.
700 % o exception: return any errors or warnings in this structure.
703 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
704 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
721 assert(image != (Image *) NULL);
722 assert(image->signature == MagickSignature);
723 if (image->debug != MagickFalse)
724 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
725 assert(draw_info != (DrawInfo *) NULL);
726 assert(draw_info->text != (char *) NULL);
727 assert(draw_info->signature == MagickSignature);
728 if (*draw_info->text == '\0')
730 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
731 annotate_info->text=DestroyString(annotate_info->text);
733 Convert newlines to multiple lines of text.
735 textlist=StringToList(draw_info->text);
736 if (textlist == (char **) NULL)
738 annotate_info->render=MagickFalse;
739 annotate_info->direction=UndefinedDirection;
740 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
741 (void) ResetMagickMemory(&extent,0,sizeof(extent));
743 Find the widest of the text lines.
745 annotate_info->text=textlist[0];
746 status=GetTypeMetrics(image,annotate_info,&extent,exception);
748 for (i=1; textlist[i] != (char *) NULL; i++)
750 annotate_info->text=textlist[i];
751 status=GetTypeMetrics(image,annotate_info,&extent,exception);
752 if (extent.width > metrics->width)
755 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
756 (i-1)*draw_info->interline_spacing);
758 Relinquish resources.
760 annotate_info->text=(char *) NULL;
761 annotate_info=DestroyDrawInfo(annotate_info);
762 for (i=0; textlist[i] != (char *) NULL; i++)
763 textlist[i]=DestroyString(textlist[i]);
764 textlist=(char **) RelinquishMagickMemory(textlist);
769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 % G e t T y p e M e t r i c s %
777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779 % GetTypeMetrics() returns the following information for the specified font
788 % maximum horizontal advance
796 % underline thickness
798 % The format of the GetTypeMetrics method is:
800 % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
801 % TypeMetric *metrics,ExceptionInfo *exception)
803 % A description of each parameter follows:
805 % o image: the image.
807 % o draw_info: the draw info.
809 % o metrics: Return the font metrics in this structure.
811 % o exception: return any errors or warnings in this structure.
814 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
815 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
826 assert(image != (Image *) NULL);
827 assert(image->signature == MagickSignature);
828 if (image->debug != MagickFalse)
829 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
830 assert(draw_info != (DrawInfo *) NULL);
831 assert(draw_info->text != (char *) NULL);
832 assert(draw_info->signature == MagickSignature);
833 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
834 annotate_info->render=MagickFalse;
835 annotate_info->direction=UndefinedDirection;
836 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
839 status=RenderType(image,annotate_info,&offset,metrics,exception);
840 if (image->debug != MagickFalse)
841 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
842 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
843 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
844 "underline position: %g; underline thickness: %g",annotate_info->text,
845 metrics->width,metrics->height,metrics->ascent,metrics->descent,
846 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
847 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
848 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
849 metrics->underline_position,metrics->underline_thickness);
850 annotate_info=DestroyDrawInfo(annotate_info);
855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 + R e n d e r T y p e %
863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
865 % RenderType() renders text on the image. It also returns the bounding box of
866 % the text relative to the image.
868 % The format of the RenderType method is:
870 % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
871 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
873 % A description of each parameter follows:
875 % o image: the image.
877 % o draw_info: the draw info.
879 % o offset: (x,y) location of text relative to image.
881 % o metrics: bounding box of text.
883 % o exception: return any errors or warnings in this structure.
886 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
887 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
898 type_info=(const TypeInfo *) NULL;
899 if (draw_info->font != (char *) NULL)
901 if (*draw_info->font == '@')
903 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
907 if (*draw_info->font == '-')
908 return(RenderX11(image,draw_info,offset,metrics,exception));
909 if (IsPathAccessible(draw_info->font) != MagickFalse)
911 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
915 type_info=GetTypeInfo(draw_info->font,exception);
916 if (type_info == (const TypeInfo *) NULL)
917 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
918 "UnableToReadFont","`%s'",draw_info->font);
920 if ((type_info == (const TypeInfo *) NULL) &&
921 (draw_info->family != (const char *) NULL))
923 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
924 draw_info->stretch,draw_info->weight,exception);
925 if (type_info == (const TypeInfo *) NULL)
926 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
927 "UnableToReadFont","`%s'",draw_info->family);
929 if (type_info == (const TypeInfo *) NULL)
930 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
931 draw_info->stretch,draw_info->weight,exception);
932 if (type_info == (const TypeInfo *) NULL)
933 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
934 draw_info->stretch,draw_info->weight,exception);
935 if (type_info == (const TypeInfo *) NULL)
936 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
937 draw_info->stretch,draw_info->weight,exception);
938 if (type_info == (const TypeInfo *) NULL)
939 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
940 draw_info->stretch,draw_info->weight,exception);
941 if (type_info == (const TypeInfo *) NULL)
942 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
943 draw_info->stretch,draw_info->weight,exception);
944 if (type_info == (const TypeInfo *) NULL)
945 type_info=GetTypeInfo("*",exception);
946 if (type_info == (const TypeInfo *) NULL)
948 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
952 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
953 annotate_info->face=type_info->face;
954 if (type_info->metrics != (char *) NULL)
955 (void) CloneString(&annotate_info->metrics,type_info->metrics);
956 if (type_info->glyphs != (char *) NULL)
957 (void) CloneString(&annotate_info->font,type_info->glyphs);
958 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
960 annotate_info=DestroyDrawInfo(annotate_info);
965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969 + R e n d e r F r e e t y p e %
973 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975 % RenderFreetype() renders text on the image with a Truetype font. It also
976 % returns the bounding box of the text relative to the image.
978 % The format of the RenderFreetype method is:
980 % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
981 % const char *encoding,const PointInfo *offset,TypeMetric *metrics,
982 % ExceptionInfo *exception)
984 % A description of each parameter follows:
986 % o image: the image.
988 % o draw_info: the draw info.
990 % o encoding: the font encoding.
992 % o offset: (x,y) location of text relative to image.
994 % o metrics: bounding box of text.
996 % o exception: return any errors or warnings in this structure.
1000 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
1002 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1003 DrawInfo *draw_info)
1009 path[MaxTextExtent];
1011 affine=draw_info->affine;
1012 (void) FormatLocaleString(path,MaxTextExtent,
1013 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
1014 p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
1015 affine.ty-to->y/64.0);
1016 (void) ConcatenateString(&draw_info->primitive,path);
1020 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1026 path[MaxTextExtent];
1028 affine=draw_info->affine;
1029 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
1030 to->x/64.0,affine.ty-to->y/64.0);
1031 (void) ConcatenateString(&draw_info->primitive,path);
1035 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1041 path[MaxTextExtent];
1043 affine=draw_info->affine;
1044 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
1045 to->x/64.0,affine.ty-to->y/64.0);
1046 (void) ConcatenateString(&draw_info->primitive,path);
1050 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1051 DrawInfo *draw_info)
1057 path[MaxTextExtent];
1059 affine=draw_info->affine;
1060 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
1061 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1062 affine.ty-to->y/64.0);
1063 (void) ConcatenateString(&draw_info->primitive,path);
1067 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1068 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1069 ExceptionInfo *exception)
1071 #if !defined(FT_OPEN_PATHNAME)
1072 #define FT_OPEN_PATHNAME ft_open_pathname
1075 typedef struct _GlyphInfo
1144 static FT_Outline_Funcs
1147 (FT_Outline_MoveTo_Func) TraceMoveTo,
1148 (FT_Outline_LineTo_Func) TraceLineTo,
1149 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1150 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1158 Initialize Truetype library.
1160 ft_status=FT_Init_FreeType(&library);
1162 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1164 args.flags=FT_OPEN_PATHNAME;
1165 if (draw_info->font == (char *) NULL)
1166 args.pathname=ConstantString("helvetica");
1168 if (*draw_info->font != '@')
1169 args.pathname=ConstantString(draw_info->font);
1171 args.pathname=ConstantString(draw_info->font+1);
1172 face=(FT_Face) NULL;
1173 ft_status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
1174 args.pathname=DestroyString(args.pathname);
1177 (void) FT_Done_FreeType(library);
1178 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
1179 "UnableToReadFont","`%s'",draw_info->font);
1180 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1182 if ((draw_info->metrics != (char *) NULL) &&
1183 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1184 (void) FT_Attach_File(face,draw_info->metrics);
1185 encoding_type=ft_encoding_unicode;
1186 ft_status=FT_Select_Charmap(face,encoding_type);
1187 if ((ft_status != 0) && (face->num_charmaps != 0))
1188 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1189 if (encoding != (const char *) NULL)
1191 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1192 encoding_type=ft_encoding_adobe_custom;
1193 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1194 encoding_type=ft_encoding_adobe_expert;
1195 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1196 encoding_type=ft_encoding_adobe_standard;
1197 if (LocaleCompare(encoding,"AppleRoman") == 0)
1198 encoding_type=ft_encoding_apple_roman;
1199 if (LocaleCompare(encoding,"BIG5") == 0)
1200 encoding_type=ft_encoding_big5;
1201 if (LocaleCompare(encoding,"GB2312") == 0)
1202 encoding_type=ft_encoding_gb2312;
1203 if (LocaleCompare(encoding,"Johab") == 0)
1204 encoding_type=ft_encoding_johab;
1205 #if defined(ft_encoding_latin_1)
1206 if (LocaleCompare(encoding,"Latin-1") == 0)
1207 encoding_type=ft_encoding_latin_1;
1209 if (LocaleCompare(encoding,"Latin-2") == 0)
1210 encoding_type=ft_encoding_latin_2;
1211 if (LocaleCompare(encoding,"None") == 0)
1212 encoding_type=ft_encoding_none;
1213 if (LocaleCompare(encoding,"SJIScode") == 0)
1214 encoding_type=ft_encoding_sjis;
1215 if (LocaleCompare(encoding,"Symbol") == 0)
1216 encoding_type=ft_encoding_symbol;
1217 if (LocaleCompare(encoding,"Unicode") == 0)
1218 encoding_type=ft_encoding_unicode;
1219 if (LocaleCompare(encoding,"Wansung") == 0)
1220 encoding_type=ft_encoding_wansung;
1221 ft_status=FT_Select_Charmap(face,encoding_type);
1224 (void) FT_Done_Face(face);
1225 (void) FT_Done_FreeType(library);
1226 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1232 resolution.x=DefaultResolution;
1233 resolution.y=DefaultResolution;
1234 if (draw_info->density != (char *) NULL)
1242 flags=ParseGeometry(draw_info->density,&geometry_info);
1243 resolution.x=geometry_info.rho;
1244 resolution.y=geometry_info.sigma;
1245 if ((flags & SigmaValue) == 0)
1246 resolution.y=resolution.x;
1248 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1249 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1250 (FT_UInt) resolution.y);
1251 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1252 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1253 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1254 metrics->descent=(double) face->size->metrics.descender/64.0;
1256 metrics->origin.x=0;
1257 metrics->origin.y=0;
1258 metrics->height=(double) face->size->metrics.height/64.0;
1259 metrics->max_advance=0.0;
1260 if (face->size->metrics.max_advance > MagickEpsilon)
1261 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1262 metrics->bounds.x1=0.0;
1263 metrics->bounds.y1=metrics->descent;
1264 metrics->bounds.x2=metrics->ascent+metrics->descent;
1265 metrics->bounds.y2=metrics->ascent+metrics->descent;
1266 metrics->underline_position=face->underline_position/64.0;
1267 metrics->underline_thickness=face->underline_thickness/64.0;
1268 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
1270 (void) FT_Done_Face(face);
1271 (void) FT_Done_FreeType(library);
1275 Compute bounding box.
1277 if (image->debug != MagickFalse)
1278 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1279 "font-encoding %s; text-encoding %s; pointsize %g",
1280 draw_info->font != (char *) NULL ? draw_info->font : "none",
1281 encoding != (char *) NULL ? encoding : "none",
1282 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1283 draw_info->pointsize);
1284 flags=FT_LOAD_NO_BITMAP;
1285 if (draw_info->text_antialias == MagickFalse)
1286 flags|=FT_LOAD_TARGET_MONO;
1289 #if defined(FT_LOAD_TARGET_LIGHT)
1290 flags|=FT_LOAD_TARGET_LIGHT;
1291 #elif defined(FT_LOAD_TARGET_LCD)
1292 flags|=FT_LOAD_TARGET_LCD;
1295 value=GetImageProperty(image,"type:hinting",exception);
1296 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1297 flags|=FT_LOAD_NO_HINTING;
1301 last_glyph.image=NULL;
1308 if (draw_info->render != MagickFalse)
1310 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1311 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1312 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1313 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1315 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1316 (void) CloneString(&annotate_info->primitive,"path '");
1317 if (draw_info->render != MagickFalse)
1319 if (image->storage_class != DirectClass)
1320 (void) SetImageStorageClass(image,DirectClass,exception);
1321 if (image->alpha_trait == UndefinedPixelTrait)
1322 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1325 if (draw_info->direction == RightToLeftDirection)
1329 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1330 if (GetUTFCode(p) < 0)
1332 utf8=(unsigned char *) NULL;
1333 if (GetUTFCode(p) == 0)
1337 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1338 if (utf8 != (unsigned char *) NULL)
1342 for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1345 Render UTF-8 sequence.
1347 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1349 glyph.id=FT_Get_Char_Index(face,'?');
1350 if ((glyph.id != 0) && (last_glyph.id != 0))
1352 if (FT_HAS_KERNING(face))
1357 ft_status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1358 ft_kerning_default,&kerning);
1360 origin.x+=(FT_Pos) (direction*kerning.x);
1362 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
1364 glyph.origin=origin;
1365 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1368 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1371 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1375 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
1376 metrics->bounds.x1=(double) bounds.xMin;
1377 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
1378 metrics->bounds.y1=(double) bounds.yMin;
1379 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
1380 metrics->bounds.x2=(double) bounds.xMax;
1381 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
1382 metrics->bounds.y2=(double) bounds.yMax;
1383 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1384 (draw_info->stroke_pattern != (Image *) NULL))
1386 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
1391 annotate_info->affine.tx=glyph.origin.x/64.0;
1392 annotate_info->affine.ty=glyph.origin.y/64.0;
1393 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1394 outline,&OutlineMethods,annotate_info);
1397 FT_Vector_Transform(&glyph.origin,&affine);
1398 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1399 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1400 (FT_Vector *) NULL,MagickTrue);
1403 bitmap=(FT_BitmapGlyph) glyph.image;
1404 point.x=offset->x+bitmap->left;
1405 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1406 point.x=offset->x+(origin.x >> 6);
1407 point.y=offset->y-bitmap->top;
1408 if (draw_info->render != MagickFalse)
1413 register unsigned char
1417 Rasterize the glyph.
1419 image_view=AcquireAuthenticCacheView(image,exception);
1420 p=bitmap->bitmap.buffer;
1421 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1444 if (status == MagickFalse)
1446 x_offset=(ssize_t) ceil(point.x-0.5);
1447 y_offset=(ssize_t) ceil(point.y+y-0.5);
1448 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1451 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1455 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1456 bitmap->bitmap.width,1,exception);
1457 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1459 n=y*bitmap->bitmap.pitch-1;
1460 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
1464 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1466 q+=GetPixelChannels(image);
1469 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
1470 fill_opacity=(double) (p[n])/(bitmap->bitmap.num_grays-1);
1472 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1473 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1474 if (draw_info->text_antialias == MagickFalse)
1475 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1476 if (active == MagickFalse)
1477 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1479 if (q == (Quantum *) NULL)
1481 GetPixelInfo(image,&fill_color);
1482 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1484 fill_opacity=fill_opacity*fill_color.alpha;
1485 CompositePixelOver(image,&fill_color,fill_opacity,q,
1486 GetPixelAlpha(image,q),q);
1487 if (active == MagickFalse)
1489 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1490 if (sync == MagickFalse)
1493 q+=GetPixelChannels(image);
1495 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1496 if (sync == MagickFalse)
1499 image_view=DestroyCacheView(image_view);
1501 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1502 metrics->width=bitmap->left+bitmap->bitmap.width;
1503 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1504 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1505 (IsUTFSpace(code) == MagickFalse))
1506 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
1508 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
1509 metrics->origin.x=(double) origin.x;
1510 metrics->origin.y=(double) origin.y;
1511 if (last_glyph.id != 0)
1512 FT_Done_Glyph(last_glyph.image);
1516 if (utf8 != (unsigned char *) NULL)
1517 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
1518 if (last_glyph.id != 0)
1519 FT_Done_Glyph(last_glyph.image);
1520 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1521 (draw_info->stroke_pattern != (Image *) NULL))
1523 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
1528 annotate_info->linejoin=RoundJoin;
1529 annotate_info->affine.tx=offset->x;
1530 annotate_info->affine.ty=offset->y;
1531 (void) ConcatenateString(&annotate_info->primitive,"'");
1532 (void) DrawImage(image,annotate_info,exception);
1536 Determine font metrics.
1538 glyph.id=FT_Get_Char_Index(face,'_');
1539 glyph.origin=origin;
1540 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1543 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1546 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1550 FT_Vector_Transform(&glyph.origin,&affine);
1551 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1552 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1553 (FT_Vector *) NULL,MagickTrue);
1554 bitmap=(FT_BitmapGlyph) glyph.image;
1555 if (bitmap->left > metrics->width)
1556 metrics->width=bitmap->left;
1559 FT_Done_Glyph(glyph.image);
1561 metrics->width-=metrics->bounds.x1/64.0;
1562 metrics->width+=annotate_info->stroke_width;
1563 metrics->bounds.x1/=64.0;
1564 metrics->bounds.y1/=64.0;
1565 metrics->bounds.x2/=64.0;
1566 metrics->bounds.y2/=64.0;
1567 metrics->origin.x/=64.0;
1568 metrics->origin.y/=64.0;
1570 Relinquish resources.
1572 annotate_info=DestroyDrawInfo(annotate_info);
1573 (void) FT_Done_Face(face);
1574 (void) FT_Done_FreeType(library);
1578 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1579 const char *magick_unused(encoding),const PointInfo *offset,
1580 TypeMetric *metrics,ExceptionInfo *exception)
1582 (void) ThrowMagickException(exception,GetMagickModule(),
1583 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
1584 draw_info->font != (char *) NULL ? draw_info->font : "none");
1585 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1590 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1594 + R e n d e r P o s t s c r i p t %
1598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 % RenderPostscript() renders text on the image with a Postscript font. It
1601 % also returns the bounding box of the text relative to the image.
1603 % The format of the RenderPostscript method is:
1605 % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1606 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1608 % A description of each parameter follows:
1610 % o image: the image.
1612 % o draw_info: the draw info.
1614 % o offset: (x,y) location of text relative to image.
1616 % o metrics: bounding box of text.
1618 % o exception: return any errors or warnings in this structure.
1622 static inline size_t MagickMin(const size_t x,const size_t y)
1629 static char *EscapeParenthesis(const char *text)
1644 buffer=AcquireString(text);
1646 for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
1648 if ((text[i] == '(') || (text[i] == ')'))
1659 static MagickBooleanType RenderPostscript(Image *image,
1660 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1661 ExceptionInfo *exception)
1664 filename[MaxTextExtent],
1665 geometry[MaxTextExtent],
1698 Render label with a Postscript font.
1700 if (image->debug != MagickFalse)
1701 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1702 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1703 draw_info->font : "none",draw_info->pointsize);
1705 unique_file=AcquireUniqueFileResource(filename);
1706 if (unique_file != -1)
1707 file=fdopen(unique_file,"wb");
1708 if ((unique_file == -1) || (file == (FILE *) NULL))
1710 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
1711 return(MagickFalse);
1713 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1714 (void) FormatLocaleFile(file,"/ReencodeType\n");
1715 (void) FormatLocaleFile(file,"{\n");
1716 (void) FormatLocaleFile(file," findfont dup length\n");
1717 (void) FormatLocaleFile(file,
1718 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
1719 (void) FormatLocaleFile(file,
1720 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
1721 (void) FormatLocaleFile(file,"} bind def\n");
1723 Sample to compute bounding box.
1725 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1726 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1727 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
1730 length=strlen(draw_info->text);
1731 for (i=0; i <= (ssize_t) (length+2); i++)
1733 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1734 draw_info->affine.ry*2.0*draw_info->pointsize);
1735 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1736 draw_info->affine.sy*2.0*draw_info->pointsize);
1737 if (point.x > extent.x)
1739 if (point.y > extent.y)
1742 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
1743 extent.x/2.0,extent.y/2.0);
1744 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
1745 draw_info->pointsize);
1746 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1747 (strchr(draw_info->font,'/') != (char *) NULL))
1748 (void) FormatLocaleFile(file,
1749 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1751 (void) FormatLocaleFile(file,
1752 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1754 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
1755 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1756 draw_info->affine.sy);
1757 text=EscapeParenthesis(draw_info->text);
1758 if (identity == MagickFalse)
1759 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1761 (void) FormatLocaleFile(file,"(%s) show\n",text);
1762 text=DestroyString(text);
1763 (void) FormatLocaleFile(file,"showpage\n");
1764 (void) fclose(file);
1765 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
1766 floor(extent.x+0.5),floor(extent.y+0.5));
1767 annotate_info=AcquireImageInfo();
1768 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
1770 (void) CloneString(&annotate_info->page,geometry);
1771 if (draw_info->density != (char *) NULL)
1772 (void) CloneString(&annotate_info->density,draw_info->density);
1773 annotate_info->antialias=draw_info->text_antialias;
1774 annotate_image=ReadImage(annotate_info,exception);
1775 CatchException(exception);
1776 annotate_info=DestroyImageInfo(annotate_info);
1777 (void) RelinquishUniqueFileResource(filename);
1778 if (annotate_image == (Image *) NULL)
1779 return(MagickFalse);
1780 (void) NegateImage(annotate_image,MagickFalse,exception);
1781 resolution.x=DefaultResolution;
1782 resolution.y=DefaultResolution;
1783 if (draw_info->density != (char *) NULL)
1791 flags=ParseGeometry(draw_info->density,&geometry_info);
1792 resolution.x=geometry_info.rho;
1793 resolution.y=geometry_info.sigma;
1794 if ((flags & SigmaValue) == 0)
1795 resolution.y=resolution.x;
1797 if (identity == MagickFalse)
1798 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
1804 crop_info=GetImageBoundingBox(annotate_image,exception);
1805 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
1806 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
1807 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
1809 (void) FormatLocaleString(geometry,MaxTextExtent,
1810 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
1811 crop_info.height,(double) crop_info.x,(double) crop_info.y);
1812 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
1814 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1815 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1816 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1817 metrics->ascent=metrics->pixels_per_em.x;
1818 metrics->descent=metrics->pixels_per_em.y/-5.0;
1819 metrics->width=(double) annotate_image->columns/
1820 ExpandAffine(&draw_info->affine);
1821 metrics->height=1.152*metrics->pixels_per_em.x;
1822 metrics->max_advance=metrics->pixels_per_em.x;
1823 metrics->bounds.x1=0.0;
1824 metrics->bounds.y1=metrics->descent;
1825 metrics->bounds.x2=metrics->ascent+metrics->descent;
1826 metrics->bounds.y2=metrics->ascent+metrics->descent;
1827 metrics->underline_position=(-2.0);
1828 metrics->underline_thickness=1.0;
1829 if (draw_info->render == MagickFalse)
1831 annotate_image=DestroyImage(annotate_image);
1834 if (draw_info->fill.alpha != TransparentAlpha)
1848 if (image->alpha_trait == UndefinedPixelTrait)
1849 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1850 if (annotate_image->alpha_trait == UndefinedPixelTrait)
1851 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1853 fill_color=draw_info->fill;
1854 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
1855 for (y=0; y < (ssize_t) annotate_image->rows; y++)
1863 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1865 if (q == (Quantum *) NULL)
1867 for (x=0; x < (ssize_t) annotate_image->columns; x++)
1869 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
1870 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1871 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
1872 SetPixelRed(annotate_image,fill_color.red,q);
1873 SetPixelGreen(annotate_image,fill_color.green,q);
1874 SetPixelBlue(annotate_image,fill_color.blue,q);
1875 q+=GetPixelChannels(annotate_image);
1877 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1878 if (sync == MagickFalse)
1881 annotate_view=DestroyCacheView(annotate_view);
1882 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
1883 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
1884 metrics->descent)-0.5),exception);
1886 annotate_image=DestroyImage(annotate_image);
1891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1895 + R e n d e r X 1 1 %
1899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1901 % RenderX11() renders text on the image with an X11 font. It also returns the
1902 % bounding box of the text relative to the image.
1904 % The format of the RenderX11 method is:
1906 % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
1907 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1909 % A description of each parameter follows:
1911 % o image: the image.
1913 % o draw_info: the draw info.
1915 % o offset: (x,y) location of text relative to image.
1917 % o metrics: bounding box of text.
1919 % o exception: return any errors or warnings in this structure.
1922 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1923 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1928 if (annotate_semaphore == (SemaphoreInfo *) NULL)
1929 ActivateSemaphoreInfo(&annotate_semaphore);
1930 LockSemaphoreInfo(annotate_semaphore);
1931 status=XRenderImage(image,draw_info,offset,metrics,exception);
1932 UnlockSemaphoreInfo(annotate_semaphore);