diff --git a/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc b/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc index 48fba3894a..b8377ede8e 100644 --- a/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc +++ b/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc @@ -6,7 +6,6 @@ #include #include -#include #include // TODO(awdavies): Need to fix this regarding issue #47. @@ -32,11 +31,24 @@ static constexpr char kTextInputTypeName[] = "name"; #if defined(_MSC_VER) // TODO(naifu): This temporary code is to solve link error.(VS2015/2017) // https://social.msdn.microsoft.com/Forums/vstudio/en-US/8f40dcd8-c67f-4eba-9134-a19b9178e481/vs-2015-rc-linker-stdcodecvt-error -std::locale::id std::codecvt::id; +std::locale::id std::codecvt::id; #endif // defined(_MSC_VER) namespace flutter { +namespace { + +// Returns true if |code_point| is a leading surrogate of a surrogate pair. +bool IsLeadingSurrogate(char32_t code_point) { + return (code_point & 0xFFFFFC00) == 0xD800; +} +// Returns true if |code_point| is a trailing surrogate of a surrogate pair. +bool IsTrailingSurrogate(char32_t code_point) { + return (code_point & 0xFFFFFC00) == 0xDC00; +} + +} // namespace + TextInputModel::TextInputModel(int client_id, const rapidjson::Value& config) : client_id_(client_id), selection_base_(text_.begin()), @@ -72,15 +84,11 @@ bool TextInputModel::SetEditingState(size_t selection_base, if (selection_extent > text.size()) { return false; } - std::wstring_convert, char32_t> utf32conv; - text_ = utf32conv.from_bytes(text); - // selection_base and selection_extent are relative to UTF-16, so don't - // necessarily line up. The full fix would be to use UTF-16 here so - // the models align, but for now just make sure it doesn't crash; - // longer term the goal is to eliminate having two sources of truth. - size_t length = text_.length(); - selection_base_ = text_.begin() + std::min(selection_base, length); - selection_extent_ = text_.begin() + std::min(selection_extent, length); + std::wstring_convert, char16_t> + utf16_converter; + text_ = utf16_converter.from_bytes(text); + selection_base_ = text_.begin() + selection_base; + selection_extent_ = text_.begin() + selection_extent; return true; } @@ -90,12 +98,26 @@ void TextInputModel::DeleteSelected() { selection_extent_ = selection_base_; } -void TextInputModel::AddCharacter(char32_t c) { +void TextInputModel::AddCodePoint(char32_t c) { + if (c <= 0xFFFF) { + AddText(std::u16string({static_cast(c)})); + } else { + char32_t to_decompose = c - 0x10000; + AddText(std::u16string({ + // High surrogate. + static_cast((to_decompose >> 10) + 0xd800), + // Low surrogate. + static_cast((to_decompose % 0x400) + 0xdc00), + })); + } +} + +void TextInputModel::AddText(const std::u16string& text) { if (selection_base_ != selection_extent_) { DeleteSelected(); } - selection_extent_ = text_.insert(selection_extent_, c); - selection_extent_++; + selection_extent_ = text_.insert(selection_extent_, text.begin(), text.end()); + selection_extent_ += text.length(); selection_base_ = selection_extent_; } @@ -105,7 +127,8 @@ bool TextInputModel::Backspace() { return true; } if (selection_base_ != text_.begin()) { - selection_base_ = text_.erase(selection_base_ - 1, selection_base_); + int count = IsTrailingSurrogate(*(selection_base_ - 1)) ? 2 : 1; + selection_base_ = text_.erase(selection_base_ - count, selection_base_); selection_extent_ = selection_base_; return true; } @@ -118,7 +141,8 @@ bool TextInputModel::Delete() { return true; } if (selection_base_ != text_.end()) { - selection_base_ = text_.erase(selection_base_, selection_base_ + 1); + int count = IsLeadingSurrogate(*selection_base_) ? 2 : 1; + selection_base_ = text_.erase(selection_base_, selection_base_ + count); selection_extent_ = selection_base_; return true; } @@ -143,8 +167,9 @@ bool TextInputModel::MoveCursorForward() { } // If not at the end, move the extent forward. if (selection_extent_ != text_.end()) { - selection_extent_++; - selection_base_++; + int count = IsLeadingSurrogate(*selection_base_) ? 2 : 1; + selection_base_ += count; + selection_extent_ = selection_base_; return true; } return false; @@ -159,8 +184,9 @@ bool TextInputModel::MoveCursorBack() { } // If not at the start, move the beginning backward. if (selection_base_ != text_.begin()) { - selection_base_--; - selection_extent_--; + int count = IsTrailingSurrogate(*(selection_base_ - 1)) ? 2 : 1; + selection_base_ -= count; + selection_extent_ = selection_base_; return true; } return false; @@ -186,9 +212,11 @@ std::unique_ptr TextInputModel::GetState() const { static_cast(selection_extent_ - text_.begin()), allocator); editing_state.AddMember(kSelectionIsDirectionalKey, false, allocator); - std::wstring_convert, char32_t> utf8conv; + std::wstring_convert, char16_t> + utf8_converter; editing_state.AddMember( - kTextKey, rapidjson::Value(utf8conv.to_bytes(text_), allocator).Move(), + kTextKey, + rapidjson::Value(utf8_converter.to_bytes(text_), allocator).Move(), allocator); args->PushBack(editing_state, allocator); return args; diff --git a/engine/src/flutter/shell/platform/common/cpp/text_input_model.h b/engine/src/flutter/shell/platform/common/cpp/text_input_model.h index 0688512aed..5aeda0af4b 100644 --- a/engine/src/flutter/shell/platform/common/cpp/text_input_model.h +++ b/engine/src/flutter/shell/platform/common/cpp/text_input_model.h @@ -27,12 +27,18 @@ class TextInputModel { size_t selection_extent, const std::string& text); - // Adds a character. + // Adds a Unicode code point. // // Either appends after the cursor (when selection base and extent are the - // same), or deletes the selected characters, replacing the text with the - // character specified. - void AddCharacter(char32_t c); + // same), or deletes the selected text, replacing it with the given + // code point. + void AddCodePoint(char32_t c); + + // Adds a UTF-16 text. + // + // Either appends after the cursor (when selection base and extent are the + // same), or deletes the selected text, replacing it with the given text. + void AddText(const std::u16string& text); // Deletes either the selection, or one character ahead of the cursor. // @@ -89,12 +95,12 @@ class TextInputModel { private: void DeleteSelected(); - std::u32string text_; + std::u16string text_; int client_id_; std::string input_type_; std::string input_action_; - std::u32string::iterator selection_base_; - std::u32string::iterator selection_extent_; + std::u16string::iterator selection_base_; + std::u16string::iterator selection_extent_; }; } // namespace flutter diff --git a/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc b/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc index d1f163b2f1..344e33d3da 100644 --- a/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/glfw/text_input_plugin.cc @@ -38,7 +38,7 @@ void TextInputPlugin::CharHook(GLFWwindow* window, unsigned int code_point) { if (active_model_ == nullptr) { return; } - active_model_->AddCharacter(code_point); + active_model_->AddCodePoint(code_point); SendStateUpdate(*active_model_); } @@ -181,7 +181,7 @@ void TextInputPlugin::SendStateUpdate(const TextInputModel& model) { void TextInputPlugin::EnterPressed(TextInputModel* model) { if (model->input_type() == kMultilineInputType) { - model->AddCharacter('\n'); + model->AddCodePoint('\n'); SendStateUpdate(*model); } auto args = std::make_unique(rapidjson::kArrayType); diff --git a/engine/src/flutter/shell/platform/windows/key_event_handler.cc b/engine/src/flutter/shell/platform/windows/key_event_handler.cc index 8079f197cf..3747a9f5a0 100644 --- a/engine/src/flutter/shell/platform/windows/key_event_handler.cc +++ b/engine/src/flutter/shell/platform/windows/key_event_handler.cc @@ -92,8 +92,8 @@ KeyEventHandler::KeyEventHandler(flutter::BinaryMessenger* messenger) KeyEventHandler::~KeyEventHandler() = default; -void KeyEventHandler::CharHook(Win32FlutterWindow* window, - char32_t code_point) {} +void KeyEventHandler::TextHook(Win32FlutterWindow* window, + const std::u16string& code_point) {} void KeyEventHandler::KeyboardHook(Win32FlutterWindow* window, int key, diff --git a/engine/src/flutter/shell/platform/windows/key_event_handler.h b/engine/src/flutter/shell/platform/windows/key_event_handler.h index 1b28e90b71..6e9a77f1d3 100644 --- a/engine/src/flutter/shell/platform/windows/key_event_handler.h +++ b/engine/src/flutter/shell/platform/windows/key_event_handler.h @@ -6,6 +6,7 @@ #define FLUTTER_SHELL_PLATFORM_WINDOWS_KEY_EVENT_HANDLER_H_ #include +#include #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/basic_message_channel.h" #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h" @@ -34,7 +35,8 @@ class KeyEventHandler : public KeyboardHookHandler { char32_t character) override; // |KeyboardHookHandler| - void CharHook(Win32FlutterWindow* window, char32_t code_point) override; + void TextHook(Win32FlutterWindow* window, + const std::u16string& text) override; private: // The Flutter system channel for key event messages. diff --git a/engine/src/flutter/shell/platform/windows/keyboard_hook_handler.h b/engine/src/flutter/shell/platform/windows/keyboard_hook_handler.h index 4583b2bb76..aa574a545d 100644 --- a/engine/src/flutter/shell/platform/windows/keyboard_hook_handler.h +++ b/engine/src/flutter/shell/platform/windows/keyboard_hook_handler.h @@ -7,6 +7,8 @@ #include "flutter/shell/platform/windows/public/flutter_windows.h" +#include + namespace flutter { class Win32FlutterWindow; @@ -23,8 +25,9 @@ class KeyboardHookHandler { int action, char32_t character) = 0; - // A function for hooking into unicode code point input. - virtual void CharHook(Win32FlutterWindow* window, char32_t code_point) = 0; + // A function for hooking into Unicode text input. + virtual void TextHook(Win32FlutterWindow* window, + const std::u16string& text) = 0; }; } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/testing/win32_window_test.cc b/engine/src/flutter/shell/platform/windows/testing/win32_window_test.cc index 53ad12b5b7..da4bc43b9d 100644 --- a/engine/src/flutter/shell/platform/windows/testing/win32_window_test.cc +++ b/engine/src/flutter/shell/platform/windows/testing/win32_window_test.cc @@ -19,7 +19,7 @@ void Win32WindowTest::OnPointerUp(double x, double y, UINT button) {} void Win32WindowTest::OnPointerLeave() {} -void Win32WindowTest::OnChar(char32_t code_point) {} +void Win32WindowTest::OnText(const std::u16string& text) {} void Win32WindowTest::OnKey(int key, int scancode, diff --git a/engine/src/flutter/shell/platform/windows/testing/win32_window_test.h b/engine/src/flutter/shell/platform/windows/testing/win32_window_test.h index 77d25464b9..5d8630c018 100644 --- a/engine/src/flutter/shell/platform/windows/testing/win32_window_test.h +++ b/engine/src/flutter/shell/platform/windows/testing/win32_window_test.h @@ -42,7 +42,7 @@ class Win32WindowTest : public Win32Window { void OnPointerLeave() override; // |Win32Window| - void OnChar(char32_t code_point) override; + void OnText(const std::u16string& text) override; // |Win32Window| void OnKey(int key, int scancode, int action, char32_t character) override; diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc index 10b3864d4f..b0f5d988ab 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.cc +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.cc @@ -36,12 +36,12 @@ static constexpr char kInternalConsistencyError[] = namespace flutter { -void TextInputPlugin::CharHook(Win32FlutterWindow* window, - char32_t code_point) { +void TextInputPlugin::TextHook(Win32FlutterWindow* window, + const std::u16string& text) { if (active_model_ == nullptr) { return; } - active_model_->AddCharacter(code_point); + active_model_->AddText(text); SendStateUpdate(*active_model_); } @@ -183,7 +183,7 @@ void TextInputPlugin::SendStateUpdate(const TextInputModel& model) { void TextInputPlugin::EnterPressed(TextInputModel* model) { if (model->input_type() == kMultilineInputType) { - model->AddCharacter('\n'); + model->AddText(std::u16string({u'\n'})); SendStateUpdate(*model); } auto args = std::make_unique(rapidjson::kArrayType); diff --git a/engine/src/flutter/shell/platform/windows/text_input_plugin.h b/engine/src/flutter/shell/platform/windows/text_input_plugin.h index dced9a6954..44179f8b8b 100644 --- a/engine/src/flutter/shell/platform/windows/text_input_plugin.h +++ b/engine/src/flutter/shell/platform/windows/text_input_plugin.h @@ -35,7 +35,8 @@ class TextInputPlugin : public KeyboardHookHandler { char32_t character) override; // |KeyboardHookHandler| - void CharHook(Win32FlutterWindow* window, char32_t code_point) override; + void TextHook(Win32FlutterWindow* window, + const std::u16string& text) override; private: // Sends the current state of the given model to the Flutter engine. diff --git a/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc b/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc index 73c46febf3..4976a70b06 100644 --- a/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc +++ b/engine/src/flutter/shell/platform/windows/win32_flutter_window.cc @@ -161,9 +161,9 @@ void Win32FlutterWindow::OnPointerLeave() { } } -void Win32FlutterWindow::OnChar(char32_t code_point) { +void Win32FlutterWindow::OnText(const std::u16string& text) { if (process_events_) { - SendChar(code_point); + SendText(text); } } @@ -264,9 +264,9 @@ void Win32FlutterWindow::SendPointerLeave() { SendPointerEventWithData(event); } -void Win32FlutterWindow::SendChar(char32_t code_point) { +void Win32FlutterWindow::SendText(const std::u16string& text) { for (const auto& handler : keyboard_hook_handlers_) { - handler->CharHook(this, code_point); + handler->TextHook(this, text); } } diff --git a/engine/src/flutter/shell/platform/windows/win32_flutter_window.h b/engine/src/flutter/shell/platform/windows/win32_flutter_window.h index bce1f2e6fd..aff194a0e5 100644 --- a/engine/src/flutter/shell/platform/windows/win32_flutter_window.h +++ b/engine/src/flutter/shell/platform/windows/win32_flutter_window.h @@ -57,7 +57,7 @@ class Win32FlutterWindow : public Win32Window { void OnPointerLeave() override; // |Win32Window| - void OnChar(char32_t code_point) override; + void OnText(const std::u16string& text) override; // |Win32Window| void OnKey(int key, int scancode, int action, char32_t character) override; @@ -112,8 +112,8 @@ class Win32FlutterWindow : public Win32Window { // event is called. void SendPointerLeave(); - // Reports a keyboard character to Flutter engine. - void SendChar(char32_t code_point); + // Reports text input to Flutter engine. + void SendText(const std::u16string& text); // Reports a raw keyboard message to Flutter engine. void SendKey(int key, int scancode, int action, char32_t character); diff --git a/engine/src/flutter/shell/platform/windows/win32_window.cc b/engine/src/flutter/shell/platform/windows/win32_window.cc index 11f4b4c021..e4cf979167 100644 --- a/engine/src/flutter/shell/platform/windows/win32_window.cc +++ b/engine/src/flutter/shell/platform/windows/win32_window.cc @@ -8,6 +8,13 @@ namespace flutter { +namespace { +char32_t CodePointFromSurrogatePair(wchar_t high, wchar_t low) { + return 0x10000 + ((static_cast(high) & 0x000003FF) << 10) + + (low & 0x3FF); +} +} // namespace + Win32Window::Win32Window() { // Get the DPI of the primary monitor as the initial DPI. If Per-Monitor V2 is // supported, |current_dpi_| should be updated in the @@ -195,19 +202,21 @@ Win32Window::MessageHandler(HWND hwnd, case WM_SYSDEADCHAR: case WM_CHAR: case WM_SYSCHAR: { - char32_t code_point = static_cast(wparam); - static char32_t s_pending_lead_surrogate = 0; - // If code_point is LeadSurrogate, save to combine to potentially form - // a complex Unicode character. - if ((code_point & 0xFFFFFC00) == 0xD800) { - s_pending_lead_surrogate = code_point; - } else if (s_pending_lead_surrogate != 0 && - (code_point & 0xFFFFFC00) == 0xDC00) { - // Merge TrailSurrogate and LeadSurrogate. - code_point = 0x10000 + - ((s_pending_lead_surrogate & 0x000003FF) << 10) + - (code_point & 0x3FF); - s_pending_lead_surrogate = 0; + static wchar_t s_pending_high_surrogate = 0; + + wchar_t character = static_cast(wparam); + std::u16string text({character}); + char32_t code_point = character; + if (IS_HIGH_SURROGATE(character)) { + // Save to send later with the trailing surrogate. + s_pending_high_surrogate = character; + } else if (IS_LOW_SURROGATE(character) && + s_pending_high_surrogate != 0) { + text.insert(text.begin(), s_pending_high_surrogate); + // Merge the surrogate pairs for the key event. + code_point = + CodePointFromSurrogatePair(s_pending_high_surrogate, character); + s_pending_high_surrogate = 0; } // Of the messages handled here, only WM_CHAR should be treated as @@ -217,9 +226,9 @@ Win32Window::MessageHandler(HWND hwnd, // - Lead surrogates, which like dead keys will be send once combined. // - ASCII control characters, which are sent as WM_CHAR events for all // control key shortcuts. - if (message == WM_CHAR && code_point >= U' ' && - s_pending_lead_surrogate == 0) { - window->OnChar(code_point); + if (message == WM_CHAR && s_pending_high_surrogate == 0 && + character >= u' ') { + window->OnText(text); } // All key presses that generate a character should be sent from diff --git a/engine/src/flutter/shell/platform/windows/win32_window.h b/engine/src/flutter/shell/platform/windows/win32_window.h index ba959ffcaa..d4481d89e2 100644 --- a/engine/src/flutter/shell/platform/windows/win32_window.h +++ b/engine/src/flutter/shell/platform/windows/win32_window.h @@ -102,8 +102,8 @@ class Win32Window { // Called when the mouse leaves the window. virtual void OnPointerLeave() = 0; - // Called when character input occurs. - virtual void OnChar(char32_t code_point) = 0; + // Called when text input occurs. + virtual void OnText(const std::u16string& text) = 0; // Called when raw keyboard input occurs. virtual void OnKey(int key, int scancode, int action, char32_t character) = 0;