text: Use font metrics to determine line heights#31291
text: Use font metrics to determine line heights#31291QuLogic wants to merge 4 commits intomatplotlib:text-overhaulfrom
Conversation
Discussed on the call today and decided not to add line gap for a single-line text. This should be closer to what we used to do, and will minimize the number of changes that occur due to this. |
This is minimally different from the `x` measurement, but technically more correct. We still do the measurement for fonts we don't ship, but that may change with Unicode Math fonts in the future.
This follows from CSS' default for line height. At the moment, the behaviour has not been changed, and still just falls back to 1.2 for 'normal'.
We follow the process from [the CSS Inline Layout module](https://www.w3.org/TR/css-inline-3/), specifically: 1. The default ascent and descent come from the `OS/2` font table, or failing that, the `hhea` table, with final fallback to the measurement we used to do. 2. If `linespacing` (cf line height in CSS) is normal, then we do as before and size each line based on the maximum ascent/descent of its contents. Additionally, apply the line gap from the font metrics as half-leading around each line. 3. If `linespacing` is a float, then scale it by font size of the first available font, and keep it fixed for each line. However, if we are drawing a single line, then we do not add the line gap around the line, to keep them a similar height as before.
|
I've now pushed fixes to the |
PR summary
As outlined in #31220, we follow the process from the CSS Inline Layout module, specifically:
OS/2font table, or failing that, thehheatable, with final fallback to the measurement we used to do.linespacing(cf line height in CSS) is normal, then we do as before and size each line based on the maximum ascent/descent of its contents. Additionally, apply the line gap from the font metrics as half-leading around each line.linespacingis a float, then scale it by font size of the first available font, and keep it fixed for each line.Additionally, the first commit changes mathtext to use the x-height from the default fonts. We can read that from font metrics, but I thought I'd leave the general case to something like #31048.
Note, this is based on the
text-overhaul-figuresbranch, so more test images than necessary are changed here. Please look at the last commit for the test images. Most test image changes are minor, maybe half a pixel or so with the text. The main ones you probably want to look at are those with multiple lines, such as 'basictext_wrap.png`.One thing to decide is whether to apply the line gap for a single line. Previously, I don't believe we ended up doing that, because the line spacing was added before the second and subsequent lines. This PR currently always adds it, which means the text bounding box is taller in all cases. This causes such changes as the fancy bbox patches in
boxarrow_test_image.pngto all be larger, or e.g., every multi-artist legend to be larger. To minimize such changes, we can decide to drop the line gap for single-line text entries, if we want?AI Disclosure
None
PR checklist