Skip to content

Commit

Permalink
feat: warn when font weights are not available
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerzl committed Mar 6, 2024
1 parent c06dd6d commit d9b2780
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/providers/bunny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ 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)))
if (weights.length == 0)
throw "No available font weights."
else if (variants.weights.length !== weights.length)
logger.warn(`Font weights \`${variants.weights}\` are not entirely available for \`${family}\` from \`bunny\`.`)
const styleMap = {
italic: 'i',
oblique: 'i',
Expand Down
1 change: 1 addition & 0 deletions src/providers/fontshare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
numbers.push(style.weight.number)
}

if (numbers.length == 0) throw "No available font weights."
const css = await fontAPI(`/css?f[]=${font.slug + '@' + numbers.join(',')}`)

// TODO: support subsets and axes
Expand Down
9 changes: 9 additions & 0 deletions src/providers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ async function getFontDetails (family: string, variants: ResolveFontFacesOptions
const weights = variableWeight
? [`${variableWeight.min}..${variableWeight.max}`]
: variants.weights.filter(weight => String(weight) in font.fonts)

if (weights.length == 0)
throw "No available font weights."
else if (
(variableWeight && !(variants.weights.every(weight => Number(weight) >= variableWeight.min && Number(weight) <= variableWeight.max))) ||
(!variableWeight && variants.weights.length !== weights.length)
)
logger.warn(`Font weights \`${variants.weights}\` are not entirely available for \`${family}\` from \`google\`.`)

const resolvedVariants = weights.flatMap(w => [...styles].map(s => `${s},${w}`)).sort()

let css = ''
Expand Down

0 comments on commit d9b2780

Please sign in to comment.