2021-04-08 16:11:57 +05:30
|
|
|
import imageminZopfli from 'imagemin-zopfli';
|
2021-10-23 01:35:53 +05:30
|
|
|
import {optimize} from 'svgo';
|
2021-04-08 16:11:57 +05:30
|
|
|
import {fabric} from 'fabric';
|
2021-04-15 16:32:34 +05:30
|
|
|
import fs from 'fs';
|
2021-04-08 16:11:57 +05:30
|
|
|
import {resolve, dirname} from 'path';
|
|
|
|
import {fileURLToPath} from 'url';
|
2020-07-26 15:17:51 +05:30
|
|
|
|
2021-04-15 16:32:34 +05:30
|
|
|
const {readFile, writeFile} = fs.promises;
|
2021-04-08 16:11:57 +05:30
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
2020-12-19 06:47:27 +05:30
|
|
|
const logoFile = resolve(__dirname, '../assets/logo.svg');
|
2022-05-23 21:24:48 +05:30
|
|
|
const faviconFile = resolve(__dirname, '../assets/favicon.svg');
|
2020-12-19 06:47:27 +05:30
|
|
|
|
2020-07-26 15:17:51 +05:30
|
|
|
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-02 00:34:35 +05:30
|
|
|
async function generate(svg, outputFile, {size, bg}) {
|
|
|
|
if (outputFile.endsWith('.svg')) {
|
2021-03-22 09:34:19 +05:30
|
|
|
const {data} = optimize(svg, {
|
2021-10-23 01:35:53 +05:30
|
|
|
plugins: [
|
|
|
|
'preset-default',
|
2021-03-22 09:34:19 +05:30
|
|
|
'removeDimensions',
|
|
|
|
{
|
|
|
|
name: 'addAttributesToSVGElement',
|
|
|
|
params: {attributes: [{width: size}, {height: size}]}
|
|
|
|
},
|
2021-10-23 01:35:53 +05:30
|
|
|
],
|
2021-01-02 00:34:35 +05:30
|
|
|
});
|
|
|
|
await writeFile(outputFile, data);
|
|
|
|
return;
|
|
|
|
}
|
2020-12-19 06:47:27 +05:30
|
|
|
|
2020-07-26 15:17:51 +05:30
|
|
|
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-19 06:47:27 +05:30
|
|
|
const gitea = process.argv.slice(2).includes('gitea');
|
2022-05-23 21:24:48 +05:30
|
|
|
const logoSvg = await readFile(logoFile, 'utf8');
|
|
|
|
const faviconSvg = await readFile(faviconFile, 'utf8');
|
2021-01-02 00:34:35 +05:30
|
|
|
|
2020-12-19 06:47:27 +05:30
|
|
|
await Promise.all([
|
2022-05-23 21:24:48 +05:30
|
|
|
generate(logoSvg, resolve(__dirname, '../public/img/logo.svg'), {size: 32}),
|
|
|
|
generate(logoSvg, resolve(__dirname, '../public/img/logo.png'), {size: 512}),
|
|
|
|
generate(faviconSvg, resolve(__dirname, '../public/img/favicon.svg'), {size: 32}),
|
|
|
|
generate(faviconSvg, resolve(__dirname, '../public/img/favicon.png'), {size: 180}),
|
|
|
|
generate(logoSvg, resolve(__dirname, '../public/img/avatar_default.png'), {size: 200}),
|
|
|
|
generate(logoSvg, resolve(__dirname, '../public/img/apple-touch-icon.png'), {size: 180, bg: true}),
|
|
|
|
gitea && generate(logoSvg, resolve(__dirname, '../public/img/gitea.svg'), {size: 32}),
|
2020-12-19 06:47:27 +05:30
|
|
|
]);
|
2020-07-26 15:17:51 +05:30
|
|
|
}
|
|
|
|
|
2021-12-04 12:13:14 +05:30
|
|
|
main().then(exit).catch(exit);
|
2020-07-26 15:17:51 +05:30
|
|
|
|