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"
32 #include "FontCache.h"
34 //------------------------------------------------------------------------
36 struct StdFontMapEntry
{
41 // Acrobat 4.0 and earlier substituted Base14-compatible fonts without
42 // providing Widths and a FontDescriptor, so we munge the names into
43 // the proper Base14 names. This table is from implementation note 44
44 // in the PDF 1.4 spec, with some additions based on empirical
46 static StdFontMapEntry stdFontMap
[] = {
47 { "Arial", "Helvetica" },
48 { "Arial,Bold", "Helvetica-Bold" },
49 { "Arial,BoldItalic", "Helvetica-BoldOblique" },
50 { "Arial,Italic", "Helvetica-Oblique" },
51 { "Arial-Bold", "Helvetica-Bold" },
52 { "Arial-BoldItalic", "Helvetica-BoldOblique" },
53 { "Arial-BoldItalicMT", "Helvetica-BoldOblique" },
54 { "Arial-BoldMT", "Helvetica-Bold" },
55 { "Arial-Italic", "Helvetica-Oblique" },
56 { "Arial-ItalicMT", "Helvetica-Oblique" },
57 { "ArialMT", "Helvetica" },
58 { "Courier,Bold", "Courier-Bold" },
59 { "Courier,BoldItalic", "Courier-BoldOblique" },
60 { "Courier,Italic", "Courier-Oblique" },
61 { "CourierNew", "Courier" },
62 { "CourierNew,Bold", "Courier-Bold" },
63 { "CourierNew,BoldItalic", "Courier-BoldOblique" },
64 { "CourierNew,Italic", "Courier-Oblique" },
65 { "CourierNew-Bold", "Courier-Bold" },
66 { "CourierNew-BoldItalic", "Courier-BoldOblique" },
67 { "CourierNew-Italic", "Courier-Oblique" },
68 { "CourierNewPS-BoldItalicMT", "Courier-BoldOblique" },
69 { "CourierNewPS-BoldMT", "Courier-Bold" },
70 { "CourierNewPS-ItalicMT", "Courier-Oblique" },
71 { "CourierNewPSMT", "Courier" },
72 { "Helvetica,Bold", "Helvetica-Bold" },
73 { "Helvetica,BoldItalic", "Helvetica-BoldOblique" },
74 { "Helvetica,Italic", "Helvetica-Oblique" },
75 { "Helvetica-BoldItalic", "Helvetica-BoldOblique" },
76 { "Helvetica-Italic", "Helvetica-Oblique" },
77 { "Symbol,Bold", "Symbol" },
78 { "Symbol,BoldItalic", "Symbol" },
79 { "Symbol,Italic", "Symbol" },
80 { "TimesNewRoman", "Times-Roman" },
81 { "TimesNewRoman,Bold", "Times-Bold" },
82 { "TimesNewRoman,BoldItalic", "Times-BoldItalic" },
83 { "TimesNewRoman,Italic", "Times-Italic" },
84 { "TimesNewRoman-Bold", "Times-Bold" },
85 { "TimesNewRoman-BoldItalic", "Times-BoldItalic" },
86 { "TimesNewRoman-Italic", "Times-Italic" },
87 { "TimesNewRomanPS", "Times-Roman" },
88 { "TimesNewRomanPS-Bold", "Times-Bold" },
89 { "TimesNewRomanPS-BoldItalic", "Times-BoldItalic" },
90 { "TimesNewRomanPS-BoldItalicMT", "Times-BoldItalic" },
91 { "TimesNewRomanPS-BoldMT", "Times-Bold" },
92 { "TimesNewRomanPS-Italic", "Times-Italic" },
93 { "TimesNewRomanPS-ItalicMT", "Times-Italic" },
94 { "TimesNewRomanPSMT", "Times-Roman" },
95 { "TimesNewRomanPSMT,Bold", "Times-Bold" },
96 { "TimesNewRomanPSMT,BoldItalic", "Times-BoldItalic" },
97 { "TimesNewRomanPSMT,Italic", "Times-Italic" }
100 //------------------------------------------------------------------------
102 //------------------------------------------------------------------------
104 GfxFont
*GfxFont::makeFont(XRef
*xref
, char *tagA
, Ref idA
, Dict
*fontDict
) {
109 // get base font name
111 fontDict
->lookup("BaseFont", &obj1
);
113 nameA
= new GString(obj1
.getName());
116 sprintf(buf
,"Unnamed-%d-%d", idA
.gen
, idA
.num
);
117 nameA
= new GString(buf
);
123 fontDict
->lookup("Subtype", &obj1
);
124 if (obj1
.isName("Type1") || obj1
.isName("MMType1")) {
125 font
= new Gfx8BitFont(xref
, tagA
, idA
, nameA
, fontType1
, fontDict
);
126 } else if (obj1
.isName("Type1C")) {
127 font
= new Gfx8BitFont(xref
, tagA
, idA
, nameA
, fontType1C
, fontDict
);
128 } else if (obj1
.isName("Type3")) {
129 font
= new Gfx8BitFont(xref
, tagA
, idA
, nameA
, fontType3
, fontDict
);
130 } else if (obj1
.isName("TrueType")) {
131 font
= new Gfx8BitFont(xref
, tagA
, idA
, nameA
, fontTrueType
, fontDict
);
132 } else if (obj1
.isName("Type0")) {
133 font
= new GfxCIDFont(xref
, tagA
, idA
, nameA
, fontDict
);
135 error(-1, "Unknown font type: '%s'",
136 obj1
.isName() ? obj1
.getName() : "???");
137 font
= new Gfx8BitFont(xref
, tagA
, idA
, nameA
, fontUnknownType
, fontDict
);
141 if (font
&& !font
->isOk()) {
149 GfxFont::GfxFont(char *tagA
, Ref idA
, GString
*nameA
) {
151 tag
= new GString(tagA
);
159 GfxFont::~GfxFont() {
161 if (origName
&& origName
!= name
) {
175 void GfxFont::readFontDescriptor(XRef
*xref
, Dict
*fontDict
) {
176 Object obj1
, obj2
, obj3
, obj4
;
180 // assume Times-Roman by default (for substitution purposes)
187 if (fontDict
->lookup("FontDescriptor", &obj1
)->isDict()) {
190 if (obj1
.dictLookup("Flags", &obj2
)->isInt()) {
191 flags
= obj2
.getInt();
196 obj1
.dictLookup("FontName", &obj2
);
198 embFontName
= new GString(obj2
.getName());
202 // look for embedded font file
203 if (obj1
.dictLookupNF("FontFile", &obj2
)->isRef()) {
204 embFontID
= obj2
.getRef();
205 if (type
!= fontType1
) {
206 error(-1, "Mismatch between font type and embedded font file");
211 if (embFontID
.num
== -1 &&
212 obj1
.dictLookupNF("FontFile2", &obj2
)->isRef()) {
213 embFontID
= obj2
.getRef();
214 if (type
!= fontTrueType
&& type
!= fontCIDType2
) {
215 error(-1, "Mismatch between font type and embedded font file");
216 type
= type
== fontCIDType0
? fontCIDType2
: fontTrueType
;
220 if (embFontID
.num
== -1 &&
221 obj1
.dictLookupNF("FontFile3", &obj2
)->isRef()) {
222 if (obj2
.fetch(xref
, &obj3
)->isStream()) {
223 obj3
.streamGetDict()->lookup("Subtype", &obj4
);
224 if (obj4
.isName("Type1")) {
225 embFontID
= obj2
.getRef();
226 if (type
!= fontType1
) {
227 error(-1, "Mismatch between font type and embedded font file");
230 } else if (obj4
.isName("Type1C")) {
231 embFontID
= obj2
.getRef();
232 if (type
!= fontType1
&& type
!= fontType1C
) {
233 error(-1, "Mismatch between font type and embedded font file");
236 } else if (obj4
.isName("TrueType")) {
237 embFontID
= obj2
.getRef();
238 if (type
!= fontTrueType
) {
239 error(-1, "Mismatch between font type and embedded font file");
242 } else if (obj4
.isName("CIDFontType0C")) {
243 embFontID
= obj2
.getRef();
244 if (type
!= fontCIDType0
) {
245 error(-1, "Mismatch between font type and embedded font file");
247 type
= fontCIDType0C
;
249 error(-1, "Unknown embedded font type '%s'",
250 obj4
.isName() ? obj4
.getName() : "???");
258 // look for MissingWidth
259 obj1
.dictLookup("MissingWidth", &obj2
);
261 missingWidth
= obj2
.getNum();
265 // get Ascent and Descent
266 obj1
.dictLookup("Ascent", &obj2
);
268 t
= 0.001 * obj2
.getNum();
269 // some broken font descriptors set ascent and descent to 0
275 obj1
.dictLookup("Descent", &obj2
);
277 t
= 0.001 * obj2
.getNum();
278 // some broken font descriptors set ascent and descent to 0
282 // some broken font descriptors specify a positive descent
290 if (obj1
.dictLookup("FontBBox", &obj2
)->isArray()) {
291 for (i
= 0; i
< 4 && i
< obj2
.arrayGetLength(); ++i
) {
292 if (obj2
.arrayGet(i
, &obj3
)->isNum()) {
293 fontBBox
[i
] = 0.001 * obj3
.getNum();
304 CharCodeToUnicode
*GfxFont::readToUnicodeCMap(Dict
*fontDict
, int nBits
,
305 CharCodeToUnicode
*ctu
) {
310 if (!fontDict
->lookup("ToUnicode", &obj1
)->isStream()) {
316 while ((c
= obj1
.streamGetChar()) != EOF
) {
322 ctu
->mergeCMap(buf
, nBits
);
324 ctu
= CharCodeToUnicode::parseCMap(buf
, nBits
);
330 void GfxFont::findExtFontFile() {
331 static char *type1Exts
[] = { ".pfa", ".pfb", ".ps", "", NULL
};
332 static char *ttExts
[] = { ".ttf", NULL
};
335 if (type
== fontType1
) {
336 extFontFile
= globalParams
->findFontFile(name
, type1Exts
);
337 } else if (type
== fontTrueType
) {
338 extFontFile
= globalParams
->findFontFile(name
, ttExts
);
343 char *GfxFont::readExtFontFile(int *len
) {
347 if (!(f
= myfopen(extFontFile
->getCString(), "rb"))) {
348 error(-1, "External font file '%s' vanished", extFontFile
->getCString());
351 myfseek(f
, 0, SEEK_END
);
352 *len
= (int)myftell(f
);
353 myfseek(f
, 0, SEEK_SET
);
354 buf
= (char *)gmalloc(*len
);
355 if ((int)myfread(buf
, 1, *len
, f
) != *len
) {
356 error(-1, "Error reading external font file '%s'",
357 extFontFile
->getCString());
363 char *GfxFont::readEmbFontFile(XRef
*xref
, int *len
) {
370 obj1
.initRef(embFontID
.num
, embFontID
.gen
);
371 obj1
.fetch(xref
, &obj2
);
372 if (!obj2
.isStream()) {
373 error(-1, "Embedded font file is not a stream");
379 str
= obj2
.getStream();
384 while ((c
= str
->getChar()) != EOF
) {
387 buf
= (char *)grealloc(buf
, size
);
400 //------------------------------------------------------------------------
402 //------------------------------------------------------------------------
404 Gfx8BitFont::Gfx8BitFont(XRef
*xref
, char *tagA
, Ref idA
, GString
*nameA
,
405 GfxFontType typeA
, Dict
*fontDict
):
406 GfxFont(tagA
, idA
, nameA
)
409 BuiltinFont
*builtinFont
;
411 GBool baseEncFromFontFile
;
419 Unicode toUnicode
[256];
420 CharCodeToUnicode
*utu
, *ctu2
;
423 int firstChar
, lastChar
;
425 Object obj1
, obj2
, obj3
;
431 // do font name substitution for various aliases of the Base 14 font
434 name2
= name
->copy();
436 while (i
< name2
->getLength()) {
437 if (name2
->getChar(i
) == ' ') {
444 b
= sizeof(stdFontMap
) / sizeof(StdFontMapEntry
);
445 // invariant: stdFontMap[a].altName <= name2 < stdFontMap[b].altName
448 if (name2
->cmp(stdFontMap
[m
].altName
) >= 0) {
454 if (!name2
->cmp(stdFontMap
[a
].altName
)) {
455 name
= new GString(stdFontMap
[a
].properName
);
460 // is it a built-in font?
463 for (i
= 0; i
< nBuiltinFonts
; ++i
) {
464 if (!name
->cmp(builtinFonts
[i
].name
)) {
465 builtinFont
= &builtinFonts
[i
];
471 // default ascent/descent values
473 ascent
= 0.001 * builtinFont
->ascent
;
474 descent
= 0.001 * builtinFont
->descent
;
475 fontBBox
[0] = 0.001 * builtinFont
->bbox
[0];
476 fontBBox
[1] = 0.001 * builtinFont
->bbox
[1];
477 fontBBox
[2] = 0.001 * builtinFont
->bbox
[2];
478 fontBBox
[3] = 0.001 * builtinFont
->bbox
[3];
482 fontBBox
[0] = fontBBox
[1] = fontBBox
[2] = fontBBox
[3] = 0;
485 // get info from font descriptor
486 readFontDescriptor(xref
, fontDict
);
488 // for non-embedded fonts, don't trust the ascent/descent/bbox
489 // values from the font descriptor
490 if (builtinFont
&& embFontID
.num
< 0) {
491 ascent
= 0.001 * builtinFont
->ascent
;
492 descent
= 0.001 * builtinFont
->descent
;
493 fontBBox
[0] = 0.001 * builtinFont
->bbox
[0];
494 fontBBox
[1] = 0.001 * builtinFont
->bbox
[1];
495 fontBBox
[2] = 0.001 * builtinFont
->bbox
[2];
496 fontBBox
[3] = 0.001 * builtinFont
->bbox
[3];
499 // look for an external font file
503 fontMat
[0] = fontMat
[3] = 1;
504 fontMat
[1] = fontMat
[2] = fontMat
[4] = fontMat
[5] = 0;
505 if (fontDict
->lookup("FontMatrix", &obj1
)->isArray()) {
506 for (i
= 0; i
< 6 && i
< obj1
.arrayGetLength(); ++i
) {
507 if (obj1
.arrayGet(i
, &obj2
)->isNum()) {
508 fontMat
[i
] = obj2
.getNum();
515 // get Type 3 bounding box, font definition, and resources
516 if (type
== fontType3
) {
517 if (fontDict
->lookup("FontBBox", &obj1
)->isArray()) {
518 for (i
= 0; i
< 4 && i
< obj1
.arrayGetLength(); ++i
) {
519 if (obj1
.arrayGet(i
, &obj2
)->isNum()) {
520 fontBBox
[i
] = obj2
.getNum();
526 if (!fontDict
->lookup("CharProcs", &charProcs
)->isDict()) {
527 error(-1, "Missing or invalid CharProcs dictionary in Type 3 font");
530 if (!fontDict
->lookup("Resources", &resources
)->isDict()) {
535 //----- build the font encoding -----
537 // Encodings start with a base encoding, which can come from
538 // (in order of priority):
539 // 1. FontDict.Encoding or FontDict.Encoding.BaseEncoding
540 // - MacRoman / MacExpert / WinAnsi / Standard
541 // 2. embedded or external font file
543 // - builtin --> builtin encoding
544 // - TrueType --> WinAnsiEncoding
545 // - others --> StandardEncoding
546 // and then add a list of differences (if any) from
547 // FontDict.Encoding.Differences.
549 // check FontDict for base encoding
550 hasEncoding
= gFalse
;
551 usesMacRomanEnc
= gFalse
;
553 baseEncFromFontFile
= gFalse
;
554 fontDict
->lookup("Encoding", &obj1
);
556 obj1
.dictLookup("BaseEncoding", &obj2
);
557 if (obj2
.isName("MacRomanEncoding")) {
559 usesMacRomanEnc
= gTrue
;
560 baseEnc
= macRomanEncoding
;
561 } else if (obj2
.isName("MacExpertEncoding")) {
563 baseEnc
= macExpertEncoding
;
564 } else if (obj2
.isName("WinAnsiEncoding")) {
566 baseEnc
= winAnsiEncoding
;
569 } else if (obj1
.isName("MacRomanEncoding")) {
571 usesMacRomanEnc
= gTrue
;
572 baseEnc
= macRomanEncoding
;
573 } else if (obj1
.isName("MacExpertEncoding")) {
575 baseEnc
= macExpertEncoding
;
576 } else if (obj1
.isName("WinAnsiEncoding")) {
578 baseEnc
= winAnsiEncoding
;
581 // check embedded or external font file for base encoding
582 // (only for Type 1 fonts - trying to get an encoding out of a
583 // TrueType font is a losing proposition)
587 if (type
== fontType1
&& (extFontFile
|| embFontID
.num
>= 0)) {
589 ffT1
= FoFiType1::load(extFontFile
->getCString());
591 buf
= readEmbFontFile(xref
, &len
);
592 ffT1
= FoFiType1::make(buf
, len
);
595 if (ffT1
->getName()) {
599 embFontName
= new GString(ffT1
->getName());
602 baseEnc
= ffT1
->getEncoding();
603 baseEncFromFontFile
= gTrue
;
606 } else if (type
== fontType1C
&& (extFontFile
|| embFontID
.num
>= 0)) {
608 ffT1C
= FoFiType1C::load(extFontFile
->getCString());
610 buf
= readEmbFontFile(xref
, &len
);
611 ffT1C
= FoFiType1C::make(buf
, len
);
614 if (ffT1C
->getName()) {
618 embFontName
= new GString(ffT1C
->getName());
621 baseEnc
= ffT1C
->getEncoding();
622 baseEncFromFontFile
= gTrue
;
630 // get default base encoding
632 if (builtinFont
&& embFontID
.num
< 0) {
633 baseEnc
= builtinFont
->defaultBaseEnc
;
635 } else if (type
== fontTrueType
) {
636 baseEnc
= winAnsiEncoding
;
638 baseEnc
= standardEncoding
;
642 // copy the base encoding
643 for (i
= 0; i
< 256; ++i
) {
645 if ((encFree
[i
] = baseEncFromFontFile
) && enc
[i
]) {
646 enc
[i
] = copyString(baseEnc
[i
]);
650 // some Type 1C font files have empty encodings, which can break the
651 // T1C->T1 conversion (since the 'seac' operator depends on having
652 // the accents in the encoding), so we fill in any gaps from
654 if (type
== fontType1C
&& (extFontFile
|| embFontID
.num
>= 0) &&
655 baseEncFromFontFile
) {
656 for (i
= 0; i
< 256; ++i
) {
657 if (!enc
[i
] && standardEncoding
[i
]) {
658 enc
[i
] = standardEncoding
[i
];
664 // merge differences into encoding
666 obj1
.dictLookup("Differences", &obj2
);
667 if (obj2
.isArray()) {
670 for (i
= 0; i
< obj2
.arrayGetLength(); ++i
) {
671 obj2
.arrayGet(i
, &obj3
);
673 code
= obj3
.getInt();
674 } else if (obj3
.isName()) {
675 if (code
>= 0 && code
< 256) {
679 enc
[code
] = copyString(obj3
.getName());
680 encFree
[code
] = gTrue
;
684 error(-1, "Wrong type in font encoding resource differences (%s)",
700 //----- build the mapping to Unicode -----
702 // pass 1: use the name-to-Unicode mapping table
703 missing
= hex
= gFalse
;
704 for (code
= 0; code
< 256; ++code
) {
705 if ((charName
= enc
[code
])) {
706 if (!(toUnicode
[code
] = globalParams
->mapNameToUnicode(charName
)) &&
707 strcmp(charName
, ".notdef")) {
708 // if it wasn't in the name-to-Unicode table, check for a
709 // name that looks like 'Axx' or 'xx', where 'A' is any letter
710 // and 'xx' is two hex digits
711 if ((strlen(charName
) == 3 &&
712 isalpha(charName
[0]) &&
713 isxdigit(charName
[1]) && isxdigit(charName
[2]) &&
714 ((charName
[1] >= 'a' && charName
[1] <= 'f') ||
715 (charName
[1] >= 'A' && charName
[1] <= 'F') ||
716 (charName
[2] >= 'a' && charName
[2] <= 'f') ||
717 (charName
[2] >= 'A' && charName
[2] <= 'F'))) ||
718 (strlen(charName
) == 2 &&
719 isxdigit(charName
[0]) && isxdigit(charName
[1]) &&
720 ((charName
[0] >= 'a' && charName
[0] <= 'f') ||
721 (charName
[0] >= 'A' && charName
[0] <= 'F') ||
722 (charName
[1] >= 'a' && charName
[1] <= 'f') ||
723 (charName
[1] >= 'A' && charName
[1] <= 'F')))) {
733 // pass 2: try to fill in the missing chars, looking for names of
734 // the form 'Axx', 'xx', 'Ann', 'ABnn', or 'nn', where 'A' and 'B'
735 // are any letters, 'xx' is two hex digits, and 'nn' is 2-4
737 if (missing
&& globalParams
->getMapNumericCharNames()) {
738 for (code
= 0; code
< 256; ++code
) {
739 if ((charName
= enc
[code
]) && !toUnicode
[code
] &&
740 strcmp(charName
, ".notdef")) {
741 n
= strlen(charName
);
743 if (hex
&& n
== 3 && isalpha(charName
[0]) &&
744 isxdigit(charName
[1]) && isxdigit(charName
[2])) {
745 sscanf(charName
+1, "%x", &code2
);
746 } else if (hex
&& n
== 2 &&
747 isxdigit(charName
[0]) && isxdigit(charName
[1])) {
748 sscanf(charName
, "%x", &code2
);
749 } else if (!hex
&& n
>= 2 && n
<= 4 &&
750 isdigit(charName
[0]) && isdigit(charName
[1])) {
751 code2
= atoi(charName
);
752 } else if (n
>= 3 && n
<= 5 &&
753 isdigit(charName
[1]) && isdigit(charName
[2])) {
754 code2
= atoi(charName
+1);
755 } else if (n
>= 4 && n
<= 6 &&
756 isdigit(charName
[2]) && isdigit(charName
[3])) {
757 code2
= atoi(charName
+2);
759 if (code2
>= 0 && code2
<= 0xff) {
760 toUnicode
[code
] = (Unicode
)code2
;
766 // construct the char code -> Unicode mapping object
767 ctu
= CharCodeToUnicode::make8BitToUnicode(toUnicode
);
769 // merge in a ToUnicode CMap, if there is one -- this overwrites
770 // existing entries in ctu, i.e., the ToUnicode CMap takes
771 // precedence, but the other encoding info is allowed to fill in any
773 readToUnicodeCMap(fontDict
, 8, ctu
);
775 // look for a Unicode-to-Unicode mapping
776 if (name
&& (utu
= globalParams
->getUnicodeToUnicode(name
))) {
777 for (i
= 0; i
< 256; ++i
) {
780 ctu2
= CharCodeToUnicode::make8BitToUnicode(toUnicode
);
781 for (i
= 0; i
< 256; ++i
) {
782 n
= ctu
->mapToUnicode((CharCode
)i
, uBuf
, 8);
784 n
= utu
->mapToUnicode((CharCode
)uBuf
[0], uBuf
, 8);
786 ctu2
->setMapping((CharCode
)i
, uBuf
, n
);
795 //----- get the character widths -----
797 // initialize all widths
798 for (code
= 0; code
< 256; ++code
) {
799 widths
[code
] = missingWidth
* 0.001;
802 // use widths from font dict, if present
803 fontDict
->lookup("FirstChar", &obj1
);
804 firstChar
= obj1
.isInt() ? obj1
.getInt() : 0;
806 if (firstChar
< 0 || firstChar
> 255) {
809 fontDict
->lookup("LastChar", &obj1
);
810 lastChar
= obj1
.isInt() ? obj1
.getInt() : 255;
812 if (lastChar
< 0 || lastChar
> 255) {
815 mul
= (type
== fontType3
) ? fontMat
[0] : 0.001;
816 fontDict
->lookup("Widths", &obj1
);
817 if (obj1
.isArray()) {
818 flags
|= fontFixedWidth
;
819 if (obj1
.arrayGetLength() < lastChar
- firstChar
+ 1) {
820 lastChar
= firstChar
+ obj1
.arrayGetLength() - 1;
822 for (code
= firstChar
; code
<= lastChar
; ++code
) {
823 obj1
.arrayGet(code
- firstChar
, &obj2
);
825 widths
[code
] = obj2
.getNum() * mul
;
826 if (widths
[code
] != widths
[firstChar
]) {
827 flags
&= ~fontFixedWidth
;
833 // use widths from built-in font
834 } else if (builtinFont
) {
835 // this is a kludge for broken PDF files that encode char 32
837 if (builtinFont
->widths
->getWidth("space", &w
)) {
838 widths
[32] = 0.001 * w
;
840 for (code
= 0; code
< 256; ++code
) {
841 if (enc
[code
] && builtinFont
->widths
->getWidth(enc
[code
], &w
)) {
842 widths
[code
] = 0.001 * w
;
846 // couldn't find widths -- use defaults
848 // this is technically an error -- the Widths entry is required
849 // for all but the Base-14 fonts -- but certain PDF generators
850 // apparently don't include widths for Arial and TimesNewRoman
851 if (isFixedWidth()) {
853 } else if (isSerif()) {
864 builtinFont
= builtinFontSubst
[i
];
865 // this is a kludge for broken PDF files that encode char 32
867 if (builtinFont
->widths
->getWidth("space", &w
)) {
868 widths
[32] = 0.001 * w
;
870 for (code
= 0; code
< 256; ++code
) {
871 if (enc
[code
] && builtinFont
->widths
->getWidth(enc
[code
], &w
)) {
872 widths
[code
] = 0.001 * w
;
881 Gfx8BitFont::~Gfx8BitFont() {
884 for (i
= 0; i
< 256; ++i
) {
885 if (encFree
[i
] && enc
[i
]) {
890 if (charProcs
.isDict()) {
893 if (resources
.isDict()) {
898 int Gfx8BitFont::getNextChar(char *s
, int len
, CharCode
*code
,
899 Unicode
*u
, int uSize
, int *uLen
,
900 double *dx
, double *dy
, double *ox
, double *oy
) {
903 *code
= c
= (CharCode
)(*s
& 0xff);
904 *uLen
= ctu
->mapToUnicode(c
, u
, uSize
);
910 CharCodeToUnicode
*Gfx8BitFont::getToUnicode() {
915 Gushort
*Gfx8BitFont::getCodeToGIDMap(FoFiTrueType
*ff
) {
917 int cmapPlatform
, cmapEncoding
;
918 int unicodeCmap
, macRomanCmap
, msSymbolCmap
, cmap
;
919 GBool useMacRoman
, useUnicode
;
924 map
= (Gushort
*)gmallocn(256, sizeof(Gushort
));
925 for (i
= 0; i
< 256; ++i
) {
929 // To match up with the Adobe-defined behaviour, we choose a cmap
931 // 1. If the PDF font has an encoding:
932 // 1a. If the PDF font specified MacRomanEncoding and the
933 // TrueType font has a Macintosh Roman cmap, use it, and
934 // reverse map the char names through MacRomanEncoding to
936 // 1b. If the TrueType font has a Microsoft Unicode cmap or a
937 // non-Microsoft Unicode cmap, use it, and use the Unicode
938 // indexes, not the char codes.
939 // 1c. If the PDF font is symbolic and the TrueType font has a
940 // Microsoft Symbol cmap, use it, and use char codes
941 // directly (possibly with an offset of 0xf000).
942 // 1d. If the TrueType font has a Macintosh Roman cmap, use it,
944 // 2. If the PDF font does not have an encoding or the PDF font is
946 // 2a. If the TrueType font has a Macintosh Roman cmap, use it,
947 // and use char codes directly (possibly with an offset of
949 // 2b. If the TrueType font has a Microsoft Symbol cmap, use it,
950 // and use char codes directly (possible with an offset of
952 // 3. If none of these rules apply, use the first cmap and hope for
953 // the best (this shouldn't happen).
954 unicodeCmap
= macRomanCmap
= msSymbolCmap
= -1;
955 for (i
= 0; i
< ff
->getNumCmaps(); ++i
) {
956 cmapPlatform
= ff
->getCmapPlatform(i
);
957 cmapEncoding
= ff
->getCmapEncoding(i
);
958 if ((cmapPlatform
== 3 && cmapEncoding
== 1) ||
961 } else if (cmapPlatform
== 1 && cmapEncoding
== 0) {
963 } else if (cmapPlatform
== 3 && cmapEncoding
== 0) {
968 useMacRoman
= gFalse
;
971 if (usesMacRomanEnc
&& macRomanCmap
>= 0) {
974 } else if (unicodeCmap
>= 0) {
977 } else if ((flags
& fontSymbolic
) && msSymbolCmap
>= 0) {
979 } else if ((flags
& fontSymbolic
) && macRomanCmap
>= 0) {
981 } else if (macRomanCmap
>= 0) {
986 if (macRomanCmap
>= 0) {
988 } else if (msSymbolCmap
>= 0) {
993 // reverse map the char names through MacRomanEncoding, then map the
994 // char codes through the cmap
996 for (i
= 0; i
< 256; ++i
) {
997 if ((charName
= enc
[i
])) {
998 if ((code
= globalParams
->getMacRomanCharCode(charName
))) {
999 map
[i
] = ff
->mapCodeToGID(cmap
, code
);
1004 // map Unicode through the cmap
1005 } else if (useUnicode
) {
1006 for (i
= 0; i
< 256; ++i
) {
1007 if (((charName
= enc
[i
]) &&
1008 (u
= globalParams
->mapNameToUnicode(charName
))) ||
1009 (n
= ctu
->mapToUnicode((CharCode
)i
, &u
, 1))) {
1010 map
[i
] = ff
->mapCodeToGID(cmap
, u
);
1014 // map the char codes through the cmap, possibly with an offset of
1017 for (i
= 0; i
< 256; ++i
) {
1018 if (!(map
[i
] = ff
->mapCodeToGID(cmap
, i
))) {
1019 map
[i
] = ff
->mapCodeToGID(cmap
, 0xf000 + i
);
1024 // try the TrueType 'post' table to handle any unmapped characters
1025 for (i
= 0; i
< 256; ++i
) {
1026 if (!map
[i
] && (charName
= enc
[i
])) {
1027 map
[i
] = (Gushort
)(int)ff
->mapNameToGID(charName
);
1034 Dict
*Gfx8BitFont::getCharProcs() {
1035 return charProcs
.isDict() ? charProcs
.getDict() : (Dict
*)NULL
;
1038 Object
*Gfx8BitFont::getCharProc(int code
, Object
*proc
) {
1039 if (enc
[code
] && charProcs
.isDict()) {
1040 charProcs
.dictLookup(enc
[code
], proc
);
1047 Dict
*Gfx8BitFont::getResources() {
1048 return resources
.isDict() ? resources
.getDict() : (Dict
*)NULL
;
1051 //------------------------------------------------------------------------
1053 //------------------------------------------------------------------------
1055 static int CDECL
cmpWidthExcep(const void *w1
, const void *w2
) {
1056 return ((GfxFontCIDWidthExcep
*)w1
)->first
-
1057 ((GfxFontCIDWidthExcep
*)w2
)->first
;
1060 static int CDECL
cmpWidthExcepV(const void *w1
, const void *w2
) {
1061 return ((GfxFontCIDWidthExcepV
*)w1
)->first
-
1062 ((GfxFontCIDWidthExcepV
*)w2
)->first
;
1065 GfxCIDFont::GfxCIDFont(XRef
*xref
, char *tagA
, Ref idA
, GString
*nameA
,
1067 GfxFont(tagA
, idA
, nameA
)
1070 GString
*collection
, *cMapName
;
1071 Object desFontDictObj
;
1072 Object obj1
, obj2
, obj3
, obj4
, obj5
, obj6
;
1073 CharCodeToUnicode
*utu
;
1077 int excepsSize
, i
, j
, k
, n
;
1081 fontBBox
[0] = fontBBox
[1] = fontBBox
[2] = fontBBox
[3] = 0;
1084 widths
.defWidth
= 1.0;
1085 widths
.defHeight
= -1.0;
1086 widths
.defVY
= 0.880;
1087 widths
.exceps
= NULL
;
1089 widths
.excepsV
= NULL
;
1090 widths
.nExcepsV
= 0;
1094 // get the descendant font
1095 if (!fontDict
->lookup("DescendantFonts", &obj1
)->isArray()) {
1096 error(-1, "Missing DescendantFonts entry in Type 0 font");
1100 if (!obj1
.arrayGet(0, &desFontDictObj
)->isDict()) {
1101 error(-1, "Bad descendant font in Type 0 font");
1105 desFontDict
= desFontDictObj
.getDict();
1108 if (!desFontDict
->lookup("Subtype", &obj1
)) {
1109 error(-1, "Missing Subtype entry in Type 0 descendant font");
1112 if (obj1
.isName("CIDFontType0")) {
1113 type
= fontCIDType0
;
1114 } else if (obj1
.isName("CIDFontType2")) {
1115 type
= fontCIDType2
;
1117 error(-1, "Unknown Type 0 descendant font type '%s'",
1118 obj1
.isName() ? obj1
.getName() : "???");
1123 // get info from font descriptor
1124 readFontDescriptor(xref
, desFontDict
);
1126 // look for an external font file
1129 //----- encoding info -----
1132 if (!desFontDict
->lookup("CIDSystemInfo", &obj1
)->isDict()) {
1133 error(-1, "Missing CIDSystemInfo dictionary in Type 0 descendant font");
1136 obj1
.dictLookup("Registry", &obj2
);
1137 obj1
.dictLookup("Ordering", &obj3
);
1138 if (!obj2
.isString() || !obj3
.isString()) {
1139 error(-1, "Invalid CIDSystemInfo dictionary in Type 0 descendant font");
1142 collection
= obj2
.getString()->copy()->append('-')->append(obj3
.getString());
1147 // look for a ToUnicode CMap
1148 if (!(ctu
= readToUnicodeCMap(fontDict
, 16, NULL
))) {
1150 // the "Adobe-Identity" and "Adobe-UCS" collections don't have
1151 // cidToUnicode files
1152 if (collection
->cmp("Adobe-Identity") &&
1153 collection
->cmp("Adobe-UCS")) {
1155 // look for a user-supplied .cidToUnicode file
1156 if (!(ctu
= globalParams
->getCIDToUnicode(collection
))) {
1157 error(-1, "Unknown character collection '%s'",
1158 collection
->getCString());
1159 // fall-through, assuming the Identity mapping -- this appears
1160 // to match Adobe's behavior
1165 // look for a Unicode-to-Unicode mapping
1166 if (name
&& (utu
= globalParams
->getUnicodeToUnicode(name
))) {
1168 for (c
= 0; c
< ctu
->getLength(); ++c
) {
1169 n
= ctu
->mapToUnicode(c
, uBuf
, 8);
1171 n
= utu
->mapToUnicode((CharCode
)uBuf
[0], uBuf
, 8);
1173 ctu
->setMapping(c
, uBuf
, n
);
1183 // encoding (i.e., CMap)
1184 //~ need to handle a CMap stream here
1185 //~ also need to deal with the UseCMap entry in the stream dict
1186 if (!fontDict
->lookup("Encoding", &obj1
)->isName()) {
1187 error(-1, "Missing or invalid Encoding entry in Type 0 font");
1191 cMapName
= new GString(obj1
.getName());
1193 if (!(cMap
= globalParams
->getCMap(collection
, cMapName
))) {
1194 error(-1, "Unknown CMap '%s' for character collection '%s'",
1195 cMapName
->getCString(), collection
->getCString());
1203 // CIDToGIDMap (for embedded TrueType fonts)
1204 if (type
== fontCIDType2
) {
1205 desFontDict
->lookup("CIDToGIDMap", &obj1
);
1206 if (obj1
.isStream()) {
1209 cidToGID
= (Gushort
*)gmallocn(i
, sizeof(Gushort
));
1211 while ((c1
= obj1
.streamGetChar()) != EOF
&&
1212 (c2
= obj1
.streamGetChar()) != EOF
) {
1213 if (cidToGIDLen
== i
) {
1215 cidToGID
= (Gushort
*)greallocn(cidToGID
, i
, sizeof(Gushort
));
1217 cidToGID
[cidToGIDLen
++] = (Gushort
)((c1
<< 8) + c2
);
1219 } else if (!obj1
.isName("Identity") && !obj1
.isNull()) {
1220 error(-1, "Invalid CIDToGIDMap entry in CID font");
1225 //----- character metrics -----
1227 // default char width
1228 if (desFontDict
->lookup("DW", &obj1
)->isInt()) {
1229 widths
.defWidth
= obj1
.getInt() * 0.001;
1233 // char width exceptions
1234 if (desFontDict
->lookup("W", &obj1
)->isArray()) {
1237 while (i
+ 1 < obj1
.arrayGetLength()) {
1238 obj1
.arrayGet(i
, &obj2
);
1239 obj1
.arrayGet(i
+ 1, &obj3
);
1240 if (obj2
.isInt() && obj3
.isInt() && i
+ 2 < obj1
.arrayGetLength()) {
1241 if (obj1
.arrayGet(i
+ 2, &obj4
)->isNum()) {
1242 if (widths
.nExceps
== excepsSize
) {
1244 widths
.exceps
= (GfxFontCIDWidthExcep
*)
1245 greallocn(widths
.exceps
,
1246 excepsSize
, sizeof(GfxFontCIDWidthExcep
));
1248 widths
.exceps
[widths
.nExceps
].first
= obj2
.getInt();
1249 widths
.exceps
[widths
.nExceps
].last
= obj3
.getInt();
1250 widths
.exceps
[widths
.nExceps
].width
= obj4
.getNum() * 0.001;
1253 error(-1, "Bad widths array in Type 0 font");
1257 } else if (obj2
.isInt() && obj3
.isArray()) {
1258 if (widths
.nExceps
+ obj3
.arrayGetLength() > excepsSize
) {
1259 excepsSize
= (widths
.nExceps
+ obj3
.arrayGetLength() + 15) & ~15;
1260 widths
.exceps
= (GfxFontCIDWidthExcep
*)
1261 greallocn(widths
.exceps
,
1262 excepsSize
, sizeof(GfxFontCIDWidthExcep
));
1265 for (k
= 0; k
< obj3
.arrayGetLength(); ++k
) {
1266 if (obj3
.arrayGet(k
, &obj4
)->isNum()) {
1267 widths
.exceps
[widths
.nExceps
].first
= j
;
1268 widths
.exceps
[widths
.nExceps
].last
= j
;
1269 widths
.exceps
[widths
.nExceps
].width
= obj4
.getNum() * 0.001;
1273 error(-1, "Bad widths array in Type 0 font");
1279 error(-1, "Bad widths array in Type 0 font");
1285 qsort(widths
.exceps
, widths
.nExceps
, sizeof(GfxFontCIDWidthExcep
),
1290 // default metrics for vertical font
1291 if (desFontDict
->lookup("DW2", &obj1
)->isArray() &&
1292 obj1
.arrayGetLength() == 2) {
1293 if (obj1
.arrayGet(0, &obj2
)->isNum()) {
1294 widths
.defVY
= obj2
.getNum() * 0.001;
1297 if (obj1
.arrayGet(1, &obj2
)->isNum()) {
1298 widths
.defHeight
= obj2
.getNum() * 0.001;
1304 // char metric exceptions for vertical font
1305 if (desFontDict
->lookup("W2", &obj1
)->isArray()) {
1308 while (i
+ 1 < obj1
.arrayGetLength()) {
1309 obj1
.arrayGet(i
, &obj2
);
1310 obj1
.arrayGet(i
+ 1, &obj3
);
1311 if (obj2
.isInt() && obj3
.isInt() && i
+ 4 < obj1
.arrayGetLength()) {
1312 if (obj1
.arrayGet(i
+ 2, &obj4
)->isNum() &&
1313 obj1
.arrayGet(i
+ 3, &obj5
)->isNum() &&
1314 obj1
.arrayGet(i
+ 4, &obj6
)->isNum()) {
1315 if (widths
.nExcepsV
== excepsSize
) {
1317 widths
.excepsV
= (GfxFontCIDWidthExcepV
*)
1318 greallocn(widths
.excepsV
,
1319 excepsSize
, sizeof(GfxFontCIDWidthExcepV
));
1321 widths
.excepsV
[widths
.nExcepsV
].first
= obj2
.getInt();
1322 widths
.excepsV
[widths
.nExcepsV
].last
= obj3
.getInt();
1323 widths
.excepsV
[widths
.nExcepsV
].height
= obj4
.getNum() * 0.001;
1324 widths
.excepsV
[widths
.nExcepsV
].vx
= obj5
.getNum() * 0.001;
1325 widths
.excepsV
[widths
.nExcepsV
].vy
= obj6
.getNum() * 0.001;
1328 error(-1, "Bad widths (W2) array in Type 0 font");
1334 } else if (obj2
.isInt() && obj3
.isArray()) {
1335 if (widths
.nExcepsV
+ obj3
.arrayGetLength() / 3 > excepsSize
) {
1337 (widths
.nExcepsV
+ obj3
.arrayGetLength() / 3 + 15) & ~15;
1338 widths
.excepsV
= (GfxFontCIDWidthExcepV
*)
1339 greallocn(widths
.excepsV
,
1340 excepsSize
, sizeof(GfxFontCIDWidthExcepV
));
1343 for (k
= 0; k
< obj3
.arrayGetLength(); k
+= 3) {
1344 if (obj3
.arrayGet(k
, &obj4
)->isNum() &&
1345 obj3
.arrayGet(k
+1, &obj5
)->isNum() &&
1346 obj3
.arrayGet(k
+2, &obj6
)->isNum()) {
1347 widths
.excepsV
[widths
.nExceps
].first
= j
;
1348 widths
.excepsV
[widths
.nExceps
].last
= j
;
1349 widths
.excepsV
[widths
.nExceps
].height
= obj4
.getNum() * 0.001;
1350 widths
.excepsV
[widths
.nExceps
].vx
= obj5
.getNum() * 0.001;
1351 widths
.excepsV
[widths
.nExceps
].vy
= obj6
.getNum() * 0.001;
1355 error(-1, "Bad widths (W2) array in Type 0 font");
1363 error(-1, "Bad widths (W2) array in Type 0 font");
1369 qsort(widths
.excepsV
, widths
.nExcepsV
, sizeof(GfxFontCIDWidthExcepV
),
1374 desFontDictObj
.free();
1384 desFontDictObj
.free();
1388 GfxCIDFont::~GfxCIDFont() {
1395 gfree(widths
.exceps
);
1396 gfree(widths
.excepsV
);
1402 int GfxCIDFont::getNextChar(char *s
, int len
, CharCode
*code
,
1403 Unicode
*u
, int uSize
, int *uLen
,
1404 double *dx
, double *dy
, double *ox
, double *oy
) {
1406 double w
, h
, vx
, vy
;
1416 *code
= (CharCode
)(cid
= cMap
->getCID(s
, len
, &n
));
1418 *uLen
= ctu
->mapToUnicode(cid
, u
, uSize
);
1424 if (cMap
->getWMode() == 0) {
1425 w
= widths
.defWidth
;
1427 if (widths
.nExceps
> 0 && cid
>= widths
.exceps
[0].first
) {
1430 // invariant: widths.exceps[a].first <= cid < widths.exceps[b].first
1433 if (widths
.exceps
[m
].first
<= cid
) {
1439 if (cid
<= widths
.exceps
[a
].last
) {
1440 w
= widths
.exceps
[a
].width
;
1447 h
= widths
.defHeight
;
1448 vx
= widths
.defWidth
/ 2;
1450 if (widths
.nExcepsV
> 0 && cid
>= widths
.excepsV
[0].first
) {
1452 b
= widths
.nExcepsV
;
1453 // invariant: widths.excepsV[a].first <= cid < widths.excepsV[b].first
1456 if (widths
.excepsV
[m
].last
<= cid
) {
1462 if (cid
<= widths
.excepsV
[a
].last
) {
1463 h
= widths
.excepsV
[a
].height
;
1464 vx
= widths
.excepsV
[a
].vx
;
1465 vy
= widths
.excepsV
[a
].vy
;
1478 int GfxCIDFont::getWMode() {
1479 return cMap
? cMap
->getWMode() : 0;
1482 CharCodeToUnicode
*GfxCIDFont::getToUnicode() {
1489 GString
*GfxCIDFont::getCollection() {
1490 return cMap
? cMap
->getCollection() : (GString
*)NULL
;
1493 //------------------------------------------------------------------------
1495 //------------------------------------------------------------------------
1497 GfxFontDict::GfxFontDict(XRef
*xref
, Ref
*fontDictRef
, Dict
*fontDict
) {
1502 numFonts
= fontDict
->getLength();
1503 fonts
= (Entry
*)gmallocn(numFonts
, sizeof(Entry
));
1504 for (i
= 0; i
< numFonts
; ++i
) {
1505 fontDict
->getValNF(i
, &obj1
);
1506 fonts
[i
].tag
= new GString(fontDict
->getKey(i
));
1510 // no indirect reference for this font, so invent a unique one
1511 // (legal generation numbers are five digits, so any 6-digit
1512 // number would be safe)
1515 r
.gen
= 100000 + fontDictRef
->num
;
1520 fonts
[i
].gfxFont
= gfxFontCache
->getGfxFont(xref
, fontDict
->getKey(i
), r
);
1525 GfxFontDict::~GfxFontDict() {
1528 for (i
= 0; i
< numFonts
; ++i
) {
1529 gfxFontCache
->release(fonts
[i
].gfxFont
);
1530 delete fonts
[i
].tag
;
1535 GfxFont
*GfxFontDict::lookup(char *tag
) {
1538 for (i
= 0; i
< numFonts
; ++i
) {
1539 if (fonts
[i
].tag
->cmp(tag
) == 0) {
1540 return fonts
[i
].gfxFont
;