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