Skip to content

Commit

Permalink
feat(api): make FreeType optional and disabled by default
Browse files Browse the repository at this point in the history
FreeType renderer is still included in the final build.
Its usage is toggleable and DISABLED by default.
  • Loading branch information
SpaiR committed Aug 18, 2024
1 parent 0325ec9 commit 5d5a44b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
20 changes: 17 additions & 3 deletions buildSrc/src/main/groovy/tool/generator/GenerateLibs.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ class GenerateLibs extends DefaultTask {
spec.from(project.rootProject.file('include/imgui/misc/freetype')) { CopySpec it -> it.include('*.h', '*.cpp') }
spec.into("$jniDir/misc/freetype")
}
enableDefine('IMGUI_ENABLE_FREETYPE')

// Since we give a possibility to build library without enabled freetype - define should be set like that.
replaceSourceFileContent("imconfig.h", "//#define IMGUI_ENABLE_FREETYPE", "#define IMGUI_ENABLE_FREETYPE")

// Binding specific behavior to handle FreeType.
// By defining IMGUI_ENABLE_FREETYPE, Dear ImGui will default to using the FreeType font renderer.
// However, we modify the source code to ensure that, even with this, the STB_TrueType renderer is used instead.
// To use the FreeType font renderer, it must be explicitly forced on the atlas manually.
replaceSourceFileContent("imgui_draw.cpp", "ImGuiFreeType::GetBuilderForFreeType()", "ImFontAtlasGetBuilderForStbTruetype()")
}

// Copy dirent for ImGuiFileDialog
Expand Down Expand Up @@ -181,7 +189,13 @@ class GenerateLibs extends DefaultTask {
target.libraries += ' -lfreetype'
}

void enableDefine(String define) {
new File("$jniDir/imconfig.h").text += "#define $define"
void replaceSourceFileContent(String fileName, String replaceWhat, String replaceWith) {
def sourceFile = new File("$jniDir/$fileName")
def sourceTxt = sourceFile.text
def sourceTxtModified = sourceTxt.replace(replaceWhat, replaceWith)
if (sourceTxt == sourceTxtModified) {
throw new IllegalStateException("Unable to replace [$fileName] with content [$replaceWith]!")
}
sourceFile.text = sourceTxtModified
}
}
6 changes: 5 additions & 1 deletion example/src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ protected void initImGui(final Configuration config) {
* For more information read: https://github.com/ocornut/imgui/blob/33cdbe97b8fd233c6c12ca216e76398c2e89b0d8/docs/FONTS.md
*/
private void initFonts(final ImGuiIO io) {
io.getFonts().addFontDefault(); // Add default font for latin glyphs
// This enables FreeType font renderer, which is disabled by default.
io.getFonts().setFreeTypeRenderer(true);

// Add default font for latin glyphs
io.getFonts().addFontDefault();

// You can use the ImFontGlyphRangesBuilder helper to create glyph ranges based on text input.
// For example: for a game where your script is known, if you can feed your entire script to it (using addText) and only build the characters the game needs.
Expand Down
26 changes: 26 additions & 0 deletions imgui-binding/src/generated/java/imgui/ImFontAtlas.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,32 @@ public void clear() {
// Building in RGBA32 format is provided for convenience and compatibility, but note that unless you manually manipulate or copy color data into
// the texture (e.g. when using the AddCustomRect*** api), then the RGB pixels emitted will always be white (~75% of memory/bandwidth waste.

/*JNI
#ifdef IMGUI_ENABLE_FREETYPE
#include "misc/freetype/imgui_freetype.h"
#endif
*/

/**
* <b>BINDING NOTICE:</b> This method is specific to the imgui-java binding.
* <p>
* Since FreeType is included in the final build, it's possible to use both font renderers (STB_TrueType and FreeType) simultaneously without needing to rebuild the library.
* By default, we use small hacks to set STB_TrueType as the default font renderer. However, this method allows you to enforce the use of the FreeType renderer.
* <p>
* This method MUST be called before invoking the "#build" or "#getTexData*" methods.
*
* @param enabled true to enable the FreeType font renderer
*/
public native void setFreeTypeRenderer(boolean enabled); /*
#ifdef IMGUI_ENABLE_FREETYPE
if (enabled) {
THIS->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType();
} else {
THIS->FontBuilderIO = NULL;
}
#endif
*/

/**
* Build pixels data. This is called automatically for you by the GetTexData*** functions.
*/
Expand Down
26 changes: 26 additions & 0 deletions imgui-binding/src/main/java/imgui/ImFontAtlas.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ protected long create() {
// Building in RGBA32 format is provided for convenience and compatibility, but note that unless you manually manipulate or copy color data into
// the texture (e.g. when using the AddCustomRect*** api), then the RGB pixels emitted will always be white (~75% of memory/bandwidth waste.

/*JNI
#ifdef IMGUI_ENABLE_FREETYPE
#include "misc/freetype/imgui_freetype.h"
#endif
*/

/**
* <b>BINDING NOTICE:</b> This method is specific to the imgui-java binding.
* <p>
* Since FreeType is included in the final build, it's possible to use both font renderers (STB_TrueType and FreeType) simultaneously without needing to rebuild the library.
* By default, we use small hacks to set STB_TrueType as the default font renderer. However, this method allows you to enforce the use of the FreeType renderer.
* <p>
* This method MUST be called before invoking the "#build" or "#getTexData*" methods.
*
* @param enabled true to enable the FreeType font renderer
*/
public native void setFreeTypeRenderer(boolean enabled); /*
#ifdef IMGUI_ENABLE_FREETYPE
if (enabled) {
THIS->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType();
} else {
THIS->FontBuilderIO = NULL;
}
#endif
*/

/**
* Build pixels data. This is called automatically for you by the GetTexData*** functions.
*/
Expand Down
4 changes: 3 additions & 1 deletion imgui-binding/src/main/native/imconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//-----------------------------------------------------------------------------

#pragma once

#include "jni_assertion.h"

//---- Define assertion handler. Defaults to calling assert().
Expand All @@ -30,6 +31,7 @@

//---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names.
#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
//#define IMGUI_DISABLE_OBSOLETE_KEYIO // 1.87: disable legacy io.KeyMap[]+io.KeysDown[] in favor io.AddKeyEvent(). This will be folded into IMGUI_DISABLE_OBSOLETE_FUNCTIONS in a few versions.

//---- Disable all of Dear ImGui or don't implement standard windows.
// It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp.
Expand Down Expand Up @@ -77,7 +79,7 @@

//---- Use stb_truetype to build and rasterize the font atlas (default)
// The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend.
//#define IMGUI_ENABLE_STB_TRUETYPE
#define IMGUI_ENABLE_STB_TRUETYPE

//---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4.
// This will be inlined as part of ImVec2 and ImVec4 class declarations.
Expand Down

0 comments on commit 5d5a44b

Please sign in to comment.