From 661e5e2dd9ffccbe79cfe86e3a67da9e1344982b Mon Sep 17 00:00:00 2001 From: mo <131036780+mbolling@users.noreply.github.com> Date: Wed, 4 Nov 2026 23:36:37 -0500 Subject: [PATCH] SHX fonts: bound a glyph to 8,192 points and 32,768 steps Every character of a text may draw a glyph's full budget, so the budget is what a small hostile font multiplies: with 50,000 points and 200,000 steps an 11 KB font drew a 1,000-character line in 3 s and 1.7 GB, and a 21 KB one took 6.4 s to measure 2,000 characters. The new bounds still leave 128 full circles or 16 shapes of the format's 2,000 bytes per glyph. Co-Authored-By: Claude Opus 5.5 --- crates/fonts/src/shx/program.rs | 8 ++++---- crates/fonts/src/shx/tests.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/crates/fonts/src/shx/program.rs b/crates/fonts/src/shx/program.rs index c2b8d9f6c..469f18cf0 100644 --- a/crates/fonts/src/shx/program.rs +++ b/crates/fonts/src/shx/program.rs @@ -25,10 +25,10 @@ use super::{ShxFont, ShxGlyph, ShxKind}; const MAX_DEPTH: usize = 16; /// Location stack (the format promises four). const MAX_STACK: usize = 64; -/// Bytes run per glyph, subshapes included. -const MAX_STEPS: usize = 200_000; -/// Points per glyph. -const MAX_POINTS: usize = 50_000; +/// Bytes run per glyph, subshapes included (16 shapes of the format's 2,000 bytes). +const MAX_STEPS: usize = 32_768; +/// Points per glyph (128 full circles): every character of a text may draw this many. +const MAX_POINTS: usize = 8_192; /// Coordinates in font units. const MAX_COORD: f64 = 1.0e7; /// Arc segments per full turn. diff --git a/crates/fonts/src/shx/tests.rs b/crates/fonts/src/shx/tests.rs index 1b242220f..999a1a652 100644 --- a/crates/fonts/src/shx/tests.rs +++ b/crates/fonts/src/shx/tests.rs @@ -323,6 +323,24 @@ fn a_far_extended_subshape_cell_is_cut_off() { assert!(close(f.glyph(0x82A1, false).unwrap().advance, Vec2::new(255.0, 0.0))); } +#[test] +fn subshape_fan_out_past_the_point_and_step_bounds_is_cut_off() { + // Hostile: a few hundred bytes of fan-out drew 48,000 points per character (1.7 GB for a + // 1,000-character text) or ran 200,000 steps per character with the pen up. + let src = format!( + "*1,1,TOO_MANY_POINTS\n{a}0\n*2,1,HUNDRED\n{b}0\n*3,3,V\n011,019,0\n*4,1,ENOUGH\n{c}0\n*5,1,TOO_MANY_STEPS\n{d}0\n*6,1,UP\n{e}0\n*7,3,U\n2,010,0\n", + a = "7,2,".repeat(41), + b = "7,3,".repeat(100), + c = "7,2,".repeat(40), + d = "7,6,".repeat(100), + e = "7,7,".repeat(100), + ); + let f = font(&src); + assert!(f.glyph(1, false).is_none() && f.glyph(5, false).is_none()); + assert_eq!(f.glyph(4, false).unwrap().strokes.iter().map(Vec::len).sum::(), 8001); + assert!(f.glyph(6, false).is_some()); +} + #[test] fn a_doubled_minus_sign_is_not_a_number() { // Hostile: the digits after the sign were themselves negative, down to i64::MIN; as a big