Skip to content

Commit

Permalink
Fix: svg import ignores rectangle radius when only one side is set.
Browse files Browse the repository at this point in the history
This follows the SVG spec in which only one side can be set and set the other
side to the same value in this case.
This also adds support for percent values.

Fixes paperjs#1863
  • Loading branch information
sasensi committed Nov 4, 2020
1 parent 0956710 commit 216412d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/svg/SvgImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,25 @@ new function() {

// https://www.w3.org/TR/SVG/shapes.html#RectElement
rect: function(node) {
var rx = SvgElement.get(node, 'rx');
var ry = SvgElement.get(node, 'ry');
if (rx === null && ry !== null) {
rx = ry;
} else if (ry === null && rx !== null) {
ry = rx;
}
if (/%\s*$/.test(rx)) {
rx = parseFloat(rx) * rootSize.width / 100;
}
if (/%\s*$/.test(ry)) {
ry = parseFloat(ry) * rootSize.height / 100;
}
var radius = rx !== null && ry !== null ? new Size(rx, ry) : null;
return new Shape.Rectangle(new Rectangle(
getPoint(node),
getSize(node)
), getSize(node, 'rx', 'ry'));
},
), radius);
},

// https://www.w3.org/TR/SVG/shapes.html#LineElement
line: function(node) {
Expand Down
9 changes: 9 additions & 0 deletions test/assets/rectangles.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/tests/SvgImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ if (!isNodeContext) {
'gradients-1': {},
'gradients-2': !isPhantomContext && {},
'gradients-3': {},
'gradients-4': {}
'gradients-4': {},
'rectangles': {}
};
Base.each(svgFiles, function(options, name) {
if (options) {
Expand Down

0 comments on commit 216412d

Please sign in to comment.