Skip to content

Commit

Permalink
feat(fontsource): support subsets
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerzl committed Mar 20, 2024
1 parent 34d1b15 commit 5fcda78
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/providers/fontsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export default {
},
} satisfies FontProvider

/** internal */

const fontAPI = $fetch.create({
baseURL: 'https://api.fontsource.org/v1'
})
Expand Down Expand Up @@ -103,23 +101,23 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
const font = fonts[id]!
const weights = variants.weights.filter(weight => font.weights.includes(Number(weight)))
const styles = variants.styles.filter(style => font.styles.includes(style))
const subsets = variants.subsets ? variants.subsets.filter(subset => font.subsets.includes(subset)) : [font.defSubset]
if (weights.length === 0 || styles.length === 0) return []

const fontDetail = await fontAPI<FontsourceFontDetail>(`/fonts/${font.id}`, { responseType: 'json' })
const fontFaceData: NormalizedFontFaceData[] = []

// TODO: support subsets apart from default
const defaultSubset = fontDetail.defSubset

for (const weight of weights) {
for (const style of styles) {
const variantUrl = fontDetail.variants[weight]![style]![defaultSubset]!.url
fontFaceData.push({
style,
weight,
src: Object.entries(variantUrl).map(([format, url]) => ({ url, format })),
unicodeRange: fontDetail.unicodeRange[defaultSubset]?.split(',')
})
for (const subset of subsets) {
for (const weight of weights) {
for (const style of styles) {
const variantUrl = fontDetail.variants[weight]![style]![subset]!.url
fontFaceData.push({
style,
weight,
src: Object.entries(variantUrl).map(([format, url]) => ({url, format})),
unicodeRange: fontDetail.unicodeRange[subset]?.split(',')
})
}
}
}

Expand Down

0 comments on commit 5fcda78

Please sign in to comment.