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)
100# include <freetype/ftoutln.h>
102#if defined(FT_BBOX_H)
105# include <freetype/ftbbox.h>
108#if defined(MAGICKCORE_RAQM_DELEGATE)
133static MagickBooleanType
134 RenderType(Image *,
const DrawInfo *,
const PointInfo *,TypeMetric *),
135 RenderPostscript(Image *,
const DrawInfo *,
const PointInfo *,TypeMetric *),
136 RenderFreetype(Image *,
const DrawInfo *,
const char *,
const PointInfo *,
138 RenderX11(Image *,
const DrawInfo *,
const PointInfo *,TypeMetric *);
158MagickExport MagickBooleanType AnnotateComponentGenesis(
void)
161 annotate_semaphore=AllocateSemaphoreInfo();
183MagickExport
void AnnotateComponentTerminus(
void)
186 ActivateSemaphoreInfo(&annotate_semaphore);
187 DestroySemaphoreInfo(&annotate_semaphore);
214MagickExport MagickBooleanType AnnotateImage(Image *image,
215 const DrawInfo *draw_info)
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+
349 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
350 annotate_info->affine.sy*metrics.ascent;
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+
360 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
361 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
365 case (GravityType) NorthEastGravity:
367 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
368 geometry.width+i*annotate_info->affine.ry*height-
369 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
370 (metrics.ascent+metrics.descent)-1.0;
371 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
372 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
373 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
377 case (GravityType) WestGravity:
379 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
380 annotate_info->affine.ry*height+annotate_info->affine.ry*
381 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
382 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
383 geometry.height/2.0+i*annotate_info->affine.sy*height+
384 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
385 (number_lines-1.0)*height)/2.0;
388 case (GravityType) StaticGravity:
389 case (GravityType) CenterGravity:
391 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
392 geometry.width/2.0+i*annotate_info->affine.ry*height-
393 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
394 (metrics.ascent+metrics.descent-(number_lines-1)*height)/2.0;
395 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
396 geometry.height/2.0+i*annotate_info->affine.sy*height-
397 annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
398 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
401 case (GravityType) EastGravity:
403 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
404 geometry.width+i*annotate_info->affine.ry*height-
405 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
406 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0-1.0;
407 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
408 geometry.height/2.0+i*annotate_info->affine.sy*height-
409 annotate_info->affine.rx*metrics.width+annotate_info->affine.sy*
410 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
413 case (GravityType) SouthWestGravity:
415 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
416 annotate_info->affine.ry*height-annotate_info->affine.ry*
417 (number_lines-1.0)*height;
418 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
419 geometry.height+i*annotate_info->affine.sy*height-
420 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
423 case (GravityType) SouthGravity:
425 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
426 geometry.width/2.0+i*annotate_info->affine.ry*height-
427 annotate_info->affine.sx*metrics.width/2.0-annotate_info->affine.ry*
428 (number_lines-1.0)*height/2.0;
429 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
430 geometry.height+i*annotate_info->affine.sy*height-
431 annotate_info->affine.rx*metrics.width/2.0-annotate_info->affine.sy*
432 (number_lines-1.0)*height+metrics.descent;
435 case (GravityType) SouthEastGravity:
437 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
438 geometry.width+i*annotate_info->affine.ry*height-
439 annotate_info->affine.sx*metrics.width-annotate_info->affine.ry*
440 (number_lines-1.0)*height-1.0;
441 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
442 geometry.height+i*annotate_info->affine.sy*height-
443 annotate_info->affine.rx*metrics.width-annotate_info->affine.sy*
444 (number_lines-1.0)*height+metrics.descent;
448 switch (annotate->align)
452 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
453 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
458 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
459 annotate_info->affine.sx*metrics.width/2.0;
460 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
461 annotate_info->affine.rx*metrics.width/2.0;
466 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
467 annotate_info->affine.sx*metrics.width;
468 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
469 annotate_info->affine.rx*metrics.width;
475 if (draw_info->undercolor.opacity != TransparentOpacity)
483 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
484 undercolor_info->fill=draw_info->undercolor;
485 undercolor_info->affine=draw_info->affine;
486 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
487 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
488 (void) FormatLocaleString(primitive,MaxTextExtent,
489 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(
double) height);
490 (void) CloneString(&undercolor_info->primitive,primitive);
491 (void) DrawImage(image,undercolor_info);
492 (void) DestroyDrawInfo(undercolor_info);
494 annotate_info->affine.tx=offset.x;
495 annotate_info->affine.ty=offset.y;
496 pixel=annotate_info->fill;
497 if (annotate_info->stroke.opacity != TransparentOpacity)
498 pixel=annotate_info->stroke;
499 (void) QueryColorname(image,&pixel,SVGCompliance,color,&image->exception);
500 (void) FormatLocaleString(primitive,MagickPathExtent,
"stroke %s "
501 "stroke-width %g line 0,0 %g,0",color,(
double)
502 metrics.underline_thickness,(
double) metrics.width);
506 switch (annotate->decorate)
508 case OverlineDecoration:
510 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
511 metrics.descent-metrics.underline_position));
512 (void) CloneString(&annotate_info->primitive,primitive);
513 (void) DrawImage(image,annotate_info);
516 case UnderlineDecoration:
518 annotate_info->affine.ty-=(draw_info->affine.sy*
519 metrics.underline_position);
520 (void) CloneString(&annotate_info->primitive,primitive);
521 (void) DrawImage(image,annotate_info);
527 status=RenderType(image,annotate,&offset,&metrics);
528 if (status == MagickFalse)
530 if (annotate->decorate == LineThroughDecoration)
532 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
533 metrics.underline_position+metrics.descent*2)/2.0);
534 (void) CloneString(&annotate_info->primitive,primitive);
535 (void) DrawImage(image,annotate_info);
541 annotate_info=DestroyDrawInfo(annotate_info);
542 annotate=DestroyDrawInfo(annotate);
543 textlist=(
char **) RelinquishMagickMemory(textlist);
544 text=DestroyString(text);
581static inline char *ReplaceSpaceWithNewline(
char **caption,
char *space)
586 if ((caption == (
char **) NULL) || (space == (
char *) NULL))
587 return((
char *) NULL);
588 octets=(size_t) GetUTFOctets(space);
602 length=strlen(*caption);
604 offset=space-(*caption);
607 target=AcquireString(*caption);
608 CopyMagickString(target,*caption,(
size_t) offset+2);
609 ConcatenateMagickString(target,space+octets,length);
610 (void) DestroyString(*caption);
612 space=(*caption)+offset;
618MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
619 const MagickBooleanType split,TypeMetric *metrics,
char **caption)
636 if ((caption == (
char **) NULL) || (*caption == (
char *) NULL))
641 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
652 if ((IsUTFSpace(code) != MagickFalse) &&
653 (IsNonBreakingUTFSpace(code) == MagickFalse))
656 if (width > image->columns)
658 p=ReplaceSpaceWithNewline(caption,s);
662 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
665 status=GetTypeMetrics(image,draw_info,metrics);
666 if (status == MagickFalse)
668 width=CastDoubleToUnsigned(metrics->width+draw_info->stroke_width+0.5);
669 if (width <= image->columns)
671 if (s != (
char *) NULL)
672 p=ReplaceSpaceWithNewline(caption,s);
674 if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
680 if ((n > 0) && ((*caption)[n-1] !=
'\n'))
685 target=AcquireString(*caption);
686 CopyMagickString(target,*caption,(
size_t) n+1);
687 ConcatenateMagickString(target,
"\n",strlen(*caption)+1);
688 ConcatenateMagickString(target,p,strlen(*caption)+2);
689 (void) DestroyString(*caption);
698 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
699 if (GetUTFCode(p) ==
'\n')
751MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
752 const DrawInfo *draw_info,TypeMetric *metrics)
778 assert(image != (Image *) NULL);
779 assert(image->signature == MagickCoreSignature);
780 if (IsEventLogging() != MagickFalse)
781 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
782 assert(draw_info != (DrawInfo *) NULL);
783 assert(draw_info->text != (
char *) NULL);
784 assert(draw_info->signature == MagickCoreSignature);
785 if (*draw_info->text ==
'\0')
787 (void) ThrowMagickException(&image->exception,GetMagickModule(),
788 OptionError,
"LabelExpected",
"`%s'",image->filename);
791 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
792 annotate_info->text=DestroyString(annotate_info->text);
796 textlist=StringToStrings(draw_info->text,&count);
797 if (textlist == (
char **) NULL)
799 annotate_info=DestroyDrawInfo(annotate_info);
802 annotate_info->render=MagickFalse;
803 annotate_info->direction=UndefinedDirection;
804 (void) memset(metrics,0,
sizeof(*metrics));
805 (void) memset(&extent,0,
sizeof(extent));
809 annotate_info->text=textlist[0];
810 status=GetTypeMetrics(image,annotate_info,&extent);
812 height=(count*(size_t) (metrics->ascent-metrics->descent+
813 0.5)+(count-1)*draw_info->interline_spacing);
814 size=(MagickSizeType) fabs(height);
815 if (AcquireMagickResource(HeightResource,size) == MagickFalse)
817 (void) ThrowMagickException(&image->exception,GetMagickModule(),
818 ImageError,
"WidthOrHeightExceedsLimit",
"`%s'",image->filename);
823 for (i=1; i < (ssize_t) count; i++)
825 annotate_info->text=textlist[i];
826 status=GetTypeMetrics(image,annotate_info,&extent);
827 if (status == MagickFalse)
829 if (extent.width > metrics->width)
831 size=(MagickSizeType) fabs(extent.width);
832 if (AcquireMagickResource(WidthResource,size) == MagickFalse)
834 (void) ThrowMagickException(&image->exception,GetMagickModule(),
835 ImageError,
"WidthOrHeightExceedsLimit",
"`%s'",image->filename);
840 metrics->height=(double) height;
845 annotate_info->text=(
char *) NULL;
846 annotate_info=DestroyDrawInfo(annotate_info);
847 for (i=0; i < (ssize_t) count; i++)
848 textlist[i]=DestroyString(textlist[i]);
849 textlist=(
char **) RelinquishMagickMemory(textlist);
897MagickExport MagickBooleanType GetTypeMetrics(Image *image,
898 const DrawInfo *draw_info,TypeMetric *metrics)
909 assert(image != (Image *) NULL);
910 assert(image->signature == MagickCoreSignature);
911 if (IsEventLogging() != MagickFalse)
912 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
913 assert(draw_info != (DrawInfo *) NULL);
914 assert(draw_info->text != (
char *) NULL);
915 assert(draw_info->signature == MagickCoreSignature);
916 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
917 annotate_info->render=MagickFalse;
918 annotate_info->direction=UndefinedDirection;
919 (void) memset(metrics,0,
sizeof(*metrics));
922 status=RenderType(image,annotate_info,&offset,metrics);
923 if (draw_info->debug != MagickFalse)
924 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Metrics: text: %s; "
925 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
926 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
927 "underline position: %g; underline thickness: %g",annotate_info->text,
928 metrics->width,metrics->height,metrics->ascent,metrics->descent,
929 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
930 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
931 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
932 metrics->underline_position,metrics->underline_thickness);
933 annotate_info=DestroyDrawInfo(annotate_info);
967static MagickBooleanType RenderType(Image *image,
const DrawInfo *draw_info,
968 const PointInfo *offset,TypeMetric *metrics)
985 type_info=(
const TypeInfo *) NULL;
986 if (draw_info->font != (
char *) NULL)
988 if (*draw_info->font ==
'@')
990 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
994 if (*draw_info->font ==
'-')
995 return(RenderX11(image,draw_info,offset,metrics));
996 if (*draw_info->font ==
'^')
997 return(RenderPostscript(image,draw_info,offset,metrics));
998 if (IsPathAccessible(draw_info->font) != MagickFalse)
1000 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
1004 type_info=GetTypeInfo(draw_info->font,&image->exception);
1005 if (type_info == (
const TypeInfo *) NULL)
1006 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1007 TypeWarning,
"UnableToReadFont",
"`%s'",draw_info->font);
1009 if ((type_info == (
const TypeInfo *) NULL) &&
1010 (draw_info->family != (
const char *) NULL))
1012 if (strpbrk(draw_info->family,
",'\"") == (
char *) NULL)
1013 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
1014 draw_info->stretch,draw_info->weight,&image->exception);
1015 if (type_info == (
const TypeInfo *) NULL)
1029 family=StringToArgv(draw_info->family,&number_families);
1030 for (i=1; i < (ssize_t) number_families; i++)
1032 (void) SubstituteString(&family[i],
",",
"");
1033 type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1034 draw_info->stretch,draw_info->weight,&image->exception);
1035 if ((type_info != (
const TypeInfo *) NULL) &&
1036 (LocaleCompare(family[i],type_info->family) == 0))
1039 for (i=0; i < (ssize_t) number_families; i++)
1040 family[i]=DestroyString(family[i]);
1041 family=(
char **) RelinquishMagickMemory(family);
1042 if (type_info == (
const TypeInfo *) NULL)
1043 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1044 TypeWarning,
"UnableToReadFont",
"`%s'",draw_info->family);
1047 font=GetPolicyValue(
"system:font");
1048 if (font != (
char *) NULL)
1050 if (IsPathAccessible(font) != MagickFalse)
1055 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1056 annotate_info->font=font;
1057 status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1059 annotate_info=DestroyDrawInfo(annotate_info);
1062 font=DestroyString(font);
1064 sans_exception=AcquireExceptionInfo();
1065 if (type_info == (
const TypeInfo *) NULL)
1066 type_info=GetTypeInfoByFamily((
const char *) NULL,draw_info->style,
1067 draw_info->stretch,draw_info->weight,sans_exception);
1068 if (type_info == (
const TypeInfo *) NULL)
1069 type_info=GetTypeInfo(
"*",sans_exception);
1070 sans_exception=DestroyExceptionInfo(sans_exception);
1071 if (type_info == (
const TypeInfo *) NULL)
1073 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics);
1076 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1077 annotate_info->face=type_info->face;
1078 if (type_info->metrics != (
char *) NULL)
1079 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1080 if (type_info->glyphs != (
char *) NULL)
1081 (void) CloneString(&annotate_info->font,type_info->glyphs);
1082 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics);
1083 annotate_info=DestroyDrawInfo(annotate_info);
1120#if defined(MAGICKCORE_FREETYPE_DELEGATE)
1122#if defined(MAGICKCORE_RAQM_DELEGATE)
1123static size_t ComplexRaqmTextLayout(
const Image *image,
1124 const DrawInfo *draw_info,
const char *text,
const size_t length,
1125 const FT_Face face,GraphemeInfo **grapheme,ExceptionInfo *exception)
1145 if (rq == (raqm_t *) NULL)
1147 if (raqm_set_text_utf8(rq,text,length) == 0)
1149 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1151 if (raqm_set_freetype_face(rq,face) == 0)
1153 features=GetImageProperty(image,
"type:features");
1154 if (features != (
const char *) NULL)
1169 token_info=AcquireTokenInfo();
1170 token=AcquireString(
"");
1171 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1172 &breaker,&next,"e);
1173 while (status_token == 0)
1175 raqm_add_font_feature(rq,token,(
int) strlen(token));
1176 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1177 &breaker,&next,"e);
1179 token_info=DestroyTokenInfo(token_info);
1180 token=DestroyString(token);
1182 if (raqm_layout(rq) == 0)
1184 glyphs=raqm_get_glyphs(rq,&extent);
1185 if (glyphs == (raqm_glyph_t *) NULL)
1190 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(extent,
sizeof(**grapheme));
1191 if (*grapheme == (GraphemeInfo *) NULL)
1196 for (i=0; i < (ssize_t) extent; i++)
1198 (*grapheme)[i].index=glyphs[i].index;
1199 (*grapheme)[i].x_offset=(size_t) glyphs[i].x_offset;
1200 (*grapheme)[i].x_advance=(size_t) glyphs[i].x_advance;
1201 (*grapheme)[i].y_offset=(size_t) glyphs[i].y_offset;
1202 (*grapheme)[i].y_advance=(size_t) glyphs[i].y_advance;
1203 (*grapheme)[i].cluster=glyphs[i].cluster;
1210static size_t ComplexTextLayout(
const DrawInfo *draw_info,
const char *text,
1211 const size_t length,
const FT_Face face,
const FT_Int32 flags,
1212 GraphemeInfo **grapheme)
1226 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(length+1,
sizeof(**grapheme));
1227 if (*grapheme == (GraphemeInfo *) NULL)
1231 for (i=0; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p), i++)
1233 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,GetUTFCode(p));
1234 (*grapheme)[i].x_offset=0;
1235 (*grapheme)[i].y_offset=0;
1236 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1238 if (FT_HAS_KERNING(face))
1246 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1247 (*grapheme)[i].index,ft_kerning_default,&kerning);
1249 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1250 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1253 (void) FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1254 (*grapheme)[i].x_advance=face->glyph->advance.x;
1255 (*grapheme)[i].y_advance=face->glyph->advance.y;
1256 (*grapheme)[i].cluster=p-text;
1257 last_glyph=(*grapheme)[i].index;
1263static void FTCloseStream(FT_Stream stream)
1265 FILE *file = (FILE *) stream->descriptor.pointer;
1266 if (file != (FILE *) NULL)
1267 (void) fclose(file);
1268 stream->descriptor.pointer=NULL;
1271static unsigned long FTReadStream(FT_Stream stream,
unsigned long offset,
1272 unsigned char *buffer,
unsigned long count)
1274 FILE *file = (FILE *) stream->descriptor.pointer;
1275 if (file == (FILE *) NULL)
1279 if (offset > stream->size)
1282 return((
unsigned long) fseek(file,(off_t) offset,SEEK_SET));
1284 return((
unsigned long) fread(buffer,1,count,file));
1287static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1289 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1293static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1294 DrawInfo *draw_info)
1300 path[MaxTextExtent];
1302 affine=draw_info->affine;
1303 (void) FormatLocaleString(path,MaxTextExtent,
"C%g,%g %g,%g %g,%g",affine.tx+
1304 p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,
1305 affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1306 (void) ConcatenateString(&draw_info->primitive,path);
1310static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1316 path[MaxTextExtent];
1318 affine=draw_info->affine;
1319 (void) FormatLocaleString(path,MaxTextExtent,
"L%g,%g",affine.tx+to->x/64.0,
1320 affine.ty-to->y/64.0);
1321 (void) ConcatenateString(&draw_info->primitive,path);
1325static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1331 path[MaxTextExtent];
1333 affine=draw_info->affine;
1334 (void) FormatLocaleString(path,MaxTextExtent,
"M%g,%g",affine.tx+to->x/64.0,
1335 affine.ty-to->y/64.0);
1336 (void) ConcatenateString(&draw_info->primitive,path);
1340static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1341 DrawInfo *draw_info)
1347 path[MaxTextExtent];
1349 affine=draw_info->affine;
1350 (void) FormatLocaleString(path,MaxTextExtent,
"Q%g,%g %g,%g",affine.tx+
1351 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1353 (void) ConcatenateString(&draw_info->primitive,path);
1357static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1359#if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1360 return(FT_Error_String(ft_status));
1362 magick_unreferenced(ft_status);
1363 return((
const char *) NULL);
1367static MagickBooleanType RenderFreetype(Image *image,
const DrawInfo *draw_info,
1368 const char *encoding,
const PointInfo *offset,TypeMetric *metrics)
1370#if !defined(FT_OPEN_PATHNAME)
1371#define FT_OPEN_PATHNAME ft_open_pathname
1374#define ThrowFreetypeErrorException(tag,ft_status,value) \
1376 const char *error_string = FreetypeErrorMessage(ft_status); \
1377 if (error_string != (const char *) NULL) \
1378 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1379 tag,"`%s (%s)'",value, error_string); \
1381 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1382 tag,"`%s'",value); \
1385 typedef struct _GlyphInfo
1470 static FT_Outline_Funcs
1473 (FT_Outline_MoveTo_Func) TraceMoveTo,
1474 (FT_Outline_LineTo_Func) TraceLineTo,
1475 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1476 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1489 exception=(&image->exception);
1490 ft_status=FT_Init_FreeType(&library);
1493 ThrowFreetypeErrorException(
"UnableToInitializeFreetypeLibrary",
1494 ft_status,image->filename);
1495 return(MagickFalse);
1497 face_index=(FT_Long) draw_info->face;
1501 (void) memset(&args,0,
sizeof(args));
1502 if (draw_info->font == (
char *) NULL)
1504 const TypeInfo *type_info = GetTypeInfo(
"*",exception);
1505 if (type_info != (
const TypeInfo *) NULL)
1506 args.pathname=ConstantString(type_info->glyphs);
1509 if (*draw_info->font !=
'@')
1510 args.pathname=ConstantString(draw_info->font);
1516 ImageInfo *image_info=AcquireImageInfo();
1517 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1519 (void) SetImageInfo(image_info,0,exception);
1520 face_index=(FT_Long) image_info->scene;
1521 args.pathname=ConstantString(image_info->filename);
1522 image_info=DestroyImageInfo(image_info);
1527 stream=(FT_StreamRec *) AcquireCriticalMemory(
sizeof(*stream));
1528 (void) memset(stream,0,
sizeof(*stream));
1529 if (stat(args.pathname,&attributes) == 0)
1530 stream->size=attributes.st_size >= 0 ? (
unsigned long)
1531 attributes.st_size : 0;
1532 stream->descriptor.pointer=fopen_utf8(args.pathname,
"rb");
1533 stream->read=(&FTReadStream);
1534 stream->close=(&FTCloseStream);
1535 args.flags=FT_OPEN_STREAM;
1537 face=(FT_Face) NULL;
1538 ft_status=FT_Open_Face(library,&args,face_index,&face);
1541 (void) FT_Done_FreeType(library);
1542 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1543 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,args.pathname);
1544 args.pathname=DestroyString(args.pathname);
1545 return(MagickFalse);
1547 args.pathname=DestroyString(args.pathname);
1548 if ((draw_info->metrics != (
char *) NULL) &&
1549 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1550 (void) FT_Attach_File(face,draw_info->metrics);
1551 encoding_type=FT_ENCODING_UNICODE;
1552 ft_status=FT_Select_Charmap(face,encoding_type);
1553 if ((ft_status != 0) && (face->num_charmaps != 0))
1554 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1555 if (encoding != (
const char *) NULL)
1557 if (LocaleCompare(encoding,
"AdobeCustom") == 0)
1558 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1559 if (LocaleCompare(encoding,
"AdobeExpert") == 0)
1560 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1561 if (LocaleCompare(encoding,
"AdobeStandard") == 0)
1562 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1563 if (LocaleCompare(encoding,
"AppleRoman") == 0)
1564 encoding_type=FT_ENCODING_APPLE_ROMAN;
1565 if (LocaleCompare(encoding,
"BIG5") == 0)
1566 encoding_type=FT_ENCODING_BIG5;
1567#if defined(FT_ENCODING_PRC)
1568 if (LocaleCompare(encoding,
"GB2312") == 0)
1569 encoding_type=FT_ENCODING_PRC;
1571#if defined(FT_ENCODING_JOHAB)
1572 if (LocaleCompare(encoding,
"Johab") == 0)
1573 encoding_type=FT_ENCODING_JOHAB;
1575#if defined(FT_ENCODING_ADOBE_LATIN_1)
1576 if (LocaleCompare(encoding,
"Latin-1") == 0)
1577 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1579#if defined(FT_ENCODING_ADOBE_LATIN_2)
1580 if (LocaleCompare(encoding,
"Latin-2") == 0)
1581 encoding_type=FT_ENCODING_OLD_LATIN_2;
1583 if (LocaleCompare(encoding,
"None") == 0)
1584 encoding_type=FT_ENCODING_NONE;
1585 if (LocaleCompare(encoding,
"SJIScode") == 0)
1586 encoding_type=FT_ENCODING_SJIS;
1587 if (LocaleCompare(encoding,
"Symbol") == 0)
1588 encoding_type=FT_ENCODING_MS_SYMBOL;
1589 if (LocaleCompare(encoding,
"Unicode") == 0)
1590 encoding_type=FT_ENCODING_UNICODE;
1591 if (LocaleCompare(encoding,
"Wansung") == 0)
1592 encoding_type=FT_ENCODING_WANSUNG;
1593 ft_status=FT_Select_Charmap(face,encoding_type);
1596 (void) FT_Done_Face(face);
1597 (void) FT_Done_FreeType(library);
1598 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1599 ThrowFreetypeErrorException(
"UnrecognizedFontEncoding",ft_status,
1601 return(MagickFalse);
1607 resolution.x=DefaultResolution;
1608 resolution.y=DefaultResolution;
1609 if (draw_info->density != (
char *) NULL)
1617 flags=ParseGeometry(draw_info->density,&geometry_info);
1618 if ((flags & RhoValue) != 0)
1619 resolution.x=geometry_info.rho;
1620 resolution.y=resolution.x;
1621 if ((flags & SigmaValue) != 0)
1622 resolution.y=geometry_info.sigma;
1624 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1625 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1626 (FT_UInt) resolution.y);
1629 (void) FT_Done_Face(face);
1630 (void) FT_Done_FreeType(library);
1631 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1632 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,
1634 return(MagickFalse);
1636 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1637 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1638 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1639 metrics->descent=(double) face->size->metrics.descender/64.0;
1640 if (face->size->metrics.ascender == 0)
1645 metrics->ascent=face->size->metrics.y_ppem;
1646 if (face->size->metrics.descender == 0)
1647 metrics->descent=face->size->metrics.y_ppem/-3.5;
1650 metrics->origin.x=0;
1651 metrics->origin.y=0;
1652 metrics->height=(double) face->size->metrics.height/64.0;
1653 metrics->max_advance=0.0;
1654 if (face->size->metrics.max_advance > MagickEpsilon)
1655 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1656 metrics->bounds.x1=0.0;
1657 metrics->bounds.y1=metrics->descent;
1658 metrics->bounds.x2=metrics->ascent+metrics->descent;
1659 metrics->bounds.y2=metrics->ascent+metrics->descent;
1660 metrics->underline_position=face->underline_position*
1661 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1662 metrics->underline_thickness=face->underline_thickness*
1663 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1665 FT_Get_First_Char(face,&first_glyph_id);
1666 if ((draw_info->text == (
char *) NULL) || (*draw_info->text ==
'\0') ||
1667 (first_glyph_id == 0))
1669 (void) FT_Done_Face(face);
1670 (void) FT_Done_FreeType(library);
1671 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1677 if (draw_info->debug != MagickFalse)
1678 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Font %s; "
1679 "font-encoding %s; text-encoding %s; pointsize %g",
1680 draw_info->font != (
char *) NULL ? draw_info->font :
"none",
1681 encoding != (
char *) NULL ? encoding :
"none",
1682 draw_info->encoding != (
char *) NULL ? draw_info->encoding :
"none",
1683 draw_info->pointsize);
1684 flags=FT_LOAD_DEFAULT;
1685 if (draw_info->render == MagickFalse)
1686 flags=FT_LOAD_NO_BITMAP;
1687 if (draw_info->text_antialias == MagickFalse)
1688 flags|=FT_LOAD_TARGET_MONO;
1691#if defined(FT_LOAD_TARGET_LIGHT)
1692 flags|=FT_LOAD_TARGET_LIGHT;
1693#elif defined(FT_LOAD_TARGET_LCD)
1694 flags|=FT_LOAD_TARGET_LCD;
1697 value=GetImageProperty(image,
"type:hinting");
1698 if ((value != (
const char *) NULL) && (LocaleCompare(value,
"off") == 0))
1699 flags|=FT_LOAD_NO_HINTING;
1701 glyph.image=(FT_Glyph) NULL;
1709 if (draw_info->render != MagickFalse)
1711 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1712 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1713 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1714 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1716 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1717 if (annotate_info->dash_pattern != (
double *) NULL)
1718 annotate_info->dash_pattern[0]=0.0;
1719 (void) CloneString(&annotate_info->primitive,
"path '");
1721 if (draw_info->render != MagickFalse)
1723 if (image->storage_class != DirectClass)
1724 (void) SetImageStorageClass(image,DirectClass);
1725 if (image->matte == MagickFalse)
1726 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1728 for (p=draw_info->text; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
1729 if (GetUTFCode(p) < 0)
1731 utf8=(
unsigned char *) NULL;
1732 if (GetUTFCode(p) == 0)
1736 utf8=ConvertLatin1ToUTF8((
unsigned char *) draw_info->text);
1737 if (utf8 != (
unsigned char *) NULL)
1740 grapheme=(GraphemeInfo *) NULL;
1741#if defined(MAGICKCORE_RAQM_DELEGATE)
1742 length=ComplexRaqmTextLayout(image,draw_info,p,strlen(p),face,&grapheme,
1745 length=ComplexTextLayout(draw_info,p,strlen(p),face,flags,&grapheme);
1747 missing_glyph_id=FT_Get_Char_Index(face,
' ');
1749 last_character=(ssize_t) length-1;
1750 for (i=0; i < (ssize_t) length; i++)
1764 glyph.id=grapheme[i].index;
1766 glyph.id=missing_glyph_id;
1767 if ((glyph.id != 0) && (last_glyph_id != 0))
1768 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1769 glyph.origin=origin;
1770 glyph.origin.x+=grapheme[i].x_offset;
1771 glyph.origin.y+=grapheme[i].y_offset;
1772 if (glyph.image != (FT_Glyph) NULL)
1774 FT_Done_Glyph(glyph.image);
1775 glyph.image=(FT_Glyph) NULL;
1777 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1780 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1783 outline=((FT_OutlineGlyph) glyph.image)->outline;
1784 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1785 (IsEmptyOutline(outline) == MagickFalse))
1787 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1790 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1791 metrics->bounds.x1=(double) bounds.xMin;
1792 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1793 metrics->bounds.y1=(double) bounds.yMin;
1794 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1795 metrics->bounds.x2=(double) bounds.xMax;
1796 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1797 metrics->bounds.y2=(double) bounds.yMax;
1798 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1799 (draw_info->stroke_pattern != (Image *) NULL)) &&
1800 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1805 annotate_info->affine.tx=glyph.origin.x/64.0;
1806 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1807 if (IsEmptyOutline(outline) == MagickFalse)
1808 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1811 FT_Vector_Transform(&glyph.origin,&affine);
1812 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1813 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1814 (FT_Vector *) NULL,MagickTrue);
1817 bitmap=(FT_BitmapGlyph) glyph.image;
1818 point.x=offset->x+bitmap->left;
1819 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1820 point.x+=(origin.x/64.0);
1821 point.y=offset->y-bitmap->top;
1822 if (draw_info->render != MagickFalse)
1836 transparent_fill=((draw_info->fill.opacity == TransparentOpacity) &&
1837 (draw_info->fill_pattern == (Image *) NULL) &&
1838 (draw_info->stroke.opacity == TransparentOpacity) &&
1839 (draw_info->stroke_pattern == (Image *) NULL)) ? MagickTrue :
1841 p=bitmap->bitmap.buffer;
1842 image_view=AcquireAuthenticCacheView(image,exception);
1843#if defined(MAGICKCORE_OPENMP_SUPPORT)
1844 #pragma omp parallel for schedule(static) shared(status) \
1845 magick_number_threads(image,image,bitmap->bitmap.rows,4)
1847 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1868 if (status == MagickFalse)
1870 x_offset=CastDoubleToLong(ceil(point.x-0.5));
1871 y_offset=CastDoubleToLong(ceil(point.y+y-0.5));
1872 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1874 q=(PixelPacket *) NULL;
1875 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1879 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1880 bitmap->bitmap.width,1,exception);
1881 active=q != (PixelPacket *) NULL ? MagickTrue : MagickFalse;
1883 n=y*bitmap->bitmap.pitch;
1884 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
1886 x_offset=CastDoubleToLong(ceil(point.x+x-0.5));
1887 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1889 if (q != (PixelPacket *) NULL)
1894 if (bitmap->bitmap.buffer != (
unsigned char *) NULL)
1896 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
1897 fill_opacity=(MagickRealType) (p[n])/(
1898 bitmap->bitmap.num_grays-1);
1900 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1901 fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1902 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1904 if (draw_info->text_antialias == MagickFalse)
1905 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1906 if (active == MagickFalse)
1907 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1909 if (q == (PixelPacket *) NULL)
1911 if (transparent_fill == MagickFalse)
1913 (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
1914 fill_opacity=(double) QuantumRange-(
double) fill_opacity*
1915 ((double) QuantumRange-(double) fill_color.opacity);
1916 MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q);
1924 Da=1.0-(QuantumScale*((double) QuantumRange-(double)
1927 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*(
double)
1929 SetPixelAlpha(q,fill_opacity);
1931 if (active == MagickFalse)
1933 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1934 if (sync == MagickFalse)
1939 sync=SyncCacheViewAuthenticPixels(image_view,exception);
1940 if (sync == MagickFalse)
1943 image_view=DestroyCacheView(image_view);
1944 if (((draw_info->stroke.opacity != TransparentOpacity) ||
1945 (draw_info->stroke_pattern != (Image *) NULL)) &&
1946 (status != MagickFalse))
1951 annotate_info->linejoin=RoundJoin;
1952 annotate_info->affine.tx=offset->x;
1953 annotate_info->affine.ty=offset->y;
1954 (void) ConcatenateString(&annotate_info->primitive,
"'");
1955 if (strlen(annotate_info->primitive) > 7)
1956 (void) DrawImage(image,annotate_info);
1957 (void) CloneString(&annotate_info->primitive,
"path '");
1960 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1961 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
1962 (IsUTFSpace(code) == MagickFalse))
1963 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
1965 if (i == last_character)
1966 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
1968 origin.x+=(FT_Pos) grapheme[i].x_advance;
1969 origin.y+=(FT_Pos) grapheme[i].y_advance;
1970 metrics->origin.x=(double) origin.x;
1971 metrics->origin.y=(double) origin.y;
1972 if (metrics->origin.x > metrics->width)
1973 metrics->width=metrics->origin.x;
1974 last_glyph_id=glyph.id;
1975 code=GetUTFCode(p+grapheme[i].cluster);
1977 if (grapheme != (GraphemeInfo *) NULL)
1978 grapheme=(GraphemeInfo *) RelinquishMagickMemory(grapheme);
1979 if (utf8 != (
unsigned char *) NULL)
1980 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
1981 if (glyph.image != (FT_Glyph) NULL)
1983 FT_Done_Glyph(glyph.image);
1984 glyph.image=(FT_Glyph) NULL;
1989 metrics->bounds.x1/=64.0;
1990 metrics->bounds.y1/=64.0;
1991 metrics->bounds.x2/=64.0;
1992 metrics->bounds.y2/=64.0;
1993 metrics->origin.x/=64.0;
1994 metrics->origin.y/=64.0;
1995 metrics->width/=64.0;
1999 annotate_info=DestroyDrawInfo(annotate_info);
2000 (void) FT_Done_Face(face);
2001 (void) FT_Done_FreeType(library);
2002 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
2006static MagickBooleanType RenderFreetype(Image *image,
const DrawInfo *draw_info,
2007 const char *magick_unused(encoding),
const PointInfo *offset,
2008 TypeMetric *metrics)
2010 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2011 MissingDelegateWarning,
"DelegateLibrarySupportNotBuiltIn",
"`%s' (Freetype)",
2012 draw_info->font != (
char *) NULL ? draw_info->font :
"none");
2013 return(RenderPostscript(image,draw_info,offset,metrics));
2048static char *EscapeParenthesis(
const char *source)
2062 assert(source != (
const char *) NULL);
2064 for (p=source; *p !=
'\0'; p++)
2066 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2069 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2074 destination=(
char *) NULL;
2075 if (~length >= (MaxTextExtent-1))
2076 destination=(
char *) AcquireQuantumMemory(length+MaxTextExtent,
2077 sizeof(*destination));
2078 if (destination == (
char *) NULL)
2079 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2082 for (p=source; *p !=
'\0'; p++)
2084 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2089 return(destination);
2092static MagickBooleanType RenderPostscript(Image *image,
2093 const DrawInfo *draw_info,
const PointInfo *offset,TypeMetric *metrics)
2096 filename[MaxTextExtent],
2097 geometry[MaxTextExtent],
2133 if (draw_info->debug != MagickFalse)
2134 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2135 "Font %s; pointsize %g",draw_info->font != (
char *) NULL ?
2136 draw_info->font :
"none",draw_info->pointsize);
2138 unique_file=AcquireUniqueFileResource(filename);
2139 if (unique_file != -1)
2140 file=fdopen(unique_file,
"wb");
2141 if ((unique_file == -1) || (file == (FILE *) NULL))
2143 ThrowFileException(&image->exception,FileOpenError,
"UnableToOpenFile",
2145 return(MagickFalse);
2147 (void) FormatLocaleFile(file,
"%%!PS-Adobe-3.0\n");
2148 (void) FormatLocaleFile(file,
"/ReencodeType\n");
2149 (void) FormatLocaleFile(file,
"{\n");
2150 (void) FormatLocaleFile(file,
" findfont dup length\n");
2151 (void) FormatLocaleFile(file,
2152 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2153 (void) FormatLocaleFile(file,
2154 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2155 (void) FormatLocaleFile(file,
"} bind def\n");
2159 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2160 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2161 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2164 length=strlen(draw_info->text);
2165 for (i=0; i <= (ssize_t) (length+2); i++)
2167 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2168 draw_info->affine.ry*2.0*draw_info->pointsize);
2169 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2170 draw_info->affine.sy*2.0*draw_info->pointsize);
2171 if (point.x > extent.x)
2173 if (point.y > extent.y)
2176 (void) FormatLocaleFile(file,
"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2177 extent.x/2.0,extent.y/2.0);
2178 (void) FormatLocaleFile(file,
"%g %g scale\n",draw_info->pointsize,
2179 draw_info->pointsize);
2180 if ((draw_info->font == (
char *) NULL) || (*draw_info->font ==
'\0') ||
2181 (strchr(draw_info->font,
'/') != (
char *) NULL))
2182 (void) FormatLocaleFile(file,
2183 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2185 (
void) FormatLocaleFile(file,
2186 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2188 (void) FormatLocaleFile(file,
"[%g %g %g %g 0 0] concat\n",
2189 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2190 draw_info->affine.sy);
2191 text=EscapeParenthesis(draw_info->text);
2192 if (identity == MagickFalse)
2193 (void) FormatLocaleFile(file,
"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2195 (void) FormatLocaleFile(file,
"(%s) show\n",text);
2196 text=DestroyString(text);
2197 (void) FormatLocaleFile(file,
"showpage\n");
2198 (void) fclose(file);
2199 (void) FormatLocaleString(geometry,MaxTextExtent,
"%.20gx%.20g+0+0!",
2200 floor(extent.x+0.5),floor(extent.y+0.5));
2201 annotate_info=AcquireImageInfo();
2202 (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,
"ps:%s",
2204 (void) CloneString(&annotate_info->page,geometry);
2205 if (draw_info->density != (
char *) NULL)
2206 (void) CloneString(&annotate_info->density,draw_info->density);
2207 annotate_info->antialias=draw_info->text_antialias;
2208 annotate_image=ReadImage(annotate_info,&image->exception);
2209 CatchException(&image->exception);
2210 annotate_info=DestroyImageInfo(annotate_info);
2211 (void) RelinquishUniqueFileResource(filename);
2212 if (annotate_image == (Image *) NULL)
2213 return(MagickFalse);
2214 resolution.x=DefaultResolution;
2215 resolution.y=DefaultResolution;
2216 if (draw_info->density != (
char *) NULL)
2224 flags=ParseGeometry(draw_info->density,&geometry_info);
2225 if ((flags & RhoValue) != 0)
2226 resolution.x=geometry_info.rho;
2227 resolution.y=resolution.x;
2228 if ((flags & SigmaValue) != 0)
2229 resolution.y=geometry_info.sigma;
2231 if (identity == MagickFalse)
2232 (void) TransformImage(&annotate_image,
"0x0",(
char *) NULL);
2238 crop_info=GetImageBoundingBox(annotate_image,&annotate_image->exception);
2239 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2240 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2241 crop_info.y=CastDoubleToLong(ceil((resolution.y/DefaultResolution)*
2243 (void) FormatLocaleString(geometry,MaxTextExtent,
2244 "%.20gx%.20g%+.20g%+.20g",(
double) crop_info.width,(
double)
2245 crop_info.height,(
double) crop_info.x,(
double) crop_info.y);
2246 (void) TransformImage(&annotate_image,geometry,(
char *) NULL);
2248 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2249 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2250 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2251 metrics->ascent=metrics->pixels_per_em.y;
2252 metrics->descent=metrics->pixels_per_em.y/-5.0;
2253 metrics->width=(double) annotate_image->columns/
2254 ExpandAffine(&draw_info->affine);
2255 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2256 metrics->max_advance=metrics->pixels_per_em.x;
2257 metrics->bounds.x1=0.0;
2258 metrics->bounds.y1=metrics->descent;
2259 metrics->bounds.x2=metrics->ascent+metrics->descent;
2260 metrics->bounds.y2=metrics->ascent+metrics->descent;
2261 metrics->underline_position=(-2.0);
2262 metrics->underline_thickness=1.0;
2263 if (draw_info->render == MagickFalse)
2265 annotate_image=DestroyImage(annotate_image);
2268 if (draw_info->fill.opacity != TransparentOpacity)
2285 if (image->matte == MagickFalse)
2286 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
2287 if (annotate_image->matte == MagickFalse)
2288 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel);
2289 fill_color=draw_info->fill;
2291 exception=(&image->exception);
2292 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2293#if defined(MAGICKCORE_OPENMP_SUPPORT)
2294 #pragma omp parallel for schedule(static) shared(status) \
2295 magick_number_threads(annotate_image,annotate_image,annotate_image->rows,4)
2297 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2305 if (status == MagickFalse)
2307 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2309 if (q == (PixelPacket *) NULL)
2314 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2316 (void) GetFillColor(draw_info,x,y,&fill_color);
2317 SetPixelAlpha(q,ClampToQuantum(((((
double) QuantumRange-
2318 GetPixelIntensity(annotate_image,q))*((
double) QuantumRange-
2319 (
double) fill_color.opacity))/(
double) QuantumRange)));
2320 SetPixelRed(q,fill_color.red);
2321 SetPixelGreen(q,fill_color.green);
2322 SetPixelBlue(q,fill_color.blue);
2325 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2326 if (sync == MagickFalse)
2329 annotate_view=DestroyCacheView(annotate_view);
2330 (void) CompositeImage(image,OverCompositeOp,annotate_image,
2331 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2332 metrics->descent)-0.5));
2334 annotate_image=DestroyImage(annotate_image);
2368static MagickBooleanType RenderX11(Image *image,
const DrawInfo *draw_info,
2369 const PointInfo *offset,TypeMetric *metrics)
2375 ActivateSemaphoreInfo(&annotate_semaphore);
2376 LockSemaphoreInfo(annotate_semaphore);
2377 status=XRenderImage(image,draw_info,offset,metrics);
2378 UnlockSemaphoreInfo(annotate_semaphore);