diff --git a/Libraries/LibGfx/Painter.h b/Libraries/LibGfx/Painter.h index 67aef92dfda..71ca4fbf0a0 100644 --- a/Libraries/LibGfx/Painter.h +++ b/Libraries/LibGfx/Painter.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -30,6 +31,7 @@ public: virtual void stroke_path(Gfx::Path const&, Gfx::Color, float thickness) = 0; virtual void stroke_path(Gfx::Path const&, Gfx::Color, float thickness, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) = 0; virtual void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, ReadonlySpan, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) = 0; + virtual void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, ReadonlySpan, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle const&, Gfx::Path::JoinStyle const&) = 0; virtual void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule) = 0; virtual void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) = 0; diff --git a/Libraries/LibGfx/PainterSkia.cpp b/Libraries/LibGfx/PainterSkia.cpp index 082b8e9af7c..473fe325904 100644 --- a/Libraries/LibGfx/PainterSkia.cpp +++ b/Libraries/LibGfx/PainterSkia.cpp @@ -207,6 +207,25 @@ void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::PaintStyle const& pain impl().canvas()->drawPath(sk_path, paint); } +void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::PaintStyle const& paint_style, ReadonlySpan filters, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle const& cap_style, Gfx::Path::JoinStyle const& join_style) +{ + // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing. + if (thickness <= 0) + return; + + auto sk_path = to_skia_path(path); + auto paint = to_skia_paint(paint_style, filters); + paint.setAntiAlias(true); + float alpha = paint.getAlphaf(); + paint.setAlphaf(alpha * global_alpha); + paint.setStyle(SkPaint::Style::kStroke_Style); + paint.setStrokeWidth(thickness); + paint.setStrokeCap(to_skia_cap(cap_style)); + paint.setStrokeJoin(to_skia_join(join_style)); + paint.setBlender(to_skia_blender(compositing_and_blending_operator)); + impl().canvas()->drawPath(sk_path, paint); +} + void PainterSkia::fill_path(Gfx::Path const& path, Gfx::Color color, Gfx::WindingRule winding_rule) { SkPaint paint; diff --git a/Libraries/LibGfx/PainterSkia.h b/Libraries/LibGfx/PainterSkia.h index 1c3427ab46d..ee1f8f54347 100644 --- a/Libraries/LibGfx/PainterSkia.h +++ b/Libraries/LibGfx/PainterSkia.h @@ -25,6 +25,7 @@ public: virtual void stroke_path(Gfx::Path const&, Gfx::Color, float thickness) override; virtual void stroke_path(Gfx::Path const&, Gfx::Color, float thickness, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) override; virtual void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, ReadonlySpan, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) override; + virtual void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, ReadonlySpan, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle const&, Gfx::Path::JoinStyle const&) override; virtual void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule) override; virtual void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) override; virtual void fill_path(Gfx::Path const&, Gfx::PaintStyle const&, ReadonlySpan, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::WindingRule) override; diff --git a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 9ef2b31af72..8d59386715e 100644 --- a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -292,6 +292,33 @@ void CanvasRenderingContext2D::begin_path() path().clear(); } +static Gfx::Path::CapStyle to_gfx_cap(Bindings::CanvasLineCap const& cap_style) +{ + switch (cap_style) { + case Bindings::CanvasLineCap::Butt: + return Gfx::Path::CapStyle::Butt; + case Bindings::CanvasLineCap::Round: + return Gfx::Path::CapStyle::Round; + case Bindings::CanvasLineCap::Square: + return Gfx::Path::CapStyle::Square; + } + VERIFY_NOT_REACHED(); +} + +static Gfx::Path::JoinStyle to_gfx_join(Bindings::CanvasLineJoin const& join_style) +{ + switch (join_style) { + case Bindings::CanvasLineJoin::Round: + return Gfx::Path::JoinStyle::Round; + case Bindings::CanvasLineJoin::Bevel: + return Gfx::Path::JoinStyle::Bevel; + case Bindings::CanvasLineJoin::Miter: + return Gfx::Path::JoinStyle::Miter; + } + + VERIFY_NOT_REACHED(); +} + void CanvasRenderingContext2D::stroke_internal(Gfx::Path const& path) { auto* painter = this->painter(); @@ -302,8 +329,10 @@ void CanvasRenderingContext2D::stroke_internal(Gfx::Path const& path) auto& state = drawing_state(); - // FIXME: Honor state's line_cap, line_join, miter_limit, dash_list, and line_dash_offset. - painter->stroke_path(path, state.stroke_style.to_gfx_paint_style(), state.filters, state.line_width, state.global_alpha, state.current_compositing_and_blending_operator); + // FIXME: Honor state's miter_limit, dash_list, and line_dash_offset. + auto line_cap = to_gfx_cap(state.line_cap); + auto line_join = to_gfx_join(state.line_join); + painter->stroke_path(path, state.stroke_style.to_gfx_paint_style(), state.filters, state.line_width, state.global_alpha, state.current_compositing_and_blending_operator, line_cap, line_join); did_draw(path.bounding_box()); } diff --git a/Tests/LibWeb/Screenshot/expected/canvas-stroke-styles-ref.html b/Tests/LibWeb/Screenshot/expected/canvas-stroke-styles-ref.html new file mode 100644 index 00000000000..418cd37f9c2 --- /dev/null +++ b/Tests/LibWeb/Screenshot/expected/canvas-stroke-styles-ref.html @@ -0,0 +1,9 @@ + + diff --git a/Tests/LibWeb/Screenshot/images/canvas-stroke-styles-ref.png b/Tests/LibWeb/Screenshot/images/canvas-stroke-styles-ref.png new file mode 100644 index 00000000000..bdf600169ed Binary files /dev/null and b/Tests/LibWeb/Screenshot/images/canvas-stroke-styles-ref.png differ diff --git a/Tests/LibWeb/Screenshot/input/canvas-stroke-styles.html b/Tests/LibWeb/Screenshot/input/canvas-stroke-styles.html new file mode 100644 index 00000000000..44a51b1366a --- /dev/null +++ b/Tests/LibWeb/Screenshot/input/canvas-stroke-styles.html @@ -0,0 +1,147 @@ + + + +