diff options
author | Akshay <[email protected]> | 2021-04-25 05:15:17 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-04-25 05:15:17 +0100 |
commit | c322b0ffd7a0baa31b200cec82aa386c26188b53 (patch) | |
tree | 9fa0d6d6b7cf0bdf0957f9d9f03297911f300ab4 | |
parent | 5c239f9a420ea86fae08be70deece25e0e1b98ff (diff) |
add `export-png` primitive
-rw-r--r-- | Cargo.lock | 47 | ||||
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | src/app.rs | 3 | ||||
-rw-r--r-- | src/lisp/error.rs | 2 | ||||
-rw-r--r-- | src/lisp/prelude.rs | 15 |
5 files changed, 69 insertions, 5 deletions
@@ -3,6 +3,12 @@ | |||
3 | version = 3 | 3 | version = 3 |
4 | 4 | ||
5 | [[package]] | 5 | [[package]] |
6 | name = "adler32" | ||
7 | version = "1.2.0" | ||
8 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
9 | checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" | ||
10 | |||
11 | [[package]] | ||
6 | name = "aho-corasick" | 12 | name = "aho-corasick" |
7 | version = "0.7.15" | 13 | version = "0.7.15" |
8 | source = "registry+https://github.com/rust-lang/crates.io-index" | 14 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -59,6 +65,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
59 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" | 65 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" |
60 | 66 | ||
61 | [[package]] | 67 | [[package]] |
68 | name = "crc32fast" | ||
69 | version = "1.2.1" | ||
70 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
71 | checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" | ||
72 | dependencies = [ | ||
73 | "cfg-if 1.0.0", | ||
74 | ] | ||
75 | |||
76 | [[package]] | ||
77 | name = "deflate" | ||
78 | version = "0.8.6" | ||
79 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
80 | checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174" | ||
81 | dependencies = [ | ||
82 | "adler32", | ||
83 | "byteorder", | ||
84 | ] | ||
85 | |||
86 | [[package]] | ||
62 | name = "env_logger" | 87 | name = "env_logger" |
63 | version = "0.8.3" | 88 | version = "0.8.3" |
64 | source = "registry+https://github.com/rust-lang/crates.io-index" | 89 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -120,11 +145,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
120 | checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" | 145 | checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" |
121 | 146 | ||
122 | [[package]] | 147 | [[package]] |
148 | name = "miniz_oxide" | ||
149 | version = "0.3.7" | ||
150 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
151 | checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" | ||
152 | dependencies = [ | ||
153 | "adler32", | ||
154 | ] | ||
155 | |||
156 | [[package]] | ||
123 | name = "obi" | 157 | name = "obi" |
124 | version = "0.1.0" | 158 | version = "0.1.0" |
125 | dependencies = [ | 159 | dependencies = [ |
126 | "bitvec", | 160 | "bitvec", |
127 | "byteorder", | 161 | "byteorder", |
162 | "png", | ||
128 | ] | 163 | ] |
129 | 164 | ||
130 | [[package]] | 165 | [[package]] |
@@ -134,6 +169,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
134 | checksum = "d70072c20945e1ab871c472a285fc772aefd4f5407723c206242f2c6f94595d6" | 169 | checksum = "d70072c20945e1ab871c472a285fc772aefd4f5407723c206242f2c6f94595d6" |
135 | 170 | ||
136 | [[package]] | 171 | [[package]] |
172 | name = "png" | ||
173 | version = "0.16.8" | ||
174 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
175 | checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6" | ||
176 | dependencies = [ | ||
177 | "bitflags", | ||
178 | "crc32fast", | ||
179 | "deflate", | ||
180 | "miniz_oxide", | ||
181 | ] | ||
182 | |||
183 | [[package]] | ||
137 | name = "radium" | 184 | name = "radium" |
138 | version = "0.6.2" | 185 | version = "0.6.2" |
139 | source = "registry+https://github.com/rust-lang/crates.io-index" | 186 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -8,8 +8,11 @@ edition = "2018" | |||
8 | 8 | ||
9 | [dependencies] | 9 | [dependencies] |
10 | sdl2 = {version = "0.34", features = ["ttf"]} | 10 | sdl2 = {version = "0.34", features = ["ttf"]} |
11 | # obi = { git = "https://github.com/nerdypepper/obi", rev = "63af58d6a3fd68bdb313305881627dab534346e2" } | 11 | # obi = { git = "https://github.com/nerdypepper/obi", rev = "7773bfb4c63ab3ea49b428e20ef0946b713fd5f5" } |
12 | obi = { path = "../obi"} | ||
13 | env_logger = "0.8.3" | 12 | env_logger = "0.8.3" |
14 | log = "0.4.0" | 13 | log = "0.4.0" |
15 | pico-args = "0.4.0" | 14 | pico-args = "0.4.0" |
15 | |||
16 | [dependencies.obi] | ||
17 | path = "../obi" | ||
18 | features = ["png"] | ||
@@ -904,8 +904,7 @@ impl<'ctx> AppState<'ctx> { | |||
904 | }); | 904 | }); |
905 | } | 905 | } |
906 | } | 906 | } |
907 | Brush::Circle(CircleBrush { size }) | 907 | Brush::Circle(CircleBrush { size }) => { |
908 | | Brush::Line(LineBrush { size, .. }) => { | ||
909 | let pt = (x, y); | 908 | let pt = (x, y); |
910 | let val = self.active_color; | 909 | let val = self.active_color; |
911 | if let Ok(o) = self.paint_point(pt, val, size) { | 910 | 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 { | |||
98 | DivByZero, | 98 | DivByZero, |
99 | TypeMismatch, | 99 | TypeMismatch, |
100 | NoFileName, | 100 | NoFileName, |
101 | FileError(io::Error), | ||
101 | AccessEmptyList, | 102 | AccessEmptyList, |
102 | InvalidCoordinates((LispNumber, LispNumber)), | 103 | InvalidCoordinates((LispNumber, LispNumber)), |
103 | AssertionError { expected: LispExpr, got: LispExpr }, | 104 | AssertionError { expected: LispExpr, got: LispExpr }, |
@@ -126,6 +127,7 @@ impl fmt::Display for EvalError { | |||
126 | Self::TypeMismatch => write!(f, "mismatched types"), | 127 | Self::TypeMismatch => write!(f, "mismatched types"), |
127 | Self::DivByZero => write!(f, "attempt to divide by zero"), | 128 | Self::DivByZero => write!(f, "attempt to divide by zero"), |
128 | Self::NoFileName => write!(f, "no file name specified"), | 129 | Self::NoFileName => write!(f, "no file name specified"), |
130 | Self::FileError(e) => write!(f, "file error: {}", e), | ||
129 | Self::AccessEmptyList => write!(f, "attempted to access empty list"), | 131 | Self::AccessEmptyList => write!(f, "attempted to access empty list"), |
130 | Self::InvalidCoordinates((x, y)) => write!(f, "invalid coordinates: {} {}", x, y), | 132 | Self::InvalidCoordinates((x, y)) => write!(f, "invalid coordinates: {} {}", x, y), |
131 | Self::AssertionError { expected, got } => { | 133 | 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::{ | |||
14 | utils::{load_script, rect_coords}, | 14 | utils::{load_script, rect_coords}, |
15 | }; | 15 | }; |
16 | 16 | ||
17 | use std::convert::TryInto; | 17 | use std::{convert::TryInto, fs::File, io::BufWriter, path::Path}; |
18 | 18 | ||
19 | #[macro_export] | 19 | #[macro_export] |
20 | macro_rules! primitive { | 20 | macro_rules! primitive { |
@@ -260,6 +260,18 @@ pub fn new_env() -> Result<Environment, LispError> { | |||
260 | } | 260 | } |
261 | }); | 261 | }); |
262 | 262 | ||
263 | primitive!(env, Arity::Exact(1), "export-png", |args, app| { | ||
264 | if let LispExpr::StringLit(s) = &args[0] { | ||
265 | let export_path = Path::new(&s); | ||
266 | let file = File::create(export_path).map_err(EvalError::FileError)?; | ||
267 | let w = BufWriter::new(file); | ||
268 | app.export().write_png(w); | ||
269 | Ok(LispExpr::Unit) | ||
270 | } else { | ||
271 | Err(EvalError::TypeMismatch.into()) | ||
272 | } | ||
273 | }); | ||
274 | |||
263 | primitive!(env, Arity::Exact(0), "grid-enabled?", |_, app| { | 275 | primitive!(env, Arity::Exact(0), "grid-enabled?", |_, app| { |
264 | Ok(LispExpr::BoolLit(app.grid.enabled)) | 276 | Ok(LispExpr::BoolLit(app.grid.enabled)) |
265 | }); | 277 | }); |
@@ -524,5 +536,6 @@ pub fn new_env() -> Result<Environment, LispError> { | |||
524 | primitive!(env, Arity::Exact(1), "id", |args, _| { | 536 | primitive!(env, Arity::Exact(1), "id", |args, _| { |
525 | Ok(args[0].clone()) | 537 | Ok(args[0].clone()) |
526 | }); | 538 | }); |
539 | |||
527 | Ok(env) | 540 | Ok(env) |
528 | } | 541 | } |