4646
4747from collections import Iterable
4848import json
49+ import logging
4950import os
51+ from pathlib import Path
5052import sys
5153from threading import Timer
5254import warnings
53- import logging
5455
5556from matplotlib import afm , cbook , ft2font , rcParams , get_cachedir
5657from matplotlib .compat import subprocess
146147]
147148
148149if not USE_FONTCONFIG and sys .platform != 'win32' :
149- home = os .environ .get ('HOME' )
150- if home is not None :
151- # user fonts on OSX
152- path = os .path .join (home , 'Library' , 'Fonts' )
153- OSXFontDirectories .append (path )
154- path = os .path .join (home , '.fonts' )
155- X11FontDirectories .append (path )
150+ OSXFontDirectories .append (str (Path .home () / "Library/Fonts" ))
151+ X11FontDirectories .append (str (Path .home () / ".fonts" ))
156152
157153
158154def get_fontext_synonyms (fontext ):
@@ -168,19 +164,20 @@ def get_fontext_synonyms(fontext):
168164def list_fonts (directory , extensions ):
169165 """
170166 Return a list of all fonts matching any of the extensions,
171- possibly upper-cased, found recursively under the directory.
167+ found recursively under the directory.
172168 """
173- pattern = ';' .join (['*.%s;*.%s' % (ext , ext .upper ())
174- for ext in extensions ])
175- return cbook .listFiles (directory , pattern )
169+ return [str (path )
170+ for ext in extensions
171+ for path in filter (Path .is_file ,
172+ Path (directory ).rglob ("*.{}" .format (ext )))]
176173
177174
178175def win32FontDirectory ():
179- """
176+ r """
180177 Return the user-specified font directory for Win32. This is
181178 looked up from the registry key::
182179
183- \\ \\ HKEY_CURRENT_USER\\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Explorer\\ Shell Folders\ \ Fonts
180+ \\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
184181
185182 If the key is not found, $WINDIR/Fonts will be returned.
186183 """
@@ -234,16 +231,10 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
234231 # Work around for https://bugs.python.org/issue25778, which
235232 # is fixed in Py>=3.6.1.
236233 direc = direc .split ("\0 " , 1 )[0 ]
237- if not os .path .dirname (direc ):
238- direc = os .path .join (directory , direc )
239- direc = os .path .abspath (direc ).lower ()
240- if os .path .splitext (direc )[1 ][1 :] in fontext :
241- items .add (direc )
242- except EnvironmentError :
243- continue
244- except WindowsError :
245- continue
246- except MemoryError :
234+ path = Path (directory , direc ).resolve ()
235+ if path .suffix .lower () in fontext :
236+ items .add (str (path ))
237+ except (OSError , MemoryError ):
247238 continue
248239 return list (items )
249240 finally :
@@ -263,6 +254,9 @@ def OSXInstalledFonts(directories=None, fontext='ttf'):
263254 files = []
264255 for path in directories :
265256 if fontext is None :
257+ cbook .warn_deprecated (
258+ "3.0" , "Support for listing all files regardless of extension "
259+ "is deprecated." )
266260 files .extend (cbook .listFiles (path , '*' ))
267261 else :
268262 files .extend (list_fonts (path , fontext ))
@@ -299,7 +293,7 @@ def get_fontconfig_fonts(fontext='ttf'):
299293 """
300294 fontext = get_fontext_synonyms (fontext )
301295 return [fname for fname in _call_fc_list ()
302- if os . path . splitext (fname )[ 1 ][ 1 :] in fontext ]
296+ if Path (fname ). suffix in fontext ]
303297
304298
305299def findSystemFonts (fontpaths = None , fontext = 'ttf' ):
@@ -320,8 +314,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
320314 fontpaths = [fontdir ]
321315 # now get all installed fonts directly...
322316 for f in win32InstalledFonts (fontdir ):
323- base , ext = os .path .splitext (f )
324- if len (ext )> 1 and ext [1 :].lower () in fontexts :
317+ if Path (f ).suffix in fontexts :
325318 fontfiles .add (f )
326319 else :
327320 fontpaths = X11FontDirectories
@@ -1290,16 +1283,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
12901283 else :
12911284 fontlist = self .ttflist
12921285
1293- if directory is not None :
1294- directory = os .path .normcase (directory )
1295-
12961286 best_score = 1e64
12971287 best_font = None
12981288
12991289 for font in fontlist :
13001290 if (directory is not None and
1301- os .path .commonprefix ([os .path .normcase (font .fname ),
1302- directory ]) != directory ):
1291+ Path (directory ) not in Path (font .fname ).parents ):
13031292 continue
13041293 # Matching family should have highest priority, so it is multiplied
13051294 # by 10.0
0 commit comments