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.
This commit is contained in:
JonRC
2026-11-23 00:53:54 -03:00
parent 104197e298
commit 610849efa3
3 changed files with 38 additions and 7 deletions
+18 -6
View File
@@ -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<Node>], kept: &HashMap<(String, String), String>, images: &BTreeMap<String, ImageBlob>) {
if kept.is_empty() {
return;
@@ -121,6 +122,9 @@ pub(crate) fn keep_file_fonts(nodes: &mut [Arc<Node>], 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<FileFont> = 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<Node>], 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<Node>], 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::*;
+13
View File
@@ -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.
+7 -1
View File
@@ -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..)