Truly independent web browser
LibGfx: Add Path::stroke_to_fill()
Andreas Kling21d7bfd1bc20
diff --git a/Libraries/LibGfx/Path.cpp b/Libraries/LibGfx/Path.cppindex 8cbd24cece9..3a1811a15fb 100644--- a/Libraries/LibGfx/Path.cpp+++ b/Libraries/LibGfx/Path.cpp@@ -34,6 +34,7 @@ void* ladybird_gfx_path_clone(void const*);void* ladybird_gfx_path_create_from_serialized_bytes(u8 const* bytes, size_t count);void* ladybird_gfx_path_copy_transformed(void const*, float const* affine_values);bool ladybird_gfx_path_contains(void const*, float, float, int);+void* ladybird_gfx_path_stroke_to_fill(void const*, float thickness, int cap_style, int join_style, float miter_limit, float const* dash_array, size_t dash_count, float dash_offset);void ladybird_gfx_path_set_fill_type(void*, int);void* ladybird_gfx_path_create_from_glyph_runs(LadybirdGfxGlyphRun const* runs, size_t run_count);void* ladybird_gfx_path_place_glyph_runs_along(void const* path, LadybirdGfxGlyphRun const* runs, size_t run_count, float offset);@@ -226,6 +227,19 @@ extern "C" bool ladybird_gfx_path_contains(void const* path, float x, float y, ireturn static_cast<Gfx::Path const*>(path)->contains({ x, y }, static_cast<Gfx::WindingRule>(winding_rule));}+extern "C" void* ladybird_gfx_path_stroke_to_fill(void const* path, float thickness, int cap_style, int join_style, float miter_limit, float const* dash_array, size_t dash_count, float dash_offset)+{+ Gfx::Path::StrokeStyle style {+ .thickness = thickness,+ .cap_style = static_cast<Gfx::Path::CapStyle>(cap_style),+ .join_style = static_cast<Gfx::Path::JoinStyle>(join_style),+ .miter_limit = miter_limit,+ .dash_array = { dash_array, dash_count },+ .dash_offset = dash_offset,+ };+ return new Gfx::Path(static_cast<Gfx::Path const*>(path)->stroke_to_fill(style));+}+extern "C" bool ladybird_gfx_path_equals(void const* a, void const* b){auto const& path_a = *static_cast<Gfx::Path const*>(a);diff --git a/Libraries/LibGfx/Path.h b/Libraries/LibGfx/Path.hindex d0aa4df8395..2a6bc5306e8 100644--- a/Libraries/LibGfx/Path.h+++ b/Libraries/LibGfx/Path.h@@ -112,6 +112,19 @@ public:Bevel,};+ struct StrokeStyle {+ float thickness { 1 };+ CapStyle cap_style { CapStyle::Butt };+ JoinStyle join_style { JoinStyle::Miter };+ float miter_limit { 4 };+ ReadonlySpan<float> dash_array;+ float dash_offset { 0 };+ };++ // The outline of the area that stroking this path with the given style covers, as a path to fill with the+ // nonzero winding rule.+ [[nodiscard]] Gfx::Path stroke_to_fill(StrokeStyle const&) const;+void elliptical_arc_to(FloatPoint point, FloatSize radii, float x_axis_rotation, bool large_arc, bool sweep) { impl().elliptical_arc_to(point, radii, x_axis_rotation, large_arc, sweep); }void arc_to(FloatPoint point, float radius, bool large_arc, bool sweep) { impl().arc_to(point, radius, large_arc, sweep); }void quadratic_bezier_curve_to(FloatPoint through, FloatPoint point) { impl().quadratic_bezier_curve_to(through, point); }diff --git a/Libraries/LibGfx/PathSkia.cpp b/Libraries/LibGfx/PathSkia.cppindex aac57fb0b72..c85fa90ec1c 100644--- a/Libraries/LibGfx/PathSkia.cpp+++ b/Libraries/LibGfx/PathSkia.cpp@@ -20,7 +20,9 @@#include <core/SkPath.h>#include <core/SkPathBuilder.h>#include <core/SkPathMeasure.h>+#include <core/SkPathUtils.h>#include <core/SkString.h>+#include <effects/SkDashPathEffect.h>#include <utils/SkParsePath.h>#include <harfbuzz/hb.h>@@ -695,6 +697,32 @@ NonnullOwnPtr<PathImpl> PathImplSkia::clone() constreturn adopt_own(*new PathImplSkia(*this));}+NonnullOwnPtr<PathImplSkia> PathImplSkia::stroke_to_fill(Path::StrokeStyle const& style) const+{+ auto outline = PathImplSkia::create();+ // NB: Skia treats zero thickness as a hairline, which covers no area.+ if (!(style.thickness > 0))+ return outline;++ SkPaint paint;+ paint.setStyle(SkPaint::kStroke_Style);+ paint.setStrokeWidth(style.thickness);+ paint.setStrokeCap(to_skia_cap(style.cap_style));+ paint.setStrokeJoin(to_skia_join(style.join_style));+ paint.setStrokeMiter(style.miter_limit);+ if (!style.dash_array.is_empty())+ paint.setPathEffect(SkDashPathEffect::Make({ style.dash_array.data(), style.dash_array.size() }, style.dash_offset));+ skpathutils::FillPathWithPaint(sk_path(), paint, &outline->sk_path_builder());+ outline->sk_path_builder().setFillType(SkPathFillType::kWinding);+ outline->update_state_from_builder();+ return outline;+}++Gfx::Path Path::stroke_to_fill(StrokeStyle const& style) const+{+ return Gfx::Path { static_cast<PathImplSkia const&>(impl()).stroke_to_fill(style) };+}+NonnullOwnPtr<PathImpl> PathImplSkia::copy_transformed(Gfx::AffineTransform const& transform) const{auto new_path = adopt_own(*new PathImplSkia(*this));diff --git a/Libraries/LibGfx/PathSkia.h b/Libraries/LibGfx/PathSkia.hindex b7526ad019b..e6d32b738d1 100644--- a/Libraries/LibGfx/PathSkia.h+++ b/Libraries/LibGfx/PathSkia.h@@ -51,6 +51,8 @@ public:virtual String to_svg_string() const override;+ [[nodiscard]] NonnullOwnPtr<PathImplSkia> stroke_to_fill(Path::StrokeStyle const&) const;+SkPath const& sk_path() const;SkPathBuilder& sk_path_builder();void update_state_from_builder();diff --git a/Libraries/LibGfx/Rust/src/path.rs b/Libraries/LibGfx/Rust/src/path.rsindex 1edb46cfc7a..0b938831a50 100644--- a/Libraries/LibGfx/Rust/src/path.rs+++ b/Libraries/LibGfx/Rust/src/path.rs@@ -28,6 +28,16 @@ unsafe extern "C" {context: *mut c_void,);fn ladybird_gfx_path_contains(path: *const c_void, x: f32, y: f32, winding_rule: i32) -> bool;+ fn ladybird_gfx_path_stroke_to_fill(+ path: *const c_void,+ thickness: f32,+ cap_style: i32,+ join_style: i32,+ miter_limit: f32,+ dash_array: *const f32,+ dash_count: usize,+ dash_offset: f32,+ ) -> *mut c_void;fn ladybird_gfx_path_length(path: *const c_void) -> f32;fn ladybird_gfx_path_create_from_ops(kinds: *const u8, values: *const f32, count: usize) -> *mut c_void;fn ladybird_gfx_path_append_ops(path: *mut c_void, kinds: *const u8, values: *const f32, count: usize);@@ -311,6 +321,17 @@ unsafe extern "C" fn append_to_string(context: *mut c_void, bytes: *const u8, le}}+/// How a path is stroked, as `OwnedPath::stroke_to_fill()` takes it.+#[derive(Clone, Debug, Default, PartialEq)]+pub struct StrokeStyle {+ pub thickness: f32,+ pub cap_style: crate::CapStyle,+ pub join_style: crate::JoinStyle,+ pub miter_limit: f32,+ pub dash_array: Vec<f32>,+ pub dash_offset: f32,+}+/// The sole owner of a heap-allocated `Gfx::Path`, destroying it on drop.pub struct OwnedPath {raw: NonNull<c_void>,@@ -470,6 +491,25 @@ impl OwnedPath {unsafe { ladybird_gfx_path_contains(self.raw.as_ptr(), x, y, winding_rule) }}+ /// The outline of the area that stroking this path with the given style covers, as a path to fill with the+ /// nonzero winding rule.+ pub fn stroke_to_fill(&self, style: &StrokeStyle) -> OwnedPath {+ // SAFETY: The path and the dash array are live for the synchronous call, which returns a fresh heap-allocated+ // Gfx::Path.+ unsafe {+ OwnedPath::adopt(ladybird_gfx_path_stroke_to_fill(+ self.raw.as_ptr(),+ style.thickness,+ style.cap_style as i32,+ style.join_style as i32,+ style.miter_limit,+ style.dash_array.as_ptr(),+ style.dash_array.len(),+ style.dash_offset,+ ))+ }+ }+pub fn to_svg_string(&self) -> String {let mut output = String::new();// SAFETY: The path is live for the duration of the call, and the callback only writes