@qrcommunication/gigapdf-lib 0.51.0 → 0.52.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,63 @@ 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.52.1] - 2026-06-18
8
+
9
+ ### Fixed
10
+
11
+ - **JPEG encoder — final-byte padding no longer corrupts the last code.** The
12
+ entropy writer's `flush` padded the trailing partial byte with a fixed 7-bit
13
+ `0x7F`; for any partial byte holding more than one written bit, the extra
14
+ 1-bits bled into the already-written Huffman code (ITU-T T.81 §F.1.2.3
15
+ requires padding *only* the free low bits with 1s). The lib's own decoder
16
+ tolerated it, but strict third-party decoders could misread the last code or
17
+ reject the non-conformant padding. `flush` now pads exactly the free bits.
18
+
19
+ ## [0.52.0] - 2026-06-18
20
+
21
+ ### Added
22
+
23
+ - **Unified editable document model — reconstruction, importers, exporters, edit
24
+ operations, full JS round-trip.** A format-agnostic `model::Document`
25
+ (Section → Page → Block{Paragraph, Heading, List, Table, Image, Shape, TextBox,
26
+ Sheet, Slide} → Inline) that every format imports into and exports from. PDF →
27
+ model via `reconstruct_model` (structural: positioned runs are rebuilt into
28
+ paragraphs, headings, lists and tables, honouring the `/StructTree` tag tree when
29
+ present); Office/HTML/image → model importers; model →
30
+ DOCX/XLSX/PPTX/ODT/ODS/ODP/HTML/RTF/PDF structured exporters (real editable
31
+ content, not a raster). New `model::edit` operations (`ModelOp`: set/restyle run,
32
+ insert/delete/move block, table & sheet cells…) with `apply_ops`. Exposed to JS:
33
+ `toModel`, `officeToModel`, `htmlToModel`, `applyModelOps`, and
34
+ `modelToDocx/Xlsx/Pptx/Odt/Ods/Odp/Html/Rtf/Pdf`. Foundation for editing any
35
+ document format through one editable model.
36
+ - **Text direction & document-language detection (RTL).** `documentLanguage()`
37
+ reports the dominant script and reading direction (Arabic, Hebrew, Latin, CJK…),
38
+ and each text element now carries its `direction` (`ltr`/`rtl`/`neutral`), so
39
+ editors can switch the canvas and layer properties to right-to-left for correct
40
+ editing.
41
+ - **Page margins + running headers/footers.** `pageMargins`/`setPageMargins`
42
+ (CropBox-aware, falling back to the printable content box) and `setHeader`/
43
+ `setFooter` with `{{page}}`/`{{pages}}` tokens, alignment, page ranges and a
44
+ first-page toggle. Baking is idempotent (wrapped in marked content) and
45
+ reversible via `removeHeaders`/`removeFooters`.
46
+ - **Pixel-perfect colour & images.** Full PDF colour-space resolution — Separation,
47
+ DeviceN (with type-0/2/3 functions and a new PostScript type-4 calculator tint
48
+ transform), Indexed, ICCBased (via `/N` + `/Alternate`), CalRGB/CalGray, Lab
49
+ (D50) and accurate CMYK — applied to fills, strokes (`cs`/`CS` + `sc`/`scn`) and
50
+ image XObjects (honouring `/BitsPerComponent` and `/Decode`), fixing
51
+ blank/garbled non-RGB images. **Progressive (SOF2) JPEG** decoding lands in full
52
+ (baseline already supported; arithmetic-coded JPEG is skipped, not blanked).
53
+
54
+ ### Fixed
55
+
56
+ - **Subset-CFF glyphs no longer render as tofu.** Simple `/Type1` fonts embedding a
57
+ CFF program (`/FontFile3 /Type1C`, e.g. subsetted MyriadPro/Nexa) now resolve
58
+ glyphs through the CFF charset (`code → glyph name → gid`) instead of a Unicode
59
+ `cmap` they do not carry; an unresolved code paints nothing rather than a box.
60
+ - **Baseline JPEG images now render.** `/DCTDecode` image XObjects (direct and
61
+ nested inside form XObjects, with `/SMask`) are decoded and blitted — previously
62
+ they were skipped, leaving blank slides and image-shaped holes.
63
+
7
64
  ## [0.51.0] - 2026-06-18
8
65
 
9
66
  ### Added
package/dist/index.cjs CHANGED
@@ -671,6 +671,113 @@ var GigaPdfEngine = class _GigaPdfEngine {
671
671
  )
672
672
  );
673
673
  }
674
+ // ── unified editable model: lower / edit / raise ───────────────────────────
675
+ //
676
+ // The {@link GigaDocument} model is a format-neutral tree (sections → pages →
677
+ // blocks → runs). Lower any format into it (`*ToModel`), edit it with
678
+ // {@link applyModelOps}, then raise it back to any format (`modelTo*`). This is
679
+ // the substrate for a universal editor that edits every format the same way.
680
+ /** Decode a model-JSON buffer returned by a `gp_model_*` export; `null` on an
681
+ * empty (error) result. */
682
+ _modelOrNull(call) {
683
+ const s = this._str(call);
684
+ return s ? JSON.parse(s) : null;
685
+ }
686
+ /**
687
+ * Lower an Office document (DOCX/XLSX/PPTX/ODT/ODS/ODP, auto-detected) into the
688
+ * unified {@link GigaDocument} model. Returns `null` if the bytes aren't a
689
+ * recognized Office container.
690
+ */
691
+ officeToModel(office) {
692
+ return this._withBytes(
693
+ office,
694
+ (p, l) => this._modelOrNull((o) => this.ex.gp_model_from_office(p, l, o))
695
+ );
696
+ }
697
+ /** Lower an HTML string into the unified {@link GigaDocument} model. */
698
+ htmlToModel(html) {
699
+ return this._withStr(
700
+ html,
701
+ (p, l) => JSON.parse(this._str((o) => this.ex.gp_model_from_html(p, l, o)))
702
+ );
703
+ }
704
+ /**
705
+ * Apply a batch of {@link ModelOp} edits to a model and return the edited
706
+ * model. Ops run in order; out-of-range addresses (and any op that can't be
707
+ * parsed) are silently skipped, so a partially-valid batch never throws.
708
+ */
709
+ applyModelOps(model, ops) {
710
+ return this._withStr(
711
+ JSON.stringify(model),
712
+ (mp, ml) => this._withStr(
713
+ JSON.stringify(ops),
714
+ (op, ol) => JSON.parse(this._str((o) => this.ex.gp_model_apply_ops(mp, ml, op, ol, o)))
715
+ )
716
+ );
717
+ }
718
+ /** Raise a {@link GigaDocument} model to an editable Word document (`.docx`). */
719
+ modelToDocx(model) {
720
+ return this._withStr(
721
+ JSON.stringify(model),
722
+ (p, l) => this._buffer((o) => this.ex.gp_model_to_docx(p, l, o))
723
+ );
724
+ }
725
+ /** Raise a model to an Excel workbook (`.xlsx`). */
726
+ modelToXlsx(model) {
727
+ return this._withStr(
728
+ JSON.stringify(model),
729
+ (p, l) => this._buffer((o) => this.ex.gp_model_to_xlsx(p, l, o))
730
+ );
731
+ }
732
+ /** Raise a model to a PowerPoint presentation (`.pptx`). */
733
+ modelToPptx(model) {
734
+ return this._withStr(
735
+ JSON.stringify(model),
736
+ (p, l) => this._buffer((o) => this.ex.gp_model_to_pptx(p, l, o))
737
+ );
738
+ }
739
+ /** Raise a model to an OpenDocument Text (`.odt`). */
740
+ modelToOdt(model) {
741
+ return this._withStr(
742
+ JSON.stringify(model),
743
+ (p, l) => this._buffer((o) => this.ex.gp_model_to_odt(p, l, o))
744
+ );
745
+ }
746
+ /** Raise a model to an OpenDocument Spreadsheet (`.ods`). */
747
+ modelToOds(model) {
748
+ return this._withStr(
749
+ JSON.stringify(model),
750
+ (p, l) => this._buffer((o) => this.ex.gp_model_to_ods(p, l, o))
751
+ );
752
+ }
753
+ /** Raise a model to an OpenDocument Presentation (`.odp`). */
754
+ modelToOdp(model) {
755
+ return this._withStr(
756
+ JSON.stringify(model),
757
+ (p, l) => this._buffer((o) => this.ex.gp_model_to_odp(p, l, o))
758
+ );
759
+ }
760
+ /** Raise a model back to a PDF. */
761
+ modelToPdf(model) {
762
+ return this._withStr(
763
+ JSON.stringify(model),
764
+ (p, l) => this._buffer((o) => this.ex.gp_model_to_pdf(p, l, o))
765
+ );
766
+ }
767
+ /** Raise a model to standalone HTML (decoded UTF-8 string). */
768
+ modelToHtml(model) {
769
+ return this._withStr(
770
+ JSON.stringify(model),
771
+ (p, l) => this._str((o) => this.ex.gp_model_to_html(p, l, o))
772
+ );
773
+ }
774
+ /** Raise a model to RTF (decoded UTF-8 string). */
775
+ modelToRtf(model) {
776
+ return this._withStr(
777
+ JSON.stringify(model),
778
+ (p, l) => this._str((o) => this.ex.gp_model_to_rtf(p, l, o))
779
+ );
780
+ }
674
781
  };
675
782
  function packHtmlResources(resources) {
676
783
  let size = 4;
@@ -774,6 +881,24 @@ var GigaPdfDoc = class {
774
881
  textElements(page) {
775
882
  return this.g._json((o) => this.ex().gp_text_elements_json(this.h, page, o));
776
883
  }
884
+ /**
885
+ * The document's aggregate language signal — its dominant reading direction
886
+ * (`ltr`/`rtl`/`neutral`), writing system, and a best-effort ISO-639-1
887
+ * language code — computed across every page's decoded text. Lets a host
888
+ * pick fonts, set the UI direction or label the document without its own bidi
889
+ * pass (e.g. detect an Arabic/Hebrew/Japanese PDF). `lang` is omitted when the
890
+ * script does not pin a single language.
891
+ */
892
+ documentLanguage() {
893
+ const raw = this.g._json(
894
+ (o) => this.ex().gp_document_language(this.h, o)
895
+ );
896
+ return {
897
+ direction: raw.direction,
898
+ script: raw.script,
899
+ ...raw.lang != null ? { lang: raw.lang } : {}
900
+ };
901
+ }
777
902
  /**
778
903
  * Every image element on a page: its placement box (user space, bottom-left),
779
904
  * the embeddable encoded bytes (`data`) + `format` (`jpeg`/`png`/`jp2`/
@@ -1022,6 +1147,40 @@ var GigaPdfDoc = class {
1022
1147
  pageInfo(page) {
1023
1148
  return this.g._json((o) => this.ex().gp_page_info_json(this.h, page, o));
1024
1149
  }
1150
+ // margins + running header/footer
1151
+ /**
1152
+ * A page's margins (points): the gap between `/CropBox` and `/MediaBox` when a
1153
+ * CropBox exists, else estimated from the content bounding box.
1154
+ */
1155
+ pageMargins(page) {
1156
+ return this.g._json((o) => this.ex().gp_page_margins(this.h, page, o));
1157
+ }
1158
+ /**
1159
+ * Set a page's margins (points) by insetting its `/CropBox` from the `/MediaBox`
1160
+ * — a real, visible margin change. Returns `true` on success.
1161
+ */
1162
+ setPageMargins(page, m) {
1163
+ return this.ex().gp_set_page_margins(this.h, page, m.top, m.right, m.bottom, m.left) === 0;
1164
+ }
1165
+ /**
1166
+ * Bake a running header onto every in-range page (idempotent: re-baking
1167
+ * replaces the prior header). Returns `true` on success.
1168
+ */
1169
+ setHeader(spec) {
1170
+ return this.g._withStr(JSON.stringify(spec), (p, l) => this.ex().gp_set_header(this.h, p, l)) === 0;
1171
+ }
1172
+ /** Bake a running footer onto every in-range page (idempotent). */
1173
+ setFooter(spec) {
1174
+ return this.g._withStr(JSON.stringify(spec), (p, l) => this.ex().gp_set_footer(this.h, p, l)) === 0;
1175
+ }
1176
+ /** Remove every previously-baked running header from all pages. */
1177
+ removeHeaders() {
1178
+ return this.ex().gp_remove_headers(this.h) === 0;
1179
+ }
1180
+ /** Remove every previously-baked running footer from all pages. */
1181
+ removeFooters() {
1182
+ return this.ex().gp_remove_footers(this.h) === 0;
1183
+ }
1025
1184
  // render
1026
1185
  renderPage(page, scale = 1) {
1027
1186
  return this.g._buffer((o) => this.ex().gp_render_page(this.h, page, scale, o));
@@ -1147,6 +1306,17 @@ var GigaPdfDoc = class {
1147
1306
  toHtml() {
1148
1307
  return this.g._str((o) => this.ex().gp_to_html(this.h, o));
1149
1308
  }
1309
+ /**
1310
+ * Reconstruct this PDF into the **unified editable model** — the
1311
+ * format-neutral {@link GigaDocument} tree (sections → pages → blocks → runs)
1312
+ * that every format lowers into. Edit it with
1313
+ * {@link GigaPdfEngine.applyModelOps}, then export it to any target with
1314
+ * {@link GigaPdfEngine.modelToDocx} / {@link GigaPdfEngine.modelToPdf} / … —
1315
+ * the foundation for editing any document indifferently of its source format.
1316
+ */
1317
+ toModel() {
1318
+ return JSON.parse(this.g._str((o) => this.ex().gp_model_from_pdf(this.h, o)));
1319
+ }
1150
1320
  toDocx() {
1151
1321
  return this.g._buffer((o) => this.ex().gp_to_docx(this.h, o));
1152
1322
  }