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

Skip to content

Commit ba82993

Browse files
authored
Merge pull request #18927 from anntzer/mf
Improve warning message for missing font family specified via alias.
2 parents 87c79e6 + 3f4172a commit ba82993

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

lib/matplotlib/font_manager.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,12 @@ def set_default_weight(self, weight):
11131113
"""
11141114
self.__default_weight = weight
11151115

1116+
@staticmethod
1117+
def _expand_aliases(family):
1118+
if family in ('sans', 'sans serif'):
1119+
family = 'sans-serif'
1120+
return rcParams['font.' + family]
1121+
11161122
# Each of the scoring functions below should return a value between
11171123
# 0.0 (perfect match) and 1.0 (terrible match)
11181124
def score_family(self, families, family2):
@@ -1135,10 +1141,7 @@ def score_family(self, families, family2):
11351141
for i, family1 in enumerate(families):
11361142
family1 = family1.lower()
11371143
if family1 in font_family_aliases:
1138-
if family1 in ('sans', 'sans serif'):
1139-
family1 = 'sans-serif'
1140-
options = rcParams['font.' + family1]
1141-
options = [x.lower() for x in options]
1144+
options = [*map(str.lower, self._expand_aliases(family1))]
11421145
if family2 in options:
11431146
idx = options.index(family2)
11441147
return (i + (idx / len(options))) * step
@@ -1341,6 +1344,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
13411344
_log.warning(
13421345
'findfont: Font family %s not found. Falling back to %s.',
13431346
prop.get_family(), self.defaultFamily[fontext])
1347+
for family in map(str.lower, prop.get_family()):
1348+
if family in font_family_aliases:
1349+
_log.warning(
1350+
"findfont: Generic family %r not found because "
1351+
"none of the following families were found: %s",
1352+
family, ", ".join(self._expand_aliases(family)))
13441353
default_prop = prop.copy()
13451354
default_prop.set_family(self.defaultFamily[fontext])
13461355
return self.findfont(default_prop, fontext, directory,

lib/matplotlib/tests/test_font_manager.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,15 @@ def test_fork():
210210
ctx = multiprocessing.get_context("fork")
211211
with ctx.Pool(processes=2) as pool:
212212
pool.map(_model_handler, range(2))
213+
214+
215+
def test_missing_family(caplog):
216+
plt.rcParams["font.sans-serif"] = ["this-font-does-not-exist"]
217+
with caplog.at_level("WARNING"):
218+
findfont("sans")
219+
assert [rec.getMessage() for rec in caplog.records] == [
220+
"findfont: Font family ['sans'] not found. "
221+
"Falling back to DejaVu Sans.",
222+
"findfont: Generic family 'sans' not found because none of the "
223+
"following families were found: this-font-does-not-exist",
224+
]

0 commit comments

Comments
 (0)