font-loader.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. type MonoFont = {
  2. id: string
  3. family: string
  4. regular: string
  5. bold: string
  6. }
  7. let files: Record<string, () => Promise<string>> | undefined
  8. function getFiles() {
  9. if (files) return files
  10. files = import.meta.glob("./assets/fonts/*.woff2", { import: "default" }) as Record<string, () => Promise<string>>
  11. return files
  12. }
  13. export const MONO_NERD_FONTS = [
  14. {
  15. id: "jetbrains-mono",
  16. family: "JetBrains Mono Nerd Font",
  17. regular: "./assets/fonts/jetbrains-mono-nerd-font.woff2",
  18. bold: "./assets/fonts/jetbrains-mono-nerd-font-bold.woff2",
  19. },
  20. {
  21. id: "fira-code",
  22. family: "Fira Code Nerd Font",
  23. regular: "./assets/fonts/fira-code-nerd-font.woff2",
  24. bold: "./assets/fonts/fira-code-nerd-font-bold.woff2",
  25. },
  26. {
  27. id: "cascadia-code",
  28. family: "Cascadia Code Nerd Font",
  29. regular: "./assets/fonts/cascadia-code-nerd-font.woff2",
  30. bold: "./assets/fonts/cascadia-code-nerd-font-bold.woff2",
  31. },
  32. {
  33. id: "hack",
  34. family: "Hack Nerd Font",
  35. regular: "./assets/fonts/hack-nerd-font.woff2",
  36. bold: "./assets/fonts/hack-nerd-font-bold.woff2",
  37. },
  38. {
  39. id: "source-code-pro",
  40. family: "Source Code Pro Nerd Font",
  41. regular: "./assets/fonts/source-code-pro-nerd-font.woff2",
  42. bold: "./assets/fonts/source-code-pro-nerd-font-bold.woff2",
  43. },
  44. {
  45. id: "inconsolata",
  46. family: "Inconsolata Nerd Font",
  47. regular: "./assets/fonts/inconsolata-nerd-font.woff2",
  48. bold: "./assets/fonts/inconsolata-nerd-font-bold.woff2",
  49. },
  50. {
  51. id: "roboto-mono",
  52. family: "Roboto Mono Nerd Font",
  53. regular: "./assets/fonts/roboto-mono-nerd-font.woff2",
  54. bold: "./assets/fonts/roboto-mono-nerd-font-bold.woff2",
  55. },
  56. {
  57. id: "ubuntu-mono",
  58. family: "Ubuntu Mono Nerd Font",
  59. regular: "./assets/fonts/ubuntu-mono-nerd-font.woff2",
  60. bold: "./assets/fonts/ubuntu-mono-nerd-font-bold.woff2",
  61. },
  62. {
  63. id: "intel-one-mono",
  64. family: "Intel One Mono Nerd Font",
  65. regular: "./assets/fonts/intel-one-mono-nerd-font.woff2",
  66. bold: "./assets/fonts/intel-one-mono-nerd-font-bold.woff2",
  67. },
  68. {
  69. id: "meslo-lgs",
  70. family: "Meslo LGS Nerd Font",
  71. regular: "./assets/fonts/meslo-lgs-nerd-font.woff2",
  72. bold: "./assets/fonts/meslo-lgs-nerd-font-bold.woff2",
  73. },
  74. {
  75. id: "iosevka",
  76. family: "Iosevka Nerd Font",
  77. regular: "./assets/fonts/iosevka-nerd-font.woff2",
  78. bold: "./assets/fonts/iosevka-nerd-font-bold.woff2",
  79. },
  80. {
  81. id: "geist-mono",
  82. family: "GeistMono Nerd Font",
  83. regular: "./assets/fonts/GeistMonoNerdFontMono-Regular.woff2",
  84. bold: "./assets/fonts/GeistMonoNerdFontMono-Bold.woff2",
  85. },
  86. ] satisfies MonoFont[]
  87. const mono = Object.fromEntries(MONO_NERD_FONTS.map((font) => [font.id, font])) as Record<string, MonoFont>
  88. const loads = new Map<string, Promise<void>>()
  89. function css(font: { family: string; regular: string; bold: string }) {
  90. return `
  91. @font-face {
  92. font-family: "${font.family}";
  93. src: url("${font.regular}") format("woff2");
  94. font-display: swap;
  95. font-style: normal;
  96. font-weight: 400;
  97. }
  98. @font-face {
  99. font-family: "${font.family}";
  100. src: url("${font.bold}") format("woff2");
  101. font-display: swap;
  102. font-style: normal;
  103. font-weight: 700;
  104. }
  105. `
  106. }
  107. export function ensureMonoFont(id: string | undefined) {
  108. if (!id || id === "ibm-plex-mono") return Promise.resolve()
  109. if (typeof document !== "object") return Promise.resolve()
  110. const font = mono[id]
  111. if (!font) return Promise.resolve()
  112. const styleId = `oc-font-${font.id}`
  113. if (document.getElementById(styleId)) return Promise.resolve()
  114. const hit = loads.get(font.id)
  115. if (hit) return hit
  116. const files = getFiles()
  117. const load = Promise.all([files[font.regular]?.(), files[font.bold]?.()]).then(([regular, bold]) => {
  118. if (!regular || !bold) return
  119. if (document.getElementById(styleId)) return
  120. const style = document.createElement("style")
  121. style.id = styleId
  122. style.textContent = css({ family: font.family, regular, bold })
  123. document.head.appendChild(style)
  124. })
  125. loads.set(font.id, load)
  126. return load
  127. }