Skip to content

Commit 383028b

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 692df3f commit 383028b

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
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):
@@ -2329,10 +2350,7 @@ def _make_space(self, percentage: float) -> Kern:
23292350
key = (state.font, state.fontsize, state.dpi)
23302351
width = self._em_width_cache.get(key)
23312352
if width is None:
2332-
metrics = state.fontset.get_metrics(
2333-
'it', mpl.rcParams['mathtext.default'], 'm',
2334-
state.fontsize, state.dpi)
2335-
width = metrics.advance
2353+
width = state.fontset.get_quad('it', state.fontsize, state.dpi)
23362354
self._em_width_cache[key] = width
23372355
return Kern(width * percentage)
23382356

0 commit comments

Comments
 (0)