Improve the extractInfo code to be more robust

This commit is contained in:
Vivien Nicolas 2011-07-01 05:16:27 +02:00
parent 697aa0f15e
commit ae2d130f40

View File

@ -1333,14 +1333,19 @@ var Type1Parser = function() {
* extracted from and eexec encrypted block of data * extracted from and eexec encrypted block of data
*/ */
this.extractFontProgram = function t1_extractFontProgram(stream) { this.extractFontProgram = function t1_extractFontProgram(stream) {
var eexecString = decrypt(stream, kEexecEncryptionKey, 4); var eexec = decrypt(stream, kEexecEncryptionKey, 4);
var subrs = [], glyphs = []; var eexecString = "";
var inGlyphs = false; for (var i = 0; i < eexec.length; i++)
var inSubrs = false; eexecString += String.fromCharCode(eexec[i]);
var glyph = "";
var glyphsSection = false, subrsSection = false;
var extracted = {
subrs: [],
charstrings: []
};
var glyph = "";
var token = ""; var token = "";
var index = 0;
var length = 0; var length = 0;
var c = ""; var c = "";
@ -1348,52 +1353,39 @@ var Type1Parser = function() {
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
var c = eexecString[i]; var c = eexecString[i];
if (inSubrs && c == 0x52) { if ((glyphsSection || subrsSection) && c == "R") {
length = parseInt(length); var data = eexec.slice(i + 3, i + 3 + length);
var data = eexecString.slice(i + 3, i + 3 + length); var encoded = decrypt(data, kCharStringsEncryptionKey, 4);
var encodedSubr = decrypt(data, kCharStringsEncryptionKey, 4); var str = decodeCharString(encoded);
var str = decodeCharString(encodedSubr);
subrs.push(str.charstring); if (glyphsSection) {
i += 3 + length; extracted.charstrings.push({
} else if (inGlyphs && c == 0x52) {
length = parseInt(length);
var data = eexecString.slice(i + 3, i + 3 + length);
var encodedCharstring = decrypt(data, kCharStringsEncryptionKey, 4);
var str = decodeCharString(encodedCharstring);
glyphs.push({
glyph: glyph, glyph: glyph,
data: str.charstring, data: str.charstring,
lsb: str.lsb, lsb: str.lsb,
width: str.width width: str.width
}); });
i += 3 + length; } else {
} else if (inGlyphs && c == 0x2F) { extracted.subrs.push(str.charstring);
}
i += length + 3;
} else if (c == " ") {
length = parseInt(token);
token = ""; token = "";
glyph = "";
while ((c = eexecString[++i]) != 0x20)
glyph += String.fromCharCode(c);
} else if (!inSubrs && !inGlyphs && c == 0x2F && eexecString[i+1] == 0x53) {
while ((c = eexecString[++i]) != 0x20) {};
inSubrs = true;
} else if (c == 0x20) {
index = length;
length = token;
token = "";
} else if (c == 0x2F && eexecString[i+1] == 0x43 && eexecString[i+2] == 0x68) {
while ((c = eexecString[++i]) != 0x20) {};
inSubrs = false;
inGlyphs = true;
} else { } else {
token += String.fromCharCode(c); token += c;
if (!glyphsSection) {
glyphsSection = token.indexOf("/CharString") != -1;
subrsSection = subrsSection || token.indexOf("Subrs") != -1;
} else if (c == "/") {
token = glyph = "";
while ((c = eexecString[++i]) != " ")
glyph += c;
}
} }
} }
return {
subrs: subrs, return extracted;
charstrings: glyphs
}
} }
}; };