overlays: Ditch wstring for u32string

- Turns out wstring is not the same as u32string on windows.
This commit is contained in:
kd-11 2020-02-24 19:03:19 +03:00 committed by kd-11
parent 13ef0cc8c4
commit f6ebd88687
9 changed files with 118 additions and 132 deletions

View File

@ -331,7 +331,7 @@ namespace rsx
u16 w = 0;
u16 h = 0;
std::wstring text;
std::u32string text;
font* font_ref = nullptr;
text_align alignment = left;
bool wrap_text = false;
@ -433,11 +433,11 @@ namespace rsx
virtual void set_text(const std::string& text)
{
this->text = utf8_to_wstring(text);
this->text = utf8_to_u32string(text);
is_compiled = false;
}
virtual void set_text(const std::wstring& text)
virtual void set_text(const std::u32string& text)
{
this->text = text;
is_compiled = false;
@ -466,7 +466,7 @@ namespace rsx
return font_ref ? font_ref : fontmgr::get("Arial", 12);
}
virtual std::vector<vertex> render_text(const wchar_t *string, f32 x, f32 y)
virtual std::vector<vertex> render_text(const char32_t *string, f32 x, f32 y)
{
auto renderer = get_font();
@ -1036,7 +1036,7 @@ namespace rsx
int get_selected_index();
std::wstring get_selected_item();
std::u32string get_selected_item();
void set_cancel_only(bool cancel_only);
void translate(s16 _x, s16 _y) override;
@ -1060,7 +1060,7 @@ namespace rsx
using label::label;
void move_caret(direction dir);
void insert_text(const std::wstring& str);
void insert_text(const std::u32string& str);
void erase();
compiled_resource& get_compiled() override;

View File

@ -34,7 +34,7 @@ namespace rsx
}
}
void edit_text::insert_text(const std::wstring& str)
void edit_text::insert_text(const std::u32string& str)
{
if (caret_position == 0)
{
@ -65,7 +65,7 @@ namespace rsx
if (caret_position == 1)
{
text = text.length() > 1 ? text.substr(1) : L"";
text = text.length() > 1 ? text.substr(1) : U"";
}
else if (caret_position == text.length())
{

View File

@ -6,7 +6,7 @@ namespace rsx
{
namespace overlays
{
void codepage::initialize_glyphs(u16 codepage_id, f32 font_size, const std::vector<u8>& ttf_data)
void codepage::initialize_glyphs(char32_t codepage_id, f32 font_size, const std::vector<u8>& ttf_data)
{
glyph_base = (codepage_id * 256);
glyph_data.resize(bitmap_width * bitmap_height);
@ -31,7 +31,7 @@ namespace rsx
stbtt_PackEnd(&context);
}
stbtt_aligned_quad codepage::get_char(wchar_t c, f32& x_advance, f32& y_advance)
stbtt_aligned_quad codepage::get_char(char32_t c, f32& x_advance, f32& y_advance)
{
stbtt_aligned_quad quad;
stbtt_GetPackedQuad(pack_info.data(), bitmap_width, bitmap_height, (c - glyph_base), &x_advance, &y_advance, &quad, false);
@ -51,7 +51,7 @@ namespace rsx
initialized = true;
}
language_class font::classify(u16 codepage_id)
language_class font::classify(char32_t codepage_id)
{
switch (codepage_id)
{
@ -151,7 +151,7 @@ namespace rsx
return result;
}
codepage* font::initialize_codepage(u16 codepage_id)
codepage* font::initialize_codepage(char32_t codepage_id)
{
// Init glyph
const auto class_ = classify(codepage_id);
@ -210,7 +210,7 @@ namespace rsx
}
else
{
rsx_log.error("Failed to initialize font '%s.ttf' on codepage %d", font_name, codepage_id);
rsx_log.error("Failed to initialize font '%s.ttf' on codepage %d", font_name, static_cast<u32>(codepage_id));
return nullptr;
}
@ -232,7 +232,7 @@ namespace rsx
return ret;
}
stbtt_aligned_quad font::get_char(wchar_t c, f32& x_advance, f32& y_advance)
stbtt_aligned_quad font::get_char(char32_t c, f32& x_advance, f32& y_advance)
{
if (!initialized)
return {};
@ -265,7 +265,7 @@ namespace rsx
}
}
void font::render_text_ex(std::vector<vertex>& result, f32& x_advance, f32& y_advance, const wchar_t* text, u32 char_limit, u16 max_width, bool wrap)
void font::render_text_ex(std::vector<vertex>& result, f32& x_advance, f32& y_advance, const char32_t* text, u32 char_limit, u16 max_width, bool wrap)
{
x_advance = 0.f;
y_advance = 0.f;
@ -401,7 +401,7 @@ namespace rsx
}
}
std::vector<vertex> font::render_text(const wchar_t* text, u16 max_width, bool wrap)
std::vector<vertex> font::render_text(const char32_t* text, u16 max_width, bool wrap)
{
std::vector<vertex> result;
f32 unused_x, unused_y;
@ -410,7 +410,7 @@ namespace rsx
return result;
}
std::pair<f32, f32> font::get_char_offset(const wchar_t* text, u16 max_length, u16 max_width, bool wrap)
std::pair<f32, f32> font::get_char_offset(const char32_t* text, u16 max_length, u16 max_width, bool wrap)
{
std::vector<vertex> unused;
f32 loc_x, loc_y;

View File

@ -37,11 +37,11 @@ namespace rsx
std::vector<stbtt_packedchar> pack_info;
std::vector<u8> glyph_data;
u16 glyph_base = 0;
char32_t glyph_base = 0;
f32 sampler_z = 0.f;
void initialize_glyphs(u16 codepage_id, f32 font_size, const std::vector<u8>& ttf_data);
stbtt_aligned_quad get_char(wchar_t c, f32& x_advance, f32& y_advance);
void initialize_glyphs(char32_t codepage_id, f32 font_size, const std::vector<u8>& ttf_data);
stbtt_aligned_quad get_char(char32_t c, f32& x_advance, f32& y_advance);
};
class font
@ -52,30 +52,30 @@ namespace rsx
f32 em_size = 0.f;
std::string font_name;
std::vector<std::pair<u32, std::unique_ptr<codepage>>> m_glyph_map;
std::vector<std::pair<char32_t, std::unique_ptr<codepage>>> m_glyph_map;
bool initialized = false;
struct
{
u16 codepage_id = 0;
char32_t codepage_id = 0;
codepage* page = nullptr;
}
codepage_cache;
language_class classify(u16 page);
language_class classify(char32_t page);
glyph_load_setup get_glyph_files(language_class class_);
codepage* initialize_codepage(u16 page);
codepage* initialize_codepage(char32_t page);
public:
font(const char* ttf_name, f32 size);
stbtt_aligned_quad get_char(wchar_t c, f32& x_advance, f32& y_advance);
stbtt_aligned_quad get_char(char32_t c, f32& x_advance, f32& y_advance);
void render_text_ex(std::vector<vertex>& result, f32& x_advance, f32& y_advance, const wchar_t* text, u32 char_limit, u16 max_width, bool wrap);
void render_text_ex(std::vector<vertex>& result, f32& x_advance, f32& y_advance, const char32_t* text, u32 char_limit, u16 max_width, bool wrap);
std::vector<vertex> render_text(const wchar_t* text, u16 max_width = UINT16_MAX, bool wrap = false);
std::vector<vertex> render_text(const char32_t* text, u16 max_width = UINT16_MAX, bool wrap = false);
std::pair<f32, f32> get_char_offset(const wchar_t* text, u16 max_length, u16 max_width = UINT16_MAX, bool wrap = false);
std::pair<f32, f32> get_char_offset(const char32_t* text, u16 max_length, u16 max_width = UINT16_MAX, bool wrap = false);
bool matches(const char* name, int size) const { return font_name == name && static_cast<int>(size_pt) == size; }
std::string_view get_name() const { return font_name; };

View File

@ -150,7 +150,7 @@ namespace rsx
return m_selected_entry;
}
std::wstring list_view::get_selected_item()
std::u32string list_view::get_selected_item()
{
if (m_selected_entry < 0)
return {};

View File

@ -29,7 +29,7 @@ namespace rsx
fade_animation.active = true;
}
void osk_dialog::initialize_layout(const std::vector<grid_entry_ctor>& layout, const std::wstring& title, const std::wstring& initial_text)
void osk_dialog::initialize_layout(const std::vector<grid_entry_ctor>& layout, const std::u32string& title, const std::u32string& initial_text)
{
const u32 cell_count = num_rows * num_columns;
@ -370,7 +370,7 @@ namespace rsx
}
case pad_button::select:
{
on_shift(L"");
on_shift(U"");
break;
}
case pad_button::start:
@ -380,12 +380,12 @@ namespace rsx
}
case pad_button::triangle:
{
on_space(L"");
on_space(U"");
break;
}
case pad_button::square:
{
on_backspace(L"");
on_backspace(U"");
break;
}
case pad_button::cross:
@ -405,7 +405,7 @@ namespace rsx
void osk_dialog::on_text_changed()
{
const auto ws = wstring_to_utf16(m_preview.text);
const auto ws = u32string_to_utf16(m_preview.text);
const auto length = (ws.length() + 1) * sizeof(char16_t);
memcpy(osk_text, ws.c_str(), length);
@ -417,10 +417,10 @@ namespace rsx
m_update = true;
}
void osk_dialog::on_default_callback(const std::wstring& str)
void osk_dialog::on_default_callback(const std::u32string& str)
{
// Append to output text
if (m_preview.text == L"[Enter Text]")
if (m_preview.text == U"[Enter Text]")
{
m_preview.caret_position = ::narrow<u16>(str.length());
m_preview.set_text(str);
@ -443,17 +443,17 @@ namespace rsx
on_text_changed();
}
void osk_dialog::on_shift(const std::wstring&)
void osk_dialog::on_shift(const std::u32string&)
{
selected_z = (selected_z + 1) % num_layers;
m_update = true;
}
void osk_dialog::on_space(const std::wstring&)
void osk_dialog::on_space(const std::u32string&)
{
if (!(flags & CELL_OSKDIALOG_NO_SPACE))
{
on_default_callback(L" ");
on_default_callback(U" ");
}
else
{
@ -461,7 +461,7 @@ namespace rsx
}
}
void osk_dialog::on_backspace(const std::wstring&)
void osk_dialog::on_backspace(const std::u32string&)
{
if (m_preview.text.empty())
{
@ -472,11 +472,11 @@ namespace rsx
on_text_changed();
}
void osk_dialog::on_enter(const std::wstring&)
void osk_dialog::on_enter(const std::u32string&)
{
if (!(flags & CELL_OSKDIALOG_NO_RETURN))
{
on_default_callback(L"\n");
on_default_callback(U"\n");
}
else
{
@ -617,57 +617,57 @@ namespace rsx
std::vector<osk_dialog::grid_entry_ctor> layout =
{
// Alphanumeric
{{L"1", L"!"}, default_bg, 1},
{{L"2", L"@"}, default_bg, 1},
{{L"3", L"#"}, default_bg, 1},
{{L"4", L"$"}, default_bg, 1},
{{L"5", L"%"}, default_bg, 1},
{{L"6", L"^"}, default_bg, 1},
{{L"7", L"&"}, default_bg, 1},
{{L"8", L"*"}, default_bg, 1},
{{L"9", L"("}, default_bg, 1},
{{L"0", L")"}, default_bg, 1},
{{U"1", U"!"}, default_bg, 1},
{{U"2", U"@"}, default_bg, 1},
{{U"3", U"#"}, default_bg, 1},
{{U"4", U"$"}, default_bg, 1},
{{U"5", U"%"}, default_bg, 1},
{{U"6", U"^"}, default_bg, 1},
{{U"7", U"&"}, default_bg, 1},
{{U"8", U"*"}, default_bg, 1},
{{U"9", U"("}, default_bg, 1},
{{U"0", U")"}, default_bg, 1},
// Alpha
{{L"q", L"Q"}, default_bg, 1},
{{L"w", L"W"}, default_bg, 1},
{{L"e", L"E"}, default_bg, 1},
{{L"r", L"R"}, default_bg, 1},
{{L"t", L"T"}, default_bg, 1},
{{L"y", L"Y"}, default_bg, 1},
{{L"u", L"U"}, default_bg, 1},
{{L"i", L"I"}, default_bg, 1},
{{L"o", L"O"}, default_bg, 1},
{{L"p", L"P"}, default_bg, 1},
{{L"a", L"A"}, default_bg, 1},
{{L"s", L"S"}, default_bg, 1},
{{L"d", L"D"}, default_bg, 1},
{{L"f", L"F"}, default_bg, 1},
{{L"g", L"G"}, default_bg, 1},
{{L"h", L"H"}, default_bg, 1},
{{L"j", L"J"}, default_bg, 1},
{{L"k", L"K"}, default_bg, 1},
{{L"l", L"L"}, default_bg, 1},
{{L"'", L"\""}, default_bg, 1},
{{L"z", L"Z"}, default_bg, 1},
{{L"x", L"X"}, default_bg, 1},
{{L"c", L"C"}, default_bg, 1},
{{L"v", L"V"}, default_bg, 1},
{{L"b", L"B"}, default_bg, 1},
{{L"n", L"N"}, default_bg, 1},
{{L"m", L"M"}, default_bg, 1},
{{L"-", L"_"}, default_bg, 1},
{{L"+", L"="}, default_bg, 1},
{{L",", L"?"}, default_bg, 1},
{{U"q", U"Q"}, default_bg, 1},
{{U"w", U"W"}, default_bg, 1},
{{U"e", U"E"}, default_bg, 1},
{{U"r", U"R"}, default_bg, 1},
{{U"t", U"T"}, default_bg, 1},
{{U"y", U"Y"}, default_bg, 1},
{{U"u", U"U"}, default_bg, 1},
{{U"i", U"I"}, default_bg, 1},
{{U"o", U"O"}, default_bg, 1},
{{U"p", U"P"}, default_bg, 1},
{{U"a", U"A"}, default_bg, 1},
{{U"s", U"S"}, default_bg, 1},
{{U"d", U"D"}, default_bg, 1},
{{U"f", U"F"}, default_bg, 1},
{{U"g", U"G"}, default_bg, 1},
{{U"h", U"H"}, default_bg, 1},
{{U"j", U"J"}, default_bg, 1},
{{U"k", U"K"}, default_bg, 1},
{{U"U", U"U"}, default_bg, 1},
{{U"'", U"\""}, default_bg, 1},
{{U"z", U"Z"}, default_bg, 1},
{{U"x", U"X"}, default_bg, 1},
{{U"c", U"C"}, default_bg, 1},
{{U"v", U"V"}, default_bg, 1},
{{U"b", U"B"}, default_bg, 1},
{{U"n", U"N"}, default_bg, 1},
{{U"m", U"M"}, default_bg, 1},
{{U"-", U"_"}, default_bg, 1},
{{U"+", U"="}, default_bg, 1},
{{U",", U"?"}, default_bg, 1},
// Special
{{L"Shift"}, special2_bg, 2, button_flags::_default, shift_callback },
{{L"Space"}, special_bg, 4, button_flags::_space, space_callback },
{{L"Backspace"}, special_bg, 2, button_flags::_default, delete_callback },
{{L"Enter"}, special2_bg, 2, button_flags::_return, enter_callback },
{{U"Shift"}, special2_bg, 2, button_flags::_default, shift_callback },
{{U"Space"}, special_bg, 4, button_flags::_space, space_callback },
{{U"Backspace"}, special_bg, 2, button_flags::_default, delete_callback },
{{U"Enter"}, special2_bg, 2, button_flags::_return, enter_callback },
};
initialize_layout(layout, utf16_to_wstring(message), utf16_to_wstring(init_text));
initialize_layout(layout, utf16_to_u32string(message), utf16_to_u32string(init_text));
}
}
}

View File

@ -154,6 +154,6 @@ using vector3f = vector3_base<float>;
std::string utf8_to_ascii8(const std::string& utf8_string);
std::string utf16_to_ascii8(const std::u16string& utf16_string);
std::u16string ascii8_to_utf16(const std::string& ascii_string);
std::wstring utf8_to_wstring(const std::string& utf8_string);
std::u16string wstring_to_utf16(const std::wstring& w_string);
std::wstring utf16_to_wstring(const std::u16string& utf16_string);
std::u32string utf8_to_u32string(const std::string& utf8_string);
std::u16string u32string_to_utf16(const std::u32string& utf32_string);
std::u32string utf16_to_u32string(const std::u16string& utf16_string);

View File

@ -168,57 +168,43 @@ std::u16string ascii8_to_utf16(const std::string& ascii_string)
return out;
}
std::wstring utf8_to_wstring(const std::string& utf8_string)
std::u32string utf8_to_u32string(const std::string& utf8_string)
{
std::wstring result;
std::u32string result;
result.reserve(utf8_string.size());
process_multibyte(utf8_string, [&result](u32 code)
{
result.push_back(static_cast<wchar_t>(code));
result.push_back(static_cast<char32_t>(code));
});
return result;
}
std::u16string wstring_to_utf16(const std::wstring& w_string)
std::u16string u32string_to_utf16(const std::u32string& utf32_string)
{
if constexpr (sizeof(wchar_t) == sizeof(char16_t))
{
return reinterpret_cast<const char16_t*>(w_string.data());
}
else
{
std::u16string result;
result.reserve(w_string.size());
std::u16string result;
result.reserve(utf32_string.size());
for (const auto& code : w_string)
{
result.push_back(code);
}
return result;
for (const auto& code : utf32_string)
{
result.push_back(static_cast<char16_t>(code));
}
return result;
}
std::wstring utf16_to_wstring(const std::u16string& utf16_string)
std::u32string utf16_to_u32string(const std::u16string& utf16_string)
{
if constexpr (sizeof(wchar_t) == sizeof(char16_t))
{
return reinterpret_cast<const wchar_t*>(utf16_string.data());
}
else
{
std::wstring result;
result.reserve(utf16_string.size());
std::u32string result;
result.reserve(utf16_string.size());
for (const auto& code : utf16_string)
{
result.push_back(code);
}
return result;
for (const auto& code : utf16_string)
{
result.push_back(code);
}
return result;
}
namespace rsx

View File

@ -330,7 +330,7 @@ namespace rsx
struct osk_dialog : public user_interface, public OskDialogBase
{
using callback_t = std::function<void(const std::wstring&)>;
using callback_t = std::function<void(const std::u32string&)>;
enum border_flags
{
@ -360,13 +360,13 @@ namespace rsx
bool selected = false;
bool enabled = false;
std::vector<std::wstring> outputs;
std::vector<std::u32string> outputs;
callback_t callback;
};
struct grid_entry_ctor
{
std::vector<std::wstring> outputs;
std::vector<std::u32string> outputs;
color4f color;
u32 num_cell_hz;
button_flags type_flags;
@ -412,17 +412,17 @@ namespace rsx
void Create(const std::string& title, const std::u16string& message, char16_t* init_text, u32 charlimit, u32 options) override = 0;
void Close(bool ok) override;
void initialize_layout(const std::vector<grid_entry_ctor>& layout, const std::wstring& title, const std::wstring& initial_text);
void initialize_layout(const std::vector<grid_entry_ctor>& layout, const std::u32string& title, const std::u32string& initial_text);
void update() override;
void on_button_pressed(pad_button button_press) override;
void on_text_changed();
void on_default_callback(const std::wstring&);
void on_shift(const std::wstring&);
void on_space(const std::wstring&);
void on_backspace(const std::wstring&);
void on_enter(const std::wstring&);
void on_default_callback(const std::u32string&);
void on_shift(const std::u32string&);
void on_space(const std::u32string&);
void on_backspace(const std::u32string&);
void on_enter(const std::u32string&);
compiled_resource get_compiled() override;
};