-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (32 loc) · 1.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const fs = require('fs')
const xml = require('xml-js')
const svg2ttf = require('svg2ttf')
const ttf2woff = require('ttf2woff')
const wawoff2 = require('wawoff2')
class SvgAwesome {
constructor(configObject) {
configObject.types.forEach(type => {
// Entry file
let entry = fs.readFileSync(configObject.entryDir + type.filename + '.svg', 'utf8')
entry = entry.replace(/&(?!(?:apos|quot|[gl]t|amp);|#)/g, '&')
// Map JSON to keep only selected glyphs
let json = xml.xml2js(entry , { compact: true, spaces: 4 })
json.svg.defs.font.glyph = json.svg.defs.font.glyph.filter(key => type.keep.includes(key['_attributes']['glyph-name']))
// Check for output folder existence
if (!fs.existsSync(configObject.outputDir)) {
fs.mkdirSync(configObject.outputDir)
}
// Save TTF file
let svg = xml.js2xml(json, { compact: true, spaces: 4 })
fs.writeFileSync(configObject.outputDir + type.filename + '.ttf', new Buffer(svg2ttf(svg).buffer))
// Save WOFF file
let ttf = fs.readFileSync(configObject.outputDir + type.filename + '.ttf')
fs.writeFileSync(configObject.outputDir + type.filename + '.woff', new Buffer(ttf2woff(ttf).buffer))
// Save WOFF2 file
wawoff2.compress(ttf).then(output => {
fs.writeFileSync(configObject.outputDir + type.filename + '.woff2', output)
})
})
}
}
module.exports = SvgAwesome