Coverage Report

Created: 2026-03-15 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/GfxFont.h
Line
Count
Source
1
//========================================================================
2
//
3
// GfxFont.h
4
//
5
// Copyright 1996-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#ifndef GFXFONT_H
10
#define GFXFONT_H
11
12
#include <aconf.h>
13
14
#include "gtypes.h"
15
#include "GString.h"
16
#include "Object.h"
17
#include "CharTypes.h"
18
19
class GList;
20
class GHash;
21
class Dict;
22
class CMap;
23
class CharCodeToUnicode;
24
class FoFiTrueType;
25
class FoFiType1C;
26
struct GfxFontCIDWidths;
27
struct Base14FontMapEntry;
28
class GfxFontDictEntry;
29
class FNVHash;
30
31
//------------------------------------------------------------------------
32
// GfxFontType
33
//------------------------------------------------------------------------
34
35
enum GfxFontType {
36
  //----- Gfx8BitFont
37
  fontUnknownType,
38
  fontType1,
39
  fontType1C,
40
  fontType1COT,
41
  fontType3,
42
  fontTrueType,
43
  fontTrueTypeOT,
44
  //----- GfxCIDFont
45
  fontCIDType0,
46
  fontCIDType0C,
47
  fontCIDType0COT,
48
  fontCIDType2,
49
  fontCIDType2OT
50
};
51
52
//------------------------------------------------------------------------
53
// GfxFontCIDWidths
54
//------------------------------------------------------------------------
55
56
struct GfxFontCIDWidthExcep {
57
  CID first;      // this record applies to
58
  CID last;     //   CIDs <first>..<last>
59
  double width;     // char width
60
};
61
62
struct GfxFontCIDWidthExcepV {
63
  CID first;      // this record applies to
64
  CID last;     //   CIDs <first>..<last>
65
  double height;    // char height
66
  double vx, vy;    // origin position
67
};
68
69
struct GfxFontCIDWidths {
70
  double defWidth;    // default char width
71
  double defHeight;   // default char height
72
  double defVY;     // default origin position
73
  GfxFontCIDWidthExcep *exceps; // exceptions
74
  int nExceps;      // number of valid entries in exceps
75
  GfxFontCIDWidthExcepV * // exceptions for vertical font
76
    excepsV;
77
  int nExcepsV;     // number of valid entries in excepsV
78
};
79
80
//------------------------------------------------------------------------
81
// GfxFontLoc
82
//------------------------------------------------------------------------
83
84
enum GfxFontLocType {
85
  gfxFontLocEmbedded,   // font embedded in PDF file
86
  gfxFontLocExternal,   // external font file
87
  gfxFontLocResident    // font resident in PS printer
88
};
89
90
class GfxFontLoc {
91
public:
92
93
  GfxFontLoc();
94
  ~GfxFontLoc();
95
96
  GfxFontLocType locType;
97
  GfxFontType fontType;
98
  Ref embFontID;    // embedded stream obj ID
99
        //   (if locType == gfxFontLocEmbedded)
100
  GString *path;    // font file path
101
        //   (if locType == gfxFontLocExternal)
102
        // PS font name
103
        //   (if locType == gfxFontLocResident)
104
  int fontNum;      // for TrueType collections and Mac dfonts
105
        //   (if locType == gfxFontLocExternal)
106
  double oblique;   // sheer factor to oblique this font
107
        //   (used when substituting a plain
108
        //   font for an oblique font)
109
  GString *encoding;    // PS font encoding, only for 16-bit fonts
110
        //   (if locType == gfxFontLocResident)
111
  int wMode;      // writing mode, only for 16-bit fonts
112
        //   (if locType == gfxFontLocResident)
113
  int substIdx;     // substitute font index
114
        //   (if locType == gfxFontLocExternal,
115
        //   and a Base-14 substitution was made)
116
};
117
118
//------------------------------------------------------------------------
119
// GfxFont
120
//------------------------------------------------------------------------
121
122
358k
#define fontFixedWidth (1 << 0)
123
96.3k
#define fontSerif      (1 << 1)
124
1.76k
#define fontSymbolic   (1 << 2)
125
45.2k
#define fontItalic     (1 << 6)
126
46.1k
#define fontBold       (1 << 18)
127
128
class GfxFont {
129
public:
130
131
  // Build a GfxFont object.
132
  static GfxFont *makeFont(XRef *xref, const char *tagA,
133
         Ref idA, Dict *fontDict);
134
135
  // Create a simple default font, to substitute for an undefined font
136
  // object.
137
  static GfxFont *makeDefaultFont(XRef *xref);
138
139
  GfxFont(const char *tagA, Ref idA, GString *nameA,
140
    GfxFontType typeA, Ref embFontIDA);
141
142
  virtual ~GfxFont();
143
144
40.9k
  GBool isOk() { return ok; }
145
146
  // Get font tag.
147
0
  GString *getTag() { return tag; }
148
149
  // Get font dictionary ID.
150
1.39M
  Ref *getID() { return &id; }
151
152
  // Does this font match the tag?
153
0
  GBool matches(char *tagA) { return !tag->cmp(tagA); }
154
155
  // Get the original font name (ignornig any munging that might have
156
  // been done to map to a canonical Base-14 font name).
157
1.24M
  GString *getName() { return name; }
158
159
  // Get font type.
160
843k
  GfxFontType getType() { return type; }
161
68.4k
  virtual GBool isCIDFont() { return gFalse; }
162
163
  // Get embedded font ID, i.e., a ref for the font file stream.
164
  // Returns false if there is no embedded font.
165
  GBool getEmbeddedFontID(Ref *embID)
166
5.68k
    { *embID = embFontID; return embFontID.num >= 0; }
167
168
  // Get the PostScript font name for the embedded font.  Returns
169
  // NULL if there is no embedded font.
170
5.51k
  GString *getEmbeddedFontName() { return embFontName; }
171
172
  // Get font descriptor flags.
173
0
  int getFlags() { return flags; }
174
10.7k
  GBool isFixedWidth() { return flags & fontFixedWidth; }
175
10.7k
  GBool isSerif() { return flags & fontSerif; }
176
0
  GBool isSymbolic() { return flags & fontSymbolic; }
177
44.8k
  GBool isItalic() { return flags & fontItalic; }
178
44.8k
  GBool isBold() { return flags & fontBold; }
179
180
  // Return the font matrix.
181
20.5k
  double *getFontMatrix() { return fontMat; }
182
183
  // Return the font bounding box.
184
12.0k
  double *getFontBBox() { return fontBBox; }
185
186
  // Return the ascent and descent values.
187
0
  double getAscent() { return ascent; }
188
36
  double getDescent() { return descent; }
189
36
  double getDeclaredAscent() { return declaredAscent; }
190
191
  // Return the writing mode (0=horizontal, 1=vertical).
192
548k
  virtual int getWMode() { return 0; }
193
194
  // Locate the font file for this font.  If <ps> is true, includes PS
195
  // printer-resident fonts.  Returns NULL on failure.
196
  GfxFontLoc *locateFont(XRef *xref, GBool ps);
197
198
  // Locate a Base-14 font file for a specified font name.
199
  static GfxFontLoc *locateBase14Font(GString *base14Name);
200
201
  // Read an embedded font file into a buffer.
202
  char *readEmbFontFile(XRef *xref, int *len);
203
204
  // Get the next char from a string <s> of <len> bytes, returning the
205
  // char <code>, its Unicode mapping <u>, its displacement vector
206
  // (<dx>, <dy>), and its origin offset vector (<ox>, <oy>).  <uSize>
207
  // is the number of entries available in <u>, and <uLen> is set to
208
  // the number actually used.  Returns the number of bytes used by
209
  // the char code.
210
  virtual int getNextChar(char *s, int len, CharCode *code,
211
        Unicode *u, int uSize, int *uLen,
212
        double *dx, double *dy, double *ox, double *oy) = 0;
213
214
  // Returns true if this font is likely to be problematic when
215
  // converting text to Unicode.
216
  virtual GBool problematicForUnicode() = 0;
217
218
  // Returns true if this font swaps the left and right parens in its
219
  // ToUnicode map. This is likely a kludge used by bad PDF generators
220
  // with Arabic fonts.
221
  virtual GBool parensAreSwapped(XRef *xref) = 0;
222
223
protected:
224
225
  static GfxFontType getFontType(XRef *xref, Dict *fontDict, Ref *embID);
226
  void readFontDescriptor(XRef *xref, Dict *fontDict);
227
  CharCodeToUnicode *readToUnicodeCMap(Dict *fontDict, int nBits,
228
               CharCodeToUnicode *ctu);
229
  static GfxFontLoc *getExternalFont(GString *path, int fontNum,
230
             double oblique, GBool cid);
231
232
  GString *tag;     // PDF font tag
233
  Ref id;     // reference (used as unique ID)
234
  GString *name;    // font name
235
  GfxFontType type;   // type of font
236
  int flags;      // font descriptor flags
237
  GString *embFontName;   // name of embedded font
238
  Ref embFontID;    // ref to embedded font file stream
239
  double fontMat[6];    // font matrix
240
  double fontBBox[4];   // font bounding box
241
  double missingWidth;    // "default" width
242
  double ascent;    // max height above baseline
243
  double descent;   // max depth below baseline
244
  double declaredAscent;  // ascent value, before munging
245
  GBool hasToUnicode;   // true if the font has a ToUnicode map
246
  GBool ok;
247
};
248
249
//------------------------------------------------------------------------
250
// Gfx8BitFont
251
//------------------------------------------------------------------------
252
253
class Gfx8BitFont: public GfxFont {
254
public:
255
256
  Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, GString *nameA,
257
        GfxFontType typeA, Ref embFontIDA, Dict *fontDict);
258
259
  virtual ~Gfx8BitFont();
260
261
  virtual int getNextChar(char *s, int len, CharCode *code,
262
        Unicode *u, int uSize, int *uLen,
263
        double *dx, double *dy, double *ox, double *oy);
264
265
  // Return the encoding.
266
2.65k
  char **getEncoding() { return enc; }
267
268
  // Return the Unicode map.
269
  CharCodeToUnicode *getToUnicode();
270
271
  // Return the character name associated with <code>.
272
0
  char *getCharName(int code) { return enc[code]; }
273
274
  // Returns true if the PDF font specified an encoding.
275
0
  GBool getHasEncoding() { return hasEncoding; }
276
277
  // Returns true if the PDF font specified MacRomanEncoding.
278
0
  GBool getUsesMacRomanEnc() { return usesMacRomanEnc; }
279
280
  // Get width of a character.
281
2.60k
  double getWidth(Guchar c) { return widths[c]; }
282
283
  // Return a char code-to-GID mapping for the provided font file.
284
  // (This is only useful for TrueType fonts.)
285
  int *getCodeToGIDMap(FoFiTrueType *ff);
286
287
  // Return a char code-to-GID mapping for the provided font file.
288
  // (This is only useful for Type1C fonts.)
289
  int *getCodeToGIDMap(FoFiType1C *ff);
290
291
  // Return the Type 3 CharProc dictionary, or NULL if none.
292
  Dict *getCharProcs();
293
294
  // Return the Type 3 CharProc for the character associated with <code>.
295
  Object *getCharProc(int code, Object *proc);
296
  Object *getCharProcNF(int code, Object *proc);
297
298
  // Return the Type 3 Resources dictionary, or NULL if none.
299
  Dict *getResources();
300
301
  virtual GBool problematicForUnicode();
302
303
  virtual GBool parensAreSwapped(XRef *xref);
304
305
private:
306
307
  Base14FontMapEntry *base14; // for Base-14 fonts only; NULL otherwise
308
  char *enc[256];   // char code --> char name
309
  char encFree[256];    // boolean for each char name: if set,
310
        //   the string is malloc'ed
311
  CharCodeToUnicode *ctu; // char code --> Unicode
312
  GBool hasEncoding;
313
  GBool usesMacRomanEnc;
314
  GBool baseEncFromFontFile;
315
  GBool usedNumericHeuristic;
316
  double widths[256];   // character widths
317
  Object charProcs;   // Type 3 CharProcs dictionary
318
  Object resources;   // Type 3 Resources dictionary
319
320
  friend class GfxFont;
321
};
322
323
//------------------------------------------------------------------------
324
// GfxCIDFont
325
//------------------------------------------------------------------------
326
327
class GfxCIDFont: public GfxFont {
328
public:
329
330
  GfxCIDFont(XRef *xref, const char *tagA, Ref idA, GString *nameA,
331
       GfxFontType typeA, Ref embFontIDA, Dict *fontDict);
332
333
  virtual ~GfxCIDFont();
334
335
31.6k
  virtual GBool isCIDFont() { return gTrue; }
336
337
  virtual int getNextChar(char *s, int len, CharCode *code,
338
        Unicode *u, int uSize, int *uLen,
339
        double *dx, double *dy, double *ox, double *oy);
340
341
  // Return the writing mode (0=horizontal, 1=vertical).
342
  virtual int getWMode();
343
344
  // Return the Unicode map.
345
  CharCodeToUnicode *getToUnicode();
346
347
  // Get the collection name (<registry>-<ordering>).
348
  GString *getCollection();
349
350
  // Return the horizontal width for <cid>.
351
  double getWidth(CID cid);
352
353
  // Return the CID-to-GID mapping table.  These should only be called
354
  // if type is fontCIDType2.
355
1.22k
  int *getCIDToGID() { return cidToGID; }
356
0
  int getCIDToGIDLen() { return cidToGIDLen; }
357
358
  // Returns true if this font uses the Identity-H encoding (cmap),
359
  // and the Adobe-Identity character collection, and does not have a
360
  // CIDToGIDMap.  When this is true for a CID TrueType font, Adobe
361
  // appears to treat char codes as raw GIDs.
362
0
  GBool usesIdentityEncoding() { return identityEnc; }
363
364
  virtual GBool problematicForUnicode();
365
366
  virtual GBool parensAreSwapped(XRef *xref);
367
368
private:
369
370
  void readTrueTypeUnicodeMapping(XRef *xref);
371
  void getHorizontalMetrics(CID cid, double *w);
372
  void getVerticalMetrics(CID cid, double *h,
373
        double *vx, double *vy);
374
375
  GString *collection;    // collection name
376
  CMap *cMap;     // char code --> CID
377
  CharCodeToUnicode *ctu; // CID/char code --> Unicode
378
  GBool ctuUsesCharCode;  // true: ctu maps char code to Unicode;
379
        //   false: ctu maps CID to Unicode
380
  GfxFontCIDWidths widths;  // character widths
381
  int *cidToGID;    // CID --> GID mapping (for embedded
382
        //   TrueType fonts)
383
  int cidToGIDLen;
384
  GBool hasKnownCollection;
385
  GBool hasIdentityCIDToGID;
386
  GBool identityEnc;
387
};
388
389
//------------------------------------------------------------------------
390
// GfxFontDict
391
//------------------------------------------------------------------------
392
393
class GfxFontDict {
394
public:
395
396
  // Build the font dictionary, given the PDF font dictionary.
397
  GfxFontDict(XRef *xrefA, Ref *fontDictRef, Dict *fontDict);
398
399
  // Destructor.
400
  ~GfxFontDict();
401
402
  // Get the specified font.
403
  GfxFont *lookup(char *tag);
404
  GfxFont *lookupByRef(Ref ref);
405
406
  // Iterative access.
407
  int getNumFonts();
408
  GfxFont *getFont(int i);
409
410
private:
411
412
  friend class GfxFont;
413
414
  void loadAll();
415
  void load(char *tag, GfxFontDictEntry *entry);
416
  static int hashFontObject(Object *obj);
417
  static void hashFontObject1(Object *obj, FNVHash *h);
418
419
  XRef *xref;
420
  GHash *fonts;     // hash table of fonts, mapping from
421
        //   tag to GfxFontDictEntry; this may
422
        //   contain duplicates, i.e., two
423
        //   tags that map to the same font
424
  GList *uniqueFonts;   // list of all unique font objects (no dups)
425
        //   that have been loaded
426
};
427
428
#endif