A fast, helpful, and open-source document parser
fix: judge clipped glyphs by their ink box, not the loose box
Pierre-Loic doulcet8c7ca551bf8f
diff --git a/crates/liteparse/src/text_clip.rs b/crates/liteparse/src/text_clip.rsindex fb1c0db..8b1d788 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.- /// Any intersection with the loose glyph box preserves the complete glyph.+ /// Any intersection with the glyph's ink box preserves the complete glyph.pub(crate) fn hides(&self, cv: &CharView<'_, '_>) -> bool {if cv.is_generated() {return false;@@ -103,7 +103,14 @@ impl TextClip {else {return false;};- let Some(glyph) = cv.loose_char_box().or_else(|| cv.strict_char_box()) else {+ // The ink (strict) box decides: a loose box carries the font's side bearings and+ // ascent, so it can graze a clip edge by a fraction of a point while every pixel of+ // the glyph lies outside. Whitespace and inkless glyphs keep the loose box.+ let inked = !char::from_u32(cv.unicode()).is_some_and(char::is_whitespace);+ let strict = cv+ .strict_char_box()+ .filter(|b| inked && b.right > b.left && b.top > b.bottom);+ let Some(glyph) = strict.or_else(|| cv.loose_char_box()) else {return false;};if ![glyph.left, glyph.right, glyph.bottom, glyph.top]diff --git a/crates/liteparse/tests/clipped_text.rs b/crates/liteparse/tests/clipped_text.rsindex 3a8945d..54bfff8 100644--- a/crates/liteparse/tests/clipped_text.rs+++ b/crates/liteparse/tests/clipped_text.rs@@ -80,8 +80,16 @@ fn clipped_overflow_does_not_leak_into_next_row() {#[test]fn horizontal_partial_glyphs_survive_in_full_at_every_page_rotation() {+ // Courier advances 6 pt, so C's advance starts at x = 22 and its ink a little after.+ // A clip edge at 22 touches the advance but shows none of C's ink.for rotation in [0, 90, 180, 270] {- for (edge, expected) in [(26, "ABC"), (24, "ABC"), (22, "ABC"), (21, "AB")] {+ for (edge, expected) in [+ (26, "ABC"),+ (24, "ABC"),+ (23, "ABC"),+ (22, "AB"),+ (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@@ -184,24 +192,62 @@ fn unsupported_parent_clip_preserves_form_text() {);}+/// Left, right, bottom and top of a box in y-up page space.+type Edges = (f32, f32, f32, f32);++/// The glyph's ink box (its strict box, `grounding_bounds`) in y-up page space, with+/// its loose box (advance width by font ascent and descent), as (left, right, bottom, top).+fn glyph_boxes(item: &RawTextItem) -> (Edges, Edges) {+ // Raw item coordinates have a top-left origin; content streams use y-up.+ let ink = item.grounding_bounds.unwrap();+ let ink = (ink.left, ink.right, 800.0 - ink.bottom, 800.0 - ink.top);+ let top = 800.0 - item.y;+ let loose = (item.x, item.x + item.width, top - item.height, top);+ (ink, loose)+}+#[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;+ // A clip that shows 0.1 pt of the glyph's ink at any edge keeps the whole glyph.+ let ((left, right, bottom, top), _) = glyph_boxes(glyph);+ let (width, height) = (right - left, top - bottom);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),+ (left - 1.0, bottom - 1.0, 1.1, height + 2.0),+ (right - 0.1, bottom - 1.0, 1.1, height + 2.0),+ (left - 1.0, bottom - 1.0, width + 2.0, 1.1),+ (left - 1.0, top - 0.1, 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);}}++#[test]+fn glyphs_whose_ink_lies_outside_the_clip_are_hidden_even_when_their_loose_box_grazes_it() {+ // The loose box carries side bearings, ascent and descent, so a clip edge can+ // touch it while no ink is visible. Each clip reaches 0.05 pt into the loose box.+ 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();+ let ((ink_left, _, ink_bottom, ink_top), (left, right, bottom, top)) = glyph_boxes(glyph);+ let graze = 0.05;+ assert!(+ ink_left > left + graze,+ "A's ink must start after its advance"+ );+ assert!(ink_bottom > bottom + graze && ink_top < top - graze);+ let (width, height) = (right - left, top - bottom);+ let clips = [+ (left - 1.0, bottom - 1.0, 1.0 + graze, height + 2.0),+ (left - 1.0, bottom - 1.0, width + 2.0, 1.0 + graze),+ (left - 1.0, top - graze, width + 2.0, 1.0 + graze),+ ];+ for (x, y, width, height) in clips {+ let content = format!("q {x} {y} {width} {height} re W n {text} Q");+ assert_text(&pdf(&content, &[], 0), "");+ }+}