InlineFormattingContext.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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(BlockContainer& containing_block, FormattingContext* parent)
  18. : FormattingContext(Type::Inline, containing_block, parent)
  19. {
  20. }
  21. InlineFormattingContext::~InlineFormattingContext()
  22. {
  23. }
  24. InlineFormattingContext::AvailableSpaceForLineInfo InlineFormattingContext::available_space_for_line(float y) const
  25. {
  26. if (!parent()->is_block_formatting_context())
  27. return { 0, context_box().width() };
  28. AvailableSpaceForLineInfo info;
  29. auto const& bfc = static_cast<BlockFormattingContext const&>(*parent());
  30. for (ssize_t i = bfc.left_floating_boxes().size() - 1; i >= 0; --i) {
  31. auto& floating_box = *bfc.left_floating_boxes().at(i);
  32. auto rect = floating_box.margin_box_as_relative_rect();
  33. if (rect.contains_vertically(y)) {
  34. info.left = rect.right() + 1;
  35. break;
  36. }
  37. }
  38. info.right = containing_block().width();
  39. for (ssize_t i = bfc.right_floating_boxes().size() - 1; i >= 0; --i) {
  40. auto& floating_box = *bfc.right_floating_boxes().at(i);
  41. auto rect = floating_box.margin_box_as_relative_rect();
  42. if (rect.contains_vertically(y)) {
  43. info.right = rect.left() - 1;
  44. break;
  45. }
  46. }
  47. return info;
  48. }
  49. void InlineFormattingContext::run(Box&, LayoutMode layout_mode)
  50. {
  51. VERIFY(containing_block().children_are_inline());
  52. generate_line_boxes(layout_mode);
  53. containing_block().for_each_child([&](auto& child) {
  54. VERIFY(child.is_inline());
  55. if (is<Box>(child) && child.is_absolutely_positioned()) {
  56. layout_absolutely_positioned_element(verify_cast<Box>(child));
  57. return;
  58. }
  59. });
  60. float min_line_height = containing_block().line_height();
  61. float max_line_width = 0;
  62. float content_height = 0;
  63. for (auto& line_box : containing_block().line_boxes()) {
  64. float max_height = min_line_height;
  65. for (auto& fragment : line_box.fragments()) {
  66. max_height = max(max_height, fragment.height());
  67. }
  68. max_line_width = max(max_line_width, line_box.width());
  69. content_height += max_height;
  70. }
  71. if (layout_mode != LayoutMode::Default) {
  72. containing_block().set_width(max_line_width);
  73. }
  74. containing_block().set_height(content_height);
  75. }
  76. float InlineFormattingContext::available_width_at_line(size_t line_index) const
  77. {
  78. // TODO: Remove this function, along with the old-style splitting functions.
  79. VERIFY_NOT_REACHED();
  80. auto info = available_space_for_line(line_index);
  81. return info.right - info.left;
  82. }
  83. void InlineFormattingContext::dimension_box_on_line(Box& box, LayoutMode layout_mode)
  84. {
  85. if (is<ReplacedBox>(box)) {
  86. auto& replaced = verify_cast<ReplacedBox>(box);
  87. replaced.set_width(compute_width_for_replaced_element(replaced));
  88. replaced.set_height(compute_height_for_replaced_element(replaced));
  89. return;
  90. }
  91. if (box.is_inline_block()) {
  92. auto& inline_block = const_cast<BlockContainer&>(verify_cast<BlockContainer>(box));
  93. if (inline_block.computed_values().width().is_length() && inline_block.computed_values().width().length().is_undefined_or_auto()) {
  94. auto result = calculate_shrink_to_fit_widths(inline_block);
  95. auto width_of_containing_block = CSS::Length::make_px(containing_block().width());
  96. auto margin_left = inline_block.computed_values().margin().left.resolved(width_of_containing_block).resolved_or_zero(inline_block).to_px(inline_block);
  97. auto border_left_width = inline_block.computed_values().border_left().width;
  98. auto padding_left = inline_block.computed_values().padding().left.resolved(width_of_containing_block).resolved_or_zero(inline_block).to_px(inline_block);
  99. auto margin_right = inline_block.computed_values().margin().right.resolved(width_of_containing_block).resolved_or_zero(inline_block).to_px(inline_block);
  100. auto border_right_width = inline_block.computed_values().border_right().width;
  101. auto padding_right = inline_block.computed_values().padding().right.resolved(width_of_containing_block).resolved_or_zero(inline_block).to_px(inline_block);
  102. auto available_width = containing_block().width()
  103. - margin_left
  104. - border_left_width
  105. - padding_left
  106. - padding_right
  107. - border_right_width
  108. - margin_right;
  109. auto width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  110. inline_block.set_width(width);
  111. } else {
  112. auto container_width = CSS::Length::make_px(containing_block().width());
  113. inline_block.set_width(inline_block.computed_values().width().resolved(container_width).resolved_or_zero(inline_block).to_px(inline_block));
  114. }
  115. (void)layout_inside(inline_block, layout_mode);
  116. if (inline_block.computed_values().height().is_length() && inline_block.computed_values().height().length().is_undefined_or_auto()) {
  117. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  118. } else {
  119. auto container_height = CSS::Length::make_px(containing_block().height());
  120. inline_block.set_height(inline_block.computed_values().height().resolved(container_height).resolved_or_zero(inline_block).to_px(inline_block));
  121. }
  122. return;
  123. }
  124. // Non-replaced, non-inline-block, box on a line!?
  125. // I don't think we should be here. Dump the box tree so we can take a look at it.
  126. dbgln("FIXME: I've been asked to dimension a non-replaced, non-inline-block box on a line:");
  127. dump_tree(box);
  128. }
  129. void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
  130. {
  131. containing_block().line_boxes().clear();
  132. InlineLevelIterator iterator(containing_block(), layout_mode);
  133. LineBuilder line_builder(*this);
  134. for (;;) {
  135. auto item_opt = iterator.next(line_builder.available_width_for_current_line());
  136. if (!item_opt.has_value())
  137. break;
  138. auto& item = item_opt.value();
  139. // Ignore collapsible whitespace chunks at the start of line, and if the last fragment already ends in whitespace.
  140. if (item.is_collapsible_whitespace && containing_block().line_boxes().last().is_empty_or_ends_in_whitespace())
  141. continue;
  142. switch (item.type) {
  143. case InlineLevelIterator::Item::Type::ForcedBreak:
  144. line_builder.break_line();
  145. break;
  146. case InlineLevelIterator::Item::Type::Element: {
  147. auto& box = verify_cast<Layout::Box>(*item.node);
  148. dimension_box_on_line(box, layout_mode);
  149. line_builder.break_if_needed(layout_mode, box.width(), item.should_force_break);
  150. line_builder.append_box(box);
  151. break;
  152. }
  153. case InlineLevelIterator::Item::Type::Text: {
  154. auto& text_node = verify_cast<Layout::TextNode>(*item.node);
  155. line_builder.break_if_needed(layout_mode, item.width, item.should_force_break);
  156. line_builder.append_text_chunk(
  157. text_node,
  158. item.offset_in_node,
  159. item.length_in_node,
  160. item.width,
  161. text_node.font().glyph_height());
  162. break;
  163. }
  164. }
  165. }
  166. for (auto& line_box : containing_block().line_boxes()) {
  167. line_box.trim_trailing_whitespace();
  168. }
  169. line_builder.remove_last_line_if_empty();
  170. }
  171. }