Skip to content

Commit d7bb91d

Browse files
committed
[fonts] Add example of how to work with fonts
1 parent c8931af commit d7bb91d

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

imgui-binding/src/main/java/imgui/ImFontConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ public final class ImFontConfig implements ImDestroyable {
1212
public ImFontConfig() {
1313
ImGui.touch();
1414
ptr = nCreate();
15+
setFontDataOwnedByAtlas(false); // Read method javadoc
1516
}
1617

1718
ImFontConfig(final long ptr) {
1819
this.ptr = ptr;
20+
setFontDataOwnedByAtlas(false); // Read method javadoc
1921
}
2022

2123
@Override
@@ -86,6 +88,10 @@ public void destroy() {
8688

8789
/**
8890
* TTF/OTF data ownership taken by the container ImFontAtlas (will delete memory itself).
91+
* <p>
92+
* BINDING NOTICE: By default binding will set this value to <b>false</b>.
93+
* If this is not done, Dear ImGui will try to free memory allocated by JVM to store fonts data while running {@link ImGui#destroyContext()}.
94+
* This will result into a native exception, since JVM by itself controls it's own resources.
8995
*/
9096
public native void setFontDataOwnedByAtlas(boolean isFontDataOwnedByAtlas); /*
9197
IM_FONT_CONFIG->FontDataOwnedByAtlas = isFontDataOwnedByAtlas;

imgui-lwjgl3/src/test/java/ImGuiGlfwExample.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import imgui.ImBool;
2+
import imgui.ImFontAtlas;
3+
import imgui.ImFontConfig;
24
import imgui.ImGui;
35
import imgui.ImGuiIO;
46
import imgui.ImString;
@@ -17,6 +19,10 @@
1719
import org.lwjgl.opengl.GL;
1820
import org.lwjgl.system.MemoryStack;
1921

22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.io.UncheckedIOException;
2026
import java.nio.IntBuffer;
2127
import java.util.Objects;
2228

@@ -230,6 +236,33 @@ public String get() {
230236
}
231237
});
232238

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+
233266
// IMPORTANT!!!
234267
// Method initializes renderer itself.
235268
// This method SHOULD be called after you've initialized your ImGui configuration (fonts and so on).
@@ -380,6 +413,23 @@ private void destroyGlfw() {
380413
Objects.requireNonNull(glfwSetErrorCallback(null)).free();
381414
}
382415

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+
383433
public static void main(final String[] args) {
384434
new ImGuiGlfwExample().run();
385435
}
186 KB
Binary file not shown.
127 KB
Binary file not shown.
155 KB
Binary file not shown.

0 commit comments

Comments
 (0)