From 736209abbe59f00914311ff151712b1221e24006 Mon Sep 17 00:00:00 2001 From: Benau Date: Thu, 10 Sep 2020 16:32:04 +0800 Subject: [PATCH] Make chat message mixed with RTL name and LTR text look better --- src/network/protocols/client_lobby.cpp | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index e686dd4f4..2df77bcd2 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -69,6 +69,13 @@ #include #include +#ifndef SERVER_ONLY +extern "C" +{ + #include +} +#endif + // ============================================================================ /** The protocol that manages starting a race with the server. It uses a * finite state machine: @@ -1419,6 +1426,42 @@ void ClientLobby::sendChat(irr::core::stringw text, KartTeam team) name = PlayerManager::getCurrentOnlineProfile()->getUserName(); else name = player->getName(); + // Make message look better by aligning to left or right side depends + // on name and text + // (LTR: RTL text always right; RTL : LTR text always left) +#ifndef SERVER_ONLY + if (!name.empty() && !text.empty()) + { + SBCodepointSequence codepoint_sequence; + codepoint_sequence.stringEncoding = sizeof(wchar_t) == 2 ? + SBStringEncodingUTF16 : SBStringEncodingUTF32; + codepoint_sequence.stringBuffer = (void*)name.c_str(); + codepoint_sequence.stringLength = name.size(); + SBAlgorithmRef bidi_algorithm = + SBAlgorithmCreate(&codepoint_sequence); + SBParagraphRef first_paragraph = + SBAlgorithmCreateParagraph(bidi_algorithm, 0, (int32_t)-1, + SBLevelDefaultLTR); + SBLevel name_level = SBParagraphGetBaseLevel(first_paragraph); + SBParagraphRelease(first_paragraph); + SBAlgorithmRelease(bidi_algorithm); + + codepoint_sequence.stringBuffer = (void*)text.c_str(); + codepoint_sequence.stringLength = text.size(); + bidi_algorithm = + SBAlgorithmCreate(&codepoint_sequence); + first_paragraph = + SBAlgorithmCreateParagraph(bidi_algorithm, 0, (int32_t)-1, + SBLevelDefaultLTR); + SBLevel text_level = SBParagraphGetBaseLevel(first_paragraph); + SBParagraphRelease(first_paragraph); + SBAlgorithmRelease(bidi_algorithm); + if (name_level % 2 == 0 && text_level % 2 != 0) + name = core::stringw(L"\u200F") + name; + else if (name_level % 2 != 0 && text_level % 2 == 0) + name = core::stringw(L"\u200E") + name; + } +#endif chat->encodeString16(name + L": " + text, 1000/*max_len*/); if (team != KART_TEAM_NONE)