#[derive(Debug, Clone, Copy)]
pub struct TextBounds {
pub x: f64,
pub y: f64,
pub w: f64,
pub h: f64,
pub ascent: f64,
pub descent: f64,
}
#[derive(Debug, Clone)]
pub struct GlyphMetric {
pub cluster: String,
pub x_offset: f64,
pub y_offset: f64,
pub advance: f64,
pub width: f64,
}
pub trait TextMetrics {
fn measure_text(&self, text: &str) -> f64;
fn text_bounds(&self, text: &str, font: &str) -> TextBounds;
fn measure_text_glyphs(&self, text: &str, font: &str) -> Vec<GlyphMetric> {
let mut cumulative = 0.0f64;
text.chars()
.map(|c| {
let cluster = c.to_string();
let bounds = self.text_bounds(&cluster, font);
let advance = bounds.w;
let x_off = cumulative;
cumulative += advance;
GlyphMetric {
cluster,
x_offset: x_off,
y_offset: 0.0,
advance,
width: advance,
}
})
.collect()
}
}