ps: check selected glyphs before text labeling

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 18:09:31 +00:00
1 parent 204240ad80
commit 1fdb3934b5
2 files changed
+45 -31

No files matched your search

+35 -31
View File
@@ -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::Seg>, 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<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.
// 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(|_| ())
}
+10
View File
@@ -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(