Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(convertShapeToPath): convert rectangles with radius to paths #1767

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 39 additions & 10 deletions plugins/convertShapeToPath.js
Expand Up @@ -33,34 +33,63 @@ exports.fn = (root, params) => {
if (
node.name === 'rect' &&
node.attributes.width != null &&
node.attributes.height != null &&
node.attributes.rx == null &&
node.attributes.ry == null
node.attributes.height != null
) {
const x = Number(node.attributes.x || '0');
const y = Number(node.attributes.y || '0');
const width = Number(node.attributes.width);
const height = Number(node.attributes.height);
let rrx =
Number(node.attributes.rx) || Number(node.attributes.ry) || 0;
let rry =
Number(node.attributes.ry) || Number(node.attributes.rx) || 0;

rrx = rrx > width / 2 ? width / 2 : rrx;
rry = rry > height / 2 ? height / 2 : rry;
// Values like '100%' compute to NaN, thus running after
// cleanupNumericValues when 'px' units has already been removed.
// TODO: Calculate sizes from % and non-px units if possible.
if (Number.isNaN(x - y + width - height)) return;

/**
* @type {Array<PathDataItem>}
*/
const pathData = [
{ command: 'M', args: [x, y] },
{ command: 'H', args: [x + width] },
{ command: 'V', args: [y + height] },
{ command: 'H', args: [x] },
{ command: 'z', args: [] },
];
let pathData = [];

if (rrx === 0 && rry === 0) {
pathData = [
{ command: 'M', args: [x, y] },
{ command: 'H', args: [x + width] },
{ command: 'V', args: [y + height] },
{ command: 'H', args: [x] },
{ command: 'z', args: [] },
];
} else {
pathData = [
{ command: 'M', args: [x + rrx, y] },
{ command: 'H', args: [x + width - rrx] },
{ command: 'A', args: [rrx, rry, 0, 0, 1, x + width, y + rry] },
{ command: 'V', args: [y + height - rry] },
{
command: 'A',
args: [rrx, rry, 0, 0, 1, x + width - rrx, y + height],
},
{ command: 'H', args: [x + rrx] },
{ command: 'A', args: [rrx, rry, 0, 0, 1, x, y + height - rry] },
{ command: 'V', args: [y + rry] },
{ command: 'A', args: [rrx, rry, 0, 0, 1, x + rrx, y] },
{ command: 'z', args: [] },
];
}

node.name = 'path';
node.attributes.d = stringifyPathData({ pathData, precision });
delete node.attributes.x;
delete node.attributes.y;
delete node.attributes.width;
delete node.attributes.height;
delete node.attributes.rx;
delete node.attributes.ry;
}

// convert line to path
Expand Down