use of net.minecraft.client.font.Font in project SpeedRunIGT by RedLime.
the class FontUtils method addFont.
public static void addFont(HashMap<Identifier, List<Font>> map, File file, File configFile) {
FileInputStream fileInputStream = null;
STBTTFontinfo sTBTTFontinfo = null;
ByteBuffer byteBuffer = null;
Throwable throwable = null;
try {
fileInputStream = new FileInputStream(file);
sTBTTFontinfo = STBTTFontinfo.malloc();
byteBuffer = TextureUtil.readAllToByteBuffer(fileInputStream);
byteBuffer.flip();
if (!STBTruetype.stbtt_InitFont(sTBTTFontinfo, byteBuffer)) {
return;
}
Identifier fontIdentifier = new Identifier(SpeedRunIGT.MOD_ID, file.getName().toLowerCase(Locale.ROOT).replace(".ttf", "").replaceAll(" ", "_").replaceAll("[^a-z0-9/._-]", ""));
ArrayList<Font> fontArrayList = new ArrayList<>();
FontConfigure fontConfigure;
if (configFile != null && configFile.exists()) {
fontConfigure = FontConfigure.fromJson(FileUtils.readFileToString(configFile, StandardCharsets.UTF_8));
} else {
fontConfigure = FontConfigure.create();
}
fontArrayList.add(new TrueTypeFont(byteBuffer, sTBTTFontinfo, fontConfigure.size, fontConfigure.oversample, fontConfigure.shift[0], fontConfigure.shift[1], fontConfigure.skip));
SpeedRunIGT.FONT_MAPS.put(fontIdentifier, new FontIdentifier(file, fontIdentifier, fontConfigure));
fontArrayList.add(new BlankFont());
map.put(fontIdentifier, fontArrayList);
} catch (FileNotFoundException e) {
if (sTBTTFontinfo != null)
sTBTTFontinfo.free();
MemoryUtil.memFree(byteBuffer);
} catch (IOException throwable1) {
throwable = throwable1;
} finally {
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (IOException e) {
if (throwable != null)
throwable.addSuppressed(e);
}
}
}
use of net.minecraft.client.font.Font in project SpeedRunIGT by RedLime.
the class MinecraftClientMixin method onInit.
/**
* Add import font system
*/
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/debug/DebugRenderer;<init>(Lnet/minecraft/client/MinecraftClient;)V", shift = At.Shift.BEFORE))
public void onInit(RunArgs args, CallbackInfo ci) {
this.resourceManager.registerListener(new SinglePreparationResourceReloadListener<Map<Identifier, List<Font>>>() {
@Override
protected Map<Identifier, List<Font>> prepare(ResourceManager manager, Profiler profiler) {
SpeedRunIGT.FONT_MAPS.clear();
try {
HashMap<Identifier, List<Font>> map = new HashMap<>();
File[] fontFiles = SpeedRunIGT.FONT_PATH.toFile().listFiles();
if (fontFiles == null)
return new HashMap<>();
for (File file : Arrays.stream(fontFiles).filter(file -> file.getName().endsWith(".ttf")).collect(Collectors.toList())) {
File config = SpeedRunIGT.FONT_PATH.resolve(file.getName().substring(0, file.getName().length() - 4) + ".json").toFile();
if (config.exists()) {
FontUtils.addFont(map, file, config);
} else {
FontUtils.addFont(map, file, null);
}
}
return map;
} catch (Throwable e) {
return new HashMap<>();
}
}
@Override
protected void apply(Map<Identifier, List<Font>> loader, ResourceManager manager, Profiler profiler) {
try {
FontManagerAccessor fontManager = (FontManagerAccessor) ((MinecraftClientAccessor) MinecraftClient.getInstance()).getFontManager();
for (Map.Entry<Identifier, List<Font>> listEntry : loader.entrySet()) {
FontStorage fontStorage = new FontStorage(fontManager.getTextureManager(), listEntry.getKey());
fontStorage.setFonts(listEntry.getValue());
fontManager.getFontStorages().put(listEntry.getKey(), fontStorage);
}
TimerDrawer.fontHeightMap.clear();
} catch (Throwable e) {
SpeedRunIGT.error("Error! failed import timer fonts!");
e.printStackTrace();
}
}
});
}
Aggregations