forked from omacranger/fontawesome-subset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (77 loc) · 3.22 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Author: Logan Graham <loganparkergraham@gmail.com>
*/
const
fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp'),
svg2ttf = require('svg2ttf'),
ttf2eot = require('ttf2eot'),
ttf2woff = require('ttf2woff'),
ttf2woff2 = require('ttf2woff2');
/**
* This function will take an object of glyph names and output a subset of the standard fonts optimized in size for
* use on websites / external resources.
*
* @param subset Array or Object containing glyph / font family names.
* @param output_dir Directory output generated webfonts.
* @param options Object of options / tweaks for further customization.
*/
function fontawesomeSubset(subset, output_dir, options){
// Maps style to actual font name / file name.
const font_map = {
solid: 'fa-solid-900',
light: 'fa-light-300',
regular: 'fa-regular-400',
brands: 'fa-brands-400'
};
let opts = {
...{
package: 'free',
}, ...options
};
// If 'subset' is set to array, turn into object defaulted for 'solid' use (fontawesome free)
if (Array.isArray(subset)) {
subset = {'solid': subset};
}
for (let [font_family, value] of Object.entries(subset)) {
// Skip if invalid font family
let svg_file_path = `node_modules/@fortawesome/fontawesome-${opts.package}/webfonts/${font_map[font_family]}.svg`;
// Skip if current font family is not found in font_map.
if (Object.keys(font_map).indexOf(font_family) === -1) {
continue;
}
if (!fs.existsSync(svg_file_path)) {
console.error("Missing fontawesome dependencies. Try running `npm install` or changing the font style.");
continue;
}
let svg_file = fs.readFileSync(svg_file_path).toString(),
glyphs_to_remove = ((svg_file) => {
let glyphs = [],
matcher = new RegExp('<glyph glyph-name="([^"]+)"', 'gms'),
current_match;
while (current_match = matcher.exec(svg_file)) {
if (value.indexOf(current_match[1]) === -1) {
glyphs.push(current_match[1]);
}
}
return glyphs;
})(svg_file),
svg_contents_new = svg_file.replace(new RegExp(`(<glyph glyph-name="(${glyphs_to_remove.join('|')})".*?\\/>)`, 'gms'), '').replace(/>\s+</gms, '><'),
ttf_utils = svg2ttf(svg_contents_new, {}),
ttf = Buffer.from(ttf_utils.buffer);
mkdirp.sync(path.resolve(output_dir), (err) => {
if (err) {
console.error(err);
console.error("Unable to create directory for output files.");
}
});
let output_file = path.resolve(output_dir, font_map[font_family]);
fs.writeFileSync(`${output_file}.svg`, svg_contents_new);
fs.writeFileSync(`${output_file}.ttf`, ttf);
fs.writeFileSync(`${output_file}.eot`, new Buffer(ttf2eot(ttf_utils).buffer));
fs.writeFileSync(`${output_file}.woff`, new Buffer(ttf2woff(ttf_utils).buffer));
fs.writeFileSync(`${output_file}.woff2`, ttf2woff2(ttf));
}
}
module.exports = fontawesomeSubset;