crisol-plot-render 1.9.0

Headless SVG/PNG renderer for Crisol PlotSpec charts, with no window or GPU.
Documentation
//! The bundled font and its metrics.

use crate::RenderError;
use ab_glyph::{Font, FontRef, PxScale, ScaleFont};

const FONT_BYTES: &[u8] = include_bytes!("../assets/DejaVuSans.ttf");

/// The bundled font bytes, for embedding.
pub(crate) fn font_bytes() -> &'static [u8] {
    FONT_BYTES
}

/// The bundled DejaVu Sans face.
pub(crate) fn font() -> Result<FontRef<'static>, RenderError> {
    FontRef::try_from_slice(FONT_BYTES).map_err(|error| RenderError::Font(error.to_string()))
}

/// The width of one line at `size`, in pixels.
pub(crate) fn width(font: &FontRef<'_>, text: &str, size: f32) -> f32 {
    let scaled = font.as_scaled(PxScale::from(size));
    text.chars()
        .map(|character| scaled.h_advance(font.glyph_id(character)))
        .sum()
}

/// The ascent of the bundled font at `size`, in pixels.
pub(crate) fn ascent(font: &FontRef<'_>, size: f32) -> f32 {
    font.as_scaled(PxScale::from(size)).ascent()
}