3636import os , sys , glob , shutil
3737from sets import Set
3838import matplotlib
39- from matplotlib import afm
39+ from matplotlib import afm
4040from matplotlib import ft2font
4141from matplotlib import rcParams , get_home , get_configdir
4242from matplotlib .cbook import is_string_like
9595 path = os .path .join (home , '.fonts' )
9696 X11FontDirectories .append (path )
9797
98+ def get_fontext_synonyms (fontext ):
99+ return {'ttf' : ('ttf' , 'otf' ),
100+ 'afm' : ('afm' ,)}[fontext ]
101+
98102def win32FontDirectory ():
99103 """Return the user-specified font directory for Win32."""
100104
@@ -121,6 +125,8 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
121125 if directory is None :
122126 directory = win32FontDirectory ()
123127
128+ fontext = get_fontext_synonyms (fontext )
129+
124130 key , items = None , {}
125131 for fontdir in MSFontDirectories :
126132 try :
@@ -129,15 +135,18 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
129135 continue
130136
131137 if not local :
132- return glob .glob (os .path .join (directory , '*.' + fontext ))
138+ files = []
139+ for ext in fontext :
140+ files .extend (glob .glob (os .path .join (directory , '*.' + ext )))
141+ return files
133142 try :
134143 for j in range (_winreg .QueryInfoKey (local )[1 ]):
135144 try :
136145 key , direc , any = _winreg .EnumValue ( local , j )
137146 if not os .path .dirname (direc ):
138147 direc = os .path .join (directory , direc )
139148 direc = os .path .abspath (direc ).lower ()
140- if direc [ - 4 :] == '.' + fontext :
149+ if os . path . splitext ( direc )[ 1 ][ 1 :] in fontext :
141150 items [direc ] = 1
142151 except EnvironmentError :
143152 continue
@@ -168,13 +177,16 @@ def OSXInstalledFonts(directory=None, fontext=None):
168177 if directory is None :
169178 directory = OSXFontDirectory ()
170179
180+ fontext = get_fontext_synonyms (fontext )
181+
171182 files = []
172183 for path in directory :
173184 if fontext is None :
174185 files .extend (glob .glob (os .path .join (path ,'*' )))
175186 else :
176- files .extend (glob .glob (os .path .join (path , '*.' + fontext )))
177- files .extend (glob .glob (os .path .join (path , '*.' + fontext .upper ())))
187+ for ext in fontext :
188+ files .extend (glob .glob (os .path .join (path , '*.' + ext )))
189+ files .extend (glob .glob (os .path .join (path , '*.' + ext .upper ())))
178190 return files
179191
180192
@@ -201,12 +213,14 @@ def get_fontconfig_fonts(fontext='ttf'):
201213 except ImportError :
202214 return {}
203215
216+ fontext = get_fontext_synonyms (fontext )
217+
204218 fontfiles = {}
205219 status , output = commands .getstatusoutput ("fc-list file" )
206220 if status == 0 :
207221 for line in output .split ('\n ' ):
208222 fname = line .split (':' )[0 ]
209- if (os .path .splitext (fname )[1 ] == "." + fontext and
223+ if (os .path .splitext (fname )[1 ][ 1 :] in fontext and
210224 os .path .exists (fname )):
211225 fontfiles [fname ] = 1
212226
@@ -221,7 +235,8 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
221235 AFM fonts as an option.
222236 """
223237 fontfiles = {}
224-
238+ fontexts = get_fontext_synonyms (fontext )
239+
225240 if fontpaths is None :
226241 if sys .platform == 'win32' :
227242 fontdir = win32FontDirectory ()
@@ -230,7 +245,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
230245 # now get all installed fonts directly...
231246 for f in win32InstalledFonts (fontdir ):
232247 base , ext = os .path .splitext (f )
233- if len (ext )> 1 and ext [1 :].lower ()== fontext :
248+ if len (ext )> 1 and ext [1 :].lower () in fontexts :
234249 fontfiles [f ] = 1
235250 else :
236251 fontpaths = x11FontDirectory ()
@@ -246,8 +261,10 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
246261 fontpaths = [fontpaths ]
247262
248263 for path in fontpaths :
249- files = glob .glob (os .path .join (path , '*.' + fontext ))
250- files .extend (glob .glob (os .path .join (path , '*.' + fontext .upper ())))
264+ files = []
265+ for ext in fontexts :
266+ files .extend (glob .glob (os .path .join (path , '*.' + ext )))
267+ files .extend (glob .glob (os .path .join (path , '*.' + ext .upper ())))
251268 for fname in files :
252269 fontfiles [os .path .abspath (fname )] = 1
253270
@@ -1047,16 +1064,17 @@ def lookup_name(name):
10471064
10481065 def fc_match (pattern , fontext ):
10491066 import commands
1067+ fontexts = get_fontext_synonyms (fontext )
10501068 ext = "." + fontext
10511069 status , output = commands .getstatusoutput ('fc-match -sv "%s"' % pattern )
10521070 if status == 0 :
10531071 for match in _fc_match_regex .finditer (output ):
10541072 file = match .group (1 )
1055- if os .path .splitext (file )[1 ] == ext :
1073+ if os .path .splitext (file )[1 ][ 1 :] in fontexts :
10561074 return file
10571075 return None
10581076
1059- _fc_match_regex = re .compile (r'\sfile:\s+"(. *)"' )
1077+ _fc_match_regex = re .compile (r'\sfile:\s+"([^"] *)"' )
10601078 _fc_match_cache = {}
10611079
10621080 def findfont (prop , fontext = 'ttf' ):
0 commit comments