bitmap font type: workaround precision loss

This commit is contained in:
Bixilon 2023-06-17 20:54:37 +02:00
parent ddc6ba0de4
commit 483dbf0921
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 20 additions and 4 deletions

View File

@ -1,3 +1,16 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.rendering.font.types.bitmap package de.bixilon.minosoft.gui.rendering.font.types.bitmap
import de.bixilon.kotlinglm.vec2.Vec2 import de.bixilon.kotlinglm.vec2.Vec2
@ -90,11 +103,11 @@ class BitmapFontTypeTest {
fun `multiple rows`() { fun `multiple rows`() {
val font = load(IntArray(64) { it % 3 }, IntArray(64) { (it + 2) % 4 }, chars = arrayOf(IntArray(16) { 'A'.code + it }, IntArray(16) { 'A'.code + 16 + it }, IntArray(16) { 'A'.code + 32 + it }, IntArray(16) { 'A'.code + 48 + it })) val font = load(IntArray(64) { it % 3 }, IntArray(64) { (it + 2) % 4 }, chars = arrayOf(IntArray(16) { 'A'.code + it }, IntArray(16) { 'A'.code + 16 + it }, IntArray(16) { 'A'.code + 32 + it }, IntArray(16) { 'A'.code + 48 + it }))
font.assert('A', 3.0f, Vec2(0.0f, 0), Vec2(0.0234375f, 0.25f)) font.assert('A', 3.0f, Vec2(0.0f, 0), Vec2(0.0234375f, 0.249f))
font.assert('P', 2.0f, Vec2(0.9365f, 0.0), Vec2(0.953125f, 0.25f)) font.assert('P', 2.0f, Vec2(0.9365f, 0.0), Vec2(0.953125f, 0.249f))
font.assert('Q', 2.0f, Vec2(0.0068125f, 0.25f), Vec2(0.015625f, 0.5f)) font.assert('Q', 2.0f, Vec2(0.0068125f, 0.25f), Vec2(0.015625f, 0.499f))
font.assert('a', 1.0f, Vec2(0.014625f, 0.5f), Vec2(0.0078125f, 0.75f)) font.assert('a', 1.0f, Vec2(0.014625f, 0.5f), Vec2(0.0078125f, 0.749f))
font.assert('q', 3.0f, Vec2(0.0f, 0.75f), Vec2(0.0234375f, 1.0f)) font.assert('q', 3.0f, Vec2(0.0f, 0.75f), Vec2(0.0234375f, 1.0f))
} }
} }

View File

@ -110,6 +110,9 @@ class BitmapFontType(
val uvEnd = Vec2(offset) val uvEnd = Vec2(offset)
uvEnd.x += width * pixel.x uvEnd.x += width * pixel.x
uvEnd.y += height * pixel.y uvEnd.y += height * pixel.y
if (uvEnd.y > RenderConstants.UV_ADD && uvEnd.y < 1.0f) {
uvEnd.y -= RenderConstants.UV_ADD // this workarounds some precision loss
}
val scaledWidth = width / (height.toFloat() / FontProperties.CHAR_BASE_HEIGHT) val scaledWidth = width / (height.toFloat() / FontProperties.CHAR_BASE_HEIGHT)