Skip to content

Commit 2b3a7a2

Browse files
authored
Merge pull request #3516 from milseman/coregraphics
[Core Graphics] Rename several APIs to be more Swifty and consistent
2 parents cc024d0 + 67e58f0 commit 2b3a7a2

File tree

4 files changed

+107
-62
lines changed

4 files changed

+107
-62
lines changed

apinotes/CoreGraphics.apinotes

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,23 @@ Functions:
7070
- Name: CGAffineTransformIsIdentity
7171
SwiftName: getter:CGAffineTransform.isIdentity(self:)
7272
- Name: CGAffineTransformTranslate
73-
SwiftName: CGAffineTransform.translateBy(self:x:y:)
73+
SwiftName: CGAffineTransform.translatedBy(self:x:y:)
7474
- Name: CGAffineTransformScale
75-
SwiftName: CGAffineTransform.scaleBy(self:x:y:)
75+
SwiftName: CGAffineTransform.scaledBy(self:x:y:)
7676
- Name: CGAffineTransformRotate
77-
SwiftName: CGAffineTransform.rotate(self:_:)
77+
SwiftName: CGAffineTransform.rotated(self:by:)
7878
- Name: CGAffineTransformConcat
79-
SwiftName: CGAffineTransform.concat(self:_:)
79+
SwiftName: CGAffineTransform.concatenating(self:_:)
80+
- Name: CGAffineTransformInvert
81+
SwiftName: CGAffineTransform.inverted(self:)
8082
- Name: CGAffineTransformEqualToTransform
8183
SwiftName: CGAffineTransform.equalTo(self:_:)
8284
- Name: CGPointApplyAffineTransform
83-
SwiftName: CGPoint.apply(self:transform:)
85+
SwiftName: CGPoint.applying(self:_:)
8486
- Name: CGSizeApplyAffineTransform
85-
SwiftName: CGSize.apply(self:transform:)
87+
SwiftName: CGSize.applying(self:_:)
8688
- Name: CGRectApplyAffineTransform
87-
SwiftName: CGRect.apply(self:transform:)
89+
SwiftName: CGRect.applying(self:_:)
8890
# CGBitmapContext
8991
- Name: CGBitmapContextCreateWithData
9092
SwiftName: CGContext.init(data:width:height:bitsPerComponent:bytesPerRow:space:bitmapInfo:releaseCallback:releaseInfo:)
@@ -126,7 +128,7 @@ Functions:
126128
# - Name: CGColorCreateCopyWithAlpha
127129
# SwiftName: CGColor.init(_:alpha:)
128130
- Name: CGColorCreateCopyByMatchingToColorSpace
129-
SwiftName: CGColor.convert(_:intent:self:options:)
131+
SwiftName: CGColor.converted(to:intent:self:options:)
130132
- Name: CGColorEqualToColor
131133
SwiftName: CGColor.equalTo(self:_:)
132134
# CGColorSpace
@@ -141,12 +143,14 @@ Functions:
141143
- Name: CGColorSpaceCopyICCProfile
142144
SwiftName: getter:CGColorSpace.iccData(self:)
143145
# CGContext
146+
- Name: CGContextConcatCTM
147+
SwiftName: CGContext.concatenate(self:_:)
144148
- Name: CGContextScaleCTM
145-
SwiftName: CGContext.scale(self:x:y:)
149+
SwiftName: CGContext.scaleBy(self:x:y:)
146150
- Name: CGContextTranslateCTM
147-
SwiftName: CGContext.translate(self:x:y:)
151+
SwiftName: CGContext.translateBy(self:x:y:)
148152
- Name: CGContextRotateCTM
149-
SwiftName: CGContext.rotate(self:byAngle:)
153+
SwiftName: CGContext.rotate(self:by:)
150154
- Name: CGContextSetLineWidth
151155
SwiftName: CGContext.setLineWidth(self:_:)
152156
- Name: CGContextSetMiterLimit
@@ -206,9 +210,11 @@ Functions:
206210
- Name: CGContextGetClipBoundingBox
207211
SwiftName: getter:CGContext.boundingBoxOfClipPath(self:)
208212
- Name: CGContextClipToRect
209-
SwiftName: CGContext.clipTo(self:_:)
213+
SwiftName: CGContext.clip(self:to:)
210214
- Name: CGContextClipToRects
211-
SwiftName: CGContext.clipTo(self:_:count:)
215+
SwiftName: CGContext.clip(self:to:count:)
216+
- Name: CGContextClipToMask
217+
SwiftName: CGContext.clip(self:to:mask:)
212218
- Name: CGContextSetFillColor
213219
SwiftName: CGContext.setFillColor(self:_:)
214220
- Name: CGContextSetFillColorWithColor
@@ -353,6 +359,8 @@ Functions:
353359
SwiftName: CGSize.equalTo(self:_:)
354360
- Name: CGRectEqualToRect
355361
SwiftName: CGRect.equalTo(self:_:)
362+
- Name: CGRectDivide
363+
SwiftName: CGRect.divided(self:slice:remainder:atDistance:from:)
356364
# CGGradient
357365
# - Name: CGGradientCreateWithColorComponents
358366
# SwiftName: CGGradient.init(colorComponentsSpace:components:locations:count:)
@@ -485,6 +493,8 @@ Globals:
485493
Availability: nonswift
486494
- Name: kCGColorSpaceGenericRGB
487495
Availability: nonswift
496+
- Name: kCGColorSpaceSRGB
497+
SwiftName: CGColorSpace.sRGB
488498

489499
#
490500
# Enums

stdlib/public/SDK/CoreGraphics/CoreGraphics.swift

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -147,43 +147,13 @@ public extension CGRect {
147147
}
148148

149149
@_transparent // @fragile
150-
mutating func standardizeInPlace() {
151-
self = standardized
152-
}
153-
154-
@_transparent // @fragile
155-
mutating func makeIntegralInPlace() {
156-
self = integral
157-
}
158-
159-
@_transparent // @fragile
160-
mutating func insetInPlace(dx: CGFloat, dy: CGFloat) {
161-
self = insetBy(dx: dx, dy: dy)
162-
}
163-
164-
@_transparent // @fragile
165-
mutating func offsetInPlace(dx: CGFloat, dy: CGFloat) {
166-
self = offsetBy(dx: dx, dy: dy)
167-
}
168-
169-
@_transparent // @fragile
170-
mutating func formUnion(_ rect: CGRect) {
171-
self = union(rect)
172-
}
173-
174-
@_transparent // @fragile
175-
mutating func formIntersection(_ rect: CGRect) {
176-
self = intersection(rect)
177-
}
178-
179-
@_transparent // @fragile
180-
func divide(_ atDistance: CGFloat, fromEdge: CGRectEdge)
150+
func divided(atDistance: CGFloat, from fromEdge: CGRectEdge)
181151
-> (slice: CGRect, remainder: CGRect)
182152
{
183153
var slice = CGRect.zero
184154
var remainder = CGRect.zero
185-
divide(slice: &slice, remainder: &remainder, amount: atDistance,
186-
edge: fromEdge)
155+
divided(slice: &slice, remainder: &remainder, atDistance: atDistance,
156+
from: fromEdge)
187157
return (slice, remainder)
188158
}
189159
}

test/1_stdlib/CGGeometry.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,28 +167,28 @@ assert(unstandard.maxY == 20)
167167
assert(unstandard == standard)
168168
assert(unstandard.standardized == standard)
169169

170-
unstandard.standardizeInPlace()
170+
unstandard = unstandard.standardized
171171
print_(unstandard, "standardized unstandard")
172172
// CHECK-NEXT: standardized unstandard -20.0 -30.0 30.0 50.0
173173

174174
rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25)
175175
print_(rect.insetBy(dx: 1, dy: -2), "insetBy")
176176
// CHECK-NEXT: insetBy 12.25 20.25 31.25 48.25
177-
rect.insetInPlace(dx: 1, dy: -2)
177+
rect = rect.insetBy(dx: 1, dy: -2)
178178
print_(rect, "insetInPlace")
179179
// CHECK-NEXT: insetInPlace 12.25 20.25 31.25 48.25
180180

181181
rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25)
182182
print_(rect.offsetBy(dx: 3, dy: -4), "offsetBy")
183183
// CHECK-NEXT: offsetBy 14.25 18.25 33.25 44.25
184-
rect.offsetInPlace(dx: 3, dy: -4)
184+
rect = rect.offsetBy(dx: 3, dy: -4)
185185
print_(rect, "offsetInPlace")
186186
// CHECK-NEXT: offsetInPlace 14.25 18.25 33.25 44.25
187187

188188
rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25)
189189
print_(rect.integral, "integral")
190190
// CHECK-NEXT: integral 11.0 22.0 34.0 45.0
191-
rect.makeIntegralInPlace()
191+
rect = rect.integral
192192
print_(rect, "makeIntegralInPlace")
193193
// CHECK-NEXT: makeIntegralInPlace 11.0 22.0 34.0 45.0
194194

@@ -203,9 +203,9 @@ print_(rect.union(distantRect), "union distant")
203203
// CHECK-NEXT: union small 10.0 20.0 34.5 46.5
204204
// CHECK-NEXT: union big 1.0 2.0 101.0 102.0
205205
// CHECK-NEXT: union distant 11.25 22.25 989.75 1978.75
206-
rect.formUnion(smallRect)
207-
rect.formUnion(bigRect)
208-
rect.formUnion(distantRect)
206+
rect = rect.union(smallRect)
207+
rect = rect.union(bigRect)
208+
rect = rect.union(distantRect)
209209
print_(rect, "formUnion")
210210
// CHECK-NEXT: formUnion 1.0 2.0 1000.0 1999.0
211211

@@ -217,13 +217,13 @@ print_(rect.intersection(distantRect), "intersect distant")
217217
// CHECK-NEXT: intersect big 11.25 22.25 33.25 44.25
218218
// CHECK-NEXT: intersect distant inf inf 0.0 0.0
219219
assert(rect.intersects(smallRect))
220-
rect.formIntersection(smallRect)
220+
rect = rect.intersection(smallRect)
221221
assert(!rect.isEmpty)
222222
assert(rect.intersects(bigRect))
223-
rect.formIntersection(bigRect)
223+
rect = rect.intersection(bigRect)
224224
assert(!rect.isEmpty)
225225
assert(!rect.intersects(distantRect))
226-
rect.formIntersection(distantRect)
226+
rect = rect.intersection(distantRect)
227227
assert(rect.isEmpty)
228228

229229

@@ -235,7 +235,7 @@ assert(!rect.contains(bigRect))
235235

236236

237237
rect = CGRect(x: 11.25, y: 22.25, width: 33.25, height: 44.25)
238-
var (slice, remainder) = rect.divide(5, fromEdge:CGRectEdge.minXEdge)
238+
var (slice, remainder) = rect.divided(atDistance: 5, from:CGRectEdge.minXEdge)
239239
print_(slice, "slice")
240240
print_(remainder, "remainder")
241241
// CHECK-NEXT: slice 11.25 22.25 5.0 44.25

test/ClangModules/CoreGraphics_test.swift

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
// RUN: %target-swift-frontend -emit-ir -O %s | FileCheck %s
1+
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.11 -emit-ir -O %s | FileCheck %s
22

33
// Test some imported CG APIs
44
import CoreGraphics
55

66
// REQUIRES: OS=macosx
77

8+
// CHECK-LABEL: define void {{.*}}rotationAround{{.*}} {
89
// Get a transform that will rotate around a given offset
910
public func rotationAround(offset: CGPoint, angle: CGFloat,
1011
transform: CGAffineTransform = .identity) -> CGAffineTransform {
1112
// FIXME: consistent API namings
12-
return transform.translateBy(x: offset.x, y: offset.y)
13-
.rotate(angle)
14-
.translateBy(x: -offset.x, y: -offset.y)
13+
return transform.translatedBy(x: offset.x, y: offset.y)
14+
.rotated(by: angle)
15+
.translatedBy(x: -offset.x, y: -offset.y)
1516

1617
// CHECK: call void @CGAffineTransformTranslate(%{{.*}}.CGAffineTransform* {{.*}}, %{{.*}}.CGAffineTransform* {{.*}},{{.*}}, {{.*}})
1718
// CHECK: call void @CGAffineTransformRotate(%{{.*}}.CGAffineTransform* {{.*}}, %{{.*}}.CGAffineTransform* {{.*}},{{.*}})
1819
// CHECK: call void @CGAffineTransformTranslate(%{{.*}}.CGAffineTransform* {{.*}}, %{{.*}}.CGAffineTransform* {{.*}},{{.*}}, {{.*}})
20+
//
21+
// CHECK: ret void
1922
}
2023

21-
// Trace a path in red
24+
// CHECK-LABEL: define void {{.*}}trace{{.*}} {
2225
public func trace(in context: CGContext, path: CGPath) {
2326
let red = CGColor(red: 1, green: 0, blue: 0, alpha: 1)
2427
context.saveGState()
@@ -32,15 +35,77 @@ public func trace(in context: CGContext, path: CGPath) {
3235
// CHECK: call void @CGContextSetStrokeColorWithColor(%{{.*}}.CGContext* %{{.*}}, %{{.*}}.CGColor* %{{.*}})
3336
// CHECK: call void @CGContextStrokePath(%{{.*}}.CGContext* %{{.*}})
3437
// CHECK: call void @CGContextRestoreGState(%{{.*}}.CGContext* %{{.*}})
38+
//
39+
// CHECK: ret void
3540
}
3641

42+
// CHECK-LABEL: define void {{.*}}pdfOperations{{.*}} {
3743
public func pdfOperations(_ context: CGContext) {
3844
context.beginPDFPage(nil)
3945
context.endPDFPage()
4046
context.closePDF()
4147
// CHECK: call void @CGPDFContextBeginPage(%{{.*}}.CGContext* %{{.*}}, %{{.*}}.__CFDictionary* {{.*}})
4248
// CHECK: call void @CGPDFContextEndPage(%{{.*}}.CGContext* %{{.*}})
4349
// CHECK: call void @CGPDFContextClose(%{{.*}}.CGContext* %{{.*}})
50+
//
51+
// CHECK: ret void
52+
}
53+
54+
// Test some more recently renamed APIs
55+
56+
// CHECK-LABEL: define void {{.*}}testColorRenames{{.*}} {
57+
public func testColorRenames(color: CGColor,
58+
intent: CGColorRenderingIntent) {
59+
let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)!
60+
// CHECK: %{{.*}} = load {{.*}}%struct.__CFString** @kCGColorSpaceSRGB {{.*}}, align 8
61+
// CHECK: %{{.*}} = {{.*}} call %struct.CGColorSpace* @CGColorSpaceCreateWithName(%struct.__CFString* %{{.*}})
62+
63+
let _ = color.converted(to: colorSpace, intent: intent, options: nil)
64+
// CHECK: %{{.*}} = {{.*}} call %struct.CGColor* @CGColorCreateCopyByMatchingToColorSpace(%struct.CGColorSpace* %{{.*}}, i32 %{{.*}}, %struct.CGColor* %{{.*}}, %struct.__CFDictionary* null)
65+
//
66+
// CHECK: ret void
67+
}
68+
69+
// CHECK-LABEL: define void {{.*}}testRenames{{.*}} {
70+
public func testRenames(transform: CGAffineTransform, context: CGContext,
71+
point: CGPoint, size: CGSize, rect: CGRect,
72+
image: CGImage,
73+
edge: CGRectEdge) {
74+
let transform = transform.inverted().concatenating(transform)
75+
// CHECK: call void @CGAffineTransformInvert(%struct.CGAffineTransform* {{.*}}, %struct.CGAffineTransform* {{.*}})
76+
// CHECK: call void @CGAffineTransformConcat(%struct.CGAffineTransform* {{.*}}, %struct.CGAffineTransform* {{.*}}, %struct.CGAffineTransform* {{.*}})
77+
78+
let _ = point.applying(transform)
79+
var rect = rect.applying(transform)
80+
let _ = size.applying(transform)
81+
// CHECK: %{{.*}} = call { double, double } @CGPointApplyAffineTransform(double %{{.*}}, double %{{.*}}, %struct.CGAffineTransform* {{.*}})
82+
// CHECK: call void @CGRectApplyAffineTransform(%struct.CGRect* {{.*}}, %struct.CGRect* {{.*}}, %struct.CGAffineTransform* {{.*}})
83+
// CHECK: %{{.*}} = call { double, double } @CGSizeApplyAffineTransform(double %{{.*}}, double %{{.*}}, %struct.CGAffineTransform* {{.*}})
84+
85+
context.concatenate(transform)
86+
context.rotate(by: CGFloat.pi)
87+
context.scaleBy(x: 1.0, y: 1.0)
88+
context.translateBy(x: 1.0, y: 1.0)
89+
// CHECK: call void @CGContextConcatCTM(%struct.CGContext* [[CONTEXT:%[0-9]+]], %struct.CGAffineTransform* {{.*}})
90+
// CHECK: call void @CGContextRotateCTM(%struct.CGContext* [[CONTEXT]], double {{.*}})
91+
// CHECK: call void @CGContextScaleCTM(%struct.CGContext* [[CONTEXT]], double {{1\.0+.*}}, double {{1\.0+.*}})
92+
// CHECK: call void @CGContextTranslateCTM(%struct.CGContext* [[CONTEXT]], double {{1\.0+.*}}, double {{1\.0+.*}})
93+
94+
context.clip(to: rect)
95+
context.clip(to: &rect, count: 2)
96+
context.clip(to: rect, mask: image)
97+
// CHECK: call void @CGContextClipToRect(%struct.CGContext* [[CONTEXT]], %struct.CGRect* byval nonnull align 8 %{{.*}})
98+
// CHECK: call void @CGContextClipToRects(%struct.CGContext* [[CONTEXT]], %struct.CGRect* nonnull %rect, i64 2)
99+
// CHECK: call void @CGContextClipToMask(%struct.CGContext* [[CONTEXT]], %struct.CGRect* byval nonnull align 8 %{{.*}}, %struct.CGImage* %{{.*}})
44100

101+
var slice = CGRect.zero
102+
var remainder = CGRect.zero
103+
rect.divided(slice: &slice, remainder: &remainder, atDistance: CGFloat(2.0),
104+
from: edge)
105+
assert((slice, remainder) == rect.divided(atDistance: CGFloat(2.0),
106+
from: edge))
107+
// CHECK: call void @CGRectDivide(%struct.CGRect* byval nonnull align 8 %{{.*}}, %struct.CGRect* nonnull %slice, %struct.CGRect* nonnull %remainder, double {{2\.0+.*}}, i32 %{{.*}})
108+
//
109+
// CHECK: ret void
45110
}
46111

0 commit comments

Comments
 (0)