A fast, helpful, and open-source document parser
wip
Logan Markewich2edea8cbc910
diff --git a/crates/liteparse-ooxml/src/xlsx/drawings.rs b/crates/liteparse-ooxml/src/xlsx/drawings.rsindex d35b432..4a7d631 100644--- a/crates/liteparse-ooxml/src/xlsx/drawings.rs+++ b/crates/liteparse-ooxml/src/xlsx/drawings.rs@@ -1,14 +1,30 @@-//! SpreadsheetML DrawingML: the pictures floating over a sheet's grid.+//! SpreadsheetML DrawingML: the pictures and text shapes floating over a+//! sheet's grid.//!//! A worksheet references at most one drawing part (`<drawing r:id>`); the//! part holds a list of *anchors* (ECMA-376 §20.5), each placing one object —//! a picture, a shape, a chart frame, or a group — against the grid. This-//! module reads **pictures only**: the corpus census (1,248 workbooks) found-//! 2,276 `xdr:pic` against 386 chart frames and 28,745 shapes, and a chart-//! has no image bytes to extract while a shape's text is a separate content-//! gap, recorded in the plan doc rather than half-solved here.+//! module reads **pictures** and **text-bearing shapes**: the corpus census+//! (1,248 workbooks) found 2,276 `xdr:pic`, 386 chart frames, and — among+//! ~29k `xdr:sp` — 1,637 visible shapes carrying real text (137k chars of+//! titles, form labels, and instructions invisible to the grid). Charts have+//! no image bytes to extract and stay out of scope.//!-//! Census-driven decisions:+//! Census-driven decisions (`xlsx_shapetext_census`, see plan doc):+//!+//! * **`hidden="1"` shapes are skipped.** 1,004 of the corpus's 2,641+//! text-bearing shapes are legacy form controls (`name="Check Box N"`,+//! zero-extent, stacked option labels like `YES`/`NO`/`UNKNOWN`) that+//! Excel itself never renders.+//! * **`mc:Fallback` subtrees are skipped** — the PPTX shape walk's+//! prefer-Choice rule, third format it applies to. Neither branch is a+//! superset (Choice-only reads +119 shapes, −0.8% chars vs Fallback-only),+//! and reading *both* — what this parser did before it knew about MCE —+//! double-places every picture that appears in each branch.+//! * **`cxnSp` text is not read**: zero corpus occurrences. Likewise+//! `a:hlinkClick`/`a:fld` resolution (7 and 4 shapes corpus-wide).+//!+//! Census-driven decisions for pictures://!//! * **All three anchor kinds are read.** `oneCellAnchor` is the majority//! (19,228 against 9,157 `twoCellAnchor`), so treating the two-cell form as@@ -94,14 +110,108 @@ pub(crate) struct RawPic {pub(crate) rel_id: String,}-/// Parse one `xl/drawings/drawingN.xml` part into its placed pictures.+/// One visible text-bearing shape placed on a sheet. Unlike a picture there+/// is nothing to resolve — the text is complete in the drawing part — so+/// this is the final form, not a `Raw*` intermediate.+#[derive(Clone, Debug)]+pub struct SheetShape {+ pub anchor: PicAnchor,+ /// `xdr:cNvPr@name`, when the producer wrote one.+ pub name: Option<String>,+ /// The shape's `xdr:txBody`, lowered through the shared DrawingML text+ /// model — the same `CT_TextBody` a PPTX shape carries.+ pub body: crate::pptx::text::TextBody,+}++/// Everything one drawing part yields.+#[derive(Default)]+pub(crate) struct DrawingContent {+ pub(crate) pics: Vec<RawPic>,+ pub(crate) shapes: Vec<SheetShape>,+}++/// An `xdr:sp` currently open in the walk, before its anchor is known.+#[derive(Default)]+struct OpenShape {+ name: Option<String>,+ hidden: bool,+ body: Option<crate::pptx::text::TextBody>,+ seen_cnvpr: bool,+}++/// Flat text rescue for OMML equation bodies.+///+/// 56 corpus shapes (6 workbooks, engineering calculators) hold their text+/// in `a14:m` math — `m:oMath` trees whose glyphs sit in `m:t` runs the+/// structured text model does not know, so the body lowers to empty. This+/// collects every `t`-element's text in document order into one run per+/// `a:p` (`m:t` and `a:t` both strip to `t` under `local_name`, which is+/// exactly the reading the shape-text census counted). Math *structure* —+/// fractions, sub/superscripts — flattens to its glyph sequence; recorded,+/// not hidden. Returns `None` when no text is found at all.+fn flat_math_body(txbody: &[u8]) -> Option<crate::pptx::text::TextBody> {+ use crate::model::{Inline, RunElement, TextRun};+ use crate::pptx::text::{TextBody, TextParagraph};++ let mut reader = Reader::from_reader(txbody);+ let mut buf = Vec::new();+ let mut paras: Vec<String> = Vec::new();+ let mut in_t = false;+ loop {+ match reader.read_event_into(&mut buf) {+ Err(_) => return None,+ Ok(Event::Eof) => break,+ Ok(Event::Start(ref e)) => match local_name(e.name().as_ref()) {+ b"p" => paras.push(String::new()),+ b"t" => in_t = true,+ _ => {}+ },+ Ok(Event::Text(ref t)) if in_t => {+ if let (Some(last), Ok(txt)) = (paras.last_mut(), t.decode()) {+ last.push_str(&txt);+ }+ }+ Ok(Event::End(ref e)) => {+ if local_name(e.name().as_ref()) == b"t" {+ in_t = false;+ }+ }+ Ok(_) => {}+ }+ buf.clear();+ }+ if paras.iter().all(|p| p.trim().is_empty()) {+ return None;+ }+ Some(TextBody {+ body_pr: None,+ list_style: Default::default(),+ paragraphs: paras+ .into_iter()+ .map(|text| TextParagraph {+ properties: Default::default(),+ content: vec![Inline::TextRun(Box::new(TextRun {+ style_id: None,+ properties: Default::default(),+ content: vec![RunElement::Text(text)],+ rsids: Default::default(),+ }))],+ end_run_properties: None,+ })+ .collect(),+ })+}++/// Parse one `xl/drawings/drawingN.xml` part into its placed pictures and+/// text shapes.////// Fail-open like the rest of the reader: a malformed drawing part costs its-/// pictures, never the workbook.-pub(crate) fn parse_drawing(data: &[u8]) -> Result<Vec<RawPic>> {+/// pictures and shapes, never the workbook.+pub(crate) fn parse_drawing(data: &[u8]) -> Result<DrawingContent> {let mut reader = Reader::from_reader(data);let mut buf = Vec::new();- let mut out = Vec::new();+ let mut skip_buf = Vec::new();+ let mut out = DrawingContent::default();// State for the anchor currently open. `object_depth` counts how far// inside a placed object (pic/sp/grpSp/graphicFrame/cxnSp) the cursor is:@@ -118,12 +228,23 @@ pub(crate) fn parse_drawing(data: &[u8]) -> Result<Vec<RawPic>> {let mut pic_depth: u32 = 0;let mut pending: Vec<(Option<String>, Option<String>)> = Vec::new(); // (name, rel_id)+ // The sp currently open, if any. `xdr:sp` cannot nest (only groups+ // nest), so one slot suffices; like `pending`, its result waits for the+ // anchor's End event, where the placement is finally known.+ let mut cur_sp: Option<OpenShape> = None;+ let mut pending_shapes: Vec<(Option<String>, crate::pptx::text::TextBody)> = Vec::new();+// Corner currently being filled and the element text being accumulated.let mut corner: Option<bool> = None; // true = from, false = tolet mut text_target: Option<&'static str> = None;let mut text = String::new();loop {+ // Byte offset where the next event's own bytes begin — whitespace+ // between tags is its own Text event, so after any event this is+ // exactly the start of the following tag. Used to slice a txBody+ // element out of the part verbatim.+ let tag_start = reader.buffer_position() as usize;let event = reader.read_event_into(&mut buf).map_err(quick_xml::DeError::from)?;@@ -134,6 +255,16 @@ pub(crate) fn parse_drawing(data: &[u8]) -> Result<Vec<RawPic>> {let name = local_name(qname.as_ref());let empty = matches!(event, Event::Empty(_));match name {+ // MCE: keep the Choice branch, skip the Fallback — the+ // PPTX shape walk's rule. Reading both (what this parser+ // did before it knew about MCE) double-places any object+ // that appears in each branch.+ b"Fallback" if !empty => {+ let end = e.to_end().into_owned();+ reader+ .read_to_end_into(end.name(), &mut skip_buf)+ .map_err(quick_xml::DeError::from)?;+ }b"oneCellAnchor" | b"twoCellAnchor" | b"absoluteAnchor" if !empty => {anchor_kind = Some(match name {b"oneCellAnchor" => "one",@@ -148,6 +279,8 @@ pub(crate) fn parse_drawing(data: &[u8]) -> Result<Vec<RawPic>> {object_depth = 0;pic_depth = 0;pending.clear();+ pending_shapes.clear();+ cur_sp = None;}b"pic" | b"sp" | b"grpSp" | b"graphicFrame" | b"cxnSp"if anchor_kind.is_some() =>@@ -158,6 +291,9 @@ pub(crate) fn parse_drawing(data: &[u8]) -> Result<Vec<RawPic>> {pic_depth += 1;}}+ if name == b"sp" && !empty {+ cur_sp = Some(OpenShape::default());+ }if !empty {object_depth += 1;}@@ -189,7 +325,9 @@ pub(crate) fn parse_drawing(data: &[u8]) -> Result<Vec<RawPic>> {pos = Some((x, y));}// `cNvPr` also names shapes and groups; only a picture's- // own (the first at its depth) is its display name.+ // or shape's own (the first at its depth) is its display+ // name. Order matters: an sp closes before a sibling pic+ // opens, so at most one of the two branches is live.b"cNvPr" if pic_depth > 0 => {if let Some(last) = pending.last_mut()&& last.0.is_none()@@ -197,6 +335,41 @@ pub(crate) fn parse_drawing(data: &[u8]) -> Result<Vec<RawPic>> {last.0 = attr(e, b"name").filter(|n| !n.is_empty());}}+ b"cNvPr" => {+ if let Some(sh) = cur_sp.as_mut()+ && !sh.seen_cnvpr+ {+ sh.seen_cnvpr = true;+ sh.name = attr(e, b"name").filter(|n| !n.is_empty());+ sh.hidden = attr(e, b"hidden").as_deref() == Some("1");+ }+ }+ // The shape's text body, sliced out verbatim and lowered+ // through the shared DrawingML text model. quick-xml's+ // serde layer matches local names with prefixes stripped,+ // so the `xdr:txBody` slice parses without namespace+ // re-declaration. A body that fails to parse costs its+ // shape's text, nothing else.+ b"txBody" if cur_sp.is_some() && pic_depth == 0 && !empty => {+ let end = e.to_end().into_owned();+ reader+ .read_to_end_into(end.name(), &mut skip_buf)+ .map_err(quick_xml::DeError::from)?;+ let tag_end = reader.buffer_position() as usize;+ let slice = &data[tag_start..tag_end];+ if let Some(sh) = cur_sp.as_mut()+ && let Ok(body) = crate::pptx::text::parse_text_body(slice)+ {+ // An OMML equation body (`a14:m` → `m:t` runs) is+ // invisible to the structured text model and+ // lowers to empty; rescue its glyphs flat.+ sh.body = if body.plain_text().trim().is_empty() {+ flat_math_body(slice).or(Some(body))+ } else {+ Some(body)+ };+ }+ }// The blip inside `xdr:blipFill`. A shape's background// fill also uses `a:blip`, so only capture inside a pic.b"blip" if pic_depth > 0 => {@@ -239,20 +412,37 @@ pub(crate) fn parse_drawing(data: &[u8]) -> Result<Vec<RawPic>> {};for (name, rel_id) in pending.drain(..) {if let Some(rel_id) = rel_id {- out.push(RawPic {+ out.pics.push(RawPic {anchor,name: name.clone(),rel_id,});}}+ for (name, body) in pending_shapes.drain(..) {+ out.shapes.push(SheetShape { anchor, name, body });+ }}}b"pic" if pic_depth > 0 => {pic_depth -= 1;object_depth = object_depth.saturating_sub(1);}- b"sp" | b"grpSp" | b"graphicFrame" | b"cxnSp" => {+ b"sp" => {+ object_depth = object_depth.saturating_sub(1);+ if let Some(sh) = cur_sp.take() {+ // Hidden shapes are legacy form controls Excel never+ // renders; whitespace-only bodies carry no content.+ // Both censused, both dropped here.+ if !sh.hidden+ && let Some(body) = sh.body+ && !body.plain_text().trim().is_empty()+ {+ pending_shapes.push((sh.name, body));+ }+ }+ }+ b"grpSp" | b"graphicFrame" | b"cxnSp" => {object_depth = object_depth.saturating_sub(1);}b"from" | b"to" => corner = None,@@ -301,7 +491,7 @@ mod tests {<xdr:clientData/></xdr:twoCellAnchor></xdr:wsDr>"#,pic("rId1", "Logo"));- let pics = parse_drawing(xml.as_bytes()).unwrap();+ let pics = parse_drawing(xml.as_bytes()).unwrap().pics;assert_eq!(pics.len(), 1);assert_eq!(pics[0].rel_id, "rId1");assert_eq!(pics[0].name.as_deref(), Some("Logo"));@@ -337,7 +527,7 @@ mod tests {<xdr:clientData/></xdr:oneCellAnchor></xdr:wsDr>"#,pic("rId2", "P"));- let pics = parse_drawing(xml.as_bytes()).unwrap();+ let pics = parse_drawing(xml.as_bytes()).unwrap().pics;assert_eq!(pics[0].anchor,PicAnchor::OneCell {@@ -363,7 +553,7 @@ mod tests {pic("rId3", "A"),pic("rId4", "B"));- let pics = parse_drawing(xml.as_bytes()).unwrap();+ let pics = parse_drawing(xml.as_bytes()).unwrap().pics;assert_eq!(pics.len(), 2);assert_eq!(pics[0].rel_id, "rId3");assert_eq!(pics[0].name.as_deref(), Some("A"));@@ -394,7 +584,9 @@ mod tests {<xdr:sp><xdr:spPr><a:blipFill><a:blip r:embed="rId9"/></a:blipFill></xdr:spPr></xdr:sp><xdr:clientData/></xdr:oneCellAnchor></xdr:wsDr>"#);- assert!(parse_drawing(xml.as_bytes()).unwrap().is_empty());+ let content = parse_drawing(xml.as_bytes()).unwrap();+ assert!(content.pics.is_empty());+ assert!(content.shapes.is_empty());}#[test]@@ -406,7 +598,7 @@ mod tests {<xdr:clientData/></xdr:absoluteAnchor></xdr:wsDr>"#,pic("rId5", "Abs"));- let pics = parse_drawing(xml.as_bytes()).unwrap();+ let pics = parse_drawing(xml.as_bytes()).unwrap().pics;assert_eq!(pics[0].anchor,PicAnchor::Absolute {@@ -427,6 +619,162 @@ mod tests {<xdr:graphicFrame><a:graphic><a:graphicData uri="chart"/></a:graphic></xdr:graphicFrame><xdr:clientData/></xdr:twoCellAnchor></xdr:wsDr>"#);- assert!(parse_drawing(xml.as_bytes()).unwrap().is_empty());+ let content = parse_drawing(xml.as_bytes()).unwrap();+ assert!(content.pics.is_empty());+ assert!(content.shapes.is_empty());+ }++ fn text_sp(body: &str) -> String {+ format!(+ r#"<xdr:sp><xdr:nvSpPr><xdr:cNvPr id="7" name="TextBox 1"/><xdr:cNvSpPr txBox="1"/></xdr:nvSpPr>+ <xdr:spPr><a:xfrm><a:off x="1" y="2"/><a:ext cx="999" cy="888"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></xdr:spPr>+ <xdr:txBody><a:bodyPr/><a:lstStyle/>{body}</xdr:txBody></xdr:sp>"#+ )+ }++ /// A text shape parses into a lowered body carrying the anchor that was+ /// only known after the shape closed.+ #[test]+ fn a_text_shape_takes_its_anchor_and_lowers_its_body() {+ let xml = format!(+ r#"<xdr:wsDr {NS}><xdr:twoCellAnchor>+ <xdr:from><xdr:col>2</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>3</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:to><xdr:col>5</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>6</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:to>+ {}+ <xdr:clientData/></xdr:twoCellAnchor></xdr:wsDr>"#,+ text_sp(+ r#"<a:p><a:r><a:rPr lang="en-US" sz="1100"/><a:t>Section title</a:t></a:r></a:p><a:p><a:r><a:t>second line</a:t></a:r></a:p>"#+ )+ );+ let content = parse_drawing(xml.as_bytes()).unwrap();+ assert!(content.pics.is_empty());+ assert_eq!(content.shapes.len(), 1);+ let sh = &content.shapes[0];+ assert_eq!(sh.name.as_deref(), Some("TextBox 1"));+ assert_eq!(+ sh.body.plain_text(),+ "Section title+second line"+ );+ assert_eq!(+ sh.anchor.from_cell(),+ Some(CellAnchor {+ col: 2,+ row: 3,+ ..Default::default()+ })+ );+ }++ /// `hidden="1"` marks a legacy form control (checkbox option labels and+ /// friends) that Excel never renders — 1,004 of them in the corpus.+ #[test]+ fn a_hidden_shape_is_a_form_control_and_is_skipped() {+ let xml = format!(+ r#"<xdr:wsDr {NS}><xdr:oneCellAnchor>+ <xdr:from><xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:ext cx="1" cy="1"/>+ <xdr:sp><xdr:nvSpPr><xdr:cNvPr id="9" name="Check Box 15" hidden="1"/></xdr:nvSpPr>+ <xdr:txBody><a:bodyPr/><a:p><a:r><a:t>UNKNOWN</a:t></a:r></a:p></xdr:txBody></xdr:sp>+ <xdr:clientData/></xdr:oneCellAnchor></xdr:wsDr>"#+ );+ assert!(parse_drawing(xml.as_bytes()).unwrap().shapes.is_empty());+ }++ /// A whitespace-only body carries no content; the census counted 4,454.+ #[test]+ fn a_whitespace_only_body_is_skipped() {+ let xml = format!(+ r#"<xdr:wsDr {NS}><xdr:oneCellAnchor>+ <xdr:from><xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:ext cx="1" cy="1"/>+ {}+ <xdr:clientData/></xdr:oneCellAnchor></xdr:wsDr>"#,+ text_sp(r#"<a:p><a:r><a:t> </a:t></a:r></a:p>"#)+ );+ assert!(parse_drawing(xml.as_bytes()).unwrap().shapes.is_empty());+ }++ /// A grouped text shape inherits the group's anchor, like a grouped+ /// picture — and its cNvPr is its own, not the group's.+ #[test]+ fn a_grouped_text_shape_takes_the_group_anchor_and_its_own_name() {+ let xml = format!(+ r#"<xdr:wsDr {NS}><xdr:twoCellAnchor>+ <xdr:from><xdr:col>3</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>4</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:to><xdr:col>6</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>9</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:to>+ <xdr:grpSp><xdr:nvGrpSpPr><xdr:cNvPr id="1" name="Group 1"/></xdr:nvGrpSpPr>+ {}+ </xdr:grpSp>+ <xdr:clientData/></xdr:twoCellAnchor></xdr:wsDr>"#,+ text_sp(r#"<a:p><a:r><a:t>label</a:t></a:r></a:p>"#)+ );+ let content = parse_drawing(xml.as_bytes()).unwrap();+ assert_eq!(content.shapes.len(), 1);+ assert_eq!(content.shapes[0].name.as_deref(), Some("TextBox 1"));+ assert_eq!(+ content.shapes[0].anchor.from_cell(),+ Some(CellAnchor {+ col: 3,+ row: 4,+ ..Default::default()+ })+ );+ }++ /// MCE: the Choice branch wins, the Fallback is skipped — an object in+ /// both branches yields ONE placement, not two. This was a live bug for+ /// pictures (~12 phantom placements corpus-wide) before shapes arrived.+ #[test]+ fn mce_prefers_choice_and_never_reads_both_branches() {+ let xml = format!(+ r#"<xdr:wsDr {NS} xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"><xdr:twoCellAnchor>+ <xdr:from><xdr:col>1</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>1</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:to><xdr:col>4</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>4</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:to>+ <mc:AlternateContent>+ <mc:Choice Requires="a14">{}{}</mc:Choice>+ <mc:Fallback>{}{}</mc:Fallback>+ </mc:AlternateContent>+ <xdr:clientData/></xdr:twoCellAnchor></xdr:wsDr>"#,+ text_sp(r#"<a:p><a:r><a:t>choice text</a:t></a:r></a:p>"#),+ pic("rId1", "P"),+ text_sp(r#"<a:p><a:r><a:t>fallback text</a:t></a:r></a:p>"#),+ pic("rId1", "P")+ );+ let content = parse_drawing(xml.as_bytes()).unwrap();+ assert_eq!(content.pics.len(), 1);+ assert_eq!(content.shapes.len(), 1);+ assert_eq!(content.shapes[0].body.plain_text(), "choice text");+ }++ /// An OMML equation shape keeps its glyphs: the structured model sees an+ /// empty paragraph, and the flat rescue reads the `m:t` runs instead.+ #[test]+ fn an_omml_equation_body_flattens_to_its_glyphs() {+ let xml = format!(+ r#"<xdr:wsDr {NS} xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"><xdr:oneCellAnchor>+ <xdr:from><xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:ext cx="1" cy="1"/>+ <xdr:sp><xdr:nvSpPr><xdr:cNvPr id="4" name="Eq"/></xdr:nvSpPr><xdr:spPr/>+ <xdr:txBody><a:bodyPr/><a:p><a:pPr/><a14:m><m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><m:r><m:t>𝑛=</m:t></m:r><m:r><m:t>𝑑1/6</m:t></m:r></m:oMath></a14:m></a:p></xdr:txBody></xdr:sp>+ <xdr:clientData/></xdr:oneCellAnchor></xdr:wsDr>"#+ );+ let content = parse_drawing(xml.as_bytes()).unwrap();+ assert_eq!(content.shapes.len(), 1);+ assert_eq!(content.shapes[0].body.plain_text(), "𝑛=𝑑1/6");+ }++ /// The census's zero: a connector's text (schema-legal, corpus-absent)+ /// is not read, and its presence does not disturb a sibling text shape.+ #[test]+ fn a_connector_yields_no_text_shape() {+ let xml = format!(+ r#"<xdr:wsDr {NS}><xdr:oneCellAnchor>+ <xdr:from><xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:ext cx="1" cy="1"/>+ <xdr:cxnSp><xdr:nvCxnSpPr><xdr:cNvPr id="3" name="Line 1"/></xdr:nvCxnSpPr><xdr:spPr/></xdr:cxnSp>+ <xdr:clientData/></xdr:oneCellAnchor></xdr:wsDr>"#+ );+ assert!(parse_drawing(xml.as_bytes()).unwrap().shapes.is_empty());}}diff --git a/crates/liteparse-ooxml/src/xlsx/mod.rs b/crates/liteparse-ooxml/src/xlsx/mod.rsindex 52dbe7f..2b9bdc1 100644--- a/crates/liteparse-ooxml/src/xlsx/mod.rs+++ b/crates/liteparse-ooxml/src/xlsx/mod.rs@@ -52,7 +52,7 @@ pub mod styles;pub mod text;mod xml;-pub use drawings::{CellAnchor, PicAnchor, SheetPic};+pub use drawings::{CellAnchor, PicAnchor, SheetPic, SheetShape};pub use package::{SheetEntry, WorkbookPackage, walk};pub use refs::{CellRef, RangeRef, column_label, parse_cell, parse_column, parse_range};pub use sheet::{Cell, CellValue, ColInfo, Hyperlink, Row, Sheet, SheetStats};@@ -150,25 +150,29 @@ pub fn read(data: &[u8]) -> Result<Workbook> {let drawing_path = resolve_target(part_directory(&entry.path), &rel.target);if let Some(dxml) = pkg.package.get_part(&drawing_path) {match drawings::parse_drawing(dxml) {- Ok(raws) if !raws.is_empty() => {- let drels = package::load_rels(&pkg.package, &drawing_path);- let ddir = part_directory(&drawing_path).to_string();- for raw in raws {- let Some(r) = drels- .find_by_id(&raw.rel_id)- .filter(|r| r.target_mode != TargetMode::External)- else {- continue;- };- pending_pics.push((- sheets.len(),- raw.anchor,- raw.name,- resolve_target(&ddir, &r.target),- ));+ Ok(content) => {+ if !content.pics.is_empty() {+ let drels = package::load_rels(&pkg.package, &drawing_path);+ let ddir = part_directory(&drawing_path).to_string();+ for raw in content.pics {+ let Some(r) = drels+ .find_by_id(&raw.rel_id)+ .filter(|r| r.target_mode != TargetMode::External)+ else {+ continue;+ };+ pending_pics.push((+ sheets.len(),+ raw.anchor,+ raw.name,+ resolve_target(&ddir, &r.target),+ ));+ }}+ // Text shapes are complete as parsed — no+ // media, no rels — so they attach directly.+ s.shapes = content.shapes;}- Ok(_) => {}Err(e) => {log::warn!("drawing part {drawing_path} failed to parse: {e}")}diff --git a/crates/liteparse-ooxml/src/xlsx/sheet.rs b/crates/liteparse-ooxml/src/xlsx/sheet.rsindex 17d1b16..fb85320 100644--- a/crates/liteparse-ooxml/src/xlsx/sheet.rs+++ b/crates/liteparse-ooxml/src/xlsx/sheet.rs@@ -154,6 +154,10 @@ pub struct Sheet {/// the drawing part — empty until then, and empty for the 69% of corpus/// workbooks whose sheets draw nothing.pub pics: Vec<crate::xlsx::drawings::SheetPic>,+ /// Visible text-bearing shapes floating over the grid, from the same+ /// drawing part as `pics`. Titles, form labels, instructions — content+ /// no cell holds.+ pub shapes: Vec<crate::xlsx::drawings::SheetShape>,pub stats: SheetStats,}diff --git a/crates/liteparse/src/office/inline.rs b/crates/liteparse/src/office/inline.rsindex 6782a90..424db6e 100644--- a/crates/liteparse/src/office/inline.rs+++ b/crates/liteparse/src/office/inline.rs@@ -11,6 +11,8 @@//! whichever reader produced it, which is the whole point of both pipelines//! converging on `markdown_layout::Block`.+use liteparse_ooxml::model::{RunProperties, StrikeStyle};+use crate::markdown_layout::{apply_link, escape_inline};/// Effective character formatting after the style cascade is applied.@@ -21,6 +23,16 @@ pub(crate) struct Fmt {pub(crate) strike: bool,}+/// The [`Fmt`] a DrawingML run's (cascade-resolved or explicit) properties+/// declare. Shared by the PPTX emitter and the XLSX text-shape path.+pub(crate) fn fmt_of(p: &RunProperties) -> Fmt {+ Fmt {+ bold: p.bold.unwrap_or(false),+ italic: p.italic.unwrap_or(false),+ strike: matches!(p.strike, Some(StrikeStyle::Single | StrikeStyle::Double)),+ }+}+impl Fmt {pub(crate) fn is_plain(self) -> bool {!self.bold && !self.italic && !self.strikediff --git a/crates/liteparse/src/office/pptx.rs b/crates/liteparse/src/office/pptx.rsindex cb8df6e..6bf69df 100644--- a/crates/liteparse/src/office/pptx.rs+++ b/crates/liteparse/src/office/pptx.rs@@ -12,7 +12,7 @@//! The design below follows that census throughout; where a rule looks//! arbitrary it is usually a corpus measurement, and the comment says which.-use liteparse_ooxml::model::{ColorMap, Inline, RunElement, RunProperties, StrikeStyle};+use liteparse_ooxml::model::{ColorMap, Inline, RunElement};use liteparse_ooxml::pptx::{self, AutoNumberScheme, Background, BackgroundSource, Bullet, DeckTextDefaults,GraphicFramePayload, ListStyle, MatchRule, Placeholder, PlaceholderGeometry, PlaceholderKind,@@ -27,7 +27,7 @@ use std::collections::HashMap;use crate::error::LiteParseError;use crate::markdown_layout::{Block, Cell};use crate::office::figures::FigureSink;-use crate::office::inline::{Chunk, Fmt, render_chunks};+use crate::office::inline::{Chunk, fmt_of, render_chunks};use crate::types::{ExtractedImage, Rect};/// Two shapes whose tops differ by less than this sit on the same visual row@@ -1458,14 +1458,6 @@ fn hyperlink_url(h: &liteparse_ooxml::model::Hyperlink, ctx: &SlideCtx<'_>) -> O}}-fn fmt_of(p: &RunProperties) -> Fmt {- Fmt {- bold: p.bold.unwrap_or(false),- italic: p.italic.unwrap_or(false),- strike: matches!(p.strike, Some(StrikeStyle::Single | StrikeStyle::Double)),- }-}-#[cfg(test)]mod tests {use super::*;diff --git a/crates/liteparse/src/office/xlsx.rs b/crates/liteparse/src/office/xlsx.rsindex e349649..cc3d87b 100644--- a/crates/liteparse/src/office/xlsx.rs+++ b/crates/liteparse/src/office/xlsx.rs@@ -41,10 +41,15 @@use std::collections::HashMap;use std::ops::Range;-use liteparse_ooxml::xlsx::{self, Cell as GridCell, CellValue, Row, Sheet, SheetPic, Workbook};+use liteparse_ooxml::model::{Inline, RunElement};+use liteparse_ooxml::pptx::TextParagraph;+use liteparse_ooxml::xlsx::{+ self, Cell as GridCell, CellValue, Row, Sheet, SheetPic, SheetShape, Workbook,+};use crate::error::LiteParseError;use crate::markdown_layout::{Block, Cell, apply_link, escape_inline};+use crate::office::inline::{Chunk, fmt_of, render_chunks};/// Sheet names become `#` — the workbook's only structural rank, mirroring/// slide titles on the PPTX path.@@ -120,6 +125,10 @@ pub fn emit_workbook(wb: &Workbook, opts: EmitOptions) -> NativeWorkbook {},si,));+ let (shapes_above, shapes_below) = shape_blocks(sheet);+ for b in shapes_above {+ blocks.push((b, si));+ }if let Some(plan) = SheetPlan::build(wb, sheet, opts) {let n = plan.rows.len();for page in plan.page_blocks(wb, &[0..n]) {@@ -128,6 +137,9 @@ pub fn emit_workbook(wb: &Workbook, opts: EmitOptions) -> NativeWorkbook {}}}+ for b in shapes_below {+ blocks.push((b, si));+ }if opts.figures {for b in figure_blocks(sheet, si) {blocks.push((b, si));@@ -180,6 +192,124 @@ pub(crate) fn figure_blocks(sheet: &Sheet, sheet_index: usize) -> Vec<Block> {out}+/// A sheet's text shapes in reading order — the same anchor sort as+/// [`ordered_pics`], and for the same reason: the geometry pass walks the+/// same list, so a shape's blocks and its placed items cannot disagree on+/// order.+pub(crate) fn ordered_shapes(sheet: &Sheet) -> Vec<&SheetShape> {+ let mut shapes: Vec<&SheetShape> = sheet.shapes.iter().collect();+ shapes.sort_by_key(|s| match s.anchor.from_cell() {+ Some(c) => (c.row as i64, c.col as i64, c.row_off_emu, c.col_off_emu),+ None => match s.anchor {+ xlsx::PicAnchor::Absolute { pos_emu, .. } => (i64::MAX, pos_emu.1, pos_emu.0, 0),+ _ => unreachable!("from_cell is None only for Absolute"),+ },+ });+ shapes+}++/// The paragraph blocks for a sheet's floating text shapes, split at the+/// grid: `(before the table, after the table)`.+///+/// The census (plan doc, floating text-shape entry) measured where shapes+/// anchor: 33% sit *above* the sheet's first written row, and the eye check+/// says those are titles and section navigation — emitting them after the+/// table would bury every title under its own data. Everything else follows+/// the figures precedent: shapes float *over* the grid, so interleaving with+/// specific rows would be false precision, and they emit after the table,+/// before the figure refs.+///+/// Census-scoped non-goals: bullets (7 shapes corpus-wide) emit as plain+/// paragraphs; `a:hlinkClick` (7) keeps its text and drops the link, which+/// would need the drawing part's rels threaded through the reader.+pub(crate) fn shape_blocks(sheet: &Sheet) -> (Vec<Block>, Vec<Block>) {+ let first_row = first_written_row(sheet);+ let mut above = Vec::new();+ let mut below = Vec::new();+ for shape in ordered_shapes(sheet) {+ let dst = if shape_is_above(shape, first_row) {+ &mut above+ } else {+ &mut below+ };+ dst.extend(shape_paragraphs(shape));+ }+ (above, below)+}++/// The index of the sheet's first written row with cells — the boundary the+/// above/below split keys on.+pub(crate) fn first_written_row(sheet: &Sheet) -> Option<u32> {+ sheet+ .rows+ .iter()+ .find(|r| !r.cells.is_empty())+ .map(|r| r.index)+}++/// Whether a shape anchors above the grid's first written row. A shape on a+/// sheet with no written cells is the sheet's only content and lands in the+/// "after" half, where an empty grid emits nothing before it.+pub(crate) fn shape_is_above(shape: &SheetShape, first_row: Option<u32>) -> bool {+ match (shape.anchor.from_cell(), first_row) {+ (Some(c), Some(first)) => c.row < first,+ _ => false,+ }+}++/// One shape's paragraph blocks, in body order. Shared by the doc emitter+/// and the geometry pass so a shape's blocks cannot differ between the two.+pub(crate) fn shape_paragraphs(shape: &SheetShape) -> Vec<Block> {+ let mut out = Vec::new();+ for para in &shape.body.paragraphs {+ let chunks = shape_chunks(para);+ let (text, bold, italic) = render_chunks(&chunks, true);+ if text.is_empty() {+ continue;+ }+ out.push(Block::Paragraph { text, bold, italic });+ }+ out+}++/// A shape paragraph's formatting-tagged chunks. Unlike the PPTX path there+/// is no cascade to resolve — a worksheet drawing has no master or layout —+/// so a run's explicit properties are the whole truth.+fn shape_chunks(para: &TextParagraph) -> Vec<Chunk> {+ fn walk(inlines: &[Inline], out: &mut Vec<Chunk>) {+ for inline in inlines {+ match inline {+ Inline::TextRun(run) => {+ let mut text = String::new();+ for el in &run.content {+ match el {+ RunElement::Text(t) => text.push_str(t),+ RunElement::Tab => text.push('\t'),+ // `a:br` inside one paragraph; `Block`'s+ // single-line text cannot hold it (the PPTX+ // path's rule, same reason).+ RunElement::LineBreak(_) => text.push(' '),+ _ => {}+ }+ }+ if !text.is_empty() {+ out.push(Chunk {+ fmt: fmt_of(&run.properties),+ link: None,+ text,+ });+ }+ }+ Inline::Hyperlink(h) => walk(&h.content, out),+ _ => {}+ }+ }+ }+ let mut out = Vec::new();+ walk(¶.content, &mut out);+ out+}+/// A merge's place in the emitted grid: spans counted over *emitted* rows and/// *retained* columns, so a merge across skipped-empty rows does not claim/// table rows that do not exist.@@ -1056,6 +1186,122 @@ pub(crate) mod tests {/// Header rows mark only the slice containing the grid's first row —/// a continuation page's table has no header.+ // ── floating text shapes ────────────────────────────────────────────────++ /// Parts hanging one text shape off sheet1 via a drawing part. Shapes+ /// resolve nothing, so no drawing rels and no media are needed.+ fn shape_parts(anchor_xml: &str) -> Vec<(String, String)> {+ let sheet_rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">+ <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing1.xml"/>+</Relationships>"#;+ let drawing = format!(+ r#"<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">{anchor_xml}</xdr:wsDr>"#+ );+ vec![+ (+ "xl/worksheets/_rels/sheet1.xml.rels".to_string(),+ sheet_rels.to_string(),+ ),+ ("xl/drawings/drawing1.xml".to_string(), drawing),+ ]+ }++ fn shape_anchor(from_row: u32, paras: &str) -> String {+ format!(+ r#"<xdr:oneCellAnchor>+ <xdr:from><xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>{from_row}</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:ext cx="914400" cy="457200"/>+ <xdr:sp><xdr:nvSpPr><xdr:cNvPr id="5" name="TextBox 1"/><xdr:cNvSpPr txBox="1"/></xdr:nvSpPr>+ <xdr:spPr/><xdr:txBody><a:bodyPr/>{paras}</xdr:txBody></xdr:sp>+ <xdr:clientData/></xdr:oneCellAnchor>"#+ )+ }++ fn shape_blocks_of(sheet_xml: &str, anchor_xml: &str) -> Vec<Block> {+ let parts = shape_parts(anchor_xml);+ let extra: Vec<(&str, &str)> = parts+ .iter()+ .map(|(a, b)| (a.as_str(), b.as_str()))+ .collect();+ let wb = workbook_from(sheet_xml, &extra);+ emit_workbook(&wb, EmitOptions::default())+ .blocks+ .into_iter()+ .map(|(b, _)| b)+ .collect()+ }++ const TWO_ROW_GRID: &str = r#"<worksheet><sheetData>+ <row r="3"><c r="A3" t="inlineStr"><is><t>a</t></is></c><c r="B3"><v>1</v></c></row>+ <row r="4"><c r="A4" t="inlineStr"><is><t>b</t></is></c><c r="B4"><v>2</v></c></row>+ </sheetData><drawing r:id="rId7"/></worksheet>"#;++ /// The census's placement rule: a shape anchored above the first written+ /// row is a title and must not be buried under its own table.+ #[test]+ fn a_shape_above_the_grid_emits_before_the_table() {+ let blocks = shape_blocks_of(+ TWO_ROW_GRID,+ &shape_anchor(0, r#"<a:p><a:r><a:t>Quarterly Summary</a:t></a:r></a:p>"#),+ );+ assert!(matches!(&blocks[0], Block::Heading { .. }));+ assert!(+ matches!(&blocks[1], Block::Paragraph { text, .. } if text == "Quarterly Summary"),+ "title shape should precede the table, got {blocks:?}"+ );+ assert!(matches!(&blocks[2], Block::MergedTable { .. }));+ }++ /// Everything else follows the figures precedent: after the table.+ #[test]+ fn a_shape_over_the_grid_emits_after_the_table() {+ let blocks = shape_blocks_of(+ TWO_ROW_GRID,+ &shape_anchor(3, r#"<a:p><a:r><a:t>see note</a:t></a:r></a:p>"#),+ );+ assert!(matches!(&blocks[1], Block::MergedTable { .. }));+ assert!(+ matches!(&blocks[2], Block::Paragraph { text, .. } if text == "see note"),+ "annotation shape should follow the table, got {blocks:?}"+ );+ }++ /// 54 corpus shapes sit on sheets with no written cells — today those+ /// sheets emit nothing at all; the shape is the sheet's only content.+ #[test]+ fn a_shape_on_an_empty_sheet_is_its_only_content() {+ let blocks = shape_blocks_of(+ r#"<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheetData/><drawing r:id="rId7"/></worksheet>"#,+ &shape_anchor(2, r#"<a:p><a:r><a:t>orphan text</a:t></a:r></a:p>"#),+ );+ assert!(matches!(&blocks[0], Block::Heading { .. }));+ assert!(+ matches!(&blocks[1], Block::Paragraph { text, .. } if text == "orphan text"),+ "got {blocks:?}"+ );+ assert_eq!(blocks.len(), 2);+ }++ /// Run emphasis reaches the block the same way the PPTX path's does, and+ /// each paragraph is its own block.+ #[test]+ fn shape_paragraphs_carry_emphasis_and_split_per_paragraph() {+ let blocks = shape_blocks_of(+ TWO_ROW_GRID,+ &shape_anchor(+ 3,+ r#"<a:p><a:r><a:rPr b="1"/><a:t>NOTES:</a:t></a:r></a:p><a:p><a:r><a:t>fill in blue cells</a:t></a:r></a:p>"#,+ ),+ );+ assert!(+ matches!(&blocks[2], Block::Paragraph { text, bold: true, .. } if text == "NOTES:"),+ "got {blocks:?}"+ );+ assert!(+ matches!(&blocks[3], Block::Paragraph { text, bold: false, .. } if text == "fill in blue cells")+ );+ }+#[test]fn header_rows_do_not_repeat_on_continuation_pages() {let wb = workbook_from(diff --git a/crates/liteparse/src/office/xlsx_layout.rs b/crates/liteparse/src/office/xlsx_layout.rsindex 251ada0..5631ee9 100644--- a/crates/liteparse/src/office/xlsx_layout.rs+++ b/crates/liteparse/src/office/xlsx_layout.rs@@ -33,7 +33,7 @@use std::ops::Range;-use liteparse_ooxml::xlsx::{CellAnchor, PicAnchor, Sheet, Workbook};+use liteparse_ooxml::xlsx::{CellAnchor, PicAnchor, Sheet, SheetShape, Workbook};use super::figures::FigureSink;use super::xlsx::{EmitOptions, SHEET_HEADING_LEVEL, SheetPlan};@@ -215,15 +215,19 @@ pub fn workbook_to_pages(wb: &Workbook, opts: EmitOptions) -> NativeXlsx {),};- // Doc-level: the unsplit emission (one full-range slice), then the- // sheet's figures — the same order `emit_workbook` writes, so the- // two doc emissions stay byte-identical.+ // Doc-level: title-like shapes above the grid, the unsplit emission+ // (one full-range slice), the remaining shapes, then the sheet's+ // figures — the same order `emit_workbook` writes, so the two doc+ // emissions stay byte-identical.+ let (shapes_above, shapes_below) = super::xlsx::shape_blocks(sheet);+ out.all_blocks.extend(shapes_above);if let Some(p) = &plan {let n = p.rows.len();for page in p.page_blocks(wb, &[0..n]) {out.all_blocks.extend(page);}}+ out.all_blocks.extend(shapes_below);if opts.figures {out.all_blocks.extend(super::xlsx::figure_blocks(sheet, si));}@@ -234,14 +238,42 @@ pub fn workbook_to_pages(wb: &Workbook, opts: EmitOptions) -> NativeXlsx {};blocks_per_page[0].insert(0, heading);+ // Text shapes: page assignment + rect through the same anchor+ // placement as pictures, blocks mirroring the doc order per page —+ // above-grid shapes right after the heading, the rest after the+ // page's table slice (and before its figure refs, pushed below).+ // Items follow the pass's own philosophy — unit conversion, not+ // layout: one item per paragraph, stacked evenly in the shape's box.+ let grid_w = *geo.x_off.last().unwrap() as f32;+ let page_width = (grid_w + 2.0 * MARGIN).max(MIN_PAGE_WIDTH);+ let canvas = CanvasGrid::build(sheet);+ let first_row = super::xlsx::first_written_row(sheet);+ let mut shape_items_per_page: Vec<Vec<TextItem>> = vec![Vec::new(); ranges.len()];+ let mut above_blocks: Vec<Block> = Vec::new();+ for shape in super::xlsx::ordered_shapes(sheet) {+ let (page_local, rect) = geo.place_pic(+ cols,+ &row_indices,+ &ranges,+ page_width,+ &canvas,+ &shape.anchor,+ );+ shape_text_items(shape, &rect, &mut shape_items_per_page[page_local]);+ let blocks = super::xlsx::shape_paragraphs(shape);+ if super::xlsx::shape_is_above(shape, first_row) {+ above_blocks.extend(blocks);+ } else {+ blocks_per_page[page_local].extend(blocks);+ }+ }+ blocks_per_page[0].splice(1..1, above_blocks);+// Pictures: page assignment + rect from the packed grid, figure// blocks on the page the anchor lands on, bytes into the sink. The// local ordinal mirrors `figure_blocks` (both skip media we do not// surface), and the sink's id must agree with it by construction.- let grid_w = *geo.x_off.last().unwrap() as f32;- let page_width = (grid_w + 2.0 * MARGIN).max(MIN_PAGE_WIDTH);let mut rects_per_page: Vec<Vec<Rect>> = vec![Vec::new(); ranges.len()];- let canvas = CanvasGrid::build(sheet);sink.reset_ordinal();let mut n_fig = 0u32;for pic in super::xlsx::ordered_pics(sheet) {@@ -283,10 +315,11 @@ pub fn workbook_to_pages(wb: &Workbook, opts: EmitOptions) -> NativeXlsx {}for (i, blocks) in blocks_per_page.into_iter().enumerate() {- let page = match &plan {+ let mut page = match &plan {Some(p) => geo.build_page(wb, p, ranges[i].clone(), out.pages.len() + 1),None => empty_page(out.pages.len() + 1),};+ append_items(&mut page, std::mem::take(&mut shape_items_per_page[i]));out.pages.push(page);out.page_blocks.push(blocks);out.pic_rects.push(std::mem::take(&mut rects_per_page[i]));@@ -296,6 +329,70 @@ pub fn workbook_to_pages(wb: &Workbook, opts: EmitOptions) -> NativeXlsx {out}+/// One `TextItem` per non-empty shape paragraph, stacked evenly inside the+/// shape's box — the same fidelity contract as a cell's item (the box is+/// real, the intra-box line layout is not attempted). Font facts come from+/// the paragraph's first run: 99.5% of corpus shape runs declare `sz`, and a+/// theme-referenced face (no explicit name) stays `None` like an unstyled+/// cell would.+fn shape_text_items(shape: &SheetShape, rect: &Rect, out: &mut Vec<TextItem>) {+ let paras: Vec<&liteparse_ooxml::pptx::TextParagraph> = shape+ .body+ .paragraphs+ .iter()+ .filter(|p| !p.text().trim().is_empty())+ .collect();+ if paras.is_empty() {+ return;+ }+ let each_h = rect.height / paras.len() as f32;+ for (i, para) in paras.iter().enumerate() {+ fn first_run(+ inlines: &[liteparse_ooxml::model::Inline],+ ) -> Option<&liteparse_ooxml::model::TextRun> {+ inlines.iter().find_map(|inl| match inl {+ liteparse_ooxml::model::Inline::TextRun(r) => Some(&**r),+ liteparse_ooxml::model::Inline::Hyperlink(h) => first_run(&h.content),+ _ => None,+ })+ }+ let props = first_run(¶.content).map(|r| &r.properties);+ out.push(TextItem {+ text: para.text().trim().replace('\n', " "),+ x: rect.x,+ y: rect.y + i as f32 * each_h,+ width: rect.width,+ height: each_h,+ font_name: props.and_then(|p| p.fonts.ascii.explicit.clone()),+ font_size: props+ .and_then(|p| p.font_size)+ .map(|s| s.raw() as f32 / 2.0),+ font_weight: props.and_then(|p| p.bold).unwrap_or(false).then_some(700),+ ..Default::default()+ });+ }+}++/// Extend a built page with extra items, keeping `content_bounds` the union+/// it was defined as.+fn append_items(page: &mut Page, items: Vec<TextItem>) {+ if items.is_empty() {+ return;+ }+ page.text_items.extend(items);+ page.content_bounds = page+ .text_items+ .iter()+ .map(|t| (t.x, t.y, t.x + t.width, t.y + t.height))+ .reduce(|a, b| (a.0.min(b.0), a.1.min(b.1), a.2.max(b.2), a.3.max(b.3)))+ .map(|(x0, y0, x1, y1)| Rect {+ x: x0,+ y: y0,+ width: x1 - x0,+ height: y1 - y0,+ });+}+fn empty_page(page_number: usize) -> Page {Page {page_number,@@ -996,4 +1093,140 @@ mod tests {"geometry-pass doc blocks diverge from the emitter's");}++ // ── floating text shapes ────────────────────────────────────────────────++ fn shape_drawing_parts(anchor_xml: &str) -> Vec<(String, String)> {+ let sheet_rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">+ <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing" Target="../drawings/drawing1.xml"/>+</Relationships>"#;+ let drawing = format!(+ r#"<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">{anchor_xml}</xdr:wsDr>"#+ );+ vec![+ (+ "xl/worksheets/_rels/sheet1.xml.rels".to_string(),+ sheet_rels.to_string(),+ ),+ ("xl/drawings/drawing1.xml".to_string(), drawing),+ ]+ }++ /// A two-paragraph shape yields one item per paragraph, stacked in the+ /// shape's box, carrying the run's declared face and size — and the doc+ /// blocks stay byte-equal to the emitter's, title-before-table split+ /// included.+ #[test]+ fn shape_items_stack_in_the_box_and_doc_blocks_match_the_emitter() {+ let sheet = r#"<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheetData>+ <row r="4"><c r="A4" t="inlineStr"><is><t>a</t></is></c><c r="B4"><v>1</v></c></row>+ <row r="5"><c r="A5"><v>2</v></c><c r="B5"><v>3</v></c></row>+ </sheetData><drawing r:id="rId7"/></worksheet>"#;+ // Anchored at row 0 — above the first written row (r=4, index 3).+ let anchor = r#"<xdr:oneCellAnchor>+ <xdr:from><xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>0</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:ext cx="914400" cy="457200"/>+ <xdr:sp><xdr:nvSpPr><xdr:cNvPr id="5" name="TextBox 1"/><xdr:cNvSpPr txBox="1"/></xdr:nvSpPr>+ <xdr:spPr/><xdr:txBody><a:bodyPr/>+ <a:p><a:r><a:rPr sz="1400" b="1"><a:latin typeface="Arial"/></a:rPr><a:t>Title</a:t></a:r></a:p>+ <a:p><a:r><a:rPr sz="1100"/><a:t>subtitle</a:t></a:r></a:p>+ </xdr:txBody></xdr:sp>+ <xdr:clientData/></xdr:oneCellAnchor>"#;+ let parts = shape_drawing_parts(anchor);+ let extra: Vec<(&str, &str)> = parts+ .iter()+ .map(|(a, b)| (a.as_str(), b.as_str()))+ .collect();+ let wb = workbook_from(sheet, &extra);+ let opts = EmitOptions::default();++ let emitted: Vec<Block> = crate::office::xlsx::emit_workbook(&wb, opts)+ .blocks+ .into_iter()+ .map(|(b, _)| b)+ .collect();+ let nx = workbook_to_pages(&wb, opts);+ assert_eq!(+ format!("{emitted:?}"),+ format!("{:?}", nx.all_blocks),+ "geometry-pass doc blocks diverge from the emitter's"+ );++ // Page blocks mirror the doc split: title right after the heading.+ assert!(matches!(&nx.page_blocks[0][0], Block::Heading { .. }));+ assert!(+ matches!(&nx.page_blocks[0][1], Block::Paragraph { text, .. } if text == "Title"),+ "got {:?}",+ nx.page_blocks[0]+ );++ // Two items, stacked halves of the 914400×457200 EMU box (72×36 pt),+ // anchored at the grid origin (row 0 collapses to the packed top).+ let items: Vec<&TextItem> = nx.pages[0]+ .text_items+ .iter()+ .filter(|t| t.text == "Title" || t.text == "subtitle")+ .collect();+ assert_eq!(items.len(), 2);+ assert_eq!(items[0].font_name.as_deref(), Some("Arial"));+ assert_eq!(items[0].font_size, Some(14.0));+ assert_eq!(items[0].font_weight, Some(700));+ assert_eq!(items[1].font_size, Some(11.0));+ assert!(+ (items[0].width - 72.0).abs() < 0.5,+ "box width is the anchor ext"+ );+ assert!((items[0].height - 18.0).abs() < 0.5, "half the box each");+ assert!((items[1].y - items[0].y - 18.0).abs() < 0.5, "stacked");+ }++ /// A shape anchored on later pages' rows lands its blocks and items on+ /// that page, like a picture.+ #[test]+ fn a_shape_lands_on_its_anchor_row_page() {+ let mut rows = String::new();+ for r in 1..=60 {+ rows.push_str(&format!(+ r#"<row r="{r}" ht="20"><c r="A{r}"><v>{r}</v></c></row>"#+ ));+ }+ let sheet = format!(+ r#"<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheetData>{rows}</sheetData><drawing r:id="rId7"/></worksheet>"#+ );+ let anchor = r#"<xdr:oneCellAnchor>+ <xdr:from><xdr:col>0</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>50</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>+ <xdr:ext cx="914400" cy="457200"/>+ <xdr:sp><xdr:nvSpPr><xdr:cNvPr id="5" name="T"/></xdr:nvSpPr><xdr:spPr/>+ <xdr:txBody><a:bodyPr/><a:p><a:r><a:t>deep note</a:t></a:r></a:p></xdr:txBody></xdr:sp>+ <xdr:clientData/></xdr:oneCellAnchor>"#;+ let parts = shape_drawing_parts(anchor);+ let extra: Vec<(&str, &str)> = parts+ .iter()+ .map(|(a, b)| (a.as_str(), b.as_str()))+ .collect();+ let wb = workbook_from(&sheet, &extra);+ let nx = workbook_to_pages(&wb, EmitOptions::default());+ assert!(nx.pages.len() > 1, "60 rows at 20pt must paginate");+ let with_item: Vec<usize> = nx+ .pages+ .iter()+ .enumerate()+ .filter(|(_, p)| p.text_items.iter().any(|t| t.text == "deep note"))+ .map(|(i, _)| i)+ .collect();+ let with_block: Vec<usize> = nx+ .page_blocks+ .iter()+ .enumerate()+ .filter(|(_, blocks)| {+ blocks+ .iter()+ .any(|b| matches!(b, Block::Paragraph { text, .. } if text == "deep note"))+ })+ .map(|(i, _)| i)+ .collect();+ assert_eq!(with_item, with_block, "item page and block page agree");+ assert_eq!(with_item.len(), 1);+ assert!(with_item[0] > 0, "row 50 is not on page 1");+ }}