pdfkit 0.20.2 → 0.21.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/README.md +35 -0
- package/js/pdfkit.browser.mjs +2409 -433
- package/js/pdfkit.browser.mjs.map +1 -1
- package/js/pdfkit.js +2451 -453
- package/js/pdfkit.js.map +1 -1
- package/js/pdfkit.node.mjs +2443 -449
- package/js/pdfkit.node.mjs.map +1 -1
- package/js/pdfkit.standalone.js +21578 -32704
- package/js/standard-fonts/Symbol.cjs +4 -3
- package/js/standard-fonts/Symbol.mjs +4 -3
- package/js/standard-fonts/ZapfDingbats.cjs +4 -3
- package/js/standard-fonts/ZapfDingbats.mjs +4 -3
- package/package.json +24 -21
- package/.yarn/install-state.gz +0 -0
- package/.yarn/releases/yarn-4.16.0.cjs +0 -944
- package/.yarnrc.yml +0 -10
- package/CHANGELOG.md +0 -198
- package/js/data/Courier-Bold.afm +0 -342
- package/js/data/Courier-BoldOblique.afm +0 -342
- package/js/data/Courier-Oblique.afm +0 -342
- package/js/data/Courier.afm +0 -342
- package/js/data/Helvetica-Bold.afm +0 -2827
- package/js/data/Helvetica-BoldOblique.afm +0 -2827
- package/js/data/Helvetica-Oblique.afm +0 -3051
- package/js/data/Helvetica.afm +0 -3051
- package/js/data/Symbol.afm +0 -213
- package/js/data/Times-Bold.afm +0 -2588
- package/js/data/Times-BoldItalic.afm +0 -2384
- package/js/data/Times-Italic.afm +0 -2667
- package/js/data/Times-Roman.afm +0 -2419
- package/js/data/ZapfDingbats.afm +0 -225
- package/js/pdfkit.browser.js +0 -7530
- package/js/pdfkit.browser.js.map +0 -1
- package/js/pdfkit.browser.old.js +0 -7517
- package/js/pdfkit.browser.old.mjs +0 -7342
- package/js/pdfkit.old.js +0 -7276
- package/jsconfig.json +0 -13
- package/rollup.config.mjs +0 -193
- package/tools/README.md +0 -124
- package/tools/afm-converter.js +0 -317
- package/tools/asset-plugin.mjs +0 -17
- package/tools/binary-plugin.mjs +0 -18
- package/tools/convert-afm-parsed.js +0 -8
- package/tools/convert-afm-runtime.js +0 -8
- package/types/jest.custom-matchers.d.ts +0 -28
- package/vitest.config.js +0 -13
package/jsconfig.json
DELETED
package/rollup.config.mjs
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { babel } from '@rollup/plugin-babel';
|
|
2
|
-
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
|
3
|
-
import asset from './tools/asset-plugin.mjs';
|
|
4
|
-
import binary from './tools/binary-plugin.mjs';
|
|
5
|
-
|
|
6
|
-
const iccProfilePath = 'lib/mixins/data/sRGB_IEC61966_2_1.icc';
|
|
7
|
-
|
|
8
|
-
const external = [
|
|
9
|
-
'module',
|
|
10
|
-
'stream',
|
|
11
|
-
'fs',
|
|
12
|
-
'zlib',
|
|
13
|
-
'fflate',
|
|
14
|
-
'fontkit',
|
|
15
|
-
'events',
|
|
16
|
-
'linebreak',
|
|
17
|
-
'png-js',
|
|
18
|
-
'js-md5',
|
|
19
|
-
'@noble/hashes/utils',
|
|
20
|
-
'@noble/hashes/sha256',
|
|
21
|
-
'@noble/ciphers/aes',
|
|
22
|
-
'crypto',
|
|
23
|
-
'saslprep',
|
|
24
|
-
];
|
|
25
|
-
|
|
26
|
-
const supportedBrowsers = [
|
|
27
|
-
'Firefox 115', // ESR from 2023
|
|
28
|
-
'iOS 16', // from 2022
|
|
29
|
-
'Safari 16', // from 2022
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
const standardFontInputs = {
|
|
33
|
-
Courier: 'lib/font/generated/Courier.js',
|
|
34
|
-
CourierBold: 'lib/font/generated/CourierBold.js',
|
|
35
|
-
CourierBoldOblique: 'lib/font/generated/CourierBoldOblique.js',
|
|
36
|
-
CourierOblique: 'lib/font/generated/CourierOblique.js',
|
|
37
|
-
Helvetica: 'lib/font/generated/Helvetica.js',
|
|
38
|
-
HelveticaBold: 'lib/font/generated/HelveticaBold.js',
|
|
39
|
-
HelveticaBoldOblique: 'lib/font/generated/HelveticaBoldOblique.js',
|
|
40
|
-
HelveticaOblique: 'lib/font/generated/HelveticaOblique.js',
|
|
41
|
-
Symbol: 'lib/font/generated/Symbol.js',
|
|
42
|
-
TimesBold: 'lib/font/generated/TimesBold.js',
|
|
43
|
-
TimesBoldItalic: 'lib/font/generated/TimesBoldItalic.js',
|
|
44
|
-
TimesItalic: 'lib/font/generated/TimesItalic.js',
|
|
45
|
-
TimesRoman: 'lib/font/generated/TimesRoman.js',
|
|
46
|
-
ZapfDingbats: 'lib/font/generated/ZapfDingbats.js',
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const browserPlugins = () => [
|
|
50
|
-
binary('.icc'),
|
|
51
|
-
nodeResolve({
|
|
52
|
-
exportConditions: ['default'],
|
|
53
|
-
}),
|
|
54
|
-
babel({
|
|
55
|
-
babelHelpers: 'bundled',
|
|
56
|
-
babelrc: false,
|
|
57
|
-
targets: {
|
|
58
|
-
browsers: supportedBrowsers,
|
|
59
|
-
},
|
|
60
|
-
presets: [
|
|
61
|
-
[
|
|
62
|
-
'@babel/preset-env',
|
|
63
|
-
{
|
|
64
|
-
modules: false,
|
|
65
|
-
targets: {
|
|
66
|
-
browsers: supportedBrowsers,
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
],
|
|
70
|
-
],
|
|
71
|
-
comments: false,
|
|
72
|
-
}),
|
|
73
|
-
];
|
|
74
|
-
|
|
75
|
-
export default [
|
|
76
|
-
// CommonJS and ES builds for Node
|
|
77
|
-
{
|
|
78
|
-
input: 'lib/document.node.js',
|
|
79
|
-
external,
|
|
80
|
-
output: [
|
|
81
|
-
{
|
|
82
|
-
name: 'pdfkit',
|
|
83
|
-
file: 'js/pdfkit.js',
|
|
84
|
-
format: 'cjs',
|
|
85
|
-
sourcemap: true,
|
|
86
|
-
interop: 'default',
|
|
87
|
-
exports: 'named',
|
|
88
|
-
footer:
|
|
89
|
-
'module.exports = exports.default;\nmodule.exports.PDFDocument = exports.PDFDocument;\nmodule.exports.LineWrapper = exports.LineWrapper;\nmodule.exports.registerFile = exports.registerFile;',
|
|
90
|
-
},
|
|
91
|
-
{
|
|
92
|
-
file: 'js/pdfkit.node.mjs',
|
|
93
|
-
format: 'es',
|
|
94
|
-
sourcemap: true,
|
|
95
|
-
},
|
|
96
|
-
],
|
|
97
|
-
plugins: [
|
|
98
|
-
asset(iccProfilePath, 'data/sRGB_IEC61966_2_1.icc'),
|
|
99
|
-
nodeResolve({
|
|
100
|
-
exportConditions: ['node'],
|
|
101
|
-
}),
|
|
102
|
-
babel({
|
|
103
|
-
targets: {
|
|
104
|
-
node: '20',
|
|
105
|
-
},
|
|
106
|
-
babelHelpers: 'bundled',
|
|
107
|
-
babelrc: false,
|
|
108
|
-
presets: [
|
|
109
|
-
[
|
|
110
|
-
'@babel/preset-env',
|
|
111
|
-
{
|
|
112
|
-
modules: false,
|
|
113
|
-
targets: {
|
|
114
|
-
node: '20',
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
],
|
|
118
|
-
],
|
|
119
|
-
comments: false,
|
|
120
|
-
}),
|
|
121
|
-
],
|
|
122
|
-
},
|
|
123
|
-
// ES for green browsers
|
|
124
|
-
{
|
|
125
|
-
input: 'lib/document.browser.js',
|
|
126
|
-
external,
|
|
127
|
-
output: {
|
|
128
|
-
name: 'pdfkit.es',
|
|
129
|
-
file: 'js/pdfkit.browser.mjs',
|
|
130
|
-
format: 'es',
|
|
131
|
-
sourcemap: true,
|
|
132
|
-
},
|
|
133
|
-
plugins: browserPlugins(),
|
|
134
|
-
},
|
|
135
|
-
// CommonJS browser build consumed by Browserify for the standalone bundle.
|
|
136
|
-
{
|
|
137
|
-
input: 'lib/document.browser.standalone.js',
|
|
138
|
-
external,
|
|
139
|
-
output: {
|
|
140
|
-
file: 'js/pdfkit.browser.js',
|
|
141
|
-
format: 'cjs',
|
|
142
|
-
sourcemap: true,
|
|
143
|
-
interop: 'default',
|
|
144
|
-
exports: 'named',
|
|
145
|
-
footer:
|
|
146
|
-
'module.exports = exports.default;\nmodule.exports.PDFDocument = exports.PDFDocument;',
|
|
147
|
-
},
|
|
148
|
-
plugins: browserPlugins(),
|
|
149
|
-
},
|
|
150
|
-
// Experimental helpers for collecting document output.
|
|
151
|
-
{
|
|
152
|
-
input: 'lib/output.js',
|
|
153
|
-
output: [
|
|
154
|
-
{
|
|
155
|
-
file: 'js/output.cjs',
|
|
156
|
-
format: 'cjs',
|
|
157
|
-
exports: 'named',
|
|
158
|
-
},
|
|
159
|
-
{
|
|
160
|
-
file: 'js/output.mjs',
|
|
161
|
-
format: 'es',
|
|
162
|
-
},
|
|
163
|
-
],
|
|
164
|
-
plugins: browserPlugins(),
|
|
165
|
-
},
|
|
166
|
-
// Standard font data is kept outside the main bundles so it can be loaded
|
|
167
|
-
// individually on demand.
|
|
168
|
-
{
|
|
169
|
-
input: standardFontInputs,
|
|
170
|
-
output: [
|
|
171
|
-
{
|
|
172
|
-
dir: 'js/standard-fonts',
|
|
173
|
-
format: 'cjs',
|
|
174
|
-
entryFileNames: '[name].cjs',
|
|
175
|
-
chunkFileNames: 'chunks/[name]-[hash].cjs',
|
|
176
|
-
exports: 'default',
|
|
177
|
-
},
|
|
178
|
-
{
|
|
179
|
-
dir: 'js/standard-fonts',
|
|
180
|
-
format: 'es',
|
|
181
|
-
entryFileNames: '[name].mjs',
|
|
182
|
-
chunkFileNames: 'chunks/[name]-[hash].mjs',
|
|
183
|
-
},
|
|
184
|
-
],
|
|
185
|
-
plugins: [
|
|
186
|
-
babel({
|
|
187
|
-
babelHelpers: 'bundled',
|
|
188
|
-
babelrc: false,
|
|
189
|
-
comments: false,
|
|
190
|
-
}),
|
|
191
|
-
],
|
|
192
|
-
},
|
|
193
|
-
];
|
package/tools/README.md
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
# AFM conversion tools
|
|
2
|
-
|
|
3
|
-
The AFM conversion tools generate one ECMAScript module per input font. Runtime
|
|
4
|
-
conversion also generates a shared glyph-name module when Courier, Helvetica,
|
|
5
|
-
or Times is present. PDFKit emits each generated runtime module as separate CJS
|
|
6
|
-
and ESM artifacts. Node loads them on demand, while browser applications import
|
|
7
|
-
and register only the standard fonts they use.
|
|
8
|
-
|
|
9
|
-
Raw AFM files remain the source of truth and are retained in distributions.
|
|
10
|
-
The runtime modules in `lib/font/generated` are committed build inputs. Do not
|
|
11
|
-
edit them directly; regenerate them from the AFM sources and format the result.
|
|
12
|
-
|
|
13
|
-
## Commands
|
|
14
|
-
|
|
15
|
-
Generate compact runtime data:
|
|
16
|
-
|
|
17
|
-
```sh
|
|
18
|
-
node tools/convert-afm-runtime.js --output-dir /tmp/pdfkit-afm-runtime
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Generate the complete parsed AFM state used for converter verification:
|
|
22
|
-
|
|
23
|
-
```sh
|
|
24
|
-
node tools/convert-afm-parsed.js --output-dir /tmp/pdfkit-afm-parsed
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
With no input paths, each command converts all bundled AFMs. Pass one or more
|
|
28
|
-
AFM paths after `--output-dir` to convert only selected fonts. Both converters
|
|
29
|
-
use the same normalized filenames, such as `Helvetica.js`,
|
|
30
|
-
`HelveticaBold.js`, and `TimesBoldItalic.js`, so use separate output
|
|
31
|
-
directories when retaining both formats.
|
|
32
|
-
|
|
33
|
-
Runtime conversion also creates `standardGlyphNames.js` when a font uses the
|
|
34
|
-
standard Roman glyph table. Each font file is an importable ES module with a
|
|
35
|
-
default export.
|
|
36
|
-
|
|
37
|
-
## Parsed-data schema
|
|
38
|
-
|
|
39
|
-
The parsed-data module exports the parser's three data structures without
|
|
40
|
-
normalizing their values:
|
|
41
|
-
|
|
42
|
-
```js
|
|
43
|
-
export default {
|
|
44
|
-
attributes: { FontName: 'Helvetica' },
|
|
45
|
-
glyphWidths: { space: 278 },
|
|
46
|
-
kernPairs: { 'A\0V': -70 },
|
|
47
|
-
};
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
This format is immediately usable after JavaScript module parsing and serves
|
|
51
|
-
as the low-decoding-cost baseline. Because glyph names are repeated in the
|
|
52
|
-
large kerning maps, it is only modestly smaller than AFM for proportional
|
|
53
|
-
fonts.
|
|
54
|
-
|
|
55
|
-
## Runtime schema
|
|
56
|
-
|
|
57
|
-
The runtime module keeps the font-level values PDFKit consumes and only the
|
|
58
|
-
glyphs reachable through its current 256-entry character mapping:
|
|
59
|
-
|
|
60
|
-
```js
|
|
61
|
-
import glyphNames from './standardGlyphNames.js';
|
|
62
|
-
|
|
63
|
-
export default {
|
|
64
|
-
name: 'Helvetica',
|
|
65
|
-
bbox: [-166, -225, 1000, 931],
|
|
66
|
-
ascender: 718,
|
|
67
|
-
descender: -207,
|
|
68
|
-
xHeight: 523,
|
|
69
|
-
capHeight: 718,
|
|
70
|
-
glyphNames,
|
|
71
|
-
glyphWidths: [278, 278, 355],
|
|
72
|
-
kernPairs: [-70, [12, 4, 9]],
|
|
73
|
-
};
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
`standardGlyphNames.js` exports the identical space-separated 215-name table
|
|
77
|
-
used by all Courier, Helvetica, and Times variants. Bundlers include this
|
|
78
|
-
dependency once when one or more of those fonts are used. Symbol, ZapfDingbats,
|
|
79
|
-
and nonmatching AFMs keep their glyph-name strings inline.
|
|
80
|
-
|
|
81
|
-
`glyphWidths` uses indexes matching `glyphNames`. `kernPairs` alternates a
|
|
82
|
-
kerning amount and an array of delta-encoded pair IDs. A pair ID is
|
|
83
|
-
`leftIndex * glyphCount + rightIndex`. Pair IDs are sorted within each amount
|
|
84
|
-
group before delta encoding.
|
|
85
|
-
|
|
86
|
-
Decoder pseudocode:
|
|
87
|
-
|
|
88
|
-
```js
|
|
89
|
-
const names = data.glyphNames.split(' ');
|
|
90
|
-
const widths = Object.fromEntries(
|
|
91
|
-
names.map((name, index) => [name, data.glyphWidths[index]]),
|
|
92
|
-
);
|
|
93
|
-
const kernPairs = {};
|
|
94
|
-
|
|
95
|
-
for (let index = 0; index < data.kernPairs.length; index += 2) {
|
|
96
|
-
const amount = data.kernPairs[index];
|
|
97
|
-
let pairId = 0;
|
|
98
|
-
|
|
99
|
-
for (const delta of data.kernPairs[index + 1]) {
|
|
100
|
-
pairId += delta;
|
|
101
|
-
const left = Math.floor(pairId / names.length);
|
|
102
|
-
const right = pairId % names.length;
|
|
103
|
-
kernPairs[`${names[left]}\0${names[right]}`] = amount;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
## Format rationale
|
|
109
|
-
|
|
110
|
-
Across PDFKit's 14 AFMs, the raw sources are about 619 KB. Generated output,
|
|
111
|
-
including license headers, is about 550 KB for direct parsed data and 52 KB for
|
|
112
|
-
compact runtime data. Sharing the 1,492-byte Roman glyph-name string removes 11
|
|
113
|
-
duplicate copies from a complete runtime conversion. These size limits are
|
|
114
|
-
also guarded by the converter tests.
|
|
115
|
-
|
|
116
|
-
[`@pdf-lib/standard-fonts`](https://github.com/Hopding/standard-fonts) deflates
|
|
117
|
-
base64-encoded JSON, which is compact but requires inflation and JSON parsing
|
|
118
|
-
at runtime. [jsPDF](https://raw.githubusercontent.com/parallax/jsPDF/master/src/modules/standard_fonts_metrics.js)
|
|
119
|
-
uses custom numeric packing and a character-level decoder. The runtime format
|
|
120
|
-
here borrows numeric indexing and value grouping while retaining native
|
|
121
|
-
JavaScript numbers and arrays, avoiding an inflate dependency or custom token
|
|
122
|
-
parser. The source fields and `KPX` behavior follow the
|
|
123
|
-
[Adobe AFM specification](https://www.adobe.com/content/dam/acom/en/devnet/font/pdfs/5004.AFM_Spec.pdf)
|
|
124
|
-
and PDFKit's existing parser.
|
package/tools/afm-converter.js
DELETED
|
@@ -1,317 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
|
|
4
|
-
const RUNTIME_GLYPH_NAMES = `\
|
|
5
|
-
.notdef space exclam quotedbl numbersign dollar percent ampersand quotesingle
|
|
6
|
-
parenleft parenright asterisk plus comma hyphen period slash zero one two three
|
|
7
|
-
four five six seven eight nine colon semicolon less equal greater question at A
|
|
8
|
-
B C D E F G H I J K L M N O P Q R S T U V W X Y Z bracketleft backslash
|
|
9
|
-
bracketright asciicircum underscore grave a b c d e f g h i j k l m n o p q r
|
|
10
|
-
s t u v w x y z braceleft bar braceright asciitilde Euro quotesinglbase florin
|
|
11
|
-
quotedblbase ellipsis dagger daggerdbl circumflex perthousand Scaron
|
|
12
|
-
guilsinglleft OE Zcaron quoteleft quoteright quotedblleft quotedblright bullet
|
|
13
|
-
endash emdash tilde trademark scaron guilsinglright oe zcaron ydieresis
|
|
14
|
-
exclamdown cent sterling currency yen brokenbar section dieresis copyright
|
|
15
|
-
ordfeminine guillemotleft logicalnot registered macron degree plusminus
|
|
16
|
-
twosuperior threesuperior acute mu paragraph periodcentered cedilla onesuperior
|
|
17
|
-
ordmasculine guillemotright onequarter onehalf threequarters questiondown Agrave
|
|
18
|
-
Aacute Acircumflex Atilde Adieresis Aring AE Ccedilla Egrave Eacute Ecircumflex
|
|
19
|
-
Edieresis Igrave Iacute Icircumflex Idieresis Eth Ntilde Ograve Oacute
|
|
20
|
-
Ocircumflex Otilde Odieresis multiply Oslash Ugrave Uacute Ucircumflex Udieresis
|
|
21
|
-
Yacute Thorn germandbls agrave aacute acircumflex atilde adieresis aring ae
|
|
22
|
-
ccedilla egrave eacute ecircumflex edieresis igrave iacute icircumflex idieresis
|
|
23
|
-
eth ntilde ograve oacute ocircumflex otilde odieresis divide oslash ugrave
|
|
24
|
-
uacute ucircumflex udieresis yacute thorn`.split(/\s+/);
|
|
25
|
-
|
|
26
|
-
const STANDARD_GLYPH_NAMES = RUNTIME_GLYPH_NAMES.filter(
|
|
27
|
-
(name) => name !== '.notdef',
|
|
28
|
-
).join(' ');
|
|
29
|
-
const STANDARD_GLYPH_NAMES_FILE = 'standardGlyphNames.js';
|
|
30
|
-
const DEFAULT_AFM_DIRECTORY = path.join(__dirname, '../lib/font/data');
|
|
31
|
-
|
|
32
|
-
const ADOBE_DISTRIBUTION_NOTICE =
|
|
33
|
-
'This file and the 14 PostScript(R) AFM files it accompanies may be used, ' +
|
|
34
|
-
'copied, and distributed for any purpose and without charge, with or ' +
|
|
35
|
-
'without modification, provided that all copyright notices are retained; ' +
|
|
36
|
-
'that the AFM files are not distributed without this file; that all ' +
|
|
37
|
-
'modifications to this file or any of the AFM files are prominently noted ' +
|
|
38
|
-
'in the modified file(s); and that this paragraph is not modified. Adobe ' +
|
|
39
|
-
'Systems has no responsibility or obligation to support the use of the AFM ' +
|
|
40
|
-
'files.';
|
|
41
|
-
|
|
42
|
-
function parseAFM(contents) {
|
|
43
|
-
const attributes = {};
|
|
44
|
-
const glyphWidths = {};
|
|
45
|
-
const kernPairs = {};
|
|
46
|
-
let section = '';
|
|
47
|
-
|
|
48
|
-
for (const line of contents.split('\n')) {
|
|
49
|
-
let match;
|
|
50
|
-
let attribute;
|
|
51
|
-
|
|
52
|
-
if ((match = line.match(/^Start(\w+)/))) {
|
|
53
|
-
section = match[1];
|
|
54
|
-
continue;
|
|
55
|
-
} else if (/^End(\w+)/.test(line)) {
|
|
56
|
-
section = '';
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
switch (section) {
|
|
61
|
-
case 'FontMetrics': {
|
|
62
|
-
match = line.match(/(^\w+)\s+(.*)/);
|
|
63
|
-
if (!match) {
|
|
64
|
-
throw new Error(`Invalid font metrics line: ${line}`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const key = match[1];
|
|
68
|
-
const value = match[2];
|
|
69
|
-
if ((attribute = attributes[key])) {
|
|
70
|
-
if (!Array.isArray(attribute)) {
|
|
71
|
-
attribute = attributes[key] = [attribute];
|
|
72
|
-
}
|
|
73
|
-
attribute.push(value);
|
|
74
|
-
} else {
|
|
75
|
-
attributes[key] = value;
|
|
76
|
-
}
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
case 'CharMetrics': {
|
|
81
|
-
if (!/^CH?\s/.test(line)) {
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const nameMatch = line.match(/\bN\s+(\.?\w+)\s*;/);
|
|
86
|
-
const widthMatch = line.match(/\bWX\s+(\d+)\s*;/);
|
|
87
|
-
if (!nameMatch || !widthMatch) {
|
|
88
|
-
throw new Error(`Invalid character metrics line: ${line}`);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
glyphWidths[nameMatch[1]] = +widthMatch[1];
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
case 'KernPairs': {
|
|
96
|
-
match = line.match(/^KPX\s+(\.?\w+)\s+(\.?\w+)\s+(-?\d+)/);
|
|
97
|
-
if (match) {
|
|
98
|
-
kernPairs[`${match[1]}\0${match[2]}`] = parseInt(match[3]);
|
|
99
|
-
}
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (!attributes.FontName || !attributes.FontBBox) {
|
|
106
|
-
throw new Error('AFM data is missing FontName or FontBBox.');
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return { attributes, glyphWidths, kernPairs };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function createParsedData(parsed) {
|
|
113
|
-
return parsed;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function createRuntimeData(parsed) {
|
|
117
|
-
const { attributes, glyphWidths, kernPairs } = parsed;
|
|
118
|
-
const glyphNames = RUNTIME_GLYPH_NAMES.filter((name) =>
|
|
119
|
-
Object.hasOwn(glyphWidths, name),
|
|
120
|
-
);
|
|
121
|
-
const glyphIndexes = Object.fromEntries(
|
|
122
|
-
glyphNames.map((name, index) => [name, index]),
|
|
123
|
-
);
|
|
124
|
-
const pairsByAmount = new Map();
|
|
125
|
-
|
|
126
|
-
for (const [pair, amount] of Object.entries(kernPairs)) {
|
|
127
|
-
const [left, right] = pair.split('\0');
|
|
128
|
-
if (!(left in glyphIndexes) || !(right in glyphIndexes)) {
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
let pairIds = pairsByAmount.get(amount);
|
|
133
|
-
if (!pairIds) {
|
|
134
|
-
pairIds = [];
|
|
135
|
-
pairsByAmount.set(amount, pairIds);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
pairIds.push(glyphIndexes[left] * glyphNames.length + glyphIndexes[right]);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const compactKernPairs = [];
|
|
142
|
-
const amounts = [...pairsByAmount.keys()].sort((a, b) => a - b);
|
|
143
|
-
for (const amount of amounts) {
|
|
144
|
-
const pairIds = pairsByAmount.get(amount).sort((a, b) => a - b);
|
|
145
|
-
const deltas = pairIds.map((pairId, index) =>
|
|
146
|
-
index === 0 ? pairId : pairId - pairIds[index - 1],
|
|
147
|
-
);
|
|
148
|
-
compactKernPairs.push(amount, deltas);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return {
|
|
152
|
-
name: attributes.FontName,
|
|
153
|
-
bbox: attributes.FontBBox.trim().split(/\s+/).map(Number),
|
|
154
|
-
ascender: +(attributes.Ascender || 0),
|
|
155
|
-
descender: +(attributes.Descender || 0),
|
|
156
|
-
xHeight: +(attributes.XHeight || 0),
|
|
157
|
-
capHeight: +(attributes.CapHeight || 0),
|
|
158
|
-
glyphNames: glyphNames.join(' '),
|
|
159
|
-
glyphWidths: glyphNames.map((name) => glyphWidths[name]),
|
|
160
|
-
kernPairs: compactKernPairs,
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function asArray(value) {
|
|
165
|
-
return Array.isArray(value) ? value : value ? [value] : [];
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function createHeader(inputName, attributes) {
|
|
169
|
-
const notices = asArray(attributes.Notice).map((notice) => ` * ${notice}`);
|
|
170
|
-
|
|
171
|
-
return [
|
|
172
|
-
'/*',
|
|
173
|
-
` * Generated from ${inputName}. Do not edit directly.`,
|
|
174
|
-
' * The source AFM metrics were converted to JavaScript; this is a modification.',
|
|
175
|
-
...notices,
|
|
176
|
-
' *',
|
|
177
|
-
` * ${ADOBE_DISTRIBUTION_NOTICE}`,
|
|
178
|
-
' */',
|
|
179
|
-
].join('\n');
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function createModule(inputName, parsed, format) {
|
|
183
|
-
const header = createHeader(inputName, parsed.attributes);
|
|
184
|
-
if (format === 'parsed') {
|
|
185
|
-
return {
|
|
186
|
-
output: `${header}\nexport default ${JSON.stringify(createParsedData(parsed))};\n`,
|
|
187
|
-
usesStandardGlyphNames: false,
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const data = createRuntimeData(parsed);
|
|
192
|
-
if (data.glyphNames !== STANDARD_GLYPH_NAMES) {
|
|
193
|
-
return {
|
|
194
|
-
output: `${header}\nexport default ${JSON.stringify(data)};\n`,
|
|
195
|
-
usesStandardGlyphNames: false,
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const serialized = JSON.stringify(data).replace(
|
|
200
|
-
`"glyphNames":${JSON.stringify(STANDARD_GLYPH_NAMES)}`,
|
|
201
|
-
'glyphNames',
|
|
202
|
-
);
|
|
203
|
-
return {
|
|
204
|
-
output:
|
|
205
|
-
`${header}\n` +
|
|
206
|
-
`import glyphNames from './${STANDARD_GLYPH_NAMES_FILE}';\n` +
|
|
207
|
-
`export default ${serialized};\n`,
|
|
208
|
-
usesStandardGlyphNames: true,
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function createStandardGlyphNamesModule() {
|
|
213
|
-
return (
|
|
214
|
-
'/* Generated by tools/convert-afm-runtime.js. Do not edit directly. */\n' +
|
|
215
|
-
`export default ${JSON.stringify(STANDARD_GLYPH_NAMES)};\n`
|
|
216
|
-
);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function parseArguments(argv) {
|
|
220
|
-
let outputDirectory;
|
|
221
|
-
const inputs = [];
|
|
222
|
-
|
|
223
|
-
for (let index = 0; index < argv.length; index++) {
|
|
224
|
-
const argument = argv[index];
|
|
225
|
-
if (argument === '--output-dir') {
|
|
226
|
-
outputDirectory = argv[++index];
|
|
227
|
-
if (!outputDirectory || outputDirectory.startsWith('--')) {
|
|
228
|
-
throw new Error('--output-dir requires a directory.');
|
|
229
|
-
}
|
|
230
|
-
} else if (argument.startsWith('--')) {
|
|
231
|
-
throw new Error(`Unknown option: ${argument}`);
|
|
232
|
-
} else {
|
|
233
|
-
inputs.push(argument);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
if (!outputDirectory) {
|
|
238
|
-
throw new Error('Usage: --output-dir <directory> [font.afm ...]');
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (inputs.length === 0) {
|
|
242
|
-
inputs.push(
|
|
243
|
-
...fs
|
|
244
|
-
.readdirSync(DEFAULT_AFM_DIRECTORY)
|
|
245
|
-
.filter((file) => path.extname(file).toLowerCase() === '.afm')
|
|
246
|
-
.sort()
|
|
247
|
-
.map((file) => path.join(DEFAULT_AFM_DIRECTORY, file)),
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return { outputDirectory, inputs };
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
function convertFiles(argv, format, log = console.log) {
|
|
255
|
-
const { outputDirectory, inputs } = parseArguments(argv);
|
|
256
|
-
const conversions = [];
|
|
257
|
-
const outputNames = new Set();
|
|
258
|
-
|
|
259
|
-
for (const input of inputs) {
|
|
260
|
-
if (path.extname(input).toLowerCase() !== '.afm') {
|
|
261
|
-
throw new Error(`Expected an .afm input file: ${input}`);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
const basename = path.basename(input, path.extname(input));
|
|
265
|
-
const outputName = `${basename.replaceAll('-', '')}.js`;
|
|
266
|
-
if (outputNames.has(outputName)) {
|
|
267
|
-
throw new Error(`Duplicate output filename: ${outputName}`);
|
|
268
|
-
}
|
|
269
|
-
outputNames.add(outputName);
|
|
270
|
-
|
|
271
|
-
const contents = fs.readFileSync(input, 'utf8');
|
|
272
|
-
const parsed = parseAFM(contents);
|
|
273
|
-
const generatedModule = createModule(path.basename(input), parsed, format);
|
|
274
|
-
conversions.push({
|
|
275
|
-
input,
|
|
276
|
-
inputBytes: Buffer.byteLength(contents),
|
|
277
|
-
outputName,
|
|
278
|
-
...generatedModule,
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
fs.mkdirSync(outputDirectory, { recursive: true });
|
|
283
|
-
if (
|
|
284
|
-
conversions.some(({ usesStandardGlyphNames }) => usesStandardGlyphNames)
|
|
285
|
-
) {
|
|
286
|
-
const output = createStandardGlyphNamesModule();
|
|
287
|
-
const outputPath = path.join(outputDirectory, STANDARD_GLYPH_NAMES_FILE);
|
|
288
|
-
fs.writeFileSync(outputPath, output);
|
|
289
|
-
log(
|
|
290
|
-
`Standard glyph names -> ${outputPath} ` +
|
|
291
|
-
`(${Buffer.byteLength(output)} bytes)`,
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
for (const conversion of conversions) {
|
|
296
|
-
const outputPath = path.join(outputDirectory, conversion.outputName);
|
|
297
|
-
fs.writeFileSync(outputPath, conversion.output);
|
|
298
|
-
const outputBytes = Buffer.byteLength(conversion.output);
|
|
299
|
-
const reduction = (100 * (1 - outputBytes / conversion.inputBytes)).toFixed(
|
|
300
|
-
1,
|
|
301
|
-
);
|
|
302
|
-
log(
|
|
303
|
-
`${conversion.input} -> ${outputPath} ` +
|
|
304
|
-
`(${conversion.inputBytes} -> ${outputBytes} bytes, ${reduction}% smaller)`,
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
module.exports = {
|
|
310
|
-
convertFiles,
|
|
311
|
-
createParsedData,
|
|
312
|
-
createRuntimeData,
|
|
313
|
-
parseAFM,
|
|
314
|
-
RUNTIME_GLYPH_NAMES,
|
|
315
|
-
STANDARD_GLYPH_NAMES,
|
|
316
|
-
STANDARD_GLYPH_NAMES_FILE,
|
|
317
|
-
};
|
package/tools/asset-plugin.mjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { readFile } from 'fs/promises';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Emits a source file as an asset at a fixed path in the Rollup output.
|
|
5
|
-
*/
|
|
6
|
-
export default function asset(sourcePath, outputPath) {
|
|
7
|
-
return {
|
|
8
|
-
name: 'asset',
|
|
9
|
-
async buildStart() {
|
|
10
|
-
this.emitFile({
|
|
11
|
-
type: 'asset',
|
|
12
|
-
fileName: outputPath,
|
|
13
|
-
source: await readFile(sourcePath),
|
|
14
|
-
});
|
|
15
|
-
},
|
|
16
|
-
};
|
|
17
|
-
}
|
package/tools/binary-plugin.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { readFile } from 'fs/promises';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Bundles binary files (by extension) as base64 strings, so they can be
|
|
5
|
-
* imported without reading them from disk at runtime.
|
|
6
|
-
*/
|
|
7
|
-
export default function binary(...extensions) {
|
|
8
|
-
return {
|
|
9
|
-
name: 'binary',
|
|
10
|
-
enforce: 'pre',
|
|
11
|
-
async load(id) {
|
|
12
|
-
if (!extensions.some((extension) => id.endsWith(extension))) return null;
|
|
13
|
-
|
|
14
|
-
const data = await readFile(id);
|
|
15
|
-
return `export default ${JSON.stringify(data.toString('base64'))};`;
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
}
|