diff --git a/Makefile b/Makefile index fcba36ac..240577f1 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/base.js b/src/base.js index e224180d..c437c237 100644 --- a/src/base.js +++ b/src/base.js @@ -1,5 +1,5 @@ import r from 'restructure'; -const fs = require('fs'); +// const fs = require('fs'); var fontkit = {}; export default fontkit; @@ -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++) { diff --git a/src/opentype/shapers/ArabicShaper.js b/src/opentype/shapers/ArabicShaper.js index 2ccbbd29..ed50d32c 100644 --- a/src/opentype/shapers/ArabicShaper.js +++ b/src/opentype/shapers/ArabicShaper.js @@ -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 = { diff --git a/src/opentype/shapers/IndicShaper.js b/src/opentype/shapers/IndicShaper.js index 53a57148..bce32db9 100644 --- a/src/opentype/shapers/IndicShaper.js +++ b/src/opentype/shapers/IndicShaper.js @@ -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); /** diff --git a/src/opentype/shapers/UniversalShaper.js b/src/opentype/shapers/UniversalShaper.js index bd8679a2..3246fa2e 100644 --- a/src/opentype/shapers/UniversalShaper.js +++ b/src/opentype/shapers/UniversalShaper.js @@ -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); /** diff --git a/src/opentype/shapers/gen-indic.js b/src/opentype/shapers/gen-indic.js index 805fb85b..1fd0b761 100644 --- a/src/opentype/shapers/gen-indic.js +++ b/src/opentype/shapers/gen-indic.js @@ -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)); diff --git a/src/opentype/shapers/gen-use.js b/src/opentype/shapers/gen-use.js index b7cb944e..57dd077f 100644 --- a/src/opentype/shapers/gen-use.js +++ b/src/opentype/shapers/gen-use.js @@ -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({ diff --git a/src/opentype/shapers/generate-data.js b/src/opentype/shapers/generate-data.js index f07dcd2f..3aaa615d 100644 --- a/src/opentype/shapers/generate-data.js +++ b/src/opentype/shapers/generate-data.js @@ -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());