InlineFormattingContext.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2020, 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/BlockBox.h>
  10. #include <LibWeb/Layout/BlockFormattingContext.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Layout/InlineFormattingContext.h>
  13. #include <LibWeb/Layout/ReplacedBox.h>
  14. namespace Web::Layout {
  15. InlineFormattingContext::InlineFormattingContext(Box& containing_block, FormattingContext* parent)
  16. : FormattingContext(containing_block, parent)
  17. {
  18. }
  19. InlineFormattingContext::~InlineFormattingContext()
  20. {
  21. }
  22. struct AvailableSpaceForLineInfo {
  23. float left { 0 };
  24. float right { 0 };
  25. };
  26. static AvailableSpaceForLineInfo available_space_for_line(const InlineFormattingContext& context, size_t line_index)
  27. {
  28. AvailableSpaceForLineInfo info;
  29. // FIXME: This is a total hack guess since we don't actually know the final y position of lines here!
  30. float line_height = context.containing_block().line_height();
  31. float y = (line_index * line_height);
  32. auto& bfc = static_cast<const BlockFormattingContext&>(*context.parent());
  33. for (ssize_t i = bfc.left_floating_boxes().size() - 1; i >= 0; --i) {
  34. auto& floating_box = *bfc.left_floating_boxes().at(i);
  35. auto rect = floating_box.margin_box_as_relative_rect();
  36. if (rect.contains_vertically(y)) {
  37. info.left = rect.right() + 1;
  38. break;
  39. }
  40. }
  41. info.right = context.containing_block().width();
  42. for (ssize_t i = bfc.right_floating_boxes().size() - 1; i >= 0; --i) {
  43. auto& floating_box = *bfc.right_floating_boxes().at(i);
  44. auto rect = floating_box.margin_box_as_relative_rect();
  45. if (rect.contains_vertically(y)) {
  46. info.right = rect.left() - 1;
  47. break;
  48. }
  49. }
  50. return info;
  51. }
  52. float InlineFormattingContext::available_width_at_line(size_t line_index) const
  53. {
  54. auto info = available_space_for_line(*this, line_index);
  55. return info.right - info.left;
  56. }
  57. void InlineFormattingContext::run(Box&, LayoutMode layout_mode)
  58. {
  59. VERIFY(containing_block().children_are_inline());
  60. containing_block().line_boxes().clear();
  61. containing_block().for_each_child([&](auto& child) {
  62. VERIFY(child.is_inline());
  63. if (is<Box>(child) && child.is_absolutely_positioned()) {
  64. layout_absolutely_positioned_element(verify_cast<Box>(child));
  65. return;
  66. }
  67. child.split_into_lines(*this, layout_mode);
  68. });
  69. for (auto& line_box : containing_block().line_boxes()) {
  70. line_box.trim_trailing_whitespace();
  71. }
  72. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  73. if (!containing_block().line_boxes().is_empty() && containing_block().line_boxes().last().fragments().is_empty())
  74. containing_block().line_boxes().take_last();
  75. auto text_align = containing_block().computed_values().text_align();
  76. float min_line_height = containing_block().line_height();
  77. float content_height = 0;
  78. float max_linebox_width = 0;
  79. for (size_t line_index = 0; line_index < containing_block().line_boxes().size(); ++line_index) {
  80. auto& line_box = containing_block().line_boxes()[line_index];
  81. float max_height = min_line_height;
  82. for (auto& fragment : line_box.fragments()) {
  83. max_height = max(max_height, fragment.height());
  84. }
  85. float x_offset = available_space_for_line(*this, line_index).left;
  86. float excess_horizontal_space = (float)containing_block().width() - line_box.width();
  87. switch (text_align) {
  88. case CSS::TextAlign::Center:
  89. case CSS::TextAlign::LibwebCenter:
  90. x_offset += excess_horizontal_space / 2;
  91. break;
  92. case CSS::TextAlign::Right:
  93. x_offset += excess_horizontal_space;
  94. break;
  95. case CSS::TextAlign::Left:
  96. case CSS::TextAlign::Justify:
  97. default:
  98. break;
  99. }
  100. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  101. int whitespace_count = 0;
  102. if (text_align == CSS::TextAlign::Justify) {
  103. for (auto& fragment : line_box.fragments()) {
  104. if (fragment.is_justifiable_whitespace()) {
  105. ++whitespace_count;
  106. excess_horizontal_space_including_whitespace += fragment.width();
  107. }
  108. }
  109. }
  110. float justified_space_width = whitespace_count ? (excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0;
  111. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  112. auto& fragment = line_box.fragments()[i];
  113. if (fragment.type() == LineBoxFragment::Type::Leading || fragment.type() == LineBoxFragment::Type::Trailing) {
  114. fragment.set_height(max_height);
  115. } else {
  116. fragment.set_height(max(min_line_height, fragment.height()));
  117. }
  118. // Vertically align everyone's bottom to the line.
  119. // FIXME: Support other kinds of vertical alignment.
  120. fragment.set_offset({ roundf(x_offset + fragment.offset().x()), content_height + (max_height - fragment.height()) });
  121. if (text_align == CSS::TextAlign::Justify
  122. && fragment.is_justifiable_whitespace()
  123. && fragment.width() != justified_space_width) {
  124. float diff = justified_space_width - fragment.width();
  125. fragment.set_width(justified_space_width);
  126. // Shift subsequent sibling fragments to the right to adjust for change in width.
  127. for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
  128. auto offset = line_box.fragments()[j].offset();
  129. offset.translate_by(diff, 0);
  130. line_box.fragments()[j].set_offset(offset);
  131. }
  132. }
  133. }
  134. if (!line_box.fragments().is_empty()) {
  135. float left_edge = line_box.fragments().first().offset().x();
  136. float right_edge = line_box.fragments().last().offset().x() + line_box.fragments().last().width();
  137. float final_line_box_width = right_edge - left_edge;
  138. line_box.m_width = final_line_box_width;
  139. max_linebox_width = max(max_linebox_width, final_line_box_width);
  140. }
  141. content_height += max_height;
  142. }
  143. if (layout_mode != LayoutMode::Default) {
  144. containing_block().set_width(max_linebox_width);
  145. }
  146. containing_block().set_height(content_height);
  147. }
  148. void InlineFormattingContext::dimension_box_on_line(Box& box, LayoutMode layout_mode)
  149. {
  150. if (is<ReplacedBox>(box)) {
  151. auto& replaced = verify_cast<ReplacedBox>(box);
  152. replaced.set_width(compute_width_for_replaced_element(replaced));
  153. replaced.set_height(compute_height_for_replaced_element(replaced));
  154. return;
  155. }
  156. if (box.is_inline_block()) {
  157. auto& inline_block = const_cast<BlockBox&>(verify_cast<BlockBox>(box));
  158. if (inline_block.computed_values().width().is_undefined_or_auto()) {
  159. auto result = calculate_shrink_to_fit_widths(inline_block);
  160. auto margin_left = inline_block.computed_values().margin().left.resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block);
  161. auto border_left_width = inline_block.computed_values().border_left().width;
  162. auto padding_left = inline_block.computed_values().padding().left.resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block);
  163. auto margin_right = inline_block.computed_values().margin().right.resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block);
  164. auto border_right_width = inline_block.computed_values().border_right().width;
  165. auto padding_right = inline_block.computed_values().padding().right.resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block);
  166. auto available_width = containing_block().width()
  167. - margin_left
  168. - border_left_width
  169. - padding_left
  170. - padding_right
  171. - border_right_width
  172. - margin_right;
  173. auto width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  174. inline_block.set_width(width);
  175. } else {
  176. inline_block.set_width(inline_block.computed_values().width().resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block));
  177. }
  178. layout_inside(inline_block, layout_mode);
  179. if (inline_block.computed_values().height().is_undefined_or_auto()) {
  180. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  181. } else {
  182. inline_block.set_height(inline_block.computed_values().height().resolved_or_zero(inline_block, containing_block().height()).to_px(inline_block));
  183. }
  184. return;
  185. }
  186. // Non-replaced, non-inline-block, box on a line!?
  187. // I don't think we should be here. Dump the box tree so we can take a look at it.
  188. dbgln("FIXME: I've been asked to dimension a non-replaced, non-inline-block box on a line:");
  189. dump_tree(box);
  190. }
  191. }