Variable fonts
A normal font file holds one weight. Regular and Bold are two files, and Light and Black are two more. A variable font is one file that holds the whole range: it has axes, and any value on an axis is a font. The usual axis is the weight (wght, 100 thin to 900 black), and fonts may also have a width (wdth), a slant (slnt), an italic switch (ital), an optical size (opsz) or axes of their own.
import os
import libstb as stb
os.makedirs("out", exist_ok=True)
font = stb.Font.open("Vazirmatn-Variable.ttf")
# A variable font is one file with design axes. This one has a single axis, the weight.
for axis in font.axes:
print(f"axis {axis.tag}: {int(axis.min)} to {int(axis.max)}, default {int(axis.default)}")
print("weight now:", font.weight)
# with_weight() is a new Font that shares the file's bytes. The font you called it on does not change.
for weight in (100, 400, 900):
print(f"width of 'Hello' at weight {weight}: {round(font.with_weight(weight).measure('Hello', 32).width)}")
print("the original is still at weight", font.weight)
# Five weights, and one in between with the axis by its name, on a flat background.
page = stb.Image.open("astronaut.png").crop(0, 0, 512, 330).fill((245, 243, 238))
for i, weight in enumerate((100, 300, 400, 700, 900)):
page = page.draw_text(f"Hello, libstb {weight}", font.with_weight(weight), 36, 20, 10 + 52 * i, color="#1d3557")
page = page.draw_text("Hello, libstb 550", font.with_variation("wght", 550), 36, 20, 270, color="#e63946")
page.write("out/weights.png")
# A value outside the range is clamped to it; an axis the font does not have is an error.
print("weight 2000 is clamped to", font.with_weight(2000).weight)
try:
font.with_variation("wdth", 80)
except ValueError:
print("an axis the font does not have: rejected")
# A font that is not variable comes back unchanged.
regular = stb.Font.open("Vazirmatn-Regular.ttf")
print("a static font with_weight(700) has weight", regular.with_weight(700).weight, "and", len(regular.axes), "axes")
axis wght: 100 to 900, default 400
weight now: 400
width of 'Hello' at weight 100: 44
width of 'Hello' at weight 400: 47
width of 'Hello' at weight 900: 48
the original is still at weight 400
weight 2000 is clamped to 900
an axis the font does not have: rejected
a static font with_weight(700) has weight 400 and 0 axes

Setting an axis
with_variation(tag, value) returns a new font with that axis set to the value, clamped to the axis's range, and every other axis as it is in the font you called it on. The new font shares the bytes of the file, so making one is cheap, and the outlines, the advances and the glyph boxes follow the new position, as the picture shows. with_weight(w) is the same for the weight axis, with the CSS numbers (400 regular, 700 bold), and a font that has no weight axis is returned unchanged.
The tag must be the four characters of an axis the font has, and the value a finite number. Anything else is an error, a ValueError.
What stays as it was
Two things come from the default instance and do not follow the axes: the kerning (GPOS) and the vertical metrics. So a text set in a heavy weight is spaced with the kerning of the default one. The widths printed by the example show how far the advances move between weights 100 and 900 for this font.
Reading the axes
axes lists every axis with its tag, its minimum, its default and its maximum, and is empty for a font that is not variable. weight on the font is the current value of the weight axis, rounded, so it follows with_weight. Finding fonts shows the range of the weight axis from the header, as weight_min and weight_max, without opening the font.
Limits
libstb reads variable fonts with TrueType outlines (the gvar table). A subset of a font with an axis applied is not supported yet; a variable font at its default settings can be subset.
API at a glance
| The axes | font.axes, each with tag, min, default, max |
|---|---|
| Set an axis | font.with_variation("wght", 550) |
| Set the weight | font.with_weight(700) |
| The current weight | font.weight |