Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M2: retain clipping text in filtered extraction (#3116)

jonathanwxh-cellff5a2fc9eb9e
diff --git a/ROADMAP.md b/ROADMAP.md
index c895ed2c3..eede1bb98 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -160,6 +160,7 @@ Ranked; detail and the full milestone table in [docs/roadmap.md](docs/roadmap.md
| 8 | 1.0 polish: performance, right-to-left, missing languages, keyboard-only (M14) | 55–95 |
## Progress log
+- **2026-10-18 (M2, #3116):** Distinguish clipping-only text from invisible OCR so image-filtered extraction retains clipped headings and ActualText. Vendor changes and regression coverage are documented; estimates unchanged.
- **2026-10-15 (M6, AcroForm XML text):** Plain AcroForm XML imports retain all direct text around comments and processing instructions, including CDATA and character references. Synthetic regressions verify exact values and unknown catalog data after incremental and full save/reopen. Nested-element and XFA interpretation are unchanged. Estimates unchanged.
- **2026-10-15 (M6, #1917 refresh):** Integrated upstream `99941daa` while retaining the XML/XFDF attribute whitespace correction and all upstream progress. Only independent documentation conflicts required resolution; focused production and test files are unchanged from the previously green CI head. Estimates unchanged.
- **2026-10-15 (M1, #2267 refresh):** Integrated upstream `99941daa` while retaining the raw-deflate fallback correction and all upstream progress. Only independent documentation conflicts required resolution; focused production and test files are unchanged from the previously green CI head. Estimates unchanged.
diff --git a/crates/render/src/text.rs b/crates/render/src/text.rs
index e163b204b..7dbe3cebb 100644
--- a/crates/render/src/text.rs
+++ b/crates/render/src/text.rs
@@ -1804,6 +1804,20 @@ mod tests {
assert!(unfilled_px.rgba.as_chunks::<4>().0.iter().all(|p| p[..3] == [255, 255, 255]));
}
+ #[test]
+ fn clipping_mode_filter_keeps_titles_and_excludes_ocr() {
+ let pdf = marked_pages(&[
+ "q BT /F1 24 Tf 7 Tr 30 200 Td (Clipped heading) Tj ET 0 0 1 rg 0 0 300 300 re f Q BT /F1 12 Tf 3 Tr 30 100 Td (Hidden OCR) Tj ET",
+ "q /Span <</ActualText (Accessible heading)>> BDC BT /F1 24 Tf 7 Tr 30 200 Td (Clipped heading) Tj ET EMC 0 0 1 rg 0 0 300 300 re f Q",
+ ]);
+ assert!(marked_text(&pdf, 0).plain_text().contains("Hidden OCR"));
+ for (page, expected) in [(0, "Clipped heading"), (1, "Accessible heading")] {
+ let text = extract_page_filtered(&pdf, page, &InterpreterSettings::default(), TextFilter { images: true, ..Default::default() }).unwrap();
+ assert_eq!(text.plain_text(), expected);
+ assert!(!text.find("heading").is_empty());
+ }
+ }
+
/// One page per `(encoding, content)`, in Helvetica (`/F1`).
fn encoded_pages(pages: &[(lopdf::Object, &[u8])]) -> Pdf {
use lopdf::{Document, Object, Stream, dictionary};
diff --git a/docs/roadmap.md b/docs/roadmap.md
index 2c65496b6..16528303b 100644
--- a/docs/roadmap.md
+++ b/docs/roadmap.md
@@ -1,5 +1,7 @@
# PdfCraft roadmap: milestones and what's next
+> **2026-10-18:** M2 / #3116: clipping-only glyphs stay extractable with the image filter; invisible OCR remains excluded. Estimates unchanged.
+
> **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-15 · **Change:** minor (M6: AcroForm XML text; estimates unchanged)
> **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-15 · **Change:** minor (M6: preserve XML/XFDF attribute whitespace; refreshed upstream; estimates unchanged) · **Target:** Adobe Acrobat Pro (Acrobat DC, continuous track 26.002.21931, macOS)
> **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-15 · **Change:** minor (M1: raw-deflate fallback preserves decoding-limit errors; refreshed upstream; estimates unchanged) · **Target:** Adobe Acrobat Pro (Acrobat DC, continuous track 26.002.21931, macOS)
diff --git a/vendor/README.md b/vendor/README.md
index 62be4cad9..06e592e47 100644
--- a/vendor/README.md
+++ b/vendor/README.md
@@ -52,3 +52,14 @@ black-point normalization is unchanged. `lab_bradford_tests` checks independent
D50/equal-energy neutral and chromatic oracles, D65 identity, and direct/Indexed
consistency (including an asymmetric chromatic Range), and zero/overflowing
cone-response fallback.
+
+## Clipping-only text reporting (#3109 / #3116)
+
+`hayro-interpret/src/interpret/text.rs` reports mode 7 glyphs after installing their
+clip, and `hayro-interpret/src/types.rs` distinguishes `GlyphDrawMode::Clip` from
+invisible OCR text. `hayro/src/renderer.rs` does not paint either mode directly.
+This preserves clipping-only titles when `TextFilter.images` excludes invisible OCR.
+Coverage: `text::tests::clipping_mode_filter_keeps_titles_and_excludes_ocr` (plain
+and ActualText filtered extraction), and
+`text::tests::clipping_mode_text_is_extracted_and_searchable_like_other_rendering_modes`
+(extraction, search, glyph geometry, clip-through pixels and no-paint pixels).
diff --git a/vendor/hayro-interpret/src/interpret/text.rs b/vendor/hayro-interpret/src/interpret/text.rs
index 8b80b4bdb..cd5e147f9 100644
--- a/vendor/hayro-interpret/src/interpret/text.rs
+++ b/vendor/hayro-interpret/src/interpret/text.rs
@@ -126,15 +126,15 @@ pub(crate) fn show_glyph<'a>(
}
TextRenderingMode::Clip => {
clip_glyph(ctx, glyph, glyph_transform);
- // PdfCraft patch: still call draw_glyph for clip-only text (`7 Tr`), as mode 3 does,
- // so text extraction, search and search-redaction see text that is filled through its
+ // PdfCraft patch: report clipping text separately from invisible OCR text.
+ // Extraction filters must retain text that is filled through its
// clipping path without rendering devices drawing it directly.
device.draw_glyph(
glyph,
ctx.get().ctm,
glyph_transform,
&get_paint(ctx, false),
- &GlyphDrawMode::Invisible,
+ &GlyphDrawMode::Clip,
);
}
TextRenderingMode::FillAndClip => {
diff --git a/vendor/hayro-interpret/src/types.rs b/vendor/hayro-interpret/src/types.rs
index 64d2b849b..685ed3e99 100644
--- a/vendor/hayro-interpret/src/types.rs
+++ b/vendor/hayro-interpret/src/types.rs
@@ -284,6 +284,8 @@ pub enum GlyphDrawMode {
Fill,
/// Draw using a stroke.
Stroke(StrokeProps),
+ /// PdfCraft patch: text that shapes a clip; report it without painting a glyph.
+ Clip,
/// Invisible text (for text extraction but not visual rendering).
Invisible,
}
diff --git a/vendor/hayro/src/renderer.rs b/vendor/hayro/src/renderer.rs
index 0619b4c6d..dee2f983f 100644
--- a/vendor/hayro/src/renderer.rs
+++ b/vendor/hayro/src/renderer.rs
@@ -1686,8 +1686,8 @@ impl<'a> Device<'a> for Renderer {
GlyphDrawMode::Stroke(s) => {
Self::stroke_glyph(self, glyph, transform, glyph_transform, paint, s);
}
- GlyphDrawMode::Invisible => {
- // Don't render invisible text for visual output
+ GlyphDrawMode::Invisible | GlyphDrawMode::Clip => {
+ // PdfCraft patch: neither mode paints; clipping was installed by the interpreter.
}
}
}