InlineFormattingContext.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include <LibWeb/Layout/SVGSVGBox.h>
  17. namespace Web::Layout {
  18. constexpr float text_justification_threshold = 0.1;
  19. InlineFormattingContext::InlineFormattingContext(LayoutState& state, BlockContainer const& containing_block, BlockFormattingContext& parent)
  20. : FormattingContext(Type::Inline, state, containing_block, &parent)
  21. , m_containing_block_state(state.get(containing_block))
  22. {
  23. }
  24. InlineFormattingContext::~InlineFormattingContext() = default;
  25. BlockFormattingContext& InlineFormattingContext::parent()
  26. {
  27. return static_cast<BlockFormattingContext&>(*FormattingContext::parent());
  28. }
  29. BlockFormattingContext const& InlineFormattingContext::parent() const
  30. {
  31. return static_cast<BlockFormattingContext const&>(*FormattingContext::parent());
  32. }
  33. float InlineFormattingContext::leftmost_x_offset_at(float y) const
  34. {
  35. // NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
  36. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state);
  37. float y_in_root = box_in_root_rect.y() + y;
  38. auto space = parent().space_used_by_floats(y_in_root);
  39. return space.left;
  40. }
  41. float InlineFormattingContext::available_space_for_line(float y) const
  42. {
  43. // NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
  44. auto& root_block = parent().root();
  45. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), root_block, m_state);
  46. float y_in_root = box_in_root_rect.y() + y;
  47. auto space = parent().space_used_by_floats(y_in_root);
  48. space.left = space.left;
  49. space.right = min(m_available_space->width.to_px() - space.right, m_available_space->width.to_px());
  50. return space.right - space.left;
  51. }
  52. float InlineFormattingContext::automatic_content_width() const
  53. {
  54. return m_automatic_content_width;
  55. }
  56. float InlineFormattingContext::automatic_content_height() const
  57. {
  58. return m_automatic_content_height;
  59. }
  60. void InlineFormattingContext::run(Box const&, LayoutMode layout_mode, AvailableSpace const& available_space)
  61. {
  62. VERIFY(containing_block().children_are_inline());
  63. m_available_space = available_space;
  64. generate_line_boxes(layout_mode);
  65. float max_line_width = 0;
  66. float content_height = 0;
  67. for (auto& line_box : m_containing_block_state.line_boxes) {
  68. max_line_width = max(max_line_width, line_box.width());
  69. content_height += line_box.height();
  70. }
  71. m_automatic_content_width = max_line_width;
  72. m_automatic_content_height = content_height;
  73. }
  74. void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode layout_mode)
  75. {
  76. auto width_of_containing_block = CSS::Length::make_px(m_available_space->width.to_px());
  77. auto& box_state = m_state.get_mutable(box);
  78. auto const& computed_values = box.computed_values();
  79. box_state.margin_left = computed_values.margin().left().resolved(box, width_of_containing_block).to_px(box);
  80. box_state.border_left = computed_values.border_left().width;
  81. box_state.padding_left = computed_values.padding().left().resolved(box, width_of_containing_block).to_px(box);
  82. box_state.margin_right = computed_values.margin().right().resolved(box, width_of_containing_block).to_px(box);
  83. box_state.border_right = computed_values.border_right().width;
  84. box_state.padding_right = computed_values.padding().right().resolved(box, width_of_containing_block).to_px(box);
  85. box_state.margin_top = computed_values.margin().top().resolved(box, width_of_containing_block).to_px(box);
  86. box_state.border_top = computed_values.border_top().width;
  87. box_state.padding_top = computed_values.padding().top().resolved(box, width_of_containing_block).to_px(box);
  88. box_state.padding_bottom = computed_values.padding().bottom().resolved(box, width_of_containing_block).to_px(box);
  89. box_state.border_bottom = computed_values.border_bottom().width;
  90. box_state.margin_bottom = computed_values.margin().bottom().resolved(box, width_of_containing_block).to_px(box);
  91. if (is<ReplacedBox>(box)) {
  92. auto& replaced = verify_cast<ReplacedBox>(box);
  93. if (is<SVGSVGBox>(box))
  94. (void)layout_inside(replaced, layout_mode, *m_available_space);
  95. box_state.set_content_width(compute_width_for_replaced_element(m_state, replaced, *m_available_space));
  96. box_state.set_content_height(compute_height_for_replaced_element(m_state, replaced, *m_available_space));
  97. return;
  98. }
  99. // Any box that has simple flow inside should have generated line box fragments already.
  100. VERIFY(!box.display().is_flow_inside());
  101. auto const& width_value = box.computed_values().width();
  102. float unconstrained_width = 0;
  103. if (should_treat_width_as_auto(box, *m_available_space)) {
  104. auto result = calculate_shrink_to_fit_widths(box);
  105. auto available_width = m_available_space->width.to_px()
  106. - box_state.margin_left
  107. - box_state.border_left
  108. - box_state.padding_left
  109. - box_state.padding_right
  110. - box_state.border_right
  111. - box_state.margin_right;
  112. unconstrained_width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  113. } else {
  114. if (width_value.contains_percentage() && !m_available_space->width.is_definite()) {
  115. // NOTE: We can't resolve percentages yet. We'll have to wait until after inner layout.
  116. } else {
  117. auto container_width = CSS::Length::make_px(m_available_space->width.to_px());
  118. unconstrained_width = width_value.resolved(box, container_width).to_px(box);
  119. }
  120. }
  121. float width = unconstrained_width;
  122. auto computed_max_width = box.computed_values().max_width();
  123. if (!computed_max_width.is_none()) {
  124. auto max_width = computed_max_width.resolved(box, width_of_containing_block).to_px(box);
  125. width = min(width, max_width);
  126. }
  127. auto computed_min_width = box.computed_values().min_width();
  128. if (!computed_min_width.is_auto()) {
  129. auto min_width = computed_min_width.resolved(box, width_of_containing_block).to_px(box);
  130. width = max(width, min_width);
  131. }
  132. box_state.set_content_width(width);
  133. auto independent_formatting_context = layout_inside(box, layout_mode, box_state.available_inner_space_or_constraints_from(*m_available_space));
  134. auto const& height_value = box.computed_values().height();
  135. if (height_value.is_auto()) {
  136. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  137. parent().compute_height(box, AvailableSpace(AvailableSize::make_indefinite(), AvailableSize::make_indefinite()));
  138. } else {
  139. auto container_height = CSS::Length::make_px(m_containing_block_state.content_height());
  140. box_state.set_content_height(height_value.resolved(box, container_height).to_px(box));
  141. }
  142. if (independent_formatting_context)
  143. independent_formatting_context->parent_context_did_dimension_child_root_box();
  144. }
  145. void InlineFormattingContext::apply_justification_to_fragments(CSS::TextJustify text_justify, LineBox& line_box, bool is_last_line)
  146. {
  147. switch (text_justify) {
  148. case CSS::TextJustify::None:
  149. return;
  150. // FIXME: These two cases currently fall back to auto, handle them as well.
  151. case CSS::TextJustify::InterCharacter:
  152. case CSS::TextJustify::InterWord:
  153. case CSS::TextJustify::Auto:
  154. break;
  155. }
  156. float excess_horizontal_space = m_available_space->width.to_px() - line_box.width();
  157. // Only justify the text if the excess horizontal space is less than or
  158. // equal to 10%, or if we are not looking at the last line box.
  159. if (is_last_line && excess_horizontal_space / m_available_space->width.to_px() > text_justification_threshold)
  160. return;
  161. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  162. size_t whitespace_count = 0;
  163. for (auto& fragment : line_box.fragments()) {
  164. if (fragment.is_justifiable_whitespace()) {
  165. ++whitespace_count;
  166. excess_horizontal_space_including_whitespace += fragment.width().value();
  167. }
  168. }
  169. float justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / static_cast<float>(whitespace_count)) : 0;
  170. // This is the amount that each fragment will be offset by. If a whitespace
  171. // fragment is shorter than the justified space width, it increases to push
  172. // subsequent fragments, and decreases to pull them back otherwise.
  173. float running_diff = 0;
  174. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  175. auto& fragment = line_box.fragments()[i];
  176. auto offset = fragment.offset();
  177. offset.translate_by(running_diff, 0);
  178. fragment.set_offset(offset);
  179. if (fragment.is_justifiable_whitespace()
  180. && fragment.width() != justified_space_width) {
  181. running_diff += justified_space_width - fragment.width().value();
  182. fragment.set_width(justified_space_width);
  183. }
  184. }
  185. }
  186. void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
  187. {
  188. auto& containing_block_state = m_state.get_mutable(containing_block());
  189. auto& line_boxes = containing_block_state.line_boxes;
  190. line_boxes.clear_with_capacity();
  191. InlineLevelIterator iterator(*this, m_state, containing_block(), layout_mode);
  192. LineBuilder line_builder(*this, m_state);
  193. for (;;) {
  194. auto item_opt = iterator.next(line_builder.available_width_for_current_line());
  195. if (!item_opt.has_value())
  196. break;
  197. auto& item = item_opt.value();
  198. // Ignore collapsible whitespace chunks at the start of line, and if the last fragment already ends in whitespace.
  199. if (item.is_collapsible_whitespace && (line_boxes.is_empty() || line_boxes.last().is_empty_or_ends_in_whitespace()))
  200. continue;
  201. switch (item.type) {
  202. case InlineLevelIterator::Item::Type::ForcedBreak:
  203. line_builder.break_line();
  204. break;
  205. case InlineLevelIterator::Item::Type::Element: {
  206. auto& box = verify_cast<Layout::Box>(*item.node);
  207. line_builder.break_if_needed(item.border_box_width());
  208. line_builder.append_box(box, item.border_start + item.padding_start, item.padding_end + item.border_end, item.margin_start, item.margin_end);
  209. break;
  210. }
  211. case InlineLevelIterator::Item::Type::AbsolutelyPositionedElement:
  212. if (is<Box>(*item.node))
  213. parent().add_absolutely_positioned_box(static_cast<Layout::Box const&>(*item.node));
  214. break;
  215. case InlineLevelIterator::Item::Type::FloatingElement:
  216. if (is<Box>(*item.node))
  217. parent().layout_floating_box(static_cast<Layout::Box const&>(*item.node), containing_block(), layout_mode, *m_available_space, 0, &line_builder);
  218. break;
  219. case InlineLevelIterator::Item::Type::Text: {
  220. auto& text_node = verify_cast<Layout::TextNode>(*item.node);
  221. if (text_node.computed_values().white_space() != CSS::WhiteSpace::Nowrap && line_builder.break_if_needed(item.border_box_width())) {
  222. // If whitespace caused us to break, we swallow the whitespace instead of
  223. // putting it on the next line.
  224. // If we're in a whitespace-collapsing context, we can simply check the flag.
  225. if (item.is_collapsible_whitespace)
  226. break;
  227. // In whitespace-preserving contexts (white-space: pre*), we have to check manually.
  228. auto view = text_node.text_for_rendering().substring_view(item.offset_in_node, item.length_in_node);
  229. if (view.is_whitespace())
  230. break;
  231. }
  232. line_builder.append_text_chunk(
  233. text_node,
  234. item.offset_in_node,
  235. item.length_in_node,
  236. item.border_start + item.padding_start,
  237. item.padding_end + item.border_end,
  238. item.margin_start,
  239. item.margin_end,
  240. item.width,
  241. text_node.line_height());
  242. break;
  243. }
  244. }
  245. }
  246. for (auto& line_box : line_boxes) {
  247. line_box.trim_trailing_whitespace();
  248. }
  249. line_builder.remove_last_line_if_empty();
  250. auto const& containing_block = this->containing_block();
  251. auto text_align = containing_block.computed_values().text_align();
  252. auto text_justify = containing_block.computed_values().text_justify();
  253. if (text_align == CSS::TextAlign::Justify) {
  254. for (size_t i = 0; i < line_boxes.size(); i++) {
  255. auto& line_box = line_boxes[i];
  256. auto is_last_line = i == line_boxes.size() - 1;
  257. apply_justification_to_fragments(text_justify, line_box, is_last_line);
  258. }
  259. }
  260. }
  261. bool InlineFormattingContext::any_floats_intrude_at_y(float y) const
  262. {
  263. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state);
  264. float y_in_root = box_in_root_rect.y() + y;
  265. auto space = parent().space_used_by_floats(y_in_root);
  266. return space.left > 0 || space.right > 0;
  267. }
  268. bool InlineFormattingContext::can_fit_new_line_at_y(float y) const
  269. {
  270. auto box_in_root_rect = content_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state);
  271. float y_in_root = box_in_root_rect.y() + y;
  272. auto space_top = parent().space_used_by_floats(y_in_root);
  273. auto space_bottom = parent().space_used_by_floats(y_in_root + containing_block().line_height() - 1);
  274. [[maybe_unused]] auto top_left_edge = space_top.left;
  275. [[maybe_unused]] auto top_right_edge = m_available_space->width.to_px() - space_top.right;
  276. [[maybe_unused]] auto bottom_left_edge = space_bottom.left;
  277. [[maybe_unused]] auto bottom_right_edge = m_available_space->width.to_px() - space_bottom.right;
  278. if (top_left_edge > bottom_right_edge)
  279. return false;
  280. if (bottom_left_edge > top_right_edge)
  281. return false;
  282. return true;
  283. }
  284. }