From 89fc88074e6eecb1b735247ad1b481dd97584ce5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 10 Nov 2026 22:25:22 +0000 Subject: [PATCH] address review: share the ODT spaces budget between content and header/footer parts; warn once when it is spent --- crates/formats/src/odt/body.rs | 8 ++++++-- crates/formats/src/odt/import.rs | 4 +++- crates/formats/src/odt/page_parts.rs | 5 +++++ crates/formats/src/odt/tests.rs | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/crates/formats/src/odt/body.rs b/crates/formats/src/odt/body.rs index 55f448fc84..fd57a63fd3 100644 --- a/crates/formats/src/odt/body.rs +++ b/crates/formats/src/odt/body.rs @@ -158,7 +158,9 @@ pub(super) struct Body<'a> { /// Insertions open in the body: (region id, revision). active_ins: Vec<(String, RevMark)>, /// Spaces `text:s` may still add ([`MAX_EXPANDED_SPACES`]). - spaces_left: usize, + pub(super) spaces_left: usize, + /// The spaces-budget warning was logged. + spaces_warned: bool, } /// Most spaces the document's `text:s` elements add in all: each may ask for 1,000 in 20 @@ -203,6 +205,7 @@ impl<'a> Body<'a> { del: None, active_ins: Vec::new(), spaces_left: MAX_EXPANDED_SPACES, + spaces_warned: false, } } @@ -832,7 +835,8 @@ impl Body<'_> { "s" => { let asked = get(a, "text:c").and_then(|v| v.trim().parse::().ok()).unwrap_or(1).clamp(1, 1000); let count = asked.min(self.spaces_left).max(1); - if count < asked && self.spaces_left > 0 { + if asked > self.spaces_left && !self.spaces_warned { + self.spaces_warned = true; log::warn!("odt: the document's spaces pass {MAX_EXPANDED_SPACES}; each further text:s adds one"); } self.spaces_left = self.spaces_left.saturating_sub(count); diff --git a/crates/formats/src/odt/import.rs b/crates/formats/src/odt/import.rs index 39742d3e83..bfa28620d4 100644 --- a/crates/formats/src/odt/import.rs +++ b/crates/formats/src/odt/import.rs @@ -1,4 +1,4 @@ -use super::body::Body; +use super::body::{Body, MAX_EXPANDED_SPACES}; use super::limits::{MAX_ENTRY, MAX_SECTIONS}; use super::styles::{Styles, column_sep, columns_of}; use super::xml::{Attrs, Ev, events, get, length, local}; @@ -140,6 +140,8 @@ fn parse_tracked(bytes: &[u8]) -> Result<(Flow, SectionProps, Option, Opti }; let parts = styles_xml.as_deref().map(|x| page_parts::read(x, &styles, &mut pages, &mut fetch)).unwrap_or_default(); let mut body = Body::new(&styles, &pages, &mut fetch); + // The headers and footers' spaces count against the same budget as the content's. + body.spaces_left = MAX_EXPANDED_SPACES.saturating_sub(parts.spaces_used); events(&content, |ev| body.feed(ev)); let mut blocks = body.take_blocks(); // An open section with nothing in it (after a column section at the end) isn't one: the diff --git a/crates/formats/src/odt/page_parts.rs b/crates/formats/src/odt/page_parts.rs index ce7ed90477..51d89ae41c 100644 --- a/crates/formats/src/odt/page_parts.rs +++ b/crates/formats/src/odt/page_parts.rs @@ -4,6 +4,7 @@ use std::collections::HashSet; use wordcraft_doc::{Document, Part, PartKind}; +use super::body::MAX_EXPANDED_SPACES; use super::{Body, Ev, FBlock, Pages, SectionProps, Styles, events, get, model}; /// Most XML events kept for all headers and footers together; a slot past it is left out. @@ -54,6 +55,8 @@ impl Slot { pub(super) struct Parts { parts: Vec<(PartKind, Vec)>, even_odd: bool, + /// Spaces the parts' `text:s` elements added, charged to the document's budget. + pub(super) spaces_used: usize, } /// Read each visible header and footer of the master pages in `pages`, and name it on its master @@ -109,9 +112,11 @@ pub(super) fn read(xml: &[u8], styles: &Styles, pages: &mut Pages, fetch: &mut d for (name, kind, evs) in found { let mut reader = Body::new(styles, &none, fetch); reader.in_text = true; + reader.spaces_left = MAX_EXPANDED_SPACES.saturating_sub(out.spaces_used); for ev in evs { reader.feed(ev); } + out.spaces_used = MAX_EXPANDED_SPACES.saturating_sub(reader.spaces_left); out.parts.push((if kind.footer() { PartKind::Footer } else { PartKind::Header }, reader.take_blocks())); if let Some(sec) = pages.masters.get_mut(&name) { kind.set(sec, out.parts.len() as u32); diff --git a/crates/formats/src/odt/tests.rs b/crates/formats/src/odt/tests.rs index 59169bc70d..ce2b3464bc 100644 --- a/crates/formats/src/odt/tests.rs +++ b/crates/formats/src/odt/tests.rs @@ -1156,3 +1156,17 @@ fn repeated_spaces_stop_growing_past_the_document_budget() { ); assert!(import(&package(&content, None)).unwrap().plain_text(wordcraft_doc::StoryRef::Body).contains("a b")); } + +#[test] +fn header_spaces_count_against_the_content_budget() { + // A header that uses up the budget leaves the content one space per `text:s`. + let spaces = r#""#.repeat(20_000); + let styles = format!( + r#"h{spaces}"# + ); + let content = format!( + r#"ab"# + ); + let doc = import(&package(&content, Some(&styles))).unwrap(); + assert!(doc.plain_text(wordcraft_doc::StoryRef::Body).contains("a b")); +}