@@ -10,8 +10,31 @@ import com.lambda.module.modules.client.FontSettings
1010import com.lambda.util.math.Vec2d
1111import java.awt.Color
1212
13- class FontRenderer (private val font : LambdaFont ) : Renderer(VertexMode .TRIANGLES , VertexAttrib .Group .FONT ) {
13+ class FontRenderer (
14+ private val font : LambdaFont ,
15+ private val emojis : LambdaMoji ,
16+ ) : Renderer(VertexMode .TRIANGLES , VertexAttrib .Group .FONT ) {
1417 var scaleMultiplier = 1.0
18+ private val emojiRegex = Regex (" :[a-zA-Z0-9_]+:" )
19+
20+ /* *
21+ * Parses the emojis in the given text.
22+ *
23+ * @param text The text to parse.
24+ * @return A list of triples containing the emoji text, start index, and end index.
25+ */
26+ private fun parseEmojis (text : String ): List <
27+ Triple <CharInfo , Int , Int >> {
28+ val result = mutableListOf<Triple <CharInfo , Int , Int >>()
29+ val matches = emojiRegex.findAll(text)
30+
31+ matches.forEach {
32+ val index = it.value.substring(1 , it.value.length - 1 )
33+ result.add(Triple (emojis[index] ? : return @forEach, it.range.first, it.range.last))
34+ }
35+
36+ return result
37+ }
1538
1639 fun build (
1740 text : String ,
@@ -28,7 +51,25 @@ class FontRenderer(private val font: LambdaFont) : Renderer(VertexMode.TRIANGLES
2851 var posX = 0.0
2952 val posY = getHeight(scale) * - 0.5 + baselineOffset * actualScale
3053
31- text.toCharArray().forEach { char ->
54+ val emojis = parseEmojis(text)
55+
56+ val subText = emojis.asReversed().fold(text) { acc, (
57+ charInfo, start, end
58+ ) ->
59+ val emojiWidth = charInfo.size.x * actualScale
60+ val emojiHeight = charInfo.size.y * actualScale
61+
62+ val startPos = Vec2d (posX, posY)
63+ val endPos = startPos + Vec2d (emojiWidth, emojiHeight)
64+
65+ putChar(position, startPos, endPos, color, charInfo)
66+
67+ posX + = emojiWidth + scaledGap
68+
69+ acc.replaceRange(start, end, " " )
70+ }
71+
72+ subText.toCharArray().forEach { char ->
3273 val charInfo = font[char] ? : return @forEach
3374 val scaledSize = charInfo.size * actualScale
3475
@@ -50,8 +91,20 @@ class FontRenderer(private val font: LambdaFont) : Renderer(VertexMode.TRIANGLES
5091 fun getWidth (text : String , scale : Double = 1.0): Double {
5192 var width = 0.0
5293
53- text.forEach { char ->
54- val glyph = font[char] ? : return @forEach
94+ val emojis = parseEmojis(text)
95+
96+ val subText = emojis.asReversed().fold(text) { acc, (
97+ charInfo, start, end
98+ ) ->
99+ val emojiWidth = charInfo.size.x
100+
101+ width + = emojiWidth + gap
102+
103+ acc.replaceRange(start, end, " " )
104+ }
105+
106+ subText.forEach {
107+ val glyph = font[it] ? : return @forEach
55108 width + = glyph.size.x + gap
56109 }
57110
@@ -88,6 +141,7 @@ class FontRenderer(private val font: LambdaFont) : Renderer(VertexMode.TRIANGLES
88141 override fun render () {
89142 shader.use()
90143 font.glyphs.bind()
144+ // emojis.glyphs.bind() // You have to modify the uniform in the shader to use the correct texture
91145 super .render()
92146 }
93147
@@ -98,4 +152,4 @@ class FontRenderer(private val font: LambdaFont) : Renderer(VertexMode.TRIANGLES
98152 private val baselineOffset get() = FontSettings .baselineOffset * 2.0f - 10f
99153 private val gap get() = FontSettings .gapSetting * 0.5f - 0.8f
100154 }
101- }
155+ }
0 commit comments