LineBuilder.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/LineBuilder.h>
  7. #include <LibWeb/Layout/TextNode.h>
  8. namespace Web::Layout {
  9. LineBuilder::LineBuilder(InlineFormattingContext& context, FormattingState& formatting_state)
  10. : m_context(context)
  11. , m_formatting_state(formatting_state)
  12. , m_containing_block_state(formatting_state.get_mutable(context.containing_block()))
  13. {
  14. begin_new_line(false);
  15. }
  16. LineBuilder::~LineBuilder()
  17. {
  18. if (m_last_line_needs_update)
  19. update_last_line();
  20. }
  21. void LineBuilder::break_line()
  22. {
  23. update_last_line();
  24. m_containing_block_state.line_boxes.append(LineBox());
  25. begin_new_line(true);
  26. }
  27. void LineBuilder::begin_new_line(bool increment_y)
  28. {
  29. if (increment_y)
  30. m_current_y += max(m_max_height_on_current_line, m_context.containing_block().line_height());
  31. auto space = m_context.available_space_for_line(m_current_y);
  32. m_available_width_for_current_line = space.right - space.left;
  33. m_max_height_on_current_line = 0;
  34. m_last_line_needs_update = true;
  35. }
  36. LineBox& LineBuilder::ensure_last_line_box()
  37. {
  38. auto& line_boxes = m_containing_block_state.line_boxes;
  39. if (line_boxes.is_empty())
  40. line_boxes.append(LineBox {});
  41. return line_boxes.last();
  42. }
  43. void LineBuilder::append_box(Box const& box, float leading_size, float trailing_size, float leading_margin, float trailing_margin)
  44. {
  45. auto& box_state = m_formatting_state.get_mutable(box);
  46. auto& line_box = ensure_last_line_box();
  47. line_box.add_fragment(box, 0, 0, leading_size, trailing_size, leading_margin, trailing_margin, box_state.content_width, box_state.content_height, box_state.border_box_top(), box_state.border_box_bottom());
  48. m_max_height_on_current_line = max(m_max_height_on_current_line, box_state.border_box_height());
  49. box_state.containing_line_box_fragment = LineBoxFragmentCoordinate {
  50. .line_box_index = m_containing_block_state.line_boxes.size() - 1,
  51. .fragment_index = line_box.fragments().size() - 1,
  52. };
  53. }
  54. void LineBuilder::append_text_chunk(TextNode const& text_node, size_t offset_in_node, size_t length_in_node, float leading_size, float trailing_size, float leading_margin, float trailing_margin, float content_width, float content_height)
  55. {
  56. ensure_last_line_box().add_fragment(text_node, offset_in_node, length_in_node, leading_size, trailing_size, leading_margin, trailing_margin, content_width, content_height, 0, 0);
  57. m_max_height_on_current_line = max(m_max_height_on_current_line, content_height);
  58. }
  59. bool LineBuilder::should_break(LayoutMode layout_mode, float next_item_width, bool should_force_break)
  60. {
  61. if (layout_mode == LayoutMode::AllPossibleLineBreaks)
  62. return true;
  63. if (should_force_break)
  64. return true;
  65. if (layout_mode == LayoutMode::OnlyRequiredLineBreaks)
  66. return false;
  67. auto const& line_boxes = m_containing_block_state.line_boxes;
  68. if (line_boxes.is_empty() || line_boxes.last().is_empty())
  69. return false;
  70. auto current_line_width = line_boxes.last().width();
  71. return (current_line_width + next_item_width) > m_available_width_for_current_line;
  72. }
  73. static float box_baseline(FormattingState const& state, Box const& box)
  74. {
  75. auto const& box_state = state.get(box);
  76. if (!box_state.line_boxes.is_empty())
  77. return box_state.offset.y() + box_state.line_boxes.last().baseline();
  78. if (box.has_children() && !box.children_are_inline()) {
  79. auto const* child_box = box.last_child_of_type<Box>();
  80. VERIFY(child_box);
  81. return box_baseline(state, *child_box);
  82. }
  83. return box_state.border_box_height();
  84. }
  85. void LineBuilder::update_last_line()
  86. {
  87. m_last_line_needs_update = false;
  88. auto& line_boxes = m_containing_block_state.line_boxes;
  89. if (line_boxes.is_empty())
  90. return;
  91. auto& line_box = line_boxes.last();
  92. auto text_align = m_context.containing_block().computed_values().text_align();
  93. float x_offset = m_context.available_space_for_line(m_current_y).left;
  94. float bottom = m_current_y + m_context.containing_block().line_height();
  95. float excess_horizontal_space = m_containing_block_state.content_width - line_box.width();
  96. switch (text_align) {
  97. case CSS::TextAlign::Center:
  98. case CSS::TextAlign::LibwebCenter:
  99. x_offset += excess_horizontal_space / 2;
  100. break;
  101. case CSS::TextAlign::Right:
  102. x_offset += excess_horizontal_space;
  103. break;
  104. case CSS::TextAlign::Left:
  105. case CSS::TextAlign::Justify:
  106. default:
  107. break;
  108. }
  109. auto fragment_baseline = [&](auto const& fragment) -> float {
  110. if (fragment.layout_node().is_text_node())
  111. return fragment.layout_node().font().baseline();
  112. auto const& box = verify_cast<Layout::Box>(fragment.layout_node());
  113. return box_baseline(m_formatting_state, box);
  114. };
  115. auto line_box_baseline = [&] {
  116. float line_box_baseline = 0;
  117. for (auto const& fragment : line_box.fragments()) {
  118. auto baseline = fragment_baseline(fragment);
  119. // NOTE: For fragments with a <length> vertical-align, shift the fragment baseline down by the length.
  120. // This ensures that we make enough vertical space on the line for any manually-aligned fragments.
  121. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  122. baseline += length_percentage->length().to_px(fragment.layout_node());
  123. line_box_baseline = max(line_box_baseline, baseline);
  124. }
  125. return line_box_baseline;
  126. }();
  127. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  128. auto& fragment = line_box.fragments()[i];
  129. float new_fragment_x = roundf(x_offset + fragment.offset().x());
  130. float new_fragment_y = 0;
  131. auto y_value_for_alignment = [&](CSS::VerticalAlign vertical_align) {
  132. switch (vertical_align) {
  133. case CSS::VerticalAlign::Baseline:
  134. case CSS::VerticalAlign::Bottom:
  135. case CSS::VerticalAlign::Middle:
  136. case CSS::VerticalAlign::Sub:
  137. case CSS::VerticalAlign::Super:
  138. case CSS::VerticalAlign::TextBottom:
  139. case CSS::VerticalAlign::TextTop:
  140. case CSS::VerticalAlign::Top:
  141. // FIXME: These are all 'baseline'
  142. return m_current_y + line_box_baseline - fragment_baseline(fragment) + fragment.border_box_top();
  143. }
  144. VERIFY_NOT_REACHED();
  145. };
  146. auto const& vertical_align = fragment.layout_node().computed_values().vertical_align();
  147. if (vertical_align.has<CSS::VerticalAlign>()) {
  148. new_fragment_y = y_value_for_alignment(vertical_align.get<CSS::VerticalAlign>());
  149. } else {
  150. if (auto length_percentage = vertical_align.get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length()) {
  151. auto vertical_align_amount = length_percentage->length().to_px(fragment.layout_node());
  152. new_fragment_y = y_value_for_alignment(CSS::VerticalAlign::Baseline) - vertical_align_amount;
  153. }
  154. }
  155. fragment.set_offset({ new_fragment_x, new_fragment_y });
  156. bottom = max(bottom, new_fragment_y + fragment.height() + fragment.border_box_bottom());
  157. }
  158. line_box.m_bottom = bottom;
  159. line_box.m_baseline = line_box_baseline;
  160. }
  161. void LineBuilder::remove_last_line_if_empty()
  162. {
  163. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  164. auto& line_boxes = m_containing_block_state.line_boxes;
  165. if (!line_boxes.is_empty() && line_boxes.last().fragments().is_empty()) {
  166. line_boxes.take_last();
  167. m_last_line_needs_update = false;
  168. }
  169. }
  170. }