aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/platform/graphics/Geometry.cpp42
-rw-r--r--src/platform/graphics/quartz/Geometry.cpp14
2 files changed, 53 insertions, 3 deletions
diff --git a/src/platform/graphics/Geometry.cpp b/src/platform/graphics/Geometry.cpp
index 859a7bd6..896b324f 100644
--- a/src/platform/graphics/Geometry.cpp
+++ b/src/platform/graphics/Geometry.cpp
@@ -1,9 +1,11 @@
#include "cru/platform/graphics/Geometry.h"
-
#include "cru/base/StringUtil.h"
+#include "cru/platform/GraphicsBase.h"
+#include "cru/platform/Matrix.h"
#include "cru/platform/graphics/Factory.h"
#include <cmath>
+#include <numbers>
#include <unordered_set>
namespace cru::platform::graphics {
@@ -19,6 +21,40 @@ std::unique_ptr<IGeometry> IGeometry::CreateStrokeGeometry(
"not supported on this platform.");
}
+IGeometryBuilder::ArcInfo IGeometryBuilder::CalculateArcInfo(
+ const Point& start_point, const Point& radius, float angle,
+ bool is_large_arc, bool is_clockwise, const Point& end_point) {
+ auto matrix =
+ Matrix::Rotation(-angle) * Matrix::Scale(1 / radius.x, 1 / radius.y);
+ auto s1 = matrix.TransformPoint(start_point),
+ s2 = matrix.TransformPoint(end_point);
+ if (!is_clockwise) {
+ using std::swap;
+ swap(s1, s2);
+ }
+
+ auto mid = (s1 + s2) / 2;
+ auto d = s1.Distance(s2) / 2;
+ auto dc = std::sqrt(1 - d * d);
+ auto a = std::atan2(s1.x - s2.x, s2.y - s1.y);
+ Point center(mid.x - dc * std::cos(a), mid.y - dc * std::sin(a));
+
+ if (std::abs(center.x) < 0.000001) {
+ center.x = 0.f;
+ }
+ if (std::abs(center.y) < 0.000001) {
+ center.y = 0.f;
+ }
+
+ auto start_angle = std::atan2(s1.y - center.y, s1.x - center.x);
+ auto end_angle = std::atan2(s2.y - center.y, s2.x - center.x);
+ if (end_angle < 0) {
+ end_angle += std::numbers::pi_v<float> * 2;
+ }
+
+ return {matrix.Inverted()->TransformPoint(center), start_angle, end_angle};
+}
+
void IGeometryBuilder::RelativeMoveTo(const Point& offset) {
MoveTo(GetCurrentPosition() + offset);
}
@@ -233,9 +269,9 @@ void IGeometryBuilder::ParseAndApplySvgPathData(std::string_view path_d) {
++position;
}
-
auto result = cru::string::ParseToNumber<float>(
- path_d.substr(position), cru::string::ParseToNumberFlags::AllowTrailingJunk);
+ path_d.substr(position),
+ cru::string::ParseToNumberFlags::AllowTrailingJunk);
if (!result.valid) throw Exception("Invalid svg path data number.");
diff --git a/src/platform/graphics/quartz/Geometry.cpp b/src/platform/graphics/quartz/Geometry.cpp
index 4c2f90a6..9b93ce15 100644
--- a/src/platform/graphics/quartz/Geometry.cpp
+++ b/src/platform/graphics/quartz/Geometry.cpp
@@ -67,6 +67,20 @@ void QuartzGeometryBuilder::QuadraticBezierTo(const Point &control_point,
control_point.y, end_point.x, end_point.y);
}
+void QuartzGeometryBuilder::ArcTo(const Point &radius, float angle,
+ bool is_large_arc, bool is_clockwise,
+ const Point &end_point) {
+ auto info = CalculateArcInfo(GetCurrentPosition(), radius, angle,
+ is_large_arc, is_clockwise, end_point);
+
+ auto matrix =
+ Matrix::Translation(info.center) * Matrix::Scale(radius.x, radius.y);
+ CGAffineTransform transform = Convert(matrix);
+
+ CGPathAddArc(cg_mutable_path_, &transform, 0, 0, 1, info.start_angle,
+ info.end_angle, true);
+}
+
void QuartzGeometryBuilder::CloseFigure(bool close) {
if (close) CGPathCloseSubpath(cg_mutable_path_);
}