46
46
47
47
from collections import Iterable
48
48
import json
49
+ import logging
49
50
import os
51
+ from pathlib import Path
50
52
import sys
51
53
from threading import Timer
52
54
import warnings
53
- import logging
54
55
55
56
from matplotlib import afm , cbook , ft2font , rcParams , get_cachedir
56
57
from matplotlib .compat import subprocess
146
147
]
147
148
148
149
if 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" ))
156
152
157
153
158
154
def get_fontext_synonyms (fontext ):
@@ -168,19 +164,20 @@ def get_fontext_synonyms(fontext):
168
164
def list_fonts (directory , extensions ):
169
165
"""
170
166
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.
172
168
"""
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 )))]
176
173
177
174
178
175
def win32FontDirectory ():
179
- """
176
+ r """
180
177
Return the user-specified font directory for Win32. This is
181
178
looked up from the registry key::
182
179
183
- \\ \\ HKEY_CURRENT_USER\\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Explorer\\ Shell Folders\ \ Fonts
180
+ \\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
184
181
185
182
If the key is not found, $WINDIR/Fonts will be returned.
186
183
"""
@@ -234,16 +231,10 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
234
231
# Work around for https://bugs.python.org/issue25778, which
235
232
# is fixed in Py>=3.6.1.
236
233
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 ):
247
238
continue
248
239
return list (items )
249
240
finally :
@@ -263,6 +254,9 @@ def OSXInstalledFonts(directories=None, fontext='ttf'):
263
254
files = []
264
255
for path in directories :
265
256
if fontext is None :
257
+ cbook .warn_deprecated (
258
+ "3.0" , "Support for listing all files regardless of extension "
259
+ "is deprecated." )
266
260
files .extend (cbook .listFiles (path , '*' ))
267
261
else :
268
262
files .extend (list_fonts (path , fontext ))
@@ -299,7 +293,7 @@ def get_fontconfig_fonts(fontext='ttf'):
299
293
"""
300
294
fontext = get_fontext_synonyms (fontext )
301
295
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 ]
303
297
304
298
305
299
def findSystemFonts (fontpaths = None , fontext = 'ttf' ):
@@ -320,8 +314,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
320
314
fontpaths = [fontdir ]
321
315
# now get all installed fonts directly...
322
316
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 :
325
318
fontfiles .add (f )
326
319
else :
327
320
fontpaths = X11FontDirectories
@@ -1290,16 +1283,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1290
1283
else :
1291
1284
fontlist = self .ttflist
1292
1285
1293
- if directory is not None :
1294
- directory = os .path .normcase (directory )
1295
-
1296
1286
best_score = 1e64
1297
1287
best_font = None
1298
1288
1299
1289
for font in fontlist :
1300
1290
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 ):
1303
1292
continue
1304
1293
# Matching family should have highest priority, so it is multiplied
1305
1294
# by 10.0
0 commit comments