Skip to main content

document_graphics/
lib.rs

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