Skip to content

Commit fc2203b

Browse files
authored
fix(motd): match precise native alignment to the shared viewer (#113)
1 parent 6b8a53d commit fc2203b

3 files changed

Lines changed: 162 additions & 8 deletions

File tree

‎velocity/src/main/kotlin/gg/grounds/proxy/velocity/motd/CompiledMotd.kt‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ class CompiledMotd(document: JsonObject, private val alignments: List<String>) {
4848
width +=
4949
(advances[char] ?: 6) +
5050
(if (
51-
char != " " &&
52-
style.decoration(TextDecoration.BOLD) ==
53-
TextDecoration.State.TRUE
51+
style.decoration(TextDecoration.BOLD) ==
52+
TextDecoration.State.TRUE
5453
)
5554
1
5655
else 0)
@@ -60,19 +59,57 @@ class CompiledMotd(document: JsonObject, private val alignments: List<String>) {
6059
measure(line, Style.empty())
6160
val remaining = (lineWidth - width).coerceAtLeast(0)
6261
val alignment = alignments.getOrNull(index) ?: "left"
63-
val spaces =
62+
val target =
6463
when (alignment) {
65-
"center" -> kotlin.math.floor(remaining / 8.0 + .5).toInt()
66-
"right" -> kotlin.math.floor(remaining / 4.0 + .5).toInt()
67-
else -> 0
64+
"center" -> remaining / 2.0
65+
"right" -> remaining.toDouble()
66+
else -> 0.0
6867
}
68+
val (regular, bold) = padding(target, remaining)
6969
if (index > 0) result = result.append(Component.newline())
70-
result = result.append(Component.text(" ".repeat(spaces))).append(line)
70+
if (regular > 0)
71+
result =
72+
result.append(
73+
Component.text(" ".repeat(regular)).decoration(TextDecoration.BOLD, false)
74+
)
75+
if (bold > 0)
76+
result =
77+
result.append(
78+
Component.text(" ".repeat(bold)).decoration(TextDecoration.BOLD, true)
79+
)
80+
result = result.append(line)
7181
}
7282
return result
7383
}
7484

7585
companion object {
86+
/** Same bounded 4/5-pixel native space search as library-motd. */
87+
private fun padding(target: Double, available: Int): Pair<Int, Int> {
88+
var regular = 0
89+
var bold = 0
90+
var advance = 0
91+
var error = kotlin.math.abs(target)
92+
for (b in 0..3) {
93+
val near = (target - b * 5) / 4
94+
for (r in listOf(kotlin.math.floor(near).toInt(), kotlin.math.ceil(near).toInt())) {
95+
val candidate = r * 4 + b * 5
96+
if (r < 0 || candidate > available) continue
97+
val distance = kotlin.math.abs(candidate - target)
98+
if (
99+
distance < error ||
100+
(distance == error &&
101+
(candidate > advance || (candidate == advance && b < bold)))
102+
) {
103+
regular = r
104+
bold = b
105+
advance = candidate
106+
error = distance
107+
}
108+
}
109+
}
110+
return regular to bold
111+
}
112+
76113
private val advances: Map<String, Int> =
77114
CompiledMotd::class
78115
.java
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package gg.grounds.proxy.velocity.motd
2+
3+
import com.google.gson.JsonParser
4+
import net.kyori.adventure.text.Component
5+
import net.kyori.adventure.text.TextComponent
6+
import net.kyori.adventure.text.format.Style
7+
import net.kyori.adventure.text.format.TextDecoration
8+
import org.junit.jupiter.api.Assertions.*
9+
import org.junit.jupiter.api.Test
10+
11+
class NativeMotdAlignmentTest {
12+
private fun characters(component: Component): List<Pair<String, Boolean>> {
13+
val chars = mutableListOf<Pair<String, Boolean>>()
14+
fun walk(node: Component, inherited: Style) {
15+
val style = node.style().merge(inherited, Style.Merge.Strategy.IF_ABSENT_ON_TARGET)
16+
if (node is TextComponent)
17+
node.content().codePoints().forEach {
18+
chars.add(
19+
String(Character.toChars(it)) to
20+
(style.decoration(TextDecoration.BOLD) == TextDecoration.State.TRUE)
21+
)
22+
}
23+
node.children().forEach { walk(it, style) }
24+
}
25+
walk(component, Style.empty())
26+
return chars
27+
}
28+
29+
@Test
30+
fun `matches the shared native padding and resolved placeholder fixtures`() {
31+
val samples =
32+
JsonParser.parseReader(
33+
javaClass.getResourceAsStream("/server-list/native-padding.json")!!.reader()
34+
)
35+
.asJsonArray
36+
val advances =
37+
JsonParser.parseReader(
38+
javaClass.getResourceAsStream("/motd/java-1.21.1-advances.json")!!.reader()
39+
)
40+
.asJsonObject
41+
for (sample in samples) {
42+
val s = sample.asJsonObject
43+
val source = s.get("source").asString
44+
val document =
45+
JsonParser.parseString(
46+
"""{"nodes":[{"type":"opaque","format":"minimessage","raw":${s.get("source")}}]}"""
47+
)
48+
.asJsonObject
49+
val values =
50+
s.getAsJsonObject("values").entrySet().associate { it.key to it.value.asString }
51+
val result =
52+
CompiledMotd(document, s.getAsJsonArray("alignments").map { it.asString })
53+
.render(values, s.get("width").asInt)
54+
val lines = componentLines(result)
55+
assertEquals(s.getAsJsonArray("expected").size(), lines.size, s.get("id").asString)
56+
for ((index, line) in lines.withIndex()) {
57+
val expected = s.getAsJsonArray("expected")[index].asJsonObject
58+
val chars = characters(line)
59+
val count = expected.get("regular").asInt + expected.get("bold").asInt
60+
val prefix = chars.take(count)
61+
assertTrue(prefix.all { it.first == " " }, source)
62+
assertEquals(expected.get("bold").asInt, prefix.count { it.second }, source)
63+
assertEquals(
64+
expected.get("padding").asInt,
65+
prefix.sumOf { if (it.second) 5 else 4 },
66+
source,
67+
)
68+
assertEquals(
69+
expected.get("width").asInt,
70+
chars.drop(count).sumOf {
71+
(advances[it.first]?.asInt ?: 6) + if (it.second) 1 else 0
72+
},
73+
source,
74+
)
75+
assertFalse(chars.joinToString("") { it.first }.contains("{{"), source)
76+
}
77+
}
78+
}
79+
80+
@Test
81+
fun `uses the closest representable padding for short and full-width lines`() {
82+
val document =
83+
JsonParser.parseString("""{"nodes":[{"type":"text","value":"A"}]}""").asJsonObject
84+
for (alignment in listOf("center", "right")) for (remaining in 0..280) {
85+
val target = if (alignment == "center") remaining / 2.0 else remaining.toDouble()
86+
val candidates =
87+
(0..3)
88+
.flatMap { b -> (0..(remaining / 4)).map { r -> r * 4 + b * 5 } }
89+
.filter { it <= remaining }
90+
val expected =
91+
candidates.minWith(
92+
compareBy<Int> { kotlin.math.abs(it - target) }.thenByDescending { it }
93+
)
94+
val chars =
95+
characters(
96+
CompiledMotd(document, listOf(alignment)).render(emptyMap(), remaining + 6)
97+
)
98+
assertEquals("A", chars.last().first)
99+
assertFalse(chars.last().second, "padding bold must not affect the MOTD")
100+
assertEquals(
101+
expected,
102+
chars.dropLast(1).sumOf { if (it.second) 5 else 4 },
103+
"$alignment $remaining",
104+
)
105+
}
106+
}
107+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{"id":"modern-user-motd","width":267,"source":"<b>GROUNDS NETWORK</b><newline>Stage System | {{region}}","values":{"region":"eu"},"alignments":["center","center"],"expected":[{"width":103,"padding":82,"regular":18,"bold":2},{"width":88,"padding":90,"regular":20,"bold":2}]},
3+
{"id":"legacy-user-motd","width":271,"source":"<b>GROUNDS NETWORK</b><newline>Stage System | {{region}}","values":{"region":"eu"},"alignments":["center","center"],"expected":[{"width":103,"padding":84,"regular":21,"bold":0},{"width":88,"padding":92,"regular":23,"bold":0}]},
4+
{"id":"modern-right","width":267,"source":"A","values":{},"alignments":["right"],"expected":[{"width":6,"padding":261,"regular":64,"bold":1}]},
5+
{"id":"legacy-right","width":271,"source":"A","values":{},"alignments":["right"],"expected":[{"width":6,"padding":265,"regular":65,"bold":1}]},
6+
{"id":"bold-space-measurement","width":40,"source":"<b>A A</b>","values":{},"alignments":["center"],"expected":[{"width":19,"padding":10,"regular":0,"bold":2}]},
7+
{"id":"short-unrepresentable-center","width":8,"source":"A","values":{},"alignments":["center"],"expected":[{"width":6,"padding":0,"regular":0,"bold":0}]},
8+
{"id":"short-right","width":13,"source":"A","values":{},"alignments":["right"],"expected":[{"width":6,"padding":5,"regular":0,"bold":1}]},
9+
{"id":"left-preserves-literal-indentation","width":267,"source":" <b>A</b>","values":{},"alignments":["left"],"expected":[{"width":15,"padding":0,"regular":0,"bold":0}]}
10+
]

0 commit comments

Comments
 (0)