LineBuilder.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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, LayoutMode layout_mode)
  10. : m_context(context)
  11. , m_formatting_state(formatting_state)
  12. , m_containing_block_state(formatting_state.get_mutable(context.containing_block()))
  13. , m_layout_mode(layout_mode)
  14. {
  15. begin_new_line(false);
  16. }
  17. LineBuilder::~LineBuilder()
  18. {
  19. if (m_last_line_needs_update)
  20. update_last_line();
  21. }
  22. void LineBuilder::break_line()
  23. {
  24. update_last_line();
  25. m_containing_block_state.line_boxes.append(LineBox());
  26. begin_new_line(true);
  27. }
  28. void LineBuilder::begin_new_line(bool increment_y)
  29. {
  30. if (increment_y)
  31. m_current_y += max(m_max_height_on_current_line, m_context.containing_block().line_height());
  32. switch (m_layout_mode) {
  33. case LayoutMode::Normal:
  34. m_available_width_for_current_line = m_context.available_space_for_line(m_current_y);
  35. break;
  36. case LayoutMode::MinContent:
  37. m_available_width_for_current_line = 0;
  38. break;
  39. case LayoutMode::MaxContent:
  40. m_available_width_for_current_line = INFINITY;
  41. break;
  42. }
  43. m_max_height_on_current_line = 0;
  44. m_last_line_needs_update = true;
  45. }
  46. LineBox& LineBuilder::ensure_last_line_box()
  47. {
  48. auto& line_boxes = m_containing_block_state.line_boxes;
  49. if (line_boxes.is_empty())
  50. line_boxes.append(LineBox {});
  51. return line_boxes.last();
  52. }
  53. void LineBuilder::append_box(Box const& box, float leading_size, float trailing_size, float leading_margin, float trailing_margin)
  54. {
  55. auto& box_state = m_formatting_state.get_mutable(box);
  56. auto& line_box = ensure_last_line_box();
  57. 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());
  58. m_max_height_on_current_line = max(m_max_height_on_current_line, box_state.border_box_height());
  59. box_state.containing_line_box_fragment = LineBoxFragmentCoordinate {
  60. .line_box_index = m_containing_block_state.line_boxes.size() - 1,
  61. .fragment_index = line_box.fragments().size() - 1,
  62. };
  63. }
  64. 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)
  65. {
  66. 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);
  67. m_max_height_on_current_line = max(m_max_height_on_current_line, content_height);
  68. }
  69. bool LineBuilder::should_break(LayoutMode layout_mode, float next_item_width, bool should_force_break)
  70. {
  71. if (layout_mode == LayoutMode::MinContent)
  72. return true;
  73. if (should_force_break)
  74. return true;
  75. if (layout_mode == LayoutMode::MaxContent)
  76. return false;
  77. auto const& line_boxes = m_containing_block_state.line_boxes;
  78. if (line_boxes.is_empty() || line_boxes.last().is_empty())
  79. return false;
  80. auto current_line_width = line_boxes.last().width();
  81. return (current_line_width + next_item_width) > m_available_width_for_current_line;
  82. }
  83. static float box_baseline(FormattingState const& state, Box const& box)
  84. {
  85. auto const& box_state = state.get(box);
  86. if (!box_state.line_boxes.is_empty())
  87. return box_state.offset.y() + box_state.line_boxes.last().baseline();
  88. if (box.has_children() && !box.children_are_inline()) {
  89. auto const* child_box = box.last_child_of_type<Box>();
  90. VERIFY(child_box);
  91. return box_baseline(state, *child_box);
  92. }
  93. return box_state.border_box_height();
  94. }
  95. void LineBuilder::update_last_line()
  96. {
  97. m_last_line_needs_update = false;
  98. auto& line_boxes = m_containing_block_state.line_boxes;
  99. if (line_boxes.is_empty())
  100. return;
  101. auto& line_box = line_boxes.last();
  102. auto text_align = m_context.containing_block().computed_values().text_align();
  103. float x_offset = m_context.leftmost_x_offset_at(m_current_y);
  104. float bottom = m_current_y + m_context.containing_block().line_height();
  105. float excess_horizontal_space = m_containing_block_state.content_width - line_box.width();
  106. switch (text_align) {
  107. case CSS::TextAlign::Center:
  108. case CSS::TextAlign::LibwebCenter:
  109. x_offset += excess_horizontal_space / 2;
  110. break;
  111. case CSS::TextAlign::Right:
  112. x_offset += excess_horizontal_space;
  113. break;
  114. case CSS::TextAlign::Left:
  115. case CSS::TextAlign::Justify:
  116. default:
  117. break;
  118. }
  119. auto fragment_baseline = [&](auto const& fragment) -> float {
  120. if (fragment.layout_node().is_text_node())
  121. return fragment.layout_node().font().baseline();
  122. auto const& box = verify_cast<Layout::Box>(fragment.layout_node());
  123. return box_baseline(m_formatting_state, box);
  124. };
  125. auto line_box_baseline = [&] {
  126. float line_box_baseline = 0;
  127. for (auto const& fragment : line_box.fragments()) {
  128. auto baseline = fragment_baseline(fragment);
  129. // NOTE: For fragments with a <length> vertical-align, shift the fragment baseline down by the length.
  130. // This ensures that we make enough vertical space on the line for any manually-aligned fragments.
  131. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  132. baseline += length_percentage->length().to_px(fragment.layout_node());
  133. line_box_baseline = max(line_box_baseline, baseline);
  134. }
  135. return line_box_baseline;
  136. }();
  137. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  138. auto& fragment = line_box.fragments()[i];
  139. float new_fragment_x = roundf(x_offset + fragment.offset().x());
  140. float new_fragment_y = 0;
  141. auto y_value_for_alignment = [&](CSS::VerticalAlign vertical_align) {
  142. switch (vertical_align) {
  143. case CSS::VerticalAlign::Baseline:
  144. case CSS::VerticalAlign::Bottom:
  145. case CSS::VerticalAlign::Middle:
  146. case CSS::VerticalAlign::Sub:
  147. case CSS::VerticalAlign::Super:
  148. case CSS::VerticalAlign::TextBottom:
  149. case CSS::VerticalAlign::TextTop:
  150. case CSS::VerticalAlign::Top:
  151. // FIXME: These are all 'baseline'
  152. return m_current_y + line_box_baseline - fragment_baseline(fragment) + fragment.border_box_top();
  153. }
  154. VERIFY_NOT_REACHED();
  155. };
  156. auto const& vertical_align = fragment.layout_node().computed_values().vertical_align();
  157. if (vertical_align.has<CSS::VerticalAlign>()) {
  158. new_fragment_y = y_value_for_alignment(vertical_align.get<CSS::VerticalAlign>());
  159. } else {
  160. if (auto length_percentage = vertical_align.get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length()) {
  161. auto vertical_align_amount = length_percentage->length().to_px(fragment.layout_node());
  162. new_fragment_y = y_value_for_alignment(CSS::VerticalAlign::Baseline) - vertical_align_amount;
  163. }
  164. }
  165. fragment.set_offset({ new_fragment_x, new_fragment_y });
  166. bottom = max(bottom, new_fragment_y + fragment.height() + fragment.border_box_bottom());
  167. }
  168. line_box.m_bottom = bottom;
  169. line_box.m_baseline = line_box_baseline;
  170. }
  171. void LineBuilder::remove_last_line_if_empty()
  172. {
  173. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  174. auto& line_boxes = m_containing_block_state.line_boxes;
  175. if (!line_boxes.is_empty() && line_boxes.last().fragments().is_empty()) {
  176. line_boxes.take_last();
  177. m_last_line_needs_update = false;
  178. }
  179. }
  180. void LineBuilder::adjust_last_line_after_inserting_floating_box(Badge<BlockFormattingContext>, CSS::Float float_, float space_used_by_float)
  181. {
  182. // NOTE: LineBuilder generates lines from left-to-right, so if we've just added a left-side float,
  183. // that means every fragment already on this line has to move towards the right.
  184. if (float_ == CSS::Float::Left && !m_containing_block_state.line_boxes.is_empty()) {
  185. for (auto& fragment : m_containing_block_state.line_boxes.last().fragments())
  186. fragment.set_offset(fragment.offset().translated(space_used_by_float, 0));
  187. m_containing_block_state.line_boxes.last().m_width += space_used_by_float;
  188. }
  189. m_available_width_for_current_line -= space_used_by_float;
  190. if (m_available_width_for_current_line < 0)
  191. m_available_width_for_current_line = 0;
  192. }
  193. }