@qrcommunication/gigapdf-lib 0.42.0 → 0.44.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
@@ -18,6 +18,17 @@ type Exports = {
18
18
  gp_free(ptr: number, len: number): void;
19
19
  [k: string]: any;
20
20
  };
21
+ /**
22
+ * A trained per-script OCR model bundled with the package. `alpha` covers
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
27
+ * {@link GigaPdfEngine.loadAllBundledOcrModels}.
28
+ */
29
+ type OcrScript = "alpha" | "arabic" | "devanagari" | "bengali" | "tamil";
30
+ /** Every trained script — load all for "recognize text in any language". */
31
+ declare const ALL_OCR_SCRIPTS: readonly OcrScript[];
21
32
  /** Loaded engine module. Create documents with {@link open} / {@link openEncrypted}. */
22
33
  declare class GigaPdfEngine {
23
34
  private readonly ex;
@@ -157,6 +168,41 @@ declare class GigaPdfEngine {
157
168
  decodeAvif(avif: Uint8Array): DecodedImage | null;
158
169
  /** Unpack a `[w:u32 LE][h:u32 LE][rgba]` decode buffer; `null` if empty. */
159
170
  _decodeFramed(bytes: Uint8Array, fn: (p: number, l: number, o: number) => number): DecodedImage | null;
171
+ /**
172
+ * Load a `.gpocr` line-level OCR model blob into the engine's runtime registry
173
+ * — Latin/Cyrillic/Greek (`alpha`), Arabic (RTL), Devanagari, Bengali, Tamil…
174
+ * The host supplies the bytes (the wasm ships no weights, like fonts), so the
175
+ * module stays lean. Once loaded, {@link GigaPdfDoc.ocr} / {@link GigaPdfDoc.ocrText}
176
+ * route to the CRNN+CTC line recognizer (per-page fallback to the built-in
177
+ * mono-glyph Latin classifier). Models are global to the engine; call after
178
+ * `load`/`loadDefault`. Returns `true` on success, `false` on a malformed blob.
179
+ */
180
+ loadOcrModel(model: Uint8Array): boolean;
181
+ /** Drop every runtime-loaded OCR model (reverts to the mono-glyph classifier). */
182
+ clearOcrModels(): void;
183
+ /**
184
+ * Resolve a bundled `.gpocr` model (shipped under `models/`) and load it into
185
+ * the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
186
+ * from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
187
+ * on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
188
+ * Bengali, Tamil) or to upgrade Latin/Cyrillic/Greek to the CRNN (`alpha`).
189
+ */
190
+ loadBundledOcrModel(script: OcrScript): Promise<boolean>;
191
+ /**
192
+ * Load several bundled OCR models (idempotent per call). Returns the scripts
193
+ * that loaded successfully. Pass {@link ALL_OCR_SCRIPTS} to recognize text in
194
+ * any supported language; the engine's script detector routes each line to the
195
+ * right model.
196
+ */
197
+ loadBundledOcrModels(scripts: readonly OcrScript[]): Promise<OcrScript[]>;
198
+ /**
199
+ * Discover and load EVERY bundled `.gpocr` model (Node hosts only). The
200
+ * engine's script detector then routes each recognized line to the matching
201
+ * model, so the OCR recognizes text in any shipped script — Latin, Cyrillic,
202
+ * Greek, Arabic, Urdu, Hebrew (the RTL group), Devanagari, Bengali, Tamil, and
203
+ * any model added later — with no code change. Returns the number loaded.
204
+ */
205
+ loadAllBundledOcrModels(): Promise<number>;
160
206
  fontCatalog(): FontInfo[];
161
207
  /** Google Fonts CSS2 URL for a family/weight/italic (host fetches it). */
162
208
  fontRequestUrl(family: string, weight?: number, italic?: boolean): string;
@@ -939,4 +985,4 @@ declare class GigaPdfDoc {
939
985
  }): boolean;
940
986
  }
941
987
 
942
- export { type AnnotationInfo, type Attachment, type Box, type DecodedImage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, GigaPdfDoc, GigaPdfEngine, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type NamedDest, type OcrWord, type OutlineEntry, type PageInfo, type PathSegment, type RadioOption, type SearchHit, type SignP12Options, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet };
988
+ export { ALL_OCR_SCRIPTS, type AnnotationInfo, type Attachment, type Box, type DecodedImage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, GigaPdfDoc, GigaPdfEngine, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type NamedDest, type OcrScript, type OcrWord, type OutlineEntry, type PageInfo, type PathSegment, type RadioOption, type SearchHit, type SignP12Options, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet };
package/dist/index.d.ts CHANGED
@@ -18,6 +18,17 @@ type Exports = {
18
18
  gp_free(ptr: number, len: number): void;
19
19
  [k: string]: any;
20
20
  };
21
+ /**
22
+ * A trained per-script OCR model bundled with the package. `alpha` covers
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
27
+ * {@link GigaPdfEngine.loadAllBundledOcrModels}.
28
+ */
29
+ type OcrScript = "alpha" | "arabic" | "devanagari" | "bengali" | "tamil";
30
+ /** Every trained script — load all for "recognize text in any language". */
31
+ declare const ALL_OCR_SCRIPTS: readonly OcrScript[];
21
32
  /** Loaded engine module. Create documents with {@link open} / {@link openEncrypted}. */
22
33
  declare class GigaPdfEngine {
23
34
  private readonly ex;
@@ -157,6 +168,41 @@ declare class GigaPdfEngine {
157
168
  decodeAvif(avif: Uint8Array): DecodedImage | null;
158
169
  /** Unpack a `[w:u32 LE][h:u32 LE][rgba]` decode buffer; `null` if empty. */
159
170
  _decodeFramed(bytes: Uint8Array, fn: (p: number, l: number, o: number) => number): DecodedImage | null;
171
+ /**
172
+ * Load a `.gpocr` line-level OCR model blob into the engine's runtime registry
173
+ * — Latin/Cyrillic/Greek (`alpha`), Arabic (RTL), Devanagari, Bengali, Tamil…
174
+ * The host supplies the bytes (the wasm ships no weights, like fonts), so the
175
+ * module stays lean. Once loaded, {@link GigaPdfDoc.ocr} / {@link GigaPdfDoc.ocrText}
176
+ * route to the CRNN+CTC line recognizer (per-page fallback to the built-in
177
+ * mono-glyph Latin classifier). Models are global to the engine; call after
178
+ * `load`/`loadDefault`. Returns `true` on success, `false` on a malformed blob.
179
+ */
180
+ loadOcrModel(model: Uint8Array): boolean;
181
+ /** Drop every runtime-loaded OCR model (reverts to the mono-glyph classifier). */
182
+ clearOcrModels(): void;
183
+ /**
184
+ * Resolve a bundled `.gpocr` model (shipped under `models/`) and load it into
185
+ * the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
186
+ * from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
187
+ * on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
188
+ * Bengali, Tamil) or to upgrade Latin/Cyrillic/Greek to the CRNN (`alpha`).
189
+ */
190
+ loadBundledOcrModel(script: OcrScript): Promise<boolean>;
191
+ /**
192
+ * Load several bundled OCR models (idempotent per call). Returns the scripts
193
+ * that loaded successfully. Pass {@link ALL_OCR_SCRIPTS} to recognize text in
194
+ * any supported language; the engine's script detector routes each line to the
195
+ * right model.
196
+ */
197
+ loadBundledOcrModels(scripts: readonly OcrScript[]): Promise<OcrScript[]>;
198
+ /**
199
+ * Discover and load EVERY bundled `.gpocr` model (Node hosts only). The
200
+ * engine's script detector then routes each recognized line to the matching
201
+ * model, so the OCR recognizes text in any shipped script — Latin, Cyrillic,
202
+ * Greek, Arabic, Urdu, Hebrew (the RTL group), Devanagari, Bengali, Tamil, and
203
+ * any model added later — with no code change. Returns the number loaded.
204
+ */
205
+ loadAllBundledOcrModels(): Promise<number>;
160
206
  fontCatalog(): FontInfo[];
161
207
  /** Google Fonts CSS2 URL for a family/weight/italic (host fetches it). */
162
208
  fontRequestUrl(family: string, weight?: number, italic?: boolean): string;
@@ -939,4 +985,4 @@ declare class GigaPdfDoc {
939
985
  }): boolean;
940
986
  }
941
987
 
942
- export { type AnnotationInfo, type Attachment, type Box, type DecodedImage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, GigaPdfDoc, GigaPdfEngine, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type NamedDest, type OcrWord, type OutlineEntry, type PageInfo, type PathSegment, type RadioOption, type SearchHit, type SignP12Options, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet };
988
+ export { ALL_OCR_SCRIPTS, type AnnotationInfo, type Attachment, type Box, type DecodedImage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, GigaPdfDoc, GigaPdfEngine, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type NamedDest, type OcrScript, type OcrWord, type OutlineEntry, type PageInfo, type PathSegment, type RadioOption, type SearchHit, type SignP12Options, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet };
package/dist/index.js CHANGED
@@ -1,6 +1,20 @@
1
1
  // src/index.ts
2
2
  var enc = new TextEncoder();
3
3
  var dec = new TextDecoder();
4
+ var OCR_MODEL_FILES = {
5
+ alpha: "ocr_alpha.gpocr",
6
+ arabic: "ocr_arabic.gpocr",
7
+ devanagari: "ocr_deva.gpocr",
8
+ bengali: "ocr_beng.gpocr",
9
+ tamil: "ocr_taml.gpocr"
10
+ };
11
+ var ALL_OCR_SCRIPTS = [
12
+ "alpha",
13
+ "arabic",
14
+ "devanagari",
15
+ "bengali",
16
+ "tamil"
17
+ ];
4
18
  var GigaPdfEngine = class _GigaPdfEngine {
5
19
  constructor(ex) {
6
20
  this.ex = ex;
@@ -51,8 +65,13 @@ var GigaPdfEngine = class _GigaPdfEngine {
51
65
  * for the consuming route so the `.wasm` is copied into the standalone bundle.
52
66
  */
53
67
  static async loadDefault() {
54
- const { readFile } = await import("fs/promises");
55
- const { fileURLToPath } = await import("url");
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");
56
75
  const wasmPath = fileURLToPath(new URL("../gigapdf.wasm", import.meta.url));
57
76
  return _GigaPdfEngine.load(await readFile(wasmPath));
58
77
  }
@@ -351,6 +370,87 @@ var GigaPdfEngine = class _GigaPdfEngine {
351
370
  const dv = new DataView(framed.buffer, framed.byteOffset, framed.byteLength);
352
371
  return { width: dv.getUint32(0, true), height: dv.getUint32(4, true), rgba: framed.subarray(8) };
353
372
  }
373
+ // ── OCR models (host-loaded `.gpocr` blobs — multi-script line recognizer) ─
374
+ /**
375
+ * Load a `.gpocr` line-level OCR model blob into the engine's runtime registry
376
+ * — Latin/Cyrillic/Greek (`alpha`), Arabic (RTL), Devanagari, Bengali, Tamil…
377
+ * The host supplies the bytes (the wasm ships no weights, like fonts), so the
378
+ * module stays lean. Once loaded, {@link GigaPdfDoc.ocr} / {@link GigaPdfDoc.ocrText}
379
+ * route to the CRNN+CTC line recognizer (per-page fallback to the built-in
380
+ * mono-glyph Latin classifier). Models are global to the engine; call after
381
+ * `load`/`loadDefault`. Returns `true` on success, `false` on a malformed blob.
382
+ */
383
+ loadOcrModel(model) {
384
+ return this._withBytes(model, (p, l) => this.ex.gp_ocr_load_model(p, l)) === 1;
385
+ }
386
+ /** Drop every runtime-loaded OCR model (reverts to the mono-glyph classifier). */
387
+ clearOcrModels() {
388
+ this.ex.gp_ocr_clear_models();
389
+ }
390
+ /**
391
+ * Resolve a bundled `.gpocr` model (shipped under `models/`) and load it into
392
+ * the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
393
+ * from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
394
+ * on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
395
+ * Bengali, Tamil) or to upgrade Latin/Cyrillic/Greek to the CRNN (`alpha`).
396
+ */
397
+ 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));
409
+ }
410
+ /**
411
+ * Load several bundled OCR models (idempotent per call). Returns the scripts
412
+ * that loaded successfully. Pass {@link ALL_OCR_SCRIPTS} to recognize text in
413
+ * any supported language; the engine's script detector routes each line to the
414
+ * right model.
415
+ */
416
+ async loadBundledOcrModels(scripts) {
417
+ const loaded = [];
418
+ for (const script of scripts) {
419
+ if (await this.loadBundledOcrModel(script)) loaded.push(script);
420
+ }
421
+ return loaded;
422
+ }
423
+ /**
424
+ * Discover and load EVERY bundled `.gpocr` model (Node hosts only). The
425
+ * engine's script detector then routes each recognized line to the matching
426
+ * model, so the OCR recognizes text in any shipped script — Latin, Cyrillic,
427
+ * Greek, Arabic, Urdu, Hebrew (the RTL group), Devanagari, Bengali, Tamil, and
428
+ * any model added later — with no code change. Returns the number loaded.
429
+ */
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
+ }
444
+ 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
+ );
450
+ if (this.loadOcrModel(bytes)) count += 1;
451
+ }
452
+ return count;
453
+ }
354
454
  // ── fonts (catalog / Google Fonts URL — the host performs the fetch) ───────
355
455
  fontCatalog() {
356
456
  return this._json((o) => this.ex.gp_font_catalog_json(o));
@@ -1514,6 +1614,7 @@ var GigaPdfDoc = class {
1514
1614
  }
1515
1615
  };
1516
1616
  export {
1617
+ ALL_OCR_SCRIPTS,
1517
1618
  GigaPdfDoc,
1518
1619
  GigaPdfEngine
1519
1620
  };