blob: 35df7593ad89c44561ef60f7bb3de786af577edb [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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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*/
cristy4c08aed2011-07-01 19:47:50 +000045#include "MagickCore/studio.h"
46#include "MagickCore/annotate.h"
cristy5ff4eaf2011-09-03 01:38:02 +000047#include "MagickCore/annotate-private.h"
cristy4c08aed2011-07-01 19:47:50 +000048#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"
cristyd1dd6e42011-09-04 01:46:08 +000076#include "MagickCore/utility-private.h"
cristybcbda3f2011-09-03 13:01:22 +000077#include "MagickCore/xwindow.h"
cristy4c08aed2011-07-01 19:47:50 +000078#include "MagickCore/xwindow-private.h"
cristy3ed852e2009-09-05 21:47:34 +000079#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
108/*
cristyd7ecaca2011-01-24 14:14:53 +0000109 Annotate semaphores.
110*/
111static SemaphoreInfo
112 *annotate_semaphore = (SemaphoreInfo *) NULL;
113
114/*
cristy3ed852e2009-09-05 21:47:34 +0000115 Forward declarations.
116*/
117static MagickBooleanType
cristy5cbc0162011-08-29 00:36:28 +0000118 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
119 ExceptionInfo *),
120 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
121 ExceptionInfo *),
cristy3ed852e2009-09-05 21:47:34 +0000122 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
cristy5cbc0162011-08-29 00:36:28 +0000123 TypeMetric *,ExceptionInfo *),
124 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
125 ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000126
127/*
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129% %
130% %
131% %
cristyd7ecaca2011-01-24 14:14:53 +0000132+ 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*/
cristy5ff4eaf2011-09-03 01:38:02 +0000145MagickPrivate MagickBooleanType AnnotateComponentGenesis(void)
cristyd7ecaca2011-01-24 14:14:53 +0000146{
147 AcquireSemaphoreInfo(&annotate_semaphore);
148 return(MagickTrue);
149}
150
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*/
cristy5ff4eaf2011-09-03 01:38:02 +0000169MagickPrivate void AnnotateComponentTerminus(void)
cristyd7ecaca2011-01-24 14:14:53 +0000170{
171 if (annotate_semaphore == (SemaphoreInfo *) NULL)
172 AcquireSemaphoreInfo(&annotate_semaphore);
173 DestroySemaphoreInfo(&annotate_semaphore);
174}
175
176/*
177%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178% %
179% %
180% %
cristy3ed852e2009-09-05 21:47:34 +0000181% 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%
cristy5cbc0162011-08-29 00:36:28 +0000215% MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
216% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000217%
218% A description of each parameter follows:
219%
220% o image: the image.
221%
222% o draw_info: the draw info.
223%
cristy5cbc0162011-08-29 00:36:28 +0000224% o exception: return any errors or warnings in this structure.
225%
cristy3ed852e2009-09-05 21:47:34 +0000226*/
227MagickExport MagickBooleanType AnnotateImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000228 const DrawInfo *draw_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000229{
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
cristybb503372010-05-27 20:51:26 +0000250 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000251 i;
252
253 size_t
254 length;
255
256 TypeMetric
257 metrics;
258
cristybb503372010-05-27 20:51:26 +0000259 size_t
cristy58460392009-09-09 01:52:06 +0000260 height,
cristy3ed852e2009-09-05 21:47:34 +0000261 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]);
cristybb503372010-05-27 20:51:26 +0000280 number_lines=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000281 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,
cristy5cbc0162011-08-29 00:36:28 +0000288 exception);
cristy3ed852e2009-09-05 21:47:34 +0000289 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
290 }
cristy5cbc0162011-08-29 00:36:28 +0000291 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000292 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]);
cristy5cbc0162011-08-29 00:36:28 +0000302 (void) GetTypeMetrics(image,annotate,&metrics,exception);
cristybb503372010-05-27 20:51:26 +0000303 height=(ssize_t) (metrics.ascent-metrics.descent+
cristy58460392009-09-09 01:52:06 +0000304 draw_info->interline_spacing+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000305 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 }
cristy4c08aed2011-07-01 19:47:50 +0000450 if (draw_info->undercolor.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000451 {
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;
cristyb51dff52011-05-19 16:55:47 +0000463 (void) FormatLocaleString(primitive,MaxTextExtent,
cristya8549b12011-05-18 19:05:08 +0000464 "rectangle 0,0 %g,%.20g",metrics.origin.x,(double) height);
cristy3ed852e2009-09-05 21:47:34 +0000465 (void) CloneString(&undercolor_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000466 (void) DrawImage(image,undercolor_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000467 (void) DestroyDrawInfo(undercolor_info);
468 }
469 annotate_info->affine.tx=offset.x;
470 annotate_info->affine.ty=offset.y;
cristyb51dff52011-05-19 16:55:47 +0000471 (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
cristya8549b12011-05-18 19:05:08 +0000472 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
cristy3ed852e2009-09-05 21:47:34 +0000473 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);
cristy018f07f2011-09-04 21:15:19 +0000478 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000479 }
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);
cristy018f07f2011-09-04 21:15:19 +0000486 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000487 }
488 /*
489 Annotate image with text.
490 */
cristy5cbc0162011-08-29 00:36:28 +0000491 status=RenderType(image,annotate,&offset,&metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000492 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);
cristy018f07f2011-09-04 21:15:19 +0000499 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000500 }
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
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%
cristybb503372010-05-27 20:51:26 +0000529% ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000530% const MagickBooleanType split,TypeMetric *metrics,char **caption,
531% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000532%
533% A description of each parameter follows.
534%
535% o image: The image.
536%
cristy3ed852e2009-09-05 21:47:34 +0000537% o draw_info: the draw info.
538%
cristy6b1d05e2010-09-22 19:17:27 +0000539% o split: when no convenient line breaks-- insert newline.
540%
cristy3ed852e2009-09-05 21:47:34 +0000541% o metrics: Return the font metrics in this structure.
542%
cristy6b1d05e2010-09-22 19:17:27 +0000543% o caption: the caption.
544%
cristy5cbc0162011-08-29 00:36:28 +0000545% o exception: return any errors or warnings in this structure.
546%
cristy3ed852e2009-09-05 21:47:34 +0000547*/
cristybb503372010-05-27 20:51:26 +0000548MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000549 const MagickBooleanType split,TypeMetric *metrics,char **caption,
550 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000551{
552 MagickBooleanType
553 status;
554
555 register char
556 *p,
557 *q,
558 *s;
559
cristybb503372010-05-27 20:51:26 +0000560 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000561 i;
562
cristybb503372010-05-27 20:51:26 +0000563 size_t
cristy3ed852e2009-09-05 21:47:34 +0000564 width;
565
cristy6193b862011-08-06 16:33:59 +0000566 ssize_t
567 n;
568
cristy3ed852e2009-09-05 21:47:34 +0000569 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;
cristy2911f2d2011-08-20 23:04:43 +0000575 if (GetUTFCode(p) == '\n')
cristye2c81ad2011-08-20 22:54:14 +0000576 q=draw_info->text;
cristybb503372010-05-27 20:51:26 +0000577 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
cristy3ed852e2009-09-05 21:47:34 +0000578 *q++=(*(p+i));
579 *q='\0';
cristy5cbc0162011-08-29 00:36:28 +0000580 status=GetTypeMetrics(image,draw_info,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000581 if (status == MagickFalse)
582 break;
cristybb503372010-05-27 20:51:26 +0000583 width=(size_t) floor(metrics->width+0.5);
cristy6193b862011-08-06 16:33:59 +0000584 if (width <= image->columns)
585 continue;
cristy4aa314e2011-08-19 13:59:45 +0000586 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
cristy3ed852e2009-09-05 21:47:34 +0000587 {
588 *s='\n';
589 p=s;
590 }
591 else
cristy4aa314e2011-08-19 13:59:45 +0000592 if ((s != (char *) NULL) || (split != MagickFalse))
cristy6b1d05e2010-09-22 19:17:27 +0000593 {
594 char
595 *target;
cristy3ed852e2009-09-05 21:47:34 +0000596
cristy6b1d05e2010-09-22 19:17:27 +0000597 /*
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 }
cristy3ed852e2009-09-05 21:47:34 +0000609 q=draw_info->text;
cristy6193b862011-08-06 16:33:59 +0000610 s=(char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000611 }
cristy6193b862011-08-06 16:33:59 +0000612 n=0;
cristy3ed852e2009-09-05 21:47:34 +0000613 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
614 if (GetUTFCode(p) == '\n')
cristy6193b862011-08-06 16:33:59 +0000615 n++;
616 return(n);
cristy3ed852e2009-09-05 21:47:34 +0000617}
618
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,
cristy5cbc0162011-08-29 00:36:28 +0000655% const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000656%
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%
cristy5cbc0162011-08-29 00:36:28 +0000665% o exception: return any errors or warnings in this structure.
666%
cristy3ed852e2009-09-05 21:47:34 +0000667*/
668MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000669 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000670{
671 char
672 **textlist;
673
674 DrawInfo
675 *annotate_info;
676
677 MagickBooleanType
678 status;
679
cristybb503372010-05-27 20:51:26 +0000680 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000681 i;
682
683 TypeMetric
684 extent;
685
cristy3ed852e2009-09-05 21:47:34 +0000686 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;
cristy024843f2011-06-03 17:52:52 +0000704 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000705 (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];
cristy5cbc0162011-08-29 00:36:28 +0000711 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000712 *metrics=extent;
713 for (i=1; textlist[i] != (char *) NULL; i++)
714 {
715 annotate_info->text=textlist[i];
cristy5cbc0162011-08-29 00:36:28 +0000716 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000717 if (extent.width > metrics->width)
718 *metrics=extent;
719 }
cristye2c81ad2011-08-20 22:54:14 +0000720 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
721 (i-1)*draw_info->interline_spacing);
cristy3ed852e2009-09-05 21:47:34 +0000722 /*
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
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,
cristy5cbc0162011-08-29 00:36:28 +0000766% TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000767%
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%
cristy5cbc0162011-08-29 00:36:28 +0000776% o exception: return any errors or warnings in this structure.
777%
cristy3ed852e2009-09-05 21:47:34 +0000778*/
779MagickExport MagickBooleanType GetTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000780 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000781{
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;
cristy024843f2011-06-03 17:52:52 +0000800 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000801 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
802 offset.x=0.0;
803 offset.y=0.0;
cristy5cbc0162011-08-29 00:36:28 +0000804 status=RenderType(image,annotate_info,&offset,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000805 if (image->debug != MagickFalse)
806 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
cristye7f51092010-01-17 00:39:37 +0000807 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
cristya8549b12011-05-18 19:05:08 +0000808 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
cristye7f51092010-01-17 00:39:37 +0000809 "underline position: %g; underline thickness: %g",annotate_info->text,
cristy3ed852e2009-09-05 21:47:34 +0000810 metrics->width,metrics->height,metrics->ascent,metrics->descent,
811 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
cristy4c08aed2011-07-01 19:47:50 +0000812 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
813 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
cristy3ed852e2009-09-05 21:47:34 +0000814 metrics->underline_position,metrics->underline_thickness);
815 annotate_info=DestroyDrawInfo(annotate_info);
816 return(status);
817}
818
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,
cristy5cbc0162011-08-29 00:36:28 +0000836% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000837%
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%
cristy5cbc0162011-08-29 00:36:28 +0000848% o exception: return any errors or warnings in this structure.
849%
cristy3ed852e2009-09-05 21:47:34 +0000850*/
851static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000852 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000853{
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,
cristy5cbc0162011-08-29 00:36:28 +0000869 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000870 return(status);
871 }
872 if (*draw_info->font == '-')
cristy5cbc0162011-08-29 00:36:28 +0000873 return(RenderX11(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +0000874 if (IsPathAccessible(draw_info->font) != MagickFalse)
875 {
876 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000877 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000878 return(status);
879 }
cristy5cbc0162011-08-29 00:36:28 +0000880 type_info=GetTypeInfo(draw_info->font,exception);
cristy3ed852e2009-09-05 21:47:34 +0000881 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000882 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
883 "UnableToReadFont","`%s'",draw_info->font);
cristy3ed852e2009-09-05 21:47:34 +0000884 }
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,
cristy5cbc0162011-08-29 00:36:28 +0000889 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000890 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000891 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
892 "UnableToReadFont","`%s'",draw_info->family);
cristy3ed852e2009-09-05 21:47:34 +0000893 }
894 if (type_info == (const TypeInfo *) NULL)
cristyd984f272010-03-16 01:46:12 +0000895 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000896 draw_info->stretch,draw_info->weight,exception);
cristyd984f272010-03-16 01:46:12 +0000897 if (type_info == (const TypeInfo *) NULL)
cristycbb34a42010-03-16 01:45:18 +0000898 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000899 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000900 if (type_info == (const TypeInfo *) NULL)
cristy704acaa2010-03-23 13:08:21 +0000901 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000902 draw_info->stretch,draw_info->weight,exception);
cristy704acaa2010-03-23 13:08:21 +0000903 if (type_info == (const TypeInfo *) NULL)
cristy6193b862011-08-06 16:33:59 +0000904 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000905 draw_info->stretch,draw_info->weight,exception);
cristy6193b862011-08-06 16:33:59 +0000906 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000907 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000908 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000909 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000910 type_info=GetTypeInfo("*",exception);
cristy704acaa2010-03-23 13:08:21 +0000911 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000912 {
cristy5cbc0162011-08-29 00:36:28 +0000913 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
914 exception);
cristy3ed852e2009-09-05 21:47:34 +0000915 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);
cristy5cbc0162011-08-29 00:36:28 +0000923 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
924 exception);
cristy3ed852e2009-09-05 21:47:34 +0000925 annotate_info=DestroyDrawInfo(annotate_info);
926 return(status);
927}
928
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,
cristy5cbc0162011-08-29 00:36:28 +0000946% const char *encoding,const PointInfo *offset,TypeMetric *metrics,
947% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000948%
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%
cristy5cbc0162011-08-29 00:36:28 +0000961% o exception: return any errors or warnings in this structure.
962%
cristy3ed852e2009-09-05 21:47:34 +0000963*/
964
965#if defined(MAGICKCORE_FREETYPE_DELEGATE)
cristy2857ffc2010-03-27 23:44:00 +0000966
cristy3ed852e2009-09-05 21:47:34 +0000967static 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;
cristyb51dff52011-05-19 16:55:47 +0000977 (void) FormatLocaleString(path,MaxTextExtent,
cristya8549b12011-05-18 19:05:08 +0000978 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
cristy8cd5b312010-01-07 01:10:24 +0000979 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);
cristy3ed852e2009-09-05 21:47:34 +0000981 (void) ConcatenateString(&draw_info->primitive,path);
982 return(0);
983}
984
985static 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;
cristyb51dff52011-05-19 16:55:47 +0000994 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +0000995 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +0000996 (void) ConcatenateString(&draw_info->primitive,path);
997 return(0);
998}
999
1000static 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;
cristyb51dff52011-05-19 16:55:47 +00001009 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +00001010 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001011 (void) ConcatenateString(&draw_info->primitive,path);
1012 return(0);
1013}
1014
1015static 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;
cristyb51dff52011-05-19 16:55:47 +00001025 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
cristy3ed852e2009-09-05 21:47:34 +00001026 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
1032static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001033 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1034 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001035{
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
cristyc9b12952010-03-28 01:12:28 +00001055 double
1056 direction;
1057
cristy3ed852e2009-09-05 21:47:34 +00001058 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
cristy3ed852e2009-09-05 21:47:34 +00001095 PointInfo
1096 point,
1097 resolution;
1098
1099 register char
1100 *p;
1101
cristy9d314ff2011-03-09 01:30:28 +00001102 ssize_t
1103 code,
1104 y;
1105
cristy3ed852e2009-09-05 21:47:34 +00001106 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
cristy2857ffc2010-03-27 23:44:00 +00001116 unsigned char
1117 *utf8;
1118
cristy3ed852e2009-09-05 21:47:34 +00001119 /*
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;
cristyeaedf062010-05-29 22:36:02 +00001135 status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
cristy3ed852e2009-09-05 21:47:34 +00001136 args.pathname=DestroyString(args.pathname);
1137 if (status != 0)
1138 {
1139 (void) FT_Done_FreeType(library);
cristy5cbc0162011-08-29 00:36:28 +00001140 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
1141 "UnableToReadFont","`%s'",draw_info->font);
1142 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001143 }
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; "
cristye7f51092010-01-17 00:39:37 +00001237 "font-encoding %s; text-encoding %s; pointsize %g",
cristy3ed852e2009-09-05 21:47:34 +00001238 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");
cristy33204242010-10-07 23:17:12 +00001244 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
cristy3ed852e2009-09-05 21:47:34 +00001245 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)
cristy5cbc0162011-08-29 00:36:28 +00001268 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00001269 if (image->matte == MagickFalse)
cristy5cbc0162011-08-29 00:36:28 +00001270 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001271 }
cristyc9b12952010-03-28 01:12:28 +00001272 direction=1.0;
cristycf5467b2010-03-28 16:22:06 +00001273 if (draw_info->direction == RightToLeftDirection)
cristyc9b12952010-03-28 01:12:28 +00001274 direction=(-1.0);
cristy3ed852e2009-09-05 21:47:34 +00001275 point.x=0.0;
1276 point.y=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001277 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
cristybd512462010-02-13 02:13:14 +00001278 if (GetUTFCode(p) < 0)
1279 break;
cristy2857ffc2010-03-27 23:44:00 +00001280 utf8=(unsigned char *) NULL;
cristyd913ea12010-02-13 02:32:57 +00001281 if (GetUTFCode(p) == 0)
cristy2857ffc2010-03-27 23:44:00 +00001282 p=draw_info->text;
cristybd512462010-02-13 02:13:14 +00001283 else
cristybd512462010-02-13 02:13:14 +00001284 {
cristy2857ffc2010-03-27 23:44:00 +00001285 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1286 if (utf8 != (unsigned char *) NULL)
cristyc9b12952010-03-28 01:12:28 +00001287 p=(char *) utf8;
cristy2857ffc2010-03-27 23:44:00 +00001288 }
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)
cristy94b11832011-09-08 19:46:03 +00001300 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
cristy2857ffc2010-03-27 23:44:00 +00001301 else
1302 if (FT_HAS_KERNING(face))
1303 {
1304 FT_Vector
1305 kerning;
cristybd512462010-02-13 02:13:14 +00001306
cristy2857ffc2010-03-27 23:44:00 +00001307 status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1308 ft_kerning_default,&kerning);
1309 if (status == 0)
cristy94b11832011-09-08 19:46:03 +00001310 origin.x+=(FT_Pos) (direction*kerning.x);
cristy2857ffc2010-03-27 23:44:00 +00001311 }
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)
cristy4c08aed2011-07-01 19:47:50 +00001333 if ((draw_info->stroke.alpha != TransparentAlpha) ||
cristy2857ffc2010-03-27 23:44:00 +00001334 (draw_info->stroke_pattern != (Image *) NULL))
cristybd512462010-02-13 02:13:14 +00001335 {
cristy2857ffc2010-03-27 23:44:00 +00001336 /*
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;
cristy2f5b5c72011-09-16 16:52:00 +00001352 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1353 point.x=offset->x+(origin.x >> 6);
cristy2857ffc2010-03-27 23:44:00 +00001354 point.y=offset->y-bitmap->top;
1355 if (draw_info->render != MagickFalse)
1356 {
1357 CacheView
1358 *image_view;
cristybd512462010-02-13 02:13:14 +00001359
cristy2857ffc2010-03-27 23:44:00 +00001360 MagickBooleanType
1361 status;
1362
cristy2f5b5c72011-09-16 16:52:00 +00001363 register unsigned char
1364 *p;
1365
cristy2857ffc2010-03-27 23:44:00 +00001366 /*
1367 Rasterize the glyph.
1368 */
1369 status=MagickTrue;
cristy2857ffc2010-03-27 23:44:00 +00001370 image_view=AcquireCacheView(image);
cristy2f5b5c72011-09-16 16:52:00 +00001371 p=bitmap->bitmap.buffer;
cristybb503372010-05-27 20:51:26 +00001372 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
cristy2857ffc2010-03-27 23:44:00 +00001373 {
cristybd512462010-02-13 02:13:14 +00001374 MagickBooleanType
cristy2857ffc2010-03-27 23:44:00 +00001375 active,
1376 sync;
cristybd512462010-02-13 02:13:14 +00001377
cristy2857ffc2010-03-27 23:44:00 +00001378 MagickRealType
1379 fill_opacity;
cristybd512462010-02-13 02:13:14 +00001380
cristy2857ffc2010-03-27 23:44:00 +00001381 PixelPacket
1382 fill_color;
cristybd512462010-02-13 02:13:14 +00001383
cristy4c08aed2011-07-01 19:47:50 +00001384 register Quantum
cristy2857ffc2010-03-27 23:44:00 +00001385 *restrict q;
cristybd512462010-02-13 02:13:14 +00001386
cristy88158062011-06-09 00:31:43 +00001387 register ssize_t
1388 x;
1389
cristy73ce31b2010-06-13 23:17:39 +00001390 ssize_t
cristy2f5b5c72011-09-16 16:52:00 +00001391 n,
cristy73ce31b2010-06-13 23:17:39 +00001392 x_offset,
1393 y_offset;
1394
cristy2857ffc2010-03-27 23:44:00 +00001395 if (status == MagickFalse)
1396 continue;
cristybb503372010-05-27 20:51:26 +00001397 x_offset=(ssize_t) ceil(point.x-0.5);
1398 y_offset=(ssize_t) ceil(point.y+y-0.5);
1399 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy2857ffc2010-03-27 23:44:00 +00001400 continue;
cristy4c08aed2011-07-01 19:47:50 +00001401 q=(Quantum *) NULL;
cristybb503372010-05-27 20:51:26 +00001402 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001403 active=MagickFalse;
1404 else
cristybd512462010-02-13 02:13:14 +00001405 {
cristy2857ffc2010-03-27 23:44:00 +00001406 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1407 bitmap->bitmap.width,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001408 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
cristybd512462010-02-13 02:13:14 +00001409 }
cristy2f5b5c72011-09-16 16:52:00 +00001410 n=y*bitmap->bitmap.pitch;
1411 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
cristy2857ffc2010-03-27 23:44:00 +00001412 {
1413 x_offset++;
cristy2f5b5c72011-09-16 16:52:00 +00001414 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001415 {
cristyed231572011-07-14 02:18:59 +00001416 q+=GetPixelChannels(image);
cristy2857ffc2010-03-27 23:44:00 +00001417 continue;
1418 }
cristy2f5b5c72011-09-16 16:52:00 +00001419 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
1420 fill_opacity=(MagickRealType) (p[n])/(bitmap->bitmap.num_grays-1);
1421 else
1422 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1423 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
cristy2857ffc2010-03-27 23:44:00 +00001424 if (draw_info->text_antialias == MagickFalse)
1425 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1426 if (active == MagickFalse)
1427 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1428 exception);
cristy6193b862011-08-06 16:33:59 +00001429 if (q == (Quantum *) NULL)
cristy2857ffc2010-03-27 23:44:00 +00001430 {
cristyed231572011-07-14 02:18:59 +00001431 q+=GetPixelChannels(image);
cristy2857ffc2010-03-27 23:44:00 +00001432 continue;
1433 }
1434 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
cristy4c08aed2011-07-01 19:47:50 +00001435 fill_opacity=fill_opacity*fill_color.alpha;
1436 CompositePixelOver(image,&fill_color,fill_opacity,q,
1437 GetPixelAlpha(image,q),q);
cristy2857ffc2010-03-27 23:44:00 +00001438 if (active == MagickFalse)
1439 {
1440 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1441 if (sync == MagickFalse)
1442 status=MagickFalse;
1443 }
cristyed231572011-07-14 02:18:59 +00001444 q+=GetPixelChannels(image);
cristybd512462010-02-13 02:13:14 +00001445 }
cristy2857ffc2010-03-27 23:44:00 +00001446 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1447 if (sync == MagickFalse)
1448 status=MagickFalse;
cristybd512462010-02-13 02:13:14 +00001449 }
cristy2857ffc2010-03-27 23:44:00 +00001450 image_view=DestroyCacheView(image_view);
1451 }
1452 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1453 metrics->width=bitmap->left+bitmap->bitmap.width;
1454 if ((draw_info->interword_spacing != 0.0) &&
1455 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1456 (IsUTFSpace(code) == MagickFalse))
cristy94b11832011-09-08 19:46:03 +00001457 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
cristy2857ffc2010-03-27 23:44:00 +00001458 else
cristy94b11832011-09-08 19:46:03 +00001459 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
cristy2857ffc2010-03-27 23:44:00 +00001460 metrics->origin.x=origin.x;
1461 metrics->origin.y=origin.y;
cristybd5a96c2011-08-21 00:04:26 +00001462 if (last_glyph.id != 0)
1463 FT_Done_Glyph(last_glyph.image);
cristy2857ffc2010-03-27 23:44:00 +00001464 last_glyph=glyph;
1465 code=GetUTFCode(p);
1466 }
1467 if (utf8 != (unsigned char *) NULL)
1468 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
cristybd5a96c2011-08-21 00:04:26 +00001469 if (last_glyph.id != 0)
1470 FT_Done_Glyph(last_glyph.image);
cristy4c08aed2011-07-01 19:47:50 +00001471 if ((draw_info->stroke.alpha != TransparentAlpha) ||
cristy3ed852e2009-09-05 21:47:34 +00001472 (draw_info->stroke_pattern != (Image *) NULL))
1473 {
1474 if (draw_info->render != MagickFalse)
1475 {
1476 /*
1477 Draw text stroke.
1478 */
1479 annotate_info->linejoin=RoundJoin;
1480 annotate_info->affine.tx=offset->x;
1481 annotate_info->affine.ty=offset->y;
1482 (void) ConcatenateString(&annotate_info->primitive,"'");
cristy018f07f2011-09-04 21:15:19 +00001483 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001484 }
1485 }
1486 /*
1487 Determine font metrics.
1488 */
1489 glyph.id=FT_Get_Char_Index(face,'_');
1490 glyph.origin=origin;
1491 status=FT_Load_Glyph(face,glyph.id,flags);
1492 if (status == 0)
1493 {
1494 status=FT_Get_Glyph(face->glyph,&glyph.image);
1495 if (status == 0)
1496 {
1497 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1498 outline,&bounds);
1499 if (status == 0)
1500 {
1501 FT_Vector_Transform(&glyph.origin,&affine);
1502 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1503 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1504 (FT_Vector *) NULL,MagickTrue);
1505 bitmap=(FT_BitmapGlyph) glyph.image;
1506 if (bitmap->left > metrics->width)
1507 metrics->width=bitmap->left;
1508 }
1509 }
cristye2c81ad2011-08-20 22:54:14 +00001510 FT_Done_Glyph(glyph.image);
cristy3ed852e2009-09-05 21:47:34 +00001511 }
1512 metrics->width-=metrics->bounds.x1/64.0;
1513 metrics->bounds.x1/=64.0;
1514 metrics->bounds.y1/=64.0;
1515 metrics->bounds.x2/=64.0;
1516 metrics->bounds.y2/=64.0;
1517 metrics->origin.x/=64.0;
1518 metrics->origin.y/=64.0;
1519 /*
1520 Relinquish resources.
1521 */
1522 annotate_info=DestroyDrawInfo(annotate_info);
1523 (void) FT_Done_Face(face);
1524 (void) FT_Done_FreeType(library);
1525 return(MagickTrue);
1526}
1527#else
1528static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1529 const char *magick_unused(encoding),const PointInfo *offset,
cristy5cbc0162011-08-29 00:36:28 +00001530 TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001531{
cristy5cbc0162011-08-29 00:36:28 +00001532 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001533 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (Freetype)",
cristyee081822011-02-02 19:07:30 +00001534 draw_info->font != (char *) NULL ? draw_info->font : "none");
cristy3ed852e2009-09-05 21:47:34 +00001535 return(RenderPostscript(image,draw_info,offset,metrics));
1536}
1537#endif
1538
1539/*
1540%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1541% %
1542% %
1543% %
1544+ R e n d e r P o s t s c r i p t %
1545% %
1546% %
1547% %
1548%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1549%
1550% RenderPostscript() renders text on the image with a Postscript font. It
1551% also returns the bounding box of the text relative to the image.
1552%
1553% The format of the RenderPostscript method is:
1554%
1555% MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001556% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001557%
1558% A description of each parameter follows:
1559%
1560% o image: the image.
1561%
1562% o draw_info: the draw info.
1563%
1564% o offset: (x,y) location of text relative to image.
1565%
1566% o metrics: bounding box of text.
1567%
cristy5cbc0162011-08-29 00:36:28 +00001568% o exception: return any errors or warnings in this structure.
1569%
cristy3ed852e2009-09-05 21:47:34 +00001570*/
1571
1572static inline size_t MagickMin(const size_t x,const size_t y)
1573{
1574 if (x < y)
1575 return(x);
1576 return(y);
1577}
1578
1579static char *EscapeParenthesis(const char *text)
1580{
1581 char
1582 *buffer;
1583
1584 register char
1585 *p;
1586
cristybb503372010-05-27 20:51:26 +00001587 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001588 i;
1589
1590 size_t
1591 escapes;
1592
1593 escapes=0;
1594 buffer=AcquireString(text);
1595 p=buffer;
cristybb503372010-05-27 20:51:26 +00001596 for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
cristy3ed852e2009-09-05 21:47:34 +00001597 {
1598 if ((text[i] == '(') || (text[i] == ')'))
1599 {
1600 *p++='\\';
1601 escapes++;
1602 }
1603 *p++=text[i];
1604 }
1605 *p='\0';
1606 return(buffer);
1607}
1608
1609static MagickBooleanType RenderPostscript(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00001610 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1611 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001612{
1613 char
1614 filename[MaxTextExtent],
1615 geometry[MaxTextExtent],
1616 *text;
1617
1618 FILE
1619 *file;
1620
1621 Image
1622 *annotate_image;
1623
1624 ImageInfo
1625 *annotate_info;
1626
1627 int
1628 unique_file;
1629
cristy3ed852e2009-09-05 21:47:34 +00001630 MagickBooleanType
1631 identity;
1632
1633 PointInfo
1634 extent,
1635 point,
1636 resolution;
1637
cristybb503372010-05-27 20:51:26 +00001638 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001639 i;
1640
cristy9d314ff2011-03-09 01:30:28 +00001641 ssize_t
1642 y;
1643
cristy3ed852e2009-09-05 21:47:34 +00001644 /*
1645 Render label with a Postscript font.
1646 */
1647 if (image->debug != MagickFalse)
1648 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +00001649 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
cristy3ed852e2009-09-05 21:47:34 +00001650 draw_info->font : "none",draw_info->pointsize);
1651 file=(FILE *) NULL;
1652 unique_file=AcquireUniqueFileResource(filename);
1653 if (unique_file != -1)
1654 file=fdopen(unique_file,"wb");
1655 if ((unique_file == -1) || (file == (FILE *) NULL))
1656 {
cristy5cbc0162011-08-29 00:36:28 +00001657 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
cristy3ed852e2009-09-05 21:47:34 +00001658 return(MagickFalse);
1659 }
cristyb51dff52011-05-19 16:55:47 +00001660 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1661 (void) FormatLocaleFile(file,"/ReencodeType\n");
1662 (void) FormatLocaleFile(file,"{\n");
1663 (void) FormatLocaleFile(file," findfont dup length\n");
1664 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001665 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
cristyb51dff52011-05-19 16:55:47 +00001666 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001667 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
cristyb51dff52011-05-19 16:55:47 +00001668 (void) FormatLocaleFile(file,"} bind def\n");
cristy3ed852e2009-09-05 21:47:34 +00001669 /*
1670 Sample to compute bounding box.
1671 */
1672 identity=(draw_info->affine.sx == draw_info->affine.sy) &&
1673 (draw_info->affine.rx == 0.0) && (draw_info->affine.ry == 0.0) ?
1674 MagickTrue : MagickFalse;
1675 extent.x=0.0;
1676 extent.y=0.0;
cristybb503372010-05-27 20:51:26 +00001677 for (i=0; i <= (ssize_t) (strlen(draw_info->text)+2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001678 {
1679 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1680 draw_info->affine.ry*2.0*draw_info->pointsize);
1681 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1682 draw_info->affine.sy*2.0*draw_info->pointsize);
1683 if (point.x > extent.x)
1684 extent.x=point.x;
1685 if (point.y > extent.y)
1686 extent.y=point.y;
1687 }
cristyb51dff52011-05-19 16:55:47 +00001688 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
cristy3ed852e2009-09-05 21:47:34 +00001689 extent.x/2.0,extent.y/2.0);
cristyb51dff52011-05-19 16:55:47 +00001690 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
cristy3ed852e2009-09-05 21:47:34 +00001691 draw_info->pointsize);
1692 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1693 (strchr(draw_info->font,'/') != (char *) NULL))
cristyb51dff52011-05-19 16:55:47 +00001694 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001695 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1696 else
cristy1e604812011-05-19 18:07:50 +00001697 (void) FormatLocaleFile(file,
1698 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1699 draw_info->font);
cristyb51dff52011-05-19 16:55:47 +00001700 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
cristy8cd5b312010-01-07 01:10:24 +00001701 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1702 draw_info->affine.sy);
cristy3ed852e2009-09-05 21:47:34 +00001703 text=EscapeParenthesis(draw_info->text);
1704 if (identity == MagickFalse)
cristy1e604812011-05-19 18:07:50 +00001705 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1706 text);
cristyb51dff52011-05-19 16:55:47 +00001707 (void) FormatLocaleFile(file,"(%s) show\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001708 text=DestroyString(text);
cristyb51dff52011-05-19 16:55:47 +00001709 (void) FormatLocaleFile(file,"showpage\n");
cristy3ed852e2009-09-05 21:47:34 +00001710 (void) fclose(file);
cristyb51dff52011-05-19 16:55:47 +00001711 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
cristye8c25f92010-06-03 00:53:06 +00001712 floor(extent.x+0.5),floor(extent.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001713 annotate_info=AcquireImageInfo();
cristyb51dff52011-05-19 16:55:47 +00001714 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
cristy3ed852e2009-09-05 21:47:34 +00001715 filename);
1716 (void) CloneString(&annotate_info->page,geometry);
1717 if (draw_info->density != (char *) NULL)
1718 (void) CloneString(&annotate_info->density,draw_info->density);
1719 annotate_info->antialias=draw_info->text_antialias;
cristy5cbc0162011-08-29 00:36:28 +00001720 annotate_image=ReadImage(annotate_info,exception);
1721 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00001722 annotate_info=DestroyImageInfo(annotate_info);
1723 (void) RelinquishUniqueFileResource(filename);
1724 if (annotate_image == (Image *) NULL)
1725 return(MagickFalse);
1726 resolution.x=DefaultResolution;
1727 resolution.y=DefaultResolution;
1728 if (draw_info->density != (char *) NULL)
1729 {
1730 GeometryInfo
1731 geometry_info;
1732
1733 MagickStatusType
1734 flags;
1735
1736 flags=ParseGeometry(draw_info->density,&geometry_info);
1737 resolution.x=geometry_info.rho;
1738 resolution.y=geometry_info.sigma;
1739 if ((flags & SigmaValue) == 0)
1740 resolution.y=resolution.x;
1741 }
1742 if (identity == MagickFalse)
1743 (void) TransformImage(&annotate_image,"0x0",(char *) NULL);
1744 else
1745 {
1746 RectangleInfo
1747 crop_info;
1748
cristy5cbc0162011-08-29 00:36:28 +00001749 crop_info=GetImageBoundingBox(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001750 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
cristy3ed852e2009-09-05 21:47:34 +00001751 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
cristybb503372010-05-27 20:51:26 +00001752 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
cristy06609ee2010-03-17 20:21:27 +00001753 0.5);
cristyb51dff52011-05-19 16:55:47 +00001754 (void) FormatLocaleString(geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00001755 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
cristye8c25f92010-06-03 00:53:06 +00001756 crop_info.height,(double) crop_info.x,(double) crop_info.y);
cristy3ed852e2009-09-05 21:47:34 +00001757 (void) TransformImage(&annotate_image,geometry,(char *) NULL);
1758 }
1759 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1760 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1761 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1762 metrics->ascent=metrics->pixels_per_em.x;
1763 metrics->descent=metrics->pixels_per_em.y/-5.0;
1764 metrics->width=(double) annotate_image->columns/
1765 ExpandAffine(&draw_info->affine);
1766 metrics->height=1.152*metrics->pixels_per_em.x;
1767 metrics->max_advance=metrics->pixels_per_em.x;
1768 metrics->bounds.x1=0.0;
1769 metrics->bounds.y1=metrics->descent;
1770 metrics->bounds.x2=metrics->ascent+metrics->descent;
1771 metrics->bounds.y2=metrics->ascent+metrics->descent;
1772 metrics->underline_position=(-2.0);
1773 metrics->underline_thickness=1.0;
1774 if (draw_info->render == MagickFalse)
1775 {
1776 annotate_image=DestroyImage(annotate_image);
1777 return(MagickTrue);
1778 }
cristy4c08aed2011-07-01 19:47:50 +00001779 if (draw_info->fill.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001780 {
cristy3ed852e2009-09-05 21:47:34 +00001781 MagickBooleanType
1782 sync;
1783
1784 PixelPacket
1785 fill_color;
1786
1787 CacheView
1788 *annotate_view;
1789
1790 /*
1791 Render fill color.
1792 */
cristy63240882011-08-05 19:05:27 +00001793 if (image->matte == MagickFalse)
1794 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1795 if (annotate_image->matte == MagickFalse)
1796 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1797 exception);
1798 fill_color=draw_info->fill;
cristy3ed852e2009-09-05 21:47:34 +00001799 annotate_view=AcquireCacheView(annotate_image);
cristybb503372010-05-27 20:51:26 +00001800 for (y=0; y < (ssize_t) annotate_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001801 {
cristybb503372010-05-27 20:51:26 +00001802 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001803 x;
1804
cristy4c08aed2011-07-01 19:47:50 +00001805 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001806 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001807
1808 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1809 1,exception);
cristy6193b862011-08-06 16:33:59 +00001810 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001811 break;
cristybb503372010-05-27 20:51:26 +00001812 for (x=0; x < (ssize_t) annotate_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001813 {
1814 (void) GetFillColor(draw_info,x,y,&fill_color);
cristy4c08aed2011-07-01 19:47:50 +00001815 SetPixelAlpha(annotate_image,ClampToQuantum((((MagickRealType)
1816 GetPixelIntensity(annotate_image,q)*fill_color.alpha)/
1817 QuantumRange)),q);
1818 SetPixelRed(annotate_image,fill_color.red,q);
1819 SetPixelGreen(annotate_image,fill_color.green,q);
1820 SetPixelBlue(annotate_image,fill_color.blue,q);
cristyed231572011-07-14 02:18:59 +00001821 q+=GetPixelChannels(annotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001822 }
1823 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1824 if (sync == MagickFalse)
1825 break;
1826 }
1827 annotate_view=DestroyCacheView(annotate_view);
1828 (void) CompositeImage(image,OverCompositeOp,annotate_image,
cristybb503372010-05-27 20:51:26 +00001829 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
cristy06609ee2010-03-17 20:21:27 +00001830 metrics->descent)-0.5));
cristy3ed852e2009-09-05 21:47:34 +00001831 }
1832 annotate_image=DestroyImage(annotate_image);
1833 return(MagickTrue);
1834}
1835
1836/*
1837%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1838% %
1839% %
1840% %
1841+ R e n d e r X 1 1 %
1842% %
1843% %
1844% %
1845%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1846%
1847% RenderX11() renders text on the image with an X11 font. It also returns the
1848% bounding box of the text relative to the image.
1849%
1850% The format of the RenderX11 method is:
1851%
1852% MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001853% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001854%
1855% A description of each parameter follows:
1856%
1857% o image: the image.
1858%
1859% o draw_info: the draw info.
1860%
1861% o offset: (x,y) location of text relative to image.
1862%
1863% o metrics: bounding box of text.
1864%
cristy5cbc0162011-08-29 00:36:28 +00001865% o exception: return any errors or warnings in this structure.
1866%
cristy3ed852e2009-09-05 21:47:34 +00001867*/
1868#if defined(MAGICKCORE_X11_DELEGATE)
1869static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001870 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001871{
1872 MagickBooleanType
1873 status;
1874
1875 static DrawInfo
1876 cache_info;
1877
1878 static Display
1879 *display = (Display *) NULL;
1880
1881 static XAnnotateInfo
1882 annotate_info;
1883
1884 static XFontStruct
1885 *font_info;
1886
1887 static XPixelInfo
1888 pixel;
1889
1890 static XResourceInfo
1891 resource_info;
1892
1893 static XrmDatabase
1894 resource_database;
1895
1896 static XStandardColormap
1897 *map_info;
1898
1899 static XVisualInfo
1900 *visual_info;
1901
cristybb503372010-05-27 20:51:26 +00001902 size_t
cristy3ed852e2009-09-05 21:47:34 +00001903 height,
1904 width;
1905
cristyd7ecaca2011-01-24 14:14:53 +00001906 if (annotate_semaphore == (SemaphoreInfo *) NULL)
1907 AcquireSemaphoreInfo(&annotate_semaphore);
1908 LockSemaphoreInfo(annotate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001909 if (display == (Display *) NULL)
1910 {
cristy104cea82009-10-25 02:26:51 +00001911 const char
1912 *client_name;
1913
cristy3ed852e2009-09-05 21:47:34 +00001914 ImageInfo
1915 *image_info;
1916
1917 /*
1918 Open X server connection.
1919 */
1920 display=XOpenDisplay(draw_info->server_name);
1921 if (display == (Display *) NULL)
1922 {
1923 ThrowXWindowException(XServerError,"UnableToOpenXServer",
1924 draw_info->server_name);
1925 return(MagickFalse);
1926 }
1927 /*
1928 Get user defaults from X resource database.
1929 */
1930 (void) XSetErrorHandler(XError);
1931 image_info=AcquireImageInfo();
cristy104cea82009-10-25 02:26:51 +00001932 client_name=GetClientName();
cristy309c8612009-10-25 13:16:09 +00001933 resource_database=XGetResourceDatabase(display,client_name);
cristy104cea82009-10-25 02:26:51 +00001934 XGetResourceInfo(image_info,resource_database,client_name,&resource_info);
cristy3ed852e2009-09-05 21:47:34 +00001935 resource_info.close_server=MagickFalse;
1936 resource_info.colormap=PrivateColormap;
1937 resource_info.font=AcquireString(draw_info->font);
1938 resource_info.background_color=AcquireString("#ffffffffffff");
1939 resource_info.foreground_color=AcquireString("#000000000000");
1940 map_info=XAllocStandardColormap();
1941 if (map_info == (XStandardColormap *) NULL)
1942 {
1943 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
1944 image->filename);
1945 return(MagickFalse);
1946 }
1947 /*
1948 Initialize visual info.
1949 */
1950 visual_info=XBestVisualInfo(display,map_info,&resource_info);
1951 if (visual_info == (XVisualInfo *) NULL)
1952 {
1953 ThrowXWindowException(XServerError,"UnableToGetVisual",
1954 image->filename);
1955 return(MagickFalse);
1956 }
1957 map_info->colormap=(Colormap) NULL;
cristyf2faecf2010-05-28 19:19:36 +00001958 pixel.pixels=(unsigned long *) NULL;
cristy3ed852e2009-09-05 21:47:34 +00001959 /*
1960 Initialize Standard Colormap info.
1961 */
1962 XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen),
1963 map_info);
cristy4c08aed2011-07-01 19:47:50 +00001964 XGetPixelInfo(display,visual_info,map_info,&resource_info,(Image *) NULL,
1965 &pixel);
cristy3ed852e2009-09-05 21:47:34 +00001966 pixel.annotate_context=XDefaultGC(display,visual_info->screen);
1967 /*
1968 Initialize font info.
1969 */
1970 font_info=XBestFont(display,&resource_info,MagickFalse);
1971 if (font_info == (XFontStruct *) NULL)
1972 {
1973 ThrowXWindowException(XServerError,"UnableToLoadFont",
1974 draw_info->font);
1975 return(MagickFalse);
1976 }
1977 if ((map_info == (XStandardColormap *) NULL) ||
1978 (visual_info == (XVisualInfo *) NULL) ||
1979 (font_info == (XFontStruct *) NULL))
1980 {
1981 XFreeResources(display,visual_info,map_info,&pixel,font_info,
1982 &resource_info,(XWindowInfo *) NULL);
1983 ThrowXWindowException(XServerError,"UnableToLoadFont",
1984 image->filename);
1985 return(MagickFalse);
1986 }
1987 cache_info=(*draw_info);
1988 }
cristyd7ecaca2011-01-24 14:14:53 +00001989 UnlockSemaphoreInfo(annotate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001990 /*
1991 Initialize annotate info.
1992 */
1993 XGetAnnotateInfo(&annotate_info);
1994 annotate_info.stencil=ForegroundStencil;
1995 if (cache_info.font != draw_info->font)
1996 {
1997 /*
1998 Type name has changed.
1999 */
2000 (void) XFreeFont(display,font_info);
2001 (void) CloneString(&resource_info.font,draw_info->font);
2002 font_info=XBestFont(display,&resource_info,MagickFalse);
2003 if (font_info == (XFontStruct *) NULL)
2004 {
2005 ThrowXWindowException(XServerError,"UnableToLoadFont",
2006 draw_info->font);
2007 return(MagickFalse);
2008 }
2009 }
2010 if (image->debug != MagickFalse)
2011 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +00002012 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
cristy3ed852e2009-09-05 21:47:34 +00002013 draw_info->font : "none",draw_info->pointsize);
2014 cache_info=(*draw_info);
2015 annotate_info.font_info=font_info;
2016 annotate_info.text=(char *) draw_info->text;
2017 annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text,
2018 (int) strlen(draw_info->text));
2019 annotate_info.height=(unsigned int) font_info->ascent+font_info->descent;
2020 metrics->pixels_per_em.x=(double) font_info->max_bounds.width;
2021 metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent;
2022 metrics->ascent=(double) font_info->ascent+4;
2023 metrics->descent=(double) (-font_info->descent);
2024 metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine);
2025 metrics->height=font_info->ascent+font_info->descent;
2026 metrics->max_advance=(double) font_info->max_bounds.width;
2027 metrics->bounds.x1=0.0;
2028 metrics->bounds.y1=metrics->descent;
2029 metrics->bounds.x2=metrics->ascent+metrics->descent;
2030 metrics->bounds.y2=metrics->ascent+metrics->descent;
2031 metrics->underline_position=(-2.0);
2032 metrics->underline_thickness=1.0;
2033 if (draw_info->render == MagickFalse)
2034 return(MagickTrue);
cristy4c08aed2011-07-01 19:47:50 +00002035 if (draw_info->fill.alpha == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +00002036 return(MagickTrue);
2037 /*
2038 Render fill color.
2039 */
2040 width=annotate_info.width;
2041 height=annotate_info.height;
2042 if ((draw_info->affine.rx != 0.0) || (draw_info->affine.ry != 0.0))
2043 {
2044 if (((draw_info->affine.sx-draw_info->affine.sy) == 0.0) &&
2045 ((draw_info->affine.rx+draw_info->affine.ry) == 0.0))
2046 annotate_info.degrees=(180.0/MagickPI)*
2047 atan2(draw_info->affine.rx,draw_info->affine.sx);
2048 }
cristyb51dff52011-05-19 16:55:47 +00002049 (void) FormatLocaleString(annotate_info.geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002050 "%.20gx%.20g%+.20g%+.20g",(double) width,(double) height,
cristye8c25f92010-06-03 00:53:06 +00002051 ceil(offset->x-0.5),ceil(offset->y-metrics->ascent-metrics->descent+
cristy06609ee2010-03-17 20:21:27 +00002052 draw_info->interline_spacing-0.5));
cristy3ed852e2009-09-05 21:47:34 +00002053 pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red);
2054 pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green);
2055 pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue);
2056 status=XAnnotateImage(display,&pixel,&annotate_info,image);
2057 if (status == 0)
2058 {
2059 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
2060 image->filename);
2061 return(MagickFalse);
2062 }
2063 return(MagickTrue);
2064}
2065#else
2066static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00002067 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002068{
2069 (void) draw_info;
2070 (void) offset;
2071 (void) metrics;
cristy5cbc0162011-08-29 00:36:28 +00002072 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00002073 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","`%s' (X11)",
2074 image->filename);
2075 return(MagickFalse);
2076}
2077#endif