Created
February 23, 2012 00:38
-
-
Save pepelsbey/1888739 to your computer and use it in GitHub Desktop.
SVG Round
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple and very destructive script by Simon Pieters (see original page for extra features) for SVG file size optimization. Now in Node.js wrapper:
svground
, put it in/usr/local/bin/
, makechmod +x svground
svground file quality
wherefile
is the path to the file andquality
is quality from 1 to needed number.Sometimes it shaves off a lot of useless fractional values without any notable visual difference.