From a7e13905dbc75c70a61f5d364ef15f8109686560 Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Tue, 8 Oct 2024 17:21:48 +0200 Subject: [PATCH] _LoadFontImpl: fill debug ImFontConfig->Name (cf https://github.com/pthom/imgui_bundle/issues/263) --- src/hello_imgui/impl/hello_imgui_font.cpp | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/hello_imgui/impl/hello_imgui_font.cpp b/src/hello_imgui/impl/hello_imgui_font.cpp index 84e63ae7..0523ba5b 100644 --- a/src/hello_imgui/impl/hello_imgui_font.cpp +++ b/src/hello_imgui/impl/hello_imgui_font.cpp @@ -10,6 +10,8 @@ #endif #include +#include +#include #ifdef IOS #include "hello_imgui/internal/platform/getAppleBundleResourcePath.h" @@ -122,12 +124,46 @@ namespace HelloImGui return 1.f; } + // Fills ImFontConfig->Name with the base filename of the font file + the font size + // (ImFontConfig->Name is only used for debugging purposes, and displayed in the Style Editor) + static void Priv_CopyDebugFontNameToFontConfig( + const std::string& fontFilename, + float fontSize, + ImFontConfig* dstFontConfig) + { + // Step 1: Extract the base filename without path + std::string filename = fontFilename; + size_t lastSlash = filename.find_last_of("/\\"); + if (lastSlash != std::string::npos) { + filename = filename.substr(lastSlash + 1); + } + + // Step 2: Remove the extension + size_t lastDot = filename.find_last_of('.'); + if (lastDot != std::string::npos) { + filename = filename.substr(0, lastDot); + } + + // Step 3: Round fontSize to the nearest integer + int roundedSize = static_cast(std::lround(fontSize)); + + // Step 4: Create the name string (e.g., "Arial 10") + std::string nameWithSize = filename + " " + std::to_string(roundedSize); + + // Step 5: Copy the result into dstFontConfig->Name, ensuring no overflow + int bufferSize = sizeof(dstFontConfig->Name); + std::strncpy(dstFontConfig->Name, nameWithSize.c_str(), bufferSize - 1); + dstFontConfig->Name[bufferSize - 1] = '\0'; // Ensure null termination + } + ImFont* _LoadFontImpl(const std::string & fontFilename, float fontSize_, const FontLoadingParams& params_) { gDidCallHelloImGuiLoadFontTTF = true; FontLoadingParams params = params_; + Priv_CopyDebugFontNameToFontConfig(fontFilename, fontSize_, ¶ms.fontConfig); + // Font oversampling (set by dpiAwareParams) { const auto& dpiAwareParams = HelloImGui::GetDpiAwareParams();