11package com.lambda.graphics.renderer.gui.font.glyph
22
3+ import com.google.common.math.IntMath.pow
34import com.lambda.Lambda.LOG
45import com.lambda.graphics.texture.MipmapTexture
56import com.lambda.module.modules.client.FontSettings
@@ -12,6 +13,7 @@ import java.net.URL
1213import java.util.zip.ZipFile
1314import javax.imageio.ImageIO
1415import kotlin.math.ceil
16+ import kotlin.math.log2
1517import kotlin.math.sqrt
1618import 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
0 commit comments