@forgesworn/qr
Version:
Dependency-free QR code encoder and decoder for TypeScript, browsers, workers and Node, with optional WebAssembly and tracked camera scanning.
28 lines (27 loc) • 1.34 kB
JavaScript
import { fromEci, hanziText, kanjiText, looksShiftJis } from './text.js';
/** ABI 2 text import. Called during segment parsing, before a symbol is accepted. */
export function textImports(memory) {
const encoder = new TextEncoder();
return {
qr_text: {
decode(kind, value, pointer, length, output, capacity) {
// Rust may have grown memory while allocating the destination. Views never
// survive this call and the callback never calls back into the module.
const buffer = memory().buffer;
const bytes = new Uint8Array(buffer, pointer >>> 0, length >>> 0);
const text = kind === 0 ? fromEci(value, bytes)
: kind === 1 ? kanjiText(value)
: kind === 2 ? hanziText(value >>> 8, value & 255)
: kind === 3 && looksShiftJis(bytes) ? fromEci(20, bytes)
: undefined;
if (text === undefined)
return -1;
const encoded = encoder.encode(text);
if (encoded.length > (capacity >>> 0))
return -1;
new Uint8Array(buffer, output >>> 0, capacity >>> 0).set(encoded);
return encoded.length;
},
},
};
}