File size: 2,725 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { formatAvailableValues } from '../format-available-values'
import { nextFontError } from '../next-font-error'
import { googleFontsMetadata } from './google-fonts-metadata'

/**
 * Validates and gets the data for each font axis required to generate the Google Fonts URL.
 */
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')
  // Make sure the order is correct, otherwise Google Fonts will return an error
  // If only normal is set, we can skip returning the ital axis as normal is the default
  const ital = hasItalic ? [...(hasNormal ? ['0'] : []), '1'] : undefined

  // Weights will always contain one element if it's a variable font
  if (weights[0] === 'variable') {
    // Get all the available axes for the current font from the metadata file
    const allAxes = googleFontsMetadata[fontFamily].axes
    if (!allAxes) {
      throw new Error('invariant variable font without axes')
    }

    if (selectedVariableAxes) {
      // The axes other than weight and style that can be defined for the current variable font
      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') {
        // In variable fonts the weight is a range
        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,
    }
  }
}