Skip to content

Commit

Permalink
Fix AffineTransform initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
parkera committed Nov 7, 2016
1 parent ed1156b commit 8f8f6fd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
2 changes: 0 additions & 2 deletions apinotes/Foundation.apinotes
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,6 @@ Tags:
- Name: NSURLHandleStatus
SwiftName: NSURLHandle.Status
Typedefs:
- Name: NSAffineTransformStruct
SwiftName: AffineTransform
- Name: NSComparator
SwiftName: Comparator
- Name: NSDecimal
Expand Down
47 changes: 37 additions & 10 deletions stdlib/public/SDK/Foundation/AffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,48 @@

private let ε: CGFloat = 2.22045e-16

extension AffineTransform : ReferenceConvertible, Hashable, CustomStringConvertible {
public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConvertible {
public var m11, m12, m21, m22, tX, tY: CGFloat

public typealias ReferenceType = NSAffineTransform

private init(reference: NSAffineTransform) {
self = reference.transformStruct

/**
Creates an affine transformation.
*/
public init(m11: CGFloat, m12: CGFloat, m21: CGFloat, m22: CGFloat, tX: CGFloat, tY: CGFloat) {
self.m11 = m11
self.m12 = m12
self.m21 = m21
self.m22 = m22
self.tX = tX
self.tY = tY
}

fileprivate init(reference: NSAffineTransform) {
m11 = reference.transformStruct.m11
m12 = reference.transformStruct.m12
m21 = reference.transformStruct.m21
m22 = reference.transformStruct.m22
tX = reference.transformStruct.tX
tY = reference.transformStruct.tY
}

private var reference : NSAffineTransform {
fileprivate var reference : NSAffineTransform {
let ref = NSAffineTransform()
ref.transformStruct = self
ref.transformStruct = NSAffineTransformStruct(m11: m11, m12: m12, m21: m21, m22: m22, tX: tX, tY: tY)
return ref
}

/**
Creates an affine transformation matrix with identity values.
- seealso: identity
*/
public init() {
self.init(m11: CGFloat(1.0), m12: CGFloat(0.0),
m21: CGFloat(0.0), m22: CGFloat(1.0),
tX: CGFloat(0.0), tY: CGFloat(0.0))
}

/**
Creates an affine transformation matrix from translation values.
The matrix takes the following form:
Expand Down Expand Up @@ -270,9 +299,7 @@ extension AffineTransform : _ObjectiveCBridgeable {

@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSAffineTransform {
let t = NSAffineTransform()
t.transformStruct = self
return t
return self.reference
}

public static func _forceBridgeFromObjectiveC(_ x: NSAffineTransform, result: inout AffineTransform?) {
Expand All @@ -282,7 +309,7 @@ extension AffineTransform : _ObjectiveCBridgeable {
}

public static func _conditionallyBridgeFromObjectiveC(_ x: NSAffineTransform, result: inout AffineTransform?) -> Bool {
result = x.transformStruct
result = AffineTransform(reference: x)
return true // Can't fail
}

Expand Down
6 changes: 4 additions & 2 deletions test/stdlib/TestAffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ class TestAffineTransform : TestAffineTransformSuper {
}

func test_BasicConstruction() {
let defaultAffineTransform = AffineTransform()
let identityTransform = AffineTransform.identity

expectEqual(defaultAffineTransform, identityTransform)

// The diagonal entries (1,1) and (2,2) of the identity matrix are ones. The other entries are zeros.
// TODO: These should use DBL_MAX but it's not available as part of Glibc on Linux
Expand Down Expand Up @@ -324,8 +327,7 @@ class TestAffineTransform : TestAffineTransformSuper {
AffineTransform(m11: 0.498, m12: -0.284, m21: -0.742, m22: 0.3248, tX: 12, tY: 44)
]
for val in values {
let ref = NSAffineTransform()
ref.transformStruct = val
let ref = val as NSAffineTransform
expectEqual(ref.hashValue, val.hashValue)
}
}
Expand Down

0 comments on commit 8f8f6fd

Please sign in to comment.