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

Skip to content

Commit 1fd871f

Browse files
committed
Search also for user fonts on Windows (#12954)
1 parent 5cdd2e5 commit 1fd871f

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

lib/matplotlib/font_manager.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@
9999
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts',
100100
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts']
101101

102+
MSUserFontDirectories = [
103+
os.path.join(str(Path.home()), r'AppData\Local\Microsoft\Windows\Fonts'),
104+
os.path.join(str(Path.home()), r'AppData\Roaming\Microsoft\Windows\Fonts')]
105+
102106
X11FontDirectories = [
103107
# an old standard installation point
104108
"/usr/X11R6/lib/X11/fonts/TTF/",
@@ -170,9 +174,9 @@ def win32FontDirectory():
170174
def win32InstalledFonts(directory=None, fontext='ttf'):
171175
"""
172176
Search for fonts in the specified font directory, or use the
173-
system directories if none given. A list of TrueType font
174-
filenames are returned by default, or AFM fonts if *fontext* ==
175-
'afm'.
177+
system directories if none given. Additionally, it is searched for user
178+
fonts installed. A list of TrueType font filenames are returned by default,
179+
or AFM fonts if *fontext* == 'afm'.
176180
"""
177181
import winreg
178182

@@ -183,24 +187,46 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
183187

184188
items = set()
185189
for fontdir in MSFontDirectories:
190+
# System fonts
186191
try:
187192
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, fontdir) as local:
188193
for j in range(winreg.QueryInfoKey(local)[1]):
189-
key, direc, tp = winreg.EnumValue(local, j)
190-
if not isinstance(direc, str):
194+
# Usually, value contains the name of the font file.
195+
key, value, tp = winreg.EnumValue(local, j)
196+
if not isinstance(value, str):
191197
continue
192198
# Work around for https://bugs.python.org/issue25778, which
193199
# is fixed in Py>=3.6.1.
194-
direc = direc.split("\0", 1)[0]
200+
value = value.split("\0", 1)[0]
201+
try:
202+
path = Path(directory, value).resolve()
203+
except RuntimeError:
204+
# Don't fail with invalid entries.
205+
continue
206+
if path.suffix.lower() in fontext:
207+
items.add(str(path))
208+
except (OSError, MemoryError):
209+
continue
210+
211+
# User fonts
212+
try:
213+
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, fontdir) as local:
214+
for j in range(winreg.QueryInfoKey(local)[1]):
215+
# Usually, value contains the absolute path to the font
216+
# file.
217+
key, value, tp = winreg.EnumValue(local, j)
218+
if not isinstance(value, str):
219+
continue
195220
try:
196-
path = Path(directory, direc).resolve()
221+
path = Path(value).resolve()
197222
except RuntimeError:
198223
# Don't fail with invalid entries.
199224
continue
200225
if path.suffix.lower() in fontext:
201226
items.add(str(path))
202227
except (OSError, MemoryError):
203228
continue
229+
204230
return list(items)
205231

206232

@@ -253,7 +279,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
253279

254280
if fontpaths is None:
255281
if sys.platform == 'win32':
256-
fontpaths = [win32FontDirectory()]
282+
fontpaths = MSUserFontDirectories + [win32FontDirectory()]
257283
# now get all installed fonts directly...
258284
fontfiles.update(win32InstalledFonts(fontext=fontext))
259285
else:

0 commit comments

Comments
 (0)