@qrcommunication/gigapdf-lib 0.104.0 → 0.106.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
@@ -4,6 +4,71 @@ 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
+ ## [Unreleased]
8
+
9
+ ## [0.106.0] - 2026-06-27
10
+
11
+ ### Fixed
12
+
13
+ - **`textElements` now reports the scope-correct font for text inside form
14
+ XObjects.** A run drawn through a reusable form XObject (CERFA / invoice /
15
+ letterhead templates) was styled against the page's `/Font` table instead of the
16
+ form's own, so its `fontFamily`/`bold`/`italic` collapsed to "Helvetica" regular
17
+ — losing the embedded face and weight, and misplacing the editor overlay (wrong
18
+ metrics). Each run is now styled against the font table of its own scope.
19
+
20
+ ### Added
21
+
22
+ - **`TextElementInfo.baseFont`** — the run's raw `/BaseFont` with the subset prefix
23
+ kept (e.g. `"ABCDEF+TimesNewRomanPSMT"`), resolved against the run's own scope, so
24
+ a host editor can target the exact embedded subset rather than only the collapsed
25
+ `fontFamily`. Empty when the font carried no `/BaseFont` (e.g. a Type3 font).
26
+
27
+ ## [0.105.0] - 2026-06-26
28
+
29
+ ### Fixed
30
+
31
+ - **Text extraction no longer corrupts subset fonts with repacked `/Differences`
32
+ glyphs.** When a simple font assigns real glyphs to ASCII-punctuation codes via
33
+ `/Encoding`+`/Differences` (`gNN` names) and ships a broken identity-aligned
34
+ `/ToUnicode` omitting those codes, the ASCII gap-filler used to invent a
35
+ `code → chr(code)` mapping that masked the real glyph (consulted first) — e.g.
36
+ `extractText` returned `'&are%ts'` for `'parents'`. The filler now defers to the
37
+ font's `/Encoding`+`/Differences` for those codes. Extraction-only fix; rendering
38
+ was already correct, and composite (Type0) fonts are unaffected.
39
+
40
+ ### Added
41
+
42
+ - **Baked running header/footer excluded from the editable views.** A header/footer
43
+ baked with `setHeader`/`setFooter` (tagged `/GPHF`) is no longer returned by
44
+ `elements`, `textRuns` or `blocks` — re-opening a header-baked document never
45
+ turns the header into editable body content nor desyncs `replaceText` /
46
+ `transformElement` / `removeElement` / `reorderElement` indices. `headerFooter()`
47
+ still recovers it.
48
+ - **`renderPageExcludingMarkedContent(page, scale?, skipText?, marker?)`** — render
49
+ a page in one pass with a baked marked-content band (default `"GPHF"` = the
50
+ running header/footer) omitted, so the band shows in the raster but never doubles
51
+ against an editable overlay; with `skipText` it is the editor's text-free
52
+ background minus the band.
53
+ - **`setEditorMeta(json)` / `editorMeta()`** — store/read an opaque JSON
54
+ editor-metadata sidecar (catalog `/GigaPDF /EditorMeta`, compressed; ignored by
55
+ standard readers; survives save/open).
56
+ - **`setEditorMargins(page, m)` / `editorMargins(page)`** — per-page editor display
57
+ margins persisted in the sidecar (under `margins`), **without** touching
58
+ `/CropBox` (distinct from `setPageMargins`, the real recrop).
59
+ - **`setRunningHeaderFooter(def, opts?)` / `runningHeaderFooter()`** — a rich,
60
+ Word-like running header/footer (types `RunningHeaderFooter`, `HFZone`, `HFItem`,
61
+ `HFAlign`). The `def` is the source of truth (stored in the editor-meta sidecar
62
+ under `headerFooter`); its visible `/GPHF` band is regenerated per page, so it is
63
+ excluded from `elements`/`textRuns` and masked by
64
+ `renderPageExcludingMarkedContent`. A zone (`default`/`firstPage`/`evenPage`/`oddPage`)
65
+ holds `HFItem`s of `type: "text"` (drawn in an **embedded** font — the item's
66
+ `fontRef`, else the bundled OFL face, never base-14) or `type: "image"` (pixels
67
+ supplied via `opts.images`, an iterable of `[imageId, bytes]` — a `Map` works).
68
+ Tokens `{{page}}`, `{{pages}}`, `{{date}}` (pass `opts.date`) and `{{title}}` are
69
+ substituted at bake time; re-baking is idempotent. The flat
70
+ `setHeader`/`setFooter` API is unchanged.
71
+
7
72
  ## [0.104.0] - 2026-06-25
8
73
 
9
74
  Four issue lists closed — [#75], [#76], [#77], [#78]: PDF→model reconstruction,
package/dist/index.cjs CHANGED
@@ -155,6 +155,24 @@ var GigaPdfEngine = class _GigaPdfEngine {
155
155
  this.ex.gp_free(lenPtr, 4);
156
156
  return s ? JSON.parse(s) : [];
157
157
  }
158
+ /**
159
+ * Like {@link _str} but distinguishes a **null** result (the export returned a
160
+ * null pointer — e.g. an `Option::None`) from a present-but-empty string.
161
+ * Returns `null` only for the null pointer; otherwise the decoded string.
162
+ */
163
+ _strOrNull(call) {
164
+ const lenPtr = this.ex.gp_alloc(4);
165
+ const dataPtr = call(lenPtr);
166
+ if (dataPtr === 0) {
167
+ this.ex.gp_free(lenPtr, 4);
168
+ return null;
169
+ }
170
+ const len = this.dv().getUint32(lenPtr, true);
171
+ const s = dec.decode(this.u8().slice(dataPtr, dataPtr + len));
172
+ this.ex.gp_free(dataPtr, len);
173
+ this.ex.gp_free(lenPtr, 4);
174
+ return s;
175
+ }
158
176
  /**
159
177
  * Decode standard Base64 (RFC 4648) to bytes. Pure-JS table decode, so it
160
178
  * works identically in Node and the browser with no dependency (used to turn
@@ -1940,6 +1958,42 @@ var GigaPdfDoc = class {
1940
1958
  setPageMargins(page, m) {
1941
1959
  return this.ex().gp_set_page_margins(this.h, page, m.top, m.right, m.bottom, m.left) === 0;
1942
1960
  }
1961
+ /**
1962
+ * Store an **opaque** JSON string as the document's GigaPDF editor-metadata
1963
+ * sidecar — private editor state that travels with the PDF (catalog
1964
+ * `/GigaPDF /EditorMeta`, a FlateDecode-compressed stream). It is ignored by
1965
+ * every standard PDF reader and the round-trip survives save/open. Pass a JSON
1966
+ * **object** if you also use {@link setEditorMargins} (which read-modify-writes
1967
+ * that object's `margins` key). Returns `true` on success.
1968
+ */
1969
+ setEditorMeta(json) {
1970
+ return this.g._withStr(json, (p, l) => this.ex().gp_set_editor_meta(this.h, p, l)) === 0;
1971
+ }
1972
+ /**
1973
+ * The document's GigaPDF editor-metadata sidecar string (set by
1974
+ * {@link setEditorMeta}), or `null` when the document carries none.
1975
+ */
1976
+ editorMeta() {
1977
+ return this.g._strOrNull((o) => this.ex().gp_editor_meta(this.h, o));
1978
+ }
1979
+ /**
1980
+ * Record the editor's per-page **display** margins (points) in the
1981
+ * editor-metadata sidecar (under `margins`, keyed by page) **without touching
1982
+ * `/CropBox` or `/MediaBox`** — distinct from {@link setPageMargins}, which
1983
+ * performs a real visible recrop. Any other host keys in the sidecar are
1984
+ * preserved. Returns `true` on success.
1985
+ */
1986
+ setEditorMargins(page, m) {
1987
+ return this.ex().gp_set_editor_margins(this.h, page, m.top, m.right, m.bottom, m.left) === 0;
1988
+ }
1989
+ /**
1990
+ * The editor's per-page display margins (points) recorded for `page` by
1991
+ * {@link setEditorMargins}, or `null` when none were stored. Reads only the
1992
+ * sidecar — never `/CropBox`.
1993
+ */
1994
+ editorMargins(page) {
1995
+ return this.g._jsonOrNull((o) => this.ex().gp_editor_margins(this.h, page, o));
1996
+ }
1943
1997
  /**
1944
1998
  * All five page boundary boxes (`media`/`crop`/`bleed`/`trim`/`art`) for the
1945
1999
  * 1-based `page`, each as `[x0, y0, x1, y1]` in points, with ISO 32000-1
@@ -2086,6 +2140,78 @@ var GigaPdfDoc = class {
2086
2140
  (o) => this.ex().gp_header_footer(this.h, o)
2087
2141
  );
2088
2142
  }
2143
+ /**
2144
+ * Bake a **rich**, Word-like running header/footer from a {@link RunningHeaderFooter}
2145
+ * `def` (the source of truth — stored in the editor-meta sidecar, its visible
2146
+ * `/GPHF` band regenerated on every page). Text is set in an embedded font
2147
+ * (each item's `fontRef`, else the bundled OFL face — never base-14) and images
2148
+ * via the same path as {@link addImage}; both live inside the `/GPHF` span, so
2149
+ * they are excluded from {@link elements}/{@link textRuns} and masked by
2150
+ * {@link renderPageExcludingMarkedContent}. Idempotent (re-baking replaces the
2151
+ * prior band).
2152
+ *
2153
+ * `opts.date` is the bake date for `{{date}}` (the engine is clockless — pass
2154
+ * e.g. `new Date().toISOString().slice(0, 10)`); omit to fall back to the
2155
+ * document's `/Info` date. `opts.images` supplies the pixels each
2156
+ * `HFItem` of `type: "image"` references by `imageId` — an iterable of
2157
+ * `[imageId, bytes]` (a `Map<number, Uint8Array>` works directly); an image
2158
+ * whose id is absent is skipped. Returns `true` on success.
2159
+ *
2160
+ * @example
2161
+ * doc.setRunningHeaderFooter(
2162
+ * {
2163
+ * default: {
2164
+ * header: [{ type: "image", imageId: 1, w: 80, h: 24, anchor: "left" }],
2165
+ * footer: [{ type: "text", text: "{{title}} — {{page}}/{{pages}}", anchor: "center" }],
2166
+ * },
2167
+ * firstPage: { header: [], footer: [] }, // blank cover
2168
+ * differentFirstPage: true,
2169
+ * differentOddEven: true,
2170
+ * evenPage: { header: [{ type: "text", text: "{{page}}", anchor: "left" }], footer: [] },
2171
+ * oddPage: { header: [{ type: "text", text: "{{page}}", anchor: "right" }], footer: [] },
2172
+ * },
2173
+ * { date: new Date().toISOString().slice(0, 10), images: new Map([[1, logoPng]]) }
2174
+ * );
2175
+ */
2176
+ setRunningHeaderFooter(def, opts) {
2177
+ const entries = opts?.images ? Array.from(opts.images) : [];
2178
+ let total = 4;
2179
+ for (const [, bytes] of entries) total += 8 + bytes.length;
2180
+ const blob = new Uint8Array(total);
2181
+ const dv = new DataView(blob.buffer);
2182
+ let off = 0;
2183
+ dv.setUint32(off, entries.length >>> 0, true);
2184
+ off += 4;
2185
+ for (const [id, bytes] of entries) {
2186
+ dv.setUint32(off, id >>> 0, true);
2187
+ off += 4;
2188
+ dv.setUint32(off, bytes.length >>> 0, true);
2189
+ off += 4;
2190
+ blob.set(bytes, off);
2191
+ off += bytes.length;
2192
+ }
2193
+ return this.g._withStr(
2194
+ JSON.stringify(def),
2195
+ (dp, dl) => this.g._withOptStr(
2196
+ opts?.date,
2197
+ (tp, tl) => this.g._withBytes(
2198
+ blob,
2199
+ (ip, il) => this.ex().gp_set_running_header_footer(this.h, dp, dl, tp, tl, ip, il)
2200
+ )
2201
+ )
2202
+ ) === 0;
2203
+ }
2204
+ /**
2205
+ * The rich {@link RunningHeaderFooter} definition recorded by
2206
+ * {@link setRunningHeaderFooter} (read from the editor-meta sidecar — the
2207
+ * source of truth), or a minimal one reconstructed from a legacy flat
2208
+ * {@link setHeader}/{@link setFooter} bake, or `null` when neither is present.
2209
+ */
2210
+ runningHeaderFooter() {
2211
+ return this.g._jsonOrNull(
2212
+ (o) => this.ex().gp_running_header_footer(this.h, o)
2213
+ );
2214
+ }
2089
2215
  // render
2090
2216
  renderPage(page, scale = 1) {
2091
2217
  return this.g._buffer((o) => this.ex().gp_render_page(this.h, page, scale, o));
@@ -2122,6 +2248,33 @@ var GigaPdfDoc = class {
2122
2248
  )
2123
2249
  );
2124
2250
  }
2251
+ /**
2252
+ * Rasterize a page to a PNG **excluding the ops inside a baked marked-content
2253
+ * span** named `marker` — and, when `skipText` is true, the page's text too — in
2254
+ * a **single** raster pass. For `marker = "GPHF"` (the default) this drops the
2255
+ * baked running header/footer band: the extraction views ({@link elements},
2256
+ * {@link textRuns}) already omit that band (it is never editable body content),
2257
+ * and this render omits it from the picture so it cannot double against an
2258
+ * editable overlay. With `skipText = true` it is the editor's text-free display
2259
+ * background minus the band; with `skipText = false`, the full page minus the
2260
+ * band. Like {@link renderPageNoText}, form-field widget appearances are omitted.
2261
+ */
2262
+ renderPageExcludingMarkedContent(page, scale = 1, skipText = true, marker = "GPHF") {
2263
+ return this.g._buffer(
2264
+ (o) => this.g._withStr(
2265
+ marker,
2266
+ (p, l) => this.ex().gp_render_page_excluding_marked_content(
2267
+ this.h,
2268
+ page,
2269
+ scale,
2270
+ skipText ? 1 : 0,
2271
+ p,
2272
+ l,
2273
+ o
2274
+ )
2275
+ )
2276
+ );
2277
+ }
2125
2278
  // fonts — embed a downloaded font, then add real selectable text
2126
2279
  /**
2127
2280
  * Embed an outline font program as a Type0 font and return its object number