@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 CHANGED
@@ -1,4 +1,15 @@
1
- # `@peculiar/pdf-font`
1
+ <h1 align="center">
2
+ @peculiar/pdf-font
3
+ </h1>
4
+
5
+ <div align="center">
6
+
7
+ ![GitHub License](https://img.shields.io/github/license/PeculiarVentures/pdf)
8
+ ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/PeculiarVentures/pdf/release.yml?branch=main&label=release)
9
+ [![npm version](https://img.shields.io/npm/v/@peculiar/pdf-font.svg)](https://www.npmjs.com/package/@peculiar/pdf-font)
10
+ [![npm downloads](https://img.shields.io/npm/dm/@peculiar/pdf-font.svg)](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 'pvtsutils';
2
-
3
- declare enum DefaultFonts {
4
- Courier = "Courier",
5
- CourierBold = "Courier-Bold",
6
- CourierBoldOblique = "Courier-BoldOblique",
7
- CourierOblique = "Courier-Oblique",
8
- Helvetica = "Helvetica",
9
- HelveticaBold = "Helvetica-Bold",
10
- HelveticaBoldOblique = "Helvetica-BoldOblique",
11
- HelveticaOblique = "Helvetica-Oblique",
12
- Symbol = "Symbol",
13
- TimesBold = "Times-Bold",
14
- TimesBoldItalic = "Times-BoldItalic",
15
- TimesItalic = "Times-Italic",
16
- TimesRoman = "Times-Roman",
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
- * Index of the glyph in the font
29
- */
30
- index: number;
31
- /**
32
- * The glyph's advance width, in font units.
33
- */
34
- advanceWidth: number;
35
- /**
36
- * Unicode scalar value(s) associated with the glyph
37
- */
38
- unicode: number[];
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
- declare class FontGlyph implements IFontGlyph {
42
- index: number;
43
- advanceWidth: number;
44
- unicode: number[];
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
- declare class FontHead {
49
- xMin: number;
50
- xMax: number;
51
- yMin: number;
52
- yMax: number;
44
+ export declare class FontHead {
45
+ xMin: number;
46
+ xMax: number;
47
+ yMin: number;
48
+ yMax: number;
53
49
  }
54
-
55
- declare class FontName {
56
- fontFamily: string;
57
- fontSubfamily: string;
58
- fullName: string;
59
- postScriptName: string;
50
+ export declare class FontName {
51
+ fontFamily: string;
52
+ fontSubfamily: string;
53
+ fullName: string;
54
+ postScriptName: string;
60
55
  }
61
-
62
- declare class FontOS2 {
63
- sCapHeight: number;
64
- sxHeight: number;
65
- usWeightClass: number;
56
+ export declare class FontOS2 {
57
+ sCapHeight: number;
58
+ sxHeight: number;
59
+ usWeightClass: number;
66
60
  }
67
-
68
- declare class FontPost {
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
- * The number of font units per em square.
78
- */
79
- unitsPerEm: number;
80
- /**
81
- * The font's ascent value, defined as the vertical distance from the baseline to the top of the font.
82
- */
83
- ascent: number;
84
- /**
85
- * The font's descent value, defined as the vertical distance from the baseline to the bottom of the font.
86
- */
87
- descent: number;
88
- /**
89
- * Looks for the GLYPH by the unicode
90
- * @param code unicode
91
- * @returns
92
- */
93
- findGlyph(code: number): IFontGlyph | null;
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
- name: FontName;
103
- head: FontHead;
104
- os2: FontOS2;
105
- post: FontPost;
106
- glyphs: FontGlyph[];
107
- unitsPerEm: number;
108
- ascent: number;
109
- descent: number;
110
- /**
111
- * Looks for the GLYPH by the unicode
112
- * @param code unicode
113
- * @returns
114
- */
115
- findGlyph(code: number): FontGlyph | null;
116
- /**
117
- * Returns a list of unique glyphs for all characters in the specified text.
118
- * The glyphs are sorted by their index in ascending order.
119
- * @param text - The input text to get glyphs for
120
- * @returns Array of FontGlyph objects corresponding to unique characters in the text
121
- */
122
- getGlyphs(text: string): FontGlyph[];
123
- /**
124
- * Compares two FontGlyph objects by their index for sorting purposes.
125
- * @param a - First FontGlyph to compare
126
- * @param b - Second FontGlyph to compare
127
- * @returns -1 if a < b, 1 if a > b, 0 if equal
128
- * @private
129
- */
130
- private static sortByIndex;
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
- private static readonly cache;
140
- /**
141
- * Creates a FontInfo object from either a default font or a buffer source.
142
- * @param source - The font source, either a DefaultFonts enum value or a BufferSource
143
- * @returns A FontInfo object representing the font
144
- */
145
- static create(source: DefaultFonts | BufferSource): FontInfo;
146
- /**
147
- * Creates a FontInfo object from a default font.
148
- * Uses internal caching to improve performance for frequently used fonts.
149
- * @param source - The default font to create
150
- * @returns A FontInfo object representing the default font
151
- * @throws Error if the specified default font doesn't exist
152
- */
153
- static createDefault(source: DefaultFonts): FontInfo;
154
- /**
155
- * Creates a FontInfo object from a font file buffer.
156
- * @param source - The font file buffer
157
- * @returns A FontInfo object containing the parsed font data
158
- * @throws Error if required font tables are missing
159
- */
160
- static createFile(source: BufferSource): FontInfo;
161
- /**
162
- * Gets glyph indexes for the specified text from a font.
163
- * @param fontjs - The font object
164
- * @param text - The text to get indexes for
165
- * @returns An array of glyph indexes
166
- * @private
167
- */
168
- private static getIndexes;
169
- /**
170
- * Creates a subset of a font containing only the glyphs needed for the specified text.
171
- * @param source - The original font buffer
172
- * @param text - The text that determines which glyphs to include in the subset
173
- * @returns A new ArrayBuffer containing the subsetted font
174
- */
175
- static subsetFont(source: BufferSource, text: string): ArrayBuffer;
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
+ }