diff options
author | crupest <crupest@outlook.com> | 2020-07-05 23:06:02 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-07-05 23:06:02 +0800 |
commit | 5c805e494425a88da1813902b1ad8a1ab351e30d (patch) | |
tree | be3cfd96dcac19db3e256d610d48b5083c489a6c /include | |
parent | bbec59718bf8a824583869126762013112f8e568 (diff) | |
download | cru-5c805e494425a88da1813902b1ad8a1ab351e30d.tar.gz cru-5c805e494425a88da1813902b1ad8a1ab351e30d.tar.bz2 cru-5c805e494425a88da1813902b1ad8a1ab351e30d.zip |
...
Diffstat (limited to 'include')
37 files changed, 145 insertions, 205 deletions
diff --git a/include/cru/common/Base.hpp b/include/cru/common/Base.hpp index 409c2b0e..93d6f9a6 100644 --- a/include/cru/common/Base.hpp +++ b/include/cru/common/Base.hpp @@ -47,5 +47,5 @@ using Index = gsl::index; #define CRU_DEFINE_CLASS_LOG_TAG(tag) \ private: \ - constexpr static std::string_view log_tag = tag; + constexpr static std::u16string_view log_tag = tag; } // namespace cru diff --git a/include/cru/common/Logger.hpp b/include/cru/common/Logger.hpp index f76e4626..f83ba8dc 100644 --- a/include/cru/common/Logger.hpp +++ b/include/cru/common/Logger.hpp @@ -15,30 +15,7 @@ enum class LogLevel { Debug, Info, Warn, Error }; struct ILogSource : virtual Interface { // Write the string s. LogLevel is just a helper. It has no effect on the // content to write. - virtual void Write(LogLevel level, std::string_view s) = 0; -}; - -class StdioLogSource : public virtual ILogSource { - public: - StdioLogSource() = default; - - CRU_DELETE_COPY(StdioLogSource) - CRU_DELETE_MOVE(StdioLogSource) - - ~StdioLogSource() override = default; - - void Write(LogLevel level, std::string_view s) override { - // TODO: Emmm... Since it is buggy to use narrow char in UTF-8 on Windows. I - // think this implementation might be broken. (However, I didn't test it.) - // Maybe, I should detect Windows and use wide char (And I didn't test this - // either) or other more complicated implementation. Currently, I settled - // with this. - if (level == LogLevel::Error) { - std::cerr << s; - } else { - std::cout << s; - } - } + virtual void Write(LogLevel level, const std::u16string& s) = 0; }; class Logger : public Object { @@ -58,8 +35,8 @@ class Logger : public Object { void RemoveSource(ILogSource* source); public: - void Log(LogLevel level, std::string_view s); - void Log(LogLevel level, std::string_view tag, std::string_view s); + void Log(LogLevel level, std::u16string_view s); + void Log(LogLevel level, std::u16string_view tag, std::u16string_view s); public: std::list<std::unique_ptr<ILogSource>> sources_; @@ -92,7 +69,7 @@ void Error(TArgs&&... args) { } template <typename... TArgs> -void TagDebug([[maybe_unused]] std::string_view tag, +void TagDebug([[maybe_unused]] std::u16string_view tag, [[maybe_unused]] TArgs&&... args) { #ifdef CRU_DEBUG Logger::GetInstance()->Log(LogLevel::Debug, tag, @@ -101,19 +78,19 @@ void TagDebug([[maybe_unused]] std::string_view tag, } template <typename... TArgs> -void TagInfo(std::string_view tag, TArgs&&... args) { +void TagInfo(std::u16string_view tag, TArgs&&... args) { Logger::GetInstance()->Log(LogLevel::Info, tag, fmt::format(std::forward<TArgs>(args)...)); } template <typename... TArgs> -void TagWarn(std::string_view tag, TArgs&&... args) { +void TagWarn(std::u16string_view tag, TArgs&&... args) { Logger::GetInstance()->Log(LogLevel::Warn, tag, fmt::format(std::forward<TArgs>(args)...)); } template <typename... TArgs> -void TagError(std::string_view tag, TArgs&&... args) { +void TagError(std::u16string_view tag, TArgs&&... args) { Logger::GetInstance()->Log(LogLevel::Error, tag, fmt::format(std::forward<TArgs>(args)...)); } diff --git a/include/cru/common/PreConfig.hpp b/include/cru/common/PreConfig.hpp index 4bccef1d..802f17f8 100644 --- a/include/cru/common/PreConfig.hpp +++ b/include/cru/common/PreConfig.hpp @@ -6,3 +6,4 @@ #endif #define _CRT_SECURE_NO_WARNINGS +#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp index a44ae6b4..714f1d49 100644 --- a/include/cru/common/StringUtil.hpp +++ b/include/cru/common/StringUtil.hpp @@ -3,37 +3,90 @@ namespace cru { using CodePoint = std::int32_t; -constexpr CodePoint k_code_point_end = -1; +constexpr CodePoint k_invalid_code_point = -1; class TextEncodeException : public std::runtime_error { public: using runtime_error::runtime_error; }; -class Utf8Iterator : public Object { +inline bool IsSurrogatePair(char16_t c) { return c >= 0xD800 && c <= 0xDFFF; } + +inline bool IsSurrogatePairLeading(char16_t c) { + return c >= 0xD800 && c <= 0xDBFF; +} + +inline bool IsSurrogatePairTrailing(char16_t c) { + return c >= 0xDC00 && c <= 0xDFFF; +} + +class Utf16Iterator : public Object { public: - explicit Utf8Iterator(const std::string_view& string) : string_(string) {} - Utf8Iterator(const std::string_view& string, Index position) - : string_(string), position_(position) {} + explicit Utf16Iterator(std::u16string_view string) + : string_(std::move(string)) {} + Utf16Iterator(std::u16string_view string, Index position) + : string_(std::move(string)), position_(position) {} - CRU_DEFAULT_COPY(Utf8Iterator) - CRU_DEFAULT_MOVE(Utf8Iterator) + CRU_DEFAULT_COPY(Utf16Iterator) + CRU_DEFAULT_MOVE(Utf16Iterator) - ~Utf8Iterator() = default; + ~Utf16Iterator() = default; public: - void SetToHead() { position_ = 0; } + void SetPositionToHead() { position_ = 0; } void SetPosition(Index position) { position_ = position; } - // Advance current position and get next code point. Return k_code_point_end - // if there is no next code unit(point). Throw TextEncodeException if decoding - // fails. + // Backward current position and get previous code point. Return + // k_invalid_code_point if reach head. Throw TextEncodeException if encounter + // encoding problem. + CodePoint Previous(); + + // Advance current position and get next code point. Return + // k_invalid_code_point if reach tail. Throw TextEncodeException if encounter + // encoding problem. CodePoint Next(); Index CurrentPosition() const { return this->position_; } private: - std::string_view string_; + std::u16string_view string_; Index position_ = 0; }; + +Index PreviousIndex(std::u16string_view string, Index current); +Index NextIndex(std::u16string_view string, Index current); + +std::string ToUtf8(const std::u16string& s); +inline std::string ToUtf8(std::u16string_view s) { + return ToUtf8(std::u16string{s}); +} + +// class Utf8Iterator : public Object { +// public: +// explicit Utf8Iterator(const std::string_view& string) : string_(string) {} +// Utf8Iterator(const std::string_view& string, Index position) +// : string_(string), position_(position) {} + +// CRU_DEFAULT_COPY(Utf8Iterator) +// CRU_DEFAULT_MOVE(Utf8Iterator) + +// ~Utf8Iterator() = default; + +// public: +// void SetToHead() { position_ = 0; } +// void SetPosition(Index position) { position_ = position; } + +// // Advance current position and get next code point. Return +// k_invalid_code_point +// // if there is no next code unit(point). Throw TextEncodeException if +// decoding +// // fails. +// CodePoint Next(); + +// Index CurrentPosition() const { return this->position_; } + +// private: +// std::string_view string_; +// Index position_ = 0; +// }; } // namespace cru diff --git a/include/cru/platform/Check.hpp b/include/cru/platform/Check.hpp index f4bbcfe8..d3180582 100644 --- a/include/cru/platform/Check.hpp +++ b/include/cru/platform/Check.hpp @@ -2,6 +2,8 @@ #include "Exception.hpp" #include "Resource.hpp" +#include "cru/common/StringUtil.hpp" + #include <fmt/format.h> #include <memory> #include <type_traits> @@ -9,14 +11,14 @@ namespace cru::platform { template <typename TTarget> TTarget* CheckPlatform(INativeResource* resource, - const std::string_view& target_platform) { + const std::u16string_view& target_platform) { Expects(resource); const auto result = dynamic_cast<TTarget*>(resource); if (result == nullptr) { throw UnsupportPlatformException(fmt::format( "Try to convert resource to target platform failed. Platform id of " "resource to convert: {} . Target platform id: {} .", - resource->GetPlatformId(), target_platform)); + ToUtf8(resource->GetPlatformId()), ToUtf8(target_platform))); } return result; } @@ -24,7 +26,7 @@ TTarget* CheckPlatform(INativeResource* resource, template <typename TTarget, typename TSource> std::shared_ptr<TTarget> CheckPlatform( const std::shared_ptr<TSource>& resource, - const std::string_view& target_platform) { + const std::u16string_view& target_platform) { static_assert(std::is_base_of_v<INativeResource, TSource>, "TSource must be a subclass of INativeResource."); Expects(resource); @@ -33,7 +35,7 @@ std::shared_ptr<TTarget> CheckPlatform( throw UnsupportPlatformException(fmt::format( "Try to convert resource to target platform failed. Platform id of " "resource to convert: {} . Target platform id: {} .", - resource->GetPlatformId(), target_platform)); + ToUtf8(resource->GetPlatformId()), ToUtf8(target_platform))); } return result; } diff --git a/include/cru/platform/Resource.hpp b/include/cru/platform/Resource.hpp index 72cfaf52..7a85d9c1 100644 --- a/include/cru/platform/Resource.hpp +++ b/include/cru/platform/Resource.hpp @@ -5,6 +5,6 @@ namespace cru::platform { struct INativeResource : virtual Interface { - virtual std::string_view GetPlatformId() const = 0; + virtual std::u16string_view GetPlatformId() const = 0; }; } // namespace cru::platform diff --git a/include/cru/platform/graph/Factory.hpp b/include/cru/platform/graph/Factory.hpp index 0a425d15..b4e68f12 100644 --- a/include/cru/platform/graph/Factory.hpp +++ b/include/cru/platform/graph/Factory.hpp @@ -16,10 +16,10 @@ struct IGraphFactory : virtual INativeResource { virtual std::unique_ptr<IGeometryBuilder> CreateGeometryBuilder() = 0; - virtual std::unique_ptr<IFont> CreateFont(const std::string_view& font_family, + virtual std::unique_ptr<IFont> CreateFont(std::u16string font_family, float font_size) = 0; virtual std::unique_ptr<ITextLayout> CreateTextLayout( - std::shared_ptr<IFont> font, std::string text) = 0; + std::shared_ptr<IFont> font, std::u16string text) = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/graph/TextLayout.hpp b/include/cru/platform/graph/TextLayout.hpp index 4086ac56..7dd20987 100644 --- a/include/cru/platform/graph/TextLayout.hpp +++ b/include/cru/platform/graph/TextLayout.hpp @@ -6,8 +6,8 @@ namespace cru::platform::graph { struct ITextLayout : virtual IGraphResource { - virtual std::string GetText() = 0; - virtual void SetText(std::string new_text) = 0; + virtual std::u16string GetText() = 0; + virtual void SetText(std::u16string new_text) = 0; virtual std::shared_ptr<IFont> GetFont() = 0; virtual void SetFont(std::shared_ptr<IFont> font) = 0; @@ -17,7 +17,7 @@ struct ITextLayout : virtual IGraphResource { virtual Rect GetTextBounds() = 0; virtual std::vector<Rect> TextRangeRect(const TextRange& text_range) = 0; - virtual Point TextSinglePoint(gsl::index position, bool trailing) = 0; + virtual Point TextSinglePoint(Index position, bool trailing) = 0; virtual TextHitTestResult HitTest(const Point& point) = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/native/InputMethod.hpp b/include/cru/platform/native/InputMethod.hpp index 1ede15b2..1c5b287e 100644 --- a/include/cru/platform/native/InputMethod.hpp +++ b/include/cru/platform/native/InputMethod.hpp @@ -18,28 +18,11 @@ struct CompositionClause { using CompositionClauses = std::vector<CompositionClause>; struct CompositionText { - std::string text; + std::u16string text; CompositionClauses clauses; TextRange selection; }; -inline std::ostream& operator<<(std::ostream& stream, - const CompositionText& composition_text) { - stream << "text: " << composition_text.text << "\n" - << "clauses:\n"; - for (int i = 0; i < static_cast<int>(composition_text.clauses.size()); i++) { - const auto& clause = composition_text.clauses[i]; - stream << "\t" << i << ". start:" << clause.start << " end:" << clause.end; - if (clause.target) { - stream << " target"; - } - stream << "\n"; - } - stream << "selection: position:" << composition_text.selection.position - << " count:" << composition_text.selection.count; - return stream; -} - struct IInputMethodContext : virtual INativeResource { // Return true if you should draw composition text manually. Return false if // system will take care of that for you. @@ -67,7 +50,7 @@ struct IInputMethodContext : virtual INativeResource { // Triggered every time composition text changes. virtual IEvent<std::nullptr_t>* CompositionEvent() = 0; - virtual IEvent<std::string_view>* TextEvent() = 0; + virtual IEvent<std::u16string_view>* TextEvent() = 0; }; struct IInputMethodManager : virtual INativeResource { diff --git a/include/cru/ui/ClickDetector.hpp b/include/cru/ui/ClickDetector.hpp index 3977fb8e..4ffe5d05 100644 --- a/include/cru/ui/ClickDetector.hpp +++ b/include/cru/ui/ClickDetector.hpp @@ -36,7 +36,7 @@ enum class ClickState { }; class ClickDetector : public Object { - CRU_DEFINE_CLASS_LOG_TAG("cru::ui::ClickDetector") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::ClickDetector") public: explicit ClickDetector(Control* control); diff --git a/include/cru/ui/Control.hpp b/include/cru/ui/Control.hpp index 347163be..bd86bc2f 100644 --- a/include/cru/ui/Control.hpp +++ b/include/cru/ui/Control.hpp @@ -22,7 +22,7 @@ class Control : public Object { ~Control() override = default; public: - virtual std::string_view GetControlType() const = 0; + virtual std::u16string_view GetControlType() const = 0; //*************** region: tree *************** public: diff --git a/include/cru/ui/UiEvent.hpp b/include/cru/ui/UiEvent.hpp index 39f26aee..5adace8a 100644 --- a/include/cru/ui/UiEvent.hpp +++ b/include/cru/ui/UiEvent.hpp @@ -212,7 +212,7 @@ class KeyEventArgs : public UiEventArgs { class CharEventArgs : public UiEventArgs { public: - CharEventArgs(Object* sender, Object* original_sender, std::string c) + CharEventArgs(Object* sender, Object* original_sender, std::u16string c) : UiEventArgs(sender, original_sender), c_(std::move(c)) {} CharEventArgs(const CharEventArgs& other) = default; CharEventArgs(CharEventArgs&& other) = default; @@ -220,9 +220,9 @@ class CharEventArgs : public UiEventArgs { CharEventArgs& operator=(CharEventArgs&& other) = default; ~CharEventArgs() override = default; - std::string GetChar() const { return c_; } + std::u16string GetChar() const { return c_; } private: - std::string c_; + std::u16string c_; }; } // namespace cru::ui::event diff --git a/include/cru/ui/UiHost.hpp b/include/cru/ui/UiHost.hpp index 1a5c6302..b1658ef6 100644 --- a/include/cru/ui/UiHost.hpp +++ b/include/cru/ui/UiHost.hpp @@ -32,7 +32,7 @@ struct AfterLayoutEventArgs {}; // 4. Delete Window when deleting_ is false and IsRetainAfterDestroy is false in // OnNativeDestroy. class UiHost : public Object, public SelfResolvable<UiHost> { - CRU_DEFINE_CLASS_LOG_TAG("cru::ui::UiHost") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::UiHost") public: // This will create root window render object and attach it to window. diff --git a/include/cru/ui/Window.hpp b/include/cru/ui/Window.hpp index eb2ecfbb..450ea97b 100644 --- a/include/cru/ui/Window.hpp +++ b/include/cru/ui/Window.hpp @@ -6,7 +6,7 @@ class Window final : public ContentControl { friend UiHost; public: - static constexpr std::string_view control_type = "Window"; + static constexpr std::u16string_view control_type = u"Window"; public: static Window* CreateOverlapped(); @@ -24,7 +24,7 @@ class Window final : public ContentControl { ~Window() override; public: - std::string_view GetControlType() const final; + std::u16string_view GetControlType() const final; render::RenderObject* GetRenderObject() const override; diff --git a/include/cru/ui/controls/Button.hpp b/include/cru/ui/controls/Button.hpp index 8a11409c..a4f727d6 100644 --- a/include/cru/ui/controls/Button.hpp +++ b/include/cru/ui/controls/Button.hpp @@ -7,7 +7,7 @@ namespace cru::ui::controls { class Button : public ContentControl { public: - static constexpr std::string_view control_type = "Button"; + static constexpr std::u16string_view control_type = u"Button"; static Button* Create() { return new Button(); } @@ -21,7 +21,7 @@ class Button : public ContentControl { Button& operator=(Button&& other) = delete; ~Button() override; - std::string_view GetControlType() const final { return control_type; } + std::u16string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; diff --git a/include/cru/ui/controls/Container.hpp b/include/cru/ui/controls/Container.hpp index e3d78365..304d402c 100644 --- a/include/cru/ui/controls/Container.hpp +++ b/include/cru/ui/controls/Container.hpp @@ -3,7 +3,7 @@ namespace cru::ui::controls { class Container : public ContentControl { - static constexpr std::string_view control_type = "Container"; + static constexpr std::u16string_view control_type = u"Container"; protected: Container(); @@ -15,7 +15,7 @@ class Container : public ContentControl { ~Container() override; public: - std::string_view GetControlType() const final { return control_type; } + std::u16string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; diff --git a/include/cru/ui/controls/FlexLayout.hpp b/include/cru/ui/controls/FlexLayout.hpp index 3d6087c2..87162569 100644 --- a/include/cru/ui/controls/FlexLayout.hpp +++ b/include/cru/ui/controls/FlexLayout.hpp @@ -4,7 +4,7 @@ namespace cru::ui::controls { class FlexLayout : public LayoutControl { public: - static constexpr std::string_view control_type = "FlexLayout"; + static constexpr std::u16string_view control_type = u"FlexLayout"; static FlexLayout* Create() { return new FlexLayout(); } @@ -18,7 +18,7 @@ class FlexLayout : public LayoutControl { FlexLayout& operator=(FlexLayout&& other) = delete; ~FlexLayout() override; - std::string_view GetControlType() const final { return control_type; } + std::u16string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; diff --git a/include/cru/ui/controls/StackLayout.hpp b/include/cru/ui/controls/StackLayout.hpp index d5998cc4..c0b95044 100644 --- a/include/cru/ui/controls/StackLayout.hpp +++ b/include/cru/ui/controls/StackLayout.hpp @@ -4,7 +4,7 @@ namespace cru::ui::controls { class StackLayout : public LayoutControl { public: - static constexpr std::string_view control_type = "StackLayout"; + static constexpr std::u16string_view control_type = u"StackLayout"; static StackLayout* Create() { return new StackLayout(); } @@ -17,7 +17,7 @@ class StackLayout : public LayoutControl { ~StackLayout() override; - std::string_view GetControlType() const final { return control_type; } + std::u16string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; diff --git a/include/cru/ui/controls/TextBlock.hpp b/include/cru/ui/controls/TextBlock.hpp index 1b1b4a5c..8a9a3bff 100644 --- a/include/cru/ui/controls/TextBlock.hpp +++ b/include/cru/ui/controls/TextBlock.hpp @@ -7,7 +7,7 @@ class TextControlService; class TextBlock : public NoChildControl { public: - static constexpr std::string_view control_type = "TextBlock"; + static constexpr std::u16string_view control_type = u"TextBlock"; static TextBlock* Create() { return new TextBlock(); } @@ -21,12 +21,12 @@ class TextBlock : public NoChildControl { TextBlock& operator=(TextBlock&& other) = delete; ~TextBlock() override; - std::string_view GetControlType() const final { return control_type; } + std::u16string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; - std::string GetText() const; - void SetText(std::string text); + std::u16string GetText() const; + void SetText(std::u16string text); gsl::not_null<render::TextRenderObject*> GetTextRenderObject(); render::ScrollRenderObject* GetScrollRenderObject() { return nullptr; } diff --git a/include/cru/ui/controls/TextBox.hpp b/include/cru/ui/controls/TextBox.hpp index 3d4de7c0..5976f6da 100644 --- a/include/cru/ui/controls/TextBox.hpp +++ b/include/cru/ui/controls/TextBox.hpp @@ -10,7 +10,7 @@ class TextControlService; class TextBox : public NoChildControl { public: - static constexpr std::string_view control_type = "TextBox"; + static constexpr std::u16string_view control_type = u"TextBox"; static TextBox* Create() { return new TextBox(); } @@ -23,7 +23,7 @@ class TextBox : public NoChildControl { ~TextBox() override; - std::string_view GetControlType() const final { return control_type; } + std::u16string_view GetControlType() const final { return control_type; } render::RenderObject* GetRenderObject() const override; diff --git a/include/cru/ui/render/BorderRenderObject.hpp b/include/cru/ui/render/BorderRenderObject.hpp index 94e888d4..587f051a 100644 --- a/include/cru/ui/render/BorderRenderObject.hpp +++ b/include/cru/ui/render/BorderRenderObject.hpp @@ -3,7 +3,7 @@ namespace cru::ui::render { class BorderRenderObject : public RenderObject { - CRU_DEFINE_CLASS_LOG_TAG("cru::ui::render::BorderRenderObject") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::BorderRenderObject") public: BorderRenderObject(); diff --git a/include/cru/ui/render/FlexLayoutRenderObject.hpp b/include/cru/ui/render/FlexLayoutRenderObject.hpp index 87a41c7e..ee29d1e4 100644 --- a/include/cru/ui/render/FlexLayoutRenderObject.hpp +++ b/include/cru/ui/render/FlexLayoutRenderObject.hpp @@ -74,7 +74,7 @@ namespace cru::ui::render { // and just fill the rest space with blank. // class FlexLayoutRenderObject : public LayoutRenderObject<FlexChildLayoutData> { - CRU_DEFINE_CLASS_LOG_TAG("cru::ui::render::FlexLayoutRenderObject") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::FlexLayoutRenderObject") public: FlexLayoutRenderObject() = default; diff --git a/include/cru/ui/render/LayoutHelper.hpp b/include/cru/ui/render/LayoutHelper.hpp index 3469ccf0..518dc5a3 100644 --- a/include/cru/ui/render/LayoutHelper.hpp +++ b/include/cru/ui/render/LayoutHelper.hpp @@ -9,6 +9,6 @@ float CalculateAnchorByAlignment(Alignment alignment, float start_point, MeasureLength StackLayoutCalculateChildMaxLength( MeasureLength parent_preferred_size, MeasureLength parent_max_size, - MeasureLength child_min_size, std::string_view log_tag, - std::string_view exceeds_message); + MeasureLength child_min_size, std::u16string_view log_tag, + std::u16string_view exceeds_message); } // namespace cru::ui::render diff --git a/include/cru/ui/render/RenderObject.hpp b/include/cru/ui/render/RenderObject.hpp index 2e784afc..f820f029 100644 --- a/include/cru/ui/render/RenderObject.hpp +++ b/include/cru/ui/render/RenderObject.hpp @@ -37,7 +37,7 @@ namespace cru::ui::render { class RenderObject : public Object { friend WindowRenderObject; - CRU_DEFINE_CLASS_LOG_TAG("cru::ui::render::RenderObject") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::RenderObject") protected: enum class ChildMode { diff --git a/include/cru/ui/render/StackLayoutRenderObject.hpp b/include/cru/ui/render/StackLayoutRenderObject.hpp index 534d7f22..303241c5 100644 --- a/include/cru/ui/render/StackLayoutRenderObject.hpp +++ b/include/cru/ui/render/StackLayoutRenderObject.hpp @@ -23,7 +23,7 @@ namespace cru::ui::render { // to min size. class StackLayoutRenderObject : public LayoutRenderObject<StackChildLayoutData> { - CRU_DEFINE_CLASS_LOG_TAG("cru::ui::render:StackLayoutRenderObject") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render:StackLayoutRenderObject") public: StackLayoutRenderObject() = default; diff --git a/include/cru/ui/render/TextRenderObject.hpp b/include/cru/ui/render/TextRenderObject.hpp index 77a92b4f..32d96797 100644 --- a/include/cru/ui/render/TextRenderObject.hpp +++ b/include/cru/ui/render/TextRenderObject.hpp @@ -18,7 +18,7 @@ namespace cru::ui::render { // If the result layout box is bigger than actual text box, then text is center // aligned. class TextRenderObject : public RenderObject { - CRU_DEFINE_CLASS_LOG_TAG("cru::ui::render::TextRenderObject") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::TextRenderObject") public: constexpr static float default_caret_width = 2; @@ -34,8 +34,8 @@ class TextRenderObject : public RenderObject { TextRenderObject& operator=(TextRenderObject&& other) = delete; ~TextRenderObject() override; - std::string GetText() const; - void SetText(std::string new_text); + std::u16string GetText() const; + void SetText(std::u16string new_text); std::shared_ptr<platform::graph::IBrush> GetBrush() const { return brush_; } void SetBrush(std::shared_ptr<platform::graph::IBrush> new_brush); diff --git a/include/cru/win/Exception.hpp b/include/cru/win/Exception.hpp index 234aea69..3a95aa5d 100644 --- a/include/cru/win/Exception.hpp +++ b/include/cru/win/Exception.hpp @@ -10,7 +10,7 @@ namespace cru::platform::win { class HResultError : public platform::PlatformException { public: explicit HResultError(HRESULT h_result); - explicit HResultError(HRESULT h_result, const std::string_view& message); + explicit HResultError(HRESULT h_result, std::string_view message); CRU_DEFAULT_COPY(HResultError) CRU_DEFAULT_MOVE(HResultError) @@ -27,8 +27,7 @@ inline void ThrowIfFailed(const HRESULT h_result) { if (FAILED(h_result)) throw HResultError(h_result); } -inline void ThrowIfFailed(const HRESULT h_result, - const std::string_view& message) { +inline void ThrowIfFailed(const HRESULT h_result, std::string_view message) { if (FAILED(h_result)) throw HResultError(h_result, message); } @@ -36,8 +35,8 @@ class Win32Error : public platform::PlatformException { public: // ::GetLastError is automatically called to get the error code. // The same as Win32Error(::GetLastError(), message) - explicit Win32Error(const std::string_view& message); - Win32Error(DWORD error_code, const std::string_view& message); + explicit Win32Error(std::string_view message); + Win32Error(DWORD error_code, std::string_view message); CRU_DEFAULT_COPY(Win32Error) CRU_DEFAULT_MOVE(Win32Error) diff --git a/include/cru/win/String.hpp b/include/cru/win/String.hpp deleted file mode 100644 index ac07f57b..00000000 --- a/include/cru/win/String.hpp +++ /dev/null @@ -1,75 +0,0 @@ -/* -Because the text encoding problem on Windows, here I write some functions -related to text encoding. The utf-8 and utf-16 conversion function is provided -by win32 api. However win32 api does not provide any function about charactor -iteration or index by code point. (At least I haven't found.) I don't use icu -because it is not easy to build it on Windows and the bundled version in Windows -(https://docs.microsoft.com/en-us/windows/win32/intl/international-components-for-unicode--icu-) -is only available after Windows 10 Creators Update. - -Luckily, both utf-8 and utf-16 encoding are easy to learn and program with if we -only do simple iteration rather than do much sophisticated work about -complicated error situations. (And I learn the internal of the encoding by the -way.) -*/ - -#pragma once -#include "WinPreConfig.hpp" - -#include "cru/common/StringUtil.hpp" - -#include <cstdint> -#include <stdexcept> -#include <string> -#include <string_view> - -namespace cru::platform::win { -std::string ToUtf8String(const std::wstring_view& string); -std::wstring ToUtf16String(const std::string_view& string); - -inline bool IsSurrogatePair(wchar_t c) { return c >= 0xD800 && c <= 0xDFFF; } - -inline bool IsSurrogatePairLeading(wchar_t c) { - return c >= 0xD800 && c <= 0xDBFF; -} - -inline bool IsSurrogatePairTrailing(wchar_t c) { - return c >= 0xDC00 && c <= 0xDFFF; -} - -class Utf16Iterator : public Object { - static_assert( - sizeof(wchar_t) == 2, - "Emmm, according to my knowledge, wchar_t should be 2-length on " - "Windows. If not, Utf16 will be broken."); - - public: - Utf16Iterator(const std::wstring_view& string) : string_(string) {} - - CRU_DEFAULT_COPY(Utf16Iterator) - CRU_DEFAULT_MOVE(Utf16Iterator) - - ~Utf16Iterator() = default; - - public: - void SetToHead() { position_ = 0; } - - // Advance current position and get next code point. Return k_code_point_end - // if there is no next code unit(point). Throw TextEncodeException if decoding - // fails. - CodePoint Next(); - - Index CurrentPosition() const { return this->position_; } - - private: - std::wstring_view string_; - Index position_ = 0; -}; - -Index IndexUtf8ToUtf16(const std::string_view& utf8_string, Index utf8_index, - const std::wstring_view& utf16_string); - -Index IndexUtf16ToUtf8(const std::wstring_view& utf16_string, Index utf16_index, - const std::string_view& utf8_string); - -} // namespace cru::platform::win diff --git a/include/cru/win/graph/direct/Factory.hpp b/include/cru/win/graph/direct/Factory.hpp index 763d4b2b..e70454f5 100644 --- a/include/cru/win/graph/direct/Factory.hpp +++ b/include/cru/win/graph/direct/Factory.hpp @@ -38,11 +38,11 @@ class DirectGraphFactory : public DirectResource, public virtual IGraphFactory { std::unique_ptr<IGeometryBuilder> CreateGeometryBuilder() override; - std::unique_ptr<IFont> CreateFont(const std::string_view& font_family, + std::unique_ptr<IFont> CreateFont(std::u16string font_family, float font_size) override; std::unique_ptr<ITextLayout> CreateTextLayout(std::shared_ptr<IFont> font, - std::string text) override; + std::u16string text) override; private: Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_; diff --git a/include/cru/win/graph/direct/Font.hpp b/include/cru/win/graph/direct/Font.hpp index ecf9fd81..2195f3e4 100644 --- a/include/cru/win/graph/direct/Font.hpp +++ b/include/cru/win/graph/direct/Font.hpp @@ -11,7 +11,7 @@ class DWriteFont : public DirectGraphResource, public virtual IFont, public virtual IComResource<IDWriteTextFormat> { public: - DWriteFont(DirectGraphFactory* factory, const std::string_view& font_family, + DWriteFont(DirectGraphFactory* factory, std::u16string font_family, float font_size); CRU_DELETE_COPY(DWriteFont) @@ -27,6 +27,7 @@ class DWriteFont : public DirectGraphResource, float GetFontSize() override; private: + std::u16string font_family_; Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; }; } // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/Resource.hpp b/include/cru/win/graph/direct/Resource.hpp index d0a30dbd..6162ebd8 100644 --- a/include/cru/win/graph/direct/Resource.hpp +++ b/include/cru/win/graph/direct/Resource.hpp @@ -10,7 +10,7 @@ class DirectGraphFactory; class DirectResource : public Object, public virtual INativeResource { public: - static constexpr std::string_view k_platform_id = "Windows Direct"; + static constexpr std::u16string_view k_platform_id = u"Windows Direct"; protected: DirectResource() = default; @@ -22,7 +22,7 @@ class DirectResource : public Object, public virtual INativeResource { ~DirectResource() override = default; public: - std::string_view GetPlatformId() const final { return k_platform_id; } + std::u16string_view GetPlatformId() const final { return k_platform_id; } }; class DirectGraphResource : public DirectResource, diff --git a/include/cru/win/graph/direct/TextLayout.hpp b/include/cru/win/graph/direct/TextLayout.hpp index 40c63dbe..c53cf655 100644 --- a/include/cru/win/graph/direct/TextLayout.hpp +++ b/include/cru/win/graph/direct/TextLayout.hpp @@ -15,7 +15,7 @@ class DWriteTextLayout : public DirectGraphResource, public virtual IComResource<IDWriteTextLayout> { public: DWriteTextLayout(DirectGraphFactory* factory, std::shared_ptr<IFont> font, - std::string text); + std::u16string text); CRU_DELETE_COPY(DWriteTextLayout) CRU_DELETE_MOVE(DWriteTextLayout) @@ -28,8 +28,8 @@ class DWriteTextLayout : public DirectGraphResource, } public: - std::string GetText() override; - void SetText(std::string new_text) override; + std::u16string GetText() override; + void SetText(std::u16string new_text) override; std::shared_ptr<IFont> GetFont() override; void SetFont(std::shared_ptr<IFont> font) override; @@ -41,12 +41,11 @@ class DWriteTextLayout : public DirectGraphResource, // Return empty vector if text_range.count is 0. Text range could be in // reverse direction, it should be normalized first in implementation. std::vector<Rect> TextRangeRect(const TextRange& text_range) override; - Point TextSinglePoint(gsl::index position, bool trailing) override; + Point TextSinglePoint(Index position, bool trailing) override; TextHitTestResult HitTest(const Point& point) override; private: - std::string text_; - std::wstring w_text_; + std::u16string text_; std::shared_ptr<DWriteFont> font_; float max_width_ = std::numeric_limits<float>::max(); float max_height_ = std::numeric_limits<float>::max(); diff --git a/include/cru/win/native/Cursor.hpp b/include/cru/win/native/Cursor.hpp index 44a6a362..373b9170 100644 --- a/include/cru/win/native/Cursor.hpp +++ b/include/cru/win/native/Cursor.hpp @@ -7,7 +7,7 @@ namespace cru::platform::native::win { class WinCursor : public WinNativeResource, public virtual ICursor { - CRU_DEFINE_CLASS_LOG_TAG("cru::platform::native::win::WinCursor") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::WinCursor") public: WinCursor(HCURSOR handle, bool auto_destroy); diff --git a/include/cru/win/native/GodWindow.hpp b/include/cru/win/native/GodWindow.hpp index 0820bdb3..8b20e01f 100644 --- a/include/cru/win/native/GodWindow.hpp +++ b/include/cru/win/native/GodWindow.hpp @@ -5,7 +5,7 @@ namespace cru::platform::native::win { class GodWindow : public Object { - CRU_DEFINE_CLASS_LOG_TAG("cru::platform::native::win::GodWindow") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::GodWindow") public: explicit GodWindow(WinUiApplication* application); diff --git a/include/cru/win/native/InputMethod.hpp b/include/cru/win/native/InputMethod.hpp index 45422ace..113f460d 100644 --- a/include/cru/win/native/InputMethod.hpp +++ b/include/cru/win/native/InputMethod.hpp @@ -12,7 +12,7 @@ namespace cru::platform::native::win { class AutoHIMC : public Object { - CRU_DEFINE_CLASS_LOG_TAG("cru::platform::native::win::AutoHIMC") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::AutoHIMC") public: explicit AutoHIMC(HWND hwnd); @@ -35,7 +35,7 @@ class AutoHIMC : public Object { class WinInputMethodContext : public WinNativeResource, public virtual IInputMethodContext { - CRU_DEFINE_CLASS_LOG_TAG("cru::platform::native::win::WinInputMethodContext") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::WinInputMethodContext") public: WinInputMethodContext(gsl::not_null<WinNativeWindow*> window); @@ -65,12 +65,12 @@ class WinInputMethodContext : public WinNativeResource, IEvent<std::nullptr_t>* CompositionEvent() override; - IEvent<std::string_view>* TextEvent() override; + IEvent<std::u16string_view>* TextEvent() override; private: void OnWindowNativeMessage(WindowNativeMessageEventArgs& args); - std::string GetResultString(); + std::u16string GetResultString(); std::optional<AutoHIMC> TryGetHIMC(); @@ -82,7 +82,7 @@ class WinInputMethodContext : public WinNativeResource, Event<std::nullptr_t> composition_start_event_; Event<std::nullptr_t> composition_end_event_; Event<std::nullptr_t> composition_event_; - Event<std::string_view> text_event_; + Event<std::u16string_view> text_event_; }; class WinInputMethodManager : public WinNativeResource, diff --git a/include/cru/win/native/Resource.hpp b/include/cru/win/native/Resource.hpp index 7afaca0f..0de0e1a8 100644 --- a/include/cru/win/native/Resource.hpp +++ b/include/cru/win/native/Resource.hpp @@ -6,7 +6,7 @@ namespace cru::platform::native::win { class WinNativeResource : public Object, public virtual INativeResource { public: - static constexpr std::string_view k_platform_id = "Windows"; + static constexpr std::u16string_view k_platform_id = u"Windows"; protected: WinNativeResource() = default; @@ -18,6 +18,6 @@ class WinNativeResource : public Object, public virtual INativeResource { ~WinNativeResource() override = default; public: - std::string_view GetPlatformId() const final { return k_platform_id; } + std::u16string_view GetPlatformId() const final { return k_platform_id; } }; } // namespace cru::platform::native::win diff --git a/include/cru/win/native/Window.hpp b/include/cru/win/native/Window.hpp index 521a0a06..3e0b11cd 100644 --- a/include/cru/win/native/Window.hpp +++ b/include/cru/win/native/Window.hpp @@ -8,7 +8,7 @@ namespace cru::platform::native::win { class WinNativeWindow : public WinNativeResource, public virtual INativeWindow { - CRU_DEFINE_CLASS_LOG_TAG("cru::platform::native::win::WinNativeWindow") + CRU_DEFINE_CLASS_LOG_TAG(u"cru::platform::native::win::WinNativeWindow") public: WinNativeWindow(WinUiApplication* application, WindowClass* window_class, |