Skip to main content

crisol_plot_render/
scene.rs

1//! Renderer-independent primitives: what a chart draws, before it becomes PNG or SVG.
2
3use crisol_core::color::Rgba;
4use crisol_plot_core::transform::{ScreenPoint, ScreenRect};
5
6/// Horizontal text anchor.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum Anchor {
9    /// Left edge at the position.
10    Left,
11    /// Center at the position.
12    Center,
13    /// Right edge at the position.
14    Right,
15}
16
17/// One primitive of a rendered chart.
18#[derive(Clone, Debug, PartialEq)]
19pub enum Prim {
20    /// A filled or stroked rectangle.
21    Rect {
22        /// Bounds.
23        rect: ScreenRect,
24        /// Fill.
25        fill: Rgba,
26        /// Optional stroke and width.
27        stroke: Option<(f32, Rgba)>,
28    },
29    /// A stroked polyline.
30    Line {
31        /// Vertices.
32        points: Vec<ScreenPoint>,
33        /// Width in pixels.
34        width: f32,
35        /// Color.
36        color: Rgba,
37    },
38    /// A filled or stroked polygon.
39    Polygon {
40        /// Vertices.
41        points: Vec<ScreenPoint>,
42        /// Fill.
43        fill: Rgba,
44        /// Optional stroke and width.
45        stroke: Option<(f32, Rgba)>,
46    },
47    /// A circle.
48    Circle {
49        /// Center.
50        center: ScreenPoint,
51        /// Radius in pixels.
52        radius: f32,
53        /// Fill.
54        fill: Rgba,
55        /// Optional stroke and width.
56        stroke: Option<(f32, Rgba)>,
57    },
58    /// One line of text; `pos` is the top-left of the line box.
59    Text {
60        /// Anchor point.
61        pos: ScreenPoint,
62        /// Text.
63        text: String,
64        /// Font size in pixels.
65        size: f32,
66        /// Color.
67        color: Rgba,
68        /// Horizontal anchor.
69        anchor: Anchor,
70    },
71}
72
73impl Prim {
74    /// A horizontal line.
75    pub fn hline(from: f32, to: f32, y: f32, width: f32, color: Rgba) -> Self {
76        Self::Line {
77            points: vec![ScreenPoint::new(from, y), ScreenPoint::new(to, y)],
78            width,
79            color,
80        }
81    }
82
83    /// A vertical line.
84    pub fn vline(x: f32, from: f32, to: f32, width: f32, color: Rgba) -> Self {
85        Self::Line {
86            points: vec![ScreenPoint::new(x, from), ScreenPoint::new(x, to)],
87            width,
88            color,
89        }
90    }
91}