Search in sources :

Example 1 with Font

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);
        }
    }
}
Also used : TrueTypeFont(net.minecraft.client.font.TrueTypeFont) STBTTFontinfo(org.lwjgl.stb.STBTTFontinfo) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Font(net.minecraft.client.font.Font) BlankFont(net.minecraft.client.font.BlankFont) TrueTypeFont(net.minecraft.client.font.TrueTypeFont) Identifier(net.minecraft.util.Identifier) BlankFont(net.minecraft.client.font.BlankFont)

Example 2 with Font

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();
            }
        }
    });
}
Also used : MinecraftClientAccessor(com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor) HashMap(java.util.HashMap) ReloadableResourceManager(net.minecraft.resource.ReloadableResourceManager) ResourceManager(net.minecraft.resource.ResourceManager) Font(net.minecraft.client.font.Font) Identifier(net.minecraft.util.Identifier) Profiler(net.minecraft.util.profiler.Profiler) FontStorage(net.minecraft.client.font.FontStorage) FontManagerAccessor(com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

Font (net.minecraft.client.font.Font)2 Identifier (net.minecraft.util.Identifier)2 FontManagerAccessor (com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor)1 MinecraftClientAccessor (com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor)1 File (java.io.File)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BlankFont (net.minecraft.client.font.BlankFont)1 FontStorage (net.minecraft.client.font.FontStorage)1 TrueTypeFont (net.minecraft.client.font.TrueTypeFont)1 ReloadableResourceManager (net.minecraft.resource.ReloadableResourceManager)1 ResourceManager (net.minecraft.resource.ResourceManager)1 Profiler (net.minecraft.util.profiler.Profiler)1 STBTTFontinfo (org.lwjgl.stb.STBTTFontinfo)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1