text renderer: fix crash when text starts with newline + tests

This commit is contained in:
Bixilon 2023-06-24 01:06:17 +02:00
parent 60cda1237e
commit 51d4a31e6c
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 22 additions and 2 deletions

View File

@ -444,5 +444,25 @@ class ChatComponentRendererTest {
) )
} }
fun `first line blank`() {
val text = TextComponent("\nbcd")
val info = render(text)
info.assert(lineIndex = 1, size = Vec2(5.0f, 22.0f), lines = listOf(
LineRenderInfo(BaseComponent(), 0.0f),
LineRenderInfo(BaseComponent(TextComponent("bcd")), 5.0f),
))
}
fun `first component blank`() {
val text = BaseComponent(TextComponent("\n"), TextComponent("bcd"))
val info = render(text)
info.assert(lineIndex = 1, size = Vec2(5.0f, 22.0f), lines = listOf(
LineRenderInfo(BaseComponent(), 0.0f),
LineRenderInfo(BaseComponent(TextComponent("bcd")), 5.0f),
))
}
// TODO: shadow, formatting (just basic, that is code point renderer's job) // TODO: shadow, formatting (just basic, that is code point renderer's job)
} }

View File

@ -30,11 +30,11 @@ class TextRenderInfo(
size.x = maxOf(offset.offset.x - offset.initial.x + width, size.x) size.x = maxOf(offset.offset.x - offset.initial.x + width, size.x)
val line: LineRenderInfo val line: LineRenderInfo
if (lineIndex == 0 && lines.isEmpty()) { if ((lineIndex == 0 && lines.isEmpty()) || lineIndex >= lines.size) {
// first char of all lines // first char of all lines
line = LineRenderInfo() line = LineRenderInfo()
lines += line lines += line
size.y = properties.lineHeight size.y += properties.lineHeight
} else { } else {
line = lines[lineIndex] line = lines[lineIndex]
} }