use valo_geometry::{Color, Point};
#[derive(Clone, Debug, PartialEq)]
pub struct TextStyle {
pub families: Vec<String>,
pub weight: u16,
pub italic: bool,
pub stretch: f32,
pub kerning: bool,
pub variant_caps: VariantCaps,
pub size: f32,
pub color: Color,
pub letter_spacing: f32,
pub word_spacing: f32,
pub height: Option<f32>,
pub decoration: Option<Decoration>,
pub shadows: Vec<Shadow>,
}
impl Default for TextStyle {
fn default() -> Self {
Self {
families: Vec::new(),
weight: 400,
italic: false,
stretch: crate::font::NORMAL_STRETCH,
kerning: true,
variant_caps: VariantCaps::Normal,
size: 14.0,
color: Color::BLACK,
letter_spacing: 0.0,
word_spacing: 0.0,
height: None,
decoration: None,
shadows: Vec::new(),
}
}
}
impl TextStyle {
pub fn new(family: &str, size: f32, color: Color) -> Self {
Self {
families: vec![family.to_owned()],
size,
color,
..Default::default()
}
}
pub fn font_attrs(&self) -> crate::font::FontAttrs {
crate::font::FontAttrs {
weight: self.weight,
italic: self.italic,
stretch: self.stretch,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum VariantCaps {
#[default]
Normal,
SmallCaps,
AllSmallCaps,
PetiteCaps,
AllPetiteCaps,
Unicase,
TitlingCaps,
}
impl VariantCaps {
pub fn feature_tags(self) -> &'static [&'static [u8; 4]] {
match self {
Self::Normal => &[],
Self::SmallCaps => &[b"smcp"],
Self::AllSmallCaps => &[b"c2sc", b"smcp"],
Self::PetiteCaps => &[b"pcap"],
Self::AllPetiteCaps => &[b"c2pc", b"pcap"],
Self::Unicase => &[b"unic"],
Self::TitlingCaps => &[b"titl"],
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Decoration {
pub kind: DecorationKind,
pub color: Option<Color>,
pub thickness: f32,
}
impl Decoration {
pub fn new(kind: DecorationKind) -> Self {
Self {
kind,
color: None,
thickness: 1.0,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DecorationKind {
Underline,
LineThrough,
Overline,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Shadow {
pub color: Color,
pub offset: Point,
pub blur: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextAlign {
#[default]
Left,
Center,
Right,
Justify,
}
impl From<TextAlign> for ParagraphStyle {
fn from(align: TextAlign) -> Self {
Self {
align,
..Default::default()
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextDirection {
Ltr,
Rtl,
}
#[derive(Clone, Debug, Default)]
pub struct ParagraphStyle {
pub align: TextAlign,
pub direction: Option<TextDirection>,
pub preserve_trailing_whitespace: bool,
pub max_lines: Option<u32>,
pub ellipsis: Option<String>,
}