Skip to content

Commit 63eba3b

Browse files
committed
Add validation for fontstretch
1 parent b57b39b commit 63eba3b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 15 additions & 1 deletion
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_rcparams.py

Lines changed: 21 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy