summaryrefslogtreecommitdiffstats
path: root/build/generate-images.js
blob: 9b7b8201720eb2427b1be3645ead6e954cfc0e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
'use strict';

const imageminZopfli = require('imagemin-zopfli');
const {fabric} = require('fabric');
const {DOMParser, XMLSerializer} = require('xmldom');
const {readFile, writeFile} = require('fs').promises;
const {resolve} = require('path');
const Svgo = require('svgo');

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});
    });
  });
}

async function generateSvgFavicon(svg, outputFile) {
  const svgo = new Svgo({
    plugins: [
      {removeDimensions: true},
      {
        addAttributesToSVGElement: {
          attributes: [
            {'width': '32'},
            {'height': '32'},
          ],
        },
      },
    ],
  });

  const {data} = await svgo.optimize(svg);
  await writeFile(outputFile, data);
}

async function generate(svg, outputFile, {size, bg, removeDetail} = {}) {
  const parser = new DOMParser();
  const serializer = new XMLSerializer();
  const document = parser.parseFromString(svg);

  if (removeDetail) {
    for (const el of Array.from(document.getElementsByTagName('g') || [])) {
      for (const attribute of Array.from(el.attributes || [])) {
        if (attribute.name === 'class' && attribute.value === 'detail-remove') {
          el.parentNode.removeChild(el);
        }
      }
    }
  }

  svg = serializer.serializeToString(document);

  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() {
  const svg = await readFile(resolve(__dirname, '../assets/logo.svg'), 'utf8');
  await generateSvgFavicon(svg, resolve(__dirname, '../public/img/favicon.svg'));
  await generate(svg, resolve(__dirname, '../public/img/gitea-lg.png'), {size: 880});
  await generate(svg, resolve(__dirname, '../public/img/gitea-512.png'), {size: 512});
  await generate(svg, resolve(__dirname, '../public/img/gitea-192.png'), {size: 192});
  await generate(svg, resolve(__dirname, '../public/img/gitea-sm.png'), {size: 120});
  await generate(svg, resolve(__dirname, '../public/img/avatar_default.png'), {size: 200});
  await generate(svg, resolve(__dirname, '../public/img/favicon.png'), {size: 180, removeDetail: true});
  await generate(svg, resolve(__dirname, '../public/img/apple-touch-icon.png'), {size: 180, bg: true});
}

main().then(exit).catch(exit);