Skip to main content

plotine_render/
renderer.rs

1use kurbo::BezPath;
2use plotine_core::{Point, Rect, Result};
3
4use crate::primitives::{FillStyle, StrokeStyle, TextStyle};
5
6/// Backend-agnostic drawing surface.
7///
8/// Implement this trait to add a new output format. The built-in backends are:
9/// - `plotine-backend-skia::SkiaRenderer` — raster PNG via tiny-skia
10/// - `plotine-backend-svg::SvgRenderer` — deterministic SVG
11///
12/// Recipes produce [`kurbo::BezPath`] geometry; backends only need to fill/stroke
13/// paths, draw text, and manage a clip stack.
14pub trait Renderer {
15    fn clear(&mut self, color: plotine_core::Color) -> Result<()>;
16
17    fn fill_rect(&mut self, rect: Rect, style: &FillStyle) -> Result<()>;
18
19    fn stroke_rect(&mut self, rect: Rect, style: &StrokeStyle) -> Result<()>;
20
21    fn fill_path(&mut self, path: &BezPath, style: &FillStyle) -> Result<()>;
22
23    fn stroke_path(&mut self, path: &BezPath, style: &StrokeStyle) -> Result<()>;
24
25    fn draw_line(&mut self, p0: Point, p1: Point, style: &StrokeStyle) -> Result<()> {
26        let mut path = BezPath::new();
27        path.move_to(p0.to_kurbo());
28        path.line_to(p1.to_kurbo());
29        self.stroke_path(&path, style)
30    }
31
32    fn draw_text(&mut self, text: &str, position: Point, style: &TextStyle) -> Result<()>;
33
34    /// Blit an RGBA8 image with top-left at `position` (figure pixels).
35    ///
36    /// `rgba.len()` must be `width * height * 4`. Used by optional external LaTeX
37    /// (`feature = "latex"`) and any future raster overlays.
38    fn draw_rgba_image(
39        &mut self,
40        rgba: &[u8],
41        width: u32,
42        height: u32,
43        position: Point,
44    ) -> Result<()>;
45
46    /// Measure text width/height in pixels for layout (upright face).
47    fn measure_text(&self, text: &str, size_px: f32) -> Result<(f64, f64)> {
48        self.measure_text_styled(text, size_px, false)
49    }
50
51    /// Measure text with optional italic/oblique face (mathtext variables).
52    fn measure_text_styled(&self, text: &str, size_px: f32, italic: bool) -> Result<(f64, f64)>;
53
54    /// Restrict subsequent drawing to `rect` until [`pop_clip`](Self::pop_clip).
55    fn push_clip_rect(&mut self, rect: Rect) -> Result<()>;
56
57    fn pop_clip(&mut self) -> Result<()>;
58
59    fn width(&self) -> u32;
60    fn height(&self) -> u32;
61}