Skip to content

Commit 91a75d7

Browse files
timhoffmMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #13766: Search for fonts in XDG directory as well.
1 parent 5999431 commit 91a75d7

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

lib/matplotlib/font_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
# common application, not really useful
115115
"/usr/lib/openoffice/share/fonts/truetype/",
116116
# user fonts
117+
str(Path(os.environ.get('XDG_DATA_HOME',
118+
Path.home() / ".local/share")) / "fonts"),
117119
str(Path.home() / ".fonts"),
118120
]
119121

lib/matplotlib/tests/test_font_manager.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from matplotlib.font_manager import (
1313
findfont, findSystemFonts, FontProperties, fontManager, json_dump,
1414
json_load, get_font, get_fontconfig_fonts, is_opentype_cff_font,
15-
MSUserFontDirectories)
15+
MSUserFontDirectories, _call_fc_list)
1616
from matplotlib import pyplot as plt, rc_context
1717

1818
has_fclist = shutil.which('fc-list') is not None
@@ -131,7 +131,34 @@ def test_find_ttc():
131131
fig.savefig(BytesIO(), format="ps")
132132

133133

134-
def test_user_fonts():
134+
@pytest.mark.skipif(sys.platform != 'linux', reason='Linux only')
135+
def test_user_fonts_linux(tmpdir, monkeypatch):
136+
font_test_file = 'mpltest.ttf'
137+
138+
# Precondition: the test font should not be available
139+
fonts = findSystemFonts()
140+
if any(font_test_file in font for font in fonts):
141+
pytest.skip(f'{font_test_file} already exists in system fonts')
142+
143+
# Prepare a temporary user font directory
144+
user_fonts_dir = tmpdir.join('fonts')
145+
user_fonts_dir.ensure(dir=True)
146+
shutil.copyfile(Path(__file__).parent / font_test_file,
147+
user_fonts_dir.join(font_test_file))
148+
149+
with monkeypatch.context() as m:
150+
m.setenv('XDG_DATA_HOME', str(tmpdir))
151+
_call_fc_list.cache_clear()
152+
# Now, the font should be available
153+
fonts = findSystemFonts()
154+
assert any(font_test_file in font for font in fonts)
155+
156+
# Make sure the temporary directory is no longer cached.
157+
_call_fc_list.cache_clear()
158+
159+
160+
@pytest.mark.skipif(sys.platform != 'win32', reason='Windows only')
161+
def test_user_fonts_win32():
135162
if not os.environ.get('APPVEYOR', False):
136163
pytest.xfail('This test does only work on appveyor since user fonts '
137164
'are Windows specific and the developer\'s font '
@@ -141,7 +168,8 @@ def test_user_fonts():
141168

142169
# Precondition: the test font should not be available
143170
fonts = findSystemFonts()
144-
assert not any(font_test_file in font for font in fonts)
171+
if any(font_test_file in font for font in fonts):
172+
pytest.skip(f'{font_test_file} already exists in system fonts')
145173

146174
user_fonts_dir = MSUserFontDirectories[0]
147175

0 commit comments

Comments
 (0)