drop font file loading, resolve fonts by name only
the Font setting already accepts java logical fonts and any installed system family by typing it, so the fros/fonts folder machinery was dead weight: file resolution, the auto-created directory, and the 2-second retry for files dropped in later are all gone. an unknown name now just logs "Font '<name>' not found" once per change and falls back to the vanilla font. the empty fros/fonts directory was removed from the game dir as well
This commit is contained in:
@@ -2,7 +2,6 @@ package cc.fros.util.render;
|
||||
|
||||
import cc.fros.features.modules.client.ClickGuiModule;
|
||||
import com.mojang.blaze3d.platform.NativeImage;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.client.gui.GuiGraphics;
|
||||
import net.minecraft.client.renderer.RenderPipelines;
|
||||
import net.minecraft.client.renderer.texture.DynamicTexture;
|
||||
@@ -18,9 +17,6 @@ import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -28,18 +24,17 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static cc.fros.util.traits.Util.mc;
|
||||
|
||||
/**
|
||||
* Renders client UI text with a user supplied ttf/otf font.
|
||||
* Renders client UI text with a font picked by name in the ClickGui
|
||||
* settings (java's logical fonts or any installed system family).
|
||||
* Text is rasterized with AWT and cached as textures; when no font
|
||||
* is configured everything falls through to the vanilla font.
|
||||
* resolves everything falls through to the vanilla font.
|
||||
*/
|
||||
public final class FrosFont {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger("FrosFont");
|
||||
private static final Path FONT_DIR = FabricLoader.getInstance().getGameDir().resolve("fros").resolve("fonts");
|
||||
private static final int MAX_CACHE = 400;
|
||||
|
||||
private static final int[] PALETTE = {
|
||||
@@ -85,7 +80,6 @@ public final class FrosFont {
|
||||
private static float loadedScale = 1;
|
||||
/** raster pixels from the top of a line box to the middle of the cap band */
|
||||
private static float visualCenterRaster = 4f;
|
||||
private static long lastAttempt;
|
||||
private static int glyphCounter;
|
||||
|
||||
private FrosFont() {
|
||||
@@ -159,47 +153,28 @@ public final class FrosFont {
|
||||
if (name == null) name = "";
|
||||
float scale = currentScale();
|
||||
|
||||
boolean changed = !Objects.equals(name, loadedName) || size != loadedSize || scale != loadedScale;
|
||||
if (!changed) {
|
||||
boolean missing = awtFont == null && !name.isBlank();
|
||||
// retry a missing file every couple of seconds so dropping one
|
||||
// into the folder loads without touching the setting again
|
||||
if (!missing || System.currentTimeMillis() - lastAttempt < 2000) return;
|
||||
if (!Objects.equals(name, loadedName) || size != loadedSize || scale != loadedScale) {
|
||||
reset(name, size, scale);
|
||||
}
|
||||
reset(name, size, scale, changed);
|
||||
}
|
||||
|
||||
private static void reset(String name, float size, float scale, boolean verbose) {
|
||||
private static void reset(String name, float size, float scale) {
|
||||
loadedName = name;
|
||||
loadedSize = size;
|
||||
loadedScale = scale;
|
||||
lastAttempt = System.currentTimeMillis();
|
||||
awtFont = null;
|
||||
metrics = null;
|
||||
clearGlyphs();
|
||||
|
||||
try {
|
||||
Files.createDirectories(FONT_DIR);
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Could not create font directory {}", FONT_DIR, e);
|
||||
return;
|
||||
}
|
||||
if (name.isBlank()) return;
|
||||
try {
|
||||
Path file = resolveFontFile(name);
|
||||
if (file != null) {
|
||||
applyFont(java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, file.toFile()),
|
||||
size * scale, "Loaded font " + file.getFileName(), verbose);
|
||||
return;
|
||||
}
|
||||
|
||||
java.awt.Font installed = installedFont(name);
|
||||
if (installed != null) {
|
||||
applyFont(installed, size * scale, "Using system font '" + name + "'", verbose);
|
||||
applyFont(installed, size * scale, "Using system font '" + name + "'", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (verbose) LOGGER.warn("Font '{}' not found in {}", name, FONT_DIR);
|
||||
LOGGER.warn("Font '{}' not found", name);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Failed to load font '{}'", name, e);
|
||||
awtFont = null;
|
||||
@@ -259,36 +234,6 @@ public final class FrosFont {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Path resolveFontFile(String name) {
|
||||
try {
|
||||
Files.createDirectories(FONT_DIR);
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Could not create font directory {}", FONT_DIR, e);
|
||||
return null;
|
||||
}
|
||||
|
||||
String lower = name.toLowerCase(Locale.ROOT);
|
||||
if (lower.endsWith(".ttf") || lower.endsWith(".otf")) {
|
||||
Path direct = FONT_DIR.resolve(name);
|
||||
return Files.isRegularFile(direct) ? direct : null;
|
||||
}
|
||||
|
||||
try (Stream<Path> files = Files.list(FONT_DIR)) {
|
||||
return files.filter(Files::isRegularFile)
|
||||
.filter(path -> {
|
||||
String fileName = path.getFileName().toString().toLowerCase(Locale.ROOT);
|
||||
return fileName.equals(lower)
|
||||
|| fileName.equals(lower + ".ttf")
|
||||
|| fileName.equals(lower + ".otf");
|
||||
})
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Could not list font directory {}", FONT_DIR, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void clearGlyphs() {
|
||||
for (Glyph glyph : new ArrayList<>(GLYPHS.values())) {
|
||||
queueRelease(glyph.id());
|
||||
|
||||
Reference in New Issue
Block a user