Coverage Report

Created: 2026-07-04 07:15

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