Skip to content

Commit 648592f

Browse files
Fix CGAffineTransform on non-Darwin platform (#5)
Co-authored-by: Kyle <[email protected]>
1 parent 5a96a88 commit 648592f

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

Sources/OpenCoreGraphics/CGAffineTransform.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ extension CGAffineTransform {
5252
}
5353

5454
public func translatedBy(x tx: CGFloat, y ty: CGFloat) -> CGAffineTransform {
55-
let t = CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: Double(tx), ty: Double(ty))
56-
return concatenating(t)
55+
CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: Double(tx), ty: Double(ty)).concatenating(self)
5756
}
5857

5958
public func scaledBy(x sx: CGFloat, y sy: CGFloat) -> CGAffineTransform {
60-
let s = CGAffineTransform(a: Double(sx), b: 0, c: 0, d: Double(sy), tx: 0, ty: 0)
61-
return concatenating(s)
59+
CGAffineTransform(a: Double(sx), b: 0, c: 0, d: Double(sy), tx: 0, ty: 0).concatenating(self)
6260
}
6361

6462
public func rotated(by angle: CGFloat) -> CGAffineTransform {
65-
return concatenating(CGAffineTransform(rotationAngle: angle))
63+
CGAffineTransform(rotationAngle: angle).concatenating(self)
6664
}
6765

6866
public func inverted() -> CGAffineTransform {

Tests/OpenCoreGraphicsShimsTests/CGAffineTransformTests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import Testing
66
import OpenCoreGraphicsShims
77
import Numerics
8+
#if OPENCOREGRAPHICS_COREGRAPHICS
9+
import struct CoreGraphics.CGAffineTransform
10+
#else
11+
import struct OpenCoreGraphics.CGAffineTransform
12+
#endif
813

914
@Suite
1015
struct CGAffineTransformTests {
@@ -92,4 +97,46 @@ struct CGAffineTransformTests {
9297
#expect(inv.tx.isApproximatelyEqual(to: singular.tx))
9398
#expect(inv.ty.isApproximatelyEqual(to: singular.ty))
9499
}
100+
101+
// MARK: - TranslateBy
102+
103+
@Test
104+
func translatedBy() {
105+
let t1 = CGAffineTransform(a: 1, b: 2, c: 3, d: 4, tx: 5, ty: 6)
106+
let t2 = t1.translatedBy(x: 7, y: 8)
107+
#expect(t2.a.isApproximatelyEqual(to: 1.0))
108+
#expect(t2.b.isApproximatelyEqual(to: 2.0))
109+
#expect(t2.c.isApproximatelyEqual(to: 3.0))
110+
#expect(t2.d.isApproximatelyEqual(to: 4.0))
111+
#expect(t2.tx.isApproximatelyEqual(to: 36.0))
112+
#expect(t2.ty.isApproximatelyEqual(to: 52.0))
113+
}
114+
115+
// MARK: - ScaledBy
116+
117+
@Test
118+
func scaledBy() {
119+
let t1 = CGAffineTransform(a: 1, b: 2, c: 3, d: 4, tx: 5, ty: 6)
120+
let t2 = t1.scaledBy(x: 7, y: 8)
121+
#expect(t2.a.isApproximatelyEqual(to: 7.0))
122+
#expect(t2.b.isApproximatelyEqual(to: 14.0))
123+
#expect(t2.c.isApproximatelyEqual(to: 24.0))
124+
#expect(t2.d.isApproximatelyEqual(to: 32.0))
125+
#expect(t2.tx.isApproximatelyEqual(to: 5.0))
126+
#expect(t2.ty.isApproximatelyEqual(to: 6.0))
127+
}
128+
129+
// MARK: - Rotated
130+
131+
@Test
132+
func rotated() {
133+
let t1 = CGAffineTransform(a: 1, b: 2, c: 3, d: 4, tx: 5, ty: 6)
134+
let t2 = t1.rotated(by: .pi / 2)
135+
#expect(t2.a.isApproximatelyEqual(to: 3.0))
136+
#expect(t2.b.isApproximatelyEqual(to: 4.0))
137+
#expect(t2.c.isApproximatelyEqual(to: -1.0))
138+
#expect(t2.d.isApproximatelyEqual(to: -2.0))
139+
#expect(t2.tx.isApproximatelyEqual(to: 5.0))
140+
#expect(t2.ty.isApproximatelyEqual(to: 6.0))
141+
}
95142
}

0 commit comments

Comments
 (0)