Skip to content

Commit

Permalink
Merge 2ab2d9f into 6041f90
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Oct 16, 2014
2 parents 6041f90 + 2ab2d9f commit 9449be9
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ You can optionally check the buffer lengths & stop downloading the image after a
* PSD
* TIFF
* WebP
* SVG

##### Upcoming
* SVG
* SWF

##### Credits
Expand Down
2 changes: 1 addition & 1 deletion lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = [
'jpg',
'png',
'psd',
// 'svg',
'svg',
'tiff',
'webp'
];
69 changes: 69 additions & 0 deletions lib/types/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

var svgReg = /<svg[^>]+[^>]*>/;
function isSVG (buffer) {
return svgReg.test(buffer);
}

var extractorRegExps = {
'root': /<svg [^>]+>/,
'width': /(^|\s)width\s*=\s*"(.+?)"/i,
'height': /(^|\s)height\s*=\s*"(.+?)"/i,
'viewbox': /(^|\s)viewbox\s*=\s*"(.+?)"/i
};

function getRatio (viewbox) {
var ratio = 1;
if (viewbox && viewbox[2]) {
var dim = viewbox[2].split(/\s/g);
if (dim.length === 4) {
dim = dim.map(function (i) {
return parseInt(i, 10);
});
ratio = (dim[2] - dim[0]) / (dim[3] - dim[1]);
}
}
return ratio;
}

function parse (buffer) {
var body = buffer.toString().replace(/[\r\n\s]+/g, ' ');
var section = body.match(extractorRegExps.root);
var root = section && section[0];
if (root) {
var width = root.match(extractorRegExps.width);
var height = root.match(extractorRegExps.height);
var viewbox = root.match(extractorRegExps.viewbox);
var ratio = getRatio(viewbox);
return {
'width': parseInt(width && width[2], 10) || 0,
'height': parseInt(height && height[2], 10) || 0,
'ratio': ratio
};
}
}

function calculate (buffer) {

var parsed = parse(buffer);
var width = parsed.width;
var height = parsed.height;
var ratio = parsed.ratio;

if (width && height) {
return { 'width': width, 'height': height };
} else {
if (width) {
return { 'width': width, 'height': Math.floor(width / ratio) };
} else if (height) {
return { 'width': Math.floor(height * ratio), 'height': height };
} else {
throw new TypeError('invalid svg');
}
}
}

module.exports = {
'detect': isSVG,
'calculate': calculate
};
1 change: 1 addition & 0 deletions specs/images/invalid/sample.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions specs/images/valid/svg/height.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
4 changes: 4 additions & 0 deletions specs/images/valid/svg/viewBox.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions specs/images/valid/svg/width.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9449be9

Please sign in to comment.