Skip to content

Commit

Permalink
Merge pull request #140 from yWorks/fix-rounded-rects
Browse files Browse the repository at this point in the history
fix rounded rects when either rx or ry is omitted
  • Loading branch information
HackbrettXXX authored Jul 17, 2020
2 parents 9c27339 + 35f9dfe commit 3979bf9
Show file tree
Hide file tree
Showing 34 changed files with 43 additions and 22 deletions.
51 changes: 29 additions & 22 deletions src/nodes/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,36 @@ export class Rect extends GeometryNode {
if (!isFinite(w) || w <= 0 || !isFinite(h) || h <= 0) {
return null
}
const MyArc = (4 / 3) * (Math.SQRT2 - 1),
rx = Math.min(
parseFloat(getAttribute(this.element, context.styleSheets, 'rx') || '0'),
w * 0.5
),
ry = Math.min(
parseFloat(getAttribute(this.element, context.styleSheets, 'ry') || '0'),
h * 0.5
)
let x = parseFloat(getAttribute(this.element, context.styleSheets, 'x') || '0'),
y = parseFloat(getAttribute(this.element, context.styleSheets, 'y') || '0')

return new Path()
.moveTo((x += rx), y)
.lineTo((x += w - 2 * rx), y)
.curveTo(x + rx * MyArc, y, x + rx, y + (ry - ry * MyArc), (x += rx), (y += ry))
.lineTo(x, (y += h - 2 * ry))
.curveTo(x, y + ry * MyArc, x - rx * MyArc, y + ry, (x -= rx), (y += ry))
.lineTo((x += -w + 2 * rx), y)
.curveTo(x - rx * MyArc, y, x - rx, y - ry * MyArc, (x -= rx), (y -= ry))
.lineTo(x, (y += -h + 2 * ry))
.curveTo(x, y - ry * MyArc, x + rx * MyArc, y - ry, (x += rx), (y -= ry))
.close()
const rxAttr = getAttribute(this.element, context.styleSheets, 'rx')
const ryAttr = getAttribute(this.element, context.styleSheets, 'ry')
const rx = Math.min(parseFloat(rxAttr || ryAttr || '0'), w * 0.5)
const ry = Math.min(parseFloat(ryAttr || rxAttr || '0'), h * 0.5)

let x = parseFloat(getAttribute(this.element, context.styleSheets, 'x') || '0')
let y = parseFloat(getAttribute(this.element, context.styleSheets, 'y') || '0')

const arc = (4 / 3) * (Math.SQRT2 - 1)
if (rx === 0 && ry === 0) {
return new Path()
.moveTo(x, y)
.lineTo(x + w, y)
.lineTo(x + w, y + h)
.lineTo(x, y + h)
.close()
} else {
return new Path()
.moveTo((x += rx), y)
.lineTo((x += w - 2 * rx), y)
.curveTo(x + rx * arc, y, x + rx, y + (ry - ry * arc), (x += rx), (y += ry))
.lineTo(x, (y += h - 2 * ry))
.curveTo(x, y + ry * arc, x - rx * arc, y + ry, (x -= rx), (y += ry))
.lineTo((x += -w + 2 * rx), y)
.curveTo(x - rx * arc, y, x - rx, y - ry * arc, (x -= rx), (y -= ry))
.lineTo(x, (y += -h + 2 * ry))
.curveTo(x, y - ry * arc, x + rx * arc, y - ry, (x += rx), (y -= ry))
.close()
}
}

protected computeNodeTransformCore(context: Context): Matrix {
Expand Down
1 change: 1 addition & 0 deletions test/common/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ window.tests = [
'pattern-units',
'patterns',
'polyline',
'rect',
'references',
'remote-images',
'strokes-and-bounding-boxes',
Expand Down
Binary file modified test/specs/attribute-style-precedence/reference.pdf
Binary file not shown.
Binary file modified test/specs/clippath-svg/reference.pdf
Binary file not shown.
Binary file modified test/specs/clippath/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-bpmn/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-computer-network/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-diagram1/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-diagram2/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-flowchart1/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-movies/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-organization-chart-new/reference.pdf
Binary file not shown.
Binary file modified test/specs/complete-social-network/reference.pdf
Binary file not shown.
Binary file modified test/specs/display-none-and-visibility-inheritance/reference.pdf
Binary file not shown.
Binary file modified test/specs/fill-and-stroke-inheritance/reference.pdf
Binary file not shown.
Binary file modified test/specs/fill-and-stroke-opacity/reference.pdf
Binary file not shown.
Binary file modified test/specs/fill-and-stroke-rgba/reference.pdf
Binary file not shown.
Binary file modified test/specs/gradient-default-coordinates/reference.pdf
Binary file not shown.
Binary file modified test/specs/gradient-percent-offset/reference.pdf
Binary file not shown.
Binary file modified test/specs/gradient-units/reference.pdf
Binary file not shown.
Binary file modified test/specs/gradients-and-patterns-mixed/reference.pdf
Binary file not shown.
Binary file modified test/specs/hidden-clippath/reference.pdf
Binary file not shown.
Binary file modified test/specs/image-svg-urls/reference.pdf
Binary file not shown.
Binary file modified test/specs/opacity-and-rgba/reference.pdf
Binary file not shown.
Binary file modified test/specs/pattern-units/reference.pdf
Binary file not shown.
Binary file modified test/specs/patterns/reference.pdf
Binary file not shown.
Binary file added test/specs/rect/reference.pdf
Binary file not shown.
13 changes: 13 additions & 0 deletions test/specs/rect/spec.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/specs/references/reference.pdf
Binary file not shown.
Binary file modified test/specs/remote-images/reference.pdf
Binary file not shown.
Binary file modified test/specs/symbols/reference.pdf
Binary file not shown.
Binary file modified test/specs/transforms-parsing/reference.pdf
Binary file not shown.
Binary file modified test/specs/url-references-with-quotes/reference.pdf
Binary file not shown.
Binary file modified test/specs/zero-width-strokes/reference.pdf
Binary file not shown.

0 comments on commit 3979bf9

Please sign in to comment.