42
42
see license/LICENSE_TTFQUERY.
43
43
"""
44
44
45
- import os , sys , glob , subprocess
45
+ import os , sys , glob , subprocess , warnings
46
46
try :
47
47
set
48
48
except NameError :
@@ -974,7 +974,7 @@ class FontManager:
974
974
# Increment this version number whenever the font cache data
975
975
# format or behavior has changed and requires a existing font
976
976
# cache files to be rebuilt.
977
- __version__ = 5
977
+ __version__ = 6
978
978
979
979
def __init__ (self , size = None , weight = 'normal' ):
980
980
self ._version = self .__version__
@@ -1001,6 +1001,9 @@ def __init__(self, size=None, weight='normal'):
1001
1001
# Load TrueType fonts and create font dictionary.
1002
1002
1003
1003
self .ttffiles = findSystemFonts (paths ) + findSystemFonts ()
1004
+ self .defaultFamily = {
1005
+ 'ttf' : 'Bitstream Vera Sans' ,
1006
+ 'afm' : 'Helvetica' }
1004
1007
self .defaultFont = {}
1005
1008
1006
1009
for fname in self .ttffiles :
@@ -1164,7 +1167,8 @@ def score_size(self, size1, size2):
1164
1167
return 1.0
1165
1168
return abs (sizeval1 - sizeval2 ) / 72.0
1166
1169
1167
- def findfont (self , prop , fontext = 'ttf' , directory = None ):
1170
+ def findfont (self , prop , fontext = 'ttf' , directory = None ,
1171
+ fallback_to_default = True ):
1168
1172
"""
1169
1173
Search the font list for the font that most closely matches
1170
1174
the :class:`FontProperties` *prop*.
@@ -1181,6 +1185,10 @@ def findfont(self, prop, fontext='ttf', directory=None):
1181
1185
The result is cached, so subsequent lookups don't have to
1182
1186
perform the O(n) nearest neighbor search.
1183
1187
1188
+ If `fallback_to_default` is True, will fallback to the default
1189
+ font family (usually "Bitstream Vera Sans" or "Helvetica") if
1190
+ the first lookup hard-fails.
1191
+
1184
1192
See the `W3C Cascading Style Sheet, Level 1
1185
1193
<http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ documentation
1186
1194
for a description of the font finding algorithm.
@@ -1229,12 +1237,25 @@ def findfont(self, prop, fontext='ttf', directory=None):
1229
1237
break
1230
1238
1231
1239
if best_font is None or best_score >= 10.0 :
1232
- verbose .report ('findfont: Could not match %s. Returning %s' %
1233
- (prop , self .defaultFont [fontext ]))
1234
- result = self .defaultFont [fontext ]
1240
+ if fallback_to_default :
1241
+ warnings .warn (
1242
+ 'findfont: Font family %s not found. Falling back to %s' %
1243
+ (prop .get_family (), self .defaultFamily [fontext ]))
1244
+ default_prop = prop .copy ()
1245
+ default_prop .set_family (self .defaultFamily [fontext ])
1246
+ return self .findfont (default_prop , fontext , directory , False )
1247
+ else :
1248
+ # This is a hard fail -- we can't find anything reasonable,
1249
+ # so just return the vera.ttf
1250
+ warnings .warn (
1251
+ 'findfont: Could not match %s. Returning %s' %
1252
+ (prop , self .defaultFont [fontext ]),
1253
+ UserWarning )
1254
+ result = self .defaultFont [fontext ]
1235
1255
else :
1236
- verbose .report ('findfont: Matching %s to %s (%s) with score of %f' %
1237
- (prop , best_font .name , best_font .fname , best_score ))
1256
+ verbose .report (
1257
+ 'findfont: Matching %s to %s (%s) with score of %f' %
1258
+ (prop , best_font .name , best_font .fname , best_score ))
1238
1259
result = best_font .fname
1239
1260
1240
1261
font_cache [hash (prop )] = result
0 commit comments