ps: keep synthesized text spans inside clip scopes

This commit is contained in:
Dylan M. Taylor
2026-11-15 18:07:43 +00:00
parent d181e08d3d
commit 204240ad80
3 changed files with 27 additions and 3 deletions
+9 -2
View File
@@ -567,12 +567,19 @@ fn key_code(code: u32) -> Option<u8> {
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
+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) {
+13
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,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();