Skip to content

Commit

Permalink
tests/: added test for #3863 `apply_redactions() does not work as exp…
Browse files Browse the repository at this point in the history
…ected`.
  • Loading branch information
julian-smith-artifex-com committed Oct 1, 2024
1 parent 8b5932d commit f2a7c09
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 1 deletion.
Binary file added tests/resources/test_3863.pdf
Binary file not shown.
Binary file added tests/resources/test_3863.pdf.pdf.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/test_3863.pdf.pdf.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/test_3863.pdf.pdf.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/test_3863.pdf.pdf.3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/test_3863.pdf.pdf.4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/test_3863.pdf.pdf.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/test_3863.pdf.pdf.6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/test_3863.pdf.pdf.7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 54 additions & 1 deletion tests/test_annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
"""
Test PDF annotation insertions.
"""
import pymupdf

import os

import pymupdf
import gentle_compare


pymupdf.TOOLS.set_annot_stem("jorj")

red = (1, 0, 0)
Expand Down Expand Up @@ -364,3 +368,52 @@ def test_3209():
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

def test_3863():
if pymupdf.mupdf_version_tuple < (1, 24, 10):
print(f'test_3863(): not running because {pymupdf.mupdf_version_tuple=} < 1.24.10.')
return

path_in = os.path.normpath(f'{__file__}/../../tests/resources/test_3863.pdf')
path_out = os.path.normpath(f'{__file__}/../../tests/test_3863.pdf.pdf')

# Create redacted PDF.
print(f'Loading {path_in=}.')
with pymupdf.open(path_in) as document:

for num, page in enumerate(document):
print(f"Page {num + 1} - {page.rect}:")

for image in page.get_images(full=True):
print(f" - Image: {image}")

redact_rect = page.rect

if page.rotation in (90, 270):
redact_rect = pymupdf.Rect(0, 0, page.rect.height, page.rect.width)

page.add_redact_annot(redact_rect)
page.apply_redactions(images=pymupdf.PDF_REDACT_IMAGE_NONE)

print(f'Writing to {path_out=}.')
document.save(path_out)

with pymupdf.open(path_out) as document:
assert len(document) == 8

# Create PNG for each page of redacted PDF.
for num, page in enumerate(document):
path_png = f'{path_out}.{num}.png'
pixmap = page.get_pixmap()
print(f'Writing to {path_png=}.')
pixmap.save(path_png)
# Compare with expected png.

print(f'Comparing page PNGs with expected PNGs.')
for num, _ in enumerate(document):
path_png = f'{path_out}.{num}.png'
path_png_expected = f'{path_in}.pdf.{num}.png'
print(f'{path_png=}.')
print(f'{path_png_expected=}.')
rms = gentle_compare.pixmaps_rms(path_png, path_png_expected, ' ')
assert rms == 0

0 comments on commit f2a7c09

Please sign in to comment.