1
//========================================================================
5
// Copyright 1996-2003 Glyph & Cog, LLC
7
//========================================================================
9
//========================================================================
11
// Modified under the Poppler project - http://poppler.freedesktop.org
13
// Copyright (C) 2005, 2006, 2008, 2009 Albert Astals Cid <aacid@kde.org>
14
// Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com>
15
// Copyright (C) 2006 Takashi Iwai <tiwai@suse.de>
16
// Copyright (C) 2007 Julien Rebetez <julienr@svn.gnome.org>
17
// Copyright (C) 2007 Jeff Muizelaar <jeff@infidigm.net>
18
// Copyright (C) 2007 Koji Otani <sho@bbr.jp>
19
// Copyright (C) 2007 Ed Catmur <ed@catmur.co.uk>
20
// Copyright (C) 2008 Jonathan Kew <jonathan_kew@sil.org>
21
// Copyright (C) 2008 Ed Avis <eda@waniasset.com>
22
// Copyright (C) 2008 Hib Eris <hib@hiberis.nl>
23
// Copyright (C) 2009 Peter Kerzum <kerzum@yandex-team.ru>
24
// Copyright (C) 2009 David Benjamin <davidben@mit.edu>
26
// To see a description of the changes please see the Changelog file that
27
// came with your tarball or type make ChangeLog if you are building from git
29
//========================================================================
33
#ifdef USE_GCC_PRAGMAS
34
#pragma implementation
45
#include "GlobalParams.h"
47
#include "CharCodeToUnicode.h"
48
#include "FontEncodingTables.h"
49
#include "BuiltinFontTables.h"
50
#include <fofi/FoFiType1.h>
51
#include <fofi/FoFiType1C.h>
52
#include <fofi/FoFiTrueType.h>
55
//------------------------------------------------------------------------
57
struct StdFontMapEntry {
62
// Acrobat 4.0 and earlier substituted Base14-compatible fonts without
63
// providing Widths and a FontDescriptor, so we munge the names into
64
// the proper Base14 names. This table is from implementation note 44
65
// in the PDF 1.4 spec, with some additions based on empirical
67
static const StdFontMapEntry stdFontMap[] = {
68
{ "Arial", "Helvetica" },
69
{ "Arial,Bold", "Helvetica-Bold" },
70
{ "Arial,BoldItalic", "Helvetica-BoldOblique" },
71
{ "Arial,Italic", "Helvetica-Oblique" },
72
{ "Arial-Bold", "Helvetica-Bold" },
73
{ "Arial-BoldItalic", "Helvetica-BoldOblique" },
74
{ "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
75
{ "Arial-BoldMT", "Helvetica-Bold" },
76
{ "Arial-Italic", "Helvetica-Oblique" },
77
{ "Arial-ItalicMT", "Helvetica-Oblique" },
78
{ "ArialMT", "Helvetica" },
79
{ "Courier,Bold", "Courier-Bold" },
80
{ "Courier,BoldItalic", "Courier-BoldOblique" },
81
{ "Courier,Italic", "Courier-Oblique" },
82
{ "CourierNew", "Courier" },
83
{ "CourierNew,Bold", "Courier-Bold" },
84
{ "CourierNew,BoldItalic", "Courier-BoldOblique" },
85
{ "CourierNew,Italic", "Courier-Oblique" },
86
{ "CourierNew-Bold", "Courier-Bold" },
87
{ "CourierNew-BoldItalic", "Courier-BoldOblique" },
88
{ "CourierNew-Italic", "Courier-Oblique" },
89
{ "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
90
{ "CourierNewPS-BoldMT", "Courier-Bold" },
91
{ "CourierNewPS-ItalicMT", "Courier-Oblique" },
92
{ "CourierNewPSMT", "Courier" },
93
{ "Helvetica,Bold", "Helvetica-Bold" },
94
{ "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
95
{ "Helvetica,Italic", "Helvetica-Oblique" },
96
{ "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
97
{ "Helvetica-Italic", "Helvetica-Oblique" },
98
{ "Symbol,Bold", "Symbol" },
99
{ "Symbol,BoldItalic", "Symbol" },
100
{ "Symbol,Italic", "Symbol" },
101
{ "TimesNewRoman", "Times-Roman" },
102
{ "TimesNewRoman,Bold", "Times-Bold" },
103
{ "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
104
{ "TimesNewRoman,Italic", "Times-Italic" },
105
{ "TimesNewRoman-Bold", "Times-Bold" },
106
{ "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
107
{ "TimesNewRoman-Italic", "Times-Italic" },
108
{ "TimesNewRomanPS", "Times-Roman" },
109
{ "TimesNewRomanPS-Bold", "Times-Bold" },
110
{ "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
111
{ "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
112
{ "TimesNewRomanPS-BoldMT", "Times-Bold" },
113
{ "TimesNewRomanPS-Italic", "Times-Italic" },
114
{ "TimesNewRomanPS-ItalicMT", "Times-Italic" },
115
{ "TimesNewRomanPSMT", "Times-Roman" },
116
{ "TimesNewRomanPSMT,Bold", "Times-Bold" },
117
{ "TimesNewRomanPSMT,BoldItalic", "Times-BoldItalic" },
118
{ "TimesNewRomanPSMT,Italic", "Times-Italic" }
121
static int parseCharName(char *charName, Unicode *uBuf, int uLen,
122
GBool names, GBool ligatures,
123
GBool numeric, GBool hex, GBool variants);
125
//------------------------------------------------------------------------
127
//------------------------------------------------------------------------
129
GfxFont *GfxFont::makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict) {
134
// get base font name
136
fontDict->lookup("BaseFont", &obj1);
138
nameA = new GooString(obj1.getName());
144
fontDict->lookup("Subtype", &obj1);
145
if (obj1.isName("Type1") || obj1.isName("MMType1")) {
146
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
147
} else if (obj1.isName("Type1C")) {
148
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
149
} else if (obj1.isName("Type3")) {
150
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
151
} else if (obj1.isName("TrueType")) {
152
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
153
} else if (obj1.isName("Type0")) {
154
font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
156
error(-1, "Unknown font type: '%s'",
157
obj1.isName() ? obj1.getName() : "???");
158
font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
165
GfxFont::GfxFont(char *tagA, Ref idA, GooString *nameA) {
167
tag = new GooString(tagA);
174
stretch = StretchNotDefined;
175
weight = WeightNotDefined;
180
GfxFont::~GfxFont() {
183
if (origName && origName != name) {
198
void GfxFont::incRefCnt() {
202
void GfxFont::decRefCnt() {
207
void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
208
Object obj1, obj2, obj3, obj4;
212
// assume Times-Roman by default (for substitution purposes)
219
if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
222
if (obj1.dictLookup("Flags", &obj2)->isInt()) {
223
flags = obj2.getInt();
228
obj1.dictLookup("FontName", &obj2);
230
embFontName = new GooString(obj2.getName());
233
if (embFontName == NULL) {
234
// get name with typo
235
obj1.dictLookup("Fontname", &obj2);
237
embFontName = new GooString(obj2.getName());
238
error(-1, "The file uses Fontname instead of FontName please notify the creator that the file is broken");
244
obj1.dictLookup("FontFamily", &obj2);
245
if (obj2.isString()) family = new GooString(obj2.getString());
249
obj1.dictLookup("FontStretch", &obj2);
251
if (strcmp(obj2.getName(), "UltraCondensed") == 0) stretch = UltraCondensed;
252
else if (strcmp(obj2.getName(), "ExtraCondensed") == 0) stretch = ExtraCondensed;
253
else if (strcmp(obj2.getName(), "Condensed") == 0) stretch = Condensed;
254
else if (strcmp(obj2.getName(), "SemiCondensed") == 0) stretch = SemiCondensed;
255
else if (strcmp(obj2.getName(), "Normal") == 0) stretch = Normal;
256
else if (strcmp(obj2.getName(), "SemiExpanded") == 0) stretch = SemiExpanded;
257
else if (strcmp(obj2.getName(), "Expanded") == 0) stretch = Expanded;
258
else if (strcmp(obj2.getName(), "ExtraExpanded") == 0) stretch = ExtraExpanded;
259
else if (strcmp(obj2.getName(), "UltraExpanded") == 0) stretch = UltraExpanded;
260
else error(-1, "Invalid Font Stretch");
265
obj1.dictLookup("FontWeight", &obj2);
267
if (obj2.getNum() == 100) weight = W100;
268
else if (obj2.getNum() == 200) weight = W200;
269
else if (obj2.getNum() == 300) weight = W300;
270
else if (obj2.getNum() == 400) weight = W400;
271
else if (obj2.getNum() == 500) weight = W500;
272
else if (obj2.getNum() == 600) weight = W600;
273
else if (obj2.getNum() == 700) weight = W700;
274
else if (obj2.getNum() == 800) weight = W800;
275
else if (obj2.getNum() == 900) weight = W900;
276
else error(-1, "Invalid Font Weight");
280
// look for embedded font file
281
if (obj1.dictLookupNF("FontFile", &obj2)->isRef()) {
282
embFontID = obj2.getRef();
283
if (type != fontType1) {
284
error(-1, "Mismatch between font type and embedded font file");
286
error(-1, "CID font has FontFile attribute; assuming CIDType0");
294
if (embFontID.num == -1 &&
295
obj1.dictLookupNF("FontFile2", &obj2)->isRef()) {
296
embFontID = obj2.getRef();
297
if (type != fontTrueType && type != fontCIDType2) {
298
error(-1, "Mismatch between font type and embedded font file");
299
type = isCIDFont() ? fontCIDType2 : fontTrueType;
303
if (embFontID.num == -1 &&
304
obj1.dictLookupNF("FontFile3", &obj2)->isRef()) {
305
if (obj2.fetch(xref, &obj3)->isStream()) {
306
obj3.streamGetDict()->lookup("Subtype", &obj4);
307
if (obj4.isName("Type1")) {
308
embFontID = obj2.getRef();
309
if (type != fontType1) {
310
error(-1, "Mismatch between font type and embedded font file");
312
error(-1, "Embedded CID font has type Type1; assuming CIDType0");
318
} else if (obj4.isName("Type1C")) {
319
embFontID = obj2.getRef();
320
if (type != fontType1 && type != fontType1C) {
321
error(-1, "Mismatch between font type and embedded font file");
324
error(-1, "Embedded CID font has type Type1C; assuming CIDType0C");
325
type = fontCIDType0C;
329
} else if (obj4.isName("TrueType")) {
330
embFontID = obj2.getRef();
331
if (type != fontTrueType) {
332
error(-1, "Mismatch between font type and embedded font file");
334
error(-1, "Embedded CID font has type TrueType; assuming CIDType2");
340
} else if (obj4.isName("CIDFontType0C")) {
341
embFontID = obj2.getRef();
342
if (type != fontCIDType0) {
343
error(-1, "Mismatch between font type and embedded font file");
346
type = fontCIDType0C;
348
error(-1, "Embedded non-CID font has type CIDFontType0c; assuming Type1C");
351
} else if (obj4.isName("OpenType")) {
352
embFontID = obj2.getRef();
353
if (type == fontTrueType) {
354
type = fontTrueTypeOT;
355
} else if (type == fontType1) {
357
} else if (type == fontCIDType0) {
358
type = fontCIDType0COT;
359
} else if (type == fontCIDType2) {
360
type = fontCIDType2OT;
362
error(-1, "Mismatch between font type and embedded font file");
365
error(-1, "Unknown embedded font type '%s'",
366
obj4.isName() ? obj4.getName() : "???");
374
// look for MissingWidth
375
obj1.dictLookup("MissingWidth", &obj2);
377
missingWidth = obj2.getNum();
381
// get Ascent and Descent
382
obj1.dictLookup("Ascent", &obj2);
384
t = 0.001 * obj2.getNum();
385
// some broken font descriptors set ascent and descent to 0
391
obj1.dictLookup("Descent", &obj2);
393
t = 0.001 * obj2.getNum();
394
// some broken font descriptors set ascent and descent to 0
398
// some broken font descriptors specify a positive descent
406
if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
407
for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
408
if (obj2.arrayGet(i, &obj3)->isNum()) {
409
fontBBox[i] = 0.001 * obj3.getNum();
420
CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits,
421
CharCodeToUnicode *ctu) {
426
if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
430
buf = new GooString();
432
while ((c = obj1.streamGetChar()) != EOF) {
438
ctu->mergeCMap(buf, nBits);
440
ctu = CharCodeToUnicode::parseCMap(buf, nBits);
446
void GfxFont::findExtFontFile() {
447
static char *type1Exts[] = { ".pfa", ".pfb", ".ps", "", NULL };
448
static char *ttExts[] = { ".ttf", ".ttc", NULL };
451
if (type == fontType1) {
452
extFontFile = globalParams->findFontFile(name, type1Exts);
453
} else if (type == fontTrueType) {
454
extFontFile = globalParams->findFontFile(name, ttExts);
459
char *GfxFont::readExtFontFile(int *len) {
463
if (!(f = fopen(extFontFile->getCString(), "rb"))) {
464
error(-1, "External font file '%s' vanished", extFontFile->getCString());
467
if (fseek(f, 0, SEEK_END) != 0) {
468
error(-1, "Cannot seek to end of '%s'", extFontFile->getCString());
472
*len = (int)ftell(f);
473
if (fseek(f, 0, SEEK_SET) != 0) {
474
error(-1, "Cannot seek to start of '%s'", extFontFile->getCString());
478
buf = (char *)gmalloc(*len);
479
if ((int)fread(buf, 1, *len, f) != *len) {
480
error(-1, "Error reading external font file '%s'",
481
extFontFile->getCString());
487
char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
494
obj1.initRef(embFontID.num, embFontID.gen);
495
obj1.fetch(xref, &obj2);
496
if (!obj2.isStream()) {
497
error(-1, "Embedded font file is not a stream");
504
str = obj2.getStream();
509
while ((c = str->getChar()) != EOF) {
512
buf = (char *)grealloc(buf, size);
525
//------------------------------------------------------------------------
527
//------------------------------------------------------------------------
529
Gfx8BitFont::Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GooString *nameA,
530
GfxFontType typeA, Dict *fontDict):
531
GfxFont(tagA, idA, nameA)
534
BuiltinFont *builtinFont;
536
GBool baseEncFromFontFile;
544
Unicode toUnicode[256];
545
CharCodeToUnicode *utu, *ctu2;
548
int firstChar, lastChar;
550
Object obj1, obj2, obj3;
557
// do font name substitution for various aliases of the Base 14 font
560
name2 = name->copy();
562
while (i < name2->getLength()) {
563
if (name2->getChar(i) == ' ') {
570
b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
571
// invariant: stdFontMap[a].altName <= name2 < stdFontMap[b].altName
574
if (name2->cmp(stdFontMap[m].altName) >= 0) {
580
if (!name2->cmp(stdFontMap[a].altName)) {
581
name = new GooString(stdFontMap[a].properName);
586
// is it a built-in font?
589
for (i = 0; i < nBuiltinFonts; ++i) {
590
if (!name->cmp(builtinFonts[i].name)) {
591
builtinFont = &builtinFonts[i];
597
// default ascent/descent values
599
ascent = 0.001 * builtinFont->ascent;
600
descent = 0.001 * builtinFont->descent;
601
fontBBox[0] = 0.001 * builtinFont->bbox[0];
602
fontBBox[1] = 0.001 * builtinFont->bbox[1];
603
fontBBox[2] = 0.001 * builtinFont->bbox[2];
604
fontBBox[3] = 0.001 * builtinFont->bbox[3];
608
fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
611
// get info from font descriptor
612
readFontDescriptor(xref, fontDict);
614
// for non-embedded fonts, don't trust the ascent/descent/bbox
615
// values from the font descriptor
616
if (builtinFont && embFontID.num < 0) {
617
ascent = 0.001 * builtinFont->ascent;
618
descent = 0.001 * builtinFont->descent;
619
fontBBox[0] = 0.001 * builtinFont->bbox[0];
620
fontBBox[1] = 0.001 * builtinFont->bbox[1];
621
fontBBox[2] = 0.001 * builtinFont->bbox[2];
622
fontBBox[3] = 0.001 * builtinFont->bbox[3];
625
// look for an external font file
629
fontMat[0] = fontMat[3] = 1;
630
fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
631
if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
632
for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
633
if (obj1.arrayGet(i, &obj2)->isNum()) {
634
fontMat[i] = obj2.getNum();
641
// get Type 3 bounding box, font definition, and resources
642
if (type == fontType3) {
643
if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
644
for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
645
if (obj1.arrayGet(i, &obj2)->isNum()) {
646
fontBBox[i] = obj2.getNum();
652
if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
653
error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
656
if (!fontDict->lookup("Resources", &resources)->isDict()) {
661
//----- build the font encoding -----
663
// Encodings start with a base encoding, which can come from
664
// (in order of priority):
665
// 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
666
// - MacRoman / MacExpert / WinAnsi / Standard
667
// 2. embedded or external font file
669
// - builtin --> builtin encoding
670
// - TrueType --> WinAnsiEncoding
671
// - others --> StandardEncoding
672
// and then add a list of differences (if any) from
673
// FontDict.Encoding.Differences.
675
// check FontDict for base encoding
676
hasEncoding = gFalse;
677
usesMacRomanEnc = gFalse;
679
baseEncFromFontFile = gFalse;
680
fontDict->lookup("Encoding", &obj1);
682
obj1.dictLookup("BaseEncoding", &obj2);
683
if (obj2.isName("MacRomanEncoding")) {
685
usesMacRomanEnc = gTrue;
686
baseEnc = macRomanEncoding;
687
} else if (obj2.isName("MacExpertEncoding")) {
689
baseEnc = macExpertEncoding;
690
} else if (obj2.isName("WinAnsiEncoding")) {
692
baseEnc = winAnsiEncoding;
695
} else if (obj1.isName("MacRomanEncoding")) {
697
usesMacRomanEnc = gTrue;
698
baseEnc = macRomanEncoding;
699
} else if (obj1.isName("MacExpertEncoding")) {
701
baseEnc = macExpertEncoding;
702
} else if (obj1.isName("WinAnsiEncoding")) {
704
baseEnc = winAnsiEncoding;
707
// check embedded or external font file for base encoding
708
// (only for Type 1 fonts - trying to get an encoding out of a
709
// TrueType font is a losing proposition)
713
if (type == fontType1 && (extFontFile || embFontID.num >= 0)) {
715
ffT1 = FoFiType1::load(extFontFile->getCString());
717
buf = readEmbFontFile(xref, &len);
718
ffT1 = FoFiType1::make(buf, len);
721
if (ffT1->getName()) {
725
embFontName = new GooString(ffT1->getName());
728
baseEnc = ffT1->getEncoding();
729
baseEncFromFontFile = gTrue;
732
} else if (type == fontType1C && (extFontFile || embFontID.num >= 0)) {
734
ffT1C = FoFiType1C::load(extFontFile->getCString());
736
buf = readEmbFontFile(xref, &len);
737
ffT1C = FoFiType1C::make(buf, len);
740
if (ffT1C->getName()) {
744
embFontName = new GooString(ffT1C->getName());
747
baseEnc = ffT1C->getEncoding();
748
baseEncFromFontFile = gTrue;
756
// get default base encoding
758
if (builtinFont && embFontID.num < 0) {
759
baseEnc = builtinFont->defaultBaseEnc;
761
} else if (type == fontTrueType) {
762
baseEnc = winAnsiEncoding;
764
baseEnc = standardEncoding;
768
// copy the base encoding
769
for (i = 0; i < 256; ++i) {
771
if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
772
enc[i] = copyString(baseEnc[i]);
776
// some Type 1C font files have empty encodings, which can break the
777
// T1C->T1 conversion (since the 'seac' operator depends on having
778
// the accents in the encoding), so we fill in any gaps from
780
if (type == fontType1C && (extFontFile || embFontID.num >= 0) &&
781
baseEncFromFontFile) {
782
for (i = 0; i < 256; ++i) {
783
if (!enc[i] && standardEncoding[i]) {
784
enc[i] = standardEncoding[i];
790
// merge differences into encoding
792
obj1.dictLookup("Differences", &obj2);
793
if (obj2.isArray()) {
796
for (i = 0; i < obj2.arrayGetLength(); ++i) {
797
obj2.arrayGet(i, &obj3);
799
code = obj3.getInt();
800
} else if (obj3.isName()) {
801
if (code >= 0 && code < 256) {
805
enc[code] = copyString(obj3.getName());
806
encFree[code] = gTrue;
810
error(-1, "Wrong type in font encoding resource differences (%s)",
826
//----- build the mapping to Unicode -----
828
// pass 1: use the name-to-Unicode mapping table
829
missing = hex = gFalse;
830
for (code = 0; code < 256; ++code) {
831
if ((charName = enc[code])) {
832
if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
833
strcmp(charName, ".notdef")) {
834
// if it wasn't in the name-to-Unicode table, check for a
835
// name that looks like 'Axx' or 'xx', where 'A' is any letter
836
// and 'xx' is two hex digits
837
if ((strlen(charName) == 3 &&
838
isalpha(charName[0]) &&
839
isxdigit(charName[1]) && isxdigit(charName[2]) &&
840
((charName[1] >= 'a' && charName[1] <= 'f') ||
841
(charName[1] >= 'A' && charName[1] <= 'F') ||
842
(charName[2] >= 'a' && charName[2] <= 'f') ||
843
(charName[2] >= 'A' && charName[2] <= 'F'))) ||
844
(strlen(charName) == 2 &&
845
isxdigit(charName[0]) && isxdigit(charName[1]) &&
846
((charName[0] >= 'a' && charName[0] <= 'f') ||
847
(charName[0] >= 'A' && charName[0] <= 'F') ||
848
(charName[1] >= 'a' && charName[1] <= 'f') ||
849
(charName[1] >= 'A' && charName[1] <= 'F')))) {
859
// construct the char code -> Unicode mapping object
860
ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
862
// pass 2: try to fill in the missing chars, looking for ligatures, numeric
863
// references and variants
865
for (code = 0; code < 256; ++code) {
866
if (!toUnicode[code]) {
867
if ((charName = enc[code]) && strcmp(charName, ".notdef")
868
&& (n = parseCharName(charName, uBuf, sizeof(uBuf)/sizeof(*uBuf),
869
gFalse, // don't check simple names (pass 1)
870
gTrue, // do check ligatures
871
globalParams->getMapNumericCharNames(),
873
gTrue))) { // do check variants
874
ctu->setMapping((CharCode)code, uBuf, n);
875
} else if (globalParams->getMapUnknownCharNames()) {
876
// if the 'mapUnknownCharNames' flag is set, do a simple pass-through
877
// mapping for unknown character names
878
if (charName && charName[0]) {
879
for (n = 0; n < (int)(sizeof(uBuf)/sizeof(*uBuf)); ++n)
880
if (!(uBuf[n] = charName[n]))
882
ctu->setMapping((CharCode)code, uBuf, n);
885
ctu->setMapping((CharCode)code, uBuf, 1);
892
// merge in a ToUnicode CMap, if there is one -- this overwrites
893
// existing entries in ctu, i.e., the ToUnicode CMap takes
894
// precedence, but the other encoding info is allowed to fill in any
896
readToUnicodeCMap(fontDict, 8, ctu);
898
// look for a Unicode-to-Unicode mapping
899
if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
901
for (i = 0; i < 256; ++i) {
904
ctu2 = CharCodeToUnicode::make8BitToUnicode(toUnicode);
905
for (i = 0; i < 256; ++i) {
906
n = ctu->mapToUnicode((CharCode)i, &uAux);
908
n = utu->mapToUnicode((CharCode)uAux[0], &uAux);
910
ctu2->setMapping((CharCode)i, uAux, n);
919
//----- get the character widths -----
921
// initialize all widths
922
for (code = 0; code < 256; ++code) {
923
widths[code] = missingWidth * 0.001;
926
// use widths from font dict, if present
927
fontDict->lookup("FirstChar", &obj1);
928
firstChar = obj1.isInt() ? obj1.getInt() : 0;
930
if (firstChar < 0 || firstChar > 255) {
933
fontDict->lookup("LastChar", &obj1);
934
lastChar = obj1.isInt() ? obj1.getInt() : 255;
936
if (lastChar < 0 || lastChar > 255) {
939
mul = (type == fontType3) ? fontMat[0] : 0.001;
940
fontDict->lookup("Widths", &obj1);
941
if (obj1.isArray()) {
942
flags |= fontFixedWidth;
943
if (obj1.arrayGetLength() < lastChar - firstChar + 1) {
944
lastChar = firstChar + obj1.arrayGetLength() - 1;
946
for (code = firstChar; code <= lastChar; ++code) {
947
obj1.arrayGet(code - firstChar, &obj2);
949
widths[code] = obj2.getNum() * mul;
950
if (widths[code] != widths[firstChar]) {
951
flags &= ~fontFixedWidth;
957
// use widths from built-in font
958
} else if (builtinFont) {
959
// this is a kludge for broken PDF files that encode char 32
961
if (builtinFont->widths->getWidth("space", &w)) {
962
widths[32] = 0.001 * w;
964
for (code = 0; code < 256; ++code) {
965
if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
966
widths[code] = 0.001 * w;
970
// couldn't find widths -- use defaults
972
// this is technically an error -- the Widths entry is required
973
// for all but the Base-14 fonts -- but certain PDF generators
974
// apparently don't include widths for Arial and TimesNewRoman
975
if (isFixedWidth()) {
977
} else if (isSerif()) {
988
builtinFont = builtinFontSubst[i];
989
// this is a kludge for broken PDF files that encode char 32
991
if (builtinFont->widths->getWidth("space", &w)) {
992
widths[32] = 0.001 * w;
994
for (code = 0; code < 256; ++code) {
995
if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
996
widths[code] = 0.001 * w;
1005
Gfx8BitFont::~Gfx8BitFont() {
1008
for (i = 0; i < 256; ++i) {
1009
if (encFree[i] && enc[i]) {
1014
if (charProcs.isDict()) {
1017
if (resources.isDict()) {
1022
// This function is in part a derived work of the Adobe Glyph Mapping
1023
// Convention: http://www.adobe.com/devnet/opentype/archives/glyph.html
1024
// Algorithmic comments are excerpted from that document to aid
1026
static int parseCharName(char *charName, Unicode *uBuf, int uLen,
1027
GBool names, GBool ligatures,
1028
GBool numeric, GBool hex, GBool variants)
1031
error(-1, "Zero-length output buffer (recursion overflow?) in "
1032
"parseCharName, component \"%s\"", charName);
1035
// Step 1: drop all the characters from the glyph name starting with the
1036
// first occurrence of a period (U+002E FULL STOP), if any.
1038
char *var_part = strchr(charName, '.');
1039
if (var_part == charName) {
1040
return 0; // .notdef or similar
1041
} else if (var_part != NULL) {
1042
// parse names of the form 7.oldstyle, P.swash, s.sc, etc.
1043
char *main_part = gstrndup(charName, var_part - charName);
1044
GBool namesRecurse = gTrue, variantsRecurse = gFalse;
1045
int n = parseCharName(main_part, uBuf, uLen, namesRecurse, ligatures,
1046
numeric, hex, variantsRecurse);
1051
// Step 2: split the remaining string into a sequence of components, using
1052
// underscore (U+005F LOW LINE) as the delimiter.
1053
if (ligatures && strchr(charName, '_')) {
1054
// parse names of the form A_a (e.g. f_i, T_h, l_quotesingle)
1055
char *lig_part, *lig_end, *lig_copy;
1057
lig_part = lig_copy = copyString(charName);
1059
if ((lig_end = strchr(lig_part, '_')))
1061
if (lig_part[0] != '\0') {
1062
GBool namesRecurse = gTrue, ligaturesRecurse = gFalse;
1063
if ((m = parseCharName(lig_part, uBuf + n, uLen - n, namesRecurse,
1064
ligaturesRecurse, numeric, hex, variants)))
1067
error(-1, "Could not parse ligature component \"%s\" of \"%s\" in "
1068
"parseCharName", lig_part, charName);
1070
lig_part = lig_end + 1;
1071
} while (lig_end && n < uLen);
1075
// Step 3: map each component to a character string according to the
1076
// procedure below, and concatenate those strings; the result is the
1077
// character string to which the glyph name is mapped.
1078
// 3.1. if the font is Zapf Dingbats (PostScript FontName ZapfDingbats), and
1079
// the component is in the ZapfDingbats list, then map it to the
1080
// corresponding character in that list.
1081
// 3.2. otherwise, if the component is in the Adobe Glyph List, then map it
1082
// to the corresponding character in that list.
1083
if (names && (uBuf[0] = globalParams->mapNameToUnicode(charName))) {
1087
unsigned int n = strlen(charName);
1088
// 3.3. otherwise, if the component is of the form "uni" (U+0075 U+006E
1089
// U+0069) followed by a sequence of uppercase hexadecimal digits (0 .. 9,
1090
// A .. F, i.e. U+0030 .. U+0039, U+0041 .. U+0046), the length of that
1091
// sequence is a multiple of four, and each group of four digits represents
1092
// a number in the set {0x0000 .. 0xD7FF, 0xE000 .. 0xFFFF}, then interpret
1093
// each such number as a Unicode scalar value and map the component to the
1094
// string made of those scalar values. Note that the range and digit length
1095
// restrictions mean that the "uni" prefix can be used only with Unicode
1096
// values from the Basic Multilingual Plane (BMP).
1097
if (n >= 7 && (n % 4) == 3 && !strncmp(charName, "uni", 3)) {
1100
for (i = 0, m = 3; i < uLen && m < n; m += 4) {
1101
if (isxdigit(charName[m]) && isxdigit(charName[m + 1]) &&
1102
isxdigit(charName[m + 2]) && isxdigit(charName[m + 3])) {
1104
sscanf(charName + m, "%4x", &u);
1105
if (u <= 0xD7FF || (0xE000 <= u && u <= 0xFFFF)) {
1112
// 3.4. otherwise, if the component is of the form "u" (U+0075) followed by
1113
// a sequence of four to six uppercase hexadecimal digits {0 .. 9, A .. F}
1114
// (U+0030 .. U+0039, U+0041 .. U+0046), and those digits represent a
1115
// number in {0x0000 .. 0xD7FF, 0xE000 .. 0x10FFFF}, then interpret this
1116
// number as a Unicode scalar value and map the component to the string
1117
// made of this scalar value.
1118
if (n >= 5 && n <= 7 && charName[0] == 'u' && isxdigit(charName[1]) &&
1119
isxdigit(charName[2]) && isxdigit(charName[3]) && isxdigit(charName[4])
1120
&& (n <= 5 || isxdigit(charName[5]))
1121
&& (n <= 6 || isxdigit(charName[6]))) {
1123
sscanf(charName + 1, "%x", &u);
1124
if (u <= 0xD7FF || (0xE000 <= u && u <= 0x10FFFF)) {
1129
// Not in Adobe Glyph Mapping convention: look for names of the form 'Axx',
1130
// 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B' are any letters, 'xx' is
1131
// two hex digits, and 'nn' is 2-4 decimal digits
1132
if (hex && n == 3 && isalpha(charName[0]) &&
1133
isxdigit(charName[1]) && isxdigit(charName[2])) {
1134
sscanf(charName+1, "%x", (unsigned int *)uBuf);
1136
} else if (hex && n == 2 &&
1137
isxdigit(charName[0]) && isxdigit(charName[1])) {
1138
sscanf(charName, "%x", (unsigned int *)uBuf);
1140
} else if (!hex && n >= 2 && n <= 4 &&
1141
isdigit(charName[0]) && isdigit(charName[1])) {
1142
uBuf[0] = (Unicode)atoi(charName);
1144
} else if (n >= 3 && n <= 5 &&
1145
isdigit(charName[1]) && isdigit(charName[2])) {
1146
uBuf[0] = (Unicode)atoi(charName+1);
1148
} else if (n >= 4 && n <= 6 &&
1149
isdigit(charName[2]) && isdigit(charName[3])) {
1150
uBuf[0] = (Unicode)atoi(charName+2);
1154
// 3.5. otherwise, map the component to the empty string
1158
int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
1159
Unicode **u, int *uLen,
1160
double *dx, double *dy, double *ox, double *oy) {
1163
*code = c = (CharCode)(*s & 0xff);
1164
*uLen = ctu->mapToUnicode(c, u);
1166
*dy = *ox = *oy = 0;
1170
CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
1175
Gushort *Gfx8BitFont::getCodeToGIDMap(FoFiTrueType *ff) {
1177
int cmapPlatform, cmapEncoding;
1178
int unicodeCmap, macRomanCmap, msSymbolCmap, cmap;
1179
GBool useMacRoman, useUnicode;
1184
map = (Gushort *)gmallocn(256, sizeof(Gushort));
1185
for (i = 0; i < 256; ++i) {
1189
// To match up with the Adobe-defined behaviour, we choose a cmap
1191
// 1. If the PDF font has an encoding:
1192
// 1a. If the PDF font specified MacRomanEncoding and the
1193
// TrueType font has a Macintosh Roman cmap, use it, and
1194
// reverse map the char names through MacRomanEncoding to
1196
// 1b. If the TrueType font has a Microsoft Unicode cmap or a
1197
// non-Microsoft Unicode cmap, use it, and use the Unicode
1198
// indexes, not the char codes.
1199
// 1c. If the PDF font is symbolic and the TrueType font has a
1200
// Microsoft Symbol cmap, use it, and use char codes
1201
// directly (possibly with an offset of 0xf000).
1202
// 1d. If the TrueType font has a Macintosh Roman cmap, use it,
1204
// 2. If the PDF font does not have an encoding or the PDF font is
1206
// 2a. If the TrueType font has a Macintosh Roman cmap, use it,
1207
// and use char codes directly (possibly with an offset of
1209
// 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
1210
// and use char codes directly (possible with an offset of
1212
// 3. If none of these rules apply, use the first cmap and hope for
1213
// the best (this shouldn't happen).
1214
unicodeCmap = macRomanCmap = msSymbolCmap = -1;
1215
for (i = 0; i < ff->getNumCmaps(); ++i) {
1216
cmapPlatform = ff->getCmapPlatform(i);
1217
cmapEncoding = ff->getCmapEncoding(i);
1218
if ((cmapPlatform == 3 && cmapEncoding == 1) ||
1219
cmapPlatform == 0) {
1221
} else if (cmapPlatform == 1 && cmapEncoding == 0) {
1223
} else if (cmapPlatform == 3 && cmapEncoding == 0) {
1228
useMacRoman = gFalse;
1229
useUnicode = gFalse;
1231
if (usesMacRomanEnc && macRomanCmap >= 0) {
1232
cmap = macRomanCmap;
1233
useMacRoman = gTrue;
1234
} else if (unicodeCmap >= 0) {
1237
} else if ((flags & fontSymbolic) && msSymbolCmap >= 0) {
1238
cmap = msSymbolCmap;
1239
} else if ((flags & fontSymbolic) && macRomanCmap >= 0) {
1240
cmap = macRomanCmap;
1241
} else if (macRomanCmap >= 0) {
1242
cmap = macRomanCmap;
1243
useMacRoman = gTrue;
1246
if (msSymbolCmap >= 0) {
1247
cmap = msSymbolCmap;
1248
} else if (macRomanCmap >= 0) {
1249
cmap = macRomanCmap;
1253
// reverse map the char names through MacRomanEncoding, then map the
1254
// char codes through the cmap
1256
for (i = 0; i < 256; ++i) {
1257
if ((charName = enc[i])) {
1258
if ((code = globalParams->getMacRomanCharCode(charName))) {
1259
map[i] = ff->mapCodeToGID(cmap, code);
1264
// map Unicode through the cmap
1265
} else if (useUnicode) {
1267
for (i = 0; i < 256; ++i) {
1268
if (((charName = enc[i]) && (u = globalParams->mapNameToUnicode(charName))))
1269
map[i] = ff->mapCodeToGID(cmap, u);
1272
n = ctu->mapToUnicode((CharCode)i, &uAux);
1273
if (n > 0) map[i] = ff->mapCodeToGID(cmap, uAux[0]);
1277
// map the char codes through the cmap, possibly with an offset of
1280
for (i = 0; i < 256; ++i) {
1281
if (!(map[i] = ff->mapCodeToGID(cmap, i))) {
1282
map[i] = ff->mapCodeToGID(cmap, 0xf000 + i);
1287
// try the TrueType 'post' table to handle any unmapped characters
1288
for (i = 0; i < 256; ++i) {
1289
if (!map[i] && (charName = enc[i])) {
1290
map[i] = (Gushort)(int)ff->mapNameToGID(charName);
1297
Dict *Gfx8BitFont::getCharProcs() {
1298
return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
1301
Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
1302
if (enc[code] && charProcs.isDict()) {
1303
charProcs.dictLookup(enc[code], proc);
1310
Dict *Gfx8BitFont::getResources() {
1311
return resources.isDict() ? resources.getDict() : (Dict *)NULL;
1314
//------------------------------------------------------------------------
1316
//------------------------------------------------------------------------
1318
static int CDECL cmpWidthExcep(const void *w1, const void *w2) {
1319
return ((GfxFontCIDWidthExcep *)w1)->first -
1320
((GfxFontCIDWidthExcep *)w2)->first;
1323
static int CDECL cmpWidthExcepV(const void *w1, const void *w2) {
1324
return ((GfxFontCIDWidthExcepV *)w1)->first -
1325
((GfxFontCIDWidthExcepV *)w2)->first;
1328
GfxCIDFont::GfxCIDFont(XRef *xref, char *tagA, Ref idA, GooString *nameA,
1330
GfxFont(tagA, idA, nameA)
1333
GooString *collection, *cMapName;
1334
Object desFontDictObj;
1335
Object obj1, obj2, obj3, obj4, obj5, obj6;
1336
CharCodeToUnicode *utu;
1340
int excepsSize, i, j, k, n;
1345
fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
1348
widths.defWidth = 1.0;
1349
widths.defHeight = -1.0;
1350
widths.defVY = 0.880;
1351
widths.exceps = NULL;
1353
widths.excepsV = NULL;
1354
widths.nExcepsV = 0;
1358
// get the descendant font
1359
if (!fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
1360
error(-1, "Missing DescendantFonts entry in Type 0 font");
1364
if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
1365
error(-1, "Bad descendant font in Type 0 font");
1369
desFontDict = desFontDictObj.getDict();
1372
if (!desFontDict->lookup("Subtype", &obj1)) {
1373
error(-1, "Missing Subtype entry in Type 0 descendant font");
1376
if (obj1.isName("CIDFontType0")) {
1377
type = fontCIDType0;
1378
} else if (obj1.isName("CIDFontType2")) {
1379
type = fontCIDType2;
1381
error(-1, "Unknown Type 0 descendant font type '%s'",
1382
obj1.isName() ? obj1.getName() : "???");
1387
// get info from font descriptor
1388
readFontDescriptor(xref, desFontDict);
1390
// look for an external font file
1393
//----- encoding info -----
1396
if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
1397
error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
1400
obj1.dictLookup("Registry", &obj2);
1401
obj1.dictLookup("Ordering", &obj3);
1402
if (!obj2.isString() || !obj3.isString()) {
1403
error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
1406
collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
1411
// look for a ToUnicode CMap
1412
if (!(ctu = readToUnicodeCMap(fontDict, 16, NULL))) {
1414
// the "Adobe-Identity" and "Adobe-UCS" collections don't have
1415
// cidToUnicode files
1416
if (collection->cmp("Adobe-Identity") &&
1417
collection->cmp("Adobe-UCS")) {
1419
// look for a user-supplied .cidToUnicode file
1420
if (!(ctu = globalParams->getCIDToUnicode(collection))) {
1421
// I'm not completely sure that this is the best thing to do
1422
// but it seems to produce better results when the .cidToUnicode
1423
// files from the poppler-data package are missing. At least
1424
// we know that assuming the Identity mapping is definitely wrong.
1426
static const char * knownCollections [] = {
1433
for (size_t i = 0; i < sizeof(knownCollections)/sizeof(knownCollections[0]); i++) {
1434
if (collection->cmp(knownCollections[i]) == 0) {
1435
error(-1, "Missing language pack for '%s' mapping", collection->getCString());
1440
error(-1, "Unknown character collection '%s'",
1441
collection->getCString());
1442
// fall-through, assuming the Identity mapping -- this appears
1443
// to match Adobe's behavior
1448
// look for a Unicode-to-Unicode mapping
1449
if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
1451
for (c = 0; c < ctu->getLength(); ++c) {
1452
n = ctu->mapToUnicode(c, &uBuf);
1454
n = utu->mapToUnicode((CharCode)uBuf[0], &uBuf);
1456
ctu->setMapping(c, uBuf, n);
1466
// encoding (i.e., CMap)
1467
//~ also need to deal with the UseCMap entry in the stream dict
1468
if (!fontDict->lookup("Encoding", &obj1)->isName()) {
1469
GBool success = gFalse;
1470
if (obj1.isStream()) {
1472
Stream *s = obj1.getStream();
1473
s->getDict()->lookup("CMapName", &objName);
1474
if (objName.isName())
1476
cMapName = new GooString(objName.getName());
1477
cMap = globalParams->getCMap(collection, cMapName, s);
1484
error(-1, "Missing or invalid Encoding entry in Type 0 font");
1489
cMapName = new GooString(obj1.getName());
1490
cMap = globalParams->getCMap(collection, cMapName);
1495
error(-1, "Unknown CMap '%s' for character collection '%s'",
1496
cMapName->getCString(), collection->getCString());
1501
// CIDToGIDMap (for embedded TrueType fonts)
1502
if (type == fontCIDType2 || type == fontCIDType2OT) {
1503
desFontDict->lookup("CIDToGIDMap", &obj1);
1504
if (obj1.isStream()) {
1507
cidToGID = (Gushort *)gmallocn(i, sizeof(Gushort));
1509
while ((c1 = obj1.streamGetChar()) != EOF &&
1510
(c2 = obj1.streamGetChar()) != EOF) {
1511
if (cidToGIDLen == i) {
1513
cidToGID = (Gushort *)greallocn(cidToGID, i, sizeof(Gushort));
1515
cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
1517
} else if (!obj1.isName("Identity") && !obj1.isNull()) {
1518
error(-1, "Invalid CIDToGIDMap entry in CID font");
1523
//----- character metrics -----
1525
// default char width
1526
if (desFontDict->lookup("DW", &obj1)->isInt()) {
1527
widths.defWidth = obj1.getInt() * 0.001;
1531
// char width exceptions
1532
if (desFontDict->lookup("W", &obj1)->isArray()) {
1535
while (i + 1 < obj1.arrayGetLength()) {
1536
obj1.arrayGet(i, &obj2);
1537
obj1.arrayGet(i + 1, &obj3);
1538
if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
1539
if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
1540
if (widths.nExceps == excepsSize) {
1542
widths.exceps = (GfxFontCIDWidthExcep *)
1543
greallocn(widths.exceps,
1544
excepsSize, sizeof(GfxFontCIDWidthExcep));
1546
widths.exceps[widths.nExceps].first = obj2.getInt();
1547
widths.exceps[widths.nExceps].last = obj3.getInt();
1548
widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1551
error(-1, "Bad widths array in Type 0 font");
1555
} else if (obj2.isInt() && obj3.isArray()) {
1556
if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1557
excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1558
widths.exceps = (GfxFontCIDWidthExcep *)
1559
greallocn(widths.exceps,
1560
excepsSize, sizeof(GfxFontCIDWidthExcep));
1563
for (k = 0; k < obj3.arrayGetLength(); ++k) {
1564
if (obj3.arrayGet(k, &obj4)->isNum()) {
1565
widths.exceps[widths.nExceps].first = j;
1566
widths.exceps[widths.nExceps].last = j;
1567
widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1571
error(-1, "Bad widths array in Type 0 font");
1577
error(-1, "Bad widths array in Type 0 font");
1583
qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
1588
// default metrics for vertical font
1589
if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1590
obj1.arrayGetLength() == 2) {
1591
if (obj1.arrayGet(0, &obj2)->isNum()) {
1592
widths.defVY = obj2.getNum() * 0.001;
1595
if (obj1.arrayGet(1, &obj2)->isNum()) {
1596
widths.defHeight = obj2.getNum() * 0.001;
1602
// char metric exceptions for vertical font
1603
if (desFontDict->lookup("W2", &obj1)->isArray()) {
1606
while (i + 1 < obj1.arrayGetLength()) {
1607
obj1.arrayGet(i, &obj2);
1608
obj1.arrayGet(i+ 1, &obj3);
1609
if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1610
if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1611
obj1.arrayGet(i + 3, &obj5)->isNum() &&
1612
obj1.arrayGet(i + 4, &obj6)->isNum()) {
1613
if (widths.nExcepsV == excepsSize) {
1615
widths.excepsV = (GfxFontCIDWidthExcepV *)
1616
greallocn(widths.excepsV,
1617
excepsSize, sizeof(GfxFontCIDWidthExcepV));
1619
widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1620
widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1621
widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1622
widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1623
widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1626
error(-1, "Bad widths (W2) array in Type 0 font");
1632
} else if (obj2.isInt() && obj3.isArray()) {
1633
if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
1635
(widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
1636
widths.excepsV = (GfxFontCIDWidthExcepV *)
1637
greallocn(widths.excepsV,
1638
excepsSize, sizeof(GfxFontCIDWidthExcepV));
1641
for (k = 0; k < obj3.arrayGetLength(); k += 3) {
1642
if (obj3.arrayGet(k, &obj4)->isNum() &&
1643
obj3.arrayGet(k+1, &obj5)->isNum() &&
1644
obj3.arrayGet(k+2, &obj6)->isNum()) {
1645
widths.excepsV[widths.nExcepsV].first = j;
1646
widths.excepsV[widths.nExcepsV].last = j;
1647
widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1648
widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1649
widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1653
error(-1, "Bad widths (W2) array in Type 0 font");
1661
error(-1, "Bad widths (W2) array in Type 0 font");
1667
qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
1672
desFontDictObj.free();
1682
desFontDictObj.free();
1686
GfxCIDFont::~GfxCIDFont() {
1693
gfree(widths.exceps);
1694
gfree(widths.excepsV);
1700
int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
1701
Unicode **u, int *uLen,
1702
double *dx, double *dy, double *ox, double *oy) {
1704
double w, h, vx, vy;
1714
*code = (CharCode)(cid = cMap->getCID(s, len, &n));
1716
*uLen = ctu->mapToUnicode(cid, u);
1722
if (cMap->getWMode() == 0) {
1723
w = widths.defWidth;
1725
if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
1728
// invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1731
if (widths.exceps[m].first <= cid) {
1737
if (cid <= widths.exceps[a].last) {
1738
w = widths.exceps[a].width;
1745
h = widths.defHeight;
1746
vx = widths.defWidth / 2;
1748
if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
1750
b = widths.nExcepsV;
1751
// invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1754
if (widths.excepsV[m].last <= cid) {
1760
if (cid <= widths.excepsV[a].last) {
1761
h = widths.excepsV[a].height;
1762
vx = widths.excepsV[a].vx;
1763
vy = widths.excepsV[a].vy;
1776
int GfxCIDFont::getWMode() {
1777
return cMap ? cMap->getWMode() : 0;
1780
CharCodeToUnicode *GfxCIDFont::getToUnicode() {
1787
GooString *GfxCIDFont::getCollection() {
1788
return cMap ? cMap->getCollection() : (GooString *)NULL;
1791
Gushort GfxCIDFont::mapCodeToGID(FoFiTrueType *ff, int cmapi,
1792
Unicode unicode, GBool wmode) {
1793
Gushort gid = ff->mapCodeToGID(cmapi,unicode);
1795
Gushort vgid = ff->mapToVertGID(gid);
1796
if (vgid != 0) gid = vgid;
1801
Gushort *GfxCIDFont::getCodeToGIDMap(FoFiTrueType *ff, int *mapsizep) {
1802
#define N_UCS_CANDIDATES 2
1803
/* space characters */
1804
static const unsigned long spaces[] = {
1805
0x2000,0x2001,0x2002,0x2003,0x2004,0x2005,0x2006,0x2007,
1806
0x2008,0x2009,0x200A,0x00A0,0x200B,0x2060,0x3000,0xFEFF,
1809
static char *adobe_cns1_cmaps[] = {
1816
static char *adobe_gb1_cmaps[] = {
1823
static char *adobe_japan1_cmaps[] = {
1830
static char *adobe_japan2_cmaps[] = {
1837
static char *adobe_korea1_cmaps[] = {
1844
static struct CMapListEntry {
1865
"Adobe-Japan1-UCS2",
1871
"Adobe-Japan2-UCS2",
1877
"Adobe-Korea1-UCS2",
1885
Gushort *codeToGID = 0;
1894
int cmapPlatform, cmapEncoding;
1898
if (!ctu) return NULL;
1899
if (getCollection()->cmp("Adobe-Identity") == 0) return NULL;
1900
if (getEmbeddedFontID(&embID)) {
1901
/* if this font is embedded font,
1902
* CIDToGIDMap should be embedded in PDF file
1903
* and already set. So return it.
1905
*mapsizep = getCIDToGIDLen();
1906
return getCIDToGID();
1909
/* we use only unicode cmap */
1911
for (i = 0; i < ff->getNumCmaps(); ++i) {
1912
cmapPlatform = ff->getCmapPlatform(i);
1913
cmapEncoding = ff->getCmapEncoding(i);
1914
if (cmapPlatform == 3 && cmapEncoding == 10) {
1917
/* use UCS-4 cmap */
1919
} else if (cmapPlatform == 3 && cmapEncoding == 1) {
1922
} else if (cmapPlatform == 0 && cmap < 0) {
1930
for (lp = CMapList;lp->collection != 0;lp++) {
1931
if (strcmp(lp->collection,getCollection()->getCString()) == 0) {
1936
tumap = new Unicode[n];
1937
humap = new Unicode[n*N_UCS_CANDIDATES];
1938
memset(humap,0,sizeof(Unicode)*n*N_UCS_CANDIDATES);
1939
if (lp->collection != 0) {
1940
CharCodeToUnicode *tctu;
1941
GooString tname(lp->toUnicodeMap);
1943
if ((tctu = CharCodeToUnicode::parseCMapFromFile(&tname,16)) != 0) {
1945
for (cid = 0;cid < n ;cid++) {
1949
len = tctu->mapToUnicode(cid,&ucodes);
1951
tumap[cid] = ucodes[0];
1953
/* if not single character, ignore it */
1959
vumap = new Unicode[n];
1960
memset(vumap,0,sizeof(Unicode)*n);
1961
for (cmapName = lp->CMaps;*cmapName != 0;cmapName++) {
1962
GooString cname(*cmapName);
1964
if ((cMap = globalParams->getCMap(getCollection(),&cname))
1966
if (cMap->getWMode()) {
1967
cMap->setReverseMap(vumap,n,1);
1969
cMap->setReverseMap(humap,n,N_UCS_CANDIDATES);
1974
ff->setupGSUB(lp->scriptTag);
1976
error(-1,"Unknown character collection %s\n",
1977
getCollection()->getCString());
1978
if ((ctu = getToUnicode()) != 0) {
1980
for (cid = 0;cid < n ;cid++) {
1983
if (ctu->mapToUnicode(cid, &ucode))
1984
humap[cid*N_UCS_CANDIDATES] = ucode[0];
1986
humap[cid*N_UCS_CANDIDATES] = 0;
1987
for (i = 1;i < N_UCS_CANDIDATES;i++) {
1988
humap[cid*N_UCS_CANDIDATES+i] = 0;
1994
// map CID -> Unicode -> GID
1995
codeToGID = (Gushort *)gmallocn(n, sizeof(Gushort));
1996
for (code = 0; code < n; ++code) {
2003
for (i = 0;i < N_UCS_CANDIDATES
2004
&& gid == 0 && (unicode = humap[code*N_UCS_CANDIDATES+i]) != 0;i++) {
2005
gid = mapCodeToGID(ff,cmap,unicode,gFalse);
2008
if (gid == 0 && vumap != 0) {
2009
unicode = vumap[code];
2011
gid = mapCodeToGID(ff,cmap,unicode,gTrue);
2012
if (gid == 0 && tumap != 0) {
2013
if ((unicode = tumap[code]) != 0) {
2014
gid = mapCodeToGID(ff,cmap,unicode,gTrue);
2019
if (gid == 0 && tumap != 0) {
2020
if ((unicode = tumap[code]) != 0) {
2021
gid = mapCodeToGID(ff,cmap,unicode,gFalse);
2025
/* special handling space characters */
2026
const unsigned long *p;
2028
if (humap != 0) unicode = humap[code];
2030
/* check if code is space character , so map code to 0x0020 */
2031
for (p = spaces;*p != 0;p++) {
2032
if (*p == unicode) {
2034
gid = mapCodeToGID(ff,cmap,unicode,wmode);
2040
codeToGID[code] = gid;
2043
if (humap != 0) delete[] humap;
2044
if (tumap != 0) delete[] tumap;
2045
if (vumap != 0) delete[] vumap;
2049
double GfxCIDFont::getWidth (char* s, int len) {
2054
CID cid = cMap->getCID(s, len, &nUsed);
2056
w = widths.defWidth;
2057
if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
2060
// invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
2063
if (widths.exceps[m].first <= cid) {
2069
if (cid <= widths.exceps[a].last) {
2070
w = widths.exceps[a].width;
2076
//------------------------------------------------------------------------
2078
//------------------------------------------------------------------------
2080
GfxFontDict::GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict) {
2085
numFonts = fontDict->getLength();
2086
fonts = (GfxFont **)gmallocn(numFonts, sizeof(GfxFont *));
2087
for (i = 0; i < numFonts; ++i) {
2088
fontDict->getValNF(i, &obj1);
2089
obj1.fetch(xref, &obj2);
2090
if (obj2.isDict()) {
2094
// no indirect reference for this font, so invent a unique one
2095
// (legal generation numbers are five digits, so any 6-digit
2096
// number would be safe)
2099
r.gen = 100000 + fontDictRef->num;
2104
fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i),
2106
if (fonts[i] && !fonts[i]->isOk()) {
2107
// XXX: it may be meaningful to distinguish between
2108
// NULL and !isOk() so that when we do lookups
2109
// we can tell the difference between a missing font
2110
// and a font that is just !isOk()
2111
fonts[i]->decRefCnt();
2115
error(-1, "font resource is not a dictionary");
2123
GfxFontDict::~GfxFontDict() {
2126
for (i = 0; i < numFonts; ++i) {
2128
fonts[i]->decRefCnt();
2134
GfxFont *GfxFontDict::lookup(char *tag) {
2137
for (i = 0; i < numFonts; ++i) {
2138
if (fonts[i] && fonts[i]->matches(tag)) {