InlineFormattingContext.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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& 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 = containing_block().margin_box_rect_in_ancestor_coordinate_space(parent().root());
  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 = floating_box.margin_box_as_relative_rect();
  42. if (rect.contains_vertically(y_in_root)) {
  43. info.left = rect.right() + 1;
  44. break;
  45. }
  46. }
  47. info.right = 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 = floating_box.margin_box_as_relative_rect();
  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&, 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 : 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. if (layout_mode != LayoutMode::Default) {
  81. containing_block().set_content_width(max_line_width);
  82. }
  83. containing_block().set_content_height(content_height);
  84. }
  85. void InlineFormattingContext::dimension_box_on_line(Box& box, LayoutMode layout_mode)
  86. {
  87. auto width_of_containing_block = CSS::Length::make_px(containing_block().content_width());
  88. auto& box_model = box.box_model();
  89. box_model.margin.left = box.computed_values().margin().left.resolved(box, width_of_containing_block).to_px(box);
  90. box_model.border.left = box.computed_values().border_left().width;
  91. box_model.padding.left = box.computed_values().padding().left.resolved(box, width_of_containing_block).to_px(box);
  92. box_model.margin.right = box.computed_values().margin().right.resolved(box, width_of_containing_block).to_px(box);
  93. box_model.border.right = box.computed_values().border_right().width;
  94. box_model.padding.right = box.computed_values().padding().right.resolved(box, width_of_containing_block).to_px(box);
  95. if (is<ReplacedBox>(box)) {
  96. auto& replaced = verify_cast<ReplacedBox>(box);
  97. replaced.set_content_width(compute_width_for_replaced_element(replaced));
  98. replaced.set_content_height(compute_height_for_replaced_element(replaced));
  99. return;
  100. }
  101. if (box.is_inline_block()) {
  102. auto& inline_block = const_cast<BlockContainer&>(verify_cast<BlockContainer>(box));
  103. auto& width_value = inline_block.computed_values().width();
  104. if (!width_value.has_value() || (width_value->is_length() && width_value->length().is_auto())) {
  105. auto result = calculate_shrink_to_fit_widths(inline_block);
  106. auto available_width = containing_block().content_width()
  107. - box_model.margin.left
  108. - box_model.border.left
  109. - box_model.padding.left
  110. - box_model.padding.right
  111. - box_model.border.right
  112. - box_model.margin.right;
  113. auto width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  114. inline_block.set_content_width(width);
  115. } else {
  116. auto container_width = CSS::Length::make_px(containing_block().content_width());
  117. inline_block.set_content_width(width_value->resolved(box, container_width).to_px(inline_block));
  118. }
  119. auto independent_formatting_context = layout_inside(inline_block, layout_mode);
  120. auto& height_value = inline_block.computed_values().height();
  121. if (!height_value.has_value() || (height_value->is_length() && height_value->length().is_auto())) {
  122. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  123. BlockFormattingContext::compute_height(inline_block);
  124. } else {
  125. auto container_height = CSS::Length::make_px(containing_block().content_height());
  126. inline_block.set_content_height(height_value->resolved(box, container_height).to_px(inline_block));
  127. }
  128. independent_formatting_context->parent_context_did_dimension_child_root_box();
  129. return;
  130. }
  131. // Non-replaced, non-inline-block, box on a line!?
  132. // I don't think we should be here. Dump the box tree so we can take a look at it.
  133. dbgln("FIXME: I've been asked to dimension a non-replaced, non-inline-block box on a line:");
  134. dump_tree(box);
  135. }
  136. void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
  137. {
  138. containing_block().line_boxes().clear();
  139. InlineLevelIterator iterator(*this, containing_block(), layout_mode);
  140. LineBuilder line_builder(*this);
  141. for (;;) {
  142. auto item_opt = iterator.next(line_builder.available_width_for_current_line());
  143. if (!item_opt.has_value())
  144. break;
  145. auto& item = item_opt.value();
  146. // Ignore collapsible whitespace chunks at the start of line, and if the last fragment already ends in whitespace.
  147. if (item.is_collapsible_whitespace && containing_block().line_boxes().last().is_empty_or_ends_in_whitespace())
  148. continue;
  149. switch (item.type) {
  150. case InlineLevelIterator::Item::Type::ForcedBreak:
  151. line_builder.break_line();
  152. break;
  153. case InlineLevelIterator::Item::Type::Element: {
  154. auto& box = verify_cast<Layout::Box>(*item.node);
  155. line_builder.break_if_needed(layout_mode, item.border_box_width(), item.should_force_break);
  156. line_builder.append_box(box, item.border_start + item.padding_start, item.padding_end + item.border_end);
  157. break;
  158. }
  159. case InlineLevelIterator::Item::Type::Text: {
  160. auto& text_node = verify_cast<Layout::TextNode>(*item.node);
  161. line_builder.break_if_needed(layout_mode, item.border_box_width(), item.should_force_break);
  162. line_builder.append_text_chunk(
  163. text_node,
  164. item.offset_in_node,
  165. item.length_in_node,
  166. item.border_start + item.padding_start,
  167. item.padding_end + item.border_end,
  168. item.width,
  169. text_node.font().glyph_height());
  170. break;
  171. }
  172. }
  173. }
  174. for (auto& line_box : containing_block().line_boxes()) {
  175. line_box.trim_trailing_whitespace();
  176. }
  177. line_builder.remove_last_line_if_empty();
  178. }
  179. }