mirror of
https://github.com/curioustorvald/Terrarum-sans-bitmap.git
synced 2026-06-10 07:54:05 +09:00
scale support for MovableType
This commit is contained in:
@@ -27,7 +27,9 @@ class MovableType(
|
|||||||
var height = 0; private set
|
var height = 0; private set
|
||||||
internal val hash: Long = inputText.getHash()
|
internal val hash: Long = inputText.getHash()
|
||||||
private var disposed = false
|
private var disposed = false
|
||||||
private val typesettedSlugs = ArrayList<List<Block>>()
|
val typesettedSlugs = ArrayList<List<Block>>()
|
||||||
|
|
||||||
|
var width = 0; private set
|
||||||
|
|
||||||
constructor(font: TerrarumSansBitmap, string: String, paperWidth: Int) : this(font, font.normaliseStringForMovableType(string), paperWidth)
|
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)
|
val nextPosX = (slug.lastOrNull()?.getEndPos() ?: 0)
|
||||||
slug.add(Block(nextPosX, box))
|
slug.add(Block(nextPosX, box))
|
||||||
slugWidth += box.width
|
slugWidth += box.width
|
||||||
|
|
||||||
|
width = maxOf(width, nextPosX + box.width)
|
||||||
}
|
}
|
||||||
fun dispatchSlug() {
|
fun dispatchSlug() {
|
||||||
typesettedSlugs.add(slug)
|
typesettedSlugs.add(slug)
|
||||||
@@ -153,6 +157,9 @@ class MovableType(
|
|||||||
|
|
||||||
// if adding the box would cause overflow
|
// if adding the box would cause overflow
|
||||||
if (slugWidthForOverflowCalc + box.width > paperWidth) {
|
if (slugWidthForOverflowCalc + box.width > paperWidth) {
|
||||||
|
// text overflow occured; set the width to the max value
|
||||||
|
width = paperWidth
|
||||||
|
|
||||||
// badness: always positive and weighted
|
// badness: always positive and weighted
|
||||||
// widthDelta: can be positive or negative
|
// widthDelta: can be positive or negative
|
||||||
val (badnessW, widthDeltaW) = getBadnessW(box) // widthDeltaW is always positive
|
val (badnessW, widthDeltaW) = getBadnessW(box) // widthDeltaW is always positive
|
||||||
@@ -268,14 +275,22 @@ class MovableType(
|
|||||||
drawJobs[absoluteLineNum]?.invoke(x, y + lineNum * lineHeight, absoluteLineNum)
|
drawJobs[absoluteLineNum]?.invoke(x, y + lineNum * lineHeight, absoluteLineNum)
|
||||||
|
|
||||||
lineBlocks.forEach {
|
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)
|
// 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
|
fun getEndPos() = this.posX + this.block.width
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -332,9 +332,6 @@ class TerrarumSansBitmap(
|
|||||||
|
|
||||||
private var nullProp = GlyphProps(15)
|
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())
|
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? {
|
override fun draw(batch: Batch, charSeq: CharSequence, x: Float, y: Float): GlyphLayout? {
|
||||||
// charSeq.forEach { dbgprn("${it.toInt().charInfo()} ${glyphProps[it.toInt()]}") }
|
// charSeq.forEach { dbgprn("${it.toInt().charInfo()} ${glyphProps[it.toInt()]}") }
|
||||||
@@ -372,9 +369,12 @@ class TerrarumSansBitmap(
|
|||||||
textBuffer = cacheObj.glyphLayout!!.textBuffer
|
textBuffer = cacheObj.glyphLayout!!.textBuffer
|
||||||
tempLinotype = cacheObj.glyphLayout!!.linotype
|
tempLinotype = cacheObj.glyphLayout!!.linotype
|
||||||
|
|
||||||
|
val linotypeScaleOffsetX = -linotypePaddingX * (scale - 1)
|
||||||
|
val linotypeScaleOffsetY = -linotypePaddingY * (scale - 1) * (if (flipY) -1 else 1)
|
||||||
|
|
||||||
batch.draw(tempLinotype,
|
batch.draw(tempLinotype,
|
||||||
(x - linotypePaddingX).toFloat(),
|
(x - linotypePaddingX).toFloat() + linotypeScaleOffsetX,
|
||||||
(y - linotypePaddingY).toFloat() + (if (flipY) (tempLinotype.height) else 0) * scale,
|
(y - linotypePaddingY).toFloat() + linotypeScaleOffsetY + (if (flipY) (tempLinotype.height) else 0) * scale,
|
||||||
tempLinotype.width.toFloat() * scale,
|
tempLinotype.width.toFloat() * scale,
|
||||||
(tempLinotype.height.toFloat()) * (if (flipY) -1 else 1) * scale
|
(tempLinotype.height.toFloat()) * (if (flipY) -1 else 1) * scale
|
||||||
)
|
)
|
||||||
@@ -2042,6 +2042,9 @@ class TerrarumSansBitmap(
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
|
const internal val linotypePaddingX = 16
|
||||||
|
const internal val linotypePaddingY = 10
|
||||||
|
|
||||||
const val LINE_HEIGHT = 24
|
const val LINE_HEIGHT = 24
|
||||||
|
|
||||||
fun CodepointSequence.getHash(): Long {
|
fun CodepointSequence.getHash(): Long {
|
||||||
|
|||||||
Reference in New Issue
Block a user