Skip to main content

style/values/generics/
svg.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//! Generic types for CSS values in SVG
6
7use crate::derives::*;
8use crate::parser::{Parse, ParserContext};
9use cssparser::Parser;
10use style_traits::ParseError;
11
12/// The fallback of an SVG paint server value.
13#[derive(
14    Animate,
15    Clone,
16    ComputeSquaredDistance,
17    Debug,
18    MallocSizeOf,
19    PartialEq,
20    Parse,
21    SpecifiedValueInfo,
22    ToAnimatedValue,
23    ToAnimatedZero,
24    ToComputedValue,
25    ToCss,
26    ToResolvedValue,
27    ToShmem,
28)]
29#[repr(C, u8)]
30pub enum GenericSVGPaintFallback<C> {
31    /// The `none` keyword.
32    None,
33    /// A magic value that represents no fallback specified and serializes to
34    /// the empty string.
35    #[css(skip)]
36    Unset,
37    /// A color.
38    Color(C),
39}
40
41pub use self::GenericSVGPaintFallback as SVGPaintFallback;
42
43/// An SVG paint value
44///
45/// <https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint>
46#[derive(
47    Animate,
48    Clone,
49    ComputeSquaredDistance,
50    Debug,
51    MallocSizeOf,
52    PartialEq,
53    SpecifiedValueInfo,
54    ToAnimatedValue,
55    ToAnimatedZero,
56    ToComputedValue,
57    ToCss,
58    ToResolvedValue,
59    ToShmem,
60    ToTyped,
61)]
62#[animation(no_bound(Url))]
63#[repr(C)]
64#[typed(todo_derive_fields)]
65pub struct GenericSVGPaint<Color, Url> {
66    /// The paint source.
67    pub kind: GenericSVGPaintKind<Color, Url>,
68    /// The fallback color.
69    pub fallback: GenericSVGPaintFallback<Color>,
70}
71
72pub use self::GenericSVGPaint as SVGPaint;
73
74impl<C, U> Default for SVGPaint<C, U> {
75    fn default() -> Self {
76        Self {
77            kind: SVGPaintKind::None,
78            fallback: SVGPaintFallback::Unset,
79        }
80    }
81}
82
83/// An SVG paint value without the fallback.
84///
85/// Whereas the spec only allows PaintServer to have a fallback, Gecko lets the
86/// context properties have a fallback as well.
87#[derive(
88    Animate,
89    Clone,
90    ComputeSquaredDistance,
91    Debug,
92    MallocSizeOf,
93    PartialEq,
94    Parse,
95    SpecifiedValueInfo,
96    ToAnimatedValue,
97    ToAnimatedZero,
98    ToComputedValue,
99    ToCss,
100    ToResolvedValue,
101    ToShmem,
102)]
103#[animation(no_bound(U))]
104#[repr(C, u8)]
105pub enum GenericSVGPaintKind<C, U> {
106    /// `none`
107    #[animation(error)]
108    None,
109    /// `<color>`
110    Color(C),
111    /// `url(...)`
112    #[animation(error)]
113    PaintServer(U),
114    /// `context-fill`
115    ContextFill,
116    /// `context-stroke`
117    ContextStroke,
118}
119
120pub use self::GenericSVGPaintKind as SVGPaintKind;
121
122impl<C: Parse, U: Parse> Parse for SVGPaint<C, U> {
123    fn parse<'i, 't>(
124        context: &ParserContext,
125        input: &mut Parser<'i, 't>,
126    ) -> Result<Self, ParseError<'i>> {
127        let kind = SVGPaintKind::parse(context, input)?;
128        if matches!(kind, SVGPaintKind::None | SVGPaintKind::Color(..)) {
129            return Ok(SVGPaint {
130                kind,
131                fallback: SVGPaintFallback::Unset,
132            });
133        }
134        let fallback = input
135            .try_parse(|i| SVGPaintFallback::parse(context, i))
136            .unwrap_or(SVGPaintFallback::Unset);
137        Ok(SVGPaint { kind, fallback })
138    }
139}
140
141/// An SVG length value supports `context-value` in addition to length.
142#[derive(
143    Animate,
144    Clone,
145    ComputeSquaredDistance,
146    Copy,
147    Debug,
148    MallocSizeOf,
149    PartialEq,
150    SpecifiedValueInfo,
151    ToAnimatedValue,
152    ToAnimatedZero,
153    ToComputedValue,
154    ToCss,
155    ToResolvedValue,
156    ToShmem,
157    ToTyped,
158)]
159#[repr(C, u8)]
160#[typed(todo_derive_fields)]
161pub enum GenericSVGLength<L> {
162    /// `<length> | <percentage> | <number>`
163    LengthPercentage(L),
164    /// `context-value`
165    #[animation(error)]
166    ContextValue,
167}
168
169pub use self::GenericSVGLength as SVGLength;
170
171/// Generic value for stroke-dasharray.
172#[derive(
173    Clone,
174    Debug,
175    MallocSizeOf,
176    PartialEq,
177    SpecifiedValueInfo,
178    ToAnimatedValue,
179    ToAnimatedZero,
180    ToComputedValue,
181    ToCss,
182    ToResolvedValue,
183    ToShmem,
184    ToTyped,
185)]
186#[repr(C, u8)]
187#[typed(todo_derive_fields)]
188pub enum GenericSVGStrokeDashArray<L> {
189    /// `[ <length> | <percentage> | <number> ]#`
190    #[css(comma)]
191    Values(#[css(if_empty = "none", iterable)] crate::OwnedSlice<L>),
192    /// `context-value`
193    ContextValue,
194}
195
196pub use self::GenericSVGStrokeDashArray as SVGStrokeDashArray;
197
198/// An SVG opacity value accepts `context-{fill,stroke}-opacity` in
199/// addition to opacity value.
200#[derive(
201    Animate,
202    Clone,
203    ComputeSquaredDistance,
204    Copy,
205    Debug,
206    MallocSizeOf,
207    PartialEq,
208    Parse,
209    SpecifiedValueInfo,
210    ToAnimatedValue,
211    ToAnimatedZero,
212    ToComputedValue,
213    ToCss,
214    ToResolvedValue,
215    ToShmem,
216    ToTyped,
217)]
218#[repr(C, u8)]
219#[typed(todo_derive_fields)]
220pub enum GenericSVGOpacity<OpacityType> {
221    /// `<opacity-value>`
222    Opacity(OpacityType),
223    /// `context-fill-opacity`
224    #[animation(error)]
225    ContextFillOpacity,
226    /// `context-stroke-opacity`
227    #[animation(error)]
228    ContextStrokeOpacity,
229}
230
231pub use self::GenericSVGOpacity as SVGOpacity;