8000 Merge pull request #21752 from anntzer/pdfstream · matplotlib/matplotlib@161ec4a · GitHub
[go: up one dir, main page]

Skip to content

Commit 161ec4a

Browse files
authored
Merge pull request #21752 from anntzer/pdfstream
Add a helper for directly output pdf streams.
2 parents 7c4e9a1 + 1921fef commit 161ec4a

File tree

1 file changed

+23
-37
lines changed

1 file changed

+23
-37
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,11 @@ def endStream(self):
848848
self.currentstream.end()
849849
self.currentstream = None
850850

851+
def outputStream(self, ref, data, *, extra=None):
852+
self.beginStream(ref.id, None, extra)
853+
self.currentstream.write(data)
854+
self.endStream()
855+
851856
def _write_annotations(self):
852857
for annotsObject, annotations in self._annotations:
853858
self.writeObject(annotsObject, annotations)
@@ -1052,13 +1057,10 @@ def createType1Descriptor(self, t1font, fontfile):
10521057

10531058
self.writeObject(fontdescObject, descriptor)
10541059

1055-
self.beginStream(fontfileObject.id, None,
1056-
{'Length1': len(t1font.parts[0]),
1057-
'Length2': len(t1font.parts[1]),
1058-
'Length3': 0})
1059-
self.currentstream.write(t1font.parts[0])
1060-
self.currentstream.write(t1font.parts[1])
1061-
self.endStream()
1060+
self.outputStream(fontfileObject, b"".join(t1font.parts[:2]),
1061+
extra={'Length1': len(t1font.parts[0]),
1062+
'Length2': len(t1font.parts[1]),
1063+
'Length3': 0})
10621064

10631065
return fontdescObject
10641066

@@ -1181,13 +1183,13 @@ def get_char_width(charcode):
11811183
charprocs = {}
11821184
for charname in sorted(rawcharprocs):
11831185
stream = rawcharprocs[charname]
1184-
charprocDict = {'Length': len(stream)}
1186+
charprocDict = {}
11851187
# The 2-byte characters are used as XObjects, so they
11861188
# need extra info in their dictionary
11871189
if charname in multi_byte_chars:
1188-
charprocDict['Type'] = Name('XObject')
1189-
charprocDict['Subtype'] = Name('Form')
1190-
charprocDict['BBox'] = bbox
1190+
charprocDict = {'Type': Name('XObject'),
1191+
'Subtype': Name('Form'),
1192+
'BBox': bbox}
11911193
# Each glyph includes bounding box information,
11921194
# but xpdf and ghostscript can't handle it in a
11931195
# Form XObject (they segfault!!!), so we remove it
@@ -1196,9 +1198,7 @@ def get_char_width(charcode):
11961198
# value.
11971199
stream = stream[stream.find(b"d1") + 2:]
11981200
charprocObject = self.reserveObject('charProc')
1199-
self.beginStream(charprocObject.id, None, charprocDict)
1200-
self.currentstream.write(stream)
1201-
self.endStream()
1201+
self.outputStream(charprocObject, stream, extra=charprocDict)
12021202

12031203
# Send the glyphs with ccode > 255 to the XObject dictionary,
12041204
# and the others to the font itself
@@ -1266,12 +1266,9 @@ def embedTTFType42(font, characters, descriptor):
12661266

12671267
# Make fontfile stream
12681268
descriptor['FontFile2'] = fontfileObject
1269-
self.beginStream(
1270-
fontfileObject.id,
1271-
self.reserveObject('length of font stream'),
1272-
{'Length1': fontdata.getbuffer().nbytes})
1273-
self.currentstream.write(fontdata.getvalue())
1274-
self.endStream()
1269+
self.outputStream(
1270+
fontfileObject, fontdata.getvalue(),
1271+
extra={'Length1': fontdata.getbuffer().nbytes})
12751272

12761273
# Make the 'W' (Widths) array, CidToGidMap and ToUnicode CMap
12771274
# at the same time
@@ -1330,10 +1327,9 @@ def embedTTFType42(font, characters, descriptor):
13301327
rawcharprocs = _get_pdf_charprocs(filename, glyph_ids)
13311328
for charname in sorted(rawcharprocs):
13321329
stream = rawcharprocs[charname]
1333-
charprocDict = {'Length': len(stream)}
1334-
charprocDict['Type'] = Name('XObject')
1335-
charprocDict['Subtype'] = Name('Form')
1336-
charprocDict['BBox'] = bbox
1330+
charprocDict = {'Type': Name('XObject'),
1331+
'Subtype': Name('Form'),
1332+
'BBox': bbox}
13371333
# Each glyph includes bounding box information,
13381334
# but xpdf and ghostscript can't handle it in a
13391335
# Form XObject (they segfault!!!), so we remove it
@@ -1342,27 +1338,17 @@ def embedTTFType42(font, characters, descriptor):
13421338
# value.
13431339
stream = stream[stream.find(b"d1") + 2:]
13441340
charprocObject = self.reserveObject('charProc')
1345-
self.beginStream(charprocObject.id, None, charprocDict)
1346-
self.currentstream.write(stream)
1347-
self.endStream()
1341+
self.outputStream(charprocObject, stream, extra=charprocDict)
13481342

13491343
name = self._get_xobject_glyph_name(filename, charname)
13501344
self.multi_byte_charprocs[name] = charprocObject
13511345

13521346
# CIDToGIDMap stream
13531347
cid_to_gid_map = "".join(cid_to_gid_map).encode("utf-16be")
1354-
self.beginStream(cidToGidMapObject.id,
1355-
None,
1356-
{'Length': len(cid_to_gid_map)})
1357-
self.currentstream.write(cid_to_gid_map)
1358-
self.endStream()
1348+
self.outputStream(cidToGidMapObject, cid_to_gid_map)
13591349

13601350
# ToUnicode CMap
1361-
self.beginStream(toUnicodeMapObject.id,
1362-
None,
1363-
{'Length': unicode_cmap})
1364-
self.currentstream.write(unicode_cmap)
1365-
self.endStream()
1351+
self.outputStream(toUnicodeMapObject, unicode_cmap)
13661352

13671353
descriptor['MaxWidth'] = max_width
13681354

0 commit comments

Comments
 (0)
0