mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 02:03:02 +00:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -590,7 +590,7 @@ pub fn shape_key(path: &BezPath) -> Option<u64> {
|
||||
|
||||
/// `path` read for matching: one reading per choice of starting corners (the corner of each contour
|
||||
/// farthest from the centre of all the shape's corners; every tied corner when `all_ties`, else the
|
||||
/// first), at most [`MAX_VARIANTS`]. Empty for a shape with nothing to match.
|
||||
/// first), at most [`MAX_VARIANTS`] tried. Empty for a shape with nothing to match.
|
||||
fn signatures(path: &BezPath, all_ties: bool) -> Vec<Signature> {
|
||||
let bounds = path.bounding_box();
|
||||
let size = bounds.width().hypot(bounds.height());
|
||||
@@ -629,10 +629,14 @@ fn signatures(path: &BezPath, all_ties: bool) -> Vec<Signature> {
|
||||
.collect();
|
||||
let mut out = vec![];
|
||||
let mut pick = vec![0usize; cs.len()];
|
||||
// Readings tried, kept or not: a shape whose readings all fail (its sums run past what f64
|
||||
// holds) tries no more than this either.
|
||||
let mut tried = 0usize;
|
||||
loop {
|
||||
if out.len() >= MAX_VARIANTS {
|
||||
if tried >= MAX_VARIANTS {
|
||||
break;
|
||||
}
|
||||
tried += 1;
|
||||
if let Some(sig) = sample(&cs, &starts, &pick, accuracy) {
|
||||
out.push(sig);
|
||||
}
|
||||
@@ -870,6 +874,24 @@ mod tests {
|
||||
assert!(outlines_agree(&out, &out, tol));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn readings_that_all_fail_stop_at_the_cap() {
|
||||
// 48 concentric squares (four tied corners each: 4^48 choices) so large that every reading
|
||||
// overflows: the tries stop at the cap instead of running through the choices.
|
||||
let mut p = BezPath::new();
|
||||
for i in 1..=48 {
|
||||
let r = 1e153 * f64::from(i);
|
||||
p.move_to((-r, -r));
|
||||
p.line_to((r, -r));
|
||||
p.line_to((r, r));
|
||||
p.line_to((-r, r));
|
||||
p.close_path();
|
||||
}
|
||||
let t0 = std::time::Instant::now();
|
||||
assert!(signatures(&p, true).is_empty());
|
||||
assert!(t0.elapsed() < std::time::Duration::from_secs(5), "{:?}", t0.elapsed());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_same_corners_joined_another_way_are_another_outline() {
|
||||
let poly = |pts: &[(f64, f64)]| {
|
||||
|
||||
Reference in New Issue
Block a user