Skip to content

Instantly share code, notes, and snippets.

@pepelsbey
Created February 23, 2012 00:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepelsbey/1888739 to your computer and use it in GitHub Desktop.
Save pepelsbey/1888739 to your computer and use it in GitHub Desktop.
SVG Round
#!/usr/bin/env node
var fs = require('fs'), params = process.argv.slice(2);
fs.readFile(params[0], 'utf8', function(error, data) {
var input = data,
quality = params[1],
current = output = '';
for(var i=0; i < input.length; i++) {
if(/\d/.test(input[i])) {
current += input[i]
} else if(current != '' && /\./.test(input[i])) {
current += input[i];
} else {
output += (current == '' ? '' : Math.round(current * quality)) + input[i];
current = '';
}
}
output += current == '' ? '' : Math.round(current * quality);
output = output.replace(/^<\?xml\s+[^>]+>/, '')
.replace(/\s+PUBLIC\s+(("[^"]*"|'[^']*')\s*){2}/, '')
.replace(/<\!DOCTYPE\s+[^\s]+>/, '')
.replace(/http:\/\/www.w\d+org\/\d+\/svg/g, 'http://www.w3.org/2000/svg')
.replace(/http:\/\/www.w\d+org\/\d+\/xlink/g, 'http://www.w3.org/1999/xlink')
.replace(/http:\/\/www.w\d+org\/\d+\/xhtml/g, 'http://www.w3.org/1999/xhtml');
fs.writeFile('new-' + params[0], output);
});
@pepelsbey
Copy link
Author

Simple and very destructive script by Simon Pieters (see original page for extra features) for SVG file size optimization. Now in Node.js wrapper:

  • Rename this script to svground, put it in /usr/local/bin/, make chmod +x svground
  • Run svground file quality where file is the path to the file and quality is quality from 1 to needed number.

Sometimes it shaves off a lot of useless fractional values without any notable visual difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment