diff options
author | crupest <crupest@outlook.com> | 2022-05-15 14:08:06 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-05-15 14:08:06 +0800 |
commit | 8ad2966933957ac5d6ff8dcd5e732736fd5e4dc6 (patch) | |
tree | 77e41cc14264060517c0f7ed95837012afb8342e /src/platform/graphics/quartz/Geometry.cpp | |
parent | 9e0c9d3499bc50c3534b4dc500d8b5d0b5f22752 (diff) | |
download | cru-8ad2966933957ac5d6ff8dcd5e732736fd5e4dc6.tar.gz cru-8ad2966933957ac5d6ff8dcd5e732736fd5e4dc6.tar.bz2 cru-8ad2966933957ac5d6ff8dcd5e732736fd5e4dc6.zip |
...
Diffstat (limited to 'src/platform/graphics/quartz/Geometry.cpp')
-rw-r--r-- | src/platform/graphics/quartz/Geometry.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/platform/graphics/quartz/Geometry.cpp b/src/platform/graphics/quartz/Geometry.cpp new file mode 100644 index 00000000..e6558fbb --- /dev/null +++ b/src/platform/graphics/quartz/Geometry.cpp @@ -0,0 +1,79 @@ +#include "cru/platform/graphics/quartz/Geometry.h" +#include "cru/platform/graphics/quartz/Convert.h" + +#include <memory> + +namespace cru::platform::graphics::quartz { +QuartzGeometry::QuartzGeometry(IGraphicsFactory *graphics_factory, + CGPathRef cg_path) + : OsxQuartzResource(graphics_factory), cg_path_(cg_path) {} + +QuartzGeometry::~QuartzGeometry() { CGPathRelease(cg_path_); } + +bool QuartzGeometry::FillContains(const Point &point) { + return CGPathContainsPoint(cg_path_, nullptr, CGPoint{point.x, point.y}, + kCGPathFill); +} + +Rect QuartzGeometry::GetBounds() { + auto bounds = CGPathGetPathBoundingBox(cg_path_); + if (CGRectIsNull(bounds)) return {}; + return Convert(bounds); +} + +std::unique_ptr<IGeometry> QuartzGeometry::Transform(const Matrix &matrix) { + auto cg_matrix = Convert(matrix); + auto cg_path = CGPathCreateCopyByTransformingPath(cg_path_, &cg_matrix); + return std::make_unique<QuartzGeometry>(GetGraphicsFactory(), cg_path); +} + +std::unique_ptr<IGeometry> QuartzGeometry::CreateStrokeGeometry(float width) { + auto cg_path = CGPathCreateCopyByStrokingPath( + cg_path_, nullptr, width, kCGLineCapButt, kCGLineJoinMiter, 10); + return std::make_unique<QuartzGeometry>(GetGraphicsFactory(), cg_path); +} + +QuartzGeometryBuilder::QuartzGeometryBuilder(IGraphicsFactory *graphics_factory) + : OsxQuartzResource(graphics_factory) { + cg_mutable_path_ = CGPathCreateMutable(); +} + +QuartzGeometryBuilder::~QuartzGeometryBuilder() { + CGPathRelease(cg_mutable_path_); +} + +Point QuartzGeometryBuilder::GetCurrentPosition() { + return Convert(CGPathGetCurrentPoint(cg_mutable_path_)); +} + +void QuartzGeometryBuilder::MoveTo(const Point &point) { + CGPathMoveToPoint(cg_mutable_path_, nullptr, point.x, point.y); +} + +void QuartzGeometryBuilder::LineTo(const Point &point) { + CGPathAddLineToPoint(cg_mutable_path_, nullptr, point.x, point.y); +} + +void QuartzGeometryBuilder::CubicBezierTo(const Point &start_control_point, + const Point &end_control_point, + const Point &end_point) { + CGPathAddCurveToPoint(cg_mutable_path_, nullptr, start_control_point.x, + start_control_point.y, end_control_point.x, + end_control_point.y, end_point.x, end_point.y); +} + +void QuartzGeometryBuilder::QuadraticBezierTo(const Point &control_point, + const Point &end_point) { + CGPathAddQuadCurveToPoint(cg_mutable_path_, nullptr, control_point.x, + control_point.y, end_point.x, end_point.y); +} + +void QuartzGeometryBuilder::CloseFigure(bool close) { + if (close) CGPathCloseSubpath(cg_mutable_path_); +} + +std::unique_ptr<IGeometry> QuartzGeometryBuilder::Build() { + return std::make_unique<QuartzGeometry>(GetGraphicsFactory(), + CGPathCreateCopy(cg_mutable_path_)); +} +} // namespace cru::platform::graphics::quartz |