waft_rendering/
text_boundary.rs1use std::rc::Rc;
9
10use unicode_general_category::{GeneralCategory, get_general_category};
11use waft_embedder::{TextPosition, TextRange};
12use waft_foundation::App;
13use waft_painting::InlineSpanRef;
14use waft_services::{TextBoundary, UntilPredicate};
15
16use crate::editable::RenderEditable;
17use crate::object::RenderHandle;
18use crate::paragraph::RenderParagraph;
19
20#[derive(Clone, Copy, Debug)]
28pub struct LineBoundary {
29 text_layout: RenderHandle<RenderEditable>,
30}
31
32impl LineBoundary {
33 pub fn new(text_layout: RenderHandle<RenderEditable>) -> LineBoundary {
35 LineBoundary { text_layout }
36 }
37}
38
39impl TextBoundary for LineBoundary {
40 fn get_text_boundary_at(&self, app: &mut App, position: i32) -> TextRange {
41 self.text_layout
42 .get_line_at_offset(app, TextPosition::new(position.max(0)))
43 .range()
44 }
45}
46
47#[derive(Clone)]
61pub struct WordBoundary {
62 text: InlineSpanRef,
63 layout: WordBoundaryLayout,
64}
65
66#[derive(Clone)]
68enum WordBoundaryLayout {
69 Editable(RenderHandle<RenderEditable>),
70 Paragraph(RenderHandle<RenderParagraph>),
71}
72
73impl WordBoundary {
74 pub(crate) fn new(
76 text: InlineSpanRef,
77 paragraph: RenderHandle<RenderEditable>,
78 ) -> WordBoundary {
79 WordBoundary {
80 text,
81 layout: WordBoundaryLayout::Editable(paragraph),
82 }
83 }
84
85 pub(crate) fn for_paragraph(
87 text: InlineSpanRef,
88 paragraph: RenderHandle<RenderParagraph>,
89 ) -> WordBoundary {
90 WordBoundary {
91 text,
92 layout: WordBoundaryLayout::Paragraph(paragraph),
93 }
94 }
95
96 pub fn move_by_word_boundary(&self) -> Box<dyn TextBoundary> {
108 move_by_word_boundary(self.text.clone(), Box::new(self.clone()))
109 }
110}
111
112impl TextBoundary for WordBoundary {
113 fn get_text_boundary_at(&self, app: &mut App, position: i32) -> TextRange {
114 match self.layout {
115 WordBoundaryLayout::Editable(paragraph) => {
116 paragraph.get_word_boundary(app, TextPosition::new(position.max(0)))
117 }
118 WordBoundaryLayout::Paragraph(paragraph) => {
119 paragraph.get_word_boundary(app, TextPosition::new(position.max(0)))
120 }
121 }
122 }
123}
124
125pub(crate) fn move_by_word_boundary(
128 text: InlineSpanRef,
129 boundary: Box<dyn TextBoundary>,
130) -> Box<dyn TextBoundary> {
131 let text = Rc::clone(&text);
132 Box::new(UntilTextBoundary {
133 text_boundary: boundary,
134 predicate: Rc::new(move |offset, forward| {
135 skip_spaces_and_punctuations(&text, offset, forward)
136 }),
137 })
138}
139
140fn code_point_from_surrogates(high_surrogate: i32, low_surrogate: i32) -> i32 {
143 const BASE: i32 = 0x010000 - (0xD800 << 10) - 0xDC00;
144 (high_surrogate << 10) + low_surrogate + BASE
145}
146
147fn code_point_at(text: &InlineSpanRef, index: i32) -> Option<i32> {
149 let code_unit_at_index = i32::from(text.clone().code_unit_at(index)?);
150 Some(match code_unit_at_index & 0xFC00 {
151 0xD800 => code_point_from_surrogates(
152 code_unit_at_index,
153 i32::from(text.clone().code_unit_at(index + 1)?),
154 ),
155 0xDC00 => code_point_from_surrogates(
156 i32::from(text.clone().code_unit_at(index - 1)?),
157 code_unit_at_index,
158 ),
159 _ => code_unit_at_index,
160 })
161}
162
163fn is_newline(code_point: i32) -> bool {
164 matches!(
166 code_point,
167 0x000A | 0x0085 | 0x000B | 0x000C | 0x2028 | 0x2029 )
174}
175
176fn is_space_separator_or_punctuation(code_point: i32) -> bool {
178 let Some(character) = u32::try_from(code_point).ok().and_then(char::from_u32) else {
179 return false;
180 };
181 matches!(
182 get_general_category(character),
183 GeneralCategory::SpaceSeparator
184 | GeneralCategory::ConnectorPunctuation
185 | GeneralCategory::DashPunctuation
186 | GeneralCategory::OpenPunctuation
187 | GeneralCategory::ClosePunctuation
188 | GeneralCategory::InitialPunctuation
189 | GeneralCategory::FinalPunctuation
190 | GeneralCategory::OtherPunctuation
191 )
192}
193
194fn skip_spaces_and_punctuations(text: &InlineSpanRef, offset: i32, forward: bool) -> bool {
195 let inner_code_point = code_point_at(text, if forward { offset - 1 } else { offset });
199 let outer_code_unit = text
200 .clone()
201 .code_unit_at(if forward { offset } else { offset - 1 });
202
203 let Some(inner_code_point) = inner_code_point else {
210 return true;
211 };
212 let Some(outer_code_unit) = outer_code_unit else {
213 return true;
214 };
215 let hard_break_rules_apply =
217 is_newline(inner_code_point) || is_newline(i32::from(outer_code_unit));
218 hard_break_rules_apply || !is_space_separator_or_punctuation(inner_code_point)
219}
220
221struct UntilTextBoundary {
224 text_boundary: Box<dyn TextBoundary>,
225 predicate: Rc<UntilPredicate>,
226}
227
228impl TextBoundary for UntilTextBoundary {
229 fn get_leading_text_boundary_at(&self, app: &mut App, position: i32) -> Option<i32> {
230 if position < 0 {
231 return None;
232 }
233 let offset = self
234 .text_boundary
235 .get_leading_text_boundary_at(app, position)?;
236 if (self.predicate)(offset, false) {
237 Some(offset)
238 } else {
239 self.get_leading_text_boundary_at(app, offset - 1)
240 }
241 }
242
243 fn get_trailing_text_boundary_at(&self, app: &mut App, position: i32) -> Option<i32> {
244 let offset = self
245 .text_boundary
246 .get_trailing_text_boundary_at(app, position.max(0))?;
247 if (self.predicate)(offset, true) {
248 Some(offset)
249 } else {
250 self.get_trailing_text_boundary_at(app, offset)
251 }
252 }
253}