svg2png.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*!
  2. * grunt-svg2png
  3. * https://github.com/dbushell/grunt-svg2png
  4. *
  5. * Copyright (c) 2013 David Bushell
  6. * Licensed under The MIT License (MIT)
  7. */
  8. 'use strict';
  9. var phantomjs = require('phantomjs'),
  10. path = require('path');
  11. module.exports = function(grunt)
  12. {
  13. grunt.registerMultiTask('svg2png', 'Rasterize SVG to PNG images using PhantomJS', function()
  14. {
  15. var done = this.async(),
  16. start = new Date(),
  17. completed = 0,
  18. files = [],
  19. total = 0;
  20. this.files.forEach(function(fset)
  21. {
  22. fset.src.forEach(function(svg)
  23. {
  24. var src = path.resolve((fset.cwd || "") + svg),
  25. dest;
  26. if (fset.dest) {
  27. dest = path.resolve(fset.dest) + '/' + svg;
  28. } else {
  29. dest = src;
  30. }
  31. files.push({
  32. src: src,
  33. dest: dest.replace(/\.svg$/i, '.png')
  34. });
  35. });
  36. total = files.length;
  37. });
  38. if (!total) {
  39. grunt.log.subhead('No files to rasterize');
  40. done();
  41. return;
  42. }
  43. grunt.log.subhead('Rasterizing SVG to PNG (' + files.length + ' files)...');
  44. var styles = {
  45. 'bold' : ['\x1B[1m', '\x1B[22m'],
  46. 'italic' : ['\x1B[3m', '\x1B[23m'],
  47. 'underline' : ['\x1B[4m', '\x1B[24m'],
  48. 'inverse' : ['\x1B[7m', '\x1B[27m'],
  49. 'strikethrough' : ['\x1B[9m', '\x1B[29m'],
  50. 'white' : ['\x1B[37m', '\x1B[39m'],
  51. 'grey' : ['\x1B[90m', '\x1B[39m'],
  52. 'black' : ['\x1B[30m', '\x1B[39m'],
  53. 'blue' : ['\x1B[34m', '\x1B[39m'],
  54. 'cyan' : ['\x1B[36m', '\x1B[39m'],
  55. 'green' : ['\x1B[32m', '\x1B[39m'],
  56. 'magenta' : ['\x1B[35m', '\x1B[39m'],
  57. 'red' : ['\x1B[31m', '\x1B[39m'],
  58. 'yellow' : ['\x1B[33m', '\x1B[39m']
  59. };
  60. var style = function(str, format)
  61. {
  62. return styles[format][0] + str + styles[format][1];
  63. };
  64. var update = function()
  65. {
  66. var hasTerminal = !!process.stdout.clearLine;
  67. if (hasTerminal) {
  68. process.stdout.clearLine();
  69. process.stdout.cursorTo(0);
  70. }
  71. var str = style('0%', 'yellow') + ' [ ',
  72. arr = [],
  73. count = total,
  74. percent = ((100 / total) * completed).toFixed(2);
  75. while(count--) {
  76. arr.push(count < completed ? '=' : ' ');
  77. }
  78. str += arr.reverse().join('');
  79. str += ' ] ' + style(percent + "%", 'green') + ' (' + ((new Date() - start) / 1000).toFixed(1) + 's) ';
  80. process.stdout.write(str + (hasTerminal ? '' : "\n"));
  81. };
  82. var spawn = require('child_process').spawn(
  83. phantomjs.path,
  84. [ path.resolve(__dirname, 'lib/svg2png.js') ]
  85. );
  86. spawn.stdin.write(JSON.stringify(files));
  87. spawn.stdin.end();
  88. spawn.stdout.on('data', function(buffer)
  89. {
  90. try {
  91. var result = JSON.parse(buffer.toString());
  92. if (result.status) {
  93. completed++;
  94. update();
  95. }
  96. } catch (e) { }
  97. });
  98. spawn.on('exit', function()
  99. {
  100. update();
  101. grunt.log.write("\n");
  102. grunt.log.ok("Rasterization complete.");
  103. done();
  104. });
  105. update();
  106. });
  107. };