blob: 445ba88a9699c3200ebbb89ec236c6ec837b9da1 [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);
cristye1c94d92015-06-28 12:16:33 +0000292 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000293 if (image->debug != MagickFalse)
294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
295 assert(draw_info != (DrawInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000296 assert(draw_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000297 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-
Cristy70aa59b2015-10-05 06:30:10 -0400400 (number_lines-1.0)*height)/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,
cristydcc02b02015-06-08 23:08:35 +0000493 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(double) height);
cristy3ed852e2009-09-05 21:47:34 +0000494 (void) CloneString(&undercolor_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000495 (void) DrawImage(image,undercolor_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000496 (void) DestroyDrawInfo(undercolor_info);
497 }
498 annotate_info->affine.tx=offset.x;
499 annotate_info->affine.ty=offset.y;
cristy151b66d2015-04-15 10:50:31 +0000500 (void) FormatLocaleString(primitive,MagickPathExtent,"stroke-width %g "
cristya8549b12011-05-18 19:05:08 +0000501 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
cristy3ed852e2009-09-05 21:47:34 +0000502 if (annotate->decorate == OverlineDecoration)
503 {
504 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
505 metrics.descent-metrics.underline_position));
506 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000507 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000508 }
509 else
510 if (annotate->decorate == UnderlineDecoration)
511 {
512 annotate_info->affine.ty-=(draw_info->affine.sy*
513 metrics.underline_position);
514 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000515 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000516 }
517 /*
518 Annotate image with text.
519 */
cristy5cbc0162011-08-29 00:36:28 +0000520 status=RenderType(image,annotate,&offset,&metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000521 if (status == MagickFalse)
522 break;
523 if (annotate->decorate == LineThroughDecoration)
524 {
525 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
526 metrics.underline_position+metrics.descent)/2.0);
527 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000528 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000529 }
530 }
531 /*
532 Relinquish resources.
533 */
534 annotate_info=DestroyDrawInfo(annotate_info);
535 annotate=DestroyDrawInfo(annotate);
536 for (i=0; textlist[i] != (char *) NULL; i++)
537 textlist[i]=DestroyString(textlist[i]);
538 textlist=(char **) RelinquishMagickMemory(textlist);
539 return(status);
540}
541
542/*
543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544% %
545% %
546% %
547% F o r m a t M a g i c k C a p t i o n %
548% %
549% %
550% %
551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552%
553% FormatMagickCaption() formats a caption so that it fits within the image
554% width. It returns the number of lines in the formatted caption.
555%
556% The format of the FormatMagickCaption method is:
557%
cristybb503372010-05-27 20:51:26 +0000558% ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000559% const MagickBooleanType split,TypeMetric *metrics,char **caption,
560% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000561%
562% A description of each parameter follows.
563%
564% o image: The image.
565%
cristy3ed852e2009-09-05 21:47:34 +0000566% o draw_info: the draw info.
567%
cristy6b1d05e2010-09-22 19:17:27 +0000568% o split: when no convenient line breaks-- insert newline.
569%
cristy3ed852e2009-09-05 21:47:34 +0000570% o metrics: Return the font metrics in this structure.
571%
cristy6b1d05e2010-09-22 19:17:27 +0000572% o caption: the caption.
573%
cristy5cbc0162011-08-29 00:36:28 +0000574% o exception: return any errors or warnings in this structure.
575%
cristy3ed852e2009-09-05 21:47:34 +0000576*/
cristybb503372010-05-27 20:51:26 +0000577MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000578 const MagickBooleanType split,TypeMetric *metrics,char **caption,
579 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000580{
cristy74966db2011-09-20 19:37:21 +0000581 char
582 *text;
583
cristy3ed852e2009-09-05 21:47:34 +0000584 MagickBooleanType
585 status;
586
587 register char
588 *p,
589 *q,
590 *s;
591
cristybb503372010-05-27 20:51:26 +0000592 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000593 i;
594
cristybb503372010-05-27 20:51:26 +0000595 size_t
cristy3ed852e2009-09-05 21:47:34 +0000596 width;
597
cristy6193b862011-08-06 16:33:59 +0000598 ssize_t
599 n;
600
cristy74966db2011-09-20 19:37:21 +0000601 text=AcquireString(draw_info->text);
cristy3ed852e2009-09-05 21:47:34 +0000602 q=draw_info->text;
603 s=(char *) NULL;
604 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
605 {
606 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
607 s=p;
cristy2911f2d2011-08-20 23:04:43 +0000608 if (GetUTFCode(p) == '\n')
cristye2c81ad2011-08-20 22:54:14 +0000609 q=draw_info->text;
cristybb503372010-05-27 20:51:26 +0000610 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
cristy3ed852e2009-09-05 21:47:34 +0000611 *q++=(*(p+i));
612 *q='\0';
cristy5cbc0162011-08-29 00:36:28 +0000613 status=GetTypeMetrics(image,draw_info,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000614 if (status == MagickFalse)
615 break;
cristy78fed5e2013-06-02 17:24:16 +0000616 width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
cristy74966db2011-09-20 19:37:21 +0000617 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
cristy6193b862011-08-06 16:33:59 +0000618 continue;
cristy74966db2011-09-20 19:37:21 +0000619 (void) strcpy(text,draw_info->text);
cristy4aa314e2011-08-19 13:59:45 +0000620 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
cristy3ed852e2009-09-05 21:47:34 +0000621 {
622 *s='\n';
623 p=s;
624 }
625 else
cristy4aa314e2011-08-19 13:59:45 +0000626 if ((s != (char *) NULL) || (split != MagickFalse))
cristy6b1d05e2010-09-22 19:17:27 +0000627 {
628 char
629 *target;
cristy3ed852e2009-09-05 21:47:34 +0000630
cristy6b1d05e2010-09-22 19:17:27 +0000631 /*
632 No convenient line breaks-- insert newline.
633 */
634 target=AcquireString(*caption);
635 n=p-(*caption);
636 CopyMagickString(target,*caption,n+1);
637 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
638 ConcatenateMagickString(target,p,strlen(*caption)+2);
639 (void) DestroyString(*caption);
640 *caption=target;
641 p=(*caption)+n;
642 }
cristy3ed852e2009-09-05 21:47:34 +0000643 q=draw_info->text;
cristy6193b862011-08-06 16:33:59 +0000644 s=(char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000645 }
cristy74966db2011-09-20 19:37:21 +0000646 text=DestroyString(text);
cristy6193b862011-08-06 16:33:59 +0000647 n=0;
cristy3ed852e2009-09-05 21:47:34 +0000648 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
649 if (GetUTFCode(p) == '\n')
cristy6193b862011-08-06 16:33:59 +0000650 n++;
651 return(n);
cristy3ed852e2009-09-05 21:47:34 +0000652}
653
654/*
655%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
656% %
657% %
658% %
659% G e t M u l t i l i n e T y p e M e t r i c s %
660% %
661% %
662% %
663%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664%
665% GetMultilineTypeMetrics() returns the following information for the
666% specified font and text:
667%
668% character width
669% character height
670% ascender
671% descender
672% text width
673% text height
674% maximum horizontal advance
675% bounds: x1
676% bounds: y1
677% bounds: x2
678% bounds: y2
679% origin: x
680% origin: y
681% underline position
682% underline thickness
683%
684% This method is like GetTypeMetrics() but it returns the maximum text width
685% and height for multiple lines of text.
686%
687% The format of the GetMultilineTypeMetrics method is:
688%
689% MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000690% const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000691%
692% A description of each parameter follows:
693%
694% o image: the image.
695%
696% o draw_info: the draw info.
697%
698% o metrics: Return the font metrics in this structure.
699%
cristy5cbc0162011-08-29 00:36:28 +0000700% o exception: return any errors or warnings in this structure.
701%
cristy3ed852e2009-09-05 21:47:34 +0000702*/
703MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000704 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000705{
706 char
707 **textlist;
708
709 DrawInfo
710 *annotate_info;
711
712 MagickBooleanType
713 status;
714
cristybb503372010-05-27 20:51:26 +0000715 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000716 i;
717
718 TypeMetric
719 extent;
720
cristy3ed852e2009-09-05 21:47:34 +0000721 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000722 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000723 if (image->debug != MagickFalse)
724 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
725 assert(draw_info != (DrawInfo *) NULL);
726 assert(draw_info->text != (char *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000727 assert(draw_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000728 if (*draw_info->text == '\0')
729 return(MagickFalse);
730 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
731 annotate_info->text=DestroyString(annotate_info->text);
732 /*
733 Convert newlines to multiple lines of text.
734 */
735 textlist=StringToList(draw_info->text);
736 if (textlist == (char **) NULL)
737 return(MagickFalse);
738 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000739 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000740 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
741 (void) ResetMagickMemory(&extent,0,sizeof(extent));
742 /*
743 Find the widest of the text lines.
744 */
745 annotate_info->text=textlist[0];
cristy5cbc0162011-08-29 00:36:28 +0000746 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000747 *metrics=extent;
748 for (i=1; textlist[i] != (char *) NULL; i++)
749 {
750 annotate_info->text=textlist[i];
cristy5cbc0162011-08-29 00:36:28 +0000751 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000752 if (extent.width > metrics->width)
753 *metrics=extent;
754 }
cristye2c81ad2011-08-20 22:54:14 +0000755 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
756 (i-1)*draw_info->interline_spacing);
cristy3ed852e2009-09-05 21:47:34 +0000757 /*
758 Relinquish resources.
759 */
760 annotate_info->text=(char *) NULL;
761 annotate_info=DestroyDrawInfo(annotate_info);
762 for (i=0; textlist[i] != (char *) NULL; i++)
763 textlist[i]=DestroyString(textlist[i]);
764 textlist=(char **) RelinquishMagickMemory(textlist);
765 return(status);
766}
767
768/*
769%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770% %
771% %
772% %
773% G e t T y p e M e t r i c s %
774% %
775% %
776% %
777%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
778%
779% GetTypeMetrics() returns the following information for the specified font
780% and text:
781%
782% character width
783% character height
784% ascender
785% descender
786% text width
787% text height
788% maximum horizontal advance
789% bounds: x1
790% bounds: y1
791% bounds: x2
792% bounds: y2
793% origin: x
794% origin: y
795% underline position
796% underline thickness
797%
798% The format of the GetTypeMetrics method is:
799%
800% MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000801% TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000802%
803% A description of each parameter follows:
804%
805% o image: the image.
806%
807% o draw_info: the draw info.
808%
809% o metrics: Return the font metrics in this structure.
810%
cristy5cbc0162011-08-29 00:36:28 +0000811% o exception: return any errors or warnings in this structure.
812%
cristy3ed852e2009-09-05 21:47:34 +0000813*/
814MagickExport MagickBooleanType GetTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000815 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000816{
817 DrawInfo
818 *annotate_info;
819
820 MagickBooleanType
821 status;
822
823 PointInfo
824 offset;
825
826 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000827 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000828 if (image->debug != MagickFalse)
829 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
830 assert(draw_info != (DrawInfo *) NULL);
831 assert(draw_info->text != (char *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000832 assert(draw_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000833 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
834 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000835 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000836 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
837 offset.x=0.0;
838 offset.y=0.0;
cristy5cbc0162011-08-29 00:36:28 +0000839 status=RenderType(image,annotate_info,&offset,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000840 if (image->debug != MagickFalse)
841 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
cristye7f51092010-01-17 00:39:37 +0000842 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
cristya8549b12011-05-18 19:05:08 +0000843 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
cristye7f51092010-01-17 00:39:37 +0000844 "underline position: %g; underline thickness: %g",annotate_info->text,
cristy3ed852e2009-09-05 21:47:34 +0000845 metrics->width,metrics->height,metrics->ascent,metrics->descent,
846 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
cristy4c08aed2011-07-01 19:47:50 +0000847 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
848 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
cristy3ed852e2009-09-05 21:47:34 +0000849 metrics->underline_position,metrics->underline_thickness);
850 annotate_info=DestroyDrawInfo(annotate_info);
851 return(status);
852}
853
854/*
855%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856% %
857% %
858% %
859+ R e n d e r T y p e %
860% %
861% %
862% %
863%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864%
865% RenderType() renders text on the image. It also returns the bounding box of
866% the text relative to the image.
867%
868% The format of the RenderType method is:
869%
870% MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000871% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000872%
873% A description of each parameter follows:
874%
875% o image: the image.
876%
877% o draw_info: the draw info.
878%
879% o offset: (x,y) location of text relative to image.
880%
881% o metrics: bounding box of text.
882%
cristy5cbc0162011-08-29 00:36:28 +0000883% o exception: return any errors or warnings in this structure.
884%
cristy3ed852e2009-09-05 21:47:34 +0000885*/
886static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000887 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000888{
889 const TypeInfo
890 *type_info;
891
892 DrawInfo
893 *annotate_info;
894
895 MagickBooleanType
896 status;
897
898 type_info=(const TypeInfo *) NULL;
899 if (draw_info->font != (char *) NULL)
900 {
901 if (*draw_info->font == '@')
902 {
903 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000904 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000905 return(status);
906 }
907 if (*draw_info->font == '-')
cristy5cbc0162011-08-29 00:36:28 +0000908 return(RenderX11(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +0000909 if (IsPathAccessible(draw_info->font) != MagickFalse)
910 {
911 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000912 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000913 return(status);
914 }
cristy5cbc0162011-08-29 00:36:28 +0000915 type_info=GetTypeInfo(draw_info->font,exception);
cristy3ed852e2009-09-05 21:47:34 +0000916 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000917 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000918 "UnableToReadFont","`%s'",draw_info->font);
cristy3ed852e2009-09-05 21:47:34 +0000919 }
920 if ((type_info == (const TypeInfo *) NULL) &&
921 (draw_info->family != (const char *) NULL))
922 {
923 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000924 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000925 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000926 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000927 "UnableToReadFont","`%s'",draw_info->family);
cristy3ed852e2009-09-05 21:47:34 +0000928 }
929 if (type_info == (const TypeInfo *) NULL)
cristyd984f272010-03-16 01:46:12 +0000930 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000931 draw_info->stretch,draw_info->weight,exception);
cristyd984f272010-03-16 01:46:12 +0000932 if (type_info == (const TypeInfo *) NULL)
cristycbb34a42010-03-16 01:45:18 +0000933 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000934 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000935 if (type_info == (const TypeInfo *) NULL)
cristy704acaa2010-03-23 13:08:21 +0000936 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000937 draw_info->stretch,draw_info->weight,exception);
cristy704acaa2010-03-23 13:08:21 +0000938 if (type_info == (const TypeInfo *) NULL)
cristy6193b862011-08-06 16:33:59 +0000939 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000940 draw_info->stretch,draw_info->weight,exception);
cristy6193b862011-08-06 16:33:59 +0000941 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000942 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000943 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000944 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000945 type_info=GetTypeInfo("*",exception);
cristy704acaa2010-03-23 13:08:21 +0000946 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000947 {
cristy5cbc0162011-08-29 00:36:28 +0000948 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
949 exception);
cristy3ed852e2009-09-05 21:47:34 +0000950 return(status);
951 }
952 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
953 annotate_info->face=type_info->face;
954 if (type_info->metrics != (char *) NULL)
955 (void) CloneString(&annotate_info->metrics,type_info->metrics);
956 if (type_info->glyphs != (char *) NULL)
957 (void) CloneString(&annotate_info->font,type_info->glyphs);
cristy5cbc0162011-08-29 00:36:28 +0000958 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
959 exception);
cristy3ed852e2009-09-05 21:47:34 +0000960 annotate_info=DestroyDrawInfo(annotate_info);
961 return(status);
962}
963
964/*
965%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966% %
967% %
968% %
969+ R e n d e r F r e e t y p e %
970% %
971% %
972% %
973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974%
975% RenderFreetype() renders text on the image with a Truetype font. It also
976% returns the bounding box of the text relative to the image.
977%
978% The format of the RenderFreetype method is:
979%
980% MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000981% const char *encoding,const PointInfo *offset,TypeMetric *metrics,
982% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000983%
984% A description of each parameter follows:
985%
986% o image: the image.
987%
988% o draw_info: the draw info.
989%
990% o encoding: the font encoding.
991%
992% o offset: (x,y) location of text relative to image.
993%
994% o metrics: bounding box of text.
995%
cristy5cbc0162011-08-29 00:36:28 +0000996% o exception: return any errors or warnings in this structure.
997%
cristy3ed852e2009-09-05 21:47:34 +0000998*/
999
1000#if defined(MAGICKCORE_FREETYPE_DELEGATE)
cristy2857ffc2010-03-27 23:44:00 +00001001
cristy3ed852e2009-09-05 21:47:34 +00001002static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1003 DrawInfo *draw_info)
1004{
1005 AffineMatrix
1006 affine;
1007
1008 char
cristy151b66d2015-04-15 10:50:31 +00001009 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001010
1011 affine=draw_info->affine;
Cristy9c282cc2015-08-29 17:37:22 -04001012 (void) FormatLocaleString(path,MagickPathExtent,"C%g,%g %g,%g %g,%g",
1013 affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-
1014 q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001015 (void) ConcatenateString(&draw_info->primitive,path);
1016 return(0);
1017}
1018
1019static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1020{
1021 AffineMatrix
1022 affine;
1023
1024 char
cristy151b66d2015-04-15 10:50:31 +00001025 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001026
1027 affine=draw_info->affine;
Cristy9c282cc2015-08-29 17:37:22 -04001028 (void) FormatLocaleString(path,MagickPathExtent,"L%g,%g",affine.tx+to->x/64.0,
1029 affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001030 (void) ConcatenateString(&draw_info->primitive,path);
1031 return(0);
1032}
1033
1034static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1035{
1036 AffineMatrix
1037 affine;
1038
1039 char
cristy151b66d2015-04-15 10:50:31 +00001040 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001041
1042 affine=draw_info->affine;
Cristy9c282cc2015-08-29 17:37:22 -04001043 (void) FormatLocaleString(path,MagickPathExtent,"M%g,%g",affine.tx+to->x/64.0,
1044 affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001045 (void) ConcatenateString(&draw_info->primitive,path);
1046 return(0);
1047}
1048
1049static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1050 DrawInfo *draw_info)
1051{
1052 AffineMatrix
1053 affine;
1054
1055 char
cristy151b66d2015-04-15 10:50:31 +00001056 path[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001057
1058 affine=draw_info->affine;
Cristy9c282cc2015-08-29 17:37:22 -04001059 (void) FormatLocaleString(path,MagickPathExtent,"Q%g,%g %g,%g",affine.tx+
1060 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1061 to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001062 (void) ConcatenateString(&draw_info->primitive,path);
1063 return(0);
1064}
1065
1066static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001067 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1068 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001069{
1070#if !defined(FT_OPEN_PATHNAME)
1071#define FT_OPEN_PATHNAME ft_open_pathname
1072#endif
1073
1074 typedef struct _GlyphInfo
1075 {
1076 FT_UInt
1077 id;
1078
1079 FT_Vector
1080 origin;
1081
1082 FT_Glyph
1083 image;
1084 } GlyphInfo;
1085
1086 const char
1087 *value;
1088
cristyc9b12952010-03-28 01:12:28 +00001089 double
1090 direction;
1091
cristy3ed852e2009-09-05 21:47:34 +00001092 DrawInfo
1093 *annotate_info;
1094
1095 FT_BBox
1096 bounds;
1097
1098 FT_BitmapGlyph
1099 bitmap;
1100
1101 FT_Encoding
1102 encoding_type;
1103
1104 FT_Error
cristyf8f295f2013-08-11 17:26:49 +00001105 ft_status;
cristy3ed852e2009-09-05 21:47:34 +00001106
1107 FT_Face
1108 face;
1109
1110 FT_Int32
1111 flags;
1112
1113 FT_Library
1114 library;
1115
1116 FT_Matrix
1117 affine;
1118
1119 FT_Open_Args
1120 args;
1121
1122 FT_Vector
1123 origin;
1124
1125 GlyphInfo
1126 glyph,
1127 last_glyph;
1128
cristyf8f295f2013-08-11 17:26:49 +00001129 MagickBooleanType
1130 status;
1131
cristy3ed852e2009-09-05 21:47:34 +00001132 PointInfo
1133 point,
1134 resolution;
1135
1136 register char
1137 *p;
1138
cristy9d314ff2011-03-09 01:30:28 +00001139 ssize_t
1140 code,
1141 y;
1142
cristy3ed852e2009-09-05 21:47:34 +00001143 static FT_Outline_Funcs
1144 OutlineMethods =
1145 {
1146 (FT_Outline_MoveTo_Func) TraceMoveTo,
1147 (FT_Outline_LineTo_Func) TraceLineTo,
1148 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1149 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1150 0, 0
1151 };
1152
cristy2857ffc2010-03-27 23:44:00 +00001153 unsigned char
1154 *utf8;
1155
cristy3ed852e2009-09-05 21:47:34 +00001156 /*
1157 Initialize Truetype library.
1158 */
cristyf8f295f2013-08-11 17:26:49 +00001159 ft_status=FT_Init_FreeType(&library);
1160 if (ft_status != 0)
cristy3ed852e2009-09-05 21:47:34 +00001161 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1162 image->filename);
1163 args.flags=FT_OPEN_PATHNAME;
1164 if (draw_info->font == (char *) NULL)
1165 args.pathname=ConstantString("helvetica");
1166 else
1167 if (*draw_info->font != '@')
1168 args.pathname=ConstantString(draw_info->font);
1169 else
1170 args.pathname=ConstantString(draw_info->font+1);
1171 face=(FT_Face) NULL;
cristyf8f295f2013-08-11 17:26:49 +00001172 ft_status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
cristy3ed852e2009-09-05 21:47:34 +00001173 args.pathname=DestroyString(args.pathname);
cristyf8f295f2013-08-11 17:26:49 +00001174 if (ft_status != 0)
cristy3ed852e2009-09-05 21:47:34 +00001175 {
1176 (void) FT_Done_FreeType(library);
cristy5cbc0162011-08-29 00:36:28 +00001177 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
cristyefe601c2013-01-05 17:51:12 +00001178 "UnableToReadFont","`%s'",draw_info->font);
cristy5cbc0162011-08-29 00:36:28 +00001179 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001180 }
1181 if ((draw_info->metrics != (char *) NULL) &&
1182 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1183 (void) FT_Attach_File(face,draw_info->metrics);
1184 encoding_type=ft_encoding_unicode;
cristyf8f295f2013-08-11 17:26:49 +00001185 ft_status=FT_Select_Charmap(face,encoding_type);
1186 if ((ft_status != 0) && (face->num_charmaps != 0))
1187 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
cristy3ed852e2009-09-05 21:47:34 +00001188 if (encoding != (const char *) NULL)
1189 {
1190 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1191 encoding_type=ft_encoding_adobe_custom;
1192 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1193 encoding_type=ft_encoding_adobe_expert;
1194 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1195 encoding_type=ft_encoding_adobe_standard;
1196 if (LocaleCompare(encoding,"AppleRoman") == 0)
1197 encoding_type=ft_encoding_apple_roman;
1198 if (LocaleCompare(encoding,"BIG5") == 0)
1199 encoding_type=ft_encoding_big5;
1200 if (LocaleCompare(encoding,"GB2312") == 0)
1201 encoding_type=ft_encoding_gb2312;
1202 if (LocaleCompare(encoding,"Johab") == 0)
1203 encoding_type=ft_encoding_johab;
1204#if defined(ft_encoding_latin_1)
1205 if (LocaleCompare(encoding,"Latin-1") == 0)
1206 encoding_type=ft_encoding_latin_1;
1207#endif
1208 if (LocaleCompare(encoding,"Latin-2") == 0)
1209 encoding_type=ft_encoding_latin_2;
1210 if (LocaleCompare(encoding,"None") == 0)
1211 encoding_type=ft_encoding_none;
1212 if (LocaleCompare(encoding,"SJIScode") == 0)
1213 encoding_type=ft_encoding_sjis;
1214 if (LocaleCompare(encoding,"Symbol") == 0)
1215 encoding_type=ft_encoding_symbol;
1216 if (LocaleCompare(encoding,"Unicode") == 0)
1217 encoding_type=ft_encoding_unicode;
1218 if (LocaleCompare(encoding,"Wansung") == 0)
1219 encoding_type=ft_encoding_wansung;
cristyf8f295f2013-08-11 17:26:49 +00001220 ft_status=FT_Select_Charmap(face,encoding_type);
1221 if (ft_status != 0)
cristye3b2bb82014-02-15 13:20:08 +00001222 {
1223 (void) FT_Done_Face(face);
1224 (void) FT_Done_FreeType(library);
1225 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1226 }
cristy3ed852e2009-09-05 21:47:34 +00001227 }
1228 /*
1229 Set text size.
1230 */
1231 resolution.x=DefaultResolution;
1232 resolution.y=DefaultResolution;
1233 if (draw_info->density != (char *) NULL)
1234 {
1235 GeometryInfo
1236 geometry_info;
1237
1238 MagickStatusType
dirkfa589d62015-09-20 16:59:31 +02001239 geometry_flags;
cristy3ed852e2009-09-05 21:47:34 +00001240
dirkfa589d62015-09-20 16:59:31 +02001241 geometry_flags=ParseGeometry(draw_info->density,&geometry_info);
cristy3ed852e2009-09-05 21:47:34 +00001242 resolution.x=geometry_info.rho;
1243 resolution.y=geometry_info.sigma;
dirkfa589d62015-09-20 16:59:31 +02001244 if ((geometry_flags & SigmaValue) == 0)
cristy3ed852e2009-09-05 21:47:34 +00001245 resolution.y=resolution.x;
1246 }
cristyf8f295f2013-08-11 17:26:49 +00001247 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
cristy3ed852e2009-09-05 21:47:34 +00001248 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1249 (FT_UInt) resolution.y);
1250 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1251 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1252 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1253 metrics->descent=(double) face->size->metrics.descender/64.0;
1254 metrics->width=0;
1255 metrics->origin.x=0;
1256 metrics->origin.y=0;
1257 metrics->height=(double) face->size->metrics.height/64.0;
1258 metrics->max_advance=0.0;
1259 if (face->size->metrics.max_advance > MagickEpsilon)
1260 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1261 metrics->bounds.x1=0.0;
1262 metrics->bounds.y1=metrics->descent;
1263 metrics->bounds.x2=metrics->ascent+metrics->descent;
1264 metrics->bounds.y2=metrics->ascent+metrics->descent;
1265 metrics->underline_position=face->underline_position/64.0;
1266 metrics->underline_thickness=face->underline_thickness/64.0;
cristy71709bf2011-09-25 13:10:29 +00001267 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
cristy3ed852e2009-09-05 21:47:34 +00001268 {
1269 (void) FT_Done_Face(face);
1270 (void) FT_Done_FreeType(library);
1271 return(MagickTrue);
1272 }
1273 /*
1274 Compute bounding box.
1275 */
1276 if (image->debug != MagickFalse)
1277 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
cristye7f51092010-01-17 00:39:37 +00001278 "font-encoding %s; text-encoding %s; pointsize %g",
cristy3ed852e2009-09-05 21:47:34 +00001279 draw_info->font != (char *) NULL ? draw_info->font : "none",
1280 encoding != (char *) NULL ? encoding : "none",
1281 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1282 draw_info->pointsize);
1283 flags=FT_LOAD_NO_BITMAP;
cristy55a804e2012-01-13 17:46:11 +00001284 if (draw_info->text_antialias == MagickFalse)
cristy4f9709e2012-06-10 01:54:51 +00001285 flags|=FT_LOAD_TARGET_MONO;
cristy55a804e2012-01-13 17:46:11 +00001286 else
1287 {
cristya89189d2012-01-13 17:54:57 +00001288#if defined(FT_LOAD_TARGET_LIGHT)
cristy55a804e2012-01-13 17:46:11 +00001289 flags|=FT_LOAD_TARGET_LIGHT;
cristya89189d2012-01-13 17:54:57 +00001290#elif defined(FT_LOAD_TARGET_LCD)
1291 flags|=FT_LOAD_TARGET_LCD;
cristy55a804e2012-01-13 17:46:11 +00001292#endif
1293 }
cristyd15e6592011-10-15 00:13:06 +00001294 value=GetImageProperty(image,"type:hinting",exception);
cristy33204242010-10-07 23:17:12 +00001295 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
cristy3ed852e2009-09-05 21:47:34 +00001296 flags|=FT_LOAD_NO_HINTING;
1297 glyph.id=0;
1298 glyph.image=NULL;
1299 last_glyph.id=0;
1300 last_glyph.image=NULL;
1301 origin.x=0;
1302 origin.y=0;
1303 affine.xx=65536L;
1304 affine.yx=0L;
1305 affine.xy=0L;
1306 affine.yy=65536L;
1307 if (draw_info->render != MagickFalse)
1308 {
1309 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1310 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1311 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1312 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1313 }
1314 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
Cristy9c282cc2015-08-29 17:37:22 -04001315 if (annotate_info->dash_pattern != (double *) NULL)
1316 annotate_info->dash_pattern[0]=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001317 (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;
Cristy9c282cc2015-08-29 17:37:22 -04001384 if (((draw_info->stroke.alpha != TransparentAlpha) ||
1385 (draw_info->stroke_pattern != (Image *) NULL)) &&
1386 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
cristyf8f295f2013-08-11 17:26:49 +00001387 {
Cristy9c282cc2015-08-29 17:37:22 -04001388 /*
1389 Trace the glyph.
1390 */
1391 annotate_info->affine.tx=glyph.origin.x/64.0;
1392 annotate_info->affine.ty=glyph.origin.y/64.0;
1393 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->outline,
1394 &OutlineMethods,annotate_info);
1395 }
cristy2857ffc2010-03-27 23:44:00 +00001396 FT_Vector_Transform(&glyph.origin,&affine);
1397 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
cristyf8f295f2013-08-11 17:26:49 +00001398 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
cristy2857ffc2010-03-27 23:44:00 +00001399 (FT_Vector *) NULL,MagickTrue);
cristyf8f295f2013-08-11 17:26:49 +00001400 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001401 continue;
1402 bitmap=(FT_BitmapGlyph) glyph.image;
1403 point.x=offset->x+bitmap->left;
cristy2f5b5c72011-09-16 16:52:00 +00001404 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1405 point.x=offset->x+(origin.x >> 6);
cristy2857ffc2010-03-27 23:44:00 +00001406 point.y=offset->y-bitmap->top;
1407 if (draw_info->render != MagickFalse)
1408 {
1409 CacheView
1410 *image_view;
cristybd512462010-02-13 02:13:14 +00001411
cristy2f5b5c72011-09-16 16:52:00 +00001412 register unsigned char
dirkfa589d62015-09-20 16:59:31 +02001413 *r;
cristy2f5b5c72011-09-16 16:52:00 +00001414
cristy2857ffc2010-03-27 23:44:00 +00001415 /*
1416 Rasterize the glyph.
1417 */
cristy46ff2672012-12-14 15:32:26 +00001418 image_view=AcquireAuthenticCacheView(image,exception);
dirkfa589d62015-09-20 16:59:31 +02001419 r=bitmap->bitmap.buffer;
cristybb503372010-05-27 20:51:26 +00001420 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
cristy2857ffc2010-03-27 23:44:00 +00001421 {
cristybd512462010-02-13 02:13:14 +00001422 MagickBooleanType
cristy2857ffc2010-03-27 23:44:00 +00001423 active,
1424 sync;
cristybd512462010-02-13 02:13:14 +00001425
cristya19f1d72012-08-07 18:24:38 +00001426 double
cristy2857ffc2010-03-27 23:44:00 +00001427 fill_opacity;
cristybd512462010-02-13 02:13:14 +00001428
cristy101ab702011-10-13 13:06:32 +00001429 PixelInfo
cristy2857ffc2010-03-27 23:44:00 +00001430 fill_color;
cristybd512462010-02-13 02:13:14 +00001431
cristy4c08aed2011-07-01 19:47:50 +00001432 register Quantum
cristy2857ffc2010-03-27 23:44:00 +00001433 *restrict q;
cristybd512462010-02-13 02:13:14 +00001434
cristy88158062011-06-09 00:31:43 +00001435 register ssize_t
1436 x;
1437
cristy73ce31b2010-06-13 23:17:39 +00001438 ssize_t
cristy2f5b5c72011-09-16 16:52:00 +00001439 n,
cristy73ce31b2010-06-13 23:17:39 +00001440 x_offset,
1441 y_offset;
1442
cristy2857ffc2010-03-27 23:44:00 +00001443 if (status == MagickFalse)
1444 continue;
cristybb503372010-05-27 20:51:26 +00001445 x_offset=(ssize_t) ceil(point.x-0.5);
1446 y_offset=(ssize_t) ceil(point.y+y-0.5);
1447 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy2857ffc2010-03-27 23:44:00 +00001448 continue;
cristy4c08aed2011-07-01 19:47:50 +00001449 q=(Quantum *) NULL;
cristybb503372010-05-27 20:51:26 +00001450 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001451 active=MagickFalse;
1452 else
cristybd512462010-02-13 02:13:14 +00001453 {
cristy2857ffc2010-03-27 23:44:00 +00001454 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1455 bitmap->bitmap.width,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001456 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
cristybd512462010-02-13 02:13:14 +00001457 }
cristy854209a2011-09-19 17:02:57 +00001458 n=y*bitmap->bitmap.pitch-1;
1459 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
cristy2857ffc2010-03-27 23:44:00 +00001460 {
cristy854209a2011-09-19 17:02:57 +00001461 n++;
cristy2857ffc2010-03-27 23:44:00 +00001462 x_offset++;
cristy2f5b5c72011-09-16 16:52:00 +00001463 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001464 {
cristydcf8f732015-01-24 20:54:48 +00001465 if (q != (Quantum *) NULL)
1466 q+=GetPixelChannels(image);
cristy2857ffc2010-03-27 23:44:00 +00001467 continue;
1468 }
cristy2f5b5c72011-09-16 16:52:00 +00001469 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
dirkfa589d62015-09-20 16:59:31 +02001470 fill_opacity=(double) (r[n])/(bitmap->bitmap.num_grays-1);
cristy2f5b5c72011-09-16 16:52:00 +00001471 else
dirkfa589d62015-09-20 16:59:31 +02001472 fill_opacity=((r[(x >> 3)+y*bitmap->bitmap.pitch] &
cristy2f5b5c72011-09-16 16:52:00 +00001473 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
cristy2857ffc2010-03-27 23:44:00 +00001474 if (draw_info->text_antialias == MagickFalse)
1475 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1476 if (active == MagickFalse)
1477 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1478 exception);
cristy6193b862011-08-06 16:33:59 +00001479 if (q == (Quantum *) NULL)
cristy9c5171c2014-05-25 21:26:39 +00001480 continue;
cristyec061ec2011-10-21 17:41:17 +00001481 GetPixelInfo(image,&fill_color);
cristy2ed42f62011-10-02 19:49:57 +00001482 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1483 exception);
cristy4c08aed2011-07-01 19:47:50 +00001484 fill_opacity=fill_opacity*fill_color.alpha;
1485 CompositePixelOver(image,&fill_color,fill_opacity,q,
1486 GetPixelAlpha(image,q),q);
cristy2857ffc2010-03-27 23:44:00 +00001487 if (active == MagickFalse)
1488 {
1489 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1490 if (sync == MagickFalse)
1491 status=MagickFalse;
1492 }
cristyed231572011-07-14 02:18:59 +00001493 q+=GetPixelChannels(image);
cristybd512462010-02-13 02:13:14 +00001494 }
cristy2857ffc2010-03-27 23:44:00 +00001495 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1496 if (sync == MagickFalse)
1497 status=MagickFalse;
cristybd512462010-02-13 02:13:14 +00001498 }
cristy2857ffc2010-03-27 23:44:00 +00001499 image_view=DestroyCacheView(image_view);
Cristy9c282cc2015-08-29 17:37:22 -04001500 if (((draw_info->stroke.alpha != TransparentAlpha) ||
1501 (draw_info->stroke_pattern != (Image *) NULL)) &&
1502 (status != MagickFalse))
1503 {
1504 /*
1505 Draw text stroke.
1506 */
1507 annotate_info->linejoin=RoundJoin;
1508 annotate_info->affine.tx=offset->x;
1509 annotate_info->affine.ty=offset->y;
1510 (void) ConcatenateString(&annotate_info->primitive,"'");
1511 (void) DrawImage(image,annotate_info,exception);
1512 (void) CloneString(&annotate_info->primitive,"path '");
1513 }
cristy2857ffc2010-03-27 23:44:00 +00001514 }
1515 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1516 metrics->width=bitmap->left+bitmap->bitmap.width;
cristyaa83c2c2011-09-21 13:36:25 +00001517 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
cristy2857ffc2010-03-27 23:44:00 +00001518 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1519 (IsUTFSpace(code) == MagickFalse))
cristy94b11832011-09-08 19:46:03 +00001520 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
cristy2857ffc2010-03-27 23:44:00 +00001521 else
cristy94b11832011-09-08 19:46:03 +00001522 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
cristyaa83c2c2011-09-21 13:36:25 +00001523 metrics->origin.x=(double) origin.x;
1524 metrics->origin.y=(double) origin.y;
cristybd5a96c2011-08-21 00:04:26 +00001525 if (last_glyph.id != 0)
1526 FT_Done_Glyph(last_glyph.image);
cristy2857ffc2010-03-27 23:44:00 +00001527 last_glyph=glyph;
1528 code=GetUTFCode(p);
1529 }
1530 if (utf8 != (unsigned char *) NULL)
1531 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
cristybd5a96c2011-08-21 00:04:26 +00001532 if (last_glyph.id != 0)
1533 FT_Done_Glyph(last_glyph.image);
cristy3ed852e2009-09-05 21:47:34 +00001534 /*
1535 Determine font metrics.
1536 */
1537 glyph.id=FT_Get_Char_Index(face,'_');
1538 glyph.origin=origin;
cristyf8f295f2013-08-11 17:26:49 +00001539 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1540 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001541 {
cristyf8f295f2013-08-11 17:26:49 +00001542 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1543 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001544 {
cristyf8f295f2013-08-11 17:26:49 +00001545 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1546 outline,&bounds);
1547 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001548 {
1549 FT_Vector_Transform(&glyph.origin,&affine);
1550 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
cristyf8f295f2013-08-11 17:26:49 +00001551 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
cristy3ed852e2009-09-05 21:47:34 +00001552 (FT_Vector *) NULL,MagickTrue);
1553 bitmap=(FT_BitmapGlyph) glyph.image;
1554 if (bitmap->left > metrics->width)
1555 metrics->width=bitmap->left;
1556 }
1557 }
cristye2c81ad2011-08-20 22:54:14 +00001558 FT_Done_Glyph(glyph.image);
cristy3ed852e2009-09-05 21:47:34 +00001559 }
1560 metrics->width-=metrics->bounds.x1/64.0;
cristy55cb9f12012-08-23 09:02:28 +00001561 metrics->width+=annotate_info->stroke_width;
cristy3ed852e2009-09-05 21:47:34 +00001562 metrics->bounds.x1/=64.0;
1563 metrics->bounds.y1/=64.0;
1564 metrics->bounds.x2/=64.0;
1565 metrics->bounds.y2/=64.0;
1566 metrics->origin.x/=64.0;
1567 metrics->origin.y/=64.0;
1568 /*
1569 Relinquish resources.
1570 */
1571 annotate_info=DestroyDrawInfo(annotate_info);
1572 (void) FT_Done_Face(face);
1573 (void) FT_Done_FreeType(library);
cristyf8f295f2013-08-11 17:26:49 +00001574 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001575}
1576#else
1577static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1578 const char *magick_unused(encoding),const PointInfo *offset,
cristy5cbc0162011-08-29 00:36:28 +00001579 TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001580{
cristy5cbc0162011-08-29 00:36:28 +00001581 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001582 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
cristyee081822011-02-02 19:07:30 +00001583 draw_info->font != (char *) NULL ? draw_info->font : "none");
cristye3b062a2011-12-17 18:34:52 +00001584 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001585}
1586#endif
1587
1588/*
1589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1590% %
1591% %
1592% %
1593+ R e n d e r P o s t s c r i p t %
1594% %
1595% %
1596% %
1597%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1598%
1599% RenderPostscript() renders text on the image with a Postscript font. It
1600% also returns the bounding box of the text relative to the image.
1601%
1602% The format of the RenderPostscript method is:
1603%
1604% MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001605% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001606%
1607% A description of each parameter follows:
1608%
1609% o image: the image.
1610%
1611% o draw_info: the draw info.
1612%
1613% o offset: (x,y) location of text relative to image.
1614%
1615% o metrics: bounding box of text.
1616%
cristy5cbc0162011-08-29 00:36:28 +00001617% o exception: return any errors or warnings in this structure.
1618%
cristy3ed852e2009-09-05 21:47:34 +00001619*/
1620
cristy3ed852e2009-09-05 21:47:34 +00001621static char *EscapeParenthesis(const char *text)
1622{
1623 char
1624 *buffer;
1625
1626 register char
1627 *p;
1628
cristybb503372010-05-27 20:51:26 +00001629 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001630 i;
1631
1632 size_t
1633 escapes;
1634
1635 escapes=0;
1636 buffer=AcquireString(text);
1637 p=buffer;
cristy151b66d2015-04-15 10:50:31 +00001638 for (i=0; i < (ssize_t) MagickMin(strlen(text),MagickPathExtent-escapes-1); i++)
cristy3ed852e2009-09-05 21:47:34 +00001639 {
1640 if ((text[i] == '(') || (text[i] == ')'))
1641 {
1642 *p++='\\';
1643 escapes++;
1644 }
1645 *p++=text[i];
1646 }
1647 *p='\0';
1648 return(buffer);
1649}
1650
1651static MagickBooleanType RenderPostscript(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00001652 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1653 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001654{
1655 char
cristy151b66d2015-04-15 10:50:31 +00001656 filename[MagickPathExtent],
1657 geometry[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +00001658 *text;
1659
1660 FILE
1661 *file;
1662
1663 Image
1664 *annotate_image;
1665
1666 ImageInfo
1667 *annotate_info;
1668
1669 int
1670 unique_file;
1671
cristy3ed852e2009-09-05 21:47:34 +00001672 MagickBooleanType
1673 identity;
1674
1675 PointInfo
1676 extent,
1677 point,
1678 resolution;
1679
cristybb503372010-05-27 20:51:26 +00001680 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001681 i;
1682
cristy14d71292012-05-20 16:48:13 +00001683 size_t
1684 length;
1685
cristy9d314ff2011-03-09 01:30:28 +00001686 ssize_t
1687 y;
1688
cristy3ed852e2009-09-05 21:47:34 +00001689 /*
1690 Render label with a Postscript font.
1691 */
1692 if (image->debug != MagickFalse)
1693 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +00001694 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
cristy3ed852e2009-09-05 21:47:34 +00001695 draw_info->font : "none",draw_info->pointsize);
1696 file=(FILE *) NULL;
1697 unique_file=AcquireUniqueFileResource(filename);
1698 if (unique_file != -1)
1699 file=fdopen(unique_file,"wb");
1700 if ((unique_file == -1) || (file == (FILE *) NULL))
1701 {
cristy5cbc0162011-08-29 00:36:28 +00001702 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
cristy3ed852e2009-09-05 21:47:34 +00001703 return(MagickFalse);
1704 }
cristyb51dff52011-05-19 16:55:47 +00001705 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1706 (void) FormatLocaleFile(file,"/ReencodeType\n");
1707 (void) FormatLocaleFile(file,"{\n");
1708 (void) FormatLocaleFile(file," findfont dup length\n");
1709 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001710 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
cristyb51dff52011-05-19 16:55:47 +00001711 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001712 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
cristyb51dff52011-05-19 16:55:47 +00001713 (void) FormatLocaleFile(file,"} bind def\n");
cristy3ed852e2009-09-05 21:47:34 +00001714 /*
1715 Sample to compute bounding box.
1716 */
cristyaa83c2c2011-09-21 13:36:25 +00001717 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1718 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1719 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001720 extent.x=0.0;
1721 extent.y=0.0;
cristy14d71292012-05-20 16:48:13 +00001722 length=strlen(draw_info->text);
1723 for (i=0; i <= (ssize_t) (length+2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001724 {
1725 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1726 draw_info->affine.ry*2.0*draw_info->pointsize);
1727 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1728 draw_info->affine.sy*2.0*draw_info->pointsize);
1729 if (point.x > extent.x)
1730 extent.x=point.x;
1731 if (point.y > extent.y)
1732 extent.y=point.y;
1733 }
cristyb51dff52011-05-19 16:55:47 +00001734 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
cristy3ed852e2009-09-05 21:47:34 +00001735 extent.x/2.0,extent.y/2.0);
cristyb51dff52011-05-19 16:55:47 +00001736 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
cristy3ed852e2009-09-05 21:47:34 +00001737 draw_info->pointsize);
1738 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1739 (strchr(draw_info->font,'/') != (char *) NULL))
cristyb51dff52011-05-19 16:55:47 +00001740 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001741 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1742 else
cristy1e604812011-05-19 18:07:50 +00001743 (void) FormatLocaleFile(file,
1744 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1745 draw_info->font);
cristyb51dff52011-05-19 16:55:47 +00001746 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
cristy8cd5b312010-01-07 01:10:24 +00001747 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1748 draw_info->affine.sy);
cristy3ed852e2009-09-05 21:47:34 +00001749 text=EscapeParenthesis(draw_info->text);
1750 if (identity == MagickFalse)
cristy1e604812011-05-19 18:07:50 +00001751 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1752 text);
cristyb51dff52011-05-19 16:55:47 +00001753 (void) FormatLocaleFile(file,"(%s) show\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001754 text=DestroyString(text);
cristyb51dff52011-05-19 16:55:47 +00001755 (void) FormatLocaleFile(file,"showpage\n");
cristy3ed852e2009-09-05 21:47:34 +00001756 (void) fclose(file);
cristy151b66d2015-04-15 10:50:31 +00001757 (void) FormatLocaleString(geometry,MagickPathExtent,"%.20gx%.20g+0+0!",
cristye8c25f92010-06-03 00:53:06 +00001758 floor(extent.x+0.5),floor(extent.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001759 annotate_info=AcquireImageInfo();
cristy151b66d2015-04-15 10:50:31 +00001760 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,"ps:%s",
cristy3ed852e2009-09-05 21:47:34 +00001761 filename);
1762 (void) CloneString(&annotate_info->page,geometry);
1763 if (draw_info->density != (char *) NULL)
1764 (void) CloneString(&annotate_info->density,draw_info->density);
1765 annotate_info->antialias=draw_info->text_antialias;
cristy5cbc0162011-08-29 00:36:28 +00001766 annotate_image=ReadImage(annotate_info,exception);
1767 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00001768 annotate_info=DestroyImageInfo(annotate_info);
1769 (void) RelinquishUniqueFileResource(filename);
1770 if (annotate_image == (Image *) NULL)
1771 return(MagickFalse);
cristy55cb9f12012-08-23 09:02:28 +00001772 (void) NegateImage(annotate_image,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001773 resolution.x=DefaultResolution;
1774 resolution.y=DefaultResolution;
1775 if (draw_info->density != (char *) NULL)
1776 {
1777 GeometryInfo
1778 geometry_info;
1779
1780 MagickStatusType
1781 flags;
1782
1783 flags=ParseGeometry(draw_info->density,&geometry_info);
1784 resolution.x=geometry_info.rho;
1785 resolution.y=geometry_info.sigma;
1786 if ((flags & SigmaValue) == 0)
1787 resolution.y=resolution.x;
1788 }
1789 if (identity == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001790 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001791 else
1792 {
1793 RectangleInfo
1794 crop_info;
1795
cristy5cbc0162011-08-29 00:36:28 +00001796 crop_info=GetImageBoundingBox(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001797 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
cristy3ed852e2009-09-05 21:47:34 +00001798 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
cristybb503372010-05-27 20:51:26 +00001799 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
cristy06609ee2010-03-17 20:21:27 +00001800 0.5);
cristy151b66d2015-04-15 10:50:31 +00001801 (void) FormatLocaleString(geometry,MagickPathExtent,
cristy6d8abba2010-06-03 01:10:47 +00001802 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
cristye8c25f92010-06-03 00:53:06 +00001803 crop_info.height,(double) crop_info.x,(double) crop_info.y);
cristye941a752011-10-15 01:52:48 +00001804 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001805 }
1806 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1807 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1808 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1809 metrics->ascent=metrics->pixels_per_em.x;
1810 metrics->descent=metrics->pixels_per_em.y/-5.0;
1811 metrics->width=(double) annotate_image->columns/
1812 ExpandAffine(&draw_info->affine);
1813 metrics->height=1.152*metrics->pixels_per_em.x;
1814 metrics->max_advance=metrics->pixels_per_em.x;
1815 metrics->bounds.x1=0.0;
1816 metrics->bounds.y1=metrics->descent;
1817 metrics->bounds.x2=metrics->ascent+metrics->descent;
1818 metrics->bounds.y2=metrics->ascent+metrics->descent;
1819 metrics->underline_position=(-2.0);
1820 metrics->underline_thickness=1.0;
1821 if (draw_info->render == MagickFalse)
1822 {
1823 annotate_image=DestroyImage(annotate_image);
1824 return(MagickTrue);
1825 }
cristy4c08aed2011-07-01 19:47:50 +00001826 if (draw_info->fill.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001827 {
cristybd659482011-10-09 23:19:32 +00001828 CacheView
1829 *annotate_view;
1830
cristy3ed852e2009-09-05 21:47:34 +00001831 MagickBooleanType
1832 sync;
1833
cristy101ab702011-10-13 13:06:32 +00001834 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001835 fill_color;
1836
cristy3ed852e2009-09-05 21:47:34 +00001837 /*
1838 Render fill color.
1839 */
cristy17f11b02014-12-20 19:37:04 +00001840 if (image->alpha_trait == UndefinedPixelTrait)
cristy63240882011-08-05 19:05:27 +00001841 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy17f11b02014-12-20 19:37:04 +00001842 if (annotate_image->alpha_trait == UndefinedPixelTrait)
cristy63240882011-08-05 19:05:27 +00001843 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1844 exception);
1845 fill_color=draw_info->fill;
cristy46ff2672012-12-14 15:32:26 +00001846 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001847 for (y=0; y < (ssize_t) annotate_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001848 {
cristybb503372010-05-27 20:51:26 +00001849 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001850 x;
1851
cristy4c08aed2011-07-01 19:47:50 +00001852 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001853 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001854
1855 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1856 1,exception);
cristy6193b862011-08-06 16:33:59 +00001857 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001858 break;
cristybb503372010-05-27 20:51:26 +00001859 for (x=0; x < (ssize_t) annotate_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001860 {
cristy2ed42f62011-10-02 19:49:57 +00001861 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy55cb9f12012-08-23 09:02:28 +00001862 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1863 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
cristy4c08aed2011-07-01 19:47:50 +00001864 SetPixelRed(annotate_image,fill_color.red,q);
1865 SetPixelGreen(annotate_image,fill_color.green,q);
1866 SetPixelBlue(annotate_image,fill_color.blue,q);
cristyed231572011-07-14 02:18:59 +00001867 q+=GetPixelChannels(annotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001868 }
1869 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1870 if (sync == MagickFalse)
1871 break;
1872 }
1873 annotate_view=DestroyCacheView(annotate_view);
cristy39172402012-03-30 13:04:39 +00001874 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
cristybb503372010-05-27 20:51:26 +00001875 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
cristye941a752011-10-15 01:52:48 +00001876 metrics->descent)-0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00001877 }
1878 annotate_image=DestroyImage(annotate_image);
1879 return(MagickTrue);
1880}
1881
1882/*
1883%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1884% %
1885% %
1886% %
1887+ R e n d e r X 1 1 %
1888% %
1889% %
1890% %
1891%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1892%
1893% RenderX11() renders text on the image with an X11 font. It also returns the
1894% bounding box of the text relative to the image.
1895%
1896% The format of the RenderX11 method is:
1897%
1898% MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001899% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001900%
1901% A description of each parameter follows:
1902%
1903% o image: the image.
1904%
1905% o draw_info: the draw info.
1906%
1907% o offset: (x,y) location of text relative to image.
1908%
1909% o metrics: bounding box of text.
1910%
cristy5cbc0162011-08-29 00:36:28 +00001911% o exception: return any errors or warnings in this structure.
1912%
cristy3ed852e2009-09-05 21:47:34 +00001913*/
cristycc168c22013-04-18 14:42:31 +00001914static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1915 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001916{
1917 MagickBooleanType
1918 status;
1919
cristyd7ecaca2011-01-24 14:14:53 +00001920 if (annotate_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +00001921 ActivateSemaphoreInfo(&annotate_semaphore);
cristyd7ecaca2011-01-24 14:14:53 +00001922 LockSemaphoreInfo(annotate_semaphore);
cristy534d0a32013-04-13 16:27:24 +00001923 status=XRenderImage(image,draw_info,offset,metrics,exception);
cristyd7ecaca2011-01-24 14:14:53 +00001924 UnlockSemaphoreInfo(annotate_semaphore);
cristy534d0a32013-04-13 16:27:24 +00001925 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001926}