use std::path::PathBuf;
use crate::color::Color;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Anchor {
TopLeft,
TopCenter,
TopRight,
CenterLeft,
#[default]
Center,
CenterRight,
BottomLeft,
BottomCenter,
BottomRight,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TextStyle {
pub font_size: u32,
pub color: Color,
pub font_file: Option<PathBuf>,
pub box_color: Option<Color>,
pub box_border_width: u32,
}
impl Default for TextStyle {
fn default() -> Self {
Self {
font_size: 48,
color: Color::WHITE,
font_file: None,
box_color: None,
box_border_width: 0,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TextSpec {
pub text: String,
pub anchor: Anchor,
pub offset: (i32, i32),
pub style: TextStyle,
}
impl TextSpec {
#[must_use]
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
anchor: Anchor::Center,
offset: (0, 0),
style: TextStyle::default(),
}
}
}
#[cfg(test)]
mod tests {
use super::{Anchor, TextSpec, TextStyle};
use crate::color::Color;
#[test]
fn anchor_default_should_be_center() {
assert_eq!(Anchor::default(), Anchor::Center);
}
#[test]
fn text_style_default_should_be_white_48() {
let s = TextStyle::default();
assert_eq!(s.font_size, 48);
assert_eq!(s.color, Color::WHITE);
assert!(s.font_file.is_none());
assert!(s.box_color.is_none());
}
#[test]
fn text_spec_new_should_center_with_default_style() {
let spec = TextSpec::new("Hello");
assert_eq!(spec.text, "Hello");
assert_eq!(spec.anchor, Anchor::Center);
assert_eq!(spec.offset, (0, 0));
assert_eq!(spec.style, TextStyle::default());
}
}