From ab484b2f0a8541cc5c5a172e4f5ea2620d91c8ba Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 12 Oct 2023 20:10:07 +0800 Subject: SvgGeometryBuilder done! --- include/cru/platform/graphics/Geometry.h | 2 +- include/cru/platform/graphics/SvgGeometry.h | 57 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) (limited to 'include/cru/platform/graphics') 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 Build() = 0; - void ParseAndApplySvgPathData(StringView path_d); + virtual void ParseAndApplySvgPathData(StringView path_d); }; std::unique_ptr 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 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 Build() override; + + String GetPathData() const { return current_; } + + void ParseAndApplySvgPathData(StringView path_d) override; + + template + void Append(StringView format, Args&&... args) { + current_ += Format(format, std::forward(args)...); + current_ += u' '; + } + + void AppendCommand(StringView command); + void Append(bool flag); + void Append(float number); + void Append(const Point& point); + private: String current_; }; -- cgit v1.2.3