1 //========================================================================
5 // Copyright 1996-2002 Glyph & Cog, LLC
7 //========================================================================
10 #pragma implementation
22 #include "GlobalParams.h"
24 #include "CharCodeToUnicode.h"
25 #include "FontEncodingTables.h"
26 #include "BuiltinFontTables.h"
30 //------------------------------------------------------------------------
32 struct StdFontMapEntry {
37 static StdFontMapEntry stdFontMap[] = {
38 { "Arial", "Helvetica" },
39 { "Arial,Bold", "Helvetica-Bold" },
40 { "Arial,BoldItalic", "Helvetica-BoldOblique" },
41 { "Arial,Italic", "Helvetica-Oblique" },
42 { "Arial-Bold", "Helvetica-Bold" },
43 { "Arial-BoldItalic", "Helvetica-BoldOblique" },
44 { "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
45 { "Arial-BoldMT", "Helvetica-Bold" },
46 { "Arial-Italic", "Helvetica-Oblique" },
47 { "Arial-ItalicMT", "Helvetica-Oblique" },
48 { "ArialMT", "Helvetica" },
49 { "Courier,Bold", "Courier-Bold" },
50 { "Courier,Italic", "Courier-Oblique" },
51 { "Courier,BoldItalic", "Courier-BoldOblique" },
52 { "CourierNew", "Courier" },
53 { "CourierNew,Bold", "Courier-Bold" },
54 { "CourierNew,BoldItalic", "Courier-BoldOblique" },
55 { "CourierNew,Italic", "Courier-Oblique" },
56 { "CourierNew-Bold", "Courier-Bold" },
57 { "CourierNew-BoldItalic", "Courier-BoldOblique" },
58 { "CourierNew-Italic", "Courier-Oblique" },
59 { "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
60 { "CourierNewPS-BoldMT", "Courier-Bold" },
61 { "CourierNewPS-ItalicMT", "Courier-Oblique" },
62 { "CourierNewPSMT", "Courier" },
63 { "Helvetica,Bold", "Helvetica-Bold" },
64 { "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
65 { "Helvetica,Italic", "Helvetica-Oblique" },
66 { "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
67 { "Helvetica-Italic", "Helvetica-Oblique" },
68 { "TimesNewRoman", "Times-Roman" },
69 { "TimesNewRoman,Bold", "Times-Bold" },
70 { "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
71 { "TimesNewRoman,Italic", "Times-Italic" },
72 { "TimesNewRoman-Bold", "Times-Bold" },
73 { "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
74 { "TimesNewRoman-Italic", "Times-Italic" },
75 { "TimesNewRomanPS", "Times-Roman" },
76 { "TimesNewRomanPS-Bold", "Times-Bold" },
77 { "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
78 { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
79 { "TimesNewRomanPS-BoldMT", "Times-Bold" },
80 { "TimesNewRomanPS-Italic", "Times-Italic" },
81 { "TimesNewRomanPS-ItalicMT", "Times-Italic" },
82 { "TimesNewRomanPSMT", "Times-Roman" }
85 //------------------------------------------------------------------------
87 //------------------------------------------------------------------------
89 GfxFont *GfxFont::makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict) {
96 fontDict->lookup("BaseFont", &obj1);
98 nameA = new GString(obj1.getName());
104 fontDict->lookup("Subtype", &obj1);
105 if (obj1.isName("Type1") || obj1.isName("MMType1")) {
106 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
107 } else if (obj1.isName("Type1C")) {
108 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
109 } else if (obj1.isName("Type3")) {
110 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
111 } else if (obj1.isName("TrueType")) {
112 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
113 } else if (obj1.isName("Type0")) {
114 font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
116 error(-1, "Unknown font type: '%s'",
117 obj1.isName() ? obj1.getName() : "???");
118 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
125 GfxFont::GfxFont(char *tagA, Ref idA, GString *nameA) {
127 tag = new GString(tagA);
134 GfxFont::~GfxFont() {
147 void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
148 Object obj1, obj2, obj3, obj4;
152 // assume Times-Roman by default (for substitution purposes)
159 if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
162 if (obj1.dictLookup("Flags", &obj2)->isInt()) {
163 flags = obj2.getInt();
168 obj1.dictLookup("FontName", &obj2);
170 embFontName = new GString(obj2.getName());
174 // look for embedded font file
175 if (obj1.dictLookupNF("FontFile", &obj2)->isRef()) {
176 if (type == fontType1) {
177 embFontID = obj2.getRef();
179 error(-1, "Mismatch between font type and embedded font file");
183 if (embFontID.num == -1 &&
184 obj1.dictLookupNF("FontFile2", &obj2)->isRef()) {
185 if (type == fontTrueType || type == fontCIDType2) {
186 embFontID = obj2.getRef();
188 error(-1, "Mismatch between font type and embedded font file");
192 if (embFontID.num == -1 &&
193 obj1.dictLookupNF("FontFile3", &obj2)->isRef()) {
194 if (obj2.fetch(xref, &obj3)->isStream()) {
195 obj3.streamGetDict()->lookup("Subtype", &obj4);
196 if (obj4.isName("Type1")) {
197 if (type == fontType1) {
198 embFontID = obj2.getRef();
200 error(-1, "Mismatch between font type and embedded font file");
202 } else if (obj4.isName("Type1C")) {
203 if (type == fontType1) {
205 embFontID = obj2.getRef();
206 } else if (type == fontType1C) {
207 embFontID = obj2.getRef();
209 error(-1, "Mismatch between font type and embedded font file");
211 } else if (obj4.isName("TrueType")) {
212 if (type == fontTrueType) {
213 embFontID = obj2.getRef();
215 error(-1, "Mismatch between font type and embedded font file");
217 } else if (obj4.isName("CIDFontType0C")) {
218 if (type == fontCIDType0) {
219 type = fontCIDType0C;
220 embFontID = obj2.getRef();
222 error(-1, "Mismatch between font type and embedded font file");
225 error(-1, "Unknown embedded font type '%s'",
226 obj4.isName() ? obj4.getName() : "???");
234 // look for MissingWidth
235 obj1.dictLookup("MissingWidth", &obj2);
237 missingWidth = obj2.getNum();
241 // get Ascent and Descent
242 obj1.dictLookup("Ascent", &obj2);
244 t = 0.001 * obj2.getNum();
245 // some broken font descriptors set ascent and descent to 0
251 obj1.dictLookup("Descent", &obj2);
253 t = 0.001 * obj2.getNum();
254 // some broken font descriptors set ascent and descent to 0
262 if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
263 for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
264 if (obj2.arrayGet(i, &obj3)->isNum()) {
265 fontBBox[i] = 0.001 * obj3.getNum();
276 CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits) {
277 CharCodeToUnicode *ctu;
282 if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
288 while ((c = obj1.streamGetChar()) != EOF) {
293 ctu = CharCodeToUnicode::parseCMap(buf, nBits);
298 void GfxFont::findExtFontFile() {
300 if (type == fontType1) {
301 extFontFile = globalParams->findFontFile(name, ".pfa", ".pfb");
302 } else if (type == fontTrueType) {
303 extFontFile = globalParams->findFontFile(name, ".ttf", NULL);
308 char *GfxFont::readExtFontFile(int *len) {
312 if (!(f = fopen(extFontFile->getCString(), "rb"))) {
313 error(-1, "External font file '%s' vanished", extFontFile->getCString());
316 fseek(f, 0, SEEK_END);
317 *len = (int)ftell(f);
318 fseek(f, 0, SEEK_SET);
319 buf = (char *)gmalloc(*len);
320 if ((int)fread(buf, 1, *len, f) != *len) {
321 error(-1, "Error reading external font file '%s'", extFontFile);
327 char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
334 obj1.initRef(embFontID.num, embFontID.gen);
335 obj1.fetch(xref, &obj2);
336 if (!obj2.isStream()) {
337 error(-1, "Embedded font file is not a stream");
343 str = obj2.getStream();
348 while ((c = str->getChar()) != EOF) {
351 buf = (char *)grealloc(buf, size);
364 //------------------------------------------------------------------------
366 //------------------------------------------------------------------------
368 Gfx8BitFont::Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
369 GfxFontType typeA, Dict *fontDict):
370 GfxFont(tagA, idA, nameA)
372 BuiltinFont *builtinFont;
374 GBool baseEncFromFontFile;
381 Unicode toUnicode[256];
383 int firstChar, lastChar;
385 Object obj1, obj2, obj3;
391 // Acrobat 4.0 and earlier substituted Base14-compatible fonts
392 // without providing Widths and a FontDescriptor, so we munge the
393 // names into the proper Base14 names. (This table is from
394 // implementation note 44 in the PDF 1.4 spec.)
397 b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
398 // invariant: stdFontMap[a].altName <= name < stdFontMap[b].altName
401 if (name->cmp(stdFontMap[m].altName) >= 0) {
407 if (!name->cmp(stdFontMap[a].altName)) {
409 name = new GString(stdFontMap[a].properName);
413 // is it a built-in font?
416 for (i = 0; i < nBuiltinFonts; ++i) {
417 if (!name->cmp(builtinFonts[i].name)) {
418 builtinFont = &builtinFonts[i];
424 // default ascent/descent values
426 ascent = 0.001 * builtinFont->ascent;
427 descent = 0.001 * builtinFont->descent;
428 fontBBox[0] = 0.001 * builtinFont->bbox[0];
429 fontBBox[1] = 0.001 * builtinFont->bbox[1];
430 fontBBox[2] = 0.001 * builtinFont->bbox[2];
431 fontBBox[3] = 0.001 * builtinFont->bbox[3];
435 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
438 // get info from font descriptor
439 readFontDescriptor(xref, fontDict);
441 // look for an external font file
445 fontMat[0] = fontMat[3] = 1;
446 fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
447 if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
448 for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
449 if (obj1.arrayGet(i, &obj2)->isNum()) {
450 fontMat[i] = obj2.getNum();
457 // get Type 3 bounding box, font definition, and resources
458 if (type == fontType3) {
459 if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
460 for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
461 if (obj1.arrayGet(i, &obj2)->isNum()) {
462 fontBBox[i] = obj2.getNum();
468 if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
469 error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
472 if (!fontDict->lookup("Resources", &resources)->isDict()) {
477 //----- build the font encoding -----
479 // Encodings start with a base encoding, which can come from
480 // (in order of priority):
481 // 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
482 // - MacRoman / MacExpert / WinAnsi / Standard
483 // 2. embedded or external font file
485 // - builtin --> builtin encoding
486 // - TrueType --> MacRomanEncoding
487 // - others --> StandardEncoding
488 // and then add a list of differences (if any) from
489 // FontDict.Encoding.Differences.
491 // check FontDict for base encoding
492 hasEncoding = gFalse;
494 baseEncFromFontFile = gFalse;
495 fontDict->lookup("Encoding", &obj1);
497 obj1.dictLookup("BaseEncoding", &obj2);
498 if (obj2.isName("MacRomanEncoding")) {
500 baseEnc = macRomanEncoding;
501 } else if (obj2.isName("MacExpertEncoding")) {
503 baseEnc = macExpertEncoding;
504 } else if (obj2.isName("WinAnsiEncoding")) {
506 baseEnc = winAnsiEncoding;
507 } else if (obj2.isName("StandardEncoding")) {
509 baseEnc = standardEncoding;
512 } else if (obj1.isName("MacRomanEncoding")) {
514 baseEnc = macRomanEncoding;
515 } else if (obj1.isName("MacExpertEncoding")) {
517 baseEnc = macExpertEncoding;
518 } else if (obj1.isName("WinAnsiEncoding")) {
520 baseEnc = winAnsiEncoding;
521 } else if (obj1.isName("StandardEncoding")) {
523 baseEnc = standardEncoding;
526 // check embedded or external font file for base encoding
527 // (only for Type 1 fonts - trying to get an encoding out of a
528 // TrueType font is a losing proposition)
531 if ((type == fontType1 || type == fontType1C) &&
532 (extFontFile || embFontID.num >= 0)) {
534 buf = readExtFontFile(&len);
536 buf = readEmbFontFile(xref, &len);
539 if (type == fontType1C && !strncmp(buf, "%!", 2)) {
540 // various tools (including Adobe's) occasionally embed Type 1
541 // fonts but label them Type 1C
544 if (type == fontType1) {
545 fontFile = new Type1FontFile(buf, len);
547 fontFile = new Type1CFontFile(buf, len);
549 if (fontFile->getName()) {
553 embFontName = new GString(fontFile->getName());
556 baseEnc = fontFile->getEncoding();
557 baseEncFromFontFile = gTrue;
563 // get default base encoding
566 baseEnc = builtinFont->defaultBaseEnc;
567 } else if (type == fontTrueType) {
568 baseEnc = macRomanEncoding;
570 baseEnc = standardEncoding;
574 // copy the base encoding
575 for (i = 0; i < 256; ++i) {
577 if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
578 enc[i] = copyString(baseEnc[i]);
582 // merge differences into encoding
584 obj1.dictLookup("Differences", &obj2);
585 if (obj2.isArray()) {
588 for (i = 0; i < obj2.arrayGetLength(); ++i) {
589 obj2.arrayGet(i, &obj3);
591 code = obj3.getInt();
592 } else if (obj3.isName()) {
597 enc[code] = copyString(obj3.getName());
598 encFree[code] = gTrue;
602 error(-1, "Wrong type in font encoding resource differences (%s)",
615 //----- build the mapping to Unicode -----
617 // look for a ToUnicode CMap
618 if (!(ctu = readToUnicodeCMap(fontDict, 8))) {
620 // no ToUnicode CMap, so use the char names
622 // pass 1: use the name-to-Unicode mapping table
623 missing = hex = gFalse;
624 for (code = 0; code < 256; ++code) {
625 if ((charName = enc[code])) {
626 if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
627 strcmp(charName, ".notdef")) {
628 // if it wasn't in the name-to-Unicode table, check for a
629 // name that looks like 'Axx' or 'xx', where 'A' is any letter
630 // and 'xx' is two hex digits
631 if ((strlen(charName) == 3 &&
632 isalpha(charName[0]) &&
633 isxdigit(charName[1]) && isxdigit(charName[2]) &&
634 ((charName[1] >= 'a' && charName[1] <= 'f') ||
635 (charName[1] >= 'A' && charName[1] <= 'F') ||
636 (charName[2] >= 'a' && charName[2] <= 'f') ||
637 (charName[2] >= 'A' && charName[2] <= 'F'))) ||
638 (strlen(charName) == 2 &&
639 isxdigit(charName[0]) && isxdigit(charName[1]) &&
640 ((charName[0] >= 'a' && charName[0] <= 'f') ||
641 (charName[0] >= 'A' && charName[0] <= 'F') ||
642 (charName[1] >= 'a' && charName[1] <= 'f') ||
643 (charName[1] >= 'A' && charName[1] <= 'F')))) {
653 // pass 2: try to fill in the missing chars, looking for names of
654 // the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
655 // are any letters, 'xx' is two hex digits, and 'nn' is 2-4
657 if (missing && globalParams->getMapNumericCharNames()) {
658 for (code = 0; code < 256; ++code) {
659 if ((charName = enc[code]) && !toUnicode[code] &&
660 strcmp(charName, ".notdef")) {
661 n = strlen(charName);
663 if (hex && n == 3 && isalpha(charName[0]) &&
664 isxdigit(charName[1]) && isxdigit(charName[2])) {
665 sscanf(charName+1, "%x", &code2);
666 } else if (hex && n == 2 &&
667 isxdigit(charName[0]) && isxdigit(charName[1])) {
668 sscanf(charName, "%x", &code2);
669 } else if (!hex && n >= 2 && n <= 4 &&
670 isdigit(charName[0]) && isdigit(charName[1])) {
671 code2 = atoi(charName);
672 } else if (n >= 3 && n <= 5 &&
673 isdigit(charName[1]) && isdigit(charName[2])) {
674 code2 = atoi(charName+1);
675 } else if (n >= 4 && n <= 6 &&
676 isdigit(charName[2]) && isdigit(charName[3])) {
677 code2 = atoi(charName+2);
679 if (code2 >= 0 && code2 <= 0xff) {
680 toUnicode[code] = (Unicode)code2;
686 ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
689 //----- get the character widths -----
691 // initialize all widths
692 for (code = 0; code < 256; ++code) {
693 widths[code] = missingWidth * 0.001;
696 // use widths from font dict, if present
697 fontDict->lookup("FirstChar", &obj1);
698 firstChar = obj1.isInt() ? obj1.getInt() : 0;
700 fontDict->lookup("LastChar", &obj1);
701 lastChar = obj1.isInt() ? obj1.getInt() : 255;
703 mul = (type == fontType3) ? fontMat[0] : 0.001;
704 fontDict->lookup("Widths", &obj1);
705 if (obj1.isArray()) {
706 flags |= fontFixedWidth;
707 for (code = firstChar; code <= lastChar; ++code) {
708 obj1.arrayGet(code - firstChar, &obj2);
710 widths[code] = obj2.getNum() * mul;
711 if (widths[code] != widths[firstChar]) {
712 flags &= ~fontFixedWidth;
718 // use widths from built-in font
719 } else if (builtinFont) {
720 // this is a kludge for broken PDF files that encode char 32
722 if (builtinFont->widths->getWidth("space", &w)) {
723 widths[32] = 0.001 * w;
725 for (code = 0; code < 256; ++code) {
726 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
727 widths[code] = 0.001 * w;
731 // couldn't find widths -- use defaults
733 // this is technically an error -- the Widths entry is required
734 // for all but the Base-14 fonts -- but certain PDF generators
735 // apparently don't include widths for Arial and TimesNewRoman
736 if (isFixedWidth()) {
738 } else if (isSerif()) {
749 builtinFont = builtinFontSubst[i];
750 // this is a kludge for broken PDF files that encode char 32
752 if (builtinFont->widths->getWidth("space", &w)) {
753 widths[32] = 0.001 * w;
755 for (code = 0; code < 256; ++code) {
756 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
757 widths[code] = 0.001 * w;
766 Gfx8BitFont::~Gfx8BitFont() {
769 for (i = 0; i < 256; ++i) {
770 if (encFree[i] && enc[i]) {
775 if (charProcs.isDict()) {
778 if (resources.isDict()) {
783 int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
784 Unicode *u, int uSize, int *uLen,
785 double *dx, double *dy, double *ox, double *oy) {
788 *code = c = (CharCode)(*s & 0xff);
789 *uLen = ctu->mapToUnicode(c, u, uSize);
795 CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
800 Dict *Gfx8BitFont::getCharProcs() {
801 return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
804 Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
805 if (charProcs.isDict()) {
806 charProcs.dictLookup(enc[code], proc);
813 Dict *Gfx8BitFont::getResources() {
814 return resources.isDict() ? resources.getDict() : (Dict *)NULL;
817 //------------------------------------------------------------------------
819 //------------------------------------------------------------------------
821 static int cmpWidthExcep(const void *w1, const void *w2) {
822 return ((GfxFontCIDWidthExcep *)w1)->first -
823 ((GfxFontCIDWidthExcep *)w2)->first;
826 static int cmpWidthExcepV(const void *w1, const void *w2) {
827 return ((GfxFontCIDWidthExcepV *)w1)->first -
828 ((GfxFontCIDWidthExcepV *)w2)->first;
831 GfxCIDFont::GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
833 GfxFont(tagA, idA, nameA)
836 GString *collection, *cMapName;
837 Object desFontDictObj;
838 Object obj1, obj2, obj3, obj4, obj5, obj6;
840 int excepsSize, i, j, k;
844 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
847 widths.defWidth = 1.0;
848 widths.defHeight = -1.0;
849 widths.defVY = 0.880;
850 widths.exceps = NULL;
852 widths.excepsV = NULL;
857 // get the descendant font
858 if (!fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
859 error(-1, "Missing DescendantFonts entry in Type 0 font");
863 if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
864 error(-1, "Bad descendant font in Type 0 font");
868 desFontDict = desFontDictObj.getDict();
871 if (!desFontDict->lookup("Subtype", &obj1)) {
872 error(-1, "Missing Subtype entry in Type 0 descendant font");
875 if (obj1.isName("CIDFontType0")) {
877 } else if (obj1.isName("CIDFontType2")) {
880 error(-1, "Unknown Type 0 descendant font type '%s'",
881 obj1.isName() ? obj1.getName() : "???");
886 // get info from font descriptor
887 readFontDescriptor(xref, desFontDict);
889 // look for an external font file
892 //----- encoding info -----
895 if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
896 error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
899 obj1.dictLookup("Registry", &obj2);
900 obj1.dictLookup("Ordering", &obj3);
901 if (!obj2.isString() || !obj3.isString()) {
902 error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
905 collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
910 // look for a ToUnicode CMap
911 if (!(ctu = readToUnicodeCMap(fontDict, 16))) {
913 // the "Adobe-Identity" and "Adobe-UCS" collections don't have
914 // cidToUnicode files
915 if (collection->cmp("Adobe-Identity") &&
916 collection->cmp("Adobe-UCS")) {
918 // look for a user-supplied .cidToUnicode file
919 if (!(ctu = globalParams->getCIDToUnicode(collection))) {
920 error(-1, "Unknown character collection '%s'",
921 collection->getCString());
928 // encoding (i.e., CMap)
929 //~ need to handle a CMap stream here
930 //~ also need to deal with the UseCMap entry in the stream dict
931 if (!fontDict->lookup("Encoding", &obj1)->isName()) {
932 error(-1, "Missing or invalid Encoding entry in Type 0 font");
936 cMapName = new GString(obj1.getName());
938 if (!(cMap = globalParams->getCMap(collection, cMapName))) {
939 error(-1, "Unknown CMap '%s' for character collection '%s'",
940 cMapName->getCString(), collection->getCString());
948 // CIDToGIDMap (for embedded TrueType fonts)
949 if (type == fontCIDType2) {
950 fontDict->lookup("CIDToGIDMap", &obj1);
951 if (obj1.isStream()) {
954 cidToGID = (Gushort *)gmalloc(i * sizeof(Gushort));
956 while ((c1 = obj1.streamGetChar()) != EOF &&
957 (c2 = obj1.streamGetChar()) != EOF) {
958 if (cidToGIDLen == i) {
960 cidToGID = (Gushort *)grealloc(cidToGID, i * sizeof(Gushort));
962 cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
964 } else if (!obj1.isName("Identity") && !obj1.isNull()) {
965 error(-1, "Invalid CIDToGIDMap entry in CID font");
970 //----- character metrics -----
972 // default char width
973 if (desFontDict->lookup("DW", &obj1)->isInt()) {
974 widths.defWidth = obj1.getInt() * 0.001;
978 // char width exceptions
979 if (desFontDict->lookup("W", &obj1)->isArray()) {
982 while (i + 1 < obj1.arrayGetLength()) {
983 obj1.arrayGet(i, &obj2);
984 obj1.arrayGet(i + 1, &obj3);
985 if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
986 if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
987 if (widths.nExceps == excepsSize) {
989 widths.exceps = (GfxFontCIDWidthExcep *)
990 grealloc(widths.exceps,
991 excepsSize * sizeof(GfxFontCIDWidthExcep));
993 widths.exceps[widths.nExceps].first = obj2.getInt();
994 widths.exceps[widths.nExceps].last = obj3.getInt();
995 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
998 error(-1, "Bad widths array in Type 0 font");
1002 } else if (obj2.isInt() && obj3.isArray()) {
1003 if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1004 excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1005 widths.exceps = (GfxFontCIDWidthExcep *)
1006 grealloc(widths.exceps,
1007 excepsSize * sizeof(GfxFontCIDWidthExcep));
1010 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1011 if (obj3.arrayGet(k, &obj4)->isNum()) {
1012 widths.exceps[widths.nExceps].first = j;
1013 widths.exceps[widths.nExceps].last = j;
1014 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1018 error(-1, "Bad widths array in Type 0 font");
1024 error(-1, "Bad widths array in Type 0 font");
1030 qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
1035 // default metrics for vertical font
1036 if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1037 obj1.arrayGetLength() == 2) {
1038 if (obj1.arrayGet(0, &obj2)->isNum()) {
1039 widths.defVY = obj1.getNum() * 0.001;
1042 if (obj1.arrayGet(1, &obj2)->isNum()) {
1043 widths.defHeight = obj1.getNum() * 0.001;
1049 // char metric exceptions for vertical font
1050 if (desFontDict->lookup("W2", &obj1)->isArray()) {
1053 while (i + 1 < obj1.arrayGetLength()) {
1054 obj1.arrayGet(0, &obj2);
1055 obj2.arrayGet(0, &obj3);
1056 if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1057 if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1058 obj1.arrayGet(i + 3, &obj5)->isNum() &&
1059 obj1.arrayGet(i + 4, &obj6)->isNum()) {
1060 if (widths.nExcepsV == excepsSize) {
1062 widths.excepsV = (GfxFontCIDWidthExcepV *)
1063 grealloc(widths.excepsV,
1064 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1066 widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1067 widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1068 widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1069 widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1070 widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1073 error(-1, "Bad widths (W2) array in Type 0 font");
1079 } else if (obj2.isInt() && obj3.isArray()) {
1080 if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
1082 (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
1083 widths.excepsV = (GfxFontCIDWidthExcepV *)
1084 grealloc(widths.excepsV,
1085 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1088 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1089 if (obj3.arrayGet(k, &obj4)->isNum() &&
1090 obj3.arrayGet(k, &obj5)->isNum() &&
1091 obj3.arrayGet(k, &obj6)->isNum()) {
1092 widths.excepsV[widths.nExceps].first = j;
1093 widths.excepsV[widths.nExceps].last = j;
1094 widths.excepsV[widths.nExceps].height = obj4.getNum() * 0.001;
1095 widths.excepsV[widths.nExceps].vx = obj5.getNum() * 0.001;
1096 widths.excepsV[widths.nExceps].vy = obj6.getNum() * 0.001;
1100 error(-1, "Bad widths (W2) array in Type 0 font");
1108 error(-1, "Bad widths (W2) array in Type 0 font");
1114 qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
1119 desFontDictObj.free();
1129 desFontDictObj.free();
1133 GfxCIDFont::~GfxCIDFont() {
1140 gfree(widths.exceps);
1141 gfree(widths.excepsV);
1147 int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
1148 Unicode *u, int uSize, int *uLen,
1149 double *dx, double *dy, double *ox, double *oy) {
1151 double w, h, vx, vy;
1161 *code = (CharCode)(cid = cMap->getCID(s, len, &n));
1163 *uLen = ctu->mapToUnicode(cid, u, uSize);
1169 if (cMap->getWMode() == 0) {
1170 w = widths.defWidth;
1172 if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
1175 // invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1178 if (widths.exceps[m].first <= cid) {
1184 if (cid <= widths.exceps[a].last) {
1185 w = widths.exceps[a].width;
1192 h = widths.defHeight;
1193 vx = widths.defWidth / 2;
1195 if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
1197 b = widths.nExcepsV;
1198 // invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1201 if (widths.excepsV[m].last <= cid) {
1207 if (cid <= widths.excepsV[a].last) {
1208 h = widths.excepsV[a].height;
1209 vx = widths.excepsV[a].vx;
1210 vy = widths.excepsV[a].vy;
1223 int GfxCIDFont::getWMode() {
1224 return cMap ? cMap->getWMode() : 0;
1227 CharCodeToUnicode *GfxCIDFont::getToUnicode() {
1232 GString *GfxCIDFont::getCollection() {
1233 return cMap ? cMap->getCollection() : (GString *)NULL;
1236 //------------------------------------------------------------------------
1238 //------------------------------------------------------------------------
1240 GfxFontDict::GfxFontDict(XRef *xref, Dict *fontDict) {
1244 numFonts = fontDict->getLength();
1245 fonts = (GfxFont **)gmalloc(numFonts * sizeof(GfxFont *));
1246 for (i = 0; i < numFonts; ++i) {
1247 fontDict->getValNF(i, &obj1);
1248 obj1.fetch(xref, &obj2);
1249 if (obj1.isRef() && obj2.isDict()) {
1250 fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i),
1251 obj1.getRef(), obj2.getDict());
1252 if (fonts[i] && !fonts[i]->isOk()) {
1257 error(-1, "font resource is not a dictionary reference");
1265 GfxFontDict::~GfxFontDict() {
1268 for (i = 0; i < numFonts; ++i) {
1276 GfxFont *GfxFontDict::lookup(char *tag) {
1279 for (i = 0; i < numFonts; ++i) {
1280 if (fonts[i] && fonts[i]->matches(tag)) {