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