Skip to content

Truly independent web browser

LibGfx: Measure the lengths of curves more precisely

Jelle Raaijmakersc24d2f2fab7c
diff --git a/Libraries/LibGfx/PathSkia.cpp b/Libraries/LibGfx/PathSkia.cpp
index 7c1ee3b2592..372d351904b 100644
--- a/Libraries/LibGfx/PathSkia.cpp
+++ b/Libraries/LibGfx/PathSkia.cpp
@@ -207,9 +207,14 @@ void PathImplSkia::glyph_run(GlyphRun const& glyph_run)
update_state_from_path(sk_path());
}
+// By default, Skia measures a curve by the lengths of the lines between points along it that are at most half a unit off
+// the curve, which leaves the length of a circle with a radius of 25 units about 1 unit short. Measure curves sixteen
+// times as precisely instead.
+static constexpr SkScalar path_measure_resolution_scale = 16;
+
NonnullOwnPtr<PathImpl> PathImplSkia::place_glyph_runs_along(ReadonlySpan<NonnullRefPtr<GlyphRun>> glyph_runs, float offset) const
{
- SkPathMeasure path_measure(sk_path(), false);
+ SkPathMeasure path_measure(sk_path(), false, path_measure_resolution_scale);
SkScalar path_length = path_measure.getLength();
auto output_path = PathImplSkia::create();
@@ -296,7 +301,7 @@ Gfx::FloatRect PathImplSkia::bounding_box() const
float PathImplSkia::length() const
{
- SkPathMeasure path_measure(sk_path(), false);
+ SkPathMeasure path_measure(sk_path(), false, path_measure_resolution_scale);
float length = 0;
do {
length += path_measure.getLength();
diff --git a/Tests/LibWeb/Screenshot/expected/svg-textPath.png b/Tests/LibWeb/Screenshot/expected/svg-textPath.png
index 5fe4d77d9a0..1a1168ccb7a 100644
Binary files a/Tests/LibWeb/Screenshot/expected/svg-textPath.png and b/Tests/LibWeb/Screenshot/expected/svg-textPath.png differ
diff --git a/Tests/LibWeb/Text/expected/SVG/svg-geometry-get-total-length-detached.txt b/Tests/LibWeb/Text/expected/SVG/svg-geometry-get-total-length-detached.txt
index fbdff0b3018..908c9d0f0d3 100644
--- a/Tests/LibWeb/Text/expected/SVG/svg-geometry-get-total-length-detached.txt
+++ b/Tests/LibWeb/Text/expected/SVG/svg-geometry-get-total-length-detached.txt
@@ -2,7 +2,7 @@ rect: 60
rect with inline style: 100
rect with var(): 70
rect with a custom property: 100
-circle: 62
+circle: 63
path: 50
rect in detached subtree: 20
rect in template content: 60
diff --git a/Tests/LibWeb/Text/expected/display_list/dashed-border-with-border-radius.txt b/Tests/LibWeb/Text/expected/display_list/dashed-border-with-border-radius.txt
index e7e54208725..dd2fbd37ad2 100644
--- a/Tests/LibWeb/Text/expected/display_list/dashed-border-with-border-radius.txt
+++ b/Tests/LibWeb/Text/expected/display_list/dashed-border-with-border-radius.txt
@@ -6,16 +6,16 @@ AccumulatedVisualContext Tree:
[e0 in s0] effects=[]
DisplayList:
-StrokePath@s1/e0 path_bounding_rect=[8,8 120x80] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=29.63
-StrokePath@s1/e0 path_bounding_rect=[110.677666,95.322334 17.322334x65.35534] thickness=10 color=rgb(0, 0, 255) dash_array_size=2 dash_offset=14.75 inline_clips=[clip_path=[bounds: [112,96 16x64], curved path: 68 commands]]
-StrokePath@s1/e0 path_bounding_rect=[15.322332,150.67767 105.35533x17.322327] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=12.38 inline_clips=[clip_path=[bounds: [16,152 104x17], curved path: 68 commands]]
-StrokePath@s1/e0 path_bounding_rect=[8,95.322334 17.322332x65.35534] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=14.75 inline_clips=[clip_path=[bounds: [7,96 17x64], curved path: 68 commands]]
-StrokePath@s1/e0 path_bounding_rect=[15.322332,87.99999 105.35533x17.322342] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=12.38 inline_clips=[clip_path=[bounds: [16,88 104x16], curved path: 68 commands]]
-StrokePath@s1/e0 path_bounding_rect=[8,168 120x80] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=30.92
-StrokePath@s1/e0 path_bounding_rect=[8,248 120x80] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=9.88
-StrokePath@s1/e0 path_bounding_rect=[110.677666,335.32233 17.322334x65.35535] thickness=10 color=rgb(0, 0, 255) dash_array_size=2 dash_offset=9.84 inline_clips=[clip_path=[bounds: [112,336 16x64], curved path: 68 commands]]
-StrokePath@s1/e0 path_bounding_rect=[15.322332,390.67767 105.35533x17.322327] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=9.90 inline_clips=[clip_path=[bounds: [16,392 104x16], curved path: 68 commands]]
-StrokePath@s1/e0 path_bounding_rect=[8,335.32233 17.322332x65.35535] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=9.84 inline_clips=[clip_path=[bounds: [7,336 17x64], curved path: 68 commands]]
-StrokePath@s1/e0 path_bounding_rect=[15.322332,327.99997 105.35533x17.322357] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=9.90 inline_clips=[clip_path=[bounds: [16,328 104x16], curved path: 68 commands]]
+StrokePath@s1/e0 path_bounding_rect=[8,8 120x80] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=29.72
+StrokePath@s1/e0 path_bounding_rect=[110.677666,95.322334 17.322334x65.35534] thickness=10 color=rgb(0, 0, 255) dash_array_size=2 dash_offset=14.81 inline_clips=[clip_path=[bounds: [112,96 16x64], curved path: 68 commands]]
+StrokePath@s1/e0 path_bounding_rect=[15.322332,150.67767 105.35533x17.322327] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=12.41 inline_clips=[clip_path=[bounds: [16,152 104x17], curved path: 68 commands]]
+StrokePath@s1/e0 path_bounding_rect=[8,95.322334 17.322332x65.35534] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=14.81 inline_clips=[clip_path=[bounds: [7,96 17x64], curved path: 68 commands]]
+StrokePath@s1/e0 path_bounding_rect=[15.322332,87.99999 105.35533x17.322342] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=12.41 inline_clips=[clip_path=[bounds: [16,88 104x16], curved path: 68 commands]]
+StrokePath@s1/e0 path_bounding_rect=[8,168 120x80] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=30.93
+StrokePath@s1/e0 path_bounding_rect=[8,248 120x80] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=9.91
+StrokePath@s1/e0 path_bounding_rect=[110.677666,335.32233 17.322334x65.35535] thickness=10 color=rgb(0, 0, 255) dash_array_size=2 dash_offset=9.88 inline_clips=[clip_path=[bounds: [112,336 16x64], curved path: 68 commands]]
+StrokePath@s1/e0 path_bounding_rect=[15.322332,390.67767 105.35533x17.322327] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=9.93 inline_clips=[clip_path=[bounds: [16,392 104x16], curved path: 68 commands]]
+StrokePath@s1/e0 path_bounding_rect=[8,335.32233 17.322332x65.35535] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=9.88 inline_clips=[clip_path=[bounds: [7,336 17x64], curved path: 68 commands]]
+StrokePath@s1/e0 path_bounding_rect=[15.322332,327.99997 105.35533x17.322357] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=9.93 inline_clips=[clip_path=[bounds: [16,328 104x16], curved path: 68 commands]]
StrokePath@s1/e0 path_bounding_rect=[8,408 120x80] thickness=10 color=rgb(255, 0, 0) dash_array_size=2 dash_offset=10.31