use iced::advanced::text::{
Alignment, LineHeight, Paragraph as _, Shaping, Text, Wrapping,
};
use iced::alignment::Vertical;
use iced::{Font, Pixels, Size};
type Paragraph = <iced::Renderer as iced::advanced::text::Renderer>::Paragraph;
const SAMPLE: &str = "0000000000000000";
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Metrics {
pub advance: f32,
pub line_height: f32,
pub size: f32,
}
impl Metrics {
#[must_use]
pub fn measure(font: Font, size: f32, line_height: f32) -> Self {
let text = Text {
content: SAMPLE,
bounds: Size::new(f32::INFINITY, f32::INFINITY),
size: Pixels(size),
line_height: LineHeight::Absolute(Pixels(line_height)),
font,
align_x: Alignment::Left,
align_y: Vertical::Top,
shaping: Shaping::Basic,
wrapping: Wrapping::None,
};
let width = Paragraph::with_text(text).min_width();
let advance = width / SAMPLE.chars().count() as f32;
Self { advance, line_height, size }
}
}