use crate::paint::{Color, DecorationStyle};
use crate::style::{
LengthPercentage, StyleError,
box_values::{length_percentage, unsupported},
component::Component,
grammar::Keyword,
units::Relative,
};
use serde::Serialize;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub(crate) struct DecorationLines {
pub underline: bool,
pub overline: bool,
pub line_through: bool,
}
impl DecorationLines {
pub(crate) fn is_empty(self) -> bool {
!(self.underline || self.overline || self.line_through)
}
pub(super) fn from_component(value: &Component) -> Result<Self, StyleError> {
let mut keywords = Vec::new();
value.keywords(&mut keywords);
let mut lines = Self::default();
for keyword in keywords {
match keyword {
Keyword::Underline => lines.underline = true,
Keyword::Overline => lines.overline = true,
Keyword::LineThrough => lines.line_through = true,
Keyword::None | Keyword::Blink => {}
_ => return Err(unsupported(value)),
}
}
Ok(lines)
}
}
pub(super) fn decoration_style(value: &Component) -> Result<DecorationStyle, StyleError> {
match value.keyword() {
Some(Keyword::Solid) => Ok(DecorationStyle::Solid),
Some(Keyword::Double) => Ok(DecorationStyle::Double),
Some(Keyword::Dotted) => Ok(DecorationStyle::Dotted),
Some(Keyword::Dashed) => Ok(DecorationStyle::Dashed),
Some(Keyword::Wavy) => Ok(DecorationStyle::Wavy),
_ => Err(unsupported(value)),
}
}
pub(super) fn thickness(
value: &Component,
relative: &Relative<'_>,
) -> Result<Option<f32>, StyleError> {
match value.keyword() {
Some(Keyword::Auto | Keyword::FromFont) => Ok(None),
_ => Ok(Some(crate::style::box_values::font_length_percentage(
value, relative,
)?)),
}
}
pub(super) fn underline_offset(
value: &Component,
relative: &Relative<'_>,
) -> Result<Option<LengthPercentage>, StyleError> {
match value.keyword() {
Some(Keyword::Auto) => Ok(None),
_ => length_percentage(value, relative).map(Some),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub(crate) struct TextDecoration {
pub lines: DecorationLines,
pub style: DecorationStyle,
pub color: Color,
pub thickness: Option<f32>,
}
impl Default for TextDecoration {
fn default() -> Self {
Self {
lines: DecorationLines::default(),
style: DecorationStyle::Solid,
color: Color::BLACK,
thickness: None,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub(crate) enum UnderlinePosition {
#[default]
Font,
Under,
}
impl UnderlinePosition {
pub(super) fn from_component(value: &Component) -> Self {
let mut keywords = Vec::new();
value.keywords(&mut keywords);
if keywords.contains(&Keyword::Under) {
Self::Under
} else {
Self::Font
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
pub(crate) struct TextDecorations {
pub declared: TextDecoration,
pub propagated: Vec<TextDecoration>,
pub underline_position: UnderlinePosition,
pub underline_offset: Option<LengthPercentage>,
}
impl TextDecorations {
pub(super) fn propagated(&self) -> Vec<TextDecoration> {
let mut propagated = self.propagated.clone();
if !self.declared.lines.is_empty() {
propagated.push(self.declared);
}
propagated
}
pub(crate) fn applied(&self) -> impl Iterator<Item = &TextDecoration> {
self.propagated
.iter()
.chain((!self.declared.lines.is_empty()).then_some(&self.declared))
}
}