-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (60 loc) · 1.66 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const svg2ttf = require('svg2ttf');
const SVGIcons2SVGFontStream = require('svgicons2svgfont');
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const fontName = process.argv[2];
const fontStream = new SVGIcons2SVGFontStream({
fontHeight: 1024,
fontName,
normalize: true,
});
let css = `@font-face {
font-family: "${fontName}";
src: url("${fontName}.ttf") format("truetype");
}
[class^="icon-"], [class*=" icon-"] {
font-family: "${fontName}" !important;
font-style: normal;
font-variant: normal;
font-weight: normal;
line-height: 1;
text-transform: none;
speak: none;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
}
`;
function canonicalize(name) {
return name.replace(/[^A-Za-z0-9]+/, '-').toLowerCase();
}
fontStream.pipe(fs.createWriteStream(`${fontName}.svg`))
.on('finish', async () => {
const svg = await readFile(`${fontName}.svg`, 'utf8');
await writeFile(`${fontName}.ttf`, svg2ttf(svg, {}).buffer);
await writeFile(`${fontName}.css`, css);
console.log('TTF created');
})
.on('error', (err) => console.log(err));
process.argv.slice(3).forEach((filename) => {
const m = path.basename(filename).match(/^[Uu]([0-9A-Fa-f]+?)-(.+?)\.svg$/);
if (m) {
const glyph = fs.createReadStream(filename);
glyph.metadata = {
unicode: [
String.fromCharCode(parseInt(m[1], 16))
],
name: m[2]
};
fontStream.write(glyph);
css += `
.icon-${canonicalize(m[2])}:before {
content: "\\${m[1]}";
}
`;
}
});
fontStream.end();