Skip to content

Simplify FontProperties init. #22504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 18 additions & 31 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,30 +682,21 @@ 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)
self.set_weight(weight)
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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down