MagickCore 7.1.2-32
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
annotate.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% AAA N N N N OOO TTTTT AAA TTTTT EEEEE %
7% A A NN N NN N O O T A A T E %
8% AAAAA N N N N N N O O T AAAAA T EEE %
9% A A N NN N NN O O T A A T E %
10% A A N N N N OOO T A A T EEEEE %
11% %
12% %
13% MagickCore Image Annotation Methods %
14% %
15% Software Design %
16% Cristy %
17% July 1992 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% 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% https://imagemagick.org/license/ %
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*/
45#include "MagickCore/studio.h"
46#include "MagickCore/annotate.h"
47#include "MagickCore/annotate-private.h"
48#include "MagickCore/attribute.h"
49#include "MagickCore/cache-private.h"
50#include "MagickCore/cache-view.h"
51#include "MagickCore/channel.h"
52#include "MagickCore/client.h"
53#include "MagickCore/color.h"
54#include "MagickCore/color-private.h"
55#include "MagickCore/colorspace-private.h"
56#include "MagickCore/composite.h"
57#include "MagickCore/composite-private.h"
58#include "MagickCore/constitute.h"
59#include "MagickCore/draw.h"
60#include "MagickCore/draw-private.h"
61#include "MagickCore/enhance.h"
62#include "MagickCore/exception.h"
63#include "MagickCore/exception-private.h"
64#include "MagickCore/gem.h"
65#include "MagickCore/geometry.h"
66#include "MagickCore/image-private.h"
67#include "MagickCore/log.h"
68#include "MagickCore/quantum.h"
69#include "MagickCore/quantum-private.h"
70#include "MagickCore/pixel-accessor.h"
71#include "MagickCore/policy.h"
72#include "MagickCore/property.h"
73#include "MagickCore/resource_.h"
74#include "MagickCore/semaphore.h"
75#include "MagickCore/statistic.h"
76#include "MagickCore/string_.h"
77#include "MagickCore/token.h"
78#include "MagickCore/token-private.h"
79#include "MagickCore/transform.h"
80#include "MagickCore/transform-private.h"
81#include "MagickCore/type.h"
82#include "MagickCore/utility.h"
83#include "MagickCore/utility-private.h"
84#include "MagickCore/xwindow.h"
85#include "MagickCore/xwindow-private.h"
86#include "coders/ghostscript-private.h"
87#if defined(MAGICKCORE_FREETYPE_DELEGATE)
88#if defined(__MINGW32__)
89# undef interface
90#endif
91#include <ft2build.h>
92#if defined(FT_FREETYPE_H)
93# include FT_FREETYPE_H
94#else
95# include <freetype/freetype.h>
96#endif
97#if defined(FT_GLYPH_H)
98# include FT_GLYPH_H
99#else
100# include <freetype/ftglyph.h>
101#endif
102#if defined(FT_OUTLINE_H)
103# include FT_OUTLINE_H
104#else
105# include <freetype/ftoutln.h>
106#endif
107#if defined(FT_BBOX_H)
108# include FT_BBOX_H
109#else
110# include <freetype/ftbbox.h>
111#endif
112#if defined(FT_MODULE_H)
113# include FT_MODULE_H
114#else
115# include <freetype/ftmodapi.h>
116#endif
117#endif
118#if defined(MAGICKCORE_RAQM_DELEGATE)
119#include <raqm.h>
120#endif
121typedef struct _GraphemeInfo
122{
123 ssize_t
124 index,
125 x_offset,
126 x_advance,
127 y_offset,
128 y_advance;
129
130 size_t
131 cluster;
132} GraphemeInfo;
133
134/*
135 Annotate semaphores.
136*/
137static SemaphoreInfo
138 *annotate_semaphore = (SemaphoreInfo *) NULL;
139
140/*
141 Forward declarations.
142*/
143static MagickBooleanType
144 RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
145 ExceptionInfo *),
146 RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
147 ExceptionInfo *),
148 RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
149 TypeMetric *,ExceptionInfo *),
150 RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *,
151 ExceptionInfo *);
152
153/*
154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155% %
156% %
157% %
158+ A n n o t a t e C o m p o n e n t G e n e s i s %
159% %
160% %
161% %
162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163%
164% AnnotateComponentGenesis() instantiates the annotate component.
165%
166% The format of the AnnotateComponentGenesis method is:
167%
168% MagickBooleanType AnnotateComponentGenesis(void)
169%
170*/
171MagickPrivate MagickBooleanType AnnotateComponentGenesis(void)
172{
173 if (annotate_semaphore == (SemaphoreInfo *) NULL)
174 annotate_semaphore=AcquireSemaphoreInfo();
175 return(MagickTrue);
176}
177
178/*
179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180% %
181% %
182% %
183+ 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 %
184% %
185% %
186% %
187%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188%
189% AnnotateComponentTerminus() destroys the annotate component.
190%
191% The format of the AnnotateComponentTerminus method is:
192%
193% AnnotateComponentTerminus(void)
194%
195*/
196MagickPrivate void AnnotateComponentTerminus(void)
197{
198 if (annotate_semaphore == (SemaphoreInfo *) NULL)
199 ActivateSemaphoreInfo(&annotate_semaphore);
200 RelinquishSemaphoreInfo(&annotate_semaphore);
201}
202
203/*
204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205% %
206% %
207% %
208% A n n o t a t e I m a g e %
209% %
210% %
211% %
212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213%
214% AnnotateImage() annotates an image with text.
215%
216% The format of the AnnotateImage method is:
217%
218% MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info,
219% ExceptionInfo *exception)
220%
221% A description of each parameter follows:
222%
223% o image: the image.
224%
225% o draw_info: the draw info.
226%
227% o exception: return any errors or warnings in this structure.
228%
229*/
230MagickExport MagickBooleanType AnnotateImage(Image *image,
231 const DrawInfo *draw_info,ExceptionInfo *exception)
232{
233 char
234 *p,
235 color[MagickPathExtent],
236 primitive[MagickPathExtent],
237 *text,
238 **textlist;
239
240 DrawInfo
241 *annotate,
242 *annotate_info;
243
244 GeometryInfo
245 geometry_info;
246
247 MagickBooleanType
248 status;
249
250 PixelInfo
251 pixel;
252
253 PointInfo
254 offset;
255
256 RectangleInfo
257 geometry;
258
259 size_t
260 height,
261 number_lines;
262
263 ssize_t
264 i;
265
266 TypeMetric
267 metrics;
268
269 assert(image != (Image *) NULL);
270 assert(image->signature == MagickCoreSignature);
271 if (IsEventLogging() != MagickFalse)
272 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
273 assert(draw_info != (DrawInfo *) NULL);
274 assert(draw_info->signature == MagickCoreSignature);
275 if (draw_info->text == (char *) NULL)
276 return(MagickFalse);
277 if (*draw_info->text == '\0')
278 return(MagickTrue);
279 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
280 text=annotate->text;
281 annotate->text=(char *) NULL;
282 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
283 number_lines=1;
284 for (p=text; *p != '\0'; p++)
285 if (*p == '\n')
286 number_lines++;
287 textlist=(char **) AcquireQuantumMemory(number_lines+1,sizeof(*textlist));
288 if (textlist == (char **) NULL)
289 {
290 annotate_info=DestroyDrawInfo(annotate_info);
291 annotate=DestroyDrawInfo(annotate);
292 text=DestroyString(text);
293 return(MagickFalse);
294 }
295 p=text;
296 for (i=0; i < (ssize_t) number_lines; i++)
297 {
298 char
299 *q;
300
301 textlist[i]=p;
302 for (q=p; *q != '\0'; q++)
303 if ((*q == '\r') || (*q == '\n'))
304 break;
305 if (*q == '\r')
306 {
307 *q='\0';
308 q++;
309 }
310 *q='\0';
311 p=q+1;
312 }
313 textlist[i]=(char *) NULL;
314 SetGeometry(image,&geometry);
315 SetGeometryInfo(&geometry_info);
316 if (annotate_info->geometry != (char *) NULL)
317 {
318 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
319 exception);
320 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
321 }
322 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
323 {
324 annotate_info=DestroyDrawInfo(annotate_info);
325 annotate=DestroyDrawInfo(annotate);
326 textlist=(char **) RelinquishMagickMemory(textlist);
327 text=DestroyString(text);
328 return(MagickFalse);
329 }
330 if (IsGrayColorspace(image->colorspace) != MagickFalse)
331 (void) SetImageColorspace(image,sRGBColorspace,exception);
332 status=MagickTrue;
333 (void) memset(&metrics,0,sizeof(metrics));
334 for (i=0; textlist[i] != (char *) NULL; i++)
335 {
336 if (*textlist[i] == '\0')
337 continue;
338 /*
339 Position text relative to image.
340 */
341 annotate_info->affine.tx=geometry_info.xi-(double) image->page.x;
342 annotate_info->affine.ty=geometry_info.psi-(double) image->page.y;
343 (void) CloneString(&annotate->text,textlist[i]);
344 if ((metrics.width == 0) || (annotate->gravity != NorthWestGravity))
345 (void) GetTypeMetrics(image,annotate,&metrics,exception);
346 height=CastDoubleToSizeT(metrics.ascent-metrics.descent+0.5);
347 if (height == 0)
348 height=(size_t) draw_info->pointsize;
349 height=CastDoubleToSizeT(floor((double) height+
350 draw_info->interline_spacing+0.5));
351 switch (annotate->gravity)
352 {
353 case UndefinedGravity:
354 default:
355 {
356 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
357 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
358 break;
359 }
360 case (GravityType) NorthWestGravity:
361 {
362 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
363 annotate_info->affine.ry*height+annotate_info->affine.ry*
364 (metrics.ascent+metrics.descent);
365 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
366 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
367 annotate_info->affine.sy*metrics.ascent;
368 break;
369 }
370 case (GravityType) NorthGravity:
371 {
372 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
373 geometry.width/2.0+i*annotate_info->affine.ry*height-
374 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
375 (metrics.ascent+metrics.descent);
376 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
377 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
378 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
379 metrics.width/2.0;
380 break;
381 }
382 case (GravityType) NorthEastGravity:
383 {
384 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
385 geometry.width+i*annotate_info->affine.ry*height-
386 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
387 (metrics.ascent+metrics.descent)-1.0;
388 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
389 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
390 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
391 metrics.width;
392 break;
393 }
394 case (GravityType) WestGravity:
395 {
396 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
397 annotate_info->affine.ry*height+annotate_info->affine.ry*
398 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
399 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
400 geometry.height/2.0+i*annotate_info->affine.sy*height+
401 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
402 (number_lines-1.0)*height)/2.0;
403 break;
404 }
405 case (GravityType) CenterGravity:
406 {
407 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
408 geometry.width/2.0+i*annotate_info->affine.ry*height-
409 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
410 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
411 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
412 geometry.height/2.0+i*annotate_info->affine.sy*height-
413 annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
414 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
415 break;
416 }
417 case (GravityType) EastGravity:
418 {
419 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
420 geometry.width+i*annotate_info->affine.ry*height-
421 annotate_info->affine.sx*metrics.width+
422 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
423 (number_lines-1.0)*height)/2.0-1.0;
424 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
425 geometry.height/2.0+i*annotate_info->affine.sy*height-
426 annotate_info->affine.rx*metrics.width+
427 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
428 (number_lines-1.0)*height)/2.0;
429 break;
430 }
431 case (GravityType) SouthWestGravity:
432 {
433 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
434 annotate_info->affine.ry*height-annotate_info->affine.ry*
435 (number_lines-1.0)*height;
436 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
437 geometry.height+i*annotate_info->affine.sy*height-
438 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
439 break;
440 }
441 case (GravityType) SouthGravity:
442 {
443 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
444 geometry.width/2.0+i*annotate_info->affine.ry*height-
445 annotate_info->affine.sx*metrics.width/2.0-
446 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
447 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
448 geometry.height+i*annotate_info->affine.sy*height-
449 annotate_info->affine.rx*metrics.width/2.0-
450 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
451 break;
452 }
453 case (GravityType) SouthEastGravity:
454 {
455 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
456 geometry.width+i*annotate_info->affine.ry*height-
457 annotate_info->affine.sx*metrics.width-
458 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
459 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
460 geometry.height+i*annotate_info->affine.sy*height-
461 annotate_info->affine.rx*metrics.width-
462 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
463 break;
464 }
465 }
466 switch (annotate->align)
467 {
468 case LeftAlign:
469 {
470 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
471 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
472 break;
473 }
474 case CenterAlign:
475 {
476 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
477 annotate_info->affine.sx*metrics.width/2.0;
478 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
479 annotate_info->affine.rx*metrics.width/2.0;
480 break;
481 }
482 case RightAlign:
483 {
484 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
485 annotate_info->affine.sx*metrics.width;
486 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
487 annotate_info->affine.rx*metrics.width;
488 break;
489 }
490 default:
491 break;
492 }
493 if (draw_info->undercolor.alpha != (MagickRealType) TransparentAlpha)
494 {
495 DrawInfo
496 *undercolor_info;
497
498 /*
499 Text box.
500 */
501 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
502 undercolor_info->fill=draw_info->undercolor;
503 undercolor_info->affine=draw_info->affine;
504 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
505 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
506 (void) FormatLocaleString(primitive,MagickPathExtent,
507 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(double) height);
508 (void) CloneString(&undercolor_info->primitive,primitive);
509 (void) DrawImage(image,undercolor_info,exception);
510 (void) DestroyDrawInfo(undercolor_info);
511 }
512 annotate_info->affine.tx=offset.x;
513 annotate_info->affine.ty=offset.y;
514 pixel=annotate_info->fill;
515 if (annotate_info->stroke.alpha != (MagickRealType) TransparentAlpha)
516 pixel=annotate_info->stroke;
517 (void) QueryColorname(image,&pixel,AllCompliance,color,exception);
518 (void) FormatLocaleString(primitive,MagickPathExtent,"stroke %s "
519 "stroke-width %g line 0,0 %g,0",color,(double)
520 metrics.underline_thickness,(double) metrics.width);
521 /*
522 Annotate image with text.
523 */
524 switch (annotate->decorate)
525 {
526 case OverlineDecoration:
527 {
528 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
529 metrics.descent-metrics.underline_position));
530 (void) CloneString(&annotate_info->primitive,primitive);
531 (void) DrawImage(image,annotate_info,exception);
532 break;
533 }
534 case UnderlineDecoration:
535 {
536 annotate_info->affine.ty-=(draw_info->affine.sy*
537 metrics.underline_position);
538 (void) CloneString(&annotate_info->primitive,primitive);
539 (void) DrawImage(image,annotate_info,exception);
540 break;
541 }
542 default:
543 break;
544 }
545 status=RenderType(image,annotate,&offset,&metrics,exception);
546 if (status == MagickFalse)
547 break;
548
549 if (annotate->decorate == LineThroughDecoration) {
550 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
551 metrics.underline_position+metrics.descent*2)/2.0);
552 (void) CloneString(&annotate_info->primitive,primitive);
553 (void) DrawImage(image,annotate_info,exception);
554 }
555 }
556 /*
557 Relinquish resources.
558 */
559 annotate_info=DestroyDrawInfo(annotate_info);
560 annotate=DestroyDrawInfo(annotate);
561 textlist=(char **) RelinquishMagickMemory(textlist);
562 text=DestroyString(text);
563 return(status);
564}
565
566/*
567%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568% %
569% %
570% %
571% F o r m a t M a g i c k C a p t i o n %
572% %
573% %
574% %
575%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576%
577% FormatMagickCaption() formats a caption so that it fits within the image
578% width. It returns the number of lines in the formatted caption.
579%
580% The format of the FormatMagickCaption method is:
581%
582% ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
583% const MagickBooleanType split,TypeMetric *metrics,char **caption,
584% ExceptionInfo *exception)
585%
586% A description of each parameter follows.
587%
588% o image: The image.
589%
590% o draw_info: the draw info.
591%
592% o split: when no convenient line breaks-- insert newline.
593%
594% o metrics: Return the font metrics in this structure.
595%
596% o caption: the caption.
597%
598% o exception: return any errors or warnings in this structure.
599%
600*/
601
602static inline char *ReplaceSpaceWithNewline(char **caption,char *space)
603{
604 size_t
605 octets;
606
607 if ((caption == (char **) NULL) || (space == (char *) NULL))
608 return((char *) NULL);
609 octets=(size_t) GetUTFOctets(space);
610 if (octets == 1)
611 *space='\n';
612 else
613 {
614 char
615 *target;
616
617 size_t
618 length;
619
620 ssize_t
621 offset;
622
623 length=strlen(*caption);
624 *space='\n';
625 offset=space-(*caption);
626 if (offset >= 0)
627 {
628 target=AcquireString(*caption);
629 (void) CopyMagickString(target,*caption,(size_t) offset+2);
630 (void) ConcatenateMagickString(target,space+octets,length);
631 (void) DestroyString(*caption);
632 *caption=target;
633 space=(*caption)+offset;
634 }
635 }
636 return(space);
637}
638
639MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
640 const MagickBooleanType split,TypeMetric *metrics,char **caption,
641 ExceptionInfo *exception)
642{
643 char
644 *p,
645 *q,
646 *s;
647
648 MagickBooleanType
649 status;
650
651 size_t
652 width;
653
654 ssize_t
655 i,
656 n;
657
658 q=draw_info->text;
659 s=(char *) NULL;
660 width=0;
661 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
662 {
663 int
664 code;
665
666 code=GetUTFCode(p);
667 if (code == '\n')
668 {
669 q=draw_info->text;
670 continue;
671 }
672 if ((draw_info->word_break != BreakWordBreakType) &&
673 (IsUTFSpace(code) != MagickFalse) &&
674 (IsNonBreakingUTFSpace(code) == MagickFalse))
675 {
676 if (width > image->columns)
677 p=ReplaceSpaceWithNewline(caption,p);
678 s=p;
679 }
680 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
681 *q++=(*(p+i));
682 *q='\0';
683 status=GetTypeMetrics(image,draw_info,metrics,exception);
684 if (status == MagickFalse)
685 break;
686 width=CastDoubleToSizeT(metrics->width+draw_info->stroke_width+0.5);
687 if (width <= image->columns)
688 continue;
689 if (s != (char *) NULL)
690 p=ReplaceSpaceWithNewline(caption,s);
691 else
692 if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
693 {
694 /*
695 No convenient line breaks-- insert newline.
696 */
697 n=p-(*caption);
698 if ((n > 0) && ((*caption)[n-1] != '\n'))
699 {
700 char
701 *target;
702
703 target=AcquireString(*caption);
704 (void) CopyMagickString(target,*caption,(size_t) n+1);
705 (void) ConcatenateMagickString(target,"\n",strlen(*caption)+1);
706 (void) ConcatenateMagickString(target,p,strlen(*caption)+2);
707 (void) DestroyString(*caption);
708 *caption=target;
709 p=(*caption)+n;
710 }
711 }
712 q=draw_info->text;
713 s=(char *) NULL;
714 }
715 n=0;
716 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
717 if (GetUTFCode(p) == '\n')
718 n++;
719 return(n);
720}
721
722/*
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724% %
725% %
726% %
727% G e t M u l t i l i n e T y p e M e t r i c s %
728% %
729% %
730% %
731%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732%
733% GetMultilineTypeMetrics() returns the following information for the
734% specified font and text:
735%
736% character width
737% character height
738% ascender
739% descender
740% text width
741% text height
742% maximum horizontal advance
743% bounds: x1
744% bounds: y1
745% bounds: x2
746% bounds: y2
747% origin: x
748% origin: y
749% underline position
750% underline thickness
751%
752% This method is like GetTypeMetrics() but it returns the maximum text width
753% and height for multiple lines of text.
754%
755% The format of the GetMultilineTypeMetrics method is:
756%
757% MagickBooleanType GetMultilineTypeMetrics(Image *image,
758% const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
759%
760% A description of each parameter follows:
761%
762% o image: the image.
763%
764% o draw_info: the draw info.
765%
766% o metrics: Return the font metrics in this structure.
767%
768% o exception: return any errors or warnings in this structure.
769%
770*/
771MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
772 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
773{
774 char
775 **textlist;
776
777 double
778 height;
779
780 DrawInfo
781 *annotate_info;
782
783 MagickBooleanType
784 status;
785
786 MagickSizeType
787 size;
788
789 ssize_t
790 i;
791
792 size_t
793 count;
794
795 TypeMetric
796 extent;
797
798 assert(image != (Image *) NULL);
799 assert(image->signature == MagickCoreSignature);
800 if (IsEventLogging() != MagickFalse)
801 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
802 assert(draw_info != (DrawInfo *) NULL);
803 assert(draw_info->text != (char *) NULL);
804 assert(draw_info->signature == MagickCoreSignature);
805 if (*draw_info->text == '\0')
806 {
807 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
808 "LabelExpected","`%s'",image->filename);
809 return(MagickFalse);
810 }
811 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
812 annotate_info->text=DestroyString(annotate_info->text);
813 /*
814 Convert newlines to multiple lines of text.
815 */
816 textlist=StringToStrings(draw_info->text,&count);
817 if (textlist == (char **) NULL)
818 {
819 annotate_info=DestroyDrawInfo(annotate_info);
820 return(MagickFalse);
821 }
822 annotate_info->render=MagickFalse;
823 annotate_info->direction=UndefinedDirection;
824 (void) memset(metrics,0,sizeof(*metrics));
825 (void) memset(&extent,0,sizeof(extent));
826 /*
827 Find the widest of the text lines.
828 */
829 annotate_info->text=textlist[0];
830 status=GetTypeMetrics(image,annotate_info,&extent,exception);
831 *metrics=extent;
832 height=(count*(size_t) (metrics->ascent-metrics->descent+
833 0.5)+(count-1)*draw_info->interline_spacing);
834 size=(MagickSizeType) fabs(height);
835 if (AcquireMagickResource(HeightResource,size) == MagickFalse)
836 {
837 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
838 "WidthOrHeightExceedsLimit","`%s'",image->filename);
839 status=MagickFalse;
840 }
841 else
842 {
843 for (i=1; i < (ssize_t) count; i++)
844 {
845 annotate_info->text=textlist[i];
846 status=GetTypeMetrics(image,annotate_info,&extent,exception);
847 if (status == MagickFalse)
848 break;
849 if (extent.width > metrics->width)
850 *metrics=extent;
851 size=(MagickSizeType) fabs(extent.width);
852 if (AcquireMagickResource(WidthResource,size) == MagickFalse)
853 {
854 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
855 "WidthOrHeightExceedsLimit","`%s'",image->filename);
856 status=MagickFalse;
857 break;
858 }
859 }
860 metrics->height=(double) height;
861 }
862 /*
863 Relinquish resources.
864 */
865 annotate_info->text=(char *) NULL;
866 annotate_info=DestroyDrawInfo(annotate_info);
867 for (i=0; i < (ssize_t) count; i++)
868 textlist[i]=DestroyString(textlist[i]);
869 textlist=(char **) RelinquishMagickMemory(textlist);
870 return(status);
871}
872
873/*
874%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
875% %
876% %
877% %
878% G e t T y p e M e t r i c s %
879% %
880% %
881% %
882%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
883%
884% GetTypeMetrics() returns the following information for the specified font
885% and text:
886%
887% character width
888% character height
889% ascender
890% descender
891% text width
892% text height
893% maximum horizontal advance
894% bounds: x1
895% bounds: y1
896% bounds: x2
897% bounds: y2
898% origin: x
899% origin: y
900% underline position
901% underline thickness
902%
903% The format of the GetTypeMetrics method is:
904%
905% MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
906% TypeMetric *metrics,ExceptionInfo *exception)
907%
908% A description of each parameter follows:
909%
910% o image: the image.
911%
912% o draw_info: the draw info.
913%
914% o metrics: Return the font metrics in this structure.
915%
916% o exception: return any errors or warnings in this structure.
917%
918*/
919MagickExport MagickBooleanType GetTypeMetrics(Image *image,
920 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
921{
922 DrawInfo
923 *annotate_info;
924
925 MagickBooleanType
926 status;
927
928 PointInfo
929 offset;
930
931 assert(image != (Image *) NULL);
932 assert(image->signature == MagickCoreSignature);
933 if (IsEventLogging() != MagickFalse)
934 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
935 assert(draw_info != (DrawInfo *) NULL);
936 assert(draw_info->text != (char *) NULL);
937 assert(draw_info->signature == MagickCoreSignature);
938 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
939 annotate_info->render=MagickFalse;
940 annotate_info->direction=UndefinedDirection;
941 (void) memset(metrics,0,sizeof(*metrics));
942 offset.x=0.0;
943 offset.y=0.0;
944 status=RenderType(image,annotate_info,&offset,metrics,exception);
945 if (draw_info->debug != MagickFalse)
946 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
947 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
948 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
949 "underline position: %g; underline thickness: %g",annotate_info->text,
950 metrics->width,metrics->height,metrics->ascent,metrics->descent,
951 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
952 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
953 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
954 metrics->underline_position,metrics->underline_thickness);
955 annotate_info=DestroyDrawInfo(annotate_info);
956 return(status);
957}
958
959/*
960%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
961% %
962% %
963% %
964+ R e n d e r T y p e %
965% %
966% %
967% %
968%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969%
970% RenderType() renders text on the image. It also returns the bounding box of
971% the text relative to the image.
972%
973% The format of the RenderType method is:
974%
975% MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
976% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
977%
978% A description of each parameter follows:
979%
980% o image: the image.
981%
982% o draw_info: the draw info.
983%
984% o offset: (x,y) location of text relative to image.
985%
986% o metrics: bounding box of text.
987%
988% o exception: return any errors or warnings in this structure.
989%
990*/
991static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
992 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
993{
994 char
995 *font;
996
997 const TypeInfo
998 *type_info;
999
1000 DrawInfo
1001 *annotate_info;
1002
1003 MagickBooleanType
1004 status;
1005
1006 type_info=(const TypeInfo *) NULL;
1007 if (draw_info->font != (char *) NULL)
1008 {
1009 if (*draw_info->font == '@')
1010 {
1011 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
1012 metrics,exception);
1013 return(status);
1014 }
1015 if (*draw_info->font == '-')
1016 return(RenderX11(image,draw_info,offset,metrics,exception));
1017 if (*draw_info->font == '^')
1018 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1019 if (IsPathAccessible(draw_info->font) != MagickFalse)
1020 {
1021 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
1022 metrics,exception);
1023 return(status);
1024 }
1025 type_info=GetTypeInfo(draw_info->font,exception);
1026 if (type_info == (const TypeInfo *) NULL)
1027 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1028 "UnableToReadFont","`%s'",draw_info->font);
1029 }
1030 if ((type_info == (const TypeInfo *) NULL) &&
1031 (draw_info->family != (const char *) NULL))
1032 {
1033 if (strpbrk(draw_info->family,",'\"") == (char *) NULL)
1034 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
1035 draw_info->stretch,draw_info->weight,exception);
1036 if (type_info == (const TypeInfo *) NULL)
1037 {
1038 char
1039 **family;
1040
1041 int
1042 number_families;
1043
1044 ssize_t
1045 i;
1046
1047 /*
1048 Parse font family list.
1049 */
1050 family=StringToArgv(draw_info->family,&number_families);
1051 for (i=1; i < (ssize_t) number_families; i++)
1052 {
1053 (void) SubstituteString(&family[i],",","");
1054 type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1055 draw_info->stretch,draw_info->weight,exception);
1056 if ((type_info != (const TypeInfo *) NULL) &&
1057 (LocaleCompare(family[i],type_info->family) == 0))
1058 break;
1059 }
1060 for (i=0; i < (ssize_t) number_families; i++)
1061 family[i]=DestroyString(family[i]);
1062 family=(char **) RelinquishMagickMemory(family);
1063 if (type_info == (const TypeInfo *) NULL)
1064 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1065 "UnableToReadFont","`%s'",draw_info->family);
1066 }
1067 }
1068 font=GetPolicyValue("system:font");
1069 if (font != (char *) NULL)
1070 {
1071 if (IsPathAccessible(font) != MagickFalse)
1072 {
1073 /*
1074 Render with default system font.
1075 */
1076 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1077 annotate_info->font=font;
1078 status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1079 offset,metrics,exception);
1080 annotate_info=DestroyDrawInfo(annotate_info);
1081 return(status);
1082 }
1083 font=DestroyString(font);
1084 }
1085 if (type_info == (const TypeInfo *) NULL)
1086 {
1087 ExceptionInfo
1088 *sans_exception;
1089
1090 /*
1091 Search for a default font.
1092 */
1093 sans_exception=AcquireExceptionInfo();
1094 if (type_info == (const TypeInfo *) NULL)
1095 type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
1096 draw_info->stretch,draw_info->weight,sans_exception);
1097 if (type_info == (const TypeInfo *) NULL)
1098 type_info=GetTypeInfo("*",sans_exception);
1099 sans_exception=DestroyExceptionInfo(sans_exception);
1100 }
1101 if (type_info == (const TypeInfo *) NULL)
1102 {
1103 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
1104 exception);
1105 return(status);
1106 }
1107 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1108 annotate_info->face=type_info->face;
1109 if (type_info->metrics != (char *) NULL)
1110 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1111 if (type_info->glyphs != (char *) NULL)
1112 (void) CloneString(&annotate_info->font,type_info->glyphs);
1113 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
1114 exception);
1115 annotate_info=DestroyDrawInfo(annotate_info);
1116 return(status);
1117}
1118
1119/*
1120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1121% %
1122% %
1123% %
1124+ R e n d e r F r e e t y p e %
1125% %
1126% %
1127% %
1128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1129%
1130% RenderFreetype() renders text on the image with a Truetype font. It also
1131% returns the bounding box of the text relative to the image.
1132%
1133% The format of the RenderFreetype method is:
1134%
1135% MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
1136% const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1137% ExceptionInfo *exception)
1138%
1139% A description of each parameter follows:
1140%
1141% o image: the image.
1142%
1143% o draw_info: the draw info.
1144%
1145% o encoding: the font encoding.
1146%
1147% o offset: (x,y) location of text relative to image.
1148%
1149% o metrics: bounding box of text.
1150%
1151% o exception: return any errors or warnings in this structure.
1152%
1153*/
1154
1155#if defined(MAGICKCORE_FREETYPE_DELEGATE)
1156
1157#if defined(MAGICKCORE_RAQM_DELEGATE)
1158static MagickBooleanType ContainsRaqmParagraphSeparator(const unsigned char *p,
1159 const size_t length)
1160{
1161 size_t
1162 i;
1163
1164 if ((p == (const unsigned char*) NULL) || (length == 0))
1165 return(MagickFalse);
1166
1167 for (i = 0; i < length; i++)
1168 {
1169 /* LF or CR */
1170 if ((p[i] == '\n') || (p[i] == '\r'))
1171 return(MagickTrue);
1172
1173 /* UTF-8 NEL U+0085 => C2 85 */
1174 if ((p[i] == 0xC2) && (i + 1 < length) && (p[i + 1] == 0x85))
1175 return(MagickTrue);
1176
1177 /* UTF-8 LS/PS U+2028/U+2029 => E2 80 A8/A9 */
1178 if ((p[i] == 0xE2) && (i + 2 < length) && (p[i + 1] == 0x80) &&
1179 ((p[i + 2] == 0xA8) || (p[i + 2] == 0xA9)))
1180 return(MagickTrue);
1181 }
1182
1183 return(MagickFalse);
1184}
1185
1186static size_t ComplexRaqmTextLayout(const Image *image,
1187 const DrawInfo *draw_info,const char *text,const size_t length,
1188 const FT_Face face,GraphemeInfo **grapheme,ExceptionInfo *exception)
1189{
1190 const char
1191 *features;
1192
1193 raqm_t
1194 *rq;
1195
1196 raqm_glyph_t
1197 *glyphs;
1198
1199 size_t
1200 extent;
1201
1202 ssize_t
1203 i;
1204
1205 extent=0;
1206 rq=raqm_create();
1207 if (rq == (raqm_t *) NULL)
1208 goto cleanup;
1209 if (raqm_set_text_utf8(rq,text,length) == 0)
1210 goto cleanup;
1211 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1212 goto cleanup;
1213 if (raqm_set_freetype_face(rq,face) == 0)
1214 goto cleanup;
1215 features=GetImageProperty(image,"type:features",exception);
1216 if (features != (const char *) NULL)
1217 {
1218 char
1219 breaker,
1220 quote,
1221 *token;
1222
1223 int
1224 next,
1225 status_token;
1226
1227 TokenInfo
1228 *token_info;
1229
1230 next=0;
1231 token_info=AcquireTokenInfo();
1232 token=AcquireString("");
1233 status_token=Tokenizer(token_info,0,token,50,features,"",",","",'\0',
1234 &breaker,&next,&quote);
1235 while (status_token == 0)
1236 {
1237 raqm_add_font_feature(rq,token,(int) strlen(token));
1238 status_token=Tokenizer(token_info,0,token,50,features,"",",","",'\0',
1239 &breaker,&next,&quote);
1240 }
1241 token_info=DestroyTokenInfo(token_info);
1242 token=DestroyString(token);
1243 }
1244 if (raqm_layout(rq) == 0)
1245 goto cleanup;
1246 glyphs=raqm_get_glyphs(rq,&extent);
1247 if (glyphs == (raqm_glyph_t *) NULL)
1248 {
1249 extent=0;
1250 goto cleanup;
1251 }
1252 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(extent,sizeof(**grapheme));
1253 if (*grapheme == (GraphemeInfo *) NULL)
1254 {
1255 extent=0;
1256 goto cleanup;
1257 }
1258 for (i=0; i < (ssize_t) extent; i++)
1259 {
1260 (*grapheme)[i].index=glyphs[i].index;
1261 (*grapheme)[i].x_offset=glyphs[i].x_offset;
1262 (*grapheme)[i].x_advance=glyphs[i].x_advance;
1263 (*grapheme)[i].y_offset=glyphs[i].y_offset;
1264 (*grapheme)[i].y_advance=glyphs[i].y_advance;
1265 (*grapheme)[i].cluster=glyphs[i].cluster;
1266 }
1267
1268cleanup:
1269 raqm_destroy(rq);
1270 return(extent);
1271}
1272#endif
1273
1274static size_t ComplexTextLayout(const DrawInfo *draw_info,const char *text,
1275 const size_t length,const FT_Face face,const FT_Int32 flags,
1276 GraphemeInfo **grapheme)
1277{
1278 const char
1279 *p;
1280
1281 ssize_t
1282 i;
1283
1284 ssize_t
1285 last_glyph;
1286
1287 /*
1288 Simple layout for bi-directional text (right-to-left or left-to-right).
1289 */
1290 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(length+1,sizeof(**grapheme));
1291 if (*grapheme == (GraphemeInfo *) NULL)
1292 return(0);
1293 last_glyph=0;
1294 p=text;
1295 for (i=0; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p), i++)
1296 {
1297 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,(FT_ULong)
1298 GetUTFCode(p));
1299 (*grapheme)[i].x_offset=0;
1300 (*grapheme)[i].y_offset=0;
1301 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1302 {
1303 if (FT_HAS_KERNING(face))
1304 {
1305 FT_Error
1306 ft_status;
1307
1308 FT_Vector
1309 kerning;
1310
1311 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1312 (*grapheme)[i].index,ft_kerning_default,&kerning);
1313 if (ft_status == 0)
1314 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1315 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1316 }
1317 }
1318 (void) FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1319 (*grapheme)[i].x_advance=face->glyph->advance.x;
1320 (*grapheme)[i].y_advance=face->glyph->advance.y;
1321 (*grapheme)[i].cluster=(size_t) (p-text);
1322 last_glyph=(*grapheme)[i].index;
1323 }
1324 return((size_t) i);
1325}
1326
1327static void FreetypeCloseStream(FT_Stream stream)
1328{
1329 FILE
1330 *file;
1331
1332 file=(FILE *) stream->descriptor.pointer;
1333 if (file != (FILE *) NULL)
1334 (void) fclose(file);
1335 stream->descriptor.pointer=NULL;
1336}
1337
1338static unsigned long FreetypeReadStream(FT_Stream stream,unsigned long offset,
1339 unsigned char *buffer,unsigned long count)
1340{
1341 FILE
1342 *file;
1343
1344 unsigned long
1345 result;
1346
1347 file=(FILE *) stream->descriptor.pointer;
1348 if (file == (FILE *) NULL)
1349 return(0);
1350 if (offset > stream->size)
1351 result=1;
1352 else
1353 result=(unsigned long) fseek(file,(off_t) offset,SEEK_SET);
1354 if (count == 0) /* seek operation */
1355 return(result);
1356 if (result != 0)
1357 return(0);
1358 return((unsigned long) fread(buffer,1,count,file));
1359}
1360
1361static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1362{
1363 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1364 MagickFalse);
1365}
1366
1367static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1368 DrawInfo *draw_info)
1369{
1370 AffineMatrix
1371 affine;
1372
1373 char
1374 path[MagickPathExtent];
1375
1376 affine=draw_info->affine;
1377 (void) FormatLocaleString(path,MagickPathExtent,"C%g,%g %g,%g %g,%g",
1378 affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-
1379 q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1380 (void) ConcatenateString(&draw_info->primitive,path);
1381 return(0);
1382}
1383
1384static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1385{
1386 AffineMatrix
1387 affine;
1388
1389 char
1390 path[MagickPathExtent];
1391
1392 affine=draw_info->affine;
1393 (void) FormatLocaleString(path,MagickPathExtent,"L%g,%g",affine.tx+to->x/64.0,
1394 affine.ty-to->y/64.0);
1395 (void) ConcatenateString(&draw_info->primitive,path);
1396 return(0);
1397}
1398
1399static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1400{
1401 AffineMatrix
1402 affine;
1403
1404 char
1405 path[MagickPathExtent];
1406
1407 affine=draw_info->affine;
1408 (void) FormatLocaleString(path,MagickPathExtent,"M%g,%g",affine.tx+to->x/64.0,
1409 affine.ty-to->y/64.0);
1410 (void) ConcatenateString(&draw_info->primitive,path);
1411 return(0);
1412}
1413
1414static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1415 DrawInfo *draw_info)
1416{
1417 AffineMatrix
1418 affine;
1419
1420 char
1421 path[MagickPathExtent];
1422
1423 affine=draw_info->affine;
1424 (void) FormatLocaleString(path,MagickPathExtent,"Q%g,%g %g,%g",affine.tx+
1425 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1426 to->y/64.0);
1427 (void) ConcatenateString(&draw_info->primitive,path);
1428 return(0);
1429}
1430
1431#if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1432static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1433{
1434 return(FT_Error_String(ft_status));
1435#else
1436static inline const char *FreetypeErrorMessage(
1437 FT_Error magick_unused(ft_status))
1438{
1439 magick_unreferenced(ft_status);
1440 return((const char *) NULL);
1441#endif
1442}
1443
1444static void *FreetypeAlloc(FT_Memory magick_unused(memory),long extent)
1445{
1446 magick_unreferenced(memory);
1447 if (extent <= 0)
1448 return((void *) NULL);
1449 return(AcquireQuantumMemory((size_t) extent,1));
1450}
1451
1452static void *FreetypeRealloc(FT_Memory magick_unused(memory),long length,
1453 long extent,void *block)
1454{
1455 void
1456 *cloned_block,
1457 *new_block;
1458
1459 magick_unreferenced(memory);
1460 if (extent <= 0)
1461 return(RelinquishMagickMemory(block));
1462 if (block == (void *) NULL)
1463 return(AcquireQuantumMemory((size_t) extent,1));
1464 cloned_block=AcquireQuantumMemory((size_t) length,1);
1465 if (cloned_block == (void *) NULL)
1466 return((void *) NULL);
1467 (void) memcpy(cloned_block,block,(size_t) length);
1468 new_block=ResizeQuantumMemory(cloned_block,(size_t) extent,1);
1469 if (new_block == (void *) NULL)
1470 return((void *) NULL);
1471 block=RelinquishMagickMemory(block);
1472 return(new_block);
1473}
1474
1475static void FreetypeFree(FT_Memory magick_unused(memory),void *block)
1476{
1477 magick_unreferenced(memory);
1478 (void) RelinquishMagickMemory(block);
1479}
1480
1481static FT_Memory FreetypeAcquireMemoryManager(void)
1482{
1483 FT_Memory
1484 memory;
1485
1486 memory=(FT_Memory) AcquireCriticalMemory(sizeof(*memory));
1487 memset(memory,0,sizeof(*memory));
1488 memory->alloc=(&FreetypeAlloc);
1489 memory->realloc=(&FreetypeRealloc);
1490 memory->free=(&FreetypeFree);
1491 return(memory);
1492}
1493
1494static void FreetypeDone(FT_Memory memory,FT_Library library,
1495 FT_StreamRec *stream)
1496{
1497 (void) FT_Done_Library(library);
1498 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1499 memory=(FT_Memory) RelinquishMagickMemory(memory);
1500}
1501
1502static FT_Error FreetypeInit(FT_Memory memory,FT_Library *alibrary)
1503{
1504 FT_Error
1505 ft_status;
1506
1507 ft_status=FT_New_Library(memory,alibrary);
1508 if (ft_status != 0)
1509 RelinquishMagickMemory(memory);
1510 else
1511 FT_Add_Default_Modules(*alibrary);
1512 FT_Set_Default_Properties(*alibrary);
1513 return(ft_status);
1514}
1515
1516static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1517 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1518 ExceptionInfo *exception)
1519{
1520#if !defined(FT_OPEN_PATHNAME)
1521#define FT_OPEN_PATHNAME ft_open_pathname
1522#endif
1523
1524#define ThrowFreetypeErrorException(tag,ft_status,value) \
1525{ \
1526 const char *error_string = FreetypeErrorMessage(ft_status); \
1527 if (error_string != (const char *) NULL) \
1528 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1529 tag,"`%s (%s)'",value, error_string); \
1530 else \
1531 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1532 tag,"`%s'",value); \
1533}
1534
1535 typedef struct _GlyphInfo
1536 {
1537 FT_UInt
1538 id;
1539
1540 FT_Vector
1541 origin;
1542
1543 FT_Glyph
1544 image;
1545 } GlyphInfo;
1546
1547 char
1548 *p;
1549
1550 const char
1551 *value;
1552
1553 DrawInfo
1554 *annotate_info;
1555
1556 FT_BBox
1557 bounds;
1558
1559 FT_BitmapGlyph
1560 bitmap;
1561
1562 FT_Encoding
1563 encoding_type;
1564
1565 FT_Error
1566 ft_status;
1567
1568 FT_Face
1569 face;
1570
1571 FT_Int32
1572 flags;
1573
1574 FT_Library
1575 library;
1576
1577 FT_Long
1578 face_index;
1579
1580 FT_Matrix
1581 affine;
1582
1583 FT_Memory
1584 memory;
1585
1586 FT_Open_Args
1587 args;
1588
1589 FT_StreamRec
1590 *stream;
1591
1592 FT_UInt
1593 first_glyph_id,
1594 last_glyph_id,
1595 missing_glyph_id;
1596
1597 FT_Vector
1598 origin;
1599
1600 GlyphInfo
1601 glyph;
1602
1603 GraphemeInfo
1604 *grapheme;
1605
1606 MagickBooleanType
1607 status;
1608
1609 PointInfo
1610 point,
1611 resolution;
1612
1613 ssize_t
1614 i;
1615
1616 size_t
1617 length;
1618
1619 ssize_t
1620 code,
1621 last_character,
1622 y;
1623
1624 static FT_Outline_Funcs
1625 OutlineMethods =
1626 {
1627 (FT_Outline_MoveTo_Func) TraceMoveTo,
1628 (FT_Outline_LineTo_Func) TraceLineTo,
1629 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1630 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1631 0, 0
1632 };
1633
1634 struct stat
1635 attributes;
1636
1637 unsigned char
1638 *utf8;
1639
1640 /*
1641 Initialize Truetype library.
1642 */
1643 if ((draw_info->font != (char *) NULL) && (*draw_info->font == '@') &&
1644 (IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,draw_info->font) == MagickFalse))
1645 ThrowPolicyException(draw_info->font,MagickFalse);
1646 memory=FreetypeAcquireMemoryManager();
1647 if (memory == (FT_Memory) NULL)
1648 ThrowBinaryException(ResourceLimitError,"UnableToInitializeFreetypeLibrary",
1649 image->filename);
1650 ft_status=FreetypeInit(memory,&library);
1651 if (ft_status != 0)
1652 {
1653 ThrowFreetypeErrorException("UnableToInitializeFreetypeLibrary",ft_status,
1654 image->filename);
1655 return(MagickFalse);
1656 }
1657 /*
1658 Open font face.
1659 */
1660 face_index=(FT_Long) draw_info->face;
1661 (void) memset(&args,0,sizeof(args));
1662 if (draw_info->font == (char *) NULL)
1663 {
1664 const TypeInfo *type_info = GetTypeInfo("*",exception);
1665 if (type_info != (const TypeInfo *) NULL)
1666 args.pathname=ConstantString(type_info->glyphs);
1667 }
1668 else
1669 if (*draw_info->font != '@')
1670 args.pathname=ConstantString(draw_info->font);
1671 else
1672 {
1673 /*
1674 Extract face index, e.g. @msgothic[1].
1675 */
1676 ImageInfo *image_info = AcquireImageInfo();
1677 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1678 MagickPathExtent);
1679 (void) SetImageInfo(image_info,0,exception);
1680 face_index=(FT_Long) image_info->scene;
1681 args.pathname=ConstantString(image_info->filename);
1682 image_info=DestroyImageInfo(image_info);
1683 }
1684 /*
1685 Configure streaming interface.
1686 */
1687 stream=(FT_StreamRec *) AcquireCriticalMemory(sizeof(*stream));
1688 (void) memset(stream,0,sizeof(*stream));
1689 if (stat(args.pathname,&attributes) == 0)
1690 stream->size=attributes.st_size >= 0 ? (unsigned long)
1691 attributes.st_size : 0;
1692 stream->descriptor.pointer=fopen_utf8(args.pathname,"rb");
1693 stream->read=(&FreetypeReadStream);
1694 stream->close=(&FreetypeCloseStream);
1695 args.flags=FT_OPEN_STREAM;
1696 args.stream=stream;
1697 face=(FT_Face) NULL;
1698 ft_status=FT_Open_Face(library,&args,face_index,&face);
1699 if (ft_status != 0)
1700 {
1701 FreetypeDone(memory,library,stream);
1702 ThrowFreetypeErrorException("UnableToReadFont",ft_status,args.pathname);
1703 args.pathname=DestroyString(args.pathname);
1704 return(MagickFalse);
1705 }
1706 args.pathname=DestroyString(args.pathname);
1707 if ((draw_info->metrics != (char *) NULL) &&
1708 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1709 (void) FT_Attach_File(face,draw_info->metrics);
1710 encoding_type=FT_ENCODING_UNICODE;
1711 ft_status=FT_Select_Charmap(face,encoding_type);
1712 if ((ft_status != 0) && (face->num_charmaps != 0))
1713 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1714 if (encoding != (const char *) NULL)
1715 {
1716 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1717 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1718 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1719 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1720 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1721 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1722 if (LocaleCompare(encoding,"AppleRoman") == 0)
1723 encoding_type=FT_ENCODING_APPLE_ROMAN;
1724 if (LocaleCompare(encoding,"BIG5") == 0)
1725 encoding_type=FT_ENCODING_BIG5;
1726#if defined(FT_ENCODING_PRC)
1727 if (LocaleCompare(encoding,"GB2312") == 0)
1728 encoding_type=FT_ENCODING_PRC;
1729#endif
1730#if defined(FT_ENCODING_JOHAB)
1731 if (LocaleCompare(encoding,"Johab") == 0)
1732 encoding_type=FT_ENCODING_JOHAB;
1733#endif
1734#if defined(FT_ENCODING_ADOBE_LATIN_1)
1735 if (LocaleCompare(encoding,"Latin-1") == 0)
1736 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1737#endif
1738#if defined(FT_ENCODING_ADOBE_LATIN_2)
1739 if (LocaleCompare(encoding,"Latin-2") == 0)
1740 encoding_type=FT_ENCODING_OLD_LATIN_2;
1741#endif
1742 if (LocaleCompare(encoding,"None") == 0)
1743 encoding_type=FT_ENCODING_NONE;
1744 if (LocaleCompare(encoding,"SJIScode") == 0)
1745 encoding_type=FT_ENCODING_SJIS;
1746 if (LocaleCompare(encoding,"Symbol") == 0)
1747 encoding_type=FT_ENCODING_MS_SYMBOL;
1748 if (LocaleCompare(encoding,"Unicode") == 0)
1749 encoding_type=FT_ENCODING_UNICODE;
1750 if (LocaleCompare(encoding,"Wansung") == 0)
1751 encoding_type=FT_ENCODING_WANSUNG;
1752 ft_status=FT_Select_Charmap(face,encoding_type);
1753 if (ft_status != 0)
1754 {
1755 (void) FT_Done_Face(face);
1756 FreetypeDone(memory,library,stream);
1757 ThrowFreetypeErrorException("UnrecognizedFontEncoding",ft_status,
1758 encoding);
1759 return(MagickFalse);
1760 }
1761 }
1762 /*
1763 Set text size.
1764 */
1765 resolution.x=DefaultResolution;
1766 resolution.y=DefaultResolution;
1767 if (draw_info->density != (char *) NULL)
1768 {
1769 GeometryInfo
1770 geometry_info;
1771
1772 MagickStatusType
1773 geometry_flags;
1774
1775 geometry_flags=ParseGeometry(draw_info->density,&geometry_info);
1776 if ((geometry_flags & RhoValue) != 0)
1777 resolution.x=geometry_info.rho;
1778 resolution.y=resolution.x;
1779 if ((geometry_flags & SigmaValue) != 0)
1780 resolution.y=geometry_info.sigma;
1781 }
1782 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1783 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1784 (FT_UInt) resolution.y);
1785 if (ft_status != 0)
1786 {
1787 (void) FT_Done_Face(face);
1788 FreetypeDone(memory,library,stream);
1789 ThrowFreetypeErrorException("UnableToReadFont",ft_status,
1790 draw_info->font);
1791 return(MagickFalse);
1792 }
1793 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1794 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1795 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1796 metrics->descent=(double) face->size->metrics.descender/64.0;
1797 if (face->size->metrics.ascender == 0)
1798 {
1799 /*
1800 Sanitize buggy ascender and descender values.
1801 */
1802 metrics->ascent=face->size->metrics.y_ppem;
1803 if (face->size->metrics.descender == 0)
1804 metrics->descent=face->size->metrics.y_ppem/-3.5;
1805 }
1806 metrics->width=0;
1807 metrics->origin.x=0;
1808 metrics->origin.y=0;
1809 metrics->height=(double) face->size->metrics.height/64.0;
1810 metrics->max_advance=0.0;
1811 if (face->size->metrics.max_advance > MagickEpsilon)
1812 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1813 metrics->bounds.x1=0.0;
1814 metrics->bounds.y1=metrics->descent;
1815 metrics->bounds.x2=metrics->ascent+metrics->descent;
1816 metrics->bounds.y2=metrics->ascent+metrics->descent;
1817 metrics->underline_position=face->underline_position*
1818 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1819 metrics->underline_thickness=face->underline_thickness*
1820 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1821 first_glyph_id=0;
1822 FT_Get_First_Char(face,&first_glyph_id);
1823 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0') ||
1824 (first_glyph_id == 0))
1825 {
1826 (void) FT_Done_Face(face);
1827 FreetypeDone(memory,library,stream);
1828 return(MagickTrue);
1829 }
1830 /*
1831 Compute bounding box.
1832 */
1833 if (draw_info->debug != MagickFalse)
1834 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1835 "font-encoding %s; text-encoding %s; pointsize %g",
1836 draw_info->font != (char *) NULL ? draw_info->font : "none",
1837 encoding != (char *) NULL ? encoding : "none",
1838 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1839 draw_info->pointsize);
1840 flags=FT_LOAD_DEFAULT;
1841 if (draw_info->render == MagickFalse)
1842 flags=FT_LOAD_NO_BITMAP;
1843 if (draw_info->text_antialias == MagickFalse)
1844 flags|=FT_LOAD_TARGET_MONO;
1845 else
1846 {
1847#if defined(FT_LOAD_TARGET_LIGHT)
1848 flags|=FT_LOAD_TARGET_LIGHT;
1849#elif defined(FT_LOAD_TARGET_LCD)
1850 flags|=FT_LOAD_TARGET_LCD;
1851#endif
1852 }
1853 value=GetImageProperty(image,"type:hinting",exception);
1854 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1855 flags|=FT_LOAD_NO_HINTING;
1856 glyph.id=0;
1857 glyph.image=(FT_Glyph) NULL;
1858 last_glyph_id=0;
1859 origin.x=0;
1860 origin.y=0;
1861 affine.xx=65536L;
1862 affine.yx=0L;
1863 affine.xy=0L;
1864 affine.yy=65536L;
1865 if (draw_info->render != MagickFalse)
1866 {
1867 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1868 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1869 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1870 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1871 }
1872 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1873 if (annotate_info->dash_pattern != (double *) NULL)
1874 annotate_info->dash_pattern[0]=0.0;
1875 (void) CloneString(&annotate_info->primitive,"path '");
1876 status=MagickTrue;
1877 if (draw_info->render != MagickFalse)
1878 {
1879 if (image->storage_class != DirectClass)
1880 (void) SetImageStorageClass(image,DirectClass,exception);
1881 if ((image->alpha_trait & BlendPixelTrait) == 0)
1882 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1883 }
1884 for (p=draw_info->text; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
1885 if (GetUTFCode(p) < 0)
1886 break;
1887 utf8=(unsigned char *) NULL;
1888 if (GetUTFCode(p) == 0)
1889 p=draw_info->text;
1890 else
1891 {
1892 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1893 if (utf8 != (unsigned char *) NULL)
1894 p=(char *) utf8;
1895 }
1896 grapheme=(GraphemeInfo *) NULL;
1897 length=strlen(p);
1898#if defined(MAGICKCORE_RAQM_DELEGATE)
1899 if (ContainsRaqmParagraphSeparator((const unsigned char *) p,length) == MagickFalse)
1900 length=ComplexRaqmTextLayout(image,draw_info,p,length,face,&grapheme,
1901 exception);
1902 else
1903#endif
1904 length=ComplexTextLayout(draw_info,p,length,face,flags,&grapheme);
1905 missing_glyph_id=FT_Get_Char_Index(face,' ');
1906 code=0;
1907 last_character=(ssize_t) length-1;
1908 for (i=0; i < (ssize_t) length; i++)
1909 {
1910 FT_Outline
1911 outline;
1912
1913 /*
1914 Render UTF-8 sequence.
1915 */
1916 glyph.id=(FT_UInt) grapheme[i].index;
1917 if (glyph.id == 0)
1918 glyph.id=missing_glyph_id;
1919 if ((glyph.id != 0) && (last_glyph_id != 0))
1920 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1921 glyph.origin=origin;
1922 glyph.origin.x+=(FT_Pos) grapheme[i].x_offset;
1923 glyph.origin.y+=(FT_Pos) grapheme[i].y_offset;
1924 if (glyph.image != (FT_Glyph) NULL)
1925 {
1926 FT_Done_Glyph(glyph.image);
1927 glyph.image=(FT_Glyph) NULL;
1928 }
1929 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1930 if (ft_status != 0)
1931 continue;
1932 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1933 if (ft_status != 0)
1934 continue;
1935 outline=((FT_OutlineGlyph) glyph.image)->outline;
1936 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1937 (IsEmptyOutline(outline) == MagickFalse))
1938 continue;
1939 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1940 if (ft_status != 0)
1941 continue;
1942 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1943 metrics->bounds.x1=(double) bounds.xMin;
1944 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1945 metrics->bounds.y1=(double) bounds.yMin;
1946 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1947 metrics->bounds.x2=(double) bounds.xMax;
1948 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1949 metrics->bounds.y2=(double) bounds.yMax;
1950 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
1951 (draw_info->stroke_pattern != (Image *) NULL)) &&
1952 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1953 {
1954 /*
1955 Trace the glyph.
1956 */
1957 annotate_info->affine.tx=glyph.origin.x/64.0;
1958 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1959 if (IsEmptyOutline(outline) == MagickFalse)
1960 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1961 annotate_info);
1962 }
1963 FT_Vector_Transform(&glyph.origin,&affine);
1964 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1965 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1966 (FT_Vector *) NULL,MagickTrue);
1967 if (ft_status != 0)
1968 continue;
1969 bitmap=(FT_BitmapGlyph) glyph.image;
1970 point.x=offset->x+bitmap->left;
1971 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1972 point.x+=(origin.x/64.0);
1973 point.y=offset->y-bitmap->top;
1974 if (draw_info->render != MagickFalse)
1975 {
1976 CacheView
1977 *image_view;
1978
1979 MagickBooleanType
1980 transparent_fill;
1981
1982 unsigned char
1983 *r;
1984
1985 /*
1986 Rasterize the glyph.
1987 */
1988 transparent_fill=((draw_info->fill.alpha == (MagickRealType) TransparentAlpha) &&
1989 (draw_info->fill_pattern == (Image *) NULL) &&
1990 (draw_info->stroke.alpha == (MagickRealType) TransparentAlpha) &&
1991 (draw_info->stroke_pattern == (Image *) NULL)) ? MagickTrue :
1992 MagickFalse;
1993 r=bitmap->bitmap.buffer;
1994 image_view=AcquireAuthenticCacheView(image,exception);
1995#if defined(MAGICKCORE_OPENMP_SUPPORT)
1996 #pragma omp parallel for schedule(static) shared(status) \
1997 magick_number_threads(image,image,bitmap->bitmap.rows,4)
1998#endif
1999 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
2000 {
2001 double
2002 fill_opacity;
2003
2004 MagickBooleanType
2005 active,
2006 sync;
2007
2008 PixelInfo
2009 fill_color;
2010
2011 Quantum
2012 *magick_restrict q;
2013
2014 ssize_t
2015 x;
2016
2017 ssize_t
2018 n,
2019 x_offset,
2020 y_offset;
2021
2022 if (status == MagickFalse)
2023 continue;
2024 x_offset=CastDoubleToSsizeT(ceil(point.x-0.5));
2025 y_offset=CastDoubleToSsizeT(ceil(point.y+y-0.5));
2026 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
2027 continue;
2028 q=(Quantum *) NULL;
2029 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
2030 active=MagickFalse;
2031 else
2032 {
2033 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
2034 bitmap->bitmap.width,1,exception);
2035 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
2036 }
2037 n=y*bitmap->bitmap.pitch;
2038 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
2039 {
2040 x_offset=CastDoubleToSsizeT(ceil(point.x+x-0.5));
2041 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
2042 {
2043 if (q != (Quantum *) NULL)
2044 q+=(ptrdiff_t) GetPixelChannels(image);
2045 continue;
2046 }
2047 fill_opacity=1.0;
2048 if (bitmap->bitmap.buffer != (unsigned char *) NULL)
2049 {
2050 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
2051 fill_opacity=(double) (r[n])/(bitmap->bitmap.num_grays-1);
2052 else
2053 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
2054 fill_opacity=((r[(x >> 3)+y*bitmap->bitmap.pitch] &
2055 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
2056 }
2057 if (draw_info->text_antialias == MagickFalse)
2058 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
2059 if (active == MagickFalse)
2060 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
2061 exception);
2062 if (q == (Quantum *) NULL)
2063 continue;
2064 if (transparent_fill == MagickFalse)
2065 {
2066 GetPixelInfo(image,&fill_color);
2067 GetFillColor(draw_info,x_offset,y_offset,&fill_color,exception);
2068 fill_opacity=fill_opacity*fill_color.alpha;
2069 CompositePixelOver(image,&fill_color,fill_opacity,q,
2070 GetPixelAlpha(image,q),q);
2071 }
2072 else
2073 {
2074 double
2075 Sa,
2076 Da;
2077
2078 Da=1.0-(QuantumScale*(double) GetPixelAlpha(image,q));
2079 Sa=fill_opacity;
2080 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*(double)
2081 QuantumRange;
2082 SetPixelAlpha(image,(const Quantum) fill_opacity,q);
2083 }
2084 if (active == MagickFalse)
2085 {
2086 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2087 if (sync == MagickFalse)
2088 status=MagickFalse;
2089 }
2090 q+=(ptrdiff_t) GetPixelChannels(image);
2091 }
2092 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2093 if (sync == MagickFalse)
2094 status=MagickFalse;
2095 }
2096 image_view=DestroyCacheView(image_view);
2097 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
2098 (draw_info->stroke_pattern != (Image *) NULL)) &&
2099 (status != MagickFalse))
2100 {
2101 /*
2102 Draw text stroke.
2103 */
2104 annotate_info->linejoin=RoundJoin;
2105 annotate_info->affine.tx=offset->x;
2106 annotate_info->affine.ty=offset->y;
2107 (void) ConcatenateString(&annotate_info->primitive,"'");
2108 if (strlen(annotate_info->primitive) > 7)
2109 (void) DrawImage(image,annotate_info,exception);
2110 (void) CloneString(&annotate_info->primitive,"path '");
2111 }
2112 }
2113 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
2114 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
2115 (IsUTFSpace((int) code) == MagickFalse))
2116 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
2117 else
2118 if (i == last_character)
2119 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
2120 else
2121 origin.x+=(FT_Pos) grapheme[i].x_advance;
2122 origin.y+=(FT_Pos) grapheme[i].y_advance;
2123 metrics->origin.x=(double) origin.x;
2124 metrics->origin.y=(double) origin.y;
2125 if (metrics->origin.x > metrics->width)
2126 metrics->width=metrics->origin.x;
2127 last_glyph_id=glyph.id;
2128 code=GetUTFCode(p+grapheme[i].cluster);
2129 }
2130 if (grapheme != (GraphemeInfo *) NULL)
2131 grapheme=(GraphemeInfo *) RelinquishMagickMemory(grapheme);
2132 if (utf8 != (unsigned char *) NULL)
2133 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
2134 if (glyph.image != (FT_Glyph) NULL)
2135 {
2136 FT_Done_Glyph(glyph.image);
2137 glyph.image=(FT_Glyph) NULL;
2138 }
2139 /*
2140 Determine font metrics.
2141 */
2142 metrics->bounds.x1/=64.0;
2143 metrics->bounds.y1/=64.0;
2144 metrics->bounds.x2/=64.0;
2145 metrics->bounds.y2/=64.0;
2146 metrics->origin.x/=64.0;
2147 metrics->origin.y/=64.0;
2148 metrics->width=ceil(metrics->width/64.0);
2149 /*
2150 Relinquish resources.
2151 */
2152 annotate_info=DestroyDrawInfo(annotate_info);
2153 (void) FT_Done_Face(face);
2154 FreetypeDone(memory,library,stream);
2155 return(status);
2156}
2157#else
2158static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
2159 const char *magick_unused(encoding),const PointInfo *offset,
2160 TypeMetric *metrics,ExceptionInfo *exception)
2161{
2162 magick_unreferenced(encoding);
2163 (void) ThrowMagickException(exception,GetMagickModule(),
2164 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
2165 draw_info->font != (char *) NULL ? draw_info->font : "none");
2166 return(RenderPostscript(image,draw_info,offset,metrics,exception));
2167}
2168#endif
2169
2170/*
2171%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2172% %
2173% %
2174% %
2175+ R e n d e r P o s t s c r i p t %
2176% %
2177% %
2178% %
2179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2180%
2181% RenderPostscript() renders text on the image with a Postscript font. It
2182% also returns the bounding box of the text relative to the image.
2183%
2184% The format of the RenderPostscript method is:
2185%
2186% MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
2187% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2188%
2189% A description of each parameter follows:
2190%
2191% o image: the image.
2192%
2193% o draw_info: the draw info.
2194%
2195% o offset: (x,y) location of text relative to image.
2196%
2197% o metrics: bounding box of text.
2198%
2199% o exception: return any errors or warnings in this structure.
2200%
2201*/
2202
2203static MagickBooleanType IsValidPostscriptFontname(const char *font)
2204{
2205 const unsigned char
2206 *p;
2207
2208 if ((font == (const char *) NULL) || (*font == '\0'))
2209 return(MagickFalse);
2210 for (p=(const unsigned char *) font; *p != '\0'; p++)
2211 {
2212 if ((isalnum(*p) != 0) || (*p == '-') || (*p == '_') || (*p == '.'))
2213 continue;
2214 return(MagickFalse);
2215 }
2216 return(MagickTrue);
2217}
2218
2219static MagickBooleanType RenderPostscript(Image *image,
2220 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
2221 ExceptionInfo *exception)
2222{
2223 char
2224 filename[MagickPathExtent],
2225 geometry[MagickPathExtent],
2226 *text;
2227
2228 FILE
2229 *file;
2230
2231 Image
2232 *annotate_image;
2233
2234 ImageInfo
2235 *annotate_info;
2236
2237 int
2238 unique_file;
2239
2240 MagickBooleanType
2241 identity,
2242 status;
2243
2244 PointInfo
2245 extent,
2246 point,
2247 resolution;
2248
2249 ssize_t
2250 i;
2251
2252 size_t
2253 length;
2254
2255 ssize_t
2256 y;
2257
2258 /*
2259 Render label with a Postscript font.
2260 */
2261 if (draw_info->debug != MagickFalse)
2262 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2263 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
2264 draw_info->font : "none",draw_info->pointsize);
2265 file=(FILE *) NULL;
2266 unique_file=AcquireUniqueFileResource(filename);
2267 if (unique_file != -1)
2268 file=fdopen(unique_file,"wb");
2269 if ((unique_file == -1) || (file == (FILE *) NULL))
2270 {
2271 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
2272 return(MagickFalse);
2273 }
2274 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
2275 (void) FormatLocaleFile(file,"/ReencodeType\n");
2276 (void) FormatLocaleFile(file,"{\n");
2277 (void) FormatLocaleFile(file," findfont dup length\n");
2278 (void) FormatLocaleFile(file,
2279 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2280 (void) FormatLocaleFile(file,
2281 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2282 (void) FormatLocaleFile(file,"} bind def\n");
2283 /*
2284 Sample to compute bounding box.
2285 */
2286 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2287 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2288 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2289 extent.x=0.0;
2290 extent.y=0.0;
2291 length=strlen(draw_info->text);
2292 for (i=0; i <= (ssize_t) (length+2); i++)
2293 {
2294 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2295 draw_info->affine.ry*2.0*draw_info->pointsize);
2296 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2297 draw_info->affine.sy*2.0*draw_info->pointsize);
2298 if (point.x > extent.x)
2299 extent.x=point.x;
2300 if (point.y > extent.y)
2301 extent.y=point.y;
2302 }
2303 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2304 extent.x/2.0,extent.y/2.0);
2305 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
2306 draw_info->pointsize);
2307 if (IsValidPostscriptFontname(draw_info->font) == MagickFalse)
2308 (void) FormatLocaleFile(file,
2309 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2310 else
2311 (void) FormatLocaleFile(file,
2312 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2313 draw_info->font);
2314 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
2315 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2316 draw_info->affine.sy);
2317 text=EscapeParenthesis(draw_info->text,MAGICK_SIZE_MAX,exception);
2318 if (identity == MagickFalse)
2319 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2320 text);
2321 (void) FormatLocaleFile(file,"(%s) show\n",text);
2322 text=DestroyString(text);
2323 (void) FormatLocaleFile(file,"showpage\n");
2324 (void) fclose(file);
2325 (void) FormatLocaleString(geometry,MagickPathExtent,"%.17gx%.17g+0+0!",
2326 floor(extent.x+0.5),floor(extent.y+0.5));
2327 annotate_info=AcquireImageInfo();
2328 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,"ps:%s",
2329 filename);
2330 (void) CloneString(&annotate_info->page,geometry);
2331 if (draw_info->density != (char *) NULL)
2332 (void) CloneString(&annotate_info->density,draw_info->density);
2333 annotate_info->antialias=draw_info->text_antialias;
2334 annotate_image=ReadImage(annotate_info,exception);
2335 CatchException(exception);
2336 annotate_info=DestroyImageInfo(annotate_info);
2337 (void) RelinquishUniqueFileResource(filename);
2338 if (annotate_image == (Image *) NULL)
2339 return(MagickFalse);
2340 (void) NegateImage(annotate_image,MagickFalse,exception);
2341 resolution.x=DefaultResolution;
2342 resolution.y=DefaultResolution;
2343 if (draw_info->density != (char *) NULL)
2344 {
2345 GeometryInfo
2346 geometry_info;
2347
2348 MagickStatusType
2349 flags;
2350
2351 flags=ParseGeometry(draw_info->density,&geometry_info);
2352 if ((flags & RhoValue) != 0)
2353 resolution.x=geometry_info.rho;
2354 resolution.y=resolution.x;
2355 if ((flags & SigmaValue) != 0)
2356 resolution.y=geometry_info.sigma;
2357 }
2358 if (identity == MagickFalse)
2359 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
2360 else
2361 {
2362 RectangleInfo
2363 crop_info;
2364
2365 crop_info=GetImageBoundingBox(annotate_image,exception);
2366 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2367 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2368 crop_info.y=CastDoubleToSsizeT(ceil((resolution.y/DefaultResolution)*
2369 extent.y/8.0-0.5));
2370 (void) FormatLocaleString(geometry,MagickPathExtent,
2371 "%.17gx%.17g%+.20g%+.20g",(double) crop_info.width,(double)
2372 crop_info.height,(double) crop_info.x,(double) crop_info.y);
2373 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
2374 }
2375 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2376 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2377 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2378 metrics->ascent=metrics->pixels_per_em.y;
2379 metrics->descent=metrics->pixels_per_em.y/-5.0;
2380 metrics->width=(double) annotate_image->columns/
2381 ExpandAffine(&draw_info->affine);
2382 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2383 metrics->max_advance=metrics->pixels_per_em.x;
2384 metrics->bounds.x1=0.0;
2385 metrics->bounds.y1=metrics->descent;
2386 metrics->bounds.x2=metrics->ascent+metrics->descent;
2387 metrics->bounds.y2=metrics->ascent+metrics->descent;
2388 metrics->underline_position=(-2.0);
2389 metrics->underline_thickness=1.0;
2390 if (draw_info->render == MagickFalse)
2391 {
2392 annotate_image=DestroyImage(annotate_image);
2393 return(MagickTrue);
2394 }
2395 if (draw_info->fill.alpha != (MagickRealType) TransparentAlpha)
2396 {
2397 CacheView
2398 *annotate_view;
2399
2400 MagickBooleanType
2401 sync;
2402
2403 PixelInfo
2404 fill_color;
2405
2406 /*
2407 Render fill color.
2408 */
2409 if ((image->alpha_trait & BlendPixelTrait) == 0)
2410 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2411 if (annotate_image->alpha_trait == UndefinedPixelTrait)
2412 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
2413 exception);
2414 fill_color=draw_info->fill;
2415 status=MagickTrue;
2416 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2417#if defined(MAGICKCORE_OPENMP_SUPPORT)
2418 #pragma omp parallel for schedule(static) shared(status) \
2419 magick_number_threads(annotate_image,annotate_image,annotate_image->rows,4)
2420#endif
2421 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2422 {
2423 ssize_t
2424 x;
2425
2426 Quantum
2427 *magick_restrict q;
2428
2429 if (status == MagickFalse)
2430 continue;
2431 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2432 1,exception);
2433 if (q == (Quantum *) NULL)
2434 {
2435 status=MagickFalse;
2436 continue;
2437 }
2438 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2439 {
2440 GetFillColor(draw_info,x,y,&fill_color,exception);
2441 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
2442 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
2443 SetPixelRed(annotate_image,(const Quantum) fill_color.red,q);
2444 SetPixelGreen(annotate_image,(const Quantum) fill_color.green,q);
2445 SetPixelBlue(annotate_image,(const Quantum) fill_color.blue,q);
2446 q+=(ptrdiff_t) GetPixelChannels(annotate_image);
2447 }
2448 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2449 if (sync == MagickFalse)
2450 status=MagickFalse;
2451 }
2452 annotate_view=DestroyCacheView(annotate_view);
2453 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
2454 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2455 metrics->descent)-0.5),exception);
2456 }
2457 annotate_image=DestroyImage(annotate_image);
2458 return(MagickTrue);
2459}
2460
2461/*
2462%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2463% %
2464% %
2465% %
2466+ R e n d e r X 1 1 %
2467% %
2468% %
2469% %
2470%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2471%
2472% RenderX11() renders text on the image with an X11 font. It also returns the
2473% bounding box of the text relative to the image.
2474%
2475% The format of the RenderX11 method is:
2476%
2477% MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
2478% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2479%
2480% A description of each parameter follows:
2481%
2482% o image: the image.
2483%
2484% o draw_info: the draw info.
2485%
2486% o offset: (x,y) location of text relative to image.
2487%
2488% o metrics: bounding box of text.
2489%
2490% o exception: return any errors or warnings in this structure.
2491%
2492*/
2493static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
2494 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2495{
2496 MagickBooleanType
2497 status;
2498
2499 if (annotate_semaphore == (SemaphoreInfo *) NULL)
2500 ActivateSemaphoreInfo(&annotate_semaphore);
2501 LockSemaphoreInfo(annotate_semaphore);
2502 status=XRenderImage(image,draw_info,offset,metrics,exception);
2503 UnlockSemaphoreInfo(annotate_semaphore);
2504 return(status);
2505}