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) {
141 if (start >= input.size()) {
145 const uint8_t C0 = input[start];
148 if ((C0 & 0b1000'0000) == 0b0000'0000) {
149 *ucs = C0 & 0b0111'1111;
155 if ((C0 & 0b1110'0000) == 0b1100'0000 &&
156 start + 1 < input.size()) {
157 const uint8_t C1 = input[start + 1];
159 *ucs += C0 & 0b0001'1111;
161 *ucs += C1 & 0b0011'1111;
167 if ((C0 & 0b1111'0000) == 0b1110'0000 &&
168 start + 2 < input.size()) {
169 const uint8_t C1 = input[start + 1];
170 const uint8_t C2 = input[start + 2];
172 *ucs += C0 & 0b0000'1111;
174 *ucs += C1 & 0b0011'1111;
176 *ucs += C2 & 0b0011'1111;
182 if ((C0 & 0b1111'1000) == 0b1111'0000 &&
183 start + 3 < input.size()) {
184 const uint8_t C1 = input[start + 1];
185 const uint8_t C2 = input[start + 2];
186 const uint8_t C3 = input[start + 3];
188 *ucs += C0 & 0b0000'0111;
190 *ucs += C1 & 0b0011'1111;
192 *ucs += C2 & 0b0011'1111;
194 *ucs += C3 & 0b0011'1111;
210 if (start >= input.size()) {
216 if constexpr (
sizeof(wchar_t) == 4) {
223 int32_t C0 = input[start];
226 if (C0 < 0xd800 || C0 >= 0xdc00) {
233 if (start + 1 >= input.size()) {
238 int32_t C1 = input[start + 1];
239 *ucs = ((C0 & 0x3ff) << 10) + (C1 & 0x3ff) + 0x10000;
245 return Bisearch(ucs, g_extend_characters);
261 const uint32_t LINE_FEED = 10;
262 return ucs != LINE_FEED;
264 if (ucs >= 0x7f && ucs < 0xa0) {
271 WordBreakPropertyInterval interval = {0, 0, WBP::ALetter};
273 return interval.property;
277 return codepoint_width(uint32_t(ucs));
283 for (
const wchar_t& it : text) {
301 if (input.size() == 1) {
302 const char c = input[0];
303 if (c >= 32 && c < 127) {
311 bool is_pure_ascii =
true;
312 for (
const char c : input) {
313 if (c < 31 || c >= 127) {
314 is_pure_ascii =
false;
319 return static_cast<int>(input.size());
324 while (start < input.size()) {
325 uint32_t codepoint = 0;
349 std::vector<std::string> out;
350 out.reserve(input.size());
353 while (start < input.size()) {
354 uint32_t codepoint = 0;
360 const auto append = input.substr(start, end - start);
371 out.back() += append;
379 out.emplace_back(append);
380 out.emplace_back(
"");
385 out.emplace_back(append);
398 if ((input[start] & 0b1100'0000) == 0b1000'0000) {
402 uint32_t codepoint = 0;
404 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
416 bool glyph_found =
false;
417 while (start < input.size()) {
419 uint32_t codepoint = 0;
420 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
431 return static_cast<int>(start);
438 return static_cast<int>(input.size());
441size_t GlyphIterate(std::string_view input,
int glyph_offset,
size_t start) {
442 if (glyph_offset >= 0) {
443 for (
int i = 0; i < glyph_offset; ++i) {
448 for (
int i = 0; i < -glyph_offset; ++i) {
457 std::vector<int> out;
458 out.reserve(input.size());
461 while (start < input.size()) {
462 uint32_t codepoint = 0;
463 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
500 while (start < input.size()) {
501 uint32_t codepoint = 0;
502 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
525 std::vector<WordBreakProperty> out;
526 out.reserve(input.size());
529 while (start < input.size()) {
530 uint32_t codepoint = 0;
547 WordBreakPropertyInterval interval = {0, 0, WBP::ALetter};
549 out.push_back(interval.property);
559 uint32_t codepoint = 0;
576 if (codepoint <= 0b000'0000'0111'1111) {
577 const uint8_t p1 = codepoint;
583 if (codepoint <= 0b000'0111'1111'1111) {
584 uint8_t p2 = codepoint & 0b111111;
586 uint8_t p1 = codepoint;
587 out.push_back(0b11000000 + p1);
588 out.push_back(0b10000000 + p2);
593 if (codepoint <= 0b1111'1111'1111'1111) {
594 uint8_t p3 = codepoint & 0b111111;
596 uint8_t p2 = codepoint & 0b111111;
598 uint8_t p1 = codepoint;
599 out.push_back(0b11100000 + p1);
600 out.push_back(0b10000000 + p2);
601 out.push_back(0b10000000 + p3);
606 if (codepoint <= 0b1'0000'1111'1111'1111'1111) {
607 uint8_t p4 = codepoint & 0b111111;
609 uint8_t p3 = codepoint & 0b111111;
611 uint8_t p2 = codepoint & 0b111111;
613 uint8_t p1 = codepoint;
614 out.push_back(0b11110000 + p1);
615 out.push_back(0b10000000 + p2);
616 out.push_back(0b10000000 + p3);
617 out.push_back(0b10000000 + p4);
631 uint32_t codepoint = 0;
634 if constexpr (
sizeof(wchar_t) == 4) {
635 out.push_back(codepoint);
643 if (codepoint < 0xD800 || (codepoint > 0xDFFF && codepoint < 0x10000)) {
644 uint16_t p0 = codepoint;
650 codepoint -= 0x010000;
651 uint16_t p0 = (((codepoint << 12) >> 22) + 0xD800);
652 uint16_t p1 = (((codepoint << 22) >> 22) + 0xDC00);
Decorator size(WidthOrHeight direction, Constraint constraint, int value)
對元素的大小套用限制。
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)
將 std::wstring 轉換為 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