diff options
-rw-r--r-- | CMakeSettings.json | 14 | ||||
-rw-r--r-- | include/cru/platform/graph_factory.hpp | 7 | ||||
-rw-r--r-- | include/cru/platform/text_layout.hpp | 3 | ||||
-rw-r--r-- | include/cru/platform/win/win_text_layout.hpp | 18 | ||||
-rw-r--r-- | src/platform_win/win_text_layout.cpp | 71 | ||||
-rw-r--r-- | src/ui/render/border_render_object.cpp | 2 |
6 files changed, 108 insertions, 7 deletions
diff --git a/CMakeSettings.json b/CMakeSettings.json index ce7531e0..4931d795 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -2,31 +2,35 @@ "configurations": [ { "name": "x86-Debug", - "generator": "Visual Studio 15 2017", + "generator": "Ninja", + "inheritEnvironments": [ "msvc_x86_x64" ], "configurationType": "Debug", "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}" }, { "name": "x86-Realse", - "generator": "Visual Studio 15 2017", + "generator": "Ninja", + "inheritEnvironments": [ "msvc_x86_x64" ], "configurationType": "Release", "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}" }, { "name": "x64-Debug", - "generator": "Visual Studio 15 2017 Win64", + "generator": "Ninja", + "inheritEnvironments": [ "msvc_x64" ], "configurationType": "Debug", "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}" }, { "name": "x64-Release", - "generator": "Visual Studio 15 2017 Win64", + "generator": "Ninja", + "inheritEnvironments": [ "msvc_x64" ], "configurationType": "Release", "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}" } ] -}
\ No newline at end of file +} diff --git a/include/cru/platform/graph_factory.hpp b/include/cru/platform/graph_factory.hpp index f2e5f286..312dc0ea 100644 --- a/include/cru/platform/graph_factory.hpp +++ b/include/cru/platform/graph_factory.hpp @@ -3,12 +3,19 @@ #include "cru/common/ui_base.hpp" +#include <string> +#include <string_view> + namespace cru::platform { struct SolidColorBrush; struct GeometryBuilder; +struct FontDescriptor; +struct TextLayout; struct GraphFactory : virtual Interface { virtual SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; virtual GeometryBuilder* CreateGeometryBuilder() = 0; + virtual FontDescriptor* CreateFontDescriptor(const std::wstring_view& font_family) = 0; + virtual TextLayout* CreateTextLayout(std::shared_ptr<FontDescriptor> font, std::wstring text) = 0; }; } // namespace cru::platform diff --git a/include/cru/platform/text_layout.hpp b/include/cru/platform/text_layout.hpp index fce6ce5f..7bcf01fe 100644 --- a/include/cru/platform/text_layout.hpp +++ b/include/cru/platform/text_layout.hpp @@ -4,9 +4,12 @@ #include "cru/common/ui_base.hpp" #include <vector> +#include <string> namespace cru::platform { struct TextLayout : virtual Interface { + virtual std::wstring GetText() = 0; + virtual void SetText(std::wstring new_text) = 0; virtual void SetMaxWidth(float max_width) = 0; virtual void SetMaxHeight(float max_height) = 0; virtual ui::Rect GetTextBounds() = 0; diff --git a/include/cru/platform/win/win_text_layout.hpp b/include/cru/platform/win/win_text_layout.hpp index 68961dd7..277bbcae 100644 --- a/include/cru/platform/win/win_text_layout.hpp +++ b/include/cru/platform/win/win_text_layout.hpp @@ -7,18 +7,34 @@ #include <memory> namespace cru::platform::win { +class GraphManager; + class WinTextLayout : public Object, public virtual TextLayout { public: - explicit WinTextLayout(std::shared_ptr<WinFontDescriptor> font); + explicit WinTextLayout(GraphManager* graph_manager, + std::shared_ptr<WinFontDescriptor> font, std::wstring text); WinTextLayout(const WinTextLayout& other) = delete; WinTextLayout(WinTextLayout&& other) = delete; WinTextLayout& operator=(const WinTextLayout& other) = delete; WinTextLayout& operator=(WinTextLayout&& other) = delete; ~WinTextLayout() override; + std::wstring GetText() override; + void SetText(std::wstring new_text) override; + void SetMaxWidth(float max_width) override; + void SetMaxHeight(float max_height) override; + ui::Rect GetTextBounds() override; + std::vector<ui::Rect> TextRangeRect( + const ui::TextRange& text_range) override; + IDWriteTextLayout* GetDWriteTextLayout() const { return text_layout_.Get(); } private: + GraphManager* graph_manager_; + std::wstring text_; + std::shared_ptr<WinFontDescriptor> font_descriptor_; + float max_width_ = 0.0f; + float max_height_ = 0.0f; Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; }; } // namespace cru::platform::win diff --git a/src/platform_win/win_text_layout.cpp b/src/platform_win/win_text_layout.cpp index 9ab70d2a..9915b56c 100644 --- a/src/platform_win/win_text_layout.cpp +++ b/src/platform_win/win_text_layout.cpp @@ -1,5 +1,76 @@ #include "cru/platform/win/win_text_layout.hpp" +#include "cru/platform/win/exception.hpp" +#include "cru/platform/win/graph_manager.hpp" + +#include <cassert> +#include <utility> + namespace cru::platform::win { +WinTextLayout::WinTextLayout(GraphManager* graph_manager, + std::shared_ptr<WinFontDescriptor> font, + std::wstring text) { + assert(graph_manager); + assert(font); + graph_manager_ = graph_manager; + text_.swap(text); + font_descriptor_.swap(font); + + ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( + text_.c_str(), text_.size(), font_descriptor_->GetDWriteTextFormat(), + max_width_, max_height_, &text_layout_)); +} + +std::wstring WinTextLayout::GetText() { return text_; } + +void WinTextLayout::SetText(std::wstring new_text) { + text_.swap(new_text); + ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout( + text_.c_str(), text_.size(), font_descriptor_->GetDWriteTextFormat(), + max_width_, max_height_, &text_layout_)); +} + +void WinTextLayout::SetMaxWidth(float max_width) { + max_width_ = max_width; + ThrowIfFailed(text_layout_->SetMaxWidth(max_width_)); +} + +void WinTextLayout::SetMaxHeight(float max_height) { + max_height_ = max_height; + ThrowIfFailed(text_layout_->SetMaxHeight(max_height_)); +} + +ui::Rect WinTextLayout::GetTextBounds() { + DWRITE_TEXT_METRICS metrics; + ThrowIfFailed(text_layout_->GetMetrics(&metrics)); + return ui::Rect{metrics.left, metrics.top, metrics.width, metrics.height}; +} + +std::vector<ui::Rect> WinTextLayout::TextRangeRect( + const ui::TextRange& text_range) { + DWRITE_TEXT_METRICS text_metrics; + ThrowIfFailed(text_layout_->GetMetrics(&text_metrics)); + const auto metrics_count = + text_metrics.lineCount * text_metrics.maxBidiReorderingDepth; + + std::vector<DWRITE_HIT_TEST_METRICS> hit_test_metrics(metrics_count); + UINT32 actual_count; + text_layout_->HitTestTextRange(text_range.position, text_range.count, 0, 0, + hit_test_metrics.data(), metrics_count, + &actual_count); + + hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, + hit_test_metrics.cend()); + + std::vector<ui::Rect> result; + result.reserve(actual_count); + + for (const auto& metrics : hit_test_metrics) { + result.push_back(ui::Rect{metrics.left, metrics.top, + metrics.left + metrics.width, + metrics.top + metrics.height}); + } + return result; } +} // namespace cru::platform::win diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp index cff219f7..93e87c90 100644 --- a/src/ui/render/border_render_object.cpp +++ b/src/ui/render/border_render_object.cpp @@ -1,4 +1,4 @@ -#include "cru/ui/render/border_render_object.hpp" + #include "cru/ui/render/border_render_object.hpp" #include "cru/platform/debug.hpp" #include "cru/platform/geometry.hpp" |