blob: 0a985f2edb3a5afa1689b6ccb30ef762caa70e04 [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{
cristy3d162a92014-02-16 14:05:06 +0000148 annotate_semaphore=AcquireSemaphoreInfo();
cristyd7ecaca2011-01-24 14:14:53 +0000149 return(MagickTrue);
150}
151
152/*
153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154% %
155% %
156% %
157+ 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 %
158% %
159% %
160% %
161%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162%
163% AnnotateComponentTerminus() destroys the annotate component.
164%
165% The format of the AnnotateComponentTerminus method is:
166%
167% AnnotateComponentTerminus(void)
168%
169*/
cristy5ff4eaf2011-09-03 01:38:02 +0000170MagickPrivate void AnnotateComponentTerminus(void)
cristyd7ecaca2011-01-24 14:14:53 +0000171{
172 if (annotate_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +0000173 ActivateSemaphoreInfo(&annotate_semaphore);
cristy3d162a92014-02-16 14:05:06 +0000174 RelinquishSemaphoreInfo(&annotate_semaphore);
cristyd7ecaca2011-01-24 14:14:53 +0000175}
176
177/*
178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179% %
180% %
181% %
cristy3ed852e2009-09-05 21:47:34 +0000182% A n n o t a t e I m a g e %
183% %
184% %
185% %
186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187%
188% AnnotateImage() annotates an image with text. Optionally you can include
189% any of the following bits of information about the image by embedding
190% the appropriate special characters:
191%
192% %b file size in bytes.
193% %c comment.
194% %d directory in which the image resides.
195% %e extension of the image file.
196% %f original filename of the image.
197% %h height of image.
198% %i filename of the image.
199% %k number of unique colors.
200% %l image label.
201% %m image file format.
202% %n number of images in a image sequence.
203% %o output image filename.
204% %p page number of the image.
205% %q image depth (8 or 16).
206% %q image depth (8 or 16).
207% %s image scene number.
208% %t image filename without any extension.
209% %u a unique temporary filename.
210% %w image width.
211% %x x resolution of the image.
212% %y y resolution of the image.
213%
214% The format of the AnnotateImage method is:
215%
cristy5cbc0162011-08-29 00:36:28 +0000216% MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
217% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000218%
219% A description of each parameter follows:
220%
221% o image: the image.
222%
223% o draw_info: the draw info.
224%
cristy5cbc0162011-08-29 00:36:28 +0000225% o exception: return any errors or warnings in this structure.
226%
cristy3ed852e2009-09-05 21:47:34 +0000227*/
228MagickExport MagickBooleanType AnnotateImage(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000229 const DrawInfo *draw_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000230{
231 char
232 primitive[MaxTextExtent],
233 **textlist;
234
235 DrawInfo
236 *annotate,
237 *annotate_info;
238
239 GeometryInfo
240 geometry_info;
241
242 MagickBooleanType
243 status;
244
245 PointInfo
246 offset;
247
248 RectangleInfo
249 geometry;
250
cristybb503372010-05-27 20:51:26 +0000251 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000252 i;
253
254 size_t
255 length;
256
257 TypeMetric
258 metrics;
259
cristybb503372010-05-27 20:51:26 +0000260 size_t
cristy58460392009-09-09 01:52:06 +0000261 height,
cristy3ed852e2009-09-05 21:47:34 +0000262 number_lines;
263
264 assert(image != (Image *) NULL);
265 assert(image->signature == MagickSignature);
266 if (image->debug != MagickFalse)
267 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
268 assert(draw_info != (DrawInfo *) NULL);
269 assert(draw_info->signature == MagickSignature);
270 if (draw_info->text == (char *) NULL)
271 return(MagickFalse);
272 if (*draw_info->text == '\0')
273 return(MagickTrue);
274 textlist=StringToList(draw_info->text);
275 if (textlist == (char **) NULL)
276 return(MagickFalse);
277 length=strlen(textlist[0]);
278 for (i=1; textlist[i] != (char *) NULL; i++)
279 if (strlen(textlist[i]) > length)
280 length=strlen(textlist[i]);
cristybb503372010-05-27 20:51:26 +0000281 number_lines=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000282 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
283 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
284 SetGeometry(image,&geometry);
285 SetGeometryInfo(&geometry_info);
286 if (annotate_info->geometry != (char *) NULL)
287 {
288 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
cristy5cbc0162011-08-29 00:36:28 +0000289 exception);
cristy3ed852e2009-09-05 21:47:34 +0000290 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
291 }
cristy5cbc0162011-08-29 00:36:28 +0000292 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000293 return(MagickFalse);
cristya6400b12013-03-15 12:20:18 +0000294 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0c81d062013-04-21 15:22:02 +0000295 (void) SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000296 status=MagickTrue;
297 for (i=0; textlist[i] != (char *) NULL; i++)
298 {
299 /*
300 Position text relative to image.
301 */
302 annotate_info->affine.tx=geometry_info.xi-image->page.x;
303 annotate_info->affine.ty=geometry_info.psi-image->page.y;
304 (void) CloneString(&annotate->text,textlist[i]);
cristy5cbc0162011-08-29 00:36:28 +0000305 (void) GetTypeMetrics(image,annotate,&metrics,exception);
cristybb503372010-05-27 20:51:26 +0000306 height=(ssize_t) (metrics.ascent-metrics.descent+
cristy58460392009-09-09 01:52:06 +0000307 draw_info->interline_spacing+0.5);
cristy3ed852e2009-09-05 21:47:34 +0000308 switch (annotate->gravity)
309 {
310 case UndefinedGravity:
311 default:
312 {
313 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
314 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
315 break;
316 }
317 case NorthWestGravity:
318 {
319 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
320 annotate_info->affine.ry*height+annotate_info->affine.ry*
321 (metrics.ascent+metrics.descent);
322 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
323 annotate_info->affine.sy*height+annotate_info->affine.sy*
324 metrics.ascent;
325 break;
326 }
327 case NorthGravity:
328 {
329 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
330 geometry.width/2.0+i*annotate_info->affine.ry*height-
331 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
332 annotate_info->affine.ry*(metrics.ascent+metrics.descent);
333 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
334 annotate_info->affine.sy*height+annotate_info->affine.sy*
335 metrics.ascent-annotate_info->affine.rx*(metrics.width-
336 metrics.bounds.x1)/2.0;
337 break;
338 }
339 case NorthEastGravity:
340 {
341 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
342 geometry.width+i*annotate_info->affine.ry*height-
343 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
344 annotate_info->affine.ry*(metrics.ascent+metrics.descent)-1.0;
345 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
346 annotate_info->affine.sy*height+annotate_info->affine.sy*
347 metrics.ascent-annotate_info->affine.rx*(metrics.width-
348 metrics.bounds.x1);
349 break;
350 }
351 case WestGravity:
352 {
353 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
354 annotate_info->affine.ry*height+annotate_info->affine.ry*
355 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
356 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
357 geometry.height/2.0+i*annotate_info->affine.sy*height+
358 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
359 (number_lines-1.0)*height)/2.0;
360 break;
361 }
cristy3ed852e2009-09-05 21:47:34 +0000362 case CenterGravity:
363 {
364 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
365 geometry.width/2.0+i*annotate_info->affine.ry*height-
366 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0+
367 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
368 (number_lines-1)*height)/2.0;
369 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
370 geometry.height/2.0+i*annotate_info->affine.sy*height-
371 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0+
372 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
373 (number_lines-1.0)*height)/2.0;
374 break;
375 }
376 case EastGravity:
377 {
378 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
379 geometry.width+i*annotate_info->affine.ry*height-
380 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)+
381 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
382 (number_lines-1.0)*height)/2.0-1.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.rx*(metrics.width+metrics.bounds.x1)+
386 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
387 (number_lines-1.0)*height)/2.0;
388 break;
389 }
390 case SouthWestGravity:
391 {
392 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
393 annotate_info->affine.ry*height-annotate_info->affine.ry*
394 (number_lines-1.0)*height;
395 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
396 geometry.height+i*annotate_info->affine.sy*height-
397 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
398 break;
399 }
400 case SouthGravity:
401 {
402 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
403 geometry.width/2.0+i*annotate_info->affine.ry*height-
404 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0-
405 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
406 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
407 geometry.height+i*annotate_info->affine.sy*height-
408 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0-
409 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
410 break;
411 }
412 case SouthEastGravity:
413 {
414 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
415 geometry.width+i*annotate_info->affine.ry*height-
416 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)-
417 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
418 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
419 geometry.height+i*annotate_info->affine.sy*height-
420 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)-
421 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
422 break;
423 }
424 }
425 switch (annotate->align)
426 {
427 case LeftAlign:
428 {
429 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
430 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
431 break;
432 }
433 case CenterAlign:
434 {
435 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
436 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1)/2.0;
437 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
438 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1)/2.0;
439 break;
440 }
441 case RightAlign:
442 {
443 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
444 annotate_info->affine.sx*(metrics.width+metrics.bounds.x1);
445 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
446 annotate_info->affine.rx*(metrics.width+metrics.bounds.x1);
447 break;
448 }
449 default:
450 break;
451 }
cristy4c08aed2011-07-01 19:47:50 +0000452 if (draw_info->undercolor.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +0000453 {
454 DrawInfo
455 *undercolor_info;
456
457 /*
458 Text box.
459 */
460 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
461 undercolor_info->fill=draw_info->undercolor;
462 undercolor_info->affine=draw_info->affine;
463 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
464 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
cristyb51dff52011-05-19 16:55:47 +0000465 (void) FormatLocaleString(primitive,MaxTextExtent,
cristy90ceebd2013-06-13 12:42:09 +0000466 "rectangle -0.5,-0.5 %g,%.20g",metrics.origin.x,(double) height);
cristy3ed852e2009-09-05 21:47:34 +0000467 (void) CloneString(&undercolor_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000468 (void) DrawImage(image,undercolor_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000469 (void) DestroyDrawInfo(undercolor_info);
470 }
471 annotate_info->affine.tx=offset.x;
472 annotate_info->affine.ty=offset.y;
cristyb51dff52011-05-19 16:55:47 +0000473 (void) FormatLocaleString(primitive,MaxTextExtent,"stroke-width %g "
cristya8549b12011-05-18 19:05:08 +0000474 "line 0,0 %g,0",metrics.underline_thickness,metrics.width);
cristy3ed852e2009-09-05 21:47:34 +0000475 if (annotate->decorate == OverlineDecoration)
476 {
477 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
478 metrics.descent-metrics.underline_position));
479 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000480 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000481 }
482 else
483 if (annotate->decorate == UnderlineDecoration)
484 {
485 annotate_info->affine.ty-=(draw_info->affine.sy*
486 metrics.underline_position);
487 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000488 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000489 }
490 /*
491 Annotate image with text.
492 */
cristy5cbc0162011-08-29 00:36:28 +0000493 status=RenderType(image,annotate,&offset,&metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000494 if (status == MagickFalse)
495 break;
496 if (annotate->decorate == LineThroughDecoration)
497 {
498 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
499 metrics.underline_position+metrics.descent)/2.0);
500 (void) CloneString(&annotate_info->primitive,primitive);
cristy018f07f2011-09-04 21:15:19 +0000501 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000502 }
503 }
504 /*
505 Relinquish resources.
506 */
507 annotate_info=DestroyDrawInfo(annotate_info);
508 annotate=DestroyDrawInfo(annotate);
509 for (i=0; textlist[i] != (char *) NULL; i++)
510 textlist[i]=DestroyString(textlist[i]);
511 textlist=(char **) RelinquishMagickMemory(textlist);
512 return(status);
513}
514
515/*
516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517% %
518% %
519% %
520% F o r m a t M a g i c k C a p t i o n %
521% %
522% %
523% %
524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525%
526% FormatMagickCaption() formats a caption so that it fits within the image
527% width. It returns the number of lines in the formatted caption.
528%
529% The format of the FormatMagickCaption method is:
530%
cristybb503372010-05-27 20:51:26 +0000531% ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000532% const MagickBooleanType split,TypeMetric *metrics,char **caption,
533% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000534%
535% A description of each parameter follows.
536%
537% o image: The image.
538%
cristy3ed852e2009-09-05 21:47:34 +0000539% o draw_info: the draw info.
540%
cristy6b1d05e2010-09-22 19:17:27 +0000541% o split: when no convenient line breaks-- insert newline.
542%
cristy3ed852e2009-09-05 21:47:34 +0000543% o metrics: Return the font metrics in this structure.
544%
cristy6b1d05e2010-09-22 19:17:27 +0000545% o caption: the caption.
546%
cristy5cbc0162011-08-29 00:36:28 +0000547% o exception: return any errors or warnings in this structure.
548%
cristy3ed852e2009-09-05 21:47:34 +0000549*/
cristybb503372010-05-27 20:51:26 +0000550MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000551 const MagickBooleanType split,TypeMetric *metrics,char **caption,
552 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000553{
cristy74966db2011-09-20 19:37:21 +0000554 char
555 *text;
556
cristy3ed852e2009-09-05 21:47:34 +0000557 MagickBooleanType
558 status;
559
560 register char
561 *p,
562 *q,
563 *s;
564
cristybb503372010-05-27 20:51:26 +0000565 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000566 i;
567
cristybb503372010-05-27 20:51:26 +0000568 size_t
cristy3ed852e2009-09-05 21:47:34 +0000569 width;
570
cristy6193b862011-08-06 16:33:59 +0000571 ssize_t
572 n;
573
cristy74966db2011-09-20 19:37:21 +0000574 text=AcquireString(draw_info->text);
cristy3ed852e2009-09-05 21:47:34 +0000575 q=draw_info->text;
576 s=(char *) NULL;
577 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
578 {
579 if (IsUTFSpace(GetUTFCode(p)) != MagickFalse)
580 s=p;
cristy2911f2d2011-08-20 23:04:43 +0000581 if (GetUTFCode(p) == '\n')
cristye2c81ad2011-08-20 22:54:14 +0000582 q=draw_info->text;
cristybb503372010-05-27 20:51:26 +0000583 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
cristy3ed852e2009-09-05 21:47:34 +0000584 *q++=(*(p+i));
585 *q='\0';
cristy5cbc0162011-08-29 00:36:28 +0000586 status=GetTypeMetrics(image,draw_info,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000587 if (status == MagickFalse)
588 break;
cristy78fed5e2013-06-02 17:24:16 +0000589 width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
cristy74966db2011-09-20 19:37:21 +0000590 if ((width <= image->columns) || (strcmp(text,draw_info->text) == 0))
cristy6193b862011-08-06 16:33:59 +0000591 continue;
cristy74966db2011-09-20 19:37:21 +0000592 (void) strcpy(text,draw_info->text);
cristy4aa314e2011-08-19 13:59:45 +0000593 if ((s != (char *) NULL) && (GetUTFOctets(s) == 1))
cristy3ed852e2009-09-05 21:47:34 +0000594 {
595 *s='\n';
596 p=s;
597 }
598 else
cristy4aa314e2011-08-19 13:59:45 +0000599 if ((s != (char *) NULL) || (split != MagickFalse))
cristy6b1d05e2010-09-22 19:17:27 +0000600 {
601 char
602 *target;
cristy3ed852e2009-09-05 21:47:34 +0000603
cristy6b1d05e2010-09-22 19:17:27 +0000604 /*
605 No convenient line breaks-- insert newline.
606 */
607 target=AcquireString(*caption);
608 n=p-(*caption);
609 CopyMagickString(target,*caption,n+1);
610 ConcatenateMagickString(target,"\n",strlen(*caption)+1);
611 ConcatenateMagickString(target,p,strlen(*caption)+2);
612 (void) DestroyString(*caption);
613 *caption=target;
614 p=(*caption)+n;
615 }
cristy3ed852e2009-09-05 21:47:34 +0000616 q=draw_info->text;
cristy6193b862011-08-06 16:33:59 +0000617 s=(char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000618 }
cristy74966db2011-09-20 19:37:21 +0000619 text=DestroyString(text);
cristy6193b862011-08-06 16:33:59 +0000620 n=0;
cristy3ed852e2009-09-05 21:47:34 +0000621 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
622 if (GetUTFCode(p) == '\n')
cristy6193b862011-08-06 16:33:59 +0000623 n++;
624 return(n);
cristy3ed852e2009-09-05 21:47:34 +0000625}
626
627/*
628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629% %
630% %
631% %
632% G e t M u l t i l i n e T y p e M e t r i c s %
633% %
634% %
635% %
636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637%
638% GetMultilineTypeMetrics() returns the following information for the
639% specified font and text:
640%
641% character width
642% character height
643% ascender
644% descender
645% text width
646% text height
647% maximum horizontal advance
648% bounds: x1
649% bounds: y1
650% bounds: x2
651% bounds: y2
652% origin: x
653% origin: y
654% underline position
655% underline thickness
656%
657% This method is like GetTypeMetrics() but it returns the maximum text width
658% and height for multiple lines of text.
659%
660% The format of the GetMultilineTypeMetrics method is:
661%
662% MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000663% const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000664%
665% A description of each parameter follows:
666%
667% o image: the image.
668%
669% o draw_info: the draw info.
670%
671% o metrics: Return the font metrics in this structure.
672%
cristy5cbc0162011-08-29 00:36:28 +0000673% o exception: return any errors or warnings in this structure.
674%
cristy3ed852e2009-09-05 21:47:34 +0000675*/
676MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000677 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000678{
679 char
680 **textlist;
681
682 DrawInfo
683 *annotate_info;
684
685 MagickBooleanType
686 status;
687
cristybb503372010-05-27 20:51:26 +0000688 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000689 i;
690
691 TypeMetric
692 extent;
693
cristy3ed852e2009-09-05 21:47:34 +0000694 assert(image != (Image *) NULL);
695 assert(image->signature == MagickSignature);
696 if (image->debug != MagickFalse)
697 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
698 assert(draw_info != (DrawInfo *) NULL);
699 assert(draw_info->text != (char *) NULL);
700 assert(draw_info->signature == MagickSignature);
701 if (*draw_info->text == '\0')
702 return(MagickFalse);
703 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
704 annotate_info->text=DestroyString(annotate_info->text);
705 /*
706 Convert newlines to multiple lines of text.
707 */
708 textlist=StringToList(draw_info->text);
709 if (textlist == (char **) NULL)
710 return(MagickFalse);
711 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000712 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000713 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
714 (void) ResetMagickMemory(&extent,0,sizeof(extent));
715 /*
716 Find the widest of the text lines.
717 */
718 annotate_info->text=textlist[0];
cristy5cbc0162011-08-29 00:36:28 +0000719 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000720 *metrics=extent;
721 for (i=1; textlist[i] != (char *) NULL; i++)
722 {
723 annotate_info->text=textlist[i];
cristy5cbc0162011-08-29 00:36:28 +0000724 status=GetTypeMetrics(image,annotate_info,&extent,exception);
cristy3ed852e2009-09-05 21:47:34 +0000725 if (extent.width > metrics->width)
726 *metrics=extent;
727 }
cristye2c81ad2011-08-20 22:54:14 +0000728 metrics->height=(double) (i*(size_t) (metrics->ascent-metrics->descent+0.5)+
729 (i-1)*draw_info->interline_spacing);
cristy3ed852e2009-09-05 21:47:34 +0000730 /*
731 Relinquish resources.
732 */
733 annotate_info->text=(char *) NULL;
734 annotate_info=DestroyDrawInfo(annotate_info);
735 for (i=0; textlist[i] != (char *) NULL; i++)
736 textlist[i]=DestroyString(textlist[i]);
737 textlist=(char **) RelinquishMagickMemory(textlist);
738 return(status);
739}
740
741/*
742%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
743% %
744% %
745% %
746% G e t T y p e M e t r i c s %
747% %
748% %
749% %
750%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751%
752% GetTypeMetrics() returns the following information for the specified font
753% and text:
754%
755% character width
756% character height
757% ascender
758% descender
759% text width
760% text height
761% maximum horizontal advance
762% bounds: x1
763% bounds: y1
764% bounds: x2
765% bounds: y2
766% origin: x
767% origin: y
768% underline position
769% underline thickness
770%
771% The format of the GetTypeMetrics method is:
772%
773% MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000774% TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000775%
776% A description of each parameter follows:
777%
778% o image: the image.
779%
780% o draw_info: the draw info.
781%
782% o metrics: Return the font metrics in this structure.
783%
cristy5cbc0162011-08-29 00:36:28 +0000784% o exception: return any errors or warnings in this structure.
785%
cristy3ed852e2009-09-05 21:47:34 +0000786*/
787MagickExport MagickBooleanType GetTypeMetrics(Image *image,
cristy5cbc0162011-08-29 00:36:28 +0000788 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000789{
790 DrawInfo
791 *annotate_info;
792
793 MagickBooleanType
794 status;
795
796 PointInfo
797 offset;
798
799 assert(image != (Image *) NULL);
800 assert(image->signature == MagickSignature);
801 if (image->debug != MagickFalse)
802 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
803 assert(draw_info != (DrawInfo *) NULL);
804 assert(draw_info->text != (char *) NULL);
805 assert(draw_info->signature == MagickSignature);
806 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
807 annotate_info->render=MagickFalse;
cristy024843f2011-06-03 17:52:52 +0000808 annotate_info->direction=UndefinedDirection;
cristy3ed852e2009-09-05 21:47:34 +0000809 (void) ResetMagickMemory(metrics,0,sizeof(*metrics));
810 offset.x=0.0;
811 offset.y=0.0;
cristy5cbc0162011-08-29 00:36:28 +0000812 status=RenderType(image,annotate_info,&offset,metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000813 if (image->debug != MagickFalse)
814 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
cristye7f51092010-01-17 00:39:37 +0000815 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
cristya8549b12011-05-18 19:05:08 +0000816 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
cristye7f51092010-01-17 00:39:37 +0000817 "underline position: %g; underline thickness: %g",annotate_info->text,
cristy3ed852e2009-09-05 21:47:34 +0000818 metrics->width,metrics->height,metrics->ascent,metrics->descent,
819 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
cristy4c08aed2011-07-01 19:47:50 +0000820 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
821 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
cristy3ed852e2009-09-05 21:47:34 +0000822 metrics->underline_position,metrics->underline_thickness);
823 annotate_info=DestroyDrawInfo(annotate_info);
824 return(status);
825}
826
827/*
828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829% %
830% %
831% %
832+ R e n d e r T y p e %
833% %
834% %
835% %
836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837%
838% RenderType() renders text on the image. It also returns the bounding box of
839% the text relative to the image.
840%
841% The format of the RenderType method is:
842%
843% MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000844% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000845%
846% A description of each parameter follows:
847%
848% o image: the image.
849%
850% o draw_info: the draw info.
851%
852% o offset: (x,y) location of text relative to image.
853%
854% o metrics: bounding box of text.
855%
cristy5cbc0162011-08-29 00:36:28 +0000856% o exception: return any errors or warnings in this structure.
857%
cristy3ed852e2009-09-05 21:47:34 +0000858*/
859static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000860 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000861{
862 const TypeInfo
863 *type_info;
864
865 DrawInfo
866 *annotate_info;
867
868 MagickBooleanType
869 status;
870
871 type_info=(const TypeInfo *) NULL;
872 if (draw_info->font != (char *) NULL)
873 {
874 if (*draw_info->font == '@')
875 {
876 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000877 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000878 return(status);
879 }
880 if (*draw_info->font == '-')
cristy5cbc0162011-08-29 00:36:28 +0000881 return(RenderX11(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +0000882 if (IsPathAccessible(draw_info->font) != MagickFalse)
883 {
884 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
cristy5cbc0162011-08-29 00:36:28 +0000885 metrics,exception);
cristy3ed852e2009-09-05 21:47:34 +0000886 return(status);
887 }
cristy5cbc0162011-08-29 00:36:28 +0000888 type_info=GetTypeInfo(draw_info->font,exception);
cristy3ed852e2009-09-05 21:47:34 +0000889 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000890 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000891 "UnableToReadFont","`%s'",draw_info->font);
cristy3ed852e2009-09-05 21:47:34 +0000892 }
893 if ((type_info == (const TypeInfo *) NULL) &&
894 (draw_info->family != (const char *) NULL))
895 {
896 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000897 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000898 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000899 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
cristyefe601c2013-01-05 17:51:12 +0000900 "UnableToReadFont","`%s'",draw_info->family);
cristy3ed852e2009-09-05 21:47:34 +0000901 }
902 if (type_info == (const TypeInfo *) NULL)
cristyd984f272010-03-16 01:46:12 +0000903 type_info=GetTypeInfoByFamily("Arial",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000904 draw_info->stretch,draw_info->weight,exception);
cristyd984f272010-03-16 01:46:12 +0000905 if (type_info == (const TypeInfo *) NULL)
cristycbb34a42010-03-16 01:45:18 +0000906 type_info=GetTypeInfoByFamily("Helvetica",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000907 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000908 if (type_info == (const TypeInfo *) NULL)
cristy704acaa2010-03-23 13:08:21 +0000909 type_info=GetTypeInfoByFamily("Century Schoolbook",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000910 draw_info->stretch,draw_info->weight,exception);
cristy704acaa2010-03-23 13:08:21 +0000911 if (type_info == (const TypeInfo *) NULL)
cristy6193b862011-08-06 16:33:59 +0000912 type_info=GetTypeInfoByFamily("Sans",draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000913 draw_info->stretch,draw_info->weight,exception);
cristy6193b862011-08-06 16:33:59 +0000914 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000915 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
cristy5cbc0162011-08-29 00:36:28 +0000916 draw_info->stretch,draw_info->weight,exception);
cristy3ed852e2009-09-05 21:47:34 +0000917 if (type_info == (const TypeInfo *) NULL)
cristy5cbc0162011-08-29 00:36:28 +0000918 type_info=GetTypeInfo("*",exception);
cristy704acaa2010-03-23 13:08:21 +0000919 if (type_info == (const TypeInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000920 {
cristy5cbc0162011-08-29 00:36:28 +0000921 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
922 exception);
cristy3ed852e2009-09-05 21:47:34 +0000923 return(status);
924 }
925 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
926 annotate_info->face=type_info->face;
927 if (type_info->metrics != (char *) NULL)
928 (void) CloneString(&annotate_info->metrics,type_info->metrics);
929 if (type_info->glyphs != (char *) NULL)
930 (void) CloneString(&annotate_info->font,type_info->glyphs);
cristy5cbc0162011-08-29 00:36:28 +0000931 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
932 exception);
cristy3ed852e2009-09-05 21:47:34 +0000933 annotate_info=DestroyDrawInfo(annotate_info);
934 return(status);
935}
936
937/*
938%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939% %
940% %
941% %
942+ R e n d e r F r e e t y p e %
943% %
944% %
945% %
946%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
947%
948% RenderFreetype() renders text on the image with a Truetype font. It also
949% returns the bounding box of the text relative to the image.
950%
951% The format of the RenderFreetype method is:
952%
953% MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +0000954% const char *encoding,const PointInfo *offset,TypeMetric *metrics,
955% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000956%
957% A description of each parameter follows:
958%
959% o image: the image.
960%
961% o draw_info: the draw info.
962%
963% o encoding: the font encoding.
964%
965% o offset: (x,y) location of text relative to image.
966%
967% o metrics: bounding box of text.
968%
cristy5cbc0162011-08-29 00:36:28 +0000969% o exception: return any errors or warnings in this structure.
970%
cristy3ed852e2009-09-05 21:47:34 +0000971*/
972
973#if defined(MAGICKCORE_FREETYPE_DELEGATE)
cristy2857ffc2010-03-27 23:44:00 +0000974
cristy3ed852e2009-09-05 21:47:34 +0000975static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
976 DrawInfo *draw_info)
977{
978 AffineMatrix
979 affine;
980
981 char
982 path[MaxTextExtent];
983
984 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +0000985 (void) FormatLocaleString(path,MaxTextExtent,
cristya8549b12011-05-18 19:05:08 +0000986 "C%g,%g %g,%g %g,%g",affine.tx+p->x/64.0,affine.ty-
cristy8cd5b312010-01-07 01:10:24 +0000987 p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,affine.tx+to->x/64.0,
988 affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +0000989 (void) ConcatenateString(&draw_info->primitive,path);
990 return(0);
991}
992
993static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
994{
995 AffineMatrix
996 affine;
997
998 char
999 path[MaxTextExtent];
1000
1001 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +00001002 (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +00001003 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001004 (void) ConcatenateString(&draw_info->primitive,path);
1005 return(0);
1006}
1007
1008static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1009{
1010 AffineMatrix
1011 affine;
1012
1013 char
1014 path[MaxTextExtent];
1015
1016 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +00001017 (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+
cristy8cd5b312010-01-07 01:10:24 +00001018 to->x/64.0,affine.ty-to->y/64.0);
cristy3ed852e2009-09-05 21:47:34 +00001019 (void) ConcatenateString(&draw_info->primitive,path);
1020 return(0);
1021}
1022
1023static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1024 DrawInfo *draw_info)
1025{
1026 AffineMatrix
1027 affine;
1028
1029 char
1030 path[MaxTextExtent];
1031
1032 affine=draw_info->affine;
cristyb51dff52011-05-19 16:55:47 +00001033 (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",
cristy3ed852e2009-09-05 21:47:34 +00001034 affine.tx+control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,
1035 affine.ty-to->y/64.0);
1036 (void) ConcatenateString(&draw_info->primitive,path);
1037 return(0);
1038}
1039
1040static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001041 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1042 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001043{
1044#if !defined(FT_OPEN_PATHNAME)
1045#define FT_OPEN_PATHNAME ft_open_pathname
1046#endif
1047
1048 typedef struct _GlyphInfo
1049 {
1050 FT_UInt
1051 id;
1052
1053 FT_Vector
1054 origin;
1055
1056 FT_Glyph
1057 image;
1058 } GlyphInfo;
1059
1060 const char
1061 *value;
1062
cristyc9b12952010-03-28 01:12:28 +00001063 double
1064 direction;
1065
cristy3ed852e2009-09-05 21:47:34 +00001066 DrawInfo
1067 *annotate_info;
1068
1069 FT_BBox
1070 bounds;
1071
1072 FT_BitmapGlyph
1073 bitmap;
1074
1075 FT_Encoding
1076 encoding_type;
1077
1078 FT_Error
cristyf8f295f2013-08-11 17:26:49 +00001079 ft_status;
cristy3ed852e2009-09-05 21:47:34 +00001080
1081 FT_Face
1082 face;
1083
1084 FT_Int32
1085 flags;
1086
1087 FT_Library
1088 library;
1089
1090 FT_Matrix
1091 affine;
1092
1093 FT_Open_Args
1094 args;
1095
1096 FT_Vector
1097 origin;
1098
1099 GlyphInfo
1100 glyph,
1101 last_glyph;
1102
cristyf8f295f2013-08-11 17:26:49 +00001103 MagickBooleanType
1104 status;
1105
cristy3ed852e2009-09-05 21:47:34 +00001106 PointInfo
1107 point,
1108 resolution;
1109
1110 register char
1111 *p;
1112
cristy9d314ff2011-03-09 01:30:28 +00001113 ssize_t
1114 code,
1115 y;
1116
cristy3ed852e2009-09-05 21:47:34 +00001117 static FT_Outline_Funcs
1118 OutlineMethods =
1119 {
1120 (FT_Outline_MoveTo_Func) TraceMoveTo,
1121 (FT_Outline_LineTo_Func) TraceLineTo,
1122 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1123 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1124 0, 0
1125 };
1126
cristy2857ffc2010-03-27 23:44:00 +00001127 unsigned char
1128 *utf8;
1129
cristy3ed852e2009-09-05 21:47:34 +00001130 /*
1131 Initialize Truetype library.
1132 */
cristyf8f295f2013-08-11 17:26:49 +00001133 ft_status=FT_Init_FreeType(&library);
1134 if (ft_status != 0)
cristy3ed852e2009-09-05 21:47:34 +00001135 ThrowBinaryException(TypeError,"UnableToInitializeFreetypeLibrary",
1136 image->filename);
1137 args.flags=FT_OPEN_PATHNAME;
1138 if (draw_info->font == (char *) NULL)
1139 args.pathname=ConstantString("helvetica");
1140 else
1141 if (*draw_info->font != '@')
1142 args.pathname=ConstantString(draw_info->font);
1143 else
1144 args.pathname=ConstantString(draw_info->font+1);
1145 face=(FT_Face) NULL;
cristyf8f295f2013-08-11 17:26:49 +00001146 ft_status=FT_Open_Face(library,&args,(long) draw_info->face,&face);
cristy3ed852e2009-09-05 21:47:34 +00001147 args.pathname=DestroyString(args.pathname);
cristyf8f295f2013-08-11 17:26:49 +00001148 if (ft_status != 0)
cristy3ed852e2009-09-05 21:47:34 +00001149 {
1150 (void) FT_Done_FreeType(library);
cristy5cbc0162011-08-29 00:36:28 +00001151 (void) ThrowMagickException(exception,GetMagickModule(),TypeError,
cristyefe601c2013-01-05 17:51:12 +00001152 "UnableToReadFont","`%s'",draw_info->font);
cristy5cbc0162011-08-29 00:36:28 +00001153 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001154 }
1155 if ((draw_info->metrics != (char *) NULL) &&
1156 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1157 (void) FT_Attach_File(face,draw_info->metrics);
1158 encoding_type=ft_encoding_unicode;
cristyf8f295f2013-08-11 17:26:49 +00001159 ft_status=FT_Select_Charmap(face,encoding_type);
1160 if ((ft_status != 0) && (face->num_charmaps != 0))
1161 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
cristy3ed852e2009-09-05 21:47:34 +00001162 if (encoding != (const char *) NULL)
1163 {
1164 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1165 encoding_type=ft_encoding_adobe_custom;
1166 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1167 encoding_type=ft_encoding_adobe_expert;
1168 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1169 encoding_type=ft_encoding_adobe_standard;
1170 if (LocaleCompare(encoding,"AppleRoman") == 0)
1171 encoding_type=ft_encoding_apple_roman;
1172 if (LocaleCompare(encoding,"BIG5") == 0)
1173 encoding_type=ft_encoding_big5;
1174 if (LocaleCompare(encoding,"GB2312") == 0)
1175 encoding_type=ft_encoding_gb2312;
1176 if (LocaleCompare(encoding,"Johab") == 0)
1177 encoding_type=ft_encoding_johab;
1178#if defined(ft_encoding_latin_1)
1179 if (LocaleCompare(encoding,"Latin-1") == 0)
1180 encoding_type=ft_encoding_latin_1;
1181#endif
1182 if (LocaleCompare(encoding,"Latin-2") == 0)
1183 encoding_type=ft_encoding_latin_2;
1184 if (LocaleCompare(encoding,"None") == 0)
1185 encoding_type=ft_encoding_none;
1186 if (LocaleCompare(encoding,"SJIScode") == 0)
1187 encoding_type=ft_encoding_sjis;
1188 if (LocaleCompare(encoding,"Symbol") == 0)
1189 encoding_type=ft_encoding_symbol;
1190 if (LocaleCompare(encoding,"Unicode") == 0)
1191 encoding_type=ft_encoding_unicode;
1192 if (LocaleCompare(encoding,"Wansung") == 0)
1193 encoding_type=ft_encoding_wansung;
cristyf8f295f2013-08-11 17:26:49 +00001194 ft_status=FT_Select_Charmap(face,encoding_type);
1195 if (ft_status != 0)
cristye3b2bb82014-02-15 13:20:08 +00001196 {
1197 (void) FT_Done_Face(face);
1198 (void) FT_Done_FreeType(library);
1199 ThrowBinaryException(TypeError,"UnrecognizedFontEncoding",encoding);
1200 }
cristy3ed852e2009-09-05 21:47:34 +00001201 }
1202 /*
1203 Set text size.
1204 */
1205 resolution.x=DefaultResolution;
1206 resolution.y=DefaultResolution;
1207 if (draw_info->density != (char *) NULL)
1208 {
1209 GeometryInfo
1210 geometry_info;
1211
1212 MagickStatusType
1213 flags;
1214
1215 flags=ParseGeometry(draw_info->density,&geometry_info);
1216 resolution.x=geometry_info.rho;
1217 resolution.y=geometry_info.sigma;
1218 if ((flags & SigmaValue) == 0)
1219 resolution.y=resolution.x;
1220 }
cristyf8f295f2013-08-11 17:26:49 +00001221 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
cristy3ed852e2009-09-05 21:47:34 +00001222 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1223 (FT_UInt) resolution.y);
1224 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1225 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1226 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1227 metrics->descent=(double) face->size->metrics.descender/64.0;
1228 metrics->width=0;
1229 metrics->origin.x=0;
1230 metrics->origin.y=0;
1231 metrics->height=(double) face->size->metrics.height/64.0;
1232 metrics->max_advance=0.0;
1233 if (face->size->metrics.max_advance > MagickEpsilon)
1234 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1235 metrics->bounds.x1=0.0;
1236 metrics->bounds.y1=metrics->descent;
1237 metrics->bounds.x2=metrics->ascent+metrics->descent;
1238 metrics->bounds.y2=metrics->ascent+metrics->descent;
1239 metrics->underline_position=face->underline_position/64.0;
1240 metrics->underline_thickness=face->underline_thickness/64.0;
cristy71709bf2011-09-25 13:10:29 +00001241 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0'))
cristy3ed852e2009-09-05 21:47:34 +00001242 {
1243 (void) FT_Done_Face(face);
1244 (void) FT_Done_FreeType(library);
1245 return(MagickTrue);
1246 }
1247 /*
1248 Compute bounding box.
1249 */
1250 if (image->debug != MagickFalse)
1251 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
cristye7f51092010-01-17 00:39:37 +00001252 "font-encoding %s; text-encoding %s; pointsize %g",
cristy3ed852e2009-09-05 21:47:34 +00001253 draw_info->font != (char *) NULL ? draw_info->font : "none",
1254 encoding != (char *) NULL ? encoding : "none",
1255 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1256 draw_info->pointsize);
1257 flags=FT_LOAD_NO_BITMAP;
cristy55a804e2012-01-13 17:46:11 +00001258 if (draw_info->text_antialias == MagickFalse)
cristy4f9709e2012-06-10 01:54:51 +00001259 flags|=FT_LOAD_TARGET_MONO;
cristy55a804e2012-01-13 17:46:11 +00001260 else
1261 {
cristya89189d2012-01-13 17:54:57 +00001262#if defined(FT_LOAD_TARGET_LIGHT)
cristy55a804e2012-01-13 17:46:11 +00001263 flags|=FT_LOAD_TARGET_LIGHT;
cristya89189d2012-01-13 17:54:57 +00001264#elif defined(FT_LOAD_TARGET_LCD)
1265 flags|=FT_LOAD_TARGET_LCD;
cristy55a804e2012-01-13 17:46:11 +00001266#endif
1267 }
cristyd15e6592011-10-15 00:13:06 +00001268 value=GetImageProperty(image,"type:hinting",exception);
cristy33204242010-10-07 23:17:12 +00001269 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
cristy3ed852e2009-09-05 21:47:34 +00001270 flags|=FT_LOAD_NO_HINTING;
1271 glyph.id=0;
1272 glyph.image=NULL;
1273 last_glyph.id=0;
1274 last_glyph.image=NULL;
1275 origin.x=0;
1276 origin.y=0;
1277 affine.xx=65536L;
1278 affine.yx=0L;
1279 affine.xy=0L;
1280 affine.yy=65536L;
1281 if (draw_info->render != MagickFalse)
1282 {
1283 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1284 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1285 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1286 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1287 }
1288 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1289 (void) CloneString(&annotate_info->primitive,"path '");
1290 if (draw_info->render != MagickFalse)
1291 {
1292 if (image->storage_class != DirectClass)
cristy5cbc0162011-08-29 00:36:28 +00001293 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a46d822012-08-28 23:32:39 +00001294 if (image->alpha_trait != BlendPixelTrait)
cristy5cbc0162011-08-29 00:36:28 +00001295 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001296 }
cristyc9b12952010-03-28 01:12:28 +00001297 direction=1.0;
cristycf5467b2010-03-28 16:22:06 +00001298 if (draw_info->direction == RightToLeftDirection)
cristyc9b12952010-03-28 01:12:28 +00001299 direction=(-1.0);
cristy3ed852e2009-09-05 21:47:34 +00001300 point.x=0.0;
1301 point.y=0.0;
cristy3ed852e2009-09-05 21:47:34 +00001302 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
cristybd512462010-02-13 02:13:14 +00001303 if (GetUTFCode(p) < 0)
1304 break;
cristy2857ffc2010-03-27 23:44:00 +00001305 utf8=(unsigned char *) NULL;
cristyd913ea12010-02-13 02:32:57 +00001306 if (GetUTFCode(p) == 0)
cristy2857ffc2010-03-27 23:44:00 +00001307 p=draw_info->text;
cristybd512462010-02-13 02:13:14 +00001308 else
cristybd512462010-02-13 02:13:14 +00001309 {
cristy2857ffc2010-03-27 23:44:00 +00001310 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1311 if (utf8 != (unsigned char *) NULL)
cristyc9b12952010-03-28 01:12:28 +00001312 p=(char *) utf8;
cristy2857ffc2010-03-27 23:44:00 +00001313 }
cristyf8f295f2013-08-11 17:26:49 +00001314 status=MagickTrue;
cristy2857ffc2010-03-27 23:44:00 +00001315 for (code=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1316 {
1317 /*
1318 Render UTF-8 sequence.
1319 */
1320 glyph.id=FT_Get_Char_Index(face,GetUTFCode(p));
1321 if (glyph.id == 0)
1322 glyph.id=FT_Get_Char_Index(face,'?');
1323 if ((glyph.id != 0) && (last_glyph.id != 0))
1324 {
cristyaa83c2c2011-09-21 13:36:25 +00001325 if (fabs(draw_info->kerning) >= MagickEpsilon)
cristy94b11832011-09-08 19:46:03 +00001326 origin.x+=(FT_Pos) (64.0*direction*draw_info->kerning);
cristy2857ffc2010-03-27 23:44:00 +00001327 else
1328 if (FT_HAS_KERNING(face))
1329 {
1330 FT_Vector
1331 kerning;
cristybd512462010-02-13 02:13:14 +00001332
cristyf8f295f2013-08-11 17:26:49 +00001333 ft_status=FT_Get_Kerning(face,last_glyph.id,glyph.id,
cristy2857ffc2010-03-27 23:44:00 +00001334 ft_kerning_default,&kerning);
cristyf8f295f2013-08-11 17:26:49 +00001335 if (ft_status == 0)
cristy94b11832011-09-08 19:46:03 +00001336 origin.x+=(FT_Pos) (direction*kerning.x);
cristy2857ffc2010-03-27 23:44:00 +00001337 }
1338 }
1339 glyph.origin=origin;
cristyf8f295f2013-08-11 17:26:49 +00001340 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1341 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001342 continue;
cristyf8f295f2013-08-11 17:26:49 +00001343 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1344 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001345 continue;
cristyf8f295f2013-08-11 17:26:49 +00001346 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->outline,
cristy2857ffc2010-03-27 23:44:00 +00001347 &bounds);
cristyf8f295f2013-08-11 17:26:49 +00001348 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001349 continue;
1350 if ((p == draw_info->text) || (bounds.xMin < metrics->bounds.x1))
cristyaa83c2c2011-09-21 13:36:25 +00001351 metrics->bounds.x1=(double) bounds.xMin;
cristy2857ffc2010-03-27 23:44:00 +00001352 if ((p == draw_info->text) || (bounds.yMin < metrics->bounds.y1))
cristyaa83c2c2011-09-21 13:36:25 +00001353 metrics->bounds.y1=(double) bounds.yMin;
cristy2857ffc2010-03-27 23:44:00 +00001354 if ((p == draw_info->text) || (bounds.xMax > metrics->bounds.x2))
cristyaa83c2c2011-09-21 13:36:25 +00001355 metrics->bounds.x2=(double) bounds.xMax;
cristy2857ffc2010-03-27 23:44:00 +00001356 if ((p == draw_info->text) || (bounds.yMax > metrics->bounds.y2))
cristyaa83c2c2011-09-21 13:36:25 +00001357 metrics->bounds.y2=(double) bounds.yMax;
cristyf8f295f2013-08-11 17:26:49 +00001358 if ((draw_info->stroke.alpha != TransparentAlpha) ||
1359 (draw_info->stroke_pattern != (Image *) NULL))
1360 {
1361 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
1362 {
1363 /*
1364 Trace the glyph.
1365 */
1366 annotate_info->affine.tx=glyph.origin.x/64.0;
1367 annotate_info->affine.ty=glyph.origin.y/64.0;
1368 (void) FT_Outline_Decompose(&((FT_OutlineGlyph) glyph.image)->
1369 outline,&OutlineMethods,annotate_info);
1370 }
cristy2857ffc2010-03-27 23:44:00 +00001371 }
1372 FT_Vector_Transform(&glyph.origin,&affine);
1373 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
cristyf8f295f2013-08-11 17:26:49 +00001374 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
cristy2857ffc2010-03-27 23:44:00 +00001375 (FT_Vector *) NULL,MagickTrue);
cristyf8f295f2013-08-11 17:26:49 +00001376 if (ft_status != 0)
cristy2857ffc2010-03-27 23:44:00 +00001377 continue;
1378 bitmap=(FT_BitmapGlyph) glyph.image;
1379 point.x=offset->x+bitmap->left;
cristy2f5b5c72011-09-16 16:52:00 +00001380 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1381 point.x=offset->x+(origin.x >> 6);
cristy2857ffc2010-03-27 23:44:00 +00001382 point.y=offset->y-bitmap->top;
1383 if (draw_info->render != MagickFalse)
1384 {
1385 CacheView
1386 *image_view;
cristybd512462010-02-13 02:13:14 +00001387
cristy2f5b5c72011-09-16 16:52:00 +00001388 register unsigned char
1389 *p;
1390
cristy2857ffc2010-03-27 23:44:00 +00001391 /*
1392 Rasterize the glyph.
1393 */
cristy46ff2672012-12-14 15:32:26 +00001394 image_view=AcquireAuthenticCacheView(image,exception);
cristy2f5b5c72011-09-16 16:52:00 +00001395 p=bitmap->bitmap.buffer;
cristybb503372010-05-27 20:51:26 +00001396 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
cristy2857ffc2010-03-27 23:44:00 +00001397 {
cristybd512462010-02-13 02:13:14 +00001398 MagickBooleanType
cristy2857ffc2010-03-27 23:44:00 +00001399 active,
1400 sync;
cristybd512462010-02-13 02:13:14 +00001401
cristya19f1d72012-08-07 18:24:38 +00001402 double
cristy2857ffc2010-03-27 23:44:00 +00001403 fill_opacity;
cristybd512462010-02-13 02:13:14 +00001404
cristy101ab702011-10-13 13:06:32 +00001405 PixelInfo
cristy2857ffc2010-03-27 23:44:00 +00001406 fill_color;
cristybd512462010-02-13 02:13:14 +00001407
cristy4c08aed2011-07-01 19:47:50 +00001408 register Quantum
cristy2857ffc2010-03-27 23:44:00 +00001409 *restrict q;
cristybd512462010-02-13 02:13:14 +00001410
cristy88158062011-06-09 00:31:43 +00001411 register ssize_t
1412 x;
1413
cristy73ce31b2010-06-13 23:17:39 +00001414 ssize_t
cristy2f5b5c72011-09-16 16:52:00 +00001415 n,
cristy73ce31b2010-06-13 23:17:39 +00001416 x_offset,
1417 y_offset;
1418
cristy2857ffc2010-03-27 23:44:00 +00001419 if (status == MagickFalse)
1420 continue;
cristybb503372010-05-27 20:51:26 +00001421 x_offset=(ssize_t) ceil(point.x-0.5);
1422 y_offset=(ssize_t) ceil(point.y+y-0.5);
1423 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
cristy2857ffc2010-03-27 23:44:00 +00001424 continue;
cristy4c08aed2011-07-01 19:47:50 +00001425 q=(Quantum *) NULL;
cristybb503372010-05-27 20:51:26 +00001426 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001427 active=MagickFalse;
1428 else
cristybd512462010-02-13 02:13:14 +00001429 {
cristy2857ffc2010-03-27 23:44:00 +00001430 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1431 bitmap->bitmap.width,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001432 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
cristybd512462010-02-13 02:13:14 +00001433 }
cristy854209a2011-09-19 17:02:57 +00001434 n=y*bitmap->bitmap.pitch-1;
1435 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++)
cristy2857ffc2010-03-27 23:44:00 +00001436 {
cristy854209a2011-09-19 17:02:57 +00001437 n++;
cristy2857ffc2010-03-27 23:44:00 +00001438 x_offset++;
cristy2f5b5c72011-09-16 16:52:00 +00001439 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
cristy2857ffc2010-03-27 23:44:00 +00001440 {
cristyed231572011-07-14 02:18:59 +00001441 q+=GetPixelChannels(image);
cristy2857ffc2010-03-27 23:44:00 +00001442 continue;
1443 }
cristy2f5b5c72011-09-16 16:52:00 +00001444 if (bitmap->bitmap.pixel_mode != ft_pixel_mode_mono)
cristya19f1d72012-08-07 18:24:38 +00001445 fill_opacity=(double) (p[n])/(bitmap->bitmap.num_grays-1);
cristy2f5b5c72011-09-16 16:52:00 +00001446 else
1447 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1448 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
cristy2857ffc2010-03-27 23:44:00 +00001449 if (draw_info->text_antialias == MagickFalse)
1450 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1451 if (active == MagickFalse)
1452 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1453 exception);
cristy6193b862011-08-06 16:33:59 +00001454 if (q == (Quantum *) NULL)
cristy2857ffc2010-03-27 23:44:00 +00001455 {
cristyed231572011-07-14 02:18:59 +00001456 q+=GetPixelChannels(image);
cristy2857ffc2010-03-27 23:44:00 +00001457 continue;
1458 }
cristyec061ec2011-10-21 17:41:17 +00001459 GetPixelInfo(image,&fill_color);
cristy2ed42f62011-10-02 19:49:57 +00001460 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color,
1461 exception);
cristy4c08aed2011-07-01 19:47:50 +00001462 fill_opacity=fill_opacity*fill_color.alpha;
1463 CompositePixelOver(image,&fill_color,fill_opacity,q,
1464 GetPixelAlpha(image,q),q);
cristy2857ffc2010-03-27 23:44:00 +00001465 if (active == MagickFalse)
1466 {
1467 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1468 if (sync == MagickFalse)
1469 status=MagickFalse;
1470 }
cristyed231572011-07-14 02:18:59 +00001471 q+=GetPixelChannels(image);
cristybd512462010-02-13 02:13:14 +00001472 }
cristy2857ffc2010-03-27 23:44:00 +00001473 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1474 if (sync == MagickFalse)
1475 status=MagickFalse;
cristybd512462010-02-13 02:13:14 +00001476 }
cristy2857ffc2010-03-27 23:44:00 +00001477 image_view=DestroyCacheView(image_view);
1478 }
1479 if ((bitmap->left+bitmap->bitmap.width) > metrics->width)
1480 metrics->width=bitmap->left+bitmap->bitmap.width;
cristyaa83c2c2011-09-21 13:36:25 +00001481 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
cristy2857ffc2010-03-27 23:44:00 +00001482 (IsUTFSpace(GetUTFCode(p)) != MagickFalse) &&
1483 (IsUTFSpace(code) == MagickFalse))
cristy94b11832011-09-08 19:46:03 +00001484 origin.x+=(FT_Pos) (64.0*direction*draw_info->interword_spacing);
cristy2857ffc2010-03-27 23:44:00 +00001485 else
cristy94b11832011-09-08 19:46:03 +00001486 origin.x+=(FT_Pos) (direction*face->glyph->advance.x);
cristyaa83c2c2011-09-21 13:36:25 +00001487 metrics->origin.x=(double) origin.x;
1488 metrics->origin.y=(double) origin.y;
cristybd5a96c2011-08-21 00:04:26 +00001489 if (last_glyph.id != 0)
1490 FT_Done_Glyph(last_glyph.image);
cristy2857ffc2010-03-27 23:44:00 +00001491 last_glyph=glyph;
1492 code=GetUTFCode(p);
1493 }
1494 if (utf8 != (unsigned char *) NULL)
1495 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
cristybd5a96c2011-08-21 00:04:26 +00001496 if (last_glyph.id != 0)
1497 FT_Done_Glyph(last_glyph.image);
cristy4c08aed2011-07-01 19:47:50 +00001498 if ((draw_info->stroke.alpha != TransparentAlpha) ||
cristy3ed852e2009-09-05 21:47:34 +00001499 (draw_info->stroke_pattern != (Image *) NULL))
1500 {
cristyf8f295f2013-08-11 17:26:49 +00001501 if ((status != MagickFalse) && (draw_info->render != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001502 {
1503 /*
1504 Draw text stroke.
1505 */
1506 annotate_info->linejoin=RoundJoin;
1507 annotate_info->affine.tx=offset->x;
1508 annotate_info->affine.ty=offset->y;
1509 (void) ConcatenateString(&annotate_info->primitive,"'");
cristy018f07f2011-09-04 21:15:19 +00001510 (void) DrawImage(image,annotate_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001511 }
1512 }
1513 /*
1514 Determine font metrics.
1515 */
1516 glyph.id=FT_Get_Char_Index(face,'_');
1517 glyph.origin=origin;
cristyf8f295f2013-08-11 17:26:49 +00001518 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1519 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001520 {
cristyf8f295f2013-08-11 17:26:49 +00001521 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1522 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001523 {
cristyf8f295f2013-08-11 17:26:49 +00001524 ft_status=FT_Outline_Get_BBox(&((FT_OutlineGlyph) glyph.image)->
1525 outline,&bounds);
1526 if (ft_status == 0)
cristy3ed852e2009-09-05 21:47:34 +00001527 {
1528 FT_Vector_Transform(&glyph.origin,&affine);
1529 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
cristyf8f295f2013-08-11 17:26:49 +00001530 ft_status=FT_Glyph_To_Bitmap(&glyph.image,ft_render_mode_normal,
cristy3ed852e2009-09-05 21:47:34 +00001531 (FT_Vector *) NULL,MagickTrue);
1532 bitmap=(FT_BitmapGlyph) glyph.image;
1533 if (bitmap->left > metrics->width)
1534 metrics->width=bitmap->left;
1535 }
1536 }
cristye2c81ad2011-08-20 22:54:14 +00001537 FT_Done_Glyph(glyph.image);
cristy3ed852e2009-09-05 21:47:34 +00001538 }
1539 metrics->width-=metrics->bounds.x1/64.0;
cristy55cb9f12012-08-23 09:02:28 +00001540 metrics->width+=annotate_info->stroke_width;
cristy3ed852e2009-09-05 21:47:34 +00001541 metrics->bounds.x1/=64.0;
1542 metrics->bounds.y1/=64.0;
1543 metrics->bounds.x2/=64.0;
1544 metrics->bounds.y2/=64.0;
1545 metrics->origin.x/=64.0;
1546 metrics->origin.y/=64.0;
1547 /*
1548 Relinquish resources.
1549 */
1550 annotate_info=DestroyDrawInfo(annotate_info);
1551 (void) FT_Done_Face(face);
1552 (void) FT_Done_FreeType(library);
cristyf8f295f2013-08-11 17:26:49 +00001553 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001554}
1555#else
1556static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1557 const char *magick_unused(encoding),const PointInfo *offset,
cristy5cbc0162011-08-29 00:36:28 +00001558 TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001559{
cristy5cbc0162011-08-29 00:36:28 +00001560 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001561 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
cristyee081822011-02-02 19:07:30 +00001562 draw_info->font != (char *) NULL ? draw_info->font : "none");
cristye3b062a2011-12-17 18:34:52 +00001563 return(RenderPostscript(image,draw_info,offset,metrics,exception));
cristy3ed852e2009-09-05 21:47:34 +00001564}
1565#endif
1566
1567/*
1568%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1569% %
1570% %
1571% %
1572+ R e n d e r P o s t s c r i p t %
1573% %
1574% %
1575% %
1576%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1577%
1578% RenderPostscript() renders text on the image with a Postscript font. It
1579% also returns the bounding box of the text relative to the image.
1580%
1581% The format of the RenderPostscript method is:
1582%
1583% MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001584% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001585%
1586% A description of each parameter follows:
1587%
1588% o image: the image.
1589%
1590% o draw_info: the draw info.
1591%
1592% o offset: (x,y) location of text relative to image.
1593%
1594% o metrics: bounding box of text.
1595%
cristy5cbc0162011-08-29 00:36:28 +00001596% o exception: return any errors or warnings in this structure.
1597%
cristy3ed852e2009-09-05 21:47:34 +00001598*/
1599
1600static inline size_t MagickMin(const size_t x,const size_t y)
1601{
1602 if (x < y)
1603 return(x);
1604 return(y);
1605}
1606
1607static char *EscapeParenthesis(const char *text)
1608{
1609 char
1610 *buffer;
1611
1612 register char
1613 *p;
1614
cristybb503372010-05-27 20:51:26 +00001615 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001616 i;
1617
1618 size_t
1619 escapes;
1620
1621 escapes=0;
1622 buffer=AcquireString(text);
1623 p=buffer;
cristybb503372010-05-27 20:51:26 +00001624 for (i=0; i < (ssize_t) MagickMin(strlen(text),MaxTextExtent-escapes-1); i++)
cristy3ed852e2009-09-05 21:47:34 +00001625 {
1626 if ((text[i] == '(') || (text[i] == ')'))
1627 {
1628 *p++='\\';
1629 escapes++;
1630 }
1631 *p++=text[i];
1632 }
1633 *p='\0';
1634 return(buffer);
1635}
1636
1637static MagickBooleanType RenderPostscript(Image *image,
cristy5cbc0162011-08-29 00:36:28 +00001638 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
1639 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001640{
1641 char
1642 filename[MaxTextExtent],
1643 geometry[MaxTextExtent],
1644 *text;
1645
1646 FILE
1647 *file;
1648
1649 Image
1650 *annotate_image;
1651
1652 ImageInfo
1653 *annotate_info;
1654
1655 int
1656 unique_file;
1657
cristy3ed852e2009-09-05 21:47:34 +00001658 MagickBooleanType
1659 identity;
1660
1661 PointInfo
1662 extent,
1663 point,
1664 resolution;
1665
cristybb503372010-05-27 20:51:26 +00001666 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001667 i;
1668
cristy14d71292012-05-20 16:48:13 +00001669 size_t
1670 length;
1671
cristy9d314ff2011-03-09 01:30:28 +00001672 ssize_t
1673 y;
1674
cristy3ed852e2009-09-05 21:47:34 +00001675 /*
1676 Render label with a Postscript font.
1677 */
1678 if (image->debug != MagickFalse)
1679 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
cristye7f51092010-01-17 00:39:37 +00001680 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
cristy3ed852e2009-09-05 21:47:34 +00001681 draw_info->font : "none",draw_info->pointsize);
1682 file=(FILE *) NULL;
1683 unique_file=AcquireUniqueFileResource(filename);
1684 if (unique_file != -1)
1685 file=fdopen(unique_file,"wb");
1686 if ((unique_file == -1) || (file == (FILE *) NULL))
1687 {
cristy5cbc0162011-08-29 00:36:28 +00001688 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
cristy3ed852e2009-09-05 21:47:34 +00001689 return(MagickFalse);
1690 }
cristyb51dff52011-05-19 16:55:47 +00001691 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
1692 (void) FormatLocaleFile(file,"/ReencodeType\n");
1693 (void) FormatLocaleFile(file,"{\n");
1694 (void) FormatLocaleFile(file," findfont dup length\n");
1695 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001696 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
cristyb51dff52011-05-19 16:55:47 +00001697 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001698 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
cristyb51dff52011-05-19 16:55:47 +00001699 (void) FormatLocaleFile(file,"} bind def\n");
cristy3ed852e2009-09-05 21:47:34 +00001700 /*
1701 Sample to compute bounding box.
1702 */
cristyaa83c2c2011-09-21 13:36:25 +00001703 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
1704 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
1705 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001706 extent.x=0.0;
1707 extent.y=0.0;
cristy14d71292012-05-20 16:48:13 +00001708 length=strlen(draw_info->text);
1709 for (i=0; i <= (ssize_t) (length+2); i++)
cristy3ed852e2009-09-05 21:47:34 +00001710 {
1711 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
1712 draw_info->affine.ry*2.0*draw_info->pointsize);
1713 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
1714 draw_info->affine.sy*2.0*draw_info->pointsize);
1715 if (point.x > extent.x)
1716 extent.x=point.x;
1717 if (point.y > extent.y)
1718 extent.y=point.y;
1719 }
cristyb51dff52011-05-19 16:55:47 +00001720 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
cristy3ed852e2009-09-05 21:47:34 +00001721 extent.x/2.0,extent.y/2.0);
cristyb51dff52011-05-19 16:55:47 +00001722 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
cristy3ed852e2009-09-05 21:47:34 +00001723 draw_info->pointsize);
1724 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
1725 (strchr(draw_info->font,'/') != (char *) NULL))
cristyb51dff52011-05-19 16:55:47 +00001726 (void) FormatLocaleFile(file,
cristy3ed852e2009-09-05 21:47:34 +00001727 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
1728 else
cristy1e604812011-05-19 18:07:50 +00001729 (void) FormatLocaleFile(file,
1730 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
1731 draw_info->font);
cristyb51dff52011-05-19 16:55:47 +00001732 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
cristy8cd5b312010-01-07 01:10:24 +00001733 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
1734 draw_info->affine.sy);
cristy3ed852e2009-09-05 21:47:34 +00001735 text=EscapeParenthesis(draw_info->text);
1736 if (identity == MagickFalse)
cristy1e604812011-05-19 18:07:50 +00001737 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
1738 text);
cristyb51dff52011-05-19 16:55:47 +00001739 (void) FormatLocaleFile(file,"(%s) show\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001740 text=DestroyString(text);
cristyb51dff52011-05-19 16:55:47 +00001741 (void) FormatLocaleFile(file,"showpage\n");
cristy3ed852e2009-09-05 21:47:34 +00001742 (void) fclose(file);
cristyb51dff52011-05-19 16:55:47 +00001743 (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
cristye8c25f92010-06-03 00:53:06 +00001744 floor(extent.x+0.5),floor(extent.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +00001745 annotate_info=AcquireImageInfo();
cristyb51dff52011-05-19 16:55:47 +00001746 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
cristy3ed852e2009-09-05 21:47:34 +00001747 filename);
1748 (void) CloneString(&annotate_info->page,geometry);
1749 if (draw_info->density != (char *) NULL)
1750 (void) CloneString(&annotate_info->density,draw_info->density);
1751 annotate_info->antialias=draw_info->text_antialias;
cristy5cbc0162011-08-29 00:36:28 +00001752 annotate_image=ReadImage(annotate_info,exception);
1753 CatchException(exception);
cristy3ed852e2009-09-05 21:47:34 +00001754 annotate_info=DestroyImageInfo(annotate_info);
1755 (void) RelinquishUniqueFileResource(filename);
1756 if (annotate_image == (Image *) NULL)
1757 return(MagickFalse);
cristy55cb9f12012-08-23 09:02:28 +00001758 (void) NegateImage(annotate_image,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001759 resolution.x=DefaultResolution;
1760 resolution.y=DefaultResolution;
1761 if (draw_info->density != (char *) NULL)
1762 {
1763 GeometryInfo
1764 geometry_info;
1765
1766 MagickStatusType
1767 flags;
1768
1769 flags=ParseGeometry(draw_info->density,&geometry_info);
1770 resolution.x=geometry_info.rho;
1771 resolution.y=geometry_info.sigma;
1772 if ((flags & SigmaValue) == 0)
1773 resolution.y=resolution.x;
1774 }
1775 if (identity == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001776 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001777 else
1778 {
1779 RectangleInfo
1780 crop_info;
1781
cristy5cbc0162011-08-29 00:36:28 +00001782 crop_info=GetImageBoundingBox(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001783 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
cristy3ed852e2009-09-05 21:47:34 +00001784 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
cristybb503372010-05-27 20:51:26 +00001785 crop_info.y=(ssize_t) ceil((resolution.y/DefaultResolution)*extent.y/8.0-
cristy06609ee2010-03-17 20:21:27 +00001786 0.5);
cristyb51dff52011-05-19 16:55:47 +00001787 (void) FormatLocaleString(geometry,MaxTextExtent,
cristy6d8abba2010-06-03 01:10:47 +00001788 "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
cristye8c25f92010-06-03 00:53:06 +00001789 crop_info.height,(double) crop_info.x,(double) crop_info.y);
cristye941a752011-10-15 01:52:48 +00001790 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001791 }
1792 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
1793 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
1794 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
1795 metrics->ascent=metrics->pixels_per_em.x;
1796 metrics->descent=metrics->pixels_per_em.y/-5.0;
1797 metrics->width=(double) annotate_image->columns/
1798 ExpandAffine(&draw_info->affine);
1799 metrics->height=1.152*metrics->pixels_per_em.x;
1800 metrics->max_advance=metrics->pixels_per_em.x;
1801 metrics->bounds.x1=0.0;
1802 metrics->bounds.y1=metrics->descent;
1803 metrics->bounds.x2=metrics->ascent+metrics->descent;
1804 metrics->bounds.y2=metrics->ascent+metrics->descent;
1805 metrics->underline_position=(-2.0);
1806 metrics->underline_thickness=1.0;
1807 if (draw_info->render == MagickFalse)
1808 {
1809 annotate_image=DestroyImage(annotate_image);
1810 return(MagickTrue);
1811 }
cristy4c08aed2011-07-01 19:47:50 +00001812 if (draw_info->fill.alpha != TransparentAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001813 {
cristybd659482011-10-09 23:19:32 +00001814 CacheView
1815 *annotate_view;
1816
cristy3ed852e2009-09-05 21:47:34 +00001817 MagickBooleanType
1818 sync;
1819
cristy101ab702011-10-13 13:06:32 +00001820 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001821 fill_color;
1822
cristy3ed852e2009-09-05 21:47:34 +00001823 /*
1824 Render fill color.
1825 */
cristy8a46d822012-08-28 23:32:39 +00001826 if (image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001827 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
cristy8a46d822012-08-28 23:32:39 +00001828 if (annotate_image->alpha_trait != BlendPixelTrait)
cristy63240882011-08-05 19:05:27 +00001829 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
1830 exception);
1831 fill_color=draw_info->fill;
cristy46ff2672012-12-14 15:32:26 +00001832 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
cristybb503372010-05-27 20:51:26 +00001833 for (y=0; y < (ssize_t) annotate_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001834 {
cristybb503372010-05-27 20:51:26 +00001835 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001836 x;
1837
cristy4c08aed2011-07-01 19:47:50 +00001838 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001839 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001840
1841 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
1842 1,exception);
cristy6193b862011-08-06 16:33:59 +00001843 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001844 break;
cristybb503372010-05-27 20:51:26 +00001845 for (x=0; x < (ssize_t) annotate_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001846 {
cristy2ed42f62011-10-02 19:49:57 +00001847 (void) GetFillColor(draw_info,x,y,&fill_color,exception);
cristy55cb9f12012-08-23 09:02:28 +00001848 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
1849 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
cristy4c08aed2011-07-01 19:47:50 +00001850 SetPixelRed(annotate_image,fill_color.red,q);
1851 SetPixelGreen(annotate_image,fill_color.green,q);
1852 SetPixelBlue(annotate_image,fill_color.blue,q);
cristyed231572011-07-14 02:18:59 +00001853 q+=GetPixelChannels(annotate_image);
cristy3ed852e2009-09-05 21:47:34 +00001854 }
1855 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
1856 if (sync == MagickFalse)
1857 break;
1858 }
1859 annotate_view=DestroyCacheView(annotate_view);
cristy39172402012-03-30 13:04:39 +00001860 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
cristybb503372010-05-27 20:51:26 +00001861 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
cristye941a752011-10-15 01:52:48 +00001862 metrics->descent)-0.5),exception);
cristy3ed852e2009-09-05 21:47:34 +00001863 }
1864 annotate_image=DestroyImage(annotate_image);
1865 return(MagickTrue);
1866}
1867
1868/*
1869%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1870% %
1871% %
1872% %
1873+ R e n d e r X 1 1 %
1874% %
1875% %
1876% %
1877%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1878%
1879% RenderX11() renders text on the image with an X11 font. It also returns the
1880% bounding box of the text relative to the image.
1881%
1882% The format of the RenderX11 method is:
1883%
1884% MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
cristy5cbc0162011-08-29 00:36:28 +00001885% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001886%
1887% A description of each parameter follows:
1888%
1889% o image: the image.
1890%
1891% o draw_info: the draw info.
1892%
1893% o offset: (x,y) location of text relative to image.
1894%
1895% o metrics: bounding box of text.
1896%
cristy5cbc0162011-08-29 00:36:28 +00001897% o exception: return any errors or warnings in this structure.
1898%
cristy3ed852e2009-09-05 21:47:34 +00001899*/
cristycc168c22013-04-18 14:42:31 +00001900static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
1901 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001902{
1903 MagickBooleanType
1904 status;
1905
cristyd7ecaca2011-01-24 14:14:53 +00001906 if (annotate_semaphore == (SemaphoreInfo *) NULL)
cristy04b11db2014-02-16 15:10:39 +00001907 ActivateSemaphoreInfo(&annotate_semaphore);
cristyd7ecaca2011-01-24 14:14:53 +00001908 LockSemaphoreInfo(annotate_semaphore);
cristy534d0a32013-04-13 16:27:24 +00001909 status=XRenderImage(image,draw_info,offset,metrics,exception);
cristyd7ecaca2011-01-24 14:14:53 +00001910 UnlockSemaphoreInfo(annotate_semaphore);
cristy534d0a32013-04-13 16:27:24 +00001911 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001912}