Skip to content

Commit eab91a4

Browse files
committed
FIX: also include fonts from generic family in fallback
If we pass the generic family to `_findfont_cached` it will still only return 1 font. This is a problem if we need a glyph which is not in the first font in the generic font list that is actually installed on the system. This is not the most elegant code, but it works.
1 parent a48f1a2 commit eab91a4

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

lib/matplotlib/font_manager.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,27 +1402,48 @@ def _find_fonts_by_props(self, prop, fontext='ttf', directory=None,
14021402

14031403
fpaths = []
14041404
for family in prop.get_family():
1405-
cprop = prop.copy()
1406-
cprop.set_family(family) # set current prop's family
1405+
if family in font_family_aliases:
1406+
# deal with the sans/san-serif/sans serif issue
1407+
generic_family = {
1408+
'sans': 'sans-serif',
1409+
'sans serif': 'sans-serif'
1410+
}.get(family, family)
1411+
1412+
for fam in filter(
1413+
lambda _: _ not in font_family_aliases,
1414+
mpl.rcParams[f'font.{generic_family}']
1415+
):
1416+
cprop = prop.copy()
1417+
cprop.set_family(fam)
1418+
try:
1419+
# for each of the fonts in the fallback chain look it up
1420+
fpaths.append(
1421+
self.findfont(
1422+
cprop, fontext, directory,
1423+
fallback_to_default=False, # don't fallback to default
1424+
rebuild_if_missing=rebuild_if_missing,
1425+
)
1426+
)
1427+
1428+
except ValueError:
1429+
# but do not worry if one is missing
1430+
...
1431+
else:
1432+
cprop = prop.copy()
1433+
cprop.set_family(family) # set current prop's family
14071434

1408-
try:
1409-
fpaths.append(
1410-
self.findfont(
1411-
cprop, fontext, directory,
1412-
fallback_to_default=False, # don't fallback to default
1413-
rebuild_if_missing=rebuild_if_missing,
1414-
)
1415-
)
1416-
except ValueError:
1417-
if family in font_family_aliases:
1418-
_log.warning(
1419-
"findfont: Generic family %r not found because "
1420-
"none of the following families were found: %s",
1421-
family, ", ".join(self._expand_aliases(family))
1435+
try:
1436+
fpaths.append(
1437+
self.findfont(
1438+
cprop, fontext, directory,
1439+
fallback_to_default=False, # don't fallback to default
1440+
rebuild_if_missing=rebuild_if_missing,
1441+
)
14221442
)
1423-
else:
1443+
except ValueError:
14241444
_log.warning("findfont: Font family %r not found.", family)
14251445

1446+
14261447
# only add default family if no other font was found and
14271448
# fallback_to_default is enabled
14281449
if not fpaths:

0 commit comments

Comments
 (0)