@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/dist/index.d.cts CHANGED
@@ -271,6 +271,41 @@ declare class GigaPdfEngine {
271
271
  * inlined and never listed.
272
272
  */
273
273
  htmlNeededResources(html: string, header?: string, footer?: string): HtmlResourceNeed[];
274
+ /** Decode a model-JSON buffer returned by a `gp_model_*` export; `null` on an
275
+ * empty (error) result. */
276
+ private _modelOrNull;
277
+ /**
278
+ * Lower an Office document (DOCX/XLSX/PPTX/ODT/ODS/ODP, auto-detected) into the
279
+ * unified {@link GigaDocument} model. Returns `null` if the bytes aren't a
280
+ * recognized Office container.
281
+ */
282
+ officeToModel(office: Uint8Array): GigaDocument | null;
283
+ /** Lower an HTML string into the unified {@link GigaDocument} model. */
284
+ htmlToModel(html: string): GigaDocument;
285
+ /**
286
+ * Apply a batch of {@link ModelOp} edits to a model and return the edited
287
+ * model. Ops run in order; out-of-range addresses (and any op that can't be
288
+ * parsed) are silently skipped, so a partially-valid batch never throws.
289
+ */
290
+ applyModelOps(model: GigaDocument, ops: ModelOp[]): GigaDocument;
291
+ /** Raise a {@link GigaDocument} model to an editable Word document (`.docx`). */
292
+ modelToDocx(model: GigaDocument): Uint8Array;
293
+ /** Raise a model to an Excel workbook (`.xlsx`). */
294
+ modelToXlsx(model: GigaDocument): Uint8Array;
295
+ /** Raise a model to a PowerPoint presentation (`.pptx`). */
296
+ modelToPptx(model: GigaDocument): Uint8Array;
297
+ /** Raise a model to an OpenDocument Text (`.odt`). */
298
+ modelToOdt(model: GigaDocument): Uint8Array;
299
+ /** Raise a model to an OpenDocument Spreadsheet (`.ods`). */
300
+ modelToOds(model: GigaDocument): Uint8Array;
301
+ /** Raise a model to an OpenDocument Presentation (`.odp`). */
302
+ modelToOdp(model: GigaDocument): Uint8Array;
303
+ /** Raise a model back to a PDF. */
304
+ modelToPdf(model: GigaDocument): Uint8Array;
305
+ /** Raise a model to standalone HTML (decoded UTF-8 string). */
306
+ modelToHtml(model: GigaDocument): string;
307
+ /** Raise a model to RTF (decoded UTF-8 string). */
308
+ modelToRtf(model: GigaDocument): string;
274
309
  }
275
310
  /** A Google font the HTML engine needs (resolved from the catalogue). */
276
311
  interface HtmlFontRequest {
@@ -396,7 +431,206 @@ interface TextElementInfo {
396
431
  color: [number, number, number];
397
432
  /** Baseline rotation in degrees (0 = upright). */
398
433
  rotation: number;
434
+ /**
435
+ * Reading direction of this run by its strong characters: `"rtl"` for
436
+ * Arabic/Hebrew, `"ltr"` for Latin/Greek/Cyrillic/CJK, `"neutral"` when the
437
+ * run is only digits/punctuation/whitespace.
438
+ */
439
+ direction: 'ltr' | 'rtl' | 'neutral';
440
+ }
441
+ /**
442
+ * The aggregate language signal of a document from
443
+ * {@link GigaPdfDoc.documentLanguage}: its dominant reading {@link
444
+ * TextElementInfo.direction | direction}, writing system, and a best-effort
445
+ * ISO-639-1 language code (`"ar"`, `"he"`, `"zh"`/`"ja"`…), `undefined` when
446
+ * the script does not pin a single language (e.g. plain Latin).
447
+ */
448
+ interface DocumentLanguage {
449
+ direction: 'ltr' | 'rtl' | 'neutral';
450
+ /** Dominant script: `"arabic" | "hebrew" | "latin" | "greek" | "cyrillic" | "cjk" | "other"`. */
451
+ script: string;
452
+ /** Best-effort ISO-639-1 code, or `undefined` when undecidable. */
453
+ lang?: string;
454
+ }
455
+ /** A portable font fallback class (mirrors `convert::style::Generic`). */
456
+ type GigaGeneric = 'sans' | 'serif' | 'mono';
457
+ /** A run's character style (mirror of `model::style::CharStyle`'s JSON). */
458
+ interface GigaCharStyle {
459
+ family: string;
460
+ generic: GigaGeneric;
461
+ size_pt: number;
462
+ bold: boolean;
463
+ italic: boolean;
464
+ underline: boolean;
465
+ strike: boolean;
466
+ /** RGB `0..=1`, or `null` for default black. */
467
+ color: [number, number, number] | null;
468
+ valign: 'baseline' | 'super' | 'sub';
469
+ }
470
+ /** An inline node (tagged): a styled run, a line break, an image, or a link. */
471
+ type GigaInline = {
472
+ t: 'run';
473
+ text: string;
474
+ style: GigaCharStyle;
475
+ source_index: number | null;
476
+ } | {
477
+ t: 'break';
478
+ } | {
479
+ t: 'image';
480
+ [k: string]: unknown;
481
+ } | {
482
+ t: 'link';
483
+ [k: string]: unknown;
484
+ };
485
+ /** A block payload (tagged by `t`); only the common shapes are spelled out. */
486
+ interface GigaBlockKind {
487
+ t: 'paragraph' | 'heading' | 'list' | 'table' | 'image' | 'shape' | 'textbox' | 'sheet' | 'slide';
488
+ /** The variant body — e.g. a paragraph carries `{ runs: GigaInline[], … }`. */
489
+ v?: unknown;
490
+ }
491
+ /** A block: a stable id, an optional placement frame + rotation, and its kind. */
492
+ interface GigaBlock {
493
+ id: number;
494
+ frame: {
495
+ x: number;
496
+ y: number;
497
+ w: number;
498
+ h: number;
499
+ } | null;
500
+ rotation: {
501
+ t: 'd0' | 'd90' | 'd180' | 'd270' | 'deg';
502
+ v?: number;
503
+ };
504
+ kind: GigaBlockKind;
505
+ }
506
+ /** A page: a list of blocks; `absolute` flags slide/form (positioned) layout. */
507
+ interface GigaPage {
508
+ blocks: GigaBlock[];
509
+ absolute: boolean;
510
+ }
511
+ /** A section: one page geometry, optional running header/footer, and its pages. */
512
+ interface GigaSection {
513
+ geometry: {
514
+ width: number;
515
+ height: number;
516
+ [k: string]: unknown;
517
+ };
518
+ header: GigaBlock[] | null;
519
+ footer: GigaBlock[] | null;
520
+ pages: GigaPage[];
399
521
  }
522
+ /**
523
+ * The unified editable document model — the format-neutral tree every format
524
+ * lowers into and is reconstructed from. `v` is the envelope version. Leaves
525
+ * beyond what a host typically edits (`styles`, `outline`, `resources`) are
526
+ * carried opaquely so a round-trip preserves them.
527
+ */
528
+ interface GigaDocument {
529
+ v: number;
530
+ meta: {
531
+ title: string | null;
532
+ author: string | null;
533
+ subject: string | null;
534
+ keywords: string[];
535
+ lang: string | null;
536
+ };
537
+ styles: unknown;
538
+ sections: GigaSection[];
539
+ outline: unknown[];
540
+ resources: unknown;
541
+ }
542
+ /**
543
+ * A positional block address `[section, page, index]` (all zero-based) — the
544
+ * stable handle an edit op uses to target a block (mirrors `model::BlockAddr`).
545
+ */
546
+ type GigaBlockAddr = [section: number, page: number, index: number];
547
+ /**
548
+ * A character-style patch for restyle/insert ops: only the present fields are
549
+ * applied (mirror of `model::edit::StylePatch`). `color: null` clears the colour
550
+ * (→ default black); omitting `color` leaves it unchanged.
551
+ */
552
+ interface GigaStylePatch {
553
+ family?: string;
554
+ generic?: GigaGeneric;
555
+ size_pt?: number;
556
+ bold?: boolean;
557
+ italic?: boolean;
558
+ underline?: boolean;
559
+ strike?: boolean;
560
+ color?: [number, number, number] | null;
561
+ }
562
+ /** A typed spreadsheet cell value (tagged; mirror of `model::CellValue`). */
563
+ type GigaCellValue = {
564
+ t: 'empty';
565
+ } | {
566
+ t: 'text';
567
+ v: string;
568
+ } | {
569
+ t: 'number';
570
+ v: number;
571
+ } | {
572
+ t: 'bool';
573
+ v: boolean;
574
+ };
575
+ /**
576
+ * A single editing operation against a {@link GigaDocument} model, mirroring the
577
+ * JSON shape of `model::edit::ModelOp`. Pass an array to
578
+ * {@link GigaPdfEngine.applyModelOps}; ops run in order and out-of-range
579
+ * addresses are no-ops.
580
+ */
581
+ type ModelOp = {
582
+ op: 'setRunText';
583
+ addr: GigaBlockAddr;
584
+ run: number;
585
+ text: string;
586
+ } | {
587
+ op: 'restyleRun';
588
+ addr: GigaBlockAddr;
589
+ run: number;
590
+ style: GigaStylePatch;
591
+ } | {
592
+ op: 'insertRun';
593
+ addr: GigaBlockAddr;
594
+ run: number;
595
+ text: string;
596
+ style?: GigaStylePatch;
597
+ } | {
598
+ op: 'deleteRun';
599
+ addr: GigaBlockAddr;
600
+ run: number;
601
+ } | {
602
+ op: 'insertBlock';
603
+ addr: GigaBlockAddr;
604
+ block: GigaBlock;
605
+ } | {
606
+ op: 'deleteBlock';
607
+ addr: GigaBlockAddr;
608
+ } | {
609
+ op: 'moveBlock';
610
+ addr: GigaBlockAddr;
611
+ to: GigaBlockAddr;
612
+ } | {
613
+ op: 'setBlockText';
614
+ addr: GigaBlockAddr;
615
+ text: string;
616
+ } | {
617
+ op: 'restyleBlock';
618
+ addr: GigaBlockAddr;
619
+ style: GigaStylePatch;
620
+ } | {
621
+ op: 'setCellText';
622
+ addr: GigaBlockAddr;
623
+ row: number;
624
+ col: number;
625
+ text: string;
626
+ } | {
627
+ op: 'setSheetCell';
628
+ addr: GigaBlockAddr;
629
+ sheet: number;
630
+ row: number;
631
+ col: number;
632
+ value: GigaCellValue;
633
+ };
400
634
  /**
401
635
  * An image element from {@link GigaPdfDoc.imageElements}: its placement box
402
636
  * (page user space, origin bottom-left), the embeddable encoded bytes + format,
@@ -629,6 +863,37 @@ interface PageInfo {
629
863
  /** Raw `/MediaBox` `[x0, y0, x1, y1]` in user-space points (preserves origin). */
630
864
  mediaBox: [number, number, number, number];
631
865
  }
866
+ /** Per-side page margins, in points. */
867
+ interface PageMargins {
868
+ top: number;
869
+ right: number;
870
+ bottom: number;
871
+ left: number;
872
+ }
873
+ /** Horizontal alignment of header/footer text within the printable width. */
874
+ type HeaderFooterAlign = "left" | "center" | "right";
875
+ /**
876
+ * A running header/footer to bake onto an existing PDF (see {@link GigaPdfDoc.setHeader} /
877
+ * {@link GigaPdfDoc.setFooter}). `text` may contain the tokens `{{page}}` (1-based page
878
+ * number) and `{{pages}}` (total page count), substituted per page. Text is drawn in
879
+ * standard Helvetica inside the top (header) / bottom (footer) margin band.
880
+ */
881
+ interface HeaderFooterSpec {
882
+ /** Template text, with `{{page}}` / `{{pages}}` tokens. */
883
+ text: string;
884
+ /** Horizontal alignment (default `"left"`). */
885
+ align?: HeaderFooterAlign;
886
+ /** Font size in points (default `10`). */
887
+ fontSize?: number;
888
+ /** RGB fill colour, `0..1` per channel (default black `[0,0,0]`). */
889
+ color?: [number, number, number];
890
+ /** Inclusive 1-based page range `[first, last]`; omit for every page. */
891
+ pageRange?: [number, number] | null;
892
+ /** Draw on the first page of the range too (default `true`). */
893
+ showOnFirstPage?: boolean;
894
+ /** Band height from the page edge, in points (default `36`). */
895
+ bandHeight?: number;
896
+ }
632
897
  /** Visual styling for a newly-created form field. */
633
898
  interface FieldStyle {
634
899
  /** Text size in points; `0` (default) auto-sizes to the field box. */
@@ -670,6 +935,15 @@ declare class GigaPdfDoc {
670
935
  * for a reader's per-run text layer (which `elements()` omits font + colour).
671
936
  */
672
937
  textElements(page: number): TextElementInfo[];
938
+ /**
939
+ * The document's aggregate language signal — its dominant reading direction
940
+ * (`ltr`/`rtl`/`neutral`), writing system, and a best-effort ISO-639-1
941
+ * language code — computed across every page's decoded text. Lets a host
942
+ * pick fonts, set the UI direction or label the document without its own bidi
943
+ * pass (e.g. detect an Arabic/Hebrew/Japanese PDF). `lang` is omitted when the
944
+ * script does not pin a single language.
945
+ */
946
+ documentLanguage(): DocumentLanguage;
673
947
  /**
674
948
  * Every image element on a page: its placement box (user space, bottom-left),
675
949
  * the embeddable encoded bytes (`data`) + `format` (`jpeg`/`png`/`jp2`/
@@ -768,6 +1042,27 @@ declare class GigaPdfDoc {
768
1042
  copyPage(page: number): number;
769
1043
  /** A page's size (points) and `/Rotate` (0/90/180/270). */
770
1044
  pageInfo(page: number): PageInfo;
1045
+ /**
1046
+ * A page's margins (points): the gap between `/CropBox` and `/MediaBox` when a
1047
+ * CropBox exists, else estimated from the content bounding box.
1048
+ */
1049
+ pageMargins(page: number): PageMargins;
1050
+ /**
1051
+ * Set a page's margins (points) by insetting its `/CropBox` from the `/MediaBox`
1052
+ * — a real, visible margin change. Returns `true` on success.
1053
+ */
1054
+ setPageMargins(page: number, m: PageMargins): boolean;
1055
+ /**
1056
+ * Bake a running header onto every in-range page (idempotent: re-baking
1057
+ * replaces the prior header). Returns `true` on success.
1058
+ */
1059
+ setHeader(spec: HeaderFooterSpec): boolean;
1060
+ /** Bake a running footer onto every in-range page (idempotent). */
1061
+ setFooter(spec: HeaderFooterSpec): boolean;
1062
+ /** Remove every previously-baked running header from all pages. */
1063
+ removeHeaders(): boolean;
1064
+ /** Remove every previously-baked running footer from all pages. */
1065
+ removeFooters(): boolean;
771
1066
  renderPage(page: number, scale?: number): Uint8Array;
772
1067
  /**
773
1068
  * Embed an outline font program as a Type0 font and return its object number
@@ -823,6 +1118,15 @@ declare class GigaPdfDoc {
823
1118
  embeddedFonts(): EmbeddedFont[];
824
1119
  toText(): string;
825
1120
  toHtml(): string;
1121
+ /**
1122
+ * Reconstruct this PDF into the **unified editable model** — the
1123
+ * format-neutral {@link GigaDocument} tree (sections → pages → blocks → runs)
1124
+ * that every format lowers into. Edit it with
1125
+ * {@link GigaPdfEngine.applyModelOps}, then export it to any target with
1126
+ * {@link GigaPdfEngine.modelToDocx} / {@link GigaPdfEngine.modelToPdf} / … —
1127
+ * the foundation for editing any document indifferently of its source format.
1128
+ */
1129
+ toModel(): GigaDocument;
826
1130
  toDocx(): Uint8Array;
827
1131
  toPptx(): Uint8Array;
828
1132
  /** Convert to an editable OpenDocument Presentation (`.odp`). */
@@ -998,4 +1302,4 @@ declare class GigaPdfDoc {
998
1302
  }): boolean;
999
1303
  }
1000
1304
 
1001
- export { ALL_OCR_SCRIPTS, type AnnotationInfo, type Attachment, type Box, type DecodedImage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, GigaPdfDoc, GigaPdfEngine, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type NamedDest, type OcrScript, type OcrWord, type OutlineEntry, type PageInfo, type PathSegment, type RadioOption, type SearchHit, type SignP12Options, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet };
1305
+ export { ALL_OCR_SCRIPTS, type AnnotationInfo, type Attachment, type Box, type DecodedImage, type DocumentLanguage, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, type GigaBlock, type GigaBlockAddr, type GigaBlockKind, type GigaCellValue, type GigaCharStyle, type GigaDocument, type GigaGeneric, type GigaInline, type GigaPage, GigaPdfDoc, GigaPdfEngine, type GigaSection, type GigaStylePatch, type HeaderFooterAlign, type HeaderFooterSpec, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type HtmlResource, type HtmlResourceNeed, type ImageElementInfo, type LayerInfo, type LinkInfo, type ModelOp, type NamedDest, type OcrScript, type OcrWord, type OutlineEntry, type PageInfo, type PageMargins, type PathSegment, type RadioOption, type SearchHit, type SignP12Options, type TextElementInfo, type TextLine, type TextRunInfo, type VectorPathInfo, type XlsxSheet };