Skip to content

Commit

Permalink
pathLenght for SVG elments & path rx, ry
Browse files Browse the repository at this point in the history
  • Loading branch information
tib committed Feb 18, 2022
1 parent 8bdb215 commit ecb4d01
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 10 deletions.
8 changes: 6 additions & 2 deletions Sources/SwiftSvg/Circle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
// Created by Tibor Bodecs on 2021. 11. 29..
//


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

public init(cx: Double, cy: Double, r: Double) {
public init(cx: Double,
cy: Double,
r: Double,
pathLength: Double? = nil) {
super.init()
setAttributes([
.init(key: "cx", value: cx.preciseString),
.init(key: "cy", value: cy.preciseString),
.init(key: "r", value: r.preciseString),
])
attribute("pathLength", pathLength?.preciseString)
}
}
9 changes: 7 additions & 2 deletions Sources/SwiftSvg/Ellipse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
// Created by Tibor Bodecs on 2021. 12. 21..
//

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

public init(cx: Double, cy: Double, rx: Double, ry: Double) {
public init(cx: Double,
cy: Double,
rx: Double,
ry: Double,
pathLength: Double? = nil) {
super.init()
setAttributes([
.init(key: "cx", value: cx.preciseString),
.init(key: "cy", value: cy.preciseString),
.init(key: "rx", value: rx.preciseString),
.init(key: "ry", value: ry.preciseString),
])

attribute("pathLength", pathLength?.preciseString)
}
}
9 changes: 7 additions & 2 deletions Sources/SwiftSvg/Line.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
// Created by Tibor Bodecs on 2021. 11. 29..
//

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

public init(x1: Double, y1: Double, x2: Double, y2: Double) {
public init(x1: Double,
y1: Double,
x2: Double,
y2: Double,
pathLength: Double? = nil) {
super.init()
setAttributes([
.init(key: "x1", value: x1.preciseString),
.init(key: "y1", value: y1.preciseString),
.init(key: "x2", value: x2.preciseString),
.init(key: "y2", value: y2.preciseString),
])

attribute("pathLength", pathLength?.preciseString)
}
}
4 changes: 3 additions & 1 deletion Sources/SwiftSvg/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
//


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

public init(_ d: String) {
public init(_ d: String, pathLength: Double? = nil) {
super.init()
setAttributes([
.init(key: "d", value: d),
])
attribute("pathLength", pathLength?.preciseString)
}
}
4 changes: 3 additions & 1 deletion Sources/SwiftSvg/Polygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
// Created by Tibor Bodecs on 2021. 12. 21..
//

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

public init(_ points: [Double]) {
public init(_ points: [Double], pathLength: Double? = nil) {
super.init()
setAttributes([
.init(key: "points", value: points.map(\.preciseString).joined(separator: " ")),
])
attribute("pathLength", pathLength?.preciseString)
}
}
4 changes: 3 additions & 1 deletion Sources/SwiftSvg/Polyline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
// Created by Tibor Bodecs on 2021. 12. 21..
//

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

public init(_ points: [Double]) {
public init(_ points: [Double], pathLength: Double? = nil) {
super.init()
setAttributes([
.init(key: "points", value: points.map(\.preciseString).joined(separator: " ")),
])
attribute("pathLength", pathLength?.preciseString)
}
}
12 changes: 11 additions & 1 deletion Sources/SwiftSvg/Rect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
// Created by Tibor Bodecs on 2021. 12. 21..
//

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

public init(x: Double, y: Double, width: Double, height: Double) {
public init(x: Double,
y: Double,
width: Double,
height: Double,
rx: Double? = nil,
ry: Double? = nil,
pathLength: Double? = nil) {
super.init()
setAttributes([
.init(key: "x", value: x.preciseString),
.init(key: "y", value: y.preciseString),
.init(key: "width", value: width.preciseString),
.init(key: "height", value: height.preciseString),
])
attribute("rx", rx?.preciseString)
attribute("ry", ry?.preciseString)
attribute("pathLength", pathLength?.preciseString)
}
}
1 change: 1 addition & 0 deletions Sources/SwiftSvg/Svg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Tibor Bodecs on 2021. 11. 29..
//

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

}
Expand Down
13 changes: 13 additions & 0 deletions Tests/SwiftSvgTests/SwiftSvgTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ final class SwiftSvgTests: XCTestCase {
""")
}

func testRectWithOptionalAttributes() {
let doc = Document {
Svg {
Rect(x: 1, y: 2, width: 3, height: 4, rx: 5, ry: 6, pathLength: 7)
}
}
XCTAssertEqual(DocumentRenderer().render(doc), """
<svg>
<rect x="1" y="2" width="3" height="4" rx="5" ry="6" pathLength="7"></rect>
</svg>
""")
}

func testEllipse() {
let doc = Document {
Svg {
Expand Down

0 comments on commit ecb4d01

Please sign in to comment.