Skip to content

Comments

mathtext: Fetch quad width & axis height from font metrics#31110

Open
QuLogic wants to merge 3 commits intomatplotlib:text-overhaulfrom
QuLogic:space-axisheight-metrics
Open

mathtext: Fetch quad width & axis height from font metrics#31110
QuLogic wants to merge 3 commits intomatplotlib:text-overhaulfrom
QuLogic:space-axisheight-metrics

Conversation

@QuLogic
Copy link
Member

@QuLogic QuLogic commented Feb 7, 2026

PR summary

As a followup to #31046 that @anntzer requested, this fetches the quad width and axis height from the font metrics, either the TFM or the TeX table in FontForge.

For Computer Modern and STIX, the axis height metric is very close to the previous calculation and there are minuscule changes. The quad width is a bit bigger than the old 'm' calculation, but it does appear closer to usetex=True output.

For DejaVu however, I'm not super happy with the embedded metrics. In #22852, @tfpf originally intended to calculate the axis height so that fraction bars aligned with the minus sign. This is not the case with the metric embedded in the font, and it's a pixel or two lower. Additionally, the quad space is completely bogus since it's -2048, so I didn't even bother adding it. I have left the axis height metric in for review here, but we may want to revert that for this font, especially since this is our default.

Note, I have based this PR on the text-overhaul-figures branch so that the subsequent commits only show the relevant changes, but that does inflate the "Files changed" count.

PR checklist

This mostly has a minimal change to results as the axis height is fairly
closely aligned with the minus sign as previously implemented.
These are mostly wider than the previous calculation of the 'm' width.
The values are invalid in the DejaVu fonts (-2048), so those still use
the old method.
@QuLogic QuLogic added this to the v3.11.0 milestone Feb 7, 2026
@github-project-automation github-project-automation bot moved this to Waiting for other PR in Font and text overhaul Feb 7, 2026
@QuLogic QuLogic moved this from Waiting for other PR to Ready for Review in Font and text overhaul Feb 7, 2026
@anntzer
Copy link
Contributor

anntzer commented Feb 7, 2026

Re: DejaVu's font constants: I think you can't actually believe the values that fontforge gives you, because they appear to be nonstandard tables that I believe are "autofilled" by fontforge -- see https://fontforge.org/docs/techref/non-standard.html; also opening e.g. DejaVuSans.ttf with fontTools shows that there isn't actually a "TeX " table in that file (if I got that correctly).
I suspect that fontforge just has its own algorithm for filling in these values, although I couldn't actually find them from a quick skim over their source tree. I guess that for fonts like DejaVu Sans with no math metrics it makes sense to just fall back to our own guesses (use "m" and "x" metrics; align fraction bar midway of equal sign).

@llohse
Copy link

llohse commented Feb 10, 2026

Doesn't it make sense to also add underline thickness and x-height while you are at it? For the LaTeX fonts and STIX, these should be very reliable, as LaTeX uses them.
For OpenType math, they can be easily converted from the MATH table.

This only leaves DejaVu, for which one can measure the values once, and all other "custom" fonts, for which one can fall back to the old behavior.

@QuLogic
Copy link
Member Author

QuLogic commented Feb 18, 2026

Probably we should do those as well, but maybe that should wait for the refactoring in #31121 as it makes it easier to fetch from the constants where necessary.

@tacaswell
Copy link
Member

It looks like 3 more images need to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

5 participants