Skip to content

Commit

Permalink
Merge pull request #36 from borodean/master
Browse files Browse the repository at this point in the history
Support SVG with only viewbox specified
  • Loading branch information
borodean committed Nov 23, 2015
2 parents 0061e5f + 6d25401 commit c7b79c1
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 43 deletions.
95 changes: 52 additions & 43 deletions lib/types/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,70 @@ function isSVG (buffer) {
}

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

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 parseViewbox (viewbox) {
var bounds = viewbox.split(' ');
return {
'width': parseInt(bounds[2], 10),
'height': parseInt(bounds[3], 10)
};
}

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);
function parseAttributes (root) {
var width = root.match(extractorRegExps.width);
var height = root.match(extractorRegExps.height);
var viewbox = root.match(extractorRegExps.viewbox);
return {
'width': width && parseInt(width[2], 10),
'height': height && parseInt(height[2], 10),
'viewbox': viewbox && parseViewbox(viewbox[2])
};
}

function calculateByDimensions (attrs) {
return {
'width': attrs.width,
'height': attrs.height
};
}

function calculateByViewbox (attrs) {
var ratio = attrs.viewbox.width / attrs.viewbox.height;
if (attrs.width) {
return {
'width': parseInt(width && width[2], 10) || 0,
'height': parseInt(height && height[2], 10) || 0,
'ratio': ratio
'width': attrs.width,
'height': Math.floor(attrs.width / ratio)
};
}
if (attrs.height) {
return {
'width': Math.floor(attrs.height * ratio),
'height': attrs.height
};
}
return {
'width': attrs.viewbox.width,
'height': attrs.viewbox.height
};
}

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');
var root = buffer.toString('utf8').match(extractorRegExps.root);
if (root) {
var attrs = parseAttributes(root[0]);
if (attrs.width && attrs.height) {
return calculateByDimensions(attrs);
}
if (attrs.viewbox) {
return calculateByViewbox(attrs);
}
}
throw new TypeError('invalid svg');
}

module.exports = {
Expand Down
File renamed without changes
1 change: 1 addition & 0 deletions specs/images/invalid/width.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/percentage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions specs/images/valid/svg/single-quotes.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
File renamed without changes
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.
File renamed without changes

0 comments on commit c7b79c1

Please sign in to comment.