Skip to content

Commit 5836a2e

Browse files
committed
Fixes text + setTextBounds with bitmap font
1 parent 76cc17c commit 5836a2e

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

korge-sandbox/src/Main.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ suspend fun main() = Korge(
133133
//Demo(::MainMasks),
134134
//Demo(::MainShape2dScene),
135135
//Demo(::MainUIStacks),
136-
Demo(::MainAudioScene),
136+
Demo(::MainTextBounds2),
137+
//Demo(::MainAudioScene),
137138
//Demo(::MainAsteroids),
138139
//Demo(::MainSprites10k),
139140
//Demo(::MainStressMatrixMultiplication),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package samples
2+
3+
import korlibs.image.color.*
4+
import korlibs.image.font.*
5+
import korlibs.image.text.*
6+
import korlibs.korge.scene.*
7+
import korlibs.korge.view.*
8+
import korlibs.math.geom.*
9+
10+
class MainTextBounds2 : Scene() {
11+
override suspend fun SContainer.sceneMain() {
12+
for ((tbindex, withTextBounds) in listOf(true, false).withIndex()) {
13+
for ((tindex, text) in listOf("hello world", "hello\nworld").withIndex()) {
14+
for ((findex, font) in listOf(DefaultTtfFontAsBitmap, DefaultTtfFont).withIndex()) {
15+
for ((hindex, horizontal) in listOf(HorizontalAlign.LEFT, HorizontalAlign.CENTER, HorizontalAlign.RIGHT, HorizontalAlign.JUSTIFY).withIndex()) {
16+
for ((vindex, vertical) in listOf(VerticalAlign.TOP, VerticalAlign.CENTER, VerticalAlign.BOTTOM, VerticalAlign.BASELINE).withIndex()) {
17+
val textColor = if (findex == 0) Colors.RED else Colors.GREEN
18+
container {
19+
val W = 70
20+
val H = 60
21+
val PAD = 6
22+
position(
23+
10 + (W + PAD) * (hindex) + ((findex + (tindex * 2.1)) * ((W + PAD + 2) * 4)),
24+
10 + (H + PAD) * vindex + (tbindex * ((H + PAD + 4) * 4))
25+
)
26+
if (withTextBounds) {
27+
solidRect(Size(W, H), Colors.DIMGREY)
28+
text(text, alignment = TextAlignment(horizontal, vertical), color = textColor, font = font, textSize = 10) {
29+
this.setTextBounds(Rectangle(0, 0, W, H))
30+
}
31+
} else {
32+
line(Point(-4, 0), Point(+4, 0), Colors.DIMGREY)
33+
line(Point(0, -4), Point(0, +4), Colors.DIMGREY)
34+
text(text, alignment = TextAlignment(horizontal, vertical), color = textColor, font = font, textSize = 10)
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}

korge/src/korlibs/korge/view/Text.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,25 @@ open class Text(
309309
//println("lineInfos=$lineInfos")
310310
//println("Text.BitmapFont: bounds=$bounds, firstBounds=$firstBounds, textWidth=$textWidth, textHeight=$textHeight, verticalAlign=$verticalAlign")
311311

312-
//val dx = (-_textBounds.width - textWidth) * horizontalAlign.ratio
313-
val dx = _textBounds.x
314-
val dy: Double = when (verticalAlign) {
315-
VerticalAlign.BASELINE -> 0.0
316-
VerticalAlign.TOP -> firstLineInfos.maxTop
317-
else -> firstLineInfos.maxTop - (totalHeight) * verticalAlign.ratioFake
312+
val dx = when {
313+
autoSize -> _textBounds.x
314+
else -> _textBounds.x + 1 + ((_textBounds.width - textWidth) * horizontalAlign.ratio)
318315
}
316+
val dy: Double = when {
317+
autoSize -> {
318+
_textBounds.y + firstLineInfos.maxTop
319+
}
320+
else -> {
321+
when (verticalAlign) {
322+
VerticalAlign.BASELINE -> _textBounds.y
323+
else -> (_textBounds.y + firstLineInfos.maxTop) + ((_textBounds.height - totalHeight) * verticalAlign.ratioFake)
324+
}
325+
}
326+
327+
}
328+
329+
330+
//println("dx=$dx, dy=$dy, totalHeight=$totalHeight, textHeight=$textHeight, textWidth=$textWidth, _textBounds=$_textBounds")
319331

320332
val program = font.agProgram
321333
//val program = null

0 commit comments

Comments
 (0)