Skip to content

Commit

Permalink
Add param onlyUseFontDpiResponsive
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Apr 12, 2024
1 parent 3e57dfb commit d3882aa
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/hello_imgui/dpi_aware.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ struct DpiAwareParams
// (This parameter will be used to set ImGui::GetIO().FontGlobalScale at startup)
float fontRenderingScale = 0.0f;

// `onlyUseFontDpiResponsive`
// If true, guarantees that only HelloImGui::LoadDpiResponsiveFont will be used to load fonts.
// (also for the default font)
bool onlyUseFontDpiResponsive = false;

// `dpiFontLoadingFactor`
// factor by which font size should be multiplied at loading time to get a similar
// visible size on different OSes.
Expand Down
19 changes: 18 additions & 1 deletion src/hello_imgui/impl/hello_imgui_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,28 @@ namespace HelloImGui
}

std::vector<FontDpiResponsive> gAllDpiResponsiveFonts;
bool gWasLoadFontBareCalled = false;
bool gWasLoadFontDpiResponsiveCalled = false;


ImFont* LoadFont(const std::string & fontFilename, float fontSize_, const FontLoadingParams& params_)
{
IM_ASSERT((!gWasLoadFontDpiResponsiveCalled) && "If using LoadFontDpiResponsive(), only use it, and do not use LoadFont()!");

auto runnerParams = HelloImGui::GetRunnerParams();
IM_ASSERT(! runnerParams->dpiAwareParams.onlyUseFontDpiResponsive && "If runnerParams->dpiAwareParams.onlyUseFontDpiResponsive is true, you must use LoadFontDpiResponsive() instead of LoadFont()");

gWasLoadFontBareCalled = true;
printf("LoadFont(%s, %f)\n", fontFilename.c_str(), fontSize_);
return _LoadFontImpl(fontFilename, fontSize_, params_);
}

FontDpiResponsive* LoadFontDpiResponsive(const std::string & fontFilename, float fontSize,
const FontLoadingParams & fontLoadingParams)
{
IM_ASSERT((!gWasLoadFontBareCalled) && "If using LoadFontDpiResponsive(), only use it, and do not use LoadFont()!");
gWasLoadFontDpiResponsiveCalled = true;

// Ensure that we have enough capacity, so that pointers remain valid
constexpr size_t maxFonts = 100;
if (gAllDpiResponsiveFonts.capacity() == 0)
Expand All @@ -243,8 +254,13 @@ namespace HelloImGui
return dpiResponsiveFont;
}

void _ReloadAllDpiResponsiveFonts()
bool _ReloadAllDpiResponsiveFonts()
{
if (gWasLoadFontBareCalled)
{
fprintf(stderr, "_ReloadAllDpiResponsiveFonts failed: ony call LoadFontDpiResponsive if you want this to work\n");
return false;
}
printf("_ReloadAllDpiResponsiveFonts\n");
auto& imguiFonts = ImGui::GetIO().Fonts;
imguiFonts->Clear();
Expand All @@ -258,6 +274,7 @@ namespace HelloImGui
}
bool buildSuccess = imguiFonts->Build();
IM_ASSERT(buildSuccess && "_ReloadAllDpiResponsiveFonts: Failed to build fonts");
return true;
}


Expand Down
20 changes: 15 additions & 5 deletions src/hello_imgui/impl/imgui_default_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace ImGuiDefaultSettings

void LoadDefaultFont_WithFontAwesomeIcons()
{
auto defaultIconFont = HelloImGui::GetRunnerParams()->callbacks.defaultIconFont;
auto runnerParams = HelloImGui::GetRunnerParams();
auto defaultIconFont = runnerParams->callbacks.defaultIconFont;
float fontSize = 15.f;

std::string fontFilename = "fonts/DroidSans.ttf";
Expand All @@ -25,8 +26,14 @@ void LoadDefaultFont_WithFontAwesomeIcons()
return;
}

auto font = LoadFontDpiResponsive(fontFilename, fontSize);
if (defaultIconFont == HelloImGui::DefaultIconFont::NoIcons)
bool useDpiResponsiveFonts = runnerParams->dpiAwareParams.onlyUseFontDpiResponsive;

if (useDpiResponsiveFonts)
LoadFontDpiResponsive(fontFilename, fontSize);
else
LoadFont(fontFilename, fontSize);

if (defaultIconFont == HelloImGui::DefaultIconFont::NoIcons)
return;

std::string iconFontFile;
Expand All @@ -43,8 +50,11 @@ void LoadDefaultFont_WithFontAwesomeIcons()
HelloImGui::FontLoadingParams fontParams;
fontParams.mergeToLastFont = true;
fontParams.useFullGlyphRange = true;
font = LoadFontDpiResponsive(iconFontFile, fontSize, fontParams);
(void) font;

if (useDpiResponsiveFonts)
LoadFontDpiResponsive(iconFontFile, fontSize, fontParams);
else
LoadFont(iconFontFile, fontSize, fontParams);
}

void SetupDefaultImGuiConfig()
Expand Down
22 changes: 12 additions & 10 deletions src/hello_imgui/internal/backend_impls/abstract_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace HelloImGui
void setFinalAppWindowScreenshotRgbBuffer(const ImageBuffer& b);

// Encapsulated inside hello_imgui_font.cpp
void _ReloadAllDpiResponsiveFonts();
bool _ReloadAllDpiResponsiveFonts();


// =====================================================================================================================
Expand Down Expand Up @@ -1260,17 +1260,19 @@ void AbstractRunner::CreateFramesAndRender()
if (_CheckDpiAwareParamsChanges(params))
{
printf("_CheckDpiAwareParamsChanges returned true => reload all fonts\n");
mRenderingBackendCallbacks->Impl_DestroyFontTexture();
_ReloadAllDpiResponsiveFonts();
// cf https://github.com/ocornut/imgui/issues/6547: we need to recreate the rendering backend device objects
mRenderingBackendCallbacks->Impl_CreateFontTexture();
#ifdef HELLOIMGUI_WITH_NETIMGUI
if (params.remoteParams.enableRemoting)
if (_ReloadAllDpiResponsiveFonts())
{
gNetImGuiWrapper = std::make_unique<NetImGuiWrapper>();
gNetImGuiWrapper->sendFonts();
// cf https://github.com/ocornut/imgui/issues/6547: we need to recreate the rendering backend device objects
mRenderingBackendCallbacks->Impl_DestroyFontTexture();
mRenderingBackendCallbacks->Impl_CreateFontTexture();
#ifdef HELLOIMGUI_WITH_NETIMGUI
if (params.remoteParams.enableRemoting)
{
gNetImGuiWrapper = std::make_unique<NetImGuiWrapper>();
gNetImGuiWrapper->sendFonts();
}
#endif
}
#endif
}

mIdxFrame += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@


// Demonstrate how to load additional fonts (fonts - part 1/3)
ImFont * gCustomFont = nullptr;
HelloImGui::FontDpiResponsive * gCustomFont = nullptr;
void MyLoadFonts()
{

HelloImGui::ImGuiDefaultSettings::LoadDefaultFont_WithFontAwesomeIcons(); // The font that is loaded first is the default font
gCustomFont = HelloImGui::LoadFont("fonts/Akronim-Regular.ttf", 40.f); // will be loaded from the assets folder
gCustomFont = HelloImGui::LoadFontDpiResponsive("fonts/Akronim-Regular.ttf", 40.f); // will be loaded from the assets folder
}


Expand Down Expand Up @@ -38,7 +38,7 @@ int main(int , char *[]) {
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.

// Demo custom font usage (fonts - part 3/3)
ImGui::PushFont(gCustomFont);
ImGui::PushFont(gCustomFont->font);
ImGui::Text("Custom font");
ImGui::PopFont();

Expand Down Expand Up @@ -77,6 +77,7 @@ int main(int , char *[]) {
params.imGuiWindowParams.showMenuBar = true;
params.imGuiWindowParams.showMenu_App = false;

params.dpiAwareParams.onlyUseFontDpiResponsive = true;
params.remoteParams.enableRemoting = true;

HelloImGui::Run(params);
Expand Down

0 comments on commit d3882aa

Please sign in to comment.