Skip to content

Commit

Permalink
Update web worker API
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Aug 19, 2024
1 parent e1a02d9 commit 2afa5a8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
29 changes: 13 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cfg_if! {
if #[cfg(target_family = "wasm")] {
use std::collections::HashMap;
use dfont::DFont;
use render::{test_font_glyphs, test_font_words};
use render::{test_font_words, encodedglyphs};
use serde_json::json;
use ttj::table_diff;
use skrifa::MetadataProvider;
Expand Down Expand Up @@ -80,19 +80,6 @@ cfg_if! {
})).unwrap_or("Couldn't do it".to_string());
}

#[wasm_bindgen]
pub fn diff(font_a: &[u8], font_b: &[u8]) -> String {
let f_a = DFont::new(font_a);
let f_b = DFont::new(font_b);
let val = json!({
"tables": table_diff(&f_a.fontref(), &f_b.fontref()),
"glyphs": test_font_glyphs(&f_a, &f_b),
"words": test_font_words(&f_a, &f_b),
});
serde_json::to_string(&val)
.unwrap_or("Couldn't do it".to_string())
}

#[wasm_bindgen]
pub fn diff_tables(font_a: &[u8], font_b: &[u8], f: &js_sys::Function) {
let f_a = DFont::new(font_a);
Expand All @@ -105,14 +92,24 @@ cfg_if! {
}

#[wasm_bindgen]
pub fn diff_glyphs(font_a: &[u8], font_b: &[u8], location: &str, f: &js_sys::Function) {
pub fn modified_glyphs(font_a: &[u8], font_b: &[u8], location: &str, f: &js_sys::Function) {
let mut f_a = DFont::new(font_a);
let mut f_b = DFont::new(font_b);
let _hack = f_a.set_location(location);
let _hack = f_b.set_location(location);

let val = json!({
"glyphs": test_font_glyphs(&f_a, &f_b)
"modified_glyphs": encodedglyphs::modified_encoded_glyphs(&f_a, &f_b)
});
f.call1(&JsValue::NULL, &JsValue::from_str(&serde_json::to_string(&val).unwrap_or("Couldn't do it".to_string()))).unwrap();
}

#[wasm_bindgen]
pub fn new_missing_glyphs(font_a: &[u8], font_b: &[u8],f: &js_sys::Function) {
let f_a = DFont::new(font_a);
let f_b = DFont::new(font_b);
let val = json!({
"new_missing_glyphs": encodedglyphs::new_missing_glyphs(&f_a, &f_b)
});
f.call1(&JsValue::NULL, &JsValue::from_str(&serde_json::to_string(&val).unwrap_or("Couldn't do it".to_string()))).unwrap();
}
Expand Down
3 changes: 2 additions & 1 deletion src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,15 @@ pub(crate) fn diff_many_words(
if commands_a == commands_b {
continue;
}
let buffers_same = buffer_a == buffer_b;
let img_a = renderer_a.render_positioned_glyphs(&commands_a);
let img_b = renderer_b.render_positioned_glyphs(&commands_b);
let percent = count_differences(img_a, img_b);
if percent > threshold {
differences.push(Difference {
word: word.to_string(),
buffer_a,
buffer_b,
buffer_b: if buffers_same { None } else { Some(buffer_b) },
// diff_map,
ot_features: "".to_string(),
lang: "".to_string(),
Expand Down
81 changes: 44 additions & 37 deletions www/webworker.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
var module = import('../pkg/diffenator3.js');
var module = import("../pkg/diffenator3.js");
async function init() {
let wasm = await module;
self.postMessage({ type: "ready" })
// console.log("Got wasm module", wasm);
wasm.debugging();
self.onmessage = async (event) => {
// console.log("Worker received message");
// console.log(event);
const { command, beforeFont, location, afterFont } = event.data;
if (command == "axes") {
let obj = JSON.parse(wasm.axes(beforeFont, afterFont))
obj["type"] = "axes";
self.postMessage(obj);
} else if (command == "tables") {
wasm.diff_tables(beforeFont, afterFont, (tables) => {
self.postMessage({
"type": "tables",
"tables": JSON.parse(tables)["tables"]
});
});
} else if (command == "glyphs") {
wasm.diff_glyphs(beforeFont, afterFont, location, (glyphs) => {
self.postMessage({
"type": "glyphs",
"glyphs": JSON.parse(glyphs)["glyphs"]
});
});
} else if (command == "words") {
wasm.diff_words(beforeFont, afterFont, location, (words) => {
self.postMessage({
"type": "words",
"words": JSON.parse(words)["words"]
});
});
}

let wasm = await module;
self.postMessage({ type: "ready" });
// console.log("Got wasm module", wasm);
wasm.debugging();
self.onmessage = async (event) => {
// console.log("Worker received message");
// console.log(event);
const { command, beforeFont, location, afterFont } = event.data;
if (command == "axes") {
let obj = JSON.parse(wasm.axes(beforeFont, afterFont));
obj["type"] = "axes";
self.postMessage(obj);
} else if (command == "tables") {
wasm.diff_tables(beforeFont, afterFont, (tables) => {
self.postMessage({
type: "tables",
tables: JSON.parse(tables)["tables"],
});
});
} else if (command == "new_missing_glyphs") {
wasm.new_missing_glyphs(beforeFont, afterFont, (new_missing_glyphs) => {
self.postMessage({
type: "new_missing_glyphs",
new_missing_glyphs:
JSON.parse(new_missing_glyphs)["new_missing_glyphs"],
});
});
} else if (command == "modified_glyphs") {
wasm.modified_glyphs(beforeFont, afterFont, location, (glyphs) => {
self.postMessage({
type: "modified_glyphs",
modified_glyphs: JSON.parse(glyphs)["modified_glyphs"],
});
});
} else if (command == "words") {
wasm.diff_words(beforeFont, afterFont, location, (words) => {
self.postMessage({
type: "words",
words: JSON.parse(words)["words"],
});
});
}
return self;
};
return self;
}

init();

0 comments on commit 2afa5a8

Please sign in to comment.