Skip to content

Commit

Permalink
Fix bug in Page.add_ink_annot().
Browse files Browse the repository at this point in the history
Addresses #3209.
  • Loading branch information
julian-smith-artifex-com committed Feb 29, 2024
1 parent aae7d1b commit 9552f5e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
30 changes: 16 additions & 14 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,11 +1573,12 @@ def vertices(self):
CheckParent(self)
annot = self.this
assert isinstance(annot, mupdf.PdfAnnot)
#fz_point point; # point object to work with
annot_obj = mupdf.pdf_annot_obj(annot)
page = mupdf.pdf_annot_page(annot)
page_ctm = mupdf.FzMatrix() # page transformation matrix
dummy = mupdf.FzRect(0) # Will have .m_internal=NULL.
mupdf.pdf_page_transform(mupdf.pdf_annot_page(annot), dummy, page_ctm)
derot = JM_derotate_page_matrix(mupdf.pdf_annot_page(annot))
dummy = mupdf.FzRect() # Out-param for mupdf.pdf_page_transform().
mupdf.pdf_page_transform(page, dummy, page_ctm)
derot = JM_derotate_page_matrix(page)
page_ctm = mupdf.fz_concat(page_ctm, derot)

#----------------------------------------------------------------
Expand All @@ -1586,14 +1587,14 @@ def vertices(self):
# Every pair of floats is one point, that needs to be separately
# transformed with the page transformation matrix.
#----------------------------------------------------------------
o = mupdf.pdf_dict_get(mupdf.pdf_annot_obj(annot), PDF_NAME('Vertices'))
if not o.m_internal: o = mupdf.pdf_dict_get(mupdf.pdf_annot_obj(annot), PDF_NAME('L'))
if not o.m_internal: o = mupdf.pdf_dict_get(mupdf.pdf_annot_obj(annot), PDF_NAME('QuadPoints'))
if not o.m_internal: o = mupdf.pdf_dict_gets(mupdf.pdf_annot_obj(annot), 'CL')

o = mupdf.pdf_dict_get(annot_obj, PDF_NAME('Vertices'))
if not o.m_internal: o = mupdf.pdf_dict_get(annot_obj, PDF_NAME('L'))
if not o.m_internal: o = mupdf.pdf_dict_get(annot_obj, PDF_NAME('QuadPoints'))
if not o.m_internal: o = mupdf.pdf_dict_gets(annot_obj, 'CL')
if o.m_internal:
# handle lists with 1-level depth --------------------------------
#weiter:
# handle lists with 1-level depth
# weiter
res = []
for i in range(0, mupdf.pdf_array_len(o), 2):
x = mupdf.pdf_to_real(mupdf.pdf_array_get(o, i))
Expand All @@ -1602,9 +1603,10 @@ def vertices(self):
point = mupdf.fz_transform_point(point, page_ctm)
res.append( (point.x, point.y))
return res

else:
# InkList has 2-level lists --------------------------------------

o = mupdf.pdf_dict_gets(annot_obj, 'InkList')
if o.m_internal:
# InkList has 2-level lists
#inklist:
res = []
for i in range(mupdf.pdf_array_len(o)):
Expand Down
13 changes: 12 additions & 1 deletion tests/test_annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,15 @@ def test_3131():
first_annot, _ = page.annots()
first_annot.next.type


def test_3209():
pdf = fitz.Document(filetype="pdf")
page = pdf.new_page()
page.add_ink_annot([[(300,300), (400, 380), (350, 350)]])
n = 0
for annot in page.annots():
n += 1
assert annot.vertices == [[(300.0, 300.0), (400.0, 380.0), (350.0, 350.0)]]
assert n == 1
path = os.path.abspath(f'{__file__}/../../tests/test_3209_out.pdf')
pdf.save(path) # Check the output PDF that the annotation is correctly drawn

0 comments on commit 9552f5e

Please sign in to comment.