Truly independent web browser
LibGfx: Find the point and tangent at a distance along a path
Jelle Raaijmakerse5e3ff207189
diff --git a/Libraries/LibGfx/Path.h b/Libraries/LibGfx/Path.hindex 45bd61c67c4..304d9ac4a75 100644--- a/Libraries/LibGfx/Path.h+++ b/Libraries/LibGfx/Path.h@@ -8,6 +8,7 @@#include <AK/Forward.h>#include <AK/NonnullOwnPtr.h>+#include <AK/Optional.h>#include <AK/String.h>#include <AK/Vector.h>#include <AK/kmalloc.h>@@ -15,11 +16,17 @@#include <LibGfx/Forward.h>#include <LibGfx/Point.h>#include <LibGfx/Rect.h>+#include <LibGfx/Vector2.h>#include <LibGfx/WindingRule.h>#include <LibIPC/Forward.h>namespace Gfx {+struct PointAlongPath {+ FloatPoint point;+ FloatVector2 tangent;+};+class PathImpl {public:AK_ALLOC_WITH_KMALLOC;@@ -47,6 +54,7 @@ public:virtual Gfx::FloatPoint last_point() const = 0;virtual Gfx::FloatRect bounding_box() const = 0;virtual float length() const = 0;+ virtual Optional<PointAlongPath> point_at_length(float distance) const = 0;virtual void set_fill_type(Gfx::WindingRule winding_rule) = 0;virtual bool contains(FloatPoint point, Gfx::WindingRule) const = 0;@@ -114,6 +122,7 @@ public:Gfx::FloatPoint last_point() const { return impl().last_point(); }Gfx::FloatRect bounding_box() const { return impl().bounding_box(); }float length() const { return impl().length(); }+ Optional<PointAlongPath> point_at_length(float distance) const { return impl().point_at_length(distance); }bool contains(FloatPoint point, Gfx::WindingRule winding_rule) const { return impl().contains(point, winding_rule); }void set_fill_type(Gfx::WindingRule winding_rule) { impl().set_fill_type(winding_rule); }diff --git a/Libraries/LibGfx/PathSkia.cpp b/Libraries/LibGfx/PathSkia.cppindex 372d351904b..f797373c716 100644--- a/Libraries/LibGfx/PathSkia.cpp+++ b/Libraries/LibGfx/PathSkia.cpp@@ -309,6 +309,36 @@ float PathImplSkia::length() constreturn length;}+// The point at a distance along the path, clamped to its ends, and the unit tangent of the path there. The gaps between+// the subpaths do not add to the distance. A path without length is only its start point, where it has no tangent.+Optional<PointAlongPath> PathImplSkia::point_at_length(float distance) const+{+ SkPathMeasure path_measure(sk_path(), false, path_measure_resolution_scale);+ Optional<PointAlongPath> end_of_path;+ auto remaining_distance = max(distance, 0.f);+ do {+ auto subpath_length = path_measure.getLength();+ if (subpath_length <= 0)+ continue;+ SkPoint position;+ SkVector tangent;+ auto distance_in_subpath = min(remaining_distance, subpath_length);+ if (!path_measure.getPosTan(distance_in_subpath, &position, &tangent))+ continue;+ end_of_path = PointAlongPath { { position.x(), position.y() }, { tangent.x(), tangent.y() } };+ if (remaining_distance <= subpath_length)+ return end_of_path;+ remaining_distance -= subpath_length;+ } while (path_measure.nextContour());++ if (end_of_path.has_value())+ return end_of_path;+ if (sk_path().countPoints() == 0)+ return {};+ auto start = sk_path().getPoint(0);+ return PointAlongPath { { start.x(), start.y() }, { 0, 0 } };+}+bool PathImplSkia::contains(FloatPoint point, Gfx::WindingRule winding_rule) const{SkPath temp_path = sk_path();diff --git a/Libraries/LibGfx/PathSkia.h b/Libraries/LibGfx/PathSkia.hindex 5562b57f1fa..d50bea98636 100644--- a/Libraries/LibGfx/PathSkia.h+++ b/Libraries/LibGfx/PathSkia.h@@ -40,6 +40,7 @@ public:virtual Gfx::FloatPoint last_point() const override;virtual Gfx::FloatRect bounding_box() const override;virtual float length() const override;+ virtual Optional<PointAlongPath> point_at_length(float distance) const override;virtual bool contains(FloatPoint point, Gfx::WindingRule) const override;virtual void set_fill_type(Gfx::WindingRule winding_rule) override;diff --git a/Tests/LibGfx/CMakeLists.txt b/Tests/LibGfx/CMakeLists.txtindex 8c7e8a37322..0ca06e0d288 100644--- a/Tests/LibGfx/CMakeLists.txt+++ b/Tests/LibGfx/CMakeLists.txt@@ -6,6 +6,7 @@ set(TEST_SOURCESTestFont.cppTestImageDecoder.cppTestImageWriter.cpp+ TestPath.cppTestQuad.cppTestRect.cppTestSharedFontProvider.cppdiff --git a/Tests/LibGfx/TestPath.cpp b/Tests/LibGfx/TestPath.cppnew file mode 100644index 00000000000..d7216804d02--- /dev/null+++ b/Tests/LibGfx/TestPath.cpp@@ -0,0 +1,65 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibGfx/Path.h>+#include <LibTest/TestCase.h>++static void expect_point_along_path(Gfx::Path const& path, float distance, Gfx::FloatPoint point, Gfx::FloatVector2 tangent)+{+ auto point_along_path = path.point_at_length(distance);+ VERIFY(point_along_path.has_value());+ EXPECT_APPROXIMATE(point_along_path->point.x(), point.x());+ EXPECT_APPROXIMATE(point_along_path->point.y(), point.y());+ EXPECT_APPROXIMATE(point_along_path->tangent.x(), tangent.x());+ EXPECT_APPROXIMATE(point_along_path->tangent.y(), tangent.y());+}++TEST_CASE(point_at_length_along_a_line)+{+ Gfx::Path path;+ path.move_to({ 0, 0 });+ path.line_to({ 100, 0 });++ expect_point_along_path(path, 25, { 25, 0 }, { 1, 0 });+ expect_point_along_path(path, -10, { 0, 0 }, { 1, 0 });+ expect_point_along_path(path, 1000, { 100, 0 }, { 1, 0 });+}++TEST_CASE(point_at_length_skips_the_gaps_between_subpaths)+{+ Gfx::Path path;+ path.move_to({ 0, 0 });+ path.line_to({ 10, 0 });+ path.move_to({ 50, 50 });+ path.move_to({ 100, 100 });+ path.line_to({ 100, 110 });++ expect_point_along_path(path, 5, { 5, 0 }, { 1, 0 });+ expect_point_along_path(path, 15, { 100, 105 }, { 0, 1 });+ expect_point_along_path(path, 25, { 100, 110 }, { 0, 1 });+}++TEST_CASE(point_at_length_along_a_closed_path)+{+ Gfx::Path path;+ path.move_to({ 0, 0 });+ path.line_to({ 10, 0 });+ path.line_to({ 10, 10 });+ path.line_to({ 0, 10 });+ path.close();++ expect_point_along_path(path, 15, { 10, 5 }, { 0, 1 });+ expect_point_along_path(path, 35, { 0, 5 }, { 0, -1 });+}++TEST_CASE(point_at_length_of_a_path_without_length)+{+ Gfx::Path path;+ path.move_to({ 10, 20 });+ expect_point_along_path(path, 5, { 10, 20 }, { 0, 0 });++ EXPECT(!Gfx::Path {}.point_at_length(0).has_value());+}