|
| 1 | +// optimize-font.ts |
| 2 | + |
| 3 | +// Import your font data |
| 4 | +import Fontmin from "fontmin" |
| 5 | +import fs from "fs" |
| 6 | +import path from "path" |
| 7 | +import { japanese_colors as fontData } from "./japanese_colors.ts" |
| 8 | + |
| 9 | +// 修改为文件夹路径 |
| 10 | +const srcDir = |
| 11 | + "/Volumes/homes/cs-magic/cs-magic-inner/assets/fonts/16_SourceHanSerifHK/SubsetOTF/HK" |
| 12 | +const destPath = "/Users/mark/coding/apps/totem-gen_mini_2/src/assets/fonts" |
| 13 | + |
| 14 | +const loadTexts = () => { |
| 15 | + const chinese_words_list = fontData.map(item => item.Chinese_Name) |
| 16 | + const uniqueChars = [...new Set(chinese_words_list.join("").split(""))].join("") |
| 17 | + console.log("Total unique characters:", uniqueChars.length) |
| 18 | + const n = 40 |
| 19 | + for (let i = 0; i < uniqueChars.length; i += n) { |
| 20 | + console.log(uniqueChars.substring(i, i + n)) |
| 21 | + } |
| 22 | + return uniqueChars |
| 23 | +} |
| 24 | + |
| 25 | +const text = loadTexts() |
| 26 | + |
| 27 | +// 处理单个字体文件 |
| 28 | +const processFont = (srcPath: string) => { |
| 29 | + const fileName = path.basename(srcPath) |
| 30 | + console.log(`Processing font: ${fileName}...`) |
| 31 | + |
| 32 | + const fontmin = new Fontmin() |
| 33 | + .src(srcPath) |
| 34 | + .use( |
| 35 | + Fontmin.otf2ttf({ |
| 36 | + text, |
| 37 | + }), |
| 38 | + ) |
| 39 | + .use(Fontmin.ttf2woff2()) |
| 40 | + .dest(destPath) |
| 41 | + |
| 42 | + return new Promise((resolve, reject) => { |
| 43 | + fontmin.run((err, files) => { |
| 44 | + if (err) { |
| 45 | + reject(err) |
| 46 | + return |
| 47 | + } |
| 48 | + console.log(`Completed processing: ${fileName}`) |
| 49 | + resolve(files) |
| 50 | + }) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +// 主函数:处理文件夹中的所有字体文件 |
| 55 | +const processFontDirectory = async () => { |
| 56 | + try { |
| 57 | + // 读取源目录中的所有文件 |
| 58 | + const files = fs.readdirSync(srcDir) |
| 59 | + const fontFiles = files.filter( |
| 60 | + file => file.toLowerCase().endsWith(".otf") || file.toLowerCase().endsWith(".ttf"), |
| 61 | + ) |
| 62 | + |
| 63 | + console.log(`Found ${fontFiles.length} font files to process`) |
| 64 | + |
| 65 | + // 依次处理每个字体文件 |
| 66 | + for (const file of fontFiles) { |
| 67 | + const srcPath = path.join(srcDir, file) |
| 68 | + await processFont(srcPath) |
| 69 | + } |
| 70 | + |
| 71 | + console.log("All fonts have been processed successfully!") |
| 72 | + } catch (error) { |
| 73 | + console.error("Error processing fonts:", error) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// 执行主函数 |
| 78 | +processFontDirectory() |
0 commit comments