4747from collections import Iterable
4848from functools import lru_cache
4949import json
50+ import logging
5051import os
52+ from pathlib import Path
5153import sys
5254from threading import Timer
5355import warnings
54- import logging
5556
5657from matplotlib import afm , cbook , ft2font , rcParams , get_cachedir
5758from matplotlib .compat import subprocess
142143]
143144
144145if not USE_FONTCONFIG and sys .platform != 'win32' :
145- home = os .environ .get ('HOME' )
146- if home is not None :
147- # user fonts on OSX
148- path = os .path .join (home , 'Library' , 'Fonts' )
149- OSXFontDirectories .append (path )
150- path = os .path .join (home , '.fonts' )
151- X11FontDirectories .append (path )
146+ OSXFontDirectories .append (str (Path .home () / "Library/Fonts" ))
147+ X11FontDirectories .append (str (Path .home () / ".fonts" ))
152148
153149
154150def get_fontext_synonyms (fontext ):
@@ -164,19 +160,20 @@ def get_fontext_synonyms(fontext):
164160def list_fonts (directory , extensions ):
165161 """
166162 Return a list of all fonts matching any of the extensions,
167- possibly upper-cased, found recursively under the directory.
163+ found recursively under the directory.
168164 """
169- pattern = ';' .join (['*.%s;*.%s' % (ext , ext .upper ())
170- for ext in extensions ])
171- return cbook .listFiles (directory , pattern )
165+ return [str (path )
166+ for ext in extensions
167+ for path in filter (Path .is_file ,
168+ Path (directory ).rglob ("*.{}" .format (ext )))]
172169
173170
174171def win32FontDirectory ():
175- """
172+ r """
176173 Return the user-specified font directory for Win32. This is
177174 looked up from the registry key::
178175
179- \\ \\ HKEY_CURRENT_USER\\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Explorer\\ Shell Folders\ \ Fonts
176+ \\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
180177
181178 If the key is not found, $WINDIR/Fonts will be returned.
182179 """
@@ -230,16 +227,10 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
230227 # Work around for https://bugs.python.org/issue25778, which
231228 # is fixed in Py>=3.6.1.
232229 direc = direc .split ("\0 " , 1 )[0 ]
233- if not os .path .dirname (direc ):
234- direc = os .path .join (directory , direc )
235- direc = os .path .abspath (direc ).lower ()
236- if os .path .splitext (direc )[1 ][1 :] in fontext :
237- items .add (direc )
238- except EnvironmentError :
239- continue
240- except WindowsError :
241- continue
242- except MemoryError :
230+ path = Path (directory , direc ).resolve ()
231+ if path .suffix .lower () in fontext :
232+ items .add (str (path ))
233+ except (OSError , MemoryError ):
243234 continue
244235 return list (items )
245236 finally :
@@ -259,6 +250,9 @@ def OSXInstalledFonts(directories=None, fontext='ttf'):
259250 files = []
260251 for path in directories :
261252 if fontext is None :
253+ cbook .warn_deprecated (
254+ "3.0" , "Support for listing all files regardless of extension "
255+ "is deprecated." )
262256 files .extend (cbook .listFiles (path , '*' ))
263257 else :
264258 files .extend (list_fonts (path , fontext ))
@@ -295,7 +289,7 @@ def get_fontconfig_fonts(fontext='ttf'):
295289 """
296290 fontext = get_fontext_synonyms (fontext )
297291 return [fname for fname in _call_fc_list ()
298- if os . path . splitext (fname )[ 1 ] [1 :] in fontext ]
292+ if Path (fname ). suffix [1 :] in fontext ]
299293
300294
301295def findSystemFonts (fontpaths = None , fontext = 'ttf' ):
@@ -316,8 +310,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
316310 fontpaths = [fontdir ]
317311 # now get all installed fonts directly...
318312 for f in win32InstalledFonts (fontdir ):
319- base , ext = os .path .splitext (f )
320- if len (ext )> 1 and ext [1 :].lower () in fontexts :
313+ if Path (f ).suffix in fontexts :
321314 fontfiles .add (f )
322315 else :
323316 fontpaths = X11FontDirectories
@@ -1302,16 +1295,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
13021295 else :
13031296 fontlist = self .ttflist
13041297
1305- if directory is not None :
1306- directory = os .path .normcase (directory )
1307-
13081298 best_score = 1e64
13091299 best_font = None
13101300
13111301 for font in fontlist :
13121302 if (directory is not None and
1313- os .path .commonprefix ([os .path .normcase (font .fname ),
1314- directory ]) != directory ):
1303+ Path (directory ) not in Path (font .fname ).parents ):
13151304 continue
13161305 # Matching family should have highest priority, so it is multiplied
13171306 # by 10.0
0 commit comments