Skip to content

Commit 96be1df

Browse files
Colton LathropQuLogic
authored andcommitted
Log a warning if selected font weight differs from requested
1 parent 4c345b4 commit 96be1df

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``font_manager.findfont`` logs if selected font weight does not match requested
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lib/matplotlib/font_manager.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@
172172
]
173173

174174

175+
def _normalize_weight(weight):
176+
return weight if isinstance(weight, Number) else weight_dict[weight]
177+
178+
175179
def get_fontext_synonyms(fontext):
176180
"""
177181
Return a list of file extensions that are synonyms for
@@ -1256,8 +1260,8 @@ def score_weight(self, weight1, weight2):
12561260
# exact match of the weight names, e.g. weight1 == weight2 == "regular"
12571261
if cbook._str_equal(weight1, weight2):
12581262
return 0.0
1259-
w1 = weight1 if isinstance(weight1, Number) else weight_dict[weight1]
1260-
w2 = weight2 if isinstance(weight2, Number) else weight_dict[weight2]
1263+
w1 = _normalize_weight(weight1)
1264+
w2 = _normalize_weight(weight2)
12611265
return 0.95 * (abs(w1 - w2) / 1000) + 0.05
12621266

12631267
def score_size(self, size1, size2):
@@ -1480,6 +1484,11 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
14801484
best_font = font
14811485
if score == 0:
14821486
break
1487+
if best_font is not None:
1488+
if (_normalize_weight(prop.get_weight()) !=
1489+
_normalize_weight(best_font.weight)):
1490+
_log.warning('findfont: Failed to find font weight %s, now using %s.',
1491+
prop.get_weight(), best_font.weight)
14831492

14841493
if best_font is None or best_score >= 10.0:
14851494
if fallback_to_default:

lib/matplotlib/tests/test_font_manager.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from matplotlib.font_manager import (
1616
findfont, findSystemFonts, FontEntry, FontProperties, fontManager,
1717
json_dump, json_load, get_font, is_opentype_cff_font,
18-
MSUserFontDirectories, _get_fontconfig_fonts, ttfFontProperty)
18+
MSUserFontDirectories, ttfFontProperty,
19+
_get_fontconfig_fonts, _normalize_weight)
1920
from matplotlib import cbook, ft2font, pyplot as plt, rc_context, figure as mfigure
2021
from matplotlib.testing import subprocess_run_helper, subprocess_run_for_testing
2122

@@ -407,3 +408,26 @@ def test_fontproperties_init_deprecation():
407408
# Since this case is not covered by docs, I've refrained from jumping
408409
# extra hoops to detect this possible API misuse.
409410
FontProperties(family="serif-24:style=oblique:weight=bold")
411+
412+
413+
def test_map_weights():
414+
assert _normalize_weight('ultralight') == 100
415+
assert _normalize_weight('light') == 200
416+
assert _normalize_weight('normal') == 400
417+
assert _normalize_weight('regular') == 400
418+
assert _normalize_weight('book') == 400
419+
assert _normalize_weight('medium') == 500
420+
assert _normalize_weight('roman') == 500
421+
assert _normalize_weight('semibold') == 600
422+
assert _normalize_weight('demibold') == 600
423+
assert _normalize_weight('demi') == 600
424+
assert _normalize_weight('bold') == 700
425+
assert _normalize_weight('heavy') == 800
426+
assert _normalize_weight('extra bold') == 800
427+
assert _normalize_weight('black') == 900
428+
429+
430+
def test_font_match_warning(caplog):
431+
findfont(FontProperties(family=["DejaVu Sans"], weight=750))
432+
logs = [rec.message for rec in caplog.records]
433+
assert 'findfont: Failed to find font weight 750, now using 700.' in logs

0 commit comments

Comments
 (0)