Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

Commit

Permalink
Merge branch 'feature/single-instance'
Browse files Browse the repository at this point in the history
  • Loading branch information
dbushell committed Jun 12, 2013
2 parents faa97f5 + c0e8b70 commit e78e903
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@

"description": "Grunt plugin to rasterize SVG to PNG images using PhantomJS",

"version": "0.1.2",
"version": "0.2.0",

"author": {
"name": "David Bushell",
Expand Down
49 changes: 30 additions & 19 deletions tasks/lib/svg2png.js
Expand Up @@ -6,32 +6,43 @@
* Licensed under The MIT License (MIT)
*/

var fs = require('fs');
var fs = require('fs'),
page = require('webpage').create(),
files = JSON.parse(phantom.args[0]),
total = files.length,
next = 0,

var inputFile = phantom.args[0],
outputFile = phantom.args[1],
file, svgdata, frag, svg, width, height;

svgdata = fs.read(inputFile) || '',
var nextFile = function()
{
if (next >= total) {
phantom.exit(0);
return;
}

frag = window.document.createElement('div'),
file = files[next++];

page = require('webpage').create();
svgdata = fs.read(file.src) || '';

frag.innerHTML = svgdata;
frag = window.document.createElement('div');
frag.innerHTML = svgdata;

var svg = frag.querySelector('svg'),
width = svg.getAttribute('width'),
svg = frag.querySelector('svg');
width = svg.getAttribute('width');
height = svg.getAttribute('height');

page.viewportSize = {
width: parseFloat(width),
height: parseFloat(height)
page.viewportSize = {
width: parseFloat(width),
height: parseFloat(height)
};

page.open('data:image/svg+xml;utf8,' + svgdata, function(status)
{
page.render(file.dest);
console.log(JSON.stringify({ 'file': file, 'status': status }));
nextFile();
});
};

// page.open(inputFile, function(status)
page.open('data:image/svg+xml;utf8,' + svgdata, function(status)
{
page.render(outputFile);
phantom.exit(false);
process.exit(0);
});
nextFile();
70 changes: 25 additions & 45 deletions tasks/svg2png.js
Expand Up @@ -18,9 +18,7 @@ module.exports = function(grunt)

var done = this.async(),
start = new Date(),
processed = 0,
completed = 0,
maxspawns = 10,
files = [],
total = 0;

Expand Down Expand Up @@ -50,42 +48,6 @@ module.exports = function(grunt)

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()
{
if (!files[processed]) {
return;
}
grunt.util.spawn(
{
cmd: phantomjs.path,
args: [
path.resolve(__dirname, 'lib/svg2png.js'),
files[processed].src,
files[processed].dest
]
},
function(error, result, code) {
onComplete();
}
);
processed++;
};

var styles = {

'bold' : ['\x1B[1m', '\x1B[22m'],
Expand Down Expand Up @@ -134,14 +96,32 @@ module.exports = function(grunt)
process.stdout.write(str);
};

if (!total) {
onComplete();
} else {
update();
while(maxspawns--) {
next();
var spawn = grunt.util.spawn({
cmd: phantomjs.path,
args: [
path.resolve(__dirname, 'lib/svg2png.js'),
JSON.stringify(files)
]
},
function(err, result, code)
{
grunt.log.write("\n");
grunt.log.ok("Rasterization complete.");
done();
}
}
);

spawn.stdout.on('data', function(buffer)
{
try {
var result = JSON.parse(buffer.toString());
if (result.status) {
completed++;
update();
}
} catch (e) { }
});

update();
});
};

0 comments on commit e78e903

Please sign in to comment.