From 2cc39cfb0e7d21d460ef92c9d8fdb284bcec2be0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 6 Jan 2021 11:31:19 +0100 Subject: [PATCH] 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. --- Libraries/LibWeb/DOM/Document.cpp | 11 +++------- .../LibWeb/Layout/InlineFormattingContext.cpp | 4 ++-- Libraries/LibWeb/Layout/Node.cpp | 20 +++++++++++-------- Libraries/LibWeb/Layout/Node.h | 13 ++++++++++++ 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 6ad51f40f8..c781439706 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -343,15 +343,10 @@ RefPtr Document::background_image() const if (!body_layout_node) 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 {}; - - auto& image_value = static_cast(*background_image.value()); - if (!image_value.bitmap()) - return {}; - - return *image_value.bitmap(); + return background_image->bitmap(); } URL Document::complete_url(const String& string) const diff --git a/Libraries/LibWeb/Layout/InlineFormattingContext.cpp b/Libraries/LibWeb/Layout/InlineFormattingContext.cpp index 470591c538..b73daefecd 100644 --- a/Libraries/LibWeb/Layout/InlineFormattingContext.cpp +++ b/Libraries/LibWeb/Layout/InlineFormattingContext.cpp @@ -55,7 +55,7 @@ static AvailableSpaceForLineInfo available_space_for_line(const InlineFormatting AvailableSpaceForLineInfo info; // 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); auto& bfc = static_cast(*context.parent()); @@ -110,7 +110,7 @@ void InlineFormattingContext::run(Box&, LayoutMode layout_mode) containing_block().line_boxes().take_last(); 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 max_linebox_width = 0; diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cpp index 406660e503..14e007a906 100644 --- a/Libraries/LibWeb/Layout/Node.cpp +++ b/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 { if (is(*this)) @@ -223,6 +216,18 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style) auto& computed_values = static_cast(m_computed_values); 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(bgimage.value()); + } auto position = specified_style.position(); if (position.has_value()) @@ -310,5 +315,4 @@ bool Node::is_inline_block() const { return is_inline() && is(*this); } - } diff --git a/Libraries/LibWeb/Layout/Node.h b/Libraries/LibWeb/Layout/Node.h index a92f7bc879..d59cec6ebe 100644 --- a/Libraries/LibWeb/Layout/Node.h +++ b/Libraries/LibWeb/Layout/Node.h @@ -206,6 +206,9 @@ public: void apply_style(const CSS::StyleProperties&); 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: NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr); @@ -213,6 +216,9 @@ protected: private: CSS::ComputedValues m_computed_values; RefPtr m_font; + float m_line_height { 0 }; + float m_font_size { 0 }; + RefPtr m_background_image; NonnullRefPtr m_specified_style; CSS::Position m_position; @@ -240,6 +246,13 @@ inline const Gfx::Font& Node::font() const return parent()->font(); } +inline float Node::font_size() const +{ + if (m_has_style) + return static_cast(this)->font_size(); + return parent()->font_size(); +} + inline const CSS::StyleProperties& Node::specified_style() const { if (m_has_style)