text_measures/
text_measures.rs1use macroquad::prelude::*;
2
3fn draw_text_annotated(text: &str, font: Option<&Font>, x: f32, baseline: f32) {
4 let size = measure_text(text, font, 100, 1.0);
5
6 draw_rectangle(x, baseline - size.offset_y, size.width, size.height, BLUE);
8
9 draw_rectangle(x, baseline - 2.0, size.width, 4.0, RED);
11
12 draw_rectangle(x + size.width, baseline - 1.0, 120.0, 1.0, GRAY);
14 draw_text(
15 "baseline",
16 x + size.width + 10.0,
17 baseline - 5.0,
18 30.0,
19 WHITE,
20 );
21
22 draw_rectangle(x, baseline - 2.0 - size.offset_y, size.width, 4.0, RED);
24
25 draw_rectangle(
27 x + size.width,
28 baseline - size.offset_y - 1.0,
29 120.0,
30 1.0,
31 GRAY,
32 );
33 draw_text(
34 "topline",
35 x + size.width + 10.0,
36 baseline - size.offset_y - 5.0,
37 30.0,
38 WHITE,
39 );
40
41 draw_rectangle(
43 x,
44 baseline - 2.0 - size.offset_y + size.height,
45 size.width,
46 4.0,
47 RED,
48 );
49
50 draw_rectangle(
52 x + size.width,
53 baseline - size.offset_y + size.height - 1.0,
54 120.0,
55 1.0,
56 GRAY,
57 );
58 draw_text(
59 "bottomline",
60 x + size.width + 10.0,
61 baseline - size.offset_y + size.height - 5.0,
62 30.0,
63 WHITE,
64 );
65
66 draw_text_ex(
67 text,
68 x,
69 baseline,
70 TextParams {
71 font_size: 100,
72 font,
73 ..Default::default()
74 },
75 );
76}
77
78#[macroquad::main("Text")]
79async fn main() {
80 let font = load_ttf_font("./examples/DancingScriptRegular.ttf")
81 .await
82 .unwrap();
83
84 loop {
85 clear_background(BLACK);
86
87 let text = "abcdIj";
88
89 draw_text_annotated(text, None, 40.0, 200.0);
90 draw_text_annotated(text, Some(&font), 400.0, 400.0);
91
92 next_frame().await
93 }
94}