Skip to content

Truly independent web browser

LibGfx: Don't leave a lone move before an appended path

Aliaksandr Kalenike6951d7ffe61
diff --git a/Libraries/LibGfx/PathSkia.cpp b/Libraries/LibGfx/PathSkia.cpp
index 8bf225e3994..913058efca4 100644
--- a/Libraries/LibGfx/PathSkia.cpp
+++ b/Libraries/LibGfx/PathSkia.cpp
@@ -463,10 +463,56 @@ NonnullOwnPtr<PathImpl> PathImplSkia::place_glyph_runs_along(ReadonlySpan<Nonnul
return output_path;
}
+static SkPathBuilder copy_without_last_verb(SkPathBuilder const& builder)
+{
+ auto const verbs = builder.verbs();
+ auto const points = builder.points();
+ auto const conic_weights = builder.conicWeights();
+ SkPathBuilder copy(builder.fillType());
+ size_t point_index = 0;
+ size_t conic_weight_index = 0;
+ for (size_t verb_index = 0; verb_index + 1 < verbs.size(); ++verb_index) {
+ switch (verbs[verb_index]) {
+ case SkPathVerb::kMove:
+ copy.moveTo(points[point_index]);
+ point_index += 1;
+ break;
+ case SkPathVerb::kLine:
+ copy.lineTo(points[point_index]);
+ point_index += 1;
+ break;
+ case SkPathVerb::kQuad:
+ copy.quadTo(points[point_index], points[point_index + 1]);
+ point_index += 2;
+ break;
+ case SkPathVerb::kConic:
+ copy.conicTo(points[point_index], points[point_index + 1], conic_weights[conic_weight_index++]);
+ point_index += 2;
+ break;
+ case SkPathVerb::kCubic:
+ copy.cubicTo(points[point_index], points[point_index + 1], points[point_index + 2]);
+ point_index += 3;
+ break;
+ case SkPathVerb::kClose:
+ copy.close();
+ break;
+ }
+ }
+ return copy;
+}
+
void PathImplSkia::append_path(Gfx::Path const& other)
{
auto const& other_impl = static_cast<PathImplSkia const&>(other.impl());
- sk_path_builder().addPath(other_impl.sk_path());
+ auto const other_path = other_impl.sk_path();
+ auto& path_builder = sk_path_builder();
+ // The other path starts with a move. If this path ends with a move, the result has two moves in sequence. That is
+ // not a valid path, and the compositor gets an empty path. Remove the move first. A subpath with one point is not
+ // filled or stroked.
+ auto const verbs = path_builder.verbs();
+ if (!other_path.isEmpty() && verbs.size() > 1 && verbs.back() == SkPathVerb::kMove)
+ path_builder = copy_without_last_verb(path_builder);
+ path_builder.addPath(other_path);
if (other_impl.m_has_current_point) {
m_has_current_point = true;
m_last_move_to = other_impl.m_last_move_to;
diff --git a/Tests/LibWeb/Ref/expected/canvas-path2d-add-path-after-closed-subpath-ref.html b/Tests/LibWeb/Ref/expected/canvas-path2d-add-path-after-closed-subpath-ref.html
new file mode 100644
index 00000000000..81284d41bb2
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/canvas-path2d-add-path-after-closed-subpath-ref.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<canvas id="c" width="200" height="100"></canvas>
+<script>
+ const ctx = c.getContext("2d");
+ ctx.fillStyle = "green";
+ ctx.fillRect(10, 10, 50, 50);
+ ctx.fillRect(80, 10, 50, 50);
+</script>
diff --git a/Tests/LibWeb/Ref/input/canvas-path2d-add-path-after-closed-subpath.html b/Tests/LibWeb/Ref/input/canvas-path2d-add-path-after-closed-subpath.html
new file mode 100644
index 00000000000..535856a0757
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/canvas-path2d-add-path-after-closed-subpath.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/canvas-path2d-add-path-after-closed-subpath-ref.html" />
+<canvas id="c" width="200" height="100"></canvas>
+<script>
+ const ctx = c.getContext("2d");
+ ctx.fillStyle = "green";
+
+ const path = new Path2D("M10 10 H60 V60 H10 Z");
+ path.addPath(new Path2D("M80 10 H130 V60 H80 Z"));
+ ctx.fill(path);
+</script>