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