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)]
70pub struct GenericLetterSpacing<L>(pub L);
71/// This is generic just to make the #[derive()] code do the right thing for lengths.
72pub type LetterSpacing = GenericLetterSpacing<LengthPercentage>;
73
74impl LetterSpacing {
75    /// Return the `normal` computed value, which is just zero.
76    #[inline]
77    pub fn normal() -> Self {
78        Self(LengthPercentage::zero())
79    }
80}
81
82impl ToCss for LetterSpacing {
83    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
84    where
85        W: Write,
86    {
87        // https://drafts.csswg.org/css-text/#propdef-letter-spacing
88        //
89        // For legacy reasons, a computed letter-spacing of zero yields a
90        // resolved value (getComputedStyle() return value) of normal.
91        if self.0.is_zero() {
92            return dest.write_str("normal");
93        }
94        self.0.to_css(dest)
95    }
96}
97
98/// A computed value for the `word-spacing` property.
99pub type WordSpacing = LengthPercentage;
100
101impl WordSpacing {
102    /// Return the `normal` computed value, which is just zero.
103    #[inline]
104    pub fn normal() -> Self {
105        LengthPercentage::zero()
106    }
107}
108
109/// Computed value for the text-emphasis-style property
110#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss, ToResolvedValue)]
111#[allow(missing_docs)]
112#[repr(C, u8)]
113pub enum TextEmphasisStyle {
114    /// [ <fill> || <shape> ]
115    Keyword {
116        #[css(skip_if = "TextEmphasisFillMode::is_filled")]
117        fill: TextEmphasisFillMode,
118        shape: TextEmphasisShapeKeyword,
119    },
120    /// `none`
121    None,
122    /// `<string>` (of which only the first grapheme cluster will be used).
123    String(crate::OwnedStr),
124}