diff --git a/CHANGELOG.md b/CHANGELOG.md index cb5a10c..4454310 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [Unreleased] + +- Fixed mirrored text drawing its letters unflipped + ## [3.1.2] - Item icons on 1.21.8 and newer are drawn by vanilla's own GUI item renderer, so animated and enchanted items, oversized models such as tridents, and resource pack item models look the same as in vanilla menus diff --git a/core/src/main/kotlin/org/polyfrost/compose/render/PolyTessellator.kt b/core/src/main/kotlin/org/polyfrost/compose/render/PolyTessellator.kt index 0d7e434..66ec2d8 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/render/PolyTessellator.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/render/PolyTessellator.kt @@ -21,6 +21,7 @@ import kotlin.math.roundToInt import kotlin.math.sin import kotlin.math.sqrt import kotlin.math.tan +import kotlin.math.withSign public abstract class PolyTessellator : PolyRenderer { // explicit API mode demands visibility on members of these protected classes, yet the @@ -1219,8 +1220,8 @@ public abstract class PolyTessellator : PolyRenderer { bucket = snapped } } - val kx = k * ax / big - val ky = k * ay / big + val kx = (k * ax / big).withSign(state.sx) + val ky = (k * ay / big).withSign(state.sy) runFace = face runBucket = bucket runOutline = outline @@ -1572,6 +1573,9 @@ public abstract class PolyTessellator : PolyRenderer { private var shearBase = 0f private fun glyphQuad(x0: Float, y0: Float, x1: Float, y1: Float, u0: Float, v0: Float, u1: Float, v1: Float, c: Int) { + // a quad mirrored on one axis winds backwards, which culling pipelines drop + if (x1 < x0) return glyphQuad(x1, y0, x0, y1, u1, v0, u0, v1, c) + if (y1 < y0) return glyphQuad(x0, y1, x1, y0, u0, v1, u1, v0, c) if (shearX == 0f) return quadPx(x0, y0, x1, y1, u0, v0, u1, v1, c) val s0 = shearX * (shearBase - y0) val s1 = shearX * (shearBase - y1) diff --git a/core/src/test/kotlin/org/polyfrost/compose/render/PolyTessellatorTest.kt b/core/src/test/kotlin/org/polyfrost/compose/render/PolyTessellatorTest.kt index 3205212..4223cc0 100644 --- a/core/src/test/kotlin/org/polyfrost/compose/render/PolyTessellatorTest.kt +++ b/core/src/test/kotlin/org/polyfrost/compose/render/PolyTessellatorTest.kt @@ -779,6 +779,35 @@ class PolyTessellatorTest { } } + @Test + fun mirroredTextFlipsGlyphsInPlace() { + val face = object : PolyGlyphPage.Face { + override fun glyph(codepoint: Int) = 1 + override fun advance(glyph: Int) = 1f + // like stb, a non-positive pixel size rasterizes nothing + override fun raster(glyph: Int, pixelSize: Float) = if (pixelSize > 0f) PolyGlyphPage.Raster(0, -4, 2, 4, ByteArray(8) { -1 }) else null + } + val old = PolyGlyphPage.rasterizer + PolyGlyphPage.rasterizer = object : PolyGlyphPage.Rasterizer { override fun face(name: String) = face } + try { + for (sy in listOf(1f, -1f)) { + fun bounds(draw: Recorder.() -> Unit): List { + val b = Recorder().run { scale(-1f, sy); draw(); done() }.single() + assertWinding(b) + val xs = (0 until 4).map { b.verts[it * 4] } + val ys = (0 until 4).map { b.verts[it * 4 + 1] } + assertTrue(b.verts[xs.indexOf(xs.max()) * 4 + 2] < b.verts[xs.indexOf(xs.min()) * 4 + 2], "texture not mirrored at scale -1, $sy") + return listOf(xs.min(), ys.min(), xs.max(), ys.max()) + } + val top = if (sy > 0f) 16f else -20f + assertEquals(listOf(-12f, top, -10f, top + 4f), bounds { text("a", 10f, 20f, -1, PolyFont("mirror", 4f)) }) + assertEquals(listOf(-13f, top - 1f, -9f, top + 5f), bounds { textOutline("a", 10f, 20f, -1, PolyFont("mirror", 4f), 1f) }) + } + } finally { + PolyGlyphPage.rasterizer = old + } + } + @Test fun mcOutlineCoversEachPixelOnce() { val face = object : PolyGlyphPage.Face {