45 #include "magick/studio.h"
46 #include "magick/annotate.h"
47 #include "magick/attribute.h"
48 #include "magick/cache-private.h"
49 #include "magick/cache-view.h"
50 #include "magick/channel.h"
51 #include "magick/client.h"
52 #include "magick/color.h"
53 #include "magick/color-private.h"
54 #include "magick/colorspace-private.h"
55 #include "magick/composite.h"
56 #include "magick/composite-private.h"
57 #include "magick/constitute.h"
58 #include "magick/draw.h"
59 #include "magick/draw-private.h"
60 #include "magick/exception.h"
61 #include "magick/exception-private.h"
62 #include "magick/gem.h"
63 #include "magick/geometry.h"
64 #include "magick/image-private.h"
65 #include "magick/log.h"
66 #include "magick/quantum.h"
67 #include "magick/quantum-private.h"
68 #include "magick/pixel-accessor.h"
69 #include "magick/policy.h"
70 #include "magick/property.h"
71 #include "magick/resource_.h"
72 #include "magick/semaphore.h"
73 #include "magick/statistic.h"
74 #include "magick/string_.h"
75 #include "magick/token.h"
76 #include "magick/token-private.h"
77 #include "magick/transform.h"
78 #include "magick/type.h"
79 #include "magick/utility.h"
80 #include "magick/utility-private.h"
81 #include "magick/xwindow-private.h"
82 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
83 #if defined(__MINGW32__)
87 #if defined(FT_FREETYPE_H)
88 # include FT_FREETYPE_H
90 # include <freetype/freetype.h>
92 #if defined(FT_GLYPH_H)
95 # include <freetype/ftglyph.h>
97 #if defined(FT_OUTLINE_H)
98 # include FT_OUTLINE_H
100 # include <freetype/ftoutln.h>
102 #if defined(FT_BBOX_H)
105 # include <freetype/ftbbox.h>
108 #if defined(MAGICKCORE_RAQM_DELEGATE)
132 static MagickBooleanType
157 MagickExport MagickBooleanType AnnotateComponentGenesis(
void)
160 annotate_semaphore=AllocateSemaphoreInfo();
182 MagickExport
void AnnotateComponentTerminus(
void)
185 ActivateSemaphoreInfo(&annotate_semaphore);
186 DestroySemaphoreInfo(&annotate_semaphore);
213 MagickExport MagickBooleanType AnnotateImage(
Image *image,
218 color[MaxTextExtent],
219 primitive[MaxTextExtent],
252 assert(image != (
Image *) NULL);
253 assert(image->signature == MagickCoreSignature);
254 if (IsEventLogging() != MagickFalse)
255 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
256 assert(draw_info != (
DrawInfo *) NULL);
257 assert(draw_info->signature == MagickCoreSignature);
258 if (draw_info->text == (
char *) NULL)
260 if (*draw_info->text ==
'\0')
262 annotate=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
264 annotate->text=(
char *) NULL;
265 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
267 for (p=text; *p !=
'\0'; p++)
270 textlist=(
char **) AcquireQuantumMemory(number_lines+1,
sizeof(*textlist));
271 if (textlist == (
char **) NULL)
273 annotate_info=DestroyDrawInfo(annotate_info);
274 annotate=DestroyDrawInfo(annotate);
275 text=DestroyString(text);
279 for (i=0; i < (ssize_t) number_lines; i++)
285 for (q=p; *q !=
'\0'; q++)
286 if ((*q ==
'\r') || (*q ==
'\n'))
296 textlist[i]=(
char *) NULL;
297 SetGeometry(image,&geometry);
298 SetGeometryInfo(&geometry_info);
299 if (annotate_info->geometry != (
char *) NULL)
301 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
303 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
305 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
307 annotate_info=DestroyDrawInfo(annotate_info);
308 annotate=DestroyDrawInfo(annotate);
309 textlist=(
char **) RelinquishMagickMemory(textlist);
310 text=DestroyString(text);
313 if (IsGrayColorspace(image->colorspace) != MagickFalse)
314 (void) SetImageColorspace(image,sRGBColorspace);
316 (void) memset(&metrics,0,
sizeof(metrics));
317 for (i=0; textlist[i] != (
char *) NULL; i++)
319 if (*textlist[i] ==
'\0')
324 annotate_info->affine.tx=geometry_info.xi-image->page.x;
325 annotate_info->affine.ty=geometry_info.psi-image->page.y;
326 (void) CloneString(&annotate->text,textlist[i]);
327 if ((metrics.width == 0) || (annotate->gravity != NorthWestGravity))
328 (void) GetTypeMetrics(image,annotate,&metrics);
329 height=(size_t) floor(metrics.ascent-metrics.descent+0.5);
331 height=draw_info->pointsize;
332 height+=(size_t) floor(draw_info->interline_spacing+0.5);
333 switch (annotate->gravity)
335 case UndefinedGravity:
338 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
339 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
342 case (GravityType) NorthWestGravity:
344 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
345 annotate_info->affine.ry*height+annotate_info->affine.ry*
346 (metrics.ascent+metrics.descent);
347 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
348 annotate_info->affine.sy*height+annotate_info->affine.sy*
352 case (GravityType) NorthGravity:
354 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
355 geometry.width/2.0+i*annotate_info->affine.ry*height-
356 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
357 (metrics.ascent+metrics.descent);
358 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
359 annotate_info->affine.sy*height+annotate_info->affine.sy*
360 metrics.ascent-annotate_info->affine.rx*metrics.width/2.0;
363 case (GravityType) NorthEastGravity:
365 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
366 geometry.width+i*annotate_info->affine.ry*height-
367 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
368 (metrics.ascent+metrics.descent)-1.0;
369 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
370 annotate_info->affine.sy*height+annotate_info->affine.sy*
371 metrics.ascent-annotate_info->affine.rx*metrics.width;
374 case (GravityType) WestGravity:
376 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
377 annotate_info->affine.ry*height+annotate_info->affine.ry*
378 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
379 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
380 geometry.height/2.0+i*annotate_info->affine.sy*height+
381 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
382 (number_lines-1.0)*height)/2.0;
385 case (GravityType) StaticGravity:
386 case (GravityType) CenterGravity:
388 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
389 geometry.width/2.0+i*annotate_info->affine.ry*height-
390 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
391 (metrics.ascent+metrics.descent-(number_lines-1)*height)/2.0;
392 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
393 geometry.height/2.0+i*annotate_info->affine.sy*height-
394 annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
395 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
398 case (GravityType) EastGravity:
400 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
401 geometry.width+i*annotate_info->affine.ry*height-
402 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
403 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0-1.0;
404 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
405 geometry.height/2.0+i*annotate_info->affine.sy*height-
406 annotate_info->affine.rx*metrics.width+annotate_info->affine.sy*
407 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
410 case (GravityType) SouthWestGravity:
412 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
413 annotate_info->affine.ry*height-annotate_info->affine.ry*
414 (number_lines-1.0)*height;
415 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
416 geometry.height+i*annotate_info->affine.sy*height-
417 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
420 case (GravityType) SouthGravity:
422 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
423 geometry.width/2.0+i*annotate_info->affine.ry*height-
424 annotate_info->affine.sx*metrics.width/2.0-annotate_info->affine.ry*
425 (number_lines-1.0)*height/2.0;
426 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
427 geometry.height+i*annotate_info->affine.sy*height-
428 annotate_info->affine.rx*metrics.width/2.0-annotate_info->affine.sy*
429 (number_lines-1.0)*height+metrics.descent;
432 case (GravityType) SouthEastGravity:
434 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
435 geometry.width+i*annotate_info->affine.ry*height-
436 annotate_info->affine.sx*metrics.width-annotate_info->affine.ry*
437 (number_lines-1.0)*height-1.0;
438 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
439 geometry.height+i*annotate_info->affine.sy*height-
440 annotate_info->affine.rx*metrics.width-annotate_info->affine.sy*
441 (number_lines-1.0)*height+metrics.descent;
445 switch (annotate->align)
449 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
450 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
455 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
456 annotate_info->affine.sx*metrics.width/2.0;
457 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
458 annotate_info->affine.rx*metrics.width/2.0;
463 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
464 annotate_info->affine.sx*metrics.width;
465 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
466 annotate_info->affine.rx*metrics.width;
472 if (draw_info->undercolor.opacity != TransparentOpacity)
481 undercolor_info->fill=draw_info->undercolor;
482 undercolor_info->affine=draw_info->affine;
483 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
484 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
485 (void) FormatLocaleString(primitive,MaxTextExtent,
486 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(
double) height);
487 (void) CloneString(&undercolor_info->primitive,primitive);
488 (void) DrawImage(image,undercolor_info);
489 (void) DestroyDrawInfo(undercolor_info);
491 annotate_info->affine.tx=offset.x;
492 annotate_info->affine.ty=offset.y;
493 pixel=annotate_info->fill;
494 if (annotate_info->stroke.opacity != TransparentOpacity)
495 pixel=annotate_info->stroke;
496 (void) QueryColorname(image,&pixel,SVGCompliance,color,&image->exception);
497 (void) FormatLocaleString(primitive,MagickPathExtent,
"stroke %s "
498 "stroke-width %g line 0,0 %g,0",color,(
double)
499 metrics.underline_thickness,(double) metrics.width);
503 switch (annotate->decorate)
505 case OverlineDecoration:
507 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
508 metrics.descent-metrics.underline_position));
509 (void) CloneString(&annotate_info->primitive,primitive);
510 (void) DrawImage(image,annotate_info);
513 case UnderlineDecoration:
515 annotate_info->affine.ty-=(draw_info->affine.sy*
516 metrics.underline_position);
517 (void) CloneString(&annotate_info->primitive,primitive);
518 (void) DrawImage(image,annotate_info);
524 status=RenderType(image,annotate,&offset,&metrics);
525 if (status == MagickFalse)
527 if (annotate->decorate == LineThroughDecoration)
529 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
530 metrics.underline_position+metrics.descent*2)/2.0);
531 (void) CloneString(&annotate_info->primitive,primitive);
532 (void) DrawImage(image,annotate_info);
538 annotate_info=DestroyDrawInfo(annotate_info);
539 annotate=DestroyDrawInfo(annotate);
540 textlist=(
char **) RelinquishMagickMemory(textlist);
541 text=DestroyString(text);
578 static inline char *ReplaceSpaceWithNewline(
char **caption,
char *space)
583 octets=(size_t) GetUTFOctets(space);
595 length=strlen(*caption);
597 offset=space-(*caption);
598 target=AcquireString(*caption);
599 CopyMagickString(target,*caption,offset+2);
600 ConcatenateMagickString(target,space+octets,length);
601 (void) DestroyString(*caption);
603 space=(*caption)+offset;
608 MagickExport ssize_t FormatMagickCaption(
Image *image,
DrawInfo *draw_info,
609 const MagickBooleanType split,
TypeMetric *metrics,
char **caption)
631 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
642 if ((IsUTFSpace(code) != MagickFalse) &&
643 (IsNonBreakingUTFSpace(code) == MagickFalse))
646 if (width > image->columns)
647 p=ReplaceSpaceWithNewline(caption,s);
649 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
652 status=GetTypeMetrics(image,draw_info,metrics);
653 if (status == MagickFalse)
655 width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
656 if (width <= image->columns)
658 if (s != (
char *) NULL)
659 p=ReplaceSpaceWithNewline(caption,s);
661 if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
667 if ((n > 0) && ((*caption)[n-1] !=
'\n'))
672 target=AcquireString(*caption);
673 CopyMagickString(target,*caption,n+1);
674 ConcatenateMagickString(target,
"\n",strlen(*caption)+1);
675 ConcatenateMagickString(target,p,strlen(*caption)+2);
676 (void) DestroyString(*caption);
685 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
686 if (GetUTFCode(p) ==
'\n')
738 MagickExport MagickBooleanType GetMultilineTypeMetrics(
Image *image,
765 assert(image != (
Image *) NULL);
766 assert(image->signature == MagickCoreSignature);
767 if (IsEventLogging() != MagickFalse)
768 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
769 assert(draw_info != (
DrawInfo *) NULL);
770 assert(draw_info->text != (
char *) NULL);
771 assert(draw_info->signature == MagickCoreSignature);
772 if (*draw_info->text ==
'\0')
774 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
775 annotate_info->text=DestroyString(annotate_info->text);
779 textlist=StringToStrings(draw_info->text,&count);
780 if (textlist == (
char **) NULL)
782 annotate_info->render=MagickFalse;
783 annotate_info->direction=UndefinedDirection;
784 (void) memset(metrics,0,
sizeof(*metrics));
785 (void) memset(&extent,0,
sizeof(extent));
789 annotate_info->text=textlist[0];
790 status=GetTypeMetrics(image,annotate_info,&extent);
792 height=(count*(size_t) (metrics->ascent-metrics->descent+
793 0.5)+(count-1)*draw_info->interline_spacing);
794 size=(MagickSizeType) fabs(height);
795 if (AcquireMagickResource(HeightResource,size) == MagickFalse)
797 (void) ThrowMagickException(&image->exception,GetMagickModule(),
798 ImageError,
"WidthOrHeightExceedsLimit",
"`%s'",image->filename);
803 for (i=1; i < (ssize_t) count; i++)
805 annotate_info->text=textlist[i];
806 status=GetTypeMetrics(image,annotate_info,&extent);
807 if (status == MagickFalse)
809 if (extent.width > metrics->width)
811 size=(MagickSizeType) fabs(extent.width);
812 if (AcquireMagickResource(WidthResource,size) == MagickFalse)
814 (void) ThrowMagickException(&image->exception,GetMagickModule(),
815 ImageError,
"WidthOrHeightExceedsLimit",
"`%s'",image->filename);
820 metrics->height=(double) height;
825 annotate_info->text=(
char *) NULL;
826 annotate_info=DestroyDrawInfo(annotate_info);
827 for (i=0; i < (ssize_t) count; i++)
828 textlist[i]=DestroyString(textlist[i]);
829 textlist=(
char **) RelinquishMagickMemory(textlist);
877 MagickExport MagickBooleanType GetTypeMetrics(
Image *image,
889 assert(image != (
Image *) NULL);
890 assert(image->signature == MagickCoreSignature);
891 if (IsEventLogging() != MagickFalse)
892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
893 assert(draw_info != (
DrawInfo *) NULL);
894 assert(draw_info->text != (
char *) NULL);
895 assert(draw_info->signature == MagickCoreSignature);
896 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
897 annotate_info->render=MagickFalse;
898 annotate_info->direction=UndefinedDirection;
899 (void) memset(metrics,0,
sizeof(*metrics));
902 status=RenderType(image,annotate_info,&offset,metrics);
903 if (draw_info->debug != MagickFalse)
904 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Metrics: text: %s; "
905 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
906 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
907 "underline position: %g; underline thickness: %g",annotate_info->text,
908 metrics->width,metrics->height,metrics->ascent,metrics->descent,
909 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
910 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
911 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
912 metrics->underline_position,metrics->underline_thickness);
913 annotate_info=DestroyDrawInfo(annotate_info);
947 static MagickBooleanType RenderType(
Image *image,
const DrawInfo *draw_info,
966 if (draw_info->font != (
char *) NULL)
968 if (*draw_info->font ==
'@')
970 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
974 if (*draw_info->font ==
'-')
975 return(RenderX11(image,draw_info,offset,metrics));
976 if (*draw_info->font ==
'^')
977 return(RenderPostscript(image,draw_info,offset,metrics));
978 if (IsPathAccessible(draw_info->font) != MagickFalse)
980 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
984 type_info=GetTypeInfo(draw_info->font,&image->exception);
985 if (type_info == (
const TypeInfo *) NULL)
986 (void) ThrowMagickException(&image->exception,GetMagickModule(),
987 TypeWarning,
"UnableToReadFont",
"`%s'",draw_info->font);
989 if ((type_info == (
const TypeInfo *) NULL) &&
990 (draw_info->family != (
const char *) NULL))
992 if (strstr(draw_info->family,
",'\"") == (
char *) NULL)
993 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
994 draw_info->stretch,draw_info->weight,&image->exception);
995 if (type_info == (
const TypeInfo *) NULL)
1009 family=StringToArgv(draw_info->family,&number_families);
1010 for (i=1; i < (ssize_t) number_families; i++)
1012 (void) SubstituteString(&family[i],
",",
"");
1013 type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1014 draw_info->stretch,draw_info->weight,&image->exception);
1015 if ((type_info != (
const TypeInfo *) NULL) &&
1016 (LocaleCompare(family[i],type_info->family) == 0))
1019 for (i=0; i < (ssize_t) number_families; i++)
1020 family[i]=DestroyString(family[i]);
1021 family=(
char **) RelinquishMagickMemory(family);
1022 if (type_info == (
const TypeInfo *) NULL)
1023 (
void) ThrowMagickException(&image->exception,GetMagickModule(),
1024 TypeWarning,
"UnableToReadFont",
"`%s'",draw_info->family);
1027 font=GetPolicyValue(
"system:font");
1028 if (font != (
char *) NULL)
1030 if (IsPathAccessible(font) != MagickFalse)
1035 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1036 annotate_info->font=font;
1037 status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1039 annotate_info=DestroyDrawInfo(annotate_info);
1042 font=DestroyString(font);
1044 sans_exception=AcquireExceptionInfo();
1045 if (type_info == (
const TypeInfo *) NULL)
1046 type_info=GetTypeInfoByFamily(
"Open Sans",draw_info->style,
1047 draw_info->stretch,draw_info->weight,sans_exception);
1048 if (type_info == (
const TypeInfo *) NULL)
1049 type_info=GetTypeInfoByFamily(
"Sans Serif",draw_info->style,
1050 draw_info->stretch,draw_info->weight,sans_exception);
1051 if (type_info == (
const TypeInfo *) NULL)
1052 type_info=GetTypeInfoByFamily((
const char *) NULL,draw_info->style,
1053 draw_info->stretch,draw_info->weight,sans_exception);
1054 if (type_info == (
const TypeInfo *) NULL)
1055 type_info=GetTypeInfo(
"*",sans_exception);
1056 sans_exception=DestroyExceptionInfo(sans_exception);
1057 if (type_info == (
const TypeInfo *) NULL)
1059 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics);
1062 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1063 annotate_info->face=type_info->face;
1064 if (type_info->metrics != (
char *) NULL)
1065 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1066 if (type_info->glyphs != (
char *) NULL)
1067 (
void) CloneString(&annotate_info->font,type_info->glyphs);
1068 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics);
1069 annotate_info=DestroyDrawInfo(annotate_info);
1106 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
1108 static size_t ComplexTextLayout(
const Image *image,
const DrawInfo *draw_info,
1109 const char *text,
const size_t length,
const FT_Face face,
const FT_Int32 flags,
1112 #if defined(MAGICKCORE_RAQM_DELEGATE)
1128 magick_unreferenced(flags);
1131 if (rq == (raqm_t *) NULL)
1133 if (raqm_set_text_utf8(rq,text,length) == 0)
1135 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1137 if (raqm_set_freetype_face(rq,face) == 0)
1139 features=GetImageProperty(image,
"type:features");
1140 if (features != (
const char *) NULL)
1155 token_info=AcquireTokenInfo();
1156 token=AcquireString(
"");
1157 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1158 &breaker,&next,"e);
1159 while (status_token == 0)
1161 raqm_add_font_feature(rq,token,strlen(token));
1162 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1163 &breaker,&next,"e);
1165 token_info=DestroyTokenInfo(token_info);
1166 token=DestroyString(token);
1168 if (raqm_layout(rq) == 0)
1170 glyphs=raqm_get_glyphs(rq,&extent);
1171 if (glyphs == (raqm_glyph_t *) NULL)
1176 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(extent,
sizeof(**grapheme));
1182 for (i=0; i < (ssize_t) extent; i++)
1184 (*grapheme)[i].index=glyphs[i].index;
1185 (*grapheme)[i].x_offset=glyphs[i].x_offset;
1186 (*grapheme)[i].x_advance=glyphs[i].x_advance;
1187 (*grapheme)[i].y_offset=glyphs[i].y_offset;
1188 (*grapheme)[i].cluster=glyphs[i].cluster;
1207 magick_unreferenced(image);
1208 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(length+1,
sizeof(**grapheme));
1213 for (i=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p), i++)
1215 (*grapheme)[i].index=FT_Get_Char_Index(face,GetUTFCode(p));
1216 (*grapheme)[i].x_offset=0;
1217 (*grapheme)[i].y_offset=0;
1218 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1220 if (FT_HAS_KERNING(face))
1228 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1229 (*grapheme)[i].index,ft_kerning_default,&kerning);
1231 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1232 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1235 (void) FT_Load_Glyph(face,(*grapheme)[i].index,flags);
1236 (*grapheme)[i].x_advance=face->glyph->advance.x;
1237 (*grapheme)[i].cluster=p-text;
1238 last_glyph=(*grapheme)[i].index;
1244 static void FTCloseStream(FT_Stream stream)
1246 FILE *file = (FILE *) stream->descriptor.pointer;
1247 if (file != (FILE *) NULL)
1248 (
void) fclose(file);
1249 stream->descriptor.pointer=NULL;
1252 static unsigned long FTReadStream(FT_Stream stream,
unsigned long offset,
1253 unsigned char *buffer,
unsigned long count)
1255 FILE *file = (FILE *) stream->descriptor.pointer;
1256 if (file == (FILE *) NULL)
1260 if (fseek(file,(off_t) offset,SEEK_SET) != 0)
1262 return((
unsigned long) fread(buffer,1,count,file));
1265 static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1267 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1271 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1278 path[MaxTextExtent];
1280 affine=draw_info->affine;
1281 (void) FormatLocaleString(path,MaxTextExtent,
"C%g,%g %g,%g %g,%g",affine.tx+
1282 p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,
1283 affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1284 (void) ConcatenateString(&draw_info->primitive,path);
1288 static int TraceLineTo(FT_Vector *to,
DrawInfo *draw_info)
1294 path[MaxTextExtent];
1296 affine=draw_info->affine;
1297 (void) FormatLocaleString(path,MaxTextExtent,
"L%g,%g",affine.tx+to->x/64.0,
1298 affine.ty-to->y/64.0);
1299 (void) ConcatenateString(&draw_info->primitive,path);
1303 static int TraceMoveTo(FT_Vector *to,
DrawInfo *draw_info)
1309 path[MaxTextExtent];
1311 affine=draw_info->affine;
1312 (void) FormatLocaleString(path,MaxTextExtent,
"M%g,%g",affine.tx+to->x/64.0,
1313 affine.ty-to->y/64.0);
1314 (void) ConcatenateString(&draw_info->primitive,path);
1318 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1325 path[MaxTextExtent];
1327 affine=draw_info->affine;
1328 (void) FormatLocaleString(path,MaxTextExtent,
"Q%g,%g %g,%g",affine.tx+
1329 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1331 (void) ConcatenateString(&draw_info->primitive,path);
1335 static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1337 #if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1338 return(FT_Error_String(ft_status));
1340 magick_unreferenced(ft_status);
1341 return((
const char *) NULL);
1345 static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
1348 #if !defined(FT_OPEN_PATHNAME)
1349 #define FT_OPEN_PATHNAME ft_open_pathname
1352 #define ThrowFreetypeErrorException(tag,ft_status,value) \
1355 *error_string=FreetypeErrorMessage(ft_status); \
1356 if (error_string != (const char *) NULL) \
1357 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1358 tag,"`%s (%s)'",value, error_string); \
1360 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1361 tag,"`%s'",value); \
1364 typedef struct _GlyphInfo
1449 static FT_Outline_Funcs
1452 (FT_Outline_MoveTo_Func) TraceMoveTo,
1453 (FT_Outline_LineTo_Func) TraceLineTo,
1454 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1455 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1468 exception=(&image->exception);
1469 ft_status=FT_Init_FreeType(&library);
1471 ThrowFreetypeErrorException("UnableToInitializeFreetypeLibrary",ft_status,
1473 face_index=(FT_Long) draw_info->face;
1477 (
void) memset(&args,0,sizeof(args));
1478 if (draw_info->font == (
char *) NULL)
1479 args.pathname=ConstantString("helvetica");
1481 if (*draw_info->font != '@')
1482 args.pathname=ConstantString(draw_info->font);
1488 ImageInfo *image_info=AcquireImageInfo();
1489 (void) strcpy(image_info->filename,draw_info->font+1);
1490 (void) SetImageInfo(image_info,0,exception);
1491 face_index=(FT_Long) image_info->scene;
1492 args.pathname=ConstantString(image_info->filename);
1493 image_info=DestroyImageInfo(image_info);
1498 stream=(FT_StreamRec *) AcquireCriticalMemory(
sizeof(*stream));
1499 (void) memset(stream,0,
sizeof(*stream));
1500 (void) stat(args.pathname,&attributes);
1501 stream->size=attributes.st_size;
1502 stream->descriptor.pointer=fopen_utf8(args.pathname,
"rb");
1503 stream->read=(&FTReadStream);
1504 stream->close=(&FTCloseStream);
1505 args.flags=FT_OPEN_STREAM;
1507 face=(FT_Face) NULL;
1508 ft_status=FT_Open_Face(library,&args,face_index,&face);
1511 (void) FT_Done_FreeType(library);
1512 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,args.pathname);
1513 args.pathname=DestroyString(args.pathname);
1514 return(MagickFalse);
1516 args.pathname=DestroyString(args.pathname);
1517 if ((draw_info->metrics != (
char *) NULL) &&
1518 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1519 (void) FT_Attach_File(face,draw_info->metrics);
1520 encoding_type=FT_ENCODING_UNICODE;
1521 ft_status=FT_Select_Charmap(face,encoding_type);
1522 if ((ft_status != 0) && (face->num_charmaps != 0))
1523 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1524 if (encoding != (
const char *) NULL)
1526 if (LocaleCompare(encoding,
"AdobeCustom") == 0)
1527 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1528 if (LocaleCompare(encoding,
"AdobeExpert") == 0)
1529 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1530 if (LocaleCompare(encoding,
"AdobeStandard") == 0)
1531 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1532 if (LocaleCompare(encoding,
"AppleRoman") == 0)
1533 encoding_type=FT_ENCODING_APPLE_ROMAN;
1534 if (LocaleCompare(encoding,
"BIG5") == 0)
1535 encoding_type=FT_ENCODING_BIG5;
1536 #if defined(FT_ENCODING_PRC)
1537 if (LocaleCompare(encoding,
"GB2312") == 0)
1538 encoding_type=FT_ENCODING_PRC;
1540 #if defined(FT_ENCODING_JOHAB)
1541 if (LocaleCompare(encoding,
"Johab") == 0)
1542 encoding_type=FT_ENCODING_JOHAB;
1544 #if defined(FT_ENCODING_ADOBE_LATIN_1)
1545 if (LocaleCompare(encoding,
"Latin-1") == 0)
1546 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1548 #if defined(FT_ENCODING_ADOBE_LATIN_2)
1549 if (LocaleCompare(encoding,
"Latin-2") == 0)
1550 encoding_type=FT_ENCODING_OLD_LATIN_2;
1552 if (LocaleCompare(encoding,
"None") == 0)
1553 encoding_type=FT_ENCODING_NONE;
1554 if (LocaleCompare(encoding,
"SJIScode") == 0)
1555 encoding_type=FT_ENCODING_SJIS;
1556 if (LocaleCompare(encoding,
"Symbol") == 0)
1557 encoding_type=FT_ENCODING_MS_SYMBOL;
1558 if (LocaleCompare(encoding,
"Unicode") == 0)
1559 encoding_type=FT_ENCODING_UNICODE;
1560 if (LocaleCompare(encoding,
"Wansung") == 0)
1561 encoding_type=FT_ENCODING_WANSUNG;
1562 ft_status=FT_Select_Charmap(face,encoding_type);
1565 (void) FT_Done_Face(face);
1566 (void) FT_Done_FreeType(library);
1567 ThrowFreetypeErrorException(
"UnrecognizedFontEncoding",ft_status,
1569 return(MagickFalse);
1575 resolution.x=DefaultResolution;
1576 resolution.y=DefaultResolution;
1577 if (draw_info->density != (
char *) NULL)
1585 flags=ParseGeometry(draw_info->density,&geometry_info);
1586 if ((flags & RhoValue) != 0)
1587 resolution.x=geometry_info.rho;
1588 resolution.y=resolution.x;
1589 if ((flags & SigmaValue) != 0)
1590 resolution.y=geometry_info.sigma;
1592 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1593 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1594 (FT_UInt) resolution.y);
1597 (void) FT_Done_Face(face);
1598 (void) FT_Done_FreeType(library);
1599 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,
1601 return(MagickFalse);
1603 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1604 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1605 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1606 metrics->descent=(
double) face->size->metrics.descender/64.0;
1607 if (face->size->metrics.ascender == 0)
1612 metrics->ascent=face->size->metrics.y_ppem;
1613 if (face->size->metrics.descender == 0)
1614 metrics->descent=face->size->metrics.y_ppem/-3.5;
1617 metrics->origin.x=0;
1618 metrics->origin.y=0;
1619 metrics->height=(double) face->size->metrics.height/64.0;
1620 metrics->max_advance=0.0;
1621 if (face->size->metrics.max_advance > MagickEpsilon)
1622 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1623 metrics->bounds.x1=0.0;
1624 metrics->bounds.y1=metrics->descent;
1625 metrics->bounds.x2=metrics->ascent+metrics->descent;
1626 metrics->bounds.y2=metrics->ascent+metrics->descent;
1627 metrics->underline_position=face->underline_position*
1628 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1629 metrics->underline_thickness=face->underline_thickness*
1630 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1632 FT_Get_First_Char(face,&first_glyph_id);
1633 if ((draw_info->text == (
char *) NULL) || (*draw_info->text ==
'\0') ||
1634 (first_glyph_id == 0))
1636 (void) FT_Done_Face(face);
1637 (void) FT_Done_FreeType(library);
1643 if (draw_info->debug != MagickFalse)
1644 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Font %s; "
1645 "font-encoding %s; text-encoding %s; pointsize %g",
1646 draw_info->font != (
char *) NULL ? draw_info->font :
"none",
1647 encoding != (
char *) NULL ? encoding :
"none",
1648 draw_info->encoding != (
char *) NULL ? draw_info->encoding :
"none",
1649 draw_info->pointsize);
1650 flags=FT_LOAD_DEFAULT;
1651 if (draw_info->render == MagickFalse)
1652 flags=FT_LOAD_NO_BITMAP;
1653 if (draw_info->text_antialias == MagickFalse)
1654 flags|=FT_LOAD_TARGET_MONO;
1657 #if defined(FT_LOAD_TARGET_LIGHT)
1658 flags|=FT_LOAD_TARGET_LIGHT;
1659 #elif defined(FT_LOAD_TARGET_LCD)
1660 flags|=FT_LOAD_TARGET_LCD;
1663 value=GetImageProperty(image,
"type:hinting");
1664 if ((value != (
const char *) NULL) && (LocaleCompare(value,
"off") == 0))
1665 flags|=FT_LOAD_NO_HINTING;
1667 glyph.image=(FT_Glyph) NULL;
1675 if (draw_info->render != MagickFalse)
1677 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1678 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1679 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1680 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1682 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1683 if (annotate_info->dash_pattern != (
double *) NULL)
1684 annotate_info->dash_pattern[0]=0.0;
1685 (void) CloneString(&annotate_info->primitive,
"path '");
1687 if (draw_info->render != MagickFalse)
1689 if (image->storage_class != DirectClass)
1690 (void) SetImageStorageClass(image,DirectClass);
1691 if (image->matte == MagickFalse)
1692 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1694 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1695 if (GetUTFCode(p) < 0)
1697 utf8=(
unsigned char *) NULL;
1698 if (GetUTFCode(p) == 0)
1702 utf8=ConvertLatin1ToUTF8((
unsigned char *) draw_info->text);
1703 if (utf8 != (
unsigned char *) NULL)
1707 length=ComplexTextLayout(image,draw_info,p,strlen(p),face,flags,&grapheme);
1708 missing_glyph_id=FT_Get_Char_Index(face,
' ');
1710 last_character=(ssize_t) length-1;
1711 for (i=0; i < (ssize_t) length; i++)
1725 glyph.id=grapheme[i].index;
1727 glyph.id=missing_glyph_id;
1728 if ((glyph.id != 0) && (last_glyph_id != 0))
1729 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1730 glyph.origin=origin;
1731 glyph.origin.x+=grapheme[i].x_offset;
1732 glyph.origin.y+=grapheme[i].y_offset;
1733 if (glyph.image != (FT_Glyph) NULL)
1735 FT_Done_Glyph(glyph.image);
1736 glyph.image=(FT_Glyph) NULL;
1738 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1741 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1744 outline=((FT_OutlineGlyph) glyph.image)->outline;
1745 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1746 (IsEmptyOutline(outline) == MagickFalse))
1748 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1751 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1752 metrics->bounds.x1=(double) bounds.xMin;
1753 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1754 metrics->bounds.y1=(double) bounds.yMin;
1755 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1756 metrics->bounds.x2=(double) bounds.xMax;
1757 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1758 metrics->bounds.y2=(double) bounds.yMax;
1759 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1760 (draw_info->stroke_pattern != (
Image *) NULL)) &&
1761 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1766 annotate_info->affine.tx=glyph.origin.x/64.0;
1767 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1768 if (IsEmptyOutline(outline) == MagickFalse)
1769 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1772 FT_Vector_Transform(&glyph.origin,&affine);
1773 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1774 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1775 (FT_Vector *) NULL,MagickTrue);
1778 bitmap=(FT_BitmapGlyph) glyph.image;
1779 point.x=offset->x+bitmap->left;
1780 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1781 point.x+=(origin.x/64.0);
1782 point.y=offset->y-bitmap->top;
1783 if (draw_info->render != MagickFalse)
1797 transparent_fill=((draw_info->fill.opacity == TransparentOpacity) &&
1798 (draw_info->fill_pattern == (
Image *) NULL) &&
1799 (draw_info->stroke.opacity == TransparentOpacity) &&
1800 (draw_info->stroke_pattern == (
Image *) NULL)) ? MagickTrue :
1802 p=bitmap->bitmap.buffer;
1803 image_view=AcquireAuthenticCacheView(image,exception);
1804 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1825 if (status == MagickFalse)
1827 x_offset=CastDoubleToLong(ceil(point.x-0.5));
1828 y_offset=CastDoubleToLong(ceil(point.y+y-0.5));
1829 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1832 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1836 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1837 bitmap->bitmap.width,1,exception);
1838 active=q != (
PixelPacket *) NULL ? MagickTrue : MagickFalse;
1840 n=y*bitmap->bitmap.pitch;
1841 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
1843 x_offset=CastDoubleToLong(ceil(point.x+x-0.5));
1844 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1851 if (bitmap->bitmap.buffer != (
unsigned char *) NULL)
1853 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
1854 fill_opacity=(MagickRealType) (p[n])/(
1855 bitmap->bitmap.num_grays-1);
1857 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1858 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1859 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1861 if (draw_info->text_antialias == MagickFalse)
1862 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1863 if (active == MagickFalse)
1864 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1868 if (transparent_fill == MagickFalse)
1870 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
1871 fill_opacity=QuantumRange-fill_opacity*(QuantumRange-
1872 fill_color.opacity);
1873 MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q);
1881 Da=1.0-(QuantumScale*(QuantumRange-q->opacity));
1883 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*QuantumRange;
1884 SetPixelAlpha(q,fill_opacity);
1886 if (active == MagickFalse)
1888 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1889 if (sync == MagickFalse)
1894 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1895 if (sync == MagickFalse)
1898 image_view=DestroyCacheView(image_view);
1899 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1900 (draw_info->stroke_pattern != (
Image *) NULL)) &&
1901 (status != MagickFalse))
1906 annotate_info->linejoin=RoundJoin;
1907 annotate_info->affine.tx=offset->x;
1908 annotate_info->affine.ty=offset->y;
1909 (void) ConcatenateString(&annotate_info->primitive,
"'");
1910 if (strlen(annotate_info->primitive) > 7)
1911 (
void) DrawImage(image,annotate_info);
1912 (void) CloneString(&annotate_info->primitive,
"path '");
1915 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1916 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
1917 (IsUTFSpace(code) == MagickFalse))
1918 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
1920 if (i == last_character)
1921 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
1923 origin.x+=(FT_Pos) grapheme[i].x_advance;
1924 metrics->origin.x=(double) origin.x;
1925 metrics->origin.y=(
double) origin.y;
1926 if (metrics->origin.x > metrics->width)
1927 metrics->width=metrics->origin.x;
1928 last_glyph_id=glyph.id;
1929 code=GetUTFCode(p+grapheme[i].cluster);
1932 grapheme=(
GraphemeInfo *) RelinquishMagickMemory(grapheme);
1933 if (utf8 != (
unsigned char *) NULL)
1934 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
1935 if (glyph.image != (FT_Glyph) NULL)
1937 FT_Done_Glyph(glyph.image);
1938 glyph.image=(FT_Glyph) NULL;
1943 metrics->bounds.x1/=64.0;
1944 metrics->bounds.y1/=64.0;
1945 metrics->bounds.x2/=64.0;
1946 metrics->bounds.y2/=64.0;
1947 metrics->origin.x/=64.0;
1948 metrics->origin.y/=64.0;
1949 metrics->width/=64.0;
1953 annotate_info=DestroyDrawInfo(annotate_info);
1954 (void) FT_Done_Face(face);
1955 (void) FT_Done_FreeType(library);
1959 static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
1960 const char *magick_unused(encoding),
const PointInfo *offset,
1963 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1964 MissingDelegateWarning,
"DelegateLibrarySupportNotBuiltIn",
"`%s' (Freetype)",
1965 draw_info->font != (
char *) NULL ? draw_info->font :
"none");
1966 return(RenderPostscript(image,draw_info,offset,metrics));
2001 static char *EscapeParenthesis(
const char *source)
2015 assert(source != (
const char *) NULL);
2017 for (p=source; *p !=
'\0'; p++)
2019 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2022 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2027 destination=(
char *) NULL;
2028 if (~length >= (MaxTextExtent-1))
2029 destination=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
2030 sizeof(*destination));
2031 if (destination == (
char *) NULL)
2032 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2035 for (p=source; *p !=
'\0'; p++)
2037 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2042 return(destination);
2045 static MagickBooleanType RenderPostscript(
Image *image,
2049 filename[MaxTextExtent],
2050 geometry[MaxTextExtent],
2085 if (draw_info->debug != MagickFalse)
2086 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2087 "Font %s; pointsize %g",draw_info->font != (
char *) NULL ?
2088 draw_info->font :
"none",draw_info->pointsize);
2090 unique_file=AcquireUniqueFileResource(filename);
2091 if (unique_file != -1)
2092 file=fdopen(unique_file,
"wb");
2093 if ((unique_file == -1) || (file == (FILE *) NULL))
2095 ThrowFileException(&image->exception,FileOpenError,
"UnableToOpenFile",
2097 return(MagickFalse);
2099 (void) FormatLocaleFile(file,
"%%!PS-Adobe-3.0\n");
2100 (void) FormatLocaleFile(file,
"/ReencodeType\n");
2101 (void) FormatLocaleFile(file,
"{\n");
2102 (void) FormatLocaleFile(file,
" findfont dup length\n");
2103 (void) FormatLocaleFile(file,
2104 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2105 (void) FormatLocaleFile(file,
2106 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2107 (void) FormatLocaleFile(file,
"} bind def\n");
2111 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2112 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2113 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2116 length=strlen(draw_info->text);
2117 for (i=0; i <= (ssize_t) (length+2); i++)
2119 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2120 draw_info->affine.ry*2.0*draw_info->pointsize);
2121 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2122 draw_info->affine.sy*2.0*draw_info->pointsize);
2123 if (point.x > extent.x)
2125 if (point.y > extent.y)
2128 (void) FormatLocaleFile(file,
"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2129 extent.x/2.0,extent.y/2.0);
2130 (void) FormatLocaleFile(file,
"%g %g scale\n",draw_info->pointsize,
2131 draw_info->pointsize);
2132 if ((draw_info->font == (
char *) NULL) || (*draw_info->font ==
'\0') ||
2133 (strchr(draw_info->font,
'/') != (
char *) NULL))
2134 (
void) FormatLocaleFile(file,
2135 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2137 (
void) FormatLocaleFile(file,
2138 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2140 (void) FormatLocaleFile(file,
"[%g %g %g %g 0 0] concat\n",
2141 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2142 draw_info->affine.sy);
2143 text=EscapeParenthesis(draw_info->text);
2144 if (identity == MagickFalse)
2145 (void) FormatLocaleFile(file,
"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2147 (void) FormatLocaleFile(file,
"(%s) show\n",text);
2148 text=DestroyString(text);
2149 (void) FormatLocaleFile(file,
"showpage\n");
2150 (void) fclose(file);
2151 (void) FormatLocaleString(geometry,MaxTextExtent,
"%.20gx%.20g+0+0!",
2152 floor(extent.x+0.5),floor(extent.y+0.5));
2153 annotate_info=AcquireImageInfo();
2154 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,
"ps:%s",
2156 (void) CloneString(&annotate_info->page,geometry);
2157 if (draw_info->density != (
char *) NULL)
2158 (
void) CloneString(&annotate_info->density,draw_info->density);
2159 annotate_info->antialias=draw_info->text_antialias;
2160 annotate_image=ReadImage(annotate_info,&image->exception);
2161 CatchException(&image->exception);
2162 annotate_info=DestroyImageInfo(annotate_info);
2163 (void) RelinquishUniqueFileResource(filename);
2164 if (annotate_image == (
Image *) NULL)
2165 return(MagickFalse);
2166 resolution.x=DefaultResolution;
2167 resolution.y=DefaultResolution;
2168 if (draw_info->density != (
char *) NULL)
2176 flags=ParseGeometry(draw_info->density,&geometry_info);
2177 if ((flags & RhoValue) != 0)
2178 resolution.x=geometry_info.rho;
2179 resolution.y=resolution.x;
2180 if ((flags & SigmaValue) != 0)
2181 resolution.y=geometry_info.sigma;
2183 if (identity == MagickFalse)
2184 (void) TransformImage(&annotate_image,
"0x0",(
char *) NULL);
2190 crop_info=GetImageBoundingBox(annotate_image,&annotate_image->exception);
2191 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2192 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2193 crop_info.y=CastDoubleToLong(ceil((resolution.y/DefaultResolution)*
2195 (void) FormatLocaleString(geometry,MaxTextExtent,
2196 "%.20gx%.20g%+.20g%+.20g",(
double) crop_info.width,(double)
2197 crop_info.height,(
double) crop_info.x,(double) crop_info.y);
2198 (void) TransformImage(&annotate_image,geometry,(
char *) NULL);
2200 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2201 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2202 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2203 metrics->ascent=metrics->pixels_per_em.y;
2204 metrics->descent=metrics->pixels_per_em.y/-5.0;
2205 metrics->width=(double) annotate_image->columns/
2206 ExpandAffine(&draw_info->affine);
2207 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2208 metrics->max_advance=metrics->pixels_per_em.x;
2209 metrics->bounds.x1=0.0;
2210 metrics->bounds.y1=metrics->descent;
2211 metrics->bounds.x2=metrics->ascent+metrics->descent;
2212 metrics->bounds.y2=metrics->ascent+metrics->descent;
2213 metrics->underline_position=(-2.0);
2214 metrics->underline_thickness=1.0;
2215 if (draw_info->render == MagickFalse)
2217 annotate_image=DestroyImage(annotate_image);
2220 if (draw_info->fill.opacity != TransparentOpacity)
2237 if (image->matte == MagickFalse)
2238 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
2239 if (annotate_image->matte == MagickFalse)
2240 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel);
2241 fill_color=draw_info->fill;
2242 exception=(&image->exception);
2243 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2244 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2252 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2256 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2258 (void) GetFillColor(draw_info,x,y,&fill_color);
2259 SetPixelAlpha(q,ClampToQuantum((((QuantumRange-GetPixelIntensity(
2260 annotate_image,q))*(QuantumRange-fill_color.opacity))/
2262 SetPixelRed(q,fill_color.red);
2263 SetPixelGreen(q,fill_color.green);
2264 SetPixelBlue(q,fill_color.blue);
2267 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2268 if (sync == MagickFalse)
2271 annotate_view=DestroyCacheView(annotate_view);
2272 (void) CompositeImage(image,OverCompositeOp,annotate_image,
2273 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2274 metrics->descent)-0.5));
2276 annotate_image=DestroyImage(annotate_image);
2310 static MagickBooleanType RenderX11(
Image *image,
const DrawInfo *draw_info,
2317 ActivateSemaphoreInfo(&annotate_semaphore);
2318 LockSemaphoreInfo(annotate_semaphore);
2319 status=XRenderImage(image,draw_info,offset,metrics);
2320 UnlockSemaphoreInfo(annotate_semaphore);