Skip to content

Commit b14233e

Browse files
Fix bug where getMetricsForFamily returned null for fonts with multiple spaces in their name (unjs#127)
1 parent e7391ee commit b14233e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/metrics.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function getMetricsForFamily(family: string) {
1212
if (family in metricCache) return metricCache[family]
1313

1414
try {
15-
const name = camelCase(family).replace(/ /, '')
15+
const name = camelCase(family).replace(/ /g, '')
1616
const metrics: Font = await import(`@capsizecss/metrics/${name}.js`).then(
1717
r => r.default /* c8 ignore next */ || r
1818
)

test/index.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,40 @@ describe('getMetricsForFamily', () => {
7777
`)
7878
})
7979

80+
it('handles font families with more than one space in the name', async () => {
81+
const metrics = await getMetricsForFamily('IBM Plex Mono')
82+
expect(metrics).toMatchInlineSnapshot(`
83+
{
84+
"ascent": 1025,
85+
"capHeight": 698,
86+
"descent": -275,
87+
"familyName": "IBM Plex Mono",
88+
"lineGap": 0,
89+
"unitsPerEm": 1000,
90+
"xHeight": 516,
91+
}
92+
`)
93+
// Test cache
94+
expect(await getMetricsForFamily('IBM Plex Mono')).toEqual(metrics)
95+
96+
expect(
97+
// eslint-disable-next-line
98+
generateFontFace(metrics!, {
99+
name: 'IBM Plex Mono fallback',
100+
fallbacks: ['Arial'],
101+
})
102+
).toMatchInlineSnapshot(`
103+
"@font-face {
104+
font-family: \\"IBM Plex Mono fallback\\";
105+
src: local(\\"Arial\\");
106+
ascent-override: 102.5%;
107+
descent-override: 27.5%;
108+
line-gap-override: 0%;
109+
}
110+
"
111+
`)
112+
})
113+
80114
it('handles non-existent metrics', async () => {
81115
const metrics = await getMetricsForFamily('Bingo Bob the Font')
82116
expect(metrics).toBeNull()

0 commit comments

Comments
 (0)