Browse Source

Remove built-in conversion between UTF-8 and UTF-16 strings, replace with Windows conversion functions in Windows shell.

Michael Ragazzon 4 years ago
parent
commit
148c4724e5

+ 0 - 1
Include/RmlUi/Config/Config.h

@@ -123,7 +123,6 @@ using Function = std::function<T>;
 // Strings.
 // Strings.
 using String = std::string;
 using String = std::string;
 using StringList = Vector< String >;
 using StringList = Vector< String >;
-using U16String = std::u16string;
 
 
 // Smart pointer types.
 // Smart pointer types.
 template<typename T>
 template<typename T>

+ 0 - 8
Include/RmlUi/Core/StringUtilities.h

@@ -131,14 +131,6 @@ namespace StringUtilities
 			--p;
 			--p;
 		return p;
 		return p;
 	}
 	}
-
-	/// Converts a string in UTF-8 encoding to a u16string in UTF-16 encoding.
-	/// Reports a warning if some or all characters could not be converted.
-	RMLUICORE_API U16String ToUTF16(const String& str);
-
-	/// Converts a u16string in UTF-16 encoding into a string in UTF-8 encoding.
-	/// Reports a warning if some or all characters could not be converted.
-	RMLUICORE_API String ToUTF8(const U16String& u16str);
 }
 }
 
 
 
 

+ 4 - 0
Samples/shell/include/win32/InputWin32.h

@@ -31,6 +31,7 @@
 
 
 #include <Input.h>
 #include <Input.h>
 #include <win32/IncludeWindows.h>
 #include <win32/IncludeWindows.h>
+#include <string>
 
 
 /**
 /**
 	Processes Windows input events and passes them through to RmlUi. Feel free to take this class and integrate it
 	Processes Windows input events and passes them through to RmlUi. Feel free to take this class and integrate it
@@ -48,4 +49,7 @@ public:
 	static void ProcessWindowsEvent(HWND window, UINT message, WPARAM w_param, LPARAM l_param);
 	static void ProcessWindowsEvent(HWND window, UINT message, WPARAM w_param, LPARAM l_param);
 };
 };
 
 
+Rml::String ConvertToUTF8(const std::wstring& wstr);
+std::wstring ConvertToUTF16(const Rml::String& str);
+
 #endif
 #endif

+ 19 - 3
Samples/shell/src/win32/InputWin32.cpp

@@ -149,9 +149,9 @@ void InputWin32::ProcessWindowsEvent(HWND window, UINT message, WPARAM w_param,
 
 
 		case WM_CHAR:
 		case WM_CHAR:
 		{
 		{
-			static char16_t first_u16_code_unit = 0;
+			static wchar_t first_u16_code_unit = 0;
 
 
-			char16_t c = (char16_t)w_param;
+			const wchar_t c = (wchar_t)w_param;
 			Rml::Character character = (Rml::Character)c;
 			Rml::Character character = (Rml::Character)c;
 
 
 			// Windows sends two-wide characters as two messages.
 			// Windows sends two-wide characters as two messages.
@@ -165,7 +165,7 @@ void InputWin32::ProcessWindowsEvent(HWND window, UINT message, WPARAM w_param,
 				if (c >= 0xDC00 && c < 0xE000 && first_u16_code_unit != 0)
 				if (c >= 0xDC00 && c < 0xE000 && first_u16_code_unit != 0)
 				{
 				{
 					// Second 16-bit code unit of a two-wide character.
 					// Second 16-bit code unit of a two-wide character.
-					Rml::String utf8 = Rml::StringUtilities::ToUTF8({ first_u16_code_unit, c });
+					Rml::String utf8 = ConvertToUTF8(std::wstring{ first_u16_code_unit, c });
 					character = Rml::StringUtilities::ToCharacter(utf8.data());
 					character = Rml::StringUtilities::ToCharacter(utf8.data());
 				}
 				}
 				else if (c == '\r')
 				else if (c == '\r')
@@ -435,3 +435,19 @@ static void InitialiseKeymap()
 	key_identifier_map[VK_PA1] = Rml::Input::KI_PA1;
 	key_identifier_map[VK_PA1] = Rml::Input::KI_PA1;
 	key_identifier_map[VK_OEM_CLEAR] = Rml::Input::KI_OEM_CLEAR;
 	key_identifier_map[VK_OEM_CLEAR] = Rml::Input::KI_OEM_CLEAR;
 }
 }
+
+Rml::String ConvertToUTF8(const std::wstring& wstr)
+{
+	const int count = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.length(), NULL, 0, NULL, NULL);
+	Rml::String str(count, 0);
+	WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL);
+	return str;
+}
+
+std::wstring ConvertToUTF16(const Rml::String& str)
+{
+	const int count = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.length(), NULL, 0);
+	std::wstring wstr(count, 0);
+	MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.length(), &wstr[0], count);
+	return wstr;
+}

+ 13 - 15
Samples/shell/src/win32/ShellWin32.cpp

@@ -42,7 +42,7 @@ static ShellRenderInterfaceExtensions* shell_renderer = nullptr;
 
 
 static bool activated = true;
 static bool activated = true;
 static bool running = false;
 static bool running = false;
-static Rml::U16String instance_name;
+static std::wstring instance_name;
 static HWND window_handle = nullptr;
 static HWND window_handle = nullptr;
 static HINSTANCE instance_handle = nullptr;
 static HINSTANCE instance_handle = nullptr;
 
 
@@ -81,7 +81,6 @@ static ProcSetProcessDpiAwarenessContext procSetProcessDpiAwarenessContext = NUL
 static ProcGetDpiForWindow procGetDpiForWindow = NULL;
 static ProcGetDpiForWindow procGetDpiForWindow = NULL;
 static ProcAdjustWindowRectExForDpi procAdjustWindowRectExForDpi = NULL;
 static ProcAdjustWindowRectExForDpi procAdjustWindowRectExForDpi = NULL;
 
 
-
 static void UpdateDpi()
 static void UpdateDpi()
 {
 {
 	if (has_dpi_support)
 	if (has_dpi_support)
@@ -207,7 +206,7 @@ bool Shell::OpenWindow(const char* in_name, ShellRenderInterfaceExtensions *_she
 
 
 	WNDCLASSW window_class;
 	WNDCLASSW window_class;
 
 
-	Rml::U16String name = Rml::StringUtilities::ToUTF16(Rml::String(in_name));
+	const std::wstring name = ConvertToUTF16(Rml::String(in_name));
 
 
 	// Fill out the window class struct.
 	// Fill out the window class struct.
 	window_class.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
 	window_class.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
@@ -219,7 +218,7 @@ bool Shell::OpenWindow(const char* in_name, ShellRenderInterfaceExtensions *_she
 	window_class.hCursor = cursor_default;
 	window_class.hCursor = cursor_default;
 	window_class.hbrBackground = nullptr;
 	window_class.hbrBackground = nullptr;
 	window_class.lpszMenuName = nullptr;
 	window_class.lpszMenuName = nullptr;
-	window_class.lpszClassName = (LPCWSTR)name.data();
+	window_class.lpszClassName = name.data();
 
 
 	if (!RegisterClassW(&window_class))
 	if (!RegisterClassW(&window_class))
 	{
 	{
@@ -230,11 +229,11 @@ bool Shell::OpenWindow(const char* in_name, ShellRenderInterfaceExtensions *_she
 	}
 	}
 
 
 	window_handle = CreateWindowExW(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
 	window_handle = CreateWindowExW(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
-								   (LPCWSTR)name.data(),	// Window class name.
-								   (LPCWSTR)name.data(),
+								   name.data(), // Window class name.
+								   name.data(),
 								   WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW,
 								   WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW,
-								   0, 0,	// Window position.
-								   0, 0,// Window size.
+								   0, 0, // Window position.
+								   0, 0, // Window size.
 								   nullptr,
 								   nullptr,
 								   nullptr,
 								   nullptr,
 								   instance_handle,
 								   instance_handle,
@@ -354,7 +353,7 @@ void Shell::DisplayError(const char* fmt, ...)
 	buffer[len + 1] = '\0';
 	buffer[len + 1] = '\0';
 	va_end(argument_list);
 	va_end(argument_list);
 
 
-	MessageBoxW(window_handle, (LPCWSTR)Rml::StringUtilities::ToUTF16(buffer).c_str(), L"Shell Error", MB_OK);
+	MessageBoxW(window_handle, ConvertToUTF16(buffer).c_str(), L"Shell Error", MB_OK);
 }
 }
 
 
 void Shell::Log(const char* fmt, ...)
 void Shell::Log(const char* fmt, ...)
@@ -374,7 +373,7 @@ void Shell::Log(const char* fmt, ...)
 	buffer[len + 1] = '\0';
 	buffer[len + 1] = '\0';
 	va_end(argument_list);
 	va_end(argument_list);
 
 
-	OutputDebugStringW((LPCWSTR)Rml::StringUtilities::ToUTF16(buffer).c_str());
+	OutputDebugStringW(ConvertToUTF16(buffer).c_str());
 }
 }
 
 
 double Shell::GetElapsedTime() 
 double Shell::GetElapsedTime() 
@@ -422,9 +421,8 @@ void Shell::SetClipboardText(const Rml::String& text_utf8)
 
 
 		EmptyClipboard();
 		EmptyClipboard();
 
 
-		const Rml::U16String text = Rml::StringUtilities::ToUTF16(text_utf8);
-
-		size_t size = sizeof(char16_t) * (text.size() + 1);
+		const std::wstring text = ConvertToUTF16(text_utf8);
+		const size_t size = sizeof(wchar_t) * (text.size() + 1);
 
 
 		HGLOBAL clipboard_data = GlobalAlloc(GMEM_FIXED, size);
 		HGLOBAL clipboard_data = GlobalAlloc(GMEM_FIXED, size);
 		memcpy(clipboard_data, text.data(), size);
 		memcpy(clipboard_data, text.data(), size);
@@ -453,9 +451,9 @@ void Shell::GetClipboardText(Rml::String& text)
 			return;
 			return;
 		}
 		}
 
 
-		const char16_t* clipboard_text = (const char16_t*)GlobalLock(clipboard_data);
+		const wchar_t* clipboard_text = (const wchar_t*)GlobalLock(clipboard_data);
 		if (clipboard_text)
 		if (clipboard_text)
-			text = Rml::StringUtilities::ToUTF8(clipboard_text);
+			text = ConvertToUTF8(clipboard_text);
 		GlobalUnlock(clipboard_data);
 		GlobalUnlock(clipboard_data);
 
 
 		CloseClipboard();
 		CloseClipboard();

+ 0 - 93
Source/Core/StringUtilities.cpp

@@ -445,7 +445,6 @@ String StringUtilities::ToUTF8(const Character* characters, int num_characters)
 	return result;
 	return result;
 }
 }
 
 
-
 size_t StringUtilities::LengthUTF8(StringView string_view)
 size_t StringUtilities::LengthUTF8(StringView string_view)
 {
 {
 	const char* const p_end = string_view.end();
 	const char* const p_end = string_view.end();
@@ -465,98 +464,6 @@ size_t StringUtilities::LengthUTF8(StringView string_view)
 	return string_view.size() - num_continuation_bytes;
 	return string_view.size() - num_continuation_bytes;
 }
 }
 
 
-U16String StringUtilities::ToUTF16(const String& input)
-{
-	U16String result;
-
-	if (input.empty())
-		return result;
-
-	Vector<Character> characters;
-	characters.reserve(input.size());
-
-	for (auto it = StringIteratorU8(input); it; ++it)
-		characters.push_back(*it);
-
-	result.reserve(input.size());
-
-	bool valid_characters = true;
-
-	for (Character character : characters)
-	{
-		char32_t c = (char32_t)character;
-
-		if (c <= 0xD7FF || (c >= 0xE000 && c <= 0xFFFF))
-		{
-			// Single 16-bit code unit.
-			result += (char16_t)c;
-		}
-		else if (c >= 0x10000 && c <= 0x10FFFF)
-		{
-			// Encode as two 16-bit code units.
-			char32_t c_shift = c - 0x10000;
-			char16_t w1 = (0xD800 | ((c_shift >> 10) & 0x3FF));
-			char16_t w2 = (0xDC00 | (c_shift & 0x3FF));
-			result += {w1, w2};
-		}
-		else
-		{
-			valid_characters = false;
-		}
-	}
-
-	if (!valid_characters)
-		Log::Message(Log::LT_WARNING, "Invalid characters encountered while converting UTF-8 string to UTF-16.");
-
-	return result;
-}
-
-String StringUtilities::ToUTF8(const U16String& input)
-{
-	Vector<Character> characters;
-	characters.reserve(input.size());
-
-	bool valid_input = true;
-	char16_t w1 = 0;
-
-	for (char16_t w : input)
-	{
-		if (w <= 0xD7FF || w >= 0xE000)
-		{
-			// Single 16-bit code unit.
-			characters.push_back((Character)(w));
-		}
-		else
-		{
-			// Two 16-bit code units.
-			if (!w1 && w < 0xDC00)
-			{
-				w1 = w;
-			}
-			else if (w1 && w >= 0xDC00)
-			{
-				characters.push_back((Character)(((((char32_t)w1 & 0x3FF) << 10) | ((char32_t)(w) & 0x3FF)) + 0x10000u));
-				w1 = 0;
-			}
-			else
-			{
-				valid_input = false;
-			}
-		}
-	}
-
-	String result;
-
-	if (characters.size() > 0)
-		result = StringUtilities::ToUTF8(characters.data(), (int)characters.size());
-
-	if (!valid_input)
-		Log::Message(Log::LT_WARNING, "Invalid characters encountered while converting UTF-16 string to UTF-8.");
-
-	return result;
-}
-
-
 StringView::StringView()
 StringView::StringView()
 {
 {
 	const char* empty_string = "";
 	const char* empty_string = "";