Skip to content

Bug: Example from matplotlib.afm not working #10677

Closed
@timhoffm

Description

@timhoffm

Bug report

Bug summary

The example from https://matplotlib.org/api/afm_api.html is not working on the current master.

>>> from matplotlib import rcParams
>>> import os.path
>>> afm_fname = os.path.join(rcParams['datapath'],
...                         'fonts', 'afm', 'ptmr8a.afm')
>>>
>>> from matplotlib.afm import AFM
>>> with open(afm_fname) as fh:
...     afm = AFM(fh)

results in

TypeError                                 Traceback (most recent call last)
<ipython-input-2-571314e27e20> in <module>()
      6 from matplotlib.afm import AFM
      7 with open(afm_fname) as fh:
----> 8     afm = AFM(fh)

~/dev/matplotlib/lib/matplotlib/afm.py in __init__(self, fh)
    341         """
    342         (dhead, dcmetrics_ascii, dcmetrics_name, dkernpairs, dcomposite) = \
--> 343             parse_afm(fh)
    344         self._header = dhead
    345         self._kern = dkernpairs

~/dev/matplotlib/lib/matplotlib/afm.py in parse_afm(fh)
    327 
    328     """
--> 329     _sanity_check(fh)
    330     dhead = _parse_header(fh)
    331     dcmetrics_ascii, dcmetrics_name = _parse_char_metrics(fh)

~/dev/matplotlib/lib/matplotlib/afm.py in _sanity_check(fh)
     99     # EndFontMetrics keyword must be the last non-empty line in the
    100     # file. We just check the first line.
--> 101     if not line.startswith(b'StartFontMetrics'):
    102         raise RuntimeError('Not an AFM file')
    103 

TypeError: startswith first arg must be str or a tuple of str, not bytes

Tested with python 3.6.

I'm neither an expert with encodings nor with font files. I assume that someone with a bit more background will directly see what to do. Therefore, I don't dig into a solution for the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions