diff --git a/Sources/Glyph/NSTextLayoutFragment+Additions.swift b/Sources/Glyph/NSTextLayoutFragment+Additions.swift index 86a5a79..bdbf3fa 100644 --- a/Sources/Glyph/NSTextLayoutFragment+Additions.swift +++ b/Sources/Glyph/NSTextLayoutFragment+Additions.swift @@ -35,15 +35,10 @@ extension NSTextLineFragment { for index in (start.. point.x { + if span.upperBound >= point.x { end = min(index + 1, length) break } - - if span.upperBound == point.x { - end = index - break - } } guard let end else { return nil } @@ -85,16 +80,24 @@ extension NSTextLayoutFragment { precondition(location >= 0) precondition(location != NSNotFound) + var locationOffset = location + for textLineFragment in textLineFragments { + // we have to shift to compute overlap, and then shift back to compute the span let bounds = textLineFragment.typographicBounds.offsetBy(dx: origin.x, dy: origin.y) - - let overlap = bounds.intersection(rect) + let overlap = bounds.intersection(rect).offsetBy(dx: -origin.x, dy: -origin.y) let span: Range = overlap.minX..