summaryrefslogtreecommitdiffstats
path: root/lua-bindings/src
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2022-12-22 12:19:59 +0100
committerMinijackson <minijackson@riseup.net>2022-12-22 12:19:59 +0100
commit92a02c34628343153b33602eae00cef46e28d191 (patch)
tree8622ec528d24e456be22d984d93aa9bcafc97399 /lua-bindings/src
downloaddiaphragm-92a02c34628343153b33602eae00cef46e28d191.tar.gz
diaphragm-92a02c34628343153b33602eae00cef46e28d191.zip
WIP
Diffstat (limited to 'lua-bindings/src')
-rw-r--r--lua-bindings/src/lib.rs116
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 @@
1use std::sync::atomic::{AtomicUsize, Ordering};
2
3use 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};
11use mlua::prelude::*;
12
13static MAX_ID: AtomicUsize = AtomicUsize::new(0);
14
15#[derive(Clone, Copy, Debug)]
16struct Float(CoreFloat);
17
18impl Float {
19 fn new() -> Float {
20 Float(CoreFloat::Variable(VariableHandle::new(
21 MAX_ID.fetch_add(1, Ordering::SeqCst),
22 )))
23 }
24}
25impl LuaUserData for Float {}
26
27fn float(_: &Lua, _: ()) -> LuaResult<Float> {
28 Ok(Float::new())
29}
30
31#[derive(Clone, Debug)]
32struct FontDescription(CoreFontDescription);
33impl LuaUserData for FontDescription {}
34
35const DEFAULT_FONT_FAMILY: &str = "serif";
36
37impl 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
48fn 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)]
80struct Text(CoreText);
81impl LuaUserData for Text {}
82
83fn 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
96fn 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]
106fn 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}