blob: 846537a4a2a2eaa0e95582c6debb15fc020a62c7 [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% %
cristyfe676ee2013-11-18 13:03:38 +000020% Copyright 1999-2014 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%
193% %b file size in bytes.
194% %c comment.
195% %d directory in which the image resides.
196% %e extension of the image file.
197% %f original filename of the image.
198% %h height of image.
199% %i filename of the image.
200% %k number of unique colors.
201% %l image label.
202% %m image file format.
203% %n number of images in a image sequence.
204% %o output image filename.
205% %p page number of the image.
206% %q image depth (8 or 16).
207% %q image depth (8 or 16).
208% %s image scene number.
209% %t image filename without any extension.
210% %u a unique temporary filename.
211% %w image width.
212% %x x resolution of the image.
213% %y y resolution of the image.
214%
215% The format of the AnnotateImage method is:
216%
cristy5cbc0162011-08-29 00:36:28 +0000217% MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
218% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000219%
220% A description of each parameter follows:
221%
222% o image: the image.
223%
224% o draw_info: the draw info.
225%
cristy5cbc0162011-08-29 00:36:28 +0000226% o exception: return any errors or warnings in this structure.
227%
cristy3ed852e2009-09-05 21:47:34 +0000228*/
229MagickExport MagickBooleanType AnnotateImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000230 const DrawInfo *draw_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000231{
232 char
233 primitive[MaxTextExtent],
234 **textlist;
235
236 DrawInfo
237 *annotate,
238 *annotate_info;
239
240 GeometryInfo
241 geometry_info;
242
243 MagickBooleanType
244 status;
245
246 PointInfo
247 offset;
248
249 RectangleInfo
250 geometry;
251
cristybb503372010-05-27 20:51:26 +0000252 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000253 i;
254
255 size_t
256 length;
257
258 TypeMetric
259 metrics;
260
cristybb503372010-05-27 20:51:26 +0000261 size_t
cristy58460392009-09-09 01:52:06 +0000262 height,
cristy3ed852e2009-09-05 21:47:34 +0000263 number_lines;
264
265 assert(image != (Image *) NULL);
266 assert(image->signature == MagickSignature);
267 if (image->debug != MagickFalse)
268 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
269 assert(draw_info != (DrawInfo *) NULL);
270 assert(draw_info->signature == MagickSignature);
271 if (draw_info->text == (char *) NULL)
272 return(MagickFalse);
273 if (*draw_info->text == '\0')
274 return(MagickTrue);
275 textlist=StringToList(draw_info->text);
276 if (textlist == (char **) NULL)
277 return(MagickFalse);
278 length=strlen(textlist[0]);
279 for (i=1; textlist[i] != (char *) NULL; i++)
280 if (strlen(textlist[i]) > length)
281 length=strlen(textlist[i]);
cristybb503372010-05-27 20:51:26 +0000282 number_lines=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000283 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
284 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
285 SetGeometry(image,&geometry);
286 SetGeometryInfo(&geometry_info);
287 if (annotate_info->geometry != (char *) NULL)
288 {
289 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
cristy5cbc0162011-08-29 00:36:28 +0000290 exception);
cristy3ed852e2009-09-05 21:47:34 +0000291 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
292 }
cristy5cbc0162011-08-29 00:36:28 +0000293 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000294 return(MagickFalse);
cristya6400b12013-03-15 12:20:18 +0000295 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +0000296 (void) SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000297 status=MagickTrue;
298 for (i=0; textlist[i] != (char *) NULL; i++)
299 {
300 /*
301 Position text relative to image.
302 */
303 annotate_info->affine.tx=geometry_info.xi-image->page.x;
304 annotate_info->affine.ty=geometry_info.psi-image->page.y;
305 (void) CloneString(&annotate->text,textlist[i]);
cristy5cbc0162011-08-29 00:36:28 +0000306 (void) GetTypeMetrics(image,annotate,&metrics,exception);
cristybb503372010-05-27 20:51:26 +0000307 height=(ssize_t) (metrics.ascent-metrics.descent+
cristy58460392009-09-09 01:52:06 +0000308 draw_info->interline_spacing+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000309 switch (annotate->gravity)
310 {
311 case UndefinedGravity:
312 default:
313 {
314 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
315 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
316 break;
317 }
318 case NorthWestGravity:
319 {
320 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
321 annotate_info->affine.ry*height+annotate_info->affine.ry*
322 (metrics.ascent+metrics.descent);
323 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
324 annotate_info->affine.sy*height+annotate_info->affine.sy*
325 metrics.ascent;
326 break;
327 }
328 case NorthGravity:
329 {
330 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
331 geometry.width/2.0+i*annotate_info->affine.ry*height-
332 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
333 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
334 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
335 annotate_info->affine.sy*height+annotate_info->affine.sy*
336 metrics.ascent-annotate_info->affine.rx*(metrics.width-
337 metrics.bounds.x1)/2.0;
338 break;
339 }
340 case NorthEastGravity:
341 {
342 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
343 geometry.width+i*annotate_info->affine.ry*height-
344 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
345 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
346 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
347 annotate_info->affine.sy*height+annotate_info->affine.sy*
348 metrics.ascent-annotate_info->affine.rx*(metrics.width-
349 metrics.bounds.x1);
350 break;
351 }
352 case WestGravity:
353 {
354 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
355 annotate_info->affine.ry*height+annotate_info->affine.ry*
356 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
357 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
358 geometry.height/2.0+i*annotate_info->affine.sy*height+
359 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
360 (number_lines-1.0)*height)/2.0;
361 break;
362 }
cristy3ed852e2009-09-05 21:47:34 +0000363 case CenterGravity:
364 {
365 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
366 geometry.width/2.0+i*annotate_info->affine.ry*height-
367 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
368 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
369 (number_lines-1)*height)/2.0;
370 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
371 geometry.height/2.0+i*annotate_info->affine.sy*height-
372 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
373 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
374 (number_lines-1.0)*height)/2.0;
375 break;
376 }
377 case EastGravity:
378 {
379 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
380 geometry.width+i*annotate_info->affine.ry*height-
381 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
382 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
383 (number_lines-1.0)*height)/2.0-1.0;
384 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
385 geometry.height/2.0+i*annotate_info->affine.sy*height-
386 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)+
387 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
388 (number_lines-1.0)*height)/2.0;
389 break;
390 }
391 case SouthWestGravity:
392 {
393 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
394 annotate_info->affine.ry*height-annotate_info->affine.ry*
395 (number_lines-1.0)*height;
396 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
397 geometry.height+i*annotate_info->affine.sy*height-
398 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
399 break;
400 }
401 case SouthGravity:
402 {
403 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
404 geometry.width/2.0+i*annotate_info->affine.ry*height-
405 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
406 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
407 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
408 geometry.height+i*annotate_info->affine.sy*height-
409 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
410 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
411 break;
412 }
413 case SouthEastGravity:
414 {
415 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
416 geometry.width+i*annotate_info->affine.ry*height-
417 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
418 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
419 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
420 geometry.height+i*annotate_info->affine.sy*height-
421 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
422 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
423 break;
424 }
425 }
426 switch (annotate->align)
427 {
428 case LeftAlign:
429 {
430 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
431 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
432 break;
433 }
434 case CenterAlign:
435 {
436 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
437 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
438 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
439 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
440 break;
441 }
442 case RightAlign:
443 {
444 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
445 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
446 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
447 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
448 break;
449 }
450 default:
451 break;
452 }
cristy4c08aed2011-07-01 19:47:50 +0000453 if (draw_info->undercolor.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000454 {
455 DrawInfo
456 *undercolor_info;
457
458 /*
459 Text box.
460 */
461 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
462 undercolor_info->fill=draw_info->undercolor;
463 undercolor_info->affine=draw_info->affine;
464 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
465 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
cristyb51dff52011-05-19 16:55:47 +0000466 (void) FormatLocaleString(primitive,MaxTextExtent,
cristy90ceebd2013-06-13 12:42:09 +0000467 "rectangle -0.5,-0.5 %g,%.20g",metrics.origin.x,(double) height);
cristy3ed852e2009-09-05 21:47:34 +0000468 (void) CloneString(&undercolor_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000469 (void) DrawImage(image,undercolor_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000470 (void) DestroyDrawInfo(undercolor_info);
471 }
472 annotate_info->affine.tx=offset.x;
473 annotate_info->affine.ty=offset.y;
cristyb51dff52011-05-19 16:55:47 +0000474 (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
cristya8549b12011-05-18 19:05:08 +0000475 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
cristy3ed852e2009-09-05 21:47:34 +0000476 if (annotate->decorate == OverlineDecoration)
477 {
478 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
479 metrics.descent-metrics.underline_position));
480 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000481 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000482 }
483 else
484 if (annotate->decorate == UnderlineDecoration)
485 {
486 annotate_info->affine.ty-=(draw_info->affine.sy*
487 metrics.underline_position);
488 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000489 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000490 }
491 /*
492 Annotate image with text.
493 */
cristy5cbc0162011-08-29 00:36:28 +0000494 status=RenderType(image,annotate,&offset,&metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000495 if (status == MagickFalse)
496 break;
497 if (annotate->decorate == LineThroughDecoration)
498 {
499 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
500 metrics.underline_position+metrics.descent)/2.0);
501 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000502 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000503 }
504 }
505 /*
506 Relinquish resources.
507 */
508 annotate_info=DestroyDrawInfo(annotate_info);
509 annotate=DestroyDrawInfo(annotate);
510 for (i=0; textlist[i] != (char *) NULL; i++)
511 textlist[i]=DestroyString(textlist[i]);
512 textlist=(char **) RelinquishMagickMemory(textlist);
513 return(status);
514}
515
516/*
517%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518% %
519% %
520% %
521% F o r m a t M a g i c k C a p t i o n %
522% %
523% %
524% %
525%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
526%
527% FormatMagickCaption() formats a caption so that it fits within the image
528% width. It returns the number of lines in the formatted caption.
529%
530% The format of the FormatMagickCaption method is:
531%
cristybb503372010-05-27 20:51:26 +0000532% ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000533% const MagickBooleanType split,TypeMetric *metrics,char **caption,
534% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000535%
536% A description of each parameter follows.
537%
538% o image: The image.
539%
cristy3ed852e2009-09-05 21:47:34 +0000540% o draw_info: the draw info.
541%
cristy6b1d05e2010-09-22 19:17:27 +0000542% o split: when no convenient line breaks-- insert newline.
543%
cristy3ed852e2009-09-05 21:47:34 +0000544% o metrics: Return the font metrics in this structure.
545%
cristy6b1d05e2010-09-22 19:17:27 +0000546% o caption: the caption.
547%
cristy5cbc0162011-08-29 00:36:28 +0000548% o exception: return any errors or warnings in this structure.
549%
cristy3ed852e2009-09-05 21:47:34 +0000550*/
cristybb503372010-05-27 20:51:26 +0000551MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000552 const MagickBooleanType split,TypeMetric *metrics,char **caption,
553 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000554{
cristy74966db2011-09-20 19:37:21 +0000555 char
556 *text;
557
cristy3ed852e2009-09-05 21:47:34 +0000558 MagickBooleanType
559 status;
560
561 register char
562 *p,
563 *q,
564 *s;
565
cristybb503372010-05-27 20:51:26 +0000566 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000567 i;
568
cristybb503372010-05-27 20:51:26 +0000569 size_t
cristy3ed852e2009-09-05 21:47:34 +0000570 width;
571
cristy6193b862011-08-06 16:33:59 +0000572 ssize_t
573 n;
574
cristy74966db2011-09-20 19:37:21 +0000575 text=AcquireString(draw_info->text);
cristy3ed852e2009-09-05 21:47:34 +0000576 q=draw_info->text;
577 s=(char *) NULL;
578 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
579 {
580 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
581 s=p;
cristy2911f2d2011-08-20 23:04:43 +0000582 if (GetUTFCode(p) == '\n')
cristye2c81ad2011-08-20 22:54:14 +0000583 q=draw_info->text;
cristybb503372010-05-27 20:51:26 +0000584 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
cristy3ed852e2009-09-05 21:47:34 +0000585 *q++=(*(p+i));
586 *q='\0';
cristy5cbc0162011-08-29 00:36:28 +0000587 status=GetTypeMetrics(image,draw_info,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000588 if (status == MagickFalse)
589 break;
cristy78fed5e2013-06-02 17:24:16 +0000590 width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
cristy74966db2011-09-20 19:37:21 +0000591 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
cristy6193b862011-08-06 16:33:59 +0000592 continue;
cristy74966db2011-09-20 19:37:21 +0000593 (void) strcpy(text,draw_info->text);
cristy4aa314e2011-08-19 13:59:45 +0000594 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
cristy3ed852e2009-09-05 21:47:34 +0000595 {
596 *s='\n';
597 p=s;
598 }
599 else
cristy4aa314e2011-08-19 13:59:45 +0000600 if ((s != (char *) NULL) || (split != MagickFalse))
cristy6b1d05e2010-09-22 19:17:27 +0000601 {
602 char
603 *target;
cristy3ed852e2009-09-05 21:47:34 +0000604
cristy6b1d05e2010-09-22 19:17:27 +0000605 /*
606 No convenient line breaks-- insert newline.
607 */
608 target=AcquireString(*caption);
609 n=p-(*caption);
610 CopyMagickString(target,*caption,n+1);
611 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
612 ConcatenateMagickString(target,p,strlen(*caption)+2);
613 (void) DestroyString(*caption);
614 *caption=target;
615 p=(*caption)+n;
616 }
cristy3ed852e2009-09-05 21:47:34 +0000617 q=draw_info->text;
cristy6193b862011-08-06 16:33:59 +0000618 s=(char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000619 }
cristy74966db2011-09-20 19:37:21 +0000620 text=DestroyString(text);
cristy6193b862011-08-06 16:33:59 +0000621 n=0;
cristy3ed852e2009-09-05 21:47:34 +0000622 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
623 if (GetUTFCode(p) == '\n')
cristy6193b862011-08-06 16:33:59 +0000624 n++;
625 return(n);
cristy3ed852e2009-09-05 21:47:34 +0000626}
627
628/*
629%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630% %
631% %
632% %
633% G e t M u l t i l i n e T y p e M e t r i c s %
634% %
635% %
636% %
637%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638%
639% GetMultilineTypeMetrics() returns the following information for the
640% specified font and text:
641%
642% character width
643% character height
644% ascender
645% descender
646% text width
647% text height
648% maximum horizontal advance
649% bounds: x1
650% bounds: y1
651% bounds: x2
652% bounds: y2
653% origin: x
654% origin: y
655% underline position
656% underline thickness
657%
658% This method is like GetTypeMetrics() but it returns the maximum text width
659% and height for multiple lines of text.
660%
661% The format of the GetMultilineTypeMetrics method is:
662%
663% MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000664% const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000665%
666% A description of each parameter follows:
667%
668% o image: the image.
669%
670% o draw_info: the draw info.
671%
672% o metrics: Return the font metrics in this structure.
673%
cristy5cbc0162011-08-29 00:36:28 +0000674% o exception: return any errors or warnings in this structure.
675%
cristy3ed852e2009-09-05 21:47:34 +0000676*/
677MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000678 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000679{
680 char
681 **textlist;
682
683 DrawInfo
684 *annotate_info;
685
686 MagickBooleanType
687 status;
688
cristybb503372010-05-27 20:51:26 +0000689 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000690 i;
691
692 TypeMetric
693 extent;
694
cristy3ed852e2009-09-05 21:47:34 +0000695 assert(image != (Image *) NULL);
696 assert(image->signature == MagickSignature);
697 if (image->debug != MagickFalse)
698 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
699 assert(draw_info != (DrawInfo *) NULL);
700 assert(draw_info->text != (char *) NULL);
701 assert(draw_info->signature == MagickSignature);
702 if (*draw_info->text == '\0')
703 return(MagickFalse);
704 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
705 annotate_info->text=DestroyString(annotate_info->text);
706 /*
707 Convert newlines to multiple lines of text.
708 */
709 textlist=StringToList(draw_info->text);
710 if (textlist == (char **) NULL)
711 return(MagickFalse);
712 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000713 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000714 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
715 (void) ResetMagickMemory(&extent,0,sizeof(extent));
716 /*
717 Find the widest of the text lines.
718 */
719 annotate_info->text=textlist[0];
cristy5cbc0162011-08-29 00:36:28 +0000720 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000721 *metrics=extent;
722 for (i=1; textlist[i] != (char *) NULL; i++)
723 {
724 annotate_info->text=textlist[i];
cristy5cbc0162011-08-29 00:36:28 +0000725 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000726 if (extent.width > metrics->width)
727 *metrics=extent;
728 }
cristye2c81ad2011-08-20 22:54:14 +0000729 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
730 (i-1)*draw_info->interline_spacing);
cristy3ed852e2009-09-05 21:47:34 +0000731 /*
732 Relinquish resources.
733 */
734 annotate_info->text=(char *) NULL;
735 annotate_info=DestroyDrawInfo(annotate_info);
736 for (i=0; textlist[i] != (char *) NULL; i++)
737 textlist[i]=DestroyString(textlist[i]);
738 textlist=(char **) RelinquishMagickMemory(textlist);
739 return(status);
740}
741
742/*
743%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
744% %
745% %
746% %
747% G e t T y p e M e t r i c s %
748% %
749% %
750% %
751%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752%
753% GetTypeMetrics() returns the following information for the specified font
754% and text:
755%
756% character width
757% character height
758% ascender
759% descender
760% text width
761% text height
762% maximum horizontal advance
763% bounds: x1
764% bounds: y1
765% bounds: x2
766% bounds: y2
767% origin: x
768% origin: y
769% underline position
770% underline thickness
771%
772% The format of the GetTypeMetrics method is:
773%
774% MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000775% TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000776%
777% A description of each parameter follows:
778%
779% o image: the image.
780%
781% o draw_info: the draw info.
782%
783% o metrics: Return the font metrics in this structure.
784%
cristy5cbc0162011-08-29 00:36:28 +0000785% o exception: return any errors or warnings in this structure.
786%
cristy3ed852e2009-09-05 21:47:34 +0000787*/
788MagickExport MagickBooleanType GetTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000789 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000790{
791 DrawInfo
792 *annotate_info;
793
794 MagickBooleanType
795 status;
796
797 PointInfo
798 offset;
799
800 assert(image != (Image *) NULL);
801 assert(image->signature == MagickSignature);
802 if (image->debug != MagickFalse)
803 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
804 assert(draw_info != (DrawInfo *) NULL);
805 assert(draw_info->text != (char *) NULL);
806 assert(draw_info->signature == MagickSignature);
807 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
808 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000809 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000810 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
811 offset.x=0.0;
812 offset.y=0.0;
cristy5cbc0162011-08-29 00:36:28 +0000813 status=RenderType(image,annotate_info,&offset,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000814 if (image->debug != MagickFalse)
815 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
cristye7f51092010-01-17 00:39:37 +0000816 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
cristya8549b12011-05-18 19:05:08 +0000817 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
cristye7f51092010-01-17 00:39:37 +0000818 "underline position: %g; underline thickness: %g",annotate_info->text,
cristy3ed852e2009-09-05 21:47:34 +0000819 metrics->width,metrics->height,metrics->ascent,metrics->descent,
820 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
cristy4c08aed2011-07-01 19:47:50 +0000821 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
822 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
cristy3ed852e2009-09-05 21:47:34 +0000823 metrics->underline_position,metrics->underline_thickness);
824 annotate_info=DestroyDrawInfo(annotate_info);
825 return(status);
826}
827
828/*
829%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830% %
831% %
832% %
833+ R e n d e r T y p e %
834% %
835% %
836% %
837%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838%
839% RenderType() renders text on the image. It also returns the bounding box of
840% the text relative to the image.
841%
842% The format of the RenderType method is:
843%
844% MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000845% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000846%
847% A description of each parameter follows:
848%
849% o image: the image.
850%
851% o draw_info: the draw info.
852%
853% o offset: (x,y) location of text relative to image.
854%
855% o metrics: bounding box of text.
856%
cristy5cbc0162011-08-29 00:36:28 +0000857% o exception: return any errors or warnings in this structure.
858%
cristy3ed852e2009-09-05 21:47:34 +0000859*/
860static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000861 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000862{
863 const TypeInfo
864 *type_info;
865
866 DrawInfo
867 *annotate_info;
868
869 MagickBooleanType
870 status;
871
872 type_info=(const TypeInfo *) NULL;
873 if (draw_info->font != (char *) NULL)
874 {
875 if (*draw_info->font == '@')
876 {
877 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000878 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000879 return(status);
880 }
881 if (*draw_info->font == '-')
cristy5cbc0162011-08-29 00:36:28 +0000882 return(RenderX11(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +0000883 if (IsPathAccessible(draw_info->font) != MagickFalse)
884 {
885 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000886 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000887 return(status);
888 }
cristy5cbc0162011-08-29 00:36:28 +0000889 type_info=GetTypeInfo(draw_info->font,exception);
cristy3ed852e2009-09-05 21:47:34 +0000890 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000891 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000892 "UnableToReadFont","`%s'",draw_info->font);
cristy3ed852e2009-09-05 21:47:34 +0000893 }
894 if ((type_info == (const TypeInfo *) NULL) &&
895 (draw_info->family != (const char *) NULL))
896 {
897 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000898 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000899 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000900 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000901 "UnableToReadFont","`%s'",draw_info->family);
cristy3ed852e2009-09-05 21:47:34 +0000902 }
903 if (type_info == (const TypeInfo *) NULL)
cristyd984f272010-03-16 01:46:12 +0000904 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000905 draw_info->stretch,draw_info->weight,exception);
cristyd984f272010-03-16 01:46:12 +0000906 if (type_info == (const TypeInfo *) NULL)
cristycbb34a42010-03-16 01:45:18 +0000907 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000908 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000909 if (type_info == (const TypeInfo *) NULL)
cristy704acaa2010-03-23 13:08:21 +0000910 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000911 draw_info->stretch,draw_info->weight,exception);
cristy704acaa2010-03-23 13:08:21 +0000912 if (type_info == (const TypeInfo *) NULL)
cristy6193b862011-08-06 16:33:59 +0000913 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000914 draw_info->stretch,draw_info->weight,exception);
cristy6193b862011-08-06 16:33:59 +0000915 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000916 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000917 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000918 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000919 type_info=GetTypeInfo("*",exception);
cristy704acaa2010-03-23 13:08:21 +0000920 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000921 {
cristy5cbc0162011-08-29 00:36:28 +0000922 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
923 exception);
cristy3ed852e2009-09-05 21:47:34 +0000924 return(status);
925 }
926 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
927 annotate_info->face=type_info->face;
928 if (type_info->metrics != (char *) NULL)
929 (void) CloneString(&annotate_info->metrics,type_info->metrics);
930 if (type_info->glyphs != (char *) NULL)
931 (void) CloneString(&annotate_info->font,type_info->glyphs);
cristy5cbc0162011-08-29 00:36:28 +0000932 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
933 exception);
cristy3ed852e2009-09-05 21:47:34 +0000934 annotate_info=DestroyDrawInfo(annotate_info);
935 return(status);
936}
937
938/*
939%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940% %
941% %
942% %
943+ R e n d e r F r e e t y p e %
944% %
945% %
946% %
947%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948%
949% RenderFreetype() renders text on the image with a Truetype font. It also
950% returns the bounding box of the text relative to the image.
951%
952% The format of the RenderFreetype method is:
953%
954% MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000955% const char *encoding,const PointInfo *offset,TypeMetric *metrics,
956% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000957%
958% A description of each parameter follows:
959%
960% o image: the image.
961%
962% o draw_info: the draw info.
963%
964% o encoding: the font encoding.
965%
966% o offset: (x,y) location of text relative to image.
967%
968% o metrics: bounding box of text.
969%
cristy5cbc0162011-08-29 00:36:28 +0000970% o exception: return any errors or warnings in this structure.
971%
cristy3ed852e2009-09-05 21:47:34 +0000972*/
973
974#if defined(MAGICKCORE_FREETYPE_DELEGATE)
cristy2857ffc2010-03-27 23:44:00 +0000975
cristy3ed852e2009-09-05 21:47:34 +0000976static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
977 DrawInfo *draw_info)
978{
979 AffineMatrix
980 affine;
981
982 char
983 path[MaxTextExtent];
984
985 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +0000986 (void) FormatLocaleString(path,MaxTextExtent,
cristya8549b12011-05-18 19:05:08 +0000987 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
cristy8cd5b312010-01-07 01:10:24 +0000988 p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
989 affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +0000990 (void) ConcatenateString(&draw_info->primitive,path);
991 return(0);
992}
993
994static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
995{
996 AffineMatrix
997 affine;
998
999 char
1000 path[MaxTextExtent];
1001
1002 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +00001003 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +00001004 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001005 (void) ConcatenateString(&draw_info->primitive,path);
1006 return(0);
1007}
1008
1009static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1010{
1011 AffineMatrix
1012 affine;
1013
1014 char
1015 path[MaxTextExtent];
1016
1017 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +00001018 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +00001019 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001020 (void) ConcatenateString(&draw_info->primitive,path);
1021 return(0);
1022}
1023
1024static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1025 DrawInfo *draw_info)
1026{
1027 AffineMatrix
1028 affine;
1029
1030 char
1031 path[MaxTextExtent];
1032
1033 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +00001034 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
cristy3ed852e2009-09-05 21:47:34 +00001035 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1036 affine.ty-to->y/64.0);
1037 (void) ConcatenateString(&draw_info->primitive,path);
1038 return(0);
1039}
1040
1041static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001042 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1043 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001044{
1045#if !defined(FT_OPEN_PATHNAME)
1046#define FT_OPEN_PATHNAME ft_open_pathname
1047#endif
1048
1049 typedef struct _GlyphInfo
1050 {
1051 FT_UInt
1052 id;
1053
1054 FT_Vector
1055 origin;
1056
1057 FT_Glyph
1058 image;
1059 } GlyphInfo;
1060
1061 const char
1062 *value;
1063
cristyc9b12952010-03-28 01:12:28 +00001064 double
1065 direction;
1066
cristy3ed852e2009-09-05 21:47:34 +00001067 DrawInfo
1068 *annotate_info;
1069
1070 FT_BBox
1071 bounds;
1072
1073 FT_BitmapGlyph
1074 bitmap;
1075
1076 FT_Encoding
1077 encoding_type;
1078
1079 FT_Error
cristyf8f295f2013-08-11 17:26:49 +00001080 ft_status;
cristy3ed852e2009-09-05 21:47:34 +00001081
1082 FT_Face
1083 face;
1084
1085 FT_Int32
1086 flags;
1087
1088 FT_Library
1089 library;
1090
1091 FT_Matrix
1092 affine;
1093
1094 FT_Open_Args
1095 args;
1096
1097 FT_Vector
1098 origin;
1099
1100 GlyphInfo
1101 glyph,
1102 last_glyph;
1103
cristyf8f295f2013-08-11 17:26:49 +00001104 MagickBooleanType
1105 status;
1106
cristy3ed852e2009-09-05 21:47:34 +00001107 PointInfo
1108 point,
1109 resolution;
1110
1111 register char
1112 *p;
1113
cristy9d314ff2011-03-09 01:30:28 +00001114 ssize_t
1115 code,
1116 y;
1117
cristy3ed852e2009-09-05 21:47:34 +00001118 static FT_Outline_Funcs
1119 OutlineMethods =
1120 {
1121 (FT_Outline_MoveTo_Func) TraceMoveTo,
1122 (FT_Outline_LineTo_Func) TraceLineTo,
1123 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1124 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1125 0, 0
1126 };
1127
cristy2857ffc2010-03-27 23:44:00 +00001128 unsigned char
1129 *utf8;
1130
cristy3ed852e2009-09-05 21:47:34 +00001131 /*
1132 Initialize Truetype library.
1133 */
cristyf8f295f2013-08-11 17:26:49 +00001134 ft_status=FT_Init_FreeType(&library);
1135 if (ft_status != 0)
cristy3ed852e2009-09-05 21:47:34 +00001136 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1137 image->filename);
1138 args.flags=FT_OPEN_PATHNAME;
1139 if (draw_info->font == (char *) NULL)
1140 args.pathname=ConstantString("helvetica");
1141 else
1142 if (*draw_info->font != '@')
1143 args.pathname=ConstantString(draw_info->font);
1144 else
1145 args.pathname=ConstantString(draw_info->font+1);
1146 face=(FT_Face) NULL;
cristyf8f295f2013-08-11 17:26:49 +00001147 ft_status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
cristy3ed852e2009-09-05 21:47:34 +00001148 args.pathname=DestroyString(args.pathname);
cristyf8f295f2013-08-11 17:26:49 +00001149 if (ft_status != 0)
cristy3ed852e2009-09-05 21:47:34 +00001150 {
1151 (void) FT_Done_FreeType(library);
cristy5cbc0162011-08-29 00:36:28 +00001152 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
cristyefe601c2013-01-05 17:51:12 +00001153 "UnableToReadFont","`%s'",draw_info->font);
cristy5cbc0162011-08-29 00:36:28 +00001154 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001155 }
1156 if ((draw_info->metrics != (char *) NULL) &&
1157 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1158 (void) FT_Attach_File(face,draw_info->metrics);
1159 encoding_type=ft_encoding_unicode;
cristyf8f295f2013-08-11 17:26:49 +00001160 ft_status=FT_Select_Charmap(face,encoding_type);
1161 if ((ft_status != 0) && (face->num_charmaps != 0))
1162 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
cristy3ed852e2009-09-05 21:47:34 +00001163 if (encoding != (const char *) NULL)
1164 {
1165 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1166 encoding_type=ft_encoding_adobe_custom;
1167 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1168 encoding_type=ft_encoding_adobe_expert;
1169 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1170 encoding_type=ft_encoding_adobe_standard;
1171 if (LocaleCompare(encoding,"AppleRoman") == 0)
1172 encoding_type=ft_encoding_apple_roman;
1173 if (LocaleCompare(encoding,"BIG5") == 0)
1174 encoding_type=ft_encoding_big5;
1175 if (LocaleCompare(encoding,"GB2312") == 0)
1176 encoding_type=ft_encoding_gb2312;
1177 if (LocaleCompare(encoding,"Johab") == 0)
1178 encoding_type=ft_encoding_johab;
1179#if defined(ft_encoding_latin_1)
1180 if (LocaleCompare(encoding,"Latin-1") == 0)
1181 encoding_type=ft_encoding_latin_1;
1182#endif
1183 if (LocaleCompare(encoding,"Latin-2") == 0)
1184 encoding_type=ft_encoding_latin_2;
1185 if (LocaleCompare(encoding,"None") == 0)
1186 encoding_type=ft_encoding_none;
1187 if (LocaleCompare(encoding,"SJIScode") == 0)
1188 encoding_type=ft_encoding_sjis;
1189 if (LocaleCompare(encoding,"Symbol") == 0)
1190 encoding_type=ft_encoding_symbol;
1191 if (LocaleCompare(encoding,"Unicode") == 0)
1192 encoding_type=ft_encoding_unicode;
1193 if (LocaleCompare(encoding,"Wansung") == 0)
1194 encoding_type=ft_encoding_wansung;
cristyf8f295f2013-08-11 17:26:49 +00001195 ft_status=FT_Select_Charmap(face,encoding_type);
1196 if (ft_status != 0)
cristye3b2bb82014-02-15 13:20:08 +00001197 {
1198 (void) FT_Done_Face(face);
1199 (void) FT_Done_FreeType(library);
1200 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1201 }
cristy3ed852e2009-09-05 21:47:34 +00001202 }
1203 /*
1204 Set text size.
1205 */
1206 resolution.x=DefaultResolution;
1207 resolution.y=DefaultResolution;
1208 if (draw_info->density != (char *) NULL)
1209 {
1210 GeometryInfo
1211 geometry_info;
1212
1213 MagickStatusType
1214 flags;
1215
1216 flags=ParseGeometry(draw_info->density,&geometry_info);
1217 resolution.x=geometry_info.rho;
1218 resolution.y=geometry_info.sigma;
1219 if ((flags & SigmaValue) == 0)
1220 resolution.y=resolution.x;
1221 }
cristyf8f295f2013-08-11 17:26:49 +00001222 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
cristy3ed852e2009-09-05 21:47:34 +00001223 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1224 (FT_UInt) resolution.y);
1225 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1226 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1227 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1228 metrics->descent=(double) face->size->metrics.descender/64.0;
1229 metrics->width=0;
1230 metrics->origin.x=0;
1231 metrics->origin.y=0;
1232 metrics->height=(double) face->size->metrics.height/64.0;
1233 metrics->max_advance=0.0;
1234 if (face->size->metrics.max_advance > MagickEpsilon)
1235 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1236 metrics->bounds.x1=0.0;
1237 metrics->bounds.y1=metrics->descent;
1238 metrics->bounds.x2=metrics->ascent+metrics->descent;
1239 metrics->bounds.y2=metrics->ascent+metrics->descent;
1240 metrics->underline_position=face->underline_position/64.0;
1241 metrics->underline_thickness=face->underline_thickness/64.0;
cristy71709bf2011-09-25 13:10:29 +00001242 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
cristy3ed852e2009-09-05 21:47:34 +00001243 {
1244 (void) FT_Done_Face(face);
1245 (void) FT_Done_FreeType(library);
1246 return(MagickTrue);
1247 }
1248 /*
1249 Compute bounding box.
1250 */
1251 if (image->debug != MagickFalse)
1252 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
cristye7f51092010-01-17 00:39:37 +00001253 "font-encoding %s; text-encoding %s; pointsize %g",
cristy3ed852e2009-09-05 21:47:34 +00001254 draw_info->font != (char *) NULL ? draw_info->font : "none",
1255 encoding != (char *) NULL ? encoding : "none",
1256 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1257 draw_info->pointsize);
1258 flags=FT_LOAD_NO_BITMAP;
cristy55a804e2012-01-13 17:46:11 +00001259 if (draw_info->text_antialias == MagickFalse)
cristy4f9709e2012-06-10 01:54:51 +00001260 flags|=FT_LOAD_TARGET_MONO;
cristy55a804e2012-01-13 17:46:11 +00001261 else
1262 {
cristya89189d2012-01-13 17:54:57 +00001263#if defined(FT_LOAD_TARGET_LIGHT)
cristy55a804e2012-01-13 17:46:11 +00001264 flags|=FT_LOAD_TARGET_LIGHT;
cristya89189d2012-01-13 17:54:57 +00001265#elif defined(FT_LOAD_TARGET_LCD)
1266 flags|=FT_LOAD_TARGET_LCD;
cristy55a804e2012-01-13 17:46:11 +00001267#endif
1268 }
cristyd15e6592011-10-15 00:13:06 +00001269 value=GetImageProperty(image,"type:hinting",exception);
cristy33204242010-10-07 23:17:12 +00001270 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
cristy3ed852e2009-09-05 21:47:34 +00001271 flags|=FT_LOAD_NO_HINTING;
1272 glyph.id=0;
1273 glyph.image=NULL;
1274 last_glyph.id=0;
1275 last_glyph.image=NULL;
1276 origin.x=0;
1277 origin.y=0;
1278 affine.xx=65536L;
1279 affine.yx=0L;
1280 affine.xy=0L;
1281 affine.yy=65536L;
1282 if (draw_info->render != MagickFalse)
1283 {
1284 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1285 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1286 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1287 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1288 }
1289 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1290 (void) CloneString(&annotate_info->primitive,"path '");
1291 if (draw_info->render != MagickFalse)
1292 {
1293 if (image->storage_class != DirectClass)
cristy5cbc0162011-08-29 00:36:28 +00001294 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a46d822012-08-28 23:32:39 +00001295 if (image->alpha_trait != BlendPixelTrait)
cristy5cbc0162011-08-29 00:36:28 +00001296 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001297 }
cristyc9b12952010-03-28 01:12:28 +00001298 direction=1.0;
cristycf5467b2010-03-28 16:22:06 +00001299 if (draw_info->direction == RightToLeftDirection)
cristyc9b12952010-03-28 01:12:28 +00001300 direction=(-1.0);
cristy3ed852e2009-09-05 21:47:34 +00001301 point.x=0.0;
1302 point.y=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001303 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
cristybd512462010-02-13 02:13:14 +00001304 if (GetUTFCode(p) < 0)
1305 break;
cristy2857ffc2010-03-27 23:44:00 +00001306 utf8=(unsigned char *) NULL;
cristyd913ea12010-02-13 02:32:57 +00001307 if (GetUTFCode(p) == 0)
cristy2857ffc2010-03-27 23:44:00 +00001308 p=draw_info->text;
cristybd512462010-02-13 02:13:14 +00001309 else
cristybd512462010-02-13 02:13:14 +00001310 {
cristy2857ffc2010-03-27 23:44:00 +00001311 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1312 if (utf8 != (unsigned char *) NULL)
cristyc9b12952010-03-28 01:12:28 +00001313 p=(char *) utf8;
cristy2857ffc2010-03-27 23:44:00 +00001314 }
cristyf8f295f2013-08-11 17:26:49 +00001315 status=MagickTrue;
cristy2857ffc2010-03-27 23:44:00 +00001316 for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1317 {
1318 /*
1319 Render UTF-8 sequence.
1320 */
1321 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1322 if (glyph.id == 0)
1323 glyph.id=FT_Get_Char_Index(face,'?');
1324 if ((glyph.id != 0) && (last_glyph.id != 0))
1325 {
cristyaa83c2c2011-09-21 13:36:25 +00001326 if (fabs(draw_info->kerning) >= MagickEpsilon)
cristy94b11832011-09-08 19:46:03 +00001327 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
cristy2857ffc2010-03-27 23:44:00 +00001328 else
1329 if (FT_HAS_KERNING(face))
1330 {
1331 FT_Vector
1332 kerning;
cristybd512462010-02-13 02:13:14 +00001333
cristyf8f295f2013-08-11 17:26:49 +00001334 ft_status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
cristy2857ffc2010-03-27 23:44:00 +00001335 ft_kerning_default,&kerning);
cristyf8f295f2013-08-11 17:26:49 +00001336 if (ft_status == 0)
cristy94b11832011-09-08 19:46:03 +00001337 origin.x+=(FT_Pos) (direction*kerning.x);
cristy2857ffc2010-03-27 23:44:00 +00001338 }
1339 }
1340 glyph.origin=origin;
cristyf8f295f2013-08-11 17:26:49 +00001341 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1342 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001343 continue;
cristyf8f295f2013-08-11 17:26:49 +00001344 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1345 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001346 continue;
cristyf8f295f2013-08-11 17:26:49 +00001347 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
cristy2857ffc2010-03-27 23:44:00 +00001348 &bounds);
cristyf8f295f2013-08-11 17:26:49 +00001349 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001350 continue;
1351 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
cristyaa83c2c2011-09-21 13:36:25 +00001352 metrics->bounds.x1=(double) bounds.xMin;
cristy2857ffc2010-03-27 23:44:00 +00001353 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
cristyaa83c2c2011-09-21 13:36:25 +00001354 metrics->bounds.y1=(double) bounds.yMin;
cristy2857ffc2010-03-27 23:44:00 +00001355 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
cristyaa83c2c2011-09-21 13:36:25 +00001356 metrics->bounds.x2=(double) bounds.xMax;
cristy2857ffc2010-03-27 23:44:00 +00001357 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
cristyaa83c2c2011-09-21 13:36:25 +00001358 metrics->bounds.y2=(double) bounds.yMax;
cristyf8f295f2013-08-11 17:26:49 +00001359 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1360 (draw_info->stroke_pattern != (Image *) NULL))
1361 {
1362 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
1363 {
1364 /*
1365 Trace the glyph.
1366 */
1367 annotate_info->affine.tx=glyph.origin.x/64.0;
1368 annotate_info->affine.ty=glyph.origin.y/64.0;
1369 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1370 outline,&OutlineMethods,annotate_info);
1371 }
cristy2857ffc2010-03-27 23:44:00 +00001372 }
1373 FT_Vector_Transform(&glyph.origin,&affine);
1374 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
cristyf8f295f2013-08-11 17:26:49 +00001375 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
cristy2857ffc2010-03-27 23:44:00 +00001376 (FT_Vector *) NULL,MagickTrue);
cristyf8f295f2013-08-11 17:26:49 +00001377 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001378 continue;
1379 bitmap=(FT_BitmapGlyph) glyph.image;
1380 point.x=offset->x+bitmap->left;
cristy2f5b5c72011-09-16 16:52:00 +00001381 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1382 point.x=offset->x+(origin.x >> 6);
cristy2857ffc2010-03-27 23:44:00 +00001383 point.y=offset->y-bitmap->top;
1384 if (draw_info->render != MagickFalse)
1385 {
1386 CacheView
1387 *image_view;
cristybd512462010-02-13 02:13:14 +00001388
cristy2f5b5c72011-09-16 16:52:00 +00001389 register unsigned char
1390 *p;
1391
cristy2857ffc2010-03-27 23:44:00 +00001392 /*
1393 Rasterize the glyph.
1394 */
cristy46ff2672012-12-14 15:32:26 +00001395 image_view=AcquireAuthenticCacheView(image,exception);
cristy2f5b5c72011-09-16 16:52:00 +00001396 p=bitmap->bitmap.buffer;
cristybb503372010-05-27 20:51:26 +00001397 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
cristy2857ffc2010-03-27 23:44:00 +00001398 {
cristybd512462010-02-13 02:13:14 +00001399 MagickBooleanType
cristy2857ffc2010-03-27 23:44:00 +00001400 active,
1401 sync;
cristybd512462010-02-13 02:13:14 +00001402
cristya19f1d72012-08-07 18:24:38 +00001403 double
cristy2857ffc2010-03-27 23:44:00 +00001404 fill_opacity;
cristybd512462010-02-13 02:13:14 +00001405
cristy101ab702011-10-13 13:06:32 +00001406 PixelInfo
cristy2857ffc2010-03-27 23:44:00 +00001407 fill_color;
cristybd512462010-02-13 02:13:14 +00001408
cristy4c08aed2011-07-01 19:47:50 +00001409 register Quantum
cristy2857ffc2010-03-27 23:44:00 +00001410 *restrict q;
cristybd512462010-02-13 02:13:14 +00001411
cristy88158062011-06-09 00:31:43 +00001412 register ssize_t
1413 x;
1414
cristy73ce31b2010-06-13 23:17:39 +00001415 ssize_t
cristy2f5b5c72011-09-16 16:52:00 +00001416 n,
cristy73ce31b2010-06-13 23:17:39 +00001417 x_offset,
1418 y_offset;
1419
cristy2857ffc2010-03-27 23:44:00 +00001420 if (status == MagickFalse)
1421 continue;
cristybb503372010-05-27 20:51:26 +00001422 x_offset=(ssize_t) ceil(point.x-0.5);
1423 y_offset=(ssize_t) ceil(point.y+y-0.5);
1424 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy2857ffc2010-03-27 23:44:00 +00001425 continue;
cristy4c08aed2011-07-01 19:47:50 +00001426 q=(Quantum *) NULL;
cristybb503372010-05-27 20:51:26 +00001427 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001428 active=MagickFalse;
1429 else
cristybd512462010-02-13 02:13:14 +00001430 {
cristy2857ffc2010-03-27 23:44:00 +00001431 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1432 bitmap->bitmap.width,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001433 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
cristybd512462010-02-13 02:13:14 +00001434 }
cristy854209a2011-09-19 17:02:57 +00001435 n=y*bitmap->bitmap.pitch-1;
1436 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
cristy2857ffc2010-03-27 23:44:00 +00001437 {
cristy854209a2011-09-19 17:02:57 +00001438 n++;
cristy2857ffc2010-03-27 23:44:00 +00001439 x_offset++;
cristy2f5b5c72011-09-16 16:52:00 +00001440 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001441 {
cristyed231572011-07-14 02:18:59 +00001442 q+=GetPixelChannels(image);
cristy2857ffc2010-03-27 23:44:00 +00001443 continue;
1444 }
cristy2f5b5c72011-09-16 16:52:00 +00001445 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
cristya19f1d72012-08-07 18:24:38 +00001446 fill_opacity=(double) (p[n])/(bitmap->bitmap.num_grays-1);
cristy2f5b5c72011-09-16 16:52:00 +00001447 else
1448 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1449 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
cristy2857ffc2010-03-27 23:44:00 +00001450 if (draw_info->text_antialias == MagickFalse)
1451 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1452 if (active == MagickFalse)
1453 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1454 exception);
cristy6193b862011-08-06 16:33:59 +00001455 if (q == (Quantum *) NULL)
cristy9c5171c2014-05-25 21:26:39 +00001456 continue;
cristyec061ec2011-10-21 17:41:17 +00001457 GetPixelInfo(image,&fill_color);
cristy2ed42f62011-10-02 19:49:57 +00001458 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1459 exception);
cristy4c08aed2011-07-01 19:47:50 +00001460 fill_opacity=fill_opacity*fill_color.alpha;
1461 CompositePixelOver(image,&fill_color,fill_opacity,q,
1462 GetPixelAlpha(image,q),q);
cristy2857ffc2010-03-27 23:44:00 +00001463 if (active == MagickFalse)
1464 {
1465 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1466 if (sync == MagickFalse)
1467 status=MagickFalse;
1468 }
cristyed231572011-07-14 02:18:59 +00001469 q+=GetPixelChannels(image);
cristybd512462010-02-13 02:13:14 +00001470 }
cristy2857ffc2010-03-27 23:44:00 +00001471 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1472 if (sync == MagickFalse)
1473 status=MagickFalse;
cristybd512462010-02-13 02:13:14 +00001474 }
cristy2857ffc2010-03-27 23:44:00 +00001475 image_view=DestroyCacheView(image_view);
1476 }
1477 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1478 metrics->width=bitmap->left+bitmap->bitmap.width;
cristyaa83c2c2011-09-21 13:36:25 +00001479 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
cristy2857ffc2010-03-27 23:44:00 +00001480 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1481 (IsUTFSpace(code) == MagickFalse))
cristy94b11832011-09-08 19:46:03 +00001482 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
cristy2857ffc2010-03-27 23:44:00 +00001483 else
cristy94b11832011-09-08 19:46:03 +00001484 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
cristyaa83c2c2011-09-21 13:36:25 +00001485 metrics->origin.x=(double) origin.x;
1486 metrics->origin.y=(double) origin.y;
cristybd5a96c2011-08-21 00:04:26 +00001487 if (last_glyph.id != 0)
1488 FT_Done_Glyph(last_glyph.image);
cristy2857ffc2010-03-27 23:44:00 +00001489 last_glyph=glyph;
1490 code=GetUTFCode(p);
1491 }
1492 if (utf8 != (unsigned char *) NULL)
1493 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
cristybd5a96c2011-08-21 00:04:26 +00001494 if (last_glyph.id != 0)
1495 FT_Done_Glyph(last_glyph.image);
cristy4c08aed2011-07-01 19:47:50 +00001496 if ((draw_info->stroke.alpha != TransparentAlpha) ||
cristy3ed852e2009-09-05 21:47:34 +00001497 (draw_info->stroke_pattern != (Image *) NULL))
1498 {
cristyf8f295f2013-08-11 17:26:49 +00001499 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001500 {
1501 /*
1502 Draw text stroke.
1503 */
1504 annotate_info->linejoin=RoundJoin;
1505 annotate_info->affine.tx=offset->x;
1506 annotate_info->affine.ty=offset->y;
1507 (void) ConcatenateString(&annotate_info->primitive,"'");
cristy018f07f2011-09-04 21:15:19 +00001508 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001509 }
1510 }
1511 /*
1512 Determine font metrics.
1513 */
1514 glyph.id=FT_Get_Char_Index(face,'_');
1515 glyph.origin=origin;
cristyf8f295f2013-08-11 17:26:49 +00001516 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1517 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001518 {
cristyf8f295f2013-08-11 17:26:49 +00001519 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1520 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001521 {
cristyf8f295f2013-08-11 17:26:49 +00001522 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1523 outline,&bounds);
1524 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001525 {
1526 FT_Vector_Transform(&glyph.origin,&affine);
1527 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
cristyf8f295f2013-08-11 17:26:49 +00001528 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
cristy3ed852e2009-09-05 21:47:34 +00001529 (FT_Vector *) NULL,MagickTrue);
1530 bitmap=(FT_BitmapGlyph) glyph.image;
1531 if (bitmap->left > metrics->width)
1532 metrics->width=bitmap->left;
1533 }
1534 }
cristye2c81ad2011-08-20 22:54:14 +00001535 FT_Done_Glyph(glyph.image);
cristy3ed852e2009-09-05 21:47:34 +00001536 }
1537 metrics->width-=metrics->bounds.x1/64.0;
cristy55cb9f12012-08-23 09:02:28 +00001538 metrics->width+=annotate_info->stroke_width;
cristy3ed852e2009-09-05 21:47:34 +00001539 metrics->bounds.x1/=64.0;
1540 metrics->bounds.y1/=64.0;
1541 metrics->bounds.x2/=64.0;
1542 metrics->bounds.y2/=64.0;
1543 metrics->origin.x/=64.0;
1544 metrics->origin.y/=64.0;
1545 /*
1546 Relinquish resources.
1547 */
1548 annotate_info=DestroyDrawInfo(annotate_info);
1549 (void) FT_Done_Face(face);
1550 (void) FT_Done_FreeType(library);
cristyf8f295f2013-08-11 17:26:49 +00001551 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001552}
1553#else
1554static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1555 const char *magick_unused(encoding),const PointInfo *offset,
cristy5cbc0162011-08-29 00:36:28 +00001556 TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001557{
cristy5cbc0162011-08-29 00:36:28 +00001558 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001559 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
cristyee081822011-02-02 19:07:30 +00001560 draw_info->font != (char *) NULL ? draw_info->font : "none");
cristye3b062a2011-12-17 18:34:52 +00001561 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001562}
1563#endif
1564
1565/*
1566%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1567% %
1568% %
1569% %
1570+ R e n d e r P o s t s c r i p t %
1571% %
1572% %
1573% %
1574%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1575%
1576% RenderPostscript() renders text on the image with a Postscript font. It
1577% also returns the bounding box of the text relative to the image.
1578%
1579% The format of the RenderPostscript method is:
1580%
1581% MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001582% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001583%
1584% A description of each parameter follows:
1585%
1586% o image: the image.
1587%
1588% o draw_info: the draw info.
1589%
1590% o offset: (x,y) location of text relative to image.
1591%
1592% o metrics: bounding box of text.
1593%
cristy5cbc0162011-08-29 00:36:28 +00001594% o exception: return any errors or warnings in this structure.
1595%
cristy3ed852e2009-09-05 21:47:34 +00001596*/
1597
1598static inline size_t MagickMin(const size_t x,const size_t y)
1599{
1600 if (x < y)
1601 return(x);
1602 return(y);
1603}
1604
1605static char *EscapeParenthesis(const char *text)
1606{
1607 char
1608 *buffer;
1609
1610 register char
1611 *p;
1612
cristybb503372010-05-27 20:51:26 +00001613 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001614 i;
1615
1616 size_t
1617 escapes;
1618
1619 escapes=0;
1620 buffer=AcquireString(text);
1621 p=buffer;
cristybb503372010-05-27 20:51:26 +00001622 for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
cristy3ed852e2009-09-05 21:47:34 +00001623 {
1624 if ((text[i] == '(') || (text[i] == ')'))
1625 {
1626 *p++='\\';
1627 escapes++;
1628 }
1629 *p++=text[i];
1630 }
1631 *p='\0';
1632 return(buffer);
1633}
1634
1635static MagickBooleanType RenderPostscript(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00001636 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1637 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001638{
1639 char
1640 filename[MaxTextExtent],
1641 geometry[MaxTextExtent],
1642 *text;
1643
1644 FILE
1645 *file;
1646
1647 Image
1648 *annotate_image;
1649
1650 ImageInfo
1651 *annotate_info;
1652
1653 int
1654 unique_file;
1655
cristy3ed852e2009-09-05 21:47:34 +00001656 MagickBooleanType
1657 identity;
1658
1659 PointInfo
1660 extent,
1661 point,
1662 resolution;
1663
cristybb503372010-05-27 20:51:26 +00001664 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001665 i;
1666
cristy14d71292012-05-20 16:48:13 +00001667 size_t
1668 length;
1669
cristy9d314ff2011-03-09 01:30:28 +00001670 ssize_t
1671 y;
1672
cristy3ed852e2009-09-05 21:47:34 +00001673 /*
1674 Render label with a Postscript font.
1675 */
1676 if (image->debug != MagickFalse)
1677 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +00001678 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
cristy3ed852e2009-09-05 21:47:34 +00001679 draw_info->font : "none",draw_info->pointsize);
1680 file=(FILE *) NULL;
1681 unique_file=AcquireUniqueFileResource(filename);
1682 if (unique_file != -1)
1683 file=fdopen(unique_file,"wb");
1684 if ((unique_file == -1) || (file == (FILE *) NULL))
1685 {
cristy5cbc0162011-08-29 00:36:28 +00001686 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
cristy3ed852e2009-09-05 21:47:34 +00001687 return(MagickFalse);
1688 }
cristyb51dff52011-05-19 16:55:47 +00001689 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1690 (void) FormatLocaleFile(file,"/ReencodeType\n");
1691 (void) FormatLocaleFile(file,"{\n");
1692 (void) FormatLocaleFile(file," findfont dup length\n");
1693 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001694 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
cristyb51dff52011-05-19 16:55:47 +00001695 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001696 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
cristyb51dff52011-05-19 16:55:47 +00001697 (void) FormatLocaleFile(file,"} bind def\n");
cristy3ed852e2009-09-05 21:47:34 +00001698 /*
1699 Sample to compute bounding box.
1700 */
cristyaa83c2c2011-09-21 13:36:25 +00001701 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1702 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1703 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001704 extent.x=0.0;
1705 extent.y=0.0;
cristy14d71292012-05-20 16:48:13 +00001706 length=strlen(draw_info->text);
1707 for (i=0; i <= (ssize_t) (length+2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001708 {
1709 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1710 draw_info->affine.ry*2.0*draw_info->pointsize);
1711 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1712 draw_info->affine.sy*2.0*draw_info->pointsize);
1713 if (point.x > extent.x)
1714 extent.x=point.x;
1715 if (point.y > extent.y)
1716 extent.y=point.y;
1717 }
cristyb51dff52011-05-19 16:55:47 +00001718 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
cristy3ed852e2009-09-05 21:47:34 +00001719 extent.x/2.0,extent.y/2.0);
cristyb51dff52011-05-19 16:55:47 +00001720 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
cristy3ed852e2009-09-05 21:47:34 +00001721 draw_info->pointsize);
1722 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1723 (strchr(draw_info->font,'/') != (char *) NULL))
cristyb51dff52011-05-19 16:55:47 +00001724 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001725 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1726 else
cristy1e604812011-05-19 18:07:50 +00001727 (void) FormatLocaleFile(file,
1728 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1729 draw_info->font);
cristyb51dff52011-05-19 16:55:47 +00001730 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
cristy8cd5b312010-01-07 01:10:24 +00001731 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1732 draw_info->affine.sy);
cristy3ed852e2009-09-05 21:47:34 +00001733 text=EscapeParenthesis(draw_info->text);
1734 if (identity == MagickFalse)
cristy1e604812011-05-19 18:07:50 +00001735 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1736 text);
cristyb51dff52011-05-19 16:55:47 +00001737 (void) FormatLocaleFile(file,"(%s) show\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001738 text=DestroyString(text);
cristyb51dff52011-05-19 16:55:47 +00001739 (void) FormatLocaleFile(file,"showpage\n");
cristy3ed852e2009-09-05 21:47:34 +00001740 (void) fclose(file);
cristyb51dff52011-05-19 16:55:47 +00001741 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
cristye8c25f92010-06-03 00:53:06 +00001742 floor(extent.x+0.5),floor(extent.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001743 annotate_info=AcquireImageInfo();
cristyb51dff52011-05-19 16:55:47 +00001744 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
cristy3ed852e2009-09-05 21:47:34 +00001745 filename);
1746 (void) CloneString(&annotate_info->page,geometry);
1747 if (draw_info->density != (char *) NULL)
1748 (void) CloneString(&annotate_info->density,draw_info->density);
1749 annotate_info->antialias=draw_info->text_antialias;
cristy5cbc0162011-08-29 00:36:28 +00001750 annotate_image=ReadImage(annotate_info,exception);
1751 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00001752 annotate_info=DestroyImageInfo(annotate_info);
1753 (void) RelinquishUniqueFileResource(filename);
1754 if (annotate_image == (Image *) NULL)
1755 return(MagickFalse);
cristy55cb9f12012-08-23 09:02:28 +00001756 (void) NegateImage(annotate_image,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001757 resolution.x=DefaultResolution;
1758 resolution.y=DefaultResolution;
1759 if (draw_info->density != (char *) NULL)
1760 {
1761 GeometryInfo
1762 geometry_info;
1763
1764 MagickStatusType
1765 flags;
1766
1767 flags=ParseGeometry(draw_info->density,&geometry_info);
1768 resolution.x=geometry_info.rho;
1769 resolution.y=geometry_info.sigma;
1770 if ((flags & SigmaValue) == 0)
1771 resolution.y=resolution.x;
1772 }
1773 if (identity == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001774 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001775 else
1776 {
1777 RectangleInfo
1778 crop_info;
1779
cristy5cbc0162011-08-29 00:36:28 +00001780 crop_info=GetImageBoundingBox(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001781 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
cristy3ed852e2009-09-05 21:47:34 +00001782 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
cristybb503372010-05-27 20:51:26 +00001783 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
cristy06609ee2010-03-17 20:21:27 +00001784 0.5);
cristyb51dff52011-05-19 16:55:47 +00001785 (void) FormatLocaleString(geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00001786 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
cristye8c25f92010-06-03 00:53:06 +00001787 crop_info.height,(double) crop_info.x,(double) crop_info.y);
cristye941a752011-10-15 01:52:48 +00001788 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001789 }
1790 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1791 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1792 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1793 metrics->ascent=metrics->pixels_per_em.x;
1794 metrics->descent=metrics->pixels_per_em.y/-5.0;
1795 metrics->width=(double) annotate_image->columns/
1796 ExpandAffine(&draw_info->affine);
1797 metrics->height=1.152*metrics->pixels_per_em.x;
1798 metrics->max_advance=metrics->pixels_per_em.x;
1799 metrics->bounds.x1=0.0;
1800 metrics->bounds.y1=metrics->descent;
1801 metrics->bounds.x2=metrics->ascent+metrics->descent;
1802 metrics->bounds.y2=metrics->ascent+metrics->descent;
1803 metrics->underline_position=(-2.0);
1804 metrics->underline_thickness=1.0;
1805 if (draw_info->render == MagickFalse)
1806 {
1807 annotate_image=DestroyImage(annotate_image);
1808 return(MagickTrue);
1809 }
cristy4c08aed2011-07-01 19:47:50 +00001810 if (draw_info->fill.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001811 {
cristybd659482011-10-09 23:19:32 +00001812 CacheView
1813 *annotate_view;
1814
cristy3ed852e2009-09-05 21:47:34 +00001815 MagickBooleanType
1816 sync;
1817
cristy101ab702011-10-13 13:06:32 +00001818 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001819 fill_color;
1820
cristy3ed852e2009-09-05 21:47:34 +00001821 /*
1822 Render fill color.
1823 */
cristy8a46d822012-08-28 23:32:39 +00001824 if (image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001825 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy8a46d822012-08-28 23:32:39 +00001826 if (annotate_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001827 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1828 exception);
1829 fill_color=draw_info->fill;
cristy46ff2672012-12-14 15:32:26 +00001830 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001831 for (y=0; y < (ssize_t) annotate_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001832 {
cristybb503372010-05-27 20:51:26 +00001833 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001834 x;
1835
cristy4c08aed2011-07-01 19:47:50 +00001836 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001837 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001838
1839 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1840 1,exception);
cristy6193b862011-08-06 16:33:59 +00001841 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001842 break;
cristybb503372010-05-27 20:51:26 +00001843 for (x=0; x < (ssize_t) annotate_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001844 {
cristy2ed42f62011-10-02 19:49:57 +00001845 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy55cb9f12012-08-23 09:02:28 +00001846 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1847 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
cristy4c08aed2011-07-01 19:47:50 +00001848 SetPixelRed(annotate_image,fill_color.red,q);
1849 SetPixelGreen(annotate_image,fill_color.green,q);
1850 SetPixelBlue(annotate_image,fill_color.blue,q);
cristyed231572011-07-14 02:18:59 +00001851 q+=GetPixelChannels(annotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001852 }
1853 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1854 if (sync == MagickFalse)
1855 break;
1856 }
1857 annotate_view=DestroyCacheView(annotate_view);
cristy39172402012-03-30 13:04:39 +00001858 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
cristybb503372010-05-27 20:51:26 +00001859 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
cristye941a752011-10-15 01:52:48 +00001860 metrics->descent)-0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00001861 }
1862 annotate_image=DestroyImage(annotate_image);
1863 return(MagickTrue);
1864}
1865
1866/*
1867%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1868% %
1869% %
1870% %
1871+ R e n d e r X 1 1 %
1872% %
1873% %
1874% %
1875%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1876%
1877% RenderX11() renders text on the image with an X11 font. It also returns the
1878% bounding box of the text relative to the image.
1879%
1880% The format of the RenderX11 method is:
1881%
1882% MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001883% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001884%
1885% A description of each parameter follows:
1886%
1887% o image: the image.
1888%
1889% o draw_info: the draw info.
1890%
1891% o offset: (x,y) location of text relative to image.
1892%
1893% o metrics: bounding box of text.
1894%
cristy5cbc0162011-08-29 00:36:28 +00001895% o exception: return any errors or warnings in this structure.
1896%
cristy3ed852e2009-09-05 21:47:34 +00001897*/
cristycc168c22013-04-18 14:42:31 +00001898static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1899 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001900{
1901 MagickBooleanType
1902 status;
1903
cristyd7ecaca2011-01-24 14:14:53 +00001904 if (annotate_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +00001905 ActivateSemaphoreInfo(&annotate_semaphore);
cristyd7ecaca2011-01-24 14:14:53 +00001906 LockSemaphoreInfo(annotate_semaphore);
cristy534d0a32013-04-13 16:27:24 +00001907 status=XRenderImage(image,draw_info,offset,metrics,exception);
cristyd7ecaca2011-01-24 14:14:53 +00001908 UnlockSemaphoreInfo(annotate_semaphore);
cristy534d0a32013-04-13 16:27:24 +00001909 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001910}