Skip to content

Commit b38439d

Browse files
committed
Test: Emoji glyphs
1 parent d421016 commit b38439d

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +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
89
import com.lambda.gui.GuiConfigurable
910
import com.lambda.interaction.PlayerPacketManager
1011
import com.lambda.interaction.RotationManager
@@ -20,6 +21,7 @@ object Loader {
2021
RotationManager,
2122
PlayerPacketManager,
2223
LambdaFont.Loader,
24+
LambdaMoji.Loader,
2325
GuiConfigurable,
2426
FriendRegistry,
2527
SoundRegistry,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lambda.graphics.renderer.gui.font
2+
3+
import com.lambda.core.Loadable
4+
import com.lambda.graphics.renderer.gui.font.glyph.EmojiGlyphs
5+
6+
enum class LambdaMoji(private val zipUrl: String) {
7+
Twemoji("https://github.com/Edouard127/emoji-generator/releases/latest/download/emojis.zip");
8+
9+
lateinit var glyphs: EmojiGlyphs
10+
11+
operator fun get(emoji: String) = glyphs.getEmoji(emoji)
12+
13+
fun loadGlyphs() {
14+
glyphs = EmojiGlyphs(zipUrl)
15+
}
16+
17+
object Loader : Loadable {
18+
override fun load(): String {
19+
LambdaMoji.entries.forEach(LambdaMoji::loadGlyphs)
20+
return "Loaded ${LambdaMoji.entries.size} emoji sets"
21+
}
22+
}
23+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.lambda.graphics.renderer.gui.font.glyph
2+
3+
import com.lambda.graphics.texture.MipmapTexture
4+
import com.lambda.module.modules.client.FontSettings
5+
import com.lambda.util.math.Vec2d
6+
import java.awt.Color
7+
import java.awt.Graphics2D
8+
import java.awt.image.BufferedImage
9+
import java.net.URL
10+
import java.nio.file.Files
11+
import java.util.zip.ZipFile
12+
import javax.imageio.ImageIO
13+
import kotlin.math.ceil
14+
import kotlin.math.sqrt
15+
16+
class EmojiGlyphs(zipUrl: String) {
17+
private val emojiMap = mutableMapOf<String, CharInfo>()
18+
private val fontTexture: MipmapTexture
19+
20+
init {
21+
val file = Files.createTempFile("emoji", ".zip").toFile()
22+
val url = URL(zipUrl)
23+
file.writeBytes(url.readBytes())
24+
25+
ZipFile(file).use { zip ->
26+
val size = zip.entries().asSequence().count()
27+
val dimensions = Vec2d(72.0, 72.0)
28+
val texelSize = 1.0 / size
29+
val width = 72 * ceil(sqrt(size.toDouble())).toInt()
30+
31+
val image = BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB)
32+
val graphics = image.graphics as Graphics2D
33+
graphics.background = Color(0, 0, 0, 0)
34+
35+
var x = 0
36+
var y = 0
37+
38+
zip.entries().asSequence().forEach { entry ->
39+
val name = entry.name.substringAfterLast("/").substringBeforeLast(".")
40+
val charImage = ImageIO.read(zip.getInputStream(entry))
41+
42+
if (x + 72 >= width) {
43+
y += 72
44+
x = 0
45+
}
46+
47+
graphics.drawImage(charImage, x, y, null)
48+
49+
val uv1 = Vec2d(x.toDouble(), y.toDouble()) * texelSize
50+
val uv2 = Vec2d(x, y).plus(dimensions) * texelSize
51+
emojiMap[name] = CharInfo(dimensions, uv1, uv2)
52+
53+
x += 72
54+
}
55+
56+
fontTexture = MipmapTexture(image)
57+
}
58+
}
59+
60+
fun bind() {
61+
with(fontTexture) {
62+
bind()
63+
setLOD(FontSettings.lodBias.toFloat())
64+
}
65+
}
66+
67+
fun getEmoji(emoji: String): CharInfo? =
68+
emojiMap[emoji]
69+
}

0 commit comments

Comments
 (0)