i_slint_core/textlayout/
glyphclusters.rs1use core::{marker::PhantomData, ops::Range};
5
6use euclid::num::Zero;
7
8use super::ShapeBuffer;
9
10#[derive(Clone)]
11pub struct GlyphCluster<Length: Clone> {
12 pub byte_range: Range<usize>,
13 pub glyph_range: Range<usize>,
14 pub width: Length,
15 pub is_whitespace: bool,
16 pub is_line_or_paragraph_separator: bool,
17}
18
19#[derive(Clone)]
20pub struct GlyphClusterIterator<'a, Length> {
21 text: &'a str,
22 shaped_text: &'a ShapeBuffer<Length>,
23 current_run: usize,
24 byte_offset: usize,
26 glyph_index: usize,
27 marker: PhantomData<Length>,
28}
29
30impl<'a, Length> GlyphClusterIterator<'a, Length> {
31 pub fn new(text: &'a str, shaped_text: &'a ShapeBuffer<Length>) -> Self {
32 Self {
33 text,
34 shaped_text,
35 current_run: 0,
36 byte_offset: 0,
37 glyph_index: 0,
38 marker: Default::default(),
39 }
40 }
41}
42
43impl<Length: Copy + Clone + Zero + core::ops::AddAssign> Iterator
44 for GlyphClusterIterator<'_, Length>
45{
46 type Item = GlyphCluster<Length>;
47
48 fn next(&mut self) -> Option<Self::Item> {
49 if self.current_run >= self.shaped_text.text_runs.len() {
50 return None;
51 }
52
53 let current_run =
54 if self.byte_offset < self.shaped_text.text_runs[self.current_run].byte_range.end {
55 &self.shaped_text.text_runs[self.current_run]
56 } else {
57 self.current_run += 1;
58 self.shaped_text.text_runs.get(self.current_run)?
59 };
60
61 let mut cluster_width: Length = Length::zero();
62
63 let cluster_start = self.glyph_index;
64
65 let mut cluster_byte_offset;
66 loop {
67 let glyph = &self.shaped_text.glyphs[self.glyph_index];
68 cluster_byte_offset = current_run.byte_range.start + glyph.text_byte_offset;
70 if cluster_byte_offset != self.byte_offset {
71 break;
72 }
73 cluster_width += glyph.advance;
74
75 self.glyph_index += 1;
76
77 if self.glyph_index >= current_run.glyph_range.end {
78 cluster_byte_offset = current_run.byte_range.end;
79 break;
80 }
81 }
82 let byte_range = self.byte_offset..cluster_byte_offset;
83 let (is_whitespace, is_line_or_paragraph_separator) = self.text[self.byte_offset..]
84 .chars()
85 .next()
86 .map(|ch| {
87 let is_line_or_paragraph_separator =
88 ch == '\n' || ch == '\u{2028}' || ch == '\u{2029}';
89 (ch.is_whitespace(), is_line_or_paragraph_separator)
90 })
91 .unwrap_or_default();
92 self.byte_offset = cluster_byte_offset;
93
94 Some(GlyphCluster {
95 byte_range,
96 glyph_range: Range { start: cluster_start, end: self.glyph_index },
97 width: cluster_width,
98 is_whitespace,
99 is_line_or_paragraph_separator,
100 })
101 }
102}