8000 Re-arrange some mathtext code to avoid variable reuse · matplotlib/matplotlib@d0eca17 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0eca17

Browse files
committed
Re-arrange some mathtext code to avoid variable reuse
1 parent 7c8ed22 commit d0eca17

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -768,21 +768,25 @@ def __init__(self, *args, **kwargs):
768768
def _map_virtual_font(self, fontname, font_class, uniindex):
769769
# Handle these "fonts" that are actually embedded in
770770
# other fonts.
771-
mapping = stix_virtual_fonts.get(fontname)
772-
if (self._sans and mapping is None
771+
font_mapping = stix_virtual_fonts.get(fontname)
772+
if (self._sans and font_mapping is None
773773
and fontname not in ('regular', 'default')):
774-
mapping = stix_virtual_fonts['sf']
774+
font_mapping = stix_virtual_fonts['sf']
775775
doing_sans_conversion = True
776776
else:
777777
doing_sans_conversion = False
778778

779-
if mapping is not None:
780-
if isinstance(mapping, dict):
781-
try:
782-
mapping = mapping[font_class]
783-
except KeyError:
784-
mapping = mapping['rm']
779+
if isinstance(font_mapping, dict):
780+
try:
781+
mapping = font_mapping[font_class]
782+
except KeyError:
783+
mapping = font_mapping['rm']
784+
elif isinstance(font_mapping, list):
785+
mapping = font_mapping
786+
else:
787+
mapping = None
785788

789+
if mapping is not None:
786790
# Binary search for the source glyph
787791
lo = 0
788792
hi = len(mapping)
@@ -1965,11 +1969,14 @@ def csnames(group, names):
19651969
ends_with_alpha.append(name)
19661970
else:
19671971
ends_with_nonalpha.append(name)
1968-
return Regex(r"\\(?P<{}>(?:{})(?![A-Za-z]){})".format(
1969-
group,
1970-
"|".join(map(re.escape, ends_with_alpha)),
1971-
"".join(f"|{s}" for s in map(re.escape, ends_with_nonalpha)),
1972-
))
1972+
return Regex(
1973+
r"\\(?P<{group}>(?:{alpha})(?![A-Za-z]){additional}{nonalpha})".format(
1974+
group=group,
1975+
alpha="|".join(map(re.escape, ends_with_alpha)),
1976+
additional="|" if ends_with_nonalpha else "",
1977+
nonalpha="|".join(map(re.escape, ends_with_nonalpha)),
1978+
)
1979+
)
19731980

19741981
p.float_literal = Regex(r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)")
19751982
p.space = oneOf(self._space_widths)("space")
@@ -2458,9 +2465,9 @@ def subsuper(self, s, loc, toks):
24582465
hlist.hpack(width, 'exactly')
24592466
vlist.extend([Vbox(0, vgap), hlist])
24602467
shift = hlist.height + vgap + nucleus.depth
2461-
vlist = Vlist(vlist)
2462-
vlist.shift_amount = shift
2463-
result = Hlist([vlist])
2468+
vlt = Vlist(vlist)
2469+
vlt.shift_amount = shift
2470+
result = Hlist([vlt])
24642471
return [result]
24652472

24662473
# We remove kerning on the last character for consistency (otherwise
@@ -2781,20 +2788,20 @@ def substack(self, toks):
27812788
parts = toks["parts"]
27822789
state = self.get_state()
27832790
thickness = state.get_current_underline_thickness()
2784-
vlist = []
27852791

27862792
hlist = [Hlist(k) for k in parts[0]]
27872793
max_width = max(map(lambda c: c.width, hlist))
27882794

2795+
vlist = []
27892796
for sub in hlist:
27902797
cp = HCentered([sub])
27912798
cp.hpack(max_width, 'exactly')
27922799
vlist.append(cp)
27932800

2794-
vlist = [val for pair in zip(vlist,
2795-
[Vbox(0, thickness * 2)] *
2796-
len(vlist)) for val in pair]
2797-
del vlist[-1]
2798-
vlt = Vlist(vlist)
2801+
stack = [val
2802+
for pair in zip(vlist, [Vbox(0, thickness * 2)] * len(vlist))
2803+
for val in pair]
2804+
del stack[-1]
2805+
vlt = Vlist(stack)
27992806
result = [Hlist([vlt])]
28002807
return result

0 commit comments

Comments
 (0)
0