Skip to content

Commit 63e4f02

Browse files
committed
Add support for stemHW/stemVW/stemSnapH/stemSnapV
1 parent 22264c0 commit 63e4f02

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

fonts.js

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,18 @@ var Type1Parser = function() {
13351335
* Returns an object containing a Subrs array and a CharStrings array
13361336
* extracted from and eexec encrypted block of data
13371337
*/
1338+
function readNumberArray(str, index) {
1339+
var start = ++index;
1340+
var count = 0;
1341+
while (str[index++] != "]")
1342+
count++;
1343+
1344+
var array = str.substr(start, count).split(" ");
1345+
for (var i = 0; i < array.length; i++)
1346+
array[i] = parseFloat(array[i] || 0);
1347+
return array;
1348+
};
1349+
13381350
this.extractFontProgram = function t1_extractFontProgram(stream) {
13391351
var eexec = decrypt(stream, kEexecEncryptionKey, 4);
13401352
var eexecString = "";
@@ -1344,7 +1356,11 @@ var Type1Parser = function() {
13441356
var glyphsSection = false, subrsSection = false;
13451357
var extracted = {
13461358
subrs: [],
1347-
charstrings: []
1359+
charstrings: [],
1360+
properties: {
1361+
stemSnapH: [0, 0],
1362+
stemSnapV: [0, 0]
1363+
}
13481364
};
13491365

13501366
var glyph = "";
@@ -1372,14 +1388,32 @@ var Type1Parser = function() {
13721388
extracted.subrs.push(str.charstring);
13731389
}
13741390
i += length + 3;
1375-
} else if (c == " ") {
1391+
} else if (c == " " || c == "\n") {
13761392
length = parseInt(token);
13771393
token = "";
13781394
} else {
13791395
token += c;
13801396
if (!glyphsSection) {
1381-
glyphsSection = token.indexOf("/CharString") != -1;
1382-
subrsSection = subrsSection || token.indexOf("Subrs") != -1;
1397+
switch (token) {
1398+
case "/CharString":
1399+
glyphsSection = true;
1400+
break;
1401+
case "/Subrs":
1402+
subrsSection = true;
1403+
break;
1404+
case "/StdHW":
1405+
extracted.properties.stdHW = readNumberArray(eexecString, i + 2)[0];
1406+
break;
1407+
case "/StdVW":
1408+
extracted.properties.stdVW = readNumberArray(eexecString, i + 2)[0];
1409+
break;
1410+
case "/StemSnapH":
1411+
extracted.properties.stemSnapH = readNumberArray(eexecString, i + 2);
1412+
break;
1413+
case "/StemSnapV":
1414+
extracted.properties.stemSnapV = readNumberArray(eexecString, i + 2);
1415+
break;
1416+
}
13831417
} else if (c == "/") {
13841418
token = glyph = "";
13851419
while ((c = eexecString[++i]) != " ")
@@ -1400,18 +1434,6 @@ var Type1Parser = function() {
14001434
textMatrix: null
14011435
};
14021436

1403-
function readNumberArray(str, index) {
1404-
var start = ++index;
1405-
var count = 0;
1406-
while ((c = str[index++]) != "]")
1407-
count++;
1408-
1409-
var array = str.substr(start, count).split(" ");
1410-
for (var i = 0; i < array.length; i++)
1411-
array[i] = parseFloat(array[i]);
1412-
return array;
1413-
};
1414-
14151437
var token = "";
14161438
var count = headerString.length;
14171439
for (var i = 0; i < count; i++) {
@@ -1439,7 +1461,6 @@ var Type1Parser = function() {
14391461

14401462
return info;
14411463
};
1442-
14431464
};
14441465

14451466
/**
@@ -1513,13 +1534,14 @@ var CFF = function(name, file, properties) {
15131534

15141535
var headerBlock = file.getBytes(length1);
15151536
var header = type1Parser.extractFontHeader(headerBlock);
1516-
for (var info in header) {
1537+
for (var info in header)
15171538
properties[info] = header[info];
1518-
}
15191539

15201540
// Decrypt the data blocks and retrieve it's content
15211541
var eexecBlock = file.getBytes(length2);
15221542
var data = type1Parser.extractFontProgram(eexecBlock);
1543+
for (var info in data.properties)
1544+
properties[info] = data.properties[info];
15231545

15241546
var charstrings = this.getOrderedCharStrings(data.charstrings);
15251547
var type2Charstrings = this.getType2Charstrings(charstrings);
@@ -1758,14 +1780,22 @@ CFF.prototype = {
17581780
"charstrings": this.createCFFIndexHeader([[0x8B, 0x0E]].concat(glyphs), true),
17591781
17601782
"private": (function(self) {
1761-
log(properties.stemSnapH);
17621783
var data =
17631784
"\x8b\x14" + // defaultWidth
17641785
"\x8b\x15" + // nominalWidth
1765-
"\x8b\x0a" + // StdHW
1766-
"\x8b\x0a" + // StdVW
1767-
"\x8b\x8b\x0c\x0c" + // StemSnapH
1768-
"\x8b\x8b\x0c\x0d"; // StemSnapV
1786+
self.encodeNumber(properties.stdHW) + "\x0a" + // StdHW
1787+
self.encodeNumber(properties.stdVW) + "\x0b"; // StdVW
1788+
1789+
var stemH = properties.stemSnapH;
1790+
for (var i = 0; i < stemH.length; i++)
1791+
data += self.encodeNumber(stemH[i]);
1792+
data += "\x0c\x0c"; // StemSnapH
1793+
1794+
var stemV = properties.stemSnapV;
1795+
for (var i = 0; i < stemV.length; i++)
1796+
data += self.encodeNumber(stemV[i]);
1797+
data += "\x0c\x0d"; // StemSnapV
1798+
17691799
data += self.encodeNumber(data.length + 4) + "\x13"; // Subrs offset
17701800
17711801
return data;

0 commit comments

Comments
 (0)