ladybird/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp
Sam Atkins aba8774c9c LibWeb: Give SVG geometry elements a position
This makes the selected-in-the-inspector outline appear in the right
place. We take the stroke-width into account when producing the
bounding box, which makes the fit nice and snug. :^)
2022-02-16 21:47:53 +01:00

53 lines
1.6 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Format.h>
#include <LibWeb/Layout/SVGFormattingContext.h>
#include <LibWeb/Layout/SVGGeometryBox.h>
#include <LibWeb/Layout/SVGSVGBox.h>
namespace Web::Layout {
SVGFormattingContext::SVGFormattingContext(Box& box, FormattingContext* parent)
: FormattingContext(Type::SVG, box, parent)
{
}
SVGFormattingContext::~SVGFormattingContext()
{
}
void SVGFormattingContext::run(Box& box, LayoutMode)
{
// FIXME: This formatting context is basically a total hack.
// It works by computing the united bounding box of all <path>'s
// within an <svg>, and using that as the size of this box.
Gfx::FloatRect total_bounding_box;
box.for_each_in_subtree_of_type<SVGBox>([&](auto& descendant) {
if (is<SVGGeometryBox>(descendant)) {
auto& geometry_box = static_cast<SVGGeometryBox&>(descendant);
auto& path = geometry_box.dom_node().get_path();
auto bounding_box = path.bounding_box();
// Stroke increases the path's size by stroke_width/2 per side.
auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0);
bounding_box.inflate(stroke_width, stroke_width);
geometry_box.set_offset(bounding_box.top_left());
geometry_box.set_content_size(bounding_box.size());
total_bounding_box = total_bounding_box.united(path.bounding_box());
}
return IterationDecision::Continue;
});
box.set_content_size(total_bounding_box.size());
}
}