From de80ee24142d2a7870a735981364718785aa840a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 6 Dec 2026 05:23:08 +0100 Subject: [PATCH] thing2(text): verify per-cluster CellText idempotence in fuzz test --- crates/thing-core/src/text/cells.rs | 7 ++++--- crates/thing-core/tests/cells.rs | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/thing-core/src/text/cells.rs b/crates/thing-core/src/text/cells.rs index cc34d19..cfac4b6 100644 --- a/crates/thing-core/src/text/cells.rs +++ b/crates/thing-core/src/text/cells.rs @@ -2,9 +2,10 @@ //! one `Measure` for Cells. Every width and every break a cell backend advances by is decided //! here, per extended grapheme cluster, and no backend computes either. //! -//! Order is sanitize, then segment and measure: the text is cleaned (controls become U+FFFD, -//! clusters without width are dropped) until segmenting it again changes nothing, so every width -//! recorded is the width of exactly the text a terminal receives. +//! Order is sanitize, then segment and measure: the text is cleaned once (controls become U+FFFD, +//! zero-width clusters are dropped while coalescing non-RI extensions across dropped boundaries) +//! and `emit` builds `CellText` directly from those kept clusters, so every width and break is +//! owned once and every emitted cluster re-segments to itself. //! //! Breaking is CSS `white-space: pre-wrap` with `overflow-wrap: break-word`: mandatory breaks //! always break, soft breaks are UAX #14 Allowed positions on a cluster boundary, trailing spaces diff --git a/crates/thing-core/tests/cells.rs b/crates/thing-core/tests/cells.rs index 16eeb95..2eade32 100644 --- a/crates/thing-core/tests/cells.rs +++ b/crates/thing-core/tests/cells.rs @@ -249,26 +249,35 @@ fn fuzz_never_emits_a_control() { for line in layout_lines(&text, wrap, None, width) { assert!(!line.chars().any(|c| c.is_control() || matches!(c, '\u{2028}' | '\u{2029}')), "{text:?} -> {line:?}"); } - // What is emitted measures to what was recorded: measuring it again changes nothing. + // What is emitted is clean and each emitted cluster re-segments to itself with the same width. let node = NodeId::new("n"); let request = TextRequest { node: &node, text: &text, style_token: "body", wrap, max_lines: None, geometry: false }; for line in CellMeasure.text(&request, width).lines { assert!(line.stops.is_empty()); let Run::Cells { text: emitted, .. } = line.run else { panic!() }; - assert_eq!(CellText::new(emitted.as_str()), emitted, "{text:?} at {width}"); - assert!(emitted.clusters().all(|(_, w)| w >= 1)); + assert_clean_cell_text(&emitted, &text, width); } if round % 20 == 0 { check_geometry(&text, wrap, None, width); } let lone = CellText::new(&text); - assert_eq!(CellText::new(lone.as_str()), lone, "{text:?}"); + assert_clean_cell_text(&lone, &text, width); let _ = leaf_measure(&text, wrap, Some(3), Avail::Definite(width)); let _ = leaf_measure(&text, wrap, None, Avail::MinContent); assert!(!CellText::new(&text).as_str().chars().any(|c| c.is_control())); } } +fn assert_clean_cell_text(ct: &CellText, raw: &str, width: f32) { + let re = CellText::new(ct.as_str()); + assert_eq!(re.as_str(), ct.as_str(), "{raw:?} at {width}"); + assert_eq!(re.width(), ct.width(), "{raw:?} at {width}"); + for (cluster, w) in ct.clusters() { + assert!(w >= 1, "{raw:?} at {width}"); + assert_eq!(CellText::new(cluster).clusters().collect::>(), [(cluster, w)], "{raw:?} at {width}"); + } +} + fn check_geometry(text: &str, wrap: bool, max_lines: Option, width: f32) { let node = NodeId::new("n"); let off = CellMeasure.text(&TextRequest { node: &node, text, style_token: "body", wrap, max_lines, geometry: false }, width);