This commit is contained in:
eric
2026-03-26 05:49:54 -05:00
parent cbe4585ff4
commit 082bcc8805
4 changed files with 224 additions and 6 deletions

View File

@@ -0,0 +1,46 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import pngToIco from 'png-to-ico'
import { Resvg } from '@resvg/resvg-js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const root = path.resolve(__dirname, '..')
const buildDir = path.join(root, 'build')
const srcSvg = path.join(buildDir, 'app-icon.svg')
const outPng = path.join(buildDir, 'icon.png')
const outIco = path.join(buildDir, 'icon.ico')
if (!fs.existsSync(srcSvg)) {
console.error(`[gen:icons] missing ${srcSvg}`)
process.exit(1)
}
fs.mkdirSync(buildDir, { recursive: true })
const svgBuf = fs.readFileSync(srcSvg)
const svg = svgBuf.toString('utf8')
function renderSvgToPngBuffer(svgInputUtf8, size) {
const resvg = new Resvg(svgInputUtf8, {
fitTo: { mode: 'width', value: size },
background: 'rgba(0,0,0,0)',
})
const img = resvg.render()
return img.asPng()
}
// 生成一张 256x256 的 PNG也可供托盘/文档等使用)
fs.writeFileSync(outPng, renderSvgToPngBuffer(svg, 256))
// Windows .ico包含多尺寸位图更稳任务栏/快捷方式/高 DPI
const sizes = [16, 24, 32, 48, 64, 128, 256]
const pngBuffers = sizes.map((s) => renderSvgToPngBuffer(svg, s))
const ico = await pngToIco(pngBuffers)
fs.writeFileSync(outIco, ico)
console.log(`[gen:icons] wrote ${path.relative(root, outIco)} and ${path.relative(root, outPng)}`)