rlvgl_core/plugins/
lottie.rs

1//! Dynamic Lottie rendering utilities.
2//!
3//! This module exposes helpers built on the `rlottie` crate for loading a
4//! Lottie JSON string and rendering frames into [`Color`] buffers.
5
6use crate::widget::Color;
7use rlottie::{Animation, Size, Surface};
8
9/// Errors that can occur when rendering a Lottie animation frame.
10#[derive(Debug, Clone)]
11pub enum Error {
12    /// The provided Lottie JSON data was invalid.
13    InvalidJson,
14}
15
16/// Render a single frame of a Lottie JSON animation.
17///
18/// * `json` - Lottie document as UTF-8 text.
19/// * `frame` - Zero-based frame index to render.
20/// * `width`/`height` - Output dimensions in pixels.
21///
22/// Returns `None` if the JSON data is invalid.
23pub fn render_lottie_frame(
24    json: &str,
25    frame: usize,
26    width: usize,
27    height: usize,
28) -> Option<alloc::vec::Vec<Color>> {
29    let mut anim = Animation::from_data(json, "mem", ".")?;
30    let mut surface = Surface::new(Size::new(width, height));
31    anim.render(frame, &mut surface);
32    Some(
33        surface
34            .data()
35            .iter()
36            .map(|px| Color(px.r, px.g, px.b, 255))
37            .collect(),
38    )
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    const SIMPLE_JSON: &str =
46        "{\"v\":\"5.7\",\"fr\":30,\"ip\":0,\"op\":0,\"w\":1,\"h\":1,\"layers\":[]}";
47
48    #[test]
49    fn render_minimal() {
50        let frame = render_lottie_frame(SIMPLE_JSON, 0, 1, 1).unwrap();
51        assert_eq!(frame.len(), 1);
52    }
53}