Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a viewbox with the same aspect ratio as the icon entity #212

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"rimraf": "5.0.7",
"semver": "7.6.2",
"simple-icons": "13.9.0",
"svg-path-bbox": "2.1.0",
"svg2ttf": "6.0.3",
"svgpath": "2.6.0",
"ttf2eot": "3.1.0",
Expand Down
19 changes: 17 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import fsSync, { promises as fs } from 'node:fs';
import path from 'node:path';
import punycode from 'punycode/punycode.js';
import * as simpleIcons from 'simple-icons/icons';
import { svgPathBbox } from 'svg-path-bbox';
import svg2ttf from 'svg2ttf';
import SVGPath from 'svgpath';
import ttf2eot from 'ttf2eot';
Expand Down Expand Up @@ -46,6 +47,19 @@ const cssDecodeUnicode = (value) => {
return value.replace('&#x', '\\').replace(';', '').toLowerCase();
};

const convertToAspectRatioViewbox = (path) => {
const pathInstance = SVGPath(path);
const [x0, y0, x1, y1] = svgPathBbox(pathInstance);
const width = x1 - x0;
const height = y1 - y0;
const scale = 24 / height;
const pathRescale = width > height ? pathInstance.scale(scale) : pathInstance;
const [offsetX, offsetY] = svgPathBbox(pathRescale);
const pathReset = pathRescale.translate(-offsetX, -offsetY);
const [x0Reset, , x1Reset] = svgPathBbox(pathReset);
return { pathReset, horizAdvX: ((x1Reset - x0Reset) / 24) * 1200 };
};

const buildSimpleIconsSvgFontFile = async () => {
const usedUnicodes = [];
const unicodeHexBySlug = [];
Expand All @@ -64,13 +78,14 @@ const buildSimpleIconsSvgFontFile = async () => {
}

const icon = simpleIcons[si];
const verticalTransformedPath = SVGPath(icon.path)
const { pathReset, horizAdvX } = convertToAspectRatioViewbox(icon.path);
const verticalTransformedPath = pathReset
.translate(0, -24)
.scale(50, -50)
.round(6)
.toString();

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

unicodeHexBySlug[icon.slug] = {
Expand Down