Skip to content

Commit

Permalink
Last position line fragment for TK2 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Dec 16, 2024
1 parent 06690a3 commit dce014c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sources/Glyph/NSTextContainer+Additions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ extension NSTextContainer {
public func lineFragment(for index: Int, offset: Int) -> (CGRect, NSRange)? {
var fragment: (CGRect, NSRange)?
let forward = offset >= 0
var targetCount = abs(offset)
var count = abs(offset)

enumerateLineFragments(from: index, forward: forward) { rect, range, stop in
if targetCount == 0 {
if count <= 0 {
fragment = (rect, range)
stop = true
return
}

targetCount -= 1
count -= 1
}

return fragment
Expand Down
28 changes: 28 additions & 0 deletions Sources/Glyph/NSTextLayoutManager+Additions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ extension NSTextLayoutManager {
})
}

// the last index in the storage might not return a fragment, and the logic required
// to figure out which one should be in effect is atually quite complex
private func lastTextLayoutFragment() -> NSTextLayoutFragment? {
guard let textContentManager else { return nil }

if let fragment = textLayoutFragment(for: documentRange.endLocation) {
return fragment
}

guard let locBefore = textContentManager.location(documentRange.endLocation, offsetBy: -1) else {
return nil
}

return textLayoutFragment(for: locBefore)
}

private func enumerateTextLineFragments(
in range: NSRange,
options: NSTextLayoutFragment.EnumerationOptions = [],
Expand All @@ -84,6 +100,18 @@ extension NSTextLayoutManager {
return
}

if textContentManager.offset(from: start, to: documentRange.endLocation) == 0 {
guard let fragment = lastTextLayoutFragment() else { return }

var stop = false

fragment.enumerateLineFragments(with: textContentManager) { lineFragment, frame, elementRange in
block(fragment, lineFragment, frame, elementRange, &stop)
}

return
}

enumerateTextLayoutFragments(from: start, options: options) { fragment in
let fragmentRange = fragment.rangeInElement

Expand Down

0 comments on commit dce014c

Please sign in to comment.