Skip to content

Commit 7c8ed22

Browse files
committed
Add abstract methods and ABC on Fonts classes
mypy rightfully complains that these methods called in the base class don't exist, so correctly indicate what should be implemented there. The ABC is not strictly necessary, but it helps with understanding as a lot of these have code that makes them seem like _not_ an ABC.
1 parent 63cfa16 commit 7c8ed22

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66

7+
import abc
78
import copy
89
import enum
910
import functools
@@ -216,7 +217,7 @@ class FontInfo(NamedTuple):
216217
offset: float
217218

218219

219-
class Fonts:
220+
class Fonts(abc.ABC):
220221
"""
221222
An abstract base class for a system of fonts to use for mathtext.
222223
@@ -248,6 +249,13 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
248249
"""
249250
return 0.
250251

252+
def _get_font(self, font: str) -> FT2Font:
253+
raise NotImplementedError
254+
255+
def _get_info(self, font: str, font_class: str, sym: str, fontsize: float,
256+
dpi: float) -> FontInfo:
257+
raise NotImplementedError
258+
251259
def get_metrics(self, font, font_class, sym, fontsize, dpi):
252260
r"""
253261
Parameters
@@ -313,7 +321,7 @@ def get_sized_alternatives_for_symbol(self, fontname, sym):
313321
return [(fontname, sym)]
314322

315323

316-
class TruetypeFonts(Fonts):
324+
class TruetypeFonts(Fonts, metaclass=abc.ABCMeta):
317325
"""
318326
A generic base class for all font setups that use Truetype fonts
319327
(through FT2Font).
@@ -324,6 +332,7 @@ def __init__(self, *args, **kwargs):
324332
# Per-instance cache.
325333
self._get_info = functools.cache(self._get_info)
326334
self._fonts = {}
335+
self.fontmap: dict[str | int, str] = {}
327336

328337
filename = findfont(self.default_font_prop)
329338
default_font = get_font(filename)
@@ -348,6 +357,10 @@ def _get_offset(self, font, glyph, fontsize, dpi):
348357
return (glyph.height / 64 / 2) + (fontsize/3 * dpi/72)
349358
return 0.
350359

360+
def _get_glyph(self, fontname: str, font_class: str,
361+
sym: str) -> tuple[FT2Font, int, bool]:
362+
raise NotImplementedError
363+
351364
# The return value of _get_info is cached per-instance.
352365
def _get_info(self, fontname, font_class, sym, fontsize, dpi):
353366
font, num, slanted = self._get_glyph(fontname, font_class, sym)
@@ -429,7 +442,6 @@ def __init__(self, *args, **kwargs):
429442
self._stix_fallback = StixFonts(*args, **kwargs)
430443

431444
super().__init__(*args, **kwargs)
432-
self.fontmap = {}
433445
for key, val in self._fontmap.items():
434446
fullpath = findfont(val)
435447
self.fontmap[key] = fullpath
@@ -543,7 +555,6 @@ def __init__(self, *args, **kwargs):
543555
self._fallback_font = font_cls(*args, **kwargs) if font_cls else None
544556

545557
super().__init__(*args, **kwargs)
546-
self.fontmap = {}
547558
for texfont in "cal rm tt it bf sf bfit".split():
548559
prop = mpl.rcParams['mathtext.' + texfont]
549560
font = findfont(prop)
@@ -641,7 +652,8 @@ def get_sized_alternatives_for_symbol(self, fontname, sym):
641652
return [(fontname, sym)]
642653

643654

644-
class DejaVuFonts(UnicodeFonts):
655+
class DejaVuFonts(UnicodeFonts, metaclass=abc.ABCMeta):
656+
_fontmap: dict[str | int, str] = {}
645657

646658
def __init__(self, *args, **kwargs):
647659
# This must come first so the backend's owner is set correctly
@@ -651,7 +663,6 @@ def __init__(self, *args, **kwargs):
651663
self._fallback_font = StixSansFonts(*args, **kwargs)
652664
self.bakoma = BakomaFonts(*args, **kwargs)
653665
TruetypeFonts.__init__(self, *args, **kwargs)
654-
self.fontmap = {}
655666
# Include Stix sized alternatives for glyphs
656667
self._fontmap.update({
657668
1: 'STIXSizeOneSym',
@@ -749,7 +760,6 @@ class StixFonts(UnicodeFonts):
749760

750761
def __init__(self, *args, **kwargs):
751762
TruetypeFonts.__init__(self, *args, **kwargs)
752-
self.fontmap = {}
753763
for key, name in self._fontmap.items():
754764
fullpath = findfont(name)
755765
self.fontmap[key] = fullpath

0 commit comments

Comments
 (0)