8000 Add a helper for directly output pdf streams. by anntzer · Pull Request #21752 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from all 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
60 changes: 23 additions & 37 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,11 @@ def endStream(self):
self.currentstream.end()
self.currentstream = None

def outputStream(self, ref, data, *, extra=None):
self.beginStream(ref.id, None, extra)
self.currentstream.write(data)
self.endStream()

def _write_annotations(self):
for annotsObject, annotations in self._annotations:
self.writeObject(annotsObject, annotations)
Expand Down Expand Up @@ -1053,13 +1058,10 @@ def createType1Descriptor(self, t1font, fontfile):

self.writeObject(fontdescObject, descriptor)

self.beginStream(fontfileObject.id, None,
{'Length1': len(t1font.parts[0]),
'Length2': len(t1font.parts[1]),
'Length3': 0})
self.currentstream.write(t1font.parts[0])
self.currentstream.write(t1font.parts[1])
self.endStream()
self.outputStream(fontfileObject, b"".join(t1font.parts[:2]),
extra={'Length1': len(t1font.parts[0]),
'Length2': len(t1font.parts[1]),
'Length3': 0})

return fontdescObject

Expand Down Expand Up @@ -1182,13 +1184,13 @@ def get_char_width(charcode):
charprocs = {}
for charname in sorted(rawcharprocs):
stream = rawcharprocs[charname]
charprocDict = {'Length': len(stream)}
charprocDict = {}
# The 2-byte characters are used as XObjects, so they
# need extra info in their dictionary
if charname in multi_byte_chars:
charprocDict['Type'] = Name('XObject')
charprocDict['Subtype'] = Name('Form')
charprocDict['BBox'] = bbox
charprocDict = {'Type': Name('XObject'),
'Subtype': Name('Form'),
'BBox': bbox}
# Each glyph includes bounding box information,
# but xpdf and ghostscript can't handle it in a
# Form XObject (they segfault!!!), so we remove it
Expand All @@ -1197,9 +1199,7 @@ def get_char_width(charcode):
# value.
stream = stream[stream.find(b"d1") + 2:]
charprocObject = self.reserveObject('charProc')
self.beginStream(charprocObject.id, None, charprocDict)
self.currentstream.write(stream)
self.endStream()
self.outputStream(charprocObject, stream, extra=charprocDict)

# Send the glyphs with ccode > 255 to the XObject dictionary,
# and the others to the font itself
Expand Down Expand Up @@ -1267,12 +1267,9 @@ def embedTTFType42(font, characters, descriptor):

# Make fontfile stream
descriptor['FontFile2'] = fontfileObject
self.beginStream(
fontfileObject.id,
self.reserveObject('length of font stream'),
{'Length1': fontdata.getbuffer().nbytes})
self.currentstream.write(fontdata.getvalue())
self.endStream()
self.outputStream(
fontfileObject, fontdata.getvalue(),
extra={'Length1': fontdata.getbuffer().nbytes})

# Make the 'W' (Widths) array, CidToGidMap and ToUnicode CMap
# at the same time
Expand Down Expand Up @@ -1331,10 +1328,9 @@ def embedTTFType42(font, characters, descriptor):
rawcharprocs = _get_pdf_charprocs(filename, glyph_ids)
for charname in sorted(rawcharprocs):
stream = rawcharprocs[charname]
charprocDict = {'Length': len(stream)}
charprocDict['Type'] = Name('XObject')
charprocDict['Subtype'] = Name('Form')
charprocDict['BBox'] = bbox
charprocDict = {'Type': Name('XObject'),
'Subtype': Name('Form'),
'BBox': bbox}
# Each glyph includes bounding box information,
# but xpdf and ghostscript can't handle it in a
# Form XObject (they segfault!!!), so we remove it
Expand All @@ -1343,27 +1339,17 @@ def embedTTFType42(font, characters, descriptor):
# value.
stream = stream[stream.find(b"d1") + 2:]
charprocObject = self.reserveObject('charProc')
self.beginStream(charprocObject.id, None, charprocDict)
self.currentstream.write(stream)
self.endStream()
self.outputStream(charprocObject, stream, extra=charprocDict)

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

# CIDToGIDMap stream
cid_to_gid_map = "".join(cid_to_gid_map).encode("utf-16be")
self.beginStream(cidToGidMapObject.id,
None,
{'Length': len(cid_to_gid_map)})
self.currentstream.write(cid_to_gid_map)
self.endStream()
self.outputStream(cidToGidMapObject, cid_to_gid_map)

# ToUnicode CMap
self.beginStream(toUnicodeMapObject.id,
None,
{'Length': unicode_cmap})
self.currentstream.write(unicode_cmap)
self.endStream()
self.outputStream(toUnicodeMapObject, unicode_cmap)

descriptor['MaxWidth'] = max_width

Expand Down
0