Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a92cb01

Browse files
committed
ENH: Add an environment variable to ignore system fonts
1 parent e6640db commit a92cb01

6 files changed

Lines changed: 47 additions & 8 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
New environment variable to ignore system fonts
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
System fonts may be ignored by setting the :envvar:`MPL_IGNORE_SYSTEM_FONTS`; this
5+
suppresses searching for system fonts (in known directories or via some
6+
platform-specific subprocess) as well as limiting the results from `.FontManager.findfont`.

doc/install/environment_variables_faq.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ Environment variables
2929
used to find a base directory in which the :file:`matplotlib` subdirectory is
3030
created.
3131

32+
.. envvar:: MPL_IGNORE_SYSTEM_FONTS
33+
34+
When this variable is set, Matplotlib will update its font cache only with its own
35+
directories. This can be used to limit subprocess usage for querying system fonts.
36+
3237
.. envvar:: PATH
3338

3439
The list of directories searched to find executable programs.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
New environment variable to ignore system fonts
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
System fonts may be ignored by setting the :envvar:`MPL_IGNORE_SYSTEM_FONTS`; this
5+
suppresses searching for system fonts (in known directories or via some
6+
platform-specific subprocess) as well as limiting the results from `.FontManager.findfont`.

lib/matplotlib/font_manager.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,31 @@ def _get_macos_fonts():
277277

278278
def findSystemFonts(fontpaths=None, fontext='ttf'):
279279
"""
280-
Search for fonts in the specified font paths. If no paths are
281-
given, will use a standard set of system paths, as well as the
282-
list of fonts tracked by fontconfig if fontconfig is installed and
283-
available. A list of TrueType fonts are returned by default with
284-
AFM fonts as an option.
280+
Find fonts in a search path, system paths, or some other platform-specific method.
281+
282+
Parameters
283+
----------
284+
fontpaths : list of str, optional
285+
Search for fonts in these specified font paths. If no paths are given and the
286+
:envvar:`MPL_IGNORE_SYSTEM_FONTS` is not set, use a standard set of system
287+
paths, as well as the list of fonts tracked by fontconfig if fontconfig is
288+
installed and available.
289+
fontext : {'ttf', 'afm'}, default: 'ttf'
290+
If 'ttf', search for TrueType fonts; if 'afm', search for with AFM fonts.
291+
292+
Returns
293+
-------
294+
list of str
295+
A list of file paths with fonts of the given type.
285296
"""
286297
fontfiles = set()
287298
fontexts = get_fontext_synonyms(fontext)
288299

289300
if fontpaths is None:
290-
if sys.platform == 'win32':
301+
if os.getenv('MPL_IGNORE_SYSTEM_FONTS'):
302+
installed_fonts = []
303+
fontpaths = []
304+
elif sys.platform == 'win32':
291305
installed_fonts = _get_win32_installed_fonts()
292306
fontpaths = []
293307
elif sys.platform == 'emscripten':
@@ -1465,6 +1479,8 @@ def findfont(self, prop, fontext='ttf', directory=None,
14651479
14661480
directory : str, optional
14671481
If given, only search this directory and its subdirectories.
1482+
If :envvar:`MPL_IGNORE_SYSTEM_FONTS` is set, then this defaults to
1483+
Matplotlib's internal font directory.
14681484
14691485
fallback_to_default : bool
14701486
If True, will fall back to the default font family (usually
@@ -1506,6 +1522,8 @@ def findfont(self, prop, fontext='ttf', directory=None,
15061522
"serif", "sans-serif", "cursive", "fantasy", "monospace"]]
15071523
rc_params = tuple(tuple(e) if isinstance(e, list) else e
15081524
for e in rc_params) # Make this hashable.
1525+
if directory is None and os.getenv('MPL_IGNORE_SYSTEM_FONTS'):
1526+
directory = cbook._get_data_path('fonts')
15091527
ret = self._findfont_cached(
15101528
prop, fontext, directory, fallback_to_default, rebuild_if_missing,
15111529
rc_params)

lib/matplotlib/font_manager.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _get_font_alt_names(
2727
font: ft2font.FT2Font, primary_name: str
2828
) -> list[tuple[str, int]]: ...
2929
def findSystemFonts(
30-
fontpaths: Iterable[str | os.PathLike] | None = ..., fontext: str = ...
30+
fontpaths: Iterable[str | os.PathLike] | None = ..., fontext: Literal['ttf', 'afm'] = ...
3131
) -> list[str]: ...
3232

3333
class FontPath(str):

lib/matplotlib/tests/test_font_manager.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,16 @@ def test_user_fonts_linux(tmpdir, monkeypatch):
248248
_get_fontconfig_fonts.cache_clear()
249249

250250

251-
def test_addfont_as_path():
251+
def test_addfont_as_path(monkeypatch):
252252
"""Smoke test that addfont() accepts pathlib.Path."""
253253
font_test_file = 'mpltest.ttf'
254254
path = Path(__file__).parent / 'data' / font_test_file
255255
try:
256256
fontManager.addfont(path)
257+
assert fontManager.findfont('mpltest:weight=500') == FontPath(path, 0)
258+
with monkeypatch.context() as m, pytest.raises(ValueError):
259+
m.setenv('MPL_IGNORE_SYSTEM_FONTS', 'true') # Can only find internal fonts.
260+
fontManager.findfont('mpltest:weight=500', fallback_to_default=False)
257261
added, = (font for font in fontManager.ttflist
258262
if font.fname.endswith(font_test_file))
259263
fontManager.ttflist.remove(added)

0 commit comments

Comments
 (0)