rive_rs/
renderer.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::fmt;
6
7use crate::math::{self, Mat};
8use crate::shapes::paint::{BlendMode, Color32, StrokeCap, StrokeJoin};
9use crate::shapes::{CommandPath, FillRule};
10
11#[derive(Clone, Debug, Default)]
12pub struct RenderPaint {
13    pub fill_rule: FillRule,
14    pub is_clipped: bool,
15    pub color: PaintColor,
16    pub style: Style,
17    pub blend_mode: BlendMode,
18}
19
20#[derive(Clone, Debug)]
21pub enum PaintColor {
22    Solid(Color32),
23    Gradient(Gradient),
24}
25
26impl Default for PaintColor {
27    fn default() -> Self {
28        Self::Solid(Color32::default())
29    }
30}
31
32#[derive(Clone, Copy, Debug)]
33pub enum Style {
34    Fill,
35    Stroke(StrokeStyle),
36}
37
38#[derive(Clone, Copy, Debug)]
39pub struct StrokeStyle {
40    pub thickness: f32,
41    pub cap: StrokeCap,
42    pub join: StrokeJoin,
43}
44
45impl Default for Style {
46    fn default() -> Self {
47        Self::Fill
48    }
49}
50
51#[derive(Clone, Debug)]
52pub struct Gradient {
53    pub r#type: GradientType,
54    pub start: math::Vec,
55    pub end: math::Vec,
56    pub stops: Vec<(Color32, f32)>,
57}
58
59#[derive(Debug)]
60pub struct GradientBuilder {
61    gradient: Gradient,
62}
63
64impl GradientBuilder {
65    pub fn new(r#type: GradientType) -> Self {
66        Self {
67            gradient: Gradient {
68                r#type,
69                start: math::Vec::default(),
70                end: math::Vec::default(),
71                stops: Vec::new(),
72            },
73        }
74    }
75
76    pub fn start(&mut self, start: math::Vec) -> &mut Self {
77        self.gradient.start = start;
78        self
79    }
80
81    pub fn end(&mut self, end: math::Vec) -> &mut Self {
82        self.gradient.end = end;
83        self
84    }
85
86    pub fn push_stop(&mut self, color: Color32, position: f32) -> &mut Self {
87        self.gradient.stops.push((color, position));
88        self
89    }
90
91    pub fn build(self) -> Gradient {
92        self.gradient
93    }
94}
95
96#[derive(Clone, Copy, Debug)]
97pub enum GradientType {
98    Linear,
99    Radial,
100}
101
102pub trait Renderer: fmt::Debug {
103    fn draw(&mut self, path: &CommandPath, transform: Mat, paint: &RenderPaint);
104    fn clip(&mut self, path: &CommandPath, transform: Mat, layers: usize);
105}