@libpdf/core 0.2.6 → 0.2.7

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.mts CHANGED
@@ -2675,6 +2675,8 @@ declare class EmbeddedFont extends PdfFont {
2675
2675
  private _subsetTag;
2676
2676
  /** Whether this font is used in a form field (prevents subsetting) */
2677
2677
  private _usedInForm;
2678
+ /** Pre-allocated PDF reference (set by PDFFonts.embed()) */
2679
+ private _ref;
2678
2680
  /** Cached descriptor */
2679
2681
  private _descriptor;
2680
2682
  private constructor();
@@ -2790,6 +2792,22 @@ declare class EmbeddedFont extends PdfFont {
2790
2792
  * Call this before re-encoding if you want a fresh subset.
2791
2793
  */
2792
2794
  resetUsage(): void;
2795
+ /**
2796
+ * Get the pre-allocated PDF reference for this font.
2797
+ *
2798
+ * Set by `PDFFonts.embed()`. At save time, the actual font objects
2799
+ * (Type0 dict, CIDFont, FontDescriptor, font program, ToUnicode)
2800
+ * are created and registered at this ref.
2801
+ *
2802
+ * @throws {Error} if the font was not embedded via `pdf.embedFont()`
2803
+ */
2804
+ get ref(): PdfRef;
2805
+ /**
2806
+ * Set the pre-allocated PDF reference.
2807
+ *
2808
+ * @internal Called by PDFFonts.embed()
2809
+ */
2810
+ setRef(ref: PdfRef): void;
2793
2811
  /**
2794
2812
  * Mark this font as used in a form field.
2795
2813
  *
package/dist/index.mjs CHANGED
@@ -10,7 +10,7 @@ import { createCMSECDSASignature } from "pkijs";
10
10
  import { base64 } from "@scure/base";
11
11
 
12
12
  //#region package.json
13
- var version = "0.2.6";
13
+ var version = "0.2.7";
14
14
 
15
15
  //#endregion
16
16
  //#region src/objects/pdf-array.ts
@@ -11699,6 +11699,8 @@ var EmbeddedFont = class EmbeddedFont extends PdfFont {
11699
11699
  _subsetTag = null;
11700
11700
  /** Whether this font is used in a form field (prevents subsetting) */
11701
11701
  _usedInForm = false;
11702
+ /** Pre-allocated PDF reference (set by PDFFonts.embed()) */
11703
+ _ref = null;
11702
11704
  /** Cached descriptor */
11703
11705
  _descriptor = null;
11704
11706
  constructor(fontProgram, fontData) {
@@ -11895,6 +11897,27 @@ var EmbeddedFont = class EmbeddedFont extends PdfFont {
11895
11897
  this._subsetTag = null;
11896
11898
  }
11897
11899
  /**
11900
+ * Get the pre-allocated PDF reference for this font.
11901
+ *
11902
+ * Set by `PDFFonts.embed()`. At save time, the actual font objects
11903
+ * (Type0 dict, CIDFont, FontDescriptor, font program, ToUnicode)
11904
+ * are created and registered at this ref.
11905
+ *
11906
+ * @throws {Error} if the font was not embedded via `pdf.embedFont()`
11907
+ */
11908
+ get ref() {
11909
+ if (!this._ref) throw new Error("Font has no PDF reference. Use pdf.embedFont() to embed fonts.");
11910
+ return this._ref;
11911
+ }
11912
+ /**
11913
+ * Set the pre-allocated PDF reference.
11914
+ *
11915
+ * @internal Called by PDFFonts.embed()
11916
+ */
11917
+ setRef(ref) {
11918
+ this._ref = ref;
11919
+ }
11920
+ /**
11898
11921
  * Mark this font as used in a form field.
11899
11922
  *
11900
11923
  * Fonts used in form fields cannot be subsetted because users may type
@@ -18674,10 +18697,8 @@ function buildResources$2(ctx, font, fontName) {
18674
18697
  const resources = new PdfDict();
18675
18698
  const fonts = new PdfDict();
18676
18699
  const cleanName = fontName.startsWith("/") ? fontName.slice(1) : fontName;
18677
- if (isEmbeddedFont(font)) {
18678
- const fontRef = ctx.registry.register(buildEmbeddedFontDict$2(font));
18679
- fonts.set(cleanName, fontRef);
18680
- } else if (isExistingFont(font) && font.ref) fonts.set(cleanName, font.ref);
18700
+ if (isEmbeddedFont(font)) fonts.set(cleanName, font.ref);
18701
+ else if (isExistingFont(font) && font.ref) fonts.set(cleanName, font.ref);
18681
18702
  else {
18682
18703
  const fontDict = new PdfDict();
18683
18704
  fontDict.set("Type", PdfName.of("Font"));
@@ -18688,14 +18709,6 @@ function buildResources$2(ctx, font, fontName) {
18688
18709
  resources.set("Font", fonts);
18689
18710
  return resources;
18690
18711
  }
18691
- function buildEmbeddedFontDict$2(font) {
18692
- const dict = new PdfDict();
18693
- dict.set("Type", PdfName.of("Font"));
18694
- dict.set("Subtype", PdfName.of("Type0"));
18695
- dict.set("BaseFont", PdfName.of(font.baseFontName));
18696
- dict.set("Encoding", PdfName.of("Identity-H"));
18697
- return dict;
18698
- }
18699
18712
 
18700
18713
  //#endregion
18701
18714
  //#region src/document/forms/choice-appearance.ts
@@ -18888,10 +18901,8 @@ function buildResources$1(ctx, font, fontName) {
18888
18901
  const resources = new PdfDict();
18889
18902
  const fonts = new PdfDict();
18890
18903
  const cleanName = fontName.startsWith("/") ? fontName.slice(1) : fontName;
18891
- if (isEmbeddedFont(font)) {
18892
- const fontRef = ctx.registry.register(buildEmbeddedFontDict$1(font));
18893
- fonts.set(cleanName, fontRef);
18894
- } else if (isExistingFont(font) && font.ref) fonts.set(cleanName, font.ref);
18904
+ if (isEmbeddedFont(font)) fonts.set(cleanName, font.ref);
18905
+ else if (isExistingFont(font) && font.ref) fonts.set(cleanName, font.ref);
18895
18906
  else {
18896
18907
  const fontDict = new PdfDict();
18897
18908
  fontDict.set("Type", PdfName.of("Font"));
@@ -18902,14 +18913,6 @@ function buildResources$1(ctx, font, fontName) {
18902
18913
  resources.set("Font", fonts);
18903
18914
  return resources;
18904
18915
  }
18905
- function buildEmbeddedFontDict$1(font) {
18906
- const dict = new PdfDict();
18907
- dict.set("Type", PdfName.of("Font"));
18908
- dict.set("Subtype", PdfName.of("Type0"));
18909
- dict.set("BaseFont", PdfName.of(font.baseFontName));
18910
- dict.set("Encoding", PdfName.of("Identity-H"));
18911
- return dict;
18912
- }
18913
18916
 
18914
18917
  //#endregion
18915
18918
  //#region src/document/forms/text-appearance.ts
@@ -19211,10 +19214,8 @@ function buildResources(ctx, font, fontName) {
19211
19214
  const resources = new PdfDict();
19212
19215
  const fonts = new PdfDict();
19213
19216
  const cleanName = fontName.startsWith("/") ? fontName.slice(1) : fontName;
19214
- if (isEmbeddedFont(font)) {
19215
- const fontRef = ctx.registry.register(buildEmbeddedFontDict(font));
19216
- fonts.set(cleanName, fontRef);
19217
- } else if (isExistingFont(font) && font.ref) fonts.set(cleanName, font.ref);
19217
+ if (isEmbeddedFont(font)) fonts.set(cleanName, font.ref);
19218
+ else if (isExistingFont(font) && font.ref) fonts.set(cleanName, font.ref);
19218
19219
  else {
19219
19220
  const fontDict = new PdfDict();
19220
19221
  fontDict.set("Type", PdfName.of("Font"));
@@ -19225,14 +19226,6 @@ function buildResources(ctx, font, fontName) {
19225
19226
  resources.set("Font", fonts);
19226
19227
  return resources;
19227
19228
  }
19228
- function buildEmbeddedFontDict(font) {
19229
- const dict = new PdfDict();
19230
- dict.set("Type", PdfName.of("Font"));
19231
- dict.set("Subtype", PdfName.of("Type0"));
19232
- dict.set("BaseFont", PdfName.of(font.baseFontName));
19233
- dict.set("Encoding", PdfName.of("Identity-H"));
19234
- return dict;
19235
- }
19236
19229
  function calculateAppearanceMatrix(width, height, rotation) {
19237
19230
  switch (Math.abs(rotation)) {
19238
19231
  case 90: return [
@@ -36760,6 +36753,7 @@ var PDFFonts = class {
36760
36753
  const font = EmbeddedFont.fromBytes(data, options);
36761
36754
  const ref = this.ctx.registry.allocateRef();
36762
36755
  this.embeddedFonts.set(font, ref);
36756
+ font.setRef(ref);
36763
36757
  return font;
36764
36758
  }
36765
36759
  /**
@@ -37419,13 +37413,7 @@ var PDFForm = class PDFForm {
37419
37413
  * Register an embedded font in the form's default resources.
37420
37414
  */
37421
37415
  registerFontInFormResources(font) {
37422
- const fontRef = this._ctx.registry.register(PdfDict.of({
37423
- Type: PdfName.of("Font"),
37424
- Subtype: PdfName.of("Type0"),
37425
- BaseFont: PdfName.of(font.baseFontName),
37426
- Encoding: PdfName.of("Identity-H")
37427
- }));
37428
- this._acroForm.addFontToResources(fontRef);
37416
+ this._acroForm.addFontToResources(font.ref);
37429
37417
  }
37430
37418
  /**
37431
37419
  * Store field styling metadata for appearance generation.