Truly independent web browser
LibWeb: Find Latin grapheme cluster boundaries without ICU
Andreas Klingb39b6df2823e
diff --git a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs b/Libraries/LibWeb/Rust/src/layout/text_chunker.rsindex e012f957968..9105d02104d 100644--- a/Libraries/LibWeb/Rust/src/layout/text_chunker.rs+++ b/Libraries/LibWeb/Rust/src/layout/text_chunker.rs@@ -211,22 +211,62 @@ impl Drop for IcuSegmenterHandle {}pub(crate) enum GraphemeSegmenter {- Ascii { length_in_code_units: usize },- Icu(IcuSegmenterHandle),+ Ascii {+ length_in_code_units: usize,+ },+ /// Text of code points below U+0300 only, where every code unit is a grapheme cluster except for CR LF.+ Latin {+ length_in_code_units: usize,+ /// Where each LF that follows a CR is, in increasing order.+ line_feeds_after_carriage_returns: Box<[usize]>,+ },+ Icu {+ handle: IcuSegmenterHandle,+ length_in_code_units: usize,+ /// One bit per code unit: whether it is a code point below U+0300 other than CR.+ latin_code_units: Box<[u64]>,+ },+}++// No code point below U+0300 extends or joins a grapheme cluster, and only CR joins the code point after it (CR LF).+fn is_latin_code_unit_outside_clusters(code_unit: u16) -> bool {+ code_unit < 0x300 && code_unit != 0x0d}impl GraphemeSegmenter {pub(crate) fn new(text: &[u16]) -> Self {- if text.iter().all(|unit| *unit <= 0x7f) {+ let Some(first_non_ascii) = text.iter().position(|unit| *unit > 0x7f) else {return Self::Ascii {length_in_code_units: text.len(),};+ };+ if text[first_non_ascii..].iter().all(|unit| *unit < 0x300) {+ let line_feeds_after_carriage_returns = text+ .windows(2)+ .enumerate()+ .filter(|(_, pair)| *pair == [0x0d, 0x0a])+ .map(|(index, _)| index + 1)+ .collect();+ return Self::Latin {+ length_in_code_units: text.len(),+ line_feeds_after_carriage_returns,+ };+ }+ let mut latin_code_units = vec![0u64; text.len().div_ceil(64)].into_boxed_slice();+ for (index, code_unit) in text.iter().enumerate() {+ if is_latin_code_unit_outside_clusters(*code_unit) {+ latin_code_units[index / 64] |= 1 << (index % 64);+ }}// SAFETY: The chunker borrows the text for its whole lifetime and the// segmenter handle is dropped with the chunker, so the ICU segmenter// never outlives the buffer it references.let raw = unsafe { unicode_layout_grapheme_segmenter_create(text.as_ptr(), text.len()) };- Self::Icu(IcuSegmenterHandle { raw })+ Self::Icu {+ handle: IcuSegmenterHandle { raw },+ length_in_code_units: text.len(),+ latin_code_units,+ }}/// Mirrors Unicode::AsciiGraphemeSegmenter for the ASCII case: every code@@ -242,7 +282,40 @@ impl GraphemeSegmenter {}Some(index + 1)}- Self::Icu(handle) => handle.next_boundary(index, inclusive),+ Self::Latin {+ length_in_code_units,+ line_feeds_after_carriage_returns,+ } => {+ let is_boundary = |index: usize| line_feeds_after_carriage_returns.binary_search(&index).is_err();+ if inclusive && index <= *length_in_code_units && is_boundary(index) {+ return Some(index);+ }+ if index >= *length_in_code_units {+ return None;+ }+ (index + 1..=*length_in_code_units).find(|&boundary| is_boundary(boundary))+ }+ Self::Icu {+ handle,+ length_in_code_units,+ latin_code_units,+ } => {+ // OPTIMIZATION: Two code points below U+0300 have a grapheme cluster boundary between them, unless the+ // first one is CR. So do such a code point and the end of the text.+ let is_latin = |index: usize| latin_code_units[index / 64] & (1 << (index % 64)) != 0;+ let length = *length_in_code_units;+ let boundary = if inclusive {+ (index > 0 && index < length && is_latin(index - 1) && is_latin(index)).then_some(index)+ } else {+ (index < length && is_latin(index) && (index + 1 == length || is_latin(index + 1)))+ .then_some(index + 1)+ };+ if boundary.is_some() {+ debug_assert_eq!(boundary, handle.next_boundary(index, inclusive));+ return boundary;+ }+ handle.next_boundary(index, inclusive)+ }}}}