From 204240ad80c652d6ca2d31d6930aedb8aa4eca2d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:07:43 +0000 Subject: [PATCH 1/2] 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(); From 1fdb3934b5a8154828dd1646289f84dbd1302b53 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:09:31 +0000 Subject: [PATCH 2/2] ps: check selected glyphs before text labeling --- crates/ps/src/read/font.rs | 66 ++++++++++++++++++++----------------- crates/ps/src/read/tests.rs | 10 ++++++ 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/crates/ps/src/read/font.rs b/crates/ps/src/read/font.rs index f7b0e5ced..510f27863 100644 --- a/crates/ps/src/read/font.rs +++ b/crates/ps/src/read/font.rs @@ -429,7 +429,7 @@ type PerGlyph<'a> = dyn FnMut(&mut Interp, u32, usize) -> R<(f64, f64)> + 'a; /// Show (or, with `draw` false, measure) a string: the total advance in user space. /// `per_char` gives each glyph's extra displacement (or, with `replace`, its whole advance). -fn show_glyphs(ip: &mut Interp, s: &[u8], draw: bool, replace: bool, per_char: &mut PerGlyph<'_>) -> R<(f64, f64)> { +fn show_glyphs(ip: &mut Interp, s: &[u8], draw: bool, replace: bool, per_char: &mut PerGlyph<'_>, text: Option<&str>) -> R<(f64, f64)> { let font = match ip.gs.font.clone() { Some(f) => f, None => return err("invalidfont"), @@ -443,6 +443,12 @@ fn show_glyphs(ip: &mut Interp, s: &[u8], draw: bool, replace: bool, per_char: & let fm = font_matrix(&font); s.iter().map(|c| (u32::from(*c), font.clone(), fm, crate::read::composite::Key2::Code(*c))).collect() }; + // Glyph procedures can issue arbitrary paint and clip operations. The selected jobs, not + // the composite font's family, determine whether this show can have a synthetic wrapper. + let marked = text.filter(|_| !jobs.iter().any(|(_, leaf, _, _)| leaf.get_name(b"FontType").and_then(|v| v.num()) == Some(3.0))); + if let Some(text) = marked { + ip.out.begin_actual_text(text, &ip.gs.clip).map_err(PsErr::Fatal)?; + } let mut total = (0.0, 0.0); let mut text_paths: Vec<(Vec, crate::read::gstate::DevColor)> = Vec::new(); let mut text_segments = 0usize; @@ -547,11 +553,18 @@ fn show_glyphs(ip: &mut Interp, s: &[u8], draw: bool, replace: bool, per_char: & Ok(total) })(); let path_limit = matches!(&result, Err(PsErr::Fatal(message)) if message == "PostScript (path limit)"); - if (result.is_ok() || !path_limit) && !ip.gs.nulldev { - for (path, color) in text_paths { - ip.out.fill(&ip.gs.clip, &path, false, color).map_err(PsErr::Fatal)?; + let paint_result = (|| { + if (result.is_ok() || !path_limit) && !ip.gs.nulldev { + for (path, color) in text_paths { + ip.out.fill(&ip.gs.clip, &path, false, color).map_err(PsErr::Fatal)?; + } } + Ok(()) + })(); + if marked.is_some() { + ip.out.end_actual_text(); } + paint_result?; result } @@ -567,25 +580,9 @@ 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. - // 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.gs.clip).map_err(PsErr::Fatal)?; - let result = show_glyphs(ip, s, draw, false, per_char); - ip.out.end_actual_text(); - result - } else { - show_glyphs(ip, s, draw, false, per_char) - } + let text = ip.gs.font.as_ref().and_then(|font| actual_text(font, s)); + let text = text.filter(|text| draw && !ip.charpath && text.chars().any(|c| !c.is_whitespace())); + show_glyphs(ip, s, draw, false, per_char, text.as_deref()) } pub fn show(ip: &mut Interp) -> R { @@ -634,14 +631,21 @@ fn explicit_show(ip: &mut Interp, xs: bool, ys: bool) -> R { return err("nocurrentpoint"); } let step = if xs && ys { 2 } else { 1 }; - show_glyphs(ip, &s, true, true, &mut |_, _, i| { - let d = |k: usize| disp.get(i * step + k).copied().unwrap_or(0.0); - Ok(match (xs, ys) { - (true, true) => (d(0), d(1)), - (true, false) => (d(0), 0.0), - _ => (0.0, d(0)), - }) - }) + show_glyphs( + ip, + &s, + true, + true, + &mut |_, _, i| { + let d = |k: usize| disp.get(i * step + k).copied().unwrap_or(0.0); + Ok(match (xs, ys) { + (true, true) => (d(0), d(1)), + (true, false) => (d(0), 0.0), + _ => (0.0, d(0)), + }) + }, + None, + ) .map(|_| ()) } diff --git a/crates/ps/src/read/tests.rs b/crates/ps/src/read/tests.rs index 99c934158..c694a7177 100644 --- a/crates/ps/src/read/tests.rs +++ b/crates/ps/src/read/tests.rs @@ -472,6 +472,16 @@ fn actual_text_uses_the_selected_encoding_and_omits_unknown_names() { assert!(!content.contains("/ActualText"), "{content}"); } +#[test] +fn composite_show_only_omits_text_for_selected_type3_glyphs() { + let setup = "/T3 8 dict dup begin /FontType 3 def /FontMatrix [0.001 0 0 0.001 0 0] def /FontBBox [0 0 1000 1000] def /Encoding 256 array def 0 1 255 { Encoding exch /.notdef put } for Encoding 65 /A put /BuildChar { pop pop 600 0 0 0 500 500 setcachedevice 0 0 500 500 rectfill } def end def /T3 T3 definefont pop /T0 8 dict dup begin /FontType 0 def /FMapType 2 def /FontMatrix [1 0 0 1 0 0] def /Encoding 256 array def 0 1 255 { Encoding exch /A put } for /FDepVector [ /Helvetica findfont /T3 findfont ] def end /T0 exch definefont pop /T0 findfont 20 scalefont setfont 10 20 moveto "; + let (standard, _) = run(&format!("{setup} <0041> show")); + assert!(standard.contains("/ActualText (AA)"), "{standard}"); + let (mixed, _) = run(&format!("{setup} <00410141> show")); + assert!(!mixed.contains("/ActualText"), "{mixed}"); + assert!(mixed.lines().any(|line| line == "f"), "the Type 3 glyph is still painted: {mixed}"); +} + #[test] fn actual_text_starts_after_persistent_clip() { let (content, _) = run(