[wasm-rust] Docs and API update

This commit is contained in:
Simon Cozens 2023-02-25 15:35:37 +00:00 committed by Behdad Esfahbod
parent 0d237d062e
commit db789eacb4
2 changed files with 14 additions and 6 deletions

View File

@ -35,12 +35,12 @@ harfbuzz-wasm = { path = "your-harfbuzz-source/src/wasm/rust/harfbuzz-wasm"}
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
pub fn shape(_font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { pub fn shape(_shape_plan: u32, _font_ref: u32, _buf_ref: u32, _features: u32) -> i32 {
1 // success! 1 // success!
} }
``` ```
This exports a shaping function which takes two arguments, tokens representing the font and the buffer, and returns a status value. We can pass these tokens back to Harfbuzz in order to use its native functions on the font and buffer objects. More on native functions later - let's get this shaper compiled and added into a font: This exports a shaping function which takes four arguments, tokens representing the shaping plan, the font and the buffer, and returns a status value. We can pass these tokens back to Harfbuzz in order to use its native functions on the font and buffer objects. More on native functions later - let's get this shaper compiled and added into a font:
* To compile the shaper, run `wasm-pack build --target nodejs`: * To compile the shaper, run `wasm-pack build --target nodejs`:
@ -85,7 +85,7 @@ In debugging builds of Harfbuzz, we can print some output from the web assembly
use harfbuzz_wasm::debug; use harfbuzz_wasm::debug;
#[wasm_bindgen] #[wasm_bindgen]
pub fn shape(_font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { pub fn shape(shape_plan, font_ref: u32, buf_ref: u32, _features: u32) -> i32 {
debug("Hello from Rust!\n"); debug("Hello from Rust!\n");
1 1
} }
@ -106,7 +106,7 @@ use wasm_bindgen::prelude::*;
use harfbuzz_wasm::{Font, GlyphBuffer}; use harfbuzz_wasm::{Font, GlyphBuffer};
#[wasm_bindgen] #[wasm_bindgen]
pub fn shape(_font_ref: u32, _buf_ref: u32, _features: u32, _num_features: u32) -> i32 { pub fn shape(_shape_plan:u32, font_ref: u32, buf_ref: u32, _features: u32) -> i32 {
let font = Font::from_ref(font_ref); let font = Font::from_ref(font_ref);
let mut buffer = GlyphBuffer::from_ref(buf_ref); let mut buffer = GlyphBuffer::from_ref(buf_ref);
for mut item in buffer.glyphs.iter_mut() { for mut item in buffer.glyphs.iter_mut() {

View File

@ -42,7 +42,7 @@ extern "C" {
fn font_get_glyph_h_advance(font: u32, glyph: u32) -> i32; fn font_get_glyph_h_advance(font: u32, glyph: u32) -> i32;
fn font_get_glyph_v_advance(font: u32, glyph: u32) -> i32; fn font_get_glyph_v_advance(font: u32, glyph: u32) -> i32;
fn face_copy_table(font: u32, tag: u32) -> Blob; fn face_copy_table(font: u32, tag: u32) -> Blob;
fn buffer_copy_contents(buffer: u32) -> CBufferContents; fn buffer_copy_contents(buffer: u32, cbuffer: *mut CBufferContents) -> bool;
fn buffer_set_contents(buffer: u32, cbuffer: &CBufferContents) -> bool; fn buffer_set_contents(buffer: u32, cbuffer: &CBufferContents) -> bool;
fn debugprint(s: *const u8); fn debugprint(s: *const u8);
fn shape_with( fn shape_with(
@ -203,7 +203,15 @@ impl<T: BufferItem> Buffer<T> {
/// The `Buffer` struct implements Drop, meaning that when the shaping /// The `Buffer` struct implements Drop, meaning that when the shaping
/// function is finished, the buffer contents are sent back to Harfbuzz. /// function is finished, the buffer contents are sent back to Harfbuzz.
pub fn from_ref(ptr: u32) -> Self { pub fn from_ref(ptr: u32) -> Self {
let c_contents: CBufferContents = unsafe { buffer_copy_contents(ptr) }; let mut c_contents = CBufferContents {
info: std::ptr::null_mut(),
position: std::ptr::null_mut(),
length: 0,
};
unsafe {
buffer_copy_contents(ptr, &mut c_contents) || panic!("Couldn't copy buffer contents")
};
let positions: Vec<CGlyphPosition> = unsafe { let positions: Vec<CGlyphPosition> = unsafe {
std::slice::from_raw_parts(c_contents.position, c_contents.length as usize).to_vec() std::slice::from_raw_parts(c_contents.position, c_contents.length as usize).to_vec()
}; };