File size: 1,303 Bytes
0daf81d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Self-hosted fonts (no Google Fonts, works in mainland China).
const base = `${import.meta.env.BASE_URL}fonts/`
const FACES: [string, string, string][] = [
  ['LW SC', 'lw-sc-500.woff2', '500'],
  ['LW SC', 'lw-sc-700.woff2', '700'],
  ['LW Smiley', 'lw-smiley.woff2', '400'],
  ['LW Poppins', 'lw-poppins-800.woff2', '800'],
  ['LW Poppins', 'lw-poppins-600.woff2', '600'],
  ['LW IPA', 'lw-ipa-500.woff2', '500'],
]

export const FONT = {
  sc: (w: number, px: number) => `${w} ${px}px "LW SC", "PingFang SC", "Microsoft YaHei", sans-serif`,
  head: (px: number) => `400 ${px}px "LW Smiley", "LW SC", "PingFang SC", sans-serif`,
  word: (px: number) => `800 ${px}px "LW Poppins", "Helvetica Neue", Arial, sans-serif`,
  wordSemi: (px: number) => `600 ${px}px "LW Poppins", "Helvetica Neue", Arial, sans-serif`,
  ipa: (px: number) => `500 ${px}px "LW IPA", "Lucida Sans Unicode", "Arial Unicode MS", sans-serif`,
}

let loading: Promise<void> | null = null
export function loadFonts(): Promise<void> {
  if (loading) return loading
  loading = Promise.all(
    FACES.map(async ([family, file, weight]) => {
      const f = new FontFace(family, `url(${base}${file})`, { weight, display: 'block' })
      await f.load()
      document.fonts.add(f)
    }),
  ).then(() => undefined)
  return loading
}