2021-04-08 03:41:57 -07:00
|
|
|
import imageminZopfli from 'imagemin-zopfli';
|
|
|
|
import {optimize, extendDefaultPlugins} from 'svgo';
|
|
|
|
import {fabric} from 'fabric';
|
|
|
|
import {readFile, writeFile} from 'fs/promises';
|
|
|
|
import {resolve, dirname} from 'path';
|
|
|
|
import {fileURLToPath} from 'url';
|
2020-07-26 02:47:51 -07:00
|
|
|
|
2021-04-08 03:41:57 -07:00
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
2020-12-18 18:17:27 -07:00
|
|
|
const logoFile = resolve(__dirname, '../assets/logo.svg');
|
|
|
|
|
2020-07-26 02:47:51 -07:00
|
|
|
function exit(err) {
|
|
|
|
if (err) console.error(err);
|
|
|
|
process.exit(err ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadSvg(svg) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
fabric.loadSVGFromString(svg, (objects, options) => {
|
|
|
|
resolve({objects, options});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-01 12:04:35 -07:00
|
|
|
async function generate(svg, outputFile, {size, bg}) {
|
|
|
|
if (outputFile.endsWith('.svg')) {
|
2021-03-21 21:04:19 -07:00
|
|
|
const {data} = optimize(svg, {
|
|
|
|
plugins: extendDefaultPlugins([
|
|
|
|
'removeDimensions',
|
|
|
|
{
|
|
|
|
name: 'addAttributesToSVGElement',
|
|
|
|
params: {attributes: [{width: size}, {height: size}]}
|
|
|
|
},
|
|
|
|
]),
|
2021-01-01 12:04:35 -07:00
|
|
|
});
|
|
|
|
await writeFile(outputFile, data);
|
|
|
|
return;
|
|
|
|
}
|
2020-12-18 18:17:27 -07:00
|
|
|
|
2020-07-26 02:47:51 -07:00
|
|
|
const {objects, options} = await loadSvg(svg);
|
|
|
|
const canvas = new fabric.Canvas();
|
|
|
|
canvas.setDimensions({width: size, height: size});
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
ctx.scale(options.width ? (size / options.width) : 1, options.height ? (size / options.height) : 1);
|
|
|
|
|
|
|
|
if (bg) {
|
|
|
|
canvas.add(new fabric.Rect({
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
height: size * (1 / (size / options.height)),
|
|
|
|
width: size * (1 / (size / options.width)),
|
|
|
|
fill: 'white',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas.add(fabric.util.groupSVGElements(objects, options));
|
|
|
|
canvas.renderAll();
|
|
|
|
|
|
|
|
let png = Buffer.from([]);
|
|
|
|
for await (const chunk of canvas.createPNGStream()) {
|
|
|
|
png = Buffer.concat([png, chunk]);
|
|
|
|
}
|
|
|
|
|
|
|
|
png = await imageminZopfli({more: true})(png);
|
|
|
|
await writeFile(outputFile, png);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2020-12-18 18:17:27 -07:00
|
|
|
const gitea = process.argv.slice(2).includes('gitea');
|
|
|
|
const svg = await readFile(logoFile, 'utf8');
|
2021-01-01 12:04:35 -07:00
|
|
|
|
2020-12-18 18:17:27 -07:00
|
|
|
await Promise.all([
|
2021-01-01 12:04:35 -07:00
|
|
|
generate(svg, resolve(__dirname, '../public/img/logo.svg'), {size: 32}),
|
|
|
|
generate(svg, resolve(__dirname, '../public/img/logo.png'), {size: 512}),
|
2020-12-18 18:17:27 -07:00
|
|
|
generate(svg, resolve(__dirname, '../public/img/favicon.png'), {size: 180}),
|
2021-01-01 12:04:35 -07:00
|
|
|
generate(svg, resolve(__dirname, '../public/img/avatar_default.png'), {size: 200}),
|
2020-12-18 18:17:27 -07:00
|
|
|
generate(svg, resolve(__dirname, '../public/img/apple-touch-icon.png'), {size: 180, bg: true}),
|
2021-01-01 12:04:35 -07:00
|
|
|
gitea && generate(svg, resolve(__dirname, '../public/img/gitea.svg'), {size: 32}),
|
2020-12-18 18:17:27 -07:00
|
|
|
]);
|
2020-07-26 02:47:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
main().then(exit).catch(exit);
|
|
|
|
|