Skip to content

Commit 40fdce6

Browse files
committed
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.
1 parent 53c5f88 commit 40fdce6

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

lib/matplotlib/font_manager.py

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -682,30 +682,21 @@ def __init__(self, family=None, style=None, variant=None, weight=None,
682682
stretch=None, size=None,
683683
fname=None, # if set, it's a hardcoded filename to use
684684
math_fontfamily=None):
685-
self._family = _normalize_font_family(rcParams['font.family'])
686-
self._slant = rcParams['font.style']
687-
self._variant = rcParams['font.variant']
688-
self._weight = rcParams['font.weight']
689-
self._stretch = rcParams['font.stretch']
690-
self._size = rcParams['font.size']
691-
self._file = None
692-
self.set_math_fontfamily(math_fontfamily)
693-
694-
if isinstance(family, str):
695-
# Treat family as a fontconfig pattern if it is the only
696-
# parameter provided.
697-
if (style is None and variant is None and weight is None and
698-
stretch is None and size is None and fname is None):
699-
self.set_fontconfig_pattern(family)
700-
return
701-
702685
self.set_family(family)
703686
self.set_style(style)
704687
self.set_variant(variant)
705688
self.set_weight(weight)
706689
self.set_stretch(stretch)
707690
self.set_file(fname)
708691
self.set_size(size)
692+
self.set_math_fontfamily(math_fontfamily)
693+
# Treat family as a fontconfig pattern if it is the only parameter
694+
# provided. Even in that case, call the other setters first to set
695+
# attributes not specified by the pattern to the rcParams defaults.
696+
if (isinstance(family, str)
697+
and style is None and variant is None and weight is None
698+
and stretch is None and size is None and fname is None):
699+
self.set_fontconfig_pattern(family)
709700

710701
@classmethod
711702
def _from_any(cls, arg):
@@ -736,7 +727,7 @@ def __hash__(self):
736727
self.get_variant(),
737728
self.get_weight(),
738729
self.get_stretch(),
739-
self.get_size_in_points(),
730+
self.get_size(),
740731
self.get_file(),
741732
self.get_math_fontfamily())
742733
return hash(l)
@@ -764,7 +755,6 @@ def get_style(self):
764755
Return the font style. Values are: 'normal', 'italic' or 'oblique'.
765756
"""
766757
return self._slant
767-
get_slant = get_style
768758

769759
def get_variant(self):
770760
"""
@@ -795,9 +785,6 @@ def get_size(self):
795785
"""
796786
return self._size
797787

798-
def get_size_in_points(self):
799-
return self._size
800-
801788
def get_file(self):
802789
"""
803790
Return the filename of the associated font.
@@ -824,8 +811,9 @@ def set_family(self, family):
824811
"""
825812
if family is None:
826813
family = rcParams['font.family']
827-
self._family = _normalize_font_family(family)
828-
set_name = set_family
814+
if isinstance(family, str):
815+
family = [family]
816+
self._family = family
829817

830818
def set_style(self, style):
831819
"""
@@ -835,7 +823,6 @@ def set_style(self, style):
835823
style = rcParams['font.style']
836824
_api.check_in_list(['normal', 'italic', 'oblique'], style=style)
837825
self._slant = style
838-
set_slant = set_style
839826

840827
def set_variant(self, variant):
841828
"""
@@ -967,6 +954,12 @@ def copy(self):
967954
"""Return a copy of self."""
968955
return copy.copy(self)
969956

957+
# Aliases
958+
set_name = set_family
959+
get_slant = get_style
960+
set_slant = set_style
961+
get_size_in_points = get_size
962+
970963

971964
class _JSONEncoder(json.JSONEncoder):
972965
def default(self, o):
@@ -1040,12 +1033,6 @@ def json_load(filename):
10401033
return json.load(fh, object_hook=_json_decode)
10411034

10421035

1043-
def _normalize_font_family(family):
1044-
if isinstance(family, str):
1045-
family = [family]
1046-
return family
1047-
1048-
10491036
class FontManager:
10501037
"""
10511038
On import, the `FontManager` singleton instance creates a list of ttf and

0 commit comments

Comments
 (0)