Automatically generate the glyph range for locales

This commit is contained in:
Guillaume Meunier
2024-06-20 21:24:31 +02:00
parent 02432b730b
commit 78b26c53fd
4 changed files with 115 additions and 22 deletions
+77 -21
View File
@@ -53,6 +53,9 @@
* struct ImGui_ImplVulkanH_Window
*/
// Automatically generated by CMakeLists
extern std::map<std::string, std::vector<int>> glyph_set_per_language;
static vk::raii::RenderPass create_renderpass(vk::raii::Device & device, vk::Format format, bool clear)
{
vk::AttachmentDescription attachment{
@@ -106,11 +109,6 @@ static void check_vk_result(VkResult result)
}
}
const std::map<std::string, std::vector<ImWchar>> glyph_ranges_per_language =
{
{"fr", {0x0020, 0x017f, 0}} // Basic Latin + Latin Supplement + Latin Extended-A
};
std::optional<std::pair<ImVec2, float>> imgui_context::ray_plane_intersection(const imgui_context::controller_state & in) const
{
if (!in.active)
@@ -268,21 +266,68 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii:
ImPlot::SetCurrentContext(plot_context);
ImGui_ImplVulkan_Init(&init_info, *renderpass);
initialize_fonts();
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// ImGui::StyleColorsClassic();
// ImGui::StyleColorsLight();
ImGuiStyle & style = ImGui::GetStyle();
style.WindowBorderSize = 0;
}
void imgui_context::add_chars(std::string_view sv)
{
for(auto it = sv.begin(); it != sv.end(); )
{
ImWchar c = (ImWchar)*it;
if (c < 0x80)
++it;
else
it += ImTextCharFromUtf8(&c, it, sv.end());
if (!glyph_range_builder.GetBit(c))
{
glyph_range_builder.AddChar(c);
glyph_range_dirty = true;
}
}
}
void imgui_context::initialize_fonts()
{
glyph_ranges.clear();
glyph_range_builder.AddRanges(io.Fonts->GetGlyphRangesDefault());
for(auto language = application::get_messages_info().language; language != ""; language = language.substr(0, language.size() - 1))
{
auto it = glyph_set_per_language.find(language);
if (it == glyph_set_per_language.end())
continue;
for(ImWchar c: it->second)
glyph_range_builder.AddChar(c);
break;
}
glyph_range_builder.BuildRanges(&glyph_ranges);
// Load Fonts
asset roboto("Roboto-Regular.ttf");
asset font_awesome_regular("Font Awesome 6 Free-Regular-400.otf");
asset font_awesome_solid("Font Awesome 6 Free-Solid-900.otf");
IM_DELETE(io.Fonts);
io.Fonts = IM_NEW(ImFontAtlas);
{
auto language = application::get_messages_info().language;
const ImWchar * range = io.Fonts->GetGlyphRangesDefault();
if (auto it = glyph_ranges_per_language.find(language); it != glyph_ranges_per_language.end())
range = it->second.data();
ImFontConfig config;
config.FontDataOwnedByAtlas = false;
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte *>(roboto.data()), roboto.size(), 30, &config, range);
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte *>(roboto.data()), roboto.size(), 30, &config, glyph_ranges.Data);
config.MergeMode = true;
config.GlyphMinAdvanceX = 40; // Use if you want to make the icon monospaced
@@ -297,14 +342,7 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii:
large_font = io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte *>(roboto.data()), roboto.size(), 75, &config);
}
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// ImGui::StyleColorsClassic();
// ImGui::StyleColorsLight();
ImGuiStyle & style = ImGui::GetStyle();
style.WindowBorderSize = 0;
glyph_range_dirty = false;
}
void imgui_context::new_frame(XrTime display_time)
@@ -477,6 +515,24 @@ void imgui_context::new_frame(XrTime display_time)
controller.second = next_state;
}
// Force recreation of the font atlas
if (glyph_range_dirty)
{
// Wait for fences before ImGui_ImplVulkan_DestroyFontsTexture is called
std::vector<vk::Fence> fences;
for (auto & f: command_buffers)
{
fences.push_back(*f.fence);
}
if (auto result = device.waitForFences(fences, true, 1'000'000'000); result != vk::Result::eSuccess)
spdlog::error("vkWaitForfences: {}", vk::to_string(result));
ImGui_ImplVulkan_DestroyFontsTexture();
initialize_fonts();
ImGui_ImplVulkan_CreateFontsTexture();
}
// Start the Dear ImGui frame
ImGui_ImplVulkan_NewFrame();