Permalink
Browse files

add svg2png tasks

  • Loading branch information...
dbushell committed Jun 11, 2013
1 parent 2a2177c commit f193b4f7099cd1f6e9d0a6bd10ad3dd8dc6a887e
Showing with 156 additions and 15 deletions.
  1. +16 −12 .jshintrc
  2. +8 −2 Gruntfile.js
  3. +28 −1 tasks/lib/svg2png.js
  4. +104 −0 tasks/svg2png.js
View
@@ -1,14 +1,18 @@
{
- "curly": true,
- "eqeqeq": true,
- "immed": true,
- "latedef": true,
- "newcap": true,
- "noarg": true,
- "sub": true,
- "undef": true,
- "boss": true,
- "eqnull": true,
- "node": true,
- "es5": true
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "sub": true,
+ "undef": true,
+ "boss": true,
+ "eqnull": true,
+ "node": true,
+ "es5": true,
+ "globals": {
+ "phantom": true,
+ "window": true
+ }
}
View
@@ -25,11 +25,17 @@ module.exports = function(grunt)
},
clean: {
- tests: ['tmp']
+ tests: ['test/**/*.png']
},
nodeunit: {
tests: ['test/*_test.js']
+ },
+
+ svg2png: {
+ all: {
+ files: 'test/**/*.svg'
+ }
}
});
@@ -40,7 +46,7 @@ module.exports = function(grunt)
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
- grunt.registerTask('test', ['clean', 'nodeunit']);
+ grunt.registerTask('test', ['clean', 'svg2png', 'nodeunit']);
grunt.registerTask('default', ['jshint', 'test']);
};
View
@@ -6,4 +6,31 @@
* Licensed under The MIT License (MIT)
*/
-'use strict';
+var fs = require('fs');
+
+var inputFile = phantom.args[0],
+ outputFile = phantom.args[1],
+
+ svgdata = fs.read(inputFile) || '',
+
+ frag = window.document.createElement('div'),
+
+ page = require('webpage').create();
+
+frag.innerHTML = svgdata;
+
+var svg = frag.querySelector('svg'),
+ width = svg.getAttribute('width'),
+ height = svg.getAttribute('height');
+
+page.viewportSize = {
+ width: parseFloat(width),
+ height: parseFloat(height)
+};
+
+page.open(inputFile, function(status)
+{
+ page.render(outputFile);
+ phantom.exit(false);
+ process.exit(0);
+});
View
@@ -13,7 +13,111 @@ module.exports = function(grunt)
grunt.registerMultiTask('svg2png', 'Rasterize SVG to PNG images using PhantomJS', function()
{
+ var done = this.async(),
+ files = grunt.file.expand(this.data.files),
+ total = files.length,
+ start = new Date(),
+ processed = 0,
+ completed = 0,
+ maxspawns = 10;
+ grunt.log.subhead('Rasterizing SVG to PNG (' + files.length + ' files)...');
+
+ var onComplete = function()
+ {
+ if (++completed >= total) {
+ update();
+ grunt.log.write("\n");
+ grunt.log.ok("Rasterization complete.");
+ done();
+ } else {
+ update();
+ if (processed < total) {
+ next();
+ }
+ }
+ };
+
+ var next = function()
+ {
+ var inputFile = files[processed++],
+ outputFile = (inputFile || '').replace(/\.svg$/i, '.png');
+
+ if (!inputFile) {
+ return;
+ }
+
+ grunt.util.spawn(
+ {
+ cmd: 'phantomjs',
+ args: [
+ 'tasks/lib/svg2png.js',
+ inputFile,
+ outputFile
+ ]
+ },
+ function(error, result, code) {
+ onComplete();
+ }
+ );
+ };
+
+ var styles = {
+
+ 'bold' : ['\x1B[1m', '\x1B[22m'],
+ 'italic' : ['\x1B[3m', '\x1B[23m'],
+ 'underline' : ['\x1B[4m', '\x1B[24m'],
+ 'inverse' : ['\x1B[7m', '\x1B[27m'],
+ 'strikethrough' : ['\x1B[9m', '\x1B[29m'],
+
+ 'white' : ['\x1B[37m', '\x1B[39m'],
+ 'grey' : ['\x1B[90m', '\x1B[39m'],
+ 'black' : ['\x1B[30m', '\x1B[39m'],
+
+ 'blue' : ['\x1B[34m', '\x1B[39m'],
+ 'cyan' : ['\x1B[36m', '\x1B[39m'],
+ 'green' : ['\x1B[32m', '\x1B[39m'],
+ 'magenta' : ['\x1B[35m', '\x1B[39m'],
+ 'red' : ['\x1B[31m', '\x1B[39m'],
+ 'yellow' : ['\x1B[33m', '\x1B[39m']
+
+ };
+
+ var style = function(str, format)
+ {
+ return styles[format][0] + str + styles[format][1];
+ };
+
+ var update = function()
+ {
+ if (!total) {
+ return;
+ }
+
+ process.stdout.clearLine();
+ process.stdout.cursorTo(0);
+
+ var str = style('0%', 'yellow') + ' [ ',
+ arr = [],
+ count = total,
+ percent = ((100 / total) * completed).toFixed(2);
+
+ while(count--) {
+ arr.push(count < completed ? '=' : ' ');
+ }
+ str += arr.reverse().join('');
+ str += ' ] ' + style(percent + "%", 'green') + ' (' + ((new Date() - start) / 1000).toFixed(1) + 's) ';
+ process.stdout.write(str);
+ };
+
+ if (!total) {
+ onComplete();
+ } else {
+ update();
+ while(maxspawns--) {
+ next();
+ }
+ }
});
};

0 comments on commit f193b4f

Please sign in to comment.