From c322b0ffd7a0baa31b200cec82aa386c26188b53 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 25 Apr 2021 09:45:17 +0530 Subject: add `export-png` primitive --- Cargo.lock | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 +++++-- src/app.rs | 3 +-- src/lisp/error.rs | 2 ++ src/lisp/prelude.rs | 15 ++++++++++++++- 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2ac9444..0857ef2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler32" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" + [[package]] name = "aho-corasick" version = "0.7.15" @@ -58,6 +64,25 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "crc32fast" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" +dependencies = [ + "cfg-if 1.0.0", +] + +[[package]] +name = "deflate" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174" +dependencies = [ + "adler32", + "byteorder", +] + [[package]] name = "env_logger" version = "0.8.3" @@ -119,12 +144,22 @@ version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +[[package]] +name = "miniz_oxide" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" +dependencies = [ + "adler32", +] + [[package]] name = "obi" version = "0.1.0" dependencies = [ "bitvec", "byteorder", + "png", ] [[package]] @@ -133,6 +168,18 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d70072c20945e1ab871c472a285fc772aefd4f5407723c206242f2c6f94595d6" +[[package]] +name = "png" +version = "0.16.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6" +dependencies = [ + "bitflags", + "crc32fast", + "deflate", + "miniz_oxide", +] + [[package]] name = "radium" version = "0.6.2" diff --git a/Cargo.toml b/Cargo.toml index 311e10b..01e7e31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,11 @@ edition = "2018" [dependencies] sdl2 = {version = "0.34", features = ["ttf"]} -# obi = { git = "https://github.com/nerdypepper/obi", rev = "63af58d6a3fd68bdb313305881627dab534346e2" } -obi = { path = "../obi"} +# obi = { git = "https://github.com/nerdypepper/obi", rev = "7773bfb4c63ab3ea49b428e20ef0946b713fd5f5" } env_logger = "0.8.3" log = "0.4.0" pico-args = "0.4.0" + +[dependencies.obi] +path = "../obi" +features = ["png"] diff --git a/src/app.rs b/src/app.rs index d0089e9..66a27e3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -904,8 +904,7 @@ impl<'ctx> AppState<'ctx> { }); } } - Brush::Circle(CircleBrush { size }) - | Brush::Line(LineBrush { size, .. }) => { + Brush::Circle(CircleBrush { size }) => { let pt = (x, y); let val = self.active_color; if let Ok(o) = self.paint_point(pt, val, size) { diff --git a/src/lisp/error.rs b/src/lisp/error.rs index f323bb8..9e9dc90 100644 --- a/src/lisp/error.rs +++ b/src/lisp/error.rs @@ -98,6 +98,7 @@ pub enum EvalError { DivByZero, TypeMismatch, NoFileName, + FileError(io::Error), AccessEmptyList, InvalidCoordinates((LispNumber, LispNumber)), AssertionError { expected: LispExpr, got: LispExpr }, @@ -126,6 +127,7 @@ impl fmt::Display for EvalError { Self::TypeMismatch => write!(f, "mismatched types"), Self::DivByZero => write!(f, "attempt to divide by zero"), Self::NoFileName => write!(f, "no file name specified"), + Self::FileError(e) => write!(f, "file error: {}", e), Self::AccessEmptyList => write!(f, "attempted to access empty list"), Self::InvalidCoordinates((x, y)) => write!(f, "invalid coordinates: {} {}", x, y), Self::AssertionError { expected, got } => { diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index 7cb46ff..8088510 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -14,7 +14,7 @@ use crate::{ utils::{load_script, rect_coords}, }; -use std::convert::TryInto; +use std::{convert::TryInto, fs::File, io::BufWriter, path::Path}; #[macro_export] macro_rules! primitive { @@ -260,6 +260,18 @@ pub fn new_env() -> Result { } }); + primitive!(env, Arity::Exact(1), "export-png", |args, app| { + if let LispExpr::StringLit(s) = &args[0] { + let export_path = Path::new(&s); + let file = File::create(export_path).map_err(EvalError::FileError)?; + let w = BufWriter::new(file); + app.export().write_png(w); + Ok(LispExpr::Unit) + } else { + Err(EvalError::TypeMismatch.into()) + } + }); + primitive!(env, Arity::Exact(0), "grid-enabled?", |_, app| { Ok(LispExpr::BoolLit(app.grid.enabled)) }); @@ -524,5 +536,6 @@ pub fn new_env() -> Result { primitive!(env, Arity::Exact(1), "id", |args, _| { Ok(args[0].clone()) }); + Ok(env) } -- cgit v1.2.3