瀏覽代碼

LibWeb: Copy some properties from specified style into layout node

Another step towards not having to carry the full specified style with
us everywhere. This isn't the ideal final layout, since we're mixing
computed and used values a bit randomly here, but one step at a time.
Andreas Kling 4 年之前
父節點
當前提交
2cc39cfb0e

+ 3 - 8
Libraries/LibWeb/DOM/Document.cpp

@@ -343,15 +343,10 @@ RefPtr<Gfx::Bitmap> Document::background_image() const
     if (!body_layout_node)
     if (!body_layout_node)
         return {};
         return {};
 
 
-    auto background_image = body_layout_node->specified_style().property(CSS::PropertyID::BackgroundImage);
-    if (!background_image.has_value() || !background_image.value()->is_image())
+    auto background_image = body_layout_node->background_image();
+    if (!background_image)
         return {};
         return {};
-
-    auto& image_value = static_cast<const CSS::ImageStyleValue&>(*background_image.value());
-    if (!image_value.bitmap())
-        return {};
-
-    return *image_value.bitmap();
+    return background_image->bitmap();
 }
 }
 
 
 URL Document::complete_url(const String& string) const
 URL Document::complete_url(const String& string) const

+ 2 - 2
Libraries/LibWeb/Layout/InlineFormattingContext.cpp

@@ -55,7 +55,7 @@ static AvailableSpaceForLineInfo available_space_for_line(const InlineFormatting
     AvailableSpaceForLineInfo info;
     AvailableSpaceForLineInfo info;
 
 
     // FIXME: This is a total hack guess since we don't actually know the final y position of lines here!
     // FIXME: This is a total hack guess since we don't actually know the final y position of lines here!
-    float line_height = context.containing_block().specified_style().line_height(context.containing_block());
+    float line_height = context.containing_block().line_height();
     float y = (line_index * line_height);
     float y = (line_index * line_height);
 
 
     auto& bfc = static_cast<const BlockFormattingContext&>(*context.parent());
     auto& bfc = static_cast<const BlockFormattingContext&>(*context.parent());
@@ -110,7 +110,7 @@ void InlineFormattingContext::run(Box&, LayoutMode layout_mode)
         containing_block().line_boxes().take_last();
         containing_block().line_boxes().take_last();
 
 
     auto text_align = containing_block().computed_values().text_align();
     auto text_align = containing_block().computed_values().text_align();
-    float min_line_height = containing_block().specified_style().line_height(containing_block());
+    float min_line_height = containing_block().line_height();
     float content_height = 0;
     float content_height = 0;
     float max_linebox_width = 0;
     float max_linebox_width = 0;
 
 

+ 12 - 8
Libraries/LibWeb/Layout/Node.cpp

@@ -157,13 +157,6 @@ void Node::set_needs_display()
     }
     }
 }
 }
 
 
-float Node::font_size() const
-{
-    // FIXME: This doesn't work right for relative font-sizes
-    auto length = specified_style().length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(10, CSS::Length::Type::Px));
-    return length.raw_value();
-}
-
 Gfx::FloatPoint Node::box_type_agnostic_position() const
 Gfx::FloatPoint Node::box_type_agnostic_position() const
 {
 {
     if (is<Box>(*this))
     if (is<Box>(*this))
@@ -223,6 +216,18 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
     auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
     auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
 
 
     m_font = specified_style.font();
     m_font = specified_style.font();
+    m_line_height = specified_style.line_height(*this);
+
+    {
+        // FIXME: This doesn't work right for relative font-sizes
+        auto length = specified_style.length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(10, CSS::Length::Type::Px));
+        m_font_size = length.raw_value();
+    }
+
+    auto bgimage = specified_style.property(CSS::PropertyID::BackgroundImage);
+    if (bgimage.has_value() && bgimage.value()->is_image()) {
+        m_background_image = static_ptr_cast<CSS::ImageStyleValue>(bgimage.value());
+    }
 
 
     auto position = specified_style.position();
     auto position = specified_style.position();
     if (position.has_value())
     if (position.has_value())
@@ -310,5 +315,4 @@ bool Node::is_inline_block() const
 {
 {
     return is_inline() && is<BlockBox>(*this);
     return is_inline() && is<BlockBox>(*this);
 }
 }
-
 }
 }

+ 13 - 0
Libraries/LibWeb/Layout/Node.h

@@ -206,6 +206,9 @@ public:
     void apply_style(const CSS::StyleProperties&);
     void apply_style(const CSS::StyleProperties&);
 
 
     const Gfx::Font& font() const { return *m_font; }
     const Gfx::Font& font() const { return *m_font; }
+    float line_height() const { return m_line_height; }
+    float font_size() const { return m_font_size; }
+    const CSS::ImageStyleValue* background_image() const { return m_background_image; }
 
 
 protected:
 protected:
     NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
     NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
@@ -213,6 +216,9 @@ protected:
 private:
 private:
     CSS::ComputedValues m_computed_values;
     CSS::ComputedValues m_computed_values;
     RefPtr<Gfx::Font> m_font;
     RefPtr<Gfx::Font> m_font;
+    float m_line_height { 0 };
+    float m_font_size { 0 };
+    RefPtr<CSS::ImageStyleValue> m_background_image;
 
 
     NonnullRefPtr<CSS::StyleProperties> m_specified_style;
     NonnullRefPtr<CSS::StyleProperties> m_specified_style;
     CSS::Position m_position;
     CSS::Position m_position;
@@ -240,6 +246,13 @@ inline const Gfx::Font& Node::font() const
     return parent()->font();
     return parent()->font();
 }
 }
 
 
+inline float Node::font_size() const
+{
+    if (m_has_style)
+        return static_cast<const NodeWithStyle*>(this)->font_size();
+    return parent()->font_size();
+}
+
 inline const CSS::StyleProperties& Node::specified_style() const
 inline const CSS::StyleProperties& Node::specified_style() const
 {
 {
     if (m_has_style)
     if (m_has_style)