|
| 1 | +let wasm; |
| 2 | + |
| 3 | +{ |
| 4 | + const module = new WebAssembly.Module(await fetch('https://github.com/matmen/ImageScript/raw/deno/utils/wasm/font.wasm').then(r => r.arrayBuffer())); |
| 5 | + const instance = new WebAssembly.Instance(module); |
| 6 | + |
| 7 | + wasm = instance.exports; |
| 8 | +} |
| 9 | + |
| 10 | +let u8array_ref = new Uint8Array(wasm.memory.buffer); |
| 11 | +let i32array_ref = new Int32Array(wasm.memory.buffer); |
| 12 | +let u32array_ref = new Uint32Array(wasm.memory.buffer); |
| 13 | + |
| 14 | +const utf8encoder = new TextEncoder(); |
| 15 | + |
| 16 | +function u8array() { |
| 17 | + return u8array_ref.buffer === wasm.memory.buffer ? u8array_ref : (u8array_ref = new Uint8Array(wasm.memory.buffer)); |
| 18 | +} |
| 19 | + |
| 20 | +function i32array() { |
| 21 | + return i32array_ref.buffer === wasm.memory.buffer ? i32array_ref : (i32array_ref = new Int32Array(wasm.memory.buffer)); |
| 22 | +} |
| 23 | + |
| 24 | +function u32array() { |
| 25 | + return u32array_ref.buffer === wasm.memory.buffer ? u32array_ref : (u32array_ref = new Uint32Array(wasm.memory.buffer)); |
| 26 | +} |
| 27 | + |
| 28 | +function ptr_to_u8array(ptr, len) { |
| 29 | + return u8array().subarray(ptr, ptr + len); |
| 30 | +} |
| 31 | + |
| 32 | +function ptr_to_u32array(ptr, len) { |
| 33 | + return u32array().subarray(ptr / 4, ptr / 4 + len); |
| 34 | +} |
| 35 | + |
| 36 | +function u8array_to_ptr(buffer) { |
| 37 | + const ptr = wasm.__wbindgen_malloc(buffer.length); |
| 38 | + u8array().set(buffer, ptr); |
| 39 | + |
| 40 | + return ptr; |
| 41 | +} |
| 42 | + |
| 43 | +function string_to_ptr(string) { |
| 44 | + let offset = 0; |
| 45 | + let len = string.length; |
| 46 | + let ptr = wasm.__wbindgen_malloc(string.length); |
| 47 | + |
| 48 | + const u8 = u8array(); |
| 49 | + while (len > offset) { |
| 50 | + const code = string.charCodeAt(offset); |
| 51 | + |
| 52 | + if (code > 0x7F) break; |
| 53 | + u8[ptr + offset++] = code; |
| 54 | + } |
| 55 | + |
| 56 | + if (offset !== len) { |
| 57 | + if (offset !== 0) string = string.substring(offset); |
| 58 | + ptr = wasm.__wbindgen_realloc(ptr, len, len = offset + string.length * 3); |
| 59 | + const ret = utf8encoder.encodeInto(string, u8array().subarray(ptr + offset, ptr + len)); |
| 60 | + |
| 61 | + offset += ret.written; |
| 62 | + } |
| 63 | + |
| 64 | + return [ptr, offset]; |
| 65 | +} |
| 66 | + |
| 67 | +const nullish = x => x == null; |
| 68 | + |
| 69 | +export function render(ptr, id, scale, r, g, b, text, max_width, wrap_style = false) { |
| 70 | + const str = string_to_ptr(text); |
| 71 | + wasm.render(ptr, id, scale, r, g, b, str[0], str[1], !nullish(max_width), max_width || 0, wrap_style); |
| 72 | +} |
| 73 | + |
| 74 | +export function buffer(id) { |
| 75 | + wasm.buffer(8, id); |
| 76 | + const i32 = i32array(); |
| 77 | + const slice = ptr_to_u8array(i32[2], i32[3]).slice(); |
| 78 | + wasm.__wbindgen_free(i32[2], i32[3]); |
| 79 | + |
| 80 | + return slice; |
| 81 | +} |
| 82 | + |
| 83 | +export function meta(id) { |
| 84 | + wasm.meta(8, id); |
| 85 | + const i32 = i32array(); |
| 86 | + const slice = ptr_to_u32array(i32[2], i32[3]).slice(); |
| 87 | + wasm.__wbindgen_free(i32[2], 4 * i32[3]); |
| 88 | + |
| 89 | + return slice; |
| 90 | +} |
| 91 | + |
| 92 | +export async function load(id, buffer, scale = 128) { |
| 93 | + wasm = await wasm; |
| 94 | + wasm.load(id, u8array_to_ptr(buffer), buffer.length, scale); |
| 95 | +} |
| 96 | + |
| 97 | +export function free(id) { |
| 98 | + wasm.free(id); |
| 99 | +} |
0 commit comments