|
| 1 | +import https from 'https' |
| 2 | +import http from 'http' |
| 3 | +import fs, { writeFileSync } from 'fs' |
| 4 | +import { exec } from 'child_process' |
| 5 | +import { dirname, resolve } from 'path' |
| 6 | + |
| 7 | +async function generate(fontFamily, variant) { |
| 8 | + const response = await fetch( |
| 9 | + `https://www.googleapis.com/webfonts/v1/webfonts?family=${fontFamily}&key=${process.env.GOOGLE_FONTS_API_KEY}`, |
| 10 | + ) |
| 11 | + const json = await response.json() |
| 12 | + if (json.error != null) { |
| 13 | + console.error('fetch font families', fontFamily, json.error) |
| 14 | + return |
| 15 | + } |
| 16 | + const result = Object.entries(json.items[0].files).find( |
| 17 | + ([name]) => name.toLocaleLowerCase() === variant.toLocaleLowerCase(), |
| 18 | + ) |
| 19 | + if (result == null) { |
| 20 | + return false |
| 21 | + } |
| 22 | + await download(result[1], 'font.ttf') |
| 23 | + await runCmd("fontforge -lang=ff -c 'Open($1); SelectAll(); RemoveOverlap(); Generate($2)' font.ttf fixed-font.ttf") |
| 24 | + await runCmd(`npm run msdf`) |
| 25 | + await runCmd(`npm run webp`) |
| 26 | + return true |
| 27 | +} |
| 28 | + |
| 29 | +const variants = { light: '300', regular: '400', medium: '500', 'semi-bold': '600', bold: '700' } |
| 30 | +const fontFamilies = ['Inter'] |
| 31 | + |
| 32 | +async function main() { |
| 33 | + let result = '' |
| 34 | + for (const fontFamily of fontFamilies) { |
| 35 | + result += `export const ${fontFamily.toLowerCase()} = {` |
| 36 | + for (const [fontWeightName, fontWeightValue] of Object.entries(variants)) { |
| 37 | + if (!(await generate(fontFamily, fontWeightValue))) { |
| 38 | + continue |
| 39 | + } |
| 40 | + result += `\t"${fontWeightName}": ${fontToJson('fixed-font.json')},` |
| 41 | + } |
| 42 | + result += `}\n\n` |
| 43 | + } |
| 44 | + writeFileSync('./index.ts', result) |
| 45 | +} |
| 46 | + |
| 47 | +main().catch(console.error) |
| 48 | + |
| 49 | +function runCmd(cmd) { |
| 50 | + return new Promise((resolve, reject) => |
| 51 | + exec(cmd, (error) => { |
| 52 | + if (error == null) { |
| 53 | + resolve() |
| 54 | + return |
| 55 | + } |
| 56 | + reject(error) |
| 57 | + }), |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +function download(url, to) { |
| 62 | + return new Promise((resolve, reject) => { |
| 63 | + const file = fs.createWriteStream(to) |
| 64 | + ;(url.startsWith('https') ? https : http) |
| 65 | + .get(url, (response) => { |
| 66 | + response.pipe(file) |
| 67 | + file.on('finish', () => { |
| 68 | + file.close() |
| 69 | + resolve() |
| 70 | + }) |
| 71 | + }) |
| 72 | + .on('error', reject) |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +function fontToJson(jsonPath) { |
| 77 | + const json = JSON.parse(fs.readFileSync(jsonPath)) |
| 78 | + |
| 79 | + for (let i = 0; i < json.pages.length; i++) { |
| 80 | + const url = resolve(dirname(jsonPath), json.pages[i]).replace('.png', '.webp') |
| 81 | + json.pages[i] = toUrl(fs.readFileSync(url), 'image/webp') |
| 82 | + } |
| 83 | + |
| 84 | + return JSON.stringify(json) |
| 85 | +} |
| 86 | + |
| 87 | +function toUrl(buf, mimeType) { |
| 88 | + return `data:${mimeType};base64,${buf.toString('base64')}` |
| 89 | +} |
0 commit comments