瀏覽代碼

LibWeb: Use coordinate instead of WeakPtr for box->fragment connection

Using WeakPtr to remember which LineBoxFragment owns which Box was
imposing some annoying constraints on the layout code. Importantly, it
was forcing us to heap-allocate fragments, which makes it much harder to
clone a FormattingState.

This patch replaces the WeakPtr with a coordinate system instead.
Fragments are referred to by their line box index + fragment index
within the line box.
Andreas Kling 3 年之前
父節點
當前提交
16a47165ee

+ 6 - 4
Userland/Libraries/LibWeb/Layout/Box.cpp

@@ -220,8 +220,10 @@ void Box::set_content_size(Gfx::FloatSize const& size)
 
 
 Gfx::FloatPoint Box::effective_offset() const
 Gfx::FloatPoint Box::effective_offset() const
 {
 {
-    if (m_containing_line_box_fragment)
-        return m_containing_line_box_fragment->offset();
+    if (m_containing_line_box_fragment.has_value()) {
+        auto const& fragment = containing_block()->line_boxes()[m_containing_line_box_fragment->line_box_index].fragments()[m_containing_line_box_fragment->fragment_index];
+        return fragment.offset();
+    }
     return m_offset;
     return m_offset;
 }
 }
 
 
@@ -234,9 +236,9 @@ const Gfx::FloatRect Box::absolute_rect() const
     return rect;
     return rect;
 }
 }
 
 
-void Box::set_containing_line_box_fragment(LineBoxFragment& fragment)
+void Box::set_containing_line_box_fragment(LineBoxFragmentCoordinate fragment_coordinate)
 {
 {
-    m_containing_line_box_fragment = fragment.make_weak_ptr();
+    m_containing_line_box_fragment = fragment_coordinate;
 }
 }
 
 
 StackingContext* Box::enclosing_stacking_context()
 StackingContext* Box::enclosing_stacking_context()

+ 6 - 2
Userland/Libraries/LibWeb/Layout/Box.h

@@ -82,7 +82,11 @@ public:
 
 
     bool is_body() const;
     bool is_body() const;
 
 
-    void set_containing_line_box_fragment(LineBoxFragment&);
+    struct LineBoxFragmentCoordinate {
+        size_t line_box_index { 0 };
+        size_t fragment_index { 0 };
+    };
+    void set_containing_line_box_fragment(LineBoxFragmentCoordinate);
 
 
     StackingContext* stacking_context() { return m_stacking_context; }
     StackingContext* stacking_context() { return m_stacking_context; }
     const StackingContext* stacking_context() const { return m_stacking_context; }
     const StackingContext* stacking_context() const { return m_stacking_context; }
@@ -138,7 +142,7 @@ private:
     Gfx::FloatSize m_content_size;
     Gfx::FloatSize m_content_size;
 
 
     // Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
     // Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
-    WeakPtr<LineBoxFragment> m_containing_line_box_fragment;
+    Optional<LineBoxFragmentCoordinate> m_containing_line_box_fragment;
 
 
     OwnPtr<StackingContext> m_stacking_context;
     OwnPtr<StackingContext> m_stacking_context;
 
 

+ 0 - 5
Userland/Libraries/LibWeb/Layout/LineBox.cpp

@@ -27,11 +27,6 @@ void LineBox::add_fragment(Node const& layout_node, int start, int length, float
         m_fragments.append(make<LineBoxFragment>(layout_node, start, length, Gfx::FloatPoint(m_width + leading_size, 0.0f), Gfx::FloatSize(content_width, content_height), border_box_top, border_box_bottom, fragment_type));
         m_fragments.append(make<LineBoxFragment>(layout_node, start, length, Gfx::FloatPoint(m_width + leading_size, 0.0f), Gfx::FloatSize(content_width, content_height), border_box_top, border_box_bottom, fragment_type));
     }
     }
     m_width += content_width + leading_size + trailing_size;
     m_width += content_width + leading_size + trailing_size;
-
-    if (is<Box>(layout_node)) {
-        // FIXME: Move this to FormattingContext!
-        const_cast<Box&>(static_cast<Box const&>(layout_node)).set_containing_line_box_fragment(m_fragments.last());
-    }
 }
 }
 
 
 void LineBox::trim_trailing_whitespace()
 void LineBox::trim_trailing_whitespace()

+ 1 - 2
Userland/Libraries/LibWeb/Layout/LineBoxFragment.h

@@ -6,14 +6,13 @@
 
 
 #pragma once
 #pragma once
 
 
-#include <AK/Weakable.h>
 #include <LibGfx/Forward.h>
 #include <LibGfx/Forward.h>
 #include <LibGfx/Rect.h>
 #include <LibGfx/Rect.h>
 #include <LibWeb/Forward.h>
 #include <LibWeb/Forward.h>
 
 
 namespace Web::Layout {
 namespace Web::Layout {
 
 
-class LineBoxFragment : public Weakable<LineBoxFragment> {
+class LineBoxFragment {
     friend class LineBox;
     friend class LineBox;
 
 
 public:
 public:

+ 8 - 1
Userland/Libraries/LibWeb/Layout/LineBuilder.cpp

@@ -52,8 +52,15 @@ LineBox& LineBuilder::ensure_last_line_box()
 void LineBuilder::append_box(Box const& box, float leading_size, float trailing_size)
 void LineBuilder::append_box(Box const& box, float leading_size, float trailing_size)
 {
 {
     auto const& box_state = m_formatting_state.get(box);
     auto const& box_state = m_formatting_state.get(box);
-    ensure_last_line_box().add_fragment(box, 0, 0, leading_size, trailing_size, box_state.content_width, box_state.content_height, box_state.border_box_top(), box_state.border_box_bottom());
+    auto& line_box = ensure_last_line_box();
+    line_box.add_fragment(box, 0, 0, leading_size, trailing_size, box_state.content_width, box_state.content_height, box_state.border_box_top(), box_state.border_box_bottom());
     m_max_height_on_current_line = max(m_max_height_on_current_line, box_state.content_height);
     m_max_height_on_current_line = max(m_max_height_on_current_line, box_state.content_height);
+
+    // FIXME: Move this to FormattingContext!
+    const_cast<Box&>(box).set_containing_line_box_fragment({
+        .line_box_index = m_containing_block_state.line_boxes.size() - 1,
+        .fragment_index = line_box.fragments().size() - 1,
+    });
 }
 }
 
 
 void LineBuilder::append_text_chunk(TextNode const& text_node, size_t offset_in_node, size_t length_in_node, float leading_size, float trailing_size, float content_width, float content_height)
 void LineBuilder::append_text_chunk(TextNode const& text_node, size_t offset_in_node, size_t length_in_node, float leading_size, float trailing_size, float content_width, float content_height)