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-2013 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/client.h"
51 #include "MagickCore/color.h"
52 #include "MagickCore/color-private.h"
53 #include "MagickCore/colorspace-private.h"
54 #include "MagickCore/composite.h"
55 #include "MagickCore/composite-private.h"
56 #include "MagickCore/constitute.h"
57 #include "MagickCore/draw.h"
58 #include "MagickCore/draw-private.h"
59 #include "MagickCore/enhance.h"
60 #include "MagickCore/exception.h"
61 #include "MagickCore/exception-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/geometry.h"
64 #include "MagickCore/image-private.h"
65 #include "MagickCore/log.h"
66 #include "MagickCore/quantum.h"
67 #include "MagickCore/quantum-private.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/property.h"
70 #include "MagickCore/resource_.h"
71 #include "MagickCore/semaphore.h"
72 #include "MagickCore/statistic.h"
73 #include "MagickCore/string_.h"
74 #include "MagickCore/token-private.h"
75 #include "MagickCore/transform.h"
76 #include "MagickCore/type.h"
77 #include "MagickCore/utility.h"
78 #include "MagickCore/utility-private.h"
79 #include "MagickCore/xwindow.h"
80 #include "MagickCore/xwindow-private.h"
81 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
82 #if defined(__MINGW32__) || defined(__MINGW64__)
86 #if defined(FT_FREETYPE_H)
87 # include FT_FREETYPE_H
89 # include <freetype/freetype.h>
91 #if defined(FT_GLYPH_H)
94 # include <freetype/ftglyph.h>
96 #if defined(FT_OUTLINE_H)
97 # include FT_OUTLINE_H
99 # include <freetype/ftoutln.h>
101 #if defined(FT_BBOX_H)
104 # include <freetype/ftbbox.h>
105 #endif /* defined(FT_BBOX_H) */
112 *annotate_semaphore = (SemaphoreInfo *) NULL;
115 Forward declarations.
117 static MagickBooleanType
118 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
120 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
122 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
123 TypeMetric *,ExceptionInfo *),
124 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 + A n n o t a t e C o m p o n e n t G e n e s i s %
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 % AnnotateComponentGenesis() instantiates the annotate component.
140 % The format of the AnnotateComponentGenesis method is:
142 % MagickBooleanType AnnotateComponentGenesis(void)
145 MagickPrivate MagickBooleanType AnnotateComponentGenesis(void)
147 AcquireSemaphoreInfo(&annotate_semaphore);
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 + 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 %
160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 % AnnotateComponentTerminus() destroys the annotate component.
164 % The format of the AnnotateComponentTerminus method is:
166 % AnnotateComponentTerminus(void)
169 MagickPrivate void AnnotateComponentTerminus(void)
171 if (annotate_semaphore == (SemaphoreInfo *) NULL)
172 AcquireSemaphoreInfo(&annotate_semaphore);
173 DestroySemaphoreInfo(&annotate_semaphore);
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 % A n n o t a t e I m a g e %
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 % AnnotateImage() annotates an image with text. Optionally you can include
188 % any of the following bits of information about the image by embedding
189 % the appropriate special characters:
191 % %b file size in bytes.
193 % %d directory in which the image resides.
194 % %e extension of the image file.
195 % %f original filename of the image.
196 % %h height of image.
197 % %i filename of the image.
198 % %k number of unique colors.
200 % %m image file format.
201 % %n number of images in a image sequence.
202 % %o output image filename.
203 % %p page number of the image.
204 % %q image depth (8 or 16).
205 % %q image depth (8 or 16).
206 % %s image scene number.
207 % %t image filename without any extension.
208 % %u a unique temporary filename.
210 % %x x resolution of the image.
211 % %y y resolution of the image.
213 % The format of the AnnotateImage method is:
215 % MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
216 % ExceptionInfo *exception)
218 % A description of each parameter follows:
220 % o image: the image.
222 % o draw_info: the draw info.
224 % o exception: return any errors or warnings in this structure.
227 MagickExport MagickBooleanType AnnotateImage(Image *image,
228 const DrawInfo *draw_info,ExceptionInfo *exception)
231 primitive[MaxTextExtent],
263 assert(image != (Image *) NULL);
264 assert(image->signature == MagickSignature);
265 if (image->debug != MagickFalse)
266 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
267 assert(draw_info != (DrawInfo *) NULL);
268 assert(draw_info->signature == MagickSignature);
269 if (draw_info->text == (char *) NULL)
271 if (*draw_info->text == '\0')
273 textlist=StringToList(draw_info->text);
274 if (textlist == (char **) NULL)
276 length=strlen(textlist[0]);
277 for (i=1; textlist[i] != (char *) NULL; i++)
278 if (strlen(textlist[i]) > length)
279 length=strlen(textlist[i]);
280 number_lines=(size_t) i;
281 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
282 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
283 SetGeometry(image,&geometry);
284 SetGeometryInfo(&geometry_info);
285 if (annotate_info->geometry != (char *) NULL)
287 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
289 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
291 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
293 if (IsGrayColorspace(image->colorspace) != MagickFalse)
294 (void) TransformImageColorspace(image,sRGBColorspace,exception);
296 for (i=0; textlist[i] != (char *) NULL; i++)
299 Position text relative to image.
301 annotate_info->affine.tx=geometry_info.xi-image->page.x;
302 annotate_info->affine.ty=geometry_info.psi-image->page.y;
303 (void) CloneString(&annotate->text,textlist[i]);
304 (void) GetTypeMetrics(image,annotate,&metrics,exception);
305 height=(ssize_t) (metrics.ascent-metrics.descent+
306 draw_info->interline_spacing+0.5);
307 switch (annotate->gravity)
309 case UndefinedGravity:
312 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
313 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
316 case NorthWestGravity:
318 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
319 annotate_info->affine.ry*height+annotate_info->affine.ry*
320 (metrics.ascent+metrics.descent);
321 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
322 annotate_info->affine.sy*height+annotate_info->affine.sy*
328 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
329 geometry.width/2.0+i*annotate_info->affine.ry*height-
330 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
331 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
332 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
333 annotate_info->affine.sy*height+annotate_info->affine.sy*
334 metrics.ascent-annotate_info->affine.rx*(metrics.width-
335 metrics.bounds.x1)/2.0;
338 case NorthEastGravity:
340 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
341 geometry.width+i*annotate_info->affine.ry*height-
342 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
343 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
344 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
345 annotate_info->affine.sy*height+annotate_info->affine.sy*
346 metrics.ascent-annotate_info->affine.rx*(metrics.width-
352 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
353 annotate_info->affine.ry*height+annotate_info->affine.ry*
354 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
355 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
356 geometry.height/2.0+i*annotate_info->affine.sy*height+
357 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
358 (number_lines-1.0)*height)/2.0;
363 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
364 geometry.width/2.0+i*annotate_info->affine.ry*height-
365 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
366 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
367 (number_lines-1)*height)/2.0;
368 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
369 geometry.height/2.0+i*annotate_info->affine.sy*height-
370 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
371 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
372 (number_lines-1.0)*height)/2.0;
377 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
378 geometry.width+i*annotate_info->affine.ry*height-
379 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
380 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
381 (number_lines-1.0)*height)/2.0-1.0;
382 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
383 geometry.height/2.0+i*annotate_info->affine.sy*height-
384 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+
385 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
386 (number_lines-1.0)*height)/2.0;
389 case SouthWestGravity:
391 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
392 annotate_info->affine.ry*height-annotate_info->affine.ry*
393 (number_lines-1.0)*height;
394 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
395 geometry.height+i*annotate_info->affine.sy*height-
396 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
401 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
402 geometry.width/2.0+i*annotate_info->affine.ry*height-
403 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
404 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
405 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
406 geometry.height+i*annotate_info->affine.sy*height-
407 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
408 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
411 case SouthEastGravity:
413 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
414 geometry.width+i*annotate_info->affine.ry*height-
415 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
416 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
417 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
418 geometry.height+i*annotate_info->affine.sy*height-
419 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
420 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
424 switch (annotate->align)
428 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
429 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
434 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
435 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
436 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
437 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
442 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
443 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
444 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
445 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
451 if (draw_info->undercolor.alpha != TransparentAlpha)
459 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
460 undercolor_info->fill=draw_info->undercolor;
461 undercolor_info->affine=draw_info->affine;
462 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
463 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
464 (void) FormatLocaleString(primitive,MaxTextExtent,
465 "rectangle 0,0 %g,%.20g",metrics.origin.x,(double) height);
466 (void) CloneString(&undercolor_info->primitive,primitive);
467 (void) DrawImage(image,undercolor_info,exception);
468 (void) DestroyDrawInfo(undercolor_info);
470 annotate_info->affine.tx=offset.x;
471 annotate_info->affine.ty=offset.y;
472 (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
473 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
474 if (annotate->decorate == OverlineDecoration)
476 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
477 metrics.descent-metrics.underline_position));
478 (void) CloneString(&annotate_info->primitive,primitive);
479 (void) DrawImage(image,annotate_info,exception);
482 if (annotate->decorate == UnderlineDecoration)
484 annotate_info->affine.ty-=(draw_info->affine.sy*
485 metrics.underline_position);
486 (void) CloneString(&annotate_info->primitive,primitive);
487 (void) DrawImage(image,annotate_info,exception);
490 Annotate image with text.
492 status=RenderType(image,annotate,&offset,&metrics,exception);
493 if (status == MagickFalse)
495 if (annotate->decorate == LineThroughDecoration)
497 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
498 metrics.underline_position+metrics.descent)/2.0);
499 (void) CloneString(&annotate_info->primitive,primitive);
500 (void) DrawImage(image,annotate_info,exception);
504 Relinquish resources.
506 annotate_info=DestroyDrawInfo(annotate_info);
507 annotate=DestroyDrawInfo(annotate);
508 for (i=0; textlist[i] != (char *) NULL; i++)
509 textlist[i]=DestroyString(textlist[i]);
510 textlist=(char **) RelinquishMagickMemory(textlist);
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 % F o r m a t M a g i c k C a p t i o n %
523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525 % FormatMagickCaption() formats a caption so that it fits within the image
526 % width. It returns the number of lines in the formatted caption.
528 % The format of the FormatMagickCaption method is:
530 % ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
531 % const MagickBooleanType split,TypeMetric *metrics,char **caption,
532 % ExceptionInfo *exception)
534 % A description of each parameter follows.
536 % o image: The image.
538 % o draw_info: the draw info.
540 % o split: when no convenient line breaks-- insert newline.
542 % o metrics: Return the font metrics in this structure.
544 % o caption: the caption.
546 % o exception: return any errors or warnings in this structure.
549 MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
550 const MagickBooleanType split,TypeMetric *metrics,char **caption,
551 ExceptionInfo *exception)
573 text=AcquireString(draw_info->text);
576 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
578 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
580 if (GetUTFCode(p) == '\n')
582 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
585 status=GetTypeMetrics(image,draw_info,metrics,exception);
586 if (status == MagickFalse)
588 width=(size_t) floor(metrics->width+metrics->max_advance/2.0+0.5);
589 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
591 (void) strcpy(text,draw_info->text);
592 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
598 if ((s != (char *) NULL) || (split != MagickFalse))
604 No convenient line breaks-- insert newline.
606 target=AcquireString(*caption);
608 CopyMagickString(target,*caption,n+1);
609 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
610 ConcatenateMagickString(target,p,strlen(*caption)+2);
611 (void) DestroyString(*caption);
618 text=DestroyString(text);
620 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
621 if (GetUTFCode(p) == '\n')
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 % G e t M u l t i l i n e T y p e M e t r i c s %
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 % GetMultilineTypeMetrics() returns the following information for the
638 % specified font and text:
646 % maximum horizontal advance
654 % underline thickness
656 % This method is like GetTypeMetrics() but it returns the maximum text width
657 % and height for multiple lines of text.
659 % The format of the GetMultilineTypeMetrics method is:
661 % MagickBooleanType GetMultilineTypeMetrics(Image *image,
662 % const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
664 % A description of each parameter follows:
666 % o image: the image.
668 % o draw_info: the draw info.
670 % o metrics: Return the font metrics in this structure.
672 % o exception: return any errors or warnings in this structure.
675 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
676 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
693 assert(image != (Image *) NULL);
694 assert(image->signature == MagickSignature);
695 if (image->debug != MagickFalse)
696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
697 assert(draw_info != (DrawInfo *) NULL);
698 assert(draw_info->text != (char *) NULL);
699 assert(draw_info->signature == MagickSignature);
700 if (*draw_info->text == '\0')
702 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
703 annotate_info->text=DestroyString(annotate_info->text);
705 Convert newlines to multiple lines of text.
707 textlist=StringToList(draw_info->text);
708 if (textlist == (char **) NULL)
710 annotate_info->render=MagickFalse;
711 annotate_info->direction=UndefinedDirection;
712 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
713 (void) ResetMagickMemory(&extent,0,sizeof(extent));
715 Find the widest of the text lines.
717 annotate_info->text=textlist[0];
718 status=GetTypeMetrics(image,annotate_info,&extent,exception);
720 for (i=1; textlist[i] != (char *) NULL; i++)
722 annotate_info->text=textlist[i];
723 status=GetTypeMetrics(image,annotate_info,&extent,exception);
724 if (extent.width > metrics->width)
727 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
728 (i-1)*draw_info->interline_spacing);
730 Relinquish resources.
732 annotate_info->text=(char *) NULL;
733 annotate_info=DestroyDrawInfo(annotate_info);
734 for (i=0; textlist[i] != (char *) NULL; i++)
735 textlist[i]=DestroyString(textlist[i]);
736 textlist=(char **) RelinquishMagickMemory(textlist);
741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
745 % G e t T y p e M e t r i c s %
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751 % GetTypeMetrics() returns the following information for the specified font
760 % maximum horizontal advance
768 % underline thickness
770 % The format of the GetTypeMetrics method is:
772 % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
773 % TypeMetric *metrics,ExceptionInfo *exception)
775 % A description of each parameter follows:
777 % o image: the image.
779 % o draw_info: the draw info.
781 % o metrics: Return the font metrics in this structure.
783 % o exception: return any errors or warnings in this structure.
786 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
787 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
798 assert(image != (Image *) NULL);
799 assert(image->signature == MagickSignature);
800 if (image->debug != MagickFalse)
801 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
802 assert(draw_info != (DrawInfo *) NULL);
803 assert(draw_info->text != (char *) NULL);
804 assert(draw_info->signature == MagickSignature);
805 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
806 annotate_info->render=MagickFalse;
807 annotate_info->direction=UndefinedDirection;
808 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
811 status=RenderType(image,annotate_info,&offset,metrics,exception);
812 if (image->debug != MagickFalse)
813 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
814 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
815 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
816 "underline position: %g; underline thickness: %g",annotate_info->text,
817 metrics->width,metrics->height,metrics->ascent,metrics->descent,
818 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
819 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
820 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
821 metrics->underline_position,metrics->underline_thickness);
822 annotate_info=DestroyDrawInfo(annotate_info);
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
831 + R e n d e r T y p e %
835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837 % RenderType() renders text on the image. It also returns the bounding box of
838 % the text relative to the image.
840 % The format of the RenderType method is:
842 % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
843 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
845 % A description of each parameter follows:
847 % o image: the image.
849 % o draw_info: the draw info.
851 % o offset: (x,y) location of text relative to image.
853 % o metrics: bounding box of text.
855 % o exception: return any errors or warnings in this structure.
858 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
859 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
870 type_info=(const TypeInfo *) NULL;
871 if (draw_info->font != (char *) NULL)
873 if (*draw_info->font == '@')
875 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
879 if (*draw_info->font == '-')
880 return(RenderX11(image,draw_info,offset,metrics,exception));
881 if (IsPathAccessible(draw_info->font) != MagickFalse)
883 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
887 type_info=GetTypeInfo(draw_info->font,exception);
888 if (type_info == (const TypeInfo *) NULL)
889 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
890 "UnableToReadFont","`%s'",draw_info->font);
892 if ((type_info == (const TypeInfo *) NULL) &&
893 (draw_info->family != (const char *) NULL))
895 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
896 draw_info->stretch,draw_info->weight,exception);
897 if (type_info == (const TypeInfo *) NULL)
898 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
899 "UnableToReadFont","`%s'",draw_info->family);
901 if (type_info == (const TypeInfo *) NULL)
902 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
903 draw_info->stretch,draw_info->weight,exception);
904 if (type_info == (const TypeInfo *) NULL)
905 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
906 draw_info->stretch,draw_info->weight,exception);
907 if (type_info == (const TypeInfo *) NULL)
908 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
909 draw_info->stretch,draw_info->weight,exception);
910 if (type_info == (const TypeInfo *) NULL)
911 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
912 draw_info->stretch,draw_info->weight,exception);
913 if (type_info == (const TypeInfo *) NULL)
914 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
915 draw_info->stretch,draw_info->weight,exception);
916 if (type_info == (const TypeInfo *) NULL)
917 type_info=GetTypeInfo("*",exception);
918 if (type_info == (const TypeInfo *) NULL)
920 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
924 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
925 annotate_info->face=type_info->face;
926 if (type_info->metrics != (char *) NULL)
927 (void) CloneString(&annotate_info->metrics,type_info->metrics);
928 if (type_info->glyphs != (char *) NULL)
929 (void) CloneString(&annotate_info->font,type_info->glyphs);
930 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
932 annotate_info=DestroyDrawInfo(annotate_info);
937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
941 + R e n d e r F r e e t y p e %
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
947 % RenderFreetype() renders text on the image with a Truetype font. It also
948 % returns the bounding box of the text relative to the image.
950 % The format of the RenderFreetype method is:
952 % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
953 % const char *encoding,const PointInfo *offset,TypeMetric *metrics,
954 % ExceptionInfo *exception)
956 % A description of each parameter follows:
958 % o image: the image.
960 % o draw_info: the draw info.
962 % o encoding: the font encoding.
964 % o offset: (x,y) location of text relative to image.
966 % o metrics: bounding box of text.
968 % o exception: return any errors or warnings in this structure.
972 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
974 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
983 affine=draw_info->affine;
984 (void) FormatLocaleString(path,MaxTextExtent,
985 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
986 p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
987 affine.ty-to->y/64.0);
988 (void) ConcatenateString(&draw_info->primitive,path);
992 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1000 affine=draw_info->affine;
1001 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
1002 to->x/64.0,affine.ty-to->y/64.0);
1003 (void) ConcatenateString(&draw_info->primitive,path);
1007 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1013 path[MaxTextExtent];
1015 affine=draw_info->affine;
1016 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
1017 to->x/64.0,affine.ty-to->y/64.0);
1018 (void) ConcatenateString(&draw_info->primitive,path);
1022 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1023 DrawInfo *draw_info)
1029 path[MaxTextExtent];
1031 affine=draw_info->affine;
1032 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
1033 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1034 affine.ty-to->y/64.0);
1035 (void) ConcatenateString(&draw_info->primitive,path);
1039 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1040 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1041 ExceptionInfo *exception)
1043 #if !defined(FT_OPEN_PATHNAME)
1044 #define FT_OPEN_PATHNAME ft_open_pathname
1047 typedef struct _GlyphInfo
1113 static FT_Outline_Funcs
1116 (FT_Outline_MoveTo_Func) TraceMoveTo,
1117 (FT_Outline_LineTo_Func) TraceLineTo,
1118 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1119 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1127 Initialize Truetype library.
1129 status=FT_Init_FreeType(&library);
1131 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1133 args.flags=FT_OPEN_PATHNAME;
1134 if (draw_info->font == (char *) NULL)
1135 args.pathname=ConstantString("helvetica");
1137 if (*draw_info->font != '@')
1138 args.pathname=ConstantString(draw_info->font);
1140 args.pathname=ConstantString(draw_info->font+1);
1141 face=(FT_Face) NULL;
1142 status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
1143 args.pathname=DestroyString(args.pathname);
1146 (void) FT_Done_FreeType(library);
1147 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
1148 "UnableToReadFont","`%s'",draw_info->font);
1149 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1151 if ((draw_info->metrics != (char *) NULL) &&
1152 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1153 (void) FT_Attach_File(face,draw_info->metrics);
1154 encoding_type=ft_encoding_unicode;
1155 status=FT_Select_Charmap(face,encoding_type);
1156 if ((status != 0) && (face->num_charmaps != 0))
1157 status=FT_Set_Charmap(face,face->charmaps[0]);
1158 if (encoding != (const char *) NULL)
1160 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1161 encoding_type=ft_encoding_adobe_custom;
1162 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1163 encoding_type=ft_encoding_adobe_expert;
1164 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1165 encoding_type=ft_encoding_adobe_standard;
1166 if (LocaleCompare(encoding,"AppleRoman") == 0)
1167 encoding_type=ft_encoding_apple_roman;
1168 if (LocaleCompare(encoding,"BIG5") == 0)
1169 encoding_type=ft_encoding_big5;
1170 if (LocaleCompare(encoding,"GB2312") == 0)
1171 encoding_type=ft_encoding_gb2312;
1172 if (LocaleCompare(encoding,"Johab") == 0)
1173 encoding_type=ft_encoding_johab;
1174 #if defined(ft_encoding_latin_1)
1175 if (LocaleCompare(encoding,"Latin-1") == 0)
1176 encoding_type=ft_encoding_latin_1;
1178 if (LocaleCompare(encoding,"Latin-2") == 0)
1179 encoding_type=ft_encoding_latin_2;
1180 if (LocaleCompare(encoding,"None") == 0)
1181 encoding_type=ft_encoding_none;
1182 if (LocaleCompare(encoding,"SJIScode") == 0)
1183 encoding_type=ft_encoding_sjis;
1184 if (LocaleCompare(encoding,"Symbol") == 0)
1185 encoding_type=ft_encoding_symbol;
1186 if (LocaleCompare(encoding,"Unicode") == 0)
1187 encoding_type=ft_encoding_unicode;
1188 if (LocaleCompare(encoding,"Wansung") == 0)
1189 encoding_type=ft_encoding_wansung;
1190 status=FT_Select_Charmap(face,encoding_type);
1192 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1197 resolution.x=DefaultResolution;
1198 resolution.y=DefaultResolution;
1199 if (draw_info->density != (char *) NULL)
1207 flags=ParseGeometry(draw_info->density,&geometry_info);
1208 resolution.x=geometry_info.rho;
1209 resolution.y=geometry_info.sigma;
1210 if ((flags & SigmaValue) == 0)
1211 resolution.y=resolution.x;
1213 status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1214 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1215 (FT_UInt) resolution.y);
1216 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1217 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1218 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1219 metrics->descent=(double) face->size->metrics.descender/64.0;
1221 metrics->origin.x=0;
1222 metrics->origin.y=0;
1223 metrics->height=(double) face->size->metrics.height/64.0;
1224 metrics->max_advance=0.0;
1225 if (face->size->metrics.max_advance > MagickEpsilon)
1226 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1227 metrics->bounds.x1=0.0;
1228 metrics->bounds.y1=metrics->descent;
1229 metrics->bounds.x2=metrics->ascent+metrics->descent;
1230 metrics->bounds.y2=metrics->ascent+metrics->descent;
1231 metrics->underline_position=face->underline_position/64.0;
1232 metrics->underline_thickness=face->underline_thickness/64.0;
1233 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
1235 (void) FT_Done_Face(face);
1236 (void) FT_Done_FreeType(library);
1240 Compute bounding box.
1242 if (image->debug != MagickFalse)
1243 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1244 "font-encoding %s; text-encoding %s; pointsize %g",
1245 draw_info->font != (char *) NULL ? draw_info->font : "none",
1246 encoding != (char *) NULL ? encoding : "none",
1247 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1248 draw_info->pointsize);
1249 flags=FT_LOAD_NO_BITMAP;
1250 if (draw_info->text_antialias == MagickFalse)
1251 flags|=FT_LOAD_TARGET_MONO;
1254 #if defined(FT_LOAD_TARGET_LIGHT)
1255 flags|=FT_LOAD_TARGET_LIGHT;
1256 #elif defined(FT_LOAD_TARGET_LCD)
1257 flags|=FT_LOAD_TARGET_LCD;
1260 value=GetImageProperty(image,"type:hinting",exception);
1261 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1262 flags|=FT_LOAD_NO_HINTING;
1266 last_glyph.image=NULL;
1273 if (draw_info->render != MagickFalse)
1275 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1276 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1277 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1278 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1280 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1281 (void) CloneString(&annotate_info->primitive,"path '");
1282 if (draw_info->render != MagickFalse)
1284 if (image->storage_class != DirectClass)
1285 (void) SetImageStorageClass(image,DirectClass,exception);
1286 if (image->alpha_trait != BlendPixelTrait)
1287 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1290 if (draw_info->direction == RightToLeftDirection)
1294 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1295 if (GetUTFCode(p) < 0)
1297 utf8=(unsigned char *) NULL;
1298 if (GetUTFCode(p) == 0)
1302 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1303 if (utf8 != (unsigned char *) NULL)
1306 for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1309 Render UTF-8 sequence.
1311 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1313 glyph.id=FT_Get_Char_Index(face,'?');
1314 if ((glyph.id != 0) && (last_glyph.id != 0))
1316 if (fabs(draw_info->kerning) >= MagickEpsilon)
1317 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
1319 if (FT_HAS_KERNING(face))
1324 status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1325 ft_kerning_default,&kerning);
1327 origin.x+=(FT_Pos) (direction*kerning.x);
1330 glyph.origin=origin;
1331 status=FT_Load_Glyph(face,glyph.id,flags);
1334 status=FT_Get_Glyph(face->glyph,&glyph.image);
1337 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1341 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
1342 metrics->bounds.x1=(double) bounds.xMin;
1343 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
1344 metrics->bounds.y1=(double) bounds.yMin;
1345 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
1346 metrics->bounds.x2=(double) bounds.xMax;
1347 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
1348 metrics->bounds.y2=(double) bounds.yMax;
1349 if (draw_info->render != MagickFalse)
1350 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1351 (draw_info->stroke_pattern != (Image *) NULL))
1356 annotate_info->affine.tx=glyph.origin.x/64.0;
1357 annotate_info->affine.ty=glyph.origin.y/64.0;
1358 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1359 outline,&OutlineMethods,annotate_info);
1361 FT_Vector_Transform(&glyph.origin,&affine);
1362 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1363 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1364 (FT_Vector *) NULL,MagickTrue);
1367 bitmap=(FT_BitmapGlyph) glyph.image;
1368 point.x=offset->x+bitmap->left;
1369 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1370 point.x=offset->x+(origin.x >> 6);
1371 point.y=offset->y-bitmap->top;
1372 if (draw_info->render != MagickFalse)
1380 register unsigned char
1384 Rasterize the glyph.
1387 image_view=AcquireAuthenticCacheView(image,exception);
1388 p=bitmap->bitmap.buffer;
1389 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1412 if (status == MagickFalse)
1414 x_offset=(ssize_t) ceil(point.x-0.5);
1415 y_offset=(ssize_t) ceil(point.y+y-0.5);
1416 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1419 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1423 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1424 bitmap->bitmap.width,1,exception);
1425 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1427 n=y*bitmap->bitmap.pitch-1;
1428 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
1432 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1434 q+=GetPixelChannels(image);
1437 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
1438 fill_opacity=(double) (p[n])/(bitmap->bitmap.num_grays-1);
1440 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1441 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1442 if (draw_info->text_antialias == MagickFalse)
1443 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1444 if (active == MagickFalse)
1445 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1447 if (q == (Quantum *) NULL)
1449 q+=GetPixelChannels(image);
1452 GetPixelInfo(image,&fill_color);
1453 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1455 fill_opacity=fill_opacity*fill_color.alpha;
1456 CompositePixelOver(image,&fill_color,fill_opacity,q,
1457 GetPixelAlpha(image,q),q);
1458 if (active == MagickFalse)
1460 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1461 if (sync == MagickFalse)
1464 q+=GetPixelChannels(image);
1466 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1467 if (sync == MagickFalse)
1470 image_view=DestroyCacheView(image_view);
1472 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1473 metrics->width=bitmap->left+bitmap->bitmap.width;
1474 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1475 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1476 (IsUTFSpace(code) == MagickFalse))
1477 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
1479 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
1480 metrics->origin.x=(double) origin.x;
1481 metrics->origin.y=(double) origin.y;
1482 if (last_glyph.id != 0)
1483 FT_Done_Glyph(last_glyph.image);
1487 if (utf8 != (unsigned char *) NULL)
1488 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
1489 if (last_glyph.id != 0)
1490 FT_Done_Glyph(last_glyph.image);
1491 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1492 (draw_info->stroke_pattern != (Image *) NULL))
1494 if (draw_info->render != MagickFalse)
1499 annotate_info->linejoin=RoundJoin;
1500 annotate_info->affine.tx=offset->x;
1501 annotate_info->affine.ty=offset->y;
1502 (void) ConcatenateString(&annotate_info->primitive,"'");
1503 (void) DrawImage(image,annotate_info,exception);
1507 Determine font metrics.
1509 glyph.id=FT_Get_Char_Index(face,'_');
1510 glyph.origin=origin;
1511 status=FT_Load_Glyph(face,glyph.id,flags);
1514 status=FT_Get_Glyph(face->glyph,&glyph.image);
1517 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1521 FT_Vector_Transform(&glyph.origin,&affine);
1522 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1523 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1524 (FT_Vector *) NULL,MagickTrue);
1525 bitmap=(FT_BitmapGlyph) glyph.image;
1526 if (bitmap->left > metrics->width)
1527 metrics->width=bitmap->left;
1530 FT_Done_Glyph(glyph.image);
1532 metrics->width-=metrics->bounds.x1/64.0;
1533 metrics->width+=annotate_info->stroke_width;
1534 metrics->bounds.x1/=64.0;
1535 metrics->bounds.y1/=64.0;
1536 metrics->bounds.x2/=64.0;
1537 metrics->bounds.y2/=64.0;
1538 metrics->origin.x/=64.0;
1539 metrics->origin.y/=64.0;
1541 Relinquish resources.
1543 annotate_info=DestroyDrawInfo(annotate_info);
1544 (void) FT_Done_Face(face);
1545 (void) FT_Done_FreeType(library);
1549 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1550 const char *magick_unused(encoding),const PointInfo *offset,
1551 TypeMetric *metrics,ExceptionInfo *exception)
1553 (void) ThrowMagickException(exception,GetMagickModule(),
1554 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
1555 draw_info->font != (char *) NULL ? draw_info->font : "none");
1556 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1565 + R e n d e r P o s t s c r i p t %
1569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1571 % RenderPostscript() renders text on the image with a Postscript font. It
1572 % also returns the bounding box of the text relative to the image.
1574 % The format of the RenderPostscript method is:
1576 % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1577 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1579 % A description of each parameter follows:
1581 % o image: the image.
1583 % o draw_info: the draw info.
1585 % o offset: (x,y) location of text relative to image.
1587 % o metrics: bounding box of text.
1589 % o exception: return any errors or warnings in this structure.
1593 static inline size_t MagickMin(const size_t x,const size_t y)
1600 static char *EscapeParenthesis(const char *text)
1615 buffer=AcquireString(text);
1617 for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
1619 if ((text[i] == '(') || (text[i] == ')'))
1630 static MagickBooleanType RenderPostscript(Image *image,
1631 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1632 ExceptionInfo *exception)
1635 filename[MaxTextExtent],
1636 geometry[MaxTextExtent],
1669 Render label with a Postscript font.
1671 if (image->debug != MagickFalse)
1672 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1673 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1674 draw_info->font : "none",draw_info->pointsize);
1676 unique_file=AcquireUniqueFileResource(filename);
1677 if (unique_file != -1)
1678 file=fdopen(unique_file,"wb");
1679 if ((unique_file == -1) || (file == (FILE *) NULL))
1681 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
1682 return(MagickFalse);
1684 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1685 (void) FormatLocaleFile(file,"/ReencodeType\n");
1686 (void) FormatLocaleFile(file,"{\n");
1687 (void) FormatLocaleFile(file," findfont dup length\n");
1688 (void) FormatLocaleFile(file,
1689 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
1690 (void) FormatLocaleFile(file,
1691 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
1692 (void) FormatLocaleFile(file,"} bind def\n");
1694 Sample to compute bounding box.
1696 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1697 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1698 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
1701 length=strlen(draw_info->text);
1702 for (i=0; i <= (ssize_t) (length+2); i++)
1704 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1705 draw_info->affine.ry*2.0*draw_info->pointsize);
1706 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1707 draw_info->affine.sy*2.0*draw_info->pointsize);
1708 if (point.x > extent.x)
1710 if (point.y > extent.y)
1713 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
1714 extent.x/2.0,extent.y/2.0);
1715 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
1716 draw_info->pointsize);
1717 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1718 (strchr(draw_info->font,'/') != (char *) NULL))
1719 (void) FormatLocaleFile(file,
1720 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1722 (void) FormatLocaleFile(file,
1723 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1725 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
1726 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1727 draw_info->affine.sy);
1728 text=EscapeParenthesis(draw_info->text);
1729 if (identity == MagickFalse)
1730 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1732 (void) FormatLocaleFile(file,"(%s) show\n",text);
1733 text=DestroyString(text);
1734 (void) FormatLocaleFile(file,"showpage\n");
1735 (void) fclose(file);
1736 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
1737 floor(extent.x+0.5),floor(extent.y+0.5));
1738 annotate_info=AcquireImageInfo();
1739 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
1741 (void) CloneString(&annotate_info->page,geometry);
1742 if (draw_info->density != (char *) NULL)
1743 (void) CloneString(&annotate_info->density,draw_info->density);
1744 annotate_info->antialias=draw_info->text_antialias;
1745 annotate_image=ReadImage(annotate_info,exception);
1746 CatchException(exception);
1747 annotate_info=DestroyImageInfo(annotate_info);
1748 (void) RelinquishUniqueFileResource(filename);
1749 if (annotate_image == (Image *) NULL)
1750 return(MagickFalse);
1751 (void) NegateImage(annotate_image,MagickFalse,exception);
1752 resolution.x=DefaultResolution;
1753 resolution.y=DefaultResolution;
1754 if (draw_info->density != (char *) NULL)
1762 flags=ParseGeometry(draw_info->density,&geometry_info);
1763 resolution.x=geometry_info.rho;
1764 resolution.y=geometry_info.sigma;
1765 if ((flags & SigmaValue) == 0)
1766 resolution.y=resolution.x;
1768 if (identity == MagickFalse)
1769 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
1775 crop_info=GetImageBoundingBox(annotate_image,exception);
1776 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
1777 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
1778 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
1780 (void) FormatLocaleString(geometry,MaxTextExtent,
1781 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
1782 crop_info.height,(double) crop_info.x,(double) crop_info.y);
1783 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
1785 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1786 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1787 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1788 metrics->ascent=metrics->pixels_per_em.x;
1789 metrics->descent=metrics->pixels_per_em.y/-5.0;
1790 metrics->width=(double) annotate_image->columns/
1791 ExpandAffine(&draw_info->affine);
1792 metrics->height=1.152*metrics->pixels_per_em.x;
1793 metrics->max_advance=metrics->pixels_per_em.x;
1794 metrics->bounds.x1=0.0;
1795 metrics->bounds.y1=metrics->descent;
1796 metrics->bounds.x2=metrics->ascent+metrics->descent;
1797 metrics->bounds.y2=metrics->ascent+metrics->descent;
1798 metrics->underline_position=(-2.0);
1799 metrics->underline_thickness=1.0;
1800 if (draw_info->render == MagickFalse)
1802 annotate_image=DestroyImage(annotate_image);
1805 if (draw_info->fill.alpha != TransparentAlpha)
1819 if (image->alpha_trait != BlendPixelTrait)
1820 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1821 if (annotate_image->alpha_trait != BlendPixelTrait)
1822 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1824 fill_color=draw_info->fill;
1825 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
1826 for (y=0; y < (ssize_t) annotate_image->rows; y++)
1834 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1836 if (q == (Quantum *) NULL)
1838 for (x=0; x < (ssize_t) annotate_image->columns; x++)
1840 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
1841 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1842 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
1843 SetPixelRed(annotate_image,fill_color.red,q);
1844 SetPixelGreen(annotate_image,fill_color.green,q);
1845 SetPixelBlue(annotate_image,fill_color.blue,q);
1846 q+=GetPixelChannels(annotate_image);
1848 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1849 if (sync == MagickFalse)
1852 annotate_view=DestroyCacheView(annotate_view);
1853 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
1854 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
1855 metrics->descent)-0.5),exception);
1857 annotate_image=DestroyImage(annotate_image);
1862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1866 + R e n d e r X 1 1 %
1870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1872 % RenderX11() renders text on the image with an X11 font. It also returns the
1873 % bounding box of the text relative to the image.
1875 % The format of the RenderX11 method is:
1877 % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
1878 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1880 % A description of each parameter follows:
1882 % o image: the image.
1884 % o draw_info: the draw info.
1886 % o offset: (x,y) location of text relative to image.
1888 % o metrics: bounding box of text.
1890 % o exception: return any errors or warnings in this structure.
1893 #if defined(MAGICKCORE_X11_DELEGATE)
1894 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1895 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1904 *display = (Display *) NULL;
1906 static XAnnotateInfo
1915 static XResourceInfo
1921 static XStandardColormap
1931 if (annotate_semaphore == (SemaphoreInfo *) NULL)
1932 AcquireSemaphoreInfo(&annotate_semaphore);
1933 LockSemaphoreInfo(annotate_semaphore);
1934 if (display == (Display *) NULL)
1943 Open X server connection.
1945 display=XOpenDisplay(draw_info->server_name);
1946 if (display == (Display *) NULL)
1948 UnlockSemaphoreInfo(annotate_semaphore);
1949 ThrowXWindowException(XServerError,"UnableToOpenXServer",
1950 draw_info->server_name);
1951 return(MagickFalse);
1954 Get user defaults from X resource database.
1956 (void) XSetErrorHandler(XError);
1957 image_info=AcquireImageInfo();
1958 client_name=GetClientName();
1959 resource_database=XGetResourceDatabase(display,client_name);
1960 XGetResourceInfo(image_info,resource_database,client_name,
1962 resource_info.close_server=MagickFalse;
1963 resource_info.colormap=PrivateColormap;
1964 resource_info.font=AcquireString(draw_info->font);
1965 resource_info.background_color=AcquireString("#ffffffffffff");
1966 resource_info.foreground_color=AcquireString("#000000000000");
1967 map_info=XAllocStandardColormap();
1968 if (map_info == (XStandardColormap *) NULL)
1970 UnlockSemaphoreInfo(annotate_semaphore);
1971 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
1973 return(MagickFalse);
1976 Initialize visual info.
1978 visual_info=XBestVisualInfo(display,map_info,&resource_info);
1979 if (visual_info == (XVisualInfo *) NULL)
1981 UnlockSemaphoreInfo(annotate_semaphore);
1982 ThrowXWindowException(XServerError,"UnableToGetVisual",
1984 return(MagickFalse);
1986 map_info->colormap=(Colormap) NULL;
1987 pixel.pixels=(unsigned long *) NULL;
1989 Initialize Standard Colormap info.
1991 XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen),
1993 XGetPixelInfo(display,visual_info,map_info,&resource_info,(Image *) NULL,
1995 pixel.annotate_context=XDefaultGC(display,visual_info->screen);
1997 Initialize font info.
1999 font_info=XBestFont(display,&resource_info,MagickFalse);
2000 if (font_info == (XFontStruct *) NULL)
2002 UnlockSemaphoreInfo(annotate_semaphore);
2003 ThrowXWindowException(XServerError,"UnableToLoadFont",
2005 return(MagickFalse);
2007 if ((map_info == (XStandardColormap *) NULL) ||
2008 (visual_info == (XVisualInfo *) NULL) ||
2009 (font_info == (XFontStruct *) NULL))
2011 UnlockSemaphoreInfo(annotate_semaphore);
2012 XFreeResources(display,visual_info,map_info,&pixel,font_info,
2013 &resource_info,(XWindowInfo *) NULL);
2014 ThrowXWindowException(XServerError,"UnableToLoadFont",
2016 return(MagickFalse);
2018 cache_info=(*draw_info);
2020 UnlockSemaphoreInfo(annotate_semaphore);
2022 Initialize annotate info.
2024 XGetAnnotateInfo(&annotate_info);
2025 annotate_info.stencil=ForegroundStencil;
2026 if (cache_info.font != draw_info->font)
2029 Type name has changed.
2031 (void) XFreeFont(display,font_info);
2032 (void) CloneString(&resource_info.font,draw_info->font);
2033 font_info=XBestFont(display,&resource_info,MagickFalse);
2034 if (font_info == (XFontStruct *) NULL)
2036 ThrowXWindowException(XServerError,"UnableToLoadFont",
2038 return(MagickFalse);
2041 if (image->debug != MagickFalse)
2042 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2043 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
2044 draw_info->font : "none",draw_info->pointsize);
2045 cache_info=(*draw_info);
2046 annotate_info.font_info=font_info;
2047 annotate_info.text=(char *) draw_info->text;
2048 annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text,
2049 (int) strlen(draw_info->text));
2050 annotate_info.height=(unsigned int) font_info->ascent+font_info->descent;
2051 metrics->pixels_per_em.x=(double) font_info->max_bounds.width;
2052 metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent;
2053 metrics->ascent=(double) font_info->ascent+4;
2054 metrics->descent=(double) (-font_info->descent);
2055 metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine);
2056 metrics->height=font_info->ascent+font_info->descent;
2057 metrics->max_advance=(double) font_info->max_bounds.width;
2058 metrics->bounds.x1=0.0;
2059 metrics->bounds.y1=metrics->descent;
2060 metrics->bounds.x2=metrics->ascent+metrics->descent;
2061 metrics->bounds.y2=metrics->ascent+metrics->descent;
2062 metrics->underline_position=(-2.0);
2063 metrics->underline_thickness=1.0;
2064 if (draw_info->render == MagickFalse)
2066 if (draw_info->fill.alpha == TransparentAlpha)
2071 width=annotate_info.width;
2072 height=annotate_info.height;
2073 if ((fabs(draw_info->affine.rx) >= MagickEpsilon) ||
2074 (fabs(draw_info->affine.ry) >= MagickEpsilon))
2076 if ((fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2077 (fabs(draw_info->affine.rx+draw_info->affine.ry) < MagickEpsilon))
2078 annotate_info.degrees=(double) (180.0/MagickPI)*
2079 atan2(draw_info->affine.rx,draw_info->affine.sx);
2081 (void) FormatLocaleString(annotate_info.geometry,MaxTextExtent,
2082 "%.20gx%.20g%+.20g%+.20g",(double) width,(double) height,
2083 ceil(offset->x-0.5),ceil(offset->y-metrics->ascent-metrics->descent+
2084 draw_info->interline_spacing-0.5));
2085 pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red);
2086 pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green);
2087 pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue);
2088 status=XAnnotateImage(display,&pixel,&annotate_info,image,exception);
2091 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
2093 return(MagickFalse);
2098 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
2099 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2104 (void) ThrowMagickException(exception,GetMagickModule(),
2105 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","'%s' (X11)",
2107 return(MagickFalse);