comparison tests/test_2634.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 import pymupdf
2
3 import difflib
4 import json
5 import os
6 import pprint
7
8
9 def test_2634():
10 if not hasattr(pymupdf, 'mupdf'):
11 print('test_2634(): Not running on classic.')
12 return
13 path = os.path.abspath(f'{__file__}/../../tests/resources/test_2634.pdf')
14 with pymupdf.open(path) as pdf, pymupdf.open() as new:
15 new.insert_pdf(pdf)
16 new.set_toc(pdf.get_toc(simple=False))
17 toc_pdf = pdf.get_toc(simple=False)
18 toc_new = new.get_toc(simple=False)
19
20 def clear_xref(toc):
21 '''
22 Clear toc items that naturally differ.
23 '''
24 for item in toc:
25 d = item[3]
26 if 'collapse' in d:
27 d['collapse'] = 'dummy'
28 if 'xref' in d:
29 d['xref'] = 'dummy'
30
31 clear_xref(toc_pdf)
32 clear_xref(toc_new)
33
34 print('toc_pdf')
35 for item in toc_pdf: print(item)
36 print()
37 print('toc_new')
38 for item in toc_new: print(item)
39
40 toc_text_pdf = pprint.pformat(toc_pdf, indent=4).split('\n')
41 toc_text_new = pprint.pformat(toc_new, indent=4).split('\n')
42
43 diff = difflib.unified_diff(
44 toc_text_pdf,
45 toc_text_new,
46 lineterm='',
47 )
48 print('\n'.join(diff))
49
50 # Check 'to' points are identical apart from rounding errors.
51 #
52 assert len(toc_new) == len(toc_pdf)
53 for a, b in zip(toc_pdf, toc_new):
54 a_dict = a[3]
55 b_dict = b[3]
56 if 'to' in a_dict:
57 assert 'to' in b_dict
58 a_to = a_dict['to']
59 b_to = b_dict['to']
60 assert isinstance(a_to, pymupdf.Point)
61 assert isinstance(b_to, pymupdf.Point)
62 if a_to != b_to:
63 print(f'Points not identical: {a_to=} {b_to=}.')
64 assert abs(a_to.x - b_to.x) < 0.01
65 assert abs(a_to.y - b_to.y) < 0.01