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