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