Skip to content

Commit 777e89c

Browse files
committed
pdf: Defer writing annotations until after pages.
This allows annotations which depend on layout of the page to be added.
1 parent 80c40b8 commit 777e89c

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,13 @@ def __init__(self, filename, metadata=None):
694694

695695
self.paths = []
696696

697-
self.pageAnnotations = [] # A list of annotations for the current page
697+
# A list of annotations for each page. Each entry is a tuple of the
698+
# overall Annots object reference that's inserted into the page object,
699+
# followed by a list of the actual annotations.
700+
self._annotations = []
701+
# For annotations added before a page is created; mostly for the
702+
# purpose of newTextnote.
703+
self.pageAnnotations = []
698704

699705
# The PDF spec recommends to include every procset
700706
procsets = [Name(x) for x in "PDF Text ImageB ImageC ImageI".split()]
@@ -720,6 +726,7 @@ def newPage(self, width, height):
720726

721727
self.width, self.height = width, height
722728
contentObject = self.reserveObject('page contents')
729+
annotsObject = self.reserveObject('annotations')
723730
thePage = {'Type': Name('Page'),
724731
'Parent': self.pagesObject,
725732
'Resources': self.resourceObject,
@@ -728,11 +735,12 @@ def newPage(self, width, height):
728735
'Group': {'Type': Name('Group'),
729736
'S': Name('Transparency'),
730737
'CS': Name('DeviceRGB')},
731-
'Annots': self.pageAnnotations,
738+
'Annots': annotsObject,
732739
}
733740
pageObject = self.reserveObject('page')
734741
self.writeObject(pageObject, thePage)
735742
self.pageList.append(pageObject)
743+
self._annotations.append((annotsObject, self.pageAnnotations))
736744

737745
self.beginStream(contentObject.id,
738746
self.reserveObject('length of content stream'))
@@ -750,14 +758,13 @@ def newTextnote(self, text, positionRect=[-100, -100, 0, 0]):
750758
'Contents': text,
751759
'Rect': positionRect,
752760
}
753-
annotObject = self.reserveObject('annotation')
754-
self.writeObject(annotObject, theNote)
755-
self.pageAnnotations.append(annotObject)
761+
self.pageAnnotations.append(theNote)
756762

757763
def finalize(self):
758764
"""Write out the various deferred objects and the pdf end matter."""
759765

760766
self.endStream()
767+
self._write_annotations()
761768
self.writeFonts()
762769
self.writeExtGSTates()
763770
self._write_soft_mask_groups()
@@ -816,6 +823,10 @@ def endStream(self):
816823
self.currentstream.end()
817824
self.currentstream = None
818825

826+
def _write_annotations(self):
827+
for annotsObject, annotations in self._annotations:
828+
self.writeObject(annotsObject, annotations)
829+
819830
def fontName(self, fontprop):
820831
"""
821832
Select a font based on fontprop and return a name suitable for

0 commit comments

Comments
 (0)