InlineFormattingContext.cpp 9.3 KB

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