diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f15891f..fea9113f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,6 +143,7 @@ endif() if (WIVRN_BUILD_CLIENT AND NOT ANDROID) pkg_check_modules(LIBAV REQUIRED IMPORTED_TARGET libavcodec libavutil libswscale libavfilter) + find_package(Fontconfig REQUIRED) if (WIVRN_USE_SYSTEM_OPENXR STREQUAL "AUTO") find_package(OpenXR 1.0.26) diff --git a/assets/Roboto-Regular.ttf b/assets/Roboto-Regular.ttf deleted file mode 100644 index ddf4bfac..00000000 Binary files a/assets/Roboto-Regular.ttf and /dev/null differ diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index e45ec2a7..b40dffab 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -76,7 +76,7 @@ else() target_link_libraries(wivrn openxr_loader) endif() - target_link_libraries(wivrn PkgConfig::LIBAV) + target_link_libraries(wivrn PkgConfig::LIBAV Fontconfig::Fontconfig) file(GLOB PLATFORM_SOURCE CONFIGURE_DEPENDS @@ -121,7 +121,8 @@ file(GLOB LOCAL_SOURCE CONFIGURE_DEPENDS "utils/*.cpp" "vk/*.cpp" "xr/*.cpp" - "render/*.cpp") + "render/*.cpp" +) file(GLOB_RECURSE VULKAN_SHADERS CONFIGURE_DEPENDS "*.glsl") target_sources(wivrn PRIVATE ${LOCAL_SOURCE} ${VULKAN_SHADERS}) @@ -182,6 +183,7 @@ endif() find_program(GETTEXT_MSGFMT_COMMAND msgfmt REQUIRED) set(MO_FILES) +set(PO_FILES) file(GLOB PO_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/locale/*") foreach(PO_DIR IN LISTS PO_DIRS) cmake_path(GET PO_DIR FILENAME LANG) @@ -191,6 +193,7 @@ foreach(PO_DIR IN LISTS PO_DIRS) else() set(MO_DIR ${CMAKE_CURRENT_BINARY_DIR}/locale/${LANG}/LC_MESSAGES) endif() + set(MO_FILE ${MO_DIR}/wivrn.mo) set(PO_FILE ${PO_DIR}/wivrn.po) @@ -198,14 +201,20 @@ foreach(PO_DIR IN LISTS PO_DIRS) COMMAND ${CMAKE_COMMAND} -E make_directory ${MO_DIR} COMMAND ${GETTEXT_MSGFMT_COMMAND} ${PO_FILE} -o ${MO_FILE} DEPENDS ${PO_FILE}) + set(MO_FILES ${MO_FILES} ${MO_FILE}) + set(PO_FILES ${PO_FILES} ${PO_FILE}) install(FILES ${MO_FILE} DESTINATION ${CMAKE_INSTALL_DATADIR}/locale/${LANG}/LC_MESSAGES) - endforeach() -add_custom_target(translations ALL DEPENDS ${MO_FILES}) +add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp + COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/../tools/extract_charset.py ${PO_FILES} > ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp + DEPENDS ${PO_FILES}) + +add_custom_target(translations ALL DEPENDS ${MO_FILES} ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp) add_dependencies(wivrn translations) +target_sources(wivrn PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/glyph_set.cpp) if(ANDROID) set(LICENSES_DIR "${CMAKE_ANDROID_ASSETS_DIRECTORIES}/licenses") @@ -221,3 +230,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/application.cpp b/client/application.cpp index 59628eec..3ea9fb79 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -859,7 +859,7 @@ void application::initialize() auto & facet = std::use_facet(loc); messages_info.language = facet.language(); messages_info.country = facet.country(); - messages_info.encoding = facet.encoding(); + messages_info.encoding = "UTF-8"; #endif spdlog::info("Current locale: language {}, country {}, encoding {}", messages_info.language, messages_info.country, messages_info.encoding); diff --git a/client/locale/ja/wivrn.po b/client/locale/ja/wivrn.po new file mode 100644 index 00000000..ab2687b1 --- /dev/null +++ b/client/locale/ja/wivrn.po @@ -0,0 +1,216 @@ +msgid "" +msgstr "" +"Project-Id-Version: WiVRn 0.16\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-06-20 10:33+0900\n" +"PO-Revision-Date: 2024-06-20 10:33+0900\n" +"Last-Translator: Mitsuami Megane \n" +"Language: Japanese\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: client/scenes/lobby.cpp:143 +msgid "No supported swapchain format" +msgstr "互換性のあるスワップチェーンは見つからなかった" + +#: client/scenes/lobby.cpp:178 +msgid "Incompatible WiVRn server: no protocol field in TXT" +msgstr "互換性のないサーバー(プロトコルのフィールドは欠落)" + +#: client/scenes/lobby.cpp:181 +#, c++-format +msgid "Incompatible WiVRn server protocol (client: {}, server: {})" +msgstr "互換性のないサーバー(クライエント{}、サーバー{})" + +#: client/scenes/lobby.cpp:196 +#, c++-format +msgid "Cannot resolve hostname: {}" +msgstr "ホスト名解決に失敗:{}" + +#: client/scenes/lobby.cpp:235 +#, c++-format +msgid "Cannot connect to {} ({}): {}" +msgstr "{}に接続できませんでした:({}) {}" + +#: client/scenes/lobby.cpp:347 +msgid "Waiting for connection" +msgstr "接続待ち" + +#: client/scenes/lobby_gui.cpp:84 +msgid "Video stream interrupted" +msgstr "動画ストリームの中断" + +#: client/scenes/lobby_gui.cpp:86 +msgid "Waiting for video stream" +msgstr "動画ストリームを待ち" + +#: client/scenes/lobby_gui.cpp:101 +msgid "Close" +msgstr "閉じる" + +#: client/scenes/lobby_gui.cpp:103 +msgid "Cancel" +msgstr "キャンセル" + +#: client/scenes/lobby_gui.cpp:108 +#, c++-format +msgid "Connection to {}" +msgstr "{}に接続中" + +#: client/scenes/lobby_gui.cpp:136 +msgid "Displayed name" +msgstr "表示名" + +#: client/scenes/lobby_gui.cpp:145 +msgid "Host name" +msgstr "ホスト名" + +#: client/scenes/lobby_gui.cpp:154 +msgid "Port" +msgstr "ポート番号" + +#: client/scenes/lobby_gui.cpp:218 +msgid "" +"Start a WiVRn server on your\n" +"local network" +msgstr "" +"ローカルネットワークでWiVRnサーバーを\n" +"起動してみて下さい" + +#: client/scenes/lobby_gui.cpp:247 +msgid "Autoconnect" +msgstr "自動接続" + +#: client/scenes/lobby_gui.cpp:279 +msgid "Connect" +msgstr "接続" + +#: client/scenes/lobby_gui.cpp:289 +msgid "Incompatible server version" +msgstr "互換性のないサーバー" + +#: client/scenes/lobby_gui.cpp:291 +msgid "Server not available" +msgstr "サーバーは利用不可" + +#: client/scenes/lobby_gui.cpp:305 +msgid "Remove" +msgstr "削除" + +#: client/scenes/lobby_gui.cpp:349 +msgid "Refresh rate" +msgstr "リフレッシュレート" + +#: client/scenes/lobby_gui.cpp:371 +msgid "Resolution scale" +msgstr "解像度スケール" + +#: client/scenes/lobby_gui.cpp:371 client/scenes/lobby_gui.cpp:375 +#, c++-format +msgid "{} - {}x{} per eye" +msgstr "{} - 目あたり{}x{}" + +#: client/scenes/lobby_gui.cpp:386 +msgid "Enable microphone" +msgstr "マイク有効" + +#: client/scenes/lobby_gui.cpp:414 +msgid "Enable video passthrough in lobby" +msgstr "ロビーでパススルーを有効" + +#: client/scenes/lobby_gui.cpp:422 +msgid "Show performance metrics" +msgstr "パフォーマンスグラフを表示" + +#: client/scenes/lobby_gui.cpp:426 +msgid "Overlay can be toggled by pressing both thumbsticks" +msgstr "両方のサムスティックを同時に押すことで切り替え可能" + +#: client/scenes/lobby_gui.cpp:456 client/scenes/lobby_gui.cpp:464 +#: client/scenes/stream_stats.cpp:141 +msgid "CPU time" +msgstr "CPU使用時間" + +#: client/scenes/lobby_gui.cpp:460 +msgid "CPU time [ms]" +msgstr "CPU使用時間、ミリ秒" + +#: client/scenes/lobby_gui.cpp:470 client/scenes/lobby_gui.cpp:478 +#: client/scenes/stream_stats.cpp:143 +msgid "GPU time" +msgstr "GPU使用時間" + +#: client/scenes/lobby_gui.cpp:474 +msgid "GPU time [ms]" +msgstr "GPU使用時間、ミリ秒" + +#: client/scenes/lobby_gui.cpp:502 client/scenes/lobby_gui.cpp:695 +msgid "Licenses" +msgstr "ライセンス" + +#: client/scenes/lobby_gui.cpp:685 +msgid "Server list" +msgstr "サーバー" + +#: client/scenes/lobby_gui.cpp:688 +msgid "Settings" +msgstr "設定" + +#: client/scenes/lobby_gui.cpp:692 +msgid "About" +msgstr "WiVRnについて" + +#: client/scenes/lobby_gui.cpp:698 +msgid "Exit" +msgstr "完了" + +#: client/scenes/stream_stats.cpp:143 +msgid "Reproject" +msgstr "リプロジェクション" + +#: client/scenes/stream_stats.cpp:144 +msgid "Blit" +msgstr "レンダー" + +#: client/scenes/stream_stats.cpp:146 +msgid "Download" +msgstr "ダウンロード" + +#: client/scenes/stream_stats.cpp:147 +msgid "Upload" +msgstr "アップロード" + +#: client/scenes/stream_stats.cpp:216 +#, c++-format +msgid "Decoder {}" +msgstr "デコーダー {}" + +#: client/scenes/stream_stats.cpp:228 +msgid "Timings [ms]" +msgstr "タイミング、ミリ秒" + +#: client/scenes/stream_stats.cpp:273 +msgid "Send" +msgstr "送信" + +#: client/scenes/stream_stats.cpp:274 +msgid "Receive" +msgstr "受信" + +#: client/scenes/stream_stats.cpp:275 +msgid "Decode" +msgstr "デコード" + +#: client/scenes/stream_stats.cpp:276 +msgid "Blitted" +msgstr "レンダー" + +#: client/scenes/stream_stats.cpp:277 +msgid "Displayed" +msgstr "表示" + +#: client/scenes/stream_stats.cpp:293 +#, c++-format +msgid "Estimated motion to photons latency: {}ms" +msgstr "「Motion-to-photon」推定遅延 {}ミリ秒" diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index cf87aa1b..d5530847 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -36,11 +36,21 @@ #include #include #include +#include #include #include +#include #include "IconsFontAwesome6.h" +#ifdef __ANDROID__ +#include +#include +#include +#else +#include +#endif + /* Do not use: * * ImGui_ImplVulkanH_SelectSurfaceFormat @@ -53,6 +63,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 +119,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 +276,221 @@ 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; + } + } +} + +#ifdef __ANDROID__ +static std::vector find_font(const ImFontGlyphRangesBuilder & glyph_range_builder, const std::string & locale) +{ + std::u16string wtext; + std::u16string_view wtextview; + for (int i = 0; i < glyph_range_builder.UsedChars.Size; ++i) + { + std::uint32_t used_chars = glyph_range_builder.UsedChars[i]; + if (used_chars == 0) + continue; + + for (int j = 0; j < 32; ++j) + { + ImWchar c = i * 32 + j; + if (used_chars & (1 << j)) + { + wtext += c; + } + } + } + wtextview = wtext; + + AFontMatcher * font_matcher = AFontMatcher_create(); + if (!font_matcher) + throw std::runtime_error("AFontMatcher_create"); + + AFontMatcher_setFamilyVariant(font_matcher, AFAMILY_VARIANT_DEFAULT); + AFontMatcher_setLocales(font_matcher, locale.c_str()); + AFontMatcher_setStyle(font_matcher, AFONT_WEIGHT_NORMAL, false); + + std::vector fonts; + + while (!wtextview.empty()) + { + uint32_t runlength = 0; + + AFont * font = AFontMatcher_match(font_matcher, "sans-serif", (uint16_t *)wtextview.data(), wtextview.size(), &runlength); + fonts.emplace_back(AFont_getFontFilePath(font)); + AFont_close(font); + + if (runlength == 0) + break; + + wtextview = wtextview.substr(runlength); + } + + AFontMatcher_destroy(font_matcher); + + return fonts; +} +#else +static std::vector find_font(const ImFontGlyphRangesBuilder & glyph_range_builder, const std::string & locale) +{ + // See https://www.camconn.cc/post/how-to-fontconfig-lib-c/ + + static FcConfig * config = FcInitLoadConfigAndFonts(); + + FcPattern * pattern = FcNameParse((const FcChar8 *)"Noto Sans"); + if (!pattern) + throw std::runtime_error("Failed to create Fontconfig pattern"); + + FcConfigSubstitute(config, pattern, FcMatchPattern); + FcDefaultSubstitute(pattern); + + // Add the locale/language to the pattern + FcPatternAddString(pattern, FC_LANG, (const FcChar8 *)locale.c_str()); + + // Add the text to the pattern (to determine appropriate characters) + FcCharSet * charset = FcCharSetCreate(); + + for (int i = 8; i < glyph_range_builder.UsedChars.Size; ++i) + { + std::uint32_t used_chars = glyph_range_builder.UsedChars[i]; + if (used_chars == 0) + continue; + + for (int j = 0; j < 32; ++j) + { + ImWchar c = i * 32 + j; + if (used_chars & (1 << j)) + { + FcCharSetAddChar(charset, c); + } + } + } + + FcPatternAddCharSet(pattern, FC_CHARSET, charset); + FcCharSetDestroy(charset); + + auto fs = FcFontSetCreate(); + + FcResult result; + FcFontSet * font_patterns = FcFontSort(config, pattern, FcTrue, nullptr, &result); + + if (!font_patterns || font_patterns->nfont == 0) + { + FcFontSetDestroy(fs); + FcPatternDestroy(pattern); + throw std::runtime_error("Fontconfig could not find ANY fonts on the system?"); + } + + FcFontSetPrint(font_patterns); + + std::vector fonts; + for (int i = 0; i < font_patterns->nfont; i++) + { + FcChar8 * font_file; + if (FcPatternGetString(font_patterns->fonts[i], FC_FILE, 0, &font_file) == FcResultMatch) + { + fonts.emplace_back((const char *)font_file); + } + } + + FcFontSetDestroy(fs); + FcPatternDestroy(pattern); + + return fonts; +} +#endif + +void imgui_context::initialize_fonts() +{ + glyph_ranges.clear(); + + // Always include Basic Latin and Latin-1 Supplement without control characters + ImWchar default_ranges[] = {0x20, 0x7f, 0xa0, 0xff, 0}; + glyph_range_builder.AddRanges(default_ranges); + + 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; + } + + for (int i = 0; i < glyph_range_builder.UsedChars.Size; ++i) + { + std::uint32_t used_chars = glyph_range_builder.UsedChars[i]; + + if (used_chars == 0) + continue; + + for (int j = 0; j < 32; ++j) + { + ImWchar c = i * 32 + j; + if (used_chars & (1 << j)) + { + char buf[8]; + ImTextCharToUtf8(buf, c); + } + } + } + + glyph_range_builder.BuildRanges(&glyph_ranges); + // Load Fonts - asset roboto("Roboto-Regular.ttf"); + auto fonts = find_font(glyph_range_builder, application::get_messages_info().language); + for (auto & i: fonts) + { + spdlog::info("Font {}", i); + } + 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); + + for (auto & font: fonts) + { + spdlog::info("Using font {}", font, application::get_messages_info().language); + io.Fonts->AddFontFromFileTTF(font.c_str(), 30, &config, glyph_ranges.Data); + config.MergeMode = true; + } config.MergeMode = true; config.GlyphMinAdvanceX = 40; // Use if you want to make the icon monospaced @@ -294,17 +502,16 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii: { ImFontConfig config; config.FontDataOwnedByAtlas = false; - large_font = io.Fonts->AddFontFromMemoryTTF(const_cast(roboto.data()), roboto.size(), 75, &config); + for (auto & font: fonts) + { + large_font = io.Fonts->AddFontFromFileTTF(font.c_str(), 75, &config, glyph_ranges.Data); + config.MergeMode = true; + } } - // Setup Dear ImGui style - ImGui::StyleColorsDark(); - // ImGui::StyleColorsClassic(); - // ImGui::StyleColorsLight(); + glyph_range_dirty = false; - ImGuiStyle & style = ImGui::GetStyle(); - - style.WindowBorderSize = 0; + ImGui_ImplVulkan_CreateFontsTexture(); } void imgui_context::new_frame(XrTime display_time) @@ -477,6 +684,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..65726edf 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(); diff --git a/tools/extract_charset.py b/tools/extract_charset.py new file mode 100755 index 00000000..405afbd0 --- /dev/null +++ b/tools/extract_charset.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +import sys +import os.path + +print( +"""// Generated file, do not edit +#include +#include +#include + +std::map> glyph_set_per_language = {""") + +for po_file in sys.argv[1:]: + lang = os.path.basename(os.path.dirname(po_file)) + + with open(po_file) as f: + charset = sorted(set(f.read())) + + # Basic Latin and Latin-1 Supplement are always included + charset = ', '.join([str(ord(c)) for c in charset if ord(c) > 255]) + + print("\t{{ \"{}\", {{{}}}}},".format(lang, charset)) + +print("};")