I have an existing ttf font where I wish to extract all Ligature mappings into this form:
{
"calendar_today": "E935",
"calendar_view_day": "E936",
...
}
I'm using fontkit with this script:
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}`);
});
});
});
However, I'm not getting the full Ligature names. output:
...
alendar_today -> E935
rop_portrait -> E3C5
ontact_phone -> E0CF
ontrol_point -> E3BA
hevron_right -> E5CC
...
What am I doing wrong? Judging by the analysis with FontForge, the font's Ligature names are not missing any characters.
As noted here, the first character is calculated according to a coverage range records.
first, calculate the leading characters
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);
}
});
then, access these characters by the index of the subtable
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}`);
});
});