Skip to content

Commit 1cd8510

Browse files
committed
Fix up mathtext constants for fraction/script alignment
Computer Modern values are taken from `cmsy10.tfm`, and divided by the x-height in that output to match the scale used in Matplotlib. DejaVu Sans/Serif and STIX constants are taken from the embedded TeX table extracted with FontForge.
1 parent 4bfa0f9 commit 1cd8510

File tree

2 files changed

+62
-32
lines changed

2 files changed

+62
-32
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -958,53 +958,83 @@ class ComputerModernFontConstants(FontConstantsBase):
958958
# type 32). Now that we're using the rendered x-height, some font constants
959959
# have been increased by the same factor to compensate.
960960
script_space = 0.132861328125
961-
supdrop = 0.354296875
962-
subdrop = 0.354296875
963-
sup1 = 0.79716796875
964-
sub1 = 0.354296875
965-
sub2 = 0.5314453125
966961
delta = 0.132861328125
967962
delta_slanted = 0.3
968963
delta_integral = 0.3
969-
num1 = 1.5
970-
num2 = 1.5
971-
num3 = 1.5
972-
denom1 = 1.6
973-
denom2 = 1.2
964+
_x_height = 451470
965+
# These all come from the cmsy10.tfm metrics, divided by the design xheight from
966+
# there, since we multiply these values by the scaled xheight later.
967+
supdrop = 404864 / _x_height
968+
subdrop = 52429 / _x_height
969+
sup1 = 432949 / _x_height
970+
sub1 = 157286 / _x_height
971+
sub2 = 259226 / _x_height
972+
num1 = 709370 / _x_height
973+
num2 = 412858 / _x_height
974+
num3 = 465286 / _x_height
975+
denom1 = 719272 / _x_height
976+
denom2 = 361592 / _x_height
974977

975978

976979
class STIXFontConstants(FontConstantsBase):
977980
script_space = 0.1
978-
sup1 = 0.8
979-
sub2 = 0.6
980981
delta = 0.05
981982
delta_slanted = 0.3
982983
delta_integral = 0.3
983-
num1 = 1.6
984-
num2 = 1.6
985-
num3 = 1.6
986-
denom1 = 1.6
987-
988-
989-
class STIXSansFontConstants(FontConstantsBase):
984+
# These values are extracted from the TeX table of STIXGeneral.ttf using FreeType,
985+
# and then divided by design xheight, since we multiply these values by the scaled
986+
# xheight later.
987+
_x_height = 450
988+
supdrop = 386 / _x_height
989+
subdrop = 50.0002 / _x_height
990+
sup1 = 413 / _x_height
991+
sub1 = 150 / _x_height
992+
sub2 = 309 / _x_height
993+
num1 = 747 / _x_height
994+
num2 = 424 / _x_height
995+
num3 = 474 / _x_height
996+
denom1 = 756 / _x_height
997+
denom2 = 375 / _x_height
998+
999+
1000+
class STIXSansFontConstants(STIXFontConstants):
9901001
script_space = 0.05
991-
sup1 = 0.8
9921002
delta_slanted = 0.6
9931003
delta_integral = 0.3
994-
num1 = 1.5
995-
num3 = 1.5
996-
denom1 = 1.5
9971004

9981005

9991006
class DejaVuSerifFontConstants(FontConstantsBase):
1000-
num1 = 1.5
1001-
num2 = 1.6
1002-
num3 = 1.4
1003-
denom1 = 1.4
1007+
# These values are extracted from the TeX table of DejaVuSerif.ttf using FreeType,
1008+
# and then divided by design xheight, since we multiply these values by the scaled
1009+
# xheight later.
1010+
_x_height = 1063
1011+
supdrop = 790.527 / _x_height
1012+
subdrop = 102.4 / _x_height
1013+
sup1 = 845.824 / _x_height
1014+
sub1 = 307.199 / _x_height
1015+
sub2 = 632.832 / _x_height
1016+
num1 = 1529.86 / _x_height
1017+
num2 = 868.352 / _x_height
1018+
num3 = 970.752 / _x_height
1019+
denom1 = 1548.29 / _x_height
1020+
denom2 = 768 / _x_height
10041021

10051022

10061023
class DejaVuSansFontConstants(FontConstantsBase):
1007-
pass
1024+
# These values are extracted from the TeX table of DejaVuSans.ttf using FreeType,
1025+
# and then divided by design xheight, since we multiply these values by the scaled
1026+
# xheight later.
1027+
_x_height = 1120
1028+
supdrop = 790.527 / _x_height
1029+
subdrop = 102.4 / _x_height
1030+
sup1 = 845.824 / _x_height
1031+
sub1 = 307.199 / _x_height
1032+
sub2 = 632.832 / _x_height
1033+
num1 = 1529.86 / _x_height
1034+
num2 = 868.352 / _x_height
1035+
num3 = 970.752 / _x_height
1036+
denom1 = 1548.29 / _x_height
1037+
denom2 = 768 / _x_height
10081038

10091039

10101040
# Maps font family names to the FontConstantBase subclass to use

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=18.09 d=7.03 s=0.00>[
580+
Hlist<w=9.51 h=15.30 d=5.00 s=0.00>[
581581
Hlist<w=0.00 h=0.00 d=0.00 s=0.00>[],
582-
Hlist<w=9.51 h=18.09 d=7.03 s=0.00>[
583-
Hlist<w=9.51 h=18.09 d=7.03 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>[
584584
Hbox,
585-
Vlist<w=7.43 h=25.12 d=0.00 s=7.03>[
585+
Vlist<w=7.43 h=20.30 d=0.00 s=5.00>[
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)