LineBuilder.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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, LayoutState& layout_state, LayoutMode layout_mode)
  10. : m_context(context)
  11. , m_layout_state(layout_state)
  12. , m_containing_block_state(layout_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. m_available_width_for_current_line = m_context.available_space_for_line(m_current_y);
  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_layout_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(float next_item_width)
  60. {
  61. auto const& line_boxes = m_containing_block_state.line_boxes;
  62. if (line_boxes.is_empty() || line_boxes.last().is_empty())
  63. return false;
  64. auto current_line_width = line_boxes.last().width();
  65. return (current_line_width + next_item_width) > m_available_width_for_current_line;
  66. }
  67. static float box_baseline(LayoutState const& state, Box const& box)
  68. {
  69. auto const& box_state = state.get(box);
  70. auto const& vertical_align = box.computed_values().vertical_align();
  71. if (vertical_align.has<CSS::VerticalAlign>()) {
  72. switch (vertical_align.get<CSS::VerticalAlign>()) {
  73. case CSS::VerticalAlign::Top:
  74. return box_state.border_box_top();
  75. case CSS::VerticalAlign::Bottom:
  76. return box_state.content_height() + box_state.border_box_bottom();
  77. default:
  78. break;
  79. }
  80. }
  81. if (!box_state.line_boxes.is_empty())
  82. return box_state.border_box_top() + box_state.offset.y() + box_state.line_boxes.last().baseline();
  83. if (box.has_children() && !box.children_are_inline()) {
  84. auto const* child_box = box.last_child_of_type<Box>();
  85. VERIFY(child_box);
  86. return box_baseline(state, *child_box);
  87. }
  88. return box_state.border_box_height();
  89. }
  90. void LineBuilder::update_last_line()
  91. {
  92. m_last_line_needs_update = false;
  93. auto& line_boxes = m_containing_block_state.line_boxes;
  94. if (line_boxes.is_empty())
  95. return;
  96. auto& line_box = line_boxes.last();
  97. auto text_align = m_context.containing_block().computed_values().text_align();
  98. float x_offset = m_context.leftmost_x_offset_at(m_current_y);
  99. float excess_horizontal_space = m_context.effective_containing_block_width() - line_box.width();
  100. switch (text_align) {
  101. case CSS::TextAlign::Center:
  102. case CSS::TextAlign::LibwebCenter:
  103. x_offset += excess_horizontal_space / 2;
  104. break;
  105. case CSS::TextAlign::Right:
  106. x_offset += excess_horizontal_space;
  107. break;
  108. case CSS::TextAlign::Left:
  109. case CSS::TextAlign::Justify:
  110. default:
  111. break;
  112. }
  113. auto line_box_baseline = [&] {
  114. float line_box_baseline = 0;
  115. for (auto& fragment : line_box.fragments()) {
  116. auto const& font = fragment.layout_node().font();
  117. auto const line_height = fragment.layout_node().line_height();
  118. auto const font_metrics = font.pixel_metrics();
  119. auto const typographic_height = font_metrics.ascent + font_metrics.descent;
  120. auto const leading = line_height - typographic_height;
  121. auto const half_leading = leading / 2;
  122. // The CSS specification calls this AD (A+D, Ascent + Descent).
  123. float fragment_baseline = 0;
  124. if (fragment.layout_node().is_text_node()) {
  125. fragment_baseline = font_metrics.ascent;
  126. } else {
  127. auto const& box = verify_cast<Layout::Box>(fragment.layout_node());
  128. fragment_baseline = box_baseline(m_layout_state, box);
  129. }
  130. fragment_baseline += half_leading;
  131. // Remember the baseline used for this fragment. This will be used when painting the fragment.
  132. fragment.set_baseline(fragment_baseline);
  133. // NOTE: For fragments with a <length> vertical-align, shift the line box baseline down by the length.
  134. // This ensures that we make enough vertical space on the line for any manually-aligned fragments.
  135. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().template get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  136. fragment_baseline += length_percentage->length().to_px(fragment.layout_node());
  137. line_box_baseline = max(line_box_baseline, fragment_baseline);
  138. }
  139. return line_box_baseline;
  140. }();
  141. // Start with the "strut", an imaginary zero-width box at the start of each line box.
  142. auto strut_top = m_current_y;
  143. auto strut_bottom = m_current_y + m_context.containing_block().line_height();
  144. float uppermost_box_top = strut_top;
  145. float lowermost_box_bottom = strut_bottom;
  146. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  147. auto& fragment = line_box.fragments()[i];
  148. float new_fragment_x = roundf(x_offset + fragment.offset().x());
  149. float new_fragment_y = 0;
  150. auto y_value_for_alignment = [&](CSS::VerticalAlign vertical_align) {
  151. switch (vertical_align) {
  152. case CSS::VerticalAlign::Baseline:
  153. return m_current_y + line_box_baseline - fragment.baseline() + fragment.border_box_top();
  154. case CSS::VerticalAlign::Top:
  155. return m_current_y + fragment.border_box_top();
  156. case CSS::VerticalAlign::Middle:
  157. case CSS::VerticalAlign::Bottom:
  158. case CSS::VerticalAlign::Sub:
  159. case CSS::VerticalAlign::Super:
  160. case CSS::VerticalAlign::TextBottom:
  161. case CSS::VerticalAlign::TextTop:
  162. // FIXME: These are all 'baseline'
  163. return m_current_y + line_box_baseline - fragment.baseline() + fragment.border_box_top();
  164. }
  165. VERIFY_NOT_REACHED();
  166. };
  167. auto const& vertical_align = fragment.layout_node().computed_values().vertical_align();
  168. if (vertical_align.has<CSS::VerticalAlign>()) {
  169. new_fragment_y = y_value_for_alignment(vertical_align.get<CSS::VerticalAlign>());
  170. } else {
  171. if (auto length_percentage = vertical_align.get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length()) {
  172. auto vertical_align_amount = length_percentage->length().to_px(fragment.layout_node());
  173. new_fragment_y = y_value_for_alignment(CSS::VerticalAlign::Baseline) - vertical_align_amount;
  174. }
  175. }
  176. fragment.set_offset({ new_fragment_x, floorf(new_fragment_y) });
  177. float top_of_inline_box = 0;
  178. float bottom_of_inline_box = 0;
  179. {
  180. // FIXME: Support inline-table elements.
  181. if (fragment.layout_node().is_replaced_box() || fragment.layout_node().is_inline_block()) {
  182. auto const& fragment_box_state = m_layout_state.get(static_cast<Box const&>(fragment.layout_node()));
  183. top_of_inline_box = fragment.offset().y() - fragment_box_state.margin_box_top();
  184. bottom_of_inline_box = fragment.offset().y() + fragment_box_state.content_height() + fragment_box_state.margin_box_bottom();
  185. } else {
  186. auto font_metrics = fragment.layout_node().font().pixel_metrics();
  187. auto typographic_height = font_metrics.ascent + font_metrics.descent;
  188. auto leading = fragment.layout_node().line_height() - typographic_height;
  189. auto half_leading = leading / 2;
  190. top_of_inline_box = fragment.offset().y() + fragment.baseline() - font_metrics.ascent - half_leading;
  191. bottom_of_inline_box = fragment.offset().y() + fragment.baseline() + font_metrics.descent + half_leading;
  192. }
  193. if (auto length_percentage = fragment.layout_node().computed_values().vertical_align().template get_pointer<CSS::LengthPercentage>(); length_percentage && length_percentage->is_length())
  194. bottom_of_inline_box += length_percentage->length().to_px(fragment.layout_node());
  195. }
  196. uppermost_box_top = min(uppermost_box_top, top_of_inline_box);
  197. lowermost_box_bottom = max(lowermost_box_bottom, bottom_of_inline_box);
  198. }
  199. // 3. The line box height is the distance between the uppermost box top and the lowermost box bottom.
  200. line_box.m_height = lowermost_box_bottom - uppermost_box_top;
  201. line_box.m_bottom = m_current_y + line_box.m_height;
  202. line_box.m_baseline = line_box_baseline;
  203. }
  204. void LineBuilder::remove_last_line_if_empty()
  205. {
  206. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  207. auto& line_boxes = m_containing_block_state.line_boxes;
  208. if (!line_boxes.is_empty() && line_boxes.last().fragments().is_empty()) {
  209. line_boxes.take_last();
  210. m_last_line_needs_update = false;
  211. }
  212. }
  213. void LineBuilder::adjust_last_line_after_inserting_floating_box(Badge<BlockFormattingContext>, CSS::Float float_, float space_used_by_float)
  214. {
  215. // NOTE: LineBuilder generates lines from left-to-right, so if we've just added a left-side float,
  216. // that means every fragment already on this line has to move towards the right.
  217. if (float_ == CSS::Float::Left && !m_containing_block_state.line_boxes.is_empty()) {
  218. for (auto& fragment : m_containing_block_state.line_boxes.last().fragments())
  219. fragment.set_offset(fragment.offset().translated(space_used_by_float, 0));
  220. m_containing_block_state.line_boxes.last().m_width += space_used_by_float;
  221. }
  222. m_available_width_for_current_line -= space_used_by_float;
  223. if (m_available_width_for_current_line < 0)
  224. m_available_width_for_current_line = 0;
  225. }
  226. }