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

Skip to content

Commit 692df3f

Browse files
committed
mathtext: Fetch axis height from font metrics
This mostly has a minimal change to results as the axis height is fairly closely aligned with the minus sign as previously implemented.
1 parent dfe4e55 commit 692df3f

2 files changed

Lines changed: 40 additions & 17 deletions

File tree

lib/matplotlib/_mathtext.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,15 @@ def _get_info(self, fontname: str, font_class: str, sym: str, fontsize: float,
414414
)
415415

416416
def get_axis_height(self, fontname: str, fontsize: float, dpi: float) -> float:
417-
# The fraction line (if present) must be aligned with the minus sign. Therefore,
418-
# the height of the latter from the baseline is the axis height.
419-
metrics = self.get_metrics(
420-
fontname, mpl.rcParams['mathtext.default'], '\u2212', fontsize, dpi)
421-
return (metrics.ymax + metrics.ymin) / 2
417+
consts = _get_font_constants(self, fontname)
418+
if consts.axis_height is not None:
419+
return consts.axis_height * fontsize * dpi / 72
420+
else:
421+
# The fraction line (if present) must be aligned with the minus sign.
422+
# Therefore, the height of the latter from the baseline is the axis height.
423+
metrics = self.get_metrics(
424+
fontname, mpl.rcParams['mathtext.default'], '\u2212', fontsize, dpi)
425+
return (metrics.ymax + metrics.ymin) / 2
422426

423427
def get_xheight(self, fontname: str, fontsize: float, dpi: float) -> float:
424428
# Some fonts report the wrong x-height, while some don't store it, so
@@ -950,6 +954,10 @@ class FontConstantsBase:
950954
# and scriptscript styles.
951955
denom2: T.ClassVar[float] = 1.1
952956

957+
# The height of a horizontal reference line used for positioning elements in a
958+
# formula, similar to a baseline, as a multiple of design size.
959+
axis_height: T.ClassVar[float | None] = None
960+
953961

954962
class ComputerModernFontConstants(FontConstantsBase):
955963
# Previously, the x-height of Computer Modern was obtained from the font
@@ -974,14 +982,17 @@ class ComputerModernFontConstants(FontConstantsBase):
974982
num3 = 465286 / _x_height
975983
denom1 = 719272 / _x_height
976984
denom2 = 361592 / _x_height
985+
# These come from the cmsy10.tfm metrics, scaled so they are in multiples of design
986+
# size.
987+
axis_height = 262144 / 2**20
977988

978989

979990
class STIXFontConstants(FontConstantsBase):
980991
script_space = 0.1
981992
delta = 0.05
982993
delta_slanted = 0.3
983994
delta_integral = 0.3
984-
# These values are extracted from the TeX table of STIXGeneral.ttf using FreeType,
995+
# These values are extracted from the TeX table of STIXGeneral.ttf using FontForge,
985996
# and then divided by design xheight, since we multiply these values by the scaled
986997
# xheight later.
987998
_x_height = 450
@@ -995,6 +1006,9 @@ class STIXFontConstants(FontConstantsBase):
9951006
num3 = 474 / _x_height
9961007
denom1 = 756 / _x_height
9971008
denom2 = 375 / _x_height
1009+
# These come from the same TeX table, scaled by Em size so they are in multiples of
1010+
# design size.
1011+
axis_height = 250 / 1000
9981012

9991013

10001014
class STIXSansFontConstants(STIXFontConstants):
@@ -1004,7 +1018,7 @@ class STIXSansFontConstants(STIXFontConstants):
10041018

10051019

10061020
class DejaVuSerifFontConstants(FontConstantsBase):
1007-
# These values are extracted from the TeX table of DejaVuSerif.ttf using FreeType,
1021+
# These values are extracted from the TeX table of DejaVuSerif.ttf using FontForge,
10081022
# and then divided by design xheight, since we multiply these values by the scaled
10091023
# xheight later.
10101024
_x_height = 1063
@@ -1018,10 +1032,13 @@ class DejaVuSerifFontConstants(FontConstantsBase):
10181032
num3 = 970.752 / _x_height
10191033
denom1 = 1548.29 / _x_height
10201034
denom2 = 768 / _x_height
1035+
# These come from the same TeX table, scaled by Em size so they are in multiples of
1036+
# design size.
1037+
axis_height = 512 / 2048
10211038

10221039

10231040
class DejaVuSansFontConstants(FontConstantsBase):
1024-
# These values are extracted from the TeX table of DejaVuSans.ttf using FreeType,
1041+
# These values are extracted from the TeX table of DejaVuSans.ttf using FontForge,
10251042
# and then divided by design xheight, since we multiply these values by the scaled
10261043
# xheight later.
10271044
_x_height = 1120
@@ -1035,6 +1052,9 @@ class DejaVuSansFontConstants(FontConstantsBase):
10351052
num3 = 970.752 / _x_height
10361053
denom1 = 1548.29 / _x_height
10371054
denom2 = 768 / _x_height
1055+
# These come from the same TeX table, scaled by Em size so they are in multiples of
1056+
# design size.
1057+
axis_height = 512 / 2048
10381058

10391059

10401060
# Maps font family names to the FontConstantBase subclass to use
@@ -1062,17 +1082,20 @@ class DejaVuSansFontConstants(FontConstantsBase):
10621082
}
10631083

10641084

1065-
def _get_font_constant_set(state: ParserState) -> type[FontConstantsBase]:
1066-
constants = _font_constant_mapping.get(
1067-
state.fontset._get_font(state.font).family_name, FontConstantsBase)
1085+
def _get_font_constants(fontset: Fonts, font: str) -> type[FontConstantsBase]:
1086+
constants = _font_constant_mapping.get(fontset._get_font(font).family_name,
1087+
FontConstantsBase)
10681088
# STIX sans isn't really its own fonts, just different code points
10691089
# in the STIX fonts, so we have to detect this one separately.
1070-
if (constants is STIXFontConstants and
1071-
isinstance(state.fontset, StixSansFonts)):
1090+
if constants is STIXFontConstants and isinstance(fontset, StixSansFonts):
10721091
return STIXSansFontConstants
10731092
return constants
10741093

10751094

1095+
def _get_font_constant_set(state: ParserState) -> type[FontConstantsBase]:
1096+
return _get_font_constants(state.fontset, state.font)
1097+
1098+
10761099
class Node:
10771100
"""A node in the TeX box model."""
10781101

lib/matplotlib/tests/test_mathtext.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,12 +577,12 @@ def test_box_repr():
577577
_mathtext.DejaVuSansFonts(fm.FontProperties(), LoadFlags.NO_HINTING),
578578
fontsize=12, dpi=100))
579579
assert s == textwrap.dedent("""\
580-
Hlist<w=9.51 h=15.30 d=5.00 s=0.00>[
580+
Hlist<w=9.51 h=14.24 d=6.06 s=0.00>[
581581
Hlist<w=0.00 h=0.00 d=0.00 s=0.00>[],
582-
Hlist<w=9.51 h=15.30 d=5.00 s=0.00>[
583-
Hlist<w=9.51 h=15.30 d=5.00 s=0.00>[
582+
Hlist<w=9.51 h=14.24 d=6.06 s=0.00>[
583+
Hlist<w=9.51 h=14.24 d=6.06 s=0.00>[
584584
Hbox,
585-
Vlist<w=7.43 h=20.30 d=0.00 s=5.00>[
585+
Vlist<w=7.43 h=20.30 d=0.00 s=6.06>[
586586
HCentered<w=7.43 h=8.51 d=0.00 s=0.00>[
587587
Glue,
588588
Hlist<w=7.43 h=8.51 d=0.00 s=0.00>[

0 commit comments

Comments
 (0)