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/attribute.h"
48 #include "magick/cache-view.h"
49 #include "magick/client.h"
50 #include "magick/color.h"
51 #include "magick/color-private.h"
52 #include "magick/composite.h"
53 #include "magick/composite-private.h"
54 #include "magick/constitute.h"
55 #include "magick/draw.h"
56 #include "magick/draw-private.h"
57 #include "magick/exception.h"
58 #include "magick/exception-private.h"
59 #include "magick/gem.h"
60 #include "magick/geometry.h"
61 #include "magick/image-private.h"
62 #include "magick/log.h"
63 #include "magick/quantum.h"
64 #include "magick/quantum-private.h"
65 #include "magick/property.h"
66 #include "magick/resource_.h"
67 #include "magick/statistic.h"
68 #include "magick/string_.h"
69 #include "magick/token-private.h"
70 #include "magick/transform.h"
71 #include "magick/type.h"
72 #include "magick/utility.h"
73 #include "magick/xwindow-private.h"
74 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
75 #if defined(__MINGW32__)
78 #if defined(MAGICKCORE_HAVE_FT2BUILD_H)
79 # include <ft2build.h>
81 #if defined(FT_FREETYPE_H)
82 # include FT_FREETYPE_H
84 # include <freetype/freetype.h>
86 #if defined(FT_GLYPH_H)
89 # include <freetype/ftglyph.h>
91 #if defined(FT_OUTLINE_H)
92 # include FT_OUTLINE_H
94 # include <freetype/ftoutln.h>
96 #if defined(FT_BBOX_H)
99 # include <freetype/ftbbox.h>
100 #endif /* defined(FT_BBOX_H) */
104 Forward declarations.
106 static MagickBooleanType
107 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *),
108 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *),
109 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
111 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *);
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % A n n o t a t e I m a g e %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % AnnotateImage() annotates an image with text. Optionally you can include
125 % any of the following bits of information about the image by embedding
126 % the appropriate special characters:
128 % %b file size in bytes.
130 % %d directory in which the image resides.
131 % %e extension of the image file.
132 % %f original filename of the image.
133 % %h height of image.
134 % %i filename of the image.
135 % %k number of unique colors.
137 % %m image file format.
138 % %n number of images in a image sequence.
139 % %o output image filename.
140 % %p page number of the image.
141 % %q image depth (8 or 16).
142 % %q image depth (8 or 16).
143 % %s image scene number.
144 % %t image filename without any extension.
145 % %u a unique temporary filename.
147 % %x x resolution of the image.
148 % %y y resolution of the image.
150 % The format of the AnnotateImage method is:
152 % MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info)
154 % A description of each parameter follows:
156 % o image: the image.
158 % o draw_info: the draw info.
161 MagickExport MagickBooleanType AnnotateImage(Image *image,
162 const DrawInfo *draw_info)
165 primitive[MaxTextExtent],
197 assert(image != (Image *) NULL);
198 assert(image->signature == MagickSignature);
199 if (image->debug != MagickFalse)
200 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
201 assert(draw_info != (DrawInfo *) NULL);
202 assert(draw_info->signature == MagickSignature);
203 if (draw_info->text == (char *) NULL)
205 if (*draw_info->text == '\0')
207 textlist=StringToList(draw_info->text);
208 if (textlist == (char **) NULL)
210 length=strlen(textlist[0]);
211 for (i=1; textlist[i] != (char *) NULL; i++)
212 if (strlen(textlist[i]) > length)
213 length=strlen(textlist[i]);
214 number_lines=(unsigned long) i;
215 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
216 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
217 SetGeometry(image,&geometry);
218 SetGeometryInfo(&geometry_info);
219 if (annotate_info->geometry != (char *) NULL)
221 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
223 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
225 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
228 for (i=0; textlist[i] != (char *) NULL; i++)
231 Position text relative to image.
233 annotate_info->affine.tx=geometry_info.xi-image->page.x;
234 annotate_info->affine.ty=geometry_info.psi-image->page.y;
235 (void) CloneString(&annotate->text,textlist[i]);
236 (void) GetTypeMetrics(image,annotate,&metrics);
237 height=(long) (metrics.ascent-metrics.descent+
238 draw_info->interline_spacing+0.5);
239 switch (annotate->gravity)
241 case UndefinedGravity:
244 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
245 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
248 case NorthWestGravity:
250 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
251 annotate_info->affine.ry*height+annotate_info->affine.ry*
252 (metrics.ascent+metrics.descent);
253 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
254 annotate_info->affine.sy*height+annotate_info->affine.sy*
260 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
261 geometry.width/2.0+i*annotate_info->affine.ry*height-
262 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
263 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
264 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
265 annotate_info->affine.sy*height+annotate_info->affine.sy*
266 metrics.ascent-annotate_info->affine.rx*(metrics.width-
267 metrics.bounds.x1)/2.0;
270 case NorthEastGravity:
272 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
273 geometry.width+i*annotate_info->affine.ry*height-
274 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
275 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
276 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
277 annotate_info->affine.sy*height+annotate_info->affine.sy*
278 metrics.ascent-annotate_info->affine.rx*(metrics.width-
284 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
285 annotate_info->affine.ry*height+annotate_info->affine.ry*
286 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
287 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
288 geometry.height/2.0+i*annotate_info->affine.sy*height+
289 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
290 (number_lines-1.0)*height)/2.0;
296 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
297 geometry.width/2.0+i*annotate_info->affine.ry*height-
298 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
299 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
300 (number_lines-1)*height)/2.0;
301 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
302 geometry.height/2.0+i*annotate_info->affine.sy*height-
303 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
304 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
305 (number_lines-1.0)*height)/2.0;
310 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
311 geometry.width+i*annotate_info->affine.ry*height-
312 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
313 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
314 (number_lines-1.0)*height)/2.0-1.0;
315 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
316 geometry.height/2.0+i*annotate_info->affine.sy*height-
317 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+
318 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
319 (number_lines-1.0)*height)/2.0;
322 case SouthWestGravity:
324 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
325 annotate_info->affine.ry*height-annotate_info->affine.ry*
326 (number_lines-1.0)*height;
327 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
328 geometry.height+i*annotate_info->affine.sy*height-
329 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
334 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
335 geometry.width/2.0+i*annotate_info->affine.ry*height-
336 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
337 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
338 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
339 geometry.height+i*annotate_info->affine.sy*height-
340 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
341 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
344 case SouthEastGravity:
346 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
347 geometry.width+i*annotate_info->affine.ry*height-
348 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
349 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
350 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
351 geometry.height+i*annotate_info->affine.sy*height-
352 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
353 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
357 switch (annotate->align)
361 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
362 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
367 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
368 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
369 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
370 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
375 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
376 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
377 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
378 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
384 if (draw_info->undercolor.opacity != TransparentOpacity)
392 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
393 undercolor_info->fill=draw_info->undercolor;
394 undercolor_info->affine=draw_info->affine;
395 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
396 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
397 (void) FormatMagickString(primitive,MaxTextExtent,
398 "rectangle 0,0 %g,%lu",metrics.origin.x,height);
399 (void) CloneString(&undercolor_info->primitive,primitive);
400 (void) DrawImage(image,undercolor_info);
401 (void) DestroyDrawInfo(undercolor_info);
403 annotate_info->affine.tx=offset.x;
404 annotate_info->affine.ty=offset.y;
405 (void) FormatMagickString(primitive,MaxTextExtent,"stroke-width %g "
406 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
407 if (annotate->decorate == OverlineDecoration)
409 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
410 metrics.descent-metrics.underline_position));
411 (void) CloneString(&annotate_info->primitive,primitive);
412 (void) DrawImage(image,annotate_info);
415 if (annotate->decorate == UnderlineDecoration)
417 annotate_info->affine.ty-=(draw_info->affine.sy*
418 metrics.underline_position);
419 (void) CloneString(&annotate_info->primitive,primitive);
420 (void) DrawImage(image,annotate_info);
423 Annotate image with text.
425 status=RenderType(image,annotate,&offset,&metrics);
426 if (status == MagickFalse)
428 if (annotate->decorate == LineThroughDecoration)
430 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
431 metrics.underline_position+metrics.descent)/2.0);
432 (void) CloneString(&annotate_info->primitive,primitive);
433 (void) DrawImage(image,annotate_info);
437 Relinquish resources.
439 annotate_info=DestroyDrawInfo(annotate_info);
440 annotate=DestroyDrawInfo(annotate);
441 for (i=0; textlist[i] != (char *) NULL; i++)
442 textlist[i]=DestroyString(textlist[i]);
443 textlist=(char **) RelinquishMagickMemory(textlist);
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 % F o r m a t M a g i c k C a p t i o n %
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458 % FormatMagickCaption() formats a caption so that it fits within the image
459 % width. It returns the number of lines in the formatted caption.
461 % The format of the FormatMagickCaption method is:
463 % long FormatMagickCaption(Image *image,DrawInfo *draw_info,
464 % TypeMetric *metrics,char **caption)
466 % A description of each parameter follows.
468 % o image: The image.
470 % o caption: the caption.
472 % o draw_info: the draw info.
474 % o metrics: Return the font metrics in this structure.
477 MagickExport long FormatMagickCaption(Image *image,DrawInfo *draw_info,
478 TypeMetric *metrics,char **caption)
496 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
498 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
500 for (i=0; i < (long) GetUTFOctets(p); i++)
503 status=GetTypeMetrics(image,draw_info,metrics);
504 if (status == MagickFalse)
506 width=(unsigned long) (metrics->width+0.5);
507 if (GetUTFCode(p) != '\n')
508 if (width <= image->columns)
510 if (s == (char *) NULL)
513 while ((IsUTFSpace(GetUTFCode(s)) == MagickFalse) &&
514 (GetUTFCode(s) != 0))
517 if (GetUTFCode(s) != 0)
531 No convenient line breaks-- insert newline.
533 target=AcquireString(*caption);
535 CopyMagickString(target,*caption,n+1);
536 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
537 ConcatenateMagickString(target,p,strlen(*caption)+2);
538 (void) DestroyString(*caption);
546 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
547 if (GetUTFCode(p) == '\n')
553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % G e t M u l t i l i n e T y p e M e t r i c s %
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 % GetMultilineTypeMetrics() returns the following information for the
564 % specified font and text:
572 % maximum horizontal advance
580 % underline thickness
582 % This method is like GetTypeMetrics() but it returns the maximum text width
583 % and height for multiple lines of text.
585 % The format of the GetMultilineTypeMetrics method is:
587 % MagickBooleanType GetMultilineTypeMetrics(Image *image,
588 % const DrawInfo *draw_info,TypeMetric *metrics)
590 % A description of each parameter follows:
592 % o image: the image.
594 % o draw_info: the draw info.
596 % o metrics: Return the font metrics in this structure.
599 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
600 const DrawInfo *draw_info,TypeMetric *metrics)
617 assert(image != (Image *) NULL);
618 assert(image->signature == MagickSignature);
619 if (image->debug != MagickFalse)
620 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
621 assert(draw_info != (DrawInfo *) NULL);
622 assert(draw_info->text != (char *) NULL);
623 assert(draw_info->signature == MagickSignature);
624 if (*draw_info->text == '\0')
626 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
627 annotate_info->text=DestroyString(annotate_info->text);
629 Convert newlines to multiple lines of text.
631 textlist=StringToList(draw_info->text);
632 if (textlist == (char **) NULL)
634 annotate_info->render=MagickFalse;
635 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
636 (void) ResetMagickMemory(&extent,0,sizeof(extent));
638 Find the widest of the text lines.
640 annotate_info->text=textlist[0];
641 status=GetTypeMetrics(image,annotate_info,&extent);
643 for (i=1; textlist[i] != (char *) NULL; i++)
645 annotate_info->text=textlist[i];
646 status=GetTypeMetrics(image,annotate_info,&extent);
647 if (extent.width > metrics->width)
650 metrics->height=(double) (i*(unsigned long) (metrics->ascent-
651 metrics->descent+0.5)+(i-1)*draw_info->interline_spacing);
653 Relinquish resources.
655 annotate_info->text=(char *) NULL;
656 annotate_info=DestroyDrawInfo(annotate_info);
657 for (i=0; textlist[i] != (char *) NULL; i++)
658 textlist[i]=DestroyString(textlist[i]);
659 textlist=(char **) RelinquishMagickMemory(textlist);
664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668 % G e t T y p e M e t r i c s %
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 % GetTypeMetrics() returns the following information for the specified font
683 % maximum horizontal advance
691 % underline thickness
693 % The format of the GetTypeMetrics method is:
695 % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
696 % TypeMetric *metrics)
698 % A description of each parameter follows:
700 % o image: the image.
702 % o draw_info: the draw info.
704 % o metrics: Return the font metrics in this structure.
707 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
708 const DrawInfo *draw_info,TypeMetric *metrics)
719 assert(image != (Image *) NULL);
720 assert(image->signature == MagickSignature);
721 if (image->debug != MagickFalse)
722 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
723 assert(draw_info != (DrawInfo *) NULL);
724 assert(draw_info->text != (char *) NULL);
725 assert(draw_info->signature == MagickSignature);
726 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
727 annotate_info->render=MagickFalse;
728 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
731 status=RenderType(image,annotate_info,&offset,metrics);
732 if (image->debug != MagickFalse)
733 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
734 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
735 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
736 "underline position: %g; underline thickness: %g",annotate_info->text,
737 metrics->width,metrics->height,metrics->ascent,metrics->descent,
738 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
739 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
740 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
741 metrics->underline_position,metrics->underline_thickness);
742 annotate_info=DestroyDrawInfo(annotate_info);
747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751 + R e n d e r T y p e %
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 % RenderType() renders text on the image. It also returns the bounding box of
758 % the text relative to the image.
760 % The format of the RenderType method is:
762 % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
763 % const PointInfo *offset,TypeMetric *metrics)
765 % A description of each parameter follows:
767 % o image: the image.
769 % o draw_info: the draw info.
771 % o offset: (x,y) location of text relative to image.
773 % o metrics: bounding box of text.
776 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
777 const PointInfo *offset,TypeMetric *metrics)
788 type_info=(const TypeInfo *) NULL;
789 if (draw_info->font != (char *) NULL)
791 if (*draw_info->font == '@')
793 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
797 if (*draw_info->font == '-')
798 return(RenderX11(image,draw_info,offset,metrics));
799 if (IsPathAccessible(draw_info->font) != MagickFalse)
801 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
805 type_info=GetTypeInfo(draw_info->font,&image->exception);
806 if (type_info == (const TypeInfo *) NULL)
807 (void) ThrowMagickException(&image->exception,GetMagickModule(),
808 TypeWarning,"UnableToReadFont","`%s'",draw_info->font);
810 if ((type_info == (const TypeInfo *) NULL) &&
811 (draw_info->family != (const char *) NULL))
813 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
814 draw_info->stretch,draw_info->weight,&image->exception);
815 if (type_info == (const TypeInfo *) NULL)
816 (void) ThrowMagickException(&image->exception,GetMagickModule(),
817 TypeWarning,"UnableToReadFont","`%s'",draw_info->family);
819 if (type_info == (const TypeInfo *) NULL)
820 type_info=GetTypeInfoByFamily("arial",draw_info->style,
821 draw_info->stretch,draw_info->weight,&image->exception);
822 if (type_info == (const TypeInfo *) NULL)
823 type_info=GetTypeInfoByFamily("helvetica",draw_info->style,
824 draw_info->stretch,draw_info->weight,&image->exception);
825 if (type_info == (const TypeInfo *) NULL)
826 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
827 draw_info->stretch,draw_info->weight,&image->exception);
828 if (type_info == (const TypeInfo *) NULL)
830 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics);
833 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
834 annotate_info->face=type_info->face;
835 if (type_info->metrics != (char *) NULL)
836 (void) CloneString(&annotate_info->metrics,type_info->metrics);
837 if (type_info->glyphs != (char *) NULL)
838 (void) CloneString(&annotate_info->font,type_info->glyphs);
839 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics);
840 annotate_info=DestroyDrawInfo(annotate_info);
845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
849 + R e n d e r F r e e t y p e %
853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 % RenderFreetype() renders text on the image with a Truetype font. It also
856 % returns the bounding box of the text relative to the image.
858 % The format of the RenderFreetype method is:
860 % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
861 % const char *encoding,const PointInfo *offset,TypeMetric *metrics)
863 % A description of each parameter follows:
865 % o image: the image.
867 % o draw_info: the draw info.
869 % o encoding: the font encoding.
871 % o offset: (x,y) location of text relative to image.
873 % o metrics: bounding box of text.
877 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
878 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
887 affine=draw_info->affine;
888 (void) FormatMagickString(path,MaxTextExtent,"C%g,%g %g,%g %g,%g",
889 affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,
890 affine.ty-q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0);
891 (void) ConcatenateString(&draw_info->primitive,path);
895 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
903 affine=draw_info->affine;
904 (void) FormatMagickString(path,MaxTextExtent,"L%g,%g",affine.tx+to->x/64.0,
905 affine.ty-to->y/64.0);
906 (void) ConcatenateString(&draw_info->primitive,path);
910 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
918 affine=draw_info->affine;
919 (void) FormatMagickString(path,MaxTextExtent,"M%g,%g",affine.tx+to->x/64.0,
920 affine.ty-to->y/64.0);
921 (void) ConcatenateString(&draw_info->primitive,path);
925 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
934 affine=draw_info->affine;
935 (void) FormatMagickString(path,MaxTextExtent,"Q%g,%g %g,%g",
936 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
937 affine.ty-to->y/64.0);
938 (void) ConcatenateString(&draw_info->primitive,path);
942 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
943 const char *encoding,const PointInfo *offset,TypeMetric *metrics)
945 #if !defined(FT_OPEN_PATHNAME)
946 #define FT_OPEN_PATHNAME ft_open_pathname
949 typedef struct _GlyphInfo
1012 static FT_Outline_Funcs
1015 (FT_Outline_MoveTo_Func) TraceMoveTo,
1016 (FT_Outline_LineTo_Func) TraceLineTo,
1017 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1018 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1023 Initialize Truetype library.
1025 status=FT_Init_FreeType(&library);
1027 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1029 args.flags=FT_OPEN_PATHNAME;
1030 if (draw_info->font == (char *) NULL)
1031 args.pathname=ConstantString("helvetica");
1033 if (*draw_info->font != '@')
1034 args.pathname=ConstantString(draw_info->font);
1036 args.pathname=ConstantString(draw_info->font+1);
1037 face=(FT_Face) NULL;
1038 status=FT_Open_Face(library,&args,draw_info->face,&face);
1039 args.pathname=DestroyString(args.pathname);
1042 (void) FT_Done_FreeType(library);
1043 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1044 TypeError,"UnableToReadFont","`%s'",draw_info->font);
1045 return(RenderPostscript(image,draw_info,offset,metrics));
1047 if ((draw_info->metrics != (char *) NULL) &&
1048 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1049 (void) FT_Attach_File(face,draw_info->metrics);
1050 encoding_type=ft_encoding_unicode;
1051 status=FT_Select_Charmap(face,encoding_type);
1052 if ((status != 0) && (face->num_charmaps != 0))
1053 status=FT_Set_Charmap(face,face->charmaps[0]);
1054 if (encoding != (const char *) NULL)
1056 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1057 encoding_type=ft_encoding_adobe_custom;
1058 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1059 encoding_type=ft_encoding_adobe_expert;
1060 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1061 encoding_type=ft_encoding_adobe_standard;
1062 if (LocaleCompare(encoding,"AppleRoman") == 0)
1063 encoding_type=ft_encoding_apple_roman;
1064 if (LocaleCompare(encoding,"BIG5") == 0)
1065 encoding_type=ft_encoding_big5;
1066 if (LocaleCompare(encoding,"GB2312") == 0)
1067 encoding_type=ft_encoding_gb2312;
1068 if (LocaleCompare(encoding,"Johab") == 0)
1069 encoding_type=ft_encoding_johab;
1070 #if defined(ft_encoding_latin_1)
1071 if (LocaleCompare(encoding,"Latin-1") == 0)
1072 encoding_type=ft_encoding_latin_1;
1074 if (LocaleCompare(encoding,"Latin-2") == 0)
1075 encoding_type=ft_encoding_latin_2;
1076 if (LocaleCompare(encoding,"None") == 0)
1077 encoding_type=ft_encoding_none;
1078 if (LocaleCompare(encoding,"SJIScode") == 0)
1079 encoding_type=ft_encoding_sjis;
1080 if (LocaleCompare(encoding,"Symbol") == 0)
1081 encoding_type=ft_encoding_symbol;
1082 if (LocaleCompare(encoding,"Unicode") == 0)
1083 encoding_type=ft_encoding_unicode;
1084 if (LocaleCompare(encoding,"Wansung") == 0)
1085 encoding_type=ft_encoding_wansung;
1086 status=FT_Select_Charmap(face,encoding_type);
1088 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1093 resolution.x=DefaultResolution;
1094 resolution.y=DefaultResolution;
1095 if (draw_info->density != (char *) NULL)
1103 flags=ParseGeometry(draw_info->density,&geometry_info);
1104 resolution.x=geometry_info.rho;
1105 resolution.y=geometry_info.sigma;
1106 if ((flags & SigmaValue) == 0)
1107 resolution.y=resolution.x;
1109 status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1110 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1111 (FT_UInt) resolution.y);
1112 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1113 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1114 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1115 metrics->descent=(double) face->size->metrics.descender/64.0;
1117 metrics->origin.x=0;
1118 metrics->origin.y=0;
1119 metrics->height=(double) face->size->metrics.height/64.0;
1120 metrics->max_advance=0.0;
1121 if (face->size->metrics.max_advance > MagickEpsilon)
1122 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1123 metrics->bounds.x1=0.0;
1124 metrics->bounds.y1=metrics->descent;
1125 metrics->bounds.x2=metrics->ascent+metrics->descent;
1126 metrics->bounds.y2=metrics->ascent+metrics->descent;
1127 metrics->underline_position=face->underline_position/64.0;
1128 metrics->underline_thickness=face->underline_thickness/64.0;
1129 if (*draw_info->text == '\0')
1131 (void) FT_Done_Face(face);
1132 (void) FT_Done_FreeType(library);
1136 Compute bounding box.
1138 if (image->debug != MagickFalse)
1139 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1140 "font-encoding %s; text-encoding %s; pointsize %g",
1141 draw_info->font != (char *) NULL ? draw_info->font : "none",
1142 encoding != (char *) NULL ? encoding : "none",
1143 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1144 draw_info->pointsize);
1145 flags=FT_LOAD_NO_BITMAP;
1146 value=GetImageProperty(image,"type:hinting");
1147 if (LocaleCompare(value,"off") == 0)
1148 flags|=FT_LOAD_NO_HINTING;
1152 last_glyph.image=NULL;
1159 if (draw_info->render != MagickFalse)
1161 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1162 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1163 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1164 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1166 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1167 (void) CloneString(&annotate_info->primitive,"path '");
1168 if (draw_info->render != MagickFalse)
1170 if (image->storage_class != DirectClass)
1171 (void) SetImageStorageClass(image,DirectClass);
1172 if (image->matte == MagickFalse)
1173 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1178 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1180 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1182 glyph.id=FT_Get_Char_Index(face,'?');
1183 if ((glyph.id != 0) && (last_glyph.id != 0))
1185 if (draw_info->kerning != 0.0)
1186 origin.x+=64.0*draw_info->kerning;
1188 if (FT_HAS_KERNING(face))
1193 status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1194 ft_kerning_default,&kerning);
1196 origin.x+=kerning.x;
1199 glyph.origin=origin;
1200 status=FT_Load_Glyph(face,glyph.id,flags);
1203 status=FT_Get_Glyph(face->glyph,&glyph.image);
1206 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1210 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
1211 metrics->bounds.x1=bounds.xMin;
1212 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
1213 metrics->bounds.y1=bounds.yMin;
1214 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
1215 metrics->bounds.x2=bounds.xMax;
1216 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
1217 metrics->bounds.y2=bounds.yMax;
1218 if (draw_info->render != MagickFalse)
1219 if ((draw_info->stroke.opacity != TransparentOpacity) ||
1220 (draw_info->stroke_pattern != (Image *) NULL))
1225 annotate_info->affine.tx=glyph.origin.x/64.0;
1226 annotate_info->affine.ty=glyph.origin.y/64.0;
1227 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->outline,
1228 &OutlineMethods,annotate_info);
1230 FT_Vector_Transform(&glyph.origin,&affine);
1231 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1232 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1233 (FT_Vector *) NULL,MagickTrue);
1236 bitmap=(FT_BitmapGlyph) glyph.image;
1237 point.x=offset->x+bitmap->left;
1238 point.y=offset->y-bitmap->top;
1239 if (draw_info->render != MagickFalse)
1251 Rasterize the glyph.
1254 exception=(&image->exception);
1255 image_view=AcquireCacheView(image);
1256 for (y=0; y < (long) bitmap->bitmap.rows; y++)
1275 register PixelPacket
1278 register unsigned char
1281 if (status == MagickFalse)
1283 x_offset=(long) (point.x+0.5);
1284 y_offset=(long) (point.y+y+0.5);
1285 if ((y_offset < 0) || (y_offset >= (long) image->rows))
1287 q=(PixelPacket *) NULL;
1288 if ((x_offset < 0) || (x_offset >= (long) image->columns))
1292 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1293 bitmap->bitmap.width,1,exception);
1294 active=q != (PixelPacket *) NULL ? MagickTrue : MagickFalse;
1296 p=bitmap->bitmap.buffer+y*bitmap->bitmap.width;
1297 for (x=0; x < (long) bitmap->bitmap.width; x++)
1300 if ((*p == 0) || (x_offset < 0) ||
1301 (x_offset >= (long) image->columns))
1307 fill_opacity=(MagickRealType) (*p)/(bitmap->bitmap.num_grays-1);
1308 if (draw_info->text_antialias == MagickFalse)
1309 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1310 if (active == MagickFalse)
1311 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1313 if (q == (PixelPacket *) NULL)
1319 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
1320 fill_opacity=QuantumRange-fill_opacity*(QuantumRange-
1321 fill_color.opacity);
1322 MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q);
1323 if (active == MagickFalse)
1325 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1326 if (sync == MagickFalse)
1332 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1333 if (sync == MagickFalse)
1336 image_view=DestroyCacheView(image_view);
1338 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1339 metrics->width=bitmap->left+bitmap->bitmap.width;
1340 if ((draw_info->interword_spacing != 0.0) &&
1341 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1342 (IsUTFSpace(code) == MagickFalse))
1343 origin.x+=64.0*draw_info->interword_spacing;
1345 origin.x+=face->glyph->advance.x;
1346 metrics->origin.x=origin.x;
1347 metrics->origin.y=origin.y;
1348 if (last_glyph.id != 0)
1349 FT_Done_Glyph(last_glyph.image);
1353 if (last_glyph.id != 0)
1354 FT_Done_Glyph(last_glyph.image);
1355 if ((draw_info->stroke.opacity != TransparentOpacity) ||
1356 (draw_info->stroke_pattern != (Image *) NULL))
1358 if (draw_info->render != MagickFalse)
1363 annotate_info->linejoin=RoundJoin;
1364 annotate_info->affine.tx=offset->x;
1365 annotate_info->affine.ty=offset->y;
1366 (void) ConcatenateString(&annotate_info->primitive,"'");
1367 (void) DrawImage(image,annotate_info);
1371 Determine font metrics.
1373 glyph.id=FT_Get_Char_Index(face,'_');
1374 glyph.origin=origin;
1375 status=FT_Load_Glyph(face,glyph.id,flags);
1378 status=FT_Get_Glyph(face->glyph,&glyph.image);
1381 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1385 FT_Vector_Transform(&glyph.origin,&affine);
1386 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1387 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1388 (FT_Vector *) NULL,MagickTrue);
1389 bitmap=(FT_BitmapGlyph) glyph.image;
1390 if (bitmap->left > metrics->width)
1391 metrics->width=bitmap->left;
1395 FT_Done_Glyph(glyph.image);
1397 metrics->width-=metrics->bounds.x1/64.0;
1398 metrics->bounds.x1/=64.0;
1399 metrics->bounds.y1/=64.0;
1400 metrics->bounds.x2/=64.0;
1401 metrics->bounds.y2/=64.0;
1402 metrics->origin.x/=64.0;
1403 metrics->origin.y/=64.0;
1405 Relinquish resources.
1407 annotate_info=DestroyDrawInfo(annotate_info);
1408 (void) FT_Done_Face(face);
1409 (void) FT_Done_FreeType(library);
1413 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1414 const char *magick_unused(encoding),const PointInfo *offset,
1415 TypeMetric *metrics)
1417 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1418 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (Freetype)",
1420 return(RenderPostscript(image,draw_info,offset,metrics));
1425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1429 + R e n d e r P o s t s c r i p t %
1433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1435 % RenderPostscript() renders text on the image with a Postscript font. It
1436 % also returns the bounding box of the text relative to the image.
1438 % The format of the RenderPostscript method is:
1440 % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1441 % const PointInfo *offset,TypeMetric *metrics)
1443 % A description of each parameter follows:
1445 % o image: the image.
1447 % o draw_info: the draw info.
1449 % o offset: (x,y) location of text relative to image.
1451 % o metrics: bounding box of text.
1455 static inline size_t MagickMin(const size_t x,const size_t y)
1462 static char *EscapeParenthesis(const char *text)
1477 buffer=AcquireString(text);
1479 for (i=0; i < (long) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
1481 if ((text[i] == '(') || (text[i] == ')'))
1492 static MagickBooleanType RenderPostscript(Image *image,
1493 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics)
1496 filename[MaxTextExtent],
1497 geometry[MaxTextExtent],
1527 Render label with a Postscript font.
1529 if (image->debug != MagickFalse)
1530 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1531 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1532 draw_info->font : "none",draw_info->pointsize);
1534 unique_file=AcquireUniqueFileResource(filename);
1535 if (unique_file != -1)
1536 file=fdopen(unique_file,"wb");
1537 if ((unique_file == -1) || (file == (FILE *) NULL))
1539 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1541 return(MagickFalse);
1543 (void) fprintf(file,"%%!PS-Adobe-3.0\n");
1544 (void) fprintf(file,"/ReencodeType\n");
1545 (void) fprintf(file,"{\n");
1546 (void) fprintf(file," findfont dup length\n");
1547 (void) fprintf(file,
1548 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
1549 (void) fprintf(file,
1550 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
1551 (void) fprintf(file,"} bind def\n");
1553 Sample to compute bounding box.
1555 identity=(draw_info->affine.sx == draw_info->affine.sy) &&
1556 (draw_info->affine.rx == 0.0) && (draw_info->affine.ry == 0.0) ?
1557 MagickTrue : MagickFalse;
1560 for (i=0; i <= (long) (strlen(draw_info->text)+2); i++)
1562 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1563 draw_info->affine.ry*2.0*draw_info->pointsize);
1564 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1565 draw_info->affine.sy*2.0*draw_info->pointsize);
1566 if (point.x > extent.x)
1568 if (point.y > extent.y)
1571 (void) fprintf(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
1572 extent.x/2.0,extent.y/2.0);
1573 (void) fprintf(file,"%g %g scale\n",draw_info->pointsize,
1574 draw_info->pointsize);
1575 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1576 (strchr(draw_info->font,'/') != (char *) NULL))
1577 (void) fprintf(file,
1578 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1580 (void) fprintf(file,"/%s-ISO dup /%s ReencodeType findfont setfont\n",
1581 draw_info->font,draw_info->font);
1582 (void) fprintf(file,"[%g %g %g %g 0 0] concat\n",draw_info->affine.sx,
1583 -draw_info->affine.rx,-draw_info->affine.ry,draw_info->affine.sy);
1584 text=EscapeParenthesis(draw_info->text);
1585 if (identity == MagickFalse)
1586 (void) fprintf(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",text);
1587 (void) fprintf(file,"(%s) show\n",text);
1588 text=DestroyString(text);
1589 (void) fprintf(file,"showpage\n");
1590 (void) fclose(file);
1591 (void) FormatMagickString(geometry,MaxTextExtent,"%ldx%ld+0+0!",(long)
1592 (extent.x+0.5),(long) (extent.y+0.5));
1593 annotate_info=AcquireImageInfo();
1594 (void) FormatMagickString(annotate_info->filename,MaxTextExtent,"ps:%s",
1596 (void) CloneString(&annotate_info->page,geometry);
1597 if (draw_info->density != (char *) NULL)
1598 (void) CloneString(&annotate_info->density,draw_info->density);
1599 annotate_info->antialias=draw_info->text_antialias;
1600 annotate_image=ReadImage(annotate_info,&image->exception);
1601 CatchException(&image->exception);
1602 annotate_info=DestroyImageInfo(annotate_info);
1603 (void) RelinquishUniqueFileResource(filename);
1604 if (annotate_image == (Image *) NULL)
1605 return(MagickFalse);
1606 resolution.x=DefaultResolution;
1607 resolution.y=DefaultResolution;
1608 if (draw_info->density != (char *) NULL)
1616 flags=ParseGeometry(draw_info->density,&geometry_info);
1617 resolution.x=geometry_info.rho;
1618 resolution.y=geometry_info.sigma;
1619 if ((flags & SigmaValue) == 0)
1620 resolution.y=resolution.x;
1622 if (identity == MagickFalse)
1623 (void) TransformImage(&annotate_image,"0x0",(char *) NULL);
1629 crop_info=GetImageBoundingBox(annotate_image,&annotate_image->exception);
1630 crop_info.height=(unsigned long) ((resolution.y/DefaultResolution)*
1631 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
1632 crop_info.y=(long) ((resolution.y/DefaultResolution)*extent.y/8.0+0.5);
1633 (void) FormatMagickString(geometry,MaxTextExtent,"%lux%lu%+ld%+ld",
1634 crop_info.width,crop_info.height,crop_info.x,crop_info.y);
1635 (void) TransformImage(&annotate_image,geometry,(char *) NULL);
1637 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1638 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1639 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1640 metrics->ascent=metrics->pixels_per_em.x;
1641 metrics->descent=metrics->pixels_per_em.y/-5.0;
1642 metrics->width=(double) annotate_image->columns/
1643 ExpandAffine(&draw_info->affine);
1644 metrics->height=1.152*metrics->pixels_per_em.x;
1645 metrics->max_advance=metrics->pixels_per_em.x;
1646 metrics->bounds.x1=0.0;
1647 metrics->bounds.y1=metrics->descent;
1648 metrics->bounds.x2=metrics->ascent+metrics->descent;
1649 metrics->bounds.y2=metrics->ascent+metrics->descent;
1650 metrics->underline_position=(-2.0);
1651 metrics->underline_thickness=1.0;
1652 if (draw_info->render == MagickFalse)
1654 annotate_image=DestroyImage(annotate_image);
1657 if (draw_info->fill.opacity != TransparentOpacity)
1674 if (image->matte == MagickFalse)
1675 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1676 if (annotate_image->matte == MagickFalse)
1677 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel);
1678 fill_color=draw_info->fill;
1679 exception=(&image->exception);
1680 annotate_view=AcquireCacheView(annotate_image);
1681 for (y=0; y < (long) annotate_image->rows; y++)
1686 register PixelPacket
1689 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1691 if (q == (PixelPacket *) NULL)
1693 for (x=0; x < (long) annotate_image->columns; x++)
1695 (void) GetFillColor(draw_info,x,y,&fill_color);
1696 q->opacity=RoundToQuantum(QuantumRange-(((QuantumRange-
1697 (MagickRealType) PixelIntensityToQuantum(q))*(QuantumRange-
1698 fill_color.opacity))/QuantumRange));
1699 q->red=fill_color.red;
1700 q->green=fill_color.green;
1701 q->blue=fill_color.blue;
1704 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1705 if (sync == MagickFalse)
1708 annotate_view=DestroyCacheView(annotate_view);
1709 (void) CompositeImage(image,OverCompositeOp,annotate_image,
1710 (long) (offset->x+0.5),(long) (offset->y-(metrics->ascent+
1711 metrics->descent)+0.5));
1713 annotate_image=DestroyImage(annotate_image);
1718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1722 + R e n d e r X 1 1 %
1726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1728 % RenderX11() renders text on the image with an X11 font. It also returns the
1729 % bounding box of the text relative to the image.
1731 % The format of the RenderX11 method is:
1733 % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
1734 % const PointInfo *offset,TypeMetric *metrics)
1736 % A description of each parameter follows:
1738 % o image: the image.
1740 % o draw_info: the draw info.
1742 % o offset: (x,y) location of text relative to image.
1744 % o metrics: bounding box of text.
1747 #if defined(MAGICKCORE_X11_DELEGATE)
1748 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1749 const PointInfo *offset,TypeMetric *metrics)
1758 *display = (Display *) NULL;
1760 static XAnnotateInfo
1769 static XResourceInfo
1775 static XStandardColormap
1785 if (display == (Display *) NULL)
1794 Open X server connection.
1796 display=XOpenDisplay(draw_info->server_name);
1797 if (display == (Display *) NULL)
1799 ThrowXWindowException(XServerError,"UnableToOpenXServer",
1800 draw_info->server_name);
1801 return(MagickFalse);
1804 Get user defaults from X resource database.
1806 (void) XSetErrorHandler(XError);
1807 image_info=AcquireImageInfo();
1808 client_name=GetClientName();
1809 resource_database=XGetResourceDatabase(display,client_name);
1810 XGetResourceInfo(image_info,resource_database,client_name,&resource_info);
1811 resource_info.close_server=MagickFalse;
1812 resource_info.colormap=PrivateColormap;
1813 resource_info.font=AcquireString(draw_info->font);
1814 resource_info.background_color=AcquireString("#ffffffffffff");
1815 resource_info.foreground_color=AcquireString("#000000000000");
1816 map_info=XAllocStandardColormap();
1817 if (map_info == (XStandardColormap *) NULL)
1819 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
1821 return(MagickFalse);
1824 Initialize visual info.
1826 visual_info=XBestVisualInfo(display,map_info,&resource_info);
1827 if (visual_info == (XVisualInfo *) NULL)
1829 ThrowXWindowException(XServerError,"UnableToGetVisual",
1831 return(MagickFalse);
1833 map_info->colormap=(Colormap) NULL;
1834 pixel.pixels=(unsigned long *) NULL;
1836 Initialize Standard Colormap info.
1838 XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen),
1840 XGetPixelPacket(display,visual_info,map_info,&resource_info,
1841 (Image *) NULL,&pixel);
1842 pixel.annotate_context=XDefaultGC(display,visual_info->screen);
1844 Initialize font info.
1846 font_info=XBestFont(display,&resource_info,MagickFalse);
1847 if (font_info == (XFontStruct *) NULL)
1849 ThrowXWindowException(XServerError,"UnableToLoadFont",
1851 return(MagickFalse);
1853 if ((map_info == (XStandardColormap *) NULL) ||
1854 (visual_info == (XVisualInfo *) NULL) ||
1855 (font_info == (XFontStruct *) NULL))
1857 XFreeResources(display,visual_info,map_info,&pixel,font_info,
1858 &resource_info,(XWindowInfo *) NULL);
1859 ThrowXWindowException(XServerError,"UnableToLoadFont",
1861 return(MagickFalse);
1863 cache_info=(*draw_info);
1866 Initialize annotate info.
1868 XGetAnnotateInfo(&annotate_info);
1869 annotate_info.stencil=ForegroundStencil;
1870 if (cache_info.font != draw_info->font)
1873 Type name has changed.
1875 (void) XFreeFont(display,font_info);
1876 (void) CloneString(&resource_info.font,draw_info->font);
1877 font_info=XBestFont(display,&resource_info,MagickFalse);
1878 if (font_info == (XFontStruct *) NULL)
1880 ThrowXWindowException(XServerError,"UnableToLoadFont",
1882 return(MagickFalse);
1885 if (image->debug != MagickFalse)
1886 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1887 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1888 draw_info->font : "none",draw_info->pointsize);
1889 cache_info=(*draw_info);
1890 annotate_info.font_info=font_info;
1891 annotate_info.text=(char *) draw_info->text;
1892 annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text,
1893 (int) strlen(draw_info->text));
1894 annotate_info.height=(unsigned int) font_info->ascent+font_info->descent;
1895 metrics->pixels_per_em.x=(double) font_info->max_bounds.width;
1896 metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent;
1897 metrics->ascent=(double) font_info->ascent+4;
1898 metrics->descent=(double) (-font_info->descent);
1899 metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine);
1900 metrics->height=font_info->ascent+font_info->descent;
1901 metrics->max_advance=(double) font_info->max_bounds.width;
1902 metrics->bounds.x1=0.0;
1903 metrics->bounds.y1=metrics->descent;
1904 metrics->bounds.x2=metrics->ascent+metrics->descent;
1905 metrics->bounds.y2=metrics->ascent+metrics->descent;
1906 metrics->underline_position=(-2.0);
1907 metrics->underline_thickness=1.0;
1908 if (draw_info->render == MagickFalse)
1910 if (draw_info->fill.opacity == TransparentOpacity)
1915 width=annotate_info.width;
1916 height=annotate_info.height;
1917 if ((draw_info->affine.rx != 0.0) || (draw_info->affine.ry != 0.0))
1919 if (((draw_info->affine.sx-draw_info->affine.sy) == 0.0) &&
1920 ((draw_info->affine.rx+draw_info->affine.ry) == 0.0))
1921 annotate_info.degrees=(180.0/MagickPI)*
1922 atan2(draw_info->affine.rx,draw_info->affine.sx);
1924 (void) FormatMagickString(annotate_info.geometry,MaxTextExtent,
1925 "%lux%lu+%ld+%ld",width,height,(long) (offset->x+0.5),
1926 (long) (offset->y-metrics->ascent-metrics->descent+
1927 draw_info->interline_spacing+0.5));
1928 pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red);
1929 pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green);
1930 pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue);
1931 status=XAnnotateImage(display,&pixel,&annotate_info,image);
1934 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
1936 return(MagickFalse);
1941 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1942 const PointInfo *offset,TypeMetric *metrics)
1947 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1948 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (X11)",
1950 return(MagickFalse);