Skip to main content

document_graphics/
lib.rs

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