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.

101 lines
3.1 KiB

  1. #!/usr/bin/env node
  2. 'use strict';
  3. const imageminZopfli = require('imagemin-zopfli');
  4. const {fabric} = require('fabric');
  5. const {DOMParser, XMLSerializer} = require('xmldom');
  6. const {readFile, writeFile} = require('fs').promises;
  7. const {resolve} = require('path');
  8. const Svgo = require('svgo');
  9. function exit(err) {
  10. if (err) console.error(err);
  11. process.exit(err ? 1 : 0);
  12. }
  13. function loadSvg(svg) {
  14. return new Promise((resolve) => {
  15. fabric.loadSVGFromString(svg, (objects, options) => {
  16. resolve({objects, options});
  17. });
  18. });
  19. }
  20. async function generateSvgFavicon(svg, outputFile) {
  21. const svgo = new Svgo({
  22. plugins: [
  23. {removeDimensions: true},
  24. {
  25. addAttributesToSVGElement: {
  26. attributes: [
  27. {'width': '32'},
  28. {'height': '32'},
  29. ],
  30. },
  31. },
  32. ],
  33. });
  34. const {data} = await svgo.optimize(svg);
  35. await writeFile(outputFile, data);
  36. }
  37. async function generate(svg, outputFile, {size, bg, removeDetail} = {}) {
  38. const parser = new DOMParser();
  39. const serializer = new XMLSerializer();
  40. const document = parser.parseFromString(svg);
  41. if (removeDetail) {
  42. for (const el of Array.from(document.getElementsByTagName('g') || [])) {
  43. for (const attribute of Array.from(el.attributes || [])) {
  44. if (attribute.name === 'class' && attribute.value === 'detail-remove') {
  45. el.parentNode.removeChild(el);
  46. }
  47. }
  48. }
  49. }
  50. svg = serializer.serializeToString(document);
  51. const {objects, options} = await loadSvg(svg);
  52. const canvas = new fabric.Canvas();
  53. canvas.setDimensions({width: size, height: size});
  54. const ctx = canvas.getContext('2d');
  55. ctx.scale(options.width ? (size / options.width) : 1, options.height ? (size / options.height) : 1);
  56. if (bg) {
  57. canvas.add(new fabric.Rect({
  58. left: 0,
  59. top: 0,
  60. height: size * (1 / (size / options.height)),
  61. width: size * (1 / (size / options.width)),
  62. fill: 'white',
  63. }));
  64. }
  65. canvas.add(fabric.util.groupSVGElements(objects, options));
  66. canvas.renderAll();
  67. let png = Buffer.from([]);
  68. for await (const chunk of canvas.createPNGStream()) {
  69. png = Buffer.concat([png, chunk]);
  70. }
  71. png = await imageminZopfli({more: true})(png);
  72. await writeFile(outputFile, png);
  73. }
  74. async function main() {
  75. const svg = await readFile(resolve(__dirname, '../assets/logo.svg'), 'utf8');
  76. await generateSvgFavicon(svg, resolve(__dirname, '../public/img/favicon.svg'));
  77. await generate(svg, resolve(__dirname, '../public/img/gitea-lg.png'), {size: 880});
  78. await generate(svg, resolve(__dirname, '../public/img/gitea-512.png'), {size: 512});
  79. await generate(svg, resolve(__dirname, '../public/img/gitea-192.png'), {size: 192});
  80. await generate(svg, resolve(__dirname, '../public/img/gitea-sm.png'), {size: 120});
  81. await generate(svg, resolve(__dirname, '../public/img/avatar_default.png'), {size: 200});
  82. await generate(svg, resolve(__dirname, '../public/img/favicon.png'), {size: 180, removeDetail: true});
  83. await generate(svg, resolve(__dirname, '../public/img/apple-touch-icon.png'), {size: 180, bg: true});
  84. }
  85. main().then(exit).catch(exit);