From c8d0f17b251e1316b03b330eebf19d0af1b6dd36 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 13:57:22 -0500 Subject: [PATCH 1/4] xfa: cache repeated prototype child counts without changing work charges --- crates/xfa/src/proto.rs | 21 ++++++++++++++++++++- crates/xfa/src/proto/tests.rs | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/crates/xfa/src/proto.rs b/crates/xfa/src/proto.rs index ae1505804e..99dc18b689 100644 --- a/crates/xfa/src/proto.rs +++ b/crates/xfa/src/proto.rs @@ -74,6 +74,7 @@ pub(crate) fn expand(template: Node) -> (Option, Vec) { soms: HashMap::new(), search: MAX_SEARCH, work: MAX_WORK, + child_counts: HashMap::new(), active: HashMap::new(), missing: false, external: false, @@ -124,6 +125,8 @@ struct Writer<'a> { search: usize, /// Children, attributes and namespaces left to look at while merging layers. work: usize, + /// Bounded memoization only; each use still pays the full child count. + child_counts: HashMap, /// The elements being written around the current one: inheriting from one is a loop. active: HashMap, missing: bool, @@ -132,6 +135,22 @@ struct Writer<'a> { layers_cut: bool, } +/// Counting a reused prototype must not itself repeatedly walk its descendants. +/// Cache only complete direct-child counts, and cap the optimization's memory. +/// Truncated counts are not exact and must never be cached. Budget charging is unchanged. +fn child_count(node: Node<'_>, work: usize, cache: &mut HashMap) -> usize { + const MAX_CACHED_COUNTS: usize = 4_096; + let stop = work.saturating_add(1); + if let Some(count) = cache.get(&node.id()) { + return (*count).min(stop); + } + let count = node.children().take(stop).count(); + if count < stop && cache.len() < MAX_CACHED_COUNTS { + cache.insert(node.id(), count); + } + count +} + fn escape(s: &str, attribute: bool, out: &mut String) { for c in s.chars() { match c { @@ -439,7 +458,7 @@ impl<'a> Writer<'a> { // further than the budget left), attributes and namespaces, and the scope around it. let mut cost = scope.len().saturating_add(layers.len()); for l in &layers { - let children = l.children().take(self.work.saturating_add(1)).count(); + let children = child_count(*l, self.work, &mut self.child_counts); cost = cost.saturating_add(children).saturating_add(l.attributes().len()).saturating_add(l.namespaces().len()); } if self.failed.is_some() || !self.charge(cost) { diff --git a/crates/xfa/src/proto/tests.rs b/crates/xfa/src/proto/tests.rs index ba7f6f8316..636886bb62 100644 --- a/crates/xfa/src/proto/tests.rs +++ b/crates/xfa/src/proto/tests.rs @@ -399,3 +399,28 @@ fn a_som_expression_past_its_length_limit_is_not_followed() { assert_eq!(field(&t, "long").common.h, None); assert!(close(field(&t, "short").common.h, 2.0 * MM)); } + +#[test] +fn cached_child_counts_preserve_truncation_and_charge_when_budget_shrinks() { + let doc = crate::xml::Document::parse("").unwrap(); + let node = doc.root_element(); + let mut cache = std::collections::HashMap::new(); + assert_eq!(super::child_count(node, 1, &mut cache), 2); + assert!(cache.is_empty(), "truncated counts are not reusable"); + assert_eq!(super::child_count(node, 10, &mut cache), 3); + assert_eq!(cache.get(&node.id()), Some(&3)); + for remaining in 0..=5 { + assert_eq!(super::child_count(node, remaining, &mut cache), node.children().take(remaining + 1).count()); + } + assert_eq!(super::child_count(node, 10, &mut cache), 3, "a cached use is still charged"); +} + +#[test] +fn child_count_memoization_has_a_fixed_entry_limit() { + let doc = crate::xml::Document::parse(&format!("{}", "".repeat(5_000))).unwrap(); + let mut cache = std::collections::HashMap::new(); + for node in doc.root_element().children() { + assert_eq!(super::child_count(node, 1, &mut cache), 0); + } + assert_eq!(cache.len(), 4_096); +} From 2f8820eda34a5b83af2912098201564e390c24bb Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 13:57:45 -0500 Subject: [PATCH 2/4] test(xfa): verify cached layers consume the same writer budget --- crates/xfa/src/proto/tests.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/xfa/src/proto/tests.rs b/crates/xfa/src/proto/tests.rs index 636886bb62..efbec768a0 100644 --- a/crates/xfa/src/proto/tests.rs +++ b/crates/xfa/src/proto/tests.rs @@ -424,3 +424,38 @@ fn child_count_memoization_has_a_fixed_entry_limit() { } assert_eq!(cache.len(), 4_096); } + +#[test] +fn cached_layers_still_subtract_work_and_stop_at_the_budget() { + let doc = crate::xml::Document::parse("").unwrap(); + let node = doc.root_element(); + let mut writer = super::Writer { + template: node, + out: String::new(), + limit: super::MAX_OUTPUT, + failed: None, + ids: None, + refs: std::collections::HashMap::new(), + soms: std::collections::HashMap::new(), + search: super::MAX_SEARCH, + work: 100, + child_counts: std::collections::HashMap::new(), + active: std::collections::HashMap::new(), + missing: false, + external: false, + looped: false, + layers_cut: false, + }; + writer.element(&[node], &super::Scope::new(), 0); + assert!(writer.failed.is_none()); + let first_cost = 100 - writer.work; + assert!(first_cost > 0); + assert!(!writer.child_counts.is_empty()); + let before = writer.work; + writer.element(&[node], &super::Scope::new(), 0); + assert!(writer.failed.is_none()); + assert_eq!(before - writer.work, first_cost, "cached layers pay the same work charge"); + writer.work = 0; + writer.element(&[node], &super::Scope::new(), 0); + assert!(writer.failed.as_ref().is_some_and(|why| why.contains("children, attributes and namespaces"))); +} From ae30592466a813c22fdd145536b18447ab9660fc Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:55:06 +0700 Subject: [PATCH 3/4] Update annotation tests after SaveOptions, Style, LinkAction, and Shape changes #2523's tests were written against earlier field lists. Later work added id_entropy, border and opacity style fields, a zoom on page links, and a line on custom stamps, so cargo xtask ci now fails at clippy on main. The tests keep the same intent: page links still use Fit Page, the view they had before they carried a zoom, and the extra style and stamp fields take their defaults. --- crates/annot/tests/malformed.rs | 24 ++++++++++++++++-------- crates/annot/tests/properties.rs | 8 ++++---- crates/annot/tests/round_trip.rs | 17 ++++++++++++----- crates/annot/tests/support/fixture.rs | 8 +++++++- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/crates/annot/tests/malformed.rs b/crates/annot/tests/malformed.rs index 64fc241baf..096a921298 100644 --- a/crates/annot/tests/malformed.rs +++ b/crates/annot/tests/malformed.rs @@ -382,7 +382,8 @@ fn a_link_area_that_is_too_small_or_broken_is_refused_before_it_is_written() { ]; for (label, rect, want_in_message) in cases { let mut doc = blank(2); - let e = links::add(&mut doc, 0, rect, &links::LinkAction::Page(1), &links::LinkStyle::default()).err(); + let e = + links::add(&mut doc, 0, rect, &links::LinkAction::Page { page: 1, zoom: links::LinkZoom::FitPage }, &links::LinkStyle::default()).err(); let msg = match &e { Some(AnnotError::Invalid(m)) => m.clone(), other => panic!("{label}: expected Invalid, got {other:?}"), @@ -440,10 +441,10 @@ fn non_finite_builder_input_is_refused_and_leaves_the_document_untouched() { } for (label, style) in [ - ("nan opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::NAN, width: 1.0, fill: None }), - ("infinite opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::INFINITY, width: 1.0, fill: None }), - ("nan colour", Style { color: [f64::NAN, 0.0, 0.0], opacity: 1.0, width: 1.0, fill: None }), - ("nan width", Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width: f64::NAN, fill: None }), + ("nan opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::NAN, width: 1.0, fill: None, ..Style::default() }), + ("infinite opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::INFINITY, width: 1.0, fill: None, ..Style::default() }), + ("nan colour", Style { color: [f64::NAN, 0.0, 0.0], opacity: 1.0, width: 1.0, fill: None, ..Style::default() }), + ("nan width", Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width: f64::NAN, fill: None, ..Style::default() }), ] { let mut doc = blank(1); let e = add_annotation( @@ -593,7 +594,14 @@ fn unvalidated_stamp_size_and_signature_contours_are_rewritten_silently_in_the_f &mut doc, &NewAnnotation { page: 0, - shape: Shape::CustomStamp { rect: [10.0, 20.0, 60.0, 50.0], name: "Sig".into(), picture, image: true, size: (1e300, f64::NAN) }, + shape: Shape::CustomStamp { + rect: [10.0, 20.0, 60.0, 50.0], + name: "Sig".into(), + picture, + image: true, + size: (1e300, f64::NAN), + line: None, + }, style: Style::default(), contents: String::new(), author: "A".into(), @@ -656,7 +664,7 @@ fn a_nan_fill_is_accepted_on_create_and_refused_on_edit() { &NewAnnotation { page: 0, shape: Shape::Rectangle { rect: [10.0, 20.0, 60.0, 40.0] }, - style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(nan) }, + style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(nan), ..Style::default() }, contents: String::new(), author: "A".into(), }, @@ -842,7 +850,7 @@ fn the_same_huge_fill_is_accepted_on_create_and_refused_on_edit() { &NewAnnotation { page: 0, shape: Shape::Rectangle { rect: [10.0, 20.0, 60.0, 40.0] }, - style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(probe) }, + style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(probe), ..Style::default() }, contents: String::new(), author: "A".into(), }, diff --git a/crates/annot/tests/properties.rs b/crates/annot/tests/properties.rs index 8efb6b7046..53a37cf067 100644 --- a/crates/annot/tests/properties.rs +++ b/crates/annot/tests/properties.rs @@ -165,7 +165,7 @@ proptest! { #[test] fn opacity_and_flags_follow_their_rules(opacity in any::()) { let mut doc = blank(1); - let added = add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: [10.0, 10.0, 60.0, 40.0] }, Style { color: [0.5, 0.25, 0.75], opacity, width: 1.0, fill: None }, String::new(), "Ada".into()), &meta("c".into())); + let added = add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: [10.0, 10.0, 60.0, 40.0] }, Style { color: [0.5, 0.25, 0.75], opacity, width: 1.0, fill: None, ..Style::default() }, String::new(), "Ada".into()), &meta("c".into())); if !opacity.is_finite() { prop_assert!(added.is_err(), "a non-finite opacity ({:?}) must be refused, not written", opacity); return Ok(()); @@ -243,7 +243,7 @@ proptest! { 2 => -0.5, _ => 0.0, }; - add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: *r }, Style { color: [0.9, 0.1, 0.1], opacity, width: 2.0, fill: None }, format!("c{i}"), "Ada".into()), &meta(format!("k{i}"))) + add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: *r }, Style { color: [0.9, 0.1, 0.1], opacity, width: 2.0, fill: None, ..Style::default() }, format!("c{i}"), "Ada".into()), &meta(format!("k{i}"))) .expect("adds"); want_rects.push(norm(*r)); } @@ -333,7 +333,7 @@ proptest! { #[test] fn width_and_fill_are_written_as_they_were_given(width in -4.0f64..12.0, fill in prop::option::of(prop::array::uniform3(0.0f64..1.0))) { let mut doc = blank(1); - add_annotation(&mut doc, &comment(0, Shape::Oval { rect: [5.0, 5.0, 65.0, 35.0] }, Style { color: [1.0, 0.0, 0.0], opacity: 1.0, width, fill }, String::new(), "Ada".into()), &meta("f".into())) + add_annotation(&mut doc, &comment(0, Shape::Oval { rect: [5.0, 5.0, 65.0, 35.0] }, Style { color: [1.0, 0.0, 0.0], opacity: 1.0, width, fill, ..Style::default() }, String::new(), "Ada".into()), &meta("f".into())) .expect("adds"); for path in [Save::Incremental, Save::FullTable] { let (file, reopened) = save_and_reopen(&doc, path); @@ -360,7 +360,7 @@ proptest! { fn a_line_keeps_its_endpoints_and_its_rectangle_spans_them(from in prop::array::uniform2(coord()), dx in 1.0f64..300.0, dy in -300.0f64..300.0, width in 0.5f64..8.0) { let to = [(from[0] + dx).min(450.0), from[1] + dy]; let mut doc = blank(1); - add_annotation(&mut doc, &comment(0, Shape::Line { from, to, start: LineEnding::None, end: LineEnding::OpenArrow }, Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width, fill: None }, String::new(), "Ada".into()), &meta("l".into())) + add_annotation(&mut doc, &comment(0, Shape::Line { from, to, start: LineEnding::None, end: LineEnding::OpenArrow }, Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width, fill: None, ..Style::default() }, String::new(), "Ada".into()), &meta("l".into())) .expect("adds"); let span = bounds_of(&[from, to]); for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { diff --git a/crates/annot/tests/round_trip.rs b/crates/annot/tests/round_trip.rs index 3e60601498..625cefc897 100644 --- a/crates/annot/tests/round_trip.rs +++ b/crates/annot/tests/round_trip.rs @@ -48,7 +48,7 @@ fn ann(page: usize, shape: Shape, contents: &str) -> NewAnnotation { } fn styled(page: usize, shape: Shape, contents: &str, color: [f64; 3], opacity: f64, width: f64, fill: Option<[f64; 3]>) -> NewAnnotation { - NewAnnotation { page, shape, style: Style { color, opacity, width, fill }, contents: contents.into(), author: "Ada".into() } + NewAnnotation { page, shape, style: Style { color, opacity, width, fill, ..Style::default() }, contents: contents.into(), author: "Ada".into() } } /// The dictionary of the `k`-th `/Annots` entry of a page. @@ -714,7 +714,7 @@ fn links_keep_page_targets_uri_and_dash_structure_across_renumbering() { &mut doc, 0, [120.0, 700.0, 240.0, 720.0], - &links::LinkAction::Page(2), + &links::LinkAction::Page { page: 2, zoom: links::LinkZoom::FitPage }, &links::LinkStyle { visible: true, width: 2.0, dashed: true, ..Default::default() }, ) .expect("links to page 3"); @@ -735,7 +735,7 @@ fn links_keep_page_targets_uri_and_dash_structure_across_renumbering() { .into_iter() .map(|l| { let a = match l.action { - links::LinkAction::Page(p) => format!("page {p}"), + links::LinkAction::Page { page: p, zoom: _ } => format!("page {p}"), links::LinkAction::Uri(u) => u, links::LinkAction::Other(o) => format!("other:{o}"), }; @@ -770,7 +770,14 @@ fn links_keep_page_targets_uri_and_dash_structure_across_renumbering() { fn removing_all_links_keeps_comments() { let mut doc = blank(2); add_annotation(&mut doc, &ann(0, Shape::Note { at: [50.0, 700.0], icon: NoteIcon::Help }, "note"), &meta("n")).expect("adds"); - links::add(&mut doc, 0, [200.0, 700.0, 300.0, 715.0], &links::LinkAction::Page(0), &links::LinkStyle::default()).expect("links"); + links::add( + &mut doc, + 0, + [200.0, 700.0, 300.0, 715.0], + &links::LinkAction::Page { page: 0, zoom: links::LinkZoom::FitPage }, + &links::LinkStyle::default(), + ) + .expect("links"); let removed = links::remove_all(&mut doc, None).expect("removes all links"); assert_eq!(removed, 1); @@ -980,7 +987,7 @@ fn a_full_rewrite_into_object_streams_describes_the_same_comments_as_a_plain_tab assert_eq!(summaries(&plain).len(), summaries(&packed).len()); // The plain-table output is bigger here: object streams compress, which is the point of them. - let opts = SaveOptions { mod_date: None, id_seed: 0x5052_494E_5443_5241, object_streams: true, drop_unlinked: false }; + let opts = SaveOptions { mod_date: None, id_seed: 0x5052_494E_5443_5241, object_streams: true, drop_unlinked: false, id_entropy: None }; let packed_bytes = pdfcraft_cos::write_full(&doc, &opts).expect("writes"); assert!( packed_bytes.len() < pdfcraft_cos::write_full(&doc, &SaveOptions { object_streams: false, ..opts }).expect("writes").len(), diff --git a/crates/annot/tests/support/fixture.rs b/crates/annot/tests/support/fixture.rs index 69567b34e9..1b466c4a54 100644 --- a/crates/annot/tests/support/fixture.rs +++ b/crates/annot/tests/support/fixture.rs @@ -118,7 +118,13 @@ pub fn save_and_reopen(doc: &Document, path: Save) -> (Vec, Document) { /// Save with the path's options. `/Info /ModDate` is left unset so a full save adds nothing that /// would make byte comparisons depend on the clock. pub fn save(doc: &Document, path: Save) -> Vec { - let opts = SaveOptions { mod_date: None, id_seed: 0x5052_494E_5443_5241, object_streams: path == Save::FullStreams, drop_unlinked: false }; + let opts = SaveOptions { + mod_date: None, + id_seed: 0x5052_494E_5443_5241, + object_streams: path == Save::FullStreams, + drop_unlinked: false, + id_entropy: None, + }; match path { Save::Incremental => write_incremental(doc, &opts), Save::FullTable | Save::FullStreams => write_full(doc, &opts), From 562c428c6ff89706bcbce96efce3f4c80d282b59 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:08:31 +0700 Subject: [PATCH 4/4] test(annot): retain current opacity and review-state semantics (#7721) * test(annot): retain current opacity and review-state semantics * style: wrap fixture imports at repository width --- crates/annot/tests/malformed.rs | 13 +++++++++---- crates/annot/tests/round_trip.rs | 9 +++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/annot/tests/malformed.rs b/crates/annot/tests/malformed.rs index 096a921298..6b8f007de7 100644 --- a/crates/annot/tests/malformed.rs +++ b/crates/annot/tests/malformed.rs @@ -294,15 +294,20 @@ fn reference_loops_terminate_and_a_delete_takes_the_cycle_with_it() { // ── wrong types where the reader wants a number or a name ───────────────────────────────────── /// Values of the wrong type in flag, opacity and colour entries: the reader falls back to its -/// defaults instead of panicking. NOTE on opacity: reading does **not** clamp, so `/CA 7.5` comes -/// back as 7.5 and would hand a nonsense value to a 0–1 slider; writing clamps (`add_annotation` -/// refuses non-finite and clamps the range). The asymmetry is recorded here, not endorsed. +/// defaults instead of panicking. Opacity is clamped on read to the supported 0-1 range; +/// the original malformed dictionary remains unchanged. #[test] fn wrong_typed_flag_opacity_and_colour_entries_fall_back_without_panicking() { let doc = one_annot(b"<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /CA 7.5 /F /NotAnInt /C [1 2] >>"); read_everything(&doc); let p = props(&doc, 0, 0).expect("still a comment"); - assert_eq!(p.opacity, 7.5, "the out-of-range opacity is passed through as written"); + assert_eq!(p.opacity, 1.0, "the out-of-range opacity is clamped on read"); + // Exercise both clamp edges and an unchanged interior value, not just a constant fallback. + for (raw, expected) in [("-0.5", 0.0), ("0.4", 0.4), ("1.5", 1.0)] { + let bytes = format!("<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /CA {raw} >>"); + let probe = one_annot(bytes.as_bytes()); + assert_eq!(props(&probe, 0, 0).expect("still a comment").opacity, expected, "opacity {raw}"); + } assert!(!p.locked, "a name where /F belongs reads as no flags at all"); assert_eq!(p.color, None, "/C with two components is not a colour"); let listed = summaries(&doc); diff --git a/crates/annot/tests/round_trip.rs b/crates/annot/tests/round_trip.rs index 625cefc897..dde7f8ebc1 100644 --- a/crates/annot/tests/round_trip.rs +++ b/crates/annot/tests/round_trip.rs @@ -33,8 +33,8 @@ use pdfcraft_cos::{Dict, Document, ObjRef, Object, SaveOptions}; use support::bytes; use support::fixture::{ - ALL_SAVES, FLAG_NO_ROTATE, FLAG_NO_ZOOM, FLAG_PRINT, FLAG_READONLY, Save, annot_entries, blank, blank_with_indirect_annots, dict_of, name_of, - num, nums, page_object_numbers, pdf, save_and_reopen, str_of, + ALL_SAVES, FLAG_HIDDEN, FLAG_NO_ROTATE, FLAG_NO_ZOOM, FLAG_PRINT, FLAG_READONLY, Save, annot_entries, blank, blank_with_indirect_annots, dict_of, + name_of, num, nums, page_object_numbers, pdf, save_and_reopen, str_of, }; /// A deterministic `/M` date and `/NM` id, so nothing here depends on the clock. @@ -658,8 +658,9 @@ fn reply_threads_and_the_delete_cascade_survive_every_save_path() { assert_eq!(str_of(&reopened, &st, b"State").as_deref(), Some("Accepted"), "{path:?}"); assert_eq!(str_of(&reopened, &st, b"StateModel").as_deref(), Some("Review"), "{path:?}"); assert_eq!(str_of(&reopened, &st, b"Subj").as_deref(), Some("Status"), "{path:?}"); - // Replies are printed but keep the icon flags of a comment; no Hidden bit here. - assert_eq!(st.int(b"F"), Some(FLAG_PRINT | FLAG_NO_ZOOM | FLAG_NO_ROTATE), "{path:?}"); + // Review-state records are hidden; ordinary text replies retain their visible comment flags. + assert_eq!(st.int(b"F"), Some(FLAG_HIDDEN | FLAG_PRINT | FLAG_NO_ZOOM | FLAG_NO_ROTATE), "{path:?}"); + assert_eq!(annot_at(&reopened, 0, 2, path).int(b"F"), Some(FLAG_PRINT | FLAG_NO_ZOOM | FLAG_NO_ROTATE), "{path:?}: ordinary reply"); let listed = summaries(&reopened); assert_eq!(listed.iter().filter(|s| s.in_reply_to.as_deref() == Some("p1")).count(), 2, "{path:?}: both replies name the parent's /NM"); assert_eq!(listed.iter().filter(|s| s.state.as_deref() == Some("Accepted")).count(), 1, "{path:?}");