A fast, helpful, and open-source document parser
fix: clip table block and cell boxes to the page
Christopher Atlanaba75de966c4
diff --git a/crates/liteparse/src/layout.rs b/crates/liteparse/src/layout.rsindex 13b1d04..fabd44f 100644--- a/crates/liteparse/src/layout.rs+++ b/crates/liteparse/src/layout.rs@@ -224,9 +224,48 @@ pub(crate) fn blocks_for_page(page: &ParsedPage, blocks: &[PositionedBlock]) ->if !page.projected_item_frames.is_empty() {remap_to_page_frame(&mut out, &page.projected_item_frames);}+ clip_tables_to_page(&mut out, page.page_width, page.page_height);out}+/// Clip table and cell boxes to the page. Ruled-grid boundaries come from+/// vector paths, whose bounds PDFium reports unclipped, so a path drawn past+/// the page edge would otherwise stretch a column (and the table) off the page.+fn clip_tables_to_page(blocks: &mut [LayoutBlock], width: f32, height: f32) {+ if !(width > 0.0 && width.is_finite() && height > 0.0 && height.is_finite()) {+ return;+ }+ for block in blocks.iter_mut() {+ if !matches!(block.kind.as_str(), "table" | "merged_table") {+ continue;+ }+ let cells = block+ .header+ .iter_mut()+ .flatten()+ .chain(block.rows.iter_mut().flatten().flatten());+ for r in std::iter::once(&mut block.bbox)+ .chain(cells.map(|c| &mut c.bbox))+ .flatten()+ {+ clip_rect(r, width, height);+ }+ }+}++/// Leaves a non-finite rect as-is (`f32::clamp` panics on NaN, and `inf - inf` is NaN).+fn clip_rect(r: &mut Rect, width: f32, height: f32) {+ if ![r.x, r.y, r.width, r.height].iter().all(|v| v.is_finite()) {+ return;+ }+ let x1 = (r.x + r.width).clamp(0.0, width);+ let y1 = (r.y + r.height).clamp(0.0, height);+ r.x = r.x.clamp(0.0, x1);+ r.y = r.y.clamp(0.0, y1);+ r.width = x1 - r.x;+ r.height = y1 - r.y;+}+/// Map text-derived block and cell boxes from the projection frame back to/// the page frame. A box is the union of the text items it was built from,/// so its page-frame counterpart is the union of those items' original@@ -325,6 +364,53 @@ mod tests {assert_eq!(blocks[2].bbox, Some(rect(300.0, 300.0, 50.0, 50.0)));}+ #[test]+ fn clip_tables_to_page_clips_offpage_cells() {+ let cell = |text: &str, bbox: Rect| LayoutCell {+ text: text.into(),+ bbox: Some(bbox),+ colspan: None,+ rowspan: None,+ };+ // A ruled column whose right boundary came from a path drawn far past+ // the 595pt page edge.+ let mut blocks = vec![+ LayoutBlock {+ header: Some(vec![cell("h", rect(-20.0, -5.0, 200.0, 20.0))]),+ rows: Some(vec![vec![+ cell("a", rect(160.0, 650.0, 274.0, 14.0)),+ cell("", rect(434.0, 650.0, 5088.0, 14.0)),+ ]]),+ ..LayoutBlock::of("table", Some(rect(160.0, 650.0, 5362.0, 14.0)))+ },+ para(rect(-10.0, 10.0, 100.0, 10.0)),+ ];+ clip_tables_to_page(&mut blocks, 595.0, 842.0);+ assert_eq!(blocks[0].bbox, Some(rect(160.0, 650.0, 435.0, 14.0)));+ let header = &blocks[0].header.as_ref().unwrap()[0];+ assert_eq!(header.bbox, Some(rect(0.0, 0.0, 180.0, 15.0)));+ let row = &blocks[0].rows.as_ref().unwrap()[0];+ assert_eq!(row[0].bbox, Some(rect(160.0, 650.0, 274.0, 14.0)));+ assert_eq!(row[1].bbox, Some(rect(434.0, 650.0, 161.0, 14.0)));+ // Only table geometry is clipped.+ assert_eq!(blocks[1].bbox, Some(rect(-10.0, 10.0, 100.0, 10.0)));+ }++ #[test]+ fn clip_tables_to_page_leaves_non_finite_geometry_alone() {+ let nan = rect(f32::NAN, 10.0, 50.0, 10.0);+ let mut blocks = vec![LayoutBlock::of("table", Some(nan))];+ clip_tables_to_page(&mut blocks, 595.0, 842.0);+ assert!(blocks[0].bbox.as_ref().unwrap().x.is_nan());+ let inf = rect(f32::NEG_INFINITY, 10.0, f32::INFINITY, 10.0);+ let mut blocks = vec![LayoutBlock::of("table", Some(inf.clone()))];+ clip_tables_to_page(&mut blocks, 595.0, 842.0);+ assert_eq!(blocks[0].bbox, Some(inf));+ let mut blocks = vec![LayoutBlock::of("table", Some(rect(0.0, 0.0, 900.0, 10.0)))];+ clip_tables_to_page(&mut blocks, f32::NAN, 842.0);+ assert_eq!(blocks[0].bbox, Some(rect(0.0, 0.0, 900.0, 10.0)));+ }+#[test]fn remap_leaves_boxes_with_no_items_untouched() {let frames = vec![(rect(0.0, 900.0, 10.0, 10.0), rect(0.0, 100.0, 10.0, 10.0))];diff --git a/crates/liteparse/tests/table_bounds.rs b/crates/liteparse/tests/table_bounds.rsnew file mode 100644index 0000000..50c377e--- /dev/null+++ b/crates/liteparse/tests/table_bounds.rs@@ -0,0 +1,92 @@+//! Synthetic PDFs keep table-geometry regressions independent of customer documents.+use liteparse::types::{PdfInput, Rect};+use liteparse::{LiteParse, LiteParseConfig};++/// A one-page 595x842 PDF holding a ruled 3x3 table whose last column is+/// stroked as a single rect running to x=5600, far past the right page edge.+fn offpage_table_pdf() -> Vec<u8> {+ let mut content = String::from("0.5 w\n");+ for y in [700, 680, 660, 640] {+ content += &format!("100 {y} m 400 {y} l S\n");+ }+ for x in [100, 250, 400] {+ content += &format!("{x} 640 m {x} 700 l S\n");+ }+ content += "400 640 5200 60 re S\n";+ let rows = [+ ["Item", "Qty", "Notes"],+ ["Apples", "3", "fresh"],+ ["Pears", "5", "ripe"],+ ];+ for (r, row) in rows.iter().enumerate() {+ let y = 686 - 20 * r;+ for (x, text) in [104, 254, 404].iter().zip(row) {+ content += &format!("BT /F1 10 Tf {x} {y} Td ({text}) Tj ET\n");+ }+ }+ let objects = [+ "<< /Type /Catalog /Pages 2 0 R >>".to_string(),+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(),+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>".to_string(),+ "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".to_string(),+ format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()),+ ];+ let mut data = b"%PDF-1.7\n".to_vec();+ let mut offsets = Vec::new();+ for (i, object) in objects.iter().enumerate() {+ offsets.push(data.len());+ data.extend_from_slice(format!("{} 0 obj\n{object}\nendobj\n", i + 1).as_bytes());+ }+ let xref = data.len();+ data.extend_from_slice(+ format!("xref\n0 {}\n0000000000 65535 f \n", objects.len() + 1).as_bytes(),+ );+ for offset in offsets {+ data.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());+ }+ data.extend_from_slice(+ format!(+ "trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n",+ objects.len() + 1+ )+ .as_bytes(),+ );+ data+}++fn assert_on_page(r: &Rect, width: f32, height: f32) {+ assert!(+ r.x >= 0.0 && r.y >= 0.0 && r.x + r.width <= width && r.y + r.height <= height,+ "{r:?} escapes the {width}x{height} page"+ );+}++#[tokio::test]+async fn table_boxes_stay_on_the_page() {+ let parser = LiteParse::new(LiteParseConfig {+ ocr_enabled: false,+ quiet: true,+ extract_blocks: true,+ ..LiteParseConfig::default()+ });+ let result = parser+ .parse_input(PdfInput::Bytes(offpage_table_pdf()))+ .await+ .unwrap();+ let page = &result.pages[0];+ let blocks = page.blocks.as_ref().unwrap();+ let table = blocks.iter().find(|b| b.kind == "table").unwrap();+ let (width, height) = (page.page_width, page.page_height);+ assert_on_page(table.bbox.as_ref().unwrap(), width, height);+ let cells = table+ .header+ .iter()+ .flatten()+ .chain(table.rows.iter().flatten().flatten());+ let mut count = 0;+ for cell in cells {+ assert_on_page(cell.bbox.as_ref().unwrap(), width, height);+ count += 1;+ }+ assert_eq!(count, 9);+}