use uzor::fonts::FontFamily;
use uzor::render::{TextAlign, TextBaseline};
#[derive(Debug, Clone)]
pub struct TextAreaData {
pub text: String,
pub x: f32,
pub y: f32,
pub font_size: f32,
pub color: [f32; 4],
pub family: FontFamily,
pub bold: bool,
pub italic: bool,
pub align: TextAlign,
pub baseline: TextBaseline,
pub clip: [f32; 4],
pub estimated_width: f32,
pub estimated_height: f32,
}
impl TextAreaData {
pub fn top_left(&self, ascent: f32, descent: f32) -> (f32, f32) {
let left = match self.align {
TextAlign::Left => self.x,
TextAlign::Center => self.x - self.estimated_width * 0.5,
TextAlign::Right => self.x - self.estimated_width,
};
let top = match self.baseline {
TextBaseline::Top => self.y,
TextBaseline::Middle => self.y - (ascent - descent) * 0.5,
TextBaseline::Bottom => self.y - (ascent - descent),
TextBaseline::Alphabetic => self.y - ascent,
};
(left, top)
}
pub fn color_u8(&self) -> [u8; 4] {
[
(self.color[0] * 255.0).clamp(0.0, 255.0) as u8,
(self.color[1] * 255.0).clamp(0.0, 255.0) as u8,
(self.color[2] * 255.0).clamp(0.0, 255.0) as u8,
(self.color[3] * 255.0).clamp(0.0, 255.0) as u8,
]
}
}