@qrcommunication/gigapdf-lib 0.41.0 → 0.43.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/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  /**
2
- * @qrcommunication/gigapdf-lib — TypeScript SDK for the zero-dependency Rust→WASM
3
- * PDF engine (gigapdf-lib). Wraps the flat `extern "C"` `gp_*` ABI behind a typed,
4
- * ergonomic class. No third-party runtime deps; the `.wasm` is self-contained.
2
+ * @qrcommunication/gigapdf-lib — TypeScript SDK for the gigapdf-lib Rust→WASM PDF
3
+ * engine: no third-party PDF/Office/image library (cryptography uses RustCrypto,
4
+ * JavaScript uses Boa). Wraps the flat `extern "C"` `gp_*` ABI behind a typed,
5
+ * ergonomic class. No third-party npm deps; the `.wasm` imports a single host
6
+ * function — `env.gp_host_random` (entropy), which `load()` supplies.
5
7
  *
6
8
  * Usage:
7
9
  * const giga = await GigaPdfEngine.load(wasmBytesOrUrl);
@@ -155,6 +157,18 @@ declare class GigaPdfEngine {
155
157
  decodeAvif(avif: Uint8Array): DecodedImage | null;
156
158
  /** Unpack a `[w:u32 LE][h:u32 LE][rgba]` decode buffer; `null` if empty. */
157
159
  _decodeFramed(bytes: Uint8Array, fn: (p: number, l: number, o: number) => number): DecodedImage | null;
160
+ /**
161
+ * Load a `.gpocr` line-level OCR model blob into the engine's runtime registry
162
+ * — Latin/Cyrillic/Greek (`alpha`), Arabic (RTL), Devanagari, Bengali, Tamil…
163
+ * The host supplies the bytes (the wasm ships no weights, like fonts), so the
164
+ * module stays lean. Once loaded, {@link GigaPdfDoc.ocr} / {@link GigaPdfDoc.ocrText}
165
+ * route to the CRNN+CTC line recognizer (per-page fallback to the built-in
166
+ * mono-glyph Latin classifier). Models are global to the engine; call after
167
+ * `load`/`loadDefault`. Returns `true` on success, `false` on a malformed blob.
168
+ */
169
+ loadOcrModel(model: Uint8Array): boolean;
170
+ /** Drop every runtime-loaded OCR model (reverts to the mono-glyph classifier). */
171
+ clearOcrModels(): void;
158
172
  fontCatalog(): FontInfo[];
159
173
  /** Google Fonts CSS2 URL for a family/weight/italic (host fetches it). */
160
174
  fontRequestUrl(family: string, weight?: number, italic?: boolean): string;
package/dist/index.js CHANGED
@@ -19,11 +19,28 @@ var GigaPdfEngine = class _GigaPdfEngine {
19
19
  } else {
20
20
  bytes = source;
21
21
  }
22
+ let ex;
22
23
  const { instance } = await WebAssembly.instantiate(
23
24
  bytes instanceof Uint8Array ? bytes.slice().buffer : bytes,
24
- {}
25
+ {
26
+ env: {
27
+ gp_host_random: (ptr, len) => {
28
+ const c = globalThis.crypto;
29
+ if (!c?.getRandomValues) {
30
+ throw new Error(
31
+ "gigapdf: wasm entropy needs Web Crypto (globalThis.crypto.getRandomValues)"
32
+ );
33
+ }
34
+ const mem = new Uint8Array(ex.memory.buffer, ptr, len);
35
+ for (let off = 0; off < len; off += 65536) {
36
+ c.getRandomValues(mem.subarray(off, Math.min(off + 65536, len)));
37
+ }
38
+ }
39
+ }
40
+ }
25
41
  );
26
- return new _GigaPdfEngine(instance.exports);
42
+ ex = instance.exports;
43
+ return new _GigaPdfEngine(ex);
27
44
  }
28
45
  /**
29
46
  * Convenience loader for Node: instantiate from the `gigapdf.wasm` shipped
@@ -34,8 +51,13 @@ var GigaPdfEngine = class _GigaPdfEngine {
34
51
  * for the consuming route so the `.wasm` is copied into the standalone bundle.
35
52
  */
36
53
  static async loadDefault() {
37
- const { readFile } = await import("fs/promises");
38
- const { fileURLToPath } = await import("url");
54
+ const nodeImport = (m) => import(
55
+ /* webpackIgnore: true */
56
+ /* @vite-ignore */
57
+ m
58
+ );
59
+ const { readFile } = await nodeImport("node:fs/promises");
60
+ const { fileURLToPath } = await nodeImport("node:url");
39
61
  const wasmPath = fileURLToPath(new URL("../gigapdf.wasm", import.meta.url));
40
62
  return _GigaPdfEngine.load(await readFile(wasmPath));
41
63
  }
@@ -334,6 +356,23 @@ var GigaPdfEngine = class _GigaPdfEngine {
334
356
  const dv = new DataView(framed.buffer, framed.byteOffset, framed.byteLength);
335
357
  return { width: dv.getUint32(0, true), height: dv.getUint32(4, true), rgba: framed.subarray(8) };
336
358
  }
359
+ // ── OCR models (host-loaded `.gpocr` blobs — multi-script line recognizer) ─
360
+ /**
361
+ * Load a `.gpocr` line-level OCR model blob into the engine's runtime registry
362
+ * — Latin/Cyrillic/Greek (`alpha`), Arabic (RTL), Devanagari, Bengali, Tamil…
363
+ * The host supplies the bytes (the wasm ships no weights, like fonts), so the
364
+ * module stays lean. Once loaded, {@link GigaPdfDoc.ocr} / {@link GigaPdfDoc.ocrText}
365
+ * route to the CRNN+CTC line recognizer (per-page fallback to the built-in
366
+ * mono-glyph Latin classifier). Models are global to the engine; call after
367
+ * `load`/`loadDefault`. Returns `true` on success, `false` on a malformed blob.
368
+ */
369
+ loadOcrModel(model) {
370
+ return this._withBytes(model, (p, l) => this.ex.gp_ocr_load_model(p, l)) === 1;
371
+ }
372
+ /** Drop every runtime-loaded OCR model (reverts to the mono-glyph classifier). */
373
+ clearOcrModels() {
374
+ this.ex.gp_ocr_clear_models();
375
+ }
337
376
  // ── fonts (catalog / Google Fonts URL — the host performs the fetch) ───────
338
377
  fontCatalog() {
339
378
  return this._json((o) => this.ex.gp_font_catalog_json(o));