简体   繁体   English

提取ttf字体连字映射

[英]Extracting ttf font ligature mappings

I have an existing ttf font where I wish to extract all Ligature mappings into this form: 我有一个现有的ttf字体 ,我希望将所有Ligature映射提取到这个表单中:

{
    "calendar_today": "E935",
    "calendar_view_day": "E936",
    ...
}

I'm using fontkit with this script: 我在这个脚本中使用了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}`);
        });
    });
});

However, I'm not getting the full Ligature names. 但是,我没有获得完整的Ligature名称。 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. 通过使用FontForge进行分析判断,字体的Ligature名称不会丢失任何字符。

图片

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}`);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM