rive_rs/
renderer.rs

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
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::fmt;

use crate::math::{self, Mat};
use crate::shapes::paint::{BlendMode, Color32, StrokeCap, StrokeJoin};
use crate::shapes::{CommandPath, FillRule};

#[derive(Clone, Debug, Default)]
pub struct RenderPaint {
    pub fill_rule: FillRule,
    pub is_clipped: bool,
    pub color: PaintColor,
    pub style: Style,
    pub blend_mode: BlendMode,
}

#[derive(Clone, Debug)]
pub enum PaintColor {
    Solid(Color32),
    Gradient(Gradient),
}

impl Default for PaintColor {
    fn default() -> Self {
        Self::Solid(Color32::default())
    }
}

#[derive(Clone, Copy, Debug)]
pub enum Style {
    Fill,
    Stroke(StrokeStyle),
}

#[derive(Clone, Copy, Debug)]
pub struct StrokeStyle {
    pub thickness: f32,
    pub cap: StrokeCap,
    pub join: StrokeJoin,
}

impl Default for Style {
    fn default() -> Self {
        Self::Fill
    }
}

#[derive(Clone, Debug)]
pub struct Gradient {
    pub r#type: GradientType,
    pub start: math::Vec,
    pub end: math::Vec,
    pub stops: Vec<(Color32, f32)>,
}

#[derive(Debug)]
pub struct GradientBuilder {
    gradient: Gradient,
}

impl GradientBuilder {
    pub fn new(r#type: GradientType) -> Self {
        Self {
            gradient: Gradient {
                r#type,
                start: math::Vec::default(),
                end: math::Vec::default(),
                stops: Vec::new(),
            },
        }
    }

    pub fn start(&mut self, start: math::Vec) -> &mut Self {
        self.gradient.start = start;
        self
    }

    pub fn end(&mut self, end: math::Vec) -> &mut Self {
        self.gradient.end = end;
        self
    }

    pub fn push_stop(&mut self, color: Color32, position: f32) -> &mut Self {
        self.gradient.stops.push((color, position));
        self
    }

    pub fn build(self) -> Gradient {
        self.gradient
    }
}

#[derive(Clone, Copy, Debug)]
pub enum GradientType {
    Linear,
    Radial,
}

pub trait Renderer: fmt::Debug {
    fn draw(&mut self, path: &CommandPath, transform: Mat, paint: &RenderPaint);
    fn clip(&mut self, path: &CommandPath, transform: Mat, layers: usize);
}