-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathTextLayoutCollection+Geometry.swift
More file actions
357 lines (301 loc) · 10.8 KB
/
Copy pathTextLayoutCollection+Geometry.swift
File metadata and controls
357 lines (301 loc) · 10.8 KB
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
349
350
351
352
353
354
355
356
357
#if TEXTUAL_ENABLE_TEXT_SELECTION
import SwiftUI
extension TextLayoutCollection {
func url(for point: CGPoint) -> URL? {
guard let layout = layouts.first(where: { $0.frame.contains(point) }) else {
return nil
}
let localPoint = CGPoint(
x: point.x - layout.origin.x,
y: point.y - layout.origin.y
)
let url = layout.runs.first {
$0.typographicBounds.contains(localPoint)
}?.url
return url
}
func firstRect(for range: TextRange) -> CGRect {
guard !range.isCollapsed else {
return caretRect(for: range.start)
}
var firstRect = CGRect.null
let layout = range.start.indexPath.layout
let line = range.start.indexPath.line
for indexPath in indexPathsForRunSlices(in: range) {
guard indexPath.layout == layout, indexPath.line == line else {
break
}
firstRect = firstRect.union(runSliceSelectionRect(at: indexPath))
}
return firstRect
}
func caretRect(for position: TextPosition) -> CGRect {
let runSliceRect = runSliceRect(at: position.indexPath)
let lineRect = lineRect(at: position.indexPath)
let layoutDirection = layoutDirection(at: position.indexPath)
let x =
(position.affinity == .downstream)
? runSliceRect.leadingEdgeX(for: layoutDirection)
: runSliceRect.trailingEdgeX(for: layoutDirection)
return CGRect(x: x, y: lineRect.minY, width: 1, height: lineRect.height)
}
func closestPosition(to point: CGPoint) -> TextPosition? {
guard !layouts.isEmpty else { return nil }
let layoutIndex = layoutIndex(closestTo: point)
let layout = layouts[layoutIndex]
guard !layout.lines.isEmpty else { return nil }
let localPoint = CGPoint(
x: point.x - layout.origin.x,
y: point.y - layout.origin.y
)
let lineIndex = layout.lineIndex(closestToY: localPoint.y)
let line = layout.lines[lineIndex]
let runIndex = line.runIndex(closestToX: localPoint.x)
let run = line.runs[runIndex]
let direction = run.layoutDirection
let runSliceIndex = run.sliceIndex(closestToX: localPoint.x)
let runSlice = run.slices[runSliceIndex]
let leadingDistance = abs(
localPoint.x - runSlice.typographicBounds.leadingEdgeX(for: direction)
)
let trailingDistance = abs(
localPoint.x - runSlice.typographicBounds.trailingEdgeX(for: direction)
)
return TextPosition(
indexPath: .init(
runSlice: runSliceIndex,
run: runIndex,
line: lineIndex,
layout: layoutIndex
),
affinity: (leadingDistance <= trailingDistance) ? .downstream : .upstream
)
}
func characterRange(at point: CGPoint) -> TextRange? {
guard !layouts.isEmpty else { return nil }
let layoutIndex = layoutIndex(closestTo: point)
let layout = layouts[layoutIndex]
guard !layout.lines.isEmpty else { return nil }
let localPoint = CGPoint(
x: point.x - layout.origin.x,
y: point.y - layout.origin.y
)
let lineIndex = layout.lineIndex(closestToY: localPoint.y)
let line = layout.lines[lineIndex]
let runIndex = line.runIndex(closestToX: localPoint.x)
let run = line.runs[runIndex]
let runSliceIndex = run.sliceIndex(closestToX: localPoint.x)
let start = TextPosition(
indexPath: .init(
runSlice: runSliceIndex, run: runIndex, line: lineIndex, layout: layoutIndex),
affinity: .downstream
)
let end = TextPosition(
indexPath: .init(
runSlice: runSliceIndex, run: runIndex, line: lineIndex, layout: layoutIndex),
affinity: .upstream
)
return TextRange(start: start, end: end)
}
func isPositionAtBlockBoundary(_ position: TextPosition) -> Bool {
if position
== TextPosition(
indexPath: .init(layout: position.indexPath.layout),
affinity: .downstream
)
{
return true
}
let layout = layouts[position.indexPath.layout]
guard
let line = layout.lines.last,
let run = line.runs.last
else {
return false
}
if position
== TextPosition(
indexPath: .init(
runSlice: run.slices.endIndex - 1,
run: line.runs.endIndex - 1,
line: layout.lines.endIndex - 1,
layout: position.indexPath.layout
),
affinity: .upstream
)
{
return true
}
return false
}
func positionAbove(_ position: TextPosition, anchor: TextPosition) -> TextPosition? {
let anchorX = runSliceRect(at: anchor.indexPath).midX
if position.indexPath.line > 0 {
return closestPosition(
to: anchorX,
layoutIndex: position.indexPath.layout,
lineIndex: position.indexPath.line - 1
)
}
if position.indexPath.layout > 0 {
let previousLayout = layouts[position.indexPath.layout - 1]
let lastLineIndex = previousLayout.lines.endIndex - 1
return closestPosition(
to: anchorX,
layoutIndex: position.indexPath.layout - 1,
lineIndex: lastLineIndex
)
}
return startPosition
}
func positionBelow(_ position: TextPosition, anchor: TextPosition) -> TextPosition? {
let anchorX = runSliceRect(at: anchor.indexPath).midX
let layout = layouts[position.indexPath.layout]
if position.indexPath.line + 1 < layout.lines.endIndex {
return closestPosition(
to: anchorX,
layoutIndex: position.indexPath.layout,
lineIndex: position.indexPath.line + 1
)
}
if position.indexPath.layout + 1 < layouts.endIndex {
return closestPosition(
to: anchorX,
layoutIndex: position.indexPath.layout + 1,
lineIndex: 0
)
}
return endPosition
}
func runSliceSelectionRect(at indexPath: IndexPath) -> CGRect {
let layout = layouts[indexPath.layout]
let line = layout.lines[indexPath.line]
let runSlice = line.runs[indexPath.run].slices[indexPath.runSlice]
var rect = runSlice.typographicBounds
rect.origin.y = line.typographicBounds.minY
rect.size.height = line.typographicBounds.height
return rect.offsetBy(dx: layout.origin.x, dy: layout.origin.y)
}
}
extension TextLayoutCollection {
fileprivate func closestPosition(
to x: CGFloat,
layoutIndex: Int,
lineIndex: Int
) -> TextPosition? {
let layout = layouts[layoutIndex]
let line = layout.lines[lineIndex]
let runIndex = line.runIndex(closestToX: x)
let run = line.runs[runIndex]
let direction = run.layoutDirection
let runSliceIndex = run.sliceIndex(closestToX: x)
let runSlice = run.slices[runSliceIndex]
let leadingDistance = abs(
x - runSlice.typographicBounds.leadingEdgeX(for: direction)
)
let trailingDistance = abs(
x - runSlice.typographicBounds.trailingEdgeX(for: direction)
)
return TextPosition(
indexPath: .init(
runSlice: runSliceIndex,
run: runIndex,
line: lineIndex,
layout: layoutIndex
),
affinity: (leadingDistance <= trailingDistance) ? .downstream : .upstream
)
}
fileprivate func runSliceRect(at indexPath: IndexPath) -> CGRect {
let layout = layouts[indexPath.layout]
let runSlice = layout.lines[indexPath.line].runs[indexPath.run].slices[indexPath.runSlice]
return runSlice.typographicBounds.offsetBy(dx: layout.origin.x, dy: layout.origin.y)
}
fileprivate func lineRect(at indexPath: IndexPath) -> CGRect {
let layout = layouts[indexPath.layout]
let line = layout.lines[indexPath.line]
return line.typographicBounds.offsetBy(dx: layout.origin.x, dy: layout.origin.y)
}
fileprivate func layoutIndex(closestTo point: CGPoint) -> Int {
var closestIndex = 0
var closestDistance = CGFloat.greatestFiniteMagnitude
for (index, layout) in zip(layouts.indices, layouts) {
let distance = layout.frame.distanceSquared(to: point)
if distance < closestDistance {
closestDistance = distance
closestIndex = index
}
}
return closestIndex
}
}
extension TextLayout {
fileprivate func lineIndex(closestToY y: CGFloat) -> Int {
var closestIndex = 0
var closestDistance = CGFloat.greatestFiniteMagnitude
for (index, line) in lines.enumerated() {
let distance = line.typographicBounds.verticalDistance(to: y)
if distance < closestDistance {
closestDistance = distance
closestIndex = index
}
}
return closestIndex
}
}
extension TextLine {
fileprivate func runIndex(closestToX x: CGFloat) -> Int {
var closestIndex = 0
var closestDistance = CGFloat.greatestFiniteMagnitude
for (index, run) in runs.enumerated() {
let distance = run.typographicBounds.horizontalDistance(to: x)
if distance < closestDistance {
closestDistance = distance
closestIndex = index
}
}
return closestIndex
}
}
extension TextRun {
fileprivate func sliceIndex(closestToX x: CGFloat) -> Int {
var closestIndex = 0
var closestDistance = CGFloat.greatestFiniteMagnitude
for (index, slice) in slices.enumerated() {
let distance = slice.typographicBounds.horizontalDistance(to: x)
if distance < closestDistance {
closestDistance = distance
closestIndex = index
}
}
return closestIndex
}
}
extension CGRect {
fileprivate func leadingEdgeX(for layoutDirection: LayoutDirection) -> CGFloat {
layoutDirection == .leftToRight ? minX : maxX
}
fileprivate func trailingEdgeX(for layoutDirection: LayoutDirection) -> CGFloat {
layoutDirection == .leftToRight ? maxX : minX
}
// Vertical distance from this rect to a Y coordinate in the same space.
// Returns 0 when `y` is inside the rect’s vertical span.
fileprivate func verticalDistance(to y: CGFloat) -> CGFloat {
if y < minY { return minY - y }
if y > maxY { return y - maxY }
return 0
}
// Horizontal distance from this rect to an X coordinate in the same space.
// Returns 0 when `x` is inside the rect’s horizontal span.
fileprivate func horizontalDistance(to x: CGFloat) -> CGFloat {
if x < minX { return minX - x }
if x > maxX { return x - maxX }
return 0
}
fileprivate func distanceSquared(to point: CGPoint) -> CGFloat {
let dx = horizontalDistance(to: point.x)
let dy = verticalDistance(to: point.y)
return dx * dx + dy * dy
}
}
#endif