Matplotlib 2.0.0rc2 is available

Install the release candidate now!



Table Of Contents

This Page

afm (Adobe Font Metrics interface)


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:

  1. copyrighted or used a non-BSD compatible license
  2. had too many dependencies and a free standing lib was needed
  3. 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
>>> afm = AFM(open(afm_fname))
>>> afm.string_width_height('What the heck?')
(6220.0, 694)
>>> afm.get_fontname()
>>> afm.get_kern_dist('A', 'f')
>>> afm.get_kern_dist('A', 'y')
>>> afm.get_bbox_char('!')
[130, -9, 238, 676]
class matplotlib.afm.AFM(fh)

Bases: object

Parse the AFM file in file object fh


Return the fontangle as float

get_bbox_char(c, isord=False)

Return the cap height as float


Return the font family name, e.g., ‘Times’


Return the font name, e.g., ‘Times-Roman’


Return the font full name, e.g., ‘Times-Roman’

get_height_char(c, isord=False)

Get the height of character c from the bounding box. This is the ink height (space is 0)


Return the standard horizontal stem width as float, or None if not specified in AFM file.

get_kern_dist(c1, c2)

Return the kerning pair distance (possibly 0) for chars c1 and c2

get_kern_dist_from_name(name1, name2)

Return the kerning pair distance (possibly 0) for chars name1 and name2

get_name_char(c, isord=False)

Get the name of the character, i.e., ‘;’ is ‘semicolon’


Return the string bounding box


Return the string bounding box


Return the underline thickness as float


Return the standard vertical stem width as float, or None if not specified in AFM file.


Return the font weight, e.g., ‘Bold’ or ‘Roman’

get_width_char(c, isord=False)

Get the width of the character from the character metric WX field


Get the width of the character from a type1 character name


Return the xheight as float


Return the string width (including kerning) and string height as a (w, h) tuple.


Parse the Adobe Font Metics file in file handle fh. Return value is a (dhead, dcmetrics, dkernpairs, dcomposite) tuple where dhead is a _parse_header() dict, dcmetrics is a _parse_composites() dict, dkernpairs is a _parse_kern_pairs() dict (possibly {}), and dcomposite is a _parse_composites() dict (possibly {})