Skip to content

Commit aba8774

Browse files
AtkinsSJawesomekling
authored andcommitted
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. :^)
1 parent ae93aeb commit aba8774

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ void SVGFormattingContext::run(Box& box, LayoutMode)
3232
if (is<SVGGeometryBox>(descendant)) {
3333
auto& geometry_box = static_cast<SVGGeometryBox&>(descendant);
3434
auto& path = geometry_box.dom_node().get_path();
35-
geometry_box.set_content_size(path.bounding_box().size());
35+
auto bounding_box = path.bounding_box();
36+
37+
// Stroke increases the path's size by stroke_width/2 per side.
38+
auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0);
39+
bounding_box.inflate(stroke_width, stroke_width);
40+
41+
geometry_box.set_offset(bounding_box.top_left());
42+
geometry_box.set_content_size(bounding_box.size());
3643

3744
total_bounding_box = total_bounding_box.united(path.bounding_box());
3845
}

Userland/Libraries/LibWeb/Layout/SVGGeometryBox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void SVGGeometryBox::paint(PaintContext& context, PaintPhase phase)
3232
Gfx::AntiAliasingPainter painter { context.painter() };
3333
auto& svg_context = context.svg_context();
3434

35-
auto offset = absolute_position();
35+
auto offset = svg_context.svg_element_position();
3636
painter.translate(offset);
3737

3838
if (auto fill_color = geometry_element.fill_color().value_or(svg_context.fill_color()); fill_color.alpha() > 0) {

Userland/Libraries/LibWeb/Layout/SVGSVGBox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void SVGSVGBox::before_children_paint(PaintContext& context, PaintPhase phase)
1919
return;
2020

2121
if (!context.has_svg_context())
22-
context.set_svg_context(SVGContext());
22+
context.set_svg_context(SVGContext(absolute_rect()));
2323

2424
SVGGraphicsBox::before_children_paint(context, phase);
2525
}

Userland/Libraries/LibWeb/SVG/SVGContext.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
#include <AK/Vector.h>
1010
#include <LibGfx/Color.h>
11+
#include <LibGfx/Rect.h>
1112

1213
namespace Web {
1314

1415
class SVGContext {
1516
public:
16-
SVGContext()
17+
SVGContext(Gfx::FloatRect svg_element_bounds)
18+
: m_svg_element_bounds(svg_element_bounds)
1719
{
1820
m_states.append(State());
1921
}
@@ -26,6 +28,8 @@ class SVGContext {
2628
void set_stroke_color(Gfx::Color color) { state().stroke_color = color; }
2729
void set_stroke_width(float width) { state().stroke_width = width; }
2830

31+
Gfx::FloatPoint svg_element_position() const { return m_svg_element_bounds.top_left(); }
32+
2933
void save() { m_states.append(m_states.last()); }
3034
void restore() { m_states.take_last(); }
3135

@@ -39,6 +43,7 @@ class SVGContext {
3943
const State& state() const { return m_states.last(); }
4044
State& state() { return m_states.last(); }
4145

46+
Gfx::FloatRect m_svg_element_bounds;
4247
Vector<State> m_states;
4348
};
4449

0 commit comments

Comments
 (0)