33struct WordBreakPropertyInterval {
44constexpr auto g_extend_characters{[]()
constexpr {
46 constexpr size_t size = []()
constexpr {
49 if (interval.property == WBP::Extend) {
57 std::array<Interval, size> result{};
60 if (interval.property == WBP::Extend) {
61 result[index++] = {interval.first, interval.last};
69bool Bisearch(uint32_t ucs,
const std::array<Interval, N>& table) {
70 if (ucs < table.front().first || ucs > table.back().last) {
77 const int mid = (min + max) / 2;
78 if (ucs > table[mid].last) {
80 }
else if (ucs < table[mid].first) {
91template <
class C,
size_t N>
92bool Bisearch(uint32_t ucs,
const std::array<C, N>& table, C* out) {
93 if (ucs < table.front().first || ucs > table.back().last) {
100 const int mid = (min + max) / 2;
101 if (ucs > table[mid].last) {
103 }
else if (ucs < table[mid].first) {
114int codepoint_width(uint32_t ucs) {
142 if (start >= input.size()) {
146 const uint8_t C0 = input[start];
149 if ((C0 & 0b1000'0000) == 0b0000'0000) {
150 *ucs = C0 & 0b0111'1111;
156 if ((C0 & 0b1110'0000) == 0b1100'0000 &&
157 start + 1 < input.size()) {
158 const uint8_t C1 = input[start + 1];
160 *ucs += C0 & 0b0001'1111;
162 *ucs += C1 & 0b0011'1111;
168 if ((C0 & 0b1111'0000) == 0b1110'0000 &&
169 start + 2 < input.size()) {
170 const uint8_t C1 = input[start + 1];
171 const uint8_t C2 = input[start + 2];
173 *ucs += C0 & 0b0000'1111;
175 *ucs += C1 & 0b0011'1111;
177 *ucs += C2 & 0b0011'1111;
183 if ((C0 & 0b1111'1000) == 0b1111'0000 &&
184 start + 3 < input.size()) {
185 const uint8_t C1 = input[start + 1];
186 const uint8_t C2 = input[start + 2];
187 const uint8_t C3 = input[start + 3];
189 *ucs += C0 & 0b0000'0111;
191 *ucs += C1 & 0b0011'1111;
193 *ucs += C2 & 0b0011'1111;
195 *ucs += C3 & 0b0011'1111;
212 if (start >= input.size()) {
218 if constexpr (
sizeof(wchar_t) == 4) {
225 int32_t C0 = input[start];
228 if (C0 < 0xd800 || C0 >= 0xdc00) {
235 if (start + 1 >= input.size()) {
240 int32_t C1 = input[start + 1];
241 *ucs = ((C0 & 0x3ff) << 10) + (C1 & 0x3ff) + 0x10000;
247 return Bisearch(ucs, g_extend_characters);
263 const uint32_t LINE_FEED = 10;
264 return ucs != LINE_FEED;
266 if (ucs >= 0x7f && ucs < 0xa0) {
273 WordBreakPropertyInterval interval = {0, 0, WBP::ALetter};
275 return interval.property;
279 return codepoint_width(uint32_t(ucs));
285 for (
const wchar_t& it : text) {
303 if (input.size() == 1) {
304 const char c = input[0];
305 if (c >= 32 && c < 127) {
313 bool is_pure_ascii =
true;
314 for (
const char c : input) {
315 if (c < 31 || c >= 127) {
316 is_pure_ascii =
false;
321 return static_cast<int>(input.size());
326 while (start < input.size()) {
327 uint32_t codepoint = 0;
351 std::vector<std::string> out;
352 out.reserve(input.size());
355 while (start < input.size()) {
356 uint32_t codepoint = 0;
362 const auto append = input.substr(start, end - start);
373 out.back() += append;
381 out.emplace_back(append);
382 out.emplace_back(
"");
387 out.emplace_back(append);
400 if ((input[start] & 0b1100'0000) == 0b1000'0000) {
404 uint32_t codepoint = 0;
406 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
418 bool glyph_found =
false;
419 while (start < input.size()) {
421 uint32_t codepoint = 0;
422 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
433 return static_cast<int>(start);
440 return static_cast<int>(input.size());
443size_t GlyphIterate(std::string_view input,
int glyph_offset,
size_t start) {
444 if (glyph_offset >= 0) {
445 for (
int i = 0; i < glyph_offset; ++i) {
450 for (
int i = 0; i < -glyph_offset; ++i) {
459 std::vector<int> out;
460 out.reserve(input.size());
463 while (start < input.size()) {
464 uint32_t codepoint = 0;
465 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
502 while (start < input.size()) {
503 uint32_t codepoint = 0;
504 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
527 std::vector<WordBreakProperty> out;
528 out.reserve(input.size());
531 while (start < input.size()) {
532 uint32_t codepoint = 0;
549 WordBreakPropertyInterval interval = {0, 0, WBP::ALetter};
551 out.push_back(interval.property);
561 uint32_t codepoint = 0;
578 if (codepoint <= 0b000'0000'0111'1111) {
579 const uint8_t p1 = codepoint;
585 if (codepoint <= 0b000'0111'1111'1111) {
586 uint8_t p2 = codepoint & 0b111111;
588 uint8_t p1 = codepoint;
589 out.push_back(0b11000000 + p1);
590 out.push_back(0b10000000 + p2);
595 if (codepoint <= 0b1111'1111'1111'1111) {
596 uint8_t p3 = codepoint & 0b111111;
598 uint8_t p2 = codepoint & 0b111111;
600 uint8_t p1 = codepoint;
601 out.push_back(0b11100000 + p1);
602 out.push_back(0b10000000 + p2);
603 out.push_back(0b10000000 + p3);
608 if (codepoint <= 0b1'0000'1111'1111'1111'1111) {
609 uint8_t p4 = codepoint & 0b111111;
611 uint8_t p3 = codepoint & 0b111111;
613 uint8_t p2 = codepoint & 0b111111;
615 uint8_t p1 = codepoint;
616 out.push_back(0b11110000 + p1);
617 out.push_back(0b10000000 + p2);
618 out.push_back(0b10000000 + p3);
619 out.push_back(0b10000000 + p4);
633 uint32_t codepoint = 0;
636 if constexpr (
sizeof(wchar_t) == 4) {
637 out.push_back(codepoint);
645 if (codepoint < 0xD800 || (codepoint > 0xDFFF && codepoint < 0x10000)) {
646 uint16_t p0 = codepoint;
652 codepoint -= 0x010000;
653 uint16_t p0 = (((codepoint << 12) >> 22) + 0xD800);
654 uint16_t p1 = (((codepoint << 22) >> 22) + 0xDC00);
Decorator size(WidthOrHeight direction, Constraint constraint, int value)
Apply a constraint on the size of an element.
The FTXUI ftxui:: namespace.
bool IsControl(uint32_t ucs)
WordBreakProperty CodepointToWordBreakProperty(uint32_t codepoint)
size_t GlyphPrevious(std::string_view input, size_t start)
FTXUI_EXPORT(SCREEN) int string_width(std std::vector< std::string > Utf8ToGlyphs(std::string_view input)
int string_width(std::string_view input)
bool IsCombining(uint32_t ucs)
int wchar_width(wchar_t ucs)
bool EatCodePoint(std::string_view input, size_t start, size_t *end, uint32_t *ucs)
std::string to_string(std::wstring_view s)
Convert a std::wstring into a UTF8 std::string.
int GlyphCount(std::string_view input)
std::vector< WordBreakProperty > Utf8ToWordBreakProperty(std::string_view input)
int wstring_width(const std::wstring &text)
std::vector< int > CellToGlyphIndex(std::string_view input)
FTXUI_EXPORT(SCREEN) std FTXUI_EXPORT(SCREEN) std std::wstring to_wstring(T s)
size_t GlyphIterate(std::string_view input, int glyph_offset, size_t start)
bool IsFullWidth(uint32_t ucs)
size_t GlyphNext(std::string_view input, size_t start)
constexpr std::array< Interval, 123 > g_full_width_characters
constexpr std::array< WordBreakPropertyInterval, 1100 > g_word_break_intervals