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)]
160pub enum GenericSVGLength<L> {
161    /// `<length> | <percentage> | <number>`
162    LengthPercentage(L),
163    /// `context-value`
164    #[animation(error)]
165    ContextValue,
166}
167
168pub use self::GenericSVGLength as SVGLength;
169
170/// Generic value for stroke-dasharray.
171#[derive(
172    Clone,
173    Debug,
174    MallocSizeOf,
175    PartialEq,
176    SpecifiedValueInfo,
177    ToAnimatedValue,
178    ToAnimatedZero,
179    ToComputedValue,
180    ToCss,
181    ToResolvedValue,
182    ToShmem,
183    ToTyped,
184)]
185#[repr(C, u8)]
186pub enum GenericSVGStrokeDashArray<L> {
187    /// `[ <length> | <percentage> | <number> ]#`
188    #[css(comma)]
189    #[typed(no_multiple_values)]
190    Values(#[css(if_empty = "none", iterable)] crate::OwnedSlice<L>),
191    /// `context-value`
192    ContextValue,
193}
194
195pub use self::GenericSVGStrokeDashArray as SVGStrokeDashArray;
196
197/// An SVG opacity value accepts `context-{fill,stroke}-opacity` in
198/// addition to opacity value.
199#[derive(
200    Animate,
201    Clone,
202    ComputeSquaredDistance,
203    Copy,
204    Debug,
205    MallocSizeOf,
206    PartialEq,
207    Parse,
208    SpecifiedValueInfo,
209    ToAnimatedValue,
210    ToAnimatedZero,
211    ToComputedValue,
212    ToCss,
213    ToResolvedValue,
214    ToShmem,
215    ToTyped,
216)]
217#[repr(C, u8)]
218pub enum GenericSVGOpacity<OpacityType> {
219    /// `<opacity-value>`
220    Opacity(OpacityType),
221    /// `context-fill-opacity`
222    #[animation(error)]
223    ContextFillOpacity,
224    /// `context-stroke-opacity`
225    #[animation(error)]
226    ContextStrokeOpacity,
227}
228
229pub use self::GenericSVGOpacity as SVGOpacity;