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