Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94120dc

Browse files
committed
[2963827] FontProperties does not seem to be working...
1. Warn when a font family can not be found 2. Fallback to the correct variant of Vera Sans (rather than always non-bold upright) when the font family lookup fails svn path=/trunk/matplotlib/; revision=8312
1 parent e43282b commit 94120dc

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

lib/matplotlib/font_manager.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
see license/LICENSE_TTFQUERY.
4343
"""
4444

45-
import os, sys, glob, subprocess
45+
import os, sys, glob, subprocess, warnings
4646
try:
4747
set
4848
except NameError:
@@ -974,7 +974,7 @@ class FontManager:
974974
# Increment this version number whenever the font cache data
975975
# format or behavior has changed and requires a existing font
976976
# cache files to be rebuilt.
977-
__version__ = 5
977+
__version__ = 6
978978

979979
def __init__(self, size=None, weight='normal'):
980980
self._version = self.__version__
@@ -1001,6 +1001,9 @@ def __init__(self, size=None, weight='normal'):
10011001
# Load TrueType fonts and create font dictionary.
10021002

10031003
self.ttffiles = findSystemFonts(paths) + findSystemFonts()
1004+
self.defaultFamily = {
1005+
'ttf': 'Bitstream Vera Sans',
1006+
'afm': 'Helvetica'}
10041007
self.defaultFont = {}
10051008

10061009
for fname in self.ttffiles:
@@ -1164,7 +1167,8 @@ def score_size(self, size1, size2):
11641167
return 1.0
11651168
return abs(sizeval1 - sizeval2) / 72.0
11661169

1167-
def findfont(self, prop, fontext='ttf', directory=None):
1170+
def findfont(self, prop, fontext='ttf', directory=None,
1171+
fallback_to_default=True):
11681172
"""
11691173
Search the font list for the font that most closely matches
11701174
the :class:`FontProperties` *prop*.
@@ -1181,6 +1185,10 @@ def findfont(self, prop, fontext='ttf', directory=None):
11811185
The result is cached, so subsequent lookups don't have to
11821186
perform the O(n) nearest neighbor search.
11831187
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+
11841192
See the `W3C Cascading Style Sheet, Level 1
11851193
<http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ documentation
11861194
for a description of the font finding algorithm.
@@ -1229,12 +1237,25 @@ def findfont(self, prop, fontext='ttf', directory=None):
12291237
break
12301238

12311239
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]
12351255
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))
12381259
result = best_font.fname
12391260

12401261
font_cache[hash(prop)] = result

0 commit comments

Comments
 (0)