InlineFormattingContext.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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(FormattingState& state, BlockContainer const& containing_block, BlockFormattingContext& parent)
  20. : FormattingContext(Type::Inline, state, containing_block, &parent)
  21. {
  22. }
  23. InlineFormattingContext::~InlineFormattingContext() = default;
  24. BlockFormattingContext& InlineFormattingContext::parent()
  25. {
  26. return static_cast<BlockFormattingContext&>(*FormattingContext::parent());
  27. }
  28. BlockFormattingContext const& InlineFormattingContext::parent() const
  29. {
  30. return static_cast<BlockFormattingContext const&>(*FormattingContext::parent());
  31. }
  32. float InlineFormattingContext::leftmost_x_offset_at(float y) const
  33. {
  34. // NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
  35. auto box_in_root_rect = margin_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state);
  36. float y_in_root = box_in_root_rect.y() + y;
  37. auto space = parent().space_used_by_floats(y_in_root);
  38. float containing_block_x = m_state.get(containing_block()).offset.x();
  39. return max(space.left, containing_block_x) - containing_block_x;
  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 box_in_root_rect = margin_box_rect_in_ancestor_coordinate_space(containing_block(), parent().root(), m_state);
  45. float y_in_root = box_in_root_rect.y() + y;
  46. auto space = parent().space_used_by_floats(y_in_root);
  47. auto const& containing_block_state = m_state.get(containing_block());
  48. auto const& root_block_state = m_state.get(parent().root());
  49. space.left = max(space.left, containing_block_state.offset.x()) - containing_block_state.offset.x();
  50. space.right = min(root_block_state.content_width - space.right, containing_block_state.offset.x() + containing_block_state.content_width);
  51. return space.right - space.left;
  52. }
  53. void InlineFormattingContext::run(Box const&, LayoutMode layout_mode)
  54. {
  55. VERIFY(containing_block().children_are_inline());
  56. generate_line_boxes(layout_mode);
  57. float min_line_height = containing_block().line_height();
  58. float max_line_width = 0;
  59. float content_height = 0;
  60. for (auto& line_box : m_state.get(containing_block()).line_boxes) {
  61. float max_height = min_line_height;
  62. for (auto& fragment : line_box.fragments()) {
  63. max_height = max(max_height, fragment.border_box_height());
  64. }
  65. max_line_width = max(max_line_width, line_box.width());
  66. content_height += max_height;
  67. }
  68. auto& containing_block_state = m_state.get_mutable(containing_block());
  69. if (layout_mode != LayoutMode::Normal) {
  70. containing_block_state.content_width = max_line_width;
  71. }
  72. containing_block_state.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_state.get(containing_block()).content_width);
  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);
  95. box_state.content_width = compute_width_for_replaced_element(m_state, replaced);
  96. box_state.content_height = compute_height_for_replaced_element(m_state, replaced);
  97. return;
  98. }
  99. if (box.is_inline_block()) {
  100. auto const& inline_block = verify_cast<BlockContainer>(box);
  101. auto const& containing_block_state = m_state.get(containing_block());
  102. auto& width_value = inline_block.computed_values().width();
  103. if (!width_value.has_value() || (width_value->is_length() && width_value->length().is_auto())) {
  104. auto result = calculate_shrink_to_fit_widths(inline_block);
  105. auto available_width = containing_block_state.content_width
  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. auto width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  113. box_state.content_width = width;
  114. } else {
  115. auto container_width = CSS::Length::make_px(containing_block_state.content_width);
  116. box_state.content_width = width_value->resolved(box, container_width).to_px(inline_block);
  117. }
  118. auto independent_formatting_context = layout_inside(inline_block, layout_mode);
  119. auto& height_value = inline_block.computed_values().height();
  120. if (!height_value.has_value() || (height_value->is_length() && height_value->length().is_auto())) {
  121. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  122. BlockFormattingContext::compute_height(inline_block, m_state);
  123. } else {
  124. auto container_height = CSS::Length::make_px(containing_block_state.content_height);
  125. box_state.content_height = height_value->resolved(box, container_height).to_px(inline_block);
  126. }
  127. independent_formatting_context->parent_context_did_dimension_child_root_box();
  128. return;
  129. }
  130. // Non-replaced, non-inline-block, box on a line!?
  131. // I don't think we should be here. Dump the box tree so we can take a look at it.
  132. dbgln("FIXME: I've been asked to dimension a non-replaced, non-inline-block box on a line:");
  133. dump_tree(box);
  134. }
  135. void InlineFormattingContext::apply_justification_to_fragments(FormattingState::NodeState const& containing_block_state, CSS::TextJustify text_justify, LineBox& line_box, bool is_last_line)
  136. {
  137. switch (text_justify) {
  138. case CSS::TextJustify::None:
  139. return;
  140. // FIXME: These two cases currently fall back to auto, handle them as well.
  141. case CSS::TextJustify::InterCharacter:
  142. case CSS::TextJustify::InterWord:
  143. case CSS::TextJustify::Auto:
  144. break;
  145. }
  146. float excess_horizontal_space = containing_block_state.content_width - line_box.width();
  147. // Only justify the text if the excess horizontal space is less than or
  148. // equal to 10%, or if we are not looking at the last line box.
  149. if (is_last_line && excess_horizontal_space / containing_block_state.content_width > text_justification_threshold)
  150. return;
  151. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  152. size_t whitespace_count = 0;
  153. for (auto& fragment : line_box.fragments()) {
  154. if (fragment.is_justifiable_whitespace()) {
  155. ++whitespace_count;
  156. excess_horizontal_space_including_whitespace += fragment.width();
  157. }
  158. }
  159. float justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / static_cast<float>(whitespace_count)) : 0;
  160. // This is the amount that each fragment will be offset by. If a whitespace
  161. // fragment is shorter than the justified space width, it increases to push
  162. // subsequent fragments, and decreases to pull them back otherwise.
  163. float running_diff = 0;
  164. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  165. auto& fragment = line_box.fragments()[i];
  166. auto offset = fragment.offset();
  167. offset.translate_by(running_diff, 0);
  168. fragment.set_offset(offset);
  169. if (fragment.is_justifiable_whitespace()
  170. && fragment.width() != justified_space_width) {
  171. running_diff += justified_space_width - fragment.width();
  172. fragment.set_width(justified_space_width);
  173. }
  174. }
  175. }
  176. void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
  177. {
  178. auto& containing_block_state = m_state.get_mutable(containing_block());
  179. auto& line_boxes = containing_block_state.line_boxes;
  180. line_boxes.clear_with_capacity();
  181. InlineLevelIterator iterator(*this, m_state, containing_block(), layout_mode);
  182. LineBuilder line_builder(*this, m_state, layout_mode);
  183. for (;;) {
  184. auto item_opt = iterator.next(line_builder.available_width_for_current_line());
  185. if (!item_opt.has_value())
  186. break;
  187. auto& item = item_opt.value();
  188. // Ignore collapsible whitespace chunks at the start of line, and if the last fragment already ends in whitespace.
  189. if (item.is_collapsible_whitespace && line_boxes.last().is_empty_or_ends_in_whitespace())
  190. continue;
  191. switch (item.type) {
  192. case InlineLevelIterator::Item::Type::ForcedBreak:
  193. line_builder.break_line();
  194. break;
  195. case InlineLevelIterator::Item::Type::Element: {
  196. auto& box = verify_cast<Layout::Box>(*item.node);
  197. line_builder.break_if_needed(layout_mode, item.border_box_width(), item.should_force_break);
  198. line_builder.append_box(box, item.border_start + item.padding_start, item.padding_end + item.border_end, item.margin_start, item.margin_end);
  199. break;
  200. }
  201. case InlineLevelIterator::Item::Type::AbsolutelyPositionedElement:
  202. if (is<Box>(*item.node))
  203. parent().add_absolutely_positioned_box(static_cast<Layout::Box const&>(*item.node));
  204. break;
  205. case InlineLevelIterator::Item::Type::FloatingElement:
  206. if (is<Box>(*item.node))
  207. parent().layout_floating_box(static_cast<Layout::Box const&>(*item.node), containing_block(), layout_mode, &line_builder);
  208. break;
  209. case InlineLevelIterator::Item::Type::Text: {
  210. auto& text_node = verify_cast<Layout::TextNode>(*item.node);
  211. line_builder.break_if_needed(layout_mode, item.border_box_width(), item.should_force_break);
  212. line_builder.append_text_chunk(
  213. text_node,
  214. item.offset_in_node,
  215. item.length_in_node,
  216. item.border_start + item.padding_start,
  217. item.padding_end + item.border_end,
  218. item.margin_start,
  219. item.margin_end,
  220. item.width,
  221. text_node.computed_values().font_size());
  222. break;
  223. }
  224. }
  225. }
  226. for (auto& line_box : line_boxes) {
  227. line_box.trim_trailing_whitespace();
  228. }
  229. line_builder.remove_last_line_if_empty();
  230. auto const& containing_block = this->containing_block();
  231. auto text_align = containing_block.computed_values().text_align();
  232. auto text_justify = containing_block.computed_values().text_justify();
  233. if (text_align == CSS::TextAlign::Justify) {
  234. auto const& containing_block_state = m_state.get(containing_block);
  235. for (size_t i = 0; i < line_boxes.size(); i++) {
  236. auto& line_box = line_boxes[i];
  237. auto is_last_line = i == line_boxes.size() - 1;
  238. apply_justification_to_fragments(containing_block_state, text_justify, line_box, is_last_line);
  239. }
  240. }
  241. }
  242. }