@qrcommunication/gigapdf-lib 0.63.0 → 0.64.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/CHANGELOG.md +38 -0
- package/dist/index.cjs +34 -119
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +18 -63
- package/dist/index.d.ts +18 -63
- package/dist/index.js +35 -123
- package/dist/index.js.map +1 -1
- package/dist/node-fs.browser.cjs +3 -13
- package/dist/node-fs.browser.cjs.map +1 -1
- package/dist/node-fs.browser.d.cts +3 -10
- package/dist/node-fs.browser.d.ts +3 -10
- package/dist/node-fs.browser.js +2 -10
- package/dist/node-fs.browser.js.map +1 -1
- package/dist/node-fs.cjs +2 -37
- package/dist/node-fs.cjs.map +1 -1
- package/dist/node-fs.d.cts +1 -11
- package/dist/node-fs.d.ts +1 -11
- package/dist/node-fs.js +1 -34
- package/dist/node-fs.js.map +1 -1
- package/gigapdf.wasm +0 -0
- package/package.json +1 -1
- package/models/ocr_alpha.gpocr +0 -0
- package/models/ocr_alpha_big.gpocr +0 -0
- package/models/ocr_alpha_hw.gpocr +0 -0
- package/models/ocr_alpha_photo.gpocr +0 -0
- package/models/ocr_alpha_print.gpocr +0 -0
- package/models/ocr_arabic.gpocr +0 -0
- package/models/ocr_arabic_small.gpocr +0 -0
- package/models/ocr_beng.gpocr +0 -0
- package/models/ocr_beng_small.gpocr +0 -0
- package/models/ocr_cjk.gpocr +0 -0
- package/models/ocr_deva.gpocr +0 -0
- package/models/ocr_deva_small.gpocr +0 -0
- package/models/ocr_jpn.gpocr +0 -0
- package/models/ocr_kann.gpocr +0 -0
- package/models/ocr_kor.gpocr +0 -0
- package/models/ocr_mlym.gpocr +0 -0
- package/models/ocr_taml.gpocr +0 -0
- package/models/ocr_taml_small.gpocr +0 -0
- package/models/ocr_telu.gpocr +0 -0
- package/models/ocr_thai.gpocr +0 -0
package/dist/index.d.cts
CHANGED
|
@@ -18,24 +18,6 @@ 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); `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
|
|
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.
|
|
35
|
-
*/
|
|
36
|
-
type OcrScript = "alpha" | "arabic" | "devanagari" | "bengali" | "tamil" | "cjk" | "japanese" | "korean" | "thai" | "telugu" | "kannada" | "malayalam" | "gujarati" | "gurmukhi" | "oriya" | "sinhala" | "georgian" | "armenian" | "khmer" | "lao" | "myanmar" | "amharic";
|
|
37
|
-
/** Every trained script — load all for "recognize text in any language". */
|
|
38
|
-
declare const ALL_OCR_SCRIPTS: readonly OcrScript[];
|
|
39
21
|
/** Loaded engine module. Create documents with {@link open} / {@link openEncrypted}. */
|
|
40
22
|
declare class GigaPdfEngine {
|
|
41
23
|
private readonly ex;
|
|
@@ -62,6 +44,14 @@ declare class GigaPdfEngine {
|
|
|
62
44
|
_buffer(call: (outLenPtr: number) => number): Uint8Array;
|
|
63
45
|
_str(call: (outLenPtr: number) => number): string;
|
|
64
46
|
_json<T = unknown>(call: (outLenPtr: number) => number): T;
|
|
47
|
+
/**
|
|
48
|
+
* Like {@link _json} but distinguishes a **null** result (the export returned a
|
|
49
|
+
* null pointer — e.g. an unrecognized container) from a present-but-empty JSON
|
|
50
|
+
* payload (`[]` / `{}`). Returns `null` only for the null pointer; otherwise the
|
|
51
|
+
* parsed JSON. Used where the Rust side is `Option<…>` and "nothing" and
|
|
52
|
+
* "unrecognized" must stay distinct.
|
|
53
|
+
*/
|
|
54
|
+
_jsonOrNull<T = unknown>(call: (outLenPtr: number) => number): T | null;
|
|
65
55
|
/**
|
|
66
56
|
* Decode standard Base64 (RFC 4648) to bytes. Pure-JS table decode, so it
|
|
67
57
|
* works identically in Node and the browser with no dependency (used to turn
|
|
@@ -104,6 +94,15 @@ declare class GigaPdfEngine {
|
|
|
104
94
|
rtfToPdf(rtf: string): Uint8Array;
|
|
105
95
|
/** Office (DOCX/ODT/PPTX/XLSX/ODS) → PDF, auto-detected. Empty array if unrecognized. */
|
|
106
96
|
officeToPdf(office: Uint8Array): Uint8Array;
|
|
97
|
+
/**
|
|
98
|
+
* Phase 1 for {@link officeToPdf} — the Google/system fonts an Office container
|
|
99
|
+
* **references but doesn't embed**. Download each `url` (→ TTF) and supply the
|
|
100
|
+
* bytes back to the host font cache so {@link officeToPdf}'s styled runs lay out
|
|
101
|
+
* with the right metrics. Faces the container embeds itself are de-obfuscated
|
|
102
|
+
* and used automatically (not listed here). Returns `null` if the bytes are not
|
|
103
|
+
* a recognized Office container, `[]` if it needs no host fonts.
|
|
104
|
+
*/
|
|
105
|
+
officeNeededFonts(office: Uint8Array): HtmlFontRequest[] | null;
|
|
107
106
|
/**
|
|
108
107
|
* Image (PNG/JPEG/GIF/WebP/AVIF) → one-page PDF, format auto-detected: the
|
|
109
108
|
* image is centred and scaled to fit on an A4 portrait page. PNG/JPEG embed
|
|
@@ -192,44 +191,6 @@ declare class GigaPdfEngine {
|
|
|
192
191
|
decodeAvif(avif: Uint8Array): DecodedImage | null;
|
|
193
192
|
/** Unpack a `[w:u32 LE][h:u32 LE][rgba]` decode buffer; `null` if empty. */
|
|
194
193
|
_decodeFramed(bytes: Uint8Array, fn: (p: number, l: number, o: number) => number): DecodedImage | null;
|
|
195
|
-
/**
|
|
196
|
-
* Load a `.gpocr` line-level OCR model blob into the engine's runtime registry
|
|
197
|
-
* — Latin/Cyrillic/Greek (`alpha`), Arabic (RTL), Devanagari, Bengali, Tamil…
|
|
198
|
-
* The host supplies the bytes (the wasm ships no weights, like fonts), so the
|
|
199
|
-
* module stays lean. Once loaded, {@link GigaPdfDoc.ocr} / {@link GigaPdfDoc.ocrText}
|
|
200
|
-
* route to the CRNN+CTC line recognizer (per-page fallback to the built-in
|
|
201
|
-
* mono-glyph Latin classifier). Models are global to the engine; call after
|
|
202
|
-
* `load`/`loadDefault`. Returns `true` on success, `false` on a malformed blob.
|
|
203
|
-
*/
|
|
204
|
-
loadOcrModel(model: Uint8Array): boolean;
|
|
205
|
-
/** Drop every runtime-loaded OCR model (reverts to the mono-glyph classifier). */
|
|
206
|
-
clearOcrModels(): void;
|
|
207
|
-
/**
|
|
208
|
-
* Resolve a bundled `.gpocr` model (shipped under `models/`) and load it into
|
|
209
|
-
* the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
|
|
210
|
-
* from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
|
|
211
|
-
* on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
|
|
212
|
-
* Bengali, Tamil, Chinese `cjk`, Japanese, Korean) or to upgrade
|
|
213
|
-
* Latin/Cyrillic/Greek to the CRNN (`alpha`). Returns `false` (rather than
|
|
214
|
-
* throwing) when the blob isn't bundled in this release — e.g. a script still
|
|
215
|
-
* training upstream — so loading a set stays resilient.
|
|
216
|
-
*/
|
|
217
|
-
loadBundledOcrModel(script: OcrScript): Promise<boolean>;
|
|
218
|
-
/**
|
|
219
|
-
* Load several bundled OCR models (idempotent per call). Returns the scripts
|
|
220
|
-
* that loaded successfully. Pass {@link ALL_OCR_SCRIPTS} to recognize text in
|
|
221
|
-
* any supported language; the engine's script detector routes each line to the
|
|
222
|
-
* right model.
|
|
223
|
-
*/
|
|
224
|
-
loadBundledOcrModels(scripts: readonly OcrScript[]): Promise<OcrScript[]>;
|
|
225
|
-
/**
|
|
226
|
-
* Discover and load EVERY bundled `.gpocr` model (Node hosts only). The
|
|
227
|
-
* engine's script detector then routes each recognized line to the matching
|
|
228
|
-
* model, so the OCR recognizes text in any shipped script — Latin, Cyrillic,
|
|
229
|
-
* Greek, Arabic, Urdu, Hebrew (the RTL group), Devanagari, Bengali, Tamil, and
|
|
230
|
-
* any model added later — with no code change. Returns the number loaded.
|
|
231
|
-
*/
|
|
232
|
-
loadAllBundledOcrModels(): Promise<number>;
|
|
233
194
|
fontCatalog(): FontInfo[];
|
|
234
195
|
/** Google Fonts CSS2 URL for a family/weight/italic (host fetches it). */
|
|
235
196
|
fontRequestUrl(family: string, weight?: number, italic?: boolean): string;
|
|
@@ -750,9 +711,6 @@ interface SearchHit extends Box {
|
|
|
750
711
|
page: number;
|
|
751
712
|
text: string;
|
|
752
713
|
}
|
|
753
|
-
interface OcrWord extends Box {
|
|
754
|
-
text: string;
|
|
755
|
-
}
|
|
756
714
|
interface TextRunInfo {
|
|
757
715
|
index: number;
|
|
758
716
|
operator: string;
|
|
@@ -1037,9 +995,6 @@ declare class GigaPdfDoc {
|
|
|
1037
995
|
*/
|
|
1038
996
|
pageBlocks(page: number): GigaBlock[];
|
|
1039
997
|
search(query: string, caseInsensitive?: boolean): SearchHit[];
|
|
1040
|
-
/** OCR a (scanned) page → words with PDF-space boxes. `scale` ≥ 2 for small text. */
|
|
1041
|
-
ocr(page: number, scale?: number): OcrWord[];
|
|
1042
|
-
ocrText(page: number, scale?: number): string;
|
|
1043
998
|
/**
|
|
1044
999
|
* Replace text run `index` on `page` with `text`. Font-aware: works with
|
|
1045
1000
|
* **any** font — a run set in an embedded Type0/Identity-H face (TrueType or
|
|
@@ -1513,4 +1468,4 @@ declare class GigaPdfDoc {
|
|
|
1513
1468
|
}): boolean;
|
|
1514
1469
|
}
|
|
1515
1470
|
|
|
1516
|
-
export {
|
|
1471
|
+
export { type AnnotationInfo, type Attachment, type Box, type DecodedImage, type DocumentLanguage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, type GigaBlock, type GigaBlockAddr, type GigaBlockKind, type GigaCellValue, type GigaCharStyle, type GigaDocument, type GigaGeneric, type GigaInline, type GigaPage, GigaPdfDoc, GigaPdfEngine, type GigaSection, type GigaStylePatch, type HeaderFooterAlign, type HeaderFooterSpec, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type ModelOp, type NamedDest, type OutlineEntry, type PageInfo, type PageMargins, 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,24 +18,6 @@ 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); `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
|
|
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.
|
|
35
|
-
*/
|
|
36
|
-
type OcrScript = "alpha" | "arabic" | "devanagari" | "bengali" | "tamil" | "cjk" | "japanese" | "korean" | "thai" | "telugu" | "kannada" | "malayalam" | "gujarati" | "gurmukhi" | "oriya" | "sinhala" | "georgian" | "armenian" | "khmer" | "lao" | "myanmar" | "amharic";
|
|
37
|
-
/** Every trained script — load all for "recognize text in any language". */
|
|
38
|
-
declare const ALL_OCR_SCRIPTS: readonly OcrScript[];
|
|
39
21
|
/** Loaded engine module. Create documents with {@link open} / {@link openEncrypted}. */
|
|
40
22
|
declare class GigaPdfEngine {
|
|
41
23
|
private readonly ex;
|
|
@@ -62,6 +44,14 @@ declare class GigaPdfEngine {
|
|
|
62
44
|
_buffer(call: (outLenPtr: number) => number): Uint8Array;
|
|
63
45
|
_str(call: (outLenPtr: number) => number): string;
|
|
64
46
|
_json<T = unknown>(call: (outLenPtr: number) => number): T;
|
|
47
|
+
/**
|
|
48
|
+
* Like {@link _json} but distinguishes a **null** result (the export returned a
|
|
49
|
+
* null pointer — e.g. an unrecognized container) from a present-but-empty JSON
|
|
50
|
+
* payload (`[]` / `{}`). Returns `null` only for the null pointer; otherwise the
|
|
51
|
+
* parsed JSON. Used where the Rust side is `Option<…>` and "nothing" and
|
|
52
|
+
* "unrecognized" must stay distinct.
|
|
53
|
+
*/
|
|
54
|
+
_jsonOrNull<T = unknown>(call: (outLenPtr: number) => number): T | null;
|
|
65
55
|
/**
|
|
66
56
|
* Decode standard Base64 (RFC 4648) to bytes. Pure-JS table decode, so it
|
|
67
57
|
* works identically in Node and the browser with no dependency (used to turn
|
|
@@ -104,6 +94,15 @@ declare class GigaPdfEngine {
|
|
|
104
94
|
rtfToPdf(rtf: string): Uint8Array;
|
|
105
95
|
/** Office (DOCX/ODT/PPTX/XLSX/ODS) → PDF, auto-detected. Empty array if unrecognized. */
|
|
106
96
|
officeToPdf(office: Uint8Array): Uint8Array;
|
|
97
|
+
/**
|
|
98
|
+
* Phase 1 for {@link officeToPdf} — the Google/system fonts an Office container
|
|
99
|
+
* **references but doesn't embed**. Download each `url` (→ TTF) and supply the
|
|
100
|
+
* bytes back to the host font cache so {@link officeToPdf}'s styled runs lay out
|
|
101
|
+
* with the right metrics. Faces the container embeds itself are de-obfuscated
|
|
102
|
+
* and used automatically (not listed here). Returns `null` if the bytes are not
|
|
103
|
+
* a recognized Office container, `[]` if it needs no host fonts.
|
|
104
|
+
*/
|
|
105
|
+
officeNeededFonts(office: Uint8Array): HtmlFontRequest[] | null;
|
|
107
106
|
/**
|
|
108
107
|
* Image (PNG/JPEG/GIF/WebP/AVIF) → one-page PDF, format auto-detected: the
|
|
109
108
|
* image is centred and scaled to fit on an A4 portrait page. PNG/JPEG embed
|
|
@@ -192,44 +191,6 @@ declare class GigaPdfEngine {
|
|
|
192
191
|
decodeAvif(avif: Uint8Array): DecodedImage | null;
|
|
193
192
|
/** Unpack a `[w:u32 LE][h:u32 LE][rgba]` decode buffer; `null` if empty. */
|
|
194
193
|
_decodeFramed(bytes: Uint8Array, fn: (p: number, l: number, o: number) => number): DecodedImage | null;
|
|
195
|
-
/**
|
|
196
|
-
* Load a `.gpocr` line-level OCR model blob into the engine's runtime registry
|
|
197
|
-
* — Latin/Cyrillic/Greek (`alpha`), Arabic (RTL), Devanagari, Bengali, Tamil…
|
|
198
|
-
* The host supplies the bytes (the wasm ships no weights, like fonts), so the
|
|
199
|
-
* module stays lean. Once loaded, {@link GigaPdfDoc.ocr} / {@link GigaPdfDoc.ocrText}
|
|
200
|
-
* route to the CRNN+CTC line recognizer (per-page fallback to the built-in
|
|
201
|
-
* mono-glyph Latin classifier). Models are global to the engine; call after
|
|
202
|
-
* `load`/`loadDefault`. Returns `true` on success, `false` on a malformed blob.
|
|
203
|
-
*/
|
|
204
|
-
loadOcrModel(model: Uint8Array): boolean;
|
|
205
|
-
/** Drop every runtime-loaded OCR model (reverts to the mono-glyph classifier). */
|
|
206
|
-
clearOcrModels(): void;
|
|
207
|
-
/**
|
|
208
|
-
* Resolve a bundled `.gpocr` model (shipped under `models/`) and load it into
|
|
209
|
-
* the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
|
|
210
|
-
* from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
|
|
211
|
-
* on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
|
|
212
|
-
* Bengali, Tamil, Chinese `cjk`, Japanese, Korean) or to upgrade
|
|
213
|
-
* Latin/Cyrillic/Greek to the CRNN (`alpha`). Returns `false` (rather than
|
|
214
|
-
* throwing) when the blob isn't bundled in this release — e.g. a script still
|
|
215
|
-
* training upstream — so loading a set stays resilient.
|
|
216
|
-
*/
|
|
217
|
-
loadBundledOcrModel(script: OcrScript): Promise<boolean>;
|
|
218
|
-
/**
|
|
219
|
-
* Load several bundled OCR models (idempotent per call). Returns the scripts
|
|
220
|
-
* that loaded successfully. Pass {@link ALL_OCR_SCRIPTS} to recognize text in
|
|
221
|
-
* any supported language; the engine's script detector routes each line to the
|
|
222
|
-
* right model.
|
|
223
|
-
*/
|
|
224
|
-
loadBundledOcrModels(scripts: readonly OcrScript[]): Promise<OcrScript[]>;
|
|
225
|
-
/**
|
|
226
|
-
* Discover and load EVERY bundled `.gpocr` model (Node hosts only). The
|
|
227
|
-
* engine's script detector then routes each recognized line to the matching
|
|
228
|
-
* model, so the OCR recognizes text in any shipped script — Latin, Cyrillic,
|
|
229
|
-
* Greek, Arabic, Urdu, Hebrew (the RTL group), Devanagari, Bengali, Tamil, and
|
|
230
|
-
* any model added later — with no code change. Returns the number loaded.
|
|
231
|
-
*/
|
|
232
|
-
loadAllBundledOcrModels(): Promise<number>;
|
|
233
194
|
fontCatalog(): FontInfo[];
|
|
234
195
|
/** Google Fonts CSS2 URL for a family/weight/italic (host fetches it). */
|
|
235
196
|
fontRequestUrl(family: string, weight?: number, italic?: boolean): string;
|
|
@@ -750,9 +711,6 @@ interface SearchHit extends Box {
|
|
|
750
711
|
page: number;
|
|
751
712
|
text: string;
|
|
752
713
|
}
|
|
753
|
-
interface OcrWord extends Box {
|
|
754
|
-
text: string;
|
|
755
|
-
}
|
|
756
714
|
interface TextRunInfo {
|
|
757
715
|
index: number;
|
|
758
716
|
operator: string;
|
|
@@ -1037,9 +995,6 @@ declare class GigaPdfDoc {
|
|
|
1037
995
|
*/
|
|
1038
996
|
pageBlocks(page: number): GigaBlock[];
|
|
1039
997
|
search(query: string, caseInsensitive?: boolean): SearchHit[];
|
|
1040
|
-
/** OCR a (scanned) page → words with PDF-space boxes. `scale` ≥ 2 for small text. */
|
|
1041
|
-
ocr(page: number, scale?: number): OcrWord[];
|
|
1042
|
-
ocrText(page: number, scale?: number): string;
|
|
1043
998
|
/**
|
|
1044
999
|
* Replace text run `index` on `page` with `text`. Font-aware: works with
|
|
1045
1000
|
* **any** font — a run set in an embedded Type0/Identity-H face (TrueType or
|
|
@@ -1513,4 +1468,4 @@ declare class GigaPdfDoc {
|
|
|
1513
1468
|
}): boolean;
|
|
1514
1469
|
}
|
|
1515
1470
|
|
|
1516
|
-
export {
|
|
1471
|
+
export { type AnnotationInfo, type Attachment, type Box, type DecodedImage, type DocumentLanguage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, type GigaBlock, type GigaBlockAddr, type GigaBlockKind, type GigaCellValue, type GigaCharStyle, type GigaDocument, type GigaGeneric, type GigaInline, type GigaPage, GigaPdfDoc, GigaPdfEngine, type GigaSection, type GigaStylePatch, type HeaderFooterAlign, type HeaderFooterSpec, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type ModelOp, type NamedDest, type OutlineEntry, type PageInfo, type PageMargins, type PathSegment, type RadioOption, type SearchHit, type SignP12Options, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet };
|
package/dist/index.js
CHANGED
|
@@ -1,59 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import {
|
|
3
|
-
loadDefaultWasmBytes,
|
|
4
|
-
readModelFile,
|
|
5
|
-
readModelsDir
|
|
6
|
-
} from "./node-fs.js";
|
|
2
|
+
import { loadDefaultWasmBytes } from "./node-fs.js";
|
|
7
3
|
var enc = new TextEncoder();
|
|
8
4
|
var dec = new TextDecoder();
|
|
9
|
-
var OCR_MODEL_FILES = {
|
|
10
|
-
alpha: "ocr_alpha.gpocr",
|
|
11
|
-
arabic: "ocr_arabic.gpocr",
|
|
12
|
-
devanagari: "ocr_deva.gpocr",
|
|
13
|
-
bengali: "ocr_beng.gpocr",
|
|
14
|
-
tamil: "ocr_taml.gpocr",
|
|
15
|
-
cjk: "ocr_cjk.gpocr",
|
|
16
|
-
japanese: "ocr_jpn.gpocr",
|
|
17
|
-
korean: "ocr_kor.gpocr",
|
|
18
|
-
thai: "ocr_thai.gpocr",
|
|
19
|
-
telugu: "ocr_telu.gpocr",
|
|
20
|
-
kannada: "ocr_kann.gpocr",
|
|
21
|
-
malayalam: "ocr_mlym.gpocr",
|
|
22
|
-
gujarati: "ocr_gujr.gpocr",
|
|
23
|
-
gurmukhi: "ocr_guru.gpocr",
|
|
24
|
-
oriya: "ocr_orya.gpocr",
|
|
25
|
-
sinhala: "ocr_sinh.gpocr",
|
|
26
|
-
georgian: "ocr_geor.gpocr",
|
|
27
|
-
armenian: "ocr_armn.gpocr",
|
|
28
|
-
khmer: "ocr_khmr.gpocr",
|
|
29
|
-
lao: "ocr_laoo.gpocr",
|
|
30
|
-
myanmar: "ocr_mymr.gpocr",
|
|
31
|
-
amharic: "ocr_ethi.gpocr"
|
|
32
|
-
};
|
|
33
|
-
var ALL_OCR_SCRIPTS = [
|
|
34
|
-
"alpha",
|
|
35
|
-
"arabic",
|
|
36
|
-
"devanagari",
|
|
37
|
-
"bengali",
|
|
38
|
-
"tamil",
|
|
39
|
-
"cjk",
|
|
40
|
-
"japanese",
|
|
41
|
-
"korean",
|
|
42
|
-
"thai",
|
|
43
|
-
"telugu",
|
|
44
|
-
"kannada",
|
|
45
|
-
"malayalam",
|
|
46
|
-
"gujarati",
|
|
47
|
-
"gurmukhi",
|
|
48
|
-
"oriya",
|
|
49
|
-
"sinhala",
|
|
50
|
-
"georgian",
|
|
51
|
-
"armenian",
|
|
52
|
-
"khmer",
|
|
53
|
-
"lao",
|
|
54
|
-
"myanmar",
|
|
55
|
-
"amharic"
|
|
56
|
-
];
|
|
57
5
|
var GigaPdfEngine = class _GigaPdfEngine {
|
|
58
6
|
constructor(ex) {
|
|
59
7
|
this.ex = ex;
|
|
@@ -144,6 +92,26 @@ var GigaPdfEngine = class _GigaPdfEngine {
|
|
|
144
92
|
const s = this._str(call);
|
|
145
93
|
return s ? JSON.parse(s) : [];
|
|
146
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Like {@link _json} but distinguishes a **null** result (the export returned a
|
|
97
|
+
* null pointer — e.g. an unrecognized container) from a present-but-empty JSON
|
|
98
|
+
* payload (`[]` / `{}`). Returns `null` only for the null pointer; otherwise the
|
|
99
|
+
* parsed JSON. Used where the Rust side is `Option<…>` and "nothing" and
|
|
100
|
+
* "unrecognized" must stay distinct.
|
|
101
|
+
*/
|
|
102
|
+
_jsonOrNull(call) {
|
|
103
|
+
const lenPtr = this.ex.gp_alloc(4);
|
|
104
|
+
const dataPtr = call(lenPtr);
|
|
105
|
+
if (dataPtr === 0) {
|
|
106
|
+
this.ex.gp_free(lenPtr, 4);
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
const len = this.dv().getUint32(lenPtr, true);
|
|
110
|
+
const s = dec.decode(this.u8().slice(dataPtr, dataPtr + len));
|
|
111
|
+
this.ex.gp_free(dataPtr, len);
|
|
112
|
+
this.ex.gp_free(lenPtr, 4);
|
|
113
|
+
return s ? JSON.parse(s) : [];
|
|
114
|
+
}
|
|
147
115
|
/**
|
|
148
116
|
* Decode standard Base64 (RFC 4648) to bytes. Pure-JS table decode, so it
|
|
149
117
|
* works identically in Node and the browser with no dependency (used to turn
|
|
@@ -274,6 +242,20 @@ var GigaPdfEngine = class _GigaPdfEngine {
|
|
|
274
242
|
(p, l) => this._buffer((o) => this.ex.gp_office_to_pdf(p, l, o))
|
|
275
243
|
);
|
|
276
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* Phase 1 for {@link officeToPdf} — the Google/system fonts an Office container
|
|
247
|
+
* **references but doesn't embed**. Download each `url` (→ TTF) and supply the
|
|
248
|
+
* bytes back to the host font cache so {@link officeToPdf}'s styled runs lay out
|
|
249
|
+
* with the right metrics. Faces the container embeds itself are de-obfuscated
|
|
250
|
+
* and used automatically (not listed here). Returns `null` if the bytes are not
|
|
251
|
+
* a recognized Office container, `[]` if it needs no host fonts.
|
|
252
|
+
*/
|
|
253
|
+
officeNeededFonts(office) {
|
|
254
|
+
return this._withBytes(
|
|
255
|
+
office,
|
|
256
|
+
(p, l) => this._jsonOrNull((o) => this.ex.gp_office_needed_fonts(p, l, o))
|
|
257
|
+
);
|
|
258
|
+
}
|
|
277
259
|
/**
|
|
278
260
|
* Image (PNG/JPEG/GIF/WebP/AVIF) → one-page PDF, format auto-detected: the
|
|
279
261
|
* image is centred and scaled to fit on an A4 portrait page. PNG/JPEG embed
|
|
@@ -435,68 +417,6 @@ var GigaPdfEngine = class _GigaPdfEngine {
|
|
|
435
417
|
const dv = new DataView(framed.buffer, framed.byteOffset, framed.byteLength);
|
|
436
418
|
return { width: dv.getUint32(0, true), height: dv.getUint32(4, true), rgba: framed.subarray(8) };
|
|
437
419
|
}
|
|
438
|
-
// ── OCR models (host-loaded `.gpocr` blobs — multi-script line recognizer) ─
|
|
439
|
-
/**
|
|
440
|
-
* Load a `.gpocr` line-level OCR model blob into the engine's runtime registry
|
|
441
|
-
* — Latin/Cyrillic/Greek (`alpha`), Arabic (RTL), Devanagari, Bengali, Tamil…
|
|
442
|
-
* The host supplies the bytes (the wasm ships no weights, like fonts), so the
|
|
443
|
-
* module stays lean. Once loaded, {@link GigaPdfDoc.ocr} / {@link GigaPdfDoc.ocrText}
|
|
444
|
-
* route to the CRNN+CTC line recognizer (per-page fallback to the built-in
|
|
445
|
-
* mono-glyph Latin classifier). Models are global to the engine; call after
|
|
446
|
-
* `load`/`loadDefault`. Returns `true` on success, `false` on a malformed blob.
|
|
447
|
-
*/
|
|
448
|
-
loadOcrModel(model) {
|
|
449
|
-
return this._withBytes(model, (p, l) => this.ex.gp_ocr_load_model(p, l)) === 1;
|
|
450
|
-
}
|
|
451
|
-
/** Drop every runtime-loaded OCR model (reverts to the mono-glyph classifier). */
|
|
452
|
-
clearOcrModels() {
|
|
453
|
-
this.ex.gp_ocr_clear_models();
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Resolve a bundled `.gpocr` model (shipped under `models/`) and load it into
|
|
457
|
-
* the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
|
|
458
|
-
* from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
|
|
459
|
-
* on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
|
|
460
|
-
* Bengali, Tamil, Chinese `cjk`, Japanese, Korean) or to upgrade
|
|
461
|
-
* Latin/Cyrillic/Greek to the CRNN (`alpha`). Returns `false` (rather than
|
|
462
|
-
* throwing) when the blob isn't bundled in this release — e.g. a script still
|
|
463
|
-
* training upstream — so loading a set stays resilient.
|
|
464
|
-
*/
|
|
465
|
-
async loadBundledOcrModel(script) {
|
|
466
|
-
try {
|
|
467
|
-
return this.loadOcrModel(await readModelFile(OCR_MODEL_FILES[script]));
|
|
468
|
-
} catch {
|
|
469
|
-
return false;
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
/**
|
|
473
|
-
* Load several bundled OCR models (idempotent per call). Returns the scripts
|
|
474
|
-
* that loaded successfully. Pass {@link ALL_OCR_SCRIPTS} to recognize text in
|
|
475
|
-
* any supported language; the engine's script detector routes each line to the
|
|
476
|
-
* right model.
|
|
477
|
-
*/
|
|
478
|
-
async loadBundledOcrModels(scripts) {
|
|
479
|
-
const loaded = [];
|
|
480
|
-
for (const script of scripts) {
|
|
481
|
-
if (await this.loadBundledOcrModel(script)) loaded.push(script);
|
|
482
|
-
}
|
|
483
|
-
return loaded;
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
* Discover and load EVERY bundled `.gpocr` model (Node hosts only). The
|
|
487
|
-
* engine's script detector then routes each recognized line to the matching
|
|
488
|
-
* model, so the OCR recognizes text in any shipped script — Latin, Cyrillic,
|
|
489
|
-
* Greek, Arabic, Urdu, Hebrew (the RTL group), Devanagari, Bengali, Tamil, and
|
|
490
|
-
* any model added later — with no code change. Returns the number loaded.
|
|
491
|
-
*/
|
|
492
|
-
async loadAllBundledOcrModels() {
|
|
493
|
-
const models = await readModelsDir();
|
|
494
|
-
let count = 0;
|
|
495
|
-
for (const { bytes } of models) {
|
|
496
|
-
if (this.loadOcrModel(bytes)) count += 1;
|
|
497
|
-
}
|
|
498
|
-
return count;
|
|
499
|
-
}
|
|
500
420
|
// ── fonts (catalog / Google Fonts URL — the host performs the fetch) ───────
|
|
501
421
|
fontCatalog() {
|
|
502
422
|
return this._json((o) => this.ex.gp_font_catalog_json(o));
|
|
@@ -994,13 +914,6 @@ var GigaPdfDoc = class {
|
|
|
994
914
|
(p, l) => this.g._json((o) => this.ex().gp_search_json(this.h, p, l, caseInsensitive ? 1 : 0, o))
|
|
995
915
|
);
|
|
996
916
|
}
|
|
997
|
-
/** OCR a (scanned) page → words with PDF-space boxes. `scale` ≥ 2 for small text. */
|
|
998
|
-
ocr(page, scale = 2) {
|
|
999
|
-
return this.g._json((o) => this.ex().gp_ocr_json(this.h, page, scale, o));
|
|
1000
|
-
}
|
|
1001
|
-
ocrText(page, scale = 2) {
|
|
1002
|
-
return this.g._str((o) => this.ex().gp_ocr_text(this.h, page, scale, o));
|
|
1003
|
-
}
|
|
1004
917
|
// editing
|
|
1005
918
|
/**
|
|
1006
919
|
* Replace text run `index` on `page` with `text`. Font-aware: works with
|
|
@@ -2045,7 +1958,6 @@ var GigaPdfDoc = class {
|
|
|
2045
1958
|
}
|
|
2046
1959
|
};
|
|
2047
1960
|
export {
|
|
2048
|
-
ALL_OCR_SCRIPTS,
|
|
2049
1961
|
GigaPdfDoc,
|
|
2050
1962
|
GigaPdfEngine
|
|
2051
1963
|
};
|