mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.5 KiB
80 lines
2.5 KiB
2 years ago
|
#!/usr/bin/env node
|
||
8 months ago
|
import imageminZopfli from 'imagemin-zopfli'; // eslint-disable-line i/no-unresolved
|
||
|
import {loadSVGFromString, Canvas, Rect, util} from 'fabric/node'; // eslint-disable-line i/no-unresolved
|
||
3 years ago
|
import {optimize} from 'svgo';
|
||
2 years ago
|
import {readFile, writeFile} from 'node:fs/promises';
|
||
9 months ago
|
import {argv, exit} from 'node:process';
|
||
4 years ago
|
|
||
9 months ago
|
function doExit(err) {
|
||
4 years ago
|
if (err) console.error(err);
|
||
9 months ago
|
exit(err ? 1 : 0);
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
async function generate(svg, path, {size, bg}) {
|
||
|
const outputFile = new URL(path, import.meta.url);
|
||
|
|
||
|
if (String(outputFile).endsWith('.svg')) {
|
||
4 years ago
|
const {data} = optimize(svg, {
|
||
3 years ago
|
plugins: [
|
||
|
'preset-default',
|
||
4 years ago
|
'removeDimensions',
|
||
|
{
|
||
|
name: 'addAttributesToSVGElement',
|
||
|
params: {attributes: [{width: size}, {height: size}]}
|
||
|
},
|
||
3 years ago
|
],
|
||
4 years ago
|
});
|
||
|
await writeFile(outputFile, data);
|
||
|
return;
|
||
|
}
|
||
4 years ago
|
|
||
9 months ago
|
const {objects, options} = await loadSVGFromString(svg);
|
||
|
const canvas = new Canvas();
|
||
4 years ago
|
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) {
|
||
9 months ago
|
canvas.add(new Rect({
|
||
4 years ago
|
left: 0,
|
||
|
top: 0,
|
||
|
height: size * (1 / (size / options.height)),
|
||
|
width: size * (1 / (size / options.width)),
|
||
|
fill: 'white',
|
||
|
}));
|
||
|
}
|
||
|
|
||
9 months ago
|
canvas.add(util.groupSVGElements(objects, options));
|
||
4 years ago
|
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() {
|
||
9 months ago
|
const gitea = argv.slice(2).includes('gitea');
|
||
2 years ago
|
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');
|
||
4 years ago
|
|
||
4 years ago
|
await Promise.all([
|
||
1 year ago
|
generate(logoSvg, '../public/assets/img/logo.svg', {size: 32}),
|
||
|
generate(logoSvg, '../public/assets/img/logo.png', {size: 512}),
|
||
|
generate(faviconSvg, '../public/assets/img/favicon.svg', {size: 32}),
|
||
|
generate(faviconSvg, '../public/assets/img/favicon.png', {size: 180}),
|
||
|
generate(logoSvg, '../public/assets/img/avatar_default.png', {size: 200}),
|
||
|
generate(logoSvg, '../public/assets/img/apple-touch-icon.png', {size: 180, bg: true}),
|
||
|
gitea && generate(logoSvg, '../public/assets/img/gitea.svg', {size: 32}),
|
||
4 years ago
|
]);
|
||
4 years ago
|
}
|
||
|
|
||
9 months ago
|
try {
|
||
9 months ago
|
doExit(await main());
|
||
9 months ago
|
} catch (err) {
|
||
9 months ago
|
doExit(err);
|
||
9 months ago
|
}
|