34
34
from collections import Iterable
35
35
from functools import lru_cache
36
36
import json
37
+ import logging
37
38
import os
39
+ from pathlib import Path
38
40
import subprocess
39
41
import sys
40
42
from threading import Timer
41
43
import warnings
42
- import logging
43
44
44
45
from matplotlib import afm , cbook , ft2font , rcParams , get_cachedir
45
46
from matplotlib .fontconfig_pattern import (
129
130
]
130
131
131
132
if not USE_FONTCONFIG and sys .platform != 'win32' :
132
- home = os .environ .get ('HOME' )
133
- if home is not None :
134
- # user fonts on OSX
135
- path = os .path .join (home , 'Library' , 'Fonts' )
136
- OSXFontDirectories .append (path )
137
- path = os .path .join (home , '.fonts' )
138
- X11FontDirectories .append (path )
133
+ OSXFontDirectories .append (str (Path .home () / "Library/Fonts" ))
134
+ X11FontDirectories .append (str (Path .home () / ".fonts" ))
139
135
140
136
141
137
def get_fontext_synonyms (fontext ):
@@ -151,34 +147,28 @@ def get_fontext_synonyms(fontext):
151
147
def list_fonts (directory , extensions ):
152
148
"""
153
149
Return a list of all fonts matching any of the extensions,
154
- possibly upper-cased, found recursively under the directory.
150
+ found recursively under the directory.
155
151
"""
156
- pattern = ';' . join ([ '*.%s;*.%s' % ( ext , ext . upper () )
157
- for ext in extensions ] )
158
- return cbook . listFiles ( directory , pattern )
152
+ return [ str ( path )
153
+ for path in Path (). rglob ( "*" )
154
+ if path . is_file () and path . suffix [ 1 :] in extensions ]
159
155
160
156
161
157
def win32FontDirectory ():
162
- """
158
+ r """
163
159
Return the user-specified font directory for Win32. This is
164
160
looked up from the registry key::
165
161
166
- \\ \\ HKEY_CURRENT_USER\\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Explorer\\ Shell Folders\ \ Fonts
162
+ \\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
167
163
168
164
If the key is not found, $WINDIR/Fonts will be returned.
169
165
"""
170
166
import winreg
171
167
try :
172
- user = winreg .OpenKey (winreg .HKEY_CURRENT_USER , MSFolders )
173
- try :
168
+ with winreg .OpenKey (winreg .HKEY_CURRENT_USER , MSFolders ) as user :
174
169
return winreg .QueryValueEx (user , 'Fonts' )[0 ]
175
- except OSError :
176
- pass # Fall through to default
177
- finally :
178
- winreg .CloseKey (user )
179
170
except OSError :
180
- pass # Fall through to default
181
- return os .path .join (os .environ ['WINDIR' ], 'Fonts' )
171
+ return os .path .join (os .environ ['WINDIR' ], 'Fonts' )
182
172
183
173
184
174
def win32InstalledFonts (directory = None , fontext = 'ttf' ):
@@ -196,37 +186,23 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
196
186
197
187
fontext = get_fontext_synonyms (fontext )
198
188
199
- key , items = None , set ()
189
+ items = set ()
200
190
for fontdir in MSFontDirectories :
201
191
try :
202
- local = winreg .OpenKey (winreg .HKEY_LOCAL_MACHINE , fontdir )
203
- except OSError :
204
- continue
205
- if not local :
206
- return list_fonts (directory , fontext )
207
- try :
208
- for j in range (winreg .QueryInfoKey (local )[1 ]):
209
- try :
192
+ with winreg .OpenKey (winreg .HKEY_LOCAL_MACHINE , fontdir ) as local :
193
+ for j in range (winreg .QueryInfoKey (local )[1 ]):
210
194
key , direc , tp = winreg .EnumValue (local , j )
211
195
if not isinstance (direc , str ):
212
196
continue
213
197
# Work around for https://bugs.python.org/issue25778, which
214
198
# is fixed in Py>=3.6.1.
215
199
direc = direc .split ("\0 " , 1 )[0 ]
216
- if not os .path .dirname (direc ):
217
- direc = os .path .join (directory , direc )
218
- direc = os .path .abspath (direc ).lower ()
219
- if os .path .splitext (direc )[1 ][1 :] in fontext :
220
- items .add (direc )
221
- except EnvironmentError :
222
- continue
223
- except WindowsError :
224
- continue
225
- except MemoryError :
226
- continue
227
- return list (items )
228
- finally :
229
- winreg .CloseKey (local )
200
+ path = Path (directory , direc ).resolve ()
201
+ if path .suffix .lower () in fontext :
202
+ items .add (str (path ))
203
+ return list (items )
204
+ except (OSError , MemoryError ):
205
+ continue
230
206
return None
231
207
232
208
@@ -242,6 +218,9 @@ def OSXInstalledFonts(directories=None, fontext='ttf'):
242
218
files = []
243
219
for path in directories :
244
220
if fontext is None :
221
+ cbook .warn_deprecated (
222
+ "3.0" , "Support for listing all files regardless of extension "
223
+ "is deprecated." )
245
224
files .extend (cbook .listFiles (path , '*' ))
246
225
else :
247
226
files .extend (list_fonts (path , fontext ))
@@ -271,7 +250,7 @@ def get_fontconfig_fonts(fontext='ttf'):
271
250
"""
272
251
fontext = get_fontext_synonyms (fontext )
273
252
return [fname for fname in _call_fc_list ()
274
- if os . path . splitext (fname )[ 1 ] [1 :] in fontext ]
253
+ if Path (fname ). suffix [1 :] in fontext ]
275
254
276
255
277
256
def findSystemFonts (fontpaths = None , fontext = 'ttf' ):
@@ -292,8 +271,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
292
271
fontpaths = [fontdir ]
293
272
# now get all installed fonts directly...
294
273
for f in win32InstalledFonts (fontdir ):
295
- base , ext = os .path .splitext (f )
296
- if len (ext )> 1 and ext [1 :].lower () in fontexts :
274
+ if Path (f ).suffix in fontexts :
297
275
fontfiles .add (f )
298
276
else :
299
277
fontpaths = X11FontDirectories
@@ -1238,16 +1216,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1238
1216
else :
1239
1217
fontlist = self .ttflist
1240
1218
1241
- if directory is not None :
1242
- directory = os .path .normcase (directory )
1243
-
1244
1219
best_score = 1e64
1245
1220
best_font = None
1246
1221
1247
1222
for font in fontlist :
1248
1223
if (directory is not None and
1249
- os .path .commonprefix ([os .path .normcase (font .fname ),
1250
- directory ]) != directory ):
1224
+ Path (directory ) not in Path (font .fname ).parents ):
1251
1225
continue
1252
1226
# Matching family should have highest priority, so it is multiplied
1253
1227
# by 10.0
0 commit comments