Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Commit

Permalink
feat: support glyphs option in Google Font (unjs#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
sushichan044 committed Dec 18, 2024
1 parent b79894f commit 9844864
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default defineFontProvider<ProviderOption>('google', async (_options = {}
async function getFontDetails(family: string, options: ResolveFontOptions) {
const font = googleFonts.find(font => font.family === family)!
const styles = [...new Set(options.styles.map(i => styleMap[i]))].sort()
const glyphs = options.glyphs?.join('')

const variableWeight = font.axes.find(a => a.tag === 'wght')
const weights = variableWeight
Expand Down Expand Up @@ -73,6 +74,7 @@ export default defineFontProvider<ProviderOption>('google', async (_options = {}
headers: { 'user-agent': userAgents[extension as keyof typeof userAgents] },
query: {
family: `${family}:${resolvedAxes.join(',')}@${resolvedVariants.join(';')}`,
...(glyphs && { text: glyphs }),
},
})
}
Expand Down
31 changes: 31 additions & 0 deletions test/providers/google.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,35 @@ describe('google', () => {
expect(resolvedStyles).toMatchObject(styles)
expect(resolvedWeights).toMatchObject(weights)
})

it('respects glyphs option and resolves optimized font', async () => {
const unifont = await createUnifont([providers.google()])

const { fonts } = await unifont.resolveFont('Poppins', {
glyphs: ['Hello', 'World'],
styles: ['normal'],
weights: ['400'],
subsets: [],
})

// Do not use sanitizeFontSource here, as we must test the url parameters
expect(fonts).toMatchInlineSnapshot(`
[
{
"src": [
{
"format": "woff2",
"url": "https://fonts.gstatic.com/l/font?kit=pxiEyp8kv8JHgFVrFJXUdVNFIvDDHy0hxgHa&skey=87759fb096548f6d&v=v22",
},
{
"format": "woff",
"url": "https://fonts.gstatic.com/l/font?kit=pxiEyp8kv8JHgFVrFJPMcBMSdJLnJzs&skey=87759fb096548f6d&v=v22",
},
],
"style": "normal",
"weight": 400,
},
]
`)
})
})

0 comments on commit 9844864

Please sign in to comment.