LineBuilder.cpp 11 KB

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