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