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
143 RenderType(Image *,
const DrawInfo *,
const PointInfo *,TypeMetric *,
145 RenderPostscript(Image *,
const DrawInfo *,
const PointInfo *,TypeMetric *,
147 RenderFreetype(Image *,
const DrawInfo *,
const char *,
const PointInfo *,
148 TypeMetric *,ExceptionInfo *),
149 RenderX11(Image *,
const DrawInfo *,
const PointInfo *,TypeMetric *,
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,
230 const DrawInfo *draw_info,ExceptionInfo *exception)
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=CastDoubleToSizeT(metrics.ascent-metrics.descent+0.5);
347 height=(size_t) draw_info->pointsize;
348 height=CastDoubleToSizeT(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+
365 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
366 annotate_info->affine.sy*metrics.ascent;
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+
376 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
377 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
381 case (GravityType) NorthEastGravity:
383 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
384 geometry.width+i*annotate_info->affine.ry*height-
385 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
386 (metrics.ascent+metrics.descent)-1.0;
387 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
388 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
389 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
393 case (GravityType) WestGravity:
395 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
396 annotate_info->affine.ry*height+annotate_info->affine.ry*
397 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
398 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
399 geometry.height/2.0+i*annotate_info->affine.sy*height+
400 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
401 (number_lines-1.0)*height)/2.0;
404 case (GravityType) CenterGravity:
406 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
407 geometry.width/2.0+i*annotate_info->affine.ry*height-
408 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
409 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
410 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
411 geometry.height/2.0+i*annotate_info->affine.sy*height-
412 annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
413 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
416 case (GravityType) EastGravity:
418 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
419 geometry.width+i*annotate_info->affine.ry*height-
420 annotate_info->affine.sx*metrics.width+
421 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
422 (number_lines-1.0)*height)/2.0-1.0;
423 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
424 geometry.height/2.0+i*annotate_info->affine.sy*height-
425 annotate_info->affine.rx*metrics.width+
426 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
427 (number_lines-1.0)*height)/2.0;
430 case (GravityType) SouthWestGravity:
432 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
433 annotate_info->affine.ry*height-annotate_info->affine.ry*
434 (number_lines-1.0)*height;
435 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
436 geometry.height+i*annotate_info->affine.sy*height-
437 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
440 case (GravityType) SouthGravity:
442 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
443 geometry.width/2.0+i*annotate_info->affine.ry*height-
444 annotate_info->affine.sx*metrics.width/2.0-
445 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
446 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
447 geometry.height+i*annotate_info->affine.sy*height-
448 annotate_info->affine.rx*metrics.width/2.0-
449 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
452 case (GravityType) SouthEastGravity:
454 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
455 geometry.width+i*annotate_info->affine.ry*height-
456 annotate_info->affine.sx*metrics.width-
457 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
458 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
459 geometry.height+i*annotate_info->affine.sy*height-
460 annotate_info->affine.rx*metrics.width-
461 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
465 switch (annotate->align)
469 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
470 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
475 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
476 annotate_info->affine.sx*metrics.width/2.0;
477 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
478 annotate_info->affine.rx*metrics.width/2.0;
483 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
484 annotate_info->affine.sx*metrics.width;
485 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
486 annotate_info->affine.rx*metrics.width;
492 if (draw_info->undercolor.alpha != (MagickRealType) TransparentAlpha)
500 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
501 undercolor_info->fill=draw_info->undercolor;
502 undercolor_info->affine=draw_info->affine;
503 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
504 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
505 (void) FormatLocaleString(primitive,MagickPathExtent,
506 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(
double) height);
507 (void) CloneString(&undercolor_info->primitive,primitive);
508 (void) DrawImage(image,undercolor_info,exception);
509 (void) DestroyDrawInfo(undercolor_info);
511 annotate_info->affine.tx=offset.x;
512 annotate_info->affine.ty=offset.y;
513 pixel=annotate_info->fill;
514 if (annotate_info->stroke.alpha != (MagickRealType) TransparentAlpha)
515 pixel=annotate_info->stroke;
516 (void) QueryColorname(image,&pixel,AllCompliance,color,exception);
517 (void) FormatLocaleString(primitive,MagickPathExtent,
"stroke %s "
518 "stroke-width %g line 0,0 %g,0",color,(
double)
519 metrics.underline_thickness,(
double) metrics.width);
523 switch (annotate->decorate)
525 case OverlineDecoration:
527 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
528 metrics.descent-metrics.underline_position));
529 (void) CloneString(&annotate_info->primitive,primitive);
530 (void) DrawImage(image,annotate_info,exception);
533 case UnderlineDecoration:
535 annotate_info->affine.ty-=(draw_info->affine.sy*
536 metrics.underline_position);
537 (void) CloneString(&annotate_info->primitive,primitive);
538 (void) DrawImage(image,annotate_info,exception);
544 status=RenderType(image,annotate,&offset,&metrics,exception);
545 if (status == MagickFalse)
548 if (annotate->decorate == LineThroughDecoration) {
549 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
550 metrics.underline_position+metrics.descent*2)/2.0);
551 (void) CloneString(&annotate_info->primitive,primitive);
552 (void) DrawImage(image,annotate_info,exception);
558 annotate_info=DestroyDrawInfo(annotate_info);
559 annotate=DestroyDrawInfo(annotate);
560 textlist=(
char **) RelinquishMagickMemory(textlist);
561 text=DestroyString(text);
601static inline char *ReplaceSpaceWithNewline(
char **caption,
char *space)
606 if ((caption == (
char **) NULL) || (space == (
char *) NULL))
607 return((
char *) NULL);
608 octets=(size_t) GetUTFOctets(space);
622 length=strlen(*caption);
624 offset=space-(*caption);
627 target=AcquireString(*caption);
628 (void) CopyMagickString(target,*caption,(
size_t) offset+2);
629 (void) ConcatenateMagickString(target,space+octets,length);
630 (void) DestroyString(*caption);
632 space=(*caption)+offset;
638MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
639 const MagickBooleanType split,TypeMetric *metrics,
char **caption,
640 ExceptionInfo *exception)
660 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
671 if ((draw_info->word_break != BreakWordBreakType) &&
672 (IsUTFSpace(code) != MagickFalse) &&
673 (IsNonBreakingUTFSpace(code) == MagickFalse))
675 if (width > image->columns)
676 p=ReplaceSpaceWithNewline(caption,p);
679 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
682 status=GetTypeMetrics(image,draw_info,metrics,exception);
683 if (status == MagickFalse)
685 width=CastDoubleToSizeT(metrics->width+draw_info->stroke_width+0.5);
686 if (width <= image->columns)
688 if (s != (
char *) NULL)
689 p=ReplaceSpaceWithNewline(caption,s);
691 if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
697 if ((n > 0) && ((*caption)[n-1] !=
'\n'))
702 target=AcquireString(*caption);
703 (void) CopyMagickString(target,*caption,(
size_t) n+1);
704 (void) ConcatenateMagickString(target,
"\n",strlen(*caption)+1);
705 (void) ConcatenateMagickString(target,p,strlen(*caption)+2);
706 (void) DestroyString(*caption);
715 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
716 if (GetUTFCode(p) ==
'\n')
770MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
771 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
797 assert(image != (Image *) NULL);
798 assert(image->signature == MagickCoreSignature);
799 if (IsEventLogging() != MagickFalse)
800 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
801 assert(draw_info != (DrawInfo *) NULL);
802 assert(draw_info->text != (
char *) NULL);
803 assert(draw_info->signature == MagickCoreSignature);
804 if (*draw_info->text ==
'\0')
806 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
807 "LabelExpected",
"`%s'",image->filename);
810 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
811 annotate_info->text=DestroyString(annotate_info->text);
815 textlist=StringToStrings(draw_info->text,&count);
816 if (textlist == (
char **) NULL)
818 annotate_info=DestroyDrawInfo(annotate_info);
821 annotate_info->render=MagickFalse;
822 annotate_info->direction=UndefinedDirection;
823 (void) memset(metrics,0,
sizeof(*metrics));
824 (void) memset(&extent,0,
sizeof(extent));
828 annotate_info->text=textlist[0];
829 status=GetTypeMetrics(image,annotate_info,&extent,exception);
831 height=(count*(size_t) (metrics->ascent-metrics->descent+
832 0.5)+(count-1)*draw_info->interline_spacing);
833 size=(MagickSizeType) fabs(height);
834 if (AcquireMagickResource(HeightResource,size) == MagickFalse)
836 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
837 "WidthOrHeightExceedsLimit",
"`%s'",image->filename);
842 for (i=1; i < (ssize_t) count; i++)
844 annotate_info->text=textlist[i];
845 status=GetTypeMetrics(image,annotate_info,&extent,exception);
846 if (status == MagickFalse)
848 if (extent.width > metrics->width)
850 size=(MagickSizeType) fabs(extent.width);
851 if (AcquireMagickResource(WidthResource,size) == MagickFalse)
853 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
854 "WidthOrHeightExceedsLimit",
"`%s'",image->filename);
859 metrics->height=(double) height;
864 annotate_info->text=(
char *) NULL;
865 annotate_info=DestroyDrawInfo(annotate_info);
866 for (i=0; i < (ssize_t) count; i++)
867 textlist[i]=DestroyString(textlist[i]);
868 textlist=(
char **) RelinquishMagickMemory(textlist);
918MagickExport MagickBooleanType GetTypeMetrics(Image *image,
919 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
930 assert(image != (Image *) NULL);
931 assert(image->signature == MagickCoreSignature);
932 if (IsEventLogging() != MagickFalse)
933 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
934 assert(draw_info != (DrawInfo *) NULL);
935 assert(draw_info->text != (
char *) NULL);
936 assert(draw_info->signature == MagickCoreSignature);
937 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
938 annotate_info->render=MagickFalse;
939 annotate_info->direction=UndefinedDirection;
940 (void) memset(metrics,0,
sizeof(*metrics));
943 status=RenderType(image,annotate_info,&offset,metrics,exception);
944 if (draw_info->debug != MagickFalse)
945 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Metrics: text: %s; "
946 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
947 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
948 "underline position: %g; underline thickness: %g",annotate_info->text,
949 metrics->width,metrics->height,metrics->ascent,metrics->descent,
950 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
951 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
952 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
953 metrics->underline_position,metrics->underline_thickness);
954 annotate_info=DestroyDrawInfo(annotate_info);
990static MagickBooleanType RenderType(Image *image,
const DrawInfo *draw_info,
991 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1005 type_info=(
const TypeInfo *) NULL;
1006 if (draw_info->font != (
char *) NULL)
1008 if (*draw_info->font ==
'@')
1010 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
1014 if (*draw_info->font ==
'-')
1015 return(RenderX11(image,draw_info,offset,metrics,exception));
1016 if (*draw_info->font ==
'^')
1017 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1018 if (IsPathAccessible(draw_info->font) != MagickFalse)
1020 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
1024 type_info=GetTypeInfo(draw_info->font,exception);
1025 if (type_info == (
const TypeInfo *) NULL)
1026 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1027 "UnableToReadFont",
"`%s'",draw_info->font);
1029 if ((type_info == (
const TypeInfo *) NULL) &&
1030 (draw_info->family != (
const char *) NULL))
1032 if (strpbrk(draw_info->family,
",'\"") == (
char *) NULL)
1033 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
1034 draw_info->stretch,draw_info->weight,exception);
1035 if (type_info == (
const TypeInfo *) NULL)
1049 family=StringToArgv(draw_info->family,&number_families);
1050 for (i=1; i < (ssize_t) number_families; i++)
1052 (void) SubstituteString(&family[i],
",",
"");
1053 type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1054 draw_info->stretch,draw_info->weight,exception);
1055 if ((type_info != (
const TypeInfo *) NULL) &&
1056 (LocaleCompare(family[i],type_info->family) == 0))
1059 for (i=0; i < (ssize_t) number_families; i++)
1060 family[i]=DestroyString(family[i]);
1061 family=(
char **) RelinquishMagickMemory(family);
1062 if (type_info == (
const TypeInfo *) NULL)
1063 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1064 "UnableToReadFont",
"`%s'",draw_info->family);
1067 font=GetPolicyValue(
"system:font");
1068 if (font != (
char *) NULL)
1070 if (IsPathAccessible(font) != MagickFalse)
1075 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1076 annotate_info->font=font;
1077 status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1078 offset,metrics,exception);
1079 annotate_info=DestroyDrawInfo(annotate_info);
1082 font=DestroyString(font);
1084 if (type_info == (
const TypeInfo *) NULL)
1092 sans_exception=AcquireExceptionInfo();
1093 if (type_info == (
const TypeInfo *) NULL)
1094 type_info=GetTypeInfoByFamily((
const char *) NULL,draw_info->style,
1095 draw_info->stretch,draw_info->weight,sans_exception);
1096 if (type_info == (
const TypeInfo *) NULL)
1097 type_info=GetTypeInfo(
"*",sans_exception);
1098 sans_exception=DestroyExceptionInfo(sans_exception);
1100 if (type_info == (
const TypeInfo *) NULL)
1102 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
1106 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1107 annotate_info->face=type_info->face;
1108 if (type_info->metrics != (
char *) NULL)
1109 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1110 if (type_info->glyphs != (
char *) NULL)
1111 (void) CloneString(&annotate_info->font,type_info->glyphs);
1112 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
1114 annotate_info=DestroyDrawInfo(annotate_info);
1154#if defined(MAGICKCORE_FREETYPE_DELEGATE)
1156#if defined(MAGICKCORE_RAQM_DELEGATE)
1157static size_t ComplexRaqmTextLayout(
const Image *image,
1158 const DrawInfo *draw_info,
const char *text,
const size_t length,
1159 const FT_Face face,GraphemeInfo **grapheme,ExceptionInfo *exception)
1178 if (rq == (raqm_t *) NULL)
1180 if (raqm_set_text_utf8(rq,text,length) == 0)
1182 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1184 if (raqm_set_freetype_face(rq,face) == 0)
1186 features=GetImageProperty(image,
"type:features",exception);
1187 if (features != (
const char *) NULL)
1202 token_info=AcquireTokenInfo();
1203 token=AcquireString(
"");
1204 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1205 &breaker,&next,"e);
1206 while (status_token == 0)
1208 raqm_add_font_feature(rq,token,(
int) strlen(token));
1209 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1210 &breaker,&next,"e);
1212 token_info=DestroyTokenInfo(token_info);
1213 token=DestroyString(token);
1215 if (raqm_layout(rq) == 0)
1217 glyphs=raqm_get_glyphs(rq,&extent);
1218 if (glyphs == (raqm_glyph_t *) NULL)
1223 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(extent,
sizeof(**grapheme));
1224 if (*grapheme == (GraphemeInfo *) NULL)
1229 for (i=0; i < (ssize_t) extent; i++)
1231 (*grapheme)[i].index=glyphs[i].index;
1232 (*grapheme)[i].x_offset=glyphs[i].x_offset;
1233 (*grapheme)[i].x_advance=glyphs[i].x_advance;
1234 (*grapheme)[i].y_offset=glyphs[i].y_offset;
1235 (*grapheme)[i].y_advance=glyphs[i].y_advance;
1236 (*grapheme)[i].cluster=glyphs[i].cluster;
1243static size_t ComplexTextLayout(
const DrawInfo *draw_info,
const char *text,
1244 const size_t length,
const FT_Face face,
const FT_Int32 flags,
1245 GraphemeInfo **grapheme)
1259 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(length+1,
sizeof(**grapheme));
1260 if (*grapheme == (GraphemeInfo *) NULL)
1264 for (i=0; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p), i++)
1266 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,(FT_ULong)
1268 (*grapheme)[i].x_offset=0;
1269 (*grapheme)[i].y_offset=0;
1270 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1272 if (FT_HAS_KERNING(face))
1280 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1281 (*grapheme)[i].index,ft_kerning_default,&kerning);
1283 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1284 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1287 (void) FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1288 (*grapheme)[i].x_advance=face->glyph->advance.x;
1289 (*grapheme)[i].y_advance=face->glyph->advance.y;
1290 (*grapheme)[i].cluster=(size_t) (p-text);
1291 last_glyph=(*grapheme)[i].index;
1297static void FreetypeCloseStream(FT_Stream stream)
1302 file=(FILE *) stream->descriptor.pointer;
1303 if (file != (FILE *) NULL)
1304 (void) fclose(file);
1305 stream->descriptor.pointer=NULL;
1308static unsigned long FreetypeReadStream(FT_Stream stream,
unsigned long offset,
1309 unsigned char *buffer,
unsigned long count)
1317 file=(FILE *) stream->descriptor.pointer;
1318 if (file == (FILE *) NULL)
1320 if (offset > stream->size)
1323 result=(
unsigned long) fseek(file,(off_t) offset,SEEK_SET);
1328 return((
unsigned long) fread(buffer,1,count,file));
1331static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1333 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1337static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1338 DrawInfo *draw_info)
1344 path[MagickPathExtent];
1346 affine=draw_info->affine;
1347 (void) FormatLocaleString(path,MagickPathExtent,
"C%g,%g %g,%g %g,%g",
1348 affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-
1349 q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1350 (void) ConcatenateString(&draw_info->primitive,path);
1354static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1360 path[MagickPathExtent];
1362 affine=draw_info->affine;
1363 (void) FormatLocaleString(path,MagickPathExtent,
"L%g,%g",affine.tx+to->x/64.0,
1364 affine.ty-to->y/64.0);
1365 (void) ConcatenateString(&draw_info->primitive,path);
1369static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1375 path[MagickPathExtent];
1377 affine=draw_info->affine;
1378 (void) FormatLocaleString(path,MagickPathExtent,
"M%g,%g",affine.tx+to->x/64.0,
1379 affine.ty-to->y/64.0);
1380 (void) ConcatenateString(&draw_info->primitive,path);
1384static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1385 DrawInfo *draw_info)
1391 path[MagickPathExtent];
1393 affine=draw_info->affine;
1394 (void) FormatLocaleString(path,MagickPathExtent,
"Q%g,%g %g,%g",affine.tx+
1395 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1397 (void) ConcatenateString(&draw_info->primitive,path);
1401#if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1402static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1404 return(FT_Error_String(ft_status));
1406static inline const char *FreetypeErrorMessage(
1407 FT_Error magick_unused(ft_status))
1409 magick_unreferenced(ft_status);
1410 return((
const char *) NULL);
1414static void *FreetypeAlloc(FT_Memory magick_unused(memory),
long size)
1416 magick_unreferenced(memory);
1418 return((
void *) NULL);
1419 if ((
size_t) size > GetMaxMemoryRequest())
1420 return((
void *) NULL);
1421 return(AcquireMagickMemory((
size_t) size));
1424static void *FreetypeRealloc(FT_Memory magick_unused(memory),
1425 long magick_unused(cur_size),
long size,
void *block)
1427 magick_unreferenced(memory);
1428 magick_unreferenced(cur_size);
1430 return((
void *) NULL);
1431 if ((
size_t) size > GetMaxMemoryRequest())
1432 return((
void *) NULL);
1433 return(ResizeMagickMemory(block,(
size_t) size));
1436static void FreetypeFree(FT_Memory magick_unused(memory),
void *block)
1438 magick_unreferenced(memory);
1439 (void) RelinquishMagickMemory(block);
1442static FT_Memory FreetypeAcquireMemoryManager(
void)
1447 memory=(FT_Memory) AcquireMagickMemory(
sizeof(*memory));
1448 if (memory == (FT_Memory) NULL)
1450 memset(memory,0,
sizeof(*memory));
1451 memory->alloc=(&FreetypeAlloc);
1452 memory->realloc=(&FreetypeRealloc);
1453 memory->free=(&FreetypeFree);
1457static void FreetypeDone(FT_Memory memory,FT_Library library,
1458 FT_StreamRec *stream)
1460 (void) FT_Done_Library(library);
1461 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1462 memory=(FT_Memory) RelinquishMagickMemory(memory);
1465static FT_Error FreetypeInit(FT_Memory memory,FT_Library *alibrary)
1470 ft_status=FT_New_Library(memory,alibrary);
1472 RelinquishMagickMemory(memory);
1474 FT_Add_Default_Modules(*alibrary);
1475 FT_Set_Default_Properties(*alibrary);
1479static MagickBooleanType RenderFreetype(Image *image,
const DrawInfo *draw_info,
1480 const char *encoding,
const PointInfo *offset,TypeMetric *metrics,
1481 ExceptionInfo *exception)
1483#if !defined(FT_OPEN_PATHNAME)
1484#define FT_OPEN_PATHNAME ft_open_pathname
1487#define ThrowFreetypeErrorException(tag,ft_status,value) \
1489 const char *error_string = FreetypeErrorMessage(ft_status); \
1490 if (error_string != (const char *) NULL) \
1491 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1492 tag,"`%s (%s)'",value, error_string); \
1494 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1495 tag,"`%s'",value); \
1498 typedef struct _GlyphInfo
1587 static FT_Outline_Funcs
1590 (FT_Outline_MoveTo_Func) TraceMoveTo,
1591 (FT_Outline_LineTo_Func) TraceLineTo,
1592 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1593 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1606 if ((draw_info->font != (
char *) NULL) && (*draw_info->font ==
'@') &&
1607 (IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,draw_info->font) == MagickFalse))
1608 ThrowPolicyException(draw_info->font,MagickFalse);
1609 memory=FreetypeAcquireMemoryManager();
1610 if (memory == (FT_Memory) NULL)
1611 ThrowBinaryException(ResourceLimitError,
"UnableToInitializeFreetypeLibrary",
1613 ft_status=FreetypeInit(memory,&library);
1616 ThrowFreetypeErrorException(
"UnableToInitializeFreetypeLibrary",ft_status,
1618 return(MagickFalse);
1623 face_index=(FT_Long) draw_info->face;
1624 (void) memset(&args,0,
sizeof(args));
1625 if (draw_info->font == (
char *) NULL)
1627 const TypeInfo *type_info = GetTypeInfo(
"*",exception);
1628 if (type_info != (
const TypeInfo *) NULL)
1629 args.pathname=ConstantString(type_info->glyphs);
1632 if (*draw_info->font !=
'@')
1633 args.pathname=ConstantString(draw_info->font);
1639 ImageInfo *image_info = AcquireImageInfo();
1640 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1642 (void) SetImageInfo(image_info,0,exception);
1643 face_index=(FT_Long) image_info->scene;
1644 args.pathname=ConstantString(image_info->filename);
1645 image_info=DestroyImageInfo(image_info);
1650 stream=(FT_StreamRec *) AcquireCriticalMemory(
sizeof(*stream));
1651 (void) memset(stream,0,
sizeof(*stream));
1652 if (stat(args.pathname,&attributes) == 0)
1653 stream->size=attributes.st_size >= 0 ? (
unsigned long)
1654 attributes.st_size : 0;
1655 stream->descriptor.pointer=fopen_utf8(args.pathname,
"rb");
1656 stream->read=(&FreetypeReadStream);
1657 stream->close=(&FreetypeCloseStream);
1658 args.flags=FT_OPEN_STREAM;
1660 face=(FT_Face) NULL;
1661 ft_status=FT_Open_Face(library,&args,face_index,&face);
1664 FreetypeDone(memory,library,stream);
1665 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,args.pathname);
1666 args.pathname=DestroyString(args.pathname);
1667 return(MagickFalse);
1669 args.pathname=DestroyString(args.pathname);
1670 if ((draw_info->metrics != (
char *) NULL) &&
1671 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1672 (void) FT_Attach_File(face,draw_info->metrics);
1673 encoding_type=FT_ENCODING_UNICODE;
1674 ft_status=FT_Select_Charmap(face,encoding_type);
1675 if ((ft_status != 0) && (face->num_charmaps != 0))
1676 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1677 if (encoding != (
const char *) NULL)
1679 if (LocaleCompare(encoding,
"AdobeCustom") == 0)
1680 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1681 if (LocaleCompare(encoding,
"AdobeExpert") == 0)
1682 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1683 if (LocaleCompare(encoding,
"AdobeStandard") == 0)
1684 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1685 if (LocaleCompare(encoding,
"AppleRoman") == 0)
1686 encoding_type=FT_ENCODING_APPLE_ROMAN;
1687 if (LocaleCompare(encoding,
"BIG5") == 0)
1688 encoding_type=FT_ENCODING_BIG5;
1689#if defined(FT_ENCODING_PRC)
1690 if (LocaleCompare(encoding,
"GB2312") == 0)
1691 encoding_type=FT_ENCODING_PRC;
1693#if defined(FT_ENCODING_JOHAB)
1694 if (LocaleCompare(encoding,
"Johab") == 0)
1695 encoding_type=FT_ENCODING_JOHAB;
1697#if defined(FT_ENCODING_ADOBE_LATIN_1)
1698 if (LocaleCompare(encoding,
"Latin-1") == 0)
1699 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1701#if defined(FT_ENCODING_ADOBE_LATIN_2)
1702 if (LocaleCompare(encoding,
"Latin-2") == 0)
1703 encoding_type=FT_ENCODING_OLD_LATIN_2;
1705 if (LocaleCompare(encoding,
"None") == 0)
1706 encoding_type=FT_ENCODING_NONE;
1707 if (LocaleCompare(encoding,
"SJIScode") == 0)
1708 encoding_type=FT_ENCODING_SJIS;
1709 if (LocaleCompare(encoding,
"Symbol") == 0)
1710 encoding_type=FT_ENCODING_MS_SYMBOL;
1711 if (LocaleCompare(encoding,
"Unicode") == 0)
1712 encoding_type=FT_ENCODING_UNICODE;
1713 if (LocaleCompare(encoding,
"Wansung") == 0)
1714 encoding_type=FT_ENCODING_WANSUNG;
1715 ft_status=FT_Select_Charmap(face,encoding_type);
1718 (void) FT_Done_Face(face);
1719 FreetypeDone(memory,library,stream);
1720 ThrowFreetypeErrorException(
"UnrecognizedFontEncoding",ft_status,
1722 return(MagickFalse);
1728 resolution.x=DefaultResolution;
1729 resolution.y=DefaultResolution;
1730 if (draw_info->density != (
char *) NULL)
1738 geometry_flags=ParseGeometry(draw_info->density,&geometry_info);
1739 if ((geometry_flags & RhoValue) != 0)
1740 resolution.x=geometry_info.rho;
1741 resolution.y=resolution.x;
1742 if ((geometry_flags & SigmaValue) != 0)
1743 resolution.y=geometry_info.sigma;
1745 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1746 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1747 (FT_UInt) resolution.y);
1750 (void) FT_Done_Face(face);
1751 FreetypeDone(memory,library,stream);
1752 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,
1754 return(MagickFalse);
1756 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1757 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1758 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1759 metrics->descent=(double) face->size->metrics.descender/64.0;
1760 if (face->size->metrics.ascender == 0)
1765 metrics->ascent=face->size->metrics.y_ppem;
1766 if (face->size->metrics.descender == 0)
1767 metrics->descent=face->size->metrics.y_ppem/-3.5;
1770 metrics->origin.x=0;
1771 metrics->origin.y=0;
1772 metrics->height=(double) face->size->metrics.height/64.0;
1773 metrics->max_advance=0.0;
1774 if (face->size->metrics.max_advance > MagickEpsilon)
1775 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1776 metrics->bounds.x1=0.0;
1777 metrics->bounds.y1=metrics->descent;
1778 metrics->bounds.x2=metrics->ascent+metrics->descent;
1779 metrics->bounds.y2=metrics->ascent+metrics->descent;
1780 metrics->underline_position=face->underline_position*
1781 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1782 metrics->underline_thickness=face->underline_thickness*
1783 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1785 FT_Get_First_Char(face,&first_glyph_id);
1786 if ((draw_info->text == (
char *) NULL) || (*draw_info->text ==
'\0') ||
1787 (first_glyph_id == 0))
1789 (void) FT_Done_Face(face);
1790 FreetypeDone(memory,library,stream);
1796 if (draw_info->debug != MagickFalse)
1797 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Font %s; "
1798 "font-encoding %s; text-encoding %s; pointsize %g",
1799 draw_info->font != (
char *) NULL ? draw_info->font :
"none",
1800 encoding != (
char *) NULL ? encoding :
"none",
1801 draw_info->encoding != (
char *) NULL ? draw_info->encoding :
"none",
1802 draw_info->pointsize);
1803 flags=FT_LOAD_DEFAULT;
1804 if (draw_info->render == MagickFalse)
1805 flags=FT_LOAD_NO_BITMAP;
1806 if (draw_info->text_antialias == MagickFalse)
1807 flags|=FT_LOAD_TARGET_MONO;
1810#if defined(FT_LOAD_TARGET_LIGHT)
1811 flags|=FT_LOAD_TARGET_LIGHT;
1812#elif defined(FT_LOAD_TARGET_LCD)
1813 flags|=FT_LOAD_TARGET_LCD;
1816 value=GetImageProperty(image,
"type:hinting",exception);
1817 if ((value != (
const char *) NULL) && (LocaleCompare(value,
"off") == 0))
1818 flags|=FT_LOAD_NO_HINTING;
1820 glyph.image=(FT_Glyph) NULL;
1828 if (draw_info->render != MagickFalse)
1830 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1831 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1832 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1833 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1835 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1836 if (annotate_info->dash_pattern != (
double *) NULL)
1837 annotate_info->dash_pattern[0]=0.0;
1838 (void) CloneString(&annotate_info->primitive,
"path '");
1840 if (draw_info->render != MagickFalse)
1842 if (image->storage_class != DirectClass)
1843 (void) SetImageStorageClass(image,DirectClass,exception);
1844 if ((image->alpha_trait & BlendPixelTrait) == 0)
1845 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1847 for (p=draw_info->text; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
1848 if (GetUTFCode(p) < 0)
1850 utf8=(
unsigned char *) NULL;
1851 if (GetUTFCode(p) == 0)
1855 utf8=ConvertLatin1ToUTF8((
unsigned char *) draw_info->text);
1856 if (utf8 != (
unsigned char *) NULL)
1859 grapheme=(GraphemeInfo *) NULL;
1860#if defined(MAGICKCORE_RAQM_DELEGATE)
1861 length=ComplexRaqmTextLayout(image,draw_info,p,strlen(p),face,&grapheme,
1864 length=ComplexTextLayout(draw_info,p,strlen(p),face,flags,&grapheme);
1866 missing_glyph_id=FT_Get_Char_Index(face,
' ');
1868 last_character=(ssize_t) length-1;
1869 for (i=0; i < (ssize_t) length; i++)
1877 glyph.id=(FT_UInt) grapheme[i].index;
1879 glyph.id=missing_glyph_id;
1880 if ((glyph.id != 0) && (last_glyph_id != 0))
1881 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1882 glyph.origin=origin;
1883 glyph.origin.x+=(FT_Pos) grapheme[i].x_offset;
1884 glyph.origin.y+=(FT_Pos) grapheme[i].y_offset;
1885 if (glyph.image != (FT_Glyph) NULL)
1887 FT_Done_Glyph(glyph.image);
1888 glyph.image=(FT_Glyph) NULL;
1890 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1893 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1896 outline=((FT_OutlineGlyph) glyph.image)->outline;
1897 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1898 (IsEmptyOutline(outline) == MagickFalse))
1900 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1903 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1904 metrics->bounds.x1=(double) bounds.xMin;
1905 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1906 metrics->bounds.y1=(double) bounds.yMin;
1907 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1908 metrics->bounds.x2=(double) bounds.xMax;
1909 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1910 metrics->bounds.y2=(double) bounds.yMax;
1911 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
1912 (draw_info->stroke_pattern != (Image *) NULL)) &&
1913 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1918 annotate_info->affine.tx=glyph.origin.x/64.0;
1919 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1920 if (IsEmptyOutline(outline) == MagickFalse)
1921 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1924 FT_Vector_Transform(&glyph.origin,&affine);
1925 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1926 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1927 (FT_Vector *) NULL,MagickTrue);
1930 bitmap=(FT_BitmapGlyph) glyph.image;
1931 point.x=offset->x+bitmap->left;
1932 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1933 point.x+=(origin.x/64.0);
1934 point.y=offset->y-bitmap->top;
1935 if (draw_info->render != MagickFalse)
1949 transparent_fill=((draw_info->fill.alpha == (MagickRealType) TransparentAlpha) &&
1950 (draw_info->fill_pattern == (Image *) NULL) &&
1951 (draw_info->stroke.alpha == (MagickRealType) TransparentAlpha) &&
1952 (draw_info->stroke_pattern == (Image *) NULL)) ? MagickTrue :
1954 r=bitmap->bitmap.buffer;
1955 image_view=AcquireAuthenticCacheView(image,exception);
1956#if defined(MAGICKCORE_OPENMP_SUPPORT)
1957 #pragma omp parallel for schedule(static) shared(status) \
1958 magick_number_threads(image,image,bitmap->bitmap.rows,4)
1960 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1983 if (status == MagickFalse)
1985 x_offset=CastDoubleToSsizeT(ceil(point.x-0.5));
1986 y_offset=CastDoubleToSsizeT(ceil(point.y+y-0.5));
1987 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1990 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1994 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1995 bitmap->bitmap.width,1,exception);
1996 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1998 n=y*bitmap->bitmap.pitch;
1999 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
2001 x_offset=CastDoubleToSsizeT(ceil(point.x+x-0.5));
2002 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
2004 if (q != (Quantum *) NULL)
2005 q+=(ptrdiff_t) GetPixelChannels(image);
2009 if (bitmap->bitmap.buffer != (
unsigned char *) NULL)
2011 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
2012 fill_opacity=(double) (r[n])/(bitmap->bitmap.num_grays-1);
2014 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
2015 fill_opacity=((r[(x >> 3)+y*bitmap->bitmap.pitch] &
2016 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
2018 if (draw_info->text_antialias == MagickFalse)
2019 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
2020 if (active == MagickFalse)
2021 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
2023 if (q == (Quantum *) NULL)
2025 if (transparent_fill == MagickFalse)
2027 GetPixelInfo(image,&fill_color);
2028 GetFillColor(draw_info,x_offset,y_offset,&fill_color,exception);
2029 fill_opacity=fill_opacity*fill_color.alpha;
2030 CompositePixelOver(image,&fill_color,fill_opacity,q,
2031 GetPixelAlpha(image,q),q);
2039 Da=1.0-(QuantumScale*(double) GetPixelAlpha(image,q));
2041 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*(
double)
2043 SetPixelAlpha(image,(
const Quantum) fill_opacity,q);
2045 if (active == MagickFalse)
2047 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2048 if (sync == MagickFalse)
2051 q+=(ptrdiff_t) GetPixelChannels(image);
2053 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2054 if (sync == MagickFalse)
2057 image_view=DestroyCacheView(image_view);
2058 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
2059 (draw_info->stroke_pattern != (Image *) NULL)) &&
2060 (status != MagickFalse))
2065 annotate_info->linejoin=RoundJoin;
2066 annotate_info->affine.tx=offset->x;
2067 annotate_info->affine.ty=offset->y;
2068 (void) ConcatenateString(&annotate_info->primitive,
"'");
2069 if (strlen(annotate_info->primitive) > 7)
2070 (void) DrawImage(image,annotate_info,exception);
2071 (void) CloneString(&annotate_info->primitive,
"path '");
2074 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
2075 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
2076 (IsUTFSpace((
int) code) == MagickFalse))
2077 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
2079 if (i == last_character)
2080 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
2082 origin.x+=(FT_Pos) grapheme[i].x_advance;
2083 origin.y+=(FT_Pos) grapheme[i].y_advance;
2084 metrics->origin.x=(double) origin.x;
2085 metrics->origin.y=(double) origin.y;
2086 if (metrics->origin.x > metrics->width)
2087 metrics->width=metrics->origin.x;
2088 last_glyph_id=glyph.id;
2089 code=GetUTFCode(p+grapheme[i].cluster);
2091 if (grapheme != (GraphemeInfo *) NULL)
2092 grapheme=(GraphemeInfo *) RelinquishMagickMemory(grapheme);
2093 if (utf8 != (
unsigned char *) NULL)
2094 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
2095 if (glyph.image != (FT_Glyph) NULL)
2097 FT_Done_Glyph(glyph.image);
2098 glyph.image=(FT_Glyph) NULL;
2103 metrics->bounds.x1/=64.0;
2104 metrics->bounds.y1/=64.0;
2105 metrics->bounds.x2/=64.0;
2106 metrics->bounds.y2/=64.0;
2107 metrics->origin.x/=64.0;
2108 metrics->origin.y/=64.0;
2109 metrics->width=ceil(metrics->width/64.0);
2113 annotate_info=DestroyDrawInfo(annotate_info);
2114 (void) FT_Done_Face(face);
2115 FreetypeDone(memory,library,stream);
2119static MagickBooleanType RenderFreetype(Image *image,
const DrawInfo *draw_info,
2120 const char *magick_unused(encoding),
const PointInfo *offset,
2121 TypeMetric *metrics,ExceptionInfo *exception)
2123 magick_unreferenced(encoding);
2124 (void) ThrowMagickException(exception,GetMagickModule(),
2125 MissingDelegateWarning,
"DelegateLibrarySupportNotBuiltIn",
"'%s' (Freetype)",
2126 draw_info->font != (
char *) NULL ? draw_info->font :
"none");
2127 return(RenderPostscript(image,draw_info,offset,metrics,exception));
2164static char *EscapeParenthesis(
const char *source)
2178 assert(source != (
const char *) NULL);
2180 for (p=source; *p !=
'\0'; p++)
2182 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2185 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2190 destination=(
char *) NULL;
2191 if (~length >= (MagickPathExtent-1))
2192 destination=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
2193 sizeof(*destination));
2194 if (destination == (
char *) NULL)
2195 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2198 for (p=source; *p !=
'\0'; p++)
2200 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2205 return(destination);
2208static MagickBooleanType RenderPostscript(Image *image,
2209 const DrawInfo *draw_info,
const PointInfo *offset,TypeMetric *metrics,
2210 ExceptionInfo *exception)
2213 filename[MagickPathExtent],
2214 geometry[MagickPathExtent],
2250 if (draw_info->debug != MagickFalse)
2251 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2252 "Font %s; pointsize %g",draw_info->font != (
char *) NULL ?
2253 draw_info->font :
"none",draw_info->pointsize);
2255 unique_file=AcquireUniqueFileResource(filename);
2256 if (unique_file != -1)
2257 file=fdopen(unique_file,
"wb");
2258 if ((unique_file == -1) || (file == (FILE *) NULL))
2260 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",filename);
2261 return(MagickFalse);
2263 (void) FormatLocaleFile(file,
"%%!PS-Adobe-3.0\n");
2264 (void) FormatLocaleFile(file,
"/ReencodeType\n");
2265 (void) FormatLocaleFile(file,
"{\n");
2266 (void) FormatLocaleFile(file,
" findfont dup length\n");
2267 (void) FormatLocaleFile(file,
2268 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2269 (void) FormatLocaleFile(file,
2270 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2271 (void) FormatLocaleFile(file,
"} bind def\n");
2275 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2276 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2277 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2280 length=strlen(draw_info->text);
2281 for (i=0; i <= (ssize_t) (length+2); i++)
2283 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2284 draw_info->affine.ry*2.0*draw_info->pointsize);
2285 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2286 draw_info->affine.sy*2.0*draw_info->pointsize);
2287 if (point.x > extent.x)
2289 if (point.y > extent.y)
2292 (void) FormatLocaleFile(file,
"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2293 extent.x/2.0,extent.y/2.0);
2294 (void) FormatLocaleFile(file,
"%g %g scale\n",draw_info->pointsize,
2295 draw_info->pointsize);
2296 if ((draw_info->font == (
char *) NULL) || (*draw_info->font ==
'\0') ||
2297 (strchr(draw_info->font,
'/') != (
char *) NULL))
2298 (void) FormatLocaleFile(file,
2299 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2301 (
void) FormatLocaleFile(file,
2302 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2304 (void) FormatLocaleFile(file,
"[%g %g %g %g 0 0] concat\n",
2305 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2306 draw_info->affine.sy);
2307 text=EscapeParenthesis(draw_info->text);
2308 if (identity == MagickFalse)
2309 (void) FormatLocaleFile(file,
"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2311 (void) FormatLocaleFile(file,
"(%s) show\n",text);
2312 text=DestroyString(text);
2313 (void) FormatLocaleFile(file,
"showpage\n");
2314 (void) fclose(file);
2315 (void) FormatLocaleString(geometry,MagickPathExtent,
"%.20gx%.20g+0+0!",
2316 floor(extent.x+0.5),floor(extent.y+0.5));
2317 annotate_info=AcquireImageInfo();
2318 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,
"ps:%s",
2320 (void) CloneString(&annotate_info->page,geometry);
2321 if (draw_info->density != (
char *) NULL)
2322 (void) CloneString(&annotate_info->density,draw_info->density);
2323 annotate_info->antialias=draw_info->text_antialias;
2324 annotate_image=ReadImage(annotate_info,exception);
2325 CatchException(exception);
2326 annotate_info=DestroyImageInfo(annotate_info);
2327 (void) RelinquishUniqueFileResource(filename);
2328 if (annotate_image == (Image *) NULL)
2329 return(MagickFalse);
2330 (void) NegateImage(annotate_image,MagickFalse,exception);
2331 resolution.x=DefaultResolution;
2332 resolution.y=DefaultResolution;
2333 if (draw_info->density != (
char *) NULL)
2341 flags=ParseGeometry(draw_info->density,&geometry_info);
2342 if ((flags & RhoValue) != 0)
2343 resolution.x=geometry_info.rho;
2344 resolution.y=resolution.x;
2345 if ((flags & SigmaValue) != 0)
2346 resolution.y=geometry_info.sigma;
2348 if (identity == MagickFalse)
2349 (void) TransformImage(&annotate_image,
"0x0",(
char *) NULL,exception);
2355 crop_info=GetImageBoundingBox(annotate_image,exception);
2356 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2357 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2358 crop_info.y=CastDoubleToSsizeT(ceil((resolution.y/DefaultResolution)*
2360 (void) FormatLocaleString(geometry,MagickPathExtent,
2361 "%.20gx%.20g%+.20g%+.20g",(
double) crop_info.width,(
double)
2362 crop_info.height,(
double) crop_info.x,(
double) crop_info.y);
2363 (void) TransformImage(&annotate_image,geometry,(
char *) NULL,exception);
2365 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2366 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2367 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2368 metrics->ascent=metrics->pixels_per_em.y;
2369 metrics->descent=metrics->pixels_per_em.y/-5.0;
2370 metrics->width=(double) annotate_image->columns/
2371 ExpandAffine(&draw_info->affine);
2372 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2373 metrics->max_advance=metrics->pixels_per_em.x;
2374 metrics->bounds.x1=0.0;
2375 metrics->bounds.y1=metrics->descent;
2376 metrics->bounds.x2=metrics->ascent+metrics->descent;
2377 metrics->bounds.y2=metrics->ascent+metrics->descent;
2378 metrics->underline_position=(-2.0);
2379 metrics->underline_thickness=1.0;
2380 if (draw_info->render == MagickFalse)
2382 annotate_image=DestroyImage(annotate_image);
2385 if (draw_info->fill.alpha != (MagickRealType) TransparentAlpha)
2399 if ((image->alpha_trait & BlendPixelTrait) == 0)
2400 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2401 if (annotate_image->alpha_trait == UndefinedPixelTrait)
2402 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
2404 fill_color=draw_info->fill;
2406 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2407#if defined(MAGICKCORE_OPENMP_SUPPORT)
2408 #pragma omp parallel for schedule(static) shared(status) \
2409 magick_number_threads(annotate_image,annotate_image,annotate_image->rows,4)
2411 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2419 if (status == MagickFalse)
2421 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2423 if (q == (Quantum *) NULL)
2428 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2430 GetFillColor(draw_info,x,y,&fill_color,exception);
2431 SetPixelAlpha(annotate_image,ClampToQuantum((((
double) QuantumScale*
2432 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
2433 SetPixelRed(annotate_image,(
const Quantum) fill_color.red,q);
2434 SetPixelGreen(annotate_image,(
const Quantum) fill_color.green,q);
2435 SetPixelBlue(annotate_image,(
const Quantum) fill_color.blue,q);
2436 q+=(ptrdiff_t) GetPixelChannels(annotate_image);
2438 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2439 if (sync == MagickFalse)
2442 annotate_view=DestroyCacheView(annotate_view);
2443 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
2444 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2445 metrics->descent)-0.5),exception);
2447 annotate_image=DestroyImage(annotate_image);
2483static MagickBooleanType RenderX11(Image *image,
const DrawInfo *draw_info,
2484 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2490 ActivateSemaphoreInfo(&annotate_semaphore);
2491 LockSemaphoreInfo(annotate_semaphore);
2492 status=XRenderImage(image,draw_info,offset,metrics,exception);
2493 UnlockSemaphoreInfo(annotate_semaphore);