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)
133 static MagickBooleanType
158 MagickExport MagickBooleanType AnnotateComponentGenesis(
void)
161 annotate_semaphore=AllocateSemaphoreInfo();
183 MagickExport
void AnnotateComponentTerminus(
void)
186 ActivateSemaphoreInfo(&annotate_semaphore);
187 DestroySemaphoreInfo(&annotate_semaphore);
214 MagickExport MagickBooleanType AnnotateImage(
Image *image,
219 color[MaxTextExtent],
220 primitive[MaxTextExtent],
253 assert(image != (
Image *) NULL);
254 assert(image->signature == MagickCoreSignature);
255 if (IsEventLogging() != MagickFalse)
256 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
257 assert(draw_info != (
DrawInfo *) NULL);
258 assert(draw_info->signature == MagickCoreSignature);
259 if (draw_info->text == (
char *) NULL)
261 if (*draw_info->text ==
'\0')
263 annotate=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
265 annotate->text=(
char *) NULL;
266 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
268 for (p=text; *p !=
'\0'; p++)
271 textlist=(
char **) AcquireQuantumMemory(number_lines+1,
sizeof(*textlist));
272 if (textlist == (
char **) NULL)
274 annotate_info=DestroyDrawInfo(annotate_info);
275 annotate=DestroyDrawInfo(annotate);
276 text=DestroyString(text);
280 for (i=0; i < (ssize_t) number_lines; i++)
286 for (q=p; *q !=
'\0'; q++)
287 if ((*q ==
'\r') || (*q ==
'\n'))
297 textlist[i]=(
char *) NULL;
298 SetGeometry(image,&geometry);
299 SetGeometryInfo(&geometry_info);
300 if (annotate_info->geometry != (
char *) NULL)
302 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
304 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
306 if (SetImageStorageClass(image,DirectClass) == MagickFalse)
308 annotate_info=DestroyDrawInfo(annotate_info);
309 annotate=DestroyDrawInfo(annotate);
310 textlist=(
char **) RelinquishMagickMemory(textlist);
311 text=DestroyString(text);
314 if (IsGrayColorspace(image->colorspace) != MagickFalse)
315 (void) SetImageColorspace(image,sRGBColorspace);
317 (void) memset(&metrics,0,
sizeof(metrics));
318 for (i=0; textlist[i] != (
char *) NULL; i++)
320 if (*textlist[i] ==
'\0')
325 annotate_info->affine.tx=geometry_info.xi-image->page.x;
326 annotate_info->affine.ty=geometry_info.psi-image->page.y;
327 (void) CloneString(&annotate->text,textlist[i]);
328 if ((metrics.width == 0) || (annotate->gravity != NorthWestGravity))
329 (void) GetTypeMetrics(image,annotate,&metrics);
330 height=CastDoubleToUnsigned(metrics.ascent-metrics.descent+0.5);
332 height=draw_info->pointsize;
333 height+=(size_t) floor(draw_info->interline_spacing+0.5);
334 switch (annotate->gravity)
336 case UndefinedGravity:
339 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
340 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
343 case (GravityType) NorthWestGravity:
345 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
346 annotate_info->affine.ry*height+annotate_info->affine.ry*
347 (metrics.ascent+metrics.descent);
348 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
349 annotate_info->affine.sy*height+annotate_info->affine.sy*
353 case (GravityType) NorthGravity:
355 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
356 geometry.width/2.0+i*annotate_info->affine.ry*height-
357 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
358 (metrics.ascent+metrics.descent);
359 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
360 annotate_info->affine.sy*height+annotate_info->affine.sy*
361 metrics.ascent-annotate_info->affine.rx*metrics.width/2.0;
364 case (GravityType) NorthEastGravity:
366 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
367 geometry.width+i*annotate_info->affine.ry*height-
368 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
369 (metrics.ascent+metrics.descent)-1.0;
370 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
371 annotate_info->affine.sy*height+annotate_info->affine.sy*
372 metrics.ascent-annotate_info->affine.rx*metrics.width;
375 case (GravityType) WestGravity:
377 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
378 annotate_info->affine.ry*height+annotate_info->affine.ry*
379 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
380 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
381 geometry.height/2.0+i*annotate_info->affine.sy*height+
382 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
383 (number_lines-1.0)*height)/2.0;
386 case (GravityType) StaticGravity:
387 case (GravityType) CenterGravity:
389 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
390 geometry.width/2.0+i*annotate_info->affine.ry*height-
391 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
392 (metrics.ascent+metrics.descent-(number_lines-1)*height)/2.0;
393 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
394 geometry.height/2.0+i*annotate_info->affine.sy*height-
395 annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
396 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
399 case (GravityType) EastGravity:
401 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
402 geometry.width+i*annotate_info->affine.ry*height-
403 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
404 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0-1.0;
405 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
406 geometry.height/2.0+i*annotate_info->affine.sy*height-
407 annotate_info->affine.rx*metrics.width+annotate_info->affine.sy*
408 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
411 case (GravityType) SouthWestGravity:
413 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
414 annotate_info->affine.ry*height-annotate_info->affine.ry*
415 (number_lines-1.0)*height;
416 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
417 geometry.height+i*annotate_info->affine.sy*height-
418 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
421 case (GravityType) SouthGravity:
423 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
424 geometry.width/2.0+i*annotate_info->affine.ry*height-
425 annotate_info->affine.sx*metrics.width/2.0-annotate_info->affine.ry*
426 (number_lines-1.0)*height/2.0;
427 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
428 geometry.height+i*annotate_info->affine.sy*height-
429 annotate_info->affine.rx*metrics.width/2.0-annotate_info->affine.sy*
430 (number_lines-1.0)*height+metrics.descent;
433 case (GravityType) SouthEastGravity:
435 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
436 geometry.width+i*annotate_info->affine.ry*height-
437 annotate_info->affine.sx*metrics.width-annotate_info->affine.ry*
438 (number_lines-1.0)*height-1.0;
439 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
440 geometry.height+i*annotate_info->affine.sy*height-
441 annotate_info->affine.rx*metrics.width-annotate_info->affine.sy*
442 (number_lines-1.0)*height+metrics.descent;
446 switch (annotate->align)
450 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
451 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
456 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
457 annotate_info->affine.sx*metrics.width/2.0;
458 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
459 annotate_info->affine.rx*metrics.width/2.0;
464 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
465 annotate_info->affine.sx*metrics.width;
466 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
467 annotate_info->affine.rx*metrics.width;
473 if (draw_info->undercolor.opacity != TransparentOpacity)
482 undercolor_info->fill=draw_info->undercolor;
483 undercolor_info->affine=draw_info->affine;
484 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
485 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
486 (void) FormatLocaleString(primitive,MaxTextExtent,
487 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(
double) height);
488 (void) CloneString(&undercolor_info->primitive,primitive);
489 (void) DrawImage(image,undercolor_info);
490 (void) DestroyDrawInfo(undercolor_info);
492 annotate_info->affine.tx=offset.x;
493 annotate_info->affine.ty=offset.y;
494 pixel=annotate_info->fill;
495 if (annotate_info->stroke.opacity != TransparentOpacity)
496 pixel=annotate_info->stroke;
497 (void) QueryColorname(image,&pixel,SVGCompliance,color,&image->exception);
498 (void) FormatLocaleString(primitive,MagickPathExtent,
"stroke %s "
499 "stroke-width %g line 0,0 %g,0",color,(
double)
500 metrics.underline_thickness,(double) metrics.width);
504 switch (annotate->decorate)
506 case OverlineDecoration:
508 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
509 metrics.descent-metrics.underline_position));
510 (void) CloneString(&annotate_info->primitive,primitive);
511 (void) DrawImage(image,annotate_info);
514 case UnderlineDecoration:
516 annotate_info->affine.ty-=(draw_info->affine.sy*
517 metrics.underline_position);
518 (void) CloneString(&annotate_info->primitive,primitive);
519 (void) DrawImage(image,annotate_info);
525 status=RenderType(image,annotate,&offset,&metrics);
526 if (status == MagickFalse)
528 if (annotate->decorate == LineThroughDecoration)
530 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
531 metrics.underline_position+metrics.descent*2)/2.0);
532 (void) CloneString(&annotate_info->primitive,primitive);
533 (void) DrawImage(image,annotate_info);
539 annotate_info=DestroyDrawInfo(annotate_info);
540 annotate=DestroyDrawInfo(annotate);
541 textlist=(
char **) RelinquishMagickMemory(textlist);
542 text=DestroyString(text);
579 static inline char *ReplaceSpaceWithNewline(
char **caption,
char *space)
584 if ((caption == (
char **) NULL) || (space == (
char *) NULL))
585 return((
char *) NULL);
586 octets=(size_t) GetUTFOctets(space);
598 length=strlen(*caption);
600 offset=space-(*caption);
601 target=AcquireString(*caption);
602 CopyMagickString(target,*caption,offset+2);
603 ConcatenateMagickString(target,space+octets,length);
604 (void) DestroyString(*caption);
606 space=(*caption)+offset;
611 MagickExport ssize_t FormatMagickCaption(
Image *image,
DrawInfo *draw_info,
612 const MagickBooleanType split,
TypeMetric *metrics,
char **caption)
629 if ((caption == (
char **) NULL) || (*caption == (
char *) NULL))
634 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
645 if ((IsUTFSpace(code) != MagickFalse) &&
646 (IsNonBreakingUTFSpace(code) == MagickFalse))
649 if (width > image->columns)
650 p=ReplaceSpaceWithNewline(caption,s);
652 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
655 status=GetTypeMetrics(image,draw_info,metrics);
656 if (status == MagickFalse)
658 width=CastDoubleToUnsigned(metrics->width+draw_info->stroke_width+0.5);
659 if (width <= image->columns)
661 if (s != (
char *) NULL)
662 p=ReplaceSpaceWithNewline(caption,s);
664 if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
670 if ((n > 0) && ((*caption)[n-1] !=
'\n'))
675 target=AcquireString(*caption);
676 CopyMagickString(target,*caption,n+1);
677 ConcatenateMagickString(target,
"\n",strlen(*caption)+1);
678 ConcatenateMagickString(target,p,strlen(*caption)+2);
679 (void) DestroyString(*caption);
688 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
689 if (GetUTFCode(p) ==
'\n')
741 MagickExport MagickBooleanType GetMultilineTypeMetrics(
Image *image,
768 assert(image != (
Image *) NULL);
769 assert(image->signature == MagickCoreSignature);
770 if (IsEventLogging() != MagickFalse)
771 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
772 assert(draw_info != (
DrawInfo *) NULL);
773 assert(draw_info->text != (
char *) NULL);
774 assert(draw_info->signature == MagickCoreSignature);
775 if (*draw_info->text ==
'\0')
777 (void) ThrowMagickException(&image->exception,GetMagickModule(),
778 OptionError,
"LabelExpected",
"`%s'",image->filename);
781 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
782 annotate_info->text=DestroyString(annotate_info->text);
786 textlist=StringToStrings(draw_info->text,&count);
787 if (textlist == (
char **) NULL)
789 annotate_info=DestroyDrawInfo(annotate_info);
792 annotate_info->render=MagickFalse;
793 annotate_info->direction=UndefinedDirection;
794 (void) memset(metrics,0,
sizeof(*metrics));
795 (void) memset(&extent,0,
sizeof(extent));
799 annotate_info->text=textlist[0];
800 status=GetTypeMetrics(image,annotate_info,&extent);
802 height=(count*(size_t) (metrics->ascent-metrics->descent+
803 0.5)+(count-1)*draw_info->interline_spacing);
804 size=(MagickSizeType) fabs(height);
805 if (AcquireMagickResource(HeightResource,size) == MagickFalse)
807 (void) ThrowMagickException(&image->exception,GetMagickModule(),
808 ImageError,
"WidthOrHeightExceedsLimit",
"`%s'",image->filename);
813 for (i=1; i < (ssize_t) count; i++)
815 annotate_info->text=textlist[i];
816 status=GetTypeMetrics(image,annotate_info,&extent);
817 if (status == MagickFalse)
819 if (extent.width > metrics->width)
821 size=(MagickSizeType) fabs(extent.width);
822 if (AcquireMagickResource(WidthResource,size) == MagickFalse)
824 (void) ThrowMagickException(&image->exception,GetMagickModule(),
825 ImageError,
"WidthOrHeightExceedsLimit",
"`%s'",image->filename);
830 metrics->height=(double) height;
835 annotate_info->text=(
char *) NULL;
836 annotate_info=DestroyDrawInfo(annotate_info);
837 for (i=0; i < (ssize_t) count; i++)
838 textlist[i]=DestroyString(textlist[i]);
839 textlist=(
char **) RelinquishMagickMemory(textlist);
887 MagickExport MagickBooleanType GetTypeMetrics(
Image *image,
899 assert(image != (
Image *) NULL);
900 assert(image->signature == MagickCoreSignature);
901 if (IsEventLogging() != MagickFalse)
902 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
903 assert(draw_info != (
DrawInfo *) NULL);
904 assert(draw_info->text != (
char *) NULL);
905 assert(draw_info->signature == MagickCoreSignature);
906 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
907 annotate_info->render=MagickFalse;
908 annotate_info->direction=UndefinedDirection;
909 (void) memset(metrics,0,
sizeof(*metrics));
912 status=RenderType(image,annotate_info,&offset,metrics);
913 if (draw_info->debug != MagickFalse)
914 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Metrics: text: %s; "
915 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
916 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
917 "underline position: %g; underline thickness: %g",annotate_info->text,
918 metrics->width,metrics->height,metrics->ascent,metrics->descent,
919 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
920 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
921 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
922 metrics->underline_position,metrics->underline_thickness);
923 annotate_info=DestroyDrawInfo(annotate_info);
957 static MagickBooleanType RenderType(
Image *image,
const DrawInfo *draw_info,
976 if (draw_info->font != (
char *) NULL)
978 if (*draw_info->font ==
'@')
980 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
984 if (*draw_info->font ==
'-')
985 return(RenderX11(image,draw_info,offset,metrics));
986 if (*draw_info->font ==
'^')
987 return(RenderPostscript(image,draw_info,offset,metrics));
988 if (IsPathAccessible(draw_info->font) != MagickFalse)
990 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
994 type_info=GetTypeInfo(draw_info->font,&image->exception);
995 if (type_info == (
const TypeInfo *) NULL)
996 (void) ThrowMagickException(&image->exception,GetMagickModule(),
997 TypeWarning,
"UnableToReadFont",
"`%s'",draw_info->font);
999 if ((type_info == (
const TypeInfo *) NULL) &&
1000 (draw_info->family != (
const char *) NULL))
1002 if (strpbrk(draw_info->family,
",'\"") == (
char *) NULL)
1003 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
1004 draw_info->stretch,draw_info->weight,&image->exception);
1005 if (type_info == (
const TypeInfo *) NULL)
1019 family=StringToArgv(draw_info->family,&number_families);
1020 for (i=1; i < (ssize_t) number_families; i++)
1022 (void) SubstituteString(&family[i],
",",
"");
1023 type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1024 draw_info->stretch,draw_info->weight,&image->exception);
1025 if ((type_info != (
const TypeInfo *) NULL) &&
1026 (LocaleCompare(family[i],type_info->family) == 0))
1029 for (i=0; i < (ssize_t) number_families; i++)
1030 family[i]=DestroyString(family[i]);
1031 family=(
char **) RelinquishMagickMemory(family);
1032 if (type_info == (
const TypeInfo *) NULL)
1033 (
void) ThrowMagickException(&image->exception,GetMagickModule(),
1034 TypeWarning,
"UnableToReadFont",
"`%s'",draw_info->family);
1037 font=GetPolicyValue(
"system:font");
1038 if (font != (
char *) NULL)
1040 if (IsPathAccessible(font) != MagickFalse)
1045 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1046 annotate_info->font=font;
1047 status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1049 annotate_info=DestroyDrawInfo(annotate_info);
1052 font=DestroyString(font);
1054 sans_exception=AcquireExceptionInfo();
1055 if (type_info == (
const TypeInfo *) NULL)
1056 type_info=GetTypeInfoByFamily(
"Noto Sans",draw_info->style,
1057 draw_info->stretch,draw_info->weight,sans_exception);
1058 if (type_info == (
const TypeInfo *) NULL)
1059 type_info=GetTypeInfoByFamily(
"Nimbus Sans",draw_info->style,
1060 draw_info->stretch,draw_info->weight,sans_exception);
1061 if (type_info == (
const TypeInfo *) NULL)
1062 type_info=GetTypeInfoByFamily((
const char *) NULL,draw_info->style,
1063 draw_info->stretch,draw_info->weight,sans_exception);
1064 if (type_info == (
const TypeInfo *) NULL)
1065 type_info=GetTypeInfo(
"*",sans_exception);
1066 sans_exception=DestroyExceptionInfo(sans_exception);
1067 if (type_info == (
const TypeInfo *) NULL)
1069 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics);
1072 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1073 annotate_info->face=type_info->face;
1074 if (type_info->metrics != (
char *) NULL)
1075 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1076 if (type_info->glyphs != (
char *) NULL)
1077 (
void) CloneString(&annotate_info->font,type_info->glyphs);
1078 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics);
1079 annotate_info=DestroyDrawInfo(annotate_info);
1116 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
1118 #if defined(MAGICKCORE_RAQM_DELEGATE)
1119 static size_t ComplexRaqmTextLayout(
const Image *image,
1120 const DrawInfo *draw_info,
const char *text,
const size_t length,
1140 if (rq == (raqm_t *) NULL)
1142 if (raqm_set_text_utf8(rq,text,length) == 0)
1144 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1146 if (raqm_set_freetype_face(rq,face) == 0)
1148 features=GetImageProperty(image,
"type:features");
1149 if (features != (
const char *) NULL)
1164 token_info=AcquireTokenInfo();
1165 token=AcquireString(
"");
1166 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1167 &breaker,&next,"e);
1168 while (status_token == 0)
1170 raqm_add_font_feature(rq,token,(
int) strlen(token));
1171 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1172 &breaker,&next,"e);
1174 token_info=DestroyTokenInfo(token_info);
1175 token=DestroyString(token);
1177 if (raqm_layout(rq) == 0)
1179 glyphs=raqm_get_glyphs(rq,&extent);
1180 if (glyphs == (raqm_glyph_t *) NULL)
1185 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(extent,
sizeof(**grapheme));
1191 for (i=0; i < (ssize_t) extent; i++)
1193 (*grapheme)[i].index=glyphs[i].index;
1194 (*grapheme)[i].x_offset=glyphs[i].x_offset;
1195 (*grapheme)[i].x_advance=glyphs[i].x_advance;
1196 (*grapheme)[i].y_offset=glyphs[i].y_offset;
1197 (*grapheme)[i].y_advance=glyphs[i].y_advance;
1198 (*grapheme)[i].cluster=glyphs[i].cluster;
1205 static size_t ComplexTextLayout(
const DrawInfo *draw_info,
const char *text,
1206 const size_t length,
const FT_Face face,
const FT_Int32 flags,
1221 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(length+1,
sizeof(**grapheme));
1226 for (i=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p), i++)
1228 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,GetUTFCode(p));
1229 (*grapheme)[i].x_offset=0;
1230 (*grapheme)[i].y_offset=0;
1231 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1233 if (FT_HAS_KERNING(face))
1241 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1242 (*grapheme)[i].index,ft_kerning_default,&kerning);
1244 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1245 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1248 (void) FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1249 (*grapheme)[i].x_advance=face->glyph->advance.x;
1250 (*grapheme)[i].y_advance=face->glyph->advance.y;
1251 (*grapheme)[i].cluster=p-text;
1252 last_glyph=(*grapheme)[i].index;
1258 static void FTCloseStream(FT_Stream stream)
1260 FILE *file = (FILE *) stream->descriptor.pointer;
1261 if (file != (FILE *) NULL)
1262 (
void) fclose(file);
1263 stream->descriptor.pointer=NULL;
1266 static unsigned long FTReadStream(FT_Stream stream,
unsigned long offset,
1267 unsigned char *buffer,
unsigned long count)
1269 FILE *file = (FILE *) stream->descriptor.pointer;
1270 if (file == (FILE *) NULL)
1274 if (offset > stream->size)
1277 return((
unsigned long) fseek(file,(off_t) offset,SEEK_SET));
1279 return((
unsigned long) fread(buffer,1,count,file));
1282 static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1284 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1288 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1295 path[MaxTextExtent];
1297 affine=draw_info->affine;
1298 (void) FormatLocaleString(path,MaxTextExtent,
"C%g,%g %g,%g %g,%g",affine.tx+
1299 p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,
1300 affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1301 (void) ConcatenateString(&draw_info->primitive,path);
1305 static int TraceLineTo(FT_Vector *to,
DrawInfo *draw_info)
1311 path[MaxTextExtent];
1313 affine=draw_info->affine;
1314 (void) FormatLocaleString(path,MaxTextExtent,
"L%g,%g",affine.tx+to->x/64.0,
1315 affine.ty-to->y/64.0);
1316 (void) ConcatenateString(&draw_info->primitive,path);
1320 static int TraceMoveTo(FT_Vector *to,
DrawInfo *draw_info)
1326 path[MaxTextExtent];
1328 affine=draw_info->affine;
1329 (void) FormatLocaleString(path,MaxTextExtent,
"M%g,%g",affine.tx+to->x/64.0,
1330 affine.ty-to->y/64.0);
1331 (void) ConcatenateString(&draw_info->primitive,path);
1335 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1342 path[MaxTextExtent];
1344 affine=draw_info->affine;
1345 (void) FormatLocaleString(path,MaxTextExtent,
"Q%g,%g %g,%g",affine.tx+
1346 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1348 (void) ConcatenateString(&draw_info->primitive,path);
1352 static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1354 #if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1355 return(FT_Error_String(ft_status));
1357 magick_unreferenced(ft_status);
1358 return((
const char *) NULL);
1362 static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
1365 #if !defined(FT_OPEN_PATHNAME)
1366 #define FT_OPEN_PATHNAME ft_open_pathname
1369 #define ThrowFreetypeErrorException(tag,ft_status,value) \
1372 *error_string=FreetypeErrorMessage(ft_status); \
1373 if (error_string != (const char *) NULL) \
1374 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1375 tag,"`%s (%s)'",value, error_string); \
1377 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1378 tag,"`%s'",value); \
1381 typedef struct _GlyphInfo
1466 static FT_Outline_Funcs
1469 (FT_Outline_MoveTo_Func) TraceMoveTo,
1470 (FT_Outline_LineTo_Func) TraceLineTo,
1471 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1472 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1485 exception=(&image->exception);
1486 ft_status=FT_Init_FreeType(&library);
1488 ThrowFreetypeErrorException(
"UnableToInitializeFreetypeLibrary",ft_status,
1490 face_index=(FT_Long) draw_info->face;
1494 (
void) memset(&args,0,
sizeof(args));
1495 if (draw_info->font == (
char *) NULL)
1497 const TypeInfo *type_info = GetTypeInfo(
"*",exception);
1498 if (type_info != (
const TypeInfo *) NULL)
1499 args.pathname=ConstantString(type_info->glyphs);
1502 if (*draw_info->font !=
'@')
1503 args.pathname=ConstantString(draw_info->font);
1509 ImageInfo *image_info=AcquireImageInfo();
1510 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1512 (void) SetImageInfo(image_info,0,exception);
1513 face_index=(FT_Long) image_info->scene;
1514 args.pathname=ConstantString(image_info->filename);
1515 image_info=DestroyImageInfo(image_info);
1520 stream=(FT_StreamRec *) AcquireCriticalMemory(
sizeof(*stream));
1521 (void) memset(stream,0,
sizeof(*stream));
1522 (void) stat(args.pathname,&attributes);
1523 stream->size=attributes.st_size;
1524 stream->descriptor.pointer=fopen_utf8(args.pathname,
"rb");
1525 stream->read=(&FTReadStream);
1526 stream->close=(&FTCloseStream);
1527 args.flags=FT_OPEN_STREAM;
1529 face=(FT_Face) NULL;
1530 ft_status=FT_Open_Face(library,&args,face_index,&face);
1533 (void) FT_Done_FreeType(library);
1534 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1535 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,args.pathname);
1536 args.pathname=DestroyString(args.pathname);
1537 return(MagickFalse);
1539 args.pathname=DestroyString(args.pathname);
1540 if ((draw_info->metrics != (
char *) NULL) &&
1541 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1542 (void) FT_Attach_File(face,draw_info->metrics);
1543 encoding_type=FT_ENCODING_UNICODE;
1544 ft_status=FT_Select_Charmap(face,encoding_type);
1545 if ((ft_status != 0) && (face->num_charmaps != 0))
1546 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1547 if (encoding != (
const char *) NULL)
1549 if (LocaleCompare(encoding,
"AdobeCustom") == 0)
1550 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1551 if (LocaleCompare(encoding,
"AdobeExpert") == 0)
1552 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1553 if (LocaleCompare(encoding,
"AdobeStandard") == 0)
1554 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1555 if (LocaleCompare(encoding,
"AppleRoman") == 0)
1556 encoding_type=FT_ENCODING_APPLE_ROMAN;
1557 if (LocaleCompare(encoding,
"BIG5") == 0)
1558 encoding_type=FT_ENCODING_BIG5;
1559 #if defined(FT_ENCODING_PRC)
1560 if (LocaleCompare(encoding,
"GB2312") == 0)
1561 encoding_type=FT_ENCODING_PRC;
1563 #if defined(FT_ENCODING_JOHAB)
1564 if (LocaleCompare(encoding,
"Johab") == 0)
1565 encoding_type=FT_ENCODING_JOHAB;
1567 #if defined(FT_ENCODING_ADOBE_LATIN_1)
1568 if (LocaleCompare(encoding,
"Latin-1") == 0)
1569 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1571 #if defined(FT_ENCODING_ADOBE_LATIN_2)
1572 if (LocaleCompare(encoding,
"Latin-2") == 0)
1573 encoding_type=FT_ENCODING_OLD_LATIN_2;
1575 if (LocaleCompare(encoding,
"None") == 0)
1576 encoding_type=FT_ENCODING_NONE;
1577 if (LocaleCompare(encoding,
"SJIScode") == 0)
1578 encoding_type=FT_ENCODING_SJIS;
1579 if (LocaleCompare(encoding,
"Symbol") == 0)
1580 encoding_type=FT_ENCODING_MS_SYMBOL;
1581 if (LocaleCompare(encoding,
"Unicode") == 0)
1582 encoding_type=FT_ENCODING_UNICODE;
1583 if (LocaleCompare(encoding,
"Wansung") == 0)
1584 encoding_type=FT_ENCODING_WANSUNG;
1585 ft_status=FT_Select_Charmap(face,encoding_type);
1588 (void) FT_Done_Face(face);
1589 (void) FT_Done_FreeType(library);
1590 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1591 ThrowFreetypeErrorException(
"UnrecognizedFontEncoding",ft_status,
1593 return(MagickFalse);
1599 resolution.x=DefaultResolution;
1600 resolution.y=DefaultResolution;
1601 if (draw_info->density != (
char *) NULL)
1609 flags=ParseGeometry(draw_info->density,&geometry_info);
1610 if ((flags & RhoValue) != 0)
1611 resolution.x=geometry_info.rho;
1612 resolution.y=resolution.x;
1613 if ((flags & SigmaValue) != 0)
1614 resolution.y=geometry_info.sigma;
1616 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1617 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1618 (FT_UInt) resolution.y);
1621 (void) FT_Done_Face(face);
1622 (void) FT_Done_FreeType(library);
1623 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1624 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,
1626 return(MagickFalse);
1628 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1629 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1630 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1631 metrics->descent=(
double) face->size->metrics.descender/64.0;
1632 if (face->size->metrics.ascender == 0)
1637 metrics->ascent=face->size->metrics.y_ppem;
1638 if (face->size->metrics.descender == 0)
1639 metrics->descent=face->size->metrics.y_ppem/-3.5;
1642 metrics->origin.x=0;
1643 metrics->origin.y=0;
1644 metrics->height=(double) face->size->metrics.height/64.0;
1645 metrics->max_advance=0.0;
1646 if (face->size->metrics.max_advance > MagickEpsilon)
1647 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1648 metrics->bounds.x1=0.0;
1649 metrics->bounds.y1=metrics->descent;
1650 metrics->bounds.x2=metrics->ascent+metrics->descent;
1651 metrics->bounds.y2=metrics->ascent+metrics->descent;
1652 metrics->underline_position=face->underline_position*
1653 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1654 metrics->underline_thickness=face->underline_thickness*
1655 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1657 FT_Get_First_Char(face,&first_glyph_id);
1658 if ((draw_info->text == (
char *) NULL) || (*draw_info->text ==
'\0') ||
1659 (first_glyph_id == 0))
1661 (void) FT_Done_Face(face);
1662 (void) FT_Done_FreeType(library);
1663 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1669 if (draw_info->debug != MagickFalse)
1670 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Font %s; "
1671 "font-encoding %s; text-encoding %s; pointsize %g",
1672 draw_info->font != (
char *) NULL ? draw_info->font :
"none",
1673 encoding != (
char *) NULL ? encoding :
"none",
1674 draw_info->encoding != (
char *) NULL ? draw_info->encoding :
"none",
1675 draw_info->pointsize);
1676 flags=FT_LOAD_DEFAULT;
1677 if (draw_info->render == MagickFalse)
1678 flags=FT_LOAD_NO_BITMAP;
1679 if (draw_info->text_antialias == MagickFalse)
1680 flags|=FT_LOAD_TARGET_MONO;
1683 #if defined(FT_LOAD_TARGET_LIGHT)
1684 flags|=FT_LOAD_TARGET_LIGHT;
1685 #elif defined(FT_LOAD_TARGET_LCD)
1686 flags|=FT_LOAD_TARGET_LCD;
1689 value=GetImageProperty(image,
"type:hinting");
1690 if ((value != (
const char *) NULL) && (LocaleCompare(value,
"off") == 0))
1691 flags|=FT_LOAD_NO_HINTING;
1693 glyph.image=(FT_Glyph) NULL;
1701 if (draw_info->render != MagickFalse)
1703 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1704 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1705 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1706 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1708 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1709 if (annotate_info->dash_pattern != (
double *) NULL)
1710 annotate_info->dash_pattern[0]=0.0;
1711 (void) CloneString(&annotate_info->primitive,
"path '");
1713 if (draw_info->render != MagickFalse)
1715 if (image->storage_class != DirectClass)
1716 (void) SetImageStorageClass(image,DirectClass);
1717 if (image->matte == MagickFalse)
1718 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1720 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1721 if (GetUTFCode(p) < 0)
1723 utf8=(
unsigned char *) NULL;
1724 if (GetUTFCode(p) == 0)
1728 utf8=ConvertLatin1ToUTF8((
unsigned char *) draw_info->text);
1729 if (utf8 != (
unsigned char *) NULL)
1733 #if defined(MAGICKCORE_RAQM_DELEGATE)
1734 length=ComplexRaqmTextLayout(image,draw_info,p,strlen(p),face,&grapheme,
1737 length=ComplexTextLayout(draw_info,p,strlen(p),face,flags,&grapheme);
1739 missing_glyph_id=FT_Get_Char_Index(face,
' ');
1741 last_character=(ssize_t) length-1;
1742 for (i=0; i < (ssize_t) length; i++)
1756 glyph.id=grapheme[i].index;
1758 glyph.id=missing_glyph_id;
1759 if ((glyph.id != 0) && (last_glyph_id != 0))
1760 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1761 glyph.origin=origin;
1762 glyph.origin.x+=grapheme[i].x_offset;
1763 glyph.origin.y+=grapheme[i].y_offset;
1764 if (glyph.image != (FT_Glyph) NULL)
1766 FT_Done_Glyph(glyph.image);
1767 glyph.image=(FT_Glyph) NULL;
1769 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1772 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1775 outline=((FT_OutlineGlyph) glyph.image)->outline;
1776 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1777 (IsEmptyOutline(outline) == MagickFalse))
1779 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1782 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1783 metrics->bounds.x1=(double) bounds.xMin;
1784 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1785 metrics->bounds.y1=(double) bounds.yMin;
1786 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1787 metrics->bounds.x2=(double) bounds.xMax;
1788 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1789 metrics->bounds.y2=(double) bounds.yMax;
1790 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1791 (draw_info->stroke_pattern != (
Image *) NULL)) &&
1792 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1797 annotate_info->affine.tx=glyph.origin.x/64.0;
1798 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1799 if (IsEmptyOutline(outline) == MagickFalse)
1800 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1803 FT_Vector_Transform(&glyph.origin,&affine);
1804 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1805 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1806 (FT_Vector *) NULL,MagickTrue);
1809 bitmap=(FT_BitmapGlyph) glyph.image;
1810 point.x=offset->x+bitmap->left;
1811 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1812 point.x+=(origin.x/64.0);
1813 point.y=offset->y-bitmap->top;
1814 if (draw_info->render != MagickFalse)
1828 transparent_fill=((draw_info->fill.opacity == TransparentOpacity) &&
1829 (draw_info->fill_pattern == (
Image *) NULL) &&
1830 (draw_info->stroke.opacity == TransparentOpacity) &&
1831 (draw_info->stroke_pattern == (
Image *) NULL)) ? MagickTrue :
1833 p=bitmap->bitmap.buffer;
1834 image_view=AcquireAuthenticCacheView(image,exception);
1835 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1856 if (status == MagickFalse)
1858 x_offset=CastDoubleToLong(ceil(point.x-0.5));
1859 y_offset=CastDoubleToLong(ceil(point.y+y-0.5));
1860 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1863 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1867 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1868 bitmap->bitmap.width,1,exception);
1869 active=q != (
PixelPacket *) NULL ? MagickTrue : MagickFalse;
1871 n=y*bitmap->bitmap.pitch;
1872 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
1874 x_offset=CastDoubleToLong(ceil(point.x+x-0.5));
1875 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1882 if (bitmap->bitmap.buffer != (
unsigned char *) NULL)
1884 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
1885 fill_opacity=(MagickRealType) (p[n])/(
1886 bitmap->bitmap.num_grays-1);
1888 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1889 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1890 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1892 if (draw_info->text_antialias == MagickFalse)
1893 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1894 if (active == MagickFalse)
1895 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1899 if (transparent_fill == MagickFalse)
1901 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
1902 fill_opacity=QuantumRange-fill_opacity*(QuantumRange-
1903 fill_color.opacity);
1904 MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q);
1912 Da=1.0-(QuantumScale*(QuantumRange-q->opacity));
1914 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*QuantumRange;
1915 SetPixelAlpha(q,fill_opacity);
1917 if (active == MagickFalse)
1919 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1920 if (sync == MagickFalse)
1925 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1926 if (sync == MagickFalse)
1929 image_view=DestroyCacheView(image_view);
1930 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1931 (draw_info->stroke_pattern != (
Image *) NULL)) &&
1932 (status != MagickFalse))
1937 annotate_info->linejoin=RoundJoin;
1938 annotate_info->affine.tx=offset->x;
1939 annotate_info->affine.ty=offset->y;
1940 (void) ConcatenateString(&annotate_info->primitive,
"'");
1941 if (strlen(annotate_info->primitive) > 7)
1942 (
void) DrawImage(image,annotate_info);
1943 (void) CloneString(&annotate_info->primitive,
"path '");
1946 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1947 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
1948 (IsUTFSpace(code) == MagickFalse))
1949 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
1951 if (i == last_character)
1952 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
1954 origin.x+=(FT_Pos) grapheme[i].x_advance;
1955 origin.y+=(FT_Pos) grapheme[i].y_advance;
1956 metrics->origin.x=(double) origin.x;
1957 metrics->origin.y=(
double) origin.y;
1958 if (metrics->origin.x > metrics->width)
1959 metrics->width=metrics->origin.x;
1960 last_glyph_id=glyph.id;
1961 code=GetUTFCode(p+grapheme[i].cluster);
1964 grapheme=(
GraphemeInfo *) RelinquishMagickMemory(grapheme);
1965 if (utf8 != (
unsigned char *) NULL)
1966 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
1967 if (glyph.image != (FT_Glyph) NULL)
1969 FT_Done_Glyph(glyph.image);
1970 glyph.image=(FT_Glyph) NULL;
1975 metrics->bounds.x1/=64.0;
1976 metrics->bounds.y1/=64.0;
1977 metrics->bounds.x2/=64.0;
1978 metrics->bounds.y2/=64.0;
1979 metrics->origin.x/=64.0;
1980 metrics->origin.y/=64.0;
1981 metrics->width/=64.0;
1985 annotate_info=DestroyDrawInfo(annotate_info);
1986 (void) FT_Done_Face(face);
1987 (void) FT_Done_FreeType(library);
1988 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1992 static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
1993 const char *magick_unused(encoding),
const PointInfo *offset,
1996 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1997 MissingDelegateWarning,
"DelegateLibrarySupportNotBuiltIn",
"`%s' (Freetype)",
1998 draw_info->font != (
char *) NULL ? draw_info->font :
"none");
1999 return(RenderPostscript(image,draw_info,offset,metrics));
2034 static char *EscapeParenthesis(
const char *source)
2048 assert(source != (
const char *) NULL);
2050 for (p=source; *p !=
'\0'; p++)
2052 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2055 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2060 destination=(
char *) NULL;
2061 if (~length >= (MaxTextExtent-1))
2062 destination=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
2063 sizeof(*destination));
2064 if (destination == (
char *) NULL)
2065 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2068 for (p=source; *p !=
'\0'; p++)
2070 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2075 return(destination);
2078 static MagickBooleanType RenderPostscript(
Image *image,
2082 filename[MaxTextExtent],
2083 geometry[MaxTextExtent],
2118 if (draw_info->debug != MagickFalse)
2119 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2120 "Font %s; pointsize %g",draw_info->font != (
char *) NULL ?
2121 draw_info->font :
"none",draw_info->pointsize);
2123 unique_file=AcquireUniqueFileResource(filename);
2124 if (unique_file != -1)
2125 file=fdopen(unique_file,
"wb");
2126 if ((unique_file == -1) || (file == (FILE *) NULL))
2128 ThrowFileException(&image->exception,FileOpenError,
"UnableToOpenFile",
2130 return(MagickFalse);
2132 (void) FormatLocaleFile(file,
"%%!PS-Adobe-3.0\n");
2133 (void) FormatLocaleFile(file,
"/ReencodeType\n");
2134 (void) FormatLocaleFile(file,
"{\n");
2135 (void) FormatLocaleFile(file,
" findfont dup length\n");
2136 (void) FormatLocaleFile(file,
2137 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2138 (void) FormatLocaleFile(file,
2139 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2140 (void) FormatLocaleFile(file,
"} bind def\n");
2144 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2145 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2146 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2149 length=strlen(draw_info->text);
2150 for (i=0; i <= (ssize_t) (length+2); i++)
2152 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2153 draw_info->affine.ry*2.0*draw_info->pointsize);
2154 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2155 draw_info->affine.sy*2.0*draw_info->pointsize);
2156 if (point.x > extent.x)
2158 if (point.y > extent.y)
2161 (void) FormatLocaleFile(file,
"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2162 extent.x/2.0,extent.y/2.0);
2163 (void) FormatLocaleFile(file,
"%g %g scale\n",draw_info->pointsize,
2164 draw_info->pointsize);
2165 if ((draw_info->font == (
char *) NULL) || (*draw_info->font ==
'\0') ||
2166 (strchr(draw_info->font,
'/') != (
char *) NULL))
2167 (
void) FormatLocaleFile(file,
2168 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2170 (
void) FormatLocaleFile(file,
2171 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2173 (void) FormatLocaleFile(file,
"[%g %g %g %g 0 0] concat\n",
2174 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2175 draw_info->affine.sy);
2176 text=EscapeParenthesis(draw_info->text);
2177 if (identity == MagickFalse)
2178 (void) FormatLocaleFile(file,
"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2180 (void) FormatLocaleFile(file,
"(%s) show\n",text);
2181 text=DestroyString(text);
2182 (void) FormatLocaleFile(file,
"showpage\n");
2183 (void) fclose(file);
2184 (void) FormatLocaleString(geometry,MaxTextExtent,
"%.20gx%.20g+0+0!",
2185 floor(extent.x+0.5),floor(extent.y+0.5));
2186 annotate_info=AcquireImageInfo();
2187 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,
"ps:%s",
2189 (void) CloneString(&annotate_info->page,geometry);
2190 if (draw_info->density != (
char *) NULL)
2191 (
void) CloneString(&annotate_info->density,draw_info->density);
2192 annotate_info->antialias=draw_info->text_antialias;
2193 annotate_image=ReadImage(annotate_info,&image->exception);
2194 CatchException(&image->exception);
2195 annotate_info=DestroyImageInfo(annotate_info);
2196 (void) RelinquishUniqueFileResource(filename);
2197 if (annotate_image == (
Image *) NULL)
2198 return(MagickFalse);
2199 resolution.x=DefaultResolution;
2200 resolution.y=DefaultResolution;
2201 if (draw_info->density != (
char *) NULL)
2209 flags=ParseGeometry(draw_info->density,&geometry_info);
2210 if ((flags & RhoValue) != 0)
2211 resolution.x=geometry_info.rho;
2212 resolution.y=resolution.x;
2213 if ((flags & SigmaValue) != 0)
2214 resolution.y=geometry_info.sigma;
2216 if (identity == MagickFalse)
2217 (void) TransformImage(&annotate_image,
"0x0",(
char *) NULL);
2223 crop_info=GetImageBoundingBox(annotate_image,&annotate_image->exception);
2224 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2225 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2226 crop_info.y=CastDoubleToLong(ceil((resolution.y/DefaultResolution)*
2228 (void) FormatLocaleString(geometry,MaxTextExtent,
2229 "%.20gx%.20g%+.20g%+.20g",(
double) crop_info.width,(double)
2230 crop_info.height,(
double) crop_info.x,(double) crop_info.y);
2231 (void) TransformImage(&annotate_image,geometry,(
char *) NULL);
2233 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2234 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2235 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2236 metrics->ascent=metrics->pixels_per_em.y;
2237 metrics->descent=metrics->pixels_per_em.y/-5.0;
2238 metrics->width=(double) annotate_image->columns/
2239 ExpandAffine(&draw_info->affine);
2240 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2241 metrics->max_advance=metrics->pixels_per_em.x;
2242 metrics->bounds.x1=0.0;
2243 metrics->bounds.y1=metrics->descent;
2244 metrics->bounds.x2=metrics->ascent+metrics->descent;
2245 metrics->bounds.y2=metrics->ascent+metrics->descent;
2246 metrics->underline_position=(-2.0);
2247 metrics->underline_thickness=1.0;
2248 if (draw_info->render == MagickFalse)
2250 annotate_image=DestroyImage(annotate_image);
2253 if (draw_info->fill.opacity != TransparentOpacity)
2270 if (image->matte == MagickFalse)
2271 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
2272 if (annotate_image->matte == MagickFalse)
2273 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel);
2274 fill_color=draw_info->fill;
2275 exception=(&image->exception);
2276 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2277 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2285 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2289 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2291 (void) GetFillColor(draw_info,x,y,&fill_color);
2292 SetPixelAlpha(q,ClampToQuantum((((QuantumRange-GetPixelIntensity(
2293 annotate_image,q))*(QuantumRange-fill_color.opacity))/
2295 SetPixelRed(q,fill_color.red);
2296 SetPixelGreen(q,fill_color.green);
2297 SetPixelBlue(q,fill_color.blue);
2300 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2301 if (sync == MagickFalse)
2304 annotate_view=DestroyCacheView(annotate_view);
2305 (void) CompositeImage(image,OverCompositeOp,annotate_image,
2306 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2307 metrics->descent)-0.5));
2309 annotate_image=DestroyImage(annotate_image);
2343 static MagickBooleanType RenderX11(
Image *image,
const DrawInfo *draw_info,
2350 ActivateSemaphoreInfo(&annotate_semaphore);
2351 LockSemaphoreInfo(annotate_semaphore);
2352 status=XRenderImage(image,draw_info,offset,metrics);
2353 UnlockSemaphoreInfo(annotate_semaphore);