MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / test_4179

Function test_4179

tests/test_textextract.py:575–661  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

573
574
575def test_4179():
576 if os.environ.get('PYMUPDF_USE_EXTRA') == '0':
577 # Looks like Python code doesn't behave same as C++, probably because
578 # of the code not being correct for Python's native unicode strings.
579 #
580 print(f'test_4179(): Not running with PYMUPDF_USE_EXTRA=0 because known to fail.')
581 return
582 # We check that using TEXT_ACCURATE_BBOXES gives the correct boxes. But
583 # this also requires that we disable PyMuPDF quad corrections.
584 #
585 path = os.path.normpath(f'{__file__}/../../tests/resources/test_4179.pdf')
586
587 # Disable anti-aliasing to avoid our drawing of multiple identical bboxes
588 # (from normal/accurate bboxes) giving slightly different results.
589 aa = pymupdf.mupdf.fz_aa_level()
590 uqc = pymupdf._globals.skip_quad_corrections
591 pymupdf.TOOLS.set_aa_level(0)
592 pymupdf.TOOLS.unset_quad_corrections(True)
593 assert pymupdf._globals.skip_quad_corrections
594 try:
595 with pymupdf.open(path) as document:
596 page = document[0]
597
598 char_sqrt = b'\xe2\x88\x9a'.decode()
599
600 # Search with defaults.
601 bboxes_search = page.search_for(char_sqrt)
602 assert len(bboxes_search) == 1
603 print(f'bboxes_search[0]:\n {bboxes_search[0]!r}')
604 page.draw_rect(bboxes_search[0], color=(1, 0, 0))
605 rms = gentle_compare.rms(bboxes_search[0], (250.0489959716797, 91.93604278564453, 258.34783935546875, 101.34073638916016))
606 assert rms < 0.01
607
608 # Search with TEXT_ACCURATE_BBOXES.
609 bboxes_search_accurate = page.search_for(
610 char_sqrt,
611 flags = (0
612 | pymupdf.TEXT_DEHYPHENATE
613 | pymupdf.TEXT_PRESERVE_WHITESPACE
614 | pymupdf.TEXT_PRESERVE_LIGATURES
615 | pymupdf.TEXT_MEDIABOX_CLIP
616 | pymupdf.TEXT_ACCURATE_BBOXES
617 ),
618 )
619 assert len(bboxes_search_accurate) == 1
620 print(f'bboxes_search_accurate[0]\n {bboxes_search_accurate[0]!r}')
621 page.draw_rect(bboxes_search_accurate[0], color=(0, 1, 0))
622 rms = gentle_compare.rms(bboxes_search_accurate[0], (250.0489959716797, 99.00948333740234, 258.34783935546875, 108.97208404541016))
623 assert rms < 0.01
624
625 # Iterate with TEXT_ACCURATE_BBOXES.
626 bboxes_iterate_accurate = list()
627 dict_ = page.get_text(
628 'rawdict',
629 flags = pymupdf.TEXT_ACCURATE_BBOXES,
630 )
631 linelist = []
632 for block in dict_['blocks']:

Callers

nothing calls this directly

Calls 9

set_aa_levelMethod · 0.80
search_forMethod · 0.80
getMethod · 0.45
draw_rectMethod · 0.45
get_textMethod · 0.45
appendMethod · 0.45
get_pixmapMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…