mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 17:17:45 +00:00
LibWeb: Use separate structure to represent fragments in paintable tree
This is a part of refactoring towards making the paintable tree independent of the layout tree. Now, instead of transferring text fragments from the layout tree to the paintable tree during the layout commit phase, we allocate separate PaintableFragments that contain only the information necessary for painting. Doing this also allows us to get rid LineBoxes, as they are used only during layout.
This commit is contained in:
parent
785fa60cca
commit
de32b77ceb
401 changed files with 2122 additions and 3614 deletions
|
@ -8,9 +8,7 @@
|
|||
#include <LibWeb/Layout/AvailableSpace.h>
|
||||
#include <LibWeb/Layout/BlockContainer.h>
|
||||
#include <LibWeb/Layout/LayoutState.h>
|
||||
#include <LibWeb/Layout/TextNode.h>
|
||||
#include <LibWeb/Layout/Viewport.h>
|
||||
#include <LibWeb/Painting/BorderRadiiData.h>
|
||||
#include <LibWeb/Painting/InlinePaintable.h>
|
||||
#include <LibWeb/Painting/SVGPathPaintable.h>
|
||||
|
||||
|
@ -86,9 +84,8 @@ static CSSPixelRect measure_scrollable_overflow(Box const& box)
|
|||
|
||||
// - All line boxes directly contained by the scroll container.
|
||||
if (is<Painting::PaintableWithLines>(box.paintable())) {
|
||||
auto const& line_boxes = static_cast<Painting::PaintableWithLines const&>(*box.paintable()).line_boxes();
|
||||
for (auto const& line_box : line_boxes) {
|
||||
scrollable_overflow_rect = scrollable_overflow_rect.united(line_box.absolute_rect());
|
||||
for (auto const& fragment : static_cast<Painting::PaintableWithLines const&>(*box.paintable()).fragments()) {
|
||||
scrollable_overflow_rect = scrollable_overflow_rect.united(fragment.absolute_rect());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -156,8 +153,8 @@ void LayoutState::resolve_relative_positions(Vector<Painting::PaintableWithLines
|
|||
// line box fragment in the parent block container that contains it.
|
||||
auto const& containing_line_box_fragment = used_values.containing_line_box_fragment.value();
|
||||
auto const& containing_block = *node.containing_block();
|
||||
auto const& containing_block_paintable = verify_cast<Painting::PaintableWithLines>(*containing_block.paintable_box());
|
||||
auto const& fragment = containing_block_paintable.line_boxes()[containing_line_box_fragment.line_box_index].fragments()[containing_line_box_fragment.fragment_index];
|
||||
auto const& containing_block_used_values = get(containing_block);
|
||||
auto const& fragment = containing_block_used_values.line_boxes[containing_line_box_fragment.line_box_index].fragments()[containing_line_box_fragment.fragment_index];
|
||||
|
||||
// The fragment has the final offset for the atomic inline, so we just need to copy it from there.
|
||||
offset = fragment.offset();
|
||||
|
@ -175,26 +172,24 @@ void LayoutState::resolve_relative_positions(Vector<Painting::PaintableWithLines
|
|||
|
||||
// Line box fragments:
|
||||
for (auto const& paintable_with_lines : paintables_with_lines) {
|
||||
for (auto const& line_box : paintable_with_lines.line_boxes()) {
|
||||
for (auto& fragment : line_box.fragments()) {
|
||||
auto const& fragment_node = fragment.layout_node();
|
||||
if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*fragment_node.parent()))
|
||||
continue;
|
||||
// Collect effective relative position offset from inline-flow parent chain.
|
||||
CSSPixelPoint offset;
|
||||
for (auto* ancestor = fragment_node.parent(); ancestor; ancestor = ancestor->parent()) {
|
||||
if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*ancestor))
|
||||
break;
|
||||
if (!ancestor->display().is_inline_outside() || !ancestor->display().is_flow_inside())
|
||||
break;
|
||||
if (ancestor->computed_values().position() == CSS::Positioning::Relative) {
|
||||
auto const& ancestor_node = static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*ancestor);
|
||||
auto const& inset = ancestor_node.box_model().inset;
|
||||
offset.translate_by(inset.left, inset.top);
|
||||
}
|
||||
for (auto& fragment : paintable_with_lines.fragments()) {
|
||||
auto const& fragment_node = fragment.layout_node();
|
||||
if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*fragment_node.parent()))
|
||||
continue;
|
||||
// Collect effective relative position offset from inline-flow parent chain.
|
||||
CSSPixelPoint offset;
|
||||
for (auto* ancestor = fragment_node.parent(); ancestor; ancestor = ancestor->parent()) {
|
||||
if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*ancestor))
|
||||
break;
|
||||
if (!ancestor->display().is_inline_outside() || !ancestor->display().is_flow_inside())
|
||||
break;
|
||||
if (ancestor->computed_values().position() == CSS::Positioning::Relative) {
|
||||
auto const& ancestor_node = static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*ancestor);
|
||||
auto const& inset = ancestor_node.box_model().inset;
|
||||
offset.translate_by(inset.left, inset.top);
|
||||
}
|
||||
const_cast<LineBoxFragment&>(fragment).set_offset(fragment.offset().translated(offset));
|
||||
}
|
||||
const_cast<Painting::PaintableFragment&>(fragment).set_offset(fragment.offset().translated(offset));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -298,10 +293,10 @@ void LayoutState::resolve_border_radii()
|
|||
}
|
||||
|
||||
for (auto& inline_paintable : inline_paintables) {
|
||||
Vector<Layout::LineBoxFragment&> fragments;
|
||||
Vector<Painting::PaintableFragment&> fragments;
|
||||
verify_cast<Painting::PaintableWithLines>(*inline_paintable.containing_block()->paintable_box()).for_each_fragment([&](auto& fragment) {
|
||||
if (inline_paintable.layout_node().is_inclusive_ancestor_of(fragment.layout_node()))
|
||||
fragments.append(const_cast<Layout::LineBoxFragment&>(fragment));
|
||||
fragments.append(const_cast<Painting::PaintableFragment&>(fragment));
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
|
@ -410,7 +405,10 @@ void LayoutState::commit(Box& root)
|
|||
|
||||
if (is<Painting::PaintableWithLines>(paintable_box)) {
|
||||
auto& paintable_with_lines = static_cast<Painting::PaintableWithLines&>(paintable_box);
|
||||
paintable_with_lines.set_line_boxes(move(used_values.line_boxes));
|
||||
for (auto& line_box : used_values.line_boxes) {
|
||||
for (auto& fragment : line_box.fragments())
|
||||
paintable_with_lines.add_fragment(fragment);
|
||||
}
|
||||
paintables_with_lines.append(paintable_with_lines);
|
||||
}
|
||||
|
||||
|
@ -432,14 +430,9 @@ void LayoutState::commit(Box& root)
|
|||
// - Measure absolute rect of each line box.
|
||||
// - Collect all text nodes, so we can create paintables for them later.
|
||||
for (auto& paintable_with_lines : paintables_with_lines) {
|
||||
for (auto& line_box : paintable_with_lines.line_boxes()) {
|
||||
CSSPixelRect line_box_absolute_rect;
|
||||
for (auto const& fragment : line_box.fragments()) {
|
||||
line_box_absolute_rect = line_box_absolute_rect.united(fragment.absolute_rect());
|
||||
if (fragment.layout_node().is_text_node())
|
||||
text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
|
||||
}
|
||||
const_cast<LineBox&>(line_box).set_absolute_rect(line_box_absolute_rect);
|
||||
for (auto& fragment : paintable_with_lines.fragments()) {
|
||||
if (fragment.layout_node().is_text_node())
|
||||
text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
#include <AK/Utf8View.h>
|
||||
#include <LibWeb/DOM/Range.h>
|
||||
#include <LibWeb/Layout/LayoutState.h>
|
||||
#include <LibWeb/Layout/LineBoxFragment.h>
|
||||
#include <LibWeb/Layout/TextNode.h>
|
||||
#include <LibWeb/Layout/Viewport.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
@ -42,113 +40,6 @@ CSSPixelRect const LineBoxFragment::absolute_rect() const
|
|||
return rect;
|
||||
}
|
||||
|
||||
int LineBoxFragment::text_index_at(CSSPixels x) const
|
||||
{
|
||||
if (!is<TextNode>(layout_node()))
|
||||
return 0;
|
||||
auto& layout_text = verify_cast<TextNode>(layout_node());
|
||||
auto& font = layout_text.first_available_font();
|
||||
Utf8View view(text());
|
||||
|
||||
CSSPixels relative_x = x - absolute_x();
|
||||
CSSPixels glyph_spacing = font.glyph_spacing();
|
||||
|
||||
if (relative_x < 0)
|
||||
return 0;
|
||||
|
||||
CSSPixels width_so_far = 0;
|
||||
for (auto it = view.begin(); it != view.end(); ++it) {
|
||||
auto previous_it = it;
|
||||
CSSPixels glyph_width = CSSPixels::nearest_value_for(font.glyph_or_emoji_width(it));
|
||||
|
||||
if ((width_so_far + glyph_width + glyph_spacing / 2) > relative_x)
|
||||
return m_start + view.byte_offset_of(previous_it);
|
||||
|
||||
width_so_far += glyph_width + glyph_spacing;
|
||||
}
|
||||
|
||||
return m_start + m_length;
|
||||
}
|
||||
|
||||
CSSPixelRect LineBoxFragment::selection_rect(Gfx::Font const& font) const
|
||||
{
|
||||
if (layout_node().selection_state() == Node::SelectionState::None)
|
||||
return {};
|
||||
|
||||
if (layout_node().selection_state() == Node::SelectionState::Full)
|
||||
return absolute_rect();
|
||||
|
||||
if (!is<TextNode>(layout_node()))
|
||||
return {};
|
||||
|
||||
auto selection = layout_node().root().selection();
|
||||
if (!selection)
|
||||
return {};
|
||||
auto range = selection->range();
|
||||
if (!range)
|
||||
return {};
|
||||
|
||||
// FIXME: m_start and m_length should be unsigned and then we won't need these casts.
|
||||
auto const start_index = static_cast<unsigned>(m_start);
|
||||
auto const end_index = static_cast<unsigned>(m_start) + static_cast<unsigned>(m_length);
|
||||
auto text = this->text();
|
||||
|
||||
if (layout_node().selection_state() == Node::SelectionState::StartAndEnd) {
|
||||
// we are in the start/end node (both the same)
|
||||
if (start_index > range->end_offset())
|
||||
return {};
|
||||
if (end_index < range->start_offset())
|
||||
return {};
|
||||
|
||||
if (range->start_offset() == range->end_offset())
|
||||
return {};
|
||||
|
||||
auto selection_start_in_this_fragment = max(0, range->start_offset() - m_start);
|
||||
auto selection_end_in_this_fragment = min(m_length, range->end_offset() - m_start);
|
||||
auto pixel_distance_to_first_selected_character = CSSPixels::nearest_value_for(font.width(text.substring_view(0, selection_start_in_this_fragment)));
|
||||
auto pixel_width_of_selection = CSSPixels::nearest_value_for(font.width(text.substring_view(selection_start_in_this_fragment, selection_end_in_this_fragment - selection_start_in_this_fragment))) + 1;
|
||||
|
||||
auto rect = absolute_rect();
|
||||
rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
|
||||
rect.set_width(pixel_width_of_selection);
|
||||
|
||||
return rect;
|
||||
}
|
||||
if (layout_node().selection_state() == Node::SelectionState::Start) {
|
||||
// we are in the start node
|
||||
if (end_index < range->start_offset())
|
||||
return {};
|
||||
|
||||
auto selection_start_in_this_fragment = max(0, range->start_offset() - m_start);
|
||||
auto selection_end_in_this_fragment = m_length;
|
||||
auto pixel_distance_to_first_selected_character = CSSPixels::nearest_value_for(font.width(text.substring_view(0, selection_start_in_this_fragment)));
|
||||
auto pixel_width_of_selection = CSSPixels::nearest_value_for(font.width(text.substring_view(selection_start_in_this_fragment, selection_end_in_this_fragment - selection_start_in_this_fragment))) + 1;
|
||||
|
||||
auto rect = absolute_rect();
|
||||
rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
|
||||
rect.set_width(pixel_width_of_selection);
|
||||
|
||||
return rect;
|
||||
}
|
||||
if (layout_node().selection_state() == Node::SelectionState::End) {
|
||||
// we are in the end node
|
||||
if (start_index > range->end_offset())
|
||||
return {};
|
||||
|
||||
auto selection_start_in_this_fragment = 0;
|
||||
auto selection_end_in_this_fragment = min(range->end_offset() - m_start, m_length);
|
||||
auto pixel_distance_to_first_selected_character = CSSPixels::nearest_value_for(font.width(text.substring_view(0, selection_start_in_this_fragment)));
|
||||
auto pixel_width_of_selection = CSSPixels::nearest_value_for(font.width(text.substring_view(selection_start_in_this_fragment, selection_end_in_this_fragment - selection_start_in_this_fragment))) + 1;
|
||||
|
||||
auto rect = absolute_rect();
|
||||
rect.set_x(rect.x() + pixel_distance_to_first_selected_character);
|
||||
rect.set_width(pixel_width_of_selection);
|
||||
|
||||
return rect;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
bool LineBoxFragment::is_atomic_inline() const
|
||||
{
|
||||
return layout_node().is_replaced_box() || (layout_node().display().is_inline_outside() && !layout_node().display().is_flow_inside());
|
||||
|
|
|
@ -36,10 +36,7 @@ public:
|
|||
int length() const { return m_length; }
|
||||
CSSPixelRect const absolute_rect() const;
|
||||
|
||||
CSSPixelPoint offset() const
|
||||
{
|
||||
return m_offset;
|
||||
}
|
||||
CSSPixelPoint offset() const { return m_offset; }
|
||||
void set_offset(CSSPixelPoint offset) { m_offset = offset; }
|
||||
|
||||
// The baseline of a fragment is the number of pixels from the top to the text baseline.
|
||||
|
@ -55,33 +52,16 @@ public:
|
|||
CSSPixels width() const { return m_size.width(); }
|
||||
CSSPixels height() const { return m_size.height(); }
|
||||
|
||||
CSSPixels border_box_height() const
|
||||
{
|
||||
return m_border_box_top + height() + m_border_box_bottom;
|
||||
}
|
||||
CSSPixels border_box_top() const { return m_border_box_top; }
|
||||
CSSPixels border_box_bottom() const { return m_border_box_bottom; }
|
||||
|
||||
CSSPixels absolute_x() const { return absolute_rect().x(); }
|
||||
|
||||
bool ends_in_whitespace() const;
|
||||
bool is_justifiable_whitespace() const;
|
||||
StringView text() const;
|
||||
|
||||
int text_index_at(CSSPixels x) const;
|
||||
|
||||
CSSPixelRect selection_rect(Gfx::Font const&) const;
|
||||
|
||||
bool is_atomic_inline() const;
|
||||
|
||||
Vector<Gfx::DrawGlyphOrEmoji> const& glyph_run() const { return m_glyph_run; }
|
||||
|
||||
Painting::BorderRadiiData const& border_radii_data() const { return m_border_radii_data; }
|
||||
void set_border_radii_data(Painting::BorderRadiiData const& border_radii_data) { m_border_radii_data = border_radii_data; }
|
||||
|
||||
bool contained_by_inline_node() const { return m_contained_by_inline_node; }
|
||||
void set_contained_by_inline_node() { m_contained_by_inline_node = true; }
|
||||
|
||||
private:
|
||||
JS::NonnullGCPtr<Node const> m_layout_node;
|
||||
int m_start { 0 };
|
||||
|
@ -92,8 +72,6 @@ private:
|
|||
CSSPixels m_border_box_bottom { 0 };
|
||||
CSSPixels m_baseline { 0 };
|
||||
Vector<Gfx::DrawGlyphOrEmoji> m_glyph_run;
|
||||
Painting::BorderRadiiData m_border_radii_data;
|
||||
bool m_contained_by_inline_node { false };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue