Skip to content

Commit 48b27e8

Browse files
committed
Fix: Emoji parsing
1 parent 951fde8 commit 48b27e8

File tree

2 files changed

+62
-33
lines changed

2 files changed

+62
-33
lines changed

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

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,47 @@ class FontRenderer(
5353

5454
val emojis = parseEmojis(text)
5555

56-
val subText = emojis.asReversed().fold(text) { acc, (
57-
charInfo, start, end
58-
) ->
59-
val emojiSize = charInfo.size * actualScale
60-
61-
val startPos = Vec2d(posX, posY)
62-
val endPos = startPos + emojiSize
63-
64-
putChar(position, startPos, endPos, color, charInfo)
65-
66-
posX += emojiSize.x + scaledGap
67-
68-
acc.replaceRange(start, end, " ")
69-
}
56+
var index = 0
57+
while (index < text.length) {
58+
emojis
59+
.firstOrNull { index in it.second..it.third }
60+
?.let { emoji ->
61+
val scaledSize = emoji.first.size * actualScale
62+
val pos1 = Vec2d(posX, posY)
63+
val pos2 = pos1 + scaledSize
64+
65+
putChar(position, pos1, pos2, color, emoji.first)
66+
67+
posX += scaledSize.x + scaledGap
68+
index += emoji.third - emoji.second + 1
69+
70+
if (index >= text.length) {
71+
// This means we've reached the end of the text
72+
return@use
73+
}
74+
}
75+
76+
val char = text[index]
77+
val glyph = font[char] ?: run {
78+
index++
79+
return@use
80+
}
7081

71-
subText.toCharArray().forEach { char ->
72-
val charInfo = font[char] ?: return@forEach
73-
val scaledSize = charInfo.size * actualScale
82+
val scaledSize = glyph.size * actualScale
7483

7584
val pos1 = Vec2d(posX, posY)
7685
val pos2 = pos1 + scaledSize
7786

7887
if (shadow && FontSettings.shadow) {
7988
val shadowPos1 = pos1 + scaledShadowShift
8089
val shadowPos2 = shadowPos1 + scaledSize
81-
putChar(position, shadowPos1, shadowPos2, shadowColor, charInfo)
90+
putChar(position, shadowPos1, shadowPos2, shadowColor, glyph)
8291
}
8392

84-
putChar(position, pos1, pos2, color, charInfo)
93+
putChar(position, pos1, pos2, color, glyph)
8594

8695
posX += scaledSize.x + scaledGap
96+
index++
8797
}
8898
}
8999

@@ -92,19 +102,30 @@ class FontRenderer(
92102

93103
val emojis = parseEmojis(text)
94104

95-
val subText = emojis.asReversed().fold(text) { acc, (
96-
charInfo, start, end
97-
) ->
98-
val emojiWidth = charInfo.size.x
99-
100-
width += emojiWidth + gap
101-
102-
acc.replaceRange(start, end, " ")
103-
}
105+
var index = 0
106+
while (index < text.length) {
107+
emojis
108+
.firstOrNull { index in it.second..it.third }
109+
?.let { emoji ->
110+
val scaledSize = emoji.first.size * getScaleFactor(scale)
111+
width += scaledSize.x + gap
112+
113+
index += emoji.third - emoji.second + 1
114+
115+
if (index >= text.length) {
116+
// This means we've reached the end of the text
117+
return width * getScaleFactor(scale)
118+
}
119+
}
120+
121+
val char = text[index]
122+
val glyph = font[char] ?: run {
123+
index++
124+
return width * getScaleFactor(scale)
125+
}
104126

105-
subText.forEach {
106-
val glyph = font[it] ?: return@forEach
107-
width += glyph.size.x + gap
127+
width += glyph.width + gap
128+
index++
108129
}
109130

110131
return width * getScaleFactor(scale)

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ package com.lambda.graphics.renderer.gui.font.glyph
22

33
import com.lambda.util.math.Vec2d
44

5-
class CharInfo(
5+
data class CharInfo(
66
val size: Vec2d,
77
val uv1: Vec2d,
88
val uv2: Vec2d
9-
)
9+
) {
10+
val width get() = size.x
11+
val height get() = size.y
12+
13+
val u1 get() = uv1.x
14+
val v1 get() = uv1.y
15+
val u2 get() = uv2.x
16+
val v2 get() = uv2.y
17+
}

0 commit comments

Comments
 (0)