11
11
The design is based on the `W3C Cascading Style Sheet, Level 1 (CSS1)
12
12
font specification <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_.
13
13
Future versions may implement the Level 2 or 2.1 specifications.
14
-
15
- Experimental support is included for using `fontconfig` on Unix
16
- variant platforms (Linux, OS X, Solaris). To enable it, set the
17
- constant ``USE_FONTCONFIG`` in this file to ``True``. Fontconfig has
18
- the advantage that it is the standard way to look up fonts on X11
19
- platforms, so if a font is installed, it is much more likely to be
20
- found.
21
14
"""
22
15
23
16
# KNOWN ISSUES
42
35
from threading import Timer
43
36
import warnings
44
37
45
- from matplotlib import afm , cbook , ft2font , rcParams , get_cachedir
38
+ import matplotlib as mpl
39
+ from matplotlib import afm , cbook , ft2font , rcParams
46
40
from matplotlib .fontconfig_pattern import (
47
41
parse_fontconfig_pattern , generate_fontconfig_pattern )
48
42
49
43
_log = logging .getLogger (__name__ )
50
44
51
- USE_FONTCONFIG = False
52
-
53
45
font_scalings = {
54
46
'xx-small' : 0.579 ,
55
47
'x-small' : 0.694 ,
102
94
MSFolders = \
103
95
r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
104
96
105
-
106
97
MSFontDirectories = [
107
98
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' ,
108
99
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts' ]
109
100
110
-
111
101
X11FontDirectories = [
112
102
# an old standard installation point
113
103
"/usr/X11R6/lib/X11/fonts/TTF/" ,
118
108
"/usr/local/share/fonts/" ,
119
109
# common application, not really useful
120
110
"/usr/lib/openoffice/share/fonts/truetype/" ,
121
- ]
111
+ # user fonts
112
+ str (Path .home () / ".fonts" ),
113
+ ]
122
114
123
115
OSXFontDirectories = [
124
116
"/Library/Fonts/" ,
125
117
"/Network/Library/Fonts/" ,
126
118
"/System/Library/Fonts/" ,
127
119
# fonts installed via MacPorts
128
- "/opt/local/share/fonts"
129
- ""
120
+ "/opt/local/share/fonts" ,
121
+ # user fonts
122
+ str (Path .home () / "Library/Fonts" ),
130
123
]
131
124
132
- if not USE_FONTCONFIG and sys .platform != 'win32' :
133
- OSXFontDirectories .append (str (Path .home () / "Library/Fonts" ))
134
- X11FontDirectories .append (str (Path .home () / ".fonts" ))
135
-
136
125
137
126
def get_fontext_synonyms (fontext ):
138
127
"""
@@ -1136,7 +1125,7 @@ def score_size(self, size1, size2):
1136
1125
sizeval2 = float (size2 )
1137
1126
except ValueError :
1138
1127
return 1.0
1139
- return abs (sizeval1 - sizeval2 ) / 72.0
1128
+ return abs (sizeval1 - sizeval2 ) / 72
1140
1129
1141
1130
def findfont (self , prop , fontext = 'ttf' , directory = None ,
1142
1131
fallback_to_default = True , rebuild_if_missing = True ):
@@ -1245,6 +1234,7 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1245
1234
1246
1235
return result
1247
1236
1237
+
1248
1238
@lru_cache ()
1249
1239
def is_opentype_cff_font (filename ):
1250
1240
"""
@@ -1258,94 +1248,35 @@ def is_opentype_cff_font(filename):
1258
1248
else :
1259
1249
return False
1260
1250
1261
- fontManager = None
1262
- _fmcache = None
1263
-
1264
1251
1265
1252
_get_font = lru_cache (64 )(ft2font .FT2Font )
1253
+ _fmcache = os .path .join (mpl .get_cachedir (), 'fontList.json' )
1254
+ fontManager = None
1255
+
1266
1256
1267
1257
def get_font (filename , hinting_factor = None ):
1268
1258
if hinting_factor is None :
1269
1259
hinting_factor = rcParams ['text.hinting_factor' ]
1270
1260
return _get_font (filename , hinting_factor )
1271
1261
1272
1262
1273
- # The experimental fontconfig-based backend.
1274
- if USE_FONTCONFIG and sys .platform != 'win32' :
1263
+ def _rebuild ():
1264
+ global fontManager
1265
+ fontManager = FontManager ()
1266
+ with cbook ._lock_path (_fmcache ):
1267
+ json_dump (fontManager , _fmcache )
1268
+ _log .info ("generated new fontManager" )
1275
1269
1276
- def fc_match (pattern , fontext ):
1277
- fontexts = get_fontext_synonyms (fontext )
1278
- ext = "." + fontext
1279
- try :
1280
- pipe = subprocess .Popen (
1281
- ['fc-match' , '-s' , '--format=%{file}\\ n' , pattern ],
1282
- stdout = subprocess .PIPE ,
1283
- stderr = subprocess .PIPE )
1284
- output = pipe .communicate ()[0 ]
1285
- except OSError :
1286
- return None
1287
-
1288
- # The bulk of the output from fc-list is ascii, so we keep the
1289
- # result in bytes and parse it as bytes, until we extract the
1290
- # filename, which is in sys.filesystemencoding().
1291
- if pipe .returncode == 0 :
1292
- for fname in map (os .fsdecode , output .split (b'\n ' )):
1293
- if os .path .splitext (fname )[1 ][1 :] in fontexts :
1294
- return fname
1295
- return None
1296
-
1297
- _fc_match_cache = {}
1298
-
1299
- def findfont (prop , fontext = 'ttf' ):
1300
- if not isinstance (prop , str ):
1301
- prop = prop .get_fontconfig_pattern ()
1302
- cached = _fc_match_cache .get (prop )
1303
- if cached is not None :
1304
- return cached
1305
-
1306
- result = fc_match (prop , fontext )
1307
- if result is None :
1308
- result = fc_match (':' , fontext )
1309
-
1310
- _fc_match_cache [prop ] = result
1311
- return result
1312
1270
1271
+ try :
1272
+ fontManager = json_load (_fmcache )
1273
+ except Exception :
1274
+ _rebuild ()
1313
1275
else :
1314
- _fmcache = None
1315
-
1316
- cachedir = get_cachedir ()
1317
- if cachedir is not None :
1318
- _fmcache = os .path .join (cachedir , 'fontList.json' )
1319
-
1320
- fontManager = None
1321
-
1322
- def _rebuild ():
1323
- global fontManager
1324
-
1325
- fontManager = FontManager ()
1326
-
1327
- if _fmcache :
1328
- with cbook ._lock_path (_fmcache ):
1329
- json_dump (fontManager , _fmcache )
1330
- _log .info ("generated new fontManager" )
1331
-
1332
- if _fmcache :
1333
- try :
1334
- fontManager = json_load (_fmcache )
1335
- if (not hasattr (fontManager , '_version' ) or
1336
- fontManager ._version != FontManager .__version__ ):
1337
- _rebuild ()
1338
- else :
1339
- fontManager .default_size = None
1340
- _log .debug ("Using fontManager instance from %s" , _fmcache )
1341
- except TimeoutError :
1342
- raise
1343
- except Exception :
1344
- _rebuild ()
1345
- else :
1276
+ if getattr (fontManager , '_version' , object ()) != FontManager .__version__ :
1346
1277
_rebuild ()
1278
+ else :
1279
+ _log .debug ("Using fontManager instance from %s" , _fmcache )
1280
+
1347
1281
1348
- def findfont (prop , ** kw ):
1349
- global fontManager
1350
- font = fontManager .findfont (prop , ** kw )
1351
- return font
1282
+ findfont = fontManager .findfont
0 commit comments