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+=(ptrdiff_t) 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+=(ptrdiff_t) 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((
const char *) NULL,draw_info->style,
1093 draw_info->stretch,draw_info->weight,sans_exception);
1094 if (type_info == (
const TypeInfo *) NULL)
1095 type_info=GetTypeInfo(
"*",sans_exception);
1096 sans_exception=DestroyExceptionInfo(sans_exception);
1098 if (type_info == (
const TypeInfo *) NULL)
1100 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
1104 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1105 annotate_info->face=type_info->face;
1106 if (type_info->metrics != (
char *) NULL)
1107 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1108 if (type_info->glyphs != (
char *) NULL)
1109 (void) CloneString(&annotate_info->font,type_info->glyphs);
1110 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
1112 annotate_info=DestroyDrawInfo(annotate_info);
1152#if defined(MAGICKCORE_FREETYPE_DELEGATE)
1154#if defined(MAGICKCORE_RAQM_DELEGATE)
1155static size_t ComplexRaqmTextLayout(
const Image *image,
1156 const DrawInfo *draw_info,
const char *text,
const size_t length,
1176 if (rq == (raqm_t *) NULL)
1178 if (raqm_set_text_utf8(rq,text,length) == 0)
1180 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1182 if (raqm_set_freetype_face(rq,face) == 0)
1184 features=GetImageProperty(image,
"type:features",exception);
1185 if (features != (
const char *) NULL)
1200 token_info=AcquireTokenInfo();
1201 token=AcquireString(
"");
1202 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1203 &breaker,&next,"e);
1204 while (status_token == 0)
1206 raqm_add_font_feature(rq,token,(
int) strlen(token));
1207 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1208 &breaker,&next,"e);
1210 token_info=DestroyTokenInfo(token_info);
1211 token=DestroyString(token);
1213 if (raqm_layout(rq) == 0)
1215 glyphs=raqm_get_glyphs(rq,&extent);
1216 if (glyphs == (raqm_glyph_t *) NULL)
1221 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(extent,
sizeof(**grapheme));
1227 for (i=0; i < (ssize_t) extent; i++)
1229 (*grapheme)[i].index=glyphs[i].index;
1230 (*grapheme)[i].x_offset=glyphs[i].x_offset;
1231 (*grapheme)[i].x_advance=glyphs[i].x_advance;
1232 (*grapheme)[i].y_offset=glyphs[i].y_offset;
1233 (*grapheme)[i].y_advance=glyphs[i].y_advance;
1234 (*grapheme)[i].cluster=glyphs[i].cluster;
1241static size_t ComplexTextLayout(
const DrawInfo *draw_info,
const char *text,
1242 const size_t length,
const FT_Face face,
const FT_Int32 flags,
1257 *grapheme=(
GraphemeInfo *) AcquireQuantumMemory(length+1,
sizeof(**grapheme));
1262 for (i=0; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p), i++)
1264 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,(FT_ULong)
1266 (*grapheme)[i].x_offset=0;
1267 (*grapheme)[i].y_offset=0;
1268 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1270 if (FT_HAS_KERNING(face))
1278 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1279 (*grapheme)[i].index,ft_kerning_default,&kerning);
1281 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1282 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1285 (void) FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1286 (*grapheme)[i].x_advance=face->glyph->advance.x;
1287 (*grapheme)[i].y_advance=face->glyph->advance.y;
1288 (*grapheme)[i].cluster=(size_t) (p-text);
1289 last_glyph=(*grapheme)[i].index;
1295static void FreetypeCloseStream(FT_Stream stream)
1300 file=(FILE *) stream->descriptor.pointer;
1301 if (file != (FILE *) NULL)
1302 (void) fclose(file);
1303 stream->descriptor.pointer=NULL;
1306static unsigned long FreetypeReadStream(FT_Stream stream,
unsigned long offset,
1307 unsigned char *buffer,
unsigned long count)
1315 file=(FILE *) stream->descriptor.pointer;
1316 if (file == (FILE *) NULL)
1318 if (offset > stream->size)
1321 result=(
unsigned long) fseek(file,(off_t) offset,SEEK_SET);
1326 return((
unsigned long) fread(buffer,1,count,file));
1329static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1331 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1335static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1342 path[MagickPathExtent];
1344 affine=draw_info->affine;
1345 (void) FormatLocaleString(path,MagickPathExtent,
"C%g,%g %g,%g %g,%g",
1346 affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-
1347 q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1348 (void) ConcatenateString(&draw_info->primitive,path);
1352static int TraceLineTo(FT_Vector *to,
DrawInfo *draw_info)
1358 path[MagickPathExtent];
1360 affine=draw_info->affine;
1361 (void) FormatLocaleString(path,MagickPathExtent,
"L%g,%g",affine.tx+to->x/64.0,
1362 affine.ty-to->y/64.0);
1363 (void) ConcatenateString(&draw_info->primitive,path);
1367static int TraceMoveTo(FT_Vector *to,
DrawInfo *draw_info)
1373 path[MagickPathExtent];
1375 affine=draw_info->affine;
1376 (void) FormatLocaleString(path,MagickPathExtent,
"M%g,%g",affine.tx+to->x/64.0,
1377 affine.ty-to->y/64.0);
1378 (void) ConcatenateString(&draw_info->primitive,path);
1382static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1389 path[MagickPathExtent];
1391 affine=draw_info->affine;
1392 (void) FormatLocaleString(path,MagickPathExtent,
"Q%g,%g %g,%g",affine.tx+
1393 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1395 (void) ConcatenateString(&draw_info->primitive,path);
1399#if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1400static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1402 return(FT_Error_String(ft_status));
1404static inline const char *FreetypeErrorMessage(
1405 FT_Error magick_unused(ft_status))
1407 magick_unreferenced(ft_status);
1408 return((
const char *) NULL);
1412static void *FreetypeAlloc(FT_Memory magick_unused(memory),
long size)
1414 magick_unreferenced(memory);
1416 return((
void *) NULL);
1417 if ((
size_t) size > GetMaxMemoryRequest())
1418 return((
void *) NULL);
1419 return(AcquireMagickMemory((
size_t) size));
1422static void *FreetypeRealloc(FT_Memory magick_unused(memory),
1423 long magick_unused(cur_size),
long size,
void *block)
1425 magick_unreferenced(memory);
1426 magick_unreferenced(cur_size);
1428 return((
void *) NULL);
1429 if ((
size_t) size > GetMaxMemoryRequest())
1430 return((
void *) NULL);
1431 return(ResizeMagickMemory(block,(
size_t) size));
1434static void FreetypeFree(FT_Memory magick_unused(memory),
void *block)
1436 magick_unreferenced(memory);
1437 (void) RelinquishMagickMemory(block);
1440static FT_Memory FreetypeAcquireMemoryManager(
void)
1445 memory=(FT_Memory) AcquireMagickMemory(
sizeof(*memory));
1446 if (memory == (FT_Memory) NULL)
1448 memset(memory,0,
sizeof(*memory));
1449 memory->alloc=(&FreetypeAlloc);
1450 memory->realloc=(&FreetypeRealloc);
1451 memory->free=(&FreetypeFree);
1455static void FreetypeDone(FT_Memory memory,FT_Library library,
1456 FT_StreamRec *stream)
1458 (void) FT_Done_Library(library);
1459 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1460 memory=(FT_Memory) RelinquishMagickMemory(memory);
1463static FT_Error FreetypeInit(FT_Memory memory,FT_Library *alibrary)
1468 ft_status=FT_New_Library(memory,alibrary);
1470 RelinquishMagickMemory(memory);
1472 FT_Add_Default_Modules(*alibrary);
1473 FT_Set_Default_Properties(*alibrary);
1477static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
1481#if !defined(FT_OPEN_PATHNAME)
1482#define FT_OPEN_PATHNAME ft_open_pathname
1485#define ThrowFreetypeErrorException(tag,ft_status,value) \
1488 *error_string=FreetypeErrorMessage(ft_status); \
1489 if (error_string != (const char *) NULL) \
1490 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1491 tag,"`%s (%s)'",value, error_string); \
1493 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1494 tag,"`%s'",value); \
1497 typedef struct _GlyphInfo
1586 static FT_Outline_Funcs
1589 (FT_Outline_MoveTo_Func) TraceMoveTo,
1590 (FT_Outline_LineTo_Func) TraceLineTo,
1591 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1592 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1605 memory=FreetypeAcquireMemoryManager();
1606 if (memory == (FT_Memory) NULL)
1607 ThrowBinaryException(ResourceLimitError,
"UnableToInitializeFreetypeLibrary",
1609 ft_status=FreetypeInit(memory,&library);
1611 ThrowFreetypeErrorException(
"UnableToInitializeFreetypeLibrary",ft_status,
1616 face_index=(FT_Long) draw_info->face;
1617 (void) memset(&args,0,
sizeof(args));
1618 if (draw_info->font == (
char *) NULL)
1620 const TypeInfo *type_info = GetTypeInfo(
"*",exception);
1621 if (type_info != (
const TypeInfo *) NULL)
1622 args.pathname=ConstantString(type_info->glyphs);
1625 if (*draw_info->font !=
'@')
1626 args.pathname=ConstantString(draw_info->font);
1632 ImageInfo *image_info = AcquireImageInfo();
1633 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1635 (void) SetImageInfo(image_info,0,exception);
1636 face_index=(FT_Long) image_info->scene;
1637 args.pathname=ConstantString(image_info->filename);
1638 image_info=DestroyImageInfo(image_info);
1643 stream=(FT_StreamRec *) AcquireCriticalMemory(
sizeof(*stream));
1644 (void) memset(stream,0,
sizeof(*stream));
1645 if (stat(args.pathname,&attributes) == 0)
1646 stream->size=attributes.st_size >= 0 ? (
unsigned long)
1647 attributes.st_size : 0;
1648 stream->descriptor.pointer=fopen_utf8(args.pathname,
"rb");
1649 stream->read=(&FreetypeReadStream);
1650 stream->close=(&FreetypeCloseStream);
1651 args.flags=FT_OPEN_STREAM;
1653 face=(FT_Face) NULL;
1654 ft_status=FT_Open_Face(library,&args,face_index,&face);
1657 FreetypeDone(memory,library,stream);
1658 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,args.pathname);
1659 args.pathname=DestroyString(args.pathname);
1660 return(MagickFalse);
1662 args.pathname=DestroyString(args.pathname);
1663 if ((draw_info->metrics != (
char *) NULL) &&
1664 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1665 (void) FT_Attach_File(face,draw_info->metrics);
1666 encoding_type=FT_ENCODING_UNICODE;
1667 ft_status=FT_Select_Charmap(face,encoding_type);
1668 if ((ft_status != 0) && (face->num_charmaps != 0))
1669 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1670 if (encoding != (
const char *) NULL)
1672 if (LocaleCompare(encoding,
"AdobeCustom") == 0)
1673 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1674 if (LocaleCompare(encoding,
"AdobeExpert") == 0)
1675 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1676 if (LocaleCompare(encoding,
"AdobeStandard") == 0)
1677 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1678 if (LocaleCompare(encoding,
"AppleRoman") == 0)
1679 encoding_type=FT_ENCODING_APPLE_ROMAN;
1680 if (LocaleCompare(encoding,
"BIG5") == 0)
1681 encoding_type=FT_ENCODING_BIG5;
1682#if defined(FT_ENCODING_PRC)
1683 if (LocaleCompare(encoding,
"GB2312") == 0)
1684 encoding_type=FT_ENCODING_PRC;
1686#if defined(FT_ENCODING_JOHAB)
1687 if (LocaleCompare(encoding,
"Johab") == 0)
1688 encoding_type=FT_ENCODING_JOHAB;
1690#if defined(FT_ENCODING_ADOBE_LATIN_1)
1691 if (LocaleCompare(encoding,
"Latin-1") == 0)
1692 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1694#if defined(FT_ENCODING_ADOBE_LATIN_2)
1695 if (LocaleCompare(encoding,
"Latin-2") == 0)
1696 encoding_type=FT_ENCODING_OLD_LATIN_2;
1698 if (LocaleCompare(encoding,
"None") == 0)
1699 encoding_type=FT_ENCODING_NONE;
1700 if (LocaleCompare(encoding,
"SJIScode") == 0)
1701 encoding_type=FT_ENCODING_SJIS;
1702 if (LocaleCompare(encoding,
"Symbol") == 0)
1703 encoding_type=FT_ENCODING_MS_SYMBOL;
1704 if (LocaleCompare(encoding,
"Unicode") == 0)
1705 encoding_type=FT_ENCODING_UNICODE;
1706 if (LocaleCompare(encoding,
"Wansung") == 0)
1707 encoding_type=FT_ENCODING_WANSUNG;
1708 ft_status=FT_Select_Charmap(face,encoding_type);
1711 (void) FT_Done_Face(face);
1712 FreetypeDone(memory,library,stream);
1713 ThrowFreetypeErrorException(
"UnrecognizedFontEncoding",ft_status,
1715 return(MagickFalse);
1721 resolution.x=DefaultResolution;
1722 resolution.y=DefaultResolution;
1723 if (draw_info->density != (
char *) NULL)
1731 geometry_flags=ParseGeometry(draw_info->density,&geometry_info);
1732 if ((geometry_flags & RhoValue) != 0)
1733 resolution.x=geometry_info.rho;
1734 resolution.y=resolution.x;
1735 if ((geometry_flags & SigmaValue) != 0)
1736 resolution.y=geometry_info.sigma;
1738 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1739 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1740 (FT_UInt) resolution.y);
1743 (void) FT_Done_Face(face);
1744 FreetypeDone(memory,library,stream);
1745 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,
1747 return(MagickFalse);
1749 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1750 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1751 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1752 metrics->descent=(double) face->size->metrics.descender/64.0;
1753 if (face->size->metrics.ascender == 0)
1758 metrics->ascent=face->size->metrics.y_ppem;
1759 if (face->size->metrics.descender == 0)
1760 metrics->descent=face->size->metrics.y_ppem/-3.5;
1763 metrics->origin.x=0;
1764 metrics->origin.y=0;
1765 metrics->height=(double) face->size->metrics.height/64.0;
1766 metrics->max_advance=0.0;
1767 if (face->size->metrics.max_advance > MagickEpsilon)
1768 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1769 metrics->bounds.x1=0.0;
1770 metrics->bounds.y1=metrics->descent;
1771 metrics->bounds.x2=metrics->ascent+metrics->descent;
1772 metrics->bounds.y2=metrics->ascent+metrics->descent;
1773 metrics->underline_position=face->underline_position*
1774 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1775 metrics->underline_thickness=face->underline_thickness*
1776 (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1778 FT_Get_First_Char(face,&first_glyph_id);
1779 if ((draw_info->text == (
char *) NULL) || (*draw_info->text ==
'\0') ||
1780 (first_glyph_id == 0))
1782 (void) FT_Done_Face(face);
1783 FreetypeDone(memory,library,stream);
1789 if (draw_info->debug != MagickFalse)
1790 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Font %s; "
1791 "font-encoding %s; text-encoding %s; pointsize %g",
1792 draw_info->font != (
char *) NULL ? draw_info->font :
"none",
1793 encoding != (
char *) NULL ? encoding :
"none",
1794 draw_info->encoding != (
char *) NULL ? draw_info->encoding :
"none",
1795 draw_info->pointsize);
1796 flags=FT_LOAD_DEFAULT;
1797 if (draw_info->render == MagickFalse)
1798 flags=FT_LOAD_NO_BITMAP;
1799 if (draw_info->text_antialias == MagickFalse)
1800 flags|=FT_LOAD_TARGET_MONO;
1803#if defined(FT_LOAD_TARGET_LIGHT)
1804 flags|=FT_LOAD_TARGET_LIGHT;
1805#elif defined(FT_LOAD_TARGET_LCD)
1806 flags|=FT_LOAD_TARGET_LCD;
1809 value=GetImageProperty(image,
"type:hinting",exception);
1810 if ((value != (
const char *) NULL) && (LocaleCompare(value,
"off") == 0))
1811 flags|=FT_LOAD_NO_HINTING;
1813 glyph.image=(FT_Glyph) NULL;
1821 if (draw_info->render != MagickFalse)
1823 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1824 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1825 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1826 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1828 annotate_info=CloneDrawInfo((
ImageInfo *) NULL,draw_info);
1829 if (annotate_info->dash_pattern != (
double *) NULL)
1830 annotate_info->dash_pattern[0]=0.0;
1831 (void) CloneString(&annotate_info->primitive,
"path '");
1833 if (draw_info->render != MagickFalse)
1835 if (image->storage_class != DirectClass)
1836 (void) SetImageStorageClass(image,DirectClass,exception);
1837 if ((image->alpha_trait & BlendPixelTrait) == 0)
1838 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1840 for (p=draw_info->text; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
1841 if (GetUTFCode(p) < 0)
1843 utf8=(
unsigned char *) NULL;
1844 if (GetUTFCode(p) == 0)
1848 utf8=ConvertLatin1ToUTF8((
unsigned char *) draw_info->text);
1849 if (utf8 != (
unsigned char *) NULL)
1853#if defined(MAGICKCORE_RAQM_DELEGATE)
1854 length=ComplexRaqmTextLayout(image,draw_info,p,strlen(p),face,&grapheme,
1857 length=ComplexTextLayout(draw_info,p,strlen(p),face,flags,&grapheme);
1859 missing_glyph_id=FT_Get_Char_Index(face,
' ');
1861 last_character=(ssize_t) length-1;
1862 for (i=0; i < (ssize_t) length; i++)
1870 glyph.id=(FT_UInt) grapheme[i].index;
1872 glyph.id=missing_glyph_id;
1873 if ((glyph.id != 0) && (last_glyph_id != 0))
1874 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1875 glyph.origin=origin;
1876 glyph.origin.x+=(FT_Pos) grapheme[i].x_offset;
1877 glyph.origin.y+=(FT_Pos) grapheme[i].y_offset;
1878 if (glyph.image != (FT_Glyph) NULL)
1880 FT_Done_Glyph(glyph.image);
1881 glyph.image=(FT_Glyph) NULL;
1883 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1886 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1889 outline=((FT_OutlineGlyph) glyph.image)->outline;
1890 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1891 (IsEmptyOutline(outline) == MagickFalse))
1893 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1896 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1897 metrics->bounds.x1=(double) bounds.xMin;
1898 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1899 metrics->bounds.y1=(double) bounds.yMin;
1900 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1901 metrics->bounds.x2=(double) bounds.xMax;
1902 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1903 metrics->bounds.y2=(double) bounds.yMax;
1904 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
1905 (draw_info->stroke_pattern != (
Image *) NULL)) &&
1906 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1911 annotate_info->affine.tx=glyph.origin.x/64.0;
1912 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1913 if (IsEmptyOutline(outline) == MagickFalse)
1914 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1917 FT_Vector_Transform(&glyph.origin,&affine);
1918 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1919 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1920 (FT_Vector *) NULL,MagickTrue);
1923 bitmap=(FT_BitmapGlyph) glyph.image;
1924 point.x=offset->x+bitmap->left;
1925 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1926 point.x+=(origin.x/64.0);
1927 point.y=offset->y-bitmap->top;
1928 if (draw_info->render != MagickFalse)
1942 transparent_fill=((draw_info->fill.alpha == (MagickRealType) TransparentAlpha) &&
1943 (draw_info->fill_pattern == (
Image *) NULL) &&
1944 (draw_info->stroke.alpha == (MagickRealType) TransparentAlpha) &&
1945 (draw_info->stroke_pattern == (
Image *) NULL)) ? MagickTrue :
1947 r=bitmap->bitmap.buffer;
1948 image_view=AcquireAuthenticCacheView(image,exception);
1949#if defined(MAGICKCORE_OPENMP_SUPPORT)
1950 #pragma omp parallel for schedule(static) shared(status) \
1951 magick_number_threads(image,image,bitmap->bitmap.rows,4)
1953 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1976 if (status == MagickFalse)
1978 x_offset=CastDoubleToLong(ceil(point.x-0.5));
1979 y_offset=CastDoubleToLong(ceil(point.y+y-0.5));
1980 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1983 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1987 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1988 bitmap->bitmap.width,1,exception);
1989 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1991 n=y*bitmap->bitmap.pitch;
1992 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
1994 x_offset=CastDoubleToLong(ceil(point.x+x-0.5));
1995 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1997 if (q != (Quantum *) NULL)
1998 q+=(ptrdiff_t) GetPixelChannels(image);
2002 if (bitmap->bitmap.buffer != (
unsigned char *) NULL)
2004 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
2005 fill_opacity=(double) (r[n])/(bitmap->bitmap.num_grays-1);
2007 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
2008 fill_opacity=((r[(x >> 3)+y*bitmap->bitmap.pitch] &
2009 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
2011 if (draw_info->text_antialias == MagickFalse)
2012 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
2013 if (active == MagickFalse)
2014 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
2016 if (q == (Quantum *) NULL)
2018 if (transparent_fill == MagickFalse)
2020 GetPixelInfo(image,&fill_color);
2021 GetFillColor(draw_info,x_offset,y_offset,&fill_color,exception);
2022 fill_opacity=fill_opacity*fill_color.alpha;
2023 CompositePixelOver(image,&fill_color,fill_opacity,q,
2024 GetPixelAlpha(image,q),q);
2032 Da=1.0-(QuantumScale*(double) GetPixelAlpha(image,q));
2034 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*(
double)
2036 SetPixelAlpha(image,fill_opacity,q);
2038 if (active == MagickFalse)
2040 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2041 if (sync == MagickFalse)
2044 q+=(ptrdiff_t) GetPixelChannels(image);
2046 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2047 if (sync == MagickFalse)
2050 image_view=DestroyCacheView(image_view);
2051 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
2052 (draw_info->stroke_pattern != (
Image *) NULL)) &&
2053 (status != MagickFalse))
2058 annotate_info->linejoin=RoundJoin;
2059 annotate_info->affine.tx=offset->x;
2060 annotate_info->affine.ty=offset->y;
2061 (void) ConcatenateString(&annotate_info->primitive,
"'");
2062 if (strlen(annotate_info->primitive) > 7)
2063 (void) DrawImage(image,annotate_info,exception);
2064 (void) CloneString(&annotate_info->primitive,
"path '");
2067 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
2068 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
2069 (IsUTFSpace(code) == MagickFalse))
2070 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
2072 if (i == last_character)
2073 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
2075 origin.x+=(FT_Pos) grapheme[i].x_advance;
2076 origin.y+=(FT_Pos) grapheme[i].y_advance;
2077 metrics->origin.x=(double) origin.x;
2078 metrics->origin.y=(double) origin.y;
2079 if (metrics->origin.x > metrics->width)
2080 metrics->width=metrics->origin.x;
2081 last_glyph_id=glyph.id;
2082 code=GetUTFCode(p+grapheme[i].cluster);
2085 grapheme=(
GraphemeInfo *) RelinquishMagickMemory(grapheme);
2086 if (utf8 != (
unsigned char *) NULL)
2087 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
2088 if (glyph.image != (FT_Glyph) NULL)
2090 FT_Done_Glyph(glyph.image);
2091 glyph.image=(FT_Glyph) NULL;
2096 metrics->bounds.x1/=64.0;
2097 metrics->bounds.y1/=64.0;
2098 metrics->bounds.x2/=64.0;
2099 metrics->bounds.y2/=64.0;
2100 metrics->origin.x/=64.0;
2101 metrics->origin.y/=64.0;
2102 metrics->width=ceil(metrics->width/64.0);
2106 annotate_info=DestroyDrawInfo(annotate_info);
2107 (void) FT_Done_Face(face);
2108 FreetypeDone(memory,library,stream);
2112static MagickBooleanType RenderFreetype(
Image *image,
const DrawInfo *draw_info,
2113 const char *magick_unused(encoding),
const PointInfo *offset,
2116 magick_unreferenced(encoding);
2117 (void) ThrowMagickException(exception,GetMagickModule(),
2118 MissingDelegateWarning,
"DelegateLibrarySupportNotBuiltIn",
"'%s' (Freetype)",
2119 draw_info->font != (
char *) NULL ? draw_info->font :
"none");
2120 return(RenderPostscript(image,draw_info,offset,metrics,exception));
2157static char *EscapeParenthesis(
const char *source)
2171 assert(source != (
const char *) NULL);
2173 for (p=source; *p !=
'\0'; p++)
2175 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2178 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2183 destination=(
char *) NULL;
2184 if (~length >= (MagickPathExtent-1))
2185 destination=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
2186 sizeof(*destination));
2187 if (destination == (
char *) NULL)
2188 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2191 for (p=source; *p !=
'\0'; p++)
2193 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2198 return(destination);
2201static MagickBooleanType RenderPostscript(
Image *image,
2206 filename[MagickPathExtent],
2207 geometry[MagickPathExtent],
2243 if (draw_info->debug != MagickFalse)
2244 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2245 "Font %s; pointsize %g",draw_info->font != (
char *) NULL ?
2246 draw_info->font :
"none",draw_info->pointsize);
2248 unique_file=AcquireUniqueFileResource(filename);
2249 if (unique_file != -1)
2250 file=fdopen(unique_file,
"wb");
2251 if ((unique_file == -1) || (file == (FILE *) NULL))
2253 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",filename);
2254 return(MagickFalse);
2256 (void) FormatLocaleFile(file,
"%%!PS-Adobe-3.0\n");
2257 (void) FormatLocaleFile(file,
"/ReencodeType\n");
2258 (void) FormatLocaleFile(file,
"{\n");
2259 (void) FormatLocaleFile(file,
" findfont dup length\n");
2260 (void) FormatLocaleFile(file,
2261 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2262 (void) FormatLocaleFile(file,
2263 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2264 (void) FormatLocaleFile(file,
"} bind def\n");
2268 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2269 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2270 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2273 length=strlen(draw_info->text);
2274 for (i=0; i <= (ssize_t) (length+2); i++)
2276 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2277 draw_info->affine.ry*2.0*draw_info->pointsize);
2278 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2279 draw_info->affine.sy*2.0*draw_info->pointsize);
2280 if (point.x > extent.x)
2282 if (point.y > extent.y)
2285 (void) FormatLocaleFile(file,
"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2286 extent.x/2.0,extent.y/2.0);
2287 (void) FormatLocaleFile(file,
"%g %g scale\n",draw_info->pointsize,
2288 draw_info->pointsize);
2289 if ((draw_info->font == (
char *) NULL) || (*draw_info->font ==
'\0') ||
2290 (strchr(draw_info->font,
'/') != (
char *) NULL))
2291 (void) FormatLocaleFile(file,
2292 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2294 (
void) FormatLocaleFile(file,
2295 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2297 (void) FormatLocaleFile(file,
"[%g %g %g %g 0 0] concat\n",
2298 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2299 draw_info->affine.sy);
2300 text=EscapeParenthesis(draw_info->text);
2301 if (identity == MagickFalse)
2302 (void) FormatLocaleFile(file,
"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2304 (void) FormatLocaleFile(file,
"(%s) show\n",text);
2305 text=DestroyString(text);
2306 (void) FormatLocaleFile(file,
"showpage\n");
2307 (void) fclose(file);
2308 (void) FormatLocaleString(geometry,MagickPathExtent,
"%.20gx%.20g+0+0!",
2309 floor(extent.x+0.5),floor(extent.y+0.5));
2310 annotate_info=AcquireImageInfo();
2311 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,
"ps:%s",
2313 (void) CloneString(&annotate_info->page,geometry);
2314 if (draw_info->density != (
char *) NULL)
2315 (void) CloneString(&annotate_info->density,draw_info->density);
2316 annotate_info->antialias=draw_info->text_antialias;
2317 annotate_image=ReadImage(annotate_info,exception);
2318 CatchException(exception);
2319 annotate_info=DestroyImageInfo(annotate_info);
2320 (void) RelinquishUniqueFileResource(filename);
2321 if (annotate_image == (
Image *) NULL)
2322 return(MagickFalse);
2323 (void) NegateImage(annotate_image,MagickFalse,exception);
2324 resolution.x=DefaultResolution;
2325 resolution.y=DefaultResolution;
2326 if (draw_info->density != (
char *) NULL)
2334 flags=ParseGeometry(draw_info->density,&geometry_info);
2335 if ((flags & RhoValue) != 0)
2336 resolution.x=geometry_info.rho;
2337 resolution.y=resolution.x;
2338 if ((flags & SigmaValue) != 0)
2339 resolution.y=geometry_info.sigma;
2341 if (identity == MagickFalse)
2342 (void) TransformImage(&annotate_image,
"0x0",(
char *) NULL,exception);
2348 crop_info=GetImageBoundingBox(annotate_image,exception);
2349 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2350 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2351 crop_info.y=CastDoubleToLong(ceil((resolution.y/DefaultResolution)*
2353 (void) FormatLocaleString(geometry,MagickPathExtent,
2354 "%.20gx%.20g%+.20g%+.20g",(
double) crop_info.width,(
double)
2355 crop_info.height,(
double) crop_info.x,(
double) crop_info.y);
2356 (void) TransformImage(&annotate_image,geometry,(
char *) NULL,exception);
2358 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2359 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2360 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2361 metrics->ascent=metrics->pixels_per_em.y;
2362 metrics->descent=metrics->pixels_per_em.y/-5.0;
2363 metrics->width=(double) annotate_image->columns/
2364 ExpandAffine(&draw_info->affine);
2365 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2366 metrics->max_advance=metrics->pixels_per_em.x;
2367 metrics->bounds.x1=0.0;
2368 metrics->bounds.y1=metrics->descent;
2369 metrics->bounds.x2=metrics->ascent+metrics->descent;
2370 metrics->bounds.y2=metrics->ascent+metrics->descent;
2371 metrics->underline_position=(-2.0);
2372 metrics->underline_thickness=1.0;
2373 if (draw_info->render == MagickFalse)
2375 annotate_image=DestroyImage(annotate_image);
2378 if (draw_info->fill.alpha != (MagickRealType) TransparentAlpha)
2392 if ((image->alpha_trait & BlendPixelTrait) == 0)
2393 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2394 if (annotate_image->alpha_trait == UndefinedPixelTrait)
2395 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
2397 fill_color=draw_info->fill;
2399 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2400#if defined(MAGICKCORE_OPENMP_SUPPORT)
2401 #pragma omp parallel for schedule(static) shared(status) \
2402 magick_number_threads(annotate_image,annotate_image,annotate_image->rows,4)
2404 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2412 if (status == MagickFalse)
2414 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2416 if (q == (Quantum *) NULL)
2421 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2423 GetFillColor(draw_info,x,y,&fill_color,exception);
2424 SetPixelAlpha(annotate_image,ClampToQuantum((((
double) QuantumScale*
2425 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
2426 SetPixelRed(annotate_image,fill_color.red,q);
2427 SetPixelGreen(annotate_image,fill_color.green,q);
2428 SetPixelBlue(annotate_image,fill_color.blue,q);
2429 q+=(ptrdiff_t) GetPixelChannels(annotate_image);
2431 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2432 if (sync == MagickFalse)
2435 annotate_view=DestroyCacheView(annotate_view);
2436 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
2437 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2438 metrics->descent)-0.5),exception);
2440 annotate_image=DestroyImage(annotate_image);
2476static MagickBooleanType RenderX11(
Image *image,
const DrawInfo *draw_info,
2483 ActivateSemaphoreInfo(&annotate_semaphore);
2484 LockSemaphoreInfo(annotate_semaphore);
2485 status=XRenderImage(image,draw_info,offset,metrics,exception);
2486 UnlockSemaphoreInfo(annotate_semaphore);