99
99
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' ,
100
100
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts' ]
101
101
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
+
102
106
X11FontDirectories = [
103
107
# an old standard installation point
104
108
"/usr/X11R6/lib/X11/fonts/TTF/" ,
@@ -170,9 +174,9 @@ def win32FontDirectory():
170
174
def win32InstalledFonts (directory = None , fontext = 'ttf' ):
171
175
"""
172
176
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'.
176
180
"""
177
181
import winreg
178
182
@@ -183,24 +187,46 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
183
187
184
188
items = set ()
185
189
for fontdir in MSFontDirectories :
190
+ # System fonts
186
191
try :
187
192
with winreg .OpenKey (winreg .HKEY_LOCAL_MACHINE , fontdir ) as local :
188
193
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 ):
191
197
continue
192
198
# Work around for https://bugs.python.org/issue25778, which
193
199
# 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
195
220
try :
196
- path = Path (directory , direc ).resolve ()
221
+ path = Path (value ).resolve ()
197
222
except RuntimeError :
198
223
# Don't fail with invalid entries.
199
224
continue
200
225
if path .suffix .lower () in fontext :
201
226
items .add (str (path ))
202
227
except (OSError , MemoryError ):
203
228
continue
229
+
204
230
return list (items )
205
231
206
232
@@ -253,7 +279,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
253
279
254
280
if fontpaths is None :
255
281
if sys .platform == 'win32' :
256
- fontpaths = [win32FontDirectory ()]
282
+ fontpaths = MSUserFontDirectories + [win32FontDirectory ()]
257
283
# now get all installed fonts directly...
258
284
fontfiles .update (win32InstalledFonts (fontext = fontext ))
259
285
else :
0 commit comments