Skip to content

Commit a873aae

Browse files
committed
Use a viewbox with the same aspect ratio as the icon entity
1 parent b8c397e commit a873aae

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"semver": "7.6.2",
4949
"serve": "14.2.3",
5050
"simple-icons": "14.0.0",
51+
"svg-path-bbox": "2.1.0",
5152
"svg2ttf": "6.0.3",
5253
"svgpath": "2.6.0",
5354
"ttf2eot": "3.1.0",

scripts/build.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import fsSync, { promises as fs } from 'node:fs';
1010
import path from 'node:path';
1111
import punycode from 'punycode/punycode.js';
1212
import * as simpleIcons from 'simple-icons/icons';
13+
import { svgPathBbox } from 'svg-path-bbox';
1314
import svg2ttf from 'svg2ttf';
1415
import SVGPath from 'svgpath';
1516
import ttf2eot from 'ttf2eot';
@@ -46,6 +47,19 @@ const cssDecodeUnicode = (value) => {
4647
return value.replace('&#x', '\\').replace(';', '').toLowerCase();
4748
};
4849

50+
const convertToAspectRatioViewbox = (path) => {
51+
const pathInstance = SVGPath(path);
52+
const [x0, y0, x1, y1] = svgPathBbox(pathInstance);
53+
const width = x1 - x0;
54+
const height = y1 - y0;
55+
const scale = 24 / height;
56+
const pathRescale = width > height ? pathInstance.scale(scale) : pathInstance;
57+
const [offsetX, offsetY] = svgPathBbox(pathRescale);
58+
const pathReset = pathRescale.translate(-offsetX, -offsetY);
59+
const [x0Reset, , x1Reset] = svgPathBbox(pathReset);
60+
return { pathReset, horizAdvX: ((x1Reset - x0Reset) / 24) * 1200 };
61+
};
62+
4963
const buildSimpleIconsSvgFontFile = async () => {
5064
const usedUnicodes = [];
5165
const unicodeHexBySlug = [];
@@ -64,13 +78,14 @@ const buildSimpleIconsSvgFontFile = async () => {
6478
}
6579

6680
const icon = simpleIcons[si];
67-
const verticalTransformedPath = SVGPath(icon.path)
81+
const { pathReset, horizAdvX } = convertToAspectRatioViewbox(icon.path);
82+
const verticalTransformedPath = pathReset
6883
.translate(0, -24)
6984
.scale(50, -50)
7085
.round(6)
7186
.toString();
7287

73-
glyphsContent += `<glyph glyph-name="${icon.slug}" unicode="${unicodeString}" d="${verticalTransformedPath}" horiz-adv-x="1200"/>`;
88+
glyphsContent += `<glyph glyph-name="${icon.slug}" unicode="${unicodeString}" d="${verticalTransformedPath}" horiz-adv-x="${horizAdvX}"/>`;
7489
usedUnicodes.push(unicodeString);
7590

7691
unicodeHexBySlug[icon.slug] = {

0 commit comments

Comments
 (0)