Skip to content

Commit 4a9bc4d

Browse files
committed
fix(google): handle variable font weights
1 parent d2c9ac1 commit 4a9bc4d

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/providers/google.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ interface FontIndexMeta {
3333
width: number | null
3434
lineHeight: number | null
3535
}>
36+
axes: Array<{
37+
tag: string
38+
min: number
39+
max: number
40+
defaultValue: number
41+
}>
3642
}
3743

3844
/** internal */
@@ -57,16 +63,19 @@ function isGoogleFont (family: string) {
5763
return fonts.some(font => font.family === family)
5864
}
5965

66+
const styleMap = {
67+
italic: '1',
68+
oblique: '1',
69+
normal: '0'
70+
}
6071
async function getFontDetails (family: string, variants: ResolveFontFacesOptions) {
6172
const font = fonts.find(font => font.family === family)!
62-
const weights = variants.weights.filter(weight => String(weight) in font.fonts)
63-
const styleMap = {
64-
italic: '1',
65-
oblique: '1',
66-
normal: '0'
67-
}
73+
const styles = [...new Set(variants.styles.map(i => styleMap[i]))].sort()
6874

69-
const styles = new Set(variants.styles.map(i => styleMap[i]))
75+
const variableWeight = font.axes.find(a => a.tag === 'wght')
76+
const weights = variableWeight
77+
? [`${variableWeight.min}..${variableWeight.max}`]
78+
: variants.weights.filter(weight => String(weight) in font.fonts)
7079
const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${s},${w}`))
7180

7281
let css = ''

0 commit comments

Comments
 (0)