1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 09:17:35 +00:00

LibWeb: Resolve box shadow data for paintable boxes during layout

Step towards making the paintable tree independent of the layout tree.
This commit is contained in:
Aliaksandr Kalenik 2023-12-19 19:42:00 +01:00 committed by Andreas Kling
parent f68c67bf3f
commit b2abd1dd05
4 changed files with 35 additions and 26 deletions

View file

@ -336,6 +336,33 @@ void LayoutState::resolve_border_radii()
} }
} }
void LayoutState::resolve_box_shadow_data()
{
for (auto& it : used_values_per_layout_node) {
auto& used_values = *it.value;
auto& node = const_cast<NodeWithStyle&>(used_values.node());
auto* paintable = node.paintable();
if (paintable && is<Painting::PaintableBox>(*paintable)) {
auto box_shadow_data = node.computed_values().box_shadow();
if (box_shadow_data.is_empty())
continue;
auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
Vector<Painting::ShadowData> resolved_box_shadow_data;
resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
for (auto const& layer : box_shadow_data) {
resolved_box_shadow_data.empend(
layer.color,
layer.offset_x.to_px(node),
layer.offset_y.to_px(node),
layer.blur_radius.to_px(node),
layer.spread_distance.to_px(node),
layer.placement == CSS::ShadowPlacement::Outer ? Painting::ShadowPlacement::Outer : Painting::ShadowPlacement::Inner);
}
paintable_box.set_box_shadow_data(move(resolved_box_shadow_data));
}
}
}
void LayoutState::commit(Box& root) void LayoutState::commit(Box& root)
{ {
// Only the top-level LayoutState should ever be committed. // Only the top-level LayoutState should ever be committed.
@ -432,6 +459,7 @@ void LayoutState::commit(Box& root)
} }
resolve_border_radii(); resolve_border_radii();
resolve_box_shadow_data();
} }
void LayoutState::UsedValues::set_node(NodeWithStyle& node, UsedValues const* containing_block_used_values) void LayoutState::UsedValues::set_node(NodeWithStyle& node, UsedValues const* containing_block_used_values)

View file

@ -187,6 +187,7 @@ struct LayoutState {
private: private:
void resolve_relative_positions(Vector<Painting::PaintableWithLines&> const&); void resolve_relative_positions(Vector<Painting::PaintableWithLines&> const&);
void resolve_border_radii(); void resolve_border_radii();
void resolve_box_shadow_data();
}; };
} }

View file

@ -148,8 +148,7 @@ CSSPixelRect PaintableBox::compute_absolute_paint_rect() const
if (computed_values().overflow_y() == CSS::Overflow::Visible) if (computed_values().overflow_y() == CSS::Overflow::Visible)
rect.unite_vertically(scrollable_overflow_rect); rect.unite_vertically(scrollable_overflow_rect);
} }
auto resolved_box_shadow_data = resolve_box_shadow_data(); for (auto const& shadow : box_shadow_data()) {
for (auto const& shadow : resolved_box_shadow_data) {
if (shadow.placement == ShadowPlacement::Inner) if (shadow.placement == ShadowPlacement::Inner)
continue; continue;
auto inflate = shadow.spread_distance + shadow.blur_radius; auto inflate = shadow.spread_distance + shadow.blur_radius;
@ -355,30 +354,9 @@ void PaintableBox::paint_background(PaintContext& context) const
Painting::paint_background(context, layout_box(), background_rect, background_color, computed_values().image_rendering(), background_layers, normalized_border_radii_data()); Painting::paint_background(context, layout_box(), background_rect, background_color, computed_values().image_rendering(), background_layers, normalized_border_radii_data());
} }
Vector<ShadowData> PaintableBox::resolve_box_shadow_data() const
{
auto box_shadow_data = computed_values().box_shadow();
if (box_shadow_data.is_empty())
return {};
Vector<ShadowData> resolved_box_shadow_data;
resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
for (auto const& layer : box_shadow_data) {
resolved_box_shadow_data.empend(
layer.color,
layer.offset_x.to_px(layout_box()),
layer.offset_y.to_px(layout_box()),
layer.blur_radius.to_px(layout_box()),
layer.spread_distance.to_px(layout_box()),
layer.placement == CSS::ShadowPlacement::Outer ? ShadowPlacement::Outer : ShadowPlacement::Inner);
}
return resolved_box_shadow_data;
}
void PaintableBox::paint_box_shadow(PaintContext& context) const void PaintableBox::paint_box_shadow(PaintContext& context) const
{ {
auto resolved_box_shadow_data = resolve_box_shadow_data(); auto const& resolved_box_shadow_data = box_shadow_data();
if (resolved_box_shadow_data.is_empty()) if (resolved_box_shadow_data.is_empty())
return; return;
auto borders_data = BordersData { auto borders_data = BordersData {

View file

@ -191,6 +191,9 @@ public:
BorderRadiiData const& border_radii_data() const { return m_border_radii_data; } BorderRadiiData const& border_radii_data() const { return m_border_radii_data; }
void set_border_radii_data(BorderRadiiData const& border_radii_data) { m_border_radii_data = border_radii_data; } void set_border_radii_data(BorderRadiiData const& border_radii_data) { m_border_radii_data = border_radii_data; }
void set_box_shadow_data(Vector<ShadowData> box_shadow_data) { m_box_shadow_data = move(box_shadow_data); }
Vector<ShadowData> const& box_shadow_data() const { return m_box_shadow_data; }
PaintableBox const* nearest_scrollable_ancestor() const; PaintableBox const* nearest_scrollable_ancestor() const;
protected: protected:
@ -206,8 +209,6 @@ protected:
virtual CSSPixelRect compute_absolute_rect() const; virtual CSSPixelRect compute_absolute_rect() const;
virtual CSSPixelRect compute_absolute_paint_rect() const; virtual CSSPixelRect compute_absolute_paint_rect() const;
Vector<ShadowData> resolve_box_shadow_data() const;
private: private:
[[nodiscard]] virtual bool is_paintable_box() const final { return true; } [[nodiscard]] virtual bool is_paintable_box() const final { return true; }
@ -230,6 +231,7 @@ private:
Optional<TableCellCoordinates> m_table_cell_coordinates; Optional<TableCellCoordinates> m_table_cell_coordinates;
BorderRadiiData m_border_radii_data; BorderRadiiData m_border_radii_data;
Vector<ShadowData> m_box_shadow_data;
}; };
class PaintableWithLines : public PaintableBox { class PaintableWithLines : public PaintableBox {