Description
Problem
Matplotlib loads a variable font as a single static font. Whatever weight is requested, the text is drawn with the font's default instance.
Reproduction with Inter 4.1, whose InterVariable.ttf has a weight axis from 100 to 900:
# /// script
# dependencies = ["matplotlib"]
# ///
import io
import urllib.request
import zipfile
import matplotlib.pyplot as plt
from matplotlib import font_manager as fm
# Inter 4.1; InterVariable.ttf has a weight axis from 100 to 900.
url = "https://github.com/rsms/inter/releases/download/v4.1/Inter-4.1.zip"
with urllib.request.urlopen(url) as response:
path = zipfile.ZipFile(io.BytesIO(response.read())).extract("InterVariable.ttf")
fm.fontManager.addfont(path)
print([(e.name, e.style, e.weight) for e in fm.fontManager.ttflist if e.fname == path])
fig, ax = plt.subplots(figsize=(6, 2.2))
ax.axis("off")
for i, weight in enumerate(["light", "regular", "bold", "black"]):
ax.text(0, 0.85 - 0.25 * i, f"Inter Variable, weight={weight!r}",
family="Inter Variable", weight=weight, size=18)
fig.savefig("variable.png", dpi=100)Output with Matplotlib 3.11.2:
[('Inter Variable', 'normal', 400)]
findfont: Failed to find font weight light for Inter Variable, now using 400.
findfont: Failed to find font weight bold for Inter Variable, now using 400.
findfont: Failed to find font weight black for Inter Variable, now using 400.
All four lines of the figure are drawn in the same Regular weight.
The font is registered as one entry with one weight, so the font manager cannot know that the file covers the other weights. The result depends on where the font designer put the default instance. Inter defaults to Regular, so the result is merely not bold. The font where we first hit this defaults to its lightest weight (25), so every plot came out in hairline text.
Variable fonts have been part of OpenType since 1.8 (2016) and many typefaces are now distributed mainly or only in this form. The workaround is to install static instances of the same family, which is not always possible.
I found no open issue for this. #31434 adds the internal FT2Font API to read and set variations, and asks how the public side should behave.
Proposed solution
Treat a variable font as matching any value inside its axis ranges, and set the axis when the font is used:
weightselects a value on thewghtaxis.stretchselects a value on thewdthaxis.style="italic"or"oblique"uses theitalorslntaxis if the font has one.
This answers the question raised in #31434 in favour of automatic matching: FontProperties(family="Inter Variable", weight="bold") should give bold text without the user knowing that the font is variable. It is how CSS font-weight behaves in browsers, which the font manager is already modelled on, and how fontconfig and Typst behave.
Activity