A fast, helpful, and open-source document parser
fix: keep glyphs whose ink misses a clip by under 2% of the em
Pierre-Loic doulcetf5e3d38804ee
diff --git a/crates/liteparse/src/text_clip.rs b/crates/liteparse/src/text_clip.rsindex 8b1d788..2b04b35 100644--- a/crates/liteparse/src/text_clip.rs+++ b/crates/liteparse/src/text_clip.rs@@ -92,7 +92,8 @@ impl TextClip {}/// Generated separators have no reliable source object and are preserved.- /// Any intersection with the glyph's ink box preserves the complete glyph.+ /// A glyph is kept whole when its ink box, widened by [`INK_MARGIN`] of its em+ /// extent, meets the clip; glyphs without ink are judged by their loose box.pub(crate) fn hides(&self, cv: &CharView<'_, '_>) -> bool {if cv.is_generated() {return false;@@ -107,11 +108,14 @@ impl TextClip {// 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 loose = cv.loose_char_box();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;+ let (glyph, margin) = match (strict, loose) {+ (Some(ink), _) => (ink, INK_MARGIN * loose.map_or(0.0, em_extent)),+ (None, Some(loose)) => (loose, 0.0),+ (None, None) => return false,};if ![glyph.left, glyph.right, glyph.bottom, glyph.top].iter()@@ -123,15 +127,31 @@ impl TextClip {}// A tiny tolerance prevents float rounding at a clip edge losing a glyph.const EPS: f64 = 0.001;+ let tol = EPS + margin;bounds.left > bounds.right|| bounds.bottom > bounds.top- || 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+ || f64::from(glyph.right) < f64::from(bounds.left) - tol+ || f64::from(glyph.left) > f64::from(bounds.right) + tol+ || f64::from(glyph.top) < f64::from(bounds.bottom) - tol+ || f64::from(glyph.bottom) > f64::from(bounds.top) + tol}}+/// Share of a glyph's em extent by which its ink box may miss a clip and still count+/// as visible. PDFium's strict box can understate the painted outline (a substituted+/// font, quantised glyph bounds) by about 1% of the font size; that edge of ink is drawn.+const INK_MARGIN: f64 = 0.02;++/// The larger side of the loose box, about one em (ascent to descent) at any rotation.+fn em_extent(loose: RectF) -> f64 {+ let em = f64::from(+ (loose.right - loose.left)+ .abs()+ .max((loose.top - loose.bottom).abs()),+ );+ if em.is_finite() { em } else { 0.0 }+}+fn compose(p: &Matrix, c: &Matrix) -> Matrix {Matrix {a: p.a * c.a + p.c * c.b,diff --git a/crates/liteparse/tests/clipped_text.rs b/crates/liteparse/tests/clipped_text.rsindex 54bfff8..ff6c702 100644--- a/crates/liteparse/tests/clipped_text.rs+++ b/crates/liteparse/tests/clipped_text.rs@@ -228,26 +228,58 @@ fn partial_glyphs_keep_the_full_text_and_geometry_at_all_clip_edges() {#[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.+ // 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 only.+ let graze = 0.05;+ for (text, vertical) in [("A", true), (".", false)] {+ let content = format!("BT /F1 10 Tf 10 20 Td ({text}) Tj ET");+ let original = extract(&pdf(&content, &[], 0));+ let glyph = original.0.iter().find(|item| item.text == text).unwrap();+ let ((ink_left, ink_right, ink_bottom, ink_top), (left, right, bottom, top)) =+ glyph_boxes(glyph);+ let (width, height) = (right - left, top - bottom);+ let clips = if vertical {+ // "A" stands on the baseline and stops below the ascent.+ assert!(ink_bottom > bottom + 1.0 && ink_top < top - 1.0);+ [+ (left - 1.0, bottom - 1.0, width + 2.0, 1.0 + graze),+ (left - 1.0, top - graze, width + 2.0, 1.0 + graze),+ ]+ } else {+ // "." is narrow ink in the middle of a 6 pt advance.+ assert!(ink_left > left + 1.0 && ink_right < right - 1.0);+ [+ (left - 1.0, bottom - 1.0, 1.0 + graze, height + 2.0),+ (right - graze, bottom - 1.0, 1.0 + graze, height + 2.0),+ ]+ };+ for (x, y, width, height) in clips {+ let content = format!("q {x} {y} {width} {height} re W n {content} Q");+ assert_text(&pdf(&content, &[], 0), "");+ }+ }+}++#[test]+fn ink_just_outside_the_clip_is_kept_within_a_small_share_of_the_em() {+ // PDFium's ink box can understate the painted outline by about 1% of the font+ // size, so a clip edge 0.1 pt past the ink of a 10 pt glyph still shows it.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"+ let ((left, right, _, top), _) = glyph_boxes(glyph);+ let content = format!(+ "q {} {} {} 5 re W n {text} Q",+ left - 1.0,+ top + 0.1,+ right - left + 2.0);- 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), "");- }+ assert_eq!(extract(&pdf(&content, &[], 0)), original);+ let content = format!(+ "q {} {} {} 5 re W n {text} Q",+ left - 1.0,+ top + 1.0,+ right - left + 2.0+ );+ assert_text(&pdf(&content, &[], 0), "");}