Skip to content

Commit d75461c

Browse files
committed
Better emoji parsing
1 parent 87cb9b7 commit d75461c

File tree

5 files changed

+50
-30
lines changed

5 files changed

+50
-30
lines changed

common/src/main/kotlin/com/lambda/core/Loader.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.lambda.Lambda.LOG
55
import com.lambda.command.CommandRegistry
66
import com.lambda.friend.FriendRegistry
77
import com.lambda.graphics.renderer.gui.font.LambdaFont
8-
import com.lambda.graphics.renderer.gui.font.LambdaMoji
8+
import com.lambda.graphics.renderer.gui.font.LambdaEmoji
99
import com.lambda.gui.GuiConfigurable
1010
import com.lambda.gui.HudGuiConfigurable
1111
import com.lambda.interaction.PlayerPacketManager
@@ -23,7 +23,7 @@ object Loader {
2323
RotationManager,
2424
PlayerPacketManager,
2525
LambdaFont.Loader,
26-
LambdaMoji.Loader,
26+
LambdaEmoji.Loader,
2727
GuiConfigurable,
2828
HudGuiConfigurable,
2929
FriendRegistry,

common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/FontRenderer.kt

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
55
import com.lambda.graphics.buffer.vao.vertex.VertexMode
66
import com.lambda.graphics.renderer.gui.font.glyph.GlyphInfo
77
import com.lambda.graphics.shader.Shader
8+
import com.lambda.module.modules.client.LambdaMoji
89
import com.lambda.module.modules.client.RenderSettings
910
import com.lambda.util.math.ColorUtils.a
1011
import com.lambda.util.math.ColorUtils.setAlpha
@@ -13,7 +14,7 @@ import java.awt.Color
1314

1415
class FontRenderer(
1516
private val font: LambdaFont,
16-
private val emojis: LambdaMoji
17+
private val emojis: LambdaEmoji
1718
) {
1819
private val vao = VAO(VertexMode.TRIANGLES, VertexAttrib.Group.FONT)
1920

@@ -24,7 +25,7 @@ class FontRenderer(
2425
* Parses the emojis in the given text.
2526
*
2627
* @param text The text to parse.
27-
* @return A list of pairs containing the glyph info and the range of the emoji in the text.
28+
* @return A list of pairs containing the glyph info and the range of the emoji in the text.
2829
*/
2930
fun parseEmojis(text: String) =
3031
mutableListOf<Pair<GlyphInfo, IntRange>>().apply {
@@ -102,36 +103,44 @@ class FontRenderer(
102103

103104
val emojis = parseEmojis(text)
104105

105-
repeat(text.length) { index ->
106-
fun draw(info: GlyphInfo, color: Color, offset: Double = 0.0) {
107-
val scaledSize = info.size * actualScale
108-
val pos1 = Vec2d(posX, posY) + offset * actualScale
109-
val pos2 = pos1 + scaledSize
106+
fun draw(info: GlyphInfo, color: Color, offset: Double = 0.0) {
107+
val scaledSize = info.size * actualScale
108+
val pos1 = Vec2d(posX, posY) + offset * actualScale
109+
val pos2 = pos1 + scaledSize
110110

111-
block(info, pos1, pos2, color)
112-
if (offset == 0.0) posX += scaledSize.x + scaledGap
113-
}
111+
block(info, pos1, pos2, color)
112+
if (offset == 0.0) posX += scaledSize.x + scaledGap
113+
}
114114

115-
// Check if there's an emoji
116-
emojis.firstOrNull { index in it.second }?.let { emoji ->
117-
// Replace first emoji char by an emoji glyph and skip the other ones
118-
if (index == emoji.second.first) {
119-
draw(emoji.first, emojiColor)
120-
}
115+
var index = 0
116+
textProcessor@ while (index < text.length) {
117+
var innerLoopContact = false // Instead of using BreakContinueInInlineLambdas, we use this
121118

122-
return@repeat
119+
if (LambdaMoji.isEnabled) {
120+
// Check if there are emojis to render
121+
emojis.firstOrNull { index in it.second }?.let { emoji ->
122+
if (index == emoji.second.first) draw(emoji.first, emojiColor)
123+
124+
// Skip the emoji
125+
index = emoji.second.last + 1
126+
innerLoopContact = true
127+
}
123128
}
124129

130+
if (innerLoopContact) continue@textProcessor
131+
125132
// Render chars
126-
font[text[index]]?.let { info ->
127-
// Draw a shadow before
128-
if (shadow && RenderSettings.shadow && shadowShift > 0.0) {
129-
draw(info, shadowColor, shadowShift)
130-
}
133+
val charInfo = font[text[index]] ?: continue@textProcessor
131134

132-
// Draw actual char over the shadow
133-
draw(info, color)
135+
// Draw a shadow before
136+
if (shadow && RenderSettings.shadow && shadowShift > 0.0) {
137+
draw(charInfo, shadowColor, shadowShift)
134138
}
139+
140+
// Draw actual char over the shadow
141+
draw(charInfo, color)
142+
143+
index++
135144
}
136145
}
137146

common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaMoji.kt renamed to common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.lambda.graphics.renderer.gui.font
33
import com.lambda.core.Loadable
44
import com.lambda.graphics.renderer.gui.font.glyph.EmojiGlyphs
55

6-
enum class LambdaMoji(private val zipUrl: String) {
6+
enum class LambdaEmoji(private val zipUrl: String) {
77
Twemoji("https://github.com/Edouard127/emoji-generator/releases/latest/download/emojis.zip");
88

99
lateinit var glyphs: EmojiGlyphs // TODO: Support multiple emoji pools
@@ -16,7 +16,7 @@ enum class LambdaMoji(private val zipUrl: String) {
1616

1717
object Loader : Loadable {
1818
override fun load(): String {
19-
entries.forEach(LambdaMoji::loadGlyphs)
19+
entries.forEach(LambdaEmoji::loadGlyphs)
2020
return "Loaded ${entries.size} emoji pools"
2121
}
2222
}

common/src/main/kotlin/com/lambda/gui/api/RenderLayer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.lambda.gui.api
22

33
import com.lambda.graphics.renderer.gui.font.FontRenderer
44
import com.lambda.graphics.renderer.gui.font.LambdaFont
5-
import com.lambda.graphics.renderer.gui.font.LambdaMoji
5+
import com.lambda.graphics.renderer.gui.font.LambdaEmoji
66
import com.lambda.graphics.renderer.gui.rect.FilledRectRenderer
77
import com.lambda.graphics.renderer.gui.rect.OutlineRectRenderer
88

@@ -11,7 +11,7 @@ class RenderLayer {
1111
val outline = OutlineRectRenderer()
1212
val font = FontRenderer(
1313
LambdaFont.FiraSansRegular,
14-
LambdaMoji.Twemoji,
14+
LambdaEmoji.Twemoji,
1515
)
1616

1717
fun render() {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.lambda.module.modules.client
2+
3+
import com.lambda.module.Module
4+
import com.lambda.module.tag.ModuleTag
5+
6+
object LambdaMoji : Module(
7+
name = "LambdaMoji",
8+
description = "",
9+
defaultTags = setOf(ModuleTag.CLIENT, ModuleTag.RENDER),
10+
enabledByDefault = true,
11+
)

0 commit comments

Comments
 (0)