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-2014 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 AcquireSemaphoreInfo(&annotate_semaphore);
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 + 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 %
161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 % AnnotateComponentTerminus() destroys the annotate component.
165 % The format of the AnnotateComponentTerminus method is:
167 % AnnotateComponentTerminus(void)
170 MagickPrivate void AnnotateComponentTerminus(void)
172 if (annotate_semaphore == (SemaphoreInfo *) NULL)
173 AcquireSemaphoreInfo(&annotate_semaphore);
174 DestroySemaphoreInfo(&annotate_semaphore);
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 % A n n o t a t e I m a g e %
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 % AnnotateImage() annotates an image with text. Optionally you can include
189 % any of the following bits of information about the image by embedding
190 % the appropriate special characters:
192 % %b file size in bytes.
194 % %d directory in which the image resides.
195 % %e extension of the image file.
196 % %f original filename of the image.
197 % %h height of image.
198 % %i filename of the image.
199 % %k number of unique colors.
201 % %m image file format.
202 % %n number of images in a image sequence.
203 % %o output image filename.
204 % %p page number of the image.
205 % %q image depth (8 or 16).
206 % %q image depth (8 or 16).
207 % %s image scene number.
208 % %t image filename without any extension.
209 % %u a unique temporary filename.
211 % %x x resolution of the image.
212 % %y y resolution of the image.
214 % The format of the AnnotateImage method is:
216 % MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
217 % ExceptionInfo *exception)
219 % A description of each parameter follows:
221 % o image: the image.
223 % o draw_info: the draw info.
225 % o exception: return any errors or warnings in this structure.
228 MagickExport MagickBooleanType AnnotateImage(Image *image,
229 const DrawInfo *draw_info,ExceptionInfo *exception)
232 primitive[MaxTextExtent],
264 assert(image != (Image *) NULL);
265 assert(image->signature == MagickSignature);
266 if (image->debug != MagickFalse)
267 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
268 assert(draw_info != (DrawInfo *) NULL);
269 assert(draw_info->signature == MagickSignature);
270 if (draw_info->text == (char *) NULL)
272 if (*draw_info->text == '\0')
274 textlist=StringToList(draw_info->text);
275 if (textlist == (char **) NULL)
277 length=strlen(textlist[0]);
278 for (i=1; textlist[i] != (char *) NULL; i++)
279 if (strlen(textlist[i]) > length)
280 length=strlen(textlist[i]);
281 number_lines=(size_t) i;
282 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
283 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
284 SetGeometry(image,&geometry);
285 SetGeometryInfo(&geometry_info);
286 if (annotate_info->geometry != (char *) NULL)
288 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
290 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
292 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
294 if (IsGrayColorspace(image->colorspace) != MagickFalse)
295 (void) SetImageColorspace(image,sRGBColorspace,exception);
297 for (i=0; textlist[i] != (char *) NULL; i++)
300 Position text relative to image.
302 annotate_info->affine.tx=geometry_info.xi-image->page.x;
303 annotate_info->affine.ty=geometry_info.psi-image->page.y;
304 (void) CloneString(&annotate->text,textlist[i]);
305 (void) GetTypeMetrics(image,annotate,&metrics,exception);
306 height=(ssize_t) (metrics.ascent-metrics.descent+
307 draw_info->interline_spacing+0.5);
308 switch (annotate->gravity)
310 case UndefinedGravity:
313 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
314 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
317 case NorthWestGravity:
319 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
320 annotate_info->affine.ry*height+annotate_info->affine.ry*
321 (metrics.ascent+metrics.descent);
322 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
323 annotate_info->affine.sy*height+annotate_info->affine.sy*
329 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
330 geometry.width/2.0+i*annotate_info->affine.ry*height-
331 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
332 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
333 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
334 annotate_info->affine.sy*height+annotate_info->affine.sy*
335 metrics.ascent-annotate_info->affine.rx*(metrics.width-
336 metrics.bounds.x1)/2.0;
339 case NorthEastGravity:
341 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
342 geometry.width+i*annotate_info->affine.ry*height-
343 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
344 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
345 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
346 annotate_info->affine.sy*height+annotate_info->affine.sy*
347 metrics.ascent-annotate_info->affine.rx*(metrics.width-
353 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
354 annotate_info->affine.ry*height+annotate_info->affine.ry*
355 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
356 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
357 geometry.height/2.0+i*annotate_info->affine.sy*height+
358 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
359 (number_lines-1.0)*height)/2.0;
364 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
365 geometry.width/2.0+i*annotate_info->affine.ry*height-
366 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
367 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
368 (number_lines-1)*height)/2.0;
369 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
370 geometry.height/2.0+i*annotate_info->affine.sy*height-
371 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
372 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
373 (number_lines-1.0)*height)/2.0;
378 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
379 geometry.width+i*annotate_info->affine.ry*height-
380 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
381 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
382 (number_lines-1.0)*height)/2.0-1.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.rx*(metrics.width+metrics.bounds.x1)+
386 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
387 (number_lines-1.0)*height)/2.0;
390 case SouthWestGravity:
392 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
393 annotate_info->affine.ry*height-annotate_info->affine.ry*
394 (number_lines-1.0)*height;
395 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
396 geometry.height+i*annotate_info->affine.sy*height-
397 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
402 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
403 geometry.width/2.0+i*annotate_info->affine.ry*height-
404 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
405 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
406 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
407 geometry.height+i*annotate_info->affine.sy*height-
408 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
409 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
412 case SouthEastGravity:
414 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
415 geometry.width+i*annotate_info->affine.ry*height-
416 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
417 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
418 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
419 geometry.height+i*annotate_info->affine.sy*height-
420 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
421 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
425 switch (annotate->align)
429 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
430 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
435 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
436 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
437 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
438 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
443 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
444 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
445 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
446 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
452 if (draw_info->undercolor.alpha != TransparentAlpha)
460 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
461 undercolor_info->fill=draw_info->undercolor;
462 undercolor_info->affine=draw_info->affine;
463 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
464 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
465 (void) FormatLocaleString(primitive,MaxTextExtent,
466 "rectangle -0.5,-0.5 %g,%.20g",metrics.origin.x,(double) height);
467 (void) CloneString(&undercolor_info->primitive,primitive);
468 (void) DrawImage(image,undercolor_info,exception);
469 (void) DestroyDrawInfo(undercolor_info);
471 annotate_info->affine.tx=offset.x;
472 annotate_info->affine.ty=offset.y;
473 (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
474 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
475 if (annotate->decorate == OverlineDecoration)
477 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
478 metrics.descent-metrics.underline_position));
479 (void) CloneString(&annotate_info->primitive,primitive);
480 (void) DrawImage(image,annotate_info,exception);
483 if (annotate->decorate == UnderlineDecoration)
485 annotate_info->affine.ty-=(draw_info->affine.sy*
486 metrics.underline_position);
487 (void) CloneString(&annotate_info->primitive,primitive);
488 (void) DrawImage(image,annotate_info,exception);
491 Annotate image with text.
493 status=RenderType(image,annotate,&offset,&metrics,exception);
494 if (status == MagickFalse)
496 if (annotate->decorate == LineThroughDecoration)
498 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
499 metrics.underline_position+metrics.descent)/2.0);
500 (void) CloneString(&annotate_info->primitive,primitive);
501 (void) DrawImage(image,annotate_info,exception);
505 Relinquish resources.
507 annotate_info=DestroyDrawInfo(annotate_info);
508 annotate=DestroyDrawInfo(annotate);
509 for (i=0; textlist[i] != (char *) NULL; i++)
510 textlist[i]=DestroyString(textlist[i]);
511 textlist=(char **) RelinquishMagickMemory(textlist);
516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520 % F o r m a t M a g i c k C a p t i o n %
524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
526 % FormatMagickCaption() formats a caption so that it fits within the image
527 % width. It returns the number of lines in the formatted caption.
529 % The format of the FormatMagickCaption method is:
531 % ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
532 % const MagickBooleanType split,TypeMetric *metrics,char **caption,
533 % ExceptionInfo *exception)
535 % A description of each parameter follows.
537 % o image: The image.
539 % o draw_info: the draw info.
541 % o split: when no convenient line breaks-- insert newline.
543 % o metrics: Return the font metrics in this structure.
545 % o caption: the caption.
547 % o exception: return any errors or warnings in this structure.
550 MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
551 const MagickBooleanType split,TypeMetric *metrics,char **caption,
552 ExceptionInfo *exception)
574 text=AcquireString(draw_info->text);
577 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
579 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
581 if (GetUTFCode(p) == '\n')
583 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
586 status=GetTypeMetrics(image,draw_info,metrics,exception);
587 if (status == MagickFalse)
589 width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
590 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
592 (void) strcpy(text,draw_info->text);
593 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
599 if ((s != (char *) NULL) || (split != MagickFalse))
605 No convenient line breaks-- insert newline.
607 target=AcquireString(*caption);
609 CopyMagickString(target,*caption,n+1);
610 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
611 ConcatenateMagickString(target,p,strlen(*caption)+2);
612 (void) DestroyString(*caption);
619 text=DestroyString(text);
621 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
622 if (GetUTFCode(p) == '\n')
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 % G e t M u l t i l i n e T y p e M e t r i c s %
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % GetMultilineTypeMetrics() returns the following information for the
639 % specified font and text:
647 % maximum horizontal advance
655 % underline thickness
657 % This method is like GetTypeMetrics() but it returns the maximum text width
658 % and height for multiple lines of text.
660 % The format of the GetMultilineTypeMetrics method is:
662 % MagickBooleanType GetMultilineTypeMetrics(Image *image,
663 % const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
665 % A description of each parameter follows:
667 % o image: the image.
669 % o draw_info: the draw info.
671 % o metrics: Return the font metrics in this structure.
673 % o exception: return any errors or warnings in this structure.
676 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
677 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
694 assert(image != (Image *) NULL);
695 assert(image->signature == MagickSignature);
696 if (image->debug != MagickFalse)
697 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
698 assert(draw_info != (DrawInfo *) NULL);
699 assert(draw_info->text != (char *) NULL);
700 assert(draw_info->signature == MagickSignature);
701 if (*draw_info->text == '\0')
703 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
704 annotate_info->text=DestroyString(annotate_info->text);
706 Convert newlines to multiple lines of text.
708 textlist=StringToList(draw_info->text);
709 if (textlist == (char **) NULL)
711 annotate_info->render=MagickFalse;
712 annotate_info->direction=UndefinedDirection;
713 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
714 (void) ResetMagickMemory(&extent,0,sizeof(extent));
716 Find the widest of the text lines.
718 annotate_info->text=textlist[0];
719 status=GetTypeMetrics(image,annotate_info,&extent,exception);
721 for (i=1; textlist[i] != (char *) NULL; i++)
723 annotate_info->text=textlist[i];
724 status=GetTypeMetrics(image,annotate_info,&extent,exception);
725 if (extent.width > metrics->width)
728 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
729 (i-1)*draw_info->interline_spacing);
731 Relinquish resources.
733 annotate_info->text=(char *) NULL;
734 annotate_info=DestroyDrawInfo(annotate_info);
735 for (i=0; textlist[i] != (char *) NULL; i++)
736 textlist[i]=DestroyString(textlist[i]);
737 textlist=(char **) RelinquishMagickMemory(textlist);
742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
746 % G e t T y p e M e t r i c s %
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752 % GetTypeMetrics() returns the following information for the specified font
761 % maximum horizontal advance
769 % underline thickness
771 % The format of the GetTypeMetrics method is:
773 % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
774 % TypeMetric *metrics,ExceptionInfo *exception)
776 % A description of each parameter follows:
778 % o image: the image.
780 % o draw_info: the draw info.
782 % o metrics: Return the font metrics in this structure.
784 % o exception: return any errors or warnings in this structure.
787 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
788 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
799 assert(image != (Image *) NULL);
800 assert(image->signature == MagickSignature);
801 if (image->debug != MagickFalse)
802 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
803 assert(draw_info != (DrawInfo *) NULL);
804 assert(draw_info->text != (char *) NULL);
805 assert(draw_info->signature == MagickSignature);
806 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
807 annotate_info->render=MagickFalse;
808 annotate_info->direction=UndefinedDirection;
809 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
812 status=RenderType(image,annotate_info,&offset,metrics,exception);
813 if (image->debug != MagickFalse)
814 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
815 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
816 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
817 "underline position: %g; underline thickness: %g",annotate_info->text,
818 metrics->width,metrics->height,metrics->ascent,metrics->descent,
819 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
820 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
821 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
822 metrics->underline_position,metrics->underline_thickness);
823 annotate_info=DestroyDrawInfo(annotate_info);
828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832 + R e n d e r T y p e %
836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838 % RenderType() renders text on the image. It also returns the bounding box of
839 % the text relative to the image.
841 % The format of the RenderType method is:
843 % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
844 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
846 % A description of each parameter follows:
848 % o image: the image.
850 % o draw_info: the draw info.
852 % o offset: (x,y) location of text relative to image.
854 % o metrics: bounding box of text.
856 % o exception: return any errors or warnings in this structure.
859 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
860 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
871 type_info=(const TypeInfo *) NULL;
872 if (draw_info->font != (char *) NULL)
874 if (*draw_info->font == '@')
876 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
880 if (*draw_info->font == '-')
881 return(RenderX11(image,draw_info,offset,metrics,exception));
882 if (IsPathAccessible(draw_info->font) != MagickFalse)
884 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
888 type_info=GetTypeInfo(draw_info->font,exception);
889 if (type_info == (const TypeInfo *) NULL)
890 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
891 "UnableToReadFont","`%s'",draw_info->font);
893 if ((type_info == (const TypeInfo *) NULL) &&
894 (draw_info->family != (const char *) NULL))
896 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
897 draw_info->stretch,draw_info->weight,exception);
898 if (type_info == (const TypeInfo *) NULL)
899 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
900 "UnableToReadFont","`%s'",draw_info->family);
902 if (type_info == (const TypeInfo *) NULL)
903 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
904 draw_info->stretch,draw_info->weight,exception);
905 if (type_info == (const TypeInfo *) NULL)
906 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
907 draw_info->stretch,draw_info->weight,exception);
908 if (type_info == (const TypeInfo *) NULL)
909 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
910 draw_info->stretch,draw_info->weight,exception);
911 if (type_info == (const TypeInfo *) NULL)
912 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
913 draw_info->stretch,draw_info->weight,exception);
914 if (type_info == (const TypeInfo *) NULL)
915 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
916 draw_info->stretch,draw_info->weight,exception);
917 if (type_info == (const TypeInfo *) NULL)
918 type_info=GetTypeInfo("*",exception);
919 if (type_info == (const TypeInfo *) NULL)
921 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
925 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
926 annotate_info->face=type_info->face;
927 if (type_info->metrics != (char *) NULL)
928 (void) CloneString(&annotate_info->metrics,type_info->metrics);
929 if (type_info->glyphs != (char *) NULL)
930 (void) CloneString(&annotate_info->font,type_info->glyphs);
931 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
933 annotate_info=DestroyDrawInfo(annotate_info);
938 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942 + R e n d e r F r e e t y p e %
946 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948 % RenderFreetype() renders text on the image with a Truetype font. It also
949 % returns the bounding box of the text relative to the image.
951 % The format of the RenderFreetype method is:
953 % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
954 % const char *encoding,const PointInfo *offset,TypeMetric *metrics,
955 % ExceptionInfo *exception)
957 % A description of each parameter follows:
959 % o image: the image.
961 % o draw_info: the draw info.
963 % o encoding: the font encoding.
965 % o offset: (x,y) location of text relative to image.
967 % o metrics: bounding box of text.
969 % o exception: return any errors or warnings in this structure.
973 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
975 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
984 affine=draw_info->affine;
985 (void) FormatLocaleString(path,MaxTextExtent,
986 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
987 p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
988 affine.ty-to->y/64.0);
989 (void) ConcatenateString(&draw_info->primitive,path);
993 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1001 affine=draw_info->affine;
1002 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
1003 to->x/64.0,affine.ty-to->y/64.0);
1004 (void) ConcatenateString(&draw_info->primitive,path);
1008 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1014 path[MaxTextExtent];
1016 affine=draw_info->affine;
1017 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
1018 to->x/64.0,affine.ty-to->y/64.0);
1019 (void) ConcatenateString(&draw_info->primitive,path);
1023 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1024 DrawInfo *draw_info)
1030 path[MaxTextExtent];
1032 affine=draw_info->affine;
1033 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
1034 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1035 affine.ty-to->y/64.0);
1036 (void) ConcatenateString(&draw_info->primitive,path);
1040 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1041 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1042 ExceptionInfo *exception)
1044 #if !defined(FT_OPEN_PATHNAME)
1045 #define FT_OPEN_PATHNAME ft_open_pathname
1048 typedef struct _GlyphInfo
1117 static FT_Outline_Funcs
1120 (FT_Outline_MoveTo_Func) TraceMoveTo,
1121 (FT_Outline_LineTo_Func) TraceLineTo,
1122 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1123 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1131 Initialize Truetype library.
1133 ft_status=FT_Init_FreeType(&library);
1135 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1137 args.flags=FT_OPEN_PATHNAME;
1138 if (draw_info->font == (char *) NULL)
1139 args.pathname=ConstantString("helvetica");
1141 if (*draw_info->font != '@')
1142 args.pathname=ConstantString(draw_info->font);
1144 args.pathname=ConstantString(draw_info->font+1);
1145 face=(FT_Face) NULL;
1146 ft_status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
1147 args.pathname=DestroyString(args.pathname);
1150 (void) FT_Done_FreeType(library);
1151 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
1152 "UnableToReadFont","`%s'",draw_info->font);
1153 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1155 if ((draw_info->metrics != (char *) NULL) &&
1156 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1157 (void) FT_Attach_File(face,draw_info->metrics);
1158 encoding_type=ft_encoding_unicode;
1159 ft_status=FT_Select_Charmap(face,encoding_type);
1160 if ((ft_status != 0) && (face->num_charmaps != 0))
1161 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1162 if (encoding != (const char *) NULL)
1164 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1165 encoding_type=ft_encoding_adobe_custom;
1166 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1167 encoding_type=ft_encoding_adobe_expert;
1168 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1169 encoding_type=ft_encoding_adobe_standard;
1170 if (LocaleCompare(encoding,"AppleRoman") == 0)
1171 encoding_type=ft_encoding_apple_roman;
1172 if (LocaleCompare(encoding,"BIG5") == 0)
1173 encoding_type=ft_encoding_big5;
1174 if (LocaleCompare(encoding,"GB2312") == 0)
1175 encoding_type=ft_encoding_gb2312;
1176 if (LocaleCompare(encoding,"Johab") == 0)
1177 encoding_type=ft_encoding_johab;
1178 #if defined(ft_encoding_latin_1)
1179 if (LocaleCompare(encoding,"Latin-1") == 0)
1180 encoding_type=ft_encoding_latin_1;
1182 if (LocaleCompare(encoding,"Latin-2") == 0)
1183 encoding_type=ft_encoding_latin_2;
1184 if (LocaleCompare(encoding,"None") == 0)
1185 encoding_type=ft_encoding_none;
1186 if (LocaleCompare(encoding,"SJIScode") == 0)
1187 encoding_type=ft_encoding_sjis;
1188 if (LocaleCompare(encoding,"Symbol") == 0)
1189 encoding_type=ft_encoding_symbol;
1190 if (LocaleCompare(encoding,"Unicode") == 0)
1191 encoding_type=ft_encoding_unicode;
1192 if (LocaleCompare(encoding,"Wansung") == 0)
1193 encoding_type=ft_encoding_wansung;
1194 ft_status=FT_Select_Charmap(face,encoding_type);
1196 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1201 resolution.x=DefaultResolution;
1202 resolution.y=DefaultResolution;
1203 if (draw_info->density != (char *) NULL)
1211 flags=ParseGeometry(draw_info->density,&geometry_info);
1212 resolution.x=geometry_info.rho;
1213 resolution.y=geometry_info.sigma;
1214 if ((flags & SigmaValue) == 0)
1215 resolution.y=resolution.x;
1217 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1218 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1219 (FT_UInt) resolution.y);
1220 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1221 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1222 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1223 metrics->descent=(double) face->size->metrics.descender/64.0;
1225 metrics->origin.x=0;
1226 metrics->origin.y=0;
1227 metrics->height=(double) face->size->metrics.height/64.0;
1228 metrics->max_advance=0.0;
1229 if (face->size->metrics.max_advance > MagickEpsilon)
1230 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1231 metrics->bounds.x1=0.0;
1232 metrics->bounds.y1=metrics->descent;
1233 metrics->bounds.x2=metrics->ascent+metrics->descent;
1234 metrics->bounds.y2=metrics->ascent+metrics->descent;
1235 metrics->underline_position=face->underline_position/64.0;
1236 metrics->underline_thickness=face->underline_thickness/64.0;
1237 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
1239 (void) FT_Done_Face(face);
1240 (void) FT_Done_FreeType(library);
1244 Compute bounding box.
1246 if (image->debug != MagickFalse)
1247 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1248 "font-encoding %s; text-encoding %s; pointsize %g",
1249 draw_info->font != (char *) NULL ? draw_info->font : "none",
1250 encoding != (char *) NULL ? encoding : "none",
1251 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1252 draw_info->pointsize);
1253 flags=FT_LOAD_NO_BITMAP;
1254 if (draw_info->text_antialias == MagickFalse)
1255 flags|=FT_LOAD_TARGET_MONO;
1258 #if defined(FT_LOAD_TARGET_LIGHT)
1259 flags|=FT_LOAD_TARGET_LIGHT;
1260 #elif defined(FT_LOAD_TARGET_LCD)
1261 flags|=FT_LOAD_TARGET_LCD;
1264 value=GetImageProperty(image,"type:hinting",exception);
1265 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1266 flags|=FT_LOAD_NO_HINTING;
1270 last_glyph.image=NULL;
1277 if (draw_info->render != MagickFalse)
1279 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1280 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1281 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1282 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1284 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1285 (void) CloneString(&annotate_info->primitive,"path '");
1286 if (draw_info->render != MagickFalse)
1288 if (image->storage_class != DirectClass)
1289 (void) SetImageStorageClass(image,DirectClass,exception);
1290 if (image->alpha_trait != BlendPixelTrait)
1291 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1294 if (draw_info->direction == RightToLeftDirection)
1298 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1299 if (GetUTFCode(p) < 0)
1301 utf8=(unsigned char *) NULL;
1302 if (GetUTFCode(p) == 0)
1306 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1307 if (utf8 != (unsigned char *) NULL)
1311 for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1314 Render UTF-8 sequence.
1316 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1318 glyph.id=FT_Get_Char_Index(face,'?');
1319 if ((glyph.id != 0) && (last_glyph.id != 0))
1321 if (fabs(draw_info->kerning) >= MagickEpsilon)
1322 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
1324 if (FT_HAS_KERNING(face))
1329 ft_status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1330 ft_kerning_default,&kerning);
1332 origin.x+=(FT_Pos) (direction*kerning.x);
1335 glyph.origin=origin;
1336 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1339 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1342 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1346 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
1347 metrics->bounds.x1=(double) bounds.xMin;
1348 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
1349 metrics->bounds.y1=(double) bounds.yMin;
1350 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
1351 metrics->bounds.x2=(double) bounds.xMax;
1352 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
1353 metrics->bounds.y2=(double) bounds.yMax;
1354 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1355 (draw_info->stroke_pattern != (Image *) NULL))
1357 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
1362 annotate_info->affine.tx=glyph.origin.x/64.0;
1363 annotate_info->affine.ty=glyph.origin.y/64.0;
1364 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1365 outline,&OutlineMethods,annotate_info);
1368 FT_Vector_Transform(&glyph.origin,&affine);
1369 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1370 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1371 (FT_Vector *) NULL,MagickTrue);
1374 bitmap=(FT_BitmapGlyph) glyph.image;
1375 point.x=offset->x+bitmap->left;
1376 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1377 point.x=offset->x+(origin.x >> 6);
1378 point.y=offset->y-bitmap->top;
1379 if (draw_info->render != MagickFalse)
1384 register unsigned char
1388 Rasterize the glyph.
1390 image_view=AcquireAuthenticCacheView(image,exception);
1391 p=bitmap->bitmap.buffer;
1392 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1415 if (status == MagickFalse)
1417 x_offset=(ssize_t) ceil(point.x-0.5);
1418 y_offset=(ssize_t) ceil(point.y+y-0.5);
1419 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1422 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1426 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1427 bitmap->bitmap.width,1,exception);
1428 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1430 n=y*bitmap->bitmap.pitch-1;
1431 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
1435 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1437 q+=GetPixelChannels(image);
1440 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
1441 fill_opacity=(double) (p[n])/(bitmap->bitmap.num_grays-1);
1443 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1444 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1445 if (draw_info->text_antialias == MagickFalse)
1446 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1447 if (active == MagickFalse)
1448 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1450 if (q == (Quantum *) NULL)
1452 q+=GetPixelChannels(image);
1455 GetPixelInfo(image,&fill_color);
1456 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1458 fill_opacity=fill_opacity*fill_color.alpha;
1459 CompositePixelOver(image,&fill_color,fill_opacity,q,
1460 GetPixelAlpha(image,q),q);
1461 if (active == MagickFalse)
1463 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1464 if (sync == MagickFalse)
1467 q+=GetPixelChannels(image);
1469 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1470 if (sync == MagickFalse)
1473 image_view=DestroyCacheView(image_view);
1475 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1476 metrics->width=bitmap->left+bitmap->bitmap.width;
1477 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1478 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1479 (IsUTFSpace(code) == MagickFalse))
1480 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
1482 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
1483 metrics->origin.x=(double) origin.x;
1484 metrics->origin.y=(double) origin.y;
1485 if (last_glyph.id != 0)
1486 FT_Done_Glyph(last_glyph.image);
1490 if (utf8 != (unsigned char *) NULL)
1491 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
1492 if (last_glyph.id != 0)
1493 FT_Done_Glyph(last_glyph.image);
1494 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1495 (draw_info->stroke_pattern != (Image *) NULL))
1497 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
1502 annotate_info->linejoin=RoundJoin;
1503 annotate_info->affine.tx=offset->x;
1504 annotate_info->affine.ty=offset->y;
1505 (void) ConcatenateString(&annotate_info->primitive,"'");
1506 (void) DrawImage(image,annotate_info,exception);
1510 Determine font metrics.
1512 glyph.id=FT_Get_Char_Index(face,'_');
1513 glyph.origin=origin;
1514 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1517 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1520 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1524 FT_Vector_Transform(&glyph.origin,&affine);
1525 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1526 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1527 (FT_Vector *) NULL,MagickTrue);
1528 bitmap=(FT_BitmapGlyph) glyph.image;
1529 if (bitmap->left > metrics->width)
1530 metrics->width=bitmap->left;
1533 FT_Done_Glyph(glyph.image);
1535 metrics->width-=metrics->bounds.x1/64.0;
1536 metrics->width+=annotate_info->stroke_width;
1537 metrics->bounds.x1/=64.0;
1538 metrics->bounds.y1/=64.0;
1539 metrics->bounds.x2/=64.0;
1540 metrics->bounds.y2/=64.0;
1541 metrics->origin.x/=64.0;
1542 metrics->origin.y/=64.0;
1544 Relinquish resources.
1546 annotate_info=DestroyDrawInfo(annotate_info);
1547 (void) FT_Done_Face(face);
1548 (void) FT_Done_FreeType(library);
1552 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1553 const char *magick_unused(encoding),const PointInfo *offset,
1554 TypeMetric *metrics,ExceptionInfo *exception)
1556 (void) ThrowMagickException(exception,GetMagickModule(),
1557 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
1558 draw_info->font != (char *) NULL ? draw_info->font : "none");
1559 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1568 + R e n d e r P o s t s c r i p t %
1572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1574 % RenderPostscript() renders text on the image with a Postscript font. It
1575 % also returns the bounding box of the text relative to the image.
1577 % The format of the RenderPostscript method is:
1579 % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1580 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1582 % A description of each parameter follows:
1584 % o image: the image.
1586 % o draw_info: the draw info.
1588 % o offset: (x,y) location of text relative to image.
1590 % o metrics: bounding box of text.
1592 % o exception: return any errors or warnings in this structure.
1596 static inline size_t MagickMin(const size_t x,const size_t y)
1603 static char *EscapeParenthesis(const char *text)
1618 buffer=AcquireString(text);
1620 for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
1622 if ((text[i] == '(') || (text[i] == ')'))
1633 static MagickBooleanType RenderPostscript(Image *image,
1634 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1635 ExceptionInfo *exception)
1638 filename[MaxTextExtent],
1639 geometry[MaxTextExtent],
1672 Render label with a Postscript font.
1674 if (image->debug != MagickFalse)
1675 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1676 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1677 draw_info->font : "none",draw_info->pointsize);
1679 unique_file=AcquireUniqueFileResource(filename);
1680 if (unique_file != -1)
1681 file=fdopen(unique_file,"wb");
1682 if ((unique_file == -1) || (file == (FILE *) NULL))
1684 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
1685 return(MagickFalse);
1687 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1688 (void) FormatLocaleFile(file,"/ReencodeType\n");
1689 (void) FormatLocaleFile(file,"{\n");
1690 (void) FormatLocaleFile(file," findfont dup length\n");
1691 (void) FormatLocaleFile(file,
1692 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
1693 (void) FormatLocaleFile(file,
1694 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
1695 (void) FormatLocaleFile(file,"} bind def\n");
1697 Sample to compute bounding box.
1699 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1700 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1701 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
1704 length=strlen(draw_info->text);
1705 for (i=0; i <= (ssize_t) (length+2); i++)
1707 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1708 draw_info->affine.ry*2.0*draw_info->pointsize);
1709 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1710 draw_info->affine.sy*2.0*draw_info->pointsize);
1711 if (point.x > extent.x)
1713 if (point.y > extent.y)
1716 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
1717 extent.x/2.0,extent.y/2.0);
1718 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
1719 draw_info->pointsize);
1720 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1721 (strchr(draw_info->font,'/') != (char *) NULL))
1722 (void) FormatLocaleFile(file,
1723 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1725 (void) FormatLocaleFile(file,
1726 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1728 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
1729 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1730 draw_info->affine.sy);
1731 text=EscapeParenthesis(draw_info->text);
1732 if (identity == MagickFalse)
1733 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1735 (void) FormatLocaleFile(file,"(%s) show\n",text);
1736 text=DestroyString(text);
1737 (void) FormatLocaleFile(file,"showpage\n");
1738 (void) fclose(file);
1739 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
1740 floor(extent.x+0.5),floor(extent.y+0.5));
1741 annotate_info=AcquireImageInfo();
1742 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
1744 (void) CloneString(&annotate_info->page,geometry);
1745 if (draw_info->density != (char *) NULL)
1746 (void) CloneString(&annotate_info->density,draw_info->density);
1747 annotate_info->antialias=draw_info->text_antialias;
1748 annotate_image=ReadImage(annotate_info,exception);
1749 CatchException(exception);
1750 annotate_info=DestroyImageInfo(annotate_info);
1751 (void) RelinquishUniqueFileResource(filename);
1752 if (annotate_image == (Image *) NULL)
1753 return(MagickFalse);
1754 (void) NegateImage(annotate_image,MagickFalse,exception);
1755 resolution.x=DefaultResolution;
1756 resolution.y=DefaultResolution;
1757 if (draw_info->density != (char *) NULL)
1765 flags=ParseGeometry(draw_info->density,&geometry_info);
1766 resolution.x=geometry_info.rho;
1767 resolution.y=geometry_info.sigma;
1768 if ((flags & SigmaValue) == 0)
1769 resolution.y=resolution.x;
1771 if (identity == MagickFalse)
1772 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
1778 crop_info=GetImageBoundingBox(annotate_image,exception);
1779 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
1780 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
1781 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
1783 (void) FormatLocaleString(geometry,MaxTextExtent,
1784 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
1785 crop_info.height,(double) crop_info.x,(double) crop_info.y);
1786 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
1788 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1789 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1790 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1791 metrics->ascent=metrics->pixels_per_em.x;
1792 metrics->descent=metrics->pixels_per_em.y/-5.0;
1793 metrics->width=(double) annotate_image->columns/
1794 ExpandAffine(&draw_info->affine);
1795 metrics->height=1.152*metrics->pixels_per_em.x;
1796 metrics->max_advance=metrics->pixels_per_em.x;
1797 metrics->bounds.x1=0.0;
1798 metrics->bounds.y1=metrics->descent;
1799 metrics->bounds.x2=metrics->ascent+metrics->descent;
1800 metrics->bounds.y2=metrics->ascent+metrics->descent;
1801 metrics->underline_position=(-2.0);
1802 metrics->underline_thickness=1.0;
1803 if (draw_info->render == MagickFalse)
1805 annotate_image=DestroyImage(annotate_image);
1808 if (draw_info->fill.alpha != TransparentAlpha)
1822 if (image->alpha_trait != BlendPixelTrait)
1823 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1824 if (annotate_image->alpha_trait != BlendPixelTrait)
1825 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1827 fill_color=draw_info->fill;
1828 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
1829 for (y=0; y < (ssize_t) annotate_image->rows; y++)
1837 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1839 if (q == (Quantum *) NULL)
1841 for (x=0; x < (ssize_t) annotate_image->columns; x++)
1843 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
1844 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1845 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
1846 SetPixelRed(annotate_image,fill_color.red,q);
1847 SetPixelGreen(annotate_image,fill_color.green,q);
1848 SetPixelBlue(annotate_image,fill_color.blue,q);
1849 q+=GetPixelChannels(annotate_image);
1851 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1852 if (sync == MagickFalse)
1855 annotate_view=DestroyCacheView(annotate_view);
1856 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
1857 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
1858 metrics->descent)-0.5),exception);
1860 annotate_image=DestroyImage(annotate_image);
1865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1869 + R e n d e r X 1 1 %
1873 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1875 % RenderX11() renders text on the image with an X11 font. It also returns the
1876 % bounding box of the text relative to the image.
1878 % The format of the RenderX11 method is:
1880 % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
1881 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1883 % A description of each parameter follows:
1885 % o image: the image.
1887 % o draw_info: the draw info.
1889 % o offset: (x,y) location of text relative to image.
1891 % o metrics: bounding box of text.
1893 % o exception: return any errors or warnings in this structure.
1896 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1897 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1902 if (annotate_semaphore == (SemaphoreInfo *) NULL)
1903 AcquireSemaphoreInfo(&annotate_semaphore);
1904 LockSemaphoreInfo(annotate_semaphore);
1905 status=XRenderImage(image,draw_info,offset,metrics,exception);
1906 UnlockSemaphoreInfo(annotate_semaphore);