diff --git a/crates/odf/src/text/body.rs b/crates/odf/src/text/body.rs index f07595676..ec29259df 100644 --- a/crates/odf/src/text/body.rs +++ b/crates/odf/src/text/body.rs @@ -106,6 +106,10 @@ pub(super) struct Body<'a> { depth: usize, /// The markup being read (`content.xml`), for keeping paragraphs' own (empty: none kept). pub(super) raw: &'a [u8], + /// The markup being read, body's or a header's, and where the start tag being read ends in it: + /// for looking into a picture before reading it (empty: not known). + pub(super) markup: &'a [u8], + pub(super) tag_end: usize, /// The paragraph whose markup is being kept (see [`KeptOpen`]). kept_open: Option, /// The body table whose markup is being kept (its `done`: the container's length before it). @@ -257,6 +261,8 @@ impl<'a> Body<'a> { skip: 0, depth: 0, raw: &[], + markup: &[], + tag_end: 0, kept_open: None, kept_table: None, kept_index: None, @@ -455,6 +461,7 @@ impl<'a> Body<'a> { /// whole and refers to nothing outside itself keeps its markup (`Para::kept`), so a save /// that doesn't change it writes it as it was (see `craft_wordmodel::kept`). pub(super) fn feed_at(&mut self, ev: Ev, at: (usize, usize)) { + self.tag_end = at.1; self.hide_read(); self.hidden_section(&ev); // A body index, kept whole: its settings and templates, which WordCraft writes its own. @@ -1467,10 +1474,33 @@ impl Body<'_> { } } + /// Whether the `draw:image` just started holds any text, read from its markup as the rest of + /// the file is read (`xml::holds_words`); `true` when the markup isn't known, so nothing in + /// it is skipped unread. + fn picture_holds_words(&self) -> bool { + let Some(before) = self.markup.get(..self.tag_end) else { return true }; + if before.is_empty() { + return true; + } + if before.ends_with(b"/>") { + return false; + } + super::xml::holds_words(self.markup, self.tag_end) + } + /// Part of a frame: its picture, embedded object, description or contour. fn frame_part(&mut self, q: &str, a: &Attrs) { match local(q) { "image" => { + // What a picture holds (§10.4.4 lets it hold text; LibreOffice writes an empty + // paragraph in one it draws as a shape, as it does every picture in a group) isn't + // the text's: read as a paragraph, it ended the one around the frame. An empty one + // isn't read. Words in one still are, as a paragraph of their own (which still ends + // the one around it): skipped, they'd be lost on the paragraph's first edit or a + // save to another format. + if q == "draw:image" && !self.picture_holds_words() { + self.skip = 1; + } // A chart's replacement picture (what readers that can't draw it show): kept with // the chart as read, written back with it while it is unchanged. if let Some(href) = get(a, "xlink:href") diff --git a/crates/odf/src/text/import.rs b/crates/odf/src/text/import.rs index fcd98b164..b9ffe9ba3 100644 --- a/crates/odf/src/text/import.rs +++ b/crates/odf/src/text/import.rs @@ -933,6 +933,7 @@ fn parse_tracked(bytes: &[u8], paper: (f32, f32)) -> Result { let mut body = Body::new(&styles, &pages, &mut fetch); (body.entries, body.manifest) = (objects.0, Some(objects.1)); body.raw = &content; + body.markup = &content; body.spaces_left = spaces_left; super::xml::events_at(&content, |ev, at| body.feed_at(ev, at)); let mut blocks = body.take_blocks(); diff --git a/crates/odf/src/text/page_parts.rs b/crates/odf/src/text/page_parts.rs index 6a67da3af..a484ae44d 100644 --- a/crates/odf/src/text/page_parts.rs +++ b/crates/odf/src/text/page_parts.rs @@ -4,7 +4,8 @@ use std::collections::HashSet; use craft_wordmodel::{Document, PartKind}; -use super::{Body, Ev, FBlock, Pages, SectionProps, Styles, events, get, model}; +use super::xml::events_at; +use super::{Body, Ev, FBlock, Pages, SectionProps, Styles, get, model}; /// Most XML events kept for all headers and footers together; a slot past it is left out. const MAX_EVENTS: usize = 1 << 20; @@ -79,13 +80,13 @@ pub(super) fn read( objects: (&[String], &std::collections::HashMap), spaces_left: &mut usize, ) -> Parts { - let mut found: Vec<(String, Slot, Vec)> = Vec::new(); + let mut found: Vec<(String, Slot, Vec<(Ev, usize)>)> = Vec::new(); let mut master: Option = None; // The slot being read, its nesting depth and whether it is kept. let mut slot: Option<(usize, bool)> = None; let mut seen = HashSet::new(); let mut room = MAX_EVENTS; - events(xml, |ev| { + events_at(xml, |ev, at| { if let Some((depth, keep)) = &mut slot { match &ev { Ev::End(_) if *depth == 0 => { @@ -102,7 +103,7 @@ pub(super) fn read( } if *keep && let Some((.., evs)) = found.last_mut() { room -= 1; - evs.push(ev); + evs.push((ev, at.1)); } return; } @@ -130,7 +131,9 @@ pub(super) fn read( reader.in_text = true; (reader.entries, reader.manifest) = (objects.0, Some(objects.1)); reader.spaces_left = *spaces_left; - for ev in evs { + reader.markup = xml; + for (ev, end) in evs { + reader.tag_end = end; reader.feed(ev); } *spaces_left = reader.spaces_left; diff --git a/crates/odf/src/text/tests_more/kept.rs b/crates/odf/src/text/tests_more/kept.rs index 88d271142..2efbad997 100644 --- a/crates/odf/src/text/tests_more/kept.rs +++ b/crates/odf/src/text/tests_more/kept.rs @@ -159,6 +159,170 @@ fn a_file_a_kept_definition_names_is_written_with_it() { assert!(m.contains(r#"manifest:full-path="Pictures/paper.png""#), "{m}"); } +/// A picture as LibreOffice writes one it draws as a shape: a frame with a text style, and in its +/// `draw:image` an empty paragraph (ODF 1.3 §10.4.4 lets a picture hold text). +const SHAPE_PICTURE: &str = r#""#; +const AFTER_PICTURE: &str = r#"Text after it."#; + +/// A package whose text is `body`, with the styles and the picture [`SHAPE_PICTURE`] names. +fn shape_picture_doc(body: &str) -> (Vec, Vec) { + let png = [b"\x89PNG\r\n\x1a\n".as_slice(), &[5u8; 64]].concat(); + let content = format!( + r#"{body}"# + ); + (crate::text::tests::zip_of(&[("content.xml", content.as_bytes()), ("Pictures/x.png", &png)]), png) +} + +#[test] +fn a_picture_holding_an_empty_paragraph_stays_in_its_own() { + let first = format!(r#"{SHAPE_PICTURE}"#); + let (pkg, png) = shape_picture_doc(&format!("{first}{AFTER_PICTURE}")); + let d = import(&pkg).unwrap(); + // Two paragraphs, as written: the picture in the first, the text in the second. The picture's + // own paragraph used to end the one around it, and the picture joined the next one. + assert_eq!(d.body.len(), 2); + let (a, b) = (d.body[0].as_para().unwrap(), d.body[1].as_para().unwrap()); + assert_eq!(a.objects.len(), 1, "the picture is in the first paragraph"); + assert!(b.objects.is_empty()); + assert_eq!(b.text, "Text after it."); + + let saved = export(&d).unwrap(); + let c = content_of(&saved); + assert!(c.contains(&format!("{first}\n{AFTER_PICTURE}")), "as read: {c}"); + for def in [r#"style:name="P1""#, r#"style:name="P2""#, r#"style:name="gr1""#] { + assert!(c.contains(def), "{def}: {c}"); + } + let mut z = zip::ZipArchive::new(std::io::Cursor::new(saved.clone())).unwrap(); + let mut got = Vec::new(); + std::io::Read::read_to_end(&mut z.by_name("Pictures/x.png").unwrap(), &mut got).unwrap(); + assert_eq!(got, png); + // Saved again, still as read. + assert!(content_of(&export(&import(&saved).unwrap()).unwrap()).contains(&format!("{first}\n{AFTER_PICTURE}"))); +} + +#[test] +fn a_grouped_picture_holding_an_empty_paragraph_stays_in_its_own() { + // LibreOffice writes every picture in a group this way. + let member = SHAPE_PICTURE.replace(r#" text:anchor-type="paragraph" draw:z-index="0""#, ""); + let first = format!( + r#"{member}"# + ); + let (pkg, _) = shape_picture_doc(&format!("{first}{AFTER_PICTURE}")); + let d = import(&pkg).unwrap(); + assert_eq!(d.body.len(), 2); + assert_eq!(d.body[0].as_para().unwrap().objects.len(), 1, "the group is in the first paragraph"); + assert_eq!(d.body[1].as_para().unwrap().text, "Text after it."); + let c = content_of(&export(&d).unwrap()); + assert!(c.contains(&format!("{first}\n{AFTER_PICTURE}")), "{c}"); +} + +#[test] +fn a_picture_holding_a_paragraph_in_a_cell_or_a_text_box_stays_in_its_own() { + let inner = format!(r#"Before {SHAPE_PICTURE} after."#); + let cell = + format!(r#"{inner}"#); + let boxed = format!( + r#"{inner}"# + ); + let (pkg, _) = shape_picture_doc(&format!("{cell}{boxed}")); + let d = import(&pkg).unwrap(); + let craft_wordmodel::Block::Table(t) = &*d.body[0] else { panic!("a table: {:?}", d.body[0]) }; + let in_cell = &t.rows[0].cells[0].blocks; + assert_eq!(in_cell.len(), 1); + assert_eq!(in_cell[0].as_para().unwrap().text, "Before \u{FFFC} after."); + let saved = content_of(&export(&d).unwrap()); + assert_eq!(saved.matches(inner.as_str()).count(), 2, "as read: {saved}"); +} + +/// All the text of `blocks`, paragraph by paragraph. +fn texts(blocks: &[std::sync::Arc]) -> Vec { + blocks.iter().filter_map(|b| b.as_para()).map(|p| p.text.clone()).collect() +} + +#[test] +fn words_in_a_picture_are_not_lost() { + // LibreOffice draws them on the picture. Skipped like an empty paragraph, they were only in the + // paragraph's markup as read: gone once the paragraph was edited, or saved to another format. + let held = SHAPE_PICTURE.replace("", r#"Words on it"#); + let first = format!(r#"{held}"#); + let (pkg, _) = shape_picture_doc(&format!("{first}{AFTER_PICTURE}")); + let mut d = import(&pkg).unwrap(); + assert!(texts(&d.body).iter().any(|t| t.contains("Words on it")), "{:?}", texts(&d.body)); + assert!(d.plain_text(craft_wordmodel::StoryRef::Body).contains("Words on it")); + d.kept.clear(); + assert!(content_of(&export(&d).unwrap()).contains("Words on it")); +} + +#[test] +fn a_picture_in_a_header_holding_an_empty_paragraph_stays_in_its_own_and_words_stay() { + let header = |held: &str| { + let styles = format!( + r#"Head {held} line"# + ); + let png = [b"\x89PNG\r\n\x1a\n".as_slice(), &[5u8; 64]].concat(); + let content = format!( + r#"Body"# + ); + let pkg = crate::text::tests::zip_of(&[("content.xml", content.as_bytes()), ("styles.xml", styles.as_bytes()), ("Pictures/x.png", &png)]); + let d = import(&pkg).unwrap(); + let part = d.parts.values().find(|p| p.kind == craft_wordmodel::PartKind::Header).expect("a header"); + texts(&part.blocks) + }; + // An empty paragraph in the picture doesn't end the header's. + assert_eq!(header(SHAPE_PICTURE), vec!["Head \u{FFFC} line".to_string()]); + // Words in one are read. + let held = SHAPE_PICTURE.replace("", "Words on it"); + assert!(header(&held).iter().any(|t| t.contains("Words on it")), "{:?}", header(&held)); +} + +/// The paragraphs of a document whose first paragraph holds [`SHAPE_PICTURE`] with `inside` in +/// place of its empty paragraph, followed by [`AFTER_PICTURE`]. +fn with_in_picture(inside: &str) -> craft_wordmodel::Document { + let held = SHAPE_PICTURE.replace("", inside); + let (pkg, _) = shape_picture_doc(&format!(r#"A {held} B{AFTER_PICTURE}"#)); + import(&pkg).unwrap() +} + +#[test] +fn words_in_a_picture_written_as_cdata_or_after_a_comment_are_not_lost() { + // Read as the parser reads them, not as the bytes look: CDATA is text, and a comment naming + // the picture's end doesn't end it. + for inside in ["", "Words on it"] { + let mut d = with_in_picture(inside); + assert!(texts(&d.body).iter().any(|t| t.contains("Words on it")), "{inside}: {:?}", texts(&d.body)); + assert!(d.plain_text(craft_wordmodel::StoryRef::Body).contains("Words on it"), "{inside}"); + d.kept.clear(); + assert!(content_of(&export(&d).unwrap()).contains("Words on it"), "{inside}"); + } +} + +#[test] +fn an_empty_picture_with_a_greater_than_sign_in_its_markup_stays_in_its_own() { + // A `>` in an attribute or a comment isn't a word. + for inside in [r#""#, "", ""] { + let d = with_in_picture(inside); + assert_eq!(texts(&d.body), ["A \u{FFFC} B", "Text after it."], "{inside}"); + } +} + +#[test] +fn many_pictures_without_their_ends_are_read_in_linear_time() { + // Each picture's contents are looked at only so far: a picture whose end is far off, or never + // comes, isn't looked through to the end of the file once for every picture. + let run = |n: usize| { + let stray = "".repeat(n); + let open = "".repeat(n); + let (pkg, _) = shape_picture_doc(&format!( + r#"{stray}{open}"# + )); + let t = std::time::Instant::now(); + assert!(import(&pkg).is_ok()); + t.elapsed() + }; + let took = run(20_000); + assert!(took.as_secs_f32() < 3.0, "{took:?}"); +} + #[test] fn the_files_font_declarations_are_kept() { // A reader without a font picks its substitute by how the file declares it. WordCraft's own diff --git a/crates/odf/src/text/xml.rs b/crates/odf/src/text/xml.rs index d96ee4917..7a448eb12 100644 --- a/crates/odf/src/text/xml.rs +++ b/crates/odf/src/text/xml.rs @@ -35,6 +35,16 @@ pub(super) enum Ev { Text(String), } +/// No limit but the input's own size (each part is at most a package entry). +const LIMITS: craft_xml::Limits = craft_xml::Limits { + max_input: usize::MAX, + max_depth: usize::MAX, + max_nodes: usize::MAX, + max_elements: usize::MAX, + max_attrs: usize::MAX, + max_attrs_total: usize::MAX, +}; + /// Stream XML events (empty elements become start + end). Malformed XML ends the stream. pub(super) fn events(xml: &[u8], mut f: impl FnMut(Ev)) { events_at(xml, |ev, _| f(ev)); @@ -43,17 +53,8 @@ pub(super) fn events(xml: &[u8], mut f: impl FnMut(Ev)) { /// [`events`], each with the byte range of `xml` its markup takes (an empty element's start /// and end both take the whole element). pub(super) fn events_at(xml: &[u8], mut f: impl FnMut(Ev, (usize, usize))) { - // Names and attributes as written; no limit but the input's own size (each part is at most a - // package entry), and at most 128 attributes kept per element. - let limits = craft_xml::Limits { - max_input: usize::MAX, - max_depth: usize::MAX, - max_nodes: usize::MAX, - max_elements: usize::MAX, - max_attrs: usize::MAX, - max_attrs_total: usize::MAX, - }; - let opts = craft_xml::Options::default().with_limits(limits); + // Names and attributes as written, and at most 128 attributes kept per element. + let opts = craft_xml::Options::default().with_limits(LIMITS); let mut events = opts.events(xml); // Malformed XML ends the stream. while let Ok(Some(ev)) = events.next() { @@ -70,3 +71,55 @@ pub(super) fn events_at(xml: &[u8], mut f: impl FnMut(Ev, (usize, usize))) { } } } + +/// Most events, and bytes, [`holds_words`] looks at: an element still open past them counts as +/// holding words (an empty one LibreOffice writes is a handful of events). +const LOOK_EVENTS: usize = 64; +const LOOK_BYTES: usize = 4096; + +/// Whether the element whose start tag ends at `from` in `xml` holds any words before its end, +/// read as [`events`] reads them: text, CDATA or a reference that isn't only whitespace (comments +/// and processing instructions hold none). Any end at its own depth ends it, as in [`events`]. +/// `true` when that can't be told within [`LOOK_EVENTS`] events and [`LOOK_BYTES`] bytes, or +/// the markup is malformed, so nothing in it is taken for empty unread. +pub(super) fn holds_words(xml: &[u8], from: usize) -> bool { + let Some(rest) = xml.get(from..from.saturating_add(LOOK_BYTES).min(xml.len())) else { return true }; + // The reader skips an end tag with nothing open, so the contents are read inside a stand-in + // start tag, which the element's own end (any end at its depth) closes. + const OPEN: &[u8] = b"<_>"; + let mut wrapped = Vec::with_capacity(OPEN.len().saturating_add(rest.len())); + wrapped.extend_from_slice(OPEN); + wrapped.extend_from_slice(rest); + // An unknown entity is an error rather than left out, so that it counts as words. + let opts = craft_xml::Options { unknown_entity: craft_xml::UnknownEntity::Error, ..craft_xml::Options::default().with_limits(LIMITS) }; + let mut r = opts.events(&wrapped); + if !matches!(r.next(), Ok(Some(craft_xml::Event::Start { .. }))) { + return true; + } + let mut depth = 0usize; + let mut looked = 0usize; + // An empty element's start and end are one piece of markup (one span): counted once. + let mut last = 0..0; + while looked < LOOK_EVENTS { + let ev = match r.next() { + Ok(Some(ev)) => ev, + Ok(None) | Err(_) => return true, + }; + let span = r.span(); + if span != last { + looked = looked.saturating_add(1); + } + let markup = wrapped.get(span.clone()).unwrap_or_default(); + last = span; + match ev { + craft_xml::Event::Start { .. } => depth = depth.saturating_add(1), + craft_xml::Event::End { .. } if depth == 0 => return false, + craft_xml::Event::End { .. } => depth = depth.saturating_sub(1), + // A reference, resolved: words whatever it stands for. + craft_xml::Event::Text(_) if markup.starts_with(b"&") => return true, + craft_xml::Event::Text(t) | craft_xml::Event::CData(t) if t.bytes().any(|b| !b.is_ascii_whitespace()) => return true, + _ => {} + } + } + true +}