Just learned that in compose UI 1.3.3, at least, trying to draw a text on a DrawScope off the canvas causes a crash. The latest androidx main appears to have the same code, so likely will crash as well.
Landry Norris
03/23/2023, 6:46 PM
Relevant code in TextPainter.kt:
Copy code
private fun DrawScope.textLayoutConstraints(
size: Size,
topLeft: Offset
): Constraints {
val minWidth: Int
val maxWidth: Int
val isWidthNaN = size.isUnspecified || size.width.isNaN()
if (isWidthNaN) {
minWidth = 0
maxWidth = ceil(this.size.width - topLeft.x).roundToInt()
} else {
val fixedWidth = ceil(size.width).roundToInt()
minWidth = fixedWidth
maxWidth = fixedWidth
}
val minHeight: Int
val maxHeight: Int
val isHeightNaN = size.isUnspecified || size.height.isNaN()
if (isHeightNaN) {
minHeight = 0
maxHeight = ceil(this.size.height - topLeft.y).roundToInt()
} else {
val fixedHeight = ceil(size.height).roundToInt()
minHeight = fixedHeight
maxHeight = fixedHeight
}
return Constraints(minWidth, maxWidth, minHeight, maxHeight)
}
Landry Norris
03/23/2023, 6:47 PM
As you can see, if topLeft.x > this.size.width, maxWidth will be negative, while minWidth is 0. When it creates the Constraint, an assert fails.
Copy code
require(maxWidth >= minWidth) {
"maxWidth($maxWidth) must be >= than minWidth($minWidth)"
}