Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address 3820 #3825

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6835,6 +6835,17 @@ def uri_to_dict(uri):
else:
ret[item] = None
return ret

def unescape(name):
"""Unescape '%AB' substrings to chr(0xAB)."""
split = name.replace("%%", "%25") # take care of escaped '%'
split = split.split("%")
newname = split[0]
for item in split[1:]:
piece = item[:2]
newname += chr(int(piece, base=16))
newname += item[2:]
return newname

if rlink and not self.uri.startswith("#"):
self.uri = f"#page={rlink[0] + 1}&zoom=0,{_format_g(rlink[1])},{_format_g(rlink[2])}"
Expand Down Expand Up @@ -6862,7 +6873,7 @@ def uri_to_dict(uri):
m = re.match('^#nameddest=(.*)', self.uri)
assert document
if document and m:
named = m.group(1)
named = unescape(m.group(1))
self.named = document.resolve_names().get(named)
if self.named is None:
# document.resolve_names() does not contain an
Expand Down
Binary file added tests/resources/test-3820.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/test_toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
circular = os.path.join(scriptdir, "resources", "circular-toc.pdf")
full_toc = os.path.join(scriptdir, "resources", "full_toc.txt")
simple_toc = os.path.join(scriptdir, "resources", "simple_toc.txt")
file_3820 = os.path.join(scriptdir, "resources", "test-3820.pdf")
doc = pymupdf.open(filename)


Expand Down Expand Up @@ -274,3 +275,14 @@ def test_3400():
links_actual.append( (page_i, link) )

assert links_actual == links_expected



def test_3820():
"""Ensure all extended TOC items point to pages."""
doc = pymupdf.open(file_3820)
toc = doc.get_toc(simple=False)
for _, _, epage, dest in toc:
assert epage == dest["page"] + 1


Loading