28#include <TGUI/Config.hpp>
30#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
38TGUI_MODULE_EXPORT
namespace tgui
47 template <
typename CharT>
48 void encodeCharUtf8(
char32_t input, std::basic_string<CharT>& outStrUtf8)
52 outStrUtf8.push_back(
static_cast<CharT
>(input));
57 if ((input > 0x0010FFFF) || ((input >= 0xD800) && (input <= 0xDBFF)))
61 std::size_t bytestoWrite;
62 std::uint8_t firstByteMask;
68 else if (input < 0x10000)
80 std::array<CharT, 4> bytes;
81 if (bytestoWrite == 4) { bytes[3] =
static_cast<CharT
>((input | 0x80) & 0xBF); input >>= 6; }
82 if (bytestoWrite >= 3) { bytes[2] =
static_cast<CharT
>((input | 0x80) & 0xBF); input >>= 6; }
83 bytes[1] =
static_cast<CharT
>((input | 0x80) & 0xBF); input >>= 6;
84 bytes[0] =
static_cast<CharT
>(input | firstByteMask);
87 outStrUtf8.append(bytes.begin(), bytes.begin() +
static_cast<std::ptrdiff_t
>(bytestoWrite));
97 template <
typename CharIt>
98 CharIt decodeCharUtf8(CharIt inputCharIt, CharIt inputEndIt, std::u32string& outStrUtf32)
100 if (
static_cast<std::uint8_t
>(*inputCharIt) < 128)
102 outStrUtf32.push_back(
static_cast<char32_t>(
static_cast<std::uint8_t
>(*inputCharIt)));
103 return ++inputCharIt;
107 static const std::uint32_t offsetsMap[6] = { 0x00000000, 0x00003080, 0x000E2080, 0x03C82080, 0xFA082080, 0x82082080 };
108 static const std::uint8_t trailingMap[128] =
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
113 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
117 const std::uint8_t trailingBytes = trailingMap[
static_cast<std::uint8_t
>(*inputCharIt) - 128];
118 const std::uint32_t offset = offsetsMap[trailingBytes];
119 const auto remainingBytes = std::distance(inputCharIt, inputEndIt) - 1;
120 if (remainingBytes >=
static_cast<decltype(remainingBytes)
>(trailingBytes))
122 char32_t outputChar = 0;
123 for (std::uint8_t i = 0; i < trailingBytes; ++i)
125 outputChar +=
static_cast<char32_t>(
static_cast<std::uint8_t
>(*inputCharIt++));
129 outputChar +=
static_cast<char32_t>(
static_cast<std::uint8_t
>(*inputCharIt++));
130 outputChar -= offset;
131 outStrUtf32.push_back(outputChar);
134 inputCharIt = inputEndIt;
139#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201811L)
145 TGUI_NODISCARD
inline std::u8string convertUtf32toUtf8(
const std::u32string& strUtf32)
147 std::u8string outStrUtf8;
148 outStrUtf8.reserve(strUtf32.length() + 1);
149 for (
const char32_t& codepoint : strUtf32)
150 encodeCharUtf8(codepoint, outStrUtf8);
162 template <
typename CharIt>
163 TGUI_NODISCARD std::u32string convertUtf8toUtf32(CharIt inputBegin, CharIt inputEnd)
165 std::u32string outStrUtf32;
166 outStrUtf32.reserve(
static_cast<std::size_t
>((inputEnd - inputBegin) + 1));
168 auto it = inputBegin;
169 while (it < inputEnd)
170 it = decodeCharUtf8(it, inputEnd, outStrUtf32);
181 template <
typename U16CharIt>
182 TGUI_NODISCARD std::u32string convertUtf16toUtf32(U16CharIt inputBegin, U16CharIt inputEnd)
184 std::u32string outStrUtf32;
185 outStrUtf32.reserve(
static_cast<std::size_t
>((inputEnd - inputBegin) + 1));
187 auto it = inputBegin;
188 while (it < inputEnd)
190 const char16_t first = *it++;
193 if ((first < 0xD800) || (first > 0xDBFF))
195 outStrUtf32.push_back(
static_cast<char32_t>(first));
203 const char16_t second = *it++;
204 if ((second >= 0xDC00) && (second <= 0xDFFF))
205 outStrUtf32.push_back(((
static_cast<char32_t>(first) - 0xD800) << 10) + (
static_cast<char32_t>(second) - 0xDC00) + 0x0010000);
216 template <
typename WCharIt>
217 TGUI_NODISCARD std::u32string convertWidetoUtf32(WCharIt inputBegin, WCharIt inputEnd)
219 std::u32string outStrUtf32;
220 outStrUtf32.reserve(
static_cast<std::size_t
>((inputEnd - inputBegin) + 1));
223 for (
auto it = inputBegin; it != inputEnd; ++it)
224 outStrUtf32.push_back(
static_cast<char32_t>(*it));
234 TGUI_NODISCARD
inline std::string convertUtf32toStdStringUtf8(
const std::u32string& strUtf32)
236 std::string outStrUtf8;
237 outStrUtf8.reserve(strUtf32.length() + 1);
238 for (
const char32_t codepoint : strUtf32)
239 encodeCharUtf8(codepoint, outStrUtf8);
249 TGUI_NODISCARD
inline std::wstring convertUtf32toWide(
const std::u32string& strUtf32)
252 outStr.reserve(strUtf32.length() + 1);
254 TGUI_IF_CONSTEXPR (
sizeof(
wchar_t) == 4)
257 for (
const char32_t codepoint : strUtf32)
258 outStr.push_back(
static_cast<wchar_t>(codepoint));
263 for (
const char32_t codepoint : strUtf32)
265 if ((codepoint < 0xD800) || ((codepoint > 0xDFFF) && (codepoint <= 0xFFFF)))
266 outStr.push_back(
static_cast<wchar_t>(codepoint));
278 TGUI_NODISCARD
inline std::u16string convertUtf32toUtf16(
const std::u32string& strUtf32)
280 std::u16string outStrUtf16;
281 outStrUtf16.reserve(strUtf32.length() + 1);
283 for (
const char32_t codepoint : strUtf32)
286 if (codepoint <= 0xFFFF)
288 if ((codepoint < 0xD800) || (codepoint > 0xDFFF))
289 outStrUtf16.push_back(
static_cast<char16_t>(codepoint));
293 else if (codepoint > 0x0010FFFF)
297 outStrUtf16.push_back(
static_cast<char16_t>(((codepoint - 0x0010000) >> 10) + 0xD800));
298 outStrUtf16.push_back(
static_cast<char16_t>(((codepoint - 0x0010000) & 0x3FFUL) + 0xDC00));
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38