unicode fonts support

This commit is contained in:
Timerix 2024-07-30 21:21:30 +03:00
parent 056904fab3
commit 5548e0f4a8
3 changed files with 12 additions and 5 deletions

Binary file not shown.

View File

@ -3,10 +3,15 @@
namespace GraphC::gui::fonts { namespace GraphC::gui::fonts {
// select all glyphs from font
static const ImWchar glyph_ranges[] = {
0x0020, 0xFFFF, 0
};
ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi){ ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi){
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
font_size *= dpi; font_size *= dpi;
return io.Fonts->AddFontFromFileTTF(file_path.c_str(), font_size); return io.Fonts->AddFontFromFileTTF(file_path.c_str(), font_size, nullptr, glyph_ranges);
} }
ImFont* ImFont_LoadEmbedded(const std::string& font_name, f32 font_size, f32 dpi){ ImFont* ImFont_LoadEmbedded(const std::string& font_name, f32 font_size, f32 dpi){
@ -14,12 +19,15 @@ ImFont* ImFont_LoadEmbedded(const std::string& font_name, f32 font_size, f32 dpi
font_size *= dpi; font_size *= dpi;
ImFontConfig font_cfg = ImFontConfig(); ImFontConfig font_cfg = ImFontConfig();
font_cfg.FontDataOwnedByAtlas = false; font_cfg.FontDataOwnedByAtlas = false;
ksprintf(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", font_name, font_size); ksprintf(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s %ipx", font_name.c_str(), (i32)font_size);
const resources::EmbeddedResource** _res = nullptr; const resources::EmbeddedResource** _res = nullptr;
if(!resources::getMap()->tryGet("fonts/" + font_name + ".ttf", &_res)) if(!resources::getMap()->tryGet("fonts/" + font_name + ".ttf", &_res))
throw UsefulException(format("can't load embedded font '%s'", font_name.c_str())); throw UsefulException(format("can't load embedded font '%s'", font_name.c_str()));
const resources::EmbeddedResource* res = *_res; const resources::EmbeddedResource* res = *_res;
return io.Fonts->AddFontFromMemoryTTF((void*)(res->data), res->size, font_size, &font_cfg);
return io.Fonts->AddFontFromMemoryTTF((void*)(res->data), res->size,
font_size, &font_cfg, glyph_ranges);
} }
} }

View File

@ -73,8 +73,7 @@ void GUI::init(const char* window_title){
// Setup Dear ImGui style // Setup Dear ImGui style
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
f32 dpi = getDPI(); f32 dpi = getDPI();
io.FontDefault=fonts:: ImFont_LoadEmbedded(default_font, default_font_size, dpi); io.FontDefault = fonts::ImFont_LoadEmbedded(default_font, default_font_size, dpi);
fonts:: ImFont_LoadEmbedded("Cousine-Regular", default_font_size, dpi);
ImNodes::CreateContext(); ImNodes::CreateContext();
ImNodes::StyleColorsDark(); ImNodes::StyleColorsDark();