Coverage Report

Created: 2026-04-04 06:06

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
1.43M
static int readFromStream(void *data) {
135
1.43M
  return ((Stream *)data)->getChar();
136
1.43M
}
137
138
//------------------------------------------------------------------------
139
// GfxFontLoc
140
//------------------------------------------------------------------------
141
142
6.77k
GfxFontLoc::GfxFontLoc() {
143
6.77k
  path = NULL;
144
6.77k
  fontNum = 0;
145
6.77k
  oblique = 0;
146
6.77k
  encoding = NULL;
147
6.77k
  substIdx = -1;
148
6.77k
}
149
150
6.77k
GfxFontLoc::~GfxFontLoc() {
151
6.77k
  if (path) {
152
0
    delete path;
153
0
  }
154
6.77k
  if (encoding) {
155
0
    delete encoding;
156
0
  }
157
6.77k
}
158
159
//------------------------------------------------------------------------
160
// GfxFont
161
//------------------------------------------------------------------------
162
163
GfxFont *GfxFont::makeFont(XRef *xref, const char *tagA,
164
41.6k
         Ref idA, Dict *fontDict) {
165
41.6k
  GString *nameA;
166
41.6k
  Ref embFontIDA;
167
41.6k
  GfxFontType typeA;
168
41.6k
  GfxFont *font;
169
41.6k
  Object obj1;
170
171
  // get base font name
172
41.6k
  nameA = NULL;
173
41.6k
  fontDict->lookup("BaseFont", &obj1);
174
41.6k
  if (obj1.isName()) {
175
28.3k
    nameA = new GString(obj1.getName());
176
28.3k
  } else if (obj1.isString()) {
177
9
    nameA = obj1.getString()->copy();
178
9
  }
179
41.6k
  obj1.free();
180
181
  // get embedded font ID and font type
182
41.6k
  typeA = getFontType(xref, fontDict, &embFontIDA);
183
184
  // create the font object
185
41.6k
  font = NULL;
186
41.6k
  if (typeA < fontCIDType0) {
187
28.5k
    font = new Gfx8BitFont(xref, tagA, idA, nameA, typeA, embFontIDA,
188
28.5k
         fontDict);
189
28.5k
  } else {
190
13.0k
    font = new GfxCIDFont(xref, tagA, idA, nameA, typeA, embFontIDA,
191
13.0k
        fontDict);
192
13.0k
  }
193
194
41.6k
  return font;
195
41.6k
}
196
197
10.2k
GfxFont *GfxFont::makeDefaultFont(XRef *xref) {
198
10.2k
  Object type, subtype, baseFont;
199
10.2k
  type.initName("Font");
200
10.2k
  subtype.initName("Type1");
201
10.2k
  baseFont.initName("Helvetica");
202
10.2k
  Object fontDict;
203
10.2k
  fontDict.initDict(xref);
204
10.2k
  fontDict.dictAdd(copyString("Type"), &type);
205
10.2k
  fontDict.dictAdd(copyString("Subtype"), &subtype);
206
10.2k
  fontDict.dictAdd(copyString("BaseFont"), &baseFont);
207
208
10.2k
  Ref r;
209
10.2k
  r.gen = 100000;
210
10.2k
  r.num = GfxFontDict::hashFontObject(&fontDict);
211
212
10.2k
  GfxFont *font = makeFont(xref, "undef", r, fontDict.getDict());
213
10.2k
  fontDict.free();
214
215
10.2k
  return font;
216
10.2k
}
217
218
GfxFont::GfxFont(const char *tagA, Ref idA, GString *nameA,
219
41.6k
     GfxFontType typeA, Ref embFontIDA) {
220
41.6k
  ok = gFalse;
221
41.6k
  tag = new GString(tagA);
222
41.6k
  id = idA;
223
41.6k
  name = nameA;
224
41.6k
  type = typeA;
225
41.6k
  embFontID = embFontIDA;
226
41.6k
  embFontName = NULL;
227
41.6k
  hasToUnicode = gFalse;
228
41.6k
}
229
230
41.6k
GfxFont::~GfxFont() {
231
41.6k
  delete tag;
232
41.6k
  if (name) {
233
28.3k
    delete name;
234
28.3k
  }
235
41.6k
  if (embFontName) {
236
14.9k
    delete embFontName;
237
14.9k
  }
238
41.6k
}
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
41.6k
GfxFontType GfxFont::getFontType(XRef *xref, Dict *fontDict, Ref *embID) {
251
41.6k
  GfxFontType t, expectedType;
252
41.6k
  FoFiIdentifierType fft;
253
41.6k
  Dict *fontDict2;
254
41.6k
  Object subtype, fontDesc, obj1, obj2, obj3, obj4;
255
41.6k
  GBool isType0, err;
256
257
41.6k
  t = fontUnknownType;
258
41.6k
  embID->num = embID->gen = -1;
259
41.6k
  err = gFalse;
260
261
41.6k
  fontDict->lookup("Subtype", &subtype);
262
41.6k
  expectedType = fontUnknownType;
263
41.6k
  isType0 = gFalse;
264
41.6k
  if (subtype.isName("Type1") || subtype.isName("MMType1")) {
265
16.4k
    expectedType = fontType1;
266
25.1k
  } else if (subtype.isName("Type1C")) {
267
4
    expectedType = fontType1C;
268
25.1k
  } else if (subtype.isName("Type3")) {
269
2.43k
    expectedType = fontType3;
270
22.7k
  } else if (subtype.isName("TrueType")) {
271
2.10k
    expectedType = fontTrueType;
272
20.6k
  } else if (subtype.isName("Type0")) {
273
13.8k
    isType0 = gTrue;
274
13.8k
  } else {
275
6.78k
    error(errSyntaxWarning, -1, "Unknown font type: '{0:s}'",
276
6.78k
    subtype.isName() ? subtype.getName() : "???");
277
6.78k
  }
278
41.6k
  subtype.free();
279
280
41.6k
  fontDict2 = fontDict;
281
41.6k
  if (fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
282
17.3k
    if (obj1.arrayGetLength() == 0) {
283
1
      error(errSyntaxWarning, -1, "Empty DescendantFonts array in font");
284
1
      obj2.initNull();
285
17.3k
    } else if (obj1.arrayGet(0, &obj2)->isDict()) {
286
16.9k
      if (!isType0) {
287
3.81k
  error(errSyntaxWarning, -1, "Non-CID font with DescendantFonts array");
288
3.81k
      }
289
16.9k
      fontDict2 = obj2.getDict();
290
16.9k
      fontDict2->lookup("Subtype", &subtype);
291
16.9k
      if (subtype.isName("CIDFontType0")) {
292
6.86k
  if (isType0) {
293
6.85k
    expectedType = fontCIDType0;
294
6.85k
  }
295
10.1k
      } else if (subtype.isName("CIDFontType2")) {
296
6.89k
  if (isType0) {
297
5.81k
    expectedType = fontCIDType2;
298
5.81k
  }
299
6.89k
      }
300
16.9k
      subtype.free();
301
16.9k
    }
302
24.3k
  } else {
303
24.3k
    obj2.initNull();
304
24.3k
  }
305
306
  // NB: the PDF spec doesn't say anything about precedence, but Adobe
307
  // uses FontFile3 over FontFile2 if both are present.
308
41.6k
  if (fontDict2->lookup("FontDescriptor", &fontDesc)->isDict()) {
309
20.8k
    if (fontDesc.dictLookupNF("FontFile3", &obj3)->isRef()) {
310
6.38k
      *embID = obj3.getRef();
311
6.38k
      if (obj3.fetch(xref, &obj4)->isStream()) {
312
5.94k
  obj4.streamGetDict()->lookup("Subtype", &subtype);
313
5.94k
  if (subtype.isName("Type1")) {
314
17
    if (expectedType != fontType1) {
315
15
      err = gTrue;
316
15
      expectedType = isType0 ? fontCIDType0 : fontType1;
317
15
    }
318
5.92k
  } else if (subtype.isName("Type1C")) {
319
2.16k
    if (expectedType == fontType1) {
320
1.98k
      expectedType = fontType1C;
321
1.98k
    } else if (expectedType != fontType1C) {
322
176
      err = gTrue;
323
176
      expectedType = isType0 ? fontCIDType0C : fontType1C;
324
176
    }
325
3.76k
  } else if (subtype.isName("TrueType")) {
326
0
    if (expectedType != fontTrueType) {
327
0
      err = gTrue;
328
0
      expectedType = isType0 ? fontCIDType2 : fontTrueType;
329
0
    }
330
3.76k
  } else if (subtype.isName("CIDFontType0C")) {
331
277
    if (expectedType == fontCIDType0) {
332
68
      expectedType = fontCIDType0C;
333
209
    } else {
334
209
      err = gTrue;
335
209
      expectedType = isType0 ? fontCIDType0C : fontType1C;
336
209
    }
337
3.48k
  } else if (subtype.isName("OpenType")) {
338
137
    if (expectedType == fontTrueType) {
339
0
      expectedType = fontTrueTypeOT;
340
137
    } else if (expectedType == fontType1) {
341
82
      expectedType = fontType1COT;
342
82
    } else if (expectedType == fontCIDType0) {
343
27
      expectedType = fontCIDType0COT;
344
28
    } else if (expectedType == fontCIDType2) {
345
0
      expectedType = fontCIDType2OT;
346
28
    } else {
347
28
      err = gTrue;
348
28
    }
349
3.35k
  } else {
350
3.35k
    error(errSyntaxError, -1, "Unknown font type '{0:s}'",
351
3.35k
    subtype.isName() ? subtype.getName() : "???");
352
3.35k
  }
353
5.94k
  subtype.free();
354
5.94k
      }
355
6.38k
      obj4.free();
356
6.38k
    }
357
20.8k
    obj3.free();
358
20.8k
    if (embID->num == -1) {
359
14.4k
      if (fontDesc.dictLookupNF("FontFile2", &obj3)->isRef()) {
360
8.70k
  *embID = obj3.getRef();
361
8.70k
  if (isType0) {
362
5.47k
    expectedType = fontCIDType2;
363
5.47k
  } else if (expectedType != fontTrueType) {
364
1.22k
    err = gTrue;
365
1.22k
  }
366
8.70k
      }
367
14.4k
      obj3.free();
368
14.4k
    }
369
20.8k
    if (embID->num == -1) {
370
5.74k
      if (fontDesc.dictLookupNF("FontFile", &obj3)->isRef()) {
371
2.16k
  *embID = obj3.getRef();
372
2.16k
  if (expectedType != fontType1) {
373
470
    err = gTrue;
374
470
  }
375
2.16k
      }
376
5.74k
      obj3.free();
377
5.74k
    }
378
20.8k
  }
379
41.6k
  fontDesc.free();
380
381
41.6k
  t = fontUnknownType;
382
41.6k
  if (embID->num >= 0) {
383
17.2k
    obj3.initRef(embID->num, embID->gen);
384
17.2k
    obj3.fetch(xref, &obj4);
385
17.2k
    if (obj4.isStream()) {
386
16.1k
      obj4.streamReset();
387
16.1k
      fft = FoFiIdentifier::identifyStream(&readFromStream, obj4.getStream());
388
16.1k
      obj4.streamClose();
389
16.1k
      switch (fft) {
390
1.78k
      case fofiIdType1PFA:
391
1.78k
      case fofiIdType1PFB:
392
1.78k
  t = fontType1;
393
1.78k
  break;
394
2.65k
      case fofiIdCFF8Bit:
395
2.65k
  t = isType0 ? fontCIDType0C : fontType1C;
396
2.65k
  break;
397
50
      case fofiIdCFFCID:
398
50
  t = fontCIDType0C;
399
50
  break;
400
7.04k
      case fofiIdTrueType:
401
7.04k
      case fofiIdTrueTypeCollection:
402
7.04k
  t = isType0 ? fontCIDType2 : fontTrueType;
403
7.04k
  break;
404
11
      case fofiIdOpenTypeCFF8Bit:
405
11
  t = isType0 ? fontCIDType0COT : fontType1COT;
406
11
  break;
407
0
      case fofiIdOpenTypeCFFCID:
408
0
  t = fontCIDType0COT;
409
0
  break;
410
4.59k
      default:
411
4.59k
  error(errSyntaxError, -1, "Embedded font file may be invalid");
412
4.59k
  break;
413
16.1k
      }
414
16.1k
    } else {
415
1.13k
      error(errSyntaxError, -1, "Embedded font object is wrong type");
416
1.13k
    }
417
17.2k
    obj4.free();
418
17.2k
    obj3.free();
419
17.2k
  }
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
41.6k
  if (t == fontUnknownType) {
425
30.1k
    embID->num = -1;
426
30.1k
    embID->gen = -1;
427
30.1k
    t = expectedType;
428
30.1k
  }
429
430
41.6k
  if (t != expectedType) {
431
2.14k
    err = gTrue;
432
2.14k
  }
433
434
41.6k
  if (err) {
435
2.75k
    error(errSyntaxWarning, -1,
436
2.75k
    "Mismatch between font type and embedded font file");
437
2.75k
  }
438
439
41.6k
  obj2.free();
440
41.6k
  obj1.free();
441
442
41.6k
  return t;
443
41.6k
}
444
445
41.6k
void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
446
41.6k
  Object obj1, obj2, obj3, obj4;
447
41.6k
  double t, t2;
448
41.6k
  int i;
449
450
  // assume Times-Roman by default (for substitution purposes)
451
41.6k
  flags = fontSerif;
452
453
41.6k
  if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
454
455
    // get flags
456
18.3k
    if (obj1.dictLookup("Flags", &obj2)->isInt()) {
457
13.3k
      flags = obj2.getInt();
458
13.3k
    }
459
18.3k
    obj2.free();
460
461
    // get name
462
18.3k
    obj1.dictLookup("FontName", &obj2);
463
18.3k
    if (obj2.isName()) {
464
14.3k
      embFontName = new GString(obj2.getName());
465
14.3k
    }
466
18.3k
    obj2.free();
467
468
    // look for MissingWidth
469
18.3k
    obj1.dictLookup("MissingWidth", &obj2);
470
18.3k
    if (obj2.isNum()) {
471
143
      missingWidth = obj2.getNum();
472
143
    }
473
18.3k
    obj2.free();
474
475
    // font FontBBox
476
18.3k
    if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
477
52.6k
      for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
478
42.1k
  if (obj2.arrayGet(i, &obj3)->isNum()) {
479
39.6k
    fontBBox[i] = 0.001 * obj3.getNum();
480
39.6k
  }
481
42.1k
  obj3.free();
482
42.1k
      }
483
10.5k
    }
484
18.3k
    obj2.free();
485
486
    // get Ascent (but also look at CapHeight)
487
18.3k
    obj1.dictLookup("Ascent", &obj2);
488
18.3k
    obj1.dictLookup("CapHeight", &obj3);
489
18.3k
    if (obj2.isNum() || obj3.isNum()) {
490
16.4k
      if (obj2.isNum()) {
491
15.9k
  t = 0.001 * obj2.getNum();
492
  // some broken font descriptors specify a negative ascent
493
15.9k
  if (t < 0) {
494
26
    t = -t;
495
26
  }
496
15.9k
      } else {
497
505
  t = 0;
498
505
      }
499
16.4k
      if (obj3.isNum()) {
500
14.9k
  t2 = 0.001 * obj3.getNum();
501
  // some broken font descriptors specify a negative ascent
502
14.9k
  if (t2 < 0) {
503
28
    t2 = -t2;
504
28
  }
505
14.9k
      } else {
506
1.45k
  t2 = 0;
507
1.45k
      }
508
16.4k
      if (t != 0 && t < 1.9) {
509
15.4k
  declaredAscent = t;
510
15.4k
      }
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
16.4k
      if (t > 0.55 && t < 1.9) {
516
14.8k
  if (t2 > 0.55 && t2 < 1.9) {
517
13.4k
    ascent = (t < t2) ? t : t2;
518
13.4k
  } else {
519
1.42k
    ascent = t;
520
1.42k
  }
521
14.8k
      } else if (t2 > 0.55 && t2 < 1.9) {
522
158
  ascent = t2;
523
1.44k
      } else if (fontBBox[3] > 0.55 && fontBBox[3] < 1.9 &&
524
859
     fontBBox[1] < fontBBox[3]) {
525
858
  ascent = fontBBox[3];
526
858
      } else if (t > 0 && t < 1.9) {
527
61
  ascent = t;
528
528
      } else if (t2 > 0 && t2 < 1.9) {
529
26
  ascent = t2;
530
26
      }
531
16.4k
    }
532
18.3k
    obj2.free();
533
18.3k
    obj3.free();
534
535
    // get Descent
536
18.3k
    obj1.dictLookup("Descent", &obj2);
537
18.3k
    if (obj2.isNum()) {
538
15.6k
      t = 0.001 * obj2.getNum();
539
      // some broken font descriptors specify a positive descent
540
15.6k
      if (t > 0) {
541
92
  t = -t;
542
92
      }
543
      // some broken font descriptors set ascent and descent to 0
544
15.6k
      if (t < 0 && t > -1.9) {
545
14.6k
  descent = t;
546
14.6k
      } else if (fontBBox[1] > -1.9 && fontBBox[1] <= 0 &&
547
948
     fontBBox[1] < fontBBox[3]) {
548
735
  descent = fontBBox[1];
549
735
      }
550
15.6k
    }
551
18.3k
    obj2.free();
552
553
18.3k
  }
554
41.6k
  obj1.free();
555
556
  // scan font name for bold/italic tags and update the flags
557
41.6k
  if (name) {
558
28.3k
    i = name->getLength();
559
28.3k
    if (i > 2 && !strncmp(name->getCString() + i - 2, "MT", 2)) {
560
6.16k
      i -= 2;
561
6.16k
    }
562
28.3k
    if (i > 6 && !strncmp(name->getCString() + i - 6, "Italic", 6)) {
563
329
      flags |= fontItalic;
564
329
      i -= 6;
565
28.0k
    } else if (i > 2 && !strncmp(name->getCString() + i - 2, "It", 2)) {
566
7
      flags |= fontItalic;
567
7
      i -= 2;
568
28.0k
    } else if (i > 7 && !strncmp(name->getCString() + i - 7, "Oblique", 7)) {
569
38
      flags |= fontItalic;
570
38
      i -= 7;
571
38
    }
572
28.3k
    if (i > 0) {
573
28.3k
      char c = name->getChar(i-1);
574
28.3k
      if (!((c >= 'A' && c <= 'Z') ||
575
20.8k
      (c >= 'a' && c <= 'z') ||
576
3.15k
      (c >= '0' && c <= '9'))) {
577
1.26k
  --i;
578
1.26k
      }
579
28.3k
    }
580
28.3k
    if (i > 4 && !strncmp(name->getCString() + i - 4, "Bold", 4)) {
581
1.24k
      flags |= fontBold;
582
1.24k
    }
583
28.3k
  }
584
41.6k
}
585
586
CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits,
587
37.8k
                CharCodeToUnicode *ctu) {
588
37.8k
  GString *buf;
589
37.8k
  Object obj1;
590
37.8k
  char buf2[4096];
591
37.8k
  int n;
592
593
37.8k
  if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
594
24.0k
    obj1.free();
595
24.0k
    return NULL;
596
24.0k
  }
597
13.8k
  buf = new GString();
598
13.8k
  obj1.streamReset();
599
27.5k
  while ((n = obj1.streamGetBlock(buf2, sizeof(buf2))) > 0) {
600
13.7k
    buf->append(buf2, n);
601
13.7k
  }
602
13.8k
  obj1.streamClose();
603
13.8k
  obj1.free();
604
13.8k
  if (ctu) {
605
5.17k
    ctu->mergeCMap(buf, nBits);
606
8.63k
  } else {
607
8.63k
    ctu = CharCodeToUnicode::parseCMap(buf, nBits);
608
8.63k
  }
609
13.8k
  delete buf;
610
13.8k
  hasToUnicode = gTrue;
611
13.8k
  return ctu;
612
37.8k
}
613
614
49.2k
GfxFontLoc *GfxFont::locateFont(XRef *xref, GBool ps) {
615
49.2k
  GfxFontLoc *fontLoc;
616
49.2k
  SysFontType sysFontType;
617
49.2k
  FoFiIdentifierType fft;
618
49.2k
  GString *path, *base14Name, *substName;
619
49.2k
  PSFontParam16 *psFont16;
620
49.2k
  Object refObj, embFontObj;
621
49.2k
  int substIdx, fontNum;
622
49.2k
  double oblique;
623
49.2k
  GBool embed;
624
625
49.2k
  if (type == fontType3) {
626
0
    return NULL;
627
0
  }
628
629
  //----- embedded font
630
49.2k
  if (embFontID.num >= 0) {
631
6.77k
    embed = gTrue;
632
6.77k
    refObj.initRef(embFontID.num, embFontID.gen);
633
6.77k
    refObj.fetch(xref, &embFontObj);
634
6.77k
    if (!embFontObj.isStream()) {
635
0
      error(errSyntaxError, -1, "Embedded font object is wrong type");
636
0
      embed = gFalse;
637
0
    }
638
6.77k
    embFontObj.free();
639
6.77k
    refObj.free();
640
6.77k
    if (embed) {
641
6.77k
      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
6.77k
      if (embed) {
665
6.77k
  fontLoc = new GfxFontLoc();
666
6.77k
  fontLoc->locType = gfxFontLocEmbedded;
667
6.77k
  fontLoc->fontType = type;
668
6.77k
  fontLoc->embFontID = embFontID;
669
6.77k
  return fontLoc;
670
6.77k
      }
671
6.77k
    }
672
6.77k
  }
673
674
  //----- PS passthrough
675
42.5k
  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
42.5k
  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
42.5k
  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
42.5k
  if (!ps && !isCIDFont() && ((Gfx8BitFont *)this)->base14) {
701
24.3k
    base14Name = new GString(((Gfx8BitFont *)this)->base14->base14Name);
702
24.3k
    path = globalParams->findBase14FontFile(base14Name, &fontNum, &oblique);
703
24.3k
    delete base14Name;
704
24.3k
    if (path && (fontLoc = getExternalFont(path, fontNum, oblique, gFalse))) {
705
0
      return fontLoc;
706
0
    }
707
24.3k
  }
708
709
  //----- system font
710
42.5k
  if (name && (path = globalParams->findSystemFontFile(name, &sysFontType,
711
40.2k
                   &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
42.5k
  if (!isCIDFont()) {
754
755
    //----- 8-bit PS resident font
756
30.2k
    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
30.2k
    if (flags & fontFixedWidth) {
768
388
      substIdx = 0;
769
29.8k
    } else if (flags & fontSerif) {
770
29.1k
      substIdx = 8;
771
29.1k
    } else {
772
690
      substIdx = 4;
773
690
    }
774
30.2k
    if (isBold()) {
775
51
      substIdx += 2;
776
51
    }
777
30.2k
    if (isItalic()) {
778
4
      substIdx += 1;
779
4
    }
780
30.2k
    substName = new GString(base14SubstFonts[substIdx]);
781
30.2k
    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
30.2k
    } else {
791
30.2k
      path = globalParams->findBase14FontFile(substName, &fontNum, &oblique);
792
30.2k
      delete substName;
793
30.2k
      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
30.2k
    }
802
803
    // failed to find a substitute font
804
30.2k
    return NULL;
805
30.2k
  }
806
807
  //----- 16-bit PS resident font
808
12.2k
  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
12.2k
  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
12.2k
  if ((path = globalParams->findCCFontFile(
835
12.2k
        ((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
12.2k
  return NULL;
845
12.2k
}
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
4.41k
char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
912
4.41k
  char *buf;
913
4.41k
  Object obj1, obj2;
914
4.41k
  Stream *str;
915
4.41k
  int size, n;
916
917
4.41k
  obj1.initRef(embFontID.num, embFontID.gen);
918
4.41k
  obj1.fetch(xref, &obj2);
919
4.41k
  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
4.41k
  str = obj2.getStream();
927
928
4.41k
  size = 4096;
929
4.41k
  buf = (char *)gmalloc(size);
930
4.41k
  *len = 0;
931
4.41k
  str->reset();
932
10.4k
  do {
933
10.4k
    if (*len > size - 4096) {
934
4.85k
      if (size > INT_MAX / 2) {
935
0
  error(errSyntaxError, -1, "Embedded font file is too large");
936
0
  break;
937
0
      }
938
4.85k
      size *= 2;
939
4.85k
      buf = (char *)grealloc(buf, size);
940
4.85k
    }
941
10.4k
    n = str->getBlock(buf + *len, 4096);
942
10.4k
    *len += n;
943
10.4k
  } while (n == 4096);
944
4.41k
  str->close();
945
946
4.41k
  obj2.free();
947
4.41k
  obj1.free();
948
949
4.41k
  return buf;
950
4.41k
}
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
28.5k
  GfxFont(tagA, idA, nameA, typeA, embFontIDA)
959
28.5k
{
960
28.5k
  GString *name2;
961
28.5k
  BuiltinFont *builtinFont;
962
28.5k
  const char **baseEnc;
963
28.5k
  char *buf;
964
28.5k
  int len;
965
28.5k
  FoFiType1 *ffT1;
966
28.5k
  FoFiType1C *ffT1C;
967
28.5k
  int code, code2;
968
28.5k
  char *charName;
969
28.5k
  GBool missing, hex;
970
28.5k
  Unicode toUnicode[256];
971
28.5k
  CharCodeToUnicode *utu, *ctu2;
972
28.5k
  Unicode uBuf[8];
973
28.5k
  double mul;
974
28.5k
  int firstChar, lastChar;
975
28.5k
  Gushort w;
976
28.5k
  Object obj1, obj2, obj3;
977
28.5k
  int n, i, a, b, m;
978
979
28.5k
  ctu = NULL;
980
981
  // do font name substitution for various aliases of the Base 14 font
982
  // names
983
28.5k
  base14 = NULL;
984
28.5k
  if (name) {
985
21.9k
    name2 = name->copy();
986
21.9k
    i = 0;
987
322k
    while (i < name2->getLength()) {
988
300k
      if (name2->getChar(i) == ' ') {
989
1.78k
  name2->del(i);
990
298k
      } else {
991
298k
  ++i;
992
298k
      }
993
300k
    }
994
21.9k
    a = 0;
995
21.9k
    b = sizeof(base14FontMap) / sizeof(Base14FontMapEntry);
996
    // invariant: base14FontMap[a].altName <= name2 < base14FontMap[b].altName
997
154k
    while (b - a > 1) {
998
132k
      m = (a + b) / 2;
999
132k
      if (name2->cmp(base14FontMap[m].altName) >= 0) {
1000
70.4k
  a = m;
1001
70.4k
      } else {
1002
62.3k
  b = m;
1003
62.3k
      }
1004
132k
    }
1005
21.9k
    if (!name2->cmp(base14FontMap[a].altName)) {
1006
12.8k
      base14 = &base14FontMap[a];
1007
12.8k
    }
1008
21.9k
    delete name2;
1009
21.9k
  }
1010
1011
  // is it a built-in font?
1012
28.5k
  builtinFont = NULL;
1013
28.5k
  if (base14) {
1014
61.5k
    for (i = 0; i < nBuiltinFonts; ++i) {
1015
61.5k
      if (!strcmp(base14->base14Name, builtinFonts[i].name)) {
1016
12.8k
  builtinFont = &builtinFonts[i];
1017
12.8k
  break;
1018
12.8k
      }
1019
61.5k
    }
1020
12.8k
  }
1021
1022
  // default ascent/descent values
1023
28.5k
  if (builtinFont) {
1024
12.8k
    missingWidth = builtinFont->missingWidth;
1025
12.8k
    ascent = 0.001 * builtinFont->ascent;
1026
12.8k
    descent = 0.001 * builtinFont->descent;
1027
12.8k
    declaredAscent = ascent;
1028
12.8k
    fontBBox[0] = 0.001 * builtinFont->bbox[0];
1029
12.8k
    fontBBox[1] = 0.001 * builtinFont->bbox[1];
1030
12.8k
    fontBBox[2] = 0.001 * builtinFont->bbox[2];
1031
12.8k
    fontBBox[3] = 0.001 * builtinFont->bbox[3];
1032
15.7k
  } else {
1033
15.7k
    missingWidth = 0;
1034
15.7k
    ascent = 0.75;
1035
15.7k
    descent = -0.25;
1036
15.7k
    declaredAscent = ascent;
1037
15.7k
    fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
1038
15.7k
  }
1039
1040
  // get info from font descriptor
1041
28.5k
  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
28.5k
  if (builtinFont) {
1046
12.8k
    ascent = 0.001 * builtinFont->ascent;
1047
12.8k
    descent = 0.001 * builtinFont->descent;
1048
12.8k
    declaredAscent = ascent;
1049
12.8k
    fontBBox[0] = 0.001 * builtinFont->bbox[0];
1050
12.8k
    fontBBox[1] = 0.001 * builtinFont->bbox[1];
1051
12.8k
    fontBBox[2] = 0.001 * builtinFont->bbox[2];
1052
12.8k
    fontBBox[3] = 0.001 * builtinFont->bbox[3];
1053
12.8k
  }
1054
1055
  // get font matrix
1056
28.5k
  fontMat[0] = fontMat[3] = 1;
1057
28.5k
  fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
1058
28.5k
  if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
1059
9.07k
    for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
1060
6.99k
      if (obj1.arrayGet(i, &obj2)->isNum()) {
1061
4.18k
  fontMat[i] = obj2.getNum();
1062
4.18k
      }
1063
6.99k
      obj2.free();
1064
6.99k
    }
1065
2.08k
  }
1066
28.5k
  obj1.free();
1067
1068
  // get Type 3 bounding box, font definition, and resources
1069
28.5k
  if (type == fontType3) {
1070
2.43k
    if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
1071
10.3k
      for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
1072
8.26k
  if (obj1.arrayGet(i, &obj2)->isNum()) {
1073
7.80k
    fontBBox[i] = obj2.getNum();
1074
7.80k
  }
1075
8.26k
  obj2.free();
1076
8.26k
      }
1077
2.10k
    }
1078
2.43k
    obj1.free();
1079
2.43k
    if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
1080
1.62k
      error(errSyntaxError, -1,
1081
1.62k
      "Missing or invalid CharProcs dictionary in Type 3 font");
1082
1.62k
      charProcs.free();
1083
1.62k
    }
1084
2.43k
    if (!fontDict->lookup("Resources", &resources)->isDict()) {
1085
1.94k
      resources.free();
1086
1.94k
    }
1087
2.43k
  }
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
28.5k
  hasEncoding = gFalse;
1105
28.5k
  usesMacRomanEnc = gFalse;
1106
28.5k
  baseEnc = NULL;
1107
28.5k
  baseEncFromFontFile = gFalse;
1108
28.5k
  fontDict->lookup("Encoding", &obj1);
1109
28.5k
  if (obj1.isDict()) {
1110
2.62k
    obj1.dictLookup("BaseEncoding", &obj2);
1111
2.62k
    if (obj2.isName("MacRomanEncoding")) {
1112
0
      hasEncoding = gTrue;
1113
0
      usesMacRomanEnc = gTrue;
1114
0
      baseEnc = macRomanEncoding;
1115
2.62k
    } else if (obj2.isName("MacExpertEncoding")) {
1116
0
      hasEncoding = gTrue;
1117
0
      baseEnc = macExpertEncoding;
1118
2.62k
    } else if (obj2.isName("WinAnsiEncoding")) {
1119
281
      hasEncoding = gTrue;
1120
281
      baseEnc = winAnsiEncoding;
1121
281
    }
1122
2.62k
    obj2.free();
1123
25.9k
  } else if (obj1.isName("MacRomanEncoding")) {
1124
1.38k
    hasEncoding = gTrue;
1125
1.38k
    usesMacRomanEnc = gTrue;
1126
1.38k
    baseEnc = macRomanEncoding;
1127
24.5k
  } else if (obj1.isName("MacExpertEncoding")) {
1128
0
    hasEncoding = gTrue;
1129
0
    baseEnc = macExpertEncoding;
1130
24.5k
  } else if (obj1.isName("WinAnsiEncoding")) {
1131
2.33k
    hasEncoding = gTrue;
1132
2.33k
    baseEnc = winAnsiEncoding;
1133
2.33k
  }
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
28.5k
  if (builtinFont && embFontID.num < 0 &&
1140
11.7k
      (builtinFont->defaultBaseEnc == symbolEncoding ||
1141
11.7k
       builtinFont->defaultBaseEnc == zapfDingbatsEncoding) &&
1142
36
      !obj1.isDict()) {
1143
36
    baseEnc = NULL;
1144
36
  }
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
28.5k
  ffT1 = NULL;
1150
28.5k
  ffT1C = NULL;
1151
28.5k
  buf = NULL;
1152
28.5k
  if (type == fontType1 && embFontID.num >= 0) {
1153
1.78k
    if ((buf = readEmbFontFile(xref, &len))) {
1154
1.78k
      if ((ffT1 = FoFiType1::make(buf, len))) {
1155
1.78k
  if (ffT1->getName()) {
1156
503
    if (embFontName) {
1157
438
      delete embFontName;
1158
438
    }
1159
503
    embFontName = new GString(ffT1->getName());
1160
503
  }
1161
1.78k
  if (!baseEnc) {
1162
1.76k
    baseEnc = (const char **)ffT1->getEncoding();
1163
1.76k
    baseEncFromFontFile = gTrue;
1164
1.76k
  }
1165
1.78k
      }
1166
1.78k
      gfree(buf);
1167
1.78k
    }
1168
26.8k
  } else if (type == fontType1C && embFontID.num >= 0) {
1169
2.63k
    if ((buf = readEmbFontFile(xref, &len))) {
1170
2.63k
      if ((ffT1C = FoFiType1C::make(buf, len))) {
1171
1.64k
  if (ffT1C->getName()) {
1172
1.64k
    if (embFontName) {
1173
1.09k
      delete embFontName;
1174
1.09k
    }
1175
1.64k
    embFontName = new GString(ffT1C->getName());
1176
1.64k
  }
1177
1.64k
  if (!baseEnc) {
1178
781
    baseEnc = (const char **)ffT1C->getEncoding();
1179
781
    baseEncFromFontFile = gTrue;
1180
781
  }
1181
1.64k
      }
1182
2.63k
      gfree(buf);
1183
2.63k
    }
1184
2.63k
  }
1185
1186
  // get default base encoding
1187
28.5k
  if (!baseEnc) {
1188
23.2k
    if (builtinFont && embFontID.num < 0) {
1189
10.7k
      baseEnc = builtinFont->defaultBaseEnc;
1190
10.7k
      hasEncoding = gTrue;
1191
12.4k
    } else if (type == fontTrueType) {
1192
3.17k
      baseEnc = winAnsiEncoding;
1193
9.24k
    } else {
1194
9.24k
      baseEnc = standardEncoding;
1195
9.24k
    }
1196
23.2k
  }
1197
1198
  // copy the base encoding
1199
7.34M
  for (i = 0; i < 256; ++i) {
1200
7.31M
    enc[i] = (char *)baseEnc[i];
1201
7.31M
    if ((encFree[i] = (char)baseEncFromFontFile) && enc[i]) {
1202
235k
      enc[i] = copyString(baseEnc[i]);
1203
235k
    }
1204
7.31M
  }
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
28.5k
  if (type == fontType1C && embFontID.num >= 0 && baseEncFromFontFile) {
1211
200k
    for (i = 0; i < 256; ++i) {
1212
199k
      if (!enc[i] && standardEncoding[i]) {
1213
79.3k
  enc[i] = (char *)standardEncoding[i];
1214
79.3k
  encFree[i] = gFalse;
1215
79.3k
      }
1216
199k
    }
1217
781
  }
1218
1219
  // merge differences into encoding
1220
28.5k
  if (obj1.isDict()) {
1221
2.62k
    obj1.dictLookup("Differences", &obj2);
1222
2.62k
    if (obj2.isArray()) {
1223
2.30k
      hasEncoding = gTrue;
1224
2.30k
      code = 0;
1225
253k
      for (i = 0; i < obj2.arrayGetLength(); ++i) {
1226
250k
  obj2.arrayGet(i, &obj3);
1227
250k
  if (obj3.isInt()) {
1228
43.4k
    code = obj3.getInt();
1229
207k
  } else if (obj3.isName()) {
1230
132k
    if (code >= 0 && code < 256) {
1231
131k
      if (encFree[code]) {
1232
25.4k
        gfree(enc[code]);
1233
25.4k
      }
1234
131k
      enc[code] = copyString(obj3.getName());
1235
131k
      encFree[code] = gTrue;
1236
131k
    }
1237
132k
    ++code;
1238
132k
  } else {
1239
74.5k
    error(errSyntaxError, -1,
1240
74.5k
    "Wrong type in font encoding resource differences ({0:s})",
1241
74.5k
    obj3.getTypeName());
1242
74.5k
  }
1243
250k
  obj3.free();
1244
250k
      }
1245
2.30k
    }
1246
2.62k
    obj2.free();
1247
2.62k
  }
1248
28.5k
  obj1.free();
1249
28.5k
  if (ffT1) {
1250
1.78k
    delete ffT1;
1251
1.78k
  }
1252
28.5k
  if (ffT1C) {
1253
1.64k
    delete ffT1C;
1254
1.64k
  }
1255
1256
  //----- build the mapping to Unicode -----
1257
1258
  // pass 1: use the name-to-Unicode mapping table
1259
28.5k
  missing = hex = gFalse;
1260
7.34M
  for (code = 0; code < 256; ++code) {
1261
7.31M
    if ((charName = enc[code])) {
1262
4.76M
      if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
1263
52.8k
    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
43.9k
  if ((strlen(charName) == 3 &&
1268
14.4k
       isalpha(charName[0] & 0xff) &&
1269
14.3k
       isxdigit(charName[1] & 0xff) && isxdigit(charName[2] & 0xff) &&
1270
12.1k
       ((charName[1] >= 'a' && charName[1] <= 'f') ||
1271
12.0k
        (charName[1] >= 'A' && charName[1] <= 'F') ||
1272
11.9k
        (charName[2] >= 'a' && charName[2] <= 'f') ||
1273
11.3k
        (charName[2] >= 'A' && charName[2] <= 'F'))) ||
1274
43.1k
      (strlen(charName) == 2 &&
1275
4.66k
       isxdigit(charName[0] & 0xff) && isxdigit(charName[1] & 0xff) &&
1276
1.61k
       ((charName[0] >= 'a' && charName[0] <= 'f') ||
1277
1.38k
        (charName[0] >= 'A' && charName[0] <= 'F') ||
1278
184
        (charName[1] >= 'a' && charName[1] <= 'f') ||
1279
2.35k
        (charName[1] >= 'A' && charName[1] <= 'F')))) {
1280
2.35k
    hex = gTrue;
1281
2.35k
  }
1282
43.9k
  missing = gTrue;
1283
43.9k
      }
1284
4.76M
    } else {
1285
2.55M
      toUnicode[code] = 0;
1286
2.55M
    }
1287
7.31M
  }
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
28.5k
  usedNumericHeuristic = gFalse;
1301
28.5k
  if (missing && globalParams->getMapNumericCharNames()) {
1302
386k
    for (code = 0; code < 256; ++code) {
1303
385k
      if ((charName = enc[code]) && !toUnicode[code] &&
1304
47.6k
    strcmp(charName, ".notdef")) {
1305
43.9k
  n = (int)strlen(charName);
1306
43.9k
  code2 = -1;
1307
43.9k
  if (hex && n == 3 && isalpha(charName[0] & 0xff) &&
1308
5.69k
      isxdigit(charName[1] & 0xff) && isxdigit(charName[2] & 0xff)) {
1309
5.37k
    sscanf(charName+1, "%x", &code2);
1310
38.5k
  } else if (hex && n == 2 &&
1311
2.40k
       isxdigit(charName[0] & 0xff) &&
1312
2.24k
       isxdigit(charName[1] & 0xff)) {
1313
1.60k
    sscanf(charName, "%x", &code2);
1314
36.9k
  } else if (!hex && n >= 2 && n <= 4 &&
1315
25.0k
       isdigit(charName[0] & 0xff) && isdigit(charName[1] & 0xff)) {
1316
23
    code2 = atoi(charName);
1317
36.9k
  } else if (n >= 3 && n <= 5 &&
1318
26.1k
       isdigit(charName[1] & 0xff) && isdigit(charName[2] & 0xff)) {
1319
22.7k
    code2 = atoi(charName+1);
1320
22.7k
  } else if (n >= 4 && n <= 6 &&
1321
1.55k
       isdigit(charName[2] & 0xff) && isdigit(charName[3] & 0xff)) {
1322
66
    code2 = atoi(charName+2);
1323
14.0k
  } else if (n >= 7 && charName[0] == 'u' && charName[1] == 'n' &&
1324
69
       charName[2] == 'i' &&
1325
63
       isxdigit(charName[3] & 0xff) &&
1326
49
       isxdigit(charName[4] & 0xff) &&
1327
48
       isxdigit(charName[5] & 0xff) &&
1328
47
       isxdigit(charName[6] & 0xff)) {
1329
46
    sscanf(charName + 3, "%x", &code2);
1330
46
  }
1331
43.9k
  if (code2 >= 0 && code2 <= 0xffff) {
1332
29.8k
    toUnicode[code] = (Unicode)code2;
1333
29.8k
    usedNumericHeuristic = gTrue;
1334
29.8k
  }
1335
43.9k
      }
1336
385k
    }
1337
1338
  // if the 'mapUnknownCharNames' flag is set, do a simple pass-through
1339
  // mapping for unknown character names
1340
27.0k
  } 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
28.5k
  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
28.5k
  readToUnicodeCMap(fontDict, 8, ctu);
1356
1357
  // look for a Unicode-to-Unicode mapping
1358
28.5k
  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
7.34M
  for (code = 0; code < 256; ++code) {
1381
7.31M
    widths[code] = missingWidth * 0.001;
1382
7.31M
  }
1383
1384
  // use widths from font dict, if present
1385
28.5k
  fontDict->lookup("FirstChar", &obj1);
1386
28.5k
  firstChar = obj1.isInt() ? obj1.getInt() : 0;
1387
28.5k
  obj1.free();
1388
28.5k
  if (firstChar < 0 || firstChar > 255) {
1389
25
    firstChar = 0;
1390
25
  }
1391
28.5k
  fontDict->lookup("LastChar", &obj1);
1392
28.5k
  lastChar = obj1.isInt() ? obj1.getInt() : 255;
1393
28.5k
  obj1.free();
1394
28.5k
  if (lastChar < 0 || lastChar > 255) {
1395
1
    lastChar = 255;
1396
1
  }
1397
28.5k
  mul = (type == fontType3) ? fontMat[0] : 0.001;
1398
28.5k
  fontDict->lookup("Widths", &obj1);
1399
28.5k
  if (obj1.isArray()) {
1400
9.94k
    flags |= fontFixedWidth;
1401
9.94k
    if (obj1.arrayGetLength() < lastChar - firstChar + 1) {
1402
3.91k
      lastChar = firstChar + obj1.arrayGetLength() - 1;
1403
3.91k
    }
1404
553k
    for (code = firstChar; code <= lastChar; ++code) {
1405
543k
      obj1.arrayGet(code - firstChar, &obj2);
1406
543k
      if (obj2.isNum()) {
1407
520k
  widths[code] = obj2.getNum() * mul;
1408
520k
  if (fabs(widths[code] - widths[firstChar]) > 0.00001) {
1409
471k
    flags &= ~fontFixedWidth;
1410
471k
  }
1411
520k
      }
1412
543k
      obj2.free();
1413
543k
    }
1414
1415
  // use widths from built-in font
1416
18.6k
  } else if (builtinFont) {
1417
    // this is a kludge for broken PDF files that encode char 32
1418
    // as .notdef
1419
12.8k
    if (builtinFont->widths->getWidth("space", &w)) {
1420
12.8k
      widths[32] = 0.001 * w;
1421
12.8k
    }
1422
3.29M
    for (code = 0; code < 256; ++code) {
1423
3.27M
      if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
1424
2.05M
  widths[code] = 0.001 * w;
1425
2.05M
      }
1426
3.27M
    }
1427
1428
  // couldn't find widths -- use defaults 
1429
12.8k
  } 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
5.84k
    if (isFixedWidth()) {
1434
6
      i = 0;
1435
5.83k
    } else if (isSerif()) {
1436
5.15k
      i = 8;
1437
5.15k
    } else {
1438
685
      i = 4;
1439
685
    }
1440
5.84k
    if (isBold()) {
1441
181
      i += 2;
1442
181
    }
1443
5.84k
    if (isItalic()) {
1444
47
      i += 1;
1445
47
    }
1446
5.84k
    builtinFont = builtinFontSubst[i];
1447
    // this is a kludge for broken PDF files that encode char 32
1448
    // as .notdef
1449
5.84k
    if (builtinFont->widths->getWidth("space", &w)) {
1450
5.84k
      widths[32] = 0.001 * w;
1451
5.84k
    }
1452
1.50M
    for (code = 0; code < 256; ++code) {
1453
1.49M
      if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
1454
899k
  widths[code] = 0.001 * w;
1455
899k
      }
1456
1.49M
    }
1457
5.84k
  }
1458
28.5k
  obj1.free();
1459
1460
28.5k
  ok = gTrue;
1461
28.5k
}
1462
1463
28.5k
Gfx8BitFont::~Gfx8BitFont() {
1464
28.5k
  int i;
1465
1466
7.34M
  for (i = 0; i < 256; ++i) {
1467
7.31M
    if (encFree[i] && enc[i]) {
1468
351k
      gfree(enc[i]);
1469
351k
    }
1470
7.31M
  }
1471
28.5k
  ctu->decRefCnt();
1472
28.5k
  if (charProcs.isDict()) {
1473
814
    charProcs.free();
1474
814
  }
1475
28.5k
  if (resources.isDict()) {
1476
489
    resources.free();
1477
489
  }
1478
28.5k
}
1479
1480
int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
1481
           Unicode *u, int uSize, int *uLen,
1482
7.36M
           double *dx, double *dy, double *ox, double *oy) {
1483
7.36M
  CharCode c;
1484
1485
7.36M
  *code = c = (CharCode)(*s & 0xff);
1486
7.36M
  *uLen = ctu->mapToUnicode(c, u, uSize);
1487
7.36M
  *dx = widths[c];
1488
7.36M
  *dy = *ox = *oy = 0;
1489
7.36M
  return 1;
1490
7.36M
}
1491
1492
0
CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
1493
0
  ctu->incRefCnt();
1494
0
  return ctu;
1495
0
}
1496
1497
2.07k
int *Gfx8BitFont::getCodeToGIDMap(FoFiTrueType *ff) {
1498
2.07k
  int *map;
1499
2.07k
  int cmapPlatform, cmapEncoding;
1500
2.07k
  int unicodeCmap, macRomanCmap, macUnicodeCmap, msSymbolCmap, cmap;
1501
2.07k
  GBool nonsymbolic, useMacRoman, useUnicode;
1502
2.07k
  char *charName;
1503
2.07k
  Unicode u;
1504
2.07k
  int code, i, n;
1505
1506
2.07k
  map = (int *)gmallocn(256, sizeof(int));
1507
533k
  for (i = 0; i < 256; ++i) {
1508
531k
    map[i] = 0;
1509
531k
  }
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
2.07k
  unicodeCmap = macRomanCmap = macUnicodeCmap = msSymbolCmap = -1;
1515
4.19k
  for (i = 0; i < ff->getNumCmaps(); ++i) {
1516
2.12k
    cmapPlatform = ff->getCmapPlatform(i);
1517
2.12k
    cmapEncoding = ff->getCmapEncoding(i);
1518
2.12k
    if (cmapPlatform == 3 && cmapEncoding == 1) {
1519
5
      unicodeCmap = i;
1520
2.11k
    } else if (cmapPlatform == 1 && cmapEncoding == 0) {
1521
1.09k
      macRomanCmap = i;
1522
1.09k
    } else if (cmapPlatform == 0 && cmapEncoding <= 4) {
1523
182
      macUnicodeCmap = i;
1524
842
    } else if (cmapPlatform == 3 && cmapEncoding == 0) {
1525
10
      msSymbolCmap = i;
1526
10
    }
1527
2.12k
  }
1528
2.07k
  useMacRoman = gFalse;
1529
2.07k
  useUnicode = gFalse;
1530
2.07k
  nonsymbolic = !(flags & fontSymbolic);
1531
2.07k
  if (embFontID.num < 0 && hasEncoding && unicodeCmap >= 0) { 
1532
0
    cmap = unicodeCmap;
1533
0
    useUnicode = gTrue;
1534
2.07k
  } else if (!nonsymbolic && msSymbolCmap >= 0) {
1535
10
    cmap = msSymbolCmap;
1536
2.06k
  } else if (unicodeCmap >= 0) {
1537
2
    cmap = unicodeCmap;
1538
2
    useUnicode = nonsymbolic;
1539
2.06k
  } 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
2.06k
  } else if (macUnicodeCmap >= 0) {
1545
45
    cmap = macUnicodeCmap;
1546
45
    useUnicode = nonsymbolic;
1547
2.01k
  } else if (macRomanCmap >= 0) {
1548
1.08k
    cmap = macRomanCmap;
1549
1.08k
    useMacRoman = nonsymbolic;
1550
1.08k
  } else {
1551
931
    cmap = 0;
1552
931
  }
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
2.07k
  if (useMacRoman) {
1558
106k
    for (i = 0; i < 256; ++i) {
1559
105k
      if ((charName = enc[i])) {
1560
92.7k
  if ((code = globalParams->getMacRomanCharCode(charName))) {
1561
84.8k
    map[i] = ff->mapCodeToGID(cmap, code);
1562
84.8k
  } else if (unicodeCmap >= 0 &&
1563
0
       (u = globalParams->mapNameToUnicode(charName))) {
1564
0
    map[i] = ff->mapCodeToGID(unicodeCmap, u);
1565
0
  }
1566
92.7k
      } else if (unicodeCmap >= 0 &&
1567
0
     (n = ctu->mapToUnicode((CharCode)i, &u, 1))) {
1568
0
  map[i] = ff->mapCodeToGID(cmap, u);
1569
13.2k
      } else {
1570
13.2k
  map[i] = -1;
1571
13.2k
      }
1572
105k
    }
1573
1574
  // map Unicode through the cmap
1575
1.66k
  } else if (useUnicode) {
1576
7.71k
    for (i = 0; i < 256; ++i) {
1577
7.68k
      if (((charName = enc[i]) &&
1578
6.72k
     (u = globalParams->mapNameToUnicode(charName))) ||
1579
6.81k
    (n = ctu->mapToUnicode((CharCode)i, &u, 1))) {
1580
6.81k
  map[i] = ff->mapCodeToGID(cmap, u);
1581
6.81k
      } else {
1582
862
  map[i] = -1;
1583
862
      }
1584
7.68k
    }
1585
1586
  // map the char codes through the cmap, possibly with an offset of
1587
  // 0xf000
1588
1.63k
  } else {
1589
419k
    for (i = 0; i < 256; ++i) {
1590
417k
      if (!(map[i] = ff->mapCodeToGID(cmap, i))) {
1591
333k
  map[i] = ff->mapCodeToGID(cmap, 0xf000 + i);
1592
333k
      }
1593
417k
    }
1594
1.63k
  }
1595
1596
  // try the TrueType 'post' table to handle any unmapped characters
1597
533k
  for (i = 0; i < 256; ++i) {
1598
531k
    if (map[i] <= 0 && (charName = enc[i])) {
1599
376k
      map[i] = ff->mapNameToGID(charName);
1600
376k
    }
1601
531k
  }
1602
1603
2.07k
  return map;
1604
2.07k
}
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
294k
    map[i] = 0;
1614
294k
  }
1615
1616
1.14k
  nameToGID = ff->getNameToGIDMap();
1617
295k
  for (i = 0; i < 256; ++i) {
1618
294k
    if (!enc[i]) {
1619
59.0k
      continue;
1620
59.0k
    }
1621
235k
    gid = nameToGID->lookupInt(enc[i]);
1622
235k
    if (gid < 0 || gid >= 65536) {
1623
0
      continue;
1624
0
    }
1625
235k
    map[i] = gid;
1626
235k
  }
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
2.02M
Object *Gfx8BitFont::getCharProcNF(int code, Object *proc) {
1647
2.02M
  if (enc[code] && charProcs.isDict()) {
1648
1.17M
    charProcs.dictLookupNF(enc[code], proc);
1649
1.17M
  } else {
1650
851k
    proc->initNull();
1651
851k
  }
1652
2.02M
  return proc;
1653
2.02M
}
1654
1655
2.02M
Dict *Gfx8BitFont::getResources() {
1656
2.02M
  return resources.isDict() ? resources.getDict() : (Dict *)NULL;
1657
2.02M
}
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
13.0k
  GfxFont(tagA, idA, nameA, typeA, embFontIDA)
1729
13.0k
{
1730
13.0k
  Dict *desFontDict;
1731
13.0k
  Object desFontDictObj;
1732
13.0k
  Object obj1, obj2, obj3, obj4, obj5, obj6;
1733
13.0k
  CharCodeToUnicode *utu;
1734
13.0k
  CharCode c;
1735
13.0k
  Unicode uBuf[8];
1736
13.0k
  int c1, c2;
1737
13.0k
  int excepsSize, i, j, k, n;
1738
1739
13.0k
  missingWidth = 0;
1740
13.0k
  ascent = 0.95;
1741
13.0k
  descent = -0.35;
1742
13.0k
  declaredAscent = ascent;
1743
13.0k
  fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
1744
13.0k
  collection = NULL;
1745
13.0k
  cMap = NULL;
1746
13.0k
  ctu = NULL;
1747
13.0k
  ctuUsesCharCode = gTrue;
1748
13.0k
  widths.defWidth = 1.0;
1749
13.0k
  widths.defHeight = -1.0;
1750
13.0k
  widths.defVY = 0.880;
1751
13.0k
  widths.exceps = NULL;
1752
13.0k
  widths.nExceps = 0;
1753
13.0k
  widths.excepsV = NULL;
1754
13.0k
  widths.nExcepsV = 0;
1755
13.0k
  cidToGID = NULL;
1756
13.0k
  cidToGIDLen = 0;
1757
1758
  // get the descendant font
1759
13.0k
  if (!fontDict->lookup("DescendantFonts", &obj1)->isArray() ||
1760
13.0k
      obj1.arrayGetLength() == 0) {
1761
21
    error(errSyntaxError, -1,
1762
21
    "Missing or empty DescendantFonts entry in Type 0 font");
1763
21
    obj1.free();
1764
21
    goto err1;
1765
21
  }
1766
13.0k
  if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
1767
7
    error(errSyntaxError, -1, "Bad descendant font in Type 0 font");
1768
7
    goto err2;
1769
7
  }
1770
13.0k
  obj1.free();
1771
13.0k
  desFontDict = desFontDictObj.getDict();
1772
1773
  // get info from font descriptor
1774
13.0k
  readFontDescriptor(xref, desFontDict);
1775
1776
  //----- encoding info -----
1777
1778
  // char collection
1779
13.0k
  if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
1780
1.51k
    error(errSyntaxError, -1,
1781
1.51k
    "Missing CIDSystemInfo dictionary in Type 0 descendant font");
1782
1.51k
    goto err2;
1783
1.51k
  }
1784
11.5k
  obj1.dictLookup("Registry", &obj2);
1785
11.5k
  obj1.dictLookup("Ordering", &obj3);
1786
11.5k
  if (!obj2.isString() || !obj3.isString()) {
1787
234
    error(errSyntaxError, -1,
1788
234
    "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
1789
234
    goto err3;
1790
234
  }
1791
11.2k
  collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
1792
11.2k
  obj3.free();
1793
11.2k
  obj2.free();
1794
11.2k
  obj1.free();
1795
1796
  // encoding (i.e., CMap)
1797
11.2k
  if (fontDict->lookup("Encoding", &obj1)->isNull()) {
1798
213
    error(errSyntaxError, -1, "Missing Encoding entry in Type 0 font");
1799
213
    goto err2;
1800
213
  }
1801
11.0k
  if (!(cMap = CMap::parse(NULL, collection, &obj1))) {
1802
1.76k
    goto err2;
1803
1.76k
  }
1804
1805
  // check for fonts that use the Identity-H encoding (cmap), and the
1806
  // Adobe-Identity character collection
1807
9.29k
  identityEnc = obj1.isName("Identity-H") &&
1808
5.06k
                !collection->cmp("Adobe-Identity");
1809
1810
9.29k
  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
9.29k
  hasIdentityCIDToGID = gFalse;
1817
9.29k
  desFontDict->lookup("CIDToGIDMap", &obj1);
1818
9.29k
  if (obj1.isStream()) {
1819
57
    cidToGIDLen = 0;
1820
57
    i = 64;
1821
57
    cidToGID = (int *)gmallocn(i, sizeof(int));
1822
57
    obj1.streamReset();
1823
276k
    while ((c1 = obj1.streamGetChar()) != EOF &&
1824
276k
     (c2 = obj1.streamGetChar()) != EOF) {
1825
276k
      if (cidToGIDLen == i) {
1826
161
  i *= 2;
1827
161
  cidToGID = (int *)greallocn(cidToGID, i, sizeof(int));
1828
161
      }
1829
276k
      cidToGID[cidToGIDLen++] = (c1 << 8) + c2;
1830
276k
    }
1831
57
    obj1.streamClose();
1832
57
    identityEnc = gFalse;
1833
9.23k
  } else if (obj1.isName("Identity")) {
1834
4.02k
    hasIdentityCIDToGID = gTrue;
1835
5.21k
  } else if (!obj1.isNull()) {
1836
832
    error(errSyntaxError, -1, "Invalid CIDToGIDMap entry in CID font");
1837
832
  }
1838
9.29k
  obj1.free();
1839
1840
  // look for a ToUnicode CMap
1841
9.29k
  hasKnownCollection = gFalse;
1842
9.29k
  if (globalParams->getUseTrueTypeUnicodeMapping()) {
1843
0
    readTrueTypeUnicodeMapping(xref);
1844
0
  }
1845
9.29k
  if (!ctu) {
1846
9.29k
    ctu = readToUnicodeCMap(fontDict, 16, NULL);
1847
9.29k
  }
1848
9.29k
  if (!ctu) {
1849
6.84k
    ctuUsesCharCode = gFalse;
1850
1851
    // use an identity mapping for the "Adobe-Identity" and
1852
    // "Adobe-UCS" collections
1853
6.84k
    if (!collection->cmp("Adobe-Identity") ||
1854
4.93k
  !collection->cmp("Adobe-UCS")) {
1855
1.90k
      ctu = CharCodeToUnicode::makeIdentityMapping();
1856
1857
    // look for a user-supplied .cidToUnicode file
1858
4.93k
    } else if ((ctu = globalParams->getCIDToUnicode(collection))) {
1859
0
      hasKnownCollection = gTrue;
1860
1861
4.93k
    } else {
1862
4.93k
      error(errSyntaxError, -1,
1863
4.93k
      "Unknown character collection '{0:t}'", collection);
1864
1865
      // fall back to an identity mapping
1866
4.93k
      ctu = CharCodeToUnicode::makeIdentityMapping();
1867
4.93k
    }
1868
6.84k
  }
1869
1870
  // look for a Unicode-to-Unicode mapping
1871
9.29k
  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
9.29k
  if (desFontDict->lookup("DW", &obj1)->isNum()) {
1897
985
    widths.defWidth = obj1.getNum() * 0.001;
1898
985
  }
1899
9.29k
  obj1.free();
1900
1901
  // char width exceptions
1902
9.29k
  if (desFontDict->lookup("W", &obj1)->isArray()) {
1903
9.05k
    excepsSize = 0;
1904
9.05k
    i = 0;
1905
51.2k
    while (i + 1 < obj1.arrayGetLength()) {
1906
42.2k
      obj1.arrayGet(i, &obj2);
1907
42.2k
      obj1.arrayGet(i + 1, &obj3);
1908
42.2k
      if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
1909
11.3k
  if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
1910
8.52k
    if (widths.nExceps == excepsSize) {
1911
18
      excepsSize += 16;
1912
18
      widths.exceps = (GfxFontCIDWidthExcep *)
1913
18
        greallocn(widths.exceps,
1914
18
      excepsSize, sizeof(GfxFontCIDWidthExcep));
1915
18
    }
1916
8.52k
    widths.exceps[widths.nExceps].first = obj2.getInt();
1917
8.52k
    widths.exceps[widths.nExceps].last = obj3.getInt();
1918
8.52k
    widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1919
8.52k
    ++widths.nExceps;
1920
8.52k
  } else {
1921
2.79k
    error(errSyntaxError, -1, "Bad widths array in Type 0 font");
1922
2.79k
  }
1923
11.3k
  obj4.free();
1924
11.3k
  i += 3;
1925
30.9k
      } else if (obj2.isInt() && obj3.isArray()) {
1926
17.3k
  if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1927
8.81k
    excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1928
8.81k
    widths.exceps = (GfxFontCIDWidthExcep *)
1929
8.81k
      greallocn(widths.exceps,
1930
8.81k
          excepsSize, sizeof(GfxFontCIDWidthExcep));
1931
8.81k
  }
1932
17.3k
  j = obj2.getInt();
1933
47.5k
  for (k = 0; k < obj3.arrayGetLength(); ++k) {
1934
30.2k
    if (obj3.arrayGet(k, &obj4)->isNum()) {
1935
29.2k
      widths.exceps[widths.nExceps].first = j;
1936
29.2k
      widths.exceps[widths.nExceps].last = j;
1937
29.2k
      widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1938
29.2k
      ++j;
1939
29.2k
      ++widths.nExceps;
1940
29.2k
    } else {
1941
959
      error(errSyntaxError, -1, "Bad widths array in Type 0 font");
1942
959
    }
1943
30.2k
    obj4.free();
1944
30.2k
  }
1945
17.3k
  i += 2;
1946
17.3k
      } else {
1947
13.5k
  error(errSyntaxError, -1, "Bad widths array in Type 0 font");
1948
13.5k
  ++i;
1949
13.5k
      }
1950
42.2k
      obj3.free();
1951
42.2k
      obj2.free();
1952
42.2k
    }
1953
9.05k
  }
1954
9.29k
  obj1.free();
1955
1956
  // default metrics for vertical font
1957
9.29k
  if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1958
8
      obj1.arrayGetLength() == 2) {
1959
7
    if (obj1.arrayGet(0, &obj2)->isNum()) {
1960
7
      widths.defVY = obj2.getNum() * 0.001;
1961
7
    }
1962
7
    obj2.free();
1963
7
    if (obj1.arrayGet(1, &obj2)->isNum()) {
1964
4
      widths.defHeight = obj2.getNum() * 0.001;
1965
4
    }
1966
7
    obj2.free();
1967
7
  }
1968
9.29k
  obj1.free();
1969
1970
  // char metric exceptions for vertical font
1971
9.29k
  if (desFontDict->lookup("W2", &obj1)->isArray()) {
1972
38
    excepsSize = 0;
1973
38
    i = 0;
1974
351
    while (i + 1 < obj1.arrayGetLength()) {
1975
313
      obj1.arrayGet(i, &obj2);
1976
313
      obj1.arrayGet(i+ 1, &obj3);
1977
313
      if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1978
61
  if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1979
48
      obj1.arrayGet(i + 3, &obj5)->isNum() &&
1980
34
      obj1.arrayGet(i + 4, &obj6)->isNum()) {
1981
24
    if (widths.nExcepsV == excepsSize) {
1982
16
      excepsSize += 16;
1983
16
      widths.excepsV = (GfxFontCIDWidthExcepV *)
1984
16
        greallocn(widths.excepsV,
1985
16
      excepsSize, sizeof(GfxFontCIDWidthExcepV));
1986
16
    }
1987
24
    widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1988
24
    widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1989
24
    widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1990
24
    widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1991
24
    widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1992
24
    ++widths.nExcepsV;
1993
37
  } else {
1994
37
    error(errSyntaxError, -1, "Bad widths (W2) array in Type 0 font");
1995
37
  }
1996
61
  obj6.free();
1997
61
  obj5.free();
1998
61
  obj4.free();
1999
61
  i += 5;
2000
252
      } else if (obj2.isInt() && obj3.isArray()) {
2001
13
  if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
2002
10
    excepsSize =
2003
10
      (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
2004
10
    widths.excepsV = (GfxFontCIDWidthExcepV *)
2005
10
      greallocn(widths.excepsV,
2006
10
          excepsSize, sizeof(GfxFontCIDWidthExcepV));
2007
10
  }
2008
13
  j = obj2.getInt();
2009
47
  for (k = 0; k + 2 < obj3.arrayGetLength(); k += 3) {
2010
34
    if (obj3.arrayGet(k, &obj4)->isNum() &&
2011
23
        obj3.arrayGet(k+1, &obj5)->isNum() &&
2012
14
        obj3.arrayGet(k+2, &obj6)->isNum()) {
2013
8
      widths.excepsV[widths.nExcepsV].first = j;
2014
8
      widths.excepsV[widths.nExcepsV].last = j;
2015
8
      widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
2016
8
      widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
2017
8
      widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
2018
8
      ++j;
2019
8
      ++widths.nExcepsV;
2020
26
    } else {
2021
26
      error(errSyntaxError, -1, "Bad widths (W2) array in Type 0 font");
2022
26
    }
2023
34
    obj6.free();
2024
34
    obj5.free();
2025
34
    obj4.free();
2026
34
  }
2027
13
  i += 2;
2028
239
      } else {
2029
239
  error(errSyntaxError, -1, "Bad widths (W2) array in Type 0 font");
2030
239
  ++i;
2031
239
      }
2032
313
      obj3.free();
2033
313
      obj2.free();
2034
313
    }
2035
38
  }
2036
9.29k
  obj1.free();
2037
2038
9.29k
  desFontDictObj.free();
2039
9.29k
  ok = gTrue;
2040
9.29k
  return;
2041
2042
234
 err3:
2043
234
  obj3.free();
2044
234
  obj2.free();
2045
3.73k
 err2:
2046
3.73k
  obj1.free();
2047
3.73k
  desFontDictObj.free();
2048
3.75k
 err1:
2049
3.75k
  error(errSyntaxError, -1, "Failed to parse font object for '{0:t}'", name);
2050
3.75k
}
2051
2052
13.0k
GfxCIDFont::~GfxCIDFont() {
2053
13.0k
  if (collection) {
2054
11.2k
    delete collection;
2055
11.2k
  }
2056
13.0k
  if (cMap) {
2057
9.29k
    cMap->decRefCnt();
2058
9.29k
  }
2059
13.0k
  if (ctu) {
2060
9.29k
    ctu->decRefCnt();
2061
9.29k
  }
2062
13.0k
  gfree(widths.exceps);
2063
13.0k
  gfree(widths.excepsV);
2064
13.0k
  if (cidToGID) {
2065
57
    gfree(cidToGID);
2066
57
  }
2067
13.0k
}
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
179k
          double *dx, double *dy, double *ox, double *oy) {
2157
179k
  CID cid;
2158
179k
  CharCode c;
2159
179k
  int n;
2160
2161
179k
  if (!cMap) {
2162
0
    *code = 0;
2163
0
    *uLen = 0;
2164
0
    *dx = *dy = 0;
2165
0
    return 1;
2166
0
  }
2167
2168
179k
  *code = (CharCode)(cid = cMap->getCID(s, len, &c, &n));
2169
179k
  if (ctu) {
2170
179k
    *uLen = ctu->mapToUnicode(ctuUsesCharCode ? c : cid, u, uSize);
2171
179k
  } else {
2172
0
    *uLen = 0;
2173
0
  }
2174
179k
  if (!*uLen && uSize >= 1 && globalParams->getMapUnknownCharNames()) {
2175
0
    u[0] = *code;
2176
0
    *uLen = 1;
2177
0
  }
2178
2179
  // horizontal
2180
179k
  if (cMap->getWMode() == 0) {
2181
178k
    getHorizontalMetrics(cid, dx);
2182
178k
    *dy = *ox = *oy = 0;
2183
2184
  // vertical
2185
178k
  } else {
2186
967
    getVerticalMetrics(cid, dy, ox, oy);
2187
967
    *dx = 0;
2188
967
  }
2189
2190
179k
  return n;
2191
179k
}
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
179k
void GfxCIDFont::getHorizontalMetrics(CID cid, double *w) {
2198
179k
  int i;
2199
968k
  for (i = 0; i < widths.nExceps; ++i) {
2200
808k
    if (widths.exceps[i].first <= cid && cid <= widths.exceps[i].last) {
2201
18.7k
      *w = widths.exceps[i].width;
2202
18.7k
      return;
2203
18.7k
    }
2204
808k
  }
2205
160k
  *w = widths.defWidth;
2206
160k
}
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
967
            double *vx, double *vy) {
2214
967
  int i;
2215
1.02k
  for (i = 0; i < widths.nExcepsV; ++i) {
2216
118
    if (widths.excepsV[i].first <= cid && cid <= widths.excepsV[i].last) {
2217
57
      *h = widths.excepsV[i].height;
2218
57
      *vx = widths.excepsV[i].vx;
2219
57
      *vy = widths.excepsV[i].vy;
2220
57
      return;
2221
57
    }
2222
118
  }
2223
910
  *h = widths.defHeight;
2224
910
  getHorizontalMetrics(cid, vx);
2225
910
  *vx /= 2;
2226
910
  *vy = widths.defVY;
2227
910
}
2228
2229
70.9k
int GfxCIDFont::getWMode() {
2230
70.9k
  return cMap ? cMap->getWMode() : 0;
2231
70.9k
}
2232
2233
0
CharCodeToUnicode *GfxCIDFont::getToUnicode() {
2234
0
  if (ctu) {
2235
0
    ctu->incRefCnt();
2236
0
  }
2237
0
  return ctu;
2238
0
}
2239
2240
12.2k
GString *GfxCIDFont::getCollection() {
2241
12.2k
  return cMap ? cMap->getCollection() : (GString *)NULL;
2242
12.2k
}
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
111k
GfxFontDictEntry::GfxFontDictEntry(Ref refA, Object *fontObjA) {
2411
111k
  loaded = gFalse;
2412
111k
  ref = refA;
2413
111k
  fontObjA->copy(&fontObj);
2414
111k
  font = NULL;
2415
111k
}
2416
2417
111k
GfxFontDictEntry::~GfxFontDictEntry() {
2418
  // NB: If the font has been loaded, font is non-NULL and is owned by
2419
  // GfxFontDict.uniqueFonts.
2420
111k
  if (!loaded) {
2421
73.4k
    fontObj.free();
2422
73.4k
  }
2423
111k
}
2424
2425
37.9k
void GfxFontDictEntry::load(GfxFont *fontA) {
2426
37.9k
  loaded = gTrue;
2427
37.9k
  font = fontA;
2428
37.9k
  fontObj.free();
2429
37.9k
}
2430
2431
46.7k
GfxFontDict::GfxFontDict(XRef *xrefA, Ref *fontDictRef, Dict *fontDict) {
2432
46.7k
  xref = xrefA;
2433
46.7k
  fonts = new GHash(gTrue);
2434
46.7k
  uniqueFonts = new GList();
2435
2436
158k
  for (int i = 0; i < fontDict->getLength(); ++i) {
2437
111k
    char *tag = fontDict->getKey(i);
2438
111k
    Object fontObj;
2439
111k
    fontDict->getValNF(i, &fontObj);
2440
111k
    Ref r;
2441
111k
    if (fontObj.isRef()) {
2442
83.5k
      r = fontObj.getRef();
2443
83.5k
    } else if (fontDictRef) {
2444
      // legal generation numbers are five digits, so we use a
2445
      // 6-digit number here
2446
267
      r.gen = 100000 + fontDictRef->num;
2447
267
      r.num = i;
2448
27.6k
    } else {
2449
      // no indirect reference for this font, or for the containing
2450
      // font dict, so hash the font and use that
2451
27.6k
      r.gen = 100000;
2452
27.6k
      r.num = hashFontObject(&fontObj);
2453
27.6k
    }
2454
111k
    fonts->add(new GString(tag), new GfxFontDictEntry(r, &fontObj));
2455
111k
    fontObj.free();
2456
111k
  }
2457
46.7k
}
2458
2459
46.7k
GfxFontDict::~GfxFontDict() {
2460
46.7k
  deleteGList(uniqueFonts, GfxFont);
2461
46.7k
  deleteGHash(fonts, GfxFontDictEntry);
2462
46.7k
}
2463
2464
115k
GfxFont *GfxFontDict::lookup(char *tag) {
2465
115k
  GfxFontDictEntry *entry = (GfxFontDictEntry *)fonts->lookup(tag);
2466
115k
  if (!entry) {
2467
43.5k
    return NULL;
2468
43.5k
  }
2469
71.8k
  load(tag, entry);
2470
71.8k
  return entry->font;
2471
115k
}
2472
2473
22
GfxFont *GfxFontDict::lookupByRef(Ref ref) {
2474
22
  GHashIter *iter;
2475
22
  GString *tag;
2476
22
  GfxFontDictEntry *entry;
2477
22
  fonts->startIter(&iter);
2478
99
  while (fonts->getNext(&iter, &tag, (void **)&entry)) {
2479
85
    if (entry->ref.num == ref.num && entry->ref.gen == ref.gen) {
2480
8
      fonts->killIter(&iter);
2481
8
      load(tag->getCString(), entry);
2482
8
      return entry->font;
2483
8
    }
2484
85
  }
2485
14
  return NULL;
2486
22
}
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
71.8k
void GfxFontDict::load(char *tag, GfxFontDictEntry *entry) {
2508
71.8k
  if (entry->loaded) {
2509
33.8k
    return;
2510
33.8k
  }
2511
2512
  // check for a duplicate that has already been loaded
2513
  // (don't do this for "synthetic" refs)
2514
37.9k
  if (entry->fontObj.isRef()) {
2515
44.4k
    for (int i = 0; i < uniqueFonts->getLength(); ++i) {
2516
7.77k
      GfxFont *font = (GfxFont *)uniqueFonts->get(i);
2517
7.77k
      if (font->getID()->num == entry->ref.num &&
2518
5
    font->getID()->gen == entry->ref.gen) {
2519
3
  entry->load(font);
2520
3
  return;
2521
3
      }
2522
7.77k
    }
2523
36.6k
  }
2524
2525
37.9k
  GfxFont *font = NULL;
2526
37.9k
  Object obj;
2527
37.9k
  entry->fontObj.fetch(xref, &obj);
2528
37.9k
  if (obj.isDict()) {
2529
31.4k
    font = GfxFont::makeFont(xref, tag, entry->ref, obj.getDict());
2530
31.4k
    if (font->isOk()) {
2531
27.6k
      uniqueFonts->append(font);
2532
27.6k
    } else {
2533
3.75k
      delete font;
2534
3.75k
      font = NULL;
2535
3.75k
    }
2536
31.4k
  } else {
2537
6.53k
    error(errSyntaxError, -1, "font resource is not a dictionary");
2538
6.53k
  }
2539
37.9k
  obj.free();
2540
37.9k
  entry->load(font);
2541
37.9k
}
2542
2543
// FNV-1a hash
2544
class FNVHash {
2545
public:
2546
2547
37.8k
  FNVHash() {
2548
37.8k
    h = 2166136261U;
2549
37.8k
  }
2550
2551
5.20M
  void hash(char c) {
2552
5.20M
    h ^= c & 0xff;
2553
5.20M
    h *= 16777619;
2554
5.20M
  }
2555
2556
476k
  void hash(char *p, int n) {
2557
476k
    int i;
2558
5.44M
    for (i = 0; i < n; ++i) {
2559
4.96M
      hash(p[i]);
2560
4.96M
    }
2561
476k
  }
2562
2563
37.8k
  int get31() {
2564
37.8k
    return (h ^ (h >> 31)) & 0x7fffffff;
2565
37.8k
  }
2566
2567
private:
2568
2569
  Guint h;
2570
};
2571
2572
37.8k
int GfxFontDict::hashFontObject(Object *obj) {
2573
37.8k
  FNVHash h;
2574
2575
37.8k
  hashFontObject1(obj, &h);
2576
37.8k
  return h.get31();
2577
37.8k
}
2578
2579
243k
void GfxFontDict::hashFontObject1(Object *obj, FNVHash *h) {
2580
243k
  Object obj2;
2581
243k
  GString *s;
2582
243k
  char *p;
2583
243k
  double r;
2584
243k
  int n, i;
2585
2586
243k
  switch (obj->getType()) {
2587
163
  case objBool:
2588
163
    h->hash('b');
2589
163
    h->hash(obj->getBool() ? 1 : 0);
2590
163
    break;
2591
47.3k
  case objInt:
2592
47.3k
    h->hash('i');
2593
47.3k
    n = obj->getInt();
2594
47.3k
    h->hash((char *)&n, sizeof(int));
2595
47.3k
    break;
2596
3.25k
  case objReal:
2597
3.25k
    h->hash('r');
2598
3.25k
    r = obj->getReal();
2599
3.25k
    h->hash((char *)&r, sizeof(double));
2600
3.25k
    break;
2601
4.17k
  case objString:
2602
4.17k
    h->hash('s');
2603
4.17k
    s = obj->getString();
2604
4.17k
    h->hash(s->getCString(), s->getLength());
2605
4.17k
    break;
2606
54.0k
  case objName:
2607
54.0k
    h->hash('n');
2608
54.0k
    p = obj->getName();
2609
54.0k
    h->hash(p, (int)strlen(p));
2610
54.0k
    break;
2611
101
  case objNull:
2612
101
    h->hash('z');
2613
101
    break;
2614
6.97k
  case objArray:
2615
6.97k
    h->hash('a');
2616
6.97k
    n = obj->arrayGetLength();
2617
6.97k
    h->hash((char *)&n, sizeof(int));
2618
43.7k
    for (i = 0; i < n; ++i) {
2619
36.7k
      obj->arrayGetNF(i, &obj2);
2620
36.7k
      hashFontObject1(&obj2, h);
2621
36.7k
      obj2.free();
2622
36.7k
    }
2623
6.97k
    break;
2624
21.4k
  case objDict:
2625
21.4k
    h->hash('d');
2626
21.4k
    n = obj->dictGetLength();
2627
21.4k
    h->hash((char *)&n, sizeof(int));
2628
190k
    for (i = 0; i < n; ++i) {
2629
168k
      p = obj->dictGetKey(i);
2630
168k
      h->hash(p, (int)strlen(p));
2631
168k
      obj->dictGetValNF(i, &obj2);
2632
168k
      hashFontObject1(&obj2, h);
2633
168k
      obj2.free();
2634
168k
    }
2635
21.4k
    break;
2636
243
  case objStream:
2637
    // this should never happen - streams must be indirect refs
2638
243
    break;
2639
85.2k
  case objRef:
2640
85.2k
    h->hash('f');
2641
85.2k
    n = obj->getRefNum();
2642
85.2k
    h->hash((char *)&n, sizeof(int));
2643
85.2k
    n = obj->getRefGen();
2644
85.2k
    h->hash((char *)&n, sizeof(int));
2645
85.2k
    break;
2646
20.1k
  default:
2647
20.1k
    h->hash('u');
2648
20.1k
    break;
2649
243k
  }
2650
243k
}