Skip to content

Commit 9e67574

Browse files
committed
Add validation for fontstretch
1 parent 3377656 commit 9e67574

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

doc/api/font_manager_api.rst

+4
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
:undoc-members:
88
:show-inheritance:
99

10+
.. data:: fontManager
11+
12+
The global instance of `FontManager`.
13+
1014
.. autoclass:: FontEntry
1115
:no-undoc-members:

lib/matplotlib/font_manager.py

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
The design is based on the `W3C Cascading Style Sheet, Level 1 (CSS1)
1212
font specification <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_.
1313
Future versions may implement the Level 2 or 2.1 specifications.
14-
15-
.. data:: fontManager
16-
17-
The singleton instance of `FontManager`.
1814
"""
1915

2016
# KNOWN ISSUES

lib/matplotlib/rcsetup.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,20 @@ def validate_fontweight(s):
387387
raise ValueError(f'{s} is not a valid font weight.') from e
388388

389389

390+
def validate_fontstretch(s):
391+
stretchvalues = [
392+
'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed',
393+
'normal', 'semi-expanded', 'expanded', 'extra-expanded',
394+
'ultra-expanded']
395+
# Note: Historically, stretchvalues have been case-sensitive in Matplotlib
396+
if s in stretchvalues:
397+
return s
398+
try:
399+
return int(s)
400+
except (ValueError, TypeError) as e:
401+
raise ValueError(f'{s} is not a valid font stretch.') from e
402+
403+
390404
def validate_font_properties(s):
391405
parse_fontconfig_pattern(s)
392406
return s
@@ -900,7 +914,7 @@ def _convert_validator_spec(key, conv):
900914
"font.family": validate_stringlist, # used by text object
901915
"font.style": validate_string,
902916
"font.variant": validate_string,
903-
"font.stretch": validate_string,
917+
"font.stretch": validate_fontstretch,
904918
"font.weight": validate_fontweight,
905919
"font.size": validate_float, # Base font size in points
906920
"font.serif": validate_stringlist,

lib/matplotlib/tests/test_font_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ def test_user_fonts_linux(tmpdir, monkeypatch):
176176
_get_fontconfig_fonts.cache_clear()
177177

178178

179-
def test_addfont():
179+
def test_addfont_as_path():
180+
"""Smoke test that addfont() accepts pathlib.Path."""
180181
font_test_file = 'mpltest.ttf'
181182
path = Path(__file__).parent / font_test_file
182-
# Add font using Path, which should not produce an error. See #22582
183183
fontManager.addfont(path)
184184

185185

lib/matplotlib/tests/test_rcparams.py

+21
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
_validate_color_or_linecolor,
2121
validate_cycler,
2222
validate_float,
23+
validate_fontstretch,
2324
validate_fontweight,
2425
validate_hatch,
2526
validate_hist_bins,
@@ -469,6 +470,26 @@ def test_validate_fontweight(weight, parsed_weight):
469470
assert validate_fontweight(weight) == parsed_weight
470471

471472

473+
@pytest.mark.parametrize('stretch, parsed_stretch', [
474+
('expanded', 'expanded'),
475+
('EXPANDED', ValueError), # stretch is case-sensitive
476+
(100, 100),
477+
('100', 100),
478+
(np.array(100), 100),
479+
# fractional fontweights are not defined. This should actually raise a
480+
# ValueError, but historically did not.
481+
(20.6, 20),
482+
('20.6', ValueError),
483+
([100], ValueError),
484+
])
485+
def test_validate_fontstretch(stretch, parsed_stretch):
486+
if parsed_stretch is ValueError:
487+
with pytest.raises(ValueError):
488+
validate_fontstretch(stretch)
489+
else:
490+
assert validate_fontstretch(stretch) == parsed_stretch
491+
492+
472493
def test_keymaps():
473494
key_list = [k for k in mpl.rcParams if 'keymap' in k]
474495
for k in key_list:

0 commit comments

Comments
 (0)