47
47
from collections import Iterable
48
48
from functools import lru_cache
49
49
import json
50
+ import logging
50
51
import os
52
+ from pathlib import Path
51
53
import sys
52
54
from threading import Timer
53
55
import warnings
54
- import logging
55
56
56
57
from matplotlib import afm , cbook , ft2font , rcParams , get_cachedir
57
58
from matplotlib .compat import subprocess
142
143
]
143
144
144
145
if 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" ))
152
148
153
149
154
150
def get_fontext_synonyms (fontext ):
@@ -164,19 +160,20 @@ def get_fontext_synonyms(fontext):
164
160
def list_fonts (directory , extensions ):
165
161
"""
166
162
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.
168
164
"""
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 )))]
172
169
173
170
174
171
def win32FontDirectory ():
175
- """
172
+ r """
176
173
Return the user-specified font directory for Win32. This is
177
174
looked up from the registry key::
178
175
179
- \\ \\ HKEY_CURRENT_USER\\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Explorer\\ Shell Folders\ \ Fonts
176
+ \\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
180
177
181
178
If the key is not found, $WINDIR/Fonts will be returned.
182
179
"""
@@ -230,16 +227,10 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
230
227
# Work around for https://bugs.python.org/issue25778, which
231
228
# is fixed in Py>=3.6.1.
232
229
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 ):
243
234
continue
244
235
return list (items )
245
236
finally :
@@ -259,6 +250,9 @@ def OSXInstalledFonts(directories=None, fontext='ttf'):
259
250
files = []
260
251
for path in directories :
261
252
if fontext is None :
253
+ cbook .warn_deprecated (
254
+ "3.0" , "Support for listing all files regardless of extension "
255
+ "is deprecated." )
262
256
files .extend (cbook .listFiles (path , '*' ))
263
257
else :
264
258
files .extend (list_fonts (path , fontext ))
@@ -295,7 +289,7 @@ def get_fontconfig_fonts(fontext='ttf'):
295
289
"""
296
290
fontext = get_fontext_synonyms (fontext )
297
291
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 ]
299
293
300
294
301
295
def findSystemFonts (fontpaths = None , fontext = 'ttf' ):
@@ -316,8 +310,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
316
310
fontpaths = [fontdir ]
317
311
# now get all installed fonts directly...
318
312
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 :
321
314
fontfiles .add (f )
322
315
else :
323
316
fontpaths = X11FontDirectories
@@ -1302,16 +1295,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1302
1295
else :
1303
1296
fontlist = self .ttflist
1304
1297
1305
- if directory is not None :
1306
- directory = os .path .normcase (directory )
1307
-
1308
1298
best_score = 1e64
1309
1299
best_font = None
1310
1300
1311
1301
for font in fontlist :
1312
1302
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 ):
1315
1304
continue
1316
1305
# Matching family should have highest priority, so it is multiplied
1317
1306
# by 10.0
0 commit comments