Skip to content

Commit 518c213

Browse files
tests/: added tests of stext bboxes.
test_4245() - location of text highlighting. test_4180 - location of text redactions. test_4182 - location of text boxes.
1 parent f42ef85 commit 518c213

File tree

7 files changed

+67
-0
lines changed

7 files changed

+67
-0
lines changed

tests/resources/test_4180.pdf

131 KB
Binary file not shown.
111 KB
Loading

tests/resources/test_4182.pdf

1.09 MB
Binary file not shown.
177 KB
Loading

tests/resources/test_4245.pdf

508 KB
Binary file not shown.
115 KB
Loading

tests/test_textextract.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
No checks performed - just contribute to code coverage.
44
"""
55
import os
6+
import platform
67
import sys
78
import textwrap
89

910
import pymupdf
1011

12+
import gentle_compare
13+
14+
1115
pymupdfdir = os.path.abspath(f'{__file__}/../..')
1216
scriptdir = f'{pymupdfdir}/tests'
1317
filename = os.path.join(scriptdir, "resources", "symbol-list.pdf")
@@ -447,3 +451,66 @@ def test_4139():
447451
assert s['alpha'] == 255
448452
else:
449453
assert not 'alpha' in s
454+
455+
456+
def test_4245():
457+
path = os.path.normpath(f'{__file__}/../../tests/resources/test_4245.pdf')
458+
with pymupdf.open(path) as document:
459+
page = document[0]
460+
regions = page.search_for('Bart Simpson')
461+
for region in regions:
462+
highlight = page.add_highlight_annot(region)
463+
highlight.update()
464+
pixmap = page.get_pixmap()
465+
path_out = os.path.normpath(f'{__file__}/../../tests/resources/test_4245_out.png')
466+
pixmap.save(path_out)
467+
468+
path_expected = os.path.normpath(f'{__file__}/../../tests/resources/test_4245_expected.png')
469+
rms = gentle_compare.pixmaps_rms(path_expected, pixmap)
470+
print(f'{rms=}')
471+
assert rms < 0.01
472+
473+
474+
def test_4180():
475+
path = os.path.normpath(f'{__file__}/../../tests/resources/test_4180.pdf')
476+
with pymupdf.open(path) as document:
477+
page = document[0]
478+
regions = page.search_for('Reference is made')
479+
for region in regions:
480+
page.add_redact_annot(region, fill=(0, 0, 0))
481+
page.apply_redactions()
482+
pixmap = page.get_pixmap()
483+
path_out = os.path.normpath(f'{__file__}/../../tests/resources/test_4180_out.png')
484+
pixmap.save(path_out)
485+
486+
path_expected = os.path.normpath(f'{__file__}/../../tests/resources/test_4180_expected.png')
487+
rms = gentle_compare.pixmaps_rms(path_expected, pixmap)
488+
print(f'{rms=}')
489+
assert rms < 0.01
490+
491+
492+
def test_4182():
493+
path = os.path.normpath(f'{__file__}/../../tests/resources/test_4182.pdf')
494+
with pymupdf.open(path) as document:
495+
page = document[0]
496+
dict_ = page.get_text('dict')
497+
linelist = []
498+
for block in dict_['blocks']:
499+
if block['type'] == 0:
500+
paranum = block['number']
501+
if 'lines' in block:
502+
for line in block.get('lines', ()):
503+
for span in line['spans']:
504+
if span['text'].strip():
505+
page.draw_rect(span['bbox'], color=(1, 0, 0))
506+
linelist.append([paranum, span['bbox'], repr(span['text'])])
507+
pixmap = page.get_pixmap()
508+
path_out = os.path.normpath(f'{__file__}/../../tests/resources/test_4182_out.png')
509+
pixmap.save(path_out)
510+
if platform.system() != 'Windows': # Output on Windows can fail due to non-utf8 stdout.
511+
for l in linelist:
512+
print(l)
513+
path_expected = os.path.normpath(f'{__file__}/../../tests/resources/test_4182_expected.png')
514+
rms = gentle_compare.pixmaps_rms(path_expected, pixmap)
515+
print(f'{rms=}')
516+
assert rms < 0.01

0 commit comments

Comments
 (0)