Coverage Report

Created: 2026-06-22 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/GfxFont.cc
Line
Count
Source
1
//========================================================================
2
//
3
// GfxFont.cc
4
//
5
// Copyright 1996-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include <ctype.h>
15
#include <math.h>
16
#include <limits.h>
17
#include "gmem.h"
18
#include "gmempp.h"
19
#include "GList.h"
20
#include "GHash.h"
21
#include "Error.h"
22
#include "Object.h"
23
#include "Dict.h"
24
#include "GlobalParams.h"
25
#include "CMap.h"
26
#include "CharCodeToUnicode.h"
27
#include "FontEncodingTables.h"
28
#include "BuiltinFontTables.h"
29
#include "FoFiIdentifier.h"
30
#include "FoFiType1.h"
31
#include "FoFiType1C.h"
32
#include "FoFiTrueType.h"
33
#include "GfxFont.h"
34
35
//------------------------------------------------------------------------
36
37
struct Base14FontMapEntry {
38
  const char *altName;
39
  const char *base14Name;
40
};
41
42
static Base14FontMapEntry base14FontMap[] = {
43
  { "Arial",                        "Helvetica" },
44
  { "Arial,Bold",                   "Helvetica-Bold" },
45
  { "Arial,BoldItalic",             "Helvetica-BoldOblique" },
46
  { "Arial,Italic",                 "Helvetica-Oblique" },
47
  { "Arial-Bold",                   "Helvetica-Bold" },
48
  { "Arial-BoldItalic",             "Helvetica-BoldOblique" },
49
  { "Arial-BoldItalicMT",           "Helvetica-BoldOblique" },
50
  { "Arial-BoldMT",                 "Helvetica-Bold" },
51
  { "Arial-Italic",                 "Helvetica-Oblique" },
52
  { "Arial-ItalicMT",               "Helvetica-Oblique" },
53
  { "ArialMT",                      "Helvetica" },
54
  { "Courier",                      "Courier" },
55
  { "Courier,Bold",                 "Courier-Bold" },
56
  { "Courier,BoldItalic",           "Courier-BoldOblique" },
57
  { "Courier,Italic",               "Courier-Oblique" },
58
  { "Courier-Bold",                 "Courier-Bold" },
59
  { "Courier-BoldOblique",          "Courier-BoldOblique" },
60
  { "Courier-Oblique",              "Courier-Oblique" },
61
  { "CourierNew",                   "Courier" },
62
  { "CourierNew,Bold",              "Courier-Bold" },
63
  { "CourierNew,BoldItalic",        "Courier-BoldOblique" },
64
  { "CourierNew,Italic",            "Courier-Oblique" },
65
  { "CourierNew-Bold",              "Courier-Bold" },
66
  { "CourierNew-BoldItalic",        "Courier-BoldOblique" },
67
  { "CourierNew-Italic",            "Courier-Oblique" },
68
  { "CourierNewPS-BoldItalicMT",    "Courier-BoldOblique" },
69
  { "CourierNewPS-BoldMT",          "Courier-Bold" },
70
  { "CourierNewPS-ItalicMT",        "Courier-Oblique" },
71
  { "CourierNewPSMT",               "Courier" },
72
  { "Helvetica",                    "Helvetica" },
73
  { "Helvetica,Bold",               "Helvetica-Bold" },
74
  { "Helvetica,BoldItalic",         "Helvetica-BoldOblique" },
75
  { "Helvetica,Italic",             "Helvetica-Oblique" },
76
  { "Helvetica-Bold",               "Helvetica-Bold" },
77
  { "Helvetica-BoldItalic",         "Helvetica-BoldOblique" },
78
  { "Helvetica-BoldOblique",        "Helvetica-BoldOblique" },
79
  { "Helvetica-Italic",             "Helvetica-Oblique" },
80
  { "Helvetica-Oblique",            "Helvetica-Oblique" },
81
  { "Symbol",                       "Symbol" },
82
  { "Symbol,Bold",                  "Symbol" },
83
  { "Symbol,BoldItalic",            "Symbol" },
84
  { "Symbol,Italic",                "Symbol" },
85
  { "Times-Bold",                   "Times-Bold" },
86
  { "Times-BoldItalic",             "Times-BoldItalic" },
87
  { "Times-Italic",                 "Times-Italic" },
88
  { "Times-Roman",                  "Times-Roman" },
89
  { "TimesNewRoman",                "Times-Roman" },
90
  { "TimesNewRoman,Bold",           "Times-Bold" },
91
  { "TimesNewRoman,BoldItalic",     "Times-BoldItalic" },
92
  { "TimesNewRoman,Italic",         "Times-Italic" },
93
  { "TimesNewRoman-Bold",           "Times-Bold" },
94
  { "TimesNewRoman-BoldItalic",     "Times-BoldItalic" },
95
  { "TimesNewRoman-Italic",         "Times-Italic" },
96
  { "TimesNewRomanPS",              "Times-Roman" },
97
  { "TimesNewRomanPS-Bold",         "Times-Bold" },
98
  { "TimesNewRomanPS-BoldItalic",   "Times-BoldItalic" },
99
  { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
100
  { "TimesNewRomanPS-BoldMT",       "Times-Bold" },
101
  { "TimesNewRomanPS-Italic",       "Times-Italic" },
102
  { "TimesNewRomanPS-ItalicMT",     "Times-Italic" },
103
  { "TimesNewRomanPSMT",            "Times-Roman" },
104
  { "TimesNewRomanPSMT,Bold",       "Times-Bold" },
105
  { "TimesNewRomanPSMT,BoldItalic", "Times-BoldItalic" },
106
  { "TimesNewRomanPSMT,Italic",     "Times-Italic" },
107
  { "ZapfDingbats",                 "ZapfDingbats" }
108
};
109
110
//------------------------------------------------------------------------
111
112
// index: {fixed:0, sans-serif:4, serif:8} + bold*2 + italic
113
// NB: must be in same order as psSubstFonts in PSOutputDev.cc
114
static const char *base14SubstFonts[14] = {
115
  "Courier",
116
  "Courier-Oblique",
117
  "Courier-Bold",
118
  "Courier-BoldOblique",
119
  "Helvetica",
120
  "Helvetica-Oblique",
121
  "Helvetica-Bold",
122
  "Helvetica-BoldOblique",
123
  "Times-Roman",
124
  "Times-Italic",
125
  "Times-Bold",
126
  "Times-BoldItalic",
127
  // the last two are never used for substitution
128
  "Symbol",
129
  "ZapfDingbats"
130
};
131
132
//------------------------------------------------------------------------
133
134
18.4M
static int readFromStream(void *data) {
135
18.4M
  return ((Stream *)data)->getChar();
136
18.4M
}
137
138
//------------------------------------------------------------------------
139
// GfxFontLoc
140
//------------------------------------------------------------------------
141
142
5.17k
GfxFontLoc::GfxFontLoc() {
143
5.17k
  path = NULL;
144
5.17k
  fontNum = 0;
145
5.17k
  oblique = 0;
146
5.17k
  encoding = NULL;
147
5.17k
  substIdx = -1;
148
5.17k
}
149
150
5.17k
GfxFontLoc::~GfxFontLoc() {
151
5.17k
  if (path) {
152
0
    delete path;
153
0
  }
154
5.17k
  if (encoding) {
155
0
    delete encoding;
156
0
  }
157
5.17k
}
158
159
//------------------------------------------------------------------------
160
// GfxFont
161
//------------------------------------------------------------------------
162
163
GfxFont *GfxFont::makeFont(XRef *xref, const char *tagA,
164
29.8k
         Ref idA, Dict *fontDict) {
165
29.8k
  GString *nameA;
166
29.8k
  Ref embFontIDA;
167
29.8k
  GfxFontType typeA;
168
29.8k
  GfxFont *font;
169
29.8k
  Object obj1;
170
171
  // get base font name
172
29.8k
  nameA = NULL;
173
29.8k
  fontDict->lookup("BaseFont", &obj1);
174
29.8k
  if (obj1.isName()) {
175
20.2k
    nameA = new GString(obj1.getName());
176
20.2k
  } else if (obj1.isString()) {
177
6
    nameA = obj1.getString()->copy();
178
6
  }
179
29.8k
  obj1.free();
180
181
  // get embedded font ID and font type
182
29.8k
  typeA = getFontType(xref, fontDict, &embFontIDA);
183
184
  // create the font object
185
29.8k
  font = NULL;
186
29.8k
  if (typeA < fontCIDType0) {
187
25.4k
    font = new Gfx8BitFont(xref, tagA, idA, nameA, typeA, embFontIDA,
188
25.4k
         fontDict);
189
25.4k
  } else {
190
4.35k
    font = new GfxCIDFont(xref, tagA, idA, nameA, typeA, embFontIDA,
191
4.35k
        fontDict);
192
4.35k
  }
193
194
29.8k
  return font;
195
29.8k
}
196
197
7.40k
GfxFont *GfxFont::makeDefaultFont(XRef *xref) {
198
7.40k
  Object type, subtype, baseFont;
199
7.40k
  type.initName("Font");
200
7.40k
  subtype.initName("Type1");
201
7.40k
  baseFont.initName("Helvetica");
202
7.40k
  Object fontDict;
203
7.40k
  fontDict.initDict(xref);
204
7.40k
  fontDict.dictAdd(copyString("Type"), &type);
205
7.40k
  fontDict.dictAdd(copyString("Subtype"), &subtype);
206
7.40k
  fontDict.dictAdd(copyString("BaseFont"), &baseFont);
207
208
7.40k
  Ref r;
209
7.40k
  r.gen = 100000;
210
7.40k
  r.num = GfxFontDict::hashFontObject(&fontDict);
211
212
7.40k
  GfxFont *font = makeFont(xref, "undef", r, fontDict.getDict());
213
7.40k
  fontDict.free();
214
215
7.40k
  return font;
216
7.40k
}
217
218
GfxFont::GfxFont(const char *tagA, Ref idA, GString *nameA,
219
29.8k
     GfxFontType typeA, Ref embFontIDA) {
220
29.8k
  ok = gFalse;
221
29.8k
  tag = new GString(tagA);
222
29.8k
  id = idA;
223
29.8k
  name = nameA;
224
29.8k
  type = typeA;
225
29.8k
  embFontID = embFontIDA;
226
29.8k
  embFontName = NULL;
227
29.8k
  hasToUnicode = gFalse;
228
29.8k
}
229
230
29.8k
GfxFont::~GfxFont() {
231
29.8k
  delete tag;
232
29.8k
  if (name) {
233
20.2k
    delete name;
234
20.2k
  }
235
29.8k
  if (embFontName) {
236
7.66k
    delete embFontName;
237
7.66k
  }
238
29.8k
}
239
240
// This function extracts three pieces of information:
241
// 1. the "expected" font type, i.e., the font type implied by
242
//    Font.Subtype, DescendantFont.Subtype, and
243
//    FontDescriptor.FontFile3.Subtype
244
// 2. the embedded font object ID
245
// 3. the actual font type - determined by examining the embedded font
246
//    if there is one, otherwise equal to the expected font type
247
// If the expected and actual font types don't match, a warning
248
// message is printed.  The expected font type is not used for
249
// anything else.
250
29.8k
GfxFontType GfxFont::getFontType(XRef *xref, Dict *fontDict, Ref *embID) {
251
29.8k
  GfxFontType t, expectedType;
252
29.8k
  FoFiIdentifierType fft;
253
29.8k
  Dict *fontDict2;
254
29.8k
  Object subtype, fontDesc, obj1, obj2, obj3, obj4;
255
29.8k
  GBool isType0, err;
256
257
29.8k
  t = fontUnknownType;
258
29.8k
  embID->num = embID->gen = -1;
259
29.8k
  err = gFalse;
260
261
29.8k
  fontDict->lookup("Subtype", &subtype);
262
29.8k
  expectedType = fontUnknownType;
263
29.8k
  isType0 = gFalse;
264
29.8k
  if (subtype.isName("Type1") || subtype.isName("MMType1")) {
265
12.0k
    expectedType = fontType1;
266
17.7k
  } else if (subtype.isName("Type1C")) {
267
1
    expectedType = fontType1C;
268
17.7k
  } else if (subtype.isName("Type3")) {
269
4.21k
    expectedType = fontType3;
270
13.5k
  } else if (subtype.isName("TrueType")) {
271
1.52k
    expectedType = fontTrueType;
272
12.0k
  } else if (subtype.isName("Type0")) {
273
5.84k
    isType0 = gTrue;
274
6.17k
  } else {
275
6.17k
    error(errSyntaxWarning, -1, "Unknown font type: '{0:s}'",
276
6.17k
    subtype.isName() ? subtype.getName() : "???");
277
6.17k
  }
278
29.8k
  subtype.free();
279
280
29.8k
  fontDict2 = fontDict;
281
29.8k
  if (fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
282
9.68k
    if (obj1.arrayGetLength() == 0) {
283
1
      error(errSyntaxWarning, -1, "Empty DescendantFonts array in font");
284
1
      obj2.initNull();
285
9.68k
    } else if (obj1.arrayGet(0, &obj2)->isDict()) {
286
8.61k
      if (!isType0) {
287
4.15k
  error(errSyntaxWarning, -1, "Non-CID font with DescendantFonts array");
288
4.15k
      }
289
8.61k
      fontDict2 = obj2.getDict();
290
8.61k
      fontDict2->lookup("Subtype", &subtype);
291
8.61k
      if (subtype.isName("CIDFontType0")) {
292
2.12k
  if (isType0) {
293
1.69k
    expectedType = fontCIDType0;
294
1.69k
  }
295
6.48k
      } else if (subtype.isName("CIDFontType2")) {
296
3.21k
  if (isType0) {
297
2.31k
    expectedType = fontCIDType2;
298
2.31k
  }
299
3.21k
      }
300
8.61k
      subtype.free();
301
8.61k
    }
302
20.1k
  } else {
303
20.1k
    obj2.initNull();
304
20.1k
  }
305
306
  // NB: the PDF spec doesn't say anything about precedence, but Adobe
307
  // uses FontFile3 over FontFile2 if both are present.
308
29.8k
  if (fontDict2->lookup("FontDescriptor", &fontDesc)->isDict()) {
309
12.7k
    if (fontDesc.dictLookupNF("FontFile3", &obj3)->isRef()) {
310
3.25k
      *embID = obj3.getRef();
311
3.25k
      if (obj3.fetch(xref, &obj4)->isStream()) {
312
3.17k
  obj4.streamGetDict()->lookup("Subtype", &subtype);
313
3.17k
  if (subtype.isName("Type1")) {
314
9
    if (expectedType != fontType1) {
315
9
      err = gTrue;
316
9
      expectedType = isType0 ? fontCIDType0 : fontType1;
317
9
    }
318
3.16k
  } else if (subtype.isName("Type1C")) {
319
1.43k
    if (expectedType == fontType1) {
320
1.26k
      expectedType = fontType1C;
321
1.26k
    } else if (expectedType != fontType1C) {
322
172
      err = gTrue;
323
172
      expectedType = isType0 ? fontCIDType0C : fontType1C;
324
172
    }
325
1.73k
  } else if (subtype.isName("TrueType")) {
326
0
    if (expectedType != fontTrueType) {
327
0
      err = gTrue;
328
0
      expectedType = isType0 ? fontCIDType2 : fontTrueType;
329
0
    }
330
1.73k
  } else if (subtype.isName("CIDFontType0C")) {
331
512
    if (expectedType == fontCIDType0) {
332
129
      expectedType = fontCIDType0C;
333
383
    } else {
334
383
      err = gTrue;
335
383
      expectedType = isType0 ? fontCIDType0C : fontType1C;
336
383
    }
337
1.22k
  } else if (subtype.isName("OpenType")) {
338
531
    if (expectedType == fontTrueType) {
339
0
      expectedType = fontTrueTypeOT;
340
531
    } else if (expectedType == fontType1) {
341
80
      expectedType = fontType1COT;
342
451
    } else if (expectedType == fontCIDType0) {
343
46
      expectedType = fontCIDType0COT;
344
405
    } else if (expectedType == fontCIDType2) {
345
5
      expectedType = fontCIDType2OT;
346
400
    } else {
347
400
      err = gTrue;
348
400
    }
349
690
  } else {
350
690
    error(errSyntaxError, -1, "Unknown font type '{0:s}'",
351
690
    subtype.isName() ? subtype.getName() : "???");
352
690
  }
353
3.17k
  subtype.free();
354
3.17k
      }
355
3.25k
      obj4.free();
356
3.25k
    }
357
12.7k
    obj3.free();
358
12.7k
    if (embID->num == -1) {
359
9.50k
      if (fontDesc.dictLookupNF("FontFile2", &obj3)->isRef()) {
360
4.62k
  *embID = obj3.getRef();
361
4.62k
  if (isType0) {
362
2.00k
    expectedType = fontCIDType2;
363
2.61k
  } else if (expectedType != fontTrueType) {
364
1.17k
    err = gTrue;
365
1.17k
  }
366
4.62k
      }
367
9.50k
      obj3.free();
368
9.50k
    }
369
12.7k
    if (embID->num == -1) {
370
4.88k
      if (fontDesc.dictLookupNF("FontFile", &obj3)->isRef()) {
371
1.76k
  *embID = obj3.getRef();
372
1.76k
  if (expectedType != fontType1) {
373
379
    err = gTrue;
374
379
  }
375
1.76k
      }
376
4.88k
      obj3.free();
377
4.88k
    }
378
12.7k
  }
379
29.8k
  fontDesc.free();
380
381
29.8k
  t = fontUnknownType;
382
29.8k
  if (embID->num >= 0) {
383
9.64k
    obj3.initRef(embID->num, embID->gen);
384
9.64k
    obj3.fetch(xref, &obj4);
385
9.64k
    if (obj4.isStream()) {
386
9.31k
      obj4.streamReset();
387
9.31k
      fft = FoFiIdentifier::identifyStream(&readFromStream, obj4.getStream());
388
9.31k
      obj4.streamClose();
389
9.31k
      switch (fft) {
390
1.23k
      case fofiIdType1PFA:
391
1.46k
      case fofiIdType1PFB:
392
1.46k
  t = fontType1;
393
1.46k
  break;
394
2.24k
      case fofiIdCFF8Bit:
395
2.24k
  t = isType0 ? fontCIDType0C : fontType1C;
396
2.24k
  break;
397
66
      case fofiIdCFFCID:
398
66
  t = fontCIDType0C;
399
66
  break;
400
3.80k
      case fofiIdTrueType:
401
3.80k
      case fofiIdTrueTypeCollection:
402
3.80k
  t = isType0 ? fontCIDType2 : fontTrueType;
403
3.80k
  break;
404
512
      case fofiIdOpenTypeCFF8Bit:
405
512
  t = isType0 ? fontCIDType0COT : fontType1COT;
406
512
  break;
407
0
      case fofiIdOpenTypeCFFCID:
408
0
  t = fontCIDType0COT;
409
0
  break;
410
1.21k
      default:
411
1.21k
  error(errSyntaxError, -1, "Embedded font file may be invalid");
412
1.21k
  break;
413
9.31k
      }
414
9.31k
    } else {
415
334
      error(errSyntaxError, -1, "Embedded font object is wrong type");
416
334
    }
417
9.64k
    obj4.free();
418
9.64k
    obj3.free();
419
9.64k
  }
420
421
  // If the embedded font doesn't exist, or it's not a stream object,
422
  // or we couldn't identify its type, then remove the reference so
423
  // nothing tries to use it later.
424
29.8k
  if (t == fontUnknownType) {
425
21.7k
    embID->num = -1;
426
21.7k
    embID->gen = -1;
427
21.7k
    t = expectedType;
428
21.7k
  }
429
430
29.8k
  if (t != expectedType) {
431
2.36k
    err = gTrue;
432
2.36k
  }
433
434
29.8k
  if (err) {
435
3.28k
    error(errSyntaxWarning, -1,
436
3.28k
    "Mismatch between font type and embedded font file");
437
3.28k
  }
438
439
29.8k
  obj2.free();
440
29.8k
  obj1.free();
441
442
29.8k
  return t;
443
29.8k
}
444
445
29.7k
void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
446
29.7k
  Object obj1, obj2, obj3, obj4;
447
29.7k
  double t, t2;
448
29.7k
  int i;
449
450
  // assume Times-Roman by default (for substitution purposes)
451
29.7k
  flags = fontSerif;
452
453
29.7k
  if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
454
455
    // get flags
456
10.1k
    if (obj1.dictLookup("Flags", &obj2)->isInt()) {
457
6.57k
      flags = obj2.getInt();
458
6.57k
    }
459
10.1k
    obj2.free();
460
461
    // get name
462
10.1k
    obj1.dictLookup("FontName", &obj2);
463
10.1k
    if (obj2.isName()) {
464
7.28k
      embFontName = new GString(obj2.getName());
465
7.28k
    }
466
10.1k
    obj2.free();
467
468
    // look for MissingWidth
469
10.1k
    obj1.dictLookup("MissingWidth", &obj2);
470
10.1k
    if (obj2.isNum()) {
471
387
      missingWidth = obj2.getNum();
472
387
    }
473
10.1k
    obj2.free();
474
475
    // font FontBBox
476
10.1k
    if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
477
29.4k
      for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
478
23.5k
  if (obj2.arrayGet(i, &obj3)->isNum()) {
479
21.6k
    fontBBox[i] = 0.001 * obj3.getNum();
480
21.6k
  }
481
23.5k
  obj3.free();
482
23.5k
      }
483
5.92k
    }
484
10.1k
    obj2.free();
485
486
    // get Ascent (but also look at CapHeight)
487
10.1k
    obj1.dictLookup("Ascent", &obj2);
488
10.1k
    obj1.dictLookup("CapHeight", &obj3);
489
10.1k
    if (obj2.isNum() || obj3.isNum()) {
490
8.33k
      if (obj2.isNum()) {
491
7.94k
  t = 0.001 * obj2.getNum();
492
  // some broken font descriptors specify a negative ascent
493
7.94k
  if (t < 0) {
494
12
    t = -t;
495
12
  }
496
7.94k
      } else {
497
386
  t = 0;
498
386
      }
499
8.33k
      if (obj3.isNum()) {
500
6.59k
  t2 = 0.001 * obj3.getNum();
501
  // some broken font descriptors specify a negative ascent
502
6.59k
  if (t2 < 0) {
503
43
    t2 = -t2;
504
43
  }
505
6.59k
      } else {
506
1.73k
  t2 = 0;
507
1.73k
      }
508
8.33k
      if (t != 0 && t < 1.9) {
509
7.61k
  declaredAscent = t;
510
7.61k
      }
511
      // The ascent is generally expected to be in the (0.55, 1.9) range.
512
      // If Ascent and/or CapHeight is in that range, use the smaller one
513
      // (because the most common problem is that Ascent is too large).
514
      // Otherwise check for a value in (0, 0.55], and use that.
515
8.33k
      if (t > 0.55 && t < 1.9) {
516
7.51k
  if (t2 > 0.55 && t2 < 1.9) {
517
5.73k
    ascent = (t < t2) ? t : t2;
518
5.73k
  } else {
519
1.77k
    ascent = t;
520
1.77k
  }
521
7.51k
      } else if (t2 > 0.55 && t2 < 1.9) {
522
151
  ascent = t2;
523
671
      } else if (fontBBox[3] > 0.55 && fontBBox[3] < 1.9 &&
524
299
     fontBBox[1] < fontBBox[3]) {
525
299
  ascent = fontBBox[3];
526
372
      } else if (t > 0 && t < 1.9) {
527
13
  ascent = t;
528
359
      } else if (t2 > 0 && t2 < 1.9) {
529
26
  ascent = t2;
530
26
      }
531
8.33k
    }
532
10.1k
    obj2.free();
533
10.1k
    obj3.free();
534
535
    // get Descent
536
10.1k
    obj1.dictLookup("Descent", &obj2);
537
10.1k
    if (obj2.isNum()) {
538
7.95k
      t = 0.001 * obj2.getNum();
539
      // some broken font descriptors specify a positive descent
540
7.95k
      if (t > 0) {
541
97
  t = -t;
542
97
      }
543
      // some broken font descriptors set ascent and descent to 0
544
7.95k
      if (t < 0 && t > -1.9) {
545
7.21k
  descent = t;
546
7.21k
      } else if (fontBBox[1] > -1.9 && fontBBox[1] <= 0 &&
547
697
     fontBBox[1] < fontBBox[3]) {
548
560
  descent = fontBBox[1];
549
560
      }
550
7.95k
    }
551
10.1k
    obj2.free();
552
553
10.1k
  }
554
29.7k
  obj1.free();
555
556
  // scan font name for bold/italic tags and update the flags
557
29.7k
  if (name) {
558
20.1k
    i = name->getLength();
559
20.1k
    if (i > 2 && !strncmp(name->getCString() + i - 2, "MT", 2)) {
560
4.10k
      i -= 2;
561
4.10k
    }
562
20.1k
    if (i > 6 && !strncmp(name->getCString() + i - 6, "Italic", 6)) {
563
223
      flags |= fontItalic;
564
223
      i -= 6;
565
19.9k
    } else if (i > 2 && !strncmp(name->getCString() + i - 2, "It", 2)) {
566
5
      flags |= fontItalic;
567
5
      i -= 2;
568
19.9k
    } else if (i > 7 && !strncmp(name->getCString() + i - 7, "Oblique", 7)) {
569
17
      flags |= fontItalic;
570
17
      i -= 7;
571
17
    }
572
20.1k
    if (i > 0) {
573
20.1k
      char c = name->getChar(i-1);
574
20.1k
      if (!((c >= 'A' && c <= 'Z') ||
575
15.4k
      (c >= 'a' && c <= 'z') ||
576
1.97k
      (c >= '0' && c <= '9'))) {
577
627
  --i;
578
627
      }
579
20.1k
    }
580
20.1k
    if (i > 4 && !strncmp(name->getCString() + i - 4, "Bold", 4)) {
581
1.72k
      flags |= fontBold;
582
1.72k
    }
583
20.1k
  }
584
29.7k
}
585
586
CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits,
587
27.9k
                CharCodeToUnicode *ctu) {
588
27.9k
  GString *buf;
589
27.9k
  Object obj1;
590
27.9k
  char buf2[4096];
591
27.9k
  int n;
592
593
27.9k
  if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
594
21.6k
    obj1.free();
595
21.6k
    return NULL;
596
21.6k
  }
597
6.26k
  buf = new GString();
598
6.26k
  obj1.streamReset();
599
11.1k
  while ((n = obj1.streamGetBlock(buf2, sizeof(buf2))) > 0) {
600
4.93k
    buf->append(buf2, n);
601
4.93k
  }
602
6.26k
  obj1.streamClose();
603
6.26k
  obj1.free();
604
6.26k
  if (ctu) {
605
4.86k
    ctu->mergeCMap(buf, nBits);
606
4.86k
  } else {
607
1.39k
    ctu = CharCodeToUnicode::parseCMap(buf, nBits);
608
1.39k
  }
609
6.26k
  delete buf;
610
6.26k
  hasToUnicode = gTrue;
611
6.26k
  return ctu;
612
27.9k
}
613
614
27.9k
GfxFontLoc *GfxFont::locateFont(XRef *xref, GBool ps) {
615
27.9k
  GfxFontLoc *fontLoc;
616
27.9k
  SysFontType sysFontType;
617
27.9k
  FoFiIdentifierType fft;
618
27.9k
  GString *path, *base14Name, *substName;
619
27.9k
  PSFontParam16 *psFont16;
620
27.9k
  Object refObj, embFontObj;
621
27.9k
  int substIdx, fontNum;
622
27.9k
  double oblique;
623
27.9k
  GBool embed;
624
625
27.9k
  if (type == fontType3) {
626
0
    return NULL;
627
0
  }
628
629
  //----- embedded font
630
27.9k
  if (embFontID.num >= 0) {
631
5.17k
    embed = gTrue;
632
5.17k
    refObj.initRef(embFontID.num, embFontID.gen);
633
5.17k
    refObj.fetch(xref, &embFontObj);
634
5.17k
    if (!embFontObj.isStream()) {
635
0
      error(errSyntaxError, -1, "Embedded font object is wrong type");
636
0
      embed = gFalse;
637
0
    }
638
5.17k
    embFontObj.free();
639
5.17k
    refObj.free();
640
5.17k
    if (embed) {
641
5.17k
      if (ps) {
642
0
  switch (type) {
643
0
  case fontType1:
644
0
  case fontType1C:
645
0
  case fontType1COT:
646
0
    embed = globalParams->getPSEmbedType1();
647
0
    break;
648
0
  case fontTrueType:
649
0
  case fontTrueTypeOT:
650
0
    embed = globalParams->getPSEmbedTrueType();
651
0
    break;
652
0
  case fontCIDType0C:
653
0
  case fontCIDType0COT:
654
0
    embed = globalParams->getPSEmbedCIDPostScript();
655
0
    break;
656
0
  case fontCIDType2:
657
0
  case fontCIDType2OT:
658
0
    embed = globalParams->getPSEmbedCIDTrueType();
659
0
    break;
660
0
  default:
661
0
    break;
662
0
  }
663
0
      }
664
5.17k
      if (embed) {
665
5.17k
  fontLoc = new GfxFontLoc();
666
5.17k
  fontLoc->locType = gfxFontLocEmbedded;
667
5.17k
  fontLoc->fontType = type;
668
5.17k
  fontLoc->embFontID = embFontID;
669
5.17k
  return fontLoc;
670
5.17k
      }
671
5.17k
    }
672
5.17k
  }
673
674
  //----- PS passthrough
675
22.7k
  if (ps && name && !isCIDFont() && globalParams->getPSFontPassthrough()) {
676
0
    fontLoc = new GfxFontLoc();
677
0
    fontLoc->locType = gfxFontLocResident;
678
0
    fontLoc->fontType = fontType1;
679
0
    fontLoc->path = name->copy();
680
0
    return fontLoc;
681
0
  }
682
683
  //----- external font file (fontFile, fontDir)
684
22.7k
  if (name && (path = globalParams->findFontFile(name))) {
685
0
    if ((fontLoc = getExternalFont(path, 0, 0, isCIDFont()))) {
686
0
      return fontLoc;
687
0
    }
688
0
  }
689
690
  //----- PS resident Base-14 font
691
22.7k
  if (ps && !isCIDFont() && ((Gfx8BitFont *)this)->base14) {
692
0
    fontLoc = new GfxFontLoc();
693
0
    fontLoc->locType = gfxFontLocResident;
694
0
    fontLoc->fontType = fontType1;
695
0
    fontLoc->path = new GString(((Gfx8BitFont *)this)->base14->base14Name);
696
0
    return fontLoc;
697
0
  }
698
699
  //----- external font file for Base-14 font
700
22.7k
  if (!ps && !isCIDFont() && ((Gfx8BitFont *)this)->base14) {
701
19.5k
    base14Name = new GString(((Gfx8BitFont *)this)->base14->base14Name);
702
19.5k
    path = globalParams->findBase14FontFile(base14Name, &fontNum, &oblique);
703
19.5k
    delete base14Name;
704
19.5k
    if (path && (fontLoc = getExternalFont(path, fontNum, oblique, gFalse))) {
705
0
      return fontLoc;
706
0
    }
707
19.5k
  }
708
709
  //----- system font
710
22.7k
  if (name && (path = globalParams->findSystemFontFile(name, &sysFontType,
711
22.1k
                   &fontNum))) {
712
0
    fontLoc = new GfxFontLoc();
713
0
    fontLoc->locType = gfxFontLocExternal;
714
0
    fontLoc->path = path;
715
0
    fontLoc->fontNum = fontNum;
716
0
    fft = FoFiIdentifier::identifyFile(fontLoc->path->getCString());
717
0
    switch (fft) {
718
0
    case fofiIdType1PFA:
719
0
    case fofiIdType1PFB:
720
0
      fontLoc->fontType = fontType1;
721
0
      break;
722
0
    case fofiIdCFF8Bit:
723
0
      fontLoc->fontType = fontType1C;
724
0
      break;
725
0
    case fofiIdCFFCID:
726
0
      fontLoc->fontType = fontCIDType0C;
727
0
      break;
728
0
    case fofiIdTrueType:
729
0
    case fofiIdTrueTypeCollection:
730
0
      fontLoc->fontType = isCIDFont() ? fontCIDType2 : fontTrueType;
731
0
      break;
732
0
    case fofiIdOpenTypeCFF8Bit:
733
0
      fontLoc->fontType = fontType1COT;
734
0
      break;
735
0
    case fofiIdOpenTypeCFFCID:
736
0
      fontLoc->fontType = fontCIDType0COT;
737
0
      break;
738
0
    case fofiIdDfont:
739
0
      fontLoc->fontType = isCIDFont() ? fontCIDType2 : fontTrueType;
740
0
      break;
741
0
    case fofiIdUnknown:
742
0
    case fofiIdError:
743
0
    default:
744
0
      delete fontLoc;
745
0
      fontLoc = NULL;
746
0
      break;
747
0
    }
748
0
    if (fontLoc) {
749
0
      return fontLoc;
750
0
    }
751
0
  }
752
753
22.7k
  if (!isCIDFont()) {
754
755
    //----- 8-bit PS resident font
756
20.9k
    if (ps) {
757
0
      if (name && (path = globalParams->getPSResidentFont(name))) {
758
0
  fontLoc = new GfxFontLoc();
759
0
  fontLoc->locType = gfxFontLocResident;
760
0
  fontLoc->fontType = fontType1;
761
0
  fontLoc->path = path;
762
0
  return fontLoc;
763
0
      }
764
0
    }
765
766
    //----- 8-bit font substitution
767
20.9k
    if (flags & fontFixedWidth) {
768
2.24k
      substIdx = 0;
769
18.6k
    } else if (flags & fontSerif) {
770
18.2k
      substIdx = 8;
771
18.2k
    } else {
772
419
      substIdx = 4;
773
419
    }
774
20.9k
    if (isBold()) {
775
27
      substIdx += 2;
776
27
    }
777
20.9k
    if (isItalic()) {
778
0
      substIdx += 1;
779
0
    }
780
20.9k
    substName = new GString(base14SubstFonts[substIdx]);
781
20.9k
    if (ps) {
782
0
      error(errSyntaxWarning, -1, "Substituting font '{0:s}' for '{1:t}'",
783
0
      base14SubstFonts[substIdx], name);
784
0
      fontLoc = new GfxFontLoc();
785
0
      fontLoc->locType = gfxFontLocResident;
786
0
      fontLoc->fontType = fontType1;
787
0
      fontLoc->path = substName;
788
0
      fontLoc->substIdx = substIdx;
789
0
      return fontLoc;
790
20.9k
    } else {
791
20.9k
      path = globalParams->findBase14FontFile(substName, &fontNum, &oblique);
792
20.9k
      delete substName;
793
20.9k
      if (path) {
794
0
  if ((fontLoc = getExternalFont(path, fontNum, oblique, gFalse))) {
795
0
    error(errSyntaxWarning, -1, "Substituting font '{0:s}' for '{1:t}'",
796
0
    base14SubstFonts[substIdx], name);
797
0
    fontLoc->substIdx = substIdx;
798
0
    return fontLoc;
799
0
  }
800
0
      }
801
20.9k
    }
802
803
    // failed to find a substitute font
804
20.9k
    return NULL;
805
20.9k
  }
806
807
  //----- 16-bit PS resident font
808
1.82k
  if (ps && name && ((psFont16 = globalParams->getPSResidentFont16(
809
0
           name,
810
0
           ((GfxCIDFont *)this)->getWMode())))) {
811
0
    fontLoc = new GfxFontLoc();
812
0
    fontLoc->locType = gfxFontLocResident;
813
0
    fontLoc->fontType = fontCIDType0; // this is not used
814
0
    fontLoc->path = psFont16->psFontName->copy();
815
0
    fontLoc->encoding = psFont16->encoding->copy();
816
0
    fontLoc->wMode = psFont16->wMode;
817
0
    return fontLoc;
818
0
  }
819
1.82k
  if (ps && ((psFont16 = globalParams->getPSResidentFontCC(
820
0
         ((GfxCIDFont *)this)->getCollection(),
821
0
         ((GfxCIDFont *)this)->getWMode())))) {
822
0
    error(errSyntaxWarning, -1, "Substituting font '{0:t}' for '{1:t}'",
823
0
    psFont16->psFontName, name);
824
0
    fontLoc = new GfxFontLoc();
825
0
    fontLoc->locType = gfxFontLocResident;
826
0
    fontLoc->fontType = fontCIDType0; // this is not used
827
0
    fontLoc->path = psFont16->psFontName->copy();
828
0
    fontLoc->encoding = psFont16->encoding->copy();
829
0
    fontLoc->wMode = psFont16->wMode;
830
0
    return fontLoc;
831
0
  }
832
833
  //----- CID font substitution
834
1.82k
  if ((path = globalParams->findCCFontFile(
835
1.82k
        ((GfxCIDFont *)this)->getCollection()))) {
836
0
    if ((fontLoc = getExternalFont(path, 0, 0, gTrue))) {
837
0
      error(errSyntaxWarning, -1, "Substituting font '{0:t}' for '{1:t}'",
838
0
      fontLoc->path, name);
839
0
      return fontLoc;
840
0
    }
841
0
  }
842
843
  // failed to find a substitute font
844
1.82k
  return NULL;
845
1.82k
}
846
847
0
GfxFontLoc *GfxFont::locateBase14Font(GString *base14Name) {
848
0
  GString *path;
849
0
  int fontNum;
850
0
  double oblique;
851
852
0
  path = globalParams->findBase14FontFile(base14Name, &fontNum, &oblique);
853
0
  if (!path) {
854
0
    return NULL;
855
0
  }
856
0
  return getExternalFont(path, fontNum, oblique, gFalse);
857
0
}
858
859
GfxFontLoc *GfxFont::getExternalFont(GString *path, int fontNum,
860
0
             double oblique, GBool cid) {
861
0
  FoFiIdentifierType fft;
862
0
  GfxFontType fontType;
863
0
  GfxFontLoc *fontLoc;
864
865
0
  fft = FoFiIdentifier::identifyFile(path->getCString());
866
0
  switch (fft) {
867
0
  case fofiIdType1PFA:
868
0
  case fofiIdType1PFB:
869
0
    fontType = fontType1;
870
0
    break;
871
0
  case fofiIdCFF8Bit:
872
0
    fontType = fontType1C;
873
0
    break;
874
0
  case fofiIdCFFCID:
875
0
    fontType = fontCIDType0C;
876
0
    break;
877
0
  case fofiIdTrueType:
878
0
  case fofiIdTrueTypeCollection:
879
0
    fontType = cid ? fontCIDType2 : fontTrueType;
880
0
    break;
881
0
  case fofiIdOpenTypeCFF8Bit:
882
0
    fontType = fontType1COT;
883
0
    break;
884
0
  case fofiIdOpenTypeCFFCID:
885
0
    fontType = fontCIDType0COT;
886
0
    break;
887
0
  case fofiIdDfont:
888
0
    fontType = cid ? fontCIDType2 : fontTrueType;
889
0
    break;
890
0
  case fofiIdUnknown:
891
0
  case fofiIdError:
892
0
  default:
893
0
    fontType = fontUnknownType;
894
0
    break;
895
0
  }
896
0
  if (fontType == fontUnknownType ||
897
0
      (cid ? (fontType < fontCIDType0)
898
0
           : (fontType >= fontCIDType0))) {
899
0
    delete path;
900
0
    return NULL;
901
0
  }
902
0
  fontLoc = new GfxFontLoc();
903
0
  fontLoc->locType = gfxFontLocExternal;
904
0
  fontLoc->fontType = fontType;
905
0
  fontLoc->path = path;
906
0
  fontLoc->fontNum = fontNum;
907
0
  fontLoc->oblique = oblique;
908
0
  return fontLoc;
909
0
}
910
911
3.42k
char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
912
3.42k
  char *buf;
913
3.42k
  Object obj1, obj2;
914
3.42k
  Stream *str;
915
3.42k
  int size, n;
916
917
3.42k
  obj1.initRef(embFontID.num, embFontID.gen);
918
3.42k
  obj1.fetch(xref, &obj2);
919
3.42k
  if (!obj2.isStream()) {
920
0
    error(errSyntaxError, -1, "Embedded font file is not a stream");
921
0
    obj2.free();
922
0
    obj1.free();
923
0
    embFontID.num = -1;
924
0
    return NULL;
925
0
  }
926
3.42k
  str = obj2.getStream();
927
928
3.42k
  size = 4096;
929
3.42k
  buf = (char *)gmalloc(size);
930
3.42k
  *len = 0;
931
3.42k
  str->reset();
932
10.5k
  do {
933
10.5k
    if (*len > size - 4096) {
934
4.49k
      if (size > INT_MAX / 2) {
935
0
  error(errSyntaxError, -1, "Embedded font file is too large");
936
0
  break;
937
0
      }
938
4.49k
      size *= 2;
939
4.49k
      buf = (char *)grealloc(buf, size);
940
4.49k
    }
941
10.5k
    n = str->getBlock(buf + *len, 4096);
942
10.5k
    *len += n;
943
10.5k
  } while (n == 4096);
944
3.42k
  str->close();
945
946
3.42k
  obj2.free();
947
3.42k
  obj1.free();
948
949
3.42k
  return buf;
950
3.42k
}
951
952
//------------------------------------------------------------------------
953
// Gfx8BitFont
954
//------------------------------------------------------------------------
955
956
Gfx8BitFont::Gfx8BitFont(XRef *xref, const char *tagA, Ref idA, GString *nameA,
957
       GfxFontType typeA, Ref embFontIDA, Dict *fontDict):
958
25.4k
  GfxFont(tagA, idA, nameA, typeA, embFontIDA)
959
25.4k
{
960
25.4k
  GString *name2;
961
25.4k
  BuiltinFont *builtinFont;
962
25.4k
  const char **baseEnc;
963
25.4k
  char *buf;
964
25.4k
  int len;
965
25.4k
  FoFiType1 *ffT1;
966
25.4k
  FoFiType1C *ffT1C;
967
25.4k
  int code, code2;
968
25.4k
  char *charName;
969
25.4k
  GBool missing, hex;
970
25.4k
  Unicode toUnicode[256];
971
25.4k
  CharCodeToUnicode *utu, *ctu2;
972
25.4k
  Unicode uBuf[8];
973
25.4k
  double mul;
974
25.4k
  int firstChar, lastChar;
975
25.4k
  Gushort w;
976
25.4k
  Object obj1, obj2, obj3;
977
25.4k
  int n, i, a, b, m;
978
979
25.4k
  ctu = NULL;
980
981
  // do font name substitution for various aliases of the Base 14 font
982
  // names
983
25.4k
  base14 = NULL;
984
25.4k
  if (name) {
985
17.4k
    name2 = name->copy();
986
17.4k
    i = 0;
987
241k
    while (i < name2->getLength()) {
988
224k
      if (name2->getChar(i) == ' ') {
989
681
  name2->del(i);
990
223k
      } else {
991
223k
  ++i;
992
223k
      }
993
224k
    }
994
17.4k
    a = 0;
995
17.4k
    b = sizeof(base14FontMap) / sizeof(Base14FontMapEntry);
996
    // invariant: base14FontMap[a].altName <= name2 < base14FontMap[b].altName
997
122k
    while (b - a > 1) {
998
105k
      m = (a + b) / 2;
999
105k
      if (name2->cmp(base14FontMap[m].altName) >= 0) {
1000
55.6k
  a = m;
1001
55.6k
      } else {
1002
49.4k
  b = m;
1003
49.4k
      }
1004
105k
    }
1005
17.4k
    if (!name2->cmp(base14FontMap[a].altName)) {
1006
10.2k
      base14 = &base14FontMap[a];
1007
10.2k
    }
1008
17.4k
    delete name2;
1009
17.4k
  }
1010
1011
  // is it a built-in font?
1012
25.4k
  builtinFont = NULL;
1013
25.4k
  if (base14) {
1014
47.4k
    for (i = 0; i < nBuiltinFonts; ++i) {
1015
47.4k
      if (!strcmp(base14->base14Name, builtinFonts[i].name)) {
1016
10.2k
  builtinFont = &builtinFonts[i];
1017
10.2k
  break;
1018
10.2k
      }
1019
47.4k
    }
1020
10.2k
  }
1021
1022
  // default ascent/descent values
1023
25.4k
  if (builtinFont) {
1024
10.2k
    missingWidth = builtinFont->missingWidth;
1025
10.2k
    ascent = 0.001 * builtinFont->ascent;
1026
10.2k
    descent = 0.001 * builtinFont->descent;
1027
10.2k
    declaredAscent = ascent;
1028
10.2k
    fontBBox[0] = 0.001 * builtinFont->bbox[0];
1029
10.2k
    fontBBox[1] = 0.001 * builtinFont->bbox[1];
1030
10.2k
    fontBBox[2] = 0.001 * builtinFont->bbox[2];
1031
10.2k
    fontBBox[3] = 0.001 * builtinFont->bbox[3];
1032
15.2k
  } else {
1033
15.2k
    missingWidth = 0;
1034
15.2k
    ascent = 0.75;
1035
15.2k
    descent = -0.25;
1036
15.2k
    declaredAscent = ascent;
1037
15.2k
    fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
1038
15.2k
  }
1039
1040
  // get info from font descriptor
1041
25.4k
  readFontDescriptor(xref, fontDict);
1042
1043
  // for Base-14 fonts (even if embedded), don't trust the
1044
  // ascent/descent/bbox values from the font descriptor
1045
25.4k
  if (builtinFont) {
1046
10.2k
    ascent = 0.001 * builtinFont->ascent;
1047
10.2k
    descent = 0.001 * builtinFont->descent;
1048
10.2k
    declaredAscent = ascent;
1049
10.2k
    fontBBox[0] = 0.001 * builtinFont->bbox[0];
1050
10.2k
    fontBBox[1] = 0.001 * builtinFont->bbox[1];
1051
10.2k
    fontBBox[2] = 0.001 * builtinFont->bbox[2];
1052
10.2k
    fontBBox[3] = 0.001 * builtinFont->bbox[3];
1053
10.2k
  }
1054
1055
  // get font matrix
1056
25.4k
  fontMat[0] = fontMat[3] = 1;
1057
25.4k
  fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
1058
25.4k
  if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
1059
13.8k
    for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
1060
11.5k
      if (obj1.arrayGet(i, &obj2)->isNum()) {
1061
10.8k
  fontMat[i] = obj2.getNum();
1062
10.8k
      }
1063
11.5k
      obj2.free();
1064
11.5k
    }
1065
2.23k
  }
1066
25.4k
  obj1.free();
1067
1068
  // get Type 3 bounding box, font definition, and resources
1069
25.4k
  if (type == fontType3) {
1070
4.21k
    if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
1071
14.8k
      for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
1072
10.7k
  if (obj1.arrayGet(i, &obj2)->isNum()) {
1073
10.2k
    fontBBox[i] = obj2.getNum();
1074
10.2k
  }
1075
10.7k
  obj2.free();
1076
10.7k
      }
1077
4.03k
    }
1078
4.21k
    obj1.free();
1079
4.21k
    if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
1080
89
      error(errSyntaxError, -1,
1081
89
      "Missing or invalid CharProcs dictionary in Type 3 font");
1082
89
      charProcs.free();
1083
89
    }
1084
4.21k
    if (!fontDict->lookup("Resources", &resources)->isDict()) {
1085
360
      resources.free();
1086
360
    }
1087
4.21k
  }
1088
1089
  //----- build the font encoding -----
1090
1091
  // Encodings start with a base encoding, which can come from
1092
  // (in order of priority):
1093
  //   1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
1094
  //        - MacRoman / MacExpert / WinAnsi / Standard
1095
  //   2. embedded or external font file
1096
  //   3. default:
1097
  //        - builtin --> builtin encoding
1098
  //        - TrueType --> WinAnsiEncoding
1099
  //        - others --> StandardEncoding
1100
  // and then add a list of differences (if any) from
1101
  // FontDict.Encoding.Differences.
1102
1103
  // check FontDict for base encoding
1104
25.4k
  hasEncoding = gFalse;
1105
25.4k
  usesMacRomanEnc = gFalse;
1106
25.4k
  baseEnc = NULL;
1107
25.4k
  baseEncFromFontFile = gFalse;
1108
25.4k
  fontDict->lookup("Encoding", &obj1);
1109
25.4k
  if (obj1.isDict()) {
1110
5.46k
    obj1.dictLookup("BaseEncoding", &obj2);
1111
5.46k
    if (obj2.isName("MacRomanEncoding")) {
1112
0
      hasEncoding = gTrue;
1113
0
      usesMacRomanEnc = gTrue;
1114
0
      baseEnc = macRomanEncoding;
1115
5.46k
    } else if (obj2.isName("MacExpertEncoding")) {
1116
0
      hasEncoding = gTrue;
1117
0
      baseEnc = macExpertEncoding;
1118
5.46k
    } else if (obj2.isName("WinAnsiEncoding")) {
1119
338
      hasEncoding = gTrue;
1120
338
      baseEnc = winAnsiEncoding;
1121
338
    }
1122
5.46k
    obj2.free();
1123
20.0k
  } else if (obj1.isName("MacRomanEncoding")) {
1124
936
    hasEncoding = gTrue;
1125
936
    usesMacRomanEnc = gTrue;
1126
936
    baseEnc = macRomanEncoding;
1127
19.0k
  } else if (obj1.isName("MacExpertEncoding")) {
1128
0
    hasEncoding = gTrue;
1129
0
    baseEnc = macExpertEncoding;
1130
19.0k
  } else if (obj1.isName("WinAnsiEncoding")) {
1131
1.57k
    hasEncoding = gTrue;
1132
1.57k
    baseEnc = winAnsiEncoding;
1133
1.57k
  }
1134
1135
  // a non-embedded Symbol or ZapfDingbats font should never use one
1136
  // of the non-symbol encodings -- I've encountered PDF files that
1137
  // set the Encoding to WinAnsiEncoding, which doesn't make any sense
1138
  // (and other viewers also seem to ignore it)
1139
25.4k
  if (builtinFont && embFontID.num < 0 &&
1140
9.39k
      (builtinFont->defaultBaseEnc == symbolEncoding ||
1141
9.38k
       builtinFont->defaultBaseEnc == zapfDingbatsEncoding) &&
1142
19
      !obj1.isDict()) {
1143
19
    baseEnc = NULL;
1144
19
  }
1145
1146
  // check embedded font file for base encoding
1147
  // (only for Type 1 fonts - trying to get an encoding out of a
1148
  // TrueType font is a losing proposition)
1149
25.4k
  ffT1 = NULL;
1150
25.4k
  ffT1C = NULL;
1151
25.4k
  buf = NULL;
1152
25.4k
  if (type == fontType1 && embFontID.num >= 0) {
1153
1.46k
    if ((buf = readEmbFontFile(xref, &len))) {
1154
1.46k
      if ((ffT1 = FoFiType1::make(buf, len))) {
1155
1.46k
  if (ffT1->getName()) {
1156
488
    if (embFontName) {
1157
441
      delete embFontName;
1158
441
    }
1159
488
    embFontName = new GString(ffT1->getName());
1160
488
  }
1161
1.46k
  if (!baseEnc) {
1162
1.29k
    baseEnc = (const char **)ffT1->getEncoding();
1163
1.29k
    baseEncFromFontFile = gTrue;
1164
1.29k
  }
1165
1.46k
      }
1166
1.46k
      gfree(buf);
1167
1.46k
    }
1168
24.0k
  } else if (type == fontType1C && embFontID.num >= 0) {
1169
1.96k
    if ((buf = readEmbFontFile(xref, &len))) {
1170
1.96k
      if ((ffT1C = FoFiType1C::make(buf, len))) {
1171
981
  if (ffT1C->getName()) {
1172
981
    if (embFontName) {
1173
639
      delete embFontName;
1174
639
    }
1175
981
    embFontName = new GString(ffT1C->getName());
1176
981
  }
1177
981
  if (!baseEnc) {
1178
470
    baseEnc = (const char **)ffT1C->getEncoding();
1179
470
    baseEncFromFontFile = gTrue;
1180
470
  }
1181
981
      }
1182
1.96k
      gfree(buf);
1183
1.96k
    }
1184
1.96k
  }
1185
1186
  // get default base encoding
1187
25.4k
  if (!baseEnc) {
1188
21.7k
    if (builtinFont && embFontID.num < 0) {
1189
8.53k
      baseEnc = builtinFont->defaultBaseEnc;
1190
8.53k
      hasEncoding = gTrue;
1191
13.1k
    } else if (type == fontTrueType) {
1192
2.15k
      baseEnc = winAnsiEncoding;
1193
11.0k
    } else {
1194
11.0k
      baseEnc = standardEncoding;
1195
11.0k
    }
1196
21.7k
  }
1197
1198
  // copy the base encoding
1199
6.54M
  for (i = 0; i < 256; ++i) {
1200
6.51M
    enc[i] = (char *)baseEnc[i];
1201
6.51M
    if ((encFree[i] = (char)baseEncFromFontFile) && enc[i]) {
1202
165k
      enc[i] = copyString(baseEnc[i]);
1203
165k
    }
1204
6.51M
  }
1205
1206
  // some Type 1C font files have empty encodings, which can break the
1207
  // T1C->T1 conversion (since the 'seac' operator depends on having
1208
  // the accents in the encoding), so we fill in any gaps from
1209
  // StandardEncoding
1210
25.4k
  if (type == fontType1C && embFontID.num >= 0 && baseEncFromFontFile) {
1211
120k
    for (i = 0; i < 256; ++i) {
1212
120k
      if (!enc[i] && standardEncoding[i]) {
1213
48.4k
  enc[i] = (char *)standardEncoding[i];
1214
48.4k
  encFree[i] = gFalse;
1215
48.4k
      }
1216
120k
    }
1217
470
  }
1218
1219
  // merge differences into encoding
1220
25.4k
  if (obj1.isDict()) {
1221
5.46k
    obj1.dictLookup("Differences", &obj2);
1222
5.46k
    if (obj2.isArray()) {
1223
4.14k
      hasEncoding = gTrue;
1224
4.14k
      code = 0;
1225
187k
      for (i = 0; i < obj2.arrayGetLength(); ++i) {
1226
183k
  obj2.arrayGet(i, &obj3);
1227
183k
  if (obj3.isInt()) {
1228
28.2k
    code = obj3.getInt();
1229
155k
  } else if (obj3.isName()) {
1230
106k
    if (code >= 0 && code < 256) {
1231
106k
      if (encFree[code]) {
1232
19.1k
        gfree(enc[code]);
1233
19.1k
      }
1234
106k
      enc[code] = copyString(obj3.getName());
1235
106k
      encFree[code] = gTrue;
1236
106k
    }
1237
106k
    ++code;
1238
106k
  } else {
1239
48.5k
    error(errSyntaxError, -1,
1240
48.5k
    "Wrong type in font encoding resource differences ({0:s})",
1241
48.5k
    obj3.getTypeName());
1242
48.5k
  }
1243
183k
  obj3.free();
1244
183k
      }
1245
4.14k
    }
1246
5.46k
    obj2.free();
1247
5.46k
  }
1248
25.4k
  obj1.free();
1249
25.4k
  if (ffT1) {
1250
1.46k
    delete ffT1;
1251
1.46k
  }
1252
25.4k
  if (ffT1C) {
1253
981
    delete ffT1C;
1254
981
  }
1255
1256
  //----- build the mapping to Unicode -----
1257
1258
  // pass 1: use the name-to-Unicode mapping table
1259
25.4k
  missing = hex = gFalse;
1260
6.54M
  for (code = 0; code < 256; ++code) {
1261
6.51M
    if ((charName = enc[code])) {
1262
4.14M
      if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
1263
43.5k
    strcmp(charName, ".notdef")) {
1264
  // if it wasn't in the name-to-Unicode table, check for a
1265
  // name that looks like 'Axx' or 'xx', where 'A' is any letter
1266
  // and 'xx' is two hex digits
1267
35.3k
  if ((strlen(charName) == 3 &&
1268
12.2k
       isalpha(charName[0] & 0xff) &&
1269
12.1k
       isxdigit(charName[1] & 0xff) && isxdigit(charName[2] & 0xff) &&
1270
10.7k
       ((charName[1] >= 'a' && charName[1] <= 'f') ||
1271
10.6k
        (charName[1] >= 'A' && charName[1] <= 'F') ||
1272
10.6k
        (charName[2] >= 'a' && charName[2] <= 'f') ||
1273
10.1k
        (charName[2] >= 'A' && charName[2] <= 'F'))) ||
1274
34.6k
      (strlen(charName) == 2 &&
1275
3.45k
       isxdigit(charName[0] & 0xff) && isxdigit(charName[1] & 0xff) &&
1276
1.35k
       ((charName[0] >= 'a' && charName[0] <= 'f') ||
1277
1.17k
        (charName[0] >= 'A' && charName[0] <= 'F') ||
1278
199
        (charName[1] >= 'a' && charName[1] <= 'f') ||
1279
1.93k
        (charName[1] >= 'A' && charName[1] <= 'F')))) {
1280
1.93k
    hex = gTrue;
1281
1.93k
  }
1282
35.3k
  missing = gTrue;
1283
35.3k
      }
1284
4.14M
    } else {
1285
2.36M
      toUnicode[code] = 0;
1286
2.36M
    }
1287
6.51M
  }
1288
1289
  // pass 2: try to fill in the missing chars, looking for names of
1290
  // any of the following forms:
1291
  // - 'xx'
1292
  // - 'Axx'
1293
  // - 'nn'
1294
  // - 'Ann'
1295
  // - 'ABnn'
1296
  // - 'unixxxx' (possibly followed by garbage - some Arabic files
1297
  //             use 'uni0628.medi', etc.)
1298
  // where 'A' and 'B' are any letters, 'xx' is two hex digits, 'xxxx'
1299
  // is four hex digits, and 'nn' is 2-4 decimal digits
1300
25.4k
  usedNumericHeuristic = gFalse;
1301
25.4k
  if (missing && globalParams->getMapNumericCharNames()) {
1302
919k
    for (code = 0; code < 256; ++code) {
1303
916k
      if ((charName = enc[code]) && !toUnicode[code] &&
1304
38.6k
    strcmp(charName, ".notdef")) {
1305
35.3k
  n = (int)strlen(charName);
1306
35.3k
  code2 = -1;
1307
35.3k
  if (hex && n == 3 && isalpha(charName[0] & 0xff) &&
1308
5.81k
      isxdigit(charName[1] & 0xff) && isxdigit(charName[2] & 0xff)) {
1309
5.02k
    sscanf(charName+1, "%x", &code2);
1310
30.3k
  } else if (hex && n == 2 &&
1311
1.97k
       isxdigit(charName[0] & 0xff) &&
1312
1.76k
       isxdigit(charName[1] & 0xff)) {
1313
1.31k
    sscanf(charName, "%x", &code2);
1314
28.9k
  } else if (!hex && n >= 2 && n <= 4 &&
1315
18.6k
       isdigit(charName[0] & 0xff) && isdigit(charName[1] & 0xff)) {
1316
47
    code2 = atoi(charName);
1317
28.9k
  } else if (n >= 3 && n <= 5 &&
1318
20.2k
       isdigit(charName[1] & 0xff) && isdigit(charName[2] & 0xff)) {
1319
15.5k
    code2 = atoi(charName+1);
1320
15.5k
  } else if (n >= 4 && n <= 6 &&
1321
3.77k
       isdigit(charName[2] & 0xff) && isdigit(charName[3] & 0xff)) {
1322
69
    code2 = atoi(charName+2);
1323
13.3k
  } else if (n >= 7 && charName[0] == 'u' && charName[1] == 'n' &&
1324
53
       charName[2] == 'i' &&
1325
42
       isxdigit(charName[3] & 0xff) &&
1326
34
       isxdigit(charName[4] & 0xff) &&
1327
33
       isxdigit(charName[5] & 0xff) &&
1328
32
       isxdigit(charName[6] & 0xff)) {
1329
31
    sscanf(charName + 3, "%x", &code2);
1330
31
  }
1331
35.3k
  if (code2 >= 0 && code2 <= 0xffff) {
1332
22.0k
    toUnicode[code] = (Unicode)code2;
1333
22.0k
    usedNumericHeuristic = gTrue;
1334
22.0k
  }
1335
35.3k
      }
1336
916k
    }
1337
1338
  // if the 'mapUnknownCharNames' flag is set, do a simple pass-through
1339
  // mapping for unknown character names
1340
21.8k
  } else if (missing && globalParams->getMapUnknownCharNames()) {
1341
0
    for (code = 0; code < 256; ++code) {
1342
0
      if (!toUnicode[code]) {
1343
0
  toUnicode[code] = code;
1344
0
      }
1345
0
    }
1346
0
  }
1347
1348
  // construct the char code -> Unicode mapping object
1349
25.4k
  ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
1350
1351
  // merge in a ToUnicode CMap, if there is one -- this overwrites
1352
  // existing entries in ctu, i.e., the ToUnicode CMap takes
1353
  // precedence, but the other encoding info is allowed to fill in any
1354
  // holes
1355
25.4k
  readToUnicodeCMap(fontDict, 8, ctu);
1356
1357
  // look for a Unicode-to-Unicode mapping
1358
25.4k
  if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
1359
0
    for (i = 0; i < 256; ++i) {
1360
0
      toUnicode[i] = 0;
1361
0
    }
1362
0
    ctu2 = CharCodeToUnicode::make8BitToUnicode(toUnicode);
1363
0
    for (i = 0; i < 256; ++i) {
1364
0
      n = ctu->mapToUnicode((CharCode)i, uBuf, 8);
1365
0
      if (n >= 1) {
1366
0
  n = utu->mapToUnicode((CharCode)uBuf[0], uBuf, 8);
1367
0
  if (n >= 1) {
1368
0
    ctu2->setMapping((CharCode)i, uBuf, n);
1369
0
  }
1370
0
      }
1371
0
    }
1372
0
    utu->decRefCnt();
1373
0
    delete ctu;
1374
0
    ctu = ctu2;
1375
0
  }
1376
1377
  //----- get the character widths -----
1378
1379
  // initialize all widths
1380
6.54M
  for (code = 0; code < 256; ++code) {
1381
6.51M
    widths[code] = missingWidth * 0.001;
1382
6.51M
  }
1383
1384
  // use widths from font dict, if present
1385
25.4k
  fontDict->lookup("FirstChar", &obj1);
1386
25.4k
  firstChar = obj1.isInt() ? obj1.getInt() : 0;
1387
25.4k
  obj1.free();
1388
25.4k
  if (firstChar < 0 || firstChar > 255) {
1389
42
    firstChar = 0;
1390
42
  }
1391
25.4k
  fontDict->lookup("LastChar", &obj1);
1392
25.4k
  lastChar = obj1.isInt() ? obj1.getInt() : 255;
1393
25.4k
  obj1.free();
1394
25.4k
  if (lastChar < 0 || lastChar > 255) {
1395
0
    lastChar = 255;
1396
0
  }
1397
25.4k
  mul = (type == fontType3) ? fontMat[0] : 0.001;
1398
25.4k
  fontDict->lookup("Widths", &obj1);
1399
25.4k
  if (obj1.isArray()) {
1400
8.82k
    flags |= fontFixedWidth;
1401
8.82k
    if (obj1.arrayGetLength() < lastChar - firstChar + 1) {
1402
1.49k
      lastChar = firstChar + obj1.arrayGetLength() - 1;
1403
1.49k
    }
1404
359k
    for (code = firstChar; code <= lastChar; ++code) {
1405
350k
      obj1.arrayGet(code - firstChar, &obj2);
1406
350k
      if (obj2.isNum()) {
1407
339k
  widths[code] = obj2.getNum() * mul;
1408
339k
  if (fabs(widths[code] - widths[firstChar]) > 0.00001) {
1409
303k
    flags &= ~fontFixedWidth;
1410
303k
  }
1411
339k
      }
1412
350k
      obj2.free();
1413
350k
    }
1414
1415
  // use widths from built-in font
1416
16.6k
  } else if (builtinFont) {
1417
    // this is a kludge for broken PDF files that encode char 32
1418
    // as .notdef
1419
10.2k
    if (builtinFont->widths->getWidth("space", &w)) {
1420
10.2k
      widths[32] = 0.001 * w;
1421
10.2k
    }
1422
2.62M
    for (code = 0; code < 256; ++code) {
1423
2.61M
      if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
1424
1.62M
  widths[code] = 0.001 * w;
1425
1.62M
      }
1426
2.61M
    }
1427
1428
  // couldn't find widths -- use defaults 
1429
10.2k
  } else {
1430
    // this is technically an error -- the Widths entry is required
1431
    // for all but the Base-14 fonts -- but certain PDF generators
1432
    // apparently don't include widths for Arial and TimesNewRoman
1433
6.42k
    if (isFixedWidth()) {
1434
251
      i = 0;
1435
6.17k
    } else if (isSerif()) {
1436
5.69k
      i = 8;
1437
5.69k
    } else {
1438
477
      i = 4;
1439
477
    }
1440
6.42k
    if (isBold()) {
1441
804
      i += 2;
1442
804
    }
1443
6.42k
    if (isItalic()) {
1444
37
      i += 1;
1445
37
    }
1446
6.42k
    builtinFont = builtinFontSubst[i];
1447
    // this is a kludge for broken PDF files that encode char 32
1448
    // as .notdef
1449
6.42k
    if (builtinFont->widths->getWidth("space", &w)) {
1450
6.42k
      widths[32] = 0.001 * w;
1451
6.42k
    }
1452
1.65M
    for (code = 0; code < 256; ++code) {
1453
1.64M
      if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
1454
992k
  widths[code] = 0.001 * w;
1455
992k
      }
1456
1.64M
    }
1457
6.42k
  }
1458
25.4k
  obj1.free();
1459
1460
25.4k
  ok = gTrue;
1461
25.4k
}
1462
1463
25.4k
Gfx8BitFont::~Gfx8BitFont() {
1464
25.4k
  int i;
1465
1466
6.54M
  for (i = 0; i < 256; ++i) {
1467
6.51M
    if (encFree[i] && enc[i]) {
1468
258k
      gfree(enc[i]);
1469
258k
    }
1470
6.51M
  }
1471
25.4k
  ctu->decRefCnt();
1472
25.4k
  if (charProcs.isDict()) {
1473
4.12k
    charProcs.free();
1474
4.12k
  }
1475
25.4k
  if (resources.isDict()) {
1476
3.85k
    resources.free();
1477
3.85k
  }
1478
25.4k
}
1479
1480
int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
1481
           Unicode *u, int uSize, int *uLen,
1482
5.01M
           double *dx, double *dy, double *ox, double *oy) {
1483
5.01M
  CharCode c;
1484
1485
5.01M
  *code = c = (CharCode)(*s & 0xff);
1486
5.01M
  *uLen = ctu->mapToUnicode(c, u, uSize);
1487
5.01M
  *dx = widths[c];
1488
5.01M
  *dy = *ox = *oy = 0;
1489
5.01M
  return 1;
1490
5.01M
}
1491
1492
0
CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
1493
0
  ctu->incRefCnt();
1494
0
  return ctu;
1495
0
}
1496
1497
1.44k
int *Gfx8BitFont::getCodeToGIDMap(FoFiTrueType *ff) {
1498
1.44k
  int *map;
1499
1.44k
  int cmapPlatform, cmapEncoding;
1500
1.44k
  int unicodeCmap, macRomanCmap, macUnicodeCmap, msSymbolCmap, cmap;
1501
1.44k
  GBool nonsymbolic, useMacRoman, useUnicode;
1502
1.44k
  char *charName;
1503
1.44k
  Unicode u;
1504
1.44k
  int code, i, n;
1505
1506
1.44k
  map = (int *)gmallocn(256, sizeof(int));
1507
370k
  for (i = 0; i < 256; ++i) {
1508
369k
    map[i] = 0;
1509
369k
  }
1510
1511
  // This is based on the cmap/encoding selection algorithm in the PDF
1512
  // 2.0 spec, but with some differences to match up with Adobe's
1513
  // behavior.
1514
1.44k
  unicodeCmap = macRomanCmap = macUnicodeCmap = msSymbolCmap = -1;
1515
4.47k
  for (i = 0; i < ff->getNumCmaps(); ++i) {
1516
3.03k
    cmapPlatform = ff->getCmapPlatform(i);
1517
3.03k
    cmapEncoding = ff->getCmapEncoding(i);
1518
3.03k
    if (cmapPlatform == 3 && cmapEncoding == 1) {
1519
1
      unicodeCmap = i;
1520
3.03k
    } else if (cmapPlatform == 1 && cmapEncoding == 0) {
1521
724
      macRomanCmap = i;
1522
2.31k
    } else if (cmapPlatform == 0 && cmapEncoding <= 4) {
1523
1.64k
      macUnicodeCmap = i;
1524
1.64k
    } else if (cmapPlatform == 3 && cmapEncoding == 0) {
1525
22
      msSymbolCmap = i;
1526
22
    }
1527
3.03k
  }
1528
1.44k
  useMacRoman = gFalse;
1529
1.44k
  useUnicode = gFalse;
1530
1.44k
  nonsymbolic = !(flags & fontSymbolic);
1531
1.44k
  if (embFontID.num < 0 && hasEncoding && unicodeCmap >= 0) { 
1532
0
    cmap = unicodeCmap;
1533
0
    useUnicode = gTrue;
1534
1.44k
  } else if (!nonsymbolic && msSymbolCmap >= 0) {
1535
16
    cmap = msSymbolCmap;
1536
1.42k
  } else if (unicodeCmap >= 0) {
1537
1
    cmap = unicodeCmap;
1538
1
    useUnicode = nonsymbolic;
1539
1.42k
  } else if (usesMacRomanEnc && macRomanCmap >= 0) {
1540
    // MacRoman cmap has higher precedence than Mac Unicode only if
1541
    // the font uses the MacRoman encoding
1542
0
    cmap = macRomanCmap;
1543
0
    useMacRoman = gTrue;
1544
1.42k
  } else if (macUnicodeCmap >= 0) {
1545
29
    cmap = macUnicodeCmap;
1546
29
    useUnicode = nonsymbolic;
1547
1.39k
  } else if (macRomanCmap >= 0) {
1548
722
    cmap = macRomanCmap;
1549
722
    useMacRoman = nonsymbolic;
1550
722
  } else {
1551
674
    cmap = 0;
1552
674
  }
1553
1554
  // reverse map the char names through MacRomanEncoding, then map the
1555
  // char codes through the cmap; fall back on Unicode if that doesn't
1556
  // work
1557
1.44k
  if (useMacRoman) {
1558
58.8k
    for (i = 0; i < 256; ++i) {
1559
58.6k
      if ((charName = enc[i])) {
1560
51.2k
  if ((code = globalParams->getMacRomanCharCode(charName))) {
1561
46.9k
    map[i] = ff->mapCodeToGID(cmap, code);
1562
46.9k
  } else if (unicodeCmap >= 0 &&
1563
0
       (u = globalParams->mapNameToUnicode(charName))) {
1564
0
    map[i] = ff->mapCodeToGID(unicodeCmap, u);
1565
0
  }
1566
51.2k
      } else if (unicodeCmap >= 0 &&
1567
0
     (n = ctu->mapToUnicode((CharCode)i, &u, 1))) {
1568
0
  map[i] = ff->mapCodeToGID(cmap, u);
1569
7.32k
      } else {
1570
7.32k
  map[i] = -1;
1571
7.32k
      }
1572
58.6k
    }
1573
1574
  // map Unicode through the cmap
1575
1.21k
  } else if (useUnicode) {
1576
5.14k
    for (i = 0; i < 256; ++i) {
1577
5.12k
      if (((charName = enc[i]) &&
1578
4.48k
     (u = globalParams->mapNameToUnicode(charName))) ||
1579
4.49k
    (n = ctu->mapToUnicode((CharCode)i, &u, 1))) {
1580
4.49k
  map[i] = ff->mapCodeToGID(cmap, u);
1581
4.49k
      } else {
1582
621
  map[i] = -1;
1583
621
      }
1584
5.12k
    }
1585
1586
  // map the char codes through the cmap, possibly with an offset of
1587
  // 0xf000
1588
1.19k
  } else {
1589
306k
    for (i = 0; i < 256; ++i) {
1590
305k
      if (!(map[i] = ff->mapCodeToGID(cmap, i))) {
1591
243k
  map[i] = ff->mapCodeToGID(cmap, 0xf000 + i);
1592
243k
      }
1593
305k
    }
1594
1.19k
  }
1595
1596
  // try the TrueType 'post' table to handle any unmapped characters
1597
370k
  for (i = 0; i < 256; ++i) {
1598
369k
    if (map[i] <= 0 && (charName = enc[i])) {
1599
260k
      map[i] = ff->mapNameToGID(charName);
1600
260k
    }
1601
369k
  }
1602
1603
1.44k
  return map;
1604
1.44k
}
1605
1606
1.14k
int *Gfx8BitFont::getCodeToGIDMap(FoFiType1C *ff) {
1607
1.14k
  int *map;
1608
1.14k
  GHash *nameToGID;
1609
1.14k
  int i, gid;
1610
1611
1.14k
  map = (int *)gmallocn(256, sizeof(int));
1612
295k
  for (i = 0; i < 256; ++i) {
1613
293k
    map[i] = 0;
1614
293k
  }
1615
1616
1.14k
  nameToGID = ff->getNameToGIDMap();
1617
295k
  for (i = 0; i < 256; ++i) {
1618
293k
    if (!enc[i]) {
1619
83.4k
      continue;
1620
83.4k
    }
1621
210k
    gid = nameToGID->lookupInt(enc[i]);
1622
210k
    if (gid < 0 || gid >= 65536) {
1623
0
      continue;
1624
0
    }
1625
210k
    map[i] = gid;
1626
210k
  }
1627
1628
1.14k
  delete nameToGID;
1629
1630
1.14k
  return map;
1631
1.14k
}
1632
1633
0
Dict *Gfx8BitFont::getCharProcs() {
1634
0
  return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
1635
0
}
1636
1637
0
Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
1638
0
  if (enc[code] && charProcs.isDict()) {
1639
0
    charProcs.dictLookup(enc[code], proc);
1640
0
  } else {
1641
0
    proc->initNull();
1642
0
  }
1643
0
  return proc;
1644
0
}
1645
1646
1.79M
Object *Gfx8BitFont::getCharProcNF(int code, Object *proc) {
1647
1.79M
  if (enc[code] && charProcs.isDict()) {
1648
979k
    charProcs.dictLookupNF(enc[code], proc);
1649
979k
  } else {
1650
819k
    proc->initNull();
1651
819k
  }
1652
1.79M
  return proc;
1653
1.79M
}
1654
1655
1.79M
Dict *Gfx8BitFont::getResources() {
1656
1.79M
  return resources.isDict() ? resources.getDict() : (Dict *)NULL;
1657
1.79M
}
1658
1659
0
GBool Gfx8BitFont::problematicForUnicode() {
1660
0
  GString *nameLC;
1661
0
  GBool symbolic;
1662
1663
  // potential inputs:
1664
  // - font is embedded (GfxFont.embFontID.num >= 0)
1665
  // - font name (GfxFont.name)
1666
  // - font type (GfxFont.type)
1667
  // - Base-14 font (Gfx8BitFont.base14 != NULL)
1668
  // - symbolic (GfxFont.flags & fontSymbolic)
1669
  // - has Encoding array (Gfx8BitFont.hasEncoding)
1670
  // - extracted base encoding from embedded font file
1671
  //   (Gfx8BitFont.baseEncFromFontFile)
1672
  // - has a ToUnicode map (GfxFont.hasToUnicode)
1673
  // - used the numeric glyph name heuristic
1674
  //   (Gfx8BitFont.usedNumericHeuristic)
1675
1676
0
  if (name) {
1677
0
    nameLC = name->copy();
1678
0
    nameLC->lowerCase();
1679
0
    symbolic = strstr(nameLC->getCString(), "dingbat") ||
1680
0
               strstr(nameLC->getCString(), "wingding") ||
1681
0
               strstr(nameLC->getCString(), "commpi");
1682
0
    delete nameLC;
1683
0
    if (symbolic) {
1684
0
      return gFalse;
1685
0
    }
1686
0
  }
1687
1688
0
  if (embFontID.num >= 0) {
1689
0
    switch (type) {
1690
0
    case fontType1:
1691
0
    case fontType1C:
1692
0
    case fontType1COT:
1693
0
      return !hasToUnicode && (!hasEncoding || usedNumericHeuristic);
1694
1695
0
    case fontType3:
1696
      // this never happens -- Type 3 fonts don't set embFontID
1697
0
      return !hasToUnicode && (!hasEncoding || usedNumericHeuristic);
1698
1699
0
    case fontTrueType:
1700
0
    case fontTrueTypeOT:
1701
0
      return !hasToUnicode && !hasEncoding;
1702
1703
0
    default:
1704
0
      return !hasToUnicode;
1705
0
    }
1706
1707
0
  } else if (type == fontType3) {
1708
0
    return !hasToUnicode && (!hasEncoding || usedNumericHeuristic);
1709
1710
0
  } else {
1711
    // NB: type will be fontTypeUnknown if the PDF specifies an
1712
    // invalid font type -- which is ok, if we have a ToUnicode map or
1713
    // an encoding
1714
0
    return !hasToUnicode && !hasEncoding;
1715
0
  }
1716
0
}
1717
1718
0
GBool Gfx8BitFont::parensAreSwapped(XRef *xref) {
1719
0
  return gFalse;
1720
0
}
1721
1722
//------------------------------------------------------------------------
1723
// GfxCIDFont
1724
//------------------------------------------------------------------------
1725
1726
GfxCIDFont::GfxCIDFont(XRef *xref, const char *tagA, Ref idA, GString *nameA,
1727
           GfxFontType typeA, Ref embFontIDA, Dict *fontDict):
1728
4.35k
  GfxFont(tagA, idA, nameA, typeA, embFontIDA)
1729
4.35k
{
1730
4.35k
  Dict *desFontDict;
1731
4.35k
  Object desFontDictObj;
1732
4.35k
  Object obj1, obj2, obj3, obj4, obj5, obj6;
1733
4.35k
  CharCodeToUnicode *utu;
1734
4.35k
  CharCode c;
1735
4.35k
  Unicode uBuf[8];
1736
4.35k
  int c1, c2;
1737
4.35k
  int excepsSize, i, j, k, n;
1738
1739
4.35k
  missingWidth = 0;
1740
4.35k
  ascent = 0.95;
1741
4.35k
  descent = -0.35;
1742
4.35k
  declaredAscent = ascent;
1743
4.35k
  fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
1744
4.35k
  collection = NULL;
1745
4.35k
  cMap = NULL;
1746
4.35k
  ctu = NULL;
1747
4.35k
  ctuUsesCharCode = gTrue;
1748
4.35k
  widths.defWidth = 1.0;
1749
4.35k
  widths.defHeight = -1.0;
1750
4.35k
  widths.defVY = 0.880;
1751
4.35k
  widths.exceps = NULL;
1752
4.35k
  widths.nExceps = 0;
1753
4.35k
  widths.excepsV = NULL;
1754
4.35k
  widths.nExcepsV = 0;
1755
4.35k
  cidToGID = NULL;
1756
4.35k
  cidToGIDLen = 0;
1757
1758
  // get the descendant font
1759
4.35k
  if (!fontDict->lookup("DescendantFonts", &obj1)->isArray() ||
1760
4.32k
      obj1.arrayGetLength() == 0) {
1761
28
    error(errSyntaxError, -1,
1762
28
    "Missing or empty DescendantFonts entry in Type 0 font");
1763
28
    obj1.free();
1764
28
    goto err1;
1765
28
  }
1766
4.32k
  if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
1767
33
    error(errSyntaxError, -1, "Bad descendant font in Type 0 font");
1768
33
    goto err2;
1769
33
  }
1770
4.29k
  obj1.free();
1771
4.29k
  desFontDict = desFontDictObj.getDict();
1772
1773
  // get info from font descriptor
1774
4.29k
  readFontDescriptor(xref, desFontDict);
1775
1776
  //----- encoding info -----
1777
1778
  // char collection
1779
4.29k
  if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
1780
185
    error(errSyntaxError, -1,
1781
185
    "Missing CIDSystemInfo dictionary in Type 0 descendant font");
1782
185
    goto err2;
1783
185
  }
1784
4.10k
  obj1.dictLookup("Registry", &obj2);
1785
4.10k
  obj1.dictLookup("Ordering", &obj3);
1786
4.10k
  if (!obj2.isString() || !obj3.isString()) {
1787
65
    error(errSyntaxError, -1,
1788
65
    "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
1789
65
    goto err3;
1790
65
  }
1791
4.04k
  collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
1792
4.04k
  obj3.free();
1793
4.04k
  obj2.free();
1794
4.04k
  obj1.free();
1795
1796
  // encoding (i.e., CMap)
1797
4.04k
  if (fontDict->lookup("Encoding", &obj1)->isNull()) {
1798
21
    error(errSyntaxError, -1, "Missing Encoding entry in Type 0 font");
1799
21
    goto err2;
1800
21
  }
1801
4.02k
  if (!(cMap = CMap::parse(NULL, collection, &obj1))) {
1802
1.56k
    goto err2;
1803
1.56k
  }
1804
1805
  // check for fonts that use the Identity-H encoding (cmap), and the
1806
  // Adobe-Identity character collection
1807
2.45k
  identityEnc = obj1.isName("Identity-H") &&
1808
2.08k
                !collection->cmp("Adobe-Identity");
1809
1810
2.45k
  obj1.free();
1811
1812
  // CIDToGIDMap
1813
  // (the PDF 1.7 spec only allows these for TrueType fonts, but
1814
  // Acrobat apparently also allows them for OpenType CFF fonts -- and
1815
  // the PDF 2.0 spec has removed the prohibition)
1816
2.45k
  hasIdentityCIDToGID = gFalse;
1817
2.45k
  desFontDict->lookup("CIDToGIDMap", &obj1);
1818
2.45k
  if (obj1.isStream()) {
1819
41
    cidToGIDLen = 0;
1820
41
    i = 64;
1821
41
    cidToGID = (int *)gmallocn(i, sizeof(int));
1822
41
    obj1.streamReset();
1823
272k
    while ((c1 = obj1.streamGetChar()) != EOF &&
1824
272k
     (c2 = obj1.streamGetChar()) != EOF) {
1825
271k
      if (cidToGIDLen == i) {
1826
124
  i *= 2;
1827
124
  cidToGID = (int *)greallocn(cidToGID, i, sizeof(int));
1828
124
      }
1829
271k
      cidToGID[cidToGIDLen++] = (c1 << 8) + c2;
1830
271k
    }
1831
41
    obj1.streamClose();
1832
41
    identityEnc = gFalse;
1833
2.41k
  } else if (obj1.isName("Identity")) {
1834
1.30k
    hasIdentityCIDToGID = gTrue;
1835
1.30k
  } else if (!obj1.isNull()) {
1836
526
    error(errSyntaxError, -1, "Invalid CIDToGIDMap entry in CID font");
1837
526
  }
1838
2.45k
  obj1.free();
1839
1840
  // look for a ToUnicode CMap
1841
2.45k
  hasKnownCollection = gFalse;
1842
2.45k
  if (globalParams->getUseTrueTypeUnicodeMapping()) {
1843
0
    readTrueTypeUnicodeMapping(xref);
1844
0
  }
1845
2.45k
  if (!ctu) {
1846
2.45k
    ctu = readToUnicodeCMap(fontDict, 16, NULL);
1847
2.45k
  }
1848
2.45k
  if (!ctu) {
1849
1.76k
    ctuUsesCharCode = gFalse;
1850
1851
    // use an identity mapping for the "Adobe-Identity" and
1852
    // "Adobe-UCS" collections
1853
1.76k
    if (!collection->cmp("Adobe-Identity") ||
1854
992
  !collection->cmp("Adobe-UCS")) {
1855
992
      ctu = CharCodeToUnicode::makeIdentityMapping();
1856
1857
    // look for a user-supplied .cidToUnicode file
1858
992
    } else if ((ctu = globalParams->getCIDToUnicode(collection))) {
1859
0
      hasKnownCollection = gTrue;
1860
1861
772
    } else {
1862
772
      error(errSyntaxError, -1,
1863
772
      "Unknown character collection '{0:t}'", collection);
1864
1865
      // fall back to an identity mapping
1866
772
      ctu = CharCodeToUnicode::makeIdentityMapping();
1867
772
    }
1868
1.76k
  }
1869
1870
  // look for a Unicode-to-Unicode mapping
1871
2.45k
  if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
1872
0
    if (ctu) {
1873
0
      if (ctu->isIdentity()) {
1874
0
  ctu->decRefCnt();
1875
0
  ctu = utu;
1876
0
      } else {
1877
0
  for (c = 0; c < ctu->getLength(); ++c) {
1878
0
    n = ctu->mapToUnicode(c, uBuf, 8);
1879
0
    if (n >= 1) {
1880
0
      n = utu->mapToUnicode((CharCode)uBuf[0], uBuf, 8);
1881
0
      if (n >= 1) {
1882
0
        ctu->setMapping(c, uBuf, n);
1883
0
      }
1884
0
    }
1885
0
  }
1886
0
  utu->decRefCnt();
1887
0
      }
1888
0
    } else {
1889
0
      ctu = utu;
1890
0
    }
1891
0
  }
1892
1893
  //----- character metrics -----
1894
1895
  // default char width
1896
2.45k
  if (desFontDict->lookup("DW", &obj1)->isNum()) {
1897
762
    widths.defWidth = obj1.getNum() * 0.001;
1898
762
  }
1899
2.45k
  obj1.free();
1900
1901
  // char width exceptions
1902
2.45k
  if (desFontDict->lookup("W", &obj1)->isArray()) {
1903
1.92k
    excepsSize = 0;
1904
1.92k
    i = 0;
1905
9.50k
    while (i + 1 < obj1.arrayGetLength()) {
1906
7.57k
      obj1.arrayGet(i, &obj2);
1907
7.57k
      obj1.arrayGet(i + 1, &obj3);
1908
7.57k
      if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
1909
632
  if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
1910
401
    if (widths.nExceps == excepsSize) {
1911
17
      excepsSize += 16;
1912
17
      widths.exceps = (GfxFontCIDWidthExcep *)
1913
17
        greallocn(widths.exceps,
1914
17
      excepsSize, sizeof(GfxFontCIDWidthExcep));
1915
17
    }
1916
401
    widths.exceps[widths.nExceps].first = obj2.getInt();
1917
401
    widths.exceps[widths.nExceps].last = obj3.getInt();
1918
401
    widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1919
401
    ++widths.nExceps;
1920
401
  } else {
1921
231
    error(errSyntaxError, -1, "Bad widths array in Type 0 font");
1922
231
  }
1923
632
  obj4.free();
1924
632
  i += 3;
1925
6.94k
      } else if (obj2.isInt() && obj3.isArray()) {
1926
5.78k
  if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1927
2.06k
    excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1928
2.06k
    widths.exceps = (GfxFontCIDWidthExcep *)
1929
2.06k
      greallocn(widths.exceps,
1930
2.06k
          excepsSize, sizeof(GfxFontCIDWidthExcep));
1931
2.06k
  }
1932
5.78k
  j = obj2.getInt();
1933
24.1k
  for (k = 0; k < obj3.arrayGetLength(); ++k) {
1934
18.3k
    if (obj3.arrayGet(k, &obj4)->isNum()) {
1935
17.5k
      widths.exceps[widths.nExceps].first = j;
1936
17.5k
      widths.exceps[widths.nExceps].last = j;
1937
17.5k
      widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1938
17.5k
      ++j;
1939
17.5k
      ++widths.nExceps;
1940
17.5k
    } else {
1941
803
      error(errSyntaxError, -1, "Bad widths array in Type 0 font");
1942
803
    }
1943
18.3k
    obj4.free();
1944
18.3k
  }
1945
5.78k
  i += 2;
1946
5.78k
      } else {
1947
1.16k
  error(errSyntaxError, -1, "Bad widths array in Type 0 font");
1948
1.16k
  ++i;
1949
1.16k
      }
1950
7.57k
      obj3.free();
1951
7.57k
      obj2.free();
1952
7.57k
    }
1953
1.92k
  }
1954
2.45k
  obj1.free();
1955
1956
  // default metrics for vertical font
1957
2.45k
  if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1958
11
      obj1.arrayGetLength() == 2) {
1959
10
    if (obj1.arrayGet(0, &obj2)->isNum()) {
1960
9
      widths.defVY = obj2.getNum() * 0.001;
1961
9
    }
1962
10
    obj2.free();
1963
10
    if (obj1.arrayGet(1, &obj2)->isNum()) {
1964
1
      widths.defHeight = obj2.getNum() * 0.001;
1965
1
    }
1966
10
    obj2.free();
1967
10
  }
1968
2.45k
  obj1.free();
1969
1970
  // char metric exceptions for vertical font
1971
2.45k
  if (desFontDict->lookup("W2", &obj1)->isArray()) {
1972
37
    excepsSize = 0;
1973
37
    i = 0;
1974
238
    while (i + 1 < obj1.arrayGetLength()) {
1975
201
      obj1.arrayGet(i, &obj2);
1976
201
      obj1.arrayGet(i+ 1, &obj3);
1977
201
      if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1978
39
  if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1979
34
      obj1.arrayGet(i + 3, &obj5)->isNum() &&
1980
28
      obj1.arrayGet(i + 4, &obj6)->isNum()) {
1981
18
    if (widths.nExcepsV == excepsSize) {
1982
12
      excepsSize += 16;
1983
12
      widths.excepsV = (GfxFontCIDWidthExcepV *)
1984
12
        greallocn(widths.excepsV,
1985
12
      excepsSize, sizeof(GfxFontCIDWidthExcepV));
1986
12
    }
1987
18
    widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1988
18
    widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1989
18
    widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1990
18
    widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1991
18
    widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1992
18
    ++widths.nExcepsV;
1993
21
  } else {
1994
21
    error(errSyntaxError, -1, "Bad widths (W2) array in Type 0 font");
1995
21
  }
1996
39
  obj6.free();
1997
39
  obj5.free();
1998
39
  obj4.free();
1999
39
  i += 5;
2000
162
      } else if (obj2.isInt() && obj3.isArray()) {
2001
23
  if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
2002
18
    excepsSize =
2003
18
      (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
2004
18
    widths.excepsV = (GfxFontCIDWidthExcepV *)
2005
18
      greallocn(widths.excepsV,
2006
18
          excepsSize, sizeof(GfxFontCIDWidthExcepV));
2007
18
  }
2008
23
  j = obj2.getInt();
2009
134
  for (k = 0; k + 2 < obj3.arrayGetLength(); k += 3) {
2010
111
    if (obj3.arrayGet(k, &obj4)->isNum() &&
2011
60
        obj3.arrayGet(k+1, &obj5)->isNum() &&
2012
38
        obj3.arrayGet(k+2, &obj6)->isNum()) {
2013
26
      widths.excepsV[widths.nExcepsV].first = j;
2014
26
      widths.excepsV[widths.nExcepsV].last = j;
2015
26
      widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
2016
26
      widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
2017
26
      widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
2018
26
      ++j;
2019
26
      ++widths.nExcepsV;
2020
85
    } else {
2021
85
      error(errSyntaxError, -1, "Bad widths (W2) array in Type 0 font");
2022
85
    }
2023
111
    obj6.free();
2024
111
    obj5.free();
2025
111
    obj4.free();
2026
111
  }
2027
23
  i += 2;
2028
139
      } else {
2029
139
  error(errSyntaxError, -1, "Bad widths (W2) array in Type 0 font");
2030
139
  ++i;
2031
139
      }
2032
201
      obj3.free();
2033
201
      obj2.free();
2034
201
    }
2035
37
  }
2036
2.45k
  obj1.free();
2037
2038
2.45k
  desFontDictObj.free();
2039
2.45k
  ok = gTrue;
2040
2.45k
  return;
2041
2042
65
 err3:
2043
65
  obj3.free();
2044
65
  obj2.free();
2045
1.86k
 err2:
2046
1.86k
  obj1.free();
2047
1.86k
  desFontDictObj.free();
2048
1.89k
 err1:
2049
1.89k
  error(errSyntaxError, -1, "Failed to parse font object for '{0:t}'", name);
2050
1.89k
}
2051
2052
4.35k
GfxCIDFont::~GfxCIDFont() {
2053
4.35k
  if (collection) {
2054
4.04k
    delete collection;
2055
4.04k
  }
2056
4.35k
  if (cMap) {
2057
2.45k
    cMap->decRefCnt();
2058
2.45k
  }
2059
4.35k
  if (ctu) {
2060
2.45k
    ctu->decRefCnt();
2061
2.45k
  }
2062
4.35k
  gfree(widths.exceps);
2063
4.35k
  gfree(widths.excepsV);
2064
4.35k
  if (cidToGID) {
2065
41
    gfree(cidToGID);
2066
41
  }
2067
4.35k
}
2068
2069
// Construct a code-to-Unicode mapping, based on the TrueType Unicode
2070
// cmap (if present).  Constructs ctu if succesful; leaves ctu = null
2071
// otherwise.  Always leaves ctu = null for non-TrueType fonts.
2072
0
void GfxCIDFont::readTrueTypeUnicodeMapping(XRef *xref) {
2073
0
  char *buf;
2074
0
  FoFiTrueType *ff;
2075
0
  Unicode *gidToUnicode, *codeToUnicode;
2076
0
  Unicode u;
2077
0
  int bufLen, cmapPlatform, cmapEncoding, unicodeCmap;
2078
0
  int nGlyphs, nMappings, gid, i;
2079
2080
  // must be an embedded TrueType font, with an unknown char collection
2081
0
  if ((type != fontCIDType2 && type != fontCIDType2OT) ||
2082
0
      embFontID.num < 0 ||
2083
0
      hasKnownCollection) {
2084
0
    goto err0;
2085
0
  }
2086
2087
  // read the embedded font and construct a FoFiTrueType
2088
0
  if (!(buf = readEmbFontFile(xref, &bufLen))) {
2089
0
    goto err0;
2090
0
  }
2091
0
  if (!(ff = FoFiTrueType::make(buf, bufLen, 0))) {
2092
0
    goto err1;
2093
0
  }
2094
2095
  // find the TrueType Unicode cmap
2096
0
  unicodeCmap = -1;
2097
0
  for (i = 0; i < ff->getNumCmaps(); ++i) {
2098
0
    cmapPlatform = ff->getCmapPlatform(i);
2099
0
    cmapEncoding = ff->getCmapEncoding(i);
2100
0
    if ((cmapPlatform == 3 && cmapEncoding == 1) ||
2101
0
  (cmapPlatform == 0 && cmapEncoding <= 4)) {
2102
0
      unicodeCmap = i;
2103
0
      break;
2104
0
    }
2105
0
  }
2106
0
  if (unicodeCmap < 0) {
2107
0
    goto err2;
2108
0
  }
2109
2110
  // construct reverse GID-to-Unicode map
2111
0
  nGlyphs = ff->getNumGlyphs();
2112
0
  gidToUnicode = (Unicode *)gmallocn(nGlyphs, sizeof(Unicode));
2113
0
  memset(gidToUnicode, 0, nGlyphs * sizeof(Unicode));
2114
0
  nMappings = 0;
2115
0
  for (u = 1; u <= 0xffff; ++u) {
2116
0
    gid = ff->mapCodeToGID(unicodeCmap, (int)u);
2117
0
    if (gid > 0 && gid < nGlyphs) {
2118
0
      gidToUnicode[gid] = u;
2119
0
      ++nMappings;
2120
0
    }
2121
0
  }
2122
  // bail out if the Unicode cmap was completely empty
2123
0
  if (nMappings == 0) {
2124
0
    goto err3;
2125
0
  }
2126
2127
  // construct code-to-Unicode map
2128
0
  codeToUnicode = (Unicode *)gmallocn(65536, sizeof(Unicode));
2129
0
  memset(codeToUnicode, 0, 65536 * sizeof(Unicode));
2130
0
  for (i = 0; i <= 0xffff; ++i) {
2131
    // we've already checked for an identity encoding, so CID = i
2132
0
    if (cidToGID && i < cidToGIDLen) {
2133
0
      gid = cidToGID[i];
2134
0
    } else {
2135
0
      gid = i;
2136
0
    }
2137
0
    if (gid < nGlyphs && gidToUnicode[gid] > 0) {
2138
0
      codeToUnicode[i] = gidToUnicode[gid];
2139
0
    }
2140
0
  }
2141
0
  ctu = CharCodeToUnicode::make16BitToUnicode(codeToUnicode);
2142
2143
0
  gfree(codeToUnicode);
2144
0
 err3:
2145
0
  gfree(gidToUnicode);
2146
0
 err2:
2147
0
  delete ff;
2148
0
 err1:
2149
0
  gfree(buf);
2150
0
 err0:
2151
0
  return;
2152
0
}
2153
2154
int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
2155
          Unicode *u, int uSize, int *uLen,
2156
103k
          double *dx, double *dy, double *ox, double *oy) {
2157
103k
  CID cid;
2158
103k
  CharCode c;
2159
103k
  int n;
2160
2161
103k
  if (!cMap) {
2162
0
    *code = 0;
2163
0
    *uLen = 0;
2164
0
    *dx = *dy = 0;
2165
0
    return 1;
2166
0
  }
2167
2168
103k
  *code = (CharCode)(cid = cMap->getCID(s, len, &c, &n));
2169
103k
  if (ctu) {
2170
103k
    *uLen = ctu->mapToUnicode(ctuUsesCharCode ? c : cid, u, uSize);
2171
103k
  } else {
2172
0
    *uLen = 0;
2173
0
  }
2174
103k
  if (!*uLen && uSize >= 1 && globalParams->getMapUnknownCharNames()) {
2175
0
    u[0] = *code;
2176
0
    *uLen = 1;
2177
0
  }
2178
2179
  // horizontal
2180
103k
  if (cMap->getWMode() == 0) {
2181
101k
    getHorizontalMetrics(cid, dx);
2182
101k
    *dy = *ox = *oy = 0;
2183
2184
  // vertical
2185
101k
  } else {
2186
2.30k
    getVerticalMetrics(cid, dy, ox, oy);
2187
2.30k
    *dx = 0;
2188
2.30k
  }
2189
2190
103k
  return n;
2191
103k
}
2192
2193
// NB: Section 9.7.4.3 in the PDF 2.0 spec says that, in the case of
2194
// duplicate entries in the metrics, the first entry should be used.
2195
// This means we need to leave the metrics in the original order and
2196
// perform a linear search.  (Or use a more complex data structure.)
2197
103k
void GfxCIDFont::getHorizontalMetrics(CID cid, double *w) {
2198
103k
  int i;
2199
652k
  for (i = 0; i < widths.nExceps; ++i) {
2200
566k
    if (widths.exceps[i].first <= cid && cid <= widths.exceps[i].last) {
2201
17.7k
      *w = widths.exceps[i].width;
2202
17.7k
      return;
2203
17.7k
    }
2204
566k
  }
2205
85.9k
  *w = widths.defWidth;
2206
85.9k
}
2207
2208
// NB: Section 9.7.4.3 in the PDF 2.0 spec says that, in the case of
2209
// duplicate entries in the metrics, the first entry should be used.
2210
// This means we need to leave the metrics in the original order and
2211
// perform a linear search.  (Or use a more complex data structure.)
2212
void GfxCIDFont::getVerticalMetrics(CID cid, double *h,
2213
2.30k
            double *vx, double *vy) {
2214
2.30k
  int i;
2215
2.51k
  for (i = 0; i < widths.nExcepsV; ++i) {
2216
277
    if (widths.excepsV[i].first <= cid && cid <= widths.excepsV[i].last) {
2217
72
      *h = widths.excepsV[i].height;
2218
72
      *vx = widths.excepsV[i].vx;
2219
72
      *vy = widths.excepsV[i].vy;
2220
72
      return;
2221
72
    }
2222
277
  }
2223
2.23k
  *h = widths.defHeight;
2224
2.23k
  getHorizontalMetrics(cid, vx);
2225
2.23k
  *vx /= 2;
2226
2.23k
  *vy = widths.defVY;
2227
2.23k
}
2228
2229
32.5k
int GfxCIDFont::getWMode() {
2230
32.5k
  return cMap ? cMap->getWMode() : 0;
2231
32.5k
}
2232
2233
0
CharCodeToUnicode *GfxCIDFont::getToUnicode() {
2234
0
  if (ctu) {
2235
0
    ctu->incRefCnt();
2236
0
  }
2237
0
  return ctu;
2238
0
}
2239
2240
1.82k
GString *GfxCIDFont::getCollection() {
2241
1.82k
  return cMap ? cMap->getCollection() : (GString *)NULL;
2242
1.82k
}
2243
2244
0
double GfxCIDFont::getWidth(CID cid) {
2245
0
  double w;
2246
2247
0
  getHorizontalMetrics(cid, &w);
2248
0
  return w;
2249
0
}
2250
2251
0
GBool GfxCIDFont::problematicForUnicode() {
2252
0
  GString *nameLC;
2253
0
  GBool symbolic;
2254
2255
  // potential inputs:
2256
  // - font is embedded (GfxFont.embFontID.num >= 0)
2257
  // - font name (GfxFont.name)
2258
  // - font type (GfxFont.type)
2259
  // - symbolic (GfxFont.flags & fontSymbolic)
2260
  // - has a ToUnicode map (GfxFont.hasToUnicode)
2261
  // - collection is Adobe-Identity or Adobe-UCS
2262
  //   (GfxCIDFont.collection - compare string)
2263
  // - collection is known AdobeCJK (GfxCIDFont.hasKnownCollection)
2264
  // - has non-Identity CIDToGIDMap (GfxCIDFont.cidToGID != NULL)
2265
  // - has Identity CIDToGIDMap (GfxCIDFont.hasIdentityCIDToGID)
2266
2267
0
  if (name) {
2268
0
    nameLC = name->copy();
2269
0
    nameLC->lowerCase();
2270
0
    symbolic = strstr(nameLC->getCString(), "dingbat") ||
2271
0
               strstr(nameLC->getCString(), "wingding") ||
2272
0
               strstr(nameLC->getCString(), "commpi");
2273
0
    delete nameLC;
2274
0
    if (symbolic) {
2275
0
      return gFalse;
2276
0
    }
2277
0
  }
2278
2279
0
  if (embFontID.num >= 0) {
2280
0
    switch (type) {
2281
0
    case fontCIDType0:
2282
0
    case fontCIDType0C:
2283
0
    case fontCIDType0COT:
2284
0
      return !hasToUnicode && !hasKnownCollection;
2285
2286
0
    case fontCIDType2:
2287
0
    case fontCIDType2OT:
2288
0
      return !hasToUnicode && !hasKnownCollection;
2289
2290
0
    default:
2291
0
      return !hasToUnicode;
2292
0
    }
2293
2294
0
  } else {
2295
0
    return !hasToUnicode;
2296
0
  }
2297
0
}
2298
2299
0
GBool GfxCIDFont::parensAreSwapped(XRef *xref) {
2300
0
  char *buf = NULL;
2301
0
  FoFiTrueType *ff = NULL;
2302
0
  int bufLen;
2303
0
  int unicodeCmap, leftParenGID, rightParenGID, leftParenCID, rightParenCID;
2304
0
  Unicode leftParenUnicode, rightParenUnicode;
2305
0
  GBool swapped;
2306
2307
  // only relevant for embedded TrueType fonts, with unknown char
2308
  // collections, and with ToUnicode maps
2309
0
  if ((type != fontCIDType2 && type != fontCIDType2OT) ||
2310
0
      embFontID.num < 0 ||
2311
0
      hasKnownCollection ||
2312
0
      !hasToUnicode) {
2313
0
    return gFalse;
2314
0
  }
2315
2316
  // read the embedded font and construct a FoFiTrueType
2317
0
  if (!(buf = readEmbFontFile(xref, &bufLen))) {
2318
0
    goto err;
2319
0
  }
2320
0
  if (!(ff = FoFiTrueType::make(buf, bufLen, 0))) {
2321
0
    goto err;
2322
0
  }
2323
2324
  // find the TrueType Unicode cmap
2325
0
  unicodeCmap = -1;
2326
0
  for (int i = 0; i < ff->getNumCmaps(); ++i) {
2327
0
    int cmapPlatform = ff->getCmapPlatform(i);
2328
0
    int cmapEncoding = ff->getCmapEncoding(i);
2329
0
    if ((cmapPlatform == 3 && cmapEncoding == 1) ||
2330
0
  (cmapPlatform == 0 && cmapEncoding <= 4)) {
2331
0
      unicodeCmap = i;
2332
0
      break;
2333
0
    }
2334
0
  }
2335
0
  if (unicodeCmap < 0) {
2336
0
    goto err;
2337
0
  }
2338
2339
  // map parens to GIDs using the TrueType Unicode cmap
2340
0
  leftParenGID = ff->mapCodeToGID(unicodeCmap, 0x28);
2341
0
  rightParenGID = ff->mapCodeToGID(unicodeCmap, 0x29);
2342
0
  if (leftParenGID == 0 || rightParenGID == 0) {
2343
0
    goto err;
2344
0
  }
2345
2346
  // map GIDs to CIDs using the inverted CIDToGID map
2347
0
  leftParenCID = -1;
2348
0
  rightParenCID = -1;
2349
0
  if (cidToGID) {
2350
0
    for (int i = 0;
2351
0
   i <= cidToGIDLen && leftParenCID < 0 && rightParenCID < 0;
2352
0
   ++i) {
2353
0
      if (cidToGID[i] == leftParenGID) {
2354
0
  leftParenCID = i;
2355
0
      } else if (cidToGID[i] == rightParenGID) {
2356
0
  rightParenCID = i;
2357
0
      }
2358
0
    }
2359
0
    if (leftParenCID < 0 || rightParenCID < 0) {
2360
0
      goto err;
2361
0
    }
2362
0
  } else {
2363
0
    leftParenCID = leftParenGID;
2364
0
    rightParenCID = rightParenGID;
2365
0
  }
2366
2367
  // map CIDs to Unicode using the ToUnicode map
2368
0
  if (ctu->mapToUnicode((CharCode)leftParenCID, &leftParenUnicode, 1) < 1 ||
2369
0
      ctu->mapToUnicode((CharCode)rightParenCID, &rightParenUnicode, 1) < 1) {
2370
0
    goto err;
2371
0
  }
2372
2373
  // check for swap
2374
0
  swapped = leftParenUnicode == 0x29 && rightParenUnicode == 0x28;
2375
2376
0
  delete ff;
2377
0
  ff = NULL;
2378
0
  gfree(buf);
2379
0
  buf = NULL;
2380
2381
0
  return swapped;
2382
2383
0
 err:
2384
0
  if (ff) {
2385
0
    delete ff;
2386
0
  }
2387
0
  if (buf) {
2388
0
    gfree(buf);
2389
0
  }
2390
0
  return gFalse;
2391
0
}
2392
2393
//------------------------------------------------------------------------
2394
// GfxFontDict
2395
//------------------------------------------------------------------------
2396
2397
class GfxFontDictEntry {
2398
public:
2399
2400
  GfxFontDictEntry(Ref refA, Object *fontObjA);
2401
  ~GfxFontDictEntry();
2402
  void load(GfxFont *fontA);
2403
2404
  GBool loaded;
2405
  Ref ref;
2406
  Object fontObj;   // valid if unloaded
2407
  GfxFont *font;    // valid if loaded
2408
};
2409
2410
81.4k
GfxFontDictEntry::GfxFontDictEntry(Ref refA, Object *fontObjA) {
2411
81.4k
  loaded = gFalse;
2412
81.4k
  ref = refA;
2413
81.4k
  fontObjA->copy(&fontObj);
2414
81.4k
  font = NULL;
2415
81.4k
}
2416
2417
81.3k
GfxFontDictEntry::~GfxFontDictEntry() {
2418
  // NB: If the font has been loaded, font is non-NULL and is owned by
2419
  // GfxFontDict.uniqueFonts.
2420
81.3k
  if (!loaded) {
2421
52.9k
    fontObj.free();
2422
52.9k
  }
2423
81.3k
}
2424
2425
28.4k
void GfxFontDictEntry::load(GfxFont *fontA) {
2426
28.4k
  loaded = gTrue;
2427
28.4k
  font = fontA;
2428
28.4k
  fontObj.free();
2429
28.4k
}
2430
2431
62.5k
GfxFontDict::GfxFontDict(XRef *xrefA, Ref *fontDictRef, Dict *fontDict) {
2432
62.5k
  xref = xrefA;
2433
62.5k
  fonts = new GHash(gTrue);
2434
62.5k
  uniqueFonts = new GList();
2435
2436
143k
  for (int i = 0; i < fontDict->getLength(); ++i) {
2437
81.4k
    char *tag = fontDict->getKey(i);
2438
81.4k
    Object fontObj;
2439
81.4k
    fontDict->getValNF(i, &fontObj);
2440
81.4k
    Ref r;
2441
81.4k
    if (fontObj.isRef()) {
2442
72.0k
      r = fontObj.getRef();
2443
72.0k
    } else if (fontDictRef) {
2444
      // legal generation numbers are five digits, so we use a
2445
      // 6-digit number here
2446
157
      r.gen = 100000 + fontDictRef->num;
2447
157
      r.num = i;
2448
9.24k
    } else {
2449
      // no indirect reference for this font, or for the containing
2450
      // font dict, so hash the font and use that
2451
9.24k
      r.gen = 100000;
2452
9.24k
      r.num = hashFontObject(&fontObj);
2453
9.24k
    }
2454
81.4k
    fonts->add(new GString(tag), new GfxFontDictEntry(r, &fontObj));
2455
81.4k
    fontObj.free();
2456
81.4k
  }
2457
62.5k
}
2458
2459
62.5k
GfxFontDict::~GfxFontDict() {
2460
62.5k
  deleteGList(uniqueFonts, GfxFont);
2461
62.5k
  deleteGHash(fonts, GfxFontDictEntry);
2462
62.5k
}
2463
2464
86.6k
GfxFont *GfxFontDict::lookup(char *tag) {
2465
86.6k
  GfxFontDictEntry *entry = (GfxFontDictEntry *)fonts->lookup(tag);
2466
86.6k
  if (!entry) {
2467
31.1k
    return NULL;
2468
31.1k
  }
2469
55.5k
  load(tag, entry);
2470
55.5k
  return entry->font;
2471
86.6k
}
2472
2473
0
GfxFont *GfxFontDict::lookupByRef(Ref ref) {
2474
0
  GHashIter *iter;
2475
0
  GString *tag;
2476
0
  GfxFontDictEntry *entry;
2477
0
  fonts->startIter(&iter);
2478
0
  while (fonts->getNext(&iter, &tag, (void **)&entry)) {
2479
0
    if (entry->ref.num == ref.num && entry->ref.gen == ref.gen) {
2480
0
      fonts->killIter(&iter);
2481
0
      load(tag->getCString(), entry);
2482
0
      return entry->font;
2483
0
    }
2484
0
  }
2485
0
  return NULL;
2486
0
}
2487
2488
0
int GfxFontDict::getNumFonts() {
2489
0
  loadAll();
2490
0
  return uniqueFonts->getLength();
2491
0
}
2492
2493
0
GfxFont *GfxFontDict::getFont(int i) {
2494
0
  return (GfxFont *)uniqueFonts->get(i);
2495
0
}
2496
2497
0
void GfxFontDict::loadAll() {
2498
0
  GHashIter *iter;
2499
0
  GString *tag;
2500
0
  GfxFontDictEntry *entry;
2501
0
  fonts->startIter(&iter);
2502
0
  while (fonts->getNext(&iter, &tag, (void **)&entry)) {
2503
0
    load(tag->getCString(), entry);
2504
0
  }
2505
0
}
2506
2507
55.5k
void GfxFontDict::load(char *tag, GfxFontDictEntry *entry) {
2508
55.5k
  if (entry->loaded) {
2509
27.0k
    return;
2510
27.0k
  }
2511
2512
  // check for a duplicate that has already been loaded
2513
  // (don't do this for "synthetic" refs)
2514
28.4k
  if (entry->fontObj.isRef()) {
2515
30.4k
    for (int i = 0; i < uniqueFonts->getLength(); ++i) {
2516
4.39k
      GfxFont *font = (GfxFont *)uniqueFonts->get(i);
2517
4.39k
      if (font->getID()->num == entry->ref.num &&
2518
9
    font->getID()->gen == entry->ref.gen) {
2519
2
  entry->load(font);
2520
2
  return;
2521
2
      }
2522
4.39k
    }
2523
26.0k
  }
2524
2525
28.4k
  GfxFont *font = NULL;
2526
28.4k
  Object obj;
2527
28.4k
  entry->fontObj.fetch(xref, &obj);
2528
28.4k
  if (obj.isDict()) {
2529
22.4k
    font = GfxFont::makeFont(xref, tag, entry->ref, obj.getDict());
2530
22.4k
    if (font->isOk()) {
2531
20.5k
      uniqueFonts->append(font);
2532
20.5k
    } else {
2533
1.89k
      delete font;
2534
1.89k
      font = NULL;
2535
1.89k
    }
2536
22.4k
  } else {
2537
6.03k
    error(errSyntaxError, -1, "font resource is not a dictionary");
2538
6.03k
  }
2539
28.4k
  obj.free();
2540
28.4k
  entry->load(font);
2541
28.4k
}
2542
2543
// FNV-1a hash
2544
class FNVHash {
2545
public:
2546
2547
16.6k
  FNVHash() {
2548
16.6k
    h = 2166136261U;
2549
16.6k
  }
2550
2551
2.81M
  void hash(char c) {
2552
2.81M
    h ^= c & 0xff;
2553
2.81M
    h *= 16777619;
2554
2.81M
  }
2555
2556
109k
  void hash(char *p, int n) {
2557
109k
    int i;
2558
2.84M
    for (i = 0; i < n; ++i) {
2559
2.74M
      hash(p[i]);
2560
2.74M
    }
2561
109k
  }
2562
2563
16.6k
  int get31() {
2564
16.6k
    return (h ^ (h >> 31)) & 0x7fffffff;
2565
16.6k
  }
2566
2567
private:
2568
2569
  Guint h;
2570
};
2571
2572
16.6k
int GfxFontDict::hashFontObject(Object *obj) {
2573
16.6k
  FNVHash h;
2574
2575
16.6k
  hashFontObject1(obj, &h);
2576
16.6k
  return h.get31();
2577
16.6k
}
2578
2579
73.3k
void GfxFontDict::hashFontObject1(Object *obj, FNVHash *h) {
2580
73.3k
  Object obj2;
2581
73.3k
  GString *s;
2582
73.3k
  char *p;
2583
73.3k
  double r;
2584
73.3k
  int n, i;
2585
2586
73.3k
  switch (obj->getType()) {
2587
157
  case objBool:
2588
157
    h->hash('b');
2589
157
    h->hash(obj->getBool() ? 1 : 0);
2590
157
    break;
2591
10.2k
  case objInt:
2592
10.2k
    h->hash('i');
2593
10.2k
    n = obj->getInt();
2594
10.2k
    h->hash((char *)&n, sizeof(int));
2595
10.2k
    break;
2596
1.95k
  case objReal:
2597
1.95k
    h->hash('r');
2598
1.95k
    r = obj->getReal();
2599
1.95k
    h->hash((char *)&r, sizeof(double));
2600
1.95k
    break;
2601
1.57k
  case objString:
2602
1.57k
    h->hash('s');
2603
1.57k
    s = obj->getString();
2604
1.57k
    h->hash(s->getCString(), s->getLength());
2605
1.57k
    break;
2606
35.2k
  case objName:
2607
35.2k
    h->hash('n');
2608
35.2k
    p = obj->getName();
2609
35.2k
    h->hash(p, (int)strlen(p));
2610
35.2k
    break;
2611
125
  case objNull:
2612
125
    h->hash('z');
2613
125
    break;
2614
1.41k
  case objArray:
2615
1.41k
    h->hash('a');
2616
1.41k
    n = obj->arrayGetLength();
2617
1.41k
    h->hash((char *)&n, sizeof(int));
2618
18.6k
    for (i = 0; i < n; ++i) {
2619
17.2k
      obj->arrayGetNF(i, &obj2);
2620
17.2k
      hashFontObject1(&obj2, h);
2621
17.2k
      obj2.free();
2622
17.2k
    }
2623
1.41k
    break;
2624
11.8k
  case objDict:
2625
11.8k
    h->hash('d');
2626
11.8k
    n = obj->dictGetLength();
2627
11.8k
    h->hash((char *)&n, sizeof(int));
2628
51.2k
    for (i = 0; i < n; ++i) {
2629
39.4k
      p = obj->dictGetKey(i);
2630
39.4k
      h->hash(p, (int)strlen(p));
2631
39.4k
      obj->dictGetValNF(i, &obj2);
2632
39.4k
      hashFontObject1(&obj2, h);
2633
39.4k
      obj2.free();
2634
39.4k
    }
2635
11.8k
    break;
2636
195
  case objStream:
2637
    // this should never happen - streams must be indirect refs
2638
195
    break;
2639
4.02k
  case objRef:
2640
4.02k
    h->hash('f');
2641
4.02k
    n = obj->getRefNum();
2642
4.02k
    h->hash((char *)&n, sizeof(int));
2643
4.02k
    n = obj->getRefGen();
2644
4.02k
    h->hash((char *)&n, sizeof(int));
2645
4.02k
    break;
2646
6.50k
  default:
2647
6.50k
    h->hash('u');
2648
6.50k
    break;
2649
73.3k
  }
2650
73.3k
}