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

Skip to content

Commit 8137091

Browse files
committed
mathtext refactoring: replace FontConstants lookup
Replace lookup table for font constants based on the family name by methods in their respective classes. Removes non-local call of private _get_font method in _get_font_constant_set. This simplifies implementing dynamically loaded font constants.
1 parent d4bace4 commit 8137091

1 file changed

Lines changed: 22 additions & 43 deletions

File tree

lib/matplotlib/_mathtext.py

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ def get_sized_alternatives_for_symbol(self, fontname: str,
341341
"""
342342
return [(fontname, sym)]
343343

344+
def get_font_constants(self) -> type[FontConstantsBase]:
345+
return FontConstantsBase
346+
344347

345348
class TruetypeFonts(Fonts, metaclass=abc.ABCMeta):
346349
"""
@@ -420,7 +423,7 @@ def _get_info(self, fontname: str, font_class: str, sym: str, fontsize: float,
420423
)
421424

422425
def get_axis_height(self, fontname: str, fontsize: float, dpi: float) -> float:
423-
consts = _get_font_constants(self, fontname)
426+
consts = self.get_font_constants()
424427
if consts.axis_height is not None:
425428
return consts.axis_height * fontsize * dpi / 72
426429
else:
@@ -431,7 +434,7 @@ def get_axis_height(self, fontname: str, fontsize: float, dpi: float) -> float:
431434
return (metrics.ymax + metrics.ymin) / 2
432435

433436
def get_quad(self, fontname: str, fontsize: float, dpi: float) -> float:
434-
consts = _get_font_constants(self, fontname)
437+
consts = self.get_font_constants()
435438
if consts.quad is not None:
436439
return consts.quad * fontsize * dpi / 72
437440
else:
@@ -551,6 +554,9 @@ def get_sized_alternatives_for_symbol(self, fontname: str,
551554
sym: str) -> list[tuple[str, str]]:
552555
return self._size_alternatives.get(sym, [(fontname, sym)])
553556

557+
def get_font_constants(self) -> type[FontConstantsBase]:
558+
return ComputerModernFontConstants
559+
554560

555561
class UnicodeFonts(TruetypeFonts):
556562
"""
@@ -741,6 +747,9 @@ class DejaVuSerifFonts(DejaVuFonts):
741747
0: 'DejaVu Serif',
742748
}
743749

750+
def get_font_constants(self) -> type[FontConstantsBase]:
751+
return DejaVuSerifFontConstants
752+
744753

745754
class DejaVuSansFonts(DejaVuFonts):
746755
"""
@@ -759,6 +768,9 @@ class DejaVuSansFonts(DejaVuFonts):
759768
0: 'DejaVu Sans',
760769
}
761770

771+
def get_font_constants(self) -> type[FontConstantsBase]:
772+
return DejaVuSansFontConstants
773+
762774

763775
class StixFonts(UnicodeFonts):
764776
"""
@@ -874,6 +886,12 @@ def get_sized_alternatives_for_symbol( # type: ignore[override]
874886
alternatives = alternatives[:-1]
875887
return alternatives
876888

889+
def get_font_constants(self) -> type[FontConstantsBase]:
890+
if self._sans:
891+
return STIXSansFontConstants
892+
else:
893+
return STIXFontConstants
894+
877895

878896
class StixSansFonts(StixFonts):
879897
"""
@@ -1078,45 +1096,6 @@ class DejaVuSansFontConstants(FontConstantsBase):
10781096
axis_height = 512 / 2048
10791097

10801098

1081-
# Maps font family names to the FontConstantBase subclass to use
1082-
_font_constant_mapping = {
1083-
'DejaVu Sans': DejaVuSansFontConstants,
1084-
'DejaVu Sans Mono': DejaVuSansFontConstants,
1085-
'DejaVu Serif': DejaVuSerifFontConstants,
1086-
'cmb10': ComputerModernFontConstants,
1087-
'cmex10': ComputerModernFontConstants,
1088-
'cmmi10': ComputerModernFontConstants,
1089-
'cmr10': ComputerModernFontConstants,
1090-
'cmss10': ComputerModernFontConstants,
1091-
'cmsy10': ComputerModernFontConstants,
1092-
'cmtt10': ComputerModernFontConstants,
1093-
'STIXGeneral': STIXFontConstants,
1094-
'STIXNonUnicode': STIXFontConstants,
1095-
'STIXSizeFiveSym': STIXFontConstants,
1096-
'STIXSizeFourSym': STIXFontConstants,
1097-
'STIXSizeThreeSym': STIXFontConstants,
1098-
'STIXSizeTwoSym': STIXFontConstants,
1099-
'STIXSizeOneSym': STIXFontConstants,
1100-
# Map the fonts we used to ship, just for good measure
1101-
'Bitstream Vera Sans': DejaVuSansFontConstants,
1102-
'Bitstream Vera': DejaVuSansFontConstants,
1103-
}
1104-
1105-
1106-
def _get_font_constants(fontset: Fonts, font: str) -> type[FontConstantsBase]:
1107-
constants = _font_constant_mapping.get(fontset._get_font(font).family_name,
1108-
FontConstantsBase)
1109-
# STIX sans isn't really its own fonts, just different code points
1110-
# in the STIX fonts, so we have to detect this one separately.
1111-
if constants is STIXFontConstants and isinstance(fontset, StixSansFonts):
1112-
return STIXSansFontConstants
1113-
return constants
1114-
1115-
1116-
def _get_font_constant_set(state: ParserState) -> type[FontConstantsBase]:
1117-
return _get_font_constants(state.fontset, state.font)
1118-
1119-
11201099
class Node:
11211100
"""A node in the TeX box model."""
11221101

@@ -2649,7 +2628,7 @@ def subsuper(self, s: str, loc: int, toks: ParseResults) -> T.Any:
26492628
nucleus = Hlist([nucleus])
26502629

26512630
# Handle regular sub/superscripts
2652-
consts = _get_font_constant_set(state)
2631+
consts = state.fontset.get_font_constants()
26532632
lc_height = last_char.height
26542633
lc_baseline = 0
26552634
if self.is_dropsub(last_char):
@@ -2743,7 +2722,7 @@ def _genfrac(self, ldelim: str, rdelim: str, rule: float | None, style: _MathSty
27432722

27442723
axis_height = state.fontset.get_axis_height(
27452724
state.font, state.fontsize, state.dpi)
2746-
consts = _get_font_constant_set(state)
2725+
consts = state.fontset.get_font_constants()
27472726
x_height = state.fontset.get_xheight(state.font, state.fontsize, state.dpi)
27482727

27492728
for _ in range(style.value):

0 commit comments

Comments
 (0)