fimfic2epub/src/subsetFont.js

37 lines
866 B
JavaScript
Raw Permalink Normal View History

2018-03-13 10:07:31 +13:00
import isNode from 'detect-node'
import { Font } from 'fonteditor-core'
import fs from 'fs'
import fetch from './fetch'
2020-01-16 23:09:13 +13:00
import FileType from 'file-type'
2018-03-13 10:07:31 +13:00
async function subsetFont (fontPath, glyphs, options = {}) {
let data
2019-10-08 22:31:42 +13:00
const fontdata = Buffer.from(fontPath, 'binary')
2020-01-16 23:09:13 +13:00
const type = await FileType.fromBuffer(fontdata)
2018-03-15 06:04:50 +13:00
if (type && type.mime === 'font/ttf') {
2018-03-15 09:02:15 +13:00
data = fontdata.buffer
2018-03-13 10:07:31 +13:00
} else {
2018-03-15 06:04:50 +13:00
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)
})
2018-03-13 10:07:31 +13:00
})
}
2018-03-13 10:07:31 +13:00
}
return Font.create(data, {
type: 'ttf',
subset: glyphs,
hinting: true
}).write({
type: 'ttf',
hinting: true
})
}
export default subsetFont