@3sln/emon 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +113 -2
- package/THIRD_PARTY_NOTICES.md +4759 -0
- package/emon.wasm +0 -0
- package/package.json +34 -4
- package/src/index.d.ts +193 -0
- package/src/index.js +577 -0
- package/src/ocr.js +106 -0
- package/src/pdfium.d.ts +12 -0
- package/src/pdfium.js +287 -0
- package/src/render.js +55 -0
- package/src/sources.js +149 -0
- package/src/tesseract.d.ts +14 -0
- package/src/tesseract.js +114 -0
- package/src/wasm.js +111 -0
package/src/wasm.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// The WASM binding's wrapper (DEVLOG P9.3): the module's C ABI (`host/wasm/src/wasm.rs`) as methods. The module has no imports,
|
|
2
|
+
// so instantiating it needs nothing of the page: no filesystem, no network, no glue. Everything here is integers and byte ranges
|
|
3
|
+
// of the module's memory; `memory.buffer` is re-read after every call because a call may grow it.
|
|
4
|
+
|
|
5
|
+
export class Wasm {
|
|
6
|
+
/** @param {WebAssembly.Instance} instance */
|
|
7
|
+
constructor(instance) {
|
|
8
|
+
this.x = instance.exports;
|
|
9
|
+
this.kind = { render: this.x.emon_const(0), decode: this.x.emon_const(1), ocr: this.x.emon_const(2), bytes: this.x.emon_const(3), crop: this.x.emon_const(4), decodeImg: this.x.emon_const(5) };
|
|
10
|
+
this.status = { ok: this.x.emon_const(10), failed: this.x.emon_const(11), unsupported: this.x.emon_const(12), limit: this.x.emon_const(13) };
|
|
11
|
+
this.ocrWord = { dict: this.x.emon_const(14), numeric: this.x.emon_const(15) };
|
|
12
|
+
// the argument indices of a `render` / `crop` request (kin's `rd-*`; the record is `[len kind id arg...]`): read a request by these, never by a literal offset
|
|
13
|
+
this.rd = { input: this.x.emon_const(16), page: this.x.emon_const(17), dpi: this.x.emon_const(18), layer: this.x.emon_const(19), x: this.x.emon_const(20), y: this.x.emon_const(21), w: this.x.emon_const(22), h: this.x.emon_const(23) };
|
|
14
|
+
// kin's table of limits, by its kind: `emon_const(100 + K)` is the default of limit K (`input-bytes` is 0), so no default is typed here
|
|
15
|
+
this.inputBytes = this.x.emon_const(100);
|
|
16
|
+
// the kind (the row of kin's table of limits, `kin/limits.kin`) of `pixels`, the bound a renderer's bitmap keeps: `emon_limit(h, pixelsKind)` is the
|
|
17
|
+
// conversion's own (the default or its `limit.pixels`). A kind is a row index, not a default; `bb embedders` holds this to the table.
|
|
18
|
+
this.pixelsKind = 9;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Instantiate the module from bytes, a compiled module, a `Response` (streamed), a promise of one, or a URL.
|
|
23
|
+
* @param {ArrayBuffer|Uint8Array|WebAssembly.Module|Response|Promise<Response>|string|URL} src
|
|
24
|
+
*/
|
|
25
|
+
static async instantiate(src) {
|
|
26
|
+
if (typeof src === 'string' || src instanceof URL) src = fetch(src);
|
|
27
|
+
src = await src;
|
|
28
|
+
if (src instanceof WebAssembly.Module) return new Wasm(await WebAssembly.instantiate(src, {}));
|
|
29
|
+
if (typeof Response !== 'undefined' && src instanceof Response) {
|
|
30
|
+
if (WebAssembly.instantiateStreaming && (src.headers.get('content-type') || '').startsWith('application/wasm')) {
|
|
31
|
+
return new Wasm((await WebAssembly.instantiateStreaming(src, {})).instance);
|
|
32
|
+
}
|
|
33
|
+
src = await src.arrayBuffer();
|
|
34
|
+
}
|
|
35
|
+
return new Wasm((await WebAssembly.instantiate(src, {})).instance);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// THE OCR POLICY IS KIN'S (`emon.ocrport`, P10.7, D396): these ask the module what an `ocr` request means.
|
|
39
|
+
/** The argument index of the request record's first language. */
|
|
40
|
+
ocrLangFirst(rec) {
|
|
41
|
+
return this.withBytes(new Uint8Array(Int32Array.from(rec).buffer), (p) => this.x.emon_ocr_lang_first(p, rec.length));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Which of the `installed` model names read the request record: indices into `installed` (all of them when it names none). */
|
|
45
|
+
ocrModels(rec, installed) {
|
|
46
|
+
const names = [];
|
|
47
|
+
installed.forEach((s, i) => {
|
|
48
|
+
if (i) names.push(0);
|
|
49
|
+
for (const ch of s) names.push(ch.codePointAt(0));
|
|
50
|
+
});
|
|
51
|
+
const n = this.withBytes(new Uint8Array(Int32Array.from(rec).buffer), (p) =>
|
|
52
|
+
this.withBytes(new Uint8Array(Int32Array.from(names).buffer), (ip) => this.x.emon_ocr_models(p, rec.length, ip, names.length)),
|
|
53
|
+
);
|
|
54
|
+
const out = this.outI32();
|
|
55
|
+
this.outFree();
|
|
56
|
+
return n < 0 ? [] : Array.from(out);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The language a model read a word in, as a request packs it. */
|
|
60
|
+
ocrWordLang(model) {
|
|
61
|
+
const cps = Int32Array.from(Array.from(model, (c) => c.codePointAt(0)));
|
|
62
|
+
return this.withBytes(new Uint8Array(cps.buffer), (p) => this.x.emon_ocr_word_lang(p, cps.length));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A line's type (0 text, 1 table, 2 image, 3 rule, 4 other) from Tesseract's PolyBlockType. */
|
|
66
|
+
ocrBlockType(poly) {
|
|
67
|
+
return this.x.emon_ocr_block_type(poly | 0);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get mem() {
|
|
71
|
+
return new Uint8Array(this.x.memory.buffer);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Copy `u8` into the module's memory and run `f(ptr, len)`; the copy is freed after. */
|
|
75
|
+
withBytes(u8, f) {
|
|
76
|
+
const p = this.x.emon_alloc(u8.length);
|
|
77
|
+
try {
|
|
78
|
+
this.mem.set(u8, p);
|
|
79
|
+
return f(p, u8.length);
|
|
80
|
+
} finally {
|
|
81
|
+
this.x.emon_free(p, u8.length);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
withText(s, f) {
|
|
86
|
+
return this.withBytes(new TextEncoder().encode(s), f);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The shared output buffer, as a view of the module's memory (valid until the next call) */
|
|
90
|
+
outView() {
|
|
91
|
+
return new Uint8Array(this.x.memory.buffer, this.x.emon_out_ptr(), this.x.emon_out_len());
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The output as bytes in the JS heap (a copy). */
|
|
95
|
+
outBytes() {
|
|
96
|
+
return this.outView().slice();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
outText() {
|
|
100
|
+
return new TextDecoder().decode(this.outView());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
outI32() {
|
|
104
|
+
const v = this.outView();
|
|
105
|
+
return new Int32Array(v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
outFree() {
|
|
109
|
+
this.x.emon_out_free();
|
|
110
|
+
}
|
|
111
|
+
}
|