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 <noreply@anthropic.com>
This commit is contained in:
mo
2026-11-04 23:53:42 -05:00
co-authored by Claude Opus 5.5
parent 225407ca61
commit 661e5e2dd9
2 changed files with 22 additions and 4 deletions
+4 -4
View File
@@ -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.
+18
View File
@@ -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::<usize>(), 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