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.6 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. function exit(err) {
  9. if (err) console.error(err);
  10. process.exit(err ? 1 : 0);
  11. }
  12. function loadSvg(svg) {
  13. return new Promise((resolve) => {
  14. fabric.loadSVGFromString(svg, (objects, options) => {
  15. resolve({objects, options});
  16. });
  17. });
  18. }
  19. async function generate(svg, outputFile, {size, bg, removeDetail} = {}) {
  20. const parser = new DOMParser();
  21. const serializer = new XMLSerializer();
  22. const document = parser.parseFromString(svg);
  23. if (removeDetail) {
  24. for (const el of Array.from(document.getElementsByTagName('g') || [])) {
  25. for (const attribute of Array.from(el.attributes || [])) {
  26. if (attribute.name === 'class' && attribute.value === 'detail-remove') {
  27. el.parentNode.removeChild(el);
  28. }
  29. }
  30. }
  31. }
  32. svg = serializer.serializeToString(document);
  33. const {objects, options} = await loadSvg(svg);
  34. const canvas = new fabric.Canvas();
  35. canvas.setDimensions({width: size, height: size});
  36. const ctx = canvas.getContext('2d');
  37. ctx.scale(options.width ? (size / options.width) : 1, options.height ? (size / options.height) : 1);
  38. if (bg) {
  39. canvas.add(new fabric.Rect({
  40. left: 0,
  41. top: 0,
  42. height: size * (1 / (size / options.height)),
  43. width: size * (1 / (size / options.width)),
  44. fill: 'white',
  45. }));
  46. }
  47. canvas.add(fabric.util.groupSVGElements(objects, options));
  48. canvas.renderAll();
  49. let png = Buffer.from([]);
  50. for await (const chunk of canvas.createPNGStream()) {
  51. png = Buffer.concat([png, chunk]);
  52. }
  53. png = await imageminZopfli({more: true})(png);
  54. await writeFile(outputFile, png);
  55. }
  56. async function main() {
  57. const svg = await readFile(resolve(__dirname, '../assets/logo.svg'), 'utf8');
  58. await generate(svg, resolve(__dirname, '../public/img/gitea-lg.png'), {size: 880});
  59. await generate(svg, resolve(__dirname, '../public/img/gitea-512.png'), {size: 512});
  60. await generate(svg, resolve(__dirname, '../public/img/gitea-192.png'), {size: 192});
  61. await generate(svg, resolve(__dirname, '../public/img/gitea-sm.png'), {size: 120});
  62. await generate(svg, resolve(__dirname, '../public/img/avatar_default.png'), {size: 200});
  63. await generate(svg, resolve(__dirname, '../public/img/favicon.png'), {size: 180, removeDetail: true});
  64. await generate(svg, resolve(__dirname, '../public/img/apple-touch-icon.png'), {size: 180, bg: true});
  65. }
  66. main().then(exit).catch(exit);