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