Coverage Report

Created: 2026-10-13 06:47

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