Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
presets: [
'@babel/preset-typescript',
['@babel/preset-env', { bugfixes: true, targets: { node: 'current' } }],
[
'@babel/preset-env',
{ bugfixes: true, targets: { node: 'current' }, modules: false },
],
],
};
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
testPathIgnorePatterns: ['node_modules', '<rootDir>/site/.cache'],
extensionsToTreatAsEsm: ['.ts'],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"%packages": "pnpm --filter='@capsizecss/*' --aggregate-output",
"%site": "pnpm --filter=./site",
"start": "pnpm site:start",
"test": "jest",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest",
"format": "prettier --write .",
"lint": "manypkg check && prettier --check . && tsc",
"dev": "pnpm unpack:generate && pnpm %packages dev && pnpm metrics:generate",
Expand Down
3 changes: 1 addition & 2 deletions packages/unpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@
"generate": "tsx scripts/generate-weightings"
},
"dependencies": {
"fontkit": "^2.0.2"
"fontkitten": "^0.0.10"
},
"devDependencies": {
"@types/fontkit": "^2.0.1",
"@types/node": "^22.18.8",
"fast-xml-parser": "^4.3.2",
"sort-keys": "^5.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/unpack/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { readFile } from 'node:fs/promises';
import { fromUrl, fromBlob, fromBuffer } from '../index';
import { join } from 'node:path';

const __dirname = join(new URL(import.meta.url).pathname, '..');

const expectedMetrics = {
ascent: 1069,
capHeight: 714,
Expand Down
53 changes: 23 additions & 30 deletions packages/unpack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as fontkit from 'fontkit';
import type { Font as FontKitFont } from 'fontkit';
import {
create,
type Font as FontKitFont,
type FontCollection,
} from 'fontkitten';
import { readFile } from 'node:fs/promises';

import weightings from './weightings';

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

export type Font = ReturnType<typeof unpackMetricsFromFont>;

const handleCollectionErrors = ({
font,
postscriptName,
apiName,
apiParamName,
}: {
font: FontKitFont | null;
postscriptName?: string;
apiName: string;
apiParamName: string;
}) => {
function handleCollectionErrors(
font: FontKitFont | FontCollection | null,
{
postscriptName,
apiName,
apiParamName,
}: { postscriptName?: string; apiName: string; apiParamName: string },
): asserts font is FontKitFont {
if (postscriptName && font === null) {
throw new Error(
[
Expand Down Expand Up @@ -126,25 +127,18 @@ const handleCollectionErrors = ({
].join('\n'),
);
}
};
}

interface Options {
postscriptName?: string;
}

export const fromFile = (path: string, options?: Options): Promise<Font> => {
const { postscriptName } = options || {};

return fontkit.open(path, postscriptName).then((font) => {
handleCollectionErrors({
font,
postscriptName,
apiName: 'fromFile',
apiParamName: 'path',
});

return unpackMetricsFromFont(font);
});
export const fromFile = async (
path: string,
options?: Options,
): Promise<Font> => {
const buffer = await readFile(path);
return _fromBuffer(buffer, 'fromFile', 'path', options);
};

const _fromBuffer = async (
Expand All @@ -155,10 +149,9 @@ const _fromBuffer = async (
) => {
const { postscriptName } = options || {};

const fontkitFont = fontkit.create(buffer, postscriptName);
const fontkitFont = create(buffer, postscriptName);

handleCollectionErrors({
font: fontkitFont,
handleCollectionErrors(fontkitFont, {
postscriptName,
apiName,
apiParamName,
Expand Down
82 changes: 7 additions & 75 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.