From 0457ac44708a6f96ff25457015417fce1e6aa021 Mon Sep 17 00:00:00 2001 From: zbx1425 Date: Fri, 14 Jun 2024 23:23:19 +0800 Subject: [PATCH] Fix MTR's sans font overridden with Noto Sans CJK causing draw issue --- .../render/scripting/ScriptResourceUtil.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/ScriptResourceUtil.java b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/ScriptResourceUtil.java index be170c2a..1d4d0936 100644 --- a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/ScriptResourceUtil.java +++ b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/ScriptResourceUtil.java @@ -126,26 +126,34 @@ public static String readString(ResourceLocation identifier) { private static final ResourceLocation NOTO_SANS_LOCATION = new ResourceLocation(mtr.MTR.MOD_ID, "font/noto-sans-semibold.ttf"); private static final ResourceLocation NOTO_SERIF_LOCATION = new ResourceLocation(mtr.MTR.MOD_ID, "font/noto-serif-cjk-tc-semibold.ttf"); private static boolean hasNotoSansCjk = false; + private static Font NOTO_SANS_MAYBE_CJK; public static Font getSystemFont(String fontName) { ClientCacheAccessor clientCache = (ClientCacheAccessor) ClientData.DATA_CACHE; ResourceManager resourceManager = Minecraft.getInstance().getResourceManager(); switch (fontName) { case "Noto Sans" -> { - if (clientCache.getFont() == null || (hasNotoSansCjk && !clientCache.getFont().canDisplay('草'))) { - try { - if (hasNotoSansCjk) { - clientCache.setFont(Font.createFont(Font.TRUETYPE_FONT, - Utilities.getInputStream(resourceManager.getResource(NOTO_SANS_CJK_LOCATION)))); - } else { - clientCache.setFont(Font.createFont(Font.TRUETYPE_FONT, - Utilities.getInputStream(resourceManager.getResource(NOTO_SANS_LOCATION)))); + if (NOTO_SANS_MAYBE_CJK == null) { + if (hasNotoSansCjk) { + try { + NOTO_SANS_MAYBE_CJK = Font.createFont(Font.TRUETYPE_FONT, + Utilities.getInputStream(resourceManager.getResource(NOTO_SANS_CJK_LOCATION))); + } catch (Exception ex) { + Main.LOGGER.warn("Failed loading font", ex); } - } catch (Exception ex) { - Main.LOGGER.warn("Failed loading font", ex); + } else { + if (clientCache.getFont() == null) { + try { + clientCache.setFont(Font.createFont(Font.TRUETYPE_FONT, + Utilities.getInputStream(resourceManager.getResource(NOTO_SANS_LOCATION)))); + } catch (Exception ex) { + Main.LOGGER.warn("Failed loading font", ex); + } + } + NOTO_SANS_MAYBE_CJK = clientCache.getFont(); } } - return clientCache.getFont(); + return NOTO_SANS_MAYBE_CJK; } case "Noto Serif" -> { if (clientCache.getFontCjk() == null) {