@qrcommunication/gigapdf-lib 0.63.0 → 0.65.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/dist/index.cjs +60 -119
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +35 -63
  5. package/dist/index.d.ts +35 -63
  6. package/dist/index.js +61 -123
  7. package/dist/index.js.map +1 -1
  8. package/dist/node-fs.browser.cjs +3 -13
  9. package/dist/node-fs.browser.cjs.map +1 -1
  10. package/dist/node-fs.browser.d.cts +3 -10
  11. package/dist/node-fs.browser.d.ts +3 -10
  12. package/dist/node-fs.browser.js +2 -10
  13. package/dist/node-fs.browser.js.map +1 -1
  14. package/dist/node-fs.cjs +2 -37
  15. package/dist/node-fs.cjs.map +1 -1
  16. package/dist/node-fs.d.cts +1 -11
  17. package/dist/node-fs.d.ts +1 -11
  18. package/dist/node-fs.js +1 -34
  19. package/dist/node-fs.js.map +1 -1
  20. package/gigapdf.wasm +0 -0
  21. package/package.json +1 -1
  22. package/models/ocr_alpha.gpocr +0 -0
  23. package/models/ocr_alpha_big.gpocr +0 -0
  24. package/models/ocr_alpha_hw.gpocr +0 -0
  25. package/models/ocr_alpha_photo.gpocr +0 -0
  26. package/models/ocr_alpha_print.gpocr +0 -0
  27. package/models/ocr_arabic.gpocr +0 -0
  28. package/models/ocr_arabic_small.gpocr +0 -0
  29. package/models/ocr_beng.gpocr +0 -0
  30. package/models/ocr_beng_small.gpocr +0 -0
  31. package/models/ocr_cjk.gpocr +0 -0
  32. package/models/ocr_deva.gpocr +0 -0
  33. package/models/ocr_deva_small.gpocr +0 -0
  34. package/models/ocr_jpn.gpocr +0 -0
  35. package/models/ocr_kann.gpocr +0 -0
  36. package/models/ocr_kor.gpocr +0 -0
  37. package/models/ocr_mlym.gpocr +0 -0
  38. package/models/ocr_taml.gpocr +0 -0
  39. package/models/ocr_taml_small.gpocr +0 -0
  40. package/models/ocr_telu.gpocr +0 -0
  41. package/models/ocr_thai.gpocr +0 -0
package/CHANGELOG.md CHANGED
@@ -4,6 +4,58 @@ 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.65.0] - 2026-06-22
8
+
9
+ ### Added
10
+
11
+ - **Office→PDF phase-2 fonts** — `officeToPdfWith(office, fonts)` (ABI
12
+ `gp_office_to_pdf_with_fonts`, core `office_to_pdf_with_fonts`) completes the
13
+ two-phase font flow opened by `officeNeededFonts`: hand back the host-fetched
14
+ faces for the families a container **references but doesn't embed** (e.g.
15
+ Carlito for a Calibri reference) and styled runs lay out + paint with the right
16
+ metrics instead of drifting onto the bundled fallback. The supplied faces are
17
+ merged with whatever the document embeds itself — **embedded faces win on
18
+ conflict** — so an empty `fonts` array yields exactly `officeToPdf`'s output
19
+ (no regression). `fonts` uses the same packed blob as `htmlRender`.
20
+
21
+ ## [0.64.0] - 2026-06-22
22
+
23
+ Office↔PDF fidelity program — import all formats → PDF and export PDF → all
24
+ formats much closer to 1:1, including complex layouts (boxes/encadrés).
25
+
26
+ ### Added
27
+
28
+ - **Office→PDF preserves absolute layout** — presentation/box geometry is no
29
+ longer reflowed into a flat stack. PPTX/ODP shapes, images and tables carrying
30
+ an explicit `a:xfrm` / `draw:frame` are emitted at their exact coordinates
31
+ (EMU/ODF units → pt), with slide backgrounds and `a:schemeClr` theme colours
32
+ resolved. DOCX floating/anchored drawings (`wp:anchor`) and text boxes
33
+ (`w:txbxContent`) become absolutely-positioned frames (the “encadrés”), and
34
+ explicit page breaks (`w:br type=page`, `w:pageBreakBefore`, section breaks)
35
+ are honoured.
36
+ - **XLSX/ODS render with cell styling** — fonts (bold/italic/underline/size/
37
+ colour/family), borders, alignment and row heights are read from each cell's
38
+ style and applied at render (theme colours resolved); ODS cells were previously
39
+ unstyled. Merges, column widths and number formats unchanged.
40
+ - **PDF→Office export preserves absolute layout** — text boxes, images and vector
41
+ rectangles/paths (fill/stroke/dash) are exported at their exact coordinates for
42
+ PPTX/ODP/DOCX/ODT, so an exported deck/doc opened in PowerPoint/Word/Impress/
43
+ Writer looks like the source PDF, encadrés included.
44
+ - **Office→PDF embeds the document's own fonts** — a self-embedding DOCX/PPTX/
45
+ XLSX (`word|ppt|xl/fonts/*.odttf`, de-obfuscated per ECMA-376 §17.8.1) or ODT/
46
+ ODS/ODP (`Fonts/*`, TTF/OTF) renders with its **own** typefaces (exact glyphs
47
+ and metrics, no reflow drift) instead of the bundled Liberation fallback.
48
+ - **`officeNeededFonts(office)` / `gp_office_needed_fonts`** — phase-1 for
49
+ `officeToPdf`: returns the fonts a container **references but doesn't embed**
50
+ (`HtmlFontRequest[]`), so the host can fetch metric clones (Carlito↔Calibri,
51
+ Arimo↔Arial, …) into its font cache for correct line-breaking. `null` for an
52
+ unrecognized archive, `[]` when nothing is needed.
53
+ - **Stateful RTF rendering** — `rtfToPdf` now uses a real RTF parser with a `{}`
54
+ group state stack: character styling (`\b \i \ul \strike \cf \fs \f` via
55
+ font/colour tables), paragraph alignment/indents (`\qc\qr\qj\li\fi`), tables
56
+ (`\trowd\cell\row`) and correct CP1252 (`\'80`→€, smart quotes, dashes) instead
57
+ of the previous text-only extraction.
58
+
7
59
  ## [0.63.0] - 2026-06-22
8
60
 
9
61
  ### Changed
package/dist/index.cjs CHANGED
@@ -20,7 +20,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- ALL_OCR_SCRIPTS: () => ALL_OCR_SCRIPTS,
24
23
  GigaPdfDoc: () => GigaPdfDoc,
25
24
  GigaPdfEngine: () => GigaPdfEngine
26
25
  });
@@ -28,54 +27,6 @@ module.exports = __toCommonJS(index_exports);
28
27
  var import_node_fs = require("./node-fs.js");
29
28
  var enc = new TextEncoder();
30
29
  var dec = new TextDecoder();
31
- var OCR_MODEL_FILES = {
32
- alpha: "ocr_alpha.gpocr",
33
- arabic: "ocr_arabic.gpocr",
34
- devanagari: "ocr_deva.gpocr",
35
- bengali: "ocr_beng.gpocr",
36
- tamil: "ocr_taml.gpocr",
37
- cjk: "ocr_cjk.gpocr",
38
- japanese: "ocr_jpn.gpocr",
39
- korean: "ocr_kor.gpocr",
40
- thai: "ocr_thai.gpocr",
41
- telugu: "ocr_telu.gpocr",
42
- kannada: "ocr_kann.gpocr",
43
- malayalam: "ocr_mlym.gpocr",
44
- gujarati: "ocr_gujr.gpocr",
45
- gurmukhi: "ocr_guru.gpocr",
46
- oriya: "ocr_orya.gpocr",
47
- sinhala: "ocr_sinh.gpocr",
48
- georgian: "ocr_geor.gpocr",
49
- armenian: "ocr_armn.gpocr",
50
- khmer: "ocr_khmr.gpocr",
51
- lao: "ocr_laoo.gpocr",
52
- myanmar: "ocr_mymr.gpocr",
53
- amharic: "ocr_ethi.gpocr"
54
- };
55
- var ALL_OCR_SCRIPTS = [
56
- "alpha",
57
- "arabic",
58
- "devanagari",
59
- "bengali",
60
- "tamil",
61
- "cjk",
62
- "japanese",
63
- "korean",
64
- "thai",
65
- "telugu",
66
- "kannada",
67
- "malayalam",
68
- "gujarati",
69
- "gurmukhi",
70
- "oriya",
71
- "sinhala",
72
- "georgian",
73
- "armenian",
74
- "khmer",
75
- "lao",
76
- "myanmar",
77
- "amharic"
78
- ];
79
30
  var GigaPdfEngine = class _GigaPdfEngine {
80
31
  constructor(ex) {
81
32
  this.ex = ex;
@@ -166,6 +117,26 @@ var GigaPdfEngine = class _GigaPdfEngine {
166
117
  const s = this._str(call);
167
118
  return s ? JSON.parse(s) : [];
168
119
  }
120
+ /**
121
+ * Like {@link _json} but distinguishes a **null** result (the export returned a
122
+ * null pointer — e.g. an unrecognized container) from a present-but-empty JSON
123
+ * payload (`[]` / `{}`). Returns `null` only for the null pointer; otherwise the
124
+ * parsed JSON. Used where the Rust side is `Option<…>` and "nothing" and
125
+ * "unrecognized" must stay distinct.
126
+ */
127
+ _jsonOrNull(call) {
128
+ const lenPtr = this.ex.gp_alloc(4);
129
+ const dataPtr = call(lenPtr);
130
+ if (dataPtr === 0) {
131
+ this.ex.gp_free(lenPtr, 4);
132
+ return null;
133
+ }
134
+ const len = this.dv().getUint32(lenPtr, true);
135
+ const s = dec.decode(this.u8().slice(dataPtr, dataPtr + len));
136
+ this.ex.gp_free(dataPtr, len);
137
+ this.ex.gp_free(lenPtr, 4);
138
+ return s ? JSON.parse(s) : [];
139
+ }
169
140
  /**
170
141
  * Decode standard Base64 (RFC 4648) to bytes. Pure-JS table decode, so it
171
142
  * works identically in Node and the browser with no dependency (used to turn
@@ -296,6 +267,46 @@ var GigaPdfEngine = class _GigaPdfEngine {
296
267
  (p, l) => this._buffer((o) => this.ex.gp_office_to_pdf(p, l, o))
297
268
  );
298
269
  }
270
+ /**
271
+ * Phase 1 for {@link officeToPdf} — the Google/system fonts an Office container
272
+ * **references but doesn't embed**. Download each `url` (→ TTF) and supply the
273
+ * bytes back to the host font cache so {@link officeToPdf}'s styled runs lay out
274
+ * with the right metrics. Faces the container embeds itself are de-obfuscated
275
+ * and used automatically (not listed here). Returns `null` if the bytes are not
276
+ * a recognized Office container, `[]` if it needs no host fonts.
277
+ */
278
+ officeNeededFonts(office) {
279
+ return this._withBytes(
280
+ office,
281
+ (p, l) => this._jsonOrNull((o) => this.ex.gp_office_needed_fonts(p, l, o))
282
+ );
283
+ }
284
+ /**
285
+ * Phase 2 for {@link officeNeededFonts} — render an Office container to PDF with
286
+ * the host-fetched `fonts` embedded, so families the container **references but
287
+ * doesn't embed** (reported by {@link officeNeededFonts}) lay out with the right
288
+ * metrics (e.g. Carlito for a Calibri reference) instead of drifting onto the
289
+ * bundled fallback. Faces the container embeds itself win on conflict, so an
290
+ * empty `fonts` array yields exactly {@link officeToPdf}'s output. Empty array
291
+ * if the bytes are not a recognized Office container.
292
+ *
293
+ * @example
294
+ * const reqs = giga.officeNeededFonts(docx); // phase 1: what to fetch
295
+ * const fonts = await Promise.all( // host fetches each url → TTF
296
+ * (reqs ?? []).map(async (r) => ({ ...r, ttf: await fetchTtf(r.url) }))
297
+ * );
298
+ * const pdf = giga.officeToPdfWith(docx, fonts); // phase 2: render with them
299
+ */
300
+ officeToPdfWith(office, fonts = []) {
301
+ const blob = packHtmlFonts(fonts);
302
+ return this._withBytes(
303
+ office,
304
+ (op, ol) => this._withBytes(
305
+ blob,
306
+ (fp, fl) => this._buffer((o) => this.ex.gp_office_to_pdf_with_fonts(op, ol, fp, fl, o))
307
+ )
308
+ );
309
+ }
299
310
  /**
300
311
  * Image (PNG/JPEG/GIF/WebP/AVIF) → one-page PDF, format auto-detected: the
301
312
  * image is centred and scaled to fit on an A4 portrait page. PNG/JPEG embed
@@ -457,68 +468,6 @@ var GigaPdfEngine = class _GigaPdfEngine {
457
468
  const dv = new DataView(framed.buffer, framed.byteOffset, framed.byteLength);
458
469
  return { width: dv.getUint32(0, true), height: dv.getUint32(4, true), rgba: framed.subarray(8) };
459
470
  }
460
- // ── OCR models (host-loaded `.gpocr` blobs — multi-script line recognizer) ─
461
- /**
462
- * Load a `.gpocr` line-level OCR model blob into the engine's runtime registry
463
- * — Latin/Cyrillic/Greek (`alpha`), Arabic (RTL), Devanagari, Bengali, Tamil…
464
- * The host supplies the bytes (the wasm ships no weights, like fonts), so the
465
- * module stays lean. Once loaded, {@link GigaPdfDoc.ocr} / {@link GigaPdfDoc.ocrText}
466
- * route to the CRNN+CTC line recognizer (per-page fallback to the built-in
467
- * mono-glyph Latin classifier). Models are global to the engine; call after
468
- * `load`/`loadDefault`. Returns `true` on success, `false` on a malformed blob.
469
- */
470
- loadOcrModel(model) {
471
- return this._withBytes(model, (p, l) => this.ex.gp_ocr_load_model(p, l)) === 1;
472
- }
473
- /** Drop every runtime-loaded OCR model (reverts to the mono-glyph classifier). */
474
- clearOcrModels() {
475
- this.ex.gp_ocr_clear_models();
476
- }
477
- /**
478
- * Resolve a bundled `.gpocr` model (shipped under `models/`) and load it into
479
- * the engine via {@link loadOcrModel}. **Node hosts only** — it reads the blob
480
- * from disk the same way {@link loadDefault} resolves the wasm. Returns `true`
481
- * on success. Use this to recognize non-Latin scripts (Arabic, Devanagari,
482
- * Bengali, Tamil, Chinese `cjk`, Japanese, Korean) or to upgrade
483
- * Latin/Cyrillic/Greek to the CRNN (`alpha`). Returns `false` (rather than
484
- * throwing) when the blob isn't bundled in this release — e.g. a script still
485
- * training upstream — so loading a set stays resilient.
486
- */
487
- async loadBundledOcrModel(script) {
488
- try {
489
- return this.loadOcrModel(await (0, import_node_fs.readModelFile)(OCR_MODEL_FILES[script]));
490
- } catch {
491
- return false;
492
- }
493
- }
494
- /**
495
- * Load several bundled OCR models (idempotent per call). Returns the scripts
496
- * that loaded successfully. Pass {@link ALL_OCR_SCRIPTS} to recognize text in
497
- * any supported language; the engine's script detector routes each line to the
498
- * right model.
499
- */
500
- async loadBundledOcrModels(scripts) {
501
- const loaded = [];
502
- for (const script of scripts) {
503
- if (await this.loadBundledOcrModel(script)) loaded.push(script);
504
- }
505
- return loaded;
506
- }
507
- /**
508
- * Discover and load EVERY bundled `.gpocr` model (Node hosts only). The
509
- * engine's script detector then routes each recognized line to the matching
510
- * model, so the OCR recognizes text in any shipped script — Latin, Cyrillic,
511
- * Greek, Arabic, Urdu, Hebrew (the RTL group), Devanagari, Bengali, Tamil, and
512
- * any model added later — with no code change. Returns the number loaded.
513
- */
514
- async loadAllBundledOcrModels() {
515
- const models = await (0, import_node_fs.readModelsDir)();
516
- let count = 0;
517
- for (const { bytes } of models) {
518
- if (this.loadOcrModel(bytes)) count += 1;
519
- }
520
- return count;
521
- }
522
471
  // ── fonts (catalog / Google Fonts URL — the host performs the fetch) ───────
523
472
  fontCatalog() {
524
473
  return this._json((o) => this.ex.gp_font_catalog_json(o));
@@ -1016,13 +965,6 @@ var GigaPdfDoc = class {
1016
965
  (p, l) => this.g._json((o) => this.ex().gp_search_json(this.h, p, l, caseInsensitive ? 1 : 0, o))
1017
966
  );
1018
967
  }
1019
- /** OCR a (scanned) page → words with PDF-space boxes. `scale` ≥ 2 for small text. */
1020
- ocr(page, scale = 2) {
1021
- return this.g._json((o) => this.ex().gp_ocr_json(this.h, page, scale, o));
1022
- }
1023
- ocrText(page, scale = 2) {
1024
- return this.g._str((o) => this.ex().gp_ocr_text(this.h, page, scale, o));
1025
- }
1026
968
  // editing
1027
969
  /**
1028
970
  * Replace text run `index` on `page` with `text`. Font-aware: works with
@@ -2068,7 +2010,6 @@ var GigaPdfDoc = class {
2068
2010
  };
2069
2011
  // Annotate the CommonJS export names for ESM import in node:
2070
2012
  0 && (module.exports = {
2071
- ALL_OCR_SCRIPTS,
2072
2013
  GigaPdfDoc,
2073
2014
  GigaPdfEngine
2074
2015
  });