Skip to content

Commit

Permalink
Use binary search in SkiaParagraph.lineMetricsForOffset (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sasha authored Dec 11, 2023
1 parent 5b153cc commit 976f6fe
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,16 @@ internal class SkiaParagraph(
private fun lineMetricsForOffset(offset: Int): LineMetrics? {
checkOffsetIsValid(offset)
val metrics = lineMetrics
for (line in metrics) {
if (offset < line.endIncludingNewline) {
return line
}
}
if (metrics.isEmpty()) {
return null
}
return metrics.last()

val index = metrics.asList().binarySearch {
if (offset >= it.endIncludingNewline) -1 else 1
}

// The search will always return a negative value because the comparison never returns 0
return metrics[(-index - 1).coerceAtMost(metrics.lastIndex)]
}

override fun getLineHeight(lineIndex: Int) =
Expand Down

0 comments on commit 976f6fe

Please sign in to comment.