use embedded_graphics::{
mono_font::{
MonoFont,
ascii::{FONT_6X10, FONT_7X14, FONT_9X18_BOLD, FONT_10X20},
},
pixelcolor::Rgb565,
};
use crate::EdgeInsets;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextFont {
Caption,
Body,
BodyStrong,
Title,
}
impl TextFont {
pub(super) fn mono(self) -> &'static MonoFont<'static> {
match self {
Self::Caption => &FONT_6X10,
Self::Body => &FONT_7X14,
Self::BodyStrong => &FONT_9X18_BOLD,
Self::Title => &FONT_10X20,
}
}
pub(super) fn advance(self) -> i32 {
let font = self.mono();
(font.character_size.width + font.character_spacing) as i32
}
pub(super) fn line_height(self) -> i32 {
self.mono().character_size.height as i32
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TextRunStyle {
pub font: TextFont,
pub color: Rgb565,
}
impl TextRunStyle {
pub const fn new(font: TextFont, color: Rgb565) -> Self {
Self { font, color }
}
pub const fn caption(color: Rgb565) -> Self {
Self::new(TextFont::Caption, color)
}
pub const fn body(color: Rgb565) -> Self {
Self::new(TextFont::Body, color)
}
pub const fn body_strong(color: Rgb565) -> Self {
Self::new(TextFont::BodyStrong, color)
}
pub const fn title(color: Rgb565) -> Self {
Self::new(TextFont::Title, color)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TextSpan<'a> {
pub text: &'a str,
pub style: TextRunStyle,
}
impl<'a> TextSpan<'a> {
pub const fn new(text: &'a str, style: TextRunStyle) -> Self {
Self { text, style }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextAlignment {
Leading,
Center,
Trailing,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextVerticalAlignment {
Top,
Center,
Bottom,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextWrap {
SingleLine,
Multiline,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TextViewStyle {
pub alignment: TextAlignment,
pub vertical_alignment: TextVerticalAlignment,
pub wrap: TextWrap,
pub line_spacing: u32,
pub insets: EdgeInsets,
pub background: Option<Rgb565>,
pub border: Option<Rgb565>,
pub border_width: u32,
pub corner_radius: u32,
}
impl TextViewStyle {
pub const fn new() -> Self {
Self {
alignment: TextAlignment::Leading,
vertical_alignment: TextVerticalAlignment::Top,
wrap: TextWrap::SingleLine,
line_spacing: 4,
insets: EdgeInsets::all(0),
background: None,
border: None,
border_width: 0,
corner_radius: 0,
}
}
pub fn with_alignment(mut self, alignment: TextAlignment) -> Self {
self.alignment = alignment;
self
}
pub fn with_vertical_alignment(mut self, alignment: TextVerticalAlignment) -> Self {
self.vertical_alignment = alignment;
self
}
pub fn with_wrap(mut self, wrap: TextWrap) -> Self {
self.wrap = wrap;
self
}
pub fn with_line_spacing(mut self, line_spacing: u32) -> Self {
self.line_spacing = line_spacing;
self
}
pub fn with_insets(mut self, insets: EdgeInsets) -> Self {
self.insets = insets;
self
}
pub fn with_background(mut self, background: Rgb565) -> Self {
self.background = Some(background);
self
}
pub fn with_border(mut self, border: Rgb565, border_width: u32) -> Self {
self.border = Some(border);
self.border_width = border_width;
self
}
pub fn with_corner_radius(mut self, corner_radius: u32) -> Self {
self.corner_radius = corner_radius;
self
}
}
impl Default for TextViewStyle {
fn default() -> Self {
Self::new()
}
}