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    ToTyped,
60)]
61#[animation(no_bound(Url))]
62#[repr(C)]
63pub struct GenericSVGPaint<Color, Url> {
64    /// The paint source.
65    pub kind: GenericSVGPaintKind<Color, Url>,
66    /// The fallback color.
67    pub fallback: GenericSVGPaintFallback<Color>,
68}
69
70pub use self::GenericSVGPaint as SVGPaint;
71
72impl<C, U> Default for SVGPaint<C, U> {
73    fn default() -> Self {
74        Self {
75            kind: SVGPaintKind::None,
76            fallback: SVGPaintFallback::Unset,
77        }
78    }
79}
80
81/// An SVG paint value without the fallback.
82///
83/// Whereas the spec only allows PaintServer to have a fallback, Gecko lets the
84/// context properties have a fallback as well.
85#[derive(
86    Animate,
87    Clone,
88    ComputeSquaredDistance,
89    Debug,
90    MallocSizeOf,
91    PartialEq,
92    Parse,
93    SpecifiedValueInfo,
94    ToAnimatedValue,
95    ToAnimatedZero,
96    ToComputedValue,
97    ToCss,
98    ToResolvedValue,
99    ToShmem,
100)]
101#[animation(no_bound(U))]
102#[repr(C, u8)]
103pub enum GenericSVGPaintKind<C, U> {
104    /// `none`
105    #[animation(error)]
106    None,
107    /// `<color>`
108    Color(C),
109    /// `url(...)`
110    #[animation(error)]
111    PaintServer(U),
112    /// `context-fill`
113    ContextFill,
114    /// `context-stroke`
115    ContextStroke,
116}
117
118pub use self::GenericSVGPaintKind as SVGPaintKind;
119
120impl<C: Parse, U: Parse> Parse for SVGPaint<C, U> {
121    fn parse<'i, 't>(
122        context: &ParserContext,
123        input: &mut Parser<'i, 't>,
124    ) -> Result<Self, ParseError<'i>> {
125        let kind = SVGPaintKind::parse(context, input)?;
126        if matches!(kind, SVGPaintKind::None | SVGPaintKind::Color(..)) {
127            return Ok(SVGPaint {
128                kind,
129                fallback: SVGPaintFallback::Unset,
130            });
131        }
132        let fallback = input
133            .try_parse(|i| SVGPaintFallback::parse(context, i))
134            .unwrap_or(SVGPaintFallback::Unset);
135        Ok(SVGPaint { kind, fallback })
136    }
137}
138
139/// An SVG length value supports `context-value` in addition to length.
140#[derive(
141    Animate,
142    Clone,
143    ComputeSquaredDistance,
144    Copy,
145    Debug,
146    MallocSizeOf,
147    PartialEq,
148    SpecifiedValueInfo,
149    ToAnimatedValue,
150    ToAnimatedZero,
151    ToComputedValue,
152    ToCss,
153    ToResolvedValue,
154    ToShmem,
155    ToTyped,
156)]
157#[repr(C, u8)]
158pub enum GenericSVGLength<L> {
159    /// `<length> | <percentage> | <number>`
160    LengthPercentage(L),
161    /// `context-value`
162    #[animation(error)]
163    ContextValue,
164}
165
166pub use self::GenericSVGLength as SVGLength;
167
168/// Generic value for stroke-dasharray.
169#[derive(
170    Clone,
171    Debug,
172    MallocSizeOf,
173    PartialEq,
174    SpecifiedValueInfo,
175    ToAnimatedValue,
176    ToAnimatedZero,
177    ToComputedValue,
178    ToCss,
179    ToResolvedValue,
180    ToShmem,
181    ToTyped,
182)]
183#[repr(C, u8)]
184pub enum GenericSVGStrokeDashArray<L> {
185    /// `[ <length> | <percentage> | <number> ]#`
186    #[css(comma)]
187    Values(#[css(if_empty = "none", iterable)] crate::OwnedSlice<L>),
188    /// `context-value`
189    ContextValue,
190}
191
192pub use self::GenericSVGStrokeDashArray as SVGStrokeDashArray;
193
194/// An SVG opacity value accepts `context-{fill,stroke}-opacity` in
195/// addition to opacity value.
196#[derive(
197    Animate,
198    Clone,
199    ComputeSquaredDistance,
200    Copy,
201    Debug,
202    MallocSizeOf,
203    PartialEq,
204    Parse,
205    SpecifiedValueInfo,
206    ToAnimatedValue,
207    ToAnimatedZero,
208    ToComputedValue,
209    ToCss,
210    ToResolvedValue,
211    ToShmem,
212    ToTyped,
213)]
214#[repr(C, u8)]
215pub enum GenericSVGOpacity<OpacityType> {
216    /// `<opacity-value>`
217    Opacity(OpacityType),
218    /// `context-fill-opacity`
219    #[animation(error)]
220    ContextFillOpacity,
221    /// `context-stroke-opacity`
222    #[animation(error)]
223    ContextStrokeOpacity,
224}
225
226pub use self::GenericSVGOpacity as SVGOpacity;