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