| import { formatAvailableValues } from '../format-available-values' |
| import { nextFontError } from '../next-font-error' |
| import { googleFontsMetadata } from './google-fonts-metadata' |
|
|
| |
| |
| |
| export function getFontAxes( |
| fontFamily: string, |
| weights: string[], |
| styles: string[], |
| selectedVariableAxes?: string[] |
| ): { |
| wght?: string[] |
| ital?: string[] |
| variableAxes?: [string, string][] |
| } { |
| const hasItalic = styles.includes('italic') |
| const hasNormal = styles.includes('normal') |
| |
| |
| const ital = hasItalic ? [...(hasNormal ? ['0'] : []), '1'] : undefined |
|
|
| |
| if (weights[0] === 'variable') { |
| |
| const allAxes = googleFontsMetadata[fontFamily].axes |
| if (!allAxes) { |
| throw new Error('invariant variable font without axes') |
| } |
|
|
| if (selectedVariableAxes) { |
| |
| const defineAbleAxes: string[] = allAxes |
| .map(({ tag }) => tag) |
| .filter((tag) => tag !== 'wght') |
|
|
| if (defineAbleAxes.length === 0) { |
| nextFontError(`Font \`${fontFamily}\` has no definable \`axes\``) |
| } |
| if (!Array.isArray(selectedVariableAxes)) { |
| nextFontError( |
| `Invalid axes value for font \`${fontFamily}\`, expected an array of axes.\nAvailable axes: ${formatAvailableValues( |
| defineAbleAxes |
| )}` |
| ) |
| } |
| selectedVariableAxes.forEach((key) => { |
| if (!defineAbleAxes.some((tag) => tag === key)) { |
| nextFontError( |
| `Invalid axes value \`${key}\` for font \`${fontFamily}\`.\nAvailable axes: ${formatAvailableValues( |
| defineAbleAxes |
| )}` |
| ) |
| } |
| }) |
| } |
|
|
| let weightAxis: string | undefined |
| let variableAxes: [string, string][] | undefined |
| for (const { tag, min, max } of allAxes) { |
| if (tag === 'wght') { |
| |
| weightAxis = `${min}..${max}` |
| } else if (selectedVariableAxes?.includes(tag)) { |
| if (!variableAxes) { |
| variableAxes = [] |
| } |
| variableAxes.push([tag, `${min}..${max}`]) |
| } |
| } |
|
|
| return { |
| wght: weightAxis ? [weightAxis] : undefined, |
| ital, |
| variableAxes, |
| } |
| } else { |
| return { |
| ital, |
| wght: weights, |
| } |
| } |
| } |
|
|