-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: merge provider work from
nuxt/fonts
- Loading branch information
Showing
7 changed files
with
868 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { hash } from 'ohash' | ||
|
||
import type { FontProvider, ResolveFontFacesOptions } from '../types' | ||
import { extractFontFaceData } from '../css/parse' | ||
import { cachedData } from '../cache' | ||
import { $fetch } from '../fetch' | ||
import { logger } from '../logger' | ||
|
||
interface ProviderOption { | ||
id?: string[] | string | ||
} | ||
|
||
export default { | ||
async setup(options: ProviderOption) { | ||
if (!options.id) { | ||
return | ||
} | ||
await initialiseFontMeta(typeof options.id === 'string' ? [options.id] : options.id) | ||
}, | ||
async resolveFontFaces(fontFamily, defaults) { | ||
Check failure on line 20 in src/providers/adobe.ts GitHub Actions / test
|
||
if (!isAdobeFont(fontFamily)) { | ||
return | ||
} | ||
|
||
return { | ||
fonts: await cachedData(`adobe:${fontFamily}-${hash(defaults)}-data.json`, () => getFontDetails(fontFamily, defaults), { | ||
onError(err) { | ||
logger.error(`Could not fetch metadata for \`${fontFamily}\` from \`adobe\`.`, err) | ||
return [] | ||
}, | ||
}), | ||
} | ||
}, | ||
} satisfies FontProvider | ||
|
||
const fontAPI = $fetch.create({ | ||
baseURL: 'https://typekit.com', | ||
}) | ||
|
||
const fontCSSAPI = $fetch.create({ | ||
baseURL: 'https://use.typekit.net', | ||
}) | ||
|
||
interface AdobeFontMeta { | ||
kits: AdobeFontKit[] | ||
} | ||
|
||
interface AdobeFontAPI { | ||
kit: AdobeFontKit | ||
} | ||
|
||
interface AdobeFontKit { | ||
id: string | ||
families: AdobeFontFamily[] | ||
} | ||
|
||
interface AdobeFontFamily { | ||
id: string | ||
name: string | ||
slug: string | ||
css_names: string[] | ||
css_stack: string | ||
variations: string[] | ||
} | ||
|
||
let fonts: AdobeFontMeta | ||
const familyMap = new Map<string, string>() | ||
|
||
async function getAdobeFontMeta(id: string): Promise<AdobeFontKit> { | ||
const { kit } = await fontAPI<AdobeFontAPI>(`/api/v1/json/kits/${id}/published`, { responseType: 'json' }) | ||
return kit | ||
} | ||
|
||
async function initialiseFontMeta(kits: string[]) { | ||
fonts = { | ||
kits: await Promise.all(kits.map(id => cachedData(`adobe:meta-${id}.json`, () => getAdobeFontMeta(id), { | ||
onError() { | ||
logger.error('Could not download `adobe` font metadata. `@nuxt/fonts` will not be able to inject `@font-face` rules for adobe.') | ||
return null | ||
}, | ||
}))).then(r => r.filter((meta): meta is AdobeFontKit => !!meta)), | ||
} | ||
for (const kit in fonts.kits) { | ||
const families = fonts.kits[kit]!.families | ||
for (const family in families) { | ||
familyMap.set(families[family]!.name, families[family]!.id) | ||
} | ||
} | ||
} | ||
|
||
function isAdobeFont(family: string) { | ||
return familyMap.has(family) | ||
} | ||
|
||
async function getFontDetails(family: string, variants: ResolveFontFacesOptions) { | ||
variants.weights = variants.weights.map(String) | ||
|
||
for (const kit in fonts.kits) { | ||
const font = fonts.kits[kit]!.families.find(f => f.name === family)! | ||
if (!font) { | ||
continue | ||
} | ||
|
||
const styles: string[] = [] | ||
for (const style of font.variations) { | ||
if (style.includes('i') && !variants.styles.includes('italic')) { | ||
continue | ||
} | ||
if (!variants.weights.includes(String(style.slice(-1) + '00'))) { | ||
continue | ||
} | ||
styles.push(style) | ||
} | ||
if (styles.length === 0) { | ||
continue | ||
} | ||
const css = await fontCSSAPI<string>(`${fonts.kits[kit]!.id}.css`) | ||
|
||
// TODO: Not sure whether this css_names array always has a single element. Still need to investigate. | ||
const cssName = font.css_names[0] ?? family.toLowerCase().split(' ').join('-') | ||
|
||
return extractFontFaceData(css, cssName) | ||
} | ||
|
||
return [] | ||
} |
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,88 @@ | ||
import { hash } from 'ohash' | ||
|
||
import type { FontProvider, ResolveFontFacesOptions } from '../types' | ||
import { extractFontFaceData } from '../css/parse' | ||
import { cachedData } from '../cache' | ||
import { $fetch } from '../fetch' | ||
import { logger } from '../logger' | ||
|
||
export default { | ||
async setup() { | ||
await initialiseFontMeta() | ||
}, | ||
async resolveFontFaces(fontFamily, defaults) { | ||
if (!isBunnyFont(fontFamily)) { | ||
return | ||
} | ||
|
||
return { | ||
fonts: await cachedData(`bunny:${fontFamily}-${hash(defaults)}-data.json`, () => getFontDetails(fontFamily, defaults), { | ||
onError(err) { | ||
logger.error(`Could not fetch metadata for \`${fontFamily}\` from \`bunny\`.`, err) | ||
return [] | ||
}, | ||
}), | ||
} | ||
}, | ||
} satisfies FontProvider | ||
|
||
/** internal */ | ||
|
||
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>() | ||
|
||
async function initialiseFontMeta() { | ||
fonts = await cachedData('bunny:meta.json', () => fontAPI<BunnyFontMeta>('/list', { responseType: 'json' }), { | ||
onError() { | ||
logger.error('Could not download `bunny` font metadata. `@nuxt/fonts` will not be able to inject `@font-face` rules for bunny.') | ||
return {} | ||
}, | ||
}) | ||
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))) | ||
const styleMap = { | ||
italic: 'i', | ||
oblique: 'i', | ||
normal: '', | ||
} | ||
const styles = new Set(variants.styles.map(i => styleMap[i])) | ||
if (weights.length === 0 || styles.size === 0) return [] | ||
|
||
const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${w}${s}`)) | ||
|
||
const css = await fontAPI<string>('/css', { | ||
query: { | ||
family: id + ':' + resolvedVariants.join(','), | ||
}, | ||
}) | ||
|
||
// TODO: support subsets | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { hash } from 'ohash' | ||
|
||
import type { FontProvider, ResolveFontFacesOptions } from '../types' | ||
import { extractFontFaceData } from '../css/parse' | ||
import { cachedData } from '../cache' | ||
import { $fetch } from '../fetch' | ||
import { logger } from '../logger' | ||
|
||
export default { | ||
async setup() { | ||
await initialiseFontMeta() | ||
}, | ||
async resolveFontFaces(fontFamily, defaults) { | ||
if (!isFontshareFont(fontFamily)) { | ||
return | ||
} | ||
|
||
return { | ||
fonts: await cachedData(`fontshare:${fontFamily}-${hash(defaults)}-data.json`, () => getFontDetails(fontFamily, defaults), { | ||
onError(err) { | ||
logger.error(`Could not fetch metadata for \`${fontFamily}\` from \`fontshare\`.`, err) | ||
return [] | ||
}, | ||
}), | ||
} | ||
}, | ||
} satisfies FontProvider | ||
|
||
/** internal */ | ||
|
||
const fontAPI = $fetch.create({ | ||
baseURL: 'https://api.fontshare.com/v2', | ||
}) | ||
|
||
interface FontshareFontMeta { | ||
slug: string | ||
name: string | ||
styles: Array<{ | ||
default: boolean | ||
file: string | ||
id: string | ||
is_italic: boolean | ||
is_variable: boolean | ||
properties: { | ||
ascending_leading: number | ||
body_height: null | ||
cap_height: number | ||
descending_leading: number | ||
max_char_width: number | ||
x_height: number | ||
y_max: number | ||
y_min: number | ||
} | ||
weight: { | ||
label: string | ||
name: string | ||
native_name: null | ||
number: number | ||
weight: number | ||
} | ||
}> | ||
} | ||
|
||
let fonts: FontshareFontMeta[] | ||
const families = new Set<string>() | ||
|
||
async function initialiseFontMeta() { | ||
fonts = await cachedData('fontshare:meta.json', async () => { | ||
const fonts: FontshareFontMeta[] = [] | ||
let offset = 0 | ||
let chunk | ||
do { | ||
chunk = await fontAPI<{ fonts: FontshareFontMeta[], has_more: boolean }>('/fonts', { | ||
responseType: 'json', | ||
query: { | ||
offset, | ||
limit: 100, | ||
}, | ||
}) | ||
fonts.push(...chunk.fonts) | ||
offset++ | ||
} while (chunk.has_more) | ||
return fonts | ||
}, { | ||
onError() { | ||
logger.error('Could not download `fontshare` font metadata. `@nuxt/fonts` will not be able to inject `@font-face` rules for fontshare.') | ||
return [] | ||
}, | ||
}) | ||
for (const font of fonts) { | ||
families.add(font.name) | ||
} | ||
} | ||
|
||
function isFontshareFont(family: string) { | ||
return families.has(family) | ||
} | ||
|
||
async function getFontDetails(family: string, variants: ResolveFontFacesOptions) { | ||
// https://api.fontshare.com/v2/css?f[]=alpino@300 | ||
const font = fonts.find(f => f.name === family)! | ||
const numbers: number[] = [] | ||
for (const style of font.styles) { | ||
if (style.is_italic && !variants.styles.includes('italic')) { | ||
continue | ||
} | ||
if (!variants.weights.includes(String(style.weight.number))) { | ||
continue | ||
} | ||
numbers.push(style.weight.number) | ||
} | ||
|
||
if (numbers.length === 0) return [] | ||
|
||
const css = await fontAPI<string>(`/css?f[]=${font.slug + '@' + numbers.join(',')}`) | ||
|
||
// TODO: support subsets and axes | ||
return extractFontFaceData(css) | ||
} |
Oops, something went wrong.