From c25d273844d07f2b29815c063ab823829111efd0 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 26 Mar 2019 04:38:16 -0400 Subject: [PATCH 1/3] Search for fonts in XDG directory as well. It currently checks ~/.fonts, but not ~/.local/share/fonts, which fontconfig does do. --- lib/matplotlib/font_manager.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index f8626a0c4cea..578ea4f62bb3 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -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"), ] From 4b5a2dba3ddd61f4be82a104d54e4ee44c5b2ae2 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 26 Mar 2019 05:17:06 -0400 Subject: [PATCH 2/3] Add a test that XDG font directories work. --- lib/matplotlib/tests/test_font_manager.py | 30 +++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_font_manager.py b/lib/matplotlib/tests/test_font_manager.py index 4296dc398416..bd8bdb6ab7a0 100644 --- a/lib/matplotlib/tests/test_font_manager.py +++ b/lib/matplotlib/tests/test_font_manager.py @@ -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 @@ -131,7 +131,33 @@ 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() + assert not any(font_test_file in font for font in 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 ' From 2f0dca959675b7f40223f44287d7b0e8a0359ea1 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 28 Mar 2019 00:29:28 -0400 Subject: [PATCH 3/3] Skip font test if font is already available. --- lib/matplotlib/tests/test_font_manager.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_font_manager.py b/lib/matplotlib/tests/test_font_manager.py index bd8bdb6ab7a0..5ac0ce7d7cae 100644 --- a/lib/matplotlib/tests/test_font_manager.py +++ b/lib/matplotlib/tests/test_font_manager.py @@ -137,7 +137,8 @@ def test_user_fonts_linux(tmpdir, monkeypatch): # 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') # Prepare a temporary user font directory user_fonts_dir = tmpdir.join('fonts') @@ -167,7 +168,8 @@ def test_user_fonts_win32(): # 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]