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
45
38
import warnings
46
39
47
40
import matplotlib as mpl
48
- from matplotlib import afm , cbook , ft2font , rcParams , get_cachedir
41
+ from matplotlib import afm , cbook , ft2font , rcParams
49
42
from matplotlib .fontconfig_pattern import (
50
43
parse_fontconfig_pattern , generate_fontconfig_pattern )
51
44
52
45
_log = logging .getLogger (__name__ )
53
46
54
- USE_FONTCONFIG = False
55
-
56
47
font_scalings = {
57
48
'xx-small' : 0.579 ,
58
49
'x-small' : 0.694 ,
105
96
MSFolders = \
106
97
r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
107
98
108
-
109
99
MSFontDirectories = [
110
100
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts' ,
111
101
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts' ]
112
102
113
-
114
103
X11FontDirectories = [
115
104
# an old standard installation point
116
105
"/usr/X11R6/lib/X11/fonts/TTF/" ,
121
110
"/usr/local/share/fonts/" ,
122
111
# common application, not really useful
123
112
"/usr/lib/openoffice/share/fonts/truetype/" ,
124
- ]
113
+ # user fonts
114
+ str (Path .home () / ".fonts" ),
115
+ ]
125
116
126
117
OSXFontDirectories = [
127
118
"/Library/Fonts/" ,
128
119
"/Network/Library/Fonts/" ,
129
120
"/System/Library/Fonts/" ,
130
121
# fonts installed via MacPorts
131
- "/opt/local/share/fonts"
132
- ""
122
+ "/opt/local/share/fonts" ,
123
+ # user fonts
124
+ str (Path .home () / "Library/Fonts" ),
133
125
]
134
126
135
- if not USE_FONTCONFIG and sys .platform != 'win32' :
136
- OSXFontDirectories .append (str (Path .home () / "Library/Fonts" ))
137
- X11FontDirectories .append (str (Path .home () / ".fonts" ))
138
-
139
127
140
128
def get_fontext_synonyms (fontext ):
141
129
"""
@@ -1154,7 +1142,7 @@ def score_size(self, size1, size2):
1154
1142
sizeval2 = float (size2 )
1155
1143
except ValueError :
1156
1144
return 1.0
1157
- return abs (sizeval1 - sizeval2 ) / 72.0
1145
+ return abs (sizeval1 - sizeval2 ) / 72
1158
1146
1159
1147
def findfont (self , prop , fontext = 'ttf' , directory = None ,
1160
1148
fallback_to_default = True , rebuild_if_missing = True ):
@@ -1263,6 +1251,7 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1263
1251
1264
1252
return result
1265
1253
1254
+
1266
1255
@lru_cache ()
1267
1256
def is_opentype_cff_font (filename ):
1268
1257
"""
@@ -1276,95 +1265,36 @@ def is_opentype_cff_font(filename):
1276
1265
else :
1277
1266
return False
1278
1267
1279
- fontManager = None
1280
- _fmcache = None
1281
-
1282
1268
1283
1269
_get_font = lru_cache (64 )(ft2font .FT2Font )
1270
+ _fmcache = os .path .join (
1271
+ mpl .get_cachedir (), 'fontlist-v{}.json' .format (FontManager .__version__ ))
1272
+ fontManager = None
1273
+
1284
1274
1285
1275
def get_font (filename , hinting_factor = None ):
1286
1276
if hinting_factor is None :
1287
1277
hinting_factor = rcParams ['text.hinting_factor' ]
1288
1278
return _get_font (filename , hinting_factor )
1289
1279
1290
1280
1291
- # The experimental fontconfig-based backend.
1292
- if USE_FONTCONFIG and sys .platform != 'win32' :
1281
+ def _rebuild ():
1282
+ global fontManager
1283
+ fontManager = FontManager ()
1284
+ with cbook ._lock_path (_fmcache ):
1285
+ json_dump (fontManager , _fmcache )
1286
+ _log .info ("generated new fontManager" )
1293
1287
1294
- def fc_match (pattern , fontext ):
1295
- fontexts = get_fontext_synonyms (fontext )
1296
- ext = "." + fontext
1297
- try :
1298
- pipe = subprocess .Popen (
1299
- ['fc-match' , '-s' , '--format=%{file}\\ n' , pattern ],
1300
- stdout = subprocess .PIPE ,
1301
- stderr = subprocess .PIPE )
1302
- output = pipe .communicate ()[0 ]
1303
- except OSError :
1304
- return None
1305
-
1306
- # The bulk of the output from fc-list is ascii, so we keep the
1307
- # result in bytes and parse it as bytes, until we extract the
1308
- # filename, which is in sys.filesystemencoding().
1309
- if pipe .returncode == 0 :
1310
- for fname in map (os .fsdecode , output .split (b'\n ' )):
1311
- if os .path .splitext (fname )[1 ][1 :] in fontexts :
1312
- return fname
1313
- return None
1314
-
1315
- _fc_match_cache = {}
1316
-
1317
- def findfont (prop , fontext = 'ttf' ):
1318
- if not isinstance (prop , str ):
1319
- prop = prop .get_fontconfig_pattern ()
1320
- cached = _fc_match_cache .get (prop )
1321
- if cached is not None :
1322
- return cached
1323
-
1324
- result = fc_match (prop , fontext )
1325
- if result is None :
1326
- result = fc_match (':' , fontext )
1327
-
1328
- _fc_match_cache [prop ] = result
1329
- return result
1330
1288
1289
+ try :
1290
+ fontManager = json_load (_fmcache )
1291
+ except Exception :
1292
+ _rebuild ()
1331
1293
else :
1332
- _fmcache = None
1333
-
1334
- cachedir = get_cachedir ()
1335
- if cachedir is not None :
1336
- _fmcache = os .path .join (
1337
- cachedir , 'fontlist-v{}.json' .format (FontManager .__version__ ))
1338
-
1339
- fontManager = None
1340
-
1341
- def _rebuild ():
1342
- global fontManager
1343
-
1344
- fontManager = FontManager ()
1345
-
1346
- if _fmcache :
1347
- with cbook ._lock_path (_fmcache ):
1348
- json_dump (fontManager , _fmcache )
1349
- _log .debug ("generated new fontManager" )
1350
-
1351
- if _fmcache :
1352
- try :
1353
- fontManager = json_load (_fmcache )
1354
- if (not hasattr (fontManager , '_version' ) or
1355
- fontManager ._version != FontManager .__version__ ):
1356
- _rebuild ()
1357
- else :
1358
- fontManager .default_size = None
1359
- _log .debug ("Using fontManager instance from %s" , _fmcache )
1360
- except TimeoutError :
1361
- raise
1362
- except Exception :
1363
- _rebuild ()
1364
- else :
1294
+ if getattr (fontManager , '_version' , object ()) != FontManager .__version__ :
1365
1295
_rebuild ()
1296
+ else :
1297
+ _log .debug ("Using fontManager instance from %s" , _fmcache )
1298
+
1366
1299
1367
- def findfont (prop , ** kw ):
1368
- global fontManager
1369
- font = fontManager .findfont (prop , ** kw )
1370
- return font
1300
+ findfont = fontManager .findfont
0 commit comments