UNPKG

2.84 kBJavaScriptView Raw
1// Source https://gist.github.com/romanlv/0b5b1d5d942b01da9e7bd46f07d42584
2const fs = require('fs');
3const { promisify } = require('util');
4const glob = require('glob');
5const xml2js = require('xml2js');
6
7const SVGIcons2SVGFontStream = require('svgicons2svgfont');
8const SVGIconsDirStream = require('svgicons2svgfont/src/iconsdir');
9
10const svg2ttf = require('svg2ttf');
11
12const readFileAsync = promisify(fs.readFile);
13const writeFileAsync = promisify(fs.writeFile);
14
15function makeSvgFont(fontName, svgs, svgFontPath) {
16 const files = glob.sync(svgs);
17 const options = {
18 // see list of all the options
19 // https://github.com/nfroidure/svgicons2svgfont#cli-interface
20 // https://github.com/nfroidure/svgicons2svgfont/blob/master/bin/svgicons2svgfont.js#L76
21 fontHeight: 1000,
22 normalize: true,
23 };
24
25 return new Promise((resolve, reject) => {
26 new SVGIconsDirStream(files, {})
27 .pipe(
28 new SVGIcons2SVGFontStream({
29 ...options,
30 fontName,
31 })
32 )
33 .pipe(fs.createWriteStream(svgFontPath))
34 .on('finish', resolve)
35 .on('error', reject);
36 });
37}
38
39async function convertSvg2Ttf(svgFontPath, output) {
40 const ttf = svg2ttf(await readFileAsync(svgFontPath, 'utf8'), {});
41 await writeFileAsync(output, Buffer.from(ttf.buffer));
42}
43
44async function generateGlyphMap(svgFontPath, output) {
45 const parser = new xml2js.Parser();
46 const glyphMap = {};
47 const data = await readFileAsync(svgFontPath);
48
49 return new Promise((resolve, reject) => {
50 parser.parseString(data, function(err, result) {
51 if (err !== null) {
52 reject(err);
53 }
54 if (!result) {
55 console.error(`cannot parse ${svgFontPath}`);
56 }
57
58 const icons = result.svg.defs[0].font[0].glyph;
59
60 icons.forEach(({ $: icon }) => {
61 const name = icon['glyph-name'];
62 const code = icon.unicode.charCodeAt(0);
63 glyphMap[name] = code;
64 });
65
66 fs.writeFileSync(output, JSON.stringify(glyphMap, null, 2));
67
68 resolve(glyphMap);
69 });
70 });
71}
72
73async function main() {
74 const fontName = 'Ionicons';
75
76 // this file is temporary
77 const svgFontPath = `./${fontName}.svg`;
78 const glyphMapPath = `./glyphmaps/${fontName}.json`;
79 const tffPath = `./Fonts/${fontName}.ttf`;
80
81 // create svg font from svg icons, it will use `svgicons2svgfont` to convert
82 // `rect', 'circle` etc... to `path`s that can be used for font generation
83 await makeSvgFont(fontName, './Ioniconstmp/*.svg', svgFontPath);
84
85 await Promise.all([
86 // create json file with map of icon name and character code in font, needed for `react-native-vector-icons` integration
87 generateGlyphMap(svgFontPath, glyphMapPath),
88 // convert svg font to ttf font
89 convertSvg2Ttf(svgFontPath, tffPath),
90 ]);
91 console.log(`updated: ${tffPath} and ${glyphMapPath}`);
92}
93
94main();