Files
itsRevela-LCE_Revelations/Minecraft.Client/ChatScreen.cpp
Revela d7822ac81e Enable multi-language font rendering and Unicode text input
Goal:
Allow players to type and display text in any language supported by
Unicode, including Chinese, Japanese, Korean, Thai, Arabic, Korean, Hindi, and more. This
covers all text surfaces: chat editor, chat messages, signs (in-world
and editor), world name/seed, server address/port fields, and all
Iggy Flash UI text fields.

Multi-language support:
Two complementary rendering systems were added to handle Unicode text
across the entire client:

1. Iggy UI (Flash-based text fields): A new UIUnicodeBitmapFont class
   serves Java Minecraft's glyph page PNGs (glyph_00.png-glyph_FF.png)
   through Iggy's bitmap font provider API. Registered as the global
   fallback font with metrics matching the Mojangles bitmap font for
   correct baseline alignment. When the primary bitmap font lacks a
   glyph, it returns IGGY_GLYPH_INVALID and Iggy seamlessly falls back
   to the unicode bitmap font.

2. Legacy C++ Font renderer (chat editor, in-world signs): Revived the
   commented-out unicode glyph page system in Font.cpp. Characters not
   in the bitmap font texture are rendered from glyph page PNGs loaded
   on demand, with proper texture switching mid-string.

3. ChatScreen input: Removed the restrictive acceptableLetters filter
   so all printable Unicode characters are accepted in chat.

Languages now supported for text input and rendering:
- Japanese (Hiragana, Katakana, Kanji)
- Chinese (Simplified and Traditional)
- Korean (Hangul)
- Thai
- Arabic
- Hindi (Devanagari)
- Russian (Cyrillic) - already worked via bitmap font
- Greek - already worked via bitmap font
- Polish, Czech, Turkish (Extended Latin) - already worked via bitmap font
- Armenian, Georgian, and other scripts covered by glyph pages

Security fixes:
- Fixed memset under-initialization of Font::charWidths (zeroed 460
  bytes instead of 460*sizeof(int)=1840 bytes, leaving entries 115+
  uninitialized) - pre-existing bug
- Added bounds checks to all UIUnicodeBitmapFont callbacks to reject
  glyph IDs outside [0, 65535], preventing OOB array access
- Added bounds check in Font::width() section-sign fallback path to
  prevent OOB read on charWidths[] with high codepoints
- Blocked Unicode bidirectional override characters (U+202A-202E,
  U+2066-2069) in chat input to prevent message spoofing

Memory leak fix:
- Fixed SignTileEntity::load allocating wchar_t[256] with new[] on
  every sign load without freeing. Replaced with stack allocation.

Debug logging:
- Added [SIGN] prefixed logging for sign save/update operations
- Added [CHAT] prefixed logging for chat send/receive operations

Files changed:
- UIUnicodeBitmapFont.h/.cpp (new) - Iggy bitmap font for glyph pages
- UIBitmapFont.cpp - Return IGGY_GLYPH_INVALID for unknown chars
- UIFontData.h/.cpp - Added hasGlyph() method
- UIController.h/.cpp - Load and register unicode bitmap fallback font
- UITTFFont.h/.cpp - Added registerAsDefaultFonts parameter
- Font.h/.cpp - Revived unicode glyph page rendering system
- ChatScreen.cpp - Accept all Unicode input, block bidi overrides
- Gui.cpp - Chat display debug logging
- ClientConnection.cpp - Sign update debug logging
- SignTileEntity.cpp - Sign save logging, memory leak fix
2026-03-16 23:08:05 -05:00

192 lines
5.7 KiB
C++

#include "stdafx.h"
#include "ChatScreen.h"
#include "ClientConnection.h"
#include "Font.h"
#include "MultiplayerLocalPlayer.h"
#include "..\Minecraft.World\SharedConstants.h"
#include "..\Minecraft.World\StringHelpers.h"
#include "..\Minecraft.World\ChatPacket.h"
const wstring ChatScreen::allowedChars = SharedConstants::acceptableLetters;
vector<wstring> ChatScreen::s_chatHistory;
int ChatScreen::s_historyIndex = -1;
wstring ChatScreen::s_historyDraft;
bool ChatScreen::isAllowedChatChar(wchar_t c)
{
if (c < 0x20) return false;
// Block Unicode bidirectional override characters that can be used to
// spoof chat messages or impersonate players.
if (c >= 0x202A && c <= 0x202E) return false; // LRE, RLE, PDF, LRO, RLO
if (c >= 0x2066 && c <= 0x2069) return false; // LRI, RLI, FSI, PDI
return true;
}
ChatScreen::ChatScreen()
{
frame = 0;
cursorIndex = 0;
s_historyIndex = -1;
}
void ChatScreen::init()
{
Keyboard::enableRepeatEvents(true);
}
void ChatScreen::removed()
{
Keyboard::enableRepeatEvents(false);
}
void ChatScreen::tick()
{
frame++;
if (cursorIndex > static_cast<int>(message.length()))
cursorIndex = static_cast<int>(message.length());
}
void ChatScreen::handlePasteRequest()
{
wstring pasted = Screen::getClipboard();
for (size_t i = 0; i < pasted.length() && static_cast<int>(message.length()) < SharedConstants::maxChatLength; i++)
{
if (isAllowedChatChar(pasted[i]))
{
message.insert(cursorIndex, 1, pasted[i]);
cursorIndex++;
}
}
}
void ChatScreen::applyHistoryMessage()
{
message = s_historyIndex >= 0 ? s_chatHistory[s_historyIndex] : s_historyDraft;
cursorIndex = static_cast<int>(message.length());
}
void ChatScreen::handleHistoryUp()
{
if (s_chatHistory.empty()) return;
if (s_historyIndex == -1)
{
s_historyDraft = message;
s_historyIndex = static_cast<int>(s_chatHistory.size()) - 1;
}
else if (s_historyIndex > 0)
s_historyIndex--;
applyHistoryMessage();
}
void ChatScreen::handleHistoryDown()
{
if (s_chatHistory.empty()) return;
if (s_historyIndex < static_cast<int>(s_chatHistory.size()) - 1)
s_historyIndex++;
else
s_historyIndex = -1;
applyHistoryMessage();
}
void ChatScreen::keyPressed(wchar_t ch, int eventKey)
{
if (eventKey == Keyboard::KEY_ESCAPE)
{
minecraft->setScreen(nullptr);
return;
}
if (eventKey == Keyboard::KEY_RETURN)
{
wstring trim = trimString(message);
{ char buf[64]; sprintf_s(buf, "[CHAT] Sending (%d chars): ", (int)trim.length()); OutputDebugStringA(buf); }
OutputDebugStringW(trim.c_str());
OutputDebugStringA("\n");
if (trim.length() > 0)
{
if (!minecraft->handleClientSideCommand(trim))
{
MultiplayerLocalPlayer* mplp = dynamic_cast<MultiplayerLocalPlayer*>(minecraft->player.get());
if (mplp && mplp->connection)
mplp->connection->send(shared_ptr<ChatPacket>(new ChatPacket(trim)));
}
if (s_chatHistory.empty() || s_chatHistory.back() != trim)
{
s_chatHistory.push_back(trim);
if (s_chatHistory.size() > CHAT_HISTORY_MAX)
s_chatHistory.erase(s_chatHistory.begin());
}
}
minecraft->setScreen(nullptr);
return;
}
if (eventKey == Keyboard::KEY_UP) { handleHistoryUp(); return; }
if (eventKey == Keyboard::KEY_DOWN) { handleHistoryDown(); return; }
if (eventKey == Keyboard::KEY_LEFT)
{
if (cursorIndex > 0)
cursorIndex--;
return;
}
if (eventKey == Keyboard::KEY_RIGHT)
{
if (cursorIndex < static_cast<int>(message.length()))
cursorIndex++;
return;
}
if (eventKey == Keyboard::KEY_BACK && cursorIndex > 0)
{
message.erase(cursorIndex - 1, 1);
cursorIndex--;
return;
}
if (isAllowedChatChar(ch) && static_cast<int>(message.length()) < SharedConstants::maxChatLength)
{
message.insert(cursorIndex, 1, ch);
cursorIndex++;
{ char buf[64]; sprintf_s(buf, "[CHAT] Char U+%04X accepted (%d chars)\n", (unsigned)ch, (int)message.length()); OutputDebugStringA(buf); }
}
}
void ChatScreen::render(int xm, int ym, float a)
{
fill(2, height - 14, width - 2, height - 2, 0x80000000);
const wstring prefix = L"> ";
int x = 4;
drawString(font, prefix, x, height - 12, 0xe0e0e0);
x += font->width(prefix);
wstring beforeCursor = message.substr(0, cursorIndex);
wstring afterCursor = message.substr(cursorIndex);
drawStringLiteral(font, beforeCursor, x, height - 12, 0xe0e0e0);
x += font->widthLiteral(beforeCursor);
if (frame / 6 % 2 == 0)
drawString(font, L"_", x, height - 12, 0xe0e0e0);
x += font->width(L"_");
drawStringLiteral(font, afterCursor, x, height - 12, 0xe0e0e0);
Screen::render(xm, ym, a);
}
void ChatScreen::mouseClicked(int x, int y, int buttonNum)
{
if (buttonNum == 0)
{
if (minecraft->gui->selectedName != L"") // 4J - was nullptr comparison
{
if (message.length() > 0 && message[message.length()-1]!=L' ')
{
message = message.substr(0, cursorIndex) + L" " + message.substr(cursorIndex);
cursorIndex++;
}
size_t nameLen = minecraft->gui->selectedName.length();
size_t insertLen = (message.length() + nameLen <= SharedConstants::maxChatLength) ? nameLen : (SharedConstants::maxChatLength - message.length());
if (insertLen > 0)
{
message = message.substr(0, cursorIndex) + minecraft->gui->selectedName.substr(0, insertLen) + message.substr(cursorIndex);
cursorIndex += static_cast<int>(insertLen);
}
}
else
{
Screen::mouseClicked(x, y, buttonNum);
}
}
}