From 78b26c53fdb3d623a2d1621c43d09c8be835ec7e Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Sun, 16 Jun 2024 11:29:29 +0200 Subject: [PATCH] Automatically generate the glyph range for locales --- client/CMakeLists.txt | 26 +++++++++- client/render/imgui_impl.cpp | 98 ++++++++++++++++++++++++++++-------- client/render/imgui_impl.h | 8 +++ client/scenes/lobby_gui.cpp | 5 ++ 4 files changed, 115 insertions(+), 22 deletions(-) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index e45ec2a7..1d37bf3d 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -121,7 +121,9 @@ file(GLOB LOCAL_SOURCE CONFIGURE_DEPENDS "utils/*.cpp" "vk/*.cpp" "xr/*.cpp" - "render/*.cpp") + "render/*.cpp" + ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp +) file(GLOB_RECURSE VULKAN_SHADERS CONFIGURE_DEPENDS "*.glsl") target_sources(wivrn PRIVATE ${LOCAL_SOURCE} ${VULKAN_SHADERS}) @@ -183,6 +185,11 @@ find_program(GETTEXT_MSGFMT_COMMAND msgfmt REQUIRED) set(MO_FILES) file(GLOB PO_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/locale/*") +file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp "// generated file\n") +file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp "#include \n") +file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp "#include \n") +file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp "#include \n") +file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp "std::map> glyph_set_per_language = {\n") foreach(PO_DIR IN LISTS PO_DIRS) cmake_path(GET PO_DIR FILENAME LANG) @@ -200,9 +207,19 @@ foreach(PO_DIR IN LISTS PO_DIRS) DEPENDS ${PO_FILE}) set(MO_FILES ${MO_FILES} ${MO_FILE}) + # Characters 0x20 to 0xff are always included + execute_process( + COMMAND python -c "print('{\"${LANG}\", {' + ', '.join([str(ord(c)) for c in sorted(set(open('wivrn.po').read())) if ord(c) > 255]) + '}},')" + WORKING_DIRECTORY ${PO_DIR} + # OUTPUT_QUIET + OUTPUT_VARIABLE out + ) + file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp ${out}) + install(FILES ${MO_FILE} DESTINATION ${CMAKE_INSTALL_DATADIR}/locale/${LANG}/LC_MESSAGES) endforeach() +file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp "};\n") add_custom_target(translations ALL DEPENDS ${MO_FILES}) add_dependencies(wivrn translations) @@ -221,3 +238,10 @@ if (OPENXR_SOURCE_DIR) endif() install(DIRECTORY "${LICENSES_DIR}" DESTINATION "${CMAKE_INSTALL_DATADIR}/wivrn") + +if(NOT ANDROID) + set_target_properties(wivrn PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/share/wivrn) + file(CREATE_LINK ${CMAKE_SOURCE_DIR}/assets ${CMAKE_BINARY_DIR}/share/wivrn/assets SYMBOLIC) + file(CREATE_LINK ${CMAKE_CURRENT_BINARY_DIR}/locale ${CMAKE_BINARY_DIR}/share/locale SYMBOLIC) +endif() diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index cf87aa1b..05a37522 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -53,6 +53,9 @@ * struct ImGui_ImplVulkanH_Window */ +// Automatically generated by CMakeLists +extern std::map> 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> glyph_ranges_per_language = - { - {"fr", {0x0020, 0x017f, 0}} // Basic Latin + Latin Supplement + Latin Extended-A -}; - std::optional> 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(roboto.data()), roboto.size(), 30, &config, range); + io.Fonts->AddFontFromMemoryTTF(const_cast(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(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 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(); diff --git a/client/render/imgui_impl.h b/client/render/imgui_impl.h index a2b42e47..64f55dbf 100644 --- a/client/render/imgui_impl.h +++ b/client/render/imgui_impl.h @@ -131,6 +131,12 @@ private: bool button_pressed = false; + ImFontGlyphRangesBuilder glyph_range_builder; + ImVector glyph_ranges; + bool glyph_range_dirty = true; + + void initialize_fonts(); + public: imgui_context( vk::raii::PhysicalDevice physical_device, @@ -210,4 +216,6 @@ public: ImTextureID load_texture(const std::string & filename); void free_texture(ImTextureID); void set_current(); + + void add_chars(std::string_view sv); }; diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index 5f9c573f..efa9f020 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -609,6 +609,11 @@ static void ScrollWhenDraggingOnVoid(ImVec2 delta) XrCompositionLayerQuad scenes::lobby::draw_gui(XrTime predicted_display_time) { + for(const auto& [key, server]: application::get_config().servers) + { + imgui_ctx->add_chars(server.service.name); + } + imgui_ctx->new_frame(predicted_display_time); ImGuiStyle & style = ImGui::GetStyle();