Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ci/mypy-stubtest-allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ matplotlib\.ticker\.LogitLocator\.nonsingular

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

# 3.6 Pending deprecations
matplotlib\.figure\.Figure\.set_constrained_layout
Expand Down
75 changes: 34 additions & 41 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,20 +337,15 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
# Per-instance cache.
self._get_info = functools.cache(self._get_info) # type: ignore[method-assign]
self._fonts = {}
self.fontmap: dict[str | int, str] = {}
self.fontmap: dict[str, str] = {}

filename = findfont(self.default_font_prop)
default_font = get_font(filename)
self._fonts['default'] = default_font
self._fonts['regular'] = default_font

def _get_font(self, font: str | int) -> FT2Font:
if font in self.fontmap:
basename = self.fontmap[font]
else:
# NOTE: An int is only passed by subclasses which have placed int keys into
# `self.fontmap`, so we must cast this to confirm it to typing.
basename = T.cast(str, font)
def _get_font(self, font: str) -> FT2Font:
basename = self.fontmap.get(font, font)
cached_font = self._fonts.get(basename)
if cached_font is None and os.path.exists(basename):
cached_font = get_font(basename)
Expand Down Expand Up @@ -580,12 +575,13 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
# include STIX sized alternatives for glyphs if fallback is STIX
if isinstance(self._fallback_font, StixFonts):
stixsizedaltfonts = {
0: 'STIXGeneral',
1: 'STIXSizeOneSym',
2: 'STIXSizeTwoSym',
3: 'STIXSizeThreeSym',
4: 'STIXSizeFourSym',
5: 'STIXSizeFiveSym'}
'0': 'STIXGeneral',
'1': 'STIXSizeOneSym',
'2': 'STIXSizeTwoSym',
'3': 'STIXSizeThreeSym',
'4': 'STIXSizeFourSym',
'5': 'STIXSizeFiveSym',
}

for size, name in stixsizedaltfonts.items():
fullpath = findfont(name)
Expand Down Expand Up @@ -643,7 +639,7 @@ def _get_glyph(self, fontname: str, font_class: str,

g = self._fallback_font._get_glyph(fontname, font_class, sym)
family = g[0].family_name
if family in list(BakomaFonts._fontmap.values()):
if family in BakomaFonts._fontmap.values():
family = "Computer Modern"
_log.info("Substituting symbol %s from %s", sym, family)
return g
Expand All @@ -664,13 +660,12 @@ def _get_glyph(self, fontname: str, font_class: str,
def get_sized_alternatives_for_symbol(self, fontname: str,
sym: str) -> list[tuple[str, str]]:
if self._fallback_font:
return self._fallback_font.get_sized_alternatives_for_symbol(
fontname, sym)
return self._fallback_font.get_sized_alternatives_for_symbol(fontname, sym)
return [(fontname, sym)]


class DejaVuFonts(UnicodeFonts, metaclass=abc.ABCMeta):
_fontmap: dict[str | int, str] = {}
_fontmap: dict[str, str] = {}

def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlags):
# This must come first so the backend's owner is set correctly
Expand All @@ -682,11 +677,11 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
TruetypeFonts.__init__(self, default_font_prop, load_glyph_flags)
# Include Stix sized alternatives for glyphs
self._fontmap.update({
1: 'STIXSizeOneSym',
2: 'STIXSizeTwoSym',
3: 'STIXSizeThreeSym',
4: 'STIXSizeFourSym',
5: 'STIXSizeFiveSym',
'1': 'STIXSizeOneSym',
'2': 'STIXSizeTwoSym',
'3': 'STIXSizeThreeSym',
'4': 'STIXSizeFourSym',
'5': 'STIXSizeFiveSym',
})
for key, name in self._fontmap.items():
fullpath = findfont(name)
Expand Down Expand Up @@ -724,7 +719,7 @@ class DejaVuSerifFonts(DejaVuFonts):
'sf': 'DejaVu Sans',
'tt': 'DejaVu Sans Mono',
'ex': 'DejaVu Serif Display',
0: 'DejaVu Serif',
'0': 'DejaVu Serif',
}


Expand All @@ -742,7 +737,7 @@ class DejaVuSansFonts(DejaVuFonts):
'sf': 'DejaVu Sans',
'tt': 'DejaVu Sans Mono',
'ex': 'DejaVu Sans Display',
0: 'DejaVu Sans',
'0': 'DejaVu Sans',
}


Expand All @@ -758,20 +753,20 @@ class StixFonts(UnicodeFonts):

- handles sized alternative characters for the STIXSizeX fonts.
"""
_fontmap: dict[str | int, str] = {
_fontmap = {
'rm': 'STIXGeneral',
'it': 'STIXGeneral:italic',
'bf': 'STIXGeneral:weight=bold',
'bfit': 'STIXGeneral:italic:bold',
'nonunirm': 'STIXNonUnicode',
'nonuniit': 'STIXNonUnicode:italic',
'nonunibf': 'STIXNonUnicode:weight=bold',
0: 'STIXGeneral',
1: 'STIXSizeOneSym',
2: 'STIXSizeTwoSym',
3: 'STIXSizeThreeSym',
4: 'STIXSizeFourSym',
5: 'STIXSizeFiveSym',
'0': 'STIXGeneral',
'1': 'STIXSizeOneSym',
'2': 'STIXSizeTwoSym',
'3': 'STIXSizeThreeSym',
'4': 'STIXSizeFourSym',
'5': 'STIXSizeFiveSym',
}
_fallback_font = None
_sans = False
Expand Down Expand Up @@ -838,10 +833,8 @@ def _map_virtual_font(self, fontname: str, font_class: str,
return fontname, uniindex

@functools.cache
def get_sized_alternatives_for_symbol( # type: ignore[override]
self,
fontname: str,
sym: str) -> list[tuple[str, str]] | list[tuple[int, str]]:
def get_sized_alternatives_for_symbol(self, fontname: str,
sym: str) -> list[tuple[str, str]]:
fixes = {
'\\{': '{', '\\}': '}', '\\[': '[', '\\]': ']',
'<': '\N{MATHEMATICAL LEFT ANGLE BRACKET}',
Expand All @@ -852,8 +845,8 @@ def get_sized_alternatives_for_symbol( # type: ignore[override]
uniindex = get_unicode_index(sym)
except ValueError:
return [(fontname, sym)]
alternatives = [(i, chr(uniindex)) for i in range(6)
if self._get_font(i).get_char_index(uniindex) != 0]
alternatives = [(str(i), chr(uniindex)) for i in range(6)
if self._get_font(str(i)).get_char_index(uniindex) != 0]
# The largest size of the radical symbol in STIX has incorrect
# metrics that cause it to be disconnected from the stem.
if sym == r'\__sqrt__':
Expand Down Expand Up @@ -1171,7 +1164,7 @@ def __init__(self, elements: T.Sequence[Node]):
self.glue_sign = 0 # 0: normal, -1: shrinking, 1: stretching
self.glue_order = 0 # The order of infinity (0 - 3) for the glue

def __repr__(self):
def __repr__(self) -> str:
return "{}<w={:.02f} h={:.02f} d={:.02f} s={:.02f}>[{}]".format(
super().__repr__(),
self.width, self.height,
Expand Down Expand Up @@ -1542,7 +1535,7 @@ def __init__(self, c: str, height: float, depth: float, state: ParserState,
break

shift = 0.0
if state.font != 0 or len(alternatives) == 1:
if state.font != '0' or len(alternatives) == 1:
if factor is None:
factor = target_total / (char.height + char.depth)
state.fontsize *= factor
Expand Down Expand Up @@ -2528,7 +2521,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
# Handle regular sub/superscripts
consts = _get_font_constant_set(state)
lc_height = last_char.height
lc_baseline = 0
lc_baseline = 0.0
if self.is_dropsub(last_char):
lc_baseline = last_char.depth

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/ft2font.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class FT2Font(Buffer):
_kerning_factor: int = ...
) -> None: ...
if sys.version_info[:2] >= (3, 12):
def __buffer__(self, flags: int) -> memoryview: ...
def __buffer__(self, /, flags: int) -> memoryview: ...
def _get_fontmap(self, string: str) -> dict[str, FT2Font]: ...
def clear(self) -> None: ...
def draw_glyph_to_bitmap(
Expand Down Expand Up @@ -286,7 +286,7 @@ class FT2Image(Buffer):
def __init__(self, width: int, height: int) -> None: ...
def draw_rect_filled(self, x0: int, y0: int, x1: int, y1: int) -> None: ...
if sys.version_info[:2] >= (3, 12):
def __buffer__(self, flags: int) -> memoryview: ...
def __buffer__(self, /, flags: int) -> memoryview: ...

@final
class Glyph:
Expand Down
Loading