36
36
import os ,sys ,glob ,shutil
37
37
from sets import Set
38
38
import matplotlib
39
- from matplotlib import afm
39
+ from matplotlib import afm
40
40
from matplotlib import ft2font
41
41
from matplotlib import rcParams ,get_home ,get_configdir
42
42
from matplotlib .cbook import is_string_like
95
95
path = os .path .join (home ,'.fonts' )
96
96
X11FontDirectories .append (path )
97
97
98
+ def get_fontext_synonyms (fontext ):
99
+ return {'ttf' : ('ttf' ,'otf' ),
100
+ 'afm' : ('afm' ,)}[fontext ]
101
+
98
102
def win32FontDirectory ():
99
103
"""Return the user-specified font directory for Win32."""
100
104
@@ -121,6 +125,8 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
121
125
if directory is None :
122
126
directory = win32FontDirectory ()
123
127
128
+ fontext = get_fontext_synonyms (fontext )
129
+
124
130
key ,items = None , {}
125
131
for fontdir in MSFontDirectories :
126
132
try :
@@ -129,15 +135,18 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
129
135
continue
130
136
131
137
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
133
142
try :
134
143
for j in range (_winreg .QueryInfoKey (local )[1 ]):
135
144
try :
136
145
key ,direc ,any = _winreg .EnumValue (local ,j )
137
146
if not os .path .dirname (direc ):
138
147
direc = os .path .join (directory ,direc )
139
148
direc = os .path .abspath (direc ).lower ()
140
- if direc [ - 4 :]== '.' + fontext :
149
+ if os . path . splitext ( direc )[ 1 ][ 1 :]in fontext :
141
150
items [direc ]= 1
142
151
except EnvironmentError :
143
152
continue
@@ -168,13 +177,16 @@ def OSXInstalledFonts(directory=None, fontext=None):
168
177
if directory is None :
169
178
directory = OSXFontDirectory ()
170
179
180
+ fontext = get_fontext_synonyms (fontext )
181
+
171
182
files = []
172
183
for path in directory :
173
184
if fontext is None :
174
185
files .extend (glob .glob (os .path .join (path ,'*' )))
175
186
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 ())))
178
190
return files
179
191
180
192
@@ -201,12 +213,14 @@ def get_fontconfig_fonts(fontext='ttf'):
201
213
except ImportError :
202
214
return {}
203
215
216
+ fontext = get_fontext_synonyms (fontext )
217
+
204
218
fontfiles = {}
205
219
status ,output = commands .getstatusoutput ("fc-list file" )
206
220
if status == 0 :
207
221
for line in output .split ('\n ' ):
208
222
fname = line .split (':' )[0 ]
209
- if (os .path .splitext (fname )[1 ]== "." + fontext and
223
+ if (os .path .splitext (fname )[1 ][ 1 :] in fontext and
210
224
os .path .exists (fname )):
211
225
fontfiles [fname ]= 1
212
226
@@ -221,7 +235,8 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
221
235
AFM fonts as an option.
222
236
"""
223
237
fontfiles = {}
224
-
238
+ fontexts = get_fontext_synonyms (fontext )
239
+
225
240
if fontpaths is None :
226
241
if sys .platform == 'win32' :
227
242
fontdir = win32FontDirectory ()
@@ -230,7 +245,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
230
245
# now get all installed fonts directly...
231
246
for f in win32InstalledFonts (fontdir ):
232
247
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 :
234
249
fontfiles [f ]= 1
235
250
else :
236
251
fontpaths = x11FontDirectory ()
@@ -246,8 +261,10 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
246
261
fontpaths = [fontpaths ]
247
262
248
263
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 ())))
251
268
for fname in files :
252
269
fontfiles [os .path .abspath (fname )]= 1
253
270
@@ -1047,16 +1064,17 @@ def lookup_name(name):
1047
1064
1048
1065
def fc_match (pattern ,fontext ):
1049
1066
import commands
1067
+ fontexts = get_fontext_synonyms (fontext )
1050
1068
ext = "." + fontext
1051
1069
status ,output = commands .getstatusoutput ('fc-match -sv "%s"' % pattern )
1052
1070
if status == 0 :
1053
1071
for match in _fc_match_regex .finditer (output ):
1054
1072
file = match .group (1 )
1055
- if os .path .splitext (file )[1 ]== ext :
1073
+ if os .path .splitext (file )[1 ][ 1 :] in fontexts :
1056
1074
return file
1057
1075
return None
1058
1076
1059
- _fc_match_regex = re .compile (r'\sfile:\s+"(. *)"' )
1077
+ _fc_match_regex = re .compile (r'\sfile:\s+"([^"] *)"' )
1060
1078
_fc_match_cache = {}
1061
1079
1062
1080
def findfont (prop ,fontext = 'ttf' ):