blob: 79ac5180146a5ca9d84a808e291169b8fb4b7e44 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package cc.polyfrost.oneconfig.renderer.font;
import cc.polyfrost.oneconfig.utils.IOUtils;
import java.io.IOException;
import java.lang.reflect.Field;
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 (Field field : Fonts.class.getDeclaredFields()) {
try {
field.setAccessible(true);
Object font = field.get(null);
if (!(font instanceof Font)) continue;
loadFont(vg, (Font) font);
} catch (Exception e) {
throw new RuntimeException("Could not initialize fonts");
}
}
}
/**
* 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);
}
}
}
|