Skip to content

Commit 73dde94

Browse files
QuLogicmeeseeksmachine
authored andcommitted
Backport PR #24655: Update font_manager to only use registry on Win
1 parent 425320f commit 73dde94

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
On Windows only fonts known to the registry will be discovered
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Previously, Matplotlib would recursively walk user and system font directories
5+
to discover fonts, however this lead to a number of undesirable behaviors
6+
including finding deleted fonts. Now Matplotlib will only find fonts that are
7+
known to the Windows registry.
8+
9+
This means that any user installed fonts must go through the Windows font
10+
installer rather than simply being copied to the correct folder.
11+
12+
This only impacts the set of fonts Matplotlib will consider when using
13+
`matplotlib.font_manager.findfont`. To use an arbitrary font, directly pass the
14+
path to a font as shown in
15+
:doc:`/gallery/text_labels_and_annotations/font_file`.

lib/matplotlib/font_manager.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,11 @@ def list_fonts(directory, extensions):
186186
recursively under the directory.
187187
"""
188188
extensions = ["." + ext for ext in extensions]
189-
if sys.platform == 'win32' and directory == win32FontDirectory():
190-
return [os.path.join(directory, filename)
191-
for filename in os.listdir(directory)
192-
if os.path.isfile(os.path.join(directory, filename))]
193-
else:
194-
return [os.path.join(dirpath, filename)
195-
# os.walk ignores access errors, unlike Path.glob.
196-
for dirpath, _, filenames in os.walk(directory)
197-
for filename in filenames
198-
if Path(filename).suffix.lower() in extensions]
189+
return [os.path.join(dirpath, filename)
190+
# os.walk ignores access errors, unlike Path.glob.
191+
for dirpath, _, filenames in os.walk(directory)
192+
for filename in filenames
193+
if Path(filename).suffix.lower() in extensions]
199194

200195

201196
def win32FontDirectory():
@@ -275,7 +270,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
275270
if fontpaths is None:
276271
if sys.platform == 'win32':
277272
installed_fonts = _get_win32_installed_fonts()
278-
fontpaths = MSUserFontDirectories + [win32FontDirectory()]
273+
fontpaths = []
279274
else:
280275
installed_fonts = _get_fontconfig_fonts()
281276
if sys.platform == 'darwin':

lib/matplotlib/tests/test_font_manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_user_fonts_win32():
200200
pytest.xfail("This test should only run on CI (appveyor or azure) "
201201
"as the developer's font directory should remain "
202202
"unchanged.")
203-
203+
pytest.xfail("We need to update the registry for this test to work")
204204
font_test_file = 'mpltest.ttf'
205205

206206
# Precondition: the test font should not be available

0 commit comments

Comments
 (0)