|
3 | 3 | No checks performed - just contribute to code coverage. |
4 | 4 | """ |
5 | 5 | import os |
| 6 | +import platform |
6 | 7 | import sys |
7 | 8 | import textwrap |
8 | 9 |
|
9 | 10 | import pymupdf |
10 | 11 |
|
| 12 | +import gentle_compare |
| 13 | + |
| 14 | + |
11 | 15 | pymupdfdir = os.path.abspath(f'{__file__}/../..') |
12 | 16 | scriptdir = f'{pymupdfdir}/tests' |
13 | 17 | filename = os.path.join(scriptdir, "resources", "symbol-list.pdf") |
@@ -447,3 +451,66 @@ def test_4139(): |
447 | 451 | assert s['alpha'] == 255 |
448 | 452 | else: |
449 | 453 | 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