crisol-plot-render 1.13.0

Headless SVG/PNG renderer for Crisol PlotSpec charts, with no window or GPU.
Documentation
//! Renderer-independent primitives: what a chart draws, before it becomes PNG or SVG.

use crisol_core::color::Rgba;
use crisol_plot_core::transform::{ScreenPoint, ScreenRect};

/// Horizontal text anchor.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Anchor {
    /// Left edge at the position.
    Left,
    /// Center at the position.
    Center,
    /// Right edge at the position.
    Right,
}

/// One primitive of a rendered chart.
#[derive(Clone, Debug, PartialEq)]
pub enum Prim {
    /// A filled or stroked rectangle.
    Rect {
        /// Bounds.
        rect: ScreenRect,
        /// Fill.
        fill: Rgba,
        /// Optional stroke and width.
        stroke: Option<(f32, Rgba)>,
    },
    /// A stroked polyline.
    Line {
        /// Vertices.
        points: Vec<ScreenPoint>,
        /// Width in pixels.
        width: f32,
        /// Color.
        color: Rgba,
    },
    /// A filled or stroked polygon.
    Polygon {
        /// Vertices.
        points: Vec<ScreenPoint>,
        /// Fill.
        fill: Rgba,
        /// Optional stroke and width.
        stroke: Option<(f32, Rgba)>,
    },
    /// A circle.
    Circle {
        /// Center.
        center: ScreenPoint,
        /// Radius in pixels.
        radius: f32,
        /// Fill.
        fill: Rgba,
        /// Optional stroke and width.
        stroke: Option<(f32, Rgba)>,
    },
    /// One line of text; `pos` is the top-left of the line box.
    Text {
        /// Anchor point.
        pos: ScreenPoint,
        /// Text.
        text: String,
        /// Font size in pixels.
        size: f32,
        /// Color.
        color: Rgba,
        /// Horizontal anchor.
        anchor: Anchor,
    },
}

impl Prim {
    /// A horizontal line.
    pub fn hline(from: f32, to: f32, y: f32, width: f32, color: Rgba) -> Self {
        Self::Line {
            points: vec![ScreenPoint::new(from, y), ScreenPoint::new(to, y)],
            width,
            color,
        }
    }

    /// A vertical line.
    pub fn vline(x: f32, from: f32, to: f32, width: f32, color: Rgba) -> Self {
        Self::Line {
            points: vec![ScreenPoint::new(x, from), ScreenPoint::new(x, to)],
            width,
            color,
        }
    }
}