Skip to content

Commit dba2d7b

Browse files
anntzertimhoffm
authored andcommitted
Canonicalize weights extracted for AFM fonts. (#12991)
1 parent 6a071fd commit dba2d7b

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/matplotlib/font_manager.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ def afmFontProperty(fontpath, font):
398398
-------
399399
`FontEntry`
400400
The extracted font properties.
401-
402401
"""
403402

404403
name = font.get_familyname()
@@ -422,6 +421,8 @@ def afmFontProperty(fontpath, font):
422421
variant = 'normal'
423422

424423
weight = font.get_weight().lower()
424+
if weight not in weight_dict:
425+
weight = 'normal'
425426

426427
# Stretch can be absolute and relative
427428
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
@@ -935,7 +936,7 @@ class FontManager(object):
935936
# Increment this version number whenever the font cache data
936937
# format or behavior has changed and requires a existing font
937938
# cache files to be rebuilt.
938-
__version__ = 300
939+
__version__ = 310
939940

940941
def __init__(self, size=None, weight='normal'):
941942
self._version = self.__version__

lib/matplotlib/tests/test_afm.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from io import BytesIO
22

3-
import matplotlib.afm as afm
3+
from matplotlib import afm
4+
from matplotlib import font_manager as fm
45

56

67
AFM_TEST_DATA = b"""StartFontMetrics 2.0
@@ -75,6 +76,12 @@ def test_parse_char_metrics():
7576

7677
def test_get_familyname_guessed():
7778
fh = BytesIO(AFM_TEST_DATA)
78-
fm = afm.AFM(fh)
79-
del fm._header[b'FamilyName'] # remove FamilyName, so we have to guess
80-
assert fm.get_familyname() == 'My Font'
79+
font = afm.AFM(fh)
80+
del font._header[b'FamilyName'] # remove FamilyName, so we have to guess
81+
assert font.get_familyname() == 'My Font'
82+
83+
84+
def test_font_manager_weight_normalization():
85+
font = afm.AFM(BytesIO(
86+
AFM_TEST_DATA.replace(b"Weight Bold\n", b"Weight Custom\n")))
87+
assert fm.afmFontProperty("", font).weight == "normal"

0 commit comments

Comments
 (0)