37
37
38
38
if T .TYPE_CHECKING :
39
39
from collections .abc import Iterable
40
- from .ft2font import Glyph
40
+ from .ft2font import CharacterCodeType , Glyph
41
+
41
42
42
43
ParserElement .enable_packrat ()
43
44
_log = logging .getLogger ("matplotlib.mathtext" )
47
48
# FONTS
48
49
49
50
50
- def get_unicode_index (symbol : str ) -> int : # Publicly exported.
51
+ def get_unicode_index (symbol : str ) -> CharacterCodeType : # Publicly exported.
51
52
r"""
52
53
Return the integer index (from the Unicode table) of *symbol*.
53
54
@@ -85,7 +86,7 @@ class VectorParse(NamedTuple):
85
86
width : float
86
87
height : float
87
88
depth : float
88
- glyphs : list [tuple [FT2Font , float , int , float , float ]]
89
+ glyphs : list [tuple [FT2Font , float , CharacterCodeType , float , float ]]
89
90
rects : list [tuple [float , float , float , float ]]
90
91
91
92
VectorParse .__module__ = "matplotlib.mathtext"
@@ -212,7 +213,7 @@ class FontInfo(NamedTuple):
212
213
fontsize : float
213
214
postscript_name : str
214
215
metrics : FontMetrics
215
- num : int
216
+ num : CharacterCodeType
216
217
glyph : Glyph
217
218
offset : float
218
219
@@ -365,7 +366,7 @@ def _get_offset(self, font: FT2Font, glyph: Glyph, fontsize: float,
365
366
return 0.
366
367
367
368
def _get_glyph (self , fontname : str , font_class : str ,
368
- sym : str ) -> tuple [FT2Font , int , bool ]:
369
+ sym : str ) -> tuple [FT2Font , CharacterCodeType , bool ]:
369
370
raise NotImplementedError
370
371
371
372
# The return value of _get_info is cached per-instance.
@@ -425,7 +426,9 @@ def get_kern(self, font1: str, fontclass1: str, sym1: str, fontsize1: float,
425
426
info1 = self ._get_info (font1 , fontclass1 , sym1 , fontsize1 , dpi )
426
427
info2 = self ._get_info (font2 , fontclass2 , sym2 , fontsize2 , dpi )
427
428
font = info1 .font
428
- return font .get_kerning (info1 .num , info2 .num , Kerning .DEFAULT ) / 64
429
+ return font .get_kerning (font .get_char_index (info1 .num ),
430
+ font .get_char_index (info2 .num ),
431
+ Kerning .DEFAULT ) / 64
429
432
return super ().get_kern (font1 , fontclass1 , sym1 , fontsize1 ,
430
433
font2 , fontclass2 , sym2 , fontsize2 , dpi )
431
434
@@ -459,7 +462,7 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
459
462
_slanted_symbols = set (r"\int \oint" .split ())
460
463
461
464
def _get_glyph (self , fontname : str , font_class : str ,
462
- sym : str ) -> tuple [FT2Font , int , bool ]:
465
+ sym : str ) -> tuple [FT2Font , CharacterCodeType , bool ]:
463
466
font = None
464
467
if fontname in self .fontmap and sym in latex_to_bakoma :
465
468
basename , num = latex_to_bakoma [sym ]
@@ -551,7 +554,7 @@ class UnicodeFonts(TruetypeFonts):
551
554
# Some glyphs are not present in the `cmr10` font, and must be brought in
552
555
# from `cmsy10`. Map the Unicode indices of those glyphs to the indices at
553
556
# which they are found in `cmsy10`.
554
- _cmr10_substitutions = {
557
+ _cmr10_substitutions : dict [ CharacterCodeType , CharacterCodeType ] = {
555
558
0x00D7 : 0x00A3 , # Multiplication sign.
556
559
0x2212 : 0x00A1 , # Minus sign.
557
560
}
@@ -594,11 +597,11 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
594
597
_slanted_symbols = set (r"\int \oint" .split ())
595
598
596
599
def _map_virtual_font (self , fontname : str , font_class : str ,
597
- uniindex : int ) -> tuple [str , int ]:
600
+ uniindex : CharacterCodeType ) -> tuple [str , CharacterCodeType ]:
598
601
return fontname , uniindex
599
602
600
603
def _get_glyph (self , fontname : str , font_class : str ,
601
- sym : str ) -> tuple [FT2Font , int , bool ]:
604
+ sym : str ) -> tuple [FT2Font , CharacterCodeType , bool ]:
602
605
try :
603
606
uniindex = get_unicode_index (sym )
604
607
found_symbol = True
@@ -607,8 +610,7 @@ def _get_glyph(self, fontname: str, font_class: str,
607
610
found_symbol = False
608
611
_log .warning ("No TeX to Unicode mapping for %a." , sym )
609
612
610
- fontname , uniindex = self ._map_virtual_font (
611
- fontname , font_class , uniindex )
613
+ fontname , uniindex = self ._map_virtual_font (fontname , font_class , uniindex )
612
614
613
615
new_fontname = fontname
614
616
@@ -693,7 +695,7 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
693
695
self .fontmap [name ] = fullpath
694
696
695
697
def _get_glyph (self , fontname : str , font_class : str ,
696
- sym : str ) -> tuple [FT2Font , int , bool ]:
698
+ sym : str ) -> tuple [FT2Font , CharacterCodeType , bool ]:
697
699
# Override prime symbol to use Bakoma.
698
700
if sym == r'\prime' :
699
701
return self .bakoma ._get_glyph (fontname , font_class , sym )
@@ -783,7 +785,7 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
783
785
self .fontmap [name ] = fullpath
784
786
785
787
def _map_virtual_font (self , fontname : str , font_class : str ,
786
- uniindex : int ) -> tuple [str , int ]:
788
+ uniindex : CharacterCodeType ) -> tuple [str , CharacterCodeType ]:
787
789
# Handle these "fonts" that are actually embedded in
788
790
# other fonts.
789
791
font_mapping = stix_virtual_fonts .get (fontname )
@@ -1170,7 +1172,7 @@ def __init__(self, elements: T.Sequence[Node]):
1170
1172
self .glue_sign = 0 # 0: normal, -1: shrinking, 1: stretching
1171
1173
self .glue_order = 0 # The order of infinity (0 - 3) for the glue
1172
1174
1173
- def __repr__ (self ):
1175
+ def __repr__ (self ) -> str :
1174
1176
return "{}<w={:.02f} h={:.02f} d={:.02f} s={:.02f}>[{}]" .format (
1175
1177
super ().__repr__ (),
1176
1178
self .width , self .height ,
0 commit comments