@@ -257,6 +257,8 @@ def log( text='', caller=1):
257257 line = frame_record.lineno
258258 function = frame_record.function
259259 text = f'{filename}:{line}:{function}(): {text}'
260+ del stack
261+ # Leaving <stack> to be garbage collected, appears to change behaviour.
260262 if _g_log_items_active:
261263 _g_log_items.append(text)
262264 if _g_out_log:
@@ -3241,13 +3243,25 @@ def cre_annot(lnk, xref_dst, pno_src, ctm):
32413243 if len(links) == 0: # no links there
32423244 page_src = None
32433245 continue
3244- ctm = ~page_src.transformation_matrix # calc page transformation matrix
32453246 page_dst = doc1[pno_dst[i]] # load destination page
3247+
3248+ # In our call above to page_src.get_links(), we end up in
3249+ # fz_load_links(). This extracts the raw rects (encoded as strings
3250+ # such as `/Rect[10 782 40 822]`) and multiplies them by page_ctm
3251+ # from pdf_page_transform().
3252+ #
3253+ # We want to recreate the original raw rects, so we need to
3254+ # multiply by inverse of page_ctm. This fixes #4958.
3255+ ctm = mupdf.FzMatrix()
3256+ page_src_pdf_document = _as_pdf_page(page_src)
3257+ mupdf.pdf_page_transform(page_src_pdf_document, mupdf.FzRect(0), ctm)
3258+ ictm = Matrix(mupdf.fz_invert_matrix(ctm))
3259+
32463260 link_tab = [] # store all link definitions here
32473261 for l in links:
32483262 if l["kind"] == LINK_GOTO and (l["page"] not in pno_src):
32493263 continue # GOTO link target not in copied pages
3250- annot_text = cre_annot(l, xref_dst, pno_src, ctm )
3264+ annot_text = cre_annot(l, xref_dst, pno_src, ictm )
32513265 if annot_text:
32523266 link_tab.append(annot_text)
32533267 if link_tab != []:
0 commit comments