From fbfcdb9ae53d079b39bf068b67498b9755bd6e22 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 29 May 2025 18:20:09 -0400 Subject: [PATCH] Clean up AFM code Since AFM is now private, we can delete unused methods without deprecation. Additionally, add `AFM.get_glyph_name` so that the PostScript mathtext code doesn't need to special case AFM files. --- lib/matplotlib/_afm.py | 145 +++++++------------------- lib/matplotlib/backends/backend_ps.py | 7 +- lib/matplotlib/tests/test_afm.py | 8 ++ lib/matplotlib/tests/test_text.py | 7 -- 4 files changed, 49 insertions(+), 118 deletions(-) diff --git a/lib/matplotlib/_afm.py b/lib/matplotlib/_afm.py index 558efe16392f..9094206c2d7c 100644 --- a/lib/matplotlib/_afm.py +++ b/lib/matplotlib/_afm.py @@ -1,5 +1,5 @@ """ -A python interface to Adobe Font Metrics Files. +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 @@ -16,19 +16,11 @@ >>> from pathlib import Path >>> afm_path = Path(mpl.get_data_path(), 'fonts', 'afm', 'ptmr8a.afm') >>> ->>> from matplotlib.afm import AFM +>>> from matplotlib._afm import AFM >>> with afm_path.open('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 @@ -87,20 +79,23 @@ def _to_bool(s): def _parse_header(fh): """ - Read the font metrics header (up to the char metrics) and returns - a dictionary mapping *key* to *val*. *val* will be converted to the - appropriate python type as necessary; e.g.: + Read the font metrics header (up to the char metrics). - * 'False'->False - * '0'->0 - * '-168 -218 1000 898'-> [-168, -218, 1000, 898] + Returns + ------- + dict + A dictionary mapping *key* to *val*. Dictionary keys are: - Dictionary keys are + StartFontMetrics, FontName, FullName, FamilyName, Weight, ItalicAngle, + IsFixedPitch, FontBBox, UnderlinePosition, UnderlineThickness, Version, + Notice, EncodingScheme, CapHeight, XHeight, Ascender, Descender, + StartCharMetrics - StartFontMetrics, FontName, FullName, FamilyName, Weight, - ItalicAngle, IsFixedPitch, FontBBox, UnderlinePosition, - UnderlineThickness, Version, Notice, EncodingScheme, CapHeight, - XHeight, Ascender, Descender, StartCharMetrics + *val* will be converted to the appropriate Python type as necessary, e.g.,: + + * 'False' -> False + * '0' -> 0 + * '-168 -218 1000 898' -> [-168, -218, 1000, 898] """ header_converters = { b'StartFontMetrics': _to_float, @@ -185,11 +180,9 @@ def _parse_header(fh): def _parse_char_metrics(fh): """ - Parse the given filehandle for character metrics information and return - the information as dicts. + Parse the given filehandle for character metrics information. - It is assumed that the file cursor is on the line behind - 'StartCharMetrics'. + It is assumed that the file cursor is on the line behind 'StartCharMetrics'. Returns ------- @@ -239,14 +232,15 @@ def _parse_char_metrics(fh): def _parse_kern_pairs(fh): """ - Return a kern pairs dictionary; keys are (*char1*, *char2*) tuples and - values are the kern pair value. For example, a kern pairs line like - ``KPX A y -50`` - - will be represented as:: + Return a kern pairs dictionary. - d[ ('A', 'y') ] = -50 + Returns + ------- + dict + Keys are (*char1*, *char2*) tuples and values are the kern pair value. For + example, a kern pairs line like ``KPX A y -50`` will be represented as:: + d['A', 'y'] = -50 """ line = next(fh) @@ -279,8 +273,7 @@ def _parse_kern_pairs(fh): def _parse_composites(fh): """ - Parse the given filehandle for composites information return them as a - dict. + Parse the given filehandle for composites information. It is assumed that the file cursor is on the line behind 'StartComposites'. @@ -363,36 +356,6 @@ def __init__(self, fh): self._metrics, self._metrics_by_name = _parse_char_metrics(fh) self._kern, self._composite = _parse_optional(fh) - def get_bbox_char(self, c, isord=False): - if not isord: - c = ord(c) - return self._metrics[c].bbox - - def string_width_height(self, s): - """ - Return the string width (including kerning) and string height - as a (*w*, *h*) tuple. - """ - if not len(s): - return 0, 0 - total_width = 0 - namelast = None - miny = 1e9 - maxy = 0 - for c in s: - if c == '\n': - continue - wx, name, bbox = self._metrics[ord(c)] - - total_width += wx + self._kern.get((namelast, name), 0) - l, b, w, h = bbox - miny = min(miny, b) - maxy = max(maxy, b + h) - - namelast = name - - return total_width, maxy - miny - def get_str_bbox_and_descent(self, s): """Return the string bounding box and the maximal descent.""" if not len(s): @@ -423,45 +386,29 @@ def get_str_bbox_and_descent(self, s): return left, miny, total_width, maxy - miny, -miny - def get_str_bbox(self, s): - """Return the string bounding box.""" - return self.get_str_bbox_and_descent(s)[:4] - - def get_name_char(self, c, isord=False): - """Get the name of the character, i.e., ';' is 'semicolon'.""" - if not isord: - c = ord(c) - return self._metrics[c].name + def get_glyph_name(self, glyph_ind): # For consistency with FT2Font. + """Get the name of the glyph, i.e., ord(';') is 'semicolon'.""" + return self._metrics[glyph_ind].name - def get_width_char(self, c, isord=False): + def get_char_index(self, c): # For consistency with FT2Font. """ - Get the width of the character from the character metric WX field. + Return the glyph index corresponding to a character code point. + + Note, for AFM fonts, we treat the glyph index the same as the codepoint. """ - if not isord: - c = ord(c) + return c + + def get_width_char(self, c): + """Get the width of the character code from the character metric WX field.""" return self._metrics[c].width def get_width_from_char_name(self, name): """Get the width of the character from a type1 character name.""" return self._metrics_by_name[name].width - def get_height_char(self, c, isord=False): - """Get the bounding box (ink) height of character *c* (space is 0).""" - if not isord: - c = ord(c) - return self._metrics[c].bbox[-1] - - def get_kern_dist(self, c1, c2): - """ - Return the kerning pair distance (possibly 0) for chars *c1* and *c2*. - """ - name1, name2 = self.get_name_char(c1), self.get_name_char(c2) - return self.get_kern_dist_from_name(name1, name2) - def get_kern_dist_from_name(self, name1, name2): """ - Return the kerning pair distance (possibly 0) for chars - *name1* and *name2*. + Return the kerning pair distance (possibly 0) for chars *name1* and *name2*. """ return self._kern.get((name1, name2), 0) @@ -493,7 +440,7 @@ def get_familyname(self): return re.sub(extras, '', name) @property - def family_name(self): + def family_name(self): # For consistency with FT2Font. """The font family name, e.g., 'Times'.""" return self.get_familyname() @@ -516,17 +463,3 @@ def get_xheight(self): def get_underline_thickness(self): """Return the underline thickness as float.""" return self._header[b'UnderlineThickness'] - - def get_horizontal_stem_width(self): - """ - Return the standard horizontal stem width as float, or *None* if - not specified in AFM file. - """ - return self._header.get(b'StdHW', None) - - def get_vertical_stem_width(self): - """ - Return the standard vertical stem width as float, or *None* if - not specified in AFM file. - """ - return self._header.get(b'StdVW', None) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index c1f4348016bb..f6b8455a15a7 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -24,7 +24,6 @@ import matplotlib as mpl from matplotlib import _api, cbook, _path, _text_helpers -from matplotlib._afm import AFM from matplotlib.backend_bases import ( _Backend, FigureCanvasBase, FigureManagerBase, RendererBase) from matplotlib.cbook import is_writable_file_like, file_requires_unicode @@ -787,7 +786,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): width = font.get_width_from_char_name(name) except KeyError: name = 'question' - width = font.get_width_char('?') + width = font.get_width_char(ord('?')) kern = font.get_kern_dist_from_name(last_name, name) last_name = name thisx += kern * scale @@ -835,9 +834,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): lastfont = font.postscript_name, fontsize self._pswriter.write( f"/{font.postscript_name} {fontsize} selectfont\n") - glyph_name = ( - font.get_name_char(chr(num)) if isinstance(font, AFM) else - font.get_glyph_name(font.get_char_index(num))) + glyph_name = font.get_glyph_name(font.get_char_index(num)) self._pswriter.write( f"{ox:g} {oy:g} moveto\n" f"/{glyph_name} glyphshow\n") diff --git a/lib/matplotlib/tests/test_afm.py b/lib/matplotlib/tests/test_afm.py index e5c6a83937cd..80cf8ac60feb 100644 --- a/lib/matplotlib/tests/test_afm.py +++ b/lib/matplotlib/tests/test_afm.py @@ -135,3 +135,11 @@ def test_malformed_header(afm_data, caplog): _afm._parse_header(fh) assert len(caplog.records) == 1 + + +def test_afm_kerning(): + fn = fm.findfont("Helvetica", fontext="afm") + with open(fn, 'rb') as fh: + afm = _afm.AFM(fh) + assert afm.get_kern_dist_from_name('A', 'V') == -70.0 + assert afm.get_kern_dist_from_name('V', 'A') == -80.0 diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 79a9e2d66c46..407d7a96be4d 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -208,13 +208,6 @@ def test_antialiasing(): mpl.rcParams['text.antialiased'] = False # Should not affect existing text. -def test_afm_kerning(): - fn = mpl.font_manager.findfont("Helvetica", fontext="afm") - with open(fn, 'rb') as fh: - afm = mpl._afm.AFM(fh) - assert afm.string_width_height('VAVAVAVAVAVA') == (7174.0, 718) - - @image_comparison(['text_contains.png']) def test_contains(): fig = plt.figure()