4242 see license/LICENSE_TTFQUERY.
4343"""
4444
45- import os , sys , glob , subprocess , warnings
45+ import os , sys , subprocess , warnings
4646try :
4747 set
4848except NameError :
5252from matplotlib import ft2font
5353from matplotlib import rcParams , get_configdir
5454from matplotlib .cbook import is_string_like
55+ import matplotlib .cbook as cbook
5556from matplotlib .fontconfig_pattern import \
5657 parse_fontconfig_pattern , generate_fontconfig_pattern
5758
@@ -155,6 +156,15 @@ def get_fontext_synonyms(fontext):
155156 'otf' : ('ttf' , 'otf' ),
156157 'afm' : ('afm' ,)}[fontext ]
157158
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+
158168def win32FontDirectory ():
159169 """
160170 Return the user-specified font directory for Win32. This is
@@ -204,10 +214,7 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
204214 continue
205215
206216 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 )
211218 try :
212219 for j in range (_winreg .QueryInfoKey (local )[1 ]):
213220 try :
@@ -227,65 +234,24 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
227234 _winreg .CloseKey (local )
228235 return None
229236
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' ):
250238 """
251239 Get list of font files on OS X - ignores font suffix by default.
252240 """
253- if directory is None :
254- directory = OSXFontDirectory ()
241+ if directories is None :
242+ directories = OSXFontDirectories
255243
256244 fontext = get_fontext_synonyms (fontext )
257245
258246 files = []
259- for path in directory :
247+ for path in directories :
260248 if fontext is None :
261- files .extend (glob . glob ( os . path . join ( path , '*' ) ))
249+ files .extend (cbook . listFiles ( path , '*' ))
262250 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 ))
266252 return files
267253
268254
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-
289255def get_fontconfig_fonts (fontext = 'ttf' ):
290256 """
291257 Grab a list of all the fonts that are being tracked by fontconfig
@@ -334,7 +300,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
334300 if len (ext )> 1 and ext [1 :].lower () in fontexts :
335301 fontfiles [f ] = 1
336302 else :
337- fontpaths = x11FontDirectory ()
303+ fontpaths = X11FontDirectories
338304 # check for OS X & load its fonts if present
339305 if sys .platform == 'darwin' :
340306 for f in OSXInstalledFonts (fontext = fontext ):
@@ -347,10 +313,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
347313 fontpaths = [fontpaths ]
348314
349315 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 )
354317 for fname in files :
355318 fontfiles [os .path .abspath (fname )] = 1
356319
0 commit comments