Coverage Report

Created: 2026-07-16 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/splash/SplashFontEngine.cc
Line
Count
Source
1
//========================================================================
2
//
3
// SplashFontEngine.cc
4
//
5
// Copyright 2003-2013 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdlib.h>
12
#include <stdio.h>
13
#ifndef _WIN32
14
#  include <unistd.h>
15
#endif
16
#include "gmem.h"
17
#include "gmempp.h"
18
#include "GString.h"
19
#include "GList.h"
20
#include "SplashMath.h"
21
#include "SplashFTFontEngine.h"
22
#include "SplashFontFile.h"
23
#include "SplashFontFileID.h"
24
#include "SplashFont.h"
25
#include "SplashFontEngine.h"
26
27
#ifdef VMS
28
#if (__VMS_VER < 70000000)
29
extern "C" int unlink(char *filename);
30
#endif
31
#endif
32
33
//------------------------------------------------------------------------
34
// SplashFontEngine
35
//------------------------------------------------------------------------
36
37
SplashFontEngine::SplashFontEngine(
38
#if HAVE_FREETYPE_H
39
           GBool enableFreeType,
40
           Guint freeTypeFlags,
41
#endif
42
17.6k
           GBool aa) {
43
17.6k
  int i;
44
45
300k
  for (i = 0; i < splashFontCacheSize; ++i) {
46
283k
    fontCache[i] = NULL;
47
283k
  }
48
17.6k
  badFontFiles = new GList();
49
50
17.6k
#if HAVE_FREETYPE_H
51
17.6k
  if (enableFreeType) {
52
17.6k
    ftEngine = SplashFTFontEngine::init(aa, freeTypeFlags);
53
17.6k
  } else {
54
0
    ftEngine = NULL;
55
0
  }
56
17.6k
#endif
57
17.6k
}
58
59
17.6k
SplashFontEngine::~SplashFontEngine() {
60
17.6k
  int i;
61
62
300k
  for (i = 0; i < splashFontCacheSize; ++i) {
63
282k
    if (fontCache[i]) {
64
8.69k
      delete fontCache[i];
65
8.69k
    }
66
282k
  }
67
17.6k
  deleteGList(badFontFiles, SplashFontFileID);
68
69
17.6k
#if HAVE_FREETYPE_H
70
17.6k
  if (ftEngine) {
71
17.6k
    delete ftEngine;
72
17.6k
  }
73
17.6k
#endif
74
17.6k
}
75
76
109k
SplashFontFile *SplashFontEngine::getFontFile(SplashFontFileID *id) {
77
109k
  SplashFontFile *fontFile;
78
109k
  int i;
79
80
1.22M
  for (i = 0; i < splashFontCacheSize; ++i) {
81
1.15M
    if (fontCache[i]) {
82
41.0k
      fontFile = fontCache[i]->getFontFile();
83
41.0k
      if (fontFile && fontFile->getID()->matches(id)) {
84
39.7k
  return fontFile;
85
39.7k
      }
86
41.0k
    }
87
1.15M
  }
88
69.4k
  return NULL;
89
109k
}
90
91
122k
GBool SplashFontEngine::checkForBadFontFile(SplashFontFileID *id) {
92
127k
  for (int i = 0; i < badFontFiles->getLength(); ++i) {
93
18.3k
    if (((SplashFontFileID *)badFontFiles->get(i))->matches(id)) {
94
13.3k
      return gTrue;
95
13.3k
    }
96
18.3k
  }
97
109k
  return gFalse;
98
122k
}
99
100
SplashFontFile *SplashFontEngine::loadType1Font(SplashFontFileID *idA,
101
#if LOAD_FONTS_FROM_MEM
102
            GString *fontBuf,
103
#else
104
            char *fileName,
105
            GBool deleteFile,
106
#endif
107
1.73k
            const char **enc) {
108
1.73k
  SplashFontFile *fontFile;
109
110
1.73k
  fontFile = NULL;
111
1.73k
#if HAVE_FREETYPE_H
112
1.73k
  if (!fontFile && ftEngine) {
113
1.73k
    fontFile = ftEngine->loadType1Font(idA,
114
#if LOAD_FONTS_FROM_MEM
115
               fontBuf,
116
#else
117
1.73k
               fileName, deleteFile,
118
1.73k
#endif
119
1.73k
               enc);
120
1.73k
  }
121
1.73k
#endif
122
123
1.73k
#if !LOAD_FONTS_FROM_MEM && !defined(_WIN32) && !defined(__ANDROID__)
124
  // delete the (temporary) font file -- with Unix hard link
125
  // semantics, this will remove the last link; otherwise it will
126
  // return an error, leaving the file to be deleted later (if
127
  // loadXYZFont failed, the file will always be deleted)
128
1.73k
  if (deleteFile) {
129
1.73k
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
130
1.73k
  }
131
1.73k
#endif
132
133
1.73k
  if (!fontFile) {
134
1.61k
    badFontFiles->append(idA);
135
1.61k
  }
136
137
1.73k
  return fontFile;
138
1.73k
}
139
140
SplashFontFile *SplashFontEngine::loadType1CFont(SplashFontFileID *idA,
141
#if LOAD_FONTS_FROM_MEM
142
             GString *fontBuf,
143
#else
144
             char *fileName,
145
             GBool deleteFile,
146
#endif
147
             int *codeToGID,
148
1.77k
             const char **enc) {
149
1.77k
  SplashFontFile *fontFile;
150
151
1.77k
  fontFile = NULL;
152
1.77k
  if (!fontFile) {
153
1.77k
    gfree(codeToGID);
154
1.77k
  }
155
1.77k
#if HAVE_FREETYPE_H
156
1.77k
  if (!fontFile && ftEngine) {
157
1.77k
    fontFile = ftEngine->loadType1CFont(idA,
158
#if LOAD_FONTS_FROM_MEM
159
          fontBuf,
160
#else
161
1.77k
          fileName, deleteFile,
162
1.77k
#endif
163
1.77k
          enc);
164
1.77k
  }
165
1.77k
#endif
166
167
1.77k
#if !LOAD_FONTS_FROM_MEM && !defined(_WIN32) && !defined(__ANDROID__)
168
  // delete the (temporary) font file -- with Unix hard link
169
  // semantics, this will remove the last link; otherwise it will
170
  // return an error, leaving the file to be deleted later (if
171
  // loadXYZFont failed, the file will always be deleted)
172
1.77k
  if (deleteFile) {
173
1.77k
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
174
1.77k
  }
175
1.77k
#endif
176
177
1.77k
  if (!fontFile) {
178
430
    badFontFiles->append(idA);
179
430
  }
180
181
1.77k
  return fontFile;
182
1.77k
}
183
184
SplashFontFile *SplashFontEngine::loadOpenTypeT1CFont(SplashFontFileID *idA,
185
#if LOAD_FONTS_FROM_MEM
186
                  GString *fontBuf,
187
#else
188
                  char *fileName,
189
                  GBool deleteFile,
190
#endif
191
                  int *codeToGID,
192
795
                  const char **enc) {
193
795
  SplashFontFile *fontFile;
194
195
795
  fontFile = NULL;
196
795
  if (!fontFile) {
197
795
    gfree(codeToGID);
198
795
  }
199
795
#if HAVE_FREETYPE_H
200
795
  if (!fontFile && ftEngine) {
201
795
    fontFile = ftEngine->loadOpenTypeT1CFont(idA,
202
#if LOAD_FONTS_FROM_MEM
203
               fontBuf,
204
#else
205
795
               fileName, deleteFile,
206
795
#endif
207
795
               enc);
208
795
  }
209
795
#endif
210
211
795
#if !LOAD_FONTS_FROM_MEM && !defined(_WIN32) && !defined(__ANDROID__)
212
  // delete the (temporary) font file -- with Unix hard link
213
  // semantics, this will remove the last link; otherwise it will
214
  // return an error, leaving the file to be deleted later (if
215
  // loadXYZFont failed, the file will always be deleted)
216
795
  if (deleteFile) {
217
795
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
218
795
  }
219
795
#endif
220
221
795
  if (!fontFile) {
222
577
    badFontFiles->append(idA);
223
577
  }
224
225
795
  return fontFile;
226
795
}
227
228
SplashFontFile *SplashFontEngine::loadCIDFont(SplashFontFileID *idA,
229
#if LOAD_FONTS_FROM_MEM
230
                GString *fontBuf,
231
#else
232
                char *fileName,
233
                GBool deleteFile,
234
#endif
235
                int *codeToGID,
236
53
                int codeToGIDLen) {
237
53
  SplashFontFile *fontFile;
238
239
53
  fontFile = NULL;
240
53
#if HAVE_FREETYPE_H
241
53
  if (!fontFile && ftEngine) {
242
53
    fontFile = ftEngine->loadCIDFont(idA,
243
#if LOAD_FONTS_FROM_MEM
244
             fontBuf,
245
#else
246
53
             fileName, deleteFile,
247
53
#endif
248
53
             codeToGID, codeToGIDLen);
249
53
  }
250
53
#endif
251
252
53
  if (!fontFile) {
253
26
    gfree(codeToGID);
254
26
  }
255
256
53
#if !LOAD_FONTS_FROM_MEM && !defined(_WIN32) && !defined(__ANDROID__)
257
  // delete the (temporary) font file -- with Unix hard link
258
  // semantics, this will remove the last link; otherwise it will
259
  // return an error, leaving the file to be deleted later (if
260
  // loadXYZFont failed, the file will always be deleted)
261
53
  if (deleteFile) {
262
53
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
263
53
  }
264
53
#endif
265
266
53
  if (!fontFile) {
267
26
    badFontFiles->append(idA);
268
26
  }
269
270
53
  return fontFile;
271
53
}
272
273
SplashFontFile *SplashFontEngine::loadOpenTypeCFFFont(SplashFontFileID *idA,
274
#if LOAD_FONTS_FROM_MEM
275
                  GString *fontBuf,
276
#else
277
                  char *fileName,
278
                  GBool deleteFile,
279
#endif
280
                  int *codeToGID,
281
63
                  int codeToGIDLen) {
282
63
  SplashFontFile *fontFile;
283
284
63
  fontFile = NULL;
285
63
#if HAVE_FREETYPE_H
286
63
  if (!fontFile && ftEngine) {
287
63
    fontFile = ftEngine->loadOpenTypeCFFFont(idA,
288
#if LOAD_FONTS_FROM_MEM
289
               fontBuf,
290
#else
291
63
               fileName, deleteFile,
292
63
#endif
293
63
               codeToGID, codeToGIDLen);
294
63
  }
295
63
#endif
296
297
63
  if (!fontFile) {
298
20
    gfree(codeToGID);
299
20
  }
300
301
63
#if !LOAD_FONTS_FROM_MEM && !defined(_WIN32) && !defined(__ANDROID__)
302
  // delete the (temporary) font file -- with Unix hard link
303
  // semantics, this will remove the last link; otherwise it will
304
  // return an error, leaving the file to be deleted later (if
305
  // loadXYZFont failed, the file will always be deleted)
306
63
  if (deleteFile) {
307
63
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
308
63
  }
309
63
#endif
310
311
63
  if (!fontFile) {
312
20
    badFontFiles->append(idA);
313
20
  }
314
315
63
  return fontFile;
316
63
}
317
318
SplashFontFile *SplashFontEngine::loadTrueTypeFont(SplashFontFileID *idA,
319
#if LOAD_FONTS_FROM_MEM
320
               GString *fontBuf,
321
#else
322
               char *fileName,
323
               GBool deleteFile,
324
#endif
325
               int fontNum,
326
               int *codeToGID,
327
               int codeToGIDLen,
328
4.06k
               char *fontName) {
329
4.06k
  SplashFontFile *fontFile;
330
331
4.06k
  fontFile = NULL;
332
4.06k
#if HAVE_FREETYPE_H
333
4.06k
  if (!fontFile && ftEngine) {
334
4.06k
    fontFile = ftEngine->loadTrueTypeFont(idA,
335
#if LOAD_FONTS_FROM_MEM
336
            fontBuf,
337
#else
338
4.06k
            fileName, deleteFile,
339
4.06k
#endif
340
4.06k
            fontNum, codeToGID, codeToGIDLen);
341
4.06k
  }
342
4.06k
#endif
343
344
4.06k
  if (!fontFile) {
345
404
    gfree(codeToGID);
346
404
  }
347
348
4.06k
#if !LOAD_FONTS_FROM_MEM && !defined(_WIN32) && !defined(__ANDROID__)
349
  // delete the (temporary) font file -- with Unix hard link
350
  // semantics, this will remove the last link; otherwise it will
351
  // return an error, leaving the file to be deleted later (if
352
  // loadXYZFont failed, the file will always be deleted)
353
4.06k
  if (deleteFile) {
354
4.06k
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
355
4.06k
  }
356
4.06k
#endif
357
358
4.06k
  if (!fontFile) {
359
404
    badFontFiles->append(idA);
360
404
  }
361
362
4.06k
  return fontFile;
363
4.06k
}
364
365
SplashFont *SplashFontEngine::getFont(SplashFontFile *fontFile,
366
              SplashCoord *textMat,
367
45.1k
              SplashCoord *ctm) {
368
45.1k
  SplashCoord mat[4];
369
45.1k
  SplashFont *font;
370
45.1k
  int i, j;
371
372
45.1k
  mat[0] = textMat[0] * ctm[0] + textMat[1] * ctm[2];
373
45.1k
  mat[1] = -(textMat[0] * ctm[1] + textMat[1] * ctm[3]);
374
45.1k
  mat[2] = textMat[2] * ctm[0] + textMat[3] * ctm[2];
375
45.1k
  mat[3] = -(textMat[2] * ctm[1] + textMat[3] * ctm[3]);
376
45.1k
  if (!splashCheckDet(mat[0], mat[1], mat[2], mat[3], 0.01)) {
377
    // avoid a singular (or close-to-singular) matrix
378
45.1k
    mat[0] = 0.01;  mat[1] = 0;
379
45.1k
    mat[2] = 0;     mat[3] = 0.01;
380
45.1k
  }
381
382
45.1k
  font = fontCache[0];
383
45.1k
  if (font && font->matches(fontFile, mat, textMat)) {
384
35.8k
    return font;
385
35.8k
  }
386
141k
  for (i = 1; i < splashFontCacheSize; ++i) {
387
133k
    font = fontCache[i];
388
133k
    if (font && font->matches(fontFile, mat, textMat)) {
389
2.04k
      for (j = i; j > 0; --j) {
390
1.51k
  fontCache[j] = fontCache[j-1];
391
1.51k
      }
392
522
      fontCache[0] = font;
393
522
      return font;
394
522
    }
395
133k
  }
396
8.77k
  font = fontFile->makeFont(mat, textMat);
397
8.77k
  if (fontCache[splashFontCacheSize - 1]) {
398
73
    delete fontCache[splashFontCacheSize - 1];
399
73
  }
400
140k
  for (j = splashFontCacheSize - 1; j > 0; --j) {
401
131k
    fontCache[j] = fontCache[j-1];
402
131k
  }
403
8.77k
  fontCache[0] = font;
404
8.77k
  return font;
405
9.29k
}