address review: charge only the text block_chars scans against the text budget

This commit is contained in:
Dylan M. Taylor committed 2026-11-11 02:35:27 +00:00
1 parent 34651cb4db
commit 9e4e135971
2 files changed
+70 -10

No files matched your search

+35
View File
@@ -516,6 +516,41 @@ mod shapes {
assert!(d.warnings.iter().any(|w| w.contains("document text budget")), "{:?}", d.warnings);
}
#[test]
fn text_past_the_character_limit_is_not_charged_so_a_later_text_node_survives() {
// 64 references to one segment of 1 MiB + 1 characters: only the first is scanned (the
// block is cut at the character limit), so only that scan is charged to the text budget.
let frame = || (tag(b"TxtH"), F::Obj(tag(b"ArFr"), vec![(tag(b"FrmB"), F::F64s(vec![0.0, 0.0, 100.0, 50.0]))]));
let segment = F::Def(200, vec![tag(b"Segm")], vec![(tag(b"Utf8"), F::Str("a".repeat((1 << 20) + 1)))]);
let mut refs = vec![segment];
refs.extend((0..63).map(|_| F::Ref(200)));
let big_block = vec![(tag(b"Glyp"), F::Obj(tag(b"Glyp"), vec![(tag(b"Mixd"), F::Shared(refs))]))];
let big =
F::Def(100, vec![tag(b"TxtA")], vec![frame(), (tag(b"StSt"), F::Obj(tag(b"StSt"), vec![(tag(b"Blok"), F::Objs(tag(b"Blok"), vec![big_block]))]))]);
let run = F::Obj(tag(b"Item"), vec![]);
let attributes = F::Obj(tag(b"GAtt"), vec![(tag(b"Runs"), F::Objs(tag(b"Runs"), vec![vec![(tag(b"Indx"), F::I32(2)), (tag(b"Item"), run)]]))]);
let small_block = vec![(tag(b"Glyp"), F::Obj(tag(b"Glyp"), vec![(tag(b"Utf8"), F::Str("ok".into()))])), (tag(b"GAtt"), attributes)];
let small = F::Def(
101,
vec![tag(b"TxtA")],
vec![frame(), (tag(b"StSt"), F::Obj(tag(b"StSt"), vec![(tag(b"Blok"), F::Objs(tag(b"Blok"), vec![small_block]))]))],
);
let group = F::Def(102, vec![tag(b"Grup")], vec![(tag(b"Chld"), F::Shared(vec![F::Ref(100), F::Ref(101)]))]);
let d = read(vec![big, small, group], 102);
assert!(!d.warnings.iter().any(|w| w.contains("document text budget")), "{:?}", d.warnings);
fn collect(nodes: &[crate::model::Node], out: &mut Vec<String>) {
for n in nodes {
if let Kind::Text(t) = &n.kind {
out.push(t.runs.iter().map(|r| r.text.as_str()).collect());
}
collect(&n.children, out);
}
}
let mut texts = Vec::new();
collect(&d.spreads[0].nodes, &mut texts);
assert!(texts.iter().any(|t| t == "ok"), "{texts:?}");
}
#[test]
fn pie_angles_with_huge_finite_values_do_not_loop_or_emit_nonfinite_points() {
let document = read(vec![shape(100, b"ShPi", vec![(tag(b"AngS"), F::F64(1.0e308)), (tag(b"AngE"), F::F64(-1.0e308))])], 100);
+35 -10
View File
@@ -45,14 +45,8 @@ pub(crate) fn read(r: &mut Reader, id: ObjId, world: Affine) -> Option<Text> {
r.warn("text beyond the document text budget (paragraph alignment left out)");
}
}
// `block_chars` scans the `Utf8` string, or walks each segment's `Glys` slots (nulls included) and `Utf8` text: charge the scan first.
let slots = match s.str(glyphs, b"Utf8") {
Some(text) => text.len(),
None => s
.objs(glyphs, b"Mixd")
.iter()
.fold(0usize, |n, seg| n.saturating_add(s.array_len(*seg, b"Glys").unwrap_or(0)).saturating_add(s.str(*seg, b"Utf8").map_or(0, str::len))),
};
// `block_chars` scans the `Utf8` string, or walks each segment's `Glys` slots (nulls included) and `Utf8` text, and stops at the character limit: charge only that scan, first.
let slots = scan_cost(s, glyphs, room(total));
if !r.charge_text(slots) {
r.warn("text beyond the document text budget (truncated)");
break;
@@ -105,12 +99,43 @@ pub(crate) fn read(r: &mut Reader, id: ObjId, world: Affine) -> Option<Text> {
Some(Text { runs, align, anchor: Point { x: anchor_x, y: first_baseline }, frame: if artistic { None } else { Some(bounds) }, transform: world })
}
/// One past the allowance is enough to trip the check in `read`; `block_chars` stops there.
fn room(total: usize) -> usize {
MAX_CHARS.saturating_sub(total).saturating_add(1)
}
/// The bytes of `t` that holding `want` characters of it scans, and the characters taken.
fn prefix(t: &str, want: usize) -> (usize, usize) {
t.char_indices().enumerate().find(|(n, _)| *n == want).map_or((t.len(), t.chars().count()), |(n, (i, _))| (i, n))
}
/// What `block_chars` scans for a block, in the units `charge_text` takes: the `Utf8` bytes it
/// reads, or per segment its `Glys` slots (nulls included) and the `Utf8` bytes it reads, up to the
/// character allowance `room`; text past it is never scanned, so it is not charged.
fn scan_cost(s: &Stream, glyphs: ObjId, room: usize) -> usize {
if let Some(t) = s.str(glyphs, b"Utf8") {
return prefix(t, room).0;
}
let (mut len, mut cost) = (0usize, 0usize);
for seg in s.objs(glyphs, b"Mixd") {
if len >= room {
break;
}
let inline = s.objs(seg, b"Glys").len();
len += inline.min(room - len);
cost = cost.saturating_add(s.array_len(seg, b"Glys").unwrap_or(0));
let (bytes, chars) = prefix(s.str(seg, b"Utf8").unwrap_or_default(), room.saturating_sub(len));
len += chars;
cost = cost.saturating_add(bytes);
}
cost
}
/// The characters of one text block. A block without a UTF-8 string is a list of segments: each
/// inline object (page number, anchor) takes one `U+FFFC` before its segment's text. Stops once
/// `total` plus these characters would pass the cap.
fn block_chars(s: &Stream, glyphs: ObjId, total: usize, placeholders: &mut bool) -> Vec<char> {
// One past the allowance is enough to trip the check in `read`; both arms stop there.
let room = MAX_CHARS.saturating_sub(total).saturating_add(1);
let room = room(total);
match s.str(glyphs, b"Utf8") {
Some(t) => t.chars().take(room).collect(),
None => {