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