Skip to content

Commit 61f51f3

Browse files
authored
Replace fontkit with fontkitten (#238)
1 parent eb8fa57 commit 61f51f3

File tree

9 files changed

+69
-122
lines changed

9 files changed

+69
-122
lines changed

.changeset/perfect-islands-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@capsizecss/unpack": patch
3+
---
4+
5+
Reduces `@capsizecss/unpack` install size by using a lighter weight package for extracting font file metrics

.changeset/silver-buttons-bake.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@capsizecss/unpack": major
3+
---
4+
5+
This package is now ESM-only.
6+
7+
In most projects you can continue to use the package as before. CommonJS (CJS) projects using Node.js <20, should update to use a dynamic import:
8+
9+
```js
10+
// For CJS projects before Node 20
11+
const { fromBuffer } = await import('@capsizecss/unpack');
12+
13+
// For all other projects
14+
import { fromBuffer } from '@capsizecss/unpack';
15+
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ See the [package](packages/unpack/README.md) for documentation.
401401
## Thanks
402402

403403
- [Vincent De Oliveira](https://twitter.com/iamvdo) for writing [Deep dive CSS: font metrics, line-height and vertical-align](https://iamvdo.me/en/blog/css-font-metrics-line-height-and-vertical-align), which provided the research needed to build all this.
404-
- [Devon Govett](https://github.com/devongovett) for creating [Fontkit](https://github.com/foliojs/fontkit), which does all the heavy lifting of extracting the font metrics under the covers.
404+
- [Devon Govett](https://github.com/devongovett) for creating [Fontkit](https://github.com/foliojs/fontkit). A [fork of Fontkit](https://github.com/delucis/fontkitten) does all the heavy lifting of extracting the font metrics under the covers.
405405
- [SEEK](https://www.seek.com.au) for giving us the space to do interesting work.
406406

407407
## License

packages/unpack/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ The font metrics object returned contains the following properties:
100100

101101
## Thanks
102102

103-
- [Devon Govett](https://github.com/devongovett) for creating [Fontkit](https://github.com/foliojs/fontkit), which does all the heavy lifting of extracting the font metrics under the covers.
103+
- [Devon Govett](https://github.com/devongovett) for creating [Fontkit](https://github.com/foliojs/fontkit). A [fork of Fontkit](https://github.com/delucis/fontkitten) does all the heavy lifting of extracting the font metrics under the covers.
104104
- [SEEK](https://www.seek.com.au) for giving us the space to do interesting work.
105105

106106
## License

packages/unpack/package.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@
2323
"name": "Michael Taranto",
2424
"homepage": "https://github.com/michaeltaranto"
2525
},
26+
"type": "module",
2627
"exports": {
2728
".": {
2829
"@capsizecss/src": "./src/index.ts",
29-
"import": "./dist/index.mjs",
30-
"require": "./dist/index.cjs"
30+
"default": "./dist/index.mjs"
3131
},
3232
"./package.json": "./package.json"
3333
},
34-
"main": "./dist/index.cjs",
3534
"module": "./dist/index.mjs",
36-
"types": "./dist/index.d.cts",
35+
"types": "./dist/index.d.mts",
3736
"files": [
3837
"dist"
3938
],
@@ -42,10 +41,9 @@
4241
"generate": "tsx scripts/generate-weightings"
4342
},
4443
"dependencies": {
45-
"fontkit": "^2.0.2"
44+
"fontkitten": "^1.0.0"
4645
},
4746
"devDependencies": {
48-
"@types/fontkit": "^2.0.1",
4947
"@types/node": "^22.18.8",
5048
"fast-xml-parser": "^4.3.2",
5149
"sort-keys": "^5.0.0",
@@ -57,11 +55,9 @@
5755
},
5856
"publishConfig": {
5957
"exports": {
60-
".": {
61-
"import": "./dist/index.mjs",
62-
"require": "./dist/index.cjs"
63-
},
58+
".": "./dist/index.mjs",
6459
"./package.json": "./package.json"
6560
}
66-
}
61+
},
62+
"main": "./dist/index.mjs"
6763
}

packages/unpack/scripts/generate-weightings.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import fs from 'fs/promises';
2-
import path from 'path';
1+
import fs from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
34
import { XMLParser } from 'fast-xml-parser';
45
import sortKeys from 'sort-keys';
56

7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8+
69
type WikiNewsFeed = {
710
feed: {
811
doc: {

packages/unpack/src/index.ts

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import * as fontkit from 'fontkit';
2-
import type { Font as FontKitFont } from 'fontkit';
1+
import {
2+
create,
3+
type Font as FontKitFont,
4+
type FontCollection,
5+
} from 'fontkitten';
6+
import { readFile } from 'node:fs/promises';
37

48
import weightings from './weightings';
59

@@ -83,17 +87,14 @@ const unpackMetricsFromFont = (font: FontKitFont) => {
8387

8488
export type Font = ReturnType<typeof unpackMetricsFromFont>;
8589

86-
const handleCollectionErrors = ({
87-
font,
88-
postscriptName,
89-
apiName,
90-
apiParamName,
91-
}: {
92-
font: FontKitFont | null;
93-
postscriptName?: string;
94-
apiName: string;
95-
apiParamName: string;
96-
}) => {
90+
function handleCollectionErrors(
91+
font: FontKitFont | FontCollection | null,
92+
{
93+
postscriptName,
94+
apiName,
95+
apiParamName,
96+
}: { postscriptName?: string; apiName: string; apiParamName: string },
97+
): asserts font is FontKitFont {
9798
if (postscriptName && font === null) {
9899
throw new Error(
99100
[
@@ -108,7 +109,7 @@ const handleCollectionErrors = ({
108109
);
109110
}
110111

111-
if (font !== null && 'fonts' in font && Array.isArray(font.fonts)) {
112+
if (font !== null && font.isCollection) {
112113
const availableNames = font.fonts.map((f) => f.postscriptName);
113114
throw new Error(
114115
[
@@ -126,25 +127,18 @@ const handleCollectionErrors = ({
126127
].join('\n'),
127128
);
128129
}
129-
};
130+
}
130131

131132
interface Options {
132133
postscriptName?: string;
133134
}
134135

135-
export const fromFile = (path: string, options?: Options): Promise<Font> => {
136-
const { postscriptName } = options || {};
137-
138-
return fontkit.open(path, postscriptName).then((font) => {
139-
handleCollectionErrors({
140-
font,
141-
postscriptName,
142-
apiName: 'fromFile',
143-
apiParamName: 'path',
144-
});
145-
146-
return unpackMetricsFromFont(font);
147-
});
136+
export const fromFile = async (
137+
path: string,
138+
options?: Options,
139+
): Promise<Font> => {
140+
const buffer = await readFile(path);
141+
return _fromBuffer(buffer, 'fromFile', 'path', options);
148142
};
149143

150144
const _fromBuffer = async (
@@ -155,10 +149,9 @@ const _fromBuffer = async (
155149
) => {
156150
const { postscriptName } = options || {};
157151

158-
const fontkitFont = fontkit.create(buffer, postscriptName);
152+
const fontkitFont = create(buffer, postscriptName);
159153

160-
handleCollectionErrors({
161-
font: fontkitFont,
154+
handleCollectionErrors(fontkitFont, {
162155
postscriptName,
163156
apiName,
164157
apiParamName,

packages/unpack/tsdown.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { defineConfig } from 'tsdown';
22
import { baseConfig } from '../../tsdown.base.config.ts';
33

4-
export default defineConfig(baseConfig);
4+
export default defineConfig({
5+
...baseConfig,
6+
format: ['esm'],
7+
});

0 commit comments

Comments
 (0)