@qrcommunication/gigapdf-lib 0.52.5 → 0.54.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.cts CHANGED
@@ -21,12 +21,19 @@ type Exports = {
21
21
  /**
22
22
  * A trained per-script OCR model bundled with the package. `alpha` covers
23
23
  * Latin, Cyrillic and Greek; `arabic` is the **RTL group** (Arabic, Urdu,
24
- * Hebrew); the rest are dedicated scripts. The matching `.gpocr` CRNN blob
25
- * ships under `models/` and is host-loaded on demand. To recognize any shipped
26
- * language at once (including models added later), prefer
24
+ * Hebrew); `cjk` is Chinese (2401-class); `japanese` / `korean` are their own
25
+ * groups (kana+kanji / Hangul, each + full ASCII for mixed alphanumerics); the
26
+ * rest are dedicated Indic scripts. The matching `.gpocr` CRNN blob ships under
27
+ * `models/` and is host-loaded on demand. To recognize any shipped language at
28
+ * once (including models added later), prefer
27
29
  * {@link GigaPdfEngine.loadAllBundledOcrModels}.
30
+ *
31
+ * Some models may not be bundled in every release (a script can still be
32
+ * training upstream) — {@link GigaPdfEngine.loadBundledOcrModel} returns `false`
33
+ * for an absent blob instead of throwing, so listing them in
34
+ * {@link ALL_OCR_SCRIPTS} stays safe.
28
35
  */
29
- type OcrScript = "alpha" | "arabic" | "devanagari" | "bengali" | "tamil";
36
+ type OcrScript = "alpha" | "arabic" | "devanagari" | "bengali" | "tamil" | "cjk" | "japanese" | "korean";
30
37
  /** Every trained script — load all for "recognize text in any language". */
31
38
  declare const ALL_OCR_SCRIPTS: readonly OcrScript[];
32
39
  /** Loaded engine module. Create documents with {@link open} / {@link openEncrypted}. */
@@ -185,7 +192,10 @@ declare class GigaPdfEngine {
185
192
  * the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
186
193
  * from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
187
194
  * on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
188
- * Bengali, Tamil) or to upgrade Latin/Cyrillic/Greek to the CRNN (`alpha`).
195
+ * Bengali, Tamil, Chinese `cjk`, Japanese, Korean) or to upgrade
196
+ * Latin/Cyrillic/Greek to the CRNN (`alpha`). Returns `false` (rather than
197
+ * throwing) when the blob isn't bundled in this release — e.g. a script still
198
+ * training upstream — so loading a set stays resilient.
189
199
  */
190
200
  loadBundledOcrModel(script: OcrScript): Promise<boolean>;
191
201
  /**
package/dist/index.d.ts CHANGED
@@ -21,12 +21,19 @@ type Exports = {
21
21
  /**
22
22
  * A trained per-script OCR model bundled with the package. `alpha` covers
23
23
  * Latin, Cyrillic and Greek; `arabic` is the **RTL group** (Arabic, Urdu,
24
- * Hebrew); the rest are dedicated scripts. The matching `.gpocr` CRNN blob
25
- * ships under `models/` and is host-loaded on demand. To recognize any shipped
26
- * language at once (including models added later), prefer
24
+ * Hebrew); `cjk` is Chinese (2401-class); `japanese` / `korean` are their own
25
+ * groups (kana+kanji / Hangul, each + full ASCII for mixed alphanumerics); the
26
+ * rest are dedicated Indic scripts. The matching `.gpocr` CRNN blob ships under
27
+ * `models/` and is host-loaded on demand. To recognize any shipped language at
28
+ * once (including models added later), prefer
27
29
  * {@link GigaPdfEngine.loadAllBundledOcrModels}.
30
+ *
31
+ * Some models may not be bundled in every release (a script can still be
32
+ * training upstream) — {@link GigaPdfEngine.loadBundledOcrModel} returns `false`
33
+ * for an absent blob instead of throwing, so listing them in
34
+ * {@link ALL_OCR_SCRIPTS} stays safe.
28
35
  */
29
- type OcrScript = "alpha" | "arabic" | "devanagari" | "bengali" | "tamil";
36
+ type OcrScript = "alpha" | "arabic" | "devanagari" | "bengali" | "tamil" | "cjk" | "japanese" | "korean";
30
37
  /** Every trained script — load all for "recognize text in any language". */
31
38
  declare const ALL_OCR_SCRIPTS: readonly OcrScript[];
32
39
  /** Loaded engine module. Create documents with {@link open} / {@link openEncrypted}. */
@@ -185,7 +192,10 @@ declare class GigaPdfEngine {
185
192
  * the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
186
193
  * from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
187
194
  * on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
188
- * Bengali, Tamil) or to upgrade Latin/Cyrillic/Greek to the CRNN (`alpha`).
195
+ * Bengali, Tamil, Chinese `cjk`, Japanese, Korean) or to upgrade
196
+ * Latin/Cyrillic/Greek to the CRNN (`alpha`). Returns `false` (rather than
197
+ * throwing) when the blob isn't bundled in this release — e.g. a script still
198
+ * training upstream — so loading a set stays resilient.
189
199
  */
190
200
  loadBundledOcrModel(script: OcrScript): Promise<boolean>;
191
201
  /**
package/dist/index.js CHANGED
@@ -1,4 +1,9 @@
1
1
  // src/index.ts
2
+ import {
3
+ loadDefaultWasmBytes,
4
+ readModelFile,
5
+ readModelsDir
6
+ } from "./node-fs.js";
2
7
  var enc = new TextEncoder();
3
8
  var dec = new TextDecoder();
4
9
  var OCR_MODEL_FILES = {
@@ -6,14 +11,20 @@ var OCR_MODEL_FILES = {
6
11
  arabic: "ocr_arabic.gpocr",
7
12
  devanagari: "ocr_deva.gpocr",
8
13
  bengali: "ocr_beng.gpocr",
9
- tamil: "ocr_taml.gpocr"
14
+ tamil: "ocr_taml.gpocr",
15
+ cjk: "ocr_cjk.gpocr",
16
+ japanese: "ocr_jpn.gpocr",
17
+ korean: "ocr_kor.gpocr"
10
18
  };
11
19
  var ALL_OCR_SCRIPTS = [
12
20
  "alpha",
13
21
  "arabic",
14
22
  "devanagari",
15
23
  "bengali",
16
- "tamil"
24
+ "tamil",
25
+ "cjk",
26
+ "japanese",
27
+ "korean"
17
28
  ];
18
29
  var GigaPdfEngine = class _GigaPdfEngine {
19
30
  constructor(ex) {
@@ -65,15 +76,7 @@ var GigaPdfEngine = class _GigaPdfEngine {
65
76
  * for the consuming route so the `.wasm` is copied into the standalone bundle.
66
77
  */
67
78
  static async loadDefault() {
68
- const nodeImport = (m) => import(
69
- /* webpackIgnore: true */
70
- /* @vite-ignore */
71
- m
72
- );
73
- const { readFile } = await nodeImport("node:fs/promises");
74
- const { fileURLToPath } = await nodeImport("node:url");
75
- const wasmPath = fileURLToPath(new URL("../gigapdf.wasm", import.meta.url));
76
- return _GigaPdfEngine.load(await readFile(wasmPath));
79
+ return _GigaPdfEngine.load(await loadDefaultWasmBytes());
77
80
  }
78
81
  // ── linear-memory ABI helpers (internal) ─────────────────────────────────
79
82
  u8() {
@@ -392,20 +395,17 @@ var GigaPdfEngine = class _GigaPdfEngine {
392
395
  * the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
393
396
  * from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
394
397
  * on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
395
- * Bengali, Tamil) or to upgrade Latin/Cyrillic/Greek to the CRNN (`alpha`).
398
+ * Bengali, Tamil, Chinese `cjk`, Japanese, Korean) or to upgrade
399
+ * Latin/Cyrillic/Greek to the CRNN (`alpha`). Returns `false` (rather than
400
+ * throwing) when the blob isn't bundled in this release — e.g. a script still
401
+ * training upstream — so loading a set stays resilient.
396
402
  */
397
403
  async loadBundledOcrModel(script) {
398
- const nodeImport = (m) => import(
399
- /* webpackIgnore: true */
400
- /* @vite-ignore */
401
- m
402
- );
403
- const { readFile } = await nodeImport("node:fs/promises");
404
- const { fileURLToPath } = await nodeImport("node:url");
405
- const path = fileURLToPath(
406
- new URL(`../models/${OCR_MODEL_FILES[script]}`, import.meta.url)
407
- );
408
- return this.loadOcrModel(await readFile(path));
404
+ try {
405
+ return this.loadOcrModel(await readModelFile(OCR_MODEL_FILES[script]));
406
+ } catch {
407
+ return false;
408
+ }
409
409
  }
410
410
  /**
411
411
  * Load several bundled OCR models (idempotent per call). Returns the scripts
@@ -428,25 +428,9 @@ var GigaPdfEngine = class _GigaPdfEngine {
428
428
  * any model added later — with no code change. Returns the number loaded.
429
429
  */
430
430
  async loadAllBundledOcrModels() {
431
- const nodeImport = (m) => import(
432
- /* webpackIgnore: true */
433
- /* @vite-ignore */
434
- m
435
- );
436
- const { readFile, readdir } = await nodeImport("node:fs/promises");
437
- const { fileURLToPath } = await nodeImport("node:url");
438
- let entries;
439
- try {
440
- entries = await readdir(fileURLToPath(new URL("../models/", import.meta.url)));
441
- } catch {
442
- return 0;
443
- }
431
+ const models = await readModelsDir();
444
432
  let count = 0;
445
- for (const name of entries) {
446
- if (!name.endsWith(".gpocr")) continue;
447
- const bytes = await readFile(
448
- fileURLToPath(new URL(`../models/${name}`, import.meta.url))
449
- );
433
+ for (const { bytes } of models) {
450
434
  if (this.loadOcrModel(bytes)) count += 1;
451
435
  }
452
436
  return count;