diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/font/types/unicode/legacy/LegacyUnicodeFontTypeTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/font/types/unicode/legacy/LegacyUnicodeFontTypeTest.kt new file mode 100644 index 000000000..b2e1feb87 --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/font/types/unicode/legacy/LegacyUnicodeFontTypeTest.kt @@ -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::class.java, InputStream::class.java) + + init { + LOAD_PAGE.isAccessible = true + } + + private fun createTexture(): DummyTexture { + return DummyTexture(minosoft("test")) + } + + private fun load(sizes: ByteArray): Array { + val array = ByteArray(256) + System.arraycopy(sizes, 0, array, 0, sizes.size) + + val sizes = ByteArrayInputStream(array) + val texture = createTexture() + val chars: Array = 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) + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/types/unicode/legacy/LegacyUnicodeFontType.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/types/unicode/legacy/LegacyUnicodeFontType.kt index 54444f34c..db49cd104 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/font/types/unicode/legacy/LegacyUnicodeFontType.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/types/unicode/legacy/LegacyUnicodeFontType.kt @@ -47,9 +47,9 @@ class LegacyUnicodeFontType( companion object : FontTypeFactory { 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, )