Skip to content

Commit

Permalink
feat: support glyphs option in Google Icons (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
sushichan044 committed Dec 18, 2024
1 parent 9844864 commit 59a38e8
Show file tree
Hide file tree
Showing 2 changed files with 361 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/providers/googleicons.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { hash } from 'ohash'
import type { ResolveFontOptions } from '../types'

import { hash } from 'ohash'
import { extractFontFaceData } from '../css/parse'
import { $fetch } from '../fetch'
import { defineFontProvider } from '../utils'
Expand All @@ -21,7 +22,10 @@ export default defineFontProvider('googleicons', async (_options, ctx) => {
// svg: 'Mozilla/4.0 (iPad; CPU OS 4_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/4.1 Mobile/9A405 Safari/7534.48.3',
}

async function getFontDetails(family: string) {
async function getFontDetails(family: string, options: ResolveFontOptions) {
// Google Icons require sorted icon names, or we will see a 400 error
const iconNames = options.glyphs?.sort().join(',')

let css = ''

if (family.includes('Icons')) {
Expand Down Expand Up @@ -51,6 +55,7 @@ export default defineFontProvider('googleicons', async (_options, ctx) => {
headers: { 'user-agent': userAgents[extension as keyof typeof userAgents] },
query: {
family: `${family}:` + `opsz,wght,FILL,[email protected],100..700,0..1,-50..200`,
...(iconNames && { icon_names: iconNames }),
},
})
}
Expand All @@ -60,12 +65,12 @@ export default defineFontProvider('googleicons', async (_options, ctx) => {
}

return {
async resolveFont(fontFamily, defaults) {
async resolveFont(fontFamily, options) {
if (!googleIcons.includes(fontFamily)) {
return
}

const fonts = await ctx.storage.getItem(`googleicons:${fontFamily}-${hash(defaults)}-data.json`, () => getFontDetails(fontFamily))
const fonts = await ctx.storage.getItem(`googleicons:${fontFamily}-${hash(options)}-data.json`, () => getFontDetails(fontFamily, options))
return { fonts }
},
}
Expand Down
Loading

0 comments on commit 59a38e8

Please sign in to comment.