From baa7cf141b8121473edceae16c1a20a6d47bd202 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 27 Jun 2019 00:18:48 +0800 Subject: ...... --- include/cru/common/auto_delete.hpp | 14 -- include/cru/common/endable.hpp | 21 -- include/cru/common/ui_base.hpp | 257 ------------------------ include/cru/platform/graph/brush.hpp | 49 ++++- include/cru/platform/graph/font.hpp | 16 +- include/cru/platform/graph/geometry.hpp | 48 ++++- include/cru/platform/graph/graph_factory.hpp | 48 +++-- include/cru/platform/graph/painter.hpp | 45 +++-- include/cru/platform/graph/text_layout.hpp | 36 +++- include/cru/platform/graphic_base.hpp | 257 ++++++++++++++++++++++++ include/cru/platform/matrix.hpp | 4 +- include/cru/platform/native_resource.hpp | 22 ++ include/cru/win/exception.hpp | 2 +- include/cru/win/graph/direct/brush.hpp | 26 +++ include/cru/win/graph/direct/com_resource.hpp | 11 + include/cru/win/graph/direct/convert_util.hpp | 108 ++++++++++ include/cru/win/graph/direct/direct_factory.hpp | 16 ++ include/cru/win/graph/direct/exception.hpp | 36 ++++ include/cru/win/graph/direct/font.hpp | 28 +++ include/cru/win/graph/direct/geometry.hpp | 48 +++++ include/cru/win/graph/direct/graph_factory.hpp | 60 ++++++ include/cru/win/graph/direct/painter.hpp | 43 ++++ include/cru/win/graph/direct/text_layout.hpp | 42 ++++ include/cru/win/graph/util/convert_util.hpp | 108 ---------- include/cru/win/graph/win_brush.hpp | 32 --- include/cru/win/graph/win_font.hpp | 28 --- include/cru/win/graph/win_geometry.hpp | 48 ----- include/cru/win/graph/win_graph_factory.hpp | 61 ------ include/cru/win/graph/win_native_factory.hpp | 16 -- include/cru/win/graph/win_painter.hpp | 43 ---- include/cru/win/graph/win_text_layout.hpp | 42 ---- include/cru/win/win_pre_config.hpp | 1 + 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 --------- 48 files changed, 1349 insertions(+), 1174 deletions(-) delete mode 100644 include/cru/common/auto_delete.hpp delete mode 100644 include/cru/common/endable.hpp delete mode 100644 include/cru/common/ui_base.hpp create mode 100644 include/cru/platform/graphic_base.hpp create mode 100644 include/cru/platform/native_resource.hpp create mode 100644 include/cru/win/graph/direct/brush.hpp create mode 100644 include/cru/win/graph/direct/com_resource.hpp create mode 100644 include/cru/win/graph/direct/convert_util.hpp create mode 100644 include/cru/win/graph/direct/direct_factory.hpp create mode 100644 include/cru/win/graph/direct/exception.hpp create mode 100644 include/cru/win/graph/direct/font.hpp create mode 100644 include/cru/win/graph/direct/geometry.hpp create mode 100644 include/cru/win/graph/direct/graph_factory.hpp create mode 100644 include/cru/win/graph/direct/painter.hpp create mode 100644 include/cru/win/graph/direct/text_layout.hpp delete mode 100644 include/cru/win/graph/util/convert_util.hpp delete mode 100644 include/cru/win/graph/win_brush.hpp delete mode 100644 include/cru/win/graph/win_font.hpp delete mode 100644 include/cru/win/graph/win_geometry.hpp delete mode 100644 include/cru/win/graph/win_graph_factory.hpp delete mode 100644 include/cru/win/graph/win_native_factory.hpp delete mode 100644 include/cru/win/graph/win_painter.hpp delete mode 100644 include/cru/win/graph/win_text_layout.hpp 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 diff --git a/include/cru/common/auto_delete.hpp b/include/cru/common/auto_delete.hpp deleted file mode 100644 index ae66f7bf..00000000 --- a/include/cru/common/auto_delete.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "base.hpp" - -namespace cru { -// A instance of class implementing this interface is able to -// delete itseft when program exits. Such as IGraphFactory, -// IUiApplication. -struct IAutoDelete : virtual Interface { - // Get whether it will delete itself when program exits. - virtual bool IsAutoDelete() const = 0; - // Set whether it will delete itself when program exits. - virtual void SetAutoDelete(bool value) = 0; -}; -} // namespace cru diff --git a/include/cru/common/endable.hpp b/include/cru/common/endable.hpp deleted file mode 100644 index 4459b069..00000000 --- a/include/cru/common/endable.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include "base.hpp" - -namespace cru { -// Although c++ has destructor called automatically. But there is -// occasion when an instance of class needs to end with a result -// and release all internal resources. -// eg. IGeometryBuild will end with building a Geometry and release -// some resources. IPainter will end drawing and release some -// resources and map the drawing result onto target. -// note: You can't call End twice. And most methods on the object -// is invalid to call after End is called. Get whether it is ended -// by IsEnded. -template -struct IEndable : virtual Interface { - // Get whether the object is ended. - virtual bool IsEnded() const = 0; - // End the object with a result. - virtual TResult End() = 0; -}; -} diff --git a/include/cru/common/ui_base.hpp b/include/cru/common/ui_base.hpp deleted file mode 100644 index 017e3bd1..00000000 --- a/include/cru/common/ui_base.hpp +++ /dev/null @@ -1,257 +0,0 @@ -#pragma once -#include "pre_config.hpp" - -#include -#include -#include - -namespace cru::ui { -struct Point final { - constexpr Point() = default; - constexpr Point(const float x, const float y) : x(x), y(y) {} - - float x = 0; - float y = 0; -}; - -constexpr bool operator==(const Point& left, const Point& right) { - return left.x == right.x && left.y == right.y; -} - -constexpr bool operator!=(const Point& left, const Point& right) { - return !(left == right); -} - -struct Size final { - constexpr Size() = default; - constexpr Size(const float width, const float height) - : width(width), height(height) {} - - float width = 0; - float height = 0; -}; - -constexpr Size operator+(const Size& left, const Size& right) { - return Size(left.width + right.width, left.height + right.height); -} - -constexpr Size operator-(const Size& left, const Size& right) { - return Size(left.width - right.width, left.height - right.height); -} - -constexpr bool operator==(const Size& left, const Size& right) { - return left.width == right.width && left.height == right.height; -} - -constexpr bool operator!=(const Size& left, const Size& right) { - return !(left == right); -} - -struct Thickness final { - constexpr Thickness() : Thickness(0) {} - - constexpr explicit Thickness(const float width) - : left(width), top(width), right(width), bottom(width) {} - - constexpr explicit Thickness(const float horizontal, const float vertical) - : left(horizontal), top(vertical), right(horizontal), bottom(vertical) {} - - constexpr Thickness(const float left, const float top, const float right, - const float bottom) - : left(left), top(top), right(right), bottom(bottom) {} - - constexpr float GetHorizontalTotal() const { return left + right; } - - constexpr float GetVerticalTotal() const { return top + bottom; } - - void SetLeftRight(const float value) { left = right = value; } - - void SetTopBottom(const float value) { top = bottom = value; } - - void SetAll(const float value) { left = top = right = bottom = value; } - - constexpr float Validate() const { - return left >= 0.0 && top >= 0.0 && right >= 0.0 && bottom >= 0.0; - } - - float left; - float top; - float right; - float bottom; -}; - -constexpr bool operator==(const Thickness& left, const Thickness& right) { - return left.left == right.left && left.top == right.top && - left.right == right.right && left.bottom == right.bottom; -} - -constexpr bool operator!=(const Thickness& left, const Thickness& right) { - return !(left == right); -} - -struct Rect final { - constexpr Rect() = default; - constexpr Rect(const float left, const float top, const float width, - const float height) - : left(left), top(top), width(width), height(height) {} - constexpr Rect(const Point& lefttop, const Size& size) - : left(lefttop.x), - top(lefttop.y), - width(size.width), - height(size.height) {} - - constexpr static Rect FromVertices(const float left, const float top, - const float right, const float bottom) { - return Rect(left, top, right - left, bottom - top); - } - - constexpr static Rect FromCenter(const Point& center, const float width, - const float height) { - return Rect(center.x - width / 2.0f, center.y - height / 2.0f, width, - height); - } - - constexpr float GetRight() const { return left + width; } - - constexpr float GetBottom() const { return top + height; } - - constexpr Point GetLeftTop() const { return Point(left, top); } - - constexpr Point GetRightBottom() const { - return Point(left + width, top + height); - } - - constexpr Point GetLeftBottom() const { return Point(left, top + height); } - - constexpr Point GetRightTop() const { return Point(left + width, top); } - - constexpr Point GetCenter() const { - return Point(left + width / 2.0f, top + height / 2.0f); - } - - constexpr Size GetSize() const { return Size(width, height); } - - constexpr Rect Shrink(const Thickness& thickness) const { - return Rect(left + thickness.left, top + thickness.top, - width - thickness.GetHorizontalTotal(), - height - thickness.GetVerticalTotal()); - } - - constexpr bool IsPointInside(const Point& point) const { - return point.x >= left && point.x < GetRight() && point.y >= top && - point.y < GetBottom(); - } - - float left = 0.0f; - float top = 0.0f; - float width = 0.0f; - float height = 0.0f; -}; - -constexpr bool operator==(const Rect& left, const Rect& right) { - return left.left == right.left && left.top == right.top && - left.width == right.width && left.height == right.height; -} - -constexpr bool operator!=(const Rect& left, const Rect& right) { - return !(left == right); -} - -struct RoundedRect final { - constexpr RoundedRect() = default; - constexpr RoundedRect(const Rect& rect, const float radius_x, - const float radius_y) - : rect(rect), radius_x(radius_x), radius_y(radius_y) {} - - Rect rect{}; - float radius_x = 0.0f; - float radius_y = 0.0f; -}; - -constexpr bool operator==(const RoundedRect& left, const RoundedRect& right) { - return left.rect == right.rect && left.radius_x == right.radius_x && - left.radius_y == right.radius_y; -} - -constexpr bool operator!=(const RoundedRect& left, const RoundedRect& right) { - return !(left == right); -} - -struct Ellipse final { - constexpr Ellipse() = default; - constexpr Ellipse(const Point& center, const float radius_x, - const float radius_y) - : center(center), radius_x(radius_x), radius_y(radius_y) {} - - constexpr static Ellipse FromRect(const Rect& rect) { - return Ellipse(rect.GetCenter(), rect.width / 2.0f, rect.height / 2.0f); - } - - constexpr Rect GetBoundRect() const { - return Rect::FromCenter(center, radius_x * 2.0f, radius_y * 2.0f); - } - - Point center{}; - float radius_x = 0.0f; - float radius_y = 0.0f; -}; - -constexpr bool operator==(const Ellipse& left, const Ellipse& right) { - return left.center == right.center && left.radius_x == right.radius_x && - left.radius_y == right.radius_y; -} - -constexpr bool operator!=(const Ellipse& left, const Ellipse& right) { - return !(left == right); -} - -struct TextRange final { - constexpr static std::optional FromTwoSides(unsigned first, - unsigned second) { - if (first > second) - return std::make_optional(second, first - second); - if (first < second) - return std::make_optional(first, second - first); - return std::nullopt; - } - - constexpr static std::pair ToTwoSides( - std::optional text_range, unsigned default_position = 0) { - if (text_range.has_value()) - return std::make_pair( - text_range.value().position, - text_range.value().position + text_range.value().count); - return std::make_pair(default_position, default_position); - } - - constexpr TextRange() = default; - constexpr TextRange(const unsigned position, const unsigned count) - : position(position), count(count) {} - - unsigned position = 0; - unsigned count = 0; -}; - -struct Color { - constexpr Color() : Color(0, 0, 0, 255) {} - constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, - std::uint8_t alpha = 255) - : red(red), green(green), blue(blue), alpha(alpha) {} - - constexpr static Color FromHex(std::uint32_t hex) { - return Color(hex & (0b11111111 << 16), hex & (0b11111111 << 8), - hex & (0b11111111), hex & (0b11111111 << 24)); - } - - std::uint8_t red; - std::uint8_t green; - std::uint8_t blue; - std::uint8_t alpha; -}; - -namespace colors { -constexpr Color black{0, 0, 0}; -constexpr Color white{255, 255, 255}; -constexpr Color skyblue = Color::FromHex(0x87ceeb); -} // namespace colors -} // namespace cru::ui diff --git a/include/cru/platform/graph/brush.hpp b/include/cru/platform/graph/brush.hpp index 7688ec4a..d292ae82 100644 --- a/include/cru/platform/graph/brush.hpp +++ b/include/cru/platform/graph/brush.hpp @@ -1,13 +1,46 @@ #pragma once -#include "cru/common/base.hpp" - -#include "cru/common/ui_base.hpp" +#include "../graphic_base.hpp" +#include "../native_resource.hpp" namespace cru::platform::graph { -struct IBrush : public virtual Interface {}; +class Brush : public NativeResource { + protected: + Brush() = default; + + public: + Brush(const Brush& other) = delete; + Brush& operator=(const Brush& other) = delete; + + Brush(Brush&& other) = delete; + Brush& operator=(Brush&& other) = delete; + + ~Brush() override = default; +}; + +class SolidColorBrush : public Brush { + protected: + SolidColorBrush() = default; + + public: + SolidColorBrush(const SolidColorBrush& other) = delete; + SolidColorBrush& operator=(const SolidColorBrush& other) = delete; + + SolidColorBrush(SolidColorBrush&& other) = delete; + SolidColorBrush& operator=(SolidColorBrush&& other) = delete; + + ~SolidColorBrush() = default; + + public: + Color GetColor() { return color_; } + void SetColor(const Color& color) { + color_ = color; + OnSetColor(color); + } + + protected: + virtual void OnSetColor(const Color& color) = 0; -struct ISolidColorBrush : public virtual IBrush { - virtual ui::Color GetColor() = 0; - virtual void SetColor(const ui::Color& color) = 0; + protected: + Color color_ = colors::black; }; -} // namespace cru::platform +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/font.hpp b/include/cru/platform/graph/font.hpp index f74dc7db..bd470256 100644 --- a/include/cru/platform/graph/font.hpp +++ b/include/cru/platform/graph/font.hpp @@ -1,8 +1,18 @@ #pragma once -#include "cru/common/base.hpp" +#include "../native_resource.hpp" namespace cru::platform::graph { -struct IFontDescriptor : virtual Interface { +class Font : public NativeResource { + protected: + Font() = default; + public: + Font(const Font& other) = delete; + Font& operator=(const Font& other) = delete; + + Font(Font&& other) = delete; + Font& operator=(Font&& other) = delete; + + ~Font() override = default; }; -} +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/geometry.hpp b/include/cru/platform/graph/geometry.hpp index 71bf8c73..d31b3b27 100644 --- a/include/cru/platform/graph/geometry.hpp +++ b/include/cru/platform/graph/geometry.hpp @@ -1,19 +1,45 @@ #pragma once -#include "cru/common/base.hpp" - -#include "cru/common/endable.hpp" -#include "cru/common/ui_base.hpp" +#include "../graphic_base.hpp" +#include "../native_resource.hpp" namespace cru::platform::graph { -struct IGeometry : virtual Interface { - virtual bool FillContains(const ui::Point& point) = 0; +class Geometry : public NativeResource { + protected: + Geometry() = default; + + public: + Geometry(const Geometry& other) = delete; + Geometry& operator=(const Geometry& other) = delete; + + Geometry(Geometry&& other) = delete; + Geometry& operator=(Geometry&& other) = delete; + + ~Geometry() override = default; + + public: + virtual bool FillContains(const Point& point) = 0; }; -struct IGeometryBuilder : virtual Interface, virtual IEndable { - virtual void BeginFigure(const ui::Point& point) = 0; - virtual void LineTo(const ui::Point& point) = 0; - virtual void QuadraticBezierTo(const ui::Point& control_point, - const ui::Point& end_point) = 0; +class GeometryBuilder : public NativeResource { + protected: + GeometryBuilder() = default; + + public: + GeometryBuilder(const GeometryBuilder& other) = delete; + GeometryBuilder& operator=(const GeometryBuilder& other) = delete; + + GeometryBuilder(GeometryBuilder&& other) = delete; + GeometryBuilder& operator=(GeometryBuilder&& other) = delete; + + ~GeometryBuilder() override = default; + + public: + virtual void BeginFigure(const Point& point) = 0; + virtual void LineTo(const Point& point) = 0; + virtual void QuadraticBezierTo(const Point& control_point, + const Point& end_point) = 0; virtual void CloseFigure(bool close) = 0; + + virtual Geometry* Build() = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/graph/graph_factory.hpp b/include/cru/platform/graph/graph_factory.hpp index 60d4ed8a..69afc7b3 100644 --- a/include/cru/platform/graph/graph_factory.hpp +++ b/include/cru/platform/graph/graph_factory.hpp @@ -1,8 +1,6 @@ #pragma once -#include "cru/common/base.hpp" - -#include "cru/common/auto_delete.hpp" -#include "cru/common/ui_base.hpp" +#include "../graphic_base.hpp" +#include "../native_resource.hpp" #include "brush.hpp" #include "font.hpp" @@ -20,20 +18,42 @@ namespace cru::platform::graph { // IGraphFactory::CreateInstance and set auto-delete to true. // The manual creation method of IGraphFactory provides a you a way to use graph // related tools without interact with actual ui like window system. -struct IGraphFactory : virtual Interface, virtual IAutoDelete { +class GraphFactory : public NativeResource { + public: // Create a platform-specific instance and save it as the global instance. // Do not create the instance twice. Implements should assert for that. // After creating, get the instance by GetInstance. - static IGraphFactory* CreateInstance(); + static GraphFactory* CreateInstance(); // Get the global instance. If it is not created, then return nullptr. - static IGraphFactory* GetInstance(); - - virtual ISolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; - virtual IGeometryBuilder* CreateGeometryBuilder() = 0; - virtual IFontDescriptor* CreateFontDescriptor( - const std::wstring_view& font_family, float font_size) = 0; - virtual ITextLayout* CreateTextLayout(std::shared_ptr font, - std::wstring text) = 0; + static GraphFactory* GetInstance(); + + protected: + GraphFactory() = default; + + public: + GraphFactory(const GraphFactory& other) = delete; + GraphFactory& operator=(const GraphFactory& other) = delete; + + GraphFactory(GraphFactory&& other) = delete; + GraphFactory& operator=(GraphFactory&& other) = delete; + + ~GraphFactory() override = default; + + public: + virtual SolidColorBrush* CreateSolidColorBrush() = 0; + SolidColorBrush* CreateSolidColorBrush(const Color& color) { + const auto brush = CreateSolidColorBrush(); + brush->SetColor(color); + return brush; + } + + virtual GeometryBuilder* CreateGeometryBuilder() = 0; + + virtual Font* CreateFont(const std::wstring_view& font_family, + float font_size) = 0; + + virtual TextLayout* CreateTextLayout(std::shared_ptr font, + std::wstring text) = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/graph/painter.hpp b/include/cru/platform/graph/painter.hpp index 34c895d4..1096aa7c 100644 --- a/include/cru/platform/graph/painter.hpp +++ b/include/cru/platform/graph/painter.hpp @@ -1,26 +1,41 @@ #pragma once -#include "cru/common/base.hpp" - +#include "../graphic_base.hpp" #include "../matrix.hpp" -#include "cru/common/endable.hpp" -#include "cru/common/ui_base.hpp" +#include "../native_resource.hpp" namespace cru::platform::graph { -struct IBrush; -struct IGeometry; -struct ITextLayout; +class Brush; +class Geometry; +class TextLayout; + +class Painter : public NativeResource { + protected: + Painter() = default; + + public: + Painter(const Painter& other) = delete; + Painter& operator=(const Painter& other) = delete; + + Painter(Painter&& other) = delete; + Painter& operator=(Painter&& other) = delete; + + ~Painter() override = default; -struct IPainter : virtual Interface, virtual IEndable { + public: virtual Matrix GetTransform() = 0; virtual void SetTransform(const Matrix& matrix) = 0; - virtual void Clear(const ui::Color& color) = 0; - virtual void StrokeRectangle(const ui::Rect& rectangle, IBrush* brush, + + virtual void Clear(const Color& color) = 0; + + virtual void StrokeRectangle(const Rect& rectangle, Brush* brush, float width) = 0; - virtual void FillRectangle(const ui::Rect& rectangle, IBrush* brush) = 0; - virtual void StrokeGeometry(IGeometry* geometry, IBrush* brush, + virtual void FillRectangle(const Rect& rectangle, Brush* brush) = 0; + + virtual void StrokeGeometry(Geometry* geometry, Brush* brush, float width) = 0; - virtual void FillGeometry(IGeometry* geometry, IBrush* brush) = 0; - virtual void DrawText(const ui::Point& offset, ITextLayout* text_layout, - IBrush* brush) = 0; + virtual void FillGeometry(Geometry* geometry, Brush* brush) = 0; + + virtual void DrawText(const Point& offset, TextLayout* text_layout, + Brush* brush) = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/graph/text_layout.hpp b/include/cru/platform/graph/text_layout.hpp index 894c1408..56943098 100644 --- a/include/cru/platform/graph/text_layout.hpp +++ b/include/cru/platform/graph/text_layout.hpp @@ -1,24 +1,38 @@ #pragma once -#include "cru/common/base.hpp" - -#include "cru/common/ui_base.hpp" +#include "../graphic_base.hpp" +#include "../native_resource.hpp" #include #include #include namespace cru::platform::graph { -struct IFontDescriptor; +class Font; + +class TextLayout : public NativeResource { + protected: + TextLayout() = default; + + public: + TextLayout(const TextLayout& other) = delete; + TextLayout& operator=(const TextLayout& other) = delete; + + TextLayout(TextLayout&& other) = delete; + TextLayout& operator=(TextLayout&& other) = delete; -struct ITextLayout : virtual Interface { + ~TextLayout() override = default; + + public: virtual std::wstring GetText() = 0; virtual void SetText(std::wstring new_text) = 0; - virtual std::shared_ptr GetFont() = 0; - virtual void SetFont(std::shared_ptr font) = 0; + + virtual std::shared_ptr GetFont() = 0; + virtual void SetFont(std::shared_ptr font) = 0; + virtual void SetMaxWidth(float max_width) = 0; virtual void SetMaxHeight(float max_height) = 0; - virtual ui::Rect GetTextBounds() = 0; - virtual std::vector TextRangeRect( - const ui::TextRange& text_range) = 0; + + virtual Rect GetTextBounds() = 0; + virtual std::vector TextRangeRect(const TextRange& text_range) = 0; }; -} // namespace cru::platform +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphic_base.hpp b/include/cru/platform/graphic_base.hpp new file mode 100644 index 00000000..2aa4e2cb --- /dev/null +++ b/include/cru/platform/graphic_base.hpp @@ -0,0 +1,257 @@ +#pragma once +#include "cru/common/pre_config.hpp" + +#include +#include +#include + +namespace cru::platform { +struct Point final { + constexpr Point() = default; + constexpr Point(const float x, const float y) : x(x), y(y) {} + + float x = 0; + float y = 0; +}; + +constexpr bool operator==(const Point& left, const Point& right) { + return left.x == right.x && left.y == right.y; +} + +constexpr bool operator!=(const Point& left, const Point& right) { + return !(left == right); +} + +struct Size final { + constexpr Size() = default; + constexpr Size(const float width, const float height) + : width(width), height(height) {} + + float width = 0; + float height = 0; +}; + +constexpr Size operator+(const Size& left, const Size& right) { + return Size(left.width + right.width, left.height + right.height); +} + +constexpr Size operator-(const Size& left, const Size& right) { + return Size(left.width - right.width, left.height - right.height); +} + +constexpr bool operator==(const Size& left, const Size& right) { + return left.width == right.width && left.height == right.height; +} + +constexpr bool operator!=(const Size& left, const Size& right) { + return !(left == right); +} + +struct Thickness final { + constexpr Thickness() : Thickness(0) {} + + constexpr explicit Thickness(const float width) + : left(width), top(width), right(width), bottom(width) {} + + constexpr explicit Thickness(const float horizontal, const float vertical) + : left(horizontal), top(vertical), right(horizontal), bottom(vertical) {} + + constexpr Thickness(const float left, const float top, const float right, + const float bottom) + : left(left), top(top), right(right), bottom(bottom) {} + + constexpr float GetHorizontalTotal() const { return left + right; } + + constexpr float GetVerticalTotal() const { return top + bottom; } + + void SetLeftRight(const float value) { left = right = value; } + + void SetTopBottom(const float value) { top = bottom = value; } + + void SetAll(const float value) { left = top = right = bottom = value; } + + constexpr float Validate() const { + return left >= 0.0 && top >= 0.0 && right >= 0.0 && bottom >= 0.0; + } + + float left; + float top; + float right; + float bottom; +}; + +constexpr bool operator==(const Thickness& left, const Thickness& right) { + return left.left == right.left && left.top == right.top && + left.right == right.right && left.bottom == right.bottom; +} + +constexpr bool operator!=(const Thickness& left, const Thickness& right) { + return !(left == right); +} + +struct Rect final { + constexpr Rect() = default; + constexpr Rect(const float left, const float top, const float width, + const float height) + : left(left), top(top), width(width), height(height) {} + constexpr Rect(const Point& lefttop, const Size& size) + : left(lefttop.x), + top(lefttop.y), + width(size.width), + height(size.height) {} + + constexpr static Rect FromVertices(const float left, const float top, + const float right, const float bottom) { + return Rect(left, top, right - left, bottom - top); + } + + constexpr static Rect FromCenter(const Point& center, const float width, + const float height) { + return Rect(center.x - width / 2.0f, center.y - height / 2.0f, width, + height); + } + + constexpr float GetRight() const { return left + width; } + + constexpr float GetBottom() const { return top + height; } + + constexpr Point GetLeftTop() const { return Point(left, top); } + + constexpr Point GetRightBottom() const { + return Point(left + width, top + height); + } + + constexpr Point GetLeftBottom() const { return Point(left, top + height); } + + constexpr Point GetRightTop() const { return Point(left + width, top); } + + constexpr Point GetCenter() const { + return Point(left + width / 2.0f, top + height / 2.0f); + } + + constexpr Size GetSize() const { return Size(width, height); } + + constexpr Rect Shrink(const Thickness& thickness) const { + return Rect(left + thickness.left, top + thickness.top, + width - thickness.GetHorizontalTotal(), + height - thickness.GetVerticalTotal()); + } + + constexpr bool IsPointInside(const Point& point) const { + return point.x >= left && point.x < GetRight() && point.y >= top && + point.y < GetBottom(); + } + + float left = 0.0f; + float top = 0.0f; + float width = 0.0f; + float height = 0.0f; +}; + +constexpr bool operator==(const Rect& left, const Rect& right) { + return left.left == right.left && left.top == right.top && + left.width == right.width && left.height == right.height; +} + +constexpr bool operator!=(const Rect& left, const Rect& right) { + return !(left == right); +} + +struct RoundedRect final { + constexpr RoundedRect() = default; + constexpr RoundedRect(const Rect& rect, const float radius_x, + const float radius_y) + : rect(rect), radius_x(radius_x), radius_y(radius_y) {} + + Rect rect{}; + float radius_x = 0.0f; + float radius_y = 0.0f; +}; + +constexpr bool operator==(const RoundedRect& left, const RoundedRect& right) { + return left.rect == right.rect && left.radius_x == right.radius_x && + left.radius_y == right.radius_y; +} + +constexpr bool operator!=(const RoundedRect& left, const RoundedRect& right) { + return !(left == right); +} + +struct Ellipse final { + constexpr Ellipse() = default; + constexpr Ellipse(const Point& center, const float radius_x, + const float radius_y) + : center(center), radius_x(radius_x), radius_y(radius_y) {} + + constexpr static Ellipse FromRect(const Rect& rect) { + return Ellipse(rect.GetCenter(), rect.width / 2.0f, rect.height / 2.0f); + } + + constexpr Rect GetBoundRect() const { + return Rect::FromCenter(center, radius_x * 2.0f, radius_y * 2.0f); + } + + Point center{}; + float radius_x = 0.0f; + float radius_y = 0.0f; +}; + +constexpr bool operator==(const Ellipse& left, const Ellipse& right) { + return left.center == right.center && left.radius_x == right.radius_x && + left.radius_y == right.radius_y; +} + +constexpr bool operator!=(const Ellipse& left, const Ellipse& right) { + return !(left == right); +} + +struct TextRange final { + constexpr static std::optional FromTwoSides(unsigned first, + unsigned second) { + if (first > second) + return std::make_optional(second, first - second); + if (first < second) + return std::make_optional(first, second - first); + return std::nullopt; + } + + constexpr static std::pair ToTwoSides( + std::optional text_range, unsigned default_position = 0) { + if (text_range.has_value()) + return std::make_pair( + text_range.value().position, + text_range.value().position + text_range.value().count); + return std::make_pair(default_position, default_position); + } + + constexpr TextRange() = default; + constexpr TextRange(const unsigned position, const unsigned count) + : position(position), count(count) {} + + unsigned position = 0; + unsigned count = 0; +}; + +struct Color { + constexpr Color() : Color(0, 0, 0, 255) {} + constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, + std::uint8_t alpha = 255) + : red(red), green(green), blue(blue), alpha(alpha) {} + + constexpr static Color FromHex(std::uint32_t hex) { + return Color(hex & (0b11111111 << 16), hex & (0b11111111 << 8), + hex & (0b11111111), hex & (0b11111111 << 24)); + } + + std::uint8_t red; + std::uint8_t green; + std::uint8_t blue; + std::uint8_t alpha; +}; + +namespace colors { +constexpr Color black{0, 0, 0}; +constexpr Color white{255, 255, 255}; +constexpr Color skyblue = Color::FromHex(0x87ceeb); +} // namespace colors +} // namespace cru::ui diff --git a/include/cru/platform/matrix.hpp b/include/cru/platform/matrix.hpp index 37e4725e..b0165a88 100644 --- a/include/cru/platform/matrix.hpp +++ b/include/cru/platform/matrix.hpp @@ -1,7 +1,5 @@ #pragma once -#include "cru/common/pre_config.hpp" - -#include "cru/common/ui_base.hpp" +#include "graphic_base.hpp" #include diff --git a/include/cru/platform/native_resource.hpp b/include/cru/platform/native_resource.hpp new file mode 100644 index 00000000..787c46a5 --- /dev/null +++ b/include/cru/platform/native_resource.hpp @@ -0,0 +1,22 @@ +#pragma once +#include "cru/common/base.hpp" + +#include + +namespace cru::platform { +class NativeResource : public Object { + protected: + NativeResource() = default; + + public: + NativeResource(NativeResource&& other) = delete; + NativeResource& operator=(NativeResource&& other) = delete; + + NativeResource(NativeResource&& other) = delete; + NativeResource& operator=(NativeResource&& other) = delete; + ~NativeResource() override = default; + + public: + virtual std::wstring_view GetPlatformId() const = 0; +}; +} // namespace cru::platform diff --git a/include/cru/win/exception.hpp b/include/cru/win/exception.hpp index 39c52c45..9e038960 100644 --- a/include/cru/win/exception.hpp +++ b/include/cru/win/exception.hpp @@ -6,7 +6,7 @@ #include #include -namespace cru::win { +namespace cru::platform::win { class HResultError : public platform::PlatformException { public: explicit HResultError(HRESULT h_result); diff --git a/include/cru/win/graph/direct/brush.hpp b/include/cru/win/graph/direct/brush.hpp new file mode 100644 index 00000000..9775b5c1 --- /dev/null +++ b/include/cru/win/graph/direct/brush.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "com_resource.hpp" +#include "direct_factory.hpp" + +#include "cru/platform/graph/brush.hpp" + +namespace cru::platform::graph::win::direct { +class D2DSolidColorBrush : public SolidColorBrush, + public IComResource { + public: + explicit D2DSolidColorBrush(IDirectFactory* factory); + D2DSolidColorBrush(const D2DSolidColorBrush& other) = delete; + D2DSolidColorBrush(D2DSolidColorBrush&& other) = delete; + D2DSolidColorBrush& operator=(const D2DSolidColorBrush& other) = delete; + D2DSolidColorBrush& operator=(D2DSolidColorBrush&& other) = delete; + ~D2DSolidColorBrush() override = default; + + ID2D1SolidColorBrush* GetComInterface() override { return brush_.Get(); } + + protected: + void OnSetColor(const Color& color) override; + + private: + Microsoft::WRL::ComPtr brush_; +}; +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/com_resource.hpp b/include/cru/win/graph/direct/com_resource.hpp new file mode 100644 index 00000000..aa2c366b --- /dev/null +++ b/include/cru/win/graph/direct/com_resource.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "../../win_pre_config.hpp" + +namespace cru::platform::graph::win::direct { +template +struct IComResource { + virtual ~IComResource() = default; + + virtual TInterface* GetComInterface() = 0; +}; +} // namespace cru::platform::graph::win_direct diff --git a/include/cru/win/graph/direct/convert_util.hpp b/include/cru/win/graph/direct/convert_util.hpp new file mode 100644 index 00000000..2c45c63a --- /dev/null +++ b/include/cru/win/graph/direct/convert_util.hpp @@ -0,0 +1,108 @@ +#pragma once +#include "../../win_pre_config.hpp" + +#include "cru/platform/graphic_base.hpp" +#include "cru/platform/matrix.hpp" + +namespace cru::platform::graph::win::direct { +inline D2D1_MATRIX_3X2_F Convert(const platform::Matrix& matrix) { + D2D1_MATRIX_3X2_F m; + m._11 = matrix.m11; + m._12 = matrix.m12; + m._21 = matrix.m21; + m._22 = matrix.m22; + m._31 = matrix.m31; + m._32 = matrix.m32; + return m; +} + +inline D2D1_COLOR_F Convert(const Color& color) { + return D2D1::ColorF(color.red / 255.0f, color.green / 255.0f, + color.blue / 255.0f, color.alpha / 255.0f); +} + +inline D2D1_POINT_2F Convert(const Point& point) { + return D2D1::Point2F(point.x, point.y); +} + +inline D2D1_RECT_F Convert(const Rect& rect) { + return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, + rect.top + rect.height); +} + +inline D2D1_ROUNDED_RECT Convert(const RoundedRect& rounded_rect) { + return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, + rounded_rect.radius_y); +} + +inline D2D1_ELLIPSE Convert(const Ellipse& ellipse) { + return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, + ellipse.radius_y); +} + +inline platform::Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { + return platform::Matrix{matrix._11, matrix._12, matrix._21, + matrix._22, matrix._31, matrix._32}; +} + +inline Color Convert(const D2D1_COLOR_F& color) { + auto floor = [](float n) { return static_cast(n + 0.5f); }; + return Color{floor(color.r * 255.0f), floor(color.g * 255.0f), + floor(color.b * 255.0f), floor(color.a * 255.0f)}; +} + +inline Point Convert(const D2D1_POINT_2F& point) { + return Point(point.x, point.y); +} + +inline Rect Convert(const D2D1_RECT_F& rect) { + return Rect(rect.left, rect.top, rect.right - rect.left, + rect.bottom - rect.top); +} + +inline RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { + return RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, + rounded_rect.radiusY); +} + +inline Ellipse Convert(const D2D1_ELLIPSE& ellipse) { + return Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); +} + +inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return left.x == right.x && left.y == right.y; +} + +inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return left.left == right.left && left.top == right.top && + left.right == right.right && left.bottom == right.bottom; +} + +inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return left.rect == right.rect && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return left.point == right.point && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return !(left == right); +} +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/direct_factory.hpp b/include/cru/win/graph/direct/direct_factory.hpp new file mode 100644 index 00000000..b150d5aa --- /dev/null +++ b/include/cru/win/graph/direct/direct_factory.hpp @@ -0,0 +1,16 @@ +#pragma once +#include "../../win_pre_config.hpp" + +namespace cru::platform::graph::win::direct { +// Interface provides access to root d2d resources. +struct IDirectFactory { + virtual ~IDirectFactory() = default; + + virtual ID2D1Factory1* GetD2D1Factory() const = 0; + virtual ID2D1DeviceContext* GetD2D1DeviceContext() const = 0; + virtual ID3D11Device* GetD3D11Device() const = 0; + virtual IDXGIFactory2* GetDxgiFactory() const = 0; + virtual IDWriteFactory* GetDWriteFactory() const = 0; + virtual IDWriteFontCollection* GetSystemFontCollection() const = 0; +}; +} // namespace cru::platform::graph::win::direct diff --git a/include/cru/win/graph/direct/exception.hpp b/include/cru/win/graph/direct/exception.hpp new file mode 100644 index 00000000..bfa14aaf --- /dev/null +++ b/include/cru/win/graph/direct/exception.hpp @@ -0,0 +1,36 @@ +#pragma once +#include "../../win_pre_config.hpp" + +#include "cru/platform/exception.hpp" + +#include +#include + +namespace cru::platform::graph::win::direct { + +class HResultError : public PlatformException { + public: + explicit HResultError(HRESULT h_result); + explicit HResultError(HRESULT h_result, + const std::string_view& additional_message); + HResultError(const HResultError& other) = default; + HResultError(HResultError&& other) = default; + HResultError& operator=(const HResultError& other) = default; + HResultError& operator=(HResultError&& other) = default; + ~HResultError() override = default; + + HRESULT GetHResult() const { return h_result_; } + + private: + HRESULT h_result_; +}; + +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) { + if (FAILED(h_result)) throw HResultError(h_result, message); +} +} // namespace cru::platform::graph::win::direct \ No newline at end of file diff --git a/include/cru/win/graph/direct/font.hpp b/include/cru/win/graph/direct/font.hpp new file mode 100644 index 00000000..44fa7edd --- /dev/null +++ b/include/cru/win/graph/direct/font.hpp @@ -0,0 +1,28 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/graph/font.hpp" + +#include + +namespace cru::win::graph { +struct IWinNativeFactory; + +class WinFontDescriptor : public Object, + public virtual platform::graph::IFontDescriptor { + public: + explicit WinFontDescriptor(IWinNativeFactory* factory, + const std::wstring_view& font_family, + float font_size); + WinFontDescriptor(const WinFontDescriptor& other) = delete; + WinFontDescriptor(WinFontDescriptor&& other) = delete; + WinFontDescriptor& operator=(const WinFontDescriptor& other) = delete; + WinFontDescriptor& operator=(WinFontDescriptor&& other) = delete; + ~WinFontDescriptor() override = default; + + IDWriteTextFormat* GetDWriteTextFormat() const { return text_format_.Get(); } + + private: + Microsoft::WRL::ComPtr text_format_; +}; +} // namespace cru::win::graph diff --git a/include/cru/win/graph/direct/geometry.hpp b/include/cru/win/graph/direct/geometry.hpp new file mode 100644 index 00000000..e312f13c --- /dev/null +++ b/include/cru/win/graph/direct/geometry.hpp @@ -0,0 +1,48 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/graph/geometry.hpp" + +namespace cru::win::graph { +struct IWinNativeFactory; + +class WinGeometryBuilder : public Object, + public virtual platform::graph::IGeometryBuilder { + public: + explicit WinGeometryBuilder(IWinNativeFactory* factory); + WinGeometryBuilder(const WinGeometryBuilder& other) = delete; + WinGeometryBuilder(WinGeometryBuilder&& other) = delete; + WinGeometryBuilder& operator=(const WinGeometryBuilder& other) = delete; + WinGeometryBuilder& operator=(WinGeometryBuilder&& other) = delete; + ~WinGeometryBuilder() override; + + void BeginFigure(const ui::Point& point) override; + void LineTo(const ui::Point& point) override; + void QuadraticBezierTo(const ui::Point& control_point, + const ui::Point& end_point) override; + void CloseFigure(bool close) override; + platform::graph::IGeometry* End() override; + bool IsEnded() const override { return geometry_ != nullptr; } + + private: + Microsoft::WRL::ComPtr geometry_; + Microsoft::WRL::ComPtr geometry_sink_; +}; + +class WinGeometry : public Object, public virtual platform::graph::IGeometry { + public: + explicit WinGeometry(Microsoft::WRL::ComPtr geometry); + WinGeometry(const WinGeometry& other) = delete; + WinGeometry(WinGeometry&& other) = delete; + WinGeometry& operator=(const WinGeometry& other) = delete; + WinGeometry& operator=(WinGeometry&& other) = delete; + ~WinGeometry() override = default; + + bool FillContains(const ui::Point& point) override; + + ID2D1PathGeometry* GetNative() const { return geometry_.Get(); } + + private: + Microsoft::WRL::ComPtr geometry_; +}; +} // namespace cru::win::graph diff --git a/include/cru/win/graph/direct/graph_factory.hpp b/include/cru/win/graph/direct/graph_factory.hpp new file mode 100644 index 00000000..b3c901be --- /dev/null +++ b/include/cru/win/graph/direct/graph_factory.hpp @@ -0,0 +1,60 @@ +#pragma once +#include "direct_factory.hpp" + +#include "brush.hpp" + +#include "cru/platform/graph/graph_factory.hpp" + +namespace cru::platform::graph::win::direct { +class DirectGraphFactory : public GraphFactory, IDirectFactory { + friend GraphFactory* GraphFactory::CreateInstance(); + + public: + static DirectGraphFactory* GetInstance(); + + private: + DirectGraphFactory(); + + public: + DirectGraphFactory(const DirectGraphFactory& other) = delete; + DirectGraphFactory(DirectGraphFactory&& other) = delete; + DirectGraphFactory& operator=(const DirectGraphFactory& other) = delete; + DirectGraphFactory& operator=(DirectGraphFactory&& other) = delete; + ~DirectGraphFactory() override; + + ID2D1Factory1* GetD2D1Factory() const override { return d2d1_factory_.Get(); } + ID2D1DeviceContext* GetD2D1DeviceContext() const override { + return d2d1_device_context_.Get(); + } + ID3D11Device* GetD3D11Device() const override { return d3d11_device_.Get(); } + IDXGIFactory2* GetDxgiFactory() const override { return dxgi_factory_.Get(); } + IDWriteFactory* GetDWriteFactory() const override { + return dwrite_factory_.Get(); + } + IDWriteFontCollection* GetSystemFontCollection() const override { + return dwrite_system_font_collection_.Get(); + } + + D2DSolidColorBrush* CreateSolidColorBrush() override; + + D2DGeometryBuilder* CreateGeometryBuilder() override; + D2DFont* CreateFont( + const std::wstring_view& font_family, float font_size) override; + DWriteTextLayout* CreateTextLayout( + std::shared_ptr font, + std::wstring text) override; + + bool IsAutoDelete() const override { return auto_delete_; } + void SetAutoDelete(bool value) override { auto_delete_ = value; } + + private: + bool auto_delete_ = false; + + Microsoft::WRL::ComPtr d3d11_device_; + Microsoft::WRL::ComPtr d2d1_factory_; + Microsoft::WRL::ComPtr d2d1_device_context_; + Microsoft::WRL::ComPtr dxgi_factory_; + Microsoft::WRL::ComPtr dwrite_factory_; + Microsoft::WRL::ComPtr dwrite_system_font_collection_; +}; +} // namespace cru::win::graph diff --git a/include/cru/win/graph/direct/painter.hpp b/include/cru/win/graph/direct/painter.hpp new file mode 100644 index 00000000..f218488c --- /dev/null +++ b/include/cru/win/graph/direct/painter.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/graph/painter.hpp" + +namespace cru::win::graph { +class GraphManager; + +class WinPainter : public Object, public virtual platform::graph::IPainter { + public: + explicit WinPainter(ID2D1RenderTarget* render_target); + WinPainter(const WinPainter& other) = delete; + WinPainter(WinPainter&& other) = delete; + WinPainter& operator=(const WinPainter& other) = delete; + WinPainter& operator=(WinPainter&& other) = delete; + ~WinPainter() override = default; + + platform::Matrix GetTransform() override; + void SetTransform(const platform::Matrix& matrix) override; + void Clear(const ui::Color& color) override; + void StrokeRectangle(const ui::Rect& rectangle, platform::graph::IBrush* brush, + float width) override; + void FillRectangle(const ui::Rect& rectangle, + platform::graph::IBrush* brush) override; + void StrokeGeometry(platform::graph::IGeometry* geometry, + platform::graph::IBrush* brush, float width) override; + void FillGeometry(platform::graph::IGeometry* geometry, + platform::graph::IBrush* brush) override; + void DrawText(const ui::Point& offset, + platform::graph::ITextLayout* text_layout, + platform::graph::IBrush* brush) override; + void End() override final; + bool IsEnded() const override final { return is_draw_ended_; } + + protected: + virtual void DoEndDraw() = 0; + + private: + ID2D1RenderTarget* render_target_; + + bool is_draw_ended_ = false; +}; +} // namespace cru::win::graph diff --git a/include/cru/win/graph/direct/text_layout.hpp b/include/cru/win/graph/direct/text_layout.hpp new file mode 100644 index 00000000..7339eff9 --- /dev/null +++ b/include/cru/win/graph/direct/text_layout.hpp @@ -0,0 +1,42 @@ +#pragma once +#include "../win_pre_config.hpp" + +#include "cru/platform/graph/text_layout.hpp" + +#include + +namespace cru::win::graph { +struct IWinNativeFactory; +class WinFontDescriptor; + +class WinTextLayout : public Object, public virtual platform::graph::ITextLayout { + public: + explicit WinTextLayout(IWinNativeFactory* factory, + std::shared_ptr 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 = default; + + std::wstring GetText() override; + void SetText(std::wstring new_text) override; + std::shared_ptr GetFont(); + void SetFont(std::shared_ptr font); + void SetMaxWidth(float max_width) override; + void SetMaxHeight(float max_height) override; + ui::Rect GetTextBounds() override; + std::vector TextRangeRect( + const ui::TextRange& text_range) override; + + IDWriteTextLayout* GetDWriteTextLayout() const { return text_layout_.Get(); } + + private: + IWinNativeFactory* factory_; + std::wstring text_; + std::shared_ptr font_descriptor_; + float max_width_ = 0.0f; + float max_height_ = 0.0f; + Microsoft::WRL::ComPtr text_layout_; +}; +} // namespace cru::platform::win diff --git a/include/cru/win/graph/util/convert_util.hpp b/include/cru/win/graph/util/convert_util.hpp deleted file mode 100644 index 4b5368b1..00000000 --- a/include/cru/win/graph/util/convert_util.hpp +++ /dev/null @@ -1,108 +0,0 @@ -#pragma once -#include "../../win_pre_config.hpp" - -#include "cru/common/ui_base.hpp" -#include "cru/platform/matrix.hpp" - -namespace cru::win::graph::util { -inline D2D1_MATRIX_3X2_F Convert(const platform::Matrix& matrix) { - D2D1_MATRIX_3X2_F m; - m._11 = matrix.m11; - m._12 = matrix.m12; - m._21 = matrix.m21; - m._22 = matrix.m22; - m._31 = matrix.m31; - m._32 = matrix.m32; - return m; -} - -inline D2D1_COLOR_F Convert(const ui::Color& color) { - return D2D1::ColorF(color.red / 255.0f, color.green / 255.0f, - color.blue / 255.0f, color.alpha / 255.0f); -} - -inline D2D1_POINT_2F Convert(const ui::Point& point) { - return D2D1::Point2F(point.x, point.y); -} - -inline D2D1_RECT_F Convert(const ui::Rect& rect) { - return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, - rect.top + rect.height); -} - -inline D2D1_ROUNDED_RECT Convert(const ui::RoundedRect& rounded_rect) { - return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, - rounded_rect.radius_y); -} - -inline D2D1_ELLIPSE Convert(const ui::Ellipse& ellipse) { - return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, - ellipse.radius_y); -} - -inline platform::Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { - return platform::Matrix{matrix._11, matrix._12, matrix._21, - matrix._22, matrix._31, matrix._32}; -} - -inline ui::Color Convert(const D2D1_COLOR_F& color) { - auto floor = [](float n) { return static_cast(n + 0.5f); }; - return ui::Color{floor(color.r * 255.0f), floor(color.g * 255.0f), - floor(color.b * 255.0f), floor(color.a * 255.0f)}; -} - -inline ui::Point Convert(const D2D1_POINT_2F& point) { - return ui::Point(point.x, point.y); -} - -inline ui::Rect Convert(const D2D1_RECT_F& rect) { - return ui::Rect(rect.left, rect.top, rect.right - rect.left, - rect.bottom - rect.top); -} - -inline ui::RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { - return ui::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, - rounded_rect.radiusY); -} - -inline ui::Ellipse Convert(const D2D1_ELLIPSE& ellipse) { - return ui::Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); -} - -inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { - return left.x == right.x && left.y == right.y; -} - -inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { - return !(left == right); -} - -inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { - return left.left == right.left && left.top == right.top && - left.right == right.right && left.bottom == right.bottom; -} - -inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { - return !(left == right); -} - -inline bool operator==(const D2D1_ROUNDED_RECT& left, - const D2D1_ROUNDED_RECT& right) { - return left.rect == right.rect && left.radiusX == right.radiusX && - left.radiusY == right.radiusY; -} - -inline bool operator!=(const D2D1_ROUNDED_RECT& left, - const D2D1_ROUNDED_RECT& right) { - return !(left == right); -} - -inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { - return left.point == right.point && left.radiusX == right.radiusX && - left.radiusY == right.radiusY; -} - -inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { - return !(left == right); -} -} // namespace cru::win::graph::util diff --git a/include/cru/win/graph/win_brush.hpp b/include/cru/win/graph/win_brush.hpp deleted file mode 100644 index c81019c0..00000000 --- a/include/cru/win/graph/win_brush.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/platform/graph/brush.hpp" - -namespace cru::win::graph { -struct IWinNativeFactory; - -struct IWinBrush : virtual platform::graph::IBrush { - virtual ID2D1Brush* GetD2DBrush() = 0; -}; - -class WinSolidColorBrush : public Object, - public virtual platform::graph::ISolidColorBrush, - public virtual IWinBrush { - public: - WinSolidColorBrush(IWinNativeFactory* factory, const ui::Color& color); - WinSolidColorBrush(const WinSolidColorBrush& other) = delete; - WinSolidColorBrush(WinSolidColorBrush&& other) = delete; - WinSolidColorBrush& operator=(const WinSolidColorBrush& other) = delete; - WinSolidColorBrush& operator=(WinSolidColorBrush&& other) = delete; - ~WinSolidColorBrush() override = default; - - ui::Color GetColor() override; - void SetColor(const ui::Color& color) override; - - ID2D1Brush* GetD2DBrush() override { return brush_.Get(); } - - private: - Microsoft::WRL::ComPtr brush_; -}; -} // namespace cru::win::graph diff --git a/include/cru/win/graph/win_font.hpp b/include/cru/win/graph/win_font.hpp deleted file mode 100644 index 44fa7edd..00000000 --- a/include/cru/win/graph/win_font.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/platform/graph/font.hpp" - -#include - -namespace cru::win::graph { -struct IWinNativeFactory; - -class WinFontDescriptor : public Object, - public virtual platform::graph::IFontDescriptor { - public: - explicit WinFontDescriptor(IWinNativeFactory* factory, - const std::wstring_view& font_family, - float font_size); - WinFontDescriptor(const WinFontDescriptor& other) = delete; - WinFontDescriptor(WinFontDescriptor&& other) = delete; - WinFontDescriptor& operator=(const WinFontDescriptor& other) = delete; - WinFontDescriptor& operator=(WinFontDescriptor&& other) = delete; - ~WinFontDescriptor() override = default; - - IDWriteTextFormat* GetDWriteTextFormat() const { return text_format_.Get(); } - - private: - Microsoft::WRL::ComPtr text_format_; -}; -} // namespace cru::win::graph diff --git a/include/cru/win/graph/win_geometry.hpp b/include/cru/win/graph/win_geometry.hpp deleted file mode 100644 index e312f13c..00000000 --- a/include/cru/win/graph/win_geometry.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/platform/graph/geometry.hpp" - -namespace cru::win::graph { -struct IWinNativeFactory; - -class WinGeometryBuilder : public Object, - public virtual platform::graph::IGeometryBuilder { - public: - explicit WinGeometryBuilder(IWinNativeFactory* factory); - WinGeometryBuilder(const WinGeometryBuilder& other) = delete; - WinGeometryBuilder(WinGeometryBuilder&& other) = delete; - WinGeometryBuilder& operator=(const WinGeometryBuilder& other) = delete; - WinGeometryBuilder& operator=(WinGeometryBuilder&& other) = delete; - ~WinGeometryBuilder() override; - - void BeginFigure(const ui::Point& point) override; - void LineTo(const ui::Point& point) override; - void QuadraticBezierTo(const ui::Point& control_point, - const ui::Point& end_point) override; - void CloseFigure(bool close) override; - platform::graph::IGeometry* End() override; - bool IsEnded() const override { return geometry_ != nullptr; } - - private: - Microsoft::WRL::ComPtr geometry_; - Microsoft::WRL::ComPtr geometry_sink_; -}; - -class WinGeometry : public Object, public virtual platform::graph::IGeometry { - public: - explicit WinGeometry(Microsoft::WRL::ComPtr geometry); - WinGeometry(const WinGeometry& other) = delete; - WinGeometry(WinGeometry&& other) = delete; - WinGeometry& operator=(const WinGeometry& other) = delete; - WinGeometry& operator=(WinGeometry&& other) = delete; - ~WinGeometry() override = default; - - bool FillContains(const ui::Point& point) override; - - ID2D1PathGeometry* GetNative() const { return geometry_.Get(); } - - private: - Microsoft::WRL::ComPtr geometry_; -}; -} // namespace cru::win::graph diff --git a/include/cru/win/graph/win_graph_factory.hpp b/include/cru/win/graph/win_graph_factory.hpp deleted file mode 100644 index 2df7ed9a..00000000 --- a/include/cru/win/graph/win_graph_factory.hpp +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/platform/graph/graph_factory.hpp" -#include "win_native_factory.hpp" - -namespace cru::win::graph { -class WinGraphFactory : public Object, - public virtual platform::graph::IGraphFactory, - public virtual IWinNativeFactory { - friend IGraphFactory* IGraphFactory::CreateInstance(); - - public: - static WinGraphFactory* GetInstance(); - - private: - explicit WinGraphFactory(); - - public: - WinGraphFactory(const WinGraphFactory& other) = delete; - WinGraphFactory(WinGraphFactory&& other) = delete; - WinGraphFactory& operator=(const WinGraphFactory& other) = delete; - WinGraphFactory& operator=(WinGraphFactory&& other) = delete; - ~WinGraphFactory() override; - - ID2D1Factory1* GetD2D1Factory() const override { return d2d1_factory_.Get(); } - ID2D1DeviceContext* GetD2D1DeviceContext() const override { - return d2d1_device_context_.Get(); - } - ID3D11Device* GetD3D11Device() const override { return d3d11_device_.Get(); } - IDXGIFactory2* GetDxgiFactory() const override { return dxgi_factory_.Get(); } - IDWriteFactory* GetDWriteFactory() const override { - return dwrite_factory_.Get(); - } - IDWriteFontCollection* GetSystemFontCollection() const override { - return dwrite_system_font_collection_.Get(); - } - - platform::graph::ISolidColorBrush* CreateSolidColorBrush( - const ui::Color& color) override; - platform::graph::IGeometryBuilder* CreateGeometryBuilder() override; - platform::graph::IFontDescriptor* CreateFontDescriptor( - const std::wstring_view& font_family, float font_size); - platform::graph::ITextLayout* CreateTextLayout( - std::shared_ptr font, - std::wstring text); - - bool IsAutoDelete() const override { return auto_delete_; } - void SetAutoDelete(bool value) override { auto_delete_ = value; } - - private: - bool auto_delete_ = false; - - Microsoft::WRL::ComPtr d3d11_device_; - Microsoft::WRL::ComPtr d2d1_factory_; - Microsoft::WRL::ComPtr d2d1_device_context_; - Microsoft::WRL::ComPtr dxgi_factory_; - Microsoft::WRL::ComPtr dwrite_factory_; - Microsoft::WRL::ComPtr dwrite_system_font_collection_; -}; -} // namespace cru::win::graph diff --git a/include/cru/win/graph/win_native_factory.hpp b/include/cru/win/graph/win_native_factory.hpp deleted file mode 100644 index f5ad033c..00000000 --- a/include/cru/win/graph/win_native_factory.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/common/base.hpp" - -namespace cru::win::graph { -// Interface provides access to root d2d resources. -struct IWinNativeFactory : virtual Interface { - virtual ID2D1Factory1* GetD2D1Factory() const = 0; - virtual ID2D1DeviceContext* GetD2D1DeviceContext() const = 0; - virtual ID3D11Device* GetD3D11Device() const = 0; - virtual IDXGIFactory2* GetDxgiFactory() const = 0; - virtual IDWriteFactory* GetDWriteFactory() const = 0; - virtual IDWriteFontCollection* GetSystemFontCollection() const = 0; -}; -} diff --git a/include/cru/win/graph/win_painter.hpp b/include/cru/win/graph/win_painter.hpp deleted file mode 100644 index f218488c..00000000 --- a/include/cru/win/graph/win_painter.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/platform/graph/painter.hpp" - -namespace cru::win::graph { -class GraphManager; - -class WinPainter : public Object, public virtual platform::graph::IPainter { - public: - explicit WinPainter(ID2D1RenderTarget* render_target); - WinPainter(const WinPainter& other) = delete; - WinPainter(WinPainter&& other) = delete; - WinPainter& operator=(const WinPainter& other) = delete; - WinPainter& operator=(WinPainter&& other) = delete; - ~WinPainter() override = default; - - platform::Matrix GetTransform() override; - void SetTransform(const platform::Matrix& matrix) override; - void Clear(const ui::Color& color) override; - void StrokeRectangle(const ui::Rect& rectangle, platform::graph::IBrush* brush, - float width) override; - void FillRectangle(const ui::Rect& rectangle, - platform::graph::IBrush* brush) override; - void StrokeGeometry(platform::graph::IGeometry* geometry, - platform::graph::IBrush* brush, float width) override; - void FillGeometry(platform::graph::IGeometry* geometry, - platform::graph::IBrush* brush) override; - void DrawText(const ui::Point& offset, - platform::graph::ITextLayout* text_layout, - platform::graph::IBrush* brush) override; - void End() override final; - bool IsEnded() const override final { return is_draw_ended_; } - - protected: - virtual void DoEndDraw() = 0; - - private: - ID2D1RenderTarget* render_target_; - - bool is_draw_ended_ = false; -}; -} // namespace cru::win::graph diff --git a/include/cru/win/graph/win_text_layout.hpp b/include/cru/win/graph/win_text_layout.hpp deleted file mode 100644 index 7339eff9..00000000 --- a/include/cru/win/graph/win_text_layout.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include "../win_pre_config.hpp" - -#include "cru/platform/graph/text_layout.hpp" - -#include - -namespace cru::win::graph { -struct IWinNativeFactory; -class WinFontDescriptor; - -class WinTextLayout : public Object, public virtual platform::graph::ITextLayout { - public: - explicit WinTextLayout(IWinNativeFactory* factory, - std::shared_ptr 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 = default; - - std::wstring GetText() override; - void SetText(std::wstring new_text) override; - std::shared_ptr GetFont(); - void SetFont(std::shared_ptr font); - void SetMaxWidth(float max_width) override; - void SetMaxHeight(float max_height) override; - ui::Rect GetTextBounds() override; - std::vector TextRangeRect( - const ui::TextRange& text_range) override; - - IDWriteTextLayout* GetDWriteTextLayout() const { return text_layout_.Get(); } - - private: - IWinNativeFactory* factory_; - std::wstring text_; - std::shared_ptr font_descriptor_; - float max_width_ = 0.0f; - float max_height_ = 0.0f; - Microsoft::WRL::ComPtr text_layout_; -}; -} // namespace cru::platform::win diff --git a/include/cru/win/win_pre_config.hpp b/include/cru/win/win_pre_config.hpp index 6962eb7b..7466a62c 100644 --- a/include/cru/win/win_pre_config.hpp +++ b/include/cru/win/win_pre_config.hpp @@ -5,6 +5,7 @@ #include #undef CreateWindow #undef DrawText +#undef CreateFont #include #include 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