forked from kiliman/operator-mono-lig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsub.js
313 lines (273 loc) · 8.46 KB
/
gsub.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
const xpath = require('xpath');
const NodeType = {};
NodeType.TEXT_NODE = 3;
let dom;
let gsubDom;
let scriptListDom;
let featureListDom;
let lookupListDom;
let featureDom;
let lookupIndex = 0;
let chainIndex = 0;
const substLookupMap = {};
const initGsubTables = (_dom, options) => {
dom = _dom;
gsubDom = xpath.select('ttFont/GSUB', dom, true);
// look for 'calt' feature
featureListDom = xpath.select('FeatureList', gsubDom, true);
lookupListDom = xpath.select('LookupList', gsubDom, true);
if (options.italicsHack) {
// remove all features from FeatureList
Array.from(featureListDom.childNodes).forEach((c) => {
featureListDom.removeChild(c);
});
Array.from(xpath.select('ttFont/GSUB//FeatureIndex', dom)).forEach((c) => {
c.parentNode.removeChild(c);
});
// remove all lookups from LookupList
Array.from(lookupListDom.childNodes).forEach((c) =>
lookupListDom.removeChild(c)
);
}
};
const buildGsubTables = (_dom, ligature, options) => {
dom = _dom;
if (/_/.test(ligature.glyph) === false) {
// not a ligature, so see if glyph exists in font (as substitute)
const glyphNode = xpath.select(
`ttFont/GlyphOrder/GlyphID[@name="${ligature.name}"]`,
dom,
true
);
if (!glyphNode) {
return;
}
}
const glyphs = ligature.name.replace(/\.liga$/, '').split('_');
const isSingleGlyph = glyphs.length == 1;
gsubDom = xpath.select('ttFont/GSUB', dom, true);
// look for 'calt' feature
featureListDom = xpath.select('FeatureList', gsubDom, true);
let featureRecord;
const featureTag = xpath.select(
'FeatureRecord/FeatureTag[@value="calt"]',
featureListDom,
true
);
if (!featureTag) {
const featureListCount = xpath.select(
'count(FeatureRecord)',
featureListDom,
true
);
featureRecord = createElementWithAttributes('FeatureRecord', {
index: featureListCount,
});
featureRecord.appendChild(
createElementWithAttributes('FeatureTag', { value: 'calt' })
);
featureRecord.appendChild(dom.createElement('Feature'));
featureListDom.appendChild(featureRecord);
} else {
featureRecord = featureTag.parentNode;
}
featureDom = xpath.select('Feature', featureRecord, true);
const featureIndex = featureRecord.getAttribute('index');
// add feature to scriptlist for DFLT and latn if not present
addFeatureToScriptList('DFLT', featureIndex);
addFeatureToScriptList('latn', featureIndex);
lookupIndex = xpath.select('count(Lookup)', lookupListDom, true);
const lookupDom = createElementWithAttributes('Lookup', {
index: lookupIndex++,
});
appendChildren(
lookupDom,
createElementWithAttributes('LookupType', { value: isSingleGlyph ? 1 : 6 }),
createElementWithAttributes('LookupFlag', { value: 0 })
);
// add contextual lookup to feature
const featureLookupCount = xpath.select(
'count(LookupListIndex)',
featureDom,
true
);
featureDom.appendChild(
createElementWithAttributes('LookupListIndex', {
index: featureLookupCount,
value: lookupDom.getAttribute('index'),
})
);
if (isSingleGlyph) {
lookupDom.appendChild(
getSubstLookup(ligature.name, ligature.glyph, lookupDom)
);
} else {
lookupDom.appendChild(buildBacktrackFirst(glyphs));
lookupDom.appendChild(buildLookAheadLast(glyphs));
lookupDom.appendChild(buildLigatureSubst(glyphs, ligature.glyph));
// remaining context
for (let n = glyphs.length - 2; n >= 0; n--) {
lookupDom.appendChild(
buildChainContext(getLIGs(n), [glyphs[n]], glyphs.slice(n + 1), 'LIG')
);
}
}
lookupListDom.appendChild(lookupDom);
};
const finalizeGsubTables = () => {
// get current LookupList count
let newIndex = xpath.select('count(Lookup)', lookupListDom, true);
// append added lookups
Object.entries(substLookupMap).forEach(([, lookup]) => {
// fixup existing lookups with new index
const oldIndex = lookup.getAttribute('index');
const substLookups = xpath.select(
`//ChainContextSubst/SubstLookupRecord/LookupListIndex[@value="${oldIndex}"]`,
lookupListDom,
false
);
substLookups.forEach((substLookup) => {
substLookup.setAttribute('value', newIndex);
});
lookup.setAttribute('index', newIndex++);
lookupListDom.appendChild(lookup);
});
};
//const dump = dom => console.log(format(serialize(dom)));
const addFeatureToScriptList = (tag, featureIndex) => {
scriptListDom = xpath.select('ScriptList', gsubDom, true);
const defaultLangSys = xpath.select(
`ScriptRecord/ScriptTag[@value="${tag}"]/../Script/DefaultLangSys`,
scriptListDom,
true
);
const featureIndexNode = xpath.select(
`FeatureIndex[@value=${featureIndex}]`,
defaultLangSys,
true
);
if (!featureIndexNode) {
const count = xpath.select('count(FeatureIndex)', defaultLangSys, true);
defaultLangSys.appendChild(
createElementWithAttributes('FeatureIndex', {
index: count,
value: featureIndex,
})
);
}
};
const buildBacktrackFirst = (glyphs) => {
// backtrack = first glyph
// input = first glphy
// lookAhead = [1..n]
return buildChainContext([glyphs[0]], [glyphs[0]], glyphs.slice(1));
};
const buildLookAheadLast = (glyphs) => {
// backtrack = none
// input = first glyph
// lookAhead = [1..n] + [n]
return buildChainContext(
[],
[glyphs[0]],
[...glyphs.slice(1), glyphs.slice(-1)]
);
};
const buildLigatureSubst = (glyphs, lookup) => {
// backtrack = LIG * n-1
// input = last glyph
// lookAhead = none
return buildChainContext(
getLIGs(glyphs.length - 1),
glyphs.slice(-1),
[],
lookup
);
};
const getLIGs = (length) => new Array(length).fill('LIG', 0, length);
const buildChainContext = (backtrack, input, lookAhead, substitute) => {
const chainDom = createElementWithAttributes('ChainContextSubst', {
index: chainIndex++,
Format: 3,
});
buildCoverage(chainDom, 'BacktrackCoverage', backtrack);
buildCoverage(chainDom, 'InputCoverage', input);
buildCoverage(chainDom, 'LookAheadCoverage', lookAhead);
if (substitute) {
const lookup = getSubstLookup(input[0], substitute);
const listIndex = lookup.getAttribute('index');
const substLookupRecordDom = chainDom.appendChild(
createElementWithAttributes('SubstLookupRecord', { index: 0 })
);
appendChildren(
substLookupRecordDom,
createElementWithAttributes('SequenceIndex', { value: 0 }),
createElementWithAttributes('LookupListIndex', { value: listIndex })
);
}
return chainDom;
};
const buildCoverage = (chainDom, tagName, coverage) => {
if (coverage) {
coverage.forEach((glyph, i) => {
const coverageDom = createElementWithAttributes(tagName, {
index: i++,
Format: 1,
});
coverageDom.appendChild(
createElementWithAttributes('Glyph', { value: glyph })
);
chainDom.appendChild(coverageDom);
});
}
};
const getSubstLookup = (input, output, lookupDom) => {
if (!lookupDom) {
lookupDom = substLookupMap[output];
}
if (!lookupDom) {
lookupDom = createElementWithAttributes('Lookup', {
index: Object.keys(substLookupMap).length + 1,
});
lookupDom.appendChild(
createElementWithAttributes('LookupType', { value: 1 })
);
lookupDom.appendChild(
createElementWithAttributes('LookupFlag', { value: 0 })
);
substLookupMap[output] = lookupDom;
}
let singleSubstDom = xpath.select('SingleSubst', lookupDom, true);
if (!singleSubstDom) {
singleSubstDom = createElementWithAttributes('SingleSubst', {
index: 0,
Format: 2,
});
lookupDom.appendChild(singleSubstDom);
}
// look for input subst
const substitionDom = xpath.select(
`Substitution[@in="${input}"]`,
singleSubstDom,
true
);
if (!substitionDom) {
singleSubstDom.appendChild(
createElementWithAttributes('Substitution', { in: input, out: output })
);
}
return lookupDom;
};
const createElementWithAttributes = (tagName, attributes) => {
const element = dom.createElement(tagName);
Object.entries(attributes).forEach((attribute) => {
element.setAttribute(attribute[0], attribute[1]);
});
return element;
};
const appendChildren = (node, ...children) => {
children.forEach((child) => node.appendChild(child));
};
//const serialize = dom => new XMLSerializer().serializeToString(dom);
exports.initGsubTables = initGsubTables;
exports.buildGsubTables = buildGsubTables;
exports.finalizeGsubTables = finalizeGsubTables;