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-2011 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/composite.h"
54 #include "MagickCore/composite-private.h"
55 #include "MagickCore/constitute.h"
56 #include "MagickCore/draw.h"
57 #include "MagickCore/draw-private.h"
58 #include "MagickCore/exception.h"
59 #include "MagickCore/exception-private.h"
60 #include "MagickCore/gem.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image-private.h"
63 #include "MagickCore/log.h"
64 #include "MagickCore/quantum.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/pixel-accessor.h"
67 #include "MagickCore/property.h"
68 #include "MagickCore/resource_.h"
69 #include "MagickCore/semaphore.h"
70 #include "MagickCore/statistic.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/token-private.h"
73 #include "MagickCore/transform.h"
74 #include "MagickCore/type.h"
75 #include "MagickCore/utility.h"
76 #include "MagickCore/utility-private.h"
77 #include "MagickCore/xwindow.h"
78 #include "MagickCore/xwindow-private.h"
79 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
80 #if defined(__MINGW32__)
83 #if defined(MAGICKCORE_HAVE_FT2BUILD_H)
84 # include <ft2build.h>
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)
294 for (i=0; textlist[i] != (char *) NULL; i++)
297 Position text relative to image.
299 annotate_info->affine.tx=geometry_info.xi-image->page.x;
300 annotate_info->affine.ty=geometry_info.psi-image->page.y;
301 (void) CloneString(&annotate->text,textlist[i]);
302 (void) GetTypeMetrics(image,annotate,&metrics,exception);
303 height=(ssize_t) (metrics.ascent-metrics.descent+
304 draw_info->interline_spacing+0.5);
305 switch (annotate->gravity)
307 case UndefinedGravity:
310 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
311 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
314 case NorthWestGravity:
316 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
317 annotate_info->affine.ry*height+annotate_info->affine.ry*
318 (metrics.ascent+metrics.descent);
319 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
320 annotate_info->affine.sy*height+annotate_info->affine.sy*
326 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
327 geometry.width/2.0+i*annotate_info->affine.ry*height-
328 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
329 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
330 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
331 annotate_info->affine.sy*height+annotate_info->affine.sy*
332 metrics.ascent-annotate_info->affine.rx*(metrics.width-
333 metrics.bounds.x1)/2.0;
336 case NorthEastGravity:
338 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
339 geometry.width+i*annotate_info->affine.ry*height-
340 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
341 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
342 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
343 annotate_info->affine.sy*height+annotate_info->affine.sy*
344 metrics.ascent-annotate_info->affine.rx*(metrics.width-
350 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
351 annotate_info->affine.ry*height+annotate_info->affine.ry*
352 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
353 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
354 geometry.height/2.0+i*annotate_info->affine.sy*height+
355 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
356 (number_lines-1.0)*height)/2.0;
362 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
363 geometry.width/2.0+i*annotate_info->affine.ry*height-
364 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
365 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
366 (number_lines-1)*height)/2.0;
367 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
368 geometry.height/2.0+i*annotate_info->affine.sy*height-
369 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
370 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
371 (number_lines-1.0)*height)/2.0;
376 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
377 geometry.width+i*annotate_info->affine.ry*height-
378 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
379 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
380 (number_lines-1.0)*height)/2.0-1.0;
381 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
382 geometry.height/2.0+i*annotate_info->affine.sy*height-
383 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+
384 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
385 (number_lines-1.0)*height)/2.0;
388 case SouthWestGravity:
390 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
391 annotate_info->affine.ry*height-annotate_info->affine.ry*
392 (number_lines-1.0)*height;
393 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
394 geometry.height+i*annotate_info->affine.sy*height-
395 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
400 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
401 geometry.width/2.0+i*annotate_info->affine.ry*height-
402 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
403 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
404 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
405 geometry.height+i*annotate_info->affine.sy*height-
406 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
407 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
410 case SouthEastGravity:
412 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
413 geometry.width+i*annotate_info->affine.ry*height-
414 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
415 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
416 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
417 geometry.height+i*annotate_info->affine.sy*height-
418 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
419 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
423 switch (annotate->align)
427 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
428 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
433 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
434 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
435 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
436 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
441 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
442 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
443 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
444 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
450 if (draw_info->undercolor.alpha != TransparentAlpha)
458 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
459 undercolor_info->fill=draw_info->undercolor;
460 undercolor_info->affine=draw_info->affine;
461 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
462 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
463 (void) FormatLocaleString(primitive,MaxTextExtent,
464 "rectangle 0,0 %g,%.20g",metrics.origin.x,(double) height);
465 (void) CloneString(&undercolor_info->primitive,primitive);
466 (void) DrawImage(image,undercolor_info,exception);
467 (void) DestroyDrawInfo(undercolor_info);
469 annotate_info->affine.tx=offset.x;
470 annotate_info->affine.ty=offset.y;
471 (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
472 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
473 if (annotate->decorate == OverlineDecoration)
475 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
476 metrics.descent-metrics.underline_position));
477 (void) CloneString(&annotate_info->primitive,primitive);
478 (void) DrawImage(image,annotate_info,exception);
481 if (annotate->decorate == UnderlineDecoration)
483 annotate_info->affine.ty-=(draw_info->affine.sy*
484 metrics.underline_position);
485 (void) CloneString(&annotate_info->primitive,primitive);
486 (void) DrawImage(image,annotate_info,exception);
489 Annotate image with text.
491 status=RenderType(image,annotate,&offset,&metrics,exception);
492 if (status == MagickFalse)
494 if (annotate->decorate == LineThroughDecoration)
496 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
497 metrics.underline_position+metrics.descent)/2.0);
498 (void) CloneString(&annotate_info->primitive,primitive);
499 (void) DrawImage(image,annotate_info,exception);
503 Relinquish resources.
505 annotate_info=DestroyDrawInfo(annotate_info);
506 annotate=DestroyDrawInfo(annotate);
507 for (i=0; textlist[i] != (char *) NULL; i++)
508 textlist[i]=DestroyString(textlist[i]);
509 textlist=(char **) RelinquishMagickMemory(textlist);
514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 % F o r m a t M a g i c k C a p t i o n %
522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 % FormatMagickCaption() formats a caption so that it fits within the image
525 % width. It returns the number of lines in the formatted caption.
527 % The format of the FormatMagickCaption method is:
529 % ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
530 % const MagickBooleanType split,TypeMetric *metrics,char **caption,
531 % ExceptionInfo *exception)
533 % A description of each parameter follows.
535 % o image: The image.
537 % o draw_info: the draw info.
539 % o split: when no convenient line breaks-- insert newline.
541 % o metrics: Return the font metrics in this structure.
543 % o caption: the caption.
545 % o exception: return any errors or warnings in this structure.
548 MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
549 const MagickBooleanType split,TypeMetric *metrics,char **caption,
550 ExceptionInfo *exception)
571 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
573 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
575 if (GetUTFCode(p) == '\n')
577 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
580 status=GetTypeMetrics(image,draw_info,metrics,exception);
581 if (status == MagickFalse)
583 width=(size_t) floor(metrics->width+0.5);
584 if (width <= image->columns)
586 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
592 if ((s != (char *) NULL) || (split != MagickFalse))
598 No convenient line breaks-- insert newline.
600 target=AcquireString(*caption);
602 CopyMagickString(target,*caption,n+1);
603 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
604 ConcatenateMagickString(target,p,strlen(*caption)+2);
605 (void) DestroyString(*caption);
613 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
614 if (GetUTFCode(p) == '\n')
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 % G e t M u l t i l i n e T y p e M e t r i c s %
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 % GetMultilineTypeMetrics() returns the following information for the
631 % specified font and text:
639 % maximum horizontal advance
647 % underline thickness
649 % This method is like GetTypeMetrics() but it returns the maximum text width
650 % and height for multiple lines of text.
652 % The format of the GetMultilineTypeMetrics method is:
654 % MagickBooleanType GetMultilineTypeMetrics(Image *image,
655 % const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
657 % A description of each parameter follows:
659 % o image: the image.
661 % o draw_info: the draw info.
663 % o metrics: Return the font metrics in this structure.
665 % o exception: return any errors or warnings in this structure.
668 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
669 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
686 assert(image != (Image *) NULL);
687 assert(image->signature == MagickSignature);
688 if (image->debug != MagickFalse)
689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
690 assert(draw_info != (DrawInfo *) NULL);
691 assert(draw_info->text != (char *) NULL);
692 assert(draw_info->signature == MagickSignature);
693 if (*draw_info->text == '\0')
695 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
696 annotate_info->text=DestroyString(annotate_info->text);
698 Convert newlines to multiple lines of text.
700 textlist=StringToList(draw_info->text);
701 if (textlist == (char **) NULL)
703 annotate_info->render=MagickFalse;
704 annotate_info->direction=UndefinedDirection;
705 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
706 (void) ResetMagickMemory(&extent,0,sizeof(extent));
708 Find the widest of the text lines.
710 annotate_info->text=textlist[0];
711 status=GetTypeMetrics(image,annotate_info,&extent,exception);
713 for (i=1; textlist[i] != (char *) NULL; i++)
715 annotate_info->text=textlist[i];
716 status=GetTypeMetrics(image,annotate_info,&extent,exception);
717 if (extent.width > metrics->width)
720 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
721 (i-1)*draw_info->interline_spacing);
723 Relinquish resources.
725 annotate_info->text=(char *) NULL;
726 annotate_info=DestroyDrawInfo(annotate_info);
727 for (i=0; textlist[i] != (char *) NULL; i++)
728 textlist[i]=DestroyString(textlist[i]);
729 textlist=(char **) RelinquishMagickMemory(textlist);
734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738 % G e t T y p e M e t r i c s %
742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
744 % GetTypeMetrics() returns the following information for the specified font
753 % maximum horizontal advance
761 % underline thickness
763 % The format of the GetTypeMetrics method is:
765 % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
766 % TypeMetric *metrics,ExceptionInfo *exception)
768 % A description of each parameter follows:
770 % o image: the image.
772 % o draw_info: the draw info.
774 % o metrics: Return the font metrics in this structure.
776 % o exception: return any errors or warnings in this structure.
779 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
780 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
791 assert(image != (Image *) NULL);
792 assert(image->signature == MagickSignature);
793 if (image->debug != MagickFalse)
794 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
795 assert(draw_info != (DrawInfo *) NULL);
796 assert(draw_info->text != (char *) NULL);
797 assert(draw_info->signature == MagickSignature);
798 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
799 annotate_info->render=MagickFalse;
800 annotate_info->direction=UndefinedDirection;
801 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
804 status=RenderType(image,annotate_info,&offset,metrics,exception);
805 if (image->debug != MagickFalse)
806 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
807 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
808 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
809 "underline position: %g; underline thickness: %g",annotate_info->text,
810 metrics->width,metrics->height,metrics->ascent,metrics->descent,
811 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
812 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
813 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
814 metrics->underline_position,metrics->underline_thickness);
815 annotate_info=DestroyDrawInfo(annotate_info);
820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 + R e n d e r T y p e %
828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830 % RenderType() renders text on the image. It also returns the bounding box of
831 % the text relative to the image.
833 % The format of the RenderType method is:
835 % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
836 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
838 % A description of each parameter follows:
840 % o image: the image.
842 % o draw_info: the draw info.
844 % o offset: (x,y) location of text relative to image.
846 % o metrics: bounding box of text.
848 % o exception: return any errors or warnings in this structure.
851 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
852 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
863 type_info=(const TypeInfo *) NULL;
864 if (draw_info->font != (char *) NULL)
866 if (*draw_info->font == '@')
868 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
872 if (*draw_info->font == '-')
873 return(RenderX11(image,draw_info,offset,metrics,exception));
874 if (IsPathAccessible(draw_info->font) != MagickFalse)
876 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
880 type_info=GetTypeInfo(draw_info->font,exception);
881 if (type_info == (const TypeInfo *) NULL)
882 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
883 "UnableToReadFont","`%s'",draw_info->font);
885 if ((type_info == (const TypeInfo *) NULL) &&
886 (draw_info->family != (const char *) NULL))
888 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
889 draw_info->stretch,draw_info->weight,exception);
890 if (type_info == (const TypeInfo *) NULL)
891 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
892 "UnableToReadFont","`%s'",draw_info->family);
894 if (type_info == (const TypeInfo *) NULL)
895 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
896 draw_info->stretch,draw_info->weight,exception);
897 if (type_info == (const TypeInfo *) NULL)
898 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
899 draw_info->stretch,draw_info->weight,exception);
900 if (type_info == (const TypeInfo *) NULL)
901 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
902 draw_info->stretch,draw_info->weight,exception);
903 if (type_info == (const TypeInfo *) NULL)
904 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
905 draw_info->stretch,draw_info->weight,exception);
906 if (type_info == (const TypeInfo *) NULL)
907 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
908 draw_info->stretch,draw_info->weight,exception);
909 if (type_info == (const TypeInfo *) NULL)
910 type_info=GetTypeInfo("*",exception);
911 if (type_info == (const TypeInfo *) NULL)
913 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
917 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
918 annotate_info->face=type_info->face;
919 if (type_info->metrics != (char *) NULL)
920 (void) CloneString(&annotate_info->metrics,type_info->metrics);
921 if (type_info->glyphs != (char *) NULL)
922 (void) CloneString(&annotate_info->font,type_info->glyphs);
923 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
925 annotate_info=DestroyDrawInfo(annotate_info);
930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
934 + R e n d e r F r e e t y p e %
938 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 % RenderFreetype() renders text on the image with a Truetype font. It also
941 % returns the bounding box of the text relative to the image.
943 % The format of the RenderFreetype method is:
945 % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
946 % const char *encoding,const PointInfo *offset,TypeMetric *metrics,
947 % ExceptionInfo *exception)
949 % A description of each parameter follows:
951 % o image: the image.
953 % o draw_info: the draw info.
955 % o encoding: the font encoding.
957 % o offset: (x,y) location of text relative to image.
959 % o metrics: bounding box of text.
961 % o exception: return any errors or warnings in this structure.
965 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
967 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
976 affine=draw_info->affine;
977 (void) FormatLocaleString(path,MaxTextExtent,
978 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
979 p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
980 affine.ty-to->y/64.0);
981 (void) ConcatenateString(&draw_info->primitive,path);
985 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
993 affine=draw_info->affine;
994 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
995 to->x/64.0,affine.ty-to->y/64.0);
996 (void) ConcatenateString(&draw_info->primitive,path);
1000 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1006 path[MaxTextExtent];
1008 affine=draw_info->affine;
1009 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
1010 to->x/64.0,affine.ty-to->y/64.0);
1011 (void) ConcatenateString(&draw_info->primitive,path);
1015 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1016 DrawInfo *draw_info)
1022 path[MaxTextExtent];
1024 affine=draw_info->affine;
1025 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
1026 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1027 affine.ty-to->y/64.0);
1028 (void) ConcatenateString(&draw_info->primitive,path);
1032 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1033 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1034 ExceptionInfo *exception)
1036 #if !defined(FT_OPEN_PATHNAME)
1037 #define FT_OPEN_PATHNAME ft_open_pathname
1040 typedef struct _GlyphInfo
1106 static FT_Outline_Funcs
1109 (FT_Outline_MoveTo_Func) TraceMoveTo,
1110 (FT_Outline_LineTo_Func) TraceLineTo,
1111 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1112 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1120 Initialize Truetype library.
1122 status=FT_Init_FreeType(&library);
1124 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1126 args.flags=FT_OPEN_PATHNAME;
1127 if (draw_info->font == (char *) NULL)
1128 args.pathname=ConstantString("helvetica");
1130 if (*draw_info->font != '@')
1131 args.pathname=ConstantString(draw_info->font);
1133 args.pathname=ConstantString(draw_info->font+1);
1134 face=(FT_Face) NULL;
1135 status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
1136 args.pathname=DestroyString(args.pathname);
1139 (void) FT_Done_FreeType(library);
1140 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
1141 "UnableToReadFont","`%s'",draw_info->font);
1142 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1144 if ((draw_info->metrics != (char *) NULL) &&
1145 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1146 (void) FT_Attach_File(face,draw_info->metrics);
1147 encoding_type=ft_encoding_unicode;
1148 status=FT_Select_Charmap(face,encoding_type);
1149 if ((status != 0) && (face->num_charmaps != 0))
1150 status=FT_Set_Charmap(face,face->charmaps[0]);
1151 if (encoding != (const char *) NULL)
1153 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1154 encoding_type=ft_encoding_adobe_custom;
1155 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1156 encoding_type=ft_encoding_adobe_expert;
1157 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1158 encoding_type=ft_encoding_adobe_standard;
1159 if (LocaleCompare(encoding,"AppleRoman") == 0)
1160 encoding_type=ft_encoding_apple_roman;
1161 if (LocaleCompare(encoding,"BIG5") == 0)
1162 encoding_type=ft_encoding_big5;
1163 if (LocaleCompare(encoding,"GB2312") == 0)
1164 encoding_type=ft_encoding_gb2312;
1165 if (LocaleCompare(encoding,"Johab") == 0)
1166 encoding_type=ft_encoding_johab;
1167 #if defined(ft_encoding_latin_1)
1168 if (LocaleCompare(encoding,"Latin-1") == 0)
1169 encoding_type=ft_encoding_latin_1;
1171 if (LocaleCompare(encoding,"Latin-2") == 0)
1172 encoding_type=ft_encoding_latin_2;
1173 if (LocaleCompare(encoding,"None") == 0)
1174 encoding_type=ft_encoding_none;
1175 if (LocaleCompare(encoding,"SJIScode") == 0)
1176 encoding_type=ft_encoding_sjis;
1177 if (LocaleCompare(encoding,"Symbol") == 0)
1178 encoding_type=ft_encoding_symbol;
1179 if (LocaleCompare(encoding,"Unicode") == 0)
1180 encoding_type=ft_encoding_unicode;
1181 if (LocaleCompare(encoding,"Wansung") == 0)
1182 encoding_type=ft_encoding_wansung;
1183 status=FT_Select_Charmap(face,encoding_type);
1185 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1190 resolution.x=DefaultResolution;
1191 resolution.y=DefaultResolution;
1192 if (draw_info->density != (char *) NULL)
1200 flags=ParseGeometry(draw_info->density,&geometry_info);
1201 resolution.x=geometry_info.rho;
1202 resolution.y=geometry_info.sigma;
1203 if ((flags & SigmaValue) == 0)
1204 resolution.y=resolution.x;
1206 status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1207 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1208 (FT_UInt) resolution.y);
1209 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1210 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1211 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1212 metrics->descent=(double) face->size->metrics.descender/64.0;
1214 metrics->origin.x=0;
1215 metrics->origin.y=0;
1216 metrics->height=(double) face->size->metrics.height/64.0;
1217 metrics->max_advance=0.0;
1218 if (face->size->metrics.max_advance > MagickEpsilon)
1219 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1220 metrics->bounds.x1=0.0;
1221 metrics->bounds.y1=metrics->descent;
1222 metrics->bounds.x2=metrics->ascent+metrics->descent;
1223 metrics->bounds.y2=metrics->ascent+metrics->descent;
1224 metrics->underline_position=face->underline_position/64.0;
1225 metrics->underline_thickness=face->underline_thickness/64.0;
1226 if (*draw_info->text == '\0')
1228 (void) FT_Done_Face(face);
1229 (void) FT_Done_FreeType(library);
1233 Compute bounding box.
1235 if (image->debug != MagickFalse)
1236 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1237 "font-encoding %s; text-encoding %s; pointsize %g",
1238 draw_info->font != (char *) NULL ? draw_info->font : "none",
1239 encoding != (char *) NULL ? encoding : "none",
1240 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1241 draw_info->pointsize);
1242 flags=FT_LOAD_NO_BITMAP;
1243 value=GetImageProperty(image,"type:hinting");
1244 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1245 flags|=FT_LOAD_NO_HINTING;
1249 last_glyph.image=NULL;
1256 if (draw_info->render != MagickFalse)
1258 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1259 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1260 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1261 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1263 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1264 (void) CloneString(&annotate_info->primitive,"path '");
1265 if (draw_info->render != MagickFalse)
1267 if (image->storage_class != DirectClass)
1268 (void) SetImageStorageClass(image,DirectClass,exception);
1269 if (image->matte == MagickFalse)
1270 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1273 if (draw_info->direction == RightToLeftDirection)
1277 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1278 if (GetUTFCode(p) < 0)
1280 utf8=(unsigned char *) NULL;
1281 if (GetUTFCode(p) == 0)
1285 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1286 if (utf8 != (unsigned char *) NULL)
1289 for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1292 Render UTF-8 sequence.
1294 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1296 glyph.id=FT_Get_Char_Index(face,'?');
1297 if ((glyph.id != 0) && (last_glyph.id != 0))
1299 if (draw_info->kerning != 0.0)
1300 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
1302 if (FT_HAS_KERNING(face))
1307 status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1308 ft_kerning_default,&kerning);
1310 origin.x+=(FT_Pos) (direction*kerning.x);
1313 glyph.origin=origin;
1314 status=FT_Load_Glyph(face,glyph.id,flags);
1317 status=FT_Get_Glyph(face->glyph,&glyph.image);
1320 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1324 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
1325 metrics->bounds.x1=bounds.xMin;
1326 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
1327 metrics->bounds.y1=bounds.yMin;
1328 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
1329 metrics->bounds.x2=bounds.xMax;
1330 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
1331 metrics->bounds.y2=bounds.yMax;
1332 if (draw_info->render != MagickFalse)
1333 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1334 (draw_info->stroke_pattern != (Image *) NULL))
1339 annotate_info->affine.tx=glyph.origin.x/64.0;
1340 annotate_info->affine.ty=glyph.origin.y/64.0;
1341 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1342 outline,&OutlineMethods,annotate_info);
1344 FT_Vector_Transform(&glyph.origin,&affine);
1345 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1346 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1347 (FT_Vector *) NULL,MagickTrue);
1350 bitmap=(FT_BitmapGlyph) glyph.image;
1351 point.x=offset->x+bitmap->left;
1352 point.y=offset->y-bitmap->top;
1353 if (draw_info->render != MagickFalse)
1362 Rasterize the glyph.
1365 image_view=AcquireCacheView(image);
1366 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1384 register unsigned char
1391 if (status == MagickFalse)
1393 x_offset=(ssize_t) ceil(point.x-0.5);
1394 y_offset=(ssize_t) ceil(point.y+y-0.5);
1395 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1398 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1402 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1403 bitmap->bitmap.width,1,exception);
1404 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1406 p=bitmap->bitmap.buffer+y*bitmap->bitmap.width;
1407 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
1410 if ((*p == 0) || (x_offset < 0) ||
1411 (x_offset >= (ssize_t) image->columns))
1414 q+=GetPixelChannels(image);
1417 fill_opacity=(MagickRealType) (*p)/(bitmap->bitmap.num_grays-1);
1418 if (draw_info->text_antialias == MagickFalse)
1419 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1420 if (active == MagickFalse)
1421 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1423 if (q == (Quantum *) NULL)
1426 q+=GetPixelChannels(image);
1429 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
1430 fill_opacity=fill_opacity*fill_color.alpha;
1431 CompositePixelOver(image,&fill_color,fill_opacity,q,
1432 GetPixelAlpha(image,q),q);
1433 if (active == MagickFalse)
1435 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1436 if (sync == MagickFalse)
1440 q+=GetPixelChannels(image);
1442 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1443 if (sync == MagickFalse)
1446 image_view=DestroyCacheView(image_view);
1448 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1449 metrics->width=bitmap->left+bitmap->bitmap.width;
1450 if ((draw_info->interword_spacing != 0.0) &&
1451 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1452 (IsUTFSpace(code) == MagickFalse))
1453 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
1455 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
1456 metrics->origin.x=origin.x;
1457 metrics->origin.y=origin.y;
1458 if (last_glyph.id != 0)
1459 FT_Done_Glyph(last_glyph.image);
1463 if (utf8 != (unsigned char *) NULL)
1464 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
1465 if (last_glyph.id != 0)
1466 FT_Done_Glyph(last_glyph.image);
1467 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1468 (draw_info->stroke_pattern != (Image *) NULL))
1470 if (draw_info->render != MagickFalse)
1475 annotate_info->linejoin=RoundJoin;
1476 annotate_info->affine.tx=offset->x;
1477 annotate_info->affine.ty=offset->y;
1478 (void) ConcatenateString(&annotate_info->primitive,"'");
1479 (void) DrawImage(image,annotate_info,exception);
1483 Determine font metrics.
1485 glyph.id=FT_Get_Char_Index(face,'_');
1486 glyph.origin=origin;
1487 status=FT_Load_Glyph(face,glyph.id,flags);
1490 status=FT_Get_Glyph(face->glyph,&glyph.image);
1493 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1497 FT_Vector_Transform(&glyph.origin,&affine);
1498 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1499 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1500 (FT_Vector *) NULL,MagickTrue);
1501 bitmap=(FT_BitmapGlyph) glyph.image;
1502 if (bitmap->left > metrics->width)
1503 metrics->width=bitmap->left;
1506 FT_Done_Glyph(glyph.image);
1508 metrics->width-=metrics->bounds.x1/64.0;
1509 metrics->bounds.x1/=64.0;
1510 metrics->bounds.y1/=64.0;
1511 metrics->bounds.x2/=64.0;
1512 metrics->bounds.y2/=64.0;
1513 metrics->origin.x/=64.0;
1514 metrics->origin.y/=64.0;
1516 Relinquish resources.
1518 annotate_info=DestroyDrawInfo(annotate_info);
1519 (void) FT_Done_Face(face);
1520 (void) FT_Done_FreeType(library);
1524 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1525 const char *magick_unused(encoding),const PointInfo *offset,
1526 TypeMetric *metrics,ExceptionInfo *exception)
1528 (void) ThrowMagickException(exception,GetMagickModule(),
1529 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (Freetype)",
1530 draw_info->font != (char *) NULL ? draw_info->font : "none");
1531 return(RenderPostscript(image,draw_info,offset,metrics));
1536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1540 + R e n d e r P o s t s c r i p t %
1544 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1546 % RenderPostscript() renders text on the image with a Postscript font. It
1547 % also returns the bounding box of the text relative to the image.
1549 % The format of the RenderPostscript method is:
1551 % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1552 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1554 % A description of each parameter follows:
1556 % o image: the image.
1558 % o draw_info: the draw info.
1560 % o offset: (x,y) location of text relative to image.
1562 % o metrics: bounding box of text.
1564 % o exception: return any errors or warnings in this structure.
1568 static inline size_t MagickMin(const size_t x,const size_t y)
1575 static char *EscapeParenthesis(const char *text)
1590 buffer=AcquireString(text);
1592 for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
1594 if ((text[i] == '(') || (text[i] == ')'))
1605 static MagickBooleanType RenderPostscript(Image *image,
1606 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1607 ExceptionInfo *exception)
1610 filename[MaxTextExtent],
1611 geometry[MaxTextExtent],
1641 Render label with a Postscript font.
1643 if (image->debug != MagickFalse)
1644 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1645 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1646 draw_info->font : "none",draw_info->pointsize);
1648 unique_file=AcquireUniqueFileResource(filename);
1649 if (unique_file != -1)
1650 file=fdopen(unique_file,"wb");
1651 if ((unique_file == -1) || (file == (FILE *) NULL))
1653 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
1654 return(MagickFalse);
1656 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1657 (void) FormatLocaleFile(file,"/ReencodeType\n");
1658 (void) FormatLocaleFile(file,"{\n");
1659 (void) FormatLocaleFile(file," findfont dup length\n");
1660 (void) FormatLocaleFile(file,
1661 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
1662 (void) FormatLocaleFile(file,
1663 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
1664 (void) FormatLocaleFile(file,"} bind def\n");
1666 Sample to compute bounding box.
1668 identity=(draw_info->affine.sx == draw_info->affine.sy) &&
1669 (draw_info->affine.rx == 0.0) && (draw_info->affine.ry == 0.0) ?
1670 MagickTrue : MagickFalse;
1673 for (i=0; i <= (ssize_t) (strlen(draw_info->text)+2); i++)
1675 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1676 draw_info->affine.ry*2.0*draw_info->pointsize);
1677 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1678 draw_info->affine.sy*2.0*draw_info->pointsize);
1679 if (point.x > extent.x)
1681 if (point.y > extent.y)
1684 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
1685 extent.x/2.0,extent.y/2.0);
1686 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
1687 draw_info->pointsize);
1688 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1689 (strchr(draw_info->font,'/') != (char *) NULL))
1690 (void) FormatLocaleFile(file,
1691 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1693 (void) FormatLocaleFile(file,
1694 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1696 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
1697 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1698 draw_info->affine.sy);
1699 text=EscapeParenthesis(draw_info->text);
1700 if (identity == MagickFalse)
1701 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1703 (void) FormatLocaleFile(file,"(%s) show\n",text);
1704 text=DestroyString(text);
1705 (void) FormatLocaleFile(file,"showpage\n");
1706 (void) fclose(file);
1707 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
1708 floor(extent.x+0.5),floor(extent.y+0.5));
1709 annotate_info=AcquireImageInfo();
1710 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
1712 (void) CloneString(&annotate_info->page,geometry);
1713 if (draw_info->density != (char *) NULL)
1714 (void) CloneString(&annotate_info->density,draw_info->density);
1715 annotate_info->antialias=draw_info->text_antialias;
1716 annotate_image=ReadImage(annotate_info,exception);
1717 CatchException(exception);
1718 annotate_info=DestroyImageInfo(annotate_info);
1719 (void) RelinquishUniqueFileResource(filename);
1720 if (annotate_image == (Image *) NULL)
1721 return(MagickFalse);
1722 resolution.x=DefaultResolution;
1723 resolution.y=DefaultResolution;
1724 if (draw_info->density != (char *) NULL)
1732 flags=ParseGeometry(draw_info->density,&geometry_info);
1733 resolution.x=geometry_info.rho;
1734 resolution.y=geometry_info.sigma;
1735 if ((flags & SigmaValue) == 0)
1736 resolution.y=resolution.x;
1738 if (identity == MagickFalse)
1739 (void) TransformImage(&annotate_image,"0x0",(char *) NULL);
1745 crop_info=GetImageBoundingBox(annotate_image,exception);
1746 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
1747 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
1748 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
1750 (void) FormatLocaleString(geometry,MaxTextExtent,
1751 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
1752 crop_info.height,(double) crop_info.x,(double) crop_info.y);
1753 (void) TransformImage(&annotate_image,geometry,(char *) NULL);
1755 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1756 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1757 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1758 metrics->ascent=metrics->pixels_per_em.x;
1759 metrics->descent=metrics->pixels_per_em.y/-5.0;
1760 metrics->width=(double) annotate_image->columns/
1761 ExpandAffine(&draw_info->affine);
1762 metrics->height=1.152*metrics->pixels_per_em.x;
1763 metrics->max_advance=metrics->pixels_per_em.x;
1764 metrics->bounds.x1=0.0;
1765 metrics->bounds.y1=metrics->descent;
1766 metrics->bounds.x2=metrics->ascent+metrics->descent;
1767 metrics->bounds.y2=metrics->ascent+metrics->descent;
1768 metrics->underline_position=(-2.0);
1769 metrics->underline_thickness=1.0;
1770 if (draw_info->render == MagickFalse)
1772 annotate_image=DestroyImage(annotate_image);
1775 if (draw_info->fill.alpha != TransparentAlpha)
1789 if (image->matte == MagickFalse)
1790 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1791 if (annotate_image->matte == MagickFalse)
1792 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1794 fill_color=draw_info->fill;
1795 annotate_view=AcquireCacheView(annotate_image);
1796 for (y=0; y < (ssize_t) annotate_image->rows; y++)
1804 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1806 if (q == (Quantum *) NULL)
1808 for (x=0; x < (ssize_t) annotate_image->columns; x++)
1810 (void) GetFillColor(draw_info,x,y,&fill_color);
1811 SetPixelAlpha(annotate_image,ClampToQuantum((((MagickRealType)
1812 GetPixelIntensity(annotate_image,q)*fill_color.alpha)/
1814 SetPixelRed(annotate_image,fill_color.red,q);
1815 SetPixelGreen(annotate_image,fill_color.green,q);
1816 SetPixelBlue(annotate_image,fill_color.blue,q);
1817 q+=GetPixelChannels(annotate_image);
1819 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1820 if (sync == MagickFalse)
1823 annotate_view=DestroyCacheView(annotate_view);
1824 (void) CompositeImage(image,OverCompositeOp,annotate_image,
1825 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
1826 metrics->descent)-0.5));
1828 annotate_image=DestroyImage(annotate_image);
1833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1837 + R e n d e r X 1 1 %
1841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1843 % RenderX11() renders text on the image with an X11 font. It also returns the
1844 % bounding box of the text relative to the image.
1846 % The format of the RenderX11 method is:
1848 % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
1849 % const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1851 % A description of each parameter follows:
1853 % o image: the image.
1855 % o draw_info: the draw info.
1857 % o offset: (x,y) location of text relative to image.
1859 % o metrics: bounding box of text.
1861 % o exception: return any errors or warnings in this structure.
1864 #if defined(MAGICKCORE_X11_DELEGATE)
1865 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1866 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1875 *display = (Display *) NULL;
1877 static XAnnotateInfo
1886 static XResourceInfo
1892 static XStandardColormap
1902 if (annotate_semaphore == (SemaphoreInfo *) NULL)
1903 AcquireSemaphoreInfo(&annotate_semaphore);
1904 LockSemaphoreInfo(annotate_semaphore);
1905 if (display == (Display *) NULL)
1914 Open X server connection.
1916 display=XOpenDisplay(draw_info->server_name);
1917 if (display == (Display *) NULL)
1919 ThrowXWindowException(XServerError,"UnableToOpenXServer",
1920 draw_info->server_name);
1921 return(MagickFalse);
1924 Get user defaults from X resource database.
1926 (void) XSetErrorHandler(XError);
1927 image_info=AcquireImageInfo();
1928 client_name=GetClientName();
1929 resource_database=XGetResourceDatabase(display,client_name);
1930 XGetResourceInfo(image_info,resource_database,client_name,&resource_info);
1931 resource_info.close_server=MagickFalse;
1932 resource_info.colormap=PrivateColormap;
1933 resource_info.font=AcquireString(draw_info->font);
1934 resource_info.background_color=AcquireString("#ffffffffffff");
1935 resource_info.foreground_color=AcquireString("#000000000000");
1936 map_info=XAllocStandardColormap();
1937 if (map_info == (XStandardColormap *) NULL)
1939 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
1941 return(MagickFalse);
1944 Initialize visual info.
1946 visual_info=XBestVisualInfo(display,map_info,&resource_info);
1947 if (visual_info == (XVisualInfo *) NULL)
1949 ThrowXWindowException(XServerError,"UnableToGetVisual",
1951 return(MagickFalse);
1953 map_info->colormap=(Colormap) NULL;
1954 pixel.pixels=(unsigned long *) NULL;
1956 Initialize Standard Colormap info.
1958 XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen),
1960 XGetPixelInfo(display,visual_info,map_info,&resource_info,(Image *) NULL,
1962 pixel.annotate_context=XDefaultGC(display,visual_info->screen);
1964 Initialize font info.
1966 font_info=XBestFont(display,&resource_info,MagickFalse);
1967 if (font_info == (XFontStruct *) NULL)
1969 ThrowXWindowException(XServerError,"UnableToLoadFont",
1971 return(MagickFalse);
1973 if ((map_info == (XStandardColormap *) NULL) ||
1974 (visual_info == (XVisualInfo *) NULL) ||
1975 (font_info == (XFontStruct *) NULL))
1977 XFreeResources(display,visual_info,map_info,&pixel,font_info,
1978 &resource_info,(XWindowInfo *) NULL);
1979 ThrowXWindowException(XServerError,"UnableToLoadFont",
1981 return(MagickFalse);
1983 cache_info=(*draw_info);
1985 UnlockSemaphoreInfo(annotate_semaphore);
1987 Initialize annotate info.
1989 XGetAnnotateInfo(&annotate_info);
1990 annotate_info.stencil=ForegroundStencil;
1991 if (cache_info.font != draw_info->font)
1994 Type name has changed.
1996 (void) XFreeFont(display,font_info);
1997 (void) CloneString(&resource_info.font,draw_info->font);
1998 font_info=XBestFont(display,&resource_info,MagickFalse);
1999 if (font_info == (XFontStruct *) NULL)
2001 ThrowXWindowException(XServerError,"UnableToLoadFont",
2003 return(MagickFalse);
2006 if (image->debug != MagickFalse)
2007 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2008 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
2009 draw_info->font : "none",draw_info->pointsize);
2010 cache_info=(*draw_info);
2011 annotate_info.font_info=font_info;
2012 annotate_info.text=(char *) draw_info->text;
2013 annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text,
2014 (int) strlen(draw_info->text));
2015 annotate_info.height=(unsigned int) font_info->ascent+font_info->descent;
2016 metrics->pixels_per_em.x=(double) font_info->max_bounds.width;
2017 metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent;
2018 metrics->ascent=(double) font_info->ascent+4;
2019 metrics->descent=(double) (-font_info->descent);
2020 metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine);
2021 metrics->height=font_info->ascent+font_info->descent;
2022 metrics->max_advance=(double) font_info->max_bounds.width;
2023 metrics->bounds.x1=0.0;
2024 metrics->bounds.y1=metrics->descent;
2025 metrics->bounds.x2=metrics->ascent+metrics->descent;
2026 metrics->bounds.y2=metrics->ascent+metrics->descent;
2027 metrics->underline_position=(-2.0);
2028 metrics->underline_thickness=1.0;
2029 if (draw_info->render == MagickFalse)
2031 if (draw_info->fill.alpha == TransparentAlpha)
2036 width=annotate_info.width;
2037 height=annotate_info.height;
2038 if ((draw_info->affine.rx != 0.0) || (draw_info->affine.ry != 0.0))
2040 if (((draw_info->affine.sx-draw_info->affine.sy) == 0.0) &&
2041 ((draw_info->affine.rx+draw_info->affine.ry) == 0.0))
2042 annotate_info.degrees=(180.0/MagickPI)*
2043 atan2(draw_info->affine.rx,draw_info->affine.sx);
2045 (void) FormatLocaleString(annotate_info.geometry,MaxTextExtent,
2046 "%.20gx%.20g%+.20g%+.20g",(double) width,(double) height,
2047 ceil(offset->x-0.5),ceil(offset->y-metrics->ascent-metrics->descent+
2048 draw_info->interline_spacing-0.5));
2049 pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red);
2050 pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green);
2051 pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue);
2052 status=XAnnotateImage(display,&pixel,&annotate_info,image);
2055 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
2057 return(MagickFalse);
2062 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
2063 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2068 (void) ThrowMagickException(exception,GetMagickModule(),
2069 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (X11)",
2071 return(MagickFalse);