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 //! Generic types for text properties.
6 
7 use crate::parser::ParserContext;
8 use crate::values::animated::ToAnimatedZero;
9 use cssparser::Parser;
10 use style_traits::ParseError;
11 
12 /// A generic value for the `initial-letter` property.
13 #[derive(
14     Clone,
15     Copy,
16     Debug,
17     MallocSizeOf,
18     PartialEq,
19     SpecifiedValueInfo,
20     ToComputedValue,
21     ToCss,
22     ToResolvedValue,
23     ToShmem,
24 )]
25 pub enum InitialLetter<Number, Integer> {
26     /// `normal`
27     Normal,
28     /// `<number> <integer>?`
29     Specified(Number, Option<Integer>),
30 }
31 
32 impl<N, I> InitialLetter<N, I> {
33     /// Returns `normal`.
34     #[inline]
normal() -> Self35     pub fn normal() -> Self {
36         InitialLetter::Normal
37     }
38 }
39 
40 /// A generic spacing value for the `letter-spacing` and `word-spacing` properties.
41 #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)]
42 pub enum Spacing<Value> {
43     /// `normal`
44     Normal,
45     /// `<value>`
46     Value(Value),
47 }
48 
49 impl<Value> Spacing<Value> {
50     /// Returns `normal`.
51     #[inline]
normal() -> Self52     pub fn normal() -> Self {
53         Spacing::Normal
54     }
55 
56     /// Parses.
57     #[inline]
parse_with<'i, 't, F>( context: &ParserContext, input: &mut Parser<'i, 't>, parse: F, ) -> Result<Self, ParseError<'i>> where F: FnOnce(&ParserContext, &mut Parser<'i, 't>) -> Result<Value, ParseError<'i>>,58     pub fn parse_with<'i, 't, F>(
59         context: &ParserContext,
60         input: &mut Parser<'i, 't>,
61         parse: F,
62     ) -> Result<Self, ParseError<'i>>
63     where
64         F: FnOnce(&ParserContext, &mut Parser<'i, 't>) -> Result<Value, ParseError<'i>>,
65     {
66         if input
67             .try_parse(|i| i.expect_ident_matching("normal"))
68             .is_ok()
69         {
70             return Ok(Spacing::Normal);
71         }
72         parse(context, input).map(Spacing::Value)
73     }
74 }
75 
76 #[cfg(feature = "gecko")]
line_height_moz_block_height_enabled(context: &ParserContext) -> bool77 fn line_height_moz_block_height_enabled(context: &ParserContext) -> bool {
78     context.in_ua_sheet() ||
79         static_prefs::pref!("layout.css.line-height-moz-block-height.content.enabled")
80 }
81 
82 /// A generic value for the `line-height` property.
83 #[derive(
84     Animate,
85     Clone,
86     ComputeSquaredDistance,
87     Copy,
88     Debug,
89     MallocSizeOf,
90     PartialEq,
91     SpecifiedValueInfo,
92     ToAnimatedValue,
93     ToCss,
94     ToShmem,
95     ToResolvedValue,
96     Parse,
97 )]
98 #[repr(C, u8)]
99 pub enum GenericLineHeight<N, L> {
100     /// `normal`
101     Normal,
102     /// `-moz-block-height`
103     #[cfg(feature = "gecko")]
104     #[parse(condition = "line_height_moz_block_height_enabled")]
105     MozBlockHeight,
106     /// `<number>`
107     Number(N),
108     /// `<length-percentage>`
109     Length(L),
110 }
111 
112 pub use self::GenericLineHeight as LineHeight;
113 
114 impl<N, L> ToAnimatedZero for LineHeight<N, L> {
115     #[inline]
to_animated_zero(&self) -> Result<Self, ()>116     fn to_animated_zero(&self) -> Result<Self, ()> {
117         Err(())
118     }
119 }
120 
121 impl<N, L> LineHeight<N, L> {
122     /// Returns `normal`.
123     #[inline]
normal() -> Self124     pub fn normal() -> Self {
125         LineHeight::Normal
126     }
127 }
128 
129 /// Implements type for text-decoration-thickness
130 /// which takes the grammar of auto | from-font | <length> | <percentage>
131 ///
132 /// https://drafts.csswg.org/css-text-decor-4/
133 #[repr(C, u8)]
134 #[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
135 #[derive(
136     Animate,
137     Clone,
138     Copy,
139     ComputeSquaredDistance,
140     ToAnimatedZero,
141     Debug,
142     Eq,
143     MallocSizeOf,
144     Parse,
145     PartialEq,
146     SpecifiedValueInfo,
147     ToComputedValue,
148     ToCss,
149     ToResolvedValue,
150     ToShmem,
151 )]
152 #[allow(missing_docs)]
153 pub enum GenericTextDecorationLength<L> {
154     LengthPercentage(L),
155     Auto,
156     FromFont,
157 }
158