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=CastDoubleToUnsigned(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 if ((caption == (
char **) NULL) || (space == (
char *) NULL))
584 return((
char *) NULL);
585 octets=(size_t) GetUTFOctets(space);
597 length=strlen(*caption);
599 offset=space-(*caption);
600 target=AcquireString(*caption);
601 CopyMagickString(target,*caption,offset+2);
602 ConcatenateMagickString(target,space+octets,length);
603 (void) DestroyString(*caption);
605 space=(*caption)+offset;
610 MagickExport ssize_t FormatMagickCaption(
Image *image,
DrawInfo *draw_info,
611 const MagickBooleanType split,
TypeMetric *metrics,
char **caption)
628 if ((caption == (
char **) NULL) || (*caption == (
char *) NULL))
633 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
644 if ((IsUTFSpace(code) != MagickFalse) &&
645 (IsNonBreakingUTFSpace(code) == MagickFalse))
648 if (width > image->columns)
649 p=ReplaceSpaceWithNewline(caption,s);
651 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
654 status=GetTypeMetrics(image,draw_info,metrics);
655 if (status == MagickFalse)
657 width=CastDoubleToUnsigned(metrics->width+draw_info->stroke_width+0.5);
658 if (width <= image->columns)
660 if (s != (
char *) NULL)
661 p=ReplaceSpaceWithNewline(caption,s);
663 if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
669 if ((n > 0) && ((*caption)[n-1] !=
'\n'))
674 target=AcquireString(*caption);
675 CopyMagickString(target,*caption,n+1);
676 ConcatenateMagickString(target,
"\n",strlen(*caption)+1);
677 ConcatenateMagickString(target,p,strlen(*caption)+2);
678 (void) DestroyString(*caption);
687 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
688 if (GetUTFCode(p) ==
'\n')
740 MagickExport MagickBooleanType GetMultilineTypeMetrics(
Image *image,
767 assert(image != (
Image *) NULL);
768 assert(image->signature == MagickCoreSignature);
769 if (IsEventLogging() != MagickFalse)
770 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
771 assert(draw_info != (
DrawInfo *) NULL);
772 assert(draw_info->text != (
char *) NULL);
773 assert(draw_info->signature == MagickCoreSignature);
774 if (*draw_info->text ==
'\0')
776 (void) ThrowMagickException(&image->exception,GetMagickModule(),
777 OptionError,
"LabelExpected",
"`%s'",image->filename);
780 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
781 annotate_info->text=DestroyString(annotate_info->text);
785 textlist=StringToStrings(draw_info->text,&count);
786 if (textlist == (
char **) NULL)
788 annotate_info->render=MagickFalse;
789 annotate_info->direction=UndefinedDirection;
790 (void) memset(metrics,0,
sizeof(*metrics));
791 (void) memset(&extent,0,
sizeof(extent));
795 annotate_info->text=textlist[0];
796 status=GetTypeMetrics(image,annotate_info,&extent);
798 height=(count*(size_t) (metrics->ascent-metrics->descent+
799 0.5)+(count-1)*draw_info->interline_spacing);
800 size=(MagickSizeType) fabs(height);
801 if (AcquireMagickResource(HeightResource,size) == MagickFalse)
803 (void) ThrowMagickException(&image->exception,GetMagickModule(),
804 ImageError,
"WidthOrHeightExceedsLimit",
"`%s'",image->filename);
809 for (i=1; i < (ssize_t) count; i++)
811 annotate_info->text=textlist[i];
812 status=GetTypeMetrics(image,annotate_info,&extent);
813 if (status == MagickFalse)
815 if (extent.width > metrics->width)
817 size=(MagickSizeType) fabs(extent.width);
818 if (AcquireMagickResource(WidthResource,size) == MagickFalse)
820 (void) ThrowMagickException(&image->exception,GetMagickModule(),
821 ImageError,
"WidthOrHeightExceedsLimit",
"`%s'",image->filename);
826 metrics->height=(double) height;
831 annotate_info->text=(
char *) NULL;
832 annotate_info=DestroyDrawInfo(annotate_info);
833 for (i=0; i < (ssize_t) count; i++)
834 textlist[i]=DestroyString(textlist[i]);
835 textlist=(
char **) RelinquishMagickMemory(textlist);
883 MagickExport MagickBooleanType GetTypeMetrics(
Image *image,
895 assert(image != (
Image *) NULL);
896 assert(image->signature == MagickCoreSignature);
897 if (IsEventLogging() != MagickFalse)
898 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
899 assert(draw_info != (
DrawInfo *) NULL);
900 assert(draw_info->text != (
char *) NULL);
901 assert(draw_info->signature == MagickCoreSignature);
902 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
903 annotate_info->render=MagickFalse;
904 annotate_info->direction=UndefinedDirection;
905 (void) memset(metrics,0,
sizeof(*metrics));
908 status=RenderType(image,annotate_info,&offset,metrics);
909 if (draw_info->debug != MagickFalse)
910 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Metrics: text: %s; "
911 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
912 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
913 "underline position: %g; underline thickness: %g",annotate_info->text,
914 metrics->width,metrics->height,metrics->ascent,metrics->descent,
915 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
916 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
917 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
918 metrics->underline_position,metrics->underline_thickness);
919 annotate_info=DestroyDrawInfo(annotate_info);
953 static MagickBooleanType RenderType(
Image *image,
const DrawInfo *draw_info,
972 if (draw_info->font != (
char *) NULL)
974 if (*draw_info->font ==
'@')
976 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
980 if (*draw_info->font ==
'-')
981 return(RenderX11(image,draw_info,offset,metrics));
982 if (*draw_info->font ==
'^')
983 return(RenderPostscript(image,draw_info,offset,metrics));
984 if (IsPathAccessible(draw_info->font) != MagickFalse)
986 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
990 type_info=GetTypeInfo(draw_info->font,&image->exception);
991 if (type_info == (
const TypeInfo *) NULL)
992 (void) ThrowMagickException(&image->exception,GetMagickModule(),
993 TypeWarning,
"UnableToReadFont",
"`%s'",draw_info->font);
995 if ((type_info == (
const TypeInfo *) NULL) &&
996 (draw_info->family != (
const char *) NULL))
998 if (strpbrk(draw_info->family,
",'\"") == (
char *) NULL)
999 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
1000 draw_info->stretch,draw_info->weight,&image->exception);
1001 if (type_info == (
const TypeInfo *) NULL)
1015 family=StringToArgv(draw_info->family,&number_families);
1016 for (i=1; i < (ssize_t) number_families; i++)
1018 (void) SubstituteString(&family[i],
",",
"");
1019 type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1020 draw_info->stretch,draw_info->weight,&image->exception);
1021 if ((type_info != (
const TypeInfo *) NULL) &&
1022 (LocaleCompare(family[i],type_info->family) == 0))
1025 for (i=0; i < (ssize_t) number_families; i++)
1026 family[i]=DestroyString(family[i]);
1027 family=(
char **) RelinquishMagickMemory(family);
1028 if (type_info == (
const TypeInfo *) NULL)
1029 (
void) ThrowMagickException(&image->exception,GetMagickModule(),
1030 TypeWarning,
"UnableToReadFont",
"`%s'",draw_info->family);
1033 font=GetPolicyValue(
"system:font");
1034 if (font != (
char *) NULL)
1036 if (IsPathAccessible(font) != MagickFalse)
1041 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1042 annotate_info->font=font;
1043 status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1045 annotate_info=DestroyDrawInfo(annotate_info);
1048 font=DestroyString(font);
1050 sans_exception=AcquireExceptionInfo();
1051 if (type_info == (
const TypeInfo *) NULL)
1052 type_info=GetTypeInfoByFamily(
"Noto Sans",draw_info->style,
1053 draw_info->stretch,draw_info->weight,sans_exception);
1054 if (type_info == (
const TypeInfo *) NULL)
1055 type_info=GetTypeInfoByFamily(
"Nimbus Sans",draw_info->style,
1056 draw_info->stretch,draw_info->weight,sans_exception);
1057 if (type_info == (
const TypeInfo *) NULL)
1058 type_info=GetTypeInfoByFamily((
const char *) NULL,draw_info->style,
1059 draw_info->stretch,draw_info->weight,sans_exception);
1060 if (type_info == (
const TypeInfo *) NULL)
1061 type_info=GetTypeInfo(
"*",sans_exception);
1062 sans_exception=DestroyExceptionInfo(sans_exception);
1063 if (type_info == (
const TypeInfo *) NULL)
1065 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics);
1068 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1069 annotate_info->face=type_info->face;
1070 if (type_info->metrics != (
char *) NULL)
1071 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1072 if (type_info->glyphs != (
char *) NULL)
1073 (
void) CloneString(&annotate_info->font,type_info->glyphs);
1074 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics);
1075 annotate_info=DestroyDrawInfo(annotate_info);
1112 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
1114 #if defined(MAGICKCORE_RAQM_DELEGATE)
1115 static size_t ComplexRaqmTextLayout(
const Image *image,
1116 const DrawInfo *draw_info,
const char *text,
const size_t length,
1136 if (rq == (raqm_t *) NULL)
1138 if (raqm_set_text_utf8(rq,text,length) == 0)
1140 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1142 if (raqm_set_freetype_face(rq,face) == 0)
1144 features=GetImageProperty(image,
"type:features");
1145 if (features != (
const char *) NULL)
1160 token_info=AcquireTokenInfo();
1161 token=AcquireString(
"");
1162 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1163 &breaker,&next,"e);
1164 while (status_token == 0)
1166 raqm_add_font_feature(rq,token,(
int) strlen(token));
1167 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1168 &breaker,&next,"e);
1170 token_info=DestroyTokenInfo(token_info);
1171 token=DestroyString(token);
1173 if (raqm_layout(rq) == 0)
1175 glyphs=raqm_get_glyphs(rq,&extent);
1176 if (glyphs == (raqm_glyph_t *) NULL)
1181 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(extent,
sizeof(**grapheme));
1187 for (i=0; i < (ssize_t) extent; i++)
1189 (*grapheme)[i].index=glyphs[i].index;
1190 (*grapheme)[i].x_offset=glyphs[i].x_offset;
1191 (*grapheme)[i].x_advance=glyphs[i].x_advance;
1192 (*grapheme)[i].y_offset=glyphs[i].y_offset;
1193 (*grapheme)[i].cluster=glyphs[i].cluster;
1200 static size_t ComplexTextLayout(
const DrawInfo *draw_info,
const char *text,
1201 const size_t length,
const FT_Face face,
const FT_Int32 flags,
1216 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(length+1,
sizeof(**grapheme));
1221 for (i=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p), i++)
1223 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,GetUTFCode(p));
1224 (*grapheme)[i].x_offset=0;
1225 (*grapheme)[i].y_offset=0;
1226 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1228 if (FT_HAS_KERNING(face))
1236 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1237 (*grapheme)[i].index,ft_kerning_default,&kerning);
1239 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1240 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1243 (void) FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1244 (*grapheme)[i].x_advance=face->glyph->advance.x;
1245 (*grapheme)[i].cluster=p-text;
1246 last_glyph=(*grapheme)[i].index;
1252 static void FTCloseStream(FT_Stream stream)
1254 FILE *file = (FILE *) stream->descriptor.pointer;
1255 if (file != (FILE *) NULL)
1256 (
void) fclose(file);
1257 stream->descriptor.pointer=NULL;
1260 static unsigned long FTReadStream(FT_Stream stream,
unsigned long offset,
1261 unsigned char *buffer,
unsigned long count)
1263 FILE *file = (FILE *) stream->descriptor.pointer;
1264 if (file == (FILE *) NULL)
1268 if (offset > stream->size)
1271 return((
unsigned long) fseek(file,(off_t) offset,SEEK_SET));
1273 return((
unsigned long) fread(buffer,1,count,file));
1276 static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1278 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1282 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1289 path[MaxTextExtent];
1291 affine=draw_info->affine;
1292 (void) FormatLocaleString(path,MaxTextExtent,
"C%g,%g %g,%g %g,%g",affine.tx+
1293 p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,
1294 affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1295 (void) ConcatenateString(&draw_info->primitive,path);
1299 static int TraceLineTo(FT_Vector *to,
DrawInfo *draw_info)
1305 path[MaxTextExtent];
1307 affine=draw_info->affine;
1308 (void) FormatLocaleString(path,MaxTextExtent,
"L%g,%g",affine.tx+to->x/64.0,
1309 affine.ty-to->y/64.0);
1310 (void) ConcatenateString(&draw_info->primitive,path);
1314 static int TraceMoveTo(FT_Vector *to,
DrawInfo *draw_info)
1320 path[MaxTextExtent];
1322 affine=draw_info->affine;
1323 (void) FormatLocaleString(path,MaxTextExtent,
"M%g,%g",affine.tx+to->x/64.0,
1324 affine.ty-to->y/64.0);
1325 (void) ConcatenateString(&draw_info->primitive,path);
1329 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1336 path[MaxTextExtent];
1338 affine=draw_info->affine;
1339 (void) FormatLocaleString(path,MaxTextExtent,
"Q%g,%g %g,%g",affine.tx+
1340 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1342 (void) ConcatenateString(&draw_info->primitive,path);
1346 static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1348 #if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1349 return(FT_Error_String(ft_status));
1351 magick_unreferenced(ft_status);
1352 return((
const char *) NULL);
1356 static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
1359 #if !defined(FT_OPEN_PATHNAME)
1360 #define FT_OPEN_PATHNAME ft_open_pathname
1363 #define ThrowFreetypeErrorException(tag,ft_status,value) \
1366 *error_string=FreetypeErrorMessage(ft_status); \
1367 if (error_string != (const char *) NULL) \
1368 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1369 tag,"`%s (%s)'",value, error_string); \
1371 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1372 tag,"`%s'",value); \
1375 typedef struct _GlyphInfo
1460 static FT_Outline_Funcs
1463 (FT_Outline_MoveTo_Func) TraceMoveTo,
1464 (FT_Outline_LineTo_Func) TraceLineTo,
1465 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1466 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1479 exception=(&image->exception);
1480 ft_status=FT_Init_FreeType(&library);
1482 ThrowFreetypeErrorException("UnableToInitializeFreetypeLibrary",ft_status,
1484 face_index=(FT_Long) draw_info->face;
1488 (
void) memset(&args,0,sizeof(args));
1489 if (draw_info->font == (
char *) NULL)
1491 const TypeInfo *type_info = GetTypeInfo(
"*",exception);
1492 if (type_info != (
const TypeInfo *) NULL)
1493 args.pathname=ConstantString(type_info->glyphs);
1496 if (*draw_info->font !=
'@')
1497 args.pathname=ConstantString(draw_info->font);
1503 ImageInfo *image_info=AcquireImageInfo();
1504 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1506 (void) SetImageInfo(image_info,0,exception);
1507 face_index=(FT_Long) image_info->scene;
1508 args.pathname=ConstantString(image_info->filename);
1509 image_info=DestroyImageInfo(image_info);
1514 stream=(FT_StreamRec *) AcquireCriticalMemory(
sizeof(*stream));
1515 (void) memset(stream,0,
sizeof(*stream));
1516 (void) stat(args.pathname,&attributes);
1517 stream->size=attributes.st_size;
1518 stream->descriptor.pointer=fopen_utf8(args.pathname,
"rb");
1519 stream->read=(&FTReadStream);
1520 stream->close=(&FTCloseStream);
1521 args.flags=FT_OPEN_STREAM;
1523 face=(FT_Face) NULL;
1524 ft_status=FT_Open_Face(library,&args,face_index,&face);
1527 (void) FT_Done_FreeType(library);
1528 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1529 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,args.pathname);
1530 args.pathname=DestroyString(args.pathname);
1531 return(MagickFalse);
1533 args.pathname=DestroyString(args.pathname);
1534 if ((draw_info->metrics != (
char *) NULL) &&
1535 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1536 (void) FT_Attach_File(face,draw_info->metrics);
1537 encoding_type=FT_ENCODING_UNICODE;
1538 ft_status=FT_Select_Charmap(face,encoding_type);
1539 if ((ft_status != 0) && (face->num_charmaps != 0))
1540 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1541 if (encoding != (
const char *) NULL)
1543 if (LocaleCompare(encoding,
"AdobeCustom") == 0)
1544 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1545 if (LocaleCompare(encoding,
"AdobeExpert") == 0)
1546 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1547 if (LocaleCompare(encoding,
"AdobeStandard") == 0)
1548 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1549 if (LocaleCompare(encoding,
"AppleRoman") == 0)
1550 encoding_type=FT_ENCODING_APPLE_ROMAN;
1551 if (LocaleCompare(encoding,
"BIG5") == 0)
1552 encoding_type=FT_ENCODING_BIG5;
1553 #if defined(FT_ENCODING_PRC)
1554 if (LocaleCompare(encoding,
"GB2312") == 0)
1555 encoding_type=FT_ENCODING_PRC;
1557 #if defined(FT_ENCODING_JOHAB)
1558 if (LocaleCompare(encoding,
"Johab") == 0)
1559 encoding_type=FT_ENCODING_JOHAB;
1561 #if defined(FT_ENCODING_ADOBE_LATIN_1)
1562 if (LocaleCompare(encoding,
"Latin-1") == 0)
1563 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1565 #if defined(FT_ENCODING_ADOBE_LATIN_2)
1566 if (LocaleCompare(encoding,
"Latin-2") == 0)
1567 encoding_type=FT_ENCODING_OLD_LATIN_2;
1569 if (LocaleCompare(encoding,
"None") == 0)
1570 encoding_type=FT_ENCODING_NONE;
1571 if (LocaleCompare(encoding,
"SJIScode") == 0)
1572 encoding_type=FT_ENCODING_SJIS;
1573 if (LocaleCompare(encoding,
"Symbol") == 0)
1574 encoding_type=FT_ENCODING_MS_SYMBOL;
1575 if (LocaleCompare(encoding,
"Unicode") == 0)
1576 encoding_type=FT_ENCODING_UNICODE;
1577 if (LocaleCompare(encoding,
"Wansung") == 0)
1578 encoding_type=FT_ENCODING_WANSUNG;
1579 ft_status=FT_Select_Charmap(face,encoding_type);
1582 (void) FT_Done_Face(face);
1583 (void) FT_Done_FreeType(library);
1584 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1585 ThrowFreetypeErrorException(
"UnrecognizedFontEncoding",ft_status,
1587 return(MagickFalse);
1593 resolution.x=DefaultResolution;
1594 resolution.y=DefaultResolution;
1595 if (draw_info->density != (
char *) NULL)
1603 flags=ParseGeometry(draw_info->density,&geometry_info);
1604 if ((flags & RhoValue) != 0)
1605 resolution.x=geometry_info.rho;
1606 resolution.y=resolution.x;
1607 if ((flags & SigmaValue) != 0)
1608 resolution.y=geometry_info.sigma;
1610 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1611 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1612 (FT_UInt) resolution.y);
1615 (void) FT_Done_Face(face);
1616 (void) FT_Done_FreeType(library);
1617 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1618 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,
1620 return(MagickFalse);
1622 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1623 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1624 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1625 metrics->descent=(
double) face->size->metrics.descender/64.0;
1626 if (face->size->metrics.ascender == 0)
1631 metrics->ascent=face->size->metrics.y_ppem;
1632 if (face->size->metrics.descender == 0)
1633 metrics->descent=face->size->metrics.y_ppem/-3.5;
1636 metrics->origin.x=0;
1637 metrics->origin.y=0;
1638 metrics->height=(double) face->size->metrics.height/64.0;
1639 metrics->max_advance=0.0;
1640 if (face->size->metrics.max_advance > MagickEpsilon)
1641 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1642 metrics->bounds.x1=0.0;
1643 metrics->bounds.y1=metrics->descent;
1644 metrics->bounds.x2=metrics->ascent+metrics->descent;
1645 metrics->bounds.y2=metrics->ascent+metrics->descent;
1646 metrics->underline_position=face->underline_position*
1647 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1648 metrics->underline_thickness=face->underline_thickness*
1649 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1651 FT_Get_First_Char(face,&first_glyph_id);
1652 if ((draw_info->text == (
char *) NULL) || (*draw_info->text ==
'\0') ||
1653 (first_glyph_id == 0))
1655 (void) FT_Done_Face(face);
1656 (void) FT_Done_FreeType(library);
1657 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1663 if (draw_info->debug != MagickFalse)
1664 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Font %s; "
1665 "font-encoding %s; text-encoding %s; pointsize %g",
1666 draw_info->font != (
char *) NULL ? draw_info->font :
"none",
1667 encoding != (
char *) NULL ? encoding :
"none",
1668 draw_info->encoding != (
char *) NULL ? draw_info->encoding :
"none",
1669 draw_info->pointsize);
1670 flags=FT_LOAD_DEFAULT;
1671 if (draw_info->render == MagickFalse)
1672 flags=FT_LOAD_NO_BITMAP;
1673 if (draw_info->text_antialias == MagickFalse)
1674 flags|=FT_LOAD_TARGET_MONO;
1677 #if defined(FT_LOAD_TARGET_LIGHT)
1678 flags|=FT_LOAD_TARGET_LIGHT;
1679 #elif defined(FT_LOAD_TARGET_LCD)
1680 flags|=FT_LOAD_TARGET_LCD;
1683 value=GetImageProperty(image,
"type:hinting");
1684 if ((value != (
const char *) NULL) && (LocaleCompare(value,
"off") == 0))
1685 flags|=FT_LOAD_NO_HINTING;
1687 glyph.image=(FT_Glyph) NULL;
1695 if (draw_info->render != MagickFalse)
1697 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1698 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1699 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1700 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1702 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1703 if (annotate_info->dash_pattern != (
double *) NULL)
1704 annotate_info->dash_pattern[0]=0.0;
1705 (void) CloneString(&annotate_info->primitive,
"path '");
1707 if (draw_info->render != MagickFalse)
1709 if (image->storage_class != DirectClass)
1710 (void) SetImageStorageClass(image,DirectClass);
1711 if (image->matte == MagickFalse)
1712 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1714 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1715 if (GetUTFCode(p) < 0)
1717 utf8=(
unsigned char *) NULL;
1718 if (GetUTFCode(p) == 0)
1722 utf8=ConvertLatin1ToUTF8((
unsigned char *) draw_info->text);
1723 if (utf8 != (
unsigned char *) NULL)
1727 #if defined(MAGICKCORE_RAQM_DELEGATE)
1728 length=ComplexRaqmTextLayout(image,draw_info,p,strlen(p),face,&grapheme,
1731 length=ComplexTextLayout(draw_info,p,strlen(p),face,flags,&grapheme);
1733 missing_glyph_id=FT_Get_Char_Index(face,
' ');
1735 last_character=(ssize_t) length-1;
1736 for (i=0; i < (ssize_t) length; i++)
1750 glyph.id=grapheme[i].index;
1752 glyph.id=missing_glyph_id;
1753 if ((glyph.id != 0) && (last_glyph_id != 0))
1754 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1755 glyph.origin=origin;
1756 glyph.origin.x+=grapheme[i].x_offset;
1757 glyph.origin.y+=grapheme[i].y_offset;
1758 if (glyph.image != (FT_Glyph) NULL)
1760 FT_Done_Glyph(glyph.image);
1761 glyph.image=(FT_Glyph) NULL;
1763 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1766 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1769 outline=((FT_OutlineGlyph) glyph.image)->outline;
1770 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1771 (IsEmptyOutline(outline) == MagickFalse))
1773 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1776 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1777 metrics->bounds.x1=(double) bounds.xMin;
1778 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1779 metrics->bounds.y1=(double) bounds.yMin;
1780 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1781 metrics->bounds.x2=(double) bounds.xMax;
1782 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1783 metrics->bounds.y2=(double) bounds.yMax;
1784 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1785 (draw_info->stroke_pattern != (
Image *) NULL)) &&
1786 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1791 annotate_info->affine.tx=glyph.origin.x/64.0;
1792 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1793 if (IsEmptyOutline(outline) == MagickFalse)
1794 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1797 FT_Vector_Transform(&glyph.origin,&affine);
1798 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1799 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1800 (FT_Vector *) NULL,MagickTrue);
1803 bitmap=(FT_BitmapGlyph) glyph.image;
1804 point.x=offset->x+bitmap->left;
1805 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1806 point.x+=(origin.x/64.0);
1807 point.y=offset->y-bitmap->top;
1808 if (draw_info->render != MagickFalse)
1822 transparent_fill=((draw_info->fill.opacity == TransparentOpacity) &&
1823 (draw_info->fill_pattern == (
Image *) NULL) &&
1824 (draw_info->stroke.opacity == TransparentOpacity) &&
1825 (draw_info->stroke_pattern == (
Image *) NULL)) ? MagickTrue :
1827 p=bitmap->bitmap.buffer;
1828 image_view=AcquireAuthenticCacheView(image,exception);
1829 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1850 if (status == MagickFalse)
1852 x_offset=CastDoubleToLong(ceil(point.x-0.5));
1853 y_offset=CastDoubleToLong(ceil(point.y+y-0.5));
1854 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1857 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1861 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1862 bitmap->bitmap.width,1,exception);
1863 active=q != (
PixelPacket *) NULL ? MagickTrue : MagickFalse;
1865 n=y*bitmap->bitmap.pitch;
1866 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
1868 x_offset=CastDoubleToLong(ceil(point.x+x-0.5));
1869 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1876 if (bitmap->bitmap.buffer != (
unsigned char *) NULL)
1878 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
1879 fill_opacity=(MagickRealType) (p[n])/(
1880 bitmap->bitmap.num_grays-1);
1882 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1883 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1884 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1886 if (draw_info->text_antialias == MagickFalse)
1887 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1888 if (active == MagickFalse)
1889 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1893 if (transparent_fill == MagickFalse)
1895 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
1896 fill_opacity=QuantumRange-fill_opacity*(QuantumRange-
1897 fill_color.opacity);
1898 MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q);
1906 Da=1.0-(QuantumScale*(QuantumRange-q->opacity));
1908 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*QuantumRange;
1909 SetPixelAlpha(q,fill_opacity);
1911 if (active == MagickFalse)
1913 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1914 if (sync == MagickFalse)
1919 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1920 if (sync == MagickFalse)
1923 image_view=DestroyCacheView(image_view);
1924 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1925 (draw_info->stroke_pattern != (
Image *) NULL)) &&
1926 (status != MagickFalse))
1931 annotate_info->linejoin=RoundJoin;
1932 annotate_info->affine.tx=offset->x;
1933 annotate_info->affine.ty=offset->y;
1934 (void) ConcatenateString(&annotate_info->primitive,
"'");
1935 if (strlen(annotate_info->primitive) > 7)
1936 (
void) DrawImage(image,annotate_info);
1937 (void) CloneString(&annotate_info->primitive,
"path '");
1940 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1941 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
1942 (IsUTFSpace(code) == MagickFalse))
1943 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
1945 if (i == last_character)
1946 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
1948 origin.x+=(FT_Pos) grapheme[i].x_advance;
1949 metrics->origin.x=(double) origin.x;
1950 metrics->origin.y=(
double) origin.y;
1951 if (metrics->origin.x > metrics->width)
1952 metrics->width=metrics->origin.x;
1953 last_glyph_id=glyph.id;
1954 code=GetUTFCode(p+grapheme[i].cluster);
1957 grapheme=(
GraphemeInfo *) RelinquishMagickMemory(grapheme);
1958 if (utf8 != (
unsigned char *) NULL)
1959 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
1960 if (glyph.image != (FT_Glyph) NULL)
1962 FT_Done_Glyph(glyph.image);
1963 glyph.image=(FT_Glyph) NULL;
1968 metrics->bounds.x1/=64.0;
1969 metrics->bounds.y1/=64.0;
1970 metrics->bounds.x2/=64.0;
1971 metrics->bounds.y2/=64.0;
1972 metrics->origin.x/=64.0;
1973 metrics->origin.y/=64.0;
1974 metrics->width/=64.0;
1978 annotate_info=DestroyDrawInfo(annotate_info);
1979 (void) FT_Done_Face(face);
1980 (void) FT_Done_FreeType(library);
1981 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1985 static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
1986 const char *magick_unused(encoding),
const PointInfo *offset,
1989 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1990 MissingDelegateWarning,
"DelegateLibrarySupportNotBuiltIn",
"`%s' (Freetype)",
1991 draw_info->font != (
char *) NULL ? draw_info->font :
"none");
1992 return(RenderPostscript(image,draw_info,offset,metrics));
2027 static char *EscapeParenthesis(
const char *source)
2041 assert(source != (
const char *) NULL);
2043 for (p=source; *p !=
'\0'; p++)
2045 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2048 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2053 destination=(
char *) NULL;
2054 if (~length >= (MaxTextExtent-1))
2055 destination=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
2056 sizeof(*destination));
2057 if (destination == (
char *) NULL)
2058 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2061 for (p=source; *p !=
'\0'; p++)
2063 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2068 return(destination);
2071 static MagickBooleanType RenderPostscript(
Image *image,
2075 filename[MaxTextExtent],
2076 geometry[MaxTextExtent],
2111 if (draw_info->debug != MagickFalse)
2112 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2113 "Font %s; pointsize %g",draw_info->font != (
char *) NULL ?
2114 draw_info->font :
"none",draw_info->pointsize);
2116 unique_file=AcquireUniqueFileResource(filename);
2117 if (unique_file != -1)
2118 file=fdopen(unique_file,
"wb");
2119 if ((unique_file == -1) || (file == (FILE *) NULL))
2121 ThrowFileException(&image->exception,FileOpenError,
"UnableToOpenFile",
2123 return(MagickFalse);
2125 (void) FormatLocaleFile(file,
"%%!PS-Adobe-3.0\n");
2126 (void) FormatLocaleFile(file,
"/ReencodeType\n");
2127 (void) FormatLocaleFile(file,
"{\n");
2128 (void) FormatLocaleFile(file,
" findfont dup length\n");
2129 (void) FormatLocaleFile(file,
2130 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2131 (void) FormatLocaleFile(file,
2132 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2133 (void) FormatLocaleFile(file,
"} bind def\n");
2137 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2138 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2139 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2142 length=strlen(draw_info->text);
2143 for (i=0; i <= (ssize_t) (length+2); i++)
2145 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2146 draw_info->affine.ry*2.0*draw_info->pointsize);
2147 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2148 draw_info->affine.sy*2.0*draw_info->pointsize);
2149 if (point.x > extent.x)
2151 if (point.y > extent.y)
2154 (void) FormatLocaleFile(file,
"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2155 extent.x/2.0,extent.y/2.0);
2156 (void) FormatLocaleFile(file,
"%g %g scale\n",draw_info->pointsize,
2157 draw_info->pointsize);
2158 if ((draw_info->font == (
char *) NULL) || (*draw_info->font ==
'\0') ||
2159 (strchr(draw_info->font,
'/') != (
char *) NULL))
2160 (
void) FormatLocaleFile(file,
2161 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2163 (
void) FormatLocaleFile(file,
2164 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2166 (void) FormatLocaleFile(file,
"[%g %g %g %g 0 0] concat\n",
2167 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2168 draw_info->affine.sy);
2169 text=EscapeParenthesis(draw_info->text);
2170 if (identity == MagickFalse)
2171 (void) FormatLocaleFile(file,
"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2173 (void) FormatLocaleFile(file,
"(%s) show\n",text);
2174 text=DestroyString(text);
2175 (void) FormatLocaleFile(file,
"showpage\n");
2176 (void) fclose(file);
2177 (void) FormatLocaleString(geometry,MaxTextExtent,
"%.20gx%.20g+0+0!",
2178 floor(extent.x+0.5),floor(extent.y+0.5));
2179 annotate_info=AcquireImageInfo();
2180 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,
"ps:%s",
2182 (void) CloneString(&annotate_info->page,geometry);
2183 if (draw_info->density != (
char *) NULL)
2184 (
void) CloneString(&annotate_info->density,draw_info->density);
2185 annotate_info->antialias=draw_info->text_antialias;
2186 annotate_image=ReadImage(annotate_info,&image->exception);
2187 CatchException(&image->exception);
2188 annotate_info=DestroyImageInfo(annotate_info);
2189 (void) RelinquishUniqueFileResource(filename);
2190 if (annotate_image == (
Image *) NULL)
2191 return(MagickFalse);
2192 resolution.x=DefaultResolution;
2193 resolution.y=DefaultResolution;
2194 if (draw_info->density != (
char *) NULL)
2202 flags=ParseGeometry(draw_info->density,&geometry_info);
2203 if ((flags & RhoValue) != 0)
2204 resolution.x=geometry_info.rho;
2205 resolution.y=resolution.x;
2206 if ((flags & SigmaValue) != 0)
2207 resolution.y=geometry_info.sigma;
2209 if (identity == MagickFalse)
2210 (void) TransformImage(&annotate_image,
"0x0",(
char *) NULL);
2216 crop_info=GetImageBoundingBox(annotate_image,&annotate_image->exception);
2217 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2218 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2219 crop_info.y=CastDoubleToLong(ceil((resolution.y/DefaultResolution)*
2221 (void) FormatLocaleString(geometry,MaxTextExtent,
2222 "%.20gx%.20g%+.20g%+.20g",(
double) crop_info.width,(double)
2223 crop_info.height,(
double) crop_info.x,(double) crop_info.y);
2224 (void) TransformImage(&annotate_image,geometry,(
char *) NULL);
2226 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2227 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2228 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2229 metrics->ascent=metrics->pixels_per_em.y;
2230 metrics->descent=metrics->pixels_per_em.y/-5.0;
2231 metrics->width=(double) annotate_image->columns/
2232 ExpandAffine(&draw_info->affine);
2233 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2234 metrics->max_advance=metrics->pixels_per_em.x;
2235 metrics->bounds.x1=0.0;
2236 metrics->bounds.y1=metrics->descent;
2237 metrics->bounds.x2=metrics->ascent+metrics->descent;
2238 metrics->bounds.y2=metrics->ascent+metrics->descent;
2239 metrics->underline_position=(-2.0);
2240 metrics->underline_thickness=1.0;
2241 if (draw_info->render == MagickFalse)
2243 annotate_image=DestroyImage(annotate_image);
2246 if (draw_info->fill.opacity != TransparentOpacity)
2263 if (image->matte == MagickFalse)
2264 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
2265 if (annotate_image->matte == MagickFalse)
2266 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel);
2267 fill_color=draw_info->fill;
2268 exception=(&image->exception);
2269 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2270 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2278 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2282 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2284 (void) GetFillColor(draw_info,x,y,&fill_color);
2285 SetPixelAlpha(q,ClampToQuantum((((QuantumRange-GetPixelIntensity(
2286 annotate_image,q))*(QuantumRange-fill_color.opacity))/
2288 SetPixelRed(q,fill_color.red);
2289 SetPixelGreen(q,fill_color.green);
2290 SetPixelBlue(q,fill_color.blue);
2293 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2294 if (sync == MagickFalse)
2297 annotate_view=DestroyCacheView(annotate_view);
2298 (void) CompositeImage(image,OverCompositeOp,annotate_image,
2299 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2300 metrics->descent)-0.5));
2302 annotate_image=DestroyImage(annotate_image);
2336 static MagickBooleanType RenderX11(
Image *image,
const DrawInfo *draw_info,
2343 ActivateSemaphoreInfo(&annotate_semaphore);
2344 LockSemaphoreInfo(annotate_semaphore);
2345 status=XRenderImage(image,draw_info,offset,metrics);
2346 UnlockSemaphoreInfo(annotate_semaphore);