Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion korge-sandbox/src/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ suspend fun main() = Korge(
//Demo(::MainMasks),
//Demo(::MainShape2dScene),
//Demo(::MainUIStacks),
Demo(::MainAudioScene),
Demo(::MainTextBounds2),
//Demo(::MainAudioScene),
//Demo(::MainAsteroids),
//Demo(::MainSprites10k),
//Demo(::MainStressMatrixMultiplication),
Expand Down
43 changes: 43 additions & 0 deletions korge-sandbox/src/samples/MainTextBounds2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package samples

import korlibs.image.color.*
import korlibs.image.font.*
import korlibs.image.text.*
import korlibs.korge.scene.*
import korlibs.korge.view.*
import korlibs.math.geom.*

class MainTextBounds2 : Scene() {
override suspend fun SContainer.sceneMain() {
for ((tbindex, withTextBounds) in listOf(true, false).withIndex()) {
for ((tindex, text) in listOf("hello world", "hello\nworld").withIndex()) {
for ((findex, font) in listOf(DefaultTtfFontAsBitmap, DefaultTtfFont).withIndex()) {
for ((hindex, horizontal) in listOf(HorizontalAlign.LEFT, HorizontalAlign.CENTER, HorizontalAlign.RIGHT, HorizontalAlign.JUSTIFY).withIndex()) {
for ((vindex, vertical) in listOf(VerticalAlign.TOP, VerticalAlign.CENTER, VerticalAlign.BOTTOM, VerticalAlign.BASELINE).withIndex()) {
val textColor = if (findex == 0) Colors.RED else Colors.GREEN
container {
val W = 70
val H = 60
val PAD = 6
position(
10 + (W + PAD) * (hindex) + ((findex + (tindex * 2.1)) * ((W + PAD + 2) * 4)),
10 + (H + PAD) * vindex + (tbindex * ((H + PAD + 4) * 4))
)
if (withTextBounds) {
solidRect(Size(W, H), Colors.DIMGREY)
text(text, alignment = TextAlignment(horizontal, vertical), color = textColor, font = font, textSize = 10) {
this.setTextBounds(Rectangle(0, 0, W, H))
}
} else {
line(Point(-4, 0), Point(+4, 0), Colors.DIMGREY)
line(Point(0, -4), Point(0, +4), Colors.DIMGREY)
text(text, alignment = TextAlignment(horizontal, vertical), color = textColor, font = font, textSize = 10)
}
}
}
}
}
}
}
}
}
24 changes: 18 additions & 6 deletions korge/src/korlibs/korge/view/Text.kt
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,25 @@ open class Text(
//println("lineInfos=$lineInfos")
//println("Text.BitmapFont: bounds=$bounds, firstBounds=$firstBounds, textWidth=$textWidth, textHeight=$textHeight, verticalAlign=$verticalAlign")

//val dx = (-_textBounds.width - textWidth) * horizontalAlign.ratio
val dx = _textBounds.x
val dy: Double = when (verticalAlign) {
VerticalAlign.BASELINE -> 0.0
VerticalAlign.TOP -> firstLineInfos.maxTop
else -> firstLineInfos.maxTop - (totalHeight) * verticalAlign.ratioFake
val dx = when {
autoSize -> _textBounds.x
else -> _textBounds.x + 1 + ((_textBounds.width - textWidth) * horizontalAlign.ratio)
}
val dy: Double = when {
autoSize -> {
_textBounds.y + firstLineInfos.maxTop
}
else -> {
when (verticalAlign) {
VerticalAlign.BASELINE -> _textBounds.y
else -> (_textBounds.y + firstLineInfos.maxTop) + ((_textBounds.height - totalHeight) * verticalAlign.ratioFake)
}
}

}


//println("dx=$dx, dy=$dy, totalHeight=$totalHeight, textHeight=$textHeight, textWidth=$textWidth, _textBounds=$_textBounds")

val program = font.agProgram
//val program = null
Expand Down
Loading