Skip to content

Commit ae2d130

Browse files
committed
Improve the extractInfo code to be more robust
1 parent 697aa0f commit ae2d130

File tree

1 file changed

+36
-44
lines changed

1 file changed

+36
-44
lines changed

fonts.js

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,67 +1333,59 @@ var Type1Parser = function() {
13331333
* extracted from and eexec encrypted block of data
13341334
*/
13351335
this.extractFontProgram = function t1_extractFontProgram(stream) {
1336-
var eexecString = decrypt(stream, kEexecEncryptionKey, 4);
1337-
var subrs = [], glyphs = [];
1338-
var inGlyphs = false;
1339-
var inSubrs = false;
1340-
var glyph = "";
1336+
var eexec = decrypt(stream, kEexecEncryptionKey, 4);
1337+
var eexecString = "";
1338+
for (var i = 0; i < eexec.length; i++)
1339+
eexecString += String.fromCharCode(eexec[i]);
1340+
1341+
var glyphsSection = false, subrsSection = false;
1342+
var extracted = {
1343+
subrs: [],
1344+
charstrings: []
1345+
};
13411346

1347+
var glyph = "";
13421348
var token = "";
1343-
var index = 0;
13441349
var length = 0;
13451350

13461351
var c = "";
13471352
var count = eexecString.length;
13481353
for (var i = 0; i < count; i++) {
13491354
var c = eexecString[i];
13501355

1351-
if (inSubrs && c == 0x52) {
1352-
length = parseInt(length);
1353-
var data = eexecString.slice(i + 3, i + 3 + length);
1354-
var encodedSubr = decrypt(data, kCharStringsEncryptionKey, 4);
1355-
var str = decodeCharString(encodedSubr);
1356-
1357-
subrs.push(str.charstring);
1358-
i += 3 + length;
1359-
} else if (inGlyphs && c == 0x52) {
1360-
length = parseInt(length);
1361-
var data = eexecString.slice(i + 3, i + 3 + length);
1362-
var encodedCharstring = decrypt(data, kCharStringsEncryptionKey, 4);
1363-
var str = decodeCharString(encodedCharstring);
1364-
1365-
glyphs.push({
1356+
if ((glyphsSection || subrsSection) && c == "R") {
1357+
var data = eexec.slice(i + 3, i + 3 + length);
1358+
var encoded = decrypt(data, kCharStringsEncryptionKey, 4);
1359+
var str = decodeCharString(encoded);
1360+
1361+
if (glyphsSection) {
1362+
extracted.charstrings.push({
13661363
glyph: glyph,
13671364
data: str.charstring,
13681365
lsb: str.lsb,
13691366
width: str.width
1370-
});
1371-
i += 3 + length;
1372-
} else if (inGlyphs && c == 0x2F) {
1373-
token = "";
1374-
glyph = "";
1375-
1376-
while ((c = eexecString[++i]) != 0x20)
1377-
glyph += String.fromCharCode(c);
1378-
} else if (!inSubrs && !inGlyphs && c == 0x2F && eexecString[i+1] == 0x53) {
1379-
while ((c = eexecString[++i]) != 0x20) {};
1380-
inSubrs = true;
1381-
} else if (c == 0x20) {
1382-
index = length;
1383-
length = token;
1367+
});
1368+
} else {
1369+
extracted.subrs.push(str.charstring);
1370+
}
1371+
i += length + 3;
1372+
} else if (c == " ") {
1373+
length = parseInt(token);
13841374
token = "";
1385-
} else if (c == 0x2F && eexecString[i+1] == 0x43 && eexecString[i+2] == 0x68) {
1386-
while ((c = eexecString[++i]) != 0x20) {};
1387-
inSubrs = false;
1388-
inGlyphs = true;
13891375
} else {
1390-
token += String.fromCharCode(c);
1376+
token += c;
1377+
if (!glyphsSection) {
1378+
glyphsSection = token.indexOf("/CharString") != -1;
1379+
subrsSection = subrsSection || token.indexOf("Subrs") != -1;
1380+
} else if (c == "/") {
1381+
token = glyph = "";
1382+
while ((c = eexecString[++i]) != " ")
1383+
glyph += c;
1384+
}
13911385
}
13921386
}
1393-
return {
1394-
subrs: subrs,
1395-
charstrings: glyphs
1396-
}
1387+
1388+
return extracted;
13971389
}
13981390
};
13991391

0 commit comments

Comments
 (0)