Skip to content

Commit fa66bee

Browse files
committed
translate and expand
1 parent b6f681e commit fa66bee

File tree

4 files changed

+73
-20
lines changed

4 files changed

+73
-20
lines changed

SwiftDraw/CommandLine+Process.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,17 @@ public extension CommandLine {
9595
#if canImport(CoreGraphics)
9696
switch config.format {
9797
case .jpeg:
98-
let insets = try makeImageInsets(for: config.insets)
99-
return try image.jpegData(size: config.size.cgValue, scale: config.scale.cgValue, insets: insets)
98+
return try image
99+
.inset(makeImageInsets(for: config.insets))
100+
.jpegData(size: config.size.cgValue, scale: config.scale.cgValue)
100101
case .pdf:
101-
let insets = try makeImageInsets(for: config.insets)
102-
return try image.pdfData(size: config.size.cgValue, insets: insets)
102+
return try image
103+
.inset(makeImageInsets(for: config.insets))
104+
.pdfData(size: config.size.cgValue)
103105
case .png:
104-
let insets = try makeImageInsets(for: config.insets)
105-
return try image.pngData(size: config.size.cgValue, scale: config.scale.cgValue, insets: insets)
106+
return try image
107+
.inset(makeImageInsets(for: config.insets))
108+
.pngData(size: config.size.cgValue, scale: config.scale.cgValue)
106109
case .swift, .sfsymbol:
107110
throw Error.unsupported
108111
}
@@ -129,6 +132,12 @@ public extension CommandLine {
129132
}
130133
}
131134

135+
private extension SVG {
136+
func inset(_ insets: Insets) -> SVG {
137+
expand(top: -insets.top, left: -insets.left, bottom: -insets.bottom, right: -insets.right)
138+
}
139+
}
140+
132141
#if canImport(CoreGraphics)
133142
private extension CommandLine.Scale {
134143
var cgValue: CGFloat {

SwiftDraw/SVG+CoreGraphics.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,17 @@ public extension CGContext {
3838
func draw(_ image: SVG, in rect: CGRect? = nil) {
3939
let defaultRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
4040
let renderer = CGRenderer(context: self)
41+
saveGState()
4142

42-
guard let rect = rect, rect != defaultRect else {
43-
renderer.perform(image.commands)
44-
return
43+
if let rect = rect, rect != defaultRect {
44+
translateBy(x: rect.origin.x, y: rect.origin.y)
45+
scaleBy(
46+
x: rect.width / image.size.width,
47+
y: rect.height / image.size.height
48+
)
4549
}
50+
renderer.perform(image.commands)
4651

47-
let scale = CGSize(width: rect.width / image.size.width,
48-
height: rect.height / image.size.height)
49-
draw(image.commands, in: rect, scale: scale)
50-
}
51-
52-
fileprivate func draw(_ commands: [RendererCommand<CGTypes>], in rect: CGRect, scale: CGSize = CGSize(width: 1.0, height: 1.0)) {
53-
let renderer = CGRenderer(context: self)
54-
saveGState()
55-
translateBy(x: rect.origin.x, y: rect.origin.y)
56-
scaleBy(x: scale.width, y: scale.height)
57-
renderer.perform(commands)
5852
restoreGState()
5953
}
6054
}

SwiftDraw/SVG.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ extension SVG {
9494
)
9595
return copy
9696
}
97+
98+
public func translate(tx: CGFloat, ty: CGFloat) -> SVG {
99+
var copy = self
100+
copy.commands.insert(.translate(tx: tx, ty: ty), at: 0)
101+
return copy
102+
}
103+
104+
public func expand(top: CGFloat = 0,
105+
left: CGFloat = 0,
106+
bottom: CGFloat = 0,
107+
right: CGFloat = 0) -> SVG {
108+
var copy = self
109+
copy.commands.insert(.translate(tx: left, ty: top), at: 0)
110+
copy.size.width += left + right
111+
copy.size.height += top + bottom
112+
return copy
113+
}
97114
}
98115

99116
extension SVG {
@@ -105,6 +122,17 @@ extension SVG {
105122
public mutating func scaled(x: CGFloat, y: CGFloat) {
106123
self = scale(x: x, y: y)
107124
}
125+
126+
public mutating func translated(tx: CGFloat, ty: CGFloat) {
127+
self = translate(tx: tx, ty: ty)
128+
}
129+
130+
public mutating func expanded(top: CGFloat = 0,
131+
left: CGFloat = 0,
132+
bottom: CGFloat = 0,
133+
right: CGFloat = 0) {
134+
self = expand(top: top, left: left, bottom: bottom, right: right)
135+
}
108136
}
109137

110138
extension SVG {

SwiftDrawTests/SVGTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ final class SVGTests: XCTestCase {
9898
copy.scaled(5)
9999
XCTAssertEqual(copy.size, CGSize(width: 500, height: 500))
100100
}
101+
102+
func testTranslate() {
103+
let image = SVG.makeLines()
104+
105+
XCTAssertEqual(image.size, CGSize(width: 100, height: 100))
106+
XCTAssertEqual(image.translate(tx: 10, ty: 10).size, CGSize(width: 100, height: 100))
107+
108+
var copy = image
109+
copy.translated(tx: 50, ty: 50)
110+
XCTAssertEqual(copy.size, CGSize(width: 100, height: 100))
111+
}
112+
113+
func testExpand() {
114+
let image = SVG.makeLines()
115+
116+
XCTAssertEqual(image.size, CGSize(width: 100, height: 100))
117+
XCTAssertEqual(image.expand(top: 50, right: 30).size, CGSize(width: 130, height: 150))
118+
119+
var copy = image
120+
copy.expanded(left: -50, bottom: 30)
121+
XCTAssertEqual(copy.size, CGSize(width: 50, height: 130))
122+
}
101123
}
102124

103125
private extension SVG {

0 commit comments

Comments
 (0)