Skip to content

Commit

Permalink
Add g&text tag in svg (#26)
Browse files Browse the repository at this point in the history
Also basic docs & test for G & Text tag
  • Loading branch information
AaronGe88inTHU authored May 29, 2022
1 parent f87a1cf commit 1fd007c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Sources/SwiftSvg/G.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// G.swift
//
//
// Created by Aaron Ge on 2022/4/26.
//

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

}


25 changes: 25 additions & 0 deletions Sources/SwiftSvg/Text.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Text.swift
//
//
// Created by Aaron Ge on 2022/4/8.
//

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


}

public extension Text{
/// The x coordinate of the starting point of the text baseline.
func x(_ value: Double=0) -> Self {
attribute("x", value.preciseString)
}

/// The y coordinate of the starting point of the text baseline.
func y(_ value: Double=0) -> Self {
attribute("y", value.preciseString)
}

}
50 changes: 50 additions & 0 deletions Tests/SwiftSvgTests/SwiftSvgTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,55 @@ final class SwiftSvgTests: XCTestCase {
</svg>
""")
}


func testText(){
let doc = Document{
Svg{
Text("I love SVG")
.x(0)
.y(15)
.attribute("fill", "red")
.attribute("transform", "rotate(30 20,40)")
}
.height(60)
.width(200)
}

XCTAssertEqual(DocumentRenderer().render(doc), """
<svg height="60" width="200">
<text x="0" y="15" fill="red" transform="rotate(30 20,40)">I love SVG</text>
</svg>
""")
}

func testG(){
let doc = Document{
Svg{
G{
Circle(cx: 40, cy: 40, r: 25)
Circle(cx: 60, cy: 60, r: 25)
}
.attribute("fill", "white")
.attribute("stroke", "green")
.attribute("stroke-width", "5")
}
.viewBox(minX: 0, minY: 0, width: 100, height: 100)

}

XCTAssertEqual(DocumentRenderer().render(doc), """
<svg viewBox="0 0 100 100">
<g fill="white" stroke="green" stroke-width="5">
<circle cx="40" cy="40" r="25"></circle>
<circle cx="60" cy="60" r="25"></circle>
</g>
</svg>
""")
}





}

0 comments on commit 1fd007c

Please sign in to comment.