A fast, helpful, and open-source document parser
fix: preserve partially clipped glyphs in full
Pierre-Loic doulcet289e9c595278
diff --git a/crates/liteparse/src/raw_text.rs b/crates/liteparse/src/raw_text.rsindex 7d6d97c..29bce08 100644--- a/crates/liteparse/src/raw_text.rs+++ b/crates/liteparse/src/raw_text.rs@@ -6,7 +6,8 @@//! normalisation, no dedup — so its output is a stable function of what pdfium//! reports and a consumer can build its own segmentation on top. Glyphs hidden by//! known rectangular clips are omitted before grouping; partially clipped glyphs-//! are kept when their loose-box centre is inside. Unknown clips preserve text.+//! are kept in full whenever their loose box intersects the clip. Unknown clips+//! preserve text.//!//! Rules://!diff --git a/crates/liteparse/src/text_clip.rs b/crates/liteparse/src/text_clip.rsindex 74ef9c6..fb1c0db 100644--- a/crates/liteparse/src/text_clip.rs+++ b/crates/liteparse/src/text_clip.rs@@ -92,7 +92,7 @@ impl TextClip {}/// Generated separators have no reliable source object and are preserved.- /// Partially clipped glyphs survive when their loose-box centre is inside.+ /// Any intersection with the loose glyph box preserves the complete glyph.pub(crate) fn hides(&self, cv: &CharView<'_, '_>) -> bool {if cv.is_generated() {return false;@@ -114,16 +114,14 @@ impl TextClip {{return false;}- let x = f64::from(glyph.left) * 0.5 + f64::from(glyph.right) * 0.5;- let y = f64::from(glyph.bottom) * 0.5 + f64::from(glyph.top) * 0.5;// A tiny tolerance prevents float rounding at a clip edge losing a glyph.const EPS: f64 = 0.001;bounds.left > bounds.right|| bounds.bottom > bounds.top- || x < f64::from(bounds.left) - EPS- || x > f64::from(bounds.right) + EPS- || y < f64::from(bounds.bottom) - EPS- || y > f64::from(bounds.top) + EPS+ || f64::from(glyph.right) < f64::from(bounds.left) - EPS+ || f64::from(glyph.left) > f64::from(bounds.right) + EPS+ || f64::from(glyph.top) < f64::from(bounds.bottom) - EPS+ || f64::from(glyph.bottom) > f64::from(bounds.top) + EPS}}diff --git a/crates/liteparse/tests/clipped_text.rs b/crates/liteparse/tests/clipped_text.rsindex 0da5c91..3a8945d 100644--- a/crates/liteparse/tests/clipped_text.rs+++ b/crates/liteparse/tests/clipped_text.rs@@ -79,9 +79,9 @@ fn clipped_overflow_does_not_leak_into_next_row() {}#[test]-fn horizontal_partial_glyphs_use_centre_and_page_rotation_does_not_matter() {+fn horizontal_partial_glyphs_survive_in_full_at_every_page_rotation() {for rotation in [0, 90, 180, 270] {- for (edge, expected) in [(26, "ABC"), (24, "AB")] {+ for (edge, expected) in [(26, "ABC"), (24, "ABC"), (22, "ABC"), (21, "AB")] {let content = format!("q 10 10 {} 30 re W n BT /F1 10 Tf 10 20 Td (ABCDE) Tj ET Q",edge - 10@@ -102,13 +102,13 @@ fn nested_forms_compose_transforms_and_inherit_outer_clips() {"2 0 0 2 10 15 cm /Inner Do",);let content = "q 100 100 35 300 re W n 1 0 0 1 100 100 cm /Outer Do Q";- assert_text(&pdf(content, &[&outer, &inner], 0), "A");+ assert_text(&pdf(content, &[&outer, &inner], 0), "AB");}#[test]fn form_bbox_clips_text_without_explicit_clip_operator() {let form = stream(- "/Type /XObject /Subtype /Form /BBox [0 0 18 100] /Resources << /Font << /F1 4 0 R >> >>",+ "/Type /XObject /Subtype /Form /BBox [0 0 17 100] /Resources << /Font << /F1 4 0 R >> >>","BT /F1 10 Tf 0 20 Td (ABCDE) Tj ET",);assert_text(&pdf("1 0 0 1 100 100 cm /Outer Do", &[&form], 0), "ABC");@@ -117,7 +117,7 @@ fn form_bbox_clips_text_without_explicit_clip_operator() {#[test]fn stacked_clips_intersect_and_restore_does_not_leak() {let content = "q 0 0 100 100 re W n 10 10 14 30 re W* n BT /F1 10 Tf 10 20 Td (ABCDE) Tj ET Q BT /F1 10 Tf 100 100 Td (RESTORED) Tj ET";- assert_text(&pdf(content, &[], 0), "AB RESTORED");+ assert_text(&pdf(content, &[], 0), "ABC RESTORED");}#[test]@@ -168,7 +168,7 @@ fn content_matrix_is_not_applied_to_clip_twice() {&[],0,),- "AB",+ "ABC",);}@@ -183,3 +183,25 @@ fn unsupported_parent_clip_preserves_form_text() {"KEEP",);}++#[test]+fn partial_glyphs_keep_the_full_text_and_geometry_at_all_clip_edges() {+ let text = "BT /F1 10 Tf 10 20 Td (A) Tj ET";+ let original = extract(&pdf(text, &[], 0));+ let glyph = original.0.iter().find(|item| item.text == "A").unwrap();+ // Raw item coordinates have a top-left origin; content streams use y-up.+ let left = glyph.x;+ let right = left + glyph.width;+ let top = 800.0 - glyph.y;+ let bottom = top - glyph.height;+ let clips = [+ (left - 1.0, bottom - 1.0, 1.1, glyph.height + 2.0),+ (right - 0.1, bottom - 1.0, 1.1, glyph.height + 2.0),+ (left - 1.0, bottom - 1.0, glyph.width + 2.0, 1.1),+ (left - 1.0, top - 0.1, glyph.width + 2.0, 1.1),+ ];+ for (x, y, width, height) in clips {+ let content = format!("q {x} {y} {width} {height} re W n {text} Q");+ assert_eq!(extract(&pdf(&content, &[], 0)), original);+ }+}