1
0
mirror of synced 2024-11-12 02:00:52 +01:00

ui: Fix wide characters and utf-8 display in inspector

This commit is contained in:
WerWolv 2021-08-30 19:12:46 +02:00
parent ef9f0cfca7
commit 32eddbf581
4 changed files with 15 additions and 11 deletions

View File

@ -5,6 +5,8 @@
#include <cstring>
#include <ctime>
#include <codecvt>
#include <locale>
#include <imgui_internal.h>
@ -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<char16_t*>(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<wchar_t*>(buffer.data()), endian);
std::wstring_convert<std::codecvt_utf8<wchar_t>> 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<char8_t*>(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; };

View File

@ -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);

View File

@ -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<char>(c);
}
}

View File

@ -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