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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use std::cmp;
use {
Align,
CharacterCache,
Color,
Colorable,
Dimension,
FontSize,
LineBreak,
Range,
Rect,
Scalar,
Theme,
Ui,
Widget,
};
use widget;
pub struct Text<'a> {
pub common: widget::CommonBuilder,
pub text: &'a str,
pub style: Style,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Style {
pub maybe_font_size: Option<FontSize>,
pub maybe_color: Option<Color>,
pub maybe_wrap: Option<Option<Wrap>>,
pub maybe_line_spacing: Option<Scalar>,
pub maybe_text_align: Option<Align>,
}
pub const KIND: widget::Kind = "Text";
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Wrap {
Character,
Whitespace,
}
#[derive(Clone, Debug, PartialEq)]
pub struct State {
string: String,
line_breaks: Vec<(usize, Option<LineBreak>)>,
}
impl<'a> Text<'a> {
pub fn new(text: &'a str) -> Self {
Text {
common: widget::CommonBuilder::new(),
text: text,
style: Style::new(),
}
}
pub fn font_size(mut self, size: FontSize) -> Self {
self.style.maybe_font_size = Some(size);
self
}
pub fn no_line_wrap(mut self) -> Self {
self.style.maybe_wrap = Some(None);
self
}
pub fn wrap_by_word(mut self) -> Self {
self.style.maybe_wrap = Some(Some(Wrap::Whitespace));
self
}
pub fn wrap_by_character(mut self) -> Self {
self.style.maybe_wrap = Some(Some(Wrap::Character));
self
}
pub fn with_style(mut self, style: Style) -> Self {
self.style = style;
self
}
pub fn align_text_left(mut self) -> Self {
self.style.maybe_text_align = Some(Align::Start);
self
}
pub fn align_text_middle(mut self) -> Self {
self.style.maybe_text_align = Some(Align::Middle);
self
}
pub fn align_text_right(mut self) -> Self {
self.style.maybe_text_align = Some(Align::End);
self
}
pub fn line_spacing(mut self, height: Scalar) -> Self {
self.style.maybe_line_spacing = Some(height);
self
}
}
impl Style {
pub fn new() -> Style {
Style {
maybe_color: None,
maybe_font_size: None,
maybe_wrap: None,
maybe_line_spacing: None,
maybe_text_align: None,
}
}
pub fn color(&self, theme: &Theme) -> Color {
self.maybe_color.unwrap_or(theme.label_color)
}
pub fn font_size(&self, theme: &Theme) -> FontSize {
self.maybe_font_size.unwrap_or(theme.font_size_medium)
}
pub fn wrap(&self, theme: &Theme) -> Option<Wrap> {
const DEFAULT_WRAP: Option<Wrap> = Some(Wrap::Whitespace);
self.maybe_wrap.or_else(|| theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_wrap.unwrap_or(DEFAULT_WRAP)
})).unwrap_or(DEFAULT_WRAP)
}
pub fn line_spacing(&self, theme: &Theme) -> Scalar {
const DEFAULT_LINE_SPACING: Scalar = 1.0;
self.maybe_line_spacing.or_else(|| theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_line_spacing.unwrap_or(DEFAULT_LINE_SPACING)
})).unwrap_or(DEFAULT_LINE_SPACING)
}
pub fn text_align(&self, theme: &Theme) -> Align {
const DEFAULT_TEXT_ALIGN: Align = Align::Start;
self.maybe_text_align.or_else(|| theme.widget_style::<Self>(KIND).and_then(|default| {
default.style.maybe_text_align
})).unwrap_or(DEFAULT_TEXT_ALIGN)
}
}
impl<'a> Widget for Text<'a> {
type State = State;
type Style = Style;
fn common(&self) -> &widget::CommonBuilder {
&self.common
}
fn common_mut(&mut self) -> &mut widget::CommonBuilder {
&mut self.common
}
fn unique_kind(&self) -> &'static str {
KIND
}
fn init_state(&self) -> State {
State {
string: String::new(),
line_breaks: Vec::new(),
}
}
fn style(&self) -> Style {
self.style.clone()
}
fn default_x_dimension<C: CharacterCache>(&self, ui: &Ui<C>) -> Dimension {
let font_size = self.style.font_size(&ui.theme);
let mut max_width = 0.0;
for line in self.text.lines() {
let width = ui.glyph_cache.width(font_size, line);
max_width = ::utils::partial_max(max_width, width);
}
Dimension::Absolute(max_width)
}
fn default_y_dimension<C: CharacterCache>(&self, ui: &Ui<C>) -> Dimension {
use position::Sizeable;
let text = &self.text;
let font_size = self.style.font_size(&ui.theme);
let num_lines = match self.style.wrap(&ui.theme) {
None => text.lines().count(),
Some(wrap) => match self.get_width(ui) {
None => text.lines().count(),
Some(max_w) => match wrap {
Wrap::Character =>
ui.glyph_cache.line_breaks_by_character(font_size, text, max_w).count(),
Wrap::Whitespace =>
ui.glyph_cache.line_breaks_by_whitespace(font_size, text, max_w).count(),
},
},
};
let line_spacing = self.style.line_spacing(&ui.theme);
let height = total_height(cmp::max(num_lines, 1), font_size, line_spacing);
Dimension::Absolute(height)
}
fn update<C: CharacterCache>(self, args: widget::UpdateArgs<Self, C>) {
let widget::UpdateArgs { rect, state, style, ui, .. } = args;
let Text { text, .. } = self;
let maybe_wrap = style.wrap(ui.theme());
let font_size = style.font_size(ui.theme());
let new_line_breaks = || match maybe_wrap {
None =>
ui.glyph_cache().line_breaks_by_character(font_size, text, ::std::f64::MAX),
Some(Wrap::Character) =>
ui.glyph_cache().line_breaks_by_character(font_size, text, rect.w()),
Some(Wrap::Whitespace) =>
ui.glyph_cache().line_breaks_by_whitespace(font_size, text, rect.w()),
};
if &state.view().string[..] != text {
state.update(|state| {
state.string = text.to_owned();
state.line_breaks = new_line_breaks().collect();
});
} else {
use utils::write_if_different;
use std::borrow::Cow;
let maybe_new_line_breaks = {
let line_breaks = &state.view().line_breaks[..];
match write_if_different(line_breaks, new_line_breaks()) {
Cow::Owned(new) => Some(new),
_ => None,
}
};
if let Some(new_line_breaks) = maybe_new_line_breaks {
state.update(|state| state.line_breaks = new_line_breaks);
}
}
}
}
impl<'a> Colorable for Text<'a> {
fn color(mut self, color: Color) -> Self {
self.style.maybe_color = Some(color);
self
}
}
impl State {
pub fn lines(&self) -> Lines {
::glyph_cache::Lines::new(&self.string, self.line_breaks.iter().cloned())
}
pub fn line_rects<'a>(&'a self,
container: Rect,
h_align: Align,
font_size: FontSize,
line_spacing: Scalar) -> LineRects<'a, Lines<'a>>
{
let lines = self.lines();
LineRects::new(lines, container, h_align, font_size, line_spacing)
}
}
pub fn total_height(num_lines: usize, font_size: FontSize, line_spacing: Scalar) -> Scalar {
font_size as Scalar * num_lines as Scalar + line_spacing * (num_lines - 1) as Scalar
}
pub type Lines<'a> =
::glyph_cache::Lines<'a, ::std::iter::Cloned<::std::slice::Iter<'a, (usize, Option<LineBreak>)>>>;
pub struct LineRects<'a, I: 'a> {
lines: I,
font_size: FontSize,
y_step: Scalar,
h_align: Align,
container_x: Range,
y: Scalar,
strs: ::std::marker::PhantomData<&'a ()>,
}
impl<'a, I> LineRects<'a, I> {
pub fn new(lines: I,
container: Rect,
h_align: Align,
font_size: FontSize,
line_spacing: Scalar) -> LineRects<'a, I>
where I: Iterator<Item=&'a str>,
{
let height = font_size as Scalar;
LineRects {
lines: lines,
font_size: font_size,
y_step: -(line_spacing + height),
h_align: h_align,
container_x: container.x,
y: Range::new(0.0, height).align_end_of(container.y).middle(),
strs: ::std::marker::PhantomData,
}
}
pub fn next_with_line<C>(&mut self, cache: &mut C) -> Option<(Rect, &'a str)>
where I: Iterator<Item=&'a str>,
C: CharacterCache,
{
let LineRects { ref mut lines, font_size, y_step, h_align, container_x, ref mut y, .. } = *self;
lines.next().map(|line| {
let w = cache.width(font_size, line);
let h = font_size as Scalar;
let w_range = Range::new(0.0, w);
let x = match h_align {
Align::Start => w_range.align_start_of(container_x),
Align::Middle => w_range.align_middle_of(container_x),
Align::End => w_range.align_end_of(container_x),
}.middle();
let xy = [x, *y];
let wh = [w, h];
let rect = Rect::from_xy_dim(xy, wh);
*y += y_step;
(rect, line)
})
}
}