Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
18 changes: 7 additions & 11 deletions packages/unpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@
"name": "Michael Taranto",
"homepage": "https://github.com/michaeltaranto"
},
"type": "module",
"exports": {
".": {
"@capsizecss/src": "./src/index.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
"default": "./dist/index.mjs"
},
"./package.json": "./package.json"
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.cts",
"types": "./dist/index.d.mts",
"files": [
"dist"
],
Expand All @@ -42,10 +41,9 @@
"generate": "tsx scripts/generate-weightings"
},
"dependencies": {
"fontkit": "^2.0.2"
"fontkitten": "^1.0.0"
},
"devDependencies": {
"@types/fontkit": "^2.0.1",
"@types/node": "^22.18.8",
"fast-xml-parser": "^4.3.2",
"sort-keys": "^5.0.0",
Expand All @@ -57,11 +55,9 @@
},
"publishConfig": {
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
".": "./dist/index.mjs",
"./package.json": "./package.json"
}
}
},
"main": "./dist/index.mjs"
}
7 changes: 5 additions & 2 deletions packages/unpack/scripts/generate-weightings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import fs from 'fs/promises';
import path from 'path';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { XMLParser } from 'fast-xml-parser';
import sortKeys from 'sort-keys';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

type WikiNewsFeed = {
feed: {
doc: {
Expand Down
55 changes: 24 additions & 31 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 All @@ -108,7 +109,7 @@ const handleCollectionErrors = ({
);
}

if (font !== null && 'fonts' in font && Array.isArray(font.fonts)) {
if (font !== null && font.isCollection) {
const availableNames = font.fonts.map((f) => f.postscriptName);
throw new Error(
[
Expand All @@ -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
5 changes: 4 additions & 1 deletion packages/unpack/tsdown.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { defineConfig } from 'tsdown';
import { baseConfig } from '../../tsdown.base.config.ts';

export default defineConfig(baseConfig);
export default defineConfig({
...baseConfig,
format: ['esm'],
});
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.