Merge pull request #1694 from storytold/codex/ps-actualtext-scope

Keep synthesized PostScript text spans inside clip scopes
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 00:02:17 +01:00
commit c0f7be37f1
3 files changed
+62 -24

No files matched your search

+34 -23
View File
@@ -454,7 +454,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"),
@@ -468,6 +468,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::Seg>, crate::read::gstate::DevColor)> = Vec::new();
let mut text_segments = 0usize;
@@ -572,11 +578,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
}
@@ -593,17 +606,8 @@ fn show_string(ip: &mut Interp, s: &[u8], draw: bool, per_char: &mut PerGlyph<'_
// 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));
if draw
&& !ip.charpath
&& let Some(text) = text.filter(|text| text.chars().any(|c| !c.is_whitespace()))
{
ip.out.begin_actual_text(&text);
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 = 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 {
@@ -652,14 +656,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(|_| ())
}
+5 -1
View File
@@ -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<Rc<Clip>>) -> 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) {
+23
View File
@@ -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,28 @@ 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(
"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();