Skip to content

Commit 0744ec9

Browse files
committed
mathtext: Fetch quad width from font metrics
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.
1 parent 1cacc20 commit 0744ec9

File tree

319 files changed

+5817
-5437
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

319 files changed

+5817
-5437
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ def get_axis_height(self, font: str, fontsize: float, dpi: float) -> float:
312312
"""
313313
raise NotImplementedError()
314314

315+
def get_quad(self, font: str, fontsize: float, dpi: float) -> float:
316+
"""
317+
Get the size of a quad for the given *font* and *fontsize*.
318+
"""
319+
raise NotImplementedError()
320+
315321
def get_xheight(self, font: str, fontsize: float, dpi: float) -> float:
316322
"""
317323
Get the xheight for the given *font* and *fontsize*.
@@ -424,6 +430,16 @@ def get_axis_height(self, fontname: str, fontsize: float, dpi: float) -> float:
424430
fontname, mpl.rcParams['mathtext.default'], '\u2212', fontsize, dpi)
425431
return (metrics.ymax + metrics.ymin) / 2
426432

433+
def get_quad(self, fontname: str, fontsize: float, dpi: float) -> float:
434+
consts = _get_font_constants(self, fontname)
435+
if consts.quad is not None:
436+
return consts.quad * fontsize * dpi / 72
437+
else:
438+
# With no other option, we measure the size of an 'm'.
439+
metrics = self.get_metrics(
440+
fontname, mpl.rcParams['mathtext.default'], 'm', fontsize, dpi)
441+
return metrics.advance
442+
427443
def get_xheight(self, fontname: str, fontsize: float, dpi: float) -> float:
428444
# Some fonts report the wrong x-height, while some don't store it, so
429445
# we do a poor man's x-height.
@@ -958,6 +974,9 @@ class FontConstantsBase:
958974
# formula, similar to a baseline, as a multiple of design size.
959975
axis_height: T.ClassVar[float | None] = None
960976

977+
# The size of a quad space in LaTeX, as a multiple of design size.
978+
quad: T.ClassVar[float | None] = None
979+
961980

962981
class ComputerModernFontConstants(FontConstantsBase):
963982
# Previously, the x-height of Computer Modern was obtained from the font
@@ -985,6 +1004,7 @@ class ComputerModernFontConstants(FontConstantsBase):
9851004
# These come from the cmsy10.tfm metrics, scaled so they are in multiples of design
9861005
# size.
9871006
axis_height = 262144 / 2**20
1007+
quad = 1048579 / 2**20
9881008

9891009

9901010
class STIXFontConstants(FontConstantsBase):
@@ -1009,6 +1029,7 @@ class STIXFontConstants(FontConstantsBase):
10091029
# These come from the same TeX table, scaled by Em size so they are in multiples of
10101030
# design size.
10111031
axis_height = 250 / 1000
1032+
quad = 1000 / 1000
10121033

10131034

10141035
class STIXSansFontConstants(STIXFontConstants):
@@ -2330,10 +2351,7 @@ def _make_space(self, percentage: float) -> Kern:
23302351
key = (state.font, state.fontsize, state.dpi)
23312352
width = self._em_width_cache.get(key)
23322353
if width is None:
2333-
metrics = state.fontset.get_metrics(
2334-
'it', mpl.rcParams['mathtext.default'], 'm',
2335-
state.fontsize, state.dpi)
2336-
width = metrics.advance
2354+
width = state.fontset.get_quad('it', state.fontsize, state.dpi)
23372355
self._em_width_cache[key] = width
23382356
return Kern(width * percentage)
23392357

Binary file not shown.
-75 Bytes
Loading

0 commit comments

Comments
 (0)