77
88import Foundation
99
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.
1012struct TypesetContext {
1113 let documentRange : NSRange
1214 let displayData : TextLine . DisplayData
1315
1416 /// Accumulated generated line fragments.
1517 var lines : [ TextLineStorage < LineFragment > . BuildItem ] = [ ]
1618 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 )
1821
1922 /// Tracks the current position when laying out runs
2023 var currentPosition : Int = 0
2124
25+ // MARK: - Fragment Context Modification
26+
27+ /// Appends an attachment to the current ``fragmentContext``
28+ /// - Parameter attachment: The type-erased attachment to append.
2229 mutating func appendAttachment( _ attachment: AnyTextAttachment ) {
2330 // Check if we can append this attachment to the current line
2431 if fragmentContext. width + attachment. width > displayData. maxWidth {
@@ -33,7 +40,12 @@ struct TypesetContext {
3340 fragmentContext. height = fragmentContext. height == 0 ? maxHeight : fragmentContext. height
3441 currentPosition += attachment. range. length
3542 }
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.
3749 mutating func appendText( typesettingRange: NSRange , lineBreak: Int , typesetData: CTLineTypesetData ) {
3850 fragmentContext. contents. append (
3951 . init( data: . text( line: typesetData. ctLine) , width: typesetData. width)
@@ -44,6 +56,9 @@ struct TypesetContext {
4456 currentPosition = lineBreak + typesettingRange. location
4557 }
4658
59+ // MARK: - Pop Fragments
60+
61+ /// Pop the current fragment state into a new line fragment, and reset the fragment state.
4762 mutating func popCurrentData( ) {
4863 let fragment = LineFragment (
4964 documentRange: NSRange (
0 commit comments