InlineFormattingContext.cpp 11 KB

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