M7/G4: retype bounds readings tried and glyphs laid out (#4934)

Readings of a shape are capped by those tried, not those kept, so a shape
whose every reading fails stops at the cap. The layout check grows a
window from each line start, doubling while the type draws the outlines,
and starts the next line at a mismatch, so a line split at every glyph
costs about twice its glyphs, not half their square; a glyph budget
leaves anything past it as outlines. A tracked line of 450 glyphs: 259 ms.
This commit is contained in:
ruchirguitar
2026-11-18 19:39:40 +05:30
parent 5e5003507e
commit 4fe27f830c
3 changed files with 95 additions and 24 deletions
+52 -22
View File
@@ -299,31 +299,61 @@ impl Line {
/// `lines` checked against their own layout: a line splits where its type stops drawing the
/// outlines, and a glyph its type cannot draw even on its own stays an outline (`unmatched`).
/// The layouts are bounded by the number of glyphs.
/// The glyphs laid out are bounded by a multiple of the number of glyphs.
fn verified(lines: Vec<Line>, system_layout: bool, unmatched: &mut Vec<NodeId>) -> Vec<Line> {
let mut budget = lines.iter().map(|l| l.pieces.len() * 2 + 2).sum::<usize>();
let mut todo: std::collections::VecDeque<Line> = lines.into();
// Glyphs laid out in all, at most: past it, the glyphs left unchecked stay outlines.
let mut budget = lines.iter().map(|l| l.pieces.len()).sum::<usize>().saturating_mul(8).saturating_add(256);
let mut out = vec![];
while let Some(l) = todo.pop_front() {
if budget == 0 {
// Unchecked glyphs stay outlines.
unmatched.extend(l.pieces);
continue;
}
budget -= 1;
for l in lines {
let n = l.pieces.len();
match l.first_mismatch(&l.text_object(system_layout)) {
None => out.push(l),
// The first glyph on its own may still be drawn as it was (a contextual form changed it).
Some(0) if n > 1 => {
for part in [l.part(1, n), l.part(0, 1)].into_iter().flatten() {
todo.push_front(part);
}
}
Some(0) => unmatched.extend(l.pieces.first().copied()),
Some(k) => {
for part in [l.part(k, n), l.part(0, k)].into_iter().flatten() {
todo.push_front(part);
// Each new line starts at `s`. Its type is laid out over a window from `s` that doubles
// while the type draws the window's glyphs as the outlines do, so finding where it stops
// costs about twice the glyphs before that point, not the whole rest of the line each time.
let mut s = 0;
while s < n {
let mut w = 8usize;
loop {
let e = n.min(s.saturating_add(w));
let Some(part) = l.part(s, e) else {
s = n;
break;
};
let Some(left) = budget.checked_sub(e - s) else {
// Unchecked glyphs stay outlines.
unmatched.extend(l.pieces.get(s..).unwrap_or_default().iter().copied());
s = n;
break;
};
budget = left;
match part.first_mismatch(&part.text_object(system_layout)) {
// The rest of the line is drawn as it was: one object.
None if e == n => {
out.push(part);
s = n;
break;
}
None => w = w.saturating_mul(2),
// The first glyph on its own may still be drawn as it was (a contextual form
// changed it in the line); else it stays an outline.
Some(0) => {
let alone = if e - s > 1 { l.part(s, s + 1) } else { Some(part) };
match alone {
Some(a) if budget > 0 && a.first_mismatch(&a.text_object(system_layout)).is_none() => {
budget -= 1;
out.push(a);
}
_ => unmatched.extend(l.pieces.get(s).copied()),
}
s += 1;
break;
}
// The glyphs before the mismatch are drawn as they were: one object; the next
// line starts at the mismatch.
Some(k) => {
out.extend(l.part(s, s + k));
s += k;
break;
}
}
}
}
+19
View File
@@ -518,3 +518,22 @@ fn timing_with_hundreds_of_cjk_outlines() {
s.execute("edit.undo", &json!({})).unwrap();
}
}
#[test]
fn a_long_tracked_line_is_checked_in_time() {
// Tracking splits the line at every glyph: the layout check must not lay out the rest of the
// line again after each split.
let text = "abe".repeat(150);
let mut s = session();
let id = s.execute("text.create", &json!({"x": 10, "y": 100, "text": text, "size": 4})).unwrap()["id"].as_u64().unwrap();
s.execute("text.setRangeStyle", &json!({"id": id, "start": 0, "end": text.len(), "tracking": 50})).unwrap();
s.execute("select.set", &json!({"ids": [id]})).unwrap();
let g = s.execute("type.createOutlines", &json!({})).unwrap()["ids"][0].as_u64().unwrap();
s.execute("select.set", &json!({"ids": [g]})).unwrap();
let t0 = std::time::Instant::now();
let r = s.execute("type.retype", &json!({})).unwrap();
let texts: Vec<String> = r["texts"].as_array().unwrap().iter().map(|t| t.as_str().unwrap().to_owned()).collect();
assert_eq!(texts.concat(), text);
assert_eq!(r["unmatched"], json!([]));
eprintln!("450 tracked glyphs: {:?}, {} objects", t0.elapsed(), texts.len());
}