2022-06-05 23:27:25 -04:00
|
|
|
#!/usr/bin/env node
|
2021-04-08 06:41:57 -04:00
|
|
|
import imageminZopfli from 'imagemin-zopfli';
|
2021-10-22 16:05:53 -04:00
|
|
|
import {optimize} from 'svgo';
|
2021-04-08 06:41:57 -04:00
|
|
|
import {fabric} from 'fabric';
|
2022-12-20 17:15:47 -05:00
|
|
|
import {readFile, writeFile} from 'node:fs/promises';
|
2020-12-18 20:17:27 -05:00
|
|
|
|
2020-07-26 05:47:51 -04: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});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-05 23:27:25 -04:00
|
|
|
async function generate(svg, path, {size, bg}) {
|
|
|
|
const outputFile = new URL(path, import.meta.url);
|
|
|
|
|
|
|
|
if (String(outputFile).endsWith('.svg')) {
|
2021-03-22 00:04:19 -04:00
|
|
|
const {data} = optimize(svg, {
|
2021-10-22 16:05:53 -04:00
|
|
|
plugins: [
|
|
|
|
'preset-default',
|
2021-03-22 00:04:19 -04:00
|
|
|
'removeDimensions',
|
|
|
|
{
|
|
|
|
name: 'addAttributesToSVGElement',
|
|
|
|
params: {attributes: [{width: size}, {height: size}]}
|
|
|
|
},
|
2021-10-22 16:05:53 -04:00
|
|
|
],
|
2021-01-01 14:04:35 -05:00
|
|
|
});
|
|
|
|
await writeFile(outputFile, data);
|
|
|
|
return;
|
|
|
|
}
|
2020-12-18 20:17:27 -05:00
|
|
|
|
2020-07-26 05:47:51 -04: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 20:17:27 -05:00
|
|
|
const gitea = process.argv.slice(2).includes('gitea');
|
2022-06-05 23:27:25 -04:00
|
|
|
const logoSvg = await readFile(new URL('../assets/logo.svg', import.meta.url), 'utf8');
|
|
|
|
const faviconSvg = await readFile(new URL('../assets/favicon.svg', import.meta.url), 'utf8');
|
2021-01-01 14:04:35 -05:00
|
|
|
|
2020-12-18 20:17:27 -05:00
|
|
|
await Promise.all([
|
2022-06-05 23:27:25 -04:00
|
|
|
generate(logoSvg, '../public/img/logo.svg', {size: 32}),
|
|
|
|
generate(logoSvg, '../public/img/logo.png', {size: 512}),
|
|
|
|
generate(faviconSvg, '../public/img/favicon.svg', {size: 32}),
|
|
|
|
generate(faviconSvg, '../public/img/favicon.png', {size: 180}),
|
|
|
|
generate(logoSvg, '../public/img/avatar_default.png', {size: 200}),
|
|
|
|
generate(logoSvg, '../public/img/apple-touch-icon.png', {size: 180, bg: true}),
|
|
|
|
gitea && generate(logoSvg, '../public/img/gitea.svg', {size: 32}),
|
2020-12-18 20:17:27 -05:00
|
|
|
]);
|
2020-07-26 05:47:51 -04:00
|
|
|
}
|
|
|
|
|
2021-12-04 01:43:14 -05:00
|
|
|
main().then(exit).catch(exit);
|