1
//========================================================================
5
// Copyright 1996-2003 Glyph & Cog, LLC
7
//========================================================================
11
#ifdef USE_GCC_PRAGMAS
12
#pragma implementation
23
#include "GlobalParams.h"
25
#include "CharCodeToUnicode.h"
26
#include "FontEncodingTables.h"
27
#include "BuiltinFontTables.h"
28
#include "FoFiType1.h"
29
#include "FoFiType1C.h"
30
#include "FoFiTrueType.h"
33
//------------------------------------------------------------------------
35
struct StdFontMapEntry {
40
// Acrobat 4.0 and earlier substituted Base14-compatible fonts without
41
// providing Widths and a FontDescriptor, so we munge the names into
42
// the proper Base14 names. This table is from implementation note 44
43
// in the PDF 1.4 spec, with some additions based on empirical
45
static StdFontMapEntry stdFontMap[] = {
46
{ "Arial", "Helvetica" },
47
{ "Arial,Bold", "Helvetica-Bold" },
48
{ "Arial,BoldItalic", "Helvetica-BoldOblique" },
49
{ "Arial,Italic", "Helvetica-Oblique" },
50
{ "Arial-Bold", "Helvetica-Bold" },
51
{ "Arial-BoldItalic", "Helvetica-BoldOblique" },
52
{ "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
53
{ "Arial-BoldMT", "Helvetica-Bold" },
54
{ "Arial-Italic", "Helvetica-Oblique" },
55
{ "Arial-ItalicMT", "Helvetica-Oblique" },
56
{ "ArialMT", "Helvetica" },
57
{ "Courier,Bold", "Courier-Bold" },
58
{ "Courier,BoldItalic", "Courier-BoldOblique" },
59
{ "Courier,Italic", "Courier-Oblique" },
60
{ "CourierNew", "Courier" },
61
{ "CourierNew,Bold", "Courier-Bold" },
62
{ "CourierNew,BoldItalic", "Courier-BoldOblique" },
63
{ "CourierNew,Italic", "Courier-Oblique" },
64
{ "CourierNew-Bold", "Courier-Bold" },
65
{ "CourierNew-BoldItalic", "Courier-BoldOblique" },
66
{ "CourierNew-Italic", "Courier-Oblique" },
67
{ "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
68
{ "CourierNewPS-BoldMT", "Courier-Bold" },
69
{ "CourierNewPS-ItalicMT", "Courier-Oblique" },
70
{ "CourierNewPSMT", "Courier" },
71
{ "Helvetica,Bold", "Helvetica-Bold" },
72
{ "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
73
{ "Helvetica,Italic", "Helvetica-Oblique" },
74
{ "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
75
{ "Helvetica-Italic", "Helvetica-Oblique" },
76
{ "Symbol,Bold", "Symbol" },
77
{ "Symbol,BoldItalic", "Symbol" },
78
{ "Symbol,Italic", "Symbol" },
79
{ "TimesNewRoman", "Times-Roman" },
80
{ "TimesNewRoman,Bold", "Times-Bold" },
81
{ "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
82
{ "TimesNewRoman,Italic", "Times-Italic" },
83
{ "TimesNewRoman-Bold", "Times-Bold" },
84
{ "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
85
{ "TimesNewRoman-Italic", "Times-Italic" },
86
{ "TimesNewRomanPS", "Times-Roman" },
87
{ "TimesNewRomanPS-Bold", "Times-Bold" },
88
{ "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
89
{ "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
90
{ "TimesNewRomanPS-BoldMT", "Times-Bold" },
91
{ "TimesNewRomanPS-Italic", "Times-Italic" },
92
{ "TimesNewRomanPS-ItalicMT", "Times-Italic" },
93
{ "TimesNewRomanPSMT", "Times-Roman" },
94
{ "TimesNewRomanPSMT,Bold", "Times-Bold" },
95
{ "TimesNewRomanPSMT,BoldItalic", "Times-BoldItalic" },
96
{ "TimesNewRomanPSMT,Italic", "Times-Italic" }
99
//------------------------------------------------------------------------
101
//------------------------------------------------------------------------
103
GfxFont *GfxFont::makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict) {
108
// get base font name
110
fontDict->lookup("BaseFont", &obj1);
112
nameA = new GString(obj1.getName());
118
fontDict->lookup("Subtype", &obj1);
119
if (obj1.isName("Type1") || obj1.isName("MMType1")) {
120
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
121
} else if (obj1.isName("Type1C")) {
122
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
123
} else if (obj1.isName("Type3")) {
124
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
125
} else if (obj1.isName("TrueType")) {
126
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
127
} else if (obj1.isName("Type0")) {
128
font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
130
error(-1, "Unknown font type: '%s'",
131
obj1.isName() ? obj1.getName() : "???");
132
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
139
GfxFont::GfxFont(char *tagA, Ref idA, GString *nameA) {
141
tag = new GString(tagA);
149
GfxFont::~GfxFont() {
151
if (origName && origName != name) {
165
void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
166
Object obj1, obj2, obj3, obj4;
170
// assume Times-Roman by default (for substitution purposes)
177
if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
180
if (obj1.dictLookup("Flags", &obj2)->isInt()) {
181
flags = obj2.getInt();
186
obj1.dictLookup("FontName", &obj2);
188
embFontName = new GString(obj2.getName());
192
// look for embedded font file
193
if (obj1.dictLookupNF("FontFile", &obj2)->isRef()) {
194
embFontID = obj2.getRef();
195
if (type != fontType1) {
196
error(-1, "Mismatch between font type and embedded font file");
201
if (embFontID.num == -1 &&
202
obj1.dictLookupNF("FontFile2", &obj2)->isRef()) {
203
embFontID = obj2.getRef();
204
if (type != fontTrueType && type != fontCIDType2) {
205
error(-1, "Mismatch between font type and embedded font file");
206
type = type == fontCIDType0 ? fontCIDType2 : fontTrueType;
210
if (embFontID.num == -1 &&
211
obj1.dictLookupNF("FontFile3", &obj2)->isRef()) {
212
if (obj2.fetch(xref, &obj3)->isStream()) {
213
obj3.streamGetDict()->lookup("Subtype", &obj4);
214
if (obj4.isName("Type1")) {
215
embFontID = obj2.getRef();
216
if (type != fontType1) {
217
error(-1, "Mismatch between font type and embedded font file");
220
} else if (obj4.isName("Type1C")) {
221
embFontID = obj2.getRef();
222
if (type != fontType1 && type != fontType1C) {
223
error(-1, "Mismatch between font type and embedded font file");
226
} else if (obj4.isName("TrueType")) {
227
embFontID = obj2.getRef();
228
if (type != fontTrueType) {
229
error(-1, "Mismatch between font type and embedded font file");
232
} else if (obj4.isName("CIDFontType0C")) {
233
embFontID = obj2.getRef();
234
if (type != fontCIDType0) {
235
error(-1, "Mismatch between font type and embedded font file");
237
type = fontCIDType0C;
238
} else if (obj4.isName("OpenType")) {
239
embFontID = obj2.getRef();
240
if (type == fontTrueType) {
241
type = fontTrueTypeOT;
242
} else if (type == fontType1) {
244
} else if (type == fontCIDType0) {
245
type = fontCIDType0COT;
246
} else if (type == fontCIDType2) {
247
type = fontCIDType2OT;
249
error(-1, "Mismatch between font type and embedded font file");
252
error(-1, "Unknown embedded font type '%s'",
253
obj4.isName() ? obj4.getName() : "???");
261
// look for MissingWidth
262
obj1.dictLookup("MissingWidth", &obj2);
264
missingWidth = obj2.getNum();
268
// get Ascent and Descent
269
obj1.dictLookup("Ascent", &obj2);
271
t = 0.001 * obj2.getNum();
272
// some broken font descriptors set ascent and descent to 0
278
obj1.dictLookup("Descent", &obj2);
280
t = 0.001 * obj2.getNum();
281
// some broken font descriptors set ascent and descent to 0
285
// some broken font descriptors specify a positive descent
293
if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
294
for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
295
if (obj2.arrayGet(i, &obj3)->isNum()) {
296
fontBBox[i] = 0.001 * obj3.getNum();
307
CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits,
308
CharCodeToUnicode *ctu) {
313
if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
319
while ((c = obj1.streamGetChar()) != EOF) {
325
ctu->mergeCMap(buf, nBits);
327
ctu = CharCodeToUnicode::parseCMap(buf, nBits);
333
void GfxFont::findExtFontFile() {
334
static char *type1Exts[] = { ".pfa", ".pfb", ".ps", "", NULL };
335
static char *ttExts[] = { ".ttf", NULL };
338
if (type == fontType1) {
339
extFontFile = globalParams->findFontFile(name, type1Exts);
340
} else if (type == fontTrueType) {
341
extFontFile = globalParams->findFontFile(name, ttExts);
346
char *GfxFont::readExtFontFile(int *len) {
350
if (!(f = fopen(extFontFile->getCString(), "rb"))) {
351
error(-1, "External font file '%s' vanished", extFontFile->getCString());
354
fseek(f, 0, SEEK_END);
355
*len = (int)ftell(f);
356
fseek(f, 0, SEEK_SET);
357
buf = (char *)gmalloc(*len);
358
if ((int)fread(buf, 1, *len, f) != *len) {
359
error(-1, "Error reading external font file '%s'",
360
extFontFile->getCString());
366
char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
373
obj1.initRef(embFontID.num, embFontID.gen);
374
obj1.fetch(xref, &obj2);
375
if (!obj2.isStream()) {
376
error(-1, "Embedded font file is not a stream");
382
str = obj2.getStream();
387
while ((c = str->getChar()) != EOF) {
390
buf = (char *)grealloc(buf, size);
403
//------------------------------------------------------------------------
405
//------------------------------------------------------------------------
407
Gfx8BitFont::Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
408
GfxFontType typeA, Dict *fontDict):
409
GfxFont(tagA, idA, nameA)
412
BuiltinFont *builtinFont;
414
GBool baseEncFromFontFile;
422
Unicode toUnicode[256];
423
CharCodeToUnicode *utu, *ctu2;
426
int firstChar, lastChar;
428
Object obj1, obj2, obj3;
434
// do font name substitution for various aliases of the Base 14 font
437
name2 = name->copy();
439
while (i < name2->getLength()) {
440
if (name2->getChar(i) == ' ') {
447
b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
448
// invariant: stdFontMap[a].altName <= name2 < stdFontMap[b].altName
451
if (name2->cmp(stdFontMap[m].altName) >= 0) {
457
if (!name2->cmp(stdFontMap[a].altName)) {
458
name = new GString(stdFontMap[a].properName);
463
// is it a built-in font?
466
for (i = 0; i < nBuiltinFonts; ++i) {
467
if (!name->cmp(builtinFonts[i].name)) {
468
builtinFont = &builtinFonts[i];
474
// default ascent/descent values
476
ascent = 0.001 * builtinFont->ascent;
477
descent = 0.001 * builtinFont->descent;
478
fontBBox[0] = 0.001 * builtinFont->bbox[0];
479
fontBBox[1] = 0.001 * builtinFont->bbox[1];
480
fontBBox[2] = 0.001 * builtinFont->bbox[2];
481
fontBBox[3] = 0.001 * builtinFont->bbox[3];
485
fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
488
// get info from font descriptor
489
readFontDescriptor(xref, fontDict);
491
// for non-embedded fonts, don't trust the ascent/descent/bbox
492
// values from the font descriptor
493
if (builtinFont && embFontID.num < 0) {
494
ascent = 0.001 * builtinFont->ascent;
495
descent = 0.001 * builtinFont->descent;
496
fontBBox[0] = 0.001 * builtinFont->bbox[0];
497
fontBBox[1] = 0.001 * builtinFont->bbox[1];
498
fontBBox[2] = 0.001 * builtinFont->bbox[2];
499
fontBBox[3] = 0.001 * builtinFont->bbox[3];
502
// look for an external font file
506
fontMat[0] = fontMat[3] = 1;
507
fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
508
if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
509
for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
510
if (obj1.arrayGet(i, &obj2)->isNum()) {
511
fontMat[i] = obj2.getNum();
518
// get Type 3 bounding box, font definition, and resources
519
if (type == fontType3) {
520
if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
521
for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
522
if (obj1.arrayGet(i, &obj2)->isNum()) {
523
fontBBox[i] = obj2.getNum();
529
if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
530
error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
533
if (!fontDict->lookup("Resources", &resources)->isDict()) {
538
//----- build the font encoding -----
540
// Encodings start with a base encoding, which can come from
541
// (in order of priority):
542
// 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
543
// - MacRoman / MacExpert / WinAnsi / Standard
544
// 2. embedded or external font file
546
// - builtin --> builtin encoding
547
// - TrueType --> WinAnsiEncoding
548
// - others --> StandardEncoding
549
// and then add a list of differences (if any) from
550
// FontDict.Encoding.Differences.
552
// check FontDict for base encoding
553
hasEncoding = gFalse;
554
usesMacRomanEnc = gFalse;
556
baseEncFromFontFile = gFalse;
557
fontDict->lookup("Encoding", &obj1);
559
obj1.dictLookup("BaseEncoding", &obj2);
560
if (obj2.isName("MacRomanEncoding")) {
562
usesMacRomanEnc = gTrue;
563
baseEnc = macRomanEncoding;
564
} else if (obj2.isName("MacExpertEncoding")) {
566
baseEnc = macExpertEncoding;
567
} else if (obj2.isName("WinAnsiEncoding")) {
569
baseEnc = winAnsiEncoding;
572
} else if (obj1.isName("MacRomanEncoding")) {
574
usesMacRomanEnc = gTrue;
575
baseEnc = macRomanEncoding;
576
} else if (obj1.isName("MacExpertEncoding")) {
578
baseEnc = macExpertEncoding;
579
} else if (obj1.isName("WinAnsiEncoding")) {
581
baseEnc = winAnsiEncoding;
584
// check embedded or external font file for base encoding
585
// (only for Type 1 fonts - trying to get an encoding out of a
586
// TrueType font is a losing proposition)
590
if (type == fontType1 && (extFontFile || embFontID.num >= 0)) {
592
ffT1 = FoFiType1::load(extFontFile->getCString());
594
buf = readEmbFontFile(xref, &len);
595
ffT1 = FoFiType1::make(buf, len);
598
if (ffT1->getName()) {
602
embFontName = new GString(ffT1->getName());
605
baseEnc = ffT1->getEncoding();
606
baseEncFromFontFile = gTrue;
609
} else if (type == fontType1C && (extFontFile || embFontID.num >= 0)) {
611
ffT1C = FoFiType1C::load(extFontFile->getCString());
613
buf = readEmbFontFile(xref, &len);
614
ffT1C = FoFiType1C::make(buf, len);
617
if (ffT1C->getName()) {
621
embFontName = new GString(ffT1C->getName());
624
baseEnc = ffT1C->getEncoding();
625
baseEncFromFontFile = gTrue;
633
// get default base encoding
635
if (builtinFont && embFontID.num < 0) {
636
baseEnc = builtinFont->defaultBaseEnc;
638
} else if (type == fontTrueType) {
639
baseEnc = winAnsiEncoding;
641
baseEnc = standardEncoding;
645
// copy the base encoding
646
for (i = 0; i < 256; ++i) {
648
if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
649
enc[i] = copyString(baseEnc[i]);
653
// some Type 1C font files have empty encodings, which can break the
654
// T1C->T1 conversion (since the 'seac' operator depends on having
655
// the accents in the encoding), so we fill in any gaps from
657
if (type == fontType1C && (extFontFile || embFontID.num >= 0) &&
658
baseEncFromFontFile) {
659
for (i = 0; i < 256; ++i) {
660
if (!enc[i] && standardEncoding[i]) {
661
enc[i] = standardEncoding[i];
667
// merge differences into encoding
669
obj1.dictLookup("Differences", &obj2);
670
if (obj2.isArray()) {
673
for (i = 0; i < obj2.arrayGetLength(); ++i) {
674
obj2.arrayGet(i, &obj3);
676
code = obj3.getInt();
677
} else if (obj3.isName()) {
678
if (code >= 0 && code < 256) {
682
enc[code] = copyString(obj3.getName());
683
encFree[code] = gTrue;
687
error(-1, "Wrong type in font encoding resource differences (%s)",
703
//----- build the mapping to Unicode -----
705
// pass 1: use the name-to-Unicode mapping table
706
missing = hex = gFalse;
707
for (code = 0; code < 256; ++code) {
708
if ((charName = enc[code])) {
709
if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
710
strcmp(charName, ".notdef")) {
711
// if it wasn't in the name-to-Unicode table, check for a
712
// name that looks like 'Axx' or 'xx', where 'A' is any letter
713
// and 'xx' is two hex digits
714
if ((strlen(charName) == 3 &&
715
isalpha(charName[0]) &&
716
isxdigit(charName[1]) && isxdigit(charName[2]) &&
717
((charName[1] >= 'a' && charName[1] <= 'f') ||
718
(charName[1] >= 'A' && charName[1] <= 'F') ||
719
(charName[2] >= 'a' && charName[2] <= 'f') ||
720
(charName[2] >= 'A' && charName[2] <= 'F'))) ||
721
(strlen(charName) == 2 &&
722
isxdigit(charName[0]) && isxdigit(charName[1]) &&
723
((charName[0] >= 'a' && charName[0] <= 'f') ||
724
(charName[0] >= 'A' && charName[0] <= 'F') ||
725
(charName[1] >= 'a' && charName[1] <= 'f') ||
726
(charName[1] >= 'A' && charName[1] <= 'F')))) {
736
// pass 2: try to fill in the missing chars, looking for names of
737
// the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
738
// are any letters, 'xx' is two hex digits, and 'nn' is 2-4
740
if (missing && globalParams->getMapNumericCharNames()) {
741
for (code = 0; code < 256; ++code) {
742
if ((charName = enc[code]) && !toUnicode[code] &&
743
strcmp(charName, ".notdef")) {
744
n = strlen(charName);
746
if (hex && n == 3 && isalpha(charName[0]) &&
747
isxdigit(charName[1]) && isxdigit(charName[2])) {
748
sscanf(charName+1, "%x", &code2);
749
} else if (hex && n == 2 &&
750
isxdigit(charName[0]) && isxdigit(charName[1])) {
751
sscanf(charName, "%x", &code2);
752
} else if (!hex && n >= 2 && n <= 4 &&
753
isdigit(charName[0]) && isdigit(charName[1])) {
754
code2 = atoi(charName);
755
} else if (n >= 3 && n <= 5 &&
756
isdigit(charName[1]) && isdigit(charName[2])) {
757
code2 = atoi(charName+1);
758
} else if (n >= 4 && n <= 6 &&
759
isdigit(charName[2]) && isdigit(charName[3])) {
760
code2 = atoi(charName+2);
762
if (code2 >= 0 && code2 <= 0xff) {
763
toUnicode[code] = (Unicode)code2;
768
// if the 'mapUnknownCharNames' flag is set, do a simple pass-through
769
// mapping for unknown character names
770
} else if (missing && globalParams->getMapUnknownCharNames()) {
771
for (code = 0; code < 256; ++code) {
772
if (!toUnicode[code]) {
773
toUnicode[code] = code;
778
// construct the char code -> Unicode mapping object
779
ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
781
// merge in a ToUnicode CMap, if there is one -- this overwrites
782
// existing entries in ctu, i.e., the ToUnicode CMap takes
783
// precedence, but the other encoding info is allowed to fill in any
785
readToUnicodeCMap(fontDict, 8, ctu);
787
// look for a Unicode-to-Unicode mapping
788
if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
789
for (i = 0; i < 256; ++i) {
792
ctu2 = CharCodeToUnicode::make8BitToUnicode(toUnicode);
793
for (i = 0; i < 256; ++i) {
794
n = ctu->mapToUnicode((CharCode)i, uBuf, 8);
796
n = utu->mapToUnicode((CharCode)uBuf[0], uBuf, 8);
798
ctu2->setMapping((CharCode)i, uBuf, n);
807
//----- get the character widths -----
809
// initialize all widths
810
for (code = 0; code < 256; ++code) {
811
widths[code] = missingWidth * 0.001;
814
// use widths from font dict, if present
815
fontDict->lookup("FirstChar", &obj1);
816
firstChar = obj1.isInt() ? obj1.getInt() : 0;
818
if (firstChar < 0 || firstChar > 255) {
821
fontDict->lookup("LastChar", &obj1);
822
lastChar = obj1.isInt() ? obj1.getInt() : 255;
824
if (lastChar < 0 || lastChar > 255) {
827
mul = (type == fontType3) ? fontMat[0] : 0.001;
828
fontDict->lookup("Widths", &obj1);
829
if (obj1.isArray()) {
830
flags |= fontFixedWidth;
831
if (obj1.arrayGetLength() < lastChar - firstChar + 1) {
832
lastChar = firstChar + obj1.arrayGetLength() - 1;
834
for (code = firstChar; code <= lastChar; ++code) {
835
obj1.arrayGet(code - firstChar, &obj2);
837
widths[code] = obj2.getNum() * mul;
838
if (widths[code] != widths[firstChar]) {
839
flags &= ~fontFixedWidth;
845
// use widths from built-in font
846
} else if (builtinFont) {
847
// this is a kludge for broken PDF files that encode char 32
849
if (builtinFont->widths->getWidth("space", &w)) {
850
widths[32] = 0.001 * w;
852
for (code = 0; code < 256; ++code) {
853
if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
854
widths[code] = 0.001 * w;
858
// couldn't find widths -- use defaults
860
// this is technically an error -- the Widths entry is required
861
// for all but the Base-14 fonts -- but certain PDF generators
862
// apparently don't include widths for Arial and TimesNewRoman
863
if (isFixedWidth()) {
865
} else if (isSerif()) {
876
builtinFont = builtinFontSubst[i];
877
// this is a kludge for broken PDF files that encode char 32
879
if (builtinFont->widths->getWidth("space", &w)) {
880
widths[32] = 0.001 * w;
882
for (code = 0; code < 256; ++code) {
883
if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
884
widths[code] = 0.001 * w;
893
Gfx8BitFont::~Gfx8BitFont() {
896
for (i = 0; i < 256; ++i) {
897
if (encFree[i] && enc[i]) {
902
if (charProcs.isDict()) {
905
if (resources.isDict()) {
910
int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
911
Unicode *u, int uSize, int *uLen,
912
double *dx, double *dy, double *ox, double *oy) {
915
*code = c = (CharCode)(*s & 0xff);
916
*uLen = ctu->mapToUnicode(c, u, uSize);
922
CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
927
Gushort *Gfx8BitFont::getCodeToGIDMap(FoFiTrueType *ff) {
929
int cmapPlatform, cmapEncoding;
930
int unicodeCmap, macRomanCmap, msSymbolCmap, cmap;
931
GBool useMacRoman, useUnicode;
936
map = (Gushort *)gmallocn(256, sizeof(Gushort));
937
for (i = 0; i < 256; ++i) {
941
// To match up with the Adobe-defined behaviour, we choose a cmap
943
// 1. If the PDF font has an encoding:
944
// 1a. If the PDF font specified MacRomanEncoding and the
945
// TrueType font has a Macintosh Roman cmap, use it, and
946
// reverse map the char names through MacRomanEncoding to
948
// 1b. If the TrueType font has a Microsoft Unicode cmap or a
949
// non-Microsoft Unicode cmap, use it, and use the Unicode
950
// indexes, not the char codes.
951
// 1c. If the PDF font is symbolic and the TrueType font has a
952
// Microsoft Symbol cmap, use it, and use char codes
953
// directly (possibly with an offset of 0xf000).
954
// 1d. If the TrueType font has a Macintosh Roman cmap, use it,
956
// 2. If the PDF font does not have an encoding or the PDF font is
958
// 2a. If the TrueType font has a Macintosh Roman cmap, use it,
959
// and use char codes directly (possibly with an offset of
961
// 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
962
// and use char codes directly (possible with an offset of
964
// 3. If none of these rules apply, use the first cmap and hope for
965
// the best (this shouldn't happen).
966
unicodeCmap = macRomanCmap = msSymbolCmap = -1;
967
for (i = 0; i < ff->getNumCmaps(); ++i) {
968
cmapPlatform = ff->getCmapPlatform(i);
969
cmapEncoding = ff->getCmapEncoding(i);
970
if ((cmapPlatform == 3 && cmapEncoding == 1) ||
973
} else if (cmapPlatform == 1 && cmapEncoding == 0) {
975
} else if (cmapPlatform == 3 && cmapEncoding == 0) {
980
useMacRoman = gFalse;
983
if (usesMacRomanEnc && macRomanCmap >= 0) {
986
} else if (unicodeCmap >= 0) {
989
} else if ((flags & fontSymbolic) && msSymbolCmap >= 0) {
991
} else if ((flags & fontSymbolic) && macRomanCmap >= 0) {
993
} else if (macRomanCmap >= 0) {
998
if (msSymbolCmap >= 0) {
1000
} else if (macRomanCmap >= 0) {
1001
cmap = macRomanCmap;
1005
// reverse map the char names through MacRomanEncoding, then map the
1006
// char codes through the cmap
1008
for (i = 0; i < 256; ++i) {
1009
if ((charName = enc[i])) {
1010
if ((code = globalParams->getMacRomanCharCode(charName))) {
1011
map[i] = ff->mapCodeToGID(cmap, code);
1016
// map Unicode through the cmap
1017
} else if (useUnicode) {
1018
for (i = 0; i < 256; ++i) {
1019
if (((charName = enc[i]) &&
1020
(u = globalParams->mapNameToUnicode(charName))) ||
1021
(n = ctu->mapToUnicode((CharCode)i, &u, 1))) {
1022
map[i] = ff->mapCodeToGID(cmap, u);
1026
// map the char codes through the cmap, possibly with an offset of
1029
for (i = 0; i < 256; ++i) {
1030
if (!(map[i] = ff->mapCodeToGID(cmap, i))) {
1031
map[i] = ff->mapCodeToGID(cmap, 0xf000 + i);
1036
// try the TrueType 'post' table to handle any unmapped characters
1037
for (i = 0; i < 256; ++i) {
1038
if (!map[i] && (charName = enc[i])) {
1039
map[i] = (Gushort)(int)ff->mapNameToGID(charName);
1046
Dict *Gfx8BitFont::getCharProcs() {
1047
return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
1050
Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
1051
if (enc[code] && charProcs.isDict()) {
1052
charProcs.dictLookup(enc[code], proc);
1059
Dict *Gfx8BitFont::getResources() {
1060
return resources.isDict() ? resources.getDict() : (Dict *)NULL;
1063
//------------------------------------------------------------------------
1065
//------------------------------------------------------------------------
1067
static int CDECL cmpWidthExcep(const void *w1, const void *w2) {
1068
return ((GfxFontCIDWidthExcep *)w1)->first -
1069
((GfxFontCIDWidthExcep *)w2)->first;
1072
static int CDECL cmpWidthExcepV(const void *w1, const void *w2) {
1073
return ((GfxFontCIDWidthExcepV *)w1)->first -
1074
((GfxFontCIDWidthExcepV *)w2)->first;
1077
GfxCIDFont::GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
1079
GfxFont(tagA, idA, nameA)
1082
GString *collection, *cMapName;
1083
Object desFontDictObj;
1084
Object obj1, obj2, obj3, obj4, obj5, obj6;
1085
CharCodeToUnicode *utu;
1089
int excepsSize, i, j, k, n;
1093
fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
1096
widths.defWidth = 1.0;
1097
widths.defHeight = -1.0;
1098
widths.defVY = 0.880;
1099
widths.exceps = NULL;
1101
widths.excepsV = NULL;
1102
widths.nExcepsV = 0;
1106
// get the descendant font
1107
if (!fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
1108
error(-1, "Missing DescendantFonts entry in Type 0 font");
1112
if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
1113
error(-1, "Bad descendant font in Type 0 font");
1117
desFontDict = desFontDictObj.getDict();
1120
if (!desFontDict->lookup("Subtype", &obj1)) {
1121
error(-1, "Missing Subtype entry in Type 0 descendant font");
1124
if (obj1.isName("CIDFontType0")) {
1125
type = fontCIDType0;
1126
} else if (obj1.isName("CIDFontType2")) {
1127
type = fontCIDType2;
1129
error(-1, "Unknown Type 0 descendant font type '%s'",
1130
obj1.isName() ? obj1.getName() : "???");
1135
// get info from font descriptor
1136
readFontDescriptor(xref, desFontDict);
1138
// look for an external font file
1141
//----- encoding info -----
1144
if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
1145
error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
1148
obj1.dictLookup("Registry", &obj2);
1149
obj1.dictLookup("Ordering", &obj3);
1150
if (!obj2.isString() || !obj3.isString()) {
1151
error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
1154
collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
1159
// look for a ToUnicode CMap
1160
if (!(ctu = readToUnicodeCMap(fontDict, 16, NULL))) {
1162
// the "Adobe-Identity" and "Adobe-UCS" collections don't have
1163
// cidToUnicode files
1164
if (collection->cmp("Adobe-Identity") &&
1165
collection->cmp("Adobe-UCS")) {
1167
// look for a user-supplied .cidToUnicode file
1168
if (!(ctu = globalParams->getCIDToUnicode(collection))) {
1169
error(-1, "Unknown character collection '%s'",
1170
collection->getCString());
1171
// fall-through, assuming the Identity mapping -- this appears
1172
// to match Adobe's behavior
1177
// look for a Unicode-to-Unicode mapping
1178
if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
1180
for (c = 0; c < ctu->getLength(); ++c) {
1181
n = ctu->mapToUnicode(c, uBuf, 8);
1183
n = utu->mapToUnicode((CharCode)uBuf[0], uBuf, 8);
1185
ctu->setMapping(c, uBuf, n);
1195
// encoding (i.e., CMap)
1196
//~ need to handle a CMap stream here
1197
//~ also need to deal with the UseCMap entry in the stream dict
1198
if (!fontDict->lookup("Encoding", &obj1)->isName()) {
1199
error(-1, "Missing or invalid Encoding entry in Type 0 font");
1203
cMapName = new GString(obj1.getName());
1205
if (!(cMap = globalParams->getCMap(collection, cMapName))) {
1206
error(-1, "Unknown CMap '%s' for character collection '%s'",
1207
cMapName->getCString(), collection->getCString());
1215
// CIDToGIDMap (for embedded TrueType fonts)
1216
if (type == fontCIDType2) {
1217
desFontDict->lookup("CIDToGIDMap", &obj1);
1218
if (obj1.isStream()) {
1221
cidToGID = (Gushort *)gmallocn(i, sizeof(Gushort));
1223
while ((c1 = obj1.streamGetChar()) != EOF &&
1224
(c2 = obj1.streamGetChar()) != EOF) {
1225
if (cidToGIDLen == i) {
1227
cidToGID = (Gushort *)greallocn(cidToGID, i, sizeof(Gushort));
1229
cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
1231
} else if (!obj1.isName("Identity") && !obj1.isNull()) {
1232
error(-1, "Invalid CIDToGIDMap entry in CID font");
1237
//----- character metrics -----
1239
// default char width
1240
if (desFontDict->lookup("DW", &obj1)->isInt()) {
1241
widths.defWidth = obj1.getInt() * 0.001;
1245
// char width exceptions
1246
if (desFontDict->lookup("W", &obj1)->isArray()) {
1249
while (i + 1 < obj1.arrayGetLength()) {
1250
obj1.arrayGet(i, &obj2);
1251
obj1.arrayGet(i + 1, &obj3);
1252
if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
1253
if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
1254
if (widths.nExceps == excepsSize) {
1256
widths.exceps = (GfxFontCIDWidthExcep *)
1257
greallocn(widths.exceps,
1258
excepsSize, sizeof(GfxFontCIDWidthExcep));
1260
widths.exceps[widths.nExceps].first = obj2.getInt();
1261
widths.exceps[widths.nExceps].last = obj3.getInt();
1262
widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1265
error(-1, "Bad widths array in Type 0 font");
1269
} else if (obj2.isInt() && obj3.isArray()) {
1270
if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1271
excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1272
widths.exceps = (GfxFontCIDWidthExcep *)
1273
greallocn(widths.exceps,
1274
excepsSize, sizeof(GfxFontCIDWidthExcep));
1277
for (k = 0; k < obj3.arrayGetLength(); ++k) {
1278
if (obj3.arrayGet(k, &obj4)->isNum()) {
1279
widths.exceps[widths.nExceps].first = j;
1280
widths.exceps[widths.nExceps].last = j;
1281
widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1285
error(-1, "Bad widths array in Type 0 font");
1291
error(-1, "Bad widths array in Type 0 font");
1297
qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
1302
// default metrics for vertical font
1303
if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1304
obj1.arrayGetLength() == 2) {
1305
if (obj1.arrayGet(0, &obj2)->isNum()) {
1306
widths.defVY = obj2.getNum() * 0.001;
1309
if (obj1.arrayGet(1, &obj2)->isNum()) {
1310
widths.defHeight = obj2.getNum() * 0.001;
1316
// char metric exceptions for vertical font
1317
if (desFontDict->lookup("W2", &obj1)->isArray()) {
1320
while (i + 1 < obj1.arrayGetLength()) {
1321
obj1.arrayGet(i, &obj2);
1322
obj1.arrayGet(i+ 1, &obj3);
1323
if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1324
if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1325
obj1.arrayGet(i + 3, &obj5)->isNum() &&
1326
obj1.arrayGet(i + 4, &obj6)->isNum()) {
1327
if (widths.nExcepsV == excepsSize) {
1329
widths.excepsV = (GfxFontCIDWidthExcepV *)
1330
greallocn(widths.excepsV,
1331
excepsSize, sizeof(GfxFontCIDWidthExcepV));
1333
widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1334
widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1335
widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1336
widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1337
widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1340
error(-1, "Bad widths (W2) array in Type 0 font");
1346
} else if (obj2.isInt() && obj3.isArray()) {
1347
if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
1349
(widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
1350
widths.excepsV = (GfxFontCIDWidthExcepV *)
1351
greallocn(widths.excepsV,
1352
excepsSize, sizeof(GfxFontCIDWidthExcepV));
1355
for (k = 0; k < obj3.arrayGetLength(); k += 3) {
1356
if (obj3.arrayGet(k, &obj4)->isNum() &&
1357
obj3.arrayGet(k+1, &obj5)->isNum() &&
1358
obj3.arrayGet(k+2, &obj6)->isNum()) {
1359
widths.excepsV[widths.nExceps].first = j;
1360
widths.excepsV[widths.nExceps].last = j;
1361
widths.excepsV[widths.nExceps].height = obj4.getNum() * 0.001;
1362
widths.excepsV[widths.nExceps].vx = obj5.getNum() * 0.001;
1363
widths.excepsV[widths.nExceps].vy = obj6.getNum() * 0.001;
1367
error(-1, "Bad widths (W2) array in Type 0 font");
1375
error(-1, "Bad widths (W2) array in Type 0 font");
1381
qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
1386
desFontDictObj.free();
1396
desFontDictObj.free();
1400
GfxCIDFont::~GfxCIDFont() {
1407
gfree(widths.exceps);
1408
gfree(widths.excepsV);
1414
int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
1415
Unicode *u, int uSize, int *uLen,
1416
double *dx, double *dy, double *ox, double *oy) {
1418
double w, h, vx, vy;
1428
*code = (CharCode)(cid = cMap->getCID(s, len, &n));
1430
*uLen = ctu->mapToUnicode(cid, u, uSize);
1436
if (cMap->getWMode() == 0) {
1437
w = widths.defWidth;
1439
if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
1442
// invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1445
if (widths.exceps[m].first <= cid) {
1451
if (cid <= widths.exceps[a].last) {
1452
w = widths.exceps[a].width;
1459
h = widths.defHeight;
1460
vx = widths.defWidth / 2;
1462
if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
1464
b = widths.nExcepsV;
1465
// invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1468
if (widths.excepsV[m].last <= cid) {
1474
if (cid <= widths.excepsV[a].last) {
1475
h = widths.excepsV[a].height;
1476
vx = widths.excepsV[a].vx;
1477
vy = widths.excepsV[a].vy;
1490
int GfxCIDFont::getWMode() {
1491
return cMap ? cMap->getWMode() : 0;
1494
CharCodeToUnicode *GfxCIDFont::getToUnicode() {
1501
GString *GfxCIDFont::getCollection() {
1502
return cMap ? cMap->getCollection() : (GString *)NULL;
1505
//------------------------------------------------------------------------
1507
//------------------------------------------------------------------------
1509
GfxFontDict::GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict) {
1514
numFonts = fontDict->getLength();
1515
fonts = (GfxFont **)gmallocn(numFonts, sizeof(GfxFont *));
1516
for (i = 0; i < numFonts; ++i) {
1517
fontDict->getValNF(i, &obj1);
1518
obj1.fetch(xref, &obj2);
1519
if (obj2.isDict()) {
1523
// no indirect reference for this font, so invent a unique one
1524
// (legal generation numbers are five digits, so any 6-digit
1525
// number would be safe)
1528
r.gen = 100000 + fontDictRef->num;
1533
fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i),
1535
if (fonts[i] && !fonts[i]->isOk()) {
1540
error(-1, "font resource is not a dictionary");
1548
GfxFontDict::~GfxFontDict() {
1551
for (i = 0; i < numFonts; ++i) {
1559
GfxFont *GfxFontDict::lookup(char *tag) {
1562
for (i = 0; i < numFonts; ++i) {
1563
if (fonts[i] && fonts[i]->matches(tag)) {