Skip to content

Commit fab7dad

Browse files
authored
Merge pull request matplotlib#32034 from meeseeksmachine/auto-backport-of-pr-31975-on-v3.11.x
Backport PR matplotlib#31975 on branch v3.11.x (pdf/ps: Stop retaining glyph indices in font subsets)
2 parents 8c31804 + be2a54d commit fab7dad

12 files changed

Lines changed: 1745 additions & 37593 deletions

File tree

ci/minver-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
contourpy==1.0.1
44
cycler==0.10
5-
fonttools==4.22.0
5+
fonttools==4.28.2
66
importlib-resources==3.2.0
77
kiwisolver==1.3.2
88
meson-python==0.13.2

doc/install/dependencies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ reference.
2424
* `contourpy <https://pypi.org/project/contourpy/>`_ (>= 1.0.1)
2525
* `cycler <https://matplotlib.org/cycler/>`_ (>= 0.10.0)
2626
* `dateutil <https://pypi.org/project/python-dateutil/>`_ (>= 2.7)
27-
* `fontTools <https://fonttools.readthedocs.io/en/latest/>`_ (>= 4.22.0)
27+
* `fontTools <https://fonttools.readthedocs.io/en/latest/>`_ (>= 4.28.2)
2828
* `kiwisolver <https://github.com/nucleic/kiwi>`_ (>= 1.3.1)
2929
* `NumPy <https://numpy.org>`_ (>= 1.25)
3030
* `packaging <https://pypi.org/project/packaging/>`_ (>= 20.0)

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ dependencies:
1515
- cxx-compiler
1616
- contourpy>=1.0.1
1717
- cycler>=0.10.0
18-
- fonttools>=4.22.0
18+
- fonttools>=4.28.2
1919
- importlib-resources>=3.2.0
2020
- kiwisolver>=1.3.1
2121
- pybind11>=2.13.2

lib/matplotlib/backends/_backend_pdf_ps.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from __future__ import annotations
66

7+
from contextlib import contextmanager
78
from io import BytesIO
89
import functools
910
import logging
@@ -18,6 +19,9 @@
1819

1920

2021
if typing.TYPE_CHECKING:
22+
from collections.abc import Generator
23+
from contextlib import AbstractContextManager
24+
2125
from .font_manager import FontPath
2226
from .ft2font import CharacterCodeType, FT2Font, GlyphIndexType
2327
from fontTools.ttLib import TTFont
@@ -35,7 +39,30 @@ def _cached_get_afm_from_fname(fname):
3539
return AFM(fh)
3640

3741

38-
def get_glyphs_subset(fontfile: FontPath, glyphs: set[GlyphIndexType]) -> TTFont:
42+
class SubsetResults(typing.NamedTuple):
43+
"""
44+
The results of a subsetting operation on a font.
45+
46+
Attributes
47+
----------
48+
font : TTFont
49+
An open font object representing the subset.
50+
glyph_index_map : dict
51+
Mapping of requested glyph indices to actual subset glyph indices.
52+
"""
53+
54+
font: TTFont
55+
glyph_index_map: dict[GlyphIndexType, GlyphIndexType]
56+
57+
@contextmanager
58+
def _as_cm(self) -> Generator[SubsetResults, None, None]:
59+
with self.font:
60+
yield self
61+
62+
63+
def get_glyphs_subset(
64+
fontfile: FontPath, glyphs: set[GlyphIndexType]
65+
) -> AbstractContextManager[SubsetResults]:
3966
"""
4067
Subset a TTF font.
4168
@@ -50,12 +77,10 @@ def get_glyphs_subset(fontfile: FontPath, glyphs: set[GlyphIndexType]) -> TTFont
5077
5178
Returns
5279
-------
53-
fontTools.ttLib.ttFont.TTFont
54-
An open font object representing the subset, which needs to
55-
be closed by the caller.
80+
SubsetResults
81+
The font and new glyph index mapping.
5682
"""
57-
options = subset.Options(glyph_names=True, recommended_glyphs=True,
58-
retain_gids=True)
83+
options = subset.Options(glyph_names=True, recommended_glyphs=True)
5984

6085
# Prevent subsetting extra tables.
6186
options.drop_tables += [
@@ -87,7 +112,7 @@ def get_glyphs_subset(fontfile: FontPath, glyphs: set[GlyphIndexType]) -> TTFont
87112
subsetter = subset.Subsetter(options=options)
88113
subsetter.populate(gids=glyphs)
89114
subsetter.subset(font)
90-
return font
115+
return SubsetResults(font, subsetter.glyph_index_map)._as_cm()
91116

92117

93118
def font_as_file(font):

lib/matplotlib/backends/backend_pdf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ def embedTTFType42(font, subset_index, charmap, descriptor):
12471247
_log.debug("SUBSET %r:%d characters: %s", filename, subset_index, charmap)
12481248
with _backend_pdf_ps.get_glyphs_subset(filename,
12491249
charmap.values()) as subset:
1250-
fontdata = _backend_pdf_ps.font_as_file(subset)
1250+
fontdata = _backend_pdf_ps.font_as_file(subset.font)
12511251
_log.debug(
12521252
"SUBSET %r:%d %d -> %d", filename, subset_index,
12531253
os.stat(filename).st_size, fontdata.getbuffer().nbytes
@@ -1290,7 +1290,8 @@ def embedTTFType42(font, subset_index, charmap, descriptor):
12901290
cid_to_gid_map = ['\0'] * 65536
12911291
widths = []
12921292
max_ccode = 0
1293-
for ccode, gind in charmap.items():
1293+
for ccode, orig_glyph_index in charmap.items():
1294+
gind = subset.glyph_index_map[orig_glyph_index]
12941295
glyph = font.load_glyph(gind,
12951296
flags=LoadFlags.NO_SCALE | LoadFlags.NO_HINTING)
12961297
widths.append((ccode, cvt(glyph.horiAdvance)))

lib/matplotlib/backends/backend_ps.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,13 @@ def _font_to_ps_type42(font_path, subset_index, glyph_indices, fh):
190190
with (fontTools.ttLib.TTFont(font_path.path,
191191
fontNumber=font_path.face_index) as font,
192192
_backend_pdf_ps.get_glyphs_subset(font_path, glyph_indices) as subset):
193-
fontdata = _backend_pdf_ps.font_as_file(subset).getvalue()
193+
new_font = subset.font
194+
fontdata = _backend_pdf_ps.font_as_file(new_font).getvalue()
194195
_log.debug(
195196
"SUBSET %s:%d %d -> %d", font_path, subset_index,
196197
os.stat(font_path).st_size, len(fontdata)
197198
)
198-
fh.write(_serialize_type42(font, subset_index, subset, fontdata))
199+
fh.write(_serialize_type42(font, subset_index, new_font, fontdata))
199200
except RuntimeError:
200201
_log.warning(
201202
"The PostScript backend does not currently support the selected font (%s).",
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)