Skip to content

Commit 2959e57

Browse files
committed
improve integral and slanted nuclei placement
1 parent 4d7d377 commit 2959e57

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

lib/matplotlib/mathtext.py

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,15 +1187,15 @@ def get_underline_thickness(self, font, fontsize, dpi):
11871187
# get any smaller
11881188
NUM_SIZE_LEVELS = 6
11891189
# Percentage of x-height of additional horiz. space after sub/superscripts
1190-
SCRIPT_SPACE = {'cm': 0.025,
1191-
'stix': 0.20,
1190+
SCRIPT_SPACE = {'cm': 0.05,
1191+
'stix': 0.10,
11921192
'stixsans': 0.10,
1193-
'arevsans': 0.10}
1193+
'arevsans': 0.05}
11941194
## Percentage of x-height that sub/superscripts drop below the baseline
1195-
SUBDROP = {'cm': 0.3,
1195+
SUBDROP = {'cm': 0.2,
11961196
'stix': 0.4,
11971197
'stixsans': 0.4,
1198-
'arevsans': 0.3}
1198+
'arevsans': 0.4}
11991199
# Percentage of x-height that superscripts are raised from the baseline
12001200
SUP1 = {'cm': 0.45,
12011201
'stix': 0.8,
@@ -1211,25 +1211,25 @@ def get_underline_thickness(self, font, fontsize, dpi):
12111211
SUB2 = {'cm': 0.3,
12121212
'stix': 0.6,
12131213
'stixsans': 0.5,
1214-
'arevsans': 0.8}
1214+
'arevsans': 0.5}
12151215
# Percentage of x-height that sub/supercripts are offset relative to the
1216-
# nucleus end
1217-
DELTA = {'cm': 0.10,
1218-
'stix': 0.10,
1219-
'stixsans': 0.25,
1220-
'arevsans': 0.12}
1221-
# Additional percentage of last character height that supercripts are offset
1222-
# relative to the subscript for slanted nuclei
1223-
DELTASLANTED = {'cm': 0.05,
1216+
# nucleus edge
1217+
DELTA = {'cm': 0.025,
12241218
'stix': 0.05,
12251219
'stixsans': 0.05,
1226-
'arevsans': 0.12}
1227-
# Percentage of x-height that supercripts are offset relative to the subscript
1228-
# for integrals
1229-
DELTAINTEGRAL = {'cm': 0.5,
1230-
'stix': 0.5,
1231-
'stixsans': 0.4,
1232-
'arevsans': 0.5}
1220+
'arevsans': 0.05}
1221+
# Additional percentage of last character height above 2/3 of the x-height that
1222+
# supercripts are offset relative to the subscript for slanted nuclei
1223+
DELTASLANTED = {'cm': 0.4,
1224+
'stix': 0.3,
1225+
'stixsans': 0.5,
1226+
'arevsans': 0.2}
1227+
# Percentage of x-height that supercripts and subscripts are offset for
1228+
# integrals
1229+
DELTAINTEGRAL = {'cm': 0.325,
1230+
'stix': 0.3,
1231+
'stixsans': 0.3,
1232+
'arevsans': 0.3}
12331233

12341234
class MathTextWarning(Warning):
12351235
pass
@@ -2718,24 +2718,35 @@ def subsuper(self, s, loc, toks):
27182718
"Subscript/superscript sequence is too long. "
27192719
"Use braces { } to remove ambiguity.")
27202720

2721+
# We remove kerning for consistency (otherwise it will compute kerning
2722+
# based on non-shrinked characters and may put them very close together
2723+
# when superscripted)
2724+
# We change the width of the last character to match the advance to
2725+
# consider some fonts with weird metrics: e.g. stix's f has a width of
2726+
# 7.75 and a kerning of -4.0 for an advance of 3.72, and we want to put
2727+
# the superscript at the advance
27212728
last_char = nucleus
27222729
if isinstance(nucleus,Hlist):
2723-
# remove kerns
2724-
new_children = []
2725-
for child in nucleus.children:
2726-
if not isinstance(child, Kern):
2727-
new_children.append(child)
2728-
nucleus = Hlist(new_children, do_kern=False)
2730+
new_children = nucleus.children
27292731
if len(new_children):
2732+
# remove last kern
2733+
if isinstance(new_children[-1],Kern):
2734+
new_children = new_children[:-1]
27302735
last_char = new_children[-1]
2736+
last_char.width = last_char._metrics.advance
2737+
nucleus = Hlist(new_children, do_kern=False)
27312738
else:
2739+
last_char.width = last_char._metrics.advance
27322740
nucleus = Hlist([nucleus],do_kern=False)
27332741

2742+
27342743
state = self.get_state()
27352744
rule_thickness = state.font_output.get_underline_thickness(
27362745
state.font, state.fontsize, state.dpi)
27372746
xHeight = state.font_output.get_xheight(
27382747
state.font, state.fontsize, state.dpi)
2748+
print(last_char, last_char.height,
2749+
xHeight)
27392750

27402751
fs = rcParams['mathtext.fontset']
27412752
# If a custom fontset is used, check if it is Arev Sans, otherwise use
@@ -2747,6 +2758,7 @@ def subsuper(self, s, loc, toks):
27472758
else:
27482759
fs = 'cm'
27492760

2761+
27502762
if napostrophes:
27512763
if super is None:
27522764
super = Hlist([])
@@ -2792,11 +2804,13 @@ def subsuper(self, s, loc, toks):
27922804
superkern = DELTA[fs] * xHeight
27932805
subkern = DELTA[fs] * xHeight
27942806
if self.is_slanted(last_char):
2795-
superkern += DELTASLANTED[fs] * xHeight
2807+
superkern += DELTASLANTED[fs] * (lc_height - xHeight * 2. / 3.)
27962808
if self.is_dropsub(last_char):
2797-
subkern = -DELTAINTEGRAL[fs] * lc_height
2809+
subkern = (2 * DELTA[fs] - DELTAINTEGRAL[fs]) * lc_height
2810+
superkern = (2 * DELTA[fs] + DELTAINTEGRAL[fs]) * lc_height
27982811
else:
2799-
subkern = 0.25 * DELTA[fs] * lc_height
2812+
subkern = -DELTA[fs] * xHeight
2813+
#subkern, superkern =0,0
28002814

28012815
if super is None:
28022816
# node757
@@ -2833,7 +2847,8 @@ def subsuper(self, s, loc, toks):
28332847
y])
28342848
x.shift_amount = shift_down
28352849

2836-
x.width += SCRIPT_SPACE[fs] * xHeight
2850+
if not self.is_dropsub(last_char):
2851+
x.width += SCRIPT_SPACE[fs] * xHeight
28372852
result = Hlist([nucleus, x], do_kern=False)
28382853
return [result]
28392854

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy