MagickCore 7.1.2-31
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-image->page.x;
342 annotate_info->affine.ty=geometry_info.psi-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 size)
1445{
1446 magick_unreferenced(memory);
1447 if (size < 0)
1448 return((void *) NULL);
1449 if ((size_t) size > GetMaxMemoryRequest())
1450 return((void *) NULL);
1451 return(AcquireMagickMemory((size_t) size));
1452}
1453
1454static void *FreetypeRealloc(FT_Memory magick_unused(memory),
1455 long magick_unused(cur_size),long size,void *block)
1456{
1457 magick_unreferenced(memory);
1458 magick_unreferenced(cur_size);
1459 if (size < 0)
1460 return((void *) NULL);
1461 if ((size_t) size > GetMaxMemoryRequest())
1462 return((void *) NULL);
1463 return(ResizeMagickMemory(block,(size_t) size));
1464}
1465
1466static void FreetypeFree(FT_Memory magick_unused(memory),void *block)
1467{
1468 magick_unreferenced(memory);
1469 (void) RelinquishMagickMemory(block);
1470}
1471
1472static FT_Memory FreetypeAcquireMemoryManager(void)
1473{
1474 FT_Memory
1475 memory;
1476
1477 memory=(FT_Memory) AcquireMagickMemory(sizeof(*memory));
1478 if (memory == (FT_Memory) NULL)
1479 return(memory);
1480 memset(memory,0,sizeof(*memory));
1481 memory->alloc=(&FreetypeAlloc);
1482 memory->realloc=(&FreetypeRealloc);
1483 memory->free=(&FreetypeFree);
1484 return(memory);
1485}
1486
1487static void FreetypeDone(FT_Memory memory,FT_Library library,
1488 FT_StreamRec *stream)
1489{
1490 (void) FT_Done_Library(library);
1491 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1492 memory=(FT_Memory) RelinquishMagickMemory(memory);
1493}
1494
1495static FT_Error FreetypeInit(FT_Memory memory,FT_Library *alibrary)
1496{
1497 FT_Error
1498 ft_status;
1499
1500 ft_status=FT_New_Library(memory,alibrary);
1501 if (ft_status != 0)
1502 RelinquishMagickMemory(memory);
1503 else
1504 FT_Add_Default_Modules(*alibrary);
1505 FT_Set_Default_Properties(*alibrary);
1506 return(ft_status);
1507}
1508
1509static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1510 const char *encoding,const PointInfo *offset,TypeMetric *metrics,
1511 ExceptionInfo *exception)
1512{
1513#if !defined(FT_OPEN_PATHNAME)
1514#define FT_OPEN_PATHNAME ft_open_pathname
1515#endif
1516
1517#define ThrowFreetypeErrorException(tag,ft_status,value) \
1518{ \
1519 const char *error_string = FreetypeErrorMessage(ft_status); \
1520 if (error_string != (const char *) NULL) \
1521 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1522 tag,"`%s (%s)'",value, error_string); \
1523 else \
1524 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1525 tag,"`%s'",value); \
1526}
1527
1528 typedef struct _GlyphInfo
1529 {
1530 FT_UInt
1531 id;
1532
1533 FT_Vector
1534 origin;
1535
1536 FT_Glyph
1537 image;
1538 } GlyphInfo;
1539
1540 char
1541 *p;
1542
1543 const char
1544 *value;
1545
1546 DrawInfo
1547 *annotate_info;
1548
1549 FT_BBox
1550 bounds;
1551
1552 FT_BitmapGlyph
1553 bitmap;
1554
1555 FT_Encoding
1556 encoding_type;
1557
1558 FT_Error
1559 ft_status;
1560
1561 FT_Face
1562 face;
1563
1564 FT_Int32
1565 flags;
1566
1567 FT_Library
1568 library;
1569
1570 FT_Long
1571 face_index;
1572
1573 FT_Matrix
1574 affine;
1575
1576 FT_Memory
1577 memory;
1578
1579 FT_Open_Args
1580 args;
1581
1582 FT_StreamRec
1583 *stream;
1584
1585 FT_UInt
1586 first_glyph_id,
1587 last_glyph_id,
1588 missing_glyph_id;
1589
1590 FT_Vector
1591 origin;
1592
1593 GlyphInfo
1594 glyph;
1595
1596 GraphemeInfo
1597 *grapheme;
1598
1599 MagickBooleanType
1600 status;
1601
1602 PointInfo
1603 point,
1604 resolution;
1605
1606 ssize_t
1607 i;
1608
1609 size_t
1610 length;
1611
1612 ssize_t
1613 code,
1614 last_character,
1615 y;
1616
1617 static FT_Outline_Funcs
1618 OutlineMethods =
1619 {
1620 (FT_Outline_MoveTo_Func) TraceMoveTo,
1621 (FT_Outline_LineTo_Func) TraceLineTo,
1622 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1623 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1624 0, 0
1625 };
1626
1627 struct stat
1628 attributes;
1629
1630 unsigned char
1631 *utf8;
1632
1633 /*
1634 Initialize Truetype library.
1635 */
1636 if ((draw_info->font != (char *) NULL) && (*draw_info->font == '@') &&
1637 (IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,draw_info->font) == MagickFalse))
1638 ThrowPolicyException(draw_info->font,MagickFalse);
1639 memory=FreetypeAcquireMemoryManager();
1640 if (memory == (FT_Memory) NULL)
1641 ThrowBinaryException(ResourceLimitError,"UnableToInitializeFreetypeLibrary",
1642 image->filename);
1643 ft_status=FreetypeInit(memory,&library);
1644 if (ft_status != 0)
1645 {
1646 ThrowFreetypeErrorException("UnableToInitializeFreetypeLibrary",ft_status,
1647 image->filename);
1648 return(MagickFalse);
1649 }
1650 /*
1651 Open font face.
1652 */
1653 face_index=(FT_Long) draw_info->face;
1654 (void) memset(&args,0,sizeof(args));
1655 if (draw_info->font == (char *) NULL)
1656 {
1657 const TypeInfo *type_info = GetTypeInfo("*",exception);
1658 if (type_info != (const TypeInfo *) NULL)
1659 args.pathname=ConstantString(type_info->glyphs);
1660 }
1661 else
1662 if (*draw_info->font != '@')
1663 args.pathname=ConstantString(draw_info->font);
1664 else
1665 {
1666 /*
1667 Extract face index, e.g. @msgothic[1].
1668 */
1669 ImageInfo *image_info = AcquireImageInfo();
1670 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1671 MagickPathExtent);
1672 (void) SetImageInfo(image_info,0,exception);
1673 face_index=(FT_Long) image_info->scene;
1674 args.pathname=ConstantString(image_info->filename);
1675 image_info=DestroyImageInfo(image_info);
1676 }
1677 /*
1678 Configure streaming interface.
1679 */
1680 stream=(FT_StreamRec *) AcquireCriticalMemory(sizeof(*stream));
1681 (void) memset(stream,0,sizeof(*stream));
1682 if (stat(args.pathname,&attributes) == 0)
1683 stream->size=attributes.st_size >= 0 ? (unsigned long)
1684 attributes.st_size : 0;
1685 stream->descriptor.pointer=fopen_utf8(args.pathname,"rb");
1686 stream->read=(&FreetypeReadStream);
1687 stream->close=(&FreetypeCloseStream);
1688 args.flags=FT_OPEN_STREAM;
1689 args.stream=stream;
1690 face=(FT_Face) NULL;
1691 ft_status=FT_Open_Face(library,&args,face_index,&face);
1692 if (ft_status != 0)
1693 {
1694 FreetypeDone(memory,library,stream);
1695 ThrowFreetypeErrorException("UnableToReadFont",ft_status,args.pathname);
1696 args.pathname=DestroyString(args.pathname);
1697 return(MagickFalse);
1698 }
1699 args.pathname=DestroyString(args.pathname);
1700 if ((draw_info->metrics != (char *) NULL) &&
1701 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1702 (void) FT_Attach_File(face,draw_info->metrics);
1703 encoding_type=FT_ENCODING_UNICODE;
1704 ft_status=FT_Select_Charmap(face,encoding_type);
1705 if ((ft_status != 0) && (face->num_charmaps != 0))
1706 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1707 if (encoding != (const char *) NULL)
1708 {
1709 if (LocaleCompare(encoding,"AdobeCustom") == 0)
1710 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1711 if (LocaleCompare(encoding,"AdobeExpert") == 0)
1712 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1713 if (LocaleCompare(encoding,"AdobeStandard") == 0)
1714 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1715 if (LocaleCompare(encoding,"AppleRoman") == 0)
1716 encoding_type=FT_ENCODING_APPLE_ROMAN;
1717 if (LocaleCompare(encoding,"BIG5") == 0)
1718 encoding_type=FT_ENCODING_BIG5;
1719#if defined(FT_ENCODING_PRC)
1720 if (LocaleCompare(encoding,"GB2312") == 0)
1721 encoding_type=FT_ENCODING_PRC;
1722#endif
1723#if defined(FT_ENCODING_JOHAB)
1724 if (LocaleCompare(encoding,"Johab") == 0)
1725 encoding_type=FT_ENCODING_JOHAB;
1726#endif
1727#if defined(FT_ENCODING_ADOBE_LATIN_1)
1728 if (LocaleCompare(encoding,"Latin-1") == 0)
1729 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1730#endif
1731#if defined(FT_ENCODING_ADOBE_LATIN_2)
1732 if (LocaleCompare(encoding,"Latin-2") == 0)
1733 encoding_type=FT_ENCODING_OLD_LATIN_2;
1734#endif
1735 if (LocaleCompare(encoding,"None") == 0)
1736 encoding_type=FT_ENCODING_NONE;
1737 if (LocaleCompare(encoding,"SJIScode") == 0)
1738 encoding_type=FT_ENCODING_SJIS;
1739 if (LocaleCompare(encoding,"Symbol") == 0)
1740 encoding_type=FT_ENCODING_MS_SYMBOL;
1741 if (LocaleCompare(encoding,"Unicode") == 0)
1742 encoding_type=FT_ENCODING_UNICODE;
1743 if (LocaleCompare(encoding,"Wansung") == 0)
1744 encoding_type=FT_ENCODING_WANSUNG;
1745 ft_status=FT_Select_Charmap(face,encoding_type);
1746 if (ft_status != 0)
1747 {
1748 (void) FT_Done_Face(face);
1749 FreetypeDone(memory,library,stream);
1750 ThrowFreetypeErrorException("UnrecognizedFontEncoding",ft_status,
1751 encoding);
1752 return(MagickFalse);
1753 }
1754 }
1755 /*
1756 Set text size.
1757 */
1758 resolution.x=DefaultResolution;
1759 resolution.y=DefaultResolution;
1760 if (draw_info->density != (char *) NULL)
1761 {
1762 GeometryInfo
1763 geometry_info;
1764
1765 MagickStatusType
1766 geometry_flags;
1767
1768 geometry_flags=ParseGeometry(draw_info->density,&geometry_info);
1769 if ((geometry_flags & RhoValue) != 0)
1770 resolution.x=geometry_info.rho;
1771 resolution.y=resolution.x;
1772 if ((geometry_flags & SigmaValue) != 0)
1773 resolution.y=geometry_info.sigma;
1774 }
1775 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1776 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1777 (FT_UInt) resolution.y);
1778 if (ft_status != 0)
1779 {
1780 (void) FT_Done_Face(face);
1781 FreetypeDone(memory,library,stream);
1782 ThrowFreetypeErrorException("UnableToReadFont",ft_status,
1783 draw_info->font);
1784 return(MagickFalse);
1785 }
1786 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1787 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1788 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1789 metrics->descent=(double) face->size->metrics.descender/64.0;
1790 if (face->size->metrics.ascender == 0)
1791 {
1792 /*
1793 Sanitize buggy ascender and descender values.
1794 */
1795 metrics->ascent=face->size->metrics.y_ppem;
1796 if (face->size->metrics.descender == 0)
1797 metrics->descent=face->size->metrics.y_ppem/-3.5;
1798 }
1799 metrics->width=0;
1800 metrics->origin.x=0;
1801 metrics->origin.y=0;
1802 metrics->height=(double) face->size->metrics.height/64.0;
1803 metrics->max_advance=0.0;
1804 if (face->size->metrics.max_advance > MagickEpsilon)
1805 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1806 metrics->bounds.x1=0.0;
1807 metrics->bounds.y1=metrics->descent;
1808 metrics->bounds.x2=metrics->ascent+metrics->descent;
1809 metrics->bounds.y2=metrics->ascent+metrics->descent;
1810 metrics->underline_position=face->underline_position*
1811 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1812 metrics->underline_thickness=face->underline_thickness*
1813 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1814 first_glyph_id=0;
1815 FT_Get_First_Char(face,&first_glyph_id);
1816 if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0') ||
1817 (first_glyph_id == 0))
1818 {
1819 (void) FT_Done_Face(face);
1820 FreetypeDone(memory,library,stream);
1821 return(MagickTrue);
1822 }
1823 /*
1824 Compute bounding box.
1825 */
1826 if (draw_info->debug != MagickFalse)
1827 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1828 "font-encoding %s; text-encoding %s; pointsize %g",
1829 draw_info->font != (char *) NULL ? draw_info->font : "none",
1830 encoding != (char *) NULL ? encoding : "none",
1831 draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1832 draw_info->pointsize);
1833 flags=FT_LOAD_DEFAULT;
1834 if (draw_info->render == MagickFalse)
1835 flags=FT_LOAD_NO_BITMAP;
1836 if (draw_info->text_antialias == MagickFalse)
1837 flags|=FT_LOAD_TARGET_MONO;
1838 else
1839 {
1840#if defined(FT_LOAD_TARGET_LIGHT)
1841 flags|=FT_LOAD_TARGET_LIGHT;
1842#elif defined(FT_LOAD_TARGET_LCD)
1843 flags|=FT_LOAD_TARGET_LCD;
1844#endif
1845 }
1846 value=GetImageProperty(image,"type:hinting",exception);
1847 if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1848 flags|=FT_LOAD_NO_HINTING;
1849 glyph.id=0;
1850 glyph.image=(FT_Glyph) NULL;
1851 last_glyph_id=0;
1852 origin.x=0;
1853 origin.y=0;
1854 affine.xx=65536L;
1855 affine.yx=0L;
1856 affine.xy=0L;
1857 affine.yy=65536L;
1858 if (draw_info->render != MagickFalse)
1859 {
1860 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1861 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1862 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1863 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1864 }
1865 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1866 if (annotate_info->dash_pattern != (double *) NULL)
1867 annotate_info->dash_pattern[0]=0.0;
1868 (void) CloneString(&annotate_info->primitive,"path '");
1869 status=MagickTrue;
1870 if (draw_info->render != MagickFalse)
1871 {
1872 if (image->storage_class != DirectClass)
1873 (void) SetImageStorageClass(image,DirectClass,exception);
1874 if ((image->alpha_trait & BlendPixelTrait) == 0)
1875 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1876 }
1877 for (p=draw_info->text; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
1878 if (GetUTFCode(p) < 0)
1879 break;
1880 utf8=(unsigned char *) NULL;
1881 if (GetUTFCode(p) == 0)
1882 p=draw_info->text;
1883 else
1884 {
1885 utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1886 if (utf8 != (unsigned char *) NULL)
1887 p=(char *) utf8;
1888 }
1889 grapheme=(GraphemeInfo *) NULL;
1890 length=strlen(p);
1891#if defined(MAGICKCORE_RAQM_DELEGATE)
1892 if (ContainsRaqmParagraphSeparator((const unsigned char *) p,length) == MagickFalse)
1893 length=ComplexRaqmTextLayout(image,draw_info,p,length,face,&grapheme,
1894 exception);
1895 else
1896#endif
1897 length=ComplexTextLayout(draw_info,p,length,face,flags,&grapheme);
1898 missing_glyph_id=FT_Get_Char_Index(face,' ');
1899 code=0;
1900 last_character=(ssize_t) length-1;
1901 for (i=0; i < (ssize_t) length; i++)
1902 {
1903 FT_Outline
1904 outline;
1905
1906 /*
1907 Render UTF-8 sequence.
1908 */
1909 glyph.id=(FT_UInt) grapheme[i].index;
1910 if (glyph.id == 0)
1911 glyph.id=missing_glyph_id;
1912 if ((glyph.id != 0) && (last_glyph_id != 0))
1913 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1914 glyph.origin=origin;
1915 glyph.origin.x+=(FT_Pos) grapheme[i].x_offset;
1916 glyph.origin.y+=(FT_Pos) grapheme[i].y_offset;
1917 if (glyph.image != (FT_Glyph) NULL)
1918 {
1919 FT_Done_Glyph(glyph.image);
1920 glyph.image=(FT_Glyph) NULL;
1921 }
1922 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1923 if (ft_status != 0)
1924 continue;
1925 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1926 if (ft_status != 0)
1927 continue;
1928 outline=((FT_OutlineGlyph) glyph.image)->outline;
1929 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1930 (IsEmptyOutline(outline) == MagickFalse))
1931 continue;
1932 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1933 if (ft_status != 0)
1934 continue;
1935 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1936 metrics->bounds.x1=(double) bounds.xMin;
1937 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1938 metrics->bounds.y1=(double) bounds.yMin;
1939 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1940 metrics->bounds.x2=(double) bounds.xMax;
1941 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1942 metrics->bounds.y2=(double) bounds.yMax;
1943 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
1944 (draw_info->stroke_pattern != (Image *) NULL)) &&
1945 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1946 {
1947 /*
1948 Trace the glyph.
1949 */
1950 annotate_info->affine.tx=glyph.origin.x/64.0;
1951 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1952 if (IsEmptyOutline(outline) == MagickFalse)
1953 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1954 annotate_info);
1955 }
1956 FT_Vector_Transform(&glyph.origin,&affine);
1957 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1958 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1959 (FT_Vector *) NULL,MagickTrue);
1960 if (ft_status != 0)
1961 continue;
1962 bitmap=(FT_BitmapGlyph) glyph.image;
1963 point.x=offset->x+bitmap->left;
1964 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1965 point.x+=(origin.x/64.0);
1966 point.y=offset->y-bitmap->top;
1967 if (draw_info->render != MagickFalse)
1968 {
1969 CacheView
1970 *image_view;
1971
1972 MagickBooleanType
1973 transparent_fill;
1974
1975 unsigned char
1976 *r;
1977
1978 /*
1979 Rasterize the glyph.
1980 */
1981 transparent_fill=((draw_info->fill.alpha == (MagickRealType) TransparentAlpha) &&
1982 (draw_info->fill_pattern == (Image *) NULL) &&
1983 (draw_info->stroke.alpha == (MagickRealType) TransparentAlpha) &&
1984 (draw_info->stroke_pattern == (Image *) NULL)) ? MagickTrue :
1985 MagickFalse;
1986 r=bitmap->bitmap.buffer;
1987 image_view=AcquireAuthenticCacheView(image,exception);
1988#if defined(MAGICKCORE_OPENMP_SUPPORT)
1989 #pragma omp parallel for schedule(static) shared(status) \
1990 magick_number_threads(image,image,bitmap->bitmap.rows,4)
1991#endif
1992 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1993 {
1994 double
1995 fill_opacity;
1996
1997 MagickBooleanType
1998 active,
1999 sync;
2000
2001 PixelInfo
2002 fill_color;
2003
2004 Quantum
2005 *magick_restrict q;
2006
2007 ssize_t
2008 x;
2009
2010 ssize_t
2011 n,
2012 x_offset,
2013 y_offset;
2014
2015 if (status == MagickFalse)
2016 continue;
2017 x_offset=CastDoubleToSsizeT(ceil(point.x-0.5));
2018 y_offset=CastDoubleToSsizeT(ceil(point.y+y-0.5));
2019 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
2020 continue;
2021 q=(Quantum *) NULL;
2022 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
2023 active=MagickFalse;
2024 else
2025 {
2026 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
2027 bitmap->bitmap.width,1,exception);
2028 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
2029 }
2030 n=y*bitmap->bitmap.pitch;
2031 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
2032 {
2033 x_offset=CastDoubleToSsizeT(ceil(point.x+x-0.5));
2034 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
2035 {
2036 if (q != (Quantum *) NULL)
2037 q+=(ptrdiff_t) GetPixelChannels(image);
2038 continue;
2039 }
2040 fill_opacity=1.0;
2041 if (bitmap->bitmap.buffer != (unsigned char *) NULL)
2042 {
2043 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
2044 fill_opacity=(double) (r[n])/(bitmap->bitmap.num_grays-1);
2045 else
2046 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
2047 fill_opacity=((r[(x >> 3)+y*bitmap->bitmap.pitch] &
2048 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
2049 }
2050 if (draw_info->text_antialias == MagickFalse)
2051 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
2052 if (active == MagickFalse)
2053 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
2054 exception);
2055 if (q == (Quantum *) NULL)
2056 continue;
2057 if (transparent_fill == MagickFalse)
2058 {
2059 GetPixelInfo(image,&fill_color);
2060 GetFillColor(draw_info,x_offset,y_offset,&fill_color,exception);
2061 fill_opacity=fill_opacity*fill_color.alpha;
2062 CompositePixelOver(image,&fill_color,fill_opacity,q,
2063 GetPixelAlpha(image,q),q);
2064 }
2065 else
2066 {
2067 double
2068 Sa,
2069 Da;
2070
2071 Da=1.0-(QuantumScale*(double) GetPixelAlpha(image,q));
2072 Sa=fill_opacity;
2073 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*(double)
2074 QuantumRange;
2075 SetPixelAlpha(image,(const Quantum) fill_opacity,q);
2076 }
2077 if (active == MagickFalse)
2078 {
2079 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2080 if (sync == MagickFalse)
2081 status=MagickFalse;
2082 }
2083 q+=(ptrdiff_t) GetPixelChannels(image);
2084 }
2085 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2086 if (sync == MagickFalse)
2087 status=MagickFalse;
2088 }
2089 image_view=DestroyCacheView(image_view);
2090 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
2091 (draw_info->stroke_pattern != (Image *) NULL)) &&
2092 (status != MagickFalse))
2093 {
2094 /*
2095 Draw text stroke.
2096 */
2097 annotate_info->linejoin=RoundJoin;
2098 annotate_info->affine.tx=offset->x;
2099 annotate_info->affine.ty=offset->y;
2100 (void) ConcatenateString(&annotate_info->primitive,"'");
2101 if (strlen(annotate_info->primitive) > 7)
2102 (void) DrawImage(image,annotate_info,exception);
2103 (void) CloneString(&annotate_info->primitive,"path '");
2104 }
2105 }
2106 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
2107 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
2108 (IsUTFSpace((int) code) == MagickFalse))
2109 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
2110 else
2111 if (i == last_character)
2112 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
2113 else
2114 origin.x+=(FT_Pos) grapheme[i].x_advance;
2115 origin.y+=(FT_Pos) grapheme[i].y_advance;
2116 metrics->origin.x=(double) origin.x;
2117 metrics->origin.y=(double) origin.y;
2118 if (metrics->origin.x > metrics->width)
2119 metrics->width=metrics->origin.x;
2120 last_glyph_id=glyph.id;
2121 code=GetUTFCode(p+grapheme[i].cluster);
2122 }
2123 if (grapheme != (GraphemeInfo *) NULL)
2124 grapheme=(GraphemeInfo *) RelinquishMagickMemory(grapheme);
2125 if (utf8 != (unsigned char *) NULL)
2126 utf8=(unsigned char *) RelinquishMagickMemory(utf8);
2127 if (glyph.image != (FT_Glyph) NULL)
2128 {
2129 FT_Done_Glyph(glyph.image);
2130 glyph.image=(FT_Glyph) NULL;
2131 }
2132 /*
2133 Determine font metrics.
2134 */
2135 metrics->bounds.x1/=64.0;
2136 metrics->bounds.y1/=64.0;
2137 metrics->bounds.x2/=64.0;
2138 metrics->bounds.y2/=64.0;
2139 metrics->origin.x/=64.0;
2140 metrics->origin.y/=64.0;
2141 metrics->width=ceil(metrics->width/64.0);
2142 /*
2143 Relinquish resources.
2144 */
2145 annotate_info=DestroyDrawInfo(annotate_info);
2146 (void) FT_Done_Face(face);
2147 FreetypeDone(memory,library,stream);
2148 return(status);
2149}
2150#else
2151static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
2152 const char *magick_unused(encoding),const PointInfo *offset,
2153 TypeMetric *metrics,ExceptionInfo *exception)
2154{
2155 magick_unreferenced(encoding);
2156 (void) ThrowMagickException(exception,GetMagickModule(),
2157 MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","'%s' (Freetype)",
2158 draw_info->font != (char *) NULL ? draw_info->font : "none");
2159 return(RenderPostscript(image,draw_info,offset,metrics,exception));
2160}
2161#endif
2162
2163/*
2164%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2165% %
2166% %
2167% %
2168+ R e n d e r P o s t s c r i p t %
2169% %
2170% %
2171% %
2172%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2173%
2174% RenderPostscript() renders text on the image with a Postscript font. It
2175% also returns the bounding box of the text relative to the image.
2176%
2177% The format of the RenderPostscript method is:
2178%
2179% MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
2180% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2181%
2182% A description of each parameter follows:
2183%
2184% o image: the image.
2185%
2186% o draw_info: the draw info.
2187%
2188% o offset: (x,y) location of text relative to image.
2189%
2190% o metrics: bounding box of text.
2191%
2192% o exception: return any errors or warnings in this structure.
2193%
2194*/
2195
2196static MagickBooleanType RenderPostscript(Image *image,
2197 const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics,
2198 ExceptionInfo *exception)
2199{
2200 char
2201 filename[MagickPathExtent],
2202 geometry[MagickPathExtent],
2203 *text;
2204
2205 FILE
2206 *file;
2207
2208 Image
2209 *annotate_image;
2210
2211 ImageInfo
2212 *annotate_info;
2213
2214 int
2215 unique_file;
2216
2217 MagickBooleanType
2218 identity,
2219 status;
2220
2221 PointInfo
2222 extent,
2223 point,
2224 resolution;
2225
2226 ssize_t
2227 i;
2228
2229 size_t
2230 length;
2231
2232 ssize_t
2233 y;
2234
2235 /*
2236 Render label with a Postscript font.
2237 */
2238 if (draw_info->debug != MagickFalse)
2239 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2240 "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
2241 draw_info->font : "none",draw_info->pointsize);
2242 file=(FILE *) NULL;
2243 unique_file=AcquireUniqueFileResource(filename);
2244 if (unique_file != -1)
2245 file=fdopen(unique_file,"wb");
2246 if ((unique_file == -1) || (file == (FILE *) NULL))
2247 {
2248 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",filename);
2249 return(MagickFalse);
2250 }
2251 (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
2252 (void) FormatLocaleFile(file,"/ReencodeType\n");
2253 (void) FormatLocaleFile(file,"{\n");
2254 (void) FormatLocaleFile(file," findfont dup length\n");
2255 (void) FormatLocaleFile(file,
2256 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2257 (void) FormatLocaleFile(file,
2258 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2259 (void) FormatLocaleFile(file,"} bind def\n");
2260 /*
2261 Sample to compute bounding box.
2262 */
2263 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2264 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2265 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2266 extent.x=0.0;
2267 extent.y=0.0;
2268 length=strlen(draw_info->text);
2269 for (i=0; i <= (ssize_t) (length+2); i++)
2270 {
2271 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2272 draw_info->affine.ry*2.0*draw_info->pointsize);
2273 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2274 draw_info->affine.sy*2.0*draw_info->pointsize);
2275 if (point.x > extent.x)
2276 extent.x=point.x;
2277 if (point.y > extent.y)
2278 extent.y=point.y;
2279 }
2280 (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2281 extent.x/2.0,extent.y/2.0);
2282 (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
2283 draw_info->pointsize);
2284 if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
2285 (strchr(draw_info->font,'/') != (char *) NULL))
2286 (void) FormatLocaleFile(file,
2287 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2288 else
2289 (void) FormatLocaleFile(file,
2290 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2291 draw_info->font);
2292 (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
2293 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2294 draw_info->affine.sy);
2295 text=EscapeParenthesis(draw_info->text,MAGICK_SIZE_MAX,exception);
2296 if (identity == MagickFalse)
2297 (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2298 text);
2299 (void) FormatLocaleFile(file,"(%s) show\n",text);
2300 text=DestroyString(text);
2301 (void) FormatLocaleFile(file,"showpage\n");
2302 (void) fclose(file);
2303 (void) FormatLocaleString(geometry,MagickPathExtent,"%.17gx%.17g+0+0!",
2304 floor(extent.x+0.5),floor(extent.y+0.5));
2305 annotate_info=AcquireImageInfo();
2306 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,"ps:%s",
2307 filename);
2308 (void) CloneString(&annotate_info->page,geometry);
2309 if (draw_info->density != (char *) NULL)
2310 (void) CloneString(&annotate_info->density,draw_info->density);
2311 annotate_info->antialias=draw_info->text_antialias;
2312 annotate_image=ReadImage(annotate_info,exception);
2313 CatchException(exception);
2314 annotate_info=DestroyImageInfo(annotate_info);
2315 (void) RelinquishUniqueFileResource(filename);
2316 if (annotate_image == (Image *) NULL)
2317 return(MagickFalse);
2318 (void) NegateImage(annotate_image,MagickFalse,exception);
2319 resolution.x=DefaultResolution;
2320 resolution.y=DefaultResolution;
2321 if (draw_info->density != (char *) NULL)
2322 {
2323 GeometryInfo
2324 geometry_info;
2325
2326 MagickStatusType
2327 flags;
2328
2329 flags=ParseGeometry(draw_info->density,&geometry_info);
2330 if ((flags & RhoValue) != 0)
2331 resolution.x=geometry_info.rho;
2332 resolution.y=resolution.x;
2333 if ((flags & SigmaValue) != 0)
2334 resolution.y=geometry_info.sigma;
2335 }
2336 if (identity == MagickFalse)
2337 (void) TransformImage(&annotate_image,"0x0",(char *) NULL,exception);
2338 else
2339 {
2340 RectangleInfo
2341 crop_info;
2342
2343 crop_info=GetImageBoundingBox(annotate_image,exception);
2344 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2345 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2346 crop_info.y=CastDoubleToSsizeT(ceil((resolution.y/DefaultResolution)*
2347 extent.y/8.0-0.5));
2348 (void) FormatLocaleString(geometry,MagickPathExtent,
2349 "%.17gx%.17g%+.20g%+.20g",(double) crop_info.width,(double)
2350 crop_info.height,(double) crop_info.x,(double) crop_info.y);
2351 (void) TransformImage(&annotate_image,geometry,(char *) NULL,exception);
2352 }
2353 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2354 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2355 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2356 metrics->ascent=metrics->pixels_per_em.y;
2357 metrics->descent=metrics->pixels_per_em.y/-5.0;
2358 metrics->width=(double) annotate_image->columns/
2359 ExpandAffine(&draw_info->affine);
2360 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2361 metrics->max_advance=metrics->pixels_per_em.x;
2362 metrics->bounds.x1=0.0;
2363 metrics->bounds.y1=metrics->descent;
2364 metrics->bounds.x2=metrics->ascent+metrics->descent;
2365 metrics->bounds.y2=metrics->ascent+metrics->descent;
2366 metrics->underline_position=(-2.0);
2367 metrics->underline_thickness=1.0;
2368 if (draw_info->render == MagickFalse)
2369 {
2370 annotate_image=DestroyImage(annotate_image);
2371 return(MagickTrue);
2372 }
2373 if (draw_info->fill.alpha != (MagickRealType) TransparentAlpha)
2374 {
2375 CacheView
2376 *annotate_view;
2377
2378 MagickBooleanType
2379 sync;
2380
2381 PixelInfo
2382 fill_color;
2383
2384 /*
2385 Render fill color.
2386 */
2387 if ((image->alpha_trait & BlendPixelTrait) == 0)
2388 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2389 if (annotate_image->alpha_trait == UndefinedPixelTrait)
2390 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
2391 exception);
2392 fill_color=draw_info->fill;
2393 status=MagickTrue;
2394 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2395#if defined(MAGICKCORE_OPENMP_SUPPORT)
2396 #pragma omp parallel for schedule(static) shared(status) \
2397 magick_number_threads(annotate_image,annotate_image,annotate_image->rows,4)
2398#endif
2399 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2400 {
2401 ssize_t
2402 x;
2403
2404 Quantum
2405 *magick_restrict q;
2406
2407 if (status == MagickFalse)
2408 continue;
2409 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2410 1,exception);
2411 if (q == (Quantum *) NULL)
2412 {
2413 status=MagickFalse;
2414 continue;
2415 }
2416 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2417 {
2418 GetFillColor(draw_info,x,y,&fill_color,exception);
2419 SetPixelAlpha(annotate_image,ClampToQuantum((((double) QuantumScale*
2420 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
2421 SetPixelRed(annotate_image,(const Quantum) fill_color.red,q);
2422 SetPixelGreen(annotate_image,(const Quantum) fill_color.green,q);
2423 SetPixelBlue(annotate_image,(const Quantum) fill_color.blue,q);
2424 q+=(ptrdiff_t) GetPixelChannels(annotate_image);
2425 }
2426 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2427 if (sync == MagickFalse)
2428 status=MagickFalse;
2429 }
2430 annotate_view=DestroyCacheView(annotate_view);
2431 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
2432 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2433 metrics->descent)-0.5),exception);
2434 }
2435 annotate_image=DestroyImage(annotate_image);
2436 return(MagickTrue);
2437}
2438
2439/*
2440%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2441% %
2442% %
2443% %
2444+ R e n d e r X 1 1 %
2445% %
2446% %
2447% %
2448%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2449%
2450% RenderX11() renders text on the image with an X11 font. It also returns the
2451% bounding box of the text relative to the image.
2452%
2453% The format of the RenderX11 method is:
2454%
2455% MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
2456% const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2457%
2458% A description of each parameter follows:
2459%
2460% o image: the image.
2461%
2462% o draw_info: the draw info.
2463%
2464% o offset: (x,y) location of text relative to image.
2465%
2466% o metrics: bounding box of text.
2467%
2468% o exception: return any errors or warnings in this structure.
2469%
2470*/
2471static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
2472 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2473{
2474 MagickBooleanType
2475 status;
2476
2477 if (annotate_semaphore == (SemaphoreInfo *) NULL)
2478 ActivateSemaphoreInfo(&annotate_semaphore);
2479 LockSemaphoreInfo(annotate_semaphore);
2480 status=XRenderImage(image,draw_info,offset,metrics,exception);
2481 UnlockSemaphoreInfo(annotate_semaphore);
2482 return(status);
2483}