1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//! Animation attribute types as specified in [animations](https://svgwg.org/specs/animations/)
#[cfg(feature = "parse")]
use oxvg_parse::{error::Error, Parse, Parser};
#[cfg(feature = "serialize")]
use oxvg_serialize::{error::PrinterError, Printer, ToValue};
use crate::{atom::Atom, enum_attr};
use super::{
animation_timing::ClockValue,
core::{Integer, Number},
};
enum_attr!(
/// Specifies the namespace in which the target attribute and its associated values are defined.
/// [w3 | SVG 1.1](https://www.w3.org/TR/2011/REC-SVG11-20110816/animate.html#AttributeTypeAttribute)
AttributeType {
/// This specifies that the value of ‘attributeName’ is the name of a CSS property.
CSS: "CSS",
/// This specifies that the value of ‘attributeName’ is the name of an XML attribute.
XML: "XML",
/// he implementation should match the ‘attributeName’ to an attribute for the target element.
Auto: "auto",
}
);
#[derive(Clone, Debug, PartialEq)]
/// Defines when the element should begin
/// [w3](https://svgwg.org/specs/animations/#BeginValueListSyntax)
pub enum BeginEnd<'i> {
/// (Clock-value)
OffsetValue(ClockValue),
/// (Id-value "." ( "begin" | "end" )) (Clock-value)?
SyncbaseValue {
/// An ID reference to another element that has animations to sync with
id: Atom<'i>,
/// Whether the animation should sync with the beginning or end of the referenced element
begin: bool,
/// The clock time to delay the synced animation by
offset: Option<ClockValue>,
},
/// (Id-value ".")? (Event-ref) (Clock-value)?
EventValue {
/// An ID reference to another element that has events to sync with
id: Option<Atom<'i>>,
// TODO: Event ID
/// The event name to sync animations with
event: Atom<'i>,
/// The clock time to delay the synced animation by
offset: Option<ClockValue>,
},
/// (Id-value ".")? "repeat(<integer>)" (Clock-value)?
RepeatValue {
/// An ID reference to another element
id: Option<Atom<'i>>,
/// The number of repetitions
repeat: Integer,
/// The clock time to delay the synced animation by
offset: Option<ClockValue>,
},
/// "accessKey(<character>)" (Clock-value)?
AccessKeyValue {
/// The key name that will begin the animation when pressed by the user
character: Atom<'i>,
/// The clock time to delay the synced animation by
offset: Option<ClockValue>,
},
/// "wallclock(<wallclock-value>)"
WallclockSyncValue(Atom<'i>),
/// "indefinite"
Indefinite,
}
#[cfg(feature = "parse")]
impl<'input> Parse<'input> for BeginEnd<'input> {
fn parse<'t>(input: &mut Parser<'input>) -> Result<Self, Error<'input>> {
input
.try_parse(|input| {
input
.expect_ident_matching("indefinite")
.map(|()| Self::Indefinite)
})
.or_else(|_| input.try_parse(ClockValue::parse).map(Self::OffsetValue))
.or_else(|_| {
input.try_parse(|input| {
input.expect_ident_matching("accessKey")?;
input.expect_char('(')?;
let character = input.take_matches(|char| char != ')').into();
input.expect_char(')')?;
let offset = input.try_parse(ClockValue::parse).ok();
Ok(Self::AccessKeyValue { character, offset })
})
})
.or_else(|_: Error<'input>| {
input.try_parse(|input| {
input.expect_ident_matching("wallclock")?;
input.expect_char('(')?;
let wallclock_value = input.take_matches(|char| char != ')').into();
input.expect_char(')')?;
Ok(Self::WallclockSyncValue(wallclock_value))
})
})
.or_else(|_: Error<'input>| {
let id = input.expect_ident()?;
let id = match id.rfind(['-', '+']) {
Some(n) => {
input.rewind(id.len() - n);
&id[..n]
}
None => id,
};
let (id, event) = match id.rsplit_once('.') {
Some((id, event)) => (Some(id), event),
None => (None, id),
};
if event == "repeat" {
input.expect_char('(')?;
let repeat = i32::parse(input)?;
input.expect_char(')')?;
let offset = ClockValue::parse(input).ok();
return Ok(Self::RepeatValue {
id: id.map(Into::into),
repeat,
offset,
});
}
input.skip_whitespace();
let offset = input.try_parse(ClockValue::parse).ok();
if let (Some(id), "begin" | "end") = (id, event) {
Ok(Self::SyncbaseValue {
id: id.into(),
begin: event == "begin",
offset,
})
} else {
Ok(Self::EventValue {
id: id.map(Into::into),
event: event.into(),
offset,
})
}
})
}
}
#[cfg(feature = "serialize")]
impl ToValue for BeginEnd<'_> {
fn write_value<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match self {
Self::OffsetValue(clock_value) => clock_value.write_value(dest),
Self::SyncbaseValue { id, begin, offset } => {
dest.write_str(id)?;
dest.write_char('.')?;
if *begin {
dest.write_str("begin")?;
} else {
dest.write_str("end")?;
}
if let Some(clock_value) = offset {
if !clock_value.is_negative() {
dest.write_char('+')?;
}
clock_value.write_value(dest)?;
}
Ok(())
}
Self::EventValue { id, event, offset } => {
if let Some(id) = id {
dest.write_str(id)?;
dest.write_char('.')?;
}
dest.write_str(event)?;
if let Some(clock_value) = offset {
if !clock_value.is_negative() {
dest.write_char('+')?;
}
clock_value.write_value(dest)?;
}
Ok(())
}
Self::RepeatValue { id, repeat, offset } => {
if let Some(id) = id {
dest.write_str(id)?;
dest.write_char('.')?;
}
dest.write_str("repeat(")?;
repeat.write_value(dest)?;
dest.write_char(')')?;
if let Some(clock_value) = offset {
if !clock_value.is_negative() {
dest.write_char('+')?;
}
clock_value.write_value(dest)?;
}
Ok(())
}
Self::AccessKeyValue { character, offset } => {
dest.write_str("accessKey(")?;
dest.write_str(character)?;
dest.write_char(')')?;
if let Some(clock_value) = offset {
if !clock_value.is_negative() {
dest.write_char('+')?;
}
clock_value.write_value(dest)?;
}
Ok(())
}
Self::WallclockSyncValue(wallclock_value) => {
dest.write_str("wallclock(")?;
dest.write_str(wallclock_value)?;
dest.write_char(')')
}
Self::Indefinite => dest.write_str("indefinite"),
}
}
}
#[test]
fn begin_end() {
use crate::attribute::animation_timing::Metric;
let clock_value = ClockValue::TimecountValue {
timecount: -15.0,
metric: Metric::Second,
};
assert_eq!(
BeginEnd::parse_string("0"),
Ok(BeginEnd::OffsetValue(ClockValue::TimecountValue {
timecount: 0.0,
metric: Metric::Second
}))
);
assert_eq!(
BeginEnd::parse_string("-15s"),
Ok(BeginEnd::OffsetValue(clock_value.clone()))
);
assert_eq!(
BeginEnd::parse_string("id.begin -15s"),
Ok(BeginEnd::SyncbaseValue {
id: "id".into(),
begin: true,
offset: Some(clock_value.clone())
})
);
assert_eq!(
BeginEnd::parse_string("id.end"),
Ok(BeginEnd::SyncbaseValue {
id: "id".into(),
begin: false,
offset: None
})
);
assert_eq!(
BeginEnd::parse_string("id2.end"),
Ok(BeginEnd::SyncbaseValue {
id: "id2".into(),
begin: false,
offset: None
})
);
assert_eq!(
BeginEnd::parse_string("onclick-15s"),
Ok(BeginEnd::EventValue {
id: None,
event: "onclick".into(),
offset: Some(clock_value.clone())
})
);
assert_eq!(
BeginEnd::parse_string("id.onclick"),
Ok(BeginEnd::EventValue {
id: Some("id".into()),
event: "onclick".into(),
offset: None
})
);
assert_eq!(
BeginEnd::parse_string("repeat(1) -15s"),
Ok(BeginEnd::RepeatValue {
id: None,
repeat: 1,
offset: Some(clock_value.clone())
})
);
assert_eq!(
BeginEnd::parse_string("id.repeat(0)"),
Ok(BeginEnd::RepeatValue {
id: Some("id".into()),
repeat: 0,
offset: None
})
);
assert_eq!(
BeginEnd::parse_string("accessKey(s)-15s"),
Ok(BeginEnd::AccessKeyValue {
character: "s".into(),
offset: Some(clock_value.clone())
})
);
assert_eq!(
BeginEnd::parse_string("wallclock(01/01/1960)"),
Ok(BeginEnd::WallclockSyncValue("01/01/1960".into()))
);
assert_eq!(
BeginEnd::parse_string("indefinite"),
Ok(BeginEnd::Indefinite)
);
assert_eq!(BeginEnd::parse_string("0;"), Err(Error::ExpectedDone));
}
enum_attr!(
/// Specifies the interpolation mode for the animation.
/// [w3](https://svgwg.org/specs/animations/#CalcModeAttribute)
CalcMode {
/// This specifies that the animation function will jump from one value to the next without any interpolation.
Discrete: "discrete",
/// Simple linear interpolation between values is used to calculate the animation function.
Linear: "linear",
/// Defines interpolation to produce an even pace of change across the animation.
Paced: "paced",
/// Interpolates from one value in the 'values' list to the next according to a time function defined by a cubic Bézier spline.
Spline: "spline",
}
);
#[derive(Clone, Debug, PartialEq)]
/// A set of Bézier control points associated with the ‘keyTimes’ list
/// [w3](https://svgwg.org/specs/animations/#KeySplinesAttribute)
pub struct ControlPoint(pub [Number; 4]);
#[cfg(feature = "parse")]
impl<'input> Parse<'input> for ControlPoint {
fn parse<'t>(input: &mut Parser<'input>) -> Result<Self, Error<'input>> {
input.skip_whitespace();
let x1 = Number::parse(input)?;
input.skip_whitespace();
input.skip_char(',');
input.skip_whitespace();
let y1 = Number::parse(input)?;
input.skip_whitespace();
input.skip_char(',');
input.skip_whitespace();
let x2 = Number::parse(input)?;
input.skip_whitespace();
input.skip_char(',');
input.skip_whitespace();
let y2 = Number::parse(input)?;
Ok(Self([x1, y1, x2, y2]))
}
}
#[cfg(feature = "serialize")]
impl ToValue for ControlPoint {
fn write_value<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
let Self([x1, y1, x2, y2]) = self;
x1.write_value(dest)?;
dest.write_char(' ')?;
y1.write_value(dest)?;
dest.write_char(' ')?;
x2.write_value(dest)?;
dest.write_char(' ')?;
y2.write_value(dest)
}
}