font subset function

This commit is contained in:
daniel-j 2018-03-12 22:07:31 +01:00
parent dfd6e9897e
commit 5836ecb136

29
src/subsetFont.js Normal file
View file

@ -0,0 +1,29 @@
import isNode from 'detect-node'
import { Font } from 'fonteditor-core'
import fs from 'fs'
import fetch from './fetch'
async function subsetFont (fontPath, glyphs, options = {}) {
let data
if (!isNode || !options.local) {
data = await fetch(fontPath, 'arraybuffer')
} else {
data = await new Promise((resolve, reject) => {
fs.readFile(fontPath, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
}
return Font.create(data, {
type: 'ttf',
subset: glyphs,
hinting: true
}).write({
type: 'ttf',
hinting: true
})
}
export default subsetFont