Skip to content

Commit c0071d5

Browse files
improve extra line fragment handling with TK2
1 parent 25ffb29 commit c0071d5

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

Sources/Glyph/NSTextContainer+Additions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension NSTextContainer {
1111
/// - Parameter strictIntersection: If true, the result will only be rect and range strictly within the `rect` parameter. This is more expensive to compute.
1212
public func enumerateLineFragments(for rect: CGRect, strictIntersection: Bool, block: (CGRect, NSRange, inout Bool) -> Void) {
1313
if #available(macOS 12.0, iOS 15.0, *), let textLayoutManager {
14-
textLayoutManager.enumerateLineFragments(for: rect, strictIntersection: strictIntersection, block: block)
14+
textLayoutManager.enumerateLineFragments(for: rect, strictIntersection: strictIntersection, options: [.ensuresExtraLineFragment], block: block)
1515

1616
return
1717
}

Sources/Glyph/NSTextLayoutFragment+Additions.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import UIKit
99
extension NSTextLineFragment {
1010
/// span has to be within this fragment's coordinate system
1111
func rangeOfCharacters(intersecting span: Range<CGFloat>) -> NSRange? {
12-
let length = characterRange.length
12+
// even an empty fragment will respond to locationForCharacter(at: 0)
13+
14+
let length = max(characterRange.length, 1)
1315

1416
var start: Int?
1517

@@ -29,6 +31,11 @@ extension NSTextLineFragment {
2931
}
3032

3133
guard let start else { return nil }
34+
35+
// continuing to look here for an empty fragment doens't make sense
36+
if characterRange.length == 0 {
37+
return NSRange(start..<start)
38+
}
3239

3340
var end: Int?
3441

Sources/Glyph/NSTextLayoutManager+Additions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ extension NSTextLayoutManager {
211211
public func boundingRect(for range: NSRange) -> CGRect? {
212212
var rect: CGRect? = nil
213213

214-
enumerateTextLineFragments(in: range, options: [.ensuresLayout]) { fragment, lineFragment, lineRect, lineRange, offset in
214+
enumerateTextLineFragments(in: range, options: [.ensuresLayout, .ensuresExtraLineFragment]) { fragment, lineFragment, lineRect, lineRange, offset in
215215
// we need to limit the check to what overlaps `range`
216216
let startIndex = max(range.lowerBound, lineRange.lowerBound) - lineRange.lowerBound
217217
let endIndex = min(range.upperBound, lineRange.upperBound) - lineRange.lowerBound

0 commit comments

Comments
 (0)