Skip to main content

devpage_design/
lib.rs

1//! The design engine: parses `.design` files, resolves them into scenes, lays
2//! them out and paints them. It does no I/O, so the same code runs natively
3//! in devpage and compiled to WebAssembly in the browser.
4
5pub mod atlas;
6pub mod parse;
7pub mod proto;
8pub mod render;
9pub mod resolve;
10pub mod shadow;
11pub mod wire;
12
13use std::fmt;
14
15pub use render::Renderer;
16pub use resolve::{Files, Scene, Theme, resolve};
17
18/// Something wrong with a design, at a place in one of its files.
19#[derive(Debug, Clone, PartialEq)]
20pub struct Problem {
21    pub file: String,
22    pub pos: parse::Pos,
23    pub message: String,
24}
25
26impl fmt::Display for Problem {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(
29            f,
30            "{}:{}:{}: {}",
31            self.file, self.pos.line, self.pos.col, self.message
32        )
33    }
34}
35
36/// Paints a scene's whole drawing at 1× in `theme` and encodes it as a PNG,
37/// or says why it can't: see [`Renderer::drawing`].
38pub fn png(renderer: &mut Renderer, scene: &Scene, theme: Theme) -> Result<Vec<u8>, String> {
39    let drawing = renderer
40        .drawing(scene, theme, None)
41        .map_err(|e| e.message)?;
42    render::paint_list(drawing.area, &drawing.list, 1.0)
43        .into_png()
44        .map_err(|e| format!("encoding the PNG: {e}"))
45}