const config = require('./config.json'); const fonts = config.fonts; const texts = config.texts; const path = require('path'); const opentype = require('opentype.js'); const glyphs = [...new Set(texts.split(''))].join(''); fonts.forEach(item => { const font = opentype.loadSync(item); const postScriptName = font.getEnglishName('postScriptName'); const dist = path.join( 'dist', postScriptName.replace(/-/g, '_').toLowerCase() + '_subset.otf' ); const [familyName, styleName] = postScriptName.split('-'); const notdefGlyph = font.glyphs.get(0); notdefGlyph.name = '.notdef'; subGlyphs = [notdefGlyph].concat(font.stringToGlyphs(glyphs)); const subsetFont = new opentype.Font({ familyName: familyName, styleName: styleName, unitsPerEm: font.unitsPerEm, ascender: font.ascender, descender: font.descender, designer: font.getEnglishName('designer'), designerURL: font.getEnglishName('designerURL'), manufacturer: font.getEnglishName('manufacturer'), manufacturerURL: font.getEnglishName('manufacturerURL'), license: font.getEnglishName('license'), licenseURL: font.getEnglishName('licenseURL'), version: font.getEnglishName('version'), description: font.getEnglishName('description'), copyright: 'This is a subset font of ' + postScriptName + '. ' + font.getEnglishName('copyright'), trademark: font.getEnglishName('trademark'), glyphs: subGlyphs }); subsetFont.download(dist); });