pdf: moving an annotation moves its spec geometry too
- Commit
df93895d861df1b4b7e4fbb42f499cdf7b7f290a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 12:06 UTC (5 days ago)
- Parents
965c5fb
update_annotation_rect wrote only the new /Rect, leaving the annotation's spec geometry at the pre-drag position: /QuadPoints (Highlight/Underline/StrikeOut/Squiggly), /Vertices (Polygon/PolyLine), /L (Line), /CL (FreeText callout), /InkList (Ink). In-app it looked moved only because the renderer fits the stale /AP stream onto /Rect; any consumer regenerating appearance from that geometry — including this renderer's own no-AP fallback — paints and selects it where it used to be. duplicate_annotation in the same file already translated all of these by the shift; the move path was the outlier. The move now computes the raw-page-space delta from old to new /Rect and translates QuadPoints/Vertices/L/CL and the nested InkList by it (pure translation, matching the drag); Rect-relative entries are untouched. Regression: a highlight dragged (+20,+15) carries its QuadPoints in memory and in the saved/reopened file, plus an InkList twin and a Kotlin round-trip. Verified: cargo test pdf_render 676 passed; :pdf:testDevUnitTest BUILD SUCCESSFUL; lint errors are pre-existing on untouched files.
Diff
pdf/src/main/rust/src/annotations_part5.rs
diff --git a/pdf/src/main/rust/src/annotations_part5.rs b/pdf/src/main/rust/src/annotations_part5.rsindex f73e7938ea..e1321505cb 100644--- a/pdf/src/main/rust/src/annotations_part5.rs+++ b/pdf/src/main/rust/src/annotations_part5.rs@@ -115,8 +115,50 @@ pub(crate) fn update_annotation_rect(handle: i64, page_index: i32, annot_id: i64 }; let pr = page_rect(doc, page_index, rect); let id = decode_id(annot_id);+ // The drag this serves is a pure translation, and several annotation+ // types carry their real geometry outside /Rect in absolute page+ // coordinates: /QuadPoints (text markup, §12.5.6.10), /Vertices+ // (Polygon/PolyLine), /L (Line), /CL (FreeText callout) and /InkList+ // (Ink). Writing only /Rect left all of them at the pre-drag position+ // in the saved file, so a consumer that synthesizes or regenerates the+ // appearance from that geometry — this renderer's own no-/AP fallback+ // included — paints and selects the annotation where it used to be.+ // `duplicate_annotation` already translates exactly these entries; the+ // move path must too. (/RD and friends are Rect-relative and stay.)+ let old = doc+ .get_dictionary(id)+ .ok()+ .and_then(|d| d.get(b"Rect").ok())+ .and_then(|o| read_rect(doc, o))+ .map(normalize_rect); if let Ok(dict) = doc.get_dictionary_mut(id) { dict.set("Rect", rect_obj(pr));+ if let Some(old) = old {+ let (dx, dy) = (pr[0] - old[0], pr[1] - old[1]);+ if dx != 0.0 || dy != 0.0 {+ for key in [b"QuadPoints".as_ref(), b"Vertices", b"L", b"CL"] {+ if let Ok(Object::Array(a)) = dict.get(key) {+ let mut moved = a.clone();+ offset_flat(&mut moved, dx, dy);+ dict.set(key.to_vec(), Object::Array(moved));+ }+ }+ if let Ok(Object::Array(lists)) = dict.get(b"InkList") {+ let moved: Vec<Object> = lists+ .iter()+ .map(|l| match l {+ Object::Array(pts) => {+ let mut p = pts.clone();+ offset_flat(&mut p, dx, dy);+ Object::Array(p)+ }+ other => other.clone(),+ })+ .collect();+ dict.set("InkList", Object::Array(moved));+ }+ }+ } true } else { false
pdf/src/main/rust/src/cov_annotations_tests.rs
diff --git a/pdf/src/main/rust/src/cov_annotations_tests.rs b/pdf/src/main/rust/src/cov_annotations_tests.rsindex 328e8be3c2..fe100f87f4 100644--- a/pdf/src/main/rust/src/cov_annotations_tests.rs+++ b/pdf/src/main/rust/src/cov_annotations_tests.rs@@ -180,3 +180,82 @@ fn delete_with_wrong_page_preserves_annotation() { assert!(delete_annotation(h, 0, id), "correct-page delete still works"); close_document(h); }++/// Dragging an annotation (SELECT tool, undo, redo all funnel into+/// `update_annotation_rect`) must move the annotation's spec geometry with+/// its `/Rect`: `/QuadPoints` is the REQUIRED geometry of a text-markup+/// annotation (ISO 32000 §12.5.6.10) and `/InkList` of an Ink annotation+/// (§12.5.6.13). Moving only `/Rect` leaves those entries at the pre-drag+/// position in the saved file, so any consumer that synthesizes or+/// regenerates the appearance from them — including this renderer's own+/// no-`/AP` fallback — paints and selects the annotation where it used to+/// be. `duplicate_annotation` already translates exactly these entries;+/// the move path must too.+#[test]+fn moving_an_annotation_moves_its_quadpoints_and_inklist() {+ fn numbers(doc: &Document, id: i64, key: &[u8]) -> Vec<f64> {+ let dict = doc.get_dictionary(decode_id(id)).expect("annot dict");+ match key {+ b"InkList" => {+ let outer = dict.get(key).expect("InkList").as_array().expect("InkList array");+ outer[0].as_array().expect("stroke").iter().map(|o| num(o).expect("n")).collect()+ }+ _ => dict+ .get(key)+ .expect("key")+ .as_array()+ .expect("array")+ .iter()+ .map(|o| num(o).expect("n"))+ .collect(),+ }+ }+ let h = create_empty_document();+ assert_eq!(append_image_page(h, &[0xff, 0xd8], 100, 100), 1);++ // Highlight authored at editor rect [10,10,100,50] (page is unrotated,+ // so editor space == raw page space): /QuadPoints starts as its corners.+ let hl = add_highlight(h, 0, [10.0, 10.0, 100.0, 50.0], 0xFFFFFF00).expect("highlight");+ // Ink stroke (20,20)-(40,40): /InkList starts as the raw points.+ let ink = add_ink(h, 0, 0xFF000000, 2.0, &[20.0, 20.0, 40.0, 40.0]).expect("ink");++ // Drag both by (+20,+15), exactly as the SELECT tool does.+ assert!(update_annotation_rect(h, 0, hl, [30.0, 25.0, 120.0, 65.0]));+ let ink_rect = {+ let reg = registry().lock().unwrap();+ let doc = reg.get(&h).expect("doc");+ numbers(doc, ink, b"Rect")+ };+ assert!(update_annotation_rect(+ h,+ 0,+ ink,+ [ink_rect[0] + 20.0, ink_rect[1] + 15.0, ink_rect[2] + 20.0, ink_rect[3] + 15.0,],+ ));++ {+ let reg = registry().lock().unwrap();+ let doc = reg.get(&h).expect("doc");+ assert_eq!(numbers(doc, hl, b"Rect"), vec![30.0, 25.0, 120.0, 65.0]);+ assert_eq!(+ numbers(doc, hl, b"QuadPoints"),+ vec![30.0, 65.0, 120.0, 65.0, 30.0, 25.0, 120.0, 25.0],+ "the highlight's /QuadPoints must follow its /Rect"+ );+ assert_eq!(+ numbers(doc, ink, b"InkList"),+ vec![40.0, 35.0, 60.0, 55.0],+ "the ink stroke must follow its /Rect"+ );+ }++ // The saved file itself must carry the moved geometry.+ let saved = save_document(h).expect("save");+ let reopened = Document::load_mem(&saved).expect("reopen saved");+ assert_eq!(+ numbers(&reopened, hl, b"QuadPoints"),+ vec![30.0, 65.0, 120.0, 65.0, 30.0, 25.0, 120.0, 25.0],+ "saved /QuadPoints must be the moved ones"+ );+ close_document(h);+}
pdf/src/test/java/com/vayunmathur/pdf/platform/SafePdfDocumentRobolectricTest.kt
diff --git a/pdf/src/test/java/com/vayunmathur/pdf/platform/SafePdfDocumentRobolectricTest.kt b/pdf/src/test/java/com/vayunmathur/pdf/platform/SafePdfDocumentRobolectricTest.ktindex 62412076a1..0618342173 100644--- a/pdf/src/test/java/com/vayunmathur/pdf/platform/SafePdfDocumentRobolectricTest.kt+++ b/pdf/src/test/java/com/vayunmathur/pdf/platform/SafePdfDocumentRobolectricTest.kt@@ -114,6 +114,24 @@ class SafePdfDocumentRobolectricTest { } finally { doc.close() } } } + @Test fun movedHighlightRectSurvivesSaveReopen() { runBlocking {+ val doc = SafePdfDocument.open(ctx, uriFor(minimalPdf(), "move.pdf")); assertNotNull(doc)+ try {+ val hl = doc.addHighlight(0, 100f, 650f, 200f, 670f, 0xFFFFFF00.toInt())+ assertTrue(hl != 0L)+ // Drag by (+20,+15), as the SELECT tool does.+ assertTrue(doc.moveAnnotation(0, hl, 120f, 665f, 220f, 685f))+ val saved = doc.save(); assertNotNull(saved)+ val reopened = SafePdfDocument.open(ctx, uriFor(saved, "move2.pdf")); assertNotNull(reopened)+ try {+ val ann = reopened.annotations(0).singleOrNull()+ assertNotNull(ann, "annots=${reopened.annotations(0)}")+ assertEquals(120f, ann.x0, 0.6f); assertEquals(665f, ann.y0, 0.6f)+ assertEquals(220f, ann.x1, 0.6f); assertEquals(685f, ann.y1, 0.6f)+ } finally { reopened.close() }+ } finally { doc.close() }+ } }+ @Test fun redactionsApply() { runBlocking { val doc = SafePdfDocument.open(ctx, uriFor(minimalPdf(), "redact.pdf")); assertNotNull(doc) try {