Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Clean up AFM code#30121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
oscargus merged 1 commit intomatplotlib:mainfromQuLogic:afm-cleanup
May 31, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 39 additions & 106 deletionslib/matplotlib/_afm.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
"""
Apython interface to Adobe Font Metrics Files.
APython 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
Expand All@@ -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
Expand DownExpand Up@@ -87,20 +79,23 @@

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,
Expand DownExpand Up@@ -185,11 +180,9 @@

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
-------
Expand DownExpand Up@@ -239,14 +232,15 @@

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)
Expand DownExpand Up@@ -279,8 +273,7 @@

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'.

Expand DownExpand Up@@ -363,36 +356,6 @@
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):
Expand DownExpand Up@@ -423,45 +386,29 @@

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

Check warning on line 391 in lib/matplotlib/_afm.py

View check run for this annotation

Codecov/ codecov/patch

lib/matplotlib/_afm.py#L391

Added line #L391 was not covered by tests

defget_width_char(self, c, isord=False):
defget_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

Check warning on line 399 in lib/matplotlib/_afm.py

View check run for this annotation

Codecov/ codecov/patch

lib/matplotlib/_afm.py#L399

Added line #L399 was not covered by tests

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)

Expand DownExpand Up@@ -493,7 +440,7 @@
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()

Expand All@@ -516,17 +463,3 @@
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)
7 changes: 2 additions & 5 deletionslib/matplotlib/backends/backend_ps.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand DownExpand Up@@ -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
Expand DownExpand Up@@ -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")
Expand Down
8 changes: 8 additions & 0 deletionslib/matplotlib/tests/test_afm.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -135,3 +135,11 @@ def test_malformed_header(afm_data, caplog):
_afm._parse_header(fh)

assertlen(caplog.records)==1


deftest_afm_kerning():
fn=fm.findfont("Helvetica",fontext="afm")
withopen(fn,'rb')asfh:
afm=_afm.AFM(fh)
assertafm.get_kern_dist_from_name('A','V')==-70.0
assertafm.get_kern_dist_from_name('V','A')==-80.0
7 changes: 0 additions & 7 deletionslib/matplotlib/tests/test_text.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp