Skip to main content

plotkit_core/
renderer.rs

1//! The renderer trait — the single seam between chart logic and output backends.
2
3use crate::primitives::{Affine, Image, Paint, Path, Point, Rect, Stroke, TextStyle};
4
5/// A rendering backend that can produce raster or vector output.
6///
7/// Implementations translate plotkit's core primitive types into backend-specific
8/// draw calls. PNG, SVG, PDF, and WASM are all implementations of this trait.
9///
10/// # Contract
11///
12/// - All coordinates and transforms use plotkit's coordinate system (origin top-left, y-down).
13/// - The renderer must handle the full `Paint`, `Stroke`, and `TextStyle` types faithfully.
14/// - `finalize` consumes the renderer and returns the encoded output bytes.
15pub trait Renderer {
16    /// Returns the output dimensions in pixels.
17    fn size(&self) -> (u32, u32);
18
19    /// Fills a path with the given paint, under the given transform.
20    fn fill_path(&mut self, path: &Path, paint: &Paint, transform: Affine);
21
22    /// Strokes a path with the given paint and stroke style, under the given transform.
23    fn stroke_path(&mut self, path: &Path, paint: &Paint, stroke: &Stroke, transform: Affine);
24
25    /// Draws text at the given position with the given style, under the given transform.
26    fn draw_text(&mut self, text: &str, pos: Point, style: &TextStyle, transform: Affine);
27
28    /// Draws a raster image into the destination rectangle, under the given transform.
29    fn draw_image(&mut self, img: &Image, dst: Rect, transform: Affine);
30
31    /// Pushes a clipping path. All subsequent draws are clipped to this path.
32    fn push_clip(&mut self, path: &Path, transform: Affine);
33
34    /// Pops the most recent clipping path.
35    fn pop_clip(&mut self);
36
37    /// Measures the bounding box of the given text with the given style.
38    /// Returns the width and height in pixels.
39    fn measure_text(&self, text: &str, style: &TextStyle) -> (f64, f64);
40
41    /// Consumes the renderer and returns the encoded output (PNG bytes, SVG string as bytes, etc.).
42    fn finalize(self) -> Vec<u8>;
43}