-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<template> | ||
<div> | ||
Nuxt module playground! | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
div { | ||
font-family: 'Abel', sans-serif; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { $fetch } from 'ofetch' | ||
import type { FontProvider, ResolveFontFacesOptions } from '../types' | ||
import { extractFontFaceData } from '../css/parse' | ||
|
||
export default { | ||
async setup () { | ||
await initialiseFontMeta() | ||
}, | ||
async resolveFontFaces (fontFamily, defaults) { | ||
if (!isBunnyFont(fontFamily)) { return } | ||
|
||
return { | ||
fonts: await getFontDetails(fontFamily, defaults) | ||
} | ||
}, | ||
} satisfies FontProvider | ||
|
||
const fontAPI = $fetch.create({ | ||
baseURL: 'https://fonts.bunny.net' | ||
}) | ||
|
||
interface BunnyFontMeta { | ||
[key: string]: { | ||
category: string | ||
defSubset: string | ||
familyName: string | ||
isVariable: boolean | ||
styles: string[] | ||
variants: Record<string, number> | ||
weights: number[] | ||
} | ||
} | ||
|
||
let fonts: BunnyFontMeta | ||
const familyMap = new Map<string, string>() | ||
|
||
// TODO: Fetch and cache | ||
async function initialiseFontMeta () { | ||
fonts = await fontAPI<BunnyFontMeta>('/list', { responseType: 'json' }) | ||
for (const id in fonts) { | ||
familyMap.set(fonts[id]!.familyName!, id) | ||
} | ||
} | ||
|
||
function isBunnyFont (family: string) { | ||
return familyMap.has(family) | ||
} | ||
|
||
async function getFontDetails (family: string, variants: ResolveFontFacesOptions) { | ||
const id = familyMap.get(family) as keyof typeof fonts | ||
const font = fonts[id]! | ||
const weights = variants.weights?.filter(weight => font.weights.includes(Number(weight))) || font.weights | ||
const styleMap = { | ||
italic: 'i', | ||
oblique: 'i', | ||
normal: '' | ||
} | ||
const styles = new Set(variants.styles.map(i => styleMap[i])) | ||
const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${w}${s}`)) | ||
|
||
const css = await fontAPI('/css', { | ||
query: { | ||
family: id + ':' + resolvedVariants.join(',') | ||
} | ||
}) | ||
|
||
return extractFontFaceData(css) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters