Skip to content

Commit

Permalink
wtf
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomax committed Mar 9, 2014
1 parent 1fa6fed commit 6f3bf4c
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 90 deletions.
106 changes: 71 additions & 35 deletions src/SFNT/tables/CFF_.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define(
["struct", "dataBuilding", "asHex", "CFFHeader", "NameIndex", "StringIndex", "TopDictIndex", "Subroutines", "Charset", "Encoding", "CharStringIndex", "PrivateDict"],
function(struct, dataBuilder, asHex, CFFHeader, NameIndex, StringIndex, TopDictIndex, Subroutines, Charset, Encoding, CharStringIndex, PrivateDict) {
["struct", "dataBuilding", "asHex", "CFFHeader", "NameIndex", "StringIndex", "TopDictIndex", "SubroutineIndex", "Charset", "Encoding", "CharStringIndex", "PrivateDict"],
function(struct, dataBuilder, asHex, CFFHeader, NameIndex, StringIndex, TopDictIndex, SubroutineIndex, Charset, Encoding, CharStringIndex, PrivateDict) {
"use strict";

var encoder = dataBuilder.encoder,
Expand Down Expand Up @@ -277,6 +277,7 @@ function(struct, dataBuilder, asHex, CFFHeader, NameIndex, StringIndex, TopDictI
var st = serialize(cff[2]), top_length = st.length;
return serialize(cff).slice(header_length + index_length + top_length);
*/

return serialize(cff).slice(header_length + index_length);
};

Expand All @@ -303,15 +304,19 @@ function(struct, dataBuilder, asHex, CFFHeader, NameIndex, StringIndex, TopDictI
]);
this["name index"] = nameIndex;

// top dict comes last, despite living "here" in the CFF

var stringIndex = new StringIndex([
input.fontVersion,
input.fontName,
input.fontFamily
].concat(input.letters));
this["string index"] = stringIndex;

var subroutines = new Subroutines();
this["global subroutines"] = subroutines;
var globalSubroutines = new SubroutineIndex({
count: 0
});
this["global subroutines"] = globalSubroutines;

var charset = new Charset(stringIndex, input);
this["charset"] = charset;
Expand All @@ -322,22 +327,6 @@ function(struct, dataBuilder, asHex, CFFHeader, NameIndex, StringIndex, TopDictI
var charStringIndex = new CharStringIndex(input.letters, input.charString);
this["charstring index"] = charStringIndex;

var topDictIndex = new TopDictIndex({
"version": stringIndex.getStringId(input.fontVersion)
, "full name": stringIndex.getStringId(input.fontName)
, "family name": stringIndex.getStringId(input.fontFamily)
, "weight": 389 // one of the 390 default strings in the CFF string catalog
, "uniqueID": 1 // really this just has to be 'anything'
, "FontBBox": [input.xMin, input.yMin, input.xMax, input.yMax]
, "charset": 0 // placeholder for offset to charset block, from the beginning of the CFF file
, "Encoding": 0 // " " encoding block " "
, "CharStrings": 0 // " " charstrings block " "
, "Private": [0, 0] // " " private dict block " "
});
this["top dict index"] = topDictIndex;

//FIXME: DICT objects seem to clobber each other's value/bindings

var privateDict = new PrivateDict({
"BlueValues": [0, 0]
, "FamilyBlues": [0, 0]
Expand All @@ -346,29 +335,76 @@ function(struct, dataBuilder, asHex, CFFHeader, NameIndex, StringIndex, TopDictI
, "defaultWidthX": input.xMax
, "nominalWidthX": input.xMax
});
var pd_size = privateDict.sizeOf();
this["private dict"] = privateDict;

var topDictIndex = new TopDictIndex({
"version": stringIndex.getStringId(input.fontVersion)
, "FullName": stringIndex.getStringId(input.fontName)
, "FamilyName": stringIndex.getStringId(input.fontFamily)
, "Weight": 389 // one of the 390 default strings in the CFF string catalog
, "UniqueID": 1 // really this just has to be 'anything'
, "FontBBox": [input.xMin, input.yMin, input.xMax, input.yMax]
, "charset": 0 // placeholder for offset to charset block, from the beginning of the CFF file
, "Encoding": 0 // " " encoding block " "
, "CharStrings": 0 // " " charstrings block " "
, "Private": [0, 0] // sizeof + " " private dict block " "
});
this["top dict index"] = topDictIndex;

// generate the rest of the CFF data old-style
this["CFF data block"] = createCFF(input);

// Hook up the charset, encoding, charstrings and private dict offsets.
// we need to do this iteratively because setting their values may change
// the sizeOf for the top dict, and thus the offsets *after* the top dict.
// Hurray.
(function(headerSize, ch_off, en_off, cs_off, pd_off, o_ch_off, o_en_off, o_cs_off, o_pd_off) {
o_ch_off = o_en_off = o_cs_off = o_pd_off = -1;
ch_off = en_off = cs_off = pd_off = 0;
var base;
while(ch_off !== o_ch_off && en_off !== o_en_off && cs_off !== o_cs_off && pd_off !== o_pd_off) {
o_ch_off = ch_off; o_en_off = en_off; o_cs_off = cs_off; o_pd_off = pd_off;

base = headerSize + nameIndex.sizeOf() + topDictIndex.sizeOf() + stringIndex.sizeOf() + globalSubroutines.sizeOf();

ch_off = base;
en_off = ch_off + charset.sizeOf();
cs_off = en_off + encoding.sizeOf();
pd_off = cs_off + charStringIndex.sizeOf();

topDictIndex.set("charset", ch_off);
topDictIndex.set("Encoding", en_off);
topDictIndex.set("CharStrings", cs_off);
topDictIndex.set("Private", [pd_size, pd_off]);
topDictIndex.finalise();
}
}(this.header.sizeOf()));
}
};

CFF.prototype = new struct([
["header", "LITERAL", "the CFF header"]
, ["name index", "LITERAL", "the name index for this font"]
/*
, ["top dict index", "LITERAL", "the global font dict"]
, ["string index", "LITERAL", "the strings used in this font (there are 390 by-spec strings already)"]
, ["global subroutines", "LITERAL", "the global subroutines that all charstrings can use"]
, ["charset", "LITERAL", "the font's character set"]
, ["encoding", "LITERAL", "the encoding information for this font"]
, ["charstring index", "LITERAL", "the charstring definition for all encoded glyphs"]
, ["private dict", "LITERAL", "the private dicts; each dict maps a partial font."]
*/
, ["CFF data block", "LITERAL", "we're not going to do this as a struct build-up right now."]
]);

// FIXME: how the fuck can the CFF block affect the GSUB table. They are independent data
var newStyle = false;

if(newStyle) {
CFF.prototype = new struct([
["header", "LITERAL", "the CFF header"]
, ["name index", "LITERAL", "the name index for this font"]
, ["top dict index", "LITERAL", "the global font dict"]
, ["string index", "LITERAL", "the strings used in this font (there are 390 by-spec strings already)"]
, ["global subroutines", "LITERAL", "the global subroutines that all charstrings can use"]
, ["charset", "LITERAL", "the font's character set"]
, ["encoding", "LITERAL", "the encoding information for this font"]
, ["charstring index", "LITERAL", "the charstring definition for all encoded glyphs"]
, ["private dict", "LITERAL", "the private dicts; each dict maps a partial font."]
]);
} else {
CFF.prototype = new struct([
["header", "LITERAL", "the CFF header"]
, ["name index", "LITERAL", "the name index for this font"]
, ["CFF data block", "LITERAL", "we're not going to do this as a struct build-up right now."]
]);
}

return CFF;

Expand Down
2 changes: 1 addition & 1 deletion src/SFNT/tables/cff/CFFHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ define(["struct"], function(struct) {
}
}

CFFHeader.prototype = new struct([
CFFHeader.prototype = new struct("CFF header", [
["major", "Card8", "major version"]
, ["minor", "Card8", "minor version"]
, ["length", "Card8", "header length in bytes"]
Expand Down
2 changes: 1 addition & 1 deletion src/SFNT/tables/cff/CharStringIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ define(["INDEX", "dataBuilding"], function(INDEX, dataBuilder) {
this.finalise();
}

CharStringIndex.prototype = new INDEX();
CharStringIndex.prototype = Object.create(INDEX.prototype);

return CharStringIndex;

Expand Down
2 changes: 1 addition & 1 deletion src/SFNT/tables/cff/Charset.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ define(["struct", "dataBuilding"], function(struct, dataBuilder) {
}
};

Charset.prototype = new struct([
Charset.prototype = new struct("CFF charset", [
["format", "BYTE", ""]
, ["glyphs", "LITERAL", "actually a USHORT[]."]
]);
Expand Down
10 changes: 5 additions & 5 deletions src/SFNT/tables/cff/DICT.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
define(["struct", "dataBuilding"], function(struct, dataBuilder) {
"use strict";

var dictionaryStructure = dataBuilder.encoder.types.map(function(record) {
return [record, "CFF." + record, record];
});

var DICT = function(input) {
if(!this.parse(input)) {
input = input || {};
Expand All @@ -10,11 +14,7 @@ define(["struct", "dataBuilding"], function(struct, dataBuilder) {
}
};

var dictionaryStructure = dataBuilder.encoder.types.map(function(record) {
return [record, "CFF." + record, record];
});

DICT.prototype = new struct(dictionaryStructure);
DICT.prototype = new struct("CFF DICT", dictionaryStructure);

DICT.prototype.finalise = function() {
this.use(this.usedFields);
Expand Down
2 changes: 1 addition & 1 deletion src/SFNT/tables/cff/Encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ define(["struct", "dataBuilding"], function(struct, dataBuilder) {
}
};

Encoding.prototype = new struct([
Encoding.prototype = new struct("CFF Encoding", [
["format", "BYTE", "encoding format"]
, ["nCodes", "BYTE", "..."]
, ["codes", "LITERAL", ""]
Expand Down
32 changes: 28 additions & 4 deletions src/SFNT/tables/cff/INDEX.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ define(["struct", "dataBuilding"], function(struct, dataBuilder) {
}
}

INDEX.prototype = new struct([
INDEX.prototype = new struct("CFF INDEX", [
["count", "Card16", "number of stored items"]
, ["offSize", "OffSize", "how many bytes do offset values use in this index"]
, ["offset", "LITERAL", "depending on offSize, this is actually BYTE[], USHORT[], UINT24[] or ULONG[]. Note that offsets are relative to the byte *before* the data block, so the first offset is (almost always) 1, not 0."]
Expand All @@ -19,13 +19,37 @@ define(["struct", "dataBuilding"], function(struct, dataBuilder) {

INDEX.prototype.addItem = function(item) {
this.items.push(item);
this.count++;
this.finalise();
};

INDEX.prototype.toJSON = function() {
if(this.count === 0) {
return { count: 0 };
}
return struct.prototype.toJSON.call(this);
};

INDEX.prototype.toData = function() {
if(this.count === 0) {
return [0,0];
}
return struct.prototype.toData.call(this);
};

INDEX.prototype.sizeOf = function(fieldName) {
if(this.count === 0) {
return 2;
}
return struct.prototype.sizeOf.call(this, fieldName);
};

INDEX.prototype.finalise = function() {
var self = this,
count = this.items.length;
this.count = count;
var self = this;

if(this.count === 0) {
return;
}

var data = [];
this.items.forEach(function(item) {
Expand Down
2 changes: 1 addition & 1 deletion src/SFNT/tables/cff/NameIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ define(["INDEX", "dataBuilding"], function(INDEX, dataBuilder) {
});
}

NameIndex.prototype = new INDEX();
NameIndex.prototype = Object.create(INDEX.prototype);

return NameIndex;

Expand Down
4 changes: 2 additions & 2 deletions src/SFNT/tables/cff/PrivateDict.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ define(["DICT"], function(DICT) {
"use strict";

var PrivateDict = function(input) {
DICT.call(this, input);
DICT.call(this, input);
}

PrivateDict.prototype = new DICT();
PrivateDict.prototype = Object.create(DICT.prototype);

return PrivateDict;

Expand Down
2 changes: 1 addition & 1 deletion src/SFNT/tables/cff/StringIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ define(["INDEX", "dataBuilding"], function(INDEX, dataBuilder) {
this.strings = names;
}

StringIndex.prototype = new INDEX();
StringIndex.prototype = Object.create(INDEX.prototype);

// there are 390 predefined strings in CFF, so custom strings
// start at index 391, rather than index 0!
Expand Down
11 changes: 11 additions & 0 deletions src/SFNT/tables/cff/SubroutineIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
define(["struct", "INDEX"], function(struct, INDEX) {
"use strict";

var SubroutineIndex = function(input) {
INDEX.call(this, input);
};

SubroutineIndex.prototype = Object.create(INDEX.prototype);

return SubroutineIndex;
});
20 changes: 0 additions & 20 deletions src/SFNT/tables/cff/Subroutines.js

This file was deleted.

10 changes: 7 additions & 3 deletions src/SFNT/tables/cff/TopDictIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ define(["INDEX", "DICT"], function(INDEX, DICT) {

var TopDictIndex = function(input) {
INDEX.call(this);
var topdict = new DICT(input);
this.addItem(topdict);
this.topdict = new DICT(input);
this.addItem(this.topdict);
}

TopDictIndex.prototype = new INDEX();
TopDictIndex.prototype = Object.create(INDEX.prototype);

TopDictIndex.prototype.set = function(field, v) {
this.topdict[field] = v;
}

return TopDictIndex;

Expand Down
1 change: 1 addition & 0 deletions src/SFNT/tables/common/LangSysTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ define(["struct", "makeStructy", "dataBuilding"], function(struct, makeStructy,
this.features.forEach(function(_,i) {
data = data.concat(dataBuilder.encoder.USHORT(i));
});
console.log("data for FeatureIndex: ", data);
this.FeatureIndex = data;
};

Expand Down
10 changes: 5 additions & 5 deletions src/SFNT/tables/common/ScriptTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ define(["struct", "makeStructy"], function(struct, makeStructy) {
]);

ScriptTable.prototype.finalise = function(lookups) {
this.LangSysCount = this.langsystables - 1; // offset for DFLT
var langsystables = []
this.langsystables.forEach(function(v){
this.LangSysCount = this.langsystables.length - 1; // offset for DFLT
var data = [];
this.langsystables.forEach(function(v, idx){
v.finalise();
langsystables.push(v);
data.push(v);
});
this.LangSysTables = makeStructy(langsystables);
this.LangSysTables = makeStructy(data);
};

return ScriptTable;
Expand Down
2 changes: 1 addition & 1 deletion src/require.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
extend("SFNT/tables/cmaps/format4", ["Segment", "Segments"]);
extend("SFNT/tables/hmtx", ["LongHorMetric"]);
extend("SFNT/tables/name", ["NameRecord", "NameRecords", "StringRecord"]);
extend("SFNT/tables/cff", ["INDEX", "DICT", "CFFHeader", "NameIndex", "StringIndex", "TopDictIndex", "Subroutines", "Charset", "Encoding", "CharStringIndex", "PrivateDict"]);
extend("SFNT/tables/cff", ["INDEX", "DICT", "CFFHeader", "NameIndex", "StringIndex", "TopDictIndex", "SubroutineIndex", "Charset", "Encoding", "CharStringIndex", "PrivateDict"]);

// Set up require.js for this project
var config = {
Expand Down
Loading

0 comments on commit 6f3bf4c

Please sign in to comment.