-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-woff2.js
60 lines (51 loc) · 1.76 KB
/
get-woff2.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
54
55
56
57
58
59
60
import {Readable} from 'node:stream';
import fs from 'node:fs/promises';
import path from 'node:path';
import SVGIcons2SVGFontStream from 'svgicons2svgfont';
import pMap from 'p-map';
import svg2ttf from 'svg2ttf';
import wawoff2 from 'wawoff2';
async function getGlyphsData(files, getUnicode) {
const glyphsData = await pMap(files, async sourcePath => {
const name = path.basename(sourcePath, '.svg');
return {
contents: await fs.readFile(sourcePath, 'utf8'),
sourcePath,
metadata: {
path: sourcePath,
name,
unicode: getUnicode(name, sourcePath),
renamed: false
}
};
}, {concurrency: 100});
return glyphsData;
}
function toSvg(glyphsData, fontName) {
let result = '';
return new Promise((resolve, reject) => {
const fontStream = new SVGIcons2SVGFontStream({
fontName,
log() {}
})
.on('finish', () => resolve(result))
.on('data', data => {
result += data;
})
.on('error', error => reject(error));
for (const glyphData of glyphsData) {
const glyphStream = new Readable();
glyphStream.push(glyphData.contents);
glyphStream.push(null);
glyphStream.metadata = glyphData.metadata;
fontStream.write(glyphStream);
}
fontStream.end();
});
}
export default async function getWoff2(files, fontName, getUnicode) {
const glyphsData = await getGlyphsData(files, getUnicode);
const svg = await toSvg(glyphsData, fontName);
const ttf = Buffer.from(svg2ttf(svg, {copyright: null, ts: null, version: null}).buffer);
return wawoff2.compress(ttf);
}