Make chat message mixed with RTL name and LTR text look better

This commit is contained in:
Benau 2020-09-10 16:32:04 +08:00
parent 995fdf8c81
commit 736209abbe

View File

@ -69,6 +69,13 @@
#include <algorithm>
#include <cstdlib>
#ifndef SERVER_ONLY
extern "C"
{
#include <SheenBidi.h>
}
#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)