pdf-lite 1.4.0 → 1.6.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/EXAMPLES.md +51 -70
- package/README.md +1 -1
- package/dist/acroform/appearance/index.d.ts +4 -4
- package/dist/acroform/appearance/index.js +4 -4
- package/dist/acroform/appearance/{PdfAppearanceStream.d.ts → pdf-appearance-stream.d.ts} +9 -3
- package/dist/acroform/appearance/{PdfAppearanceStream.js → pdf-appearance-stream.js} +14 -5
- package/dist/acroform/appearance/{PdfButtonAppearanceStream.d.ts → pdf-button-appearance-stream.d.ts} +3 -2
- package/dist/acroform/appearance/pdf-button-appearance-stream.js +58 -0
- package/dist/acroform/appearance/pdf-choice-appearance-stream.d.ts +22 -0
- package/dist/acroform/appearance/pdf-choice-appearance-stream.js +75 -0
- package/dist/acroform/appearance/pdf-graphics.d.ts +51 -0
- package/dist/acroform/appearance/pdf-graphics.js +239 -0
- package/dist/acroform/appearance/{PdfTextAppearanceStream.d.ts → pdf-text-appearance-stream.d.ts} +7 -2
- package/dist/acroform/appearance/pdf-text-appearance-stream.js +104 -0
- package/dist/acroform/fields/index.d.ts +7 -7
- package/dist/acroform/fields/index.js +7 -7
- package/dist/acroform/fields/pdf-button-form-field.d.ts +14 -0
- package/dist/acroform/fields/pdf-button-form-field.js +70 -0
- package/dist/acroform/fields/pdf-choice-form-field.d.ts +19 -0
- package/dist/acroform/fields/pdf-choice-form-field.js +112 -0
- package/dist/acroform/fields/{PdfFormFieldFlags.d.ts → pdf-form-field-flags.d.ts} +5 -6
- package/dist/acroform/fields/{PdfFormFieldFlags.js → pdf-form-field-flags.js} +12 -18
- package/dist/acroform/fields/{PdfFormField.d.ts → pdf-form-field.d.ts} +37 -38
- package/dist/acroform/fields/pdf-form-field.js +519 -0
- package/dist/acroform/fields/{PdfSignatureFormField.d.ts → pdf-signature-form-field.d.ts} +1 -1
- package/dist/acroform/fields/{PdfSignatureFormField.js → pdf-signature-form-field.js} +1 -1
- package/dist/acroform/fields/{PdfTextFormField.d.ts → pdf-text-form-field.d.ts} +1 -1
- package/dist/acroform/fields/{PdfTextFormField.js → pdf-text-form-field.js} +11 -13
- package/dist/acroform/fields/types.d.ts +6 -1
- package/dist/acroform/index.d.ts +1 -3
- package/dist/acroform/index.js +1 -3
- package/dist/acroform/pdf-acro-form.d.ts +45 -0
- package/dist/acroform/pdf-acro-form.js +203 -0
- package/dist/acroform/xfa/index.d.ts +3 -3
- package/dist/acroform/xfa/index.js +2 -2
- package/dist/acroform/xfa/{PdfXfaData.d.ts → pdf-xfa-data.d.ts} +4 -3
- package/dist/acroform/xfa/{PdfXfaData.js → pdf-xfa-data.js} +16 -12
- package/dist/acroform/xfa/pdf-xfa-form.d.ts +16 -0
- package/dist/acroform/xfa/pdf-xfa-form.js +34 -0
- package/dist/annotations/index.d.ts +3 -4
- package/dist/annotations/index.js +3 -4
- package/dist/annotations/{PdfAnnotationFlags.d.ts → pdf-annotation-flags.d.ts} +3 -4
- package/dist/annotations/{PdfAnnotationFlags.js → pdf-annotation-flags.js} +5 -6
- package/dist/annotations/{PdfAnnotation.d.ts → pdf-annotation.d.ts} +31 -5
- package/dist/annotations/{PdfAnnotation.js → pdf-annotation.js} +31 -19
- package/dist/annotations/pdf-default-resources.d.ts +11 -0
- package/dist/annotations/pdf-default-resources.js +3 -0
- package/dist/annotations/{PdfWidgetAnnotation.d.ts → pdf-widget-annotation.d.ts} +1 -1
- package/dist/annotations/{PdfWidgetAnnotation.js → pdf-widget-annotation.js} +1 -1
- package/dist/core/decoder.js +1 -1
- package/dist/core/objects/pdf-array.d.ts +8 -1
- package/dist/core/objects/pdf-array.js +31 -0
- package/dist/core/objects/pdf-dictionary.d.ts +2 -0
- package/dist/core/objects/pdf-dictionary.js +14 -7
- package/dist/core/objects/pdf-hexadecimal.d.ts +1 -0
- package/dist/core/objects/pdf-hexadecimal.js +3 -3
- package/dist/core/objects/pdf-indirect-object.d.ts +18 -9
- package/dist/core/objects/pdf-indirect-object.js +75 -16
- package/dist/core/objects/pdf-number.d.ts +1 -0
- package/dist/core/objects/pdf-number.js +5 -4
- package/dist/core/objects/pdf-object-reference.d.ts +8 -1
- package/dist/core/objects/pdf-object-reference.js +14 -0
- package/dist/core/objects/pdf-object.d.ts +14 -0
- package/dist/core/objects/pdf-object.js +36 -0
- package/dist/core/objects/pdf-start-xref.d.ts +1 -0
- package/dist/core/objects/pdf-start-xref.js +4 -0
- package/dist/core/objects/pdf-stream.d.ts +44 -7
- package/dist/core/objects/pdf-stream.js +284 -26
- package/dist/core/objects/pdf-string.d.ts +1 -0
- package/dist/core/objects/pdf-string.js +3 -6
- package/dist/core/objects/pdf-trailer.d.ts +1 -0
- package/dist/core/objects/pdf-trailer.js +6 -3
- package/dist/core/objects/pdf-xref-table.js +1 -1
- package/dist/core/parser/incremental-parser.d.ts +0 -13
- package/dist/core/parser/incremental-parser.js +1 -18
- package/dist/core/ref.d.ts +3 -1
- package/dist/core/ref.js +8 -5
- package/dist/core/streams/object-stream.d.ts +1 -1
- package/dist/core/streams/object-stream.js +1 -1
- package/dist/core/tokens/token.d.ts +2 -1
- package/dist/core/tokens/token.js +3 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +24 -0
- package/dist/fonts/index.d.ts +0 -1
- package/dist/fonts/index.js +0 -1
- package/dist/fonts/pdf-font.d.ts +94 -32
- package/dist/fonts/pdf-font.js +301 -83
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/pdf/index.d.ts +2 -1
- package/dist/pdf/index.js +2 -1
- package/dist/pdf/pdf-document.d.ts +61 -36
- package/dist/pdf/pdf-document.js +315 -117
- package/dist/pdf/pdf-page.d.ts +50 -0
- package/dist/pdf/pdf-page.js +144 -0
- package/dist/pdf/pdf-pages.d.ts +28 -0
- package/dist/pdf/pdf-pages.js +94 -0
- package/dist/pdf/pdf-reader.d.ts +5 -1
- package/dist/pdf/pdf-reader.js +36 -2
- package/dist/pdf/pdf-revision.d.ts +3 -3
- package/dist/pdf/pdf-revision.js +7 -7
- package/dist/pdf/pdf-xref-lookup.js +34 -14
- package/dist/signing/document-security-store.d.ts +14 -17
- package/dist/signing/document-security-store.js +19 -34
- package/dist/signing/signer.d.ts +23 -8
- package/dist/signing/signer.js +51 -17
- package/dist/utils/encodePdfText.d.ts +17 -0
- package/dist/utils/encodePdfText.js +61 -0
- package/dist/utils/index.d.ts +1 -2
- package/dist/utils/index.js +1 -2
- package/dist/utils/needsCentralWhitespace.d.ts +10 -0
- package/dist/utils/needsCentralWhitespace.js +34 -0
- package/package.json +3 -3
- package/dist/acroform/PdfAcroForm.d.ts +0 -63
- package/dist/acroform/PdfAcroForm.js +0 -279
- package/dist/acroform/PdfFontEncodingCache.d.ts +0 -16
- package/dist/acroform/PdfFontEncodingCache.js +0 -75
- package/dist/acroform/acroform.d.ts +0 -9
- package/dist/acroform/acroform.js +0 -7
- package/dist/acroform/appearance/PdfButtonAppearanceStream.js +0 -54
- package/dist/acroform/appearance/PdfChoiceAppearanceStream.d.ts +0 -15
- package/dist/acroform/appearance/PdfChoiceAppearanceStream.js +0 -48
- package/dist/acroform/appearance/PdfTextAppearanceStream.js +0 -75
- package/dist/acroform/fields/PdfButtonFormField.d.ts +0 -9
- package/dist/acroform/fields/PdfButtonFormField.js +0 -35
- package/dist/acroform/fields/PdfChoiceFormField.d.ts +0 -9
- package/dist/acroform/fields/PdfChoiceFormField.js +0 -46
- package/dist/acroform/fields/PdfFormField.js +0 -499
- package/dist/acroform/manager.d.ts +0 -33
- package/dist/acroform/manager.js +0 -51
- package/dist/acroform/xfa/PdfXfaForm.d.ts +0 -12
- package/dist/acroform/xfa/PdfXfaForm.js +0 -64
- package/dist/annotations/PdfAnnotationWriter.d.ts +0 -20
- package/dist/annotations/PdfAnnotationWriter.js +0 -76
- package/dist/fonts/font-manager.d.ts +0 -127
- package/dist/fonts/font-manager.js +0 -378
- package/dist/pdf/errors.d.ts +0 -6
- package/dist/pdf/errors.js +0 -6
- package/dist/utils/predictors.d.ts +0 -113
- package/dist/utils/predictors.js +0 -279
- /package/dist/acroform/fields/{PdfDefaultAppearance.d.ts → pdf-default-appearance.d.ts} +0 -0
- /package/dist/acroform/fields/{PdfDefaultAppearance.js → pdf-default-appearance.js} +0 -0
- /package/dist/utils/{IterableReadableStream.d.ts → iterable-readable-stream.d.ts} +0 -0
- /package/dist/utils/{IterableReadableStream.js → iterable-readable-stream.js} +0 -0
package/dist/fonts/pdf-font.js
CHANGED
|
@@ -4,34 +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
|
|
14
|
+
* Extends PdfIndirectObject with a PdfDictionary content for the font dict.
|
|
13
15
|
*/
|
|
14
|
-
export class PdfFont extends
|
|
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
|
-
|
|
22
|
-
|
|
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 = [];
|
|
30
|
-
/**
|
|
31
|
-
* @internal
|
|
32
|
-
* Reference to the font manager that created this font.
|
|
33
|
-
*/
|
|
34
|
-
manager;
|
|
22
|
+
static _resourceCounter = 0;
|
|
23
|
+
static nextResourceName() {
|
|
24
|
+
return `F${++PdfFont._resourceCounter}`;
|
|
25
|
+
}
|
|
35
26
|
/**
|
|
36
27
|
* @internal
|
|
37
28
|
* Font descriptor with metrics and properties.
|
|
@@ -42,21 +33,37 @@ export class PdfFont extends PdfDictionary {
|
|
|
42
33
|
* Original font file bytes.
|
|
43
34
|
*/
|
|
44
35
|
_fontData;
|
|
45
|
-
constructor(
|
|
46
|
-
super();
|
|
47
|
-
if (
|
|
48
|
-
|
|
36
|
+
constructor(optionsOrFontName) {
|
|
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;
|
|
49
47
|
}
|
|
48
|
+
// Handle string parameter (simple fontName)
|
|
49
|
+
if (typeof optionsOrFontName === 'string') {
|
|
50
|
+
this.fontName = optionsOrFontName;
|
|
51
|
+
this.resourceName = optionsOrFontName;
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// Handle options object
|
|
55
|
+
const options = optionsOrFontName;
|
|
50
56
|
this.fontName = options.fontName;
|
|
51
|
-
this.resourceName = options.resourceName
|
|
57
|
+
this.resourceName = options.resourceName || PdfFont.nextResourceName();
|
|
52
58
|
this.encoding = options.encoding;
|
|
53
|
-
this.manager = options.manager;
|
|
54
|
-
this.container = options.container;
|
|
55
59
|
this._descriptor = options.descriptor;
|
|
56
60
|
this._fontData = options.fontData;
|
|
57
61
|
}
|
|
62
|
+
get dict() {
|
|
63
|
+
return this.content;
|
|
64
|
+
}
|
|
58
65
|
get fontName() {
|
|
59
|
-
const baseFont = this.get('BaseFont');
|
|
66
|
+
const baseFont = this.content.get('BaseFont');
|
|
60
67
|
if (!baseFont) {
|
|
61
68
|
return undefined;
|
|
62
69
|
}
|
|
@@ -64,23 +71,52 @@ export class PdfFont extends PdfDictionary {
|
|
|
64
71
|
}
|
|
65
72
|
set fontName(name) {
|
|
66
73
|
if (!name) {
|
|
67
|
-
this.delete('BaseFont');
|
|
74
|
+
this.content.delete('BaseFont');
|
|
68
75
|
return;
|
|
69
76
|
}
|
|
70
|
-
this.set('BaseFont', new PdfName(name));
|
|
77
|
+
this.content.set('BaseFont', new PdfName(name));
|
|
71
78
|
}
|
|
72
79
|
get encoding() {
|
|
73
|
-
const encoding = this.get('Encoding');
|
|
80
|
+
const encoding = this.content.get('Encoding');
|
|
74
81
|
return encoding ? encoding.value : undefined;
|
|
75
82
|
}
|
|
76
83
|
set encoding(enc) {
|
|
77
84
|
if (enc) {
|
|
78
|
-
this.set('Encoding', new PdfName(enc));
|
|
85
|
+
this.content.set('Encoding', new PdfName(enc));
|
|
79
86
|
}
|
|
80
87
|
else {
|
|
81
|
-
this.delete('Encoding');
|
|
88
|
+
this.content.delete('Encoding');
|
|
82
89
|
}
|
|
83
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
|
+
}
|
|
84
120
|
/**
|
|
85
121
|
* Gets the font descriptor with metrics and properties.
|
|
86
122
|
* Available for embedded fonts, undefined for standard fonts or loaded fonts without descriptor.
|
|
@@ -99,7 +135,7 @@ export class PdfFont extends PdfDictionary {
|
|
|
99
135
|
* Gets the font type (Subtype in PDF).
|
|
100
136
|
*/
|
|
101
137
|
get fontType() {
|
|
102
|
-
const subtype = this.get('Subtype');
|
|
138
|
+
const subtype = this.content.get('Subtype');
|
|
103
139
|
return subtype?.value;
|
|
104
140
|
}
|
|
105
141
|
/**
|
|
@@ -107,17 +143,17 @@ export class PdfFont extends PdfDictionary {
|
|
|
107
143
|
*/
|
|
108
144
|
set fontType(type) {
|
|
109
145
|
if (type) {
|
|
110
|
-
this.set('Subtype', new PdfName(type));
|
|
146
|
+
this.content.set('Subtype', new PdfName(type));
|
|
111
147
|
}
|
|
112
148
|
else {
|
|
113
|
-
this.delete('Subtype');
|
|
149
|
+
this.content.delete('Subtype');
|
|
114
150
|
}
|
|
115
151
|
}
|
|
116
152
|
/**
|
|
117
153
|
* Gets the first character code in the Widths array.
|
|
118
154
|
*/
|
|
119
155
|
get firstChar() {
|
|
120
|
-
const firstChar = this.get('FirstChar');
|
|
156
|
+
const firstChar = this.content.get('FirstChar');
|
|
121
157
|
return firstChar?.value;
|
|
122
158
|
}
|
|
123
159
|
/**
|
|
@@ -125,17 +161,17 @@ export class PdfFont extends PdfDictionary {
|
|
|
125
161
|
*/
|
|
126
162
|
set firstChar(value) {
|
|
127
163
|
if (value !== undefined) {
|
|
128
|
-
this.set('FirstChar', new PdfNumber(value));
|
|
164
|
+
this.content.set('FirstChar', new PdfNumber(value));
|
|
129
165
|
}
|
|
130
166
|
else {
|
|
131
|
-
this.delete('FirstChar');
|
|
167
|
+
this.content.delete('FirstChar');
|
|
132
168
|
}
|
|
133
169
|
}
|
|
134
170
|
/**
|
|
135
171
|
* Gets the last character code in the Widths array.
|
|
136
172
|
*/
|
|
137
173
|
get lastChar() {
|
|
138
|
-
const lastChar = this.get('LastChar');
|
|
174
|
+
const lastChar = this.content.get('LastChar');
|
|
139
175
|
return lastChar?.value;
|
|
140
176
|
}
|
|
141
177
|
/**
|
|
@@ -143,17 +179,17 @@ export class PdfFont extends PdfDictionary {
|
|
|
143
179
|
*/
|
|
144
180
|
set lastChar(value) {
|
|
145
181
|
if (value !== undefined) {
|
|
146
|
-
this.set('LastChar', new PdfNumber(value));
|
|
182
|
+
this.content.set('LastChar', new PdfNumber(value));
|
|
147
183
|
}
|
|
148
184
|
else {
|
|
149
|
-
this.delete('LastChar');
|
|
185
|
+
this.content.delete('LastChar');
|
|
150
186
|
}
|
|
151
187
|
}
|
|
152
188
|
/**
|
|
153
189
|
* Gets the character widths array.
|
|
154
190
|
*/
|
|
155
191
|
get widths() {
|
|
156
|
-
const widths = this.get('Widths');
|
|
192
|
+
const widths = this.content.get('Widths');
|
|
157
193
|
if (!widths)
|
|
158
194
|
return undefined;
|
|
159
195
|
return widths.items.map((item) => item.value);
|
|
@@ -163,11 +199,65 @@ export class PdfFont extends PdfDictionary {
|
|
|
163
199
|
*/
|
|
164
200
|
set widths(values) {
|
|
165
201
|
if (values) {
|
|
166
|
-
this.set('Widths', new PdfArray(values.map((w) => new PdfNumber(w))));
|
|
202
|
+
this.content.set('Widths', new PdfArray(values.map((w) => new PdfNumber(w))));
|
|
167
203
|
}
|
|
168
204
|
else {
|
|
169
|
-
this.delete('Widths');
|
|
205
|
+
this.content.delete('Widths');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Gets the raw character width (in 1000-unit em square) for a character code.
|
|
210
|
+
* Returns null if the character is not in the font's width table.
|
|
211
|
+
*
|
|
212
|
+
* @param charCode - The character code to get the width for
|
|
213
|
+
* @returns The raw character width or null if not found
|
|
214
|
+
*/
|
|
215
|
+
getRawCharacterWidth(charCode) {
|
|
216
|
+
if (this.widths === undefined || this.firstChar === undefined) {
|
|
217
|
+
return null;
|
|
170
218
|
}
|
|
219
|
+
const widthIndex = charCode - this.firstChar;
|
|
220
|
+
if (widthIndex >= 0 && widthIndex < this.widths.length) {
|
|
221
|
+
return this.widths[widthIndex];
|
|
222
|
+
}
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Gets the character width scaled to the specified font size.
|
|
227
|
+
* Returns null if the character is not in the font's width table.
|
|
228
|
+
*
|
|
229
|
+
* @param charCode - The character code to get the width for
|
|
230
|
+
* @param fontSize - The font size to scale to
|
|
231
|
+
* @returns The scaled character width or null if not found
|
|
232
|
+
*/
|
|
233
|
+
getCharacterWidth(charCode, fontSize) {
|
|
234
|
+
const rawWidth = this.getRawCharacterWidth(charCode);
|
|
235
|
+
return rawWidth !== null ? (rawWidth * fontSize) / 1000 : null;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Checks if the font has width data for a character code.
|
|
239
|
+
*
|
|
240
|
+
* @param charCode - The character code to check
|
|
241
|
+
* @returns True if width data is available, false otherwise
|
|
242
|
+
*/
|
|
243
|
+
hasCharacterWidth(charCode) {
|
|
244
|
+
return this.getRawCharacterWidth(charCode) !== null;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Gets character widths for all characters in a string.
|
|
248
|
+
* Returns null for characters not in the font's width table.
|
|
249
|
+
*
|
|
250
|
+
* @param text - The text to get character widths for
|
|
251
|
+
* @param fontSize - The font size to scale to
|
|
252
|
+
* @returns Array of character widths (null for missing characters)
|
|
253
|
+
*/
|
|
254
|
+
getCharacterWidthsForString(text, fontSize) {
|
|
255
|
+
const widths = [];
|
|
256
|
+
for (const char of text) {
|
|
257
|
+
const charCode = char.charCodeAt(0);
|
|
258
|
+
widths.push(this.getCharacterWidth(charCode, fontSize));
|
|
259
|
+
}
|
|
260
|
+
return widths;
|
|
171
261
|
}
|
|
172
262
|
/**
|
|
173
263
|
* Returns the resource name for string coercion.
|
|
@@ -179,27 +269,13 @@ export class PdfFont extends PdfDictionary {
|
|
|
179
269
|
toString() {
|
|
180
270
|
return this.resourceName;
|
|
181
271
|
}
|
|
182
|
-
/**
|
|
183
|
-
* Returns all objects that need to be committed to the PDF.
|
|
184
|
-
* Includes auxiliary objects (descriptors, streams) and the container.
|
|
185
|
-
*/
|
|
186
|
-
getObjectsToCommit() {
|
|
187
|
-
const objects = [...this.auxiliaryObjects];
|
|
188
|
-
if (this.container) {
|
|
189
|
-
objects.push(this.container);
|
|
190
|
-
}
|
|
191
|
-
return objects;
|
|
192
|
-
}
|
|
193
272
|
/**
|
|
194
273
|
* @internal
|
|
195
274
|
* Legacy property for backward compatibility with code that accesses fontRef.
|
|
196
|
-
* Returns the
|
|
275
|
+
* Returns this font's reference since PdfFont IS the indirect object.
|
|
197
276
|
*/
|
|
198
277
|
get fontRef() {
|
|
199
|
-
|
|
200
|
-
throw new Error('Font has not been written to PDF yet');
|
|
201
|
-
}
|
|
202
|
-
return this.container.reference;
|
|
278
|
+
return this.reference;
|
|
203
279
|
}
|
|
204
280
|
/**
|
|
205
281
|
* Creates a PdfFont from font file bytes.
|
|
@@ -235,16 +311,140 @@ export class PdfFont extends PdfDictionary {
|
|
|
235
311
|
* These fonts don't require font data as they're built into PDF viewers.
|
|
236
312
|
*
|
|
237
313
|
* @param fontName - One of the 14 standard PDF fonts
|
|
314
|
+
* @param widths - Optional AFM widths array (1/1000 em units) for chars 32–126
|
|
238
315
|
* @returns A PdfFont instance ready to be written to the PDF
|
|
239
316
|
*/
|
|
240
|
-
static fromStandardFont(fontName) {
|
|
317
|
+
static fromStandardFont(fontName, widths) {
|
|
241
318
|
const font = new PdfFont({ fontName });
|
|
242
319
|
// Build Type1 font dictionary
|
|
243
|
-
font.set('Type', new PdfName('Font'));
|
|
244
|
-
font.set('Subtype', new PdfName('Type1'));
|
|
245
|
-
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));
|
|
323
|
+
if (widths) {
|
|
324
|
+
font.firstChar = 32;
|
|
325
|
+
font.lastChar = 32 + widths.length - 1;
|
|
326
|
+
font.widths = widths;
|
|
327
|
+
}
|
|
246
328
|
return font;
|
|
247
329
|
}
|
|
330
|
+
// Helvetica AFM widths, chars 32–126 (95 values, 1/1000 em units)
|
|
331
|
+
static _HELVETICA_WIDTHS = [
|
|
332
|
+
278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333,
|
|
333
|
+
278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 278, 278,
|
|
334
|
+
584, 584, 584, 556, 1015, 667, 667, 722, 722, 667, 611, 778, 722, 278,
|
|
335
|
+
500, 667, 556, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944,
|
|
336
|
+
667, 667, 611, 278, 278, 278, 469, 556, 333, 556, 556, 500, 556, 556,
|
|
337
|
+
278, 556, 556, 222, 222, 500, 222, 833, 556, 556, 556, 556, 333, 500,
|
|
338
|
+
278, 556, 500, 722, 500, 500, 500, 334, 260, 334, 584,
|
|
339
|
+
];
|
|
340
|
+
// Helvetica-Bold AFM widths, chars 32–126
|
|
341
|
+
static _HELVETICA_BOLD_WIDTHS = [
|
|
342
|
+
278, 333, 474, 556, 556, 889, 722, 238, 333, 333, 389, 584, 278, 333,
|
|
343
|
+
278, 278, 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, 333, 333,
|
|
344
|
+
584, 584, 584, 611, 975, 722, 722, 722, 722, 667, 611, 778, 722, 278,
|
|
345
|
+
556, 722, 611, 833, 722, 778, 667, 778, 722, 667, 611, 722, 667, 944,
|
|
346
|
+
667, 667, 611, 333, 278, 333, 584, 556, 333, 556, 611, 556, 611, 556,
|
|
347
|
+
333, 611, 611, 278, 278, 556, 278, 889, 611, 611, 611, 611, 389, 556,
|
|
348
|
+
333, 611, 556, 778, 556, 556, 500, 389, 280, 389, 584,
|
|
349
|
+
];
|
|
350
|
+
// Times-Roman AFM widths, chars 32–126
|
|
351
|
+
static _TIMES_ROMAN_WIDTHS = [
|
|
352
|
+
250, 333, 408, 500, 500, 833, 778, 180, 333, 333, 500, 564, 250, 333,
|
|
353
|
+
250, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 278, 278,
|
|
354
|
+
564, 564, 564, 444, 921, 722, 667, 667, 722, 611, 556, 722, 722, 333,
|
|
355
|
+
389, 722, 611, 889, 722, 722, 556, 722, 667, 556, 611, 722, 722, 944,
|
|
356
|
+
722, 722, 611, 333, 278, 333, 469, 500, 333, 444, 500, 444, 500, 444,
|
|
357
|
+
333, 500, 500, 278, 278, 500, 278, 778, 500, 500, 500, 500, 333, 389,
|
|
358
|
+
278, 500, 500, 722, 500, 500, 444, 480, 200, 480, 541,
|
|
359
|
+
];
|
|
360
|
+
// Times-Bold AFM widths, chars 32–126
|
|
361
|
+
static _TIMES_BOLD_WIDTHS = [
|
|
362
|
+
250, 333, 555, 500, 500, 1000, 833, 278, 333, 333, 500, 570, 250, 333,
|
|
363
|
+
250, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 333, 333,
|
|
364
|
+
570, 570, 570, 500, 930, 722, 667, 722, 722, 667, 611, 778, 778, 389,
|
|
365
|
+
500, 778, 667, 944, 722, 778, 611, 778, 722, 556, 667, 722, 722, 1000,
|
|
366
|
+
722, 722, 667, 333, 278, 333, 581, 500, 333, 500, 556, 444, 556, 444,
|
|
367
|
+
333, 500, 556, 278, 333, 556, 278, 833, 556, 500, 556, 556, 444, 389,
|
|
368
|
+
333, 556, 500, 722, 500, 500, 444, 394, 220, 394, 520,
|
|
369
|
+
];
|
|
370
|
+
// Times-Italic AFM widths, chars 32–126
|
|
371
|
+
static _TIMES_ITALIC_WIDTHS = [
|
|
372
|
+
250, 333, 420, 500, 500, 833, 778, 214, 333, 333, 500, 675, 250, 333,
|
|
373
|
+
250, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 333, 333,
|
|
374
|
+
675, 675, 675, 500, 920, 611, 611, 667, 722, 611, 611, 722, 722, 333,
|
|
375
|
+
444, 667, 556, 833, 667, 722, 611, 722, 611, 500, 556, 722, 611, 833,
|
|
376
|
+
611, 556, 556, 389, 278, 389, 422, 500, 333, 500, 500, 444, 500, 444,
|
|
377
|
+
278, 500, 500, 278, 278, 444, 278, 722, 500, 500, 500, 500, 389, 389,
|
|
378
|
+
278, 500, 444, 667, 444, 444, 389, 400, 275, 400, 541,
|
|
379
|
+
];
|
|
380
|
+
// Times-BoldItalic AFM widths, chars 32–126
|
|
381
|
+
static _TIMES_BOLD_ITALIC_WIDTHS = [
|
|
382
|
+
250, 389, 555, 500, 500, 833, 778, 278, 333, 333, 500, 570, 250, 333,
|
|
383
|
+
250, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 333, 333,
|
|
384
|
+
570, 570, 570, 500, 832, 667, 667, 667, 722, 667, 667, 722, 778, 389,
|
|
385
|
+
500, 667, 611, 889, 722, 722, 611, 722, 667, 556, 611, 722, 667, 889,
|
|
386
|
+
667, 611, 611, 333, 278, 333, 570, 500, 333, 500, 500, 444, 500, 444,
|
|
387
|
+
333, 500, 556, 278, 278, 500, 278, 778, 556, 500, 500, 500, 389, 389,
|
|
388
|
+
278, 556, 444, 667, 500, 444, 389, 348, 220, 348, 570,
|
|
389
|
+
];
|
|
390
|
+
static HELVETICA = PdfFont.fromStandardFont('Helvetica', [
|
|
391
|
+
...PdfFont._HELVETICA_WIDTHS,
|
|
392
|
+
]);
|
|
393
|
+
static HELVETICA_BOLD = PdfFont.fromStandardFont('Helvetica-Bold', [...PdfFont._HELVETICA_BOLD_WIDTHS]);
|
|
394
|
+
static HELVETICA_OBLIQUE = PdfFont.fromStandardFont('Helvetica-Oblique', [...PdfFont._HELVETICA_WIDTHS]);
|
|
395
|
+
static HELVETICA_BOLD_OBLIQUE = PdfFont.fromStandardFont('Helvetica-BoldOblique', [...PdfFont._HELVETICA_BOLD_WIDTHS]);
|
|
396
|
+
static TIMES_ROMAN = PdfFont.fromStandardFont('Times-Roman', [
|
|
397
|
+
...PdfFont._TIMES_ROMAN_WIDTHS,
|
|
398
|
+
]);
|
|
399
|
+
static TIMES_BOLD = PdfFont.fromStandardFont('Times-Bold', [
|
|
400
|
+
...PdfFont._TIMES_BOLD_WIDTHS,
|
|
401
|
+
]);
|
|
402
|
+
static TIMES_ITALIC = PdfFont.fromStandardFont('Times-Italic', [
|
|
403
|
+
...PdfFont._TIMES_ITALIC_WIDTHS,
|
|
404
|
+
]);
|
|
405
|
+
static TIMES_BOLD_ITALIC = PdfFont.fromStandardFont('Times-BoldItalic', [...PdfFont._TIMES_BOLD_ITALIC_WIDTHS]);
|
|
406
|
+
static COURIER = PdfFont.fromStandardFont('Courier', Array(95).fill(600));
|
|
407
|
+
static COURIER_BOLD = PdfFont.fromStandardFont('Courier-Bold', Array(95).fill(600));
|
|
408
|
+
static COURIER_OBLIQUE = PdfFont.fromStandardFont('Courier-Oblique', Array(95).fill(600));
|
|
409
|
+
static COURIER_BOLD_OBLIQUE = PdfFont.fromStandardFont('Courier-BoldOblique', Array(95).fill(600));
|
|
410
|
+
static SYMBOL = PdfFont.fromStandardFont('Symbol');
|
|
411
|
+
static ZAPF_DINGBATS = PdfFont.fromStandardFont('ZapfDingbats');
|
|
412
|
+
static BY_BASE_FONT = new Map([
|
|
413
|
+
['Helvetica', PdfFont.HELVETICA],
|
|
414
|
+
['Helv', PdfFont.HELVETICA], // Alias for backward compatibility with old code using 'Helv'
|
|
415
|
+
['Helvetica-Bold', PdfFont.HELVETICA_BOLD],
|
|
416
|
+
['Helv-Bold', PdfFont.HELVETICA_BOLD], // Alias for backward compatibility with old code using 'Helv-Bold'
|
|
417
|
+
['Helvetica-Oblique', PdfFont.HELVETICA_OBLIQUE],
|
|
418
|
+
['Helv-Oblique', PdfFont.HELVETICA_OBLIQUE], // Alias for backward compatibility with old code using 'Helv-Oblique'
|
|
419
|
+
['Helvetica-BoldOblique', PdfFont.HELVETICA_BOLD_OBLIQUE],
|
|
420
|
+
['Helv-BoldOblique', PdfFont.HELVETICA_BOLD_OBLIQUE], // Alias for backward compatibility with old code using 'Helv-BoldOblique'
|
|
421
|
+
['Times-Roman', PdfFont.TIMES_ROMAN],
|
|
422
|
+
['TiRo', PdfFont.TIMES_ROMAN],
|
|
423
|
+
['Times-Bold', PdfFont.TIMES_BOLD],
|
|
424
|
+
['TiBo', PdfFont.TIMES_BOLD],
|
|
425
|
+
['Times-Italic', PdfFont.TIMES_ITALIC],
|
|
426
|
+
['TiIt', PdfFont.TIMES_ITALIC],
|
|
427
|
+
['Times-BoldItalic', PdfFont.TIMES_BOLD_ITALIC],
|
|
428
|
+
['TiBI', PdfFont.TIMES_BOLD_ITALIC],
|
|
429
|
+
['Courier', PdfFont.COURIER],
|
|
430
|
+
['Cour', PdfFont.COURIER], // Alias for backward compatibility with old code using 'Cour'
|
|
431
|
+
['Courier-Bold', PdfFont.COURIER_BOLD],
|
|
432
|
+
['Cour-Bold', PdfFont.COURIER_BOLD], // Alias for backward compatibility with old code using 'Cour-Bold'
|
|
433
|
+
['Courier-Oblique', PdfFont.COURIER_OBLIQUE],
|
|
434
|
+
['Cour-Oblique', PdfFont.COURIER_OBLIQUE], // Alias for backward compatibility with old code using 'Cour-Oblique'
|
|
435
|
+
['Courier-BoldOblique', PdfFont.COURIER_BOLD_OBLIQUE],
|
|
436
|
+
['Cour-BoldOblique', PdfFont.COURIER_BOLD_OBLIQUE], // Alias for backward compatibility with old code using 'Cour-BoldOblique'
|
|
437
|
+
['Symbol', PdfFont.SYMBOL],
|
|
438
|
+
['ZapfDingbats', PdfFont.ZAPF_DINGBATS],
|
|
439
|
+
['ZaDb', PdfFont.ZAPF_DINGBATS],
|
|
440
|
+
]);
|
|
441
|
+
/**
|
|
442
|
+
* Returns the static PdfFont instance for a standard font name, or null if not found.
|
|
443
|
+
*/
|
|
444
|
+
static getStandardFont(fontName) {
|
|
445
|
+
const font = PdfFont.BY_BASE_FONT.get(fontName);
|
|
446
|
+
return font ?? null;
|
|
447
|
+
}
|
|
248
448
|
/**
|
|
249
449
|
* Creates a TrueType font from font file data.
|
|
250
450
|
* Uses WinAnsiEncoding for standard 8-bit character support.
|
|
@@ -293,28 +493,26 @@ export class PdfFont extends PdfDictionary {
|
|
|
293
493
|
const fontDescriptorObject = new PdfIndirectObject({
|
|
294
494
|
content: fontDescriptorDict,
|
|
295
495
|
});
|
|
296
|
-
// Store auxiliary objects
|
|
297
|
-
font.auxiliaryObjects.push(fontFileObject, fontDescriptorObject);
|
|
298
496
|
// Build TrueType font dictionary
|
|
299
|
-
font.set('Type', new PdfName('Font'));
|
|
300
|
-
font.set('Subtype', new PdfName('TrueType'));
|
|
301
|
-
font.set('BaseFont', new PdfName(descriptor.fontName));
|
|
302
|
-
font.set('FontDescriptor', fontDescriptorObject.reference);
|
|
303
|
-
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'));
|
|
304
502
|
// Add width information for proper glyph rendering
|
|
305
503
|
const firstChar = descriptor.firstChar ?? 32;
|
|
306
504
|
const lastChar = descriptor.lastChar ?? 255;
|
|
307
|
-
font.set('FirstChar', new PdfNumber(firstChar));
|
|
308
|
-
font.set('LastChar', new PdfNumber(lastChar));
|
|
505
|
+
font.content.set('FirstChar', new PdfNumber(firstChar));
|
|
506
|
+
font.content.set('LastChar', new PdfNumber(lastChar));
|
|
309
507
|
if (descriptor.widths) {
|
|
310
|
-
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))));
|
|
311
509
|
}
|
|
312
510
|
else {
|
|
313
511
|
// Default: 1000 (standard em-square)
|
|
314
512
|
const defaultWidths = Array(lastChar - firstChar + 1)
|
|
315
513
|
.fill(0)
|
|
316
514
|
.map(() => new PdfNumber(1000));
|
|
317
|
-
font.set('Widths', new PdfArray(defaultWidths));
|
|
515
|
+
font.content.set('Widths', new PdfArray(defaultWidths));
|
|
318
516
|
}
|
|
319
517
|
return font;
|
|
320
518
|
}
|
|
@@ -367,8 +565,6 @@ export class PdfFont extends PdfDictionary {
|
|
|
367
565
|
const fontDescriptorObject = new PdfIndirectObject({
|
|
368
566
|
content: fontDescriptorDict,
|
|
369
567
|
});
|
|
370
|
-
// Store descriptor and font file
|
|
371
|
-
font.auxiliaryObjects.push(fontFileObject, fontDescriptorObject);
|
|
372
568
|
// Create CIDFont dictionary (descendant font)
|
|
373
569
|
const cidFontDict = new PdfDictionary();
|
|
374
570
|
cidFontDict.set('Type', new PdfName('Font'));
|
|
@@ -391,13 +587,12 @@ export class PdfFont extends PdfDictionary {
|
|
|
391
587
|
const cidFontObject = new PdfIndirectObject({
|
|
392
588
|
content: cidFontDict,
|
|
393
589
|
});
|
|
394
|
-
font.auxiliaryObjects.push(cidFontObject);
|
|
395
590
|
// Build Type0 font dictionary
|
|
396
|
-
font.set('Type', new PdfName('Font'));
|
|
397
|
-
font.set('Subtype', new PdfName('Type0'));
|
|
398
|
-
font.set('BaseFont', new PdfName(`${descriptor.fontName}-Identity-H`));
|
|
399
|
-
font.set('Encoding', new PdfName('Identity-H'));
|
|
400
|
-
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]));
|
|
401
596
|
// Create ToUnicode CMap if mappings provided
|
|
402
597
|
if (unicodeMappings && unicodeMappings.size > 0) {
|
|
403
598
|
const cmapContent = PdfFont.generateToUnicodeCMap(unicodeMappings);
|
|
@@ -409,11 +604,34 @@ export class PdfFont extends PdfDictionary {
|
|
|
409
604
|
const cmapObject = new PdfIndirectObject({
|
|
410
605
|
content: cmapStream,
|
|
411
606
|
});
|
|
412
|
-
font.set('ToUnicode', cmapObject.reference);
|
|
413
|
-
font.auxiliaryObjects.push(cmapObject);
|
|
607
|
+
font.content.set('ToUnicode', cmapObject.reference);
|
|
414
608
|
}
|
|
415
609
|
return font;
|
|
416
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
|
+
}
|
|
417
635
|
/**
|
|
418
636
|
* Generates a ToUnicode CMap for mapping CIDs to Unicode code points.
|
|
419
637
|
*/
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/pdf/index.d.ts
CHANGED
package/dist/pdf/index.js
CHANGED