blob: 52ead5b4223631fceee0052afdc8a0ac734ff797 [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
cristyb56bb242014-11-25 17:12:48 +000020% Copyright 1999-2015 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"
cristyc9afe162013-05-28 16:07:26 +000050#include "MagickCore/channel.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/client.h"
52#include "MagickCore/color.h"
53#include "MagickCore/color-private.h"
cristy54b92622012-04-09 18:42:06 +000054#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/composite.h"
56#include "MagickCore/composite-private.h"
57#include "MagickCore/constitute.h"
58#include "MagickCore/draw.h"
59#include "MagickCore/draw-private.h"
cristy3644bdf2012-08-23 09:07:08 +000060#include "MagickCore/enhance.h"
cristy4c08aed2011-07-01 19:47:50 +000061#include "MagickCore/exception.h"
62#include "MagickCore/exception-private.h"
63#include "MagickCore/gem.h"
64#include "MagickCore/geometry.h"
65#include "MagickCore/image-private.h"
66#include "MagickCore/log.h"
67#include "MagickCore/quantum.h"
68#include "MagickCore/quantum-private.h"
69#include "MagickCore/pixel-accessor.h"
70#include "MagickCore/property.h"
71#include "MagickCore/resource_.h"
72#include "MagickCore/semaphore.h"
73#include "MagickCore/statistic.h"
74#include "MagickCore/string_.h"
75#include "MagickCore/token-private.h"
76#include "MagickCore/transform.h"
77#include "MagickCore/type.h"
78#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000079#include "MagickCore/utility-private.h"
cristybcbda3f2011-09-03 13:01:22 +000080#include "MagickCore/xwindow.h"
cristy4c08aed2011-07-01 19:47:50 +000081#include "MagickCore/xwindow-private.h"
cristy3ed852e2009-09-05 21:47:34 +000082#if defined(MAGICKCORE_FREETYPE_DELEGATE)
cristy07a3cca2012-12-10 13:09:10 +000083#if defined(__MINGW32__) || defined(__MINGW64__)
cristy3ed852e2009-09-05 21:47:34 +000084# undef interface
85#endif
cristy03f187e2013-01-24 00:22:19 +000086#include <ft2build.h>
cristy3ed852e2009-09-05 21:47:34 +000087#if defined(FT_FREETYPE_H)
88# include FT_FREETYPE_H
89#else
90# include <freetype/freetype.h>
91#endif
92#if defined(FT_GLYPH_H)
93# include FT_GLYPH_H
94#else
95# include <freetype/ftglyph.h>
96#endif
97#if defined(FT_OUTLINE_H)
98# include FT_OUTLINE_H
99#else
100# include <freetype/ftoutln.h>
101#endif
102#if defined(FT_BBOX_H)
103# include FT_BBOX_H
104#else
105# include <freetype/ftbbox.h>
106#endif /* defined(FT_BBOX_H) */
107#endif
108
109/*
cristyd7ecaca2011-01-24 14:14:53 +0000110 Annotate semaphores.
111*/
112static SemaphoreInfo
113 *annotate_semaphore = (SemaphoreInfo *) NULL;
114
115/*
cristy3ed852e2009-09-05 21:47:34 +0000116 Forward declarations.
117*/
118static MagickBooleanType
cristy5cbc0162011-08-29 00:36:28 +0000119 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
120 ExceptionInfo *),
121 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
122 ExceptionInfo *),
cristy3ed852e2009-09-05 21:47:34 +0000123 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
cristy5cbc0162011-08-29 00:36:28 +0000124 TypeMetric *,ExceptionInfo *),
125 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
126 ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000127
128/*
129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130% %
131% %
132% %
cristyd7ecaca2011-01-24 14:14:53 +0000133+ A n n o t a t e C o m p o n e n t G e n e s i s %
134% %
135% %
136% %
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138%
139% AnnotateComponentGenesis() instantiates the annotate component.
140%
141% The format of the AnnotateComponentGenesis method is:
142%
143% MagickBooleanType AnnotateComponentGenesis(void)
144%
145*/
cristy5ff4eaf2011-09-03 01:38:02 +0000146MagickPrivate MagickBooleanType AnnotateComponentGenesis(void)
cristyd7ecaca2011-01-24 14:14:53 +0000147{
cristy7c977062014-04-04 14:05:53 +0000148 if (annotate_semaphore == (SemaphoreInfo *) NULL)
149 annotate_semaphore=AcquireSemaphoreInfo();
cristyd7ecaca2011-01-24 14:14:53 +0000150 return(MagickTrue);
151}
152
153/*
154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155% %
156% %
157% %
158+ 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 %
159% %
160% %
161% %
162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163%
164% AnnotateComponentTerminus() destroys the annotate component.
165%
166% The format of the AnnotateComponentTerminus method is:
167%
168% AnnotateComponentTerminus(void)
169%
170*/
cristy5ff4eaf2011-09-03 01:38:02 +0000171MagickPrivate void AnnotateComponentTerminus(void)
cristyd7ecaca2011-01-24 14:14:53 +0000172{
173 if (annotate_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000174 ActivateSemaphoreInfo(&annotate_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000175 RelinquishSemaphoreInfo(&annotate_semaphore);
cristyd7ecaca2011-01-24 14:14:53 +0000176}
177
178/*
179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180% %
181% %
182% %
cristy3ed852e2009-09-05 21:47:34 +0000183% A n n o t a t e I m a g e %
184% %
185% %
186% %
187%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188%
189% AnnotateImage() annotates an image with text. Optionally you can include
190% any of the following bits of information about the image by embedding
191% the appropriate special characters:
192%
cristy528127a2014-12-14 20:10:00 +0000193% \n newline
194% \r carriage return
195% < less-than character.
196% > greater-than character.
197% & ampersand character.
198% %% a percent sign
199% %b file size of image read in
200% %c comment meta-data property
201% %d directory component of path
202% %e filename extension or suffix
203% %f filename (including suffix)
204% %g layer canvas page geometry (equivalent to "%Wx%H%X%Y")
205% %h current image height in pixels
206% %i image filename (note: becomes output filename for "info:")
207% %k CALCULATED: number of unique colors
208% %l label meta-data property
209% %m image file format (file magic)
210% %n number of images in current image sequence
211% %o output filename (used for delegates)
212% %p index of image in current image list
213% %q quantum depth (compile-time constant)
214% %r image class and colorspace
215% %s scene number (from input unless re-assigned)
216% %t filename without directory or extension (suffix)
217% %u unique temporary filename (used for delegates)
218% %w current width in pixels
219% %x x resolution (density)
220% %y y resolution (density)
221% %z image depth (as read in unless modified, image save depth)
222% %A image transparency channel enabled (true/false)
223% %C image compression type
224% %D image GIF dispose method
225% %G original image size (%wx%h; before any resizes)
226% %H page (canvas) height
227% %M Magick filename (original file exactly as given, including read mods)
228% %O page (canvas) offset ( = %X%Y )
229% %P page (canvas) size ( = %Wx%H )
230% %Q image compression quality ( 0 = default )
231% %S ?? scenes ??
232% %T image time delay (in centi-seconds)
233% %U image resolution units
234% %W page (canvas) width
235% %X page (canvas) x offset (including sign)
236% %Y page (canvas) y offset (including sign)
237% %Z unique filename (used for delegates)
238% %@ CALCULATED: trim bounding box (without actually trimming)
239% %# CALCULATED: 'signature' hash of image values
cristy3ed852e2009-09-05 21:47:34 +0000240%
241% The format of the AnnotateImage method is:
242%
cristy5cbc0162011-08-29 00:36:28 +0000243% MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
244% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000245%
246% A description of each parameter follows:
247%
248% o image: the image.
249%
250% o draw_info: the draw info.
251%
cristy5cbc0162011-08-29 00:36:28 +0000252% o exception: return any errors or warnings in this structure.
253%
cristy3ed852e2009-09-05 21:47:34 +0000254*/
255MagickExport MagickBooleanType AnnotateImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000256 const DrawInfo *draw_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000257{
258 char
cristy151b66d2015-04-15 10:50:31 +0000259 primitive[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +0000260 **textlist;
261
262 DrawInfo
263 *annotate,
264 *annotate_info;
265
266 GeometryInfo
267 geometry_info;
268
269 MagickBooleanType
270 status;
271
272 PointInfo
273 offset;
274
275 RectangleInfo
276 geometry;
277
cristybb503372010-05-27 20:51:26 +0000278 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000279 i;
280
281 size_t
282 length;
283
284 TypeMetric
285 metrics;
286
cristybb503372010-05-27 20:51:26 +0000287 size_t
cristy58460392009-09-09 01:52:06 +0000288 height,
cristy3ed852e2009-09-05 21:47:34 +0000289 number_lines;
290
291 assert(image != (Image *) NULL);
292 assert(image->signature == MagickSignature);
293 if (image->debug != MagickFalse)
294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
295 assert(draw_info != (DrawInfo *) NULL);
296 assert(draw_info->signature == MagickSignature);
297 if (draw_info->text == (char *) NULL)
298 return(MagickFalse);
299 if (*draw_info->text == '\0')
300 return(MagickTrue);
301 textlist=StringToList(draw_info->text);
302 if (textlist == (char **) NULL)
303 return(MagickFalse);
304 length=strlen(textlist[0]);
305 for (i=1; textlist[i] != (char *) NULL; i++)
306 if (strlen(textlist[i]) > length)
307 length=strlen(textlist[i]);
cristybb503372010-05-27 20:51:26 +0000308 number_lines=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000309 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
310 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
311 SetGeometry(image,&geometry);
312 SetGeometryInfo(&geometry_info);
313 if (annotate_info->geometry != (char *) NULL)
314 {
315 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
cristy5cbc0162011-08-29 00:36:28 +0000316 exception);
cristy3ed852e2009-09-05 21:47:34 +0000317 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
318 }
cristy5cbc0162011-08-29 00:36:28 +0000319 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000320 return(MagickFalse);
cristya6400b12013-03-15 12:20:18 +0000321 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +0000322 (void) SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000323 status=MagickTrue;
324 for (i=0; textlist[i] != (char *) NULL; i++)
325 {
326 /*
327 Position text relative to image.
328 */
329 annotate_info->affine.tx=geometry_info.xi-image->page.x;
330 annotate_info->affine.ty=geometry_info.psi-image->page.y;
331 (void) CloneString(&annotate->text,textlist[i]);
cristy5cbc0162011-08-29 00:36:28 +0000332 (void) GetTypeMetrics(image,annotate,&metrics,exception);
cristybb503372010-05-27 20:51:26 +0000333 height=(ssize_t) (metrics.ascent-metrics.descent+
cristy58460392009-09-09 01:52:06 +0000334 draw_info->interline_spacing+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000335 switch (annotate->gravity)
336 {
337 case UndefinedGravity:
338 default:
339 {
340 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
341 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
342 break;
343 }
344 case NorthWestGravity:
345 {
346 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
347 annotate_info->affine.ry*height+annotate_info->affine.ry*
348 (metrics.ascent+metrics.descent);
349 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
350 annotate_info->affine.sy*height+annotate_info->affine.sy*
351 metrics.ascent;
352 break;
353 }
354 case NorthGravity:
355 {
356 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
357 geometry.width/2.0+i*annotate_info->affine.ry*height-
358 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
359 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
360 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
361 annotate_info->affine.sy*height+annotate_info->affine.sy*
362 metrics.ascent-annotate_info->affine.rx*(metrics.width-
363 metrics.bounds.x1)/2.0;
364 break;
365 }
366 case NorthEastGravity:
367 {
368 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
369 geometry.width+i*annotate_info->affine.ry*height-
370 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
371 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
372 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
373 annotate_info->affine.sy*height+annotate_info->affine.sy*
374 metrics.ascent-annotate_info->affine.rx*(metrics.width-
375 metrics.bounds.x1);
376 break;
377 }
378 case WestGravity:
379 {
380 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
381 annotate_info->affine.ry*height+annotate_info->affine.ry*
382 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
383 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
384 geometry.height/2.0+i*annotate_info->affine.sy*height+
385 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
cristy7096f1c2015-05-25 23:43:18 +0000386 (number_lines-1.0)*height)/2.0+metrics.descent/2.0;
cristy3ed852e2009-09-05 21:47:34 +0000387 break;
388 }
cristy3ed852e2009-09-05 21:47:34 +0000389 case CenterGravity:
390 {
391 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
392 geometry.width/2.0+i*annotate_info->affine.ry*height-
393 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
394 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
395 (number_lines-1)*height)/2.0;
396 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
397 geometry.height/2.0+i*annotate_info->affine.sy*height-
398 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
399 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
cristybe79a702015-05-25 23:36:23 +0000400 (number_lines-1.0)*height)/2.0+metrics.descent/2.0;
cristy3ed852e2009-09-05 21:47:34 +0000401 break;
402 }
403 case EastGravity:
404 {
405 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
406 geometry.width+i*annotate_info->affine.ry*height-
407 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
408 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
409 (number_lines-1.0)*height)/2.0-1.0;
410 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
411 geometry.height/2.0+i*annotate_info->affine.sy*height-
412 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+
413 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
cristy7096f1c2015-05-25 23:43:18 +0000414 (number_lines-1.0)*height)/2.0+metrics.descent/2.0;
cristy3ed852e2009-09-05 21:47:34 +0000415 break;
416 }
417 case SouthWestGravity:
418 {
419 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
420 annotate_info->affine.ry*height-annotate_info->affine.ry*
421 (number_lines-1.0)*height;
422 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
423 geometry.height+i*annotate_info->affine.sy*height-
424 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
425 break;
426 }
427 case SouthGravity:
428 {
429 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
430 geometry.width/2.0+i*annotate_info->affine.ry*height-
431 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
432 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
433 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
434 geometry.height+i*annotate_info->affine.sy*height-
435 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
436 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
437 break;
438 }
439 case SouthEastGravity:
440 {
441 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
442 geometry.width+i*annotate_info->affine.ry*height-
443 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
444 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
445 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
446 geometry.height+i*annotate_info->affine.sy*height-
447 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
448 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
449 break;
450 }
451 }
452 switch (annotate->align)
453 {
454 case LeftAlign:
455 {
456 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
457 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
458 break;
459 }
460 case CenterAlign:
461 {
462 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
463 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
464 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
465 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
466 break;
467 }
468 case RightAlign:
469 {
470 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
471 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
472 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
473 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
474 break;
475 }
476 default:
477 break;
478 }
cristy4c08aed2011-07-01 19:47:50 +0000479 if (draw_info->undercolor.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000480 {
481 DrawInfo
482 *undercolor_info;
483
484 /*
485 Text box.
486 */
487 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
488 undercolor_info->fill=draw_info->undercolor;
489 undercolor_info->affine=draw_info->affine;
490 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
491 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
cristy151b66d2015-04-15 10:50:31 +0000492 (void) FormatLocaleString(primitive,MagickPathExtent,
cristy49863722015-05-31 22:31:02 +0000493 "rectangle -0.5,-0.5 %g,%.20g",metrics.origin.x+0.5,(double) height+
494 0.5);
cristy3ed852e2009-09-05 21:47:34 +0000495 (void) CloneString(&undercolor_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000496 (void) DrawImage(image,undercolor_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000497 (void) DestroyDrawInfo(undercolor_info);
498 }
499 annotate_info->affine.tx=offset.x;
500 annotate_info->affine.ty=offset.y;
cristy151b66d2015-04-15 10:50:31 +0000501 (void) FormatLocaleString(primitive,MagickPathExtent,"stroke-width %g "
cristya8549b12011-05-18 19:05:08 +0000502 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
cristy3ed852e2009-09-05 21:47:34 +0000503 if (annotate->decorate == OverlineDecoration)
504 {
505 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
506 metrics.descent-metrics.underline_position));
507 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000508 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000509 }
510 else
511 if (annotate->decorate == UnderlineDecoration)
512 {
513 annotate_info->affine.ty-=(draw_info->affine.sy*
514 metrics.underline_position);
515 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000516 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000517 }
518 /*
519 Annotate image with text.
520 */
cristy5cbc0162011-08-29 00:36:28 +0000521 status=RenderType(image,annotate,&offset,&metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000522 if (status == MagickFalse)
523 break;
524 if (annotate->decorate == LineThroughDecoration)
525 {
526 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
527 metrics.underline_position+metrics.descent)/2.0);
528 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000529 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000530 }
531 }
532 /*
533 Relinquish resources.
534 */
535 annotate_info=DestroyDrawInfo(annotate_info);
536 annotate=DestroyDrawInfo(annotate);
537 for (i=0; textlist[i] != (char *) NULL; i++)
538 textlist[i]=DestroyString(textlist[i]);
539 textlist=(char **) RelinquishMagickMemory(textlist);
540 return(status);
541}
542
543/*
544%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545% %
546% %
547% %
548% F o r m a t M a g i c k C a p t i o n %
549% %
550% %
551% %
552%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553%
554% FormatMagickCaption() formats a caption so that it fits within the image
555% width. It returns the number of lines in the formatted caption.
556%
557% The format of the FormatMagickCaption method is:
558%
cristybb503372010-05-27 20:51:26 +0000559% ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000560% const MagickBooleanType split,TypeMetric *metrics,char **caption,
561% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000562%
563% A description of each parameter follows.
564%
565% o image: The image.
566%
cristy3ed852e2009-09-05 21:47:34 +0000567% o draw_info: the draw info.
568%
cristy6b1d05e2010-09-22 19:17:27 +0000569% o split: when no convenient line breaks-- insert newline.
570%
cristy3ed852e2009-09-05 21:47:34 +0000571% o metrics: Return the font metrics in this structure.
572%
cristy6b1d05e2010-09-22 19:17:27 +0000573% o caption: the caption.
574%
cristy5cbc0162011-08-29 00:36:28 +0000575% o exception: return any errors or warnings in this structure.
576%
cristy3ed852e2009-09-05 21:47:34 +0000577*/
cristybb503372010-05-27 20:51:26 +0000578MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000579 const MagickBooleanType split,TypeMetric *metrics,char **caption,
580 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000581{
cristy74966db2011-09-20 19:37:21 +0000582 char
583 *text;
584
cristy3ed852e2009-09-05 21:47:34 +0000585 MagickBooleanType
586 status;
587
588 register char
589 *p,
590 *q,
591 *s;
592
cristybb503372010-05-27 20:51:26 +0000593 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000594 i;
595
cristybb503372010-05-27 20:51:26 +0000596 size_t
cristy3ed852e2009-09-05 21:47:34 +0000597 width;
598
cristy6193b862011-08-06 16:33:59 +0000599 ssize_t
600 n;
601
cristy74966db2011-09-20 19:37:21 +0000602 text=AcquireString(draw_info->text);
cristy3ed852e2009-09-05 21:47:34 +0000603 q=draw_info->text;
604 s=(char *) NULL;
605 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
606 {
607 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
608 s=p;
cristy2911f2d2011-08-20 23:04:43 +0000609 if (GetUTFCode(p) == '\n')
cristye2c81ad2011-08-20 22:54:14 +0000610 q=draw_info->text;
cristybb503372010-05-27 20:51:26 +0000611 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
cristy3ed852e2009-09-05 21:47:34 +0000612 *q++=(*(p+i));
613 *q='\0';
cristy5cbc0162011-08-29 00:36:28 +0000614 status=GetTypeMetrics(image,draw_info,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000615 if (status == MagickFalse)
616 break;
cristy78fed5e2013-06-02 17:24:16 +0000617 width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
cristy74966db2011-09-20 19:37:21 +0000618 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
cristy6193b862011-08-06 16:33:59 +0000619 continue;
cristy74966db2011-09-20 19:37:21 +0000620 (void) strcpy(text,draw_info->text);
cristy4aa314e2011-08-19 13:59:45 +0000621 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
cristy3ed852e2009-09-05 21:47:34 +0000622 {
623 *s='\n';
624 p=s;
625 }
626 else
cristy4aa314e2011-08-19 13:59:45 +0000627 if ((s != (char *) NULL) || (split != MagickFalse))
cristy6b1d05e2010-09-22 19:17:27 +0000628 {
629 char
630 *target;
cristy3ed852e2009-09-05 21:47:34 +0000631
cristy6b1d05e2010-09-22 19:17:27 +0000632 /*
633 No convenient line breaks-- insert newline.
634 */
635 target=AcquireString(*caption);
636 n=p-(*caption);
637 CopyMagickString(target,*caption,n+1);
638 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
639 ConcatenateMagickString(target,p,strlen(*caption)+2);
640 (void) DestroyString(*caption);
641 *caption=target;
642 p=(*caption)+n;
643 }
cristy3ed852e2009-09-05 21:47:34 +0000644 q=draw_info->text;
cristy6193b862011-08-06 16:33:59 +0000645 s=(char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000646 }
cristy74966db2011-09-20 19:37:21 +0000647 text=DestroyString(text);
cristy6193b862011-08-06 16:33:59 +0000648 n=0;
cristy3ed852e2009-09-05 21:47:34 +0000649 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
650 if (GetUTFCode(p) == '\n')
cristy6193b862011-08-06 16:33:59 +0000651 n++;
652 return(n);
cristy3ed852e2009-09-05 21:47:34 +0000653}
654
655/*
656%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657% %
658% %
659% %
660% G e t M u l t i l i n e T y p e M e t r i c s %
661% %
662% %
663% %
664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665%
666% GetMultilineTypeMetrics() returns the following information for the
667% specified font and text:
668%
669% character width
670% character height
671% ascender
672% descender
673% text width
674% text height
675% maximum horizontal advance
676% bounds: x1
677% bounds: y1
678% bounds: x2
679% bounds: y2
680% origin: x
681% origin: y
682% underline position
683% underline thickness
684%
685% This method is like GetTypeMetrics() but it returns the maximum text width
686% and height for multiple lines of text.
687%
688% The format of the GetMultilineTypeMetrics method is:
689%
690% MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000691% const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000692%
693% A description of each parameter follows:
694%
695% o image: the image.
696%
697% o draw_info: the draw info.
698%
699% o metrics: Return the font metrics in this structure.
700%
cristy5cbc0162011-08-29 00:36:28 +0000701% o exception: return any errors or warnings in this structure.
702%
cristy3ed852e2009-09-05 21:47:34 +0000703*/
704MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000705 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000706{
707 char
708 **textlist;
709
710 DrawInfo
711 *annotate_info;
712
713 MagickBooleanType
714 status;
715
cristybb503372010-05-27 20:51:26 +0000716 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000717 i;
718
719 TypeMetric
720 extent;
721
cristy3ed852e2009-09-05 21:47:34 +0000722 assert(image != (Image *) NULL);
723 assert(image->signature == MagickSignature);
724 if (image->debug != MagickFalse)
725 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
726 assert(draw_info != (DrawInfo *) NULL);
727 assert(draw_info->text != (char *) NULL);
728 assert(draw_info->signature == MagickSignature);
729 if (*draw_info->text == '\0')
730 return(MagickFalse);
731 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
732 annotate_info->text=DestroyString(annotate_info->text);
733 /*
734 Convert newlines to multiple lines of text.
735 */
736 textlist=StringToList(draw_info->text);
737 if (textlist == (char **) NULL)
738 return(MagickFalse);
739 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000740 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000741 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
742 (void) ResetMagickMemory(&extent,0,sizeof(extent));
743 /*
744 Find the widest of the text lines.
745 */
746 annotate_info->text=textlist[0];
cristy5cbc0162011-08-29 00:36:28 +0000747 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000748 *metrics=extent;
749 for (i=1; textlist[i] != (char *) NULL; i++)
750 {
751 annotate_info->text=textlist[i];
cristy5cbc0162011-08-29 00:36:28 +0000752 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000753 if (extent.width > metrics->width)
754 *metrics=extent;
755 }
cristye2c81ad2011-08-20 22:54:14 +0000756 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
757 (i-1)*draw_info->interline_spacing);
cristy3ed852e2009-09-05 21:47:34 +0000758 /*
759 Relinquish resources.
760 */
761 annotate_info->text=(char *) NULL;
762 annotate_info=DestroyDrawInfo(annotate_info);
763 for (i=0; textlist[i] != (char *) NULL; i++)
764 textlist[i]=DestroyString(textlist[i]);
765 textlist=(char **) RelinquishMagickMemory(textlist);
766 return(status);
767}
768
769/*
770%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
771% %
772% %
773% %
774% G e t T y p e M e t r i c s %
775% %
776% %
777% %
778%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779%
780% GetTypeMetrics() returns the following information for the specified font
781% and text:
782%
783% character width
784% character height
785% ascender
786% descender
787% text width
788% text height
789% maximum horizontal advance
790% bounds: x1
791% bounds: y1
792% bounds: x2
793% bounds: y2
794% origin: x
795% origin: y
796% underline position
797% underline thickness
798%
799% The format of the GetTypeMetrics method is:
800%
801% MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000802% TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000803%
804% A description of each parameter follows:
805%
806% o image: the image.
807%
808% o draw_info: the draw info.
809%
810% o metrics: Return the font metrics in this structure.
811%
cristy5cbc0162011-08-29 00:36:28 +0000812% o exception: return any errors or warnings in this structure.
813%
cristy3ed852e2009-09-05 21:47:34 +0000814*/
815MagickExport MagickBooleanType GetTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000816 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000817{
818 DrawInfo
819 *annotate_info;
820
821 MagickBooleanType
822 status;
823
824 PointInfo
825 offset;
826
827 assert(image != (Image *) NULL);
828 assert(image->signature == MagickSignature);
829 if (image->debug != MagickFalse)
830 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
831 assert(draw_info != (DrawInfo *) NULL);
832 assert(draw_info->text != (char *) NULL);
833 assert(draw_info->signature == MagickSignature);
834 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
835 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000836 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000837 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
838 offset.x=0.0;
839 offset.y=0.0;
cristy5cbc0162011-08-29 00:36:28 +0000840 status=RenderType(image,annotate_info,&offset,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000841 if (image->debug != MagickFalse)
842 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
cristye7f51092010-01-17 00:39:37 +0000843 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
cristya8549b12011-05-18 19:05:08 +0000844 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
cristye7f51092010-01-17 00:39:37 +0000845 "underline position: %g; underline thickness: %g",annotate_info->text,
cristy3ed852e2009-09-05 21:47:34 +0000846 metrics->width,metrics->height,metrics->ascent,metrics->descent,
847 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
cristy4c08aed2011-07-01 19:47:50 +0000848 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
849 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
cristy3ed852e2009-09-05 21:47:34 +0000850 metrics->underline_position,metrics->underline_thickness);
851 annotate_info=DestroyDrawInfo(annotate_info);
852 return(status);
853}
854
855/*
856%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
857% %
858% %
859% %
860+ R e n d e r T y p e %
861% %
862% %
863% %
864%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
865%
866% RenderType() renders text on the image. It also returns the bounding box of
867% the text relative to the image.
868%
869% The format of the RenderType method is:
870%
871% MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000872% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000873%
874% A description of each parameter follows:
875%
876% o image: the image.
877%
878% o draw_info: the draw info.
879%
880% o offset: (x,y) location of text relative to image.
881%
882% o metrics: bounding box of text.
883%
cristy5cbc0162011-08-29 00:36:28 +0000884% o exception: return any errors or warnings in this structure.
885%
cristy3ed852e2009-09-05 21:47:34 +0000886*/
887static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000888 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000889{
890 const TypeInfo
891 *type_info;
892
893 DrawInfo
894 *annotate_info;
895
896 MagickBooleanType
897 status;
898
899 type_info=(const TypeInfo *) NULL;
900 if (draw_info->font != (char *) NULL)
901 {
902 if (*draw_info->font == '@')
903 {
904 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000905 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000906 return(status);
907 }
908 if (*draw_info->font == '-')
cristy5cbc0162011-08-29 00:36:28 +0000909 return(RenderX11(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +0000910 if (IsPathAccessible(draw_info->font) != MagickFalse)
911 {
912 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000913 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000914 return(status);
915 }
cristy5cbc0162011-08-29 00:36:28 +0000916 type_info=GetTypeInfo(draw_info->font,exception);
cristy3ed852e2009-09-05 21:47:34 +0000917 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000918 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000919 "UnableToReadFont","`%s'",draw_info->font);
cristy3ed852e2009-09-05 21:47:34 +0000920 }
921 if ((type_info == (const TypeInfo *) NULL) &&
922 (draw_info->family != (const char *) NULL))
923 {
924 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000925 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000926 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000927 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000928 "UnableToReadFont","`%s'",draw_info->family);
cristy3ed852e2009-09-05 21:47:34 +0000929 }
930 if (type_info == (const TypeInfo *) NULL)
cristyd984f272010-03-16 01:46:12 +0000931 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000932 draw_info->stretch,draw_info->weight,exception);
cristyd984f272010-03-16 01:46:12 +0000933 if (type_info == (const TypeInfo *) NULL)
cristycbb34a42010-03-16 01:45:18 +0000934 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000935 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000936 if (type_info == (const TypeInfo *) NULL)
cristy704acaa2010-03-23 13:08:21 +0000937 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000938 draw_info->stretch,draw_info->weight,exception);
cristy704acaa2010-03-23 13:08:21 +0000939 if (type_info == (const TypeInfo *) NULL)
cristy6193b862011-08-06 16:33:59 +0000940 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000941 draw_info->stretch,draw_info->weight,exception);
cristy6193b862011-08-06 16:33:59 +0000942 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000943 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000944 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000945 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000946 type_info=GetTypeInfo("*",exception);
cristy704acaa2010-03-23 13:08:21 +0000947 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000948 {
cristy5cbc0162011-08-29 00:36:28 +0000949 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
950 exception);
cristy3ed852e2009-09-05 21:47:34 +0000951 return(status);
952 }
953 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
954 annotate_info->face=type_info->face;
955 if (type_info->metrics != (char *) NULL)
956 (void) CloneString(&annotate_info->metrics,type_info->metrics);
957 if (type_info->glyphs != (char *) NULL)
958 (void) CloneString(&annotate_info->font,type_info->glyphs);
cristy5cbc0162011-08-29 00:36:28 +0000959 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
960 exception);
cristy3ed852e2009-09-05 21:47:34 +0000961 annotate_info=DestroyDrawInfo(annotate_info);
962 return(status);
963}
964
965/*
966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967% %
968% %
969% %
970+ R e n d e r F r e e t y p e %
971% %
972% %
973% %
974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975%
976% RenderFreetype() renders text on the image with a Truetype font. It also
977% returns the bounding box of the text relative to the image.
978%
979% The format of the RenderFreetype method is:
980%
981% MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000982% const char *encoding,const PointInfo *offset,TypeMetric *metrics,
983% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000984%
985% A description of each parameter follows:
986%
987% o image: the image.
988%
989% o draw_info: the draw info.
990%
991% o encoding: the font encoding.
992%
993% o offset: (x,y) location of text relative to image.
994%
995% o metrics: bounding box of text.
996%
cristy5cbc0162011-08-29 00:36:28 +0000997% o exception: return any errors or warnings in this structure.
998%
cristy3ed852e2009-09-05 21:47:34 +0000999*/
1000
1001#if defined(MAGICKCORE_FREETYPE_DELEGATE)
cristy2857ffc2010-03-27 23:44:00 +00001002
cristy3ed852e2009-09-05 21:47:34 +00001003static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1004 DrawInfo *draw_info)
1005{
1006 AffineMatrix
1007 affine;
1008
1009 char
cristy151b66d2015-04-15 10:50:31 +00001010 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001011
1012 affine=draw_info->affine;
cristy151b66d2015-04-15 10:50:31 +00001013 (void) FormatLocaleString(path,MagickPathExtent,
cristya8549b12011-05-18 19:05:08 +00001014 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
cristy8cd5b312010-01-07 01:10:24 +00001015 p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
1016 affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001017 (void) ConcatenateString(&draw_info->primitive,path);
1018 return(0);
1019}
1020
1021static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1022{
1023 AffineMatrix
1024 affine;
1025
1026 char
cristy151b66d2015-04-15 10:50:31 +00001027 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001028
1029 affine=draw_info->affine;
cristy151b66d2015-04-15 10:50:31 +00001030 (void) FormatLocaleString(path,MagickPathExtent,"L%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +00001031 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001032 (void) ConcatenateString(&draw_info->primitive,path);
1033 return(0);
1034}
1035
1036static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1037{
1038 AffineMatrix
1039 affine;
1040
1041 char
cristy151b66d2015-04-15 10:50:31 +00001042 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001043
1044 affine=draw_info->affine;
cristy151b66d2015-04-15 10:50:31 +00001045 (void) FormatLocaleString(path,MagickPathExtent,"M%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +00001046 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001047 (void) ConcatenateString(&draw_info->primitive,path);
1048 return(0);
1049}
1050
1051static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1052 DrawInfo *draw_info)
1053{
1054 AffineMatrix
1055 affine;
1056
1057 char
cristy151b66d2015-04-15 10:50:31 +00001058 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001059
1060 affine=draw_info->affine;
cristy151b66d2015-04-15 10:50:31 +00001061 (void) FormatLocaleString(path,MagickPathExtent,"Q%g,%g %g,%g",
cristy3ed852e2009-09-05 21:47:34 +00001062 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1063 affine.ty-to->y/64.0);
1064 (void) ConcatenateString(&draw_info->primitive,path);
1065 return(0);
1066}
1067
1068static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001069 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1070 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001071{
1072#if !defined(FT_OPEN_PATHNAME)
1073#define FT_OPEN_PATHNAME ft_open_pathname
1074#endif
1075
1076 typedef struct _GlyphInfo
1077 {
1078 FT_UInt
1079 id;
1080
1081 FT_Vector
1082 origin;
1083
1084 FT_Glyph
1085 image;
1086 } GlyphInfo;
1087
1088 const char
1089 *value;
1090
cristyc9b12952010-03-28 01:12:28 +00001091 double
1092 direction;
1093
cristy3ed852e2009-09-05 21:47:34 +00001094 DrawInfo
1095 *annotate_info;
1096
1097 FT_BBox
1098 bounds;
1099
1100 FT_BitmapGlyph
1101 bitmap;
1102
1103 FT_Encoding
1104 encoding_type;
1105
1106 FT_Error
cristyf8f295f2013-08-11 17:26:49 +00001107 ft_status;
cristy3ed852e2009-09-05 21:47:34 +00001108
1109 FT_Face
1110 face;
1111
1112 FT_Int32
1113 flags;
1114
1115 FT_Library
1116 library;
1117
1118 FT_Matrix
1119 affine;
1120
1121 FT_Open_Args
1122 args;
1123
1124 FT_Vector
1125 origin;
1126
1127 GlyphInfo
1128 glyph,
1129 last_glyph;
1130
cristyf8f295f2013-08-11 17:26:49 +00001131 MagickBooleanType
1132 status;
1133
cristy3ed852e2009-09-05 21:47:34 +00001134 PointInfo
1135 point,
1136 resolution;
1137
1138 register char
1139 *p;
1140
cristy9d314ff2011-03-09 01:30:28 +00001141 ssize_t
1142 code,
1143 y;
1144
cristy3ed852e2009-09-05 21:47:34 +00001145 static FT_Outline_Funcs
1146 OutlineMethods =
1147 {
1148 (FT_Outline_MoveTo_Func) TraceMoveTo,
1149 (FT_Outline_LineTo_Func) TraceLineTo,
1150 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1151 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1152 0, 0
1153 };
1154
cristy2857ffc2010-03-27 23:44:00 +00001155 unsigned char
1156 *utf8;
1157
cristy3ed852e2009-09-05 21:47:34 +00001158 /*
1159 Initialize Truetype library.
1160 */
cristyf8f295f2013-08-11 17:26:49 +00001161 ft_status=FT_Init_FreeType(&library);
1162 if (ft_status != 0)
cristy3ed852e2009-09-05 21:47:34 +00001163 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1164 image->filename);
1165 args.flags=FT_OPEN_PATHNAME;
1166 if (draw_info->font == (char *) NULL)
1167 args.pathname=ConstantString("helvetica");
1168 else
1169 if (*draw_info->font != '@')
1170 args.pathname=ConstantString(draw_info->font);
1171 else
1172 args.pathname=ConstantString(draw_info->font+1);
1173 face=(FT_Face) NULL;
cristyf8f295f2013-08-11 17:26:49 +00001174 ft_status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
cristy3ed852e2009-09-05 21:47:34 +00001175 args.pathname=DestroyString(args.pathname);
cristyf8f295f2013-08-11 17:26:49 +00001176 if (ft_status != 0)
cristy3ed852e2009-09-05 21:47:34 +00001177 {
1178 (void) FT_Done_FreeType(library);
cristy5cbc0162011-08-29 00:36:28 +00001179 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
cristyefe601c2013-01-05 17:51:12 +00001180 "UnableToReadFont","`%s'",draw_info->font);
cristy5cbc0162011-08-29 00:36:28 +00001181 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001182 }
1183 if ((draw_info->metrics != (char *) NULL) &&
1184 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1185 (void) FT_Attach_File(face,draw_info->metrics);
1186 encoding_type=ft_encoding_unicode;
cristyf8f295f2013-08-11 17:26:49 +00001187 ft_status=FT_Select_Charmap(face,encoding_type);
1188 if ((ft_status != 0) && (face->num_charmaps != 0))
1189 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
cristy3ed852e2009-09-05 21:47:34 +00001190 if (encoding != (const char *) NULL)
1191 {
1192 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1193 encoding_type=ft_encoding_adobe_custom;
1194 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1195 encoding_type=ft_encoding_adobe_expert;
1196 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1197 encoding_type=ft_encoding_adobe_standard;
1198 if (LocaleCompare(encoding,"AppleRoman") == 0)
1199 encoding_type=ft_encoding_apple_roman;
1200 if (LocaleCompare(encoding,"BIG5") == 0)
1201 encoding_type=ft_encoding_big5;
1202 if (LocaleCompare(encoding,"GB2312") == 0)
1203 encoding_type=ft_encoding_gb2312;
1204 if (LocaleCompare(encoding,"Johab") == 0)
1205 encoding_type=ft_encoding_johab;
1206#if defined(ft_encoding_latin_1)
1207 if (LocaleCompare(encoding,"Latin-1") == 0)
1208 encoding_type=ft_encoding_latin_1;
1209#endif
1210 if (LocaleCompare(encoding,"Latin-2") == 0)
1211 encoding_type=ft_encoding_latin_2;
1212 if (LocaleCompare(encoding,"None") == 0)
1213 encoding_type=ft_encoding_none;
1214 if (LocaleCompare(encoding,"SJIScode") == 0)
1215 encoding_type=ft_encoding_sjis;
1216 if (LocaleCompare(encoding,"Symbol") == 0)
1217 encoding_type=ft_encoding_symbol;
1218 if (LocaleCompare(encoding,"Unicode") == 0)
1219 encoding_type=ft_encoding_unicode;
1220 if (LocaleCompare(encoding,"Wansung") == 0)
1221 encoding_type=ft_encoding_wansung;
cristyf8f295f2013-08-11 17:26:49 +00001222 ft_status=FT_Select_Charmap(face,encoding_type);
1223 if (ft_status != 0)
cristye3b2bb82014-02-15 13:20:08 +00001224 {
1225 (void) FT_Done_Face(face);
1226 (void) FT_Done_FreeType(library);
1227 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1228 }
cristy3ed852e2009-09-05 21:47:34 +00001229 }
1230 /*
1231 Set text size.
1232 */
1233 resolution.x=DefaultResolution;
1234 resolution.y=DefaultResolution;
1235 if (draw_info->density != (char *) NULL)
1236 {
1237 GeometryInfo
1238 geometry_info;
1239
1240 MagickStatusType
1241 flags;
1242
1243 flags=ParseGeometry(draw_info->density,&geometry_info);
1244 resolution.x=geometry_info.rho;
1245 resolution.y=geometry_info.sigma;
1246 if ((flags & SigmaValue) == 0)
1247 resolution.y=resolution.x;
1248 }
cristyf8f295f2013-08-11 17:26:49 +00001249 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
cristy3ed852e2009-09-05 21:47:34 +00001250 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1251 (FT_UInt) resolution.y);
1252 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1253 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1254 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1255 metrics->descent=(double) face->size->metrics.descender/64.0;
1256 metrics->width=0;
1257 metrics->origin.x=0;
1258 metrics->origin.y=0;
1259 metrics->height=(double) face->size->metrics.height/64.0;
1260 metrics->max_advance=0.0;
1261 if (face->size->metrics.max_advance > MagickEpsilon)
1262 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1263 metrics->bounds.x1=0.0;
1264 metrics->bounds.y1=metrics->descent;
1265 metrics->bounds.x2=metrics->ascent+metrics->descent;
1266 metrics->bounds.y2=metrics->ascent+metrics->descent;
1267 metrics->underline_position=face->underline_position/64.0;
1268 metrics->underline_thickness=face->underline_thickness/64.0;
cristy71709bf2011-09-25 13:10:29 +00001269 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
cristy3ed852e2009-09-05 21:47:34 +00001270 {
1271 (void) FT_Done_Face(face);
1272 (void) FT_Done_FreeType(library);
1273 return(MagickTrue);
1274 }
1275 /*
1276 Compute bounding box.
1277 */
1278 if (image->debug != MagickFalse)
1279 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
cristye7f51092010-01-17 00:39:37 +00001280 "font-encoding %s; text-encoding %s; pointsize %g",
cristy3ed852e2009-09-05 21:47:34 +00001281 draw_info->font != (char *) NULL ? draw_info->font : "none",
1282 encoding != (char *) NULL ? encoding : "none",
1283 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1284 draw_info->pointsize);
1285 flags=FT_LOAD_NO_BITMAP;
cristy55a804e2012-01-13 17:46:11 +00001286 if (draw_info->text_antialias == MagickFalse)
cristy4f9709e2012-06-10 01:54:51 +00001287 flags|=FT_LOAD_TARGET_MONO;
cristy55a804e2012-01-13 17:46:11 +00001288 else
1289 {
cristya89189d2012-01-13 17:54:57 +00001290#if defined(FT_LOAD_TARGET_LIGHT)
cristy55a804e2012-01-13 17:46:11 +00001291 flags|=FT_LOAD_TARGET_LIGHT;
cristya89189d2012-01-13 17:54:57 +00001292#elif defined(FT_LOAD_TARGET_LCD)
1293 flags|=FT_LOAD_TARGET_LCD;
cristy55a804e2012-01-13 17:46:11 +00001294#endif
1295 }
cristyd15e6592011-10-15 00:13:06 +00001296 value=GetImageProperty(image,"type:hinting",exception);
cristy33204242010-10-07 23:17:12 +00001297 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
cristy3ed852e2009-09-05 21:47:34 +00001298 flags|=FT_LOAD_NO_HINTING;
1299 glyph.id=0;
1300 glyph.image=NULL;
1301 last_glyph.id=0;
1302 last_glyph.image=NULL;
1303 origin.x=0;
1304 origin.y=0;
1305 affine.xx=65536L;
1306 affine.yx=0L;
1307 affine.xy=0L;
1308 affine.yy=65536L;
1309 if (draw_info->render != MagickFalse)
1310 {
1311 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1312 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1313 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1314 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1315 }
1316 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1317 (void) CloneString(&annotate_info->primitive,"path '");
1318 if (draw_info->render != MagickFalse)
1319 {
1320 if (image->storage_class != DirectClass)
cristy5cbc0162011-08-29 00:36:28 +00001321 (void) SetImageStorageClass(image,DirectClass,exception);
cristy17f11b02014-12-20 19:37:04 +00001322 if (image->alpha_trait == UndefinedPixelTrait)
cristya64d8002014-11-16 12:12:12 +00001323 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001324 }
cristyc9b12952010-03-28 01:12:28 +00001325 direction=1.0;
cristycf5467b2010-03-28 16:22:06 +00001326 if (draw_info->direction == RightToLeftDirection)
cristyc9b12952010-03-28 01:12:28 +00001327 direction=(-1.0);
cristy3ed852e2009-09-05 21:47:34 +00001328 point.x=0.0;
1329 point.y=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001330 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
cristybd512462010-02-13 02:13:14 +00001331 if (GetUTFCode(p) < 0)
1332 break;
cristy2857ffc2010-03-27 23:44:00 +00001333 utf8=(unsigned char *) NULL;
cristyd913ea12010-02-13 02:32:57 +00001334 if (GetUTFCode(p) == 0)
cristy2857ffc2010-03-27 23:44:00 +00001335 p=draw_info->text;
cristybd512462010-02-13 02:13:14 +00001336 else
cristybd512462010-02-13 02:13:14 +00001337 {
cristy2857ffc2010-03-27 23:44:00 +00001338 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1339 if (utf8 != (unsigned char *) NULL)
cristyc9b12952010-03-28 01:12:28 +00001340 p=(char *) utf8;
cristy2857ffc2010-03-27 23:44:00 +00001341 }
cristyf8f295f2013-08-11 17:26:49 +00001342 status=MagickTrue;
cristy2857ffc2010-03-27 23:44:00 +00001343 for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1344 {
1345 /*
1346 Render UTF-8 sequence.
1347 */
1348 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1349 if (glyph.id == 0)
1350 glyph.id=FT_Get_Char_Index(face,'?');
1351 if ((glyph.id != 0) && (last_glyph.id != 0))
1352 {
cristy91469932014-07-25 11:00:25 +00001353 if (FT_HAS_KERNING(face))
1354 {
1355 FT_Vector
1356 kerning;
cristybd512462010-02-13 02:13:14 +00001357
cristy91469932014-07-25 11:00:25 +00001358 ft_status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
1359 ft_kerning_default,&kerning);
1360 if (ft_status == 0)
1361 origin.x+=(FT_Pos) (direction*kerning.x);
1362 }
1363 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
1364 }
cristy2857ffc2010-03-27 23:44:00 +00001365 glyph.origin=origin;
cristyf8f295f2013-08-11 17:26:49 +00001366 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1367 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001368 continue;
cristyf8f295f2013-08-11 17:26:49 +00001369 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1370 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001371 continue;
cristyf8f295f2013-08-11 17:26:49 +00001372 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
cristy2857ffc2010-03-27 23:44:00 +00001373 &bounds);
cristyf8f295f2013-08-11 17:26:49 +00001374 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001375 continue;
1376 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
cristyaa83c2c2011-09-21 13:36:25 +00001377 metrics->bounds.x1=(double) bounds.xMin;
cristy2857ffc2010-03-27 23:44:00 +00001378 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
cristyaa83c2c2011-09-21 13:36:25 +00001379 metrics->bounds.y1=(double) bounds.yMin;
cristy2857ffc2010-03-27 23:44:00 +00001380 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
cristyaa83c2c2011-09-21 13:36:25 +00001381 metrics->bounds.x2=(double) bounds.xMax;
cristy2857ffc2010-03-27 23:44:00 +00001382 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
cristyaa83c2c2011-09-21 13:36:25 +00001383 metrics->bounds.y2=(double) bounds.yMax;
cristyf8f295f2013-08-11 17:26:49 +00001384 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1385 (draw_info->stroke_pattern != (Image *) NULL))
1386 {
1387 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
1388 {
1389 /*
1390 Trace the glyph.
1391 */
1392 annotate_info->affine.tx=glyph.origin.x/64.0;
1393 annotate_info->affine.ty=glyph.origin.y/64.0;
1394 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1395 outline,&OutlineMethods,annotate_info);
1396 }
cristy2857ffc2010-03-27 23:44:00 +00001397 }
1398 FT_Vector_Transform(&glyph.origin,&affine);
1399 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
cristyf8f295f2013-08-11 17:26:49 +00001400 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
cristy2857ffc2010-03-27 23:44:00 +00001401 (FT_Vector *) NULL,MagickTrue);
cristyf8f295f2013-08-11 17:26:49 +00001402 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001403 continue;
1404 bitmap=(FT_BitmapGlyph) glyph.image;
1405 point.x=offset->x+bitmap->left;
cristy2f5b5c72011-09-16 16:52:00 +00001406 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1407 point.x=offset->x+(origin.x >> 6);
cristy2857ffc2010-03-27 23:44:00 +00001408 point.y=offset->y-bitmap->top;
1409 if (draw_info->render != MagickFalse)
1410 {
1411 CacheView
1412 *image_view;
cristybd512462010-02-13 02:13:14 +00001413
cristy2f5b5c72011-09-16 16:52:00 +00001414 register unsigned char
1415 *p;
1416
cristy2857ffc2010-03-27 23:44:00 +00001417 /*
1418 Rasterize the glyph.
1419 */
cristy46ff2672012-12-14 15:32:26 +00001420 image_view=AcquireAuthenticCacheView(image,exception);
cristy2f5b5c72011-09-16 16:52:00 +00001421 p=bitmap->bitmap.buffer;
cristybb503372010-05-27 20:51:26 +00001422 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
cristy2857ffc2010-03-27 23:44:00 +00001423 {
cristybd512462010-02-13 02:13:14 +00001424 MagickBooleanType
cristy2857ffc2010-03-27 23:44:00 +00001425 active,
1426 sync;
cristybd512462010-02-13 02:13:14 +00001427
cristya19f1d72012-08-07 18:24:38 +00001428 double
cristy2857ffc2010-03-27 23:44:00 +00001429 fill_opacity;
cristybd512462010-02-13 02:13:14 +00001430
cristy101ab702011-10-13 13:06:32 +00001431 PixelInfo
cristy2857ffc2010-03-27 23:44:00 +00001432 fill_color;
cristybd512462010-02-13 02:13:14 +00001433
cristy4c08aed2011-07-01 19:47:50 +00001434 register Quantum
cristy2857ffc2010-03-27 23:44:00 +00001435 *restrict q;
cristybd512462010-02-13 02:13:14 +00001436
cristy88158062011-06-09 00:31:43 +00001437 register ssize_t
1438 x;
1439
cristy73ce31b2010-06-13 23:17:39 +00001440 ssize_t
cristy2f5b5c72011-09-16 16:52:00 +00001441 n,
cristy73ce31b2010-06-13 23:17:39 +00001442 x_offset,
1443 y_offset;
1444
cristy2857ffc2010-03-27 23:44:00 +00001445 if (status == MagickFalse)
1446 continue;
cristybb503372010-05-27 20:51:26 +00001447 x_offset=(ssize_t) ceil(point.x-0.5);
1448 y_offset=(ssize_t) ceil(point.y+y-0.5);
1449 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy2857ffc2010-03-27 23:44:00 +00001450 continue;
cristy4c08aed2011-07-01 19:47:50 +00001451 q=(Quantum *) NULL;
cristybb503372010-05-27 20:51:26 +00001452 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001453 active=MagickFalse;
1454 else
cristybd512462010-02-13 02:13:14 +00001455 {
cristy2857ffc2010-03-27 23:44:00 +00001456 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1457 bitmap->bitmap.width,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001458 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
cristybd512462010-02-13 02:13:14 +00001459 }
cristy854209a2011-09-19 17:02:57 +00001460 n=y*bitmap->bitmap.pitch-1;
1461 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
cristy2857ffc2010-03-27 23:44:00 +00001462 {
cristy854209a2011-09-19 17:02:57 +00001463 n++;
cristy2857ffc2010-03-27 23:44:00 +00001464 x_offset++;
cristy2f5b5c72011-09-16 16:52:00 +00001465 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001466 {
cristydcf8f732015-01-24 20:54:48 +00001467 if (q != (Quantum *) NULL)
1468 q+=GetPixelChannels(image);
cristy2857ffc2010-03-27 23:44:00 +00001469 continue;
1470 }
cristy2f5b5c72011-09-16 16:52:00 +00001471 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
cristya19f1d72012-08-07 18:24:38 +00001472 fill_opacity=(double) (p[n])/(bitmap->bitmap.num_grays-1);
cristy2f5b5c72011-09-16 16:52:00 +00001473 else
1474 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1475 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
cristy2857ffc2010-03-27 23:44:00 +00001476 if (draw_info->text_antialias == MagickFalse)
1477 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1478 if (active == MagickFalse)
1479 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1480 exception);
cristy6193b862011-08-06 16:33:59 +00001481 if (q == (Quantum *) NULL)
cristy9c5171c2014-05-25 21:26:39 +00001482 continue;
cristyec061ec2011-10-21 17:41:17 +00001483 GetPixelInfo(image,&fill_color);
cristy2ed42f62011-10-02 19:49:57 +00001484 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1485 exception);
cristy4c08aed2011-07-01 19:47:50 +00001486 fill_opacity=fill_opacity*fill_color.alpha;
1487 CompositePixelOver(image,&fill_color,fill_opacity,q,
1488 GetPixelAlpha(image,q),q);
cristy2857ffc2010-03-27 23:44:00 +00001489 if (active == MagickFalse)
1490 {
1491 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1492 if (sync == MagickFalse)
1493 status=MagickFalse;
1494 }
cristyed231572011-07-14 02:18:59 +00001495 q+=GetPixelChannels(image);
cristybd512462010-02-13 02:13:14 +00001496 }
cristy2857ffc2010-03-27 23:44:00 +00001497 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1498 if (sync == MagickFalse)
1499 status=MagickFalse;
cristybd512462010-02-13 02:13:14 +00001500 }
cristy2857ffc2010-03-27 23:44:00 +00001501 image_view=DestroyCacheView(image_view);
1502 }
1503 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1504 metrics->width=bitmap->left+bitmap->bitmap.width;
cristyaa83c2c2011-09-21 13:36:25 +00001505 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
cristy2857ffc2010-03-27 23:44:00 +00001506 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1507 (IsUTFSpace(code) == MagickFalse))
cristy94b11832011-09-08 19:46:03 +00001508 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
cristy2857ffc2010-03-27 23:44:00 +00001509 else
cristy94b11832011-09-08 19:46:03 +00001510 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
cristyaa83c2c2011-09-21 13:36:25 +00001511 metrics->origin.x=(double) origin.x;
1512 metrics->origin.y=(double) origin.y;
cristybd5a96c2011-08-21 00:04:26 +00001513 if (last_glyph.id != 0)
1514 FT_Done_Glyph(last_glyph.image);
cristy2857ffc2010-03-27 23:44:00 +00001515 last_glyph=glyph;
1516 code=GetUTFCode(p);
1517 }
1518 if (utf8 != (unsigned char *) NULL)
1519 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
cristybd5a96c2011-08-21 00:04:26 +00001520 if (last_glyph.id != 0)
1521 FT_Done_Glyph(last_glyph.image);
cristy4c08aed2011-07-01 19:47:50 +00001522 if ((draw_info->stroke.alpha != TransparentAlpha) ||
cristy3ed852e2009-09-05 21:47:34 +00001523 (draw_info->stroke_pattern != (Image *) NULL))
1524 {
cristyf8f295f2013-08-11 17:26:49 +00001525 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001526 {
1527 /*
1528 Draw text stroke.
1529 */
1530 annotate_info->linejoin=RoundJoin;
1531 annotate_info->affine.tx=offset->x;
1532 annotate_info->affine.ty=offset->y;
1533 (void) ConcatenateString(&annotate_info->primitive,"'");
cristy018f07f2011-09-04 21:15:19 +00001534 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001535 }
1536 }
1537 /*
1538 Determine font metrics.
1539 */
1540 glyph.id=FT_Get_Char_Index(face,'_');
1541 glyph.origin=origin;
cristyf8f295f2013-08-11 17:26:49 +00001542 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1543 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001544 {
cristyf8f295f2013-08-11 17:26:49 +00001545 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1546 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001547 {
cristyf8f295f2013-08-11 17:26:49 +00001548 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1549 outline,&bounds);
1550 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001551 {
1552 FT_Vector_Transform(&glyph.origin,&affine);
1553 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
cristyf8f295f2013-08-11 17:26:49 +00001554 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
cristy3ed852e2009-09-05 21:47:34 +00001555 (FT_Vector *) NULL,MagickTrue);
1556 bitmap=(FT_BitmapGlyph) glyph.image;
1557 if (bitmap->left > metrics->width)
1558 metrics->width=bitmap->left;
1559 }
1560 }
cristye2c81ad2011-08-20 22:54:14 +00001561 FT_Done_Glyph(glyph.image);
cristy3ed852e2009-09-05 21:47:34 +00001562 }
1563 metrics->width-=metrics->bounds.x1/64.0;
cristy55cb9f12012-08-23 09:02:28 +00001564 metrics->width+=annotate_info->stroke_width;
cristy3ed852e2009-09-05 21:47:34 +00001565 metrics->bounds.x1/=64.0;
1566 metrics->bounds.y1/=64.0;
1567 metrics->bounds.x2/=64.0;
1568 metrics->bounds.y2/=64.0;
1569 metrics->origin.x/=64.0;
1570 metrics->origin.y/=64.0;
1571 /*
1572 Relinquish resources.
1573 */
1574 annotate_info=DestroyDrawInfo(annotate_info);
1575 (void) FT_Done_Face(face);
1576 (void) FT_Done_FreeType(library);
cristyf8f295f2013-08-11 17:26:49 +00001577 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001578}
1579#else
1580static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1581 const char *magick_unused(encoding),const PointInfo *offset,
cristy5cbc0162011-08-29 00:36:28 +00001582 TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001583{
cristy5cbc0162011-08-29 00:36:28 +00001584 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001585 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
cristyee081822011-02-02 19:07:30 +00001586 draw_info->font != (char *) NULL ? draw_info->font : "none");
cristye3b062a2011-12-17 18:34:52 +00001587 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001588}
1589#endif
1590
1591/*
1592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1593% %
1594% %
1595% %
1596+ R e n d e r P o s t s c r i p t %
1597% %
1598% %
1599% %
1600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1601%
1602% RenderPostscript() renders text on the image with a Postscript font. It
1603% also returns the bounding box of the text relative to the image.
1604%
1605% The format of the RenderPostscript method is:
1606%
1607% MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001608% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001609%
1610% A description of each parameter follows:
1611%
1612% o image: the image.
1613%
1614% o draw_info: the draw info.
1615%
1616% o offset: (x,y) location of text relative to image.
1617%
1618% o metrics: bounding box of text.
1619%
cristy5cbc0162011-08-29 00:36:28 +00001620% o exception: return any errors or warnings in this structure.
1621%
cristy3ed852e2009-09-05 21:47:34 +00001622*/
1623
cristy3ed852e2009-09-05 21:47:34 +00001624static char *EscapeParenthesis(const char *text)
1625{
1626 char
1627 *buffer;
1628
1629 register char
1630 *p;
1631
cristybb503372010-05-27 20:51:26 +00001632 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001633 i;
1634
1635 size_t
1636 escapes;
1637
1638 escapes=0;
1639 buffer=AcquireString(text);
1640 p=buffer;
cristy151b66d2015-04-15 10:50:31 +00001641 for (i=0; i < (ssize_t) MagickMin(strlen(text),MagickPathExtent-escapes-1); i++)
cristy3ed852e2009-09-05 21:47:34 +00001642 {
1643 if ((text[i] == '(') || (text[i] == ')'))
1644 {
1645 *p++='\\';
1646 escapes++;
1647 }
1648 *p++=text[i];
1649 }
1650 *p='\0';
1651 return(buffer);
1652}
1653
1654static MagickBooleanType RenderPostscript(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00001655 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1656 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001657{
1658 char
cristy151b66d2015-04-15 10:50:31 +00001659 filename[MagickPathExtent],
1660 geometry[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +00001661 *text;
1662
1663 FILE
1664 *file;
1665
1666 Image
1667 *annotate_image;
1668
1669 ImageInfo
1670 *annotate_info;
1671
1672 int
1673 unique_file;
1674
cristy3ed852e2009-09-05 21:47:34 +00001675 MagickBooleanType
1676 identity;
1677
1678 PointInfo
1679 extent,
1680 point,
1681 resolution;
1682
cristybb503372010-05-27 20:51:26 +00001683 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001684 i;
1685
cristy14d71292012-05-20 16:48:13 +00001686 size_t
1687 length;
1688
cristy9d314ff2011-03-09 01:30:28 +00001689 ssize_t
1690 y;
1691
cristy3ed852e2009-09-05 21:47:34 +00001692 /*
1693 Render label with a Postscript font.
1694 */
1695 if (image->debug != MagickFalse)
1696 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +00001697 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
cristy3ed852e2009-09-05 21:47:34 +00001698 draw_info->font : "none",draw_info->pointsize);
1699 file=(FILE *) NULL;
1700 unique_file=AcquireUniqueFileResource(filename);
1701 if (unique_file != -1)
1702 file=fdopen(unique_file,"wb");
1703 if ((unique_file == -1) || (file == (FILE *) NULL))
1704 {
cristy5cbc0162011-08-29 00:36:28 +00001705 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
cristy3ed852e2009-09-05 21:47:34 +00001706 return(MagickFalse);
1707 }
cristyb51dff52011-05-19 16:55:47 +00001708 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1709 (void) FormatLocaleFile(file,"/ReencodeType\n");
1710 (void) FormatLocaleFile(file,"{\n");
1711 (void) FormatLocaleFile(file," findfont dup length\n");
1712 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001713 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
cristyb51dff52011-05-19 16:55:47 +00001714 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001715 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
cristyb51dff52011-05-19 16:55:47 +00001716 (void) FormatLocaleFile(file,"} bind def\n");
cristy3ed852e2009-09-05 21:47:34 +00001717 /*
1718 Sample to compute bounding box.
1719 */
cristyaa83c2c2011-09-21 13:36:25 +00001720 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1721 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1722 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001723 extent.x=0.0;
1724 extent.y=0.0;
cristy14d71292012-05-20 16:48:13 +00001725 length=strlen(draw_info->text);
1726 for (i=0; i <= (ssize_t) (length+2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001727 {
1728 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1729 draw_info->affine.ry*2.0*draw_info->pointsize);
1730 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1731 draw_info->affine.sy*2.0*draw_info->pointsize);
1732 if (point.x > extent.x)
1733 extent.x=point.x;
1734 if (point.y > extent.y)
1735 extent.y=point.y;
1736 }
cristyb51dff52011-05-19 16:55:47 +00001737 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
cristy3ed852e2009-09-05 21:47:34 +00001738 extent.x/2.0,extent.y/2.0);
cristyb51dff52011-05-19 16:55:47 +00001739 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
cristy3ed852e2009-09-05 21:47:34 +00001740 draw_info->pointsize);
1741 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1742 (strchr(draw_info->font,'/') != (char *) NULL))
cristyb51dff52011-05-19 16:55:47 +00001743 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001744 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1745 else
cristy1e604812011-05-19 18:07:50 +00001746 (void) FormatLocaleFile(file,
1747 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1748 draw_info->font);
cristyb51dff52011-05-19 16:55:47 +00001749 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
cristy8cd5b312010-01-07 01:10:24 +00001750 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1751 draw_info->affine.sy);
cristy3ed852e2009-09-05 21:47:34 +00001752 text=EscapeParenthesis(draw_info->text);
1753 if (identity == MagickFalse)
cristy1e604812011-05-19 18:07:50 +00001754 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1755 text);
cristyb51dff52011-05-19 16:55:47 +00001756 (void) FormatLocaleFile(file,"(%s) show\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001757 text=DestroyString(text);
cristyb51dff52011-05-19 16:55:47 +00001758 (void) FormatLocaleFile(file,"showpage\n");
cristy3ed852e2009-09-05 21:47:34 +00001759 (void) fclose(file);
cristy151b66d2015-04-15 10:50:31 +00001760 (void) FormatLocaleString(geometry,MagickPathExtent,"%.20gx%.20g+0+0!",
cristye8c25f92010-06-03 00:53:06 +00001761 floor(extent.x+0.5),floor(extent.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001762 annotate_info=AcquireImageInfo();
cristy151b66d2015-04-15 10:50:31 +00001763 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,"ps:%s",
cristy3ed852e2009-09-05 21:47:34 +00001764 filename);
1765 (void) CloneString(&annotate_info->page,geometry);
1766 if (draw_info->density != (char *) NULL)
1767 (void) CloneString(&annotate_info->density,draw_info->density);
1768 annotate_info->antialias=draw_info->text_antialias;
cristy5cbc0162011-08-29 00:36:28 +00001769 annotate_image=ReadImage(annotate_info,exception);
1770 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00001771 annotate_info=DestroyImageInfo(annotate_info);
1772 (void) RelinquishUniqueFileResource(filename);
1773 if (annotate_image == (Image *) NULL)
1774 return(MagickFalse);
cristy55cb9f12012-08-23 09:02:28 +00001775 (void) NegateImage(annotate_image,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001776 resolution.x=DefaultResolution;
1777 resolution.y=DefaultResolution;
1778 if (draw_info->density != (char *) NULL)
1779 {
1780 GeometryInfo
1781 geometry_info;
1782
1783 MagickStatusType
1784 flags;
1785
1786 flags=ParseGeometry(draw_info->density,&geometry_info);
1787 resolution.x=geometry_info.rho;
1788 resolution.y=geometry_info.sigma;
1789 if ((flags & SigmaValue) == 0)
1790 resolution.y=resolution.x;
1791 }
1792 if (identity == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001793 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001794 else
1795 {
1796 RectangleInfo
1797 crop_info;
1798
cristy5cbc0162011-08-29 00:36:28 +00001799 crop_info=GetImageBoundingBox(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001800 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
cristy3ed852e2009-09-05 21:47:34 +00001801 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
cristybb503372010-05-27 20:51:26 +00001802 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
cristy06609ee2010-03-17 20:21:27 +00001803 0.5);
cristy151b66d2015-04-15 10:50:31 +00001804 (void) FormatLocaleString(geometry,MagickPathExtent,
cristy6d8abba2010-06-03 01:10:47 +00001805 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
cristye8c25f92010-06-03 00:53:06 +00001806 crop_info.height,(double) crop_info.x,(double) crop_info.y);
cristye941a752011-10-15 01:52:48 +00001807 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001808 }
1809 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1810 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1811 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1812 metrics->ascent=metrics->pixels_per_em.x;
1813 metrics->descent=metrics->pixels_per_em.y/-5.0;
1814 metrics->width=(double) annotate_image->columns/
1815 ExpandAffine(&draw_info->affine);
1816 metrics->height=1.152*metrics->pixels_per_em.x;
1817 metrics->max_advance=metrics->pixels_per_em.x;
1818 metrics->bounds.x1=0.0;
1819 metrics->bounds.y1=metrics->descent;
1820 metrics->bounds.x2=metrics->ascent+metrics->descent;
1821 metrics->bounds.y2=metrics->ascent+metrics->descent;
1822 metrics->underline_position=(-2.0);
1823 metrics->underline_thickness=1.0;
1824 if (draw_info->render == MagickFalse)
1825 {
1826 annotate_image=DestroyImage(annotate_image);
1827 return(MagickTrue);
1828 }
cristy4c08aed2011-07-01 19:47:50 +00001829 if (draw_info->fill.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001830 {
cristybd659482011-10-09 23:19:32 +00001831 CacheView
1832 *annotate_view;
1833
cristy3ed852e2009-09-05 21:47:34 +00001834 MagickBooleanType
1835 sync;
1836
cristy101ab702011-10-13 13:06:32 +00001837 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001838 fill_color;
1839
cristy3ed852e2009-09-05 21:47:34 +00001840 /*
1841 Render fill color.
1842 */
cristy17f11b02014-12-20 19:37:04 +00001843 if (image->alpha_trait == UndefinedPixelTrait)
cristy63240882011-08-05 19:05:27 +00001844 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy17f11b02014-12-20 19:37:04 +00001845 if (annotate_image->alpha_trait == UndefinedPixelTrait)
cristy63240882011-08-05 19:05:27 +00001846 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1847 exception);
1848 fill_color=draw_info->fill;
cristy46ff2672012-12-14 15:32:26 +00001849 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001850 for (y=0; y < (ssize_t) annotate_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001851 {
cristybb503372010-05-27 20:51:26 +00001852 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001853 x;
1854
cristy4c08aed2011-07-01 19:47:50 +00001855 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001856 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001857
1858 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1859 1,exception);
cristy6193b862011-08-06 16:33:59 +00001860 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001861 break;
cristybb503372010-05-27 20:51:26 +00001862 for (x=0; x < (ssize_t) annotate_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001863 {
cristy2ed42f62011-10-02 19:49:57 +00001864 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy55cb9f12012-08-23 09:02:28 +00001865 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1866 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
cristy4c08aed2011-07-01 19:47:50 +00001867 SetPixelRed(annotate_image,fill_color.red,q);
1868 SetPixelGreen(annotate_image,fill_color.green,q);
1869 SetPixelBlue(annotate_image,fill_color.blue,q);
cristyed231572011-07-14 02:18:59 +00001870 q+=GetPixelChannels(annotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001871 }
1872 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1873 if (sync == MagickFalse)
1874 break;
1875 }
1876 annotate_view=DestroyCacheView(annotate_view);
cristy39172402012-03-30 13:04:39 +00001877 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
cristybb503372010-05-27 20:51:26 +00001878 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
cristye941a752011-10-15 01:52:48 +00001879 metrics->descent)-0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00001880 }
1881 annotate_image=DestroyImage(annotate_image);
1882 return(MagickTrue);
1883}
1884
1885/*
1886%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1887% %
1888% %
1889% %
1890+ R e n d e r X 1 1 %
1891% %
1892% %
1893% %
1894%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1895%
1896% RenderX11() renders text on the image with an X11 font. It also returns the
1897% bounding box of the text relative to the image.
1898%
1899% The format of the RenderX11 method is:
1900%
1901% MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001902% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001903%
1904% A description of each parameter follows:
1905%
1906% o image: the image.
1907%
1908% o draw_info: the draw info.
1909%
1910% o offset: (x,y) location of text relative to image.
1911%
1912% o metrics: bounding box of text.
1913%
cristy5cbc0162011-08-29 00:36:28 +00001914% o exception: return any errors or warnings in this structure.
1915%
cristy3ed852e2009-09-05 21:47:34 +00001916*/
cristycc168c22013-04-18 14:42:31 +00001917static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1918 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001919{
1920 MagickBooleanType
1921 status;
1922
cristyd7ecaca2011-01-24 14:14:53 +00001923 if (annotate_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +00001924 ActivateSemaphoreInfo(&annotate_semaphore);
cristyd7ecaca2011-01-24 14:14:53 +00001925 LockSemaphoreInfo(annotate_semaphore);
cristy534d0a32013-04-13 16:27:24 +00001926 status=XRenderImage(image,draw_info,offset,metrics,exception);
cristyd7ecaca2011-01-24 14:14:53 +00001927 UnlockSemaphoreInfo(annotate_semaphore);
cristy534d0a32013-04-13 16:27:24 +00001928 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001929}