|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "output.hpp" |
| 4 | + |
| 5 | +#include <backends/imgui_impl_opengl3.h> |
| 6 | +#include <backends/imgui_impl_sdl2.h> |
| 7 | +#include <filesystem> |
| 8 | +#include <imgui.h> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +using namespace std::literals; |
| 12 | + |
| 13 | +namespace steppable::gui::__internals |
| 14 | +{ |
| 15 | + inline void addIfExistent(const ImGuiIO* io, |
| 16 | + const std::filesystem::path& path, |
| 17 | + const ImFontConfig* config, |
| 18 | + const ImWchar* ranges) noexcept |
| 19 | + { |
| 20 | + if (io->Fonts->Fonts.empty() and config->MergeMode) |
| 21 | + config = nullptr; |
| 22 | + if (std::filesystem::exists(path)) |
| 23 | + { |
| 24 | +#ifdef DEBUG |
| 25 | + output::info("addIfExistent"s, "Added font {0}"s, { path }); |
| 26 | +#endif |
| 27 | + io->Fonts->AddFontFromFileTTF(path.c_str(), 15.0F, config, ranges); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + inline void loadFonts(const ImGuiIO* io) noexcept |
| 32 | + { |
| 33 | + ImFontConfig config; |
| 34 | + config.MergeMode = true; |
| 35 | +#ifdef WINDOWS |
| 36 | + // WINDOWS fonts |
| 37 | + // ------------- |
| 38 | + // Chinese -> Microsoft YaHei |
| 39 | + // Cyrillic -> Segoe UI -----------------+ |
| 40 | + // Greek -> Segoe UI -----------------| |
| 41 | + // Japanese -> Meiryo | |
| 42 | + // Korean -> Malgun Gothic +--> Top-priority |
| 43 | + // Thai -> Leelawadee | |
| 44 | + // Vietnamese -> Segoe UI -----------------+ |
| 45 | + |
| 46 | + // Load top-priority fonts |
| 47 | + addIfExistent(io, "C:/Windows/Fonts/segoeui.ttf", &config, io->Fonts->GetGlyphRangesCyrillic()); |
| 48 | + addIfExistent(io, "C:/Windows/Fonts/segoeui.ttf", &config, io->Fonts->GetGlyphRangesDefault()); |
| 49 | + addIfExistent(io, "C:/Windows/Fonts/segoeui.ttf", &config, io->Fonts->GetGlyphRangesGreek()); |
| 50 | + addIfExistent(io, "C:/Windows/Fonts/segoeui.ttf", &config, io->Fonts->GetGlyphRangesVietnamese()); |
| 51 | + |
| 52 | + // Load Chinese fonts |
| 53 | + addIfExistent(io, "C:/Windows/Fonts/msyh.ttc", &config, io->Fonts->GetGlyphRangesChineseFull()); |
| 54 | + |
| 55 | + // Load Japanese fonts |
| 56 | + addIfExistent(io, "C:/Windows/Fonts/meiryo.ttc", &config, io->Fonts->GetGlyphRangesJapanese()); |
| 57 | + |
| 58 | + // Load Korean fonts |
| 59 | + addIfExistent(io, "C:/Windows/Fonts/malgun.ttf", &config, io->Fonts->GetGlyphRangesKorean()); |
| 60 | + |
| 61 | + // Load Thai fonts |
| 62 | + addIfExistent(io, "C:/Windows/Fonts/leelawad.ttf", &config, io->Fonts->GetGlyphRangesThai()); |
| 63 | +#elif defined(MACOSX) |
| 64 | + // MACOS fonts |
| 65 | + // ------------- |
| 66 | + // Chinese -> PingFang SC (*) |
| 67 | + // Cyrillic -> SF Pro -----------------+ |
| 68 | + // Greek -> SF Pro -----------------| |
| 69 | + // Japanese -> Hiragino Sans | |
| 70 | + // Korean -> Apple SD Gothic Neo +--> Top-priority |
| 71 | + // Thai -> Thonburi | |
| 72 | + // Vietnamese -> SF Pro -----------------+ |
| 73 | + // |
| 74 | + // (*) NOTE: PingFang may not be available on all systems, but STHeiti Medium is a good alternative. |
| 75 | + |
| 76 | + // Load top-priority fonts |
| 77 | + addIfExistent(io, "/Library/Fonts/SF-Pro.ttf", &config, io->Fonts->GetGlyphRangesCyrillic()); |
| 78 | + addIfExistent(io, "/Library/Fonts/SF-Pro.ttf", &config, io->Fonts->GetGlyphRangesDefault()); |
| 79 | + addIfExistent(io, "/Library/Fonts/SF-Pro.ttf", &config, io->Fonts->GetGlyphRangesGreek()); |
| 80 | + addIfExistent(io, "/Library/Fonts/SF-Pro.ttf", &config, io->Fonts->GetGlyphRangesVietnamese()); |
| 81 | + |
| 82 | + // Load Chinese fonts |
| 83 | + addIfExistent(io, "/System/Library/Fonts/PingFang.ttc", &config, io->Fonts->GetGlyphRangesChineseFull()); |
| 84 | + addIfExistent(io, "/System/Library/Fonts/STHeiti Medium.ttc", &config, io->Fonts->GetGlyphRangesChineseFull()); |
| 85 | + |
| 86 | + // Load Japanese fonts |
| 87 | + addIfExistent(io, "/System/Library/Fonts/Hiragino.ttc", &config, io->Fonts->GetGlyphRangesJapanese()); |
| 88 | + |
| 89 | + // Load Korean fonts |
| 90 | + addIfExistent(io, "/System/Library/Fonts/AppleSDGothicNeo.ttc", &config, io->Fonts->GetGlyphRangesKorean()); |
| 91 | + |
| 92 | + // Load Thai fonts |
| 93 | + addIfExistent(io, "/System/Library/Fonts/Thonburi.ttf", &config, io->Fonts->GetGlyphRangesThai()); |
| 94 | + addIfExistent(io, "/System/Library/Fonts/Supplemental/Ayuthaya.ttf", &config, io->Fonts->GetGlyphRangesThai()); |
| 95 | +#elif defined(LINUX) |
| 96 | + // LINUX fonts |
| 97 | + // ------------- |
| 98 | + // Chinese -> WenQuanYi Zen Hei |
| 99 | + // Cyrillic -> DejaVu Sans -----------------+ |
| 100 | + // Greek -> DejaVu Sans -----------------| |
| 101 | + // Japanese -> Takao Gothic | |
| 102 | + // Korean -> Nanum Gothic +--> Top-priority |
| 103 | + // Thai -> Garuda | |
| 104 | + // Vietnamese -> DejaVu Sans -----------------+ |
| 105 | + |
| 106 | + // Load top-priority fonts |
| 107 | + addIfExistent(io, "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", &config, io->Fonts->GetGlyphRangesCyrillic()); |
| 108 | + addIfExistent(io, "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", &config, io->Fonts->GetGlyphRangesDefault()); |
| 109 | + addIfExistent(io, "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", &config, io->Fonts->GetGlyphRangesGreek()); |
| 110 | + addIfExistent(io, "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", &config, io->Fonts->GetGlyphRangesVietnamese()); |
| 111 | + |
| 112 | + // Load Chinese fonts |
| 113 | + addIfExistent(io, "/usr/share/fonts/TTF/wqy-zenhei.ttc", &config, io->Fonts->GetGlyphRangesChineseFull()); |
| 114 | + |
| 115 | + // Load Japanese fonts |
| 116 | + addIfExistent(io, "/usr/share/fonts/TTF/takao-mincho.ttf", &config, io->Fonts->GetGlyphRangesJapanese()); |
| 117 | + |
| 118 | + // Load Korean fonts |
| 119 | + addIfExistent(io, "/usr/share/fonts/TTF/NanumGothic.ttf", &config, io->Fonts->GetGlyphRangesKorean()); |
| 120 | + |
| 121 | + // Load Thai fonts |
| 122 | + addIfExistent(io, "/usr/share/fonts/TTF/garuda.ttf", &config, io->Fonts->GetGlyphRangesThai()); |
| 123 | +#endif |
| 124 | + // Add the default font as well. |
| 125 | + io->Fonts->AddFontDefault(&config); |
| 126 | + io->Fonts->Build(); |
| 127 | + } |
| 128 | +} // namespace steppable::gui::__internals |
0 commit comments