Skip to content

A fast, helpful, and open-source document parser

wip

Logan Markewich96c9aeaf96d3
diff --git a/crates/liteparse/Cargo.toml b/crates/liteparse/Cargo.toml
index 3805cfa..e027747 100644
--- a/crates/liteparse/Cargo.toml
+++ b/crates/liteparse/Cargo.toml
@@ -42,6 +42,10 @@ required-features = ["pptx-native"]
name = "xlsx_emit"
required-features = ["xlsx-native"]
+[[example]]
+name = "xlsx_layout_check"
+required-features = ["xlsx-native"]
+
[features]
default = ["tesseract", "docx-native", "pptx-native", "xlsx-native"]
oar-ocr = ["dep:oar-ocr"]
diff --git a/crates/liteparse/src/office/mod.rs b/crates/liteparse/src/office/mod.rs
index a494a21..ae2d910 100644
--- a/crates/liteparse/src/office/mod.rs
+++ b/crates/liteparse/src/office/mod.rs
@@ -24,12 +24,10 @@ pub(crate) mod inline;
pub mod pptx;
#[cfg(feature = "pptx-native")]
pub mod pptx_layout;
-// Markdown-only for now: `parser.rs` does not route `.xlsx` here yet, because
-// `parse_from_native_blocks` needs real pages and the XLSX geometry pass is a
-// later step — the same order the PPTX path landed in (emitter first, wiring
-// once geometry existed).
#[cfg(feature = "xlsx-native")]
pub mod xlsx;
+#[cfg(feature = "xlsx-native")]
+pub mod xlsx_layout;
/// The input's bytes when it is a `.docx` the native path should try, `None`
/// otherwise. Path inputs are matched on extension only (`.doc`/`.docm`/
@@ -53,6 +51,27 @@ pub(crate) fn docx_bytes(input: &crate::types::PdfInput) -> Option<std::borrow::
}
}
+/// The input's bytes when it is an `.xlsx` the native path should try, `None`
+/// otherwise. Mirrors [`docx_bytes`]: extension-matched for paths (`.xls`,
+/// `.xlsm`, `.xlsb`, `.csv` stay on the conversion path), container-sniffed
+/// for bytes.
+#[cfg(all(feature = "xlsx-native", not(target_arch = "wasm32")))]
+pub(crate) fn xlsx_bytes(input: &crate::types::PdfInput) -> Option<std::borrow::Cow<'_, [u8]>> {
+ use crate::types::PdfInput;
+ match input {
+ PdfInput::Path(p) => {
+ let ext = std::path::Path::new(p).extension()?;
+ if !ext.eq_ignore_ascii_case("xlsx") {
+ return None;
+ }
+ std::fs::read(p).ok().map(std::borrow::Cow::Owned)
+ }
+ PdfInput::Bytes(b) => (crate::conversion::guess_extension_from_data(b).as_deref()
+ == Some("xlsx"))
+ .then_some(std::borrow::Cow::Borrowed(b.as_slice())),
+ }
+}
+
/// The input's bytes when it is a `.pptx` the native path should try, `None`
/// otherwise. Mirrors [`docx_bytes`]: extension-matched for paths (`.ppt`,
/// `.pptm`, `.potx` stay on the conversion path), container-sniffed for bytes.
diff --git a/crates/liteparse/src/office/xlsx.rs b/crates/liteparse/src/office/xlsx.rs
index ed6f429..b9ae7be 100644
--- a/crates/liteparse/src/office/xlsx.rs
+++ b/crates/liteparse/src/office/xlsx.rs
@@ -32,8 +32,14 @@
//! tables, `&<>` for HTML) on the way out. Paragraph text (banners, one-column
//! sheets) is escaped here, before the hyperlink wraps it, so a URL's own
//! underscores survive.
+//!
+//! The grid is planned once per sheet ([`SheetPlan`]) and shared between the
+//! doc-level emitter, the per-page block slices, and the geometry pass
+//! (`xlsx_layout`), so the three can never disagree on which rows and columns
+//! exist. Doc-level emission *is* a single full-range slice.
use std::collections::HashMap;
+use std::ops::Range;
use liteparse_ooxml::xlsx::{self, Cell as GridCell, CellValue, Row, Sheet, Workbook};
@@ -42,7 +48,7 @@ use crate::markdown_layout::{Block, Cell, apply_link, escape_inline};
/// Sheet names become `#` — the workbook's only structural rank, mirroring
/// slide titles on the PPTX path.
-const SHEET_HEADING_LEVEL: u8 = 1;
+pub(crate) const SHEET_HEADING_LEVEL: u8 = 1;
/// A leading merge is a banner when it spans at least this fraction of the
/// sheet's retained width (and at least [`BANNER_MIN_COLS`] columns, so a
@@ -94,7 +100,7 @@ pub fn emit_with_sources(data: &[u8], opts: EmitOptions) -> Result<NativeWorkboo
}
/// Emit an already-read workbook. Split from the bytes entry point so tests
-/// and the future geometry pass can share one parse.
+/// and the geometry pass can share one parse.
pub fn emit_workbook(wb: &Workbook, opts: EmitOptions) -> NativeWorkbook {
let mut blocks = Vec::new();
let mut sheet_names = Vec::new();
@@ -107,7 +113,14 @@ pub fn emit_workbook(wb: &Workbook, opts: EmitOptions) -> NativeWorkbook {
},
si,
));
- emit_sheet(wb, sheet, opts, si, &mut blocks);
+ if let Some(plan) = SheetPlan::build(wb, sheet, opts) {
+ let n = plan.rows.len();
+ for page in plan.page_blocks(wb, &[0..n]) {
+ for b in page {
+ blocks.push((b, si));
+ }
+ }
+ }
}
NativeWorkbook {
blocks,
@@ -118,59 +131,189 @@ pub fn emit_workbook(wb: &Workbook, opts: EmitOptions) -> NativeWorkbook {
/// 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.
-struct MergePlan {
+pub(crate) struct MergePlan {
/// Anchor in sheet coordinates, re-anchored to the first emitted row /
/// retained column inside the merge when the declared corner is empty.
- anchor: (u32, u32),
- colspan: u16,
- rowspan: u16,
+ pub(crate) anchor: (u32, u32),
+ pub(crate) colspan: u16,
/// Clamped sheet-coordinate ranges, for coverage tests.
- col_range: (u32, u32),
- row_range: (u32, u32),
+ pub(crate) col_range: (u32, u32),
+ pub(crate) row_range: (u32, u32),
}
-fn emit_sheet(
- wb: &Workbook,
- sheet: &Sheet,
- opts: EmitOptions,
- si: usize,
- out: &mut Vec<(Block, usize)>,
-) {
- // Rows the file wrote with at least one valued cell, in index order.
- // Cells are sorted within each row too — ascending is what every producer
- // writes, but §18.3.1.4 does not require it.
- let mut rows: Vec<(&Row, Vec<&GridCell>)> = sheet
- .rows
- .iter()
- .filter(|r| !r.cells.is_empty())
- .map(|r| {
- let mut cells: Vec<&GridCell> = r.cells.iter().collect();
- cells.sort_by_key(|c| c.at.col);
- (r, cells)
- })
- .collect();
- rows.sort_by_key(|(r, _)| r.index);
- if rows.is_empty() {
- return;
- }
+/// One sheet's emitted grid, planned once: which rows and columns exist, what
+/// the merges claim, where the banners stop and how many rows are headers.
+/// The markdown emitter, the per-page slicer and the geometry pass all read
+/// this one structure.
+pub(crate) struct SheetPlan<'a> {
+ /// Rows the file wrote with at least one cell, in index order, each with
+ /// its cells sorted by column (ascending is what every producer writes,
+ /// but §18.3.1.4 does not require it). Banner rows included.
+ pub(crate) rows: Vec<(&'a Row, Vec<&'a GridCell>)>,
+ /// Retained columns: every column that holds at least one cell. Wholly
+ /// empty columns inside the bbox carry no text and are compacted out —
+ /// padding 16,384 pipes onto every row of a sheet whose stray formatting
+ /// touched column XFD is the failure this prevents.
+ pub(crate) cols: Vec<u32>,
+ pub(crate) plans: Vec<MergePlan>,
+ pub(crate) anchors: HashMap<(u32, u32), usize>,
+ /// Emitted-row index → merges whose clamped range includes that row.
+ cover: HashMap<u32, Vec<usize>>,
+ /// Text folded into each merge (pass A): every covered cell's text, the
+ /// anchor's own first. A *valued* cell under a neighbour's merge is a
+ /// producer bug Excel never writes — folding keeps its text, because
+ /// "absorbed" must never mean "silently gone".
+ merge_text: Vec<Vec<String>>,
+ /// Plan index of each leading banner row, in row order. `len()` is the
+ /// number of leading rows emitted as paragraphs instead of table rows.
+ banners: Vec<usize>,
+ /// Header rows of the grid (the rows after the banners).
+ header_rows: usize,
+ links: HashMap<(u32, u32), &'a str>,
+}
- let links: HashMap<(u32, u32), &str> = if opts.links {
- sheet
- .hyperlinks
+impl<'a> SheetPlan<'a> {
+ /// `None` when the sheet has no written row with cells — the emitter
+ /// produces only the sheet heading, and the geometry pass one empty page.
+ pub(crate) fn build(wb: &Workbook, sheet: &'a Sheet, opts: EmitOptions) -> Option<Self> {
+ let mut rows: Vec<(&Row, Vec<&GridCell>)> = sheet
+ .rows
.iter()
- .filter_map(|h| {
- h.url
- .as_deref()
- .map(|u| ((h.at.start.row, h.at.start.col), u))
+ .filter(|r| !r.cells.is_empty())
+ .map(|r| {
+ let mut cells: Vec<&GridCell> = r.cells.iter().collect();
+ cells.sort_by_key(|c| c.at.col);
+ (r, cells)
})
- .collect()
- } else {
- HashMap::new()
- };
- // `escape` is true for text bound for a paragraph, false for a table
- // cell (the renderer escapes those per dialect). Escape happens *before*
- // the link wraps the text so the URL half stays raw.
- let cell_text = |cell: &GridCell, escape: bool| -> String {
+ .collect();
+ rows.sort_by_key(|(r, _)| r.index);
+ if rows.is_empty() {
+ return None;
+ }
+
+ let links: HashMap<(u32, u32), &str> = if opts.links {
+ sheet
+ .hyperlinks
+ .iter()
+ .filter_map(|h| {
+ h.url
+ .as_deref()
+ .map(|u| ((h.at.start.row, h.at.start.col), u))
+ })
+ .collect()
+ } else {
+ HashMap::new()
+ };
+
+ let mut cols: Vec<u32> = rows
+ .iter()
+ .flat_map(|(_, cells)| cells.iter().map(|c| c.at.col))
+ .collect();
+ cols.sort_unstable();
+ cols.dedup();
+ let width = cols.len();
+
+ let mut plan = SheetPlan {
+ rows,
+ cols,
+ plans: Vec::new(),
+ anchors: HashMap::new(),
+ cover: HashMap::new(),
+ merge_text: Vec::new(),
+ banners: Vec::new(),
+ header_rows: 0,
+ links,
+ };
+
+ // A one-column sheet is prose with an address, not a table. Merges
+ // cannot move text between rows here — an absorbed cell is value-less
+ // — so the grid machinery below is skipped rather than degenerated.
+ if width == 1 {
+ return Some(plan);
+ }
+
+ let row_indices: Vec<u32> = plan.rows.iter().map(|(r, _)| r.index).collect();
+ let max_row = *row_indices.last().unwrap();
+ let max_col = *plan.cols.last().unwrap();
+
+ // Plan the merges against the emitted grid. First-wins on a shared
+ // anchor: overlapping merges are invalid and Excel repairs them on
+ // open.
+ for m in &sheet.merges {
+ let Some((p, _rowspan)) = plan_merge(m, &row_indices, &plan.cols, max_row, max_col)
+ else {
+ continue;
+ };
+ if plan.anchors.contains_key(&p.anchor) {
+ continue;
+ }
+ let idx = plan.plans.len();
+ plan.anchors.insert(p.anchor, idx);
+ let lo = row_indices.partition_point(|&r| r < p.row_range.0);
+ let hi = row_indices.partition_point(|&r| r <= p.row_range.1);
+ for &ri in &row_indices[lo..hi] {
+ plan.cover.entry(ri).or_default().push(idx);
+ }
+ plan.plans.push(p);
+ }
+
+ // Pass A: fold the text of every cell a merge covers into that merge.
+ // The anchor's own cell lands first (its row precedes the others).
+ plan.merge_text = vec![Vec::new(); plan.plans.len()];
+ for (row, cells) in &plan.rows {
+ if !plan.cover.contains_key(&row.index) {
+ continue;
+ }
+ for cell in cells {
+ if let Some(idx) = plan.covering_plan(row.index, cell.at.col) {
+ let text = plan.cell_text(wb, cell, false);
+ if !text.is_empty() {
+ plan.merge_text[idx].push(text);
+ }
+ }
+ }
+ }
+
+ // Banners: leading rows whose every valued cell sits inside one
+ // shallow near-full-width merge anchored on that row. 42.1% of sheets
+ // open with one; it reads as a title and is emitted as one.
+ let min_banner_cols =
+ BANNER_MIN_COLS.max((width as f64 * BANNER_MIN_WIDTH_FRACTION) as usize);
+ for (row, cells) in &plan.rows {
+ let Some(idx) = banner_plan(
+ row,
+ cells,
+ &plan.plans,
+ &plan.anchors,
+ &row_indices,
+ min_banner_cols,
+ ) else {
+ break;
+ };
+ plan.banners.push(idx);
+ }
+
+ plan.header_rows = header_rows(wb, sheet, &plan.rows[plan.banners.len()..]);
+ Some(plan)
+ }
+
+ /// One retained column means paragraphs, never a one-pipe table.
+ pub(crate) fn is_prose(&self) -> bool {
+ self.cols.len() == 1
+ }
+
+ fn covering_plan(&self, row: u32, col: u32) -> Option<usize> {
+ self.cover.get(&row)?.iter().copied().find(|&i| {
+ let (lo, hi) = self.plans[i].col_range;
+ col >= lo && col <= hi
+ })
+ }
+
+ /// A cell's rendered text. `escape` is true for text bound for a
+ /// paragraph, false for a table cell (the renderer escapes those per
+ /// dialect). Escape happens *before* the link wraps the text so the URL
+ /// half stays raw.
+ pub(crate) fn cell_text(&self, wb: &Workbook, cell: &GridCell, escape: bool) -> String {
// `_(* #,##0_)` pads for column alignment; markdown aligns itself, so
// the padding is trimmed here and only here (the reader keeps it, per
// the numfmt step's contract).
@@ -181,201 +324,194 @@ fn emit_sheet(
} else {
raw.to_string()
};
- match links.get(&(cell.at.row, cell.at.col)) {
+ match self.links.get(&(cell.at.row, cell.at.col)) {
Some(url) => apply_link(if text.is_empty() { url } else { &text }, url),
None => text,
}
- };
+ }
- // Retained columns: every column that holds at least one cell. Wholly
- // empty columns inside the bbox carry no text and are compacted out —
- // padding 16,384 pipes onto every row of a sheet whose stray formatting
- // touched column XFD is the failure this prevents.
- let mut cols: Vec<u32> = rows
- .iter()
- .flat_map(|(_, cells)| cells.iter().map(|c| c.at.col))
- .collect();
- cols.sort_unstable();
- cols.dedup();
- let width = cols.len();
-
- // A one-column sheet is prose with an address, not a table. Merges cannot
- // move text between rows here — an absorbed cell is value-less — so the
- // grid machinery below is skipped rather than degenerated.
- if width == 1 {
- for (_, cells) in &rows {
- let cell = cells[0];
- let text = cell_text(cell, true);
- if text.is_empty() {
- continue;
- }
- let font = wb.styles.font(cell.style);
- out.push((
- Block::Paragraph {
- text,
- bold: font.bold,
- italic: font.italic,
- },
- si,
- ));
- }
- return;
- }
-
- let row_indices: Vec<u32> = rows.iter().map(|(r, _)| r.index).collect();
- let max_row = *row_indices.last().unwrap();
- let max_col = *cols.last().unwrap();
-
- // Plan the merges against the emitted grid. First-wins on a shared
- // anchor: overlapping merges are invalid and Excel repairs them on open.
- let mut plans: Vec<MergePlan> = Vec::new();
- let mut anchors: HashMap<(u32, u32), usize> = HashMap::new();
- // Emitted-row index → merges whose clamped range includes that row.
- let mut cover: HashMap<u32, Vec<usize>> = HashMap::new();
- for m in &sheet.merges {
- let Some(plan) = plan_merge(m, &row_indices, &cols, max_row, max_col) else {
- continue;
- };
- if anchors.contains_key(&plan.anchor) {
- continue;
- }
- let idx = plans.len();
- anchors.insert(plan.anchor, idx);
- let lo = row_indices.partition_point(|&r| r < plan.row_range.0);
- let hi = row_indices.partition_point(|&r| r <= plan.row_range.1);
- for &ri in &row_indices[lo..hi] {
- cover.entry(ri).or_default().push(idx);
- }
- plans.push(plan);
+ /// The external hyperlink anchored on this cell, if links are enabled.
+ pub(crate) fn link_at(&self, row: u32, col: u32) -> Option<&str> {
+ self.links.get(&(row, col)).copied()
}
- let covering_plan = |row: u32, col: u32| -> Option<usize> {
- cover.get(&row)?.iter().copied().find(|&i| {
- let (lo, hi) = plans[i].col_range;
- col >= lo && col <= hi
- })
- };
- // Pass A: fold the text of every cell a merge covers into that merge. The
- // anchor's own cell lands first (its row precedes the others). A *valued*
- // cell under a neighbour's merge is a producer bug Excel never writes —
- // folding keeps its text, because "absorbed" must never mean "silently
- // gone".
- let mut merge_text: Vec<Vec<String>> = vec![Vec::new(); plans.len()];
- for (row, cells) in &rows {
- if !cover.contains_key(&row.index) {
- continue;
- }
- for cell in cells {
- if let Some(idx) = covering_plan(row.index, cell.at.col) {
- let text = cell_text(cell, false);
- if !text.is_empty() {
- merge_text[idx].push(text);
- }
- }
- }
+ fn merge_string(&self, idx: usize) -> String {
+ self.merge_text[idx].join(" ")
}
- let merge_string = |idx: usize| merge_text[idx].join(" ");
- // Banners: leading rows whose every valued cell sits inside one shallow
- // near-full-width merge anchored on that row. 42.1% of sheets open with
- // one; it reads as a title and is emitted as one.
- let min_banner_cols = BANNER_MIN_COLS.max((width as f64 * BANNER_MIN_WIDTH_FRACTION) as usize);
- let mut banner_rows = 0usize;
- for (row, cells) in &rows {
- let Some(idx) = banner_plan(row, cells, &plans, &anchors, min_banner_cols) else {
- break;
- };
+ /// The banner paragraph for banner row `i`, or `None` when its text is
+ /// empty (the row is still consumed — it does not fall back into the
+ /// table).
+ fn banner_paragraph(&self, wb: &Workbook, i: usize) -> Option<Block> {
+ let (_, cells) = &self.rows[i];
+ let idx = self.banners[i];
// A banner merge is one row tall, so its text is this row's cells —
// re-rendered escaped, since it is bound for a paragraph.
- let (lo, hi) = plans[idx].col_range;
+ let (lo, hi) = self.plans[idx].col_range;
let text = cells
.iter()
.filter(|c| c.at.col >= lo && c.at.col <= hi)
- .map(|c| cell_text(c, true))
+ .map(|c| self.cell_text(wb, c, true))
.filter(|t| !t.is_empty())
.collect::<Vec<_>>()
.join(" ");
- if !text.is_empty() {
- let font = cells
- .iter()
- .find(|c| (c.at.row, c.at.col) == plans[idx].anchor)
- .map(|c| wb.styles.font(c.style));
- out.push((
- Block::Paragraph {
- text,
- bold: font.as_ref().is_some_and(|f| f.bold),
- italic: font.as_ref().is_some_and(|f| f.italic),
- },
- si,
- ));
+ if text.is_empty() {
+ return None;
}
- banner_rows += 1;
+ let font = cells
+ .iter()
+ .find(|c| (c.at.row, c.at.col) == self.plans[idx].anchor)
+ .map(|c| wb.styles.font(c.style));
+ Some(Block::Paragraph {
+ text,
+ bold: font.as_ref().is_some_and(|f| f.bold),
+ italic: font.as_ref().is_some_and(|f| f.italic),
+ })
}
- let grid = &rows[banner_rows..];
- if grid.is_empty() {
- return;
- }
-
- // Pass B: assemble the table, walking each row's sorted cells in step
- // with the retained columns.
- let mut table: Vec<Vec<Cell>> = Vec::with_capacity(grid.len());
- for (row, cells) in grid {
- let mut cells_out: Vec<Cell> = Vec::with_capacity(width);
- let mut it = cells.iter().copied().peekable();
- for &col in &cols {
- // Duplicate cells at one address are malformed; first wins, the
- // rest are skipped by the cursor.
- let mut at_col: Option<&GridCell> = None;
- while let Some(&c) = it.peek() {
- if c.at.col < col {
- it.next();
- } else {
- if c.at.col == col {
- at_col = Some(c);
+ /// A prose row's paragraph (one-column sheets), or `None` when empty.
+ fn prose_paragraph(&self, wb: &Workbook, i: usize) -> Option<Block> {
+ let (_, cells) = &self.rows[i];
+ let cell = cells[0];
+ let text = self.cell_text(wb, cell, true);
+ if text.is_empty() {
+ return None;
+ }
+ let font = wb.styles.font(cell.style);
+ Some(Block::Paragraph {
+ text,
+ bold: font.bold,
+ italic: font.italic,
+ })
+ }
+
+ /// Assemble the table rows for a slice of the grid (indices into the rows
+ /// *after* the banners), walking each row's sorted cells in step with the
+ /// retained columns.
+ ///
+ /// Spans are counted within the slice: a merge cut by a page break keeps
+ /// its text in the first fragment, and the continuation emits an empty
+ /// spanning cell at the merge's column so the rows below it do not shift
+ /// left. For the full grid range this reproduces doc-level emission.
+ fn table_slice(&self, wb: &Workbook, slice: Range<usize>) -> Vec<Vec<Cell>> {
+ let grid = &self.rows[self.banners.len()..];
+ let rows = &grid[slice];
+ let Some((first, _)) = rows.first() else {
+ return Vec::new();
+ };
+ let slice_lo = first.index;
+ // Rowspan in emitted units *within this slice* — never claim rows on
+ // another page.
+ let span_rows = |p: &MergePlan| -> u16 {
+ rows.iter()
+ .filter(|(r, _)| r.index >= p.row_range.0 && r.index <= p.row_range.1)
+ .count()
+ .min(u16::MAX as usize) as u16
+ };
+
+ let width = self.cols.len();
+ let mut table: Vec<Vec<Cell>> = Vec::with_capacity(rows.len());
+ for (row, cells) in rows {
+ let mut cells_out: Vec<Cell> = Vec::with_capacity(width);
+ let mut it = cells.iter().copied().peekable();
+ for &col in &self.cols {
+ // Duplicate cells at one address are malformed; first wins,
+ // the rest are skipped by the cursor.
+ let mut at_col: Option<&GridCell> = None;
+ while let Some(&c) = it.peek() {
+ if c.at.col < col {
it.next();
+ } else {
+ if c.at.col == col {
+ at_col = Some(c);
+ it.next();
+ }
+ break;
}
- break;
+ }
+ let pos = (row.index, col);
+ if let Some(&idx) = self.anchors.get(&pos) {
+ let plan = &self.plans[idx];
+ cells_out.push(Cell::spanning(
+ self.merge_string(idx),
+ plan.colspan,
+ span_rows(plan),
+ ));
+ } else if let Some(idx) = self.covering_plan(pos.0, pos.1) {
+ let plan = &self.plans[idx];
+ if plan.anchor.0 < slice_lo && row.index == slice_lo && col == plan.anchor.1 {
+ // The rest of a merge whose anchor is on an earlier
+ // page. Its text already rendered there.
+ cells_out.push(Cell::spanning(
+ String::new(),
+ plan.colspan,
+ span_rows(plan),
+ ));
+ }
+ // Otherwise absorbed by a span; any text already folded
+ // into the anchor in pass A.
+ } else {
+ let text = at_col
+ .map(|c| self.cell_text(wb, c, false))
+ .unwrap_or_default();
+ cells_out.push(Cell::new(text));
}
}
- let pos = (row.index, col);
- if let Some(&idx) = anchors.get(&pos) {
- let plan = &plans[idx];
- cells_out.push(Cell::spanning(
- merge_string(idx),
- plan.colspan,
- plan.rowspan,
- ));
- } else if covering_plan(pos.0, pos.1).is_some() {
- // Absorbed by a span; any text already folded in pass A.
- } else {
- let text = at_col.map(|c| cell_text(c, false)).unwrap_or_default();
- cells_out.push(Cell::new(text));
- }
+ table.push(cells_out);
}
- table.push(cells_out);
+ table
}
- let header_rows = header_rows(wb, sheet, grid);
- out.push((
- Block::MergedTable {
- rows: table,
- header_rows,
- },
- si,
- ));
+ /// The block stream for each range of emitted rows (banner rows counted).
+ /// One range spanning all rows yields the doc-level emission; the
+ /// geometry pass hands one range per page so per-page markdown holds the
+ /// rows that are actually on that page. `header_rows` marks only the
+ /// slice that contains the grid's first row.
+ pub(crate) fn page_blocks(&self, wb: &Workbook, ranges: &[Range<usize>]) -> Vec<Vec<Block>> {
+ let nb = self.banners.len();
+ ranges
+ .iter()
+ .map(|range| {
+ let mut out = Vec::new();
+ if self.is_prose() {
+ for i in range.clone() {
+ if let Some(p) = self.prose_paragraph(wb, i) {
+ out.push(p);
+ }
+ }
+ return out;
+ }
+ for i in range.start..range.end.min(nb) {
+ if let Some(p) = self.banner_paragraph(wb, i) {
+ out.push(p);
+ }
+ }
+ let g0 = range.start.saturating_sub(nb);
+ let g1 = range.end.saturating_sub(nb);
+ if g1 > g0 {
+ let rows = self.table_slice(wb, g0..g1);
+ out.push(Block::MergedTable {
+ rows,
+ header_rows: if g0 == 0 { self.header_rows } else { 0 },
+ });
+ }
+ out
+ })
+ .collect()
+ }
}
/// Clamp a merge to the emitted grid and count its spans in emitted units.
/// `None` means the merge touches no emitted row or retained column and
-/// renders as nothing.
+/// renders as nothing. The rowspan is returned for the census-facing tests;
+/// emission recounts spans per slice.
fn plan_merge(
m: &xlsx::RangeRef,
row_indices: &[u32],
cols: &[u32],
max_row: u32,
max_col: u32,
-) -> Option<MergePlan> {
+) -> Option<(MergePlan, u16)> {
if m.start.row > max_row || m.start.col > max_col {
return None;
}
@@ -388,16 +524,18 @@ fn plan_merge(
if col_lo == col_hi || row_lo == row_hi {
return None;
}
- Some(MergePlan {
- // Re-anchoring moves the visual corner to the first position the
- // emitted grid still has, so a merge whose declared corner sits on a
- // skipped-empty row keeps claiming its cells.
- anchor: (row_indices[row_lo], cols[col_lo]),
- colspan: (col_hi - col_lo).min(u16::MAX as usize) as u16,
- rowspan: (row_hi - row_lo).min(u16::MAX as usize) as u16,
- col_range: (m.start.col, end_col),
- row_range: (m.start.row, end_row),
- })
+ Some((
+ MergePlan {
+ // Re-anchoring moves the visual corner to the first position the
+ // emitted grid still has, so a merge whose declared corner sits
+ // on a skipped-empty row keeps claiming its cells.
+ anchor: (row_indices[row_lo], cols[col_lo]),
+ colspan: (col_hi - col_lo).min(u16::MAX as usize) as u16,
+ col_range: (m.start.col, end_col),
+ row_range: (m.start.row, end_row),
+ },
+ (row_hi - row_lo).min(u16::MAX as usize) as u16,
+ ))
}
/// The banner test: some merge is anchored on this row, one emitted row tall
@@ -408,12 +546,18 @@ fn banner_plan(
cells: &[&GridCell],
plans: &[MergePlan],
anchors: &HashMap<(u32, u32), usize>,
+ row_indices: &[u32],
min_cols: usize,
) -> Option<usize> {
+ let one_row_tall = |p: &MergePlan| {
+ let lo = row_indices.partition_point(|&r| r < p.row_range.0);
+ let hi = row_indices.partition_point(|&r| r <= p.row_range.1);
+ hi - lo == 1
+ };
let idx = cells.iter().find_map(|c| {
let &idx = anchors.get(&(row.index, c.at.col))?;
let plan = &plans[idx];
- (plan.rowspan == 1 && (plan.colspan as usize) >= min_cols).then_some(idx)
+ (one_row_tall(plan) && (plan.colspan as usize) >= min_cols).then_some(idx)
})?;
let (lo, hi) = plans[idx].col_range;
cells
@@ -432,6 +576,9 @@ fn banner_plan(
/// 3. First grid row entirely bold with ≥2 cells (12.7%).
/// 4. Type transition: first grid row has no numbers, second does (14.3%).
fn header_rows(wb: &Workbook, sheet: &Sheet, grid: &[(&Row, Vec<&GridCell>)]) -> usize {
+ if grid.is_empty() {
+ return 0;
+ }
let frozen = grid
.iter()
.take_while(|(r, _)| r.index < sheet.frozen_rows)
@@ -465,14 +612,14 @@ fn header_rows(wb: &Workbook, sheet: &Sheet, grid: &[(&Row, Vec<&GridCell>)]) ->
}
#[cfg(test)]
-mod tests {
+pub(crate) mod tests {
use super::*;
use std::io::Write as _;
/// Build a one-sheet workbook from a worksheet XML body (and optional
/// extra parts), the same in-memory container trick as the reader's own
/// tests — stating exactly which parts each rule is being tested against.
- fn workbook_from(sheet_xml: &str, extra: &[(&str, &str)]) -> Workbook {
+ pub(crate) fn workbook_from(sheet_xml: &str, extra: &[(&str, &str)]) -> Workbook {
let mut parts: Vec<(&str, &str)> = vec![
("[Content_Types].xml", "<Types/>"),
(
@@ -794,4 +941,80 @@ mod tests {
assert_eq!(blocks.len(), 1);
assert!(matches!(&blocks[0], Block::Heading { .. }));
}
+
+ /// Page slicing: a rowspan cut by a page break keeps its text on the
+ /// first page, and the continuation emits an empty spanning cell so the
+ /// columns below do not shift left.
+ #[test]
+ fn a_page_break_splits_a_rowspan_without_shifting_columns() {
+ let wb = workbook_from(
+ r#"<worksheet><sheetData>
+ <row r="1"><c r="A1" t="inlineStr"><is><t>tall</t></is></c><c r="B1"><v>1</v></c></row>
+ <row r="2"><c r="B2"><v>2</v></c></row>
+ <row r="3"><c r="B3"><v>3</v></c></row>
+ <row r="4"><c r="A4" t="inlineStr"><is><t>d</t></is></c><c r="B4"><v>4</v></c></row>
+ </sheetData><mergeCells><mergeCell ref="A1:A3"/></mergeCells></worksheet>"#,
+ &[],
+ );
+ let plan = SheetPlan::build(&wb, &wb.sheets[0], EmitOptions::default()).unwrap();
+ let pages = plan.page_blocks(&wb, &[0..2, 2..4]);
+ let table = |page: &[Block]| match &page[0] {
+ Block::MergedTable { rows, .. } => rows.clone(),
+ b => panic!("expected table, got {b:?}"),
+ };
+ let first = table(&pages[0]);
+ assert_eq!(first[0][0], Cell::spanning("tall", 1, 2), "clamped to page");
+ let second = table(&pages[1]);
+ // Continuation: empty spanning cell holds the merge's column.
+ assert_eq!(second[0], vec![Cell::spanning("", 1, 1), Cell::new("3")]);
+ assert_eq!(second[1], vec![Cell::new("d"), Cell::new("4")]);
+ }
+
+ /// The full-range slice must reproduce doc-level emission exactly — the
+ /// doc path *is* this call.
+ #[test]
+ fn one_full_range_reproduces_the_doc_table() {
+ let wb = workbook_from(
+ r#"<worksheet><sheetData>
+ <row r="1"><c r="A1" t="inlineStr"><is><t>t</t></is></c></row>
+ <row r="2"><c r="A2" t="inlineStr"><is><t>h1</t></is></c><c r="B2" t="inlineStr"><is><t>h2</t></is></c><c r="C2" t="inlineStr"><is><t>h3</t></is></c></row>
+ <row r="3"><c r="A3"><v>1</v></c><c r="B3"><v>2</v></c><c r="C3"><v>3</v></c></row>
+ </sheetData><mergeCells><mergeCell ref="A1:C1"/></mergeCells></worksheet>"#,
+ &[],
+ );
+ let doc: Vec<Block> = emit_workbook(&wb, EmitOptions::default())
+ .blocks
+ .into_iter()
+ .map(|(b, _)| b)
+ .collect();
+ let plan = SheetPlan::build(&wb, &wb.sheets[0], EmitOptions::default()).unwrap();
+ let pages = plan.page_blocks(&wb, &[0..plan.rows.len()]);
+ assert_eq!(
+ format!("{:?}", &doc[1..]),
+ format!("{:?}", &pages[0][..]),
+ "doc emission is the full slice"
+ );
+ }
+
+ /// Header rows mark only the slice containing the grid's first row —
+ /// a continuation page's table has no header.
+ #[test]
+ fn header_rows_do_not_repeat_on_continuation_pages() {
+ let wb = workbook_from(
+ r#"<worksheet><sheetData>
+ <row r="1"><c r="A1" t="inlineStr"><is><t>name</t></is></c><c r="B1" t="inlineStr"><is><t>score</t></is></c></row>
+ <row r="2"><c r="A2" t="inlineStr"><is><t>ann</t></is></c><c r="B2"><v>4</v></c></row>
+ <row r="3"><c r="A3" t="inlineStr"><is><t>bob</t></is></c><c r="B3"><v>5</v></c></row>
+ </sheetData></worksheet>"#,
+ &[],
+ );
+ let plan = SheetPlan::build(&wb, &wb.sheets[0], EmitOptions::default()).unwrap();
+ let pages = plan.page_blocks(&wb, &[0..2, 2..3]);
+ let header = |page: &[Block]| match &page[0] {
+ Block::MergedTable { header_rows, .. } => *header_rows,
+ b => panic!("expected table, got {b:?}"),
+ };
+ assert_eq!(header(&pages[0]), 1);
+ assert_eq!(header(&pages[1]), 0);
+ }
}
diff --git a/crates/liteparse/src/office/xlsx_layout.rs b/crates/liteparse/src/office/xlsx_layout.rs
new file mode 100644
index 0000000..17bbd82
--- /dev/null
+++ b/crates/liteparse/src/office/xlsx_layout.rs
@@ -0,0 +1,516 @@
+//! XLSX geometry: the planned grid → [`Page`]s with per-cell [`TextItem`]s.
+//!
+//! Unlike DOCX (a layout engine) and PPTX (a cascade), XLSX geometry is
+//! *stated*: the file declares every column width and row height, so this
+//! pass is unit conversion plus pagination — no fonts, no measurement, no
+//! host dependence. Every constant traces to `xlsx_geometry_census` over the
+//! 1,248-workbook corpus:
+//!
+//! * **Packed, not canvas.** Item positions honour the emitted grid (rows
+//! the file wrote, columns holding at least one cell), the same sparse
+//! rule as the emitter. Honouring declared positions instead puts one
+//! sheet's canvas at 16,777,274 pt tall (a stray cell on row 1,048,576);
+//! packed and canvas differ by >2× on only 256 of 5,606 sheets.
+//! * **Pages are Letter-height, content-width.** At 792 pt, 49% of sheets
+//! paginate (a sheet is not a page: the corpus median workbook is 4 pages,
+//! the largest 6,612) — but 67.5% of sheets are *wider* than Letter, so
+//! width is never split: the page takes the content's width and a
+//! downstream consumer sees an unclipped row, which is the native path's
+//! whole advantage over LibreOffice.
+//! * **Geometry inputs are declared, not guessed**: 100% of sheets state
+//! `defaultRowHeight`, 97% of retained columns have a declared width. The
+//! 8.43-unit / 15 pt fallbacks carry the remainder.
+//! * **A merge cut by a page break** (1.05% of merges) keeps its item on the
+//! anchor's page, clamped to that page's rows — mirroring the block
+//! slicer, where the continuation is an empty spanning cell.
+//!
+//! An item's rectangle is the **cell box** (inset by the cell padding the
+//! pixel formula bakes in), not a measured text box: the file states no
+//! glyph positions, and the box the grid declares is what a bbox consumer
+//! (extraction join, highlight) wants. The inset also keeps adjacent cells
+//! from touching, which is what `native_page_text` uses to put spaces
+//! between cells on a line.
+
+use std::ops::Range;
+
+use liteparse_ooxml::xlsx::{Sheet, Workbook};
+
+use super::xlsx::{EmitOptions, SHEET_HEADING_LEVEL, SheetPlan};
+use crate::markdown_layout::{Block, escape_inline};
+use crate::types::{OutlineTarget, Page, Rect, TextItem};
+
+/// The write-side constants from rust_xlsxwriter: Calibri 11's max digit
+/// width is 7 px at 96 DPI, plus 5 px of cell padding.
+const MDW: f64 = 7.0;
+const PAD_PX: f64 = 5.0;
+const PX_TO_PT: f64 = 72.0 / 96.0;
+/// Excel's fallbacks when the file declares nothing (3% of retained columns,
+/// 0% of corpus sheets for row height).
+const DEFAULT_COL_WIDTH: f64 = 8.43;
+const DEFAULT_ROW_HEIGHT_PT: f64 = 15.0;
+
+/// US Letter portrait height; width is content-driven and never split.
+const PAGE_HEIGHT: f32 = 792.0;
+const MARGIN: f32 = 36.0;
+/// Rows chunk greedily into this much vertical space per page.
+const USABLE_HEIGHT: f64 = (PAGE_HEIGHT - 2.0 * MARGIN) as f64;
+/// Pages narrower than Letter portrait are padded up to it — a two-column
+/// sheet still gets a page-shaped page.
+const MIN_PAGE_WIDTH: f32 = 612.0;
+/// Horizontal inset of an item inside its cell: half the cell padding the
+/// pixel formula already contains, in points.
+const TEXT_INSET: f32 = (PAD_PX / 2.0 * PX_TO_PT) as f32;
+
+/// Everything the native XLSX pipeline hands `parser.rs`.
+pub struct NativeXlsx {
+ /// One or more pages per sheet (every sheet gets at least one, so a page
+ /// exists for each heading), page numbers 1-based across the workbook.
+ pub pages: Vec<Page>,
+ /// Blocks per page, aligned with `pages`: the sheet heading and banners
+ /// on the sheet's first page, then the table rows that are actually on
+ /// each page (a merge cut by the break leaves an empty spanning cell on
+ /// the continuation).
+ pub page_blocks: Vec<Vec<Block>>,
+ /// Doc-level blocks: each sheet's table *unsplit*, the shape the
+ /// markdown deliverable wants.
+ pub all_blocks: Vec<Block>,
+ /// One level-1 entry per sheet at its first page, matching the emitter's
+ /// heading rank.
+ pub outline: Vec<OutlineTarget>,
+}
+
+/// Excel width units → pixels, the xlsxwriter algorithm: sub-unit widths
+/// scale the padded unit instead of adding padding.
+fn col_px(w: f64) -> f64 {
+ if w < 1.0 {
+ (w * (MDW + PAD_PX) + 0.5).floor()
+ } else {
+ (w * MDW + 0.5).floor() + PAD_PX
+ }
+}
+
+/// Lay out a whole workbook. Infallible past a successful `xlsx::read`, like
+/// the emitter: a sheet with no content still yields one empty page.
+pub fn workbook_to_pages(wb: &Workbook, opts: EmitOptions) -> NativeXlsx {
+ let mut out = NativeXlsx {
+ pages: Vec::new(),
+ page_blocks: Vec::new(),
+ all_blocks: Vec::new(),
+ outline: Vec::new(),
+ };
+
+ for sheet in &wb.sheets {
+ let heading = Block::Heading {
+ level: SHEET_HEADING_LEVEL,
+ text: escape_inline(&sheet.name),
+ };
+ out.outline.push(OutlineTarget {
+ level: SHEET_HEADING_LEVEL,
+ title: escape_inline(&sheet.name),
+ page_index: out.pages.len() as i32,
+ y_pdf: None,
+ });
+ out.all_blocks.push(heading.clone());
+
+ let Some(plan) = SheetPlan::build(wb, sheet, opts) else {
+ out.pages.push(empty_page(out.pages.len() + 1));
+ out.page_blocks.push(vec![heading]);
+ continue;
+ };
+
+ // Doc-level: the unsplit emission (one full-range slice).
+ let n = plan.rows.len();
+ for page in plan.page_blocks(wb, &[0..n]) {
+ out.all_blocks.extend(page);
+ }
+
+ let geo = SheetGeometry::build(sheet, &plan);
+ let ranges = geo.page_ranges();
+ let mut blocks_per_page = plan.page_blocks(wb, &ranges);
+ blocks_per_page[0].insert(0, heading);
+
+ for (range, blocks) in ranges.into_iter().zip(blocks_per_page) {
+ let page = geo.build_page(wb, &plan, range, out.pages.len() + 1);
+ out.pages.push(page);
+ out.page_blocks.push(blocks);
+ }
+ }
+ out
+}
+
+fn empty_page(page_number: usize) -> Page {
+ Page {
+ page_number,
+ page_width: MIN_PAGE_WIDTH,
+ page_height: PAGE_HEIGHT,
+ content_bounds: None,
+ text_items: Vec::new(),
+ graphics: Vec::new(),
+ vector_graphics: None,
+ struct_nodes: Vec::new(),
+ image_refs: Vec::new(),
+ annotations: None,
+ form_fields: None,
+ structure_tree: None,
+ }
+}
+
+/// The resolved sizes of one sheet's emitted grid: prefix sums so a rect is
+/// two subtractions, in points throughout.
+struct SheetGeometry {
+ /// `x_off[i]` = left edge of retained column `i` relative to the grid
+ /// origin; `x_off[cols.len()]` = grid width.
+ x_off: Vec<f64>,
+ /// `y_off[i]` = top edge of emitted row `i` from the grid top;
+ /// `y_off[rows.len()]` = grid height.
+ y_off: Vec<f64>,
+}
+
+impl SheetGeometry {
+ fn build(sheet: &Sheet, plan: &SheetPlan<'_>) -> Self {
+ let default_h = sheet.default_row_height.unwrap_or(DEFAULT_ROW_HEIGHT_PT);
+ let mut x_off = Vec::with_capacity(plan.cols.len() + 1);
+ let mut acc = 0.0;
+ x_off.push(0.0);
+ for &c in &plan.cols {
+ let w = sheet.col_width(c).unwrap_or(DEFAULT_COL_WIDTH).max(0.0);
+ acc += col_px(w) * PX_TO_PT;
+ x_off.push(acc);
+ }
+ let mut y_off = Vec::with_capacity(plan.rows.len() + 1);
+ let mut acc = 0.0;
+ y_off.push(0.0);
+ for (row, _) in &plan.rows {
+ // Hidden rows keep their declared height: their text is emitted
+ // (the deliberate divergence from what Excel prints), so their
+ // geometry must be somewhere real too.
+ acc += row.height.unwrap_or(default_h).max(0.0);
+ y_off.push(acc);
+ }
+ SheetGeometry { x_off, y_off }
+ }
+
+ fn row_height(&self, i: usize) -> f64 {
+ self.y_off[i + 1] - self.y_off[i]
+ }
+
+ /// Greedy pagination over emitted rows: break before the row that would
+ /// overflow the page, never breaking an empty page (a row taller than
+ /// the page — the corpus max is 410 pt — gets a page to itself).
+ fn page_ranges(&self) -> Vec<Range<usize>> {
+ let n = self.y_off.len() - 1;
+ let mut ranges = Vec::new();
+ let mut start = 0;
+ let mut acc = 0.0;
+ for i in 0..n {
+ let h = self.row_height(i);
+ if acc > 0.0 && acc + h > USABLE_HEIGHT {
+ ranges.push(start..i);
+ start = i;
+ acc = 0.0;
+ }
+ acc += h;
+ }
+ ranges.push(start..n);
+ ranges
+ }
+
+ /// One page: the rows of `range`, each non-empty cell an item at its
+ /// cell box, merge anchors at the merge's box clamped to this page.
+ fn build_page(
+ &self,
+ wb: &Workbook,
+ plan: &SheetPlan<'_>,
+ range: Range<usize>,
+ page_number: usize,
+ ) -> Page {
+ let grid_w = *self.x_off.last().unwrap();
+ let page_width = (grid_w as f32 + 2.0 * MARGIN).max(MIN_PAGE_WIDTH);
+ let y_base = self.y_off[range.start];
+
+ let mut text_items: Vec<TextItem> = Vec::new();
+ for i in range.clone() {
+ let (row, cells) = &plan.rows[i];
+ let y = MARGIN + (self.y_off[i] - y_base) as f32;
+ let row_h = self.row_height(i) as f32;
+ for cell in cells {
+ let text = wb.display_text(cell).unwrap_or_default();
+ let text = text.trim();
+ if text.is_empty() {
+ continue;
+ }
+ let ci = plan.cols.binary_search(&cell.at.col).unwrap_or_else(|_| {
+ unreachable!("retained columns are the union of all cells")
+ });
+ let (x, w, h) = match plan.anchors.get(&(row.index, cell.at.col)) {
+ Some(&idx) => {
+ let m = &plan.plans[idx];
+ // Column extent of the merge over retained columns.
+ let clo = plan.cols.partition_point(|&c| c < m.col_range.0);
+ let chi = plan.cols.partition_point(|&c| c <= m.col_range.1);
+ // Row extent, clamped to this page's rows: the block
+ // slicer clamps the rowspan the same way.
+ let mut j = i;
+ while j + 1 < range.end && plan.rows[j + 1].0.index <= m.row_range.1 {
+ j += 1;
+ }
+ (
+ self.x_off[clo],
+ self.x_off[chi] - self.x_off[clo],
+ (self.y_off[j + 1] - self.y_off[i]) as f32,
+ )
+ }
+ None => (self.x_off[ci], self.x_off[ci + 1] - self.x_off[ci], row_h),
+ };
+ let font = wb.styles.font(cell.style);
+ text_items.push(TextItem {
+ text: text.to_string(),
+ x: MARGIN + x as f32 + TEXT_INSET,
+ y,
+ width: (w as f32 - 2.0 * TEXT_INSET).max(0.0),
+ height: h,
+ font_name: font.name.clone(),
+ font_size: font.size,
+ font_weight: font.bold.then_some(700),
+ link: plan.link_at(row.index, cell.at.col).map(str::to_string),
+ ..Default::default()
+ });
+ }
+ }
+
+ let content_bounds = 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,
+ });
+
+ Page {
+ page_number,
+ page_width,
+ page_height: PAGE_HEIGHT,
+ content_bounds,
+ text_items,
+ graphics: Vec::new(),
+ vector_graphics: None,
+ struct_nodes: Vec::new(),
+ image_refs: Vec::new(),
+ annotations: None,
+ form_fields: None,
+ structure_tree: None,
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::office::xlsx::tests::workbook_from;
+
+ fn layout_of(sheet_xml: &str, extra: &[(&str, &str)]) -> NativeXlsx {
+ let wb = workbook_from(sheet_xml, extra);
+ workbook_to_pages(&wb, EmitOptions::default())
+ }
+
+ /// w=10 → px = round(10×7)+5 = 75 → 56.25 pt. The second column starts
+ /// where the first ends, and items are inset off the shared border.
+ #[test]
+ fn declared_column_widths_place_cells_side_by_side() {
+ let nx = layout_of(
+ r#"<worksheet>
+ <cols><col min="1" max="1" width="10" customWidth="1"/><col min="2" max="2" width="10" customWidth="1"/></cols>
+ <sheetData>
+ <row r="1"><c r="A1" t="inlineStr"><is><t>a</t></is></c><c r="B1" t="inlineStr"><is><t>b</t></is></c></row>
+ <row r="2"><c r="A2"><v>1</v></c><c r="B2"><v>2</v></c></row>
+ </sheetData></worksheet>"#,
+ &[],
+ );
+ assert_eq!(nx.pages.len(), 1);
+ let items = &nx.pages[0].text_items;
+ let a = &items[0];
+ let b = &items[1];
+ assert_eq!(a.x, MARGIN + TEXT_INSET);
+ assert!(
+ (b.x - (MARGIN + 56.25 + TEXT_INSET)).abs() < 1e-4,
+ "b.x = {}",
+ b.x
+ );
+ // The inset leaves a real gap between adjacent cells, which is what
+ // native_page_text keys word spacing on.
+ assert!(b.x - (a.x + a.width) > 1.0);
+ }
+
+ /// 15 rows at 100 pt against 720 pt of usable height: 7 + 7 + 1.
+ #[test]
+ fn tall_sheets_paginate_and_the_slices_match() {
+ let rows_xml: String = (1..=15)
+ .map(|r| format!(r#"<row r="{r}" ht="100"><c r="A{r}"><v>{r}</v></c><c r="B{r}"><v>{r}</v></c></row>"#))
+ .collect();
+ let nx = layout_of(
+ &format!("<worksheet><sheetData>{rows_xml}</sheetData></worksheet>"),
+ &[],
+ );
+ assert_eq!(nx.pages.len(), 3);
+ assert_eq!(nx.pages[0].text_items.len(), 14);
+ assert_eq!(nx.pages[2].text_items.len(), 2);
+ // Every page restarts at the margin.
+ for p in &nx.pages {
+ assert_eq!(p.text_items[0].y, MARGIN);
+ assert_eq!(p.page_height, PAGE_HEIGHT);
+ }
+ // Per-page tables hold exactly the rows on that page; the doc-level
+ // table is unsplit.
+ let table_rows = |blocks: &[Block]| {
+ blocks
+ .iter()
+ .find_map(|b| match b {
+ Block::MergedTable { rows, .. } => Some(rows.len()),
+ _ => None,
+ })
+ .unwrap()
+ };
+ assert_eq!(table_rows(&nx.page_blocks[0]), 7);
+ assert_eq!(table_rows(&nx.page_blocks[1]), 7);
+ assert_eq!(table_rows(&nx.page_blocks[2]), 1);
+ assert_eq!(table_rows(&nx.all_blocks), 15);
+ }
+
+ /// A merge anchor's item covers the merged box; cut by a page break it
+ /// clamps to its own page, like the block slicer's rowspan.
+ #[test]
+ fn merge_rects_span_and_clamp_at_the_break() {
+ let rows_xml: String = (1..=10)
+ .map(|r| {
+ let extra = if r == 1 {
+ r#"<c r="A1" t="inlineStr"><is><t>tall</t></is></c>"#.to_string()
+ } else {
+ String::new()
+ };
+ format!(r#"<row r="{r}" ht="100">{extra}<c r="B{r}"><v>{r}</v></c></row>"#)
+ })
+ .collect();
+ let nx = layout_of(
+ &format!(
+ "<worksheet><sheetData>{rows_xml}</sheetData><mergeCells><mergeCell ref=\"A1:A10\"/></mergeCells></worksheet>"
+ ),
+ &[],
+ );
+ assert_eq!(nx.pages.len(), 2);
+ let tall = nx.pages[0]
+ .text_items
+ .iter()
+ .find(|t| t.text == "tall")
+ .unwrap();
+ // 7 rows fit on page one; the merge's item stops at the page.
+ assert_eq!(tall.height, 700.0);
+ assert!(
+ !nx.pages[1].text_items.iter().any(|t| t.text == "tall"),
+ "the continuation page repeats no text"
+ );
+ }
+
+ #[test]
+ fn an_empty_sheet_is_one_empty_page_with_its_heading() {
+ let nx = layout_of("<worksheet><sheetData/></worksheet>", &[]);
+ assert_eq!(nx.pages.len(), 1);
+ assert!(nx.pages[0].text_items.is_empty());
+ assert!(nx.pages[0].content_bounds.is_none());
+ assert!(matches!(&nx.page_blocks[0][0], Block::Heading { .. }));
+ assert_eq!(nx.outline.len(), 1);
+ assert_eq!(nx.outline[0].page_index, 0);
+ }
+
+ /// A narrow sheet still gets a Letter-width page; a wide one takes its
+ /// content width instead of clipping.
+ #[test]
+ fn page_width_is_content_width_with_a_letter_floor() {
+ let narrow = layout_of(
+ r#"<worksheet><sheetData>
+ <row r="1"><c r="A1"><v>1</v></c><c r="B1"><v>2</v></c></row>
+ <row r="2"><c r="A2"><v>3</v></c><c r="B2"><v>4</v></c></row>
+ </sheetData></worksheet>"#,
+ &[],
+ );
+ assert_eq!(narrow.pages[0].page_width, MIN_PAGE_WIDTH);
+
+ let cols: String = (1..=30)
+ .map(|c| format!(r#"<col min="{c}" max="{c}" width="20" customWidth="1"/>"#))
+ .collect();
+ let cells: String = (0..30)
+ .map(|c| {
+ let col = crate::office::xlsx_layout::tests::col_label(c);
+ format!(r#"<c r="{col}1"><v>{c}</v></c>"#)
+ })
+ .collect();
+ let wide = layout_of(
+ &format!(
+ r#"<worksheet><cols>{cols}</cols><sheetData><row r="1">{cells}</row><row r="2"><c r="A2"><v>9</v></c></row></sheetData></worksheet>"#
+ ),
+ &[],
+ );
+ // 30 × (round(20×7)+5 = 145 px → 108.75 pt) + margins.
+ let expect = 30.0 * 108.75 + 2.0 * MARGIN;
+ assert!(
+ (wide.pages[0].page_width - expect).abs() < 0.01,
+ "got {}",
+ wide.pages[0].page_width
+ );
+ }
+
+ /// The sub-unit branch of the pixel formula: w=0.5 → round(0.5×12) = 6 px.
+ #[test]
+ fn sub_unit_widths_use_the_padded_scale() {
+ assert_eq!(super::col_px(0.5), 6.0);
+ assert_eq!(super::col_px(8.43), 64.0);
+ assert_eq!(super::col_px(10.0), 75.0);
+ }
+
+ /// Hidden rows keep their height — their text is emitted, so their
+ /// geometry is real.
+ #[test]
+ fn hidden_rows_keep_their_declared_height() {
+ let nx = layout_of(
+ r#"<worksheet><sheetData>
+ <row r="1" ht="30" hidden="1"><c r="A1" t="inlineStr"><is><t>secret</t></is></c><c r="B1"><v>1</v></c></row>
+ <row r="2"><c r="A2"><v>2</v></c><c r="B2"><v>3</v></c></row>
+ </sheetData></worksheet>"#,
+ &[],
+ );
+ let items = &nx.pages[0].text_items;
+ assert_eq!(items[0].height, 30.0);
+ assert_eq!(items[2].y, MARGIN + 30.0);
+ }
+
+ /// Items carry raw cell text; the hyperlink is a field, not markdown.
+ #[test]
+ fn items_carry_raw_text_and_the_link_field() {
+ let sheet = r#"<worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheetData>
+ <row r="1"><c r="A1" t="inlineStr"><is><t>site</t></is></c><c r="B1"><v>1</v></c></row>
+ <row r="2"><c r="A2"><v>2</v></c><c r="B2"><v>3</v></c></row>
+ </sheetData><hyperlinks><hyperlink ref="A1" r:id="rId9"/></hyperlinks></worksheet>"#;
+ let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
+ <Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://example.com" TargetMode="External"/>
+</Relationships>"#;
+ let wb = workbook_from(sheet, &[("xl/worksheets/_rels/sheet1.xml.rels", rels)]);
+ let nx = workbook_to_pages(&wb, EmitOptions { links: true });
+ let site = &nx.pages[0].text_items[0];
+ assert_eq!(site.text, "site");
+ assert_eq!(site.link.as_deref(), Some("https://example.com"));
+ }
+
+ pub(crate) fn col_label(mut col: u32) -> String {
+ let mut s = String::new();
+ loop {
+ s.insert(0, (b'A' + (col % 26) as u8) as char);
+ if col < 26 {
+ break;
+ }
+ col = col / 26 - 1;
+ }
+ s
+ }
+}
diff --git a/crates/liteparse/src/parser.rs b/crates/liteparse/src/parser.rs
index 8b84fc0..18bda74 100644
--- a/crates/liteparse/src/parser.rs
+++ b/crates/liteparse/src/parser.rs
@@ -441,6 +441,42 @@ impl LiteParse {
}
}
+ // Native XLSX path, same contract again. The workbook states its own
+ // geometry, so this one has no layout engine to fail — the fallback
+ // exists for reader errors (and the text-sparse OCR case, where a
+ // pasted scan is only reachable by rendering through LibreOffice).
+ #[cfg(all(feature = "xlsx-native", not(target_arch = "wasm32")))]
+ if self.config.office_native {
+ if let Some(reason) = self.native_xlsx_ineligible_reason() {
+ if crate::office::xlsx_bytes(&input).is_some() {
+ return Err(LiteParseError::Config(format!(
+ "`{reason}` is not supported by the native XLSX path; \
+ disable it, or set office_native = false \
+ (CLI: --no-office-native) to parse via the conversion path"
+ )));
+ }
+ } else if let Some(bytes) = crate::office::xlsx_bytes(&input) {
+ match self.try_parse_xlsx_native(&bytes) {
+ Ok(Some(result)) => {
+ let total =
+ web_time::Instant::now().duration_since(t0).as_secs_f64() * 1000.0;
+ log(&format!("[liteparse] native xlsx total: {:.1}ms", total));
+ return Ok(result);
+ }
+ Ok(None) => {
+ log(
+ "[liteparse] office_native: text-sparse xlsx, falling back to conversion path for OCR",
+ );
+ }
+ Err(e) => {
+ log(&format!(
+ "[liteparse] office_native failed ({e}); falling back to conversion path"
+ ));
+ }
+ }
+ }
+ }
+
#[cfg(not(target_arch = "wasm32"))]
let (validated_input, _guard) =
conversion::resolve_pdf_input(input, self.config.password.as_deref(), false).await?;
@@ -861,6 +897,155 @@ impl LiteParse {
}
}
+ /// Config options the native XLSX path cannot honor. Hitting one with
+ /// `office_native` on is a hard `Config` error, not a fallback.
+ ///
+ /// Two entries are XLSX-specific:
+ ///
+ /// * `extract_annotations` — hyperlinks reach `TextItem::link`, but no
+ /// `DocumentAnnotation` list is built (same v1 state the PPTX path
+ /// started in).
+ /// * `extract_images` / `image_mode = embed` — the reader does not parse
+ /// `xl/drawings`, so a workbook's embedded pictures and charts are
+ /// invisible to the native path; an explicit ask routes to the
+ /// conversion path instead of silently under-delivering. The default
+ /// `image_mode = placeholder` is *not* gated: a native parse emits no
+ /// figure refs where the conversion path could — accepted and recorded,
+ /// because gating the default would turn the native path off.
+ #[cfg(all(feature = "xlsx-native", not(target_arch = "wasm32")))]
+ fn native_xlsx_ineligible_reason(&self) -> Option<&'static str> {
+ let c = &self.config;
+ if c.extract_form_fields {
+ Some("extract_form_fields")
+ } else if c.extract_structure_tree {
+ Some("extract_structure_tree")
+ } else if c.extract_vector_graphics {
+ Some("extract_vector_graphics")
+ } else if c.crop_box.is_some() {
+ Some("crop_box")
+ } else if c.skip_diagonal_text {
+ Some("skip_diagonal_text")
+ } else if c.extract_annotations {
+ Some("extract_annotations")
+ } else if c.effective_extract_images() {
+ Some("extract_images")
+ } else {
+ None
+ }
+ }
+
+ /// Run the native XLSX pipeline. Contract matches the DOCX/PPTX siblings:
+ /// `Ok(None)` is an eligible fallback (text-sparse workbook with OCR on),
+ /// and panics map to `Err` so the caller degrades to LibreOffice.
+ #[cfg(all(feature = "xlsx-native", not(target_arch = "wasm32")))]
+ fn try_parse_xlsx_native(&self, data: &[u8]) -> Result<Option<ParseResult>, LiteParseError> {
+ std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ self.parse_xlsx_native_inner(data)
+ }))
+ .unwrap_or_else(|_| {
+ Err(LiteParseError::Conversion(
+ "native xlsx layout panicked".to_string(),
+ ))
+ })
+ }
+
+ #[cfg(all(feature = "xlsx-native", not(target_arch = "wasm32")))]
+ fn parse_xlsx_native_inner(&self, data: &[u8]) -> Result<Option<ParseResult>, LiteParseError> {
+ use crate::office::{xlsx, xlsx_layout};
+
+ let wb = liteparse_ooxml::xlsx::read(data)
+ .map_err(|e| LiteParseError::Conversion(format!("xlsx parse failed: {e}")))?;
+ let nx = xlsx_layout::workbook_to_pages(
+ &wb,
+ xlsx::EmitOptions {
+ links: self.config.extract_links,
+ },
+ );
+
+ // A text-sparse workbook with OCR on is likely a pasted scan; only
+ // the conversion path can render and OCR it.
+ if self.config.ocr_enabled {
+ let total_chars: usize = nx
+ .pages
+ .iter()
+ .flat_map(|p| &p.text_items)
+ .map(|i| i.text.trim().len())
+ .sum();
+ if total_chars < Self::NATIVE_TEXT_SPARSE_CHARS {
+ return Ok(None);
+ }
+ }
+
+ // Per-page complexity from native facts: the page's table blocks
+ // (exact, not detected), no media rects (drawings are unparsed), and
+ // a column count of 1 — a spreadsheet has no section columns.
+ let complexity: Vec<Option<crate::ocr_merge::PageComplexityStats>> =
+ if self.config.include_complexity {
+ nx.pages
+ .iter()
+ .enumerate()
+ .map(|(i, p)| {
+ let tables = nx.page_blocks[i]
+ .iter()
+ .filter(|b| {
+ matches!(
+ b,
+ crate::markdown_layout::Block::Table { .. }
+ | crate::markdown_layout::Block::MergedTable { .. }
+ )
+ })
+ .count();
+ Some(crate::ocr_merge::calculate_native_page_complexity(
+ p,
+ &[],
+ tables,
+ 1,
+ ))
+ })
+ .collect()
+ } else {
+ vec![None; nx.pages.len()]
+ };
+
+ // Page selection mirrors the other native paths: `target_pages`
+ // filters by 1-based number, then `max_pages` truncates. The outline
+ // stays whole-document.
+ let mut selected: Vec<((Page, Vec<crate::markdown_layout::Block>), _)> = nx
+ .pages
+ .into_iter()
+ .zip(nx.page_blocks)
+ .zip(complexity)
+ .collect();
+ let mut page_filtered = false;
+ if let Some(targets) = self.resolve_target_pages()? {
+ let keep: std::collections::HashSet<usize> =
+ targets.iter().map(|p| *p as usize).collect();
+ selected.retain(|((p, _), _)| keep.contains(&p.page_number));
+ page_filtered = true;
+ }
+ if selected.len() > self.config.max_pages {
+ selected.truncate(self.config.max_pages);
+ page_filtered = true;
+ }
+ let mut pages = Vec::with_capacity(selected.len());
+ let mut page_blocks = Vec::with_capacity(selected.len());
+ let mut page_stats = Vec::with_capacity(selected.len());
+ for ((p, b), s) in selected {
+ pages.push(p);
+ page_blocks.push(b);
+ page_stats.push(s);
+ }
+
+ Ok(Some(self.parse_from_native_blocks(
+ pages,
+ page_blocks,
+ (!page_filtered).then_some(nx.all_blocks),
+ nx.outline,
+ Vec::new(),
+ page_stats,
+ )))
+ }
+
/// Run the native PPTX pipeline. Contract matches the DOCX sibling:
/// `Ok(None)` is an eligible fallback (text-sparse deck with OCR on), and
/// panics map to `Err` so the caller degrades to LibreOffice.