Skip to content

Commit

Permalink
Serialize trie files as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopding committed Jan 15, 2018
1 parent e2ff84e commit 968e35c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 44 deletions.
24 changes: 12 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ PATH := ./node_modules/.bin:$(PATH)

all: index.js base.js

src/opentype/shapers/data.trie:
src/opentype/shapers/trie.json:
babel-node src/opentype/shapers/generate-data.js

src/opentype/shapers/use.trie:
src/opentype/shapers/trieUse.json:
babel-node src/opentype/shapers/gen-use.js

src/opentype/shapers/indic.trie:
src/opentype/shapers/trieIndic.json:
babel-node src/opentype/shapers/gen-indic.js

data.trie: src/opentype/shapers/data.trie
cp src/opentype/shapers/data.trie data.trie
trie.json: src/opentype/shapers/trie.json
cp src/opentype/shapers/trie.json trie.json

use.trie: src/opentype/shapers/use.trie
cp src/opentype/shapers/use.trie use.trie
trieUse.json: src/opentype/shapers/trieUse.json
cp src/opentype/shapers/trieUse.json trieUse.json

indic.trie: src/opentype/shapers/indic.trie
cp src/opentype/shapers/indic.trie indic.trie
trieIndic.json: src/opentype/shapers/trieIndic.json
cp src/opentype/shapers/trieIndic.json trieIndic.json

index.js: $(SOURCES) data.trie use.trie indic.trie
index.js: $(SOURCES) trie.json trieUse.json trieIndic.json
rollup -c -m -i src/index.js -o index.js

base.js: $(SOURCES) data.trie use.trie indic.trie
base.js: $(SOURCES) trie.json trieUse.json trieIndic.json
rollup -c -m -i src/base.js -o base.js

clean:
rm -f index.js base.js data.trie indic.trie use.trie src/opentype/shapers/data.trie src/opentype/shapers/use.trie src/opentype/shapers/use.json src/opentype/shapers/indic.trie src/opentype/shapers/indic.json
rm -f index.js base.js trie.json trieIndic.json trieUse.json src/opentype/shapers/trie.json src/opentype/shapers/trieUse.json src/opentype/shapers/use.json src/opentype/shapers/trieIndic.json src/opentype/shapers/indic.json
52 changes: 26 additions & 26 deletions src/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import r from 'restructure';
const fs = require('fs');
// const fs = require('fs');

var fontkit = {};
export default fontkit;
Expand All @@ -11,31 +11,31 @@ fontkit.registerFormat = function(format) {
formats.push(format);
};

fontkit.openSync = function(filename, postscriptName) {
let buffer = fs.readFileSync(filename);
return fontkit.create(buffer, postscriptName);
};

fontkit.open = function(filename, postscriptName, callback) {
if (typeof postscriptName === 'function') {
callback = postscriptName;
postscriptName = null;
}

fs.readFile(filename, function(err, buffer) {
if (err) { return callback(err); }

try {
var font = fontkit.create(buffer, postscriptName);
} catch (e) {
return callback(e);
}

return callback(null, font);
});

return;
};
// fontkit.openSync = function(filename, postscriptName) {
// let buffer = fs.readFileSync(filename);
// return fontkit.create(buffer, postscriptName);
// };
//
// fontkit.open = function(filename, postscriptName, callback) {
// if (typeof postscriptName === 'function') {
// callback = postscriptName;
// postscriptName = null;
// }
//
// fs.readFile(filename, function(err, buffer) {
// if (err) { return callback(err); }
//
// try {
// var font = fontkit.create(buffer, postscriptName);
// } catch (e) {
// return callback(e);
// }
//
// return callback(null, font);
// });
//
// return;
// };

fontkit.create = function(buffer, postscriptName) {
for (let i = 0; i < formats.length; i++) {
Expand Down
7 changes: 6 additions & 1 deletion src/opentype/shapers/ArabicShaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import DefaultShaper from './DefaultShaper';
import unicode from 'unicode-properties';
import UnicodeTrie from 'unicode-trie';

const trie = new UnicodeTrie(require('fs').readFileSync(__dirname + '/data.trie'));
// Trie is serialized as a Buffer in node, but here
// we may be running in a browser so we make an Uint8Array
const trieBuffer = require('./trieData.json');
const trieData = new Uint8Array(trieBuffer.data);
const trie = new UnicodeTrie(trieData);
// const trie = new UnicodeTrie(require('fs').readFileSync(__dirname + '/data.trie'));
const FEATURES = ['isol', 'fina', 'fin2', 'fin3', 'medi', 'med2', 'init'];

const ShapingClasses = {
Expand Down
8 changes: 7 additions & 1 deletion src/opentype/shapers/IndicShaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import {
} from './indic-data';

const {decompositions} = useData;
const trie = new UnicodeTrie(require('fs').readFileSync(__dirname + '/indic.trie'));

// Trie is serialized as a Buffer in node, but here
// we may be running in a browser so we make an Uint8Array
const trieBuffer = require('./trieIndic.json');
const trieData = new Uint8Array(trieBuffer.data);
const trie = new UnicodeTrie(trieData);
// const trie = new UnicodeTrie(require('fs').readFileSync(__dirname + '/indic.trie'));
const stateMachine = new StateMachine(indicMachine);

/**
Expand Down
8 changes: 7 additions & 1 deletion src/opentype/shapers/UniversalShaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import GlyphInfo from '../GlyphInfo';
import useData from './use.json';

const {categories, decompositions} = useData;
const trie = new UnicodeTrie(require('fs').readFileSync(__dirname + '/use.trie'));

// Trie is serialized as a Buffer in node, but here
// we may be running in a browser so we make an Uint8Array
const trieBuffer = require('./trieUse.json');
const trieData = new Uint8Array(trieBuffer.data);
const trie = new UnicodeTrie(trieData);
// const trie = new UnicodeTrie(require('fs').readFileSync(__dirname + '/use.trie'));
const stateMachine = new StateMachine(useData);

/**
Expand Down
5 changes: 4 additions & 1 deletion src/opentype/shapers/gen-indic.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ for (let i = 0; i < codepoints.length; i++) {
}
}

fs.writeFileSync(__dirname + '/indic.trie', trie.toBuffer());
// Trie is serialized suboptimally as JSON so it can be loaded via require,
// allowing unicode-properties to work in the browser
fs.writeFileSync(__dirname + '/trieIndic.json', JSON.stringify(trie.toBuffer()));
// fs.writeFileSync(__dirname + '/indic.trie', trie.toBuffer());

let stateMachine = compile(fs.readFileSync(__dirname + '/indic.machine', 'utf8'), symbols);
fs.writeFileSync(__dirname + '/indic.json', JSON.stringify(stateMachine));
5 changes: 4 additions & 1 deletion src/opentype/shapers/gen-use.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ function decompose(code) {
return decomposition;
}

fs.writeFileSync(__dirname + '/use.trie', trie.toBuffer());
// Trie is serialized suboptimally as JSON so it can be loaded via require,
// allowing unicode-properties to work in the browser
fs.writeFileSync(__dirname + '/trieUse.json', JSON.stringify(trie.toBuffer()));
// fs.writeFileSync(__dirname + '/use.trie', trie.toBuffer());

let stateMachine = compile(fs.readFileSync(__dirname + '/use.machine', 'utf8'), symbols);
let json = Object.assign({
Expand Down
5 changes: 4 additions & 1 deletion src/opentype/shapers/generate-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ for (let i = 0; i < codepoints.length; i++) {
}
}

fs.writeFileSync(__dirname + '/data.trie', trie.toBuffer());
// Trie is serialized suboptimally as JSON so it can be loaded via require,
// allowing unicode-properties to work in the browser
fs.writeFileSync(__dirname + '/trie.json', JSON.stringify(trie.toBuffer()));
// fs.writeFileSync(__dirname + '/data.trie', trie.toBuffer());

0 comments on commit 968e35c

Please sign in to comment.