Skip to content

Commit 3c17311

Browse files
committed
Handle file-like objects instead of saving
1 parent c83aef7 commit 3c17311

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

lib/matplotlib/backends/_backend_pdf_ps.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def _cached_get_afm_from_fname(fname):
1919
return AFM(fh)
2020

2121

22-
def getSubset(self, fontfile, characters):
22+
def getSubset(fontfile, characters):
2323
"""
2424
Subset a TTF font
2525
2626
Reads the named fontfile and restricts the font to the characters.
27-
Returns a serialization of the subset font as bytes.
27+
Returns a serialization of the subset font as file-like object.
2828
"""
2929

3030
options = subset.Options(glyph_names=True, recommended_glyphs=True)
@@ -36,7 +36,7 @@ def getSubset(self, fontfile, characters):
3636
subsetter.subset(font)
3737
fh = BytesIO()
3838
font.save(fh, reorderTables=False)
39-
return fh.getvalue()
39+
return fh
4040
finally:
4141
font.close()
4242

lib/matplotlib/backends/backend_pdf.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from datetime import datetime
99
from enum import Enum
1010
from functools import total_ordering
11+
from io import BytesIO
1112
import itertools
1213
import logging
1314
import math
@@ -1193,22 +1194,19 @@ def embedTTFType42(font, characters, descriptor):
11931194
wObject = self.reserveObject('Type 0 widths')
11941195
toUnicodeMapObject = self.reserveObject('ToUnicode map')
11951196

1196-
print(f"SUBSET {filename} characters: "
1197-
f"{''.join(chr(c) for c in characters)}")
1197+
_log.debug(f"SUBSET {filename} characters: "
1198+
f"{''.join(chr(c) for c in characters)}")
11981199
fontdata = _backend_pdf_ps.getSubset(
11991200
filename,
12001201
''.join(chr(c) for c in characters)
12011202
)
1202-
print(f'SUBSET {filename} {os.stat(filename).st_size}'
1203-
f' ↦ {len(fontdata)}')
1203+
_log.debug(f'SUBSET {filename} {os.stat(filename).st_size}'
1204+
f' ↦ {fontdata.getbuffer().nbytes}')
12041205

12051206
# reload the font object from the subset
12061207
# (all the necessary data could probably be obtained directly
12071208
# using fontLib.ttLib)
1208-
with tempfile.NamedTemporaryFile(suffix='.ttf') as tmp:
1209-
tmp.write(fontdata)
1210-
tmp.seek(0, 0)
1211-
font = FT2Font(tmp.name)
1209+
font = FT2Font(fontdata)
12121210

12131211
cidFontDict = {
12141212
'Type': Name('Font'),
@@ -1237,8 +1235,8 @@ def embedTTFType42(font, characters, descriptor):
12371235
self.beginStream(
12381236
fontfileObject.id,
12391237
self.reserveObject('length of font stream'),
1240-
{'Length1': len(fontdata)})
1241-
self.currentstream.write(fontdata)
1238+
{'Length1': fontdata.getbuffer().nbytes})
1239+
self.currentstream.write(fontdata.getvalue())
12421240
self.endStream()
12431241

12441242
# Make the 'W' (Widths) array, CidToGidMap and ToUnicode CMap

lib/matplotlib/backends/backend_ps.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,20 +965,22 @@ def print_figure_impl(fh):
965965
_log.debug(
966966
f"SUBSET {font_path} "
967967
f"{os.stat(font_path).st_size} "
968-
f"↦ {len(fontdata)}"
968+
f"↦ {fontdata.getbuffer().nbytes}"
969969
)
970970

971971
# give ttconv a subsetted font
972972
# along with updated glyph_ids
973973
with tempfile.NamedTemporaryFile(
974974
suffix=".ttf"
975975
) as tmp:
976-
tmp.write(fontdata)
977-
tmp.seek(0, 0)
978-
font = FT2Font(tmp.name)
976+
font = FT2Font(fontdata)
979977
glyph_ids = [
980978
font.get_char_index(c) for c in chars
981979
]
980+
tmp.write(fontdata.getvalue())
981+
tmp.seek(0, 0)
982+
# TODO: allow convert_ttf_to_ps
983+
# to input file objects (BytesIO)
982984
convert_ttf_to_ps(
983985
os.fsencode(tmp.name),
984986
fh,

0 commit comments

Comments
 (0)