Skip to content

Commit

Permalink
- Add support for duotone fonts
Browse files Browse the repository at this point in the history
- Update FontAwesome package versions
- Fixes for woff / eot font file type generation
  • Loading branch information
omacranger committed Nov 12, 2019
1 parent bf8213a commit f5efb38
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
32 changes: 22 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ function fontawesomeSubset(subset, output_dir, options){
solid: 'fa-solid-900',
light: 'fa-light-300',
regular: 'fa-regular-400',
brands: 'fa-brands-400'
brands: 'fa-brands-400',
duotone: 'fa-duotone-900',
};

let opts = {
const opts = {
...{
package: 'free',
}, ...options
Expand All @@ -39,7 +40,7 @@ function fontawesomeSubset(subset, output_dir, options){
subset = {'solid': subset};
}

for (let [font_family, value] of Object.entries(subset)) {
for (let [font_family, icons] 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`;

Expand All @@ -53,22 +54,33 @@ function fontawesomeSubset(subset, output_dir, options){
continue;
}

let svg_file = fs.readFileSync(svg_file_path).toString(),
const 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]);
if(font_family === 'duotone'){
// If we're matching duotone we need to remove the trailing `-secondary` or `-primary`
if (icons.indexOf(current_match[1].substring(0, current_match[1].lastIndexOf('-'))) === -1) {
glyphs.push(current_match[1]);
}
} else {
if (icons.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_utils = svg2ttf(svg_contents_new, {
fullname: 'FontAwesome ' + font_family,
familyname: 'FontAwesome',
subfamilyname: font_family,
}),
ttf = Buffer.from(ttf_utils.buffer);

mkdirp.sync(path.resolve(output_dir), (err) => {
Expand All @@ -78,12 +90,12 @@ function fontawesomeSubset(subset, output_dir, options){
}
});

let output_file = path.resolve(output_dir, font_map[font_family]);
const 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`, ttf2eot(ttf));
fs.writeFileSync(`${output_file}.woff`, ttf2woff(ttf));
fs.writeFileSync(`${output_file}.eot`, ttf2eot(ttf).buffer);
fs.writeFileSync(`${output_file}.woff`, ttf2woff(ttf).buffer);
fs.writeFileSync(`${output_file}.woff2`, ttf2woff2(ttf));
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"ttf2eot": "^2.0.0",
"ttf2woff": "^2.0.1",
"ttf2woff2": "^2.0.3",
"@fortawesome/fontawesome-free": "^5.7.2",
"@fortawesome/fontawesome-free": "^5.11.2",
"mkdirp": "^0.5.1"
},
"optionalDependencies": {
"@fortawesome/fontawesome-pro": "^5.7.2"
"@fortawesome/fontawesome-pro": "^5.11.2"
},
"license": "GPL-3.0-or-later",
"repository": {
Expand Down

0 comments on commit f5efb38

Please sign in to comment.