#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Style {
pub color: Option<String>,
pub size: f32,
pub bold: bool,
pub speed: f32,
pub effects: Vec<Effect>,
pub baseline: Baseline,
pub strike: bool,
pub underline: bool,
pub emphasis: Option<char>,
pub marks: Vec<String>,
pub outline: Option<Outline>,
pub shadow: Option<Shadow>,
pub align: Option<Align>,
pub blip: Option<String>,
}
impl Default for Style {
fn default() -> Self {
Self {
color: None,
size: 1.0,
bold: false,
speed: 1.0,
effects: Vec::new(),
baseline: Baseline::Normal,
strike: false,
underline: false,
emphasis: None,
marks: Vec::new(),
outline: None,
shadow: None,
align: None,
blip: None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Outline {
pub color: String,
pub width: f32,
}
pub(crate) const OUTLINE_WIDTH: f32 = 0.08;
impl Default for Outline {
fn default() -> Self {
Self {
color: "black".to_string(),
width: OUTLINE_WIDTH,
}
}
}
impl Outline {
#[must_use]
pub fn color(mut self, value: impl Into<String>) -> Self {
self.color = value.into();
self
}
setters! {
width: f32;
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Shadow {
pub color: String,
pub x: f32,
pub y: f32,
}
pub(crate) const SHADOW_OFFSET: f32 = 0.06;
impl Default for Shadow {
fn default() -> Self {
Self {
color: "black".to_string(),
x: SHADOW_OFFSET,
y: SHADOW_OFFSET,
}
}
}
impl Shadow {
#[must_use]
pub fn color(mut self, value: impl Into<String>) -> Self {
self.color = value.into();
self
}
setters! {
x: f32;
y: f32;
}
}
pub const EMPHASIS_MARK: char = '﹅';
pub const COLOR_NAMES: &[&str] = &[
"white", "black", "red", "green", "blue", "yellow", "cyan", "magenta", "pink", "orange",
"gray", "grey", "lime", "aqua", "maroon", "olive", "teal", "navy", "purple", "fuchsia",
"silver",
];
pub fn is_color(s: &str) -> bool {
match s.strip_prefix('#') {
Some(hex) => matches!(hex.len(), 3 | 6) && hex.chars().all(|c| c.is_ascii_hexdigit()),
None => COLOR_NAMES.iter().any(|n| n.eq_ignore_ascii_case(s)),
}
}
pub(crate) const SHIFT_SCALE: f32 = 0.6;
pub(crate) const SUP_RISE: f32 = 0.4;
pub(crate) const SUB_DROP: f32 = 0.15;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Align {
#[default]
Start,
Center,
End,
}
impl Align {
pub fn from_name(name: &str) -> Option<Self> {
match name {
"start" | "left" => Some(Self::Start),
"center" => Some(Self::Center),
"end" | "right" => Some(Self::End),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Baseline {
#[default]
Normal,
Sup,
Sub,
}
impl Style {
pub fn glyph_size(&self) -> f32 {
match self.baseline {
Baseline::Normal => self.size,
_ => self.size * SHIFT_SCALE,
}
}
pub fn rise(&self) -> f32 {
match self.baseline {
Baseline::Normal => 0.0,
Baseline::Sup => SUP_RISE * self.size,
Baseline::Sub => -SUB_DROP * self.size,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Effect {
Shake {
strength: f32,
decay: Option<f32>,
},
Wave {
strength: f32,
decay: Option<f32>,
},
Fade(f32),
NekoHop(f32),
Glow {
kind: GlowKind,
strength: f32,
secs: f32,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GlowKind {
In,
Breathe,
Flicker,
}
impl GlowKind {
pub fn default_secs(self) -> f32 {
match self {
GlowKind::In => 1.5,
GlowKind::Breathe => 2.0,
GlowKind::Flicker => 3.0,
}
}
}