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#include "coders/ghostscript-private.h"
87#if defined(MAGICKCORE_FREETYPE_DELEGATE)
88#if defined(__MINGW32__)
92#if defined(FT_FREETYPE_H)
93# include FT_FREETYPE_H
95# include <freetype/freetype.h>
97#if defined(FT_GLYPH_H)
100# include <freetype/ftglyph.h>
102#if defined(FT_OUTLINE_H)
103# include FT_OUTLINE_H
105# include <freetype/ftoutln.h>
107#if defined(FT_BBOX_H)
110# include <freetype/ftbbox.h>
112#if defined(FT_MODULE_H)
115# include <freetype/ftmodapi.h>
118#if defined(MAGICKCORE_RAQM_DELEGATE)
143static MagickBooleanType
144 RenderType(Image *,
const DrawInfo *,
const PointInfo *,TypeMetric *,
146 RenderPostscript(Image *,
const DrawInfo *,
const PointInfo *,TypeMetric *,
148 RenderFreetype(Image *,
const DrawInfo *,
const char *,
const PointInfo *,
149 TypeMetric *,ExceptionInfo *),
150 RenderX11(Image *,
const DrawInfo *,
const PointInfo *,TypeMetric *,
171MagickPrivate MagickBooleanType AnnotateComponentGenesis(
void)
174 annotate_semaphore=AcquireSemaphoreInfo();
196MagickPrivate
void AnnotateComponentTerminus(
void)
199 ActivateSemaphoreInfo(&annotate_semaphore);
200 RelinquishSemaphoreInfo(&annotate_semaphore);
230MagickExport MagickBooleanType AnnotateImage(Image *image,
231 const DrawInfo *draw_info,ExceptionInfo *exception)
235 color[MagickPathExtent],
236 primitive[MagickPathExtent],
269 assert(image != (Image *) NULL);
270 assert(image->signature == MagickCoreSignature);
271 if (IsEventLogging() != MagickFalse)
272 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
273 assert(draw_info != (DrawInfo *) NULL);
274 assert(draw_info->signature == MagickCoreSignature);
275 if (draw_info->text == (
char *) NULL)
277 if (*draw_info->text ==
'\0')
279 annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
281 annotate->text=(
char *) NULL;
282 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
284 for (p=text; *p !=
'\0'; p++)
287 textlist=(
char **) AcquireQuantumMemory(number_lines+1,
sizeof(*textlist));
288 if (textlist == (
char **) NULL)
290 annotate_info=DestroyDrawInfo(annotate_info);
291 annotate=DestroyDrawInfo(annotate);
292 text=DestroyString(text);
296 for (i=0; i < (ssize_t) number_lines; i++)
302 for (q=p; *q !=
'\0'; q++)
303 if ((*q ==
'\r') || (*q ==
'\n'))
313 textlist[i]=(
char *) NULL;
314 SetGeometry(image,&geometry);
315 SetGeometryInfo(&geometry_info);
316 if (annotate_info->geometry != (
char *) NULL)
318 (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
320 (void) ParseGeometry(annotate_info->geometry,&geometry_info);
322 if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
324 annotate_info=DestroyDrawInfo(annotate_info);
325 annotate=DestroyDrawInfo(annotate);
326 textlist=(
char **) RelinquishMagickMemory(textlist);
327 text=DestroyString(text);
330 if (IsGrayColorspace(image->colorspace) != MagickFalse)
331 (void) SetImageColorspace(image,sRGBColorspace,exception);
333 (void) memset(&metrics,0,
sizeof(metrics));
334 for (i=0; textlist[i] != (
char *) NULL; i++)
336 if (*textlist[i] ==
'\0')
341 annotate_info->affine.tx=geometry_info.xi-(double) image->page.x;
342 annotate_info->affine.ty=geometry_info.psi-(double) image->page.y;
343 (void) CloneString(&annotate->text,textlist[i]);
344 if ((metrics.width == 0) || (annotate->gravity != NorthWestGravity))
345 (void) GetTypeMetrics(image,annotate,&metrics,exception);
346 height=CastDoubleToSizeT(metrics.ascent-metrics.descent+0.5);
348 height=(size_t) draw_info->pointsize;
349 height=CastDoubleToSizeT(floor((
double) height+
350 draw_info->interline_spacing+0.5));
351 switch (annotate->gravity)
353 case UndefinedGravity:
356 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
357 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
360 case (GravityType) NorthWestGravity:
362 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
363 annotate_info->affine.ry*height+annotate_info->affine.ry*
364 (metrics.ascent+metrics.descent);
365 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
366 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
367 annotate_info->affine.sy*metrics.ascent;
370 case (GravityType) NorthGravity:
372 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
373 geometry.width/2.0+i*annotate_info->affine.ry*height-
374 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
375 (metrics.ascent+metrics.descent);
376 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
377 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
378 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
382 case (GravityType) NorthEastGravity:
384 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
385 geometry.width+i*annotate_info->affine.ry*height-
386 annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
387 (metrics.ascent+metrics.descent)-1.0;
388 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
389 (metrics.bounds.y2-metrics.ascent)+i*annotate_info->affine.sy*height+
390 annotate_info->affine.sy*metrics.ascent-annotate_info->affine.rx*
394 case (GravityType) WestGravity:
396 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
397 annotate_info->affine.ry*height+annotate_info->affine.ry*
398 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
399 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
400 geometry.height/2.0+i*annotate_info->affine.sy*height+
401 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
402 (number_lines-1.0)*height)/2.0;
405 case (GravityType) CenterGravity:
407 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
408 geometry.width/2.0+i*annotate_info->affine.ry*height-
409 annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
410 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
411 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
412 geometry.height/2.0+i*annotate_info->affine.sy*height-
413 annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
414 (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
417 case (GravityType) EastGravity:
419 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
420 geometry.width+i*annotate_info->affine.ry*height-
421 annotate_info->affine.sx*metrics.width+
422 annotate_info->affine.ry*(metrics.ascent+metrics.descent-
423 (number_lines-1.0)*height)/2.0-1.0;
424 offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
425 geometry.height/2.0+i*annotate_info->affine.sy*height-
426 annotate_info->affine.rx*metrics.width+
427 annotate_info->affine.sy*(metrics.ascent+metrics.descent-
428 (number_lines-1.0)*height)/2.0;
431 case (GravityType) SouthWestGravity:
433 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
434 annotate_info->affine.ry*height-annotate_info->affine.ry*
435 (number_lines-1.0)*height;
436 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
437 geometry.height+i*annotate_info->affine.sy*height-
438 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
441 case (GravityType) SouthGravity:
443 offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
444 geometry.width/2.0+i*annotate_info->affine.ry*height-
445 annotate_info->affine.sx*metrics.width/2.0-
446 annotate_info->affine.ry*(number_lines-1.0)*height/2.0;
447 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
448 geometry.height+i*annotate_info->affine.sy*height-
449 annotate_info->affine.rx*metrics.width/2.0-
450 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
453 case (GravityType) SouthEastGravity:
455 offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
456 geometry.width+i*annotate_info->affine.ry*height-
457 annotate_info->affine.sx*metrics.width-
458 annotate_info->affine.ry*(number_lines-1.0)*height-1.0;
459 offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
460 geometry.height+i*annotate_info->affine.sy*height-
461 annotate_info->affine.rx*metrics.width-
462 annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
466 switch (annotate->align)
470 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
471 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
476 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
477 annotate_info->affine.sx*metrics.width/2.0;
478 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
479 annotate_info->affine.rx*metrics.width/2.0;
484 offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
485 annotate_info->affine.sx*metrics.width;
486 offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
487 annotate_info->affine.rx*metrics.width;
493 if (draw_info->undercolor.alpha != (MagickRealType) TransparentAlpha)
501 undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
502 undercolor_info->fill=draw_info->undercolor;
503 undercolor_info->affine=draw_info->affine;
504 undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
505 undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
506 (void) FormatLocaleString(primitive,MagickPathExtent,
507 "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(
double) height);
508 (void) CloneString(&undercolor_info->primitive,primitive);
509 (void) DrawImage(image,undercolor_info,exception);
510 (void) DestroyDrawInfo(undercolor_info);
512 annotate_info->affine.tx=offset.x;
513 annotate_info->affine.ty=offset.y;
514 pixel=annotate_info->fill;
515 if (annotate_info->stroke.alpha != (MagickRealType) TransparentAlpha)
516 pixel=annotate_info->stroke;
517 (void) QueryColorname(image,&pixel,AllCompliance,color,exception);
518 (void) FormatLocaleString(primitive,MagickPathExtent,
"stroke %s "
519 "stroke-width %g line 0,0 %g,0",color,(
double)
520 metrics.underline_thickness,(
double) metrics.width);
524 switch (annotate->decorate)
526 case OverlineDecoration:
528 annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
529 metrics.descent-metrics.underline_position));
530 (void) CloneString(&annotate_info->primitive,primitive);
531 (void) DrawImage(image,annotate_info,exception);
534 case UnderlineDecoration:
536 annotate_info->affine.ty-=(draw_info->affine.sy*
537 metrics.underline_position);
538 (void) CloneString(&annotate_info->primitive,primitive);
539 (void) DrawImage(image,annotate_info,exception);
545 status=RenderType(image,annotate,&offset,&metrics,exception);
546 if (status == MagickFalse)
549 if (annotate->decorate == LineThroughDecoration) {
550 annotate_info->affine.ty-=(draw_info->affine.sy*(height+
551 metrics.underline_position+metrics.descent*2)/2.0);
552 (void) CloneString(&annotate_info->primitive,primitive);
553 (void) DrawImage(image,annotate_info,exception);
559 annotate_info=DestroyDrawInfo(annotate_info);
560 annotate=DestroyDrawInfo(annotate);
561 textlist=(
char **) RelinquishMagickMemory(textlist);
562 text=DestroyString(text);
602static inline char *ReplaceSpaceWithNewline(
char **caption,
char *space)
607 if ((caption == (
char **) NULL) || (space == (
char *) NULL))
608 return((
char *) NULL);
609 octets=(size_t) GetUTFOctets(space);
623 length=strlen(*caption);
625 offset=space-(*caption);
628 target=AcquireString(*caption);
629 (void) CopyMagickString(target,*caption,(
size_t) offset+2);
630 (void) ConcatenateMagickString(target,space+octets,length);
631 (void) DestroyString(*caption);
633 space=(*caption)+offset;
639MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
640 const MagickBooleanType split,TypeMetric *metrics,
char **caption,
641 ExceptionInfo *exception)
661 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
672 if ((draw_info->word_break != BreakWordBreakType) &&
673 (IsUTFSpace(code) != MagickFalse) &&
674 (IsNonBreakingUTFSpace(code) == MagickFalse))
676 if (width > image->columns)
677 p=ReplaceSpaceWithNewline(caption,p);
680 for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
683 status=GetTypeMetrics(image,draw_info,metrics,exception);
684 if (status == MagickFalse)
686 width=CastDoubleToSizeT(metrics->width+draw_info->stroke_width+0.5);
687 if (width <= image->columns)
689 if (s != (
char *) NULL)
690 p=ReplaceSpaceWithNewline(caption,s);
692 if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
698 if ((n > 0) && ((*caption)[n-1] !=
'\n'))
703 target=AcquireString(*caption);
704 (void) CopyMagickString(target,*caption,(
size_t) n+1);
705 (void) ConcatenateMagickString(target,
"\n",strlen(*caption)+1);
706 (void) ConcatenateMagickString(target,p,strlen(*caption)+2);
707 (void) DestroyString(*caption);
716 for (p=(*caption); GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
717 if (GetUTFCode(p) ==
'\n')
771MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
772 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
798 assert(image != (Image *) NULL);
799 assert(image->signature == MagickCoreSignature);
800 if (IsEventLogging() != MagickFalse)
801 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
802 assert(draw_info != (DrawInfo *) NULL);
803 assert(draw_info->text != (
char *) NULL);
804 assert(draw_info->signature == MagickCoreSignature);
805 if (*draw_info->text ==
'\0')
807 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
808 "LabelExpected",
"`%s'",image->filename);
811 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
812 annotate_info->text=DestroyString(annotate_info->text);
816 textlist=StringToStrings(draw_info->text,&count);
817 if (textlist == (
char **) NULL)
819 annotate_info=DestroyDrawInfo(annotate_info);
822 annotate_info->render=MagickFalse;
823 annotate_info->direction=UndefinedDirection;
824 (void) memset(metrics,0,
sizeof(*metrics));
825 (void) memset(&extent,0,
sizeof(extent));
829 annotate_info->text=textlist[0];
830 status=GetTypeMetrics(image,annotate_info,&extent,exception);
832 height=(count*(size_t) (metrics->ascent-metrics->descent+
833 0.5)+(count-1)*draw_info->interline_spacing);
834 size=(MagickSizeType) fabs(height);
835 if (AcquireMagickResource(HeightResource,size) == MagickFalse)
837 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
838 "WidthOrHeightExceedsLimit",
"`%s'",image->filename);
843 for (i=1; i < (ssize_t) count; i++)
845 annotate_info->text=textlist[i];
846 status=GetTypeMetrics(image,annotate_info,&extent,exception);
847 if (status == MagickFalse)
849 if (extent.width > metrics->width)
851 size=(MagickSizeType) fabs(extent.width);
852 if (AcquireMagickResource(WidthResource,size) == MagickFalse)
854 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
855 "WidthOrHeightExceedsLimit",
"`%s'",image->filename);
860 metrics->height=(double) height;
865 annotate_info->text=(
char *) NULL;
866 annotate_info=DestroyDrawInfo(annotate_info);
867 for (i=0; i < (ssize_t) count; i++)
868 textlist[i]=DestroyString(textlist[i]);
869 textlist=(
char **) RelinquishMagickMemory(textlist);
919MagickExport MagickBooleanType GetTypeMetrics(Image *image,
920 const DrawInfo *draw_info,TypeMetric *metrics,ExceptionInfo *exception)
931 assert(image != (Image *) NULL);
932 assert(image->signature == MagickCoreSignature);
933 if (IsEventLogging() != MagickFalse)
934 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
935 assert(draw_info != (DrawInfo *) NULL);
936 assert(draw_info->text != (
char *) NULL);
937 assert(draw_info->signature == MagickCoreSignature);
938 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
939 annotate_info->render=MagickFalse;
940 annotate_info->direction=UndefinedDirection;
941 (void) memset(metrics,0,
sizeof(*metrics));
944 status=RenderType(image,annotate_info,&offset,metrics,exception);
945 if (draw_info->debug != MagickFalse)
946 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Metrics: text: %s; "
947 "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
948 "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
949 "underline position: %g; underline thickness: %g",annotate_info->text,
950 metrics->width,metrics->height,metrics->ascent,metrics->descent,
951 metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
952 metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
953 metrics->pixels_per_em.x,metrics->pixels_per_em.y,
954 metrics->underline_position,metrics->underline_thickness);
955 annotate_info=DestroyDrawInfo(annotate_info);
991static MagickBooleanType RenderType(Image *image,
const DrawInfo *draw_info,
992 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
1006 type_info=(
const TypeInfo *) NULL;
1007 if (draw_info->font != (
char *) NULL)
1009 if (*draw_info->font ==
'@')
1011 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
1015 if (*draw_info->font ==
'-')
1016 return(RenderX11(image,draw_info,offset,metrics,exception));
1017 if (*draw_info->font ==
'^')
1018 return(RenderPostscript(image,draw_info,offset,metrics,exception));
1019 if (IsPathAccessible(draw_info->font) != MagickFalse)
1021 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
1025 type_info=GetTypeInfo(draw_info->font,exception);
1026 if (type_info == (
const TypeInfo *) NULL)
1027 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1028 "UnableToReadFont",
"`%s'",draw_info->font);
1030 if ((type_info == (
const TypeInfo *) NULL) &&
1031 (draw_info->family != (
const char *) NULL))
1033 if (strpbrk(draw_info->family,
",'\"") == (
char *) NULL)
1034 type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
1035 draw_info->stretch,draw_info->weight,exception);
1036 if (type_info == (
const TypeInfo *) NULL)
1050 family=StringToArgv(draw_info->family,&number_families);
1051 for (i=1; i < (ssize_t) number_families; i++)
1053 (void) SubstituteString(&family[i],
",",
"");
1054 type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1055 draw_info->stretch,draw_info->weight,exception);
1056 if ((type_info != (
const TypeInfo *) NULL) &&
1057 (LocaleCompare(family[i],type_info->family) == 0))
1060 for (i=0; i < (ssize_t) number_families; i++)
1061 family[i]=DestroyString(family[i]);
1062 family=(
char **) RelinquishMagickMemory(family);
1063 if (type_info == (
const TypeInfo *) NULL)
1064 (void) ThrowMagickException(exception,GetMagickModule(),TypeWarning,
1065 "UnableToReadFont",
"`%s'",draw_info->family);
1068 font=GetPolicyValue(
"system:font");
1069 if (font != (
char *) NULL)
1071 if (IsPathAccessible(font) != MagickFalse)
1076 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1077 annotate_info->font=font;
1078 status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1079 offset,metrics,exception);
1080 annotate_info=DestroyDrawInfo(annotate_info);
1083 font=DestroyString(font);
1085 if (type_info == (
const TypeInfo *) NULL)
1093 sans_exception=AcquireExceptionInfo();
1094 if (type_info == (
const TypeInfo *) NULL)
1095 type_info=GetTypeInfoByFamily((
const char *) NULL,draw_info->style,
1096 draw_info->stretch,draw_info->weight,sans_exception);
1097 if (type_info == (
const TypeInfo *) NULL)
1098 type_info=GetTypeInfo(
"*",sans_exception);
1099 sans_exception=DestroyExceptionInfo(sans_exception);
1101 if (type_info == (
const TypeInfo *) NULL)
1103 status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics,
1107 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1108 annotate_info->face=type_info->face;
1109 if (type_info->metrics != (
char *) NULL)
1110 (void) CloneString(&annotate_info->metrics,type_info->metrics);
1111 if (type_info->glyphs != (
char *) NULL)
1112 (void) CloneString(&annotate_info->font,type_info->glyphs);
1113 status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics,
1115 annotate_info=DestroyDrawInfo(annotate_info);
1155#if defined(MAGICKCORE_FREETYPE_DELEGATE)
1157#if defined(MAGICKCORE_RAQM_DELEGATE)
1158static MagickBooleanType ContainsRaqmParagraphSeparator(
const unsigned char *p,
1159 const size_t length)
1164 if ((p == (
const unsigned char*) NULL) || (length == 0))
1165 return(MagickFalse);
1167 for (i = 0; i < length; i++)
1170 if ((p[i] ==
'\n') || (p[i] ==
'\r'))
1174 if ((p[i] == 0xC2) && (i + 1 < length) && (p[i + 1] == 0x85))
1178 if ((p[i] == 0xE2) && (i + 2 < length) && (p[i + 1] == 0x80) &&
1179 ((p[i + 2] == 0xA8) || (p[i + 2] == 0xA9)))
1183 return(MagickFalse);
1186static size_t ComplexRaqmTextLayout(
const Image *image,
1187 const DrawInfo *draw_info,
const char *text,
const size_t length,
1188 const FT_Face face,GraphemeInfo **grapheme,ExceptionInfo *exception)
1207 if (rq == (raqm_t *) NULL)
1209 if (raqm_set_text_utf8(rq,text,length) == 0)
1211 if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1213 if (raqm_set_freetype_face(rq,face) == 0)
1215 features=GetImageProperty(image,
"type:features",exception);
1216 if (features != (
const char *) NULL)
1231 token_info=AcquireTokenInfo();
1232 token=AcquireString(
"");
1233 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1234 &breaker,&next,"e);
1235 while (status_token == 0)
1237 raqm_add_font_feature(rq,token,(
int) strlen(token));
1238 status_token=Tokenizer(token_info,0,token,50,features,
"",
",",
"",
'\0',
1239 &breaker,&next,"e);
1241 token_info=DestroyTokenInfo(token_info);
1242 token=DestroyString(token);
1244 if (raqm_layout(rq) == 0)
1246 glyphs=raqm_get_glyphs(rq,&extent);
1247 if (glyphs == (raqm_glyph_t *) NULL)
1252 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(extent,
sizeof(**grapheme));
1253 if (*grapheme == (GraphemeInfo *) NULL)
1258 for (i=0; i < (ssize_t) extent; i++)
1260 (*grapheme)[i].index=glyphs[i].index;
1261 (*grapheme)[i].x_offset=glyphs[i].x_offset;
1262 (*grapheme)[i].x_advance=glyphs[i].x_advance;
1263 (*grapheme)[i].y_offset=glyphs[i].y_offset;
1264 (*grapheme)[i].y_advance=glyphs[i].y_advance;
1265 (*grapheme)[i].cluster=glyphs[i].cluster;
1274static size_t ComplexTextLayout(
const DrawInfo *draw_info,
const char *text,
1275 const size_t length,
const FT_Face face,
const FT_Int32 flags,
1276 GraphemeInfo **grapheme)
1290 *grapheme=(GraphemeInfo *) AcquireQuantumMemory(length+1,
sizeof(**grapheme));
1291 if (*grapheme == (GraphemeInfo *) NULL)
1295 for (i=0; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p), i++)
1297 (*grapheme)[i].index=(ssize_t) FT_Get_Char_Index(face,(FT_ULong)
1299 (*grapheme)[i].x_offset=0;
1300 (*grapheme)[i].y_offset=0;
1301 if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1303 if (FT_HAS_KERNING(face))
1311 ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1312 (*grapheme)[i].index,ft_kerning_default,&kerning);
1314 (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1315 RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1318 (void) FT_Load_Glyph(face,(FT_UInt) (*grapheme)[i].index,flags);
1319 (*grapheme)[i].x_advance=face->glyph->advance.x;
1320 (*grapheme)[i].y_advance=face->glyph->advance.y;
1321 (*grapheme)[i].cluster=(size_t) (p-text);
1322 last_glyph=(*grapheme)[i].index;
1327static void FreetypeCloseStream(FT_Stream stream)
1332 file=(FILE *) stream->descriptor.pointer;
1333 if (file != (FILE *) NULL)
1334 (void) fclose(file);
1335 stream->descriptor.pointer=NULL;
1338static unsigned long FreetypeReadStream(FT_Stream stream,
unsigned long offset,
1339 unsigned char *buffer,
unsigned long count)
1347 file=(FILE *) stream->descriptor.pointer;
1348 if (file == (FILE *) NULL)
1350 if (offset > stream->size)
1353 result=(
unsigned long) fseek(file,(off_t) offset,SEEK_SET);
1358 return((
unsigned long) fread(buffer,1,count,file));
1361static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1363 return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1367static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1368 DrawInfo *draw_info)
1374 path[MagickPathExtent];
1376 affine=draw_info->affine;
1377 (void) FormatLocaleString(path,MagickPathExtent,
"C%g,%g %g,%g %g,%g",
1378 affine.tx+p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-
1379 q->y/64.0,affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1380 (void) ConcatenateString(&draw_info->primitive,path);
1384static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1390 path[MagickPathExtent];
1392 affine=draw_info->affine;
1393 (void) FormatLocaleString(path,MagickPathExtent,
"L%g,%g",affine.tx+to->x/64.0,
1394 affine.ty-to->y/64.0);
1395 (void) ConcatenateString(&draw_info->primitive,path);
1399static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1405 path[MagickPathExtent];
1407 affine=draw_info->affine;
1408 (void) FormatLocaleString(path,MagickPathExtent,
"M%g,%g",affine.tx+to->x/64.0,
1409 affine.ty-to->y/64.0);
1410 (void) ConcatenateString(&draw_info->primitive,path);
1414static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1415 DrawInfo *draw_info)
1421 path[MagickPathExtent];
1423 affine=draw_info->affine;
1424 (void) FormatLocaleString(path,MagickPathExtent,
"Q%g,%g %g,%g",affine.tx+
1425 control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1427 (void) ConcatenateString(&draw_info->primitive,path);
1431#if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1432static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1434 return(FT_Error_String(ft_status));
1436static inline const char *FreetypeErrorMessage(
1437 FT_Error magick_unused(ft_status))
1439 magick_unreferenced(ft_status);
1440 return((
const char *) NULL);
1444static void *FreetypeAlloc(FT_Memory magick_unused(memory),
long extent)
1446 magick_unreferenced(memory);
1448 return((
void *) NULL);
1449 return(AcquireQuantumMemory((
size_t) extent,1));
1452static void *FreetypeRealloc(FT_Memory magick_unused(memory),
long length,
1453 long extent,
void *block)
1459 magick_unreferenced(memory);
1461 return(RelinquishMagickMemory(block));
1462 if (block == (
void *) NULL)
1463 return(AcquireQuantumMemory((
size_t) extent,1));
1464 cloned_block=AcquireQuantumMemory((
size_t) length,1);
1465 if (cloned_block == (
void *) NULL)
1466 return((
void *) NULL);
1467 (void) memcpy(cloned_block,block,(
size_t) length);
1468 new_block=ResizeQuantumMemory(cloned_block,(
size_t) extent,1);
1469 if (new_block == (
void *) NULL)
1470 return((
void *) NULL);
1471 block=RelinquishMagickMemory(block);
1475static void FreetypeFree(FT_Memory magick_unused(memory),
void *block)
1477 magick_unreferenced(memory);
1478 (void) RelinquishMagickMemory(block);
1481static FT_Memory FreetypeAcquireMemoryManager(
void)
1486 memory=(FT_Memory) AcquireCriticalMemory(
sizeof(*memory));
1487 memset(memory,0,
sizeof(*memory));
1488 memory->alloc=(&FreetypeAlloc);
1489 memory->realloc=(&FreetypeRealloc);
1490 memory->free=(&FreetypeFree);
1494static void FreetypeDone(FT_Memory memory,FT_Library library,
1495 FT_StreamRec *stream)
1497 (void) FT_Done_Library(library);
1498 stream=(FT_StreamRec *) RelinquishMagickMemory(stream);
1499 memory=(FT_Memory) RelinquishMagickMemory(memory);
1502static FT_Error FreetypeInit(FT_Memory memory,FT_Library *alibrary)
1507 ft_status=FT_New_Library(memory,alibrary);
1509 RelinquishMagickMemory(memory);
1511 FT_Add_Default_Modules(*alibrary);
1512 FT_Set_Default_Properties(*alibrary);
1516static MagickBooleanType RenderFreetype(Image *image,
const DrawInfo *draw_info,
1517 const char *encoding,
const PointInfo *offset,TypeMetric *metrics,
1518 ExceptionInfo *exception)
1520#if !defined(FT_OPEN_PATHNAME)
1521#define FT_OPEN_PATHNAME ft_open_pathname
1524#define ThrowFreetypeErrorException(tag,ft_status,value) \
1526 const char *error_string = FreetypeErrorMessage(ft_status); \
1527 if (error_string != (const char *) NULL) \
1528 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1529 tag,"`%s (%s)'",value, error_string); \
1531 (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1532 tag,"`%s'",value); \
1535 typedef struct _GlyphInfo
1624 static FT_Outline_Funcs
1627 (FT_Outline_MoveTo_Func) TraceMoveTo,
1628 (FT_Outline_LineTo_Func) TraceLineTo,
1629 (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1630 (FT_Outline_CubicTo_Func) TraceCubicBezier,
1643 if ((draw_info->font != (
char *) NULL) && (*draw_info->font ==
'@') &&
1644 (IsRightsAuthorized(PathPolicyDomain,ReadPolicyRights,draw_info->font) == MagickFalse))
1645 ThrowPolicyException(draw_info->font,MagickFalse);
1646 memory=FreetypeAcquireMemoryManager();
1647 if (memory == (FT_Memory) NULL)
1648 ThrowBinaryException(ResourceLimitError,
"UnableToInitializeFreetypeLibrary",
1650 ft_status=FreetypeInit(memory,&library);
1653 ThrowFreetypeErrorException(
"UnableToInitializeFreetypeLibrary",ft_status,
1655 return(MagickFalse);
1660 face_index=(FT_Long) draw_info->face;
1661 (void) memset(&args,0,
sizeof(args));
1662 if (draw_info->font == (
char *) NULL)
1664 const TypeInfo *type_info = GetTypeInfo(
"*",exception);
1665 if (type_info != (
const TypeInfo *) NULL)
1666 args.pathname=ConstantString(type_info->glyphs);
1669 if (*draw_info->font !=
'@')
1670 args.pathname=ConstantString(draw_info->font);
1676 ImageInfo *image_info = AcquireImageInfo();
1677 (void) CopyMagickString(image_info->filename,draw_info->font+1,
1679 (void) SetImageInfo(image_info,0,exception);
1680 face_index=(FT_Long) image_info->scene;
1681 args.pathname=ConstantString(image_info->filename);
1682 image_info=DestroyImageInfo(image_info);
1687 stream=(FT_StreamRec *) AcquireCriticalMemory(
sizeof(*stream));
1688 (void) memset(stream,0,
sizeof(*stream));
1689 if (stat(args.pathname,&attributes) == 0)
1690 stream->size=attributes.st_size >= 0 ? (
unsigned long)
1691 attributes.st_size : 0;
1692 stream->descriptor.pointer=fopen_utf8(args.pathname,
"rb");
1693 stream->read=(&FreetypeReadStream);
1694 stream->close=(&FreetypeCloseStream);
1695 args.flags=FT_OPEN_STREAM;
1697 face=(FT_Face) NULL;
1698 ft_status=FT_Open_Face(library,&args,face_index,&face);
1701 FreetypeDone(memory,library,stream);
1702 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,args.pathname);
1703 args.pathname=DestroyString(args.pathname);
1704 return(MagickFalse);
1706 args.pathname=DestroyString(args.pathname);
1707 if ((draw_info->metrics != (
char *) NULL) &&
1708 (IsPathAccessible(draw_info->metrics) != MagickFalse))
1709 (void) FT_Attach_File(face,draw_info->metrics);
1710 encoding_type=FT_ENCODING_UNICODE;
1711 ft_status=FT_Select_Charmap(face,encoding_type);
1712 if ((ft_status != 0) && (face->num_charmaps != 0))
1713 ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1714 if (encoding != (
const char *) NULL)
1716 if (LocaleCompare(encoding,
"AdobeCustom") == 0)
1717 encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1718 if (LocaleCompare(encoding,
"AdobeExpert") == 0)
1719 encoding_type=FT_ENCODING_ADOBE_EXPERT;
1720 if (LocaleCompare(encoding,
"AdobeStandard") == 0)
1721 encoding_type=FT_ENCODING_ADOBE_STANDARD;
1722 if (LocaleCompare(encoding,
"AppleRoman") == 0)
1723 encoding_type=FT_ENCODING_APPLE_ROMAN;
1724 if (LocaleCompare(encoding,
"BIG5") == 0)
1725 encoding_type=FT_ENCODING_BIG5;
1726#if defined(FT_ENCODING_PRC)
1727 if (LocaleCompare(encoding,
"GB2312") == 0)
1728 encoding_type=FT_ENCODING_PRC;
1730#if defined(FT_ENCODING_JOHAB)
1731 if (LocaleCompare(encoding,
"Johab") == 0)
1732 encoding_type=FT_ENCODING_JOHAB;
1734#if defined(FT_ENCODING_ADOBE_LATIN_1)
1735 if (LocaleCompare(encoding,
"Latin-1") == 0)
1736 encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1738#if defined(FT_ENCODING_ADOBE_LATIN_2)
1739 if (LocaleCompare(encoding,
"Latin-2") == 0)
1740 encoding_type=FT_ENCODING_OLD_LATIN_2;
1742 if (LocaleCompare(encoding,
"None") == 0)
1743 encoding_type=FT_ENCODING_NONE;
1744 if (LocaleCompare(encoding,
"SJIScode") == 0)
1745 encoding_type=FT_ENCODING_SJIS;
1746 if (LocaleCompare(encoding,
"Symbol") == 0)
1747 encoding_type=FT_ENCODING_MS_SYMBOL;
1748 if (LocaleCompare(encoding,
"Unicode") == 0)
1749 encoding_type=FT_ENCODING_UNICODE;
1750 if (LocaleCompare(encoding,
"Wansung") == 0)
1751 encoding_type=FT_ENCODING_WANSUNG;
1752 ft_status=FT_Select_Charmap(face,encoding_type);
1755 (void) FT_Done_Face(face);
1756 FreetypeDone(memory,library,stream);
1757 ThrowFreetypeErrorException(
"UnrecognizedFontEncoding",ft_status,
1759 return(MagickFalse);
1765 resolution.x=DefaultResolution;
1766 resolution.y=DefaultResolution;
1767 if (draw_info->density != (
char *) NULL)
1775 geometry_flags=ParseGeometry(draw_info->density,&geometry_info);
1776 if ((geometry_flags & RhoValue) != 0)
1777 resolution.x=geometry_info.rho;
1778 resolution.y=resolution.x;
1779 if ((geometry_flags & SigmaValue) != 0)
1780 resolution.y=geometry_info.sigma;
1782 ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1783 (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1784 (FT_UInt) resolution.y);
1787 (void) FT_Done_Face(face);
1788 FreetypeDone(memory,library,stream);
1789 ThrowFreetypeErrorException(
"UnableToReadFont",ft_status,
1791 return(MagickFalse);
1793 metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1794 metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1795 metrics->ascent=(double) face->size->metrics.ascender/64.0;
1796 metrics->descent=(double) face->size->metrics.descender/64.0;
1797 if (face->size->metrics.ascender == 0)
1802 metrics->ascent=face->size->metrics.y_ppem;
1803 if (face->size->metrics.descender == 0)
1804 metrics->descent=face->size->metrics.y_ppem/-3.5;
1807 metrics->origin.x=0;
1808 metrics->origin.y=0;
1809 metrics->height=(double) face->size->metrics.height/64.0;
1810 metrics->max_advance=0.0;
1811 if (face->size->metrics.max_advance > MagickEpsilon)
1812 metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1813 metrics->bounds.x1=0.0;
1814 metrics->bounds.y1=metrics->descent;
1815 metrics->bounds.x2=metrics->ascent+metrics->descent;
1816 metrics->bounds.y2=metrics->ascent+metrics->descent;
1817 metrics->underline_position=face->underline_position*
1818 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1819 metrics->underline_thickness=face->underline_thickness*
1820 (metrics->pixels_per_em.x*MagickSafeReciprocal(face->units_per_EM));
1822 FT_Get_First_Char(face,&first_glyph_id);
1823 if ((draw_info->text == (
char *) NULL) || (*draw_info->text ==
'\0') ||
1824 (first_glyph_id == 0))
1826 (void) FT_Done_Face(face);
1827 FreetypeDone(memory,library,stream);
1833 if (draw_info->debug != MagickFalse)
1834 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
"Font %s; "
1835 "font-encoding %s; text-encoding %s; pointsize %g",
1836 draw_info->font != (
char *) NULL ? draw_info->font :
"none",
1837 encoding != (
char *) NULL ? encoding :
"none",
1838 draw_info->encoding != (
char *) NULL ? draw_info->encoding :
"none",
1839 draw_info->pointsize);
1840 flags=FT_LOAD_DEFAULT;
1841 if (draw_info->render == MagickFalse)
1842 flags=FT_LOAD_NO_BITMAP;
1843 if (draw_info->text_antialias == MagickFalse)
1844 flags|=FT_LOAD_TARGET_MONO;
1847#if defined(FT_LOAD_TARGET_LIGHT)
1848 flags|=FT_LOAD_TARGET_LIGHT;
1849#elif defined(FT_LOAD_TARGET_LCD)
1850 flags|=FT_LOAD_TARGET_LCD;
1853 value=GetImageProperty(image,
"type:hinting",exception);
1854 if ((value != (
const char *) NULL) && (LocaleCompare(value,
"off") == 0))
1855 flags|=FT_LOAD_NO_HINTING;
1857 glyph.image=(FT_Glyph) NULL;
1865 if (draw_info->render != MagickFalse)
1867 affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1868 affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1869 affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1870 affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1872 annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1873 if (annotate_info->dash_pattern != (
double *) NULL)
1874 annotate_info->dash_pattern[0]=0.0;
1875 (void) CloneString(&annotate_info->primitive,
"path '");
1877 if (draw_info->render != MagickFalse)
1879 if (image->storage_class != DirectClass)
1880 (void) SetImageStorageClass(image,DirectClass,exception);
1881 if ((image->alpha_trait & BlendPixelTrait) == 0)
1882 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1884 for (p=draw_info->text; GetUTFCode(p) != 0; p+=(ptrdiff_t) GetUTFOctets(p))
1885 if (GetUTFCode(p) < 0)
1887 utf8=(
unsigned char *) NULL;
1888 if (GetUTFCode(p) == 0)
1892 utf8=ConvertLatin1ToUTF8((
unsigned char *) draw_info->text);
1893 if (utf8 != (
unsigned char *) NULL)
1896 grapheme=(GraphemeInfo *) NULL;
1898#if defined(MAGICKCORE_RAQM_DELEGATE)
1899 if (ContainsRaqmParagraphSeparator((
const unsigned char *) p,length) == MagickFalse)
1900 length=ComplexRaqmTextLayout(image,draw_info,p,length,face,&grapheme,
1904 length=ComplexTextLayout(draw_info,p,length,face,flags,&grapheme);
1905 missing_glyph_id=FT_Get_Char_Index(face,
' ');
1907 last_character=(ssize_t) length-1;
1908 for (i=0; i < (ssize_t) length; i++)
1916 glyph.id=(FT_UInt) grapheme[i].index;
1918 glyph.id=missing_glyph_id;
1919 if ((glyph.id != 0) && (last_glyph_id != 0))
1920 origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1921 glyph.origin=origin;
1922 glyph.origin.x+=(FT_Pos) grapheme[i].x_offset;
1923 glyph.origin.y+=(FT_Pos) grapheme[i].y_offset;
1924 if (glyph.image != (FT_Glyph) NULL)
1926 FT_Done_Glyph(glyph.image);
1927 glyph.image=(FT_Glyph) NULL;
1929 ft_status=FT_Load_Glyph(face,glyph.id,flags);
1932 ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1935 outline=((FT_OutlineGlyph) glyph.image)->outline;
1936 if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1937 (IsEmptyOutline(outline) == MagickFalse))
1939 ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1942 if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1943 metrics->bounds.x1=(double) bounds.xMin;
1944 if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1945 metrics->bounds.y1=(double) bounds.yMin;
1946 if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1947 metrics->bounds.x2=(double) bounds.xMax;
1948 if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1949 metrics->bounds.y2=(double) bounds.yMax;
1950 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
1951 (draw_info->stroke_pattern != (Image *) NULL)) &&
1952 ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1957 annotate_info->affine.tx=glyph.origin.x/64.0;
1958 annotate_info->affine.ty=(-glyph.origin.y/64.0);
1959 if (IsEmptyOutline(outline) == MagickFalse)
1960 ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1963 FT_Vector_Transform(&glyph.origin,&affine);
1964 (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1965 ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1966 (FT_Vector *) NULL,MagickTrue);
1969 bitmap=(FT_BitmapGlyph) glyph.image;
1970 point.x=offset->x+bitmap->left;
1971 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1972 point.x+=(origin.x/64.0);
1973 point.y=offset->y-bitmap->top;
1974 if (draw_info->render != MagickFalse)
1988 transparent_fill=((draw_info->fill.alpha == (MagickRealType) TransparentAlpha) &&
1989 (draw_info->fill_pattern == (Image *) NULL) &&
1990 (draw_info->stroke.alpha == (MagickRealType) TransparentAlpha) &&
1991 (draw_info->stroke_pattern == (Image *) NULL)) ? MagickTrue :
1993 r=bitmap->bitmap.buffer;
1994 image_view=AcquireAuthenticCacheView(image,exception);
1995#if defined(MAGICKCORE_OPENMP_SUPPORT)
1996 #pragma omp parallel for schedule(static) shared(status) \
1997 magick_number_threads(image,image,bitmap->bitmap.rows,4)
1999 for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
2022 if (status == MagickFalse)
2024 x_offset=CastDoubleToSsizeT(ceil(point.x-0.5));
2025 y_offset=CastDoubleToSsizeT(ceil(point.y+y-0.5));
2026 if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
2029 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
2033 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
2034 bitmap->bitmap.width,1,exception);
2035 active=q != (Quantum *) NULL ? MagickTrue : MagickFalse;
2037 n=y*bitmap->bitmap.pitch;
2038 for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
2040 x_offset=CastDoubleToSsizeT(ceil(point.x+x-0.5));
2041 if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
2043 if (q != (Quantum *) NULL)
2044 q+=(ptrdiff_t) GetPixelChannels(image);
2048 if (bitmap->bitmap.buffer != (
unsigned char *) NULL)
2050 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
2051 fill_opacity=(double) (r[n])/(bitmap->bitmap.num_grays-1);
2053 if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
2054 fill_opacity=((r[(x >> 3)+y*bitmap->bitmap.pitch] &
2055 (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
2057 if (draw_info->text_antialias == MagickFalse)
2058 fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
2059 if (active == MagickFalse)
2060 q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
2062 if (q == (Quantum *) NULL)
2064 if (transparent_fill == MagickFalse)
2066 GetPixelInfo(image,&fill_color);
2067 GetFillColor(draw_info,x_offset,y_offset,&fill_color,exception);
2068 fill_opacity=fill_opacity*fill_color.alpha;
2069 CompositePixelOver(image,&fill_color,fill_opacity,q,
2070 GetPixelAlpha(image,q),q);
2078 Da=1.0-(QuantumScale*(double) GetPixelAlpha(image,q));
2080 fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*(
double)
2082 SetPixelAlpha(image,(
const Quantum) fill_opacity,q);
2084 if (active == MagickFalse)
2086 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2087 if (sync == MagickFalse)
2090 q+=(ptrdiff_t) GetPixelChannels(image);
2092 sync=SyncCacheViewAuthenticPixels(image_view,exception);
2093 if (sync == MagickFalse)
2096 image_view=DestroyCacheView(image_view);
2097 if (((draw_info->stroke.alpha != (MagickRealType) TransparentAlpha) ||
2098 (draw_info->stroke_pattern != (Image *) NULL)) &&
2099 (status != MagickFalse))
2104 annotate_info->linejoin=RoundJoin;
2105 annotate_info->affine.tx=offset->x;
2106 annotate_info->affine.ty=offset->y;
2107 (void) ConcatenateString(&annotate_info->primitive,
"'");
2108 if (strlen(annotate_info->primitive) > 7)
2109 (void) DrawImage(image,annotate_info,exception);
2110 (void) CloneString(&annotate_info->primitive,
"path '");
2113 if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
2114 (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
2115 (IsUTFSpace((
int) code) == MagickFalse))
2116 origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
2118 if (i == last_character)
2119 origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
2121 origin.x+=(FT_Pos) grapheme[i].x_advance;
2122 origin.y+=(FT_Pos) grapheme[i].y_advance;
2123 metrics->origin.x=(double) origin.x;
2124 metrics->origin.y=(double) origin.y;
2125 if (metrics->origin.x > metrics->width)
2126 metrics->width=metrics->origin.x;
2127 last_glyph_id=glyph.id;
2128 code=GetUTFCode(p+grapheme[i].cluster);
2130 if (grapheme != (GraphemeInfo *) NULL)
2131 grapheme=(GraphemeInfo *) RelinquishMagickMemory(grapheme);
2132 if (utf8 != (
unsigned char *) NULL)
2133 utf8=(
unsigned char *) RelinquishMagickMemory(utf8);
2134 if (glyph.image != (FT_Glyph) NULL)
2136 FT_Done_Glyph(glyph.image);
2137 glyph.image=(FT_Glyph) NULL;
2142 metrics->bounds.x1/=64.0;
2143 metrics->bounds.y1/=64.0;
2144 metrics->bounds.x2/=64.0;
2145 metrics->bounds.y2/=64.0;
2146 metrics->origin.x/=64.0;
2147 metrics->origin.y/=64.0;
2148 metrics->width=ceil(metrics->width/64.0);
2152 annotate_info=DestroyDrawInfo(annotate_info);
2153 (void) FT_Done_Face(face);
2154 FreetypeDone(memory,library,stream);
2158static MagickBooleanType RenderFreetype(Image *image,
const DrawInfo *draw_info,
2159 const char *magick_unused(encoding),
const PointInfo *offset,
2160 TypeMetric *metrics,ExceptionInfo *exception)
2162 magick_unreferenced(encoding);
2163 (void) ThrowMagickException(exception,GetMagickModule(),
2164 MissingDelegateWarning,
"DelegateLibrarySupportNotBuiltIn",
"'%s' (Freetype)",
2165 draw_info->font != (
char *) NULL ? draw_info->font :
"none");
2166 return(RenderPostscript(image,draw_info,offset,metrics,exception));
2203static MagickBooleanType IsValidPostscriptFontname(
const char *font)
2208 if ((font == (
const char *) NULL) || (*font ==
'\0'))
2209 return(MagickFalse);
2210 for (p=(
const unsigned char *) font; *p !=
'\0'; p++)
2212 if ((isalnum(*p) != 0) || (*p ==
'-') || (*p ==
'_') || (*p ==
'.'))
2214 return(MagickFalse);
2219static MagickBooleanType RenderPostscript(Image *image,
2220 const DrawInfo *draw_info,
const PointInfo *offset,TypeMetric *metrics,
2221 ExceptionInfo *exception)
2224 filename[MagickPathExtent],
2225 geometry[MagickPathExtent],
2261 if (draw_info->debug != MagickFalse)
2262 (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2263 "Font %s; pointsize %g",draw_info->font != (
char *) NULL ?
2264 draw_info->font :
"none",draw_info->pointsize);
2266 unique_file=AcquireUniqueFileResource(filename);
2267 if (unique_file != -1)
2268 file=fdopen(unique_file,
"wb");
2269 if ((unique_file == -1) || (file == (FILE *) NULL))
2271 ThrowFileException(exception,FileOpenError,
"UnableToOpenFile",filename);
2272 return(MagickFalse);
2274 (void) FormatLocaleFile(file,
"%%!PS-Adobe-3.0\n");
2275 (void) FormatLocaleFile(file,
"/ReencodeType\n");
2276 (void) FormatLocaleFile(file,
"{\n");
2277 (void) FormatLocaleFile(file,
" findfont dup length\n");
2278 (void) FormatLocaleFile(file,
2279 " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2280 (void) FormatLocaleFile(file,
2281 " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2282 (void) FormatLocaleFile(file,
"} bind def\n");
2286 identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2287 (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2288 (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2291 length=strlen(draw_info->text);
2292 for (i=0; i <= (ssize_t) (length+2); i++)
2294 point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2295 draw_info->affine.ry*2.0*draw_info->pointsize);
2296 point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2297 draw_info->affine.sy*2.0*draw_info->pointsize);
2298 if (point.x > extent.x)
2300 if (point.y > extent.y)
2303 (void) FormatLocaleFile(file,
"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2304 extent.x/2.0,extent.y/2.0);
2305 (void) FormatLocaleFile(file,
"%g %g scale\n",draw_info->pointsize,
2306 draw_info->pointsize);
2307 if (IsValidPostscriptFontname(draw_info->font) == MagickFalse)
2308 (void) FormatLocaleFile(file,
2309 "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2311 (
void) FormatLocaleFile(file,
2312 "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2314 (void) FormatLocaleFile(file,
"[%g %g %g %g 0 0] concat\n",
2315 draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2316 draw_info->affine.sy);
2317 text=EscapeParenthesis(draw_info->text,MAGICK_SIZE_MAX,exception);
2318 if (identity == MagickFalse)
2319 (void) FormatLocaleFile(file,
"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2321 (void) FormatLocaleFile(file,
"(%s) show\n",text);
2322 text=DestroyString(text);
2323 (void) FormatLocaleFile(file,
"showpage\n");
2324 (void) fclose(file);
2325 (void) FormatLocaleString(geometry,MagickPathExtent,
"%.17gx%.17g+0+0!",
2326 floor(extent.x+0.5),floor(extent.y+0.5));
2327 annotate_info=AcquireImageInfo();
2328 (void) FormatLocaleString(annotate_info->filename,MagickPathExtent,
"ps:%s",
2330 (void) CloneString(&annotate_info->page,geometry);
2331 if (draw_info->density != (
char *) NULL)
2332 (void) CloneString(&annotate_info->density,draw_info->density);
2333 annotate_info->antialias=draw_info->text_antialias;
2334 annotate_image=ReadImage(annotate_info,exception);
2335 CatchException(exception);
2336 annotate_info=DestroyImageInfo(annotate_info);
2337 (void) RelinquishUniqueFileResource(filename);
2338 if (annotate_image == (Image *) NULL)
2339 return(MagickFalse);
2340 (void) NegateImage(annotate_image,MagickFalse,exception);
2341 resolution.x=DefaultResolution;
2342 resolution.y=DefaultResolution;
2343 if (draw_info->density != (
char *) NULL)
2351 flags=ParseGeometry(draw_info->density,&geometry_info);
2352 if ((flags & RhoValue) != 0)
2353 resolution.x=geometry_info.rho;
2354 resolution.y=resolution.x;
2355 if ((flags & SigmaValue) != 0)
2356 resolution.y=geometry_info.sigma;
2358 if (identity == MagickFalse)
2359 (void) TransformImage(&annotate_image,
"0x0",(
char *) NULL,exception);
2365 crop_info=GetImageBoundingBox(annotate_image,exception);
2366 crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2367 ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2368 crop_info.y=CastDoubleToSsizeT(ceil((resolution.y/DefaultResolution)*
2370 (void) FormatLocaleString(geometry,MagickPathExtent,
2371 "%.17gx%.17g%+.20g%+.20g",(
double) crop_info.width,(
double)
2372 crop_info.height,(
double) crop_info.x,(
double) crop_info.y);
2373 (void) TransformImage(&annotate_image,geometry,(
char *) NULL,exception);
2375 metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2376 ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2377 metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2378 metrics->ascent=metrics->pixels_per_em.y;
2379 metrics->descent=metrics->pixels_per_em.y/-5.0;
2380 metrics->width=(double) annotate_image->columns/
2381 ExpandAffine(&draw_info->affine);
2382 metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2383 metrics->max_advance=metrics->pixels_per_em.x;
2384 metrics->bounds.x1=0.0;
2385 metrics->bounds.y1=metrics->descent;
2386 metrics->bounds.x2=metrics->ascent+metrics->descent;
2387 metrics->bounds.y2=metrics->ascent+metrics->descent;
2388 metrics->underline_position=(-2.0);
2389 metrics->underline_thickness=1.0;
2390 if (draw_info->render == MagickFalse)
2392 annotate_image=DestroyImage(annotate_image);
2395 if (draw_info->fill.alpha != (MagickRealType) TransparentAlpha)
2409 if ((image->alpha_trait & BlendPixelTrait) == 0)
2410 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
2411 if (annotate_image->alpha_trait == UndefinedPixelTrait)
2412 (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel,
2414 fill_color=draw_info->fill;
2416 annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2417#if defined(MAGICKCORE_OPENMP_SUPPORT)
2418 #pragma omp parallel for schedule(static) shared(status) \
2419 magick_number_threads(annotate_image,annotate_image,annotate_image->rows,4)
2421 for (y=0; y < (ssize_t) annotate_image->rows; y++)
2429 if (status == MagickFalse)
2431 q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2433 if (q == (Quantum *) NULL)
2438 for (x=0; x < (ssize_t) annotate_image->columns; x++)
2440 GetFillColor(draw_info,x,y,&fill_color,exception);
2441 SetPixelAlpha(annotate_image,ClampToQuantum((((
double) QuantumScale*
2442 GetPixelIntensity(annotate_image,q)*fill_color.alpha))),q);
2443 SetPixelRed(annotate_image,(
const Quantum) fill_color.red,q);
2444 SetPixelGreen(annotate_image,(
const Quantum) fill_color.green,q);
2445 SetPixelBlue(annotate_image,(
const Quantum) fill_color.blue,q);
2446 q+=(ptrdiff_t) GetPixelChannels(annotate_image);
2448 sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2449 if (sync == MagickFalse)
2452 annotate_view=DestroyCacheView(annotate_view);
2453 (void) CompositeImage(image,annotate_image,OverCompositeOp,MagickTrue,
2454 (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2455 metrics->descent)-0.5),exception);
2457 annotate_image=DestroyImage(annotate_image);
2493static MagickBooleanType RenderX11(Image *image,
const DrawInfo *draw_info,
2494 const PointInfo *offset,TypeMetric *metrics,ExceptionInfo *exception)
2500 ActivateSemaphoreInfo(&annotate_semaphore);
2501 LockSemaphoreInfo(annotate_semaphore);
2502 status=XRenderImage(image,draw_info,offset,metrics,exception);
2503 UnlockSemaphoreInfo(annotate_semaphore);