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