MagickCore  6.9.12-55
Convert, Edit, Or Compose Bitmap Images
 All Data Structures
annotate.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % AAA N N N N OOO TTTTT AAA TTTTT EEEEE %
7 % A A NN N NN N O O T A A T E %
8 % AAAAA N N N N N N O O T AAAAA T EEE %
9 % A A N NN N NN O O T A A T E %
10 % A A N N N N OOO T A A T EEEEE %
11 % %
12 % %
13 % MagickCore Image Annotation Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % July 1992 %
18 % %
19 % %
20 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/script/license.php %
27 % %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 % Digital Applications (www.digapp.com) contributed the stroked text algorithm.
37 % It was written by Leonard Rosenthol.
38 %
39 %
40 */
41 
42 /*
43  Include declarations.
44 */
45 #include "magick/studio.h"
46 #include "magick/annotate.h"
47 #include "magick/attribute.h"
48 #include "magick/cache-private.h"
49 #include "magick/cache-view.h"
50 #include "magick/channel.h"
51 #include "magick/client.h"
52 #include "magick/color.h"
53 #include "magick/color-private.h"
54 #include "magick/colorspace-private.h"
55 #include "magick/composite.h"
56 #include "magick/composite-private.h"
57 #include "magick/constitute.h"
58 #include "magick/draw.h"
59 #include "magick/draw-private.h"
60 #include "magick/exception.h"
61 #include "magick/exception-private.h"
62 #include "magick/gem.h"
63 #include "magick/geometry.h"
64 #include "magick/image-private.h"
65 #include "magick/log.h"
66 #include "magick/quantum.h"
67 #include "magick/quantum-private.h"
68 #include "magick/pixel-accessor.h"
69 #include "magick/policy.h"
70 #include "magick/property.h"
71 #include "magick/resource_.h"
72 #include "magick/semaphore.h"
73 #include "magick/statistic.h"
74 #include "magick/string_.h"
75 #include "magick/token.h"
76 #include "magick/token-private.h"
77 #include "magick/transform.h"
78 #include "magick/type.h"
79 #include "magick/utility.h"
80 #include "magick/utility-private.h"
81 #include "magick/xwindow-private.h"
82 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
83 #if defined(__MINGW32__)
84 # undef interface
85 #endif
86 #include <ft2build.h>
87 #if defined(FT_FREETYPE_H)
88 # include FT_FREETYPE_H
89 #else
90 # include <freetype/freetype.h>
91 #endif
92 #if defined(FT_GLYPH_H)
93 # include FT_GLYPH_H
94 #else
95 # include <freetype/ftglyph.h>
96 #endif
97 #if defined(FT_OUTLINE_H)
98 # include FT_OUTLINE_H
99 #else
100 # include <freetype/ftoutln.h>
101 #endif
102 #if defined(FT_BBOX_H)
103 # include FT_BBOX_H
104 #else
105 # include <freetype/ftbbox.h>
106 #endif /* defined(FT_BBOX_H) */
107 #endif
108 #if defined(MAGICKCORE_RAQM_DELEGATE)
109 #include <raqm.h>
110 #endif
111 typedef struct _GraphemeInfo
112 {
113  ssize_t
114  index,
115  x_offset,
116  x_advance,
117  y_offset;
118 
119  size_t
120  cluster;
121 } GraphemeInfo;
122 
123 /*
124  Annotate semaphores.
125 */
126 static SemaphoreInfo
127  *annotate_semaphore = (SemaphoreInfo *) NULL;
128 
129 /*
130  Forward declarations.
131 */
132 static MagickBooleanType
133  RenderType(Image *,const DrawInfo *,const PointInfo *,TypeMetric *),
134  RenderPostscript(Image *,const DrawInfo *,const PointInfo *,TypeMetric *),
135  RenderFreetype(Image *,const DrawInfo *,const char *,const PointInfo *,
136  TypeMetric *),
137  RenderX11(Image *,const DrawInfo *,const PointInfo *,TypeMetric *);
138 
139 /*
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % %
142 % %
143 % %
144 + A n n o t a t e C o m p o n e n t G e n e s i s %
145 % %
146 % %
147 % %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 %
150 % AnnotateComponentGenesis() instantiates the annotate component.
151 %
152 % The format of the AnnotateComponentGenesis method is:
153 %
154 % MagickBooleanType AnnotateComponentGenesis(void)
155 %
156 */
157 MagickExport MagickBooleanType AnnotateComponentGenesis(void)
158 {
159  if (annotate_semaphore == (SemaphoreInfo *) NULL)
160  annotate_semaphore=AllocateSemaphoreInfo();
161  return(MagickTrue);
162 }
163 
164 /*
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % %
167 % %
168 % %
169 + A n n o t a t e C o m p o n e n t T e r m i n u s %
170 % %
171 % %
172 % %
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 %
175 % AnnotateComponentTerminus() destroys the annotate component.
176 %
177 % The format of the AnnotateComponentTerminus method is:
178 %
179 % AnnotateComponentTerminus(void)
180 %
181 */
182 MagickExport void AnnotateComponentTerminus(void)
183 {
184  if (annotate_semaphore == (SemaphoreInfo *) NULL)
185  ActivateSemaphoreInfo(&annotate_semaphore);
186  DestroySemaphoreInfo(&annotate_semaphore);
187 }
188 
189 /*
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % %
192 % %
193 % %
194 % A n n o t a t e I m a g e %
195 % %
196 % %
197 % %
198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 %
200 % AnnotateImage() annotates an image with text.
201 %
202 % The format of the AnnotateImage method is:
203 %
204 % MagickBooleanType AnnotateImage(Image *image,DrawInfo *draw_info)
205 %
206 % A description of each parameter follows:
207 %
208 % o image: the image.
209 %
210 % o draw_info: the draw info.
211 %
212 */
213 MagickExport MagickBooleanType AnnotateImage(Image *image,
214  const DrawInfo *draw_info)
215 {
216  char
217  *p,
218  color[MaxTextExtent],
219  primitive[MaxTextExtent],
220  *text,
221  **textlist;
222 
223  DrawInfo
224  *annotate,
225  *annotate_info;
226 
228  geometry_info;
229 
230  MagickBooleanType
231  status;
232 
234  pixel;
235 
236  PointInfo
237  offset;
238 
240  geometry;
241 
242  ssize_t
243  i;
244 
245  TypeMetric
246  metrics;
247 
248  size_t
249  height,
250  number_lines;
251 
252  assert(image != (Image *) NULL);
253  assert(image->signature == MagickCoreSignature);
254  if (IsEventLogging() != MagickFalse)
255  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
256  assert(draw_info != (DrawInfo *) NULL);
257  assert(draw_info->signature == MagickCoreSignature);
258  if (draw_info->text == (char *) NULL)
259  return(MagickFalse);
260  if (*draw_info->text == '\0')
261  return(MagickTrue);
262  annotate=CloneDrawInfo((ImageInfo *) NULL,draw_info);
263  text=annotate->text;
264  annotate->text=(char *) NULL;
265  annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
266  number_lines=1;
267  for (p=text; *p != '\0'; p++)
268  if (*p == '\n')
269  number_lines++;
270  textlist=(char **) AcquireQuantumMemory(number_lines+1,sizeof(*textlist));
271  if (textlist == (char **) NULL)
272  {
273  annotate_info=DestroyDrawInfo(annotate_info);
274  annotate=DestroyDrawInfo(annotate);
275  text=DestroyString(text);
276  return(MagickFalse);
277  }
278  p=text;
279  for (i=0; i < (ssize_t) number_lines; i++)
280  {
281  char
282  *q;
283 
284  textlist[i]=p;
285  for (q=p; *q != '\0'; q++)
286  if ((*q == '\r') || (*q == '\n'))
287  break;
288  if (*q == '\r')
289  {
290  *q='\0';
291  q++;
292  }
293  *q='\0';
294  p=q+1;
295  }
296  textlist[i]=(char *) NULL;
297  SetGeometry(image,&geometry);
298  SetGeometryInfo(&geometry_info);
299  if (annotate_info->geometry != (char *) NULL)
300  {
301  (void) ParsePageGeometry(image,annotate_info->geometry,&geometry,
302  &image->exception);
303  (void) ParseGeometry(annotate_info->geometry,&geometry_info);
304  }
305  if (SetImageStorageClass(image,DirectClass) == MagickFalse)
306  {
307  annotate_info=DestroyDrawInfo(annotate_info);
308  annotate=DestroyDrawInfo(annotate);
309  textlist=(char **) RelinquishMagickMemory(textlist);
310  text=DestroyString(text);
311  return(MagickFalse);
312  }
313  if (IsGrayColorspace(image->colorspace) != MagickFalse)
314  (void) SetImageColorspace(image,sRGBColorspace);
315  status=MagickTrue;
316  (void) memset(&metrics,0,sizeof(metrics));
317  for (i=0; textlist[i] != (char *) NULL; i++)
318  {
319  if (*textlist[i] == '\0')
320  continue;
321  /*
322  Position text relative to image.
323  */
324  annotate_info->affine.tx=geometry_info.xi-image->page.x;
325  annotate_info->affine.ty=geometry_info.psi-image->page.y;
326  (void) CloneString(&annotate->text,textlist[i]);
327  if ((metrics.width == 0) || (annotate->gravity != NorthWestGravity))
328  (void) GetTypeMetrics(image,annotate,&metrics);
329  height=(size_t) floor(metrics.ascent-metrics.descent+0.5);
330  if (height == 0)
331  height=draw_info->pointsize;
332  height+=(size_t) floor(draw_info->interline_spacing+0.5);
333  switch (annotate->gravity)
334  {
335  case UndefinedGravity:
336  default:
337  {
338  offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
339  offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
340  break;
341  }
342  case (GravityType) NorthWestGravity:
343  {
344  offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
345  annotate_info->affine.ry*height+annotate_info->affine.ry*
346  (metrics.ascent+metrics.descent);
347  offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
348  annotate_info->affine.sy*height+annotate_info->affine.sy*
349  metrics.ascent;
350  break;
351  }
352  case (GravityType) NorthGravity:
353  {
354  offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
355  geometry.width/2.0+i*annotate_info->affine.ry*height-
356  annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
357  (metrics.ascent+metrics.descent);
358  offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
359  annotate_info->affine.sy*height+annotate_info->affine.sy*
360  metrics.ascent-annotate_info->affine.rx*metrics.width/2.0;
361  break;
362  }
363  case (GravityType) NorthEastGravity:
364  {
365  offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
366  geometry.width+i*annotate_info->affine.ry*height-
367  annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
368  (metrics.ascent+metrics.descent)-1.0;
369  offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+i*
370  annotate_info->affine.sy*height+annotate_info->affine.sy*
371  metrics.ascent-annotate_info->affine.rx*metrics.width;
372  break;
373  }
374  case (GravityType) WestGravity:
375  {
376  offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
377  annotate_info->affine.ry*height+annotate_info->affine.ry*
378  (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
379  offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
380  geometry.height/2.0+i*annotate_info->affine.sy*height+
381  annotate_info->affine.sy*(metrics.ascent+metrics.descent-
382  (number_lines-1.0)*height)/2.0;
383  break;
384  }
385  case (GravityType) StaticGravity:
386  case (GravityType) CenterGravity:
387  {
388  offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
389  geometry.width/2.0+i*annotate_info->affine.ry*height-
390  annotate_info->affine.sx*metrics.width/2.0+annotate_info->affine.ry*
391  (metrics.ascent+metrics.descent-(number_lines-1)*height)/2.0;
392  offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
393  geometry.height/2.0+i*annotate_info->affine.sy*height-
394  annotate_info->affine.rx*metrics.width/2.0+annotate_info->affine.sy*
395  (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
396  break;
397  }
398  case (GravityType) EastGravity:
399  {
400  offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
401  geometry.width+i*annotate_info->affine.ry*height-
402  annotate_info->affine.sx*metrics.width+annotate_info->affine.ry*
403  (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0-1.0;
404  offset.y=(geometry.height == 0 ? -1.0 : 1.0)*annotate_info->affine.ty+
405  geometry.height/2.0+i*annotate_info->affine.sy*height-
406  annotate_info->affine.rx*metrics.width+annotate_info->affine.sy*
407  (metrics.ascent+metrics.descent-(number_lines-1.0)*height)/2.0;
408  break;
409  }
410  case (GravityType) SouthWestGravity:
411  {
412  offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+i*
413  annotate_info->affine.ry*height-annotate_info->affine.ry*
414  (number_lines-1.0)*height;
415  offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
416  geometry.height+i*annotate_info->affine.sy*height-
417  annotate_info->affine.sy*(number_lines-1.0)*height+metrics.descent;
418  break;
419  }
420  case (GravityType) SouthGravity:
421  {
422  offset.x=(geometry.width == 0 ? -1.0 : 1.0)*annotate_info->affine.tx+
423  geometry.width/2.0+i*annotate_info->affine.ry*height-
424  annotate_info->affine.sx*metrics.width/2.0-annotate_info->affine.ry*
425  (number_lines-1.0)*height/2.0;
426  offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
427  geometry.height+i*annotate_info->affine.sy*height-
428  annotate_info->affine.rx*metrics.width/2.0-annotate_info->affine.sy*
429  (number_lines-1.0)*height+metrics.descent;
430  break;
431  }
432  case (GravityType) SouthEastGravity:
433  {
434  offset.x=(geometry.width == 0 ? 1.0 : -1.0)*annotate_info->affine.tx+
435  geometry.width+i*annotate_info->affine.ry*height-
436  annotate_info->affine.sx*metrics.width-annotate_info->affine.ry*
437  (number_lines-1.0)*height-1.0;
438  offset.y=(geometry.height == 0 ? 1.0 : -1.0)*annotate_info->affine.ty+
439  geometry.height+i*annotate_info->affine.sy*height-
440  annotate_info->affine.rx*metrics.width-annotate_info->affine.sy*
441  (number_lines-1.0)*height+metrics.descent;
442  break;
443  }
444  }
445  switch (annotate->align)
446  {
447  case LeftAlign:
448  {
449  offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height;
450  offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height;
451  break;
452  }
453  case CenterAlign:
454  {
455  offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
456  annotate_info->affine.sx*metrics.width/2.0;
457  offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
458  annotate_info->affine.rx*metrics.width/2.0;
459  break;
460  }
461  case RightAlign:
462  {
463  offset.x=annotate_info->affine.tx+i*annotate_info->affine.ry*height-
464  annotate_info->affine.sx*metrics.width;
465  offset.y=annotate_info->affine.ty+i*annotate_info->affine.sy*height-
466  annotate_info->affine.rx*metrics.width;
467  break;
468  }
469  default:
470  break;
471  }
472  if (draw_info->undercolor.opacity != TransparentOpacity)
473  {
474  DrawInfo
475  *undercolor_info;
476 
477  /*
478  Text box.
479  */
480  undercolor_info=CloneDrawInfo((ImageInfo *) NULL,(DrawInfo *) NULL);
481  undercolor_info->fill=draw_info->undercolor;
482  undercolor_info->affine=draw_info->affine;
483  undercolor_info->affine.tx=offset.x-draw_info->affine.ry*metrics.ascent;
484  undercolor_info->affine.ty=offset.y-draw_info->affine.sy*metrics.ascent;
485  (void) FormatLocaleString(primitive,MaxTextExtent,
486  "rectangle 0.0,0.0 %g,%g",metrics.origin.x,(double) height);
487  (void) CloneString(&undercolor_info->primitive,primitive);
488  (void) DrawImage(image,undercolor_info);
489  (void) DestroyDrawInfo(undercolor_info);
490  }
491  annotate_info->affine.tx=offset.x;
492  annotate_info->affine.ty=offset.y;
493  pixel=annotate_info->fill;
494  if (annotate_info->stroke.opacity != TransparentOpacity)
495  pixel=annotate_info->stroke;
496  (void) QueryColorname(image,&pixel,SVGCompliance,color,&image->exception);
497  (void) FormatLocaleString(primitive,MagickPathExtent,"stroke %s "
498  "stroke-width %g line 0,0 %g,0",color,(double)
499  metrics.underline_thickness,(double) metrics.width);
500  /*
501  Annotate image with text.
502  */
503  switch (annotate->decorate)
504  {
505  case OverlineDecoration:
506  {
507  annotate_info->affine.ty-=(draw_info->affine.sy*(metrics.ascent+
508  metrics.descent-metrics.underline_position));
509  (void) CloneString(&annotate_info->primitive,primitive);
510  (void) DrawImage(image,annotate_info);
511  break;
512  }
513  case UnderlineDecoration:
514  {
515  annotate_info->affine.ty-=(draw_info->affine.sy*
516  metrics.underline_position);
517  (void) CloneString(&annotate_info->primitive,primitive);
518  (void) DrawImage(image,annotate_info);
519  break;
520  }
521  default:
522  break;
523  }
524  status=RenderType(image,annotate,&offset,&metrics);
525  if (status == MagickFalse)
526  break;
527  if (annotate->decorate == LineThroughDecoration)
528  {
529  annotate_info->affine.ty-=(draw_info->affine.sy*(height+
530  metrics.underline_position+metrics.descent*2)/2.0);
531  (void) CloneString(&annotate_info->primitive,primitive);
532  (void) DrawImage(image,annotate_info);
533  }
534  }
535  /*
536  Relinquish resources.
537  */
538  annotate_info=DestroyDrawInfo(annotate_info);
539  annotate=DestroyDrawInfo(annotate);
540  textlist=(char **) RelinquishMagickMemory(textlist);
541  text=DestroyString(text);
542  return(status);
543 }
544 
545 /*
546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 % %
548 % %
549 % %
550 % F o r m a t M a g i c k C a p t i o n %
551 % %
552 % %
553 % %
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555 %
556 % FormatMagickCaption() formats a caption so that it fits within the image
557 % width. It returns the number of lines in the formatted caption.
558 %
559 % The format of the FormatMagickCaption method is:
560 %
561 % ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
562 % const MagickBooleanType split,TypeMetric *metrics,char **caption)
563 %
564 % A description of each parameter follows.
565 %
566 % o image: The image.
567 %
568 % o draw_info: the draw info.
569 %
570 % o split: when no convenient line breaks-- insert newline.
571 %
572 % o metrics: Return the font metrics in this structure.
573 %
574 % o caption: the caption.
575 %
576 */
577 
578 static inline char *ReplaceSpaceWithNewline(char **caption,char *space)
579 {
580  size_t
581  octets;
582 
583  octets=(size_t) GetUTFOctets(space);
584  if (octets == 1)
585  *space='\n';
586  else
587  {
588  char
589  *target;
590 
591  size_t
592  length,
593  offset;
594 
595  length=strlen(*caption);
596  *space='\n';
597  offset=space-(*caption);
598  target=AcquireString(*caption);
599  CopyMagickString(target,*caption,offset+2);
600  ConcatenateMagickString(target,space+octets,length);
601  (void) DestroyString(*caption);
602  *caption=target;
603  space=(*caption)+offset;
604  }
605  return(space);
606 }
607 
608 MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
609  const MagickBooleanType split,TypeMetric *metrics,char **caption)
610 {
611  MagickBooleanType
612  status;
613 
614  char
615  *p,
616  *q,
617  *s;
618 
619  ssize_t
620  i;
621 
622  size_t
623  width;
624 
625  ssize_t
626  n;
627 
628  q=draw_info->text;
629  s=(char *) NULL;
630  width=0;
631  for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
632  {
633  int
634  code;
635 
636  code=GetUTFCode(p);
637  if (code == '\n')
638  {
639  q=draw_info->text;
640  continue;
641  }
642  if ((IsUTFSpace(code) != MagickFalse) &&
643  (IsNonBreakingUTFSpace(code) == MagickFalse))
644  {
645  s=p;
646  if (width > image->columns)
647  p=ReplaceSpaceWithNewline(caption,s);
648  }
649  for (i=0; i < (ssize_t) GetUTFOctets(p); i++)
650  *q++=(*(p+i));
651  *q='\0';
652  status=GetTypeMetrics(image,draw_info,metrics);
653  if (status == MagickFalse)
654  break;
655  width=(size_t) floor(metrics->width+draw_info->stroke_width+0.5);
656  if (width <= image->columns)
657  continue;
658  if (s != (char *) NULL)
659  p=ReplaceSpaceWithNewline(caption,s);
660  else
661  if ((split != MagickFalse) || (GetUTFOctets(p) > 2))
662  {
663  /*
664  No convenient line breaks-- insert newline.
665  */
666  n=p-(*caption);
667  if ((n > 0) && ((*caption)[n-1] != '\n'))
668  {
669  char
670  *target;
671 
672  target=AcquireString(*caption);
673  CopyMagickString(target,*caption,n+1);
674  ConcatenateMagickString(target,"\n",strlen(*caption)+1);
675  ConcatenateMagickString(target,p,strlen(*caption)+2);
676  (void) DestroyString(*caption);
677  *caption=target;
678  p=(*caption)+n;
679  }
680  }
681  q=draw_info->text;
682  s=(char *) NULL;
683  }
684  n=0;
685  for (p=(*caption); GetUTFCode(p) != 0; p+=GetUTFOctets(p))
686  if (GetUTFCode(p) == '\n')
687  n++;
688  return(n);
689 }
690 
691 /*
692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
693 % %
694 % %
695 % %
696 % G e t M u l t i l i n e T y p e M e t r i c s %
697 % %
698 % %
699 % %
700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
701 %
702 % GetMultilineTypeMetrics() returns the following information for the
703 % specified font and text:
704 %
705 % character width
706 % character height
707 % ascender
708 % descender
709 % text width
710 % text height
711 % maximum horizontal advance
712 % bounds: x1
713 % bounds: y1
714 % bounds: x2
715 % bounds: y2
716 % origin: x
717 % origin: y
718 % underline position
719 % underline thickness
720 %
721 % This method is like GetTypeMetrics() but it returns the maximum text width
722 % and height for multiple lines of text.
723 %
724 % The format of the GetMultilineTypeMetrics method is:
725 %
726 % MagickBooleanType GetMultilineTypeMetrics(Image *image,
727 % const DrawInfo *draw_info,TypeMetric *metrics)
728 %
729 % A description of each parameter follows:
730 %
731 % o image: the image.
732 %
733 % o draw_info: the draw info.
734 %
735 % o metrics: Return the font metrics in this structure.
736 %
737 */
738 MagickExport MagickBooleanType GetMultilineTypeMetrics(Image *image,
739  const DrawInfo *draw_info,TypeMetric *metrics)
740 {
741  char
742  **textlist;
743 
744  double
745  height;
746 
747  DrawInfo
748  *annotate_info;
749 
750  MagickBooleanType
751  status;
752 
753  MagickSizeType
754  size;
755 
756  ssize_t
757  i;
758 
759  size_t
760  count;
761 
762  TypeMetric
763  extent;
764 
765  assert(image != (Image *) NULL);
766  assert(image->signature == MagickCoreSignature);
767  if (IsEventLogging() != MagickFalse)
768  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
769  assert(draw_info != (DrawInfo *) NULL);
770  assert(draw_info->text != (char *) NULL);
771  assert(draw_info->signature == MagickCoreSignature);
772  if (*draw_info->text == '\0')
773  return(MagickFalse);
774  annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
775  annotate_info->text=DestroyString(annotate_info->text);
776  /*
777  Convert newlines to multiple lines of text.
778  */
779  textlist=StringToStrings(draw_info->text,&count);
780  if (textlist == (char **) NULL)
781  return(MagickFalse);
782  annotate_info->render=MagickFalse;
783  annotate_info->direction=UndefinedDirection;
784  (void) memset(metrics,0,sizeof(*metrics));
785  (void) memset(&extent,0,sizeof(extent));
786  /*
787  Find the widest of the text lines.
788  */
789  annotate_info->text=textlist[0];
790  status=GetTypeMetrics(image,annotate_info,&extent);
791  *metrics=extent;
792  height=(count*(size_t) (metrics->ascent-metrics->descent+
793  0.5)+(count-1)*draw_info->interline_spacing);
794  size=(MagickSizeType) fabs(height);
795  if (AcquireMagickResource(HeightResource,size) == MagickFalse)
796  {
797  (void) ThrowMagickException(&image->exception,GetMagickModule(),
798  ImageError,"WidthOrHeightExceedsLimit","`%s'",image->filename);
799  status=MagickFalse;
800  }
801  else
802  {
803  for (i=1; i < (ssize_t) count; i++)
804  {
805  annotate_info->text=textlist[i];
806  status=GetTypeMetrics(image,annotate_info,&extent);
807  if (status == MagickFalse)
808  break;
809  if (extent.width > metrics->width)
810  *metrics=extent;
811  size=(MagickSizeType) fabs(extent.width);
812  if (AcquireMagickResource(WidthResource,size) == MagickFalse)
813  {
814  (void) ThrowMagickException(&image->exception,GetMagickModule(),
815  ImageError,"WidthOrHeightExceedsLimit","`%s'",image->filename);
816  status=MagickFalse;
817  break;
818  }
819  }
820  metrics->height=(double) height;
821  }
822  /*
823  Relinquish resources.
824  */
825  annotate_info->text=(char *) NULL;
826  annotate_info=DestroyDrawInfo(annotate_info);
827  for (i=0; i < (ssize_t) count; i++)
828  textlist[i]=DestroyString(textlist[i]);
829  textlist=(char **) RelinquishMagickMemory(textlist);
830  return(status);
831 }
832 
833 /*
834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
835 % %
836 % %
837 % %
838 % G e t T y p e M e t r i c s %
839 % %
840 % %
841 % %
842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
843 %
844 % GetTypeMetrics() returns the following information for the specified font
845 % and text:
846 %
847 % character width
848 % character height
849 % ascender
850 % descender
851 % text width
852 % text height
853 % maximum horizontal advance
854 % bounds: x1
855 % bounds: y1
856 % bounds: x2
857 % bounds: y2
858 % origin: x
859 % origin: y
860 % underline position
861 % underline thickness
862 %
863 % The format of the GetTypeMetrics method is:
864 %
865 % MagickBooleanType GetTypeMetrics(Image *image,const DrawInfo *draw_info,
866 % TypeMetric *metrics)
867 %
868 % A description of each parameter follows:
869 %
870 % o image: the image.
871 %
872 % o draw_info: the draw info.
873 %
874 % o metrics: Return the font metrics in this structure.
875 %
876 */
877 MagickExport MagickBooleanType GetTypeMetrics(Image *image,
878  const DrawInfo *draw_info,TypeMetric *metrics)
879 {
880  DrawInfo
881  *annotate_info;
882 
883  MagickBooleanType
884  status;
885 
886  PointInfo
887  offset;
888 
889  assert(image != (Image *) NULL);
890  assert(image->signature == MagickCoreSignature);
891  if (IsEventLogging() != MagickFalse)
892  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
893  assert(draw_info != (DrawInfo *) NULL);
894  assert(draw_info->text != (char *) NULL);
895  assert(draw_info->signature == MagickCoreSignature);
896  annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
897  annotate_info->render=MagickFalse;
898  annotate_info->direction=UndefinedDirection;
899  (void) memset(metrics,0,sizeof(*metrics));
900  offset.x=0.0;
901  offset.y=0.0;
902  status=RenderType(image,annotate_info,&offset,metrics);
903  if (draw_info->debug != MagickFalse)
904  (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Metrics: text: %s; "
905  "width: %g; height: %g; ascent: %g; descent: %g; max advance: %g; "
906  "bounds: %g,%g %g,%g; origin: %g,%g; pixels per em: %g,%g; "
907  "underline position: %g; underline thickness: %g",annotate_info->text,
908  metrics->width,metrics->height,metrics->ascent,metrics->descent,
909  metrics->max_advance,metrics->bounds.x1,metrics->bounds.y1,
910  metrics->bounds.x2,metrics->bounds.y2,metrics->origin.x,metrics->origin.y,
911  metrics->pixels_per_em.x,metrics->pixels_per_em.y,
912  metrics->underline_position,metrics->underline_thickness);
913  annotate_info=DestroyDrawInfo(annotate_info);
914  return(status);
915 }
916 
917 /*
918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 % %
920 % %
921 % %
922 + R e n d e r T y p e %
923 % %
924 % %
925 % %
926 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 %
928 % RenderType() renders text on the image. It also returns the bounding box of
929 % the text relative to the image.
930 %
931 % The format of the RenderType method is:
932 %
933 % MagickBooleanType RenderType(Image *image,DrawInfo *draw_info,
934 % const PointInfo *offset,TypeMetric *metrics)
935 %
936 % A description of each parameter follows:
937 %
938 % o image: the image.
939 %
940 % o draw_info: the draw info.
941 %
942 % o offset: (x,y) location of text relative to image.
943 %
944 % o metrics: bounding box of text.
945 %
946 */
947 static MagickBooleanType RenderType(Image *image,const DrawInfo *draw_info,
948  const PointInfo *offset,TypeMetric *metrics)
949 {
950  char
951  *font;
952 
953  const TypeInfo
954  *type_info;
955 
956  DrawInfo
957  *annotate_info;
958 
960  *sans_exception;
961 
962  MagickBooleanType
963  status;
964 
965  type_info=(const TypeInfo *) NULL;
966  if (draw_info->font != (char *) NULL)
967  {
968  if (*draw_info->font == '@')
969  {
970  status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
971  metrics);
972  return(status);
973  }
974  if (*draw_info->font == '-')
975  return(RenderX11(image,draw_info,offset,metrics));
976  if (*draw_info->font == '^')
977  return(RenderPostscript(image,draw_info,offset,metrics));
978  if (IsPathAccessible(draw_info->font) != MagickFalse)
979  {
980  status=RenderFreetype(image,draw_info,draw_info->encoding,offset,
981  metrics);
982  return(status);
983  }
984  type_info=GetTypeInfo(draw_info->font,&image->exception);
985  if (type_info == (const TypeInfo *) NULL)
986  (void) ThrowMagickException(&image->exception,GetMagickModule(),
987  TypeWarning,"UnableToReadFont","`%s'",draw_info->font);
988  }
989  if ((type_info == (const TypeInfo *) NULL) &&
990  (draw_info->family != (const char *) NULL))
991  {
992  if (strstr(draw_info->family,",'\"") == (char *) NULL)
993  type_info=GetTypeInfoByFamily(draw_info->family,draw_info->style,
994  draw_info->stretch,draw_info->weight,&image->exception);
995  if (type_info == (const TypeInfo *) NULL)
996  {
997  char
998  **family;
999 
1000  int
1001  number_families;
1002 
1003  ssize_t
1004  i;
1005 
1006  /*
1007  Parse font family list.
1008  */
1009  family=StringToArgv(draw_info->family,&number_families);
1010  for (i=1; i < (ssize_t) number_families; i++)
1011  {
1012  (void) SubstituteString(&family[i],",","");
1013  type_info=GetTypeInfoByFamily(family[i],draw_info->style,
1014  draw_info->stretch,draw_info->weight,&image->exception);
1015  if ((type_info != (const TypeInfo *) NULL) &&
1016  (LocaleCompare(family[i],type_info->family) == 0))
1017  break;
1018  }
1019  for (i=0; i < (ssize_t) number_families; i++)
1020  family[i]=DestroyString(family[i]);
1021  family=(char **) RelinquishMagickMemory(family);
1022  if (type_info == (const TypeInfo *) NULL)
1023  (void) ThrowMagickException(&image->exception,GetMagickModule(),
1024  TypeWarning,"UnableToReadFont","`%s'",draw_info->family);
1025  }
1026  }
1027  font=GetPolicyValue("system:font");
1028  if (font != (char *) NULL)
1029  {
1030  if (IsPathAccessible(font) != MagickFalse)
1031  {
1032  /*
1033  Render with default system font.
1034  */
1035  annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1036  annotate_info->font=font;
1037  status=RenderFreetype(image,annotate_info,annotate_info->encoding,
1038  offset,metrics);
1039  annotate_info=DestroyDrawInfo(annotate_info);
1040  return(status);
1041  }
1042  font=DestroyString(font);
1043  }
1044  sans_exception=AcquireExceptionInfo();
1045  if (type_info == (const TypeInfo *) NULL)
1046  type_info=GetTypeInfoByFamily("Open Sans",draw_info->style,
1047  draw_info->stretch,draw_info->weight,sans_exception);
1048  if (type_info == (const TypeInfo *) NULL)
1049  type_info=GetTypeInfoByFamily("Sans Serif",draw_info->style,
1050  draw_info->stretch,draw_info->weight,sans_exception);
1051  if (type_info == (const TypeInfo *) NULL)
1052  type_info=GetTypeInfoByFamily((const char *) NULL,draw_info->style,
1053  draw_info->stretch,draw_info->weight,sans_exception);
1054  if (type_info == (const TypeInfo *) NULL)
1055  type_info=GetTypeInfo("*",sans_exception);
1056  sans_exception=DestroyExceptionInfo(sans_exception);
1057  if (type_info == (const TypeInfo *) NULL)
1058  {
1059  status=RenderFreetype(image,draw_info,draw_info->encoding,offset,metrics);
1060  return(status);
1061  }
1062  annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1063  annotate_info->face=type_info->face;
1064  if (type_info->metrics != (char *) NULL)
1065  (void) CloneString(&annotate_info->metrics,type_info->metrics);
1066  if (type_info->glyphs != (char *) NULL)
1067  (void) CloneString(&annotate_info->font,type_info->glyphs);
1068  status=RenderFreetype(image,annotate_info,type_info->encoding,offset,metrics);
1069  annotate_info=DestroyDrawInfo(annotate_info);
1070  return(status);
1071 }
1072 
1073 /*
1074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1075 % %
1076 % %
1077 % %
1078 + R e n d e r F r e e t y p e %
1079 % %
1080 % %
1081 % %
1082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1083 %
1084 % RenderFreetype() renders text on the image with a Truetype font. It also
1085 % returns the bounding box of the text relative to the image.
1086 %
1087 % The format of the RenderFreetype method is:
1088 %
1089 % MagickBooleanType RenderFreetype(Image *image,DrawInfo *draw_info,
1090 % const char *encoding,const PointInfo *offset,TypeMetric *metrics)
1091 %
1092 % A description of each parameter follows:
1093 %
1094 % o image: the image.
1095 %
1096 % o draw_info: the draw info.
1097 %
1098 % o encoding: the font encoding.
1099 %
1100 % o offset: (x,y) location of text relative to image.
1101 %
1102 % o metrics: bounding box of text.
1103 %
1104 */
1105 
1106 #if defined(MAGICKCORE_FREETYPE_DELEGATE)
1107 
1108 static size_t ComplexTextLayout(const Image *image,const DrawInfo *draw_info,
1109  const char *text,const size_t length,const FT_Face face,const FT_Int32 flags,
1110  GraphemeInfo **grapheme)
1111 {
1112 #if defined(MAGICKCORE_RAQM_DELEGATE)
1113  const char
1114  *features;
1115 
1116  raqm_t
1117  *rq;
1118 
1119  raqm_glyph_t
1120  *glyphs;
1121 
1122  ssize_t
1123  i;
1124 
1125  size_t
1126  extent;
1127 
1128  magick_unreferenced(flags);
1129  extent=0;
1130  rq=raqm_create();
1131  if (rq == (raqm_t *) NULL)
1132  goto cleanup;
1133  if (raqm_set_text_utf8(rq,text,length) == 0)
1134  goto cleanup;
1135  if (raqm_set_par_direction(rq,(raqm_direction_t) draw_info->direction) == 0)
1136  goto cleanup;
1137  if (raqm_set_freetype_face(rq,face) == 0)
1138  goto cleanup;
1139  features=GetImageProperty(image,"type:features");
1140  if (features != (const char *) NULL)
1141  {
1142  char
1143  breaker,
1144  quote,
1145  *token;
1146 
1147  int
1148  next,
1149  status_token;
1150 
1151  TokenInfo
1152  *token_info;
1153 
1154  next=0;
1155  token_info=AcquireTokenInfo();
1156  token=AcquireString("");
1157  status_token=Tokenizer(token_info,0,token,50,features,"",",","",'\0',
1158  &breaker,&next,&quote);
1159  while (status_token == 0)
1160  {
1161  raqm_add_font_feature(rq,token,strlen(token));
1162  status_token=Tokenizer(token_info,0,token,50,features,"",",","",'\0',
1163  &breaker,&next,&quote);
1164  }
1165  token_info=DestroyTokenInfo(token_info);
1166  token=DestroyString(token);
1167  }
1168  if (raqm_layout(rq) == 0)
1169  goto cleanup;
1170  glyphs=raqm_get_glyphs(rq,&extent);
1171  if (glyphs == (raqm_glyph_t *) NULL)
1172  {
1173  extent=0;
1174  goto cleanup;
1175  }
1176  *grapheme=(GraphemeInfo *) AcquireQuantumMemory(extent,sizeof(**grapheme));
1177  if (*grapheme == (GraphemeInfo *) NULL)
1178  {
1179  extent=0;
1180  goto cleanup;
1181  }
1182  for (i=0; i < (ssize_t) extent; i++)
1183  {
1184  (*grapheme)[i].index=glyphs[i].index;
1185  (*grapheme)[i].x_offset=glyphs[i].x_offset;
1186  (*grapheme)[i].x_advance=glyphs[i].x_advance;
1187  (*grapheme)[i].y_offset=glyphs[i].y_offset;
1188  (*grapheme)[i].cluster=glyphs[i].cluster;
1189  }
1190 
1191 cleanup:
1192  raqm_destroy(rq);
1193  return(extent);
1194 #else
1195  const char
1196  *p;
1197 
1198  ssize_t
1199  i;
1200 
1201  ssize_t
1202  last_glyph;
1203 
1204  /*
1205  Simple layout for bi-directional text (right-to-left or left-to-right).
1206  */
1207  magick_unreferenced(image);
1208  *grapheme=(GraphemeInfo *) AcquireQuantumMemory(length+1,sizeof(**grapheme));
1209  if (*grapheme == (GraphemeInfo *) NULL)
1210  return(0);
1211  last_glyph=0;
1212  p=text;
1213  for (i=0; GetUTFCode(p) != 0; p+=GetUTFOctets(p), i++)
1214  {
1215  (*grapheme)[i].index=FT_Get_Char_Index(face,GetUTFCode(p));
1216  (*grapheme)[i].x_offset=0;
1217  (*grapheme)[i].y_offset=0;
1218  if (((*grapheme)[i].index != 0) && (last_glyph != 0))
1219  {
1220  if (FT_HAS_KERNING(face))
1221  {
1222  FT_Error
1223  ft_status;
1224 
1225  FT_Vector
1226  kerning;
1227 
1228  ft_status=FT_Get_Kerning(face,(FT_UInt) last_glyph,(FT_UInt)
1229  (*grapheme)[i].index,ft_kerning_default,&kerning);
1230  if (ft_status == 0)
1231  (*grapheme)[i-1].x_advance+=(FT_Pos) ((draw_info->direction ==
1232  RightToLeftDirection ? -1.0 : 1.0)*kerning.x);
1233  }
1234  }
1235  (void) FT_Load_Glyph(face,(*grapheme)[i].index,flags);
1236  (*grapheme)[i].x_advance=face->glyph->advance.x;
1237  (*grapheme)[i].cluster=p-text;
1238  last_glyph=(*grapheme)[i].index;
1239  }
1240  return((size_t) i);
1241 #endif
1242 }
1243 
1244 static void FTCloseStream(FT_Stream stream)
1245 {
1246  FILE *file = (FILE *) stream->descriptor.pointer;
1247  if (file != (FILE *) NULL)
1248  (void) fclose(file);
1249  stream->descriptor.pointer=NULL;
1250 }
1251 
1252 static unsigned long FTReadStream(FT_Stream stream,unsigned long offset,
1253  unsigned char *buffer,unsigned long count)
1254 {
1255  FILE *file = (FILE *) stream->descriptor.pointer;
1256  if (file == (FILE *) NULL)
1257  return(0);
1258  if (count == 0)
1259  return(0);
1260  if (fseek(file,(off_t) offset,SEEK_SET) != 0)
1261  return(0);
1262  return((unsigned long) fread(buffer,1,count,file));
1263 }
1264 
1265 static inline MagickBooleanType IsEmptyOutline(FT_Outline outline)
1266 {
1267  return((outline.n_points == 0) || (outline.n_contours <= 0) ? MagickTrue :
1268  MagickFalse);
1269 }
1270 
1271 static int TraceCubicBezier(FT_Vector *p,FT_Vector *q,FT_Vector *to,
1272  DrawInfo *draw_info)
1273 {
1274  AffineMatrix
1275  affine;
1276 
1277  char
1278  path[MaxTextExtent];
1279 
1280  affine=draw_info->affine;
1281  (void) FormatLocaleString(path,MaxTextExtent,"C%g,%g %g,%g %g,%g",affine.tx+
1282  p->x/64.0,affine.ty-p->y/64.0,affine.tx+q->x/64.0,affine.ty-q->y/64.0,
1283  affine.tx+to->x/64.0,affine.ty-to->y/64.0);
1284  (void) ConcatenateString(&draw_info->primitive,path);
1285  return(0);
1286 }
1287 
1288 static int TraceLineTo(FT_Vector *to,DrawInfo *draw_info)
1289 {
1290  AffineMatrix
1291  affine;
1292 
1293  char
1294  path[MaxTextExtent];
1295 
1296  affine=draw_info->affine;
1297  (void) FormatLocaleString(path,MaxTextExtent,"L%g,%g",affine.tx+to->x/64.0,
1298  affine.ty-to->y/64.0);
1299  (void) ConcatenateString(&draw_info->primitive,path);
1300  return(0);
1301 }
1302 
1303 static int TraceMoveTo(FT_Vector *to,DrawInfo *draw_info)
1304 {
1305  AffineMatrix
1306  affine;
1307 
1308  char
1309  path[MaxTextExtent];
1310 
1311  affine=draw_info->affine;
1312  (void) FormatLocaleString(path,MaxTextExtent,"M%g,%g",affine.tx+to->x/64.0,
1313  affine.ty-to->y/64.0);
1314  (void) ConcatenateString(&draw_info->primitive,path);
1315  return(0);
1316 }
1317 
1318 static int TraceQuadraticBezier(FT_Vector *control,FT_Vector *to,
1319  DrawInfo *draw_info)
1320 {
1321  AffineMatrix
1322  affine;
1323 
1324  char
1325  path[MaxTextExtent];
1326 
1327  affine=draw_info->affine;
1328  (void) FormatLocaleString(path,MaxTextExtent,"Q%g,%g %g,%g",affine.tx+
1329  control->x/64.0,affine.ty-control->y/64.0,affine.tx+to->x/64.0,affine.ty-
1330  to->y/64.0);
1331  (void) ConcatenateString(&draw_info->primitive,path);
1332  return(0);
1333 }
1334 
1335 static inline const char *FreetypeErrorMessage(FT_Error ft_status)
1336 {
1337 #if FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 10
1338  return(FT_Error_String(ft_status));
1339 #else
1340  magick_unreferenced(ft_status);
1341  return((const char *) NULL);
1342 #endif
1343 }
1344 
1345 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1346  const char *encoding,const PointInfo *offset,TypeMetric *metrics)
1347 {
1348 #if !defined(FT_OPEN_PATHNAME)
1349 #define FT_OPEN_PATHNAME ft_open_pathname
1350 #endif
1351 
1352 #define ThrowFreetypeErrorException(tag,ft_status,value) \
1353 { \
1354  const char \
1355  *error_string=FreetypeErrorMessage(ft_status); \
1356  if (error_string != (const char *) NULL) \
1357  (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1358  tag,"`%s (%s)'",value, error_string); \
1359  else \
1360  (void) ThrowMagickException(exception,GetMagickModule(),TypeError, \
1361  tag,"`%s'",value); \
1362 }
1363 
1364  typedef struct _GlyphInfo
1365  {
1366  FT_UInt
1367  id;
1368 
1369  FT_Vector
1370  origin;
1371 
1372  FT_Glyph
1373  image;
1374  } GlyphInfo;
1375 
1376  char
1377  *p;
1378 
1379  const char
1380  *value;
1381 
1382  DrawInfo
1383  *annotate_info;
1384 
1386  *exception;
1387 
1388  FT_BBox
1389  bounds;
1390 
1391  FT_Encoding
1392  encoding_type;
1393 
1394  FT_Error
1395  ft_status;
1396 
1397  FT_Face
1398  face;
1399 
1400  FT_Int32
1401  flags;
1402 
1403  FT_Library
1404  library;
1405 
1406  FT_Long
1407  face_index;
1408 
1409  FT_Matrix
1410  affine;
1411 
1412  FT_Open_Args
1413  args;
1414 
1415  FT_StreamRec
1416  *stream;
1417 
1418  FT_UInt
1419  first_glyph_id,
1420  last_glyph_id,
1421  missing_glyph_id;
1422 
1423  FT_Vector
1424  origin;
1425 
1426  GlyphInfo
1427  glyph;
1428 
1429  GraphemeInfo
1430  *grapheme;
1431 
1432  MagickBooleanType
1433  status;
1434 
1435  PointInfo
1436  resolution;
1437 
1438  ssize_t
1439  i;
1440 
1441  size_t
1442  length;
1443 
1444  ssize_t
1445  code,
1446  last_character,
1447  y;
1448 
1449  static FT_Outline_Funcs
1450  OutlineMethods =
1451  {
1452  (FT_Outline_MoveTo_Func) TraceMoveTo,
1453  (FT_Outline_LineTo_Func) TraceLineTo,
1454  (FT_Outline_ConicTo_Func) TraceQuadraticBezier,
1455  (FT_Outline_CubicTo_Func) TraceCubicBezier,
1456  0, 0
1457  };
1458 
1459  struct stat
1460  attributes;
1461 
1462  unsigned char
1463  *utf8;
1464 
1465  /*
1466  Initialize Truetype library.
1467  */
1468  exception=(&image->exception);
1469  ft_status=FT_Init_FreeType(&library);
1470  if (ft_status != 0)
1471  ThrowFreetypeErrorException("UnableToInitializeFreetypeLibrary",ft_status,
1472  image->filename);
1473  face_index=(FT_Long) draw_info->face;
1474  /*
1475  Open font face.
1476  */
1477  (void) memset(&args,0,sizeof(args));
1478  if (draw_info->font == (char *) NULL)
1479  args.pathname=ConstantString("helvetica");
1480  else
1481  if (*draw_info->font != '@')
1482  args.pathname=ConstantString(draw_info->font);
1483  else
1484  {
1485  /*
1486  Extract face index, e.g. @msgothic[1].
1487  */
1488  ImageInfo *image_info=AcquireImageInfo();
1489  (void) strcpy(image_info->filename,draw_info->font+1);
1490  (void) SetImageInfo(image_info,0,exception);
1491  face_index=(FT_Long) image_info->scene;
1492  args.pathname=ConstantString(image_info->filename);
1493  image_info=DestroyImageInfo(image_info);
1494  }
1495  /*
1496  Configure streaming interface.
1497  */
1498  stream=(FT_StreamRec *) AcquireCriticalMemory(sizeof(*stream));
1499  (void) memset(stream,0,sizeof(*stream));
1500  (void) stat(args.pathname,&attributes);
1501  stream->size=attributes.st_size;
1502  stream->descriptor.pointer=fopen_utf8(args.pathname,"rb");
1503  stream->read=(&FTReadStream);
1504  stream->close=(&FTCloseStream);
1505  args.flags=FT_OPEN_STREAM;
1506  args.stream=stream;
1507  face=(FT_Face) NULL;
1508  ft_status=FT_Open_Face(library,&args,face_index,&face);
1509  if (ft_status != 0)
1510  {
1511  stream=RelinquishMagickMemory(stream);
1512  (void) FT_Done_FreeType(library);
1513  ThrowFreetypeErrorException("UnableToReadFont",ft_status,args.pathname);
1514  args.pathname=DestroyString(args.pathname);
1515  return(MagickFalse);
1516  }
1517  args.pathname=DestroyString(args.pathname);
1518  if ((draw_info->metrics != (char *) NULL) &&
1519  (IsPathAccessible(draw_info->metrics) != MagickFalse))
1520  (void) FT_Attach_File(face,draw_info->metrics);
1521  encoding_type=FT_ENCODING_UNICODE;
1522  ft_status=FT_Select_Charmap(face,encoding_type);
1523  if ((ft_status != 0) && (face->num_charmaps != 0))
1524  ft_status=FT_Set_Charmap(face,face->charmaps[0]);
1525  if (encoding != (const char *) NULL)
1526  {
1527  if (LocaleCompare(encoding,"AdobeCustom") == 0)
1528  encoding_type=FT_ENCODING_ADOBE_CUSTOM;
1529  if (LocaleCompare(encoding,"AdobeExpert") == 0)
1530  encoding_type=FT_ENCODING_ADOBE_EXPERT;
1531  if (LocaleCompare(encoding,"AdobeStandard") == 0)
1532  encoding_type=FT_ENCODING_ADOBE_STANDARD;
1533  if (LocaleCompare(encoding,"AppleRoman") == 0)
1534  encoding_type=FT_ENCODING_APPLE_ROMAN;
1535  if (LocaleCompare(encoding,"BIG5") == 0)
1536  encoding_type=FT_ENCODING_BIG5;
1537 #if defined(FT_ENCODING_PRC)
1538  if (LocaleCompare(encoding,"GB2312") == 0)
1539  encoding_type=FT_ENCODING_PRC;
1540 #endif
1541 #if defined(FT_ENCODING_JOHAB)
1542  if (LocaleCompare(encoding,"Johab") == 0)
1543  encoding_type=FT_ENCODING_JOHAB;
1544 #endif
1545 #if defined(FT_ENCODING_ADOBE_LATIN_1)
1546  if (LocaleCompare(encoding,"Latin-1") == 0)
1547  encoding_type=FT_ENCODING_ADOBE_LATIN_1;
1548 #endif
1549 #if defined(FT_ENCODING_ADOBE_LATIN_2)
1550  if (LocaleCompare(encoding,"Latin-2") == 0)
1551  encoding_type=FT_ENCODING_OLD_LATIN_2;
1552 #endif
1553  if (LocaleCompare(encoding,"None") == 0)
1554  encoding_type=FT_ENCODING_NONE;
1555  if (LocaleCompare(encoding,"SJIScode") == 0)
1556  encoding_type=FT_ENCODING_SJIS;
1557  if (LocaleCompare(encoding,"Symbol") == 0)
1558  encoding_type=FT_ENCODING_MS_SYMBOL;
1559  if (LocaleCompare(encoding,"Unicode") == 0)
1560  encoding_type=FT_ENCODING_UNICODE;
1561  if (LocaleCompare(encoding,"Wansung") == 0)
1562  encoding_type=FT_ENCODING_WANSUNG;
1563  ft_status=FT_Select_Charmap(face,encoding_type);
1564  if (ft_status != 0)
1565  {
1566  (void) FT_Done_Face(face);
1567  (void) FT_Done_FreeType(library);
1568  ThrowFreetypeErrorException("UnrecognizedFontEncoding",ft_status,
1569  encoding);
1570  return(MagickFalse);
1571  }
1572  }
1573  /*
1574  Set text size.
1575  */
1576  resolution.x=DefaultResolution;
1577  resolution.y=DefaultResolution;
1578  if (draw_info->density != (char *) NULL)
1579  {
1580  GeometryInfo
1581  geometry_info;
1582 
1583  MagickStatusType
1584  flags;
1585 
1586  flags=ParseGeometry(draw_info->density,&geometry_info);
1587  if ((flags & RhoValue) != 0)
1588  resolution.x=geometry_info.rho;
1589  resolution.y=resolution.x;
1590  if ((flags & SigmaValue) != 0)
1591  resolution.y=geometry_info.sigma;
1592  }
1593  ft_status=FT_Set_Char_Size(face,(FT_F26Dot6) (64.0*draw_info->pointsize),
1594  (FT_F26Dot6) (64.0*draw_info->pointsize),(FT_UInt) resolution.x,
1595  (FT_UInt) resolution.y);
1596  if (ft_status != 0)
1597  {
1598  (void) FT_Done_Face(face);
1599  (void) FT_Done_FreeType(library);
1600  ThrowFreetypeErrorException("UnableToReadFont",ft_status,
1601  draw_info->font);
1602  return(MagickFalse);
1603  }
1604  metrics->pixels_per_em.x=face->size->metrics.x_ppem;
1605  metrics->pixels_per_em.y=face->size->metrics.y_ppem;
1606  metrics->ascent=(double) face->size->metrics.ascender/64.0;
1607  metrics->descent=(double) face->size->metrics.descender/64.0;
1608  if (face->size->metrics.ascender == 0)
1609  {
1610  /*
1611  Sanitize buggy ascender and descender values.
1612  */
1613  metrics->ascent=face->size->metrics.y_ppem;
1614  if (face->size->metrics.descender == 0)
1615  metrics->descent=face->size->metrics.y_ppem/-3.5;
1616  }
1617  metrics->width=0;
1618  metrics->origin.x=0;
1619  metrics->origin.y=0;
1620  metrics->height=(double) face->size->metrics.height/64.0;
1621  metrics->max_advance=0.0;
1622  if (face->size->metrics.max_advance > MagickEpsilon)
1623  metrics->max_advance=(double) face->size->metrics.max_advance/64.0;
1624  metrics->bounds.x1=0.0;
1625  metrics->bounds.y1=metrics->descent;
1626  metrics->bounds.x2=metrics->ascent+metrics->descent;
1627  metrics->bounds.y2=metrics->ascent+metrics->descent;
1628  metrics->underline_position=face->underline_position*
1629  (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1630  metrics->underline_thickness=face->underline_thickness*
1631  (metrics->pixels_per_em.x*PerceptibleReciprocal(face->units_per_EM));
1632  first_glyph_id=0;
1633  FT_Get_First_Char(face,&first_glyph_id);
1634  if ((draw_info->text == (char *) NULL) || (*draw_info->text == '\0') ||
1635  (first_glyph_id == 0))
1636  {
1637  (void) FT_Done_Face(face);
1638  (void) FT_Done_FreeType(library);
1639  return(MagickTrue);
1640  }
1641  /*
1642  Compute bounding box.
1643  */
1644  if (draw_info->debug != MagickFalse)
1645  (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),"Font %s; "
1646  "font-encoding %s; text-encoding %s; pointsize %g",
1647  draw_info->font != (char *) NULL ? draw_info->font : "none",
1648  encoding != (char *) NULL ? encoding : "none",
1649  draw_info->encoding != (char *) NULL ? draw_info->encoding : "none",
1650  draw_info->pointsize);
1651  flags=FT_LOAD_DEFAULT;
1652  if (draw_info->render == MagickFalse)
1653  flags=FT_LOAD_NO_BITMAP;
1654  if (draw_info->text_antialias == MagickFalse)
1655  flags|=FT_LOAD_TARGET_MONO;
1656  else
1657  {
1658 #if defined(FT_LOAD_TARGET_LIGHT)
1659  flags|=FT_LOAD_TARGET_LIGHT;
1660 #elif defined(FT_LOAD_TARGET_LCD)
1661  flags|=FT_LOAD_TARGET_LCD;
1662 #endif
1663  }
1664  value=GetImageProperty(image,"type:hinting");
1665  if ((value != (const char *) NULL) && (LocaleCompare(value,"off") == 0))
1666  flags|=FT_LOAD_NO_HINTING;
1667  glyph.id=0;
1668  glyph.image=(FT_Glyph) NULL;
1669  last_glyph_id=0;
1670  origin.x=0;
1671  origin.y=0;
1672  affine.xx=65536L;
1673  affine.yx=0L;
1674  affine.xy=0L;
1675  affine.yy=65536L;
1676  if (draw_info->render != MagickFalse)
1677  {
1678  affine.xx=(FT_Fixed) (65536L*draw_info->affine.sx+0.5);
1679  affine.yx=(FT_Fixed) (-65536L*draw_info->affine.rx+0.5);
1680  affine.xy=(FT_Fixed) (-65536L*draw_info->affine.ry+0.5);
1681  affine.yy=(FT_Fixed) (65536L*draw_info->affine.sy+0.5);
1682  }
1683  annotate_info=CloneDrawInfo((ImageInfo *) NULL,draw_info);
1684  if (annotate_info->dash_pattern != (double *) NULL)
1685  annotate_info->dash_pattern[0]=0.0;
1686  (void) CloneString(&annotate_info->primitive,"path '");
1687  status=MagickTrue;
1688  if (draw_info->render != MagickFalse)
1689  {
1690  if (image->storage_class != DirectClass)
1691  (void) SetImageStorageClass(image,DirectClass);
1692  if (image->matte == MagickFalse)
1693  (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
1694  }
1695  for (p=draw_info->text; GetUTFCode(p) != 0; p+=GetUTFOctets(p))
1696  if (GetUTFCode(p) < 0)
1697  break;
1698  utf8=(unsigned char *) NULL;
1699  if (GetUTFCode(p) == 0)
1700  p=draw_info->text;
1701  else
1702  {
1703  utf8=ConvertLatin1ToUTF8((unsigned char *) draw_info->text);
1704  if (utf8 != (unsigned char *) NULL)
1705  p=(char *) utf8;
1706  }
1707  grapheme=(GraphemeInfo *) NULL;
1708  length=ComplexTextLayout(image,draw_info,p,strlen(p),face,flags,&grapheme);
1709  missing_glyph_id=FT_Get_Char_Index(face,' ');
1710  code=0;
1711  last_character=(ssize_t) length-1;
1712  for (i=0; i < (ssize_t) length; i++)
1713  {
1714  FT_BitmapGlyph
1715  bitmap;
1716 
1717  FT_Outline
1718  outline;
1719 
1720  PointInfo
1721  point;
1722 
1723  /*
1724  Render UTF-8 sequence.
1725  */
1726  glyph.id=grapheme[i].index;
1727  if (glyph.id == 0)
1728  glyph.id=missing_glyph_id;
1729  if ((glyph.id != 0) && (last_glyph_id != 0))
1730  origin.x+=(FT_Pos) (64.0*draw_info->kerning);
1731  glyph.origin=origin;
1732  glyph.origin.x+=grapheme[i].x_offset;
1733  glyph.origin.y+=grapheme[i].y_offset;
1734  if (glyph.image != (FT_Glyph) NULL)
1735  {
1736  FT_Done_Glyph(glyph.image);
1737  glyph.image=(FT_Glyph) NULL;
1738  }
1739  ft_status=FT_Load_Glyph(face,glyph.id,flags);
1740  if (ft_status != 0)
1741  continue;
1742  ft_status=FT_Get_Glyph(face->glyph,&glyph.image);
1743  if (ft_status != 0)
1744  continue;
1745  outline=((FT_OutlineGlyph) glyph.image)->outline;
1746  if ((glyph.image->format != FT_GLYPH_FORMAT_OUTLINE) &&
1747  (IsEmptyOutline(outline) == MagickFalse))
1748  continue;
1749  ft_status=FT_Outline_Get_BBox(&outline,&bounds);
1750  if (ft_status != 0)
1751  continue;
1752  if ((bounds.xMin < metrics->bounds.x1) && (bounds.xMin != 0))
1753  metrics->bounds.x1=(double) bounds.xMin;
1754  if ((bounds.yMin < metrics->bounds.y1) && (bounds.yMin != 0))
1755  metrics->bounds.y1=(double) bounds.yMin;
1756  if ((bounds.xMax > metrics->bounds.x2) && (bounds.xMax != 0))
1757  metrics->bounds.x2=(double) bounds.xMax;
1758  if ((bounds.yMax > metrics->bounds.y2) && (bounds.yMax != 0))
1759  metrics->bounds.y2=(double) bounds.yMax;
1760  if (((draw_info->stroke.opacity != TransparentOpacity) ||
1761  (draw_info->stroke_pattern != (Image *) NULL)) &&
1762  ((status != MagickFalse) && (draw_info->render != MagickFalse)))
1763  {
1764  /*
1765  Trace the glyph.
1766  */
1767  annotate_info->affine.tx=glyph.origin.x/64.0;
1768  annotate_info->affine.ty=(-glyph.origin.y/64.0);
1769  if (IsEmptyOutline(outline) == MagickFalse)
1770  ft_status=FT_Outline_Decompose(&outline,&OutlineMethods,
1771  annotate_info);
1772  }
1773  FT_Vector_Transform(&glyph.origin,&affine);
1774  (void) FT_Glyph_Transform(glyph.image,&affine,&glyph.origin);
1775  ft_status=FT_Glyph_To_Bitmap(&glyph.image,FT_RENDER_MODE_NORMAL,
1776  (FT_Vector *) NULL,MagickTrue);
1777  if (ft_status != 0)
1778  continue;
1779  bitmap=(FT_BitmapGlyph) glyph.image;
1780  point.x=offset->x+bitmap->left;
1781  if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1782  point.x+=(origin.x/64.0);
1783  point.y=offset->y-bitmap->top;
1784  if (draw_info->render != MagickFalse)
1785  {
1786  CacheView
1787  *image_view;
1788 
1789  unsigned char
1790  *p;
1791 
1792  MagickBooleanType
1793  transparent_fill;
1794 
1795  /*
1796  Rasterize the glyph.
1797  */
1798  transparent_fill=((draw_info->fill.opacity == TransparentOpacity) &&
1799  (draw_info->fill_pattern == (Image *) NULL) &&
1800  (draw_info->stroke.opacity == TransparentOpacity) &&
1801  (draw_info->stroke_pattern == (Image *) NULL)) ? MagickTrue :
1802  MagickFalse;
1803  p=bitmap->bitmap.buffer;
1804  image_view=AcquireAuthenticCacheView(image,exception);
1805  for (y=0; y < (ssize_t) bitmap->bitmap.rows; y++)
1806  {
1807  MagickBooleanType
1808  active,
1809  sync;
1810 
1811  MagickRealType
1812  fill_opacity;
1813 
1814  PixelPacket
1815  fill_color,
1816  *magick_restrict q;
1817 
1818  ssize_t
1819  x;
1820 
1821  ssize_t
1822  n,
1823  x_offset,
1824  y_offset;
1825 
1826  if (status == MagickFalse)
1827  continue;
1828  x_offset=CastDoubleToLong(ceil(point.x-0.5));
1829  y_offset=CastDoubleToLong(ceil(point.y+y-0.5));
1830  if ((y_offset < 0) || (y_offset >= (ssize_t) image->rows))
1831  continue;
1832  q=(PixelPacket *) NULL;
1833  if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1834  active=MagickFalse;
1835  else
1836  {
1837  q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,
1838  bitmap->bitmap.width,1,exception);
1839  active=q != (PixelPacket *) NULL ? MagickTrue : MagickFalse;
1840  }
1841  n=y*bitmap->bitmap.pitch;
1842  for (x=0; x < (ssize_t) bitmap->bitmap.width; x++, n++)
1843  {
1844  x_offset=CastDoubleToLong(ceil(point.x+x-0.5));
1845  if ((x_offset < 0) || (x_offset >= (ssize_t) image->columns))
1846  {
1847  if (q != (PixelPacket *) NULL)
1848  q++;
1849  continue;
1850  }
1851  fill_opacity=1.0;
1852  if (bitmap->bitmap.buffer != (unsigned char *) NULL)
1853  {
1854  if (bitmap->bitmap.pixel_mode == ft_pixel_mode_grays)
1855  fill_opacity=(MagickRealType) (p[n])/(
1856  bitmap->bitmap.num_grays-1);
1857  else
1858  if (bitmap->bitmap.pixel_mode == ft_pixel_mode_mono)
1859  fill_opacity=((p[(x >> 3)+y*bitmap->bitmap.pitch] &
1860  (1 << (~x & 0x07)))) == 0 ? 0.0 : 1.0;
1861  }
1862  if (draw_info->text_antialias == MagickFalse)
1863  fill_opacity=fill_opacity >= 0.5 ? 1.0 : 0.0;
1864  if (active == MagickFalse)
1865  q=GetCacheViewAuthenticPixels(image_view,x_offset,y_offset,1,1,
1866  exception);
1867  if (q == (PixelPacket *) NULL)
1868  continue;
1869  if (transparent_fill == MagickFalse)
1870  {
1871  (void) GetFillColor(draw_info,x_offset,y_offset,&fill_color);
1872  fill_opacity=QuantumRange-fill_opacity*(QuantumRange-
1873  fill_color.opacity);
1874  MagickCompositeOver(&fill_color,fill_opacity,q,q->opacity,q);
1875  }
1876  else
1877  {
1878  double
1879  Sa,
1880  Da;
1881 
1882  Da=1.0-(QuantumScale*(QuantumRange-q->opacity));
1883  Sa=fill_opacity;
1884  fill_opacity=(1.0-RoundToUnity(Sa+Da-Sa*Da))*QuantumRange;
1885  SetPixelAlpha(q,fill_opacity);
1886  }
1887  if (active == MagickFalse)
1888  {
1889  sync=SyncCacheViewAuthenticPixels(image_view,exception);
1890  if (sync == MagickFalse)
1891  status=MagickFalse;
1892  }
1893  q++;
1894  }
1895  sync=SyncCacheViewAuthenticPixels(image_view,exception);
1896  if (sync == MagickFalse)
1897  status=MagickFalse;
1898  }
1899  image_view=DestroyCacheView(image_view);
1900  if (((draw_info->stroke.opacity != TransparentOpacity) ||
1901  (draw_info->stroke_pattern != (Image *) NULL)) &&
1902  (status != MagickFalse))
1903  {
1904  /*
1905  Draw text stroke.
1906  */
1907  annotate_info->linejoin=RoundJoin;
1908  annotate_info->affine.tx=offset->x;
1909  annotate_info->affine.ty=offset->y;
1910  (void) ConcatenateString(&annotate_info->primitive,"'");
1911  if (strlen(annotate_info->primitive) > 7)
1912  (void) DrawImage(image,annotate_info);
1913  (void) CloneString(&annotate_info->primitive,"path '");
1914  }
1915  }
1916  if ((fabs(draw_info->interword_spacing) >= MagickEpsilon) &&
1917  (IsUTFSpace(GetUTFCode(p+grapheme[i].cluster)) != MagickFalse) &&
1918  (IsUTFSpace(code) == MagickFalse))
1919  origin.x+=(FT_Pos) (64.0*draw_info->interword_spacing);
1920  else
1921  if (i == last_character)
1922  origin.x+=MagickMax((FT_Pos) grapheme[i].x_advance,bounds.xMax);
1923  else
1924  origin.x+=(FT_Pos) grapheme[i].x_advance;
1925  metrics->origin.x=(double) origin.x;
1926  metrics->origin.y=(double) origin.y;
1927  if (metrics->origin.x > metrics->width)
1928  metrics->width=metrics->origin.x;
1929  last_glyph_id=glyph.id;
1930  code=GetUTFCode(p+grapheme[i].cluster);
1931  }
1932  if (grapheme != (GraphemeInfo *) NULL)
1933  grapheme=(GraphemeInfo *) RelinquishMagickMemory(grapheme);
1934  if (utf8 != (unsigned char *) NULL)
1935  utf8=(unsigned char *) RelinquishMagickMemory(utf8);
1936  if (glyph.image != (FT_Glyph) NULL)
1937  {
1938  FT_Done_Glyph(glyph.image);
1939  glyph.image=(FT_Glyph) NULL;
1940  }
1941  /*
1942  Determine font metrics.
1943  */
1944  metrics->bounds.x1/=64.0;
1945  metrics->bounds.y1/=64.0;
1946  metrics->bounds.x2/=64.0;
1947  metrics->bounds.y2/=64.0;
1948  metrics->origin.x/=64.0;
1949  metrics->origin.y/=64.0;
1950  metrics->width/=64.0;
1951  /*
1952  Relinquish resources.
1953  */
1954  annotate_info=DestroyDrawInfo(annotate_info);
1955  (void) FT_Done_Face(face);
1956  (void) FT_Done_FreeType(library);
1957  return(status);
1958 }
1959 #else
1960 static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
1961  const char *magick_unused(encoding),const PointInfo *offset,
1962  TypeMetric *metrics)
1963 {
1964  (void) ThrowMagickException(&image->exception,GetMagickModule(),
1965  MissingDelegateWarning,"DelegateLibrarySupportNotBuiltIn","`%s' (Freetype)",
1966  draw_info->font != (char *) NULL ? draw_info->font : "none");
1967  return(RenderPostscript(image,draw_info,offset,metrics));
1968 }
1969 #endif
1970 
1971 /*
1972 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1973 % %
1974 % %
1975 % %
1976 + R e n d e r P o s t s c r i p t %
1977 % %
1978 % %
1979 % %
1980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1981 %
1982 % RenderPostscript() renders text on the image with a Postscript font. It
1983 % also returns the bounding box of the text relative to the image.
1984 %
1985 % The format of the RenderPostscript method is:
1986 %
1987 % MagickBooleanType RenderPostscript(Image *image,DrawInfo *draw_info,
1988 % const PointInfo *offset,TypeMetric *metrics)
1989 %
1990 % A description of each parameter follows:
1991 %
1992 % o image: the image.
1993 %
1994 % o draw_info: the draw info.
1995 %
1996 % o offset: (x,y) location of text relative to image.
1997 %
1998 % o metrics: bounding box of text.
1999 %
2000 */
2001 
2002 static char *EscapeParenthesis(const char *source)
2003 {
2004  char
2005  *destination;
2006 
2007  char
2008  *q;
2009 
2010  const char
2011  *p;
2012 
2013  size_t
2014  length;
2015 
2016  assert(source != (const char *) NULL);
2017  length=0;
2018  for (p=source; *p != '\0'; p++)
2019  {
2020  if ((*p == '\\') || (*p == '(') || (*p == ')'))
2021  {
2022  if (~length < 1)
2023  ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
2024  length++;
2025  }
2026  length++;
2027  }
2028  destination=(char *) NULL;
2029  if (~length >= (MaxTextExtent-1))
2030  destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
2031  sizeof(*destination));
2032  if (destination == (char *) NULL)
2033  ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
2034  *destination='\0';
2035  q=destination;
2036  for (p=source; *p != '\0'; p++)
2037  {
2038  if ((*p == '\\') || (*p == '(') || (*p == ')'))
2039  *q++='\\';
2040  *q++=(*p);
2041  }
2042  *q='\0';
2043  return(destination);
2044 }
2045 
2046 static MagickBooleanType RenderPostscript(Image *image,
2047  const DrawInfo *draw_info,const PointInfo *offset,TypeMetric *metrics)
2048 {
2049  char
2050  filename[MaxTextExtent],
2051  geometry[MaxTextExtent],
2052  *text;
2053 
2054  FILE
2055  *file;
2056 
2057  Image
2058  *annotate_image;
2059 
2060  ImageInfo
2061  *annotate_info;
2062 
2063  int
2064  unique_file;
2065 
2066  MagickBooleanType
2067  identity;
2068 
2069  PointInfo
2070  extent,
2071  point,
2072  resolution;
2073 
2074  ssize_t
2075  i;
2076 
2077  size_t
2078  length;
2079 
2080  ssize_t
2081  y;
2082 
2083  /*
2084  Render label with a Postscript font.
2085  */
2086  if (draw_info->debug != MagickFalse)
2087  (void) LogMagickEvent(AnnotateEvent,GetMagickModule(),
2088  "Font %s; pointsize %g",draw_info->font != (char *) NULL ?
2089  draw_info->font : "none",draw_info->pointsize);
2090  file=(FILE *) NULL;
2091  unique_file=AcquireUniqueFileResource(filename);
2092  if (unique_file != -1)
2093  file=fdopen(unique_file,"wb");
2094  if ((unique_file == -1) || (file == (FILE *) NULL))
2095  {
2096  ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
2097  filename);
2098  return(MagickFalse);
2099  }
2100  (void) FormatLocaleFile(file,"%%!PS-Adobe-3.0\n");
2101  (void) FormatLocaleFile(file,"/ReencodeType\n");
2102  (void) FormatLocaleFile(file,"{\n");
2103  (void) FormatLocaleFile(file," findfont dup length\n");
2104  (void) FormatLocaleFile(file,
2105  " dict begin { 1 index /FID ne {def} {pop pop} ifelse } forall\n");
2106  (void) FormatLocaleFile(file,
2107  " /Encoding ISOLatin1Encoding def currentdict end definefont pop\n");
2108  (void) FormatLocaleFile(file,"} bind def\n");
2109  /*
2110  Sample to compute bounding box.
2111  */
2112  identity=(fabs(draw_info->affine.sx-draw_info->affine.sy) < MagickEpsilon) &&
2113  (fabs(draw_info->affine.rx) < MagickEpsilon) &&
2114  (fabs(draw_info->affine.ry) < MagickEpsilon) ? MagickTrue : MagickFalse;
2115  extent.x=0.0;
2116  extent.y=0.0;
2117  length=strlen(draw_info->text);
2118  for (i=0; i <= (ssize_t) (length+2); i++)
2119  {
2120  point.x=fabs(draw_info->affine.sx*i*draw_info->pointsize+
2121  draw_info->affine.ry*2.0*draw_info->pointsize);
2122  point.y=fabs(draw_info->affine.rx*i*draw_info->pointsize+
2123  draw_info->affine.sy*2.0*draw_info->pointsize);
2124  if (point.x > extent.x)
2125  extent.x=point.x;
2126  if (point.y > extent.y)
2127  extent.y=point.y;
2128  }
2129  (void) FormatLocaleFile(file,"%g %g moveto\n",identity != MagickFalse ? 0.0 :
2130  extent.x/2.0,extent.y/2.0);
2131  (void) FormatLocaleFile(file,"%g %g scale\n",draw_info->pointsize,
2132  draw_info->pointsize);
2133  if ((draw_info->font == (char *) NULL) || (*draw_info->font == '\0') ||
2134  (strchr(draw_info->font,'/') != (char *) NULL))
2135  (void) FormatLocaleFile(file,
2136  "/Times-Roman-ISO dup /Times-Roman ReencodeType findfont setfont\n");
2137  else
2138  (void) FormatLocaleFile(file,
2139  "/%s-ISO dup /%s ReencodeType findfont setfont\n",draw_info->font,
2140  draw_info->font);
2141  (void) FormatLocaleFile(file,"[%g %g %g %g 0 0] concat\n",
2142  draw_info->affine.sx,-draw_info->affine.rx,-draw_info->affine.ry,
2143  draw_info->affine.sy);
2144  text=EscapeParenthesis(draw_info->text);
2145  if (identity == MagickFalse)
2146  (void) FormatLocaleFile(file,"(%s) stringwidth pop -0.5 mul -0.5 rmoveto\n",
2147  text);
2148  (void) FormatLocaleFile(file,"(%s) show\n",text);
2149  text=DestroyString(text);
2150  (void) FormatLocaleFile(file,"showpage\n");
2151  (void) fclose(file);
2152  (void) FormatLocaleString(geometry,MaxTextExtent,"%.20gx%.20g+0+0!",
2153  floor(extent.x+0.5),floor(extent.y+0.5));
2154  annotate_info=AcquireImageInfo();
2155  (void) FormatLocaleString(annotate_info->filename,MaxTextExtent,"ps:%s",
2156  filename);
2157  (void) CloneString(&annotate_info->page,geometry);
2158  if (draw_info->density != (char *) NULL)
2159  (void) CloneString(&annotate_info->density,draw_info->density);
2160  annotate_info->antialias=draw_info->text_antialias;
2161  annotate_image=ReadImage(annotate_info,&image->exception);
2162  CatchException(&image->exception);
2163  annotate_info=DestroyImageInfo(annotate_info);
2164  (void) RelinquishUniqueFileResource(filename);
2165  if (annotate_image == (Image *) NULL)
2166  return(MagickFalse);
2167  resolution.x=DefaultResolution;
2168  resolution.y=DefaultResolution;
2169  if (draw_info->density != (char *) NULL)
2170  {
2171  GeometryInfo
2172  geometry_info;
2173 
2174  MagickStatusType
2175  flags;
2176 
2177  flags=ParseGeometry(draw_info->density,&geometry_info);
2178  if ((flags & RhoValue) != 0)
2179  resolution.x=geometry_info.rho;
2180  resolution.y=resolution.x;
2181  if ((flags & SigmaValue) != 0)
2182  resolution.y=geometry_info.sigma;
2183  }
2184  if (identity == MagickFalse)
2185  (void) TransformImage(&annotate_image,"0x0",(char *) NULL);
2186  else
2187  {
2189  crop_info;
2190 
2191  crop_info=GetImageBoundingBox(annotate_image,&annotate_image->exception);
2192  crop_info.height=(size_t) ((resolution.y/DefaultResolution)*
2193  ExpandAffine(&draw_info->affine)*draw_info->pointsize+0.5);
2194  crop_info.y=CastDoubleToLong(ceil((resolution.y/DefaultResolution)*
2195  extent.y/8.0-0.5));
2196  (void) FormatLocaleString(geometry,MaxTextExtent,
2197  "%.20gx%.20g%+.20g%+.20g",(double) crop_info.width,(double)
2198  crop_info.height,(double) crop_info.x,(double) crop_info.y);
2199  (void) TransformImage(&annotate_image,geometry,(char *) NULL);
2200  }
2201  metrics->pixels_per_em.x=(resolution.y/DefaultResolution)*
2202  ExpandAffine(&draw_info->affine)*draw_info->pointsize;
2203  metrics->pixels_per_em.y=metrics->pixels_per_em.x;
2204  metrics->ascent=metrics->pixels_per_em.y;
2205  metrics->descent=metrics->pixels_per_em.y/-5.0;
2206  metrics->width=(double) annotate_image->columns/
2207  ExpandAffine(&draw_info->affine);
2208  metrics->height=floor(metrics->ascent-metrics->descent+0.5);
2209  metrics->max_advance=metrics->pixels_per_em.x;
2210  metrics->bounds.x1=0.0;
2211  metrics->bounds.y1=metrics->descent;
2212  metrics->bounds.x2=metrics->ascent+metrics->descent;
2213  metrics->bounds.y2=metrics->ascent+metrics->descent;
2214  metrics->underline_position=(-2.0);
2215  metrics->underline_thickness=1.0;
2216  if (draw_info->render == MagickFalse)
2217  {
2218  annotate_image=DestroyImage(annotate_image);
2219  return(MagickTrue);
2220  }
2221  if (draw_info->fill.opacity != TransparentOpacity)
2222  {
2224  *exception;
2225 
2226  MagickBooleanType
2227  sync;
2228 
2229  PixelPacket
2230  fill_color;
2231 
2232  CacheView
2233  *annotate_view;
2234 
2235  /*
2236  Render fill color.
2237  */
2238  if (image->matte == MagickFalse)
2239  (void) SetImageAlphaChannel(image,OpaqueAlphaChannel);
2240  if (annotate_image->matte == MagickFalse)
2241  (void) SetImageAlphaChannel(annotate_image,OpaqueAlphaChannel);
2242  fill_color=draw_info->fill;
2243  exception=(&image->exception);
2244  annotate_view=AcquireAuthenticCacheView(annotate_image,exception);
2245  for (y=0; y < (ssize_t) annotate_image->rows; y++)
2246  {
2247  PixelPacket
2248  *magick_restrict q;
2249 
2250  ssize_t
2251  x;
2252 
2253  q=GetCacheViewAuthenticPixels(annotate_view,0,y,annotate_image->columns,
2254  1,exception);
2255  if (q == (PixelPacket *) NULL)
2256  break;
2257  for (x=0; x < (ssize_t) annotate_image->columns; x++)
2258  {
2259  (void) GetFillColor(draw_info,x,y,&fill_color);
2260  SetPixelAlpha(q,ClampToQuantum((((QuantumRange-GetPixelIntensity(
2261  annotate_image,q))*(QuantumRange-fill_color.opacity))/
2262  QuantumRange)));
2263  SetPixelRed(q,fill_color.red);
2264  SetPixelGreen(q,fill_color.green);
2265  SetPixelBlue(q,fill_color.blue);
2266  q++;
2267  }
2268  sync=SyncCacheViewAuthenticPixels(annotate_view,exception);
2269  if (sync == MagickFalse)
2270  break;
2271  }
2272  annotate_view=DestroyCacheView(annotate_view);
2273  (void) CompositeImage(image,OverCompositeOp,annotate_image,
2274  (ssize_t) ceil(offset->x-0.5),(ssize_t) ceil(offset->y-(metrics->ascent+
2275  metrics->descent)-0.5));
2276  }
2277  annotate_image=DestroyImage(annotate_image);
2278  return(MagickTrue);
2279 }
2280 
2281 /*
2282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2283 % %
2284 % %
2285 % %
2286 + R e n d e r X 1 1 %
2287 % %
2288 % %
2289 % %
2290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2291 %
2292 % RenderX11() renders text on the image with an X11 font. It also returns the
2293 % bounding box of the text relative to the image.
2294 %
2295 % The format of the RenderX11 method is:
2296 %
2297 % MagickBooleanType RenderX11(Image *image,DrawInfo *draw_info,
2298 % const PointInfo *offset,TypeMetric *metrics)
2299 %
2300 % A description of each parameter follows:
2301 %
2302 % o image: the image.
2303 %
2304 % o draw_info: the draw info.
2305 %
2306 % o offset: (x,y) location of text relative to image.
2307 %
2308 % o metrics: bounding box of text.
2309 %
2310 */
2311 static MagickBooleanType RenderX11(Image *image,const DrawInfo *draw_info,
2312  const PointInfo *offset,TypeMetric *metrics)
2313 {
2314  MagickBooleanType
2315  status;
2316 
2317  if (annotate_semaphore == (SemaphoreInfo *) NULL)
2318  ActivateSemaphoreInfo(&annotate_semaphore);
2319  LockSemaphoreInfo(annotate_semaphore);
2320  status=XRenderImage(image,draw_info,offset,metrics);
2321  UnlockSemaphoreInfo(annotate_semaphore);
2322  return(status);
2323 }
Definition: image.h:152
Definition: type.h:50