From 915ee66bd6f2aa94c445ac699956e3578beb2e37 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 18 Mar 2022 12:44:19 +0100 Subject: [PATCH] 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. --- .../LibWeb/Layout/FormattingContext.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp index 2d5c1b7473..705d9844bb 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp @@ -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)