1 //========================================================================
5 // Copyright 1996-2002 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"
31 //------------------------------------------------------------------------
33 struct StdFontMapEntry {
38 static StdFontMapEntry stdFontMap[] = {
39 { "Arial", "Helvetica" },
40 { "Arial,Bold", "Helvetica-Bold" },
41 { "Arial,BoldItalic", "Helvetica-BoldOblique" },
42 { "Arial,Italic", "Helvetica-Oblique" },
43 { "Arial-Bold", "Helvetica-Bold" },
44 { "Arial-BoldItalic", "Helvetica-BoldOblique" },
45 { "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
46 { "Arial-BoldMT", "Helvetica-Bold" },
47 { "Arial-Italic", "Helvetica-Oblique" },
48 { "Arial-ItalicMT", "Helvetica-Oblique" },
49 { "ArialMT", "Helvetica" },
50 { "Courier,Bold", "Courier-Bold" },
51 { "Courier,Italic", "Courier-Oblique" },
52 { "Courier,BoldItalic", "Courier-BoldOblique" },
53 { "CourierNew", "Courier" },
54 { "CourierNew,Bold", "Courier-Bold" },
55 { "CourierNew,BoldItalic", "Courier-BoldOblique" },
56 { "CourierNew,Italic", "Courier-Oblique" },
57 { "CourierNew-Bold", "Courier-Bold" },
58 { "CourierNew-BoldItalic", "Courier-BoldOblique" },
59 { "CourierNew-Italic", "Courier-Oblique" },
60 { "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
61 { "CourierNewPS-BoldMT", "Courier-Bold" },
62 { "CourierNewPS-ItalicMT", "Courier-Oblique" },
63 { "CourierNewPSMT", "Courier" },
64 { "Helvetica,Bold", "Helvetica-Bold" },
65 { "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
66 { "Helvetica,Italic", "Helvetica-Oblique" },
67 { "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
68 { "Helvetica-Italic", "Helvetica-Oblique" },
69 { "TimesNewRoman", "Times-Roman" },
70 { "TimesNewRoman,Bold", "Times-Bold" },
71 { "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
72 { "TimesNewRoman,Italic", "Times-Italic" },
73 { "TimesNewRoman-Bold", "Times-Bold" },
74 { "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
75 { "TimesNewRoman-Italic", "Times-Italic" },
76 { "TimesNewRomanPS", "Times-Roman" },
77 { "TimesNewRomanPS-Bold", "Times-Bold" },
78 { "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
79 { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
80 { "TimesNewRomanPS-BoldMT", "Times-Bold" },
81 { "TimesNewRomanPS-Italic", "Times-Italic" },
82 { "TimesNewRomanPS-ItalicMT", "Times-Italic" },
83 { "TimesNewRomanPSMT", "Times-Roman" }
86 //------------------------------------------------------------------------
88 //------------------------------------------------------------------------
90 GfxFont *GfxFont::makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict) {
97 fontDict->lookup("BaseFont", &obj1);
99 nameA = new GString(obj1.getName());
105 fontDict->lookup("Subtype", &obj1);
106 if (obj1.isName("Type1") || obj1.isName("MMType1")) {
107 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
108 } else if (obj1.isName("Type1C")) {
109 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
110 } else if (obj1.isName("Type3")) {
111 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
112 } else if (obj1.isName("TrueType")) {
113 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
114 } else if (obj1.isName("Type0")) {
115 font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
117 error(-1, "Unknown font type: '%s'",
118 obj1.isName() ? obj1.getName() : "???");
119 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
126 GfxFont::GfxFont(char *tagA, Ref idA, GString *nameA) {
128 tag = new GString(tagA);
135 GfxFont::~GfxFont() {
148 void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
149 Object obj1, obj2, obj3, obj4;
153 // assume Times-Roman by default (for substitution purposes)
160 if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
163 if (obj1.dictLookup("Flags", &obj2)->isInt()) {
164 flags = obj2.getInt();
169 obj1.dictLookup("FontName", &obj2);
171 embFontName = new GString(obj2.getName());
175 // look for embedded font file
176 if (obj1.dictLookupNF("FontFile", &obj2)->isRef()) {
177 if (type == fontType1) {
178 embFontID = obj2.getRef();
180 error(-1, "Mismatch between font type and embedded font file");
184 if (embFontID.num == -1 &&
185 obj1.dictLookupNF("FontFile2", &obj2)->isRef()) {
186 if (type == fontTrueType || type == fontCIDType2) {
187 embFontID = obj2.getRef();
189 error(-1, "Mismatch between font type and embedded font file");
193 if (embFontID.num == -1 &&
194 obj1.dictLookupNF("FontFile3", &obj2)->isRef()) {
195 if (obj2.fetch(xref, &obj3)->isStream()) {
196 obj3.streamGetDict()->lookup("Subtype", &obj4);
197 if (obj4.isName("Type1")) {
198 if (type == fontType1) {
199 embFontID = obj2.getRef();
201 error(-1, "Mismatch between font type and embedded font file");
203 } else if (obj4.isName("Type1C")) {
204 if (type == fontType1) {
206 embFontID = obj2.getRef();
207 } else if (type == fontType1C) {
208 embFontID = obj2.getRef();
210 error(-1, "Mismatch between font type and embedded font file");
212 } else if (obj4.isName("TrueType")) {
213 if (type == fontTrueType) {
214 embFontID = obj2.getRef();
216 error(-1, "Mismatch between font type and embedded font file");
218 } else if (obj4.isName("CIDFontType0C")) {
219 if (type == fontCIDType0) {
220 type = fontCIDType0C;
221 embFontID = obj2.getRef();
223 error(-1, "Mismatch between font type and embedded font file");
226 error(-1, "Unknown embedded font type '%s'",
227 obj4.isName() ? obj4.getName() : "???");
235 // look for MissingWidth
236 obj1.dictLookup("MissingWidth", &obj2);
238 missingWidth = obj2.getNum();
242 // get Ascent and Descent
243 obj1.dictLookup("Ascent", &obj2);
245 t = 0.001 * obj2.getNum();
246 // some broken font descriptors set ascent and descent to 0
252 obj1.dictLookup("Descent", &obj2);
254 t = 0.001 * obj2.getNum();
255 // some broken font descriptors set ascent and descent to 0
263 if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
264 for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
265 if (obj2.arrayGet(i, &obj3)->isNum()) {
266 fontBBox[i] = 0.001 * obj3.getNum();
277 CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits) {
278 CharCodeToUnicode *ctu;
283 if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
289 while ((c = obj1.streamGetChar()) != EOF) {
294 ctu = CharCodeToUnicode::parseCMap(buf, nBits);
299 void GfxFont::findExtFontFile() {
301 if (type == fontType1) {
302 extFontFile = globalParams->findFontFile(name, ".pfa", ".pfb");
303 } else if (type == fontTrueType) {
304 extFontFile = globalParams->findFontFile(name, ".ttf", NULL);
309 char *GfxFont::readExtFontFile(int *len) {
313 if (!(f = fopen(extFontFile->getCString(), "rb"))) {
314 error(-1, "External font file '%s' vanished", extFontFile->getCString());
317 fseek(f, 0, SEEK_END);
318 *len = (int)ftell(f);
319 fseek(f, 0, SEEK_SET);
320 buf = (char *)gmalloc(*len);
321 if ((int)fread(buf, 1, *len, f) != *len) {
322 error(-1, "Error reading external font file '%s'", extFontFile);
328 char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
335 obj1.initRef(embFontID.num, embFontID.gen);
336 obj1.fetch(xref, &obj2);
337 if (!obj2.isStream()) {
338 error(-1, "Embedded font file is not a stream");
344 str = obj2.getStream();
349 while ((c = str->getChar()) != EOF) {
352 buf = (char *)grealloc(buf, size);
365 //------------------------------------------------------------------------
367 //------------------------------------------------------------------------
369 Gfx8BitFont::Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
370 GfxFontType typeA, Dict *fontDict):
371 GfxFont(tagA, idA, nameA)
373 BuiltinFont *builtinFont;
375 GBool baseEncFromFontFile;
382 Unicode toUnicode[256];
384 int firstChar, lastChar;
386 Object obj1, obj2, obj3;
392 // Acrobat 4.0 and earlier substituted Base14-compatible fonts
393 // without providing Widths and a FontDescriptor, so we munge the
394 // names into the proper Base14 names. (This table is from
395 // implementation note 44 in the PDF 1.4 spec.)
398 b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
399 // invariant: stdFontMap[a].altName <= name < stdFontMap[b].altName
402 if (name->cmp(stdFontMap[m].altName) >= 0) {
408 if (!name->cmp(stdFontMap[a].altName)) {
410 name = new GString(stdFontMap[a].properName);
414 // is it a built-in font?
417 for (i = 0; i < nBuiltinFonts; ++i) {
418 if (!name->cmp(builtinFonts[i].name)) {
419 builtinFont = &builtinFonts[i];
425 // default ascent/descent values
427 ascent = 0.001 * builtinFont->ascent;
428 descent = 0.001 * builtinFont->descent;
429 fontBBox[0] = 0.001 * builtinFont->bbox[0];
430 fontBBox[1] = 0.001 * builtinFont->bbox[1];
431 fontBBox[2] = 0.001 * builtinFont->bbox[2];
432 fontBBox[3] = 0.001 * builtinFont->bbox[3];
436 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
439 // get info from font descriptor
440 readFontDescriptor(xref, fontDict);
442 // look for an external font file
446 fontMat[0] = fontMat[3] = 1;
447 fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
448 if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
449 for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
450 if (obj1.arrayGet(i, &obj2)->isNum()) {
451 fontMat[i] = obj2.getNum();
458 // get Type 3 bounding box, font definition, and resources
459 if (type == fontType3) {
460 if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
461 for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
462 if (obj1.arrayGet(i, &obj2)->isNum()) {
463 fontBBox[i] = obj2.getNum();
469 if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
470 error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
473 if (!fontDict->lookup("Resources", &resources)->isDict()) {
478 //----- build the font encoding -----
480 // Encodings start with a base encoding, which can come from
481 // (in order of priority):
482 // 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
483 // - MacRoman / MacExpert / WinAnsi / Standard
484 // 2. embedded or external font file
486 // - builtin --> builtin encoding
487 // - TrueType --> MacRomanEncoding
488 // - others --> StandardEncoding
489 // and then add a list of differences (if any) from
490 // FontDict.Encoding.Differences.
492 // check FontDict for base encoding
493 hasEncoding = gFalse;
495 baseEncFromFontFile = gFalse;
496 fontDict->lookup("Encoding", &obj1);
498 obj1.dictLookup("BaseEncoding", &obj2);
499 if (obj2.isName("MacRomanEncoding")) {
501 baseEnc = macRomanEncoding;
502 } else if (obj2.isName("MacExpertEncoding")) {
504 baseEnc = macExpertEncoding;
505 } else if (obj2.isName("WinAnsiEncoding")) {
507 baseEnc = winAnsiEncoding;
508 } else if (obj2.isName("StandardEncoding")) {
510 baseEnc = standardEncoding;
513 } else if (obj1.isName("MacRomanEncoding")) {
515 baseEnc = macRomanEncoding;
516 } else if (obj1.isName("MacExpertEncoding")) {
518 baseEnc = macExpertEncoding;
519 } else if (obj1.isName("WinAnsiEncoding")) {
521 baseEnc = winAnsiEncoding;
522 } else if (obj1.isName("StandardEncoding")) {
524 baseEnc = standardEncoding;
527 // check embedded or external font file for base encoding
528 // (only for Type 1 fonts - trying to get an encoding out of a
529 // TrueType font is a losing proposition)
532 if ((type == fontType1 || type == fontType1C) &&
533 (extFontFile || embFontID.num >= 0)) {
535 buf = readExtFontFile(&len);
537 buf = readEmbFontFile(xref, &len);
540 if (type == fontType1C && !strncmp(buf, "%!", 2)) {
541 // various tools (including Adobe's) occasionally embed Type 1
542 // fonts but label them Type 1C
545 if (type == fontType1) {
546 fontFile = new Type1FontFile(buf, len);
548 fontFile = new Type1CFontFile(buf, len);
550 if (fontFile->getName()) {
554 embFontName = new GString(fontFile->getName());
557 baseEnc = fontFile->getEncoding();
558 baseEncFromFontFile = gTrue;
564 // get default base encoding
567 baseEnc = builtinFont->defaultBaseEnc;
569 } else if (type == fontTrueType) {
570 baseEnc = winAnsiEncoding;
572 baseEnc = standardEncoding;
576 // copy the base encoding
577 for (i = 0; i < 256; ++i) {
579 if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
580 enc[i] = copyString(baseEnc[i]);
584 // merge differences into encoding
586 obj1.dictLookup("Differences", &obj2);
587 if (obj2.isArray()) {
590 for (i = 0; i < obj2.arrayGetLength(); ++i) {
591 obj2.arrayGet(i, &obj3);
593 code = obj3.getInt();
594 } else if (obj3.isName()) {
599 enc[code] = copyString(obj3.getName());
600 encFree[code] = gTrue;
604 error(-1, "Wrong type in font encoding resource differences (%s)",
617 //----- build the mapping to Unicode -----
619 // look for a ToUnicode CMap
620 if (!(ctu = readToUnicodeCMap(fontDict, 8))) {
622 // no ToUnicode CMap, so use the char names
624 // pass 1: use the name-to-Unicode mapping table
625 missing = hex = gFalse;
626 for (code = 0; code < 256; ++code) {
627 if ((charName = enc[code])) {
628 if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
629 strcmp(charName, ".notdef")) {
630 // if it wasn't in the name-to-Unicode table, check for a
631 // name that looks like 'Axx' or 'xx', where 'A' is any letter
632 // and 'xx' is two hex digits
633 if ((strlen(charName) == 3 &&
634 isalpha(charName[0]) &&
635 isxdigit(charName[1]) && isxdigit(charName[2]) &&
636 ((charName[1] >= 'a' && charName[1] <= 'f') ||
637 (charName[1] >= 'A' && charName[1] <= 'F') ||
638 (charName[2] >= 'a' && charName[2] <= 'f') ||
639 (charName[2] >= 'A' && charName[2] <= 'F'))) ||
640 (strlen(charName) == 2 &&
641 isxdigit(charName[0]) && isxdigit(charName[1]) &&
642 ((charName[0] >= 'a' && charName[0] <= 'f') ||
643 (charName[0] >= 'A' && charName[0] <= 'F') ||
644 (charName[1] >= 'a' && charName[1] <= 'f') ||
645 (charName[1] >= 'A' && charName[1] <= 'F')))) {
655 // pass 2: try to fill in the missing chars, looking for names of
656 // the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
657 // are any letters, 'xx' is two hex digits, and 'nn' is 2-4
659 if (missing && globalParams->getMapNumericCharNames()) {
660 for (code = 0; code < 256; ++code) {
661 if ((charName = enc[code]) && !toUnicode[code] &&
662 strcmp(charName, ".notdef")) {
663 n = strlen(charName);
665 if (hex && n == 3 && isalpha(charName[0]) &&
666 isxdigit(charName[1]) && isxdigit(charName[2])) {
667 sscanf(charName+1, "%x", &code2);
668 } else if (hex && n == 2 &&
669 isxdigit(charName[0]) && isxdigit(charName[1])) {
670 sscanf(charName, "%x", &code2);
671 } else if (!hex && n >= 2 && n <= 4 &&
672 isdigit(charName[0]) && isdigit(charName[1])) {
673 code2 = atoi(charName);
674 } else if (n >= 3 && n <= 5 &&
675 isdigit(charName[1]) && isdigit(charName[2])) {
676 code2 = atoi(charName+1);
677 } else if (n >= 4 && n <= 6 &&
678 isdigit(charName[2]) && isdigit(charName[3])) {
679 code2 = atoi(charName+2);
681 if (code2 >= 0 && code2 <= 0xff) {
682 toUnicode[code] = (Unicode)code2;
688 ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
691 //----- get the character widths -----
693 // initialize all widths
694 for (code = 0; code < 256; ++code) {
695 widths[code] = missingWidth * 0.001;
698 // use widths from font dict, if present
699 fontDict->lookup("FirstChar", &obj1);
700 firstChar = obj1.isInt() ? obj1.getInt() : 0;
702 fontDict->lookup("LastChar", &obj1);
703 lastChar = obj1.isInt() ? obj1.getInt() : 255;
705 mul = (type == fontType3) ? fontMat[0] : 0.001;
706 fontDict->lookup("Widths", &obj1);
707 if (obj1.isArray()) {
708 flags |= fontFixedWidth;
709 for (code = firstChar; code <= lastChar; ++code) {
710 obj1.arrayGet(code - firstChar, &obj2);
712 widths[code] = obj2.getNum() * mul;
713 if (widths[code] != widths[firstChar]) {
714 flags &= ~fontFixedWidth;
720 // use widths from built-in font
721 } else if (builtinFont) {
722 // this is a kludge for broken PDF files that encode char 32
724 if (builtinFont->widths->getWidth("space", &w)) {
725 widths[32] = 0.001 * w;
727 for (code = 0; code < 256; ++code) {
728 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
729 widths[code] = 0.001 * w;
733 // couldn't find widths -- use defaults
735 // this is technically an error -- the Widths entry is required
736 // for all but the Base-14 fonts -- but certain PDF generators
737 // apparently don't include widths for Arial and TimesNewRoman
738 if (isFixedWidth()) {
740 } else if (isSerif()) {
751 builtinFont = builtinFontSubst[i];
752 // this is a kludge for broken PDF files that encode char 32
754 if (builtinFont->widths->getWidth("space", &w)) {
755 widths[32] = 0.001 * w;
757 for (code = 0; code < 256; ++code) {
758 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
759 widths[code] = 0.001 * w;
768 Gfx8BitFont::~Gfx8BitFont() {
771 for (i = 0; i < 256; ++i) {
772 if (encFree[i] && enc[i]) {
777 if (charProcs.isDict()) {
780 if (resources.isDict()) {
785 int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
786 Unicode *u, int uSize, int *uLen,
787 double *dx, double *dy, double *ox, double *oy) {
790 *code = c = (CharCode)(*s & 0xff);
791 *uLen = ctu->mapToUnicode(c, u, uSize);
797 CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
802 Dict *Gfx8BitFont::getCharProcs() {
803 return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
806 Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
807 if (charProcs.isDict()) {
808 charProcs.dictLookup(enc[code], proc);
815 Dict *Gfx8BitFont::getResources() {
816 return resources.isDict() ? resources.getDict() : (Dict *)NULL;
819 //------------------------------------------------------------------------
821 //------------------------------------------------------------------------
823 static int cmpWidthExcep(const void *w1, const void *w2) {
824 return ((GfxFontCIDWidthExcep *)w1)->first -
825 ((GfxFontCIDWidthExcep *)w2)->first;
828 static int cmpWidthExcepV(const void *w1, const void *w2) {
829 return ((GfxFontCIDWidthExcepV *)w1)->first -
830 ((GfxFontCIDWidthExcepV *)w2)->first;
833 GfxCIDFont::GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
835 GfxFont(tagA, idA, nameA)
838 GString *collection, *cMapName;
839 Object desFontDictObj;
840 Object obj1, obj2, obj3, obj4, obj5, obj6;
842 int excepsSize, i, j, k;
846 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
849 widths.defWidth = 1.0;
850 widths.defHeight = -1.0;
851 widths.defVY = 0.880;
852 widths.exceps = NULL;
854 widths.excepsV = NULL;
859 // get the descendant font
860 if (!fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
861 error(-1, "Missing DescendantFonts entry in Type 0 font");
865 if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
866 error(-1, "Bad descendant font in Type 0 font");
870 desFontDict = desFontDictObj.getDict();
873 if (!desFontDict->lookup("Subtype", &obj1)) {
874 error(-1, "Missing Subtype entry in Type 0 descendant font");
877 if (obj1.isName("CIDFontType0")) {
879 } else if (obj1.isName("CIDFontType2")) {
882 error(-1, "Unknown Type 0 descendant font type '%s'",
883 obj1.isName() ? obj1.getName() : "???");
888 // get info from font descriptor
889 readFontDescriptor(xref, desFontDict);
891 // look for an external font file
894 //----- encoding info -----
897 if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
898 error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
901 obj1.dictLookup("Registry", &obj2);
902 obj1.dictLookup("Ordering", &obj3);
903 if (!obj2.isString() || !obj3.isString()) {
904 error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
907 collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
912 // look for a ToUnicode CMap
913 if (!(ctu = readToUnicodeCMap(fontDict, 16))) {
915 // the "Adobe-Identity" and "Adobe-UCS" collections don't have
916 // cidToUnicode files
917 if (collection->cmp("Adobe-Identity") &&
918 collection->cmp("Adobe-UCS")) {
920 // look for a user-supplied .cidToUnicode file
921 if (!(ctu = globalParams->getCIDToUnicode(collection))) {
922 error(-1, "Unknown character collection '%s'",
923 collection->getCString());
930 // encoding (i.e., CMap)
931 //~ need to handle a CMap stream here
932 //~ also need to deal with the UseCMap entry in the stream dict
933 if (!fontDict->lookup("Encoding", &obj1)->isName()) {
934 error(-1, "Missing or invalid Encoding entry in Type 0 font");
938 cMapName = new GString(obj1.getName());
940 if (!(cMap = globalParams->getCMap(collection, cMapName))) {
941 error(-1, "Unknown CMap '%s' for character collection '%s'",
942 cMapName->getCString(), collection->getCString());
950 // CIDToGIDMap (for embedded TrueType fonts)
951 if (type == fontCIDType2) {
952 fontDict->lookup("CIDToGIDMap", &obj1);
953 if (obj1.isStream()) {
956 cidToGID = (Gushort *)gmalloc(i * sizeof(Gushort));
958 while ((c1 = obj1.streamGetChar()) != EOF &&
959 (c2 = obj1.streamGetChar()) != EOF) {
960 if (cidToGIDLen == i) {
962 cidToGID = (Gushort *)grealloc(cidToGID, i * sizeof(Gushort));
964 cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
966 } else if (!obj1.isName("Identity") && !obj1.isNull()) {
967 error(-1, "Invalid CIDToGIDMap entry in CID font");
972 //----- character metrics -----
974 // default char width
975 if (desFontDict->lookup("DW", &obj1)->isInt()) {
976 widths.defWidth = obj1.getInt() * 0.001;
980 // char width exceptions
981 if (desFontDict->lookup("W", &obj1)->isArray()) {
984 while (i + 1 < obj1.arrayGetLength()) {
985 obj1.arrayGet(i, &obj2);
986 obj1.arrayGet(i + 1, &obj3);
987 if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
988 if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
989 if (widths.nExceps == excepsSize) {
991 widths.exceps = (GfxFontCIDWidthExcep *)
992 grealloc(widths.exceps,
993 excepsSize * sizeof(GfxFontCIDWidthExcep));
995 widths.exceps[widths.nExceps].first = obj2.getInt();
996 widths.exceps[widths.nExceps].last = obj3.getInt();
997 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1000 error(-1, "Bad widths array in Type 0 font");
1004 } else if (obj2.isInt() && obj3.isArray()) {
1005 if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1006 excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1007 widths.exceps = (GfxFontCIDWidthExcep *)
1008 grealloc(widths.exceps,
1009 excepsSize * sizeof(GfxFontCIDWidthExcep));
1012 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1013 if (obj3.arrayGet(k, &obj4)->isNum()) {
1014 widths.exceps[widths.nExceps].first = j;
1015 widths.exceps[widths.nExceps].last = j;
1016 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1020 error(-1, "Bad widths array in Type 0 font");
1026 error(-1, "Bad widths array in Type 0 font");
1032 qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
1037 // default metrics for vertical font
1038 if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1039 obj1.arrayGetLength() == 2) {
1040 if (obj1.arrayGet(0, &obj2)->isNum()) {
1041 widths.defVY = obj1.getNum() * 0.001;
1044 if (obj1.arrayGet(1, &obj2)->isNum()) {
1045 widths.defHeight = obj1.getNum() * 0.001;
1051 // char metric exceptions for vertical font
1052 if (desFontDict->lookup("W2", &obj1)->isArray()) {
1055 while (i + 1 < obj1.arrayGetLength()) {
1056 obj1.arrayGet(0, &obj2);
1057 obj2.arrayGet(0, &obj3);
1058 if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1059 if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1060 obj1.arrayGet(i + 3, &obj5)->isNum() &&
1061 obj1.arrayGet(i + 4, &obj6)->isNum()) {
1062 if (widths.nExcepsV == excepsSize) {
1064 widths.excepsV = (GfxFontCIDWidthExcepV *)
1065 grealloc(widths.excepsV,
1066 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1068 widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1069 widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1070 widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1071 widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1072 widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1075 error(-1, "Bad widths (W2) array in Type 0 font");
1081 } else if (obj2.isInt() && obj3.isArray()) {
1082 if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
1084 (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
1085 widths.excepsV = (GfxFontCIDWidthExcepV *)
1086 grealloc(widths.excepsV,
1087 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1090 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1091 if (obj3.arrayGet(k, &obj4)->isNum() &&
1092 obj3.arrayGet(k, &obj5)->isNum() &&
1093 obj3.arrayGet(k, &obj6)->isNum()) {
1094 widths.excepsV[widths.nExceps].first = j;
1095 widths.excepsV[widths.nExceps].last = j;
1096 widths.excepsV[widths.nExceps].height = obj4.getNum() * 0.001;
1097 widths.excepsV[widths.nExceps].vx = obj5.getNum() * 0.001;
1098 widths.excepsV[widths.nExceps].vy = obj6.getNum() * 0.001;
1102 error(-1, "Bad widths (W2) array in Type 0 font");
1110 error(-1, "Bad widths (W2) array in Type 0 font");
1116 qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
1121 desFontDictObj.free();
1131 desFontDictObj.free();
1135 GfxCIDFont::~GfxCIDFont() {
1142 gfree(widths.exceps);
1143 gfree(widths.excepsV);
1149 int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
1150 Unicode *u, int uSize, int *uLen,
1151 double *dx, double *dy, double *ox, double *oy) {
1153 double w, h, vx, vy;
1163 *code = (CharCode)(cid = cMap->getCID(s, len, &n));
1165 *uLen = ctu->mapToUnicode(cid, u, uSize);
1171 if (cMap->getWMode() == 0) {
1172 w = widths.defWidth;
1174 if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
1177 // invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1180 if (widths.exceps[m].first <= cid) {
1186 if (cid <= widths.exceps[a].last) {
1187 w = widths.exceps[a].width;
1194 h = widths.defHeight;
1195 vx = widths.defWidth / 2;
1197 if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
1199 b = widths.nExcepsV;
1200 // invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1203 if (widths.excepsV[m].last <= cid) {
1209 if (cid <= widths.excepsV[a].last) {
1210 h = widths.excepsV[a].height;
1211 vx = widths.excepsV[a].vx;
1212 vy = widths.excepsV[a].vy;
1225 int GfxCIDFont::getWMode() {
1226 return cMap ? cMap->getWMode() : 0;
1229 CharCodeToUnicode *GfxCIDFont::getToUnicode() {
1234 GString *GfxCIDFont::getCollection() {
1235 return cMap ? cMap->getCollection() : (GString *)NULL;
1238 //------------------------------------------------------------------------
1240 //------------------------------------------------------------------------
1242 GfxFontDict::GfxFontDict(XRef *xref, Dict *fontDict) {
1247 numFonts = fontDict->getLength();
1248 fonts = (GfxFont **)gmalloc(numFonts * sizeof(GfxFont *));
1249 for (i = 0; i < numFonts; ++i) {
1250 fontDict->getValNF(i, &obj1);
1251 obj1.fetch(xref, &obj2);
1252 if (obj2.isDict()) {
1256 // no indirect reference for this font, so invent a unique one
1257 // (legal generation numbers are five digits, so any 6-digit
1258 // number would be safe)
1262 fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i),
1264 if (fonts[i] && !fonts[i]->isOk()) {
1269 error(-1, "font resource is not a dictionary");
1277 GfxFontDict::~GfxFontDict() {
1280 for (i = 0; i < numFonts; ++i) {
1288 GfxFont *GfxFontDict::lookup(char *tag) {
1291 for (i = 0; i < numFonts; ++i) {
1292 if (fonts[i] && fonts[i]->matches(tag)) {