style/values/computed/
text.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Computed types for text properties.
6
7use crate::values::computed::length::{Length, LengthPercentage};
8use crate::values::generics::text::{
9    GenericHyphenateLimitChars, GenericInitialLetter, GenericTextDecorationLength,
10    GenericTextDecorationTrim, GenericTextIndent,
11};
12use crate::values::generics::NumberOrAuto;
13use crate::values::specified::text as specified;
14use crate::values::specified::text::{TextEmphasisFillMode, TextEmphasisShapeKeyword};
15use crate::values::{CSSFloat, CSSInteger};
16use crate::Zero;
17use std::fmt::{self, Write};
18use style_traits::{CssWriter, ToCss};
19
20pub use crate::values::specified::text::{
21    HyphenateCharacter, LineBreak, MozControlCharacterVisibility, OverflowWrap, RubyPosition,
22    TextAlignLast, TextAutospace, TextDecorationLine, TextDecorationSkipInk, TextEmphasisPosition,
23    TextJustify, TextOverflow, TextTransform, TextUnderlinePosition, WordBreak,
24};
25
26/// A computed value for the `initial-letter` property.
27pub type InitialLetter = GenericInitialLetter<CSSFloat, CSSInteger>;
28
29/// Implements type for `text-decoration-thickness` property.
30pub type TextDecorationLength = GenericTextDecorationLength<LengthPercentage>;
31
32/// Implements type for `text-decoration-trim` property.
33pub type TextDecorationTrim = GenericTextDecorationTrim<Length>;
34
35/// The computed value of `text-align`.
36pub type TextAlign = specified::TextAlignKeyword;
37
38/// The computed value of `text-indent`.
39pub type TextIndent = GenericTextIndent<LengthPercentage>;
40
41/// A computed value for the `hyphenate-character` property.
42pub type HyphenateLimitChars = GenericHyphenateLimitChars<CSSInteger>;
43
44impl HyphenateLimitChars {
45    /// Return the `auto` value, which has all three component values as `auto`.
46    #[inline]
47    pub fn auto() -> Self {
48        Self {
49            total_word_length: NumberOrAuto::Auto,
50            pre_hyphen_length: NumberOrAuto::Auto,
51            post_hyphen_length: NumberOrAuto::Auto,
52        }
53    }
54}
55
56/// A computed value for the `letter-spacing` property.
57#[repr(transparent)]
58#[derive(
59    Animate,
60    Clone,
61    ComputeSquaredDistance,
62    Copy,
63    Debug,
64    MallocSizeOf,
65    PartialEq,
66    ToAnimatedValue,
67    ToAnimatedZero,
68    ToResolvedValue,
69    ToTyped,
70)]
71pub struct GenericLetterSpacing<L>(pub L);
72/// This is generic just to make the #[derive()] code do the right thing for lengths.
73pub type LetterSpacing = GenericLetterSpacing<LengthPercentage>;
74
75impl LetterSpacing {
76    /// Return the `normal` computed value, which is just zero.
77    #[inline]
78    pub fn normal() -> Self {
79        Self(LengthPercentage::zero())
80    }
81}
82
83impl ToCss for LetterSpacing {
84    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
85    where
86        W: Write,
87    {
88        // https://drafts.csswg.org/css-text/#propdef-letter-spacing
89        //
90        // For legacy reasons, a computed letter-spacing of zero yields a
91        // resolved value (getComputedStyle() return value) of normal.
92        if self.0.is_zero() {
93            return dest.write_str("normal");
94        }
95        self.0.to_css(dest)
96    }
97}
98
99/// A computed value for the `word-spacing` property.
100pub type WordSpacing = LengthPercentage;
101
102impl WordSpacing {
103    /// Return the `normal` computed value, which is just zero.
104    #[inline]
105    pub fn normal() -> Self {
106        LengthPercentage::zero()
107    }
108}
109
110/// Computed value for the text-emphasis-style property
111#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss, ToResolvedValue, ToTyped)]
112#[allow(missing_docs)]
113#[repr(C, u8)]
114pub enum TextEmphasisStyle {
115    /// [ <fill> || <shape> ]
116    Keyword {
117        #[css(skip_if = "TextEmphasisFillMode::is_filled")]
118        fill: TextEmphasisFillMode,
119        shape: TextEmphasisShapeKeyword,
120    },
121    /// `none`
122    None,
123    /// `<string>` (of which only the first grapheme cluster will be used).
124    String(crate::OwnedStr),
125}