/* * Copyright (c) 2020-2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include namespace Web::Layout { InlineFormattingContext::InlineFormattingContext(BlockContainer& containing_block, FormattingContext* parent) : FormattingContext(Type::Inline, containing_block, parent) { } InlineFormattingContext::~InlineFormattingContext() { } InlineFormattingContext::AvailableSpaceForLineInfo InlineFormattingContext::available_space_for_line(size_t line_index) const { if (!parent()->is_block_formatting_context()) return { 0, context_box().width() }; 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 = containing_block().line_height(); float y = (line_index * line_height); auto const& bfc = static_cast(*parent()); for (ssize_t i = bfc.left_floating_boxes().size() - 1; i >= 0; --i) { auto& floating_box = *bfc.left_floating_boxes().at(i); auto rect = floating_box.margin_box_as_relative_rect(); if (rect.contains_vertically(y)) { info.left = rect.right() + 1; break; } } info.right = containing_block().width(); for (ssize_t i = bfc.right_floating_boxes().size() - 1; i >= 0; --i) { auto& floating_box = *bfc.right_floating_boxes().at(i); auto rect = floating_box.margin_box_as_relative_rect(); if (rect.contains_vertically(y)) { info.right = rect.left() - 1; break; } } return info; } float InlineFormattingContext::available_width_at_line(size_t line_index) const { auto info = available_space_for_line(line_index); return info.right - info.left; } void InlineFormattingContext::run(Box&, LayoutMode layout_mode) { VERIFY(containing_block().children_are_inline()); generate_line_boxes(layout_mode); containing_block().for_each_child([&](auto& child) { VERIFY(child.is_inline()); if (is(child) && child.is_absolutely_positioned()) { layout_absolutely_positioned_element(verify_cast(child)); return; } }); auto text_align = containing_block().computed_values().text_align(); float min_line_height = containing_block().line_height(); float content_height = 0; float max_linebox_width = 0; for (size_t line_index = 0; line_index < containing_block().line_boxes().size(); ++line_index) { auto& line_box = containing_block().line_boxes()[line_index]; float max_height = min_line_height; for (auto& fragment : line_box.fragments()) { max_height = max(max_height, fragment.height()); } float x_offset = available_space_for_line(line_index).left; float excess_horizontal_space = (float)containing_block().width() - line_box.width(); switch (text_align) { case CSS::TextAlign::Center: case CSS::TextAlign::LibwebCenter: x_offset += excess_horizontal_space / 2; break; case CSS::TextAlign::Right: x_offset += excess_horizontal_space; break; case CSS::TextAlign::Left: case CSS::TextAlign::Justify: default: break; } float excess_horizontal_space_including_whitespace = excess_horizontal_space; int whitespace_count = 0; if (text_align == CSS::TextAlign::Justify) { for (auto& fragment : line_box.fragments()) { if (fragment.is_justifiable_whitespace()) { ++whitespace_count; excess_horizontal_space_including_whitespace += fragment.width(); } } } float justified_space_width = whitespace_count ? (excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0; for (size_t i = 0; i < line_box.fragments().size(); ++i) { auto& fragment = line_box.fragments()[i]; if (fragment.type() == LineBoxFragment::Type::Leading || fragment.type() == LineBoxFragment::Type::Trailing) { fragment.set_height(max_height); } // Vertically align everyone's bottom to the line. // FIXME: Support other kinds of vertical alignment. fragment.set_offset({ roundf(x_offset + fragment.offset().x()), content_height + (max_height - fragment.height()) }); if (text_align == CSS::TextAlign::Justify && fragment.is_justifiable_whitespace() && fragment.width() != justified_space_width) { float diff = justified_space_width - fragment.width(); fragment.set_width(justified_space_width); // Shift subsequent sibling fragments to the right to adjust for change in width. for (size_t j = i + 1; j < line_box.fragments().size(); ++j) { auto offset = line_box.fragments()[j].offset(); offset.translate_by(diff, 0); line_box.fragments()[j].set_offset(offset); } } } if (!line_box.fragments().is_empty()) { float left_edge = line_box.fragments().first().offset().x(); float right_edge = line_box.fragments().last().offset().x() + line_box.fragments().last().width(); float final_line_box_width = right_edge - left_edge; line_box.m_width = final_line_box_width; max_linebox_width = max(max_linebox_width, final_line_box_width); } content_height += max_height; } if (layout_mode != LayoutMode::Default) { containing_block().set_width(max_linebox_width); } containing_block().set_height(content_height); } void InlineFormattingContext::dimension_box_on_line(Box& box, LayoutMode layout_mode) { if (is(box)) { auto& replaced = verify_cast(box); replaced.set_width(compute_width_for_replaced_element(replaced)); replaced.set_height(compute_height_for_replaced_element(replaced)); return; } if (box.is_inline_block()) { auto& inline_block = const_cast(verify_cast(box)); if (inline_block.computed_values().width().is_length() && inline_block.computed_values().width().length().is_undefined_or_auto()) { auto result = calculate_shrink_to_fit_widths(inline_block); auto width_of_containing_block = CSS::Length::make_px(containing_block().width()); auto margin_left = inline_block.computed_values().margin().left.resolved(width_of_containing_block).resolved_or_zero(inline_block).to_px(inline_block); auto border_left_width = inline_block.computed_values().border_left().width; auto padding_left = inline_block.computed_values().padding().left.resolved(width_of_containing_block).resolved_or_zero(inline_block).to_px(inline_block); auto margin_right = inline_block.computed_values().margin().right.resolved(width_of_containing_block).resolved_or_zero(inline_block).to_px(inline_block); auto border_right_width = inline_block.computed_values().border_right().width; auto padding_right = inline_block.computed_values().padding().right.resolved(width_of_containing_block).resolved_or_zero(inline_block).to_px(inline_block); auto available_width = containing_block().width() - margin_left - border_left_width - padding_left - padding_right - border_right_width - margin_right; auto width = min(max(result.preferred_minimum_width, available_width), result.preferred_width); inline_block.set_width(width); } else { auto container_width = CSS::Length::make_px(containing_block().width()); inline_block.set_width(inline_block.computed_values().width().resolved(container_width).resolved_or_zero(inline_block).to_px(inline_block)); } (void)layout_inside(inline_block, layout_mode); if (inline_block.computed_values().height().is_length() && inline_block.computed_values().height().length().is_undefined_or_auto()) { // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7. } else { auto container_height = CSS::Length::make_px(containing_block().height()); inline_block.set_height(inline_block.computed_values().height().resolved(container_height).resolved_or_zero(inline_block).to_px(inline_block)); } return; } // Non-replaced, non-inline-block, box on a line!? // I don't think we should be here. Dump the box tree so we can take a look at it. dbgln("FIXME: I've been asked to dimension a non-replaced, non-inline-block box on a line:"); dump_tree(box); } void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode) { auto& line_boxes = containing_block().line_boxes(); line_boxes.clear(); containing_block().ensure_last_line_box(); InlineLevelIterator iterator(containing_block(), layout_mode); float available_width = containing_block().width(); for (;;) { auto item_opt = iterator.next(available_width); if (!item_opt.has_value()) break; auto& item = item_opt.value(); float current_line_width = line_boxes.last().width(); bool should_break_here = layout_mode == LayoutMode::AllPossibleLineBreaks || (current_line_width + item.width) > available_width; if (should_break_here) line_boxes.append(LineBox()); if (item.type == InlineLevelIterator::Item::Type::ForcedBreak) { line_boxes.append(LineBox()); continue; } if (item.type == InlineLevelIterator::Item::Type::Element) { auto& box = verify_cast(*item.node); dimension_box_on_line(box, LayoutMode::Default); line_boxes.last().add_fragment(box, 0, 0, box.width(), box.height()); continue; } if (item.type == InlineLevelIterator::Item::Type::Text) { auto& text_node = verify_cast(*item.node); line_boxes.last().add_fragment( text_node, item.offset_in_node, item.length_in_node, item.width, text_node.font().glyph_height()); continue; } } for (auto& line_box : containing_block().line_boxes()) { line_box.trim_trailing_whitespace(); } // If there's an empty line box at the bottom, just remove it instead of giving it height. if (!containing_block().line_boxes().is_empty() && containing_block().line_boxes().last().fragments().is_empty()) containing_block().line_boxes().take_last(); } }