Temp draw_glyphs ABI

This commit is contained in:
marceline-cramer 2022-07-18 17:51:33 -06:00
parent 1feb3e9ef2
commit 43b6aca29a
3 changed files with 26 additions and 2 deletions

View File

@ -44,6 +44,9 @@ impl PanelImpl for DummyPanel {
};
ctx.draw_rect(bounds, Color::MAGENTA);
let glyphs = layout.get_glyphs();
self.panel.draw_glyphs(glyphs.as_slice());
}
fn on_cursor_event(&mut self, kind: CursorEventKind, at: canary_script::Vec2) {

View File

@ -95,6 +95,10 @@ impl Panel {
}
}
pub fn draw_glyphs(&self, glyphs: &[GlyphPosition]) {
unsafe { draw_glyphs(glyphs.as_ptr() as u32, glyphs.len() as u32) }
}
pub fn draw_triangle(&self, v1: Vec2, v2: Vec2, v3: Vec2, color: Color) {
let vertices = [
MeshVertex {
@ -139,7 +143,7 @@ impl TextLayout {
let num = text_layout_get_glyphs_num(self.0) as usize;
let mut glyphs = Vec::with_capacity(num);
glyphs.set_len(num);
text_layout_get_glyphs_data(self.0, glyphs.as_ptr() as u32);
text_layout_get_glyphs_data(self.0, glyphs.as_ptr() as u32, num as u32);
glyphs
}
}
@ -153,10 +157,11 @@ impl Drop for TextLayout {
extern "C" {
fn draw_indexed(vertices_ptr: u32, vertices_num: u32, indices_ptr: u32, indices_num: u32);
fn draw_glyphs(glyphs_ptr: u32, glyphs_len: u32);
fn text_layout_new(text_ptr: u32, text_len: u32) -> u32;
fn text_layout_delete(id: u32);
fn text_layout_get_bounds(id: u32, rect_ptr: u32);
fn text_layout_get_glyphs_num(id: u32) -> u32;
fn text_layout_get_glyphs_data(id: u32, glyphs_ptr: u32);
fn text_layout_get_glyphs_data(id: u32, glyphs_ptr: u32, glyphs_num: u32);
}

View File

@ -11,6 +11,7 @@ pub mod text;
pub trait ScriptAbi {
fn start_draw(&self);
fn draw_indexed(&self, vertices: &[MeshVertex], indices: &[MeshIndex]);
fn draw_glyphs(&self, glyphs: &[GlyphPosition]);
fn with_draw_commands(&self, f: impl FnOnce(&[DrawCommand]));
fn text_layout_new(&self, text: &str) -> u32;
@ -101,6 +102,15 @@ impl<T: ScriptAbi> WasmtimeScript<T> {
},
)?;
linker.func_wrap(
module,
"draw_glyphs",
|mut caller: wasmtime::Caller<'_, T>, glyphs_ptr: u32, glyphs_num: u32| {
let glyphs = Self::get_memory_slice(&mut caller, glyphs_ptr, glyphs_num);
caller.data().draw_glyphs(glyphs);
},
)?;
linker.func_wrap(
module,
"text_layout_new",
@ -230,6 +240,12 @@ impl ScriptAbi for ScriptAbiImpl {
})
}
fn draw_glyphs(&self, glyphs: &[GlyphPosition]) {
let font = self.font_store.load_font("Liberation Sans");
let cmds = font.draw(glyphs, 0.9);
self.draw_cmds.lock().extend(cmds.into_iter());
}
fn with_draw_commands(&self, f: impl FnOnce(&[DrawCommand])) {
f(self.draw_cmds.lock().as_slice());
}