Skip to content

Commit ecb4d01

Browse files
committed
pathLenght for SVG elments & path rx, ry
1 parent 8bdb215 commit ecb4d01

File tree

9 files changed

+54
-10
lines changed

9 files changed

+54
-10
lines changed

Sources/SwiftSvg/Circle.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
// Created by Tibor Bodecs on 2021. 11. 29..
66
//
77

8-
8+
/// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/circle
99
open class Circle: Tag {
1010

11-
public init(cx: Double, cy: Double, r: Double) {
11+
public init(cx: Double,
12+
cy: Double,
13+
r: Double,
14+
pathLength: Double? = nil) {
1215
super.init()
1316
setAttributes([
1417
.init(key: "cx", value: cx.preciseString),
1518
.init(key: "cy", value: cy.preciseString),
1619
.init(key: "r", value: r.preciseString),
1720
])
21+
attribute("pathLength", pathLength?.preciseString)
1822
}
1923
}

Sources/SwiftSvg/Ellipse.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
// Created by Tibor Bodecs on 2021. 12. 21..
66
//
77

8+
/// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/ellipse
89
open class Ellipse: Tag {
910

10-
public init(cx: Double, cy: Double, rx: Double, ry: Double) {
11+
public init(cx: Double,
12+
cy: Double,
13+
rx: Double,
14+
ry: Double,
15+
pathLength: Double? = nil) {
1116
super.init()
1217
setAttributes([
1318
.init(key: "cx", value: cx.preciseString),
1419
.init(key: "cy", value: cy.preciseString),
1520
.init(key: "rx", value: rx.preciseString),
1621
.init(key: "ry", value: ry.preciseString),
1722
])
18-
23+
attribute("pathLength", pathLength?.preciseString)
1924
}
2025
}

Sources/SwiftSvg/Line.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
// Created by Tibor Bodecs on 2021. 11. 29..
66
//
77

8+
/// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/line
89
open class Line: Tag {
910

10-
public init(x1: Double, y1: Double, x2: Double, y2: Double) {
11+
public init(x1: Double,
12+
y1: Double,
13+
x2: Double,
14+
y2: Double,
15+
pathLength: Double? = nil) {
1116
super.init()
1217
setAttributes([
1318
.init(key: "x1", value: x1.preciseString),
1419
.init(key: "y1", value: y1.preciseString),
1520
.init(key: "x2", value: x2.preciseString),
1621
.init(key: "y2", value: y2.preciseString),
1722
])
18-
23+
attribute("pathLength", pathLength?.preciseString)
1924
}
2025
}

Sources/SwiftSvg/Path.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
//
77

88

9+
/// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
910
open class Path: Tag {
1011

11-
public init(_ d: String) {
12+
public init(_ d: String, pathLength: Double? = nil) {
1213
super.init()
1314
setAttributes([
1415
.init(key: "d", value: d),
1516
])
17+
attribute("pathLength", pathLength?.preciseString)
1618
}
1719
}

Sources/SwiftSvg/Polygon.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
// Created by Tibor Bodecs on 2021. 12. 21..
66
//
77

8+
/// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/polygon
89
open class Polygon: Tag {
910

10-
public init(_ points: [Double]) {
11+
public init(_ points: [Double], pathLength: Double? = nil) {
1112
super.init()
1213
setAttributes([
1314
.init(key: "points", value: points.map(\.preciseString).joined(separator: " ")),
1415
])
16+
attribute("pathLength", pathLength?.preciseString)
1517
}
1618
}

Sources/SwiftSvg/Polyline.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
// Created by Tibor Bodecs on 2021. 12. 21..
66
//
77

8+
/// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/polyline
89
open class Polyline: Tag {
910

10-
public init(_ points: [Double]) {
11+
public init(_ points: [Double], pathLength: Double? = nil) {
1112
super.init()
1213
setAttributes([
1314
.init(key: "points", value: points.map(\.preciseString).joined(separator: " ")),
1415
])
16+
attribute("pathLength", pathLength?.preciseString)
1517
}
1618
}

Sources/SwiftSvg/Rect.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@
55
// Created by Tibor Bodecs on 2021. 12. 21..
66
//
77

8+
/// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/rect
89
open class Rect: Tag {
910

10-
public init(x: Double, y: Double, width: Double, height: Double) {
11+
public init(x: Double,
12+
y: Double,
13+
width: Double,
14+
height: Double,
15+
rx: Double? = nil,
16+
ry: Double? = nil,
17+
pathLength: Double? = nil) {
1118
super.init()
1219
setAttributes([
1320
.init(key: "x", value: x.preciseString),
1421
.init(key: "y", value: y.preciseString),
1522
.init(key: "width", value: width.preciseString),
1623
.init(key: "height", value: height.preciseString),
1724
])
25+
attribute("rx", rx?.preciseString)
26+
attribute("ry", ry?.preciseString)
27+
attribute("pathLength", pathLength?.preciseString)
1828
}
1929
}

Sources/SwiftSvg/Svg.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Tibor Bodecs on 2021. 11. 29..
66
//
77

8+
/// https://developer.mozilla.org/en-US/docs/Web/SVG/Element
89
open class Svg: Tag {
910

1011
}

Tests/SwiftSvgTests/SwiftSvgTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ final class SwiftSvgTests: XCTestCase {
8282
""")
8383
}
8484

85+
func testRectWithOptionalAttributes() {
86+
let doc = Document {
87+
Svg {
88+
Rect(x: 1, y: 2, width: 3, height: 4, rx: 5, ry: 6, pathLength: 7)
89+
}
90+
}
91+
XCTAssertEqual(DocumentRenderer().render(doc), """
92+
<svg>
93+
<rect x="1" y="2" width="3" height="4" rx="5" ry="6" pathLength="7"></rect>
94+
</svg>
95+
""")
96+
}
97+
8598
func testEllipse() {
8699
let doc = Document {
87100
Svg {

0 commit comments

Comments
 (0)