thing2(text): unify cluster coalescing in clean and emit directly from atoms

This commit is contained in:
Meta Vens 2026-12-06 05:21:20 +01:00
commit d43a41c245
2 changed files with 66 additions and 25 deletions

View file

@ -99,10 +99,14 @@ struct Cleaned {
/// Sanitize each source extended-grapheme cluster without re-segmenting across dropped zero-width
/// clusters: every control (a tab excepted) becomes U+FFFD and every zero-width cluster is dropped
/// (its soft-break opportunity rides on the next kept cluster). A following cluster merges into the
/// previous kept one only when concatenating them collapses their width into a single cluster
/// (e.g. a modifier across a dropped bidi control), never splitting or re-pairing Regional
/// Indicators across a dropped boundary.
/// (its soft-break opportunity rides on the next kept cluster). A following cluster coalesces into
/// the previous kept one when separated by a dropped cluster and concatenating them forms one
/// extended grapheme cluster (e.g. a modifier or ZWJ sequence across a dropped bidi control), never
/// pairing Regional Indicators across a dropped boundary.
fn is_ri_cluster(s: &str) -> bool {
s.chars().next().is_some_and(|c| ('\u{1f1e6}'..='\u{1f1ff}').contains(&c))
}
fn clean(paragraph: &str) -> Cleaned {
let mut open = vec![false; paragraph.len() + 1];
for (at, kind) in linebreaks(paragraph) {
@ -133,16 +137,18 @@ fn clean(paragraph: &str) -> Cleaned {
let merged = if dropped {
if let Some(prev) = entries.last_mut() {
let prev_slice = &buf[prev.start..prev.end];
let prev_width = prev_slice.width();
let piece_width = piece.width();
buf.push_str(piece);
let combined = &buf[prev.start..];
if combined.graphemes(true).count() == 1 && combined.width() != prev_width + piece_width {
prev.end = buf.len();
prev.orig_end = orig_end;
true
if !(is_ri_cluster(prev_slice) && is_ri_cluster(piece)) {
buf.push_str(piece);
let combined = &buf[prev.start..];
if combined.graphemes(true).count() == 1 {
prev.end = buf.len();
prev.orig_end = orig_end;
true
} else {
buf.truncate(prev.end);
false
}
} else {
buf.truncate(prev.end);
false
}
} else {
@ -162,8 +168,8 @@ fn clean(paragraph: &str) -> Cleaned {
orig_start: start as u32,
orig_end,
});
dropped = false;
}
dropped = false;
}
if let Some(first) = entries.first_mut() {
first.open = false;
@ -192,26 +198,24 @@ fn atoms(cleaned: &Cleaned, base: usize) -> Vec<Atom<'_>> {
.collect()
}
/// One line as terminal text: tabs become spaces to the next stop from the line's start, and the
/// result is cleaned and segmented once more, so its recorded widths are those of the emitted text.
/// One line as terminal text: tabs become spaces to the next stop from the line's start, and each
/// kept atom carries the cluster and width already decided by `clean`.
fn emit(line: &[Atom]) -> CellText {
let mut text = String::new();
let mut out = CellText::default();
let mut column = 0;
for atom in line {
let cols = atom.cols(column);
if atom.tab {
text.extend(std::iter::repeat_n(' ', cols as usize));
for _ in 0..cols {
out.text.push(' ');
out.clusters.push((out.text.len() as u32, 1));
}
} else {
text.push_str(atom.text);
out.text.push_str(atom.text);
out.clusters.push((out.text.len() as u32, atom.width));
}
column += cols;
}
let cleaned = clean(&text);
let mut out = CellText::default();
for cluster in cleaned.buf.graphemes(true) {
out.text.push_str(cluster);
out.clusters.push((out.text.len() as u32, cluster.width().min(255) as u8));
}
out
}

View file

@ -515,6 +515,43 @@ fn ri_does_not_re_pair_across_dropped_cluster() {
assert_eq!(pair_wrapped.lines[0].stops, [Stop { byte: 0, x: 0.0 }, Stop { byte: 4, x: 1.0 }]);
assert_eq!(pair_wrapped.lines[1].source, (7, 11));
assert_eq!(pair_wrapped.lines[1].stops, [Stop { byte: 7, x: 0.0 }, Stop { byte: 11, x: 1.0 }]);
assert_eq!(&CellText::new(text), full);
}
#[test]
fn zwj_coalesces_across_dropped_cluster() {
let node = NodeId::new("n");
let cases = [
("\u{1f469}\u{200b}\u{1f3fd}\u{200d}\u{1f469}", "\u{1f469}\u{1f3fd}\u{200d}\u{1f469}"),
("\u{1f469}\u{202c}\u{1f3fd}\u{200d}\u{1f469}", "\u{1f469}\u{1f3fd}\u{200d}\u{1f469}"),
("\u{1f469}\u{200b}\u{1f3fb}\u{200d}\u{1f468}\u{200d}\u{1f467}", "\u{1f469}\u{1f3fb}\u{200d}\u{1f468}\u{200d}\u{1f467}"),
];
for (text, expected) in cases {
check_geometry(text, false, None, 80.0);
check_geometry(text, true, None, 2.0);
let unwrapped = CellMeasure.text(&TextRequest { node: &node, text, style_token: "body", wrap: false, max_lines: None, geometry: true }, 80.0);
assert_eq!(unwrapped.lines.len(), 1, "{text:?}");
let Run::Cells { text: emitted, .. } = &unwrapped.lines[0].run else { panic!() };
assert_eq!(emitted.as_str(), expected, "{text:?}");
assert_eq!(emitted.width(), 2, "{text:?}");
assert_eq!(&CellText::new(text), emitted, "{text:?}");
for stop in &unwrapped.lines[0].stops {
assert_eq!(stop.x, CellText::new(&text[..stop.byte as usize]).width() as f32, "{text:?} at {}", stop.byte);
}
let wrapped = CellMeasure.text(&TextRequest { node: &node, text, style_token: "body", wrap: true, max_lines: None, geometry: true }, 2.0);
assert_eq!(wrapped.lines.len(), 1, "{text:?}");
let Run::Cells { text: w_emitted, .. } = &wrapped.lines[0].run else { panic!() };
assert_eq!(w_emitted, emitted, "{text:?}");
let min = leaf_measure(text, true, None, Avail::MinContent);
let max = leaf_measure(text, true, None, Avail::MaxContent);
assert!(min.0 <= max.0, "{text:?}: min={min:?} max={max:?}");
assert_eq!(min, (2.0, 1.0), "{text:?}");
assert_eq!(max, (2.0, 1.0), "{text:?}");
}
}
#[test]