Skip to content

Commit b6f681e

Browse files
committed
scale SVG
1 parent 172c636 commit b6f681e

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

Examples/Sources/GalleryView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct GalleryView: View {
5858
ScrollView {
5959
LazyVStack(spacing: 20) {
6060
ForEach(images, id: \.self) { image in
61-
SVGView(svg: image)
61+
SVGView(svg: image.scale(x: 3, y: 1))
6262
.aspectRatio(contentMode: .fit)
6363
.padding([.leading, .trailing], 10)
6464
}

SwiftDraw/SVG.swift

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import Foundation
3535
import CoreGraphics
3636

3737
public struct SVG: Hashable {
38-
public let size: CGSize
38+
public private(set) var size: CGSize
3939

4040
// Array of commands that render the image
4141
// see: Renderer.swift
42-
let commands: [RendererCommand<CGTypes>]
42+
var commands: [RendererCommand<CGTypes>]
4343

4444
public init?(fileURL url: URL, options: SVG.Options = .default) {
4545
do {
@@ -78,8 +78,34 @@ public struct SVG: Hashable {
7878
}
7979
}
8080

81-
@available(*, unavailable, renamed: "SVG")
82-
public enum Image { }
81+
extension SVG {
82+
83+
public func scale(_ factor: CGFloat) -> SVG {
84+
scale(x: factor, y: factor)
85+
}
86+
87+
public func scale(x: CGFloat, y: CGFloat) -> SVG {
88+
var copy = self
89+
90+
copy.commands.insert(.scale(sx: x, sy: y), at: 0)
91+
copy.size = CGSize(
92+
width: size.width * x,
93+
height: size.height * y
94+
)
95+
return copy
96+
}
97+
}
98+
99+
extension SVG {
100+
101+
public mutating func scaled(_ factor: CGFloat) {
102+
self = scale(factor)
103+
}
104+
105+
public mutating func scaled(x: CGFloat, y: CGFloat) {
106+
self = scale(x: x, y: y)
107+
}
108+
}
83109

84110
extension SVG {
85111

@@ -103,6 +129,9 @@ extension SVG {
103129
}
104130
}
105131

132+
@available(*, unavailable, renamed: "SVG")
133+
public enum Image { }
134+
106135
#else
107136

108137
public struct SVG {

SwiftDrawTests/SVGTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ final class SVGTests: XCTestCase {
8585
XCTAssertEqual(reloaded.scale, 1)
8686
}
8787
#endif
88+
89+
func testScale() {
90+
let image = SVG.makeLines()
91+
92+
XCTAssertEqual(image.size, CGSize(width: 100, height: 100))
93+
XCTAssertEqual(image.scale(2).size, CGSize(width: 200, height: 200))
94+
XCTAssertEqual(image.scale(0.5).size, CGSize(width: 50, height: 50))
95+
XCTAssertEqual(image.scale(x: 2, y: 3).size, CGSize(width: 200, height: 300))
96+
97+
var copy = image
98+
copy.scaled(5)
99+
XCTAssertEqual(copy.size, CGSize(width: 500, height: 500))
100+
}
88101
}
89102

90103
private extension SVG {

0 commit comments

Comments
 (0)