diff options
Diffstat (limited to 'lua-bindings/src')
-rw-r--r-- | lua-bindings/src/lib.rs | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/lua-bindings/src/lib.rs b/lua-bindings/src/lib.rs new file mode 100644 index 0000000..6628ee2 --- /dev/null +++ b/lua-bindings/src/lib.rs | |||
@@ -0,0 +1,116 @@ | |||
1 | use std::sync::atomic::{AtomicUsize, Ordering}; | ||
2 | |||
3 | use diaphragm_core::{ | ||
4 | solving::VariableHandle, | ||
5 | text::{FontDescription as CoreFontDescription, FontStyle, FontWeight, Text as CoreText}, | ||
6 | types::Float as CoreFloat, | ||
7 | }; | ||
8 | // use diaphragm_cairo_renderer::CairoRenderer; | ||
9 | // use diaphragm_core::Runtime; | ||
10 | // use diaphragm_z3_solver::{z3, Z3Context}; | ||
11 | use mlua::prelude::*; | ||
12 | |||
13 | static MAX_ID: AtomicUsize = AtomicUsize::new(0); | ||
14 | |||
15 | #[derive(Clone, Copy, Debug)] | ||
16 | struct Float(CoreFloat); | ||
17 | |||
18 | impl Float { | ||
19 | fn new() -> Float { | ||
20 | Float(CoreFloat::Variable(VariableHandle::new( | ||
21 | MAX_ID.fetch_add(1, Ordering::SeqCst), | ||
22 | ))) | ||
23 | } | ||
24 | } | ||
25 | impl LuaUserData for Float {} | ||
26 | |||
27 | fn float(_: &Lua, _: ()) -> LuaResult<Float> { | ||
28 | Ok(Float::new()) | ||
29 | } | ||
30 | |||
31 | #[derive(Clone, Debug)] | ||
32 | struct FontDescription(CoreFontDescription); | ||
33 | impl LuaUserData for FontDescription {} | ||
34 | |||
35 | const DEFAULT_FONT_FAMILY: &str = "serif"; | ||
36 | |||
37 | impl Default for FontDescription { | ||
38 | fn default() -> Self { | ||
39 | Self(CoreFontDescription { | ||
40 | family: DEFAULT_FONT_FAMILY.to_string(), | ||
41 | style: FontStyle::Normal, | ||
42 | weight: FontWeight::Normal, | ||
43 | size: Float::new().0, | ||
44 | }) | ||
45 | } | ||
46 | } | ||
47 | |||
48 | fn font(_: &Lua, params: LuaTable) -> LuaResult<FontDescription> { | ||
49 | // TODO: better validation of the table | ||
50 | // What happens when I mistype a param? | ||
51 | // TODO: better error handling | ||
52 | |||
53 | let family = params | ||
54 | .get::<_, Option<_>>("family")? | ||
55 | .unwrap_or_else(|| DEFAULT_FONT_FAMILY.to_string()); | ||
56 | |||
57 | let style = match params.get::<_, Option<String>>("style")?.as_deref() { | ||
58 | Some("normal") | None => FontStyle::Normal, | ||
59 | Some(_) => return Err(LuaError::RuntimeError("Unknown style".to_string())), | ||
60 | }; | ||
61 | |||
62 | let weight = match params.get::<_, Option<String>>("weight")?.as_deref() { | ||
63 | Some("normal") | None => FontWeight::Normal, | ||
64 | Some(_) => return Err(LuaError::RuntimeError("Unknown weight".to_string())), | ||
65 | }; | ||
66 | |||
67 | let size = params | ||
68 | .get::<_, Option<_>>("size")? | ||
69 | .unwrap_or_else(Float::new); | ||
70 | |||
71 | Ok(FontDescription(CoreFontDescription { | ||
72 | family, | ||
73 | style, | ||
74 | weight, | ||
75 | size: size.0, | ||
76 | })) | ||
77 | } | ||
78 | |||
79 | #[derive(Clone, Debug)] | ||
80 | struct Text(CoreText); | ||
81 | impl LuaUserData for Text {} | ||
82 | |||
83 | fn text(_: &Lua, params: LuaTable) -> LuaResult<Text> { | ||
84 | let content = params.get("content")?; | ||
85 | |||
86 | let font = params | ||
87 | .get::<_, Option<FontDescription>>("font")? | ||
88 | .unwrap_or_default(); | ||
89 | |||
90 | Ok(Text(CoreText { | ||
91 | content, | ||
92 | font: font.0, | ||
93 | })) | ||
94 | } | ||
95 | |||
96 | fn draw(_: &Lua, params: LuaTable) -> LuaResult<()> { | ||
97 | let content: LuaTable = params.get("content")?; | ||
98 | let output: LuaTable = params.get("output")?; | ||
99 | |||
100 | dbg!(content, output); | ||
101 | |||
102 | Ok(()) | ||
103 | } | ||
104 | |||
105 | #[mlua::lua_module] | ||
106 | fn libdiaphragm(lua: &Lua) -> LuaResult<LuaTable> { | ||
107 | // TODO: the solver as a mutable global solves so much problem (pun not intended) | ||
108 | let exports = lua.create_table()?; | ||
109 | exports.set("text", lua.create_function(text)?)?; | ||
110 | exports.set("font", lua.create_function(font)?)?; | ||
111 | exports.set("float", lua.create_function(float)?)?; | ||
112 | |||
113 | exports.set("draw", lua.create_function(draw)?)?; | ||
114 | |||
115 | Ok(exports) | ||
116 | } | ||