use crate::{Paint, Rgba, TextAnimator, Vec2};
use serde::{Deserialize, Serialize};
pub const MAX_TEXT_CONTENT_BYTES: usize = 4_096;
pub const MAX_FONT_FAMILY_BYTES: usize = 256;
pub const MAX_TEXT_GENERATOR_FONT_SIZE: f32 = 1.0;
pub const LINE_HEIGHT_RANGE: std::ops::RangeInclusive<f32> = 0.5..=3.0;
pub const LETTER_SPACING_RANGE: std::ops::RangeInclusive<f32> = -0.5..=1.0;
pub const FONT_WEIGHT_RANGE: std::ops::RangeInclusive<u16> = 1..=1000;
pub const MAX_TEXT_STROKES: usize = 4;
pub const MAX_TEXT_STROKE_EXTENT: f32 = 0.5;
pub const MAX_TEXT_SHADOWS: usize = 4;
pub const MAX_TEXT_SHADOW_OFFSET: f32 = 1.0;
pub const MAX_TEXT_SHADOW_BLUR: f32 = 1.0;
pub const MAX_TEXT_PADDING: f32 = 2.0;
pub const MAX_TEXT_POSITION: f32 = 1_024.0;
pub const MAX_BLOCK_ELLIPSIS_BYTES: usize = 64;
pub const FONT_SIZE_FIT_STEPS: f32 = 8_192.0;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct TextStyle {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub font_family: Option<String>,
#[serde(default = "default_font_size")]
pub font_size: f32,
#[serde(default = "white_paint")]
pub fill: Paint,
#[serde(default = "default_line_height")]
pub line_height: f32,
#[serde(default)]
pub letter_spacing: f32,
#[serde(default = "default_font_weight")]
pub font_weight: u16,
#[serde(default)]
pub align: TextAlign,
#[serde(default = "default_max_width")]
pub max_width: f32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_height: Option<f32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub font_size_fit: Option<FontSizeFit>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub line_clamp: Option<LineClamp>,
#[serde(default = "default_position")]
pub position: Vec2,
#[serde(default = "default_anchor")]
pub anchor: Vec2,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_TEXT_STROKES }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TEXT_STROKES)))]
pub strokes: Vec<TextStroke>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_TEXT_SHADOWS }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TEXT_SHADOWS)))]
pub shadows: Vec<TextShadow>,
#[serde(default)]
pub background: Option<TextBackground>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_TEXT_ANIMATORS }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TEXT_ANIMATORS)))]
pub animators: Vec<TextAnimator>,
}
impl Default for TextStyle {
fn default() -> Self {
Self {
font_family: None,
font_size: default_font_size(),
fill: white_paint(),
line_height: default_line_height(),
letter_spacing: 0.0,
font_weight: default_font_weight(),
align: TextAlign::default(),
max_width: default_max_width(),
max_height: None,
font_size_fit: None,
line_clamp: None,
position: default_position(),
anchor: default_anchor(),
strokes: Vec::new(),
shadows: Vec::new(),
background: None,
animators: Vec::new(),
}
}
}
const fn default_font_size() -> f32 {
0.05
}
const fn white_paint() -> Paint {
Paint::Solid {
color: Rgba::new(1.0, 1.0, 1.0, 1.0),
}
}
const fn default_line_height() -> f32 {
1.2
}
const fn default_font_weight() -> u16 {
400
}
const fn default_max_width() -> f32 {
0.9
}
const fn default_position() -> Vec2 {
Vec2::new(0.5, 0.92)
}
const fn default_anchor() -> Vec2 {
Vec2::new(0.5, 1.0)
}
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct FontSizeFit {
pub min: f32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max: Option<f32>,
}
impl FontSizeFit {
pub(crate) fn is_valid(&self, max_font_size: f32) -> bool {
let size = |value: f32| value.is_finite() && value > 0.0 && value <= max_font_size;
size(self.min) && self.max.is_none_or(|max| size(max) && self.min <= max)
}
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct LineClamp {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_lines: Option<u32>,
#[serde(default)]
pub block_ellipsis: BlockEllipsis,
}
impl LineClamp {
pub(crate) fn is_valid(&self) -> bool {
self.max_lines != Some(0) && self.block_ellipsis.is_valid()
}
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum BlockEllipsis {
NoEllipsis,
#[default]
Ellipsis,
String(String),
}
impl BlockEllipsis {
#[must_use]
pub fn text(&self) -> Option<&str> {
match self {
Self::NoEllipsis => None,
Self::Ellipsis => Some("\u{2026}"),
Self::String(text) => Some(text),
}
}
fn is_valid(&self) -> bool {
match self {
Self::String(text) => {
!text.is_empty()
&& text.len() <= MAX_BLOCK_ELLIPSIS_BYTES
&& !text.chars().any(|character| {
character.is_control() || matches!(character, '\u{2028}' | '\u{2029}')
})
}
Self::NoEllipsis | Self::Ellipsis => true,
}
}
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum TextAlign {
Start,
#[default]
Center,
End,
}
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct TextStroke {
pub color: Rgba,
pub width: f32,
}
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct TextShadow {
pub color: Rgba,
#[serde(default)]
pub offset: Vec2,
#[serde(default)]
pub blur: f32,
}
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct TextBackground {
pub color: Rgba,
#[serde(default)]
pub padding: f32,
}
impl TextStyle {
pub(crate) fn is_valid(&self, max_font_size: f32) -> bool {
let finite =
|value: Vec2| value.x.abs() <= MAX_TEXT_POSITION && value.y.abs() <= MAX_TEXT_POSITION;
self.font_family
.as_ref()
.is_none_or(|family| !family.is_empty() && family.len() <= MAX_FONT_FAMILY_BYTES)
&& self.font_size > 0.0
&& self.font_size <= max_font_size
&& self.fill.is_valid()
&& LINE_HEIGHT_RANGE.contains(&self.line_height)
&& LETTER_SPACING_RANGE.contains(&self.letter_spacing)
&& FONT_WEIGHT_RANGE.contains(&self.font_weight)
&& self.max_width > 0.0
&& self.max_width <= 1.0
&& self
.max_height
.is_none_or(|height| height > 0.0 && height <= 1.0)
&& self
.font_size_fit
.is_none_or(|fit| fit.is_valid(max_font_size))
&& self.line_clamp.as_ref().is_none_or(LineClamp::is_valid)
&& finite(self.position)
&& finite(self.anchor)
&& valid_strokes(&self.strokes)
&& valid_shadows(&self.shadows)
&& crate::text_animation::valid_animators(&self.animators, false)
&& self.background.is_none_or(|background| {
background.color.is_valid()
&& (0.0..=MAX_TEXT_PADDING).contains(&background.padding)
})
}
}
pub(crate) fn valid_strokes(strokes: &[TextStroke]) -> bool {
strokes.len() <= MAX_TEXT_STROKES
&& strokes
.iter()
.all(|stroke| stroke.color.is_valid() && stroke.width.is_finite() && stroke.width > 0.0)
&& strokes.iter().map(|stroke| stroke.width).sum::<f32>() <= MAX_TEXT_STROKE_EXTENT
}
pub(crate) fn valid_shadows(shadows: &[TextShadow]) -> bool {
let offset = |value: f32| value.is_finite() && value.abs() <= MAX_TEXT_SHADOW_OFFSET;
shadows.len() <= MAX_TEXT_SHADOWS
&& shadows.iter().all(|shadow| {
shadow.color.is_valid()
&& offset(shadow.offset.x)
&& offset(shadow.offset.y)
&& (0.0..=MAX_TEXT_SHADOW_BLUR).contains(&shadow.blur)
})
}