-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtext_bounds.rs
141 lines (118 loc) · 3.35 KB
/
text_bounds.rs
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
use notan::draw::*;
use notan::prelude::*;
use notan::text::*;
use notan_math::Rect;
#[derive(AppState)]
struct State {
font: Font,
font2: Font,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(setup)
.add_config(TextConfig)
.add_config(DrawConfig)
.draw(draw)
.build()
}
fn setup(gfx: &mut Graphics) -> State {
let font = gfx
.create_font(include_bytes!("./assets/Ubuntu-B.ttf"))
.unwrap();
let font2 = gfx
.create_font(include_bytes!("./assets/kenney_pixel-webfont.ttf"))
.unwrap();
State { font, font2 }
}
fn draw(gfx: &mut Graphics, state: &mut State) {
let mut text = gfx.create_text();
text.add("This text is a ")
.font(&state.font)
.position(400.0, 30.0)
.h_align_center()
.color(Color::ORANGE)
.size(24.0);
text.chain("Section")
.font(&state.font2)
.size(26.0)
.color(Color::RED);
text.chain("\nthat uses different font-styles")
.font(&state.font)
.size(22.0)
.color(Color::PINK);
// Get the boundaries for the last section
let section_1_bounds = text.last_bounds();
text.add("Another Section")
.font(&state.font2)
.color(Color::WHITE)
.size(40.0)
.position(50.0, 300.0)
.v_align_middle();
// Get the boundaries for the last section
let section_2_bounds = text.last_bounds();
text.add("Last section")
.font(&state.font)
.color(Color::PURPLE)
.size(16.0)
.position(650.0, 450.0)
.h_align_right()
.v_align_middle();
// Get the boundaries for the last section
let section_3_bounds = text.last_bounds();
// Render the sizes numbers
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
// get the size of all bounds combined
let mixed_bounds = text.bounds();
draw.rect(
(mixed_bounds.x, mixed_bounds.y),
(mixed_bounds.width, mixed_bounds.height),
)
.color(Color::WHITE)
.alpha(0.1)
.fill();
draw.text(
&state.font,
&format!(
"Mixed size: {:.2}x{:.2}",
mixed_bounds.width, mixed_bounds.height
),
)
.h_align_center()
.v_align_bottom()
.position(400.0, 550.0)
.size(20.0)
.color(Color::OLIVE);
// draw the size of each section
draw_size(&mut draw, &state.font, section_1_bounds);
draw_size(&mut draw, &state.font, section_2_bounds);
draw_size(&mut draw, &state.font, section_3_bounds);
// draw debug info (sized)
gfx.render(&draw);
// draw the real text
gfx.render(&text);
}
fn draw_size(draw: &mut Draw, font: &Font, bounds: Rect) {
// show height
draw.line(
(bounds.max_x() + 10.0, bounds.y),
(bounds.max_x() + 10.0, bounds.max_y()),
)
.width(2.0)
.color(Color::WHITE);
draw.text(font, &format!("{}px", bounds.height))
.position(bounds.max_x() + 20.0, bounds.center_y())
.v_align_middle()
.size(20.0);
// show width
draw.line(
(bounds.x, bounds.max_y() + 10.0),
(bounds.max_x(), bounds.max_y() + 10.0),
)
.width(2.0)
.color(Color::WHITE);
draw.text(font, &format!("{:.2}px", bounds.width))
.position(bounds.center_x(), bounds.max_y() + 20.0)
.h_align_center()
.size(20.0);
}