diff --git a/extract.js b/extract.js index d3fd211..3c76941 100644 --- a/extract.js +++ b/extract.js @@ -10,13 +10,13 @@ const hash = require('hash.js'); let fontName; -const regEx = /^LIG$|uni(E0A0|E0B0|E0B2)|\.liga$/; const NodeType = {}; NodeType.TEXT_NODE = 3; function main() { fontName = process.argv[2]; + const glyphsRegEx = getGlyphsRegEx('./ligature_source/glyphs'); const srcFileName = `./ligature_source/${fontName}.ttx`; const folder = `./ligature/${fontName}`; @@ -31,7 +31,7 @@ function main() { extractAndWrite('config', extractConfig, dom); - extractCharStrings(dom); + extractCharStrings(dom, glyphsRegEx); extractAndWrite('subrs', extractSubrs, dom); extractAndWrite('gsubrs', extractGlobalSubrs, dom); @@ -39,6 +39,25 @@ function main() { console.log('Done'); } +function getGlyphsRegEx(path) { + const glyphs = fs.readFileSync(path, 'utf-8'); + const patterns = ['LIG']; + + glyphs + .split(os.EOL) + .filter(line => line.length > 0) + .forEach(line => { + const pattern = line + .replace(/\s*#.*/, '') + .replace(/[.]/g, '\\.') + .replace(/[*]/g, '.*'); + if (pattern.length) { + patterns.push(pattern); + } + }); + return new RegExp(`^(${patterns.join('|')})$`); +} + function extractAndWrite(name, func, dom) { const newDom = func(dom); console.log('Finished extracting ' + name); @@ -86,26 +105,29 @@ const extractFromPath = (path, dom) => { return newDom.documentElement; }; -const extractCharStrings = dom => { - const cmap = xpath.select('/ttfont/cmap/cmap_format_4', dom); +const extractCharStrings = (dom, glyphsRegEx) => { const charStrings = xpath.select( '/ttFont/CFF/CFFFont/CharStrings/CharString', dom ); charStrings - .filter(node => regEx.test(node.getAttribute('name'))) - .forEach(node => writeGlyphData(dom, node, cmap)); + .filter(node => glyphsRegEx.test(node.getAttribute('name'))) + .forEach(node => writeGlyphData(dom, node)); console.log('Finished writing charstrings'); }; -const writeGlyphData = (dom, node, cmap) => { +const writeGlyphData = (dom, node) => { const newDom = new DOMParser().parseFromString('').documentElement; const name = node.getAttribute('name'); newDom.setAttribute('name', name); // get map and set code - const map = xpath.select(`/ttFont/cmap/cmap_format_4/map[@name="${name}"]`, dom, true); + const map = xpath.select( + `/ttFont/cmap/cmap_format_4/map[@name="${name}"]`, + dom, + true + ); if (map) { newDom.setAttribute('code', map.getAttribute('code')); } diff --git a/ligature_source/.gitignore b/ligature_source/.gitignore index d6b7ef3..f4f2f85 100644 --- a/ligature_source/.gitignore +++ b/ligature_source/.gitignore @@ -1,2 +1,2 @@ -* -!.gitignore +*.otf +*.ttx \ No newline at end of file diff --git a/ligature_source/glyphs b/ligature_source/glyphs new file mode 100644 index 0000000..35e9eb4 --- /dev/null +++ b/ligature_source/glyphs @@ -0,0 +1,7 @@ +# List of glyphs to extract +# includes patterns + +*.liga # all ligatures +uniE0A0 # Powerline branch +uniE0B0 # Powerline right triangle +uniE0B2 # Powerline left triangle