
matplotlib.afm¶This is a python interface to Adobe Font Metrics Files. Although anumber of other python implementations exist, and may be more completethan this, it was decided not to go with them because they wereeither:
- 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 thanfigure out how to get just what was needed.
It is pretty easy to use, and requires only built-in python libs:
>>>frommatplotlibimportrcParams>>>importos.path>>>afm_fname=os.path.join(rcParams['datapath'],...'fonts','afm','ptmr8a.afm')>>>>>>frommatplotlib.afmimportAFM>>>withopen(afm_fname)asfh:...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)¶Bases:object
Parse the AFM file in file objectfh
family_name¶get_angle()¶Return the fontangle as float
get_bbox_char(c,isord=False)¶get_capheight()¶Return the cap height as float
get_familyname()¶Return the font family name, e.g., ‘Times’
get_fontname()¶Return the font name, e.g., ‘Times-Roman’
get_fullname()¶Return the font full name, e.g., ‘Times-Roman’
get_height_char(c,isord=False)¶Get the height of characterc from the bounding box. Thisis the ink height (space is 0)
get_horizontal_stem_width()¶Return the standard horizontal stem width as float, orNone ifnot specified in AFM file.
get_kern_dist(c1,c2)¶Return the kerning pair distance (possibly 0) for charsc1andc2
get_kern_dist_from_name(name1,name2)¶Return the kerning pair distance (possibly 0) for charsname1 andname2
get_name_char(c,isord=False)¶Get the name of the character, i.e., ‘;’ is ‘semicolon’
get_str_bbox(s)¶Return the string bounding box
get_str_bbox_and_descent(s)¶Return the string bounding box
get_underline_thickness()¶Return the underline thickness as float
get_vertical_stem_width()¶Return the standard vertical stem width as float, orNone ifnot specified in AFM file.
get_weight()¶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 WXfield
get_width_from_char_name(name)¶Get the width of the character from a type1 character name
get_xheight()¶Return the xheight as float
string_width_height(s)¶Return the string width (including kerning) and string heightas a (w,h) tuple.
matplotlib.afm.parse_afm(fh)¶Parse the Adobe Font Metics file in file handlefh. Return valueis a (dhead,dcmetrics,dkernpairs,dcomposite) tuple wheredhead is a_parse_header() dict,dcmetrics is a_parse_composites() dict,dkernpairs is a_parse_kern_pairs() dict (possibly {}), anddcomposite is a_parse_composites() dict (possibly {})