@peculiar/pdf-font 1.17.1 → 2.0.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/README.md +21 -1
- package/build/index.d.ts +139 -151
- package/build/index.js +288 -0
- package/package.json +38 -31
- package/build/peculiar.pdf.font.es.js +0 -369
- package/build/peculiar.pdf.font.js +0 -393
package/README.md
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
@peculiar/pdf-font
|
|
3
|
+
</h1>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+

|
|
9
|
+
[](https://www.npmjs.com/package/@peculiar/pdf-font)
|
|
10
|
+
[](https://www.npmjs.com/package/@peculiar/pdf-font)
|
|
11
|
+
|
|
12
|
+
</div>
|
|
2
13
|
|
|
3
14
|
A library for working with fonts in PDF documents, providing utilities for font loading, embedding, and text rendering.
|
|
4
15
|
|
|
@@ -8,6 +19,15 @@ A library for working with fonts in PDF documents, providing utilities for font
|
|
|
8
19
|
npm install @peculiar/pdf-font
|
|
9
20
|
```
|
|
10
21
|
|
|
22
|
+
## Breaking Changes In v2
|
|
23
|
+
|
|
24
|
+
v2 is ESM-only and requires Node.js `^20.19.0 || >=22.12.0` (1.x declared no `engines`). CommonJS code can still `require("@peculiar/pdf-font")` on these versions; Node.js 22.0–22.11 can't. See the [full notes](https://github.com/PeculiarVentures/pdf#breaking-changes-in-v2).
|
|
25
|
+
|
|
26
|
+
- **TypeScript compiling to CommonJS:** use `"module": "node20"` or `"nodenext"` (`node16` fails with TS1479). ESM and bundler setups need no changes.
|
|
27
|
+
- **Jest in CommonJS mode:** switch to ESM mode or Vitest, or configure an ESM-to-CommonJS transformer and make sure `transformIgnorePatterns` doesn't exclude the `@peculiar/*` packages.
|
|
28
|
+
- **Deep imports** (`@peculiar/pdf-font/build/...`) are blocked by `exports`; import from `@peculiar/pdf-font`.
|
|
29
|
+
- **Peer dependencies:** `@peculiar/asn1-schema` (`^3.0.0`) is a peer, shared with the other `@peculiar/asn1-*` packages in your app. npm installs peers automatically.
|
|
30
|
+
|
|
11
31
|
## Usage
|
|
12
32
|
|
|
13
33
|
### Basic Font Operations
|
package/build/index.d.ts
CHANGED
|
@@ -1,178 +1,166 @@
|
|
|
1
|
-
import { BufferSource } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
ZapfDingbats = "ZapfDingbats"
|
|
1
|
+
import { BufferSource } from "@peculiar/utils";
|
|
2
|
+
export declare enum DefaultFonts {
|
|
3
|
+
Courier = "Courier",
|
|
4
|
+
CourierBold = "Courier-Bold",
|
|
5
|
+
CourierBoldOblique = "Courier-BoldOblique",
|
|
6
|
+
CourierOblique = "Courier-Oblique",
|
|
7
|
+
Helvetica = "Helvetica",
|
|
8
|
+
HelveticaBold = "Helvetica-Bold",
|
|
9
|
+
HelveticaBoldOblique = "Helvetica-BoldOblique",
|
|
10
|
+
HelveticaOblique = "Helvetica-Oblique",
|
|
11
|
+
Symbol = "Symbol",
|
|
12
|
+
TimesBold = "Times-Bold",
|
|
13
|
+
TimesBoldItalic = "Times-BoldItalic",
|
|
14
|
+
TimesItalic = "Times-Italic",
|
|
15
|
+
TimesRoman = "Times-Roman",
|
|
16
|
+
ZapfDingbats = "ZapfDingbats"
|
|
18
17
|
}
|
|
19
|
-
declare const DefaultFontsZip: Map<DefaultFonts, string>;
|
|
20
|
-
|
|
18
|
+
export declare const DefaultFontsZip: Map<DefaultFonts, string>;
|
|
21
19
|
/**
|
|
22
20
|
* Interface representing a font glyph
|
|
23
21
|
* @remarks
|
|
24
22
|
* A font glyph is a specific graphical representation of a character in a font.
|
|
25
23
|
*/
|
|
26
|
-
interface IFontGlyph {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
24
|
+
export interface IFontGlyph {
|
|
25
|
+
/**
|
|
26
|
+
* Index of the glyph in the font
|
|
27
|
+
*/
|
|
28
|
+
index: number;
|
|
29
|
+
/**
|
|
30
|
+
* The glyph's advance width, in font units.
|
|
31
|
+
*/
|
|
32
|
+
advanceWidth: number;
|
|
33
|
+
/**
|
|
34
|
+
* Unicode scalar value(s) associated with the glyph
|
|
35
|
+
*/
|
|
36
|
+
unicode: number[];
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
constructor(params?: Partial<FontGlyph>);
|
|
38
|
+
export declare class FontGlyph implements IFontGlyph {
|
|
39
|
+
index: number;
|
|
40
|
+
advanceWidth: number;
|
|
41
|
+
unicode: number[];
|
|
42
|
+
constructor(params?: Partial<FontGlyph>);
|
|
46
43
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
yMax: number;
|
|
44
|
+
export declare class FontHead {
|
|
45
|
+
xMin: number;
|
|
46
|
+
xMax: number;
|
|
47
|
+
yMin: number;
|
|
48
|
+
yMax: number;
|
|
53
49
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
postScriptName: string;
|
|
50
|
+
export declare class FontName {
|
|
51
|
+
fontFamily: string;
|
|
52
|
+
fontSubfamily: string;
|
|
53
|
+
fullName: string;
|
|
54
|
+
postScriptName: string;
|
|
60
55
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
usWeightClass: number;
|
|
56
|
+
export declare class FontOS2 {
|
|
57
|
+
sCapHeight: number;
|
|
58
|
+
sxHeight: number;
|
|
59
|
+
usWeightClass: number;
|
|
66
60
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
italicAngle: number;
|
|
61
|
+
export declare class FontPost {
|
|
62
|
+
italicAngle: number;
|
|
70
63
|
}
|
|
71
|
-
|
|
72
64
|
/**
|
|
73
65
|
* Information about a font.
|
|
74
66
|
*/
|
|
75
|
-
interface IFontInfo {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
67
|
+
export interface IFontInfo {
|
|
68
|
+
/**
|
|
69
|
+
* The number of font units per em square.
|
|
70
|
+
*/
|
|
71
|
+
unitsPerEm: number;
|
|
72
|
+
/**
|
|
73
|
+
* The font's ascent value, defined as the vertical distance from the baseline to the top of the font.
|
|
74
|
+
*/
|
|
75
|
+
ascent: number;
|
|
76
|
+
/**
|
|
77
|
+
* The font's descent value, defined as the vertical distance from the baseline to the bottom of the font.
|
|
78
|
+
*/
|
|
79
|
+
descent: number;
|
|
80
|
+
/**
|
|
81
|
+
* Looks for the GLYPH by the unicode
|
|
82
|
+
* @param code unicode
|
|
83
|
+
* @returns
|
|
84
|
+
*/
|
|
85
|
+
findGlyph(code: number): IFontGlyph | null;
|
|
94
86
|
}
|
|
95
|
-
|
|
96
87
|
/**
|
|
97
88
|
* Implementation of IFontInfo interface that provides comprehensive information about a font.
|
|
98
89
|
* Contains font metrics, glyph data, and various font properties from different font tables
|
|
99
90
|
* (name, head, OS/2, post).
|
|
100
91
|
*/
|
|
101
|
-
declare class FontInfo implements IFontInfo {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
92
|
+
export declare class FontInfo implements IFontInfo {
|
|
93
|
+
name: FontName;
|
|
94
|
+
head: FontHead;
|
|
95
|
+
os2: FontOS2;
|
|
96
|
+
post: FontPost;
|
|
97
|
+
glyphs: FontGlyph[];
|
|
98
|
+
unitsPerEm: number;
|
|
99
|
+
ascent: number;
|
|
100
|
+
descent: number;
|
|
101
|
+
/**
|
|
102
|
+
* Looks for the GLYPH by the unicode
|
|
103
|
+
* @param code unicode
|
|
104
|
+
* @returns
|
|
105
|
+
*/
|
|
106
|
+
findGlyph(code: number): FontGlyph | null;
|
|
107
|
+
/**
|
|
108
|
+
* Returns a list of unique glyphs for all characters in the specified text.
|
|
109
|
+
* The glyphs are sorted by their index in ascending order.
|
|
110
|
+
* @param text - The input text to get glyphs for
|
|
111
|
+
* @returns Array of FontGlyph objects corresponding to unique characters in the text
|
|
112
|
+
*/
|
|
113
|
+
getGlyphs(text: string): FontGlyph[];
|
|
114
|
+
/**
|
|
115
|
+
* Compares two FontGlyph objects by their index for sorting purposes.
|
|
116
|
+
* @param a - First FontGlyph to compare
|
|
117
|
+
* @param b - Second FontGlyph to compare
|
|
118
|
+
* @returns -1 if a < b, 1 if a > b, 0 if equal
|
|
119
|
+
* @private
|
|
120
|
+
*/
|
|
121
|
+
private static sortByIndex;
|
|
131
122
|
}
|
|
132
|
-
|
|
133
123
|
/**
|
|
134
124
|
* Factory class for creating and managing font objects.
|
|
135
125
|
* Provides functionality to create fonts from different sources,
|
|
136
126
|
* manage default fonts, and create font subsets.
|
|
137
127
|
*/
|
|
138
|
-
declare abstract class FontFactory {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export { DefaultFonts, DefaultFontsZip, FontFactory, FontGlyph, FontHead, FontInfo, FontName, FontOS2, type IFontGlyph, type IFontInfo };
|
|
128
|
+
export declare abstract class FontFactory {
|
|
129
|
+
private static readonly cache;
|
|
130
|
+
/**
|
|
131
|
+
* Creates a FontInfo object from either a default font or a buffer source.
|
|
132
|
+
* @param source - The font source, either a DefaultFonts enum value or a BufferSource
|
|
133
|
+
* @returns A FontInfo object representing the font
|
|
134
|
+
*/
|
|
135
|
+
static create(source: DefaultFonts | BufferSource): FontInfo;
|
|
136
|
+
/**
|
|
137
|
+
* Creates a FontInfo object from a default font.
|
|
138
|
+
* Uses internal caching to improve performance for frequently used fonts.
|
|
139
|
+
* @param source - The default font to create
|
|
140
|
+
* @returns A FontInfo object representing the default font
|
|
141
|
+
* @throws Error if the specified default font doesn't exist
|
|
142
|
+
*/
|
|
143
|
+
static createDefault(source: DefaultFonts): FontInfo;
|
|
144
|
+
/**
|
|
145
|
+
* Creates a FontInfo object from a font file buffer.
|
|
146
|
+
* @param source - The font file buffer
|
|
147
|
+
* @returns A FontInfo object containing the parsed font data
|
|
148
|
+
* @throws Error if required font tables are missing
|
|
149
|
+
*/
|
|
150
|
+
static createFile(source: BufferSource): FontInfo;
|
|
151
|
+
/**
|
|
152
|
+
* Gets glyph indexes for the specified text from a font.
|
|
153
|
+
* @param fontjs - The font object
|
|
154
|
+
* @param text - The text to get indexes for
|
|
155
|
+
* @returns An array of glyph indexes
|
|
156
|
+
* @private
|
|
157
|
+
*/
|
|
158
|
+
private static getIndexes;
|
|
159
|
+
/**
|
|
160
|
+
* Creates a subset of a font containing only the glyphs needed for the specified text.
|
|
161
|
+
* @param source - The original font buffer
|
|
162
|
+
* @param text - The text that determines which glyphs to include in the subset
|
|
163
|
+
* @returns A new ArrayBuffer containing the subsetted font
|
|
164
|
+
*/
|
|
165
|
+
static subsetFont(source: BufferSource, text: string): ArrayBuffer;
|
|
166
|
+
}
|