@@ -171,63 +171,83 @@ def win32FontDirectory():
171171 return os .path .join (os .environ ['WINDIR' ], 'Fonts' )
172172
173173
174- def win32InstalledFonts (directory = None , fontext = 'ttf' ):
175- """
176- Search for fonts in the specified font directory, or use the
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'.
180- """
181- import winreg
174+ def win32RegistryFonts (reg_domain , base_dir ):
175+ r"""
176+ Searches for fonts in the Windows registry.
182177
183- if directory is None :
184- directory = win32FontDirectory ()
178+ Parameters
179+ ----------
180+ reg_domain : `int`
181+ The top level registry domain (e.g. HKEY_LOCAL_MACHINE).
185182
186- fontext = ['.' + ext for ext in get_fontext_synonyms (fontext )]
183+ base_dir : `string`
184+ The path to the folder where the font files are usually located (e.g.
185+ C:\Windows\Fonts). If only the filename of the font is stored in the
186+ registry, the absolute path is built relative to this base directory.
187187
188+ Returns
189+ -------
190+ `set`
191+ `pathlib.Path` objects with the absolute path to the font files found.
192+
193+ """
194+ import winreg
188195 items = set ()
189- for fontdir in MSFontDirectories :
190- # System fonts
196+
197+ for reg_path in MSFontDirectories :
191198 try :
192- with winreg .OpenKey (winreg . HKEY_LOCAL_MACHINE , fontdir ) as local :
199+ with winreg .OpenKey (reg_domain , reg_path ) as local :
193200 for j in range (winreg .QueryInfoKey (local )[1 ]):
194- # Usually, value contains the name of the font file.
201+ # value may contain the filename of the font or its
202+ # absolute path.
195203 key , value , tp = winreg .EnumValue (local , j )
196204 if not isinstance (value , str ):
197205 continue
206+
198207 # Work around for https://bugs.python.org/issue25778, which
199208 # is fixed in Py>=3.6.1.
200209 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
210210
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
220211 try :
221- path = Path (value ).resolve ()
212+ # If value contains already an absolute path, then it
213+ # is not changed further.
214+ path = Path (base_dir , value ).resolve ()
222215 except RuntimeError :
223216 # Don't fail with invalid entries.
224217 continue
225- if path . suffix . lower () in fontext :
226- items .add (str ( path ) )
218+
219+ items .add (path )
227220 except (OSError , MemoryError ):
228221 continue
229222
230- return list (items )
223+ return items
224+
225+
226+ def win32InstalledFonts (directory = None , fontext = 'ttf' ):
227+ """
228+ Search for fonts in the specified font directory, or use the
229+ system directories if none given. Additionally, it is searched for user
230+ fonts installed. A list of TrueType font filenames are returned by default,
231+ or AFM fonts if *fontext* == 'afm'.
232+ """
233+ import winreg
234+
235+ if directory is None :
236+ directory = win32FontDirectory ()
237+
238+ fontext = ['.' + ext for ext in get_fontext_synonyms (fontext )]
239+
240+ items = set ()
241+
242+ # System fonts
243+ items .update (win32RegistryFonts (winreg .HKEY_LOCAL_MACHINE , directory ))
244+
245+ # User fonts
246+ for userdir in MSUserFontDirectories :
247+ items .update (win32RegistryFonts (winreg .HKEY_CURRENT_USER , userdir ))
248+
249+ # Keep only paths with matching file extension.
250+ return [str (path ) for path in items if path .suffix .lower () in fontext ]
231251
232252
233253@cbook .deprecated ("3.1" )
0 commit comments