Skip to content

Commit

Permalink
PM-14044: Fix line-breaking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
david-livefront committed Nov 1, 2024
1 parent 2f6578f commit 1579b98
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlin.math.floor
* This character takes up no space but can be used to ensure a string is not empty. It can also
* be used to insert "safe" line-break positions in a string.
*
* Note: Is a string only contains this charactor, it is _not_ considered blank.
* Note: Is a string only contains this character, it is _not_ considered blank.
*/
const val ZERO_WIDTH_CHARACTER: String = "\u200B"

Expand Down Expand Up @@ -126,14 +126,15 @@ fun String.withLineBreaksAtWidth(
): String {
val measurer = rememberTextMeasurer()
return remember(this, widthPx, monospacedTextStyle) {
val characterSizePx = measurer
.measure("*", monospacedTextStyle)
.size
.width
val perLineCharacterLimit = floor(widthPx / characterSizePx).toInt()
if (widthPx > 0) {
val stringLengthPx = measurer
.measure(text = this, softWrap = false, style = monospacedTextStyle)
.size
.width
val linesRequired = stringLengthPx / widthPx
val charsPerLine = floor(this.length / linesRequired).toInt()
this
.chunked(perLineCharacterLimit)
.chunked(size = charsPerLine)
.joinToString(separator = "\n")
} else {
this
Expand Down

0 comments on commit 1579b98

Please sign in to comment.