Coverage Report

Created: 2026-06-22 07:14

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
10.2k
           GBool aa) {
43
10.2k
  int i;
44
45
174k
  for (i = 0; i < splashFontCacheSize; ++i) {
46
164k
    fontCache[i] = NULL;
47
164k
  }
48
10.2k
  badFontFiles = new GList();
49
50
10.2k
#if HAVE_FREETYPE_H
51
10.2k
  if (enableFreeType) {
52
10.2k
    ftEngine = SplashFTFontEngine::init(aa, freeTypeFlags);
53
10.2k
  } else {
54
0
    ftEngine = NULL;
55
0
  }
56
10.2k
#endif
57
10.2k
}
58
59
10.2k
SplashFontEngine::~SplashFontEngine() {
60
10.2k
  int i;
61
62
174k
  for (i = 0; i < splashFontCacheSize; ++i) {
63
164k
    if (fontCache[i]) {
64
5.46k
      delete fontCache[i];
65
5.46k
    }
66
164k
  }
67
10.2k
  deleteGList(badFontFiles, SplashFontFileID);
68
69
10.2k
#if HAVE_FREETYPE_H
70
10.2k
  if (ftEngine) {
71
10.2k
    delete ftEngine;
72
10.2k
  }
73
10.2k
#endif
74
10.2k
}
75
76
48.4k
SplashFontFile *SplashFontEngine::getFontFile(SplashFontFileID *id) {
77
48.4k
  SplashFontFile *fontFile;
78
48.4k
  int i;
79
80
495k
  for (i = 0; i < splashFontCacheSize; ++i) {
81
467k
    if (fontCache[i]) {
82
21.3k
      fontFile = fontCache[i]->getFontFile();
83
21.3k
      if (fontFile && fontFile->getID()->matches(id)) {
84
20.5k
  return fontFile;
85
20.5k
      }
86
21.3k
    }
87
467k
  }
88
27.9k
  return NULL;
89
48.4k
}
90
91
58.8k
GBool SplashFontEngine::checkForBadFontFile(SplashFontFileID *id) {
92
62.3k
  for (int i = 0; i < badFontFiles->getLength(); ++i) {
93
13.8k
    if (((SplashFontFileID *)badFontFiles->get(i))->matches(id)) {
94
10.3k
      return gTrue;
95
10.3k
    }
96
13.8k
  }
97
48.4k
  return gFalse;
98
58.8k
}
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
933
            const char **enc) {
108
933
  SplashFontFile *fontFile;
109
110
933
  fontFile = NULL;
111
933
#if HAVE_FREETYPE_H
112
933
  if (!fontFile && ftEngine) {
113
933
    fontFile = ftEngine->loadType1Font(idA,
114
#if LOAD_FONTS_FROM_MEM
115
               fontBuf,
116
#else
117
933
               fileName, deleteFile,
118
933
#endif
119
933
               enc);
120
933
  }
121
933
#endif
122
123
933
#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
933
  if (deleteFile) {
129
933
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
130
933
  }
131
933
#endif
132
133
933
  if (!fontFile) {
134
867
    badFontFiles->append(idA);
135
867
  }
136
137
933
  return fontFile;
138
933
}
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.17k
             const char **enc) {
149
1.17k
  SplashFontFile *fontFile;
150
151
1.17k
  fontFile = NULL;
152
1.17k
  if (!fontFile) {
153
1.17k
    gfree(codeToGID);
154
1.17k
  }
155
1.17k
#if HAVE_FREETYPE_H
156
1.17k
  if (!fontFile && ftEngine) {
157
1.17k
    fontFile = ftEngine->loadType1CFont(idA,
158
#if LOAD_FONTS_FROM_MEM
159
          fontBuf,
160
#else
161
1.17k
          fileName, deleteFile,
162
1.17k
#endif
163
1.17k
          enc);
164
1.17k
  }
165
1.17k
#endif
166
167
1.17k
#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.17k
  if (deleteFile) {
173
1.17k
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
174
1.17k
  }
175
1.17k
#endif
176
177
1.17k
  if (!fontFile) {
178
299
    badFontFiles->append(idA);
179
299
  }
180
181
1.17k
  return fontFile;
182
1.17k
}
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
466
                  const char **enc) {
193
466
  SplashFontFile *fontFile;
194
195
466
  fontFile = NULL;
196
466
  if (!fontFile) {
197
466
    gfree(codeToGID);
198
466
  }
199
466
#if HAVE_FREETYPE_H
200
466
  if (!fontFile && ftEngine) {
201
466
    fontFile = ftEngine->loadOpenTypeT1CFont(idA,
202
#if LOAD_FONTS_FROM_MEM
203
               fontBuf,
204
#else
205
466
               fileName, deleteFile,
206
466
#endif
207
466
               enc);
208
466
  }
209
466
#endif
210
211
466
#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
466
  if (deleteFile) {
217
466
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
218
466
  }
219
466
#endif
220
221
466
  if (!fontFile) {
222
340
    badFontFiles->append(idA);
223
340
  }
224
225
466
  return fontFile;
226
466
}
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
40
                int codeToGIDLen) {
237
40
  SplashFontFile *fontFile;
238
239
40
  fontFile = NULL;
240
40
#if HAVE_FREETYPE_H
241
40
  if (!fontFile && ftEngine) {
242
40
    fontFile = ftEngine->loadCIDFont(idA,
243
#if LOAD_FONTS_FROM_MEM
244
             fontBuf,
245
#else
246
40
             fileName, deleteFile,
247
40
#endif
248
40
             codeToGID, codeToGIDLen);
249
40
  }
250
40
#endif
251
252
40
  if (!fontFile) {
253
16
    gfree(codeToGID);
254
16
  }
255
256
40
#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
40
  if (deleteFile) {
262
40
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
263
40
  }
264
40
#endif
265
266
40
  if (!fontFile) {
267
16
    badFontFiles->append(idA);
268
16
  }
269
270
40
  return fontFile;
271
40
}
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
36
                  int codeToGIDLen) {
282
36
  SplashFontFile *fontFile;
283
284
36
  fontFile = NULL;
285
36
#if HAVE_FREETYPE_H
286
36
  if (!fontFile && ftEngine) {
287
36
    fontFile = ftEngine->loadOpenTypeCFFFont(idA,
288
#if LOAD_FONTS_FROM_MEM
289
               fontBuf,
290
#else
291
36
               fileName, deleteFile,
292
36
#endif
293
36
               codeToGID, codeToGIDLen);
294
36
  }
295
36
#endif
296
297
36
  if (!fontFile) {
298
18
    gfree(codeToGID);
299
18
  }
300
301
36
#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
36
  if (deleteFile) {
307
36
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
308
36
  }
309
36
#endif
310
311
36
  if (!fontFile) {
312
18
    badFontFiles->append(idA);
313
18
  }
314
315
36
  return fontFile;
316
36
}
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
2.53k
               char *fontName) {
329
2.53k
  SplashFontFile *fontFile;
330
331
2.53k
  fontFile = NULL;
332
2.53k
#if HAVE_FREETYPE_H
333
2.53k
  if (!fontFile && ftEngine) {
334
2.53k
    fontFile = ftEngine->loadTrueTypeFont(idA,
335
#if LOAD_FONTS_FROM_MEM
336
            fontBuf,
337
#else
338
2.53k
            fileName, deleteFile,
339
2.53k
#endif
340
2.53k
            fontNum, codeToGID, codeToGIDLen);
341
2.53k
  }
342
2.53k
#endif
343
344
2.53k
  if (!fontFile) {
345
237
    gfree(codeToGID);
346
237
  }
347
348
2.53k
#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
2.53k
  if (deleteFile) {
354
2.53k
    unlink(fontFile ? fontFile->fileName->getCString() : fileName);
355
2.53k
  }
356
2.53k
#endif
357
358
2.53k
  if (!fontFile) {
359
237
    badFontFiles->append(idA);
360
237
  }
361
362
2.53k
  return fontFile;
363
2.53k
}
364
365
SplashFont *SplashFontEngine::getFont(SplashFontFile *fontFile,
366
              SplashCoord *textMat,
367
23.9k
              SplashCoord *ctm) {
368
23.9k
  SplashCoord mat[4];
369
23.9k
  SplashFont *font;
370
23.9k
  int i, j;
371
372
23.9k
  mat[0] = textMat[0] * ctm[0] + textMat[1] * ctm[2];
373
23.9k
  mat[1] = -(textMat[0] * ctm[1] + textMat[1] * ctm[3]);
374
23.9k
  mat[2] = textMat[2] * ctm[0] + textMat[3] * ctm[2];
375
23.9k
  mat[3] = -(textMat[2] * ctm[1] + textMat[3] * ctm[3]);
376
23.9k
  if (!splashCheckDet(mat[0], mat[1], mat[2], mat[3], 0.01)) {
377
    // avoid a singular (or close-to-singular) matrix
378
23.9k
    mat[0] = 0.01;  mat[1] = 0;
379
23.9k
    mat[2] = 0;     mat[3] = 0.01;
380
23.9k
  }
381
382
23.9k
  font = fontCache[0];
383
23.9k
  if (font && font->matches(fontFile, mat, textMat)) {
384
17.6k
    return font;
385
17.6k
  }
386
90.8k
  for (i = 1; i < splashFontCacheSize; ++i) {
387
85.3k
    font = fontCache[i];
388
85.3k
    if (font && font->matches(fontFile, mat, textMat)) {
389
3.26k
      for (j = i; j > 0; --j) {
390
2.54k
  fontCache[j] = fontCache[j-1];
391
2.54k
      }
392
720
      fontCache[0] = font;
393
720
      return font;
394
720
    }
395
85.3k
  }
396
5.51k
  font = fontFile->makeFont(mat, textMat);
397
5.51k
  if (fontCache[splashFontCacheSize - 1]) {
398
54
    delete fontCache[splashFontCacheSize - 1];
399
54
  }
400
88.2k
  for (j = splashFontCacheSize - 1; j > 0; --j) {
401
82.7k
    fontCache[j] = fontCache[j-1];
402
82.7k
  }
403
5.51k
  fontCache[0] = font;
404
5.51k
  return font;
405
6.23k
}