Browse Source

Add support for stemHW/stemVW/stemSnapH/stemSnapV

Vivien Nicolas 14 years ago
parent
commit
63e4f0293f
  1. 78
      fonts.js

78
fonts.js

@ -1335,6 +1335,18 @@ var Type1Parser = function() {
* Returns an object containing a Subrs array and a CharStrings array * Returns an object containing a Subrs array and a CharStrings array
* extracted from and eexec encrypted block of data * extracted from and eexec encrypted block of data
*/ */
function readNumberArray(str, index) {
var start = ++index;
var count = 0;
while (str[index++] != "]")
count++;
var array = str.substr(start, count).split(" ");
for (var i = 0; i < array.length; i++)
array[i] = parseFloat(array[i] || 0);
return array;
};
this.extractFontProgram = function t1_extractFontProgram(stream) { this.extractFontProgram = function t1_extractFontProgram(stream) {
var eexec = decrypt(stream, kEexecEncryptionKey, 4); var eexec = decrypt(stream, kEexecEncryptionKey, 4);
var eexecString = ""; var eexecString = "";
@ -1344,7 +1356,11 @@ var Type1Parser = function() {
var glyphsSection = false, subrsSection = false; var glyphsSection = false, subrsSection = false;
var extracted = { var extracted = {
subrs: [], subrs: [],
charstrings: [] charstrings: [],
properties: {
stemSnapH: [0, 0],
stemSnapV: [0, 0]
}
}; };
var glyph = ""; var glyph = "";
@ -1372,14 +1388,32 @@ var Type1Parser = function() {
extracted.subrs.push(str.charstring); extracted.subrs.push(str.charstring);
} }
i += length + 3; i += length + 3;
} else if (c == " ") { } else if (c == " " || c == "\n") {
length = parseInt(token); length = parseInt(token);
token = ""; token = "";
} else { } else {
token += c; token += c;
if (!glyphsSection) { if (!glyphsSection) {
glyphsSection = token.indexOf("/CharString") != -1; switch (token) {
subrsSection = subrsSection || token.indexOf("Subrs") != -1; case "/CharString":
glyphsSection = true;
break;
case "/Subrs":
subrsSection = true;
break;
case "/StdHW":
extracted.properties.stdHW = readNumberArray(eexecString, i + 2)[0];
break;
case "/StdVW":
extracted.properties.stdVW = readNumberArray(eexecString, i + 2)[0];
break;
case "/StemSnapH":
extracted.properties.stemSnapH = readNumberArray(eexecString, i + 2);
break;
case "/StemSnapV":
extracted.properties.stemSnapV = readNumberArray(eexecString, i + 2);
break;
}
} else if (c == "/") { } else if (c == "/") {
token = glyph = ""; token = glyph = "";
while ((c = eexecString[++i]) != " ") while ((c = eexecString[++i]) != " ")
@ -1400,18 +1434,6 @@ var Type1Parser = function() {
textMatrix: null textMatrix: null
}; };
function readNumberArray(str, index) {
var start = ++index;
var count = 0;
while ((c = str[index++]) != "]")
count++;
var array = str.substr(start, count).split(" ");
for (var i = 0; i < array.length; i++)
array[i] = parseFloat(array[i]);
return array;
};
var token = ""; var token = "";
var count = headerString.length; var count = headerString.length;
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
@ -1439,7 +1461,6 @@ var Type1Parser = function() {
return info; return info;
}; };
}; };
/** /**
@ -1513,13 +1534,14 @@ var CFF = function(name, file, properties) {
var headerBlock = file.getBytes(length1); var headerBlock = file.getBytes(length1);
var header = type1Parser.extractFontHeader(headerBlock); var header = type1Parser.extractFontHeader(headerBlock);
for (var info in header) { for (var info in header)
properties[info] = header[info]; properties[info] = header[info];
}
// Decrypt the data blocks and retrieve it's content // Decrypt the data blocks and retrieve it's content
var eexecBlock = file.getBytes(length2); var eexecBlock = file.getBytes(length2);
var data = type1Parser.extractFontProgram(eexecBlock); var data = type1Parser.extractFontProgram(eexecBlock);
for (var info in data.properties)
properties[info] = data.properties[info];
var charstrings = this.getOrderedCharStrings(data.charstrings); var charstrings = this.getOrderedCharStrings(data.charstrings);
var type2Charstrings = this.getType2Charstrings(charstrings); var type2Charstrings = this.getType2Charstrings(charstrings);
@ -1758,14 +1780,22 @@ CFF.prototype = {
"charstrings": this.createCFFIndexHeader([[0x8B, 0x0E]].concat(glyphs), true), "charstrings": this.createCFFIndexHeader([[0x8B, 0x0E]].concat(glyphs), true),
"private": (function(self) { "private": (function(self) {
log(properties.stemSnapH);
var data = var data =
"\x8b\x14" + // defaultWidth "\x8b\x14" + // defaultWidth
"\x8b\x15" + // nominalWidth "\x8b\x15" + // nominalWidth
"\x8b\x0a" + // StdHW self.encodeNumber(properties.stdHW) + "\x0a" + // StdHW
"\x8b\x0a" + // StdVW self.encodeNumber(properties.stdVW) + "\x0b"; // StdVW
"\x8b\x8b\x0c\x0c" + // StemSnapH
"\x8b\x8b\x0c\x0d"; // StemSnapV var stemH = properties.stemSnapH;
for (var i = 0; i < stemH.length; i++)
data += self.encodeNumber(stemH[i]);
data += "\x0c\x0c"; // StemSnapH
var stemV = properties.stemSnapV;
for (var i = 0; i < stemV.length; i++)
data += self.encodeNumber(stemV[i]);
data += "\x0c\x0d"; // StemSnapV
data += self.encodeNumber(data.length + 4) + "\x13"; // Subrs offset data += self.encodeNumber(data.length + 4) + "\x13"; // Subrs offset
return data; return data;

Loading…
Cancel
Save