Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ab6a27

Browse files
committed
mathtext: Fetch x-height from font metrics
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.
1 parent f99a345 commit 3ab6a27

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

lib/matplotlib/_mathtext.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,15 @@ def get_quad(self, fontname: str, fontsize: float, dpi: float) -> float:
445445
return metrics.advance
446446

447447
def get_xheight(self, fontname: str, fontsize: float, dpi: float) -> float:
448-
# Some fonts report the wrong x-height, while some don't store it, so
449-
# we do a poor man's x-height.
450-
metrics = self.get_metrics(
451-
fontname, mpl.rcParams['mathtext.default'], 'x', fontsize, dpi)
452-
return metrics.iceberg
448+
consts = self.get_font_constants()
449+
if consts.x_height is not None:
450+
return consts.x_height * fontsize * dpi / 72
451+
else:
452+
# Some fonts report the wrong x-height, while some don't store it, so
453+
# we do a poor man's x-height.
454+
metrics = self.get_metrics(
455+
fontname, mpl.rcParams['mathtext.default'], 'x', fontsize, dpi)
456+
return metrics.iceberg
453457

454458
def get_underline_thickness(self, font: str, fontsize: float, dpi: float) -> float:
455459
# This function used to grab underline thickness from the font
@@ -1006,6 +1010,10 @@ class FontConstantsBase:
10061010
# The size of a quad space in LaTeX, as a multiple of design size.
10071011
quad: T.ClassVar[float | None] = None
10081012

1013+
# The size of x-height in font design units (i.e., divided by units-per-em). If not
1014+
# provided, then this will be measured from the font itself.
1015+
x_height: T.ClassVar[float | None] = None
1016+
10091017

10101018
class ComputerModernFontConstants(FontConstantsBase):
10111019
# Previously, the x-height of Computer Modern was obtained from the font
@@ -1034,17 +1042,19 @@ class ComputerModernFontConstants(FontConstantsBase):
10341042
# size.
10351043
axis_height = 262144 / 2**20
10361044
quad = 1048579 / 2**20
1045+
x_height = _x_height / 2**20
10371046

10381047

10391048
class STIXFontConstants(FontConstantsBase):
10401049
script_space = 0.1
10411050
delta = 0.05
10421051
delta_slanted = 0.3
10431052
delta_integral = 0.3
1053+
_x_height = 450
1054+
x_height = _x_height / 1000
10441055
# These values are extracted from the TeX table of STIXGeneral.ttf using FontForge,
10451056
# and then divided by design xheight, since we multiply these values by the scaled
10461057
# xheight later.
1047-
_x_height = 450
10481058
supdrop = 386 / _x_height
10491059
subdrop = 50.0002 / _x_height
10501060
sup1 = 413 / _x_height
@@ -1068,10 +1078,11 @@ class STIXSansFontConstants(STIXFontConstants):
10681078

10691079

10701080
class DejaVuSerifFontConstants(FontConstantsBase):
1081+
_x_height = 1063
1082+
x_height = _x_height / 2048
10711083
# These values are extracted from the TeX table of DejaVuSerif.ttf using FontForge,
10721084
# and then divided by design xheight, since we multiply these values by the scaled
10731085
# xheight later.
1074-
_x_height = 1063
10751086
supdrop = 790.527 / _x_height
10761087
subdrop = 102.4 / _x_height
10771088
sup1 = 845.824 / _x_height
@@ -1088,10 +1099,11 @@ class DejaVuSerifFontConstants(FontConstantsBase):
10881099

10891100

10901101
class DejaVuSansFontConstants(FontConstantsBase):
1102+
_x_height = 1120
1103+
x_height = _x_height / 2048
10911104
# These values are extracted from the TeX table of DejaVuSans.ttf using FontForge,
10921105
# and then divided by design xheight, since we multiply these values by the scaled
10931106
# xheight later.
1094-
_x_height = 1120
10951107
supdrop = 790.527 / _x_height
10961108
subdrop = 102.4 / _x_height
10971109
sup1 = 845.824 / _x_height

0 commit comments

Comments
 (0)