Skip to content

Commit 1915d3c

Browse files
committed
Merge branch 'freetype'
2 parents 407d1b9 + a0a5b65 commit 1915d3c

File tree

9 files changed

+121
-35
lines changed

9 files changed

+121
-35
lines changed

bin/imgui-java.dll

735 KB
Binary file not shown.

bin/imgui-java64.dll

773 KB
Binary file not shown.

bin/libimgui-java.so

-60 Bytes
Binary file not shown.

bin/libimgui-java64.so

-24 Bytes
Binary file not shown.

buildSrc/src/main/groovy/imgui/generate/GenerateLibs.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class GenerateLibs extends DefaultTask {
2424
// Copy ImGui h/cpp files
2525
project.copy { CopySpec spec ->
2626
spec.from(project.rootProject.file('imgui')) { CopySpec it -> it.include('*.h', '*.cpp') }
27+
spec.from(project.rootProject.file('imgui/misc/freetype')) { CopySpec it -> it.include('*.h', '*.cpp') }
2728
spec.from(project.rootProject.file('imgui-binding/src/main/native'))
2829
spec.into(jniDir)
2930
}
@@ -37,6 +38,17 @@ class GenerateLibs extends DefaultTask {
3738
def linux32 = BuildTarget.newDefaultTarget(BuildTarget.TargetOs.Linux, false)
3839
def linux64 = BuildTarget.newDefaultTarget(BuildTarget.TargetOs.Linux, true)
3940

41+
// Freetype Deps Config
42+
win32.cppFlags += " -fstack-protector -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include"
43+
win32.libraries += "-lfreetype -lbz2 -lssp"
44+
win64.cppFlags += " -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include"
45+
win64.libraries += "-lfreetype -lbz2 -lssp"
46+
linux32.cppFlags += " -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include"
47+
linux32.linkerFlags += " -lfreetype"
48+
linux64.cppFlags += " -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include"
49+
linux64.linkerFlags += " -lfreetype"
50+
// End
51+
4052
new AntScriptGenerator().generate(buildConfig, win32, win64, linux32, linux64)
4153

4254
// Generate native libraries
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package imgui;
2+
3+
/**
4+
* Read: https://raw.githubusercontent.com/ocornut/imgui/v1.76/misc/freetype/README.md
5+
*/
6+
public final class ImGuiFreeType {
7+
private ImGuiFreeType() {
8+
}
9+
10+
/*JNI
11+
#include <imgui.h>
12+
#include <imgui_freetype.h>
13+
*/
14+
15+
public static void buildFontAtlas(final ImFontAtlas atlas) {
16+
nBuildFontAtlas(atlas.ptr, 0);
17+
}
18+
19+
public static void buildFontAtlas(final ImFontAtlas atlas, final int extraFlags) {
20+
nBuildFontAtlas(atlas.ptr, extraFlags);
21+
}
22+
23+
private static native void nBuildFontAtlas(long atlasPtr, int extraFlags); /*
24+
ImGuiFreeType::BuildFontAtlas((ImFontAtlas*)atlasPtr, (unsigned int)extraFlags);
25+
*/
26+
27+
/**
28+
* Hinting greatly impacts visuals (and glyph sizes).
29+
* When disabled, FreeType generates blurrier glyphs, more or less matches the stb's output.
30+
* The Default hinting mode usually looks good, but may distort glyphs in an unusual way.
31+
* The Light hinting mode generates fuzzier glyphs but better matches Microsoft's rasterizer.
32+
* <p>
33+
* You can set those flags on a per font basis in ImFontConfig::RasterizerFlags.
34+
* Use the 'extra_flags' parameter of BuildFontAtlas() to force a flag on all your fonts.
35+
*/
36+
public static final class RasterizerFlags {
37+
/**
38+
* By default, hinting is enabled and the font's native hinter is preferred over the auto-hinter.
39+
* Disable hinting. This generally generates 'blurrier' bitmap glyphs when the glyph are rendered in any of the anti-aliased modes.
40+
*/
41+
public static final int NoHinting = 1;
42+
/**
43+
* Disable auto-hinter.
44+
*/
45+
public static final int NoAutoHint = 1 << 1;
46+
/**
47+
* Indicates that the auto-hinter is preferred over the font's native hinter.
48+
*/
49+
public static final int ForceAutoHint = 1 << 2;
50+
/**
51+
* A lighter hinting algorithm for gray-level modes. Many generated glyphs are fuzzier but better resemble their original shape.
52+
* This is achieved by snapping glyphs to the pixel grid only vertically (Y-axis),
53+
* as is done by Microsoft's ClearType and Adobe's proprietary font renderer.
54+
* This preserves inter-glyph spacing in horizontal text.
55+
*/
56+
public static final int LightHinting = 1 << 3;
57+
/**
58+
* Strong hinting algorithm that should only be used for monochrome output.
59+
*/
60+
public static final int MonoHinting = 1 << 4;
61+
/**
62+
* Styling: Should we artificially embolden the font?
63+
*/
64+
public static final int Bold = 1 << 5;
65+
/**
66+
* Styling: Should we slant the font, emulating italic style?
67+
*/
68+
public static final int Oblique = 1 << 6;
69+
/**
70+
* Disable anti-aliasing. Combine this with MonoHinting for best results!
71+
*/
72+
public static final int Monochrome = 1 << 7;
73+
}
74+
}

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import imgui.ImFontAtlas;
22
import imgui.ImFontConfig;
33
import imgui.ImGui;
4+
import imgui.ImGuiFreeType;
45
import imgui.ImGuiIO;
56
import imgui.callbacks.ImStrConsumer;
67
import imgui.callbacks.ImStrSupplier;
@@ -29,9 +30,9 @@
2930

3031
@SuppressWarnings("MagicNumber")
3132
public final class ImGuiGlfwExample {
32-
private long windowPtr; // current GLFW window pointer
33+
private long windowPtr; // pointer to the current GLFW window
3334

34-
// To get window properties
35+
// For application window properties
3536
private final int[] winWidth = new int[1];
3637
private final int[] winHeight = new int[1];
3738
private final int[] fbWidth = new int[1];
@@ -48,7 +49,7 @@ public final class ImGuiGlfwExample {
4849
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3();
4950
private String glslVersion = null; // We can initialize our renderer with different versions of the GLSL
5051

51-
// Ui to render
52+
// User UI to render
5253
private final ExampleUi exampleUi = new ExampleUi();
5354

5455
public void run() throws Exception {
@@ -60,7 +61,7 @@ public void run() throws Exception {
6061
destroyGlfw();
6162
}
6263

63-
// Initialize GLFW + create OpenGL context.
64+
// Initialize GLFW + create an OpenGL context.
6465
// All code is mostly a copy-paste from the official LWJGL3 "Get Started": https://www.lwjgl.org/guide
6566
private void initGlfw() {
6667
// Setup an error callback. The default implementation
@@ -78,7 +79,6 @@ private void initGlfw() {
7879

7980
decideGlGlslVersions();
8081

81-
// Create the window
8282
windowPtr = glfwCreateWindow(1280, 768, "Dear ImGui + GLFW + LWJGL Example", NULL, NULL);
8383

8484
if (windowPtr == NULL) {
@@ -134,6 +134,7 @@ private void initImGui() {
134134
// This line is critical for Dear ImGui to work.
135135
ImGui.createContext();
136136

137+
// ------------------------------------------------------------
137138
// Initialize ImGuiIO config
138139
final ImGuiIO io = ImGui.getIO();
139140

@@ -142,6 +143,7 @@ private void initImGui() {
142143
io.setBackendFlags(ImGuiBackendFlags.HasMouseCursors); // Mouse cursors to display while resizing windows etc.
143144
io.setBackendPlatformName("imgui_java_impl_glfw");
144145

146+
// ------------------------------------------------------------
145147
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
146148
final int[] keyMap = new int[ImGuiKey.COUNT];
147149
keyMap[ImGuiKey.Tab] = GLFW_KEY_TAB;
@@ -168,6 +170,7 @@ private void initImGui() {
168170
keyMap[ImGuiKey.Z] = GLFW_KEY_Z;
169171
io.setKeyMap(keyMap);
170172

173+
// ------------------------------------------------------------
171174
// Mouse cursors mapping
172175
mouseCursors[ImGuiMouseCursor.Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
173176
mouseCursors[ImGuiMouseCursor.TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
@@ -180,7 +183,7 @@ private void initImGui() {
180183
mouseCursors[ImGuiMouseCursor.NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
181184

182185
// ------------------------------------------------------------
183-
// Here goes GLFW callbacks to update user input in Dear ImGui
186+
// GLFW callbacks to handle user input
184187

185188
glfwSetKeyCallback(windowPtr, (w, key, scancode, action, mods) -> {
186189
if (action == GLFW_PRESS) {
@@ -238,52 +241,51 @@ public String get() {
238241

239242
// ------------------------------------------------------------
240243
// Fonts configuration
241-
242-
// -------------------
243-
// Fonts merge example
244+
// Read: https://raw.githubusercontent.com/ocornut/imgui/master/docs/FONTS.txt
244245

245246
final ImFontAtlas fontAtlas = io.getFonts();
247+
final ImFontConfig fontConfig = new ImFontConfig(); // Natively allocated object, should be explicitly destroyed
248+
249+
// Glyphs could be added per-font as well as per config used globally like here
250+
fontConfig.setGlyphRanges(fontAtlas.getGlyphRangesCyrillic());
246251

247-
// First of all we add a default font, which is 'ProggyClean.ttf, 13px'
252+
// Add a default font, which is 'ProggyClean.ttf, 13px'
248253
fontAtlas.addFontDefault();
249254

250-
final ImFontConfig fontConfig = new ImFontConfig(); // Keep in mind that creation of the ImFontConfig will allocate the native memory
251-
fontConfig.setMergeMode(true); // All fonts added while this mode is turned on will be merged with the previously added font
255+
// Fonts merge example
256+
fontConfig.setMergeMode(true); // When enabled, all fonts added with this config would be merged with the previously added font
252257
fontConfig.setPixelSnapH(true);
253-
fontConfig.setGlyphRanges(fontAtlas.getGlyphRangesCyrillic()); // Additional glyphs could be added like this or in "addFontFrom*()" methods
254258

255-
// We merge font loaded from resources with the default one. Thus we will get an absent cyrillic glyphs
256259
fontAtlas.addFontFromMemoryTTF(loadFromResources("basis33.ttf"), 16, fontConfig);
257260

258-
// Disable merge mode and add all other fonts normally
259261
fontConfig.setMergeMode(false);
260262
fontConfig.setPixelSnapH(false);
261263

262-
// ------------------------------
263264
// Fonts from file/memory example
265+
// We can add new fonts from the file system
266+
fontAtlas.addFontFromFileTTF("src/test/resources/Righteous-Regular.ttf", 14, fontConfig);
267+
fontAtlas.addFontFromFileTTF("src/test/resources/Righteous-Regular.ttf", 16, fontConfig);
264268

265-
fontConfig.setRasterizerMultiply(1.2f); // This will make fonts a bit more readable
266-
267-
// We can add new fonts directly from file
268-
fontAtlas.addFontFromFileTTF("src/test/resources/DroidSans.ttf", 13, fontConfig);
269-
fontAtlas.addFontFromFileTTF("src/test/resources/DroidSans.ttf", 14, fontConfig);
270-
271-
// Or directly from memory
272-
fontConfig.setName("Roboto-Regular.ttf, 13px"); // This name will be displayed in Style Editor
273-
fontAtlas.addFontFromMemoryTTF(loadFromResources("Roboto-Regular.ttf"), 13, fontConfig);
274-
fontConfig.setName("Roboto-Regular.ttf, 14px"); // We can apply a new config value every time we add a new font
269+
// Or directly from the memory
270+
fontConfig.setName("Roboto-Regular.ttf, 14px"); // This name will be displayed in Style Editor
275271
fontAtlas.addFontFromMemoryTTF(loadFromResources("Roboto-Regular.ttf"), 14, fontConfig);
272+
fontConfig.setName("Roboto-Regular.ttf, 16px"); // We can apply a new config value every time we add a new font
273+
fontAtlas.addFontFromMemoryTTF(loadFromResources("Roboto-Regular.ttf"), 16, fontConfig);
276274

277275
fontConfig.destroy(); // After all fonts were added we don't need this config more
278276

277+
// ------------------------------------------------------------
278+
// Use freetype instead of stb_truetype to build a fonts texture
279+
ImGuiFreeType.buildFontAtlas(fontAtlas, ImGuiFreeType.RasterizerFlags.LightHinting);
280+
279281
// Method initializes LWJGL3 renderer.
280282
// This method SHOULD be called after you've initialized your ImGui configuration (fonts and so on).
281283
// ImGui context should be created as well.
282284
imGuiGl3.init(glslVersion);
283285
}
284286

285287
// Main application loop
286-
private void loop() throws Exception {
288+
private void loop() {
287289
double time = 0; // to track our frame delta value
288290

289291
// Run the rendering loop until the user has attempted to close the window
@@ -295,7 +297,7 @@ private void loop() throws Exception {
295297

296298
startFrame((float) deltaTime);
297299

298-
// Any Dear ImGui code SHOULD go between NewFrame()/Render() methods
300+
// Any Dear ImGui code SHOULD go between ImGui.newFrame()/ImGui.render() methods
299301
ImGui.newFrame();
300302
exampleUi.render();
301303
ImGui.render();
@@ -309,32 +311,30 @@ private void startFrame(final float deltaTime) {
309311
glClearColor(exampleUi.backgroundColor[0], exampleUi.backgroundColor[1], exampleUi.backgroundColor[2], 0.0f);
310312
glClear(GL_COLOR_BUFFER_BIT);
311313

312-
// Get window size properties and mouse position
314+
// Get window properties and mouse position
313315
glfwGetWindowSize(windowPtr, winWidth, winHeight);
314316
glfwGetFramebufferSize(windowPtr, fbWidth, fbHeight);
315317
glfwGetCursorPos(windowPtr, mousePosX, mousePosY);
316318

317-
// We SHOULD call those methods to update ImGui state for current frame
319+
// We SHOULD call those methods to update Dear ImGui state for the current frame
318320
final ImGuiIO io = ImGui.getIO();
319321
io.setDisplaySize(winWidth[0], winHeight[0]);
320322
io.setDisplayFramebufferScale((float) fbWidth[0] / winWidth[0], (float) fbHeight[0] / winHeight[0]);
321323
io.setMousePos((float) mousePosX[0], (float) mousePosY[0]);
322324
io.setDeltaTime(deltaTime);
323325

324-
// Update mouse cursor
326+
// Update the mouse cursor
325327
final int imguiCursor = ImGui.getMouseCursor();
326328
glfwSetCursor(windowPtr, mouseCursors[imguiCursor]);
327329
glfwSetInputMode(windowPtr, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
328330
}
329331

330332
private void endFrame() {
331-
// After Dear ImGui prepared a draw data, we use it in LWJGL3 renderer.
333+
// After Dear ImGui prepared a draw data, we use it in the LWJGL3 renderer.
332334
// At that moment ImGui will be rendered to the current OpenGL context.
333335
imGuiGl3.render(ImGui.getDrawData());
334336

335-
glfwSwapBuffers(windowPtr); // swap the color buffers
336-
337-
// Poll for window events. The key callback above will only be invoked during this call.
337+
glfwSwapBuffers(windowPtr);
338338
glfwPollEvents();
339339
}
340340

-186 KB
Binary file not shown.
39.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)