Skip to content

Commit 00bde9c

Browse files
committed
LibWeb: Add Layout::LineBuilder class for incremental line box layout
This class will be used to place items on lines incrementally instead of the current two-phase approach.
1 parent ae301d2 commit 00bde9c

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

Userland/Libraries/LibWeb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ set(SOURCES
221221
Layout/LayoutPosition.cpp
222222
Layout/LineBox.cpp
223223
Layout/LineBoxFragment.cpp
224+
Layout/LineBuilder.cpp
224225
Layout/ListItemBox.cpp
225226
Layout/ListItemMarkerBox.cpp
226227
Layout/Node.cpp

Userland/Libraries/LibWeb/Layout/LineBox.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class LineBox {
3232
private:
3333
friend class BlockContainer;
3434
friend class InlineFormattingContext;
35+
friend class LineBuilder;
36+
3537
NonnullOwnPtrVector<LineBoxFragment> m_fragments;
3638
float m_width { 0 };
3739
};
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibWeb/Layout/LineBuilder.h>
8+
#include <LibWeb/Layout/TextNode.h>
9+
10+
namespace Web::Layout {
11+
12+
LineBuilder::LineBuilder(InlineFormattingContext& context)
13+
: m_context(context)
14+
{
15+
}
16+
17+
LineBuilder::~LineBuilder()
18+
{
19+
update_last_line();
20+
}
21+
22+
void LineBuilder::break_line()
23+
{
24+
update_last_line();
25+
m_context.containing_block().line_boxes().append(LineBox());
26+
begin_new_line();
27+
}
28+
29+
void LineBuilder::begin_new_line()
30+
{
31+
m_current_y += m_max_height_on_current_line;
32+
auto space = m_context.available_space_for_line(m_current_y);
33+
m_available_width_for_current_line = space.right - space.left;
34+
m_max_height_on_current_line = 0;
35+
}
36+
37+
void LineBuilder::append_box(Box& box)
38+
{
39+
m_context.containing_block().line_boxes().last().add_fragment(box, 0, 0, box.width(), box.height());
40+
m_max_height_on_current_line = max(m_max_height_on_current_line, box.height());
41+
}
42+
43+
void LineBuilder::append_text_chunk(TextNode& text_node, size_t offset_in_node, size_t length_in_node, float width, float height)
44+
{
45+
m_context.containing_block().line_boxes().last().add_fragment(text_node, offset_in_node, length_in_node, width, height);
46+
m_max_height_on_current_line = max(m_max_height_on_current_line, height);
47+
}
48+
49+
void LineBuilder::break_if_needed(LayoutMode layout_mode, float next_item_width)
50+
{
51+
if (layout_mode == LayoutMode::AllPossibleLineBreaks
52+
|| (m_context.containing_block().line_boxes().last().width() + next_item_width) > m_available_width_for_current_line)
53+
break_line();
54+
}
55+
56+
void LineBuilder::update_last_line()
57+
{
58+
if (m_context.containing_block().line_boxes().is_empty())
59+
return;
60+
61+
auto& line_box = m_context.containing_block().line_boxes().last();
62+
63+
auto text_align = m_context.containing_block().computed_values().text_align();
64+
float x_offset = m_context.available_space_for_line(m_current_y).left;
65+
66+
float excess_horizontal_space = m_context.containing_block().width() - line_box.width();
67+
68+
switch (text_align) {
69+
case CSS::TextAlign::Center:
70+
case CSS::TextAlign::LibwebCenter:
71+
x_offset += excess_horizontal_space / 2;
72+
break;
73+
case CSS::TextAlign::Right:
74+
x_offset += excess_horizontal_space;
75+
break;
76+
case CSS::TextAlign::Left:
77+
case CSS::TextAlign::Justify:
78+
default:
79+
break;
80+
}
81+
82+
float excess_horizontal_space_including_whitespace = excess_horizontal_space;
83+
size_t whitespace_count = 0;
84+
if (text_align == CSS::TextAlign::Justify) {
85+
for (auto& fragment : line_box.fragments()) {
86+
if (fragment.is_justifiable_whitespace()) {
87+
++whitespace_count;
88+
excess_horizontal_space_including_whitespace += fragment.width();
89+
}
90+
}
91+
}
92+
93+
float justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / static_cast<float>(whitespace_count)) : 0;
94+
95+
for (size_t i = 0; i < line_box.fragments().size(); ++i) {
96+
auto& fragment = line_box.fragments()[i];
97+
98+
// Vertically align everyone's bottom to the line.
99+
// FIXME: Support other kinds of vertical alignment.
100+
fragment.set_offset({ roundf(x_offset + fragment.offset().x()), m_current_y + (m_max_height_on_current_line - fragment.height()) });
101+
102+
if (text_align == CSS::TextAlign::Justify
103+
&& fragment.is_justifiable_whitespace()
104+
&& fragment.width() != justified_space_width) {
105+
float diff = justified_space_width - fragment.width();
106+
fragment.set_width(justified_space_width);
107+
// Shift subsequent sibling fragments to the right to adjust for change in width.
108+
for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
109+
auto offset = line_box.fragments()[j].offset();
110+
offset.translate_by(diff, 0);
111+
line_box.fragments()[j].set_offset(offset);
112+
}
113+
}
114+
}
115+
116+
if (!line_box.fragments().is_empty()) {
117+
float left_edge = line_box.fragments().first().offset().x();
118+
float right_edge = line_box.fragments().last().offset().x() + line_box.fragments().last().width();
119+
float final_line_box_width = right_edge - left_edge;
120+
line_box.m_width = final_line_box_width;
121+
}
122+
}
123+
124+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibWeb/Layout/InlineFormattingContext.h>
10+
11+
namespace Web::Layout {
12+
13+
class LineBuilder {
14+
AK_MAKE_NONCOPYABLE(LineBuilder);
15+
AK_MAKE_NONMOVABLE(LineBuilder);
16+
17+
public:
18+
explicit LineBuilder(InlineFormattingContext&);
19+
~LineBuilder();
20+
21+
void break_line();
22+
void begin_new_line();
23+
void append_box(Box&);
24+
void append_text_chunk(TextNode&, size_t offset_in_node, size_t length_in_node, float width, float height);
25+
26+
void break_if_needed(LayoutMode, float next_item_width);
27+
28+
float available_width_for_current_line() const { return m_available_width_for_current_line; }
29+
30+
void update_last_line();
31+
32+
private:
33+
InlineFormattingContext& m_context;
34+
float m_available_width_for_current_line { 0 };
35+
float m_current_y { 0 };
36+
float m_max_height_on_current_line { 0 };
37+
};
38+
39+
}

0 commit comments

Comments
 (0)