@qrcommunication/gigapdf-lib 0.16.0 → 0.18.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/dist/index.d.ts CHANGED
@@ -184,6 +184,17 @@ interface FontInfo {
184
184
  google: boolean;
185
185
  weights: number[];
186
186
  }
187
+ /** A font embedded in a document (from {@link GigaPdfDoc.embeddedFonts}). */
188
+ interface EmbeddedFont {
189
+ /** The `/BaseFont` name (may carry a `ABCDEF+` subset prefix). */
190
+ baseFont: string;
191
+ /**
192
+ * Embedded program format. `truetype` (glyf) and a full OpenType `cff`
193
+ * (`OTTO`) re-embed directly via {@link GigaPdfDoc.embedFont}; bare `cff`
194
+ * (Type1C) and `type1` are read-only here.
195
+ */
196
+ format: "truetype" | "cff" | "type1";
197
+ }
187
198
  interface Box {
188
199
  x: number;
189
200
  y: number;
@@ -273,6 +284,11 @@ interface OutlineEntry {
273
284
  title: string;
274
285
  page?: number;
275
286
  }
287
+ /** A named destination from {@link GigaPdfDoc.namedDests}: a name → page anchor. */
288
+ interface NamedDest {
289
+ name: string;
290
+ page: number;
291
+ }
276
292
  /** An optional-content layer (calque): toggle `visible`/`locked` to persist in the PDF. */
277
293
  interface LayerInfo {
278
294
  id: number;
@@ -324,6 +340,12 @@ declare class GigaPdfDoc {
324
340
  /** OCR a (scanned) page → words with PDF-space boxes. `scale` ≥ 2 for small text. */
325
341
  ocr(page: number, scale?: number): OcrWord[];
326
342
  ocrText(page: number, scale?: number): string;
343
+ /**
344
+ * Replace text run `index` on `page` with `text`. Font-aware: works with
345
+ * **any** font — a run set in an embedded Type0/Identity-H face (TrueType or
346
+ * OpenType-CFF) is re-encoded through that font's char→glyph map; base-14 and
347
+ * simple fonts use WinAnsi. Returns `false` if the run/page doesn't exist.
348
+ */
327
349
  replaceText(page: number, index: number, text: string): boolean;
328
350
  removeElement(page: number, index: number): boolean;
329
351
  moveElement(page: number, index: number, dx: number, dy: number): boolean;
@@ -396,8 +418,33 @@ declare class GigaPdfDoc {
396
418
  /** A page's size (points) and `/Rotate` (0/90/180/270). */
397
419
  pageInfo(page: number): PageInfo;
398
420
  renderPage(page: number, scale?: number): Uint8Array;
399
- embedFont(family: string, ttf: Uint8Array): number;
421
+ /**
422
+ * Embed an outline font program as a Type0 font and return its object number
423
+ * (pass to {@link addText} / re-encoded by {@link replaceText}). Accepts
424
+ * **any** font file — glyf **TrueType** (`.ttf`) or **OpenType-CFF**
425
+ * (`.otf`/`OTTO`), flavour auto-detected — so `font` may be a Google Font the
426
+ * host fetched, an `.otf` you supply, or a face pulled out of a document with
427
+ * {@link extractFont}. Returns 0 on a malformed/unsupported program.
428
+ */
429
+ embedFont(family: string, font: Uint8Array): number;
430
+ /**
431
+ * Draw real, selectable text at `(x, y)` (PDF points, origin bottom-left) in a
432
+ * font embedded with {@link embedFont} (`fontObj`). Works with **any** embedded
433
+ * face — glyf TrueType or OpenType-CFF — encoding each character through the
434
+ * font's char→glyph map (Identity-H, 2-byte glyph ids). `rgb` is packed
435
+ * `0xRRGGBB`; `rotationDeg` rotates CCW about `(x, y)`. For a built-in base-14
436
+ * family with no embedding, use {@link addStandardText}.
437
+ */
400
438
  addText(page: number, x: number, y: number, size: number, text: string, fontObj: number, rgb?: number, opacity?: number, rotationDeg?: number): boolean;
439
+ /**
440
+ * Draw `text` at `(x, y)` in a built-in **base-14 standard font** (`fontName`,
441
+ * e.g. `"Times-Bold"`, `"Courier-Oblique"`, `"Symbol"`) — no embedding needed,
442
+ * every viewer ships these 14. For any other family embed a TrueType with
443
+ * {@link embedFont} (a Google Font fetched by the host, or one pulled out of
444
+ * the document with {@link extractFont}) and use {@link addText}. Returns
445
+ * `false` on an unknown font name or bad page.
446
+ */
447
+ addStandardText(page: number, x: number, y: number, size: number, text: string, fontName: string, rgb?: number, opacity?: number, rotationDeg?: number): boolean;
401
448
  /**
402
449
  * Stamp a standard-**Helvetica** watermark (no font embed needed): `text` at
403
450
  * `(x, y)`, rotated `rotationDeg`° counter-clockwise, `rgb` packed `0xRRGGBB`,
@@ -416,6 +463,13 @@ declare class GigaPdfDoc {
416
463
  format: "truetype" | "cff" | "type1";
417
464
  bytes: Uint8Array;
418
465
  } | null;
466
+ /**
467
+ * The fonts **embedded** in the document — each `{ baseFont, format }`. Pair
468
+ * with {@link extractFont} to pull a font's bytes out and re-embed them via
469
+ * {@link embedFont}, e.g. to draw new text (with {@link addText}) in a face
470
+ * the document already carries — no external font file needed.
471
+ */
472
+ embeddedFonts(): EmbeddedFont[];
419
473
  toText(): string;
420
474
  toHtml(): string;
421
475
  toDocx(): Uint8Array;
@@ -501,6 +555,23 @@ declare class GigaPdfDoc {
501
555
  links(page: number): LinkInfo[];
502
556
  addUriLink(page: number, x0: number, y0: number, x1: number, y1: number, uri: string): boolean;
503
557
  addGotoLink(page: number, x0: number, y0: number, x1: number, y1: number, targetPage: number): boolean;
558
+ /**
559
+ * Register a named destination `name` → `targetPage` (a whole-page `/Fit`
560
+ * view) in the catalog. Links and bookmarks can then jump by name via
561
+ * {@link addGotoLinkNamed}; because resolution goes through the catalog (not a
562
+ * frozen page number), the anchor survives page extraction/split as long as
563
+ * its page is kept. Re-using a `name` overwrites its target.
564
+ */
565
+ addNamedDest(name: string, targetPage: number): boolean;
566
+ /** The catalog's named destinations as `{name, page}` pairs. */
567
+ namedDests(): NamedDest[];
568
+ /**
569
+ * Add an internal hyperlink over a rectangle that jumps to the named
570
+ * destination `name` (define it with {@link addNamedDest}). Unlike
571
+ * {@link addGotoLink} (an explicit page), this stores `/Dest /name`, keeping
572
+ * cross-references intact through split/extract.
573
+ */
574
+ addGotoLinkNamed(page: number, x0: number, y0: number, x1: number, y1: number, name: string): boolean;
504
575
  layers(): LayerInfo[];
505
576
  /** Create a new (visible, unlocked) layer; returns its id (0 on error). */
506
577
  addLayer(name: string): number;
@@ -554,4 +625,4 @@ declare class GigaPdfDoc {
554
625
  }): boolean;
555
626
  }
556
627
 
557
- export { type AnnotationInfo, type Box, type Element, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, GigaPdfDoc, GigaPdfEngine, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type LayerInfo, type LinkInfo, type OcrWord, type OutlineEntry, type PageInfo, type RadioOption, type SearchHit, type SignP12Options, type TextLine, type TextRunInfo };
628
+ export { type AnnotationInfo, type Box, type Element, type EmbeddedFont, type FieldInfo, type FieldKind, type FieldStyle, type FontInfo, GigaPdfDoc, GigaPdfEngine, type HtmlFont, type HtmlFontRequest, type HtmlMargins, type HtmlRenderOptions, type LayerInfo, type LinkInfo, type NamedDest, type OcrWord, type OutlineEntry, type PageInfo, type RadioOption, type SearchHit, type SignP12Options, type TextLine, type TextRunInfo };
package/dist/index.js CHANGED
@@ -422,6 +422,12 @@ var GigaPdfDoc = class {
422
422
  return this.g._str((o) => this.ex().gp_ocr_text(this.h, page, scale, o));
423
423
  }
424
424
  // editing
425
+ /**
426
+ * Replace text run `index` on `page` with `text`. Font-aware: works with
427
+ * **any** font — a run set in an embedded Type0/Identity-H face (TrueType or
428
+ * OpenType-CFF) is re-encoded through that font's char→glyph map; base-14 and
429
+ * simple fonts use WinAnsi. Returns `false` if the run/page doesn't exist.
430
+ */
425
431
  replaceText(page, index, text) {
426
432
  return this.g._withStr(text, (p, l) => this.ex().gp_replace_text(this.h, page, index, p, l)) === 0;
427
433
  }
@@ -625,17 +631,33 @@ var GigaPdfDoc = class {
625
631
  renderPage(page, scale = 1) {
626
632
  return this.g._buffer((o) => this.ex().gp_render_page(this.h, page, scale, o));
627
633
  }
628
- // fonts — embed a downloaded TTF, then add real selectable text
629
- embedFont(family, ttf) {
634
+ // fonts — embed a downloaded font, then add real selectable text
635
+ /**
636
+ * Embed an outline font program as a Type0 font and return its object number
637
+ * (pass to {@link addText} / re-encoded by {@link replaceText}). Accepts
638
+ * **any** font file — glyf **TrueType** (`.ttf`) or **OpenType-CFF**
639
+ * (`.otf`/`OTTO`), flavour auto-detected — so `font` may be a Google Font the
640
+ * host fetched, an `.otf` you supply, or a face pulled out of a document with
641
+ * {@link extractFont}. Returns 0 on a malformed/unsupported program.
642
+ */
643
+ embedFont(family, font) {
630
644
  const b = enc.encode(family);
631
645
  const famPtr = this.g._toWasm(b);
632
646
  const obj = this.g._withBytes(
633
- ttf,
647
+ font,
634
648
  (p, l) => this.ex().gp_embed_font(this.h, famPtr, b.length, p, l)
635
649
  );
636
650
  this.g._free(famPtr, b.length);
637
651
  return obj;
638
652
  }
653
+ /**
654
+ * Draw real, selectable text at `(x, y)` (PDF points, origin bottom-left) in a
655
+ * font embedded with {@link embedFont} (`fontObj`). Works with **any** embedded
656
+ * face — glyf TrueType or OpenType-CFF — encoding each character through the
657
+ * font's char→glyph map (Identity-H, 2-byte glyph ids). `rgb` is packed
658
+ * `0xRRGGBB`; `rotationDeg` rotates CCW about `(x, y)`. For a built-in base-14
659
+ * family with no embedding, use {@link addStandardText}.
660
+ */
639
661
  addText(page, x, y, size, text, fontObj, rgb = 0, opacity = 1, rotationDeg = 0) {
640
662
  return this.g._withStr(
641
663
  text,
@@ -654,6 +676,36 @@ var GigaPdfDoc = class {
654
676
  )
655
677
  ) === 0;
656
678
  }
679
+ /**
680
+ * Draw `text` at `(x, y)` in a built-in **base-14 standard font** (`fontName`,
681
+ * e.g. `"Times-Bold"`, `"Courier-Oblique"`, `"Symbol"`) — no embedding needed,
682
+ * every viewer ships these 14. For any other family embed a TrueType with
683
+ * {@link embedFont} (a Google Font fetched by the host, or one pulled out of
684
+ * the document with {@link extractFont}) and use {@link addText}. Returns
685
+ * `false` on an unknown font name or bad page.
686
+ */
687
+ addStandardText(page, x, y, size, text, fontName, rgb = 0, opacity = 1, rotationDeg = 0) {
688
+ return this.g._withStr(
689
+ text,
690
+ (tp, tl) => this.g._withStr(
691
+ fontName,
692
+ (fp, fl) => this.ex().gp_add_text_standard(
693
+ this.h,
694
+ page,
695
+ x,
696
+ y,
697
+ size,
698
+ tp,
699
+ tl,
700
+ fp,
701
+ fl,
702
+ RGB(rgb),
703
+ opacity,
704
+ rotationDeg
705
+ )
706
+ )
707
+ ) === 0;
708
+ }
657
709
  /**
658
710
  * Stamp a standard-**Helvetica** watermark (no font embed needed): `text` at
659
711
  * `(x, y)`, rotated `rotationDeg`° counter-clockwise, `rgb` packed `0xRRGGBB`,
@@ -684,6 +736,15 @@ var GigaPdfDoc = class {
684
736
  const format = buf[0] === 1 ? "truetype" : buf[0] === 2 ? "cff" : "type1";
685
737
  return { format, bytes: buf.subarray(1) };
686
738
  }
739
+ /**
740
+ * The fonts **embedded** in the document — each `{ baseFont, format }`. Pair
741
+ * with {@link extractFont} to pull a font's bytes out and re-embed them via
742
+ * {@link embedFont}, e.g. to draw new text (with {@link addText}) in a face
743
+ * the document already carries — no external font file needed.
744
+ */
745
+ embeddedFonts() {
746
+ return this.g._json((o) => this.ex().gp_embedded_fonts_json(this.h, o));
747
+ }
687
748
  // convert PDF → X
688
749
  toText() {
689
750
  return this.g._str((o) => this.ex().gp_to_text(this.h, o));
@@ -958,6 +1019,32 @@ var GigaPdfDoc = class {
958
1019
  addGotoLink(page, x0, y0, x1, y1, targetPage) {
959
1020
  return this.ex().gp_add_goto_link(this.h, page, x0, y0, x1, y1, targetPage) === 0;
960
1021
  }
1022
+ /**
1023
+ * Register a named destination `name` → `targetPage` (a whole-page `/Fit`
1024
+ * view) in the catalog. Links and bookmarks can then jump by name via
1025
+ * {@link addGotoLinkNamed}; because resolution goes through the catalog (not a
1026
+ * frozen page number), the anchor survives page extraction/split as long as
1027
+ * its page is kept. Re-using a `name` overwrites its target.
1028
+ */
1029
+ addNamedDest(name, targetPage) {
1030
+ return this.g._withStr(name, (p, l) => this.ex().gp_add_named_dest(this.h, p, l, targetPage)) === 0;
1031
+ }
1032
+ /** The catalog's named destinations as `{name, page}` pairs. */
1033
+ namedDests() {
1034
+ return this.g._json((o) => this.ex().gp_named_dests_json(this.h, o));
1035
+ }
1036
+ /**
1037
+ * Add an internal hyperlink over a rectangle that jumps to the named
1038
+ * destination `name` (define it with {@link addNamedDest}). Unlike
1039
+ * {@link addGotoLink} (an explicit page), this stores `/Dest /name`, keeping
1040
+ * cross-references intact through split/extract.
1041
+ */
1042
+ addGotoLinkNamed(page, x0, y0, x1, y1, name) {
1043
+ return this.g._withStr(
1044
+ name,
1045
+ (p, l) => this.ex().gp_add_goto_link_named(this.h, page, x0, y0, x1, y1, p, l)
1046
+ ) === 0;
1047
+ }
961
1048
  // optional-content layers (calques): list + show/hide + lock/unlock + remove
962
1049
  layers() {
963
1050
  return this.g._json((o) => this.ex().gp_layers_json(this.h, o));