comparison tests/test_named_links.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 os
4
5
6 def test_2886():
7 """Confirm correct insertion of a 'named' link."""
8 if not hasattr(pymupdf, "mupdf"):
9 print(f"test_2886(): not running on classic.")
10 return
11
12 path = os.path.abspath(f"{__file__}/../../tests/resources/cython.pdf")
13 doc = pymupdf.open(path)
14 # name "Doc-Start" is a valid named destination in that file
15 link = {
16 "kind": pymupdf.LINK_NAMED,
17 "from": pymupdf.Rect(0, 0, 50, 50),
18 "name": "Doc-Start",
19 }
20 # insert this link in an arbitrary page & rect
21 page = doc[-1]
22 page.insert_link(link)
23 # need this to update the internal MuPDF annotations array
24 page = doc.reload_page(page)
25
26 # our new link must be the last in the following list
27 links = page.get_links()
28 l_dict = links[-1]
29 assert l_dict["kind"] == pymupdf.LINK_NAMED
30 assert l_dict["nameddest"] == link["name"]
31 assert l_dict["from"] == link["from"]
32
33
34 def test_2922():
35 """Confirm correct recycling of a 'named' link.
36
37 Re-insertion of a named link item in 'Page.get_links()' does not have
38 the required "name" key. We test the fallback here that uses key
39 "nameddest" instead.
40 """
41 if not hasattr(pymupdf, "mupdf"):
42 print(f"test_2922(): not running on classic.")
43 return
44
45 path = os.path.abspath(f"{__file__}/../../tests/resources/cython.pdf")
46 doc = pymupdf.open(path)
47 page = doc[2] # page has a few links, all are named
48 links = page.get_links() # list of all links
49 link0 = links[0] # take arbitrary link (1st one is ok)
50 page.insert_link(link0) # insert it again
51 page = doc.reload_page(page) # ensure page updates
52 links = page.get_links() # access all links again
53 link1 = links[-1] # re-inserted link
54
55 # confirm equality of relevant key-values
56 assert link0["nameddest"] == link1["nameddest"]
57 assert link0["page"] == link1["page"]
58 assert link0["to"] == link1["to"]
59 assert link0["from"] == link1["from"]
60
61
62 def test_3301():
63 """Test correct differentiation between URI and LAUNCH links.
64
65 Links encoded as /URI in PDF are converted to either LINK_URI or
66 LINK_LAUNCH in PyMuPDF.
67 This function ensures that the 'Link.uri' containing a ':' colon
68 is converted to a URI if not explicitly starting with "file://".
69 """
70 if not hasattr(pymupdf, "mupdf"):
71 print(f"test_3301(): not running on classic.")
72 return
73
74 # list of links and their expected link "kind" upon extraction
75 text = {
76 "https://www.google.de": pymupdf.LINK_URI,
77 "http://www.google.de": pymupdf.LINK_URI,
78 "mailto:jorj.x.mckie@outlook.de": pymupdf.LINK_URI,
79 "www.wikipedia.de": pymupdf.LINK_LAUNCH,
80 "awkward:resource": pymupdf.LINK_URI,
81 "ftp://www.google.de": pymupdf.LINK_URI,
82 "some.program": pymupdf.LINK_LAUNCH,
83 "file://some.program": pymupdf.LINK_LAUNCH,
84 "another.exe": pymupdf.LINK_LAUNCH,
85 }
86
87 # make enough "from" rectangles
88 r = pymupdf.Rect(0, 0, 50, 20)
89 rects = [r + (0, r.height * i, 0, r.height * i) for i in range(len(text.keys()))]
90
91 # make test page and insert above links as kind=LINK_URI
92 doc = pymupdf.open()
93 page = doc.new_page()
94 for i, k in enumerate(text.keys()):
95 link = {"kind": pymupdf.LINK_URI, "uri": k, "from": rects[i]}
96 page.insert_link(link)
97
98 # re-cycle the PDF preparing for link extraction
99 pdfdata = doc.write()
100 doc = pymupdf.open("pdf", pdfdata)
101 page = doc[0]
102 for link in page.get_links():
103 # Extract the link text. Must be 'file' or 'uri'.
104 t = link["uri"] if (_ := link.get("file")) is None else _
105 assert text[t] == link["kind"]