summaryrefslogtreecommitdiff
path: root/docs/lab/font-files.js
blob: a7a6764575daeb8f11da93ef99dd43e589126baa (plain)
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
86
87
88
89
90
91
92
93
var fontFamilyName
var fontFamilyNameDisplay
var fontFamilyNameVar

;(()=>{
  let isLocalServer = document.location.protocol == "http:"

  const includeLabLocalFiles = isLocalServer

  const fontVersion = (
    isLocalServer || typeof interBuildVersion == "undefined" ?
      Math.round(Date.now()).toString(36) :
      interBuildVersion.replace(/\./g, "_")
  );

  fontFamilyName = 'Inter-v' + fontVersion
  fontFamilyNameDisplay = 'InterDisplay-v' + fontVersion
  fontFamilyNameVar = 'Inter-var-v' + fontVersion

  let outbuf = []
  function w(s) { outbuf.push(s) }

  function genStaticFontFace(family, cssname, filepath, weight, isItalic) {
    let styleName = ""
    switch (weight) {
      case 100: styleName = "Thin"; break
      case 200: styleName = "ExtraLight"; break
      case 300: styleName = "Light"; break
      case 400: styleName = ""; break
      case 500: styleName = "Medium"; break
      case 600: styleName = "SemiBold"; break
      case 700: styleName = "Bold"; break
      case 800: styleName = "ExtraBold"; break
      case 900: styleName = "Black"; break
    }
    if (styleName == "") {
      styleName = isItalic ? "Italic" : "Regular"
    } else if (isItalic) {
      styleName += "Italic"
    }
    let filename = `${family}-${styleName}`
    w(`@font-face {`)
    w(`  font-family: ${cssname};`)
    w(`  font-style:  ${isItalic ? "italic" : "normal"};`)
    w(`  font-weight: ${weight};`)
    w(`  font-display: block;`)
    w(`  src:`)
    if (includeLabLocalFiles) {
      let filename2 = filename.replace("InterDisplay-", "Inter-Display")
      if (filename2 == "Inter-DisplayRegular")
        filename2 = "Inter-Display"
      w(`  url("fonts/${filepath}/${filename2}.woff2?${fontVersion}") format("woff2"),`)
      w(`  url("fonts/${filepath}/${filename2}.woff?${fontVersion}") format("woff"),`)
    }
    w(`  url("../font-files/${filename}.woff2?${fontVersion}") format("woff2"),`)
    w(`  url("../font-files/${filename}.woff?${fontVersion}") format("woff2");`)
    w(`}`)
  }
  for (let [family, filepath, cssname] of [
    ["Inter",        "static", fontFamilyName],
    ["InterDisplay", "static", fontFamilyNameDisplay],
  ]) {
    for (let weight of [100,200,300,400,500,600,700,800,900]) {
      for (let isItalic of [true,false]) {
        genStaticFontFace(family, cssname, filepath, weight, isItalic)
      }
    }
  }

  for (let [srcname,cssname] of [
    ["InterVariable", fontFamilyNameVar],
    ["InterVariable-Italic", fontFamilyNameVar],
  ]) {
    w(`@font-face {
      font-family: '${cssname}';
      font-style: ${srcname.indexOf("Italic") != -1 ? "italic" : "normal"};
      font-weight: 100 900;
      font-display: block;
      src:`)
    if (includeLabLocalFiles) {
      w(`  url('fonts/var/${srcname}.woff2?${fontVersion}') format("woff2"),`)
    }
    w(`  url('../font-files/${srcname}.woff2?${fontVersion}') format("woff2");`)
    w(`}`)
  }

  let css = outbuf.join("\n")
  // console.log(css)
  const fontCSS = document.createElement("style")
  fontCSS.setAttribute('type', 'text/css')
  fontCSS.appendChild(document.createTextNode(css))
  document.head.appendChild(fontCSS)
})()