3434from collections import Iterable
3535from functools import lru_cache
3636import json
37+ import logging
3738import os
3839from pathlib import Path
3940import subprocess
4041import sys
4142from threading import Timer
4243import warnings
43- import logging
4444
4545from matplotlib import afm , cbook , ft2font , rcParams , get_cachedir
4646from matplotlib .fontconfig_pattern import (
130130]
131131
132132if not USE_FONTCONFIG and sys .platform != 'win32' :
133- home = os .environ .get ('HOME' )
134- if home is not None :
135- # user fonts on OSX
136- path = os .path .join (home , 'Library' , 'Fonts' )
137- OSXFontDirectories .append (path )
138- path = os .path .join (home , '.fonts' )
139- X11FontDirectories .append (path )
133+ OSXFontDirectories .append (str (Path .home () / "Library/Fonts" ))
134+ X11FontDirectories .append (str (Path .home () / ".fonts" ))
140135
141136
142137def get_fontext_synonyms (fontext ):
@@ -161,26 +156,20 @@ def list_fonts(directory, extensions):
161156
162157
163158def win32FontDirectory ():
164- """
159+ r """
165160 Return the user-specified font directory for Win32. This is
166161 looked up from the registry key::
167162
168- \\ \\ HKEY_CURRENT_USER\\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Explorer\\ Shell Folders\ \ Fonts
163+ \\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
169164
170165 If the key is not found, $WINDIR/Fonts will be returned.
171166 """
172167 import winreg
173168 try :
174- user = winreg .OpenKey (winreg .HKEY_CURRENT_USER , MSFolders )
175- try :
169+ with winreg .OpenKey (winreg .HKEY_CURRENT_USER , MSFolders ) as user :
176170 return winreg .QueryValueEx (user , 'Fonts' )[0 ]
177- except OSError :
178- pass # Fall through to default
179- finally :
180- winreg .CloseKey (user )
181171 except OSError :
182- pass # Fall through to default
183- return os .path .join (os .environ ['WINDIR' ], 'Fonts' )
172+ return os .path .join (os .environ ['WINDIR' ], 'Fonts' )
184173
185174
186175def win32InstalledFonts (directory = None , fontext = 'ttf' ):
@@ -198,33 +187,23 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
198187
199188 fontext = get_fontext_synonyms (fontext )
200189
201- key , items = None , set ()
190+ items = set ()
202191 for fontdir in MSFontDirectories :
203192 try :
204- local = winreg .OpenKey (winreg .HKEY_LOCAL_MACHINE , fontdir )
205- except OSError :
206- continue
207- if not local :
208- return list_fonts (directory , fontext )
209- try :
210- for j in range (winreg .QueryInfoKey (local )[1 ]):
211- try :
193+ with winreg .OpenKey (winreg .HKEY_LOCAL_MACHINE , fontdir ) as local :
194+ for j in range (winreg .QueryInfoKey (local )[1 ]):
212195 key , direc , tp = winreg .EnumValue (local , j )
213196 if not isinstance (direc , str ):
214197 continue
215198 # Work around for https://bugs.python.org/issue25778, which
216199 # is fixed in Py>=3.6.1.
217200 direc = direc .split ("\0 " , 1 )[0 ]
218- if not os .path .dirname (direc ):
219- direc = os .path .join (directory , direc )
220- direc = os .path .abspath (direc ).lower ()
221- if os .path .splitext (direc )[1 ][1 :] in fontext :
222- items .add (direc )
223- except (EnvironmentError , MemoryError , WindowsError ):
224- continue
225- return list (items )
226- finally :
227- winreg .CloseKey (local )
201+ path = Path (directory , direc ).resolve ()
202+ if path .suffix .lower () in fontext :
203+ items .add (str (path ))
204+ return list (items )
205+ except (OSError , MemoryError ):
206+ continue
228207 return None
229208
230209
@@ -261,7 +240,7 @@ def get_fontconfig_fonts(fontext='ttf'):
261240 """
262241 fontext = get_fontext_synonyms (fontext )
263242 return [fname for fname in _call_fc_list ()
264- if os . path . splitext (fname )[ 1 ] [1 :] in fontext ]
243+ if Path (fname ). suffix [1 :] in fontext ]
265244
266245
267246def findSystemFonts (fontpaths = None , fontext = 'ttf' ):
@@ -277,31 +256,21 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
277256
278257 if fontpaths is None :
279258 if sys .platform == 'win32' :
280- fontdir = win32FontDirectory ()
281-
282- fontpaths = [fontdir ]
259+ fontpaths = [win32FontDirectory ()]
283260 # now get all installed fonts directly...
284- for f in win32InstalledFonts (fontdir ):
285- base , ext = os .path .splitext (f )
286- if len (ext )> 1 and ext [1 :].lower () in fontexts :
287- fontfiles .add (f )
261+ fontfiles .update (win32InstalledFonts (fontext = fontext ))
288262 else :
289263 fontpaths = X11FontDirectories
264+ fontfiles .update (get_fontconfig_fonts (fontext ))
290265 # check for OS X & load its fonts if present
291266 if sys .platform == 'darwin' :
292- for f in OSXInstalledFonts (fontext = fontext ):
293- fontfiles .add (f )
294-
295- for f in get_fontconfig_fonts (fontext ):
296- fontfiles .add (f )
267+ fontfiles .update (OSXInstalledFonts (fontext = fontext ))
297268
298269 elif isinstance (fontpaths , str ):
299270 fontpaths = [fontpaths ]
300271
301272 for path in fontpaths :
302- files = list_fonts (path , fontexts )
303- for fname in files :
304- fontfiles .add (os .path .abspath (fname ))
273+ fontfiles .update (map (os .path .abspath , list_fonts (path , fontexts )))
305274
306275 return [fname for fname in fontfiles if os .path .exists (fname )]
307276
@@ -1222,16 +1191,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
12221191 else :
12231192 fontlist = self .ttflist
12241193
1225- if directory is not None :
1226- directory = os .path .normcase (directory )
1227-
12281194 best_score = 1e64
12291195 best_font = None
12301196
12311197 for font in fontlist :
12321198 if (directory is not None and
1233- os .path .commonprefix ([os .path .normcase (font .fname ),
1234- directory ]) != directory ):
1199+ Path (directory ) not in Path (font .fname ).parents ):
12351200 continue
12361201 # Matching family should have highest priority, so it is multiplied
12371202 # by 10.0
0 commit comments