From 83b9c34f0b5d9b6b6f65aae0e1bb92877972a5ed Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sat, 25 Feb 2023 15:23:22 +0000 Subject: [PATCH] [wasm] Add rust example --- src/wasm/sample/rust/hello-wasm/Cargo.toml | 13 +++++++++++++ src/wasm/sample/rust/hello-wasm/src/lib.rs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/wasm/sample/rust/hello-wasm/Cargo.toml create mode 100644 src/wasm/sample/rust/hello-wasm/src/lib.rs diff --git a/src/wasm/sample/rust/hello-wasm/Cargo.toml b/src/wasm/sample/rust/hello-wasm/Cargo.toml new file mode 100644 index 000000000..e8207ed35 --- /dev/null +++ b/src/wasm/sample/rust/hello-wasm/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "hello-wasm" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +#externref = "0.1.0" +wasm-bindgen = "0.2.0" +tiny-rng = "0.2.0" +harfbuzz-wasm = { path="../../rust/harfbuzz-wasm"} diff --git a/src/wasm/sample/rust/hello-wasm/src/lib.rs b/src/wasm/sample/rust/hello-wasm/src/lib.rs new file mode 100644 index 000000000..223f2699c --- /dev/null +++ b/src/wasm/sample/rust/hello-wasm/src/lib.rs @@ -0,0 +1,18 @@ +use harfbuzz_wasm::{Font, GlyphBuffer}; +use tiny_rng::{Rand, Rng}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn shape(font_ref: u32, buf_ref: u32) -> i32 { + let mut rng = Rng::from_seed(123456); + let font = Font::from_ref(font_ref); + font.shape_with(buf_ref, "ot"); + let mut buffer = GlyphBuffer::from_ref(buf_ref); + for mut item in buffer.glyphs.iter_mut() { + // Randomize it! + item.x_offset = ((rng.rand_u32() as i32) >> 24) - 120; + item.y_offset = ((rng.rand_u32() as i32) >> 24) - 120; + } + // Buffer is written back to HB on drop + 1 +}