Skip to content

Commit 70a56d2

Browse files
committed
LibWeb: Don't do horizontal inline line layout twice for last line
After pruning empty last line boxes, we now avoid re-running the horizontal fragment positioning step, since that would be wasted work.
1 parent b1fd801 commit 70a56d2

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
201201
line_box.trim_trailing_whitespace();
202202
}
203203

204-
// If there's an empty line box at the bottom, just remove it instead of giving it height.
205-
if (!containing_block().line_boxes().is_empty() && containing_block().line_boxes().last().fragments().is_empty())
206-
containing_block().line_boxes().take_last();
204+
line_builder.remove_last_line_if_empty();
207205
}
208206

209207
}

Userland/Libraries/LibWeb/Layout/LineBox.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class LineBox {
2626
void trim_trailing_whitespace();
2727

2828
bool is_empty_or_ends_in_whitespace() const;
29-
bool is_empty() { return m_fragments.is_empty(); }
29+
bool is_empty() const { return m_fragments.is_empty(); }
3030
bool ends_with_forced_line_break() const;
3131

3232
private:

Userland/Libraries/LibWeb/Layout/LineBuilder.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ namespace Web::Layout {
1212
LineBuilder::LineBuilder(InlineFormattingContext& context)
1313
: m_context(context)
1414
{
15+
begin_new_line();
1516
}
1617

1718
LineBuilder::~LineBuilder()
1819
{
19-
update_last_line();
20+
if (m_last_line_needs_update)
21+
update_last_line();
2022
}
2123

2224
void LineBuilder::break_line()
@@ -32,6 +34,8 @@ void LineBuilder::begin_new_line()
3234
auto space = m_context.available_space_for_line(m_current_y);
3335
m_available_width_for_current_line = space.right - space.left;
3436
m_max_height_on_current_line = 0;
37+
38+
m_last_line_needs_update = true;
3539
}
3640

3741
void LineBuilder::append_box(Box& box)
@@ -54,14 +58,17 @@ bool LineBuilder::should_break(LayoutMode layout_mode, float next_item_width, bo
5458
return true;
5559
if (layout_mode == LayoutMode::OnlyRequiredLineBreaks)
5660
return false;
57-
auto current_line_width = 0.0f;
58-
if (!m_context.containing_block().line_boxes().is_empty())
59-
current_line_width = m_context.containing_block().line_boxes().last().width();
61+
auto const& line_boxes = m_context.containing_block().line_boxes();
62+
if (line_boxes.is_empty() || line_boxes.last().is_empty())
63+
return false;
64+
auto current_line_width = m_context.containing_block().line_boxes().last().width();
6065
return (current_line_width + next_item_width) > m_available_width_for_current_line;
6166
}
6267

6368
void LineBuilder::update_last_line()
6469
{
70+
m_last_line_needs_update = false;
71+
6572
if (m_context.containing_block().line_boxes().is_empty())
6673
return;
6774

@@ -144,4 +151,14 @@ void LineBuilder::update_last_line()
144151
}
145152
}
146153

154+
void LineBuilder::remove_last_line_if_empty()
155+
{
156+
// If there's an empty line box at the bottom, just remove it instead of giving it height.
157+
auto& line_boxes = m_context.containing_block().line_boxes();
158+
if (!line_boxes.is_empty() && line_boxes.last().fragments().is_empty()) {
159+
line_boxes.take_last();
160+
m_last_line_needs_update = false;
161+
}
162+
}
163+
147164
}

Userland/Libraries/LibWeb/Layout/LineBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ class LineBuilder {
3333

3434
void update_last_line();
3535

36+
void remove_last_line_if_empty();
37+
3638
private:
3739
bool should_break(LayoutMode, float next_item_width, bool should_force_break);
3840

3941
InlineFormattingContext& m_context;
4042
float m_available_width_for_current_line { 0 };
4143
float m_current_y { 0 };
4244
float m_max_height_on_current_line { 0 };
45+
46+
bool m_last_line_needs_update { false };
4347
};
4448

4549
}

0 commit comments

Comments
 (0)