From c82d5174471ee769ae4e6b66e72268b1a5c6cf46 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 12 Jan 2024 15:34:13 +0100 Subject: [PATCH] LibWeb: Move line-height from NodeWithStyle to ComputedValues There's no need for this to live in the NodeWithStyle anymore. By moving it to ComputedValues we get all the right inheritance behavior for free. --- .../Libraries/LibWeb/CSS/ComputedValues.h | 4 ++++ .../CSS/ResolvedCSSStyleDeclaration.cpp | 2 +- .../LibWeb/Layout/BlockFormattingContext.cpp | 2 +- .../LibWeb/Layout/InlineFormattingContext.cpp | 4 ++-- .../Libraries/LibWeb/Layout/LineBuilder.cpp | 20 +++++++++---------- Userland/Libraries/LibWeb/Layout/Node.cpp | 6 ++---- Userland/Libraries/LibWeb/Layout/Node.h | 2 -- .../Libraries/LibWeb/Layout/TreeBuilder.cpp | 4 ---- 8 files changed, 20 insertions(+), 24 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/ComputedValues.h b/Userland/Libraries/LibWeb/CSS/ComputedValues.h index 7d411c8e12..5188814d40 100644 --- a/Userland/Libraries/LibWeb/CSS/ComputedValues.h +++ b/Userland/Libraries/LibWeb/CSS/ComputedValues.h @@ -81,6 +81,7 @@ public: static CSSPixels font_size() { return 16; } static int font_weight() { return 400; } static CSS::FontVariant font_variant() { return CSS::FontVariant::Normal; } + static CSSPixels line_height() { return 0; } static CSS::Float float_() { return CSS::Float::None; } static CSS::Length border_spacing() { return CSS::Length::make_px(0); } static CSS::CaptionSide caption_side() { return CSS::CaptionSide::Top; } @@ -390,6 +391,7 @@ public: CSSPixels font_size() const { return m_inherited.font_size; } int font_weight() const { return m_inherited.font_weight; } CSS::FontVariant font_variant() const { return m_inherited.font_variant; } + CSSPixels line_height() const { return m_inherited.line_height; } CSS::Time transition_delay() const { return m_noninherited.transition_delay; } Color outline_color() const { return m_noninherited.outline_color; } @@ -417,6 +419,7 @@ protected: CSSPixels font_size { InitialValues::font_size() }; int font_weight { InitialValues::font_weight() }; CSS::FontVariant font_variant { InitialValues::font_variant() }; + CSSPixels line_height { InitialValues::line_height() }; CSS::BorderCollapse border_collapse { InitialValues::border_collapse() }; CSS::Length border_spacing_horizontal { InitialValues::border_spacing() }; CSS::Length border_spacing_vertical { InitialValues::border_spacing() }; @@ -547,6 +550,7 @@ public: void set_font_size(CSSPixels font_size) { m_inherited.font_size = font_size; } void set_font_weight(int font_weight) { m_inherited.font_weight = font_weight; } void set_font_variant(CSS::FontVariant font_variant) { m_inherited.font_variant = font_variant; } + void set_line_height(CSSPixels line_height) { m_inherited.line_height = line_height; } void set_border_spacing_horizontal(CSS::Length border_spacing_horizontal) { m_inherited.border_spacing_horizontal = border_spacing_horizontal; } void set_border_spacing_vertical(CSS::Length border_spacing_vertical) { m_inherited.border_spacing_vertical = border_spacing_vertical; } void set_caption_side(CSS::CaptionSide caption_side) { m_inherited.caption_side = caption_side; } diff --git a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp index c363c77365..35b6a786a9 100644 --- a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp @@ -246,7 +246,7 @@ RefPtr ResolvedCSSStyleDeclaration::style_value_for_property(L auto line_height = static_cast(*layout_node.dom_node()).computed_css_values()->property(property_id); if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal) return line_height; - return LengthStyleValue::create(Length::make_px(layout_node.line_height())); + return LengthStyleValue::create(Length::make_px(layout_node.computed_values().line_height())); } // FIXME: -> block-size diff --git a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp index a5e57e88ee..20fef7b4fa 100644 --- a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp @@ -1134,7 +1134,7 @@ void BlockFormattingContext::layout_list_item_marker(ListItemBox const& list_ite list_item_state.set_content_width(list_item_state.content_width() - final_marker_width); } - auto offset_y = max(CSSPixels(0), (marker.line_height() - marker_state.content_height()) / 2); + auto offset_y = max(CSSPixels(0), (marker.computed_values().line_height() - marker_state.content_height()) / 2); auto space_and_containing_margin = intrusion_by_floats_into_box(list_item_box, offset_y); marker_state.set_content_offset({ space_and_containing_margin.left - final_marker_width, offset_y }); diff --git a/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp index 3e8093dc34..fffbca683b 100644 --- a/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp @@ -335,7 +335,7 @@ void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode) item.margin_start, item.margin_end, item.width, - text_node.line_height(), + text_node.computed_values().line_height(), item.glyph_run); break; } @@ -371,7 +371,7 @@ bool InlineFormattingContext::any_floats_intrude_at_y(CSSPixels y) const bool InlineFormattingContext::can_fit_new_line_at_y(CSSPixels y) const { auto top_intrusions = parent().intrusion_by_floats_into_box(containing_block(), y); - auto bottom_intrusions = parent().intrusion_by_floats_into_box(containing_block(), y + containing_block().line_height() - 1); + auto bottom_intrusions = parent().intrusion_by_floats_into_box(containing_block(), y + containing_block().computed_values().line_height() - 1); auto left_edge = [](auto& space) -> CSSPixels { return space.left; diff --git a/Userland/Libraries/LibWeb/Layout/LineBuilder.cpp b/Userland/Libraries/LibWeb/Layout/LineBuilder.cpp index 10879ed945..3101316621 100644 --- a/Userland/Libraries/LibWeb/Layout/LineBuilder.cpp +++ b/Userland/Libraries/LibWeb/Layout/LineBuilder.cpp @@ -50,7 +50,7 @@ void LineBuilder::begin_new_line(bool increment_y, bool is_first_break_in_sequen if (increment_y) { if (is_first_break_in_sequence) { // First break is simple, just go to the start of the next line. - m_current_y += max(m_max_height_on_current_line, m_context.containing_block().line_height()); + m_current_y += max(m_max_height_on_current_line, m_context.containing_block().computed_values().line_height()); } else { // We're doing more than one break in a row. // This means we're trying to squeeze past intruding floats. @@ -145,7 +145,7 @@ bool LineBuilder::should_break(CSSPixels next_item_width) // at this Y coordinate, we don't need to break before inserting anything. if (!m_context.any_floats_intrude_at_y(m_current_y)) return false; - if (!m_context.any_floats_intrude_at_y(m_current_y + m_context.containing_block().line_height())) + if (!m_context.any_floats_intrude_at_y(m_current_y + m_context.containing_block().computed_values().line_height())) return false; } auto current_line_width = ensure_last_line_box().width(); @@ -164,7 +164,7 @@ void LineBuilder::update_last_line() auto text_align = m_context.containing_block().computed_values().text_align(); - auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height()); + auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().computed_values().line_height()); CSSPixels x_offset_top = m_context.leftmost_x_offset_at(m_current_y); CSSPixels x_offset_bottom = m_context.leftmost_x_offset_at(m_current_y + current_line_height - 1); CSSPixels x_offset = max(x_offset_top, x_offset_bottom); @@ -193,7 +193,7 @@ void LineBuilder::update_last_line() auto strut_baseline = [&] { auto& font = m_context.containing_block().first_available_font(); - auto const line_height = m_context.containing_block().line_height(); + auto const line_height = m_context.containing_block().computed_values().line_height(); auto const font_metrics = font.pixel_metrics(); auto const typographic_height = CSSPixels::nearest_value_for(font_metrics.ascent + font_metrics.descent); auto const leading = line_height - typographic_height; @@ -205,7 +205,7 @@ void LineBuilder::update_last_line() CSSPixels line_box_baseline = strut_baseline; for (auto& fragment : line_box.fragments()) { auto const& font = fragment.layout_node().first_available_font(); - auto const line_height = fragment.layout_node().line_height(); + auto const line_height = fragment.layout_node().computed_values().line_height(); auto const font_metrics = font.pixel_metrics(); auto const typographic_height = CSSPixels::nearest_value_for(font_metrics.ascent + font_metrics.descent); auto const leading = line_height - typographic_height; @@ -240,7 +240,7 @@ void LineBuilder::update_last_line() // Start with the "strut", an imaginary zero-width box at the start of each line box. auto strut_top = m_current_y; - auto strut_bottom = m_current_y + m_context.containing_block().line_height(); + auto strut_bottom = m_current_y + m_context.containing_block().computed_values().line_height(); CSSPixels uppermost_box_top = strut_top; CSSPixels lowermost_box_bottom = strut_bottom; @@ -284,7 +284,7 @@ void LineBuilder::update_last_line() auto vertical_align_amount = length_percentage->length().to_px(fragment.layout_node()); new_fragment_y = y_value_for_alignment(CSS::VerticalAlign::Baseline) - vertical_align_amount; } else if (length_percentage->is_percentage()) { - auto vertical_align_amount = m_context.containing_block().line_height().scaled(length_percentage->percentage().as_fraction()); + auto vertical_align_amount = m_context.containing_block().computed_values().line_height().scaled(length_percentage->percentage().as_fraction()); new_fragment_y = y_value_for_alignment(CSS::VerticalAlign::Baseline) - vertical_align_amount; } } @@ -303,7 +303,7 @@ void LineBuilder::update_last_line() } else { auto font_metrics = fragment.layout_node().first_available_font().pixel_metrics(); auto typographic_height = CSSPixels::nearest_value_for(font_metrics.ascent + font_metrics.descent); - auto leading = fragment.layout_node().line_height() - typographic_height; + auto leading = fragment.layout_node().computed_values().line_height() - typographic_height; auto half_leading = leading / 2; top_of_inline_box = (fragment.offset().y() + fragment.baseline() - CSSPixels::nearest_value_for(font_metrics.ascent) - half_leading); bottom_of_inline_box = (fragment.offset().y() + fragment.baseline() + CSSPixels::nearest_value_for(font_metrics.descent) + half_leading); @@ -312,7 +312,7 @@ void LineBuilder::update_last_line() if (length_percentage->is_length()) bottom_of_inline_box += length_percentage->length().to_px(fragment.layout_node()); else if (length_percentage->is_percentage()) - bottom_of_inline_box += m_context.containing_block().line_height().scaled(length_percentage->percentage().as_fraction()); + bottom_of_inline_box += m_context.containing_block().computed_values().line_height().scaled(length_percentage->percentage().as_fraction()); } } @@ -339,7 +339,7 @@ void LineBuilder::remove_last_line_if_empty() void LineBuilder::recalculate_available_space() { - auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().line_height()); + auto current_line_height = max(m_max_height_on_current_line, m_context.containing_block().computed_values().line_height()); auto available_at_top_of_line_box = m_context.available_space_for_line(m_current_y); auto available_at_bottom_of_line_box = m_context.available_space_for_line(m_current_y + current_line_height - 1); m_available_width_for_current_line = min(available_at_bottom_of_line_box, available_at_top_of_line_box); diff --git a/Userland/Libraries/LibWeb/Layout/Node.cpp b/Userland/Libraries/LibWeb/Layout/Node.cpp index eef5c86c0d..70b74bff9d 100644 --- a/Userland/Libraries/LibWeb/Layout/Node.cpp +++ b/Userland/Libraries/LibWeb/Layout/Node.cpp @@ -352,7 +352,7 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style) m_font_list = computed_style.computed_font_list(); computed_values.set_font_size(computed_style.property(CSS::PropertyID::FontSize)->as_length().length().to_px(*this)); computed_values.set_font_weight(round_to(computed_style.property(CSS::PropertyID::FontWeight)->as_number().number())); - m_line_height = computed_style.line_height(); + computed_values.set_line_height(computed_style.line_height()); computed_values.set_vertical_align(computed_style.vertical_align()); @@ -871,9 +871,9 @@ void NodeWithStyle::propagate_style_to_anonymous_wrappers() // the parent inherits style from *this* node, not the other way around. if (display().is_table_inside() && is(parent())) { auto& table_wrapper = *static_cast(parent()); + static_cast(static_cast(const_cast(table_wrapper.computed_values()))).inherit_from(m_computed_values); transfer_table_box_computed_values_to_wrapper_computed_values(table_wrapper.m_computed_values); table_wrapper.set_font_list(*m_font_list); - table_wrapper.set_line_height(m_line_height); } // Propagate style to all anonymous children (except table wrappers!) @@ -882,7 +882,6 @@ void NodeWithStyle::propagate_style_to_anonymous_wrappers() auto& child_computed_values = static_cast(static_cast(const_cast(child.computed_values()))); child_computed_values.inherit_from(m_computed_values); child.set_font_list(*m_font_list); - child.set_line_height(m_line_height); } }); } @@ -945,7 +944,6 @@ JS::NonnullGCPtr NodeWithStyle::create_anonymous_wrapper() const auto wrapper = heap().allocate_without_realm(const_cast(document()), nullptr, m_computed_values.clone_inherited_values()); static_cast(wrapper->m_computed_values).set_display(CSS::Display(CSS::DisplayOutside::Block, CSS::DisplayInside::Flow)); wrapper->m_font_list = m_font_list; - wrapper->m_line_height = m_line_height; return *wrapper; } diff --git a/Userland/Libraries/LibWeb/Layout/Node.h b/Userland/Libraries/LibWeb/Layout/Node.h index 2c1e36c378..47672245d7 100644 --- a/Userland/Libraries/LibWeb/Layout/Node.h +++ b/Userland/Libraries/LibWeb/Layout/Node.h @@ -219,8 +219,6 @@ public: Gfx::Font const& first_available_font() const; Gfx::FontCascadeList const& font_list() const { return *m_font_list; } - CSSPixels line_height() const { return m_line_height; } - void set_line_height(CSSPixels line_height) { m_line_height = line_height; } void set_font_list(Gfx::FontCascadeList const& font_list) { m_font_list = font_list; } Vector const& background_layers() const { return computed_values().background_layers(); } const CSS::AbstractImageStyleValue* list_style_image() const { return m_list_style_image; } diff --git a/Userland/Libraries/LibWeb/Layout/TreeBuilder.cpp b/Userland/Libraries/LibWeb/Layout/TreeBuilder.cpp index 00069c879e..c20bdcd693 100644 --- a/Userland/Libraries/LibWeb/Layout/TreeBuilder.cpp +++ b/Userland/Libraries/LibWeb/Layout/TreeBuilder.cpp @@ -420,7 +420,6 @@ ErrorOr TreeBuilder::create_layout_tree(DOM::Node& dom_node, TreeBuilder:: auto content_box_computed_values = parent.computed_values().clone_inherited_values(); auto content_box_wrapper = parent.heap().template allocate_without_realm(parent.document(), nullptr, move(content_box_computed_values)); content_box_wrapper->set_font_list(parent.font_list()); - content_box_wrapper->set_line_height(parent.line_height()); content_box_wrapper->set_children_are_inline(parent.children_are_inline()); Vector> sequence; @@ -621,7 +620,6 @@ static void wrap_in_anonymous(Vector>& sequence, Node* nearest_ wrapper->append_child(*child); } wrapper->set_children_are_inline(parent.children_are_inline()); - wrapper->set_line_height(parent.line_height()); wrapper->set_font_list(parent.font_list()); if (nearest_sibling) parent.insert_before(*wrapper, *nearest_sibling); @@ -710,7 +708,6 @@ Vector> TreeBuilder::generate_missing_parents(NodeWithStyle& roo parent.remove_child(*table_box); wrapper->append_child(*table_box); wrapper->set_font_list(parent.font_list()); - wrapper->set_line_height(parent.line_height()); if (nearest_sibling) parent.insert_before(*wrapper, *nearest_sibling); @@ -746,7 +743,6 @@ static void fixup_row(Box& row_box, TableGrid const& table_grid, size_t row_inde cell_computed_values.set_vertical_align(CSS::VerticalAlign::Middle); auto cell_box = row_box.heap().template allocate_without_realm(row_box.document(), nullptr, cell_computed_values); cell_box->set_font_list(row_box.font_list()); - cell_box->set_line_height(row_box.line_height()); row_box.append_child(cell_box); } }