comparison tests/test_clip_page.py @ 1:1d09e1dec1d9 upstream

ADD: PyMuPDF v1.26.4: the original sdist. It does not yet contain MuPDF. This normally will be downloaded when building PyMuPDF.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:37:51 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 1:1d09e1dec1d9
1 """
2 Test Page method clip_to_rect.
3 """
4
5 import os
6 import pymupdf
7
8
9 def test_clip():
10 """
11 Clip a Page to a rectangle and confirm that no text has survived
12 that is completely outside the rectangle..
13 """
14 scriptdir = os.path.dirname(os.path.abspath(__file__))
15 rect = pymupdf.Rect(200, 200, 400, 500)
16 filename = os.path.join(scriptdir, "resources", "v110-changes.pdf")
17 doc = pymupdf.open(filename)
18 page = doc[0]
19 page.clip_to_rect(rect) # clip the page to the rectangle
20 # capture font warning message of MuPDF
21 assert pymupdf.TOOLS.mupdf_warnings() == "bogus font ascent/descent values (0 / 0)"
22 # extract all text characters and assert that each one
23 # has a non-empty intersection with the rectangle.
24 chars = [
25 c
26 for b in page.get_text("rawdict")["blocks"]
27 for l in b["lines"]
28 for s in l["spans"]
29 for c in s["chars"]
30 ]
31 for char in chars:
32 bbox = pymupdf.Rect(char["bbox"])
33 if bbox.is_empty:
34 continue
35 assert bbox.intersects(
36 rect
37 ), f"Character '{char['c']}' at {bbox} is outside of {rect}."