Skip to content

Truly independent web browser

LibWeb: Read the bounding box of an SVG shape from Rust

Aliaksandr Kalenikf52d21955f3f
diff --git a/Libraries/LibWeb/Painting/BoxViews.cpp b/Libraries/LibWeb/Painting/BoxViews.cpp
index f91a6687bbc..16f28c059d8 100644
--- a/Libraries/LibWeb/Painting/BoxViews.cpp
+++ b/Libraries/LibWeb/Painting/BoxViews.cpp
@@ -304,9 +304,12 @@ Compositing::SpatialNodeIndex own_scroll_node_index(Layout::Node const& node)
return row.has_value() ? row->own_scroll_node_index : Compositing::VISUAL_VIEWPORT_NODE_INDEX;
}
-Gfx::Path const* committed_svg_path(Layout::Node const& node)
+Optional<Gfx::FloatRect> committed_svg_path_bounding_box(Layout::Node const& node)
{
- return static_cast<Gfx::Path const*>(Layout::RustFFI::render_state_paintable_computed_svg_path(node.document_host(), committed_row_slot(node)));
+ Gfx::FloatRect bounding_box;
+ if (!Layout::RustFFI::render_state_paintable_computed_svg_path_bounding_box(node.document_host(), committed_row_slot(node), &bounding_box))
+ return {};
+ return bounding_box;
}
// The glyph cells a text content element's box committed for its own characters, one per code unit, copied out
diff --git a/Libraries/LibWeb/Painting/BoxViews.h b/Libraries/LibWeb/Painting/BoxViews.h
index f7ec0f0023f..98d109eac18 100644
--- a/Libraries/LibWeb/Painting/BoxViews.h
+++ b/Libraries/LibWeb/Painting/BoxViews.h
@@ -68,7 +68,7 @@ WEB_API Compositing::ContextRef accumulated_visual_context(Layout::Node const&);
WEB_API Compositing::ContextRef accumulated_visual_context_for_descendants(Layout::Node const&);
WEB_API Compositing::SpatialNodeIndex own_scroll_node_index(Layout::Node const&);
-WEB_API Gfx::Path const* committed_svg_path(Layout::Node const&);
+WEB_API Optional<Gfx::FloatRect> committed_svg_path_bounding_box(Layout::Node const&);
WEB_API Vector<Layout::RustFFI::FfiSvgTextCharacterCell> svg_text_character_cells(Layout::Node const&);
WEB_API CSSPixelSize svg_viewport_size(Layout::Node const&);
WEB_API Optional<Gfx::AffineTransform> svg_viewport_transform(Layout::Node const&);
diff --git a/Libraries/LibWeb/Rust/src/painting/ffi.rs b/Libraries/LibWeb/Rust/src/painting/ffi.rs
index 6dee69a4bb5..7069e950f31 100644
--- a/Libraries/LibWeb/Rust/src/painting/ffi.rs
+++ b/Libraries/LibWeb/Rust/src/painting/ffi.rs
@@ -695,19 +695,26 @@ pub unsafe extern "C" fn ladybird_web_record_image_paint_display_list(
}
}
+/// The bounds of the geometry that a path-like SVG element's box committed. Returns false for any other box.
#[unsafe(no_mangle)]
-pub extern "C" fn render_state_paintable_computed_svg_path(
+pub extern "C" fn render_state_paintable_computed_svg_path_bounding_box(
host: &DocumentHost,
paintable: NodeSlotId,
-) -> *const c_void {
- read_arena(host, node_read(), paintable, |arena, paintable| {
+ out: &mut libgfx_rust::FloatRect,
+) -> bool {
+ let bounding_box = read_arena(host, node_read(), paintable, |arena, paintable| {
let paintable_rows = arena.paintable_rows();
if !paintable_rows.paintable_row_is_populated(paintable) {
- return std::ptr::null();
+ return None;
}
crate::painting::paintable_geometry::committed_svg_path(&paintable_rows, paintable)
- .map_or(std::ptr::null(), |path| path.as_raw())
- })
+ .map(|path| path.bounding_box())
+ });
+ let Some(bounding_box) = bounding_box else {
+ return false;
+ };
+ *out = libgfx_rust::FloatRect::from_array(bounding_box);
+ true
}
/// The glyph cells a text content element's box committed for its own characters, one per code
diff --git a/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp b/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp
index 3f89c67e4d4..71515d0c181 100644
--- a/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp
@@ -165,8 +165,8 @@ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMRect>> SVGGraphicsElement::get_b_box(Bi
// A path-like element's bounding box covers its geometry alone; the committed content rect is
// inflated by the visible stroke width, so take the unstroked path bounds directly.
- if (auto const* committed_path = Painting::committed_svg_path(*self_layout_node))
- return Geometry::DOMRect::create(committed_path->bounding_box());
+ if (auto committed_path_bounding_box = Painting::committed_svg_path_bounding_box(*self_layout_node); committed_path_bounding_box.has_value())
+ return Geometry::DOMRect::create(*committed_path_bounding_box);
auto rect = Painting::absolute_rect(*self_layout_node).to_type<float>();
// An element with a non-positive geometry dimension is not rendered and