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