mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-18 18:15:34 +00:00
Merge pull request #7659 from storytold/fix/xfa-count-cache-20261117
XFA: cache repeated prototype child counts without relaxing work limits
This commit is contained in:
6 files changed
+133
-27
No files matched your search
@@ -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);
|
||||
@@ -382,7 +387,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 +446,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 +599,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 +669,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 +855,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(),
|
||||
},
|
||||
|
||||
@@ -165,7 +165,7 @@ proptest! {
|
||||
#[test]
|
||||
fn opacity_and_flags_follow_their_rules(opacity in any::<f64>()) {
|
||||
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] {
|
||||
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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:?}");
|
||||
@@ -714,7 +715,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 +736,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 +771,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 +988,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(),
|
||||
|
||||
@@ -118,7 +118,13 @@ pub fn save_and_reopen(doc: &Document, path: Save) -> (Vec<u8>, 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<u8> {
|
||||
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),
|
||||
|
||||
+20
-1
@@ -74,6 +74,7 @@ pub(crate) fn expand(template: Node) -> (Option<String>, Vec<String>) {
|
||||
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<NodeId, usize>,
|
||||
/// The elements being written around the current one: inheriting from one is a loop.
|
||||
active: HashMap<NodeId, u32>,
|
||||
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<NodeId, usize>) -> 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) {
|
||||
|
||||
@@ -399,3 +399,63 @@ 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("<root><a/><b/><c/></root>").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!("<root>{}</root>", "<a/>".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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cached_layers_still_subtract_work_and_stop_at_the_budget() {
|
||||
let doc = crate::xml::Document::parse("<root><a/><b/><c/></root>").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")));
|
||||
}
|
||||
Reference in new issue
Block a user