AMFP Enhanced
C++ reference: the code this unofficial fork added
Loading...
Searching...
No Matches
LangTextDecode.h
Go to the documentation of this file.
1/*
2 * AMFP-2026: decoding of a .lang <Entry> text into the engine's 16-bit wide string.
3 * Moved out of LanguageFile.cpp so it can be unit tested (tests/cpp/test_lang_text_decode.cpp).
4 *
5 * Commands: [br] -> '\n', [uNNNN] -> that code unit. Other bytes are UTF-8.
6 * abNativeUtf8=false keeps the stock decoder, which only understands 2-byte UTF-8.
7 * abNativeUtf8=true decodes 1-4 byte UTF-8 through hpl-cjk-text (BMP only; astral code
8 * points and malformed bytes become U+FFFD).
9 */
10#ifndef HPL_LANG_TEXT_DECODE_H
11#define HPL_LANG_TEXT_DECODE_H
12
13#include <cstdlib>
14#include <string>
15#include <vector>
17
18namespace hpl {
19
20/** Appends asText[aStart, aEnd) (non-ASCII bytes) decoded by hpl-cjk-text. */
21inline void LangAppendUtf8(const std::string& asText, size_t aStart, size_t aEnd, std::wstring& aOut)
22{
23 uint32_t lLen = (uint32_t)(aEnd - aStart);
24 std::vector<uint16_t> vBuf(lLen > 0 ? lLen : 1);
26 int32_t rc = hpl_cjk_decode_utf8((const uint8_t*)asText.data() + aStart, lLen, &vBuf[0], lLen, &res);
27 if(rc != HPL_CJK_OK) { aOut += (wchar_t)0xFFFD; return; }
28 for(uint32_t i = 0; i < res.len; ++i) aOut += (wchar_t)vBuf[i];
29}
30
31inline std::wstring LangDecodeText(const std::string& sString, bool abNativeUtf8)
32{
33 std::wstring wsText;
34 for(size_t i=0; i< sString.length(); ++i)
35 {
36 unsigned char c = sString[i];
37 if(c=='[')
38 {
39 // Stock command parsing, unchanged.
40 bool bFoundCommand = true;
41 std::string sCommand = "";
42 int lCount =1;
43
44 while(sString[i+lCount] != ']' && i+lCount<sString.length() && lCount < 16)
45 {
46 sCommand += sString[i+lCount];
47 lCount++;
48 }
49
50 if(sCommand=="br")
51 {
52 wsText += L'\n';
53 }
54 else if(sCommand[0]=='u')
55 {
56 int lNum = std::atoi(sCommand.substr(1).c_str());
57 wsText += (wchar_t)lNum;
58 }
59 else
60 {
61 bFoundCommand = false;
62 }
63
64 if(bFoundCommand)
65 i += lCount;
66 else
67 wsText += sString[i];
68 }
69 else if(c >= 128 && abNativeUtf8)
70 {
71 // A UTF-8 multi-byte sequence never contains a byte < 128, so decode the whole
72 // non-ASCII run at once.
73 size_t lEnd = i;
74 while(lEnd < sString.length() && (unsigned char)sString[lEnd] >= 128) ++lEnd;
75 LangAppendUtf8(sString, i, lEnd, wsText);
76 i = lEnd - 1;
77 }
78 else if(c >= 128)
79 {
80 // Stock: 2-byte UTF-8 only.
81 unsigned char c2 = sString[i+1];
82
83 int lNum = c & 0x1f; // c AND 0001 1111
84 lNum = lNum << 6;
85 lNum = lNum | (c2 & 0x3f);// c AND 0011 1111
86
87 wsText += (wchar_t)lNum;
88 ++i;
89 }
90 else
91 {
92 wsText += c;
93 }
94 }
95 return wsText;
96}
97
98}
99
100#endif
int32_t hpl_cjk_decode_utf8(const uint8_t *src, uint32_t src_len, uint16_t *dst, uint32_t dst_cap, struct HplCjkDecodeResult *out)
Decodes UTF-8 into BMP-only UTF-16.
#define HPL_CJK_OK
Success.
void LangAppendUtf8(const std::string &asText, size_t aStart, size_t aEnd, std::wstring &aOut)
Appends asText[aStart, aEnd) (non-ASCII bytes) decoded by hpl-cjk-text.
std::wstring LangDecodeText(const std::string &sString, bool abNativeUtf8)
Result of [hpl_cjk_decode_utf8].
uint32_t len
UTF-16 code units written (or needed, with HPL_CJK_BUFFER_TOO_SMALL).