@qrcommunication/gigapdf-lib 0.109.1 → 0.110.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 CHANGED
@@ -6,6 +6,54 @@ project adheres to [Semantic Versioning](https://semver.org/).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.110.0] - 2026-06-30
10
+
11
+ ### Added — `extractWebFont`: serve a document's own embedded fonts to a browser
12
+
13
+ - New engine method **`extractWebFont(name)`** (FFI `gp_extract_web_font`) returns
14
+ a PDF's **own embedded font** as a **browser-loadable sfnt** for a `@font-face`
15
+ overlay-text layer — keeping the document's **original glyphs**, never a
16
+ substitute. It complements `extractFont` (which returns the raw program for
17
+ re-embedding): where `extractFont` hands back bytes a browser's `FontFace`/OTS
18
+ routinely rejects (a bare CFF, or a `cmap`-less TrueType subset),
19
+ `extractWebFont` repairs them:
20
+ - **CFF (Type1C)** → wrapped to OpenType (`OTTO`) with a `cmap` built from the
21
+ PDF's `code → Unicode` decode mapping (the same one text extraction uses, so
22
+ it resolves subset glyphs whose `/Differences` carries opaque `gNN` names and
23
+ whose `/ToUnicode` is partial — the exact case that otherwise renders the
24
+ wrong letters).
25
+ - **TrueType** → passed through when it already maps Unicode; otherwise a `cmap`
26
+ is synthesised (`code → Unicode` from the decode path, `code → gid` borrowed
27
+ from a sibling's `cmap` for full-glyph-order subsets, else the `code == gid`
28
+ convention) and the OTS-mandatory `OS/2`/`name`/`post` are stubbed in — the
29
+ original `glyf` is kept verbatim.
30
+ - Face selection prefers an **exact raw `/BaseFont`** match (subset prefix
31
+ included), so a host that serves many disjoint subsets of one family gets
32
+ *that* subset's own glyphs per run.
33
+ - New internal helper `cff_to_otf::repair_sfnt_with_cmap` (injects a Unicode
34
+ `cmap` + the OTS-required tables around existing glyph data) and a hardened
35
+ `build_cmap` (now sorts/dedupes segments, so a synthesised format-4 table is
36
+ accepted by strict shapers — FreeType/HarfBuzz).
37
+ - Tests: `repair_injects_cmap_and_required_tables_keeping_originals` (the repair
38
+ keeps the original tables and adds a working `cmap`).
39
+
40
+ ### Added — bidirectional conversion symmetry: model importers for RTF, TXT and images
41
+
42
+ - New engine methods `rtfToModel(rtf)`, `txtToModel(text)` and
43
+ `imageToModel(image)` complete the **lower-any-format-into-the-model** matrix.
44
+ The core already exposed `rtf_to_model` / `txt_to_model` / `image_to_model`,
45
+ but they had **no WASM/SDK binding** — so `modelToRtf` could write RTF yet
46
+ nothing could read RTF *into* the model, and likewise for plain text and raster
47
+ images. The three new FFI entry points (`gp_model_from_rtf` /
48
+ `gp_model_from_txt` / `gp_model_from_image`) and their TypeScript wrappers close
49
+ that asymmetry, matching the existing `officeToModel` / `htmlToModel` /
50
+ `mdToModel` / `csvToModel`. `imageToModel` returns `null` on non-image bytes;
51
+ `rtfToModel` routes through the rich RTF parser (run styling, tables, `\pict`
52
+ images, `\field` links).
53
+ - Tests: `test/model_import.test.ts` — 7 round-trip cases through the production
54
+ WASM (`txtToModel`→`modelToPdf`, `rtfToModel`→`modelToRtf`,
55
+ `imageToModel`→`modelToPdf`, plus the non-image `null` guard).
56
+
9
57
  ## [0.109.1] - 2026-06-30
10
58
 
11
59
  ### Fixed — image-format documentation accuracy
package/dist/index.cjs CHANGED
@@ -813,6 +813,40 @@ var GigaPdfEngine = class _GigaPdfEngine {
813
813
  (p, l) => this._modelOrNull((o) => this.ex.gp_model_from_csv(p, l, o))
814
814
  );
815
815
  }
816
+ /**
817
+ * Lower an RTF string into the unified {@link GigaDocument} model. Routed
818
+ * through the rich RTF parser, so run-level styling (bold/italic/underline/
819
+ * strike, colour, size, family), tables, `\pict` images and `\field`
820
+ * hyperlinks are recovered — the inverse of {@link modelToRtf}.
821
+ */
822
+ rtfToModel(rtf) {
823
+ return this._withStr(
824
+ rtf,
825
+ (p, l) => JSON.parse(this._str((o) => this.ex.gp_model_from_rtf(p, l, o)))
826
+ );
827
+ }
828
+ /**
829
+ * Lower a plain-text string into the unified {@link GigaDocument} model: one
830
+ * paragraph per line (blank lines kept as empty paragraphs).
831
+ */
832
+ txtToModel(text) {
833
+ return this._withStr(
834
+ text,
835
+ (p, l) => JSON.parse(this._str((o) => this.ex.gp_model_from_txt(p, l, o)))
836
+ );
837
+ }
838
+ /**
839
+ * Lower a raster image (PNG/JPEG/GIF/WebP, auto-detected) into the unified
840
+ * {@link GigaDocument} model — a single full-page picture block sized to the
841
+ * image's own aspect ratio, with the bytes interned in the model's resource
842
+ * table. Returns `null` if the bytes aren't a recognized image.
843
+ */
844
+ imageToModel(image) {
845
+ return this._withBytes(
846
+ image,
847
+ (p, l) => this._modelOrNull((o) => this.ex.gp_model_from_image(p, l, o))
848
+ );
849
+ }
816
850
  /**
817
851
  * Apply a batch of {@link ModelOp} edits to a model and return the edited
818
852
  * model. Ops run in order; out-of-range addresses (and any op that can't be
@@ -2401,6 +2435,26 @@ var GigaPdfDoc = class {
2401
2435
  const format = buf[0] === 1 ? "truetype" : buf[0] === 2 ? "cff" : "type1";
2402
2436
  return { format, bytes: buf.subarray(1) };
2403
2437
  }
2438
+ /**
2439
+ * Extract an embedded font as a **browser-loadable** sfnt by (fuzzy)
2440
+ * `/BaseFont` name — for a host editor's `@font-face` overlay-text layer.
2441
+ * Unlike {@link extractFont} (raw program, for re-embedding), this always
2442
+ * returns bytes a browser's `FontFace`/OTS will accept, **keeping the
2443
+ * document's own glyphs** (no substitution): a bare CFF (Type1C) is wrapped to
2444
+ * OpenType with a `cmap` from its charset, and a `cmap`-less TrueType subset is
2445
+ * repaired with a `cmap` synthesised from the PDF's `code → Unicode` (original
2446
+ * `glyf` kept). `truetype`/`otf` are directly loadable; `cff`/`type1` are raw
2447
+ * last-resort fallbacks. `null` when nothing embedded matches.
2448
+ */
2449
+ extractWebFont(name) {
2450
+ const buf = this.g._withStr(
2451
+ name,
2452
+ (p, l) => this.g._buffer((o) => this.ex().gp_extract_web_font(this.h, p, l, o))
2453
+ );
2454
+ if (buf.length === 0) return null;
2455
+ const format = buf[0] === 1 ? "truetype" : buf[0] === 2 ? "otf" : buf[0] === 3 ? "cff" : "type1";
2456
+ return { format, bytes: buf.subarray(1) };
2457
+ }
2404
2458
  /**
2405
2459
  * The fonts **embedded** in the document — each `{ baseFont, format }`. Pair
2406
2460
  * with {@link extractFont} to pull a font's bytes out and re-embed them via