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

Skip to content

Commit 45ac295

Browse files
authored
Merge pull request #31445 from ksunden/auto-backport-of-pr-31437-on-v3.10.x
Backport PR #31437: mathtext: Fix type inconsistency with fontmaps
2 parents 51c85f7 + cada119 commit 45ac295

4 files changed

Lines changed: 36 additions & 44 deletions

File tree

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ stages:
5050
strategy:
5151
matrix:
5252
Linux_py310:
53-
vmImage: 'ubuntu-20.04' # keep one job pinned to the oldest image
53+
vmImage: 'ubuntu-22.04' # keep one job pinned to the oldest image
5454
python.version: '3.10'
5555
Linux_py311:
5656
vmImage: 'ubuntu-latest'

ci/mypy-stubtest-allowlist.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ matplotlib\.ticker\.LogitLocator\.nonsingular
2828

2929
# Stdlib/Enum considered inconsistent (no fault of ours, I don't think)
3030
matplotlib\.backend_bases\._Mode\.__new__
31-
matplotlib\.units\.Number\.__hash__
3231

3332
# 3.6 Pending deprecations
3433
matplotlib\.figure\.Figure\.set_constrained_layout

lib/matplotlib/_mathtext.py

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -331,20 +331,15 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
331331
# Per-instance cache.
332332
self._get_info = functools.cache(self._get_info) # type: ignore[method-assign]
333333
self._fonts = {}
334-
self.fontmap: dict[str | int, str] = {}
334+
self.fontmap: dict[str, str] = {}
335335

336336
filename = findfont(self.default_font_prop)
337337
default_font = get_font(filename)
338338
self._fonts['default'] = default_font
339339
self._fonts['regular'] = default_font
340340

341-
def _get_font(self, font: str | int) -> FT2Font:
342-
if font in self.fontmap:
343-
basename = self.fontmap[font]
344-
else:
345-
# NOTE: An int is only passed by subclasses which have placed int keys into
346-
# `self.fontmap`, so we must cast this to confirm it to typing.
347-
basename = T.cast(str, font)
341+
def _get_font(self, font: str) -> FT2Font:
342+
basename = self.fontmap.get(font, font)
348343
cached_font = self._fonts.get(basename)
349344
if cached_font is None and os.path.exists(basename):
350345
cached_font = get_font(basename)
@@ -574,12 +569,13 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
574569
# include STIX sized alternatives for glyphs if fallback is STIX
575570
if isinstance(self._fallback_font, StixFonts):
576571
stixsizedaltfonts = {
577-
0: 'STIXGeneral',
578-
1: 'STIXSizeOneSym',
579-
2: 'STIXSizeTwoSym',
580-
3: 'STIXSizeThreeSym',
581-
4: 'STIXSizeFourSym',
582-
5: 'STIXSizeFiveSym'}
572+
'0': 'STIXGeneral',
573+
'1': 'STIXSizeOneSym',
574+
'2': 'STIXSizeTwoSym',
575+
'3': 'STIXSizeThreeSym',
576+
'4': 'STIXSizeFourSym',
577+
'5': 'STIXSizeFiveSym',
578+
}
583579

584580
for size, name in stixsizedaltfonts.items():
585581
fullpath = findfont(name)
@@ -637,7 +633,7 @@ def _get_glyph(self, fontname: str, font_class: str,
637633

638634
g = self._fallback_font._get_glyph(fontname, font_class, sym)
639635
family = g[0].family_name
640-
if family in list(BakomaFonts._fontmap.values()):
636+
if family in BakomaFonts._fontmap.values():
641637
family = "Computer Modern"
642638
_log.info("Substituting symbol %s from %s", sym, family)
643639
return g
@@ -658,13 +654,12 @@ def _get_glyph(self, fontname: str, font_class: str,
658654
def get_sized_alternatives_for_symbol(self, fontname: str,
659655
sym: str) -> list[tuple[str, str]]:
660656
if self._fallback_font:
661-
return self._fallback_font.get_sized_alternatives_for_symbol(
662-
fontname, sym)
657+
return self._fallback_font.get_sized_alternatives_for_symbol(fontname, sym)
663658
return [(fontname, sym)]
664659

665660

666661
class DejaVuFonts(UnicodeFonts, metaclass=abc.ABCMeta):
667-
_fontmap: dict[str | int, str] = {}
662+
_fontmap: dict[str, str] = {}
668663

669664
def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlags):
670665
# This must come first so the backend's owner is set correctly
@@ -676,11 +671,11 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
676671
TruetypeFonts.__init__(self, default_font_prop, load_glyph_flags)
677672
# Include Stix sized alternatives for glyphs
678673
self._fontmap.update({
679-
1: 'STIXSizeOneSym',
680-
2: 'STIXSizeTwoSym',
681-
3: 'STIXSizeThreeSym',
682-
4: 'STIXSizeFourSym',
683-
5: 'STIXSizeFiveSym',
674+
'1': 'STIXSizeOneSym',
675+
'2': 'STIXSizeTwoSym',
676+
'3': 'STIXSizeThreeSym',
677+
'4': 'STIXSizeFourSym',
678+
'5': 'STIXSizeFiveSym',
684679
})
685680
for key, name in self._fontmap.items():
686681
fullpath = findfont(name)
@@ -718,7 +713,7 @@ class DejaVuSerifFonts(DejaVuFonts):
718713
'sf': 'DejaVu Sans',
719714
'tt': 'DejaVu Sans Mono',
720715
'ex': 'DejaVu Serif Display',
721-
0: 'DejaVu Serif',
716+
'0': 'DejaVu Serif',
722717
}
723718

724719

@@ -736,7 +731,7 @@ class DejaVuSansFonts(DejaVuFonts):
736731
'sf': 'DejaVu Sans',
737732
'tt': 'DejaVu Sans Mono',
738733
'ex': 'DejaVu Sans Display',
739-
0: 'DejaVu Sans',
734+
'0': 'DejaVu Sans',
740735
}
741736

742737

@@ -752,20 +747,20 @@ class StixFonts(UnicodeFonts):
752747
753748
- handles sized alternative characters for the STIXSizeX fonts.
754749
"""
755-
_fontmap: dict[str | int, str] = {
750+
_fontmap = {
756751
'rm': 'STIXGeneral',
757752
'it': 'STIXGeneral:italic',
758753
'bf': 'STIXGeneral:weight=bold',
759754
'bfit': 'STIXGeneral:italic:bold',
760755
'nonunirm': 'STIXNonUnicode',
761756
'nonuniit': 'STIXNonUnicode:italic',
762757
'nonunibf': 'STIXNonUnicode:weight=bold',
763-
0: 'STIXGeneral',
764-
1: 'STIXSizeOneSym',
765-
2: 'STIXSizeTwoSym',
766-
3: 'STIXSizeThreeSym',
767-
4: 'STIXSizeFourSym',
768-
5: 'STIXSizeFiveSym',
758+
'0': 'STIXGeneral',
759+
'1': 'STIXSizeOneSym',
760+
'2': 'STIXSizeTwoSym',
761+
'3': 'STIXSizeThreeSym',
762+
'4': 'STIXSizeFourSym',
763+
'5': 'STIXSizeFiveSym',
769764
}
770765
_fallback_font = None
771766
_sans = False
@@ -832,10 +827,8 @@ def _map_virtual_font(self, fontname: str, font_class: str,
832827
return fontname, uniindex
833828

834829
@functools.cache
835-
def get_sized_alternatives_for_symbol( # type: ignore[override]
836-
self,
837-
fontname: str,
838-
sym: str) -> list[tuple[str, str]] | list[tuple[int, str]]:
830+
def get_sized_alternatives_for_symbol(self, fontname: str,
831+
sym: str) -> list[tuple[str, str]]:
839832
fixes = {
840833
'\\{': '{', '\\}': '}', '\\[': '[', '\\]': ']',
841834
'<': '\N{MATHEMATICAL LEFT ANGLE BRACKET}',
@@ -846,8 +839,8 @@ def get_sized_alternatives_for_symbol( # type: ignore[override]
846839
uniindex = get_unicode_index(sym)
847840
except ValueError:
848841
return [(fontname, sym)]
849-
alternatives = [(i, chr(uniindex)) for i in range(6)
850-
if self._get_font(i).get_char_index(uniindex) != 0]
842+
alternatives = [(str(i), chr(uniindex)) for i in range(6)
843+
if self._get_font(str(i)).get_char_index(uniindex) != 0]
851844
# The largest size of the radical symbol in STIX has incorrect
852845
# metrics that cause it to be disconnected from the stem.
853846
if sym == r'\__sqrt__':
@@ -1542,7 +1535,7 @@ def __init__(self, c: str, height: float, depth: float, state: ParserState,
15421535
break
15431536

15441537
shift = 0.0
1545-
if state.font != 0 or len(alternatives) == 1:
1538+
if state.font != '0' or len(alternatives) == 1:
15461539
if factor is None:
15471540
factor = target_total / (char.height + char.depth)
15481541
state.fontsize *= factor
@@ -2530,7 +2523,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
25302523
# Handle regular sub/superscripts
25312524
constants = _get_font_constant_set(state)
25322525
lc_height = last_char.height
2533-
lc_baseline = 0
2526+
lc_baseline = 0.0
25342527
if self.is_dropsub(last_char):
25352528
lc_baseline = last_char.depth
25362529

lib/matplotlib/ft2font.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class FT2Font(Buffer):
194194
_kerning_factor: int = ...
195195
) -> None: ...
196196
if sys.version_info[:2] >= (3, 12):
197-
def __buffer__(self, flags: int) -> memoryview: ...
197+
def __buffer__(self, /, flags: int) -> memoryview: ...
198198
def _get_fontmap(self, string: str) -> dict[str, FT2Font]: ...
199199
def clear(self) -> None: ...
200200
def draw_glyph_to_bitmap(
@@ -286,7 +286,7 @@ class FT2Image(Buffer):
286286
def __init__(self, width: int, height: int) -> None: ...
287287
def draw_rect_filled(self, x0: int, y0: int, x1: int, y1: int) -> None: ...
288288
if sys.version_info[:2] >= (3, 12):
289-
def __buffer__(self, flags: int) -> memoryview: ...
289+
def __buffer__(self, /, flags: int) -> memoryview: ...
290290

291291
@final
292292
class Glyph:

0 commit comments

Comments
 (0)