TmpMenu/src/renderer/font_mgr.cpp

182 lines
30 KiB
C++
Raw Normal View History

#include "font_mgr.hpp"
#include "fonts/fonts.hpp"
#include "renderer.hpp"
#include "thread_pool.hpp"
namespace big
{
font_mgr::font_mgr(std::vector<std::pair<float, ImFont**>> extra_font_sizes) :
m_extra_font_sizes(extra_font_sizes),
m_require_extra(eAlphabetType::LATIN),
m_fonts({
{eAlphabetType::CHINESE, {"msyh.ttc", "msyh.ttf", "arial.ttf"}},
{eAlphabetType::CYRILLIC, {"msyh.ttc", "msyh.ttf", "arial.ttf"}},
{eAlphabetType::JAPANESE, {"msyh.ttc", "msyh.ttf", "arial.ttf"}},
{eAlphabetType::KOREAN, {"malgun.ttf", "arial.ttf"}},
})
{
}
bool font_mgr::can_use()
{
return m_update_lock.try_lock();
}
void font_mgr::release_use()
{
m_update_lock.unlock();
}
void font_mgr::rebuild()
{
m_update_lock.lock();
g_renderer.pre_reset();
const auto extra_font_file = get_available_font_file_for_alphabet_type();
if (m_require_extra != eAlphabetType::LATIN && !extra_font_file.exists())
{
LOG(WARNING) << "Could not find an appropriate font for the current language!";
}
const auto extra_glyph_range = get_imgui_alphabet_type();
auto& io = ImGui::GetIO();
io.Fonts->Clear();
// default font size
{
ImFontConfig fnt_cfg{};
fnt_cfg.FontDataOwnedByAtlas = false;
strcpy(fnt_cfg.Name, "Fnt20px");
io.Fonts->AddFontFromMemoryTTF(const_cast<uint8_t*>(font_storopia),
sizeof(font_storopia),
20.f,
&fnt_cfg,
io.Fonts->GetGlyphRangesDefault());
if (m_require_extra != eAlphabetType::LATIN && extra_font_file.exists())
{
fnt_cfg.MergeMode = true;
io.Fonts->AddFontFromFileTTF(extra_font_file.get_path().string().c_str(), 20.f, &fnt_cfg, extra_glyph_range);
}
io.Fonts->Build();
}
// any other font sizes we need to support
for (auto [size, font_ptr] : m_extra_font_sizes)
{
ImFontConfig fnt_cfg{};
fnt_cfg.FontDataOwnedByAtlas = false;
strcpy(fnt_cfg.Name, std::format("Fnt{}px", (int)size).c_str());
*font_ptr = io.Fonts->AddFontFromMemoryTTF(const_cast<uint8_t*>(font_storopia),
sizeof(font_storopia),
size,
&fnt_cfg,
io.Fonts->GetGlyphRangesDefault());
if (m_require_extra != eAlphabetType::LATIN && extra_font_file.exists())
{
fnt_cfg.MergeMode = true;
io.Fonts->AddFontFromFileTTF(extra_font_file.get_path().string().c_str(), size, &fnt_cfg, extra_glyph_range);
}
io.Fonts->Build();
}
// icons blueh
{
ImFontConfig font_icons_cfg{};
font_icons_cfg.FontDataOwnedByAtlas = false;
std::strcpy(font_icons_cfg.Name, "Icons");
g.window.font_icon = ImGui::GetIO().Fonts->AddFontFromMemoryTTF(const_cast<uint8_t*>(font_icons), sizeof(font_icons), 24.f, &font_icons_cfg);
}
g_renderer.post_reset();
m_update_lock.unlock();
}
void font_mgr::update_required_alphabet_type(eAlphabetType type)
{
m_require_extra = type;
g_thread_pool->push([this]
{
rebuild();
});
}
file font_mgr::get_available_font_file_for_alphabet_type()
{
static const auto fonts_folder = std::filesystem::path(std::getenv("SYSTEMROOT")) / "Fonts";
const auto& fonts = m_fonts.find(m_require_extra);
if (fonts == m_fonts.end())
return {};
for (const auto& font : fonts->second)
{
auto font_file = file(fonts_folder / font);
if (font_file.exists())
{
return font_file;
}
}
return {};
}
const ImWchar* font_mgr::GetGlyphRangesChineseSimplifiedOfficial()
{
// Store all official characters for Simplified Chinese.
// Sourced from https://en.wikipedia.org/wiki/Table_of_General_Standard_Chinese_Characters
// (Stored as accumulative offsets from the initial unicode codepoint 0x4E00. This encoding is designed to helps us compact the source code size.)
static const short accumulative_offsets_from_0x4E00[] = {0, 1, 2, 4, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 4, 2, 1, 2, 3, 1, 2, 3, 2, 2, 4, 1, 1, 1, 2, 1, 4, 1, 2, 3, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 3, 1, 1, 1, 1, 1,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 1, 1, 1, 2, 1, 6, 1, 1, 3, 2, 2, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 3, 4, 4, 1, 1, 5, 1, 1, 2, 2, 3, 1, 1, 1, 1, 3, 1, 2, 4, 1, 1, 3, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 2, 1, 1, 2, 1, 2, 1, 3, 2, 4, 1, 3, 1, 3, 1, 4, 1, 2, 1, 3, 2, 1, 2, 1, 1, 1, 3, 2, 1, 4, 6, 1, 2, 2, 5, 1, 3, 1, 1, 2, 1, 5, 1, 3, 1, 2, 2, 1, 1, 1, 2, 1, 4, 4, 5, 2, 1, 3, 3, 1, 2, 6, 2, 3, 7, 1, 1, 6, 1, 1, 6, 1, 4, 3, 4, 5, 1, 7, 6, 1, 5, 13, 2, 1, 2, 2, 3, 1, 7, 2, 1, 2, 3, 1, 1, 4, 1, 2, 2, 3, 2, 1, 19, 5, 4, 12, 4, 15, 7, 1, 2, 2, 3, 3, 2, 1, 11, 3, 6, 2, 1, 4, 1, 1, 3, 1, 2, 3, 5, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 3, 3, 1, 1, 1, 2, 5, 1, 2, 3, 1, 1, 2, 3, 1, 2, 2, 1, 1, 1, 1, 1, 2, 6, 1, 1, 3, 8, 6, 1, 2, 2, 1, 4, 4, 1, 1, 1,
static const short tw_accumulative_offsets_from_0x4E00[] = {130, 28, 232, 90, 41, 34, 30, 43, 1, 28, 45, 108, 79, 120, 52, 80, 18, 3, 3, 52, 20, 4, 4, 1, 81, 17, 542, 78, 2, 198, 5, 14, 132, 7, 1, 3, 286, 35, 11, 113, 5, 72, 2, 69, 3, 526, 116, 10, 17, 1, 68, 1, 23, 61, 458, 64, 23, 30, 2, 1, 84, 17, 68, 29, 16, 296, 58, 99, 122, 56, 322, 41, 114, 24, 165, 42, 10, 22, 22, 130, 150, 193, 618, 8, 57, 84, 17, 6, 70, 241, 98, 8, 12, 93, 515, 133, 26, 93, 50, 7, 73, 98, 56, 19, 7, 11, 26, 164, 40, 104, 13, 6, 4, 23, 60, 18, 13, 4, 63, 67, 132, 1, 35, 13, 3, 134, 108, 70, 192, 364, 7, 4, 450, 19, 106, 89, 39, 8, 146, 182, 202, 4, 321, 8, 7, 6, 31, 11, 34, 11, 34, 26, 29, 16, 99, 89, 6, 233, 36, 173, 213, 72, 148, 15, 284, 189, 27, 34, 159, 222, 36, 24, 72, 18, 32, 246, 157, 73, 32, 2, 127, 1, 32, 62, 205, 37, 113, 12, 21, 63, 90, 31, 19, 265, 10, 11, 19, 47, 3, 16, 461, 3, 43, 17, 10, 2, 6, 15, 14, 3, 19, 88, 12, 152, 1, 9, 54, 5, 343, 236, 24, 18, 20, 27, 131, 51, 55, 29, 61, 11, 250, 9, 3, 8, 237, 5, 17, 13, 13, 2, 17, 30, 25, 9, 77, 186, 17, 5, 21, 123, 30, 21, 34, 8, 167, 5, 2, 58, 33, 34, 14, 7, 21, 117, 5, 81, 2, 10, 39, 23, 91, 99, 184, 276, 172, 165, 8, 59, 3};
static ImWchar base_ranges[] = // not zero-terminated
{
0x0020,
0x00FF, // Basic Latin + Latin Supplement
0x3000,
0x30FF, // Punctuations, Hiragana, Katakana
0x31F0,
0x31FF, // Katakana Phonetic Extensions
0xFF00,
0xFFEF, // Half-width characters
};
static ImWchar full_ranges[IM_ARRAYSIZE(base_ranges) + IM_ARRAYSIZE(accumulative_offsets_from_0x4E00) * 2 + IM_ARRAYSIZE(tw_accumulative_offsets_from_0x4E00) * 2 + 1] = {0};
if (!full_ranges[0])
{
memcpy(full_ranges, base_ranges, sizeof(base_ranges));
ImWchar* out_ranges = full_ranges + IM_ARRAYSIZE(base_ranges);
int base_codepoint = 0x4E00;
for (int n = 0; n < IM_ARRAYSIZE(accumulative_offsets_from_0x4E00); n++, out_ranges += 2)
{
out_ranges[0] = out_ranges[1] = (ImWchar)(base_codepoint + accumulative_offsets_from_0x4E00[n]);
base_codepoint += accumulative_offsets_from_0x4E00[n];
}
base_codepoint = 0x4E00;
for (int n = 0; n < IM_ARRAYSIZE(tw_accumulative_offsets_from_0x4E00); n++, out_ranges += 2)
{
out_ranges[0] = out_ranges[1] = (ImWchar)(base_codepoint + tw_accumulative_offsets_from_0x4E00[n]);
base_codepoint += tw_accumulative_offsets_from_0x4E00[n];
}
out_ranges[0] = 0;
}
return &full_ranges[0];
}
const ImWchar* font_mgr::get_imgui_alphabet_type()
{
auto& io = ImGui::GetIO();
switch (m_require_extra)
{
case eAlphabetType::CHINESE: return GetGlyphRangesChineseSimplifiedOfficial();
case eAlphabetType::CYRILLIC: return io.Fonts->GetGlyphRangesCyrillic();
case eAlphabetType::JAPANESE: return io.Fonts->GetGlyphRangesJapanese();
case eAlphabetType::KOREAN: return io.Fonts->GetGlyphRangesKorean();
default:
case eAlphabetType::LATIN: return io.Fonts->GetGlyphRangesDefault();
}
}
}