Skip to main content

inset_services/
text_layout_metrics.rs

1//! Flutter counterpart: `services/text_layout_metrics.dart`.
2//!
3//! The instance methods (`getLineAtOffset` and friends) live on `RenderEditable`;
4//! they need the arena. The statics stay here.
5
6/// Static classifiers from Flutter's `TextLayoutMetrics`.
7pub struct TextLayoutMetrics;
8
9impl TextLayoutMetrics {
10    // TODO(gspencergoog): replace when we expose this ICU information.
11    /// Check if the given code unit is a white space or separator
12    /// character.
13    ///
14    /// Includes newline characters from ASCII and separators from the
15    /// [unicode separator category](https://www.compart.com/en/unicode/category/Zs)
16    pub fn is_whitespace(code_unit: i32) -> bool {
17        matches!(
18            code_unit,
19            0x9 | // horizontal tab
20            0xA | // line feed
21            0xB | // vertical tab
22            0xC | // form feed
23            0xD | // carriage return
24            0x1C | // file separator
25            0x1D | // group separator
26            0x1E | // record separator
27            0x1F | // unit separator
28            0x20 | // space
29            0xA0 | // no-break space
30            0x1680 | // ogham space mark
31            0x2000 | // en quad
32            0x2001 | // em quad
33            0x2002 | // en space
34            0x2003 | // em space
35            0x2004 | // three-per-em space
36            0x2005 | // four-er-em space
37            0x2006 | // six-per-em space
38            0x2007 | // figure space
39            0x2008 | // punctuation space
40            0x2009 | // thin space
41            0x200A | // hair space
42            0x202F | // narrow no-break space
43            0x205F | // medium mathematical space
44            0x3000 // ideographic space
45        )
46    }
47
48    /// Check if the given code unit is a line terminator character.
49    ///
50    /// Includes newline characters from ASCII
51    /// (https://www.unicode.org/standard/reports/tr13/tr13-5.html).
52    pub fn is_line_terminator(code_unit: i32) -> bool {
53        matches!(
54            code_unit,
55            0x0A | // line feed
56            0x0B | // vertical feed
57            0x0C | // form feed
58            0x0D | // carriage return
59            0x85 | // new line
60            0x2028 | // line separator
61            0x2029 // paragraph separator
62        )
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn whitespace_and_line_terminators_match_dart() {
72        assert!(TextLayoutMetrics::is_whitespace(0x20));
73        assert!(TextLayoutMetrics::is_whitespace(0xA));
74        assert!(!TextLayoutMetrics::is_whitespace(b'a' as i32));
75        assert!(TextLayoutMetrics::is_line_terminator(0x0A));
76        assert!(TextLayoutMetrics::is_line_terminator(0x2028));
77        assert!(!TextLayoutMetrics::is_line_terminator(0x20));
78    }
79}