use crate::paint::{Color, DecorationStyle};
use crate::style::{
LengthPercentage, StyleError,
box_values::{length_percentage, unsupported},
component::Component,
grammar::Keyword,
units::Relative,
values::length,
};
use serde::Serialize;
const SPELLING_ERROR_COLOR: Color = Color {
rgb: [255, 0, 0],
alpha: 1.0,
};
const GRAMMAR_ERROR_COLOR: Color = Color {
rgb: [0, 128, 0],
alpha: 1.0,
};
const ERROR_STYLE: DecorationStyle = DecorationStyle::Wavy;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub(crate) enum ErrorIndication {
Spelling,
Grammar,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub(crate) struct DecorationLines {
pub underline: bool,
pub overline: bool,
pub line_through: bool,
pub error: Option<ErrorIndication>,
}
impl DecorationLines {
pub(crate) fn is_empty(self) -> bool {
!(self.underline || self.overline || self.line_through || self.error.is_some())
}
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::SpellingError => lines.error = Some(ErrorIndication::Spelling),
Keyword::GrammarError => lines.error = Some(ErrorIndication::Grammar),
Keyword::None | Keyword::Blink => {}
_ => return Err(unsupported(value)),
}
}
Ok(lines)
}
}
impl TextDecoration {
pub(crate) fn paint(&self) -> (DecorationStyle, Color) {
match self.lines.error {
Some(ErrorIndication::Spelling) => (ERROR_STYLE, SPELLING_ERROR_COLOR),
Some(ErrorIndication::Grammar) => (ERROR_STYLE, GRAMMAR_ERROR_COLOR),
None => (self.style, self.color),
}
}
}
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 current_color: bool,
pub thickness: Option<f32>,
pub inset: DecorationInset,
pub edges: bool,
pub skip_ink: bool,
}
impl Default for TextDecoration {
fn default() -> Self {
Self {
lines: DecorationLines::default(),
style: DecorationStyle::Solid,
color: Color::BLACK,
current_color: true,
thickness: None,
inset: DecorationInset::default(),
edges: false,
skip_ink: false,
}
}
}
impl TextDecoration {
pub(super) fn zoomed(mut self, zoom: f32) -> Self {
self.thickness = self.thickness.map(|width| width * zoom);
if let DecorationInset::Lengths(lengths) = &mut self.inset {
*lengths = lengths.map(|length| length * zoom);
}
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub(crate) enum DecorationInset {
Auto,
Lengths([f32; 2]),
}
impl Default for DecorationInset {
fn default() -> Self {
Self::Lengths([0.0; 2])
}
}
impl DecorationInset {
pub(super) fn from_component(
value: &Component,
relative: &Relative<'_>,
) -> Result<Self, StyleError> {
if value.keyword() == Some(Keyword::Auto) {
return Ok(Self::Auto);
}
let lengths = value
.items()
.iter()
.map(|item| length(item, relative))
.collect::<Result<Vec<_>, _>>()?;
match lengths[..] {
[both] => Ok(Self::Lengths([both; 2])),
[start, end] => Ok(Self::Lengths([start, end])),
_ => Err(unsupported(value)),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub(crate) struct DecorationSkip {
pub objects: bool,
pub spaces: bool,
pub leading_spaces: bool,
pub trailing_spaces: bool,
pub box_decoration: bool,
}
impl Default for DecorationSkip {
fn default() -> Self {
Self {
objects: true,
spaces: false,
leading_spaces: false,
trailing_spaces: false,
box_decoration: false,
}
}
}
impl DecorationSkip {
pub(super) fn from_component(value: &Component) -> Result<(Self, bool), StyleError> {
let mut keywords = Vec::new();
value.keywords(&mut keywords);
let mut skip = Self {
objects: false,
..Self::default()
};
let mut edges = false;
for keyword in keywords {
match keyword {
Keyword::None => {}
Keyword::Objects => skip.objects = true,
Keyword::Spaces => skip.spaces = true,
Keyword::LeadingSpaces => skip.leading_spaces = true,
Keyword::TrailingSpaces => skip.trailing_spaces = true,
Keyword::BoxDecoration => skip.box_decoration = true,
Keyword::Edges => edges = true,
_ => return Err(unsupported(value)),
}
}
Ok((skip, edges))
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub(crate) struct UnderlinePosition {
pub under: bool,
pub right: Option<bool>,
}
impl UnderlinePosition {
pub(super) fn from_component(value: &Component) -> Self {
let mut keywords = Vec::new();
value.keywords(&mut keywords);
Self {
under: keywords.contains(&Keyword::Under),
right: if keywords.contains(&Keyword::Right) {
Some(true)
} else if keywords.contains(&Keyword::Left) {
Some(false)
} else {
None
},
}
}
}
#[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>,
pub skip: DecorationSkip,
}
impl TextDecorations {
pub(super) fn propagated(&self, zoom: f32) -> Vec<TextDecoration> {
let mut propagated = self.propagated.clone();
if !self.declared.lines.is_empty() {
propagated.push(self.declared.zoomed(zoom));
}
propagated
}
pub(crate) fn applied(&self) -> impl Iterator<Item = &TextDecoration> {
self.propagated
.iter()
.chain((!self.declared.lines.is_empty()).then_some(&self.declared))
}
}