InlineFormattingContext.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Length.h>
  7. #include <LibWeb/DOM/Node.h>
  8. #include <LibWeb/Dump.h>
  9. #include <LibWeb/Layout/BlockContainer.h>
  10. #include <LibWeb/Layout/BlockFormattingContext.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Layout/InlineFormattingContext.h>
  13. #include <LibWeb/Layout/InlineLevelIterator.h>
  14. #include <LibWeb/Layout/LineBuilder.h>
  15. #include <LibWeb/Layout/ReplacedBox.h>
  16. namespace Web::Layout {
  17. InlineFormattingContext::InlineFormattingContext(FormattingState& state, BlockContainer const& containing_block, BlockFormattingContext& parent)
  18. : FormattingContext(Type::Inline, state, containing_block, &parent)
  19. {
  20. }
  21. InlineFormattingContext::~InlineFormattingContext()
  22. {
  23. }
  24. BlockFormattingContext& InlineFormattingContext::parent()
  25. {
  26. return static_cast<BlockFormattingContext&>(*FormattingContext::parent());
  27. }
  28. BlockFormattingContext const& InlineFormattingContext::parent() const
  29. {
  30. return static_cast<BlockFormattingContext const&>(*FormattingContext::parent());
  31. }
  32. InlineFormattingContext::AvailableSpaceForLineInfo InlineFormattingContext::available_space_for_line(float y) const
  33. {
  34. // NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
  35. auto box_in_root_rect = margin_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state);
  36. float y_in_root = box_in_root_rect.y() + y;
  37. AvailableSpaceForLineInfo info;
  38. auto const& bfc = parent();
  39. for (ssize_t i = bfc.left_side_floats().boxes.size() - 1; i >= 0; --i) {
  40. auto const& floating_box = bfc.left_side_floats().boxes.at(i);
  41. auto rect = margin_box_rect(floating_box, m_state);
  42. if (rect.contains_vertically(y_in_root)) {
  43. info.left = rect.right() + 1;
  44. break;
  45. }
  46. }
  47. info.right = m_state.get(containing_block()).content_width;
  48. for (ssize_t i = bfc.right_side_floats().boxes.size() - 1; i >= 0; --i) {
  49. auto const& floating_box = bfc.right_side_floats().boxes.at(i);
  50. auto rect = margin_box_rect(floating_box, m_state);
  51. if (rect.contains_vertically(y_in_root)) {
  52. info.right = rect.left() - 1;
  53. break;
  54. }
  55. }
  56. return info;
  57. }
  58. void InlineFormattingContext::run(Box const&, LayoutMode layout_mode)
  59. {
  60. VERIFY(containing_block().children_are_inline());
  61. generate_line_boxes(layout_mode);
  62. containing_block().for_each_child([&](auto& child) {
  63. VERIFY(child.is_inline());
  64. if (is<Box>(child) && child.is_absolutely_positioned()) {
  65. parent().add_absolutely_positioned_box(static_cast<Box&>(child));
  66. return;
  67. }
  68. });
  69. float min_line_height = containing_block().line_height();
  70. float max_line_width = 0;
  71. float content_height = 0;
  72. for (auto& line_box : m_state.get(containing_block()).line_boxes) {
  73. float max_height = min_line_height;
  74. for (auto& fragment : line_box.fragments()) {
  75. max_height = max(max_height, fragment.height());
  76. }
  77. max_line_width = max(max_line_width, line_box.width());
  78. content_height += max_height;
  79. }
  80. auto& containing_block_state = m_state.ensure(containing_block());
  81. if (layout_mode != LayoutMode::Default) {
  82. containing_block_state.content_width = max_line_width;
  83. }
  84. containing_block_state.content_height = content_height;
  85. }
  86. void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode layout_mode)
  87. {
  88. auto width_of_containing_block = CSS::Length::make_px(m_state.get(containing_block()).content_width);
  89. auto& box_state = m_state.ensure(box);
  90. auto const& computed_values = box.computed_values();
  91. box_state.margin_left = computed_values.margin().left.resolved(box, width_of_containing_block).to_px(box);
  92. box_state.border_left = computed_values.border_left().width;
  93. box_state.padding_left = computed_values.padding().left.resolved(box, width_of_containing_block).to_px(box);
  94. box_state.margin_right = computed_values.margin().right.resolved(box, width_of_containing_block).to_px(box);
  95. box_state.border_right = computed_values.border_right().width;
  96. box_state.padding_right = computed_values.padding().right.resolved(box, width_of_containing_block).to_px(box);
  97. if (is<ReplacedBox>(box)) {
  98. auto& replaced = verify_cast<ReplacedBox>(box);
  99. box_state.content_width = compute_width_for_replaced_element(m_state, replaced);
  100. box_state.content_height = compute_height_for_replaced_element(m_state, replaced);
  101. return;
  102. }
  103. if (box.is_inline_block()) {
  104. auto const& inline_block = verify_cast<BlockContainer>(box);
  105. auto const& containing_block_state = m_state.get(containing_block());
  106. auto& width_value = inline_block.computed_values().width();
  107. if (!width_value.has_value() || (width_value->is_length() && width_value->length().is_auto())) {
  108. auto result = calculate_shrink_to_fit_widths(inline_block);
  109. auto available_width = containing_block_state.content_width
  110. - box_state.margin_left
  111. - box_state.border_left
  112. - box_state.padding_left
  113. - box_state.padding_right
  114. - box_state.border_right
  115. - box_state.margin_right;
  116. auto width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  117. box_state.content_width = width;
  118. } else {
  119. auto container_width = CSS::Length::make_px(containing_block_state.content_width);
  120. box_state.content_width = width_value->resolved(box, container_width).to_px(inline_block);
  121. }
  122. auto independent_formatting_context = layout_inside(inline_block, layout_mode);
  123. auto& height_value = inline_block.computed_values().height();
  124. if (!height_value.has_value() || (height_value->is_length() && height_value->length().is_auto())) {
  125. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  126. BlockFormattingContext::compute_height(inline_block, m_state);
  127. } else {
  128. auto container_height = CSS::Length::make_px(containing_block_state.content_height);
  129. box_state.content_height = height_value->resolved(box, container_height).to_px(inline_block);
  130. }
  131. independent_formatting_context->parent_context_did_dimension_child_root_box();
  132. return;
  133. }
  134. // Non-replaced, non-inline-block, box on a line!?
  135. // I don't think we should be here. Dump the box tree so we can take a look at it.
  136. dbgln("FIXME: I've been asked to dimension a non-replaced, non-inline-block box on a line:");
  137. dump_tree(box);
  138. }
  139. void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
  140. {
  141. auto& containing_block_state = m_state.ensure(containing_block());
  142. auto& line_boxes = containing_block_state.line_boxes;
  143. line_boxes.clear_with_capacity();
  144. InlineLevelIterator iterator(*this, m_state, containing_block(), layout_mode);
  145. LineBuilder line_builder(*this, m_state);
  146. for (;;) {
  147. auto item_opt = iterator.next(line_builder.available_width_for_current_line());
  148. if (!item_opt.has_value())
  149. break;
  150. auto& item = item_opt.value();
  151. // Ignore collapsible whitespace chunks at the start of line, and if the last fragment already ends in whitespace.
  152. if (item.is_collapsible_whitespace && line_boxes.last().is_empty_or_ends_in_whitespace())
  153. continue;
  154. switch (item.type) {
  155. case InlineLevelIterator::Item::Type::ForcedBreak:
  156. line_builder.break_line();
  157. break;
  158. case InlineLevelIterator::Item::Type::Element: {
  159. auto& box = verify_cast<Layout::Box>(*item.node);
  160. line_builder.break_if_needed(layout_mode, item.border_box_width(), item.should_force_break);
  161. line_builder.append_box(box, item.border_start + item.padding_start, item.padding_end + item.border_end);
  162. break;
  163. }
  164. case InlineLevelIterator::Item::Type::Text: {
  165. auto& text_node = verify_cast<Layout::TextNode>(*item.node);
  166. line_builder.break_if_needed(layout_mode, item.border_box_width(), item.should_force_break);
  167. line_builder.append_text_chunk(
  168. text_node,
  169. item.offset_in_node,
  170. item.length_in_node,
  171. item.border_start + item.padding_start,
  172. item.padding_end + item.border_end,
  173. item.width,
  174. text_node.font().glyph_height());
  175. break;
  176. }
  177. }
  178. }
  179. for (auto& line_box : line_boxes) {
  180. line_box.trim_trailing_whitespace();
  181. }
  182. line_builder.remove_last_line_if_empty();
  183. }
  184. }