You are reading an old version of the documentation (v3.1.0). For the latest version see https://matplotlib.org/stable/api/afm_api.html
Version 3.1.0
matplotlib
Fork me on GitHub

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:

  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
>>> 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]

As in the Adobe Font Metrics File Format Specification, all dimensions are given in units of 1/1000 of the scale factor (point size) of the font being used.

class matplotlib.afm.AFM(fh)[source]

Bases: object

Parse the AFM file in file object fh.

family_name

The font family name, e.g., 'Times'.

get_angle(self)[source]

Return the fontangle as float.

get_bbox_char(self, c, isord=False)[source]
get_capheight(self)[source]

Return the cap height as float.

get_familyname(self)[source]

Return the font family name, e.g., 'Times'.

get_fontname(self)[source]

Return the font name, e.g., 'Times-Roman'.

get_fullname(self)[source]

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

get_height_char(self, c, isord=False)[source]

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

get_horizontal_stem_width(self)[source]

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

get_kern_dist(self, c1, c2)[source]

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

get_kern_dist_from_name(self, name1, name2)[source]

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

get_name_char(self, c, isord=False)[source]

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

get_str_bbox(self, s)[source]

Return the string bounding box.

get_str_bbox_and_descent(self, s)[source]

Return the string bounding box and the maximal descent.

get_underline_thickness(self)[source]

Return the underline thickness as float.

get_vertical_stem_width(self)[source]

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

get_weight(self)[source]

Return the font weight, e.g., 'Bold' or 'Roman'.

get_width_char(self, c, isord=False)[source]

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

get_width_from_char_name(self, name)[source]

Get the width of the character from a type1 character name.

get_xheight(self)[source]

Return the xheight as float.

string_width_height(self, s)[source]

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

class matplotlib.afm.CharMetrics

Bases: tuple

Represents the character metrics of a single character.

Notes

The fields do currently only describe a subset of character metrics information defined in the AFM standard.

Create new instance of CharMetrics(width, name, bbox)

bbox

The bbox of the character (B) as a tuple (llx, lly, urx, ury).

name

The character name (N).

width

The character width (WX).

class matplotlib.afm.CompositePart

Bases: tuple

Represents the information on a composite element of a composite char.

Create new instance of CompositePart(name, dx, dy)

dx

x-displacement of the part from the origin.

dy

y-displacement of the part from the origin.

name

Name of the part, e.g. 'acute'.

matplotlib.afm.parse_afm(fh)[source]

[Deprecated]

Notes

Deprecated since version 3.0: