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
use super::Font;
use types::{Range, Rect, RectExt};
use std;
use rusttype;
use rusttype::LayoutIter;
use super::line::LineInfo;
pub struct GlyphRects<'a, 'b> {
y: Range,
next_left: f32,
layout: LayoutIter<'a, 'b>,
}
impl<'a, 'b> Iterator for GlyphRects<'a, 'b> {
type Item = Rect;
fn next(&mut self) -> Option<Self::Item> {
let GlyphRects { ref mut next_left, ref mut layout, y } = *self;
layout.next().map(|g| {
let left = *next_left;
let right = g.pixel_bounding_box()
.map(|bb| bb.max.x as f32)
.unwrap_or_else(|| left + g.unpositioned().h_metrics().advance_width);
*next_left = right;
let x = Range::new(left, right);
Rect::from_ranges(x, y)
})
}
}
pub struct GlyphRectsPerLine<'a, I> {
lines_with_rects: I,
font: &'a Font,
font_size: f32,
}
impl<'a, I> GlyphRectsPerLine<'a, I>
where I: Iterator<Item = (&'a str, Rect)>
{
pub fn new(lines_with_rects: I, font: &'a Font, font_size: f32) -> GlyphRectsPerLine<'a, I> {
GlyphRectsPerLine {
lines_with_rects: lines_with_rects,
font: font,
font_size: font_size,
}
}
}
impl<'a, I> Iterator for GlyphRectsPerLine<'a, I>
where I: Iterator<Item = (&'a str, Rect)>
{
type Item = GlyphRects<'a, 'a>;
fn next(&mut self) -> Option<Self::Item> {
let GlyphRectsPerLine { ref mut lines_with_rects, font, font_size } = *self;
let scale = super::pt_to_scale(font_size);
lines_with_rects.next().map(|(line_text, line_rect)| {
let (x, y) = (line_rect.left(), line_rect.top());
let point = rusttype::Point { x: x, y: y };
GlyphRects {
next_left: line_rect.left(),
layout: font.layout(line_text, scale, point),
y: line_rect.y_range(),
}
})
}
}
pub struct SelectedGlyphRects<'a, 'b> {
enumerated_rects: std::iter::Enumerate<GlyphRects<'a, 'b>>,
end_char_idx: usize,
}
impl<'a, 'b> Iterator for SelectedGlyphRects<'a, 'b> {
type Item = Rect;
fn next(&mut self) -> Option<Self::Item> {
let SelectedGlyphRects { ref mut enumerated_rects, end_char_idx } = *self;
enumerated_rects.next()
.and_then(|(i, rect)| if i < end_char_idx { Some(rect) } else { None })
}
}
pub struct SelectedGlyphRectsPerLine<'a, I> {
enumerated_rects_per_line: std::iter::Enumerate<GlyphRectsPerLine<'a, I>>,
start_cursor_idx: super::cursor::Index,
end_cursor_idx: super::cursor::Index,
}
impl<'a, I> SelectedGlyphRectsPerLine<'a, I>
where I: Iterator<Item = (&'a str, Rect)>
{
pub fn new(lines_with_rects: I,
font: &'a Font,
font_size: f32,
start: super::cursor::Index,
end: super::cursor::Index)
-> SelectedGlyphRectsPerLine<'a, I> {
SelectedGlyphRectsPerLine {
enumerated_rects_per_line: GlyphRectsPerLine::new(lines_with_rects, font, font_size)
.enumerate(),
start_cursor_idx: start,
end_cursor_idx: end,
}
}
}
impl<'a, I> Iterator for SelectedGlyphRectsPerLine<'a, I>
where I: Iterator<Item = (&'a str, Rect)>
{
type Item = SelectedGlyphRects<'a, 'a>;
fn next(&mut self) -> Option<Self::Item> {
let SelectedGlyphRectsPerLine { ref mut enumerated_rects_per_line,
start_cursor_idx,
end_cursor_idx } = *self;
enumerated_rects_per_line.next().map(|(i, rects)| {
let end_char_idx =
if i == end_cursor_idx.line {
end_cursor_idx.char
} else if start_cursor_idx.line <= i && i < end_cursor_idx.line {
std::u32::MAX as usize
} else {
0
};
let mut enumerated_rects = rects.enumerate();
if i == start_cursor_idx.line {
for _ in 0..start_cursor_idx.char {
enumerated_rects.next();
}
}
SelectedGlyphRects {
enumerated_rects: enumerated_rects,
end_char_idx: end_char_idx,
}
})
}
}
pub fn index_after_cursor<I>(mut line_infos: I, cursor_idx: super::cursor::Index) -> Option<usize>
where I: Iterator<Item = LineInfo>
{
line_infos.nth(cursor_idx.line)
.and_then(|line_info| {
let start_char = line_info.start_char;
let end_char = line_info.end_char();
let char_index = start_char + cursor_idx.char;
if char_index <= end_char {
Some(char_index)
} else {
None
}
})
}