Skip to content

Canonicalize weights extracted for AFM fonts. #12991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ def afmFontProperty(fontpath, font):
-------
`FontEntry`
The extracted font properties.

"""

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

weight = font.get_weight().lower()
if weight not in weight_dict:
weight = 'normal'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the fallback issue some warning or log?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, previously it didn't (although the fallback occurred in score_weight, but the idea is the same), and it's not as if the end user can do anything about that.
(To be fully honest it's not clear to me why we even bother trying to get font properties from AFM files as these only contain metrics but not outlines, so to visualize the pdf you'll need to have the actual font file (typically, .pfa/.pfb) as well; the only exception is for the "14 pdf core fonts" (helvetica, etc.) which must be embedded in each pdf viewer (and there we do still need the metrics to know the size of the glyphs for alignment purposes) but even then we could just hardcode that info in parsed form instead of reparsing them from matplotlib/mpl-data/fonts/pdfcorefonts/ every time...)


# Stretch can be absolute and relative
# Absolute stretches are: ultra-condensed, extra-condensed, condensed,
Expand Down Expand Up @@ -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__
Expand Down
15 changes: 11 additions & 4 deletions lib/matplotlib/tests/test_afm.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"