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