|
1 | 1 | import imgui.ImBool;
|
| 2 | +import imgui.ImFontAtlas; |
| 3 | +import imgui.ImFontConfig; |
2 | 4 | import imgui.ImGui;
|
3 | 5 | import imgui.ImGuiIO;
|
4 | 6 | import imgui.ImString;
|
|
17 | 19 | import org.lwjgl.opengl.GL;
|
18 | 20 | import org.lwjgl.system.MemoryStack;
|
19 | 21 |
|
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.io.UncheckedIOException; |
20 | 26 | import java.nio.IntBuffer;
|
21 | 27 | import java.util.Objects;
|
22 | 28 |
|
@@ -230,6 +236,33 @@ public String get() {
|
230 | 236 | }
|
231 | 237 | });
|
232 | 238 |
|
| 239 | + // ------------------------------------------------------------ |
| 240 | + // Fonts configuration |
| 241 | + |
| 242 | + final ImFontAtlas fontAtlas = io.getFonts(); |
| 243 | + |
| 244 | + // Dear ImGui uses 'ProggyClean.ttf, 13px' by default |
| 245 | + fontAtlas.addFontDefault(); |
| 246 | + |
| 247 | + final ImFontConfig fontConfig = new ImFontConfig(); // Keep in mind that creation of the ImFontConfig will allocate native memory |
| 248 | + fontConfig.setRasterizerMultiply(1.2f); // This will make fonts a bit more readable |
| 249 | + fontConfig.setGlyphRanges(fontAtlas.getGlyphRangesCyrillic()); // Additional glyphs could be added like here or in addFontFrom*() methods |
| 250 | + |
| 251 | + // We can add new fonts directly from file |
| 252 | + fontAtlas.addFontFromFileTTF("imgui-lwjgl3/src/test/resources/DroidSans.ttf", 13, fontConfig); |
| 253 | + fontAtlas.addFontFromFileTTF("imgui-lwjgl3/src/test/resources/DroidSans.ttf", 14, fontConfig); |
| 254 | + fontAtlas.addFontFromFileTTF("imgui-lwjgl3/src/test/resources/JetBrainsMono-Regular.ttf", 13, fontConfig); |
| 255 | + fontAtlas.addFontFromFileTTF("imgui-lwjgl3/src/test/resources/JetBrainsMono-Regular.ttf", 14, fontConfig); |
| 256 | + |
| 257 | + // Or directly from memory |
| 258 | + fontConfig.setName("Roboto-Regular.ttf, 13px"); // This name will be displayed in Style Editor |
| 259 | + fontAtlas.addFontFromMemoryTTF(loadFromResources("Roboto-Regular.ttf"), 13, fontConfig); |
| 260 | + fontConfig.setName("Roboto-Regular.ttf, 14px"); // We can apply a new config value every time we add a new font |
| 261 | + fontAtlas.addFontFromMemoryTTF(loadFromResources("Roboto-Regular.ttf"), 14, fontConfig); |
| 262 | + |
| 263 | + // After fonts were added and since we won't use fontConfig again - we should clean it |
| 264 | + fontConfig.destroy(); |
| 265 | + |
233 | 266 | // IMPORTANT!!!
|
234 | 267 | // Method initializes renderer itself.
|
235 | 268 | // This method SHOULD be called after you've initialized your ImGui configuration (fonts and so on).
|
@@ -380,6 +413,23 @@ private void destroyGlfw() {
|
380 | 413 | Objects.requireNonNull(glfwSetErrorCallback(null)).free();
|
381 | 414 | }
|
382 | 415 |
|
| 416 | + private byte[] loadFromResources(final String fileName) { |
| 417 | + try (InputStream is = Objects.requireNonNull(ImGuiGlfwExample.class.getClassLoader().getResourceAsStream(fileName)); |
| 418 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { |
| 419 | + |
| 420 | + final byte[] data = new byte[16384]; |
| 421 | + |
| 422 | + int nRead; |
| 423 | + while ((nRead = is.read(data, 0, data.length)) != -1) { |
| 424 | + buffer.write(data, 0, nRead); |
| 425 | + } |
| 426 | + |
| 427 | + return buffer.toByteArray(); |
| 428 | + } catch (IOException e) { |
| 429 | + throw new UncheckedIOException(e); |
| 430 | + } |
| 431 | + } |
| 432 | + |
383 | 433 | public static void main(final String[] args) {
|
384 | 434 | new ImGuiGlfwExample().run();
|
385 | 435 | }
|
|
0 commit comments