Do not add empty spaces anymore

This commit is contained in:
Vivien Nicolas 2011-06-10 20:45:42 +02:00
parent d1d6621164
commit a73ffc2d30
2 changed files with 91 additions and 94 deletions

View File

@ -755,6 +755,9 @@ Type1Font.prototype = {
// First 2 bytes contains the number of objects contained into this index // First 2 bytes contains the number of objects contained into this index
var count = aObjects.length; var count = aObjects.length;
if (count ==0)
return [0x00, 0x00, 0x00];
var bytes = this.integerToBytes(count, 2); var bytes = this.integerToBytes(count, 2);
for (var i = 0; i < bytes.length; i++) for (var i = 0; i < bytes.length; i++)
data.push(bytes[i]); data.push(bytes[i]);
@ -777,7 +780,7 @@ Type1Font.prototype = {
for (var i =0; i < count; i++) { for (var i =0; i < count; i++) {
for (var j = 0; j < aObjects[i].length; j++) for (var j = 0; j < aObjects[i].length; j++)
data.push(aIsByte ? aObjects[i][j] : aObjects[i][j].charCodeAt(0)); data.push(aIsByte ? aObjects[i][j] : aObjects[i].charCodeAt(j));
} }
return data; return data;
}, },
@ -797,33 +800,20 @@ Type1Font.prototype = {
encodeNumber: function(aValue) { encodeNumber: function(aValue) {
var x = 0; var x = 0;
if (aValue >= -107 && aValue <= 107) { // XXX we don't really care about Type2 optimization here...
return [aValue + 139]; if (aValue >= -32768 && aValue <= 32767) {
} else if (aValue >= 108 && aValue <= 1131) {
x = aValue - 108;
return [
this.integerToBytes(x / 256 + 247, 1),
x % 256
];
} else if (aValue >= -1131 && aValue <= -108) {
x = Math.abs(aValue) - 108;
return [
this.integerToBytes(x / 256 + 251, 1),
x % 256
];
} else if (aValue >= -32768 && aValue <= 32767) {
return [ return [
28, 28,
integerToBytes(aValue >> 8, 1), this.integerToBytes(aValue >> 8, 1),
integerToBytes(aValue, 1) this.integerToBytes(aValue, 1)
]; ];
} else if (aValue >= (-2147483647-1) && aValue <= 2147483647) { } else if (aValue >= (-2147483647-1) && aValue <= 2147483647) {
return [ return [
0xFF, 0xFF,
integerToBytes(aValue >> 24, 1), this.integerToBytes(aValue >> 24, 1),
integerToBytes(aValue >> 16, 1), this.integerToBytes(aValue >> 16, 1),
integerToBytes(aValue >> 8, 1), this.integerToBytes(aValue >> 8, 1),
integerToBytes(aValue, 1) this.integerToBytes(aValue, 1)
]; ];
} else { } else {
error("Value: " + aValue + " is not allowed"); error("Value: " + aValue + " is not allowed");
@ -834,11 +824,8 @@ Type1Font.prototype = {
var charstrings = aFont.get("CharStrings") var charstrings = aFont.get("CharStrings")
var defaultWidth = this.getDefaultWidth(charstrings); var defaultWidth = this.getDefaultWidth(charstrings);
log("defaultWidth to used: " + defaultWidth);
var charstringsCount = 0; var charstringsCount = 0;
var charstringsDataLength = 0; var charstringsDataLength = 0;
var glyphs = {}; var glyphs = {};
var subrs = aFont.get("Private").get("Subrs"); var subrs = aFont.get("Private").get("Subrs");
var parser = new Type1Parser(); var parser = new Type1Parser();
@ -848,7 +835,6 @@ Type1Font.prototype = {
charstringsCount++; charstringsCount++;
charstringsDataLength += glyphs[glyph].length; charstringsDataLength += glyphs[glyph].length;
} }
log("There is " + charstringsCount + " glyphs (size: " + charstringsDataLength + ")"); log("There is " + charstringsCount + " glyphs (size: " + charstringsDataLength + ")");
// Create a CFF font data // Create a CFF font data
@ -865,71 +851,33 @@ Type1Font.prototype = {
cff.set(nameIndex, currentOffset); cff.set(nameIndex, currentOffset);
currentOffset += nameIndex.length; currentOffset += nameIndex.length;
//Top Dict Index // Calculate strings before writing the TopDICT index in order
var topDictIndex = [ // to calculate correct relative offsets for storing 'charset'
0x00, 0x01, 0x01, 0x01, 0x2A, // and 'charstrings' data
248, 27, 0, // version var fontInfo = aFont.get("FontInfo");
248, 28, 1, // Notice var version = fontInfo.get("version");
248, 29, 2, // FullName var notice = fontInfo.get("Notice");
248, 30, 3, // FamilyName var fullName = fontInfo.get("FullName");
248, 20, 4, // Weigth var familyName = fontInfo.get("FamilyName");
82, 251, 98, 250, 105, 249, 72, 5, // FontBBox var weight = fontInfo.get("Weight");
248, 136, 15, // charset (offset: 500) var strings = [version, notice, fullName,
28, 0, 0, 16, // Encoding familyName, weight];
28, 7, 208, 17, // CharStrings (offset: 2000) var stringsIndex = this.createCFFIndexHeader(strings);
28, 0, 55, 28, 39, 16, 18 // Private (offset: 10000) var stringsDataLength = stringsIndex.length;
];
cff.set(topDictIndex, currentOffset);
currentOffset += topDictIndex.length;
// Strings Index // Create the global subroutines index
var stringsIndex = [ var globalSubrsIndex = this.createCFFIndexHeader([]);
0x00, 0x04, 0x01,
0x01, 0x05, 0x06, 0x07, 0x08,
0x31, 0x2E, 0x030, 0x35, // 1.05
0x2B, // +
0x28, // (
0x29 // )
];
cff.set(stringsIndex, currentOffset);
currentOffset += stringsIndex.length;
// Fill the charset header (first byte is the encoding)
// Global Subrs Index var charset = [0x00];
var globalSubrsIndex = [ for (var glyph in glyphs) {
0x00, 0x00, 0x00
];
cff.set(globalSubrsIndex, currentOffset);
currentOffset += globalSubrsIndex.length;
// Fill the space between this and the charset by '1'
var empty = new Array(500 - currentOffset);
for (var i = 0; i < empty.length; i++)
empty[i] = 0x01;
cff.set(empty, currentOffset);
currentOffset += empty.length;
//Declare the letters
var charset = [
0x00
];
for (var glyph in charstrings.map) {
var index = CFFStrings.indexOf(glyph); var index = CFFStrings.indexOf(glyph);
var bytes = this.integerToBytes(index, 2); var bytes = this.integerToBytes(index, 2);
charset.push(bytes[0]); charset.push(bytes[0]);
charset.push(bytes[1]); charset.push(bytes[1]);
} }
cff.set(charset, currentOffset);
currentOffset += charset.length;
// Fill the space between this and the charstrings data by '1'
var empty = new Array(2000 - currentOffset);
for (var i = 0; i < empty.length; i++)
empty[i] = 0x01;
cff.set(empty, currentOffset);
currentOffset += empty.length;
// Convert charstrings
var getNumFor = { var getNumFor = {
"hstem": 1, "hstem": 1,
"vstem": 3, "vstem": 3,
@ -966,16 +914,64 @@ Type1Font.prototype = {
r.push(charstring); r.push(charstring);
} }
var charStringsIndex = this.createCFFIndexHeader(r, true); var charstringsIndex = this.createCFFIndexHeader(r, true);
cff.set(charStringsIndex.join(" ").split(" "), currentOffset); charstringsIndex = charstringsIndex.join(" ").split(" "); // XXX why?
currentOffset += charStringsIndex.length;
// Fill the space between this and the private dict data by '1'
var empty = new Array(10000 - currentOffset); var fontBBox = aFont.get("FontBBox");
for (var i = 0; i < empty.length; i++)
empty[i] = 0x01; //Top Dict Index
cff.set(empty, currentOffset); var topDictIndex = [
currentOffset += empty.length; 0x00, 0x01, 0x01, 0x01, 0x30,
248, 27, 0, // version
248, 28, 1, // Notice
248, 29, 2, // FullName
248, 30, 3, // FamilyName
248, 31, 4, // Weight
];
for (var i = 0; i < fontBBox.length; i++)
topDictIndex = topDictIndex.concat(this.encodeNumber(fontBBox[i]));
topDictIndex.push(5) // FontBBox;
var charsetOffset = currentOffset +
(topDictIndex.length + (4 + 4 + 4 + 7)) +
stringsIndex.length +
globalSubrsIndex.length;
topDictIndex = topDictIndex.concat(this.encodeNumber(charsetOffset));
topDictIndex.push(15); // charset
topDictIndex = topDictIndex.concat([28, 0, 0, 16]) // Encoding
var charstringsOffset = charsetOffset + (charstringsCount * 2) + 1;
topDictIndex = topDictIndex.concat(this.encodeNumber(charstringsOffset));
topDictIndex.push(17); // charstrings
topDictIndex = topDictIndex.concat([28, 0, 55])
var privateOffset = charstringsOffset + charstringsIndex.length;
topDictIndex = topDictIndex.concat(this.encodeNumber(privateOffset));
topDictIndex.push(18); // Private
topDictIndex = topDictIndex.join(" ").split(" ");
// Top Dict Index
cff.set(topDictIndex, currentOffset);
currentOffset += topDictIndex.length;
// Strings Index
cff.set(stringsIndex, currentOffset);
currentOffset += stringsIndex.length;
// Global Subrs Index
cff.set(globalSubrsIndex, currentOffset);
currentOffset += globalSubrsIndex.length;
// Charset Index
cff.set(charset, currentOffset);
currentOffset += charset.length;
// Fill charstrings data
cff.set(charstringsIndex, currentOffset);
currentOffset += charstringsIndex.length;
// Private Data // Private Data
var privateData = [ var privateData = [

View File

@ -277,16 +277,17 @@ var Type2Parser = function(aFilePath) {
// Read the NAME Index // Read the NAME Index
dump("Reading Index: Names"); dump("Reading Index: Names");
font.set("Names", readFontIndexData(aStream)); font.set("Names", readFontIndexData(aStream));
log("Names: " + font.get("Names"));
// Read the Top Dict Index // Read the Top Dict Index
dump("Reading Index: TopDict"); dump("Reading Index: TopDict");
var topDict = readFontIndexData(aStream, true); var topDict = readFontIndexData(aStream, true);
log(topDict); log("TopDict: " + topDict);
// Read the String Index // Read the String Index
dump("Reading Index: Strings"); dump("Reading Index: Strings");
var strings = readFontIndexData(aStream); var strings = readFontIndexData(aStream);
log(strings); log("strings: " + strings);
// Fill up the Strings dictionary with the new unique strings // Fill up the Strings dictionary with the new unique strings
for (var i = 0; i < strings.length; i++) for (var i = 0; i < strings.length; i++)