7
7
8
8
import Foundation
9
9
10
+ /// Represents partial parsing state for typesetting a line.Used once during typesetting and then discarded.
11
+ /// Contains a few methods for appending data or popping the current line data.
10
12
struct TypesetContext {
11
13
let documentRange : NSRange
12
14
let displayData : TextLine . DisplayData
13
15
14
16
/// Accumulated generated line fragments.
15
17
var lines : [ TextLineStorage < LineFragment > . BuildItem ] = [ ]
16
18
var maxHeight : CGFloat = 0
17
- var fragmentContext : LineFragmentTypesetContext = . init( start: 0 , width: 0.0 , height: 0.0 , descent: 0.0 )
19
+ /// The current fragment typesetting context.
20
+ var fragmentContext = LineFragmentTypesetContext ( start: 0 , width: 0.0 , height: 0.0 , descent: 0.0 )
18
21
19
22
/// Tracks the current position when laying out runs
20
23
var currentPosition : Int = 0
21
24
25
+ // MARK: - Fragment Context Modification
26
+
27
+ /// Appends an attachment to the current ``fragmentContext``
28
+ /// - Parameter attachment: The type-erased attachment to append.
22
29
mutating func appendAttachment( _ attachment: AnyTextAttachment ) {
23
30
// Check if we can append this attachment to the current line
24
31
if fragmentContext. width + attachment. width > displayData. maxWidth {
@@ -33,7 +40,12 @@ struct TypesetContext {
33
40
fragmentContext. height = fragmentContext. height == 0 ? maxHeight : fragmentContext. height
34
41
currentPosition += attachment. range. length
35
42
}
36
-
43
+
44
+ /// Appends a text range to the current ``fragmentContext``
45
+ /// - Parameters:
46
+ /// - typesettingRange: The range relative to the typesetter for the current fragment context.
47
+ /// - lineBreak: The position that the text fragment should end at, relative to the typesetter's range.
48
+ /// - typesetData: Data received from the typesetter.
37
49
mutating func appendText( typesettingRange: NSRange , lineBreak: Int , typesetData: CTLineTypesetData ) {
38
50
fragmentContext. contents. append (
39
51
. init( data: . text( line: typesetData. ctLine) , width: typesetData. width)
@@ -44,6 +56,9 @@ struct TypesetContext {
44
56
currentPosition = lineBreak + typesettingRange. location
45
57
}
46
58
59
+ // MARK: - Pop Fragments
60
+
61
+ /// Pop the current fragment state into a new line fragment, and reset the fragment state.
47
62
mutating func popCurrentData( ) {
48
63
let fragment = LineFragment (
49
64
documentRange: NSRange (
0 commit comments