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
368
369
370
371
//! Sources the engine draws itself: fills, gradients, shapes, and text.
use crate::{
ClipEffect, GeneratorIssue, LayoutNode, MAX_TEXT_CONTENT_BYTES, MAX_TEXT_GENERATOR_FONT_SIZE,
ShapeItem, TextStyle, Vec2,
};
use serde::{Deserialize, Serialize};
/// Most stops in one gradient.
pub const MAX_GRADIENT_STOPS: usize = 16;
/// A straight (not premultiplied), sRGB-encoded color with alpha, each component in `0..=1`.
///
/// Read from the object or CSS hex (`"#rgb"`, `"#rgba"`, `"#rrggbb"`, `"#rrggbbaa"`, any
/// case). Always written back as the object, since components need not be multiples of 1/255.
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(
feature = "schema",
schemars(rename = "Rgba", transform = crate::schema::with_hex_form)
)]
#[serde(remote = "Self", deny_unknown_fields)]
pub struct Rgba {
/// Red component.
pub red: f32,
/// Green component.
pub green: f32,
/// Blue component.
pub blue: f32,
/// Coverage.
pub alpha: f32,
}
impl Rgba {
/// Creates a color from sRGB-encoded components and alpha.
#[must_use]
pub const fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self {
red,
green,
blue,
alpha,
}
}
pub(crate) fn is_valid(self) -> bool {
[self.red, self.green, self.blue, self.alpha]
.iter()
.all(|value| (0.0..=1.0).contains(value))
}
/// The color a CSS hex string names, if it is one.
#[must_use]
pub fn from_hex(text: &str) -> Option<Self> {
let digits = text.strip_prefix('#')?.as_bytes();
let nibble = |index: usize| char::from(digits[index]).to_digit(16);
let component = |value: u32| u8::try_from(value).ok().map(|byte| f32::from(byte) / 255.0);
let channels = match digits.len() {
3 | 4 => (0..digits.len())
.map(|index| component(nibble(index)? * 17))
.collect::<Option<Vec<_>>>()?,
6 | 8 => (0..digits.len() / 2)
.map(|index| component(nibble(index * 2)? * 16 + nibble(index * 2 + 1)?))
.collect::<Option<Vec<_>>>()?,
_ => return None,
};
Some(Self::new(
channels[0],
channels[1],
channels[2],
channels.get(3).copied().unwrap_or(1.0),
))
}
}
/// Reads a hex string or the object.
struct RgbaVisitor;
impl<'de> serde::de::Visitor<'de> for RgbaVisitor {
type Value = Rgba;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("a color: the {red, green, blue, alpha} object or a CSS hex string")
}
fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Rgba, E> {
Rgba::from_hex(value).ok_or_else(|| {
E::custom(format!(
"invalid color \"{value}\": write #rgb, #rgba, #rrggbb, or #rrggbbaa"
))
})
}
fn visit_map<A: serde::de::MapAccess<'de>>(self, map: A) -> Result<Rgba, A::Error> {
Rgba::deserialize(serde::de::value::MapAccessDeserializer::new(map))
}
}
impl<'de> Deserialize<'de> for Rgba {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(RgbaVisitor)
}
}
impl Serialize for Rgba {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Self::serialize(self, serializer)
}
}
/// The picture of a generated source.
///
/// A generated picture has the canvas's size and aspect. Positions are normalized, `(0, 0)`
/// top-left to `(1, 1)` bottom-right, and lengths are fractions of the shorter side, so the
/// picture is the same at any render resolution. A fill never changes over time; text
/// changes only through its style's animators; shapes, layouts and effects may change.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
pub enum Generator {
/// Paint over the whole picture.
Fill {
/// What covers the picture.
paint: Paint,
},
/// Shapes drawn from an ordered content tree, as an After Effects or Lottie shape layer draws
/// them. Every value in the tree may be keyframed in picture time.
Shapes {
/// The content tree, front first. See [`ShapeItem`].
#[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_SHAPE_ITEMS }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_SHAPE_ITEMS)))]
items: Vec<ShapeItem>,
},
/// Text on a transparent picture, laid out and placed by its style as caption cues are, and
/// drawn with the host's registered fonts.
Text {
/// The text, a string or runs; `\n` starts a line. Non-empty, at most
/// [`MAX_TEXT_CONTENT_BYTES`] bytes. Run times are picture time.
text: crate::TextContent,
/// How it looks and where it lies. The font size is at most
/// [`MAX_TEXT_GENERATOR_FONT_SIZE`].
#[serde(default)]
style: Box<TextStyle>,
},
/// Boxes laid out by CSS flexbox around their text and shapes, with the engine's fonts. Any
/// value in the tree may be keyframed in picture time. See [`LayoutNode`].
Layout {
/// The root box: the whole picture.
root: Box<LayoutNode>,
},
/// A generator effect drawn over the whole picture: a built-in generator of
/// `schema/effects.v1.json` or one the host registers.
Effect {
/// The versioned generator and its parameters, which may be keyframed in picture time
/// and take no data.
effect: ClipEffect,
},
}
/// A color or gradient.
///
/// Gradients interpolate premultiplied stops in linear light, so a gradient between two colors
/// passes through their physical mix and a stop fading to transparent does not darken its
/// neighbour. A bare color is read as a solid paint; paints are written back tagged.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(
feature = "schema",
schemars(rename = "Paint", transform = crate::schema::with_color_form)
)]
#[serde(
remote = "Self",
tag = "type",
rename_all = "snake_case",
deny_unknown_fields
)]
pub enum Paint {
/// One color.
Solid {
/// The color.
color: Rgba,
},
/// Colors along the line from `start` to `end`, constant across it.
LinearGradient {
/// Where offset 0 lies.
start: Vec2,
/// Where offset 1 lies. Distinct from `start`.
end: Vec2,
/// Two to [`MAX_GRADIENT_STOPS`] stops.
#[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_GRADIENT_STOPS }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_GRADIENT_STOPS)))]
stops: Vec<GradientStop>,
/// What lies beyond the first and last offsets.
#[serde(default)]
spread: Spread,
},
/// Colors along the radius of a circle.
RadialGradient {
/// Where offset 0 lies.
center: Vec2,
/// The distance of offset 1 from the centre, as a fraction of the picture's shorter side.
/// Positive.
radius: f32,
/// Two to [`MAX_GRADIENT_STOPS`] stops.
#[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_GRADIENT_STOPS }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_GRADIENT_STOPS)))]
stops: Vec<GradientStop>,
/// What lies beyond the first and last offsets.
#[serde(default)]
spread: Spread,
},
/// Colors around a point: offsets 0 to 1 are one clockwise turn.
ConicGradient {
/// The point the gradient turns around.
center: Vec2,
/// Where offset 0 lies, in degrees clockwise from straight up.
#[serde(default)]
angle_degrees: f32,
/// Two to [`MAX_GRADIENT_STOPS`] stops.
#[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_GRADIENT_STOPS }>")]
#[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_GRADIENT_STOPS)))]
stops: Vec<GradientStop>,
},
}
impl<'de> Deserialize<'de> for Paint {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use serde::de::Error as _;
// Only the presence of a tag tells the two forms apart.
let value = serde_json::Value::deserialize(deserializer)?;
if value.get("type").is_some() {
Self::deserialize(value).map_err(D::Error::custom)
} else {
<Rgba as Deserialize>::deserialize(value)
.map(|color| Self::Solid { color })
.map_err(D::Error::custom)
}
}
}
impl Serialize for Paint {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Self::serialize(self, serializer)
}
}
/// One color of a gradient.
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct GradientStop {
/// Position along the gradient, in `0..=1`, not before the previous stop.
pub offset: f32,
/// Color at that position.
pub color: Rgba,
}
/// What a linear or radial gradient shows outside its first and last stops.
#[derive(Copy, Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum Spread {
/// The end colors continue.
#[default]
Pad,
/// The gradient starts again.
Repeat,
/// The gradient runs back and forth.
Reflect,
}
impl Generator {
/// Whether every field is in range, as a project needs it to be.
#[must_use]
pub fn is_valid(&self) -> bool {
self.validate().is_ok()
}
/// Checks every field. A shapes or layout generator's problem names where in its tree it
/// lies, and an effect under [`crate::BUILTIN_EFFECT_PREFIX`] is checked against its built-in
/// generator. A layout's pixel font sizes are checked only by [`Self::validate_on`], which
/// knows the canvas.
///
/// # Errors
/// Returns the first problem, if any.
pub fn validate(&self) -> Result<(), GeneratorIssue> {
self.validate_canvas(None)
}
/// [`Self::validate`] for a generator drawn on a `width` by `height` canvas, which a layout's
/// pixel lengths refer to.
///
/// # Errors
/// Returns the first problem, if any.
pub fn validate_on(&self, width: u32, height: u32) -> Result<(), GeneratorIssue> {
self.validate_canvas(Some((width, height)))
}
fn validate_canvas(&self, canvas: Option<(u32, u32)>) -> Result<(), GeneratorIssue> {
let valid = match self {
Self::Fill { paint } => paint.is_valid(),
Self::Shapes { items } => {
return crate::shapes::validate_shape_items(items).map_err(GeneratorIssue::Shape);
}
Self::Layout { root } => return crate::layout::validate_layout(root, canvas),
Self::Effect { effect } => {
if !effect.is_valid() {
return Err(GeneratorIssue::Invalid);
}
return crate::effect_spec::check_builtin(
effect,
crate::EffectKind::Generator,
None,
)
.map_err(|problem| GeneratorIssue::Effect {
id: effect.id.clone(),
problem,
});
}
Self::Text { text, style } => {
if !(text.is_valid(MAX_TEXT_CONTENT_BYTES)
&& style.is_valid(MAX_TEXT_GENERATOR_FONT_SIZE))
{
return Err(GeneratorIssue::Invalid);
}
return match text.invalid_run_style(crate::text_animation::FontSelection::of(style))
{
Some(run) => Err(GeneratorIssue::TextRun { run }),
None => Ok(()),
};
}
};
valid.then_some(()).ok_or(GeneratorIssue::Invalid)
}
}
impl Paint {
pub(crate) fn is_valid(&self) -> bool {
match self {
Self::Solid { color } => color.is_valid(),
Self::LinearGradient {
start, end, stops, ..
} => finite(*start) && finite(*end) && start != end && valid_stops(stops),
Self::RadialGradient {
center,
radius,
stops,
..
} => finite(*center) && radius.is_finite() && *radius > 0.0 && valid_stops(stops),
Self::ConicGradient {
center,
angle_degrees,
stops,
} => finite(*center) && angle_degrees.is_finite() && valid_stops(stops),
}
}
}
fn finite(value: Vec2) -> bool {
value.x.is_finite() && value.y.is_finite()
}
fn valid_stops(stops: &[GradientStop]) -> bool {
(2..=MAX_GRADIENT_STOPS).contains(&stops.len())
&& stops
.iter()
.all(|stop| (0.0..=1.0).contains(&stop.offset) && stop.color.is_valid())
&& stops
.windows(2)
.all(|pair| pair[0].offset <= pair[1].offset)
}