From 83ffe11709c1a8d6e9d5f9cd3c1519ac2e9be79e Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Thu, 16 May 2013 23:38:28 +0200 Subject: [PATCH] Unicode characters outside the BMP (> 0xFFFF) are now correctly handled on Windows (#366) --- src/SFML/Window/Win32/WindowImplWin32.cpp | 37 +++++++++++++++++++---- src/SFML/Window/Win32/WindowImplWin32.hpp | 1 + 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp index ea277df09..0dba13ad2 100644 --- a/src/SFML/Window/Win32/WindowImplWin32.cpp +++ b/src/SFML/Window/Win32/WindowImplWin32.cpp @@ -73,7 +73,8 @@ m_icon (NULL), m_keyRepeatEnabled(true), m_isCursorIn (false), m_lastSize (0, 0), -m_resizing (false) +m_resizing (false), +m_surrogate (0) { if (m_handle) { @@ -93,7 +94,8 @@ m_icon (NULL), m_keyRepeatEnabled(true), m_isCursorIn (false), m_lastSize (mode.width, mode.height), -m_resizing (false) +m_resizing (false), +m_surrogate (0) { // Register the window class at first call if (windowCount == 0) @@ -510,10 +512,33 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) { if (m_keyRepeatEnabled || ((lParam & (1 << 30)) == 0)) { - Event event; - event.type = Event::TextEntered; - event.text.unicode = static_cast(wParam); - pushEvent(event); + // Get the code of the typed character + Uint32 character = static_cast(wParam); + + // Check if it is the first part of a surrogate pair, or a regular character + if ((character >= 0xD800) && (character <= 0xDBFF)) + { + // First part of a surrogate pair: store it and wait for the second one + m_surrogate = static_cast(character); + } + else + { + + // Check if it is the second part of a surrogate pair, or a regular character + if ((character >= 0xDC00) && (character <= 0xDFFF)) + { + // Convert the UTF-16 surrogate pair to a single UTF-32 value + Uint16 utf16[] = {m_surrogate, static_cast(character)}; + sf::Utf16::toUtf32(utf16, utf16 + 2, &character); + m_surrogate = 0; + } + + // Send a TextEntered event + Event event; + event.type = Event::TextEntered; + event.text.unicode = character; + pushEvent(event); + } } break; } diff --git a/src/SFML/Window/Win32/WindowImplWin32.hpp b/src/SFML/Window/Win32/WindowImplWin32.hpp index c6196a0e8..746e72aa9 100644 --- a/src/SFML/Window/Win32/WindowImplWin32.hpp +++ b/src/SFML/Window/Win32/WindowImplWin32.hpp @@ -238,6 +238,7 @@ private : bool m_isCursorIn; ///< Is the mouse cursor in the window's area ? Vector2u m_lastSize; ///< The last handled size of the window bool m_resizing; ///< Is the window being resized ? + Uint16 m_surrogate; ///< First half of the surrogate pair, in case we're receiving a Unicode character in two events }; } // namespace priv