Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle font families with multiple spaces #128

Merged
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
2 changes: 1 addition & 1 deletion src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function getMetricsForFamily(family: string) {
if (family in metricCache) return metricCache[family]

try {
const name = camelCase(family).replace(/ /, '')
const name = camelCase(family).replace(/ /g, '')
const metrics: Font = await import(`@capsizecss/metrics/${name}.js`).then(
r => r.default /* c8 ignore next */ || r
)
Expand Down
34 changes: 34 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,40 @@ describe('getMetricsForFamily', () => {
`)
})

it('handles font families with more than one space in the name', async () => {
const metrics = await getMetricsForFamily('IBM Plex Mono')
expect(metrics).toMatchInlineSnapshot(`
{
"ascent": 1025,
"capHeight": 698,
"descent": -275,
"familyName": "IBM Plex Mono",
"lineGap": 0,
"unitsPerEm": 1000,
"xHeight": 516,
}
`)
// Test cache
expect(await getMetricsForFamily('IBM Plex Mono')).toEqual(metrics)

expect(
// eslint-disable-next-line
generateFontFace(metrics!, {
name: 'IBM Plex Mono fallback',
fallbacks: ['Arial'],
})
).toMatchInlineSnapshot(`
"@font-face {
font-family: \\"IBM Plex Mono fallback\\";
src: local(\\"Arial\\");
ascent-override: 102.5%;
descent-override: 27.5%;
line-gap-override: 0%;
}
"
`)
})

it('handles non-existent metrics', async () => {
const metrics = await getMetricsForFamily('Bingo Bob the Font')
expect(metrics).toBeNull()
Expand Down