Coverage Report

Created: 2026-07-25 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/graphicsmagick/coders/ttf.c
Line
Count
Source
1
/*
2
% Copyright (C) 2003-2026 GraphicsMagick Group
3
% Copyright (C) 2002 ImageMagick Studio
4
% Copyright 1991-1999 E. I. du Pont de Nemours and Company
5
%
6
% This program is covered by multiple licenses, which are described in
7
% Copyright.txt. You should have received a copy of Copyright.txt with this
8
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
9
%
10
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11
%                                                                             %
12
%                                                                             %
13
%                                                                             %
14
%                            TTTTT  TTTTT  FFFFF                              %
15
%                              T      T    F                                  %
16
%                              T      T    FFF                                %
17
%                              T      T    F                                  %
18
%                              T      T    F                                  %
19
%                                                                             %
20
%                                                                             %
21
%             Return A Preview For A TrueType or Postscript Font              %
22
%                                                                             %
23
%                                                                             %
24
%                              Software Design                                %
25
%                                John Cristy                                  %
26
%                                 July 1992                                   %
27
%                                                                             %
28
%                                                                             %
29
%                                                                             %
30
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31
%
32
%
33
*/
34

35
/*
36
  Include declarations.
37
*/
38
#include "magick/studio.h"
39
#include "magick/blob.h"
40
#include "magick/pixel_cache.h"
41
#include "magick/draw.h"
42
#include "magick/magick.h"
43
#include "magick/render.h"
44
#include "magick/utility.h"
45
#include "magick/static.h"
46
#if defined(HasTTF)
47
48
#  if defined(__MINGW32__)
49
#    undef interface  /* Remove interface define */
50
#  endif
51
52
#  if defined(HAVE_FT2BUILD_H)
53
     /*
54
       Modern FreeType2 installs require that <ft2build.h> be included
55
       before including other FreeType2 headers.  Including
56
       <ft2build.h> establishes definitions used by other FreeType2
57
       headers.
58
     */
59
#    include <ft2build.h>
60
#    include FT_FREETYPE_H
61
#  else
62
     /*
63
       Very old way to include FreeType2
64
     */
65
#    include <freetype/freetype.h>
66
#  endif /* defined(HAVE_FT2BUILD_H) */
67
68
#endif /* defined(HasTTF) */
69

70
/*
71
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72
%                                                                             %
73
%                                                                             %
74
%                                                                             %
75
%   I s T T F                                                                 %
76
%                                                                             %
77
%                                                                             %
78
%                                                                             %
79
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80
%
81
%  Method IsTTF returns True if the image format type, identified by the
82
%  magick string can be handled by this module.
83
%
84
%  The format of the IsTTF method is:
85
%
86
%      unsigned int IsTTF(const unsigned char *magick,const size_t length)
87
%
88
%  A description of each parameter follows:
89
%
90
%    o status:  Method IsTTF returns True if the image format type can be
91
%               handled by this module.
92
%
93
%    o magick: This string is generally the first few bytes of an image file
94
%      or blob.
95
%
96
%    o length: Specifies the length of the magick string.
97
%
98
%
99
*/
100
101
static unsigned int IsPFA(const unsigned char *magick,const size_t length)
102
0
{
103
0
  if (length < 14)
104
0
    return(False);
105
0
  if (LocaleNCompare((char *) magick,"%!PS-AdobeFont-1.0",14) == 0)
106
0
    return(True);
107
0
  return(False);
108
0
}
109
110
static unsigned int IsTTF(const unsigned char *magick,const size_t length)
111
0
{
112
0
  if (length < 5)
113
0
    return(False);
114
0
  if ((magick[0] == 0x00U) && (magick[1] == 0x01U) && (magick[2] == 0x00U) &&
115
0
      (magick[3] == 0x00U) && (magick[4] == 0x0U))
116
0
    return(True);
117
0
  return(False);
118
0
}
119

120
/*
121
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122
%                                                                             %
123
%                                                                             %
124
%                                                                             %
125
%   R e a d T T F I m a g e                                                   %
126
%                                                                             %
127
%                                                                             %
128
%                                                                             %
129
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
%
131
%  Method ReadTTFImage reads a TrueType font file and returns it.  It
132
%  allocates the memory necessary for the new Image structure and returns a
133
%  pointer to the new image.
134
%
135
%  The format of the ReadTTFImage method is:
136
%
137
%      Image *ReadTTFImage(const ImageInfo *image_info,ExceptionInfo *exception)
138
%
139
%  A description of each parameter follows:
140
%
141
%    o image:  Method ReadTTFImage returns a pointer to the image after
142
%      reading.  A null image is returned if there is a memory shortage or
143
%      if the image cannot be read.
144
%
145
%    o image_info: Specifies a pointer to a ImageInfo structure.
146
%
147
%    o exception: return any errors or warnings in this structure.
148
%
149
%
150
*/
151
#if defined(HasTTF)
152
static Image *ReadTTFImage(const ImageInfo *image_info,ExceptionInfo *exception)
153
5.09k
{
154
5.09k
  char
155
5.09k
    buffer[MaxTextExtent];
156
157
5.09k
  static const char
158
5.09k
    Text[] =
159
5.09k
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz\n0123456789 "
160
5.09k
      "\342\352\356\373\364\344\353\357\366\374\377\340\371\351\350\347\n"
161
5.09k
      "&#~\\\"\'(-`_^@)=+\260 $\243^\250*\265\371%!\247:/;.,?<>";
162
163
5.09k
  DrawContext
164
5.09k
    context;
165
166
5.09k
  DrawInfo
167
5.09k
    *draw_info;
168
169
5.09k
  Image
170
5.09k
    *image;
171
172
5.09k
  long
173
5.09k
    y;
174
175
5.09k
  PixelPacket
176
5.09k
    background_color;
177
178
5.09k
  register long
179
5.09k
    i,
180
5.09k
    x;
181
182
5.09k
  register PixelPacket
183
5.09k
    *q;
184
185
5.09k
  unsigned int
186
5.09k
    status;
187
188
  /*
189
    Open image file.
190
  */
191
5.09k
  assert(image_info != (const ImageInfo *) NULL);
192
5.09k
  assert(image_info->signature == MagickSignature);
193
5.09k
  assert(exception != (ExceptionInfo *) NULL);
194
5.09k
  assert(exception->signature == MagickSignature);
195
5.09k
  image=AllocateImage(image_info);
196
5.09k
  image->columns=800;
197
5.09k
  image->rows=480;
198
5.09k
  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
199
5.09k
  if (status == False)
200
5.09k
    ThrowReaderException(FileOpenError,UnableToOpenFile,image);
201
5.09k
  if (CheckImagePixelLimits(image, exception) != MagickPass)
202
5.02k
    ThrowReaderException(ResourceLimitError,ImagePixelLimitExceeded,image);
203
  /*
204
    Color canvas with background color
205
  */
206
77
  background_color=image_info->background_color;
207
37.0k
  for (y=0; y < (long) image->rows; y++)
208
36.9k
  {
209
36.9k
    q=SetImagePixels(image,0,y,image->columns,1);
210
36.9k
    if (q == (PixelPacket *) NULL)
211
0
      break;
212
29.6M
    for (x=0; x < (long) image->columns; x++)
213
29.5M
      *q++=background_color;
214
36.9k
    if (!SyncImagePixelsEx(image,exception))
215
0
      break;
216
36.9k
  }
217
77
  (void) strlcpy(image->magick,image_info->magick,MaxTextExtent);
218
77
  (void) strlcpy(image->filename,image_info->filename,MaxTextExtent);
219
  /*
220
    Prepare drawing commands
221
  */
222
77
  y=20;
223
77
  draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
224
77
  draw_info->font=AllocateString(image->filename);
225
77
  draw_info->fill=image_info->pen;
226
77
  context=DrawAllocateContext(draw_info,image);
227
77
  (void) DrawPushGraphicContext(context);
228
77
  (void) DrawSetViewbox(context,0,0,image->columns,image->rows);
229
77
  (void) DrawSetFont(context,image_info->filename);
230
77
  (void) DrawSetFontSize(context,18);
231
77
  (void) DrawAnnotation(context,10,y,(const unsigned char *) Text);
232
77
  y+=20*MultilineCensus(Text)+20;
233
616
  for (i=12; i <= 72; i+=6)
234
539
  {
235
539
    y+=i+12;
236
539
    (void) DrawSetFontSize(context,18);
237
539
    (void) MagickFormatString(buffer,sizeof(buffer),"%ld",i);
238
539
    (void) DrawAnnotation(context,10,y,(const unsigned char *) buffer);
239
539
    (void) DrawSetFontSize(context,i);
240
539
    (void) DrawAnnotation(context,50,y,(const unsigned char *)
241
539
      "That which does not destroy me, only makes me stronger.");
242
539
    if (i >= 24)
243
385
      i+=6;
244
539
  }
245
77
  (void) DrawPopGraphicContext(context);
246
77
  (void) DrawRender(context);
247
  /*
248
    Free resources.
249
  */
250
77
  DestroyDrawInfo(draw_info);
251
77
  DrawDestroyContext(context);
252
77
  CloseBlob(image);
253
77
  StopTimer(&image->timer);
254
77
  return(image);
255
5.09k
}
256
#endif /* HasTTF */
257

258
/*
259
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260
%                                                                             %
261
%                                                                             %
262
%                                                                             %
263
%   R e g i s t e r T T F I m a g e                                           %
264
%                                                                             %
265
%                                                                             %
266
%                                                                             %
267
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268
%
269
%  Method RegisterTTFImage adds attributes for the TTF image format to
270
%  the list of supported formats.  The attributes include the image format
271
%  tag, a method to read and/or write the format, whether the format
272
%  supports the saving of more than one frame to the same file or blob,
273
%  whether the format supports native in-memory I/O, and a brief
274
%  description of the format.
275
%
276
%  The format of the RegisterTTFImage method is:
277
%
278
%      RegisterTTFImage(void)
279
%
280
*/
281
ModuleExport void RegisterTTFImage(void)
282
6
{
283
6
#if defined(FREETYPE_MAJOR) && defined(FREETYPE_MINOR) && defined(FREETYPE_PATCH)
284
285
6
  static const char
286
6
    version[] = "FreeType " DefineValueToString(FREETYPE_MAJOR) "."
287
6
    DefineValueToString(FREETYPE_MINOR) "." DefineValueToString(FREETYPE_PATCH);
288
6
#define HAVE_TTF_VERSION 1
289
6
#endif
290
291
6
  MagickInfo
292
6
    *entry;
293
294
  /*   *version='\0'; */
295
  /* #if defined(FREETYPE_MAJOR) && defined(FREETYPE_MINOR) && defined(FREETYPE_PATCH) */
296
  /*   MagickFormatString(version,sizeof(version),"FreeType %d.%d.%d",FREETYPE_MAJOR,FREETYPE_MINOR,FREETYPE_PATCH); */
297
  /* #endif */
298
299
6
  entry=SetMagickInfo("TTF");
300
6
#if defined(HasTTF)
301
6
  entry->decoder=(DecoderHandler) ReadTTFImage;
302
6
#endif
303
6
  entry->magick=(MagickHandler) IsTTF;
304
6
  entry->adjoin=False;
305
6
  entry->description="TrueType font";
306
6
#if defined(HAVE_TTF_VERSION)
307
6
  entry->version=version;
308
6
#endif
309
6
  entry->module="TTF";
310
6
  entry->coder_class=PrimaryCoderClass;
311
6
  (void) RegisterMagickInfo(entry);
312
313
6
  entry=SetMagickInfo("PFA");
314
6
#if defined(HasTTF)
315
6
  entry->decoder=(DecoderHandler) ReadTTFImage;
316
6
#endif
317
6
  entry->magick=(MagickHandler) IsPFA;
318
6
  entry->adjoin=False;
319
6
  entry->description="Postscript Type 1 font (ASCII)";
320
6
#if defined(HAVE_TTF_VERSION)
321
6
  entry->version=version;
322
6
#endif
323
6
  entry->module="TTF";
324
6
  entry->coder_class=PrimaryCoderClass;
325
6
  (void) RegisterMagickInfo(entry);
326
327
6
  entry=SetMagickInfo("PFB");
328
6
#if defined(HasTTF)
329
6
  entry->decoder=(DecoderHandler) ReadTTFImage;
330
6
#endif
331
6
  entry->magick=(MagickHandler) IsPFA;
332
6
  entry->adjoin=False;
333
6
  entry->description="Postscript Type 1 font (binary)";
334
6
#if defined(HAVE_TTF_VERSION)
335
6
  entry->version=version;
336
6
#endif
337
6
  entry->module="TTF";
338
6
  entry->coder_class=PrimaryCoderClass;
339
6
  (void) RegisterMagickInfo(entry);
340
6
}
341

342
/*
343
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344
%                                                                             %
345
%                                                                             %
346
%                                                                             %
347
%   U n r e g i s t e r T T F I m a g e                                       %
348
%                                                                             %
349
%                                                                             %
350
%                                                                             %
351
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352
%
353
%  Method UnregisterTTFImage removes format registrations made by the
354
%  TTF module from the list of supported formats.
355
%
356
%  The format of the UnregisterTTFImage method is:
357
%
358
%      UnregisterTTFImage(void)
359
%
360
*/
361
ModuleExport void UnregisterTTFImage(void)
362
0
{
363
0
  (void) UnregisterMagickInfo("TTF");
364
0
  (void) UnregisterMagickInfo("PFA");
365
0
  (void) UnregisterMagickInfo("PFB");
366
0
}