From baa7cf141b8121473edceae16c1a20a6d47bd202 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 27 Jun 2019 00:18:48 +0800 Subject: ...... --- src/CMakeLists.txt | 5 +- src/win/graph/CMakeLists.txt | 21 +----- src/win/graph/direct/CMakeLists.txt | 24 +++++++ src/win/graph/direct/brush.cpp | 19 ++++++ src/win/graph/direct/exception.cpp | 29 +++++++++ src/win/graph/direct/font.cpp | 29 +++++++++ src/win/graph/direct/geometry.cpp | 65 +++++++++++++++++++ src/win/graph/direct/graph_factory.cpp | 113 +++++++++++++++++++++++++++++++++ src/win/graph/direct/painter.cpp | 93 +++++++++++++++++++++++++++ src/win/graph/direct/text_layout.cpp | 94 +++++++++++++++++++++++++++ src/win/graph/win_brush.cpp | 23 ------- src/win/graph/win_font.cpp | 29 --------- src/win/graph/win_geometry.cpp | 65 ------------------- src/win/graph/win_graph_factory.cpp | 111 -------------------------------- src/win/graph/win_painter.cpp | 93 --------------------------- src/win/graph/win_text_layout.cpp | 94 --------------------------- 16 files changed, 469 insertions(+), 438 deletions(-) create mode 100644 src/win/graph/direct/CMakeLists.txt create mode 100644 src/win/graph/direct/brush.cpp create mode 100644 src/win/graph/direct/exception.cpp create mode 100644 src/win/graph/direct/font.cpp create mode 100644 src/win/graph/direct/geometry.cpp create mode 100644 src/win/graph/direct/graph_factory.cpp create mode 100644 src/win/graph/direct/painter.cpp create mode 100644 src/win/graph/direct/text_layout.cpp delete mode 100644 src/win/graph/win_brush.cpp delete mode 100644 src/win/graph/win_font.cpp delete mode 100644 src/win/graph/win_geometry.cpp delete mode 100644 src/win/graph/win_graph_factory.cpp delete mode 100644 src/win/graph/win_painter.cpp delete mode 100644 src/win/graph/win_text_layout.cpp (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 16e0c10f..f0010345 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,14 +1,11 @@ set(CRU_BASE_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/common) add_library(cru_base INTERFACE) target_sources(cru_base INTERFACE - ${CRU_BASE_INCLUDE_DIR}/auto_delete.hpp ${CRU_BASE_INCLUDE_DIR}/base.hpp - ${CRU_BASE_INCLUDE_DIR}/endable.hpp ${CRU_BASE_INCLUDE_DIR}/event.hpp ${CRU_BASE_INCLUDE_DIR}/format.hpp ${CRU_BASE_INCLUDE_DIR}/pre_config.hpp ${CRU_BASE_INCLUDE_DIR}/self_resolvable.hpp - ${CRU_BASE_INCLUDE_DIR}/ui_base.hpp ) target_include_directories(cru_base INTERFACE ${CRU_INCLUDE_DIR}) @@ -17,7 +14,9 @@ add_library(cru_platform_base INTERFACE) target_sources(cru_platform_base INTERFACE ${CRU_PLATFORM_BASE_INCLUDE_DIR}/debug.hpp ${CRU_PLATFORM_BASE_INCLUDE_DIR}/exception.hpp + ${CRU_PLATFORM_BASE_INCLUDE_DIR}/graphic_base.hpp ${CRU_PLATFORM_BASE_INCLUDE_DIR}/matrix.hpp + ${CRU_PLATFORM_BASE_INCLUDE_DIR}/native_resource.hpp ${CRU_PLATFORM_BASE_INCLUDE_DIR}/string_util.hpp ) target_link_libraries(cru_platform_base INTERFACE cru_base) diff --git a/src/win/graph/CMakeLists.txt b/src/win/graph/CMakeLists.txt index 03cd4566..c90537ac 100644 --- a/src/win/graph/CMakeLists.txt +++ b/src/win/graph/CMakeLists.txt @@ -1,20 +1 @@ -set(CRU_WIN_GRAPH_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/win/graph) - -add_library(cru_win_graph STATIC - win_brush.cpp - win_font.cpp - win_geometry.cpp - win_graph_factory.cpp - win_painter.cpp - win_text_layout.cpp -) -target_sources(cru_win_graph PUBLIC - ${CRU_WIN_GRAPH_INCLUDE_DIR}/win_brush.hpp - ${CRU_WIN_GRAPH_INCLUDE_DIR}/win_font.hpp - ${CRU_WIN_GRAPH_INCLUDE_DIR}/win_geometry.hpp - ${CRU_WIN_GRAPH_INCLUDE_DIR}/win_graph_factory.hpp - ${CRU_WIN_GRAPH_INCLUDE_DIR}/win_native_factory.hpp - ${CRU_WIN_GRAPH_INCLUDE_DIR}/win_painter.hpp - ${CRU_WIN_GRAPH_INCLUDE_DIR}/win_text_layout.hpp -) -target_link_libraries(cru_win_graph PUBLIC D3D11 D2d1 DWrite cru_win_base) +add_subdirectory(direct) diff --git a/src/win/graph/direct/CMakeLists.txt b/src/win/graph/direct/CMakeLists.txt new file mode 100644 index 00000000..7a04a778 --- /dev/null +++ b/src/win/graph/direct/CMakeLists.txt @@ -0,0 +1,24 @@ +set(CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/win/graph/direct) + +add_library(cru_win_graph_direct STATIC + brush.cpp + exception.cpp + font.cpp + geometry.cpp + graph_factory.cpp + painter.cpp + text_layout.cpp +) +target_sources(cru_win_graph_direct PUBLIC + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/brush.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/com_resource.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/convert_util.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/direct_factory.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/exception.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/font.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/geometry.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/graph_factory.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/painter.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/text_layout.hpp +) +target_link_libraries(cru_win_graph_direct PUBLIC D3D11 D2d1 DWrite cru_win_base) diff --git a/src/win/graph/direct/brush.cpp b/src/win/graph/direct/brush.cpp new file mode 100644 index 00000000..7ddd14b4 --- /dev/null +++ b/src/win/graph/direct/brush.cpp @@ -0,0 +1,19 @@ +#include "cru/win/graph/direct/brush.hpp" + +#include "cru/win/graph/direct/convert_util.hpp" +#include "cru/win/graph/direct/direct_factory.hpp" +#include "cru/win/graph/direct/exception.hpp" + +#include + +namespace cru::platform::graph::win::direct { +D2DSolidColorBrush::D2DSolidColorBrush(IDirectFactory* factory) { + assert(factory); + ThrowIfFailed(factory->GetD2D1DeviceContext()->CreateSolidColorBrush( + Convert(color_), &brush_)); +} + +void D2DSolidColorBrush::OnSetColor(const Color& color) { + brush_->SetColor(Convert(color)); +} +} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/exception.cpp b/src/win/graph/direct/exception.cpp new file mode 100644 index 00000000..23c267c1 --- /dev/null +++ b/src/win/graph/direct/exception.cpp @@ -0,0 +1,29 @@ +#include "cru/win/graph/direct/exception.hpp" + +#include "cru/common/format.hpp" + +namespace cru::platform::graph::win::direct { +using util::Format; + +inline std::string HResultMakeMessage(HRESULT h_result, + const std::string_view* message) { + char buffer[10]; + sprintf_s(buffer, "%#08x", h_result); + + if (message) + return Format( + "An HResultError is thrown. HRESULT: {}.\nAdditional message: {}\n", + buffer, *message); + else + return Format("An HResultError is thrown. HRESULT: {}.\n", buffer); +} + +HResultError::HResultError(HRESULT h_result) + : PlatformException(HResultMakeMessage(h_result, nullptr)), + h_result_(h_result) {} + +HResultError::HResultError(HRESULT h_result, + const std::string_view& additional_message) + : PlatformException(HResultMakeMessage(h_result, &additional_message)), + h_result_(h_result) {} +} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/font.cpp b/src/win/graph/direct/font.cpp new file mode 100644 index 00000000..a359d73e --- /dev/null +++ b/src/win/graph/direct/font.cpp @@ -0,0 +1,29 @@ +#include "cru/win/graph/win_font.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/win_native_factory.hpp" + +#include +#include +#include + +namespace cru::win::graph { +WinFontDescriptor::WinFontDescriptor(IWinNativeFactory* factory, + const std::wstring_view& font_family, + float font_size) { + assert(factory); + std::array buffer; + if (!::GetUserDefaultLocaleName(buffer.data(), + static_cast(buffer.size()))) + throw Win32Error(::GetLastError(), "Failed to get locale."); + + ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat( + font_family.data(), nullptr, DWRITE_FONT_WEIGHT_NORMAL, + DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, font_size, + buffer.data(), &text_format_)); + + ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER)); + ThrowIfFailed( + text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)); +} +} // namespace cru::win::graph diff --git a/src/win/graph/direct/geometry.cpp b/src/win/graph/direct/geometry.cpp new file mode 100644 index 00000000..a725eff6 --- /dev/null +++ b/src/win/graph/direct/geometry.cpp @@ -0,0 +1,65 @@ +#include "cru/win/graph/win_geometry.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/util/convert_util.hpp" +#include "cru/win/graph/win_native_factory.hpp" + +#include + +namespace cru::win::graph { +WinGeometryBuilder::WinGeometryBuilder(IWinNativeFactory* factory) { + assert(factory); + ThrowIfFailed(factory->GetD2D1Factory()->CreatePathGeometry(&geometry_)); + ThrowIfFailed(geometry_->Open(&geometry_sink_)); +} + +WinGeometryBuilder::~WinGeometryBuilder() { + if (geometry_sink_) { + ThrowIfFailed(geometry_sink_->Close()); + } +} + +void WinGeometryBuilder::BeginFigure(const ui::Point& point) { + assert(IsEnded()); + geometry_sink_->BeginFigure(util::Convert(point), D2D1_FIGURE_BEGIN_FILLED); +} + +void WinGeometryBuilder::LineTo(const ui::Point& point) { + assert(IsEnded()); + geometry_sink_->AddLine(util::Convert(point)); +} + +void WinGeometryBuilder::QuadraticBezierTo(const ui::Point& control_point, + const ui::Point& end_point) { + assert(IsEnded()); + geometry_sink_->AddQuadraticBezier(D2D1::QuadraticBezierSegment( + util::Convert(control_point), util::Convert(end_point))); +} + +void WinGeometryBuilder::CloseFigure(bool close) { + assert(IsEnded()); + geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED + : D2D1_FIGURE_END_OPEN); +} + +platform::graph::IGeometry* WinGeometryBuilder::End() { + assert(IsEnded()); + ThrowIfFailed(geometry_sink_->Close()); + geometry_sink_ = nullptr; + const auto geometry = new WinGeometry(std::move(geometry_)); + geometry_ = nullptr; + return geometry; +} + +WinGeometry::WinGeometry(Microsoft::WRL::ComPtr geometry) { + assert(geometry); + geometry_ = std::move(geometry); +} + +bool WinGeometry::FillContains(const ui::Point& point) { + BOOL result; + ThrowIfFailed(geometry_->FillContainsPoint( + util::Convert(point), D2D1::Matrix3x2F::Identity(), &result)); + return result != 0; +} +} // namespace cru::win::graph diff --git a/src/win/graph/direct/graph_factory.cpp b/src/win/graph/direct/graph_factory.cpp new file mode 100644 index 00000000..2f2bb7a5 --- /dev/null +++ b/src/win/graph/direct/graph_factory.cpp @@ -0,0 +1,113 @@ +#include "cru/win/graph/direct/graph_factory.hpp" + +#include "cru/win/graph/direct/brush.hpp" +#include "cru/win/graph/direct/exception.hpp" +#include "cru/win/graph/direct/font.hpp" +#include "cru/win/graph/direct/geometry.hpp" +#include "cru/win/graph/direct/text_layout.hpp" + +#include +#include +#include + +namespace cru::platform::graph::win::direct { +namespace { +DirectGraphFactory* instance = nullptr; +} +} // namespace cru::platform::graph::win::direct + +namespace cru::platform::graph { +void GraphFactoryAutoDeleteExitHandler() { + const auto i = + ::cru::platform::graph::win::direct::instance; // avoid long namespace + // prefix + if (i == nullptr) return; + if (i->IsAutoDelete()) delete i; +} + +GraphFactory* GraphFactory::CreateInstance() { + auto& i = ::cru::platform::graph::win::direct::instance; // avoid long + // namespace prefix + assert(i == nullptr); + i = new ::cru::platform::graph::win::direct::DirectGraphFactory(); + std::atexit(&GraphFactoryAutoDeleteExitHandler); + return i; +} + +GraphFactory* GraphFactory::GetInstance() { + return ::cru::platform::graph::win::direct::instance; +} +} // namespace cru::platform::graph + +namespace cru::platform::graph::win::direct { +DirectGraphFactory* DirectGraphFactory::GetInstance() { return instance; } + +DirectGraphFactory::DirectGraphFactory() { + UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; + +#ifdef CRU_DEBUG + creation_flags |= D3D11_CREATE_DEVICE_DEBUG; +#endif + + const D3D_FEATURE_LEVEL feature_levels[] = { + D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1}; + + Microsoft::WRL::ComPtr d3d11_device_context; + + ThrowIfFailed(D3D11CreateDevice( + nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, + feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, + &d3d11_device_, nullptr, &d3d11_device_context)); + + Microsoft::WRL::ComPtr dxgi_device; + ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); + + ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, + IID_PPV_ARGS(&d2d1_factory_))); + + Microsoft::WRL::ComPtr d2d1_device; + + ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); + + ThrowIfFailed(d2d1_device->CreateDeviceContext( + D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_)); + + // Identify the physical adapter (GPU or card) this device is runs on. + Microsoft::WRL::ComPtr dxgi_adapter; + ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); + + // Get the factory object that created the DXGI device. + ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); + + ThrowIfFailed(DWriteCreateFactory( + DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), + reinterpret_cast(dwrite_factory_.GetAddressOf()))); + + ThrowIfFailed(dwrite_factory_->GetSystemFontCollection( + &dwrite_system_font_collection_)); +} + +DirectGraphFactory::~DirectGraphFactory() { instance = nullptr; } + +D2DSolidColorBrush* DirectGraphFactory::CreateSolidColorBrush() { + return new D2DSolidColorBrush(this); +} + +platform::graph::IGeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { + return new WinGeometryBuilder(this); +} + +platform::graph::IFontDescriptor* WinGraphFactory::CreateFontDescriptor( + const std::wstring_view& font_family, float font_size) { + return new WinFontDescriptor(this, font_family, font_size); +} + +platform::graph::ITextLayout* WinGraphFactory::CreateTextLayout( + std::shared_ptr font, std::wstring text) { + const auto f = std::dynamic_pointer_cast(font); + assert(f); + return new WinTextLayout(this, std::move(f), std::move(text)); +} +} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/painter.cpp b/src/win/graph/direct/painter.cpp new file mode 100644 index 00000000..f75cf4e0 --- /dev/null +++ b/src/win/graph/direct/painter.cpp @@ -0,0 +1,93 @@ +#include "cru/win/graph/win_painter.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/win_native_factory.hpp" +#include "cru/win/graph/util/convert_util.hpp" +#include "cru/win/graph/win_brush.hpp" +#include "cru/win/graph/win_geometry.hpp" +#include "cru/win/graph/win_text_layout.hpp" + +#include + +namespace cru::win::graph { +WinPainter::WinPainter(ID2D1RenderTarget* render_target) { + assert(render_target); + render_target_ = render_target; +} + +platform::Matrix WinPainter::GetTransform() { + assert(!IsEnded()); + D2D1_MATRIX_3X2_F m; + render_target_->GetTransform(&m); + return util::Convert(m); +} + +void WinPainter::SetTransform(const platform::Matrix& matrix) { + assert(!IsEnded()); + render_target_->SetTransform(util::Convert(matrix)); +} + +void WinPainter::Clear(const ui::Color& color) { + assert(!IsEnded()); + render_target_->Clear(util::Convert(color)); +} + +void WinPainter::StrokeRectangle(const ui::Rect& rectangle, + platform::graph::IBrush* brush, float width) { + assert(!IsEnded()); + const auto b = dynamic_cast(brush); + assert(b); + render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), + width); +} + +void WinPainter::FillRectangle(const ui::Rect& rectangle, + platform::graph::IBrush* brush) { + assert(!IsEnded()); + const auto b = dynamic_cast(brush); + assert(b); + render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); +} + +void WinPainter::StrokeGeometry(platform::graph::IGeometry* geometry, + platform::graph::IBrush* brush, float width) { + assert(!IsEnded()); + const auto g = dynamic_cast(geometry); + assert(g); + const auto b = dynamic_cast(brush); + assert(b); + + render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); +} + +void WinPainter::FillGeometry(platform::graph::IGeometry* geometry, + platform::graph::IBrush* brush) { + assert(!IsEnded()); + const auto g = dynamic_cast(geometry); + assert(g); + const auto b = dynamic_cast(brush); + assert(b); + + render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); +} + +void WinPainter::DrawText(const ui::Point& offset, + platform::graph::ITextLayout* text_layout, + platform::graph::IBrush* brush) { + assert(!IsEnded()); + const auto t = dynamic_cast(text_layout); + assert(t); + const auto b = dynamic_cast(brush); + assert(b); + + render_target_->DrawTextLayout(util::Convert(offset), + t->GetDWriteTextLayout(), b->GetD2DBrush()); +} + +void WinPainter::End() { + if (!is_draw_ended_) { + is_draw_ended_ = true; + DoEndDraw(); + } +} +} // namespace cru::win::graph diff --git a/src/win/graph/direct/text_layout.cpp b/src/win/graph/direct/text_layout.cpp new file mode 100644 index 00000000..997309ad --- /dev/null +++ b/src/win/graph/direct/text_layout.cpp @@ -0,0 +1,94 @@ +#include "cru/win/graph/win_text_layout.hpp" + +#include "cru/win/exception.hpp" +#include "cru/win/graph/win_font.hpp" +#include "cru/win/graph/win_native_factory.hpp" + +#include +#include + +namespace cru::win::graph { +WinTextLayout::WinTextLayout(IWinNativeFactory* factory, + std::shared_ptr font, + std::wstring text) { + assert(factory); + assert(font); + factory_ = factory; + text_.swap(text); + font_descriptor_.swap(font); + + ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout( + text_.c_str(), static_cast(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(factory_->GetDWriteFactory()->CreateTextLayout( + text_.c_str(), static_cast(text_.size()), + font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, + &text_layout_)); +} + +std::shared_ptr WinTextLayout::GetFont() { + return font_descriptor_; +} + +void WinTextLayout::SetFont( + std::shared_ptr font) { + auto f = std::dynamic_pointer_cast(font); + assert(f); + f.swap(font_descriptor_); + ThrowIfFailed(factory_->GetDWriteFactory()->CreateTextLayout( + text_.c_str(), static_cast(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 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 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 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::win::graph diff --git a/src/win/graph/win_brush.cpp b/src/win/graph/win_brush.cpp deleted file mode 100644 index 0c783366..00000000 --- a/src/win/graph/win_brush.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "cru/win/graph/win_brush.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_native_factory.hpp" -#include "cru/win/graph/util/convert_util.hpp" - -#include - -namespace cru::win::graph { -WinSolidColorBrush::WinSolidColorBrush(IWinNativeFactory* factory, - const ui::Color& color) { - assert(factory); - ThrowIfFailed(factory->GetD2D1DeviceContext()->CreateSolidColorBrush( - util::Convert(color), &brush_)); -} - -ui::Color WinSolidColorBrush::GetColor() { - return util::Convert(brush_->GetColor()); -} -void WinSolidColorBrush::SetColor(const ui::Color& color) { - brush_->SetColor(util::Convert(color)); -} -} // namespace cru::win::graph diff --git a/src/win/graph/win_font.cpp b/src/win/graph/win_font.cpp deleted file mode 100644 index a359d73e..00000000 --- a/src/win/graph/win_font.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "cru/win/graph/win_font.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_native_factory.hpp" - -#include -#include -#include - -namespace cru::win::graph { -WinFontDescriptor::WinFontDescriptor(IWinNativeFactory* factory, - const std::wstring_view& font_family, - float font_size) { - assert(factory); - std::array buffer; - if (!::GetUserDefaultLocaleName(buffer.data(), - static_cast(buffer.size()))) - throw Win32Error(::GetLastError(), "Failed to get locale."); - - ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat( - font_family.data(), nullptr, DWRITE_FONT_WEIGHT_NORMAL, - DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, font_size, - buffer.data(), &text_format_)); - - ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER)); - ThrowIfFailed( - text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)); -} -} // namespace cru::win::graph diff --git a/src/win/graph/win_geometry.cpp b/src/win/graph/win_geometry.cpp deleted file mode 100644 index a725eff6..00000000 --- a/src/win/graph/win_geometry.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "cru/win/graph/win_geometry.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/util/convert_util.hpp" -#include "cru/win/graph/win_native_factory.hpp" - -#include - -namespace cru::win::graph { -WinGeometryBuilder::WinGeometryBuilder(IWinNativeFactory* factory) { - assert(factory); - ThrowIfFailed(factory->GetD2D1Factory()->CreatePathGeometry(&geometry_)); - ThrowIfFailed(geometry_->Open(&geometry_sink_)); -} - -WinGeometryBuilder::~WinGeometryBuilder() { - if (geometry_sink_) { - ThrowIfFailed(geometry_sink_->Close()); - } -} - -void WinGeometryBuilder::BeginFigure(const ui::Point& point) { - assert(IsEnded()); - geometry_sink_->BeginFigure(util::Convert(point), D2D1_FIGURE_BEGIN_FILLED); -} - -void WinGeometryBuilder::LineTo(const ui::Point& point) { - assert(IsEnded()); - geometry_sink_->AddLine(util::Convert(point)); -} - -void WinGeometryBuilder::QuadraticBezierTo(const ui::Point& control_point, - const ui::Point& end_point) { - assert(IsEnded()); - geometry_sink_->AddQuadraticBezier(D2D1::QuadraticBezierSegment( - util::Convert(control_point), util::Convert(end_point))); -} - -void WinGeometryBuilder::CloseFigure(bool close) { - assert(IsEnded()); - geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED - : D2D1_FIGURE_END_OPEN); -} - -platform::graph::IGeometry* WinGeometryBuilder::End() { - assert(IsEnded()); - ThrowIfFailed(geometry_sink_->Close()); - geometry_sink_ = nullptr; - const auto geometry = new WinGeometry(std::move(geometry_)); - geometry_ = nullptr; - return geometry; -} - -WinGeometry::WinGeometry(Microsoft::WRL::ComPtr geometry) { - assert(geometry); - geometry_ = std::move(geometry); -} - -bool WinGeometry::FillContains(const ui::Point& point) { - BOOL result; - ThrowIfFailed(geometry_->FillContainsPoint( - util::Convert(point), D2D1::Matrix3x2F::Identity(), &result)); - return result != 0; -} -} // namespace cru::win::graph diff --git a/src/win/graph/win_graph_factory.cpp b/src/win/graph/win_graph_factory.cpp deleted file mode 100644 index de1d28f9..00000000 --- a/src/win/graph/win_graph_factory.cpp +++ /dev/null @@ -1,111 +0,0 @@ -#include "cru/win/graph/win_graph_factory.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_brush.hpp" -#include "cru/win/graph/win_font.hpp" -#include "cru/win/graph/win_geometry.hpp" -#include "cru/win/graph/win_text_layout.hpp" - -#include -#include -#include - -namespace cru::win::graph { -namespace { -WinGraphFactory* instance = nullptr; -} -} // namespace cru::win::graph - -namespace cru::platform::graph { -void GraphFactoryAutoDeleteExitHandler() { - const auto i = ::cru::win::graph::instance; // avoid long namespace prefix - if (i == nullptr) return; - if (i->IsAutoDelete()) delete i; -} - -IGraphFactory* IGraphFactory::CreateInstance() { - auto& i = ::cru::win::graph::instance; // avoid long namespace prefix - assert(i == nullptr); - i = new cru::win::graph::WinGraphFactory(); - std::atexit(&GraphFactoryAutoDeleteExitHandler); - return i; -} - -IGraphFactory* IGraphFactory::GetInstance() { - return ::cru::win::graph::instance; -} -} // namespace cru::platform::graph - -namespace cru::win::graph { -WinGraphFactory* WinGraphFactory::GetInstance() { return instance; } - -WinGraphFactory::WinGraphFactory() { - UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; - -#ifdef CRU_DEBUG - creation_flags |= D3D11_CREATE_DEVICE_DEBUG; -#endif - - const D3D_FEATURE_LEVEL feature_levels[] = { - D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, - D3D_FEATURE_LEVEL_9_1}; - - Microsoft::WRL::ComPtr d3d11_device_context; - - ThrowIfFailed(D3D11CreateDevice( - nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, - feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, - &d3d11_device_, nullptr, &d3d11_device_context)); - - Microsoft::WRL::ComPtr dxgi_device; - ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); - - ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, - IID_PPV_ARGS(&d2d1_factory_))); - - Microsoft::WRL::ComPtr d2d1_device; - - ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); - - ThrowIfFailed(d2d1_device->CreateDeviceContext( - D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_)); - - // Identify the physical adapter (GPU or card) this device is runs on. - Microsoft::WRL::ComPtr dxgi_adapter; - ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); - - // Get the factory object that created the DXGI device. - ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); - - ThrowIfFailed(DWriteCreateFactory( - DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), - reinterpret_cast(dwrite_factory_.GetAddressOf()))); - - ThrowIfFailed(dwrite_factory_->GetSystemFontCollection( - &dwrite_system_font_collection_)); -} - -WinGraphFactory::~WinGraphFactory() { instance = nullptr; } - -platform::graph::ISolidColorBrush* WinGraphFactory::CreateSolidColorBrush( - const ui::Color& color) { - return new WinSolidColorBrush(this, color); -} - -platform::graph::IGeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { - return new WinGeometryBuilder(this); -} - -platform::graph::IFontDescriptor* WinGraphFactory::CreateFontDescriptor( - const std::wstring_view& font_family, float font_size) { - return new WinFontDescriptor(this, font_family, font_size); -} - -platform::graph::ITextLayout* WinGraphFactory::CreateTextLayout( - std::shared_ptr font, std::wstring text) { - const auto f = std::dynamic_pointer_cast(font); - assert(f); - return new WinTextLayout(this, std::move(f), std::move(text)); -} -} // namespace cru::win::graph diff --git a/src/win/graph/win_painter.cpp b/src/win/graph/win_painter.cpp deleted file mode 100644 index f75cf4e0..00000000 --- a/src/win/graph/win_painter.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include "cru/win/graph/win_painter.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_native_factory.hpp" -#include "cru/win/graph/util/convert_util.hpp" -#include "cru/win/graph/win_brush.hpp" -#include "cru/win/graph/win_geometry.hpp" -#include "cru/win/graph/win_text_layout.hpp" - -#include - -namespace cru::win::graph { -WinPainter::WinPainter(ID2D1RenderTarget* render_target) { - assert(render_target); - render_target_ = render_target; -} - -platform::Matrix WinPainter::GetTransform() { - assert(!IsEnded()); - D2D1_MATRIX_3X2_F m; - render_target_->GetTransform(&m); - return util::Convert(m); -} - -void WinPainter::SetTransform(const platform::Matrix& matrix) { - assert(!IsEnded()); - render_target_->SetTransform(util::Convert(matrix)); -} - -void WinPainter::Clear(const ui::Color& color) { - assert(!IsEnded()); - render_target_->Clear(util::Convert(color)); -} - -void WinPainter::StrokeRectangle(const ui::Rect& rectangle, - platform::graph::IBrush* brush, float width) { - assert(!IsEnded()); - const auto b = dynamic_cast(brush); - assert(b); - render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(), - width); -} - -void WinPainter::FillRectangle(const ui::Rect& rectangle, - platform::graph::IBrush* brush) { - assert(!IsEnded()); - const auto b = dynamic_cast(brush); - assert(b); - render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush()); -} - -void WinPainter::StrokeGeometry(platform::graph::IGeometry* geometry, - platform::graph::IBrush* brush, float width) { - assert(!IsEnded()); - const auto g = dynamic_cast(geometry); - assert(g); - const auto b = dynamic_cast(brush); - assert(b); - - render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width); -} - -void WinPainter::FillGeometry(platform::graph::IGeometry* geometry, - platform::graph::IBrush* brush) { - assert(!IsEnded()); - const auto g = dynamic_cast(geometry); - assert(g); - const auto b = dynamic_cast(brush); - assert(b); - - render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush()); -} - -void WinPainter::DrawText(const ui::Point& offset, - platform::graph::ITextLayout* text_layout, - platform::graph::IBrush* brush) { - assert(!IsEnded()); - const auto t = dynamic_cast(text_layout); - assert(t); - const auto b = dynamic_cast(brush); - assert(b); - - render_target_->DrawTextLayout(util::Convert(offset), - t->GetDWriteTextLayout(), b->GetD2DBrush()); -} - -void WinPainter::End() { - if (!is_draw_ended_) { - is_draw_ended_ = true; - DoEndDraw(); - } -} -} // namespace cru::win::graph diff --git a/src/win/graph/win_text_layout.cpp b/src/win/graph/win_text_layout.cpp deleted file mode 100644 index 997309ad..00000000 --- a/src/win/graph/win_text_layout.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include "cru/win/graph/win_text_layout.hpp" - -#include "cru/win/exception.hpp" -#include "cru/win/graph/win_font.hpp" -#include "cru/win/graph/win_native_factory.hpp" - -#include -#include - -namespace cru::win::graph { -WinTextLayout::WinTextLayout(IWinNativeFactory* factory, - std::shared_ptr font, - std::wstring text) { - assert(factory); - assert(font); - factory_ = factory; - text_.swap(text); - font_descriptor_.swap(font); - - ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout( - text_.c_str(), static_cast(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(factory_->GetDWriteFactory()->CreateTextLayout( - text_.c_str(), static_cast(text_.size()), - font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_, - &text_layout_)); -} - -std::shared_ptr WinTextLayout::GetFont() { - return font_descriptor_; -} - -void WinTextLayout::SetFont( - std::shared_ptr font) { - auto f = std::dynamic_pointer_cast(font); - assert(f); - f.swap(font_descriptor_); - ThrowIfFailed(factory_->GetDWriteFactory()->CreateTextLayout( - text_.c_str(), static_cast(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 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 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 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::win::graph -- cgit v1.2.3