-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathCoreGraphics.swift
348 lines (297 loc) · 8.71 KB
/
CoreGraphics.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_exported import CoreGraphics
import Darwin
//===----------------------------------------------------------------------===//
// CGGeometry
//===----------------------------------------------------------------------===//
public extension CGPoint {
static var zero: CGPoint {
@_transparent // @fragile
get { return CGPoint(x: 0, y: 0) }
}
@_transparent // @fragile
init(x: Int, y: Int) {
self.init(x: CGFloat(x), y: CGFloat(y))
}
@_transparent // @fragile
init(x: Double, y: Double) {
self.init(x: CGFloat(x), y: CGFloat(y))
}
}
extension CGPoint : CustomReflectable, CustomPlaygroundQuickLookable {
public var customMirror: Mirror {
return Mirror(self, children: ["x": x, "y": y], displayStyle: .`struct`)
}
public var customPlaygroundQuickLook: PlaygroundQuickLook {
return .point(Double(x), Double(y))
}
}
extension CGPoint : CustomDebugStringConvertible {
public var debugDescription: String {
return "(\(x), \(y))"
}
}
extension CGPoint : Equatable {}
@_transparent // @fragile
@warn_unused_result
public func == (lhs: CGPoint, rhs: CGPoint) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
public extension CGSize {
static var zero: CGSize {
@_transparent // @fragile
get { return CGSize(width: 0, height: 0) }
}
@_transparent // @fragile
init(width: Int, height: Int) {
self.init(width: CGFloat(width), height: CGFloat(height))
}
@_transparent // @fragile
init(width: Double, height: Double) {
self.init(width: CGFloat(width), height: CGFloat(height))
}
}
extension CGSize : CustomReflectable, CustomPlaygroundQuickLookable {
public var customMirror: Mirror {
return Mirror(
self,
children: ["width": width, "height": height],
displayStyle: .`struct`)
}
public var customPlaygroundQuickLook: PlaygroundQuickLook {
return .size(Double(width), Double(height))
}
}
extension CGSize : CustomDebugStringConvertible {
public var debugDescription : String {
return "(\(width), \(height))"
}
}
extension CGSize : Equatable {}
@_transparent // @fragile
@warn_unused_result
public func == (lhs: CGSize, rhs: CGSize) -> Bool {
return lhs.width == rhs.width && lhs.height == rhs.height
}
public extension CGVector {
static var zero: CGVector {
@_transparent // @fragile
get { return CGVector(dx: 0, dy: 0) }
}
@_transparent // @fragile
init(dx: Int, dy: Int) {
self.init(dx: CGFloat(dx), dy: CGFloat(dy))
}
@_transparent // @fragile
init(dx: Double, dy: Double) {
self.init(dx: CGFloat(dx), dy: CGFloat(dy))
}
}
extension CGVector : Equatable {}
@_transparent // @fragile
@warn_unused_result
public func == (lhs: CGVector, rhs: CGVector) -> Bool {
return lhs.dx == rhs.dx && lhs.dy == rhs.dy
}
public extension CGRect {
static var zero: CGRect {
@_transparent // @fragile
get { return CGRect(x: 0, y: 0, width: 0, height: 0) }
}
static var null: CGRect {
@_transparent // @fragile
get { return CGRectNull }
}
static var infinite: CGRect {
@_transparent // @fragile
get { return CGRectInfinite }
}
@_transparent // @fragile
init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) {
self.init(origin: CGPoint(x: x, y: y),
size: CGSize(width: width, height: height))
}
@_transparent // @fragile
init(x: Double, y: Double, width: Double, height: Double) {
self.init(origin: CGPoint(x: x, y: y),
size: CGSize(width: width, height: height))
}
@_transparent // @fragile
init(x: Int, y: Int, width: Int, height: Int) {
self.init(origin: CGPoint(x: x, y: y),
size: CGSize(width: width, height: height))
}
var width: CGFloat {
@_transparent // @fragile
get { return CGRectGetWidth(self) }
}
var height: CGFloat {
@_transparent // @fragile
get { return CGRectGetHeight(self) }
}
var minX: CGFloat {
@_transparent // @fragile
get { return CGRectGetMinX(self) }
}
var midX: CGFloat {
@_transparent // @fragile
get { return CGRectGetMidX(self) }
}
var maxX: CGFloat {
@_transparent // @fragile
get { return CGRectGetMaxX(self) }
}
var minY: CGFloat {
@_transparent // @fragile
get { return CGRectGetMinY(self) }
}
var midY: CGFloat {
@_transparent // @fragile
get { return CGRectGetMidY(self) }
}
var maxY: CGFloat {
@_transparent // @fragile
get { return CGRectGetMaxY(self) }
}
var isNull: Bool {
@_transparent // @fragile
get { return CGRectIsNull(self) }
}
var isEmpty: Bool {
@_transparent // @fragile
get { return CGRectIsEmpty(self) }
}
var isInfinite: Bool {
@_transparent // @fragile
get { return CGRectIsInfinite(self) }
}
var standardized: CGRect {
@_transparent // @fragile
get { return CGRectStandardize(self) }
}
var integral: CGRect {
@_transparent // @fragile
get { return CGRectIntegral(self) }
}
@_transparent // @fragile
mutating func standardizeInPlace() {
self = standardized
}
@_transparent // @fragile
mutating func makeIntegralInPlace() {
self = integral
}
@_transparent // @fragile
@warn_unused_result(mutable_variant: "insetInPlace")
func insetBy(dx dx: CGFloat, dy: CGFloat) -> CGRect {
return CGRectInset(self, dx, dy)
}
@_transparent // @fragile
mutating func insetInPlace(dx dx: CGFloat, dy: CGFloat) {
self = insetBy(dx: dx, dy: dy)
}
@_transparent // @fragile
@warn_unused_result(mutable_variant: "offsetInPlace")
func offsetBy(dx dx: CGFloat, dy: CGFloat) -> CGRect {
return CGRectOffset(self, dx, dy)
}
@_transparent // @fragile
mutating func offsetInPlace(dx dx: CGFloat, dy: CGFloat) {
self = offsetBy(dx: dx, dy: dy)
}
@_transparent // @fragile
@warn_unused_result(mutable_variant: "formUnion")
func union(rect: CGRect) -> CGRect {
return CGRectUnion(self, rect)
}
@_transparent // @fragile
mutating func formUnion(rect: CGRect) {
self = union(rect)
}
@_transparent // @fragile
@warn_unused_result(mutable_variant: "formIntersection")
func intersection(rect: CGRect) -> CGRect {
return CGRectIntersection(self, rect)
}
@_transparent // @fragile
mutating func formIntersection(rect: CGRect) {
self = intersection(rect)
}
@_transparent // @fragile
@warn_unused_result
func divide(atDistance: CGFloat, fromEdge: CGRectEdge)
-> (slice: CGRect, remainder: CGRect)
{
var slice = CGRect.zero
var remainder = CGRect.zero
CGRectDivide(self, &slice, &remainder, atDistance, fromEdge)
return (slice, remainder)
}
@_transparent // @fragile
@warn_unused_result
func contains(rect: CGRect) -> Bool {
return CGRectContainsRect(self, rect)
}
@_transparent // @fragile
@warn_unused_result
func contains(point: CGPoint) -> Bool {
return CGRectContainsPoint(self, point)
}
@_transparent // @fragile
@warn_unused_result
func intersects(rect: CGRect) -> Bool {
return CGRectIntersectsRect(self, rect)
}
}
extension CGRect : CustomReflectable, CustomPlaygroundQuickLookable {
public var customMirror: Mirror {
return Mirror(
self,
children: ["origin": origin, "size": size],
displayStyle: .`struct`)
}
public var customPlaygroundQuickLook: PlaygroundQuickLook {
return .rectangle(
Double(origin.x), Double(origin.y),
Double(size.width), Double(size.height))
}
}
extension CGRect : CustomDebugStringConvertible {
public var debugDescription : String {
return "(\(origin.x), \(origin.y), \(size.width), \(size.height))"
}
}
extension CGRect : Equatable {}
@_transparent // @fragile
@warn_unused_result
public func == (lhs: CGRect, rhs: CGRect) -> Bool {
return CGRectEqualToRect(lhs, rhs)
}
// Overlay the C names of these constants with transparent definitions. The
// C constants are opaque extern globals for no good reason.
public var CGPointZero: CGPoint {
@_transparent // @fragile
get { return CGPoint.zero }
}
public var CGRectZero: CGRect {
@_transparent // @fragile
get { return CGRect.zero }
}
public var CGSizeZero: CGSize {
@_transparent // @fragile
get { return CGSize.zero }
}
public var CGAffineTransformIdentity: CGAffineTransform {
@_transparent // @fragile
get { return CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0) }
}