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 { "Symbol,Bold", "Symbol" },
70 { "Symbol,BoldItalic", "Symbol" },
71 { "Symbol,Italic", "Symbol" },
72 { "TimesNewRoman", "Times-Roman" },
73 { "TimesNewRoman,Bold", "Times-Bold" },
74 { "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
75 { "TimesNewRoman,Italic", "Times-Italic" },
76 { "TimesNewRoman-Bold", "Times-Bold" },
77 { "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
78 { "TimesNewRoman-Italic", "Times-Italic" },
79 { "TimesNewRomanPS", "Times-Roman" },
80 { "TimesNewRomanPS-Bold", "Times-Bold" },
81 { "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
82 { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
83 { "TimesNewRomanPS-BoldMT", "Times-Bold" },
84 { "TimesNewRomanPS-Italic", "Times-Italic" },
85 { "TimesNewRomanPS-ItalicMT", "Times-Italic" },
86 { "TimesNewRomanPSMT", "Times-Roman" }
89 //------------------------------------------------------------------------
91 //------------------------------------------------------------------------
93 GfxFont *GfxFont::makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict) {
100 fontDict->lookup("BaseFont", &obj1);
102 nameA = new GString(obj1.getName());
108 fontDict->lookup("Subtype", &obj1);
109 if (obj1.isName("Type1") || obj1.isName("MMType1")) {
110 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
111 } else if (obj1.isName("Type1C")) {
112 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
113 } else if (obj1.isName("Type3")) {
114 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
115 } else if (obj1.isName("TrueType")) {
116 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
117 } else if (obj1.isName("Type0")) {
118 font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
120 error(-1, "Unknown font type: '%s'",
121 obj1.isName() ? obj1.getName() : "???");
122 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
129 GfxFont::GfxFont(char *tagA, Ref idA, GString *nameA) {
131 tag = new GString(tagA);
138 GfxFont::~GfxFont() {
151 void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
152 Object obj1, obj2, obj3, obj4;
156 // assume Times-Roman by default (for substitution purposes)
163 if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
166 if (obj1.dictLookup("Flags", &obj2)->isInt()) {
167 flags = obj2.getInt();
172 obj1.dictLookup("FontName", &obj2);
174 embFontName = new GString(obj2.getName());
178 // look for embedded font file
179 if (obj1.dictLookupNF("FontFile", &obj2)->isRef()) {
180 if (type == fontType1) {
181 embFontID = obj2.getRef();
183 error(-1, "Mismatch between font type and embedded font file");
187 if (embFontID.num == -1 &&
188 obj1.dictLookupNF("FontFile2", &obj2)->isRef()) {
189 if (type == fontTrueType || type == fontCIDType2) {
190 embFontID = obj2.getRef();
192 error(-1, "Mismatch between font type and embedded font file");
196 if (embFontID.num == -1 &&
197 obj1.dictLookupNF("FontFile3", &obj2)->isRef()) {
198 if (obj2.fetch(xref, &obj3)->isStream()) {
199 obj3.streamGetDict()->lookup("Subtype", &obj4);
200 if (obj4.isName("Type1")) {
201 if (type == fontType1) {
202 embFontID = obj2.getRef();
204 error(-1, "Mismatch between font type and embedded font file");
206 } else if (obj4.isName("Type1C")) {
207 if (type == fontType1) {
209 embFontID = obj2.getRef();
210 } else if (type == fontType1C) {
211 embFontID = obj2.getRef();
213 error(-1, "Mismatch between font type and embedded font file");
215 } else if (obj4.isName("TrueType")) {
216 if (type == fontTrueType) {
217 embFontID = obj2.getRef();
219 error(-1, "Mismatch between font type and embedded font file");
221 } else if (obj4.isName("CIDFontType0C")) {
222 if (type == fontCIDType0) {
223 type = fontCIDType0C;
224 embFontID = obj2.getRef();
226 error(-1, "Mismatch between font type and embedded font file");
229 error(-1, "Unknown embedded font type '%s'",
230 obj4.isName() ? obj4.getName() : "???");
238 // look for MissingWidth
239 obj1.dictLookup("MissingWidth", &obj2);
241 missingWidth = obj2.getNum();
245 // get Ascent and Descent
246 obj1.dictLookup("Ascent", &obj2);
248 t = 0.001 * obj2.getNum();
249 // some broken font descriptors set ascent and descent to 0
255 obj1.dictLookup("Descent", &obj2);
257 t = 0.001 * obj2.getNum();
258 // some broken font descriptors set ascent and descent to 0
262 // some broken font descriptors specify a positive descent
270 if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
271 for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
272 if (obj2.arrayGet(i, &obj3)->isNum()) {
273 fontBBox[i] = 0.001 * obj3.getNum();
284 CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits) {
285 CharCodeToUnicode *ctu;
290 if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
296 while ((c = obj1.streamGetChar()) != EOF) {
301 ctu = CharCodeToUnicode::parseCMap(buf, nBits);
306 void GfxFont::findExtFontFile() {
308 if (type == fontType1) {
309 extFontFile = globalParams->findFontFile(name, ".pfa", ".pfb");
310 } else if (type == fontTrueType) {
311 extFontFile = globalParams->findFontFile(name, ".ttf", NULL);
316 char *GfxFont::readExtFontFile(int *len) {
320 if (!(f = fopen(extFontFile->getCString(), "rb"))) {
321 error(-1, "External font file '%s' vanished", extFontFile->getCString());
324 fseek(f, 0, SEEK_END);
325 *len = (int)ftell(f);
326 fseek(f, 0, SEEK_SET);
327 buf = (char *)gmalloc(*len);
328 if ((int)fread(buf, 1, *len, f) != *len) {
329 error(-1, "Error reading external font file '%s'", extFontFile);
335 char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
342 obj1.initRef(embFontID.num, embFontID.gen);
343 obj1.fetch(xref, &obj2);
344 if (!obj2.isStream()) {
345 error(-1, "Embedded font file is not a stream");
351 str = obj2.getStream();
356 while ((c = str->getChar()) != EOF) {
359 buf = (char *)grealloc(buf, size);
372 //------------------------------------------------------------------------
374 //------------------------------------------------------------------------
376 Gfx8BitFont::Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
377 GfxFontType typeA, Dict *fontDict):
378 GfxFont(tagA, idA, nameA)
380 BuiltinFont *builtinFont;
382 GBool baseEncFromFontFile;
389 Unicode toUnicode[256];
391 int firstChar, lastChar;
393 Object obj1, obj2, obj3;
399 // Acrobat 4.0 and earlier substituted Base14-compatible fonts
400 // without providing Widths and a FontDescriptor, so we munge the
401 // names into the proper Base14 names. (This table is from
402 // implementation note 44 in the PDF 1.4 spec.)
405 b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
406 // invariant: stdFontMap[a].altName <= name < stdFontMap[b].altName
409 if (name->cmp(stdFontMap[m].altName) >= 0) {
415 if (!name->cmp(stdFontMap[a].altName)) {
417 name = new GString(stdFontMap[a].properName);
421 // is it a built-in font?
424 for (i = 0; i < nBuiltinFonts; ++i) {
425 if (!name->cmp(builtinFonts[i].name)) {
426 builtinFont = &builtinFonts[i];
432 // default ascent/descent values
434 ascent = 0.001 * builtinFont->ascent;
435 descent = 0.001 * builtinFont->descent;
436 fontBBox[0] = 0.001 * builtinFont->bbox[0];
437 fontBBox[1] = 0.001 * builtinFont->bbox[1];
438 fontBBox[2] = 0.001 * builtinFont->bbox[2];
439 fontBBox[3] = 0.001 * builtinFont->bbox[3];
443 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
446 // get info from font descriptor
447 readFontDescriptor(xref, fontDict);
449 // look for an external font file
453 fontMat[0] = fontMat[3] = 1;
454 fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
455 if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
456 for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
457 if (obj1.arrayGet(i, &obj2)->isNum()) {
458 fontMat[i] = obj2.getNum();
465 // get Type 3 bounding box, font definition, and resources
466 if (type == fontType3) {
467 if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
468 for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
469 if (obj1.arrayGet(i, &obj2)->isNum()) {
470 fontBBox[i] = obj2.getNum();
476 if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
477 error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
480 if (!fontDict->lookup("Resources", &resources)->isDict()) {
485 //----- build the font encoding -----
487 // Encodings start with a base encoding, which can come from
488 // (in order of priority):
489 // 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
490 // - MacRoman / MacExpert / WinAnsi / Standard
491 // 2. embedded or external font file
493 // - builtin --> builtin encoding
494 // - TrueType --> MacRomanEncoding
495 // - others --> StandardEncoding
496 // and then add a list of differences (if any) from
497 // FontDict.Encoding.Differences.
499 // check FontDict for base encoding
500 hasEncoding = gFalse;
502 baseEncFromFontFile = gFalse;
503 fontDict->lookup("Encoding", &obj1);
505 obj1.dictLookup("BaseEncoding", &obj2);
506 if (obj2.isName("MacRomanEncoding")) {
508 baseEnc = macRomanEncoding;
509 } else if (obj2.isName("MacExpertEncoding")) {
511 baseEnc = macExpertEncoding;
512 } else if (obj2.isName("WinAnsiEncoding")) {
514 baseEnc = winAnsiEncoding;
515 } else if (obj2.isName("StandardEncoding")) {
517 baseEnc = standardEncoding;
520 } else if (obj1.isName("MacRomanEncoding")) {
522 baseEnc = macRomanEncoding;
523 } else if (obj1.isName("MacExpertEncoding")) {
525 baseEnc = macExpertEncoding;
526 } else if (obj1.isName("WinAnsiEncoding")) {
528 baseEnc = winAnsiEncoding;
529 } else if (obj1.isName("StandardEncoding")) {
531 baseEnc = standardEncoding;
534 // check embedded or external font file for base encoding
535 // (only for Type 1 fonts - trying to get an encoding out of a
536 // TrueType font is a losing proposition)
539 if ((type == fontType1 || type == fontType1C) &&
540 (extFontFile || embFontID.num >= 0)) {
542 buf = readExtFontFile(&len);
544 buf = readEmbFontFile(xref, &len);
547 if (type == fontType1C && !strncmp(buf, "%!", 2)) {
548 // various tools (including Adobe's) occasionally embed Type 1
549 // fonts but label them Type 1C
552 if (type == fontType1) {
553 fontFile = new Type1FontFile(buf, len);
555 fontFile = new Type1CFontFile(buf, len);
557 if (fontFile->getName()) {
561 embFontName = new GString(fontFile->getName());
564 baseEnc = fontFile->getEncoding();
565 baseEncFromFontFile = gTrue;
571 // get default base encoding
574 baseEnc = builtinFont->defaultBaseEnc;
576 } else if (type == fontTrueType) {
577 baseEnc = winAnsiEncoding;
579 baseEnc = standardEncoding;
583 // copy the base encoding
584 for (i = 0; i < 256; ++i) {
586 if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
587 enc[i] = copyString(baseEnc[i]);
591 // merge differences into encoding
593 obj1.dictLookup("Differences", &obj2);
594 if (obj2.isArray()) {
597 for (i = 0; i < obj2.arrayGetLength(); ++i) {
598 obj2.arrayGet(i, &obj3);
600 code = obj3.getInt();
601 } else if (obj3.isName()) {
606 enc[code] = copyString(obj3.getName());
607 encFree[code] = gTrue;
611 error(-1, "Wrong type in font encoding resource differences (%s)",
624 //----- build the mapping to Unicode -----
626 // look for a ToUnicode CMap
627 if (!(ctu = readToUnicodeCMap(fontDict, 8))) {
629 // no ToUnicode CMap, so use the char names
631 // pass 1: use the name-to-Unicode mapping table
632 missing = hex = gFalse;
633 for (code = 0; code < 256; ++code) {
634 if ((charName = enc[code])) {
635 if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
636 strcmp(charName, ".notdef")) {
637 // if it wasn't in the name-to-Unicode table, check for a
638 // name that looks like 'Axx' or 'xx', where 'A' is any letter
639 // and 'xx' is two hex digits
640 if ((strlen(charName) == 3 &&
641 isalpha(charName[0]) &&
642 isxdigit(charName[1]) && isxdigit(charName[2]) &&
643 ((charName[1] >= 'a' && charName[1] <= 'f') ||
644 (charName[1] >= 'A' && charName[1] <= 'F') ||
645 (charName[2] >= 'a' && charName[2] <= 'f') ||
646 (charName[2] >= 'A' && charName[2] <= 'F'))) ||
647 (strlen(charName) == 2 &&
648 isxdigit(charName[0]) && isxdigit(charName[1]) &&
649 ((charName[0] >= 'a' && charName[0] <= 'f') ||
650 (charName[0] >= 'A' && charName[0] <= 'F') ||
651 (charName[1] >= 'a' && charName[1] <= 'f') ||
652 (charName[1] >= 'A' && charName[1] <= 'F')))) {
662 // pass 2: try to fill in the missing chars, looking for names of
663 // the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
664 // are any letters, 'xx' is two hex digits, and 'nn' is 2-4
666 if (missing && globalParams->getMapNumericCharNames()) {
667 for (code = 0; code < 256; ++code) {
668 if ((charName = enc[code]) && !toUnicode[code] &&
669 strcmp(charName, ".notdef")) {
670 n = strlen(charName);
672 if (hex && n == 3 && isalpha(charName[0]) &&
673 isxdigit(charName[1]) && isxdigit(charName[2])) {
674 sscanf(charName+1, "%x", &code2);
675 } else if (hex && n == 2 &&
676 isxdigit(charName[0]) && isxdigit(charName[1])) {
677 sscanf(charName, "%x", &code2);
678 } else if (!hex && n >= 2 && n <= 4 &&
679 isdigit(charName[0]) && isdigit(charName[1])) {
680 code2 = atoi(charName);
681 } else if (n >= 3 && n <= 5 &&
682 isdigit(charName[1]) && isdigit(charName[2])) {
683 code2 = atoi(charName+1);
684 } else if (n >= 4 && n <= 6 &&
685 isdigit(charName[2]) && isdigit(charName[3])) {
686 code2 = atoi(charName+2);
688 if (code2 >= 0 && code2 <= 0xff) {
689 toUnicode[code] = (Unicode)code2;
695 ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
698 //----- get the character widths -----
700 // initialize all widths
701 for (code = 0; code < 256; ++code) {
702 widths[code] = missingWidth * 0.001;
705 // use widths from font dict, if present
706 fontDict->lookup("FirstChar", &obj1);
707 firstChar = obj1.isInt() ? obj1.getInt() : 0;
709 fontDict->lookup("LastChar", &obj1);
710 lastChar = obj1.isInt() ? obj1.getInt() : 255;
712 mul = (type == fontType3) ? fontMat[0] : 0.001;
713 fontDict->lookup("Widths", &obj1);
714 if (obj1.isArray()) {
715 flags |= fontFixedWidth;
716 for (code = firstChar; code <= lastChar; ++code) {
717 obj1.arrayGet(code - firstChar, &obj2);
719 widths[code] = obj2.getNum() * mul;
720 if (widths[code] != widths[firstChar]) {
721 flags &= ~fontFixedWidth;
727 // use widths from built-in font
728 } else if (builtinFont) {
729 // this is a kludge for broken PDF files that encode char 32
731 if (builtinFont->widths->getWidth("space", &w)) {
732 widths[32] = 0.001 * w;
734 for (code = 0; code < 256; ++code) {
735 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
736 widths[code] = 0.001 * w;
740 // couldn't find widths -- use defaults
742 // this is technically an error -- the Widths entry is required
743 // for all but the Base-14 fonts -- but certain PDF generators
744 // apparently don't include widths for Arial and TimesNewRoman
745 if (isFixedWidth()) {
747 } else if (isSerif()) {
758 builtinFont = builtinFontSubst[i];
759 // this is a kludge for broken PDF files that encode char 32
761 if (builtinFont->widths->getWidth("space", &w)) {
762 widths[32] = 0.001 * w;
764 for (code = 0; code < 256; ++code) {
765 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
766 widths[code] = 0.001 * w;
775 Gfx8BitFont::~Gfx8BitFont() {
778 for (i = 0; i < 256; ++i) {
779 if (encFree[i] && enc[i]) {
784 if (charProcs.isDict()) {
787 if (resources.isDict()) {
792 int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
793 Unicode *u, int uSize, int *uLen,
794 double *dx, double *dy, double *ox, double *oy) {
797 *code = c = (CharCode)(*s & 0xff);
798 *uLen = ctu->mapToUnicode(c, u, uSize);
804 CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
809 Dict *Gfx8BitFont::getCharProcs() {
810 return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
813 Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
814 if (charProcs.isDict()) {
815 charProcs.dictLookup(enc[code], proc);
822 Dict *Gfx8BitFont::getResources() {
823 return resources.isDict() ? resources.getDict() : (Dict *)NULL;
826 //------------------------------------------------------------------------
828 //------------------------------------------------------------------------
830 static int cmpWidthExcep(const void *w1, const void *w2) {
831 return ((GfxFontCIDWidthExcep *)w1)->first -
832 ((GfxFontCIDWidthExcep *)w2)->first;
835 static int cmpWidthExcepV(const void *w1, const void *w2) {
836 return ((GfxFontCIDWidthExcepV *)w1)->first -
837 ((GfxFontCIDWidthExcepV *)w2)->first;
840 GfxCIDFont::GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
842 GfxFont(tagA, idA, nameA)
845 GString *collection, *cMapName;
846 Object desFontDictObj;
847 Object obj1, obj2, obj3, obj4, obj5, obj6;
849 int excepsSize, i, j, k;
853 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
856 widths.defWidth = 1.0;
857 widths.defHeight = -1.0;
858 widths.defVY = 0.880;
859 widths.exceps = NULL;
861 widths.excepsV = NULL;
866 // get the descendant font
867 if (!fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
868 error(-1, "Missing DescendantFonts entry in Type 0 font");
872 if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
873 error(-1, "Bad descendant font in Type 0 font");
877 desFontDict = desFontDictObj.getDict();
880 if (!desFontDict->lookup("Subtype", &obj1)) {
881 error(-1, "Missing Subtype entry in Type 0 descendant font");
884 if (obj1.isName("CIDFontType0")) {
886 } else if (obj1.isName("CIDFontType2")) {
889 error(-1, "Unknown Type 0 descendant font type '%s'",
890 obj1.isName() ? obj1.getName() : "???");
895 // get info from font descriptor
896 readFontDescriptor(xref, desFontDict);
898 // look for an external font file
901 //----- encoding info -----
904 if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
905 error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
908 obj1.dictLookup("Registry", &obj2);
909 obj1.dictLookup("Ordering", &obj3);
910 if (!obj2.isString() || !obj3.isString()) {
911 error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
914 collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
919 // look for a ToUnicode CMap
920 if (!(ctu = readToUnicodeCMap(fontDict, 16))) {
922 // the "Adobe-Identity" and "Adobe-UCS" collections don't have
923 // cidToUnicode files
924 if (collection->cmp("Adobe-Identity") &&
925 collection->cmp("Adobe-UCS")) {
927 // look for a user-supplied .cidToUnicode file
928 if (!(ctu = globalParams->getCIDToUnicode(collection))) {
929 error(-1, "Unknown character collection '%s'",
930 collection->getCString());
937 // encoding (i.e., CMap)
938 //~ need to handle a CMap stream here
939 //~ also need to deal with the UseCMap entry in the stream dict
940 if (!fontDict->lookup("Encoding", &obj1)->isName()) {
941 error(-1, "Missing or invalid Encoding entry in Type 0 font");
945 cMapName = new GString(obj1.getName());
947 if (!(cMap = globalParams->getCMap(collection, cMapName))) {
948 error(-1, "Unknown CMap '%s' for character collection '%s'",
949 cMapName->getCString(), collection->getCString());
957 // CIDToGIDMap (for embedded TrueType fonts)
958 if (type == fontCIDType2) {
959 desFontDict->lookup("CIDToGIDMap", &obj1);
960 if (obj1.isStream()) {
963 cidToGID = (Gushort *)gmalloc(i * sizeof(Gushort));
965 while ((c1 = obj1.streamGetChar()) != EOF &&
966 (c2 = obj1.streamGetChar()) != EOF) {
967 if (cidToGIDLen == i) {
969 cidToGID = (Gushort *)grealloc(cidToGID, i * sizeof(Gushort));
971 cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
973 } else if (!obj1.isName("Identity") && !obj1.isNull()) {
974 error(-1, "Invalid CIDToGIDMap entry in CID font");
979 //----- character metrics -----
981 // default char width
982 if (desFontDict->lookup("DW", &obj1)->isInt()) {
983 widths.defWidth = obj1.getInt() * 0.001;
987 // char width exceptions
988 if (desFontDict->lookup("W", &obj1)->isArray()) {
991 while (i + 1 < obj1.arrayGetLength()) {
992 obj1.arrayGet(i, &obj2);
993 obj1.arrayGet(i + 1, &obj3);
994 if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
995 if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
996 if (widths.nExceps == excepsSize) {
998 widths.exceps = (GfxFontCIDWidthExcep *)
999 grealloc(widths.exceps,
1000 excepsSize * sizeof(GfxFontCIDWidthExcep));
1002 widths.exceps[widths.nExceps].first = obj2.getInt();
1003 widths.exceps[widths.nExceps].last = obj3.getInt();
1004 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1007 error(-1, "Bad widths array in Type 0 font");
1011 } else if (obj2.isInt() && obj3.isArray()) {
1012 if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1013 excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1014 widths.exceps = (GfxFontCIDWidthExcep *)
1015 grealloc(widths.exceps,
1016 excepsSize * sizeof(GfxFontCIDWidthExcep));
1019 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1020 if (obj3.arrayGet(k, &obj4)->isNum()) {
1021 widths.exceps[widths.nExceps].first = j;
1022 widths.exceps[widths.nExceps].last = j;
1023 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1027 error(-1, "Bad widths array in Type 0 font");
1033 error(-1, "Bad widths array in Type 0 font");
1039 qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
1044 // default metrics for vertical font
1045 if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1046 obj1.arrayGetLength() == 2) {
1047 if (obj1.arrayGet(0, &obj2)->isNum()) {
1048 widths.defVY = obj1.getNum() * 0.001;
1051 if (obj1.arrayGet(1, &obj2)->isNum()) {
1052 widths.defHeight = obj1.getNum() * 0.001;
1058 // char metric exceptions for vertical font
1059 if (desFontDict->lookup("W2", &obj1)->isArray()) {
1062 while (i + 1 < obj1.arrayGetLength()) {
1063 obj1.arrayGet(0, &obj2);
1064 obj2.arrayGet(0, &obj3);
1065 if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1066 if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1067 obj1.arrayGet(i + 3, &obj5)->isNum() &&
1068 obj1.arrayGet(i + 4, &obj6)->isNum()) {
1069 if (widths.nExcepsV == excepsSize) {
1071 widths.excepsV = (GfxFontCIDWidthExcepV *)
1072 grealloc(widths.excepsV,
1073 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1075 widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1076 widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1077 widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1078 widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1079 widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1082 error(-1, "Bad widths (W2) array in Type 0 font");
1088 } else if (obj2.isInt() && obj3.isArray()) {
1089 if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
1091 (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
1092 widths.excepsV = (GfxFontCIDWidthExcepV *)
1093 grealloc(widths.excepsV,
1094 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1097 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1098 if (obj3.arrayGet(k, &obj4)->isNum() &&
1099 obj3.arrayGet(k, &obj5)->isNum() &&
1100 obj3.arrayGet(k, &obj6)->isNum()) {
1101 widths.excepsV[widths.nExceps].first = j;
1102 widths.excepsV[widths.nExceps].last = j;
1103 widths.excepsV[widths.nExceps].height = obj4.getNum() * 0.001;
1104 widths.excepsV[widths.nExceps].vx = obj5.getNum() * 0.001;
1105 widths.excepsV[widths.nExceps].vy = obj6.getNum() * 0.001;
1109 error(-1, "Bad widths (W2) array in Type 0 font");
1117 error(-1, "Bad widths (W2) array in Type 0 font");
1123 qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
1128 desFontDictObj.free();
1138 desFontDictObj.free();
1142 GfxCIDFont::~GfxCIDFont() {
1149 gfree(widths.exceps);
1150 gfree(widths.excepsV);
1156 int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
1157 Unicode *u, int uSize, int *uLen,
1158 double *dx, double *dy, double *ox, double *oy) {
1160 double w, h, vx, vy;
1170 *code = (CharCode)(cid = cMap->getCID(s, len, &n));
1172 *uLen = ctu->mapToUnicode(cid, u, uSize);
1178 if (cMap->getWMode() == 0) {
1179 w = widths.defWidth;
1181 if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
1184 // invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1187 if (widths.exceps[m].first <= cid) {
1193 if (cid <= widths.exceps[a].last) {
1194 w = widths.exceps[a].width;
1201 h = widths.defHeight;
1202 vx = widths.defWidth / 2;
1204 if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
1206 b = widths.nExcepsV;
1207 // invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1210 if (widths.excepsV[m].last <= cid) {
1216 if (cid <= widths.excepsV[a].last) {
1217 h = widths.excepsV[a].height;
1218 vx = widths.excepsV[a].vx;
1219 vy = widths.excepsV[a].vy;
1232 int GfxCIDFont::getWMode() {
1233 return cMap ? cMap->getWMode() : 0;
1236 CharCodeToUnicode *GfxCIDFont::getToUnicode() {
1241 GString *GfxCIDFont::getCollection() {
1242 return cMap ? cMap->getCollection() : (GString *)NULL;
1245 //------------------------------------------------------------------------
1247 //------------------------------------------------------------------------
1249 GfxFontDict::GfxFontDict(XRef *xref, Dict *fontDict) {
1254 numFonts = fontDict->getLength();
1255 fonts = (GfxFont **)gmalloc(numFonts * sizeof(GfxFont *));
1256 for (i = 0; i < numFonts; ++i) {
1257 fontDict->getValNF(i, &obj1);
1258 obj1.fetch(xref, &obj2);
1259 if (obj2.isDict()) {
1263 // no indirect reference for this font, so invent a unique one
1264 // (legal generation numbers are five digits, so any 6-digit
1265 // number would be safe)
1269 fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i),
1271 if (fonts[i] && !fonts[i]->isOk()) {
1276 error(-1, "font resource is not a dictionary");
1284 GfxFontDict::~GfxFontDict() {
1287 for (i = 0; i < numFonts; ++i) {
1295 GfxFont *GfxFontDict::lookup(char *tag) {
1298 for (i = 0; i < numFonts; ++i) {
1299 if (fonts[i] && fonts[i]->matches(tag)) {