pdf-lite 1.5.0 → 1.6.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.
Files changed (93) hide show
  1. package/EXAMPLES.md +51 -70
  2. package/README.md +1 -1
  3. package/dist/acroform/appearance/pdf-button-appearance-stream.d.ts +1 -1
  4. package/dist/acroform/appearance/pdf-button-appearance-stream.js +6 -2
  5. package/dist/acroform/fields/pdf-button-form-field.d.ts +0 -9
  6. package/dist/acroform/fields/pdf-button-form-field.js +7 -39
  7. package/dist/acroform/fields/pdf-choice-form-field.d.ts +2 -1
  8. package/dist/acroform/fields/pdf-choice-form-field.js +19 -38
  9. package/dist/acroform/fields/pdf-default-appearance.js +1 -1
  10. package/dist/acroform/fields/pdf-form-field.d.ts +27 -32
  11. package/dist/acroform/fields/pdf-form-field.js +180 -94
  12. package/dist/acroform/fields/pdf-text-form-field.js +6 -33
  13. package/dist/acroform/fields/types.d.ts +1 -1
  14. package/dist/acroform/index.d.ts +0 -2
  15. package/dist/acroform/index.js +0 -2
  16. package/dist/acroform/pdf-acro-form.d.ts +12 -36
  17. package/dist/acroform/pdf-acro-form.js +111 -201
  18. package/dist/acroform/xfa/pdf-xfa-data.d.ts +4 -3
  19. package/dist/acroform/xfa/pdf-xfa-data.js +16 -12
  20. package/dist/acroform/xfa/pdf-xfa-form.d.ts +9 -4
  21. package/dist/acroform/xfa/pdf-xfa-form.js +17 -39
  22. package/dist/annotations/index.d.ts +0 -1
  23. package/dist/annotations/index.js +0 -1
  24. package/dist/annotations/pdf-annotation.d.ts +7 -2
  25. package/dist/annotations/pdf-annotation.js +30 -19
  26. package/dist/annotations/pdf-default-resources.d.ts +11 -0
  27. package/dist/annotations/pdf-default-resources.js +3 -0
  28. package/dist/core/decoder.js +1 -1
  29. package/dist/core/objects/pdf-array.d.ts +8 -1
  30. package/dist/core/objects/pdf-array.js +31 -0
  31. package/dist/core/objects/pdf-dictionary.d.ts +2 -0
  32. package/dist/core/objects/pdf-dictionary.js +14 -7
  33. package/dist/core/objects/pdf-hexadecimal.d.ts +1 -0
  34. package/dist/core/objects/pdf-hexadecimal.js +3 -3
  35. package/dist/core/objects/pdf-indirect-object.d.ts +18 -9
  36. package/dist/core/objects/pdf-indirect-object.js +75 -16
  37. package/dist/core/objects/pdf-number.d.ts +1 -0
  38. package/dist/core/objects/pdf-number.js +5 -4
  39. package/dist/core/objects/pdf-object-reference.d.ts +8 -1
  40. package/dist/core/objects/pdf-object-reference.js +14 -0
  41. package/dist/core/objects/pdf-object.d.ts +14 -0
  42. package/dist/core/objects/pdf-object.js +36 -0
  43. package/dist/core/objects/pdf-start-xref.d.ts +1 -0
  44. package/dist/core/objects/pdf-start-xref.js +4 -0
  45. package/dist/core/objects/pdf-stream.d.ts +47 -7
  46. package/dist/core/objects/pdf-stream.js +301 -32
  47. package/dist/core/objects/pdf-string.d.ts +1 -0
  48. package/dist/core/objects/pdf-string.js +3 -6
  49. package/dist/core/objects/pdf-trailer.d.ts +1 -0
  50. package/dist/core/objects/pdf-trailer.js +6 -3
  51. package/dist/core/objects/pdf-xref-table.js +1 -1
  52. package/dist/core/ref.d.ts +3 -1
  53. package/dist/core/ref.js +8 -5
  54. package/dist/core/tokens/token.d.ts +2 -1
  55. package/dist/core/tokens/token.js +3 -0
  56. package/dist/fonts/index.d.ts +0 -1
  57. package/dist/fonts/index.js +0 -1
  58. package/dist/fonts/pdf-font.d.ts +32 -27
  59. package/dist/fonts/pdf-font.js +115 -77
  60. package/dist/pdf/index.d.ts +2 -0
  61. package/dist/pdf/index.js +2 -0
  62. package/dist/pdf/pdf-document.d.ts +63 -37
  63. package/dist/pdf/pdf-document.js +351 -135
  64. package/dist/pdf/pdf-page.d.ts +50 -0
  65. package/dist/pdf/pdf-page.js +144 -0
  66. package/dist/pdf/pdf-pages.d.ts +28 -0
  67. package/dist/pdf/pdf-pages.js +94 -0
  68. package/dist/pdf/pdf-reader.d.ts +5 -1
  69. package/dist/pdf/pdf-reader.js +36 -2
  70. package/dist/pdf/pdf-revision.d.ts +3 -3
  71. package/dist/pdf/pdf-revision.js +7 -7
  72. package/dist/pdf/pdf-xref-lookup.js +34 -14
  73. package/dist/signing/document-security-store.d.ts +14 -17
  74. package/dist/signing/document-security-store.js +19 -34
  75. package/dist/signing/signer.d.ts +23 -8
  76. package/dist/signing/signer.js +51 -17
  77. package/dist/utils/index.d.ts +0 -1
  78. package/dist/utils/index.js +0 -1
  79. package/dist/utils/needsCentralWhitespace.d.ts +10 -0
  80. package/dist/utils/needsCentralWhitespace.js +34 -0
  81. package/package.json +3 -3
  82. package/dist/acroform/acroform.d.ts +0 -9
  83. package/dist/acroform/acroform.js +0 -7
  84. package/dist/acroform/manager.d.ts +0 -37
  85. package/dist/acroform/manager.js +0 -57
  86. package/dist/acroform/pdf-font-encoding-cache.d.ts +0 -27
  87. package/dist/acroform/pdf-font-encoding-cache.js +0 -188
  88. package/dist/annotations/pdf-annotation-writer.d.ts +0 -20
  89. package/dist/annotations/pdf-annotation-writer.js +0 -76
  90. package/dist/fonts/manager.d.ts +0 -127
  91. package/dist/fonts/manager.js +0 -378
  92. package/dist/utils/predictors.d.ts +0 -113
  93. package/dist/utils/predictors.js +0 -279
package/dist/core/ref.js CHANGED
@@ -16,7 +16,7 @@ export class Ref {
16
16
  value;
17
17
  /** Registered callbacks for update notifications */
18
18
  callbacks = [];
19
- isModified = false;
19
+ modified = true;
20
20
  immutable = false;
21
21
  /**
22
22
  * Creates a new Ref with an initial value.
@@ -30,6 +30,12 @@ export class Ref {
30
30
  }
31
31
  this.value = value;
32
32
  }
33
+ isModified() {
34
+ return this.modified;
35
+ }
36
+ setModified(modified = true) {
37
+ this.modified = modified;
38
+ }
33
39
  setImmutable(immutable = true) {
34
40
  this.immutable = immutable;
35
41
  }
@@ -49,10 +55,7 @@ export class Ref {
49
55
  const resolvedNewValue = newValue instanceof Ref ? newValue.resolve() : newValue;
50
56
  const oldValue = this.resolve();
51
57
  if (oldValue !== resolvedNewValue) {
52
- if (this.immutable) {
53
- throw new Error(`Cannot update an immutable Ref (${oldValue} -> ${resolvedNewValue})`);
54
- }
55
- this.isModified = true;
58
+ this.modified = true;
56
59
  }
57
60
  this.value = newValue;
58
61
  this.callbacks.forEach((cb) => cb(oldValue, this.resolve()));
@@ -1,5 +1,5 @@
1
1
  import { ByteArray } from '../../types.js';
2
- export declare abstract class PdfToken {
2
+ export declare class PdfToken {
3
3
  protected rawBytes: ByteArray;
4
4
  constructor(bytes?: ByteArray);
5
5
  toBytes(): ByteArray;
@@ -7,4 +7,5 @@ export declare abstract class PdfToken {
7
7
  get type(): string;
8
8
  get byteLength(): number;
9
9
  equals(other: PdfToken): boolean;
10
+ clone(): PdfToken;
10
11
  }
@@ -29,4 +29,7 @@ export class PdfToken {
29
29
  }
30
30
  return true;
31
31
  }
32
+ clone() {
33
+ return new PdfToken(this.toBytes());
34
+ }
32
35
  }
@@ -1,4 +1,3 @@
1
1
  export * from './types.js';
2
- export * from './manager.js';
3
2
  export * from './pdf-font.js';
4
3
  export * from './parsers/index.js';
@@ -1,4 +1,3 @@
1
1
  export * from './types.js';
2
- export * from './manager.js';
3
2
  export * from './pdf-font.js';
4
3
  export * from './parsers/index.js';
@@ -4,15 +4,9 @@ import { PdfName } from '../core/objects/pdf-name.js';
4
4
  import { PdfNumber } from '../core/objects/pdf-number.js';
5
5
  import { PdfArray } from '../core/objects/pdf-array.js';
6
6
  import { PdfObjectReference } from '../core/objects/pdf-object-reference.js';
7
- import type { PdfFontManager } from './manager.js';
8
7
  import type { FontDescriptor, UnicodeFontDescriptor, FontParser } from './types.js';
9
8
  import type { ByteArray } from '../types.js';
10
- type PdfStandardFontName = 'Helvetica' | 'Helvetica-Bold' | 'Helvetica-Oblique' | 'Helvetica-BoldOblique' | 'Times-Roman' | 'Times-Bold' | 'Times-Italic' | 'Times-BoldItalic' | 'Courier' | 'Courier-Bold' | 'Courier-Oblique' | 'Courier-BoldOblique' | 'Symbol' | 'ZapfDingbats';
11
- /**
12
- * Represents an embedded font in a PDF document.
13
- * Extends PdfDictionary to provide both font metadata and PDF dictionary structure.
14
- */
15
- export declare class PdfFont extends PdfDictionary<{
9
+ type PdfFontDictionary = PdfDictionary<{
16
10
  Type: PdfName<'Font'>;
17
11
  Subtype: PdfName<'Type1' | 'TrueType' | 'Type0'>;
18
12
  BaseFont: PdfName;
@@ -23,22 +17,20 @@ export declare class PdfFont extends PdfDictionary<{
23
17
  Widths?: PdfArray<PdfNumber>;
24
18
  DescendantFonts?: PdfArray<PdfObjectReference>;
25
19
  ToUnicode?: PdfObjectReference;
26
- }> {
20
+ }>;
21
+ type PdfStandardFontName = 'Helvetica' | 'Helvetica-Bold' | 'Helvetica-Oblique' | 'Helvetica-BoldOblique' | 'Times-Roman' | 'Times-Bold' | 'Times-Italic' | 'Times-BoldItalic' | 'Courier' | 'Courier-Bold' | 'Courier-Oblique' | 'Courier-BoldOblique' | 'Symbol' | 'ZapfDingbats';
22
+ /**
23
+ * Represents an embedded font in a PDF document.
24
+ * Extends PdfIndirectObject with a PdfDictionary content for the font dict.
25
+ */
26
+ export declare class PdfFont extends PdfIndirectObject<PdfFontDictionary> {
27
27
  /**
28
28
  * The PDF resource name used in content streams (e.g., 'F1', 'F2').
29
29
  * This is the identifier used in PDF operators like `/F1 12 Tf`.
30
30
  */
31
31
  resourceName: string;
32
- /**
33
- * Reference to the container indirect object that wraps this font dict.
34
- * Set by FontManager.write() when the font is committed to the PDF.
35
- */
36
- container?: PdfIndirectObject;
37
- /**
38
- * Auxiliary objects that must be committed along with the font dict.
39
- * Includes FontDescriptor, FontFile2, CIDFont, ToUnicode, etc.
40
- */
41
- protected auxiliaryObjects: PdfIndirectObject[];
32
+ private static _resourceCounter;
33
+ private static nextResourceName;
42
34
  /**
43
35
  * @internal
44
36
  * Font descriptor with metrics and properties.
@@ -49,21 +41,34 @@ export declare class PdfFont extends PdfDictionary<{
49
41
  * Original font file bytes.
50
42
  */
51
43
  private _fontData?;
44
+ constructor(font: PdfIndirectObject);
52
45
  constructor(fontName: string);
53
46
  constructor(options: {
54
- dict?: PdfDictionary;
55
47
  fontName?: string;
56
48
  resourceName?: string;
57
49
  encoding?: string;
58
- manager?: PdfFontManager;
59
- container?: PdfIndirectObject;
60
50
  descriptor?: FontDescriptor | UnicodeFontDescriptor;
61
51
  fontData?: ByteArray;
62
52
  });
53
+ get dict(): PdfFontDictionary;
63
54
  get fontName(): string | undefined;
64
55
  set fontName(name: string | undefined);
65
56
  get encoding(): string | undefined;
66
57
  set encoding(enc: string | undefined);
58
+ /**
59
+ * Gets the encoding map from the font's Encoding dictionary's Differences array.
60
+ * Maps byte codes to Unicode characters for custom-encoded fonts.
61
+ */
62
+ get encodingMap(): Map<number, string> | null;
63
+ /**
64
+ * Gets the reverse encoding map (Unicode character → byte code).
65
+ * Useful for encoding text back into the font's custom encoding.
66
+ */
67
+ get reverseEncodingMap(): Map<string, number> | undefined;
68
+ /**
69
+ * Whether this font uses Unicode (Type0/composite) encoding.
70
+ */
71
+ get isUnicode(): boolean;
67
72
  /**
68
73
  * Gets the font descriptor with metrics and properties.
69
74
  * Available for embedded fonts, undefined for standard fonts or loaded fonts without descriptor.
@@ -147,15 +152,10 @@ export declare class PdfFont extends PdfDictionary<{
147
152
  * ```
148
153
  */
149
154
  toString(): string;
150
- /**
151
- * Returns all objects that need to be committed to the PDF.
152
- * Includes auxiliary objects (descriptors, streams) and the container.
153
- */
154
- getObjectsToCommit(): PdfIndirectObject[];
155
155
  /**
156
156
  * @internal
157
157
  * Legacy property for backward compatibility with code that accesses fontRef.
158
- * Returns the container object if available.
158
+ * Returns this font's reference since PdfFont IS the indirect object.
159
159
  */
160
160
  get fontRef(): PdfObjectReference;
161
161
  /**
@@ -230,6 +230,11 @@ export declare class PdfFont extends PdfDictionary<{
230
230
  * @returns A PdfFont instance ready to be written to the PDF
231
231
  */
232
232
  static fromType0Data(fontData: ByteArray, fontName: string, descriptor: UnicodeFontDescriptor, unicodeMappings?: Map<number, number>): PdfFont;
233
+ static fromFile(fontData: ByteArray, options?: {
234
+ fontName?: string;
235
+ unicode?: boolean;
236
+ unicodeMappings?: Map<number, number>;
237
+ }): PdfFont;
233
238
  /**
234
239
  * Generates a ToUnicode CMap for mapping CIDs to Unicode code points.
235
240
  */
@@ -4,29 +4,25 @@ import { PdfName } from '../core/objects/pdf-name.js';
4
4
  import { PdfNumber } from '../core/objects/pdf-number.js';
5
5
  import { PdfArray } from '../core/objects/pdf-array.js';
6
6
  import { PdfStream } from '../core/objects/pdf-stream.js';
7
+ import { PdfObjectReference } from '../core/objects/pdf-object-reference.js';
7
8
  import { PdfString } from '../core/objects/pdf-string.js';
9
+ import { buildEncodingMap } from '../utils/decodeWithFontEncoding.js';
8
10
  import { parseFont } from './parsers/font-parser.js';
9
11
  import { OtfParser } from './parsers/otf-parser.js';
10
12
  /**
11
13
  * Represents an embedded font in a PDF document.
12
- * Extends PdfDictionary to provide both font metadata and PDF dictionary structure.
14
+ * Extends PdfIndirectObject with a PdfDictionary content for the font dict.
13
15
  */
14
- export class PdfFont extends PdfDictionary {
16
+ export class PdfFont extends PdfIndirectObject {
15
17
  /**
16
18
  * The PDF resource name used in content streams (e.g., 'F1', 'F2').
17
19
  * This is the identifier used in PDF operators like `/F1 12 Tf`.
18
20
  */
19
21
  resourceName;
20
- /**
21
- * Reference to the container indirect object that wraps this font dict.
22
- * Set by FontManager.write() when the font is committed to the PDF.
23
- */
24
- container;
25
- /**
26
- * Auxiliary objects that must be committed along with the font dict.
27
- * Includes FontDescriptor, FontFile2, CIDFont, ToUnicode, etc.
28
- */
29
- auxiliaryObjects = [];
22
+ static _resourceCounter = 0;
23
+ static nextResourceName() {
24
+ return `F${++PdfFont._resourceCounter}`;
25
+ }
30
26
  /**
31
27
  * @internal
32
28
  * Font descriptor with metrics and properties.
@@ -38,27 +34,36 @@ export class PdfFont extends PdfDictionary {
38
34
  */
39
35
  _fontData;
40
36
  constructor(optionsOrFontName) {
41
- super();
37
+ super(new PdfIndirectObject({ content: new PdfDictionary() }));
38
+ if (optionsOrFontName instanceof PdfIndirectObject) {
39
+ // Handle PdfIndirectObject input (existing font object)
40
+ const fontObj = optionsOrFontName;
41
+ if (!(fontObj.content instanceof PdfDictionary)) {
42
+ throw new Error('PdfIndirectObject content must be a PdfDictionary');
43
+ }
44
+ this.content = fontObj.content;
45
+ this.resourceName = fontObj.reference.key;
46
+ return;
47
+ }
42
48
  // Handle string parameter (simple fontName)
43
49
  if (typeof optionsOrFontName === 'string') {
44
50
  this.fontName = optionsOrFontName;
45
51
  this.resourceName = optionsOrFontName;
46
52
  return;
47
53
  }
48
- // Handle options object (existing behavior)
54
+ // Handle options object
49
55
  const options = optionsOrFontName;
50
- if (options.dict) {
51
- this.copyFrom(options.dict);
52
- }
53
56
  this.fontName = options.fontName;
54
- this.resourceName = options.resourceName ?? '';
57
+ this.resourceName = options.resourceName || PdfFont.nextResourceName();
55
58
  this.encoding = options.encoding;
56
- this.container = options.container;
57
59
  this._descriptor = options.descriptor;
58
60
  this._fontData = options.fontData;
59
61
  }
62
+ get dict() {
63
+ return this.content;
64
+ }
60
65
  get fontName() {
61
- const baseFont = this.get('BaseFont');
66
+ const baseFont = this.content.get('BaseFont');
62
67
  if (!baseFont) {
63
68
  return undefined;
64
69
  }
@@ -66,23 +71,52 @@ export class PdfFont extends PdfDictionary {
66
71
  }
67
72
  set fontName(name) {
68
73
  if (!name) {
69
- this.delete('BaseFont');
74
+ this.content.delete('BaseFont');
70
75
  return;
71
76
  }
72
- this.set('BaseFont', new PdfName(name));
77
+ this.content.set('BaseFont', new PdfName(name));
73
78
  }
74
79
  get encoding() {
75
- const encoding = this.get('Encoding');
80
+ const encoding = this.content.get('Encoding');
76
81
  return encoding ? encoding.value : undefined;
77
82
  }
78
83
  set encoding(enc) {
79
84
  if (enc) {
80
- this.set('Encoding', new PdfName(enc));
85
+ this.content.set('Encoding', new PdfName(enc));
81
86
  }
82
87
  else {
83
- this.delete('Encoding');
88
+ this.content.delete('Encoding');
84
89
  }
85
90
  }
91
+ /**
92
+ * Gets the encoding map from the font's Encoding dictionary's Differences array.
93
+ * Maps byte codes to Unicode characters for custom-encoded fonts.
94
+ */
95
+ get encodingMap() {
96
+ const enc = this.content.get('Encoding');
97
+ const encObj = enc instanceof PdfObjectReference ? enc.resolve()?.content : enc;
98
+ const encDict = encObj instanceof PdfDictionary ? encObj : undefined;
99
+ const diffs = encDict?.get('Differences')?.as(PdfArray);
100
+ if (!diffs)
101
+ return null;
102
+ return buildEncodingMap(diffs);
103
+ }
104
+ /**
105
+ * Gets the reverse encoding map (Unicode character → byte code).
106
+ * Useful for encoding text back into the font's custom encoding.
107
+ */
108
+ get reverseEncodingMap() {
109
+ const map = this.encodingMap;
110
+ if (!map)
111
+ return undefined;
112
+ return new Map(Array.from(map, ([code, char]) => [char, code]));
113
+ }
114
+ /**
115
+ * Whether this font uses Unicode (Type0/composite) encoding.
116
+ */
117
+ get isUnicode() {
118
+ return this.fontType === 'Type0';
119
+ }
86
120
  /**
87
121
  * Gets the font descriptor with metrics and properties.
88
122
  * Available for embedded fonts, undefined for standard fonts or loaded fonts without descriptor.
@@ -101,7 +135,7 @@ export class PdfFont extends PdfDictionary {
101
135
  * Gets the font type (Subtype in PDF).
102
136
  */
103
137
  get fontType() {
104
- const subtype = this.get('Subtype');
138
+ const subtype = this.content.get('Subtype');
105
139
  return subtype?.value;
106
140
  }
107
141
  /**
@@ -109,17 +143,17 @@ export class PdfFont extends PdfDictionary {
109
143
  */
110
144
  set fontType(type) {
111
145
  if (type) {
112
- this.set('Subtype', new PdfName(type));
146
+ this.content.set('Subtype', new PdfName(type));
113
147
  }
114
148
  else {
115
- this.delete('Subtype');
149
+ this.content.delete('Subtype');
116
150
  }
117
151
  }
118
152
  /**
119
153
  * Gets the first character code in the Widths array.
120
154
  */
121
155
  get firstChar() {
122
- const firstChar = this.get('FirstChar');
156
+ const firstChar = this.content.get('FirstChar');
123
157
  return firstChar?.value;
124
158
  }
125
159
  /**
@@ -127,17 +161,17 @@ export class PdfFont extends PdfDictionary {
127
161
  */
128
162
  set firstChar(value) {
129
163
  if (value !== undefined) {
130
- this.set('FirstChar', new PdfNumber(value));
164
+ this.content.set('FirstChar', new PdfNumber(value));
131
165
  }
132
166
  else {
133
- this.delete('FirstChar');
167
+ this.content.delete('FirstChar');
134
168
  }
135
169
  }
136
170
  /**
137
171
  * Gets the last character code in the Widths array.
138
172
  */
139
173
  get lastChar() {
140
- const lastChar = this.get('LastChar');
174
+ const lastChar = this.content.get('LastChar');
141
175
  return lastChar?.value;
142
176
  }
143
177
  /**
@@ -145,17 +179,17 @@ export class PdfFont extends PdfDictionary {
145
179
  */
146
180
  set lastChar(value) {
147
181
  if (value !== undefined) {
148
- this.set('LastChar', new PdfNumber(value));
182
+ this.content.set('LastChar', new PdfNumber(value));
149
183
  }
150
184
  else {
151
- this.delete('LastChar');
185
+ this.content.delete('LastChar');
152
186
  }
153
187
  }
154
188
  /**
155
189
  * Gets the character widths array.
156
190
  */
157
191
  get widths() {
158
- const widths = this.get('Widths');
192
+ const widths = this.content.get('Widths');
159
193
  if (!widths)
160
194
  return undefined;
161
195
  return widths.items.map((item) => item.value);
@@ -165,10 +199,10 @@ export class PdfFont extends PdfDictionary {
165
199
  */
166
200
  set widths(values) {
167
201
  if (values) {
168
- this.set('Widths', new PdfArray(values.map((w) => new PdfNumber(w))));
202
+ this.content.set('Widths', new PdfArray(values.map((w) => new PdfNumber(w))));
169
203
  }
170
204
  else {
171
- this.delete('Widths');
205
+ this.content.delete('Widths');
172
206
  }
173
207
  }
174
208
  /**
@@ -235,27 +269,13 @@ export class PdfFont extends PdfDictionary {
235
269
  toString() {
236
270
  return this.resourceName;
237
271
  }
238
- /**
239
- * Returns all objects that need to be committed to the PDF.
240
- * Includes auxiliary objects (descriptors, streams) and the container.
241
- */
242
- getObjectsToCommit() {
243
- const objects = [...this.auxiliaryObjects];
244
- if (this.container) {
245
- objects.push(this.container);
246
- }
247
- return objects;
248
- }
249
272
  /**
250
273
  * @internal
251
274
  * Legacy property for backward compatibility with code that accesses fontRef.
252
- * Returns the container object if available.
275
+ * Returns this font's reference since PdfFont IS the indirect object.
253
276
  */
254
277
  get fontRef() {
255
- if (!this.container) {
256
- throw new Error('Font has not been written to PDF yet');
257
- }
258
- return this.container.reference;
278
+ return this.reference;
259
279
  }
260
280
  /**
261
281
  * Creates a PdfFont from font file bytes.
@@ -297,9 +317,9 @@ export class PdfFont extends PdfDictionary {
297
317
  static fromStandardFont(fontName, widths) {
298
318
  const font = new PdfFont({ fontName });
299
319
  // Build Type1 font dictionary
300
- font.set('Type', new PdfName('Font'));
301
- font.set('Subtype', new PdfName('Type1'));
302
- font.set('BaseFont', new PdfName(fontName));
320
+ font.content.set('Type', new PdfName('Font'));
321
+ font.content.set('Subtype', new PdfName('Type1'));
322
+ font.content.set('BaseFont', new PdfName(fontName));
303
323
  if (widths) {
304
324
  font.firstChar = 32;
305
325
  font.lastChar = 32 + widths.length - 1;
@@ -473,28 +493,26 @@ export class PdfFont extends PdfDictionary {
473
493
  const fontDescriptorObject = new PdfIndirectObject({
474
494
  content: fontDescriptorDict,
475
495
  });
476
- // Store auxiliary objects
477
- font.auxiliaryObjects.push(fontFileObject, fontDescriptorObject);
478
496
  // Build TrueType font dictionary
479
- font.set('Type', new PdfName('Font'));
480
- font.set('Subtype', new PdfName('TrueType'));
481
- font.set('BaseFont', new PdfName(descriptor.fontName));
482
- font.set('FontDescriptor', fontDescriptorObject.reference);
483
- font.set('Encoding', new PdfName('WinAnsiEncoding'));
497
+ font.content.set('Type', new PdfName('Font'));
498
+ font.content.set('Subtype', new PdfName('TrueType'));
499
+ font.content.set('BaseFont', new PdfName(descriptor.fontName));
500
+ font.content.set('FontDescriptor', fontDescriptorObject.reference);
501
+ font.content.set('Encoding', new PdfName('WinAnsiEncoding'));
484
502
  // Add width information for proper glyph rendering
485
503
  const firstChar = descriptor.firstChar ?? 32;
486
504
  const lastChar = descriptor.lastChar ?? 255;
487
- font.set('FirstChar', new PdfNumber(firstChar));
488
- font.set('LastChar', new PdfNumber(lastChar));
505
+ font.content.set('FirstChar', new PdfNumber(firstChar));
506
+ font.content.set('LastChar', new PdfNumber(lastChar));
489
507
  if (descriptor.widths) {
490
- font.set('Widths', new PdfArray(descriptor.widths.map((w) => new PdfNumber(w))));
508
+ font.content.set('Widths', new PdfArray(descriptor.widths.map((w) => new PdfNumber(w))));
491
509
  }
492
510
  else {
493
511
  // Default: 1000 (standard em-square)
494
512
  const defaultWidths = Array(lastChar - firstChar + 1)
495
513
  .fill(0)
496
514
  .map(() => new PdfNumber(1000));
497
- font.set('Widths', new PdfArray(defaultWidths));
515
+ font.content.set('Widths', new PdfArray(defaultWidths));
498
516
  }
499
517
  return font;
500
518
  }
@@ -547,8 +565,6 @@ export class PdfFont extends PdfDictionary {
547
565
  const fontDescriptorObject = new PdfIndirectObject({
548
566
  content: fontDescriptorDict,
549
567
  });
550
- // Store descriptor and font file
551
- font.auxiliaryObjects.push(fontFileObject, fontDescriptorObject);
552
568
  // Create CIDFont dictionary (descendant font)
553
569
  const cidFontDict = new PdfDictionary();
554
570
  cidFontDict.set('Type', new PdfName('Font'));
@@ -571,13 +587,12 @@ export class PdfFont extends PdfDictionary {
571
587
  const cidFontObject = new PdfIndirectObject({
572
588
  content: cidFontDict,
573
589
  });
574
- font.auxiliaryObjects.push(cidFontObject);
575
590
  // Build Type0 font dictionary
576
- font.set('Type', new PdfName('Font'));
577
- font.set('Subtype', new PdfName('Type0'));
578
- font.set('BaseFont', new PdfName(`${descriptor.fontName}-Identity-H`));
579
- font.set('Encoding', new PdfName('Identity-H'));
580
- font.set('DescendantFonts', new PdfArray([cidFontObject.reference]));
591
+ font.content.set('Type', new PdfName('Font'));
592
+ font.content.set('Subtype', new PdfName('Type0'));
593
+ font.content.set('BaseFont', new PdfName(`${descriptor.fontName}-Identity-H`));
594
+ font.content.set('Encoding', new PdfName('Identity-H'));
595
+ font.content.set('DescendantFonts', new PdfArray([cidFontObject.reference]));
581
596
  // Create ToUnicode CMap if mappings provided
582
597
  if (unicodeMappings && unicodeMappings.size > 0) {
583
598
  const cmapContent = PdfFont.generateToUnicodeCMap(unicodeMappings);
@@ -589,11 +604,34 @@ export class PdfFont extends PdfDictionary {
589
604
  const cmapObject = new PdfIndirectObject({
590
605
  content: cmapStream,
591
606
  });
592
- font.set('ToUnicode', cmapObject.reference);
593
- font.auxiliaryObjects.push(cmapObject);
607
+ font.content.set('ToUnicode', cmapObject.reference);
594
608
  }
595
609
  return font;
596
610
  }
611
+ static fromFile(fontData, options) {
612
+ // Parse the font to extract metadata
613
+ const parser = parseFont(fontData);
614
+ const info = parser.getFontInfo();
615
+ // Auto-generate font name from metadata if not provided
616
+ const fontName = options?.fontName ?? info.postScriptName ?? info.fullName;
617
+ // Get the appropriate descriptor based on unicode option
618
+ const descriptor = parser.getFontDescriptor(fontName);
619
+ // Embed using the appropriate method and return PdfFont
620
+ if (options?.unicode) {
621
+ // For Unicode fonts, we need a UnicodeFontDescriptor
622
+ // Create one by extending the base descriptor
623
+ const unicodeDescriptor = {
624
+ ...descriptor,
625
+ defaultWidth: 1000,
626
+ cidToGidMap: 'Identity',
627
+ };
628
+ return PdfFont.fromType0Data(fontData, fontName, unicodeDescriptor, options.unicodeMappings);
629
+ }
630
+ else {
631
+ // Use standard TrueType embedding
632
+ return PdfFont.fromTrueTypeData(fontData, fontName, descriptor);
633
+ }
634
+ }
597
635
  /**
598
636
  * Generates a ToUnicode CMap for mapping CIDs to Unicode code points.
599
637
  */
@@ -1,4 +1,6 @@
1
1
  export * from './pdf-document.js';
2
+ export * from './pdf-page.js';
3
+ export * from './pdf-pages.js';
2
4
  export * from './pdf-reader.js';
3
5
  export * from './pdf-revision.js';
4
6
  export * from './pdf-xref-lookup.js';
package/dist/pdf/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from './pdf-document.js';
2
+ export * from './pdf-page.js';
3
+ export * from './pdf-pages.js';
2
4
  export * from './pdf-reader.js';
3
5
  export * from './pdf-revision.js';
4
6
  export * from './pdf-xref-lookup.js';