diff options
author | crupest <crupest@outlook.com> | 2021-11-14 22:27:08 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-11-14 22:27:08 +0800 |
commit | a744ed1fea0eaf4d946909da7fdc0a4c0f9c5ec0 (patch) | |
tree | 7f22783f8e0c7f61362ef64e879b50729536fea3 /include | |
parent | 7045737332f742cec36c3d35f48fad82cd41a04b (diff) | |
download | cru-a744ed1fea0eaf4d946909da7fdc0a4c0f9c5ec0.tar.gz cru-a744ed1fea0eaf4d946909da7fdc0a4c0f9c5ec0.tar.bz2 cru-a744ed1fea0eaf4d946909da7fdc0a4c0f9c5ec0.zip |
...
Diffstat (limited to 'include')
-rw-r--r-- | include/cru/common/String.hpp | 28 | ||||
-rw-r--r-- | include/cru/common/StringUtil.hpp | 73 | ||||
-rw-r--r-- | include/cru/platform/graphics/Factory.hpp | 2 | ||||
-rw-r--r-- | include/cru/ui/controls/TextHostControlService.hpp | 38 |
4 files changed, 100 insertions, 41 deletions
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp index 544d24a4..0a8d88a0 100644 --- a/include/cru/common/String.hpp +++ b/include/cru/common/String.hpp @@ -174,8 +174,7 @@ class CRU_BASE_API String { void AppendCodePoint(CodePoint code_point); Utf16CodePointIterator CodePointIterator() const { - return Utf16CodePointIterator( - std::u16string_view(reinterpret_cast<char16_t*>(buffer_), size_)); + return Utf16CodePointIterator(buffer_, size_); } Index IndexFromCodeUnitToCodePoint(Index code_unit_index) const; @@ -378,6 +377,31 @@ inline void String::append(StringView str) { inline String String::From(StringView str) { return str.ToString(); } inline String ToString(StringView value) { return value.ToString(); } + +inline CodePoint Utf16PreviousCodePoint(StringView str, Index current, + Index* previous_position) { + return Utf16PreviousCodePoint(str.data(), str.size(), current, + previous_position); +} + +inline CodePoint Utf16NextCodePoint(StringView str, Index current, + Index* next_position) { + return Utf16NextCodePoint(str.data(), str.size(), current, next_position); +} + +inline bool Utf16IsValidInsertPosition(StringView str, Index position) { + return Utf16IsValidInsertPosition(str.data(), str.size(), position); +} + +inline Index Utf16PreviousWord(StringView str, Index position, + bool* is_space = nullptr) { + return Utf16PreviousWord(str.data(), str.size(), position, is_space); +} + +inline Index Utf16NextWord(StringView str, Index position, + bool* is_space = nullptr) { + return Utf16NextWord(str.data(), str.size(), position, is_space); +} } // namespace cru template <> diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp index cd2f4e16..a24cf924 100644 --- a/include/cru/common/StringUtil.hpp +++ b/include/cru/common/StringUtil.hpp @@ -22,20 +22,21 @@ inline bool IsUtf16SurrogatePairTrailing(char16_t c) { return c >= 0xDC00 && c <= 0xDFFF; } -CodePoint CRU_BASE_API Utf8NextCodePoint(std::string_view str, Index current, - Index* next_position); +CodePoint CRU_BASE_API Utf8NextCodePoint(const char* ptr, Index size, + Index current, Index* next_position); -CodePoint CRU_BASE_API Utf16NextCodePoint(std::u16string_view str, +CodePoint CRU_BASE_API Utf16NextCodePoint(const char16_t* ptr, Index size, Index current, Index* next_position); -CodePoint CRU_BASE_API Utf16PreviousCodePoint(std::u16string_view str, +CodePoint CRU_BASE_API Utf16PreviousCodePoint(const char16_t* ptr, Index size, Index current, Index* previous_position); -template <typename StringType> -using NextCodePointFunctionType = CodePoint (*)(StringType, Index, Index*); +template <typename CharType> +using NextCodePointFunctionType = CodePoint (*)(const CharType*, Index, Index, + Index*); -template <typename StringType, - NextCodePointFunctionType<StringType> NextCodePointFunction> +template <typename CharType, + NextCodePointFunctionType<CharType> NextCodePointFunction> class CodePointIterator { public: using difference_type = Index; @@ -47,10 +48,10 @@ class CodePointIterator { public: struct past_end_tag_t {}; - explicit CodePointIterator(StringType string) - : string_(std::move(string)), position_(0) {} - explicit CodePointIterator(StringType string, past_end_tag_t) - : string_(std::move(string)), position_(string_.size()) {} + explicit CodePointIterator(const CharType* ptr, Index size, Index current = 0) + : ptr_(ptr), size_(size), position_(0) {} + explicit CodePointIterator(const CharType* ptr, Index size, past_end_tag_t) + : ptr_(ptr), size_(size), position_(size) {} CRU_DEFAULT_COPY(CodePointIterator) CRU_DEFAULT_MOVE(CodePointIterator) @@ -58,17 +59,16 @@ class CodePointIterator { ~CodePointIterator() = default; public: - StringType GetString() const { return string_; } + const CharType* GetPtr() const { return ptr_; } + Index GetSize() const { return size_; } Index GetPosition() const { return position_; } - bool IsPastEnd() const { - return position_ == static_cast<Index>(string_.size()); - } + bool IsPastEnd() const { return position_ == static_cast<Index>(size_); } public: CodePointIterator begin() const { return *this; } CodePointIterator end() const { - return CodePointIterator{string_, past_end_tag_t{}}; + return CodePointIterator{ptr_, size_, past_end_tag_t{}}; } public: @@ -96,7 +96,7 @@ class CodePointIterator { } CodePoint operator*() const { - return NextCodePointFunction(string_, position_, &next_position_cache_); + return NextCodePointFunction(ptr_, size_, position_, &next_position_cache_); } private: @@ -104,21 +104,20 @@ class CodePointIterator { if (next_position_cache_ > position_) { position_ = next_position_cache_; } else { - NextCodePointFunction(string_, position_, &position_); + NextCodePointFunction(ptr_, size_, position_, &position_); } } private: - StringType string_; + const CharType* ptr_; + Index size_; Index position_; mutable Index next_position_cache_ = 0; }; -using Utf8CodePointIterator = - CodePointIterator<std::string_view, &Utf8NextCodePoint>; +using Utf8CodePointIterator = CodePointIterator<char, &Utf8NextCodePoint>; -using Utf16CodePointIterator = - CodePointIterator<std::u16string_view, &Utf16NextCodePoint>; +using Utf16CodePointIterator = CodePointIterator<char16_t, &Utf16NextCodePoint>; void CRU_BASE_API Utf8EncodeCodePointAppend(CodePoint code_point, std::string& str); @@ -198,30 +197,28 @@ bool Utf16EncodeCodePointAppendWithFunc(CodePoint code_point, } } -std::string CRU_BASE_API ToUtf8(std::u16string_view s); -std::u16string CRU_BASE_API ToUtf16(std::string_view s); +std::string CRU_BASE_API ToUtf8(const char16_t* ptr, Index size); +std::u16string CRU_BASE_API ToUtf16(const char* s, Index size); // If given s is not a valid utf16 string, return value is UD. -bool CRU_BASE_API Utf16IsValidInsertPosition(std::u16string_view s, - gsl::index position); +bool CRU_BASE_API Utf16IsValidInsertPosition(const char16_t* ptr, Index size, + Index position); // Return position after the character making predicate returns true or 0 if no // character doing so. -gsl::index CRU_BASE_API -Utf16BackwardUntil(std::u16string_view str, gsl::index position, +Index CRU_BASE_API +Utf16BackwardUntil(const char16_t* ptr, Index size, Index position, const std::function<bool(CodePoint)>& predicate); // Return position before the character making predicate returns true or // str.size() if no character doing so. -gsl::index CRU_BASE_API -Utf16ForwardUntil(std::u16string_view str, gsl::index position, +Index CRU_BASE_API +Utf16ForwardUntil(const char16_t* ptr, Index size, Index position, const std::function<bool(CodePoint)>& predicate); -gsl::index CRU_BASE_API Utf16PreviousWord(std::u16string_view str, - gsl::index position, - bool* is_space = nullptr); -gsl::index CRU_BASE_API Utf16NextWord(std::u16string_view str, - gsl::index position, - bool* is_space = nullptr); +Index CRU_BASE_API Utf16PreviousWord(const char16_t* ptr, Index size, + Index position, bool* is_space = nullptr); +Index CRU_BASE_API Utf16NextWord(const char16_t* ptr, Index size, + Index position, bool* is_space = nullptr); char16_t CRU_BASE_API ToLower(char16_t c); char16_t CRU_BASE_API ToUpper(char16_t c); diff --git a/include/cru/platform/graphics/Factory.hpp b/include/cru/platform/graphics/Factory.hpp index ad198929..b79e1c4f 100644 --- a/include/cru/platform/graphics/Factory.hpp +++ b/include/cru/platform/graphics/Factory.hpp @@ -7,7 +7,7 @@ #include "TextLayout.hpp" namespace cru::platform::graphics { -// Entry point of the graph module. +// Entry point of the graphics module. struct IGraphicsFactory : virtual IPlatformResource { virtual std::unique_ptr<ISolidColorBrush> CreateSolidColorBrush() = 0; diff --git a/include/cru/ui/controls/TextHostControlService.hpp b/include/cru/ui/controls/TextHostControlService.hpp index b56374dc..adf72d69 100644 --- a/include/cru/ui/controls/TextHostControlService.hpp +++ b/include/cru/ui/controls/TextHostControlService.hpp @@ -23,6 +23,44 @@ struct ITextHostControl : virtual Interface { virtual render::ScrollRenderObject* GetScrollRenderObject() = 0; }; +class TextHostControlService; + +class TextControlMovePattern : public Object { + public: + static TextControlMovePattern kLeft; + static TextControlMovePattern kRight; + static TextControlMovePattern kUp; + static TextControlMovePattern kDown; + static TextControlMovePattern kHome; + static TextControlMovePattern kEnd; + static TextControlMovePattern kPageUp; + static TextControlMovePattern kPageDown; + + using MoveFunction = + std::function<gsl::index(TextHostControlService* service, StringView text, + gsl::index current_position)>; + + TextControlMovePattern(helper::ShortcutKeyBind key_bind, + MoveFunction move_function) + : key_bind_(key_bind), move_function_(move_function) {} + + CRU_DELETE_COPY(TextControlMovePattern) + CRU_DELETE_MOVE(TextControlMovePattern) + + ~TextControlMovePattern() override = default; + + public: + helper::ShortcutKeyBind GetKeyBind() const { return key_bind_; } + gsl::index Move(TextHostControlService* service, StringView text, + gsl::index current_position) { + return move_function_(service, text, current_position); + } + + private: + helper::ShortcutKeyBind key_bind_; + MoveFunction move_function_; +}; + class TextHostControlService : public Object { CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::controls::TextControlService") |