diff options
Diffstat (limited to 'include/cru')
-rw-r--r-- | include/cru/platform/Color.h | 12 | ||||
-rw-r--r-- | include/cru/platform/GraphicsBase.h | 23 | ||||
-rw-r--r-- | include/cru/platform/graphics/Geometry.h | 2 | ||||
-rw-r--r-- | include/cru/platform/graphics/SvgGeometry.h | 57 |
4 files changed, 76 insertions, 18 deletions
diff --git a/include/cru/platform/Color.h b/include/cru/platform/Color.h index 2b38138a..0d7bce5b 100644 --- a/include/cru/platform/Color.h +++ b/include/cru/platform/Color.h @@ -257,11 +257,6 @@ extern const std::unordered_map<StringView, Color> predefined_name_color_map; std::optional<Color> GetPredefinedColorByName(StringView name); -inline String ToString(const Color& color) { - return cru::Format(u"rgba({}, {}, {}, {})", color.red, color.green, - color.blue, color.alpha); -} - struct CRU_PLATFORM_API HslColor { HslColor() = default; HslColor(float h, float s, float l, float a = 1.0f) @@ -276,3 +271,10 @@ struct CRU_PLATFORM_API HslColor { float a; }; } // namespace cru::platform + +namespace cru { +inline String ToString(const platform::Color& color) { + return cru::Format(u"rgba({}, {}, {}, {})", color.red, color.green, + color.blue, color.alpha); +} +} // namespace cru diff --git a/include/cru/platform/GraphicsBase.h b/include/cru/platform/GraphicsBase.h index f134e74d..88dc6ee2 100644 --- a/include/cru/platform/GraphicsBase.h +++ b/include/cru/platform/GraphicsBase.h @@ -5,11 +5,7 @@ #include "cru/common/Range.h" #include "cru/common/String.h" -#include <cstdint> #include <limits> -#include <optional> -#include <string> -#include <utility> namespace cru::platform { struct Size; @@ -47,10 +43,6 @@ constexpr bool operator!=(const Point& left, const Point& right) { return !(left == right); } -inline String ToString(const Point& point) { - return Format(u"(x: {}, y: {})", point.x, point.y); -} - struct CRU_PLATFORM_API Size final { static const Size kMax; static const Size kZero; @@ -88,10 +80,6 @@ constexpr bool operator!=(const Size& left, const Size& right) { return !(left == right); } -inline String ToString(const Size& size) { - return Format(u"(width: {}, height: {})", size.width, size.height); -} - struct Thickness final { constexpr Thickness() : Thickness(0) {} @@ -290,3 +278,14 @@ constexpr bool operator!=(const Ellipse& left, const Ellipse& right) { using TextRange = Range; } // namespace cru::platform + +namespace cru { + +inline String ToString(const platform::Point& point) { + return Format(u"(x: {}, y: {})", point.x, point.y); +} +inline String ToString(const platform::Size& size) { + return Format(u"(width: {}, height: {})", size.width, size.height); +} + +} // namespace cru diff --git a/include/cru/platform/graphics/Geometry.h b/include/cru/platform/graphics/Geometry.h index 112c95e6..0e0278d2 100644 --- a/include/cru/platform/graphics/Geometry.h +++ b/include/cru/platform/graphics/Geometry.h @@ -61,7 +61,7 @@ struct CRU_PLATFORM_GRAPHICS_API IGeometryBuilder : virtual IGraphicsResource { virtual std::unique_ptr<IGeometry> Build() = 0; - void ParseAndApplySvgPathData(StringView path_d); + virtual void ParseAndApplySvgPathData(StringView path_d); }; std::unique_ptr<IGeometry> CRU_PLATFORM_GRAPHICS_API diff --git a/include/cru/platform/graphics/SvgGeometry.h b/include/cru/platform/graphics/SvgGeometry.h index 742a68ca..65602578 100644 --- a/include/cru/platform/graphics/SvgGeometry.h +++ b/include/cru/platform/graphics/SvgGeometry.h @@ -1,16 +1,73 @@ #pragma once #include "Geometry.h" +#include "cru/common/Base.h" +#include "cru/common/Format.h" + +#include <utility> namespace cru::platform::graphics { +/** + * \remarks This class is purely a helper for some platforms, especially web + * canvas. It constructs a path data of svg of a list of commands. It can't + * generate a Geometry. Calling Build will throw a PlatformUnsupportedException. + * Instead, use GetPathData to get svg path data and use it to do other things. + */ class CRU_PLATFORM_GRAPHICS_API SvgGeometryBuilder : public Object, public virtual IGeometryBuilder { public: SvgGeometryBuilder(); + CRU_DELETE_COPY(SvgGeometryBuilder) + CRU_DELETE_MOVE(SvgGeometryBuilder) + ~SvgGeometryBuilder() override; + Point GetCurrentPosition() override; + + void MoveTo(const Point& point) override; + void RelativeMoveTo(const Point& offset) override; + + void LineTo(const Point& point) override; + void RelativeLineTo(const Point& offset) override; + + void CubicBezierTo(const Point& start_control_point, + const Point& end_control_point, + const Point& end_point) override; + void RelativeCubicBezierTo(const Point& start_control_offset, + const Point& end_control_offset, + const Point& end_offset) override; + + void QuadraticBezierTo(const Point& control_point, + const Point& end_point) override; + void RelativeQuadraticBezierTo(const Point& control_offset, + const Point& end_offset) override; + + void ArcTo(const Point& radius, float angle, bool is_large_arc, + bool is_clockwise, const Point& end_point) override; + void RelativeArcTo(const Point& radius, float angle, bool is_large_arc, + bool is_clockwise, const Point& end_offset) override; + + void CloseFigure(bool close) override; + + std::unique_ptr<IGeometry> Build() override; + + String GetPathData() const { return current_; } + + void ParseAndApplySvgPathData(StringView path_d) override; + + template <typename... Args> + void Append(StringView format, Args&&... args) { + current_ += Format(format, std::forward<Args>(args)...); + current_ += u' '; + } + + void AppendCommand(StringView command); + void Append(bool flag); + void Append(float number); + void Append(const Point& point); + private: String current_; }; |