8000 Type-1 subsetting · jkseppan/matplotlib@3aaf4c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3aaf4c9

Browse files
committed
Type-1 subsetting
With this I can produce smaller pdf files with usetex in some small tests, but this obviously needs more extensive testing, thus marking as draft. On top of matplotlib#20715. Closes matplotlib#127.
1 parent 9418b35 commit 3aaf4c9

File tree

3 files changed

+286
-13
lines changed

3 files changed

+286
-13
lines changed

lib/matplotlib/backends/_backend_pdf_ps.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,25 @@ class CharacterTracker:
2727
def __init__(self):
2828
self.used = {}
2929

30-
def track(self, font, s):
31-
"""Record that string *s* is being typeset using font *font*."""
30+
@staticmethod
31+
def _get_name(font):
3232
if isinstance(font, str):
3333
# Unused, can be removed after removal of track_characters.
3434
fname = font
35-
else:
35+
elif hasattr(font, 'fname'):
3636
fname = font.fname
37-
self.used.setdefault(fname, set()).update(map(ord, s))
37+
elif hasattr(font, 'name'):
38+
fname = font.name
39+
if isinstance(fname, bytes):
40+
fname = fname.decode('ascii', 'error')
41+
return fname
42+
43+
def get_used(self, font, default=None):
44+
return self.used.get(self._get_name(font), default)
45+
46+
def track(self, font, s):
47+
"""Record that string *s* is being typeset using font *font*."""
48+
self.used.setdefault(self._get_name(font), set()).update(map(ord, s))
3849

3950
# Not public, can be removed when pdf/ps merge_used_characters is removed.
4051
def merge(self, other):

lib/matplotlib/backends/backend_pdf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,8 @@ def _embedTeXFont(self, fontinfo):
963963
t1font = type1font.Type1Font(fontinfo.fontfile)
964964
if fontinfo.effects:
965965
t1font = t1font.transform(fontinfo.effects)
966+
chars = self._character_tracker.get_used(fontinfo.pdfname)
967+
t1font = t1font.subset(chars)
966968
fontdict['BaseFont'] = Name(t1font.prop['FontName'])
967969

968970
# Font descriptors may be shared between differently encoded
@@ -2227,6 +2229,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
22272229
seq += [['font', pdfname, dvifont.size]]
22282230
oldfont = dvifont
22292231
seq += [['text', x1, y1, [bytes([glyph])], x1+width]]
2232+
self.file._character_tracker.track(pdfname, chr(glyph))
22302233

22312234
# Find consecutive text strings with constant y coordinate and
22322235
# combine into a sequence of strings and kerns, or just one

0 commit comments

Comments
 (0)
0