1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00

LibWeb: Start making our layout system "transactional"

This patch adds a map of Layout::Node to FormattingState::NodeState.
Instead of updating layout nodes incrementally as layout progresses
through the formatting contexts, all updates are now written to the
corresponding NodeState instead.

At the end of layout, FormattingState::commit() is called, which
transfers all the values from the NodeState objects to the Node.

This will soon allow us to perform completely non-destructive layouts
which don't affect the tree.

Note that there are many imperfections here, and still many places
where we assign to the NodeState, but later read directly from the Node
instead. I'm just committing at this stage to make subsequent diffs
easier to understand.
This commit is contained in:
Andreas Kling 2022-02-20 15:51:24 +01:00
parent 561612f219
commit c9700e100e
27 changed files with 754 additions and 571 deletions

View file

@ -13,8 +13,9 @@
namespace Web::Layout {
InlineLevelIterator::InlineLevelIterator(Layout::InlineFormattingContext& inline_formatting_context, Layout::BlockContainer& container, LayoutMode layout_mode)
InlineLevelIterator::InlineLevelIterator(Layout::InlineFormattingContext& inline_formatting_context, Layout::FormattingState& formatting_state, Layout::BlockContainer const& container, LayoutMode layout_mode)
: m_inline_formatting_context(inline_formatting_context)
, m_formatting_state(formatting_state)
, m_container(container)
, m_next_node(container.first_child())
, m_layout_mode(layout_mode)
@ -22,18 +23,24 @@ InlineLevelIterator::InlineLevelIterator(Layout::InlineFormattingContext& inline
skip_to_next();
}
void InlineLevelIterator::enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics& node)
void InlineLevelIterator::enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const& node)
{
if (!m_extra_leading_metrics.has_value())
m_extra_leading_metrics = ExtraBoxMetrics {};
node.box_model().margin.left = node.computed_values().margin().left.resolved(node, CSS::Length::make_px(m_container.content_width())).to_px(node);
node.box_model().border.left = node.computed_values().border_left().width;
node.box_model().padding.left = node.computed_values().padding().left.resolved(node, CSS::Length::make_px(m_container.content_width())).to_px(node);
// FIXME: It's really weird that *this* is where we assign box model metrics for these layout nodes..
m_extra_leading_metrics->margin += node.box_model().margin.left;
m_extra_leading_metrics->border += node.box_model().border.left;
m_extra_leading_metrics->padding += node.box_model().padding.left;
auto& node_state = m_formatting_state.ensure(node);
auto const& container_state = m_formatting_state.get(m_container);
auto const& computed_values = node.computed_values();
node_state.margin_left = computed_values.margin().left.resolved(node, CSS::Length::make_px(container_state.content_width)).to_px(node);
node_state.border_left = computed_values.border_left().width;
node_state.padding_left = computed_values.padding().left.resolved(node, CSS::Length::make_px(container_state.content_width)).to_px(node);
m_extra_leading_metrics->margin += node_state.margin_left;
m_extra_leading_metrics->border += node_state.border_left;
m_extra_leading_metrics->padding += node_state.padding_left;
m_box_model_node_stack.append(node);
}
@ -44,26 +51,29 @@ void InlineLevelIterator::exit_node_with_box_model_metrics()
m_extra_trailing_metrics = ExtraBoxMetrics {};
auto& node = m_box_model_node_stack.last();
auto& node_state = m_formatting_state.ensure(node);
auto const& container_state = m_formatting_state.get(m_container);
auto const& computed_values = node.computed_values();
node.box_model().margin.right = node.computed_values().margin().right.resolved(node, CSS::Length::make_px(m_container.content_width())).to_px(node);
node.box_model().border.right = node.computed_values().border_right().width;
node.box_model().padding.right = node.computed_values().padding().right.resolved(node, CSS::Length::make_px(m_container.content_width())).to_px(node);
node_state.margin_right = computed_values.margin().right.resolved(node, CSS::Length::make_px(container_state.content_width)).to_px(node);
node_state.border_right = computed_values.border_right().width;
node_state.padding_right = computed_values.padding().right.resolved(node, CSS::Length::make_px(container_state.content_width)).to_px(node);
m_extra_trailing_metrics->margin += node.box_model().margin.right;
m_extra_trailing_metrics->border += node.box_model().border.right;
m_extra_trailing_metrics->padding += node.box_model().padding.right;
m_extra_trailing_metrics->margin += node_state.margin_right;
m_extra_trailing_metrics->border += node_state.border_right;
m_extra_trailing_metrics->padding += node_state.padding_right;
m_box_model_node_stack.take_last();
}
// This is similar to Layout::Node::next_in_pre_order() but will not descend into inline-block nodes.
Layout::Node* InlineLevelIterator::next_inline_node_in_pre_order(Layout::Node& current, Layout::Node const* stay_within)
Layout::Node const* InlineLevelIterator::next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within)
{
if (current.first_child() && current.first_child()->is_inline() && !current.is_inline_block())
return current.first_child();
Layout::Node* node = &current;
Layout::Node* next = nullptr;
Layout::Node const* node = &current;
Layout::Node const* next = nullptr;
while (!(next = node->next_sibling())) {
node = node->parent();
@ -91,7 +101,7 @@ void InlineLevelIterator::compute_next()
void InlineLevelIterator::skip_to_next()
{
if (m_next_node && is<Layout::NodeWithStyleAndBoxModelMetrics>(*m_next_node))
enter_node_with_box_model_metrics(static_cast<Layout::NodeWithStyleAndBoxModelMetrics&>(*m_next_node));
enter_node_with_box_model_metrics(static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*m_next_node));
m_current_node = m_next_node;
compute_next();
@ -103,10 +113,11 @@ Optional<InlineLevelIterator::Item> InlineLevelIterator::next(float available_wi
return {};
if (is<Layout::TextNode>(*m_current_node)) {
auto& text_node = static_cast<Layout::TextNode&>(*m_current_node);
auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node);
if (!m_text_node_context.has_value()) {
bool previous_is_empty_or_ends_in_whitespace = m_container.line_boxes().is_empty() || m_container.line_boxes().last().is_empty_or_ends_in_whitespace();
auto& line_boxes = m_formatting_state.get(m_container).line_boxes;
bool previous_is_empty_or_ends_in_whitespace = line_boxes.is_empty() || line_boxes.last().is_empty_or_ends_in_whitespace();
enter_text_node(text_node, previous_is_empty_or_ends_in_whitespace);
}
@ -160,11 +171,13 @@ Optional<InlineLevelIterator::Item> InlineLevelIterator::next(float available_wi
}
if (is<Layout::ReplacedBox>(*m_current_node)) {
auto& replaced_box = static_cast<Layout::ReplacedBox&>(*m_current_node);
replaced_box.prepare_for_replaced_layout();
auto& replaced_box = static_cast<Layout::ReplacedBox const&>(*m_current_node);
// FIXME: This const_cast is gross.
const_cast<Layout::ReplacedBox&>(replaced_box).prepare_for_replaced_layout();
}
auto& box = verify_cast<Layout::Box>(*m_current_node);
auto& box_state = m_formatting_state.get(box);
m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
skip_to_next();
@ -173,19 +186,19 @@ Optional<InlineLevelIterator::Item> InlineLevelIterator::next(float available_wi
.node = &box,
.offset_in_node = 0,
.length_in_node = 0,
.width = box.content_width(),
.padding_start = box.box_model().padding.left,
.padding_end = box.box_model().padding.right,
.border_start = box.box_model().border.left,
.border_end = box.box_model().border.right,
.margin_start = box.box_model().margin.left,
.margin_end = box.box_model().margin.right,
.width = box_state.content_width,
.padding_start = box_state.padding_left,
.padding_end = box_state.padding_right,
.border_start = box_state.border_left,
.border_end = box_state.border_right,
.margin_start = box_state.margin_left,
.margin_end = box_state.margin_right,
};
add_extra_box_model_metrics_to_item(item, true, true);
return item;
}
void InlineLevelIterator::enter_text_node(Layout::TextNode& text_node, bool previous_is_empty_or_ends_in_whitespace)
void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node, bool previous_is_empty_or_ends_in_whitespace)
{
bool do_collapse = true;
bool do_wrap_lines = true;
@ -209,7 +222,8 @@ void InlineLevelIterator::enter_text_node(Layout::TextNode& text_node, bool prev
do_respect_linebreaks = true;
}
text_node.compute_text_for_rendering(do_collapse, previous_is_empty_or_ends_in_whitespace);
// FIXME: The const_cast here is gross.
const_cast<TextNode&>(text_node).compute_text_for_rendering(do_collapse, previous_is_empty_or_ends_in_whitespace);
m_text_node_context = TextNodeContext {
.do_collapse = do_collapse,