summaryrefslogtreecommitdiffstats
path: root/lua-bindings
diff options
context:
space:
mode:
Diffstat (limited to 'lua-bindings')
-rw-r--r--lua-bindings/diaphragm.lua11
-rw-r--r--lua-bindings/src/lib.rs84
2 files changed, 71 insertions, 24 deletions
diff --git a/lua-bindings/diaphragm.lua b/lua-bindings/diaphragm.lua
index a308c28..d4e3dc9 100644
--- a/lua-bindings/diaphragm.lua
+++ b/lua-bindings/diaphragm.lua
@@ -1,3 +1,5 @@
1-- TODO: split file
2
1local M = {} 3local M = {}
2 4
3M.core = require("libdiaphragm") 5M.core = require("libdiaphragm")
@@ -184,6 +186,15 @@ function M.straight_path.new(params)
184 return result 186 return result
185end 187end
186 188
189M.image = {}
190
191function M.image.new(params)
192 params = params or {}
193 local result = M.core.image(params)
194 M.util.tbl_assign_reserved(params, result, bounds_reserved)
195 return result
196end
197
187function M.shape(params) 198function M.shape(params)
188 params = params or {} 199 params = params or {}
189 200
diff --git a/lua-bindings/src/lib.rs b/lua-bindings/src/lib.rs
index 86a759d..26081c8 100644
--- a/lua-bindings/src/lib.rs
+++ b/lua-bindings/src/lib.rs
@@ -11,7 +11,8 @@ use diaphragm_cairo_renderer::CairoRenderer;
11use diaphragm_core::{ 11use diaphragm_core::{
12 colors::Color, 12 colors::Color,
13 core_shapes::{ 13 core_shapes::{
14 CoreDrawable, CoreShape, Rectangle as CoreRectangle, StraightPath as CoreStraightPath, 14 CoreDrawable, CoreShape, Image as CoreImage, Rectangle as CoreRectangle,
15 StraightPath as CoreStraightPath,
15 }, 16 },
16 solving::VariableHandle, 17 solving::VariableHandle,
17 styles::Pattern, 18 styles::Pattern,
@@ -517,28 +518,6 @@ fn text(_: &Lua, params: LuaTable) -> LuaResult<Text> {
517 518
518 let context = new_shape_context(); 519 let context = new_shape_context();
519 520
520 let content_2 = content.clone();
521 let font_2 = font.0.clone();
522 let context_2 = context.clone();
523
524 // TODO: this shouldn't be here, this should be an innate property of Text. Move the
525 // Drawable<T> struct in core?
526 runtime_thread_do(Box::new(move |r| {
527 let (text_width, text_height) = r.renderer().text_extents(&content_2, &font_2);
528 let solver = r.solver_ctx();
529 // let scale = solver.float_div(font.0.size, CoreFloat::Fixed(height));
530
531 let calculated_width = solver.float_mul(&[CoreFloat::Fixed(text_width), font.0.size]);
532 let bounds_width = context_2.bounds().width(solver);
533 let width_constraint = solver.float_eq(bounds_width, calculated_width);
534 solver.constrain(width_constraint);
535
536 let calculated_height = solver.float_mul(&[CoreFloat::Fixed(text_height), font.0.size]);
537 let bounds_height = context_2.bounds().height(solver);
538 let height_constraint = solver.float_eq(bounds_height, calculated_height);
539 solver.constrain(height_constraint);
540 }));
541
542 Ok(Text(Drawable { 521 Ok(Text(Drawable {
543 shape: CoreText { 522 shape: CoreText {
544 content, 523 content,
@@ -646,6 +625,63 @@ fn straight_path(_: &Lua, params: LuaTable) -> LuaResult<StraightPath> {
646 points.try_into() 625 points.try_into()
647} 626}
648 627
628#[derive(Debug, Clone)]
629struct Image(Drawable<CoreImage>);
630impl LuaUserData for Image {
631 fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
632 methods.add_method("draw", |_, this, _params: ()| {
633 let drawable = this.0.clone().into();
634 runtime_thread_do(Box::new(|r| {
635 r.add_drawable(drawable);
636 }));
637 Ok(())
638 })
639 }
640
641 fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
642 add_bound_fields(fields);
643 }
644}
645
646impl HasContext for Image {
647 fn get_context(&self) -> &CoreShapeContext {
648 &self.0.context
649 }
650
651 fn get_context_mut(&mut self) -> &mut CoreShapeContext {
652 &mut self.0.context
653 }
654}
655
656impl TryFrom<LuaValue<'_>> for Image {
657 type Error = LuaError;
658
659 fn try_from(value: LuaValue) -> Result<Self, Self::Error> {
660 match value {
661 LuaValue::Table(t) => {
662 let path: String = t.get("path")?;
663 let keep_aspect_ratio = t.get::<_, Option<_>>("keep_aspect_ratio")?.unwrap_or(true);
664
665 Ok(Image(Drawable {
666 shape: CoreImage::new(path.into(), keep_aspect_ratio),
667 context: new_shape_context(),
668 }))
669 }
670
671 LuaValue::UserData(u) => Ok(Image(u.borrow::<Image>()?.0.clone())),
672 _ => Err(LuaError::FromLuaConversionError {
673 from: value.type_name(),
674 to: "StraightPath",
675 message: Some("Only a list of points are allowed".to_string()),
676 }),
677 }
678 }
679}
680
681fn image(_: &Lua, params: LuaTable) -> LuaResult<Image> {
682 LuaValue::Table(params).try_into()
683}
684
649// It just has a context for bounds, the rest is handled manually in Lua 685// It just has a context for bounds, the rest is handled manually in Lua
650#[derive(Debug, Clone)] 686#[derive(Debug, Clone)]
651struct ComplexShape(CoreShapeContext); 687struct ComplexShape(CoreShapeContext);
@@ -800,8 +836,8 @@ fn libdiaphragm(lua: &Lua) -> LuaResult<LuaTable> {
800 exports.set("font", lua.create_function(font)?)?; 836 exports.set("font", lua.create_function(font)?)?;
801 837
802 exports.set("rectangle", lua.create_function(rectangle)?)?; 838 exports.set("rectangle", lua.create_function(rectangle)?)?;
803
804 exports.set("straight_path", lua.create_function(straight_path)?)?; 839 exports.set("straight_path", lua.create_function(straight_path)?)?;
840 exports.set("image", lua.create_function(image)?)?;
805 841
806 exports.set("complex_shape", lua.create_function(complex_shape)?)?; 842 exports.set("complex_shape", lua.create_function(complex_shape)?)?;
807 843