use crate::derives::*;
#[cfg(feature = "gecko")]
use crate::gecko_bindings::bindings;
use crate::typed_om::{KeywordValue, ToTyped, TypedValue};
use crate::values::animated::text::TextDecorationInset as AnimatedTextDecorationInset;
use crate::values::animated::{Context as AnimatedContext, ToAnimatedValue};
use crate::values::computed::length::{CSSPixelLength, LengthPercentage};
use crate::values::generics::text::{
GenericHyphenateLimitChars, GenericInitialLetter, GenericTextDecorationInset,
GenericTextDecorationLength, GenericTextIndent,
};
use crate::values::generics::NumberOrAuto;
use crate::values::specified::text as specified;
use crate::values::specified::text::{TextEmphasisFillMode, TextEmphasisShapeKeyword};
use crate::values::{CSSFloat, CSSInteger, ComputeSquaredDistance};
use crate::Zero;
use std::fmt::{self, Write};
use style_traits::{CssString, CssWriter, ToCss};
use thin_vec::ThinVec;
pub use crate::values::specified::text::{
HyphenateCharacter, LineBreak, MozControlCharacterVisibility, OverflowWrap, RubyPosition,
TextAlignLast, TextAutospace, TextBoxEdge, TextBoxTrim, TextDecorationLine,
TextDecorationSkipInk, TextEmphasisPosition, TextJustify, TextOverflow, TextTransform,
TextUnderlinePosition, WordBreak,
};
pub type InitialLetter = GenericInitialLetter<CSSFloat, CSSInteger>;
pub type TextDecorationLength = GenericTextDecorationLength<LengthPercentage>;
pub type TextDecorationInset = GenericTextDecorationInset<LengthPercentage>;
impl ToAnimatedValue for TextDecorationInset {
type AnimatedValue = AnimatedTextDecorationInset;
fn to_animated_value(self, context: &AnimatedContext) -> Self::AnimatedValue {
match self {
Self::Auto => {
let font_size_px = context
.style
.get_font()
.clone_font_size()
.computed_size()
.px();
#[cfg(feature = "gecko")]
let auto_length = bindings::Gecko_CalcAutoDecorationInset(font_size_px);
#[cfg(feature = "servo")]
let auto_length = {
let auto_inset_factor = 1.0 / 12.5;
(font_size_px * auto_inset_factor).max(1.0)
};
let auto_length = CSSPixelLength::new(auto_length);
Self::AnimatedValue {
start: LengthPercentage::new_length(auto_length),
end: LengthPercentage::new_length(auto_length),
is_auto: true,
}
},
Self::LengthPercentage { start, end } => Self::AnimatedValue {
start: start.to_animated_value(context),
end: end.to_animated_value(context),
is_auto: false,
},
}
}
#[inline]
fn from_animated_value(value: Self::AnimatedValue) -> Self {
if value.is_auto {
Self::Auto
} else {
Self::LengthPercentage {
start: value.start,
end: value.end,
}
}
}
}
pub type TextAlign = specified::TextAlignKeyword;
pub type TextIndent = GenericTextIndent<LengthPercentage>;
pub type HyphenateLimitChars = GenericHyphenateLimitChars<CSSInteger>;
impl HyphenateLimitChars {
#[inline]
pub fn auto() -> Self {
Self {
total_word_length: NumberOrAuto::Auto,
pre_hyphen_length: NumberOrAuto::Auto,
post_hyphen_length: NumberOrAuto::Auto,
}
}
}
#[repr(transparent)]
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
ToAnimatedValue,
ToAnimatedZero,
ToResolvedValue,
)]
pub struct GenericLetterSpacing<L>(pub L);
pub type LetterSpacing = GenericLetterSpacing<LengthPercentage>;
impl LetterSpacing {
#[inline]
pub fn normal() -> Self {
Self(LengthPercentage::zero())
}
}
impl ToCss for LetterSpacing {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
if self.0.is_zero() {
return dest.write_str("normal");
}
self.0.to_css(dest)
}
}
impl ToTyped for LetterSpacing {
fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
if !self.0.has_percentage() && self.0.is_zero() {
dest.push(TypedValue::Keyword(KeywordValue(CssString::from("normal"))));
return Ok(());
}
self.0.to_typed(dest)
}
}
pub type WordSpacing = LengthPercentage;
impl WordSpacing {
#[inline]
pub fn normal() -> Self {
LengthPercentage::zero()
}
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss, ToResolvedValue, ToTyped)]
#[allow(missing_docs)]
#[repr(C, u8)]
#[typed(todo_derive_fields)]
pub enum TextEmphasisStyle {
Keyword {
#[css(skip_if = "TextEmphasisFillMode::is_filled")]
fill: TextEmphasisFillMode,
shape: TextEmphasisShapeKeyword,
},
None,
String(crate::OwnedStr),
}