InlineFormattingContext.cpp 11 KB

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