From 74925707cc39503c445ce91227fd8b73b6b1c771 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 14 Dec 2018 15:42:16 +0100 Subject: [PATCH] Canonicalize weights extracted for AFM fonts. --- lib/matplotlib/font_manager.py | 5 +++-- lib/matplotlib/tests/test_afm.py | 15 +++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index cdf3d7b85dae..827aa411125c 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -398,7 +398,6 @@ def afmFontProperty(fontpath, font): ------- `FontEntry` The extracted font properties. - """ name = font.get_familyname() @@ -422,6 +421,8 @@ def afmFontProperty(fontpath, font): variant = 'normal' weight = font.get_weight().lower() + if weight not in weight_dict: + weight = 'normal' # Stretch can be absolute and relative # Absolute stretches are: ultra-condensed, extra-condensed, condensed, @@ -935,7 +936,7 @@ class FontManager(object): # Increment this version number whenever the font cache data # format or behavior has changed and requires a existing font # cache files to be rebuilt. - __version__ = 300 + __version__ = 310 def __init__(self, size=None, weight='normal'): self._version = self.__version__ diff --git a/lib/matplotlib/tests/test_afm.py b/lib/matplotlib/tests/test_afm.py index 25c7a2ad0f92..b2d800113b0d 100644 --- a/lib/matplotlib/tests/test_afm.py +++ b/lib/matplotlib/tests/test_afm.py @@ -1,6 +1,7 @@ from io import BytesIO -import matplotlib.afm as afm +from matplotlib import afm +from matplotlib import font_manager as fm AFM_TEST_DATA = b"""StartFontMetrics 2.0 @@ -75,6 +76,12 @@ def test_parse_char_metrics(): def test_get_familyname_guessed(): fh = BytesIO(AFM_TEST_DATA) - fm = afm.AFM(fh) - del fm._header[b'FamilyName'] # remove FamilyName, so we have to guess - assert fm.get_familyname() == 'My Font' + font = afm.AFM(fh) + del font._header[b'FamilyName'] # remove FamilyName, so we have to guess + assert font.get_familyname() == 'My Font' + + +def test_font_manager_weight_normalization(): + font = afm.AFM(BytesIO( + AFM_TEST_DATA.replace(b"Weight Bold\n", b"Weight Custom\n"))) + assert fm.afmFontProperty("", font).weight == "normal"