@qrcommunication/gigapdf-lib 0.53.0 → 0.55.1

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 CHANGED
@@ -4,6 +4,66 @@ All notable changes to `@qrcommunication/gigapdf-lib` are documented here.
4
4
  The format follows [Keep a Changelog](https://keepachangelog.com/) and the
5
5
  project adheres to [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## [0.55.1] - 2026-06-21
8
+
9
+ ### Fixed
10
+
11
+ - **`imageToPdf` now embeds every PNG variant — no more empty buffer.** The pure-Rust
12
+ PNG decoder only handled 8-bit, non-interlaced images, so any PNG with a 16-bit
13
+ depth (common from screenshots and image editors), a sub-byte depth (1/2/4-bit
14
+ greyscale and palette), or Adam7 interlacing was rejected — `imageToPdf` returned
15
+ an **empty array** for those inputs. The decoder now supports the full PNG matrix:
16
+ colour types 0/2/3/4/6 at bit depths 1, 2, 4, 8 and 16, both non-interlaced and
17
+ interlaced, plus `tRNS` colour-key transparency for greyscale and truecolour
18
+ images. Transparency (PNG alpha and transcoded GIF/WebP/AVIF alpha) is preserved
19
+ via a `/DeviceGray` soft mask (`/SMask`), never flattened.
20
+
21
+ ### Added
22
+
23
+ - **`imageToPdf(image)` — raster image → one-page PDF.** PNG, JPEG, GIF, WebP and
24
+ AVIF are accepted (format auto-detected); the image is centred and scaled to fit
25
+ on an A4 portrait page and embedded as a real `/Image` XObject. PNG/JPEG embed
26
+ directly (JPEG verbatim via `/DCTDecode`); **GIF/WebP/AVIF are transcoded to PNG**
27
+ first (native `gif`/`webp`/`avif` decode → PNG encode), since the embedder only
28
+ writes PNG/JPEG XObjects. Returns an empty array for unrecognized bytes. Pure
29
+ Rust/WASM — no third-party image library.
30
+ - **AVIF dimension probe.** The image-header reader now recognizes AVIF/HEIF-still
31
+ containers and reads the canvas size from the `meta → iprp → ipco → ispe` box (a
32
+ cheap header parse, no AV1 decode; falls back to a full decode for unusual box
33
+ orderings), so `image_to_model` lowers an AVIF to a full-page image document too.
34
+ - **`mergePdfs(pdfs)` — concatenate several PDFs into one.** Appends each input's
35
+ pages in order onto the first (empty list → empty bytes; single PDF → returned
36
+ unchanged). Built on the existing `appendPages`.
37
+
38
+ ## [0.54.0] - 2026-06-20
39
+
40
+ ### Added
41
+
42
+ - **OCR front-end restoration (no API change — automatic in `ocr`).** Before recognition the
43
+ engine now (1) **auto-crops a photographed page** — detects the document's four corners on a
44
+ contrasting background and perspective-warps it head-on (8×8 DLT homography + bilinear warp,
45
+ pure `std`), and (2) **flattens uneven illumination** (flat-field divide by a local background:
46
+ shadows/glare → uniform page). Both are **gated to no-op on already-clean scans**, so they only
47
+ help phone photos / creased paper. Rescues real-world captures with zero caller changes.
48
+ - **Chinese OCR — new `cjk` script.** `loadBundledOcrModel("cjk")` / `ALL_OCR_SCRIPTS` now load
49
+ `ocr_cjk.gpocr` (data-driven **2401-class** charset, 32/64/128 backbone) — **CER 0.206 on CASIA
50
+ handwritten Chinese**, the first CJK model shipped.
51
+ - **Japanese & Korean scripts declared** (`"japanese"`, `"korean"` → `ocr_jpn.gpocr` /
52
+ `ocr_kor.gpocr`). Their charsets include kana+kanji / Hangul **plus full ASCII** (mixed
53
+ alphanumerics). Models train upstream and land in a follow-up release — `loadBundledOcrModel`
54
+ now **returns `false` for an absent blob instead of throwing**, so `ALL_OCR_SCRIPTS` stays safe.
55
+ - **Handwriting & degraded variants** bundled: `ocr_alpha_hw.gpocr` (real-cursive, **beats
56
+ Tesseract on IAM — CER 0.309 vs 0.353**) and `ocr_alpha_photo.gpocr` (degradation-augmented,
57
+ beats the plain HW model on degraded input). Host-load via `loadOcrModel`.
58
+
59
+ ### Changed
60
+
61
+ - **Non-Latin models rebuilt at the 32/64/128 backbone** — Devanagari, Bengali, Tamil and
62
+ Arabic validation CER roughly **halved** (deva 0.039, beng 0.042, taml 0.011, arabic 0.030);
63
+ the bundled `.gpocr` blobs are updated. Capacity, not data, was the bound.
64
+ - **Faster real-dataset training downloads** (dev tooling): `hw_datasets.py` fetches line images
65
+ **concurrently** (`GIGA_OCR_DL_WORKERS`), ~16× quicker — pairs with an HF Pro token.
66
+
7
67
  ## [0.52.5] - 2026-06-19
8
68
 
9
69
  ### Added
package/README.md CHANGED
@@ -107,6 +107,12 @@ Task-oriented snippets using the high-level classes. Each assumes
107
107
 
108
108
  ### Merge several PDFs
109
109
 
110
+ ```ts
111
+ const merged = giga.mergePdfs([first, second, third]); // one PDF, pages in order
112
+ ```
113
+
114
+ Or, for finer control, append page-by-page yourself:
115
+
110
116
  ```ts
111
117
  const doc = giga.open(first); // the base document
112
118
  for (const extra of [second, third]) doc.appendPages(extra); // append every page
@@ -114,6 +120,13 @@ const merged = doc.saveCompressed();
114
120
  doc.close();
115
121
  ```
116
122
 
123
+ ### Image → PDF
124
+
125
+ ```ts
126
+ const pdf = giga.imageToPdf(imageBytes); // PNG/JPEG/GIF/WebP/AVIF → one A4 page
127
+ // (image centred, shrink-to-fit, never upscaled; empty array if not an image)
128
+ ```
129
+
117
130
  ### Split — extract a page range into a new PDF
118
131
 
119
132
  ```ts
@@ -254,7 +267,8 @@ Or call `GigaPdfEngine.load(bytes)` with bytes you read yourself.
254
267
  > signatures and defaults also ship in the bundled `.d.ts`.
255
268
 
256
269
  - **`GigaPdfEngine`** — `load`/`loadDefault`, `open`/`openEncrypted`,
257
- `txtToPdf`/`htmlToPdf`/`rtfToPdf`/`officeToPdf`, `fontCatalog`/`fontRequestUrl`/`parseCssFontUrl`.
270
+ `txtToPdf`/`htmlToPdf`/`rtfToPdf`/`officeToPdf`/`imageToPdf` (PNG/JPEG/GIF/WebP/AVIF → A4 page),
271
+ `mergePdfs` (concatenate many PDFs), `fontCatalog`/`fontRequestUrl`/`parseCssFontUrl`.
258
272
  - **`GigaPdfDoc`** — text intelligence (`textRuns`, `structuredText`, `search`,
259
273
  `ocr`, `ocrText`, `elements`, `elementAt`), editing (`replaceText`,
260
274
  `removeElement`, `moveElement`, `duplicateElement`, `redact`), vector drawing
package/dist/index.cjs CHANGED
@@ -33,14 +33,20 @@ var OCR_MODEL_FILES = {
33
33
  arabic: "ocr_arabic.gpocr",
34
34
  devanagari: "ocr_deva.gpocr",
35
35
  bengali: "ocr_beng.gpocr",
36
- tamil: "ocr_taml.gpocr"
36
+ tamil: "ocr_taml.gpocr",
37
+ cjk: "ocr_cjk.gpocr",
38
+ japanese: "ocr_jpn.gpocr",
39
+ korean: "ocr_kor.gpocr"
37
40
  };
38
41
  var ALL_OCR_SCRIPTS = [
39
42
  "alpha",
40
43
  "arabic",
41
44
  "devanagari",
42
45
  "bengali",
43
- "tamil"
46
+ "tamil",
47
+ "cjk",
48
+ "japanese",
49
+ "korean"
44
50
  ];
45
51
  var GigaPdfEngine = class _GigaPdfEngine {
46
52
  constructor(ex) {
@@ -262,6 +268,40 @@ var GigaPdfEngine = class _GigaPdfEngine {
262
268
  (p, l) => this._buffer((o) => this.ex.gp_office_to_pdf(p, l, o))
263
269
  );
264
270
  }
271
+ /**
272
+ * Image (PNG/JPEG/GIF/WebP/AVIF) → one-page PDF, format auto-detected: the
273
+ * image is centred and scaled to fit on an A4 portrait page. PNG/JPEG embed
274
+ * directly; GIF/WebP/AVIF are transcoded to PNG first — all in pure Rust/WASM,
275
+ * no third-party image library. Empty array if the bytes are not a recognized
276
+ * image. To combine many images into a single document, pipe each result
277
+ * through {@link mergePdfs}.
278
+ */
279
+ imageToPdf(image) {
280
+ return this._withBytes(
281
+ image,
282
+ (p, l) => this._buffer((o) => this.ex.gp_image_to_pdf(p, l, o))
283
+ );
284
+ }
285
+ /**
286
+ * Merge several PDFs into one by appending their pages in order. Returns an
287
+ * empty document for an empty list, or the single input unchanged for a list
288
+ * of one. Each subsequent PDF is appended onto the first via
289
+ * {@link GigaPdfDoc.appendPages}; the merged bytes are returned and the working
290
+ * document is closed.
291
+ */
292
+ mergePdfs(pdfs) {
293
+ if (pdfs.length === 0) return new Uint8Array(0);
294
+ if (pdfs.length === 1) return pdfs[0];
295
+ const base = this.open(pdfs[0]);
296
+ try {
297
+ for (let i = 1; i < pdfs.length; i++) {
298
+ base.appendPages(pdfs[i]);
299
+ }
300
+ return base.save();
301
+ } finally {
302
+ base.close();
303
+ }
304
+ }
265
305
  /**
266
306
  * Write a host-built grid (`pages[rows][cells]`) to an `.xlsx` workbook — one
267
307
  * sheet per page — with the engine's native spreadsheet writer (no
@@ -411,10 +451,17 @@ var GigaPdfEngine = class _GigaPdfEngine {
411
451
  * the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
412
452
  * from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
413
453
  * on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
414
- * Bengali, Tamil) or to upgrade Latin/Cyrillic/Greek to the CRNN (`alpha`).
454
+ * Bengali, Tamil, Chinese `cjk`, Japanese, Korean) or to upgrade
455
+ * Latin/Cyrillic/Greek to the CRNN (`alpha`). Returns `false` (rather than
456
+ * throwing) when the blob isn't bundled in this release — e.g. a script still
457
+ * training upstream — so loading a set stays resilient.
415
458
  */
416
459
  async loadBundledOcrModel(script) {
417
- return this.loadOcrModel(await (0, import_node_fs.readModelFile)(OCR_MODEL_FILES[script]));
460
+ try {
461
+ return this.loadOcrModel(await (0, import_node_fs.readModelFile)(OCR_MODEL_FILES[script]));
462
+ } catch {
463
+ return false;
464
+ }
418
465
  }
419
466
  /**
420
467
  * Load several bundled OCR models (idempotent per call). Returns the scripts