Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 5 additions & 0 deletions .changeset/quick-guests-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@react-pdf/font': patch
---

refactor: font strict type checking
3 changes: 2 additions & 1 deletion packages/font/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"lib"
],
"devDependencies": {
"@types/fontkit": "^2.0.7"
"@types/fontkit": "^2.0.7",
"@types/is-url": "^1.2.32"
}
}
35 changes: 22 additions & 13 deletions packages/font/src/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
FontSourceOptions,
FontStyle,
FontWeight,
RemoteOptions,
SingleLoad,
} from './types';

const FONT_WEIGHTS = {
Expand All @@ -26,7 +28,7 @@ const FONT_WEIGHTS = {
black: 900,
};

const fetchFont = async (src: string, options) => {
const fetchFont = async (src: string, options: RemoteOptions) => {
const response = await fetch(src, options);
const data = await response.arrayBuffer();

Expand Down Expand Up @@ -60,17 +62,17 @@ class FontSource {
constructor(
src: string,
fontFamily: string,
fontStyle: FontStyle,
fontWeight: number,
options: FontSourceOptions,
fontStyle?: FontStyle,
fontWeight?: number,
options?: FontSourceOptions,
) {
this.src = src;
this.fontFamily = fontFamily;
this.fontStyle = fontStyle || 'normal';
this.fontWeight = fontWeight || 400;

this.data = null;
this.options = options;
this.options = options || {};
this.loadResultPromise = null;
}

Expand Down Expand Up @@ -115,8 +117,15 @@ class Font {
this.sources = [];
}

register({ src, fontWeight, fontStyle, ...options }) {
const numericFontWeight = resolveFontWeight(fontWeight);
register({
src,
fontWeight,
fontStyle,
...options
}: Omit<SingleLoad, 'family'>) {
const numericFontWeight = fontWeight
? resolveFontWeight(fontWeight)
: undefined;

this.sources.push(
new FontSource(src, this.family, fontStyle, numericFontWeight, options),
Expand All @@ -133,7 +142,7 @@ class Font {

// Weight resolution. https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Fallback_weights

let res: FontSource;
let font: FontSource | null = null;

const numericFontWeight = resolveFontWeight(fontWeight);

Expand All @@ -148,7 +157,7 @@ class Font {
(s) => s.fontWeight >= numericFontWeight && s.fontWeight < 500,
);

res = fit[0] || leftOffset[leftOffset.length - 1] || rightOffset[0];
font = fit[0] || leftOffset[leftOffset.length - 1] || rightOffset[0];
}

const lt = styleSources
Expand All @@ -159,20 +168,20 @@ class Font {
.sort(sortByFontWeight);

if (numericFontWeight < 400) {
res = lt[lt.length - 1] || gt[0];
font = lt[lt.length - 1] || gt[0];
}

if (numericFontWeight > 500) {
res = gt[0] || lt[lt.length - 1];
font = gt[0] || lt[lt.length - 1];
}

if (!res) {
if (!font) {
throw new Error(
`Could not resolve font for ${this.family}, fontWeight ${fontWeight}, fontStyle ${fontStyle}`,
);
}

return res;
return font;
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/font/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ class FontStore {
const fontFamilies =
typeof fontFamily === 'string' ? [fontFamily] : [...(fontFamily || [])];

const promises = [];
const promises: Promise<void>[] = [];

for (let len = fontFamilies.length, i = 0; i < len; i += 1) {
const family = fontFamilies[i];
const isStandard = standard.includes(family);
if (isStandard) return;

const f = this.getFont({ ...descriptor, fontFamily: family });
promises.push(f.load());

if (f) promises.push(f.load());
}

await Promise.all(promises);
Expand Down
7 changes: 5 additions & 2 deletions packages/font/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ export type FontDescriptor = {
fontWeight?: FontWeight;
};

export type FontSourceOptions = {
postscriptName?: string;
export type RemoteOptions = {
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
headers?: Record<string, string>;
body?: any;
};

export type FontSourceOptions = {
postscriptName?: string;
} & RemoteOptions;

export type FontSource = {
src: string;
fontStyle?: FontStyle;
Expand Down
2 changes: 1 addition & 1 deletion packages/font/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"moduleResolution": "Node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"types": ["vitest/globals"],
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,11 @@
dependencies:
ci-info "^3.1.0"

"@types/is-url@^1.2.32":
version "1.2.32"
resolved "https://registry.yarnpkg.com/@types/is-url/-/is-url-1.2.32.tgz#2883814affd004d3a6182d4a09c135d90e4fe28c"
integrity sha512-46VLdbWI8Sc+hPexQ6NLNR2YpoDyDZIpASHkJQ2Yr+Kf9Giw6LdCTkwOdsnHKPQeh7xTjTmSnxbE8qpxYuCiHA==

"@types/json-schema@*", "@types/json-schema@^7.0.8":
version "7.0.9"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d"
Expand Down