|
| 1 | +#include "gui.hpp" |
| 2 | + |
| 3 | +#include "output.hpp" |
| 4 | + |
| 5 | +#ifdef MACOSX |
| 6 | + #include <CoreFoundation/CoreFoundation.h> |
| 7 | +#endif |
| 8 | + |
| 9 | +namespace steppable::gui::__internals |
| 10 | +{ |
| 11 | + |
| 12 | + bool isDarkModeEnabled() |
| 13 | + { |
| 14 | +#ifdef MACOSX |
| 15 | + bool isDarkMode = false; |
| 16 | + CFPreferencesAppSynchronize(CFSTR("AppleInterfaceStyle")); |
| 17 | + CFPropertyListRef value = CFPreferencesCopyAppValue(CFSTR("AppleInterfaceStyle"), kCFPreferencesAnyApplication); |
| 18 | + if (value != nullptr) |
| 19 | + { |
| 20 | + const auto* interfaceStyle = static_cast<CFStringRef>(value); |
| 21 | + if (CFStringCompare(interfaceStyle, CFSTR("Dark"), 0) == kCFCompareEqualTo) |
| 22 | + isDarkMode = true; |
| 23 | + CFRelease(value); |
| 24 | + } |
| 25 | + return isDarkMode; |
| 26 | +#elif defined(LINUX) |
| 27 | + return std::filesystem::exists("/usr/share/themes/Adwaita-dark/gtk-3.0"); |
| 28 | +#elif defined(WINDOWS) |
| 29 | + HKEY key; |
| 30 | + if (RegOpenKeyExA(HKEY_CURRENT_USER, |
| 31 | + "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", |
| 32 | + 0, |
| 33 | + KEY_QUERY_VALUE, |
| 34 | + &key) == ERROR_SUCCESS) |
| 35 | + { |
| 36 | + DWORD value = 0; |
| 37 | + DWORD size = sizeof(DWORD); |
| 38 | + if (RegQueryValueExA(key, "AppsUseLightTheme", nullptr, nullptr, reinterpret_cast<LPBYTE>(&value), &size) == |
| 39 | + ERROR_SUCCESS) |
| 40 | + return value == 0; |
| 41 | + } |
| 42 | + return false; |
| 43 | +#else |
| 44 | + return false; |
| 45 | +#endif |
| 46 | + } |
| 47 | + |
| 48 | + void addFontIfExistent(const ImGuiIO* io, |
| 49 | + const std::filesystem::path& path, |
| 50 | + const ImFontConfig* config, |
| 51 | + const ImWchar* ranges) noexcept |
| 52 | + { |
| 53 | + if (io->Fonts->Fonts.empty() and config->MergeMode) |
| 54 | + config = nullptr; |
| 55 | + if (std::filesystem::exists(path)) |
| 56 | + { |
| 57 | +#ifdef DEBUG |
| 58 | + output::info("addIfExistent"s, "Added font {0}"s, { path }); |
| 59 | +#endif |
| 60 | + io->Fonts->AddFontFromFileTTF(path.c_str(), 15.0F, config, ranges); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + void loadFonts(const ImGuiIO* io) noexcept |
| 65 | + { |
| 66 | + ImFontConfig config; |
| 67 | + config.MergeMode = true; |
| 68 | +#ifdef WINDOWS |
| 69 | + // WINDOWS fonts |
| 70 | + // ------------- |
| 71 | + // Chinese -> Microsoft YaHei |
| 72 | + // Cyrillic -> Segoe UI -----------------+ |
| 73 | + // Greek -> Segoe UI -----------------| |
| 74 | + // Japanese -> Meiryo | |
| 75 | + // Korean -> Malgun Gothic +--> Top-priority |
| 76 | + // Thai -> Leelawadee | |
| 77 | + // Vietnamese -> Segoe UI -----------------+ |
| 78 | + |
| 79 | + // Load top-priority fonts |
| 80 | + addIfExistent(io, "C:/Windows/Fonts/segoeui.ttf", &config, io->Fonts->GetGlyphRangesCyrillic()); |
| 81 | + addIfExistent(io, "C:/Windows/Fonts/segoeui.ttf", &config, io->Fonts->GetGlyphRangesDefault()); |
| 82 | + addIfExistent(io, "C:/Windows/Fonts/segoeui.ttf", &config, io->Fonts->GetGlyphRangesGreek()); |
| 83 | + addIfExistent(io, "C:/Windows/Fonts/segoeui.ttf", &config, io->Fonts->GetGlyphRangesVietnamese()); |
| 84 | + |
| 85 | + // Load Chinese fonts |
| 86 | + addIfExistent(io, "C:/Windows/Fonts/msyh.ttc", &config, io->Fonts->GetGlyphRangesChineseFull()); |
| 87 | + |
| 88 | + // Load Japanese fonts |
| 89 | + addIfExistent(io, "C:/Windows/Fonts/meiryo.ttc", &config, io->Fonts->GetGlyphRangesJapanese()); |
| 90 | + |
| 91 | + // Load Korean fonts |
| 92 | + addIfExistent(io, "C:/Windows/Fonts/malgun.ttf", &config, io->Fonts->GetGlyphRangesKorean()); |
| 93 | + |
| 94 | + // Load Thai fonts |
| 95 | + addIfExistent(io, "C:/Windows/Fonts/leelawad.ttf", &config, io->Fonts->GetGlyphRangesThai()); |
| 96 | +#elif defined(MACOSX) |
| 97 | + // MACOS fonts |
| 98 | + // ------------- |
| 99 | + // Chinese -> PingFang SC (*) |
| 100 | + // Cyrillic -> SF Pro -----------------+ |
| 101 | + // Greek -> SF Pro -----------------| |
| 102 | + // Japanese -> Hiragino Sans | |
| 103 | + // Korean -> Apple SD Gothic Neo +--> Top-priority |
| 104 | + // Thai -> Thonburi | |
| 105 | + // Vietnamese -> SF Pro -----------------+ |
| 106 | + // |
| 107 | + // (*) NOTE: PingFang may not be available on all systems, but STHeiti Medium is a good alternative. |
| 108 | + |
| 109 | + // Load top-priority fonts |
| 110 | + addFontIfExistent(io, "/Library/Fonts/SF-Pro.ttf", &config, io->Fonts->GetGlyphRangesCyrillic()); |
| 111 | + addFontIfExistent(io, "/Library/Fonts/SF-Pro.ttf", &config, io->Fonts->GetGlyphRangesDefault()); |
| 112 | + addFontIfExistent(io, "/Library/Fonts/SF-Pro.ttf", &config, io->Fonts->GetGlyphRangesGreek()); |
| 113 | + addFontIfExistent(io, "/Library/Fonts/SF-Pro.ttf", &config, io->Fonts->GetGlyphRangesVietnamese()); |
| 114 | + |
| 115 | + // Load Chinese fonts |
| 116 | + addFontIfExistent(io, "/System/Library/Fonts/PingFang.ttc", &config, io->Fonts->GetGlyphRangesChineseFull()); |
| 117 | + addFontIfExistent( |
| 118 | + io, "/System/Library/Fonts/STHeiti Medium.ttc", &config, io->Fonts->GetGlyphRangesChineseFull()); |
| 119 | + |
| 120 | + // Load Japanese fonts |
| 121 | + addFontIfExistent(io, "/System/Library/Fonts/Hiragino.ttc", &config, io->Fonts->GetGlyphRangesJapanese()); |
| 122 | + |
| 123 | + // Load Korean fonts |
| 124 | + addFontIfExistent(io, "/System/Library/Fonts/AppleSDGothicNeo.ttc", &config, io->Fonts->GetGlyphRangesKorean()); |
| 125 | + |
| 126 | + // Load Thai fonts |
| 127 | + addFontIfExistent(io, "/System/Library/Fonts/Thonburi.ttf", &config, io->Fonts->GetGlyphRangesThai()); |
| 128 | + addFontIfExistent( |
| 129 | + io, "/System/Library/Fonts/Supplemental/Ayuthaya.ttf", &config, io->Fonts->GetGlyphRangesThai()); |
| 130 | +#elif defined(LINUX) |
| 131 | + // LINUX fonts |
| 132 | + // ------------- |
| 133 | + // Chinese -> WenQuanYi Zen Hei |
| 134 | + // Cyrillic -> DejaVu Sans -----------------+ |
| 135 | + // Greek -> DejaVu Sans -----------------| |
| 136 | + // Japanese -> Takao Gothic | |
| 137 | + // Korean -> Nanum Gothic +--> Top-priority |
| 138 | + // Thai -> Garuda | |
| 139 | + // Vietnamese -> DejaVu Sans -----------------+ |
| 140 | + |
| 141 | + // Load top-priority fonts |
| 142 | + addIfExistent(io, "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", &config, io->Fonts->GetGlyphRangesCyrillic()); |
| 143 | + addIfExistent(io, "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", &config, io->Fonts->GetGlyphRangesDefault()); |
| 144 | + addIfExistent(io, "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", &config, io->Fonts->GetGlyphRangesGreek()); |
| 145 | + addIfExistent(io, "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", &config, io->Fonts->GetGlyphRangesVietnamese()); |
| 146 | + |
| 147 | + // Load Chinese fonts |
| 148 | + addIfExistent(io, "/usr/share/fonts/TTF/wqy-zenhei.ttc", &config, io->Fonts->GetGlyphRangesChineseFull()); |
| 149 | + |
| 150 | + // Load Japanese fonts |
| 151 | + addIfExistent(io, "/usr/share/fonts/TTF/takao-mincho.ttf", &config, io->Fonts->GetGlyphRangesJapanese()); |
| 152 | + |
| 153 | + // Load Korean fonts |
| 154 | + addIfExistent(io, "/usr/share/fonts/TTF/NanumGothic.ttf", &config, io->Fonts->GetGlyphRangesKorean()); |
| 155 | + |
| 156 | + // Load Thai fonts |
| 157 | + addIfExistent(io, "/usr/share/fonts/TTF/garuda.ttf", &config, io->Fonts->GetGlyphRangesThai()); |
| 158 | +#endif |
| 159 | + // Add the default font as well. |
| 160 | + io->Fonts->AddFontDefault(&config); |
| 161 | + io->Fonts->Build(); |
| 162 | + } |
| 163 | +} // namespace steppable::gui::__internals |
0 commit comments