# compose

Landry Norris

03/23/2023, 6:45 PM
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.
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)
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)"
Confirmed that the crash does not happen if I wrap the drawText call in a check that topLeft is within the bounds of the canvas.


03/23/2023, 7:20 PM
It's to make you happy, not me
Slack is for conversation, not reporting obvious bugs
They can and will get lost here. But not there!