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-2009 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 "magick/studio.h"
46 #include "magick/annotate.h"
47 #include "magick/cache-view.h"
48 #include "magick/client.h"
49 #include "magick/color.h"
50 #include "magick/color-private.h"
51 #include "magick/composite.h"
52 #include "magick/composite-private.h"
53 #include "magick/constitute.h"
54 #include "magick/draw.h"
55 #include "magick/draw-private.h"
56 #include "magick/exception.h"
57 #include "magick/exception-private.h"
58 #include "magick/gem.h"
59 #include "magick/geometry.h"
60 #include "magick/image-private.h"
61 #include "magick/log.h"
62 #include "magick/quantum.h"
63 #include "magick/quantum-private.h"
64 #include "magick/property.h"
65 #include "magick/resource_.h"
66 #include "magick/statistic.h"
67 #include "magick/string_.h"
68 #include "magick/token-private.h"
69 #include "magick/transform.h"
70 #include "magick/type.h"
71 #include "magick/utility.h"
72 #include "magick/xwindow-private.h"
73 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
74 #if defined(__MINGW32__)
77 #if defined(MAGICKCORE_HAVE_FT2BUILD_H)
78 # include <ft2build.h>
80 #if defined(FT_FREETYPE_H)
81 # include FT_FREETYPE_H
83 # include <freetype/freetype.h>
85 #if defined(FT_GLYPH_H)
88 # include <freetype/ftglyph.h>
90 #if defined(FT_OUTLINE_H)
91 # include FT_OUTLINE_H
93 # include <freetype/ftoutln.h>
95 #if defined(FT_BBOX_H)
98 # include <freetype/ftbbox.h>
99 #endif /* defined(FT_BBOX_H) */
103 Forward declarations.
105 static MagickBooleanType
106 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *),
107 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *),
108 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
110 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *);
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % A n n o t a t e I m a g e %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % AnnotateImage() annotates an image with text. Optionally you can include
124 % any of the following bits of information about the image by embedding
125 % the appropriate special characters:
127 % %b file size in bytes.
129 % %d directory in which the image resides.
130 % %e extension of the image file.
131 % %f original filename of the image.
132 % %h height of image.
133 % %i filename of the image.
134 % %k number of unique colors.
136 % %m image file format.
137 % %n number of images in a image sequence.
138 % %o output image filename.
139 % %p page number of the image.
140 % %q image depth (8 or 16).
141 % %q image depth (8 or 16).
142 % %s image scene number.
143 % %t image filename without any extension.
144 % %u a unique temporary filename.
146 % %x x resolution of the image.
147 % %y y resolution of the image.
149 % The format of the AnnotateImage method is:
151 % MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info)
153 % A description of each parameter follows:
155 % o image: the image.
157 % o draw_info: the draw info.
160 MagickExport MagickBooleanType AnnotateImage(Image *image,
161 const DrawInfo *draw_info)
164 primitive[MaxTextExtent],
196 assert(image != (Image *) NULL);
197 assert(image->signature == MagickSignature);
198 if (image->debug != MagickFalse)
199 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
200 assert(draw_info != (DrawInfo *) NULL);
201 assert(draw_info->signature == MagickSignature);
202 if (draw_info->text == (char *) NULL)
204 if (*draw_info->text == '\0')
206 textlist=StringToList(draw_info->text);
207 if (textlist == (char **) NULL)
209 length=strlen(textlist[0]);
210 for (i=1; textlist[i] != (char *) NULL; i++)
211 if (strlen(textlist[i]) > length)
212 length=strlen(textlist[i]);
213 number_lines=(unsigned long) i;
214 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
215 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
216 SetGeometry(image,&geometry);
217 SetGeometryInfo(&geometry_info);
218 if (annotate_info->geometry != (char *) NULL)
220 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
222 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
224 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
227 for (i=0; textlist[i] != (char *) NULL; i++)
230 Position text relative to image.
232 annotate_info->affine.tx=geometry_info.xi-image->page.x;
233 annotate_info->affine.ty=geometry_info.psi-image->page.y;
234 (void) CloneString(&annotate->text,textlist[i]);
235 (void) GetTypeMetrics(image,annotate,&metrics);
236 height=(long) (metrics.ascent-metrics.descent+
237 draw_info->interline_spacing+0.5);
238 switch (annotate->gravity)
240 case UndefinedGravity:
243 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
244 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
247 case NorthWestGravity:
249 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
250 annotate_info->affine.ry*height+annotate_info->affine.ry*
251 (metrics.ascent+metrics.descent);
252 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
253 annotate_info->affine.sy*height+annotate_info->affine.sy*
259 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
260 geometry.width/2.0+i*annotate_info->affine.ry*height-
261 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
262 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
263 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
264 annotate_info->affine.sy*height+annotate_info->affine.sy*
265 metrics.ascent-annotate_info->affine.rx*(metrics.width-
266 metrics.bounds.x1)/2.0;
269 case NorthEastGravity:
271 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
272 geometry.width+i*annotate_info->affine.ry*height-
273 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
274 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
275 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
276 annotate_info->affine.sy*height+annotate_info->affine.sy*
277 metrics.ascent-annotate_info->affine.rx*(metrics.width-
283 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
284 annotate_info->affine.ry*height+annotate_info->affine.ry*
285 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
286 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
287 geometry.height/2.0+i*annotate_info->affine.sy*height+
288 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
289 (number_lines-1.0)*height)/2.0;
295 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
296 geometry.width/2.0+i*annotate_info->affine.ry*height-
297 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
298 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
299 (number_lines-1)*height)/2.0;
300 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
301 geometry.height/2.0+i*annotate_info->affine.sy*height-
302 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
303 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
304 (number_lines-1.0)*height)/2.0;
309 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
310 geometry.width+i*annotate_info->affine.ry*height-
311 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
312 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
313 (number_lines-1.0)*height)/2.0-1.0;
314 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
315 geometry.height/2.0+i*annotate_info->affine.sy*height-
316 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+
317 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
318 (number_lines-1.0)*height)/2.0;
321 case SouthWestGravity:
323 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
324 annotate_info->affine.ry*height-annotate_info->affine.ry*
325 (number_lines-1.0)*height;
326 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
327 geometry.height+i*annotate_info->affine.sy*height-
328 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
333 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
334 geometry.width/2.0+i*annotate_info->affine.ry*height-
335 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
336 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
337 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
338 geometry.height+i*annotate_info->affine.sy*height-
339 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
340 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
343 case SouthEastGravity:
345 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
346 geometry.width+i*annotate_info->affine.ry*height-
347 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
348 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
349 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
350 geometry.height+i*annotate_info->affine.sy*height-
351 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
352 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
356 switch (annotate->align)
360 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
361 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
366 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
367 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
368 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
369 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
374 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
375 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
376 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
377 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
383 if (draw_info->undercolor.opacity != TransparentOpacity)
391 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
392 undercolor_info->fill=draw_info->undercolor;
393 undercolor_info->affine=draw_info->affine;
394 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
395 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
396 (void) FormatMagickString(primitive,MaxTextExtent,
397 "rectangle 0,0 %g,%lu",metrics.origin.x,height);
398 (void) CloneString(&undercolor_info->primitive,primitive);
399 (void) DrawImage(image,undercolor_info);
400 (void) DestroyDrawInfo(undercolor_info);
402 annotate_info->affine.tx=offset.x;
403 annotate_info->affine.ty=offset.y;
404 (void) FormatMagickString(primitive,MaxTextExtent,"stroke-width %g "
405 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
406 if (annotate->decorate == OverlineDecoration)
408 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
409 metrics.descent-metrics.underline_position));
410 (void) CloneString(&annotate_info->primitive,primitive);
411 (void) DrawImage(image,annotate_info);
414 if (annotate->decorate == UnderlineDecoration)
416 annotate_info->affine.ty-=(draw_info->affine.sy*
417 metrics.underline_position);
418 (void) CloneString(&annotate_info->primitive,primitive);
419 (void) DrawImage(image,annotate_info);
422 Annotate image with text.
424 status=RenderType(image,annotate,&offset,&metrics);
425 if (status == MagickFalse)
427 if (annotate->decorate == LineThroughDecoration)
429 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
430 metrics.underline_position+metrics.descent)/2.0);
431 (void) CloneString(&annotate_info->primitive,primitive);
432 (void) DrawImage(image,annotate_info);
436 Relinquish resources.
438 annotate_info=DestroyDrawInfo(annotate_info);
439 annotate=DestroyDrawInfo(annotate);
440 for (i=0; textlist[i] != (char *) NULL; i++)
441 textlist[i]=DestroyString(textlist[i]);
442 textlist=(char **) RelinquishMagickMemory(textlist);
447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 % F o r m a t M a g i c k C a p t i o n %
455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 % FormatMagickCaption() formats a caption so that it fits within the image
458 % width. It returns the number of lines in the formatted caption.
460 % The format of the FormatMagickCaption method is:
462 % long FormatMagickCaption(Image *image,DrawInfo *draw_info,
463 % TypeMetric *metrics,char **caption)
465 % A description of each parameter follows.
467 % o image: The image.
469 % o caption: the caption.
471 % o draw_info: the draw info.
473 % o metrics: Return the font metrics in this structure.
476 MagickExport long FormatMagickCaption(Image *image,DrawInfo *draw_info,
477 TypeMetric *metrics,char **caption)
495 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
497 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
499 for (i=0; i < (long) GetUTFOctets(p); i++)
502 status=GetTypeMetrics(image,draw_info,metrics);
503 if (status == MagickFalse)
505 width=(unsigned long) (metrics->width+0.5);
506 if (GetUTFCode(p) != '\n')
507 if (width <= image->columns)
509 if (s == (char *) NULL)
512 while ((IsUTFSpace(GetUTFCode(s)) == MagickFalse) &&
513 (GetUTFCode(s) != 0))
516 if (GetUTFCode(s) != 0)
530 No convenient line breaks-- insert newline.
532 target=AcquireString(*caption);
534 CopyMagickString(target,*caption,n+1);
535 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
536 ConcatenateMagickString(target,p,strlen(*caption)+2);
537 (void) DestroyString(*caption);
545 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
546 if (GetUTFCode(p) == '\n')
552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 % G e t M u l t i l i n e T y p e M e t r i c s %
560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 % GetMultilineTypeMetrics() returns the following information for the
563 % specified font and text:
571 % maximum horizontal advance
579 % underline thickness
581 % This method is like GetTypeMetrics() but it returns the maximum text width
582 % and height for multiple lines of text.
584 % The format of the GetMultilineTypeMetrics method is:
586 % MagickBooleanType GetMultilineTypeMetrics(Image *image,
587 % const DrawInfo *draw_info,TypeMetric *metrics)
589 % A description of each parameter follows:
591 % o image: the image.
593 % o draw_info: the draw info.
595 % o metrics: Return the font metrics in this structure.
598 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
599 const DrawInfo *draw_info,TypeMetric *metrics)
616 assert(image != (Image *) NULL);
617 assert(image->signature == MagickSignature);
618 if (image->debug != MagickFalse)
619 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
620 assert(draw_info != (DrawInfo *) NULL);
621 assert(draw_info->text != (char *) NULL);
622 assert(draw_info->signature == MagickSignature);
623 if (*draw_info->text == '\0')
625 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
626 annotate_info->text=DestroyString(annotate_info->text);
628 Convert newlines to multiple lines of text.
630 textlist=StringToList(draw_info->text);
631 if (textlist == (char **) NULL)
633 annotate_info->render=MagickFalse;
634 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
635 (void) ResetMagickMemory(&extent,0,sizeof(extent));
637 Find the widest of the text lines.
639 annotate_info->text=textlist[0];
640 status=GetTypeMetrics(image,annotate_info,&extent);
642 for (i=1; textlist[i] != (char *) NULL; i++)
644 annotate_info->text=textlist[i];
645 status=GetTypeMetrics(image,annotate_info,&extent);
646 if (extent.width > metrics->width)
649 metrics->height=(double) (i*(unsigned long) (metrics->ascent-
650 metrics->descent+0.5)+(i-1)*draw_info->interline_spacing);
652 Relinquish resources.
654 annotate_info->text=(char *) NULL;
655 annotate_info=DestroyDrawInfo(annotate_info);
656 for (i=0; textlist[i] != (char *) NULL; i++)
657 textlist[i]=DestroyString(textlist[i]);
658 textlist=(char **) RelinquishMagickMemory(textlist);
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667 % G e t T y p e M e t r i c s %
671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
673 % GetTypeMetrics() returns the following information for the specified font
682 % maximum horizontal advance
690 % underline thickness
692 % The format of the GetTypeMetrics method is:
694 % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
695 % TypeMetric *metrics)
697 % A description of each parameter follows:
699 % o image: the image.
701 % o draw_info: the draw info.
703 % o metrics: Return the font metrics in this structure.
706 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
707 const DrawInfo *draw_info,TypeMetric *metrics)
718 assert(image != (Image *) NULL);
719 assert(image->signature == MagickSignature);
720 if (image->debug != MagickFalse)
721 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
722 assert(draw_info != (DrawInfo *) NULL);
723 assert(draw_info->text != (char *) NULL);
724 assert(draw_info->signature == MagickSignature);
725 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
726 annotate_info->render=MagickFalse;
727 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
730 status=RenderType(image,annotate_info,&offset,metrics);
731 if (image->debug != MagickFalse)
732 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
733 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
734 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
735 "underline position: %g; underline thickness: %g",annotate_info->text,
736 metrics->width,metrics->height,metrics->ascent,metrics->descent,
737 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
738 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
739 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
740 metrics->underline_position,metrics->underline_thickness);
741 annotate_info=DestroyDrawInfo(annotate_info);
746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
750 + R e n d e r T y p e %
754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 % RenderType() renders text on the image. It also returns the bounding box of
757 % the text relative to the image.
759 % The format of the RenderType method is:
761 % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
762 % const PointInfo *offset,TypeMetric *metrics)
764 % A description of each parameter follows:
766 % o image: the image.
768 % o draw_info: the draw info.
770 % o offset: (x,y) location of text relative to image.
772 % o metrics: bounding box of text.
775 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
776 const PointInfo *offset,TypeMetric *metrics)
787 type_info=(const TypeInfo *) NULL;
788 if (draw_info->font != (char *) NULL)
790 if (*draw_info->font == '@')
792 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
796 if (*draw_info->font == '-')
797 return(RenderX11(image,draw_info,offset,metrics));
798 if (IsPathAccessible(draw_info->font) != MagickFalse)
800 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
804 type_info=GetTypeInfo(draw_info->font,&image->exception);
805 if (type_info == (const TypeInfo *) NULL)
806 (void) ThrowMagickException(&image->exception,GetMagickModule(),
807 TypeWarning,"UnableToReadFont","`%s'",draw_info->font);
809 if ((type_info == (const TypeInfo *) NULL) &&
810 (draw_info->family != (const char *) NULL))
812 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
813 draw_info->stretch,draw_info->weight,&image->exception);
814 if (type_info == (const TypeInfo *) NULL)
815 (void) ThrowMagickException(&image->exception,GetMagickModule(),
816 TypeWarning,"UnableToReadFont","`%s'",draw_info->family);
818 if (type_info == (const TypeInfo *) NULL)
819 type_info=GetTypeInfoByFamily("arial",draw_info->style,
820 draw_info->stretch,draw_info->weight,&image->exception);
821 if (type_info == (const TypeInfo *) NULL)
822 type_info=GetTypeInfoByFamily("helvetica",draw_info->style,
823 draw_info->stretch,draw_info->weight,&image->exception);
824 if (type_info == (const TypeInfo *) NULL)
825 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
826 draw_info->stretch,draw_info->weight,&image->exception);
827 if (type_info == (const TypeInfo *) NULL)
829 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics);
832 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
833 annotate_info->face=type_info->face;
834 if (type_info->metrics != (char *) NULL)
835 (void) CloneString(&annotate_info->metrics,type_info->metrics);
836 if (type_info->glyphs != (char *) NULL)
837 (void) CloneString(&annotate_info->font,type_info->glyphs);
838 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics);
839 annotate_info=DestroyDrawInfo(annotate_info);
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848 + R e n d e r F r e e t y p e %
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % RenderFreetype() renders text on the image with a Truetype font. It also
855 % returns the bounding box of the text relative to the image.
857 % The format of the RenderFreetype method is:
859 % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
860 % const char *encoding,const PointInfo *offset,TypeMetric *metrics)
862 % A description of each parameter follows:
864 % o image: the image.
866 % o draw_info: the draw info.
868 % o encoding: the font encoding.
870 % o offset: (x,y) location of text relative to image.
872 % o metrics: bounding box of text.
876 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
877 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
886 affine=draw_info->affine;
887 (void) FormatMagickString(path,MaxTextExtent,"C%g,%g %g,%g %g,%g",
888 affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,
889 affine.ty-q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0);
890 (void) ConcatenateString(&draw_info->primitive,path);
894 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
902 affine=draw_info->affine;
903 (void) FormatMagickString(path,MaxTextExtent,"L%g,%g",affine.tx+to->x/64.0,
904 affine.ty-to->y/64.0);
905 (void) ConcatenateString(&draw_info->primitive,path);
909 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
917 affine=draw_info->affine;
918 (void) FormatMagickString(path,MaxTextExtent,"M%g,%g",affine.tx+to->x/64.0,
919 affine.ty-to->y/64.0);
920 (void) ConcatenateString(&draw_info->primitive,path);
924 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
933 affine=draw_info->affine;
934 (void) FormatMagickString(path,MaxTextExtent,"Q%g,%g %g,%g",
935 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
936 affine.ty-to->y/64.0);
937 (void) ConcatenateString(&draw_info->primitive,path);
941 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
942 const char *encoding,const PointInfo *offset,TypeMetric *metrics)
944 #if !defined(FT_OPEN_PATHNAME)
945 #define FT_OPEN_PATHNAME ft_open_pathname
948 typedef struct _GlyphInfo
1011 static FT_Outline_Funcs
1014 (FT_Outline_MoveTo_Func) TraceMoveTo,
1015 (FT_Outline_LineTo_Func) TraceLineTo,
1016 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1017 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1022 Initialize Truetype library.
1024 status=FT_Init_FreeType(&library);
1026 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1028 args.flags=FT_OPEN_PATHNAME;
1029 if (draw_info->font == (char *) NULL)
1030 args.pathname=ConstantString("helvetica");
1032 if (*draw_info->font != '@')
1033 args.pathname=ConstantString(draw_info->font);
1035 args.pathname=ConstantString(draw_info->font+1);
1036 face=(FT_Face) NULL;
1037 status=FT_Open_Face(library,&args,draw_info->face,&face);
1038 args.pathname=DestroyString(args.pathname);
1041 (void) FT_Done_FreeType(library);
1042 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1043 TypeError,"UnableToReadFont","`%s'",draw_info->font);
1044 return(RenderPostscript(image,draw_info,offset,metrics));
1046 if ((draw_info->metrics != (char *) NULL) &&
1047 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1048 (void) FT_Attach_File(face,draw_info->metrics);
1049 encoding_type=ft_encoding_unicode;
1050 status=FT_Select_Charmap(face,encoding_type);
1051 if ((status != 0) && (face->num_charmaps != 0))
1052 status=FT_Set_Charmap(face,face->charmaps[0]);
1053 if (encoding != (const char *) NULL)
1055 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1056 encoding_type=ft_encoding_adobe_custom;
1057 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1058 encoding_type=ft_encoding_adobe_expert;
1059 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1060 encoding_type=ft_encoding_adobe_standard;
1061 if (LocaleCompare(encoding,"AppleRoman") == 0)
1062 encoding_type=ft_encoding_apple_roman;
1063 if (LocaleCompare(encoding,"BIG5") == 0)
1064 encoding_type=ft_encoding_big5;
1065 if (LocaleCompare(encoding,"GB2312") == 0)
1066 encoding_type=ft_encoding_gb2312;
1067 if (LocaleCompare(encoding,"Johab") == 0)
1068 encoding_type=ft_encoding_johab;
1069 #if defined(ft_encoding_latin_1)
1070 if (LocaleCompare(encoding,"Latin-1") == 0)
1071 encoding_type=ft_encoding_latin_1;
1073 if (LocaleCompare(encoding,"Latin-2") == 0)
1074 encoding_type=ft_encoding_latin_2;
1075 if (LocaleCompare(encoding,"None") == 0)
1076 encoding_type=ft_encoding_none;
1077 if (LocaleCompare(encoding,"SJIScode") == 0)
1078 encoding_type=ft_encoding_sjis;
1079 if (LocaleCompare(encoding,"Symbol") == 0)
1080 encoding_type=ft_encoding_symbol;
1081 if (LocaleCompare(encoding,"Unicode") == 0)
1082 encoding_type=ft_encoding_unicode;
1083 if (LocaleCompare(encoding,"Wansung") == 0)
1084 encoding_type=ft_encoding_wansung;
1085 status=FT_Select_Charmap(face,encoding_type);
1087 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1092 resolution.x=DefaultResolution;
1093 resolution.y=DefaultResolution;
1094 if (draw_info->density != (char *) NULL)
1102 flags=ParseGeometry(draw_info->density,&geometry_info);
1103 resolution.x=geometry_info.rho;
1104 resolution.y=geometry_info.sigma;
1105 if ((flags & SigmaValue) == 0)
1106 resolution.y=resolution.x;
1108 status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1109 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1110 (FT_UInt) resolution.y);
1111 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1112 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1113 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1114 metrics->descent=(double) face->size->metrics.descender/64.0;
1116 metrics->origin.x=0;
1117 metrics->origin.y=0;
1118 metrics->height=(double) face->size->metrics.height/64.0;
1119 metrics->max_advance=0.0;
1120 if (face->size->metrics.max_advance > MagickEpsilon)
1121 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1122 metrics->bounds.x1=0.0;
1123 metrics->bounds.y1=metrics->descent;
1124 metrics->bounds.x2=metrics->ascent+metrics->descent;
1125 metrics->bounds.y2=metrics->ascent+metrics->descent;
1126 metrics->underline_position=face->underline_position/64.0;
1127 metrics->underline_thickness=face->underline_thickness/64.0;
1128 if (*draw_info->text == '\0')
1130 (void) FT_Done_Face(face);
1131 (void) FT_Done_FreeType(library);
1135 Compute bounding box.
1137 if (image->debug != MagickFalse)
1138 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1139 "font-encoding %s; text-encoding %s; pointsize %g",
1140 draw_info->font != (char *) NULL ? draw_info->font : "none",
1141 encoding != (char *) NULL ? encoding : "none",
1142 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1143 draw_info->pointsize);
1144 flags=FT_LOAD_NO_BITMAP;
1145 value=GetImageProperty(image,"type:hinting");
1146 if (LocaleCompare(value,"off") == 0)
1147 flags|=FT_LOAD_NO_HINTING;
1151 last_glyph.image=NULL;
1158 if (draw_info->render != MagickFalse)
1160 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1161 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1162 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1163 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1165 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1166 (void) CloneString(&annotate_info->primitive,"path '");
1167 if (draw_info->render != MagickFalse)
1169 if (image->storage_class != DirectClass)
1170 (void) SetImageStorageClass(image,DirectClass);
1171 if (image->matte == MagickFalse)
1172 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1177 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1179 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1181 glyph.id=FT_Get_Char_Index(face,'?');
1182 if ((glyph.id != 0) && (last_glyph.id != 0))
1184 if (draw_info->kerning != 0.0)
1185 origin.x+=64.0*draw_info->kerning;
1187 if (FT_HAS_KERNING(face))
1192 status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1193 ft_kerning_default,&kerning);
1195 origin.x+=kerning.x;
1198 glyph.origin=origin;
1199 status=FT_Load_Glyph(face,glyph.id,flags);
1202 status=FT_Get_Glyph(face->glyph,&glyph.image);
1205 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1209 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
1210 metrics->bounds.x1=bounds.xMin;
1211 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
1212 metrics->bounds.y1=bounds.yMin;
1213 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
1214 metrics->bounds.x2=bounds.xMax;
1215 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
1216 metrics->bounds.y2=bounds.yMax;
1217 if (draw_info->render != MagickFalse)
1218 if ((draw_info->stroke.opacity != TransparentOpacity) ||
1219 (draw_info->stroke_pattern != (Image *) NULL))
1224 annotate_info->affine.tx=glyph.origin.x/64.0;
1225 annotate_info->affine.ty=glyph.origin.y/64.0;
1226 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->outline,
1227 &OutlineMethods,annotate_info);
1229 FT_Vector_Transform(&glyph.origin,&affine);
1230 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1231 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1232 (FT_Vector *) NULL,MagickTrue);
1235 bitmap=(FT_BitmapGlyph) glyph.image;
1236 point.x=offset->x+bitmap->left;
1237 point.y=offset->y-bitmap->top;
1238 if (draw_info->render != MagickFalse)
1250 Rasterize the glyph.
1253 exception=(&image->exception);
1254 image_view=AcquireCacheView(image);
1255 for (y=0; y < (long) bitmap->bitmap.rows; y++)
1274 register PixelPacket
1277 register unsigned char
1280 if (status == MagickFalse)
1282 x_offset=(long) (point.x+0.5);
1283 y_offset=(long) (point.y+y+0.5);
1284 if ((y_offset < 0) || (y_offset >= (long) image->rows))
1286 q=(PixelPacket *) NULL;
1287 if ((x_offset < 0) || (x_offset >= (long) image->columns))
1291 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1292 bitmap->bitmap.width,1,exception);
1293 active=q != (PixelPacket *) NULL ? MagickTrue : MagickFalse;
1295 p=bitmap->bitmap.buffer+y*bitmap->bitmap.width;
1296 for (x=0; x < (long) bitmap->bitmap.width; x++)
1299 if ((*p == 0) || (x_offset < 0) ||
1300 (x_offset >= (long) image->columns))
1306 fill_opacity=(MagickRealType) (*p)/(bitmap->bitmap.num_grays-1);
1307 if (draw_info->text_antialias == MagickFalse)
1308 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1309 if (active == MagickFalse)
1310 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1312 if (q == (PixelPacket *) NULL)
1318 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
1319 fill_opacity=QuantumRange-fill_opacity*(QuantumRange-
1320 fill_color.opacity);
1321 MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q);
1322 if (active == MagickFalse)
1324 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1325 if (sync == MagickFalse)
1331 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1332 if (sync == MagickFalse)
1335 image_view=DestroyCacheView(image_view);
1337 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1338 metrics->width=bitmap->left+bitmap->bitmap.width;
1339 if ((draw_info->interword_spacing != 0.0) &&
1340 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1341 (IsUTFSpace(code) == MagickFalse))
1342 origin.x+=64.0*draw_info->interword_spacing;
1344 origin.x+=face->glyph->advance.x;
1345 metrics->origin.x=origin.x;
1346 metrics->origin.y=origin.y;
1347 if (last_glyph.id != 0)
1348 FT_Done_Glyph(last_glyph.image);
1352 if (last_glyph.id != 0)
1353 FT_Done_Glyph(last_glyph.image);
1354 if ((draw_info->stroke.opacity != TransparentOpacity) ||
1355 (draw_info->stroke_pattern != (Image *) NULL))
1357 if (draw_info->render != MagickFalse)
1362 annotate_info->linejoin=RoundJoin;
1363 annotate_info->affine.tx=offset->x;
1364 annotate_info->affine.ty=offset->y;
1365 (void) ConcatenateString(&annotate_info->primitive,"'");
1366 (void) DrawImage(image,annotate_info);
1370 Determine font metrics.
1372 glyph.id=FT_Get_Char_Index(face,'_');
1373 glyph.origin=origin;
1374 status=FT_Load_Glyph(face,glyph.id,flags);
1377 status=FT_Get_Glyph(face->glyph,&glyph.image);
1380 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1384 FT_Vector_Transform(&glyph.origin,&affine);
1385 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1386 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1387 (FT_Vector *) NULL,MagickTrue);
1388 bitmap=(FT_BitmapGlyph) glyph.image;
1389 if (bitmap->left > metrics->width)
1390 metrics->width=bitmap->left;
1394 FT_Done_Glyph(glyph.image);
1396 metrics->width-=metrics->bounds.x1/64.0;
1397 metrics->bounds.x1/=64.0;
1398 metrics->bounds.y1/=64.0;
1399 metrics->bounds.x2/=64.0;
1400 metrics->bounds.y2/=64.0;
1401 metrics->origin.x/=64.0;
1402 metrics->origin.y/=64.0;
1404 Relinquish resources.
1406 annotate_info=DestroyDrawInfo(annotate_info);
1407 (void) FT_Done_Face(face);
1408 (void) FT_Done_FreeType(library);
1412 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1413 const char *magick_unused(encoding),const PointInfo *offset,
1414 TypeMetric *metrics)
1416 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1417 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (Freetype)",
1419 return(RenderPostscript(image,draw_info,offset,metrics));
1424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428 + R e n d e r P o s t s c r i p t %
1432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1434 % RenderPostscript() renders text on the image with a Postscript font. It
1435 % also returns the bounding box of the text relative to the image.
1437 % The format of the RenderPostscript method is:
1439 % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1440 % const PointInfo *offset,TypeMetric *metrics)
1442 % A description of each parameter follows:
1444 % o image: the image.
1446 % o draw_info: the draw info.
1448 % o offset: (x,y) location of text relative to image.
1450 % o metrics: bounding box of text.
1454 static inline size_t MagickMin(const size_t x,const size_t y)
1461 static char *EscapeParenthesis(const char *text)
1476 buffer=AcquireString(text);
1478 for (i=0; i < (long) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
1480 if ((text[i] == '(') || (text[i] == ')'))
1491 static MagickBooleanType RenderPostscript(Image *image,
1492 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics)
1495 filename[MaxTextExtent],
1496 geometry[MaxTextExtent],
1526 Render label with a Postscript font.
1528 if (image->debug != MagickFalse)
1529 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1530 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1531 draw_info->font : "none",draw_info->pointsize);
1533 unique_file=AcquireUniqueFileResource(filename);
1534 if (unique_file != -1)
1535 file=fdopen(unique_file,"wb");
1536 if ((unique_file == -1) || (file == (FILE *) NULL))
1538 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1540 return(MagickFalse);
1542 (void) fprintf(file,"%%!PS-Adobe-3.0\n");
1543 (void) fprintf(file,"/ReencodeType\n");
1544 (void) fprintf(file,"{\n");
1545 (void) fprintf(file," findfont dup length\n");
1546 (void) fprintf(file,
1547 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
1548 (void) fprintf(file,
1549 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
1550 (void) fprintf(file,"} bind def\n");
1552 Sample to compute bounding box.
1554 identity=(draw_info->affine.sx == draw_info->affine.sy) &&
1555 (draw_info->affine.rx == 0.0) && (draw_info->affine.ry == 0.0) ?
1556 MagickTrue : MagickFalse;
1559 for (i=0; i <= (long) (strlen(draw_info->text)+2); i++)
1561 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1562 draw_info->affine.ry*2.0*draw_info->pointsize);
1563 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1564 draw_info->affine.sy*2.0*draw_info->pointsize);
1565 if (point.x > extent.x)
1567 if (point.y > extent.y)
1570 (void) fprintf(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
1571 extent.x/2.0,extent.y/2.0);
1572 (void) fprintf(file,"%g %g scale\n",draw_info->pointsize,
1573 draw_info->pointsize);
1574 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1575 (strchr(draw_info->font,'/') != (char *) NULL))
1576 (void) fprintf(file,
1577 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1579 (void) fprintf(file,"/%s-ISO dup /%s ReencodeType findfont setfont\n",
1580 draw_info->font,draw_info->font);
1581 (void) fprintf(file,"[%g %g %g %g 0 0] concat\n",draw_info->affine.sx,
1582 -draw_info->affine.rx,-draw_info->affine.ry,draw_info->affine.sy);
1583 text=EscapeParenthesis(draw_info->text);
1584 if (identity == MagickFalse)
1585 (void) fprintf(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",text);
1586 (void) fprintf(file,"(%s) show\n",text);
1587 text=DestroyString(text);
1588 (void) fprintf(file,"showpage\n");
1589 (void) fclose(file);
1590 (void) FormatMagickString(geometry,MaxTextExtent,"%ldx%ld+0+0!",(long)
1591 (extent.x+0.5),(long) (extent.y+0.5));
1592 annotate_info=AcquireImageInfo();
1593 (void) FormatMagickString(annotate_info->filename,MaxTextExtent,"ps:%s",
1595 (void) CloneString(&annotate_info->page,geometry);
1596 if (draw_info->density != (char *) NULL)
1597 (void) CloneString(&annotate_info->density,draw_info->density);
1598 annotate_info->antialias=draw_info->text_antialias;
1599 annotate_image=ReadImage(annotate_info,&image->exception);
1600 CatchException(&image->exception);
1601 annotate_info=DestroyImageInfo(annotate_info);
1602 (void) RelinquishUniqueFileResource(filename);
1603 if (annotate_image == (Image *) NULL)
1604 return(MagickFalse);
1605 resolution.x=DefaultResolution;
1606 resolution.y=DefaultResolution;
1607 if (draw_info->density != (char *) NULL)
1615 flags=ParseGeometry(draw_info->density,&geometry_info);
1616 resolution.x=geometry_info.rho;
1617 resolution.y=geometry_info.sigma;
1618 if ((flags & SigmaValue) == 0)
1619 resolution.y=resolution.x;
1621 if (identity == MagickFalse)
1622 (void) TransformImage(&annotate_image,"0x0",(char *) NULL);
1628 crop_info=GetImageBoundingBox(annotate_image,&annotate_image->exception);
1629 crop_info.height=(unsigned long) ((resolution.y/DefaultResolution)*
1630 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
1631 crop_info.y=(long) ((resolution.y/DefaultResolution)*extent.y/8.0+0.5);
1632 (void) FormatMagickString(geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
1633 crop_info.width,crop_info.height,crop_info.x,crop_info.y);
1634 (void) TransformImage(&annotate_image,geometry,(char *) NULL);
1636 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1637 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1638 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1639 metrics->ascent=metrics->pixels_per_em.x;
1640 metrics->descent=metrics->pixels_per_em.y/-5.0;
1641 metrics->width=(double) annotate_image->columns/
1642 ExpandAffine(&draw_info->affine);
1643 metrics->height=1.152*metrics->pixels_per_em.x;
1644 metrics->max_advance=metrics->pixels_per_em.x;
1645 metrics->bounds.x1=0.0;
1646 metrics->bounds.y1=metrics->descent;
1647 metrics->bounds.x2=metrics->ascent+metrics->descent;
1648 metrics->bounds.y2=metrics->ascent+metrics->descent;
1649 metrics->underline_position=(-2.0);
1650 metrics->underline_thickness=1.0;
1651 if (draw_info->render == MagickFalse)
1653 annotate_image=DestroyImage(annotate_image);
1656 if (draw_info->fill.opacity != TransparentOpacity)
1673 if (image->matte == MagickFalse)
1674 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1675 if (annotate_image->matte == MagickFalse)
1676 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel);
1677 fill_color=draw_info->fill;
1678 exception=(&image->exception);
1679 annotate_view=AcquireCacheView(annotate_image);
1680 for (y=0; y < (long) annotate_image->rows; y++)
1685 register PixelPacket
1688 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1690 if (q == (PixelPacket *) NULL)
1692 for (x=0; x < (long) annotate_image->columns; x++)
1694 (void) GetFillColor(draw_info,x,y,&fill_color);
1695 q->opacity=RoundToQuantum(QuantumRange-(((QuantumRange-
1696 (MagickRealType) PixelIntensityToQuantum(q))*(QuantumRange-
1697 fill_color.opacity))/QuantumRange));
1698 q->red=fill_color.red;
1699 q->green=fill_color.green;
1700 q->blue=fill_color.blue;
1703 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1704 if (sync == MagickFalse)
1707 annotate_view=DestroyCacheView(annotate_view);
1708 (void) CompositeImage(image,OverCompositeOp,annotate_image,
1709 (long) (offset->x+0.5),(long) (offset->y-(metrics->ascent+
1710 metrics->descent)+0.5));
1712 annotate_image=DestroyImage(annotate_image);
1717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721 + R e n d e r X 1 1 %
1725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727 % RenderX11() renders text on the image with an X11 font. It also returns the
1728 % bounding box of the text relative to the image.
1730 % The format of the RenderX11 method is:
1732 % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
1733 % const PointInfo *offset,TypeMetric *metrics)
1735 % A description of each parameter follows:
1737 % o image: the image.
1739 % o draw_info: the draw info.
1741 % o offset: (x,y) location of text relative to image.
1743 % o metrics: bounding box of text.
1746 #if defined(MAGICKCORE_X11_DELEGATE)
1747 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1748 const PointInfo *offset,TypeMetric *metrics)
1757 *display = (Display *) NULL;
1759 static XAnnotateInfo
1768 static XResourceInfo
1774 static XStandardColormap
1784 if (display == (Display *) NULL)
1790 Open X server connection.
1792 display=XOpenDisplay(draw_info->server_name);
1793 if (display == (Display *) NULL)
1795 ThrowXWindowException(XServerError,"UnableToOpenXServer",
1796 draw_info->server_name);
1797 return(MagickFalse);
1800 Get user defaults from X resource database.
1802 (void) XSetErrorHandler(XError);
1803 image_info=AcquireImageInfo();
1804 resource_database=XGetResourceDatabase(display,GetClientName());
1805 XGetResourceInfo(image_info,resource_database,GetClientName(),
1807 resource_info.close_server=MagickFalse;
1808 resource_info.colormap=PrivateColormap;
1809 resource_info.font=AcquireString(draw_info->font);
1810 resource_info.background_color=AcquireString("#ffffffffffff");
1811 resource_info.foreground_color=AcquireString("#000000000000");
1812 map_info=XAllocStandardColormap();
1813 if (map_info == (XStandardColormap *) NULL)
1815 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
1817 return(MagickFalse);
1820 Initialize visual info.
1822 visual_info=XBestVisualInfo(display,map_info,&resource_info);
1823 if (visual_info == (XVisualInfo *) NULL)
1825 ThrowXWindowException(XServerError,"UnableToGetVisual",
1827 return(MagickFalse);
1829 map_info->colormap=(Colormap) NULL;
1830 pixel.pixels=(unsigned long *) NULL;
1832 Initialize Standard Colormap info.
1834 XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen),
1836 XGetPixelPacket(display,visual_info,map_info,&resource_info,
1837 (Image *) NULL,&pixel);
1838 pixel.annotate_context=XDefaultGC(display,visual_info->screen);
1840 Initialize font info.
1842 font_info=XBestFont(display,&resource_info,MagickFalse);
1843 if (font_info == (XFontStruct *) NULL)
1845 ThrowXWindowException(XServerError,"UnableToLoadFont",
1847 return(MagickFalse);
1849 if ((map_info == (XStandardColormap *) NULL) ||
1850 (visual_info == (XVisualInfo *) NULL) ||
1851 (font_info == (XFontStruct *) NULL))
1853 XFreeResources(display,visual_info,map_info,&pixel,font_info,
1854 &resource_info,(XWindowInfo *) NULL);
1855 ThrowXWindowException(XServerError,"UnableToLoadFont",
1857 return(MagickFalse);
1859 cache_info=(*draw_info);
1862 Initialize annotate info.
1864 XGetAnnotateInfo(&annotate_info);
1865 annotate_info.stencil=ForegroundStencil;
1866 if (cache_info.font != draw_info->font)
1869 Type name has changed.
1871 (void) XFreeFont(display,font_info);
1872 (void) CloneString(&resource_info.font,draw_info->font);
1873 font_info=XBestFont(display,&resource_info,MagickFalse);
1874 if (font_info == (XFontStruct *) NULL)
1876 ThrowXWindowException(XServerError,"UnableToLoadFont",
1878 return(MagickFalse);
1881 if (image->debug != MagickFalse)
1882 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1883 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1884 draw_info->font : "none",draw_info->pointsize);
1885 cache_info=(*draw_info);
1886 annotate_info.font_info=font_info;
1887 annotate_info.text=(char *) draw_info->text;
1888 annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text,
1889 (int) strlen(draw_info->text));
1890 annotate_info.height=(unsigned int) font_info->ascent+font_info->descent;
1891 metrics->pixels_per_em.x=(double) font_info->max_bounds.width;
1892 metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent;
1893 metrics->ascent=(double) font_info->ascent+4;
1894 metrics->descent=(double) (-font_info->descent);
1895 metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine);
1896 metrics->height=font_info->ascent+font_info->descent;
1897 metrics->max_advance=(double) font_info->max_bounds.width;
1898 metrics->bounds.x1=0.0;
1899 metrics->bounds.y1=metrics->descent;
1900 metrics->bounds.x2=metrics->ascent+metrics->descent;
1901 metrics->bounds.y2=metrics->ascent+metrics->descent;
1902 metrics->underline_position=(-2.0);
1903 metrics->underline_thickness=1.0;
1904 if (draw_info->render == MagickFalse)
1906 if (draw_info->fill.opacity == TransparentOpacity)
1911 width=annotate_info.width;
1912 height=annotate_info.height;
1913 if ((draw_info->affine.rx != 0.0) || (draw_info->affine.ry != 0.0))
1915 if (((draw_info->affine.sx-draw_info->affine.sy) == 0.0) &&
1916 ((draw_info->affine.rx+draw_info->affine.ry) == 0.0))
1917 annotate_info.degrees=(180.0/MagickPI)*
1918 atan2(draw_info->affine.rx,draw_info->affine.sx);
1920 (void) FormatMagickString(annotate_info.geometry,MaxTextExtent,
1921 "%lux%lu+%ld+%ld",width,height,(long) (offset->x+0.5),
1922 (long) (offset->y-metrics->ascent-metrics->descent+
1923 draw_info->interline_spacing+0.5));
1924 pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red);
1925 pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green);
1926 pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue);
1927 status=XAnnotateImage(display,&pixel,&annotate_info,image);
1930 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
1932 return(MagickFalse);
1937 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1938 const PointInfo *offset,TypeMetric *metrics)
1943 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1944 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (X11)",
1946 return(MagickFalse);