Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/main/java/meteordevelopment/meteorclient/renderer/Fonts.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import meteordevelopment.meteorclient.utils.render.FontUtils;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand All @@ -25,6 +27,23 @@

public class Fonts {
public static final String[] BUILTIN_FONTS = {"JetBrains Mono", "Comfortaa", "Tw Cen MT", "Pixelation"};
private static final String[] FALLBACK_FONT_FAMILIES = {
"Noto Sans SC",
"Microsoft YaHei",
"Microsoft YaHei UI",
"DengXian",
"SimHei",
"SimSun",
"KaiTi",
"Microsoft JhengHei",
"Malgun Gothic",
"Yu Gothic",
"MS Gothic",
"Source Han Sans SC",
"WenQuanYi Zen Hei",
"PingFang SC",
"Hiragino Sans GB"
};

public static String DEFAULT_FONT_FAMILY;
public static FontFace DEFAULT_FONT;
Expand Down Expand Up @@ -90,4 +109,57 @@ public static FontFamily getFamily(String name) {

return null;
}

public static List<FontFace> getFallbackFonts(FontFace primary) {
List<FontFace> fonts = new ArrayList<>();

for (String familyName : FALLBACK_FONT_FAMILIES) {
FontFace font = getFallbackFont(familyName, primary);
if (font != null) {
fonts.add(font);
break;
}
}

return fonts;
}

public static List<ByteBuffer> readFontBuffers(FontFace primary) throws IOException {
List<ByteBuffer> buffers = new ArrayList<>();
buffers.add(primary.readToDirectByteBuffer());

for (FontFace fallbackFont : getFallbackFonts(primary)) {
try {
buffers.add(fallbackFont.readToDirectByteBuffer());
} catch (IOException e) {
MeteorClient.LOG.warn("Failed to load fallback font: {}", fallbackFont, e);
}
}

return buffers;
}

private static FontFace getFallbackFont(String familyName, FontFace primary) {
FontFamily family = getFamily(familyName);

if (family == null) {
String needle = familyName.toLowerCase();

for (FontFamily fontFamily : FONT_FAMILIES) {
if (fontFamily.getName().toLowerCase().contains(needle)) {
family = fontFamily;
break;
}
}
}

if (family == null) return null;

for (FontInfo.Type type : FontInfo.Type.values()) {
FontFace font = family.get(type);
if (font != null && !font.info.equals(primary.info)) return font;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package meteordevelopment.meteorclient.renderer.text;

import meteordevelopment.meteorclient.renderer.Fonts;
import meteordevelopment.meteorclient.renderer.MeshBuilder;
import meteordevelopment.meteorclient.renderer.MeshRenderer;
import meteordevelopment.meteorclient.renderer.MeteorRenderPipelines;
Expand All @@ -13,6 +14,7 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;

public class CustomTextRenderer implements TextRenderer {
public static final Color SHADOW_COLOR = new Color(60, 60, 60, 180);
Expand All @@ -32,11 +34,11 @@ public class CustomTextRenderer implements TextRenderer {
public CustomTextRenderer(FontFace fontFace) throws IOException {
this.fontFace = fontFace;

ByteBuffer buffer = fontFace.readToDirectByteBuffer();
List<ByteBuffer> buffers = Fonts.readFontBuffers(fontFace);

fonts = new Font[5];
for (int i = 0; i < fonts.length; i++) {
fonts[i] = new Font(buffer, (int) Math.round(27 * ((i * 0.5) + 1)));
fonts[i] = new Font(buffers, (int) Math.round(27 * ((i * 0.5) + 1)));
}
}

Expand Down Expand Up @@ -120,6 +122,7 @@ public void end() {

if (!scaleOnly) {
mesh.end();
font.uploadPendingGlyphs();

MeshRenderer.begin()
.attachments(Minecraft.getInstance().getMainRenderTarget())
Expand Down
Loading