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,BoldItalic", "Courier-BoldOblique" },
59 { "Courier,Italic", "Courier-Oblique" },
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 embFontID
= obj2
.getRef();
195 if (type
!= fontType1
) {
196 error(-1, "Mismatch between font type and embedded font file");
201 if (embFontID
.num
== -1 &&
202 obj1
.dictLookupNF("FontFile2", &obj2
)->isRef()) {
203 embFontID
= obj2
.getRef();
204 if (type
!= fontTrueType
&& type
!= fontCIDType2
) {
205 error(-1, "Mismatch between font type and embedded font file");
206 type
= type
== fontCIDType0
? fontCIDType2
: fontTrueType
;
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 embFontID
= obj2
.getRef();
216 if (type
!= fontType1
) {
217 error(-1, "Mismatch between font type and embedded font file");
220 } else if (obj4
.isName("Type1C")) {
221 embFontID
= obj2
.getRef();
222 if (type
!= fontType1
&& type
!= fontType1C
) {
223 error(-1, "Mismatch between font type and embedded font file");
226 } else if (obj4
.isName("TrueType")) {
227 embFontID
= obj2
.getRef();
228 if (type
!= fontTrueType
) {
229 error(-1, "Mismatch between font type and embedded font file");
232 } else if (obj4
.isName("CIDFontType0C")) {
233 embFontID
= obj2
.getRef();
234 if (type
!= fontCIDType0
) {
235 error(-1, "Mismatch between font type and embedded font file");
237 type
= fontCIDType0C
;
239 error(-1, "Unknown embedded font type '%s'",
240 obj4
.isName() ? obj4
.getName() : "???");
248 // look for MissingWidth
249 obj1
.dictLookup("MissingWidth", &obj2
);
251 missingWidth
= obj2
.getNum();
255 // get Ascent and Descent
256 obj1
.dictLookup("Ascent", &obj2
);
258 t
= 0.001 * obj2
.getNum();
259 // some broken font descriptors set ascent and descent to 0
265 obj1
.dictLookup("Descent", &obj2
);
267 t
= 0.001 * obj2
.getNum();
268 // some broken font descriptors set ascent and descent to 0
272 // some broken font descriptors specify a positive descent
280 if (obj1
.dictLookup("FontBBox", &obj2
)->isArray()) {
281 for (i
= 0; i
< 4 && i
< obj2
.arrayGetLength(); ++i
) {
282 if (obj2
.arrayGet(i
, &obj3
)->isNum()) {
283 fontBBox
[i
] = 0.001 * obj3
.getNum();
294 CharCodeToUnicode
*GfxFont::readToUnicodeCMap(Dict
*fontDict
, int nBits
,
295 CharCodeToUnicode
*ctu
) {
300 if (!fontDict
->lookup("ToUnicode", &obj1
)->isStream()) {
306 while ((c
= obj1
.streamGetChar()) != EOF
) {
312 ctu
->mergeCMap(buf
, nBits
);
314 ctu
= CharCodeToUnicode::parseCMap(buf
, nBits
);
320 void GfxFont::findExtFontFile() {
321 static char *type1Exts
[] = { ".pfa", ".pfb", ".ps", "", NULL
};
322 static char *ttExts
[] = { ".ttf", NULL
};
325 if (type
== fontType1
) {
326 extFontFile
= globalParams
->findFontFile(name
, type1Exts
);
327 } else if (type
== fontTrueType
) {
328 extFontFile
= globalParams
->findFontFile(name
, ttExts
);
333 char *GfxFont::readExtFontFile(int *len
) {
337 if (!(f
= fopen(extFontFile
->getCString(), "rb"))) {
338 error(-1, "External font file '%s' vanished", extFontFile
->getCString());
341 fseek(f
, 0, SEEK_END
);
342 *len
= (int)ftell(f
);
343 fseek(f
, 0, SEEK_SET
);
344 buf
= (char *)gmalloc(*len
);
345 if ((int)fread(buf
, 1, *len
, f
) != *len
) {
346 error(-1, "Error reading external font file '%s'",
347 extFontFile
->getCString());
353 char *GfxFont::readEmbFontFile(XRef
*xref
, int *len
) {
360 obj1
.initRef(embFontID
.num
, embFontID
.gen
);
361 obj1
.fetch(xref
, &obj2
);
362 if (!obj2
.isStream()) {
363 error(-1, "Embedded font file is not a stream");
369 str
= obj2
.getStream();
374 while ((c
= str
->getChar()) != EOF
) {
377 buf
= (char *)grealloc(buf
, size
);
390 //------------------------------------------------------------------------
392 //------------------------------------------------------------------------
394 Gfx8BitFont::Gfx8BitFont(XRef
*xref
, char *tagA
, Ref idA
, GString
*nameA
,
395 GfxFontType typeA
, Dict
*fontDict
):
396 GfxFont(tagA
, idA
, nameA
)
399 BuiltinFont
*builtinFont
;
401 GBool baseEncFromFontFile
;
409 Unicode toUnicode
[256];
410 CharCodeToUnicode
*utu
, *ctu2
;
413 int firstChar
, lastChar
;
415 Object obj1
, obj2
, obj3
;
421 // do font name substitution for various aliases of the Base 14 font
424 name2
= name
->copy();
426 while (i
< name2
->getLength()) {
427 if (name2
->getChar(i
) == ' ') {
434 b
= sizeof(stdFontMap
) / sizeof(StdFontMapEntry
);
435 // invariant: stdFontMap[a].altName <= name2 < stdFontMap[b].altName
438 if (name2
->cmp(stdFontMap
[m
].altName
) >= 0) {
444 if (!name2
->cmp(stdFontMap
[a
].altName
)) {
445 name
= new GString(stdFontMap
[a
].properName
);
450 // is it a built-in font?
453 for (i
= 0; i
< nBuiltinFonts
; ++i
) {
454 if (!name
->cmp(builtinFonts
[i
].name
)) {
455 builtinFont
= &builtinFonts
[i
];
461 // default ascent/descent values
463 ascent
= 0.001 * builtinFont
->ascent
;
464 descent
= 0.001 * builtinFont
->descent
;
465 fontBBox
[0] = 0.001 * builtinFont
->bbox
[0];
466 fontBBox
[1] = 0.001 * builtinFont
->bbox
[1];
467 fontBBox
[2] = 0.001 * builtinFont
->bbox
[2];
468 fontBBox
[3] = 0.001 * builtinFont
->bbox
[3];
472 fontBBox
[0] = fontBBox
[1] = fontBBox
[2] = fontBBox
[3] = 0;
475 // get info from font descriptor
476 readFontDescriptor(xref
, fontDict
);
478 // for non-embedded fonts, don't trust the ascent/descent/bbox
479 // values from the font descriptor
480 if (builtinFont
&& embFontID
.num
< 0) {
481 ascent
= 0.001 * builtinFont
->ascent
;
482 descent
= 0.001 * builtinFont
->descent
;
483 fontBBox
[0] = 0.001 * builtinFont
->bbox
[0];
484 fontBBox
[1] = 0.001 * builtinFont
->bbox
[1];
485 fontBBox
[2] = 0.001 * builtinFont
->bbox
[2];
486 fontBBox
[3] = 0.001 * builtinFont
->bbox
[3];
489 // look for an external font file
493 fontMat
[0] = fontMat
[3] = 1;
494 fontMat
[1] = fontMat
[2] = fontMat
[4] = fontMat
[5] = 0;
495 if (fontDict
->lookup("FontMatrix", &obj1
)->isArray()) {
496 for (i
= 0; i
< 6 && i
< obj1
.arrayGetLength(); ++i
) {
497 if (obj1
.arrayGet(i
, &obj2
)->isNum()) {
498 fontMat
[i
] = obj2
.getNum();
505 // get Type 3 bounding box, font definition, and resources
506 if (type
== fontType3
) {
507 if (fontDict
->lookup("FontBBox", &obj1
)->isArray()) {
508 for (i
= 0; i
< 4 && i
< obj1
.arrayGetLength(); ++i
) {
509 if (obj1
.arrayGet(i
, &obj2
)->isNum()) {
510 fontBBox
[i
] = obj2
.getNum();
516 if (!fontDict
->lookup("CharProcs", &charProcs
)->isDict()) {
517 error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
520 if (!fontDict
->lookup("Resources", &resources
)->isDict()) {
525 //----- build the font encoding -----
527 // Encodings start with a base encoding, which can come from
528 // (in order of priority):
529 // 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
530 // - MacRoman / MacExpert / WinAnsi / Standard
531 // 2. embedded or external font file
533 // - builtin --> builtin encoding
534 // - TrueType --> WinAnsiEncoding
535 // - others --> StandardEncoding
536 // and then add a list of differences (if any) from
537 // FontDict.Encoding.Differences.
539 // check FontDict for base encoding
540 hasEncoding
= gFalse
;
541 usesMacRomanEnc
= gFalse
;
543 baseEncFromFontFile
= gFalse
;
544 fontDict
->lookup("Encoding", &obj1
);
546 obj1
.dictLookup("BaseEncoding", &obj2
);
547 if (obj2
.isName("MacRomanEncoding")) {
549 usesMacRomanEnc
= gTrue
;
550 baseEnc
= macRomanEncoding
;
551 } else if (obj2
.isName("MacExpertEncoding")) {
553 baseEnc
= macExpertEncoding
;
554 } else if (obj2
.isName("WinAnsiEncoding")) {
556 baseEnc
= winAnsiEncoding
;
559 } else if (obj1
.isName("MacRomanEncoding")) {
561 usesMacRomanEnc
= gTrue
;
562 baseEnc
= macRomanEncoding
;
563 } else if (obj1
.isName("MacExpertEncoding")) {
565 baseEnc
= macExpertEncoding
;
566 } else if (obj1
.isName("WinAnsiEncoding")) {
568 baseEnc
= winAnsiEncoding
;
571 // check embedded or external font file for base encoding
572 // (only for Type 1 fonts - trying to get an encoding out of a
573 // TrueType font is a losing proposition)
577 if (type
== fontType1
&& (extFontFile
|| embFontID
.num
>= 0)) {
579 ffT1
= FoFiType1::load(extFontFile
->getCString());
581 buf
= readEmbFontFile(xref
, &len
);
582 ffT1
= FoFiType1::make(buf
, len
);
585 if (ffT1
->getName()) {
589 embFontName
= new GString(ffT1
->getName());
592 baseEnc
= ffT1
->getEncoding();
593 baseEncFromFontFile
= gTrue
;
596 } else if (type
== fontType1C
&& (extFontFile
|| embFontID
.num
>= 0)) {
598 ffT1C
= FoFiType1C::load(extFontFile
->getCString());
600 buf
= readEmbFontFile(xref
, &len
);
601 ffT1C
= FoFiType1C::make(buf
, len
);
604 if (ffT1C
->getName()) {
608 embFontName
= new GString(ffT1C
->getName());
611 baseEnc
= ffT1C
->getEncoding();
612 baseEncFromFontFile
= gTrue
;
620 // get default base encoding
622 if (builtinFont
&& embFontID
.num
< 0) {
623 baseEnc
= builtinFont
->defaultBaseEnc
;
625 } else if (type
== fontTrueType
) {
626 baseEnc
= winAnsiEncoding
;
628 baseEnc
= standardEncoding
;
632 // copy the base encoding
633 for (i
= 0; i
< 256; ++i
) {
635 if ((encFree
[i
] = baseEncFromFontFile
) && enc
[i
]) {
636 enc
[i
] = copyString(baseEnc
[i
]);
640 // some Type 1C font files have empty encodings, which can break the
641 // T1C->T1 conversion (since the 'seac' operator depends on having
642 // the accents in the encoding), so we fill in any gaps from
644 if (type
== fontType1C
&& (extFontFile
|| embFontID
.num
>= 0) &&
645 baseEncFromFontFile
) {
646 for (i
= 0; i
< 256; ++i
) {
647 if (!enc
[i
] && standardEncoding
[i
]) {
648 enc
[i
] = standardEncoding
[i
];
654 // merge differences into encoding
656 obj1
.dictLookup("Differences", &obj2
);
657 if (obj2
.isArray()) {
660 for (i
= 0; i
< obj2
.arrayGetLength(); ++i
) {
661 obj2
.arrayGet(i
, &obj3
);
663 code
= obj3
.getInt();
664 } else if (obj3
.isName()) {
665 if (code
>= 0 && code
< 256) {
669 enc
[code
] = copyString(obj3
.getName());
670 encFree
[code
] = gTrue
;
674 error(-1, "Wrong type in font encoding resource differences (%s)",
690 //----- build the mapping to Unicode -----
692 // pass 1: use the name-to-Unicode mapping table
693 missing
= hex
= gFalse
;
694 for (code
= 0; code
< 256; ++code
) {
695 if ((charName
= enc
[code
])) {
696 if (!(toUnicode
[code
] = globalParams
->mapNameToUnicode(charName
)) &&
697 strcmp(charName
, ".notdef")) {
698 // if it wasn't in the name-to-Unicode table, check for a
699 // name that looks like 'Axx' or 'xx', where 'A' is any letter
700 // and 'xx' is two hex digits
701 if ((strlen(charName
) == 3 &&
702 isalpha(charName
[0]) &&
703 isxdigit(charName
[1]) && isxdigit(charName
[2]) &&
704 ((charName
[1] >= 'a' && charName
[1] <= 'f') ||
705 (charName
[1] >= 'A' && charName
[1] <= 'F') ||
706 (charName
[2] >= 'a' && charName
[2] <= 'f') ||
707 (charName
[2] >= 'A' && charName
[2] <= 'F'))) ||
708 (strlen(charName
) == 2 &&
709 isxdigit(charName
[0]) && isxdigit(charName
[1]) &&
710 ((charName
[0] >= 'a' && charName
[0] <= 'f') ||
711 (charName
[0] >= 'A' && charName
[0] <= 'F') ||
712 (charName
[1] >= 'a' && charName
[1] <= 'f') ||
713 (charName
[1] >= 'A' && charName
[1] <= 'F')))) {
723 // pass 2: try to fill in the missing chars, looking for names of
724 // the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
725 // are any letters, 'xx' is two hex digits, and 'nn' is 2-4
727 if (missing
&& globalParams
->getMapNumericCharNames()) {
728 for (code
= 0; code
< 256; ++code
) {
729 if ((charName
= enc
[code
]) && !toUnicode
[code
] &&
730 strcmp(charName
, ".notdef")) {
731 n
= strlen(charName
);
733 if (hex
&& n
== 3 && isalpha(charName
[0]) &&
734 isxdigit(charName
[1]) && isxdigit(charName
[2])) {
735 sscanf(charName
+1, "%x", &code2
);
736 } else if (hex
&& n
== 2 &&
737 isxdigit(charName
[0]) && isxdigit(charName
[1])) {
738 sscanf(charName
, "%x", &code2
);
739 } else if (!hex
&& n
>= 2 && n
<= 4 &&
740 isdigit(charName
[0]) && isdigit(charName
[1])) {
741 code2
= atoi(charName
);
742 } else if (n
>= 3 && n
<= 5 &&
743 isdigit(charName
[1]) && isdigit(charName
[2])) {
744 code2
= atoi(charName
+1);
745 } else if (n
>= 4 && n
<= 6 &&
746 isdigit(charName
[2]) && isdigit(charName
[3])) {
747 code2
= atoi(charName
+2);
749 if (code2
>= 0 && code2
<= 0xff) {
750 toUnicode
[code
] = (Unicode
)code2
;
756 // construct the char code -> Unicode mapping object
757 ctu
= CharCodeToUnicode::make8BitToUnicode(toUnicode
);
759 // merge in a ToUnicode CMap, if there is one -- this overwrites
760 // existing entries in ctu, i.e., the ToUnicode CMap takes
761 // precedence, but the other encoding info is allowed to fill in any
763 readToUnicodeCMap(fontDict
, 8, ctu
);
765 // look for a Unicode-to-Unicode mapping
766 if (name
&& (utu
= globalParams
->getUnicodeToUnicode(name
))) {
767 for (i
= 0; i
< 256; ++i
) {
770 ctu2
= CharCodeToUnicode::make8BitToUnicode(toUnicode
);
771 for (i
= 0; i
< 256; ++i
) {
772 n
= ctu
->mapToUnicode((CharCode
)i
, uBuf
, 8);
774 n
= utu
->mapToUnicode((CharCode
)uBuf
[0], uBuf
, 8);
776 ctu2
->setMapping((CharCode
)i
, uBuf
, n
);
785 //----- get the character widths -----
787 // initialize all widths
788 for (code
= 0; code
< 256; ++code
) {
789 widths
[code
] = missingWidth
* 0.001;
792 // use widths from font dict, if present
793 fontDict
->lookup("FirstChar", &obj1
);
794 firstChar
= obj1
.isInt() ? obj1
.getInt() : 0;
796 if (firstChar
< 0 || firstChar
> 255) {
799 fontDict
->lookup("LastChar", &obj1
);
800 lastChar
= obj1
.isInt() ? obj1
.getInt() : 255;
802 if (lastChar
< 0 || lastChar
> 255) {
805 mul
= (type
== fontType3
) ? fontMat
[0] : 0.001;
806 fontDict
->lookup("Widths", &obj1
);
807 if (obj1
.isArray()) {
808 flags
|= fontFixedWidth
;
809 if (obj1
.arrayGetLength() < lastChar
- firstChar
+ 1) {
810 lastChar
= firstChar
+ obj1
.arrayGetLength() - 1;
812 for (code
= firstChar
; code
<= lastChar
; ++code
) {
813 obj1
.arrayGet(code
- firstChar
, &obj2
);
815 widths
[code
] = obj2
.getNum() * mul
;
816 if (widths
[code
] != widths
[firstChar
]) {
817 flags
&= ~fontFixedWidth
;
823 // use widths from built-in font
824 } else if (builtinFont
) {
825 // this is a kludge for broken PDF files that encode char 32
827 if (builtinFont
->widths
->getWidth("space", &w
)) {
828 widths
[32] = 0.001 * w
;
830 for (code
= 0; code
< 256; ++code
) {
831 if (enc
[code
] && builtinFont
->widths
->getWidth(enc
[code
], &w
)) {
832 widths
[code
] = 0.001 * w
;
836 // couldn't find widths -- use defaults
838 // this is technically an error -- the Widths entry is required
839 // for all but the Base-14 fonts -- but certain PDF generators
840 // apparently don't include widths for Arial and TimesNewRoman
841 if (isFixedWidth()) {
843 } else if (isSerif()) {
854 builtinFont
= builtinFontSubst
[i
];
855 // this is a kludge for broken PDF files that encode char 32
857 if (builtinFont
->widths
->getWidth("space", &w
)) {
858 widths
[32] = 0.001 * w
;
860 for (code
= 0; code
< 256; ++code
) {
861 if (enc
[code
] && builtinFont
->widths
->getWidth(enc
[code
], &w
)) {
862 widths
[code
] = 0.001 * w
;
871 Gfx8BitFont::~Gfx8BitFont() {
874 for (i
= 0; i
< 256; ++i
) {
875 if (encFree
[i
] && enc
[i
]) {
880 if (charProcs
.isDict()) {
883 if (resources
.isDict()) {
888 int Gfx8BitFont::getNextChar(char *s
, int len
, CharCode
*code
,
889 Unicode
*u
, int uSize
, int *uLen
,
890 double *dx
, double *dy
, double *ox
, double *oy
) {
893 *code
= c
= (CharCode
)(*s
& 0xff);
894 *uLen
= ctu
->mapToUnicode(c
, u
, uSize
);
900 CharCodeToUnicode
*Gfx8BitFont::getToUnicode() {
905 Gushort
*Gfx8BitFont::getCodeToGIDMap(FoFiTrueType
*ff
) {
907 int cmapPlatform
, cmapEncoding
;
908 int unicodeCmap
, macRomanCmap
, msSymbolCmap
, cmap
;
909 GBool useMacRoman
, useUnicode
;
914 map
= (Gushort
*)gmallocn(256, sizeof(Gushort
));
915 for (i
= 0; i
< 256; ++i
) {
919 // To match up with the Adobe-defined behaviour, we choose a cmap
921 // 1. If the PDF font has an encoding:
922 // 1a. If the PDF font specified MacRomanEncoding and the
923 // TrueType font has a Macintosh Roman cmap, use it, and
924 // reverse map the char names through MacRomanEncoding to
926 // 1b. If the TrueType font has a Microsoft Unicode cmap or a
927 // non-Microsoft Unicode cmap, use it, and use the Unicode
928 // indexes, not the char codes.
929 // 1c. If the PDF font is symbolic and the TrueType font has a
930 // Microsoft Symbol cmap, use it, and use char codes
931 // directly (possibly with an offset of 0xf000).
932 // 1d. If the TrueType font has a Macintosh Roman cmap, use it,
934 // 2. If the PDF font does not have an encoding or the PDF font is
936 // 2a. If the TrueType font has a Macintosh Roman cmap, use it,
937 // and use char codes directly (possibly with an offset of
939 // 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
940 // and use char codes directly (possible with an offset of
942 // 3. If none of these rules apply, use the first cmap and hope for
943 // the best (this shouldn't happen).
944 unicodeCmap
= macRomanCmap
= msSymbolCmap
= -1;
945 for (i
= 0; i
< ff
->getNumCmaps(); ++i
) {
946 cmapPlatform
= ff
->getCmapPlatform(i
);
947 cmapEncoding
= ff
->getCmapEncoding(i
);
948 if ((cmapPlatform
== 3 && cmapEncoding
== 1) ||
951 } else if (cmapPlatform
== 1 && cmapEncoding
== 0) {
953 } else if (cmapPlatform
== 3 && cmapEncoding
== 0) {
958 useMacRoman
= gFalse
;
961 if (usesMacRomanEnc
&& macRomanCmap
>= 0) {
964 } else if (unicodeCmap
>= 0) {
967 } else if ((flags
& fontSymbolic
) && msSymbolCmap
>= 0) {
969 } else if ((flags
& fontSymbolic
) && macRomanCmap
>= 0) {
971 } else if (macRomanCmap
>= 0) {
976 if (macRomanCmap
>= 0) {
978 } else if (msSymbolCmap
>= 0) {
983 // reverse map the char names through MacRomanEncoding, then map the
984 // char codes through the cmap
986 for (i
= 0; i
< 256; ++i
) {
987 if ((charName
= enc
[i
])) {
988 if ((code
= globalParams
->getMacRomanCharCode(charName
))) {
989 map
[i
] = ff
->mapCodeToGID(cmap
, code
);
994 // map Unicode through the cmap
995 } else if (useUnicode
) {
996 for (i
= 0; i
< 256; ++i
) {
997 if (((charName
= enc
[i
]) &&
998 (u
= globalParams
->mapNameToUnicode(charName
))) ||
999 (n
= ctu
->mapToUnicode((CharCode
)i
, &u
, 1))) {
1000 map
[i
] = ff
->mapCodeToGID(cmap
, u
);
1004 // map the char codes through the cmap, possibly with an offset of
1007 for (i
= 0; i
< 256; ++i
) {
1008 if (!(map
[i
] = ff
->mapCodeToGID(cmap
, i
))) {
1009 map
[i
] = ff
->mapCodeToGID(cmap
, 0xf000 + i
);
1014 // try the TrueType 'post' table to handle any unmapped characters
1015 for (i
= 0; i
< 256; ++i
) {
1016 if (!map
[i
] && (charName
= enc
[i
])) {
1017 map
[i
] = (Gushort
)(long)ff
->mapNameToGID(charName
);
1024 Dict
*Gfx8BitFont::getCharProcs() {
1025 return charProcs
.isDict() ? charProcs
.getDict() : (Dict
*)NULL
;
1028 Object
*Gfx8BitFont::getCharProc(int code
, Object
*proc
) {
1029 if (enc
[code
] && charProcs
.isDict()) {
1030 charProcs
.dictLookup(enc
[code
], proc
);
1037 Dict
*Gfx8BitFont::getResources() {
1038 return resources
.isDict() ? resources
.getDict() : (Dict
*)NULL
;
1041 //------------------------------------------------------------------------
1043 //------------------------------------------------------------------------
1045 static int CDECL
cmpWidthExcep(const void *w1
, const void *w2
) {
1046 return ((GfxFontCIDWidthExcep
*)w1
)->first
-
1047 ((GfxFontCIDWidthExcep
*)w2
)->first
;
1050 static int CDECL
cmpWidthExcepV(const void *w1
, const void *w2
) {
1051 return ((GfxFontCIDWidthExcepV
*)w1
)->first
-
1052 ((GfxFontCIDWidthExcepV
*)w2
)->first
;
1055 GfxCIDFont::GfxCIDFont(XRef
*xref
, char *tagA
, Ref idA
, GString
*nameA
,
1057 GfxFont(tagA
, idA
, nameA
)
1060 GString
*collection
, *cMapName
;
1061 Object desFontDictObj
;
1062 Object obj1
, obj2
, obj3
, obj4
, obj5
, obj6
;
1063 CharCodeToUnicode
*utu
;
1067 int excepsSize
, i
, j
, k
, n
;
1071 fontBBox
[0] = fontBBox
[1] = fontBBox
[2] = fontBBox
[3] = 0;
1074 widths
.defWidth
= 1.0;
1075 widths
.defHeight
= -1.0;
1076 widths
.defVY
= 0.880;
1077 widths
.exceps
= NULL
;
1079 widths
.excepsV
= NULL
;
1080 widths
.nExcepsV
= 0;
1084 // get the descendant font
1085 if (!fontDict
->lookup("DescendantFonts", &obj1
)->isArray()) {
1086 error(-1, "Missing DescendantFonts entry in Type 0 font");
1090 if (!obj1
.arrayGet(0, &desFontDictObj
)->isDict()) {
1091 error(-1, "Bad descendant font in Type 0 font");
1095 desFontDict
= desFontDictObj
.getDict();
1098 if (!desFontDict
->lookup("Subtype", &obj1
)) {
1099 error(-1, "Missing Subtype entry in Type 0 descendant font");
1102 if (obj1
.isName("CIDFontType0")) {
1103 type
= fontCIDType0
;
1104 } else if (obj1
.isName("CIDFontType2")) {
1105 type
= fontCIDType2
;
1107 error(-1, "Unknown Type 0 descendant font type '%s'",
1108 obj1
.isName() ? obj1
.getName() : "???");
1113 // get info from font descriptor
1114 readFontDescriptor(xref
, desFontDict
);
1116 // look for an external font file
1119 //----- encoding info -----
1122 if (!desFontDict
->lookup("CIDSystemInfo", &obj1
)->isDict()) {
1123 error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
1126 obj1
.dictLookup("Registry", &obj2
);
1127 obj1
.dictLookup("Ordering", &obj3
);
1128 if (!obj2
.isString() || !obj3
.isString()) {
1129 error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
1132 collection
= obj2
.getString()->copy()->append('-')->append(obj3
.getString());
1137 // look for a ToUnicode CMap
1138 if (!(ctu
= readToUnicodeCMap(fontDict
, 16, NULL
))) {
1140 // the "Adobe-Identity" and "Adobe-UCS" collections don't have
1141 // cidToUnicode files
1142 if (collection
->cmp("Adobe-Identity") &&
1143 collection
->cmp("Adobe-UCS")) {
1145 // look for a user-supplied .cidToUnicode file
1146 if (!(ctu
= globalParams
->getCIDToUnicode(collection
))) {
1147 error(-1, "Unknown character collection '%s'",
1148 collection
->getCString());
1149 // fall-through, assuming the Identity mapping -- this appears
1150 // to match Adobe's behavior
1155 // look for a Unicode-to-Unicode mapping
1156 if (name
&& (utu
= globalParams
->getUnicodeToUnicode(name
))) {
1158 for (c
= 0; c
< ctu
->getLength(); ++c
) {
1159 n
= ctu
->mapToUnicode(c
, uBuf
, 8);
1161 n
= utu
->mapToUnicode((CharCode
)uBuf
[0], uBuf
, 8);
1163 ctu
->setMapping(c
, uBuf
, n
);
1173 // encoding (i.e., CMap)
1174 //~ need to handle a CMap stream here
1175 //~ also need to deal with the UseCMap entry in the stream dict
1176 if (!fontDict
->lookup("Encoding", &obj1
)->isName()) {
1177 error(-1, "Missing or invalid Encoding entry in Type 0 font");
1181 cMapName
= new GString(obj1
.getName());
1183 if (!(cMap
= globalParams
->getCMap(collection
, cMapName
))) {
1184 error(-1, "Unknown CMap '%s' for character collection '%s'",
1185 cMapName
->getCString(), collection
->getCString());
1193 // CIDToGIDMap (for embedded TrueType fonts)
1194 if (type
== fontCIDType2
) {
1195 desFontDict
->lookup("CIDToGIDMap", &obj1
);
1196 if (obj1
.isStream()) {
1199 cidToGID
= (Gushort
*)gmallocn(i
, sizeof(Gushort
));
1201 while ((c1
= obj1
.streamGetChar()) != EOF
&&
1202 (c2
= obj1
.streamGetChar()) != EOF
) {
1203 if (cidToGIDLen
== i
) {
1205 cidToGID
= (Gushort
*)greallocn(cidToGID
, i
, sizeof(Gushort
));
1207 cidToGID
[cidToGIDLen
++] = (Gushort
)((c1
<< 8) + c2
);
1209 } else if (!obj1
.isName("Identity") && !obj1
.isNull()) {
1210 error(-1, "Invalid CIDToGIDMap entry in CID font");
1215 //----- character metrics -----
1217 // default char width
1218 if (desFontDict
->lookup("DW", &obj1
)->isInt()) {
1219 widths
.defWidth
= obj1
.getInt() * 0.001;
1223 // char width exceptions
1224 if (desFontDict
->lookup("W", &obj1
)->isArray()) {
1227 while (i
+ 1 < obj1
.arrayGetLength()) {
1228 obj1
.arrayGet(i
, &obj2
);
1229 obj1
.arrayGet(i
+ 1, &obj3
);
1230 if (obj2
.isInt() && obj3
.isInt() && i
+ 2 < obj1
.arrayGetLength()) {
1231 if (obj1
.arrayGet(i
+ 2, &obj4
)->isNum()) {
1232 if (widths
.nExceps
== excepsSize
) {
1234 widths
.exceps
= (GfxFontCIDWidthExcep
*)
1235 greallocn(widths
.exceps
,
1236 excepsSize
, sizeof(GfxFontCIDWidthExcep
));
1238 widths
.exceps
[widths
.nExceps
].first
= obj2
.getInt();
1239 widths
.exceps
[widths
.nExceps
].last
= obj3
.getInt();
1240 widths
.exceps
[widths
.nExceps
].width
= obj4
.getNum() * 0.001;
1243 error(-1, "Bad widths array in Type 0 font");
1247 } else if (obj2
.isInt() && obj3
.isArray()) {
1248 if (widths
.nExceps
+ obj3
.arrayGetLength() > excepsSize
) {
1249 excepsSize
= (widths
.nExceps
+ obj3
.arrayGetLength() + 15) & ~15;
1250 widths
.exceps
= (GfxFontCIDWidthExcep
*)
1251 greallocn(widths
.exceps
,
1252 excepsSize
, sizeof(GfxFontCIDWidthExcep
));
1255 for (k
= 0; k
< obj3
.arrayGetLength(); ++k
) {
1256 if (obj3
.arrayGet(k
, &obj4
)->isNum()) {
1257 widths
.exceps
[widths
.nExceps
].first
= j
;
1258 widths
.exceps
[widths
.nExceps
].last
= j
;
1259 widths
.exceps
[widths
.nExceps
].width
= obj4
.getNum() * 0.001;
1263 error(-1, "Bad widths array in Type 0 font");
1269 error(-1, "Bad widths array in Type 0 font");
1275 qsort(widths
.exceps
, widths
.nExceps
, sizeof(GfxFontCIDWidthExcep
),
1280 // default metrics for vertical font
1281 if (desFontDict
->lookup("DW2", &obj1
)->isArray() &&
1282 obj1
.arrayGetLength() == 2) {
1283 if (obj1
.arrayGet(0, &obj2
)->isNum()) {
1284 widths
.defVY
= obj2
.getNum() * 0.001;
1287 if (obj1
.arrayGet(1, &obj2
)->isNum()) {
1288 widths
.defHeight
= obj2
.getNum() * 0.001;
1294 // char metric exceptions for vertical font
1295 if (desFontDict
->lookup("W2", &obj1
)->isArray()) {
1298 while (i
+ 1 < obj1
.arrayGetLength()) {
1299 obj1
.arrayGet(i
, &obj2
);
1300 obj1
.arrayGet(i
+ 1, &obj3
);
1301 if (obj2
.isInt() && obj3
.isInt() && i
+ 4 < obj1
.arrayGetLength()) {
1302 if (obj1
.arrayGet(i
+ 2, &obj4
)->isNum() &&
1303 obj1
.arrayGet(i
+ 3, &obj5
)->isNum() &&
1304 obj1
.arrayGet(i
+ 4, &obj6
)->isNum()) {
1305 if (widths
.nExcepsV
== excepsSize
) {
1307 widths
.excepsV
= (GfxFontCIDWidthExcepV
*)
1308 greallocn(widths
.excepsV
,
1309 excepsSize
, sizeof(GfxFontCIDWidthExcepV
));
1311 widths
.excepsV
[widths
.nExcepsV
].first
= obj2
.getInt();
1312 widths
.excepsV
[widths
.nExcepsV
].last
= obj3
.getInt();
1313 widths
.excepsV
[widths
.nExcepsV
].height
= obj4
.getNum() * 0.001;
1314 widths
.excepsV
[widths
.nExcepsV
].vx
= obj5
.getNum() * 0.001;
1315 widths
.excepsV
[widths
.nExcepsV
].vy
= obj6
.getNum() * 0.001;
1318 error(-1, "Bad widths (W2) array in Type 0 font");
1324 } else if (obj2
.isInt() && obj3
.isArray()) {
1325 if (widths
.nExcepsV
+ obj3
.arrayGetLength() / 3 > excepsSize
) {
1327 (widths
.nExcepsV
+ obj3
.arrayGetLength() / 3 + 15) & ~15;
1328 widths
.excepsV
= (GfxFontCIDWidthExcepV
*)
1329 greallocn(widths
.excepsV
,
1330 excepsSize
, sizeof(GfxFontCIDWidthExcepV
));
1333 for (k
= 0; k
< obj3
.arrayGetLength(); k
+= 3) {
1334 if (obj3
.arrayGet(k
, &obj4
)->isNum() &&
1335 obj3
.arrayGet(k
+1, &obj5
)->isNum() &&
1336 obj3
.arrayGet(k
+2, &obj6
)->isNum()) {
1337 widths
.excepsV
[widths
.nExceps
].first
= j
;
1338 widths
.excepsV
[widths
.nExceps
].last
= j
;
1339 widths
.excepsV
[widths
.nExceps
].height
= obj4
.getNum() * 0.001;
1340 widths
.excepsV
[widths
.nExceps
].vx
= obj5
.getNum() * 0.001;
1341 widths
.excepsV
[widths
.nExceps
].vy
= obj6
.getNum() * 0.001;
1345 error(-1, "Bad widths (W2) array in Type 0 font");
1353 error(-1, "Bad widths (W2) array in Type 0 font");
1359 qsort(widths
.excepsV
, widths
.nExcepsV
, sizeof(GfxFontCIDWidthExcepV
),
1364 desFontDictObj
.free();
1374 desFontDictObj
.free();
1378 GfxCIDFont::~GfxCIDFont() {
1385 gfree(widths
.exceps
);
1386 gfree(widths
.excepsV
);
1392 int GfxCIDFont::getNextChar(char *s
, int len
, CharCode
*code
,
1393 Unicode
*u
, int uSize
, int *uLen
,
1394 double *dx
, double *dy
, double *ox
, double *oy
) {
1396 double w
, h
, vx
, vy
;
1406 *code
= (CharCode
)(cid
= cMap
->getCID(s
, len
, &n
));
1408 *uLen
= ctu
->mapToUnicode(cid
, u
, uSize
);
1414 if (cMap
->getWMode() == 0) {
1415 w
= widths
.defWidth
;
1417 if (widths
.nExceps
> 0 && cid
>= widths
.exceps
[0].first
) {
1420 // invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1423 if (widths
.exceps
[m
].first
<= cid
) {
1429 if (cid
<= widths
.exceps
[a
].last
) {
1430 w
= widths
.exceps
[a
].width
;
1437 h
= widths
.defHeight
;
1438 vx
= widths
.defWidth
/ 2;
1440 if (widths
.nExcepsV
> 0 && cid
>= widths
.excepsV
[0].first
) {
1442 b
= widths
.nExcepsV
;
1443 // invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1446 if (widths
.excepsV
[m
].last
<= cid
) {
1452 if (cid
<= widths
.excepsV
[a
].last
) {
1453 h
= widths
.excepsV
[a
].height
;
1454 vx
= widths
.excepsV
[a
].vx
;
1455 vy
= widths
.excepsV
[a
].vy
;
1468 int GfxCIDFont::getWMode() {
1469 return cMap
? cMap
->getWMode() : 0;
1472 CharCodeToUnicode
*GfxCIDFont::getToUnicode() {
1479 GString
*GfxCIDFont::getCollection() {
1480 return cMap
? cMap
->getCollection() : (GString
*)NULL
;
1483 //------------------------------------------------------------------------
1485 //------------------------------------------------------------------------
1487 GfxFontDict::GfxFontDict(XRef
*xref
, Ref
*fontDictRef
, Dict
*fontDict
) {
1492 numFonts
= fontDict
->getLength();
1493 fonts
= (GfxFont
**)gmallocn(numFonts
, sizeof(GfxFont
*));
1494 for (i
= 0; i
< numFonts
; ++i
) {
1495 fontDict
->getValNF(i
, &obj1
);
1496 obj1
.fetch(xref
, &obj2
);
1497 if (obj2
.isDict()) {
1501 // no indirect reference for this font, so invent a unique one
1502 // (legal generation numbers are five digits, so any 6-digit
1503 // number would be safe)
1506 r
.gen
= 100000 + fontDictRef
->num
;
1511 fonts
[i
] = GfxFont::makeFont(xref
, fontDict
->getKey(i
),
1513 if (fonts
[i
] && !fonts
[i
]->isOk()) {
1518 error(-1, "font resource is not a dictionary");
1526 GfxFontDict::~GfxFontDict() {
1529 for (i
= 0; i
< numFonts
; ++i
) {
1537 GfxFont
*GfxFontDict::lookup(char *tag
) {
1540 for (i
= 0; i
< numFonts
; ++i
) {
1541 if (fonts
[i
] && fonts
[i
]->matches(tag
)) {