4
4
5
5
from __future__ import annotations
6
6
7
+ import abc
7
8
import copy
8
9
import enum
9
10
import functools
@@ -216,7 +217,7 @@ class FontInfo(NamedTuple):
216
217
offset : float
217
218
218
219
219
- class Fonts :
220
+ class Fonts ( abc . ABC ) :
220
221
"""
221
222
An abstract base class for a system of fonts to use for mathtext.
222
223
@@ -248,6 +249,13 @@ def get_kern(self, font1, fontclass1, sym1, fontsize1,
248
249
"""
249
250
return 0.
250
251
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
+
251
259
def get_metrics (self , font , font_class , sym , fontsize , dpi ):
252
260
r"""
253
261
Parameters
@@ -313,7 +321,7 @@ def get_sized_alternatives_for_symbol(self, fontname, sym):
313
321
return [(fontname , sym )]
314
322
315
323
316
- class TruetypeFonts (Fonts ):
324
+ class TruetypeFonts (Fonts , metaclass = abc . ABCMeta ):
317
325
"""
318
326
A generic base class for all font setups that use Truetype fonts
319
327
(through FT2Font).
@@ -324,6 +332,7 @@ def __init__(self, *args, **kwargs):
324
332
# Per-instance cache.
325
333
self ._get_info = functools .cache (self ._get_info )
326
334
self ._fonts = {}
335
+ self .fontmap : dict [str | int , str ] = {}
327
336
328
337
filename = findfont (self .default_font_prop )
329
338
default_font = get_font (filename )
@@ -348,6 +357,10 @@ def _get_offset(self, font, glyph, fontsize, dpi):
348
357
return (glyph .height / 64 / 2 ) + (fontsize / 3 * dpi / 72 )
349
358
return 0.
350
359
360
+ def _get_glyph (self , fontname : str , font_class : str ,
361
+ sym : str ) -> tuple [FT2Font , int , bool ]:
362
+ raise NotImplementedError
363
+
351
364
# The return value of _get_info is cached per-instance.
352
365
def _get_info (self , fontname , font_class , sym , fontsize , dpi ):
353
366
font , num , slanted = self ._get_glyph (fontname , font_class , sym )
@@ -429,7 +442,6 @@ def __init__(self, *args, **kwargs):
429
442
self ._stix_fallback = StixFonts (* args , ** kwargs )
430
443
431
444
super ().__init__ (* args , ** kwargs )
432
- self .fontmap = {}
433
445
for key , val in self ._fontmap .items ():
434
446
fullpath = findfont (val )
435
447
self .fontmap [key ] = fullpath
@@ -543,7 +555,6 @@ def __init__(self, *args, **kwargs):
543
555
self ._fallback_font = font_cls (* args , ** kwargs ) if font_cls else None
544
556
545
557
super ().__init__ (* args , ** kwargs )
546
- self .fontmap = {}
547
558
for texfont in "cal rm tt it bf sf bfit" .split ():
548
559
prop = mpl .rcParams ['mathtext.' + texfont ]
549
560
font = findfont (prop )
@@ -641,7 +652,8 @@ def get_sized_alternatives_for_symbol(self, fontname, sym):
641
652
return [(fontname , sym )]
642
653
643
654
644
- class DejaVuFonts (UnicodeFonts ):
655
+ class DejaVuFonts (UnicodeFonts , metaclass = abc .ABCMeta ):
656
+ _fontmap : dict [str | int , str ] = {}
645
657
646
658
def __init__ (self , * args , ** kwargs ):
647
659
# This must come first so the backend's owner is set correctly
@@ -651,7 +663,6 @@ def __init__(self, *args, **kwargs):
651
663
self ._fallback_font = StixSansFonts (* args , ** kwargs )
652
664
self .bakoma = BakomaFonts (* args , ** kwargs )
653
665
TruetypeFonts .__init__ (self , * args , ** kwargs )
654
- self .fontmap = {}
655
666
# Include Stix sized alternatives for glyphs
656
667
self ._fontmap .update ({
657
668
1 : 'STIXSizeOneSym' ,
@@ -749,7 +760,6 @@ class StixFonts(UnicodeFonts):
749
760
750
761
def __init__ (self , * args , ** kwargs ):
751
762
TruetypeFonts .__init__ (self , * args , ** kwargs )
752
- self .fontmap = {}
753
763
for key , name in self ._fontmap .items ():
754
764
fullpath = findfont (name )
755
765
self .fontmap [key ] = fullpath
0 commit comments