Print: type left out for the page limit sends none of its glyphs

When an object passes the page body limit, the walk takes its body,
image placements and image codes back out. The glyphs it showed through
the job's fonts stayed in the page's font list, so Subset sent fonts for
type that was not printed, and Complete could send whole fonts for a face
no printed object uses. The walk now records the glyphs each object adds
and takes them back out with the rest.

Tests: the glyphs of an object left out for the limit are not in the
page's font list, and ruby readings print as outlines in place while the
base text shows through the font.
This commit is contained in:
dotCooCoo
2026-11-22 11:52:43 -08:00
parent 23ad161d30
commit f7a5e3991d
2 changed files with 73 additions and 2 deletions
+52
View File
@@ -612,3 +612,55 @@ fn complete_large_font_fails_before_partial_output() {
job.define(&used, &mut s).unwrap();
assert_eq!(procs(&s), face.glyph_count() as usize);
}
#[test]
fn type_left_out_for_the_page_limit_sends_none_of_its_glyphs() {
let family = vectorcraft_text::FALLBACK_FAMILY;
let face = face_of(family);
let kept = typed("A", family);
let mut both = kept.clone();
let layer = both.layers[0].id;
let style = CharStyle { font_family: family.into(), size: 24.0, fill: Paint::solid(Color::BLACK), ..CharStyle::default() };
let n = Node::new(both.alloc_id(), NodeKind::Text(Box::new(TextObject::point(Point::new(20.0, 90.0), "BCDEFGH", style))));
both.insert(Some(layer), 1, n).unwrap();
let o = EpsOptions { region: kept.artboards[0].rect, ..EpsOptions::default() };
let walk = |d: &Document, max: usize| {
let job = RefCell::new(fonts::FontJob::new(PrintFonts::Subset));
crate::scene::Scene::new(d, &o).with_fonts(Some(&job)).with_max_body(max).run()
};
let alone = walk(&kept, usize::MAX);
assert_eq!(alone.fonts.values().map(|g| g.len()).sum::<usize>(), 1, "{:?}", alone.fonts);
// The second object passes the limit and is left out: only the glyph of the first is used.
let page = walk(&both, alone.body.len() + 10);
assert!(page.warnings.iter().any(|w| w.contains("too large")), "{:?}", page.warnings);
assert_eq!(page.body, alone.body);
assert_eq!(page.fonts, alone.fonts);
assert!(page.fonts.values().all(|g| g.contains(&face.glyph_for('A'))), "{:?}", page.fonts);
}
#[test]
fn ruby_prints_as_outlines_where_the_canvas_draws_it() {
let family = vectorcraft_text::FALLBACK_FAMILY;
let mut d = Document::new(200.0, 100.0);
let layer = d.layers[0].id;
let style = CharStyle { font_family: family.into(), size: 24.0, fill: Paint::solid(Color::BLACK), ..CharStyle::default() };
let mut t = TextObject::point(Point::new(20.0, 60.0), "AB", style);
t.ruby = vec![vectorcraft_doc::RubySpan::new(0, 2, "xy")];
let n = Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)));
d.insert(Some(layer), 0, n).unwrap();
let area = Rect::new(36.0, 36.0, 236.0, 136.0);
let drawn = |fonts| {
let out = print(&[page(&d, None)], &[0], &fonts_job(fonts)).unwrap();
(text(&out), render_region(&crate::import(&out.bytes).unwrap().document, area, 2.0))
};
let (_, outlined) = drawn(PrintFonts::Outlines);
for fonts in [PrintFonts::Subset, PrintFonts::Complete] {
let (ps, shown) = drawn(fonts);
// The base text shows through the font; its reading is drawn as outlines, in place.
assert_eq!(ps.matches("> show").count(), 2, "{fonts:?}: {ps}");
if fonts == PrintFonts::Subset {
assert_eq!(procs(&ps), 2, "{ps}");
}
vectorcraft_testkit::raster::assert_similar(&outlined, &shown, 24.0, 0.002);
}
}
+21 -2
View File
@@ -296,6 +296,9 @@ pub(crate) struct Scene<'a> {
fonts: Option<&'a RefCell<FontJob>>,
/// The glyphs shown through `fonts`.
used: UsedGlyphs,
/// The glyphs `used` gained, in the order it gained them: an object left out for the page
/// limit takes its own back out of `used` ([`Self::forget_glyphs`]).
used_order: Vec<(usize, u32)>,
}
impl<'a> Scene<'a> {
@@ -328,6 +331,7 @@ impl<'a> Scene<'a> {
keep_below: false,
fonts: None,
used: UsedGlyphs::new(),
used_order: vec![],
}
}
@@ -561,7 +565,7 @@ impl<'a> Scene<'a> {
_ => {}
}
self.note_losses(n);
let (was, placed, codes) = (self.body.len(), self.placements.len(), self.codes.len());
let (was, placed, codes, glyphs) = (self.body.len(), self.placements.len(), self.codes.len(), self.used_order.len());
match &n.kind {
NodeKind::Layer { children, clip: false, .. } | NodeKind::Group { children, clip: false } => {
children.iter().for_each(|c| self.node(c, false));
@@ -622,10 +626,23 @@ impl<'a> Scene<'a> {
self.body.truncate(was);
self.unplace(placed);
self.forget_codes(codes);
self.forget_glyphs(glyphs);
self.warn(TOO_LARGE);
}
}
/// Take the glyphs `used` gained after its first `n` back out of it.
fn forget_glyphs(&mut self, n: usize) {
for (face, gid) in self.used_order.drain(n.min(self.used_order.len())..) {
if let Some(gids) = self.used.get_mut(&face) {
gids.remove(&gid);
if gids.is_empty() {
self.used.remove(&face);
}
}
}
}
/// How long the page body will be: what is written and what its image placements add.
fn size(&self) -> usize {
self.body.len().saturating_add(self.pending)
@@ -993,7 +1010,9 @@ impl<'a> Scene<'a> {
return None;
}
let (face, upem) = fonts.borrow_mut().glyph(g.font_id, g.gid, g.outline.elements().len())?;
self.used.entry(face).or_default().insert(g.gid);
if self.used.entry(face).or_default().insert(g.gid) {
self.used_order.push((face, g.gid));
}
Some((face, upem))
}