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
+25 -1
View File
@@ -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 <map>\n")
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp "#include <string>\n")
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp "#include <vector>\n")
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp "std::map<std::string, std::vector<int>> 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()
+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();
+8
View File
@@ -131,6 +131,12 @@ private:
bool button_pressed = false;
ImFontGlyphRangesBuilder glyph_range_builder;
ImVector<ImWchar> 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);
};
+5
View File
@@ -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();