@qrcommunication/gigapdf-lib 0.113.0 → 0.114.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
@@ -6,6 +6,105 @@ project adheres to [Semantic Versioning](https://semver.org/).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.114.1] - 2026-07-03
10
+
11
+ ### Fixed — `replaceText` no longer empties or garbles runs (re-encoding = exact inverse of decoding)
12
+
13
+ `replaceText` re-encoded the new text through the **embedded program's cmap**
14
+ (Type0) or through **blind WinAnsi** (simple fonts), ignoring the
15
+ `/ToUnicode` / `/Encoding` / `/Differences` maps the extraction actually
16
+ decodes with. On subset-heavy documents (repacked or absent cmaps, partial
17
+ per-wrapper `/ToUnicode`, spaces remapped to low codes) this silently wrote
18
+ `.notdef` gids — `replaceText` returned `true` while the run went **empty**
19
+ — or wrong gids (mojibake). Measured on a dense CERFA: 527 identical-text
20
+ replacements → 140 OK / 239 emptied / 148 garbled BEFORE, **527/527 OK**
21
+ after.
22
+
23
+ Re-encoding now inverts the run's own decode maps first, then borrows codes
24
+ from **sibling wrappers of the same physical program** (resolved by
25
+ reference, as 0.114.0 does for extraction) — borrowed codes are taught back
26
+ to the wrapper's `/ToUnicode` so the text re-reads — then the program cmap.
27
+ A character unknown to the program now fails with `false` **without
28
+ mutating the run** (never again `true` + emptied).
29
+
30
+ ## [0.114.0] - 2026-07-03
31
+
32
+ ### Fixed — embedded web fonts resolved by PHYSICAL identity (accents no longer lost)
33
+
34
+ A PDF often shares ONE embedded font program across MANY `/Font` wrappers
35
+ (69 Type0 wrappers for 20 physical programs on a dense CERFA), each wrapper
36
+ carrying only a PARTIAL `/ToUnicode` for its own codes. The web extraction
37
+ resolved everything **by name**: the Type0 parent of a CIDFont was found by
38
+ scanning for the first wrapper with the same `/BaseFont` — often a sibling
39
+ with a 2-entry `/ToUnicode` — so the served cmap silently dropped accented
40
+ characters (à/â/é/ê/è) whose glyphs DO exist in the subset.
41
+
42
+ - The Type0 parent is now resolved **by reference** (`/DescendantFonts[0]` →
43
+ object id); the name scan survives only as a last-resort fallback.
44
+ - `extractWebFont(name)` no longer dead-ends on a wrapper without a font
45
+ program: the search widens past it, so a non-embedded alias
46
+ (`TimesNewRoman,BoldItalic`) is served by an embedded sibling subset of the
47
+ same face instead of nothing.
48
+ - The served cmap is the **union** of the mappings of EVERY wrapper
49
+ referencing the program (explicit `/ToUnicode` entries first — the
50
+ requesting wrapper wins conflicts — then inferred fills), and gains a
51
+ format 12 (3,10) subtable when a codepoint is beyond the BMP.
52
+
53
+ ### Added — physical font identity (`fontId`)
54
+
55
+ - `TextElementInfo.fontId` — 8-hex content hash of the run's embedded font
56
+ program (empty when the wrapper embeds none), resolved in the run's scope
57
+ (form XObjects included): the exact run→program mapping a host needs.
58
+ - `embeddedFontsV2()` — enumeration de-duplicated **by program**:
59
+ `{ fontId, format, numGlyphs, baseFonts[] }` (one fetch per program
60
+ instead of one per wrapper name).
61
+ - `extractWebFontById(fontId, preferWrapper?)` and `webFontCoverage(fontId)`
62
+ (the mapped codepoints of the served cmap, for per-character fallback
63
+ decisions host-side). `extractWebFont(name)` and `embeddedFonts()` are
64
+ unchanged.
65
+
66
+ ### Fixed — `pageBlocks` ↔ `textElements` index seam (paragraph editing)
67
+
68
+ `pageBlocks` emitted `source_index` in the unified element index space
69
+ (text + images + shapes) while `textElements()` / `replaceText` count
70
+ **top-level text runs only** — every interposed image or shape shifted the
71
+ seam (measured up to −139 on a dense form), so a host resolving a
72
+ paragraph's runs landed on arbitrary runs anywhere on the page. Both walks
73
+ now share the same text-run ordinal (a cross-check test pins them
74
+ together), and:
75
+
76
+ - coalesced inline runs (paragraphs AND table cells) now carry
77
+ **`source_indices`** — every constituent run index; the scalar
78
+ `source_index` stays the first entry for backward compatibility;
79
+ - `split_paragraphs` is geometrically hardened: a document-level leading
80
+ bound, an absolute 2.5×font-size gap cap and a ≥20 % horizontal-overlap
81
+ requirement stop far-apart lines (a footer and a header) from ever
82
+ fusing into one "paragraph".
83
+
84
+ ### Fixed — text form-field appearances match Adobe (`setTextField`)
85
+
86
+ - **Comb fields** (`/MaxLen` + COMB flag): one character per cell, centred.
87
+ - **Auto-size** (`0 Tf`): shrink-to-fit both height AND width (4 pt floor).
88
+ - **Multiline** (`Ff` bit 13): word-wrap at the rect width even without `\n`.
89
+ - **Quadding** (`/Q`): centred/right text measured with the metrics of the
90
+ base-14 face actually named by the `/DA` (was always Helvetica).
91
+ - The appearance stream now clips to the widget rect (long values no
92
+ longer bleed over neighbouring content).
93
+
94
+ ### Added — `setCheckboxState(name, state)`
95
+
96
+ Checks a specific named on-state of a checkbox with several widgets (the
97
+ `Oui`/`non` pairs of official forms): sets `/V` and, per widget, `/AS` to
98
+ the widget's own on-state or `/Off`. `setCheckbox(true)` (first on-state)
99
+ is unchanged; `"Off"` unchecks everywhere.
100
+
101
+ ### Added — model importers for RTF, TXT and images
102
+
103
+ `rtfToModel` / `txtToModel` / `imageToModel` (WASM: `gp_model_from_rtf`,
104
+ `gp_model_from_txt`, `gp_model_from_image`) — the missing "lower into the
105
+ unified model" bindings; the core importers already existed and are
106
+ unchanged.
107
+
9
108
  ## [0.113.0] - 2026-07-01
10
109
 
11
110
  ### Added — every form-field widget placement (`FieldInfo.widgets`)
package/dist/index.cjs CHANGED
@@ -2455,6 +2455,33 @@ var GigaPdfDoc = class {
2455
2455
  const format = buf[0] === 1 ? "truetype" : buf[0] === 2 ? "otf" : buf[0] === 3 ? "cff" : "type1";
2456
2456
  return { format, bytes: buf.subarray(1) };
2457
2457
  }
2458
+ /**
2459
+ * Extract an embedded font as a **browser-loadable** sfnt by its **physical
2460
+ * identity** (`fontId` — the value {@link textElements} and
2461
+ * {@link embeddedFontsV2} report). The name-ambiguity-proof sibling of
2462
+ * {@link extractWebFont}: a form generator routinely embeds dozens of
2463
+ * same-`/BaseFont` wrappers over a handful of physical programs, each with
2464
+ * its own *partial* `/ToUnicode` — a name lookup can land on a homonym whose
2465
+ * 2-entry map drops accents the program actually carries. Resolving by
2466
+ * `fontId` pins the exact program, and the served `cmap` is rebuilt as the
2467
+ * **union of every wrapper** referencing it (explicit `/ToUnicode` entries
2468
+ * first — `preferWrapper`, the run's own {@link TextElementInfo.baseFont},
2469
+ * winning on conflict — then inferred fills). The subset is always served
2470
+ * as-is, never rejected for an incomplete map. `null` only when no embedded
2471
+ * program carries `fontId`.
2472
+ */
2473
+ extractWebFontById(fontId, preferWrapper) {
2474
+ const buf = this.g._withStr(
2475
+ fontId,
2476
+ (ip, il) => this.g._withOptStr(
2477
+ preferWrapper,
2478
+ (pp, pl) => this.g._buffer((o) => this.ex().gp_extract_web_font_by_id(this.h, ip, il, pp, pl, o))
2479
+ )
2480
+ );
2481
+ if (buf.length === 0) return null;
2482
+ const format = buf[0] === 1 ? "truetype" : buf[0] === 2 ? "otf" : buf[0] === 3 ? "cff" : "type1";
2483
+ return { format, bytes: buf.subarray(1) };
2484
+ }
2458
2485
  /**
2459
2486
  * The fonts **embedded** in the document — each `{ baseFont, format }`. Pair
2460
2487
  * with {@link extractFont} to pull a font's bytes out and re-embed them via
@@ -2464,6 +2491,30 @@ var GigaPdfDoc = class {
2464
2491
  embeddedFonts() {
2465
2492
  return this.g._json((o) => this.ex().gp_embedded_fonts_json(this.h, o));
2466
2493
  }
2494
+ /**
2495
+ * The embedded fonts **deduplicated by physical program** — one
2496
+ * {@link EmbeddedFontV2} per `/FontFile*` stream, carrying its `fontId`,
2497
+ * format, glyph count and every `/BaseFont` wrapped around it. The
2498
+ * per-program sibling of {@link embeddedFonts} (kept intact); its `fontId`s
2499
+ * feed {@link extractWebFontById} / {@link webFontCoverage}. Sorted by
2500
+ * `fontId` for a deterministic output.
2501
+ */
2502
+ embeddedFontsV2() {
2503
+ return this.g._json((o) => this.ex().gp_embedded_fonts_v2_json(this.h, o));
2504
+ }
2505
+ /**
2506
+ * The Unicode code points mapped by the `cmap` {@link extractWebFontById}
2507
+ * serves for `fontId` — sorted, deduplicated. Lets a host check, before
2508
+ * painting, whether a run's characters are covered by the served face (and
2509
+ * pick a fallback for the gaps). Empty when no embedded program carries
2510
+ * `fontId`.
2511
+ */
2512
+ webFontCoverage(fontId) {
2513
+ return this.g._withStr(
2514
+ fontId,
2515
+ (p, l) => this.g._json((o) => this.ex().gp_web_font_coverage(this.h, p, l, o))
2516
+ );
2517
+ }
2467
2518
  // convert PDF → X
2468
2519
  toText() {
2469
2520
  return this.g._str((o) => this.ex().gp_to_text(this.h, o));
@@ -3628,6 +3679,20 @@ var GigaPdfDoc = class {
3628
3679
  setCheckbox(name, checked) {
3629
3680
  return this.g._withStr(name, (p, l) => this.ex().gp_set_checkbox(this.h, p, l, checked ? 1 : 0)) === 0;
3630
3681
  }
3682
+ /**
3683
+ * Set a checkbox to a **named** appearance state — the on-state export of
3684
+ * one of its widgets. A multi-widget checkbox names one export per widget
3685
+ * (e.g. `Oui` / `non` on French CERFA forms); {@link setCheckbox} with
3686
+ * `true` only ever reaches the first export, this reaches any of them.
3687
+ * `"Off"` unchecks every widget. Returns `false` when the field or state
3688
+ * doesn't exist.
3689
+ */
3690
+ setCheckboxState(name, state) {
3691
+ return this.g._withStr(
3692
+ name,
3693
+ (nP, nL) => this.g._withStr(state, (sP, sL) => this.ex().gp_set_checkbox_state(this.h, nP, nL, sP, sL))
3694
+ ) === 0;
3695
+ }
3631
3696
  setRadio(name, value) {
3632
3697
  return this.g._withStr(
3633
3698
  name,