bj0
09/29/2025, 7:06 PM.size() on the canvas and use clipping so that the drawing functions don't have to care about the size of the canvas, they just draw their full content as normal.
This is working fine for shapes (rectangles, lines), but when I try to drawText, as soon as it gets too far outside the limits of the canvas size, it throws an IllegalArgumentException: maxWidth must be >= than minWidth, maxHeight must be >= 0. Im not sure why it does this, is there a workaround so that I can still avoid having a bunch of size checking logic in the drawing functions?romainguy
09/29/2025, 7:19 PMCanvas be just the size it needs to be on screen. The rest is handled with translation/scaling of the drawing operationsbj0
09/29/2025, 7:25 PMromainguy
09/29/2025, 7:25 PMbj0
09/29/2025, 7:26 PMromainguy
09/29/2025, 7:30 PMbj0
09/29/2025, 7:33 PMval measurer = rememberTextMeasurer()
// need to manually calc the canvas height
val density = LocalDensity.current
val canvasHeight = remember(density) {
measureHeight(
systems = systems,
measurer = measurer,
context = context,
density = density
).coerceAtMost(max)
}
Spacer(
modifier = Modifier
.padding(10.dp)
.border(width = 1.dp, color = Color.DarkGray)
.height(canvasHeight)
.fillMaxWidth()
.drawWithCache {
onDrawBehind {
//... drawing functions
}
}bj0
09/29/2025, 7:34 PMdrawText(
textMeasurer = measurer,
text = "name",
topLeft = Offset(x = 50f, y = 540f),
style = context.labelStyle,
softWrap = false,
overflow = TextOverflow.Visible
)bj0
09/29/2025, 7:35 PMjava.lang.IllegalArgumentException: maxWidth must be >= than minWidth,
maxHeight must be >= than minHeight,
minWidth and minHeight must be >= 0
at androidx.compose.ui.unit.InlineClassHelperKt.throwIllegalArgumentException(InlineClassHelper.kt:30)
at androidx.compose.ui.unit.ConstraintsKt.Constraints(Constraints.kt:638)
at androidx.compose.ui.text.TextPainterKt.textLayoutConstraints-v_w8tDc(TextPainter.kt:387)
at androidx.compose.ui.text.TextPainterKt.drawText-TPWCCtM(TextPainter.kt:226)
at androidx.compose.ui.text.TextPainterKt.drawText-TPWCCtM$default(TextPainter.kt:208)romainguy
09/29/2025, 7:41 PMmax when you compute canvasHeight (also, what does measureHeight do?)romainguy
09/29/2025, 7:49 PMdrawText I don't see what could cause this exception to happen unless there's something really weird about the size of the Spacer in your examplebj0
09/29/2025, 7:49 PMmax is 200.dp, measureHeight estimates how high the actual drawing will be (in dp), so that i can set the size of the canvas to shorter than max if it's less than 200 (shrink to fit content)bj0
09/29/2025, 7:49 PMbj0
09/29/2025, 7:49 PMromainguy
09/29/2025, 7:52 PMromainguy
09/29/2025, 7:53 PMjava.lang.Math.round(ceil(canvasHeight.toPx() - 540f)).coerceAtLeast(0)romainguy
09/29/2025, 7:53 PMromainguy
09/29/2025, 7:53 PMtoPx() if your height is already in pixels)bj0
09/29/2025, 8:01 PMcanvasHeight is 182.dp (364.0 px), so your line is printing 0romainguy
09/29/2025, 8:01 PMromainguy
09/29/2025, 8:03 PMbj0
09/29/2025, 8:03 PMromainguy
09/29/2025, 8:04 PMromainguy
09/29/2025, 8:05 PMsize is Unspecified, so it should set min/maxHeight to 0romainguy
09/29/2025, 8:05 PMConstraintsbj0
09/29/2025, 8:26 PMbj0
09/29/2025, 8:29 PMcoerceAtLeastbj0
09/29/2025, 8:30 PMif (isHeightNaN) {
minHeight = 0
maxHeight = ceil(this.size.height - topLeft.y).fastRoundToInt()
} else {
val fixedHeight = ceil(size.height).fastRoundToInt()
minHeight = fixedHeight
maxHeight = fixedHeight
}romainguy
09/29/2025, 8:30 PMromainguy
09/29/2025, 8:30 PMromainguy
09/29/2025, 8:31 PMbj0
09/29/2025, 8:31 PMcompose-bom = "2025.09.01"romainguy
09/29/2025, 8:31 PMromainguy
09/29/2025, 8:32 PMnativeCanvas on Android and uses drawText there, which won't have this issuebj0
09/29/2025, 8:32 PMromainguy
09/29/2025, 8:33 PMnativeCanvas.drawText() which won't do wrapping/multi-lineromainguy
09/29/2025, 8:33 PMbj0
09/29/2025, 8:34 PMbj0
09/29/2025, 8:34 PMbj0
09/29/2025, 10:46 PM