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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use {CharacterCache, FontSize, Scalar};
use std::cell::RefCell;
pub struct GlyphCache<C> {
ref_cell: RefCell<C>,
}
#[derive(Clone)]
pub struct CharWidths<'a, C: 'a, I> {
font_size: FontSize,
cache: &'a GlyphCache<C>,
chars: I,
}
pub type X = Scalar;
pub type HalfW = Scalar;
#[derive(Clone)]
pub struct CharXs<'a, C: 'a, I> {
widths: CharWidths<'a, C, I>,
maybe_next: Option<(HalfW, X)>,
}
#[derive(Clone)]
pub struct Widths<'a, C: 'a, I> {
font_size: FontSize,
cache: &'a GlyphCache<C>,
strs: I,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LineBreak {
Wrap(usize, usize),
Newline(usize, usize),
}
#[derive(Clone)]
pub struct LineBreaksBy<'a, C: 'a, F> {
font_size: FontSize,
cache: &'a GlyphCache<C>,
text: &'a str,
max_width: Scalar,
line_break_fn: F,
start: usize,
}
pub type NextLineBreakFn<C> = fn(&GlyphCache<C>, FontSize, &str, Scalar) -> Option<LineBreak>;
pub type LineBreaksByCharacter<'a, C> = LineBreaksBy<'a, C, NextLineBreakFn<C>>;
pub type LineBreaksByWhitespace<'a, C> = LineBreaksBy<'a, C, NextLineBreakFn<C>>;
pub struct Lines<'a, I> {
text: &'a str,
line_breaks: I,
}
pub type LinesWrappedBy<'a, C, F> = Lines<'a, LineBreaksBy<'a, C, F>>;
pub type LinesWrappedByCharacter<'a, C> = LinesWrappedBy<'a, C, NextLineBreakFn<C>>;
pub type LinesWrappedByWhitespace<'a, C> = LinesWrappedBy<'a, C, NextLineBreakFn<C>>;
impl<C> GlyphCache<C> {
pub fn new(cache: C) -> Self {
GlyphCache {
ref_cell: RefCell::new(cache),
}
}
pub fn char_width(&self, font_size: FontSize, ch: char) -> Scalar
where C: CharacterCache,
{
self.ref_cell.borrow_mut().character(font_size, ch).width()
}
pub fn char_widths<I>(&self, font_size: FontSize, chars: I) -> CharWidths<C, I::IntoIter>
where I: IntoIterator<Item=char>,
{
CharWidths {
font_size: font_size,
cache: self,
chars: chars.into_iter(),
}
}
pub fn char_xs<I>(&self, font_size: FontSize, start_x: Scalar, chars: I) -> CharXs<C, I::IntoIter>
where C: CharacterCache,
I: IntoIterator<Item=char>,
{
let mut widths = self.char_widths(font_size, chars);
let maybe_first = widths.next().map(|w| (w / 2.0, start_x));
CharXs {
maybe_next: maybe_first,
widths: widths,
}
}
pub fn width(&self, font_size: FontSize, text: &str) -> Scalar
where C: CharacterCache,
{
self.ref_cell.borrow_mut().width(font_size, text)
}
pub fn widths<I>(&self, font_size: FontSize, strs: I) -> Widths<C, I::IntoIter>
where for<'a> I: IntoIterator<Item=&'a str>,
{
Widths {
font_size: font_size,
cache: self,
strs: strs.into_iter(),
}
}
pub fn line_breaks_by<'a, F>(&'a self,
font_size: FontSize,
text: &'a str,
max_width: Scalar,
line_break_fn: F) -> LineBreaksBy<'a, C, F>
{
LineBreaksBy {
font_size: font_size,
cache: self,
text: text,
start: 0,
max_width: max_width,
line_break_fn: line_break_fn,
}
}
pub fn line_breaks_by_character<'a>(&'a self,
font_size: FontSize,
text: &'a str,
max_width: Scalar) -> LineBreaksByCharacter<'a, C>
where C: CharacterCache,
{
self.line_breaks_by(font_size, text, max_width, LineBreak::next_by_character)
}
pub fn line_breaks_by_whitespace<'a>(&'a self,
font_size: FontSize,
text: &'a str,
max_width: Scalar) -> LineBreaksByWhitespace<'a, C>
where C: CharacterCache,
{
self.line_breaks_by(font_size, text, max_width, LineBreak::next_by_whitespace)
}
pub fn lines_wrapped_by<'a, F>(&'a self,
font_size: FontSize,
text: &'a str,
max_width: Scalar,
wrap_fn: F) -> LinesWrappedBy<'a, C, F>
{
let line_breaks = self.line_breaks_by(font_size, text, max_width, wrap_fn);
Lines::new(text, line_breaks)
}
pub fn lines_wrapped_by_character<'a>(&'a self,
font_size: FontSize,
text: &'a str,
max_width: Scalar) -> LinesWrappedByCharacter<'a, C>
where C: CharacterCache,
{
let line_breaks = self.line_breaks_by_character(font_size, text, max_width);
Lines::new(text, line_breaks)
}
pub fn lines_wrapped_by_whitespace<'a>(&'a self,
font_size: FontSize,
text: &'a str,
max_width: Scalar) -> LinesWrappedByWhitespace<'a, C>
where C: CharacterCache,
{
let line_breaks = self.line_breaks_by_character(font_size, text, max_width);
Lines::new(text, line_breaks)
}
}
impl<C> ::std::ops::Deref for GlyphCache<C> {
type Target = RefCell<C>;
fn deref<'a>(&'a self) -> &'a RefCell<C> {
&self.ref_cell
}
}
impl<C> ::std::ops::DerefMut for GlyphCache<C> {
fn deref_mut<'a>(&'a mut self) -> &'a mut RefCell<C> {
&mut self.ref_cell
}
}
impl<'a, C, I> Iterator for CharWidths<'a, C, I>
where C: CharacterCache,
I: Iterator<Item=char>,
{
type Item = Scalar;
fn next(&mut self) -> Option<Self::Item> {
let CharWidths { font_size, cache, ref mut chars } = *self;
chars.next().map(|ch| cache.char_width(font_size, ch))
}
}
impl<'a, C, I> Iterator for Widths<'a, C, I>
where C: CharacterCache,
for<'b> I: Iterator<Item=&'b str>,
{
type Item = Scalar;
fn next(&mut self) -> Option<Self::Item> {
let Widths { font_size, cache, ref mut strs } = *self;
strs.next().map(|s| cache.width(font_size, s))
}
}
impl<'a, C, I> Iterator for CharXs<'a, C, I>
where C: CharacterCache,
I: Iterator<Item=char>,
{
type Item = X;
fn next(&mut self) -> Option<Self::Item> {
self.maybe_next.take().map(|(half_w, x)| {
self.maybe_next = self.widths.next().map(|next_w| {
let next_half_w = next_w / 2.0;
let step = half_w + next_half_w;
(next_half_w, x + step)
});
x
})
}
}
impl<'a, C, F> Iterator for LineBreaksBy<'a, C, F>
where C: CharacterCache,
for<'b> F: FnMut(&'b GlyphCache<C>, FontSize, &'b str, Scalar) -> Option<LineBreak>,
{
type Item = (usize, Option<LineBreak>);
fn next(&mut self) -> Option<Self::Item> {
let LineBreaksBy {
cache,
font_size,
ref text,
max_width,
ref mut line_break_fn,
ref mut start,
} = *self;
match line_break_fn(cache, font_size, &text[*start..], max_width) {
Some(next) => {
let next = match next {
LineBreak::Newline(idx, width) => LineBreak::Newline(*start + idx, width),
LineBreak::Wrap(idx, width) => LineBreak::Wrap(*start + idx, width),
};
let range = (*start, Some(next));
*start = match next {
LineBreak::Newline(idx, width) => idx + width,
LineBreak::Wrap(idx, width) => idx + width,
};
Some(range)
},
None => if *start < text.len() {
let last = Some((*start, None));
*start = text.len();
last
} else {
None
},
}
}
}
impl<'a, I> Iterator for Lines<'a, I>
where I: Iterator<Item=(usize, Option<LineBreak>)>,
{
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
let Lines {
ref text,
ref mut line_breaks,
} = *self;
line_breaks.next().map(|(start, maybe_line_break)| match maybe_line_break {
Some(line_break) => &text[start..line_break.index()],
None => &text[start..],
})
}
}
impl LineBreak {
pub fn index(self) -> usize {
match self {
LineBreak::Wrap(idx, _) | LineBreak::Newline(idx, _) => idx,
}
}
pub fn next_by_character<C>(cache: &GlyphCache<C>,
font_size: FontSize,
text: &str,
max_width: Scalar) -> Option<Self>
where C: CharacterCache,
{
let mut width = 0.0;
let mut char_indices = text.char_indices().peekable();
while let Some((i, ch)) = char_indices.next() {
if ch == '\r' {
if let Some(&(_, '\n')) = char_indices.peek() {
return Some(LineBreak::Newline(i, 2))
}
} else if ch == '\n' {
return Some(LineBreak::Newline(i, 1));
}
width += cache.char_width(font_size, ch);
if width > max_width {
return Some(LineBreak::Wrap(i, 0));
}
}
None
}
pub fn next_by_whitespace<C>(cache: &GlyphCache<C>,
font_size: FontSize,
text: &str,
max_width: Scalar) -> Option<Self>
where C: CharacterCache,
{
let mut width = 0.0;
let mut last_whitespace_start = 0;
let mut char_indices = text.char_indices().peekable();
while let Some((i, ch)) = char_indices.next() {
if ch == '\r' {
if let Some(&(_, '\n')) = char_indices.peek() {
return Some(LineBreak::Newline(i, 2))
}
} else if ch == '\n' {
return Some(LineBreak::Newline(i, 1));
}
else if ch.is_whitespace() {
last_whitespace_start = i;
}
width += cache.char_width(font_size, ch);
if width > max_width {
return Some(LineBreak::Wrap(last_whitespace_start, 1));
}
}
None
}
}
impl<'a, I> Lines<'a, I> {
pub fn new(text: &'a str, line_breaks: I) -> Self {
Lines {
text: text,
line_breaks: line_breaks,
}
}
}