1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:07:45 +00:00

LibWeb: Rewrite calculation of available space between floats

This code now works in terms of *intrusion* by left and right side
floats into a given box whose insides we're trying to layout.

Previously, it worked in terms of space occupied by floats in the root
box of the BFC they participated in. That created a bunch of edge cases
since the code asking about the information wasn't operating in root
coordinate space, but in the coordinate space of some arbitrarily nested
block descendant of the root.

This finally allows horizontal margins in the containing block chain to
affect floats and nested content correctly, and it also allows us to
remove a bogus workaround in InlineFormattingContext.
This commit is contained in:
Andreas Kling 2023-05-16 09:46:45 +02:00
parent 9bd4add734
commit bab6796099
8 changed files with 159 additions and 40 deletions

View file

@ -125,9 +125,8 @@ void BlockFormattingContext::compute_width(Box const& box, AvailableSpace const&
// sufficient space. They may even make the border box of said element narrower than defined by section 10.3.3.
// CSS2 does not define when a UA may put said element next to the float or by how much said element may
// become narrower.
auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(box, root(), m_state);
auto space = space_used_by_floats(box_in_root_rect.y());
auto remaining_width = available_space.width.to_px() - space.left - space.right;
auto intrusion = intrusion_by_floats_into_box(box, 0);
auto remaining_width = available_space.width.to_px() - intrusion.left - intrusion.right;
remaining_available_space.width = AvailableSize::make_definite(remaining_width);
}
@ -994,7 +993,13 @@ BlockFormattingContext::SpaceUsedByFloats BlockFormattingContext::space_used_by_
// NOTE: The floating box is *not* in the final horizontal position yet, but the size and vertical position is valid.
auto rect = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root(), m_state);
if (rect.contains_vertically(y.value())) {
space_used_by_floats.left = floating_box.offset_from_edge
CSSPixels offset_from_containing_block_chain_margins_between_here_and_root = 0;
for (auto const* containing_block = floating_box.box->containing_block(); containing_block && containing_block != &root(); containing_block = containing_block->containing_block()) {
auto const& containing_block_state = m_state.get(*containing_block);
offset_from_containing_block_chain_margins_between_here_and_root = max(offset_from_containing_block_chain_margins_between_here_and_root, containing_block_state.margin_box_left());
}
space_used_by_floats.left = offset_from_containing_block_chain_margins_between_here_and_root
+ floating_box.offset_from_edge
+ floating_box_state.content_width()
+ floating_box_state.margin_box_right();
break;
@ -1007,7 +1012,13 @@ BlockFormattingContext::SpaceUsedByFloats BlockFormattingContext::space_used_by_
// NOTE: The floating box is *not* in the final horizontal position yet, but the size and vertical position is valid.
auto rect = margin_box_rect_in_ancestor_coordinate_space(floating_box.box, root(), m_state);
if (rect.contains_vertically(y.value())) {
space_used_by_floats.right = floating_box.offset_from_edge
CSSPixels offset_from_containing_block_chain_margins_between_here_and_root = 0;
for (auto const* containing_block = floating_box.box->containing_block(); containing_block && containing_block != &root(); containing_block = containing_block->containing_block()) {
auto const& containing_block_state = m_state.get(*containing_block);
offset_from_containing_block_chain_margins_between_here_and_root = max(offset_from_containing_block_chain_margins_between_here_and_root, containing_block_state.margin_box_right());
}
space_used_by_floats.right = offset_from_containing_block_chain_margins_between_here_and_root
+ floating_box.offset_from_edge
+ floating_box_state.margin_box_left();
break;
}
@ -1016,6 +1027,25 @@ BlockFormattingContext::SpaceUsedByFloats BlockFormattingContext::space_used_by_
return space_used_by_floats;
}
FormattingContext::SpaceUsedByFloats BlockFormattingContext::intrusion_by_floats_into_box(Box const& box, CSSPixels y_in_box) const
{
// NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(box, root(), m_state);
CSSPixels y_in_root = box_in_root_rect.y() + y_in_box;
auto space_used_by_floats_in_root = space_used_by_floats(y_in_root);
auto left_intrusion = max(CSSPixels(0), space_used_by_floats_in_root.left - box_in_root_rect.x());
CSSPixels offset_from_containing_block_chain_margins_between_here_and_root = 0;
for (auto const* containing_block = static_cast<Box const*>(&box); containing_block && containing_block != &root(); containing_block = containing_block->containing_block()) {
auto const& containing_block_state = m_state.get(*containing_block);
offset_from_containing_block_chain_margins_between_here_and_root = max(offset_from_containing_block_chain_margins_between_here_and_root, containing_block_state.margin_box_right());
}
auto right_intrusion = max(CSSPixels(0), space_used_by_floats_in_root.right - offset_from_containing_block_chain_margins_between_here_and_root);
return { left_intrusion, right_intrusion };
}
CSSPixels BlockFormattingContext::greatest_child_width(Box const& box) const
{
// Similar to FormattingContext::greatest_child_width()

View file

@ -40,6 +40,7 @@ public:
void add_absolutely_positioned_box(Box const& box) { m_absolutely_positioned_boxes.append(box); }
SpaceUsedByFloats space_used_by_floats(CSSPixels y) const;
SpaceUsedByFloats intrusion_by_floats_into_box(Box const&, CSSPixels y_in_box) const;
virtual CSSPixels greatest_child_width(Box const&) const override;

View file

@ -44,21 +44,20 @@ CSSPixels InlineFormattingContext::leftmost_x_offset_at(CSSPixels y) const
auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state);
CSSPixels y_in_root = box_in_root_rect.y() + y;
auto space = parent().space_used_by_floats(y_in_root);
return space.left;
if (box_in_root_rect.x() >= space.left) {
// The left edge of the containing block is to the right of the rightmost left-side float.
// We start placing inline content at the left edge of the containing block.
return 0;
}
// The left edge of the containing block is to the left of the rightmost left-side float.
// We adjust the inline content insertion point by the overlap between the containing block and the float.
return space.left - box_in_root_rect.x();
}
CSSPixels InlineFormattingContext::available_space_for_line(CSSPixels y) const
{
// NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
auto& root_block = parent().root();
auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), root_block, m_state);
CSSPixels y_in_root = box_in_root_rect.y() + y;
auto space = parent().space_used_by_floats(y_in_root);
space.left = space.left;
space.right = min(m_available_space->width.to_px() - space.right, m_available_space->width.to_px());
return space.right - space.left;
auto intrusions = parent().intrusion_by_floats_into_box(containing_block(), y);
return m_available_space->width.to_px() - (intrusions.left + intrusions.right);
}
CSSPixels InlineFormattingContext::automatic_content_width() const
@ -330,15 +329,22 @@ bool InlineFormattingContext::any_floats_intrude_at_y(CSSPixels y) const
bool InlineFormattingContext::can_fit_new_line_at_y(CSSPixels y) const
{
auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state);
CSSPixels y_in_root = box_in_root_rect.y() + y;
auto space_top = parent().space_used_by_floats(y_in_root);
auto space_bottom = parent().space_used_by_floats(y_in_root + containing_block().line_height() - 1);
[[maybe_unused]] auto top_left_edge = space_top.left;
[[maybe_unused]] auto top_right_edge = m_available_space->width.to_px() - space_top.right;
[[maybe_unused]] auto bottom_left_edge = space_bottom.left;
[[maybe_unused]] auto bottom_right_edge = m_available_space->width.to_px() - space_bottom.right;
auto top_intrusions = parent().intrusion_by_floats_into_box(containing_block(), y);
auto bottom_intrusions = parent().intrusion_by_floats_into_box(containing_block(), y + containing_block().line_height() - 1);
auto left_edge = [](auto& space) -> CSSPixels {
return space.left;
};
auto right_edge = [this](auto& space) -> CSSPixels {
return m_available_space->width.to_px() - space.right;
};
auto top_left_edge = left_edge(top_intrusions);
auto top_right_edge = right_edge(top_intrusions);
auto bottom_left_edge = left_edge(bottom_intrusions);
auto bottom_right_edge = right_edge(bottom_intrusions);
if (top_left_edge > bottom_right_edge)
return false;

View file

@ -167,10 +167,6 @@ void LineBuilder::update_last_line()
CSSPixels x_offset_bottom = m_context.leftmost_x_offset_at(m_current_y + current_line_height - 1);
CSSPixels x_offset = max(x_offset_top, x_offset_bottom);
// If the IFC's containing block has left-side margin, it has already been shifted to the right by that amount.
// We subtract the margin-left here to ensure that the left-side "space used by floats" doesn't get applied twice.
x_offset = max(CSSPixels(0), x_offset - m_containing_block_state.margin_left);
CSSPixels excess_horizontal_space = m_available_width_for_current_line - line_box.width();
switch (text_align) {