From 204240ad80c652d6ca2d31d6930aedb8aa4eca2d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:07:43 +0000 Subject: [PATCH] ps: keep synthesized text spans inside clip scopes --- crates/ps/src/read/font.rs | 11 +++++++++-- crates/ps/src/read/pdf.rs | 6 +++++- crates/ps/src/read/tests.rs | 13 +++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/ps/src/read/font.rs b/crates/ps/src/read/font.rs index 9a1af3fb6..f7b0e5ced 100644 --- a/crates/ps/src/read/font.rs +++ b/crates/ps/src/read/font.rs @@ -567,12 +567,19 @@ fn key_code(code: u32) -> Option { fn show_string(ip: &mut Interp, s: &[u8], draw: bool, per_char: &mut PerGlyph<'_>) -> R<(f64, f64)> { // Whitespace-only shows advance the text position but emit no painted glyphs; avoid bloating // the content stream with an empty semantic marked-content wrapper for them. - let text = ip.gs.font.as_ref().and_then(|font| actual_text(font, s)); + // Type 3 glyph programs can paint and change clipping inside a show. Composite fonts may + // select Type 3 descendants. Do not wrap their arbitrary output in a synthetic text span. + let text = ip + .gs + .font + .as_ref() + .filter(|font| !matches!(font.get_name(b"FontType").and_then(|v| v.num()), Some(0.0 | 3.0))) + .and_then(|font| actual_text(font, s)); if draw && !ip.charpath && let Some(text) = text.filter(|text| text.chars().any(|c| !c.is_whitespace())) { - ip.out.begin_actual_text(&text); + ip.out.begin_actual_text(&text, &ip.gs.clip).map_err(PsErr::Fatal)?; let result = show_glyphs(ip, s, draw, false, per_char); ip.out.end_actual_text(); result diff --git a/crates/ps/src/read/pdf.rs b/crates/ps/src/read/pdf.rs index 0faabe09a..3231dfaa4 100644 --- a/crates/ps/src/read/pdf.rs +++ b/crates/ps/src/read/pdf.rs @@ -153,8 +153,12 @@ impl Out { } /// Preserve the semantic text for consumers that group outlined glyphs. - pub fn begin_actual_text(&mut self, text: &str) { + pub fn begin_actual_text(&mut self, text: &str, clip: &Option>) -> Result<(), String> { + // A clip can outlive the show. Establish it before the marked span so EMC does not + // accidentally close the graphics state that later painting still needs. + self.sync_clip(clip)?; self.c.begin_marked_content_with_properties(Name(b"Span")).properties().actual_text(pdf_writer::TextStr(text)); + Ok(()) } pub fn end_actual_text(&mut self) { diff --git a/crates/ps/src/read/tests.rs b/crates/ps/src/read/tests.rs index 152041659..99c934158 100644 --- a/crates/ps/src/read/tests.rs +++ b/crates/ps/src/read/tests.rs @@ -137,6 +137,7 @@ fn type3_fonts_draw_their_glyphs() { /T3 T3 definefont pop /T3 findfont 20 scalefont setfont 10 20 moveto (AA) show"); // Two glyphs, the second 12 points (600 units at 20 pt) to the right. assert_eq!(c.lines().filter(|l| *l == "f").count(), 2, "{c}"); + assert!(!c.contains("/ActualText"), "Type 3 procedures are not synthetic text spans: {c}"); assert!(c.contains("22 20 m"), "{c}"); } @@ -471,6 +472,18 @@ fn actual_text_uses_the_selected_encoding_and_omits_unknown_names() { assert!(!content.contains("/ActualText"), "{content}"); } +#[test] +fn actual_text_starts_after_persistent_clip() { + let (content, _) = run( + "newpath 0 0 moveto 50 0 lineto 50 50 lineto closepath clip newpath /Helvetica findfont 12 scalefont setfont 10 10 moveto (Hi) show 0 0 100 100 rectfill", + ); + let clip = content.find("\nW\n").unwrap(); + let span = content.find("/ActualText (Hi)").unwrap(); + let end = content.find("EMC").unwrap(); + let restore = content.rfind("\nQ").unwrap(); + assert!(clip < span && span < end && end < restore, "{content}"); +} + #[test] fn text_paint_keeps_completed_glyphs_when_a_later_glyph_hits_the_work_limit() { let ttf = tiny_ttf();