This is a repo with information how variable fonts are structured and how to build/use/manipulate them.
Spoiler: At the moment (2024-08-21) there aren't much information about the technical approach. Furthermore, the capabilities of creating it via open source tools is kind of hard.
Variable fonts are an evolution of the OpenType font specification that enables many different variations of a typeface to be incorporated into a single file.
Source: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_fonts/Variable_fonts_guide
- It is fully supported in all browsers, see MDNs Variable_fonts_guide.
- In general the font file (
.otf
/.ttf
) contains an additional table to enable the variable font.
In a naive way I thought: "We have already an icon font, it should be easy to convert it to a variable font" - I was wrong.
First of all if we use for example this tool we can generate different file types. And it may contain different icon sizes (e.g. 16, 24, 32) which might look different, with different weights etc.
But the biggest problem is that a variable font has a restriction: To interpolate a font glyph, it needs the same amount of points and lines and in the same order. Otherwise, it won't be able to interpolate properly.
A great place to start is opentype.js. You can check already created variable fonts here. And there is a good JS library behind it. Currently, they don't support writing variable fonts, but there is a PR for variable fonts support.
Based on a YouTube video you can create your own variable font with some open source tools. You need those tools:
You need to go into this directory:
cd variable-fonts
- We use a simple
test.svg
with two rectangles as a testing icon. - We run
npx @db-ux/icon-font-tools --src . --fontName test
- This will create some font files. For this example we need
fonts/all/test.woff2
- We use FontForge to open the
test.woff2
- We will save the current font as
.sfd
format liketest.sfd
. Then we change the font name via FontForge settings to something liketest_bold
and save another file astest_bold.sfd
. - Next we scroll to the bottom of the
test_bold
font inside FontForge and change the positions of the points inside the glyph. In this case we use the empty space between the rectangles to widen them. - After this we will create a font from both the
test.sfd
andtest_bold.sfd
as.ufo
file. Warning the SVG might get redrawn by@db-ux/icon-font-tools
you may need to fix issues (like the direction) in FontForge by using the "Element/Validation" setting. - Next we need to create a
.designspace
file. This is a representation (XML) for how the axis works. Check thetest.designspace
for more information. - Now we need to run
fontmake -m test.designspace -o variable --production-names --output-path test.ttf
- Goto this website and open the
test.ttf
. You should change theweight
of the icon with the slider provided. You may also seeindex.html
andindex.css
to check how you use an axis inside HTML/CSS.