Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit249f360

Browse files
committed
Add validation for fontstretch
1 parent3377656 commit249f360

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

‎doc/api/font_manager_api.rst

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
A module for finding, managing, and using fonts across platforms.
33
4-
This module provides a single `FontManager` instance, `fontManager`, that can
4+
This module provides a single `FontManager` instance,:data:`fontManager`, that can
55
be shared across backends and platforms. The `findfont`
66
function returns the best TrueType (TTF) font file in the local or
77
system font path that matches the specified `FontProperties`
@@ -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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,20 @@ def validate_fontweight(s):
387387
raiseValueError(f'{s} is not a valid font weight.')frome
388388

389389

390+
defvalidate_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+
ifsinstretchvalues:
397+
returns
398+
try:
399+
returnint(s)
400+
except (ValueError,TypeError)ase:
401+
raiseValueError(f'{s} is not a valid font stretch.')frome
402+
403+
390404
defvalidate_font_properties(s):
391405
parse_fontconfig_pattern(s)
392406
returns
@@ -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

Lines changed: 2 additions & 2 deletions
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-
deftest_addfont():
179+
deftest_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

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
assertvalidate_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+
deftest_validate_fontstretch(stretch,parsed_stretch):
486+
ifparsed_stretchisValueError:
487+
withpytest.raises(ValueError):
488+
validate_fontstretch(stretch)
489+
else:
490+
assertvalidate_fontstretch(stretch)==parsed_stretch
491+
492+
472493
deftest_keymaps():
473494
key_list= [kforkinmpl.rcParamsif'keymap'ink]
474495
forkinkey_list:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp