1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! Generic types for CSS Motion Path.

use crate::values::animated::ToAnimatedZero;
use crate::values::generics::position::{GenericPosition, GenericPositionOrAuto};
use crate::values::specified::motion::CoordBox;
use serde::Deserializer;
use std::fmt::{self, Write};
use style_traits::{CssWriter, ToCss};

/// The <size> in ray() function.
///
/// https://drafts.fxtf.org/motion-1/#valdef-offsetpath-size
#[allow(missing_docs)]
#[derive(
    Animate,
    Clone,
    ComputeSquaredDistance,
    Copy,
    Debug,
    Deserialize,
    MallocSizeOf,
    Parse,
    PartialEq,
    Serialize,
    SpecifiedValueInfo,
    ToAnimatedValue,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[repr(u8)]
pub enum RaySize {
    ClosestSide,
    ClosestCorner,
    FarthestSide,
    FarthestCorner,
    Sides,
}

/// The `ray()` function, `ray( [ <angle> && <size> && contain? && [at <position>]? ] )`
///
/// https://drafts.fxtf.org/motion-1/#valdef-offsetpath-ray
#[derive(
    Animate,
    Clone,
    ComputeSquaredDistance,
    Debug,
    Deserialize,
    MallocSizeOf,
    PartialEq,
    Serialize,
    SpecifiedValueInfo,
    ToAnimatedValue,
    ToComputedValue,
    ToResolvedValue,
    ToShmem,
)]
#[repr(C)]
pub struct GenericRayFunction<Angle, Position> {
    /// The bearing angle with `0deg` pointing up and positive angles
    /// representing clockwise rotation.
    pub angle: Angle,
    /// Decide the path length used when `offset-distance` is expressed
    /// as a percentage.
    pub size: RaySize,
    /// Clamp `offset-distance` so that the box is entirely contained
    /// within the path.
    #[animation(constant)]
    pub contain: bool,
    /// The "at <position>" part. If omitted, we use auto to represent it.
    pub position: GenericPositionOrAuto<Position>,
}

pub use self::GenericRayFunction as RayFunction;

impl<Angle, Position> ToCss for RayFunction<Angle, Position>
where
    Angle: ToCss,
    Position: ToCss,
{
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        self.angle.to_css(dest)?;

        if !matches!(self.size, RaySize::ClosestSide) {
            dest.write_char(' ')?;
            self.size.to_css(dest)?;
        }

        if self.contain {
            dest.write_str(" contain")?;
        }

        if !matches!(self.position, GenericPositionOrAuto::Auto) {
            dest.write_str(" at ")?;
            self.position.to_css(dest)?;
        }

        Ok(())
    }
}

/// Return error if we try to deserialize the url, for Gecko IPC purposes.
// Note: we cannot use #[serde(skip_deserializing)] variant attribute, which may cause the fatal
// error when trying to read the parameters because it cannot deserialize the input byte buffer,
// even if the type of OffsetPathFunction is not an url(), in our tests. This may be an issue of
// #[serde(skip_deserializing)] on enum, at least in the version (1.0) we are using. So we have to
// manually implement this deseriailzing function, but return error.
// FIXME: Bug 1847620, fiure out this is a serde issue or a gecko bug.
fn deserialize_url<'de, D, T>(_deserializer: D) -> Result<T, D::Error>
where
    D: Deserializer<'de>,
{
    use crate::serde::de::Error;
    // Return Err() so the IPC will catch it and assert this as a fetal error.
    Err(<D as Deserializer>::Error::custom(
        "we don't support the deserializing for url",
    ))
}

/// The <offset-path> value.
/// <offset-path> = <ray()> | <url> | <basic-shape>
///
/// https://drafts.fxtf.org/motion-1/#typedef-offset-path
#[derive(
    Animate,
    Clone,
    ComputeSquaredDistance,
    Debug,
    Deserialize,
    MallocSizeOf,
    PartialEq,
    Serialize,
    SpecifiedValueInfo,
    ToAnimatedValue,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[animation(no_bound(U))]
#[repr(C, u8)]
pub enum GenericOffsetPathFunction<Shapes, RayFunction, U> {
    /// ray() function, which defines a path in the polar coordinate system.
    /// Use Box<> to make sure the size of offset-path is not too large.
    #[css(function)]
    Ray(RayFunction),
    /// A URL reference to an SVG shape element. If the URL does not reference a shape element,
    /// this behaves as path("m 0 0") instead.
    #[animation(error)]
    #[serde(deserialize_with = "deserialize_url")]
    #[serde(skip_serializing)]
    Url(U),
    /// The <basic-shape> value.
    Shape(Shapes),
}

pub use self::GenericOffsetPathFunction as OffsetPathFunction;

/// The offset-path property.
/// offset-path: none | <offset-path> || <coord-box>
///
/// https://drafts.fxtf.org/motion-1/#offset-path-property
#[derive(
    Animate,
    Clone,
    ComputeSquaredDistance,
    Debug,
    Deserialize,
    MallocSizeOf,
    PartialEq,
    Serialize,
    SpecifiedValueInfo,
    ToAnimatedValue,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[repr(C, u8)]
pub enum GenericOffsetPath<Function> {
    /// <offset-path> || <coord-box>.
    OffsetPath {
        /// <offset-path> part.
        // Note: Use Box<> to make sure the size of this property doesn't go over the threshold.
        path: Box<Function>,
        /// <coord-box> part.
        #[css(skip_if = "CoordBox::is_default")]
        coord_box: CoordBox,
    },
    /// Only <coord-box>. This represents that <offset-path> is omitted, so we use the default
    /// value, inset(0 round X), where X is the value of border-radius on the element that
    /// establishes the containing block for this element.
    CoordBox(CoordBox),
    /// None value.
    #[animation(error)]
    None,
}

pub use self::GenericOffsetPath as OffsetPath;

impl<Function> OffsetPath<Function> {
    /// Return None.
    #[inline]
    pub fn none() -> Self {
        OffsetPath::None
    }
}

impl<Function> ToAnimatedZero for OffsetPath<Function> {
    #[inline]
    fn to_animated_zero(&self) -> Result<Self, ()> {
        Err(())
    }
}

/// The offset-position property, which specifies the offset starting position that is used by the
/// <offset-path> functions if they don’t specify their own starting position.
///
/// https://drafts.fxtf.org/motion-1/#offset-position-property
#[derive(
    Animate,
    Clone,
    ComputeSquaredDistance,
    Copy,
    Debug,
    Deserialize,
    MallocSizeOf,
    Parse,
    PartialEq,
    Serialize,
    SpecifiedValueInfo,
    ToAnimatedValue,
    ToAnimatedZero,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[repr(C, u8)]
pub enum GenericOffsetPosition<H, V> {
    /// The element does not have an offset starting position.
    Normal,
    /// The offset starting position is the top-left corner of the box.
    Auto,
    /// The offset starting position is the result of using the <position> to position a 0x0 object
    /// area within the box’s containing block.
    Position(
        #[css(field_bound)]
        #[parse(field_bound)]
        GenericPosition<H, V>,
    ),
}

pub use self::GenericOffsetPosition as OffsetPosition;

impl<H, V> OffsetPosition<H, V> {
    /// Returns the initial value, normal.
    #[inline]
    pub fn normal() -> Self {
        Self::Normal
    }
}