Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(comments): test /IRT to links and widgets; say whose a reply's status is

alehelb70d88f23559
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 1d865b0bc..c03628e7f 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -3421,19 +3421,21 @@ fn comments_lock_take_checkmarks_hide_and_summarize() {
/// Replies thread by their `/IRT` reference, not `/NM`: PyMuPDF names the first comment on every
/// page `fitz-A0`, which used to file every reply under page 1's comment. An `/IRT` to the
-/// annotation itself leaves it a comment of its own. Both readers (inspect on open, and the
+/// annotation itself, or to a link (never listed as a comment), leaves it a comment of its own. Both readers (inspect on open, and the
/// comment list the engine refreshes after an edit) must agree.
#[test]
fn replies_thread_by_reference_when_names_repeat_across_pages() {
let pdf = b"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >> endobj
-3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 200] /Annots [5 0 R 8 0 R] >> endobj
+3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 200] /Annots [5 0 R 8 0 R 9 0 R 10 0 R] >> endobj
4 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 200] /Annots [6 0 R 7 0 R] >> endobj
5 0 obj << /Type /Annot /Subtype /Text /Rect [20 140 40 160] /NM (fitz-A0) /T (Alpha) /Contents (COVER) >> endobj
6 0 obj << /Type /Annot /Subtype /Text /Rect [20 140 40 160] /NM (fitz-A0) /T (Beta) /Contents (SECOND) >> endobj
7 0 obj << /Type /Annot /Subtype /Text /Rect [20 100 40 120] /NM (fitz-A1) /IRT 6 0 R /T (Gamma) /Contents (ANSWER) >> endobj
8 0 obj << /Type /Annot /Subtype /Text /Rect [20 100 40 120] /NM (fitz-A1) /IRT 8 0 R /T (Delta) /Contents (SELF) >> endobj
+9 0 obj << /Type /Annot /Subtype /Link /Rect [200 20 260 40] /Border [0 0 0] /Dest [3 0 R /Fit] >> endobj
+10 0 obj << /Type /Annot /Subtype /Text /Rect [20 60 40 80] /NM (fitz-A2) /IRT 9 0 R /T (Echo) /Contents (LINKED) >> endobj
trailer << /Root 1 0 R >>
%%EOF";
let mut s = Session::new().with_clock(|| 1_700_000_000);
@@ -3444,10 +3446,13 @@ trailer << /Root 1 0 R >>
assert_eq!(answer.parent, Some((1, 0)), "{answer:?}");
let looped = all.iter().find(|a| a.contents.as_deref() == Some("SELF")).unwrap();
assert!(!looped.is_reply(), "{looped:?}");
+ let linked = all.iter().find(|a| a.contents.as_deref() == Some("LINKED")).unwrap();
+ assert!(!linked.is_reply(), "{linked:?}");
let text = comment_summary("fitz.pdf", all, SummarySort::Page);
let (cover, second) = text.split_once("SECOND").unwrap();
assert!(!cover.contains("ANSWER"), "page 1's comment has no replies: {text}");
assert!(cover.contains("Number: 2 Author: Delta"), "the self-reply is a comment of its own: {text}");
+ assert!(cover.contains("Number: 3 Author: Echo"), "so is the reply to a link: {text}");
assert_eq!(second.matches(" Author:").count(), replies, "{text}");
};
check(&s, 1);
diff --git a/crates/render/src/inspect.rs b/crates/render/src/inspect.rs
index bb22b00a8..f65a9b697 100644
--- a/crates/render/src/inspect.rs
+++ b/crates/render/src/inspect.rs
@@ -472,6 +472,11 @@ impl Annotation {
/// object, a pop-up, the annotation itself or a loop leaves it a comment of its own, so it stays
/// in view instead of hanging off nothing. Both comment readers (inspect, and the engine's list
/// after an edit) run this.
+///
+/// Only comments can be parents: both readers leave links, form widgets, pop-ups and other
+/// non-markup annotations out of `annotations` (`pdfcraft_annot::is_comment_subtype`), so an
+/// `/IRT` to one of those finds no parent here. Pop-ups are skipped again in case a caller's list
+/// has them.
pub fn link_replies(annotations: &mut [Annotation]) {
let at: HashMap<(usize, usize), usize> =
annotations.iter().enumerate().filter(|(_, a)| a.subtype != "Popup").map(|(i, a)| ((a.page, a.index), i)).collect();
diff --git a/crates/ui-egui/src/comments_panel.rs b/crates/ui-egui/src/comments_panel.rs
index dc157b30d..260fab6e5 100644
--- a/crates/ui-egui/src/comments_panel.rs
+++ b/crates/ui-egui/src/comments_panel.rs
@@ -169,8 +169,10 @@ pub(crate) fn show(
let mut page_counts: HashMap<usize, usize> = HashMap::new();
// Replies find their parent by reference (`/NM` need not be unique) and keep source order.
// A reply at any depth joins the thread of the comment it starts from, as comment_list lists
- // it; a status or checkmark stays with the annotation it was set on. Page badges count roots
- // before filtering.
+ // it. A status or checkmark stays with the annotation it was set on (its `/IRT` names it,
+ // ISO 32000-2 §12.5.6.2): one set on a reply, at any depth, is that reply's and not the
+ // comment's, so the card's badge and checkmark leave it out (comment_list reports it on the
+ // reply). Page badges count roots before filtering.
let tops = pdfcraft_render::thread_tops(&info.annotations);
for a in &info.annotations {
if let Some(parent) = a.parent {
diff --git a/crates/ui-egui/tests/it/comments.rs b/crates/ui-egui/tests/it/comments.rs
index f7b8159b9..1b8f289c1 100644
--- a/crates/ui-egui/tests/it/comments.rs
+++ b/crates/ui-egui/tests/it/comments.rs
@@ -1207,12 +1207,12 @@ trailer << /Root 1 0 R >>
// Contributor-original PDF. Page 1: an unnamed comment with a reply and a reply to that reply,
// and a Replace Text strikeout with its grouped caret. Page 2: an `/IRT` to the annotation
-// itself, to an annotation no page lists (object 11) and to a pop-up.
+// itself, to an annotation no page lists (object 11), to a pop-up, to a link and to a form field.
const THREAD_EDGES: &[u8] = b"%PDF-1.7
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj
2 0 obj << /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >> endobj
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 200] /Annots [5 0 R 6 0 R 7 0 R 8 0 R 9 0 R] >> endobj
-4 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 200] /Annots [10 0 R 12 0 R 13 0 R 14 0 R] >> endobj
+4 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 200] /Annots [10 0 R 12 0 R 13 0 R 14 0 R 15 0 R 16 0 R 17 0 R 18 0 R] >> endobj
5 0 obj << /Type /Annot /Subtype /Text /Rect [20 160 40 180] /T (Alpha) /Contents (Unnamed parent) >> endobj
6 0 obj << /Type /Annot /Subtype /Text /Rect [20 160 40 180] /IRT 5 0 R /T (Beta) /Contents (Direct reply) >> endobj
7 0 obj << /Type /Annot /Subtype /Text /Rect [20 160 40 180] /IRT 6 0 R /T (Gamma) /Contents (Reply to the reply) >> endobj
@@ -1223,6 +1223,10 @@ const THREAD_EDGES: &[u8] = b"%PDF-1.7
12 0 obj << /Type /Annot /Subtype /Text /Rect [20 120 40 140] /IRT 11 0 R /T (Delta) /Contents (Points at an unlisted note) >> endobj
13 0 obj << /Type /Annot /Subtype /Popup /Rect [120 100 280 180] >> endobj
14 0 obj << /Type /Annot /Subtype /Text /Rect [20 80 40 100] /IRT 13 0 R /T (Delta) /Contents (Points at a pop-up) >> endobj
+15 0 obj << /Type /Annot /Subtype /Link /Rect [200 20 260 40] /Border [0 0 0] /Dest [3 0 R /Fit] >> endobj
+16 0 obj << /Type /Annot /Subtype /Widget /FT /Tx /T (field) /Rect [200 60 260 80] >> endobj
+17 0 obj << /Type /Annot /Subtype /Text /Rect [20 50 40 70] /IRT 15 0 R /T (Delta) /Contents (Points at a link) >> endobj
+18 0 obj << /Type /Annot /Subtype /Text /Rect [20 20 40 40] /IRT 16 0 R /T (Delta) /Contents (Points at a form field) >> endobj
trailer << /Root 1 0 R >>
%%EOF";
@@ -1231,7 +1235,8 @@ fn replies_that_lead_nowhere_stay_comments_and_deeper_replies_join_their_thread(
let h = harness_with_bytes(THREAD_EDGES.to_vec(), |app| app.set_option("panel", "comments").unwrap());
// An `/IRT` that reaches no listed comment leaves its annotation a comment of its own,
// rather than a reply shown nowhere.
- for orphan in ["Points at itself", "Points at an unlisted note", "Points at a pop-up"] {
+ // Links, form widgets and pop-ups are never parents: neither reader lists them as comments.
+ for orphan in ["Points at itself", "Points at an unlisted note", "Points at a pop-up", "Points at a link", "Points at a form field"] {
assert!(h.get_by_label(orphan).rect().top() > h.get_by_label("Page 2").rect().top(), "{orphan}");
}
// A reply to a reply joins the thread of the comment it starts from, also when that comment
@@ -1239,9 +1244,9 @@ fn replies_that_lead_nowhere_stay_comments_and_deeper_replies_join_their_thread(
let (parent, direct, deeper) = (h.get_by_label("Unnamed parent"), h.get_by_label("Direct reply"), h.get_by_label("Reply to the reply"));
assert!(parent.rect().top() < direct.rect().top() && direct.rect().top() < deeper.rect().top());
assert!(deeper.rect().top() < h.get_by_label("Struck words").rect().top(), "under its own thread");
- // Comments: the unnamed parent, the strikeout and the three on page 2. Replies: the two
+ // Comments: the unnamed parent, the strikeout and the five on page 2. Replies: the two
// replies, not the Replace Text caret grouped with its strikeout.
- h.get_by_label("5");
+ h.get_by_label("7");
h.get_by_label("2 replies");
}