Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There's no "safe" way to use Freetype #773

Open
ruby3141 opened this issue Apr 22, 2024 · 0 comments
Open

There's no "safe" way to use Freetype #773

ruby3141 opened this issue Apr 22, 2024 · 0 comments

Comments

@ruby3141
Copy link

ruby3141 commented Apr 22, 2024

This issue contains long trivia about how I found the reason why it didn't work.
I was dead confused while solving this issue, so I elongated the explanation hoping to save some tortured souls.

If you just want to know how to enable Freetype right now, go to the end of problem description.

Is your feature request related to a problem? Please describe.

I tried using a new font (NanumSquare Neo, for those who wants to know), but it looks blurish and ugly.
So I enabled Freetype feature of imgui (and imgui-sys manually to use vcpkg),

imgui-sys = { version = "0.11.0", features = ["freetype", "use-vcpkg"] }
imgui = { version = "0.11.0", features = ["freetype"] }

but found no difference. The two result was pixel perfect identical.

Found this line on Imgui Freetype readme, so I confired that Freetype isn't activated, by changing oversampling settings.

### Known issues
- Oversampling settins are ignored but also not so much necessary with the higher quality rendering.

After many many tries of changing, rebuilding, trying, I digged into imgui-sys, insert some test codes on cpp files,
and finally found out IMGUI_ENABLE_FREETYPE is not defined when imgui main parts are compiling.

Screenshot 2024-04-22 114307

build.rs said IMGUI_ENABLE_FREETYPE sets with CIMGUI_FREETYPE, and it was.

// Set flag for dear imgui
build.define("CIMGUI_FREETYPE", None); // Sets IMGUI_ENABLE_FREETYPE
println!("cargo:DEFINE_IMGUI_ENABLE_FREETYPE=");

#define IMGUI_ENABLE_FREETYPE
#ifdef IMGUI_ENABLE_FREETYPE
#ifndef CIMGUI_FREETYPE
#error "IMGUI_FREETYPE should be defined for Freetype linking"
#endif
#else
#ifdef CIMGUI_FREETYPE
#error "IMGUI_FREETYPE should not be defined without freetype generated cimgui"
#endif
#endif
#include "./imgui/imgui.h"
#ifdef IMGUI_ENABLE_FREETYPE
#include "./imgui/misc/freetype/imgui_freetype.h"
#endif

But cimgui.cpp compiles after Imgui things. It has no effect while building Imgui main parts.

// This improves build speed by only compiling a single file, and performance by
// allowing the optimizer to inline across separate object files (note that even
// when rust is built with LTO, unless the steps are taken to allow cross-lang
// LTO (tricky), the C/C++ code won't be LTOed).
#include "./third-party/imgui-master-freetype/imgui/imgui.cpp"
#include "./third-party/imgui-master-freetype/imgui/imgui_demo.cpp"
#include "./third-party/imgui-master-freetype/imgui/imgui_draw.cpp"
#include "./third-party/imgui-master-freetype/imgui/imgui_widgets.cpp"
#include "./third-party/imgui-master-freetype/imgui/imgui_tables.cpp"
#include "./third-party/imgui-master-freetype/cimgui.cpp"
#include "./third-party/imgui-master-freetype/imgui/misc/freetype/imgui_freetype.cpp"

If IMGUI_ENABLE_FREETYPE is not set during compilation of Imgui, imstb is included and used as default.

#ifndef IMGUI_ENABLE_FREETYPE
#define IMGUI_ENABLE_STB_TRUETYPE
#endif

#ifdef IMGUI_ENABLE_FREETYPE
builder_io = ImGuiFreeType::GetBuilderForFreeType();
#elif defined(IMGUI_ENABLE_STB_TRUETYPE)
builder_io = ImFontAtlasGetBuilderForStbTruetype();

Since Freetype builder is built and included but not set as default,
it needs to be exchanged at runtime, before building and loading font atlas textures.
And I think, with current release of imgui-sys, that there's no other way than unsafely doing it.

unsafe {
    ctx.fonts().raw_mut().FontBuilderIO = ImGuiFreeType_GetBuilderForFreeType();
}

TL;DR

  1. Imgui-rs doesn't set Freetype atlas builder as default even if the feature is enabled.
    (And I think it's misleading.)
  2. For now, you can use Freetype by changing FontBuilderIO at runtime.
  3. But you should do it unsafely.

Describe the solution you'd like

  1. Make Freetype as default when the feature is enabled.

Describe alternatives you've considered

  1. Make way to exchange font builder not unsafely using imgui-sys

Additional context

Maybe having example program about using Freetype would be very helpful IMO.

ruby3141 added a commit to ruby3141/DJMAXPlus that referenced this issue Apr 22, 2024
Here's an epic(lol) about this commit.
imgui-rs/imgui-rs#773
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant