InlineFormattingContext.cpp 11 KB

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