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 subprocess
52
54
import sys
53
55
from threading import Timer
54
56
import warnings
55
- import logging
56
57
57
58
from matplotlib import afm , cbook , ft2font , rcParams , get_cachedir
58
59
from matplotlib .fontconfig_pattern import (
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,19 @@ 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 path in Path (). rglob ( "*" )
167
+ if path . is_file () and path . suffix [ 1 :] in extensions ]
172
168
173
169
174
170
def win32FontDirectory ():
175
- """
171
+ r """
176
172
Return the user-specified font directory for Win32. This is
177
173
looked up from the registry key::
178
174
179
- \\ \\ HKEY_CURRENT_USER\\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Explorer\\ Shell Folders\ \ Fonts
175
+ \\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
180
176
181
177
If the key is not found, $WINDIR/Fonts will be returned.
182
178
"""
@@ -230,16 +226,10 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
230
226
# Work around for https://bugs.python.org/issue25778, which
231
227
# is fixed in Py>=3.6.1.
232
228
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 :
229
+ path = Path (directory , direc ).resolve ()
230
+ if path .suffix .lower () in fontext :
231
+ items .add (str (path ))
232
+ except (OSError , MemoryError ):
243
233
continue
244
234
return list (items )
245
235
finally :
@@ -259,6 +249,9 @@ def OSXInstalledFonts(directories=None, fontext='ttf'):
259
249
files = []
260
250
for path in directories :
261
251
if fontext is None :
252
+ cbook .warn_deprecated (
253
+ "3.0" , "Support for listing all files regardless of extension "
254
+ "is deprecated." )
262
255
files .extend (cbook .listFiles (path , '*' ))
263
256
else :
264
257
files .extend (list_fonts (path , fontext ))
@@ -295,7 +288,7 @@ def get_fontconfig_fonts(fontext='ttf'):
295
288
"""
296
289
fontext = get_fontext_synonyms (fontext )
297
290
return [fname for fname in _call_fc_list ()
298
- if os . path . splitext (fname )[ 1 ] [1 :] in fontext ]
291
+ if Path (fname ). suffix [1 :] in fontext ]
299
292
300
293
301
294
def findSystemFonts (fontpaths = None , fontext = 'ttf' ):
@@ -316,8 +309,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
316
309
fontpaths = [fontdir ]
317
310
# now get all installed fonts directly...
318
311
for f in win32InstalledFonts (fontdir ):
319
- base , ext = os .path .splitext (f )
320
- if len (ext )> 1 and ext [1 :].lower () in fontexts :
312
+ if Path (f ).suffix in fontexts :
321
313
fontfiles .add (f )
322
314
else :
323
315
fontpaths = X11FontDirectories
@@ -1302,16 +1294,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1302
1294
else :
1303
1295
fontlist = self .ttflist
1304
1296
1305
- if directory is not None :
1306
- directory = os .path .normcase (directory )
1307
-
1308
1297
best_score = 1e64
1309
1298
best_font = None
1310
1299
1311
1300
for font in fontlist :
1312
1301
if (directory is not None and
1313
- os .path .commonprefix ([os .path .normcase (font .fname ),
1314
- directory ]) != directory ):
1302
+ Path (directory ) not in Path (font .fname ).parents ):
1315
1303
continue
1316
1304
# Matching family should have highest priority, so it is multiplied
1317
1305
# by 10.0
0 commit comments