From 40fdce6fa259aa9ca7e11bc1b74e6431dd418a52 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 19 Feb 2022 13:34:03 +0100 Subject: [PATCH] Simplify FontProperties init. The various setters already set the None attributes to the rcParams values; no need to do this twice in `__init__`. Also inline the now-single-use _normalize_font_family, move the aliases all to a single block, and prefer get_size to get_size_in_points. --- lib/matplotlib/font_manager.py | 49 +++++++++++++--------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index e7f57b8485ad..e17d847ea87c 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -682,23 +682,6 @@ def __init__(self, family=None, style=None, variant=None, weight=None, stretch=None, size=None, fname=None, # if set, it's a hardcoded filename to use math_fontfamily=None): - self._family = _normalize_font_family(rcParams['font.family']) - self._slant = rcParams['font.style'] - self._variant = rcParams['font.variant'] - self._weight = rcParams['font.weight'] - self._stretch = rcParams['font.stretch'] - self._size = rcParams['font.size'] - self._file = None - self.set_math_fontfamily(math_fontfamily) - - if isinstance(family, str): - # Treat family as a fontconfig pattern if it is the only - # parameter provided. - if (style is None and variant is None and weight is None and - stretch is None and size is None and fname is None): - self.set_fontconfig_pattern(family) - return - self.set_family(family) self.set_style(style) self.set_variant(variant) @@ -706,6 +689,14 @@ def __init__(self, family=None, style=None, variant=None, weight=None, self.set_stretch(stretch) self.set_file(fname) self.set_size(size) + self.set_math_fontfamily(math_fontfamily) + # Treat family as a fontconfig pattern if it is the only parameter + # provided. Even in that case, call the other setters first to set + # attributes not specified by the pattern to the rcParams defaults. + if (isinstance(family, str) + and style is None and variant is None and weight is None + and stretch is None and size is None and fname is None): + self.set_fontconfig_pattern(family) @classmethod def _from_any(cls, arg): @@ -736,7 +727,7 @@ def __hash__(self): self.get_variant(), self.get_weight(), self.get_stretch(), - self.get_size_in_points(), + self.get_size(), self.get_file(), self.get_math_fontfamily()) return hash(l) @@ -764,7 +755,6 @@ def get_style(self): Return the font style. Values are: 'normal', 'italic' or 'oblique'. """ return self._slant - get_slant = get_style def get_variant(self): """ @@ -795,9 +785,6 @@ def get_size(self): """ return self._size - def get_size_in_points(self): - return self._size - def get_file(self): """ Return the filename of the associated font. @@ -824,8 +811,9 @@ def set_family(self, family): """ if family is None: family = rcParams['font.family'] - self._family = _normalize_font_family(family) - set_name = set_family + if isinstance(family, str): + family = [family] + self._family = family def set_style(self, style): """ @@ -835,7 +823,6 @@ def set_style(self, style): style = rcParams['font.style'] _api.check_in_list(['normal', 'italic', 'oblique'], style=style) self._slant = style - set_slant = set_style def set_variant(self, variant): """ @@ -967,6 +954,12 @@ def copy(self): """Return a copy of self.""" return copy.copy(self) + # Aliases + set_name = set_family + get_slant = get_style + set_slant = set_style + get_size_in_points = get_size + class _JSONEncoder(json.JSONEncoder): def default(self, o): @@ -1040,12 +1033,6 @@ def json_load(filename): return json.load(fh, object_hook=_json_decode) -def _normalize_font_family(family): - if isinstance(family, str): - family = [family] - return family - - class FontManager: """ On import, the `FontManager` singleton instance creates a list of ttf and