From 32eddbf581f6f7ea4446bc625860c75360f3eb4a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 30 Aug 2021 19:12:46 +0200 Subject: [PATCH] ui: Fix wide characters and utf-8 display in inspector --- .../builtin/source/content/data_inspector.cpp | 17 ++++++++++------- plugins/libimhex/include/hex/helpers/utils.hpp | 2 +- plugins/libimhex/source/helpers/utils.cpp | 5 +++-- source/window/window.cpp | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/plugins/builtin/source/content/data_inspector.cpp b/plugins/builtin/source/content/data_inspector.cpp index e7a9f05b8..e43ee014b 100644 --- a/plugins/builtin/source/content/data_inspector.cpp +++ b/plugins/builtin/source/content/data_inspector.cpp @@ -5,6 +5,8 @@ #include #include +#include +#include #include @@ -95,9 +97,12 @@ namespace hex::plugin::builtin { return [value] { ImGui::TextUnformatted(value.c_str()); return value; }; }); - ContentRegistry::DataInspector::add("hex.builtin.inspector.wide", sizeof(char16_t), [](auto buffer, auto endian, auto style) { - auto c = *reinterpret_cast(buffer.data()); - auto value = hex::format("'{0}'", c == 0 ? '\x01' : char16_t(hex::changeEndianess(c, endian))); + ContentRegistry::DataInspector::add("hex.builtin.inspector.wide", sizeof(wchar_t), [](auto buffer, auto endian, auto style) { + auto c = hex::changeEndianess(*reinterpret_cast(buffer.data()), endian); + + std::wstring_convert> converter; + + auto value = hex::format("'{0}'", c <= 255 ? makePrintable(c) : converter.to_bytes(c)); return [value] { ImGui::TextUnformatted(value.c_str()); return value; }; }); @@ -109,10 +114,8 @@ namespace hex::plugin::builtin { std::memcpy(utf8Buffer, reinterpret_cast(buffer.data()), 4); u8 codepointSize = ImTextCharFromUtf8(&codepoint, utf8Buffer, utf8Buffer + 4); - std::memcpy(codepointString, &codepoint, std::min(codepointSize, u8(4))); - auto value = hex::format("'{0}' (U+0x{1:04X})", codepoint == 0xFFFD ? "Invalid" : - codepoint < 0xFF ? makePrintable(codepoint).c_str() : - codepointString, + std::memcpy(codepointString, utf8Buffer, std::min(codepointSize, u8(4))); + auto value = hex::format("'{0}' (U+0x{1:04X})", codepoint == 0xFFFD ? "Invalid" : codepointString, codepoint); return [value] { ImGui::TextUnformatted(value.c_str()); return value; }; diff --git a/plugins/libimhex/include/hex/helpers/utils.hpp b/plugins/libimhex/include/hex/helpers/utils.hpp index 8cdc8e226..9e5fc9f9b 100644 --- a/plugins/libimhex/include/hex/helpers/utils.hpp +++ b/plugins/libimhex/include/hex/helpers/utils.hpp @@ -32,7 +32,7 @@ namespace hex { std::string to_string(s128 value); std::string toByteString(u64 bytes); - std::string makePrintable(char c); + std::string makePrintable(u8 c); void runCommand(const std::string &command); void openWebpage(std::string url); diff --git a/plugins/libimhex/source/helpers/utils.cpp b/plugins/libimhex/source/helpers/utils.cpp index adb987f82..e6db56bc5 100644 --- a/plugins/libimhex/source/helpers/utils.cpp +++ b/plugins/libimhex/source/helpers/utils.cpp @@ -78,7 +78,7 @@ namespace hex { return result; } - std::string makePrintable(char c) { + std::string makePrintable(u8 c) { switch (c) { case 0: return "NUL"; case 1: return "SOH"; @@ -114,7 +114,8 @@ namespace hex { case 31: return "US"; case 32: return "Space"; case 127: return "DEL"; - default: return std::string() + c; + case 128 ... 255: return " "; + default: return std::string() + static_cast(c); } } diff --git a/source/window/window.cpp b/source/window/window.cpp index ddaf926b8..909ef998c 100644 --- a/source/window/window.cpp +++ b/source/window/window.cpp @@ -326,7 +326,7 @@ namespace hex { io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 13.0f * this->m_fontScale, &cfg, fontAwesomeRange); io.Fonts->AddFontFromMemoryCompressedTTF(codicons_compressed_data, codicons_compressed_size, 13.0f * this->m_fontScale, &cfg, codiconsRange); - ImGuiFreeType::BuildFontAtlas(io.Fonts); + ImGuiFreeType::BuildFontAtlas(io.Fonts, ImGuiFreeTypeBuilderFlags_Bitmap); io.Fonts->GetTexDataAsRGBA32(&px, &w, &h); // Create new font atlas