forked from kiliman/operator-mono-lig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract.js
136 lines (106 loc) · 3.8 KB
/
extract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const xpath = require('xpath');
const { DOMParser, XMLSerializer } = require('xmldom');
const format = require('xml-formatter');
let fontName;
const regEx = /\.liga$/;
const regExBlankLines = /^(?=\n)$|^\s*/gm;
const regExWhitespace = /^\s+$/;
const NodeType = {};
NodeType.TEXT_NODE = 3;
async function main() {
fontName = process.argv[2];
const srcFileName = `./ligature_source/${fontName}.ttx`;
console.log(`Reading file ${srcFileName}`);
const xml = await fs.readFileAsync(srcFileName, 'utf-8');
const dom = new DOMParser().parseFromString(xml);
await extractAndWrite('glyphs', extractGlyphs, dom);
await extractAndWrite('cmap', extractCmap, dom);
await extractAndWrite('classdef', extractClassDef, dom);
await extractAndWrite('hmtx', extractHmtx, dom);
await extractAndWrite('lookup', extractLookup, dom);
await extractAndWrite('charstrings', extractCharStrings, dom);
console.log('Done');
}
async function extractAndWrite(name, func, dom) {
const newDom = func(dom);
console.log('Finished extracting ' + name);
const fileName = `./ligature/${fontName}_${name}.xml`;
await fs.writeFileAsync(fileName, format(serialize(newDom)));
console.log('Finished writing ' + name);
}
const serialize = dom =>
new XMLSerializer().serializeToString(dom, false, node => {
if (node.nodeType === NodeType.TEXT_NODE) {
if (regExWhitespace.test(node.data)) return null;
const data = node.data
.split('\r\n')
.filter(s => /\S+/.test(s))
.map(s => s.replace(/^\s+/g, ''))
.join('\n');
return node.ownerDocument.createTextNode(data);
}
return node;
});
const extractGlyphs = dom => {
const newDom = new DOMParser().parseFromString('<GlyphOrder/>');
const glyphIds = xpath.select('/ttFont/GlyphOrder/GlyphID', dom);
glyphIds
.filter(node => regEx.test(node.getAttribute('name')))
.forEach(node => newDom.documentElement.appendChild(node));
return newDom;
};
const extractCmap = dom => {
const newDom = new DOMParser().parseFromString('<cmap/>');
const cmap = xpath.select('/ttFont/cmap/cmap_format_4', dom);
cmap.forEach(node => {
// for each cmap_format_4 node, need to copy map elements
// create and append cmap_format_4 node
const cmap4 = node.cloneNode(false);
newDom.documentElement.appendChild(cmap4);
xpath
.select('map', node)
.filter(child => regEx.test(child.getAttribute('name')))
.forEach(child => cmap4.appendChild(child));
});
return newDom;
};
const extractClassDef = dom => {
const newDom = new DOMParser().parseFromString('<GlyphClassDef/>');
const classDefs = xpath.select('/ttFont/GDEF/GlyphClassDef/ClassDef', dom);
classDefs
.filter(node => regEx.test(node.getAttribute('glyph')))
.forEach(node => newDom.documentElement.appendChild(node));
return newDom;
};
const extractHmtx = dom => {
const newDom = new DOMParser().parseFromString('<hmtx/>');
const mtx = xpath.select('/ttFont/hmtx/mtx', dom);
mtx
.filter(node => regEx.test(node.getAttribute('name')))
.forEach(node => newDom.documentElement.appendChild(node));
return newDom;
};
const extractLookup = dom => {
const newDom = new DOMParser().parseFromString('<LookupList/>');
const lookup = xpath.select(
'/ttFont/GSUB/LookupList/Lookup[@index="20"]',
dom,
true
);
newDom.documentElement.appendChild(lookup);
return newDom;
};
const extractCharStrings = dom => {
const newDom = new DOMParser().parseFromString('<CharStrings/>');
const charStrings = xpath.select(
'/ttFont/CFF/CFFFont/CharStrings/CharString',
dom
);
charStrings
.filter(node => regEx.test(node.getAttribute('name')))
.forEach(node => newDom.documentElement.appendChild(node));
return newDom;
};
main();