提取 ttf 字体连字映射
Extracting ttf font ligature mappings
我有一个现有的 ttf font,我希望将所有连字映射提取到此形式中:
{
"calendar_today": "E935",
"calendar_view_day": "E936",
...
}
我在这个脚本中使用 fontkit:
const fontkit = require('fontkit');
let font = fontkit.openSync('./MaterialIcons-Regular.ttf');
let lookupList = font.GSUB.lookupList.toArray();
let lookupListIndexes = font.GSUB.featureList[0].feature.lookupListIndexes;
lookupListIndexes.forEach(index => {
let subTable = lookupList[index].subTables[0];
let ligatureSets = subTable.ligatureSets.toArray();
ligatureSets.forEach(ligatureSet => {
ligatureSet.forEach(ligature => {
let character = font.stringsForGlyph(ligature.glyph)[0];
let characterCode = character.charCodeAt(0).toString(16).toUpperCase();
let ligatureText = ligature
.components
.map(x => font.stringsForGlyph(x)[0])
.join('');
console.log(`${ligatureText} -> ${characterCode}`);
});
});
});
但是,我没有得到完整的连字名称。输出:
...
alendar_today -> E935
rop_portrait -> E3C5
ontact_phone -> E0CF
ontrol_point -> E3BA
hevron_right -> E5CC
...
我做错了什么?通过FontForge的分析判断,字体的连字名称没有遗漏任何字符。
注意到here,第一个字符是根据覆盖范围记录计算的。
首先,计算前导字符
let leadingCharacters = [];
subTable.coverage.rangeRecords.forEach((coverage) => {
for (let i = coverage.start; i <= coverage.end; i++) {
let character = font.stringsForGlyph(i)[0];
leadingCharacters.push(character);
}
});
然后,通过子表的索引访问这些字符
let ligatureSets = subTable.ligatureSets.toArray();
ligatureSets.forEach((ligatureSet, ligatureSetIndex) => {
let leadingCharacter = leadingCharacters[ligatureSetIndex];
ligatureSet.forEach(ligature => {
let character = font.stringsForGlyph(ligature.glyph)[0];
let characterCode = character.charCodeAt(0).toString(16).toUpperCase();
let ligatureText = ligature
.components
.map(x => font.stringsForGlyph(x)[0])
.join('');
ligatureText = leadingCharacter + ligatureText;
console.log(`${ligatureText} -> ${characterCode}`);
});
});
我有一个现有的 ttf font,我希望将所有连字映射提取到此形式中:
{
"calendar_today": "E935",
"calendar_view_day": "E936",
...
}
我在这个脚本中使用 fontkit:
const fontkit = require('fontkit');
let font = fontkit.openSync('./MaterialIcons-Regular.ttf');
let lookupList = font.GSUB.lookupList.toArray();
let lookupListIndexes = font.GSUB.featureList[0].feature.lookupListIndexes;
lookupListIndexes.forEach(index => {
let subTable = lookupList[index].subTables[0];
let ligatureSets = subTable.ligatureSets.toArray();
ligatureSets.forEach(ligatureSet => {
ligatureSet.forEach(ligature => {
let character = font.stringsForGlyph(ligature.glyph)[0];
let characterCode = character.charCodeAt(0).toString(16).toUpperCase();
let ligatureText = ligature
.components
.map(x => font.stringsForGlyph(x)[0])
.join('');
console.log(`${ligatureText} -> ${characterCode}`);
});
});
});
但是,我没有得到完整的连字名称。输出:
...
alendar_today -> E935
rop_portrait -> E3C5
ontact_phone -> E0CF
ontrol_point -> E3BA
hevron_right -> E5CC
...
我做错了什么?通过FontForge的分析判断,字体的连字名称没有遗漏任何字符。
注意到here,第一个字符是根据覆盖范围记录计算的。
首先,计算前导字符
let leadingCharacters = [];
subTable.coverage.rangeRecords.forEach((coverage) => {
for (let i = coverage.start; i <= coverage.end; i++) {
let character = font.stringsForGlyph(i)[0];
leadingCharacters.push(character);
}
});
然后,通过子表的索引访问这些字符
let ligatureSets = subTable.ligatureSets.toArray();
ligatureSets.forEach((ligatureSet, ligatureSetIndex) => {
let leadingCharacter = leadingCharacters[ligatureSetIndex];
ligatureSet.forEach(ligature => {
let character = font.stringsForGlyph(ligature.glyph)[0];
let characterCode = character.charCodeAt(0).toString(16).toUpperCase();
let ligatureText = ligature
.components
.map(x => font.stringsForGlyph(x)[0])
.join('');
ligatureText = leadingCharacter + ligatureText;
console.log(`${ligatureText} -> ${characterCode}`);
});
});