From d87b0dce7c69f7d22ba4a0fbee1eadf3d2def21e Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 27 Apr 2024 16:02:22 +0900 Subject: [PATCH] scale support for MovableType --- .../torvald/terrarumsansbitmap/MovableType.kt | 21 ++++++++++++++++--- .../gdx/TerrarumSansBitmap.kt | 13 +++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/net/torvald/terrarumsansbitmap/MovableType.kt b/src/net/torvald/terrarumsansbitmap/MovableType.kt index 3ac1af8..25a913c 100644 --- a/src/net/torvald/terrarumsansbitmap/MovableType.kt +++ b/src/net/torvald/terrarumsansbitmap/MovableType.kt @@ -27,7 +27,9 @@ class MovableType( var height = 0; private set internal val hash: Long = inputText.getHash() private var disposed = false - private val typesettedSlugs = ArrayList>() + val typesettedSlugs = ArrayList>() + + var width = 0; private set constructor(font: TerrarumSansBitmap, string: String, paperWidth: Int) : this(font, font.normaliseStringForMovableType(string), paperWidth) @@ -65,6 +67,8 @@ class MovableType( val nextPosX = (slug.lastOrNull()?.getEndPos() ?: 0) slug.add(Block(nextPosX, box)) slugWidth += box.width + + width = maxOf(width, nextPosX + box.width) } fun dispatchSlug() { typesettedSlugs.add(slug) @@ -153,6 +157,9 @@ class MovableType( // if adding the box would cause overflow if (slugWidthForOverflowCalc + box.width > paperWidth) { + // text overflow occured; set the width to the max value + width = paperWidth + // badness: always positive and weighted // widthDelta: can be positive or negative val (badnessW, widthDeltaW) = getBadnessW(box) // widthDeltaW is always positive @@ -268,14 +275,22 @@ class MovableType( drawJobs[absoluteLineNum]?.invoke(x, y + lineNum * lineHeight, absoluteLineNum) lineBlocks.forEach { - batch.draw(it.block.glyphLayout!!.linotype, x + it.posX - 16, y + lineNum * lineHeight - 8) + val tex = it.block.glyphLayout!!.linotype + val linotypeScaleOffsetX = -TerrarumSansBitmap.linotypePaddingX * (font.scale - 1) + val linotypeScaleOffsetY = -TerrarumSansBitmap.linotypePaddingY * (font.scale - 1) * (if (font.flipY) -1 else 1) + batch.draw(tex, + x + it.posX * font.scale - 16 + linotypeScaleOffsetX.toFloat(), + y + lineNum * (lineHeight * font.scale) - 8 + linotypeScaleOffsetY.toFloat(), + tex.width * font.scale.toFloat(), + tex.height * font.scale.toFloat() + ) } // font.draw(batch, "I", x, y + lineNum * lineHeight + 14) } } - private data class Block(var posX: Int, val block: TextCacheObj) { // a single word + data class Block(var posX: Int, val block: TextCacheObj) { // a single word fun getEndPos() = this.posX + this.block.width } diff --git a/src/net/torvald/terrarumsansbitmap/gdx/TerrarumSansBitmap.kt b/src/net/torvald/terrarumsansbitmap/gdx/TerrarumSansBitmap.kt index 5c7d098..8dd5234 100755 --- a/src/net/torvald/terrarumsansbitmap/gdx/TerrarumSansBitmap.kt +++ b/src/net/torvald/terrarumsansbitmap/gdx/TerrarumSansBitmap.kt @@ -332,9 +332,6 @@ class TerrarumSansBitmap( private var nullProp = GlyphProps(15) - private val linotypePaddingX = 16 - private val linotypePaddingY = 10 - fun draw(batch: Batch, charSeq: CharSequence, x: Int, y: Int) = draw(batch, charSeq, x.toFloat(), y.toFloat()) override fun draw(batch: Batch, charSeq: CharSequence, x: Float, y: Float): GlyphLayout? { // charSeq.forEach { dbgprn("${it.toInt().charInfo()} ${glyphProps[it.toInt()]}") } @@ -372,9 +369,12 @@ class TerrarumSansBitmap( textBuffer = cacheObj.glyphLayout!!.textBuffer tempLinotype = cacheObj.glyphLayout!!.linotype + val linotypeScaleOffsetX = -linotypePaddingX * (scale - 1) + val linotypeScaleOffsetY = -linotypePaddingY * (scale - 1) * (if (flipY) -1 else 1) + batch.draw(tempLinotype, - (x - linotypePaddingX).toFloat(), - (y - linotypePaddingY).toFloat() + (if (flipY) (tempLinotype.height) else 0) * scale, + (x - linotypePaddingX).toFloat() + linotypeScaleOffsetX, + (y - linotypePaddingY).toFloat() + linotypeScaleOffsetY + (if (flipY) (tempLinotype.height) else 0) * scale, tempLinotype.width.toFloat() * scale, (tempLinotype.height.toFloat()) * (if (flipY) -1 else 1) * scale ) @@ -2042,6 +2042,9 @@ class TerrarumSansBitmap( companion object { + const internal val linotypePaddingX = 16 + const internal val linotypePaddingY = 10 + const val LINE_HEIGHT = 24 fun CodepointSequence.getHash(): Long {