1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use smallvec::SmallVec;
use crate::conv::to_usize;
use crate::format::FormattableText;
use crate::{shaper, Action, EnvFlags, Environment, Vec2};
mod glyph_pos;
mod text_runs;
mod wrap_lines;
pub use glyph_pos::{Effect, EffectFlags, MarkerPos, MarkerPosIter};
pub(crate) use text_runs::{LineRun, RunSpecial};
use wrap_lines::{Line, RunPart};
#[derive(Clone, Debug)]
pub struct TextDisplay {
runs: SmallVec<[shaper::GlyphRun; 1]>,
line_runs: SmallVec<[LineRun; 1]>,
pub(crate) action: Action,
required: Vec2,
wrapped_runs: Vec<RunPart>,
lines: Vec<Line>,
num_glyphs: u32,
width: f32,
}
impl Default for TextDisplay {
fn default() -> Self {
TextDisplay {
runs: Default::default(),
line_runs: Default::default(),
action: Action::All,
required: Default::default(),
wrapped_runs: Default::default(),
lines: Default::default(),
num_glyphs: 0,
width: 0.0,
}
}
}
impl TextDisplay {
#[inline]
pub fn required_action(&self) -> Action {
self.action
}
#[inline]
pub fn require_action(&mut self, action: Action) {
self.action = self.action.max(action);
}
pub fn prepare<F: FormattableText>(&mut self, text: &F, env: &Environment) {
let action = self.action;
if action == Action::None {
return;
}
self.action = Action::None;
if action >= Action::All {
let bidi = env.flags.contains(EnvFlags::BIDI);
self.prepare_runs(text, bidi, env.dir, env.dpp, env.pt_size);
} else if action == Action::Resize {
self.resize_runs(text, env.dpp, env.pt_size);
}
let wrap = env.flags.contains(EnvFlags::WRAP);
self.prepare_lines(env.bounds, wrap, env.align);
}
pub fn num_lines(&self) -> usize {
assert!(self.action.is_ready(), "kas-text::TextDisplay: not ready");
self.lines.len()
}
pub fn find_line(&self, index: usize) -> Option<(usize, std::ops::Range<usize>)> {
assert!(self.action.is_ready(), "kas-text::TextDisplay: not ready");
let mut first = None;
for (n, line) in self.lines.iter().enumerate() {
if line.text_range.end() == index {
first = Some((n, line.text_range.to_std()));
} else if line.text_range.includes(index) {
return Some((n, line.text_range.to_std()));
}
}
first
}
pub fn line_range(&self, line: usize) -> Option<std::ops::Range<usize>> {
assert!(self.action.is_ready(), "kas-text::TextDisplay: not ready");
self.lines.get(line).map(|line| line.text_range.to_std())
}
pub fn line_is_ltr(&self, line: usize) -> bool {
assert!(self.action.is_ready(), "kas-text::TextDisplay: not ready");
let first_run = self.lines[line].run_range.start();
let glyph_run = to_usize(self.wrapped_runs[first_run].glyph_run);
self.runs[glyph_run].level.is_ltr()
}
#[inline]
pub fn line_is_rtl(&self, line: usize) -> bool {
assert!(self.action.is_ready(), "kas-text::TextDisplay: not ready");
!self.line_is_ltr(line)
}
pub fn text_index_nearest(&self, pos: Vec2) -> usize {
assert!(self.action.is_ready(), "kas-text::TextDisplay: not ready");
let mut n = 0;
for (i, line) in self.lines.iter().enumerate() {
if line.top > pos.1 {
break;
}
n = i;
}
self.line_index_nearest(n, pos.0).unwrap_or(0)
}
pub fn line_index_nearest(&self, line: usize, x: f32) -> Option<usize> {
assert!(self.action.is_ready(), "kas-text::TextDisplay: not ready");
if line >= self.lines.len() {
return None;
}
let line = &self.lines[line];
let run_range = line.run_range.to_std();
let mut best = line.text_range.start;
let mut best_dist = f32::INFINITY;
let mut try_best = |dist, index| {
if dist < best_dist {
best = index;
best_dist = dist;
}
};
for run_part in &self.wrapped_runs[run_range] {
let glyph_run = &self.runs[to_usize(run_part.glyph_run)];
let rel_pos = x - run_part.offset.0;
let end_index;
if glyph_run.level.is_ltr() {
for glyph in &glyph_run.glyphs[run_part.glyph_range.to_std()] {
let dist = (glyph.position.0 - rel_pos).abs();
try_best(dist, glyph.index);
}
end_index = run_part.text_end;
} else {
let mut index = run_part.text_end;
for glyph in &glyph_run.glyphs[run_part.glyph_range.to_std()] {
let dist = (glyph.position.0 - rel_pos).abs();
try_best(dist, index);
index = glyph.index
}
end_index = index;
}
let end_pos = if run_part.glyph_range.end() < glyph_run.glyphs.len() {
glyph_run.glyphs[run_part.glyph_range.end()].position.0
} else {
glyph_run.caret
};
try_best((end_pos - rel_pos).abs(), end_index);
}
Some(to_usize(best))
}
}