Skip to content

Commit

Permalink
_LoadFontImpl: fill debug ImFontConfig->Name (cf pthom/imgui_bundle#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Oct 8, 2024
1 parent 50bc587 commit a7e1390
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/hello_imgui/impl/hello_imgui_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#endif

#include <vector>
#include <cstring>
#include <cmath>

#ifdef IOS
#include "hello_imgui/internal/platform/getAppleBundleResourcePath.h"
Expand Down Expand Up @@ -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<int>(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_, &params.fontConfig);

// Font oversampling (set by dpiAwareParams)
{
const auto& dpiAwareParams = HelloImGui::GetDpiAwareParams();
Expand Down

0 comments on commit a7e1390

Please sign in to comment.