From 7a4fcd41569604554e2ac6f153124de3902e602d Mon Sep 17 00:00:00 2001 From: Diego Muracciole Date: Mon, 24 Feb 2025 01:44:07 +0100 Subject: [PATCH] refactor: font strict type checking --- .changeset/quick-guests-love.md | 5 +++++ packages/font/package.json | 3 ++- packages/font/src/font.ts | 35 +++++++++++++++++++++------------ packages/font/src/index.ts | 5 +++-- packages/font/src/types.ts | 7 +++++-- packages/font/tsconfig.json | 2 +- yarn.lock | 5 +++++ 7 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 .changeset/quick-guests-love.md diff --git a/.changeset/quick-guests-love.md b/.changeset/quick-guests-love.md new file mode 100644 index 000000000..928855b01 --- /dev/null +++ b/.changeset/quick-guests-love.md @@ -0,0 +1,5 @@ +--- +'@react-pdf/font': patch +--- + +refactor: font strict type checking diff --git a/packages/font/package.json b/packages/font/package.json index fa69727c6..62c040830 100644 --- a/packages/font/package.json +++ b/packages/font/package.json @@ -30,6 +30,7 @@ "lib" ], "devDependencies": { - "@types/fontkit": "^2.0.7" + "@types/fontkit": "^2.0.7", + "@types/is-url": "^1.2.32" } } diff --git a/packages/font/src/font.ts b/packages/font/src/font.ts index 4e7487e31..7f5f3789f 100644 --- a/packages/font/src/font.ts +++ b/packages/font/src/font.ts @@ -7,6 +7,8 @@ import { FontSourceOptions, FontStyle, FontWeight, + RemoteOptions, + SingleLoad, } from './types'; const FONT_WEIGHTS = { @@ -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(); @@ -60,9 +62,9 @@ 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; @@ -70,7 +72,7 @@ class FontSource { this.fontWeight = fontWeight || 400; this.data = null; - this.options = options; + this.options = options || {}; this.loadResultPromise = null; } @@ -115,8 +117,15 @@ class Font { this.sources = []; } - register({ src, fontWeight, fontStyle, ...options }) { - const numericFontWeight = resolveFontWeight(fontWeight); + register({ + src, + fontWeight, + fontStyle, + ...options + }: Omit) { + const numericFontWeight = fontWeight + ? resolveFontWeight(fontWeight) + : undefined; this.sources.push( new FontSource(src, this.family, fontStyle, numericFontWeight, options), @@ -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); @@ -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 @@ -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; } } diff --git a/packages/font/src/index.ts b/packages/font/src/index.ts index f534f5f85..8179895df 100644 --- a/packages/font/src/index.ts +++ b/packages/font/src/index.ts @@ -62,7 +62,7 @@ class FontStore { const fontFamilies = typeof fontFamily === 'string' ? [fontFamily] : [...(fontFamily || [])]; - const promises = []; + const promises: Promise[] = []; for (let len = fontFamilies.length, i = 0; i < len; i += 1) { const family = fontFamilies[i]; @@ -70,7 +70,8 @@ class FontStore { if (isStandard) return; const f = this.getFont({ ...descriptor, fontFamily: family }); - promises.push(f.load()); + + if (f) promises.push(f.load()); } await Promise.all(promises); diff --git a/packages/font/src/types.ts b/packages/font/src/types.ts index 4d9b53944..5928a3d1d 100644 --- a/packages/font/src/types.ts +++ b/packages/font/src/types.ts @@ -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; body?: any; }; +export type FontSourceOptions = { + postscriptName?: string; +} & RemoteOptions; + export type FontSource = { src: string; fontStyle?: FontStyle; diff --git a/packages/font/tsconfig.json b/packages/font/tsconfig.json index 490d6d29a..816b77072 100644 --- a/packages/font/tsconfig.json +++ b/packages/font/tsconfig.json @@ -10,7 +10,7 @@ "moduleResolution": "Node", "esModuleInterop": true, "allowSyntheticDefaultImports": true, - "strict": false, + "strict": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "types": ["vitest/globals"], diff --git a/yarn.lock b/yarn.lock index f71fef7d5..509f6da63 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"