mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +08:00
Feat: Port FontRender from OpenVape
This commit is contained in:
1 parent
fa1abcb59d
commit
758bac7330
35 files changed
+7137
-2
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package cn.xiaozhou233.orangex;
|
||||
|
||||
import cn.xiaozhou233.orangex.font.FontManager;
|
||||
import cn.xiaozhou233.orangex.font.StbFontManager;
|
||||
import cn.xiaozhou233.orangex.mixin.MixinManager;
|
||||
import lombok.Getter;
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
@@ -10,6 +11,7 @@ public class OrangeX {
|
||||
public static final OrangeX INSTANCE = new OrangeX();
|
||||
public MixinManager mixinManager = new MixinManager();
|
||||
FontManager fontManager = new FontManager();
|
||||
StbFontManager stbFontManager = new StbFontManager();
|
||||
private final EventBus eventBus = EventBus.builder()
|
||||
.logNoSubscriberMessages(false)
|
||||
.logSubscriberExceptions(false)
|
||||
@@ -21,6 +23,7 @@ public class OrangeX {
|
||||
public void start() {
|
||||
System.out.println("OrangeX starting...");
|
||||
|
||||
stbFontManager.loadFonts();
|
||||
mixinManager.start();
|
||||
|
||||
System.out.println("OrangeX started.");
|
||||
|
||||
@@ -16,4 +16,4 @@ public class FontManager {
|
||||
|
||||
public FontRenderer JelloMedium32 = new FontRenderer("JelloMedium.ttf", 32, Font.PLAIN, true, true);
|
||||
public FontRenderer JelloRegular16 = new FontRenderer("JelloRegular.ttf", 16, Font.PLAIN, true, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.xiaozhou233.orangex.font;
|
||||
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
public class GlImageTexture {
|
||||
public int width;
|
||||
public int height;
|
||||
public int textureId;
|
||||
|
||||
public GlImageTexture() {
|
||||
this.textureId = GL11.glGenTextures();
|
||||
}
|
||||
|
||||
public GlImageTexture(int textureId) {
|
||||
this.textureId = textureId;
|
||||
}
|
||||
|
||||
public void bind() {
|
||||
GlStateManager.bindTexture(this.textureId);
|
||||
}
|
||||
|
||||
public void delete() {
|
||||
GL11.glDeleteTextures(this.textureId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.xiaozhou233.orangex.font;
|
||||
|
||||
import cn.xiaozhou233.orangex.font.shader.ShaderProgram;
|
||||
|
||||
public class LegacyFontShaderProgram extends ShaderProgram {
|
||||
private static final String VERTEX_SHADER_SOURCE;
|
||||
private static final String FRAGMENT_SHADER_SOURCE;
|
||||
|
||||
public LegacyFontShaderProgram() {
|
||||
super(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE);
|
||||
}
|
||||
|
||||
static {
|
||||
FRAGMENT_SHADER_SOURCE = "#version 120\nuniform sampler2D texture;\nvoid main() {\n vec2 coords = gl_TexCoord[0].xy;\n float alpha = texture2D(texture, coords).x;\n alpha = max(0.0, alpha + 0.25 * (1.0 - alpha));\n float smoothenAlpha = smoothstep(0.2, 0.8, alpha);\n alpha = mix(alpha, 0.0, 1.0 - smoothenAlpha);\n gl_FragColor = vec4(gl_Color.rgb, alpha * gl_Color.a);\n}";
|
||||
VERTEX_SHADER_SOURCE = "#version 120\nvoid main(void) {\n gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n gl_FrontColor = gl_Color;\n}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,382 @@
|
||||
package cn.xiaozhou233.orangex.font;
|
||||
|
||||
import cn.xiaozhou233.orangex.font.stb.StbTrueTypeFontInfo;
|
||||
import cn.xiaozhou233.orangex.font.stb.StbTrueTypePackContext;
|
||||
import cn.xiaozhou233.orangex.font.stb.StbTrueTypePackRange;
|
||||
import cn.xiaozhou233.orangex.font.stb.StbTrueTypePackedChar;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL12;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
public class LegacySmoothFontRenderer extends SmoothFontRenderer {
|
||||
private float ascent;
|
||||
private Map<Integer, SmoothFontGlyph> glyphs = new HashMap<Integer, SmoothFontGlyph>();
|
||||
private int textureId;
|
||||
private float pixelScale;
|
||||
private float renderScale = 0.6f;
|
||||
private LegacyFontShaderProgram shader;
|
||||
|
||||
public LegacySmoothFontRenderer(byte[] fontData, int fontSize, String fontName) {
|
||||
this.fontSize = fontSize;
|
||||
buildFont(fontData, fontSize, fontName);
|
||||
}
|
||||
|
||||
public LegacySmoothFontRenderer(String resourcePath, int fontSize) {
|
||||
this.fontSize = fontSize;
|
||||
try {
|
||||
byte[] fontData = readResource(resourcePath);
|
||||
buildFont(fontData, fontSize, resourcePath);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] readResource(String path) {
|
||||
try {
|
||||
String resourcePath = "fonts/" + path;
|
||||
InputStream is = LegacySmoothFontRenderer.class.getClassLoader().getResourceAsStream(resourcePath);
|
||||
if (is == null) {
|
||||
is = Minecraft.getMinecraft().getResourceManager().getResource(new ResourceLocation("fonts/" + path)).getInputStream();
|
||||
}
|
||||
if (is == null) return null;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[8192];
|
||||
int len;
|
||||
while ((len = is.read(buf)) != -1) {
|
||||
baos.write(buf, 0, len);
|
||||
}
|
||||
is.close();
|
||||
return baos.toByteArray();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void buildFont(byte[] fontData, int fontSize, String fontName) {
|
||||
if (fontData == null) return;
|
||||
|
||||
HashSet<Integer> charSet = new HashSet<Integer>();
|
||||
for (int i = 32; i <= 126; ++i) {
|
||||
charSet.add(i);
|
||||
}
|
||||
charSet.add(0);
|
||||
|
||||
try {
|
||||
byte[] chineseData = readResource("chinese.properties.txt");
|
||||
if (chineseData != null) {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(chineseData), StandardCharsets.UTF_8));
|
||||
int ch;
|
||||
while ((ch = reader.read()) != -1) {
|
||||
if (ch >= 32) charSet.add(ch);
|
||||
}
|
||||
reader.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
int[] chars = charSet.stream().mapToInt(Integer::intValue).toArray();
|
||||
Arrays.sort(chars);
|
||||
|
||||
int atlasSize = 4096;
|
||||
StbTrueTypeFontInfo fontInfo = new StbTrueTypeFontInfo();
|
||||
if (SmoothFontRasterState.z(fontInfo, fontData, 0) == 0) {
|
||||
System.err.println("Failed to init font: " + fontName);
|
||||
return;
|
||||
}
|
||||
|
||||
int[] ascentArr = new int[1], descentArr = new int[1], lineGap = new int[1];
|
||||
SmoothFontRasterState.x(fontInfo, ascentArr, descentArr, lineGap);
|
||||
this.ascent = ascentArr[0];
|
||||
|
||||
int[] fw = new int[1], fh = new int[1], fx = new int[1], fy = new int[1];
|
||||
SmoothFontRasterState.s(fontInfo, 72, fw, fh, fx, fy);
|
||||
float glyphHeight = fy[0] - fh[0];
|
||||
float fontHeight = ascentArr[0] - descentArr[0];
|
||||
float aspect = glyphHeight / fontHeight;
|
||||
float pixelHeight = (float) this.fontSize * (0.6f / aspect);
|
||||
|
||||
byte[] atlasData = new byte[atlasSize * atlasSize];
|
||||
StbTrueTypePackContext packCtx = new StbTrueTypePackContext();
|
||||
if (SmoothFontRasterState.C(packCtx, atlasData, atlasSize, atlasSize, 0, 1) == 0) {
|
||||
System.err.println("Failed to begin packing font");
|
||||
return;
|
||||
}
|
||||
|
||||
int oversampleX = 2, oversampleY = 2;
|
||||
SmoothFontRasterState.n(packCtx, oversampleX, oversampleY);
|
||||
|
||||
try {
|
||||
StbTrueTypePackedChar[] packedChars = new StbTrueTypePackedChar[chars.length];
|
||||
for (int i = 0; i < packedChars.length; ++i) {
|
||||
packedChars[i] = new StbTrueTypePackedChar();
|
||||
}
|
||||
StbTrueTypePackRange range = new StbTrueTypePackRange();
|
||||
range.E = pixelHeight;
|
||||
range.R = 0;
|
||||
range.N = chars;
|
||||
range.b = chars.length;
|
||||
range.M = packedChars;
|
||||
range.i = (byte) oversampleX;
|
||||
range.y = (byte) oversampleY;
|
||||
SmoothFontRasterState.c(packCtx, fontData, 0, new StbTrueTypePackRange[]{range}, 1);
|
||||
SmoothFontRasterState.S(packCtx);
|
||||
|
||||
this.pixelScale = SmoothFontRasterState.V(fontInfo, pixelHeight);
|
||||
for (int i = 0; i < chars.length; ++i) {
|
||||
int cp = chars[i];
|
||||
StbTrueTypePackedChar pc = packedChars[i];
|
||||
float invW = 1.0f / (float) atlasSize;
|
||||
float invH = 1.0f / (float) atlasSize;
|
||||
this.glyphs.put(cp, new SmoothFontGlyph(
|
||||
pc.s, pc.G, pc.t, pc.R,
|
||||
(float) pc.b * invW, (float) pc.k * invH,
|
||||
(float) pc.q * invW, (float) pc.p * invH,
|
||||
pc.A
|
||||
));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ByteBuffer buffer = BufferUtils.createByteBuffer(atlasData.length + 128);
|
||||
buffer.put(atlasData);
|
||||
buffer.flip();
|
||||
|
||||
this.textureId = GL11.glGenTextures();
|
||||
GlStateManager.bindTexture(this.textureId);
|
||||
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
|
||||
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_LUMINANCE, atlasSize, atlasSize, 0, GL11.GL_LUMINANCE, GL11.GL_UNSIGNED_BYTE, buffer);
|
||||
|
||||
shader = new LegacyFontShaderProgram();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawVerticalString(String text, double x, double y, Color color) {
|
||||
char[] chars = text.toCharArray();
|
||||
for (int i = 0; i < chars.length; ++i) {
|
||||
drawStringWithShadow(String.valueOf(chars[i]), x, y, color);
|
||||
y += getStringHeight(String.valueOf(chars[i])) * 0.9;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderString(String text, double x, double y, int color, boolean fallbackToVanilla) {
|
||||
if (!fallbackToVanilla) {
|
||||
if (text == null || text.length() == 0 || textureId == 0) return;
|
||||
|
||||
int r = (color >> 16) & 0xFF;
|
||||
int g = (color >> 8) & 0xFF;
|
||||
int b = color & 0xFF;
|
||||
int a = (color >> 24) & 0xFF;
|
||||
if (a < 0) a += 256;
|
||||
if (a == 0) a = 255;
|
||||
Color baseColor = new Color(r, g, b, a);
|
||||
|
||||
char[] colorCodes = new char[text.length()];
|
||||
Arrays.fill(colorCodes, '\u0000');
|
||||
StringBuilder sb = new StringBuilder();
|
||||
char[] chars = text.toCharArray();
|
||||
int codeIdx = 0;
|
||||
for (int i = 0; i < chars.length; ++i) {
|
||||
char c = chars[i];
|
||||
if (c == '\u00a7') {
|
||||
if (++i >= chars.length) continue;
|
||||
colorCodes[codeIdx] = chars[i];
|
||||
continue;
|
||||
}
|
||||
sb.append(c);
|
||||
++codeIdx;
|
||||
}
|
||||
text = sb.toString();
|
||||
|
||||
float f = renderScale;
|
||||
y += (double)(this.ascent * this.pixelScale * f);
|
||||
Color currentColor = baseColor;
|
||||
|
||||
int prevTex = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
|
||||
boolean blendEnabled = GL11.glIsEnabled(GL11.GL_BLEND);
|
||||
boolean texEnabled = GL11.glIsEnabled(GL11.GL_TEXTURE_2D);
|
||||
boolean lightingEnabled = GL11.glIsEnabled(GL11.GL_LIGHTING);
|
||||
boolean alphaEnabled = GL11.glIsEnabled(GL11.GL_ALPHA_TEST);
|
||||
boolean cullEnabled = GL11.glIsEnabled(GL11.GL_CULL_FACE);
|
||||
|
||||
if (!blendEnabled) GlStateManager.enableBlend();
|
||||
if (!texEnabled) GlStateManager.enableTexture2D();
|
||||
if (lightingEnabled) GlStateManager.disableLighting();
|
||||
if (!alphaEnabled) GlStateManager.enableAlpha();
|
||||
if (cullEnabled) GlStateManager.disableCull();
|
||||
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
GlStateManager.bindTexture(this.textureId);
|
||||
|
||||
if (shader != null) shader.bind();
|
||||
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
for (int i = 0; i < text.length(); ++i) {
|
||||
char c = text.charAt(i);
|
||||
SmoothFontGlyph glyph = this.glyphs.get((int) c);
|
||||
if (glyph == null) glyph = this.glyphs.get(32);
|
||||
if (glyph == null) continue;
|
||||
|
||||
char cc = colorCodes[i];
|
||||
if (cc != '\u0000') {
|
||||
float[] rgb = new float[3];
|
||||
applyColorCode(cc, rgb);
|
||||
currentColor = new Color(rgb[0], rgb[1], rgb[2]);
|
||||
if (cc == 'r') currentColor = baseColor;
|
||||
}
|
||||
|
||||
float x1 = (float) x + glyph.xOffset * f;
|
||||
float y1 = (float) y + glyph.yOffset * f;
|
||||
float x2 = (float) x + glyph.xMax * f;
|
||||
float y2 = (float) y + glyph.yMax * f;
|
||||
|
||||
GL11.glColor4f(
|
||||
(float) currentColor.getRed() / 255.0f,
|
||||
(float) currentColor.getGreen() / 255.0f,
|
||||
(float) currentColor.getBlue() / 255.0f,
|
||||
(float) baseColor.getAlpha() / 255.0f
|
||||
);
|
||||
GL11.glTexCoord2f(glyph.uMin, glyph.vMin);
|
||||
GL11.glVertex2f(x1, y1);
|
||||
GL11.glTexCoord2f(glyph.uMin, glyph.vMax);
|
||||
GL11.glVertex2f(x1, y2);
|
||||
GL11.glTexCoord2f(glyph.uMax, glyph.vMax);
|
||||
GL11.glVertex2f(x2, y2);
|
||||
GL11.glTexCoord2f(glyph.uMax, glyph.vMin);
|
||||
GL11.glVertex2f(x2, y1);
|
||||
|
||||
x += glyph.advance * f;
|
||||
}
|
||||
GL11.glEnd();
|
||||
|
||||
if (shader != null) shader.restorePreviousProgram();
|
||||
|
||||
GlStateManager.bindTexture(prevTex);
|
||||
if (!alphaEnabled) GlStateManager.disableAlpha();
|
||||
if (lightingEnabled) GlStateManager.enableLighting();
|
||||
if (!blendEnabled) GlStateManager.disableBlend();
|
||||
if (!texEnabled) GlStateManager.disableTexture2D();
|
||||
if (cullEnabled) GlStateManager.enableCull();
|
||||
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
} else {
|
||||
float f = (float) this.fontSize / 16.0f;
|
||||
float inv = 1.0f / f;
|
||||
x = Math.ceil(x * inv);
|
||||
y = Math.ceil(y * inv);
|
||||
GL11.glPushMatrix();
|
||||
GL11.glScalef(f, f, f);
|
||||
Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow(text, (float) x, (float) y, color);
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawStringWithShadow(String text, double x, double y, int color) {
|
||||
drawString(text, x + 0.5, y + 0.5, Integer.MIN_VALUE);
|
||||
renderString(text, x, y, color, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawStringWithShadow(String text, double x, double y, Color color, boolean fallbackToVanilla) {
|
||||
if (!fallbackToVanilla) {
|
||||
drawString(text, x + 0.5, y + 0.5, Integer.MIN_VALUE);
|
||||
drawString(text, x, y, color);
|
||||
} else {
|
||||
float f = (float) this.fontSize / 16.0f;
|
||||
float inv = 1.0f / f;
|
||||
x = Math.ceil(x * inv);
|
||||
y = Math.ceil(y * inv);
|
||||
GL11.glPushMatrix();
|
||||
GL11.glScalef(f, f, f);
|
||||
Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow(text, (float) x, (float) y, color.getRGB());
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getStringHeight(String text, boolean fallbackToVanilla) {
|
||||
if (!fallbackToVanilla) {
|
||||
float f = 0.6f;
|
||||
return (float) this.fontSize * f;
|
||||
}
|
||||
float f = (float) this.fontSize / 16.0f;
|
||||
return 8.0f * f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getStringWidth(String text, boolean fallbackToVanilla) {
|
||||
String stripped = this.strippedStringCache.get(text);
|
||||
if (stripped == null) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (char c : text.toCharArray()) {
|
||||
if (c == '\u00a7') continue;
|
||||
sb.append(c);
|
||||
}
|
||||
stripped = sb.toString();
|
||||
this.strippedStringCache.put(text, stripped);
|
||||
}
|
||||
text = stripped;
|
||||
|
||||
if (!fallbackToVanilla) {
|
||||
Double cached = this.stringWidthCache.get(text);
|
||||
if (cached != null) return cached;
|
||||
|
||||
float f = 0.6f;
|
||||
double width = 0.0;
|
||||
for (int i = 0; i < text.length(); ++i) {
|
||||
char c = text.charAt(i);
|
||||
SmoothFontGlyph glyph = this.glyphs.get((int) c);
|
||||
if (glyph == null) glyph = this.glyphs.get(32);
|
||||
if (glyph == null) continue;
|
||||
width += glyph.advance * f;
|
||||
}
|
||||
this.stringWidthCache.put(text, width);
|
||||
return width;
|
||||
}
|
||||
|
||||
float f = (float) this.fontSize / 16.0f;
|
||||
return Minecraft.getMinecraft().fontRendererObj.getStringWidth(text) * f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderStringWithShadow(String text, double x, double y, Color color, Color shadowColor, boolean fallbackToVanilla) {
|
||||
int n = color.getRGB();
|
||||
int n2 = shadowColor.getRGB();
|
||||
if (!fallbackToVanilla) {
|
||||
drawString(text, x + 0.5, y + 0.5, n2);
|
||||
drawString(text, x, y, color);
|
||||
} else {
|
||||
float f = (float) this.fontSize / 16.0f;
|
||||
float inv = 1.0f / f;
|
||||
x = Math.ceil(x * inv);
|
||||
y = Math.ceil(y * inv);
|
||||
GL11.glPushMatrix();
|
||||
GL11.glScalef(f, f, f);
|
||||
Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow(text, (float) x, (float) y, n);
|
||||
GL11.glPopMatrix();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.xiaozhou233.orangex.font;
|
||||
|
||||
public class SmoothFontGlyph {
|
||||
public float uMin;
|
||||
public float vMin;
|
||||
public float uMax;
|
||||
public float vMax;
|
||||
public float xOffset;
|
||||
public float yOffset;
|
||||
public float xMax;
|
||||
public float yMax;
|
||||
public float advance;
|
||||
|
||||
public SmoothFontGlyph(float xOffset, float yOffset, float xMax, float yMax, float uMin, float vMin, float uMax, float vMax, float advance) {
|
||||
this.xOffset = xOffset;
|
||||
this.xMax = xMax;
|
||||
this.yOffset = yOffset;
|
||||
this.yMax = yMax;
|
||||
this.uMin = uMin;
|
||||
this.uMax = uMax;
|
||||
this.vMin = vMin;
|
||||
this.vMax = vMax;
|
||||
this.advance = advance;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,128 @@
|
||||
package cn.xiaozhou233.orangex.font;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class SmoothFontRenderer {
|
||||
protected Map<String, String> strippedStringCache;
|
||||
protected Map<String, Double> stringWidthCache = new SmoothFontRendererWidthCache(this, 16, 0.75f, true);
|
||||
protected boolean fontSelectorEnabled = true;
|
||||
protected int fontSize;
|
||||
|
||||
public void drawString(String text, double x, double y, int color) {
|
||||
drawStringStripped(text, x, y, color, false);
|
||||
}
|
||||
|
||||
public abstract void drawVerticalString(String text, double x, double y, Color color);
|
||||
|
||||
protected void applyColorCode(char code, float[] rgb) {
|
||||
if (code == '4') { rgb[0] = 1.0f; rgb[1] = 0.0f; rgb[2] = 0.0f; }
|
||||
if (code == 'c') { rgb[0] = 1.0f; rgb[1] = 0.33f; rgb[2] = 0.33f; }
|
||||
if (code == '6') { rgb[0] = 1.0f; rgb[1] = 0.66f; rgb[2] = 0.0f; }
|
||||
if (code == 'e') { rgb[0] = 1.0f; rgb[1] = 1.0f; rgb[2] = 0.33f; }
|
||||
if (code == '2') { rgb[0] = 0.0f; rgb[1] = 0.66f; rgb[2] = 0.0f; }
|
||||
if (code == 'a') { rgb[0] = 0.33f; rgb[1] = 1.0f; rgb[2] = 0.33f; }
|
||||
if (code == 'b') { rgb[0] = 0.33f; rgb[1] = 1.0f; rgb[2] = 1.0f; }
|
||||
if (code == '3') { rgb[0] = 0.0f; rgb[1] = 0.66f; rgb[2] = 0.66f; }
|
||||
if (code == '1') { rgb[0] = 0.0f; rgb[1] = 0.0f; rgb[2] = 0.66f; }
|
||||
if (code == '9') { rgb[0] = 0.33f; rgb[1] = 0.33f; rgb[2] = 1.0f; }
|
||||
if (code == 'd') { rgb[0] = 1.0f; rgb[1] = 0.33f; rgb[2] = 1.0f; }
|
||||
if (code == '5') { rgb[0] = 0.66f; rgb[1] = 0.0f; rgb[2] = 0.66f; }
|
||||
if (code == 'f') { rgb[0] = 1.0f; rgb[1] = 1.0f; rgb[2] = 1.0f; }
|
||||
if (code == '7') { rgb[0] = 0.66f; rgb[1] = 0.66f; rgb[2] = 0.66f; }
|
||||
if (code == '8') { rgb[0] = 0.33f; rgb[1] = 0.33f; rgb[2] = 0.33f; }
|
||||
if (code == '0') { rgb[0] = 0.0f; rgb[1] = 0.0f; rgb[2] = 0.0f; }
|
||||
}
|
||||
|
||||
public void clearCaches() {
|
||||
this.stringWidthCache.clear();
|
||||
this.strippedStringCache.clear();
|
||||
}
|
||||
|
||||
public void drawString(String text, double x, double y, Color color) {
|
||||
drawString(text, x, y, color.getRGB());
|
||||
}
|
||||
|
||||
public int getStrippedStringCacheSize() {
|
||||
return this.strippedStringCache.size();
|
||||
}
|
||||
|
||||
public void drawCenteredString(String text, double x, double y, int color) {
|
||||
drawString(text, x - getStringWidth(text) / 2.0, y, color);
|
||||
}
|
||||
|
||||
public void setFontSelectorEnabled(boolean enabled) {
|
||||
this.fontSelectorEnabled = enabled;
|
||||
}
|
||||
|
||||
public abstract void drawStringWithShadow(String text, double x, double y, int color);
|
||||
|
||||
public abstract void renderString(String text, double x, double y, int color, boolean fallbackToVanilla);
|
||||
|
||||
public double getStringWidth(String text) {
|
||||
return getStringWidth(text, false);
|
||||
}
|
||||
|
||||
public double getStringHeight(String text) {
|
||||
return getStringHeight(text, false);
|
||||
}
|
||||
|
||||
public void drawString(String text, double x, double y, Color color, boolean fallbackToVanilla) {
|
||||
int rgb = color.getRGB();
|
||||
renderString(text, x, y, rgb, fallbackToVanilla);
|
||||
}
|
||||
|
||||
public abstract double getStringHeight(String text, boolean fallbackToVanilla);
|
||||
|
||||
public void drawStringStripped(String text, double x, double y, int color, boolean shadow) {
|
||||
String stripped = this.strippedStringCache.get(text);
|
||||
if (stripped == null) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
char[] chars = text.toCharArray();
|
||||
for (int i = 0; i < chars.length; ++i) {
|
||||
char c = chars[i];
|
||||
if (c == '\u00a7') {
|
||||
++i;
|
||||
continue;
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
stripped = sb.toString();
|
||||
this.strippedStringCache.put(text, stripped);
|
||||
}
|
||||
text = stripped;
|
||||
renderString(text, x, y, color, shadow);
|
||||
}
|
||||
|
||||
public int getWidthCacheSize() {
|
||||
return this.stringWidthCache.size();
|
||||
}
|
||||
|
||||
public void drawStringWithShadow(String text, double x, double y, Color color) {
|
||||
drawString(text, x + 0.5, y + 0.5, Integer.MIN_VALUE);
|
||||
drawString(text, x, y, color.getRGB());
|
||||
}
|
||||
|
||||
public abstract void renderStringWithShadow(String text, double x, double y, Color color, Color shadowColor, boolean fallbackToVanilla);
|
||||
|
||||
public void drawCenteredString(String text, double x, double y, Color color) {
|
||||
drawCenteredString(text, x, y, color.getRGB());
|
||||
}
|
||||
|
||||
public abstract void drawStringWithShadow(String text, double x, double y, Color color, boolean fallbackToVanilla);
|
||||
|
||||
public int getFontSize() {
|
||||
return this.fontSize;
|
||||
}
|
||||
|
||||
public void drawStringWithShadow(String text, double x, double y, Color color, Color shadowColor) {
|
||||
renderStringWithShadow(text, x, y, color, shadowColor, false);
|
||||
}
|
||||
|
||||
public SmoothFontRenderer() {
|
||||
this.strippedStringCache = new SmoothFontRendererStringCache(this, 16, 0.75f, true);
|
||||
}
|
||||
|
||||
public abstract double getStringWidth(String text, boolean fallbackToVanilla);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package cn.xiaozhou233.orangex.font;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SmoothFontRendererStringCache extends LinkedHashMap<String, String> {
|
||||
final SmoothFontRenderer L;
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<String, String> entry) {
|
||||
return this.size() > 5120;
|
||||
}
|
||||
|
||||
public SmoothFontRendererStringCache(SmoothFontRenderer smoothFontRenderer, int n, float f, boolean bl) {
|
||||
super(n, f, bl);
|
||||
this.L = smoothFontRenderer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package cn.xiaozhou233.orangex.font;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SmoothFontRendererWidthCache extends LinkedHashMap<String, Double> {
|
||||
final SmoothFontRenderer s;
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<String, Double> entry) {
|
||||
return this.size() > 5120;
|
||||
}
|
||||
|
||||
public SmoothFontRendererWidthCache(SmoothFontRenderer smoothFontRenderer, int n, float f, boolean bl) {
|
||||
super(n, f, bl);
|
||||
this.s = smoothFontRenderer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package cn.xiaozhou233.orangex.font;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
public class StbFontManager {
|
||||
private byte[] proximaData;
|
||||
private byte[] proximaBoldData;
|
||||
private byte[] notoData;
|
||||
private byte[] poppinsData;
|
||||
|
||||
private final ConcurrentMap<Integer, SmoothFontRenderer> regularFonts = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<Integer, SmoothFontRenderer> boldFonts = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<Integer, SmoothFontRenderer> notoFonts = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<Integer, SmoothFontRenderer> poppinsFonts = new ConcurrentHashMap<>();
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
public void loadFonts() {
|
||||
this.proximaData = readResource("proxima.ttf");
|
||||
this.proximaBoldData = readResource("proximabd.ttf");
|
||||
this.notoData = readResource("noto.ttf");
|
||||
this.poppinsData = readResource("poppins_rg.ttf");
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
private byte[] readResource(String path) {
|
||||
try {
|
||||
InputStream is = StbFontManager.class.getClassLoader().getResourceAsStream("fonts/" + path);
|
||||
if (is == null) return null;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[8192];
|
||||
int len;
|
||||
while ((len = is.read(buf)) != -1) {
|
||||
baos.write(buf, 0, len);
|
||||
}
|
||||
is.close();
|
||||
return baos.toByteArray();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public SmoothFontRenderer getProxima(int size) {
|
||||
return getFont(regularFonts, proximaData, size, "Proxima");
|
||||
}
|
||||
|
||||
public SmoothFontRenderer getProximaBold(int size) {
|
||||
return getFont(boldFonts, proximaBoldData, size, "Proxima Bold");
|
||||
}
|
||||
|
||||
public SmoothFontRenderer getNoto(int size) {
|
||||
return getFont(notoFonts, notoData, size, "Noto");
|
||||
}
|
||||
|
||||
public SmoothFontRenderer getPoppins(int size) {
|
||||
return getFont(poppinsFonts, poppinsData, size, "Poppins");
|
||||
}
|
||||
|
||||
private SmoothFontRenderer getFont(ConcurrentMap<Integer, SmoothFontRenderer> cache, byte[] data, int size, String name) {
|
||||
if (!cache.containsKey(size)) {
|
||||
if (data == null) return null;
|
||||
LegacySmoothFontRenderer renderer = new LegacySmoothFontRenderer(data, size, name);
|
||||
cache.put(size, renderer);
|
||||
return renderer;
|
||||
}
|
||||
return cache.get(size);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package cn.xiaozhou233.orangex.font.shader;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
public class ShaderProgram {
|
||||
private static int currentProgramId = -1;
|
||||
int previousProgramId;
|
||||
int vertexShaderId = 0;
|
||||
int fragmentShaderId = 0;
|
||||
boolean linked;
|
||||
int programId = GL20.glCreateProgram();
|
||||
|
||||
public ShaderProgram(String vertexShaderSource, String fragmentShaderSource) {
|
||||
this.vertexShaderId = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
|
||||
this.fragmentShaderId = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
|
||||
|
||||
GL20.glShaderSource(this.vertexShaderId, vertexShaderSource);
|
||||
GL20.glShaderSource(this.fragmentShaderId, fragmentShaderSource);
|
||||
GL20.glCompileShader(this.vertexShaderId);
|
||||
GL20.glCompileShader(this.fragmentShaderId);
|
||||
|
||||
GL20.glAttachShader(this.programId, this.vertexShaderId);
|
||||
GL20.glAttachShader(this.programId, this.fragmentShaderId);
|
||||
GL20.glLinkProgram(this.programId);
|
||||
|
||||
IntBuffer linkStatus = BufferUtils.createIntBuffer(1);
|
||||
GL20.glGetProgram(this.programId, GL20.GL_LINK_STATUS, linkStatus);
|
||||
this.linked = linkStatus.get(0) == 1;
|
||||
}
|
||||
|
||||
public boolean isLinked() {
|
||||
return this.linked;
|
||||
}
|
||||
|
||||
public int getProgramId() {
|
||||
return this.programId;
|
||||
}
|
||||
|
||||
public void restorePreviousProgram() {
|
||||
useProgram(this.previousProgramId);
|
||||
}
|
||||
|
||||
public static void useProgram(int programId) {
|
||||
GL20.glUseProgram(programId);
|
||||
currentProgramId = programId;
|
||||
}
|
||||
|
||||
public static int getCurrentProgramId() {
|
||||
if (currentProgramId == -1) {
|
||||
currentProgramId = GL11.glGetInteger(GL20.GL_CURRENT_PROGRAM);
|
||||
}
|
||||
return currentProgramId;
|
||||
}
|
||||
|
||||
public boolean bind() {
|
||||
if (!this.linked) return false;
|
||||
this.previousProgramId = getCurrentProgramId();
|
||||
useProgram(this.programId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public final class StbByteBufferView {
|
||||
public byte[] f;
|
||||
public int y;
|
||||
public int C;
|
||||
public int h;
|
||||
|
||||
public StbByteBufferView() {
|
||||
this(null, 0, 0);
|
||||
}
|
||||
|
||||
public StbByteBufferView(byte[] data, int offset, int length) {
|
||||
this.f = data;
|
||||
this.C = offset;
|
||||
this.h = 0;
|
||||
this.y = length;
|
||||
}
|
||||
|
||||
public StbByteBufferView k() {
|
||||
StbByteBufferView copy = new StbByteBufferView();
|
||||
copy.f = this.f;
|
||||
copy.C = this.C;
|
||||
copy.h = this.h;
|
||||
copy.y = this.y;
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeActiveEdge {
|
||||
public float j;
|
||||
public float e;
|
||||
public float N;
|
||||
public float m;
|
||||
public StbTrueTypeActiveEdge l;
|
||||
public float T;
|
||||
public float L;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeAlignedQuad {
|
||||
public float K;
|
||||
public float G;
|
||||
public float C;
|
||||
public float x;
|
||||
public float v;
|
||||
public float M;
|
||||
public float O;
|
||||
public float S;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeBakedChar {
|
||||
public int V;
|
||||
public float k;
|
||||
public float l;
|
||||
public int D;
|
||||
public float B;
|
||||
public int U;
|
||||
public int Q;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeBitmap {
|
||||
public byte[] E;
|
||||
public int c;
|
||||
public int K;
|
||||
public int h;
|
||||
public int A;
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeCharStringContext {
|
||||
public int Z;
|
||||
public int c;
|
||||
public int q;
|
||||
public float p;
|
||||
public int v;
|
||||
public float o;
|
||||
public float s;
|
||||
public float C;
|
||||
public int J;
|
||||
public int y;
|
||||
public int f;
|
||||
public StbTrueTypeVertex[] V;
|
||||
|
||||
public StbTrueTypeCharStringContext(int n) {
|
||||
this.J = n;
|
||||
this.v = 0;
|
||||
this.s = 0.0f;
|
||||
this.p = 0.0f;
|
||||
this.C = 0.0f;
|
||||
this.o = 0.0f;
|
||||
this.y = 0;
|
||||
this.c = 0;
|
||||
this.Z = 0;
|
||||
this.f = 0;
|
||||
this.V = null;
|
||||
this.q = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeEdge {
|
||||
public float u;
|
||||
public float d;
|
||||
public int W;
|
||||
public float X;
|
||||
public float i;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeFontInfo {
|
||||
public int h;
|
||||
public int G;
|
||||
public StbByteBufferView H;
|
||||
public int Y;
|
||||
public int L;
|
||||
public int Q;
|
||||
public int U;
|
||||
public StbByteBufferView g = new StbByteBufferView();
|
||||
public int c;
|
||||
public StbByteBufferView j;
|
||||
public byte[] S;
|
||||
public StbByteBufferView z = new StbByteBufferView();
|
||||
public StbByteBufferView J;
|
||||
public int d;
|
||||
public int T;
|
||||
public int w;
|
||||
public StbByteBufferView N;
|
||||
public int v;
|
||||
public int p;
|
||||
|
||||
public StbTrueTypeFontInfo() {
|
||||
this.j = new StbByteBufferView();
|
||||
this.J = new StbByteBufferView();
|
||||
this.H = new StbByteBufferView();
|
||||
this.N = new StbByteBufferView();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeGlyphPackNode {
|
||||
public int z;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeKerningEntry {
|
||||
public int y;
|
||||
public int q;
|
||||
public int o;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypePackAtlasState {
|
||||
public int F;
|
||||
public int a;
|
||||
public int n;
|
||||
public int f;
|
||||
public int t;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypePackContext {
|
||||
public int d;
|
||||
public StbTrueTypeGlyphPackNode[] j;
|
||||
public int m;
|
||||
public int h;
|
||||
public int G;
|
||||
public int b;
|
||||
public StbTrueTypePackAtlasState J;
|
||||
public int c;
|
||||
public int w;
|
||||
public byte[] X;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypePackRange {
|
||||
public int y;
|
||||
public int R;
|
||||
public int[] N;
|
||||
public StbTrueTypePackedChar[] M;
|
||||
public int i;
|
||||
public float E;
|
||||
private static String[] j;
|
||||
public int b;
|
||||
|
||||
public static void t(String[] stringArray) {
|
||||
j = stringArray;
|
||||
}
|
||||
|
||||
public static String[] x() {
|
||||
return j;
|
||||
}
|
||||
|
||||
static {
|
||||
if (StbTrueTypePackRange.x() != null) {
|
||||
StbTrueTypePackRange.t(new String[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypePackRectangle {
|
||||
public int e;
|
||||
public int J;
|
||||
public int P;
|
||||
public int M;
|
||||
public int N;
|
||||
public int q;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypePackedChar {
|
||||
public int p;
|
||||
public int q;
|
||||
public float R;
|
||||
public int k;
|
||||
public float A;
|
||||
public int b;
|
||||
public float G;
|
||||
public float t;
|
||||
public float s;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypePoint {
|
||||
public float E;
|
||||
public float M;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.xiaozhou233.orangex.font.stb;
|
||||
|
||||
public class StbTrueTypeVertex {
|
||||
public short h;
|
||||
public int O;
|
||||
public short H;
|
||||
public short a;
|
||||
public short D;
|
||||
public short s;
|
||||
public int v;
|
||||
public short f;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package cn.xiaozhou233.orangex.mixin.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.OrangeX;
|
||||
import cn.xiaozhou233.orangex.event.impl.EventRender2D;
|
||||
import cn.xiaozhou233.orangex.font.SmoothFontRenderer;
|
||||
import cn.yapeteam.ymixin.annotations.Inject;
|
||||
import cn.yapeteam.ymixin.annotations.Local;
|
||||
import cn.yapeteam.ymixin.annotations.Mixin;
|
||||
@@ -34,7 +35,9 @@ public class MixinGuiIngame {
|
||||
mc.entityRenderer.setupOverlayRendering();
|
||||
|
||||
OrangeX.getInstance().getEventBus().post(new EventRender2D(partialTicks));
|
||||
OrangeX.getInstance().getFontManager().JelloMedium32.drawString("OrangeX", 10, 10, 0xFFA500);
|
||||
SmoothFontRenderer font = OrangeX.getInstance().getStbFontManager().getProxima(18);
|
||||
font.drawString("Hello World!", 50, 50, 0xFFFFFFFF);
|
||||
|
||||
|
||||
GlStateManager.enableDepth();
|
||||
GlStateManager.disableBlend();
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user