Skip to content

Commit a2de268

Browse files
authored
refactor: font strict type checking (#3093)
1 parent 481b536 commit a2de268

File tree

7 files changed

+43
-19
lines changed

7 files changed

+43
-19
lines changed

.changeset/quick-guests-love.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-pdf/font': patch
3+
---
4+
5+
refactor: font strict type checking

packages/font/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"lib"
3131
],
3232
"devDependencies": {
33-
"@types/fontkit": "^2.0.7"
33+
"@types/fontkit": "^2.0.7",
34+
"@types/is-url": "^1.2.32"
3435
}
3536
}

packages/font/src/font.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
FontSourceOptions,
88
FontStyle,
99
FontWeight,
10+
RemoteOptions,
11+
SingleLoad,
1012
} from './types';
1113

1214
const FONT_WEIGHTS = {
@@ -26,7 +28,7 @@ const FONT_WEIGHTS = {
2628
black: 900,
2729
};
2830

29-
const fetchFont = async (src: string, options) => {
31+
const fetchFont = async (src: string, options: RemoteOptions) => {
3032
const response = await fetch(src, options);
3133
const data = await response.arrayBuffer();
3234

@@ -60,17 +62,17 @@ class FontSource {
6062
constructor(
6163
src: string,
6264
fontFamily: string,
63-
fontStyle: FontStyle,
64-
fontWeight: number,
65-
options: FontSourceOptions,
65+
fontStyle?: FontStyle,
66+
fontWeight?: number,
67+
options?: FontSourceOptions,
6668
) {
6769
this.src = src;
6870
this.fontFamily = fontFamily;
6971
this.fontStyle = fontStyle || 'normal';
7072
this.fontWeight = fontWeight || 400;
7173

7274
this.data = null;
73-
this.options = options;
75+
this.options = options || {};
7476
this.loadResultPromise = null;
7577
}
7678

@@ -115,8 +117,15 @@ class Font {
115117
this.sources = [];
116118
}
117119

118-
register({ src, fontWeight, fontStyle, ...options }) {
119-
const numericFontWeight = resolveFontWeight(fontWeight);
120+
register({
121+
src,
122+
fontWeight,
123+
fontStyle,
124+
...options
125+
}: Omit<SingleLoad, 'family'>) {
126+
const numericFontWeight = fontWeight
127+
? resolveFontWeight(fontWeight)
128+
: undefined;
120129

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

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

136-
let res: FontSource;
145+
let font: FontSource | null = null;
137146

138147
const numericFontWeight = resolveFontWeight(fontWeight);
139148

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

151-
res = fit[0] || leftOffset[leftOffset.length - 1] || rightOffset[0];
160+
font = fit[0] || leftOffset[leftOffset.length - 1] || rightOffset[0];
152161
}
153162

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

161170
if (numericFontWeight < 400) {
162-
res = lt[lt.length - 1] || gt[0];
171+
font = lt[lt.length - 1] || gt[0];
163172
}
164173

165174
if (numericFontWeight > 500) {
166-
res = gt[0] || lt[lt.length - 1];
175+
font = gt[0] || lt[lt.length - 1];
167176
}
168177

169-
if (!res) {
178+
if (!font) {
170179
throw new Error(
171180
`Could not resolve font for ${this.family}, fontWeight ${fontWeight}, fontStyle ${fontStyle}`,
172181
);
173182
}
174183

175-
return res;
184+
return font;
176185
}
177186
}
178187

packages/font/src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@ class FontStore {
6262
const fontFamilies =
6363
typeof fontFamily === 'string' ? [fontFamily] : [...(fontFamily || [])];
6464

65-
const promises = [];
65+
const promises: Promise<void>[] = [];
6666

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

7272
const f = this.getFont({ ...descriptor, fontFamily: family });
73-
promises.push(f.load());
73+
74+
if (f) promises.push(f.load());
7475
}
7576

7677
await Promise.all(promises);

packages/font/src/types.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ export type FontDescriptor = {
1818
fontWeight?: FontWeight;
1919
};
2020

21-
export type FontSourceOptions = {
22-
postscriptName?: string;
21+
export type RemoteOptions = {
2322
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
2423
headers?: Record<string, string>;
2524
body?: any;
2625
};
2726

27+
export type FontSourceOptions = {
28+
postscriptName?: string;
29+
} & RemoteOptions;
30+
2831
export type FontSource = {
2932
src: string;
3033
fontStyle?: FontStyle;

packages/font/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"moduleResolution": "Node",
1111
"esModuleInterop": true,
1212
"allowSyntheticDefaultImports": true,
13-
"strict": false,
13+
"strict": true,
1414
"skipLibCheck": true,
1515
"forceConsistentCasingInFileNames": true,
1616
"types": ["vitest/globals"],

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,11 @@
26302630
dependencies:
26312631
ci-info "^3.1.0"
26322632

2633+
"@types/is-url@^1.2.32":
2634+
version "1.2.32"
2635+
resolved "https://registry.yarnpkg.com/@types/is-url/-/is-url-1.2.32.tgz#2883814affd004d3a6182d4a09c135d90e4fe28c"
2636+
integrity sha512-46VLdbWI8Sc+hPexQ6NLNR2YpoDyDZIpASHkJQ2Yr+Kf9Giw6LdCTkwOdsnHKPQeh7xTjTmSnxbE8qpxYuCiHA==
2637+
26332638
"@types/json-schema@*", "@types/json-schema@^7.0.8":
26342639
version "7.0.9"
26352640
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d"

0 commit comments

Comments
 (0)