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

LibWeb: Implement shrink-to-fit layout on top of intrinsic size cache

Using the intrinsic size cache means we only perform the nested layout
to determine intrinsic size *once* per root layout pass.

Furthermore, by using a throwaway FormattingState, details of the nested
layout can't leak into and mutate the outer layout.
This commit is contained in:
Andreas Kling 2022-03-18 12:44:19 +01:00
parent 39ca39204b
commit 915ee66bd6

View file

@ -148,18 +148,11 @@ static float greatest_child_width(FormattingState const& state, Box const& box)
FormattingContext::ShrinkToFitResult FormattingContext::calculate_shrink_to_fit_widths(Box const& box)
{
// Calculate the preferred width by formatting the content without breaking lines
// other than where explicit line breaks occur.
(void)layout_inside(box, LayoutMode::OnlyRequiredLineBreaks);
float preferred_width = greatest_child_width(m_state, box);
// Also calculate the preferred minimum width, e.g., by trying all possible line breaks.
// CSS 2.2 does not define the exact algorithm.
(void)layout_inside(box, LayoutMode::AllPossibleLineBreaks);
float preferred_minimum_width = greatest_child_width(m_state, box);
return { preferred_width, preferred_minimum_width };
auto [min_content, max_content] = calculate_intrinsic_sizes(box);
return {
.preferred_width = max_content.width(),
.preferred_minimum_width = min_content.width(),
};
}
static Gfx::FloatSize solve_replaced_size_constraint(FormattingState const& state, float w, float h, ReplacedBox const& box)