Refactor font loading for Windows and Linux compatibility

- Removed Windows-specific font loading code to streamline the font loading process.
- Updated the LoadSystemFonts function to indicate that system font loading is now handled by NFD (Native File Dialog) for Linux.
- Maintained the function for compatibility while ensuring it does not perform any operations on Linux.
This commit is contained in:
scawful
2025-09-28 01:46:14 -04:00
parent 2a39cc644e
commit ff0e9c0cc3

View File

@@ -130,191 +130,10 @@ absl::Status ReloadPackageFont(const FontConfig& config) {
return absl::OkStatus();
}
#ifdef _WIN32
// Include Windows headers first to avoid namespace conflicts
#include <Windows.h>
#include <ShlObj.h>
#include <combaseapi.h>
#include <shlobj_core.h>
#include <algorithm>
#include <cctype>
namespace {
// Helper function to convert wide string to UTF-8
std::string WideToUtf8(const std::wstring& wstr) {
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
if (size_needed <= 0) return std::string();
std::string result(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &result[0], size_needed, NULL, NULL);
return result;
}
// Helper function to get Windows fonts directory
std::string GetWindowsFontsDirectory() {
wchar_t* fontsPath = nullptr;
::HRESULT hr = ::SHGetKnownFolderPath(FOLDERID_Fonts, 0, NULL, &fontsPath);
if (::SUCCEEDED(hr) && fontsPath) {
std::string result = WideToUtf8(fontsPath) + "\\";
::CoTaskMemFree(fontsPath);
return result;
}
// Fallback to default path
return "C:\\Windows\\Fonts\\";
}
// Helper function to normalize font name (lowercase, remove spaces)
std::string NormalizeFontName(const std::string& name) {
std::string result = name;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
result.erase(std::remove(result.begin(), result.end(), ' '), result.end());
return result;
}
// Check if file exists and is accessible
bool FontFileExists(const std::string& path) {
return ::GetFileAttributesA(path.c_str()) != INVALID_FILE_ATTRIBUTES;
}
}
void LoadSystemFonts() {
ImGuiIO& imgui_io = ImGui::GetIO();
// Get the Windows fonts directory
std::string fontsDir = GetWindowsFontsDirectory();
// List of essential Windows fonts to load
static const std::vector<std::string> essentialFonts = {
"arial.ttf",
"arialbd.ttf",
"times.ttf",
"timesbd.ttf",
"cour.ttf",
"courbd.ttf",
"verdana.ttf",
"verdanab.ttf",
"tahoma.ttf",
"tahomabd.ttf",
"segoeui.ttf",
"segoeuib.ttf"
};
// Load essential fonts
for (const auto& fontName : essentialFonts) {
std::string fontPath = fontsDir + fontName;
if (FontFileExists(fontPath)) {
try {
// Load the font
ImFont* font = imgui_io.Fonts->AddFontFromFileTTF(fontPath.c_str(), 16.0F);
if (font) {
// Merge icon fonts if available
static const ImWchar icons_ranges[] = {ICON_MIN_MD, 0xf900, 0};
ImFontConfig icons_config{};
icons_config.MergeMode = true;
icons_config.GlyphOffset.y = 5.0F;
icons_config.GlyphMinAdvanceX = 13.0F;
icons_config.PixelSnapH = true;
std::string iconFontPath = SetFontPath(FONT_ICON_FILE_NAME_MD);
if (FontFileExists(iconFontPath)) {
imgui_io.Fonts->AddFontFromFileTTF(iconFontPath.c_str(), ICON_FONT_SIZE,
&icons_config, icons_ranges);
}
}
} catch (...) {
// Silently continue if font loading fails
continue;
}
}
}
// Try to load additional fonts from registry (safer approach)
::HKEY hKey = nullptr;
::LONG result = ::RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts",
0, KEY_READ, &hKey);
if (result == ERROR_SUCCESS && hKey) {
::DWORD valueCount = 0;
::DWORD maxValueNameSize = 0;
::DWORD maxValueDataSize = 0;
// Get registry info
result = ::RegQueryInfoKeyA(hKey, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, &valueCount, &maxValueNameSize,
&maxValueDataSize, nullptr, nullptr);
if (result == ERROR_SUCCESS && valueCount > 0) {
// Allocate buffers with proper size limits
size_t maxNameSize = maxValueNameSize + 1;
if (maxNameSize > 1024) maxNameSize = 1024;
size_t maxDataSize = maxValueDataSize + 1;
if (maxDataSize > 4096) maxDataSize = 4096;
std::vector<char> valueName(maxNameSize);
std::vector<::BYTE> valueData(maxDataSize);
// Enumerate font entries (limit to prevent excessive loading)
::DWORD maxFontsToLoad = valueCount;
if (maxFontsToLoad > 50) maxFontsToLoad = 50;
for (::DWORD i = 0; i < maxFontsToLoad; i++) {
::DWORD valueNameSize = static_cast<::DWORD>(maxNameSize);
::DWORD valueDataSize = static_cast<::DWORD>(maxDataSize);
::DWORD valueType = 0;
result = ::RegEnumValueA(hKey, i, valueName.data(), &valueNameSize,
nullptr, &valueType, valueData.data(), &valueDataSize);
if (result == ERROR_SUCCESS && valueType == ::REG_SZ && valueDataSize > 0) {
// Ensure null termination
valueName[valueNameSize] = '\0';
valueData[valueDataSize] = '\0';
std::string fontPath(reinterpret_cast<char*>(valueData.data()));
// Normalize the font path
if (!fontPath.empty()) {
// If it's a relative path, prepend the fonts directory
if (fontPath.find(':') == std::string::npos) {
fontPath = fontsDir + fontPath;
}
// Check if it's a TTF file and exists
std::string lowerPath = fontPath;
std::transform(lowerPath.begin(), lowerPath.end(), lowerPath.begin(), ::tolower);
if ((lowerPath.find(".ttf") != std::string::npos ||
lowerPath.find(".otf") != std::string::npos) &&
FontFileExists(fontPath)) {
try {
imgui_io.Fonts->AddFontFromFileTTF(fontPath.c_str(), 16.0F);
} catch (...) {
// Continue if font loading fails
continue;
}
}
}
}
}
}
::RegCloseKey(hKey);
}
// System font loading is now handled by NFD (Native File Dialog)
// This function is kept for compatibility but does nothing
}
#elif defined(__linux__)
void LoadSystemFonts() {
// Load Linux System Fonts into ImGui
}
#endif
} // namespace core
} // namespace yaze