-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-glyphs.js
53 lines (48 loc) · 1.61 KB
/
build-glyphs.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import fontnik from 'fontnik'
import { queue } from 'd3-queue'
import os from 'os'
import { existsSync, writeFileSync, readdirSync, readFileSync, mkdirSync } from 'node:fs'
const fontDir = 'fonts'
const outDir = 'glyphs'
const licenseFileNames = ['OFL.txt' || 'LICENSE']
function writeGlyphs(opts, done) {
fontnik.range(opts, (err, zdata) => {
if (err) {
console.warn(err.toString())
process.exit(1)
}
writeFileSync(`${opts.outDir}/${opts.start}-${opts.end}.pbf`, zdata)
done()
})
}
if (!existsSync(outDir)) {
console.warn('Error: Directory %s does not exist', outDir)
process.exit(1)
}
const q = queue(Math.max(4, os.cpus().length))
const topFolders = readdirSync(fontDir)
for(const topFolder of topFolders) {
const fonts = readdirSync(`${fontDir}/${topFolder}/`)
const license = fonts.find(font => licenseFileNames.includes(font))
const licenseBuffer = readFileSync(`${fontDir}/${topFolder}/${license}`)
for(const font of fonts) {
if (font.match(/[^.]+$/)[0] === 'ttf') {
const fontBuffer = readFileSync(`${fontDir}/${topFolder}/${font}`)
fontnik.load(fontBuffer, (err, faces) => {
const filePath = `${outDir}/${faces[0].family_name} ${faces[0].style_name}`
if (!existsSync(filePath)){
mkdirSync(filePath, { recursive: true })
}
writeFileSync(`${filePath}/LICENSE`, licenseBuffer)
for (let i = 0; i < 65536; (i = i + 256)) {
q.defer(writeGlyphs, {
outDir: filePath,
font: fontBuffer,
start: i,
end: Math.min(i + 255, 65535)
})
}
})
}
}
}