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
use crate::graphics::{
self, Color, HorizontalAlignment, Point, Rectangle, VerticalAlignment,
};
use crate::ui::core::{Node, Number, Size, Style};
use crate::ui::widget::text;
use crate::ui::Renderer;
use std::cell::RefCell;
use std::f32;
impl text::Renderer for Renderer {
fn node(&self, style: Style, content: &str, size: f32) -> Node {
let font = self.font.clone();
let content = String::from(content);
let measure = RefCell::new(None);
Node::with_measure(style, move |bounds| {
let mut measure = measure.borrow_mut();
if measure.is_none() {
let bounds = (
match bounds.width {
Number::Undefined => f32::INFINITY,
Number::Defined(w) => w,
},
match bounds.height {
Number::Undefined => f32::INFINITY,
Number::Defined(h) => h,
},
);
let text = graphics::Text {
content: &content,
size,
bounds,
..graphics::Text::default()
};
let (width, height) = font.borrow_mut().measure(text);
let size = Size { width, height };
if bounds.0 == f32::INFINITY {
return size;
} else {
*measure = Some(size);
}
}
measure.unwrap()
})
}
fn draw(
&mut self,
bounds: Rectangle<f32>,
content: &str,
size: f32,
color: Color,
horizontal_alignment: HorizontalAlignment,
vertical_alignment: VerticalAlignment,
) {
self.font.borrow_mut().add(graphics::Text {
content,
position: Point::new(bounds.x, bounds.y),
bounds: (bounds.width, bounds.height),
color,
size,
horizontal_alignment,
vertical_alignment,
});
}
}