18 lines
427 B
Rust
18 lines
427 B
Rust
use rusttype::{point, Font, Scale};
|
|
|
|
pub fn get_text_width(scale: Scale, font: &Font, text: &str) -> u32 {
|
|
let mut layout = font.layout(text, scale, point(0.0, 0.0));
|
|
|
|
let min = layout
|
|
.next()
|
|
.map(|g| g.pixel_bounding_box().unwrap().min.x)
|
|
.unwrap();
|
|
|
|
let max = layout
|
|
.last()
|
|
.map(|g| g.pixel_bounding_box().unwrap().max.x)
|
|
.unwrap();
|
|
|
|
(max - min) as u32
|
|
}
|