Skip to content

Backport PR #13766 on branch v3.1.x (Search for fonts in XDG directory as well.) #13804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
# common application, not really useful
"/usr/lib/openoffice/share/fonts/truetype/",
# user fonts
str(Path(os.environ.get('XDG_DATA_HOME',
Path.home() / ".local/share")) / "fonts"),
str(Path.home() / ".fonts"),
]

Expand Down
34 changes: 31 additions & 3 deletions lib/matplotlib/tests/test_font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from matplotlib.font_manager import (
findfont, findSystemFonts, FontProperties, fontManager, json_dump,
json_load, get_font, get_fontconfig_fonts, is_opentype_cff_font,
MSUserFontDirectories)
MSUserFontDirectories, _call_fc_list)
from matplotlib import pyplot as plt, rc_context

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


def test_user_fonts():
@pytest.mark.skipif(sys.platform != 'linux', reason='Linux only')
def test_user_fonts_linux(tmpdir, monkeypatch):
font_test_file = 'mpltest.ttf'

# Precondition: the test font should not be available
fonts = findSystemFonts()
if any(font_test_file in font for font in fonts):
pytest.skip(f'{font_test_file} already exists in system fonts')

# Prepare a temporary user font directory
user_fonts_dir = tmpdir.join('fonts')
user_fonts_dir.ensure(dir=True)
shutil.copyfile(Path(__file__).parent / font_test_file,
user_fonts_dir.join(font_test_file))

with monkeypatch.context() as m:
m.setenv('XDG_DATA_HOME', str(tmpdir))
_call_fc_list.cache_clear()
# Now, the font should be available
fonts = findSystemFonts()
assert any(font_test_file in font for font in fonts)

# Make sure the temporary directory is no longer cached.
_call_fc_list.cache_clear()


@pytest.mark.skipif(sys.platform != 'win32', reason='Windows only')
def test_user_fonts_win32():
if not os.environ.get('APPVEYOR', False):
pytest.xfail('This test does only work on appveyor since user fonts '
'are Windows specific and the developer\'s font '
Expand All @@ -141,7 +168,8 @@ def test_user_fonts():

# Precondition: the test font should not be available
fonts = findSystemFonts()
assert not any(font_test_file in font for font in fonts)
if any(font_test_file in font for font in fonts):
pytest.skip(f'{font_test_file} already exists in system fonts')

user_fonts_dir = MSUserFontDirectories[0]

Expand Down