matplotlib.afm
¶This is a python interface to Adobe Font Metrics Files. Although a number of other python implementations exist, and may be more complete than this, it was decided not to go with them because they were either:
- copyrighted or used a non-BSD compatible license
- had too many dependencies and a free standing lib was needed
- Did more than needed and it was easier to write afresh rather than figure out how to get just what was needed.
It is pretty easy to use, and requires only built-in python libs:
>>> 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, 'rb') as fh:
... afm = AFM(fh)
>>> afm.string_width_height('What the heck?')
(6220.0, 694)
>>> afm.get_fontname()
'Times-Roman'
>>> afm.get_kern_dist('A', 'f')
0
>>> afm.get_kern_dist('A', 'y')
-92.0
>>> afm.get_bbox_char('!')
[130, -9, 238, 676]
matplotlib.afm.
AFM
(fh)[source]¶Bases: object
Parse the AFM file in file object fh
family_name
¶get_height_char
(c, isord=False)[source]¶Get the height of character c from the bounding box. This is the ink height (space is 0)
get_horizontal_stem_width
()[source]¶Return the standard horizontal stem width as float, or None if not specified in AFM file.
get_kern_dist_from_name
(name1, name2)[source]¶Return the kerning pair distance (possibly 0) for chars name1 and name2
get_vertical_stem_width
()[source]¶Return the standard vertical stem width as float, or None if not specified in AFM file.
matplotlib.afm.
parse_afm
(fh)[source]¶Parse the Adobe Font Metics file in file handle fh. Return value
is a (dhead, dcmetrics_ascii, dmetrics_name, dkernpairs,
dcomposite) tuple where
dhead is a _parse_header()
dict,
dcmetrics_ascii and dcmetrics_name are the two resulting dicts
from _parse_char_metrics()
,
dkernpairs is a _parse_kern_pairs()
dict (possibly {}) and
dcomposite is a _parse_composites()
dict (possibly {})