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) {
139 if (start >= input.size()) {
143 const uint8_t C0 = input[start];
146 if ((C0 & 0b1000'0000) == 0b0000'0000) {
147 *ucs = C0 & 0b0111'1111;
153 if ((C0 & 0b1110'0000) == 0b1100'0000 &&
154 start + 1 < input.size()) {
155 const uint8_t C1 = input[start + 1];
157 *ucs += C0 & 0b0001'1111;
159 *ucs += C1 & 0b0011'1111;
165 if ((C0 & 0b1111'0000) == 0b1110'0000 &&
166 start + 2 < input.size()) {
167 const uint8_t C1 = input[start + 1];
168 const uint8_t C2 = input[start + 2];
170 *ucs += C0 & 0b0000'1111;
172 *ucs += C1 & 0b0011'1111;
174 *ucs += C2 & 0b0011'1111;
180 if ((C0 & 0b1111'1000) == 0b1111'0000 &&
181 start + 3 < input.size()) {
182 const uint8_t C1 = input[start + 1];
183 const uint8_t C2 = input[start + 2];
184 const uint8_t C3 = input[start + 3];
186 *ucs += C0 & 0b0000'0111;
188 *ucs += C1 & 0b0011'1111;
190 *ucs += C2 & 0b0011'1111;
192 *ucs += C3 & 0b0011'1111;
206 if (start >= input.size()) {
212 if constexpr (
sizeof(wchar_t) == 4) {
219 int32_t C0 = input[start];
222 if (C0 < 0xd800 || C0 >= 0xdc00) {
229 if (start + 1 >= input.size()) {
234 int32_t C1 = input[start + 1];
235 *ucs = ((C0 & 0x3ff) << 10) + (C1 & 0x3ff) + 0x10000;
241 return Bisearch(ucs, g_extend_characters);
257 const uint32_t LINE_FEED = 10;
258 return ucs != LINE_FEED;
260 if (ucs >= 0x7f && ucs < 0xa0) {
267 WordBreakPropertyInterval interval = {0, 0, WBP::ALetter};
269 return interval.property;
273 return codepoint_width(uint32_t(ucs));
279 for (
const wchar_t& it : text) {
296 if (input.size() == 1) {
297 const char c = input[0];
298 if (c >= 32 && c < 127) {
305 bool is_pure_ascii =
true;
306 for (
const char c : input) {
307 if (c < 31 || c >= 127) {
308 is_pure_ascii =
false;
313 return static_cast<int>(input.size());
318 while (start < input.size()) {
319 uint32_t codepoint = 0;
343 std::vector<std::string> out;
344 out.reserve(input.size());
347 while (start < input.size()) {
348 uint32_t codepoint = 0;
354 const auto append = input.substr(start, end - start);
365 out.back() += append;
373 out.emplace_back(append);
374 out.emplace_back(
"");
379 out.emplace_back(append);
392 if ((input[start] & 0b1100'0000) == 0b1000'0000) {
396 uint32_t codepoint = 0;
398 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
410 bool glyph_found =
false;
411 while (start < input.size()) {
413 uint32_t codepoint = 0;
414 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
424 return static_cast<int>(start);
431 return static_cast<int>(input.size());
434size_t GlyphIterate(std::string_view input,
int glyph_offset,
size_t start) {
435 if (glyph_offset >= 0) {
436 for (
int i = 0; i < glyph_offset; ++i) {
441 for (
int i = 0; i < -glyph_offset; ++i) {
450 std::vector<int> out;
451 out.reserve(input.size());
454 while (start < input.size()) {
455 uint32_t codepoint = 0;
456 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
493 while (start < input.size()) {
494 uint32_t codepoint = 0;
495 const bool eaten =
EatCodePoint(input, start, &end, &codepoint);
517 std::vector<WordBreakProperty> out;
518 out.reserve(input.size());
521 while (start < input.size()) {
522 uint32_t codepoint = 0;
539 WordBreakPropertyInterval interval = {0, 0, WBP::ALetter};
541 out.push_back(interval.property);
551 uint32_t codepoint = 0;
568 if (codepoint <= 0b000'0000'0111'1111) {
569 const uint8_t p1 = codepoint;
575 if (codepoint <= 0b000'0111'1111'1111) {
576 uint8_t p2 = codepoint & 0b111111;
578 uint8_t p1 = codepoint;
579 out.push_back(0b11000000 + p1);
580 out.push_back(0b10000000 + p2);
585 if (codepoint <= 0b1111'1111'1111'1111) {
586 uint8_t p3 = codepoint & 0b111111;
588 uint8_t p2 = codepoint & 0b111111;
590 uint8_t p1 = codepoint;
591 out.push_back(0b11100000 + p1);
592 out.push_back(0b10000000 + p2);
593 out.push_back(0b10000000 + p3);
598 if (codepoint <= 0b1'0000'1111'1111'1111'1111) {
599 uint8_t p4 = codepoint & 0b111111;
601 uint8_t p3 = codepoint & 0b111111;
603 uint8_t p2 = codepoint & 0b111111;
605 uint8_t p1 = codepoint;
606 out.push_back(0b11110000 + p1);
607 out.push_back(0b10000000 + p2);
608 out.push_back(0b10000000 + p3);
609 out.push_back(0b10000000 + p4);
623 uint32_t codepoint = 0;
626 if constexpr (
sizeof(wchar_t) == 4) {
627 out.push_back(codepoint);
635 if (codepoint < 0xD800 || (codepoint > 0xDFFF && codepoint < 0x10000)) {
636 uint16_t p0 = codepoint;
642 codepoint -= 0x010000;
643 uint16_t p0 = (((codepoint << 12) >> 22) + 0xD800);
644 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