Skip to main content

plotine_render/
primitives.rs

1use plotine_core::{Color, Point};
2
3#[derive(Debug, Clone, PartialEq)]
4pub struct StrokeStyle {
5    pub color: Color,
6    pub width: f64,
7    pub cap: LineCap,
8    pub join: LineJoin,
9    /// Dash pattern in pixels: alternating on/off lengths (even length ≥ 2).
10    /// `None` means a solid stroke.
11    pub dash: Option<Vec<f64>>,
12}
13
14impl StrokeStyle {
15    pub fn new(color: Color, width: f64) -> Self {
16        Self {
17            color,
18            width,
19            cap: LineCap::Butt,
20            join: LineJoin::Miter,
21            dash: None,
22        }
23    }
24
25    /// Attach a dash pattern (pixel units). Empty / odd-length patterns are ignored.
26    pub fn with_dash(mut self, pattern: impl Into<Vec<f64>>) -> Self {
27        let p: Vec<f64> = pattern.into();
28        if p.len() >= 2 && p.len() % 2 == 0 && p.iter().all(|v| v.is_finite() && *v >= 0.0) {
29            self.dash = Some(p);
30        }
31        self
32    }
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum LineCap {
37    Butt,
38    Round,
39    Square,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum LineJoin {
44    Miter,
45    Round,
46    Bevel,
47}
48
49#[derive(Debug, Clone, Copy, PartialEq)]
50pub struct FillStyle {
51    pub color: Color,
52    /// When false, raster backends fill without anti-aliasing (mesh cells).
53    pub anti_alias: bool,
54}
55
56impl FillStyle {
57    pub fn solid(color: Color) -> Self {
58        Self {
59            color,
60            anti_alias: true,
61        }
62    }
63
64    /// Opaque mesh fill: no AA, so adjacent cells do not leak the face color.
65    pub fn solid_crisp(color: Color) -> Self {
66        Self {
67            color,
68            anti_alias: false,
69        }
70    }
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74pub enum TextAlign {
75    Left,
76    Center,
77    Right,
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub enum TextBaseline {
82    Top,
83    Middle,
84    Alphabetic,
85    Bottom,
86}
87
88#[derive(Debug, Clone, PartialEq)]
89pub struct TextStyle {
90    pub color: Color,
91    pub size_px: f32,
92    pub align: TextAlign,
93    pub baseline: TextBaseline,
94    /// Clockwise degrees in screen space (y-down). Use `-90.0` for classic y-axis labels.
95    pub rotation_deg: f64,
96    /// Heavier stem coverage (contour clabels). Default body text stays regular.
97    pub bold: bool,
98    /// Oblique / italic face (mathtext variables; matplotlib math default).
99    pub italic: bool,
100}
101
102impl TextStyle {
103    pub fn new(color: Color, size_px: f32) -> Self {
104        Self {
105            color,
106            size_px,
107            align: TextAlign::Left,
108            baseline: TextBaseline::Alphabetic,
109            rotation_deg: 0.0,
110            bold: false,
111            italic: false,
112        }
113    }
114
115    pub fn centered(mut self) -> Self {
116        self.align = TextAlign::Center;
117        self
118    }
119
120    pub fn align(mut self, align: TextAlign) -> Self {
121        self.align = align;
122        self
123    }
124
125    pub fn baseline(mut self, baseline: TextBaseline) -> Self {
126        self.baseline = baseline;
127        self
128    }
129
130    pub fn rotation(mut self, degrees: f64) -> Self {
131        self.rotation_deg = degrees;
132        self
133    }
134
135    pub fn bold(mut self, bold: bool) -> Self {
136        self.bold = bold;
137        self
138    }
139
140    pub fn italic(mut self, italic: bool) -> Self {
141        self.italic = italic;
142        self
143    }
144}
145
146/// Convenience polyline builder from points.
147pub fn polyline_from_points(points: &[Point]) -> kurbo::BezPath {
148    let mut path = kurbo::BezPath::new();
149    let mut iter = points.iter();
150    if let Some(first) = iter.next() {
151        path.move_to(first.to_kurbo());
152        for p in iter {
153            path.line_to(p.to_kurbo());
154        }
155    }
156    path
157}