blob: 8b423d5a3d19dd3a2cdedadda70f1189c5ab8ca9 [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% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 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"
cristy54b92622012-04-09 18:42:06 +000053#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000054#include "MagickCore/composite.h"
55#include "MagickCore/composite-private.h"
56#include "MagickCore/constitute.h"
57#include "MagickCore/draw.h"
58#include "MagickCore/draw-private.h"
cristy3644bdf2012-08-23 09:07:08 +000059#include "MagickCore/enhance.h"
cristy4c08aed2011-07-01 19:47:50 +000060#include "MagickCore/exception.h"
61#include "MagickCore/exception-private.h"
62#include "MagickCore/gem.h"
63#include "MagickCore/geometry.h"
64#include "MagickCore/image-private.h"
65#include "MagickCore/log.h"
66#include "MagickCore/quantum.h"
67#include "MagickCore/quantum-private.h"
68#include "MagickCore/pixel-accessor.h"
69#include "MagickCore/property.h"
70#include "MagickCore/resource_.h"
71#include "MagickCore/semaphore.h"
72#include "MagickCore/statistic.h"
73#include "MagickCore/string_.h"
74#include "MagickCore/token-private.h"
75#include "MagickCore/transform.h"
76#include "MagickCore/type.h"
77#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000078#include "MagickCore/utility-private.h"
cristybcbda3f2011-09-03 13:01:22 +000079#include "MagickCore/xwindow.h"
cristy4c08aed2011-07-01 19:47:50 +000080#include "MagickCore/xwindow-private.h"
cristy3ed852e2009-09-05 21:47:34 +000081#if defined(MAGICKCORE_FREETYPE_DELEGATE)
cristy07a3cca2012-12-10 13:09:10 +000082#if defined(__MINGW32__) || defined(__MINGW64__)
cristy3ed852e2009-09-05 21:47:34 +000083# undef interface
84#endif
cristy03f187e2013-01-24 00:22:19 +000085#include <ft2build.h>
cristy3ed852e2009-09-05 21:47:34 +000086#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);
cristy574c5b02012-04-09 16:33:15 +0000293 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristyb09db112012-07-11 12:04:31 +0000294 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000295 status=MagickTrue;
296 for (i=0; textlist[i] != (char *) NULL; i++)
297 {
298 /*
299 Position text relative to image.
300 */
301 annotate_info->affine.tx=geometry_info.xi-image->page.x;
302 annotate_info->affine.ty=geometry_info.psi-image->page.y;
303 (void) CloneString(&annotate->text,textlist[i]);
cristy5cbc0162011-08-29 00:36:28 +0000304 (void) GetTypeMetrics(image,annotate,&metrics,exception);
cristybb503372010-05-27 20:51:26 +0000305 height=(ssize_t) (metrics.ascent-metrics.descent+
cristy58460392009-09-09 01:52:06 +0000306 draw_info->interline_spacing+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000307 switch (annotate->gravity)
308 {
309 case UndefinedGravity:
310 default:
311 {
312 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
313 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
314 break;
315 }
316 case NorthWestGravity:
317 {
318 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
319 annotate_info->affine.ry*height+annotate_info->affine.ry*
320 (metrics.ascent+metrics.descent);
321 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
322 annotate_info->affine.sy*height+annotate_info->affine.sy*
323 metrics.ascent;
324 break;
325 }
326 case NorthGravity:
327 {
328 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
329 geometry.width/2.0+i*annotate_info->affine.ry*height-
330 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
331 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
332 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
333 annotate_info->affine.sy*height+annotate_info->affine.sy*
334 metrics.ascent-annotate_info->affine.rx*(metrics.width-
335 metrics.bounds.x1)/2.0;
336 break;
337 }
338 case NorthEastGravity:
339 {
340 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
341 geometry.width+i*annotate_info->affine.ry*height-
342 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
343 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
344 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
345 annotate_info->affine.sy*height+annotate_info->affine.sy*
346 metrics.ascent-annotate_info->affine.rx*(metrics.width-
347 metrics.bounds.x1);
348 break;
349 }
350 case WestGravity:
351 {
352 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
353 annotate_info->affine.ry*height+annotate_info->affine.ry*
354 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
355 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
356 geometry.height/2.0+i*annotate_info->affine.sy*height+
357 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
358 (number_lines-1.0)*height)/2.0;
359 break;
360 }
cristy3ed852e2009-09-05 21:47:34 +0000361 case CenterGravity:
362 {
363 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
364 geometry.width/2.0+i*annotate_info->affine.ry*height-
365 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
366 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
367 (number_lines-1)*height)/2.0;
368 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
369 geometry.height/2.0+i*annotate_info->affine.sy*height-
370 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
371 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
372 (number_lines-1.0)*height)/2.0;
373 break;
374 }
375 case EastGravity:
376 {
377 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
378 geometry.width+i*annotate_info->affine.ry*height-
379 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
380 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
381 (number_lines-1.0)*height)/2.0-1.0;
382 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
383 geometry.height/2.0+i*annotate_info->affine.sy*height-
384 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+
385 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
386 (number_lines-1.0)*height)/2.0;
387 break;
388 }
389 case SouthWestGravity:
390 {
391 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
392 annotate_info->affine.ry*height-annotate_info->affine.ry*
393 (number_lines-1.0)*height;
394 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
395 geometry.height+i*annotate_info->affine.sy*height-
396 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
397 break;
398 }
399 case SouthGravity:
400 {
401 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
402 geometry.width/2.0+i*annotate_info->affine.ry*height-
403 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
404 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
405 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
406 geometry.height+i*annotate_info->affine.sy*height-
407 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
408 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
409 break;
410 }
411 case SouthEastGravity:
412 {
413 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
414 geometry.width+i*annotate_info->affine.ry*height-
415 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
416 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
417 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
418 geometry.height+i*annotate_info->affine.sy*height-
419 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
420 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
421 break;
422 }
423 }
424 switch (annotate->align)
425 {
426 case LeftAlign:
427 {
428 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
429 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
430 break;
431 }
432 case CenterAlign:
433 {
434 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
435 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
436 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
437 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
438 break;
439 }
440 case RightAlign:
441 {
442 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
443 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
444 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
445 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
446 break;
447 }
448 default:
449 break;
450 }
cristy4c08aed2011-07-01 19:47:50 +0000451 if (draw_info->undercolor.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000452 {
453 DrawInfo
454 *undercolor_info;
455
456 /*
457 Text box.
458 */
459 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
460 undercolor_info->fill=draw_info->undercolor;
461 undercolor_info->affine=draw_info->affine;
462 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
463 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
cristyb51dff52011-05-19 16:55:47 +0000464 (void) FormatLocaleString(primitive,MaxTextExtent,
cristya8549b12011-05-18 19:05:08 +0000465 "rectangle 0,0 %g,%.20g",metrics.origin.x,(double) height);
cristy3ed852e2009-09-05 21:47:34 +0000466 (void) CloneString(&undercolor_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000467 (void) DrawImage(image,undercolor_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000468 (void) DestroyDrawInfo(undercolor_info);
469 }
470 annotate_info->affine.tx=offset.x;
471 annotate_info->affine.ty=offset.y;
cristyb51dff52011-05-19 16:55:47 +0000472 (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
cristya8549b12011-05-18 19:05:08 +0000473 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
cristy3ed852e2009-09-05 21:47:34 +0000474 if (annotate->decorate == OverlineDecoration)
475 {
476 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
477 metrics.descent-metrics.underline_position));
478 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000479 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000480 }
481 else
482 if (annotate->decorate == UnderlineDecoration)
483 {
484 annotate_info->affine.ty-=(draw_info->affine.sy*
485 metrics.underline_position);
486 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000487 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000488 }
489 /*
490 Annotate image with text.
491 */
cristy5cbc0162011-08-29 00:36:28 +0000492 status=RenderType(image,annotate,&offset,&metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000493 if (status == MagickFalse)
494 break;
495 if (annotate->decorate == LineThroughDecoration)
496 {
497 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
498 metrics.underline_position+metrics.descent)/2.0);
499 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000500 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000501 }
502 }
503 /*
504 Relinquish resources.
505 */
506 annotate_info=DestroyDrawInfo(annotate_info);
507 annotate=DestroyDrawInfo(annotate);
508 for (i=0; textlist[i] != (char *) NULL; i++)
509 textlist[i]=DestroyString(textlist[i]);
510 textlist=(char **) RelinquishMagickMemory(textlist);
511 return(status);
512}
513
514/*
515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516% %
517% %
518% %
519% F o r m a t M a g i c k C a p t i o n %
520% %
521% %
522% %
523%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524%
525% FormatMagickCaption() formats a caption so that it fits within the image
526% width. It returns the number of lines in the formatted caption.
527%
528% The format of the FormatMagickCaption method is:
529%
cristybb503372010-05-27 20:51:26 +0000530% ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000531% const MagickBooleanType split,TypeMetric *metrics,char **caption,
532% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000533%
534% A description of each parameter follows.
535%
536% o image: The image.
537%
cristy3ed852e2009-09-05 21:47:34 +0000538% o draw_info: the draw info.
539%
cristy6b1d05e2010-09-22 19:17:27 +0000540% o split: when no convenient line breaks-- insert newline.
541%
cristy3ed852e2009-09-05 21:47:34 +0000542% o metrics: Return the font metrics in this structure.
543%
cristy6b1d05e2010-09-22 19:17:27 +0000544% o caption: the caption.
545%
cristy5cbc0162011-08-29 00:36:28 +0000546% o exception: return any errors or warnings in this structure.
547%
cristy3ed852e2009-09-05 21:47:34 +0000548*/
cristybb503372010-05-27 20:51:26 +0000549MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000550 const MagickBooleanType split,TypeMetric *metrics,char **caption,
551 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000552{
cristy74966db2011-09-20 19:37:21 +0000553 char
554 *text;
555
cristy3ed852e2009-09-05 21:47:34 +0000556 MagickBooleanType
557 status;
558
559 register char
560 *p,
561 *q,
562 *s;
563
cristybb503372010-05-27 20:51:26 +0000564 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000565 i;
566
cristybb503372010-05-27 20:51:26 +0000567 size_t
cristy3ed852e2009-09-05 21:47:34 +0000568 width;
569
cristy6193b862011-08-06 16:33:59 +0000570 ssize_t
571 n;
572
cristy74966db2011-09-20 19:37:21 +0000573 text=AcquireString(draw_info->text);
cristy3ed852e2009-09-05 21:47:34 +0000574 q=draw_info->text;
575 s=(char *) NULL;
576 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
577 {
578 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
579 s=p;
cristy2911f2d2011-08-20 23:04:43 +0000580 if (GetUTFCode(p) == '\n')
cristye2c81ad2011-08-20 22:54:14 +0000581 q=draw_info->text;
cristybb503372010-05-27 20:51:26 +0000582 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
cristy3ed852e2009-09-05 21:47:34 +0000583 *q++=(*(p+i));
584 *q='\0';
cristy5cbc0162011-08-29 00:36:28 +0000585 status=GetTypeMetrics(image,draw_info,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000586 if (status == MagickFalse)
587 break;
cristy9e2b1e32012-08-01 11:01:28 +0000588 width=(size_t) floor(metrics->width+metrics->max_advance/2.0+0.5);
cristy74966db2011-09-20 19:37:21 +0000589 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
cristy6193b862011-08-06 16:33:59 +0000590 continue;
cristy74966db2011-09-20 19:37:21 +0000591 (void) strcpy(text,draw_info->text);
cristy4aa314e2011-08-19 13:59:45 +0000592 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
cristy3ed852e2009-09-05 21:47:34 +0000593 {
594 *s='\n';
595 p=s;
596 }
597 else
cristy4aa314e2011-08-19 13:59:45 +0000598 if ((s != (char *) NULL) || (split != MagickFalse))
cristy6b1d05e2010-09-22 19:17:27 +0000599 {
600 char
601 *target;
cristy3ed852e2009-09-05 21:47:34 +0000602
cristy6b1d05e2010-09-22 19:17:27 +0000603 /*
604 No convenient line breaks-- insert newline.
605 */
606 target=AcquireString(*caption);
607 n=p-(*caption);
608 CopyMagickString(target,*caption,n+1);
609 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
610 ConcatenateMagickString(target,p,strlen(*caption)+2);
611 (void) DestroyString(*caption);
612 *caption=target;
613 p=(*caption)+n;
614 }
cristy3ed852e2009-09-05 21:47:34 +0000615 q=draw_info->text;
cristy6193b862011-08-06 16:33:59 +0000616 s=(char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000617 }
cristy74966db2011-09-20 19:37:21 +0000618 text=DestroyString(text);
cristy6193b862011-08-06 16:33:59 +0000619 n=0;
cristy3ed852e2009-09-05 21:47:34 +0000620 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
621 if (GetUTFCode(p) == '\n')
cristy6193b862011-08-06 16:33:59 +0000622 n++;
623 return(n);
cristy3ed852e2009-09-05 21:47:34 +0000624}
625
626/*
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628% %
629% %
630% %
631% G e t M u l t i l i n e T y p e M e t r i c s %
632% %
633% %
634% %
635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636%
637% GetMultilineTypeMetrics() returns the following information for the
638% specified font and text:
639%
640% character width
641% character height
642% ascender
643% descender
644% text width
645% text height
646% maximum horizontal advance
647% bounds: x1
648% bounds: y1
649% bounds: x2
650% bounds: y2
651% origin: x
652% origin: y
653% underline position
654% underline thickness
655%
656% This method is like GetTypeMetrics() but it returns the maximum text width
657% and height for multiple lines of text.
658%
659% The format of the GetMultilineTypeMetrics method is:
660%
661% MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000662% const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000663%
664% A description of each parameter follows:
665%
666% o image: the image.
667%
668% o draw_info: the draw info.
669%
670% o metrics: Return the font metrics in this structure.
671%
cristy5cbc0162011-08-29 00:36:28 +0000672% o exception: return any errors or warnings in this structure.
673%
cristy3ed852e2009-09-05 21:47:34 +0000674*/
675MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000676 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000677{
678 char
679 **textlist;
680
681 DrawInfo
682 *annotate_info;
683
684 MagickBooleanType
685 status;
686
cristybb503372010-05-27 20:51:26 +0000687 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000688 i;
689
690 TypeMetric
691 extent;
692
cristy3ed852e2009-09-05 21:47:34 +0000693 assert(image != (Image *) NULL);
694 assert(image->signature == MagickSignature);
695 if (image->debug != MagickFalse)
696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
697 assert(draw_info != (DrawInfo *) NULL);
698 assert(draw_info->text != (char *) NULL);
699 assert(draw_info->signature == MagickSignature);
700 if (*draw_info->text == '\0')
701 return(MagickFalse);
702 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
703 annotate_info->text=DestroyString(annotate_info->text);
704 /*
705 Convert newlines to multiple lines of text.
706 */
707 textlist=StringToList(draw_info->text);
708 if (textlist == (char **) NULL)
709 return(MagickFalse);
710 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000711 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000712 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
713 (void) ResetMagickMemory(&extent,0,sizeof(extent));
714 /*
715 Find the widest of the text lines.
716 */
717 annotate_info->text=textlist[0];
cristy5cbc0162011-08-29 00:36:28 +0000718 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000719 *metrics=extent;
720 for (i=1; textlist[i] != (char *) NULL; i++)
721 {
722 annotate_info->text=textlist[i];
cristy5cbc0162011-08-29 00:36:28 +0000723 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000724 if (extent.width > metrics->width)
725 *metrics=extent;
726 }
cristye2c81ad2011-08-20 22:54:14 +0000727 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
728 (i-1)*draw_info->interline_spacing);
cristy3ed852e2009-09-05 21:47:34 +0000729 /*
730 Relinquish resources.
731 */
732 annotate_info->text=(char *) NULL;
733 annotate_info=DestroyDrawInfo(annotate_info);
734 for (i=0; textlist[i] != (char *) NULL; i++)
735 textlist[i]=DestroyString(textlist[i]);
736 textlist=(char **) RelinquishMagickMemory(textlist);
737 return(status);
738}
739
740/*
741%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
742% %
743% %
744% %
745% G e t T y p e M e t r i c s %
746% %
747% %
748% %
749%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
750%
751% GetTypeMetrics() returns the following information for the specified font
752% and text:
753%
754% character width
755% character height
756% ascender
757% descender
758% text width
759% text height
760% maximum horizontal advance
761% bounds: x1
762% bounds: y1
763% bounds: x2
764% bounds: y2
765% origin: x
766% origin: y
767% underline position
768% underline thickness
769%
770% The format of the GetTypeMetrics method is:
771%
772% MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000773% TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000774%
775% A description of each parameter follows:
776%
777% o image: the image.
778%
779% o draw_info: the draw info.
780%
781% o metrics: Return the font metrics in this structure.
782%
cristy5cbc0162011-08-29 00:36:28 +0000783% o exception: return any errors or warnings in this structure.
784%
cristy3ed852e2009-09-05 21:47:34 +0000785*/
786MagickExport MagickBooleanType GetTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000787 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000788{
789 DrawInfo
790 *annotate_info;
791
792 MagickBooleanType
793 status;
794
795 PointInfo
796 offset;
797
798 assert(image != (Image *) NULL);
799 assert(image->signature == MagickSignature);
800 if (image->debug != MagickFalse)
801 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
802 assert(draw_info != (DrawInfo *) NULL);
803 assert(draw_info->text != (char *) NULL);
804 assert(draw_info->signature == MagickSignature);
805 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
806 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000807 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000808 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
809 offset.x=0.0;
810 offset.y=0.0;
cristy5cbc0162011-08-29 00:36:28 +0000811 status=RenderType(image,annotate_info,&offset,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000812 if (image->debug != MagickFalse)
813 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
cristye7f51092010-01-17 00:39:37 +0000814 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
cristya8549b12011-05-18 19:05:08 +0000815 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
cristye7f51092010-01-17 00:39:37 +0000816 "underline position: %g; underline thickness: %g",annotate_info->text,
cristy3ed852e2009-09-05 21:47:34 +0000817 metrics->width,metrics->height,metrics->ascent,metrics->descent,
818 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
cristy4c08aed2011-07-01 19:47:50 +0000819 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
820 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
cristy3ed852e2009-09-05 21:47:34 +0000821 metrics->underline_position,metrics->underline_thickness);
822 annotate_info=DestroyDrawInfo(annotate_info);
823 return(status);
824}
825
826/*
827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828% %
829% %
830% %
831+ R e n d e r T y p e %
832% %
833% %
834% %
835%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
836%
837% RenderType() renders text on the image. It also returns the bounding box of
838% the text relative to the image.
839%
840% The format of the RenderType method is:
841%
842% MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000843% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000844%
845% A description of each parameter follows:
846%
847% o image: the image.
848%
849% o draw_info: the draw info.
850%
851% o offset: (x,y) location of text relative to image.
852%
853% o metrics: bounding box of text.
854%
cristy5cbc0162011-08-29 00:36:28 +0000855% o exception: return any errors or warnings in this structure.
856%
cristy3ed852e2009-09-05 21:47:34 +0000857*/
858static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000859 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000860{
861 const TypeInfo
862 *type_info;
863
864 DrawInfo
865 *annotate_info;
866
867 MagickBooleanType
868 status;
869
870 type_info=(const TypeInfo *) NULL;
871 if (draw_info->font != (char *) NULL)
872 {
873 if (*draw_info->font == '@')
874 {
875 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000876 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000877 return(status);
878 }
879 if (*draw_info->font == '-')
cristy5cbc0162011-08-29 00:36:28 +0000880 return(RenderX11(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +0000881 if (IsPathAccessible(draw_info->font) != MagickFalse)
882 {
883 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000884 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000885 return(status);
886 }
cristy5cbc0162011-08-29 00:36:28 +0000887 type_info=GetTypeInfo(draw_info->font,exception);
cristy3ed852e2009-09-05 21:47:34 +0000888 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000889 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000890 "UnableToReadFont","`%s'",draw_info->font);
cristy3ed852e2009-09-05 21:47:34 +0000891 }
892 if ((type_info == (const TypeInfo *) NULL) &&
893 (draw_info->family != (const char *) NULL))
894 {
895 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000896 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000897 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000898 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000899 "UnableToReadFont","`%s'",draw_info->family);
cristy3ed852e2009-09-05 21:47:34 +0000900 }
901 if (type_info == (const TypeInfo *) NULL)
cristyd984f272010-03-16 01:46:12 +0000902 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000903 draw_info->stretch,draw_info->weight,exception);
cristyd984f272010-03-16 01:46:12 +0000904 if (type_info == (const TypeInfo *) NULL)
cristycbb34a42010-03-16 01:45:18 +0000905 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000906 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000907 if (type_info == (const TypeInfo *) NULL)
cristy704acaa2010-03-23 13:08:21 +0000908 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000909 draw_info->stretch,draw_info->weight,exception);
cristy704acaa2010-03-23 13:08:21 +0000910 if (type_info == (const TypeInfo *) NULL)
cristy6193b862011-08-06 16:33:59 +0000911 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000912 draw_info->stretch,draw_info->weight,exception);
cristy6193b862011-08-06 16:33:59 +0000913 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000914 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000915 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000916 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000917 type_info=GetTypeInfo("*",exception);
cristy704acaa2010-03-23 13:08:21 +0000918 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000919 {
cristy5cbc0162011-08-29 00:36:28 +0000920 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
921 exception);
cristy3ed852e2009-09-05 21:47:34 +0000922 return(status);
923 }
924 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
925 annotate_info->face=type_info->face;
926 if (type_info->metrics != (char *) NULL)
927 (void) CloneString(&annotate_info->metrics,type_info->metrics);
928 if (type_info->glyphs != (char *) NULL)
929 (void) CloneString(&annotate_info->font,type_info->glyphs);
cristy5cbc0162011-08-29 00:36:28 +0000930 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
931 exception);
cristy3ed852e2009-09-05 21:47:34 +0000932 annotate_info=DestroyDrawInfo(annotate_info);
933 return(status);
934}
935
936/*
937%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938% %
939% %
940% %
941+ R e n d e r F r e e t y p e %
942% %
943% %
944% %
945%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946%
947% RenderFreetype() renders text on the image with a Truetype font. It also
948% returns the bounding box of the text relative to the image.
949%
950% The format of the RenderFreetype method is:
951%
952% MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000953% const char *encoding,const PointInfo *offset,TypeMetric *metrics,
954% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000955%
956% A description of each parameter follows:
957%
958% o image: the image.
959%
960% o draw_info: the draw info.
961%
962% o encoding: the font encoding.
963%
964% o offset: (x,y) location of text relative to image.
965%
966% o metrics: bounding box of text.
967%
cristy5cbc0162011-08-29 00:36:28 +0000968% o exception: return any errors or warnings in this structure.
969%
cristy3ed852e2009-09-05 21:47:34 +0000970*/
971
972#if defined(MAGICKCORE_FREETYPE_DELEGATE)
cristy2857ffc2010-03-27 23:44:00 +0000973
cristy3ed852e2009-09-05 21:47:34 +0000974static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
975 DrawInfo *draw_info)
976{
977 AffineMatrix
978 affine;
979
980 char
981 path[MaxTextExtent];
982
983 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +0000984 (void) FormatLocaleString(path,MaxTextExtent,
cristya8549b12011-05-18 19:05:08 +0000985 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
cristy8cd5b312010-01-07 01:10:24 +0000986 p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
987 affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +0000988 (void) ConcatenateString(&draw_info->primitive,path);
989 return(0);
990}
991
992static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
993{
994 AffineMatrix
995 affine;
996
997 char
998 path[MaxTextExtent];
999
1000 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +00001001 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +00001002 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001003 (void) ConcatenateString(&draw_info->primitive,path);
1004 return(0);
1005}
1006
1007static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1008{
1009 AffineMatrix
1010 affine;
1011
1012 char
1013 path[MaxTextExtent];
1014
1015 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +00001016 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +00001017 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001018 (void) ConcatenateString(&draw_info->primitive,path);
1019 return(0);
1020}
1021
1022static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1023 DrawInfo *draw_info)
1024{
1025 AffineMatrix
1026 affine;
1027
1028 char
1029 path[MaxTextExtent];
1030
1031 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +00001032 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
cristy3ed852e2009-09-05 21:47:34 +00001033 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1034 affine.ty-to->y/64.0);
1035 (void) ConcatenateString(&draw_info->primitive,path);
1036 return(0);
1037}
1038
1039static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001040 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1041 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001042{
1043#if !defined(FT_OPEN_PATHNAME)
1044#define FT_OPEN_PATHNAME ft_open_pathname
1045#endif
1046
1047 typedef struct _GlyphInfo
1048 {
1049 FT_UInt
1050 id;
1051
1052 FT_Vector
1053 origin;
1054
1055 FT_Glyph
1056 image;
1057 } GlyphInfo;
1058
1059 const char
1060 *value;
1061
cristyc9b12952010-03-28 01:12:28 +00001062 double
1063 direction;
1064
cristy3ed852e2009-09-05 21:47:34 +00001065 DrawInfo
1066 *annotate_info;
1067
1068 FT_BBox
1069 bounds;
1070
1071 FT_BitmapGlyph
1072 bitmap;
1073
1074 FT_Encoding
1075 encoding_type;
1076
1077 FT_Error
1078 status;
1079
1080 FT_Face
1081 face;
1082
1083 FT_Int32
1084 flags;
1085
1086 FT_Library
1087 library;
1088
1089 FT_Matrix
1090 affine;
1091
1092 FT_Open_Args
1093 args;
1094
1095 FT_Vector
1096 origin;
1097
1098 GlyphInfo
1099 glyph,
1100 last_glyph;
1101
cristy3ed852e2009-09-05 21:47:34 +00001102 PointInfo
1103 point,
1104 resolution;
1105
1106 register char
1107 *p;
1108
cristy9d314ff2011-03-09 01:30:28 +00001109 ssize_t
1110 code,
1111 y;
1112
cristy3ed852e2009-09-05 21:47:34 +00001113 static FT_Outline_Funcs
1114 OutlineMethods =
1115 {
1116 (FT_Outline_MoveTo_Func) TraceMoveTo,
1117 (FT_Outline_LineTo_Func) TraceLineTo,
1118 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1119 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1120 0, 0
1121 };
1122
cristy2857ffc2010-03-27 23:44:00 +00001123 unsigned char
1124 *utf8;
1125
cristy3ed852e2009-09-05 21:47:34 +00001126 /*
1127 Initialize Truetype library.
1128 */
1129 status=FT_Init_FreeType(&library);
1130 if (status != 0)
1131 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1132 image->filename);
1133 args.flags=FT_OPEN_PATHNAME;
1134 if (draw_info->font == (char *) NULL)
1135 args.pathname=ConstantString("helvetica");
1136 else
1137 if (*draw_info->font != '@')
1138 args.pathname=ConstantString(draw_info->font);
1139 else
1140 args.pathname=ConstantString(draw_info->font+1);
1141 face=(FT_Face) NULL;
cristyeaedf062010-05-29 22:36:02 +00001142 status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
cristy3ed852e2009-09-05 21:47:34 +00001143 args.pathname=DestroyString(args.pathname);
1144 if (status != 0)
1145 {
1146 (void) FT_Done_FreeType(library);
cristy5cbc0162011-08-29 00:36:28 +00001147 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
cristyefe601c2013-01-05 17:51:12 +00001148 "UnableToReadFont","`%s'",draw_info->font);
cristy5cbc0162011-08-29 00:36:28 +00001149 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001150 }
1151 if ((draw_info->metrics != (char *) NULL) &&
1152 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1153 (void) FT_Attach_File(face,draw_info->metrics);
1154 encoding_type=ft_encoding_unicode;
1155 status=FT_Select_Charmap(face,encoding_type);
1156 if ((status != 0) && (face->num_charmaps != 0))
1157 status=FT_Set_Charmap(face,face->charmaps[0]);
1158 if (encoding != (const char *) NULL)
1159 {
1160 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1161 encoding_type=ft_encoding_adobe_custom;
1162 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1163 encoding_type=ft_encoding_adobe_expert;
1164 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1165 encoding_type=ft_encoding_adobe_standard;
1166 if (LocaleCompare(encoding,"AppleRoman") == 0)
1167 encoding_type=ft_encoding_apple_roman;
1168 if (LocaleCompare(encoding,"BIG5") == 0)
1169 encoding_type=ft_encoding_big5;
1170 if (LocaleCompare(encoding,"GB2312") == 0)
1171 encoding_type=ft_encoding_gb2312;
1172 if (LocaleCompare(encoding,"Johab") == 0)
1173 encoding_type=ft_encoding_johab;
1174#if defined(ft_encoding_latin_1)
1175 if (LocaleCompare(encoding,"Latin-1") == 0)
1176 encoding_type=ft_encoding_latin_1;
1177#endif
1178 if (LocaleCompare(encoding,"Latin-2") == 0)
1179 encoding_type=ft_encoding_latin_2;
1180 if (LocaleCompare(encoding,"None") == 0)
1181 encoding_type=ft_encoding_none;
1182 if (LocaleCompare(encoding,"SJIScode") == 0)
1183 encoding_type=ft_encoding_sjis;
1184 if (LocaleCompare(encoding,"Symbol") == 0)
1185 encoding_type=ft_encoding_symbol;
1186 if (LocaleCompare(encoding,"Unicode") == 0)
1187 encoding_type=ft_encoding_unicode;
1188 if (LocaleCompare(encoding,"Wansung") == 0)
1189 encoding_type=ft_encoding_wansung;
1190 status=FT_Select_Charmap(face,encoding_type);
1191 if (status != 0)
1192 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1193 }
1194 /*
1195 Set text size.
1196 */
1197 resolution.x=DefaultResolution;
1198 resolution.y=DefaultResolution;
1199 if (draw_info->density != (char *) NULL)
1200 {
1201 GeometryInfo
1202 geometry_info;
1203
1204 MagickStatusType
1205 flags;
1206
1207 flags=ParseGeometry(draw_info->density,&geometry_info);
1208 resolution.x=geometry_info.rho;
1209 resolution.y=geometry_info.sigma;
1210 if ((flags & SigmaValue) == 0)
1211 resolution.y=resolution.x;
1212 }
1213 status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1214 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1215 (FT_UInt) resolution.y);
1216 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1217 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1218 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1219 metrics->descent=(double) face->size->metrics.descender/64.0;
1220 metrics->width=0;
1221 metrics->origin.x=0;
1222 metrics->origin.y=0;
1223 metrics->height=(double) face->size->metrics.height/64.0;
1224 metrics->max_advance=0.0;
1225 if (face->size->metrics.max_advance > MagickEpsilon)
1226 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1227 metrics->bounds.x1=0.0;
1228 metrics->bounds.y1=metrics->descent;
1229 metrics->bounds.x2=metrics->ascent+metrics->descent;
1230 metrics->bounds.y2=metrics->ascent+metrics->descent;
1231 metrics->underline_position=face->underline_position/64.0;
1232 metrics->underline_thickness=face->underline_thickness/64.0;
cristy71709bf2011-09-25 13:10:29 +00001233 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
cristy3ed852e2009-09-05 21:47:34 +00001234 {
1235 (void) FT_Done_Face(face);
1236 (void) FT_Done_FreeType(library);
1237 return(MagickTrue);
1238 }
1239 /*
1240 Compute bounding box.
1241 */
1242 if (image->debug != MagickFalse)
1243 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
cristye7f51092010-01-17 00:39:37 +00001244 "font-encoding %s; text-encoding %s; pointsize %g",
cristy3ed852e2009-09-05 21:47:34 +00001245 draw_info->font != (char *) NULL ? draw_info->font : "none",
1246 encoding != (char *) NULL ? encoding : "none",
1247 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1248 draw_info->pointsize);
1249 flags=FT_LOAD_NO_BITMAP;
cristy55a804e2012-01-13 17:46:11 +00001250 if (draw_info->text_antialias == MagickFalse)
cristy4f9709e2012-06-10 01:54:51 +00001251 flags|=FT_LOAD_TARGET_MONO;
cristy55a804e2012-01-13 17:46:11 +00001252 else
1253 {
cristya89189d2012-01-13 17:54:57 +00001254#if defined(FT_LOAD_TARGET_LIGHT)
cristy55a804e2012-01-13 17:46:11 +00001255 flags|=FT_LOAD_TARGET_LIGHT;
cristya89189d2012-01-13 17:54:57 +00001256#elif defined(FT_LOAD_TARGET_LCD)
1257 flags|=FT_LOAD_TARGET_LCD;
cristy55a804e2012-01-13 17:46:11 +00001258#endif
1259 }
cristyd15e6592011-10-15 00:13:06 +00001260 value=GetImageProperty(image,"type:hinting",exception);
cristy33204242010-10-07 23:17:12 +00001261 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
cristy3ed852e2009-09-05 21:47:34 +00001262 flags|=FT_LOAD_NO_HINTING;
1263 glyph.id=0;
1264 glyph.image=NULL;
1265 last_glyph.id=0;
1266 last_glyph.image=NULL;
1267 origin.x=0;
1268 origin.y=0;
1269 affine.xx=65536L;
1270 affine.yx=0L;
1271 affine.xy=0L;
1272 affine.yy=65536L;
1273 if (draw_info->render != MagickFalse)
1274 {
1275 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1276 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1277 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1278 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1279 }
1280 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1281 (void) CloneString(&annotate_info->primitive,"path '");
1282 if (draw_info->render != MagickFalse)
1283 {
1284 if (image->storage_class != DirectClass)
cristy5cbc0162011-08-29 00:36:28 +00001285 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a46d822012-08-28 23:32:39 +00001286 if (image->alpha_trait != BlendPixelTrait)
cristy5cbc0162011-08-29 00:36:28 +00001287 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001288 }
cristyc9b12952010-03-28 01:12:28 +00001289 direction=1.0;
cristycf5467b2010-03-28 16:22:06 +00001290 if (draw_info->direction == RightToLeftDirection)
cristyc9b12952010-03-28 01:12:28 +00001291 direction=(-1.0);
cristy3ed852e2009-09-05 21:47:34 +00001292 point.x=0.0;
1293 point.y=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001294 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
cristybd512462010-02-13 02:13:14 +00001295 if (GetUTFCode(p) < 0)
1296 break;
cristy2857ffc2010-03-27 23:44:00 +00001297 utf8=(unsigned char *) NULL;
cristyd913ea12010-02-13 02:32:57 +00001298 if (GetUTFCode(p) == 0)
cristy2857ffc2010-03-27 23:44:00 +00001299 p=draw_info->text;
cristybd512462010-02-13 02:13:14 +00001300 else
cristybd512462010-02-13 02:13:14 +00001301 {
cristy2857ffc2010-03-27 23:44:00 +00001302 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1303 if (utf8 != (unsigned char *) NULL)
cristyc9b12952010-03-28 01:12:28 +00001304 p=(char *) utf8;
cristy2857ffc2010-03-27 23:44:00 +00001305 }
1306 for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1307 {
1308 /*
1309 Render UTF-8 sequence.
1310 */
1311 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1312 if (glyph.id == 0)
1313 glyph.id=FT_Get_Char_Index(face,'?');
1314 if ((glyph.id != 0) && (last_glyph.id != 0))
1315 {
cristyaa83c2c2011-09-21 13:36:25 +00001316 if (fabs(draw_info->kerning) >= MagickEpsilon)
cristy94b11832011-09-08 19:46:03 +00001317 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
cristy2857ffc2010-03-27 23:44:00 +00001318 else
1319 if (FT_HAS_KERNING(face))
1320 {
1321 FT_Vector
1322 kerning;
cristybd512462010-02-13 02:13:14 +00001323
cristy2857ffc2010-03-27 23:44:00 +00001324 status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1325 ft_kerning_default,&kerning);
1326 if (status == 0)
cristy94b11832011-09-08 19:46:03 +00001327 origin.x+=(FT_Pos) (direction*kerning.x);
cristy2857ffc2010-03-27 23:44:00 +00001328 }
1329 }
1330 glyph.origin=origin;
1331 status=FT_Load_Glyph(face,glyph.id,flags);
1332 if (status != 0)
1333 continue;
1334 status=FT_Get_Glyph(face->glyph,&glyph.image);
1335 if (status != 0)
1336 continue;
1337 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1338 &bounds);
1339 if (status != 0)
1340 continue;
1341 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
cristyaa83c2c2011-09-21 13:36:25 +00001342 metrics->bounds.x1=(double) bounds.xMin;
cristy2857ffc2010-03-27 23:44:00 +00001343 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
cristyaa83c2c2011-09-21 13:36:25 +00001344 metrics->bounds.y1=(double) bounds.yMin;
cristy2857ffc2010-03-27 23:44:00 +00001345 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
cristyaa83c2c2011-09-21 13:36:25 +00001346 metrics->bounds.x2=(double) bounds.xMax;
cristy2857ffc2010-03-27 23:44:00 +00001347 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
cristyaa83c2c2011-09-21 13:36:25 +00001348 metrics->bounds.y2=(double) bounds.yMax;
cristy2857ffc2010-03-27 23:44:00 +00001349 if (draw_info->render != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001350 if ((draw_info->stroke.alpha != TransparentAlpha) ||
cristy2857ffc2010-03-27 23:44:00 +00001351 (draw_info->stroke_pattern != (Image *) NULL))
cristybd512462010-02-13 02:13:14 +00001352 {
cristy2857ffc2010-03-27 23:44:00 +00001353 /*
1354 Trace the glyph.
1355 */
1356 annotate_info->affine.tx=glyph.origin.x/64.0;
1357 annotate_info->affine.ty=glyph.origin.y/64.0;
1358 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1359 outline,&OutlineMethods,annotate_info);
1360 }
1361 FT_Vector_Transform(&glyph.origin,&affine);
1362 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1363 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1364 (FT_Vector *) NULL,MagickTrue);
1365 if (status != 0)
1366 continue;
1367 bitmap=(FT_BitmapGlyph) glyph.image;
1368 point.x=offset->x+bitmap->left;
cristy2f5b5c72011-09-16 16:52:00 +00001369 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1370 point.x=offset->x+(origin.x >> 6);
cristy2857ffc2010-03-27 23:44:00 +00001371 point.y=offset->y-bitmap->top;
1372 if (draw_info->render != MagickFalse)
1373 {
1374 CacheView
1375 *image_view;
cristybd512462010-02-13 02:13:14 +00001376
cristy2857ffc2010-03-27 23:44:00 +00001377 MagickBooleanType
1378 status;
1379
cristy2f5b5c72011-09-16 16:52:00 +00001380 register unsigned char
1381 *p;
1382
cristy2857ffc2010-03-27 23:44:00 +00001383 /*
1384 Rasterize the glyph.
1385 */
1386 status=MagickTrue;
cristy46ff2672012-12-14 15:32:26 +00001387 image_view=AcquireAuthenticCacheView(image,exception);
cristy2f5b5c72011-09-16 16:52:00 +00001388 p=bitmap->bitmap.buffer;
cristybb503372010-05-27 20:51:26 +00001389 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
cristy2857ffc2010-03-27 23:44:00 +00001390 {
cristybd512462010-02-13 02:13:14 +00001391 MagickBooleanType
cristy2857ffc2010-03-27 23:44:00 +00001392 active,
1393 sync;
cristybd512462010-02-13 02:13:14 +00001394
cristya19f1d72012-08-07 18:24:38 +00001395 double
cristy2857ffc2010-03-27 23:44:00 +00001396 fill_opacity;
cristybd512462010-02-13 02:13:14 +00001397
cristy101ab702011-10-13 13:06:32 +00001398 PixelInfo
cristy2857ffc2010-03-27 23:44:00 +00001399 fill_color;
cristybd512462010-02-13 02:13:14 +00001400
cristy4c08aed2011-07-01 19:47:50 +00001401 register Quantum
cristy2857ffc2010-03-27 23:44:00 +00001402 *restrict q;
cristybd512462010-02-13 02:13:14 +00001403
cristy88158062011-06-09 00:31:43 +00001404 register ssize_t
1405 x;
1406
cristy73ce31b2010-06-13 23:17:39 +00001407 ssize_t
cristy2f5b5c72011-09-16 16:52:00 +00001408 n,
cristy73ce31b2010-06-13 23:17:39 +00001409 x_offset,
1410 y_offset;
1411
cristy2857ffc2010-03-27 23:44:00 +00001412 if (status == MagickFalse)
1413 continue;
cristybb503372010-05-27 20:51:26 +00001414 x_offset=(ssize_t) ceil(point.x-0.5);
1415 y_offset=(ssize_t) ceil(point.y+y-0.5);
1416 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy2857ffc2010-03-27 23:44:00 +00001417 continue;
cristy4c08aed2011-07-01 19:47:50 +00001418 q=(Quantum *) NULL;
cristybb503372010-05-27 20:51:26 +00001419 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001420 active=MagickFalse;
1421 else
cristybd512462010-02-13 02:13:14 +00001422 {
cristy2857ffc2010-03-27 23:44:00 +00001423 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1424 bitmap->bitmap.width,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001425 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
cristybd512462010-02-13 02:13:14 +00001426 }
cristy854209a2011-09-19 17:02:57 +00001427 n=y*bitmap->bitmap.pitch-1;
1428 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
cristy2857ffc2010-03-27 23:44:00 +00001429 {
cristy854209a2011-09-19 17:02:57 +00001430 n++;
cristy2857ffc2010-03-27 23:44:00 +00001431 x_offset++;
cristy2f5b5c72011-09-16 16:52:00 +00001432 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001433 {
cristyed231572011-07-14 02:18:59 +00001434 q+=GetPixelChannels(image);
cristy2857ffc2010-03-27 23:44:00 +00001435 continue;
1436 }
cristy2f5b5c72011-09-16 16:52:00 +00001437 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
cristya19f1d72012-08-07 18:24:38 +00001438 fill_opacity=(double) (p[n])/(bitmap->bitmap.num_grays-1);
cristy2f5b5c72011-09-16 16:52:00 +00001439 else
1440 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1441 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
cristy2857ffc2010-03-27 23:44:00 +00001442 if (draw_info->text_antialias == MagickFalse)
1443 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1444 if (active == MagickFalse)
1445 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1446 exception);
cristy6193b862011-08-06 16:33:59 +00001447 if (q == (Quantum *) NULL)
cristy2857ffc2010-03-27 23:44:00 +00001448 {
cristyed231572011-07-14 02:18:59 +00001449 q+=GetPixelChannels(image);
cristy2857ffc2010-03-27 23:44:00 +00001450 continue;
1451 }
cristyec061ec2011-10-21 17:41:17 +00001452 GetPixelInfo(image,&fill_color);
cristy2ed42f62011-10-02 19:49:57 +00001453 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1454 exception);
cristy4c08aed2011-07-01 19:47:50 +00001455 fill_opacity=fill_opacity*fill_color.alpha;
1456 CompositePixelOver(image,&fill_color,fill_opacity,q,
1457 GetPixelAlpha(image,q),q);
cristy2857ffc2010-03-27 23:44:00 +00001458 if (active == MagickFalse)
1459 {
1460 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1461 if (sync == MagickFalse)
1462 status=MagickFalse;
1463 }
cristyed231572011-07-14 02:18:59 +00001464 q+=GetPixelChannels(image);
cristybd512462010-02-13 02:13:14 +00001465 }
cristy2857ffc2010-03-27 23:44:00 +00001466 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1467 if (sync == MagickFalse)
1468 status=MagickFalse;
cristybd512462010-02-13 02:13:14 +00001469 }
cristy2857ffc2010-03-27 23:44:00 +00001470 image_view=DestroyCacheView(image_view);
1471 }
1472 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1473 metrics->width=bitmap->left+bitmap->bitmap.width;
cristyaa83c2c2011-09-21 13:36:25 +00001474 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
cristy2857ffc2010-03-27 23:44:00 +00001475 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1476 (IsUTFSpace(code) == MagickFalse))
cristy94b11832011-09-08 19:46:03 +00001477 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
cristy2857ffc2010-03-27 23:44:00 +00001478 else
cristy94b11832011-09-08 19:46:03 +00001479 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
cristyaa83c2c2011-09-21 13:36:25 +00001480 metrics->origin.x=(double) origin.x;
1481 metrics->origin.y=(double) origin.y;
cristybd5a96c2011-08-21 00:04:26 +00001482 if (last_glyph.id != 0)
1483 FT_Done_Glyph(last_glyph.image);
cristy2857ffc2010-03-27 23:44:00 +00001484 last_glyph=glyph;
1485 code=GetUTFCode(p);
1486 }
1487 if (utf8 != (unsigned char *) NULL)
1488 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
cristybd5a96c2011-08-21 00:04:26 +00001489 if (last_glyph.id != 0)
1490 FT_Done_Glyph(last_glyph.image);
cristy4c08aed2011-07-01 19:47:50 +00001491 if ((draw_info->stroke.alpha != TransparentAlpha) ||
cristy3ed852e2009-09-05 21:47:34 +00001492 (draw_info->stroke_pattern != (Image *) NULL))
1493 {
1494 if (draw_info->render != MagickFalse)
1495 {
1496 /*
1497 Draw text stroke.
1498 */
1499 annotate_info->linejoin=RoundJoin;
1500 annotate_info->affine.tx=offset->x;
1501 annotate_info->affine.ty=offset->y;
1502 (void) ConcatenateString(&annotate_info->primitive,"'");
cristy018f07f2011-09-04 21:15:19 +00001503 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001504 }
1505 }
1506 /*
1507 Determine font metrics.
1508 */
1509 glyph.id=FT_Get_Char_Index(face,'_');
1510 glyph.origin=origin;
1511 status=FT_Load_Glyph(face,glyph.id,flags);
1512 if (status == 0)
1513 {
1514 status=FT_Get_Glyph(face->glyph,&glyph.image);
1515 if (status == 0)
1516 {
cristya367d942012-01-20 17:06:21 +00001517 status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
1518 &bounds);
cristy3ed852e2009-09-05 21:47:34 +00001519 if (status == 0)
1520 {
1521 FT_Vector_Transform(&glyph.origin,&affine);
1522 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1523 status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
1524 (FT_Vector *) NULL,MagickTrue);
1525 bitmap=(FT_BitmapGlyph) glyph.image;
1526 if (bitmap->left > metrics->width)
1527 metrics->width=bitmap->left;
1528 }
1529 }
cristye2c81ad2011-08-20 22:54:14 +00001530 FT_Done_Glyph(glyph.image);
cristy3ed852e2009-09-05 21:47:34 +00001531 }
1532 metrics->width-=metrics->bounds.x1/64.0;
cristy55cb9f12012-08-23 09:02:28 +00001533 metrics->width+=annotate_info->stroke_width;
cristy3ed852e2009-09-05 21:47:34 +00001534 metrics->bounds.x1/=64.0;
1535 metrics->bounds.y1/=64.0;
1536 metrics->bounds.x2/=64.0;
1537 metrics->bounds.y2/=64.0;
1538 metrics->origin.x/=64.0;
1539 metrics->origin.y/=64.0;
1540 /*
1541 Relinquish resources.
1542 */
1543 annotate_info=DestroyDrawInfo(annotate_info);
1544 (void) FT_Done_Face(face);
1545 (void) FT_Done_FreeType(library);
1546 return(MagickTrue);
1547}
1548#else
1549static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1550 const char *magick_unused(encoding),const PointInfo *offset,
cristy5cbc0162011-08-29 00:36:28 +00001551 TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001552{
cristy5cbc0162011-08-29 00:36:28 +00001553 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001554 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
cristyee081822011-02-02 19:07:30 +00001555 draw_info->font != (char *) NULL ? draw_info->font : "none");
cristye3b062a2011-12-17 18:34:52 +00001556 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001557}
1558#endif
1559
1560/*
1561%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1562% %
1563% %
1564% %
1565+ R e n d e r P o s t s c r i p t %
1566% %
1567% %
1568% %
1569%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1570%
1571% RenderPostscript() renders text on the image with a Postscript font. It
1572% also returns the bounding box of the text relative to the image.
1573%
1574% The format of the RenderPostscript method is:
1575%
1576% MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001577% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001578%
1579% A description of each parameter follows:
1580%
1581% o image: the image.
1582%
1583% o draw_info: the draw info.
1584%
1585% o offset: (x,y) location of text relative to image.
1586%
1587% o metrics: bounding box of text.
1588%
cristy5cbc0162011-08-29 00:36:28 +00001589% o exception: return any errors or warnings in this structure.
1590%
cristy3ed852e2009-09-05 21:47:34 +00001591*/
1592
1593static inline size_t MagickMin(const size_t x,const size_t y)
1594{
1595 if (x < y)
1596 return(x);
1597 return(y);
1598}
1599
1600static char *EscapeParenthesis(const char *text)
1601{
1602 char
1603 *buffer;
1604
1605 register char
1606 *p;
1607
cristybb503372010-05-27 20:51:26 +00001608 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001609 i;
1610
1611 size_t
1612 escapes;
1613
1614 escapes=0;
1615 buffer=AcquireString(text);
1616 p=buffer;
cristybb503372010-05-27 20:51:26 +00001617 for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
cristy3ed852e2009-09-05 21:47:34 +00001618 {
1619 if ((text[i] == '(') || (text[i] == ')'))
1620 {
1621 *p++='\\';
1622 escapes++;
1623 }
1624 *p++=text[i];
1625 }
1626 *p='\0';
1627 return(buffer);
1628}
1629
1630static MagickBooleanType RenderPostscript(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00001631 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1632 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001633{
1634 char
1635 filename[MaxTextExtent],
1636 geometry[MaxTextExtent],
1637 *text;
1638
1639 FILE
1640 *file;
1641
1642 Image
1643 *annotate_image;
1644
1645 ImageInfo
1646 *annotate_info;
1647
1648 int
1649 unique_file;
1650
cristy3ed852e2009-09-05 21:47:34 +00001651 MagickBooleanType
1652 identity;
1653
1654 PointInfo
1655 extent,
1656 point,
1657 resolution;
1658
cristybb503372010-05-27 20:51:26 +00001659 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001660 i;
1661
cristy14d71292012-05-20 16:48:13 +00001662 size_t
1663 length;
1664
cristy9d314ff2011-03-09 01:30:28 +00001665 ssize_t
1666 y;
1667
cristy3ed852e2009-09-05 21:47:34 +00001668 /*
1669 Render label with a Postscript font.
1670 */
1671 if (image->debug != MagickFalse)
1672 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +00001673 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
cristy3ed852e2009-09-05 21:47:34 +00001674 draw_info->font : "none",draw_info->pointsize);
1675 file=(FILE *) NULL;
1676 unique_file=AcquireUniqueFileResource(filename);
1677 if (unique_file != -1)
1678 file=fdopen(unique_file,"wb");
1679 if ((unique_file == -1) || (file == (FILE *) NULL))
1680 {
cristy5cbc0162011-08-29 00:36:28 +00001681 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
cristy3ed852e2009-09-05 21:47:34 +00001682 return(MagickFalse);
1683 }
cristyb51dff52011-05-19 16:55:47 +00001684 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1685 (void) FormatLocaleFile(file,"/ReencodeType\n");
1686 (void) FormatLocaleFile(file,"{\n");
1687 (void) FormatLocaleFile(file," findfont dup length\n");
1688 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001689 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
cristyb51dff52011-05-19 16:55:47 +00001690 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001691 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
cristyb51dff52011-05-19 16:55:47 +00001692 (void) FormatLocaleFile(file,"} bind def\n");
cristy3ed852e2009-09-05 21:47:34 +00001693 /*
1694 Sample to compute bounding box.
1695 */
cristyaa83c2c2011-09-21 13:36:25 +00001696 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1697 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1698 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001699 extent.x=0.0;
1700 extent.y=0.0;
cristy14d71292012-05-20 16:48:13 +00001701 length=strlen(draw_info->text);
1702 for (i=0; i <= (ssize_t) (length+2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001703 {
1704 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1705 draw_info->affine.ry*2.0*draw_info->pointsize);
1706 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1707 draw_info->affine.sy*2.0*draw_info->pointsize);
1708 if (point.x > extent.x)
1709 extent.x=point.x;
1710 if (point.y > extent.y)
1711 extent.y=point.y;
1712 }
cristyb51dff52011-05-19 16:55:47 +00001713 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
cristy3ed852e2009-09-05 21:47:34 +00001714 extent.x/2.0,extent.y/2.0);
cristyb51dff52011-05-19 16:55:47 +00001715 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
cristy3ed852e2009-09-05 21:47:34 +00001716 draw_info->pointsize);
1717 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1718 (strchr(draw_info->font,'/') != (char *) NULL))
cristyb51dff52011-05-19 16:55:47 +00001719 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001720 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1721 else
cristy1e604812011-05-19 18:07:50 +00001722 (void) FormatLocaleFile(file,
1723 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1724 draw_info->font);
cristyb51dff52011-05-19 16:55:47 +00001725 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
cristy8cd5b312010-01-07 01:10:24 +00001726 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1727 draw_info->affine.sy);
cristy3ed852e2009-09-05 21:47:34 +00001728 text=EscapeParenthesis(draw_info->text);
1729 if (identity == MagickFalse)
cristy1e604812011-05-19 18:07:50 +00001730 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1731 text);
cristyb51dff52011-05-19 16:55:47 +00001732 (void) FormatLocaleFile(file,"(%s) show\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001733 text=DestroyString(text);
cristyb51dff52011-05-19 16:55:47 +00001734 (void) FormatLocaleFile(file,"showpage\n");
cristy3ed852e2009-09-05 21:47:34 +00001735 (void) fclose(file);
cristyb51dff52011-05-19 16:55:47 +00001736 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
cristye8c25f92010-06-03 00:53:06 +00001737 floor(extent.x+0.5),floor(extent.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001738 annotate_info=AcquireImageInfo();
cristyb51dff52011-05-19 16:55:47 +00001739 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
cristy3ed852e2009-09-05 21:47:34 +00001740 filename);
1741 (void) CloneString(&annotate_info->page,geometry);
1742 if (draw_info->density != (char *) NULL)
1743 (void) CloneString(&annotate_info->density,draw_info->density);
1744 annotate_info->antialias=draw_info->text_antialias;
cristy5cbc0162011-08-29 00:36:28 +00001745 annotate_image=ReadImage(annotate_info,exception);
1746 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00001747 annotate_info=DestroyImageInfo(annotate_info);
1748 (void) RelinquishUniqueFileResource(filename);
1749 if (annotate_image == (Image *) NULL)
1750 return(MagickFalse);
cristy55cb9f12012-08-23 09:02:28 +00001751 (void) NegateImage(annotate_image,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001752 resolution.x=DefaultResolution;
1753 resolution.y=DefaultResolution;
1754 if (draw_info->density != (char *) NULL)
1755 {
1756 GeometryInfo
1757 geometry_info;
1758
1759 MagickStatusType
1760 flags;
1761
1762 flags=ParseGeometry(draw_info->density,&geometry_info);
1763 resolution.x=geometry_info.rho;
1764 resolution.y=geometry_info.sigma;
1765 if ((flags & SigmaValue) == 0)
1766 resolution.y=resolution.x;
1767 }
1768 if (identity == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001769 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001770 else
1771 {
1772 RectangleInfo
1773 crop_info;
1774
cristy5cbc0162011-08-29 00:36:28 +00001775 crop_info=GetImageBoundingBox(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001776 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
cristy3ed852e2009-09-05 21:47:34 +00001777 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
cristybb503372010-05-27 20:51:26 +00001778 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
cristy06609ee2010-03-17 20:21:27 +00001779 0.5);
cristyb51dff52011-05-19 16:55:47 +00001780 (void) FormatLocaleString(geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00001781 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
cristye8c25f92010-06-03 00:53:06 +00001782 crop_info.height,(double) crop_info.x,(double) crop_info.y);
cristye941a752011-10-15 01:52:48 +00001783 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001784 }
1785 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1786 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1787 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1788 metrics->ascent=metrics->pixels_per_em.x;
1789 metrics->descent=metrics->pixels_per_em.y/-5.0;
1790 metrics->width=(double) annotate_image->columns/
1791 ExpandAffine(&draw_info->affine);
1792 metrics->height=1.152*metrics->pixels_per_em.x;
1793 metrics->max_advance=metrics->pixels_per_em.x;
1794 metrics->bounds.x1=0.0;
1795 metrics->bounds.y1=metrics->descent;
1796 metrics->bounds.x2=metrics->ascent+metrics->descent;
1797 metrics->bounds.y2=metrics->ascent+metrics->descent;
1798 metrics->underline_position=(-2.0);
1799 metrics->underline_thickness=1.0;
1800 if (draw_info->render == MagickFalse)
1801 {
1802 annotate_image=DestroyImage(annotate_image);
1803 return(MagickTrue);
1804 }
cristy4c08aed2011-07-01 19:47:50 +00001805 if (draw_info->fill.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001806 {
cristybd659482011-10-09 23:19:32 +00001807 CacheView
1808 *annotate_view;
1809
cristy3ed852e2009-09-05 21:47:34 +00001810 MagickBooleanType
1811 sync;
1812
cristy101ab702011-10-13 13:06:32 +00001813 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001814 fill_color;
1815
cristy3ed852e2009-09-05 21:47:34 +00001816 /*
1817 Render fill color.
1818 */
cristy8a46d822012-08-28 23:32:39 +00001819 if (image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001820 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy8a46d822012-08-28 23:32:39 +00001821 if (annotate_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001822 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1823 exception);
1824 fill_color=draw_info->fill;
cristy46ff2672012-12-14 15:32:26 +00001825 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001826 for (y=0; y < (ssize_t) annotate_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001827 {
cristybb503372010-05-27 20:51:26 +00001828 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001829 x;
1830
cristy4c08aed2011-07-01 19:47:50 +00001831 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001832 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001833
1834 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1835 1,exception);
cristy6193b862011-08-06 16:33:59 +00001836 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001837 break;
cristybb503372010-05-27 20:51:26 +00001838 for (x=0; x < (ssize_t) annotate_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001839 {
cristy2ed42f62011-10-02 19:49:57 +00001840 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy55cb9f12012-08-23 09:02:28 +00001841 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1842 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
cristy4c08aed2011-07-01 19:47:50 +00001843 SetPixelRed(annotate_image,fill_color.red,q);
1844 SetPixelGreen(annotate_image,fill_color.green,q);
1845 SetPixelBlue(annotate_image,fill_color.blue,q);
cristyed231572011-07-14 02:18:59 +00001846 q+=GetPixelChannels(annotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001847 }
1848 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1849 if (sync == MagickFalse)
1850 break;
1851 }
1852 annotate_view=DestroyCacheView(annotate_view);
cristy39172402012-03-30 13:04:39 +00001853 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
cristybb503372010-05-27 20:51:26 +00001854 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
cristye941a752011-10-15 01:52:48 +00001855 metrics->descent)-0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00001856 }
1857 annotate_image=DestroyImage(annotate_image);
1858 return(MagickTrue);
1859}
1860
1861/*
1862%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1863% %
1864% %
1865% %
1866+ R e n d e r X 1 1 %
1867% %
1868% %
1869% %
1870%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1871%
1872% RenderX11() renders text on the image with an X11 font. It also returns the
1873% bounding box of the text relative to the image.
1874%
1875% The format of the RenderX11 method is:
1876%
1877% MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001878% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001879%
1880% A description of each parameter follows:
1881%
1882% o image: the image.
1883%
1884% o draw_info: the draw info.
1885%
1886% o offset: (x,y) location of text relative to image.
1887%
1888% o metrics: bounding box of text.
1889%
cristy5cbc0162011-08-29 00:36:28 +00001890% o exception: return any errors or warnings in this structure.
1891%
cristy3ed852e2009-09-05 21:47:34 +00001892*/
1893#if defined(MAGICKCORE_X11_DELEGATE)
1894static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001895 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001896{
1897 MagickBooleanType
1898 status;
1899
1900 static DrawInfo
1901 cache_info;
1902
1903 static Display
1904 *display = (Display *) NULL;
1905
1906 static XAnnotateInfo
1907 annotate_info;
1908
1909 static XFontStruct
1910 *font_info;
1911
1912 static XPixelInfo
1913 pixel;
1914
1915 static XResourceInfo
1916 resource_info;
1917
1918 static XrmDatabase
1919 resource_database;
1920
1921 static XStandardColormap
1922 *map_info;
1923
1924 static XVisualInfo
1925 *visual_info;
1926
cristybb503372010-05-27 20:51:26 +00001927 size_t
cristy3ed852e2009-09-05 21:47:34 +00001928 height,
1929 width;
1930
cristyd7ecaca2011-01-24 14:14:53 +00001931 if (annotate_semaphore == (SemaphoreInfo *) NULL)
1932 AcquireSemaphoreInfo(&annotate_semaphore);
1933 LockSemaphoreInfo(annotate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001934 if (display == (Display *) NULL)
1935 {
cristy104cea82009-10-25 02:26:51 +00001936 const char
1937 *client_name;
1938
cristy3ed852e2009-09-05 21:47:34 +00001939 ImageInfo
1940 *image_info;
1941
1942 /*
1943 Open X server connection.
1944 */
1945 display=XOpenDisplay(draw_info->server_name);
1946 if (display == (Display *) NULL)
1947 {
1948 ThrowXWindowException(XServerError,"UnableToOpenXServer",
1949 draw_info->server_name);
1950 return(MagickFalse);
1951 }
1952 /*
1953 Get user defaults from X resource database.
1954 */
1955 (void) XSetErrorHandler(XError);
1956 image_info=AcquireImageInfo();
cristy104cea82009-10-25 02:26:51 +00001957 client_name=GetClientName();
cristy309c8612009-10-25 13:16:09 +00001958 resource_database=XGetResourceDatabase(display,client_name);
cristyf9d6dc02012-01-19 02:14:44 +00001959 XGetResourceInfo(image_info,resource_database,client_name,
1960 &resource_info);
cristy3ed852e2009-09-05 21:47:34 +00001961 resource_info.close_server=MagickFalse;
1962 resource_info.colormap=PrivateColormap;
1963 resource_info.font=AcquireString(draw_info->font);
1964 resource_info.background_color=AcquireString("#ffffffffffff");
1965 resource_info.foreground_color=AcquireString("#000000000000");
1966 map_info=XAllocStandardColormap();
1967 if (map_info == (XStandardColormap *) NULL)
1968 {
1969 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
1970 image->filename);
1971 return(MagickFalse);
1972 }
1973 /*
1974 Initialize visual info.
1975 */
1976 visual_info=XBestVisualInfo(display,map_info,&resource_info);
1977 if (visual_info == (XVisualInfo *) NULL)
1978 {
1979 ThrowXWindowException(XServerError,"UnableToGetVisual",
1980 image->filename);
1981 return(MagickFalse);
1982 }
1983 map_info->colormap=(Colormap) NULL;
cristyf2faecf2010-05-28 19:19:36 +00001984 pixel.pixels=(unsigned long *) NULL;
cristy3ed852e2009-09-05 21:47:34 +00001985 /*
1986 Initialize Standard Colormap info.
1987 */
1988 XGetMapInfo(visual_info,XDefaultColormap(display,visual_info->screen),
1989 map_info);
cristy4c08aed2011-07-01 19:47:50 +00001990 XGetPixelInfo(display,visual_info,map_info,&resource_info,(Image *) NULL,
1991 &pixel);
cristy3ed852e2009-09-05 21:47:34 +00001992 pixel.annotate_context=XDefaultGC(display,visual_info->screen);
1993 /*
1994 Initialize font info.
1995 */
1996 font_info=XBestFont(display,&resource_info,MagickFalse);
1997 if (font_info == (XFontStruct *) NULL)
1998 {
1999 ThrowXWindowException(XServerError,"UnableToLoadFont",
2000 draw_info->font);
2001 return(MagickFalse);
2002 }
2003 if ((map_info == (XStandardColormap *) NULL) ||
2004 (visual_info == (XVisualInfo *) NULL) ||
2005 (font_info == (XFontStruct *) NULL))
2006 {
2007 XFreeResources(display,visual_info,map_info,&pixel,font_info,
2008 &resource_info,(XWindowInfo *) NULL);
2009 ThrowXWindowException(XServerError,"UnableToLoadFont",
2010 image->filename);
2011 return(MagickFalse);
2012 }
2013 cache_info=(*draw_info);
2014 }
cristyd7ecaca2011-01-24 14:14:53 +00002015 UnlockSemaphoreInfo(annotate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00002016 /*
2017 Initialize annotate info.
2018 */
2019 XGetAnnotateInfo(&annotate_info);
2020 annotate_info.stencil=ForegroundStencil;
2021 if (cache_info.font != draw_info->font)
2022 {
2023 /*
2024 Type name has changed.
2025 */
2026 (void) XFreeFont(display,font_info);
2027 (void) CloneString(&resource_info.font,draw_info->font);
2028 font_info=XBestFont(display,&resource_info,MagickFalse);
2029 if (font_info == (XFontStruct *) NULL)
2030 {
2031 ThrowXWindowException(XServerError,"UnableToLoadFont",
2032 draw_info->font);
2033 return(MagickFalse);
2034 }
2035 }
2036 if (image->debug != MagickFalse)
2037 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +00002038 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
cristy3ed852e2009-09-05 21:47:34 +00002039 draw_info->font : "none",draw_info->pointsize);
2040 cache_info=(*draw_info);
2041 annotate_info.font_info=font_info;
2042 annotate_info.text=(char *) draw_info->text;
2043 annotate_info.width=(unsigned int) XTextWidth(font_info,draw_info->text,
2044 (int) strlen(draw_info->text));
2045 annotate_info.height=(unsigned int) font_info->ascent+font_info->descent;
2046 metrics->pixels_per_em.x=(double) font_info->max_bounds.width;
2047 metrics->pixels_per_em.y=(double) font_info->ascent+font_info->descent;
2048 metrics->ascent=(double) font_info->ascent+4;
2049 metrics->descent=(double) (-font_info->descent);
2050 metrics->width=annotate_info.width/ExpandAffine(&draw_info->affine);
2051 metrics->height=font_info->ascent+font_info->descent;
2052 metrics->max_advance=(double) font_info->max_bounds.width;
2053 metrics->bounds.x1=0.0;
2054 metrics->bounds.y1=metrics->descent;
2055 metrics->bounds.x2=metrics->ascent+metrics->descent;
2056 metrics->bounds.y2=metrics->ascent+metrics->descent;
2057 metrics->underline_position=(-2.0);
2058 metrics->underline_thickness=1.0;
2059 if (draw_info->render == MagickFalse)
2060 return(MagickTrue);
cristy4c08aed2011-07-01 19:47:50 +00002061 if (draw_info->fill.alpha == TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +00002062 return(MagickTrue);
2063 /*
2064 Render fill color.
2065 */
2066 width=annotate_info.width;
2067 height=annotate_info.height;
cristyaa83c2c2011-09-21 13:36:25 +00002068 if ((fabs(draw_info->affine.rx) >= MagickEpsilon) ||
2069 (fabs(draw_info->affine.ry) >= MagickEpsilon))
cristy3ed852e2009-09-05 21:47:34 +00002070 {
cristyaa83c2c2011-09-21 13:36:25 +00002071 if ((fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2072 (fabs(draw_info->affine.rx+draw_info->affine.ry) < MagickEpsilon))
2073 annotate_info.degrees=(double) (180.0/MagickPI)*
cristy854209a2011-09-19 17:02:57 +00002074 atan2(draw_info->affine.rx,draw_info->affine.sx);
cristy3ed852e2009-09-05 21:47:34 +00002075 }
cristyb51dff52011-05-19 16:55:47 +00002076 (void) FormatLocaleString(annotate_info.geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00002077 "%.20gx%.20g%+.20g%+.20g",(double) width,(double) height,
cristye8c25f92010-06-03 00:53:06 +00002078 ceil(offset->x-0.5),ceil(offset->y-metrics->ascent-metrics->descent+
cristy06609ee2010-03-17 20:21:27 +00002079 draw_info->interline_spacing-0.5));
cristy3ed852e2009-09-05 21:47:34 +00002080 pixel.pen_color.red=ScaleQuantumToShort(draw_info->fill.red);
2081 pixel.pen_color.green=ScaleQuantumToShort(draw_info->fill.green);
2082 pixel.pen_color.blue=ScaleQuantumToShort(draw_info->fill.blue);
cristy7c3af952011-10-20 16:04:16 +00002083 status=XAnnotateImage(display,&pixel,&annotate_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002084 if (status == 0)
2085 {
2086 ThrowXWindowException(ResourceLimitError,"MemoryAllocationFailed",
2087 image->filename);
2088 return(MagickFalse);
2089 }
2090 return(MagickTrue);
2091}
2092#else
2093static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00002094 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002095{
2096 (void) draw_info;
2097 (void) offset;
2098 (void) metrics;
cristy5cbc0162011-08-29 00:36:28 +00002099 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002100 MissingDelegateError,"DelegateLibrarySupportNotBuiltIn","'%s' (X11)",
cristy3ed852e2009-09-05 21:47:34 +00002101 image->filename);
2102 return(MagickFalse);
2103}
2104#endif