Skip to main content

document_graphics/
lib.rs

1#![doc = include_str!("../README.md")]
2
3pub mod mermaid;
4pub mod svg;
5pub mod worker;
6
7pub const MAX_PIXELS: usize = 1_000_000;
8pub const MAX_EDGE: u32 = 8192;
9
10/// Straight RGBA pixels and unscaled SVG dimensions. Moving this value transfers
11/// its pixel buffer; callers need not copy it into an intermediate image format.
12#[derive(Debug)]
13pub struct Raster {
14    pub width: u32,
15    pub height: u32,
16    pub original_width: u32,
17    pub original_height: u32,
18    pub rgba: Vec<u8>,
19}
20
21/// Raster fitting is independent of logical layout. Bounds are physical pixels;
22/// scale is the requested physical pixels per logical SVG unit (1x or Retina).
23/// All output remains subject to the crate's pixel, edge and source-size caps.
24#[derive(Clone, Copy, Debug)]
25pub struct RasterOptions {
26    pub bounds: (u32, u32),
27    pub scale: f64,
28}
29impl RasterOptions {
30    pub const fn fit(bounds: (u32, u32)) -> Self {
31        Self { bounds, scale: 1. }
32    }
33    pub const fn scaled(scale: f64) -> Self {
34        Self {
35            bounds: (MAX_EDGE, MAX_EDGE),
36            scale,
37        }
38    }
39    pub(crate) fn valid(self) -> bool {
40        self.bounds.0 > 0
41            && self.bounds.1 > 0
42            && self.bounds.0 <= MAX_EDGE
43            && self.bounds.1 <= MAX_EDGE
44            && self.scale.is_finite()
45            && self.scale > 0.
46            && self.scale <= 4.
47    }
48}