legacy unicode font rendering + tests

This commit is contained in:
Bixilon 2023-06-09 17:47:05 +02:00
parent 9507d630d6
commit b5f443d442
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 98 additions and 9 deletions

View File

@ -0,0 +1,89 @@
package de.bixilon.minosoft.gui.rendering.font.types.unicode.legacy
import de.bixilon.kotlinglm.vec2.Vec2
import de.bixilon.minosoft.data.registries.identified.Namespaces.minosoft
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
import de.bixilon.minosoft.gui.rendering.system.dummy.texture.DummyTexture
import org.testng.Assert
import org.testng.Assert.assertEquals
import org.testng.annotations.Test
import java.io.ByteArrayInputStream
import java.io.InputStream
import kotlin.reflect.full.companionObject
@Test(groups = ["font"])
class LegacyUnicodeFontTypeTest {
private val LOAD_PAGE = LegacyUnicodeFontType::class.companionObject!!.java.getDeclaredMethod("loadPage", Int::class.java, AbstractTexture::class.java, Array<LegacyUnicodeCodeRenderer>::class.java, InputStream::class.java)
init {
LOAD_PAGE.isAccessible = true
}
private fun createTexture(): DummyTexture {
return DummyTexture(minosoft("test"))
}
private fun load(sizes: ByteArray): Array<LegacyUnicodeCodeRenderer?> {
val array = ByteArray(256)
System.arraycopy(sizes, 0, array, 0, sizes.size)
val sizes = ByteArrayInputStream(array)
val texture = createTexture()
val chars: Array<LegacyUnicodeCodeRenderer?> = arrayOfNulls(256)
LOAD_PAGE.invoke(LegacyUnicodeFontType, 0, texture, chars, sizes)
return chars
}
fun basicLoading() {
val chars = load(byteArrayOf())
assertEquals(chars.size, 256)
for (entry in chars) {
Assert.assertNotNull(entry)
}
}
fun firstChar() {
val sizes = byteArrayOf(0x19, 0x12, 0x45)
val chars = load(sizes)
val char = chars[0]!!
assertEquals(char.uvStart, Vec2(0, 0))
assertEquals(char.uvEnd, Vec2(0.0390625f, 0.0625f))
assertEquals(char.width, 5.0f)
}
fun emptyChar() {
val sizes = byteArrayOf(0x00)
val chars = load(sizes)
val char = chars[0]!!
assertEquals(char.uvStart, Vec2(0, 0))
assertEquals(char.uvEnd, Vec2(0.00390625, 0.0625))
assertEquals(char.width, 0.5f)
}
fun secondChar() {
val sizes = byteArrayOf(0x00, 0x28)
val chars = load(sizes)
val char = chars[1]!!
assertEquals(char.uvStart, Vec2(0.06640625, 0))
assertEquals(char.uvEnd, Vec2(0.09765625, 0.0625f))
assertEquals(char.width, 4.0f)
}
fun `19th char`() {
val sizes = ByteArray(19)
sizes[18] = 0x9E.toByte()
val chars = load(sizes)
val char = chars[18]!!
assertEquals(char.uvStart, Vec2(0.15625, 0.0625))
assertEquals(char.uvEnd, Vec2(0.18359375, 0.125))
assertEquals(char.width, 3.5f)
}
}

View File

@ -47,9 +47,9 @@ class LegacyUnicodeFontType(
companion object : FontTypeFactory<LegacyUnicodeFontType> {
override val identifier = minecraft("legacy_unicode")
private const val UNICODE_PAGES = 0xFF
private const val PAGE_SIZE = 0xFF
private const val CHAR_ROW = 0x0F
private const val CHAR_SIZE = 0x0F
private const val PAGE_SIZE = 256
private const val CHAR_ROW = 16
private const val CHAR_SIZE = 16
private const val PIXEL = 1.0f / (CHAR_SIZE * CHAR_ROW)
private const val WIDTH_SCALE = FontProperties.CHAR_BASE_HEIGHT / CHAR_SIZE.toFloat()
@ -93,21 +93,21 @@ class LegacyUnicodeFontType(
for (x in 0 until CHAR_ROW) {
val widthByte = sizes.read()
if (widthByte < 0) throw IllegalStateException("Unexpected end of sizes stream!")
if (widthByte < 0) throw IllegalStateException("Unexpected end of sizes stream (pageId=$pageId, x=$x, y=$y)!")
val xStart = ((widthByte shr 4) and 0x0F) - 1
val width = ((widthByte and 0x0F) + 1) - xStart
val xStart = maxOf(0, ((widthByte shr 4) and 0x0F) - 1)
val width = (widthByte and 0x0F) + 1 - xStart
val xOffset = (CHAR_SIZE * x) + xStart
val xOffset = ((CHAR_SIZE * x) + xStart) * PIXEL
val uvStart = Vec2(
x = xOffset * PIXEL,
x = xOffset,
y = yStart,
)
val uvEnd = Vec2(
x = xOffset + width * PIXEL,
x = xOffset + (width * PIXEL),
y = yEnd,
)