From 610849efa304caeab38f508523750c68d167bfd5 Mon Sep 17 00:00:00 2001 From: JonRC Date: Mon, 23 Nov 2026 00:53:54 -0300 Subject: [PATCH] EPS file fonts: only type shown in a carried font gets the kept font The kept fonts were attached by family and style, so type in a font the file only names whose family and style are those of a carried font (`TestOne-Roman` beside an embedded `TestOne`) got the carried font too, after its spacing had been set for the fallback font. Type shown in a carried font is now marked when it is made, and only marked type gets the kept font. --- crates/eps/src/import/file_fonts.rs | 24 ++++++++++++++++++------ crates/eps/src/import/tests_type1.rs | 13 +++++++++++++ crates/eps/src/import/text.rs | 8 +++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/crates/eps/src/import/file_fonts.rs b/crates/eps/src/import/file_fonts.rs index 7d9e7f643c..83e51bb7ee 100644 --- a/crates/eps/src/import/file_fonts.rs +++ b/crates/eps/src/import/file_fonts.rs @@ -106,9 +106,10 @@ impl FileFontGlyphs { } } -/// The type among `nodes` (and in their groups) set in the families and styles `kept` holds fonts -/// for (family and style → the font's blob in `images`): drawn in them while its text is the -/// file's, and laid out again for its bounds. +/// The type among `nodes` (and in their groups) shown in fonts the file carries ([`mark`]), in the +/// families and styles `kept` holds fonts for (family and style → the font's blob in `images`): +/// drawn in them while its text is the file's, and laid out again for its bounds. Type in a font +/// the file only names keeps the fallback font, also where its family and style are the same. pub(crate) fn keep_file_fonts(nodes: &mut [Arc], kept: &HashMap<(String, String), String>, images: &BTreeMap) { if kept.is_empty() { return; @@ -121,6 +122,9 @@ pub(crate) fn keep_file_fonts(nodes: &mut [Arc], kept: &HashMap<(String, S keep_file_fonts(children, kept, images); } let NodeKind::Text(t) = &n.kind else { continue }; + if t.file_fonts.is_none() { + continue; + } let mut fonts: Vec = vec![]; for r in &t.runs { let (family, style) = (&r.style.font_family, &r.style.font_style); @@ -130,10 +134,11 @@ pub(crate) fn keep_file_fonts(nodes: &mut [Arc], kept: &HashMap<(String, S fonts.push(FileFont { family: family.clone(), style: style.clone(), key: key.clone() }); } } - if fonts.is_empty() { - continue; - } if let NodeKind::Text(t) = &mut Arc::make_mut(n).kind { + if fonts.is_empty() { + t.file_fonts = None; + continue; + } t.file_fonts = Some(Box::new(FileFonts::new(fonts, &t.plain_text()))); db.bind_file_fonts(t, |key| images.get(key)); t.cached_bounds = Some(vectorcraft_text::layout(db, t).bounds); @@ -141,6 +146,13 @@ pub(crate) fn keep_file_fonts(nodes: &mut [Arc], kept: &HashMap<(String, S } } +/// Mark type `t` as shown in a font the file carries, so that [`keep_file_fonts`] gives it the font +/// kept for its family and style (the key is filled in then). +pub(crate) fn mark(t: &mut vectorcraft_doc::TextObject) { + let fonts = t.runs.first().map(|r| FileFont { family: r.style.font_family.clone(), style: r.style.font_style.clone(), key: String::new() }); + t.file_fonts = Some(Box::new(FileFonts::new(fonts.into_iter().collect(), &t.plain_text()))); +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/eps/src/import/tests_type1.rs b/crates/eps/src/import/tests_type1.rs index d94be757ac..500bf897eb 100644 --- a/crates/eps/src/import/tests_type1.rs +++ b/crates/eps/src/import/tests_type1.rs @@ -178,6 +178,19 @@ fn embedded_type1_type_draws_with_the_files_glyphs() { assert!(t.runs.iter().all(|r| r.style.tracking == 0.0), "{:?}", t.runs); } +/// Type in a font the file only names keeps the fallback font, and the spacing set for it, also +/// when its family and style are those of a font the file carries (`TestOne-Roman` beside the +/// embedded `TestOne`): only type shown in the carried font gets the font kept from it. +#[test] +fn a_named_font_in_a_carried_family_and_style_keeps_the_fallback_font() { + let r = program(OWN, false, "/TestOne findfont 50 scalefont setfont 10 20 moveto (A) show /TestOne-Roman 50 selectfont 10 60 moveto (AA) show"); + let t = texts(&r); + assert_eq!(t.len(), 2, "{:?}", all(&r.document)); + assert_eq!((t[1].runs[0].style.font_family.as_str(), t[1].runs[0].style.font_style.as_str()), ("Test One", "Regular")); + assert!(t[0].file_fonts.is_some()); + assert!(t[1].file_fonts.is_none(), "the named font's type isn't drawn in the carried font"); +} + /// A character the file draws with a different glyph from the one the kept font already has for /// it (here a second font of the same name whose A is a narrower square) can't go in that font: /// that string comes in as outlines, the first stays type in the file's glyphs. diff --git a/crates/eps/src/import/text.rs b/crates/eps/src/import/text.rs index e88c4d3bb9..eff4f8b20e 100644 --- a/crates/eps/src/import/text.rs +++ b/crates/eps/src/import/text.rs @@ -823,10 +823,13 @@ impl Interp<'_> { for ((d, ..), &(_, n)) in moves.iter().zip(&codes.codes) { let c: String = chars.by_ref().take(n).collect(); if painted && !c.trim().is_empty() { - let (t, ..) = self.set_text_in(&c, pen, vertical)?; + let (mut t, ..) = self.set_text_in(&c, pen, vertical)?; if draw == Draw::Path { self.type_path(&t)?; } else { + if kept { + super::file_fonts::mark(&mut t); + } let clips = self.g.clips.clone(); self.out.push(Node::new(NodeId(0), NodeKind::Text(Box::new(t))), &clips); } @@ -880,6 +883,9 @@ impl Interp<'_> { if draw == Draw::Path { self.type_path(&t)?; } else { + if kept { + super::file_fonts::mark(&mut t); + } let clips = self.g.clips.clone(); if self.out.push(Node::new(NodeId(0), NodeKind::Text(Box::new(t))), &clips).is_some() && let Some(after) = self.out.drawn.get_mut(start..)