mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:57:45 +00:00
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. :^)
This commit is contained in:
parent
ae93aeb414
commit
aba8774c9c
4 changed files with 16 additions and 4 deletions
|
@ -32,7 +32,14 @@ void SVGFormattingContext::run(Box& box, LayoutMode)
|
||||||
if (is<SVGGeometryBox>(descendant)) {
|
if (is<SVGGeometryBox>(descendant)) {
|
||||||
auto& geometry_box = static_cast<SVGGeometryBox&>(descendant);
|
auto& geometry_box = static_cast<SVGGeometryBox&>(descendant);
|
||||||
auto& path = geometry_box.dom_node().get_path();
|
auto& path = geometry_box.dom_node().get_path();
|
||||||
geometry_box.set_content_size(path.bounding_box().size());
|
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());
|
total_bounding_box = total_bounding_box.united(path.bounding_box());
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ void SVGGeometryBox::paint(PaintContext& context, PaintPhase phase)
|
||||||
Gfx::AntiAliasingPainter painter { context.painter() };
|
Gfx::AntiAliasingPainter painter { context.painter() };
|
||||||
auto& svg_context = context.svg_context();
|
auto& svg_context = context.svg_context();
|
||||||
|
|
||||||
auto offset = absolute_position();
|
auto offset = svg_context.svg_element_position();
|
||||||
painter.translate(offset);
|
painter.translate(offset);
|
||||||
|
|
||||||
if (auto fill_color = geometry_element.fill_color().value_or(svg_context.fill_color()); fill_color.alpha() > 0) {
|
if (auto fill_color = geometry_element.fill_color().value_or(svg_context.fill_color()); fill_color.alpha() > 0) {
|
||||||
|
|
|
@ -19,7 +19,7 @@ void SVGSVGBox::before_children_paint(PaintContext& context, PaintPhase phase)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!context.has_svg_context())
|
if (!context.has_svg_context())
|
||||||
context.set_svg_context(SVGContext());
|
context.set_svg_context(SVGContext(absolute_rect()));
|
||||||
|
|
||||||
SVGGraphicsBox::before_children_paint(context, phase);
|
SVGGraphicsBox::before_children_paint(context, phase);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,12 +8,14 @@
|
||||||
|
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibGfx/Color.h>
|
#include <LibGfx/Color.h>
|
||||||
|
#include <LibGfx/Rect.h>
|
||||||
|
|
||||||
namespace Web {
|
namespace Web {
|
||||||
|
|
||||||
class SVGContext {
|
class SVGContext {
|
||||||
public:
|
public:
|
||||||
SVGContext()
|
SVGContext(Gfx::FloatRect svg_element_bounds)
|
||||||
|
: m_svg_element_bounds(svg_element_bounds)
|
||||||
{
|
{
|
||||||
m_states.append(State());
|
m_states.append(State());
|
||||||
}
|
}
|
||||||
|
@ -26,6 +28,8 @@ public:
|
||||||
void set_stroke_color(Gfx::Color color) { state().stroke_color = color; }
|
void set_stroke_color(Gfx::Color color) { state().stroke_color = color; }
|
||||||
void set_stroke_width(float width) { state().stroke_width = width; }
|
void set_stroke_width(float width) { state().stroke_width = width; }
|
||||||
|
|
||||||
|
Gfx::FloatPoint svg_element_position() const { return m_svg_element_bounds.top_left(); }
|
||||||
|
|
||||||
void save() { m_states.append(m_states.last()); }
|
void save() { m_states.append(m_states.last()); }
|
||||||
void restore() { m_states.take_last(); }
|
void restore() { m_states.take_last(); }
|
||||||
|
|
||||||
|
@ -39,6 +43,7 @@ private:
|
||||||
const State& state() const { return m_states.last(); }
|
const State& state() const { return m_states.last(); }
|
||||||
State& state() { return m_states.last(); }
|
State& state() { return m_states.last(); }
|
||||||
|
|
||||||
|
Gfx::FloatRect m_svg_element_bounds;
|
||||||
Vector<State> m_states;
|
Vector<State> m_states;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue