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-2012 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__)
85 #if defined(MAGICKCORE_HAVE_FT2BUILD_H)
86 # include <ft2build.h>
88 #if defined(FT_FREETYPE_H)
89 # include FT_FREETYPE_H
91 # include <freetype/freetype.h>
93 #if defined(FT_GLYPH_H)
96 # include <freetype/ftglyph.h>
98 #if defined(FT_OUTLINE_H)
99 # include FT_OUTLINE_H
101 # include <freetype/ftoutln.h>
103 #if defined(FT_BBOX_H)
106 # include <freetype/ftbbox.h>
107 #endif /* defined(FT_BBOX_H) */
114 *annotate_semaphore = (SemaphoreInfo *) NULL;
117 Forward declarations.
119 static MagickBooleanType
120 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
122 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
124 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
125 TypeMetric *,ExceptionInfo *),
126 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 + A n n o t a t e C o m p o n e n t G e n e s i s %
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 % AnnotateComponentGenesis() instantiates the annotate component.
142 % The format of the AnnotateComponentGenesis method is:
144 % MagickBooleanType AnnotateComponentGenesis(void)
147 MagickPrivate MagickBooleanType AnnotateComponentGenesis(void)
149 AcquireSemaphoreInfo(&annotate_semaphore);
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 + A n n o t a t e C o m p o n e n t T e r m i n u s %
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 % AnnotateComponentTerminus() destroys the annotate component.
166 % The format of the AnnotateComponentTerminus method is:
168 % AnnotateComponentTerminus(void)
171 MagickPrivate void AnnotateComponentTerminus(void)
173 if (annotate_semaphore == (SemaphoreInfo *) NULL)
174 AcquireSemaphoreInfo(&annotate_semaphore);
175 DestroySemaphoreInfo(&annotate_semaphore);
179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 % A n n o t a t e I m a g e %
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 % AnnotateImage() annotates an image with text. Optionally you can include
190 % any of the following bits of information about the image by embedding
191 % the appropriate special characters:
193 % %b file size in bytes.
195 % %d directory in which the image resides.
196 % %e extension of the image file.
197 % %f original filename of the image.
198 % %h height of image.
199 % %i filename of the image.
200 % %k number of unique colors.
202 % %m image file format.
203 % %n number of images in a image sequence.
204 % %o output image filename.
205 % %p page number of the image.
206 % %q image depth (8 or 16).
207 % %q image depth (8 or 16).
208 % %s image scene number.
209 % %t image filename without any extension.
210 % %u a unique temporary filename.
212 % %x x resolution of the image.
213 % %y y resolution of the image.
215 % The format of the AnnotateImage method is:
217 % MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
218 % ExceptionInfo *exception)
220 % A description of each parameter follows:
222 % o image: the image.
224 % o draw_info: the draw info.
226 % o exception: return any errors or warnings in this structure.
229 MagickExport MagickBooleanType AnnotateImage(Image *image,
230 const DrawInfo *draw_info,ExceptionInfo *exception)
233 primitive[MaxTextExtent],
265 assert(image != (Image *) NULL);
266 assert(image->signature == MagickSignature);
267 if (image->debug != MagickFalse)
268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
269 assert(draw_info != (DrawInfo *) NULL);
270 assert(draw_info->signature == MagickSignature);
271 if (draw_info->text == (char *) NULL)
273 if (*draw_info->text == '\0')
275 textlist=StringToList(draw_info->text);
276 if (textlist == (char **) NULL)
278 length=strlen(textlist[0]);
279 for (i=1; textlist[i] != (char *) NULL; i++)
280 if (strlen(textlist[i]) > length)
281 length=strlen(textlist[i]);
282 number_lines=(size_t) i;
283 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
284 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
285 SetGeometry(image,&geometry);
286 SetGeometryInfo(&geometry_info);
287 if (annotate_info->geometry != (char *) NULL)
289 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
291 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
293 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
295 if (IsGrayColorspace(image->colorspace) != MagickFalse)
296 (void) TransformImageColorspace(image,RGBColorspace,exception);
298 for (i=0; textlist[i] != (char *) NULL; i++)
301 Position text relative to image.
303 annotate_info->affine.tx=geometry_info.xi-image->page.x;
304 annotate_info->affine.ty=geometry_info.psi-image->page.y;
305 (void) CloneString(&annotate->text,textlist[i]);
306 (void) GetTypeMetrics(image,annotate,&metrics,exception);
307 height=(ssize_t) (metrics.ascent-metrics.descent+
308 draw_info->interline_spacing+0.5);
309 switch (annotate->gravity)
311 case UndefinedGravity:
314 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
315 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
318 case NorthWestGravity:
320 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
321 annotate_info->affine.ry*height+annotate_info->affine.ry*
322 (metrics.ascent+metrics.descent);
323 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
324 annotate_info->affine.sy*height+annotate_info->affine.sy*
330 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
331 geometry.width/2.0+i*annotate_info->affine.ry*height-
332 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
333 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
334 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
335 annotate_info->affine.sy*height+annotate_info->affine.sy*
336 metrics.ascent-annotate_info->affine.rx*(metrics.width-
337 metrics.bounds.x1)/2.0;
340 case NorthEastGravity:
342 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
343 geometry.width+i*annotate_info->affine.ry*height-
344 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
345 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
346 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
347 annotate_info->affine.sy*height+annotate_info->affine.sy*
348 metrics.ascent-annotate_info->affine.rx*(metrics.width-
354 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
355 annotate_info->affine.ry*height+annotate_info->affine.ry*
356 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
357 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
358 geometry.height/2.0+i*annotate_info->affine.sy*height+
359 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
360 (number_lines-1.0)*height)/2.0;
365 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
366 geometry.width/2.0+i*annotate_info->affine.ry*height-
367 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
368 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
369 (number_lines-1)*height)/2.0;
370 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
371 geometry.height/2.0+i*annotate_info->affine.sy*height-
372 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
373 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
374 (number_lines-1.0)*height)/2.0;
379 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
380 geometry.width+i*annotate_info->affine.ry*height-
381 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
382 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
383 (number_lines-1.0)*height)/2.0-1.0;
384 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
385 geometry.height/2.0+i*annotate_info->affine.sy*height-
386 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+
387 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
388 (number_lines-1.0)*height)/2.0;
391 case SouthWestGravity:
393 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
394 annotate_info->affine.ry*height-annotate_info->affine.ry*
395 (number_lines-1.0)*height;
396 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
397 geometry.height+i*annotate_info->affine.sy*height-
398 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
403 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
404 geometry.width/2.0+i*annotate_info->affine.ry*height-
405 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
406 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
407 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
408 geometry.height+i*annotate_info->affine.sy*height-
409 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
410 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
413 case SouthEastGravity:
415 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
416 geometry.width+i*annotate_info->affine.ry*height-
417 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
418 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
419 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
420 geometry.height+i*annotate_info->affine.sy*height-
421 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
422 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
426 switch (annotate->align)
430 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
431 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
436 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
437 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
438 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
439 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
444 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
445 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
446 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
447 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
453 if (draw_info->undercolor.alpha != TransparentAlpha)
461 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
462 undercolor_info->fill=draw_info->undercolor;
463 undercolor_info->affine=draw_info->affine;
464 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
465 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
466 (void) FormatLocaleString(primitive,MaxTextExtent,
467 "rectangle 0,0 %g,%.20g",metrics.origin.x,(double) height);
468 (void) CloneString(&undercolor_info->primitive,primitive);
469 (void) DrawImage(image,undercolor_info,exception);
470 (void) DestroyDrawInfo(undercolor_info);
472 annotate_info->affine.tx=offset.x;
473 annotate_info->affine.ty=offset.y;
474 (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
475 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
476 if (annotate->decorate == OverlineDecoration)
478 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
479 metrics.descent-metrics.underline_position));
480 (void) CloneString(&annotate_info->primitive,primitive);
481 (void) DrawImage(image,annotate_info,exception);
484 if (annotate->decorate == UnderlineDecoration)
486 annotate_info->affine.ty-=(draw_info->affine.sy*
487 metrics.underline_position);
488 (void) CloneString(&annotate_info->primitive,primitive);
489 (void) DrawImage(image,annotate_info,exception);
492 Annotate image with text.
494 status=RenderType(image,annotate,&offset,&metrics,exception);
495 if (status == MagickFalse)
497 if (annotate->decorate == LineThroughDecoration)
499 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
500 metrics.underline_position+metrics.descent)/2.0);
501 (void) CloneString(&annotate_info->primitive,primitive);
502 (void) DrawImage(image,annotate_info,exception);
506 Relinquish resources.
508 annotate_info=DestroyDrawInfo(annotate_info);
509 annotate=DestroyDrawInfo(annotate);
510 for (i=0; textlist[i] != (char *) NULL; i++)
511 textlist[i]=DestroyString(textlist[i]);
512 textlist=(char **) RelinquishMagickMemory(textlist);
517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521 % F o r m a t M a g i c k C a p t i o n %
525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
527 % FormatMagickCaption() formats a caption so that it fits within the image
528 % width. It returns the number of lines in the formatted caption.
530 % The format of the FormatMagickCaption method is:
532 % ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
533 % const MagickBooleanType split,TypeMetric *metrics,char **caption,
534 % ExceptionInfo *exception)
536 % A description of each parameter follows.
538 % o image: The image.
540 % o draw_info: the draw info.
542 % o split: when no convenient line breaks-- insert newline.
544 % o metrics: Return the font metrics in this structure.
546 % o caption: the caption.
548 % o exception: return any errors or warnings in this structure.
551 MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
552 const MagickBooleanType split,TypeMetric *metrics,char **caption,
553 ExceptionInfo *exception)
575 text=AcquireString(draw_info->text);
578 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
580 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
582 if (GetUTFCode(p) == '\n')
584 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
587 status=GetTypeMetrics(image,draw_info,metrics,exception);
588 if (status == MagickFalse)
590 width=(size_t) floor(metrics->width+metrics->max_advance/2.0+0.5);
591 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
593 (void) strcpy(text,draw_info->text);
594 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
600 if ((s != (char *) NULL) || (split != MagickFalse))
606 No convenient line breaks-- insert newline.
608 target=AcquireString(*caption);
610 CopyMagickString(target,*caption,n+1);
611 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
612 ConcatenateMagickString(target,p,strlen(*caption)+2);
613 (void) DestroyString(*caption);
620 text=DestroyString(text);
622 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
623 if (GetUTFCode(p) == '\n')
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 % G e t M u l t i l i n e T y p e M e t r i c s %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % GetMultilineTypeMetrics() returns the following information for the
640 % specified font and text:
648 % maximum horizontal advance
656 % underline thickness
658 % This method is like GetTypeMetrics() but it returns the maximum text width
659 % and height for multiple lines of text.
661 % The format of the GetMultilineTypeMetrics method is:
663 % MagickBooleanType GetMultilineTypeMetrics(Image *image,
664 % const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
666 % A description of each parameter follows:
668 % o image: the image.
670 % o draw_info: the draw info.
672 % o metrics: Return the font metrics in this structure.
674 % o exception: return any errors or warnings in this structure.
677 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
678 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
695 assert(image != (Image *) NULL);
696 assert(image->signature == MagickSignature);
697 if (image->debug != MagickFalse)
698 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
699 assert(draw_info != (DrawInfo *) NULL);
700 assert(draw_info->text != (char *) NULL);
701 assert(draw_info->signature == MagickSignature);
702 if (*draw_info->text == '\0')
704 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
705 annotate_info->text=DestroyString(annotate_info->text);
707 Convert newlines to multiple lines of text.
709 textlist=StringToList(draw_info->text);
710 if (textlist == (char **) NULL)
712 annotate_info->render=MagickFalse;
713 annotate_info->direction=UndefinedDirection;
714 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
715 (void) ResetMagickMemory(&extent,0,sizeof(extent));
717 Find the widest of the text lines.
719 annotate_info->text=textlist[0];
720 status=GetTypeMetrics(image,annotate_info,&extent,exception);
722 for (i=1; textlist[i] != (char *) NULL; i++)
724 annotate_info->text=textlist[i];
725 status=GetTypeMetrics(image,annotate_info,&extent,exception);
726 if (extent.width > metrics->width)
729 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
730 (i-1)*draw_info->interline_spacing);
732 Relinquish resources.
734 annotate_info->text=(char *) NULL;
735 annotate_info=DestroyDrawInfo(annotate_info);
736 for (i=0; textlist[i] != (char *) NULL; i++)
737 textlist[i]=DestroyString(textlist[i]);
738 textlist=(char **) RelinquishMagickMemory(textlist);
743 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
747 % G e t T y p e M e t r i c s %
751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 % GetTypeMetrics() returns the following information for the specified font
762 % maximum horizontal advance
770 % underline thickness
772 % The format of the GetTypeMetrics method is:
774 % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
775 % TypeMetric *metrics,ExceptionInfo *exception)
777 % A description of each parameter follows:
779 % o image: the image.
781 % o draw_info: the draw info.
783 % o metrics: Return the font metrics in this structure.
785 % o exception: return any errors or warnings in this structure.
788 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
789 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
800 assert(image != (Image *) NULL);
801 assert(image->signature == MagickSignature);
802 if (image->debug != MagickFalse)
803 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
804 assert(draw_info != (DrawInfo *) NULL);
805 assert(draw_info->text != (char *) NULL);
806 assert(draw_info->signature == MagickSignature);
807 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
808 annotate_info->render=MagickFalse;
809 annotate_info->direction=UndefinedDirection;
810 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
813 status=RenderType(image,annotate_info,&offset,metrics,exception);
814 if (image->debug != MagickFalse)
815 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
816 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
817 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
818 "underline position: %g; underline thickness: %g",annotate_info->text,
819 metrics->width,metrics->height,metrics->ascent,metrics->descent,
820 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
821 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
822 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
823 metrics->underline_position,metrics->underline_thickness);
824 annotate_info=DestroyDrawInfo(annotate_info);
829 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
833 + R e n d e r T y p e %
837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
839 % RenderType() renders text on the image. It also returns the bounding box of
840 % the text relative to the image.
842 % The format of the RenderType method is:
844 % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
845 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
847 % A description of each parameter follows:
849 % o image: the image.
851 % o draw_info: the draw info.
853 % o offset: (x,y) location of text relative to image.
855 % o metrics: bounding box of text.
857 % o exception: return any errors or warnings in this structure.
860 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
861 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
872 type_info=(const TypeInfo *) NULL;
873 if (draw_info->font != (char *) NULL)
875 if (*draw_info->font == '@')
877 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
881 if (*draw_info->font == '-')
882 return(RenderX11(image,draw_info,offset,metrics,exception));
883 if (IsPathAccessible(draw_info->font) != MagickFalse)
885 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
889 type_info=GetTypeInfo(draw_info->font,exception);
890 if (type_info == (const TypeInfo *) NULL)
891 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
892 "UnableToReadFont","'%s'",draw_info->font);
894 if ((type_info == (const TypeInfo *) NULL) &&
895 (draw_info->family != (const char *) NULL))
897 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
898 draw_info->stretch,draw_info->weight,exception);
899 if (type_info == (const TypeInfo *) NULL)
900 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
901 "UnableToReadFont","'%s'",draw_info->family);
903 if (type_info == (const TypeInfo *) NULL)
904 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
905 draw_info->stretch,draw_info->weight,exception);
906 if (type_info == (const TypeInfo *) NULL)
907 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
908 draw_info->stretch,draw_info->weight,exception);
909 if (type_info == (const TypeInfo *) NULL)
910 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
911 draw_info->stretch,draw_info->weight,exception);
912 if (type_info == (const TypeInfo *) NULL)
913 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
914 draw_info->stretch,draw_info->weight,exception);
915 if (type_info == (const TypeInfo *) NULL)
916 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
917 draw_info->stretch,draw_info->weight,exception);
918 if (type_info == (const TypeInfo *) NULL)
919 type_info=GetTypeInfo("*",exception);
920 if (type_info == (const TypeInfo *) NULL)
922 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
926 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
927 annotate_info->face=type_info->face;
928 if (type_info->metrics != (char *) NULL)
929 (void) CloneString(&annotate_info->metrics,type_info->metrics);
930 if (type_info->glyphs != (char *) NULL)
931 (void) CloneString(&annotate_info->font,type_info->glyphs);
932 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
934 annotate_info=DestroyDrawInfo(annotate_info);
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
943 + R e n d e r F r e e t y p e %
947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 % RenderFreetype() renders text on the image with a Truetype font. It also
950 % returns the bounding box of the text relative to the image.
952 % The format of the RenderFreetype method is:
954 % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
955 % const char *encoding,const PointInfo *offset,TypeMetric *metrics,
956 % ExceptionInfo *exception)
958 % A description of each parameter follows:
960 % o image: the image.
962 % o draw_info: the draw info.
964 % o encoding: the font encoding.
966 % o offset: (x,y) location of text relative to image.
968 % o metrics: bounding box of text.
970 % o exception: return any errors or warnings in this structure.
974 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
976 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
985 affine=draw_info->affine;
986 (void) FormatLocaleString(path,MaxTextExtent,
987 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
988 p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
989 affine.ty-to->y/64.0);
990 (void) ConcatenateString(&draw_info->primitive,path);
994 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1000 path[MaxTextExtent];
1002 affine=draw_info->affine;
1003 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
1004 to->x/64.0,affine.ty-to->y/64.0);
1005 (void) ConcatenateString(&draw_info->primitive,path);
1009 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1015 path[MaxTextExtent];
1017 affine=draw_info->affine;
1018 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
1019 to->x/64.0,affine.ty-to->y/64.0);
1020 (void) ConcatenateString(&draw_info->primitive,path);
1024 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1025 DrawInfo *draw_info)
1031 path[MaxTextExtent];
1033 affine=draw_info->affine;
1034 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
1035 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1036 affine.ty-to->y/64.0);
1037 (void) ConcatenateString(&draw_info->primitive,path);
1041 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1042 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1043 ExceptionInfo *exception)
1045 #if !defined(FT_OPEN_PATHNAME)
1046 #define FT_OPEN_PATHNAME ft_open_pathname
1049 typedef struct _GlyphInfo
1115 static FT_Outline_Funcs
1118 (FT_Outline_MoveTo_Func) TraceMoveTo,
1119 (FT_Outline_LineTo_Func) TraceLineTo,
1120 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1121 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1129 Initialize Truetype library.
1131 status=FT_Init_FreeType(&library);
1133 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1135 args.flags=FT_OPEN_PATHNAME;
1136 if (draw_info->font == (char *) NULL)
1137 args.pathname=ConstantString("helvetica");
1139 if (*draw_info->font != '@')
1140 args.pathname=ConstantString(draw_info->font);
1142 args.pathname=ConstantString(draw_info->font+1);
1143 face=(FT_Face) NULL;
1144 status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
1145 args.pathname=DestroyString(args.pathname);
1148 (void) FT_Done_FreeType(library);
1149 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
1150 "UnableToReadFont","'%s'",draw_info->font);
1151 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1153 if ((draw_info->metrics != (char *) NULL) &&
1154 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1155 (void) FT_Attach_File(face,draw_info->metrics);
1156 encoding_type=ft_encoding_unicode;
1157 status=FT_Select_Charmap(face,encoding_type);
1158 if ((status != 0) && (face->num_charmaps != 0))
1159 status=FT_Set_Charmap(face,face->charmaps[0]);
1160 if (encoding != (const char *) NULL)
1162 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1163 encoding_type=ft_encoding_adobe_custom;
1164 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1165 encoding_type=ft_encoding_adobe_expert;
1166 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1167 encoding_type=ft_encoding_adobe_standard;
1168 if (LocaleCompare(encoding,"AppleRoman") == 0)
1169 encoding_type=ft_encoding_apple_roman;
1170 if (LocaleCompare(encoding,"BIG5") == 0)
1171 encoding_type=ft_encoding_big5;
1172 if (LocaleCompare(encoding,"GB2312") == 0)
1173 encoding_type=ft_encoding_gb2312;
1174 if (LocaleCompare(encoding,"Johab") == 0)
1175 encoding_type=ft_encoding_johab;
1176 #if defined(ft_encoding_latin_1)
1177 if (LocaleCompare(encoding,"Latin-1") == 0)
1178 encoding_type=ft_encoding_latin_1;
1180 if (LocaleCompare(encoding,"Latin-2") == 0)
1181 encoding_type=ft_encoding_latin_2;
1182 if (LocaleCompare(encoding,"None") == 0)
1183 encoding_type=ft_encoding_none;
1184 if (LocaleCompare(encoding,"SJIScode") == 0)
1185 encoding_type=ft_encoding_sjis;
1186 if (LocaleCompare(encoding,"Symbol") == 0)
1187 encoding_type=ft_encoding_symbol;
1188 if (LocaleCompare(encoding,"Unicode") == 0)
1189 encoding_type=ft_encoding_unicode;
1190 if (LocaleCompare(encoding,"Wansung") == 0)
1191 encoding_type=ft_encoding_wansung;
1192 status=FT_Select_Charmap(face,encoding_type);
1194 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1199 resolution.x=DefaultResolution;
1200 resolution.y=DefaultResolution;
1201 if (draw_info->density != (char *) NULL)
1209 flags=ParseGeometry(draw_info->density,&geometry_info);
1210 resolution.x=geometry_info.rho;
1211 resolution.y=geometry_info.sigma;
1212 if ((flags & SigmaValue) == 0)
1213 resolution.y=resolution.x;
1215 status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1216 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1217 (FT_UInt) resolution.y);
1218 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1219 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1220 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1221 metrics->descent=(double) face->size->metrics.descender/64.0;
1223 metrics->origin.x=0;
1224 metrics->origin.y=0;
1225 metrics->height=(double) face->size->metrics.height/64.0;
1226 metrics->max_advance=0.0;
1227 if (face->size->metrics.max_advance > MagickEpsilon)
1228 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1229 metrics->bounds.x1=0.0;
1230 metrics->bounds.y1=metrics->descent;
1231 metrics->bounds.x2=metrics->ascent+metrics->descent;
1232 metrics->bounds.y2=metrics->ascent+metrics->descent;
1233 metrics->underline_position=face->underline_position/64.0;
1234 metrics->underline_thickness=face->underline_thickness/64.0;
1235 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
1237 (void) FT_Done_Face(face);
1238 (void) FT_Done_FreeType(library);
1242 Compute bounding box.
1244 if (image->debug != MagickFalse)
1245 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1246 "font-encoding %s; text-encoding %s; pointsize %g",
1247 draw_info->font != (char *) NULL ? draw_info->font : "none",
1248 encoding != (char *) NULL ? encoding : "none",
1249 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1250 draw_info->pointsize);
1251 flags=FT_LOAD_NO_BITMAP;
1252 if (draw_info->text_antialias == MagickFalse)
1253 flags|=FT_LOAD_TARGET_MONO;
1256 #if defined(FT_LOAD_TARGET_LIGHT)
1257 flags|=FT_LOAD_TARGET_LIGHT;
1258 #elif defined(FT_LOAD_TARGET_LCD)
1259 flags|=FT_LOAD_TARGET_LCD;
1262 value=GetImageProperty(image,"type:hinting",exception);
1263 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1264 flags|=FT_LOAD_NO_HINTING;
1268 last_glyph.image=NULL;
1275 if (draw_info->render != MagickFalse)
1277 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1278 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1279 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1280 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1282 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1283 (void) CloneString(&annotate_info->primitive,"path '");
1284 if (draw_info->render != MagickFalse)
1286 if (image->storage_class != DirectClass)
1287 (void) SetImageStorageClass(image,DirectClass,exception);
1288 if (image->alpha_trait != BlendPixelTrait)
1289 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1292 if (draw_info->direction == RightToLeftDirection)
1296 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1297 if (GetUTFCode(p) < 0)
1299 utf8=(unsigned char *) NULL;
1300 if (GetUTFCode(p) == 0)
1304 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1305 if (utf8 != (unsigned char *) NULL)
1308 for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1311 Render UTF-8 sequence.
1313 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1315 glyph.id=FT_Get_Char_Index(face,'?');
1316 if ((glyph.id != 0) && (last_glyph.id != 0))
1318 if (fabs(draw_info->kerning) >= MagickEpsilon)
1319 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
1321 if (FT_HAS_KERNING(face))
1326 status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1327 ft_kerning_default,&kerning);
1329 origin.x+=(FT_Pos) (direction*kerning.x);
1332 glyph.origin=origin;
1333 status=FT_Load_Glyph(face,glyph.id,flags);
1336 status=FT_Get_Glyph(face->glyph,&glyph.image);
1339 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1343 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
1344 metrics->bounds.x1=(double) bounds.xMin;
1345 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
1346 metrics->bounds.y1=(double) bounds.yMin;
1347 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
1348 metrics->bounds.x2=(double) bounds.xMax;
1349 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
1350 metrics->bounds.y2=(double) bounds.yMax;
1351 if (draw_info->render != MagickFalse)
1352 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1353 (draw_info->stroke_pattern != (Image *) NULL))
1358 annotate_info->affine.tx=glyph.origin.x/64.0;
1359 annotate_info->affine.ty=glyph.origin.y/64.0;
1360 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1361 outline,&OutlineMethods,annotate_info);
1363 FT_Vector_Transform(&glyph.origin,&affine);
1364 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1365 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1366 (FT_Vector *) NULL,MagickTrue);
1369 bitmap=(FT_BitmapGlyph) glyph.image;
1370 point.x=offset->x+bitmap->left;
1371 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1372 point.x=offset->x+(origin.x >> 6);
1373 point.y=offset->y-bitmap->top;
1374 if (draw_info->render != MagickFalse)
1382 register unsigned char
1386 Rasterize the glyph.
1389 image_view=AcquireAuthenticCacheView(image,exception);
1390 p=bitmap->bitmap.buffer;
1391 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1414 if (status == MagickFalse)
1416 x_offset=(ssize_t) ceil(point.x-0.5);
1417 y_offset=(ssize_t) ceil(point.y+y-0.5);
1418 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1421 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1425 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1426 bitmap->bitmap.width,1,exception);
1427 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1429 n=y*bitmap->bitmap.pitch-1;
1430 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
1434 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1436 q+=GetPixelChannels(image);
1439 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
1440 fill_opacity=(double) (p[n])/(bitmap->bitmap.num_grays-1);
1442 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1443 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1444 if (draw_info->text_antialias == MagickFalse)
1445 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1446 if (active == MagickFalse)
1447 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1449 if (q == (Quantum *) NULL)
1451 q+=GetPixelChannels(image);
1454 GetPixelInfo(image,&fill_color);
1455 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1457 fill_opacity=fill_opacity*fill_color.alpha;
1458 CompositePixelOver(image,&fill_color,fill_opacity,q,
1459 GetPixelAlpha(image,q),q);
1460 if (active == MagickFalse)
1462 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1463 if (sync == MagickFalse)
1466 q+=GetPixelChannels(image);
1468 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1469 if (sync == MagickFalse)
1472 image_view=DestroyCacheView(image_view);
1474 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1475 metrics->width=bitmap->left+bitmap->bitmap.width;
1476 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1477 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1478 (IsUTFSpace(code) == MagickFalse))
1479 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
1481 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
1482 metrics->origin.x=(double) origin.x;
1483 metrics->origin.y=(double) origin.y;
1484 if (last_glyph.id != 0)
1485 FT_Done_Glyph(last_glyph.image);
1489 if (utf8 != (unsigned char *) NULL)
1490 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
1491 if (last_glyph.id != 0)
1492 FT_Done_Glyph(last_glyph.image);
1493 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1494 (draw_info->stroke_pattern != (Image *) NULL))
1496 if (draw_info->render != MagickFalse)
1501 annotate_info->linejoin=RoundJoin;
1502 annotate_info->affine.tx=offset->x;
1503 annotate_info->affine.ty=offset->y;
1504 (void) ConcatenateString(&annotate_info->primitive,"'");
1505 (void) DrawImage(image,annotate_info,exception);
1509 Determine font metrics.
1511 glyph.id=FT_Get_Char_Index(face,'_');
1512 glyph.origin=origin;
1513 status=FT_Load_Glyph(face,glyph.id,flags);
1516 status=FT_Get_Glyph(face->glyph,&glyph.image);
1519 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1523 FT_Vector_Transform(&glyph.origin,&affine);
1524 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1525 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1526 (FT_Vector *) NULL,MagickTrue);
1527 bitmap=(FT_BitmapGlyph) glyph.image;
1528 if (bitmap->left > metrics->width)
1529 metrics->width=bitmap->left;
1532 FT_Done_Glyph(glyph.image);
1534 metrics->width-=metrics->bounds.x1/64.0;
1535 metrics->width+=annotate_info->stroke_width;
1536 metrics->bounds.x1/=64.0;
1537 metrics->bounds.y1/=64.0;
1538 metrics->bounds.x2/=64.0;
1539 metrics->bounds.y2/=64.0;
1540 metrics->origin.x/=64.0;
1541 metrics->origin.y/=64.0;
1543 Relinquish resources.
1545 annotate_info=DestroyDrawInfo(annotate_info);
1546 (void) FT_Done_Face(face);
1547 (void) FT_Done_FreeType(library);
1551 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1552 const char *magick_unused(encoding),const PointInfo *offset,
1553 TypeMetric *metrics,ExceptionInfo *exception)
1555 (void) ThrowMagickException(exception,GetMagickModule(),
1556 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
1557 draw_info->font != (char *) NULL ? draw_info->font : "none");
1558 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1567 + R e n d e r P o s t s c r i p t %
1571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1573 % RenderPostscript() renders text on the image with a Postscript font. It
1574 % also returns the bounding box of the text relative to the image.
1576 % The format of the RenderPostscript method is:
1578 % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1579 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1581 % A description of each parameter follows:
1583 % o image: the image.
1585 % o draw_info: the draw info.
1587 % o offset: (x,y) location of text relative to image.
1589 % o metrics: bounding box of text.
1591 % o exception: return any errors or warnings in this structure.
1595 static inline size_t MagickMin(const size_t x,const size_t y)
1602 static char *EscapeParenthesis(const char *text)
1617 buffer=AcquireString(text);
1619 for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
1621 if ((text[i] == '(') || (text[i] == ')'))
1632 static MagickBooleanType RenderPostscript(Image *image,
1633 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1634 ExceptionInfo *exception)
1637 filename[MaxTextExtent],
1638 geometry[MaxTextExtent],
1671 Render label with a Postscript font.
1673 if (image->debug != MagickFalse)
1674 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1675 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1676 draw_info->font : "none",draw_info->pointsize);
1678 unique_file=AcquireUniqueFileResource(filename);
1679 if (unique_file != -1)
1680 file=fdopen(unique_file,"wb");
1681 if ((unique_file == -1) || (file == (FILE *) NULL))
1683 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
1684 return(MagickFalse);
1686 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1687 (void) FormatLocaleFile(file,"/ReencodeType\n");
1688 (void) FormatLocaleFile(file,"{\n");
1689 (void) FormatLocaleFile(file," findfont dup length\n");
1690 (void) FormatLocaleFile(file,
1691 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
1692 (void) FormatLocaleFile(file,
1693 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
1694 (void) FormatLocaleFile(file,"} bind def\n");
1696 Sample to compute bounding box.
1698 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1699 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1700 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
1703 length=strlen(draw_info->text);
1704 for (i=0; i <= (ssize_t) (length+2); i++)
1706 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1707 draw_info->affine.ry*2.0*draw_info->pointsize);
1708 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1709 draw_info->affine.sy*2.0*draw_info->pointsize);
1710 if (point.x > extent.x)
1712 if (point.y > extent.y)
1715 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
1716 extent.x/2.0,extent.y/2.0);
1717 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
1718 draw_info->pointsize);
1719 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1720 (strchr(draw_info->font,'/') != (char *) NULL))
1721 (void) FormatLocaleFile(file,
1722 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1724 (void) FormatLocaleFile(file,
1725 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1727 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
1728 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1729 draw_info->affine.sy);
1730 text=EscapeParenthesis(draw_info->text);
1731 if (identity == MagickFalse)
1732 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1734 (void) FormatLocaleFile(file,"(%s) show\n",text);
1735 text=DestroyString(text);
1736 (void) FormatLocaleFile(file,"showpage\n");
1737 (void) fclose(file);
1738 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
1739 floor(extent.x+0.5),floor(extent.y+0.5));
1740 annotate_info=AcquireImageInfo();
1741 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
1743 (void) CloneString(&annotate_info->page,geometry);
1744 if (draw_info->density != (char *) NULL)
1745 (void) CloneString(&annotate_info->density,draw_info->density);
1746 annotate_info->antialias=draw_info->text_antialias;
1747 annotate_image=ReadImage(annotate_info,exception);
1748 CatchException(exception);
1749 annotate_info=DestroyImageInfo(annotate_info);
1750 (void) RelinquishUniqueFileResource(filename);
1751 if (annotate_image == (Image *) NULL)
1752 return(MagickFalse);
1753 (void) NegateImage(annotate_image,MagickFalse,exception);
1754 resolution.x=DefaultResolution;
1755 resolution.y=DefaultResolution;
1756 if (draw_info->density != (char *) NULL)
1764 flags=ParseGeometry(draw_info->density,&geometry_info);
1765 resolution.x=geometry_info.rho;
1766 resolution.y=geometry_info.sigma;
1767 if ((flags & SigmaValue) == 0)
1768 resolution.y=resolution.x;
1770 if (identity == MagickFalse)
1771 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
1777 crop_info=GetImageBoundingBox(annotate_image,exception);
1778 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
1779 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
1780 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
1782 (void) FormatLocaleString(geometry,MaxTextExtent,
1783 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
1784 crop_info.height,(double) crop_info.x,(double) crop_info.y);
1785 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
1787 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1788 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1789 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1790 metrics->ascent=metrics->pixels_per_em.x;
1791 metrics->descent=metrics->pixels_per_em.y/-5.0;
1792 metrics->width=(double) annotate_image->columns/
1793 ExpandAffine(&draw_info->affine);
1794 metrics->height=1.152*metrics->pixels_per_em.x;
1795 metrics->max_advance=metrics->pixels_per_em.x;
1796 metrics->bounds.x1=0.0;
1797 metrics->bounds.y1=metrics->descent;
1798 metrics->bounds.x2=metrics->ascent+metrics->descent;
1799 metrics->bounds.y2=metrics->ascent+metrics->descent;
1800 metrics->underline_position=(-2.0);
1801 metrics->underline_thickness=1.0;
1802 if (draw_info->render == MagickFalse)
1804 annotate_image=DestroyImage(annotate_image);
1807 if (draw_info->fill.alpha != TransparentAlpha)
1821 if (image->alpha_trait != BlendPixelTrait)
1822 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1823 if (annotate_image->alpha_trait != BlendPixelTrait)
1824 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1826 fill_color=draw_info->fill;
1827 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
1828 for (y=0; y < (ssize_t) annotate_image->rows; y++)
1836 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1838 if (q == (Quantum *) NULL)
1840 for (x=0; x < (ssize_t) annotate_image->columns; x++)
1842 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
1843 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1844 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
1845 SetPixelRed(annotate_image,fill_color.red,q);
1846 SetPixelGreen(annotate_image,fill_color.green,q);
1847 SetPixelBlue(annotate_image,fill_color.blue,q);
1848 q+=GetPixelChannels(annotate_image);
1850 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1851 if (sync == MagickFalse)
1854 annotate_view=DestroyCacheView(annotate_view);
1855 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
1856 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
1857 metrics->descent)-0.5),exception);
1859 annotate_image=DestroyImage(annotate_image);
1864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1868 + R e n d e r X 1 1 %
1872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1874 % RenderX11() renders text on the image with an X11 font. It also returns the
1875 % bounding box of the text relative to the image.
1877 % The format of the RenderX11 method is:
1879 % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
1880 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1882 % A description of each parameter follows:
1884 % o image: the image.
1886 % o draw_info: the draw info.
1888 % o offset: (x,y) location of text relative to image.
1890 % o metrics: bounding box of text.
1892 % o exception: return any errors or warnings in this structure.
1895 #if defined(MAGICKCORE_X11_DELEGATE)
1896 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1897 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1906 *display = (Display *) NULL;
1908 static XAnnotateInfo
1917 static XResourceInfo
1923 static XStandardColormap
1933 if (annotate_semaphore == (SemaphoreInfo *) NULL)
1934 AcquireSemaphoreInfo(&annotate_semaphore);
1935 LockSemaphoreInfo(annotate_semaphore);
1936 if (display == (Display *) NULL)
1945 Open X server connection.
1947 display=XOpenDisplay(draw_info->server_name);
1948 if (display == (Display *) NULL)
1950 ThrowXWindowException(XServerError,"UnableToOpenXServer",
1951 draw_info->server_name);
1952 return(MagickFalse);
1955 Get user defaults from X resource database.
1957 (void) XSetErrorHandler(XError);
1958 image_info=AcquireImageInfo();
1959 client_name=GetClientName();
1960 resource_database=XGetResourceDatabase(display,client_name);
1961 XGetResourceInfo(image_info,resource_database,client_name,
1963 resource_info.close_server=MagickFalse;
1964 resource_info.colormap=PrivateColormap;
1965 resource_info.font=AcquireString(draw_info->font);
1966 resource_info.background_color=AcquireString("#ffffffffffff");
1967 resource_info.foreground_color=AcquireString("#000000000000");
1968 map_info=XAllocStandardColormap();
1969 if (map_info == (XStandardColormap *) NULL)
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 ThrowXWindowException(XServerError,"UnableToGetVisual",
1983 return(MagickFalse);
1985 map_info->colormap=(Colormap) NULL;
1986 pixel.pixels=(unsigned long *) NULL;
1988 Initialize Standard Colormap info.
1990 XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen),
1992 XGetPixelInfo(display,visual_info,map_info,&resource_info,(Image *) NULL,
1994 pixel.annotate_context=XDefaultGC(display,visual_info->screen);
1996 Initialize font info.
1998 font_info=XBestFont(display,&resource_info,MagickFalse);
1999 if (font_info == (XFontStruct *) NULL)
2001 ThrowXWindowException(XServerError,"UnableToLoadFont",
2003 return(MagickFalse);
2005 if ((map_info == (XStandardColormap *) NULL) ||
2006 (visual_info == (XVisualInfo *) NULL) ||
2007 (font_info == (XFontStruct *) NULL))
2009 XFreeResources(display,visual_info,map_info,&pixel,font_info,
2010 &resource_info,(XWindowInfo *) NULL);
2011 ThrowXWindowException(XServerError,"UnableToLoadFont",
2013 return(MagickFalse);
2015 cache_info=(*draw_info);
2017 UnlockSemaphoreInfo(annotate_semaphore);
2019 Initialize annotate info.
2021 XGetAnnotateInfo(&annotate_info);
2022 annotate_info.stencil=ForegroundStencil;
2023 if (cache_info.font != draw_info->font)
2026 Type name has changed.
2028 (void) XFreeFont(display,font_info);
2029 (void) CloneString(&resource_info.font,draw_info->font);
2030 font_info=XBestFont(display,&resource_info,MagickFalse);
2031 if (font_info == (XFontStruct *) NULL)
2033 ThrowXWindowException(XServerError,"UnableToLoadFont",
2035 return(MagickFalse);
2038 if (image->debug != MagickFalse)
2039 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2040 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
2041 draw_info->font : "none",draw_info->pointsize);
2042 cache_info=(*draw_info);
2043 annotate_info.font_info=font_info;
2044 annotate_info.text=(char *) draw_info->text;
2045 annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text,
2046 (int) strlen(draw_info->text));
2047 annotate_info.height=(unsigned int) font_info->ascent+font_info->descent;
2048 metrics->pixels_per_em.x=(double) font_info->max_bounds.width;
2049 metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent;
2050 metrics->ascent=(double) font_info->ascent+4;
2051 metrics->descent=(double) (-font_info->descent);
2052 metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine);
2053 metrics->height=font_info->ascent+font_info->descent;
2054 metrics->max_advance=(double) font_info->max_bounds.width;
2055 metrics->bounds.x1=0.0;
2056 metrics->bounds.y1=metrics->descent;
2057 metrics->bounds.x2=metrics->ascent+metrics->descent;
2058 metrics->bounds.y2=metrics->ascent+metrics->descent;
2059 metrics->underline_position=(-2.0);
2060 metrics->underline_thickness=1.0;
2061 if (draw_info->render == MagickFalse)
2063 if (draw_info->fill.alpha == TransparentAlpha)
2068 width=annotate_info.width;
2069 height=annotate_info.height;
2070 if ((fabs(draw_info->affine.rx) >= MagickEpsilon) ||
2071 (fabs(draw_info->affine.ry) >= MagickEpsilon))
2073 if ((fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2074 (fabs(draw_info->affine.rx+draw_info->affine.ry) < MagickEpsilon))
2075 annotate_info.degrees=(double) (180.0/MagickPI)*
2076 atan2(draw_info->affine.rx,draw_info->affine.sx);
2078 (void) FormatLocaleString(annotate_info.geometry,MaxTextExtent,
2079 "%.20gx%.20g%+.20g%+.20g",(double) width,(double) height,
2080 ceil(offset->x-0.5),ceil(offset->y-metrics->ascent-metrics->descent+
2081 draw_info->interline_spacing-0.5));
2082 pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red);
2083 pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green);
2084 pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue);
2085 status=XAnnotateImage(display,&pixel,&annotate_info,image,exception);
2088 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
2090 return(MagickFalse);
2095 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
2096 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2101 (void) ThrowMagickException(exception,GetMagickModule(),
2102 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","'%s' (X11)",
2104 return(MagickFalse);