]> granicus.if.org Git - imagemagick/blob - MagickCore/annotate.c
(no commit message)
[imagemagick] / MagickCore / annotate.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
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             %
11 %                                                                             %
12 %                                                                             %
13 %                   MagickCore Image Annotation Methods                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 % Digital Applications (www.digapp.com) contributed the stroked text algorithm.
37 % It was written by Leonard Rosenthol.
38 %
39 %
40 */
41 \f
42 /*
43   Include declarations.
44 */
45 #include "MagickCore/studio.h"
46 #include "MagickCore/annotate.h"
47 #include "MagickCore/annotate-private.h"
48 #include "MagickCore/attribute.h"
49 #include "MagickCore/cache-view.h"
50 #include "MagickCore/client.h"
51 #include "MagickCore/color.h"
52 #include "MagickCore/color-private.h"
53 #include "MagickCore/colorspace-private.h"
54 #include "MagickCore/composite.h"
55 #include "MagickCore/composite-private.h"
56 #include "MagickCore/constitute.h"
57 #include "MagickCore/draw.h"
58 #include "MagickCore/draw-private.h"
59 #include "MagickCore/enhance.h"
60 #include "MagickCore/exception.h"
61 #include "MagickCore/exception-private.h"
62 #include "MagickCore/gem.h"
63 #include "MagickCore/geometry.h"
64 #include "MagickCore/image-private.h"
65 #include "MagickCore/log.h"
66 #include "MagickCore/quantum.h"
67 #include "MagickCore/quantum-private.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/property.h"
70 #include "MagickCore/resource_.h"
71 #include "MagickCore/semaphore.h"
72 #include "MagickCore/statistic.h"
73 #include "MagickCore/string_.h"
74 #include "MagickCore/token-private.h"
75 #include "MagickCore/transform.h"
76 #include "MagickCore/type.h"
77 #include "MagickCore/utility.h"
78 #include "MagickCore/utility-private.h"
79 #include "MagickCore/xwindow.h"
80 #include "MagickCore/xwindow-private.h"
81 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
82 #if defined(__MINGW32__) || defined(__MINGW64__)
83 #  undef interface
84 #endif
85 #include <ft2build.h>
86 #if defined(FT_FREETYPE_H)
87 #  include FT_FREETYPE_H
88 #else
89 #  include <freetype/freetype.h>
90 #endif
91 #if defined(FT_GLYPH_H)
92 #  include FT_GLYPH_H
93 #else
94 #  include <freetype/ftglyph.h>
95 #endif
96 #if defined(FT_OUTLINE_H)
97 #  include FT_OUTLINE_H
98 #else
99 #  include <freetype/ftoutln.h>
100 #endif
101 #if defined(FT_BBOX_H)
102 #  include FT_BBOX_H
103 #else
104 #  include <freetype/ftbbox.h>
105 #endif /* defined(FT_BBOX_H) */
106 #endif
107 \f
108 /*
109   Annotate semaphores.
110 */
111 static SemaphoreInfo
112   *annotate_semaphore = (SemaphoreInfo *) NULL;
113 \f
114 /*
115   Forward declarations.
116 */
117 static MagickBooleanType
118   RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
119     ExceptionInfo *),
120   RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
121     ExceptionInfo *),
122   RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
123     TypeMetric *,ExceptionInfo *),
124   RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
125     ExceptionInfo *);
126 \f
127 /*
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 %                                                                             %
130 %                                                                             %
131 %                                                                             %
132 +   A n n o t a t e C o m p o n e n t G e n e s i s                           %
133 %                                                                             %
134 %                                                                             %
135 %                                                                             %
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 %
138 %  AnnotateComponentGenesis() instantiates the annotate component.
139 %
140 %  The format of the AnnotateComponentGenesis method is:
141 %
142 %      MagickBooleanType AnnotateComponentGenesis(void)
143 %
144 */
145 MagickPrivate MagickBooleanType AnnotateComponentGenesis(void)
146 {
147   AcquireSemaphoreInfo(&annotate_semaphore);
148   return(MagickTrue);
149 }
150 \f
151 /*
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 %                                                                             %
154 %                                                                             %
155 %                                                                             %
156 +   A n n o t a t e C o m p o n e n t T e r m i n u s                         %
157 %                                                                             %
158 %                                                                             %
159 %                                                                             %
160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 %
162 %  AnnotateComponentTerminus() destroys the annotate component.
163 %
164 %  The format of the AnnotateComponentTerminus method is:
165 %
166 %      AnnotateComponentTerminus(void)
167 %
168 */
169 MagickPrivate void AnnotateComponentTerminus(void)
170 {
171   if (annotate_semaphore == (SemaphoreInfo *) NULL)
172     AcquireSemaphoreInfo(&annotate_semaphore);
173   DestroySemaphoreInfo(&annotate_semaphore);
174 }
175 \f
176 /*
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 %                                                                             %
179 %                                                                             %
180 %                                                                             %
181 %   A n n o t a t e I m a g e                                                 %
182 %                                                                             %
183 %                                                                             %
184 %                                                                             %
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 %
187 %  AnnotateImage() annotates an image with text.  Optionally you can include
188 %  any of the following bits of information about the image by embedding
189 %  the appropriate special characters:
190 %
191 %    %b   file size in bytes.
192 %    %c   comment.
193 %    %d   directory in which the image resides.
194 %    %e   extension of the image file.
195 %    %f   original filename of the image.
196 %    %h   height of image.
197 %    %i   filename of the image.
198 %    %k   number of unique colors.
199 %    %l   image label.
200 %    %m   image file format.
201 %    %n   number of images in a image sequence.
202 %    %o   output image filename.
203 %    %p   page number of the image.
204 %    %q   image depth (8 or 16).
205 %    %q   image depth (8 or 16).
206 %    %s   image scene number.
207 %    %t   image filename without any extension.
208 %    %u   a unique temporary filename.
209 %    %w   image width.
210 %    %x   x resolution of the image.
211 %    %y   y resolution of the image.
212 %
213 %  The format of the AnnotateImage method is:
214 %
215 %      MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
216 %        ExceptionInfo *exception)
217 %
218 %  A description of each parameter follows:
219 %
220 %    o image: the image.
221 %
222 %    o draw_info: the draw info.
223 %
224 %    o exception: return any errors or warnings in this structure.
225 %
226 */
227 MagickExport MagickBooleanType AnnotateImage(Image *image,
228   const DrawInfo *draw_info,ExceptionInfo *exception)
229 {
230   char
231     primitive[MaxTextExtent],
232     **textlist;
233
234   DrawInfo
235     *annotate,
236     *annotate_info;
237
238   GeometryInfo
239     geometry_info;
240
241   MagickBooleanType
242     status;
243
244   PointInfo
245     offset;
246
247   RectangleInfo
248     geometry;
249
250   register ssize_t
251     i;
252
253   size_t
254     length;
255
256   TypeMetric
257     metrics;
258
259   size_t
260     height,
261     number_lines;
262
263   assert(image != (Image *) NULL);
264   assert(image->signature == MagickSignature);
265   if (image->debug != MagickFalse)
266     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
267   assert(draw_info != (DrawInfo *) NULL);
268   assert(draw_info->signature == MagickSignature);
269   if (draw_info->text == (char *) NULL)
270     return(MagickFalse);
271   if (*draw_info->text == '\0')
272     return(MagickTrue);
273   textlist=StringToList(draw_info->text);
274   if (textlist == (char **) NULL)
275     return(MagickFalse);
276   length=strlen(textlist[0]);
277   for (i=1; textlist[i] != (char *) NULL; i++)
278     if (strlen(textlist[i]) > length)
279       length=strlen(textlist[i]);
280   number_lines=(size_t) i;
281   annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
282   annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
283   SetGeometry(image,&geometry);
284   SetGeometryInfo(&geometry_info);
285   if (annotate_info->geometry != (char *) NULL)
286     {
287       (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
288         exception);
289       (void) ParseGeometry(annotate_info->geometry,&geometry_info);
290     }
291   if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
292     return(MagickFalse);
293   if (IsGrayColorspace(image->colorspace) != MagickFalse)
294     (void) TransformImageColorspace(image,sRGBColorspace,exception);
295   status=MagickTrue;
296   for (i=0; textlist[i] != (char *) NULL; i++)
297   {
298     /*
299       Position text relative to image.
300     */
301     annotate_info->affine.tx=geometry_info.xi-image->page.x;
302     annotate_info->affine.ty=geometry_info.psi-image->page.y;
303     (void) CloneString(&annotate->text,textlist[i]);
304     (void) GetTypeMetrics(image,annotate,&metrics,exception);
305     height=(ssize_t) (metrics.ascent-metrics.descent+
306       draw_info->interline_spacing+0.5);
307     switch (annotate->gravity)
308     {
309       case UndefinedGravity:
310       default:
311       {
312         offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
313         offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
314         break;
315       }
316       case NorthWestGravity:
317       {
318         offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
319           annotate_info->affine.ry*height+annotate_info->affine.ry*
320           (metrics.ascent+metrics.descent);
321         offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
322           annotate_info->affine.sy*height+annotate_info->affine.sy*
323           metrics.ascent;
324         break;
325       }
326       case NorthGravity:
327       {
328         offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
329           geometry.width/2.0+i*annotate_info->affine.ry*height-
330           annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
331           annotate_info->affine.ry*(metrics.ascent+metrics.descent);
332         offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
333           annotate_info->affine.sy*height+annotate_info->affine.sy*
334           metrics.ascent-annotate_info->affine.rx*(metrics.width-
335           metrics.bounds.x1)/2.0;
336         break;
337       }
338       case NorthEastGravity:
339       {
340         offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
341           geometry.width+i*annotate_info->affine.ry*height-
342           annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
343           annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
344         offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
345           annotate_info->affine.sy*height+annotate_info->affine.sy*
346           metrics.ascent-annotate_info->affine.rx*(metrics.width-
347           metrics.bounds.x1);
348         break;
349       }
350       case WestGravity:
351       {
352         offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
353           annotate_info->affine.ry*height+annotate_info->affine.ry*
354           (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
355         offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
356           geometry.height/2.0+i*annotate_info->affine.sy*height+
357           annotate_info->affine.sy*(metrics.ascent+metrics.descent-
358           (number_lines-1.0)*height)/2.0;
359         break;
360       }
361       case CenterGravity:
362       {
363         offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
364           geometry.width/2.0+i*annotate_info->affine.ry*height-
365           annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
366           annotate_info->affine.ry*(metrics.ascent+metrics.descent-
367           (number_lines-1)*height)/2.0;
368         offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
369           geometry.height/2.0+i*annotate_info->affine.sy*height-
370           annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
371           annotate_info->affine.sy*(metrics.ascent+metrics.descent-
372           (number_lines-1.0)*height)/2.0;
373         break;
374       }
375       case EastGravity:
376       {
377         offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
378           geometry.width+i*annotate_info->affine.ry*height-
379           annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
380           annotate_info->affine.ry*(metrics.ascent+metrics.descent-
381           (number_lines-1.0)*height)/2.0-1.0;
382         offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
383           geometry.height/2.0+i*annotate_info->affine.sy*height-
384           annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+
385           annotate_info->affine.sy*(metrics.ascent+metrics.descent-
386           (number_lines-1.0)*height)/2.0;
387         break;
388       }
389       case SouthWestGravity:
390       {
391         offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
392           annotate_info->affine.ry*height-annotate_info->affine.ry*
393           (number_lines-1.0)*height;
394         offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
395           geometry.height+i*annotate_info->affine.sy*height-
396           annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
397         break;
398       }
399       case SouthGravity:
400       {
401         offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
402           geometry.width/2.0+i*annotate_info->affine.ry*height-
403           annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
404           annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
405         offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
406           geometry.height+i*annotate_info->affine.sy*height-
407           annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
408           annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
409         break;
410       }
411       case SouthEastGravity:
412       {
413         offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
414           geometry.width+i*annotate_info->affine.ry*height-
415           annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
416           annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
417         offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
418           geometry.height+i*annotate_info->affine.sy*height-
419           annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
420           annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
421         break;
422       }
423     }
424     switch (annotate->align)
425     {
426       case LeftAlign:
427       {
428         offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
429         offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
430         break;
431       }
432       case CenterAlign:
433       {
434         offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
435           annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
436         offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
437           annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
438         break;
439       }
440       case RightAlign:
441       {
442         offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
443           annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
444         offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
445           annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
446         break;
447       }
448       default:
449         break;
450     }
451     if (draw_info->undercolor.alpha != TransparentAlpha)
452       {
453         DrawInfo
454           *undercolor_info;
455
456         /*
457           Text box.
458         */
459         undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
460         undercolor_info->fill=draw_info->undercolor;
461         undercolor_info->affine=draw_info->affine;
462         undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
463         undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
464         (void) FormatLocaleString(primitive,MaxTextExtent,
465           "rectangle 0,0 %g,%.20g",metrics.origin.x,(double) height);
466         (void) CloneString(&undercolor_info->primitive,primitive);
467         (void) DrawImage(image,undercolor_info,exception);
468         (void) DestroyDrawInfo(undercolor_info);
469       }
470     annotate_info->affine.tx=offset.x;
471     annotate_info->affine.ty=offset.y;
472     (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
473       "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
474     if (annotate->decorate == OverlineDecoration)
475       {
476         annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
477           metrics.descent-metrics.underline_position));
478         (void) CloneString(&annotate_info->primitive,primitive);
479         (void) DrawImage(image,annotate_info,exception);
480       }
481     else
482       if (annotate->decorate == UnderlineDecoration)
483         {
484           annotate_info->affine.ty-=(draw_info->affine.sy*
485             metrics.underline_position);
486           (void) CloneString(&annotate_info->primitive,primitive);
487           (void) DrawImage(image,annotate_info,exception);
488         }
489     /*
490       Annotate image with text.
491     */
492     status=RenderType(image,annotate,&offset,&metrics,exception);
493     if (status == MagickFalse)
494       break;
495     if (annotate->decorate == LineThroughDecoration)
496       {
497         annotate_info->affine.ty-=(draw_info->affine.sy*(height+
498           metrics.underline_position+metrics.descent)/2.0);
499         (void) CloneString(&annotate_info->primitive,primitive);
500         (void) DrawImage(image,annotate_info,exception);
501       }
502   }
503   /*
504     Relinquish resources.
505   */
506   annotate_info=DestroyDrawInfo(annotate_info);
507   annotate=DestroyDrawInfo(annotate);
508   for (i=0; textlist[i] != (char *) NULL; i++)
509     textlist[i]=DestroyString(textlist[i]);
510   textlist=(char **) RelinquishMagickMemory(textlist);
511   return(status);
512 }
513 \f
514 /*
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 %                                                                             %
517 %                                                                             %
518 %                                                                             %
519 %  F o r m a t M a g i c k C a p t i o n                                      %
520 %                                                                             %
521 %                                                                             %
522 %                                                                             %
523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 %
525 %  FormatMagickCaption() formats a caption so that it fits within the image
526 %  width.  It returns the number of lines in the formatted caption.
527 %
528 %  The format of the FormatMagickCaption method is:
529 %
530 %      ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
531 %        const MagickBooleanType split,TypeMetric *metrics,char **caption,
532 %        ExceptionInfo *exception)
533 %
534 %  A description of each parameter follows.
535 %
536 %    o image:  The image.
537 %
538 %    o draw_info: the draw info.
539 %
540 %    o split: when no convenient line breaks-- insert newline.
541 %
542 %    o metrics: Return the font metrics in this structure.
543 %
544 %    o caption: the caption.
545 %
546 %    o exception: return any errors or warnings in this structure.
547 %
548 */
549 MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
550   const MagickBooleanType split,TypeMetric *metrics,char **caption,
551   ExceptionInfo *exception)
552 {
553   char
554     *text;
555
556   MagickBooleanType
557     status;
558
559   register char
560     *p,
561     *q,
562     *s;
563
564   register ssize_t
565     i;
566
567   size_t
568     width;
569
570   ssize_t
571     n;
572
573   text=AcquireString(draw_info->text);
574   q=draw_info->text;
575   s=(char *) NULL;
576   for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
577   {
578     if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
579       s=p;
580     if (GetUTFCode(p) == '\n')
581       q=draw_info->text;
582     for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
583       *q++=(*(p+i));
584     *q='\0';
585     status=GetTypeMetrics(image,draw_info,metrics,exception);
586     if (status == MagickFalse)
587       break;
588     width=(size_t) floor(metrics->width+metrics->max_advance/2.0+0.5);
589     if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
590       continue;
591     (void) strcpy(text,draw_info->text);
592     if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
593       {
594         *s='\n';
595         p=s;
596       }
597     else
598       if ((s != (char *) NULL) || (split != MagickFalse))
599         {
600           char
601             *target;
602
603           /*
604             No convenient line breaks-- insert newline.
605           */
606           target=AcquireString(*caption);
607           n=p-(*caption);
608           CopyMagickString(target,*caption,n+1);
609           ConcatenateMagickString(target,"\n",strlen(*caption)+1);
610           ConcatenateMagickString(target,p,strlen(*caption)+2);
611           (void) DestroyString(*caption);
612           *caption=target;
613           p=(*caption)+n;
614         }
615     q=draw_info->text;
616     s=(char *) NULL;
617   }
618   text=DestroyString(text);
619   n=0;
620   for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
621     if (GetUTFCode(p) == '\n')
622       n++;
623   return(n);
624 }
625 \f
626 /*
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 %                                                                             %
629 %                                                                             %
630 %                                                                             %
631 %   G e t M u l t i l i n e T y p e M e t r i c s                             %
632 %                                                                             %
633 %                                                                             %
634 %                                                                             %
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 %
637 %  GetMultilineTypeMetrics() returns the following information for the
638 %  specified font and text:
639 %
640 %    character width
641 %    character height
642 %    ascender
643 %    descender
644 %    text width
645 %    text height
646 %    maximum horizontal advance
647 %    bounds: x1
648 %    bounds: y1
649 %    bounds: x2
650 %    bounds: y2
651 %    origin: x
652 %    origin: y
653 %    underline position
654 %    underline thickness
655 %
656 %  This method is like GetTypeMetrics() but it returns the maximum text width
657 %  and height for multiple lines of text.
658 %
659 %  The format of the GetMultilineTypeMetrics method is:
660 %
661 %      MagickBooleanType GetMultilineTypeMetrics(Image *image,
662 %        const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
663 %
664 %  A description of each parameter follows:
665 %
666 %    o image: the image.
667 %
668 %    o draw_info: the draw info.
669 %
670 %    o metrics: Return the font metrics in this structure.
671 %
672 %    o exception: return any errors or warnings in this structure.
673 %
674 */
675 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
676   const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
677 {
678   char
679     **textlist;
680
681   DrawInfo
682     *annotate_info;
683
684   MagickBooleanType
685     status;
686
687   register ssize_t
688     i;
689
690   TypeMetric
691     extent;
692
693   assert(image != (Image *) NULL);
694   assert(image->signature == MagickSignature);
695   if (image->debug != MagickFalse)
696     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
697   assert(draw_info != (DrawInfo *) NULL);
698   assert(draw_info->text != (char *) NULL);
699   assert(draw_info->signature == MagickSignature);
700   if (*draw_info->text == '\0')
701     return(MagickFalse);
702   annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
703   annotate_info->text=DestroyString(annotate_info->text);
704   /*
705     Convert newlines to multiple lines of text.
706   */
707   textlist=StringToList(draw_info->text);
708   if (textlist == (char **) NULL)
709     return(MagickFalse);
710   annotate_info->render=MagickFalse;
711   annotate_info->direction=UndefinedDirection;
712   (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
713   (void) ResetMagickMemory(&extent,0,sizeof(extent));
714   /*
715     Find the widest of the text lines.
716   */
717   annotate_info->text=textlist[0];
718   status=GetTypeMetrics(image,annotate_info,&extent,exception);
719   *metrics=extent;
720   for (i=1; textlist[i] != (char *) NULL; i++)
721   {
722     annotate_info->text=textlist[i];
723     status=GetTypeMetrics(image,annotate_info,&extent,exception);
724     if (extent.width > metrics->width)
725       *metrics=extent;
726   }
727   metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
728     (i-1)*draw_info->interline_spacing);
729   /*
730     Relinquish resources.
731   */
732   annotate_info->text=(char *) NULL;
733   annotate_info=DestroyDrawInfo(annotate_info);
734   for (i=0; textlist[i] != (char *) NULL; i++)
735     textlist[i]=DestroyString(textlist[i]);
736   textlist=(char **) RelinquishMagickMemory(textlist);
737   return(status);
738 }
739 \f
740 /*
741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
742 %                                                                             %
743 %                                                                             %
744 %                                                                             %
745 %   G e t T y p e M e t r i c s                                               %
746 %                                                                             %
747 %                                                                             %
748 %                                                                             %
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
750 %
751 %  GetTypeMetrics() returns the following information for the specified font
752 %  and text:
753 %
754 %    character width
755 %    character height
756 %    ascender
757 %    descender
758 %    text width
759 %    text height
760 %    maximum horizontal advance
761 %    bounds: x1
762 %    bounds: y1
763 %    bounds: x2
764 %    bounds: y2
765 %    origin: x
766 %    origin: y
767 %    underline position
768 %    underline thickness
769 %
770 %  The format of the GetTypeMetrics method is:
771 %
772 %      MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
773 %        TypeMetric *metrics,ExceptionInfo *exception)
774 %
775 %  A description of each parameter follows:
776 %
777 %    o image: the image.
778 %
779 %    o draw_info: the draw info.
780 %
781 %    o metrics: Return the font metrics in this structure.
782 %
783 %    o exception: return any errors or warnings in this structure.
784 %
785 */
786 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
787   const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
788 {
789   DrawInfo
790     *annotate_info;
791
792   MagickBooleanType
793     status;
794
795   PointInfo
796     offset;
797
798   assert(image != (Image *) NULL);
799   assert(image->signature == MagickSignature);
800   if (image->debug != MagickFalse)
801     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
802   assert(draw_info != (DrawInfo *) NULL);
803   assert(draw_info->text != (char *) NULL);
804   assert(draw_info->signature == MagickSignature);
805   annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
806   annotate_info->render=MagickFalse;
807   annotate_info->direction=UndefinedDirection;
808   (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
809   offset.x=0.0;
810   offset.y=0.0;
811   status=RenderType(image,annotate_info,&offset,metrics,exception);
812   if (image->debug != MagickFalse)
813     (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
814       "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
815       "bounds: %g,%g  %g,%g; origin: %g,%g; pixels per em: %g,%g; "
816       "underline position: %g; underline thickness: %g",annotate_info->text,
817       metrics->width,metrics->height,metrics->ascent,metrics->descent,
818       metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
819       metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
820       metrics->pixels_per_em.x,metrics->pixels_per_em.y,
821       metrics->underline_position,metrics->underline_thickness);
822   annotate_info=DestroyDrawInfo(annotate_info);
823   return(status);
824 }
825 \f
826 /*
827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828 %                                                                             %
829 %                                                                             %
830 %                                                                             %
831 +   R e n d e r T y p e                                                       %
832 %                                                                             %
833 %                                                                             %
834 %                                                                             %
835 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
836 %
837 %  RenderType() renders text on the image.  It also returns the bounding box of
838 %  the text relative to the image.
839 %
840 %  The format of the RenderType method is:
841 %
842 %      MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
843 %        const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
844 %
845 %  A description of each parameter follows:
846 %
847 %    o image: the image.
848 %
849 %    o draw_info: the draw info.
850 %
851 %    o offset: (x,y) location of text relative to image.
852 %
853 %    o metrics: bounding box of text.
854 %
855 %    o exception: return any errors or warnings in this structure.
856 %
857 */
858 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
859   const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
860 {
861   const TypeInfo
862     *type_info;
863
864   DrawInfo
865     *annotate_info;
866
867   MagickBooleanType
868     status;
869
870   type_info=(const TypeInfo *) NULL;
871   if (draw_info->font != (char *) NULL)
872     {
873       if (*draw_info->font == '@')
874         {
875           status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
876             metrics,exception);
877           return(status);
878         }
879       if (*draw_info->font == '-')
880         return(RenderX11(image,draw_info,offset,metrics,exception));
881       if (IsPathAccessible(draw_info->font) != MagickFalse)
882         {
883           status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
884             metrics,exception);
885           return(status);
886         }
887       type_info=GetTypeInfo(draw_info->font,exception);
888       if (type_info == (const TypeInfo *) NULL)
889         (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
890           "UnableToReadFont","`%s'",draw_info->font);
891     }
892   if ((type_info == (const TypeInfo *) NULL) &&
893       (draw_info->family != (const char *) NULL))
894     {
895       type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
896         draw_info->stretch,draw_info->weight,exception);
897       if (type_info == (const TypeInfo *) NULL)
898         (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
899           "UnableToReadFont","`%s'",draw_info->family);
900     }
901   if (type_info == (const TypeInfo *) NULL)
902     type_info=GetTypeInfoByFamily("Arial",draw_info->style,
903       draw_info->stretch,draw_info->weight,exception);
904   if (type_info == (const TypeInfo *) NULL)
905     type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
906       draw_info->stretch,draw_info->weight,exception);
907   if (type_info == (const TypeInfo *) NULL)
908     type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
909       draw_info->stretch,draw_info->weight,exception);
910   if (type_info == (const TypeInfo *) NULL)
911     type_info=GetTypeInfoByFamily("Sans",draw_info->style,
912       draw_info->stretch,draw_info->weight,exception);
913   if (type_info == (const TypeInfo *) NULL)
914     type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
915       draw_info->stretch,draw_info->weight,exception);
916   if (type_info == (const TypeInfo *) NULL)
917     type_info=GetTypeInfo("*",exception);
918   if (type_info == (const TypeInfo *) NULL)
919     {
920       status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
921         exception);
922       return(status);
923     }
924   annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
925   annotate_info->face=type_info->face;
926   if (type_info->metrics != (char *) NULL)
927     (void) CloneString(&annotate_info->metrics,type_info->metrics);
928   if (type_info->glyphs != (char *) NULL)
929     (void) CloneString(&annotate_info->font,type_info->glyphs);
930   status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
931     exception);
932   annotate_info=DestroyDrawInfo(annotate_info);
933   return(status);
934 }
935 \f
936 /*
937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938 %                                                                             %
939 %                                                                             %
940 %                                                                             %
941 +   R e n d e r F r e e t y p e                                               %
942 %                                                                             %
943 %                                                                             %
944 %                                                                             %
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946 %
947 %  RenderFreetype() renders text on the image with a Truetype font.  It also
948 %  returns the bounding box of the text relative to the image.
949 %
950 %  The format of the RenderFreetype method is:
951 %
952 %      MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
953 %        const char *encoding,const PointInfo *offset,TypeMetric *metrics,
954 %        ExceptionInfo *exception)
955 %
956 %  A description of each parameter follows:
957 %
958 %    o image: the image.
959 %
960 %    o draw_info: the draw info.
961 %
962 %    o encoding: the font encoding.
963 %
964 %    o offset: (x,y) location of text relative to image.
965 %
966 %    o metrics: bounding box of text.
967 %
968 %    o exception: return any errors or warnings in this structure.
969 %
970 */
971
972 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
973
974 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
975   DrawInfo *draw_info)
976 {
977   AffineMatrix
978     affine;
979
980   char
981     path[MaxTextExtent];
982
983   affine=draw_info->affine;
984   (void) FormatLocaleString(path,MaxTextExtent,
985     "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
986     p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
987     affine.ty-to->y/64.0);
988   (void) ConcatenateString(&draw_info->primitive,path);
989   return(0);
990 }
991
992 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
993 {
994   AffineMatrix
995     affine;
996
997   char
998     path[MaxTextExtent];
999
1000   affine=draw_info->affine;
1001   (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
1002     to->x/64.0,affine.ty-to->y/64.0);
1003   (void) ConcatenateString(&draw_info->primitive,path);
1004   return(0);
1005 }
1006
1007 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1008 {
1009   AffineMatrix
1010     affine;
1011
1012   char
1013     path[MaxTextExtent];
1014
1015   affine=draw_info->affine;
1016   (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
1017     to->x/64.0,affine.ty-to->y/64.0);
1018   (void) ConcatenateString(&draw_info->primitive,path);
1019   return(0);
1020 }
1021
1022 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1023   DrawInfo *draw_info)
1024 {
1025   AffineMatrix
1026     affine;
1027
1028   char
1029     path[MaxTextExtent];
1030
1031   affine=draw_info->affine;
1032   (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
1033     affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1034     affine.ty-to->y/64.0);
1035   (void) ConcatenateString(&draw_info->primitive,path);
1036   return(0);
1037 }
1038
1039 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1040   const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1041   ExceptionInfo *exception)
1042 {
1043 #if !defined(FT_OPEN_PATHNAME)
1044 #define FT_OPEN_PATHNAME  ft_open_pathname
1045 #endif
1046
1047   typedef struct _GlyphInfo
1048   {
1049     FT_UInt
1050       id;
1051
1052     FT_Vector
1053       origin;
1054
1055     FT_Glyph
1056       image;
1057   } GlyphInfo;
1058
1059   const char
1060     *value;
1061
1062   double
1063     direction;
1064
1065   DrawInfo
1066     *annotate_info;
1067
1068   FT_BBox
1069     bounds;
1070
1071   FT_BitmapGlyph
1072     bitmap;
1073
1074   FT_Encoding
1075     encoding_type;
1076
1077   FT_Error
1078     status;
1079
1080   FT_Face
1081     face;
1082
1083   FT_Int32
1084     flags;
1085
1086   FT_Library
1087     library;
1088
1089   FT_Matrix
1090     affine;
1091
1092   FT_Open_Args
1093     args;
1094
1095   FT_Vector
1096     origin;
1097
1098   GlyphInfo
1099     glyph,
1100     last_glyph;
1101
1102   PointInfo
1103     point,
1104     resolution;
1105
1106   register char
1107     *p;
1108
1109   ssize_t
1110     code,
1111     y;
1112
1113   static FT_Outline_Funcs
1114     OutlineMethods =
1115     {
1116       (FT_Outline_MoveTo_Func) TraceMoveTo,
1117       (FT_Outline_LineTo_Func) TraceLineTo,
1118       (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1119       (FT_Outline_CubicTo_Func) TraceCubicBezier,
1120       0, 0
1121     };
1122
1123   unsigned char
1124     *utf8;
1125
1126   /*
1127     Initialize Truetype library.
1128   */
1129   status=FT_Init_FreeType(&library);
1130   if (status != 0)
1131     ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1132       image->filename);
1133   args.flags=FT_OPEN_PATHNAME;
1134   if (draw_info->font == (char *) NULL)
1135     args.pathname=ConstantString("helvetica");
1136   else
1137     if (*draw_info->font != '@')
1138       args.pathname=ConstantString(draw_info->font);
1139     else
1140       args.pathname=ConstantString(draw_info->font+1);
1141   face=(FT_Face) NULL;
1142   status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
1143   args.pathname=DestroyString(args.pathname);
1144   if (status != 0)
1145     {
1146       (void) FT_Done_FreeType(library);
1147       (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
1148         "UnableToReadFont","`%s'",draw_info->font);
1149       return(RenderPostscript(image,draw_info,offset,metrics,exception));
1150     }
1151   if ((draw_info->metrics != (char *) NULL) &&
1152       (IsPathAccessible(draw_info->metrics) != MagickFalse))
1153     (void) FT_Attach_File(face,draw_info->metrics);
1154   encoding_type=ft_encoding_unicode;
1155   status=FT_Select_Charmap(face,encoding_type);
1156   if ((status != 0) && (face->num_charmaps != 0))
1157     status=FT_Set_Charmap(face,face->charmaps[0]);
1158   if (encoding != (const char *) NULL)
1159     {
1160       if (LocaleCompare(encoding,"AdobeCustom") == 0)
1161         encoding_type=ft_encoding_adobe_custom;
1162       if (LocaleCompare(encoding,"AdobeExpert") == 0)
1163         encoding_type=ft_encoding_adobe_expert;
1164       if (LocaleCompare(encoding,"AdobeStandard") == 0)
1165         encoding_type=ft_encoding_adobe_standard;
1166       if (LocaleCompare(encoding,"AppleRoman") == 0)
1167         encoding_type=ft_encoding_apple_roman;
1168       if (LocaleCompare(encoding,"BIG5") == 0)
1169         encoding_type=ft_encoding_big5;
1170       if (LocaleCompare(encoding,"GB2312") == 0)
1171         encoding_type=ft_encoding_gb2312;
1172       if (LocaleCompare(encoding,"Johab") == 0)
1173         encoding_type=ft_encoding_johab;
1174 #if defined(ft_encoding_latin_1)
1175       if (LocaleCompare(encoding,"Latin-1") == 0)
1176         encoding_type=ft_encoding_latin_1;
1177 #endif
1178       if (LocaleCompare(encoding,"Latin-2") == 0)
1179         encoding_type=ft_encoding_latin_2;
1180       if (LocaleCompare(encoding,"None") == 0)
1181         encoding_type=ft_encoding_none;
1182       if (LocaleCompare(encoding,"SJIScode") == 0)
1183         encoding_type=ft_encoding_sjis;
1184       if (LocaleCompare(encoding,"Symbol") == 0)
1185         encoding_type=ft_encoding_symbol;
1186       if (LocaleCompare(encoding,"Unicode") == 0)
1187         encoding_type=ft_encoding_unicode;
1188       if (LocaleCompare(encoding,"Wansung") == 0)
1189         encoding_type=ft_encoding_wansung;
1190       status=FT_Select_Charmap(face,encoding_type);
1191       if (status != 0)
1192         ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1193     }
1194   /*
1195     Set text size.
1196   */
1197   resolution.x=DefaultResolution;
1198   resolution.y=DefaultResolution;
1199   if (draw_info->density != (char *) NULL)
1200     {
1201       GeometryInfo
1202         geometry_info;
1203
1204       MagickStatusType
1205         flags;
1206
1207       flags=ParseGeometry(draw_info->density,&geometry_info);
1208       resolution.x=geometry_info.rho;
1209       resolution.y=geometry_info.sigma;
1210       if ((flags & SigmaValue) == 0)
1211         resolution.y=resolution.x;
1212     }
1213   status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1214     (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1215     (FT_UInt) resolution.y);
1216   metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1217   metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1218   metrics->ascent=(double) face->size->metrics.ascender/64.0;
1219   metrics->descent=(double) face->size->metrics.descender/64.0;
1220   metrics->width=0;
1221   metrics->origin.x=0;
1222   metrics->origin.y=0;
1223   metrics->height=(double) face->size->metrics.height/64.0;
1224   metrics->max_advance=0.0;
1225   if (face->size->metrics.max_advance > MagickEpsilon)
1226     metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1227   metrics->bounds.x1=0.0;
1228   metrics->bounds.y1=metrics->descent;
1229   metrics->bounds.x2=metrics->ascent+metrics->descent;
1230   metrics->bounds.y2=metrics->ascent+metrics->descent;
1231   metrics->underline_position=face->underline_position/64.0;
1232   metrics->underline_thickness=face->underline_thickness/64.0;
1233   if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
1234     {
1235       (void) FT_Done_Face(face);
1236       (void) FT_Done_FreeType(library);
1237       return(MagickTrue);
1238     }
1239   /*
1240     Compute bounding box.
1241   */
1242   if (image->debug != MagickFalse)
1243     (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1244       "font-encoding %s; text-encoding %s; pointsize %g",
1245       draw_info->font != (char *) NULL ? draw_info->font : "none",
1246       encoding != (char *) NULL ? encoding : "none",
1247       draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1248       draw_info->pointsize);
1249   flags=FT_LOAD_NO_BITMAP;
1250   if (draw_info->text_antialias == MagickFalse)
1251     flags|=FT_LOAD_TARGET_MONO;
1252   else
1253     {
1254 #if defined(FT_LOAD_TARGET_LIGHT)
1255       flags|=FT_LOAD_TARGET_LIGHT;
1256 #elif defined(FT_LOAD_TARGET_LCD)
1257       flags|=FT_LOAD_TARGET_LCD;
1258 #endif
1259     }
1260   value=GetImageProperty(image,"type:hinting",exception);
1261   if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1262     flags|=FT_LOAD_NO_HINTING;
1263   glyph.id=0;
1264   glyph.image=NULL;
1265   last_glyph.id=0;
1266   last_glyph.image=NULL;
1267   origin.x=0;
1268   origin.y=0;
1269   affine.xx=65536L;
1270   affine.yx=0L;
1271   affine.xy=0L;
1272   affine.yy=65536L;
1273   if (draw_info->render != MagickFalse)
1274     {
1275       affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1276       affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1277       affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1278       affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1279     }
1280   annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1281   (void) CloneString(&annotate_info->primitive,"path '");
1282   if (draw_info->render != MagickFalse)
1283     {
1284       if (image->storage_class != DirectClass)
1285         (void) SetImageStorageClass(image,DirectClass,exception);
1286       if (image->alpha_trait != BlendPixelTrait)
1287         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1288     }
1289   direction=1.0;
1290   if (draw_info->direction == RightToLeftDirection)
1291     direction=(-1.0);
1292   point.x=0.0;
1293   point.y=0.0;
1294   for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1295     if (GetUTFCode(p) < 0)
1296       break;
1297   utf8=(unsigned char *) NULL;
1298   if (GetUTFCode(p) == 0)
1299     p=draw_info->text;
1300   else
1301     {
1302       utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1303       if (utf8 != (unsigned char *) NULL)
1304         p=(char *) utf8;
1305     }
1306   for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1307   {
1308     /*
1309       Render UTF-8 sequence.
1310     */
1311     glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1312     if (glyph.id == 0)
1313       glyph.id=FT_Get_Char_Index(face,'?');
1314     if ((glyph.id != 0) && (last_glyph.id != 0))
1315       {
1316         if (fabs(draw_info->kerning) >= MagickEpsilon)
1317           origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
1318         else
1319           if (FT_HAS_KERNING(face))
1320             {
1321               FT_Vector
1322                 kerning;
1323
1324               status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1325                 ft_kerning_default,&kerning);
1326               if (status == 0)
1327                 origin.x+=(FT_Pos) (direction*kerning.x);
1328             }
1329         }
1330     glyph.origin=origin;
1331     status=FT_Load_Glyph(face,glyph.id,flags);
1332     if (status != 0)
1333       continue;
1334     status=FT_Get_Glyph(face->glyph,&glyph.image);
1335     if (status != 0)
1336       continue;
1337     status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1338       &bounds);
1339     if (status != 0)
1340       continue;
1341     if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
1342       metrics->bounds.x1=(double) bounds.xMin;
1343     if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
1344       metrics->bounds.y1=(double) bounds.yMin;
1345     if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
1346       metrics->bounds.x2=(double) bounds.xMax;
1347     if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
1348       metrics->bounds.y2=(double) bounds.yMax;
1349     if (draw_info->render != MagickFalse)
1350       if ((draw_info->stroke.alpha != TransparentAlpha) ||
1351           (draw_info->stroke_pattern != (Image *) NULL))
1352         {
1353           /*
1354             Trace the glyph.
1355           */
1356           annotate_info->affine.tx=glyph.origin.x/64.0;
1357           annotate_info->affine.ty=glyph.origin.y/64.0;
1358           (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1359             outline,&OutlineMethods,annotate_info);
1360         }
1361     FT_Vector_Transform(&glyph.origin,&affine);
1362     (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1363     status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1364       (FT_Vector *) NULL,MagickTrue);
1365     if (status != 0)
1366       continue;
1367     bitmap=(FT_BitmapGlyph) glyph.image;
1368     point.x=offset->x+bitmap->left;
1369     if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1370       point.x=offset->x+(origin.x >> 6);
1371     point.y=offset->y-bitmap->top;
1372     if (draw_info->render != MagickFalse)
1373       {
1374         CacheView
1375           *image_view;
1376
1377         MagickBooleanType
1378           status;
1379
1380         register unsigned char
1381           *p;
1382
1383         /*
1384           Rasterize the glyph.
1385         */
1386         status=MagickTrue;
1387         image_view=AcquireAuthenticCacheView(image,exception);
1388         p=bitmap->bitmap.buffer;
1389         for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1390         {
1391           MagickBooleanType
1392             active,
1393             sync;
1394
1395           double
1396             fill_opacity;
1397
1398           PixelInfo
1399             fill_color;
1400
1401           register Quantum
1402             *restrict q;
1403
1404           register ssize_t
1405             x;
1406
1407           ssize_t
1408             n,
1409             x_offset,
1410             y_offset;
1411
1412           if (status == MagickFalse)
1413             continue;
1414           x_offset=(ssize_t) ceil(point.x-0.5);
1415           y_offset=(ssize_t) ceil(point.y+y-0.5);
1416           if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1417             continue;
1418           q=(Quantum *) NULL;
1419           if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1420             active=MagickFalse;
1421           else
1422             {
1423               q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1424                 bitmap->bitmap.width,1,exception);
1425               active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1426             }
1427           n=y*bitmap->bitmap.pitch-1;
1428           for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
1429           {
1430             n++;
1431             x_offset++;
1432             if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1433               {
1434                 q+=GetPixelChannels(image);
1435                 continue;
1436               }
1437             if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
1438               fill_opacity=(double) (p[n])/(bitmap->bitmap.num_grays-1);
1439             else
1440               fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1441                 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1442             if (draw_info->text_antialias == MagickFalse)
1443               fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1444             if (active == MagickFalse)
1445               q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1446                 exception);
1447             if (q == (Quantum *) NULL)
1448               {
1449                 q+=GetPixelChannels(image);
1450                 continue;
1451               }
1452             GetPixelInfo(image,&fill_color);
1453             (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1454               exception);
1455             fill_opacity=fill_opacity*fill_color.alpha;
1456             CompositePixelOver(image,&fill_color,fill_opacity,q,
1457               GetPixelAlpha(image,q),q);
1458             if (active == MagickFalse)
1459               {
1460                 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1461                 if (sync == MagickFalse)
1462                   status=MagickFalse;
1463               }
1464             q+=GetPixelChannels(image);
1465           }
1466           sync=SyncCacheViewAuthenticPixels(image_view,exception);
1467           if (sync == MagickFalse)
1468             status=MagickFalse;
1469         }
1470         image_view=DestroyCacheView(image_view);
1471       }
1472     if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1473       metrics->width=bitmap->left+bitmap->bitmap.width;
1474     if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1475         (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1476         (IsUTFSpace(code) == MagickFalse))
1477       origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
1478     else
1479       origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
1480     metrics->origin.x=(double) origin.x;
1481     metrics->origin.y=(double) origin.y;
1482     if (last_glyph.id != 0)
1483       FT_Done_Glyph(last_glyph.image);
1484     last_glyph=glyph;
1485     code=GetUTFCode(p);
1486   }
1487   if (utf8 != (unsigned char *) NULL)
1488     utf8=(unsigned char *) RelinquishMagickMemory(utf8);
1489   if (last_glyph.id != 0)
1490     FT_Done_Glyph(last_glyph.image);
1491   if ((draw_info->stroke.alpha != TransparentAlpha) ||
1492       (draw_info->stroke_pattern != (Image *) NULL))
1493     {
1494       if (draw_info->render != MagickFalse)
1495         {
1496           /*
1497             Draw text stroke.
1498           */
1499           annotate_info->linejoin=RoundJoin;
1500           annotate_info->affine.tx=offset->x;
1501           annotate_info->affine.ty=offset->y;
1502           (void) ConcatenateString(&annotate_info->primitive,"'");
1503           (void) DrawImage(image,annotate_info,exception);
1504         }
1505       }
1506   /*
1507     Determine font metrics.
1508   */
1509   glyph.id=FT_Get_Char_Index(face,'_');
1510   glyph.origin=origin;
1511   status=FT_Load_Glyph(face,glyph.id,flags);
1512   if (status == 0)
1513     {
1514       status=FT_Get_Glyph(face->glyph,&glyph.image);
1515       if (status == 0)
1516         {
1517           status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1518             &bounds);
1519           if (status == 0)
1520             {
1521               FT_Vector_Transform(&glyph.origin,&affine);
1522               (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1523               status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1524                 (FT_Vector *) NULL,MagickTrue);
1525               bitmap=(FT_BitmapGlyph) glyph.image;
1526               if (bitmap->left > metrics->width)
1527                 metrics->width=bitmap->left;
1528             }
1529         }
1530       FT_Done_Glyph(glyph.image);
1531     }
1532   metrics->width-=metrics->bounds.x1/64.0;
1533   metrics->width+=annotate_info->stroke_width;
1534   metrics->bounds.x1/=64.0;
1535   metrics->bounds.y1/=64.0;
1536   metrics->bounds.x2/=64.0;
1537   metrics->bounds.y2/=64.0;
1538   metrics->origin.x/=64.0;
1539   metrics->origin.y/=64.0;
1540   /*
1541     Relinquish resources.
1542   */
1543   annotate_info=DestroyDrawInfo(annotate_info);
1544   (void) FT_Done_Face(face);
1545   (void) FT_Done_FreeType(library);
1546   return(MagickTrue);
1547 }
1548 #else
1549 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1550   const char *magick_unused(encoding),const PointInfo *offset,
1551   TypeMetric *metrics,ExceptionInfo *exception)
1552 {
1553   (void) ThrowMagickException(exception,GetMagickModule(),
1554     MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
1555     draw_info->font != (char *) NULL ? draw_info->font : "none");
1556   return(RenderPostscript(image,draw_info,offset,metrics,exception));
1557 }
1558 #endif
1559 \f
1560 /*
1561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1562 %                                                                             %
1563 %                                                                             %
1564 %                                                                             %
1565 +   R e n d e r P o s t s c r i p t                                           %
1566 %                                                                             %
1567 %                                                                             %
1568 %                                                                             %
1569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1570 %
1571 %  RenderPostscript() renders text on the image with a Postscript font.  It
1572 %  also returns the bounding box of the text relative to the image.
1573 %
1574 %  The format of the RenderPostscript method is:
1575 %
1576 %      MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1577 %        const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1578 %
1579 %  A description of each parameter follows:
1580 %
1581 %    o image: the image.
1582 %
1583 %    o draw_info: the draw info.
1584 %
1585 %    o offset: (x,y) location of text relative to image.
1586 %
1587 %    o metrics: bounding box of text.
1588 %
1589 %    o exception: return any errors or warnings in this structure.
1590 %
1591 */
1592
1593 static inline size_t MagickMin(const size_t x,const size_t y)
1594 {
1595   if (x < y)
1596     return(x);
1597   return(y);
1598 }
1599
1600 static char *EscapeParenthesis(const char *text)
1601 {
1602   char
1603     *buffer;
1604
1605   register char
1606     *p;
1607
1608   register ssize_t
1609     i;
1610
1611   size_t
1612     escapes;
1613
1614   escapes=0;
1615   buffer=AcquireString(text);
1616   p=buffer;
1617   for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
1618   {
1619     if ((text[i] == '(') || (text[i] == ')'))
1620       {
1621         *p++='\\';
1622         escapes++;
1623       }
1624     *p++=text[i];
1625   }
1626   *p='\0';
1627   return(buffer);
1628 }
1629
1630 static MagickBooleanType RenderPostscript(Image *image,
1631   const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1632   ExceptionInfo *exception)
1633 {
1634   char
1635     filename[MaxTextExtent],
1636     geometry[MaxTextExtent],
1637     *text;
1638
1639   FILE
1640     *file;
1641
1642   Image
1643     *annotate_image;
1644
1645   ImageInfo
1646     *annotate_info;
1647
1648   int
1649     unique_file;
1650
1651   MagickBooleanType
1652     identity;
1653
1654   PointInfo
1655     extent,
1656     point,
1657     resolution;
1658
1659   register ssize_t
1660     i;
1661
1662   size_t
1663     length;
1664
1665   ssize_t
1666     y;
1667
1668   /*
1669     Render label with a Postscript font.
1670   */
1671   if (image->debug != MagickFalse)
1672     (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
1673       "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
1674       draw_info->font : "none",draw_info->pointsize);
1675   file=(FILE *) NULL;
1676   unique_file=AcquireUniqueFileResource(filename);
1677   if (unique_file != -1)
1678     file=fdopen(unique_file,"wb");
1679   if ((unique_file == -1) || (file == (FILE *) NULL))
1680     {
1681       ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
1682       return(MagickFalse);
1683     }
1684   (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1685   (void) FormatLocaleFile(file,"/ReencodeType\n");
1686   (void) FormatLocaleFile(file,"{\n");
1687   (void) FormatLocaleFile(file,"  findfont dup length\n");
1688   (void) FormatLocaleFile(file,
1689     "  dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
1690   (void) FormatLocaleFile(file,
1691     "  /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
1692   (void) FormatLocaleFile(file,"} bind def\n");
1693   /*
1694     Sample to compute bounding box.
1695   */
1696   identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1697     (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1698     (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
1699   extent.x=0.0;
1700   extent.y=0.0;
1701   length=strlen(draw_info->text);
1702   for (i=0; i <= (ssize_t) (length+2); i++)
1703   {
1704     point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1705       draw_info->affine.ry*2.0*draw_info->pointsize);
1706     point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1707       draw_info->affine.sy*2.0*draw_info->pointsize);
1708     if (point.x > extent.x)
1709       extent.x=point.x;
1710     if (point.y > extent.y)
1711       extent.y=point.y;
1712   }
1713   (void) FormatLocaleFile(file,"%g %g moveto\n",identity  != MagickFalse ? 0.0 :
1714     extent.x/2.0,extent.y/2.0);
1715   (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
1716     draw_info->pointsize);
1717   if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1718       (strchr(draw_info->font,'/') != (char *) NULL))
1719     (void) FormatLocaleFile(file,
1720       "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1721   else
1722     (void) FormatLocaleFile(file,
1723       "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1724       draw_info->font);
1725   (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
1726     draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1727     draw_info->affine.sy);
1728   text=EscapeParenthesis(draw_info->text);
1729   if (identity == MagickFalse)
1730     (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1731       text);
1732   (void) FormatLocaleFile(file,"(%s) show\n",text);
1733   text=DestroyString(text);
1734   (void) FormatLocaleFile(file,"showpage\n");
1735   (void) fclose(file);
1736   (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
1737     floor(extent.x+0.5),floor(extent.y+0.5));
1738   annotate_info=AcquireImageInfo();
1739   (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
1740     filename);
1741   (void) CloneString(&annotate_info->page,geometry);
1742   if (draw_info->density != (char *) NULL)
1743     (void) CloneString(&annotate_info->density,draw_info->density);
1744   annotate_info->antialias=draw_info->text_antialias;
1745   annotate_image=ReadImage(annotate_info,exception);
1746   CatchException(exception);
1747   annotate_info=DestroyImageInfo(annotate_info);
1748   (void) RelinquishUniqueFileResource(filename);
1749   if (annotate_image == (Image *) NULL)
1750     return(MagickFalse);
1751   (void) NegateImage(annotate_image,MagickFalse,exception);
1752   resolution.x=DefaultResolution;
1753   resolution.y=DefaultResolution;
1754   if (draw_info->density != (char *) NULL)
1755     {
1756       GeometryInfo
1757         geometry_info;
1758
1759       MagickStatusType
1760         flags;
1761
1762       flags=ParseGeometry(draw_info->density,&geometry_info);
1763       resolution.x=geometry_info.rho;
1764       resolution.y=geometry_info.sigma;
1765       if ((flags & SigmaValue) == 0)
1766         resolution.y=resolution.x;
1767     }
1768   if (identity == MagickFalse)
1769     (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
1770   else
1771     {
1772       RectangleInfo
1773         crop_info;
1774
1775       crop_info=GetImageBoundingBox(annotate_image,exception);
1776       crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
1777         ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
1778       crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
1779         0.5);
1780       (void) FormatLocaleString(geometry,MaxTextExtent,
1781         "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
1782         crop_info.height,(double) crop_info.x,(double) crop_info.y);
1783       (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
1784     }
1785   metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1786     ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1787   metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1788   metrics->ascent=metrics->pixels_per_em.x;
1789   metrics->descent=metrics->pixels_per_em.y/-5.0;
1790   metrics->width=(double) annotate_image->columns/
1791     ExpandAffine(&draw_info->affine);
1792   metrics->height=1.152*metrics->pixels_per_em.x;
1793   metrics->max_advance=metrics->pixels_per_em.x;
1794   metrics->bounds.x1=0.0;
1795   metrics->bounds.y1=metrics->descent;
1796   metrics->bounds.x2=metrics->ascent+metrics->descent;
1797   metrics->bounds.y2=metrics->ascent+metrics->descent;
1798   metrics->underline_position=(-2.0);
1799   metrics->underline_thickness=1.0;
1800   if (draw_info->render == MagickFalse)
1801     {
1802       annotate_image=DestroyImage(annotate_image);
1803       return(MagickTrue);
1804     }
1805   if (draw_info->fill.alpha != TransparentAlpha)
1806     {
1807       CacheView
1808         *annotate_view;
1809
1810       MagickBooleanType
1811         sync;
1812
1813       PixelInfo
1814         fill_color;
1815
1816       /*
1817         Render fill color.
1818       */
1819       if (image->alpha_trait != BlendPixelTrait)
1820         (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1821       if (annotate_image->alpha_trait != BlendPixelTrait)
1822         (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1823           exception);
1824       fill_color=draw_info->fill;
1825       annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
1826       for (y=0; y < (ssize_t) annotate_image->rows; y++)
1827       {
1828         register ssize_t
1829           x;
1830
1831         register Quantum
1832           *restrict q;
1833
1834         q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1835           1,exception);
1836         if (q == (Quantum *) NULL)
1837           break;
1838         for (x=0; x < (ssize_t) annotate_image->columns; x++)
1839         {
1840           (void) GetFillColor(draw_info,x,y,&fill_color,exception);
1841           SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1842             GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
1843           SetPixelRed(annotate_image,fill_color.red,q);
1844           SetPixelGreen(annotate_image,fill_color.green,q);
1845           SetPixelBlue(annotate_image,fill_color.blue,q);
1846           q+=GetPixelChannels(annotate_image);
1847         }
1848         sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1849         if (sync == MagickFalse)
1850           break;
1851       }
1852       annotate_view=DestroyCacheView(annotate_view);
1853       (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
1854         (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
1855         metrics->descent)-0.5),exception);
1856     }
1857   annotate_image=DestroyImage(annotate_image);
1858   return(MagickTrue);
1859 }
1860 \f
1861 /*
1862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1863 %                                                                             %
1864 %                                                                             %
1865 %                                                                             %
1866 +   R e n d e r X 1 1                                                         %
1867 %                                                                             %
1868 %                                                                             %
1869 %                                                                             %
1870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1871 %
1872 %  RenderX11() renders text on the image with an X11 font.  It also returns the
1873 %  bounding box of the text relative to the image.
1874 %
1875 %  The format of the RenderX11 method is:
1876 %
1877 %      MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
1878 %        const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1879 %
1880 %  A description of each parameter follows:
1881 %
1882 %    o image: the image.
1883 %
1884 %    o draw_info: the draw info.
1885 %
1886 %    o offset: (x,y) location of text relative to image.
1887 %
1888 %    o metrics: bounding box of text.
1889 %
1890 %    o exception: return any errors or warnings in this structure.
1891 %
1892 */
1893 #if defined(MAGICKCORE_X11_DELEGATE)
1894 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1895   const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1896 {
1897   MagickBooleanType
1898     status;
1899
1900   static DrawInfo
1901     cache_info;
1902
1903   static Display
1904     *display = (Display *) NULL;
1905
1906   static XAnnotateInfo
1907     annotate_info;
1908
1909   static XFontStruct
1910     *font_info;
1911
1912   static XPixelInfo
1913     pixel;
1914
1915   static XResourceInfo
1916     resource_info;
1917
1918   static XrmDatabase
1919     resource_database;
1920
1921   static XStandardColormap
1922     *map_info;
1923
1924   static XVisualInfo
1925     *visual_info;
1926
1927   size_t
1928     height,
1929     width;
1930
1931   if (annotate_semaphore == (SemaphoreInfo *) NULL)
1932     AcquireSemaphoreInfo(&annotate_semaphore);
1933   LockSemaphoreInfo(annotate_semaphore);
1934   if (display == (Display *) NULL)
1935     {
1936       const char
1937         *client_name;
1938
1939       ImageInfo
1940         *image_info;
1941
1942       /*
1943         Open X server connection.
1944       */
1945       display=XOpenDisplay(draw_info->server_name);
1946       if (display == (Display *) NULL)
1947         {
1948           UnlockSemaphoreInfo(annotate_semaphore);
1949           ThrowXWindowException(XServerError,"UnableToOpenXServer",
1950             draw_info->server_name);
1951           return(MagickFalse);
1952         }
1953       /*
1954         Get user defaults from X resource database.
1955       */
1956       (void) XSetErrorHandler(XError);
1957       image_info=AcquireImageInfo();
1958       client_name=GetClientName();
1959       resource_database=XGetResourceDatabase(display,client_name);
1960       XGetResourceInfo(image_info,resource_database,client_name,
1961         &resource_info);
1962       resource_info.close_server=MagickFalse;
1963       resource_info.colormap=PrivateColormap;
1964       resource_info.font=AcquireString(draw_info->font);
1965       resource_info.background_color=AcquireString("#ffffffffffff");
1966       resource_info.foreground_color=AcquireString("#000000000000");
1967       map_info=XAllocStandardColormap();
1968       if (map_info == (XStandardColormap *) NULL)
1969         {
1970           UnlockSemaphoreInfo(annotate_semaphore);
1971           ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
1972             image->filename);
1973           return(MagickFalse);
1974         }
1975       /*
1976         Initialize visual info.
1977       */
1978       visual_info=XBestVisualInfo(display,map_info,&resource_info);
1979       if (visual_info == (XVisualInfo *) NULL)
1980         {
1981           UnlockSemaphoreInfo(annotate_semaphore);
1982           ThrowXWindowException(XServerError,"UnableToGetVisual",
1983             image->filename);
1984           return(MagickFalse);
1985         }
1986       map_info->colormap=(Colormap) NULL;
1987       pixel.pixels=(unsigned long *) NULL;
1988       /*
1989         Initialize Standard Colormap info.
1990       */
1991       XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen),
1992         map_info);
1993       XGetPixelInfo(display,visual_info,map_info,&resource_info,(Image *) NULL,
1994         &pixel);
1995       pixel.annotate_context=XDefaultGC(display,visual_info->screen);
1996       /*
1997         Initialize font info.
1998       */
1999       font_info=XBestFont(display,&resource_info,MagickFalse);
2000       if (font_info == (XFontStruct *) NULL)
2001         {
2002           UnlockSemaphoreInfo(annotate_semaphore);
2003           ThrowXWindowException(XServerError,"UnableToLoadFont",
2004             draw_info->font);
2005           return(MagickFalse);
2006         }
2007       if ((map_info == (XStandardColormap *) NULL) ||
2008           (visual_info == (XVisualInfo *) NULL) ||
2009           (font_info == (XFontStruct *) NULL))
2010         {
2011           UnlockSemaphoreInfo(annotate_semaphore);
2012           XFreeResources(display,visual_info,map_info,&pixel,font_info,
2013             &resource_info,(XWindowInfo *) NULL);
2014           ThrowXWindowException(XServerError,"UnableToLoadFont",
2015             image->filename);
2016           return(MagickFalse);
2017         }
2018       cache_info=(*draw_info);
2019     }
2020   UnlockSemaphoreInfo(annotate_semaphore);
2021   /*
2022     Initialize annotate info.
2023   */
2024   XGetAnnotateInfo(&annotate_info);
2025   annotate_info.stencil=ForegroundStencil;
2026   if (cache_info.font != draw_info->font)
2027     {
2028       /*
2029         Type name has changed.
2030       */
2031       (void) XFreeFont(display,font_info);
2032       (void) CloneString(&resource_info.font,draw_info->font);
2033       font_info=XBestFont(display,&resource_info,MagickFalse);
2034       if (font_info == (XFontStruct *) NULL)
2035         {
2036           ThrowXWindowException(XServerError,"UnableToLoadFont",
2037             draw_info->font);
2038           return(MagickFalse);
2039         }
2040     }
2041   if (image->debug != MagickFalse)
2042     (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2043       "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
2044       draw_info->font : "none",draw_info->pointsize);
2045   cache_info=(*draw_info);
2046   annotate_info.font_info=font_info;
2047   annotate_info.text=(char *) draw_info->text;
2048   annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text,
2049     (int) strlen(draw_info->text));
2050   annotate_info.height=(unsigned int) font_info->ascent+font_info->descent;
2051   metrics->pixels_per_em.x=(double) font_info->max_bounds.width;
2052   metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent;
2053   metrics->ascent=(double) font_info->ascent+4;
2054   metrics->descent=(double) (-font_info->descent);
2055   metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine);
2056   metrics->height=font_info->ascent+font_info->descent;
2057   metrics->max_advance=(double) font_info->max_bounds.width;
2058   metrics->bounds.x1=0.0;
2059   metrics->bounds.y1=metrics->descent;
2060   metrics->bounds.x2=metrics->ascent+metrics->descent;
2061   metrics->bounds.y2=metrics->ascent+metrics->descent;
2062   metrics->underline_position=(-2.0);
2063   metrics->underline_thickness=1.0;
2064   if (draw_info->render == MagickFalse)
2065     return(MagickTrue);
2066   if (draw_info->fill.alpha == TransparentAlpha)
2067     return(MagickTrue);
2068   /*
2069     Render fill color.
2070   */
2071   width=annotate_info.width;
2072   height=annotate_info.height;
2073   if ((fabs(draw_info->affine.rx) >= MagickEpsilon) ||
2074       (fabs(draw_info->affine.ry) >= MagickEpsilon))
2075     {
2076       if ((fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2077           (fabs(draw_info->affine.rx+draw_info->affine.ry) < MagickEpsilon))
2078         annotate_info.degrees=(double) (180.0/MagickPI)*
2079           atan2(draw_info->affine.rx,draw_info->affine.sx);
2080     }
2081   (void) FormatLocaleString(annotate_info.geometry,MaxTextExtent,
2082     "%.20gx%.20g%+.20g%+.20g",(double) width,(double) height,
2083     ceil(offset->x-0.5),ceil(offset->y-metrics->ascent-metrics->descent+
2084     draw_info->interline_spacing-0.5));
2085   pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red);
2086   pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green);
2087   pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue);
2088   status=XAnnotateImage(display,&pixel,&annotate_info,image,exception);
2089   if (status == 0)
2090     {
2091       ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
2092         image->filename);
2093       return(MagickFalse);
2094     }
2095   return(MagickTrue);
2096 }
2097 #else
2098 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
2099   const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2100 {
2101   (void) draw_info;
2102   (void) offset;
2103   (void) metrics;
2104   (void) ThrowMagickException(exception,GetMagickModule(),
2105     MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","'%s' (X11)",
2106     image->filename);
2107   return(MagickFalse);
2108 }
2109 #endif