1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
23 #include "GlobalParams.h"
25 #include "CharCodeToUnicode.h"
26 #include "FontEncodingTables.h"
27 #include "BuiltinFontTables.h"
28 #include "FoFiType1.h"
29 #include "FoFiType1C.h"
30 #include "FoFiTrueType.h"
33 //------------------------------------------------------------------------
35 struct StdFontMapEntry {
40 // Acrobat 4.0 and earlier substituted Base14-compatible fonts without
41 // providing Widths and a FontDescriptor, so we munge the names into
42 // the proper Base14 names. This table is from implementation note 44
43 // in the PDF 1.4 spec, with some additions based on empirical
45 static StdFontMapEntry stdFontMap[] = {
46 { "Arial", "Helvetica" },
47 { "Arial,Bold", "Helvetica-Bold" },
48 { "Arial,BoldItalic", "Helvetica-BoldOblique" },
49 { "Arial,Italic", "Helvetica-Oblique" },
50 { "Arial-Bold", "Helvetica-Bold" },
51 { "Arial-BoldItalic", "Helvetica-BoldOblique" },
52 { "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
53 { "Arial-BoldMT", "Helvetica-Bold" },
54 { "Arial-Italic", "Helvetica-Oblique" },
55 { "Arial-ItalicMT", "Helvetica-Oblique" },
56 { "ArialMT", "Helvetica" },
57 { "Courier,Bold", "Courier-Bold" },
58 { "Courier,Italic", "Courier-Oblique" },
59 { "Courier,BoldItalic", "Courier-BoldOblique" },
60 { "CourierNew", "Courier" },
61 { "CourierNew,Bold", "Courier-Bold" },
62 { "CourierNew,BoldItalic", "Courier-BoldOblique" },
63 { "CourierNew,Italic", "Courier-Oblique" },
64 { "CourierNew-Bold", "Courier-Bold" },
65 { "CourierNew-BoldItalic", "Courier-BoldOblique" },
66 { "CourierNew-Italic", "Courier-Oblique" },
67 { "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
68 { "CourierNewPS-BoldMT", "Courier-Bold" },
69 { "CourierNewPS-ItalicMT", "Courier-Oblique" },
70 { "CourierNewPSMT", "Courier" },
71 { "Helvetica,Bold", "Helvetica-Bold" },
72 { "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
73 { "Helvetica,Italic", "Helvetica-Oblique" },
74 { "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
75 { "Helvetica-Italic", "Helvetica-Oblique" },
76 { "Symbol,Bold", "Symbol" },
77 { "Symbol,BoldItalic", "Symbol" },
78 { "Symbol,Italic", "Symbol" },
79 { "TimesNewRoman", "Times-Roman" },
80 { "TimesNewRoman,Bold", "Times-Bold" },
81 { "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
82 { "TimesNewRoman,Italic", "Times-Italic" },
83 { "TimesNewRoman-Bold", "Times-Bold" },
84 { "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
85 { "TimesNewRoman-Italic", "Times-Italic" },
86 { "TimesNewRomanPS", "Times-Roman" },
87 { "TimesNewRomanPS-Bold", "Times-Bold" },
88 { "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
89 { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
90 { "TimesNewRomanPS-BoldMT", "Times-Bold" },
91 { "TimesNewRomanPS-Italic", "Times-Italic" },
92 { "TimesNewRomanPS-ItalicMT", "Times-Italic" },
93 { "TimesNewRomanPSMT", "Times-Roman" },
94 { "TimesNewRomanPSMT,Bold", "Times-Bold" },
95 { "TimesNewRomanPSMT,BoldItalic", "Times-BoldItalic" },
96 { "TimesNewRomanPSMT,Italic", "Times-Italic" }
99 //------------------------------------------------------------------------
101 //------------------------------------------------------------------------
103 GfxFont *GfxFont::makeFont(XRef *xref, char *tagA, Ref idA, Dict *fontDict) {
108 // get base font name
110 fontDict->lookup("BaseFont", &obj1);
112 nameA = new GString(obj1.getName());
118 fontDict->lookup("Subtype", &obj1);
119 if (obj1.isName("Type1") || obj1.isName("MMType1")) {
120 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1, fontDict);
121 } else if (obj1.isName("Type1C")) {
122 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType1C, fontDict);
123 } else if (obj1.isName("Type3")) {
124 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontType3, fontDict);
125 } else if (obj1.isName("TrueType")) {
126 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontTrueType, fontDict);
127 } else if (obj1.isName("Type0")) {
128 font = new GfxCIDFont(xref, tagA, idA, nameA, fontDict);
130 error(-1, "Unknown font type: '%s'",
131 obj1.isName() ? obj1.getName() : "???");
132 font = new Gfx8BitFont(xref, tagA, idA, nameA, fontUnknownType, fontDict);
139 GfxFont::GfxFont(char *tagA, Ref idA, GString *nameA) {
141 tag = new GString(tagA);
149 GfxFont::~GfxFont() {
151 if (origName && origName != name) {
165 void GfxFont::readFontDescriptor(XRef *xref, Dict *fontDict) {
166 Object obj1, obj2, obj3, obj4;
170 // assume Times-Roman by default (for substitution purposes)
177 if (fontDict->lookup("FontDescriptor", &obj1)->isDict()) {
180 if (obj1.dictLookup("Flags", &obj2)->isInt()) {
181 flags = obj2.getInt();
186 obj1.dictLookup("FontName", &obj2);
188 embFontName = new GString(obj2.getName());
192 // look for embedded font file
193 if (obj1.dictLookupNF("FontFile", &obj2)->isRef()) {
194 if (type == fontType1) {
195 embFontID = obj2.getRef();
197 error(-1, "Mismatch between font type and embedded font file");
201 if (embFontID.num == -1 &&
202 obj1.dictLookupNF("FontFile2", &obj2)->isRef()) {
203 if (type == fontTrueType || type == fontCIDType2) {
204 embFontID = obj2.getRef();
206 error(-1, "Mismatch between font type and embedded font file");
210 if (embFontID.num == -1 &&
211 obj1.dictLookupNF("FontFile3", &obj2)->isRef()) {
212 if (obj2.fetch(xref, &obj3)->isStream()) {
213 obj3.streamGetDict()->lookup("Subtype", &obj4);
214 if (obj4.isName("Type1")) {
215 if (type == fontType1) {
216 embFontID = obj2.getRef();
218 error(-1, "Mismatch between font type and embedded font file");
220 } else if (obj4.isName("Type1C")) {
221 if (type == fontType1) {
223 embFontID = obj2.getRef();
224 } else if (type == fontType1C) {
225 embFontID = obj2.getRef();
227 error(-1, "Mismatch between font type and embedded font file");
229 } else if (obj4.isName("TrueType")) {
230 if (type == fontTrueType) {
231 embFontID = obj2.getRef();
233 error(-1, "Mismatch between font type and embedded font file");
235 } else if (obj4.isName("CIDFontType0C")) {
236 if (type == fontCIDType0) {
237 type = fontCIDType0C;
238 embFontID = obj2.getRef();
240 error(-1, "Mismatch between font type and embedded font file");
243 error(-1, "Unknown embedded font type '%s'",
244 obj4.isName() ? obj4.getName() : "???");
252 // look for MissingWidth
253 obj1.dictLookup("MissingWidth", &obj2);
255 missingWidth = obj2.getNum();
259 // get Ascent and Descent
260 obj1.dictLookup("Ascent", &obj2);
262 t = 0.001 * obj2.getNum();
263 // some broken font descriptors set ascent and descent to 0
269 obj1.dictLookup("Descent", &obj2);
271 t = 0.001 * obj2.getNum();
272 // some broken font descriptors set ascent and descent to 0
276 // some broken font descriptors specify a positive descent
284 if (obj1.dictLookup("FontBBox", &obj2)->isArray()) {
285 for (i = 0; i < 4 && i < obj2.arrayGetLength(); ++i) {
286 if (obj2.arrayGet(i, &obj3)->isNum()) {
287 fontBBox[i] = 0.001 * obj3.getNum();
298 CharCodeToUnicode *GfxFont::readToUnicodeCMap(Dict *fontDict, int nBits,
299 CharCodeToUnicode *ctu) {
304 if (!fontDict->lookup("ToUnicode", &obj1)->isStream()) {
310 while ((c = obj1.streamGetChar()) != EOF) {
316 ctu->mergeCMap(buf, nBits);
318 ctu = CharCodeToUnicode::parseCMap(buf, nBits);
324 void GfxFont::findExtFontFile() {
325 static char *type1Exts[] = { ".pfa", ".pfb", ".ps", "", NULL };
326 static char *ttExts[] = { ".ttf", NULL };
329 if (type == fontType1) {
330 extFontFile = globalParams->findFontFile(name, type1Exts);
331 } else if (type == fontTrueType) {
332 extFontFile = globalParams->findFontFile(name, ttExts);
337 char *GfxFont::readExtFontFile(int *len) {
341 if (!(f = fopen(extFontFile->getCString(), "rb"))) {
342 error(-1, "External font file '%s' vanished", extFontFile->getCString());
345 fseek(f, 0, SEEK_END);
346 *len = (int)ftell(f);
347 fseek(f, 0, SEEK_SET);
348 buf = (char *)gmalloc(*len);
349 if ((int)fread(buf, 1, *len, f) != *len) {
350 error(-1, "Error reading external font file '%s'",
351 extFontFile->getCString());
357 char *GfxFont::readEmbFontFile(XRef *xref, int *len) {
364 obj1.initRef(embFontID.num, embFontID.gen);
365 obj1.fetch(xref, &obj2);
366 if (!obj2.isStream()) {
367 error(-1, "Embedded font file is not a stream");
373 str = obj2.getStream();
378 while ((c = str->getChar()) != EOF) {
381 buf = (char *)grealloc(buf, size);
394 //------------------------------------------------------------------------
396 //------------------------------------------------------------------------
398 Gfx8BitFont::Gfx8BitFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
399 GfxFontType typeA, Dict *fontDict):
400 GfxFont(tagA, idA, nameA)
402 BuiltinFont *builtinFont;
404 GBool baseEncFromFontFile;
412 Unicode toUnicode[256];
413 CharCodeToUnicode *utu, *ctu2;
416 int firstChar, lastChar;
418 Object obj1, obj2, obj3;
424 // do font name substitution for various aliases of the Base 14 font
428 b = sizeof(stdFontMap) / sizeof(StdFontMapEntry);
429 // invariant: stdFontMap[a].altName <= name < stdFontMap[b].altName
432 if (name->cmp(stdFontMap[m].altName) >= 0) {
438 if (!name->cmp(stdFontMap[a].altName)) {
439 name = new GString(stdFontMap[a].properName);
443 // is it a built-in font?
446 for (i = 0; i < nBuiltinFonts; ++i) {
447 if (!name->cmp(builtinFonts[i].name)) {
448 builtinFont = &builtinFonts[i];
454 // default ascent/descent values
456 ascent = 0.001 * builtinFont->ascent;
457 descent = 0.001 * builtinFont->descent;
458 fontBBox[0] = 0.001 * builtinFont->bbox[0];
459 fontBBox[1] = 0.001 * builtinFont->bbox[1];
460 fontBBox[2] = 0.001 * builtinFont->bbox[2];
461 fontBBox[3] = 0.001 * builtinFont->bbox[3];
465 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
468 // get info from font descriptor
469 readFontDescriptor(xref, fontDict);
471 // look for an external font file
475 fontMat[0] = fontMat[3] = 1;
476 fontMat[1] = fontMat[2] = fontMat[4] = fontMat[5] = 0;
477 if (fontDict->lookup("FontMatrix", &obj1)->isArray()) {
478 for (i = 0; i < 6 && i < obj1.arrayGetLength(); ++i) {
479 if (obj1.arrayGet(i, &obj2)->isNum()) {
480 fontMat[i] = obj2.getNum();
487 // get Type 3 bounding box, font definition, and resources
488 if (type == fontType3) {
489 if (fontDict->lookup("FontBBox", &obj1)->isArray()) {
490 for (i = 0; i < 4 && i < obj1.arrayGetLength(); ++i) {
491 if (obj1.arrayGet(i, &obj2)->isNum()) {
492 fontBBox[i] = obj2.getNum();
498 if (!fontDict->lookup("CharProcs", &charProcs)->isDict()) {
499 error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
502 if (!fontDict->lookup("Resources", &resources)->isDict()) {
507 //----- build the font encoding -----
509 // Encodings start with a base encoding, which can come from
510 // (in order of priority):
511 // 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
512 // - MacRoman / MacExpert / WinAnsi / Standard
513 // 2. embedded or external font file
515 // - builtin --> builtin encoding
516 // - TrueType --> MacRomanEncoding
517 // - others --> StandardEncoding
518 // and then add a list of differences (if any) from
519 // FontDict.Encoding.Differences.
521 // check FontDict for base encoding
522 hasEncoding = gFalse;
523 usesMacRomanEnc = gFalse;
525 baseEncFromFontFile = gFalse;
526 fontDict->lookup("Encoding", &obj1);
528 obj1.dictLookup("BaseEncoding", &obj2);
529 if (obj2.isName("MacRomanEncoding")) {
531 usesMacRomanEnc = gTrue;
532 baseEnc = macRomanEncoding;
533 } else if (obj2.isName("MacExpertEncoding")) {
535 baseEnc = macExpertEncoding;
536 } else if (obj2.isName("WinAnsiEncoding")) {
538 baseEnc = winAnsiEncoding;
539 } else if (obj2.isName("StandardEncoding")) {
541 baseEnc = standardEncoding;
544 } else if (obj1.isName("MacRomanEncoding")) {
546 usesMacRomanEnc = gTrue;
547 baseEnc = macRomanEncoding;
548 } else if (obj1.isName("MacExpertEncoding")) {
550 baseEnc = macExpertEncoding;
551 } else if (obj1.isName("WinAnsiEncoding")) {
553 baseEnc = winAnsiEncoding;
554 } else if (obj1.isName("StandardEncoding")) {
556 baseEnc = standardEncoding;
559 // check embedded or external font file for base encoding
560 // (only for Type 1 fonts - trying to get an encoding out of a
561 // TrueType font is a losing proposition)
565 if (type == fontType1 && (extFontFile || embFontID.num >= 0)) {
567 ffT1 = FoFiType1::load(extFontFile->getCString());
569 buf = readEmbFontFile(xref, &len);
570 ffT1 = FoFiType1::make(buf, len);
573 if (ffT1->getName()) {
577 embFontName = new GString(ffT1->getName());
580 baseEnc = ffT1->getEncoding();
581 baseEncFromFontFile = gTrue;
584 } else if (type == fontType1C && (extFontFile || embFontID.num >= 0)) {
586 ffT1C = FoFiType1C::load(extFontFile->getCString());
588 buf = readEmbFontFile(xref, &len);
589 ffT1C = FoFiType1C::make(buf, len);
592 if (ffT1C->getName()) {
596 embFontName = new GString(ffT1C->getName());
599 baseEnc = ffT1C->getEncoding();
600 baseEncFromFontFile = gTrue;
608 // get default base encoding
611 baseEnc = builtinFont->defaultBaseEnc;
613 } else if (type == fontTrueType) {
614 baseEnc = winAnsiEncoding;
616 baseEnc = standardEncoding;
620 // copy the base encoding
621 for (i = 0; i < 256; ++i) {
623 if ((encFree[i] = baseEncFromFontFile) && enc[i]) {
624 enc[i] = copyString(baseEnc[i]);
628 // some Type 1C font files have empty encodings, which can break the
629 // T1C->T1 conversion (since the 'seac' operator depends on having
630 // the accents in the encoding), so we fill in any gaps from
632 if (type == fontType1C && (extFontFile || embFontID.num >= 0) &&
633 baseEncFromFontFile) {
634 for (i = 0; i < 256; ++i) {
635 if (!enc[i] && standardEncoding[i]) {
636 enc[i] = standardEncoding[i];
642 // merge differences into encoding
644 obj1.dictLookup("Differences", &obj2);
645 if (obj2.isArray()) {
648 for (i = 0; i < obj2.arrayGetLength(); ++i) {
649 obj2.arrayGet(i, &obj3);
651 code = obj3.getInt();
652 } else if (obj3.isName()) {
653 if (code >= 0 && code < 256) {
657 enc[code] = copyString(obj3.getName());
658 encFree[code] = gTrue;
662 error(-1, "Wrong type in font encoding resource differences (%s)",
678 //----- build the mapping to Unicode -----
680 // pass 1: use the name-to-Unicode mapping table
681 missing = hex = gFalse;
682 for (code = 0; code < 256; ++code) {
683 if ((charName = enc[code])) {
684 if (!(toUnicode[code] = globalParams->mapNameToUnicode(charName)) &&
685 strcmp(charName, ".notdef")) {
686 // if it wasn't in the name-to-Unicode table, check for a
687 // name that looks like 'Axx' or 'xx', where 'A' is any letter
688 // and 'xx' is two hex digits
689 if ((strlen(charName) == 3 &&
690 isalpha(charName[0]) &&
691 isxdigit(charName[1]) && isxdigit(charName[2]) &&
692 ((charName[1] >= 'a' && charName[1] <= 'f') ||
693 (charName[1] >= 'A' && charName[1] <= 'F') ||
694 (charName[2] >= 'a' && charName[2] <= 'f') ||
695 (charName[2] >= 'A' && charName[2] <= 'F'))) ||
696 (strlen(charName) == 2 &&
697 isxdigit(charName[0]) && isxdigit(charName[1]) &&
698 ((charName[0] >= 'a' && charName[0] <= 'f') ||
699 (charName[0] >= 'A' && charName[0] <= 'F') ||
700 (charName[1] >= 'a' && charName[1] <= 'f') ||
701 (charName[1] >= 'A' && charName[1] <= 'F')))) {
711 // pass 2: try to fill in the missing chars, looking for names of
712 // the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
713 // are any letters, 'xx' is two hex digits, and 'nn' is 2-4
715 if (missing && globalParams->getMapNumericCharNames()) {
716 for (code = 0; code < 256; ++code) {
717 if ((charName = enc[code]) && !toUnicode[code] &&
718 strcmp(charName, ".notdef")) {
719 n = strlen(charName);
721 if (hex && n == 3 && isalpha(charName[0]) &&
722 isxdigit(charName[1]) && isxdigit(charName[2])) {
723 sscanf(charName+1, "%x", &code2);
724 } else if (hex && n == 2 &&
725 isxdigit(charName[0]) && isxdigit(charName[1])) {
726 sscanf(charName, "%x", &code2);
727 } else if (!hex && n >= 2 && n <= 4 &&
728 isdigit(charName[0]) && isdigit(charName[1])) {
729 code2 = atoi(charName);
730 } else if (n >= 3 && n <= 5 &&
731 isdigit(charName[1]) && isdigit(charName[2])) {
732 code2 = atoi(charName+1);
733 } else if (n >= 4 && n <= 6 &&
734 isdigit(charName[2]) && isdigit(charName[3])) {
735 code2 = atoi(charName+2);
737 if (code2 >= 0 && code2 <= 0xff) {
738 toUnicode[code] = (Unicode)code2;
744 // construct the char code -> Unicode mapping object
745 ctu = CharCodeToUnicode::make8BitToUnicode(toUnicode);
747 // merge in a ToUnicode CMap, if there is one -- this overwrites
748 // existing entries in ctu, i.e., the ToUnicode CMap takes
749 // precedence, but the other encoding info is allowed to fill in any
751 readToUnicodeCMap(fontDict, 8, ctu);
753 // look for a Unicode-to-Unicode mapping
754 if (name && (utu = globalParams->getUnicodeToUnicode(name))) {
755 for (i = 0; i < 256; ++i) {
758 ctu2 = CharCodeToUnicode::make8BitToUnicode(toUnicode);
759 for (i = 0; i < 256; ++i) {
760 n = ctu->mapToUnicode((CharCode)i, uBuf, 8);
762 n = utu->mapToUnicode((CharCode)uBuf[0], uBuf, 8);
764 ctu2->setMapping((CharCode)i, uBuf, n);
773 //----- get the character widths -----
775 // initialize all widths
776 for (code = 0; code < 256; ++code) {
777 widths[code] = missingWidth * 0.001;
780 // use widths from font dict, if present
781 fontDict->lookup("FirstChar", &obj1);
782 firstChar = obj1.isInt() ? obj1.getInt() : 0;
784 if (firstChar < 0 || firstChar > 255) {
787 fontDict->lookup("LastChar", &obj1);
788 lastChar = obj1.isInt() ? obj1.getInt() : 255;
790 if (lastChar < 0 || lastChar > 255) {
793 mul = (type == fontType3) ? fontMat[0] : 0.001;
794 fontDict->lookup("Widths", &obj1);
795 if (obj1.isArray()) {
796 flags |= fontFixedWidth;
797 if (obj1.arrayGetLength() < lastChar - firstChar + 1) {
798 lastChar = firstChar + obj1.arrayGetLength() - 1;
800 for (code = firstChar; code <= lastChar; ++code) {
801 obj1.arrayGet(code - firstChar, &obj2);
803 widths[code] = obj2.getNum() * mul;
804 if (widths[code] != widths[firstChar]) {
805 flags &= ~fontFixedWidth;
811 // use widths from built-in font
812 } else if (builtinFont) {
813 // this is a kludge for broken PDF files that encode char 32
815 if (builtinFont->widths->getWidth("space", &w)) {
816 widths[32] = 0.001 * w;
818 for (code = 0; code < 256; ++code) {
819 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
820 widths[code] = 0.001 * w;
824 // couldn't find widths -- use defaults
826 // this is technically an error -- the Widths entry is required
827 // for all but the Base-14 fonts -- but certain PDF generators
828 // apparently don't include widths for Arial and TimesNewRoman
829 if (isFixedWidth()) {
831 } else if (isSerif()) {
842 builtinFont = builtinFontSubst[i];
843 // this is a kludge for broken PDF files that encode char 32
845 if (builtinFont->widths->getWidth("space", &w)) {
846 widths[32] = 0.001 * w;
848 for (code = 0; code < 256; ++code) {
849 if (enc[code] && builtinFont->widths->getWidth(enc[code], &w)) {
850 widths[code] = 0.001 * w;
859 Gfx8BitFont::~Gfx8BitFont() {
862 for (i = 0; i < 256; ++i) {
863 if (encFree[i] && enc[i]) {
868 if (charProcs.isDict()) {
871 if (resources.isDict()) {
876 int Gfx8BitFont::getNextChar(char *s, int len, CharCode *code,
877 Unicode *u, int uSize, int *uLen,
878 double *dx, double *dy, double *ox, double *oy) {
881 *code = c = (CharCode)(*s & 0xff);
882 *uLen = ctu->mapToUnicode(c, u, uSize);
888 CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
893 Gushort *Gfx8BitFont::getCodeToGIDMap(FoFiTrueType *ff) {
895 int cmapPlatform, cmapEncoding;
896 int unicodeCmap, macRomanCmap, msSymbolCmap, cmap;
897 GBool useMacRoman, useUnicode;
902 map = (Gushort *)gmalloc(256 * sizeof(Gushort));
903 for (i = 0; i < 256; ++i) {
907 // To match up with the Adobe-defined behaviour, we choose a cmap
909 // 1. If the PDF font has an encoding:
910 // 1a. If the PDF font specified MacRomanEncoding and the
911 // TrueType font has a Macintosh Roman cmap, use it, and
912 // reverse map the char names through MacRomanEncoding to
914 // 1b. If the TrueType font has a Microsoft Unicode cmap or a
915 // non-Microsoft Unicode cmap, use it, and use the Unicode
916 // indexes, not the char codes.
917 // 1c. If the PDF font is symbolic and the TrueType font has a
918 // Microsoft Symbol cmap, use it, and use char codes
919 // directly (possibly with an offset of 0xf000).
920 // 1d. If the TrueType font has a Macintosh Roman cmap, use it,
922 // 2. If the PDF font does not have an encoding:
923 // 2a. If the TrueType font has a Macintosh Roman cmap, use it,
924 // and use char codes directly (possibly with an offset of
926 // 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
927 // and use char codes directly (possible with an offset of
929 // 3. If none of these rules apply, use the first cmap and hope for
930 // the best (this shouldn't happen).
931 unicodeCmap = macRomanCmap = msSymbolCmap = -1;
932 for (i = 0; i < ff->getNumCmaps(); ++i) {
933 cmapPlatform = ff->getCmapPlatform(i);
934 cmapEncoding = ff->getCmapEncoding(i);
935 if ((cmapPlatform == 3 && cmapEncoding == 1) ||
938 } else if (cmapPlatform == 1 && cmapEncoding == 0) {
940 } else if (cmapPlatform == 3 && cmapEncoding == 0) {
945 useMacRoman = gFalse;
948 if (usesMacRomanEnc && macRomanCmap >= 0) {
951 } else if (unicodeCmap >= 0) {
954 } else if ((flags & fontSymbolic) && msSymbolCmap >= 0) {
956 } else if (macRomanCmap >= 0) {
961 if (macRomanCmap >= 0) {
963 } else if (msSymbolCmap >= 0) {
968 // reverse map the char names through MacRomanEncoding, then map the
969 // char codes through the cmap
971 for (i = 0; i < 256; ++i) {
972 if ((charName = enc[i])) {
973 if ((code = globalParams->getMacRomanCharCode(charName))) {
974 map[i] = ff->mapCodeToGID(cmap, code);
979 // map Unicode through the cmap
980 } else if (useUnicode) {
981 for (i = 0; i < 256; ++i) {
982 if ((n = ctu->mapToUnicode((CharCode)i, &u, 1))) {
983 map[i] = ff->mapCodeToGID(cmap, u);
987 // map the char codes through the cmap, possibly with an offset of
990 for (i = 0; i < 256; ++i) {
991 if (!(map[i] = ff->mapCodeToGID(cmap, i))) {
992 map[i] = ff->mapCodeToGID(cmap, 0xf000 + i);
997 // try the TrueType 'post' table to handle any unmapped characters
998 for (i = 0; i < 256; ++i) {
999 if (!map[i] && (charName = enc[i])) {
1000 map[i] = (Gushort)(int)ff->mapNameToGID(charName);
1007 Dict *Gfx8BitFont::getCharProcs() {
1008 return charProcs.isDict() ? charProcs.getDict() : (Dict *)NULL;
1011 Object *Gfx8BitFont::getCharProc(int code, Object *proc) {
1012 if (enc[code] && charProcs.isDict()) {
1013 charProcs.dictLookup(enc[code], proc);
1020 Dict *Gfx8BitFont::getResources() {
1021 return resources.isDict() ? resources.getDict() : (Dict *)NULL;
1024 //------------------------------------------------------------------------
1026 //------------------------------------------------------------------------
1028 static int CDECL cmpWidthExcep(const void *w1, const void *w2) {
1029 return ((GfxFontCIDWidthExcep *)w1)->first -
1030 ((GfxFontCIDWidthExcep *)w2)->first;
1033 static int CDECL cmpWidthExcepV(const void *w1, const void *w2) {
1034 return ((GfxFontCIDWidthExcepV *)w1)->first -
1035 ((GfxFontCIDWidthExcepV *)w2)->first;
1038 GfxCIDFont::GfxCIDFont(XRef *xref, char *tagA, Ref idA, GString *nameA,
1040 GfxFont(tagA, idA, nameA)
1043 GString *collection, *cMapName;
1044 Object desFontDictObj;
1045 Object obj1, obj2, obj3, obj4, obj5, obj6;
1047 int excepsSize, i, j, k;
1051 fontBBox[0] = fontBBox[1] = fontBBox[2] = fontBBox[3] = 0;
1054 widths.defWidth = 1.0;
1055 widths.defHeight = -1.0;
1056 widths.defVY = 0.880;
1057 widths.exceps = NULL;
1059 widths.excepsV = NULL;
1060 widths.nExcepsV = 0;
1064 // get the descendant font
1065 if (!fontDict->lookup("DescendantFonts", &obj1)->isArray()) {
1066 error(-1, "Missing DescendantFonts entry in Type 0 font");
1070 if (!obj1.arrayGet(0, &desFontDictObj)->isDict()) {
1071 error(-1, "Bad descendant font in Type 0 font");
1075 desFontDict = desFontDictObj.getDict();
1078 if (!desFontDict->lookup("Subtype", &obj1)) {
1079 error(-1, "Missing Subtype entry in Type 0 descendant font");
1082 if (obj1.isName("CIDFontType0")) {
1083 type = fontCIDType0;
1084 } else if (obj1.isName("CIDFontType2")) {
1085 type = fontCIDType2;
1087 error(-1, "Unknown Type 0 descendant font type '%s'",
1088 obj1.isName() ? obj1.getName() : "???");
1093 // get info from font descriptor
1094 readFontDescriptor(xref, desFontDict);
1096 // look for an external font file
1099 //----- encoding info -----
1102 if (!desFontDict->lookup("CIDSystemInfo", &obj1)->isDict()) {
1103 error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
1106 obj1.dictLookup("Registry", &obj2);
1107 obj1.dictLookup("Ordering", &obj3);
1108 if (!obj2.isString() || !obj3.isString()) {
1109 error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
1112 collection = obj2.getString()->copy()->append('-')->append(obj3.getString());
1117 // look for a ToUnicode CMap
1118 if (!(ctu = readToUnicodeCMap(fontDict, 16, NULL))) {
1120 // the "Adobe-Identity" and "Adobe-UCS" collections don't have
1121 // cidToUnicode files
1122 if (collection->cmp("Adobe-Identity") &&
1123 collection->cmp("Adobe-UCS")) {
1125 // look for a user-supplied .cidToUnicode file
1126 if (!(ctu = globalParams->getCIDToUnicode(collection))) {
1127 error(-1, "Unknown character collection '%s'",
1128 collection->getCString());
1135 // encoding (i.e., CMap)
1136 //~ need to handle a CMap stream here
1137 //~ also need to deal with the UseCMap entry in the stream dict
1138 if (!fontDict->lookup("Encoding", &obj1)->isName()) {
1139 error(-1, "Missing or invalid Encoding entry in Type 0 font");
1143 cMapName = new GString(obj1.getName());
1145 if (!(cMap = globalParams->getCMap(collection, cMapName))) {
1146 error(-1, "Unknown CMap '%s' for character collection '%s'",
1147 cMapName->getCString(), collection->getCString());
1155 // CIDToGIDMap (for embedded TrueType fonts)
1156 if (type == fontCIDType2) {
1157 desFontDict->lookup("CIDToGIDMap", &obj1);
1158 if (obj1.isStream()) {
1161 cidToGID = (Gushort *)gmalloc(i * sizeof(Gushort));
1163 while ((c1 = obj1.streamGetChar()) != EOF &&
1164 (c2 = obj1.streamGetChar()) != EOF) {
1165 if (cidToGIDLen == i) {
1167 cidToGID = (Gushort *)grealloc(cidToGID, i * sizeof(Gushort));
1169 cidToGID[cidToGIDLen++] = (Gushort)((c1 << 8) + c2);
1171 } else if (!obj1.isName("Identity") && !obj1.isNull()) {
1172 error(-1, "Invalid CIDToGIDMap entry in CID font");
1177 //----- character metrics -----
1179 // default char width
1180 if (desFontDict->lookup("DW", &obj1)->isInt()) {
1181 widths.defWidth = obj1.getInt() * 0.001;
1185 // char width exceptions
1186 if (desFontDict->lookup("W", &obj1)->isArray()) {
1189 while (i + 1 < obj1.arrayGetLength()) {
1190 obj1.arrayGet(i, &obj2);
1191 obj1.arrayGet(i + 1, &obj3);
1192 if (obj2.isInt() && obj3.isInt() && i + 2 < obj1.arrayGetLength()) {
1193 if (obj1.arrayGet(i + 2, &obj4)->isNum()) {
1194 if (widths.nExceps == excepsSize) {
1196 widths.exceps = (GfxFontCIDWidthExcep *)
1197 grealloc(widths.exceps,
1198 excepsSize * sizeof(GfxFontCIDWidthExcep));
1200 widths.exceps[widths.nExceps].first = obj2.getInt();
1201 widths.exceps[widths.nExceps].last = obj3.getInt();
1202 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1205 error(-1, "Bad widths array in Type 0 font");
1209 } else if (obj2.isInt() && obj3.isArray()) {
1210 if (widths.nExceps + obj3.arrayGetLength() > excepsSize) {
1211 excepsSize = (widths.nExceps + obj3.arrayGetLength() + 15) & ~15;
1212 widths.exceps = (GfxFontCIDWidthExcep *)
1213 grealloc(widths.exceps,
1214 excepsSize * sizeof(GfxFontCIDWidthExcep));
1217 for (k = 0; k < obj3.arrayGetLength(); ++k) {
1218 if (obj3.arrayGet(k, &obj4)->isNum()) {
1219 widths.exceps[widths.nExceps].first = j;
1220 widths.exceps[widths.nExceps].last = j;
1221 widths.exceps[widths.nExceps].width = obj4.getNum() * 0.001;
1225 error(-1, "Bad widths array in Type 0 font");
1231 error(-1, "Bad widths array in Type 0 font");
1237 qsort(widths.exceps, widths.nExceps, sizeof(GfxFontCIDWidthExcep),
1242 // default metrics for vertical font
1243 if (desFontDict->lookup("DW2", &obj1)->isArray() &&
1244 obj1.arrayGetLength() == 2) {
1245 if (obj1.arrayGet(0, &obj2)->isNum()) {
1246 widths.defVY = obj2.getNum() * 0.001;
1249 if (obj1.arrayGet(1, &obj2)->isNum()) {
1250 widths.defHeight = obj2.getNum() * 0.001;
1256 // char metric exceptions for vertical font
1257 if (desFontDict->lookup("W2", &obj1)->isArray()) {
1260 while (i + 1 < obj1.arrayGetLength()) {
1261 obj1.arrayGet(i, &obj2);
1262 obj1.arrayGet(i+ 1, &obj3);
1263 if (obj2.isInt() && obj3.isInt() && i + 4 < obj1.arrayGetLength()) {
1264 if (obj1.arrayGet(i + 2, &obj4)->isNum() &&
1265 obj1.arrayGet(i + 3, &obj5)->isNum() &&
1266 obj1.arrayGet(i + 4, &obj6)->isNum()) {
1267 if (widths.nExcepsV == excepsSize) {
1269 widths.excepsV = (GfxFontCIDWidthExcepV *)
1270 grealloc(widths.excepsV,
1271 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1273 widths.excepsV[widths.nExcepsV].first = obj2.getInt();
1274 widths.excepsV[widths.nExcepsV].last = obj3.getInt();
1275 widths.excepsV[widths.nExcepsV].height = obj4.getNum() * 0.001;
1276 widths.excepsV[widths.nExcepsV].vx = obj5.getNum() * 0.001;
1277 widths.excepsV[widths.nExcepsV].vy = obj6.getNum() * 0.001;
1280 error(-1, "Bad widths (W2) array in Type 0 font");
1286 } else if (obj2.isInt() && obj3.isArray()) {
1287 if (widths.nExcepsV + obj3.arrayGetLength() / 3 > excepsSize) {
1289 (widths.nExcepsV + obj3.arrayGetLength() / 3 + 15) & ~15;
1290 widths.excepsV = (GfxFontCIDWidthExcepV *)
1291 grealloc(widths.excepsV,
1292 excepsSize * sizeof(GfxFontCIDWidthExcepV));
1295 for (k = 0; k < obj3.arrayGetLength(); k += 3) {
1296 if (obj3.arrayGet(k, &obj4)->isNum() &&
1297 obj3.arrayGet(k+1, &obj5)->isNum() &&
1298 obj3.arrayGet(k+2, &obj6)->isNum()) {
1299 widths.excepsV[widths.nExceps].first = j;
1300 widths.excepsV[widths.nExceps].last = j;
1301 widths.excepsV[widths.nExceps].height = obj4.getNum() * 0.001;
1302 widths.excepsV[widths.nExceps].vx = obj5.getNum() * 0.001;
1303 widths.excepsV[widths.nExceps].vy = obj6.getNum() * 0.001;
1307 error(-1, "Bad widths (W2) array in Type 0 font");
1315 error(-1, "Bad widths (W2) array in Type 0 font");
1321 qsort(widths.excepsV, widths.nExcepsV, sizeof(GfxFontCIDWidthExcepV),
1326 desFontDictObj.free();
1336 desFontDictObj.free();
1340 GfxCIDFont::~GfxCIDFont() {
1347 gfree(widths.exceps);
1348 gfree(widths.excepsV);
1354 int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
1355 Unicode *u, int uSize, int *uLen,
1356 double *dx, double *dy, double *ox, double *oy) {
1358 double w, h, vx, vy;
1368 *code = (CharCode)(cid = cMap->getCID(s, len, &n));
1370 *uLen = ctu->mapToUnicode(cid, u, uSize);
1376 if (cMap->getWMode() == 0) {
1377 w = widths.defWidth;
1379 if (widths.nExceps > 0 && cid >= widths.exceps[0].first) {
1382 // invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1385 if (widths.exceps[m].first <= cid) {
1391 if (cid <= widths.exceps[a].last) {
1392 w = widths.exceps[a].width;
1399 h = widths.defHeight;
1400 vx = widths.defWidth / 2;
1402 if (widths.nExcepsV > 0 && cid >= widths.excepsV[0].first) {
1404 b = widths.nExcepsV;
1405 // invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1408 if (widths.excepsV[m].last <= cid) {
1414 if (cid <= widths.excepsV[a].last) {
1415 h = widths.excepsV[a].height;
1416 vx = widths.excepsV[a].vx;
1417 vy = widths.excepsV[a].vy;
1430 int GfxCIDFont::getWMode() {
1431 return cMap ? cMap->getWMode() : 0;
1434 CharCodeToUnicode *GfxCIDFont::getToUnicode() {
1441 GString *GfxCIDFont::getCollection() {
1442 return cMap ? cMap->getCollection() : (GString *)NULL;
1445 //------------------------------------------------------------------------
1447 //------------------------------------------------------------------------
1449 GfxFontDict::GfxFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict) {
1454 numFonts = fontDict->getLength();
1455 fonts = (GfxFont **)gmalloc(numFonts * sizeof(GfxFont *));
1456 for (i = 0; i < numFonts; ++i) {
1457 fontDict->getValNF(i, &obj1);
1458 obj1.fetch(xref, &obj2);
1459 if (obj2.isDict()) {
1463 // no indirect reference for this font, so invent a unique one
1464 // (legal generation numbers are five digits, so any 6-digit
1465 // number would be safe)
1468 r.gen = 100000 + fontDictRef->num;
1473 fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i),
1475 if (fonts[i] && !fonts[i]->isOk()) {
1480 error(-1, "font resource is not a dictionary");
1488 GfxFontDict::~GfxFontDict() {
1491 for (i = 0; i < numFonts; ++i) {
1499 GfxFont *GfxFontDict::lookup(char *tag) {
1502 for (i = 0; i < numFonts; ++i) {
1503 if (fonts[i] && fonts[i]->matches(tag)) {