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))
676 if (width > image->columns)
677 p=ReplaceSpaceWithNewline(caption,s);
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) \
1490 *error_string=FreetypeErrorMessage(ft_status); \
1491 if (error_string != (const char *) NULL) \
1492 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1493 tag,"`%s (%s)'",value, error_string); \
1495 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1496 tag,"`%s'",value); \
1499 typedef struct _GlyphInfo
1588 static FT_Outline_Funcs
1591 (FT_Outline_MoveTo_Func) TraceMoveTo,
1592 (FT_Outline_LineTo_Func) TraceLineTo,
1593 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1594 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1607 memory=FreetypeAcquireMemoryManager();
1608 if (memory == (FT_Memory) NULL)
1609 ThrowBinaryException(ResourceLimitError,
"UnableToInitializeFreetypeLibrary",
1611 ft_status=FreetypeInit(memory,&library);
1613 ThrowFreetypeErrorException(
"UnableToInitializeFreetypeLibrary",ft_status,
1618 face_index=(FT_Long) draw_info->face;
1619 (void) memset(&args,0,
sizeof(args));
1620 if (draw_info->font == (
char *) NULL)
1622 const TypeInfo *type_info = GetTypeInfo(
"*",exception);
1623 if (type_info != (
const TypeInfo *) NULL)
1624 args.pathname=ConstantString(type_info->glyphs);
1627 if (*draw_info->font !=
'@')
1628 args.pathname=ConstantString(draw_info->font);
1634 ImageInfo *image_info = AcquireImageInfo();
1635 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1637 (void) SetImageInfo(image_info,0,exception);
1638 face_index=(FT_Long) image_info->scene;
1639 args.pathname=ConstantString(image_info->filename);
1640 image_info=DestroyImageInfo(image_info);
1645 stream=(FT_StreamRec *) AcquireCriticalMemory(
sizeof(*stream));
1646 (void) memset(stream,0,
sizeof(*stream));
1647 if (stat(args.pathname,&attributes) == 0)
1648 stream->size=attributes.st_size >= 0 ? (
unsigned long)
1649 attributes.st_size : 0;
1650 stream->descriptor.pointer=fopen_utf8(args.pathname,
"rb");
1651 stream->read=(&FreetypeReadStream);
1652 stream->close=(&FreetypeCloseStream);
1653 args.flags=FT_OPEN_STREAM;
1655 face=(FT_Face) NULL;
1656 ft_status=FT_Open_Face(library,&args,face_index,&face);
1659 FreetypeDone(memory,library,stream);
1660 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,args.pathname);
1661 args.pathname=DestroyString(args.pathname);
1662 return(MagickFalse);
1664 args.pathname=DestroyString(args.pathname);
1665 if ((draw_info->metrics != (
char *) NULL) &&
1666 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1667 (void) FT_Attach_File(face,draw_info->metrics);
1668 encoding_type=FT_ENCODING_UNICODE;
1669 ft_status=FT_Select_Charmap(face,encoding_type);
1670 if ((ft_status != 0) && (face->num_charmaps != 0))
1671 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1672 if (encoding != (
const char *) NULL)
1674 if (LocaleCompare(encoding,
"AdobeCustom") == 0)
1675 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1676 if (LocaleCompare(encoding,
"AdobeExpert") == 0)
1677 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1678 if (LocaleCompare(encoding,
"AdobeStandard") == 0)
1679 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1680 if (LocaleCompare(encoding,
"AppleRoman") == 0)
1681 encoding_type=FT_ENCODING_APPLE_ROMAN;
1682 if (LocaleCompare(encoding,
"BIG5") == 0)
1683 encoding_type=FT_ENCODING_BIG5;
1684#if defined(FT_ENCODING_PRC)
1685 if (LocaleCompare(encoding,
"GB2312") == 0)
1686 encoding_type=FT_ENCODING_PRC;
1688#if defined(FT_ENCODING_JOHAB)
1689 if (LocaleCompare(encoding,
"Johab") == 0)
1690 encoding_type=FT_ENCODING_JOHAB;
1692#if defined(FT_ENCODING_ADOBE_LATIN_1)
1693 if (LocaleCompare(encoding,
"Latin-1") == 0)
1694 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1696#if defined(FT_ENCODING_ADOBE_LATIN_2)
1697 if (LocaleCompare(encoding,
"Latin-2") == 0)
1698 encoding_type=FT_ENCODING_OLD_LATIN_2;
1700 if (LocaleCompare(encoding,
"None") == 0)
1701 encoding_type=FT_ENCODING_NONE;
1702 if (LocaleCompare(encoding,
"SJIScode") == 0)
1703 encoding_type=FT_ENCODING_SJIS;
1704 if (LocaleCompare(encoding,
"Symbol") == 0)
1705 encoding_type=FT_ENCODING_MS_SYMBOL;
1706 if (LocaleCompare(encoding,
"Unicode") == 0)
1707 encoding_type=FT_ENCODING_UNICODE;
1708 if (LocaleCompare(encoding,
"Wansung") == 0)
1709 encoding_type=FT_ENCODING_WANSUNG;
1710 ft_status=FT_Select_Charmap(face,encoding_type);
1713 (void) FT_Done_Face(face);
1714 FreetypeDone(memory,library,stream);
1715 ThrowFreetypeErrorException(
"UnrecognizedFontEncoding",ft_status,
1717 return(MagickFalse);
1723 resolution.x=DefaultResolution;
1724 resolution.y=DefaultResolution;
1725 if (draw_info->density != (
char *) NULL)
1733 geometry_flags=ParseGeometry(draw_info->density,&geometry_info);
1734 if ((geometry_flags & RhoValue) != 0)
1735 resolution.x=geometry_info.rho;
1736 resolution.y=resolution.x;
1737 if ((geometry_flags & SigmaValue) != 0)
1738 resolution.y=geometry_info.sigma;
1740 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1741 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1742 (FT_UInt) resolution.y);
1745 (void) FT_Done_Face(face);
1746 FreetypeDone(memory,library,stream);
1747 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,
1749 return(MagickFalse);
1751 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1752 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1753 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1754 metrics->descent=(double) face->size->metrics.descender/64.0;
1755 if (face->size->metrics.ascender == 0)
1760 metrics->ascent=face->size->metrics.y_ppem;
1761 if (face->size->metrics.descender == 0)
1762 metrics->descent=face->size->metrics.y_ppem/-3.5;
1765 metrics->origin.x=0;
1766 metrics->origin.y=0;
1767 metrics->height=(double) face->size->metrics.height/64.0;
1768 metrics->max_advance=0.0;
1769 if (face->size->metrics.max_advance > MagickEpsilon)
1770 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1771 metrics->bounds.x1=0.0;
1772 metrics->bounds.y1=metrics->descent;
1773 metrics->bounds.x2=metrics->ascent+metrics->descent;
1774 metrics->bounds.y2=metrics->ascent+metrics->descent;
1775 metrics->underline_position=face->underline_position*
1776 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1777 metrics->underline_thickness=face->underline_thickness*
1778 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1780 FT_Get_First_Char(face,&first_glyph_id);
1781 if ((draw_info->text == (
char *) NULL) || (*draw_info->text ==
'\0') ||
1782 (first_glyph_id == 0))
1784 (void) FT_Done_Face(face);
1785 FreetypeDone(memory,library,stream);
1791 if (draw_info->debug != MagickFalse)
1792 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Font %s; "
1793 "font-encoding %s; text-encoding %s; pointsize %g",
1794 draw_info->font != (
char *) NULL ? draw_info->font :
"none",
1795 encoding != (
char *) NULL ? encoding :
"none",
1796 draw_info->encoding != (
char *) NULL ? draw_info->encoding :
"none",
1797 draw_info->pointsize);
1798 flags=FT_LOAD_DEFAULT;
1799 if (draw_info->render == MagickFalse)
1800 flags=FT_LOAD_NO_BITMAP;
1801 if (draw_info->text_antialias == MagickFalse)
1802 flags|=FT_LOAD_TARGET_MONO;
1805#if defined(FT_LOAD_TARGET_LIGHT)
1806 flags|=FT_LOAD_TARGET_LIGHT;
1807#elif defined(FT_LOAD_TARGET_LCD)
1808 flags|=FT_LOAD_TARGET_LCD;
1811 value=GetImageProperty(image,
"type:hinting",exception);
1812 if ((value != (
const char *) NULL) && (LocaleCompare(value,
"off") == 0))
1813 flags|=FT_LOAD_NO_HINTING;
1815 glyph.image=(FT_Glyph) NULL;
1823 if (draw_info->render != MagickFalse)
1825 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1826 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1827 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1828 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1830 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1831 if (annotate_info->dash_pattern != (
double *) NULL)
1832 annotate_info->dash_pattern[0]=0.0;
1833 (void) CloneString(&annotate_info->primitive,
"path '");
1835 if (draw_info->render != MagickFalse)
1837 if (image->storage_class != DirectClass)
1838 (void) SetImageStorageClass(image,DirectClass,exception);
1839 if ((image->alpha_trait & BlendPixelTrait) == 0)
1840 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1842 for (p=draw_info->text; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
1843 if (GetUTFCode(p) < 0)
1845 utf8=(
unsigned char *) NULL;
1846 if (GetUTFCode(p) == 0)
1850 utf8=ConvertLatin1ToUTF8((
unsigned char *) draw_info->text);
1851 if (utf8 != (
unsigned char *) NULL)
1854 grapheme=(GraphemeInfo *) NULL;
1855#if defined(MAGICKCORE_RAQM_DELEGATE)
1856 length=ComplexRaqmTextLayout(image,draw_info,p,strlen(p),face,&grapheme,
1859 length=ComplexTextLayout(draw_info,p,strlen(p),face,flags,&grapheme);
1861 missing_glyph_id=FT_Get_Char_Index(face,
' ');
1863 last_character=(ssize_t) length-1;
1864 for (i=0; i < (ssize_t) length; i++)
1872 glyph.id=(FT_UInt) grapheme[i].index;
1874 glyph.id=missing_glyph_id;
1875 if ((glyph.id != 0) && (last_glyph_id != 0))
1876 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1877 glyph.origin=origin;
1878 glyph.origin.x+=(FT_Pos) grapheme[i].x_offset;
1879 glyph.origin.y+=(FT_Pos) grapheme[i].y_offset;
1880 if (glyph.image != (FT_Glyph) NULL)
1882 FT_Done_Glyph(glyph.image);
1883 glyph.image=(FT_Glyph) NULL;
1885 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1888 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1891 outline=((FT_OutlineGlyph) glyph.image)->outline;
1892 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1893 (IsEmptyOutline(outline) == MagickFalse))
1895 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1898 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1899 metrics->bounds.x1=(double) bounds.xMin;
1900 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1901 metrics->bounds.y1=(double) bounds.yMin;
1902 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1903 metrics->bounds.x2=(double) bounds.xMax;
1904 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1905 metrics->bounds.y2=(double) bounds.yMax;
1906 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
1907 (draw_info->stroke_pattern != (Image *) NULL)) &&
1908 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1913 annotate_info->affine.tx=glyph.origin.x/64.0;
1914 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1915 if (IsEmptyOutline(outline) == MagickFalse)
1916 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1919 FT_Vector_Transform(&glyph.origin,&affine);
1920 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1921 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1922 (FT_Vector *) NULL,MagickTrue);
1925 bitmap=(FT_BitmapGlyph) glyph.image;
1926 point.x=offset->x+bitmap->left;
1927 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1928 point.x+=(origin.x/64.0);
1929 point.y=offset->y-bitmap->top;
1930 if (draw_info->render != MagickFalse)
1944 transparent_fill=((draw_info->fill.alpha == (MagickRealType) TransparentAlpha) &&
1945 (draw_info->fill_pattern == (Image *) NULL) &&
1946 (draw_info->stroke.alpha == (MagickRealType) TransparentAlpha) &&
1947 (draw_info->stroke_pattern == (Image *) NULL)) ? MagickTrue :
1949 r=bitmap->bitmap.buffer;
1950 image_view=AcquireAuthenticCacheView(image,exception);
1951#if defined(MAGICKCORE_OPENMP_SUPPORT)
1952 #pragma omp parallel for schedule(static) shared(status) \
1953 magick_number_threads(image,image,bitmap->bitmap.rows,4)
1955 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1978 if (status == MagickFalse)
1980 x_offset=CastDoubleToSsizeT(ceil(point.x-0.5));
1981 y_offset=CastDoubleToSsizeT(ceil(point.y+y-0.5));
1982 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1985 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1989 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1990 bitmap->bitmap.width,1,exception);
1991 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
1993 n=y*bitmap->bitmap.pitch;
1994 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
1996 x_offset=CastDoubleToSsizeT(ceil(point.x+x-0.5));
1997 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1999 if (q != (Quantum *) NULL)
2000 q+=(ptrdiff_t) GetPixelChannels(image);
2004 if (bitmap->bitmap.buffer != (
unsigned char *) NULL)
2006 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
2007 fill_opacity=(double) (r[n])/(bitmap->bitmap.num_grays-1);
2009 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
2010 fill_opacity=((r[(x >> 3)+y*bitmap->bitmap.pitch] &
2011 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
2013 if (draw_info->text_antialias == MagickFalse)
2014 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
2015 if (active == MagickFalse)
2016 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
2018 if (q == (Quantum *) NULL)
2020 if (transparent_fill == MagickFalse)
2022 GetPixelInfo(image,&fill_color);
2023 GetFillColor(draw_info,x_offset,y_offset,&fill_color,exception);
2024 fill_opacity=fill_opacity*fill_color.alpha;
2025 CompositePixelOver(image,&fill_color,fill_opacity,q,
2026 GetPixelAlpha(image,q),q);
2034 Da=1.0-(QuantumScale*(double) GetPixelAlpha(image,q));
2036 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*(
double)
2038 SetPixelAlpha(image,(
const Quantum) fill_opacity,q);
2040 if (active == MagickFalse)
2042 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2043 if (sync == MagickFalse)
2046 q+=(ptrdiff_t) GetPixelChannels(image);
2048 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2049 if (sync == MagickFalse)
2052 image_view=DestroyCacheView(image_view);
2053 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
2054 (draw_info->stroke_pattern != (Image *) NULL)) &&
2055 (status != MagickFalse))
2060 annotate_info->linejoin=RoundJoin;
2061 annotate_info->affine.tx=offset->x;
2062 annotate_info->affine.ty=offset->y;
2063 (void) ConcatenateString(&annotate_info->primitive,
"'");
2064 if (strlen(annotate_info->primitive) > 7)
2065 (void) DrawImage(image,annotate_info,exception);
2066 (void) CloneString(&annotate_info->primitive,
"path '");
2069 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
2070 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
2071 (IsUTFSpace((
int) code) == MagickFalse))
2072 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
2074 if (i == last_character)
2075 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
2077 origin.x+=(FT_Pos) grapheme[i].x_advance;
2078 origin.y+=(FT_Pos) grapheme[i].y_advance;
2079 metrics->origin.x=(double) origin.x;
2080 metrics->origin.y=(double) origin.y;
2081 if (metrics->origin.x > metrics->width)
2082 metrics->width=metrics->origin.x;
2083 last_glyph_id=glyph.id;
2084 code=GetUTFCode(p+grapheme[i].cluster);
2086 if (grapheme != (GraphemeInfo *) NULL)
2087 grapheme=(GraphemeInfo *) RelinquishMagickMemory(grapheme);
2088 if (utf8 != (
unsigned char *) NULL)
2089 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
2090 if (glyph.image != (FT_Glyph) NULL)
2092 FT_Done_Glyph(glyph.image);
2093 glyph.image=(FT_Glyph) NULL;
2098 metrics->bounds.x1/=64.0;
2099 metrics->bounds.y1/=64.0;
2100 metrics->bounds.x2/=64.0;
2101 metrics->bounds.y2/=64.0;
2102 metrics->origin.x/=64.0;
2103 metrics->origin.y/=64.0;
2104 metrics->width=ceil(metrics->width/64.0);
2108 annotate_info=DestroyDrawInfo(annotate_info);
2109 (void) FT_Done_Face(face);
2110 FreetypeDone(memory,library,stream);
2114static MagickBooleanType RenderFreetype(Image *image,
const DrawInfo *draw_info,
2115 const char *magick_unused(encoding),
const PointInfo *offset,
2116 TypeMetric *metrics,ExceptionInfo *exception)
2118 magick_unreferenced(encoding);
2119 (void) ThrowMagickException(exception,GetMagickModule(),
2120 MissingDelegateWarning,
"DelegateLibrarySupportNotBuiltIn",
"'%s' (Freetype)",
2121 draw_info->font != (
char *) NULL ? draw_info->font :
"none");
2122 return(RenderPostscript(image,draw_info,offset,metrics,exception));
2159static char *EscapeParenthesis(
const char *source)
2173 assert(source != (
const char *) NULL);
2175 for (p=source; *p !=
'\0'; p++)
2177 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2180 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2185 destination=(
char *) NULL;
2186 if (~length >= (MagickPathExtent-1))
2187 destination=(
char *) AcquireQuantumMemory(length+MagickPathExtent,
2188 sizeof(*destination));
2189 if (destination == (
char *) NULL)
2190 ThrowFatalException(ResourceLimitFatalError,
"UnableToEscapeString");
2193 for (p=source; *p !=
'\0'; p++)
2195 if ((*p ==
'\\') || (*p ==
'(') || (*p ==
')'))
2200 return(destination);
2203static MagickBooleanType RenderPostscript(Image *image,
2204 const DrawInfo *draw_info,
const PointInfo *offset,TypeMetric *metrics,
2205 ExceptionInfo *exception)
2208 filename[MagickPathExtent],
2209 geometry[MagickPathExtent],
2245 if (draw_info->debug != MagickFalse)
2246 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2247 "Font %s; pointsize %g",draw_info->font != (
char *) NULL ?
2248 draw_info->font :
"none",draw_info->pointsize);
2250 unique_file=AcquireUniqueFileResource(filename);
2251 if (unique_file != -1)
2252 file=fdopen(unique_file,
"wb");
2253 if ((unique_file == -1) || (file == (FILE *) NULL))
2255 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",filename);
2256 return(MagickFalse);
2258 (void) FormatLocaleFile(file,
"%%!PS-Adobe-3.0\n");
2259 (void) FormatLocaleFile(file,
"/ReencodeType\n");
2260 (void) FormatLocaleFile(file,
"{\n");
2261 (void) FormatLocaleFile(file,
" findfont dup length\n");
2262 (void) FormatLocaleFile(file,
2263 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2264 (void) FormatLocaleFile(file,
2265 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2266 (void) FormatLocaleFile(file,
"} bind def\n");
2270 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2271 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2272 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2275 length=strlen(draw_info->text);
2276 for (i=0; i <= (ssize_t) (length+2); i++)
2278 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2279 draw_info->affine.ry*2.0*draw_info->pointsize);
2280 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2281 draw_info->affine.sy*2.0*draw_info->pointsize);
2282 if (point.x > extent.x)
2284 if (point.y > extent.y)
2287 (void) FormatLocaleFile(file,
"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2288 extent.x/2.0,extent.y/2.0);
2289 (void) FormatLocaleFile(file,
"%g %g scale\n",draw_info->pointsize,
2290 draw_info->pointsize);
2291 if ((draw_info->font == (
char *) NULL) || (*draw_info->font ==
'\0') ||
2292 (strchr(draw_info->font,
'/') != (
char *) NULL))
2293 (void) FormatLocaleFile(file,
2294 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2296 (
void) FormatLocaleFile(file,
2297 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2299 (void) FormatLocaleFile(file,
"[%g %g %g %g 0 0] concat\n",
2300 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2301 draw_info->affine.sy);
2302 text=EscapeParenthesis(draw_info->text);
2303 if (identity == MagickFalse)
2304 (void) FormatLocaleFile(file,
"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2306 (void) FormatLocaleFile(file,
"(%s) show\n",text);
2307 text=DestroyString(text);
2308 (void) FormatLocaleFile(file,
"showpage\n");
2309 (void) fclose(file);
2310 (void) FormatLocaleString(geometry,MagickPathExtent,
"%.20gx%.20g+0+0!",
2311 floor(extent.x+0.5),floor(extent.y+0.5));
2312 annotate_info=AcquireImageInfo();
2313 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,
"ps:%s",
2315 (void) CloneString(&annotate_info->page,geometry);
2316 if (draw_info->density != (
char *) NULL)
2317 (void) CloneString(&annotate_info->density,draw_info->density);
2318 annotate_info->antialias=draw_info->text_antialias;
2319 annotate_image=ReadImage(annotate_info,exception);
2320 CatchException(exception);
2321 annotate_info=DestroyImageInfo(annotate_info);
2322 (void) RelinquishUniqueFileResource(filename);
2323 if (annotate_image == (Image *) NULL)
2324 return(MagickFalse);
2325 (void) NegateImage(annotate_image,MagickFalse,exception);
2326 resolution.x=DefaultResolution;
2327 resolution.y=DefaultResolution;
2328 if (draw_info->density != (
char *) NULL)
2336 flags=ParseGeometry(draw_info->density,&geometry_info);
2337 if ((flags & RhoValue) != 0)
2338 resolution.x=geometry_info.rho;
2339 resolution.y=resolution.x;
2340 if ((flags & SigmaValue) != 0)
2341 resolution.y=geometry_info.sigma;
2343 if (identity == MagickFalse)
2344 (void) TransformImage(&annotate_image,
"0x0",(
char *) NULL,exception);
2350 crop_info=GetImageBoundingBox(annotate_image,exception);
2351 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2352 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2353 crop_info.y=CastDoubleToSsizeT(ceil((resolution.y/DefaultResolution)*
2355 (void) FormatLocaleString(geometry,MagickPathExtent,
2356 "%.20gx%.20g%+.20g%+.20g",(
double) crop_info.width,(
double)
2357 crop_info.height,(
double) crop_info.x,(
double) crop_info.y);
2358 (void) TransformImage(&annotate_image,geometry,(
char *) NULL,exception);
2360 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2361 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2362 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2363 metrics->ascent=metrics->pixels_per_em.y;
2364 metrics->descent=metrics->pixels_per_em.y/-5.0;
2365 metrics->width=(double) annotate_image->columns/
2366 ExpandAffine(&draw_info->affine);
2367 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2368 metrics->max_advance=metrics->pixels_per_em.x;
2369 metrics->bounds.x1=0.0;
2370 metrics->bounds.y1=metrics->descent;
2371 metrics->bounds.x2=metrics->ascent+metrics->descent;
2372 metrics->bounds.y2=metrics->ascent+metrics->descent;
2373 metrics->underline_position=(-2.0);
2374 metrics->underline_thickness=1.0;
2375 if (draw_info->render == MagickFalse)
2377 annotate_image=DestroyImage(annotate_image);
2380 if (draw_info->fill.alpha != (MagickRealType) TransparentAlpha)
2394 if ((image->alpha_trait & BlendPixelTrait) == 0)
2395 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2396 if (annotate_image->alpha_trait == UndefinedPixelTrait)
2397 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
2399 fill_color=draw_info->fill;
2401 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2402#if defined(MAGICKCORE_OPENMP_SUPPORT)
2403 #pragma omp parallel for schedule(static) shared(status) \
2404 magick_number_threads(annotate_image,annotate_image,annotate_image->rows,4)
2406 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2414 if (status == MagickFalse)
2416 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2418 if (q == (Quantum *) NULL)
2423 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2425 GetFillColor(draw_info,x,y,&fill_color,exception);
2426 SetPixelAlpha(annotate_image,ClampToQuantum((((
double) QuantumScale*
2427 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
2428 SetPixelRed(annotate_image,(
const Quantum) fill_color.red,q);
2429 SetPixelGreen(annotate_image,(
const Quantum) fill_color.green,q);
2430 SetPixelBlue(annotate_image,(
const Quantum) fill_color.blue,q);
2431 q+=(ptrdiff_t) GetPixelChannels(annotate_image);
2433 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2434 if (sync == MagickFalse)
2437 annotate_view=DestroyCacheView(annotate_view);
2438 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
2439 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2440 metrics->descent)-0.5),exception);
2442 annotate_image=DestroyImage(annotate_image);
2478static MagickBooleanType RenderX11(Image *image,
const DrawInfo *draw_info,
2479 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2485 ActivateSemaphoreInfo(&annotate_semaphore);
2486 LockSemaphoreInfo(annotate_semaphore);
2487 status=XRenderImage(image,draw_info,offset,metrics,exception);
2488 UnlockSemaphoreInfo(annotate_semaphore);