font fallback?

This commit is contained in:
2025-12-28 17:49:24 -05:00
parent 33c55806a4
commit 7e988dbb7f
3 changed files with 46 additions and 2 deletions

View File

@@ -11,6 +11,14 @@ namespace anm2ed::resource
pointer = ImGui::GetIO().Fonts->AddFontFromMemoryTTF(data, length, size, &config);
}
Font::Font(const char* path, int size, int number)
{
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
config.FontNo = number;
pointer = ImGui::GetIO().Fonts->AddFontFromFileTTF(path, size);
}
void Font::append(void* data, size_t length, int size)
{
ImFontConfig config;
@@ -19,6 +27,15 @@ namespace anm2ed::resource
ImGui::GetIO().Fonts->AddFontFromMemoryTTF(data, length, size, &config);
}
bool Font::append(const char* path, int size, int number)
{
ImFontConfig config;
config.MergeMode = true;
config.FontDataOwnedByAtlas = false;
config.FontNo = number;
return ImGui::GetIO().Fonts->AddFontFromFileTTF(path, size, &config);
}
Font::~Font()
{
if (get()) ImGui::GetIO().Fonts->RemoveFont(pointer);

View File

@@ -47321,7 +47321,9 @@ namespace anm2ed::resource
public:
Font();
Font(void*, size_t, int);
Font(const char* path, int size, int number = 0);
void append(void*, size_t, int);
bool append(const char* path, int size, int number = 0);
~Font();
ImFont* get();
Font& operator=(Font&&) noexcept;

View File

@@ -2,19 +2,44 @@
#include <ranges>
#include "log.h"
#include "music.h"
using namespace anm2ed::resource;
#ifdef _WIN32
static constexpr const char* CHINESE_FALLBACK = "C:\\Windows\\Fonts\\msyh.ttc";
static constexpr const char* KOREAN_FALLBACK = "C:\\Windows\\Fonts\\malgun.ttf";
static constexpr int CHINESE_FALLBACK_NUMBER = 0;
static constexpr int KOREAN_FALLBACK_NUMBER = 0;
#else
static constexpr const char* CHINESE_FALLBACK = "/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc";
static constexpr const char* KOREAN_FALLBACK = "/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc";
static constexpr int CHINESE_FALLBACK_NUMBER = 2;
static constexpr int KOREAN_FALLBACK_NUMBER = 1;
#endif
namespace anm2ed
{
Resources::Resources()
{
auto isKoreanFallback = std::filesystem::exists(KOREAN_FALLBACK);
auto isChineseFallback = std::filesystem::exists(CHINESE_FALLBACK);
if (!isKoreanFallback) logger.error(std::format("Failed to load Korean fallback font: {}", KOREAN_FALLBACK));
if (!isChineseFallback) logger.error(std::format("Failed to load Chinese fallback font: {}", CHINESE_FALLBACK));
for (auto [i, fontInfo] : std::views::enumerate(font::FONTS))
{
fonts[i] = Font((void*)fontInfo.data, fontInfo.length, font::SIZE);
fonts[i].append((void*)font::CHINESE_INFO.data, font::CHINESE_INFO.length, font::SIZE);
fonts[i].append((void*)font::KOREAN_INFO.data, font::KOREAN_INFO.length, font::SIZE);
if (isKoreanFallback)
fonts[i].append(KOREAN_FALLBACK, font::SIZE, KOREAN_FALLBACK_NUMBER);
else
fonts[i].append((void*)font::KOREAN_INFO.data, font::KOREAN_INFO.length, font::SIZE);
if (isChineseFallback)
fonts[i].append(CHINESE_FALLBACK, font::SIZE, CHINESE_FALLBACK_NUMBER);
else
fonts[i].append((void*)font::CHINESE_INFO.data, font::CHINESE_INFO.length, font::SIZE);
}
for (auto [i, iconInfo] : std::views::enumerate(icon::ICONS))