45#include "MagickCore/studio.h"
46#include "MagickCore/annotate.h"
47#include "MagickCore/annotate-private.h"
48#include "MagickCore/attribute.h"
49#include "MagickCore/cache-private.h"
50#include "MagickCore/cache-view.h"
51#include "MagickCore/channel.h"
52#include "MagickCore/client.h"
53#include "MagickCore/color.h"
54#include "MagickCore/color-private.h"
55#include "MagickCore/colorspace-private.h"
56#include "MagickCore/composite.h"
57#include "MagickCore/composite-private.h"
58#include "MagickCore/constitute.h"
59#include "MagickCore/draw.h"
60#include "MagickCore/draw-private.h"
61#include "MagickCore/enhance.h"
62#include "MagickCore/exception.h"
63#include "MagickCore/exception-private.h"
64#include "MagickCore/gem.h"
65#include "MagickCore/geometry.h"
66#include "MagickCore/image-private.h"
67#include "MagickCore/log.h"
68#include "MagickCore/quantum.h"
69#include "MagickCore/quantum-private.h"
70#include "MagickCore/pixel-accessor.h"
71#include "MagickCore/policy.h"
72#include "MagickCore/property.h"
73#include "MagickCore/resource_.h"
74#include "MagickCore/semaphore.h"
75#include "MagickCore/statistic.h"
76#include "MagickCore/string_.h"
77#include "MagickCore/token.h"
78#include "MagickCore/token-private.h"
79#include "MagickCore/transform.h"
80#include "MagickCore/transform-private.h"
81#include "MagickCore/type.h"
82#include "MagickCore/utility.h"
83#include "MagickCore/utility-private.h"
84#include "MagickCore/xwindow.h"
85#include "MagickCore/xwindow-private.h"
86#if defined(MAGICKCORE_FREETYPE_DELEGATE)
87#if defined(__MINGW32__)
91#if defined(FT_FREETYPE_H)
92# include FT_FREETYPE_H
94# include <freetype/freetype.h>
96#if defined(FT_GLYPH_H)
99# include <freetype/ftglyph.h>
101#if defined(FT_OUTLINE_H)
102# include FT_OUTLINE_H
104# include <freetype/ftoutln.h>
106#if defined(FT_BBOX_H)
109# include <freetype/ftbbox.h>
111#if defined(FT_MODULE_H)
114# include <freetype/ftmodapi.h>
117#if defined(MAGICKCORE_RAQM_DELEGATE)
142static MagickBooleanType
170MagickPrivate MagickBooleanType AnnotateComponentGenesis(
void)
173 annotate_semaphore=AcquireSemaphoreInfo();
195MagickPrivate
void AnnotateComponentTerminus(
void)
198 ActivateSemaphoreInfo(&annotate_semaphore);
199 RelinquishSemaphoreInfo(&annotate_semaphore);
229MagickExport MagickBooleanType AnnotateImage(
Image *image,
234 color[MagickPathExtent],
235 primitive[MagickPathExtent],
268 assert(image != (
Image *) NULL);
269 assert(image->signature == MagickCoreSignature);
270 if (IsEventLogging() != MagickFalse)
271 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
272 assert(draw_info != (
DrawInfo *) NULL);
273 assert(draw_info->signature == MagickCoreSignature);
274 if (draw_info->text == (
char *) NULL)
276 if (*draw_info->text ==
'\0')
278 annotate=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
280 annotate->text=(
char *) NULL;
281 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
283 for (p=text; *p !=
'\0'; p++)
286 textlist=(
char **) AcquireQuantumMemory(number_lines+1,
sizeof(*textlist));
287 if (textlist == (
char **) NULL)
289 annotate_info=DestroyDrawInfo(annotate_info);
290 annotate=DestroyDrawInfo(annotate);
291 text=DestroyString(text);
295 for (i=0; i < (ssize_t) number_lines; i++)
301 for (q=p; *q !=
'\0'; q++)
302 if ((*q ==
'\r') || (*q ==
'\n'))
312 textlist[i]=(
char *) NULL;
313 SetGeometry(image,&geometry);
314 SetGeometryInfo(&geometry_info);
315 if (annotate_info->geometry != (
char *) NULL)
317 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
319 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
321 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
323 annotate_info=DestroyDrawInfo(annotate_info);
324 annotate=DestroyDrawInfo(annotate);
325 textlist=(
char **) RelinquishMagickMemory(textlist);
326 text=DestroyString(text);
329 if (IsGrayColorspace(image->colorspace) != MagickFalse)
330 (void) SetImageColorspace(image,sRGBColorspace,exception);
332 (void) memset(&metrics,0,
sizeof(metrics));
333 for (i=0; textlist[i] != (
char *) NULL; i++)
335 if (*textlist[i] ==
'\0')
340 annotate_info->affine.tx=geometry_info.xi-image->page.x;
341 annotate_info->affine.ty=geometry_info.psi-image->page.y;
342 (void) CloneString(&annotate->text,textlist[i]);
343 if ((metrics.width == 0) || (annotate->gravity != NorthWestGravity))
344 (void) GetTypeMetrics(image,annotate,&metrics,exception);
345 height=CastDoubleToUnsigned(metrics.ascent-metrics.descent+0.5);
347 height=draw_info->pointsize;
348 height=CastDoubleToUnsigned(floor((
double) height+
349 draw_info->interline_spacing+0.5));
350 switch (annotate->gravity)
352 case UndefinedGravity:
355 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
356 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
359 case (GravityType) NorthWestGravity:
361 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
362 annotate_info->affine.ry*height+annotate_info->affine.ry*
363 (metrics.ascent+metrics.descent);
364 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
365 annotate_info->affine.sy*height+annotate_info->affine.sy*
369 case (GravityType) NorthGravity:
371 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
372 geometry.width/2.0+i*annotate_info->affine.ry*height-
373 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
374 (metrics.ascent+metrics.descent);
375 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
376 annotate_info->affine.sy*height+annotate_info->affine.sy*
377 metrics.ascent-annotate_info->affine.rx*metrics.width/2.0;
380 case (GravityType) NorthEastGravity:
382 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
383 geometry.width+i*annotate_info->affine.ry*height-
384 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
385 (metrics.ascent+metrics.descent)-1.0;
386 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
387 annotate_info->affine.sy*height+annotate_info->affine.sy*
388 metrics.ascent-annotate_info->affine.rx*metrics.width;
391 case (GravityType) WestGravity:
393 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
394 annotate_info->affine.ry*height+annotate_info->affine.ry*
395 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
396 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
397 geometry.height/2.0+i*annotate_info->affine.sy*height+
398 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
399 (number_lines-1.0)*height)/2.0;
402 case (GravityType) CenterGravity:
404 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
405 geometry.width/2.0+i*annotate_info->affine.ry*height-
406 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
407 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
408 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
409 geometry.height/2.0+i*annotate_info->affine.sy*height-
410 annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
411 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
414 case (GravityType) EastGravity:
416 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
417 geometry.width+i*annotate_info->affine.ry*height-
418 annotate_info->affine.sx*metrics.width+
419 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
420 (number_lines-1.0)*height)/2.0-1.0;
421 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
422 geometry.height/2.0+i*annotate_info->affine.sy*height-
423 annotate_info->affine.rx*metrics.width+
424 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
425 (number_lines-1.0)*height)/2.0;
428 case (GravityType) SouthWestGravity:
430 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
431 annotate_info->affine.ry*height-annotate_info->affine.ry*
432 (number_lines-1.0)*height;
433 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
434 geometry.height+i*annotate_info->affine.sy*height-
435 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
438 case (GravityType) SouthGravity:
440 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
441 geometry.width/2.0+i*annotate_info->affine.ry*height-
442 annotate_info->affine.sx*metrics.width/2.0-
443 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
444 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
445 geometry.height+i*annotate_info->affine.sy*height-
446 annotate_info->affine.rx*metrics.width/2.0-
447 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
450 case (GravityType) SouthEastGravity:
452 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
453 geometry.width+i*annotate_info->affine.ry*height-
454 annotate_info->affine.sx*metrics.width-
455 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
456 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
457 geometry.height+i*annotate_info->affine.sy*height-
458 annotate_info->affine.rx*metrics.width-
459 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
463 switch (annotate->align)
467 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
468 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
473 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
474 annotate_info->affine.sx*metrics.width/2.0;
475 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
476 annotate_info->affine.rx*metrics.width/2.0;
481 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
482 annotate_info->affine.sx*metrics.width;
483 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
484 annotate_info->affine.rx*metrics.width;
490 if (draw_info->undercolor.alpha != (MagickRealType) TransparentAlpha)
499 undercolor_info->fill=draw_info->undercolor;
500 undercolor_info->affine=draw_info->affine;
501 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
502 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
503 (void) FormatLocaleString(primitive,MagickPathExtent,
504 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(
double) height);
505 (void) CloneString(&undercolor_info->primitive,primitive);
506 (void) DrawImage(image,undercolor_info,exception);
507 (void) DestroyDrawInfo(undercolor_info);
509 annotate_info->affine.tx=offset.x;
510 annotate_info->affine.ty=offset.y;
511 pixel=annotate_info->fill;
512 if (annotate_info->stroke.alpha != (MagickRealType) TransparentAlpha)
513 pixel=annotate_info->stroke;
514 (void) QueryColorname(image,&pixel,AllCompliance,color,exception);
515 (void) FormatLocaleString(primitive,MagickPathExtent,
"stroke %s "
516 "stroke-width %g line 0,0 %g,0",color,(
double)
517 metrics.underline_thickness,(
double) metrics.width);
521 switch (annotate->decorate)
523 case OverlineDecoration:
525 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
526 metrics.descent-metrics.underline_position));
527 (void) CloneString(&annotate_info->primitive,primitive);
528 (void) DrawImage(image,annotate_info,exception);
531 case UnderlineDecoration:
533 annotate_info->affine.ty-=(draw_info->affine.sy*
534 metrics.underline_position);
535 (void) CloneString(&annotate_info->primitive,primitive);
536 (void) DrawImage(image,annotate_info,exception);
542 status=RenderType(image,annotate,&offset,&metrics,exception);
543 if (status == MagickFalse)
546 if (annotate->decorate == LineThroughDecoration) {
547 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
548 metrics.underline_position+metrics.descent*2)/2.0);
549 (void) CloneString(&annotate_info->primitive,primitive);
550 (void) DrawImage(image,annotate_info,exception);
556 annotate_info=DestroyDrawInfo(annotate_info);
557 annotate=DestroyDrawInfo(annotate);
558 textlist=(
char **) RelinquishMagickMemory(textlist);
559 text=DestroyString(text);
599static inline char *ReplaceSpaceWithNewline(
char **caption,
char *space)
604 if ((caption == (
char **) NULL) || (space == (
char *) NULL))
605 return((
char *) NULL);
606 octets=(size_t) GetUTFOctets(space);
620 length=strlen(*caption);
622 offset=space-(*caption);
625 target=AcquireString(*caption);
626 CopyMagickString(target,*caption,(
size_t) offset+2);
627 ConcatenateMagickString(target,space+octets,length);
628 (void) DestroyString(*caption);
630 space=(*caption)+offset;
636MagickExport ssize_t FormatMagickCaption(
Image *image,
DrawInfo *draw_info,
637 const MagickBooleanType split,
TypeMetric *metrics,
char **caption,
658 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
669 if ((draw_info->word_break != BreakWordBreakType) &&
670 (IsUTFSpace(code) != MagickFalse) &&
671 (IsNonBreakingUTFSpace(code) == MagickFalse))
674 if (width > image->columns)
675 p=ReplaceSpaceWithNewline(caption,s);
677 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
680 status=GetTypeMetrics(image,draw_info,metrics,exception);
681 if (status == MagickFalse)
683 width=CastDoubleToUnsigned(metrics->width+draw_info->stroke_width+0.5);
684 if (width <= image->columns)
686 if (s != (
char *) NULL)
687 p=ReplaceSpaceWithNewline(caption,s);
689 if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
695 if ((n > 0) && ((*caption)[n-1] !=
'\n'))
700 target=AcquireString(*caption);
701 CopyMagickString(target,*caption,(
size_t) n+1);
702 ConcatenateMagickString(target,
"\n",strlen(*caption)+1);
703 ConcatenateMagickString(target,p,strlen(*caption)+2);
704 (void) DestroyString(*caption);
713 for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
714 if (GetUTFCode(p) ==
'\n')
768MagickExport MagickBooleanType GetMultilineTypeMetrics(
Image *image,
795 assert(image != (
Image *) NULL);
796 assert(image->signature == MagickCoreSignature);
797 if (IsEventLogging() != MagickFalse)
798 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
799 assert(draw_info != (
DrawInfo *) NULL);
800 assert(draw_info->text != (
char *) NULL);
801 assert(draw_info->signature == MagickCoreSignature);
802 if (*draw_info->text ==
'\0')
804 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
805 "LabelExpected",
"`%s'",image->filename);
808 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
809 annotate_info->text=DestroyString(annotate_info->text);
813 textlist=StringToStrings(draw_info->text,&count);
814 if (textlist == (
char **) NULL)
816 annotate_info=DestroyDrawInfo(annotate_info);
819 annotate_info->render=MagickFalse;
820 annotate_info->direction=UndefinedDirection;
821 (void) memset(metrics,0,
sizeof(*metrics));
822 (void) memset(&extent,0,
sizeof(extent));
826 annotate_info->text=textlist[0];
827 status=GetTypeMetrics(image,annotate_info,&extent,exception);
829 height=(count*(size_t) (metrics->ascent-metrics->descent+
830 0.5)+(count-1)*draw_info->interline_spacing);
831 size=(MagickSizeType) fabs(height);
832 if (AcquireMagickResource(HeightResource,size) == MagickFalse)
834 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
835 "WidthOrHeightExceedsLimit",
"`%s'",image->filename);
840 for (i=1; i < (ssize_t) count; i++)
842 annotate_info->text=textlist[i];
843 status=GetTypeMetrics(image,annotate_info,&extent,exception);
844 if (status == MagickFalse)
846 if (extent.width > metrics->width)
848 size=(MagickSizeType) fabs(extent.width);
849 if (AcquireMagickResource(WidthResource,size) == MagickFalse)
851 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
852 "WidthOrHeightExceedsLimit",
"`%s'",image->filename);
857 metrics->height=(double) height;
862 annotate_info->text=(
char *) NULL;
863 annotate_info=DestroyDrawInfo(annotate_info);
864 for (i=0; i < (ssize_t) count; i++)
865 textlist[i]=DestroyString(textlist[i]);
866 textlist=(
char **) RelinquishMagickMemory(textlist);
916MagickExport MagickBooleanType GetTypeMetrics(
Image *image,
928 assert(image != (
Image *) NULL);
929 assert(image->signature == MagickCoreSignature);
930 if (IsEventLogging() != MagickFalse)
931 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
932 assert(draw_info != (
DrawInfo *) NULL);
933 assert(draw_info->text != (
char *) NULL);
934 assert(draw_info->signature == MagickCoreSignature);
935 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
936 annotate_info->render=MagickFalse;
937 annotate_info->direction=UndefinedDirection;
938 (void) memset(metrics,0,
sizeof(*metrics));
941 status=RenderType(image,annotate_info,&offset,metrics,exception);
942 if (draw_info->debug != MagickFalse)
943 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Metrics: text: %s; "
944 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
945 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
946 "underline position: %g; underline thickness: %g",annotate_info->text,
947 metrics->width,metrics->height,metrics->ascent,metrics->descent,
948 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
949 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
950 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
951 metrics->underline_position,metrics->underline_thickness);
952 annotate_info=DestroyDrawInfo(annotate_info);
988static MagickBooleanType RenderType(
Image *image,
const DrawInfo *draw_info,
1004 if (draw_info->font != (
char *) NULL)
1006 if (*draw_info->font ==
'@')
1008 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
1012 if (*draw_info->font ==
'-')
1013 return(RenderX11(image,draw_info,offset,metrics,exception));
1014 if (*draw_info->font ==
'^')
1015 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1016 if (IsPathAccessible(draw_info->font) != MagickFalse)
1018 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
1022 type_info=GetTypeInfo(draw_info->font,exception);
1023 if (type_info == (
const TypeInfo *) NULL)
1024 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1025 "UnableToReadFont",
"`%s'",draw_info->font);
1027 if ((type_info == (
const TypeInfo *) NULL) &&
1028 (draw_info->family != (
const char *) NULL))
1030 if (strpbrk(draw_info->family,
",'\"") == (
char *) NULL)
1031 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
1032 draw_info->stretch,draw_info->weight,exception);
1033 if (type_info == (
const TypeInfo *) NULL)
1047 family=StringToArgv(draw_info->family,&number_families);
1048 for (i=1; i < (ssize_t) number_families; i++)
1050 (void) SubstituteString(&family[i],
",",
"");
1051 type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1052 draw_info->stretch,draw_info->weight,exception);
1053 if ((type_info != (
const TypeInfo *) NULL) &&
1054 (LocaleCompare(family[i],type_info->family) == 0))
1057 for (i=0; i < (ssize_t) number_families; i++)
1058 family[i]=DestroyString(family[i]);
1059 family=(
char **) RelinquishMagickMemory(family);
1060 if (type_info == (
const TypeInfo *) NULL)
1061 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1062 "UnableToReadFont",
"`%s'",draw_info->family);
1065 font=GetPolicyValue(
"system:font");
1066 if (font != (
char *) NULL)
1068 if (IsPathAccessible(font) != MagickFalse)
1073 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1074 annotate_info->font=font;
1075 status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1076 offset,metrics,exception);
1077 annotate_info=DestroyDrawInfo(annotate_info);
1080 font=DestroyString(font);
1082 if (type_info == (
const TypeInfo *) NULL)
1090 sans_exception=AcquireExceptionInfo();
1091 if (type_info == (
const TypeInfo *) NULL)
1092 type_info=GetTypeInfoByFamily(
"Noto Sans",draw_info->style,
1093 draw_info->stretch,draw_info->weight,sans_exception);
1094 if (type_info == (
const TypeInfo *) NULL)
1095 type_info=GetTypeInfoByFamily(
"Nimbus Sans",draw_info->style,
1096 draw_info->stretch,draw_info->weight,sans_exception);
1097 if (type_info == (
const TypeInfo *) NULL)
1098 type_info=GetTypeInfoByFamily((
const char *) NULL,draw_info->style,
1099 draw_info->stretch,draw_info->weight,sans_exception);
1100 if (type_info == (
const TypeInfo *) NULL)
1101 type_info=GetTypeInfo(
"*",sans_exception);
1102 sans_exception=DestroyExceptionInfo(sans_exception);
1104 if (type_info == (
const TypeInfo *) NULL)
1106 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
1110 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1111 annotate_info->face=type_info->face;
1112 if (type_info->metrics != (
char *) NULL)
1113 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1114 if (type_info->glyphs != (
char *) NULL)
1115 (void) CloneString(&annotate_info->font,type_info->glyphs);
1116 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
1118 annotate_info=DestroyDrawInfo(annotate_info);
1158#if defined(MAGICKCORE_FREETYPE_DELEGATE)
1160#if defined(MAGICKCORE_RAQM_DELEGATE)
1161static size_t ComplexRaqmTextLayout(
const Image *image,
1162 const DrawInfo *draw_info,
const char *text,
const size_t length,
1182 if (rq == (raqm_t *) NULL)
1184 if (raqm_set_text_utf8(rq,text,length) == 0)
1186 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1188 if (raqm_set_freetype_face(rq,face) == 0)
1190 features=GetImageProperty(image,
"type:features",exception);
1191 if (features != (
const char *) NULL)
1206 token_info=AcquireTokenInfo();
1207 token=AcquireString(
"");
1208 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1209 &breaker,&next,"e);
1210 while (status_token == 0)
1212 raqm_add_font_feature(rq,token,(
int) strlen(token));
1213 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1214 &breaker,&next,"e);
1216 token_info=DestroyTokenInfo(token_info);
1217 token=DestroyString(token);
1219 if (raqm_layout(rq) == 0)
1221 glyphs=raqm_get_glyphs(rq,&extent);
1222 if (glyphs == (raqm_glyph_t *) NULL)
1227 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(extent,
sizeof(**grapheme));
1233 for (i=0; i < (ssize_t) extent; i++)
1235 (*grapheme)[i].index=glyphs[i].index;
1236 (*grapheme)[i].x_offset=glyphs[i].x_offset;
1237 (*grapheme)[i].x_advance=glyphs[i].x_advance;
1238 (*grapheme)[i].y_offset=glyphs[i].y_offset;
1239 (*grapheme)[i].y_advance=glyphs[i].y_advance;
1240 (*grapheme)[i].cluster=glyphs[i].cluster;
1247static size_t ComplexTextLayout(
const DrawInfo *draw_info,
const char *text,
1248 const size_t length,
const FT_Face face,
const FT_Int32 flags,
1263 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(length+1,
sizeof(**grapheme));
1268 for (i=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p), i++)
1270 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,(FT_ULong)
1272 (*grapheme)[i].x_offset=0;
1273 (*grapheme)[i].y_offset=0;
1274 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1276 if (FT_HAS_KERNING(face))
1284 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1285 (*grapheme)[i].index,ft_kerning_default,&kerning);
1287 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1288 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1291 (void) FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1292 (*grapheme)[i].x_advance=face->glyph->advance.x;
1293 (*grapheme)[i].y_advance=face->glyph->advance.y;
1294 (*grapheme)[i].cluster=(size_t) (p-text);
1295 last_glyph=(*grapheme)[i].index;
1301static void FreetypeCloseStream(FT_Stream stream)
1306 file=(FILE *) stream->descriptor.pointer;
1307 if (file != (FILE *) NULL)
1308 (void) fclose(file);
1309 stream->descriptor.pointer=NULL;
1312static unsigned long FreetypeReadStream(FT_Stream stream,
unsigned long offset,
1313 unsigned char *buffer,
unsigned long count)
1321 file=(FILE *) stream->descriptor.pointer;
1322 if (file == (FILE *) NULL)
1324 if (offset > stream->size)
1327 result=(
unsigned long) fseek(file,(off_t) offset,SEEK_SET);
1332 return((
unsigned long) fread(buffer,1,count,file));
1335static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1337 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1341static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1348 path[MagickPathExtent];
1350 affine=draw_info->affine;
1351 (void) FormatLocaleString(path,MagickPathExtent,
"C%g,%g %g,%g %g,%g",
1352 affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-
1353 q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1354 (void) ConcatenateString(&draw_info->primitive,path);
1358static int TraceLineTo(FT_Vector *to,
DrawInfo *draw_info)
1364 path[MagickPathExtent];
1366 affine=draw_info->affine;
1367 (void) FormatLocaleString(path,MagickPathExtent,
"L%g,%g",affine.tx+to->x/64.0,
1368 affine.ty-to->y/64.0);
1369 (void) ConcatenateString(&draw_info->primitive,path);
1373static int TraceMoveTo(FT_Vector *to,
DrawInfo *draw_info)
1379 path[MagickPathExtent];
1381 affine=draw_info->affine;
1382 (void) FormatLocaleString(path,MagickPathExtent,
"M%g,%g",affine.tx+to->x/64.0,
1383 affine.ty-to->y/64.0);
1384 (void) ConcatenateString(&draw_info->primitive,path);
1388static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1395 path[MagickPathExtent];
1397 affine=draw_info->affine;
1398 (void) FormatLocaleString(path,MagickPathExtent,
"Q%g,%g %g,%g",affine.tx+
1399 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1401 (void) ConcatenateString(&draw_info->primitive,path);
1405#if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1406static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1408 return(FT_Error_String(ft_status));
1410static inline const char *FreetypeErrorMessage(
1411 FT_Error magick_unused(ft_status))
1413 magick_unreferenced(ft_status);
1414 return((
const char *) NULL);
1418static void *FreetypeAlloc(FT_Memory magick_unused(memory),
long size)
1420 magick_unreferenced(memory);
1422 return((
void *) NULL);
1423 if ((
size_t) size > GetMaxMemoryRequest())
1424 return((
void *) NULL);
1425 return(AcquireMagickMemory((
size_t) size));
1428static void *FreetypeRealloc(FT_Memory magick_unused(memory),
1429 long magick_unused(cur_size),
long size,
void *block)
1431 magick_unreferenced(memory);
1432 magick_unreferenced(cur_size);
1434 return((
void *) NULL);
1435 if ((
size_t) size > GetMaxMemoryRequest())
1436 return((
void *) NULL);
1437 return(ResizeMagickMemory(block,(
size_t) size));
1440static void FreetypeFree(FT_Memory magick_unused(memory),
void *block)
1442 magick_unreferenced(memory);
1443 (void) RelinquishMagickMemory(block);
1446static FT_Memory FreetypeAcquireMemoryManager(
void)
1451 memory=(FT_Memory) AcquireMagickMemory(
sizeof(*memory));
1452 if (memory == (FT_Memory) NULL)
1454 memset(memory,0,
sizeof(*memory));
1455 memory->alloc=(&FreetypeAlloc);
1456 memory->realloc=(&FreetypeRealloc);
1457 memory->free=(&FreetypeFree);
1461static void FreetypeDone(FT_Memory memory,FT_Library library,
1462 FT_StreamRec *stream)
1464 (void) FT_Done_Library(library);
1465 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1466 memory=(FT_Memory) RelinquishMagickMemory(memory);
1469static FT_Error FreetypeInit(FT_Memory memory,FT_Library *alibrary)
1474 ft_status=FT_New_Library(memory,alibrary);
1476 RelinquishMagickMemory(memory);
1478 FT_Add_Default_Modules(*alibrary);
1479 FT_Set_Default_Properties(*alibrary);
1483static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
1487#if !defined(FT_OPEN_PATHNAME)
1488#define FT_OPEN_PATHNAME ft_open_pathname
1491#define ThrowFreetypeErrorException(tag,ft_status,value) \
1494 *error_string=FreetypeErrorMessage(ft_status); \
1495 if (error_string != (const char *) NULL) \
1496 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1497 tag,"`%s (%s)'",value, error_string); \
1499 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1500 tag,"`%s'",value); \
1503 typedef struct _GlyphInfo
1592 static FT_Outline_Funcs
1595 (FT_Outline_MoveTo_Func) TraceMoveTo,
1596 (FT_Outline_LineTo_Func) TraceLineTo,
1597 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1598 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1611 memory=FreetypeAcquireMemoryManager();
1612 if (memory == (FT_Memory) NULL)
1613 ThrowBinaryException(ResourceLimitError,
"UnableToInitializeFreetypeLibrary",
1615 ft_status=FreetypeInit(memory,&library);
1617 ThrowFreetypeErrorException(
"UnableToInitializeFreetypeLibrary",ft_status,
1622 face_index=(FT_Long) draw_info->face;
1623 (void) memset(&args,0,
sizeof(args));
1624 if (draw_info->font == (
char *) NULL)
1626 const TypeInfo *type_info = GetTypeInfo(
"*",exception);
1627 if (type_info != (
const TypeInfo *) NULL)
1628 args.pathname=ConstantString(type_info->glyphs);
1631 if (*draw_info->font !=
'@')
1632 args.pathname=ConstantString(draw_info->font);
1638 ImageInfo *image_info = AcquireImageInfo();
1639 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1641 (void) SetImageInfo(image_info,0,exception);
1642 face_index=(FT_Long) image_info->scene;
1643 args.pathname=ConstantString(image_info->filename);
1644 image_info=DestroyImageInfo(image_info);
1649 stream=(FT_StreamRec *) AcquireCriticalMemory(
sizeof(*stream));
1650 (void) memset(stream,0,
sizeof(*stream));
1651 if (stat(args.pathname,&attributes) == 0)
1652 stream->size=attributes.st_size >= 0 ? (
unsigned long)
1653 attributes.st_size : 0;
1654 stream->descriptor.pointer=fopen_utf8(args.pathname,
"rb");
1655 stream->read=(&FreetypeReadStream);
1656 stream->close=(&FreetypeCloseStream);
1657 args.flags=FT_OPEN_STREAM;
1659 face=(FT_Face) NULL;
1660 ft_status=FT_Open_Face(library,&args,face_index,&face);
1663 FreetypeDone(memory,library,stream);
1664 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,args.pathname);
1665 args.pathname=DestroyString(args.pathname);
1666 return(MagickFalse);
1668 args.pathname=DestroyString(args.pathname);
1669 if ((draw_info->metrics != (
char *) NULL) &&
1670 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1671 (void) FT_Attach_File(face,draw_info->metrics);
1672 encoding_type=FT_ENCODING_UNICODE;
1673 ft_status=FT_Select_Charmap(face,encoding_type);
1674 if ((ft_status != 0) && (face->num_charmaps != 0))
1675 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1676 if (encoding != (
const char *) NULL)
1678 if (LocaleCompare(encoding,
"AdobeCustom") == 0)
1679 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1680 if (LocaleCompare(encoding,
"AdobeExpert") == 0)
1681 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1682 if (LocaleCompare(encoding,
"AdobeStandard") == 0)
1683 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1684 if (LocaleCompare(encoding,
"AppleRoman") == 0)
1685 encoding_type=FT_ENCODING_APPLE_ROMAN;
1686 if (LocaleCompare(encoding,
"BIG5") == 0)
1687 encoding_type=FT_ENCODING_BIG5;
1688#if defined(FT_ENCODING_PRC)
1689 if (LocaleCompare(encoding,
"GB2312") == 0)
1690 encoding_type=FT_ENCODING_PRC;
1692#if defined(FT_ENCODING_JOHAB)
1693 if (LocaleCompare(encoding,
"Johab") == 0)
1694 encoding_type=FT_ENCODING_JOHAB;
1696#if defined(FT_ENCODING_ADOBE_LATIN_1)
1697 if (LocaleCompare(encoding,
"Latin-1") == 0)
1698 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1700#if defined(FT_ENCODING_ADOBE_LATIN_2)
1701 if (LocaleCompare(encoding,
"Latin-2") == 0)
1702 encoding_type=FT_ENCODING_OLD_LATIN_2;
1704 if (LocaleCompare(encoding,
"None") == 0)
1705 encoding_type=FT_ENCODING_NONE;
1706 if (LocaleCompare(encoding,
"SJIScode") == 0)
1707 encoding_type=FT_ENCODING_SJIS;
1708 if (LocaleCompare(encoding,
"Symbol") == 0)
1709 encoding_type=FT_ENCODING_MS_SYMBOL;
1710 if (LocaleCompare(encoding,
"Unicode") == 0)
1711 encoding_type=FT_ENCODING_UNICODE;
1712 if (LocaleCompare(encoding,
"Wansung") == 0)
1713 encoding_type=FT_ENCODING_WANSUNG;
1714 ft_status=FT_Select_Charmap(face,encoding_type);
1717 (void) FT_Done_Face(face);
1718 FreetypeDone(memory,library,stream);
1719 ThrowFreetypeErrorException(
"UnrecognizedFontEncoding",ft_status,
1721 return(MagickFalse);
1727 resolution.x=DefaultResolution;
1728 resolution.y=DefaultResolution;
1729 if (draw_info->density != (
char *) NULL)
1737 geometry_flags=ParseGeometry(draw_info->density,&geometry_info);
1738 if ((geometry_flags & RhoValue) != 0)
1739 resolution.x=geometry_info.rho;
1740 resolution.y=resolution.x;
1741 if ((geometry_flags & SigmaValue) != 0)
1742 resolution.y=geometry_info.sigma;
1744 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1745 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1746 (FT_UInt) resolution.y);
1749 (void) FT_Done_Face(face);
1750 FreetypeDone(memory,library,stream);
1751 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,
1753 return(MagickFalse);
1755 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1756 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1757 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1758 metrics->descent=(double) face->size->metrics.descender/64.0;
1759 if (face->size->metrics.ascender == 0)
1764 metrics->ascent=face->size->metrics.y_ppem;
1765 if (face->size->metrics.descender == 0)
1766 metrics->descent=face->size->metrics.y_ppem/-3.5;
1769 metrics->origin.x=0;
1770 metrics->origin.y=0;
1771 metrics->height=(double) face->size->metrics.height/64.0;
1772 metrics->max_advance=0.0;
1773 if (face->size->metrics.max_advance > MagickEpsilon)
1774 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1775 metrics->bounds.x1=0.0;
1776 metrics->bounds.y1=metrics->descent;
1777 metrics->bounds.x2=metrics->ascent+metrics->descent;
1778 metrics->bounds.y2=metrics->ascent+metrics->descent;
1779 metrics->underline_position=face->underline_position*
1780 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1781 metrics->underline_thickness=face->underline_thickness*
1782 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1784 FT_Get_First_Char(face,&first_glyph_id);
1785 if ((draw_info->text == (
char *) NULL) || (*draw_info->text ==
'\0') ||
1786 (first_glyph_id == 0))
1788 (void) FT_Done_Face(face);
1789 FreetypeDone(memory,library,stream);
1795 if (draw_info->debug != MagickFalse)
1796 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Font %s; "
1797 "font-encoding %s; text-encoding %s; pointsize %g",
1798 draw_info->font != (
char *) NULL ? draw_info->font :
"none",
1799 encoding != (
char *) NULL ? encoding :
"none",
1800 draw_info->encoding != (
char *) NULL ? draw_info->encoding :
"none",
1801 draw_info->pointsize);
1802 flags=FT_LOAD_DEFAULT;
1803 if (draw_info->render == MagickFalse)
1804 flags=FT_LOAD_NO_BITMAP;
1805 if (draw_info->text_antialias == MagickFalse)
1806 flags|=FT_LOAD_TARGET_MONO;
1809#if defined(FT_LOAD_TARGET_LIGHT)
1810 flags|=FT_LOAD_TARGET_LIGHT;
1811#elif defined(FT_LOAD_TARGET_LCD)
1812 flags|=FT_LOAD_TARGET_LCD;
1815 value=GetImageProperty(image,
"type:hinting",exception);
1816 if ((value != (
const char *) NULL) && (LocaleCompare(value,
"off") == 0))
1817 flags|=FT_LOAD_NO_HINTING;
1819 glyph.image=(FT_Glyph) NULL;
1827 if (draw_info->render != MagickFalse)
1829 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1830 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1831 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1832 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1834 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1835 if (annotate_info->dash_pattern != (
double *) NULL)
1836 annotate_info->dash_pattern[0]=0.0;
1837 (void) CloneString(&annotate_info->primitive,
"path '");
1839 if (draw_info->render != MagickFalse)
1841 if (image->storage_class != DirectClass)
1842 (void) SetImageStorageClass(image,DirectClass,exception);
1843 if ((image->alpha_trait & BlendPixelTrait) == 0)
1844 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1846 for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1847 if (GetUTFCode(p) < 0)
1849 utf8=(
unsigned char *) NULL;
1850 if (GetUTFCode(p) == 0)
1854 utf8=ConvertLatin1ToUTF8((
unsigned char *) draw_info->text);
1855 if (utf8 != (
unsigned char *) NULL)
1859#if defined(MAGICKCORE_RAQM_DELEGATE)
1860 length=ComplexRaqmTextLayout(image,draw_info,p,strlen(p),face,&grapheme,
1863 length=ComplexTextLayout(draw_info,p,strlen(p),face,flags,&grapheme);
1865 missing_glyph_id=FT_Get_Char_Index(face,
' ');
1867 last_character=(ssize_t) length-1;
1868 for (i=0; i < (ssize_t) length; i++)
1876 glyph.id=(FT_UInt) grapheme[i].index;
1878 glyph.id=missing_glyph_id;
1879 if ((glyph.id != 0) && (last_glyph_id != 0))
1880 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1881 glyph.origin=origin;
1882 glyph.origin.x+=(FT_Pos) grapheme[i].x_offset;
1883 glyph.origin.y+=(FT_Pos) grapheme[i].y_offset;
1884 if (glyph.image != (FT_Glyph) NULL)
1886 FT_Done_Glyph(glyph.image);
1887 glyph.image=(FT_Glyph) NULL;
1889 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1892 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1895 outline=((FT_OutlineGlyph) glyph.image)->outline;
1896 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1897 (IsEmptyOutline(outline) == MagickFalse))
1899 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1902 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1903 metrics->bounds.x1=(double) bounds.xMin;
1904 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1905 metrics->bounds.y1=(double) bounds.yMin;
1906 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1907 metrics->bounds.x2=(double) bounds.xMax;
1908 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1909 metrics->bounds.y2=(double) bounds.yMax;
1910 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
1911 (draw_info->stroke_pattern != (
Image *) NULL)) &&
1912 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1917 annotate_info->affine.tx=glyph.origin.x/64.0;
1918 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1919 if (IsEmptyOutline(outline) == MagickFalse)
1920 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1923 FT_Vector_Transform(&glyph.origin,&affine);
1924 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1925 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1926 (FT_Vector *) NULL,MagickTrue);
1929 bitmap=(FT_BitmapGlyph) glyph.image;
1930 point.x=offset->x+bitmap->left;
1931 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1932 point.x+=(origin.x/64.0);
1933 point.y=offset->y-bitmap->top;
1934 if (draw_info->render != MagickFalse)
1948 transparent_fill=((draw_info->fill.alpha == (MagickRealType) TransparentAlpha) &&
1949 (draw_info->fill_pattern == (
Image *) NULL) &&
1950 (draw_info->stroke.alpha == (MagickRealType) TransparentAlpha) &&
1951 (draw_info->stroke_pattern == (
Image *) NULL)) ? MagickTrue :
1953 r=bitmap->bitmap.buffer;
1954 image_view=AcquireAuthenticCacheView(image,exception);
1955#if defined(MAGICKCORE_OPENMP_SUPPORT)
1956 #pragma omp parallel for schedule(static) shared(status) \
1957 magick_number_threads(image,image,bitmap->bitmap.rows,4)
1959 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1982 if (status == MagickFalse)
1984 x_offset=CastDoubleToLong(ceil(point.x-0.5));
1985 y_offset=CastDoubleToLong(ceil(point.y+y-0.5));
1986 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1989 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1993 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1994 bitmap->bitmap.width,1,exception);
1995 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1997 n=y*bitmap->bitmap.pitch;
1998 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
2000 x_offset=CastDoubleToLong(ceil(point.x+x-0.5));
2001 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
2003 if (q != (Quantum *) NULL)
2004 q+=GetPixelChannels(image);
2008 if (bitmap->bitmap.buffer != (
unsigned char *) NULL)
2010 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
2011 fill_opacity=(double) (r[n])/(bitmap->bitmap.num_grays-1);
2013 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
2014 fill_opacity=((r[(x >> 3)+y*bitmap->bitmap.pitch] &
2015 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
2017 if (draw_info->text_antialias == MagickFalse)
2018 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
2019 if (active == MagickFalse)
2020 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
2022 if (q == (Quantum *) NULL)
2024 if (transparent_fill == MagickFalse)
2026 GetPixelInfo(image,&fill_color);
2027 GetFillColor(draw_info,x_offset,y_offset,&fill_color,exception);
2028 fill_opacity=fill_opacity*fill_color.alpha;
2029 CompositePixelOver(image,&fill_color,fill_opacity,q,
2030 GetPixelAlpha(image,q),q);
2038 Da=1.0-(QuantumScale*(double) GetPixelAlpha(image,q));
2040 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*(
double)
2042 SetPixelAlpha(image,fill_opacity,q);
2044 if (active == MagickFalse)
2046 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2047 if (sync == MagickFalse)
2050 q+=GetPixelChannels(image);
2052 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2053 if (sync == MagickFalse)
2056 image_view=DestroyCacheView(image_view);
2057 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
2058 (draw_info->stroke_pattern != (
Image *) NULL)) &&
2059 (status != MagickFalse))
2064 annotate_info->linejoin=RoundJoin;
2065 annotate_info->affine.tx=offset->x;
2066 annotate_info->affine.ty=offset->y;
2067 (void) ConcatenateString(&annotate_info->primitive,
"'");
2068 if (strlen(annotate_info->primitive) > 7)
2069 (void) DrawImage(image,annotate_info,exception);
2070 (void) CloneString(&annotate_info->primitive,
"path '");
2073 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
2074 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
2075 (IsUTFSpace(code) == MagickFalse))
2076 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
2078 if (i == last_character)
2079 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
2081 origin.x+=(FT_Pos) grapheme[i].x_advance;
2082 origin.y+=(FT_Pos) grapheme[i].y_advance;
2083 metrics->origin.x=(double) origin.x;
2084 metrics->origin.y=(double) origin.y;
2085 if (metrics->origin.x > metrics->width)
2086 metrics->width=metrics->origin.x;
2087 last_glyph_id=glyph.id;
2088 code=GetUTFCode(p+grapheme[i].cluster);
2091 grapheme=(
GraphemeInfo *) RelinquishMagickMemory(grapheme);
2092 if (utf8 != (
unsigned char *) NULL)
2093 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
2094 if (glyph.image != (FT_Glyph) NULL)
2096 FT_Done_Glyph(glyph.image);
2097 glyph.image=(FT_Glyph) NULL;
2102 metrics->bounds.x1/=64.0;
2103 metrics->bounds.y1/=64.0;
2104 metrics->bounds.x2/=64.0;
2105 metrics->bounds.y2/=64.0;
2106 metrics->origin.x/=64.0;
2107 metrics->origin.y/=64.0;
2108 metrics->width=ceil(metrics->width/64.0);
2112 annotate_info=DestroyDrawInfo(annotate_info);
2113 (void) FT_Done_Face(face);
2114 FreetypeDone(memory,library,stream);
2118static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
2119 const char *magick_unused(encoding),
const PointInfo *offset,
2122 magick_unreferenced(encoding);
2123 (void) ThrowMagickException(exception,GetMagickModule(),
2124 MissingDelegateWarning,
"DelegateLibrarySupportNotBuiltIn",
"'%s' (Freetype)",
2125 draw_info->font != (
char *) NULL ? draw_info->font :
"none");
2126 return(RenderPostscript(image,draw_info,offset,metrics,exception));
2163static char *EscapeParenthesis(
const char *source)
2177 assert(source != (
const char *) NULL);
2179 for (p=source; *p !=
'\0'; p++)
2181 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2184 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2189 destination=(
char *) NULL;
2190 if (~length >= (MagickPathExtent-1))
2191 destination=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
2192 sizeof(*destination));
2193 if (destination == (
char *) NULL)
2194 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2197 for (p=source; *p !=
'\0'; p++)
2199 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2204 return(destination);
2207static MagickBooleanType RenderPostscript(
Image *image,
2212 filename[MagickPathExtent],
2213 geometry[MagickPathExtent],
2249 if (draw_info->debug != MagickFalse)
2250 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2251 "Font %s; pointsize %g",draw_info->font != (
char *) NULL ?
2252 draw_info->font :
"none",draw_info->pointsize);
2254 unique_file=AcquireUniqueFileResource(filename);
2255 if (unique_file != -1)
2256 file=fdopen(unique_file,
"wb");
2257 if ((unique_file == -1) || (file == (FILE *) NULL))
2259 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",filename);
2260 return(MagickFalse);
2262 (void) FormatLocaleFile(file,
"%%!PS-Adobe-3.0\n");
2263 (void) FormatLocaleFile(file,
"/ReencodeType\n");
2264 (void) FormatLocaleFile(file,
"{\n");
2265 (void) FormatLocaleFile(file,
" findfont dup length\n");
2266 (void) FormatLocaleFile(file,
2267 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2268 (void) FormatLocaleFile(file,
2269 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2270 (void) FormatLocaleFile(file,
"} bind def\n");
2274 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2275 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2276 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2279 length=strlen(draw_info->text);
2280 for (i=0; i <= (ssize_t) (length+2); i++)
2282 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2283 draw_info->affine.ry*2.0*draw_info->pointsize);
2284 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2285 draw_info->affine.sy*2.0*draw_info->pointsize);
2286 if (point.x > extent.x)
2288 if (point.y > extent.y)
2291 (void) FormatLocaleFile(file,
"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2292 extent.x/2.0,extent.y/2.0);
2293 (void) FormatLocaleFile(file,
"%g %g scale\n",draw_info->pointsize,
2294 draw_info->pointsize);
2295 if ((draw_info->font == (
char *) NULL) || (*draw_info->font ==
'\0') ||
2296 (strchr(draw_info->font,
'/') != (
char *) NULL))
2297 (void) FormatLocaleFile(file,
2298 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2300 (
void) FormatLocaleFile(file,
2301 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2303 (void) FormatLocaleFile(file,
"[%g %g %g %g 0 0] concat\n",
2304 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2305 draw_info->affine.sy);
2306 text=EscapeParenthesis(draw_info->text);
2307 if (identity == MagickFalse)
2308 (void) FormatLocaleFile(file,
"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2310 (void) FormatLocaleFile(file,
"(%s) show\n",text);
2311 text=DestroyString(text);
2312 (void) FormatLocaleFile(file,
"showpage\n");
2313 (void) fclose(file);
2314 (void) FormatLocaleString(geometry,MagickPathExtent,
"%.20gx%.20g+0+0!",
2315 floor(extent.x+0.5),floor(extent.y+0.5));
2316 annotate_info=AcquireImageInfo();
2317 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,
"ps:%s",
2319 (void) CloneString(&annotate_info->page,geometry);
2320 if (draw_info->density != (
char *) NULL)
2321 (void) CloneString(&annotate_info->density,draw_info->density);
2322 annotate_info->antialias=draw_info->text_antialias;
2323 annotate_image=ReadImage(annotate_info,exception);
2324 CatchException(exception);
2325 annotate_info=DestroyImageInfo(annotate_info);
2326 (void) RelinquishUniqueFileResource(filename);
2327 if (annotate_image == (
Image *) NULL)
2328 return(MagickFalse);
2329 (void) NegateImage(annotate_image,MagickFalse,exception);
2330 resolution.x=DefaultResolution;
2331 resolution.y=DefaultResolution;
2332 if (draw_info->density != (
char *) NULL)
2340 flags=ParseGeometry(draw_info->density,&geometry_info);
2341 if ((flags & RhoValue) != 0)
2342 resolution.x=geometry_info.rho;
2343 resolution.y=resolution.x;
2344 if ((flags & SigmaValue) != 0)
2345 resolution.y=geometry_info.sigma;
2347 if (identity == MagickFalse)
2348 (void) TransformImage(&annotate_image,
"0x0",(
char *) NULL,exception);
2354 crop_info=GetImageBoundingBox(annotate_image,exception);
2355 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2356 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2357 crop_info.y=CastDoubleToLong(ceil((resolution.y/DefaultResolution)*
2359 (void) FormatLocaleString(geometry,MagickPathExtent,
2360 "%.20gx%.20g%+.20g%+.20g",(
double) crop_info.width,(
double)
2361 crop_info.height,(
double) crop_info.x,(
double) crop_info.y);
2362 (void) TransformImage(&annotate_image,geometry,(
char *) NULL,exception);
2364 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2365 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2366 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2367 metrics->ascent=metrics->pixels_per_em.y;
2368 metrics->descent=metrics->pixels_per_em.y/-5.0;
2369 metrics->width=(double) annotate_image->columns/
2370 ExpandAffine(&draw_info->affine);
2371 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2372 metrics->max_advance=metrics->pixels_per_em.x;
2373 metrics->bounds.x1=0.0;
2374 metrics->bounds.y1=metrics->descent;
2375 metrics->bounds.x2=metrics->ascent+metrics->descent;
2376 metrics->bounds.y2=metrics->ascent+metrics->descent;
2377 metrics->underline_position=(-2.0);
2378 metrics->underline_thickness=1.0;
2379 if (draw_info->render == MagickFalse)
2381 annotate_image=DestroyImage(annotate_image);
2384 if (draw_info->fill.alpha != (MagickRealType) TransparentAlpha)
2398 if ((image->alpha_trait & BlendPixelTrait) == 0)
2399 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2400 if (annotate_image->alpha_trait == UndefinedPixelTrait)
2401 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
2403 fill_color=draw_info->fill;
2405 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2406#if defined(MAGICKCORE_OPENMP_SUPPORT)
2407 #pragma omp parallel for schedule(static) shared(status) \
2408 magick_number_threads(annotate_image,annotate_image,annotate_image->rows,4)
2410 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2418 if (status == MagickFalse)
2420 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2422 if (q == (Quantum *) NULL)
2427 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2429 GetFillColor(draw_info,x,y,&fill_color,exception);
2430 SetPixelAlpha(annotate_image,ClampToQuantum((((
double) QuantumScale*
2431 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
2432 SetPixelRed(annotate_image,fill_color.red,q);
2433 SetPixelGreen(annotate_image,fill_color.green,q);
2434 SetPixelBlue(annotate_image,fill_color.blue,q);
2435 q+=GetPixelChannels(annotate_image);
2437 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2438 if (sync == MagickFalse)
2441 annotate_view=DestroyCacheView(annotate_view);
2442 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
2443 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2444 metrics->descent)-0.5),exception);
2446 annotate_image=DestroyImage(annotate_image);
2482static MagickBooleanType RenderX11(
Image *image,
const DrawInfo *draw_info,
2489 ActivateSemaphoreInfo(&annotate_semaphore);
2490 LockSemaphoreInfo(annotate_semaphore);
2491 status=XRenderImage(image,draw_info,offset,metrics,exception);
2492 UnlockSemaphoreInfo(annotate_semaphore);