Skip to content

Commit 7b4e22c

Browse files
committed
Feature: Emoji renderer
1 parent 1908d4b commit 7b4e22c

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

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

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.graphics.renderer.gui.font.glyph
22

3+
import com.google.common.math.IntMath.pow
34
import com.lambda.Lambda.LOG
45
import com.lambda.graphics.texture.MipmapTexture
56
import com.lambda.module.modules.client.FontSettings
@@ -12,6 +13,7 @@ import java.net.URL
1213
import java.util.zip.ZipFile
1314
import javax.imageio.ImageIO
1415
import kotlin.math.ceil
16+
import kotlin.math.log2
1517
import kotlin.math.sqrt
1618
import kotlin.system.measureTimeMillis
1719

@@ -40,38 +42,41 @@ class EmojiGlyphs(zipUrl: String) {
4042
val firstImage = ImageIO.read(zip.getInputStream(zip.entries().nextElement()))
4143

4244
val length = zip.size().toDouble()
43-
val squaredLength = ceil(sqrt(length)).toInt()
4445

45-
val texelSize = 1.0 / length
46-
val width = firstImage.width
47-
val height = firstImage.height
46+
// TODO: This is a hack but it works
47+
val width = pow(2, ceil(log2(firstImage.width * sqrt(length))).toInt())
48+
val height = pow(2, ceil(log2(firstImage.height * sqrt(length))).toInt())
49+
val texelSize = 1.0 / width // This assumes that the texture is a power of 2
4850

49-
image = BufferedImage(width * squaredLength, height * squaredLength, BufferedImage.TYPE_INT_ARGB)
51+
image = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
5052
graphics = image.graphics as Graphics2D
5153
graphics.color = Color(0, 0, 0, 0)
5254

53-
zip.entries().asSequence()
54-
.forEach { entry ->
55-
val name = entry.name.substringAfterLast("/").substringBeforeLast(".")
56-
val emoji = ImageIO.read(zip.getInputStream(entry))
55+
for (entry in zip.entries()) {
56+
val name = entry.name.substringAfterLast("/").substringBeforeLast(".")
57+
val emoji = ImageIO.read(zip.getInputStream(entry))
5758

58-
if (x + emoji.width >= image.width) {
59-
y += emoji.height
60-
x = 0
61-
}
59+
if (x + emoji.width >= image.width) {
60+
y += emoji.height
61+
x = 0
62+
}
6263

63-
graphics.drawImage(emoji, x, y, null)
64+
check(y + emoji.height < image.height) { "Can't load emoji glyphs. Texture size is too small" }
6465

65-
val size = Vec2d(emoji.width, emoji.height)
66-
val uv1 = Vec2d(-x, -y) * texelSize
67-
val uv2 = Vec2d(-x, -y).minus(size) * texelSize
66+
graphics.drawImage(emoji, x, y, null)
6867

69-
emojiMap[name] = CharInfo(size, uv1, uv2)
68+
val size = Vec2d(emoji.width, emoji.height)
69+
val uv1 = Vec2d(x, y) * texelSize
70+
val uv2 = Vec2d(x, y).plus(size) * texelSize
7071

71-
x += emoji.width
72-
}
72+
emojiMap[name] = CharInfo(size, -uv1, -uv2)
73+
74+
x += emoji.width
75+
}
7376
}
7477

78+
ImageIO.write(image, "png", File("emoji.png"))
79+
7580
fontTexture = MipmapTexture(image)
7681
}
7782

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class FontGlyphs(font: Font) {
4141
rowHeight = 0
4242
}
4343

44-
check(y + charImage.height < TEXTURE_SIZE) { "Can't load font glyphs. Texture size is too small" }
44+
check(y + charImage.height <= TEXTURE_SIZE) { "Can't load font glyphs. Texture size is too small" }
4545

4646
graphics.drawImage(charImage, x, y, null)
4747

0 commit comments

Comments
 (0)