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(context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
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    ToTyped,
155)]
156#[repr(C, u8)]
157pub enum GenericSVGLength<L> {
158    /// `<length> | <percentage> | <number>`
159    LengthPercentage(L),
160    /// `context-value`
161    #[animation(error)]
162    ContextValue,
163}
164
165pub use self::GenericSVGLength as SVGLength;
166
167/// Generic value for stroke-dasharray.
168#[derive(
169    Clone,
170    Debug,
171    MallocSizeOf,
172    PartialEq,
173    SpecifiedValueInfo,
174    ToAnimatedValue,
175    ToAnimatedZero,
176    ToComputedValue,
177    ToCss,
178    ToResolvedValue,
179    ToShmem,
180    ToTyped,
181)]
182#[repr(C, u8)]
183pub enum GenericSVGStrokeDashArray<L> {
184    /// `[ <length> | <percentage> | <number> ]#`
185    #[css(comma)]
186    #[typed(no_multiple_values)]
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;