diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java b/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java new file mode 100644 index 0000000..388911d --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java @@ -0,0 +1,47 @@ +package cc.polyfrost.oneconfig.renderer.font; + +import cc.polyfrost.oneconfig.utils.IOUtils; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import static org.lwjgl.nanovg.NanoVG.nvgCreateFontMem; + +public class FontManager { + public static FontManager INSTANCE = new FontManager(); + + /** + * Load all fonts in the Fonts class + * + * @param vg NanoVG context + */ + + public void initialize(long vg) { + for (Fonts fonts : Fonts.values()) { + loadFont(vg, fonts.font); + } + } + + /** + * Load a font into NanoVG + * + * @param vg NanoVG context + * @param font The font to be loaded + */ + public void loadFont(long vg, Font font) { + if (font.isLoaded()) return; + int loaded = -1; + try { + ByteBuffer buffer = IOUtils.resourceToByteBuffer(font.getFileName()); + loaded = nvgCreateFontMem(vg, font.getName(), buffer, 0); + font.setBuffer(buffer); + } catch (IOException e) { + e.printStackTrace(); + } + if (loaded == -1) { + throw new RuntimeException("Failed to initialize font " + font.getName()); + } else { + font.setLoaded(true); + } + } +} |