Skip to content

Commit 35f9dfe

Browse files
committed
fix rounded rects when either rx or ry is omitted
- when rx===ry===0 write a regular rect instead of a rounded rect with 0 radius fixes #82
1 parent 9c27339 commit 35f9dfe

File tree

34 files changed

+43
-22
lines changed

34 files changed

+43
-22
lines changed

src/nodes/rect.ts

+29-22
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,36 @@ export class Rect extends GeometryNode {
1616
if (!isFinite(w) || w <= 0 || !isFinite(h) || h <= 0) {
1717
return null
1818
}
19-
const MyArc = (4 / 3) * (Math.SQRT2 - 1),
20-
rx = Math.min(
21-
parseFloat(getAttribute(this.element, context.styleSheets, 'rx') || '0'),
22-
w * 0.5
23-
),
24-
ry = Math.min(
25-
parseFloat(getAttribute(this.element, context.styleSheets, 'ry') || '0'),
26-
h * 0.5
27-
)
28-
let x = parseFloat(getAttribute(this.element, context.styleSheets, 'x') || '0'),
29-
y = parseFloat(getAttribute(this.element, context.styleSheets, 'y') || '0')
3019

31-
return new Path()
32-
.moveTo((x += rx), y)
33-
.lineTo((x += w - 2 * rx), y)
34-
.curveTo(x + rx * MyArc, y, x + rx, y + (ry - ry * MyArc), (x += rx), (y += ry))
35-
.lineTo(x, (y += h - 2 * ry))
36-
.curveTo(x, y + ry * MyArc, x - rx * MyArc, y + ry, (x -= rx), (y += ry))
37-
.lineTo((x += -w + 2 * rx), y)
38-
.curveTo(x - rx * MyArc, y, x - rx, y - ry * MyArc, (x -= rx), (y -= ry))
39-
.lineTo(x, (y += -h + 2 * ry))
40-
.curveTo(x, y - ry * MyArc, x + rx * MyArc, y - ry, (x += rx), (y -= ry))
41-
.close()
20+
const rxAttr = getAttribute(this.element, context.styleSheets, 'rx')
21+
const ryAttr = getAttribute(this.element, context.styleSheets, 'ry')
22+
const rx = Math.min(parseFloat(rxAttr || ryAttr || '0'), w * 0.5)
23+
const ry = Math.min(parseFloat(ryAttr || rxAttr || '0'), h * 0.5)
24+
25+
let x = parseFloat(getAttribute(this.element, context.styleSheets, 'x') || '0')
26+
let y = parseFloat(getAttribute(this.element, context.styleSheets, 'y') || '0')
27+
28+
const arc = (4 / 3) * (Math.SQRT2 - 1)
29+
if (rx === 0 && ry === 0) {
30+
return new Path()
31+
.moveTo(x, y)
32+
.lineTo(x + w, y)
33+
.lineTo(x + w, y + h)
34+
.lineTo(x, y + h)
35+
.close()
36+
} else {
37+
return new Path()
38+
.moveTo((x += rx), y)
39+
.lineTo((x += w - 2 * rx), y)
40+
.curveTo(x + rx * arc, y, x + rx, y + (ry - ry * arc), (x += rx), (y += ry))
41+
.lineTo(x, (y += h - 2 * ry))
42+
.curveTo(x, y + ry * arc, x - rx * arc, y + ry, (x -= rx), (y += ry))
43+
.lineTo((x += -w + 2 * rx), y)
44+
.curveTo(x - rx * arc, y, x - rx, y - ry * arc, (x -= rx), (y -= ry))
45+
.lineTo(x, (y += -h + 2 * ry))
46+
.curveTo(x, y - ry * arc, x + rx * arc, y - ry, (x += rx), (y -= ry))
47+
.close()
48+
}
4249
}
4350

4451
protected computeNodeTransformCore(context: Context): Matrix {

test/common/tests.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ window.tests = [
3636
'pattern-units',
3737
'patterns',
3838
'polyline',
39+
'rect',
3940
'references',
4041
'remote-images',
4142
'strokes-and-bounding-boxes',
-126 Bytes
Binary file not shown.

test/specs/clippath-svg/reference.pdf

-1.35 KB
Binary file not shown.

test/specs/clippath/reference.pdf

-814 Bytes
Binary file not shown.
-9.86 KB
Binary file not shown.
Binary file not shown.
-224 Bytes
Binary file not shown.
-3.14 KB
Binary file not shown.
-3.38 KB
Binary file not shown.
-517 Bytes
Binary file not shown.
Binary file not shown.
-14.6 KB
Binary file not shown.
Binary file not shown.
-336 Bytes
Binary file not shown.
-518 Bytes
Binary file not shown.
-252 Bytes
Binary file not shown.
Binary file not shown.
-259 Bytes
Binary file not shown.
-224 Bytes
Binary file not shown.
Binary file not shown.
-226 Bytes
Binary file not shown.
-437 Bytes
Binary file not shown.
-531 Bytes
Binary file not shown.
-1.79 KB
Binary file not shown.

test/specs/patterns/reference.pdf

-200 Bytes
Binary file not shown.

test/specs/rect/reference.pdf

4.19 KB
Binary file not shown.

test/specs/rect/spec.svg

+13
Loading

test/specs/references/reference.pdf

-1005 Bytes
Binary file not shown.
-112 Bytes
Binary file not shown.

test/specs/symbols/reference.pdf

-234 Bytes
Binary file not shown.
-1.55 KB
Binary file not shown.
-379 Bytes
Binary file not shown.
-140 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)