42
42
see license/LICENSE_TTFQUERY.
43
43
"""
44
44
45
- import os , sys , glob , subprocess , warnings
45
+ import os , sys , subprocess , warnings
46
46
try :
47
47
set
48
48
except NameError :
52
52
from matplotlib import ft2font
53
53
from matplotlib import rcParams , get_configdir
54
54
from matplotlib .cbook import is_string_like
55
+ import matplotlib .cbook as cbook
55
56
from matplotlib .fontconfig_pattern import \
56
57
parse_fontconfig_pattern , generate_fontconfig_pattern
57
58
@@ -155,6 +156,15 @@ def get_fontext_synonyms(fontext):
155
156
'otf' : ('ttf' , 'otf' ),
156
157
'afm' : ('afm' ,)}[fontext ]
157
158
159
+ def list_fonts (directory , extensions ):
160
+ """
161
+ Return a list of all fonts matching any of the extensions,
162
+ possibly upper-cased, found recursively under the directory.
163
+ """
164
+ pattern = ';' .join (['*.%s;*.%s' % (ext , ext .upper ())
165
+ for ext in extensions ])
166
+ return cbook .listFiles (directory , pattern )
167
+
158
168
def win32FontDirectory ():
159
169
"""
160
170
Return the user-specified font directory for Win32. This is
@@ -204,10 +214,7 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
204
214
continue
205
215
206
216
if not local :
207
- files = []
208
- for ext in fontext :
209
- files .extend (glob .glob (os .path .join (directory , '*.' + ext )))
210
- return files
217
+ return list_fonts (directory , fontext )
211
218
try :
212
219
for j in range (_winreg .QueryInfoKey (local )[1 ]):
213
220
try :
@@ -227,65 +234,24 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
227
234
_winreg .CloseKey (local )
228
235
return None
229
236
230
- def OSXFontDirectory ():
231
- """
232
- Return the system font directories for OS X. This is done by
233
- starting at the list of hardcoded paths in
234
- :attr:`OSXFontDirectories` and returning all nested directories
235
- within them.
236
- """
237
- fontpaths = []
238
- def add (arg ,directory ,files ):
239
- fontpaths .append (directory )
240
-
241
- for fontdir in OSXFontDirectories :
242
- try :
243
- if os .path .isdir (fontdir ):
244
- os .path .walk (fontdir , add , None )
245
- except (IOError , OSError , TypeError , ValueError ):
246
- pass
247
- return fontpaths
248
-
249
- def OSXInstalledFonts (directory = None , fontext = 'ttf' ):
237
+ def OSXInstalledFonts (directories = None , fontext = 'ttf' ):
250
238
"""
251
239
Get list of font files on OS X - ignores font suffix by default.
252
240
"""
253
- if directory is None :
254
- directory = OSXFontDirectory ()
241
+ if directories is None :
242
+ directories = OSXFontDirectories
255
243
256
244
fontext = get_fontext_synonyms (fontext )
257
245
258
246
files = []
259
- for path in directory :
247
+ for path in directories :
260
248
if fontext is None :
261
- files .extend (glob . glob ( os . path . join ( path , '*' ) ))
249
+ files .extend (cbook . listFiles ( path , '*' ))
262
250
else :
263
- for ext in fontext :
264
- files .extend (glob .glob (os .path .join (path , '*.' + ext )))
265
- files .extend (glob .glob (os .path .join (path , '*.' + ext .upper ())))
251
+ files .extend (list_fonts (path , fontext ))
266
252
return files
267
253
268
254
269
- def x11FontDirectory ():
270
- """
271
- Return the system font directories for X11. This is done by
272
- starting at the list of hardcoded paths in
273
- :attr:`X11FontDirectories` and returning all nested directories
274
- within them.
275
- """
276
- fontpaths = []
277
- def add (arg ,directory ,files ):
278
- fontpaths .append (directory )
279
-
280
- for fontdir in X11FontDirectories :
281
- try :
282
- if os .path .isdir (fontdir ):
283
- os .path .walk (fontdir , add , None )
284
- except (IOError , OSError , TypeError , ValueError ):
285
- pass
286
- return fontpaths
287
-
288
-
289
255
def get_fontconfig_fonts (fontext = 'ttf' ):
290
256
"""
291
257
Grab a list of all the fonts that are being tracked by fontconfig
@@ -334,7 +300,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
334
300
if len (ext )> 1 and ext [1 :].lower () in fontexts :
335
301
fontfiles [f ] = 1
336
302
else :
337
- fontpaths = x11FontDirectory ()
303
+ fontpaths = X11FontDirectories
338
304
# check for OS X & load its fonts if present
339
305
if sys .platform == 'darwin' :
340
306
for f in OSXInstalledFonts (fontext = fontext ):
@@ -347,10 +313,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
347
313
fontpaths = [fontpaths ]
348
314
349
315
for path in fontpaths :
350
- files = []
351
- for ext in fontexts :
352
- files .extend (glob .glob (os .path .join (path , '*.' + ext )))
353
- files .extend (glob .glob (os .path .join (path , '*.' + ext .upper ())))
316
+ files = list_fonts (path , fontexts )
354
317
for fname in files :
355
318
fontfiles [os .path .abspath (fname )] = 1
356
319
0 commit comments